xref: /openbmc/linux/arch/arm/net/bpf_jit_32.c (revision 96cced4e774a2728710c8f8f48441fc7b29d6177)
1ddecdfceSMircea Gherzan /*
239c13c20SShubham Bansal  * Just-In-Time compiler for eBPF filters on 32bit ARM
3ddecdfceSMircea Gherzan  *
439c13c20SShubham Bansal  * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
5ddecdfceSMircea Gherzan  * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
6ddecdfceSMircea Gherzan  *
7ddecdfceSMircea Gherzan  * This program is free software; you can redistribute it and/or modify it
8ddecdfceSMircea Gherzan  * under the terms of the GNU General Public License as published by the
9ddecdfceSMircea Gherzan  * Free Software Foundation; version 2 of the License.
10ddecdfceSMircea Gherzan  */
11ddecdfceSMircea Gherzan 
1239c13c20SShubham Bansal #include <linux/bpf.h>
13ddecdfceSMircea Gherzan #include <linux/bitops.h>
14ddecdfceSMircea Gherzan #include <linux/compiler.h>
15ddecdfceSMircea Gherzan #include <linux/errno.h>
16ddecdfceSMircea Gherzan #include <linux/filter.h>
17ddecdfceSMircea Gherzan #include <linux/netdevice.h>
18ddecdfceSMircea Gherzan #include <linux/string.h>
19ddecdfceSMircea Gherzan #include <linux/slab.h>
20bf0098f2SDaniel Borkmann #include <linux/if_vlan.h>
21e8b56d55SDaniel Borkmann 
22ddecdfceSMircea Gherzan #include <asm/cacheflush.h>
23ddecdfceSMircea Gherzan #include <asm/hwcap.h>
243460743eSBen Dooks #include <asm/opcodes.h>
25ddecdfceSMircea Gherzan 
26ddecdfceSMircea Gherzan #include "bpf_jit_32.h"
27ddecdfceSMircea Gherzan 
2870ec3a6cSRussell King /*
290005e55aSRussell King  * eBPF prog stack layout:
3070ec3a6cSRussell King  *
3170ec3a6cSRussell King  *                         high
320005e55aSRussell King  * original ARM_SP =>     +-----+
330005e55aSRussell King  *                        |     | callee saved registers
340005e55aSRussell King  *                        +-----+ <= (BPF_FP + SCRATCH_SIZE)
3570ec3a6cSRussell King  *                        | ... | eBPF JIT scratch space
360005e55aSRussell King  * eBPF fp register =>    +-----+
370005e55aSRussell King  *   (BPF_FP)             | ... | eBPF prog stack
3870ec3a6cSRussell King  *                        +-----+
3970ec3a6cSRussell King  *                        |RSVD | JIT scratchpad
400005e55aSRussell King  * current ARM_SP =>      +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE)
4170ec3a6cSRussell King  *                        |     |
4270ec3a6cSRussell King  *                        | ... | Function call stack
4370ec3a6cSRussell King  *                        |     |
4470ec3a6cSRussell King  *                        +-----+
4570ec3a6cSRussell King  *                          low
460005e55aSRussell King  *
470005e55aSRussell King  * The callee saved registers depends on whether frame pointers are enabled.
480005e55aSRussell King  * With frame pointers (to be compliant with the ABI):
490005e55aSRussell King  *
500005e55aSRussell King  *                                high
510005e55aSRussell King  * original ARM_SP =>     +------------------+ \
520005e55aSRussell King  *                        |        pc        | |
530005e55aSRussell King  * current ARM_FP =>      +------------------+ } callee saved registers
540005e55aSRussell King  *                        |r4-r8,r10,fp,ip,lr| |
550005e55aSRussell King  *                        +------------------+ /
560005e55aSRussell King  *                                low
570005e55aSRussell King  *
580005e55aSRussell King  * Without frame pointers:
590005e55aSRussell King  *
600005e55aSRussell King  *                                high
610005e55aSRussell King  * original ARM_SP =>     +------------------+
6202088d9bSRussell King  *                        | r4-r8,r10,fp,lr  | callee saved registers
6302088d9bSRussell King  * current ARM_FP =>      +------------------+
640005e55aSRussell King  *                                low
6502088d9bSRussell King  *
6602088d9bSRussell King  * When popping registers off the stack at the end of a BPF function, we
6702088d9bSRussell King  * reference them via the current ARM_FP register.
6870ec3a6cSRussell King  */
6902088d9bSRussell King #define CALLEE_MASK	(1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \
7002088d9bSRussell King 			 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R10 | \
7102088d9bSRussell King 			 1 << ARM_FP)
7202088d9bSRussell King #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR)
7302088d9bSRussell King #define CALLEE_POP_MASK  (CALLEE_MASK | 1 << ARM_PC)
7470ec3a6cSRussell King 
75d449ceb1SRussell King enum {
76d449ceb1SRussell King 	/* Stack layout - these are offsets from (top of stack - 4) */
77d449ceb1SRussell King 	BPF_R2_HI,
78d449ceb1SRussell King 	BPF_R2_LO,
79d449ceb1SRussell King 	BPF_R3_HI,
80d449ceb1SRussell King 	BPF_R3_LO,
81d449ceb1SRussell King 	BPF_R4_HI,
82d449ceb1SRussell King 	BPF_R4_LO,
83d449ceb1SRussell King 	BPF_R5_HI,
84d449ceb1SRussell King 	BPF_R5_LO,
85d449ceb1SRussell King 	BPF_R7_HI,
86d449ceb1SRussell King 	BPF_R7_LO,
87d449ceb1SRussell King 	BPF_R8_HI,
88d449ceb1SRussell King 	BPF_R8_LO,
89d449ceb1SRussell King 	BPF_R9_HI,
90d449ceb1SRussell King 	BPF_R9_LO,
91d449ceb1SRussell King 	BPF_FP_HI,
92d449ceb1SRussell King 	BPF_FP_LO,
93d449ceb1SRussell King 	BPF_TC_HI,
94d449ceb1SRussell King 	BPF_TC_LO,
95d449ceb1SRussell King 	BPF_AX_HI,
96d449ceb1SRussell King 	BPF_AX_LO,
97d449ceb1SRussell King 	/* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
98d449ceb1SRussell King 	 * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
99d449ceb1SRussell King 	 * BPF_REG_FP and Tail call counts.
100d449ceb1SRussell King 	 */
101d449ceb1SRussell King 	BPF_JIT_SCRATCH_REGS,
102d449ceb1SRussell King };
103d449ceb1SRussell King 
1041c35ba12SRussell King /*
1051c35ba12SRussell King  * Negative "register" values indicate the register is stored on the stack
1061c35ba12SRussell King  * and are the offset from the top of the eBPF JIT scratch space.
1071c35ba12SRussell King  */
1081c35ba12SRussell King #define STACK_OFFSET(k)	(-4 - (k) * 4)
109d449ceb1SRussell King #define SCRATCH_SIZE	(BPF_JIT_SCRATCH_REGS * 4)
110d449ceb1SRussell King 
111*96cced4eSRussell King #ifdef CONFIG_FRAME_POINTER
112*96cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) ((x) - 4 * hweight16(CALLEE_PUSH_MASK) - 4)
113*96cced4eSRussell King #else
114*96cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) (x)
115*96cced4eSRussell King #endif
116*96cced4eSRussell King 
11739c13c20SShubham Bansal #define TMP_REG_1	(MAX_BPF_JIT_REG + 0)	/* TEMP Register 1 */
11839c13c20SShubham Bansal #define TMP_REG_2	(MAX_BPF_JIT_REG + 1)	/* TEMP Register 2 */
11939c13c20SShubham Bansal #define TCALL_CNT	(MAX_BPF_JIT_REG + 2)	/* Tail Call Count */
12039c13c20SShubham Bansal 
12139c13c20SShubham Bansal #define FLAG_IMM_OVERFLOW	(1 << 0)
12239c13c20SShubham Bansal 
123ddecdfceSMircea Gherzan /*
12439c13c20SShubham Bansal  * Map eBPF registers to ARM 32bit registers or stack scratch space.
125ddecdfceSMircea Gherzan  *
12639c13c20SShubham Bansal  * 1. First argument is passed using the arm 32bit registers and rest of the
12739c13c20SShubham Bansal  * arguments are passed on stack scratch space.
1282b589a7eSWang YanQing  * 2. First callee-saved argument is mapped to arm 32 bit registers and rest
12939c13c20SShubham Bansal  * arguments are mapped to scratch space on stack.
13039c13c20SShubham Bansal  * 3. We need two 64 bit temp registers to do complex operations on eBPF
13139c13c20SShubham Bansal  * registers.
13239c13c20SShubham Bansal  *
13339c13c20SShubham Bansal  * As the eBPF registers are all 64 bit registers and arm has only 32 bit
13439c13c20SShubham Bansal  * registers, we have to map each eBPF registers with two arm 32 bit regs or
13539c13c20SShubham Bansal  * scratch memory space and we have to build eBPF 64 bit register from those.
13639c13c20SShubham Bansal  *
13739c13c20SShubham Bansal  */
1381c35ba12SRussell King static const s8 bpf2a32[][2] = {
13939c13c20SShubham Bansal 	/* return value from in-kernel function, and exit value from eBPF */
14039c13c20SShubham Bansal 	[BPF_REG_0] = {ARM_R1, ARM_R0},
14139c13c20SShubham Bansal 	/* arguments from eBPF program to in-kernel function */
14239c13c20SShubham Bansal 	[BPF_REG_1] = {ARM_R3, ARM_R2},
14339c13c20SShubham Bansal 	/* Stored on stack scratch space */
144d449ceb1SRussell King 	[BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)},
145d449ceb1SRussell King 	[BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)},
146d449ceb1SRussell King 	[BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)},
147d449ceb1SRussell King 	[BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)},
14839c13c20SShubham Bansal 	/* callee saved registers that in-kernel function will preserve */
14939c13c20SShubham Bansal 	[BPF_REG_6] = {ARM_R5, ARM_R4},
15039c13c20SShubham Bansal 	/* Stored on stack scratch space */
151d449ceb1SRussell King 	[BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
152d449ceb1SRussell King 	[BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
153d449ceb1SRussell King 	[BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
15439c13c20SShubham Bansal 	/* Read only Frame Pointer to access Stack */
155d449ceb1SRussell King 	[BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
15639c13c20SShubham Bansal 	/* Temporary Register for internal BPF JIT, can be used
15739c13c20SShubham Bansal 	 * for constant blindings and others.
15839c13c20SShubham Bansal 	 */
15939c13c20SShubham Bansal 	[TMP_REG_1] = {ARM_R7, ARM_R6},
16039c13c20SShubham Bansal 	[TMP_REG_2] = {ARM_R10, ARM_R8},
16139c13c20SShubham Bansal 	/* Tail call count. Stored on stack scratch space. */
162d449ceb1SRussell King 	[TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)},
16339c13c20SShubham Bansal 	/* temporary register for blinding constants.
16439c13c20SShubham Bansal 	 * Stored on stack scratch space.
16539c13c20SShubham Bansal 	 */
166d449ceb1SRussell King 	[BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
16739c13c20SShubham Bansal };
16839c13c20SShubham Bansal 
16939c13c20SShubham Bansal #define	dst_lo	dst[1]
17039c13c20SShubham Bansal #define dst_hi	dst[0]
17139c13c20SShubham Bansal #define src_lo	src[1]
17239c13c20SShubham Bansal #define src_hi	src[0]
17339c13c20SShubham Bansal 
17439c13c20SShubham Bansal /*
17539c13c20SShubham Bansal  * JIT Context:
17639c13c20SShubham Bansal  *
17739c13c20SShubham Bansal  * prog			:	bpf_prog
17839c13c20SShubham Bansal  * idx			:	index of current last JITed instruction.
17939c13c20SShubham Bansal  * prologue_bytes	:	bytes used in prologue.
18039c13c20SShubham Bansal  * epilogue_offset	:	offset of epilogue starting.
18139c13c20SShubham Bansal  * offsets		:	array of eBPF instruction offsets in
18239c13c20SShubham Bansal  *				JITed code.
18339c13c20SShubham Bansal  * target		:	final JITed code.
18439c13c20SShubham Bansal  * epilogue_bytes	:	no of bytes used in epilogue.
18539c13c20SShubham Bansal  * imm_count		:	no of immediate counts used for global
18639c13c20SShubham Bansal  *				variables.
18739c13c20SShubham Bansal  * imms			:	array of global variable addresses.
188ddecdfceSMircea Gherzan  */
189ddecdfceSMircea Gherzan 
190ddecdfceSMircea Gherzan struct jit_ctx {
19139c13c20SShubham Bansal 	const struct bpf_prog *prog;
19239c13c20SShubham Bansal 	unsigned int idx;
19339c13c20SShubham Bansal 	unsigned int prologue_bytes;
19439c13c20SShubham Bansal 	unsigned int epilogue_offset;
195ddecdfceSMircea Gherzan 	u32 flags;
196ddecdfceSMircea Gherzan 	u32 *offsets;
197ddecdfceSMircea Gherzan 	u32 *target;
19839c13c20SShubham Bansal 	u32 stack_size;
199ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
200ddecdfceSMircea Gherzan 	u16 epilogue_bytes;
201ddecdfceSMircea Gherzan 	u16 imm_count;
202ddecdfceSMircea Gherzan 	u32 *imms;
203ddecdfceSMircea Gherzan #endif
204ddecdfceSMircea Gherzan };
205ddecdfceSMircea Gherzan 
206ddecdfceSMircea Gherzan /*
2074560cdffSNicolas Schichan  * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
208ddecdfceSMircea Gherzan  * (where the assembly routines like __aeabi_uidiv could cause problems).
209ddecdfceSMircea Gherzan  */
21039c13c20SShubham Bansal static u32 jit_udiv32(u32 dividend, u32 divisor)
211ddecdfceSMircea Gherzan {
212ddecdfceSMircea Gherzan 	return dividend / divisor;
213ddecdfceSMircea Gherzan }
214ddecdfceSMircea Gherzan 
21539c13c20SShubham Bansal static u32 jit_mod32(u32 dividend, u32 divisor)
2164560cdffSNicolas Schichan {
2174560cdffSNicolas Schichan 	return dividend % divisor;
2184560cdffSNicolas Schichan }
2194560cdffSNicolas Schichan 
220ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
221ddecdfceSMircea Gherzan {
2223460743eSBen Dooks 	inst |= (cond << 28);
2233460743eSBen Dooks 	inst = __opcode_to_mem_arm(inst);
2243460743eSBen Dooks 
225ddecdfceSMircea Gherzan 	if (ctx->target != NULL)
2263460743eSBen Dooks 		ctx->target[ctx->idx] = inst;
227ddecdfceSMircea Gherzan 
228ddecdfceSMircea Gherzan 	ctx->idx++;
229ddecdfceSMircea Gherzan }
230ddecdfceSMircea Gherzan 
231ddecdfceSMircea Gherzan /*
232ddecdfceSMircea Gherzan  * Emit an instruction that will be executed unconditionally.
233ddecdfceSMircea Gherzan  */
234ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx)
235ddecdfceSMircea Gherzan {
236ddecdfceSMircea Gherzan 	_emit(ARM_COND_AL, inst, ctx);
237ddecdfceSMircea Gherzan }
238ddecdfceSMircea Gherzan 
23939c13c20SShubham Bansal /*
24039c13c20SShubham Bansal  * Checks if immediate value can be converted to imm12(12 bits) value.
24139c13c20SShubham Bansal  */
24239c13c20SShubham Bansal static int16_t imm8m(u32 x)
243ddecdfceSMircea Gherzan {
24439c13c20SShubham Bansal 	u32 rot;
245ddecdfceSMircea Gherzan 
24639c13c20SShubham Bansal 	for (rot = 0; rot < 16; rot++)
24739c13c20SShubham Bansal 		if ((x & ~ror32(0xff, 2 * rot)) == 0)
24839c13c20SShubham Bansal 			return rol32(x, 2 * rot) | (rot << 8);
24939c13c20SShubham Bansal 	return -1;
250ddecdfceSMircea Gherzan }
251ddecdfceSMircea Gherzan 
252a8ef95a0SRussell King static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12)
253a8ef95a0SRussell King {
254a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
255a8ef95a0SRussell King 	if (imm12 >= 0)
256a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
257a8ef95a0SRussell King 	else
258a8ef95a0SRussell King 		imm12 = -imm12;
259a8ef95a0SRussell King 	return op | (imm12 & 0xfff);
260a8ef95a0SRussell King }
261a8ef95a0SRussell King 
262a8ef95a0SRussell King static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8)
263a8ef95a0SRussell King {
264a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
265a8ef95a0SRussell King 	if (imm8 >= 0)
266a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
267a8ef95a0SRussell King 	else
268a8ef95a0SRussell King 		imm8 = -imm8;
269a8ef95a0SRussell King 	return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f);
270a8ef95a0SRussell King }
271a8ef95a0SRussell King 
272a8ef95a0SRussell King #define ARM_LDR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off)
273a8ef95a0SRussell King #define ARM_LDRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off)
274a8ef95a0SRussell King #define ARM_LDRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off)
275a8ef95a0SRussell King 
276a8ef95a0SRussell King #define ARM_STR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off)
277a8ef95a0SRussell King #define ARM_STRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off)
278a8ef95a0SRussell King #define ARM_STRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off)
279a8ef95a0SRussell King 
28039c13c20SShubham Bansal /*
28139c13c20SShubham Bansal  * Initializes the JIT space with undefined instructions.
28239c13c20SShubham Bansal  */
28355309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
28455309dd3SDaniel Borkmann {
285e8b56d55SDaniel Borkmann 	u32 *ptr;
28655309dd3SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
28755309dd3SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
288e8b56d55SDaniel Borkmann 		*ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
28955309dd3SDaniel Borkmann }
29055309dd3SDaniel Borkmann 
291d1220efdSRussell King #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
292d1220efdSRussell King /* EABI requires the stack to be aligned to 64-bit boundaries */
293d1220efdSRussell King #define STACK_ALIGNMENT	8
294d1220efdSRussell King #else
295d1220efdSRussell King /* Stack must be aligned to 32-bit boundaries */
296d1220efdSRussell King #define STACK_ALIGNMENT	4
297d1220efdSRussell King #endif
298ddecdfceSMircea Gherzan 
29939c13c20SShubham Bansal /* total stack size used in JITed code */
30038ca9306SDaniel Borkmann #define _STACK_SIZE	(ctx->prog->aux->stack_depth + SCRATCH_SIZE)
301d1220efdSRussell King #define STACK_SIZE	ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
302ddecdfceSMircea Gherzan 
303ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
304ddecdfceSMircea Gherzan 
305ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx)
306ddecdfceSMircea Gherzan {
30739c13c20SShubham Bansal 	unsigned int i = 0, offset;
308ddecdfceSMircea Gherzan 	u16 imm;
309ddecdfceSMircea Gherzan 
310ddecdfceSMircea Gherzan 	/* on the "fake" run we just count them (duplicates included) */
311ddecdfceSMircea Gherzan 	if (ctx->target == NULL) {
312ddecdfceSMircea Gherzan 		ctx->imm_count++;
313ddecdfceSMircea Gherzan 		return 0;
314ddecdfceSMircea Gherzan 	}
315ddecdfceSMircea Gherzan 
316ddecdfceSMircea Gherzan 	while ((i < ctx->imm_count) && ctx->imms[i]) {
317ddecdfceSMircea Gherzan 		if (ctx->imms[i] == k)
318ddecdfceSMircea Gherzan 			break;
319ddecdfceSMircea Gherzan 		i++;
320ddecdfceSMircea Gherzan 	}
321ddecdfceSMircea Gherzan 
322ddecdfceSMircea Gherzan 	if (ctx->imms[i] == 0)
323ddecdfceSMircea Gherzan 		ctx->imms[i] = k;
324ddecdfceSMircea Gherzan 
325ddecdfceSMircea Gherzan 	/* constants go just after the epilogue */
32639c13c20SShubham Bansal 	offset =  ctx->offsets[ctx->prog->len - 1] * 4;
327ddecdfceSMircea Gherzan 	offset += ctx->prologue_bytes;
328ddecdfceSMircea Gherzan 	offset += ctx->epilogue_bytes;
329ddecdfceSMircea Gherzan 	offset += i * 4;
330ddecdfceSMircea Gherzan 
331ddecdfceSMircea Gherzan 	ctx->target[offset / 4] = k;
332ddecdfceSMircea Gherzan 
333ddecdfceSMircea Gherzan 	/* PC in ARM mode == address of the instruction + 8 */
334ddecdfceSMircea Gherzan 	imm = offset - (8 + ctx->idx * 4);
335ddecdfceSMircea Gherzan 
3360b59d880SNicolas Schichan 	if (imm & ~0xfff) {
3370b59d880SNicolas Schichan 		/*
3380b59d880SNicolas Schichan 		 * literal pool is too far, signal it into flags. we
3390b59d880SNicolas Schichan 		 * can only detect it on the second pass unfortunately.
3400b59d880SNicolas Schichan 		 */
3410b59d880SNicolas Schichan 		ctx->flags |= FLAG_IMM_OVERFLOW;
3420b59d880SNicolas Schichan 		return 0;
3430b59d880SNicolas Schichan 	}
3440b59d880SNicolas Schichan 
345ddecdfceSMircea Gherzan 	return imm;
346ddecdfceSMircea Gherzan }
347ddecdfceSMircea Gherzan 
348ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */
349ddecdfceSMircea Gherzan 
35039c13c20SShubham Bansal static inline int bpf2a32_offset(int bpf_to, int bpf_from,
35139c13c20SShubham Bansal 				 const struct jit_ctx *ctx) {
35239c13c20SShubham Bansal 	int to, from;
35339c13c20SShubham Bansal 
35439c13c20SShubham Bansal 	if (ctx->target == NULL)
35539c13c20SShubham Bansal 		return 0;
35639c13c20SShubham Bansal 	to = ctx->offsets[bpf_to];
35739c13c20SShubham Bansal 	from = ctx->offsets[bpf_from];
35839c13c20SShubham Bansal 
35939c13c20SShubham Bansal 	return to - from - 1;
36039c13c20SShubham Bansal }
36139c13c20SShubham Bansal 
362ddecdfceSMircea Gherzan /*
363ddecdfceSMircea Gherzan  * Move an immediate that's not an imm8m to a core register.
364ddecdfceSMircea Gherzan  */
36539c13c20SShubham Bansal static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
366ddecdfceSMircea Gherzan {
367ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
368ddecdfceSMircea Gherzan 	emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
369ddecdfceSMircea Gherzan #else
370ddecdfceSMircea Gherzan 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
371ddecdfceSMircea Gherzan 	if (val > 0xffff)
372ddecdfceSMircea Gherzan 		emit(ARM_MOVT(rd, val >> 16), ctx);
373ddecdfceSMircea Gherzan #endif
374ddecdfceSMircea Gherzan }
375ddecdfceSMircea Gherzan 
37639c13c20SShubham Bansal static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
377ddecdfceSMircea Gherzan {
378ddecdfceSMircea Gherzan 	int imm12 = imm8m(val);
379ddecdfceSMircea Gherzan 
380ddecdfceSMircea Gherzan 	if (imm12 >= 0)
381ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(rd, imm12), ctx);
382ddecdfceSMircea Gherzan 	else
383ddecdfceSMircea Gherzan 		emit_mov_i_no8m(rd, val, ctx);
384ddecdfceSMircea Gherzan }
385ddecdfceSMircea Gherzan 
386e9062481SRussell King static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
387ddecdfceSMircea Gherzan {
388ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_THUMB)
389ddecdfceSMircea Gherzan 		emit(ARM_BX(tgt_reg), ctx);
390ddecdfceSMircea Gherzan 	else
391ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
392e9062481SRussell King }
393e9062481SRussell King 
394ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
395ddecdfceSMircea Gherzan {
396ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5
397ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
398e9062481SRussell King 	emit_bx_r(tgt_reg, ctx);
399ddecdfceSMircea Gherzan #else
400ddecdfceSMircea Gherzan 	emit(ARM_BLX_R(tgt_reg), ctx);
401ddecdfceSMircea Gherzan #endif
402ddecdfceSMircea Gherzan }
403ddecdfceSMircea Gherzan 
40439c13c20SShubham Bansal static inline int epilogue_offset(const struct jit_ctx *ctx)
405ddecdfceSMircea Gherzan {
40639c13c20SShubham Bansal 	int to, from;
40739c13c20SShubham Bansal 	/* No need for 1st dummy run */
40839c13c20SShubham Bansal 	if (ctx->target == NULL)
40939c13c20SShubham Bansal 		return 0;
41039c13c20SShubham Bansal 	to = ctx->epilogue_offset;
41139c13c20SShubham Bansal 	from = ctx->idx;
41239c13c20SShubham Bansal 
41339c13c20SShubham Bansal 	return to - from - 2;
41439c13c20SShubham Bansal }
41539c13c20SShubham Bansal 
41639c13c20SShubham Bansal static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op)
41739c13c20SShubham Bansal {
4181c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
41939c13c20SShubham Bansal 
420ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7
421ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_IDIVA) {
42239c13c20SShubham Bansal 		if (op == BPF_DIV)
423ddecdfceSMircea Gherzan 			emit(ARM_UDIV(rd, rm, rn), ctx);
4244560cdffSNicolas Schichan 		else {
42539c13c20SShubham Bansal 			emit(ARM_UDIV(ARM_IP, rm, rn), ctx);
42639c13c20SShubham Bansal 			emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
4274560cdffSNicolas Schichan 		}
428ddecdfceSMircea Gherzan 		return;
429ddecdfceSMircea Gherzan 	}
430ddecdfceSMircea Gherzan #endif
43119fc99d0SNicolas Schichan 
43219fc99d0SNicolas Schichan 	/*
43339c13c20SShubham Bansal 	 * For BPF_ALU | BPF_DIV | BPF_K instructions
43439c13c20SShubham Bansal 	 * As ARM_R1 and ARM_R0 contains 1st argument of bpf
43539c13c20SShubham Bansal 	 * function, we need to save it on caller side to save
43639c13c20SShubham Bansal 	 * it from getting destroyed within callee.
43739c13c20SShubham Bansal 	 * After the return from the callee, we restore ARM_R0
43839c13c20SShubham Bansal 	 * ARM_R1.
43919fc99d0SNicolas Schichan 	 */
44039c13c20SShubham Bansal 	if (rn != ARM_R1) {
44139c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
442ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R1, rn), ctx);
44339c13c20SShubham Bansal 	}
44439c13c20SShubham Bansal 	if (rm != ARM_R0) {
44539c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
44619fc99d0SNicolas Schichan 		emit(ARM_MOV_R(ARM_R0, rm), ctx);
44739c13c20SShubham Bansal 	}
448ddecdfceSMircea Gherzan 
44939c13c20SShubham Bansal 	/* Call appropriate function */
45039c13c20SShubham Bansal 	emit_mov_i(ARM_IP, op == BPF_DIV ?
45139c13c20SShubham Bansal 		   (u32)jit_udiv32 : (u32)jit_mod32, ctx);
45239c13c20SShubham Bansal 	emit_blx_r(ARM_IP, ctx);
453ddecdfceSMircea Gherzan 
45439c13c20SShubham Bansal 	/* Save return value */
455ddecdfceSMircea Gherzan 	if (rd != ARM_R0)
456ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(rd, ARM_R0), ctx);
45739c13c20SShubham Bansal 
45839c13c20SShubham Bansal 	/* Restore ARM_R0 and ARM_R1 */
45939c13c20SShubham Bansal 	if (rn != ARM_R1)
46039c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
46139c13c20SShubham Bansal 	if (rm != ARM_R0)
46239c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
463ddecdfceSMircea Gherzan }
464ddecdfceSMircea Gherzan 
46547b9c3bfSRussell King /* Is the translated BPF register on stack? */
46647b9c3bfSRussell King static bool is_stacked(s8 reg)
467ddecdfceSMircea Gherzan {
46847b9c3bfSRussell King 	return reg < 0;
469ddecdfceSMircea Gherzan }
470ddecdfceSMircea Gherzan 
4717a987025SRussell King /* If a BPF register is on the stack (stk is true), load it to the
4727a987025SRussell King  * supplied temporary register and return the temporary register
4737a987025SRussell King  * for subsequent operations, otherwise just use the CPU register.
4747a987025SRussell King  */
4757a987025SRussell King static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx)
4767a987025SRussell King {
4777a987025SRussell King 	if (is_stacked(reg)) {
478*96cced4eSRussell King 		emit(ARM_LDR_I(tmp, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
4797a987025SRussell King 		reg = tmp;
4807a987025SRussell King 	}
4817a987025SRussell King 	return reg;
4827a987025SRussell King }
4837a987025SRussell King 
484a6eccac5SRussell King static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp,
485a6eccac5SRussell King 				   struct jit_ctx *ctx)
486a6eccac5SRussell King {
487a6eccac5SRussell King 	if (is_stacked(reg[1])) {
488*96cced4eSRussell King 		emit(ARM_LDR_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg[1])),
489*96cced4eSRussell King 		     ctx);
490*96cced4eSRussell King 		emit(ARM_LDR_I(tmp[0], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg[0])),
491*96cced4eSRussell King 		     ctx);
492a6eccac5SRussell King 		reg = tmp;
493a6eccac5SRussell King 	}
494a6eccac5SRussell King 	return reg;
495a6eccac5SRussell King }
496a6eccac5SRussell King 
4977a987025SRussell King /* If a BPF register is on the stack (stk is true), save the register
4987a987025SRussell King  * back to the stack.  If the source register is not the same, then
4997a987025SRussell King  * move it into the correct register.
5007a987025SRussell King  */
5017a987025SRussell King static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx)
5027a987025SRussell King {
5037a987025SRussell King 	if (is_stacked(reg))
504*96cced4eSRussell King 		emit(ARM_STR_I(src, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
5057a987025SRussell King 	else if (reg != src)
5067a987025SRussell King 		emit(ARM_MOV_R(reg, src), ctx);
5077a987025SRussell King }
5087a987025SRussell King 
509a6eccac5SRussell King static void arm_bpf_put_reg64(const s8 *reg, const s8 *src,
510a6eccac5SRussell King 			      struct jit_ctx *ctx)
511a6eccac5SRussell King {
512a6eccac5SRussell King 	if (is_stacked(reg[1])) {
513*96cced4eSRussell King 		emit(ARM_STR_I(src[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg[1])),
514*96cced4eSRussell King 		     ctx);
515*96cced4eSRussell King 		emit(ARM_STR_I(src[0], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg[0])),
516*96cced4eSRussell King 		     ctx);
517a6eccac5SRussell King 	} else {
518a6eccac5SRussell King 		if (reg[1] != src[1])
519a6eccac5SRussell King 			emit(ARM_MOV_R(reg[1], src[1]), ctx);
520a6eccac5SRussell King 		if (reg[0] != src[0])
521a6eccac5SRussell King 			emit(ARM_MOV_R(reg[0], src[0]), ctx);
522a6eccac5SRussell King 	}
523a6eccac5SRussell King }
524a6eccac5SRussell King 
5251c35ba12SRussell King static inline void emit_a32_mov_i(const s8 dst, const u32 val,
52647b9c3bfSRussell King 				  struct jit_ctx *ctx)
527ddecdfceSMircea Gherzan {
5281c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
529ddecdfceSMircea Gherzan 
53047b9c3bfSRussell King 	if (is_stacked(dst)) {
53139c13c20SShubham Bansal 		emit_mov_i(tmp[1], val, ctx);
5327a987025SRussell King 		arm_bpf_put_reg32(dst, tmp[1], ctx);
53339c13c20SShubham Bansal 	} else {
53439c13c20SShubham Bansal 		emit_mov_i(dst, val, ctx);
53539c13c20SShubham Bansal 	}
53639c13c20SShubham Bansal }
53734805931SDaniel Borkmann 
53839c13c20SShubham Bansal /* Sign extended move */
5391c35ba12SRussell King static inline void emit_a32_mov_i64(const bool is64, const s8 dst[],
54047b9c3bfSRussell King 				  const u32 val, struct jit_ctx *ctx) {
54139c13c20SShubham Bansal 	u32 hi = 0;
542ddecdfceSMircea Gherzan 
54339c13c20SShubham Bansal 	if (is64 && (val & (1<<31)))
54439c13c20SShubham Bansal 		hi = (u32)~0;
54547b9c3bfSRussell King 	emit_a32_mov_i(dst_lo, val, ctx);
54647b9c3bfSRussell King 	emit_a32_mov_i(dst_hi, hi, ctx);
54739c13c20SShubham Bansal }
54839c13c20SShubham Bansal 
54939c13c20SShubham Bansal static inline void emit_a32_add_r(const u8 dst, const u8 src,
55039c13c20SShubham Bansal 			      const bool is64, const bool hi,
55139c13c20SShubham Bansal 			      struct jit_ctx *ctx) {
55239c13c20SShubham Bansal 	/* 64 bit :
55339c13c20SShubham Bansal 	 *	adds dst_lo, dst_lo, src_lo
55439c13c20SShubham Bansal 	 *	adc dst_hi, dst_hi, src_hi
55539c13c20SShubham Bansal 	 * 32 bit :
55639c13c20SShubham Bansal 	 *	add dst_lo, dst_lo, src_lo
55739c13c20SShubham Bansal 	 */
55839c13c20SShubham Bansal 	if (!hi && is64)
55939c13c20SShubham Bansal 		emit(ARM_ADDS_R(dst, dst, src), ctx);
56039c13c20SShubham Bansal 	else if (hi && is64)
56139c13c20SShubham Bansal 		emit(ARM_ADC_R(dst, dst, src), ctx);
56239c13c20SShubham Bansal 	else
56339c13c20SShubham Bansal 		emit(ARM_ADD_R(dst, dst, src), ctx);
56439c13c20SShubham Bansal }
56539c13c20SShubham Bansal 
56639c13c20SShubham Bansal static inline void emit_a32_sub_r(const u8 dst, const u8 src,
56739c13c20SShubham Bansal 				  const bool is64, const bool hi,
56839c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
56939c13c20SShubham Bansal 	/* 64 bit :
57039c13c20SShubham Bansal 	 *	subs dst_lo, dst_lo, src_lo
57139c13c20SShubham Bansal 	 *	sbc dst_hi, dst_hi, src_hi
57239c13c20SShubham Bansal 	 * 32 bit :
57339c13c20SShubham Bansal 	 *	sub dst_lo, dst_lo, src_lo
57439c13c20SShubham Bansal 	 */
57539c13c20SShubham Bansal 	if (!hi && is64)
57639c13c20SShubham Bansal 		emit(ARM_SUBS_R(dst, dst, src), ctx);
57739c13c20SShubham Bansal 	else if (hi && is64)
57839c13c20SShubham Bansal 		emit(ARM_SBC_R(dst, dst, src), ctx);
57939c13c20SShubham Bansal 	else
58039c13c20SShubham Bansal 		emit(ARM_SUB_R(dst, dst, src), ctx);
58139c13c20SShubham Bansal }
58239c13c20SShubham Bansal 
58339c13c20SShubham Bansal static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
58439c13c20SShubham Bansal 			      const bool hi, const u8 op, struct jit_ctx *ctx){
58539c13c20SShubham Bansal 	switch (BPF_OP(op)) {
58639c13c20SShubham Bansal 	/* dst = dst + src */
58739c13c20SShubham Bansal 	case BPF_ADD:
58839c13c20SShubham Bansal 		emit_a32_add_r(dst, src, is64, hi, ctx);
58939c13c20SShubham Bansal 		break;
59039c13c20SShubham Bansal 	/* dst = dst - src */
59139c13c20SShubham Bansal 	case BPF_SUB:
59239c13c20SShubham Bansal 		emit_a32_sub_r(dst, src, is64, hi, ctx);
59339c13c20SShubham Bansal 		break;
59439c13c20SShubham Bansal 	/* dst = dst | src */
59539c13c20SShubham Bansal 	case BPF_OR:
59639c13c20SShubham Bansal 		emit(ARM_ORR_R(dst, dst, src), ctx);
59739c13c20SShubham Bansal 		break;
59839c13c20SShubham Bansal 	/* dst = dst & src */
59939c13c20SShubham Bansal 	case BPF_AND:
60039c13c20SShubham Bansal 		emit(ARM_AND_R(dst, dst, src), ctx);
60139c13c20SShubham Bansal 		break;
60239c13c20SShubham Bansal 	/* dst = dst ^ src */
60339c13c20SShubham Bansal 	case BPF_XOR:
60439c13c20SShubham Bansal 		emit(ARM_EOR_R(dst, dst, src), ctx);
60539c13c20SShubham Bansal 		break;
60639c13c20SShubham Bansal 	/* dst = dst * src */
60739c13c20SShubham Bansal 	case BPF_MUL:
60839c13c20SShubham Bansal 		emit(ARM_MUL(dst, dst, src), ctx);
60939c13c20SShubham Bansal 		break;
61039c13c20SShubham Bansal 	/* dst = dst << src */
61139c13c20SShubham Bansal 	case BPF_LSH:
61239c13c20SShubham Bansal 		emit(ARM_LSL_R(dst, dst, src), ctx);
61339c13c20SShubham Bansal 		break;
61439c13c20SShubham Bansal 	/* dst = dst >> src */
61539c13c20SShubham Bansal 	case BPF_RSH:
61639c13c20SShubham Bansal 		emit(ARM_LSR_R(dst, dst, src), ctx);
61739c13c20SShubham Bansal 		break;
61839c13c20SShubham Bansal 	/* dst = dst >> src (signed)*/
61939c13c20SShubham Bansal 	case BPF_ARSH:
62039c13c20SShubham Bansal 		emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
62139c13c20SShubham Bansal 		break;
62239c13c20SShubham Bansal 	}
62339c13c20SShubham Bansal }
62439c13c20SShubham Bansal 
62539c13c20SShubham Bansal /* ALU operation (32 bit)
62639c13c20SShubham Bansal  * dst = dst (op) src
62739c13c20SShubham Bansal  */
6281c35ba12SRussell King static inline void emit_a32_alu_r(const s8 dst, const s8 src,
62939c13c20SShubham Bansal 				  struct jit_ctx *ctx, const bool is64,
63039c13c20SShubham Bansal 				  const bool hi, const u8 op) {
6311c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
6327a987025SRussell King 	s8 rn, rd;
63339c13c20SShubham Bansal 
6347a987025SRussell King 	rn = arm_bpf_get_reg32(src, tmp[1], ctx);
6357a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
63639c13c20SShubham Bansal 	/* ALU operation */
6377a987025SRussell King 	emit_alu_r(rd, rn, is64, hi, op, ctx);
6387a987025SRussell King 	arm_bpf_put_reg32(dst, rd, ctx);
63939c13c20SShubham Bansal }
64039c13c20SShubham Bansal 
64139c13c20SShubham Bansal /* ALU operation (64 bit) */
6421c35ba12SRussell King static inline void emit_a32_alu_r64(const bool is64, const s8 dst[],
64347b9c3bfSRussell King 				  const s8 src[], struct jit_ctx *ctx,
64439c13c20SShubham Bansal 				  const u8 op) {
64547b9c3bfSRussell King 	emit_a32_alu_r(dst_lo, src_lo, ctx, is64, false, op);
64639c13c20SShubham Bansal 	if (is64)
64747b9c3bfSRussell King 		emit_a32_alu_r(dst_hi, src_hi, ctx, is64, true, op);
64839c13c20SShubham Bansal 	else
64947b9c3bfSRussell King 		emit_a32_mov_i(dst_hi, 0, ctx);
65039c13c20SShubham Bansal }
65139c13c20SShubham Bansal 
6527a987025SRussell King /* dst = src (4 bytes)*/
6531c35ba12SRussell King static inline void emit_a32_mov_r(const s8 dst, const s8 src,
65439c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
6551c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
6567a987025SRussell King 	s8 rt;
65739c13c20SShubham Bansal 
6587a987025SRussell King 	rt = arm_bpf_get_reg32(src, tmp[0], ctx);
6597a987025SRussell King 	arm_bpf_put_reg32(dst, rt, ctx);
66039c13c20SShubham Bansal }
66139c13c20SShubham Bansal 
66239c13c20SShubham Bansal /* dst = src */
6631c35ba12SRussell King static inline void emit_a32_mov_r64(const bool is64, const s8 dst[],
66447b9c3bfSRussell King 				  const s8 src[],
66547b9c3bfSRussell King 				  struct jit_ctx *ctx) {
66647b9c3bfSRussell King 	emit_a32_mov_r(dst_lo, src_lo, ctx);
66739c13c20SShubham Bansal 	if (is64) {
66839c13c20SShubham Bansal 		/* complete 8 byte move */
66947b9c3bfSRussell King 		emit_a32_mov_r(dst_hi, src_hi, ctx);
67039c13c20SShubham Bansal 	} else {
67139c13c20SShubham Bansal 		/* Zero out high 4 bytes */
67247b9c3bfSRussell King 		emit_a32_mov_i(dst_hi, 0, ctx);
67339c13c20SShubham Bansal 	}
67439c13c20SShubham Bansal }
67539c13c20SShubham Bansal 
67639c13c20SShubham Bansal /* Shift operations */
67747b9c3bfSRussell King static inline void emit_a32_alu_i(const s8 dst, const u32 val,
67839c13c20SShubham Bansal 				struct jit_ctx *ctx, const u8 op) {
6791c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
6807a987025SRussell King 	s8 rd;
68139c13c20SShubham Bansal 
6827a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
68339c13c20SShubham Bansal 
68439c13c20SShubham Bansal 	/* Do shift operation */
68539c13c20SShubham Bansal 	switch (op) {
68639c13c20SShubham Bansal 	case BPF_LSH:
68739c13c20SShubham Bansal 		emit(ARM_LSL_I(rd, rd, val), ctx);
68839c13c20SShubham Bansal 		break;
68939c13c20SShubham Bansal 	case BPF_RSH:
69039c13c20SShubham Bansal 		emit(ARM_LSR_I(rd, rd, val), ctx);
69139c13c20SShubham Bansal 		break;
69239c13c20SShubham Bansal 	case BPF_NEG:
69339c13c20SShubham Bansal 		emit(ARM_RSB_I(rd, rd, val), ctx);
69439c13c20SShubham Bansal 		break;
69539c13c20SShubham Bansal 	}
69639c13c20SShubham Bansal 
6977a987025SRussell King 	arm_bpf_put_reg32(dst, rd, ctx);
69839c13c20SShubham Bansal }
69939c13c20SShubham Bansal 
70039c13c20SShubham Bansal /* dst = ~dst (64 bit) */
70147b9c3bfSRussell King static inline void emit_a32_neg64(const s8 dst[],
70239c13c20SShubham Bansal 				struct jit_ctx *ctx){
7031c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
704a6eccac5SRussell King 	const s8 *rd;
70539c13c20SShubham Bansal 
70639c13c20SShubham Bansal 	/* Setup Operand */
707a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
70839c13c20SShubham Bansal 
70939c13c20SShubham Bansal 	/* Do Negate Operation */
710a6eccac5SRussell King 	emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx);
711a6eccac5SRussell King 	emit(ARM_RSC_I(rd[0], rd[0], 0), ctx);
71239c13c20SShubham Bansal 
713a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
71439c13c20SShubham Bansal }
71539c13c20SShubham Bansal 
71639c13c20SShubham Bansal /* dst = dst << src */
71747b9c3bfSRussell King static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[],
71847b9c3bfSRussell King 				    struct jit_ctx *ctx) {
7191c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7201c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
721a6eccac5SRussell King 	const s8 *rd;
722a6eccac5SRussell King 	s8 rt;
72339c13c20SShubham Bansal 
72439c13c20SShubham Bansal 	/* Setup Operands */
7257a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
726a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
72739c13c20SShubham Bansal 
72839c13c20SShubham Bansal 	/* Do LSH operation */
72939c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
73039c13c20SShubham Bansal 	emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
731a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx);
732a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx);
733a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx);
734a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx);
73539c13c20SShubham Bansal 
7367a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
7377a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
73839c13c20SShubham Bansal }
73939c13c20SShubham Bansal 
74039c13c20SShubham Bansal /* dst = dst >> src (signed)*/
74147b9c3bfSRussell King static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[],
74247b9c3bfSRussell King 				     struct jit_ctx *ctx) {
7431c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7441c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
745a6eccac5SRussell King 	const s8 *rd;
746a6eccac5SRussell King 	s8 rt;
74739c13c20SShubham Bansal 
7487a987025SRussell King 	/* Setup Operands */
7497a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
750a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
75139c13c20SShubham Bansal 
75239c13c20SShubham Bansal 	/* Do the ARSH operation */
75339c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
75439c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
755a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
756a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
75739c13c20SShubham Bansal 	_emit(ARM_COND_MI, ARM_B(0), ctx);
758a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
759a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx);
7607a987025SRussell King 
7617a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
7627a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
76339c13c20SShubham Bansal }
76439c13c20SShubham Bansal 
76539c13c20SShubham Bansal /* dst = dst >> src */
76647b9c3bfSRussell King static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[],
76747b9c3bfSRussell King 				    struct jit_ctx *ctx) {
7681c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7691c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
770a6eccac5SRussell King 	const s8 *rd;
771a6eccac5SRussell King 	s8 rt;
77239c13c20SShubham Bansal 
7737a987025SRussell King 	/* Setup Operands */
7747a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
775a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
77639c13c20SShubham Bansal 
77768565a1aSWang YanQing 	/* Do RSH operation */
77839c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
77939c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
780a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
781a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
782a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx);
783a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx);
7847a987025SRussell King 
7857a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
7867a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
78739c13c20SShubham Bansal }
78839c13c20SShubham Bansal 
78939c13c20SShubham Bansal /* dst = dst << val */
79047b9c3bfSRussell King static inline void emit_a32_lsh_i64(const s8 dst[],
79139c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx){
7921c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7931c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
794a6eccac5SRussell King 	const s8 *rd;
79539c13c20SShubham Bansal 
7967a987025SRussell King 	/* Setup operands */
797a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
79839c13c20SShubham Bansal 
79939c13c20SShubham Bansal 	/* Do LSH operation */
80039c13c20SShubham Bansal 	if (val < 32) {
801a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx);
802a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx);
803a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx);
80439c13c20SShubham Bansal 	} else {
80539c13c20SShubham Bansal 		if (val == 32)
806a6eccac5SRussell King 			emit(ARM_MOV_R(rd[0], rd[1]), ctx);
80739c13c20SShubham Bansal 		else
808a6eccac5SRussell King 			emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx);
809a6eccac5SRussell King 		emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx);
81039c13c20SShubham Bansal 	}
81139c13c20SShubham Bansal 
812a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
81339c13c20SShubham Bansal }
81439c13c20SShubham Bansal 
81539c13c20SShubham Bansal /* dst = dst >> val */
81647b9c3bfSRussell King static inline void emit_a32_rsh_i64(const s8 dst[],
81739c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx) {
8181c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8191c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
820a6eccac5SRussell King 	const s8 *rd;
82139c13c20SShubham Bansal 
8227a987025SRussell King 	/* Setup operands */
823a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
82439c13c20SShubham Bansal 
82539c13c20SShubham Bansal 	/* Do LSR operation */
82639c13c20SShubham Bansal 	if (val < 32) {
827a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
828a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
829a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx);
83039c13c20SShubham Bansal 	} else if (val == 32) {
831a6eccac5SRussell King 		emit(ARM_MOV_R(rd[1], rd[0]), ctx);
832a6eccac5SRussell King 		emit(ARM_MOV_I(rd[0], 0), ctx);
83339c13c20SShubham Bansal 	} else {
834a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx);
835a6eccac5SRussell King 		emit(ARM_MOV_I(rd[0], 0), ctx);
83639c13c20SShubham Bansal 	}
83739c13c20SShubham Bansal 
838a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
83939c13c20SShubham Bansal }
84039c13c20SShubham Bansal 
84139c13c20SShubham Bansal /* dst = dst >> val (signed) */
84247b9c3bfSRussell King static inline void emit_a32_arsh_i64(const s8 dst[],
84339c13c20SShubham Bansal 				     const u32 val, struct jit_ctx *ctx){
8441c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8451c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
846a6eccac5SRussell King 	const s8 *rd;
84739c13c20SShubham Bansal 
8487a987025SRussell King 	/* Setup operands */
849a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
85039c13c20SShubham Bansal 
85139c13c20SShubham Bansal 	/* Do ARSH operation */
85239c13c20SShubham Bansal 	if (val < 32) {
853a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
854a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
855a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx);
85639c13c20SShubham Bansal 	} else if (val == 32) {
857a6eccac5SRussell King 		emit(ARM_MOV_R(rd[1], rd[0]), ctx);
858a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
85939c13c20SShubham Bansal 	} else {
860a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx);
861a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
86239c13c20SShubham Bansal 	}
86339c13c20SShubham Bansal 
864a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
86539c13c20SShubham Bansal }
86639c13c20SShubham Bansal 
86747b9c3bfSRussell King static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[],
86847b9c3bfSRussell King 				    struct jit_ctx *ctx) {
8691c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8701c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
871a6eccac5SRussell King 	const s8 *rd, *rt;
87239c13c20SShubham Bansal 
8737a987025SRussell King 	/* Setup operands for multiplication */
874a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
875a6eccac5SRussell King 	rt = arm_bpf_get_reg64(src, tmp2, ctx);
87639c13c20SShubham Bansal 
87739c13c20SShubham Bansal 	/* Do Multiplication */
878a6eccac5SRussell King 	emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx);
879a6eccac5SRussell King 	emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx);
88039c13c20SShubham Bansal 	emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
88139c13c20SShubham Bansal 
882a6eccac5SRussell King 	emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx);
883a6eccac5SRussell King 	emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx);
8847a987025SRussell King 
8857a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_IP, ctx);
886a6eccac5SRussell King 	arm_bpf_put_reg32(dst_hi, rd[0], ctx);
88739c13c20SShubham Bansal }
88839c13c20SShubham Bansal 
88939c13c20SShubham Bansal /* *(size *)(dst + off) = src */
89047b9c3bfSRussell King static inline void emit_str_r(const s8 dst, const s8 src,
89139c13c20SShubham Bansal 			      const s32 off, struct jit_ctx *ctx, const u8 sz){
8921c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8937a987025SRussell King 	s8 rd;
89439c13c20SShubham Bansal 
8957a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[1], ctx);
89639c13c20SShubham Bansal 	if (off) {
89747b9c3bfSRussell King 		emit_a32_mov_i(tmp[0], off, ctx);
89839c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], rd, tmp[0]), ctx);
89939c13c20SShubham Bansal 		rd = tmp[0];
90039c13c20SShubham Bansal 	}
90139c13c20SShubham Bansal 	switch (sz) {
90239c13c20SShubham Bansal 	case BPF_W:
90339c13c20SShubham Bansal 		/* Store a Word */
90439c13c20SShubham Bansal 		emit(ARM_STR_I(src, rd, 0), ctx);
90539c13c20SShubham Bansal 		break;
90639c13c20SShubham Bansal 	case BPF_H:
90739c13c20SShubham Bansal 		/* Store a HalfWord */
90839c13c20SShubham Bansal 		emit(ARM_STRH_I(src, rd, 0), ctx);
90939c13c20SShubham Bansal 		break;
91039c13c20SShubham Bansal 	case BPF_B:
91139c13c20SShubham Bansal 		/* Store a Byte */
91239c13c20SShubham Bansal 		emit(ARM_STRB_I(src, rd, 0), ctx);
91339c13c20SShubham Bansal 		break;
91439c13c20SShubham Bansal 	}
91539c13c20SShubham Bansal }
91639c13c20SShubham Bansal 
91739c13c20SShubham Bansal /* dst = *(size*)(src + off) */
91847b9c3bfSRussell King static inline void emit_ldx_r(const s8 dst[], const s8 src,
919ec19e02bSRussell King 			      s32 off, struct jit_ctx *ctx, const u8 sz){
9201c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
92147b9c3bfSRussell King 	const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
9221c35ba12SRussell King 	s8 rm = src;
923ec19e02bSRussell King 	s32 off_max;
92439c13c20SShubham Bansal 
925ec19e02bSRussell King 	if (sz == BPF_H)
926ec19e02bSRussell King 		off_max = 0xff;
927ec19e02bSRussell King 	else
928ec19e02bSRussell King 		off_max = 0xfff;
929ec19e02bSRussell King 
930ec19e02bSRussell King 	if (off < 0 || off > off_max) {
93147b9c3bfSRussell King 		emit_a32_mov_i(tmp[0], off, ctx);
93239c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
93339c13c20SShubham Bansal 		rm = tmp[0];
934ec19e02bSRussell King 		off = 0;
935ec19e02bSRussell King 	} else if (rd[1] == rm) {
936ec19e02bSRussell King 		emit(ARM_MOV_R(tmp[0], rm), ctx);
937ec19e02bSRussell King 		rm = tmp[0];
93839c13c20SShubham Bansal 	}
93939c13c20SShubham Bansal 	switch (sz) {
940ec19e02bSRussell King 	case BPF_B:
941ec19e02bSRussell King 		/* Load a Byte */
942ec19e02bSRussell King 		emit(ARM_LDRB_I(rd[1], rm, off), ctx);
943a6eccac5SRussell King 		emit_a32_mov_i(rd[0], 0, ctx);
94439c13c20SShubham Bansal 		break;
94539c13c20SShubham Bansal 	case BPF_H:
94639c13c20SShubham Bansal 		/* Load a HalfWord */
947ec19e02bSRussell King 		emit(ARM_LDRH_I(rd[1], rm, off), ctx);
948a6eccac5SRussell King 		emit_a32_mov_i(rd[0], 0, ctx);
94939c13c20SShubham Bansal 		break;
950ec19e02bSRussell King 	case BPF_W:
951ec19e02bSRussell King 		/* Load a Word */
952ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
953a6eccac5SRussell King 		emit_a32_mov_i(rd[0], 0, ctx);
954ec19e02bSRussell King 		break;
955ec19e02bSRussell King 	case BPF_DW:
956ec19e02bSRussell King 		/* Load a Double Word */
957ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
958ec19e02bSRussell King 		emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
95939c13c20SShubham Bansal 		break;
96039c13c20SShubham Bansal 	}
961a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
96239c13c20SShubham Bansal }
96339c13c20SShubham Bansal 
96439c13c20SShubham Bansal /* Arithmatic Operation */
96539c13c20SShubham Bansal static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
96639c13c20SShubham Bansal 			     const u8 rn, struct jit_ctx *ctx, u8 op) {
96739c13c20SShubham Bansal 	switch (op) {
96839c13c20SShubham Bansal 	case BPF_JSET:
96939c13c20SShubham Bansal 		emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
97039c13c20SShubham Bansal 		emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
97139c13c20SShubham Bansal 		emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
97239c13c20SShubham Bansal 		break;
97339c13c20SShubham Bansal 	case BPF_JEQ:
97439c13c20SShubham Bansal 	case BPF_JNE:
97539c13c20SShubham Bansal 	case BPF_JGT:
97639c13c20SShubham Bansal 	case BPF_JGE:
97739c13c20SShubham Bansal 	case BPF_JLE:
97839c13c20SShubham Bansal 	case BPF_JLT:
97939c13c20SShubham Bansal 		emit(ARM_CMP_R(rd, rm), ctx);
98039c13c20SShubham Bansal 		_emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
98139c13c20SShubham Bansal 		break;
98239c13c20SShubham Bansal 	case BPF_JSLE:
98339c13c20SShubham Bansal 	case BPF_JSGT:
98439c13c20SShubham Bansal 		emit(ARM_CMP_R(rn, rt), ctx);
98539c13c20SShubham Bansal 		emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
98639c13c20SShubham Bansal 		break;
98739c13c20SShubham Bansal 	case BPF_JSLT:
98839c13c20SShubham Bansal 	case BPF_JSGE:
98939c13c20SShubham Bansal 		emit(ARM_CMP_R(rt, rn), ctx);
99039c13c20SShubham Bansal 		emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
99139c13c20SShubham Bansal 		break;
99239c13c20SShubham Bansal 	}
99339c13c20SShubham Bansal }
99439c13c20SShubham Bansal 
99539c13c20SShubham Bansal static int out_offset = -1; /* initialized on the first pass of build_body() */
99639c13c20SShubham Bansal static int emit_bpf_tail_call(struct jit_ctx *ctx)
99739c13c20SShubham Bansal {
99839c13c20SShubham Bansal 
99939c13c20SShubham Bansal 	/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
10001c35ba12SRussell King 	const s8 *r2 = bpf2a32[BPF_REG_2];
10011c35ba12SRussell King 	const s8 *r3 = bpf2a32[BPF_REG_3];
10021c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
10031c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
10041c35ba12SRussell King 	const s8 *tcc = bpf2a32[TCALL_CNT];
1005a6eccac5SRussell King 	const s8 *tc;
100639c13c20SShubham Bansal 	const int idx0 = ctx->idx;
100739c13c20SShubham Bansal #define cur_offset (ctx->idx - idx0)
1008f4483f2cSRussell King #define jmp_offset (out_offset - (cur_offset) - 2)
100939c13c20SShubham Bansal 	u32 off, lo, hi;
1010a6eccac5SRussell King 	s8 r_array, r_index;
101139c13c20SShubham Bansal 
101239c13c20SShubham Bansal 	/* if (index >= array->map.max_entries)
101339c13c20SShubham Bansal 	 *	goto out;
101439c13c20SShubham Bansal 	 */
101539c13c20SShubham Bansal 	off = offsetof(struct bpf_array, map.max_entries);
101639c13c20SShubham Bansal 	/* array->map.max_entries */
101747b9c3bfSRussell King 	emit_a32_mov_i(tmp[1], off, ctx);
10187a987025SRussell King 	r_array = arm_bpf_get_reg32(r2[1], tmp2[1], ctx);
10197a987025SRussell King 	emit(ARM_LDR_R(tmp[1], r_array, tmp[1]), ctx);
1020091f0248SRussell King 	/* index is 32-bit for arrays */
10217a987025SRussell King 	r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
102239c13c20SShubham Bansal 	/* index >= array->map.max_entries */
10237a987025SRussell King 	emit(ARM_CMP_R(r_index, tmp[1]), ctx);
102439c13c20SShubham Bansal 	_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
102539c13c20SShubham Bansal 
102639c13c20SShubham Bansal 	/* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
102739c13c20SShubham Bansal 	 *	goto out;
102839c13c20SShubham Bansal 	 * tail_call_cnt++;
102939c13c20SShubham Bansal 	 */
103039c13c20SShubham Bansal 	lo = (u32)MAX_TAIL_CALL_CNT;
103139c13c20SShubham Bansal 	hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
1032a6eccac5SRussell King 	tc = arm_bpf_get_reg64(tcc, tmp, ctx);
1033a6eccac5SRussell King 	emit(ARM_CMP_I(tc[0], hi), ctx);
1034a6eccac5SRussell King 	_emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx);
103539c13c20SShubham Bansal 	_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
1036a6eccac5SRussell King 	emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx);
1037a6eccac5SRussell King 	emit(ARM_ADC_I(tc[0], tc[0], 0), ctx);
1038a6eccac5SRussell King 	arm_bpf_put_reg64(tcc, tmp, ctx);
103939c13c20SShubham Bansal 
104039c13c20SShubham Bansal 	/* prog = array->ptrs[index]
104139c13c20SShubham Bansal 	 * if (prog == NULL)
104239c13c20SShubham Bansal 	 *	goto out;
104339c13c20SShubham Bansal 	 */
104439c13c20SShubham Bansal 	off = offsetof(struct bpf_array, ptrs);
104547b9c3bfSRussell King 	emit_a32_mov_i(tmp[1], off, ctx);
10467a987025SRussell King 	r_array = arm_bpf_get_reg32(r2[1], tmp2[1], ctx);
10477a987025SRussell King 	emit(ARM_ADD_R(tmp[1], r_array, tmp[1]), ctx);
10487a987025SRussell King 	r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
10497a987025SRussell King 	emit(ARM_MOV_SI(tmp[0], r_index, SRTYPE_ASL, 2), ctx);
105039c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp[1], tmp[0]), ctx);
105139c13c20SShubham Bansal 	emit(ARM_CMP_I(tmp[1], 0), ctx);
105239c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
105339c13c20SShubham Bansal 
105439c13c20SShubham Bansal 	/* goto *(prog->bpf_func + prologue_size); */
105539c13c20SShubham Bansal 	off = offsetof(struct bpf_prog, bpf_func);
105647b9c3bfSRussell King 	emit_a32_mov_i(tmp2[1], off, ctx);
105739c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp[1], tmp2[1]), ctx);
105839c13c20SShubham Bansal 	emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
1059e9062481SRussell King 	emit_bx_r(tmp[1], ctx);
106039c13c20SShubham Bansal 
106139c13c20SShubham Bansal 	/* out: */
106239c13c20SShubham Bansal 	if (out_offset == -1)
106339c13c20SShubham Bansal 		out_offset = cur_offset;
106439c13c20SShubham Bansal 	if (cur_offset != out_offset) {
106539c13c20SShubham Bansal 		pr_err_once("tail_call out_offset = %d, expected %d!\n",
106639c13c20SShubham Bansal 			    cur_offset, out_offset);
106739c13c20SShubham Bansal 		return -1;
106839c13c20SShubham Bansal 	}
106939c13c20SShubham Bansal 	return 0;
107039c13c20SShubham Bansal #undef cur_offset
107139c13c20SShubham Bansal #undef jmp_offset
107239c13c20SShubham Bansal }
107339c13c20SShubham Bansal 
107439c13c20SShubham Bansal /* 0xabcd => 0xcdab */
107539c13c20SShubham Bansal static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
107639c13c20SShubham Bansal {
107739c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
10781c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
107939c13c20SShubham Bansal 
108039c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
108139c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
108239c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
108339c13c20SShubham Bansal 	emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
108439c13c20SShubham Bansal #else /* ARMv6+ */
108539c13c20SShubham Bansal 	emit(ARM_REV16(rd, rn), ctx);
108639c13c20SShubham Bansal #endif
108739c13c20SShubham Bansal }
108839c13c20SShubham Bansal 
108939c13c20SShubham Bansal /* 0xabcdefgh => 0xghefcdab */
109039c13c20SShubham Bansal static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
109139c13c20SShubham Bansal {
109239c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
10931c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
109439c13c20SShubham Bansal 
109539c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
109639c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
109739c13c20SShubham Bansal 	emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
109839c13c20SShubham Bansal 
109939c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
110039c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
110139c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
110239c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
110339c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
110439c13c20SShubham Bansal 	emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
110539c13c20SShubham Bansal 	emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
110639c13c20SShubham Bansal 
110739c13c20SShubham Bansal #else /* ARMv6+ */
110839c13c20SShubham Bansal 	emit(ARM_REV(rd, rn), ctx);
110939c13c20SShubham Bansal #endif
111039c13c20SShubham Bansal }
111139c13c20SShubham Bansal 
111239c13c20SShubham Bansal // push the scratch stack register on top of the stack
1113*96cced4eSRussell King static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx)
111439c13c20SShubham Bansal {
11151c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
1116*96cced4eSRussell King 	const s8 *rt;
111739c13c20SShubham Bansal 	u16 reg_set = 0;
111839c13c20SShubham Bansal 
1119*96cced4eSRussell King 	rt = arm_bpf_get_reg64(src, tmp2, ctx);
112039c13c20SShubham Bansal 
1121*96cced4eSRussell King 	reg_set = (1 << rt[1]) | (1 << rt[0]);
112239c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
112339c13c20SShubham Bansal }
112439c13c20SShubham Bansal 
112539c13c20SShubham Bansal static void build_prologue(struct jit_ctx *ctx)
112639c13c20SShubham Bansal {
11271c35ba12SRussell King 	const s8 r0 = bpf2a32[BPF_REG_0][1];
11281c35ba12SRussell King 	const s8 r2 = bpf2a32[BPF_REG_1][1];
11291c35ba12SRussell King 	const s8 r3 = bpf2a32[BPF_REG_1][0];
11301c35ba12SRussell King 	const s8 r4 = bpf2a32[BPF_REG_6][1];
11311c35ba12SRussell King 	const s8 fplo = bpf2a32[BPF_REG_FP][1];
11321c35ba12SRussell King 	const s8 fphi = bpf2a32[BPF_REG_FP][0];
11331c35ba12SRussell King 	const s8 *tcc = bpf2a32[TCALL_CNT];
113439c13c20SShubham Bansal 
113539c13c20SShubham Bansal 	/* Save callee saved registers. */
113639c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
113702088d9bSRussell King 	u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
113802088d9bSRussell King 	emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
113939c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
114039c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
114139c13c20SShubham Bansal #else
114202088d9bSRussell King 	emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
114302088d9bSRussell King 	emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
114439c13c20SShubham Bansal #endif
114539c13c20SShubham Bansal 	/* Save frame pointer for later */
114602088d9bSRussell King 	emit(ARM_SUB_I(ARM_IP, ARM_SP, SCRATCH_SIZE), ctx);
114739c13c20SShubham Bansal 
114839c13c20SShubham Bansal 	ctx->stack_size = imm8m(STACK_SIZE);
114939c13c20SShubham Bansal 
115039c13c20SShubham Bansal 	/* Set up function call stack */
115139c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
115239c13c20SShubham Bansal 
115339c13c20SShubham Bansal 	/* Set up BPF prog stack base register */
115447b9c3bfSRussell King 	emit_a32_mov_r(fplo, ARM_IP, ctx);
115547b9c3bfSRussell King 	emit_a32_mov_i(fphi, 0, ctx);
115639c13c20SShubham Bansal 
115739c13c20SShubham Bansal 	/* mov r4, 0 */
115839c13c20SShubham Bansal 	emit(ARM_MOV_I(r4, 0), ctx);
115939c13c20SShubham Bansal 
116039c13c20SShubham Bansal 	/* Move BPF_CTX to BPF_R1 */
116139c13c20SShubham Bansal 	emit(ARM_MOV_R(r3, r4), ctx);
116239c13c20SShubham Bansal 	emit(ARM_MOV_R(r2, r0), ctx);
116339c13c20SShubham Bansal 	/* Initialize Tail Count */
1164*96cced4eSRussell King 	emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[0])), ctx);
1165*96cced4eSRussell King 	emit(ARM_STR_I(r4, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(tcc[1])), ctx);
116639c13c20SShubham Bansal 	/* end of prologue */
116739c13c20SShubham Bansal }
116839c13c20SShubham Bansal 
116902088d9bSRussell King /* restore callee saved registers. */
117039c13c20SShubham Bansal static void build_epilogue(struct jit_ctx *ctx)
117139c13c20SShubham Bansal {
117239c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
117302088d9bSRussell King 	/* When using frame pointers, some additional registers need to
117402088d9bSRussell King 	 * be loaded. */
117502088d9bSRussell King 	u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
117602088d9bSRussell King 	emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
117739c13c20SShubham Bansal 	emit(ARM_LDM(ARM_SP, reg_set), ctx);
117839c13c20SShubham Bansal #else
117939c13c20SShubham Bansal 	/* Restore callee saved registers. */
118002088d9bSRussell King 	emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
118102088d9bSRussell King 	emit(ARM_POP(CALLEE_POP_MASK), ctx);
118239c13c20SShubham Bansal #endif
118339c13c20SShubham Bansal }
118439c13c20SShubham Bansal 
118539c13c20SShubham Bansal /*
118639c13c20SShubham Bansal  * Convert an eBPF instruction to native instruction, i.e
118739c13c20SShubham Bansal  * JITs an eBPF instruction.
118839c13c20SShubham Bansal  * Returns :
118939c13c20SShubham Bansal  *	0  - Successfully JITed an 8-byte eBPF instruction
119039c13c20SShubham Bansal  *	>0 - Successfully JITed a 16-byte eBPF instruction
119139c13c20SShubham Bansal  *	<0 - Failed to JIT.
119239c13c20SShubham Bansal  */
119339c13c20SShubham Bansal static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
119439c13c20SShubham Bansal {
119539c13c20SShubham Bansal 	const u8 code = insn->code;
11961c35ba12SRussell King 	const s8 *dst = bpf2a32[insn->dst_reg];
11971c35ba12SRussell King 	const s8 *src = bpf2a32[insn->src_reg];
11981c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
11991c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
120039c13c20SShubham Bansal 	const s16 off = insn->off;
120139c13c20SShubham Bansal 	const s32 imm = insn->imm;
120239c13c20SShubham Bansal 	const int i = insn - ctx->prog->insnsi;
120339c13c20SShubham Bansal 	const bool is64 = BPF_CLASS(code) == BPF_ALU64;
1204a6eccac5SRussell King 	const s8 *rd, *rs;
1205a6eccac5SRussell King 	s8 rd_lo, rt, rm, rn;
120639c13c20SShubham Bansal 	s32 jmp_offset;
120739c13c20SShubham Bansal 
120839c13c20SShubham Bansal #define check_imm(bits, imm) do {				\
12092b589a7eSWang YanQing 	if ((imm) >= (1 << ((bits) - 1)) ||			\
12102b589a7eSWang YanQing 	    (imm) < -(1 << ((bits) - 1))) {			\
121139c13c20SShubham Bansal 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
121239c13c20SShubham Bansal 			i, imm, imm);				\
121339c13c20SShubham Bansal 		return -EINVAL;					\
121439c13c20SShubham Bansal 	}							\
121539c13c20SShubham Bansal } while (0)
121639c13c20SShubham Bansal #define check_imm24(imm) check_imm(24, imm)
1217ddecdfceSMircea Gherzan 
121834805931SDaniel Borkmann 	switch (code) {
121939c13c20SShubham Bansal 	/* ALU operations */
1220ddecdfceSMircea Gherzan 
122139c13c20SShubham Bansal 	/* dst = src */
122239c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_K:
122339c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_X:
122439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_K:
122539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_X:
122639c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
122739c13c20SShubham Bansal 		case BPF_X:
122847b9c3bfSRussell King 			emit_a32_mov_r64(is64, dst, src, ctx);
122939c13c20SShubham Bansal 			break;
123039c13c20SShubham Bansal 		case BPF_K:
123139c13c20SShubham Bansal 			/* Sign-extend immediate value to destination reg */
123247b9c3bfSRussell King 			emit_a32_mov_i64(is64, dst, imm, ctx);
123339c13c20SShubham Bansal 			break;
1234ddecdfceSMircea Gherzan 		}
1235ddecdfceSMircea Gherzan 		break;
123639c13c20SShubham Bansal 	/* dst = dst + src/imm */
123739c13c20SShubham Bansal 	/* dst = dst - src/imm */
123839c13c20SShubham Bansal 	/* dst = dst | src/imm */
123939c13c20SShubham Bansal 	/* dst = dst & src/imm */
124039c13c20SShubham Bansal 	/* dst = dst ^ src/imm */
124139c13c20SShubham Bansal 	/* dst = dst * src/imm */
124239c13c20SShubham Bansal 	/* dst = dst << src */
124339c13c20SShubham Bansal 	/* dst = dst >> src */
124434805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_K:
124534805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_X:
124634805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_K:
124734805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_X:
124834805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_K:
124934805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_X:
125034805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_K:
125134805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_X:
125239c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_K:
125339c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_X:
125439c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_K:
125539c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_X:
125634805931SDaniel Borkmann 	case BPF_ALU | BPF_LSH | BPF_X:
125734805931SDaniel Borkmann 	case BPF_ALU | BPF_RSH | BPF_X:
125839c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_K:
125939c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_X:
126039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_K:
126139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_X:
126239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_K:
126339c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_X:
126439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_K:
126539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_X:
126639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_K:
126739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_X:
126839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_K:
126939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_X:
127039c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
127139c13c20SShubham Bansal 		case BPF_X:
127247b9c3bfSRussell King 			emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code));
1273ddecdfceSMircea Gherzan 			break;
127439c13c20SShubham Bansal 		case BPF_K:
127539c13c20SShubham Bansal 			/* Move immediate value to the temporary register
127639c13c20SShubham Bansal 			 * and then do the ALU operation on the temporary
127739c13c20SShubham Bansal 			 * register as this will sign-extend the immediate
127839c13c20SShubham Bansal 			 * value into temporary reg and then it would be
127939c13c20SShubham Bansal 			 * safe to do the operation on it.
128039c13c20SShubham Bansal 			 */
128147b9c3bfSRussell King 			emit_a32_mov_i64(is64, tmp2, imm, ctx);
128247b9c3bfSRussell King 			emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code));
128339c13c20SShubham Bansal 			break;
128439c13c20SShubham Bansal 		}
128539c13c20SShubham Bansal 		break;
128639c13c20SShubham Bansal 	/* dst = dst / src(imm) */
128739c13c20SShubham Bansal 	/* dst = dst % src(imm) */
128839c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_K:
128939c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_X:
129039c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_K:
129139c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_X:
1292a6eccac5SRussell King 		rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx);
129339c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
129439c13c20SShubham Bansal 		case BPF_X:
12957a987025SRussell King 			rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx);
129639c13c20SShubham Bansal 			break;
129739c13c20SShubham Bansal 		case BPF_K:
129839c13c20SShubham Bansal 			rt = tmp2[0];
129947b9c3bfSRussell King 			emit_a32_mov_i(rt, imm, ctx);
130047b9c3bfSRussell King 			break;
130147b9c3bfSRussell King 		default:
130247b9c3bfSRussell King 			rt = src_lo;
130339c13c20SShubham Bansal 			break;
130439c13c20SShubham Bansal 		}
1305a6eccac5SRussell King 		emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code));
1306a6eccac5SRussell King 		arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
130747b9c3bfSRussell King 		emit_a32_mov_i(dst_hi, 0, ctx);
130839c13c20SShubham Bansal 		break;
130939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_K:
131039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_X:
131139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_K:
131239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_X:
131339c13c20SShubham Bansal 		goto notyet;
131439c13c20SShubham Bansal 	/* dst = dst >> imm */
131539c13c20SShubham Bansal 	/* dst = dst << imm */
131639c13c20SShubham Bansal 	case BPF_ALU | BPF_RSH | BPF_K:
131739c13c20SShubham Bansal 	case BPF_ALU | BPF_LSH | BPF_K:
131839c13c20SShubham Bansal 		if (unlikely(imm > 31))
131939c13c20SShubham Bansal 			return -EINVAL;
132039c13c20SShubham Bansal 		if (imm)
132147b9c3bfSRussell King 			emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
132247b9c3bfSRussell King 		emit_a32_mov_i(dst_hi, 0, ctx);
132339c13c20SShubham Bansal 		break;
132439c13c20SShubham Bansal 	/* dst = dst << imm */
132539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_K:
132639c13c20SShubham Bansal 		if (unlikely(imm > 63))
132739c13c20SShubham Bansal 			return -EINVAL;
132847b9c3bfSRussell King 		emit_a32_lsh_i64(dst, imm, ctx);
132939c13c20SShubham Bansal 		break;
133039c13c20SShubham Bansal 	/* dst = dst >> imm */
133139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_K:
133239c13c20SShubham Bansal 		if (unlikely(imm > 63))
133339c13c20SShubham Bansal 			return -EINVAL;
133447b9c3bfSRussell King 		emit_a32_rsh_i64(dst, imm, ctx);
133539c13c20SShubham Bansal 		break;
133639c13c20SShubham Bansal 	/* dst = dst << src */
133739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_X:
133847b9c3bfSRussell King 		emit_a32_lsh_r64(dst, src, ctx);
133939c13c20SShubham Bansal 		break;
134039c13c20SShubham Bansal 	/* dst = dst >> src */
134139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_X:
134247b9c3bfSRussell King 		emit_a32_rsh_r64(dst, src, ctx);
134339c13c20SShubham Bansal 		break;
134439c13c20SShubham Bansal 	/* dst = dst >> src (signed) */
134539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_X:
134647b9c3bfSRussell King 		emit_a32_arsh_r64(dst, src, ctx);
134739c13c20SShubham Bansal 		break;
134839c13c20SShubham Bansal 	/* dst = dst >> imm (signed) */
134939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_K:
135039c13c20SShubham Bansal 		if (unlikely(imm > 63))
135139c13c20SShubham Bansal 			return -EINVAL;
135247b9c3bfSRussell King 		emit_a32_arsh_i64(dst, imm, ctx);
135339c13c20SShubham Bansal 		break;
135439c13c20SShubham Bansal 	/* dst = ~dst */
135534805931SDaniel Borkmann 	case BPF_ALU | BPF_NEG:
135647b9c3bfSRussell King 		emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
135747b9c3bfSRussell King 		emit_a32_mov_i(dst_hi, 0, ctx);
1358ddecdfceSMircea Gherzan 		break;
135939c13c20SShubham Bansal 	/* dst = ~dst (64 bit) */
136039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_NEG:
136147b9c3bfSRussell King 		emit_a32_neg64(dst, ctx);
1362ddecdfceSMircea Gherzan 		break;
136339c13c20SShubham Bansal 	/* dst = dst * src/imm */
136439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_X:
136539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_K:
136639c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
136739c13c20SShubham Bansal 		case BPF_X:
136847b9c3bfSRussell King 			emit_a32_mul_r64(dst, src, ctx);
1369ddecdfceSMircea Gherzan 			break;
137039c13c20SShubham Bansal 		case BPF_K:
137139c13c20SShubham Bansal 			/* Move immediate value to the temporary register
137239c13c20SShubham Bansal 			 * and then do the multiplication on it as this
137339c13c20SShubham Bansal 			 * will sign-extend the immediate value into temp
137439c13c20SShubham Bansal 			 * reg then it would be safe to do the operation
137539c13c20SShubham Bansal 			 * on it.
13765bf705b4SNicolas Schichan 			 */
137747b9c3bfSRussell King 			emit_a32_mov_i64(is64, tmp2, imm, ctx);
137847b9c3bfSRussell King 			emit_a32_mul_r64(dst, tmp2, ctx);
137939c13c20SShubham Bansal 			break;
13805bf705b4SNicolas Schichan 		}
1381ddecdfceSMircea Gherzan 		break;
138239c13c20SShubham Bansal 	/* dst = htole(dst) */
138339c13c20SShubham Bansal 	/* dst = htobe(dst) */
138439c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_LE:
138539c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_BE:
1386a6eccac5SRussell King 		rd = arm_bpf_get_reg64(dst, tmp, ctx);
138739c13c20SShubham Bansal 		if (BPF_SRC(code) == BPF_FROM_LE)
138839c13c20SShubham Bansal 			goto emit_bswap_uxt;
138939c13c20SShubham Bansal 		switch (imm) {
139039c13c20SShubham Bansal 		case 16:
1391a6eccac5SRussell King 			emit_rev16(rd[1], rd[1], ctx);
139239c13c20SShubham Bansal 			goto emit_bswap_uxt;
139339c13c20SShubham Bansal 		case 32:
1394a6eccac5SRussell King 			emit_rev32(rd[1], rd[1], ctx);
139539c13c20SShubham Bansal 			goto emit_bswap_uxt;
139639c13c20SShubham Bansal 		case 64:
1397a6eccac5SRussell King 			emit_rev32(ARM_LR, rd[1], ctx);
1398a6eccac5SRussell King 			emit_rev32(rd[1], rd[0], ctx);
1399a6eccac5SRussell King 			emit(ARM_MOV_R(rd[0], ARM_LR), ctx);
1400bf0098f2SDaniel Borkmann 			break;
140139c13c20SShubham Bansal 		}
140239c13c20SShubham Bansal 		goto exit;
140339c13c20SShubham Bansal emit_bswap_uxt:
140439c13c20SShubham Bansal 		switch (imm) {
140539c13c20SShubham Bansal 		case 16:
140639c13c20SShubham Bansal 			/* zero-extend 16 bits into 64 bits */
140739c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
140847b9c3bfSRussell King 			emit_a32_mov_i(tmp2[1], 0xffff, ctx);
1409a6eccac5SRussell King 			emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx);
141039c13c20SShubham Bansal #else /* ARMv6+ */
1411a6eccac5SRussell King 			emit(ARM_UXTH(rd[1], rd[1]), ctx);
14121447f93fSNicolas Schichan #endif
1413a6eccac5SRussell King 			emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
14141447f93fSNicolas Schichan 			break;
141539c13c20SShubham Bansal 		case 32:
141639c13c20SShubham Bansal 			/* zero-extend 32 bits into 64 bits */
1417a6eccac5SRussell King 			emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
1418ddecdfceSMircea Gherzan 			break;
141939c13c20SShubham Bansal 		case 64:
142039c13c20SShubham Bansal 			/* nop */
142139c13c20SShubham Bansal 			break;
142239c13c20SShubham Bansal 		}
142339c13c20SShubham Bansal exit:
1424a6eccac5SRussell King 		arm_bpf_put_reg64(dst, rd, ctx);
142539c13c20SShubham Bansal 		break;
142639c13c20SShubham Bansal 	/* dst = imm64 */
142739c13c20SShubham Bansal 	case BPF_LD | BPF_IMM | BPF_DW:
142839c13c20SShubham Bansal 	{
142939c13c20SShubham Bansal 		const struct bpf_insn insn1 = insn[1];
143039c13c20SShubham Bansal 		u32 hi, lo = imm;
1431303249abSNicolas Schichan 
143239c13c20SShubham Bansal 		hi = insn1.imm;
143347b9c3bfSRussell King 		emit_a32_mov_i(dst_lo, lo, ctx);
143447b9c3bfSRussell King 		emit_a32_mov_i(dst_hi, hi, ctx);
143539c13c20SShubham Bansal 
143639c13c20SShubham Bansal 		return 1;
143739c13c20SShubham Bansal 	}
143839c13c20SShubham Bansal 	/* LDX: dst = *(size *)(src + off) */
143939c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_W:
144039c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_H:
144139c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_B:
144239c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_DW:
14437a987025SRussell King 		rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
144447b9c3bfSRussell King 		emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code));
144539c13c20SShubham Bansal 		break;
144639c13c20SShubham Bansal 	/* ST: *(size *)(dst + off) = imm */
144739c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_W:
144839c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_H:
144939c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_B:
145039c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_DW:
145139c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
145239c13c20SShubham Bansal 		case BPF_DW:
145339c13c20SShubham Bansal 			/* Sign-extend immediate value into temp reg */
145447b9c3bfSRussell King 			emit_a32_mov_i64(true, tmp2, imm, ctx);
145547b9c3bfSRussell King 			emit_str_r(dst_lo, tmp2[1], off, ctx, BPF_W);
145647b9c3bfSRussell King 			emit_str_r(dst_lo, tmp2[0], off+4, ctx, BPF_W);
145739c13c20SShubham Bansal 			break;
145839c13c20SShubham Bansal 		case BPF_W:
145939c13c20SShubham Bansal 		case BPF_H:
146039c13c20SShubham Bansal 		case BPF_B:
146147b9c3bfSRussell King 			emit_a32_mov_i(tmp2[1], imm, ctx);
146247b9c3bfSRussell King 			emit_str_r(dst_lo, tmp2[1], off, ctx, BPF_SIZE(code));
146339c13c20SShubham Bansal 			break;
146439c13c20SShubham Bansal 		}
146539c13c20SShubham Bansal 		break;
146639c13c20SShubham Bansal 	/* STX XADD: lock *(u32 *)(dst + off) += src */
146739c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_W:
146839c13c20SShubham Bansal 	/* STX XADD: lock *(u64 *)(dst + off) += src */
146939c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_DW:
147039c13c20SShubham Bansal 		goto notyet;
147139c13c20SShubham Bansal 	/* STX: *(size *)(dst + off) = src */
147239c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_W:
147339c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_H:
147439c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_B:
147539c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_DW:
147639c13c20SShubham Bansal 	{
147739c13c20SShubham Bansal 		u8 sz = BPF_SIZE(code);
147839c13c20SShubham Bansal 
1479a6eccac5SRussell King 		rs = arm_bpf_get_reg64(src, tmp2, ctx);
148039c13c20SShubham Bansal 
148139c13c20SShubham Bansal 		/* Store the value */
148239c13c20SShubham Bansal 		if (BPF_SIZE(code) == BPF_DW) {
1483a6eccac5SRussell King 			emit_str_r(dst_lo, rs[1], off, ctx, BPF_W);
1484a6eccac5SRussell King 			emit_str_r(dst_lo, rs[0], off+4, ctx, BPF_W);
148539c13c20SShubham Bansal 		} else {
1486a6eccac5SRussell King 			emit_str_r(dst_lo, rs[1], off, ctx, sz);
148739c13c20SShubham Bansal 		}
148839c13c20SShubham Bansal 		break;
148939c13c20SShubham Bansal 	}
149039c13c20SShubham Bansal 	/* PC += off if dst == src */
149139c13c20SShubham Bansal 	/* PC += off if dst > src */
149239c13c20SShubham Bansal 	/* PC += off if dst >= src */
149339c13c20SShubham Bansal 	/* PC += off if dst < src */
149439c13c20SShubham Bansal 	/* PC += off if dst <= src */
149539c13c20SShubham Bansal 	/* PC += off if dst != src */
149639c13c20SShubham Bansal 	/* PC += off if dst > src (signed) */
149739c13c20SShubham Bansal 	/* PC += off if dst >= src (signed) */
149839c13c20SShubham Bansal 	/* PC += off if dst < src (signed) */
149939c13c20SShubham Bansal 	/* PC += off if dst <= src (signed) */
150039c13c20SShubham Bansal 	/* PC += off if dst & src */
150139c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_X:
150239c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_X:
150339c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_X:
150439c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_X:
150539c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_X:
150639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_X:
150739c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_X:
150839c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_X:
150939c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_X:
151039c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_X:
151139c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_X:
151239c13c20SShubham Bansal 		/* Setup source registers */
15137a987025SRussell King 		rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
15147a987025SRussell King 		rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
151539c13c20SShubham Bansal 		goto go_jmp;
151639c13c20SShubham Bansal 	/* PC += off if dst == imm */
151739c13c20SShubham Bansal 	/* PC += off if dst > imm */
151839c13c20SShubham Bansal 	/* PC += off if dst >= imm */
151939c13c20SShubham Bansal 	/* PC += off if dst < imm */
152039c13c20SShubham Bansal 	/* PC += off if dst <= imm */
152139c13c20SShubham Bansal 	/* PC += off if dst != imm */
152239c13c20SShubham Bansal 	/* PC += off if dst > imm (signed) */
152339c13c20SShubham Bansal 	/* PC += off if dst >= imm (signed) */
152439c13c20SShubham Bansal 	/* PC += off if dst < imm (signed) */
152539c13c20SShubham Bansal 	/* PC += off if dst <= imm (signed) */
152639c13c20SShubham Bansal 	/* PC += off if dst & imm */
152739c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_K:
152839c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_K:
152939c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_K:
153039c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_K:
153139c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_K:
153239c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_K:
153339c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_K:
153439c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_K:
153539c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_K:
153639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_K:
153739c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_K:
153839c13c20SShubham Bansal 		if (off == 0)
153939c13c20SShubham Bansal 			break;
154039c13c20SShubham Bansal 		rm = tmp2[0];
154139c13c20SShubham Bansal 		rn = tmp2[1];
154239c13c20SShubham Bansal 		/* Sign-extend immediate value */
154347b9c3bfSRussell King 		emit_a32_mov_i64(true, tmp2, imm, ctx);
154439c13c20SShubham Bansal go_jmp:
154539c13c20SShubham Bansal 		/* Setup destination register */
1546a6eccac5SRussell King 		rd = arm_bpf_get_reg64(dst, tmp, ctx);
154739c13c20SShubham Bansal 
154839c13c20SShubham Bansal 		/* Check for the condition */
1549a6eccac5SRussell King 		emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code));
155039c13c20SShubham Bansal 
155139c13c20SShubham Bansal 		/* Setup JUMP instruction */
155239c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
155339c13c20SShubham Bansal 		switch (BPF_OP(code)) {
155439c13c20SShubham Bansal 		case BPF_JNE:
155539c13c20SShubham Bansal 		case BPF_JSET:
155639c13c20SShubham Bansal 			_emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
155739c13c20SShubham Bansal 			break;
155839c13c20SShubham Bansal 		case BPF_JEQ:
155939c13c20SShubham Bansal 			_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
156039c13c20SShubham Bansal 			break;
156139c13c20SShubham Bansal 		case BPF_JGT:
156239c13c20SShubham Bansal 			_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
156339c13c20SShubham Bansal 			break;
156439c13c20SShubham Bansal 		case BPF_JGE:
156539c13c20SShubham Bansal 			_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
156639c13c20SShubham Bansal 			break;
156739c13c20SShubham Bansal 		case BPF_JSGT:
156839c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
156939c13c20SShubham Bansal 			break;
157039c13c20SShubham Bansal 		case BPF_JSGE:
157139c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
157239c13c20SShubham Bansal 			break;
157339c13c20SShubham Bansal 		case BPF_JLE:
157439c13c20SShubham Bansal 			_emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
157539c13c20SShubham Bansal 			break;
157639c13c20SShubham Bansal 		case BPF_JLT:
157739c13c20SShubham Bansal 			_emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
157839c13c20SShubham Bansal 			break;
157939c13c20SShubham Bansal 		case BPF_JSLT:
158039c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
158139c13c20SShubham Bansal 			break;
158239c13c20SShubham Bansal 		case BPF_JSLE:
158339c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
158439c13c20SShubham Bansal 			break;
158539c13c20SShubham Bansal 		}
158639c13c20SShubham Bansal 		break;
158739c13c20SShubham Bansal 	/* JMP OFF */
158839c13c20SShubham Bansal 	case BPF_JMP | BPF_JA:
158939c13c20SShubham Bansal 	{
159039c13c20SShubham Bansal 		if (off == 0)
159139c13c20SShubham Bansal 			break;
159239c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
159339c13c20SShubham Bansal 		check_imm24(jmp_offset);
159439c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
159539c13c20SShubham Bansal 		break;
159639c13c20SShubham Bansal 	}
159739c13c20SShubham Bansal 	/* tail call */
159839c13c20SShubham Bansal 	case BPF_JMP | BPF_TAIL_CALL:
159939c13c20SShubham Bansal 		if (emit_bpf_tail_call(ctx))
160039c13c20SShubham Bansal 			return -EFAULT;
160139c13c20SShubham Bansal 		break;
160239c13c20SShubham Bansal 	/* function call */
160339c13c20SShubham Bansal 	case BPF_JMP | BPF_CALL:
160439c13c20SShubham Bansal 	{
16051c35ba12SRussell King 		const s8 *r0 = bpf2a32[BPF_REG_0];
16061c35ba12SRussell King 		const s8 *r1 = bpf2a32[BPF_REG_1];
16071c35ba12SRussell King 		const s8 *r2 = bpf2a32[BPF_REG_2];
16081c35ba12SRussell King 		const s8 *r3 = bpf2a32[BPF_REG_3];
16091c35ba12SRussell King 		const s8 *r4 = bpf2a32[BPF_REG_4];
16101c35ba12SRussell King 		const s8 *r5 = bpf2a32[BPF_REG_5];
161139c13c20SShubham Bansal 		const u32 func = (u32)__bpf_call_base + (u32)imm;
161239c13c20SShubham Bansal 
161347b9c3bfSRussell King 		emit_a32_mov_r64(true, r0, r1, ctx);
161447b9c3bfSRussell King 		emit_a32_mov_r64(true, r1, r2, ctx);
1615*96cced4eSRussell King 		emit_push_r64(r5, ctx);
1616*96cced4eSRussell King 		emit_push_r64(r4, ctx);
1617*96cced4eSRussell King 		emit_push_r64(r3, ctx);
161839c13c20SShubham Bansal 
161947b9c3bfSRussell King 		emit_a32_mov_i(tmp[1], func, ctx);
162039c13c20SShubham Bansal 		emit_blx_r(tmp[1], ctx);
162139c13c20SShubham Bansal 
162239c13c20SShubham Bansal 		emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
162339c13c20SShubham Bansal 		break;
162439c13c20SShubham Bansal 	}
162539c13c20SShubham Bansal 	/* function return */
162639c13c20SShubham Bansal 	case BPF_JMP | BPF_EXIT:
162739c13c20SShubham Bansal 		/* Optimization: when last instruction is EXIT
162839c13c20SShubham Bansal 		 * simply fallthrough to epilogue.
162939c13c20SShubham Bansal 		 */
163039c13c20SShubham Bansal 		if (i == ctx->prog->len - 1)
163139c13c20SShubham Bansal 			break;
163239c13c20SShubham Bansal 		jmp_offset = epilogue_offset(ctx);
163339c13c20SShubham Bansal 		check_imm24(jmp_offset);
163439c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
163539c13c20SShubham Bansal 		break;
163639c13c20SShubham Bansal notyet:
163739c13c20SShubham Bansal 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
163839c13c20SShubham Bansal 		return -EFAULT;
163939c13c20SShubham Bansal 	default:
164039c13c20SShubham Bansal 		pr_err_once("unknown opcode %02x\n", code);
164139c13c20SShubham Bansal 		return -EINVAL;
1642ddecdfceSMircea Gherzan 	}
16430b59d880SNicolas Schichan 
16440b59d880SNicolas Schichan 	if (ctx->flags & FLAG_IMM_OVERFLOW)
16450b59d880SNicolas Schichan 		/*
16460b59d880SNicolas Schichan 		 * this instruction generated an overflow when
16470b59d880SNicolas Schichan 		 * trying to access the literal pool, so
16480b59d880SNicolas Schichan 		 * delegate this filter to the kernel interpreter.
16490b59d880SNicolas Schichan 		 */
16500b59d880SNicolas Schichan 		return -1;
165139c13c20SShubham Bansal 	return 0;
1652ddecdfceSMircea Gherzan }
1653ddecdfceSMircea Gherzan 
165439c13c20SShubham Bansal static int build_body(struct jit_ctx *ctx)
165539c13c20SShubham Bansal {
165639c13c20SShubham Bansal 	const struct bpf_prog *prog = ctx->prog;
165739c13c20SShubham Bansal 	unsigned int i;
165839c13c20SShubham Bansal 
165939c13c20SShubham Bansal 	for (i = 0; i < prog->len; i++) {
166039c13c20SShubham Bansal 		const struct bpf_insn *insn = &(prog->insnsi[i]);
166139c13c20SShubham Bansal 		int ret;
166239c13c20SShubham Bansal 
166339c13c20SShubham Bansal 		ret = build_insn(insn, ctx);
166439c13c20SShubham Bansal 
166539c13c20SShubham Bansal 		/* It's used with loading the 64 bit immediate value. */
166639c13c20SShubham Bansal 		if (ret > 0) {
166739c13c20SShubham Bansal 			i++;
1668ddecdfceSMircea Gherzan 			if (ctx->target == NULL)
166939c13c20SShubham Bansal 				ctx->offsets[i] = ctx->idx;
167039c13c20SShubham Bansal 			continue;
167139c13c20SShubham Bansal 		}
167239c13c20SShubham Bansal 
167339c13c20SShubham Bansal 		if (ctx->target == NULL)
167439c13c20SShubham Bansal 			ctx->offsets[i] = ctx->idx;
167539c13c20SShubham Bansal 
167639c13c20SShubham Bansal 		/* If unsuccesfull, return with error code */
167739c13c20SShubham Bansal 		if (ret)
167839c13c20SShubham Bansal 			return ret;
167939c13c20SShubham Bansal 	}
168039c13c20SShubham Bansal 	return 0;
168139c13c20SShubham Bansal }
168239c13c20SShubham Bansal 
168339c13c20SShubham Bansal static int validate_code(struct jit_ctx *ctx)
168439c13c20SShubham Bansal {
168539c13c20SShubham Bansal 	int i;
168639c13c20SShubham Bansal 
168739c13c20SShubham Bansal 	for (i = 0; i < ctx->idx; i++) {
168839c13c20SShubham Bansal 		if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
168939c13c20SShubham Bansal 			return -1;
169039c13c20SShubham Bansal 	}
1691ddecdfceSMircea Gherzan 
1692ddecdfceSMircea Gherzan 	return 0;
1693ddecdfceSMircea Gherzan }
1694ddecdfceSMircea Gherzan 
169539c13c20SShubham Bansal void bpf_jit_compile(struct bpf_prog *prog)
1696ddecdfceSMircea Gherzan {
169739c13c20SShubham Bansal 	/* Nothing to do here. We support Internal BPF. */
169839c13c20SShubham Bansal }
1699ddecdfceSMircea Gherzan 
170039c13c20SShubham Bansal struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
170139c13c20SShubham Bansal {
170239c13c20SShubham Bansal 	struct bpf_prog *tmp, *orig_prog = prog;
170339c13c20SShubham Bansal 	struct bpf_binary_header *header;
170439c13c20SShubham Bansal 	bool tmp_blinded = false;
170539c13c20SShubham Bansal 	struct jit_ctx ctx;
170639c13c20SShubham Bansal 	unsigned int tmp_idx;
170739c13c20SShubham Bansal 	unsigned int image_size;
170839c13c20SShubham Bansal 	u8 *image_ptr;
170939c13c20SShubham Bansal 
171039c13c20SShubham Bansal 	/* If BPF JIT was not enabled then we must fall back to
171139c13c20SShubham Bansal 	 * the interpreter.
171239c13c20SShubham Bansal 	 */
171360b58afcSAlexei Starovoitov 	if (!prog->jit_requested)
171439c13c20SShubham Bansal 		return orig_prog;
171539c13c20SShubham Bansal 
171639c13c20SShubham Bansal 	/* If constant blinding was enabled and we failed during blinding
171739c13c20SShubham Bansal 	 * then we must fall back to the interpreter. Otherwise, we save
171839c13c20SShubham Bansal 	 * the new JITed code.
171939c13c20SShubham Bansal 	 */
172039c13c20SShubham Bansal 	tmp = bpf_jit_blind_constants(prog);
172139c13c20SShubham Bansal 
172239c13c20SShubham Bansal 	if (IS_ERR(tmp))
172339c13c20SShubham Bansal 		return orig_prog;
172439c13c20SShubham Bansal 	if (tmp != prog) {
172539c13c20SShubham Bansal 		tmp_blinded = true;
172639c13c20SShubham Bansal 		prog = tmp;
172739c13c20SShubham Bansal 	}
1728ddecdfceSMircea Gherzan 
1729ddecdfceSMircea Gherzan 	memset(&ctx, 0, sizeof(ctx));
173039c13c20SShubham Bansal 	ctx.prog = prog;
1731ddecdfceSMircea Gherzan 
173239c13c20SShubham Bansal 	/* Not able to allocate memory for offsets[] , then
173339c13c20SShubham Bansal 	 * we must fall back to the interpreter
173439c13c20SShubham Bansal 	 */
173539c13c20SShubham Bansal 	ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
173639c13c20SShubham Bansal 	if (ctx.offsets == NULL) {
173739c13c20SShubham Bansal 		prog = orig_prog;
1738ddecdfceSMircea Gherzan 		goto out;
173939c13c20SShubham Bansal 	}
174039c13c20SShubham Bansal 
174139c13c20SShubham Bansal 	/* 1) fake pass to find in the length of the JITed code,
174239c13c20SShubham Bansal 	 * to compute ctx->offsets and other context variables
174339c13c20SShubham Bansal 	 * needed to compute final JITed code.
174439c13c20SShubham Bansal 	 * Also, calculate random starting pointer/start of JITed code
174539c13c20SShubham Bansal 	 * which is prefixed by random number of fault instructions.
174639c13c20SShubham Bansal 	 *
174739c13c20SShubham Bansal 	 * If the first pass fails then there is no chance of it
174839c13c20SShubham Bansal 	 * being successful in the second pass, so just fall back
174939c13c20SShubham Bansal 	 * to the interpreter.
175039c13c20SShubham Bansal 	 */
175139c13c20SShubham Bansal 	if (build_body(&ctx)) {
175239c13c20SShubham Bansal 		prog = orig_prog;
175339c13c20SShubham Bansal 		goto out_off;
175439c13c20SShubham Bansal 	}
1755ddecdfceSMircea Gherzan 
1756ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1757ddecdfceSMircea Gherzan 	build_prologue(&ctx);
1758ddecdfceSMircea Gherzan 	ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1759ddecdfceSMircea Gherzan 
176039c13c20SShubham Bansal 	ctx.epilogue_offset = ctx.idx;
176139c13c20SShubham Bansal 
1762ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1763ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1764ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1765ddecdfceSMircea Gherzan 	ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1766ddecdfceSMircea Gherzan 
1767ddecdfceSMircea Gherzan 	ctx.idx += ctx.imm_count;
1768ddecdfceSMircea Gherzan 	if (ctx.imm_count) {
176939c13c20SShubham Bansal 		ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
177039c13c20SShubham Bansal 		if (ctx.imms == NULL) {
177139c13c20SShubham Bansal 			prog = orig_prog;
177239c13c20SShubham Bansal 			goto out_off;
177339c13c20SShubham Bansal 		}
1774ddecdfceSMircea Gherzan 	}
1775ddecdfceSMircea Gherzan #else
177639c13c20SShubham Bansal 	/* there's nothing about the epilogue on ARMv7 */
1777ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1778ddecdfceSMircea Gherzan #endif
177939c13c20SShubham Bansal 	/* Now we can get the actual image size of the JITed arm code.
178039c13c20SShubham Bansal 	 * Currently, we are not considering the THUMB-2 instructions
178139c13c20SShubham Bansal 	 * for jit, although it can decrease the size of the image.
178239c13c20SShubham Bansal 	 *
178339c13c20SShubham Bansal 	 * As each arm instruction is of length 32bit, we are translating
178439c13c20SShubham Bansal 	 * number of JITed intructions into the size required to store these
178539c13c20SShubham Bansal 	 * JITed code.
178639c13c20SShubham Bansal 	 */
178739c13c20SShubham Bansal 	image_size = sizeof(u32) * ctx.idx;
1788ddecdfceSMircea Gherzan 
178939c13c20SShubham Bansal 	/* Now we know the size of the structure to make */
179039c13c20SShubham Bansal 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
179139c13c20SShubham Bansal 				      sizeof(u32), jit_fill_hole);
179239c13c20SShubham Bansal 	/* Not able to allocate memory for the structure then
179339c13c20SShubham Bansal 	 * we must fall back to the interpretation
179439c13c20SShubham Bansal 	 */
179539c13c20SShubham Bansal 	if (header == NULL) {
179639c13c20SShubham Bansal 		prog = orig_prog;
179739c13c20SShubham Bansal 		goto out_imms;
179839c13c20SShubham Bansal 	}
179939c13c20SShubham Bansal 
180039c13c20SShubham Bansal 	/* 2.) Actual pass to generate final JIT code */
180139c13c20SShubham Bansal 	ctx.target = (u32 *) image_ptr;
1802ddecdfceSMircea Gherzan 	ctx.idx = 0;
180355309dd3SDaniel Borkmann 
1804ddecdfceSMircea Gherzan 	build_prologue(&ctx);
180539c13c20SShubham Bansal 
180639c13c20SShubham Bansal 	/* If building the body of the JITed code fails somehow,
180739c13c20SShubham Bansal 	 * we fall back to the interpretation.
180839c13c20SShubham Bansal 	 */
18090b59d880SNicolas Schichan 	if (build_body(&ctx) < 0) {
181039c13c20SShubham Bansal 		image_ptr = NULL;
18110b59d880SNicolas Schichan 		bpf_jit_binary_free(header);
181239c13c20SShubham Bansal 		prog = orig_prog;
181339c13c20SShubham Bansal 		goto out_imms;
18140b59d880SNicolas Schichan 	}
1815ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1816ddecdfceSMircea Gherzan 
181739c13c20SShubham Bansal 	/* 3.) Extra pass to validate JITed Code */
181839c13c20SShubham Bansal 	if (validate_code(&ctx)) {
181939c13c20SShubham Bansal 		image_ptr = NULL;
182039c13c20SShubham Bansal 		bpf_jit_binary_free(header);
182139c13c20SShubham Bansal 		prog = orig_prog;
182239c13c20SShubham Bansal 		goto out_imms;
182339c13c20SShubham Bansal 	}
1824ebaef649SDaniel Borkmann 	flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
1825ddecdfceSMircea Gherzan 
182639c13c20SShubham Bansal 	if (bpf_jit_enable > 1)
182739c13c20SShubham Bansal 		/* there are 2 passes here */
182839c13c20SShubham Bansal 		bpf_jit_dump(prog->len, image_size, 2, ctx.target);
182939c13c20SShubham Bansal 
183018d405afSDaniel Borkmann 	bpf_jit_binary_lock_ro(header);
183139c13c20SShubham Bansal 	prog->bpf_func = (void *)ctx.target;
183239c13c20SShubham Bansal 	prog->jited = 1;
183339c13c20SShubham Bansal 	prog->jited_len = image_size;
183439c13c20SShubham Bansal 
183539c13c20SShubham Bansal out_imms:
1836ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1837ddecdfceSMircea Gherzan 	if (ctx.imm_count)
1838ddecdfceSMircea Gherzan 		kfree(ctx.imms);
1839ddecdfceSMircea Gherzan #endif
184039c13c20SShubham Bansal out_off:
1841ddecdfceSMircea Gherzan 	kfree(ctx.offsets);
184239c13c20SShubham Bansal out:
184339c13c20SShubham Bansal 	if (tmp_blinded)
184439c13c20SShubham Bansal 		bpf_jit_prog_release_other(prog, prog == orig_prog ?
184539c13c20SShubham Bansal 					   tmp : orig_prog);
184639c13c20SShubham Bansal 	return prog;
1847ddecdfceSMircea Gherzan }
1848ddecdfceSMircea Gherzan 
1849