1 // SPDX-License-Identifier: GPL-2.0-only
3 * bpf_jit_comp.c: BPF JIT compiler
5 * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
6 * Internal BPF Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
8 #include <linux/netdevice.h>
9 #include <linux/filter.h>
10 #include <linux/if_vlan.h>
11 #include <linux/bpf.h>
12 #include <linux/memory.h>
13 #include <linux/sort.h>
14 #include <asm/extable.h>
15 #include <asm/set_memory.h>
16 #include <asm/nospec-branch.h>
17 #include <asm/text-patching.h>
18 #include <asm/asm-prototypes.h>
20 static u8
*emit_code(u8
*ptr
, u32 bytes
, unsigned int len
)
33 #define EMIT(bytes, len) \
34 do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
36 #define EMIT1(b1) EMIT(b1, 1)
37 #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
38 #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
39 #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
41 #define EMIT1_off32(b1, off) \
42 do { EMIT1(b1); EMIT(off, 4); } while (0)
43 #define EMIT2_off32(b1, b2, off) \
44 do { EMIT2(b1, b2); EMIT(off, 4); } while (0)
45 #define EMIT3_off32(b1, b2, b3, off) \
46 do { EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
47 #define EMIT4_off32(b1, b2, b3, b4, off) \
48 do { EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
50 static bool is_imm8(int value
)
52 return value
<= 127 && value
>= -128;
55 static bool is_simm32(s64 value
)
57 return value
== (s64
)(s32
)value
;
60 static bool is_uimm32(u64 value
)
62 return value
== (u64
)(u32
)value
;
66 #define EMIT_mov(DST, SRC) \
69 EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
72 static int bpf_size_to_x86_bytes(int bpf_size
)
74 if (bpf_size
== BPF_W
)
76 else if (bpf_size
== BPF_H
)
78 else if (bpf_size
== BPF_B
)
80 else if (bpf_size
== BPF_DW
)
87 * List of x86 cond jumps opcodes (. + s8)
88 * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
101 /* Pick a register outside of BPF range for JIT internal work */
102 #define AUX_REG (MAX_BPF_JIT_REG + 1)
103 #define X86_REG_R9 (MAX_BPF_JIT_REG + 2)
106 * The following table maps BPF registers to x86-64 registers.
108 * x86-64 register R12 is unused, since if used as base address
109 * register in load/store instructions, it always needs an
110 * extra byte of encoding and is callee saved.
112 * x86-64 register R9 is not used by BPF programs, but can be used by BPF
113 * trampoline. x86-64 register R10 is used for blinding (if enabled).
115 static const int reg2hex
[] = {
116 [BPF_REG_0
] = 0, /* RAX */
117 [BPF_REG_1
] = 7, /* RDI */
118 [BPF_REG_2
] = 6, /* RSI */
119 [BPF_REG_3
] = 2, /* RDX */
120 [BPF_REG_4
] = 1, /* RCX */
121 [BPF_REG_5
] = 0, /* R8 */
122 [BPF_REG_6
] = 3, /* RBX callee saved */
123 [BPF_REG_7
] = 5, /* R13 callee saved */
124 [BPF_REG_8
] = 6, /* R14 callee saved */
125 [BPF_REG_9
] = 7, /* R15 callee saved */
126 [BPF_REG_FP
] = 5, /* RBP readonly */
127 [BPF_REG_AX
] = 2, /* R10 temp register */
128 [AUX_REG
] = 3, /* R11 temp register */
129 [X86_REG_R9
] = 1, /* R9 register, 6th function argument */
132 static const int reg2pt_regs
[] = {
133 [BPF_REG_0
] = offsetof(struct pt_regs
, ax
),
134 [BPF_REG_1
] = offsetof(struct pt_regs
, di
),
135 [BPF_REG_2
] = offsetof(struct pt_regs
, si
),
136 [BPF_REG_3
] = offsetof(struct pt_regs
, dx
),
137 [BPF_REG_4
] = offsetof(struct pt_regs
, cx
),
138 [BPF_REG_5
] = offsetof(struct pt_regs
, r8
),
139 [BPF_REG_6
] = offsetof(struct pt_regs
, bx
),
140 [BPF_REG_7
] = offsetof(struct pt_regs
, r13
),
141 [BPF_REG_8
] = offsetof(struct pt_regs
, r14
),
142 [BPF_REG_9
] = offsetof(struct pt_regs
, r15
),
146 * is_ereg() == true if BPF register 'reg' maps to x86-64 r8..r15
147 * which need extra byte of encoding.
148 * rax,rcx,...,rbp have simpler encoding
150 static bool is_ereg(u32 reg
)
152 return (1 << reg
) & (BIT(BPF_REG_5
) |
162 * is_ereg_8l() == true if BPF register 'reg' is mapped to access x86-64
163 * lower 8-bit registers dil,sil,bpl,spl,r8b..r15b, which need extra byte
164 * of encoding. al,cl,dl,bl have simpler encoding.
166 static bool is_ereg_8l(u32 reg
)
168 return is_ereg(reg
) ||
169 (1 << reg
) & (BIT(BPF_REG_1
) |
174 static bool is_axreg(u32 reg
)
176 return reg
== BPF_REG_0
;
179 /* Add modifiers if 'reg' maps to x86-64 registers R8..R15 */
180 static u8
add_1mod(u8 byte
, u32 reg
)
187 static u8
add_2mod(u8 byte
, u32 r1
, u32 r2
)
196 /* Encode 'dst_reg' register into x86-64 opcode 'byte' */
197 static u8
add_1reg(u8 byte
, u32 dst_reg
)
199 return byte
+ reg2hex
[dst_reg
];
202 /* Encode 'dst_reg' and 'src_reg' registers into x86-64 opcode 'byte' */
203 static u8
add_2reg(u8 byte
, u32 dst_reg
, u32 src_reg
)
205 return byte
+ reg2hex
[dst_reg
] + (reg2hex
[src_reg
] << 3);
208 static void jit_fill_hole(void *area
, unsigned int size
)
210 /* Fill whole space with INT3 instructions */
211 memset(area
, 0xcc, size
);
215 int cleanup_addr
; /* Epilogue code offset */
218 /* Maximum number of bytes emitted while JITing one eBPF insn */
219 #define BPF_MAX_INSN_SIZE 128
220 #define BPF_INSN_SAFETY 64
222 /* Number of bytes emit_patch() needs to generate instructions */
223 #define X86_PATCH_SIZE 5
225 #define PROLOGUE_SIZE 25
228 * Emit x86-64 prologue code for BPF program and check its size.
229 * bpf_tail_call helper will skip it while jumping into another program
231 static void emit_prologue(u8
**pprog
, u32 stack_depth
, bool ebpf_from_cbpf
)
234 int cnt
= X86_PATCH_SIZE
;
236 /* BPF trampoline can be made to work without these nops,
237 * but let's waste 5 bytes for now and optimize later
239 memcpy(prog
, ideal_nops
[NOP_ATOMIC5
], cnt
);
241 EMIT1(0x55); /* push rbp */
242 EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
243 /* sub rsp, rounded_stack_depth */
244 EMIT3_off32(0x48, 0x81, 0xEC, round_up(stack_depth
, 8));
245 EMIT1(0x53); /* push rbx */
246 EMIT2(0x41, 0x55); /* push r13 */
247 EMIT2(0x41, 0x56); /* push r14 */
248 EMIT2(0x41, 0x57); /* push r15 */
249 if (!ebpf_from_cbpf
) {
250 /* zero init tail_call_cnt */
252 BUILD_BUG_ON(cnt
!= PROLOGUE_SIZE
);
257 static int emit_patch(u8
**pprog
, void *func
, void *ip
, u8 opcode
)
263 offset
= func
- (ip
+ X86_PATCH_SIZE
);
264 if (!is_simm32(offset
)) {
265 pr_err("Target call %p is out of range\n", func
);
268 EMIT1_off32(opcode
, offset
);
273 static int emit_call(u8
**pprog
, void *func
, void *ip
)
275 return emit_patch(pprog
, func
, ip
, 0xE8);
278 static int emit_jump(u8
**pprog
, void *func
, void *ip
)
280 return emit_patch(pprog
, func
, ip
, 0xE9);
283 static int __bpf_arch_text_poke(void *ip
, enum bpf_text_poke_type t
,
284 void *old_addr
, void *new_addr
,
285 const bool text_live
)
287 const u8
*nop_insn
= ideal_nops
[NOP_ATOMIC5
];
288 u8 old_insn
[X86_PATCH_SIZE
];
289 u8 new_insn
[X86_PATCH_SIZE
];
293 memcpy(old_insn
, nop_insn
, X86_PATCH_SIZE
);
296 ret
= t
== BPF_MOD_CALL
?
297 emit_call(&prog
, old_addr
, ip
) :
298 emit_jump(&prog
, old_addr
, ip
);
303 memcpy(new_insn
, nop_insn
, X86_PATCH_SIZE
);
306 ret
= t
== BPF_MOD_CALL
?
307 emit_call(&prog
, new_addr
, ip
) :
308 emit_jump(&prog
, new_addr
, ip
);
314 mutex_lock(&text_mutex
);
315 if (memcmp(ip
, old_insn
, X86_PATCH_SIZE
))
317 if (memcmp(ip
, new_insn
, X86_PATCH_SIZE
)) {
319 text_poke_bp(ip
, new_insn
, X86_PATCH_SIZE
, NULL
);
321 memcpy(ip
, new_insn
, X86_PATCH_SIZE
);
325 mutex_unlock(&text_mutex
);
329 int bpf_arch_text_poke(void *ip
, enum bpf_text_poke_type t
,
330 void *old_addr
, void *new_addr
)
332 if (!is_kernel_text((long)ip
) &&
333 !is_bpf_text_address((long)ip
))
334 /* BPF poking in modules is not supported */
337 return __bpf_arch_text_poke(ip
, t
, old_addr
, new_addr
, true);
341 * Generate the following code:
343 * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
344 * if (index >= array->map.max_entries)
346 * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
348 * prog = array->ptrs[index];
351 * goto *(prog->bpf_func + prologue_size);
354 static void emit_bpf_tail_call_indirect(u8
**pprog
)
357 int label1
, label2
, label3
;
361 * rdi - pointer to ctx
362 * rsi - pointer to bpf_array
363 * rdx - index in bpf_array
367 * if (index >= array->map.max_entries)
370 EMIT2(0x89, 0xD2); /* mov edx, edx */
371 EMIT3(0x39, 0x56, /* cmp dword ptr [rsi + 16], edx */
372 offsetof(struct bpf_array
, map
.max_entries
));
373 #define OFFSET1 (41 + RETPOLINE_RAX_BPF_JIT_SIZE) /* Number of bytes to jump */
374 EMIT2(X86_JBE
, OFFSET1
); /* jbe out */
378 * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
381 EMIT2_off32(0x8B, 0x85, -36 - MAX_BPF_STACK
); /* mov eax, dword ptr [rbp - 548] */
382 EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT
); /* cmp eax, MAX_TAIL_CALL_CNT */
383 #define OFFSET2 (30 + RETPOLINE_RAX_BPF_JIT_SIZE)
384 EMIT2(X86_JA
, OFFSET2
); /* ja out */
386 EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
387 EMIT2_off32(0x89, 0x85, -36 - MAX_BPF_STACK
); /* mov dword ptr [rbp -548], eax */
389 /* prog = array->ptrs[index]; */
390 EMIT4_off32(0x48, 0x8B, 0x84, 0xD6, /* mov rax, [rsi + rdx * 8 + offsetof(...)] */
391 offsetof(struct bpf_array
, ptrs
));
397 EMIT3(0x48, 0x85, 0xC0); /* test rax,rax */
398 #define OFFSET3 (8 + RETPOLINE_RAX_BPF_JIT_SIZE)
399 EMIT2(X86_JE
, OFFSET3
); /* je out */
402 /* goto *(prog->bpf_func + prologue_size); */
403 EMIT4(0x48, 0x8B, 0x40, /* mov rax, qword ptr [rax + 32] */
404 offsetof(struct bpf_prog
, bpf_func
));
405 EMIT4(0x48, 0x83, 0xC0, PROLOGUE_SIZE
); /* add rax, prologue_size */
408 * Wow we're ready to jump into next BPF program
409 * rdi == ctx (1st arg)
410 * rax == prog->bpf_func + prologue_size
412 RETPOLINE_RAX_BPF_JIT();
415 BUILD_BUG_ON(cnt
- label1
!= OFFSET1
);
416 BUILD_BUG_ON(cnt
- label2
!= OFFSET2
);
417 BUILD_BUG_ON(cnt
- label3
!= OFFSET3
);
421 static void emit_bpf_tail_call_direct(struct bpf_jit_poke_descriptor
*poke
,
422 u8
**pprog
, int addr
, u8
*image
)
428 * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
431 EMIT2_off32(0x8B, 0x85, -36 - MAX_BPF_STACK
); /* mov eax, dword ptr [rbp - 548] */
432 EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT
); /* cmp eax, MAX_TAIL_CALL_CNT */
433 EMIT2(X86_JA
, 14); /* ja out */
434 EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
435 EMIT2_off32(0x89, 0x85, -36 - MAX_BPF_STACK
); /* mov dword ptr [rbp -548], eax */
437 poke
->ip
= image
+ (addr
- X86_PATCH_SIZE
);
438 poke
->adj_off
= PROLOGUE_SIZE
;
440 memcpy(prog
, ideal_nops
[NOP_ATOMIC5
], X86_PATCH_SIZE
);
441 prog
+= X86_PATCH_SIZE
;
447 static void bpf_tail_call_direct_fixup(struct bpf_prog
*prog
)
449 struct bpf_jit_poke_descriptor
*poke
;
450 struct bpf_array
*array
;
451 struct bpf_prog
*target
;
454 for (i
= 0; i
< prog
->aux
->size_poke_tab
; i
++) {
455 poke
= &prog
->aux
->poke_tab
[i
];
456 WARN_ON_ONCE(READ_ONCE(poke
->ip_stable
));
458 if (poke
->reason
!= BPF_POKE_REASON_TAIL_CALL
)
461 array
= container_of(poke
->tail_call
.map
, struct bpf_array
, map
);
462 mutex_lock(&array
->aux
->poke_mutex
);
463 target
= array
->ptrs
[poke
->tail_call
.key
];
465 /* Plain memcpy is used when image is not live yet
466 * and still not locked as read-only. Once poke
467 * location is active (poke->ip_stable), any parallel
468 * bpf_arch_text_poke() might occur still on the
469 * read-write image until we finally locked it as
470 * read-only. Both modifications on the given image
471 * are under text_mutex to avoid interference.
473 ret
= __bpf_arch_text_poke(poke
->ip
, BPF_MOD_JUMP
, NULL
,
474 (u8
*)target
->bpf_func
+
475 poke
->adj_off
, false);
478 WRITE_ONCE(poke
->ip_stable
, true);
479 mutex_unlock(&array
->aux
->poke_mutex
);
483 static void emit_mov_imm32(u8
**pprog
, bool sign_propagate
,
484 u32 dst_reg
, const u32 imm32
)
491 * Optimization: if imm32 is positive, use 'mov %eax, imm32'
492 * (which zero-extends imm32) to save 2 bytes.
494 if (sign_propagate
&& (s32
)imm32
< 0) {
495 /* 'mov %rax, imm32' sign extends imm32 */
496 b1
= add_1mod(0x48, dst_reg
);
499 EMIT3_off32(b1
, b2
, add_1reg(b3
, dst_reg
), imm32
);
504 * Optimization: if imm32 is zero, use 'xor %eax, %eax'
508 if (is_ereg(dst_reg
))
509 EMIT1(add_2mod(0x40, dst_reg
, dst_reg
));
512 EMIT2(b2
, add_2reg(b3
, dst_reg
, dst_reg
));
516 /* mov %eax, imm32 */
517 if (is_ereg(dst_reg
))
518 EMIT1(add_1mod(0x40, dst_reg
));
519 EMIT1_off32(add_1reg(0xB8, dst_reg
), imm32
);
524 static void emit_mov_imm64(u8
**pprog
, u32 dst_reg
,
525 const u32 imm32_hi
, const u32 imm32_lo
)
530 if (is_uimm32(((u64
)imm32_hi
<< 32) | (u32
)imm32_lo
)) {
532 * For emitting plain u32, where sign bit must not be
533 * propagated LLVM tends to load imm64 over mov32
534 * directly, so save couple of bytes by just doing
535 * 'mov %eax, imm32' instead.
537 emit_mov_imm32(&prog
, false, dst_reg
, imm32_lo
);
539 /* movabsq %rax, imm64 */
540 EMIT2(add_1mod(0x48, dst_reg
), add_1reg(0xB8, dst_reg
));
548 static void emit_mov_reg(u8
**pprog
, bool is64
, u32 dst_reg
, u32 src_reg
)
555 EMIT_mov(dst_reg
, src_reg
);
558 if (is_ereg(dst_reg
) || is_ereg(src_reg
))
559 EMIT1(add_2mod(0x40, dst_reg
, src_reg
));
560 EMIT2(0x89, add_2reg(0xC0, dst_reg
, src_reg
));
566 /* LDX: dst_reg = *(u8*)(src_reg + off) */
567 static void emit_ldx(u8
**pprog
, u32 size
, u32 dst_reg
, u32 src_reg
, int off
)
574 /* Emit 'movzx rax, byte ptr [rax + off]' */
575 EMIT3(add_2mod(0x48, src_reg
, dst_reg
), 0x0F, 0xB6);
578 /* Emit 'movzx rax, word ptr [rax + off]' */
579 EMIT3(add_2mod(0x48, src_reg
, dst_reg
), 0x0F, 0xB7);
582 /* Emit 'mov eax, dword ptr [rax+0x14]' */
583 if (is_ereg(dst_reg
) || is_ereg(src_reg
))
584 EMIT2(add_2mod(0x40, src_reg
, dst_reg
), 0x8B);
589 /* Emit 'mov rax, qword ptr [rax+0x14]' */
590 EMIT2(add_2mod(0x48, src_reg
, dst_reg
), 0x8B);
594 * If insn->off == 0 we can save one extra byte, but
595 * special case of x86 R13 which always needs an offset
596 * is not worth the hassle
599 EMIT2(add_2reg(0x40, src_reg
, dst_reg
), off
);
601 EMIT1_off32(add_2reg(0x80, src_reg
, dst_reg
), off
);
605 /* STX: *(u8*)(dst_reg + off) = src_reg */
606 static void emit_stx(u8
**pprog
, u32 size
, u32 dst_reg
, u32 src_reg
, int off
)
613 /* Emit 'mov byte ptr [rax + off], al' */
614 if (is_ereg(dst_reg
) || is_ereg_8l(src_reg
))
615 /* Add extra byte for eregs or SIL,DIL,BPL in src_reg */
616 EMIT2(add_2mod(0x40, dst_reg
, src_reg
), 0x88);
621 if (is_ereg(dst_reg
) || is_ereg(src_reg
))
622 EMIT3(0x66, add_2mod(0x40, dst_reg
, src_reg
), 0x89);
627 if (is_ereg(dst_reg
) || is_ereg(src_reg
))
628 EMIT2(add_2mod(0x40, dst_reg
, src_reg
), 0x89);
633 EMIT2(add_2mod(0x48, dst_reg
, src_reg
), 0x89);
637 EMIT2(add_2reg(0x40, dst_reg
, src_reg
), off
);
639 EMIT1_off32(add_2reg(0x80, dst_reg
, src_reg
), off
);
643 static bool ex_handler_bpf(const struct exception_table_entry
*x
,
644 struct pt_regs
*regs
, int trapnr
,
645 unsigned long error_code
, unsigned long fault_addr
)
647 u32 reg
= x
->fixup
>> 8;
649 /* jump over faulting load and clear dest register */
650 *(unsigned long *)((void *)regs
+ reg
) = 0;
651 regs
->ip
+= x
->fixup
& 0xff;
655 static int do_jit(struct bpf_prog
*bpf_prog
, int *addrs
, u8
*image
,
656 int oldproglen
, struct jit_context
*ctx
)
658 struct bpf_insn
*insn
= bpf_prog
->insnsi
;
659 int insn_cnt
= bpf_prog
->len
;
660 bool seen_exit
= false;
661 u8 temp
[BPF_MAX_INSN_SIZE
+ BPF_INSN_SAFETY
];
662 int i
, cnt
= 0, excnt
= 0;
666 emit_prologue(&prog
, bpf_prog
->aux
->stack_depth
,
667 bpf_prog_was_classic(bpf_prog
));
668 addrs
[0] = prog
- temp
;
670 for (i
= 1; i
<= insn_cnt
; i
++, insn
++) {
671 const s32 imm32
= insn
->imm
;
672 u32 dst_reg
= insn
->dst_reg
;
673 u32 src_reg
= insn
->src_reg
;
680 switch (insn
->code
) {
682 case BPF_ALU
| BPF_ADD
| BPF_X
:
683 case BPF_ALU
| BPF_SUB
| BPF_X
:
684 case BPF_ALU
| BPF_AND
| BPF_X
:
685 case BPF_ALU
| BPF_OR
| BPF_X
:
686 case BPF_ALU
| BPF_XOR
| BPF_X
:
687 case BPF_ALU64
| BPF_ADD
| BPF_X
:
688 case BPF_ALU64
| BPF_SUB
| BPF_X
:
689 case BPF_ALU64
| BPF_AND
| BPF_X
:
690 case BPF_ALU64
| BPF_OR
| BPF_X
:
691 case BPF_ALU64
| BPF_XOR
| BPF_X
:
692 switch (BPF_OP(insn
->code
)) {
693 case BPF_ADD
: b2
= 0x01; break;
694 case BPF_SUB
: b2
= 0x29; break;
695 case BPF_AND
: b2
= 0x21; break;
696 case BPF_OR
: b2
= 0x09; break;
697 case BPF_XOR
: b2
= 0x31; break;
699 if (BPF_CLASS(insn
->code
) == BPF_ALU64
)
700 EMIT1(add_2mod(0x48, dst_reg
, src_reg
));
701 else if (is_ereg(dst_reg
) || is_ereg(src_reg
))
702 EMIT1(add_2mod(0x40, dst_reg
, src_reg
));
703 EMIT2(b2
, add_2reg(0xC0, dst_reg
, src_reg
));
706 case BPF_ALU64
| BPF_MOV
| BPF_X
:
707 case BPF_ALU
| BPF_MOV
| BPF_X
:
709 BPF_CLASS(insn
->code
) == BPF_ALU64
,
714 case BPF_ALU
| BPF_NEG
:
715 case BPF_ALU64
| BPF_NEG
:
716 if (BPF_CLASS(insn
->code
) == BPF_ALU64
)
717 EMIT1(add_1mod(0x48, dst_reg
));
718 else if (is_ereg(dst_reg
))
719 EMIT1(add_1mod(0x40, dst_reg
));
720 EMIT2(0xF7, add_1reg(0xD8, dst_reg
));
723 case BPF_ALU
| BPF_ADD
| BPF_K
:
724 case BPF_ALU
| BPF_SUB
| BPF_K
:
725 case BPF_ALU
| BPF_AND
| BPF_K
:
726 case BPF_ALU
| BPF_OR
| BPF_K
:
727 case BPF_ALU
| BPF_XOR
| BPF_K
:
728 case BPF_ALU64
| BPF_ADD
| BPF_K
:
729 case BPF_ALU64
| BPF_SUB
| BPF_K
:
730 case BPF_ALU64
| BPF_AND
| BPF_K
:
731 case BPF_ALU64
| BPF_OR
| BPF_K
:
732 case BPF_ALU64
| BPF_XOR
| BPF_K
:
733 if (BPF_CLASS(insn
->code
) == BPF_ALU64
)
734 EMIT1(add_1mod(0x48, dst_reg
));
735 else if (is_ereg(dst_reg
))
736 EMIT1(add_1mod(0x40, dst_reg
));
739 * b3 holds 'normal' opcode, b2 short form only valid
740 * in case dst is eax/rax.
742 switch (BPF_OP(insn
->code
)) {
766 EMIT3(0x83, add_1reg(b3
, dst_reg
), imm32
);
767 else if (is_axreg(dst_reg
))
768 EMIT1_off32(b2
, imm32
);
770 EMIT2_off32(0x81, add_1reg(b3
, dst_reg
), imm32
);
773 case BPF_ALU64
| BPF_MOV
| BPF_K
:
774 case BPF_ALU
| BPF_MOV
| BPF_K
:
775 emit_mov_imm32(&prog
, BPF_CLASS(insn
->code
) == BPF_ALU64
,
779 case BPF_LD
| BPF_IMM
| BPF_DW
:
780 emit_mov_imm64(&prog
, dst_reg
, insn
[1].imm
, insn
[0].imm
);
785 /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
786 case BPF_ALU
| BPF_MOD
| BPF_X
:
787 case BPF_ALU
| BPF_DIV
| BPF_X
:
788 case BPF_ALU
| BPF_MOD
| BPF_K
:
789 case BPF_ALU
| BPF_DIV
| BPF_K
:
790 case BPF_ALU64
| BPF_MOD
| BPF_X
:
791 case BPF_ALU64
| BPF_DIV
| BPF_X
:
792 case BPF_ALU64
| BPF_MOD
| BPF_K
:
793 case BPF_ALU64
| BPF_DIV
| BPF_K
:
794 EMIT1(0x50); /* push rax */
795 EMIT1(0x52); /* push rdx */
797 if (BPF_SRC(insn
->code
) == BPF_X
)
798 /* mov r11, src_reg */
799 EMIT_mov(AUX_REG
, src_reg
);
802 EMIT3_off32(0x49, 0xC7, 0xC3, imm32
);
804 /* mov rax, dst_reg */
805 EMIT_mov(BPF_REG_0
, dst_reg
);
809 * equivalent to 'xor rdx, rdx', but one byte less
813 if (BPF_CLASS(insn
->code
) == BPF_ALU64
)
815 EMIT3(0x49, 0xF7, 0xF3);
818 EMIT3(0x41, 0xF7, 0xF3);
820 if (BPF_OP(insn
->code
) == BPF_MOD
)
822 EMIT3(0x49, 0x89, 0xD3);
825 EMIT3(0x49, 0x89, 0xC3);
827 EMIT1(0x5A); /* pop rdx */
828 EMIT1(0x58); /* pop rax */
830 /* mov dst_reg, r11 */
831 EMIT_mov(dst_reg
, AUX_REG
);
834 case BPF_ALU
| BPF_MUL
| BPF_K
:
835 case BPF_ALU
| BPF_MUL
| BPF_X
:
836 case BPF_ALU64
| BPF_MUL
| BPF_K
:
837 case BPF_ALU64
| BPF_MUL
| BPF_X
:
839 bool is64
= BPF_CLASS(insn
->code
) == BPF_ALU64
;
841 if (dst_reg
!= BPF_REG_0
)
842 EMIT1(0x50); /* push rax */
843 if (dst_reg
!= BPF_REG_3
)
844 EMIT1(0x52); /* push rdx */
846 /* mov r11, dst_reg */
847 EMIT_mov(AUX_REG
, dst_reg
);
849 if (BPF_SRC(insn
->code
) == BPF_X
)
850 emit_mov_reg(&prog
, is64
, BPF_REG_0
, src_reg
);
852 emit_mov_imm32(&prog
, is64
, BPF_REG_0
, imm32
);
855 EMIT1(add_1mod(0x48, AUX_REG
));
856 else if (is_ereg(AUX_REG
))
857 EMIT1(add_1mod(0x40, AUX_REG
));
859 EMIT2(0xF7, add_1reg(0xE0, AUX_REG
));
861 if (dst_reg
!= BPF_REG_3
)
862 EMIT1(0x5A); /* pop rdx */
863 if (dst_reg
!= BPF_REG_0
) {
864 /* mov dst_reg, rax */
865 EMIT_mov(dst_reg
, BPF_REG_0
);
866 EMIT1(0x58); /* pop rax */
871 case BPF_ALU
| BPF_LSH
| BPF_K
:
872 case BPF_ALU
| BPF_RSH
| BPF_K
:
873 case BPF_ALU
| BPF_ARSH
| BPF_K
:
874 case BPF_ALU64
| BPF_LSH
| BPF_K
:
875 case BPF_ALU64
| BPF_RSH
| BPF_K
:
876 case BPF_ALU64
| BPF_ARSH
| BPF_K
:
877 if (BPF_CLASS(insn
->code
) == BPF_ALU64
)
878 EMIT1(add_1mod(0x48, dst_reg
));
879 else if (is_ereg(dst_reg
))
880 EMIT1(add_1mod(0x40, dst_reg
));
882 switch (BPF_OP(insn
->code
)) {
883 case BPF_LSH
: b3
= 0xE0; break;
884 case BPF_RSH
: b3
= 0xE8; break;
885 case BPF_ARSH
: b3
= 0xF8; break;
889 EMIT2(0xD1, add_1reg(b3
, dst_reg
));
891 EMIT3(0xC1, add_1reg(b3
, dst_reg
), imm32
);
894 case BPF_ALU
| BPF_LSH
| BPF_X
:
895 case BPF_ALU
| BPF_RSH
| BPF_X
:
896 case BPF_ALU
| BPF_ARSH
| BPF_X
:
897 case BPF_ALU64
| BPF_LSH
| BPF_X
:
898 case BPF_ALU64
| BPF_RSH
| BPF_X
:
899 case BPF_ALU64
| BPF_ARSH
| BPF_X
:
901 /* Check for bad case when dst_reg == rcx */
902 if (dst_reg
== BPF_REG_4
) {
903 /* mov r11, dst_reg */
904 EMIT_mov(AUX_REG
, dst_reg
);
908 if (src_reg
!= BPF_REG_4
) { /* common case */
909 EMIT1(0x51); /* push rcx */
911 /* mov rcx, src_reg */
912 EMIT_mov(BPF_REG_4
, src_reg
);
915 /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
916 if (BPF_CLASS(insn
->code
) == BPF_ALU64
)
917 EMIT1(add_1mod(0x48, dst_reg
));
918 else if (is_ereg(dst_reg
))
919 EMIT1(add_1mod(0x40, dst_reg
));
921 switch (BPF_OP(insn
->code
)) {
922 case BPF_LSH
: b3
= 0xE0; break;
923 case BPF_RSH
: b3
= 0xE8; break;
924 case BPF_ARSH
: b3
= 0xF8; break;
926 EMIT2(0xD3, add_1reg(b3
, dst_reg
));
928 if (src_reg
!= BPF_REG_4
)
929 EMIT1(0x59); /* pop rcx */
931 if (insn
->dst_reg
== BPF_REG_4
)
932 /* mov dst_reg, r11 */
933 EMIT_mov(insn
->dst_reg
, AUX_REG
);
936 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
939 /* Emit 'ror %ax, 8' to swap lower 2 bytes */
941 if (is_ereg(dst_reg
))
943 EMIT3(0xC1, add_1reg(0xC8, dst_reg
), 8);
945 /* Emit 'movzwl eax, ax' */
946 if (is_ereg(dst_reg
))
947 EMIT3(0x45, 0x0F, 0xB7);
950 EMIT1(add_2reg(0xC0, dst_reg
, dst_reg
));
953 /* Emit 'bswap eax' to swap lower 4 bytes */
954 if (is_ereg(dst_reg
))
958 EMIT1(add_1reg(0xC8, dst_reg
));
961 /* Emit 'bswap rax' to swap 8 bytes */
962 EMIT3(add_1mod(0x48, dst_reg
), 0x0F,
963 add_1reg(0xC8, dst_reg
));
968 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
972 * Emit 'movzwl eax, ax' to zero extend 16-bit
975 if (is_ereg(dst_reg
))
976 EMIT3(0x45, 0x0F, 0xB7);
979 EMIT1(add_2reg(0xC0, dst_reg
, dst_reg
));
982 /* Emit 'mov eax, eax' to clear upper 32-bits */
983 if (is_ereg(dst_reg
))
985 EMIT2(0x89, add_2reg(0xC0, dst_reg
, dst_reg
));
993 /* ST: *(u8*)(dst_reg + off) = imm */
994 case BPF_ST
| BPF_MEM
| BPF_B
:
995 if (is_ereg(dst_reg
))
1000 case BPF_ST
| BPF_MEM
| BPF_H
:
1001 if (is_ereg(dst_reg
))
1002 EMIT3(0x66, 0x41, 0xC7);
1006 case BPF_ST
| BPF_MEM
| BPF_W
:
1007 if (is_ereg(dst_reg
))
1012 case BPF_ST
| BPF_MEM
| BPF_DW
:
1013 EMIT2(add_1mod(0x48, dst_reg
), 0xC7);
1015 st
: if (is_imm8(insn
->off
))
1016 EMIT2(add_1reg(0x40, dst_reg
), insn
->off
);
1018 EMIT1_off32(add_1reg(0x80, dst_reg
), insn
->off
);
1020 EMIT(imm32
, bpf_size_to_x86_bytes(BPF_SIZE(insn
->code
)));
1023 /* STX: *(u8*)(dst_reg + off) = src_reg */
1024 case BPF_STX
| BPF_MEM
| BPF_B
:
1025 case BPF_STX
| BPF_MEM
| BPF_H
:
1026 case BPF_STX
| BPF_MEM
| BPF_W
:
1027 case BPF_STX
| BPF_MEM
| BPF_DW
:
1028 emit_stx(&prog
, BPF_SIZE(insn
->code
), dst_reg
, src_reg
, insn
->off
);
1031 /* LDX: dst_reg = *(u8*)(src_reg + off) */
1032 case BPF_LDX
| BPF_MEM
| BPF_B
:
1033 case BPF_LDX
| BPF_PROBE_MEM
| BPF_B
:
1034 case BPF_LDX
| BPF_MEM
| BPF_H
:
1035 case BPF_LDX
| BPF_PROBE_MEM
| BPF_H
:
1036 case BPF_LDX
| BPF_MEM
| BPF_W
:
1037 case BPF_LDX
| BPF_PROBE_MEM
| BPF_W
:
1038 case BPF_LDX
| BPF_MEM
| BPF_DW
:
1039 case BPF_LDX
| BPF_PROBE_MEM
| BPF_DW
:
1040 emit_ldx(&prog
, BPF_SIZE(insn
->code
), dst_reg
, src_reg
, insn
->off
);
1041 if (BPF_MODE(insn
->code
) == BPF_PROBE_MEM
) {
1042 struct exception_table_entry
*ex
;
1043 u8
*_insn
= image
+ proglen
;
1046 if (!bpf_prog
->aux
->extable
)
1049 if (excnt
>= bpf_prog
->aux
->num_exentries
) {
1050 pr_err("ex gen bug\n");
1053 ex
= &bpf_prog
->aux
->extable
[excnt
++];
1055 delta
= _insn
- (u8
*)&ex
->insn
;
1056 if (!is_simm32(delta
)) {
1057 pr_err("extable->insn doesn't fit into 32-bit\n");
1062 delta
= (u8
*)ex_handler_bpf
- (u8
*)&ex
->handler
;
1063 if (!is_simm32(delta
)) {
1064 pr_err("extable->handler doesn't fit into 32-bit\n");
1067 ex
->handler
= delta
;
1069 if (dst_reg
> BPF_REG_9
) {
1070 pr_err("verifier error\n");
1074 * Compute size of x86 insn and its target dest x86 register.
1075 * ex_handler_bpf() will use lower 8 bits to adjust
1076 * pt_regs->ip to jump over this x86 instruction
1077 * and upper bits to figure out which pt_regs to zero out.
1078 * End result: x86 insn "mov rbx, qword ptr [rax+0x14]"
1079 * of 4 bytes will be ignored and rbx will be zero inited.
1081 ex
->fixup
= (prog
- temp
) | (reg2pt_regs
[dst_reg
] << 8);
1085 /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
1086 case BPF_STX
| BPF_XADD
| BPF_W
:
1087 /* Emit 'lock add dword ptr [rax + off], eax' */
1088 if (is_ereg(dst_reg
) || is_ereg(src_reg
))
1089 EMIT3(0xF0, add_2mod(0x40, dst_reg
, src_reg
), 0x01);
1093 case BPF_STX
| BPF_XADD
| BPF_DW
:
1094 EMIT3(0xF0, add_2mod(0x48, dst_reg
, src_reg
), 0x01);
1095 xadd
: if (is_imm8(insn
->off
))
1096 EMIT2(add_2reg(0x40, dst_reg
, src_reg
), insn
->off
);
1098 EMIT1_off32(add_2reg(0x80, dst_reg
, src_reg
),
1103 case BPF_JMP
| BPF_CALL
:
1104 func
= (u8
*) __bpf_call_base
+ imm32
;
1105 if (!imm32
|| emit_call(&prog
, func
, image
+ addrs
[i
- 1]))
1109 case BPF_JMP
| BPF_TAIL_CALL
:
1111 emit_bpf_tail_call_direct(&bpf_prog
->aux
->poke_tab
[imm32
- 1],
1112 &prog
, addrs
[i
], image
);
1114 emit_bpf_tail_call_indirect(&prog
);
1118 case BPF_JMP
| BPF_JEQ
| BPF_X
:
1119 case BPF_JMP
| BPF_JNE
| BPF_X
:
1120 case BPF_JMP
| BPF_JGT
| BPF_X
:
1121 case BPF_JMP
| BPF_JLT
| BPF_X
:
1122 case BPF_JMP
| BPF_JGE
| BPF_X
:
1123 case BPF_JMP
| BPF_JLE
| BPF_X
:
1124 case BPF_JMP
| BPF_JSGT
| BPF_X
:
1125 case BPF_JMP
| BPF_JSLT
| BPF_X
:
1126 case BPF_JMP
| BPF_JSGE
| BPF_X
:
1127 case BPF_JMP
| BPF_JSLE
| BPF_X
:
1128 case BPF_JMP32
| BPF_JEQ
| BPF_X
:
1129 case BPF_JMP32
| BPF_JNE
| BPF_X
:
1130 case BPF_JMP32
| BPF_JGT
| BPF_X
:
1131 case BPF_JMP32
| BPF_JLT
| BPF_X
:
1132 case BPF_JMP32
| BPF_JGE
| BPF_X
:
1133 case BPF_JMP32
| BPF_JLE
| BPF_X
:
1134 case BPF_JMP32
| BPF_JSGT
| BPF_X
:
1135 case BPF_JMP32
| BPF_JSLT
| BPF_X
:
1136 case BPF_JMP32
| BPF_JSGE
| BPF_X
:
1137 case BPF_JMP32
| BPF_JSLE
| BPF_X
:
1138 /* cmp dst_reg, src_reg */
1139 if (BPF_CLASS(insn
->code
) == BPF_JMP
)
1140 EMIT1(add_2mod(0x48, dst_reg
, src_reg
));
1141 else if (is_ereg(dst_reg
) || is_ereg(src_reg
))
1142 EMIT1(add_2mod(0x40, dst_reg
, src_reg
));
1143 EMIT2(0x39, add_2reg(0xC0, dst_reg
, src_reg
));
1146 case BPF_JMP
| BPF_JSET
| BPF_X
:
1147 case BPF_JMP32
| BPF_JSET
| BPF_X
:
1148 /* test dst_reg, src_reg */
1149 if (BPF_CLASS(insn
->code
) == BPF_JMP
)
1150 EMIT1(add_2mod(0x48, dst_reg
, src_reg
));
1151 else if (is_ereg(dst_reg
) || is_ereg(src_reg
))
1152 EMIT1(add_2mod(0x40, dst_reg
, src_reg
));
1153 EMIT2(0x85, add_2reg(0xC0, dst_reg
, src_reg
));
1156 case BPF_JMP
| BPF_JSET
| BPF_K
:
1157 case BPF_JMP32
| BPF_JSET
| BPF_K
:
1158 /* test dst_reg, imm32 */
1159 if (BPF_CLASS(insn
->code
) == BPF_JMP
)
1160 EMIT1(add_1mod(0x48, dst_reg
));
1161 else if (is_ereg(dst_reg
))
1162 EMIT1(add_1mod(0x40, dst_reg
));
1163 EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg
), imm32
);
1166 case BPF_JMP
| BPF_JEQ
| BPF_K
:
1167 case BPF_JMP
| BPF_JNE
| BPF_K
:
1168 case BPF_JMP
| BPF_JGT
| BPF_K
:
1169 case BPF_JMP
| BPF_JLT
| BPF_K
:
1170 case BPF_JMP
| BPF_JGE
| BPF_K
:
1171 case BPF_JMP
| BPF_JLE
| BPF_K
:
1172 case BPF_JMP
| BPF_JSGT
| BPF_K
:
1173 case BPF_JMP
| BPF_JSLT
| BPF_K
:
1174 case BPF_JMP
| BPF_JSGE
| BPF_K
:
1175 case BPF_JMP
| BPF_JSLE
| BPF_K
:
1176 case BPF_JMP32
| BPF_JEQ
| BPF_K
:
1177 case BPF_JMP32
| BPF_JNE
| BPF_K
:
1178 case BPF_JMP32
| BPF_JGT
| BPF_K
:
1179 case BPF_JMP32
| BPF_JLT
| BPF_K
:
1180 case BPF_JMP32
| BPF_JGE
| BPF_K
:
1181 case BPF_JMP32
| BPF_JLE
| BPF_K
:
1182 case BPF_JMP32
| BPF_JSGT
| BPF_K
:
1183 case BPF_JMP32
| BPF_JSLT
| BPF_K
:
1184 case BPF_JMP32
| BPF_JSGE
| BPF_K
:
1185 case BPF_JMP32
| BPF_JSLE
| BPF_K
:
1186 /* test dst_reg, dst_reg to save one extra byte */
1188 if (BPF_CLASS(insn
->code
) == BPF_JMP
)
1189 EMIT1(add_2mod(0x48, dst_reg
, dst_reg
));
1190 else if (is_ereg(dst_reg
))
1191 EMIT1(add_2mod(0x40, dst_reg
, dst_reg
));
1192 EMIT2(0x85, add_2reg(0xC0, dst_reg
, dst_reg
));
1196 /* cmp dst_reg, imm8/32 */
1197 if (BPF_CLASS(insn
->code
) == BPF_JMP
)
1198 EMIT1(add_1mod(0x48, dst_reg
));
1199 else if (is_ereg(dst_reg
))
1200 EMIT1(add_1mod(0x40, dst_reg
));
1203 EMIT3(0x83, add_1reg(0xF8, dst_reg
), imm32
);
1205 EMIT2_off32(0x81, add_1reg(0xF8, dst_reg
), imm32
);
1207 emit_cond_jmp
: /* Convert BPF opcode to x86 */
1208 switch (BPF_OP(insn
->code
)) {
1217 /* GT is unsigned '>', JA in x86 */
1221 /* LT is unsigned '<', JB in x86 */
1225 /* GE is unsigned '>=', JAE in x86 */
1229 /* LE is unsigned '<=', JBE in x86 */
1233 /* Signed '>', GT in x86 */
1237 /* Signed '<', LT in x86 */
1241 /* Signed '>=', GE in x86 */
1245 /* Signed '<=', LE in x86 */
1248 default: /* to silence GCC warning */
1251 jmp_offset
= addrs
[i
+ insn
->off
] - addrs
[i
];
1252 if (is_imm8(jmp_offset
)) {
1253 EMIT2(jmp_cond
, jmp_offset
);
1254 } else if (is_simm32(jmp_offset
)) {
1255 EMIT2_off32(0x0F, jmp_cond
+ 0x10, jmp_offset
);
1257 pr_err("cond_jmp gen bug %llx\n", jmp_offset
);
1263 case BPF_JMP
| BPF_JA
:
1264 if (insn
->off
== -1)
1265 /* -1 jmp instructions will always jump
1266 * backwards two bytes. Explicitly handling
1267 * this case avoids wasting too many passes
1268 * when there are long sequences of replaced
1273 jmp_offset
= addrs
[i
+ insn
->off
] - addrs
[i
];
1276 /* Optimize out nop jumps */
1279 if (is_imm8(jmp_offset
)) {
1280 EMIT2(0xEB, jmp_offset
);
1281 } else if (is_simm32(jmp_offset
)) {
1282 EMIT1_off32(0xE9, jmp_offset
);
1284 pr_err("jmp gen bug %llx\n", jmp_offset
);
1289 case BPF_JMP
| BPF_EXIT
:
1291 jmp_offset
= ctx
->cleanup_addr
- addrs
[i
];
1295 /* Update cleanup_addr */
1296 ctx
->cleanup_addr
= proglen
;
1297 if (!bpf_prog_was_classic(bpf_prog
))
1298 EMIT1(0x5B); /* get rid of tail_call_cnt */
1299 EMIT2(0x41, 0x5F); /* pop r15 */
1300 EMIT2(0x41, 0x5E); /* pop r14 */
1301 EMIT2(0x41, 0x5D); /* pop r13 */
1302 EMIT1(0x5B); /* pop rbx */
1303 EMIT1(0xC9); /* leave */
1304 EMIT1(0xC3); /* ret */
1309 * By design x86-64 JIT should support all BPF instructions.
1310 * This error will be seen if new instruction was added
1311 * to the interpreter, but not to the JIT, or if there is
1314 pr_err("bpf_jit: unknown opcode %02x\n", insn
->code
);
1319 if (ilen
> BPF_MAX_INSN_SIZE
) {
1320 pr_err("bpf_jit: fatal insn size error\n");
1325 if (unlikely(proglen
+ ilen
> oldproglen
)) {
1326 pr_err("bpf_jit: fatal error\n");
1329 memcpy(image
+ proglen
, temp
, ilen
);
1336 if (image
&& excnt
!= bpf_prog
->aux
->num_exentries
) {
1337 pr_err("extable is not populated\n");
1343 static void save_regs(const struct btf_func_model
*m
, u8
**prog
, int nr_args
,
1347 /* Store function arguments to stack.
1348 * For a function that accepts two pointers the sequence will be:
1349 * mov QWORD PTR [rbp-0x10],rdi
1350 * mov QWORD PTR [rbp-0x8],rsi
1352 for (i
= 0; i
< min(nr_args
, 6); i
++)
1353 emit_stx(prog
, bytes_to_bpf_size(m
->arg_size
[i
]),
1355 i
== 5 ? X86_REG_R9
: BPF_REG_1
+ i
,
1356 -(stack_size
- i
* 8));
1359 static void restore_regs(const struct btf_func_model
*m
, u8
**prog
, int nr_args
,
1364 /* Restore function arguments from stack.
1365 * For a function that accepts two pointers the sequence will be:
1366 * EMIT4(0x48, 0x8B, 0x7D, 0xF0); mov rdi,QWORD PTR [rbp-0x10]
1367 * EMIT4(0x48, 0x8B, 0x75, 0xF8); mov rsi,QWORD PTR [rbp-0x8]
1369 for (i
= 0; i
< min(nr_args
, 6); i
++)
1370 emit_ldx(prog
, bytes_to_bpf_size(m
->arg_size
[i
]),
1371 i
== 5 ? X86_REG_R9
: BPF_REG_1
+ i
,
1373 -(stack_size
- i
* 8));
1376 static int invoke_bpf(const struct btf_func_model
*m
, u8
**pprog
,
1377 struct bpf_prog
**progs
, int prog_cnt
, int stack_size
)
1382 for (i
= 0; i
< prog_cnt
; i
++) {
1383 if (emit_call(&prog
, __bpf_prog_enter
, prog
))
1385 /* remember prog start time returned by __bpf_prog_enter */
1386 emit_mov_reg(&prog
, true, BPF_REG_6
, BPF_REG_0
);
1388 /* arg1: lea rdi, [rbp - stack_size] */
1389 EMIT4(0x48, 0x8D, 0x7D, -stack_size
);
1390 /* arg2: progs[i]->insnsi for interpreter */
1391 if (!progs
[i
]->jited
)
1392 emit_mov_imm64(&prog
, BPF_REG_2
,
1393 (long) progs
[i
]->insnsi
>> 32,
1394 (u32
) (long) progs
[i
]->insnsi
);
1395 /* call JITed bpf program or interpreter */
1396 if (emit_call(&prog
, progs
[i
]->bpf_func
, prog
))
1399 /* arg1: mov rdi, progs[i] */
1400 emit_mov_imm64(&prog
, BPF_REG_1
, (long) progs
[i
] >> 32,
1401 (u32
) (long) progs
[i
]);
1402 /* arg2: mov rsi, rbx <- start time in nsec */
1403 emit_mov_reg(&prog
, true, BPF_REG_2
, BPF_REG_6
);
1404 if (emit_call(&prog
, __bpf_prog_exit
, prog
))
1412 * __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
1413 * its 'struct btf_func_model' will be nr_args=2
1414 * The assembly code when eth_type_trans is executing after trampoline:
1418 * sub rsp, 16 // space for skb and dev
1419 * push rbx // temp regs to pass start time
1420 * mov qword ptr [rbp - 16], rdi // save skb pointer to stack
1421 * mov qword ptr [rbp - 8], rsi // save dev pointer to stack
1422 * call __bpf_prog_enter // rcu_read_lock and preempt_disable
1423 * mov rbx, rax // remember start time in bpf stats are enabled
1424 * lea rdi, [rbp - 16] // R1==ctx of bpf prog
1425 * call addr_of_jited_FENTRY_prog
1426 * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
1427 * mov rsi, rbx // prog start time
1428 * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
1429 * mov rdi, qword ptr [rbp - 16] // restore skb pointer from stack
1430 * mov rsi, qword ptr [rbp - 8] // restore dev pointer from stack
1435 * eth_type_trans has 5 byte nop at the beginning. These 5 bytes will be
1436 * replaced with 'call generated_bpf_trampoline'. When it returns
1437 * eth_type_trans will continue executing with original skb and dev pointers.
1439 * The assembly code when eth_type_trans is called from trampoline:
1443 * sub rsp, 24 // space for skb, dev, return value
1444 * push rbx // temp regs to pass start time
1445 * mov qword ptr [rbp - 24], rdi // save skb pointer to stack
1446 * mov qword ptr [rbp - 16], rsi // save dev pointer to stack
1447 * call __bpf_prog_enter // rcu_read_lock and preempt_disable
1448 * mov rbx, rax // remember start time if bpf stats are enabled
1449 * lea rdi, [rbp - 24] // R1==ctx of bpf prog
1450 * call addr_of_jited_FENTRY_prog // bpf prog can access skb and dev
1451 * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
1452 * mov rsi, rbx // prog start time
1453 * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
1454 * mov rdi, qword ptr [rbp - 24] // restore skb pointer from stack
1455 * mov rsi, qword ptr [rbp - 16] // restore dev pointer from stack
1456 * call eth_type_trans+5 // execute body of eth_type_trans
1457 * mov qword ptr [rbp - 8], rax // save return value
1458 * call __bpf_prog_enter // rcu_read_lock and preempt_disable
1459 * mov rbx, rax // remember start time in bpf stats are enabled
1460 * lea rdi, [rbp - 24] // R1==ctx of bpf prog
1461 * call addr_of_jited_FEXIT_prog // bpf prog can access skb, dev, return value
1462 * movabsq rdi, 64bit_addr_of_struct_bpf_prog // unused if bpf stats are off
1463 * mov rsi, rbx // prog start time
1464 * call __bpf_prog_exit // rcu_read_unlock, preempt_enable and stats math
1465 * mov rax, qword ptr [rbp - 8] // restore eth_type_trans's return value
1468 * add rsp, 8 // skip eth_type_trans's frame
1469 * ret // return to its caller
1471 int arch_prepare_bpf_trampoline(void *image
, void *image_end
,
1472 const struct btf_func_model
*m
, u32 flags
,
1473 struct bpf_prog
**fentry_progs
, int fentry_cnt
,
1474 struct bpf_prog
**fexit_progs
, int fexit_cnt
,
1477 int cnt
= 0, nr_args
= m
->nr_args
;
1478 int stack_size
= nr_args
* 8;
1481 /* x86-64 supports up to 6 arguments. 7+ can be added in the future */
1485 if ((flags
& BPF_TRAMP_F_RESTORE_REGS
) &&
1486 (flags
& BPF_TRAMP_F_SKIP_FRAME
))
1489 if (flags
& BPF_TRAMP_F_CALL_ORIG
)
1490 stack_size
+= 8; /* room for return value of orig_call */
1492 if (flags
& BPF_TRAMP_F_SKIP_FRAME
)
1493 /* skip patched call instruction and point orig_call to actual
1494 * body of the kernel function.
1496 orig_call
+= X86_PATCH_SIZE
;
1500 EMIT1(0x55); /* push rbp */
1501 EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
1502 EMIT4(0x48, 0x83, 0xEC, stack_size
); /* sub rsp, stack_size */
1503 EMIT1(0x53); /* push rbx */
1505 save_regs(m
, &prog
, nr_args
, stack_size
);
1508 if (invoke_bpf(m
, &prog
, fentry_progs
, fentry_cnt
, stack_size
))
1511 if (flags
& BPF_TRAMP_F_CALL_ORIG
) {
1513 restore_regs(m
, &prog
, nr_args
, stack_size
);
1515 /* call original function */
1516 if (emit_call(&prog
, orig_call
, prog
))
1518 /* remember return value in a stack for bpf prog to access */
1519 emit_stx(&prog
, BPF_DW
, BPF_REG_FP
, BPF_REG_0
, -8);
1523 if (invoke_bpf(m
, &prog
, fexit_progs
, fexit_cnt
, stack_size
))
1526 if (flags
& BPF_TRAMP_F_RESTORE_REGS
)
1527 restore_regs(m
, &prog
, nr_args
, stack_size
);
1529 if (flags
& BPF_TRAMP_F_CALL_ORIG
)
1530 /* restore original return value back into RAX */
1531 emit_ldx(&prog
, BPF_DW
, BPF_REG_0
, BPF_REG_FP
, -8);
1533 EMIT1(0x5B); /* pop rbx */
1534 EMIT1(0xC9); /* leave */
1535 if (flags
& BPF_TRAMP_F_SKIP_FRAME
)
1536 /* skip our return address and return to parent */
1537 EMIT4(0x48, 0x83, 0xC4, 8); /* add rsp, 8 */
1538 EMIT1(0xC3); /* ret */
1539 /* Make sure the trampoline generation logic doesn't overflow */
1540 if (WARN_ON_ONCE(prog
> (u8
*)image_end
- BPF_INSN_SAFETY
))
1542 return prog
- (u8
*)image
;
1545 static int emit_cond_near_jump(u8
**pprog
, void *func
, void *ip
, u8 jmp_cond
)
1551 offset
= func
- (ip
+ 2 + 4);
1552 if (!is_simm32(offset
)) {
1553 pr_err("Target %p is out of range\n", func
);
1556 EMIT2_off32(0x0F, jmp_cond
+ 0x10, offset
);
1561 static void emit_nops(u8
**pprog
, unsigned int len
)
1563 unsigned int i
, noplen
;
1570 if (noplen
> ASM_NOP_MAX
)
1571 noplen
= ASM_NOP_MAX
;
1573 for (i
= 0; i
< noplen
; i
++)
1574 EMIT1(ideal_nops
[noplen
][i
]);
1581 static int emit_fallback_jump(u8
**pprog
)
1586 #ifdef CONFIG_RETPOLINE
1587 /* Note that this assumes the the compiler uses external
1588 * thunks for indirect calls. Both clang and GCC use the same
1589 * naming convention for external thunks.
1591 err
= emit_jump(&prog
, __x86_indirect_thunk_rdx
, prog
);
1595 EMIT2(0xFF, 0xE2); /* jmp rdx */
1601 static int emit_bpf_dispatcher(u8
**pprog
, int a
, int b
, s64
*progs
)
1603 u8
*jg_reloc
, *jg_target
, *prog
= *pprog
;
1604 int pivot
, err
, jg_bytes
= 1, cnt
= 0;
1608 /* Leaf node of recursion, i.e. not a range of indices
1611 EMIT1(add_1mod(0x48, BPF_REG_3
)); /* cmp rdx,func */
1612 if (!is_simm32(progs
[a
]))
1614 EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3
),
1616 err
= emit_cond_near_jump(&prog
, /* je func */
1617 (void *)progs
[a
], prog
,
1622 err
= emit_fallback_jump(&prog
); /* jmp thunk/indirect */
1630 /* Not a leaf node, so we pivot, and recursively descend into
1631 * the lower and upper ranges.
1633 pivot
= (b
- a
) / 2;
1634 EMIT1(add_1mod(0x48, BPF_REG_3
)); /* cmp rdx,func */
1635 if (!is_simm32(progs
[a
+ pivot
]))
1637 EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3
), progs
[a
+ pivot
]);
1639 if (pivot
> 2) { /* jg upper_part */
1640 /* Require near jump. */
1642 EMIT2_off32(0x0F, X86_JG
+ 0x10, 0);
1648 err
= emit_bpf_dispatcher(&prog
, a
, a
+ pivot
, /* emit lower_part */
1653 /* From Intel 64 and IA-32 Architectures Optimization
1654 * Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler
1655 * Coding Rule 11: All branch targets should be 16-byte
1658 jg_target
= PTR_ALIGN(prog
, 16);
1659 if (jg_target
!= prog
)
1660 emit_nops(&prog
, jg_target
- prog
);
1661 jg_offset
= prog
- jg_reloc
;
1662 emit_code(jg_reloc
- jg_bytes
, jg_offset
, jg_bytes
);
1664 err
= emit_bpf_dispatcher(&prog
, a
+ pivot
+ 1, /* emit upper_part */
1673 static int cmp_ips(const void *a
, const void *b
)
1685 int arch_prepare_bpf_dispatcher(void *image
, s64
*funcs
, int num_funcs
)
1689 sort(funcs
, num_funcs
, sizeof(funcs
[0]), cmp_ips
, NULL
);
1690 return emit_bpf_dispatcher(&prog
, 0, num_funcs
- 1, funcs
);
1693 struct x64_jit_data
{
1694 struct bpf_binary_header
*header
;
1698 struct jit_context ctx
;
1701 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*prog
)
1703 struct bpf_binary_header
*header
= NULL
;
1704 struct bpf_prog
*tmp
, *orig_prog
= prog
;
1705 struct x64_jit_data
*jit_data
;
1706 int proglen
, oldproglen
= 0;
1707 struct jit_context ctx
= {};
1708 bool tmp_blinded
= false;
1709 bool extra_pass
= false;
1715 if (!prog
->jit_requested
)
1718 tmp
= bpf_jit_blind_constants(prog
);
1720 * If blinding was requested and we failed during blinding,
1721 * we must fall back to the interpreter.
1730 jit_data
= prog
->aux
->jit_data
;
1732 jit_data
= kzalloc(sizeof(*jit_data
), GFP_KERNEL
);
1737 prog
->aux
->jit_data
= jit_data
;
1739 addrs
= jit_data
->addrs
;
1741 ctx
= jit_data
->ctx
;
1742 oldproglen
= jit_data
->proglen
;
1743 image
= jit_data
->image
;
1744 header
= jit_data
->header
;
1746 goto skip_init_addrs
;
1748 addrs
= kmalloc_array(prog
->len
+ 1, sizeof(*addrs
), GFP_KERNEL
);
1755 * Before first pass, make a rough estimation of addrs[]
1756 * each BPF instruction is translated to less than 64 bytes
1758 for (proglen
= 0, i
= 0; i
<= prog
->len
; i
++) {
1762 ctx
.cleanup_addr
= proglen
;
1766 * JITed image shrinks with every pass and the loop iterates
1767 * until the image stops shrinking. Very large BPF programs
1768 * may converge on the last pass. In such case do one more
1769 * pass to emit the final image.
1771 for (pass
= 0; pass
< 20 || image
; pass
++) {
1772 proglen
= do_jit(prog
, addrs
, image
, oldproglen
, &ctx
);
1777 bpf_jit_binary_free(header
);
1782 if (proglen
!= oldproglen
) {
1783 pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
1784 proglen
, oldproglen
);
1789 if (proglen
== oldproglen
) {
1791 * The number of entries in extable is the number of BPF_LDX
1792 * insns that access kernel memory via "pointer to BTF type".
1793 * The verifier changed their opcode from LDX|MEM|size
1794 * to LDX|PROBE_MEM|size to make JITing easier.
1796 u32 align
= __alignof__(struct exception_table_entry
);
1797 u32 extable_size
= prog
->aux
->num_exentries
*
1798 sizeof(struct exception_table_entry
);
1800 /* allocate module memory for x86 insns and extable */
1801 header
= bpf_jit_binary_alloc(roundup(proglen
, align
) + extable_size
,
1802 &image
, align
, jit_fill_hole
);
1807 prog
->aux
->extable
= (void *) image
+ roundup(proglen
, align
);
1809 oldproglen
= proglen
;
1813 if (bpf_jit_enable
> 1)
1814 bpf_jit_dump(prog
->len
, proglen
, pass
+ 1, image
);
1817 if (!prog
->is_func
|| extra_pass
) {
1818 bpf_tail_call_direct_fixup(prog
);
1819 bpf_jit_binary_lock_ro(header
);
1821 jit_data
->addrs
= addrs
;
1822 jit_data
->ctx
= ctx
;
1823 jit_data
->proglen
= proglen
;
1824 jit_data
->image
= image
;
1825 jit_data
->header
= header
;
1827 prog
->bpf_func
= (void *)image
;
1829 prog
->jited_len
= proglen
;
1834 if (!image
|| !prog
->is_func
|| extra_pass
) {
1836 bpf_prog_fill_jited_linfo(prog
, addrs
+ 1);
1840 prog
->aux
->jit_data
= NULL
;
1844 bpf_jit_prog_release_other(prog
, prog
== orig_prog
?