1 /* bpf_jit_comp.c : BPF JIT compiler
3 * Copyright (C) 2011-2013 Eric Dumazet (eric.dumazet@gmail.com)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2
10 #include <linux/moduleloader.h>
11 #include <asm/cacheflush.h>
12 #include <linux/netdevice.h>
13 #include <linux/filter.h>
14 #include <linux/if_vlan.h>
15 #include <linux/random.h>
19 * EAX : BPF A accumulator
20 * EBX : BPF X accumulator
21 * RDI : pointer to skb (first argument given to JIT function)
22 * RBP : frame pointer (even if CONFIG_FRAME_POINTER=n)
23 * ECX,EDX,ESI : scratch registers
24 * r9d : skb->len - skb->data_len (headlen)
26 * -8(RBP) : saved RBX value
27 * -16(RBP)..-80(RBP) : BPF_MEMWORDS values
29 int bpf_jit_enable __read_mostly
;
32 * assembly code in arch/x86/net/bpf_jit.S
34 extern u8 sk_load_word
[], sk_load_half
[], sk_load_byte
[], sk_load_byte_msh
[];
35 extern u8 sk_load_word_positive_offset
[], sk_load_half_positive_offset
[];
36 extern u8 sk_load_byte_positive_offset
[], sk_load_byte_msh_positive_offset
[];
37 extern u8 sk_load_word_negative_offset
[], sk_load_half_negative_offset
[];
38 extern u8 sk_load_byte_negative_offset
[], sk_load_byte_msh_negative_offset
[];
40 static inline u8
*emit_code(u8
*ptr
, u32 bytes
, unsigned int len
)
53 #define EMIT(bytes, len) do { prog = emit_code(prog, bytes, len); } while (0)
55 #define EMIT1(b1) EMIT(b1, 1)
56 #define EMIT2(b1, b2) EMIT((b1) + ((b2) << 8), 2)
57 #define EMIT3(b1, b2, b3) EMIT((b1) + ((b2) << 8) + ((b3) << 16), 3)
58 #define EMIT4(b1, b2, b3, b4) EMIT((b1) + ((b2) << 8) + ((b3) << 16) + ((b4) << 24), 4)
59 #define EMIT1_off32(b1, off) do { EMIT1(b1); EMIT(off, 4);} while (0)
61 #define CLEAR_A() EMIT2(0x31, 0xc0) /* xor %eax,%eax */
62 #define CLEAR_X() EMIT2(0x31, 0xdb) /* xor %ebx,%ebx */
64 static inline bool is_imm8(int value
)
66 return value
<= 127 && value
>= -128;
69 static inline bool is_near(int offset
)
71 return offset
<= 127 && offset
>= -128;
74 #define EMIT_JMP(offset) \
77 if (is_near(offset)) \
78 EMIT2(0xeb, offset); /* jmp .+off8 */ \
80 EMIT1_off32(0xe9, offset); /* jmp .+off32 */ \
84 /* list of x86 cond jumps opcodes (. + s8)
85 * Add 0x10 (and an extra 0x0f) to generate far jumps (. + s32)
94 #define EMIT_COND_JMP(op, offset) \
96 if (is_near(offset)) \
97 EMIT2(op, offset); /* jxx .+off8 */ \
99 EMIT2(0x0f, op + 0x10); \
100 EMIT(offset, 4); /* jxx .+off32 */ \
104 #define COND_SEL(CODE, TOP, FOP) \
111 #define SEEN_DATAREF 1 /* might call external helpers */
112 #define SEEN_XREG 2 /* ebx is used */
113 #define SEEN_MEM 4 /* use mem[] for temporary storage */
115 static inline void bpf_flush_icache(void *start
, void *end
)
117 mm_segment_t old_fs
= get_fs();
121 flush_icache_range((unsigned long)start
, (unsigned long)end
);
125 #define CHOOSE_LOAD_FUNC(K, func) \
126 ((int)K < 0 ? ((int)K >= SKF_LL_OFF ? func##_negative_offset : func) : func##_positive_offset)
128 /* Helper to find the offset of pkt_type in sk_buff
129 * We want to make sure its still a 3bit field starting at a byte boundary.
131 #define PKT_TYPE_MAX 7
132 static int pkt_type_offset(void)
134 struct sk_buff skb_probe
= {
137 char *ct
= (char *)&skb_probe
;
140 for (off
= 0; off
< sizeof(struct sk_buff
); off
++) {
141 if (ct
[off
] == PKT_TYPE_MAX
)
144 pr_err_once("Please fix pkt_type_offset(), as pkt_type couldn't be found\n");
148 struct bpf_binary_header
{
150 /* Note : for security reasons, bpf code will follow a randomly
151 * sized amount of int3 instructions
156 static struct bpf_binary_header
*bpf_alloc_binary(unsigned int proglen
,
159 unsigned int sz
, hole
;
160 struct bpf_binary_header
*header
;
162 /* Most of BPF filters are really small,
163 * but if some of them fill a page, allow at least
164 * 128 extra bytes to insert a random section of int3
166 sz
= round_up(proglen
+ sizeof(*header
) + 128, PAGE_SIZE
);
167 header
= module_alloc(sz
);
171 memset(header
, 0xcc, sz
); /* fill whole space with int3 instructions */
173 header
->pages
= sz
/ PAGE_SIZE
;
174 hole
= sz
- (proglen
+ sizeof(*header
));
176 /* insert a random number of int3 instructions before BPF code */
177 *image_ptr
= &header
->image
[prandom_u32() % hole
];
181 void bpf_jit_compile(struct sk_filter
*fp
)
185 unsigned int proglen
, oldproglen
= 0;
187 int t_offset
, f_offset
;
188 u8 t_op
, f_op
, seen
= 0, pass
;
190 struct bpf_binary_header
*header
= NULL
;
192 int pc_ret0
= -1; /* bpf index of first RET #0 instruction (if any) */
193 unsigned int cleanup_addr
; /* epilogue code offset */
195 const struct sock_filter
*filter
= fp
->insns
;
201 addrs
= kmalloc(flen
* sizeof(*addrs
), GFP_KERNEL
);
205 /* Before first pass, make a rough estimation of addrs[]
206 * each bpf instruction is translated to less than 64 bytes
208 for (proglen
= 0, i
= 0; i
< flen
; i
++) {
212 cleanup_addr
= proglen
; /* epilogue address */
214 for (pass
= 0; pass
< 10; pass
++) {
215 u8 seen_or_pass0
= (pass
== 0) ? (SEEN_XREG
| SEEN_DATAREF
| SEEN_MEM
) : seen
;
216 /* no prologue/epilogue for trivial filters (RET something) */
221 EMIT4(0x55, 0x48, 0x89, 0xe5); /* push %rbp; mov %rsp,%rbp */
222 EMIT4(0x48, 0x83, 0xec, 96); /* subq $96,%rsp */
223 /* note : must save %rbx in case bpf_error is hit */
224 if (seen_or_pass0
& (SEEN_XREG
| SEEN_DATAREF
))
225 EMIT4(0x48, 0x89, 0x5d, 0xf8); /* mov %rbx, -8(%rbp) */
226 if (seen_or_pass0
& SEEN_XREG
)
227 CLEAR_X(); /* make sure we dont leek kernel memory */
230 * If this filter needs to access skb data,
231 * loads r9 and r8 with :
232 * r9 = skb->len - skb->data_len
235 if (seen_or_pass0
& SEEN_DATAREF
) {
236 if (offsetof(struct sk_buff
, len
) <= 127)
237 /* mov off8(%rdi),%r9d */
238 EMIT4(0x44, 0x8b, 0x4f, offsetof(struct sk_buff
, len
));
240 /* mov off32(%rdi),%r9d */
241 EMIT3(0x44, 0x8b, 0x8f);
242 EMIT(offsetof(struct sk_buff
, len
), 4);
244 if (is_imm8(offsetof(struct sk_buff
, data_len
)))
245 /* sub off8(%rdi),%r9d */
246 EMIT4(0x44, 0x2b, 0x4f, offsetof(struct sk_buff
, data_len
));
248 EMIT3(0x44, 0x2b, 0x8f);
249 EMIT(offsetof(struct sk_buff
, data_len
), 4);
252 if (is_imm8(offsetof(struct sk_buff
, data
)))
253 /* mov off8(%rdi),%r8 */
254 EMIT4(0x4c, 0x8b, 0x47, offsetof(struct sk_buff
, data
));
256 /* mov off32(%rdi),%r8 */
257 EMIT3(0x4c, 0x8b, 0x87);
258 EMIT(offsetof(struct sk_buff
, data
), 4);
263 switch (filter
[0].code
) {
266 case BPF_S_ANC_PROTOCOL
:
267 case BPF_S_ANC_IFINDEX
:
269 case BPF_S_ANC_RXHASH
:
271 case BPF_S_ANC_VLAN_TAG
:
272 case BPF_S_ANC_VLAN_TAG_PRESENT
:
273 case BPF_S_ANC_QUEUE
:
274 case BPF_S_ANC_PKTTYPE
:
278 /* first instruction sets A register (or is RET 'constant') */
281 /* make sure we dont leak kernel information to user */
282 CLEAR_A(); /* A = 0 */
285 for (i
= 0; i
< flen
; i
++) {
286 unsigned int K
= filter
[i
].k
;
288 switch (filter
[i
].code
) {
289 case BPF_S_ALU_ADD_X
: /* A += X; */
291 EMIT2(0x01, 0xd8); /* add %ebx,%eax */
293 case BPF_S_ALU_ADD_K
: /* A += K; */
297 EMIT3(0x83, 0xc0, K
); /* add imm8,%eax */
299 EMIT1_off32(0x05, K
); /* add imm32,%eax */
301 case BPF_S_ALU_SUB_X
: /* A -= X; */
303 EMIT2(0x29, 0xd8); /* sub %ebx,%eax */
305 case BPF_S_ALU_SUB_K
: /* A -= K */
309 EMIT3(0x83, 0xe8, K
); /* sub imm8,%eax */
311 EMIT1_off32(0x2d, K
); /* sub imm32,%eax */
313 case BPF_S_ALU_MUL_X
: /* A *= X; */
315 EMIT3(0x0f, 0xaf, 0xc3); /* imul %ebx,%eax */
317 case BPF_S_ALU_MUL_K
: /* A *= K */
319 EMIT3(0x6b, 0xc0, K
); /* imul imm8,%eax,%eax */
321 EMIT2(0x69, 0xc0); /* imul imm32,%eax */
325 case BPF_S_ALU_DIV_X
: /* A /= X; */
327 EMIT2(0x85, 0xdb); /* test %ebx,%ebx */
329 /* addrs[pc_ret0 - 1] is start address of target
330 * (addrs[i] - 4) is the address following this jmp
331 * ("xor %edx,%edx; div %ebx" being 4 bytes long)
333 EMIT_COND_JMP(X86_JE
, addrs
[pc_ret0
- 1] -
336 EMIT_COND_JMP(X86_JNE
, 2 + 5);
338 EMIT1_off32(0xe9, cleanup_addr
- (addrs
[i
] - 4)); /* jmp .+off32 */
340 EMIT4(0x31, 0xd2, 0xf7, 0xf3); /* xor %edx,%edx; div %ebx */
342 case BPF_S_ALU_MOD_X
: /* A %= X; */
344 EMIT2(0x85, 0xdb); /* test %ebx,%ebx */
346 /* addrs[pc_ret0 - 1] is start address of target
347 * (addrs[i] - 6) is the address following this jmp
348 * ("xor %edx,%edx; div %ebx;mov %edx,%eax" being 6 bytes long)
350 EMIT_COND_JMP(X86_JE
, addrs
[pc_ret0
- 1] -
353 EMIT_COND_JMP(X86_JNE
, 2 + 5);
355 EMIT1_off32(0xe9, cleanup_addr
- (addrs
[i
] - 6)); /* jmp .+off32 */
357 EMIT2(0x31, 0xd2); /* xor %edx,%edx */
358 EMIT2(0xf7, 0xf3); /* div %ebx */
359 EMIT2(0x89, 0xd0); /* mov %edx,%eax */
361 case BPF_S_ALU_MOD_K
: /* A %= K; */
366 EMIT2(0x31, 0xd2); /* xor %edx,%edx */
367 EMIT1(0xb9);EMIT(K
, 4); /* mov imm32,%ecx */
368 EMIT2(0xf7, 0xf1); /* div %ecx */
369 EMIT2(0x89, 0xd0); /* mov %edx,%eax */
371 case BPF_S_ALU_DIV_K
: /* A /= K */
374 EMIT2(0x31, 0xd2); /* xor %edx,%edx */
375 EMIT1(0xb9);EMIT(K
, 4); /* mov imm32,%ecx */
376 EMIT2(0xf7, 0xf1); /* div %ecx */
378 case BPF_S_ALU_AND_X
:
380 EMIT2(0x21, 0xd8); /* and %ebx,%eax */
382 case BPF_S_ALU_AND_K
:
383 if (K
>= 0xFFFFFF00) {
384 EMIT2(0x24, K
& 0xFF); /* and imm8,%al */
385 } else if (K
>= 0xFFFF0000) {
386 EMIT2(0x66, 0x25); /* and imm16,%ax */
389 EMIT1_off32(0x25, K
); /* and imm32,%eax */
394 EMIT2(0x09, 0xd8); /* or %ebx,%eax */
398 EMIT3(0x83, 0xc8, K
); /* or imm8,%eax */
400 EMIT1_off32(0x0d, K
); /* or imm32,%eax */
402 case BPF_S_ANC_ALU_XOR_X
: /* A ^= X; */
403 case BPF_S_ALU_XOR_X
:
405 EMIT2(0x31, 0xd8); /* xor %ebx,%eax */
407 case BPF_S_ALU_XOR_K
: /* A ^= K; */
411 EMIT3(0x83, 0xf0, K
); /* xor imm8,%eax */
413 EMIT1_off32(0x35, K
); /* xor imm32,%eax */
415 case BPF_S_ALU_LSH_X
: /* A <<= X; */
417 EMIT4(0x89, 0xd9, 0xd3, 0xe0); /* mov %ebx,%ecx; shl %cl,%eax */
419 case BPF_S_ALU_LSH_K
:
423 EMIT2(0xd1, 0xe0); /* shl %eax */
425 EMIT3(0xc1, 0xe0, K
);
427 case BPF_S_ALU_RSH_X
: /* A >>= X; */
429 EMIT4(0x89, 0xd9, 0xd3, 0xe8); /* mov %ebx,%ecx; shr %cl,%eax */
431 case BPF_S_ALU_RSH_K
: /* A >>= K; */
435 EMIT2(0xd1, 0xe8); /* shr %eax */
437 EMIT3(0xc1, 0xe8, K
);
440 EMIT2(0xf7, 0xd8); /* neg %eax */
448 EMIT1_off32(0xb8, K
); /* mov $imm32,%eax */
454 EMIT_JMP(cleanup_addr
- addrs
[i
]);
457 if (seen_or_pass0
& SEEN_XREG
)
458 EMIT4(0x48, 0x8b, 0x5d, 0xf8); /* mov -8(%rbp),%rbx */
459 EMIT1(0xc9); /* leaveq */
461 EMIT1(0xc3); /* ret */
463 case BPF_S_MISC_TAX
: /* X = A */
465 EMIT2(0x89, 0xc3); /* mov %eax,%ebx */
467 case BPF_S_MISC_TXA
: /* A = X */
469 EMIT2(0x89, 0xd8); /* mov %ebx,%eax */
471 case BPF_S_LD_IMM
: /* A = K */
475 EMIT1_off32(0xb8, K
); /* mov $imm32,%eax */
477 case BPF_S_LDX_IMM
: /* X = K */
482 EMIT1_off32(0xbb, K
); /* mov $imm32,%ebx */
484 case BPF_S_LD_MEM
: /* A = mem[K] : mov off8(%rbp),%eax */
486 EMIT3(0x8b, 0x45, 0xf0 - K
*4);
488 case BPF_S_LDX_MEM
: /* X = mem[K] : mov off8(%rbp),%ebx */
489 seen
|= SEEN_XREG
| SEEN_MEM
;
490 EMIT3(0x8b, 0x5d, 0xf0 - K
*4);
492 case BPF_S_ST
: /* mem[K] = A : mov %eax,off8(%rbp) */
494 EMIT3(0x89, 0x45, 0xf0 - K
*4);
496 case BPF_S_STX
: /* mem[K] = X : mov %ebx,off8(%rbp) */
497 seen
|= SEEN_XREG
| SEEN_MEM
;
498 EMIT3(0x89, 0x5d, 0xf0 - K
*4);
500 case BPF_S_LD_W_LEN
: /* A = skb->len; */
501 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, len
) != 4);
502 if (is_imm8(offsetof(struct sk_buff
, len
)))
503 /* mov off8(%rdi),%eax */
504 EMIT3(0x8b, 0x47, offsetof(struct sk_buff
, len
));
507 EMIT(offsetof(struct sk_buff
, len
), 4);
510 case BPF_S_LDX_W_LEN
: /* X = skb->len; */
512 if (is_imm8(offsetof(struct sk_buff
, len
)))
513 /* mov off8(%rdi),%ebx */
514 EMIT3(0x8b, 0x5f, offsetof(struct sk_buff
, len
));
517 EMIT(offsetof(struct sk_buff
, len
), 4);
520 case BPF_S_ANC_PROTOCOL
: /* A = ntohs(skb->protocol); */
521 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
522 if (is_imm8(offsetof(struct sk_buff
, protocol
))) {
523 /* movzwl off8(%rdi),%eax */
524 EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff
, protocol
));
526 EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
527 EMIT(offsetof(struct sk_buff
, protocol
), 4);
529 EMIT2(0x86, 0xc4); /* ntohs() : xchg %al,%ah */
531 case BPF_S_ANC_IFINDEX
:
532 if (is_imm8(offsetof(struct sk_buff
, dev
))) {
533 /* movq off8(%rdi),%rax */
534 EMIT4(0x48, 0x8b, 0x47, offsetof(struct sk_buff
, dev
));
536 EMIT3(0x48, 0x8b, 0x87); /* movq off32(%rdi),%rax */
537 EMIT(offsetof(struct sk_buff
, dev
), 4);
539 EMIT3(0x48, 0x85, 0xc0); /* test %rax,%rax */
540 EMIT_COND_JMP(X86_JE
, cleanup_addr
- (addrs
[i
] - 6));
541 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, ifindex
) != 4);
542 EMIT2(0x8b, 0x80); /* mov off32(%rax),%eax */
543 EMIT(offsetof(struct net_device
, ifindex
), 4);
546 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, mark
) != 4);
547 if (is_imm8(offsetof(struct sk_buff
, mark
))) {
548 /* mov off8(%rdi),%eax */
549 EMIT3(0x8b, 0x47, offsetof(struct sk_buff
, mark
));
552 EMIT(offsetof(struct sk_buff
, mark
), 4);
555 case BPF_S_ANC_RXHASH
:
556 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, rxhash
) != 4);
557 if (is_imm8(offsetof(struct sk_buff
, rxhash
))) {
558 /* mov off8(%rdi),%eax */
559 EMIT3(0x8b, 0x47, offsetof(struct sk_buff
, rxhash
));
562 EMIT(offsetof(struct sk_buff
, rxhash
), 4);
565 case BPF_S_ANC_QUEUE
:
566 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
567 if (is_imm8(offsetof(struct sk_buff
, queue_mapping
))) {
568 /* movzwl off8(%rdi),%eax */
569 EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff
, queue_mapping
));
571 EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
572 EMIT(offsetof(struct sk_buff
, queue_mapping
), 4);
577 EMIT4(0x65, 0x8b, 0x04, 0x25); /* mov %gs:off32,%eax */
578 EMIT((u32
)(unsigned long)&cpu_number
, 4); /* A = smp_processor_id(); */
583 case BPF_S_ANC_VLAN_TAG
:
584 case BPF_S_ANC_VLAN_TAG_PRESENT
:
585 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_tci
) != 2);
586 if (is_imm8(offsetof(struct sk_buff
, vlan_tci
))) {
587 /* movzwl off8(%rdi),%eax */
588 EMIT4(0x0f, 0xb7, 0x47, offsetof(struct sk_buff
, vlan_tci
));
590 EMIT3(0x0f, 0xb7, 0x87); /* movzwl off32(%rdi),%eax */
591 EMIT(offsetof(struct sk_buff
, vlan_tci
), 4);
593 BUILD_BUG_ON(VLAN_TAG_PRESENT
!= 0x1000);
594 if (filter
[i
].code
== BPF_S_ANC_VLAN_TAG
) {
595 EMIT3(0x80, 0xe4, 0xef); /* and $0xef,%ah */
597 EMIT3(0xc1, 0xe8, 0x0c); /* shr $0xc,%eax */
598 EMIT3(0x83, 0xe0, 0x01); /* and $0x1,%eax */
601 case BPF_S_ANC_PKTTYPE
:
603 int off
= pkt_type_offset();
608 /* movzbl off8(%rdi),%eax */
609 EMIT4(0x0f, 0xb6, 0x47, off
);
611 /* movbl off32(%rdi),%eax */
612 EMIT3(0x0f, 0xb6, 0x87);
615 EMIT3(0x83, 0xe0, PKT_TYPE_MAX
); /* and $0x7,%eax */
619 func
= CHOOSE_LOAD_FUNC(K
, sk_load_word
);
620 common_load
: seen
|= SEEN_DATAREF
;
621 t_offset
= func
- (image
+ addrs
[i
]);
622 EMIT1_off32(0xbe, K
); /* mov imm32,%esi */
623 EMIT1_off32(0xe8, t_offset
); /* call */
626 func
= CHOOSE_LOAD_FUNC(K
, sk_load_half
);
629 func
= CHOOSE_LOAD_FUNC(K
, sk_load_byte
);
631 case BPF_S_LDX_B_MSH
:
632 func
= CHOOSE_LOAD_FUNC(K
, sk_load_byte_msh
);
633 seen
|= SEEN_DATAREF
| SEEN_XREG
;
634 t_offset
= func
- (image
+ addrs
[i
]);
635 EMIT1_off32(0xbe, K
); /* mov imm32,%esi */
636 EMIT1_off32(0xe8, t_offset
); /* call sk_load_byte_msh */
640 common_load_ind
: seen
|= SEEN_DATAREF
| SEEN_XREG
;
641 t_offset
= func
- (image
+ addrs
[i
]);
644 EMIT3(0x8d, 0x73, K
); /* lea imm8(%rbx), %esi */
646 EMIT2(0x8d, 0xb3); /* lea imm32(%rbx),%esi */
650 EMIT2(0x89,0xde); /* mov %ebx,%esi */
652 EMIT1_off32(0xe8, t_offset
); /* call sk_load_xxx_ind */
656 goto common_load_ind
;
659 goto common_load_ind
;
661 t_offset
= addrs
[i
+ K
] - addrs
[i
];
664 COND_SEL(BPF_S_JMP_JGT_K
, X86_JA
, X86_JBE
);
665 COND_SEL(BPF_S_JMP_JGE_K
, X86_JAE
, X86_JB
);
666 COND_SEL(BPF_S_JMP_JEQ_K
, X86_JE
, X86_JNE
);
667 COND_SEL(BPF_S_JMP_JSET_K
,X86_JNE
, X86_JE
);
668 COND_SEL(BPF_S_JMP_JGT_X
, X86_JA
, X86_JBE
);
669 COND_SEL(BPF_S_JMP_JGE_X
, X86_JAE
, X86_JB
);
670 COND_SEL(BPF_S_JMP_JEQ_X
, X86_JE
, X86_JNE
);
671 COND_SEL(BPF_S_JMP_JSET_X
,X86_JNE
, X86_JE
);
673 cond_branch
: f_offset
= addrs
[i
+ filter
[i
].jf
] - addrs
[i
];
674 t_offset
= addrs
[i
+ filter
[i
].jt
] - addrs
[i
];
676 /* same targets, can avoid doing the test :) */
677 if (filter
[i
].jt
== filter
[i
].jf
) {
682 switch (filter
[i
].code
) {
683 case BPF_S_JMP_JGT_X
:
684 case BPF_S_JMP_JGE_X
:
685 case BPF_S_JMP_JEQ_X
:
687 EMIT2(0x39, 0xd8); /* cmp %ebx,%eax */
689 case BPF_S_JMP_JSET_X
:
691 EMIT2(0x85, 0xd8); /* test %ebx,%eax */
693 case BPF_S_JMP_JEQ_K
:
695 EMIT2(0x85, 0xc0); /* test %eax,%eax */
698 case BPF_S_JMP_JGT_K
:
699 case BPF_S_JMP_JGE_K
:
701 EMIT3(0x83, 0xf8, K
); /* cmp imm8,%eax */
703 EMIT1_off32(0x3d, K
); /* cmp imm32,%eax */
705 case BPF_S_JMP_JSET_K
:
707 EMIT2(0xa8, K
); /* test imm8,%al */
708 else if (!(K
& 0xFFFF00FF))
709 EMIT3(0xf6, 0xc4, K
>> 8); /* test imm8,%ah */
710 else if (K
<= 0xFFFF) {
711 EMIT2(0x66, 0xa9); /* test imm16,%ax */
714 EMIT1_off32(0xa9, K
); /* test imm32,%eax */
718 if (filter
[i
].jt
!= 0) {
719 if (filter
[i
].jf
&& f_offset
)
720 t_offset
+= is_near(f_offset
) ? 2 : 5;
721 EMIT_COND_JMP(t_op
, t_offset
);
726 EMIT_COND_JMP(f_op
, f_offset
);
729 /* hmm, too complex filter, give up with jit compiler */
734 if (unlikely(proglen
+ ilen
> oldproglen
)) {
735 pr_err("bpb_jit_compile fatal error\n");
737 module_free(NULL
, header
);
740 memcpy(image
+ proglen
, temp
, ilen
);
746 /* last bpf instruction is always a RET :
747 * use it to give the cleanup instruction(s) addr
749 cleanup_addr
= proglen
- 1; /* ret */
751 cleanup_addr
-= 1; /* leaveq */
752 if (seen_or_pass0
& SEEN_XREG
)
753 cleanup_addr
-= 4; /* mov -8(%rbp),%rbx */
756 if (proglen
!= oldproglen
)
757 pr_err("bpb_jit_compile proglen=%u != oldproglen=%u\n", proglen
, oldproglen
);
760 if (proglen
== oldproglen
) {
761 header
= bpf_alloc_binary(proglen
, &image
);
765 oldproglen
= proglen
;
768 if (bpf_jit_enable
> 1)
769 bpf_jit_dump(flen
, proglen
, pass
, image
);
772 bpf_flush_icache(header
, image
+ proglen
);
773 set_memory_ro((unsigned long)header
, header
->pages
);
774 fp
->bpf_func
= (void *)image
;
781 static void bpf_jit_free_deferred(struct work_struct
*work
)
783 struct sk_filter
*fp
= container_of(work
, struct sk_filter
, work
);
784 unsigned long addr
= (unsigned long)fp
->bpf_func
& PAGE_MASK
;
785 struct bpf_binary_header
*header
= (void *)addr
;
787 set_memory_rw(addr
, header
->pages
);
788 module_free(NULL
, header
);
792 void bpf_jit_free(struct sk_filter
*fp
)
794 if (fp
->bpf_func
!= sk_run_filter
) {
795 INIT_WORK(&fp
->work
, bpf_jit_free_deferred
);
796 schedule_work(&fp
->work
);