1 // SPDX-License-Identifier: GPL-2.0
3 * Just-In-Time compiler for eBPF filters on IA32 (32bit x86)
5 * Author: Wang YanQing (udknight@gmail.com)
6 * The code based on code and ideas from:
7 * Eric Dumazet (eric.dumazet@gmail.com)
9 * Shubham Bansal <illusionist.neo@gmail.com>
12 #include <linux/netdevice.h>
13 #include <linux/filter.h>
14 #include <linux/if_vlan.h>
15 #include <asm/cacheflush.h>
16 #include <asm/set_memory.h>
17 #include <asm/nospec-branch.h>
18 #include <asm/asm-prototypes.h>
19 #include <linux/bpf.h>
22 * eBPF prog stack layout:
25 * original ESP => +-----+
26 * | | callee saved registers
28 * | ... | eBPF JIT scratch space
29 * BPF_FP,IA32_EBP => +-----+
30 * | ... | eBPF prog stack
32 * |RSVD | JIT scratchpad
33 * current ESP => +-----+
35 * | ... | Function call stack
40 * The callee saved registers:
43 * original ESP => +------------------+ \
45 * current EBP => +------------------+ } callee saved registers
47 * +------------------+ /
51 static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
64 #define EMIT(bytes, len) \
65 do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
67 #define EMIT1(b1) EMIT(b1, 1)
68 #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
69 #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
70 #define EMIT4(b1, b2, b3, b4) \
71 EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
73 #define EMIT1_off32(b1, off) \
74 do { EMIT1(b1); EMIT(off, 4); } while (0)
75 #define EMIT2_off32(b1, b2, off) \
76 do { EMIT2(b1, b2); EMIT(off, 4); } while (0)
77 #define EMIT3_off32(b1, b2, b3, off) \
78 do { EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
79 #define EMIT4_off32(b1, b2, b3, b4, off) \
80 do { EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
82 #define jmp_label(label, jmp_insn_len) (label - cnt - jmp_insn_len)
84 static bool is_imm8(int value)
86 return value <= 127 && value >= -128;
89 static bool is_simm32(s64 value)
91 return value == (s64) (s32) value;
94 #define STACK_OFFSET(k) (k)
95 #define TCALL_CNT (MAX_BPF_JIT_REG + 0) /* Tail Call Count */
97 #define IA32_EAX (0x0)
98 #define IA32_EBX (0x3)
99 #define IA32_ECX (0x1)
100 #define IA32_EDX (0x2)
101 #define IA32_ESI (0x6)
102 #define IA32_EDI (0x7)
103 #define IA32_EBP (0x5)
104 #define IA32_ESP (0x4)
107 * List of x86 cond jumps opcodes (. + s8)
108 * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
111 #define IA32_JAE 0x73
113 #define IA32_JNE 0x75
114 #define IA32_JBE 0x76
117 #define IA32_JGE 0x7D
118 #define IA32_JLE 0x7E
121 #define COND_JMP_OPCODE_INVALID (0xFF)
124 * Map eBPF registers to IA32 32bit registers or stack scratch space.
126 * 1. All the registers, R0-R10, are mapped to scratch space on stack.
127 * 2. We need two 64 bit temp registers to do complex operations on eBPF
129 * 3. For performance reason, the BPF_REG_AX for blinding constant, is
130 * mapped to real hardware register pair, IA32_ESI and IA32_EDI.
132 * As the eBPF registers are all 64 bit registers and IA32 has only 32 bit
133 * registers, we have to map each eBPF registers with two IA32 32 bit regs
134 * or scratch memory space and we have to build eBPF 64 bit register from those.
136 * We use IA32_EAX, IA32_EDX, IA32_ECX, IA32_EBX as temporary registers.
138 static const u8 bpf2ia32[][2] = {
139 /* Return value from in-kernel function, and exit value from eBPF */
140 [BPF_REG_0] = {STACK_OFFSET(0), STACK_OFFSET(4)},
142 /* The arguments from eBPF program to in-kernel function */
143 /* Stored on stack scratch space */
144 [BPF_REG_1] = {STACK_OFFSET(8), STACK_OFFSET(12)},
145 [BPF_REG_2] = {STACK_OFFSET(16), STACK_OFFSET(20)},
146 [BPF_REG_3] = {STACK_OFFSET(24), STACK_OFFSET(28)},
147 [BPF_REG_4] = {STACK_OFFSET(32), STACK_OFFSET(36)},
148 [BPF_REG_5] = {STACK_OFFSET(40), STACK_OFFSET(44)},
150 /* Callee saved registers that in-kernel function will preserve */
151 /* Stored on stack scratch space */
152 [BPF_REG_6] = {STACK_OFFSET(48), STACK_OFFSET(52)},
153 [BPF_REG_7] = {STACK_OFFSET(56), STACK_OFFSET(60)},
154 [BPF_REG_8] = {STACK_OFFSET(64), STACK_OFFSET(68)},
155 [BPF_REG_9] = {STACK_OFFSET(72), STACK_OFFSET(76)},
157 /* Read only Frame Pointer to access Stack */
158 [BPF_REG_FP] = {STACK_OFFSET(80), STACK_OFFSET(84)},
160 /* Temporary register for blinding constants. */
161 [BPF_REG_AX] = {IA32_ESI, IA32_EDI},
163 /* Tail call count. Stored on stack scratch space. */
164 [TCALL_CNT] = {STACK_OFFSET(88), STACK_OFFSET(92)},
167 #define dst_lo dst[0]
168 #define dst_hi dst[1]
169 #define src_lo src[0]
170 #define src_hi src[1]
172 #define STACK_ALIGNMENT 8
174 * Stack space for BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4,
175 * BPF_REG_5, BPF_REG_6, BPF_REG_7, BPF_REG_8, BPF_REG_9,
176 * BPF_REG_FP, BPF_REG_AX and Tail call counts.
178 #define SCRATCH_SIZE 96
180 /* Total stack size used in JITed code */
181 #define _STACK_SIZE (stack_depth + SCRATCH_SIZE)
183 #define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
185 /* Get the offset of eBPF REGISTERs stored on scratch space. */
186 #define STACK_VAR(off) (off)
188 /* Encode 'dst_reg' register into IA32 opcode 'byte' */
189 static u8 add_1reg(u8 byte, u32 dst_reg)
191 return byte + dst_reg;
194 /* Encode 'dst_reg' and 'src_reg' registers into IA32 opcode 'byte' */
195 static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
197 return byte + dst_reg + (src_reg << 3);
200 static void jit_fill_hole(void *area, unsigned int size)
202 /* Fill whole space with int3 instructions */
203 memset(area, 0xcc, size);
206 static inline void emit_ia32_mov_i(const u8 dst, const u32 val, bool dstk,
215 EMIT2(0x33, add_2reg(0xC0, IA32_EAX, IA32_EAX));
216 /* mov dword ptr [ebp+off],eax */
217 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
220 EMIT3_off32(0xC7, add_1reg(0x40, IA32_EBP),
221 STACK_VAR(dst), val);
225 EMIT2(0x33, add_2reg(0xC0, dst, dst));
227 EMIT2_off32(0xC7, add_1reg(0xC0, dst),
233 /* dst = imm (4 bytes)*/
234 static inline void emit_ia32_mov_r(const u8 dst, const u8 src, bool dstk,
235 bool sstk, u8 **pprog)
239 u8 sreg = sstk ? IA32_EAX : src;
242 /* mov eax,dword ptr [ebp+off] */
243 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(src));
245 /* mov dword ptr [ebp+off],eax */
246 EMIT3(0x89, add_2reg(0x40, IA32_EBP, sreg), STACK_VAR(dst));
249 EMIT2(0x89, add_2reg(0xC0, dst, sreg));
255 static inline void emit_ia32_mov_r64(const bool is64, const u8 dst[],
256 const u8 src[], bool dstk,
257 bool sstk, u8 **pprog,
258 const struct bpf_prog_aux *aux)
260 emit_ia32_mov_r(dst_lo, src_lo, dstk, sstk, pprog);
262 /* complete 8 byte move */
263 emit_ia32_mov_r(dst_hi, src_hi, dstk, sstk, pprog);
264 else if (!aux->verifier_zext)
265 /* zero out high 4 bytes */
266 emit_ia32_mov_i(dst_hi, 0, dstk, pprog);
269 /* Sign extended move */
270 static inline void emit_ia32_mov_i64(const bool is64, const u8 dst[],
271 const u32 val, bool dstk, u8 **pprog)
275 if (is64 && (val & (1<<31)))
277 emit_ia32_mov_i(dst_lo, val, dstk, pprog);
278 emit_ia32_mov_i(dst_hi, hi, dstk, pprog);
282 * ALU operation (32 bit)
285 static inline void emit_ia32_mul_r(const u8 dst, const u8 src, bool dstk,
286 bool sstk, u8 **pprog)
290 u8 sreg = sstk ? IA32_ECX : src;
293 /* mov ecx,dword ptr [ebp+off] */
294 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src));
297 /* mov eax,dword ptr [ebp+off] */
298 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(dst));
301 EMIT2(0x8B, add_2reg(0xC0, dst, IA32_EAX));
304 EMIT2(0xF7, add_1reg(0xE0, sreg));
307 /* mov dword ptr [ebp+off],eax */
308 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
312 EMIT2(0x89, add_2reg(0xC0, dst, IA32_EAX));
317 static inline void emit_ia32_to_le_r64(const u8 dst[], s32 val,
318 bool dstk, u8 **pprog,
319 const struct bpf_prog_aux *aux)
323 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
324 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
326 if (dstk && val != 64) {
327 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
329 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
335 * Emit 'movzwl eax,ax' to zero extend 16-bit
339 EMIT1(add_2reg(0xC0, dreg_lo, dreg_lo));
340 if (!aux->verifier_zext)
341 /* xor dreg_hi,dreg_hi */
342 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
345 if (!aux->verifier_zext)
346 /* xor dreg_hi,dreg_hi */
347 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
354 if (dstk && val != 64) {
355 /* mov dword ptr [ebp+off],dreg_lo */
356 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
358 /* mov dword ptr [ebp+off],dreg_hi */
359 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
365 static inline void emit_ia32_to_be_r64(const u8 dst[], s32 val,
366 bool dstk, u8 **pprog,
367 const struct bpf_prog_aux *aux)
371 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
372 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
375 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
377 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
382 /* Emit 'ror %ax, 8' to swap lower 2 bytes */
384 EMIT3(0xC1, add_1reg(0xC8, dreg_lo), 8);
387 EMIT1(add_2reg(0xC0, dreg_lo, dreg_lo));
389 if (!aux->verifier_zext)
390 /* xor dreg_hi,dreg_hi */
391 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
394 /* Emit 'bswap eax' to swap lower 4 bytes */
396 EMIT1(add_1reg(0xC8, dreg_lo));
398 if (!aux->verifier_zext)
399 /* xor dreg_hi,dreg_hi */
400 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
403 /* Emit 'bswap eax' to swap lower 4 bytes */
405 EMIT1(add_1reg(0xC8, dreg_lo));
407 /* Emit 'bswap edx' to swap lower 4 bytes */
409 EMIT1(add_1reg(0xC8, dreg_hi));
411 /* mov ecx,dreg_hi */
412 EMIT2(0x89, add_2reg(0xC0, IA32_ECX, dreg_hi));
413 /* mov dreg_hi,dreg_lo */
414 EMIT2(0x89, add_2reg(0xC0, dreg_hi, dreg_lo));
415 /* mov dreg_lo,ecx */
416 EMIT2(0x89, add_2reg(0xC0, dreg_lo, IA32_ECX));
421 /* mov dword ptr [ebp+off],dreg_lo */
422 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
424 /* mov dword ptr [ebp+off],dreg_hi */
425 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
432 * ALU operation (32 bit)
433 * dst = dst (div|mod) src
435 static inline void emit_ia32_div_mod_r(const u8 op, const u8 dst, const u8 src,
436 bool dstk, bool sstk, u8 **pprog)
442 /* mov ecx,dword ptr [ebp+off] */
443 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
445 else if (src != IA32_ECX)
447 EMIT2(0x8B, add_2reg(0xC0, src, IA32_ECX));
450 /* mov eax,dword ptr [ebp+off] */
451 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
455 EMIT2(0x8B, add_2reg(0xC0, dst, IA32_EAX));
458 EMIT2(0x31, add_2reg(0xC0, IA32_EDX, IA32_EDX));
460 EMIT2(0xF7, add_1reg(0xF0, IA32_ECX));
464 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EDX),
467 EMIT2(0x89, add_2reg(0xC0, dst, IA32_EDX));
470 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
473 EMIT2(0x89, add_2reg(0xC0, dst, IA32_EAX));
479 * ALU operation (32 bit)
480 * dst = dst (shift) src
482 static inline void emit_ia32_shift_r(const u8 op, const u8 dst, const u8 src,
483 bool dstk, bool sstk, u8 **pprog)
487 u8 dreg = dstk ? IA32_EAX : dst;
491 /* mov eax,dword ptr [ebp+off] */
492 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(dst));
495 /* mov ecx,dword ptr [ebp+off] */
496 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src));
497 else if (src != IA32_ECX)
499 EMIT2(0x8B, add_2reg(0xC0, src, IA32_ECX));
511 EMIT2(0xD3, add_1reg(b2, dreg));
514 /* mov dword ptr [ebp+off],dreg */
515 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg), STACK_VAR(dst));
520 * ALU operation (32 bit)
523 static inline void emit_ia32_alu_r(const bool is64, const bool hi, const u8 op,
524 const u8 dst, const u8 src, bool dstk,
525 bool sstk, u8 **pprog)
529 u8 sreg = sstk ? IA32_EAX : src;
530 u8 dreg = dstk ? IA32_EDX : dst;
533 /* mov eax,dword ptr [ebp+off] */
534 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(src));
537 /* mov eax,dword ptr [ebp+off] */
538 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX), STACK_VAR(dst));
540 switch (BPF_OP(op)) {
541 /* dst = dst + src */
544 EMIT2(0x11, add_2reg(0xC0, dreg, sreg));
546 EMIT2(0x01, add_2reg(0xC0, dreg, sreg));
548 /* dst = dst - src */
551 EMIT2(0x19, add_2reg(0xC0, dreg, sreg));
553 EMIT2(0x29, add_2reg(0xC0, dreg, sreg));
555 /* dst = dst | src */
557 EMIT2(0x09, add_2reg(0xC0, dreg, sreg));
559 /* dst = dst & src */
561 EMIT2(0x21, add_2reg(0xC0, dreg, sreg));
563 /* dst = dst ^ src */
565 EMIT2(0x31, add_2reg(0xC0, dreg, sreg));
570 /* mov dword ptr [ebp+off],dreg */
571 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg),
576 /* ALU operation (64 bit) */
577 static inline void emit_ia32_alu_r64(const bool is64, const u8 op,
578 const u8 dst[], const u8 src[],
579 bool dstk, bool sstk,
580 u8 **pprog, const struct bpf_prog_aux *aux)
584 emit_ia32_alu_r(is64, false, op, dst_lo, src_lo, dstk, sstk, &prog);
586 emit_ia32_alu_r(is64, true, op, dst_hi, src_hi, dstk, sstk,
588 else if (!aux->verifier_zext)
589 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
594 * ALU operation (32 bit)
597 static inline void emit_ia32_alu_i(const bool is64, const bool hi, const u8 op,
598 const u8 dst, const s32 val, bool dstk,
603 u8 dreg = dstk ? IA32_EAX : dst;
607 /* mov eax,dword ptr [ebp+off] */
608 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(dst));
612 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EDX), val);
615 /* dst = dst + val */
619 EMIT3(0x83, add_1reg(0xD0, dreg), val);
621 EMIT2(0x11, add_2reg(0xC0, dreg, sreg));
624 EMIT3(0x83, add_1reg(0xC0, dreg), val);
626 EMIT2(0x01, add_2reg(0xC0, dreg, sreg));
629 /* dst = dst - val */
633 EMIT3(0x83, add_1reg(0xD8, dreg), val);
635 EMIT2(0x19, add_2reg(0xC0, dreg, sreg));
638 EMIT3(0x83, add_1reg(0xE8, dreg), val);
640 EMIT2(0x29, add_2reg(0xC0, dreg, sreg));
643 /* dst = dst | val */
646 EMIT3(0x83, add_1reg(0xC8, dreg), val);
648 EMIT2(0x09, add_2reg(0xC0, dreg, sreg));
650 /* dst = dst & val */
653 EMIT3(0x83, add_1reg(0xE0, dreg), val);
655 EMIT2(0x21, add_2reg(0xC0, dreg, sreg));
657 /* dst = dst ^ val */
660 EMIT3(0x83, add_1reg(0xF0, dreg), val);
662 EMIT2(0x31, add_2reg(0xC0, dreg, sreg));
665 EMIT2(0xF7, add_1reg(0xD8, dreg));
670 /* mov dword ptr [ebp+off],dreg */
671 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg),
676 /* ALU operation (64 bit) */
677 static inline void emit_ia32_alu_i64(const bool is64, const u8 op,
678 const u8 dst[], const u32 val,
679 bool dstk, u8 **pprog,
680 const struct bpf_prog_aux *aux)
685 if (is64 && (val & (1<<31)))
688 emit_ia32_alu_i(is64, false, op, dst_lo, val, dstk, &prog);
690 emit_ia32_alu_i(is64, true, op, dst_hi, hi, dstk, &prog);
691 else if (!aux->verifier_zext)
692 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
697 /* dst = ~dst (64 bit) */
698 static inline void emit_ia32_neg64(const u8 dst[], bool dstk, u8 **pprog)
702 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
703 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
706 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
708 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
713 EMIT2(0xF7, add_1reg(0xD8, dreg_lo));
714 /* adc dreg_hi,0x0 */
715 EMIT3(0x83, add_1reg(0xD0, dreg_hi), 0x00);
717 EMIT2(0xF7, add_1reg(0xD8, dreg_hi));
720 /* mov dword ptr [ebp+off],dreg_lo */
721 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
723 /* mov dword ptr [ebp+off],dreg_hi */
724 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
730 /* dst = dst << src */
731 static inline void emit_ia32_lsh_r64(const u8 dst[], const u8 src[],
732 bool dstk, bool sstk, u8 **pprog)
736 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
737 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
740 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
742 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
747 /* mov ecx,dword ptr [ebp+off] */
748 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
752 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_ECX));
754 /* shld dreg_hi,dreg_lo,cl */
755 EMIT3(0x0F, 0xA5, add_2reg(0xC0, dreg_hi, dreg_lo));
757 EMIT2(0xD3, add_1reg(0xE0, dreg_lo));
759 /* if ecx >= 32, mov dreg_lo into dreg_hi and clear dreg_lo */
762 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), 32);
763 /* skip the next two instructions (4 bytes) when < 32 */
766 /* mov dreg_hi,dreg_lo */
767 EMIT2(0x89, add_2reg(0xC0, dreg_hi, dreg_lo));
768 /* xor dreg_lo,dreg_lo */
769 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
772 /* mov dword ptr [ebp+off],dreg_lo */
773 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
775 /* mov dword ptr [ebp+off],dreg_hi */
776 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
783 /* dst = dst >> src (signed)*/
784 static inline void emit_ia32_arsh_r64(const u8 dst[], const u8 src[],
785 bool dstk, bool sstk, u8 **pprog)
789 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
790 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
793 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
795 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
800 /* mov ecx,dword ptr [ebp+off] */
801 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
805 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_ECX));
807 /* shrd dreg_lo,dreg_hi,cl */
808 EMIT3(0x0F, 0xAD, add_2reg(0xC0, dreg_lo, dreg_hi));
810 EMIT2(0xD3, add_1reg(0xF8, dreg_hi));
812 /* if ecx >= 32, mov dreg_hi to dreg_lo and set/clear dreg_hi depending on sign */
815 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), 32);
816 /* skip the next two instructions (5 bytes) when < 32 */
819 /* mov dreg_lo,dreg_hi */
820 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
822 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), 31);
825 /* mov dword ptr [ebp+off],dreg_lo */
826 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
828 /* mov dword ptr [ebp+off],dreg_hi */
829 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
836 /* dst = dst >> src */
837 static inline void emit_ia32_rsh_r64(const u8 dst[], const u8 src[], bool dstk,
838 bool sstk, u8 **pprog)
842 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
843 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
846 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
848 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
853 /* mov ecx,dword ptr [ebp+off] */
854 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
858 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_ECX));
860 /* shrd dreg_lo,dreg_hi,cl */
861 EMIT3(0x0F, 0xAD, add_2reg(0xC0, dreg_lo, dreg_hi));
863 EMIT2(0xD3, add_1reg(0xE8, dreg_hi));
865 /* if ecx >= 32, mov dreg_hi to dreg_lo and clear dreg_hi */
868 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), 32);
869 /* skip the next two instructions (4 bytes) when < 32 */
872 /* mov dreg_lo,dreg_hi */
873 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
874 /* xor dreg_hi,dreg_hi */
875 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
878 /* mov dword ptr [ebp+off],dreg_lo */
879 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
881 /* mov dword ptr [ebp+off],dreg_hi */
882 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
889 /* dst = dst << val */
890 static inline void emit_ia32_lsh_i64(const u8 dst[], const u32 val,
891 bool dstk, u8 **pprog)
895 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
896 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
899 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
901 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
904 /* Do LSH operation */
906 /* shld dreg_hi,dreg_lo,imm8 */
907 EMIT4(0x0F, 0xA4, add_2reg(0xC0, dreg_hi, dreg_lo), val);
908 /* shl dreg_lo,imm8 */
909 EMIT3(0xC1, add_1reg(0xE0, dreg_lo), val);
910 } else if (val >= 32 && val < 64) {
911 u32 value = val - 32;
913 /* shl dreg_lo,imm8 */
914 EMIT3(0xC1, add_1reg(0xE0, dreg_lo), value);
915 /* mov dreg_hi,dreg_lo */
916 EMIT2(0x89, add_2reg(0xC0, dreg_hi, dreg_lo));
917 /* xor dreg_lo,dreg_lo */
918 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
920 /* xor dreg_lo,dreg_lo */
921 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
922 /* xor dreg_hi,dreg_hi */
923 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
927 /* mov dword ptr [ebp+off],dreg_lo */
928 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
930 /* mov dword ptr [ebp+off],dreg_hi */
931 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
937 /* dst = dst >> val */
938 static inline void emit_ia32_rsh_i64(const u8 dst[], const u32 val,
939 bool dstk, u8 **pprog)
943 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
944 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
947 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
949 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
953 /* Do RSH operation */
955 /* shrd dreg_lo,dreg_hi,imm8 */
956 EMIT4(0x0F, 0xAC, add_2reg(0xC0, dreg_lo, dreg_hi), val);
957 /* shr dreg_hi,imm8 */
958 EMIT3(0xC1, add_1reg(0xE8, dreg_hi), val);
959 } else if (val >= 32 && val < 64) {
960 u32 value = val - 32;
962 /* shr dreg_hi,imm8 */
963 EMIT3(0xC1, add_1reg(0xE8, dreg_hi), value);
964 /* mov dreg_lo,dreg_hi */
965 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
966 /* xor dreg_hi,dreg_hi */
967 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
969 /* xor dreg_lo,dreg_lo */
970 EMIT2(0x33, add_2reg(0xC0, dreg_lo, dreg_lo));
971 /* xor dreg_hi,dreg_hi */
972 EMIT2(0x33, add_2reg(0xC0, dreg_hi, dreg_hi));
976 /* mov dword ptr [ebp+off],dreg_lo */
977 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
979 /* mov dword ptr [ebp+off],dreg_hi */
980 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
986 /* dst = dst >> val (signed) */
987 static inline void emit_ia32_arsh_i64(const u8 dst[], const u32 val,
988 bool dstk, u8 **pprog)
992 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
993 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
996 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
998 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
1001 /* Do RSH operation */
1003 /* shrd dreg_lo,dreg_hi,imm8 */
1004 EMIT4(0x0F, 0xAC, add_2reg(0xC0, dreg_lo, dreg_hi), val);
1005 /* ashr dreg_hi,imm8 */
1006 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), val);
1007 } else if (val >= 32 && val < 64) {
1008 u32 value = val - 32;
1010 /* ashr dreg_hi,imm8 */
1011 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), value);
1012 /* mov dreg_lo,dreg_hi */
1013 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
1015 /* ashr dreg_hi,imm8 */
1016 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), 31);
1018 /* ashr dreg_hi,imm8 */
1019 EMIT3(0xC1, add_1reg(0xF8, dreg_hi), 31);
1020 /* mov dreg_lo,dreg_hi */
1021 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dreg_hi));
1025 /* mov dword ptr [ebp+off],dreg_lo */
1026 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_lo),
1028 /* mov dword ptr [ebp+off],dreg_hi */
1029 EMIT3(0x89, add_2reg(0x40, IA32_EBP, dreg_hi),
1035 static inline void emit_ia32_mul_r64(const u8 dst[], const u8 src[], bool dstk,
1036 bool sstk, u8 **pprog)
1042 /* mov eax,dword ptr [ebp+off] */
1043 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1046 /* mov eax,dst_hi */
1047 EMIT2(0x8B, add_2reg(0xC0, dst_hi, IA32_EAX));
1050 /* mul dword ptr [ebp+off] */
1051 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(src_lo));
1054 EMIT2(0xF7, add_1reg(0xE0, src_lo));
1057 EMIT2(0x89, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1060 /* mov eax,dword ptr [ebp+off] */
1061 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1064 /* mov eax,dst_lo */
1065 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1068 /* mul dword ptr [ebp+off] */
1069 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(src_hi));
1072 EMIT2(0xF7, add_1reg(0xE0, src_hi));
1075 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1078 /* mov eax,dword ptr [ebp+off] */
1079 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1082 /* mov eax,dst_lo */
1083 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1086 /* mul dword ptr [ebp+off] */
1087 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(src_lo));
1090 EMIT2(0xF7, add_1reg(0xE0, src_lo));
1093 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EDX));
1096 /* mov dword ptr [ebp+off],eax */
1097 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
1099 /* mov dword ptr [ebp+off],ecx */
1100 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_ECX),
1103 /* mov dst_lo,eax */
1104 EMIT2(0x89, add_2reg(0xC0, dst_lo, IA32_EAX));
1105 /* mov dst_hi,ecx */
1106 EMIT2(0x89, add_2reg(0xC0, dst_hi, IA32_ECX));
1112 static inline void emit_ia32_mul_i64(const u8 dst[], const u32 val,
1113 bool dstk, u8 **pprog)
1119 hi = val & (1<<31) ? (u32)~0 : 0;
1120 /* movl eax,imm32 */
1121 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EAX), val);
1123 /* mul dword ptr [ebp+off] */
1124 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(dst_hi));
1127 EMIT2(0xF7, add_1reg(0xE0, dst_hi));
1130 EMIT2(0x89, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1132 /* movl eax,imm32 */
1133 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EAX), hi);
1135 /* mul dword ptr [ebp+off] */
1136 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(dst_lo));
1139 EMIT2(0xF7, add_1reg(0xE0, dst_lo));
1141 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EAX));
1143 /* movl eax,imm32 */
1144 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EAX), val);
1146 /* mul dword ptr [ebp+off] */
1147 EMIT3(0xF7, add_1reg(0x60, IA32_EBP), STACK_VAR(dst_lo));
1150 EMIT2(0xF7, add_1reg(0xE0, dst_lo));
1153 EMIT2(0x01, add_2reg(0xC0, IA32_ECX, IA32_EDX));
1156 /* mov dword ptr [ebp+off],eax */
1157 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
1159 /* mov dword ptr [ebp+off],ecx */
1160 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_ECX),
1163 /* mov dword ptr [ebp+off],eax */
1164 EMIT2(0x89, add_2reg(0xC0, dst_lo, IA32_EAX));
1165 /* mov dword ptr [ebp+off],ecx */
1166 EMIT2(0x89, add_2reg(0xC0, dst_hi, IA32_ECX));
1172 static int bpf_size_to_x86_bytes(int bpf_size)
1174 if (bpf_size == BPF_W)
1176 else if (bpf_size == BPF_H)
1178 else if (bpf_size == BPF_B)
1180 else if (bpf_size == BPF_DW)
1181 return 4; /* imm32 */
1186 struct jit_context {
1187 int cleanup_addr; /* Epilogue code offset */
1190 /* Maximum number of bytes emitted while JITing one eBPF insn */
1191 #define BPF_MAX_INSN_SIZE 128
1192 #define BPF_INSN_SAFETY 64
1194 #define PROLOGUE_SIZE 35
1197 * Emit prologue code for BPF program and check it's size.
1198 * bpf_tail_call helper will skip it while jumping into another program.
1200 static void emit_prologue(u8 **pprog, u32 stack_depth)
1204 const u8 *r1 = bpf2ia32[BPF_REG_1];
1205 const u8 fplo = bpf2ia32[BPF_REG_FP][0];
1206 const u8 fphi = bpf2ia32[BPF_REG_FP][1];
1207 const u8 *tcc = bpf2ia32[TCALL_CNT];
1220 /* sub esp,STACK_SIZE */
1221 EMIT2_off32(0x81, 0xEC, STACK_SIZE);
1222 /* sub ebp,SCRATCH_SIZE+12*/
1223 EMIT3(0x83, add_1reg(0xE8, IA32_EBP), SCRATCH_SIZE + 12);
1225 EMIT2(0x31, add_2reg(0xC0, IA32_EBX, IA32_EBX));
1227 /* Set up BPF prog stack base register */
1228 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBP), STACK_VAR(fplo));
1229 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(fphi));
1231 /* Move BPF_CTX (EAX) to BPF_REG_R1 */
1232 /* mov dword ptr [ebp+off],eax */
1233 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r1[0]));
1234 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(r1[1]));
1236 /* Initialize Tail Count */
1237 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[0]));
1238 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[1]));
1240 BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
1244 /* Emit epilogue code for BPF program */
1245 static void emit_epilogue(u8 **pprog, u32 stack_depth)
1248 const u8 *r0 = bpf2ia32[BPF_REG_0];
1251 /* mov eax,dword ptr [ebp+off]*/
1252 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r0[0]));
1253 /* mov edx,dword ptr [ebp+off]*/
1254 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX), STACK_VAR(r0[1]));
1256 /* add ebp,SCRATCH_SIZE+12*/
1257 EMIT3(0x83, add_1reg(0xC0, IA32_EBP), SCRATCH_SIZE + 12);
1259 /* mov ebx,dword ptr [ebp-12]*/
1260 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EBX), -12);
1261 /* mov esi,dword ptr [ebp-8]*/
1262 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ESI), -8);
1263 /* mov edi,dword ptr [ebp-4]*/
1264 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDI), -4);
1266 EMIT1(0xC9); /* leave */
1267 EMIT1(0xC3); /* ret */
1271 static int emit_jmp_edx(u8 **pprog, u8 *ip)
1276 #ifdef CONFIG_RETPOLINE
1277 EMIT1_off32(0xE9, (u8 *)__x86_indirect_thunk_edx - (ip + 5));
1287 * Generate the following code:
1288 * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
1289 * if (index >= array->map.max_entries)
1291 * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
1293 * prog = array->ptrs[index];
1296 * goto *(prog->bpf_func + prologue_size);
1299 static void emit_bpf_tail_call(u8 **pprog, u8 *ip)
1303 const u8 *r1 = bpf2ia32[BPF_REG_1];
1304 const u8 *r2 = bpf2ia32[BPF_REG_2];
1305 const u8 *r3 = bpf2ia32[BPF_REG_3];
1306 const u8 *tcc = bpf2ia32[TCALL_CNT];
1308 static int jmp_label1 = -1;
1311 * if (index >= array->map.max_entries)
1314 /* mov eax,dword ptr [ebp+off] */
1315 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r2[0]));
1316 /* mov edx,dword ptr [ebp+off] */
1317 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX), STACK_VAR(r3[0]));
1319 /* cmp dword ptr [eax+off],edx */
1320 EMIT3(0x39, add_2reg(0x40, IA32_EAX, IA32_EDX),
1321 offsetof(struct bpf_array, map.max_entries));
1323 EMIT2(IA32_JBE, jmp_label(jmp_label1, 2));
1326 * if (tail_call_cnt > MAX_TAIL_CALL_CNT)
1329 lo = (u32)MAX_TAIL_CALL_CNT;
1330 hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
1331 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(tcc[0]));
1332 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[1]));
1335 EMIT3(0x83, add_1reg(0xF8, IA32_EBX), hi);
1338 EMIT3(0x83, add_1reg(0xF8, IA32_ECX), lo);
1341 EMIT2(IA32_JAE, jmp_label(jmp_label1, 2));
1344 EMIT3(0x83, add_1reg(0xC0, IA32_ECX), 0x01);
1346 EMIT3(0x83, add_1reg(0xD0, IA32_EBX), 0x00);
1348 /* mov dword ptr [ebp+off],eax */
1349 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(tcc[0]));
1350 /* mov dword ptr [ebp+off],edx */
1351 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EBX), STACK_VAR(tcc[1]));
1353 /* prog = array->ptrs[index]; */
1354 /* mov edx, [eax + edx * 4 + offsetof(...)] */
1355 EMIT3_off32(0x8B, 0x94, 0x90, offsetof(struct bpf_array, ptrs));
1362 EMIT2(0x85, add_2reg(0xC0, IA32_EDX, IA32_EDX));
1364 EMIT2(IA32_JE, jmp_label(jmp_label1, 2));
1366 /* goto *(prog->bpf_func + prologue_size); */
1367 /* mov edx, dword ptr [edx + 32] */
1368 EMIT3(0x8B, add_2reg(0x40, IA32_EDX, IA32_EDX),
1369 offsetof(struct bpf_prog, bpf_func));
1370 /* add edx,prologue_size */
1371 EMIT3(0x83, add_1reg(0xC0, IA32_EDX), PROLOGUE_SIZE);
1373 /* mov eax,dword ptr [ebp+off] */
1374 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX), STACK_VAR(r1[0]));
1377 * Now we're ready to jump into next BPF program:
1378 * eax == ctx (1st arg)
1379 * edx == prog->bpf_func + prologue_size
1381 cnt += emit_jmp_edx(&prog, ip + cnt);
1383 if (jmp_label1 == -1)
1390 /* Push the scratch stack register on top of the stack. */
1391 static inline void emit_push_r64(const u8 src[], u8 **pprog)
1396 /* mov ecx,dword ptr [ebp+off] */
1397 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src_hi));
1401 /* mov ecx,dword ptr [ebp+off] */
1402 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX), STACK_VAR(src_lo));
1409 static u8 get_cond_jmp_opcode(const u8 op, bool is_cmp_lo)
1413 /* Convert BPF opcode to x86 */
1420 jmp_cond = IA32_JNE;
1423 /* GT is unsigned '>', JA in x86 */
1427 /* LT is unsigned '<', JB in x86 */
1431 /* GE is unsigned '>=', JAE in x86 */
1432 jmp_cond = IA32_JAE;
1435 /* LE is unsigned '<=', JBE in x86 */
1436 jmp_cond = IA32_JBE;
1440 /* Signed '>', GT in x86 */
1443 /* GT is unsigned '>', JA in x86 */
1448 /* Signed '<', LT in x86 */
1451 /* LT is unsigned '<', JB in x86 */
1456 /* Signed '>=', GE in x86 */
1457 jmp_cond = IA32_JGE;
1459 /* GE is unsigned '>=', JAE in x86 */
1460 jmp_cond = IA32_JAE;
1464 /* Signed '<=', LE in x86 */
1465 jmp_cond = IA32_JLE;
1467 /* LE is unsigned '<=', JBE in x86 */
1468 jmp_cond = IA32_JBE;
1470 default: /* to silence GCC warning */
1471 jmp_cond = COND_JMP_OPCODE_INVALID;
1478 static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
1479 int oldproglen, struct jit_context *ctx)
1481 struct bpf_insn *insn = bpf_prog->insnsi;
1482 int insn_cnt = bpf_prog->len;
1483 bool seen_exit = false;
1484 u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
1489 emit_prologue(&prog, bpf_prog->aux->stack_depth);
1491 for (i = 0; i < insn_cnt; i++, insn++) {
1492 const s32 imm32 = insn->imm;
1493 const bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
1494 const bool dstk = insn->dst_reg != BPF_REG_AX;
1495 const bool sstk = insn->src_reg != BPF_REG_AX;
1496 const u8 code = insn->code;
1497 const u8 *dst = bpf2ia32[insn->dst_reg];
1498 const u8 *src = bpf2ia32[insn->src_reg];
1499 const u8 *r0 = bpf2ia32[BPF_REG_0];
1506 /* ALU operations */
1508 case BPF_ALU | BPF_MOV | BPF_K:
1509 case BPF_ALU | BPF_MOV | BPF_X:
1510 case BPF_ALU64 | BPF_MOV | BPF_K:
1511 case BPF_ALU64 | BPF_MOV | BPF_X:
1512 switch (BPF_SRC(code)) {
1515 /* Special mov32 for zext. */
1516 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1519 emit_ia32_mov_r64(is64, dst, src, dstk, sstk,
1520 &prog, bpf_prog->aux);
1523 /* Sign-extend immediate value to dst reg */
1524 emit_ia32_mov_i64(is64, dst, imm32,
1529 /* dst = dst + src/imm */
1530 /* dst = dst - src/imm */
1531 /* dst = dst | src/imm */
1532 /* dst = dst & src/imm */
1533 /* dst = dst ^ src/imm */
1534 /* dst = dst * src/imm */
1535 /* dst = dst << src */
1536 /* dst = dst >> src */
1537 case BPF_ALU | BPF_ADD | BPF_K:
1538 case BPF_ALU | BPF_ADD | BPF_X:
1539 case BPF_ALU | BPF_SUB | BPF_K:
1540 case BPF_ALU | BPF_SUB | BPF_X:
1541 case BPF_ALU | BPF_OR | BPF_K:
1542 case BPF_ALU | BPF_OR | BPF_X:
1543 case BPF_ALU | BPF_AND | BPF_K:
1544 case BPF_ALU | BPF_AND | BPF_X:
1545 case BPF_ALU | BPF_XOR | BPF_K:
1546 case BPF_ALU | BPF_XOR | BPF_X:
1547 case BPF_ALU64 | BPF_ADD | BPF_K:
1548 case BPF_ALU64 | BPF_ADD | BPF_X:
1549 case BPF_ALU64 | BPF_SUB | BPF_K:
1550 case BPF_ALU64 | BPF_SUB | BPF_X:
1551 case BPF_ALU64 | BPF_OR | BPF_K:
1552 case BPF_ALU64 | BPF_OR | BPF_X:
1553 case BPF_ALU64 | BPF_AND | BPF_K:
1554 case BPF_ALU64 | BPF_AND | BPF_X:
1555 case BPF_ALU64 | BPF_XOR | BPF_K:
1556 case BPF_ALU64 | BPF_XOR | BPF_X:
1557 switch (BPF_SRC(code)) {
1559 emit_ia32_alu_r64(is64, BPF_OP(code), dst,
1560 src, dstk, sstk, &prog,
1564 emit_ia32_alu_i64(is64, BPF_OP(code), dst,
1570 case BPF_ALU | BPF_MUL | BPF_K:
1571 case BPF_ALU | BPF_MUL | BPF_X:
1572 switch (BPF_SRC(code)) {
1574 emit_ia32_mul_r(dst_lo, src_lo, dstk,
1579 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX),
1581 emit_ia32_mul_r(dst_lo, IA32_ECX, dstk,
1585 if (!bpf_prog->aux->verifier_zext)
1586 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1588 case BPF_ALU | BPF_LSH | BPF_X:
1589 case BPF_ALU | BPF_RSH | BPF_X:
1590 case BPF_ALU | BPF_ARSH | BPF_K:
1591 case BPF_ALU | BPF_ARSH | BPF_X:
1592 switch (BPF_SRC(code)) {
1594 emit_ia32_shift_r(BPF_OP(code), dst_lo, src_lo,
1599 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX),
1601 emit_ia32_shift_r(BPF_OP(code), dst_lo,
1602 IA32_ECX, dstk, false,
1606 if (!bpf_prog->aux->verifier_zext)
1607 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1609 /* dst = dst / src(imm) */
1610 /* dst = dst % src(imm) */
1611 case BPF_ALU | BPF_DIV | BPF_K:
1612 case BPF_ALU | BPF_DIV | BPF_X:
1613 case BPF_ALU | BPF_MOD | BPF_K:
1614 case BPF_ALU | BPF_MOD | BPF_X:
1615 switch (BPF_SRC(code)) {
1617 emit_ia32_div_mod_r(BPF_OP(code), dst_lo,
1618 src_lo, dstk, sstk, &prog);
1622 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX),
1624 emit_ia32_div_mod_r(BPF_OP(code), dst_lo,
1625 IA32_ECX, dstk, false,
1629 if (!bpf_prog->aux->verifier_zext)
1630 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1632 case BPF_ALU64 | BPF_DIV | BPF_K:
1633 case BPF_ALU64 | BPF_DIV | BPF_X:
1634 case BPF_ALU64 | BPF_MOD | BPF_K:
1635 case BPF_ALU64 | BPF_MOD | BPF_X:
1637 /* dst = dst >> imm */
1638 /* dst = dst << imm */
1639 case BPF_ALU | BPF_RSH | BPF_K:
1640 case BPF_ALU | BPF_LSH | BPF_K:
1641 if (unlikely(imm32 > 31))
1644 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX), imm32);
1645 emit_ia32_shift_r(BPF_OP(code), dst_lo, IA32_ECX, dstk,
1647 if (!bpf_prog->aux->verifier_zext)
1648 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1650 /* dst = dst << imm */
1651 case BPF_ALU64 | BPF_LSH | BPF_K:
1652 if (unlikely(imm32 > 63))
1654 emit_ia32_lsh_i64(dst, imm32, dstk, &prog);
1656 /* dst = dst >> imm */
1657 case BPF_ALU64 | BPF_RSH | BPF_K:
1658 if (unlikely(imm32 > 63))
1660 emit_ia32_rsh_i64(dst, imm32, dstk, &prog);
1662 /* dst = dst << src */
1663 case BPF_ALU64 | BPF_LSH | BPF_X:
1664 emit_ia32_lsh_r64(dst, src, dstk, sstk, &prog);
1666 /* dst = dst >> src */
1667 case BPF_ALU64 | BPF_RSH | BPF_X:
1668 emit_ia32_rsh_r64(dst, src, dstk, sstk, &prog);
1670 /* dst = dst >> src (signed) */
1671 case BPF_ALU64 | BPF_ARSH | BPF_X:
1672 emit_ia32_arsh_r64(dst, src, dstk, sstk, &prog);
1674 /* dst = dst >> imm (signed) */
1675 case BPF_ALU64 | BPF_ARSH | BPF_K:
1676 if (unlikely(imm32 > 63))
1678 emit_ia32_arsh_i64(dst, imm32, dstk, &prog);
1681 case BPF_ALU | BPF_NEG:
1682 emit_ia32_alu_i(is64, false, BPF_OP(code),
1683 dst_lo, 0, dstk, &prog);
1684 if (!bpf_prog->aux->verifier_zext)
1685 emit_ia32_mov_i(dst_hi, 0, dstk, &prog);
1687 /* dst = ~dst (64 bit) */
1688 case BPF_ALU64 | BPF_NEG:
1689 emit_ia32_neg64(dst, dstk, &prog);
1691 /* dst = dst * src/imm */
1692 case BPF_ALU64 | BPF_MUL | BPF_X:
1693 case BPF_ALU64 | BPF_MUL | BPF_K:
1694 switch (BPF_SRC(code)) {
1696 emit_ia32_mul_r64(dst, src, dstk, sstk, &prog);
1699 emit_ia32_mul_i64(dst, imm32, dstk, &prog);
1703 /* dst = htole(dst) */
1704 case BPF_ALU | BPF_END | BPF_FROM_LE:
1705 emit_ia32_to_le_r64(dst, imm32, dstk, &prog,
1708 /* dst = htobe(dst) */
1709 case BPF_ALU | BPF_END | BPF_FROM_BE:
1710 emit_ia32_to_be_r64(dst, imm32, dstk, &prog,
1714 case BPF_LD | BPF_IMM | BPF_DW: {
1718 emit_ia32_mov_i(dst_lo, lo, dstk, &prog);
1719 emit_ia32_mov_i(dst_hi, hi, dstk, &prog);
1724 /* speculation barrier */
1725 case BPF_ST | BPF_NOSPEC:
1726 if (boot_cpu_has(X86_FEATURE_XMM2))
1728 EMIT3(0x0F, 0xAE, 0xE8);
1730 /* ST: *(u8*)(dst_reg + off) = imm */
1731 case BPF_ST | BPF_MEM | BPF_H:
1732 case BPF_ST | BPF_MEM | BPF_B:
1733 case BPF_ST | BPF_MEM | BPF_W:
1734 case BPF_ST | BPF_MEM | BPF_DW:
1736 /* mov eax,dword ptr [ebp+off] */
1737 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1740 /* mov eax,dst_lo */
1741 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1743 switch (BPF_SIZE(code)) {
1745 EMIT(0xC6, 1); break;
1747 EMIT2(0x66, 0xC7); break;
1750 EMIT(0xC7, 1); break;
1753 if (is_imm8(insn->off))
1754 EMIT2(add_1reg(0x40, IA32_EAX), insn->off);
1756 EMIT1_off32(add_1reg(0x80, IA32_EAX),
1758 EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(code)));
1760 if (BPF_SIZE(code) == BPF_DW) {
1763 hi = imm32 & (1<<31) ? (u32)~0 : 0;
1764 EMIT2_off32(0xC7, add_1reg(0x80, IA32_EAX),
1770 /* STX: *(u8*)(dst_reg + off) = src_reg */
1771 case BPF_STX | BPF_MEM | BPF_B:
1772 case BPF_STX | BPF_MEM | BPF_H:
1773 case BPF_STX | BPF_MEM | BPF_W:
1774 case BPF_STX | BPF_MEM | BPF_DW:
1776 /* mov eax,dword ptr [ebp+off] */
1777 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1780 /* mov eax,dst_lo */
1781 EMIT2(0x8B, add_2reg(0xC0, dst_lo, IA32_EAX));
1784 /* mov edx,dword ptr [ebp+off] */
1785 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
1788 /* mov edx,src_lo */
1789 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_EDX));
1791 switch (BPF_SIZE(code)) {
1793 EMIT(0x88, 1); break;
1795 EMIT2(0x66, 0x89); break;
1798 EMIT(0x89, 1); break;
1801 if (is_imm8(insn->off))
1802 EMIT2(add_2reg(0x40, IA32_EAX, IA32_EDX),
1805 EMIT1_off32(add_2reg(0x80, IA32_EAX, IA32_EDX),
1808 if (BPF_SIZE(code) == BPF_DW) {
1810 /* mov edi,dword ptr [ebp+off] */
1811 EMIT3(0x8B, add_2reg(0x40, IA32_EBP,
1815 /* mov edi,src_hi */
1816 EMIT2(0x8B, add_2reg(0xC0, src_hi,
1819 if (is_imm8(insn->off + 4)) {
1820 EMIT2(add_2reg(0x40, IA32_EAX,
1824 EMIT1(add_2reg(0x80, IA32_EAX,
1826 EMIT(insn->off + 4, 4);
1831 /* LDX: dst_reg = *(u8*)(src_reg + off) */
1832 case BPF_LDX | BPF_MEM | BPF_B:
1833 case BPF_LDX | BPF_MEM | BPF_H:
1834 case BPF_LDX | BPF_MEM | BPF_W:
1835 case BPF_LDX | BPF_MEM | BPF_DW:
1837 /* mov eax,dword ptr [ebp+off] */
1838 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1841 /* mov eax,dword ptr [ebp+off] */
1842 EMIT2(0x8B, add_2reg(0xC0, src_lo, IA32_EAX));
1844 switch (BPF_SIZE(code)) {
1846 EMIT2(0x0F, 0xB6); break;
1848 EMIT2(0x0F, 0xB7); break;
1851 EMIT(0x8B, 1); break;
1854 if (is_imm8(insn->off))
1855 EMIT2(add_2reg(0x40, IA32_EAX, IA32_EDX),
1858 EMIT1_off32(add_2reg(0x80, IA32_EAX, IA32_EDX),
1862 /* mov dword ptr [ebp+off],edx */
1863 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EDX),
1866 /* mov dst_lo,edx */
1867 EMIT2(0x89, add_2reg(0xC0, dst_lo, IA32_EDX));
1868 switch (BPF_SIZE(code)) {
1872 if (bpf_prog->aux->verifier_zext)
1875 EMIT3(0xC7, add_1reg(0x40, IA32_EBP),
1879 /* xor dst_hi,dst_hi */
1881 add_2reg(0xC0, dst_hi, dst_hi));
1886 add_2reg(0x80, IA32_EAX, IA32_EDX),
1890 add_2reg(0x40, IA32_EBP,
1895 add_2reg(0xC0, dst_hi, IA32_EDX));
1902 case BPF_JMP | BPF_CALL:
1904 const u8 *r1 = bpf2ia32[BPF_REG_1];
1905 const u8 *r2 = bpf2ia32[BPF_REG_2];
1906 const u8 *r3 = bpf2ia32[BPF_REG_3];
1907 const u8 *r4 = bpf2ia32[BPF_REG_4];
1908 const u8 *r5 = bpf2ia32[BPF_REG_5];
1910 if (insn->src_reg == BPF_PSEUDO_CALL)
1913 func = (u8 *) __bpf_call_base + imm32;
1914 jmp_offset = func - (image + addrs[i]);
1916 if (!imm32 || !is_simm32(jmp_offset)) {
1917 pr_err("unsupported BPF func %d addr %p image %p\n",
1918 imm32, func, image);
1922 /* mov eax,dword ptr [ebp+off] */
1923 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1925 /* mov edx,dword ptr [ebp+off] */
1926 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EDX),
1929 emit_push_r64(r5, &prog);
1930 emit_push_r64(r4, &prog);
1931 emit_push_r64(r3, &prog);
1932 emit_push_r64(r2, &prog);
1934 EMIT1_off32(0xE8, jmp_offset + 9);
1936 /* mov dword ptr [ebp+off],eax */
1937 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EAX),
1939 /* mov dword ptr [ebp+off],edx */
1940 EMIT3(0x89, add_2reg(0x40, IA32_EBP, IA32_EDX),
1944 EMIT3(0x83, add_1reg(0xC0, IA32_ESP), 32);
1947 case BPF_JMP | BPF_TAIL_CALL:
1948 emit_bpf_tail_call(&prog, image + addrs[i - 1]);
1952 case BPF_JMP | BPF_JEQ | BPF_X:
1953 case BPF_JMP | BPF_JNE | BPF_X:
1954 case BPF_JMP | BPF_JGT | BPF_X:
1955 case BPF_JMP | BPF_JLT | BPF_X:
1956 case BPF_JMP | BPF_JGE | BPF_X:
1957 case BPF_JMP | BPF_JLE | BPF_X:
1958 case BPF_JMP32 | BPF_JEQ | BPF_X:
1959 case BPF_JMP32 | BPF_JNE | BPF_X:
1960 case BPF_JMP32 | BPF_JGT | BPF_X:
1961 case BPF_JMP32 | BPF_JLT | BPF_X:
1962 case BPF_JMP32 | BPF_JGE | BPF_X:
1963 case BPF_JMP32 | BPF_JLE | BPF_X:
1964 case BPF_JMP32 | BPF_JSGT | BPF_X:
1965 case BPF_JMP32 | BPF_JSLE | BPF_X:
1966 case BPF_JMP32 | BPF_JSLT | BPF_X:
1967 case BPF_JMP32 | BPF_JSGE | BPF_X: {
1968 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
1969 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
1970 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
1971 u8 sreg_lo = sstk ? IA32_ECX : src_lo;
1972 u8 sreg_hi = sstk ? IA32_EBX : src_hi;
1975 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
1979 add_2reg(0x40, IA32_EBP,
1985 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
1989 add_2reg(0x40, IA32_EBP,
1995 /* cmp dreg_hi,sreg_hi */
1996 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
1999 /* cmp dreg_lo,sreg_lo */
2000 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2003 case BPF_JMP | BPF_JSGT | BPF_X:
2004 case BPF_JMP | BPF_JSLE | BPF_X:
2005 case BPF_JMP | BPF_JSLT | BPF_X:
2006 case BPF_JMP | BPF_JSGE | BPF_X: {
2007 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2008 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2009 u8 sreg_lo = sstk ? IA32_ECX : src_lo;
2010 u8 sreg_hi = sstk ? IA32_EBX : src_hi;
2013 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2016 add_2reg(0x40, IA32_EBP,
2022 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
2025 add_2reg(0x40, IA32_EBP,
2030 /* cmp dreg_hi,sreg_hi */
2031 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
2032 EMIT2(IA32_JNE, 10);
2033 /* cmp dreg_lo,sreg_lo */
2034 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2035 goto emit_cond_jmp_signed;
2037 case BPF_JMP | BPF_JSET | BPF_X:
2038 case BPF_JMP32 | BPF_JSET | BPF_X: {
2039 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2040 u8 dreg_lo = IA32_EAX;
2041 u8 dreg_hi = IA32_EDX;
2042 u8 sreg_lo = sstk ? IA32_ECX : src_lo;
2043 u8 sreg_hi = sstk ? IA32_EBX : src_hi;
2046 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2050 add_2reg(0x40, IA32_EBP,
2054 /* mov dreg_lo,dst_lo */
2055 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dst_lo));
2057 /* mov dreg_hi,dst_hi */
2059 add_2reg(0xC0, dreg_hi, dst_hi));
2063 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_ECX),
2067 add_2reg(0x40, IA32_EBP,
2071 /* and dreg_lo,sreg_lo */
2072 EMIT2(0x23, add_2reg(0xC0, sreg_lo, dreg_lo));
2074 /* and dreg_hi,sreg_hi */
2075 EMIT2(0x23, add_2reg(0xC0, sreg_hi, dreg_hi));
2076 /* or dreg_lo,dreg_hi */
2077 EMIT2(0x09, add_2reg(0xC0, dreg_lo, dreg_hi));
2081 case BPF_JMP | BPF_JSET | BPF_K:
2082 case BPF_JMP32 | BPF_JSET | BPF_K: {
2083 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2084 u8 dreg_lo = IA32_EAX;
2085 u8 dreg_hi = IA32_EDX;
2086 u8 sreg_lo = IA32_ECX;
2087 u8 sreg_hi = IA32_EBX;
2091 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2095 add_2reg(0x40, IA32_EBP,
2099 /* mov dreg_lo,dst_lo */
2100 EMIT2(0x89, add_2reg(0xC0, dreg_lo, dst_lo));
2102 /* mov dreg_hi,dst_hi */
2104 add_2reg(0xC0, dreg_hi, dst_hi));
2108 EMIT2_off32(0xC7, add_1reg(0xC0, sreg_lo), imm32);
2110 /* and dreg_lo,sreg_lo */
2111 EMIT2(0x23, add_2reg(0xC0, sreg_lo, dreg_lo));
2113 hi = imm32 & (1 << 31) ? (u32)~0 : 0;
2115 EMIT2_off32(0xC7, add_1reg(0xC0, sreg_hi), hi);
2116 /* and dreg_hi,sreg_hi */
2117 EMIT2(0x23, add_2reg(0xC0, sreg_hi, dreg_hi));
2118 /* or dreg_lo,dreg_hi */
2119 EMIT2(0x09, add_2reg(0xC0, dreg_lo, dreg_hi));
2123 case BPF_JMP | BPF_JEQ | BPF_K:
2124 case BPF_JMP | BPF_JNE | BPF_K:
2125 case BPF_JMP | BPF_JGT | BPF_K:
2126 case BPF_JMP | BPF_JLT | BPF_K:
2127 case BPF_JMP | BPF_JGE | BPF_K:
2128 case BPF_JMP | BPF_JLE | BPF_K:
2129 case BPF_JMP32 | BPF_JEQ | BPF_K:
2130 case BPF_JMP32 | BPF_JNE | BPF_K:
2131 case BPF_JMP32 | BPF_JGT | BPF_K:
2132 case BPF_JMP32 | BPF_JLT | BPF_K:
2133 case BPF_JMP32 | BPF_JGE | BPF_K:
2134 case BPF_JMP32 | BPF_JLE | BPF_K:
2135 case BPF_JMP32 | BPF_JSGT | BPF_K:
2136 case BPF_JMP32 | BPF_JSLE | BPF_K:
2137 case BPF_JMP32 | BPF_JSLT | BPF_K:
2138 case BPF_JMP32 | BPF_JSGE | BPF_K: {
2139 bool is_jmp64 = BPF_CLASS(insn->code) == BPF_JMP;
2140 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2141 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2142 u8 sreg_lo = IA32_ECX;
2143 u8 sreg_hi = IA32_EBX;
2147 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2151 add_2reg(0x40, IA32_EBP,
2157 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX), imm32);
2159 hi = imm32 & (1 << 31) ? (u32)~0 : 0;
2161 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EBX), hi);
2162 /* cmp dreg_hi,sreg_hi */
2163 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
2166 /* cmp dreg_lo,sreg_lo */
2167 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2169 emit_cond_jmp: jmp_cond = get_cond_jmp_opcode(BPF_OP(code), false);
2170 if (jmp_cond == COND_JMP_OPCODE_INVALID)
2172 jmp_offset = addrs[i + insn->off] - addrs[i];
2173 if (is_imm8(jmp_offset)) {
2174 EMIT2(jmp_cond, jmp_offset);
2175 } else if (is_simm32(jmp_offset)) {
2176 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
2178 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
2183 case BPF_JMP | BPF_JSGT | BPF_K:
2184 case BPF_JMP | BPF_JSLE | BPF_K:
2185 case BPF_JMP | BPF_JSLT | BPF_K:
2186 case BPF_JMP | BPF_JSGE | BPF_K: {
2187 u8 dreg_lo = dstk ? IA32_EAX : dst_lo;
2188 u8 dreg_hi = dstk ? IA32_EDX : dst_hi;
2189 u8 sreg_lo = IA32_ECX;
2190 u8 sreg_hi = IA32_EBX;
2194 EMIT3(0x8B, add_2reg(0x40, IA32_EBP, IA32_EAX),
2197 add_2reg(0x40, IA32_EBP,
2203 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_ECX), imm32);
2204 hi = imm32 & (1 << 31) ? (u32)~0 : 0;
2206 EMIT2_off32(0xC7, add_1reg(0xC0, IA32_EBX), hi);
2207 /* cmp dreg_hi,sreg_hi */
2208 EMIT2(0x39, add_2reg(0xC0, dreg_hi, sreg_hi));
2209 EMIT2(IA32_JNE, 10);
2210 /* cmp dreg_lo,sreg_lo */
2211 EMIT2(0x39, add_2reg(0xC0, dreg_lo, sreg_lo));
2214 * For simplicity of branch offset computation,
2215 * let's use fixed jump coding here.
2217 emit_cond_jmp_signed: /* Check the condition for low 32-bit comparison */
2218 jmp_cond = get_cond_jmp_opcode(BPF_OP(code), true);
2219 if (jmp_cond == COND_JMP_OPCODE_INVALID)
2221 jmp_offset = addrs[i + insn->off] - addrs[i] + 8;
2222 if (is_simm32(jmp_offset)) {
2223 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
2225 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
2230 /* Check the condition for high 32-bit comparison */
2231 jmp_cond = get_cond_jmp_opcode(BPF_OP(code), false);
2232 if (jmp_cond == COND_JMP_OPCODE_INVALID)
2234 jmp_offset = addrs[i + insn->off] - addrs[i];
2235 if (is_simm32(jmp_offset)) {
2236 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
2238 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
2243 case BPF_JMP | BPF_JA:
2244 if (insn->off == -1)
2245 /* -1 jmp instructions will always jump
2246 * backwards two bytes. Explicitly handling
2247 * this case avoids wasting too many passes
2248 * when there are long sequences of replaced
2253 jmp_offset = addrs[i + insn->off] - addrs[i];
2256 /* Optimize out nop jumps */
2259 if (is_imm8(jmp_offset)) {
2260 EMIT2(0xEB, jmp_offset);
2261 } else if (is_simm32(jmp_offset)) {
2262 EMIT1_off32(0xE9, jmp_offset);
2264 pr_err("jmp gen bug %llx\n", jmp_offset);
2268 /* STX XADD: lock *(u32 *)(dst + off) += src */
2269 case BPF_STX | BPF_XADD | BPF_W:
2270 /* STX XADD: lock *(u64 *)(dst + off) += src */
2271 case BPF_STX | BPF_XADD | BPF_DW:
2273 case BPF_JMP | BPF_EXIT:
2275 jmp_offset = ctx->cleanup_addr - addrs[i];
2279 /* Update cleanup_addr */
2280 ctx->cleanup_addr = proglen;
2281 emit_epilogue(&prog, bpf_prog->aux->stack_depth);
2284 pr_info_once("*** NOT YET: opcode %02x ***\n", code);
2288 * This error will be seen if new instruction was added
2289 * to interpreter, but not to JIT or if there is junk in
2292 pr_err("bpf_jit: unknown opcode %02x\n", code);
2297 if (ilen > BPF_MAX_INSN_SIZE) {
2298 pr_err("bpf_jit: fatal insn size error\n");
2304 * When populating the image, assert that:
2306 * i) We do not write beyond the allocated space, and
2307 * ii) addrs[i] did not change from the prior run, in order
2308 * to validate assumptions made for computing branch
2311 if (unlikely(proglen + ilen > oldproglen ||
2312 proglen + ilen != addrs[i])) {
2313 pr_err("bpf_jit: fatal error\n");
2316 memcpy(image + proglen, temp, ilen);
2325 bool bpf_jit_needs_zext(void)
2330 struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
2332 struct bpf_binary_header *header = NULL;
2333 struct bpf_prog *tmp, *orig_prog = prog;
2334 int proglen, oldproglen = 0;
2335 struct jit_context ctx = {};
2336 bool tmp_blinded = false;
2342 if (!prog->jit_requested)
2345 tmp = bpf_jit_blind_constants(prog);
2347 * If blinding was requested and we failed during blinding,
2348 * we must fall back to the interpreter.
2357 addrs = kmalloc_array(prog->len, sizeof(*addrs), GFP_KERNEL);
2364 * Before first pass, make a rough estimation of addrs[]
2365 * each BPF instruction is translated to less than 64 bytes
2367 for (proglen = 0, i = 0; i < prog->len; i++) {
2371 ctx.cleanup_addr = proglen;
2374 * JITed image shrinks with every pass and the loop iterates
2375 * until the image stops shrinking. Very large BPF programs
2376 * may converge on the last pass. In such case do one more
2377 * pass to emit the final image.
2379 for (pass = 0; pass < 20 || image; pass++) {
2380 proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
2385 bpf_jit_binary_free(header);
2390 if (proglen != oldproglen) {
2391 pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
2392 proglen, oldproglen);
2397 if (proglen == oldproglen) {
2398 header = bpf_jit_binary_alloc(proglen, &image,
2405 oldproglen = proglen;
2409 if (bpf_jit_enable > 1)
2410 bpf_jit_dump(prog->len, proglen, pass + 1, image);
2413 bpf_jit_binary_lock_ro(header);
2414 prog->bpf_func = (void *)image;
2416 prog->jited_len = proglen;
2425 bpf_jit_prog_release_other(prog, prog == orig_prog ?