Commit b7969b5
Yonghong Song
[BPF] Add support for asm gotol_or_nop and nop_or_gotol insns
Two new insns are added to BPF instruction set:
gotol_or_nop
encoding: gotol encoding with src_reg = 1
nop_or_gotol
encoding: gotol encoding with src_reg = 3
Basically src_reg 'bit_0 == 1' means it is gotol_or_nop/nop_or_gotol
insn. The src_reg 'bit_1' indicates the insn itself will be a
'goto' 'bit_1 == 0' or a 'nop' 'bit_1 == 1'.
Two insns intend to support kernel static key like transformation
where the insn can be a nop or a ja.
The following is an example, where two labels,
static_key_loc_1 and static_key_loc_2, can be used
to identify the location of a particular gotol_or_nop/nop_or_gotol
location.
It is possible that user space could do
static_key_enable("static_key_loc_1")
libbpf can validate that the label "static_key_loc_1" indeed
corresponds to a gotol_or_nop/nop_or_gotol insn and it
can translated the 'static_key_enable("static_key_loc_1")'
to something like bpf syscall command 'static_key_enable prog, insn offset 1'
and kernel will do proper adjustment.
the same for static_key_disable, static_key_enabled, etc.
```
$ cat t.c
int bar(void);
int foo1(int arg1)
{
int a = arg1, b;
asm volatile goto ("r0 = 0; \
static_key_loc_1: \
gotol_or_nop %l[label]; \
r2 = 2; \
r3 = 3; \
"::
: "r0", "r2", "r3"
:label);
a = bar();
label:
b = 20 * a;
return b;
}
int foo2(int arg1)
{
int a = arg1, b;
asm volatile goto ("r0 = 0; \
static_key_loc_2: \
nop_or_gotol %l[label]; \
r2 = 2; \
r3 = 3; \
"::
: "r0", "r2", "r3"
:label);
a = bar();
label:
b = 20 * a;
return b;
}
$ clang --target=bpf -O2 -g -c t.c
$ llvm-objdump -S t.o
t.o: file format elf64-bpf
Disassembly of section .text:
0000000000000000 <foo1>:
; asm volatile goto ("r0 = 0; \
0: b7 00 00 00 00 00 00 00 r0 = 0x0
0000000000000008 <static_key_loc_1>:
1: 06 10 00 00 04 00 00 00 gotol_or_nop +0x4 <LBB0_2>
2: b7 02 00 00 02 00 00 00 r2 = 0x2
3: b7 03 00 00 03 00 00 00 r3 = 0x3
; a = bar();
4: 85 10 00 00 ff ff ff ff call -0x1
5: bf 01 00 00 00 00 00 00 r1 = r0
0000000000000030 <LBB0_2>:
; b = 20 * a;
6: 27 01 00 00 14 00 00 00 r1 *= 0x14
; return b;
7: bf 10 00 00 00 00 00 00 r0 = r1
8: 95 00 00 00 00 00 00 00 exit
0000000000000048 <foo2>:
; asm volatile goto ("r0 = 0; \
9: b7 00 00 00 00 00 00 00 r0 = 0x0
0000000000000050 <static_key_loc_2>:
10: 06 30 00 00 04 00 00 00 nop_or_gotol +0x4 <LBB1_2>
11: b7 02 00 00 02 00 00 00 r2 = 0x2
12: b7 03 00 00 03 00 00 00 r3 = 0x3
; a = bar();
13: 85 10 00 00 ff ff ff ff call -0x1
14: bf 01 00 00 00 00 00 00 r1 = r0
0000000000000078 <LBB1_2>:
; b = 20 * a;
15: 27 01 00 00 14 00 00 00 r1 *= 0x14
; return b;
16: bf 10 00 00 00 00 00 00 r0 = r1
17: 95 00 00 00 00 00 00 00 exit
```1 parent 6e761f3 commit b7969b5
File tree
4 files changed
+36
-2
lines changed- llvm/lib/Target/BPF
- AsmParser
- MCTargetDesc
4 files changed
+36
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| 234 | + | |
| 235 | + | |
234 | 236 | | |
235 | 237 | | |
236 | 238 | | |
| |||
259 | 261 | | |
260 | 262 | | |
261 | 263 | | |
| 264 | + | |
| 265 | + | |
262 | 266 | | |
263 | 267 | | |
264 | 268 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
604 | 604 | | |
605 | 605 | | |
606 | 606 | | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
607 | 633 | | |
608 | 634 | | |
609 | 635 | | |
| |||
632 | 658 | | |
633 | 659 | | |
634 | 660 | | |
| 661 | + | |
| 662 | + | |
635 | 663 | | |
636 | 664 | | |
637 | 665 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
| 106 | + | |
| 107 | + | |
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
| 100 | + | |
100 | 101 | | |
101 | 102 | | |
102 | 103 | | |
| |||
0 commit comments