1 /* bpf_jit_comp.c : BPF JIT compiler
3 * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
4 * Internal BPF Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
11 #include <linux/netdevice.h>
12 #include <linux/filter.h>
13 #include <linux/if_vlan.h>
14 #include <asm/cacheflush.h>
15 #include <asm/nospec-branch.h>
16 #include <linux/bpf.h>
18 int bpf_jit_enable __read_mostly;
21 * assembly code in arch/x86/net/bpf_jit.S
23 extern u8 sk_load_word[], sk_load_half[], sk_load_byte[];
24 extern u8 sk_load_word_positive_offset[], sk_load_half_positive_offset[];
25 extern u8 sk_load_byte_positive_offset[];
26 extern u8 sk_load_word_negative_offset[], sk_load_half_negative_offset[];
27 extern u8 sk_load_byte_negative_offset[];
29 static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
42 #define EMIT(bytes, len) \
43 do { prog = emit_code(prog, bytes, len); cnt += len; } while (0)
45 #define EMIT1(b1) EMIT(b1, 1)
46 #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
47 #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
48 #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
49 #define EMIT1_off32(b1, off) \
50 do {EMIT1(b1); EMIT(off, 4); } while (0)
51 #define EMIT2_off32(b1, b2, off) \
52 do {EMIT2(b1, b2); EMIT(off, 4); } while (0)
53 #define EMIT3_off32(b1, b2, b3, off) \
54 do {EMIT3(b1, b2, b3); EMIT(off, 4); } while (0)
55 #define EMIT4_off32(b1, b2, b3, b4, off) \
56 do {EMIT4(b1, b2, b3, b4); EMIT(off, 4); } while (0)
58 static bool is_imm8(int value)
60 return value <= 127 && value >= -128;
63 static bool is_simm32(s64 value)
65 return value == (s64) (s32) value;
69 #define EMIT_mov(DST, SRC) \
71 EMIT3(add_2mod(0x48, DST, SRC), 0x89, add_2reg(0xC0, DST, SRC)); \
74 static int bpf_size_to_x86_bytes(int bpf_size)
76 if (bpf_size == BPF_W)
78 else if (bpf_size == BPF_H)
80 else if (bpf_size == BPF_B)
82 else if (bpf_size == BPF_DW)
88 /* list of x86 cond jumps opcodes (. + s8)
89 * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
100 static void bpf_flush_icache(void *start, void *end)
102 mm_segment_t old_fs = get_fs();
106 flush_icache_range((unsigned long)start, (unsigned long)end);
110 #define CHOOSE_LOAD_FUNC(K, func) \
111 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
113 /* pick a register outside of BPF range for JIT internal work */
114 #define AUX_REG (MAX_BPF_REG + 1)
116 /* the following table maps BPF registers to x64 registers.
117 * x64 register r12 is unused, since if used as base address register
118 * in load/store instructions, it always needs an extra byte of encoding
120 static const int reg2hex[] = {
121 [BPF_REG_0] = 0, /* rax */
122 [BPF_REG_1] = 7, /* rdi */
123 [BPF_REG_2] = 6, /* rsi */
124 [BPF_REG_3] = 2, /* rdx */
125 [BPF_REG_4] = 1, /* rcx */
126 [BPF_REG_5] = 0, /* r8 */
127 [BPF_REG_6] = 3, /* rbx callee saved */
128 [BPF_REG_7] = 5, /* r13 callee saved */
129 [BPF_REG_8] = 6, /* r14 callee saved */
130 [BPF_REG_9] = 7, /* r15 callee saved */
131 [BPF_REG_FP] = 5, /* rbp readonly */
132 [AUX_REG] = 3, /* r11 temp register */
135 /* is_ereg() == true if BPF register 'reg' maps to x64 r8..r15
136 * which need extra byte of encoding.
137 * rax,rcx,...,rbp have simpler encoding
139 static bool is_ereg(u32 reg)
141 return (1 << reg) & (BIT(BPF_REG_5) |
149 * is_ereg_8l() == true if BPF register 'reg' is mapped to access x86-64
150 * lower 8-bit registers dil,sil,bpl,spl,r8b..r15b, which need extra byte
151 * of encoding. al,cl,dl,bl have simpler encoding.
153 static bool is_ereg_8l(u32 reg)
155 return is_ereg(reg) ||
156 (1 << reg) & (BIT(BPF_REG_1) |
161 /* add modifiers if 'reg' maps to x64 registers r8..r15 */
162 static u8 add_1mod(u8 byte, u32 reg)
169 static u8 add_2mod(u8 byte, u32 r1, u32 r2)
178 /* encode 'dst_reg' register into x64 opcode 'byte' */
179 static u8 add_1reg(u8 byte, u32 dst_reg)
181 return byte + reg2hex[dst_reg];
184 /* encode 'dst_reg' and 'src_reg' registers into x64 opcode 'byte' */
185 static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg)
187 return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3);
190 static void jit_fill_hole(void *area, unsigned int size)
192 /* fill whole space with int3 instructions */
193 memset(area, 0xcc, size);
197 int cleanup_addr; /* epilogue code offset */
201 /* maximum number of bytes emitted while JITing one eBPF insn */
202 #define BPF_MAX_INSN_SIZE 128
203 #define BPF_INSN_SAFETY 64
207 32 /* space for rbx, r13, r14, r15 */ + \
208 8 /* space for skb_copy_bits() buffer */)
210 #define PROLOGUE_SIZE 51
212 /* emit x64 prologue code for BPF program and check it's size.
213 * bpf_tail_call helper will skip it while jumping into another program
215 static void emit_prologue(u8 **pprog)
220 EMIT1(0x55); /* push rbp */
221 EMIT3(0x48, 0x89, 0xE5); /* mov rbp,rsp */
223 /* sub rsp, STACKSIZE */
224 EMIT3_off32(0x48, 0x81, 0xEC, STACKSIZE);
226 /* all classic BPF filters use R6(rbx) save it */
228 /* mov qword ptr [rbp-X],rbx */
229 EMIT3_off32(0x48, 0x89, 0x9D, -STACKSIZE);
231 /* bpf_convert_filter() maps classic BPF register X to R7 and uses R8
232 * as temporary, so all tcpdump filters need to spill/fill R7(r13) and
233 * R8(r14). R9(r15) spill could be made conditional, but there is only
234 * one 'bpf_error' return path out of helper functions inside bpf_jit.S
235 * The overhead of extra spill is negligible for any filter other
236 * than synthetic ones. Therefore not worth adding complexity.
239 /* mov qword ptr [rbp-X],r13 */
240 EMIT3_off32(0x4C, 0x89, 0xAD, -STACKSIZE + 8);
241 /* mov qword ptr [rbp-X],r14 */
242 EMIT3_off32(0x4C, 0x89, 0xB5, -STACKSIZE + 16);
243 /* mov qword ptr [rbp-X],r15 */
244 EMIT3_off32(0x4C, 0x89, 0xBD, -STACKSIZE + 24);
246 /* clear A and X registers */
247 EMIT2(0x31, 0xc0); /* xor eax, eax */
248 EMIT3(0x4D, 0x31, 0xED); /* xor r13, r13 */
250 /* clear tail_cnt: mov qword ptr [rbp-X], rax */
251 EMIT3_off32(0x48, 0x89, 0x85, -STACKSIZE + 32);
253 BUILD_BUG_ON(cnt != PROLOGUE_SIZE);
257 /* generate the following code:
258 * ... bpf_tail_call(void *ctx, struct bpf_array *array, u64 index) ...
259 * if (index >= array->map.max_entries)
261 * if (++tail_call_cnt > MAX_TAIL_CALL_CNT)
263 * prog = array->ptrs[index];
266 * goto *(prog->bpf_func + prologue_size);
269 static void emit_bpf_tail_call(u8 **pprog)
272 int label1, label2, label3;
275 /* rdi - pointer to ctx
276 * rsi - pointer to bpf_array
277 * rdx - index in bpf_array
280 /* if (index >= array->map.max_entries)
283 EMIT2(0x89, 0xD2); /* mov edx, edx */
284 EMIT3(0x39, 0x56, /* cmp dword ptr [rsi + 16], edx */
285 offsetof(struct bpf_array, map.max_entries));
286 #define OFFSET1 (41 + RETPOLINE_RAX_BPF_JIT_SIZE) /* number of bytes to jump */
287 EMIT2(X86_JBE, OFFSET1); /* jbe out */
290 /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
293 EMIT2_off32(0x8B, 0x85, -STACKSIZE + 36); /* mov eax, dword ptr [rbp - 516] */
294 EMIT3(0x83, 0xF8, MAX_TAIL_CALL_CNT); /* cmp eax, MAX_TAIL_CALL_CNT */
295 #define OFFSET2 (30 + RETPOLINE_RAX_BPF_JIT_SIZE)
296 EMIT2(X86_JA, OFFSET2); /* ja out */
298 EMIT3(0x83, 0xC0, 0x01); /* add eax, 1 */
299 EMIT2_off32(0x89, 0x85, -STACKSIZE + 36); /* mov dword ptr [rbp - 516], eax */
301 /* prog = array->ptrs[index]; */
302 EMIT4_off32(0x48, 0x8B, 0x84, 0xD6, /* mov rax, [rsi + rdx * 8 + offsetof(...)] */
303 offsetof(struct bpf_array, ptrs));
308 EMIT3(0x48, 0x85, 0xC0); /* test rax,rax */
309 #define OFFSET3 (8 + RETPOLINE_RAX_BPF_JIT_SIZE)
310 EMIT2(X86_JE, OFFSET3); /* je out */
313 /* goto *(prog->bpf_func + prologue_size); */
314 EMIT4(0x48, 0x8B, 0x40, /* mov rax, qword ptr [rax + 32] */
315 offsetof(struct bpf_prog, bpf_func));
316 EMIT4(0x48, 0x83, 0xC0, PROLOGUE_SIZE); /* add rax, prologue_size */
318 /* now we're ready to jump into next BPF program
319 * rdi == ctx (1st arg)
320 * rax == prog->bpf_func + prologue_size
322 RETPOLINE_RAX_BPF_JIT();
325 BUILD_BUG_ON(cnt - label1 != OFFSET1);
326 BUILD_BUG_ON(cnt - label2 != OFFSET2);
327 BUILD_BUG_ON(cnt - label3 != OFFSET3);
332 static void emit_load_skb_data_hlen(u8 **pprog)
337 /* r9d = skb->len - skb->data_len (headlen)
340 /* mov %r9d, off32(%rdi) */
341 EMIT3_off32(0x44, 0x8b, 0x8f, offsetof(struct sk_buff, len));
343 /* sub %r9d, off32(%rdi) */
344 EMIT3_off32(0x44, 0x2b, 0x8f, offsetof(struct sk_buff, data_len));
346 /* mov %r10, off32(%rdi) */
347 EMIT3_off32(0x4c, 0x8b, 0x97, offsetof(struct sk_buff, data));
351 static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
352 int oldproglen, struct jit_context *ctx)
354 struct bpf_insn *insn = bpf_prog->insnsi;
355 int insn_cnt = bpf_prog->len;
356 bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen == 0);
357 bool seen_exit = false;
358 u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY];
363 emit_prologue(&prog);
366 emit_load_skb_data_hlen(&prog);
368 for (i = 0; i < insn_cnt; i++, insn++) {
369 const s32 imm32 = insn->imm;
370 u32 dst_reg = insn->dst_reg;
371 u32 src_reg = insn->src_reg;
372 u8 b1 = 0, b2 = 0, b3 = 0;
375 bool reload_skb_data;
379 switch (insn->code) {
381 case BPF_ALU | BPF_ADD | BPF_X:
382 case BPF_ALU | BPF_SUB | BPF_X:
383 case BPF_ALU | BPF_AND | BPF_X:
384 case BPF_ALU | BPF_OR | BPF_X:
385 case BPF_ALU | BPF_XOR | BPF_X:
386 case BPF_ALU64 | BPF_ADD | BPF_X:
387 case BPF_ALU64 | BPF_SUB | BPF_X:
388 case BPF_ALU64 | BPF_AND | BPF_X:
389 case BPF_ALU64 | BPF_OR | BPF_X:
390 case BPF_ALU64 | BPF_XOR | BPF_X:
391 switch (BPF_OP(insn->code)) {
392 case BPF_ADD: b2 = 0x01; break;
393 case BPF_SUB: b2 = 0x29; break;
394 case BPF_AND: b2 = 0x21; break;
395 case BPF_OR: b2 = 0x09; break;
396 case BPF_XOR: b2 = 0x31; break;
398 if (BPF_CLASS(insn->code) == BPF_ALU64)
399 EMIT1(add_2mod(0x48, dst_reg, src_reg));
400 else if (is_ereg(dst_reg) || is_ereg(src_reg))
401 EMIT1(add_2mod(0x40, dst_reg, src_reg));
402 EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg));
406 case BPF_ALU64 | BPF_MOV | BPF_X:
407 EMIT_mov(dst_reg, src_reg);
411 case BPF_ALU | BPF_MOV | BPF_X:
412 if (is_ereg(dst_reg) || is_ereg(src_reg))
413 EMIT1(add_2mod(0x40, dst_reg, src_reg));
414 EMIT2(0x89, add_2reg(0xC0, dst_reg, src_reg));
418 case BPF_ALU | BPF_NEG:
419 case BPF_ALU64 | BPF_NEG:
420 if (BPF_CLASS(insn->code) == BPF_ALU64)
421 EMIT1(add_1mod(0x48, dst_reg));
422 else if (is_ereg(dst_reg))
423 EMIT1(add_1mod(0x40, dst_reg));
424 EMIT2(0xF7, add_1reg(0xD8, dst_reg));
427 case BPF_ALU | BPF_ADD | BPF_K:
428 case BPF_ALU | BPF_SUB | BPF_K:
429 case BPF_ALU | BPF_AND | BPF_K:
430 case BPF_ALU | BPF_OR | BPF_K:
431 case BPF_ALU | BPF_XOR | BPF_K:
432 case BPF_ALU64 | BPF_ADD | BPF_K:
433 case BPF_ALU64 | BPF_SUB | BPF_K:
434 case BPF_ALU64 | BPF_AND | BPF_K:
435 case BPF_ALU64 | BPF_OR | BPF_K:
436 case BPF_ALU64 | BPF_XOR | BPF_K:
437 if (BPF_CLASS(insn->code) == BPF_ALU64)
438 EMIT1(add_1mod(0x48, dst_reg));
439 else if (is_ereg(dst_reg))
440 EMIT1(add_1mod(0x40, dst_reg));
442 switch (BPF_OP(insn->code)) {
443 case BPF_ADD: b3 = 0xC0; break;
444 case BPF_SUB: b3 = 0xE8; break;
445 case BPF_AND: b3 = 0xE0; break;
446 case BPF_OR: b3 = 0xC8; break;
447 case BPF_XOR: b3 = 0xF0; break;
451 EMIT3(0x83, add_1reg(b3, dst_reg), imm32);
453 EMIT2_off32(0x81, add_1reg(b3, dst_reg), imm32);
456 case BPF_ALU64 | BPF_MOV | BPF_K:
457 /* optimization: if imm32 is positive,
458 * use 'mov eax, imm32' (which zero-extends imm32)
462 /* 'mov rax, imm32' sign extends imm32 */
463 b1 = add_1mod(0x48, dst_reg);
466 EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
470 case BPF_ALU | BPF_MOV | BPF_K:
471 /* mov %eax, imm32 */
472 if (is_ereg(dst_reg))
473 EMIT1(add_1mod(0x40, dst_reg));
474 EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
477 case BPF_LD | BPF_IMM | BPF_DW:
478 if (insn[1].code != 0 || insn[1].src_reg != 0 ||
479 insn[1].dst_reg != 0 || insn[1].off != 0) {
480 /* verifier must catch invalid insns */
481 pr_err("invalid BPF_LD_IMM64 insn\n");
485 /* movabsq %rax, imm64 */
486 EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
487 EMIT(insn[0].imm, 4);
488 EMIT(insn[1].imm, 4);
494 /* dst %= src, dst /= src, dst %= imm32, dst /= imm32 */
495 case BPF_ALU | BPF_MOD | BPF_X:
496 case BPF_ALU | BPF_DIV | BPF_X:
497 case BPF_ALU | BPF_MOD | BPF_K:
498 case BPF_ALU | BPF_DIV | BPF_K:
499 case BPF_ALU64 | BPF_MOD | BPF_X:
500 case BPF_ALU64 | BPF_DIV | BPF_X:
501 case BPF_ALU64 | BPF_MOD | BPF_K:
502 case BPF_ALU64 | BPF_DIV | BPF_K:
503 EMIT1(0x50); /* push rax */
504 EMIT1(0x52); /* push rdx */
506 if (BPF_SRC(insn->code) == BPF_X)
507 /* mov r11, src_reg */
508 EMIT_mov(AUX_REG, src_reg);
511 EMIT3_off32(0x49, 0xC7, 0xC3, imm32);
513 /* mov rax, dst_reg */
514 EMIT_mov(BPF_REG_0, dst_reg);
517 * equivalent to 'xor rdx, rdx', but one byte less
521 if (BPF_SRC(insn->code) == BPF_X) {
522 /* if (src_reg == 0) return 0 */
525 EMIT4(0x49, 0x83, 0xFB, 0x00);
527 /* jne .+9 (skip over pop, pop, xor and jmp) */
528 EMIT2(X86_JNE, 1 + 1 + 2 + 5);
529 EMIT1(0x5A); /* pop rdx */
530 EMIT1(0x58); /* pop rax */
531 EMIT2(0x31, 0xc0); /* xor eax, eax */
534 * addrs[i] - 11, because there are 11 bytes
535 * after this insn: div, mov, pop, pop, mov
537 jmp_offset = ctx->cleanup_addr - (addrs[i] - 11);
538 EMIT1_off32(0xE9, jmp_offset);
541 if (BPF_CLASS(insn->code) == BPF_ALU64)
543 EMIT3(0x49, 0xF7, 0xF3);
546 EMIT3(0x41, 0xF7, 0xF3);
548 if (BPF_OP(insn->code) == BPF_MOD)
550 EMIT3(0x49, 0x89, 0xD3);
553 EMIT3(0x49, 0x89, 0xC3);
555 EMIT1(0x5A); /* pop rdx */
556 EMIT1(0x58); /* pop rax */
558 /* mov dst_reg, r11 */
559 EMIT_mov(dst_reg, AUX_REG);
562 case BPF_ALU | BPF_MUL | BPF_K:
563 case BPF_ALU | BPF_MUL | BPF_X:
564 case BPF_ALU64 | BPF_MUL | BPF_K:
565 case BPF_ALU64 | BPF_MUL | BPF_X:
566 EMIT1(0x50); /* push rax */
567 EMIT1(0x52); /* push rdx */
569 /* mov r11, dst_reg */
570 EMIT_mov(AUX_REG, dst_reg);
572 if (BPF_SRC(insn->code) == BPF_X)
573 /* mov rax, src_reg */
574 EMIT_mov(BPF_REG_0, src_reg);
577 EMIT3_off32(0x48, 0xC7, 0xC0, imm32);
579 if (BPF_CLASS(insn->code) == BPF_ALU64)
580 EMIT1(add_1mod(0x48, AUX_REG));
581 else if (is_ereg(AUX_REG))
582 EMIT1(add_1mod(0x40, AUX_REG));
584 EMIT2(0xF7, add_1reg(0xE0, AUX_REG));
587 EMIT_mov(AUX_REG, BPF_REG_0);
589 EMIT1(0x5A); /* pop rdx */
590 EMIT1(0x58); /* pop rax */
592 /* mov dst_reg, r11 */
593 EMIT_mov(dst_reg, AUX_REG);
597 case BPF_ALU | BPF_LSH | BPF_K:
598 case BPF_ALU | BPF_RSH | BPF_K:
599 case BPF_ALU | BPF_ARSH | BPF_K:
600 case BPF_ALU64 | BPF_LSH | BPF_K:
601 case BPF_ALU64 | BPF_RSH | BPF_K:
602 case BPF_ALU64 | BPF_ARSH | BPF_K:
603 if (BPF_CLASS(insn->code) == BPF_ALU64)
604 EMIT1(add_1mod(0x48, dst_reg));
605 else if (is_ereg(dst_reg))
606 EMIT1(add_1mod(0x40, dst_reg));
608 switch (BPF_OP(insn->code)) {
609 case BPF_LSH: b3 = 0xE0; break;
610 case BPF_RSH: b3 = 0xE8; break;
611 case BPF_ARSH: b3 = 0xF8; break;
613 EMIT3(0xC1, add_1reg(b3, dst_reg), imm32);
616 case BPF_ALU | BPF_LSH | BPF_X:
617 case BPF_ALU | BPF_RSH | BPF_X:
618 case BPF_ALU | BPF_ARSH | BPF_X:
619 case BPF_ALU64 | BPF_LSH | BPF_X:
620 case BPF_ALU64 | BPF_RSH | BPF_X:
621 case BPF_ALU64 | BPF_ARSH | BPF_X:
623 /* check for bad case when dst_reg == rcx */
624 if (dst_reg == BPF_REG_4) {
625 /* mov r11, dst_reg */
626 EMIT_mov(AUX_REG, dst_reg);
630 if (src_reg != BPF_REG_4) { /* common case */
631 EMIT1(0x51); /* push rcx */
633 /* mov rcx, src_reg */
634 EMIT_mov(BPF_REG_4, src_reg);
637 /* shl %rax, %cl | shr %rax, %cl | sar %rax, %cl */
638 if (BPF_CLASS(insn->code) == BPF_ALU64)
639 EMIT1(add_1mod(0x48, dst_reg));
640 else if (is_ereg(dst_reg))
641 EMIT1(add_1mod(0x40, dst_reg));
643 switch (BPF_OP(insn->code)) {
644 case BPF_LSH: b3 = 0xE0; break;
645 case BPF_RSH: b3 = 0xE8; break;
646 case BPF_ARSH: b3 = 0xF8; break;
648 EMIT2(0xD3, add_1reg(b3, dst_reg));
650 if (src_reg != BPF_REG_4)
651 EMIT1(0x59); /* pop rcx */
653 if (insn->dst_reg == BPF_REG_4)
654 /* mov dst_reg, r11 */
655 EMIT_mov(insn->dst_reg, AUX_REG);
658 case BPF_ALU | BPF_END | BPF_FROM_BE:
661 /* emit 'ror %ax, 8' to swap lower 2 bytes */
663 if (is_ereg(dst_reg))
665 EMIT3(0xC1, add_1reg(0xC8, dst_reg), 8);
667 /* emit 'movzwl eax, ax' */
668 if (is_ereg(dst_reg))
669 EMIT3(0x45, 0x0F, 0xB7);
672 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
675 /* emit 'bswap eax' to swap lower 4 bytes */
676 if (is_ereg(dst_reg))
680 EMIT1(add_1reg(0xC8, dst_reg));
683 /* emit 'bswap rax' to swap 8 bytes */
684 EMIT3(add_1mod(0x48, dst_reg), 0x0F,
685 add_1reg(0xC8, dst_reg));
690 case BPF_ALU | BPF_END | BPF_FROM_LE:
693 /* emit 'movzwl eax, ax' to zero extend 16-bit
696 if (is_ereg(dst_reg))
697 EMIT3(0x45, 0x0F, 0xB7);
700 EMIT1(add_2reg(0xC0, dst_reg, dst_reg));
703 /* emit 'mov eax, eax' to clear upper 32-bits */
704 if (is_ereg(dst_reg))
706 EMIT2(0x89, add_2reg(0xC0, dst_reg, dst_reg));
714 /* ST: *(u8*)(dst_reg + off) = imm */
715 case BPF_ST | BPF_MEM | BPF_B:
716 if (is_ereg(dst_reg))
721 case BPF_ST | BPF_MEM | BPF_H:
722 if (is_ereg(dst_reg))
723 EMIT3(0x66, 0x41, 0xC7);
727 case BPF_ST | BPF_MEM | BPF_W:
728 if (is_ereg(dst_reg))
733 case BPF_ST | BPF_MEM | BPF_DW:
734 EMIT2(add_1mod(0x48, dst_reg), 0xC7);
736 st: if (is_imm8(insn->off))
737 EMIT2(add_1reg(0x40, dst_reg), insn->off);
739 EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);
741 EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
744 /* STX: *(u8*)(dst_reg + off) = src_reg */
745 case BPF_STX | BPF_MEM | BPF_B:
746 /* emit 'mov byte ptr [rax + off], al' */
747 if (is_ereg(dst_reg) || is_ereg_8l(src_reg))
748 /* Add extra byte for eregs or SIL,DIL,BPL in src_reg */
749 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x88);
753 case BPF_STX | BPF_MEM | BPF_H:
754 if (is_ereg(dst_reg) || is_ereg(src_reg))
755 EMIT3(0x66, add_2mod(0x40, dst_reg, src_reg), 0x89);
759 case BPF_STX | BPF_MEM | BPF_W:
760 if (is_ereg(dst_reg) || is_ereg(src_reg))
761 EMIT2(add_2mod(0x40, dst_reg, src_reg), 0x89);
765 case BPF_STX | BPF_MEM | BPF_DW:
766 EMIT2(add_2mod(0x48, dst_reg, src_reg), 0x89);
767 stx: if (is_imm8(insn->off))
768 EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
770 EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
774 /* LDX: dst_reg = *(u8*)(src_reg + off) */
775 case BPF_LDX | BPF_MEM | BPF_B:
776 /* emit 'movzx rax, byte ptr [rax + off]' */
777 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB6);
779 case BPF_LDX | BPF_MEM | BPF_H:
780 /* emit 'movzx rax, word ptr [rax + off]' */
781 EMIT3(add_2mod(0x48, src_reg, dst_reg), 0x0F, 0xB7);
783 case BPF_LDX | BPF_MEM | BPF_W:
784 /* emit 'mov eax, dword ptr [rax+0x14]' */
785 if (is_ereg(dst_reg) || is_ereg(src_reg))
786 EMIT2(add_2mod(0x40, src_reg, dst_reg), 0x8B);
790 case BPF_LDX | BPF_MEM | BPF_DW:
791 /* emit 'mov rax, qword ptr [rax+0x14]' */
792 EMIT2(add_2mod(0x48, src_reg, dst_reg), 0x8B);
793 ldx: /* if insn->off == 0 we can save one extra byte, but
794 * special case of x86 r13 which always needs an offset
795 * is not worth the hassle
797 if (is_imm8(insn->off))
798 EMIT2(add_2reg(0x40, src_reg, dst_reg), insn->off);
800 EMIT1_off32(add_2reg(0x80, src_reg, dst_reg),
804 /* STX XADD: lock *(u32*)(dst_reg + off) += src_reg */
805 case BPF_STX | BPF_XADD | BPF_W:
806 /* emit 'lock add dword ptr [rax + off], eax' */
807 if (is_ereg(dst_reg) || is_ereg(src_reg))
808 EMIT3(0xF0, add_2mod(0x40, dst_reg, src_reg), 0x01);
812 case BPF_STX | BPF_XADD | BPF_DW:
813 EMIT3(0xF0, add_2mod(0x48, dst_reg, src_reg), 0x01);
814 xadd: if (is_imm8(insn->off))
815 EMIT2(add_2reg(0x40, dst_reg, src_reg), insn->off);
817 EMIT1_off32(add_2reg(0x80, dst_reg, src_reg),
822 case BPF_JMP | BPF_CALL:
823 func = (u8 *) __bpf_call_base + imm32;
824 jmp_offset = func - (image + addrs[i]);
826 reload_skb_data = bpf_helper_changes_skb_data(func);
827 if (reload_skb_data) {
828 EMIT1(0x57); /* push %rdi */
829 jmp_offset += 22; /* pop, mov, sub, mov */
831 EMIT2(0x41, 0x52); /* push %r10 */
832 EMIT2(0x41, 0x51); /* push %r9 */
833 /* need to adjust jmp offset, since
834 * pop %r9, pop %r10 take 4 bytes after call insn
839 if (!imm32 || !is_simm32(jmp_offset)) {
840 pr_err("unsupported bpf func %d addr %p image %p\n",
844 EMIT1_off32(0xE8, jmp_offset);
846 if (reload_skb_data) {
847 EMIT1(0x5F); /* pop %rdi */
848 emit_load_skb_data_hlen(&prog);
850 EMIT2(0x41, 0x59); /* pop %r9 */
851 EMIT2(0x41, 0x5A); /* pop %r10 */
856 case BPF_JMP | BPF_CALL | BPF_X:
857 emit_bpf_tail_call(&prog);
861 case BPF_JMP | BPF_JEQ | BPF_X:
862 case BPF_JMP | BPF_JNE | BPF_X:
863 case BPF_JMP | BPF_JGT | BPF_X:
864 case BPF_JMP | BPF_JGE | BPF_X:
865 case BPF_JMP | BPF_JSGT | BPF_X:
866 case BPF_JMP | BPF_JSGE | BPF_X:
867 /* cmp dst_reg, src_reg */
868 EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x39,
869 add_2reg(0xC0, dst_reg, src_reg));
872 case BPF_JMP | BPF_JSET | BPF_X:
873 /* test dst_reg, src_reg */
874 EMIT3(add_2mod(0x48, dst_reg, src_reg), 0x85,
875 add_2reg(0xC0, dst_reg, src_reg));
878 case BPF_JMP | BPF_JSET | BPF_K:
879 /* test dst_reg, imm32 */
880 EMIT1(add_1mod(0x48, dst_reg));
881 EMIT2_off32(0xF7, add_1reg(0xC0, dst_reg), imm32);
884 case BPF_JMP | BPF_JEQ | BPF_K:
885 case BPF_JMP | BPF_JNE | BPF_K:
886 case BPF_JMP | BPF_JGT | BPF_K:
887 case BPF_JMP | BPF_JGE | BPF_K:
888 case BPF_JMP | BPF_JSGT | BPF_K:
889 case BPF_JMP | BPF_JSGE | BPF_K:
890 /* cmp dst_reg, imm8/32 */
891 EMIT1(add_1mod(0x48, dst_reg));
894 EMIT3(0x83, add_1reg(0xF8, dst_reg), imm32);
896 EMIT2_off32(0x81, add_1reg(0xF8, dst_reg), imm32);
898 emit_cond_jmp: /* convert BPF opcode to x86 */
899 switch (BPF_OP(insn->code)) {
908 /* GT is unsigned '>', JA in x86 */
912 /* GE is unsigned '>=', JAE in x86 */
916 /* signed '>', GT in x86 */
920 /* signed '>=', GE in x86 */
923 default: /* to silence gcc warning */
926 jmp_offset = addrs[i + insn->off] - addrs[i];
927 if (is_imm8(jmp_offset)) {
928 EMIT2(jmp_cond, jmp_offset);
929 } else if (is_simm32(jmp_offset)) {
930 EMIT2_off32(0x0F, jmp_cond + 0x10, jmp_offset);
932 pr_err("cond_jmp gen bug %llx\n", jmp_offset);
938 case BPF_JMP | BPF_JA:
939 jmp_offset = addrs[i + insn->off] - addrs[i];
941 /* optimize out nop jumps */
944 if (is_imm8(jmp_offset)) {
945 EMIT2(0xEB, jmp_offset);
946 } else if (is_simm32(jmp_offset)) {
947 EMIT1_off32(0xE9, jmp_offset);
949 pr_err("jmp gen bug %llx\n", jmp_offset);
954 case BPF_LD | BPF_IND | BPF_W:
957 case BPF_LD | BPF_ABS | BPF_W:
958 func = CHOOSE_LOAD_FUNC(imm32, sk_load_word);
960 ctx->seen_ld_abs = seen_ld_abs = true;
961 jmp_offset = func - (image + addrs[i]);
962 if (!func || !is_simm32(jmp_offset)) {
963 pr_err("unsupported bpf func %d addr %p image %p\n",
967 if (BPF_MODE(insn->code) == BPF_ABS) {
968 /* mov %esi, imm32 */
969 EMIT1_off32(0xBE, imm32);
971 /* mov %rsi, src_reg */
972 EMIT_mov(BPF_REG_2, src_reg);
976 EMIT3(0x83, 0xC6, imm32);
978 /* add %esi, imm32 */
979 EMIT2_off32(0x81, 0xC6, imm32);
982 /* skb pointer is in R6 (%rbx), it will be copied into
983 * %rdi if skb_copy_bits() call is necessary.
984 * sk_load_* helpers also use %r10 and %r9d.
987 EMIT1_off32(0xE8, jmp_offset); /* call */
990 case BPF_LD | BPF_IND | BPF_H:
993 case BPF_LD | BPF_ABS | BPF_H:
994 func = CHOOSE_LOAD_FUNC(imm32, sk_load_half);
996 case BPF_LD | BPF_IND | BPF_B:
999 case BPF_LD | BPF_ABS | BPF_B:
1000 func = CHOOSE_LOAD_FUNC(imm32, sk_load_byte);
1003 case BPF_JMP | BPF_EXIT:
1005 jmp_offset = ctx->cleanup_addr - addrs[i];
1009 /* update cleanup_addr */
1010 ctx->cleanup_addr = proglen;
1011 /* mov rbx, qword ptr [rbp-X] */
1012 EMIT3_off32(0x48, 0x8B, 0x9D, -STACKSIZE);
1013 /* mov r13, qword ptr [rbp-X] */
1014 EMIT3_off32(0x4C, 0x8B, 0xAD, -STACKSIZE + 8);
1015 /* mov r14, qword ptr [rbp-X] */
1016 EMIT3_off32(0x4C, 0x8B, 0xB5, -STACKSIZE + 16);
1017 /* mov r15, qword ptr [rbp-X] */
1018 EMIT3_off32(0x4C, 0x8B, 0xBD, -STACKSIZE + 24);
1020 EMIT1(0xC9); /* leave */
1021 EMIT1(0xC3); /* ret */
1025 /* By design x64 JIT should support all BPF instructions
1026 * This error will be seen if new instruction was added
1027 * to interpreter, but not to JIT
1028 * or if there is junk in bpf_prog
1030 pr_err("bpf_jit: unknown opcode %02x\n", insn->code);
1035 if (ilen > BPF_MAX_INSN_SIZE) {
1036 pr_err("bpf_jit_compile fatal insn size error\n");
1042 * When populating the image, assert that:
1044 * i) We do not write beyond the allocated space, and
1045 * ii) addrs[i] did not change from the prior run, in order
1046 * to validate assumptions made for computing branch
1049 if (unlikely(proglen + ilen > oldproglen ||
1050 proglen + ilen != addrs[i])) {
1051 pr_err("bpf_jit_compile fatal error\n");
1054 memcpy(image + proglen, temp, ilen);
1063 void bpf_jit_compile(struct bpf_prog *prog)
1067 void bpf_int_jit_compile(struct bpf_prog *prog)
1069 struct bpf_binary_header *header = NULL;
1070 int proglen, oldproglen = 0;
1071 struct jit_context ctx = {};
1077 if (!bpf_jit_enable)
1080 if (!prog || !prog->len)
1083 addrs = kmalloc(prog->len * sizeof(*addrs), GFP_KERNEL);
1087 /* Before first pass, make a rough estimation of addrs[]
1088 * each bpf instruction is translated to less than 64 bytes
1090 for (proglen = 0, i = 0; i < prog->len; i++) {
1094 ctx.cleanup_addr = proglen;
1096 /* JITed image shrinks with every pass and the loop iterates
1097 * until the image stops shrinking. Very large bpf programs
1098 * may converge on the last pass. In such case do one more
1099 * pass to emit the final image
1101 for (pass = 0; pass < 20 || image; pass++) {
1102 proglen = do_jit(prog, addrs, image, oldproglen, &ctx);
1106 bpf_jit_binary_free(header);
1110 if (proglen != oldproglen) {
1111 pr_err("bpf_jit: proglen=%d != oldproglen=%d\n",
1112 proglen, oldproglen);
1117 if (proglen == oldproglen) {
1118 header = bpf_jit_binary_alloc(proglen, &image,
1123 oldproglen = proglen;
1127 if (bpf_jit_enable > 1)
1128 bpf_jit_dump(prog->len, proglen, pass + 1, image);
1131 bpf_flush_icache(header, image + proglen);
1132 set_memory_ro((unsigned long)header, header->pages);
1133 prog->bpf_func = (void *)image;
1140 void bpf_jit_free(struct bpf_prog *fp)
1142 unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
1143 struct bpf_binary_header *header = (void *)addr;
1148 set_memory_rw(addr, header->pages);
1149 bpf_jit_binary_free(header);
1152 bpf_prog_unlock_free(fp);