xref: /openbmc/linux/arch/arm/net/bpf_jit_32.c (revision a8ef95a034233190b1dd73ff03472ff0f7f4fbdf)
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 
104d449ceb1SRussell King #define STACK_OFFSET(k)	((k) * 4)
105d449ceb1SRussell King #define SCRATCH_SIZE	(BPF_JIT_SCRATCH_REGS * 4)
106d449ceb1SRussell King 
10739c13c20SShubham Bansal #define TMP_REG_1	(MAX_BPF_JIT_REG + 0)	/* TEMP Register 1 */
10839c13c20SShubham Bansal #define TMP_REG_2	(MAX_BPF_JIT_REG + 1)	/* TEMP Register 2 */
10939c13c20SShubham Bansal #define TCALL_CNT	(MAX_BPF_JIT_REG + 2)	/* Tail Call Count */
11039c13c20SShubham Bansal 
11139c13c20SShubham Bansal #define FLAG_IMM_OVERFLOW	(1 << 0)
11239c13c20SShubham Bansal 
113ddecdfceSMircea Gherzan /*
11439c13c20SShubham Bansal  * Map eBPF registers to ARM 32bit registers or stack scratch space.
115ddecdfceSMircea Gherzan  *
11639c13c20SShubham Bansal  * 1. First argument is passed using the arm 32bit registers and rest of the
11739c13c20SShubham Bansal  * arguments are passed on stack scratch space.
1182b589a7eSWang YanQing  * 2. First callee-saved argument is mapped to arm 32 bit registers and rest
11939c13c20SShubham Bansal  * arguments are mapped to scratch space on stack.
12039c13c20SShubham Bansal  * 3. We need two 64 bit temp registers to do complex operations on eBPF
12139c13c20SShubham Bansal  * registers.
12239c13c20SShubham Bansal  *
12339c13c20SShubham Bansal  * As the eBPF registers are all 64 bit registers and arm has only 32 bit
12439c13c20SShubham Bansal  * registers, we have to map each eBPF registers with two arm 32 bit regs or
12539c13c20SShubham Bansal  * scratch memory space and we have to build eBPF 64 bit register from those.
12639c13c20SShubham Bansal  *
12739c13c20SShubham Bansal  */
12839c13c20SShubham Bansal static const u8 bpf2a32[][2] = {
12939c13c20SShubham Bansal 	/* return value from in-kernel function, and exit value from eBPF */
13039c13c20SShubham Bansal 	[BPF_REG_0] = {ARM_R1, ARM_R0},
13139c13c20SShubham Bansal 	/* arguments from eBPF program to in-kernel function */
13239c13c20SShubham Bansal 	[BPF_REG_1] = {ARM_R3, ARM_R2},
13339c13c20SShubham Bansal 	/* Stored on stack scratch space */
134d449ceb1SRussell King 	[BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)},
135d449ceb1SRussell King 	[BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)},
136d449ceb1SRussell King 	[BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)},
137d449ceb1SRussell King 	[BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)},
13839c13c20SShubham Bansal 	/* callee saved registers that in-kernel function will preserve */
13939c13c20SShubham Bansal 	[BPF_REG_6] = {ARM_R5, ARM_R4},
14039c13c20SShubham Bansal 	/* Stored on stack scratch space */
141d449ceb1SRussell King 	[BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
142d449ceb1SRussell King 	[BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
143d449ceb1SRussell King 	[BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
14439c13c20SShubham Bansal 	/* Read only Frame Pointer to access Stack */
145d449ceb1SRussell King 	[BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
14639c13c20SShubham Bansal 	/* Temporary Register for internal BPF JIT, can be used
14739c13c20SShubham Bansal 	 * for constant blindings and others.
14839c13c20SShubham Bansal 	 */
14939c13c20SShubham Bansal 	[TMP_REG_1] = {ARM_R7, ARM_R6},
15039c13c20SShubham Bansal 	[TMP_REG_2] = {ARM_R10, ARM_R8},
15139c13c20SShubham Bansal 	/* Tail call count. Stored on stack scratch space. */
152d449ceb1SRussell King 	[TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)},
15339c13c20SShubham Bansal 	/* temporary register for blinding constants.
15439c13c20SShubham Bansal 	 * Stored on stack scratch space.
15539c13c20SShubham Bansal 	 */
156d449ceb1SRussell King 	[BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
15739c13c20SShubham Bansal };
15839c13c20SShubham Bansal 
15939c13c20SShubham Bansal #define	dst_lo	dst[1]
16039c13c20SShubham Bansal #define dst_hi	dst[0]
16139c13c20SShubham Bansal #define src_lo	src[1]
16239c13c20SShubham Bansal #define src_hi	src[0]
16339c13c20SShubham Bansal 
16439c13c20SShubham Bansal /*
16539c13c20SShubham Bansal  * JIT Context:
16639c13c20SShubham Bansal  *
16739c13c20SShubham Bansal  * prog			:	bpf_prog
16839c13c20SShubham Bansal  * idx			:	index of current last JITed instruction.
16939c13c20SShubham Bansal  * prologue_bytes	:	bytes used in prologue.
17039c13c20SShubham Bansal  * epilogue_offset	:	offset of epilogue starting.
17139c13c20SShubham Bansal  * offsets		:	array of eBPF instruction offsets in
17239c13c20SShubham Bansal  *				JITed code.
17339c13c20SShubham Bansal  * target		:	final JITed code.
17439c13c20SShubham Bansal  * epilogue_bytes	:	no of bytes used in epilogue.
17539c13c20SShubham Bansal  * imm_count		:	no of immediate counts used for global
17639c13c20SShubham Bansal  *				variables.
17739c13c20SShubham Bansal  * imms			:	array of global variable addresses.
178ddecdfceSMircea Gherzan  */
179ddecdfceSMircea Gherzan 
180ddecdfceSMircea Gherzan struct jit_ctx {
18139c13c20SShubham Bansal 	const struct bpf_prog *prog;
18239c13c20SShubham Bansal 	unsigned int idx;
18339c13c20SShubham Bansal 	unsigned int prologue_bytes;
18439c13c20SShubham Bansal 	unsigned int epilogue_offset;
185ddecdfceSMircea Gherzan 	u32 flags;
186ddecdfceSMircea Gherzan 	u32 *offsets;
187ddecdfceSMircea Gherzan 	u32 *target;
18839c13c20SShubham Bansal 	u32 stack_size;
189ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
190ddecdfceSMircea Gherzan 	u16 epilogue_bytes;
191ddecdfceSMircea Gherzan 	u16 imm_count;
192ddecdfceSMircea Gherzan 	u32 *imms;
193ddecdfceSMircea Gherzan #endif
194ddecdfceSMircea Gherzan };
195ddecdfceSMircea Gherzan 
196ddecdfceSMircea Gherzan /*
1974560cdffSNicolas Schichan  * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
198ddecdfceSMircea Gherzan  * (where the assembly routines like __aeabi_uidiv could cause problems).
199ddecdfceSMircea Gherzan  */
20039c13c20SShubham Bansal static u32 jit_udiv32(u32 dividend, u32 divisor)
201ddecdfceSMircea Gherzan {
202ddecdfceSMircea Gherzan 	return dividend / divisor;
203ddecdfceSMircea Gherzan }
204ddecdfceSMircea Gherzan 
20539c13c20SShubham Bansal static u32 jit_mod32(u32 dividend, u32 divisor)
2064560cdffSNicolas Schichan {
2074560cdffSNicolas Schichan 	return dividend % divisor;
2084560cdffSNicolas Schichan }
2094560cdffSNicolas Schichan 
210ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
211ddecdfceSMircea Gherzan {
2123460743eSBen Dooks 	inst |= (cond << 28);
2133460743eSBen Dooks 	inst = __opcode_to_mem_arm(inst);
2143460743eSBen Dooks 
215ddecdfceSMircea Gherzan 	if (ctx->target != NULL)
2163460743eSBen Dooks 		ctx->target[ctx->idx] = inst;
217ddecdfceSMircea Gherzan 
218ddecdfceSMircea Gherzan 	ctx->idx++;
219ddecdfceSMircea Gherzan }
220ddecdfceSMircea Gherzan 
221ddecdfceSMircea Gherzan /*
222ddecdfceSMircea Gherzan  * Emit an instruction that will be executed unconditionally.
223ddecdfceSMircea Gherzan  */
224ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx)
225ddecdfceSMircea Gherzan {
226ddecdfceSMircea Gherzan 	_emit(ARM_COND_AL, inst, ctx);
227ddecdfceSMircea Gherzan }
228ddecdfceSMircea Gherzan 
22939c13c20SShubham Bansal /*
23039c13c20SShubham Bansal  * Checks if immediate value can be converted to imm12(12 bits) value.
23139c13c20SShubham Bansal  */
23239c13c20SShubham Bansal static int16_t imm8m(u32 x)
233ddecdfceSMircea Gherzan {
23439c13c20SShubham Bansal 	u32 rot;
235ddecdfceSMircea Gherzan 
23639c13c20SShubham Bansal 	for (rot = 0; rot < 16; rot++)
23739c13c20SShubham Bansal 		if ((x & ~ror32(0xff, 2 * rot)) == 0)
23839c13c20SShubham Bansal 			return rol32(x, 2 * rot) | (rot << 8);
23939c13c20SShubham Bansal 	return -1;
240ddecdfceSMircea Gherzan }
241ddecdfceSMircea Gherzan 
242*a8ef95a0SRussell King static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12)
243*a8ef95a0SRussell King {
244*a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
245*a8ef95a0SRussell King 	if (imm12 >= 0)
246*a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
247*a8ef95a0SRussell King 	else
248*a8ef95a0SRussell King 		imm12 = -imm12;
249*a8ef95a0SRussell King 	return op | (imm12 & 0xfff);
250*a8ef95a0SRussell King }
251*a8ef95a0SRussell King 
252*a8ef95a0SRussell King static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8)
253*a8ef95a0SRussell King {
254*a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
255*a8ef95a0SRussell King 	if (imm8 >= 0)
256*a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
257*a8ef95a0SRussell King 	else
258*a8ef95a0SRussell King 		imm8 = -imm8;
259*a8ef95a0SRussell King 	return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f);
260*a8ef95a0SRussell King }
261*a8ef95a0SRussell King 
262*a8ef95a0SRussell King #define ARM_LDR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off)
263*a8ef95a0SRussell King #define ARM_LDRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off)
264*a8ef95a0SRussell King #define ARM_LDRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off)
265*a8ef95a0SRussell King 
266*a8ef95a0SRussell King #define ARM_STR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off)
267*a8ef95a0SRussell King #define ARM_STRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off)
268*a8ef95a0SRussell King #define ARM_STRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off)
269*a8ef95a0SRussell King 
27039c13c20SShubham Bansal /*
27139c13c20SShubham Bansal  * Initializes the JIT space with undefined instructions.
27239c13c20SShubham Bansal  */
27355309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
27455309dd3SDaniel Borkmann {
275e8b56d55SDaniel Borkmann 	u32 *ptr;
27655309dd3SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
27755309dd3SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
278e8b56d55SDaniel Borkmann 		*ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
27955309dd3SDaniel Borkmann }
28055309dd3SDaniel Borkmann 
281d1220efdSRussell King #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
282d1220efdSRussell King /* EABI requires the stack to be aligned to 64-bit boundaries */
283d1220efdSRussell King #define STACK_ALIGNMENT	8
284d1220efdSRussell King #else
285d1220efdSRussell King /* Stack must be aligned to 32-bit boundaries */
286d1220efdSRussell King #define STACK_ALIGNMENT	4
287d1220efdSRussell King #endif
288ddecdfceSMircea Gherzan 
28939c13c20SShubham Bansal /* total stack size used in JITed code */
29038ca9306SDaniel Borkmann #define _STACK_SIZE	(ctx->prog->aux->stack_depth + SCRATCH_SIZE)
291d1220efdSRussell King #define STACK_SIZE	ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
292ddecdfceSMircea Gherzan 
29339c13c20SShubham Bansal /* Get the offset of eBPF REGISTERs stored on scratch space. */
29438ca9306SDaniel Borkmann #define STACK_VAR(off) (STACK_SIZE - off)
295ddecdfceSMircea Gherzan 
296ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
297ddecdfceSMircea Gherzan 
298ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx)
299ddecdfceSMircea Gherzan {
30039c13c20SShubham Bansal 	unsigned int i = 0, offset;
301ddecdfceSMircea Gherzan 	u16 imm;
302ddecdfceSMircea Gherzan 
303ddecdfceSMircea Gherzan 	/* on the "fake" run we just count them (duplicates included) */
304ddecdfceSMircea Gherzan 	if (ctx->target == NULL) {
305ddecdfceSMircea Gherzan 		ctx->imm_count++;
306ddecdfceSMircea Gherzan 		return 0;
307ddecdfceSMircea Gherzan 	}
308ddecdfceSMircea Gherzan 
309ddecdfceSMircea Gherzan 	while ((i < ctx->imm_count) && ctx->imms[i]) {
310ddecdfceSMircea Gherzan 		if (ctx->imms[i] == k)
311ddecdfceSMircea Gherzan 			break;
312ddecdfceSMircea Gherzan 		i++;
313ddecdfceSMircea Gherzan 	}
314ddecdfceSMircea Gherzan 
315ddecdfceSMircea Gherzan 	if (ctx->imms[i] == 0)
316ddecdfceSMircea Gherzan 		ctx->imms[i] = k;
317ddecdfceSMircea Gherzan 
318ddecdfceSMircea Gherzan 	/* constants go just after the epilogue */
31939c13c20SShubham Bansal 	offset =  ctx->offsets[ctx->prog->len - 1] * 4;
320ddecdfceSMircea Gherzan 	offset += ctx->prologue_bytes;
321ddecdfceSMircea Gherzan 	offset += ctx->epilogue_bytes;
322ddecdfceSMircea Gherzan 	offset += i * 4;
323ddecdfceSMircea Gherzan 
324ddecdfceSMircea Gherzan 	ctx->target[offset / 4] = k;
325ddecdfceSMircea Gherzan 
326ddecdfceSMircea Gherzan 	/* PC in ARM mode == address of the instruction + 8 */
327ddecdfceSMircea Gherzan 	imm = offset - (8 + ctx->idx * 4);
328ddecdfceSMircea Gherzan 
3290b59d880SNicolas Schichan 	if (imm & ~0xfff) {
3300b59d880SNicolas Schichan 		/*
3310b59d880SNicolas Schichan 		 * literal pool is too far, signal it into flags. we
3320b59d880SNicolas Schichan 		 * can only detect it on the second pass unfortunately.
3330b59d880SNicolas Schichan 		 */
3340b59d880SNicolas Schichan 		ctx->flags |= FLAG_IMM_OVERFLOW;
3350b59d880SNicolas Schichan 		return 0;
3360b59d880SNicolas Schichan 	}
3370b59d880SNicolas Schichan 
338ddecdfceSMircea Gherzan 	return imm;
339ddecdfceSMircea Gherzan }
340ddecdfceSMircea Gherzan 
341ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */
342ddecdfceSMircea Gherzan 
34339c13c20SShubham Bansal static inline int bpf2a32_offset(int bpf_to, int bpf_from,
34439c13c20SShubham Bansal 				 const struct jit_ctx *ctx) {
34539c13c20SShubham Bansal 	int to, from;
34639c13c20SShubham Bansal 
34739c13c20SShubham Bansal 	if (ctx->target == NULL)
34839c13c20SShubham Bansal 		return 0;
34939c13c20SShubham Bansal 	to = ctx->offsets[bpf_to];
35039c13c20SShubham Bansal 	from = ctx->offsets[bpf_from];
35139c13c20SShubham Bansal 
35239c13c20SShubham Bansal 	return to - from - 1;
35339c13c20SShubham Bansal }
35439c13c20SShubham Bansal 
355ddecdfceSMircea Gherzan /*
356ddecdfceSMircea Gherzan  * Move an immediate that's not an imm8m to a core register.
357ddecdfceSMircea Gherzan  */
35839c13c20SShubham Bansal static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
359ddecdfceSMircea Gherzan {
360ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
361ddecdfceSMircea Gherzan 	emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
362ddecdfceSMircea Gherzan #else
363ddecdfceSMircea Gherzan 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
364ddecdfceSMircea Gherzan 	if (val > 0xffff)
365ddecdfceSMircea Gherzan 		emit(ARM_MOVT(rd, val >> 16), ctx);
366ddecdfceSMircea Gherzan #endif
367ddecdfceSMircea Gherzan }
368ddecdfceSMircea Gherzan 
36939c13c20SShubham Bansal static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
370ddecdfceSMircea Gherzan {
371ddecdfceSMircea Gherzan 	int imm12 = imm8m(val);
372ddecdfceSMircea Gherzan 
373ddecdfceSMircea Gherzan 	if (imm12 >= 0)
374ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(rd, imm12), ctx);
375ddecdfceSMircea Gherzan 	else
376ddecdfceSMircea Gherzan 		emit_mov_i_no8m(rd, val, ctx);
377ddecdfceSMircea Gherzan }
378ddecdfceSMircea Gherzan 
379e9062481SRussell King static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
380ddecdfceSMircea Gherzan {
381ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_THUMB)
382ddecdfceSMircea Gherzan 		emit(ARM_BX(tgt_reg), ctx);
383ddecdfceSMircea Gherzan 	else
384ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
385e9062481SRussell King }
386e9062481SRussell King 
387ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
388ddecdfceSMircea Gherzan {
389ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5
390ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
391e9062481SRussell King 	emit_bx_r(tgt_reg, ctx);
392ddecdfceSMircea Gherzan #else
393ddecdfceSMircea Gherzan 	emit(ARM_BLX_R(tgt_reg), ctx);
394ddecdfceSMircea Gherzan #endif
395ddecdfceSMircea Gherzan }
396ddecdfceSMircea Gherzan 
39739c13c20SShubham Bansal static inline int epilogue_offset(const struct jit_ctx *ctx)
398ddecdfceSMircea Gherzan {
39939c13c20SShubham Bansal 	int to, from;
40039c13c20SShubham Bansal 	/* No need for 1st dummy run */
40139c13c20SShubham Bansal 	if (ctx->target == NULL)
40239c13c20SShubham Bansal 		return 0;
40339c13c20SShubham Bansal 	to = ctx->epilogue_offset;
40439c13c20SShubham Bansal 	from = ctx->idx;
40539c13c20SShubham Bansal 
40639c13c20SShubham Bansal 	return to - from - 2;
40739c13c20SShubham Bansal }
40839c13c20SShubham Bansal 
40939c13c20SShubham Bansal static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op)
41039c13c20SShubham Bansal {
41139c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
41239c13c20SShubham Bansal 
413ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7
414ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_IDIVA) {
41539c13c20SShubham Bansal 		if (op == BPF_DIV)
416ddecdfceSMircea Gherzan 			emit(ARM_UDIV(rd, rm, rn), ctx);
4174560cdffSNicolas Schichan 		else {
41839c13c20SShubham Bansal 			emit(ARM_UDIV(ARM_IP, rm, rn), ctx);
41939c13c20SShubham Bansal 			emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
4204560cdffSNicolas Schichan 		}
421ddecdfceSMircea Gherzan 		return;
422ddecdfceSMircea Gherzan 	}
423ddecdfceSMircea Gherzan #endif
42419fc99d0SNicolas Schichan 
42519fc99d0SNicolas Schichan 	/*
42639c13c20SShubham Bansal 	 * For BPF_ALU | BPF_DIV | BPF_K instructions
42739c13c20SShubham Bansal 	 * As ARM_R1 and ARM_R0 contains 1st argument of bpf
42839c13c20SShubham Bansal 	 * function, we need to save it on caller side to save
42939c13c20SShubham Bansal 	 * it from getting destroyed within callee.
43039c13c20SShubham Bansal 	 * After the return from the callee, we restore ARM_R0
43139c13c20SShubham Bansal 	 * ARM_R1.
43219fc99d0SNicolas Schichan 	 */
43339c13c20SShubham Bansal 	if (rn != ARM_R1) {
43439c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
435ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R1, rn), ctx);
43639c13c20SShubham Bansal 	}
43739c13c20SShubham Bansal 	if (rm != ARM_R0) {
43839c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
43919fc99d0SNicolas Schichan 		emit(ARM_MOV_R(ARM_R0, rm), ctx);
44039c13c20SShubham Bansal 	}
441ddecdfceSMircea Gherzan 
44239c13c20SShubham Bansal 	/* Call appropriate function */
44339c13c20SShubham Bansal 	emit_mov_i(ARM_IP, op == BPF_DIV ?
44439c13c20SShubham Bansal 		   (u32)jit_udiv32 : (u32)jit_mod32, ctx);
44539c13c20SShubham Bansal 	emit_blx_r(ARM_IP, ctx);
446ddecdfceSMircea Gherzan 
44739c13c20SShubham Bansal 	/* Save return value */
448ddecdfceSMircea Gherzan 	if (rd != ARM_R0)
449ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(rd, ARM_R0), ctx);
45039c13c20SShubham Bansal 
45139c13c20SShubham Bansal 	/* Restore ARM_R0 and ARM_R1 */
45239c13c20SShubham Bansal 	if (rn != ARM_R1)
45339c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
45439c13c20SShubham Bansal 	if (rm != ARM_R0)
45539c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
456ddecdfceSMircea Gherzan }
457ddecdfceSMircea Gherzan 
45839c13c20SShubham Bansal /* Checks whether BPF register is on scratch stack space or not. */
45939c13c20SShubham Bansal static inline bool is_on_stack(u8 bpf_reg)
460ddecdfceSMircea Gherzan {
46139c13c20SShubham Bansal 	static u8 stack_regs[] = {BPF_REG_AX, BPF_REG_3, BPF_REG_4, BPF_REG_5,
46239c13c20SShubham Bansal 				BPF_REG_7, BPF_REG_8, BPF_REG_9, TCALL_CNT,
46339c13c20SShubham Bansal 				BPF_REG_2, BPF_REG_FP};
46439c13c20SShubham Bansal 	int i, reg_len = sizeof(stack_regs);
465ddecdfceSMircea Gherzan 
46639c13c20SShubham Bansal 	for (i = 0 ; i < reg_len ; i++) {
46739c13c20SShubham Bansal 		if (bpf_reg == stack_regs[i])
46839c13c20SShubham Bansal 			return true;
46939c13c20SShubham Bansal 	}
47039c13c20SShubham Bansal 	return false;
471ddecdfceSMircea Gherzan }
472ddecdfceSMircea Gherzan 
47339c13c20SShubham Bansal static inline void emit_a32_mov_i(const u8 dst, const u32 val,
47439c13c20SShubham Bansal 				  bool dstk, struct jit_ctx *ctx)
475ddecdfceSMircea Gherzan {
47639c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
477ddecdfceSMircea Gherzan 
47839c13c20SShubham Bansal 	if (dstk) {
47939c13c20SShubham Bansal 		emit_mov_i(tmp[1], val, ctx);
48039c13c20SShubham Bansal 		emit(ARM_STR_I(tmp[1], ARM_SP, STACK_VAR(dst)), ctx);
48139c13c20SShubham Bansal 	} else {
48239c13c20SShubham Bansal 		emit_mov_i(dst, val, ctx);
48339c13c20SShubham Bansal 	}
48439c13c20SShubham Bansal }
48534805931SDaniel Borkmann 
48639c13c20SShubham Bansal /* Sign extended move */
48739c13c20SShubham Bansal static inline void emit_a32_mov_i64(const bool is64, const u8 dst[],
48839c13c20SShubham Bansal 				  const u32 val, bool dstk,
48939c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
49039c13c20SShubham Bansal 	u32 hi = 0;
491ddecdfceSMircea Gherzan 
49239c13c20SShubham Bansal 	if (is64 && (val & (1<<31)))
49339c13c20SShubham Bansal 		hi = (u32)~0;
49439c13c20SShubham Bansal 	emit_a32_mov_i(dst_lo, val, dstk, ctx);
49539c13c20SShubham Bansal 	emit_a32_mov_i(dst_hi, hi, dstk, ctx);
49639c13c20SShubham Bansal }
49739c13c20SShubham Bansal 
49839c13c20SShubham Bansal static inline void emit_a32_add_r(const u8 dst, const u8 src,
49939c13c20SShubham Bansal 			      const bool is64, const bool hi,
50039c13c20SShubham Bansal 			      struct jit_ctx *ctx) {
50139c13c20SShubham Bansal 	/* 64 bit :
50239c13c20SShubham Bansal 	 *	adds dst_lo, dst_lo, src_lo
50339c13c20SShubham Bansal 	 *	adc dst_hi, dst_hi, src_hi
50439c13c20SShubham Bansal 	 * 32 bit :
50539c13c20SShubham Bansal 	 *	add dst_lo, dst_lo, src_lo
50639c13c20SShubham Bansal 	 */
50739c13c20SShubham Bansal 	if (!hi && is64)
50839c13c20SShubham Bansal 		emit(ARM_ADDS_R(dst, dst, src), ctx);
50939c13c20SShubham Bansal 	else if (hi && is64)
51039c13c20SShubham Bansal 		emit(ARM_ADC_R(dst, dst, src), ctx);
51139c13c20SShubham Bansal 	else
51239c13c20SShubham Bansal 		emit(ARM_ADD_R(dst, dst, src), ctx);
51339c13c20SShubham Bansal }
51439c13c20SShubham Bansal 
51539c13c20SShubham Bansal static inline void emit_a32_sub_r(const u8 dst, const u8 src,
51639c13c20SShubham Bansal 				  const bool is64, const bool hi,
51739c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
51839c13c20SShubham Bansal 	/* 64 bit :
51939c13c20SShubham Bansal 	 *	subs dst_lo, dst_lo, src_lo
52039c13c20SShubham Bansal 	 *	sbc dst_hi, dst_hi, src_hi
52139c13c20SShubham Bansal 	 * 32 bit :
52239c13c20SShubham Bansal 	 *	sub dst_lo, dst_lo, src_lo
52339c13c20SShubham Bansal 	 */
52439c13c20SShubham Bansal 	if (!hi && is64)
52539c13c20SShubham Bansal 		emit(ARM_SUBS_R(dst, dst, src), ctx);
52639c13c20SShubham Bansal 	else if (hi && is64)
52739c13c20SShubham Bansal 		emit(ARM_SBC_R(dst, dst, src), ctx);
52839c13c20SShubham Bansal 	else
52939c13c20SShubham Bansal 		emit(ARM_SUB_R(dst, dst, src), ctx);
53039c13c20SShubham Bansal }
53139c13c20SShubham Bansal 
53239c13c20SShubham Bansal static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
53339c13c20SShubham Bansal 			      const bool hi, const u8 op, struct jit_ctx *ctx){
53439c13c20SShubham Bansal 	switch (BPF_OP(op)) {
53539c13c20SShubham Bansal 	/* dst = dst + src */
53639c13c20SShubham Bansal 	case BPF_ADD:
53739c13c20SShubham Bansal 		emit_a32_add_r(dst, src, is64, hi, ctx);
53839c13c20SShubham Bansal 		break;
53939c13c20SShubham Bansal 	/* dst = dst - src */
54039c13c20SShubham Bansal 	case BPF_SUB:
54139c13c20SShubham Bansal 		emit_a32_sub_r(dst, src, is64, hi, ctx);
54239c13c20SShubham Bansal 		break;
54339c13c20SShubham Bansal 	/* dst = dst | src */
54439c13c20SShubham Bansal 	case BPF_OR:
54539c13c20SShubham Bansal 		emit(ARM_ORR_R(dst, dst, src), ctx);
54639c13c20SShubham Bansal 		break;
54739c13c20SShubham Bansal 	/* dst = dst & src */
54839c13c20SShubham Bansal 	case BPF_AND:
54939c13c20SShubham Bansal 		emit(ARM_AND_R(dst, dst, src), ctx);
55039c13c20SShubham Bansal 		break;
55139c13c20SShubham Bansal 	/* dst = dst ^ src */
55239c13c20SShubham Bansal 	case BPF_XOR:
55339c13c20SShubham Bansal 		emit(ARM_EOR_R(dst, dst, src), ctx);
55439c13c20SShubham Bansal 		break;
55539c13c20SShubham Bansal 	/* dst = dst * src */
55639c13c20SShubham Bansal 	case BPF_MUL:
55739c13c20SShubham Bansal 		emit(ARM_MUL(dst, dst, src), ctx);
55839c13c20SShubham Bansal 		break;
55939c13c20SShubham Bansal 	/* dst = dst << src */
56039c13c20SShubham Bansal 	case BPF_LSH:
56139c13c20SShubham Bansal 		emit(ARM_LSL_R(dst, dst, src), ctx);
56239c13c20SShubham Bansal 		break;
56339c13c20SShubham Bansal 	/* dst = dst >> src */
56439c13c20SShubham Bansal 	case BPF_RSH:
56539c13c20SShubham Bansal 		emit(ARM_LSR_R(dst, dst, src), ctx);
56639c13c20SShubham Bansal 		break;
56739c13c20SShubham Bansal 	/* dst = dst >> src (signed)*/
56839c13c20SShubham Bansal 	case BPF_ARSH:
56939c13c20SShubham Bansal 		emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
57039c13c20SShubham Bansal 		break;
57139c13c20SShubham Bansal 	}
57239c13c20SShubham Bansal }
57339c13c20SShubham Bansal 
57439c13c20SShubham Bansal /* ALU operation (32 bit)
57539c13c20SShubham Bansal  * dst = dst (op) src
57639c13c20SShubham Bansal  */
57739c13c20SShubham Bansal static inline void emit_a32_alu_r(const u8 dst, const u8 src,
57839c13c20SShubham Bansal 				  bool dstk, bool sstk,
57939c13c20SShubham Bansal 				  struct jit_ctx *ctx, const bool is64,
58039c13c20SShubham Bansal 				  const bool hi, const u8 op) {
58139c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
58239c13c20SShubham Bansal 	u8 rn = sstk ? tmp[1] : src;
58339c13c20SShubham Bansal 
58439c13c20SShubham Bansal 	if (sstk)
58539c13c20SShubham Bansal 		emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src)), ctx);
58639c13c20SShubham Bansal 
58739c13c20SShubham Bansal 	/* ALU operation */
58839c13c20SShubham Bansal 	if (dstk) {
58939c13c20SShubham Bansal 		emit(ARM_LDR_I(tmp[0], ARM_SP, STACK_VAR(dst)), ctx);
59039c13c20SShubham Bansal 		emit_alu_r(tmp[0], rn, is64, hi, op, ctx);
59139c13c20SShubham Bansal 		emit(ARM_STR_I(tmp[0], ARM_SP, STACK_VAR(dst)), ctx);
59239c13c20SShubham Bansal 	} else {
59339c13c20SShubham Bansal 		emit_alu_r(dst, rn, is64, hi, op, ctx);
59439c13c20SShubham Bansal 	}
59539c13c20SShubham Bansal }
59639c13c20SShubham Bansal 
59739c13c20SShubham Bansal /* ALU operation (64 bit) */
59839c13c20SShubham Bansal static inline void emit_a32_alu_r64(const bool is64, const u8 dst[],
59939c13c20SShubham Bansal 				  const u8 src[], bool dstk,
60039c13c20SShubham Bansal 				  bool sstk, struct jit_ctx *ctx,
60139c13c20SShubham Bansal 				  const u8 op) {
60239c13c20SShubham Bansal 	emit_a32_alu_r(dst_lo, src_lo, dstk, sstk, ctx, is64, false, op);
60339c13c20SShubham Bansal 	if (is64)
60439c13c20SShubham Bansal 		emit_a32_alu_r(dst_hi, src_hi, dstk, sstk, ctx, is64, true, op);
60539c13c20SShubham Bansal 	else
60639c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
60739c13c20SShubham Bansal }
60839c13c20SShubham Bansal 
60939c13c20SShubham Bansal /* dst = imm (4 bytes)*/
61039c13c20SShubham Bansal static inline void emit_a32_mov_r(const u8 dst, const u8 src,
61139c13c20SShubham Bansal 				  bool dstk, bool sstk,
61239c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
61339c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
61439c13c20SShubham Bansal 	u8 rt = sstk ? tmp[0] : src;
61539c13c20SShubham Bansal 
61639c13c20SShubham Bansal 	if (sstk)
61739c13c20SShubham Bansal 		emit(ARM_LDR_I(tmp[0], ARM_SP, STACK_VAR(src)), ctx);
61839c13c20SShubham Bansal 	if (dstk)
61939c13c20SShubham Bansal 		emit(ARM_STR_I(rt, ARM_SP, STACK_VAR(dst)), ctx);
62039c13c20SShubham Bansal 	else
62139c13c20SShubham Bansal 		emit(ARM_MOV_R(dst, rt), ctx);
62239c13c20SShubham Bansal }
62339c13c20SShubham Bansal 
62439c13c20SShubham Bansal /* dst = src */
62539c13c20SShubham Bansal static inline void emit_a32_mov_r64(const bool is64, const u8 dst[],
62639c13c20SShubham Bansal 				  const u8 src[], bool dstk,
62739c13c20SShubham Bansal 				  bool sstk, struct jit_ctx *ctx) {
62839c13c20SShubham Bansal 	emit_a32_mov_r(dst_lo, src_lo, dstk, sstk, ctx);
62939c13c20SShubham Bansal 	if (is64) {
63039c13c20SShubham Bansal 		/* complete 8 byte move */
63139c13c20SShubham Bansal 		emit_a32_mov_r(dst_hi, src_hi, dstk, sstk, ctx);
63239c13c20SShubham Bansal 	} else {
63339c13c20SShubham Bansal 		/* Zero out high 4 bytes */
63439c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
63539c13c20SShubham Bansal 	}
63639c13c20SShubham Bansal }
63739c13c20SShubham Bansal 
63839c13c20SShubham Bansal /* Shift operations */
63939c13c20SShubham Bansal static inline void emit_a32_alu_i(const u8 dst, const u32 val, bool dstk,
64039c13c20SShubham Bansal 				struct jit_ctx *ctx, const u8 op) {
64139c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
64239c13c20SShubham Bansal 	u8 rd = dstk ? tmp[0] : dst;
64339c13c20SShubham Bansal 
64439c13c20SShubham Bansal 	if (dstk)
64539c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst)), ctx);
64639c13c20SShubham Bansal 
64739c13c20SShubham Bansal 	/* Do shift operation */
64839c13c20SShubham Bansal 	switch (op) {
64939c13c20SShubham Bansal 	case BPF_LSH:
65039c13c20SShubham Bansal 		emit(ARM_LSL_I(rd, rd, val), ctx);
65139c13c20SShubham Bansal 		break;
65239c13c20SShubham Bansal 	case BPF_RSH:
65339c13c20SShubham Bansal 		emit(ARM_LSR_I(rd, rd, val), ctx);
65439c13c20SShubham Bansal 		break;
65539c13c20SShubham Bansal 	case BPF_NEG:
65639c13c20SShubham Bansal 		emit(ARM_RSB_I(rd, rd, val), ctx);
65739c13c20SShubham Bansal 		break;
65839c13c20SShubham Bansal 	}
65939c13c20SShubham Bansal 
66039c13c20SShubham Bansal 	if (dstk)
66139c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst)), ctx);
66239c13c20SShubham Bansal }
66339c13c20SShubham Bansal 
66439c13c20SShubham Bansal /* dst = ~dst (64 bit) */
66539c13c20SShubham Bansal static inline void emit_a32_neg64(const u8 dst[], bool dstk,
66639c13c20SShubham Bansal 				struct jit_ctx *ctx){
66739c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
66839c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst[1];
66939c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst[0];
67039c13c20SShubham Bansal 
67139c13c20SShubham Bansal 	/* Setup Operand */
67239c13c20SShubham Bansal 	if (dstk) {
67339c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
67439c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
67539c13c20SShubham Bansal 	}
67639c13c20SShubham Bansal 
67739c13c20SShubham Bansal 	/* Do Negate Operation */
67839c13c20SShubham Bansal 	emit(ARM_RSBS_I(rd, rd, 0), ctx);
67939c13c20SShubham Bansal 	emit(ARM_RSC_I(rm, rm, 0), ctx);
68039c13c20SShubham Bansal 
68139c13c20SShubham Bansal 	if (dstk) {
68239c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
68339c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
68439c13c20SShubham Bansal 	}
68539c13c20SShubham Bansal }
68639c13c20SShubham Bansal 
68739c13c20SShubham Bansal /* dst = dst << src */
68839c13c20SShubham Bansal static inline void emit_a32_lsh_r64(const u8 dst[], const u8 src[], bool dstk,
68939c13c20SShubham Bansal 				    bool sstk, struct jit_ctx *ctx) {
69039c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
69139c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
69239c13c20SShubham Bansal 
69339c13c20SShubham Bansal 	/* Setup Operands */
69439c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
69539c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
69639c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
69739c13c20SShubham Bansal 
69839c13c20SShubham Bansal 	if (sstk)
69939c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
70039c13c20SShubham Bansal 	if (dstk) {
70139c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
70239c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
70339c13c20SShubham Bansal 	}
70439c13c20SShubham Bansal 
70539c13c20SShubham Bansal 	/* Do LSH operation */
70639c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
70739c13c20SShubham Bansal 	emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
70839c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rm, SRTYPE_ASL, rt), ctx);
70939c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd, SRTYPE_ASL, ARM_IP), ctx);
71039c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd, SRTYPE_LSR, tmp2[0]), ctx);
71139c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rd, SRTYPE_ASL, rt), ctx);
71239c13c20SShubham Bansal 
71339c13c20SShubham Bansal 	if (dstk) {
71439c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_LR, ARM_SP, STACK_VAR(dst_lo)), ctx);
71539c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_hi)), ctx);
71639c13c20SShubham Bansal 	} else {
71739c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_LR), ctx);
71839c13c20SShubham Bansal 		emit(ARM_MOV_R(rm, ARM_IP), ctx);
71939c13c20SShubham Bansal 	}
72039c13c20SShubham Bansal }
72139c13c20SShubham Bansal 
72239c13c20SShubham Bansal /* dst = dst >> src (signed)*/
72339c13c20SShubham Bansal static inline void emit_a32_arsh_r64(const u8 dst[], const u8 src[], bool dstk,
72439c13c20SShubham Bansal 				    bool sstk, struct jit_ctx *ctx) {
72539c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
72639c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
72739c13c20SShubham Bansal 	/* Setup Operands */
72839c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
72939c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
73039c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
73139c13c20SShubham Bansal 
73239c13c20SShubham Bansal 	if (sstk)
73339c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
73439c13c20SShubham Bansal 	if (dstk) {
73539c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
73639c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
73739c13c20SShubham Bansal 	}
73839c13c20SShubham Bansal 
73939c13c20SShubham Bansal 	/* Do the ARSH operation */
74039c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
74139c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
74239c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rd, SRTYPE_LSR, rt), ctx);
74339c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_ASL, ARM_IP), ctx);
74439c13c20SShubham Bansal 	_emit(ARM_COND_MI, ARM_B(0), ctx);
74539c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_ASR, tmp2[0]), ctx);
74639c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_IP, rm, SRTYPE_ASR, rt), ctx);
74739c13c20SShubham Bansal 	if (dstk) {
74839c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_LR, ARM_SP, STACK_VAR(dst_lo)), ctx);
74939c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_hi)), ctx);
75039c13c20SShubham Bansal 	} else {
75139c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_LR), ctx);
75239c13c20SShubham Bansal 		emit(ARM_MOV_R(rm, ARM_IP), ctx);
75339c13c20SShubham Bansal 	}
75439c13c20SShubham Bansal }
75539c13c20SShubham Bansal 
75639c13c20SShubham Bansal /* dst = dst >> src */
75768565a1aSWang YanQing static inline void emit_a32_rsh_r64(const u8 dst[], const u8 src[], bool dstk,
75839c13c20SShubham Bansal 				     bool sstk, struct jit_ctx *ctx) {
75939c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
76039c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
76139c13c20SShubham Bansal 	/* Setup Operands */
76239c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
76339c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
76439c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
76539c13c20SShubham Bansal 
76639c13c20SShubham Bansal 	if (sstk)
76739c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
76839c13c20SShubham Bansal 	if (dstk) {
76939c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
77039c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
77139c13c20SShubham Bansal 	}
77239c13c20SShubham Bansal 
77368565a1aSWang YanQing 	/* Do RSH operation */
77439c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
77539c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
77639c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rd, SRTYPE_LSR, rt), ctx);
77739c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_ASL, ARM_IP), ctx);
77839c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_LSR, tmp2[0]), ctx);
77939c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_IP, rm, SRTYPE_LSR, rt), ctx);
78039c13c20SShubham Bansal 	if (dstk) {
78139c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_LR, ARM_SP, STACK_VAR(dst_lo)), ctx);
78239c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_hi)), ctx);
78339c13c20SShubham Bansal 	} else {
78439c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_LR), ctx);
78539c13c20SShubham Bansal 		emit(ARM_MOV_R(rm, ARM_IP), ctx);
78639c13c20SShubham Bansal 	}
78739c13c20SShubham Bansal }
78839c13c20SShubham Bansal 
78939c13c20SShubham Bansal /* dst = dst << val */
79039c13c20SShubham Bansal static inline void emit_a32_lsh_i64(const u8 dst[], bool dstk,
79139c13c20SShubham Bansal 				     const u32 val, struct jit_ctx *ctx){
79239c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
79339c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
79439c13c20SShubham Bansal 	/* Setup operands */
79539c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
79639c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
79739c13c20SShubham Bansal 
79839c13c20SShubham Bansal 	if (dstk) {
79939c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
80039c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
80139c13c20SShubham Bansal 	}
80239c13c20SShubham Bansal 
80339c13c20SShubham Bansal 	/* Do LSH operation */
80439c13c20SShubham Bansal 	if (val < 32) {
80539c13c20SShubham Bansal 		emit(ARM_MOV_SI(tmp2[0], rm, SRTYPE_ASL, val), ctx);
80639c13c20SShubham Bansal 		emit(ARM_ORR_SI(rm, tmp2[0], rd, SRTYPE_LSR, 32 - val), ctx);
80739c13c20SShubham Bansal 		emit(ARM_MOV_SI(rd, rd, SRTYPE_ASL, val), ctx);
80839c13c20SShubham Bansal 	} else {
80939c13c20SShubham Bansal 		if (val == 32)
81039c13c20SShubham Bansal 			emit(ARM_MOV_R(rm, rd), ctx);
81139c13c20SShubham Bansal 		else
81239c13c20SShubham Bansal 			emit(ARM_MOV_SI(rm, rd, SRTYPE_ASL, val - 32), ctx);
81339c13c20SShubham Bansal 		emit(ARM_EOR_R(rd, rd, rd), ctx);
81439c13c20SShubham Bansal 	}
81539c13c20SShubham Bansal 
81639c13c20SShubham Bansal 	if (dstk) {
81739c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
81839c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
81939c13c20SShubham Bansal 	}
82039c13c20SShubham Bansal }
82139c13c20SShubham Bansal 
82239c13c20SShubham Bansal /* dst = dst >> val */
82368565a1aSWang YanQing static inline void emit_a32_rsh_i64(const u8 dst[], bool dstk,
82439c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx) {
82539c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
82639c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
82739c13c20SShubham Bansal 	/* Setup operands */
82839c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
82939c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
83039c13c20SShubham Bansal 
83139c13c20SShubham Bansal 	if (dstk) {
83239c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
83339c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
83439c13c20SShubham Bansal 	}
83539c13c20SShubham Bansal 
83639c13c20SShubham Bansal 	/* Do LSR operation */
83739c13c20SShubham Bansal 	if (val < 32) {
83839c13c20SShubham Bansal 		emit(ARM_MOV_SI(tmp2[1], rd, SRTYPE_LSR, val), ctx);
83939c13c20SShubham Bansal 		emit(ARM_ORR_SI(rd, tmp2[1], rm, SRTYPE_ASL, 32 - val), ctx);
84039c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_LSR, val), ctx);
84139c13c20SShubham Bansal 	} else if (val == 32) {
84239c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, rm), ctx);
84339c13c20SShubham Bansal 		emit(ARM_MOV_I(rm, 0), ctx);
84439c13c20SShubham Bansal 	} else {
84539c13c20SShubham Bansal 		emit(ARM_MOV_SI(rd, rm, SRTYPE_LSR, val - 32), ctx);
84639c13c20SShubham Bansal 		emit(ARM_MOV_I(rm, 0), ctx);
84739c13c20SShubham Bansal 	}
84839c13c20SShubham Bansal 
84939c13c20SShubham Bansal 	if (dstk) {
85039c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
85139c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
85239c13c20SShubham Bansal 	}
85339c13c20SShubham Bansal }
85439c13c20SShubham Bansal 
85539c13c20SShubham Bansal /* dst = dst >> val (signed) */
85639c13c20SShubham Bansal static inline void emit_a32_arsh_i64(const u8 dst[], bool dstk,
85739c13c20SShubham Bansal 				     const u32 val, struct jit_ctx *ctx){
85839c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
85939c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
86039c13c20SShubham Bansal 	 /* Setup operands */
86139c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
86239c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
86339c13c20SShubham Bansal 
86439c13c20SShubham Bansal 	if (dstk) {
86539c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
86639c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
86739c13c20SShubham Bansal 	}
86839c13c20SShubham Bansal 
86939c13c20SShubham Bansal 	/* Do ARSH operation */
87039c13c20SShubham Bansal 	if (val < 32) {
87139c13c20SShubham Bansal 		emit(ARM_MOV_SI(tmp2[1], rd, SRTYPE_LSR, val), ctx);
87239c13c20SShubham Bansal 		emit(ARM_ORR_SI(rd, tmp2[1], rm, SRTYPE_ASL, 32 - val), ctx);
87339c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_ASR, val), ctx);
87439c13c20SShubham Bansal 	} else if (val == 32) {
87539c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, rm), ctx);
87639c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_ASR, 31), ctx);
87739c13c20SShubham Bansal 	} else {
87839c13c20SShubham Bansal 		emit(ARM_MOV_SI(rd, rm, SRTYPE_ASR, val - 32), ctx);
87939c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_ASR, 31), ctx);
88039c13c20SShubham Bansal 	}
88139c13c20SShubham Bansal 
88239c13c20SShubham Bansal 	if (dstk) {
88339c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
88439c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
88539c13c20SShubham Bansal 	}
88639c13c20SShubham Bansal }
88739c13c20SShubham Bansal 
88839c13c20SShubham Bansal static inline void emit_a32_mul_r64(const u8 dst[], const u8 src[], bool dstk,
88939c13c20SShubham Bansal 				    bool sstk, struct jit_ctx *ctx) {
89039c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
89139c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
89239c13c20SShubham Bansal 	/* Setup operands for multiplication */
89339c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
89439c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
89539c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
89639c13c20SShubham Bansal 	u8 rn = sstk ? tmp2[0] : src_hi;
89739c13c20SShubham Bansal 
89839c13c20SShubham Bansal 	if (dstk) {
89939c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
90039c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
90139c13c20SShubham Bansal 	}
90239c13c20SShubham Bansal 	if (sstk) {
90339c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
90439c13c20SShubham Bansal 		emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_hi)), ctx);
90539c13c20SShubham Bansal 	}
90639c13c20SShubham Bansal 
90739c13c20SShubham Bansal 	/* Do Multiplication */
90839c13c20SShubham Bansal 	emit(ARM_MUL(ARM_IP, rd, rn), ctx);
90939c13c20SShubham Bansal 	emit(ARM_MUL(ARM_LR, rm, rt), ctx);
91039c13c20SShubham Bansal 	emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
91139c13c20SShubham Bansal 
91239c13c20SShubham Bansal 	emit(ARM_UMULL(ARM_IP, rm, rd, rt), ctx);
91339c13c20SShubham Bansal 	emit(ARM_ADD_R(rm, ARM_LR, rm), ctx);
91439c13c20SShubham Bansal 	if (dstk) {
91539c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_lo)), ctx);
91639c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
91739c13c20SShubham Bansal 	} else {
91839c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_IP), ctx);
91939c13c20SShubham Bansal 	}
92039c13c20SShubham Bansal }
92139c13c20SShubham Bansal 
92239c13c20SShubham Bansal /* *(size *)(dst + off) = src */
92339c13c20SShubham Bansal static inline void emit_str_r(const u8 dst, const u8 src, bool dstk,
92439c13c20SShubham Bansal 			      const s32 off, struct jit_ctx *ctx, const u8 sz){
92539c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
92639c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst;
92739c13c20SShubham Bansal 
92839c13c20SShubham Bansal 	if (dstk)
92939c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst)), ctx);
93039c13c20SShubham Bansal 	if (off) {
93139c13c20SShubham Bansal 		emit_a32_mov_i(tmp[0], off, false, ctx);
93239c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], rd, tmp[0]), ctx);
93339c13c20SShubham Bansal 		rd = tmp[0];
93439c13c20SShubham Bansal 	}
93539c13c20SShubham Bansal 	switch (sz) {
93639c13c20SShubham Bansal 	case BPF_W:
93739c13c20SShubham Bansal 		/* Store a Word */
93839c13c20SShubham Bansal 		emit(ARM_STR_I(src, rd, 0), ctx);
93939c13c20SShubham Bansal 		break;
94039c13c20SShubham Bansal 	case BPF_H:
94139c13c20SShubham Bansal 		/* Store a HalfWord */
94239c13c20SShubham Bansal 		emit(ARM_STRH_I(src, rd, 0), ctx);
94339c13c20SShubham Bansal 		break;
94439c13c20SShubham Bansal 	case BPF_B:
94539c13c20SShubham Bansal 		/* Store a Byte */
94639c13c20SShubham Bansal 		emit(ARM_STRB_I(src, rd, 0), ctx);
94739c13c20SShubham Bansal 		break;
94839c13c20SShubham Bansal 	}
94939c13c20SShubham Bansal }
95039c13c20SShubham Bansal 
95139c13c20SShubham Bansal /* dst = *(size*)(src + off) */
952ec19e02bSRussell King static inline void emit_ldx_r(const u8 dst[], const u8 src, bool dstk,
953ec19e02bSRussell King 			      s32 off, struct jit_ctx *ctx, const u8 sz){
95439c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
955ec19e02bSRussell King 	const u8 *rd = dstk ? tmp : dst;
95639c13c20SShubham Bansal 	u8 rm = src;
957ec19e02bSRussell King 	s32 off_max;
95839c13c20SShubham Bansal 
959ec19e02bSRussell King 	if (sz == BPF_H)
960ec19e02bSRussell King 		off_max = 0xff;
961ec19e02bSRussell King 	else
962ec19e02bSRussell King 		off_max = 0xfff;
963ec19e02bSRussell King 
964ec19e02bSRussell King 	if (off < 0 || off > off_max) {
96539c13c20SShubham Bansal 		emit_a32_mov_i(tmp[0], off, false, ctx);
96639c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
96739c13c20SShubham Bansal 		rm = tmp[0];
968ec19e02bSRussell King 		off = 0;
969ec19e02bSRussell King 	} else if (rd[1] == rm) {
970ec19e02bSRussell King 		emit(ARM_MOV_R(tmp[0], rm), ctx);
971ec19e02bSRussell King 		rm = tmp[0];
97239c13c20SShubham Bansal 	}
97339c13c20SShubham Bansal 	switch (sz) {
974ec19e02bSRussell King 	case BPF_B:
975ec19e02bSRussell King 		/* Load a Byte */
976ec19e02bSRussell King 		emit(ARM_LDRB_I(rd[1], rm, off), ctx);
977ec19e02bSRussell King 		emit_a32_mov_i(dst[0], 0, dstk, ctx);
97839c13c20SShubham Bansal 		break;
97939c13c20SShubham Bansal 	case BPF_H:
98039c13c20SShubham Bansal 		/* Load a HalfWord */
981ec19e02bSRussell King 		emit(ARM_LDRH_I(rd[1], rm, off), ctx);
982ec19e02bSRussell King 		emit_a32_mov_i(dst[0], 0, dstk, ctx);
98339c13c20SShubham Bansal 		break;
984ec19e02bSRussell King 	case BPF_W:
985ec19e02bSRussell King 		/* Load a Word */
986ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
987ec19e02bSRussell King 		emit_a32_mov_i(dst[0], 0, dstk, ctx);
988ec19e02bSRussell King 		break;
989ec19e02bSRussell King 	case BPF_DW:
990ec19e02bSRussell King 		/* Load a Double Word */
991ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
992ec19e02bSRussell King 		emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
99339c13c20SShubham Bansal 		break;
99439c13c20SShubham Bansal 	}
99539c13c20SShubham Bansal 	if (dstk)
996ec19e02bSRussell King 		emit(ARM_STR_I(rd[1], ARM_SP, STACK_VAR(dst[1])), ctx);
997ec19e02bSRussell King 	if (dstk && sz == BPF_DW)
998ec19e02bSRussell King 		emit(ARM_STR_I(rd[0], ARM_SP, STACK_VAR(dst[0])), ctx);
99939c13c20SShubham Bansal }
100039c13c20SShubham Bansal 
100139c13c20SShubham Bansal /* Arithmatic Operation */
100239c13c20SShubham Bansal static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
100339c13c20SShubham Bansal 			     const u8 rn, struct jit_ctx *ctx, u8 op) {
100439c13c20SShubham Bansal 	switch (op) {
100539c13c20SShubham Bansal 	case BPF_JSET:
100639c13c20SShubham Bansal 		emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
100739c13c20SShubham Bansal 		emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
100839c13c20SShubham Bansal 		emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
100939c13c20SShubham Bansal 		break;
101039c13c20SShubham Bansal 	case BPF_JEQ:
101139c13c20SShubham Bansal 	case BPF_JNE:
101239c13c20SShubham Bansal 	case BPF_JGT:
101339c13c20SShubham Bansal 	case BPF_JGE:
101439c13c20SShubham Bansal 	case BPF_JLE:
101539c13c20SShubham Bansal 	case BPF_JLT:
101639c13c20SShubham Bansal 		emit(ARM_CMP_R(rd, rm), ctx);
101739c13c20SShubham Bansal 		_emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
101839c13c20SShubham Bansal 		break;
101939c13c20SShubham Bansal 	case BPF_JSLE:
102039c13c20SShubham Bansal 	case BPF_JSGT:
102139c13c20SShubham Bansal 		emit(ARM_CMP_R(rn, rt), ctx);
102239c13c20SShubham Bansal 		emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
102339c13c20SShubham Bansal 		break;
102439c13c20SShubham Bansal 	case BPF_JSLT:
102539c13c20SShubham Bansal 	case BPF_JSGE:
102639c13c20SShubham Bansal 		emit(ARM_CMP_R(rt, rn), ctx);
102739c13c20SShubham Bansal 		emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
102839c13c20SShubham Bansal 		break;
102939c13c20SShubham Bansal 	}
103039c13c20SShubham Bansal }
103139c13c20SShubham Bansal 
103239c13c20SShubham Bansal static int out_offset = -1; /* initialized on the first pass of build_body() */
103339c13c20SShubham Bansal static int emit_bpf_tail_call(struct jit_ctx *ctx)
103439c13c20SShubham Bansal {
103539c13c20SShubham Bansal 
103639c13c20SShubham Bansal 	/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
103739c13c20SShubham Bansal 	const u8 *r2 = bpf2a32[BPF_REG_2];
103839c13c20SShubham Bansal 	const u8 *r3 = bpf2a32[BPF_REG_3];
103939c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
104039c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
104139c13c20SShubham Bansal 	const u8 *tcc = bpf2a32[TCALL_CNT];
104239c13c20SShubham Bansal 	const int idx0 = ctx->idx;
104339c13c20SShubham Bansal #define cur_offset (ctx->idx - idx0)
1044f4483f2cSRussell King #define jmp_offset (out_offset - (cur_offset) - 2)
104539c13c20SShubham Bansal 	u32 off, lo, hi;
104639c13c20SShubham Bansal 
104739c13c20SShubham Bansal 	/* if (index >= array->map.max_entries)
104839c13c20SShubham Bansal 	 *	goto out;
104939c13c20SShubham Bansal 	 */
105039c13c20SShubham Bansal 	off = offsetof(struct bpf_array, map.max_entries);
105139c13c20SShubham Bansal 	/* array->map.max_entries */
105239c13c20SShubham Bansal 	emit_a32_mov_i(tmp[1], off, false, ctx);
105339c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r2[1])), ctx);
105439c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp2[1], tmp[1]), ctx);
1055091f0248SRussell King 	/* index is 32-bit for arrays */
105639c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r3[1])), ctx);
105739c13c20SShubham Bansal 	/* index >= array->map.max_entries */
105839c13c20SShubham Bansal 	emit(ARM_CMP_R(tmp2[1], tmp[1]), ctx);
105939c13c20SShubham Bansal 	_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
106039c13c20SShubham Bansal 
106139c13c20SShubham Bansal 	/* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
106239c13c20SShubham Bansal 	 *	goto out;
106339c13c20SShubham Bansal 	 * tail_call_cnt++;
106439c13c20SShubham Bansal 	 */
106539c13c20SShubham Bansal 	lo = (u32)MAX_TAIL_CALL_CNT;
106639c13c20SShubham Bansal 	hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
106739c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp[1], ARM_SP, STACK_VAR(tcc[1])), ctx);
106839c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp[0], ARM_SP, STACK_VAR(tcc[0])), ctx);
106939c13c20SShubham Bansal 	emit(ARM_CMP_I(tmp[0], hi), ctx);
107039c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_CMP_I(tmp[1], lo), ctx);
107139c13c20SShubham Bansal 	_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
107239c13c20SShubham Bansal 	emit(ARM_ADDS_I(tmp[1], tmp[1], 1), ctx);
107339c13c20SShubham Bansal 	emit(ARM_ADC_I(tmp[0], tmp[0], 0), ctx);
107439c13c20SShubham Bansal 	emit(ARM_STR_I(tmp[1], ARM_SP, STACK_VAR(tcc[1])), ctx);
107539c13c20SShubham Bansal 	emit(ARM_STR_I(tmp[0], ARM_SP, STACK_VAR(tcc[0])), ctx);
107639c13c20SShubham Bansal 
107739c13c20SShubham Bansal 	/* prog = array->ptrs[index]
107839c13c20SShubham Bansal 	 * if (prog == NULL)
107939c13c20SShubham Bansal 	 *	goto out;
108039c13c20SShubham Bansal 	 */
108139c13c20SShubham Bansal 	off = offsetof(struct bpf_array, ptrs);
108239c13c20SShubham Bansal 	emit_a32_mov_i(tmp[1], off, false, ctx);
108339c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r2[1])), ctx);
108439c13c20SShubham Bansal 	emit(ARM_ADD_R(tmp[1], tmp2[1], tmp[1]), ctx);
108539c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r3[1])), ctx);
108639c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp[0], tmp2[1], SRTYPE_ASL, 2), ctx);
108739c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp[1], tmp[0]), ctx);
108839c13c20SShubham Bansal 	emit(ARM_CMP_I(tmp[1], 0), ctx);
108939c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
109039c13c20SShubham Bansal 
109139c13c20SShubham Bansal 	/* goto *(prog->bpf_func + prologue_size); */
109239c13c20SShubham Bansal 	off = offsetof(struct bpf_prog, bpf_func);
109339c13c20SShubham Bansal 	emit_a32_mov_i(tmp2[1], off, false, ctx);
109439c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp[1], tmp2[1]), ctx);
109539c13c20SShubham Bansal 	emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
1096e9062481SRussell King 	emit_bx_r(tmp[1], ctx);
109739c13c20SShubham Bansal 
109839c13c20SShubham Bansal 	/* out: */
109939c13c20SShubham Bansal 	if (out_offset == -1)
110039c13c20SShubham Bansal 		out_offset = cur_offset;
110139c13c20SShubham Bansal 	if (cur_offset != out_offset) {
110239c13c20SShubham Bansal 		pr_err_once("tail_call out_offset = %d, expected %d!\n",
110339c13c20SShubham Bansal 			    cur_offset, out_offset);
110439c13c20SShubham Bansal 		return -1;
110539c13c20SShubham Bansal 	}
110639c13c20SShubham Bansal 	return 0;
110739c13c20SShubham Bansal #undef cur_offset
110839c13c20SShubham Bansal #undef jmp_offset
110939c13c20SShubham Bansal }
111039c13c20SShubham Bansal 
111139c13c20SShubham Bansal /* 0xabcd => 0xcdab */
111239c13c20SShubham Bansal static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
111339c13c20SShubham Bansal {
111439c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
111539c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
111639c13c20SShubham Bansal 
111739c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
111839c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
111939c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
112039c13c20SShubham Bansal 	emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
112139c13c20SShubham Bansal #else /* ARMv6+ */
112239c13c20SShubham Bansal 	emit(ARM_REV16(rd, rn), ctx);
112339c13c20SShubham Bansal #endif
112439c13c20SShubham Bansal }
112539c13c20SShubham Bansal 
112639c13c20SShubham Bansal /* 0xabcdefgh => 0xghefcdab */
112739c13c20SShubham Bansal static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
112839c13c20SShubham Bansal {
112939c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
113039c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
113139c13c20SShubham Bansal 
113239c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
113339c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
113439c13c20SShubham Bansal 	emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
113539c13c20SShubham Bansal 
113639c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
113739c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
113839c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
113939c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
114039c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
114139c13c20SShubham Bansal 	emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
114239c13c20SShubham Bansal 	emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
114339c13c20SShubham Bansal 
114439c13c20SShubham Bansal #else /* ARMv6+ */
114539c13c20SShubham Bansal 	emit(ARM_REV(rd, rn), ctx);
114639c13c20SShubham Bansal #endif
114739c13c20SShubham Bansal }
114839c13c20SShubham Bansal 
114939c13c20SShubham Bansal // push the scratch stack register on top of the stack
115039c13c20SShubham Bansal static inline void emit_push_r64(const u8 src[], const u8 shift,
115139c13c20SShubham Bansal 		struct jit_ctx *ctx)
115239c13c20SShubham Bansal {
115339c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
115439c13c20SShubham Bansal 	u16 reg_set = 0;
115539c13c20SShubham Bansal 
115639c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(src[1]+shift)), ctx);
115739c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[0], ARM_SP, STACK_VAR(src[0]+shift)), ctx);
115839c13c20SShubham Bansal 
115939c13c20SShubham Bansal 	reg_set = (1 << tmp2[1]) | (1 << tmp2[0]);
116039c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
116139c13c20SShubham Bansal }
116239c13c20SShubham Bansal 
116339c13c20SShubham Bansal static void build_prologue(struct jit_ctx *ctx)
116439c13c20SShubham Bansal {
116539c13c20SShubham Bansal 	const u8 r0 = bpf2a32[BPF_REG_0][1];
116639c13c20SShubham Bansal 	const u8 r2 = bpf2a32[BPF_REG_1][1];
116739c13c20SShubham Bansal 	const u8 r3 = bpf2a32[BPF_REG_1][0];
116839c13c20SShubham Bansal 	const u8 r4 = bpf2a32[BPF_REG_6][1];
116939c13c20SShubham Bansal 	const u8 fplo = bpf2a32[BPF_REG_FP][1];
117039c13c20SShubham Bansal 	const u8 fphi = bpf2a32[BPF_REG_FP][0];
117139c13c20SShubham Bansal 	const u8 *tcc = bpf2a32[TCALL_CNT];
117239c13c20SShubham Bansal 
117339c13c20SShubham Bansal 	/* Save callee saved registers. */
117439c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
117502088d9bSRussell King 	u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
117602088d9bSRussell King 	emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
117739c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
117839c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
117939c13c20SShubham Bansal #else
118002088d9bSRussell King 	emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
118102088d9bSRussell King 	emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
118239c13c20SShubham Bansal #endif
118339c13c20SShubham Bansal 	/* Save frame pointer for later */
118402088d9bSRussell King 	emit(ARM_SUB_I(ARM_IP, ARM_SP, SCRATCH_SIZE), ctx);
118539c13c20SShubham Bansal 
118639c13c20SShubham Bansal 	ctx->stack_size = imm8m(STACK_SIZE);
118739c13c20SShubham Bansal 
118839c13c20SShubham Bansal 	/* Set up function call stack */
118939c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
119039c13c20SShubham Bansal 
119139c13c20SShubham Bansal 	/* Set up BPF prog stack base register */
119239c13c20SShubham Bansal 	emit_a32_mov_r(fplo, ARM_IP, true, false, ctx);
119339c13c20SShubham Bansal 	emit_a32_mov_i(fphi, 0, true, ctx);
119439c13c20SShubham Bansal 
119539c13c20SShubham Bansal 	/* mov r4, 0 */
119639c13c20SShubham Bansal 	emit(ARM_MOV_I(r4, 0), ctx);
119739c13c20SShubham Bansal 
119839c13c20SShubham Bansal 	/* Move BPF_CTX to BPF_R1 */
119939c13c20SShubham Bansal 	emit(ARM_MOV_R(r3, r4), ctx);
120039c13c20SShubham Bansal 	emit(ARM_MOV_R(r2, r0), ctx);
120139c13c20SShubham Bansal 	/* Initialize Tail Count */
120239c13c20SShubham Bansal 	emit(ARM_STR_I(r4, ARM_SP, STACK_VAR(tcc[0])), ctx);
120339c13c20SShubham Bansal 	emit(ARM_STR_I(r4, ARM_SP, STACK_VAR(tcc[1])), ctx);
120439c13c20SShubham Bansal 	/* end of prologue */
120539c13c20SShubham Bansal }
120639c13c20SShubham Bansal 
120702088d9bSRussell King /* restore callee saved registers. */
120839c13c20SShubham Bansal static void build_epilogue(struct jit_ctx *ctx)
120939c13c20SShubham Bansal {
121039c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
121102088d9bSRussell King 	/* When using frame pointers, some additional registers need to
121202088d9bSRussell King 	 * be loaded. */
121302088d9bSRussell King 	u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
121402088d9bSRussell King 	emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
121539c13c20SShubham Bansal 	emit(ARM_LDM(ARM_SP, reg_set), ctx);
121639c13c20SShubham Bansal #else
121739c13c20SShubham Bansal 	/* Restore callee saved registers. */
121802088d9bSRussell King 	emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
121902088d9bSRussell King 	emit(ARM_POP(CALLEE_POP_MASK), ctx);
122039c13c20SShubham Bansal #endif
122139c13c20SShubham Bansal }
122239c13c20SShubham Bansal 
122339c13c20SShubham Bansal /*
122439c13c20SShubham Bansal  * Convert an eBPF instruction to native instruction, i.e
122539c13c20SShubham Bansal  * JITs an eBPF instruction.
122639c13c20SShubham Bansal  * Returns :
122739c13c20SShubham Bansal  *	0  - Successfully JITed an 8-byte eBPF instruction
122839c13c20SShubham Bansal  *	>0 - Successfully JITed a 16-byte eBPF instruction
122939c13c20SShubham Bansal  *	<0 - Failed to JIT.
123039c13c20SShubham Bansal  */
123139c13c20SShubham Bansal static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
123239c13c20SShubham Bansal {
123339c13c20SShubham Bansal 	const u8 code = insn->code;
123439c13c20SShubham Bansal 	const u8 *dst = bpf2a32[insn->dst_reg];
123539c13c20SShubham Bansal 	const u8 *src = bpf2a32[insn->src_reg];
123639c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
123739c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
123839c13c20SShubham Bansal 	const s16 off = insn->off;
123939c13c20SShubham Bansal 	const s32 imm = insn->imm;
124039c13c20SShubham Bansal 	const int i = insn - ctx->prog->insnsi;
124139c13c20SShubham Bansal 	const bool is64 = BPF_CLASS(code) == BPF_ALU64;
124239c13c20SShubham Bansal 	const bool dstk = is_on_stack(insn->dst_reg);
124339c13c20SShubham Bansal 	const bool sstk = is_on_stack(insn->src_reg);
124439c13c20SShubham Bansal 	u8 rd, rt, rm, rn;
124539c13c20SShubham Bansal 	s32 jmp_offset;
124639c13c20SShubham Bansal 
124739c13c20SShubham Bansal #define check_imm(bits, imm) do {				\
12482b589a7eSWang YanQing 	if ((imm) >= (1 << ((bits) - 1)) ||			\
12492b589a7eSWang YanQing 	    (imm) < -(1 << ((bits) - 1))) {			\
125039c13c20SShubham Bansal 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
125139c13c20SShubham Bansal 			i, imm, imm);				\
125239c13c20SShubham Bansal 		return -EINVAL;					\
125339c13c20SShubham Bansal 	}							\
125439c13c20SShubham Bansal } while (0)
125539c13c20SShubham Bansal #define check_imm24(imm) check_imm(24, imm)
1256ddecdfceSMircea Gherzan 
125734805931SDaniel Borkmann 	switch (code) {
125839c13c20SShubham Bansal 	/* ALU operations */
1259ddecdfceSMircea Gherzan 
126039c13c20SShubham Bansal 	/* dst = src */
126139c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_K:
126239c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_X:
126339c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_K:
126439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_X:
126539c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
126639c13c20SShubham Bansal 		case BPF_X:
126739c13c20SShubham Bansal 			emit_a32_mov_r64(is64, dst, src, dstk, sstk, ctx);
126839c13c20SShubham Bansal 			break;
126939c13c20SShubham Bansal 		case BPF_K:
127039c13c20SShubham Bansal 			/* Sign-extend immediate value to destination reg */
127139c13c20SShubham Bansal 			emit_a32_mov_i64(is64, dst, imm, dstk, ctx);
127239c13c20SShubham Bansal 			break;
1273ddecdfceSMircea Gherzan 		}
1274ddecdfceSMircea Gherzan 		break;
127539c13c20SShubham Bansal 	/* dst = dst + src/imm */
127639c13c20SShubham Bansal 	/* dst = dst - src/imm */
127739c13c20SShubham Bansal 	/* dst = dst | src/imm */
127839c13c20SShubham Bansal 	/* dst = dst & src/imm */
127939c13c20SShubham Bansal 	/* dst = dst ^ src/imm */
128039c13c20SShubham Bansal 	/* dst = dst * src/imm */
128139c13c20SShubham Bansal 	/* dst = dst << src */
128239c13c20SShubham Bansal 	/* dst = dst >> src */
128334805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_K:
128434805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_X:
128534805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_K:
128634805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_X:
128734805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_K:
128834805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_X:
128934805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_K:
129034805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_X:
129139c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_K:
129239c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_X:
129339c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_K:
129439c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_X:
129534805931SDaniel Borkmann 	case BPF_ALU | BPF_LSH | BPF_X:
129634805931SDaniel Borkmann 	case BPF_ALU | BPF_RSH | BPF_X:
129739c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_K:
129839c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_X:
129939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_K:
130039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_X:
130139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_K:
130239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_X:
130339c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_K:
130439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_X:
130539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_K:
130639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_X:
130739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_K:
130839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_X:
130939c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
131039c13c20SShubham Bansal 		case BPF_X:
131139c13c20SShubham Bansal 			emit_a32_alu_r64(is64, dst, src, dstk, sstk,
131239c13c20SShubham Bansal 					 ctx, BPF_OP(code));
1313ddecdfceSMircea Gherzan 			break;
131439c13c20SShubham Bansal 		case BPF_K:
131539c13c20SShubham Bansal 			/* Move immediate value to the temporary register
131639c13c20SShubham Bansal 			 * and then do the ALU operation on the temporary
131739c13c20SShubham Bansal 			 * register as this will sign-extend the immediate
131839c13c20SShubham Bansal 			 * value into temporary reg and then it would be
131939c13c20SShubham Bansal 			 * safe to do the operation on it.
132039c13c20SShubham Bansal 			 */
132139c13c20SShubham Bansal 			emit_a32_mov_i64(is64, tmp2, imm, false, ctx);
132239c13c20SShubham Bansal 			emit_a32_alu_r64(is64, dst, tmp2, dstk, false,
132339c13c20SShubham Bansal 					 ctx, BPF_OP(code));
132439c13c20SShubham Bansal 			break;
132539c13c20SShubham Bansal 		}
132639c13c20SShubham Bansal 		break;
132739c13c20SShubham Bansal 	/* dst = dst / src(imm) */
132839c13c20SShubham Bansal 	/* dst = dst % src(imm) */
132939c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_K:
133039c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_X:
133139c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_K:
133239c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_X:
133339c13c20SShubham Bansal 		rt = src_lo;
133439c13c20SShubham Bansal 		rd = dstk ? tmp2[1] : dst_lo;
133539c13c20SShubham Bansal 		if (dstk)
133639c13c20SShubham Bansal 			emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
133739c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
133839c13c20SShubham Bansal 		case BPF_X:
133939c13c20SShubham Bansal 			rt = sstk ? tmp2[0] : rt;
134039c13c20SShubham Bansal 			if (sstk)
134139c13c20SShubham Bansal 				emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)),
134239c13c20SShubham Bansal 				     ctx);
134339c13c20SShubham Bansal 			break;
134439c13c20SShubham Bansal 		case BPF_K:
134539c13c20SShubham Bansal 			rt = tmp2[0];
134639c13c20SShubham Bansal 			emit_a32_mov_i(rt, imm, false, ctx);
134739c13c20SShubham Bansal 			break;
134839c13c20SShubham Bansal 		}
134939c13c20SShubham Bansal 		emit_udivmod(rd, rd, rt, ctx, BPF_OP(code));
135039c13c20SShubham Bansal 		if (dstk)
135139c13c20SShubham Bansal 			emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
135239c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
135339c13c20SShubham Bansal 		break;
135439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_K:
135539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_X:
135639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_K:
135739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_X:
135839c13c20SShubham Bansal 		goto notyet;
135939c13c20SShubham Bansal 	/* dst = dst >> imm */
136039c13c20SShubham Bansal 	/* dst = dst << imm */
136139c13c20SShubham Bansal 	case BPF_ALU | BPF_RSH | BPF_K:
136239c13c20SShubham Bansal 	case BPF_ALU | BPF_LSH | BPF_K:
136339c13c20SShubham Bansal 		if (unlikely(imm > 31))
136439c13c20SShubham Bansal 			return -EINVAL;
136539c13c20SShubham Bansal 		if (imm)
136639c13c20SShubham Bansal 			emit_a32_alu_i(dst_lo, imm, dstk, ctx, BPF_OP(code));
136739c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
136839c13c20SShubham Bansal 		break;
136939c13c20SShubham Bansal 	/* dst = dst << imm */
137039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_K:
137139c13c20SShubham Bansal 		if (unlikely(imm > 63))
137239c13c20SShubham Bansal 			return -EINVAL;
137339c13c20SShubham Bansal 		emit_a32_lsh_i64(dst, dstk, imm, ctx);
137439c13c20SShubham Bansal 		break;
137539c13c20SShubham Bansal 	/* dst = dst >> imm */
137639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_K:
137739c13c20SShubham Bansal 		if (unlikely(imm > 63))
137839c13c20SShubham Bansal 			return -EINVAL;
137968565a1aSWang YanQing 		emit_a32_rsh_i64(dst, dstk, imm, ctx);
138039c13c20SShubham Bansal 		break;
138139c13c20SShubham Bansal 	/* dst = dst << src */
138239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_X:
138339c13c20SShubham Bansal 		emit_a32_lsh_r64(dst, src, dstk, sstk, ctx);
138439c13c20SShubham Bansal 		break;
138539c13c20SShubham Bansal 	/* dst = dst >> src */
138639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_X:
138768565a1aSWang YanQing 		emit_a32_rsh_r64(dst, src, dstk, sstk, ctx);
138839c13c20SShubham Bansal 		break;
138939c13c20SShubham Bansal 	/* dst = dst >> src (signed) */
139039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_X:
139139c13c20SShubham Bansal 		emit_a32_arsh_r64(dst, src, dstk, sstk, ctx);
139239c13c20SShubham Bansal 		break;
139339c13c20SShubham Bansal 	/* dst = dst >> imm (signed) */
139439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_K:
139539c13c20SShubham Bansal 		if (unlikely(imm > 63))
139639c13c20SShubham Bansal 			return -EINVAL;
139739c13c20SShubham Bansal 		emit_a32_arsh_i64(dst, dstk, imm, ctx);
139839c13c20SShubham Bansal 		break;
139939c13c20SShubham Bansal 	/* dst = ~dst */
140034805931SDaniel Borkmann 	case BPF_ALU | BPF_NEG:
140139c13c20SShubham Bansal 		emit_a32_alu_i(dst_lo, 0, dstk, ctx, BPF_OP(code));
140239c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
1403ddecdfceSMircea Gherzan 		break;
140439c13c20SShubham Bansal 	/* dst = ~dst (64 bit) */
140539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_NEG:
140639c13c20SShubham Bansal 		emit_a32_neg64(dst, dstk, ctx);
1407ddecdfceSMircea Gherzan 		break;
140839c13c20SShubham Bansal 	/* dst = dst * src/imm */
140939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_X:
141039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_K:
141139c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
141239c13c20SShubham Bansal 		case BPF_X:
141339c13c20SShubham Bansal 			emit_a32_mul_r64(dst, src, dstk, sstk, ctx);
1414ddecdfceSMircea Gherzan 			break;
141539c13c20SShubham Bansal 		case BPF_K:
141639c13c20SShubham Bansal 			/* Move immediate value to the temporary register
141739c13c20SShubham Bansal 			 * and then do the multiplication on it as this
141839c13c20SShubham Bansal 			 * will sign-extend the immediate value into temp
141939c13c20SShubham Bansal 			 * reg then it would be safe to do the operation
142039c13c20SShubham Bansal 			 * on it.
14215bf705b4SNicolas Schichan 			 */
142239c13c20SShubham Bansal 			emit_a32_mov_i64(is64, tmp2, imm, false, ctx);
142339c13c20SShubham Bansal 			emit_a32_mul_r64(dst, tmp2, dstk, false, ctx);
142439c13c20SShubham Bansal 			break;
14255bf705b4SNicolas Schichan 		}
1426ddecdfceSMircea Gherzan 		break;
142739c13c20SShubham Bansal 	/* dst = htole(dst) */
142839c13c20SShubham Bansal 	/* dst = htobe(dst) */
142939c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_LE:
143039c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_BE:
143139c13c20SShubham Bansal 		rd = dstk ? tmp[0] : dst_hi;
143239c13c20SShubham Bansal 		rt = dstk ? tmp[1] : dst_lo;
143339c13c20SShubham Bansal 		if (dstk) {
143439c13c20SShubham Bansal 			emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(dst_lo)), ctx);
143539c13c20SShubham Bansal 			emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_hi)), ctx);
1436c18fe54bSNicolas Schichan 		}
143739c13c20SShubham Bansal 		if (BPF_SRC(code) == BPF_FROM_LE)
143839c13c20SShubham Bansal 			goto emit_bswap_uxt;
143939c13c20SShubham Bansal 		switch (imm) {
144039c13c20SShubham Bansal 		case 16:
144139c13c20SShubham Bansal 			emit_rev16(rt, rt, ctx);
144239c13c20SShubham Bansal 			goto emit_bswap_uxt;
144339c13c20SShubham Bansal 		case 32:
144439c13c20SShubham Bansal 			emit_rev32(rt, rt, ctx);
144539c13c20SShubham Bansal 			goto emit_bswap_uxt;
144639c13c20SShubham Bansal 		case 64:
144739c13c20SShubham Bansal 			emit_rev32(ARM_LR, rt, ctx);
144839c13c20SShubham Bansal 			emit_rev32(rt, rd, ctx);
144939c13c20SShubham Bansal 			emit(ARM_MOV_R(rd, ARM_LR), ctx);
1450bf0098f2SDaniel Borkmann 			break;
145139c13c20SShubham Bansal 		}
145239c13c20SShubham Bansal 		goto exit;
145339c13c20SShubham Bansal emit_bswap_uxt:
145439c13c20SShubham Bansal 		switch (imm) {
145539c13c20SShubham Bansal 		case 16:
145639c13c20SShubham Bansal 			/* zero-extend 16 bits into 64 bits */
145739c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
145839c13c20SShubham Bansal 			emit_a32_mov_i(tmp2[1], 0xffff, false, ctx);
145939c13c20SShubham Bansal 			emit(ARM_AND_R(rt, rt, tmp2[1]), ctx);
146039c13c20SShubham Bansal #else /* ARMv6+ */
146139c13c20SShubham Bansal 			emit(ARM_UXTH(rt, rt), ctx);
14621447f93fSNicolas Schichan #endif
146339c13c20SShubham Bansal 			emit(ARM_EOR_R(rd, rd, rd), ctx);
14641447f93fSNicolas Schichan 			break;
146539c13c20SShubham Bansal 		case 32:
146639c13c20SShubham Bansal 			/* zero-extend 32 bits into 64 bits */
146739c13c20SShubham Bansal 			emit(ARM_EOR_R(rd, rd, rd), ctx);
1468ddecdfceSMircea Gherzan 			break;
146939c13c20SShubham Bansal 		case 64:
147039c13c20SShubham Bansal 			/* nop */
147139c13c20SShubham Bansal 			break;
147239c13c20SShubham Bansal 		}
147339c13c20SShubham Bansal exit:
147439c13c20SShubham Bansal 		if (dstk) {
147539c13c20SShubham Bansal 			emit(ARM_STR_I(rt, ARM_SP, STACK_VAR(dst_lo)), ctx);
147639c13c20SShubham Bansal 			emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_hi)), ctx);
147739c13c20SShubham Bansal 		}
147839c13c20SShubham Bansal 		break;
147939c13c20SShubham Bansal 	/* dst = imm64 */
148039c13c20SShubham Bansal 	case BPF_LD | BPF_IMM | BPF_DW:
148139c13c20SShubham Bansal 	{
148239c13c20SShubham Bansal 		const struct bpf_insn insn1 = insn[1];
148339c13c20SShubham Bansal 		u32 hi, lo = imm;
1484303249abSNicolas Schichan 
148539c13c20SShubham Bansal 		hi = insn1.imm;
148639c13c20SShubham Bansal 		emit_a32_mov_i(dst_lo, lo, dstk, ctx);
148739c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, hi, dstk, ctx);
148839c13c20SShubham Bansal 
148939c13c20SShubham Bansal 		return 1;
149039c13c20SShubham Bansal 	}
149139c13c20SShubham Bansal 	/* LDX: dst = *(size *)(src + off) */
149239c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_W:
149339c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_H:
149439c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_B:
149539c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_DW:
149639c13c20SShubham Bansal 		rn = sstk ? tmp2[1] : src_lo;
149739c13c20SShubham Bansal 		if (sstk)
149839c13c20SShubham Bansal 			emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_lo)), ctx);
1499ec19e02bSRussell King 		emit_ldx_r(dst, rn, dstk, off, ctx, BPF_SIZE(code));
150039c13c20SShubham Bansal 		break;
150139c13c20SShubham Bansal 	/* ST: *(size *)(dst + off) = imm */
150239c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_W:
150339c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_H:
150439c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_B:
150539c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_DW:
150639c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
150739c13c20SShubham Bansal 		case BPF_DW:
150839c13c20SShubham Bansal 			/* Sign-extend immediate value into temp reg */
150939c13c20SShubham Bansal 			emit_a32_mov_i64(true, tmp2, imm, false, ctx);
151039c13c20SShubham Bansal 			emit_str_r(dst_lo, tmp2[1], dstk, off, ctx, BPF_W);
151139c13c20SShubham Bansal 			emit_str_r(dst_lo, tmp2[0], dstk, off+4, ctx, BPF_W);
151239c13c20SShubham Bansal 			break;
151339c13c20SShubham Bansal 		case BPF_W:
151439c13c20SShubham Bansal 		case BPF_H:
151539c13c20SShubham Bansal 		case BPF_B:
151639c13c20SShubham Bansal 			emit_a32_mov_i(tmp2[1], imm, false, ctx);
151739c13c20SShubham Bansal 			emit_str_r(dst_lo, tmp2[1], dstk, off, ctx,
151839c13c20SShubham Bansal 				   BPF_SIZE(code));
151939c13c20SShubham Bansal 			break;
152039c13c20SShubham Bansal 		}
152139c13c20SShubham Bansal 		break;
152239c13c20SShubham Bansal 	/* STX XADD: lock *(u32 *)(dst + off) += src */
152339c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_W:
152439c13c20SShubham Bansal 	/* STX XADD: lock *(u64 *)(dst + off) += src */
152539c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_DW:
152639c13c20SShubham Bansal 		goto notyet;
152739c13c20SShubham Bansal 	/* STX: *(size *)(dst + off) = src */
152839c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_W:
152939c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_H:
153039c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_B:
153139c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_DW:
153239c13c20SShubham Bansal 	{
153339c13c20SShubham Bansal 		u8 sz = BPF_SIZE(code);
153439c13c20SShubham Bansal 
153539c13c20SShubham Bansal 		rn = sstk ? tmp2[1] : src_lo;
153639c13c20SShubham Bansal 		rm = sstk ? tmp2[0] : src_hi;
153739c13c20SShubham Bansal 		if (sstk) {
153839c13c20SShubham Bansal 			emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_lo)), ctx);
153939c13c20SShubham Bansal 			emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(src_hi)), ctx);
154039c13c20SShubham Bansal 		}
154139c13c20SShubham Bansal 
154239c13c20SShubham Bansal 		/* Store the value */
154339c13c20SShubham Bansal 		if (BPF_SIZE(code) == BPF_DW) {
154439c13c20SShubham Bansal 			emit_str_r(dst_lo, rn, dstk, off, ctx, BPF_W);
154539c13c20SShubham Bansal 			emit_str_r(dst_lo, rm, dstk, off+4, ctx, BPF_W);
154639c13c20SShubham Bansal 		} else {
154739c13c20SShubham Bansal 			emit_str_r(dst_lo, rn, dstk, off, ctx, sz);
154839c13c20SShubham Bansal 		}
154939c13c20SShubham Bansal 		break;
155039c13c20SShubham Bansal 	}
155139c13c20SShubham Bansal 	/* PC += off if dst == src */
155239c13c20SShubham Bansal 	/* PC += off if dst > src */
155339c13c20SShubham Bansal 	/* PC += off if dst >= src */
155439c13c20SShubham Bansal 	/* PC += off if dst < src */
155539c13c20SShubham Bansal 	/* PC += off if dst <= src */
155639c13c20SShubham Bansal 	/* PC += off if dst != src */
155739c13c20SShubham Bansal 	/* PC += off if dst > src (signed) */
155839c13c20SShubham Bansal 	/* PC += off if dst >= src (signed) */
155939c13c20SShubham Bansal 	/* PC += off if dst < src (signed) */
156039c13c20SShubham Bansal 	/* PC += off if dst <= src (signed) */
156139c13c20SShubham Bansal 	/* PC += off if dst & src */
156239c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_X:
156339c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_X:
156439c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_X:
156539c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_X:
156639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_X:
156739c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_X:
156839c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_X:
156939c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_X:
157039c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_X:
157139c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_X:
157239c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_X:
157339c13c20SShubham Bansal 		/* Setup source registers */
157439c13c20SShubham Bansal 		rm = sstk ? tmp2[0] : src_hi;
157539c13c20SShubham Bansal 		rn = sstk ? tmp2[1] : src_lo;
157639c13c20SShubham Bansal 		if (sstk) {
157739c13c20SShubham Bansal 			emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_lo)), ctx);
157839c13c20SShubham Bansal 			emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(src_hi)), ctx);
157939c13c20SShubham Bansal 		}
158039c13c20SShubham Bansal 		goto go_jmp;
158139c13c20SShubham Bansal 	/* PC += off if dst == imm */
158239c13c20SShubham Bansal 	/* PC += off if dst > imm */
158339c13c20SShubham Bansal 	/* PC += off if dst >= imm */
158439c13c20SShubham Bansal 	/* PC += off if dst < imm */
158539c13c20SShubham Bansal 	/* PC += off if dst <= imm */
158639c13c20SShubham Bansal 	/* PC += off if dst != imm */
158739c13c20SShubham Bansal 	/* PC += off if dst > imm (signed) */
158839c13c20SShubham Bansal 	/* PC += off if dst >= imm (signed) */
158939c13c20SShubham Bansal 	/* PC += off if dst < imm (signed) */
159039c13c20SShubham Bansal 	/* PC += off if dst <= imm (signed) */
159139c13c20SShubham Bansal 	/* PC += off if dst & imm */
159239c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_K:
159339c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_K:
159439c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_K:
159539c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_K:
159639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_K:
159739c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_K:
159839c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_K:
159939c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_K:
160039c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_K:
160139c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_K:
160239c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_K:
160339c13c20SShubham Bansal 		if (off == 0)
160439c13c20SShubham Bansal 			break;
160539c13c20SShubham Bansal 		rm = tmp2[0];
160639c13c20SShubham Bansal 		rn = tmp2[1];
160739c13c20SShubham Bansal 		/* Sign-extend immediate value */
160839c13c20SShubham Bansal 		emit_a32_mov_i64(true, tmp2, imm, false, ctx);
160939c13c20SShubham Bansal go_jmp:
161039c13c20SShubham Bansal 		/* Setup destination register */
161139c13c20SShubham Bansal 		rd = dstk ? tmp[0] : dst_hi;
161239c13c20SShubham Bansal 		rt = dstk ? tmp[1] : dst_lo;
161339c13c20SShubham Bansal 		if (dstk) {
161439c13c20SShubham Bansal 			emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(dst_lo)), ctx);
161539c13c20SShubham Bansal 			emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_hi)), ctx);
161639c13c20SShubham Bansal 		}
161739c13c20SShubham Bansal 
161839c13c20SShubham Bansal 		/* Check for the condition */
161939c13c20SShubham Bansal 		emit_ar_r(rd, rt, rm, rn, ctx, BPF_OP(code));
162039c13c20SShubham Bansal 
162139c13c20SShubham Bansal 		/* Setup JUMP instruction */
162239c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
162339c13c20SShubham Bansal 		switch (BPF_OP(code)) {
162439c13c20SShubham Bansal 		case BPF_JNE:
162539c13c20SShubham Bansal 		case BPF_JSET:
162639c13c20SShubham Bansal 			_emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
162739c13c20SShubham Bansal 			break;
162839c13c20SShubham Bansal 		case BPF_JEQ:
162939c13c20SShubham Bansal 			_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
163039c13c20SShubham Bansal 			break;
163139c13c20SShubham Bansal 		case BPF_JGT:
163239c13c20SShubham Bansal 			_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
163339c13c20SShubham Bansal 			break;
163439c13c20SShubham Bansal 		case BPF_JGE:
163539c13c20SShubham Bansal 			_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
163639c13c20SShubham Bansal 			break;
163739c13c20SShubham Bansal 		case BPF_JSGT:
163839c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
163939c13c20SShubham Bansal 			break;
164039c13c20SShubham Bansal 		case BPF_JSGE:
164139c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
164239c13c20SShubham Bansal 			break;
164339c13c20SShubham Bansal 		case BPF_JLE:
164439c13c20SShubham Bansal 			_emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
164539c13c20SShubham Bansal 			break;
164639c13c20SShubham Bansal 		case BPF_JLT:
164739c13c20SShubham Bansal 			_emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
164839c13c20SShubham Bansal 			break;
164939c13c20SShubham Bansal 		case BPF_JSLT:
165039c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
165139c13c20SShubham Bansal 			break;
165239c13c20SShubham Bansal 		case BPF_JSLE:
165339c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
165439c13c20SShubham Bansal 			break;
165539c13c20SShubham Bansal 		}
165639c13c20SShubham Bansal 		break;
165739c13c20SShubham Bansal 	/* JMP OFF */
165839c13c20SShubham Bansal 	case BPF_JMP | BPF_JA:
165939c13c20SShubham Bansal 	{
166039c13c20SShubham Bansal 		if (off == 0)
166139c13c20SShubham Bansal 			break;
166239c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
166339c13c20SShubham Bansal 		check_imm24(jmp_offset);
166439c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
166539c13c20SShubham Bansal 		break;
166639c13c20SShubham Bansal 	}
166739c13c20SShubham Bansal 	/* tail call */
166839c13c20SShubham Bansal 	case BPF_JMP | BPF_TAIL_CALL:
166939c13c20SShubham Bansal 		if (emit_bpf_tail_call(ctx))
167039c13c20SShubham Bansal 			return -EFAULT;
167139c13c20SShubham Bansal 		break;
167239c13c20SShubham Bansal 	/* function call */
167339c13c20SShubham Bansal 	case BPF_JMP | BPF_CALL:
167439c13c20SShubham Bansal 	{
167539c13c20SShubham Bansal 		const u8 *r0 = bpf2a32[BPF_REG_0];
167639c13c20SShubham Bansal 		const u8 *r1 = bpf2a32[BPF_REG_1];
167739c13c20SShubham Bansal 		const u8 *r2 = bpf2a32[BPF_REG_2];
167839c13c20SShubham Bansal 		const u8 *r3 = bpf2a32[BPF_REG_3];
167939c13c20SShubham Bansal 		const u8 *r4 = bpf2a32[BPF_REG_4];
168039c13c20SShubham Bansal 		const u8 *r5 = bpf2a32[BPF_REG_5];
168139c13c20SShubham Bansal 		const u32 func = (u32)__bpf_call_base + (u32)imm;
168239c13c20SShubham Bansal 
168339c13c20SShubham Bansal 		emit_a32_mov_r64(true, r0, r1, false, false, ctx);
168439c13c20SShubham Bansal 		emit_a32_mov_r64(true, r1, r2, false, true, ctx);
168539c13c20SShubham Bansal 		emit_push_r64(r5, 0, ctx);
168639c13c20SShubham Bansal 		emit_push_r64(r4, 8, ctx);
168739c13c20SShubham Bansal 		emit_push_r64(r3, 16, ctx);
168839c13c20SShubham Bansal 
168939c13c20SShubham Bansal 		emit_a32_mov_i(tmp[1], func, false, ctx);
169039c13c20SShubham Bansal 		emit_blx_r(tmp[1], ctx);
169139c13c20SShubham Bansal 
169239c13c20SShubham Bansal 		emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
169339c13c20SShubham Bansal 		break;
169439c13c20SShubham Bansal 	}
169539c13c20SShubham Bansal 	/* function return */
169639c13c20SShubham Bansal 	case BPF_JMP | BPF_EXIT:
169739c13c20SShubham Bansal 		/* Optimization: when last instruction is EXIT
169839c13c20SShubham Bansal 		 * simply fallthrough to epilogue.
169939c13c20SShubham Bansal 		 */
170039c13c20SShubham Bansal 		if (i == ctx->prog->len - 1)
170139c13c20SShubham Bansal 			break;
170239c13c20SShubham Bansal 		jmp_offset = epilogue_offset(ctx);
170339c13c20SShubham Bansal 		check_imm24(jmp_offset);
170439c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
170539c13c20SShubham Bansal 		break;
170639c13c20SShubham Bansal notyet:
170739c13c20SShubham Bansal 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
170839c13c20SShubham Bansal 		return -EFAULT;
170939c13c20SShubham Bansal 	default:
171039c13c20SShubham Bansal 		pr_err_once("unknown opcode %02x\n", code);
171139c13c20SShubham Bansal 		return -EINVAL;
1712ddecdfceSMircea Gherzan 	}
17130b59d880SNicolas Schichan 
17140b59d880SNicolas Schichan 	if (ctx->flags & FLAG_IMM_OVERFLOW)
17150b59d880SNicolas Schichan 		/*
17160b59d880SNicolas Schichan 		 * this instruction generated an overflow when
17170b59d880SNicolas Schichan 		 * trying to access the literal pool, so
17180b59d880SNicolas Schichan 		 * delegate this filter to the kernel interpreter.
17190b59d880SNicolas Schichan 		 */
17200b59d880SNicolas Schichan 		return -1;
172139c13c20SShubham Bansal 	return 0;
1722ddecdfceSMircea Gherzan }
1723ddecdfceSMircea Gherzan 
172439c13c20SShubham Bansal static int build_body(struct jit_ctx *ctx)
172539c13c20SShubham Bansal {
172639c13c20SShubham Bansal 	const struct bpf_prog *prog = ctx->prog;
172739c13c20SShubham Bansal 	unsigned int i;
172839c13c20SShubham Bansal 
172939c13c20SShubham Bansal 	for (i = 0; i < prog->len; i++) {
173039c13c20SShubham Bansal 		const struct bpf_insn *insn = &(prog->insnsi[i]);
173139c13c20SShubham Bansal 		int ret;
173239c13c20SShubham Bansal 
173339c13c20SShubham Bansal 		ret = build_insn(insn, ctx);
173439c13c20SShubham Bansal 
173539c13c20SShubham Bansal 		/* It's used with loading the 64 bit immediate value. */
173639c13c20SShubham Bansal 		if (ret > 0) {
173739c13c20SShubham Bansal 			i++;
1738ddecdfceSMircea Gherzan 			if (ctx->target == NULL)
173939c13c20SShubham Bansal 				ctx->offsets[i] = ctx->idx;
174039c13c20SShubham Bansal 			continue;
174139c13c20SShubham Bansal 		}
174239c13c20SShubham Bansal 
174339c13c20SShubham Bansal 		if (ctx->target == NULL)
174439c13c20SShubham Bansal 			ctx->offsets[i] = ctx->idx;
174539c13c20SShubham Bansal 
174639c13c20SShubham Bansal 		/* If unsuccesfull, return with error code */
174739c13c20SShubham Bansal 		if (ret)
174839c13c20SShubham Bansal 			return ret;
174939c13c20SShubham Bansal 	}
175039c13c20SShubham Bansal 	return 0;
175139c13c20SShubham Bansal }
175239c13c20SShubham Bansal 
175339c13c20SShubham Bansal static int validate_code(struct jit_ctx *ctx)
175439c13c20SShubham Bansal {
175539c13c20SShubham Bansal 	int i;
175639c13c20SShubham Bansal 
175739c13c20SShubham Bansal 	for (i = 0; i < ctx->idx; i++) {
175839c13c20SShubham Bansal 		if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
175939c13c20SShubham Bansal 			return -1;
176039c13c20SShubham Bansal 	}
1761ddecdfceSMircea Gherzan 
1762ddecdfceSMircea Gherzan 	return 0;
1763ddecdfceSMircea Gherzan }
1764ddecdfceSMircea Gherzan 
176539c13c20SShubham Bansal void bpf_jit_compile(struct bpf_prog *prog)
1766ddecdfceSMircea Gherzan {
176739c13c20SShubham Bansal 	/* Nothing to do here. We support Internal BPF. */
176839c13c20SShubham Bansal }
1769ddecdfceSMircea Gherzan 
177039c13c20SShubham Bansal struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
177139c13c20SShubham Bansal {
177239c13c20SShubham Bansal 	struct bpf_prog *tmp, *orig_prog = prog;
177339c13c20SShubham Bansal 	struct bpf_binary_header *header;
177439c13c20SShubham Bansal 	bool tmp_blinded = false;
177539c13c20SShubham Bansal 	struct jit_ctx ctx;
177639c13c20SShubham Bansal 	unsigned int tmp_idx;
177739c13c20SShubham Bansal 	unsigned int image_size;
177839c13c20SShubham Bansal 	u8 *image_ptr;
177939c13c20SShubham Bansal 
178039c13c20SShubham Bansal 	/* If BPF JIT was not enabled then we must fall back to
178139c13c20SShubham Bansal 	 * the interpreter.
178239c13c20SShubham Bansal 	 */
178360b58afcSAlexei Starovoitov 	if (!prog->jit_requested)
178439c13c20SShubham Bansal 		return orig_prog;
178539c13c20SShubham Bansal 
178639c13c20SShubham Bansal 	/* If constant blinding was enabled and we failed during blinding
178739c13c20SShubham Bansal 	 * then we must fall back to the interpreter. Otherwise, we save
178839c13c20SShubham Bansal 	 * the new JITed code.
178939c13c20SShubham Bansal 	 */
179039c13c20SShubham Bansal 	tmp = bpf_jit_blind_constants(prog);
179139c13c20SShubham Bansal 
179239c13c20SShubham Bansal 	if (IS_ERR(tmp))
179339c13c20SShubham Bansal 		return orig_prog;
179439c13c20SShubham Bansal 	if (tmp != prog) {
179539c13c20SShubham Bansal 		tmp_blinded = true;
179639c13c20SShubham Bansal 		prog = tmp;
179739c13c20SShubham Bansal 	}
1798ddecdfceSMircea Gherzan 
1799ddecdfceSMircea Gherzan 	memset(&ctx, 0, sizeof(ctx));
180039c13c20SShubham Bansal 	ctx.prog = prog;
1801ddecdfceSMircea Gherzan 
180239c13c20SShubham Bansal 	/* Not able to allocate memory for offsets[] , then
180339c13c20SShubham Bansal 	 * we must fall back to the interpreter
180439c13c20SShubham Bansal 	 */
180539c13c20SShubham Bansal 	ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
180639c13c20SShubham Bansal 	if (ctx.offsets == NULL) {
180739c13c20SShubham Bansal 		prog = orig_prog;
1808ddecdfceSMircea Gherzan 		goto out;
180939c13c20SShubham Bansal 	}
181039c13c20SShubham Bansal 
181139c13c20SShubham Bansal 	/* 1) fake pass to find in the length of the JITed code,
181239c13c20SShubham Bansal 	 * to compute ctx->offsets and other context variables
181339c13c20SShubham Bansal 	 * needed to compute final JITed code.
181439c13c20SShubham Bansal 	 * Also, calculate random starting pointer/start of JITed code
181539c13c20SShubham Bansal 	 * which is prefixed by random number of fault instructions.
181639c13c20SShubham Bansal 	 *
181739c13c20SShubham Bansal 	 * If the first pass fails then there is no chance of it
181839c13c20SShubham Bansal 	 * being successful in the second pass, so just fall back
181939c13c20SShubham Bansal 	 * to the interpreter.
182039c13c20SShubham Bansal 	 */
182139c13c20SShubham Bansal 	if (build_body(&ctx)) {
182239c13c20SShubham Bansal 		prog = orig_prog;
182339c13c20SShubham Bansal 		goto out_off;
182439c13c20SShubham Bansal 	}
1825ddecdfceSMircea Gherzan 
1826ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1827ddecdfceSMircea Gherzan 	build_prologue(&ctx);
1828ddecdfceSMircea Gherzan 	ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1829ddecdfceSMircea Gherzan 
183039c13c20SShubham Bansal 	ctx.epilogue_offset = ctx.idx;
183139c13c20SShubham Bansal 
1832ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1833ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1834ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1835ddecdfceSMircea Gherzan 	ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1836ddecdfceSMircea Gherzan 
1837ddecdfceSMircea Gherzan 	ctx.idx += ctx.imm_count;
1838ddecdfceSMircea Gherzan 	if (ctx.imm_count) {
183939c13c20SShubham Bansal 		ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
184039c13c20SShubham Bansal 		if (ctx.imms == NULL) {
184139c13c20SShubham Bansal 			prog = orig_prog;
184239c13c20SShubham Bansal 			goto out_off;
184339c13c20SShubham Bansal 		}
1844ddecdfceSMircea Gherzan 	}
1845ddecdfceSMircea Gherzan #else
184639c13c20SShubham Bansal 	/* there's nothing about the epilogue on ARMv7 */
1847ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1848ddecdfceSMircea Gherzan #endif
184939c13c20SShubham Bansal 	/* Now we can get the actual image size of the JITed arm code.
185039c13c20SShubham Bansal 	 * Currently, we are not considering the THUMB-2 instructions
185139c13c20SShubham Bansal 	 * for jit, although it can decrease the size of the image.
185239c13c20SShubham Bansal 	 *
185339c13c20SShubham Bansal 	 * As each arm instruction is of length 32bit, we are translating
185439c13c20SShubham Bansal 	 * number of JITed intructions into the size required to store these
185539c13c20SShubham Bansal 	 * JITed code.
185639c13c20SShubham Bansal 	 */
185739c13c20SShubham Bansal 	image_size = sizeof(u32) * ctx.idx;
1858ddecdfceSMircea Gherzan 
185939c13c20SShubham Bansal 	/* Now we know the size of the structure to make */
186039c13c20SShubham Bansal 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
186139c13c20SShubham Bansal 				      sizeof(u32), jit_fill_hole);
186239c13c20SShubham Bansal 	/* Not able to allocate memory for the structure then
186339c13c20SShubham Bansal 	 * we must fall back to the interpretation
186439c13c20SShubham Bansal 	 */
186539c13c20SShubham Bansal 	if (header == NULL) {
186639c13c20SShubham Bansal 		prog = orig_prog;
186739c13c20SShubham Bansal 		goto out_imms;
186839c13c20SShubham Bansal 	}
186939c13c20SShubham Bansal 
187039c13c20SShubham Bansal 	/* 2.) Actual pass to generate final JIT code */
187139c13c20SShubham Bansal 	ctx.target = (u32 *) image_ptr;
1872ddecdfceSMircea Gherzan 	ctx.idx = 0;
187355309dd3SDaniel Borkmann 
1874ddecdfceSMircea Gherzan 	build_prologue(&ctx);
187539c13c20SShubham Bansal 
187639c13c20SShubham Bansal 	/* If building the body of the JITed code fails somehow,
187739c13c20SShubham Bansal 	 * we fall back to the interpretation.
187839c13c20SShubham Bansal 	 */
18790b59d880SNicolas Schichan 	if (build_body(&ctx) < 0) {
188039c13c20SShubham Bansal 		image_ptr = NULL;
18810b59d880SNicolas Schichan 		bpf_jit_binary_free(header);
188239c13c20SShubham Bansal 		prog = orig_prog;
188339c13c20SShubham Bansal 		goto out_imms;
18840b59d880SNicolas Schichan 	}
1885ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1886ddecdfceSMircea Gherzan 
188739c13c20SShubham Bansal 	/* 3.) Extra pass to validate JITed Code */
188839c13c20SShubham Bansal 	if (validate_code(&ctx)) {
188939c13c20SShubham Bansal 		image_ptr = NULL;
189039c13c20SShubham Bansal 		bpf_jit_binary_free(header);
189139c13c20SShubham Bansal 		prog = orig_prog;
189239c13c20SShubham Bansal 		goto out_imms;
189339c13c20SShubham Bansal 	}
1894ebaef649SDaniel Borkmann 	flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
1895ddecdfceSMircea Gherzan 
189639c13c20SShubham Bansal 	if (bpf_jit_enable > 1)
189739c13c20SShubham Bansal 		/* there are 2 passes here */
189839c13c20SShubham Bansal 		bpf_jit_dump(prog->len, image_size, 2, ctx.target);
189939c13c20SShubham Bansal 
190018d405afSDaniel Borkmann 	bpf_jit_binary_lock_ro(header);
190139c13c20SShubham Bansal 	prog->bpf_func = (void *)ctx.target;
190239c13c20SShubham Bansal 	prog->jited = 1;
190339c13c20SShubham Bansal 	prog->jited_len = image_size;
190439c13c20SShubham Bansal 
190539c13c20SShubham Bansal out_imms:
1906ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1907ddecdfceSMircea Gherzan 	if (ctx.imm_count)
1908ddecdfceSMircea Gherzan 		kfree(ctx.imms);
1909ddecdfceSMircea Gherzan #endif
191039c13c20SShubham Bansal out_off:
1911ddecdfceSMircea Gherzan 	kfree(ctx.offsets);
191239c13c20SShubham Bansal out:
191339c13c20SShubham Bansal 	if (tmp_blinded)
191439c13c20SShubham Bansal 		bpf_jit_prog_release_other(prog, prog == orig_prog ?
191539c13c20SShubham Bansal 					   tmp : orig_prog);
191639c13c20SShubham Bansal 	return prog;
1917ddecdfceSMircea Gherzan }
1918ddecdfceSMircea Gherzan 
1919