2 * Just-In-Time compiler for BPF filters on 32bit ARM
4 * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License.
11 #include <linux/bitops.h>
12 #include <linux/compiler.h>
13 #include <linux/errno.h>
14 #include <linux/filter.h>
15 #include <linux/moduleloader.h>
16 #include <linux/netdevice.h>
17 #include <linux/string.h>
18 #include <linux/slab.h>
19 #include <linux/if_vlan.h>
20 #include <asm/cacheflush.h>
21 #include <asm/hwcap.h>
23 #include "bpf_jit_32.h"
31 * r6 pointer to the skb
36 #define r_scratch ARM_R0
37 /* r1-r3 are (also) used for the unaligned loads on the non-ARMv7 slowpath */
42 #define r_skb_data ARM_R7
43 #define r_skb_hl ARM_R8
45 #define SCRATCH_SP_OFFSET 0
46 #define SCRATCH_OFF(k) (SCRATCH_SP_OFFSET + 4 * (k))
48 #define SEEN_MEM ((1 << BPF_MEMWORDS) - 1)
49 #define SEEN_MEM_WORD(k) (1 << (k))
50 #define SEEN_X (1 << BPF_MEMWORDS)
51 #define SEEN_CALL (1 << (BPF_MEMWORDS + 1))
52 #define SEEN_SKB (1 << (BPF_MEMWORDS + 2))
53 #define SEEN_DATA (1 << (BPF_MEMWORDS + 3))
55 #define FLAG_NEED_X_RESET (1 << 0)
58 const struct sk_filter *skf;
60 unsigned prologue_bytes;
66 #if __LINUX_ARM_ARCH__ < 7
73 int bpf_jit_enable __read_mostly;
75 static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset)
80 err = skb_copy_bits(skb, offset, &ret, 1);
82 return (u64)err << 32 | ret;
85 static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset)
90 err = skb_copy_bits(skb, offset, &ret, 2);
92 return (u64)err << 32 | ntohs(ret);
95 static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
100 err = skb_copy_bits(skb, offset, &ret, 4);
102 return (u64)err << 32 | ntohl(ret);
106 * Wrapper that handles both OABI and EABI and assures Thumb2 interworking
107 * (where the assembly routines like __aeabi_uidiv could cause problems).
109 static u32 jit_udiv(u32 dividend, u32 divisor)
111 return dividend / divisor;
114 static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
116 if (ctx->target != NULL)
117 ctx->target[ctx->idx] = inst | (cond << 28);
123 * Emit an instruction that will be executed unconditionally.
125 static inline void emit(u32 inst, struct jit_ctx *ctx)
127 _emit(ARM_COND_AL, inst, ctx);
130 static u16 saved_regs(struct jit_ctx *ctx)
134 if ((ctx->skf->len > 1) ||
135 (ctx->skf->insns[0].code == BPF_S_RET_A))
138 #ifdef CONFIG_FRAME_POINTER
139 ret |= (1 << ARM_FP) | (1 << ARM_IP) | (1 << ARM_LR) | (1 << ARM_PC);
141 if (ctx->seen & SEEN_CALL)
144 if (ctx->seen & (SEEN_DATA | SEEN_SKB))
146 if (ctx->seen & SEEN_DATA)
147 ret |= (1 << r_skb_data) | (1 << r_skb_hl);
148 if (ctx->seen & SEEN_X)
154 static inline int mem_words_used(struct jit_ctx *ctx)
156 /* yes, we do waste some stack space IF there are "holes" in the set" */
157 return fls(ctx->seen & SEEN_MEM);
160 static inline bool is_load_to_a(u16 inst)
168 case BPF_S_ANC_IFINDEX:
170 case BPF_S_ANC_PROTOCOL:
171 case BPF_S_ANC_RXHASH:
172 case BPF_S_ANC_VLAN_TAG:
173 case BPF_S_ANC_VLAN_TAG_PRESENT:
174 case BPF_S_ANC_QUEUE:
181 static void build_prologue(struct jit_ctx *ctx)
183 u16 reg_set = saved_regs(ctx);
184 u16 first_inst = ctx->skf->insns[0].code;
187 #ifdef CONFIG_FRAME_POINTER
188 emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
189 emit(ARM_PUSH(reg_set), ctx);
190 emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
193 emit(ARM_PUSH(reg_set), ctx);
196 if (ctx->seen & (SEEN_DATA | SEEN_SKB))
197 emit(ARM_MOV_R(r_skb, ARM_R0), ctx);
199 if (ctx->seen & SEEN_DATA) {
200 off = offsetof(struct sk_buff, data);
201 emit(ARM_LDR_I(r_skb_data, r_skb, off), ctx);
202 /* headlen = len - data_len */
203 off = offsetof(struct sk_buff, len);
204 emit(ARM_LDR_I(r_skb_hl, r_skb, off), ctx);
205 off = offsetof(struct sk_buff, data_len);
206 emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
207 emit(ARM_SUB_R(r_skb_hl, r_skb_hl, r_scratch), ctx);
210 if (ctx->flags & FLAG_NEED_X_RESET)
211 emit(ARM_MOV_I(r_X, 0), ctx);
213 /* do not leak kernel data to userspace */
214 if ((first_inst != BPF_S_RET_K) && !(is_load_to_a(first_inst)))
215 emit(ARM_MOV_I(r_A, 0), ctx);
217 /* stack space for the BPF_MEM words */
218 if (ctx->seen & SEEN_MEM)
219 emit(ARM_SUB_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
222 static void build_epilogue(struct jit_ctx *ctx)
224 u16 reg_set = saved_regs(ctx);
226 if (ctx->seen & SEEN_MEM)
227 emit(ARM_ADD_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
229 reg_set &= ~(1 << ARM_LR);
231 #ifdef CONFIG_FRAME_POINTER
232 /* the first instruction of the prologue was: mov ip, sp */
233 reg_set &= ~(1 << ARM_IP);
234 reg_set |= (1 << ARM_SP);
235 emit(ARM_LDM(ARM_SP, reg_set), ctx);
238 if (ctx->seen & SEEN_CALL)
239 reg_set |= 1 << ARM_PC;
240 emit(ARM_POP(reg_set), ctx);
243 if (!(ctx->seen & SEEN_CALL))
244 emit(ARM_BX(ARM_LR), ctx);
248 static int16_t imm8m(u32 x)
252 for (rot = 0; rot < 16; rot++)
253 if ((x & ~ror32(0xff, 2 * rot)) == 0)
254 return rol32(x, 2 * rot) | (rot << 8);
259 #if __LINUX_ARM_ARCH__ < 7
261 static u16 imm_offset(u32 k, struct jit_ctx *ctx)
263 unsigned i = 0, offset;
266 /* on the "fake" run we just count them (duplicates included) */
267 if (ctx->target == NULL) {
272 while ((i < ctx->imm_count) && ctx->imms[i]) {
273 if (ctx->imms[i] == k)
278 if (ctx->imms[i] == 0)
281 /* constants go just after the epilogue */
282 offset = ctx->offsets[ctx->skf->len];
283 offset += ctx->prologue_bytes;
284 offset += ctx->epilogue_bytes;
287 ctx->target[offset / 4] = k;
289 /* PC in ARM mode == address of the instruction + 8 */
290 imm = offset - (8 + ctx->idx * 4);
295 #endif /* __LINUX_ARM_ARCH__ */
298 * Move an immediate that's not an imm8m to a core register.
300 static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)
302 #if __LINUX_ARM_ARCH__ < 7
303 emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
305 emit(ARM_MOVW(rd, val & 0xffff), ctx);
307 emit(ARM_MOVT(rd, val >> 16), ctx);
311 static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)
313 int imm12 = imm8m(val);
316 emit(ARM_MOV_I(rd, imm12), ctx);
318 emit_mov_i_no8m(rd, val, ctx);
321 #if __LINUX_ARM_ARCH__ < 6
323 static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
325 _emit(cond, ARM_LDRB_I(ARM_R3, r_addr, 1), ctx);
326 _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
327 _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 3), ctx);
328 _emit(cond, ARM_LSL_I(ARM_R3, ARM_R3, 16), ctx);
329 _emit(cond, ARM_LDRB_I(ARM_R0, r_addr, 2), ctx);
330 _emit(cond, ARM_ORR_S(ARM_R3, ARM_R3, ARM_R1, SRTYPE_LSL, 24), ctx);
331 _emit(cond, ARM_ORR_R(ARM_R3, ARM_R3, ARM_R2), ctx);
332 _emit(cond, ARM_ORR_S(r_res, ARM_R3, ARM_R0, SRTYPE_LSL, 8), ctx);
335 static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
337 _emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
338 _emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 1), ctx);
339 _emit(cond, ARM_ORR_S(r_res, ARM_R2, ARM_R1, SRTYPE_LSL, 8), ctx);
342 static inline void emit_swap16(u8 r_dst, u8 r_src, struct jit_ctx *ctx)
344 /* r_dst = (r_src << 8) | (r_src >> 8) */
345 emit(ARM_LSL_I(ARM_R1, r_src, 8), ctx);
346 emit(ARM_ORR_S(r_dst, ARM_R1, r_src, SRTYPE_LSR, 8), ctx);
349 * we need to mask out the bits set in r_dst[23:16] due to
350 * the first shift instruction.
352 * note that 0x8ff is the encoded immediate 0x00ff0000.
354 emit(ARM_BIC_I(r_dst, r_dst, 0x8ff), ctx);
359 static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
361 _emit(cond, ARM_LDR_I(r_res, r_addr, 0), ctx);
362 #ifdef __LITTLE_ENDIAN
363 _emit(cond, ARM_REV(r_res, r_res), ctx);
367 static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
369 _emit(cond, ARM_LDRH_I(r_res, r_addr, 0), ctx);
370 #ifdef __LITTLE_ENDIAN
371 _emit(cond, ARM_REV16(r_res, r_res), ctx);
375 static inline void emit_swap16(u8 r_dst __maybe_unused,
376 u8 r_src __maybe_unused,
377 struct jit_ctx *ctx __maybe_unused)
379 #ifdef __LITTLE_ENDIAN
380 emit(ARM_REV16(r_dst, r_src), ctx);
384 #endif /* __LINUX_ARM_ARCH__ < 6 */
387 /* Compute the immediate value for a PC-relative branch. */
388 static inline u32 b_imm(unsigned tgt, struct jit_ctx *ctx)
392 if (ctx->target == NULL)
395 * BPF allows only forward jumps and the offset of the target is
396 * still the one computed during the first pass.
398 imm = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx * 4 + 8);
403 #define OP_IMM3(op, r1, r2, imm_val, ctx) \
405 imm12 = imm8m(imm_val); \
407 emit_mov_i_no8m(r_scratch, imm_val, ctx); \
408 emit(op ## _R((r1), (r2), r_scratch), ctx); \
410 emit(op ## _I((r1), (r2), imm12), ctx); \
414 static inline void emit_err_ret(u8 cond, struct jit_ctx *ctx)
416 if (ctx->ret0_fp_idx >= 0) {
417 _emit(cond, ARM_B(b_imm(ctx->ret0_fp_idx, ctx)), ctx);
418 /* NOP to keep the size constant between passes */
419 emit(ARM_MOV_R(ARM_R0, ARM_R0), ctx);
421 _emit(cond, ARM_MOV_I(ARM_R0, 0), ctx);
422 _emit(cond, ARM_B(b_imm(ctx->skf->len, ctx)), ctx);
426 static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
428 #if __LINUX_ARM_ARCH__ < 5
429 emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
431 if (elf_hwcap & HWCAP_THUMB)
432 emit(ARM_BX(tgt_reg), ctx);
434 emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
436 emit(ARM_BLX_R(tgt_reg), ctx);
440 static inline void emit_udiv(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx)
442 #if __LINUX_ARM_ARCH__ == 7
443 if (elf_hwcap & HWCAP_IDIVA) {
444 emit(ARM_UDIV(rd, rm, rn), ctx);
449 emit(ARM_MOV_R(ARM_R0, rm), ctx);
451 emit(ARM_MOV_R(ARM_R1, rn), ctx);
453 ctx->seen |= SEEN_CALL;
454 emit_mov_i(ARM_R3, (u32)jit_udiv, ctx);
455 emit_blx_r(ARM_R3, ctx);
458 emit(ARM_MOV_R(rd, ARM_R0), ctx);
461 static inline void update_on_xread(struct jit_ctx *ctx)
463 if (!(ctx->seen & SEEN_X))
464 ctx->flags |= FLAG_NEED_X_RESET;
469 static int build_body(struct jit_ctx *ctx)
471 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
472 const struct sk_filter *prog = ctx->skf;
473 const struct sock_filter *inst;
474 unsigned i, load_order, off, condt;
478 for (i = 0; i < prog->len; i++) {
479 inst = &(prog->insns[i]);
480 /* K as an immediate value operand */
483 /* compute offsets only in the fake pass */
484 if (ctx->target == NULL)
485 ctx->offsets[i] = ctx->idx * 4;
487 switch (inst->code) {
489 emit_mov_i(r_A, k, ctx);
492 ctx->seen |= SEEN_SKB;
493 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
494 emit(ARM_LDR_I(r_A, r_skb,
495 offsetof(struct sk_buff, len)), ctx);
499 ctx->seen |= SEEN_MEM_WORD(k);
500 emit(ARM_LDR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
511 /* the interpreter will deal with the negative K */
514 emit_mov_i(r_off, k, ctx);
516 ctx->seen |= SEEN_DATA | SEEN_CALL;
518 if (load_order > 0) {
519 emit(ARM_SUB_I(r_scratch, r_skb_hl,
520 1 << load_order), ctx);
521 emit(ARM_CMP_R(r_scratch, r_off), ctx);
524 emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
528 _emit(condt, ARM_ADD_R(r_scratch, r_off, r_skb_data),
532 _emit(condt, ARM_LDRB_I(r_A, r_scratch, 0),
534 else if (load_order == 1)
535 emit_load_be16(condt, r_A, r_scratch, ctx);
536 else if (load_order == 2)
537 emit_load_be32(condt, r_A, r_scratch, ctx);
539 _emit(condt, ARM_B(b_imm(i + 1, ctx)), ctx);
542 emit_mov_i(ARM_R3, (u32)load_func[load_order], ctx);
543 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
544 /* the offset is already in R1 */
545 emit_blx_r(ARM_R3, ctx);
546 /* check the result of skb_copy_bits */
547 emit(ARM_CMP_I(ARM_R1, 0), ctx);
548 emit_err_ret(ARM_COND_NE, ctx);
549 emit(ARM_MOV_R(r_A, ARM_R0), ctx);
560 OP_IMM3(ARM_ADD, r_off, r_X, k, ctx);
564 emit_mov_i(r_X, k, ctx);
566 case BPF_S_LDX_W_LEN:
567 ctx->seen |= SEEN_X | SEEN_SKB;
568 emit(ARM_LDR_I(r_X, r_skb,
569 offsetof(struct sk_buff, len)), ctx);
572 ctx->seen |= SEEN_X | SEEN_MEM_WORD(k);
573 emit(ARM_LDR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
575 case BPF_S_LDX_B_MSH:
576 /* x = ((*(frame + k)) & 0xf) << 2; */
577 ctx->seen |= SEEN_X | SEEN_DATA | SEEN_CALL;
578 /* the interpreter should deal with the negative K */
581 /* offset in r1: we might have to take the slow path */
582 emit_mov_i(r_off, k, ctx);
583 emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
585 /* load in r0: common with the slowpath */
586 _emit(ARM_COND_HI, ARM_LDRB_R(ARM_R0, r_skb_data,
589 * emit_mov_i() might generate one or two instructions,
590 * the same holds for emit_blx_r()
592 _emit(ARM_COND_HI, ARM_B(b_imm(i + 1, ctx) - 2), ctx);
594 emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
596 emit_mov_i(ARM_R3, (u32)jit_get_skb_b, ctx);
597 emit_blx_r(ARM_R3, ctx);
598 /* check the return value of skb_copy_bits */
599 emit(ARM_CMP_I(ARM_R1, 0), ctx);
600 emit_err_ret(ARM_COND_NE, ctx);
602 emit(ARM_AND_I(r_X, ARM_R0, 0x00f), ctx);
603 emit(ARM_LSL_I(r_X, r_X, 2), ctx);
606 ctx->seen |= SEEN_MEM_WORD(k);
607 emit(ARM_STR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
610 update_on_xread(ctx);
611 ctx->seen |= SEEN_MEM_WORD(k);
612 emit(ARM_STR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
614 case BPF_S_ALU_ADD_K:
616 OP_IMM3(ARM_ADD, r_A, r_A, k, ctx);
618 case BPF_S_ALU_ADD_X:
619 update_on_xread(ctx);
620 emit(ARM_ADD_R(r_A, r_A, r_X), ctx);
622 case BPF_S_ALU_SUB_K:
624 OP_IMM3(ARM_SUB, r_A, r_A, k, ctx);
626 case BPF_S_ALU_SUB_X:
627 update_on_xread(ctx);
628 emit(ARM_SUB_R(r_A, r_A, r_X), ctx);
630 case BPF_S_ALU_MUL_K:
632 emit_mov_i(r_scratch, k, ctx);
633 emit(ARM_MUL(r_A, r_A, r_scratch), ctx);
635 case BPF_S_ALU_MUL_X:
636 update_on_xread(ctx);
637 emit(ARM_MUL(r_A, r_A, r_X), ctx);
639 case BPF_S_ALU_DIV_K:
640 /* current k == reciprocal_value(userspace k) */
641 emit_mov_i(r_scratch, k, ctx);
642 /* A = top 32 bits of the product */
643 emit(ARM_UMULL(r_scratch, r_A, r_A, r_scratch), ctx);
645 case BPF_S_ALU_DIV_X:
646 update_on_xread(ctx);
647 emit(ARM_CMP_I(r_X, 0), ctx);
648 emit_err_ret(ARM_COND_EQ, ctx);
649 emit_udiv(r_A, r_A, r_X, ctx);
653 OP_IMM3(ARM_ORR, r_A, r_A, k, ctx);
656 update_on_xread(ctx);
657 emit(ARM_ORR_R(r_A, r_A, r_X), ctx);
659 case BPF_S_ALU_XOR_K:
661 OP_IMM3(ARM_EOR, r_A, r_A, k, ctx);
663 case BPF_S_ANC_ALU_XOR_X:
664 case BPF_S_ALU_XOR_X:
666 update_on_xread(ctx);
667 emit(ARM_EOR_R(r_A, r_A, r_X), ctx);
669 case BPF_S_ALU_AND_K:
671 OP_IMM3(ARM_AND, r_A, r_A, k, ctx);
673 case BPF_S_ALU_AND_X:
674 update_on_xread(ctx);
675 emit(ARM_AND_R(r_A, r_A, r_X), ctx);
677 case BPF_S_ALU_LSH_K:
678 if (unlikely(k > 31))
680 emit(ARM_LSL_I(r_A, r_A, k), ctx);
682 case BPF_S_ALU_LSH_X:
683 update_on_xread(ctx);
684 emit(ARM_LSL_R(r_A, r_A, r_X), ctx);
686 case BPF_S_ALU_RSH_K:
687 if (unlikely(k > 31))
689 emit(ARM_LSR_I(r_A, r_A, k), ctx);
691 case BPF_S_ALU_RSH_X:
692 update_on_xread(ctx);
693 emit(ARM_LSR_R(r_A, r_A, r_X), ctx);
697 emit(ARM_RSB_I(r_A, r_A, 0), ctx);
701 emit(ARM_B(b_imm(i + k + 1, ctx)), ctx);
703 case BPF_S_JMP_JEQ_K:
704 /* pc += (A == K) ? pc->jt : pc->jf */
707 case BPF_S_JMP_JGT_K:
708 /* pc += (A > K) ? pc->jt : pc->jf */
711 case BPF_S_JMP_JGE_K:
712 /* pc += (A >= K) ? pc->jt : pc->jf */
717 emit_mov_i_no8m(r_scratch, k, ctx);
718 emit(ARM_CMP_R(r_A, r_scratch), ctx);
720 emit(ARM_CMP_I(r_A, imm12), ctx);
724 _emit(condt, ARM_B(b_imm(i + inst->jt + 1,
727 _emit(condt ^ 1, ARM_B(b_imm(i + inst->jf + 1,
730 case BPF_S_JMP_JEQ_X:
731 /* pc += (A == X) ? pc->jt : pc->jf */
734 case BPF_S_JMP_JGT_X:
735 /* pc += (A > X) ? pc->jt : pc->jf */
738 case BPF_S_JMP_JGE_X:
739 /* pc += (A >= X) ? pc->jt : pc->jf */
742 update_on_xread(ctx);
743 emit(ARM_CMP_R(r_A, r_X), ctx);
745 case BPF_S_JMP_JSET_K:
746 /* pc += (A & K) ? pc->jt : pc->jf */
748 /* not set iff all zeroes iff Z==1 iff EQ */
752 emit_mov_i_no8m(r_scratch, k, ctx);
753 emit(ARM_TST_R(r_A, r_scratch), ctx);
755 emit(ARM_TST_I(r_A, imm12), ctx);
758 case BPF_S_JMP_JSET_X:
759 /* pc += (A & X) ? pc->jt : pc->jf */
760 update_on_xread(ctx);
762 emit(ARM_TST_R(r_A, r_X), ctx);
765 emit(ARM_MOV_R(ARM_R0, r_A), ctx);
768 if ((k == 0) && (ctx->ret0_fp_idx < 0))
769 ctx->ret0_fp_idx = i;
770 emit_mov_i(ARM_R0, k, ctx);
772 if (i != ctx->skf->len - 1)
773 emit(ARM_B(b_imm(prog->len, ctx)), ctx);
778 emit(ARM_MOV_R(r_X, r_A), ctx);
782 update_on_xread(ctx);
783 emit(ARM_MOV_R(r_A, r_X), ctx);
785 case BPF_S_ANC_PROTOCOL:
786 /* A = ntohs(skb->protocol) */
787 ctx->seen |= SEEN_SKB;
788 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
790 off = offsetof(struct sk_buff, protocol);
791 emit(ARM_LDRH_I(r_scratch, r_skb, off), ctx);
792 emit_swap16(r_A, r_scratch, ctx);
795 /* r_scratch = current_thread_info() */
796 OP_IMM3(ARM_BIC, r_scratch, ARM_SP, THREAD_SIZE - 1, ctx);
797 /* A = current_thread_info()->cpu */
798 BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4);
799 off = offsetof(struct thread_info, cpu);
800 emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
802 case BPF_S_ANC_IFINDEX:
803 /* A = skb->dev->ifindex */
804 ctx->seen |= SEEN_SKB;
805 off = offsetof(struct sk_buff, dev);
806 emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
808 emit(ARM_CMP_I(r_scratch, 0), ctx);
809 emit_err_ret(ARM_COND_EQ, ctx);
811 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
813 off = offsetof(struct net_device, ifindex);
814 emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
817 ctx->seen |= SEEN_SKB;
818 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
819 off = offsetof(struct sk_buff, mark);
820 emit(ARM_LDR_I(r_A, r_skb, off), ctx);
822 case BPF_S_ANC_RXHASH:
823 ctx->seen |= SEEN_SKB;
824 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, rxhash) != 4);
825 off = offsetof(struct sk_buff, rxhash);
826 emit(ARM_LDR_I(r_A, r_skb, off), ctx);
828 case BPF_S_ANC_VLAN_TAG:
829 case BPF_S_ANC_VLAN_TAG_PRESENT:
830 ctx->seen |= SEEN_SKB;
831 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
832 off = offsetof(struct sk_buff, vlan_tci);
833 emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
834 if (inst->code == BPF_S_ANC_VLAN_TAG)
835 OP_IMM3(ARM_AND, r_A, r_A, VLAN_VID_MASK, ctx);
837 OP_IMM3(ARM_AND, r_A, r_A, VLAN_TAG_PRESENT, ctx);
839 case BPF_S_ANC_QUEUE:
840 ctx->seen |= SEEN_SKB;
841 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
842 queue_mapping) != 2);
843 BUILD_BUG_ON(offsetof(struct sk_buff,
844 queue_mapping) > 0xff);
845 off = offsetof(struct sk_buff, queue_mapping);
846 emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
853 /* compute offsets only during the first pass */
854 if (ctx->target == NULL)
855 ctx->offsets[i] = ctx->idx * 4;
861 void bpf_jit_compile(struct sk_filter *fp)
870 memset(&ctx, 0, sizeof(ctx));
872 ctx.ret0_fp_idx = -1;
874 ctx.offsets = kzalloc(4 * (ctx.skf->len + 1), GFP_KERNEL);
875 if (ctx.offsets == NULL)
878 /* fake pass to fill in the ctx->seen */
879 if (unlikely(build_body(&ctx)))
883 build_prologue(&ctx);
884 ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
886 #if __LINUX_ARM_ARCH__ < 7
888 build_epilogue(&ctx);
889 ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
891 ctx.idx += ctx.imm_count;
893 ctx.imms = kzalloc(4 * ctx.imm_count, GFP_KERNEL);
894 if (ctx.imms == NULL)
898 /* there's nothing after the epilogue on ARMv7 */
899 build_epilogue(&ctx);
902 alloc_size = 4 * ctx.idx;
903 ctx.target = module_alloc(max(sizeof(struct work_struct),
905 if (unlikely(ctx.target == NULL))
909 build_prologue(&ctx);
911 build_epilogue(&ctx);
913 flush_icache_range((u32)ctx.target, (u32)(ctx.target + ctx.idx));
915 #if __LINUX_ARM_ARCH__ < 7
920 if (bpf_jit_enable > 1)
921 print_hex_dump(KERN_INFO, "BPF JIT code: ",
922 DUMP_PREFIX_ADDRESS, 16, 4, ctx.target,
925 fp->bpf_func = (void *)ctx.target;
931 static void bpf_jit_free_worker(struct work_struct *work)
933 module_free(NULL, work);
936 void bpf_jit_free(struct sk_filter *fp)
938 struct work_struct *work;
940 if (fp->bpf_func != sk_run_filter) {
941 work = (struct work_struct *)fp->bpf_func;
943 INIT_WORK(work, bpf_jit_free_worker);