2 * Just-In-Time compiler for eBPF filters on 32bit ARM
4 * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
5 * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
12 #include <linux/bpf.h>
13 #include <linux/bitops.h>
14 #include <linux/compiler.h>
15 #include <linux/errno.h>
16 #include <linux/filter.h>
17 #include <linux/netdevice.h>
18 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/if_vlan.h>
22 #include <asm/cacheflush.h>
23 #include <asm/hwcap.h>
24 #include <asm/opcodes.h>
26 #include "bpf_jit_32.h"
29 * eBPF prog stack layout:
32 * original ARM_SP => +-----+
33 * | | callee saved registers
34 * +-----+ <= (BPF_FP + SCRATCH_SIZE)
35 * | ... | eBPF JIT scratch space
36 * eBPF fp register => +-----+
37 * (BPF_FP) | ... | eBPF prog stack
39 * |RSVD | JIT scratchpad
40 * current ARM_SP => +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE)
42 * | ... | Function call stack
47 * The callee saved registers depends on whether frame pointers are enabled.
48 * With frame pointers (to be compliant with the ABI):
51 * original ARM_SP => +------------------+ \
53 * current ARM_FP => +------------------+ } callee saved registers
54 * |r4-r8,r10,fp,ip,lr| |
55 * +------------------+ /
58 * Without frame pointers:
61 * original ARM_SP => +------------------+
62 * | r4-r8,r10,fp,lr | callee saved registers
63 * current ARM_FP => +------------------+
66 * When popping registers off the stack at the end of a BPF function, we
67 * reference them via the current ARM_FP register.
69 #define CALLEE_MASK (1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \
70 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R10 | \
72 #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR)
73 #define CALLEE_POP_MASK (CALLEE_MASK | 1 << ARM_PC)
75 #define STACK_OFFSET(k) (k)
76 #define TMP_REG_1 (MAX_BPF_JIT_REG + 0) /* TEMP Register 1 */
77 #define TMP_REG_2 (MAX_BPF_JIT_REG + 1) /* TEMP Register 2 */
78 #define TCALL_CNT (MAX_BPF_JIT_REG + 2) /* Tail Call Count */
80 #define FLAG_IMM_OVERFLOW (1 << 0)
83 * Map eBPF registers to ARM 32bit registers or stack scratch space.
85 * 1. First argument is passed using the arm 32bit registers and rest of the
86 * arguments are passed on stack scratch space.
87 * 2. First callee-saved arugument is mapped to arm 32 bit registers and rest
88 * arguments are mapped to scratch space on stack.
89 * 3. We need two 64 bit temp registers to do complex operations on eBPF
92 * As the eBPF registers are all 64 bit registers and arm has only 32 bit
93 * registers, we have to map each eBPF registers with two arm 32 bit regs or
94 * scratch memory space and we have to build eBPF 64 bit register from those.
97 static const u8 bpf2a32
[][2] = {
98 /* return value from in-kernel function, and exit value from eBPF */
99 [BPF_REG_0
] = {ARM_R1
, ARM_R0
},
100 /* arguments from eBPF program to in-kernel function */
101 [BPF_REG_1
] = {ARM_R3
, ARM_R2
},
102 /* Stored on stack scratch space */
103 [BPF_REG_2
] = {STACK_OFFSET(0), STACK_OFFSET(4)},
104 [BPF_REG_3
] = {STACK_OFFSET(8), STACK_OFFSET(12)},
105 [BPF_REG_4
] = {STACK_OFFSET(16), STACK_OFFSET(20)},
106 [BPF_REG_5
] = {STACK_OFFSET(24), STACK_OFFSET(28)},
107 /* callee saved registers that in-kernel function will preserve */
108 [BPF_REG_6
] = {ARM_R5
, ARM_R4
},
109 /* Stored on stack scratch space */
110 [BPF_REG_7
] = {STACK_OFFSET(32), STACK_OFFSET(36)},
111 [BPF_REG_8
] = {STACK_OFFSET(40), STACK_OFFSET(44)},
112 [BPF_REG_9
] = {STACK_OFFSET(48), STACK_OFFSET(52)},
113 /* Read only Frame Pointer to access Stack */
114 [BPF_REG_FP
] = {STACK_OFFSET(56), STACK_OFFSET(60)},
115 /* Temporary Register for internal BPF JIT, can be used
116 * for constant blindings and others.
118 [TMP_REG_1
] = {ARM_R7
, ARM_R6
},
119 [TMP_REG_2
] = {ARM_R10
, ARM_R8
},
120 /* Tail call count. Stored on stack scratch space. */
121 [TCALL_CNT
] = {STACK_OFFSET(64), STACK_OFFSET(68)},
122 /* temporary register for blinding constants.
123 * Stored on stack scratch space.
125 [BPF_REG_AX
] = {STACK_OFFSET(72), STACK_OFFSET(76)},
128 #define dst_lo dst[1]
129 #define dst_hi dst[0]
130 #define src_lo src[1]
131 #define src_hi src[0]
137 * idx : index of current last JITed instruction.
138 * prologue_bytes : bytes used in prologue.
139 * epilogue_offset : offset of epilogue starting.
140 * offsets : array of eBPF instruction offsets in
142 * target : final JITed code.
143 * epilogue_bytes : no of bytes used in epilogue.
144 * imm_count : no of immediate counts used for global
146 * imms : array of global variable addresses.
150 const struct bpf_prog
*prog
;
152 unsigned int prologue_bytes
;
153 unsigned int epilogue_offset
;
158 #if __LINUX_ARM_ARCH__ < 7
166 * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
167 * (where the assembly routines like __aeabi_uidiv could cause problems).
169 static u32
jit_udiv32(u32 dividend
, u32 divisor
)
171 return dividend
/ divisor
;
174 static u32
jit_mod32(u32 dividend
, u32 divisor
)
176 return dividend
% divisor
;
179 static inline void _emit(int cond
, u32 inst
, struct jit_ctx
*ctx
)
181 inst
|= (cond
<< 28);
182 inst
= __opcode_to_mem_arm(inst
);
184 if (ctx
->target
!= NULL
)
185 ctx
->target
[ctx
->idx
] = inst
;
191 * Emit an instruction that will be executed unconditionally.
193 static inline void emit(u32 inst
, struct jit_ctx
*ctx
)
195 _emit(ARM_COND_AL
, inst
, ctx
);
199 * Checks if immediate value can be converted to imm12(12 bits) value.
201 static int16_t imm8m(u32 x
)
205 for (rot
= 0; rot
< 16; rot
++)
206 if ((x
& ~ror32(0xff, 2 * rot
)) == 0)
207 return rol32(x
, 2 * rot
) | (rot
<< 8);
212 * Initializes the JIT space with undefined instructions.
214 static void jit_fill_hole(void *area
, unsigned int size
)
217 /* We are guaranteed to have aligned memory. */
218 for (ptr
= area
; size
>= sizeof(u32
); size
-= sizeof(u32
))
219 *ptr
++ = __opcode_to_mem_arm(ARM_INST_UDF
);
222 #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
223 /* EABI requires the stack to be aligned to 64-bit boundaries */
224 #define STACK_ALIGNMENT 8
226 /* Stack must be aligned to 32-bit boundaries */
227 #define STACK_ALIGNMENT 4
230 /* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
231 * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
232 * BPF_REG_FP and Tail call counts.
234 #define SCRATCH_SIZE 80
236 /* total stack size used in JITed code */
237 #define _STACK_SIZE \
238 (ctx->prog->aux->stack_depth + \
240 + 4 /* extra for skb_copy_bits buffer */)
242 #define STACK_SIZE ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
244 /* Get the offset of eBPF REGISTERs stored on scratch space. */
245 #define STACK_VAR(off) (STACK_SIZE-off-4)
247 /* Offset of skb_copy_bits buffer */
248 #define SKB_BUFFER STACK_VAR(SCRATCH_SIZE)
250 #if __LINUX_ARM_ARCH__ < 7
252 static u16
imm_offset(u32 k
, struct jit_ctx
*ctx
)
254 unsigned int i
= 0, offset
;
257 /* on the "fake" run we just count them (duplicates included) */
258 if (ctx
->target
== NULL
) {
263 while ((i
< ctx
->imm_count
) && ctx
->imms
[i
]) {
264 if (ctx
->imms
[i
] == k
)
269 if (ctx
->imms
[i
] == 0)
272 /* constants go just after the epilogue */
273 offset
= ctx
->offsets
[ctx
->prog
->len
- 1] * 4;
274 offset
+= ctx
->prologue_bytes
;
275 offset
+= ctx
->epilogue_bytes
;
278 ctx
->target
[offset
/ 4] = k
;
280 /* PC in ARM mode == address of the instruction + 8 */
281 imm
= offset
- (8 + ctx
->idx
* 4);
285 * literal pool is too far, signal it into flags. we
286 * can only detect it on the second pass unfortunately.
288 ctx
->flags
|= FLAG_IMM_OVERFLOW
;
295 #endif /* __LINUX_ARM_ARCH__ */
297 static inline int bpf2a32_offset(int bpf_to
, int bpf_from
,
298 const struct jit_ctx
*ctx
) {
301 if (ctx
->target
== NULL
)
303 to
= ctx
->offsets
[bpf_to
];
304 from
= ctx
->offsets
[bpf_from
];
306 return to
- from
- 1;
310 * Move an immediate that's not an imm8m to a core register.
312 static inline void emit_mov_i_no8m(const u8 rd
, u32 val
, struct jit_ctx
*ctx
)
314 #if __LINUX_ARM_ARCH__ < 7
315 emit(ARM_LDR_I(rd
, ARM_PC
, imm_offset(val
, ctx
)), ctx
);
317 emit(ARM_MOVW(rd
, val
& 0xffff), ctx
);
319 emit(ARM_MOVT(rd
, val
>> 16), ctx
);
323 static inline void emit_mov_i(const u8 rd
, u32 val
, struct jit_ctx
*ctx
)
325 int imm12
= imm8m(val
);
328 emit(ARM_MOV_I(rd
, imm12
), ctx
);
330 emit_mov_i_no8m(rd
, val
, ctx
);
333 static void emit_bx_r(u8 tgt_reg
, struct jit_ctx
*ctx
)
335 if (elf_hwcap
& HWCAP_THUMB
)
336 emit(ARM_BX(tgt_reg
), ctx
);
338 emit(ARM_MOV_R(ARM_PC
, tgt_reg
), ctx
);
341 static inline void emit_blx_r(u8 tgt_reg
, struct jit_ctx
*ctx
)
343 #if __LINUX_ARM_ARCH__ < 5
344 emit(ARM_MOV_R(ARM_LR
, ARM_PC
), ctx
);
345 emit_bx_r(tgt_reg
, ctx
);
347 emit(ARM_BLX_R(tgt_reg
), ctx
);
351 static inline int epilogue_offset(const struct jit_ctx
*ctx
)
354 /* No need for 1st dummy run */
355 if (ctx
->target
== NULL
)
357 to
= ctx
->epilogue_offset
;
360 return to
- from
- 2;
363 static inline void emit_udivmod(u8 rd
, u8 rm
, u8 rn
, struct jit_ctx
*ctx
, u8 op
)
365 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
367 #if __LINUX_ARM_ARCH__ == 7
368 if (elf_hwcap
& HWCAP_IDIVA
) {
370 emit(ARM_UDIV(rd
, rm
, rn
), ctx
);
372 emit(ARM_UDIV(ARM_IP
, rm
, rn
), ctx
);
373 emit(ARM_MLS(rd
, rn
, ARM_IP
, rm
), ctx
);
380 * For BPF_ALU | BPF_DIV | BPF_K instructions
381 * As ARM_R1 and ARM_R0 contains 1st argument of bpf
382 * function, we need to save it on caller side to save
383 * it from getting destroyed within callee.
384 * After the return from the callee, we restore ARM_R0
388 emit(ARM_MOV_R(tmp
[0], ARM_R1
), ctx
);
389 emit(ARM_MOV_R(ARM_R1
, rn
), ctx
);
392 emit(ARM_MOV_R(tmp
[1], ARM_R0
), ctx
);
393 emit(ARM_MOV_R(ARM_R0
, rm
), ctx
);
396 /* Call appropriate function */
397 emit_mov_i(ARM_IP
, op
== BPF_DIV
?
398 (u32
)jit_udiv32
: (u32
)jit_mod32
, ctx
);
399 emit_blx_r(ARM_IP
, ctx
);
401 /* Save return value */
403 emit(ARM_MOV_R(rd
, ARM_R0
), ctx
);
405 /* Restore ARM_R0 and ARM_R1 */
407 emit(ARM_MOV_R(ARM_R1
, tmp
[0]), ctx
);
409 emit(ARM_MOV_R(ARM_R0
, tmp
[1]), ctx
);
412 /* Checks whether BPF register is on scratch stack space or not. */
413 static inline bool is_on_stack(u8 bpf_reg
)
415 static u8 stack_regs
[] = {BPF_REG_AX
, BPF_REG_3
, BPF_REG_4
, BPF_REG_5
,
416 BPF_REG_7
, BPF_REG_8
, BPF_REG_9
, TCALL_CNT
,
417 BPF_REG_2
, BPF_REG_FP
};
418 int i
, reg_len
= sizeof(stack_regs
);
420 for (i
= 0 ; i
< reg_len
; i
++) {
421 if (bpf_reg
== stack_regs
[i
])
427 static inline void emit_a32_mov_i(const u8 dst
, const u32 val
,
428 bool dstk
, struct jit_ctx
*ctx
)
430 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
433 emit_mov_i(tmp
[1], val
, ctx
);
434 emit(ARM_STR_I(tmp
[1], ARM_SP
, STACK_VAR(dst
)), ctx
);
436 emit_mov_i(dst
, val
, ctx
);
440 /* Sign extended move */
441 static inline void emit_a32_mov_i64(const bool is64
, const u8 dst
[],
442 const u32 val
, bool dstk
,
443 struct jit_ctx
*ctx
) {
446 if (is64
&& (val
& (1<<31)))
448 emit_a32_mov_i(dst_lo
, val
, dstk
, ctx
);
449 emit_a32_mov_i(dst_hi
, hi
, dstk
, ctx
);
452 static inline void emit_a32_add_r(const u8 dst
, const u8 src
,
453 const bool is64
, const bool hi
,
454 struct jit_ctx
*ctx
) {
456 * adds dst_lo, dst_lo, src_lo
457 * adc dst_hi, dst_hi, src_hi
459 * add dst_lo, dst_lo, src_lo
462 emit(ARM_ADDS_R(dst
, dst
, src
), ctx
);
464 emit(ARM_ADC_R(dst
, dst
, src
), ctx
);
466 emit(ARM_ADD_R(dst
, dst
, src
), ctx
);
469 static inline void emit_a32_sub_r(const u8 dst
, const u8 src
,
470 const bool is64
, const bool hi
,
471 struct jit_ctx
*ctx
) {
473 * subs dst_lo, dst_lo, src_lo
474 * sbc dst_hi, dst_hi, src_hi
476 * sub dst_lo, dst_lo, src_lo
479 emit(ARM_SUBS_R(dst
, dst
, src
), ctx
);
481 emit(ARM_SBC_R(dst
, dst
, src
), ctx
);
483 emit(ARM_SUB_R(dst
, dst
, src
), ctx
);
486 static inline void emit_alu_r(const u8 dst
, const u8 src
, const bool is64
,
487 const bool hi
, const u8 op
, struct jit_ctx
*ctx
){
488 switch (BPF_OP(op
)) {
489 /* dst = dst + src */
491 emit_a32_add_r(dst
, src
, is64
, hi
, ctx
);
493 /* dst = dst - src */
495 emit_a32_sub_r(dst
, src
, is64
, hi
, ctx
);
497 /* dst = dst | src */
499 emit(ARM_ORR_R(dst
, dst
, src
), ctx
);
501 /* dst = dst & src */
503 emit(ARM_AND_R(dst
, dst
, src
), ctx
);
505 /* dst = dst ^ src */
507 emit(ARM_EOR_R(dst
, dst
, src
), ctx
);
509 /* dst = dst * src */
511 emit(ARM_MUL(dst
, dst
, src
), ctx
);
513 /* dst = dst << src */
515 emit(ARM_LSL_R(dst
, dst
, src
), ctx
);
517 /* dst = dst >> src */
519 emit(ARM_LSR_R(dst
, dst
, src
), ctx
);
521 /* dst = dst >> src (signed)*/
523 emit(ARM_MOV_SR(dst
, dst
, SRTYPE_ASR
, src
), ctx
);
528 /* ALU operation (32 bit)
531 static inline void emit_a32_alu_r(const u8 dst
, const u8 src
,
532 bool dstk
, bool sstk
,
533 struct jit_ctx
*ctx
, const bool is64
,
534 const bool hi
, const u8 op
) {
535 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
536 u8 rn
= sstk
? tmp
[1] : src
;
539 emit(ARM_LDR_I(rn
, ARM_SP
, STACK_VAR(src
)), ctx
);
543 emit(ARM_LDR_I(tmp
[0], ARM_SP
, STACK_VAR(dst
)), ctx
);
544 emit_alu_r(tmp
[0], rn
, is64
, hi
, op
, ctx
);
545 emit(ARM_STR_I(tmp
[0], ARM_SP
, STACK_VAR(dst
)), ctx
);
547 emit_alu_r(dst
, rn
, is64
, hi
, op
, ctx
);
551 /* ALU operation (64 bit) */
552 static inline void emit_a32_alu_r64(const bool is64
, const u8 dst
[],
553 const u8 src
[], bool dstk
,
554 bool sstk
, struct jit_ctx
*ctx
,
556 emit_a32_alu_r(dst_lo
, src_lo
, dstk
, sstk
, ctx
, is64
, false, op
);
558 emit_a32_alu_r(dst_hi
, src_hi
, dstk
, sstk
, ctx
, is64
, true, op
);
560 emit_a32_mov_i(dst_hi
, 0, dstk
, ctx
);
563 /* dst = imm (4 bytes)*/
564 static inline void emit_a32_mov_r(const u8 dst
, const u8 src
,
565 bool dstk
, bool sstk
,
566 struct jit_ctx
*ctx
) {
567 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
568 u8 rt
= sstk
? tmp
[0] : src
;
571 emit(ARM_LDR_I(tmp
[0], ARM_SP
, STACK_VAR(src
)), ctx
);
573 emit(ARM_STR_I(rt
, ARM_SP
, STACK_VAR(dst
)), ctx
);
575 emit(ARM_MOV_R(dst
, rt
), ctx
);
579 static inline void emit_a32_mov_r64(const bool is64
, const u8 dst
[],
580 const u8 src
[], bool dstk
,
581 bool sstk
, struct jit_ctx
*ctx
) {
582 emit_a32_mov_r(dst_lo
, src_lo
, dstk
, sstk
, ctx
);
584 /* complete 8 byte move */
585 emit_a32_mov_r(dst_hi
, src_hi
, dstk
, sstk
, ctx
);
587 /* Zero out high 4 bytes */
588 emit_a32_mov_i(dst_hi
, 0, dstk
, ctx
);
592 /* Shift operations */
593 static inline void emit_a32_alu_i(const u8 dst
, const u32 val
, bool dstk
,
594 struct jit_ctx
*ctx
, const u8 op
) {
595 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
596 u8 rd
= dstk
? tmp
[0] : dst
;
599 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst
)), ctx
);
601 /* Do shift operation */
604 emit(ARM_LSL_I(rd
, rd
, val
), ctx
);
607 emit(ARM_LSR_I(rd
, rd
, val
), ctx
);
610 emit(ARM_RSB_I(rd
, rd
, val
), ctx
);
615 emit(ARM_STR_I(rd
, ARM_SP
, STACK_VAR(dst
)), ctx
);
618 /* dst = ~dst (64 bit) */
619 static inline void emit_a32_neg64(const u8 dst
[], bool dstk
,
620 struct jit_ctx
*ctx
){
621 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
622 u8 rd
= dstk
? tmp
[1] : dst
[1];
623 u8 rm
= dstk
? tmp
[0] : dst
[0];
627 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
628 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
631 /* Do Negate Operation */
632 emit(ARM_RSBS_I(rd
, rd
, 0), ctx
);
633 emit(ARM_RSC_I(rm
, rm
, 0), ctx
);
636 emit(ARM_STR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
637 emit(ARM_STR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
641 /* dst = dst << src */
642 static inline void emit_a32_lsh_r64(const u8 dst
[], const u8 src
[], bool dstk
,
643 bool sstk
, struct jit_ctx
*ctx
) {
644 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
645 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
648 u8 rt
= sstk
? tmp2
[1] : src_lo
;
649 u8 rd
= dstk
? tmp
[1] : dst_lo
;
650 u8 rm
= dstk
? tmp
[0] : dst_hi
;
653 emit(ARM_LDR_I(rt
, ARM_SP
, STACK_VAR(src_lo
)), ctx
);
655 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
656 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
659 /* Do LSH operation */
660 emit(ARM_SUB_I(ARM_IP
, rt
, 32), ctx
);
661 emit(ARM_RSB_I(tmp2
[0], rt
, 32), ctx
);
662 emit(ARM_MOV_SR(ARM_LR
, rm
, SRTYPE_ASL
, rt
), ctx
);
663 emit(ARM_ORR_SR(ARM_LR
, ARM_LR
, rd
, SRTYPE_ASL
, ARM_IP
), ctx
);
664 emit(ARM_ORR_SR(ARM_IP
, ARM_LR
, rd
, SRTYPE_LSR
, tmp2
[0]), ctx
);
665 emit(ARM_MOV_SR(ARM_LR
, rd
, SRTYPE_ASL
, rt
), ctx
);
668 emit(ARM_STR_I(ARM_LR
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
669 emit(ARM_STR_I(ARM_IP
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
671 emit(ARM_MOV_R(rd
, ARM_LR
), ctx
);
672 emit(ARM_MOV_R(rm
, ARM_IP
), ctx
);
676 /* dst = dst >> src (signed)*/
677 static inline void emit_a32_arsh_r64(const u8 dst
[], const u8 src
[], bool dstk
,
678 bool sstk
, struct jit_ctx
*ctx
) {
679 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
680 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
682 u8 rt
= sstk
? tmp2
[1] : src_lo
;
683 u8 rd
= dstk
? tmp
[1] : dst_lo
;
684 u8 rm
= dstk
? tmp
[0] : dst_hi
;
687 emit(ARM_LDR_I(rt
, ARM_SP
, STACK_VAR(src_lo
)), ctx
);
689 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
690 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
693 /* Do the ARSH operation */
694 emit(ARM_RSB_I(ARM_IP
, rt
, 32), ctx
);
695 emit(ARM_SUBS_I(tmp2
[0], rt
, 32), ctx
);
696 emit(ARM_MOV_SR(ARM_LR
, rd
, SRTYPE_LSR
, rt
), ctx
);
697 emit(ARM_ORR_SR(ARM_LR
, ARM_LR
, rm
, SRTYPE_ASL
, ARM_IP
), ctx
);
698 _emit(ARM_COND_MI
, ARM_B(0), ctx
);
699 emit(ARM_ORR_SR(ARM_LR
, ARM_LR
, rm
, SRTYPE_ASR
, tmp2
[0]), ctx
);
700 emit(ARM_MOV_SR(ARM_IP
, rm
, SRTYPE_ASR
, rt
), ctx
);
702 emit(ARM_STR_I(ARM_LR
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
703 emit(ARM_STR_I(ARM_IP
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
705 emit(ARM_MOV_R(rd
, ARM_LR
), ctx
);
706 emit(ARM_MOV_R(rm
, ARM_IP
), ctx
);
710 /* dst = dst >> src */
711 static inline void emit_a32_lsr_r64(const u8 dst
[], const u8 src
[], bool dstk
,
712 bool sstk
, struct jit_ctx
*ctx
) {
713 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
714 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
716 u8 rt
= sstk
? tmp2
[1] : src_lo
;
717 u8 rd
= dstk
? tmp
[1] : dst_lo
;
718 u8 rm
= dstk
? tmp
[0] : dst_hi
;
721 emit(ARM_LDR_I(rt
, ARM_SP
, STACK_VAR(src_lo
)), ctx
);
723 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
724 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
727 /* Do LSH operation */
728 emit(ARM_RSB_I(ARM_IP
, rt
, 32), ctx
);
729 emit(ARM_SUBS_I(tmp2
[0], rt
, 32), ctx
);
730 emit(ARM_MOV_SR(ARM_LR
, rd
, SRTYPE_LSR
, rt
), ctx
);
731 emit(ARM_ORR_SR(ARM_LR
, ARM_LR
, rm
, SRTYPE_ASL
, ARM_IP
), ctx
);
732 emit(ARM_ORR_SR(ARM_LR
, ARM_LR
, rm
, SRTYPE_LSR
, tmp2
[0]), ctx
);
733 emit(ARM_MOV_SR(ARM_IP
, rm
, SRTYPE_LSR
, rt
), ctx
);
735 emit(ARM_STR_I(ARM_LR
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
736 emit(ARM_STR_I(ARM_IP
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
738 emit(ARM_MOV_R(rd
, ARM_LR
), ctx
);
739 emit(ARM_MOV_R(rm
, ARM_IP
), ctx
);
743 /* dst = dst << val */
744 static inline void emit_a32_lsh_i64(const u8 dst
[], bool dstk
,
745 const u32 val
, struct jit_ctx
*ctx
){
746 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
747 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
749 u8 rd
= dstk
? tmp
[1] : dst_lo
;
750 u8 rm
= dstk
? tmp
[0] : dst_hi
;
753 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
754 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
757 /* Do LSH operation */
759 emit(ARM_MOV_SI(tmp2
[0], rm
, SRTYPE_ASL
, val
), ctx
);
760 emit(ARM_ORR_SI(rm
, tmp2
[0], rd
, SRTYPE_LSR
, 32 - val
), ctx
);
761 emit(ARM_MOV_SI(rd
, rd
, SRTYPE_ASL
, val
), ctx
);
764 emit(ARM_MOV_R(rm
, rd
), ctx
);
766 emit(ARM_MOV_SI(rm
, rd
, SRTYPE_ASL
, val
- 32), ctx
);
767 emit(ARM_EOR_R(rd
, rd
, rd
), ctx
);
771 emit(ARM_STR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
772 emit(ARM_STR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
776 /* dst = dst >> val */
777 static inline void emit_a32_lsr_i64(const u8 dst
[], bool dstk
,
778 const u32 val
, struct jit_ctx
*ctx
) {
779 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
780 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
782 u8 rd
= dstk
? tmp
[1] : dst_lo
;
783 u8 rm
= dstk
? tmp
[0] : dst_hi
;
786 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
787 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
790 /* Do LSR operation */
792 emit(ARM_MOV_SI(tmp2
[1], rd
, SRTYPE_LSR
, val
), ctx
);
793 emit(ARM_ORR_SI(rd
, tmp2
[1], rm
, SRTYPE_ASL
, 32 - val
), ctx
);
794 emit(ARM_MOV_SI(rm
, rm
, SRTYPE_LSR
, val
), ctx
);
795 } else if (val
== 32) {
796 emit(ARM_MOV_R(rd
, rm
), ctx
);
797 emit(ARM_MOV_I(rm
, 0), ctx
);
799 emit(ARM_MOV_SI(rd
, rm
, SRTYPE_LSR
, val
- 32), ctx
);
800 emit(ARM_MOV_I(rm
, 0), ctx
);
804 emit(ARM_STR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
805 emit(ARM_STR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
809 /* dst = dst >> val (signed) */
810 static inline void emit_a32_arsh_i64(const u8 dst
[], bool dstk
,
811 const u32 val
, struct jit_ctx
*ctx
){
812 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
813 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
815 u8 rd
= dstk
? tmp
[1] : dst_lo
;
816 u8 rm
= dstk
? tmp
[0] : dst_hi
;
819 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
820 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
823 /* Do ARSH operation */
825 emit(ARM_MOV_SI(tmp2
[1], rd
, SRTYPE_LSR
, val
), ctx
);
826 emit(ARM_ORR_SI(rd
, tmp2
[1], rm
, SRTYPE_ASL
, 32 - val
), ctx
);
827 emit(ARM_MOV_SI(rm
, rm
, SRTYPE_ASR
, val
), ctx
);
828 } else if (val
== 32) {
829 emit(ARM_MOV_R(rd
, rm
), ctx
);
830 emit(ARM_MOV_SI(rm
, rm
, SRTYPE_ASR
, 31), ctx
);
832 emit(ARM_MOV_SI(rd
, rm
, SRTYPE_ASR
, val
- 32), ctx
);
833 emit(ARM_MOV_SI(rm
, rm
, SRTYPE_ASR
, 31), ctx
);
837 emit(ARM_STR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
838 emit(ARM_STR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
842 static inline void emit_a32_mul_r64(const u8 dst
[], const u8 src
[], bool dstk
,
843 bool sstk
, struct jit_ctx
*ctx
) {
844 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
845 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
846 /* Setup operands for multiplication */
847 u8 rd
= dstk
? tmp
[1] : dst_lo
;
848 u8 rm
= dstk
? tmp
[0] : dst_hi
;
849 u8 rt
= sstk
? tmp2
[1] : src_lo
;
850 u8 rn
= sstk
? tmp2
[0] : src_hi
;
853 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
854 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
857 emit(ARM_LDR_I(rt
, ARM_SP
, STACK_VAR(src_lo
)), ctx
);
858 emit(ARM_LDR_I(rn
, ARM_SP
, STACK_VAR(src_hi
)), ctx
);
861 /* Do Multiplication */
862 emit(ARM_MUL(ARM_IP
, rd
, rn
), ctx
);
863 emit(ARM_MUL(ARM_LR
, rm
, rt
), ctx
);
864 emit(ARM_ADD_R(ARM_LR
, ARM_IP
, ARM_LR
), ctx
);
866 emit(ARM_UMULL(ARM_IP
, rm
, rd
, rt
), ctx
);
867 emit(ARM_ADD_R(rm
, ARM_LR
, rm
), ctx
);
869 emit(ARM_STR_I(ARM_IP
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
870 emit(ARM_STR_I(rm
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
872 emit(ARM_MOV_R(rd
, ARM_IP
), ctx
);
876 /* *(size *)(dst + off) = src */
877 static inline void emit_str_r(const u8 dst
, const u8 src
, bool dstk
,
878 const s32 off
, struct jit_ctx
*ctx
, const u8 sz
){
879 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
880 u8 rd
= dstk
? tmp
[1] : dst
;
883 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst
)), ctx
);
885 emit_a32_mov_i(tmp
[0], off
, false, ctx
);
886 emit(ARM_ADD_R(tmp
[0], rd
, tmp
[0]), ctx
);
892 emit(ARM_STR_I(src
, rd
, 0), ctx
);
895 /* Store a HalfWord */
896 emit(ARM_STRH_I(src
, rd
, 0), ctx
);
900 emit(ARM_STRB_I(src
, rd
, 0), ctx
);
905 /* dst = *(size*)(src + off) */
906 static inline void emit_ldx_r(const u8 dst
[], const u8 src
, bool dstk
,
907 s32 off
, struct jit_ctx
*ctx
, const u8 sz
){
908 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
909 const u8
*rd
= dstk
? tmp
: dst
;
918 if (off
< 0 || off
> off_max
) {
919 emit_a32_mov_i(tmp
[0], off
, false, ctx
);
920 emit(ARM_ADD_R(tmp
[0], tmp
[0], src
), ctx
);
923 } else if (rd
[1] == rm
) {
924 emit(ARM_MOV_R(tmp
[0], rm
), ctx
);
930 emit(ARM_LDRB_I(rd
[1], rm
, off
), ctx
);
931 emit_a32_mov_i(dst
[0], 0, dstk
, ctx
);
934 /* Load a HalfWord */
935 emit(ARM_LDRH_I(rd
[1], rm
, off
), ctx
);
936 emit_a32_mov_i(dst
[0], 0, dstk
, ctx
);
940 emit(ARM_LDR_I(rd
[1], rm
, off
), ctx
);
941 emit_a32_mov_i(dst
[0], 0, dstk
, ctx
);
944 /* Load a Double Word */
945 emit(ARM_LDR_I(rd
[1], rm
, off
), ctx
);
946 emit(ARM_LDR_I(rd
[0], rm
, off
+ 4), ctx
);
950 emit(ARM_STR_I(rd
[1], ARM_SP
, STACK_VAR(dst
[1])), ctx
);
951 if (dstk
&& sz
== BPF_DW
)
952 emit(ARM_STR_I(rd
[0], ARM_SP
, STACK_VAR(dst
[0])), ctx
);
955 /* Arithmatic Operation */
956 static inline void emit_ar_r(const u8 rd
, const u8 rt
, const u8 rm
,
957 const u8 rn
, struct jit_ctx
*ctx
, u8 op
) {
960 emit(ARM_AND_R(ARM_IP
, rt
, rn
), ctx
);
961 emit(ARM_AND_R(ARM_LR
, rd
, rm
), ctx
);
962 emit(ARM_ORRS_R(ARM_IP
, ARM_LR
, ARM_IP
), ctx
);
970 emit(ARM_CMP_R(rd
, rm
), ctx
);
971 _emit(ARM_COND_EQ
, ARM_CMP_R(rt
, rn
), ctx
);
975 emit(ARM_CMP_R(rn
, rt
), ctx
);
976 emit(ARM_SBCS_R(ARM_IP
, rm
, rd
), ctx
);
980 emit(ARM_CMP_R(rt
, rn
), ctx
);
981 emit(ARM_SBCS_R(ARM_IP
, rd
, rm
), ctx
);
986 static int out_offset
= -1; /* initialized on the first pass of build_body() */
987 static int emit_bpf_tail_call(struct jit_ctx
*ctx
)
990 /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
991 const u8
*r2
= bpf2a32
[BPF_REG_2
];
992 const u8
*r3
= bpf2a32
[BPF_REG_3
];
993 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
994 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
995 const u8
*tcc
= bpf2a32
[TCALL_CNT
];
996 const int idx0
= ctx
->idx
;
997 #define cur_offset (ctx->idx - idx0)
998 #define jmp_offset (out_offset - (cur_offset) - 2)
1001 /* if (index >= array->map.max_entries)
1004 off
= offsetof(struct bpf_array
, map
.max_entries
);
1005 /* array->map.max_entries */
1006 emit_a32_mov_i(tmp
[1], off
, false, ctx
);
1007 emit(ARM_LDR_I(tmp2
[1], ARM_SP
, STACK_VAR(r2
[1])), ctx
);
1008 emit(ARM_LDR_R(tmp
[1], tmp2
[1], tmp
[1]), ctx
);
1009 /* index is 32-bit for arrays */
1010 emit(ARM_LDR_I(tmp2
[1], ARM_SP
, STACK_VAR(r3
[1])), ctx
);
1011 /* index >= array->map.max_entries */
1012 emit(ARM_CMP_R(tmp2
[1], tmp
[1]), ctx
);
1013 _emit(ARM_COND_CS
, ARM_B(jmp_offset
), ctx
);
1015 /* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
1019 lo
= (u32
)MAX_TAIL_CALL_CNT
;
1020 hi
= (u32
)((u64
)MAX_TAIL_CALL_CNT
>> 32);
1021 emit(ARM_LDR_I(tmp
[1], ARM_SP
, STACK_VAR(tcc
[1])), ctx
);
1022 emit(ARM_LDR_I(tmp
[0], ARM_SP
, STACK_VAR(tcc
[0])), ctx
);
1023 emit(ARM_CMP_I(tmp
[0], hi
), ctx
);
1024 _emit(ARM_COND_EQ
, ARM_CMP_I(tmp
[1], lo
), ctx
);
1025 _emit(ARM_COND_HI
, ARM_B(jmp_offset
), ctx
);
1026 emit(ARM_ADDS_I(tmp
[1], tmp
[1], 1), ctx
);
1027 emit(ARM_ADC_I(tmp
[0], tmp
[0], 0), ctx
);
1028 emit(ARM_STR_I(tmp
[1], ARM_SP
, STACK_VAR(tcc
[1])), ctx
);
1029 emit(ARM_STR_I(tmp
[0], ARM_SP
, STACK_VAR(tcc
[0])), ctx
);
1031 /* prog = array->ptrs[index]
1035 off
= offsetof(struct bpf_array
, ptrs
);
1036 emit_a32_mov_i(tmp
[1], off
, false, ctx
);
1037 emit(ARM_LDR_I(tmp2
[1], ARM_SP
, STACK_VAR(r2
[1])), ctx
);
1038 emit(ARM_ADD_R(tmp
[1], tmp2
[1], tmp
[1]), ctx
);
1039 emit(ARM_LDR_I(tmp2
[1], ARM_SP
, STACK_VAR(r3
[1])), ctx
);
1040 emit(ARM_MOV_SI(tmp
[0], tmp2
[1], SRTYPE_ASL
, 2), ctx
);
1041 emit(ARM_LDR_R(tmp
[1], tmp
[1], tmp
[0]), ctx
);
1042 emit(ARM_CMP_I(tmp
[1], 0), ctx
);
1043 _emit(ARM_COND_EQ
, ARM_B(jmp_offset
), ctx
);
1045 /* goto *(prog->bpf_func + prologue_size); */
1046 off
= offsetof(struct bpf_prog
, bpf_func
);
1047 emit_a32_mov_i(tmp2
[1], off
, false, ctx
);
1048 emit(ARM_LDR_R(tmp
[1], tmp
[1], tmp2
[1]), ctx
);
1049 emit(ARM_ADD_I(tmp
[1], tmp
[1], ctx
->prologue_bytes
), ctx
);
1050 emit_bx_r(tmp
[1], ctx
);
1053 if (out_offset
== -1)
1054 out_offset
= cur_offset
;
1055 if (cur_offset
!= out_offset
) {
1056 pr_err_once("tail_call out_offset = %d, expected %d!\n",
1057 cur_offset
, out_offset
);
1065 /* 0xabcd => 0xcdab */
1066 static inline void emit_rev16(const u8 rd
, const u8 rn
, struct jit_ctx
*ctx
)
1068 #if __LINUX_ARM_ARCH__ < 6
1069 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
1071 emit(ARM_AND_I(tmp2
[1], rn
, 0xff), ctx
);
1072 emit(ARM_MOV_SI(tmp2
[0], rn
, SRTYPE_LSR
, 8), ctx
);
1073 emit(ARM_AND_I(tmp2
[0], tmp2
[0], 0xff), ctx
);
1074 emit(ARM_ORR_SI(rd
, tmp2
[0], tmp2
[1], SRTYPE_LSL
, 8), ctx
);
1076 emit(ARM_REV16(rd
, rn
), ctx
);
1080 /* 0xabcdefgh => 0xghefcdab */
1081 static inline void emit_rev32(const u8 rd
, const u8 rn
, struct jit_ctx
*ctx
)
1083 #if __LINUX_ARM_ARCH__ < 6
1084 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
1086 emit(ARM_AND_I(tmp2
[1], rn
, 0xff), ctx
);
1087 emit(ARM_MOV_SI(tmp2
[0], rn
, SRTYPE_LSR
, 24), ctx
);
1088 emit(ARM_ORR_SI(ARM_IP
, tmp2
[0], tmp2
[1], SRTYPE_LSL
, 24), ctx
);
1090 emit(ARM_MOV_SI(tmp2
[1], rn
, SRTYPE_LSR
, 8), ctx
);
1091 emit(ARM_AND_I(tmp2
[1], tmp2
[1], 0xff), ctx
);
1092 emit(ARM_MOV_SI(tmp2
[0], rn
, SRTYPE_LSR
, 16), ctx
);
1093 emit(ARM_AND_I(tmp2
[0], tmp2
[0], 0xff), ctx
);
1094 emit(ARM_MOV_SI(tmp2
[0], tmp2
[0], SRTYPE_LSL
, 8), ctx
);
1095 emit(ARM_ORR_SI(tmp2
[0], tmp2
[0], tmp2
[1], SRTYPE_LSL
, 16), ctx
);
1096 emit(ARM_ORR_R(rd
, ARM_IP
, tmp2
[0]), ctx
);
1099 emit(ARM_REV(rd
, rn
), ctx
);
1103 // push the scratch stack register on top of the stack
1104 static inline void emit_push_r64(const u8 src
[], const u8 shift
,
1105 struct jit_ctx
*ctx
)
1107 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
1110 emit(ARM_LDR_I(tmp2
[1], ARM_SP
, STACK_VAR(src
[1]+shift
)), ctx
);
1111 emit(ARM_LDR_I(tmp2
[0], ARM_SP
, STACK_VAR(src
[0]+shift
)), ctx
);
1113 reg_set
= (1 << tmp2
[1]) | (1 << tmp2
[0]);
1114 emit(ARM_PUSH(reg_set
), ctx
);
1117 static void build_prologue(struct jit_ctx
*ctx
)
1119 const u8 r0
= bpf2a32
[BPF_REG_0
][1];
1120 const u8 r2
= bpf2a32
[BPF_REG_1
][1];
1121 const u8 r3
= bpf2a32
[BPF_REG_1
][0];
1122 const u8 r4
= bpf2a32
[BPF_REG_6
][1];
1123 const u8 fplo
= bpf2a32
[BPF_REG_FP
][1];
1124 const u8 fphi
= bpf2a32
[BPF_REG_FP
][0];
1125 const u8
*tcc
= bpf2a32
[TCALL_CNT
];
1127 /* Save callee saved registers. */
1128 #ifdef CONFIG_FRAME_POINTER
1129 u16 reg_set
= CALLEE_PUSH_MASK
| 1 << ARM_IP
| 1 << ARM_PC
;
1130 emit(ARM_MOV_R(ARM_IP
, ARM_SP
), ctx
);
1131 emit(ARM_PUSH(reg_set
), ctx
);
1132 emit(ARM_SUB_I(ARM_FP
, ARM_IP
, 4), ctx
);
1134 emit(ARM_PUSH(CALLEE_PUSH_MASK
), ctx
);
1135 emit(ARM_MOV_R(ARM_FP
, ARM_SP
), ctx
);
1137 /* Save frame pointer for later */
1138 emit(ARM_SUB_I(ARM_IP
, ARM_SP
, SCRATCH_SIZE
), ctx
);
1140 ctx
->stack_size
= imm8m(STACK_SIZE
);
1142 /* Set up function call stack */
1143 emit(ARM_SUB_I(ARM_SP
, ARM_SP
, ctx
->stack_size
), ctx
);
1145 /* Set up BPF prog stack base register */
1146 emit_a32_mov_r(fplo
, ARM_IP
, true, false, ctx
);
1147 emit_a32_mov_i(fphi
, 0, true, ctx
);
1150 emit(ARM_MOV_I(r4
, 0), ctx
);
1152 /* Move BPF_CTX to BPF_R1 */
1153 emit(ARM_MOV_R(r3
, r4
), ctx
);
1154 emit(ARM_MOV_R(r2
, r0
), ctx
);
1155 /* Initialize Tail Count */
1156 emit(ARM_STR_I(r4
, ARM_SP
, STACK_VAR(tcc
[0])), ctx
);
1157 emit(ARM_STR_I(r4
, ARM_SP
, STACK_VAR(tcc
[1])), ctx
);
1158 /* end of prologue */
1161 /* restore callee saved registers. */
1162 static void build_epilogue(struct jit_ctx
*ctx
)
1164 #ifdef CONFIG_FRAME_POINTER
1165 /* When using frame pointers, some additional registers need to
1167 u16 reg_set
= CALLEE_POP_MASK
| 1 << ARM_SP
;
1168 emit(ARM_SUB_I(ARM_SP
, ARM_FP
, hweight16(reg_set
) * 4), ctx
);
1169 emit(ARM_LDM(ARM_SP
, reg_set
), ctx
);
1171 /* Restore callee saved registers. */
1172 emit(ARM_MOV_R(ARM_SP
, ARM_FP
), ctx
);
1173 emit(ARM_POP(CALLEE_POP_MASK
), ctx
);
1178 * Convert an eBPF instruction to native instruction, i.e
1179 * JITs an eBPF instruction.
1181 * 0 - Successfully JITed an 8-byte eBPF instruction
1182 * >0 - Successfully JITed a 16-byte eBPF instruction
1183 * <0 - Failed to JIT.
1185 static int build_insn(const struct bpf_insn
*insn
, struct jit_ctx
*ctx
)
1187 const u8 code
= insn
->code
;
1188 const u8
*dst
= bpf2a32
[insn
->dst_reg
];
1189 const u8
*src
= bpf2a32
[insn
->src_reg
];
1190 const u8
*tmp
= bpf2a32
[TMP_REG_1
];
1191 const u8
*tmp2
= bpf2a32
[TMP_REG_2
];
1192 const s16 off
= insn
->off
;
1193 const s32 imm
= insn
->imm
;
1194 const int i
= insn
- ctx
->prog
->insnsi
;
1195 const bool is64
= BPF_CLASS(code
) == BPF_ALU64
;
1196 const bool dstk
= is_on_stack(insn
->dst_reg
);
1197 const bool sstk
= is_on_stack(insn
->src_reg
);
1201 #define check_imm(bits, imm) do { \
1202 if ((((imm) > 0) && ((imm) >> (bits))) || \
1203 (((imm) < 0) && (~(imm) >> (bits)))) { \
1204 pr_info("[%2d] imm=%d(0x%x) out of range\n", \
1209 #define check_imm24(imm) check_imm(24, imm)
1212 /* ALU operations */
1215 case BPF_ALU
| BPF_MOV
| BPF_K
:
1216 case BPF_ALU
| BPF_MOV
| BPF_X
:
1217 case BPF_ALU64
| BPF_MOV
| BPF_K
:
1218 case BPF_ALU64
| BPF_MOV
| BPF_X
:
1219 switch (BPF_SRC(code
)) {
1221 emit_a32_mov_r64(is64
, dst
, src
, dstk
, sstk
, ctx
);
1224 /* Sign-extend immediate value to destination reg */
1225 emit_a32_mov_i64(is64
, dst
, imm
, dstk
, ctx
);
1229 /* dst = dst + src/imm */
1230 /* dst = dst - src/imm */
1231 /* dst = dst | src/imm */
1232 /* dst = dst & src/imm */
1233 /* dst = dst ^ src/imm */
1234 /* dst = dst * src/imm */
1235 /* dst = dst << src */
1236 /* dst = dst >> src */
1237 case BPF_ALU
| BPF_ADD
| BPF_K
:
1238 case BPF_ALU
| BPF_ADD
| BPF_X
:
1239 case BPF_ALU
| BPF_SUB
| BPF_K
:
1240 case BPF_ALU
| BPF_SUB
| BPF_X
:
1241 case BPF_ALU
| BPF_OR
| BPF_K
:
1242 case BPF_ALU
| BPF_OR
| BPF_X
:
1243 case BPF_ALU
| BPF_AND
| BPF_K
:
1244 case BPF_ALU
| BPF_AND
| BPF_X
:
1245 case BPF_ALU
| BPF_XOR
| BPF_K
:
1246 case BPF_ALU
| BPF_XOR
| BPF_X
:
1247 case BPF_ALU
| BPF_MUL
| BPF_K
:
1248 case BPF_ALU
| BPF_MUL
| BPF_X
:
1249 case BPF_ALU
| BPF_LSH
| BPF_X
:
1250 case BPF_ALU
| BPF_RSH
| BPF_X
:
1251 case BPF_ALU
| BPF_ARSH
| BPF_K
:
1252 case BPF_ALU
| BPF_ARSH
| BPF_X
:
1253 case BPF_ALU64
| BPF_ADD
| BPF_K
:
1254 case BPF_ALU64
| BPF_ADD
| BPF_X
:
1255 case BPF_ALU64
| BPF_SUB
| BPF_K
:
1256 case BPF_ALU64
| BPF_SUB
| BPF_X
:
1257 case BPF_ALU64
| BPF_OR
| BPF_K
:
1258 case BPF_ALU64
| BPF_OR
| BPF_X
:
1259 case BPF_ALU64
| BPF_AND
| BPF_K
:
1260 case BPF_ALU64
| BPF_AND
| BPF_X
:
1261 case BPF_ALU64
| BPF_XOR
| BPF_K
:
1262 case BPF_ALU64
| BPF_XOR
| BPF_X
:
1263 switch (BPF_SRC(code
)) {
1265 emit_a32_alu_r64(is64
, dst
, src
, dstk
, sstk
,
1269 /* Move immediate value to the temporary register
1270 * and then do the ALU operation on the temporary
1271 * register as this will sign-extend the immediate
1272 * value into temporary reg and then it would be
1273 * safe to do the operation on it.
1275 emit_a32_mov_i64(is64
, tmp2
, imm
, false, ctx
);
1276 emit_a32_alu_r64(is64
, dst
, tmp2
, dstk
, false,
1281 /* dst = dst / src(imm) */
1282 /* dst = dst % src(imm) */
1283 case BPF_ALU
| BPF_DIV
| BPF_K
:
1284 case BPF_ALU
| BPF_DIV
| BPF_X
:
1285 case BPF_ALU
| BPF_MOD
| BPF_K
:
1286 case BPF_ALU
| BPF_MOD
| BPF_X
:
1288 rd
= dstk
? tmp2
[1] : dst_lo
;
1290 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
1291 switch (BPF_SRC(code
)) {
1293 rt
= sstk
? tmp2
[0] : rt
;
1295 emit(ARM_LDR_I(rt
, ARM_SP
, STACK_VAR(src_lo
)),
1300 emit_a32_mov_i(rt
, imm
, false, ctx
);
1303 emit_udivmod(rd
, rd
, rt
, ctx
, BPF_OP(code
));
1305 emit(ARM_STR_I(rd
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
1306 emit_a32_mov_i(dst_hi
, 0, dstk
, ctx
);
1308 case BPF_ALU64
| BPF_DIV
| BPF_K
:
1309 case BPF_ALU64
| BPF_DIV
| BPF_X
:
1310 case BPF_ALU64
| BPF_MOD
| BPF_K
:
1311 case BPF_ALU64
| BPF_MOD
| BPF_X
:
1313 /* dst = dst >> imm */
1314 /* dst = dst << imm */
1315 case BPF_ALU
| BPF_RSH
| BPF_K
:
1316 case BPF_ALU
| BPF_LSH
| BPF_K
:
1317 if (unlikely(imm
> 31))
1320 emit_a32_alu_i(dst_lo
, imm
, dstk
, ctx
, BPF_OP(code
));
1321 emit_a32_mov_i(dst_hi
, 0, dstk
, ctx
);
1323 /* dst = dst << imm */
1324 case BPF_ALU64
| BPF_LSH
| BPF_K
:
1325 if (unlikely(imm
> 63))
1327 emit_a32_lsh_i64(dst
, dstk
, imm
, ctx
);
1329 /* dst = dst >> imm */
1330 case BPF_ALU64
| BPF_RSH
| BPF_K
:
1331 if (unlikely(imm
> 63))
1333 emit_a32_lsr_i64(dst
, dstk
, imm
, ctx
);
1335 /* dst = dst << src */
1336 case BPF_ALU64
| BPF_LSH
| BPF_X
:
1337 emit_a32_lsh_r64(dst
, src
, dstk
, sstk
, ctx
);
1339 /* dst = dst >> src */
1340 case BPF_ALU64
| BPF_RSH
| BPF_X
:
1341 emit_a32_lsr_r64(dst
, src
, dstk
, sstk
, ctx
);
1343 /* dst = dst >> src (signed) */
1344 case BPF_ALU64
| BPF_ARSH
| BPF_X
:
1345 emit_a32_arsh_r64(dst
, src
, dstk
, sstk
, ctx
);
1347 /* dst = dst >> imm (signed) */
1348 case BPF_ALU64
| BPF_ARSH
| BPF_K
:
1349 if (unlikely(imm
> 63))
1351 emit_a32_arsh_i64(dst
, dstk
, imm
, ctx
);
1354 case BPF_ALU
| BPF_NEG
:
1355 emit_a32_alu_i(dst_lo
, 0, dstk
, ctx
, BPF_OP(code
));
1356 emit_a32_mov_i(dst_hi
, 0, dstk
, ctx
);
1358 /* dst = ~dst (64 bit) */
1359 case BPF_ALU64
| BPF_NEG
:
1360 emit_a32_neg64(dst
, dstk
, ctx
);
1362 /* dst = dst * src/imm */
1363 case BPF_ALU64
| BPF_MUL
| BPF_X
:
1364 case BPF_ALU64
| BPF_MUL
| BPF_K
:
1365 switch (BPF_SRC(code
)) {
1367 emit_a32_mul_r64(dst
, src
, dstk
, sstk
, ctx
);
1370 /* Move immediate value to the temporary register
1371 * and then do the multiplication on it as this
1372 * will sign-extend the immediate value into temp
1373 * reg then it would be safe to do the operation
1376 emit_a32_mov_i64(is64
, tmp2
, imm
, false, ctx
);
1377 emit_a32_mul_r64(dst
, tmp2
, dstk
, false, ctx
);
1381 /* dst = htole(dst) */
1382 /* dst = htobe(dst) */
1383 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
1384 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
1385 rd
= dstk
? tmp
[0] : dst_hi
;
1386 rt
= dstk
? tmp
[1] : dst_lo
;
1388 emit(ARM_LDR_I(rt
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
1389 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
1391 if (BPF_SRC(code
) == BPF_FROM_LE
)
1392 goto emit_bswap_uxt
;
1395 emit_rev16(rt
, rt
, ctx
);
1396 goto emit_bswap_uxt
;
1398 emit_rev32(rt
, rt
, ctx
);
1399 goto emit_bswap_uxt
;
1401 emit_rev32(ARM_LR
, rt
, ctx
);
1402 emit_rev32(rt
, rd
, ctx
);
1403 emit(ARM_MOV_R(rd
, ARM_LR
), ctx
);
1410 /* zero-extend 16 bits into 64 bits */
1411 #if __LINUX_ARM_ARCH__ < 6
1412 emit_a32_mov_i(tmp2
[1], 0xffff, false, ctx
);
1413 emit(ARM_AND_R(rt
, rt
, tmp2
[1]), ctx
);
1415 emit(ARM_UXTH(rt
, rt
), ctx
);
1417 emit(ARM_EOR_R(rd
, rd
, rd
), ctx
);
1420 /* zero-extend 32 bits into 64 bits */
1421 emit(ARM_EOR_R(rd
, rd
, rd
), ctx
);
1429 emit(ARM_STR_I(rt
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
1430 emit(ARM_STR_I(rd
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
1434 case BPF_LD
| BPF_IMM
| BPF_DW
:
1436 const struct bpf_insn insn1
= insn
[1];
1440 emit_a32_mov_i(dst_lo
, lo
, dstk
, ctx
);
1441 emit_a32_mov_i(dst_hi
, hi
, dstk
, ctx
);
1445 /* LDX: dst = *(size *)(src + off) */
1446 case BPF_LDX
| BPF_MEM
| BPF_W
:
1447 case BPF_LDX
| BPF_MEM
| BPF_H
:
1448 case BPF_LDX
| BPF_MEM
| BPF_B
:
1449 case BPF_LDX
| BPF_MEM
| BPF_DW
:
1450 rn
= sstk
? tmp2
[1] : src_lo
;
1452 emit(ARM_LDR_I(rn
, ARM_SP
, STACK_VAR(src_lo
)), ctx
);
1453 emit_ldx_r(dst
, rn
, dstk
, off
, ctx
, BPF_SIZE(code
));
1455 /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */
1456 case BPF_LD
| BPF_ABS
| BPF_W
:
1457 case BPF_LD
| BPF_ABS
| BPF_H
:
1458 case BPF_LD
| BPF_ABS
| BPF_B
:
1459 /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */
1460 case BPF_LD
| BPF_IND
| BPF_W
:
1461 case BPF_LD
| BPF_IND
| BPF_H
:
1462 case BPF_LD
| BPF_IND
| BPF_B
:
1464 const u8 r4
= bpf2a32
[BPF_REG_6
][1]; /* r4 = ptr to sk_buff */
1465 const u8 r0
= bpf2a32
[BPF_REG_0
][1]; /*r0: struct sk_buff *skb*/
1467 const u8 r1
= bpf2a32
[BPF_REG_0
][0]; /* r1: int k */
1468 const u8 r2
= bpf2a32
[BPF_REG_1
][1]; /* r2: unsigned int size */
1469 const u8 r3
= bpf2a32
[BPF_REG_1
][0]; /* r3: void *buffer */
1470 const u8 r6
= bpf2a32
[TMP_REG_1
][1]; /* r6: void *(*func)(..) */
1473 /* Setting up first argument */
1474 emit(ARM_MOV_R(r0
, r4
), ctx
);
1476 /* Setting up second argument */
1477 emit_a32_mov_i(r1
, imm
, false, ctx
);
1478 if (BPF_MODE(code
) == BPF_IND
)
1479 emit_a32_alu_r(r1
, src_lo
, false, sstk
, ctx
,
1480 false, false, BPF_ADD
);
1482 /* Setting up third argument */
1483 switch (BPF_SIZE(code
)) {
1496 emit_a32_mov_i(r2
, size
, false, ctx
);
1498 /* Setting up fourth argument */
1499 emit(ARM_ADD_I(r3
, ARM_SP
, imm8m(SKB_BUFFER
)), ctx
);
1501 /* Setting up function pointer to call */
1502 emit_a32_mov_i(r6
, (unsigned int)bpf_load_pointer
, false, ctx
);
1503 emit_blx_r(r6
, ctx
);
1505 emit(ARM_EOR_R(r1
, r1
, r1
), ctx
);
1506 /* Check if return address is NULL or not.
1507 * if NULL then jump to epilogue
1508 * else continue to load the value from retn address
1510 emit(ARM_CMP_I(r0
, 0), ctx
);
1511 jmp_offset
= epilogue_offset(ctx
);
1512 check_imm24(jmp_offset
);
1513 _emit(ARM_COND_EQ
, ARM_B(jmp_offset
), ctx
);
1515 /* Load value from the address */
1516 switch (BPF_SIZE(code
)) {
1518 emit(ARM_LDR_I(r0
, r0
, 0), ctx
);
1519 emit_rev32(r0
, r0
, ctx
);
1522 emit(ARM_LDRH_I(r0
, r0
, 0), ctx
);
1523 emit_rev16(r0
, r0
, ctx
);
1526 emit(ARM_LDRB_I(r0
, r0
, 0), ctx
);
1527 /* No need to reverse */
1532 /* ST: *(size *)(dst + off) = imm */
1533 case BPF_ST
| BPF_MEM
| BPF_W
:
1534 case BPF_ST
| BPF_MEM
| BPF_H
:
1535 case BPF_ST
| BPF_MEM
| BPF_B
:
1536 case BPF_ST
| BPF_MEM
| BPF_DW
:
1537 switch (BPF_SIZE(code
)) {
1539 /* Sign-extend immediate value into temp reg */
1540 emit_a32_mov_i64(true, tmp2
, imm
, false, ctx
);
1541 emit_str_r(dst_lo
, tmp2
[1], dstk
, off
, ctx
, BPF_W
);
1542 emit_str_r(dst_lo
, tmp2
[0], dstk
, off
+4, ctx
, BPF_W
);
1547 emit_a32_mov_i(tmp2
[1], imm
, false, ctx
);
1548 emit_str_r(dst_lo
, tmp2
[1], dstk
, off
, ctx
,
1553 /* STX XADD: lock *(u32 *)(dst + off) += src */
1554 case BPF_STX
| BPF_XADD
| BPF_W
:
1555 /* STX XADD: lock *(u64 *)(dst + off) += src */
1556 case BPF_STX
| BPF_XADD
| BPF_DW
:
1558 /* STX: *(size *)(dst + off) = src */
1559 case BPF_STX
| BPF_MEM
| BPF_W
:
1560 case BPF_STX
| BPF_MEM
| BPF_H
:
1561 case BPF_STX
| BPF_MEM
| BPF_B
:
1562 case BPF_STX
| BPF_MEM
| BPF_DW
:
1564 u8 sz
= BPF_SIZE(code
);
1566 rn
= sstk
? tmp2
[1] : src_lo
;
1567 rm
= sstk
? tmp2
[0] : src_hi
;
1569 emit(ARM_LDR_I(rn
, ARM_SP
, STACK_VAR(src_lo
)), ctx
);
1570 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(src_hi
)), ctx
);
1573 /* Store the value */
1574 if (BPF_SIZE(code
) == BPF_DW
) {
1575 emit_str_r(dst_lo
, rn
, dstk
, off
, ctx
, BPF_W
);
1576 emit_str_r(dst_lo
, rm
, dstk
, off
+4, ctx
, BPF_W
);
1578 emit_str_r(dst_lo
, rn
, dstk
, off
, ctx
, sz
);
1582 /* PC += off if dst == src */
1583 /* PC += off if dst > src */
1584 /* PC += off if dst >= src */
1585 /* PC += off if dst < src */
1586 /* PC += off if dst <= src */
1587 /* PC += off if dst != src */
1588 /* PC += off if dst > src (signed) */
1589 /* PC += off if dst >= src (signed) */
1590 /* PC += off if dst < src (signed) */
1591 /* PC += off if dst <= src (signed) */
1592 /* PC += off if dst & src */
1593 case BPF_JMP
| BPF_JEQ
| BPF_X
:
1594 case BPF_JMP
| BPF_JGT
| BPF_X
:
1595 case BPF_JMP
| BPF_JGE
| BPF_X
:
1596 case BPF_JMP
| BPF_JNE
| BPF_X
:
1597 case BPF_JMP
| BPF_JSGT
| BPF_X
:
1598 case BPF_JMP
| BPF_JSGE
| BPF_X
:
1599 case BPF_JMP
| BPF_JSET
| BPF_X
:
1600 case BPF_JMP
| BPF_JLE
| BPF_X
:
1601 case BPF_JMP
| BPF_JLT
| BPF_X
:
1602 case BPF_JMP
| BPF_JSLT
| BPF_X
:
1603 case BPF_JMP
| BPF_JSLE
| BPF_X
:
1604 /* Setup source registers */
1605 rm
= sstk
? tmp2
[0] : src_hi
;
1606 rn
= sstk
? tmp2
[1] : src_lo
;
1608 emit(ARM_LDR_I(rn
, ARM_SP
, STACK_VAR(src_lo
)), ctx
);
1609 emit(ARM_LDR_I(rm
, ARM_SP
, STACK_VAR(src_hi
)), ctx
);
1612 /* PC += off if dst == imm */
1613 /* PC += off if dst > imm */
1614 /* PC += off if dst >= imm */
1615 /* PC += off if dst < imm */
1616 /* PC += off if dst <= imm */
1617 /* PC += off if dst != imm */
1618 /* PC += off if dst > imm (signed) */
1619 /* PC += off if dst >= imm (signed) */
1620 /* PC += off if dst < imm (signed) */
1621 /* PC += off if dst <= imm (signed) */
1622 /* PC += off if dst & imm */
1623 case BPF_JMP
| BPF_JEQ
| BPF_K
:
1624 case BPF_JMP
| BPF_JGT
| BPF_K
:
1625 case BPF_JMP
| BPF_JGE
| BPF_K
:
1626 case BPF_JMP
| BPF_JNE
| BPF_K
:
1627 case BPF_JMP
| BPF_JSGT
| BPF_K
:
1628 case BPF_JMP
| BPF_JSGE
| BPF_K
:
1629 case BPF_JMP
| BPF_JSET
| BPF_K
:
1630 case BPF_JMP
| BPF_JLT
| BPF_K
:
1631 case BPF_JMP
| BPF_JLE
| BPF_K
:
1632 case BPF_JMP
| BPF_JSLT
| BPF_K
:
1633 case BPF_JMP
| BPF_JSLE
| BPF_K
:
1638 /* Sign-extend immediate value */
1639 emit_a32_mov_i64(true, tmp2
, imm
, false, ctx
);
1641 /* Setup destination register */
1642 rd
= dstk
? tmp
[0] : dst_hi
;
1643 rt
= dstk
? tmp
[1] : dst_lo
;
1645 emit(ARM_LDR_I(rt
, ARM_SP
, STACK_VAR(dst_lo
)), ctx
);
1646 emit(ARM_LDR_I(rd
, ARM_SP
, STACK_VAR(dst_hi
)), ctx
);
1649 /* Check for the condition */
1650 emit_ar_r(rd
, rt
, rm
, rn
, ctx
, BPF_OP(code
));
1652 /* Setup JUMP instruction */
1653 jmp_offset
= bpf2a32_offset(i
+off
, i
, ctx
);
1654 switch (BPF_OP(code
)) {
1657 _emit(ARM_COND_NE
, ARM_B(jmp_offset
), ctx
);
1660 _emit(ARM_COND_EQ
, ARM_B(jmp_offset
), ctx
);
1663 _emit(ARM_COND_HI
, ARM_B(jmp_offset
), ctx
);
1666 _emit(ARM_COND_CS
, ARM_B(jmp_offset
), ctx
);
1669 _emit(ARM_COND_LT
, ARM_B(jmp_offset
), ctx
);
1672 _emit(ARM_COND_GE
, ARM_B(jmp_offset
), ctx
);
1675 _emit(ARM_COND_LS
, ARM_B(jmp_offset
), ctx
);
1678 _emit(ARM_COND_CC
, ARM_B(jmp_offset
), ctx
);
1681 _emit(ARM_COND_LT
, ARM_B(jmp_offset
), ctx
);
1684 _emit(ARM_COND_GE
, ARM_B(jmp_offset
), ctx
);
1689 case BPF_JMP
| BPF_JA
:
1693 jmp_offset
= bpf2a32_offset(i
+off
, i
, ctx
);
1694 check_imm24(jmp_offset
);
1695 emit(ARM_B(jmp_offset
), ctx
);
1699 case BPF_JMP
| BPF_TAIL_CALL
:
1700 if (emit_bpf_tail_call(ctx
))
1704 case BPF_JMP
| BPF_CALL
:
1706 const u8
*r0
= bpf2a32
[BPF_REG_0
];
1707 const u8
*r1
= bpf2a32
[BPF_REG_1
];
1708 const u8
*r2
= bpf2a32
[BPF_REG_2
];
1709 const u8
*r3
= bpf2a32
[BPF_REG_3
];
1710 const u8
*r4
= bpf2a32
[BPF_REG_4
];
1711 const u8
*r5
= bpf2a32
[BPF_REG_5
];
1712 const u32 func
= (u32
)__bpf_call_base
+ (u32
)imm
;
1714 emit_a32_mov_r64(true, r0
, r1
, false, false, ctx
);
1715 emit_a32_mov_r64(true, r1
, r2
, false, true, ctx
);
1716 emit_push_r64(r5
, 0, ctx
);
1717 emit_push_r64(r4
, 8, ctx
);
1718 emit_push_r64(r3
, 16, ctx
);
1720 emit_a32_mov_i(tmp
[1], func
, false, ctx
);
1721 emit_blx_r(tmp
[1], ctx
);
1723 emit(ARM_ADD_I(ARM_SP
, ARM_SP
, imm8m(24)), ctx
); // callee clean
1726 /* function return */
1727 case BPF_JMP
| BPF_EXIT
:
1728 /* Optimization: when last instruction is EXIT
1729 * simply fallthrough to epilogue.
1731 if (i
== ctx
->prog
->len
- 1)
1733 jmp_offset
= epilogue_offset(ctx
);
1734 check_imm24(jmp_offset
);
1735 emit(ARM_B(jmp_offset
), ctx
);
1738 pr_info_once("*** NOT YET: opcode %02x ***\n", code
);
1741 pr_err_once("unknown opcode %02x\n", code
);
1745 if (ctx
->flags
& FLAG_IMM_OVERFLOW
)
1747 * this instruction generated an overflow when
1748 * trying to access the literal pool, so
1749 * delegate this filter to the kernel interpreter.
1755 static int build_body(struct jit_ctx
*ctx
)
1757 const struct bpf_prog
*prog
= ctx
->prog
;
1760 for (i
= 0; i
< prog
->len
; i
++) {
1761 const struct bpf_insn
*insn
= &(prog
->insnsi
[i
]);
1764 ret
= build_insn(insn
, ctx
);
1766 /* It's used with loading the 64 bit immediate value. */
1769 if (ctx
->target
== NULL
)
1770 ctx
->offsets
[i
] = ctx
->idx
;
1774 if (ctx
->target
== NULL
)
1775 ctx
->offsets
[i
] = ctx
->idx
;
1777 /* If unsuccesfull, return with error code */
1784 static int validate_code(struct jit_ctx
*ctx
)
1788 for (i
= 0; i
< ctx
->idx
; i
++) {
1789 if (ctx
->target
[i
] == __opcode_to_mem_arm(ARM_INST_UDF
))
1796 void bpf_jit_compile(struct bpf_prog
*prog
)
1798 /* Nothing to do here. We support Internal BPF. */
1801 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*prog
)
1803 struct bpf_prog
*tmp
, *orig_prog
= prog
;
1804 struct bpf_binary_header
*header
;
1805 bool tmp_blinded
= false;
1807 unsigned int tmp_idx
;
1808 unsigned int image_size
;
1811 /* If BPF JIT was not enabled then we must fall back to
1814 if (!prog
->jit_requested
)
1817 /* If constant blinding was enabled and we failed during blinding
1818 * then we must fall back to the interpreter. Otherwise, we save
1819 * the new JITed code.
1821 tmp
= bpf_jit_blind_constants(prog
);
1830 memset(&ctx
, 0, sizeof(ctx
));
1833 /* Not able to allocate memory for offsets[] , then
1834 * we must fall back to the interpreter
1836 ctx
.offsets
= kcalloc(prog
->len
, sizeof(int), GFP_KERNEL
);
1837 if (ctx
.offsets
== NULL
) {
1842 /* 1) fake pass to find in the length of the JITed code,
1843 * to compute ctx->offsets and other context variables
1844 * needed to compute final JITed code.
1845 * Also, calculate random starting pointer/start of JITed code
1846 * which is prefixed by random number of fault instructions.
1848 * If the first pass fails then there is no chance of it
1849 * being successful in the second pass, so just fall back
1850 * to the interpreter.
1852 if (build_body(&ctx
)) {
1858 build_prologue(&ctx
);
1859 ctx
.prologue_bytes
= (ctx
.idx
- tmp_idx
) * 4;
1861 ctx
.epilogue_offset
= ctx
.idx
;
1863 #if __LINUX_ARM_ARCH__ < 7
1865 build_epilogue(&ctx
);
1866 ctx
.epilogue_bytes
= (ctx
.idx
- tmp_idx
) * 4;
1868 ctx
.idx
+= ctx
.imm_count
;
1869 if (ctx
.imm_count
) {
1870 ctx
.imms
= kcalloc(ctx
.imm_count
, sizeof(u32
), GFP_KERNEL
);
1871 if (ctx
.imms
== NULL
) {
1877 /* there's nothing about the epilogue on ARMv7 */
1878 build_epilogue(&ctx
);
1880 /* Now we can get the actual image size of the JITed arm code.
1881 * Currently, we are not considering the THUMB-2 instructions
1882 * for jit, although it can decrease the size of the image.
1884 * As each arm instruction is of length 32bit, we are translating
1885 * number of JITed intructions into the size required to store these
1888 image_size
= sizeof(u32
) * ctx
.idx
;
1890 /* Now we know the size of the structure to make */
1891 header
= bpf_jit_binary_alloc(image_size
, &image_ptr
,
1892 sizeof(u32
), jit_fill_hole
);
1893 /* Not able to allocate memory for the structure then
1894 * we must fall back to the interpretation
1896 if (header
== NULL
) {
1901 /* 2.) Actual pass to generate final JIT code */
1902 ctx
.target
= (u32
*) image_ptr
;
1905 build_prologue(&ctx
);
1907 /* If building the body of the JITed code fails somehow,
1908 * we fall back to the interpretation.
1910 if (build_body(&ctx
) < 0) {
1912 bpf_jit_binary_free(header
);
1916 build_epilogue(&ctx
);
1918 /* 3.) Extra pass to validate JITed Code */
1919 if (validate_code(&ctx
)) {
1921 bpf_jit_binary_free(header
);
1925 flush_icache_range((u32
)header
, (u32
)(ctx
.target
+ ctx
.idx
));
1927 if (bpf_jit_enable
> 1)
1928 /* there are 2 passes here */
1929 bpf_jit_dump(prog
->len
, image_size
, 2, ctx
.target
);
1931 set_memory_ro((unsigned long)header
, header
->pages
);
1932 prog
->bpf_func
= (void *)ctx
.target
;
1934 prog
->jited_len
= image_size
;
1937 #if __LINUX_ARM_ARCH__ < 7
1945 bpf_jit_prog_release_other(prog
, prog
== orig_prog
?