xref: /openbmc/linux/arch/arm/net/bpf_jit_32.c (revision c648c9c7)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ddecdfceSMircea Gherzan /*
339c13c20SShubham Bansal  * Just-In-Time compiler for eBPF filters on 32bit ARM
4ddecdfceSMircea Gherzan  *
539c13c20SShubham Bansal  * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
6ddecdfceSMircea Gherzan  * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
7ddecdfceSMircea Gherzan  */
8ddecdfceSMircea Gherzan 
939c13c20SShubham Bansal #include <linux/bpf.h>
10ddecdfceSMircea Gherzan #include <linux/bitops.h>
11ddecdfceSMircea Gherzan #include <linux/compiler.h>
12ddecdfceSMircea Gherzan #include <linux/errno.h>
13ddecdfceSMircea Gherzan #include <linux/filter.h>
14ddecdfceSMircea Gherzan #include <linux/netdevice.h>
15ddecdfceSMircea Gherzan #include <linux/string.h>
16ddecdfceSMircea Gherzan #include <linux/slab.h>
17bf0098f2SDaniel Borkmann #include <linux/if_vlan.h>
18e8b56d55SDaniel Borkmann 
19ddecdfceSMircea Gherzan #include <asm/cacheflush.h>
20ddecdfceSMircea Gherzan #include <asm/hwcap.h>
213460743eSBen Dooks #include <asm/opcodes.h>
228c9602d3SRussell King #include <asm/system_info.h>
23ddecdfceSMircea Gherzan 
24ddecdfceSMircea Gherzan #include "bpf_jit_32.h"
25ddecdfceSMircea Gherzan 
2670ec3a6cSRussell King /*
270005e55aSRussell King  * eBPF prog stack layout:
2870ec3a6cSRussell King  *
2970ec3a6cSRussell King  *                         high
300005e55aSRussell King  * original ARM_SP =>     +-----+
310005e55aSRussell King  *                        |     | callee saved registers
320005e55aSRussell King  *                        +-----+ <= (BPF_FP + SCRATCH_SIZE)
3370ec3a6cSRussell King  *                        | ... | eBPF JIT scratch space
340005e55aSRussell King  * eBPF fp register =>    +-----+
350005e55aSRussell King  *   (BPF_FP)             | ... | eBPF prog stack
3670ec3a6cSRussell King  *                        +-----+
3770ec3a6cSRussell King  *                        |RSVD | JIT scratchpad
380005e55aSRussell King  * current ARM_SP =>      +-----+ <= (BPF_FP - STACK_SIZE + SCRATCH_SIZE)
3970ec3a6cSRussell King  *                        |     |
4070ec3a6cSRussell King  *                        | ... | Function call stack
4170ec3a6cSRussell King  *                        |     |
4270ec3a6cSRussell King  *                        +-----+
4370ec3a6cSRussell King  *                          low
440005e55aSRussell King  *
450005e55aSRussell King  * The callee saved registers depends on whether frame pointers are enabled.
460005e55aSRussell King  * With frame pointers (to be compliant with the ABI):
470005e55aSRussell King  *
480005e55aSRussell King  *                              high
49bef8968dSRussell King  * original ARM_SP =>     +--------------+ \
500005e55aSRussell King  *                        |      pc      | |
51bef8968dSRussell King  * current ARM_FP =>      +--------------+ } callee saved registers
52bef8968dSRussell King  *                        |r4-r9,fp,ip,lr| |
53bef8968dSRussell King  *                        +--------------+ /
540005e55aSRussell King  *                              low
550005e55aSRussell King  *
560005e55aSRussell King  * Without frame pointers:
570005e55aSRussell King  *
580005e55aSRussell King  *                              high
59bef8968dSRussell King  * original ARM_SP =>     +--------------+
60bef8968dSRussell King  *                        |  r4-r9,fp,lr | callee saved registers
61bef8968dSRussell King  * current ARM_FP =>      +--------------+
620005e55aSRussell King  *                              low
6302088d9bSRussell King  *
6402088d9bSRussell King  * When popping registers off the stack at the end of a BPF function, we
6502088d9bSRussell King  * reference them via the current ARM_FP register.
6670ec3a6cSRussell King  */
6702088d9bSRussell King #define CALLEE_MASK	(1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \
68bef8968dSRussell King 			 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R9 | \
6902088d9bSRussell King 			 1 << ARM_FP)
7002088d9bSRussell King #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR)
7102088d9bSRussell King #define CALLEE_POP_MASK  (CALLEE_MASK | 1 << ARM_PC)
7270ec3a6cSRussell King 
73d449ceb1SRussell King enum {
74d449ceb1SRussell King 	/* Stack layout - these are offsets from (top of stack - 4) */
75d449ceb1SRussell King 	BPF_R2_HI,
76d449ceb1SRussell King 	BPF_R2_LO,
77d449ceb1SRussell King 	BPF_R3_HI,
78d449ceb1SRussell King 	BPF_R3_LO,
79d449ceb1SRussell King 	BPF_R4_HI,
80d449ceb1SRussell King 	BPF_R4_LO,
81d449ceb1SRussell King 	BPF_R5_HI,
82d449ceb1SRussell King 	BPF_R5_LO,
83d449ceb1SRussell King 	BPF_R7_HI,
84d449ceb1SRussell King 	BPF_R7_LO,
85d449ceb1SRussell King 	BPF_R8_HI,
86d449ceb1SRussell King 	BPF_R8_LO,
87d449ceb1SRussell King 	BPF_R9_HI,
88d449ceb1SRussell King 	BPF_R9_LO,
89d449ceb1SRussell King 	BPF_FP_HI,
90d449ceb1SRussell King 	BPF_FP_LO,
91d449ceb1SRussell King 	BPF_TC_HI,
92d449ceb1SRussell King 	BPF_TC_LO,
93d449ceb1SRussell King 	BPF_AX_HI,
94d449ceb1SRussell King 	BPF_AX_LO,
95d449ceb1SRussell King 	/* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
96d449ceb1SRussell King 	 * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
97d449ceb1SRussell King 	 * BPF_REG_FP and Tail call counts.
98d449ceb1SRussell King 	 */
99d449ceb1SRussell King 	BPF_JIT_SCRATCH_REGS,
100d449ceb1SRussell King };
101d449ceb1SRussell King 
1021c35ba12SRussell King /*
1031c35ba12SRussell King  * Negative "register" values indicate the register is stored on the stack
1041c35ba12SRussell King  * and are the offset from the top of the eBPF JIT scratch space.
1051c35ba12SRussell King  */
1061c35ba12SRussell King #define STACK_OFFSET(k)	(-4 - (k) * 4)
107d449ceb1SRussell King #define SCRATCH_SIZE	(BPF_JIT_SCRATCH_REGS * 4)
108d449ceb1SRussell King 
10996cced4eSRussell King #ifdef CONFIG_FRAME_POINTER
11096cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) ((x) - 4 * hweight16(CALLEE_PUSH_MASK) - 4)
11196cced4eSRussell King #else
11296cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) (x)
11396cced4eSRussell King #endif
11496cced4eSRussell King 
11539c13c20SShubham Bansal #define TMP_REG_1	(MAX_BPF_JIT_REG + 0)	/* TEMP Register 1 */
11639c13c20SShubham Bansal #define TMP_REG_2	(MAX_BPF_JIT_REG + 1)	/* TEMP Register 2 */
11739c13c20SShubham Bansal #define TCALL_CNT	(MAX_BPF_JIT_REG + 2)	/* Tail Call Count */
11839c13c20SShubham Bansal 
11939c13c20SShubham Bansal #define FLAG_IMM_OVERFLOW	(1 << 0)
12039c13c20SShubham Bansal 
121ddecdfceSMircea Gherzan /*
12239c13c20SShubham Bansal  * Map eBPF registers to ARM 32bit registers or stack scratch space.
123ddecdfceSMircea Gherzan  *
12439c13c20SShubham Bansal  * 1. First argument is passed using the arm 32bit registers and rest of the
12539c13c20SShubham Bansal  * arguments are passed on stack scratch space.
1262b589a7eSWang YanQing  * 2. First callee-saved argument is mapped to arm 32 bit registers and rest
12739c13c20SShubham Bansal  * arguments are mapped to scratch space on stack.
12839c13c20SShubham Bansal  * 3. We need two 64 bit temp registers to do complex operations on eBPF
12939c13c20SShubham Bansal  * registers.
13039c13c20SShubham Bansal  *
13139c13c20SShubham Bansal  * As the eBPF registers are all 64 bit registers and arm has only 32 bit
13239c13c20SShubham Bansal  * registers, we have to map each eBPF registers with two arm 32 bit regs or
13339c13c20SShubham Bansal  * scratch memory space and we have to build eBPF 64 bit register from those.
13439c13c20SShubham Bansal  *
13539c13c20SShubham Bansal  */
1361c35ba12SRussell King static const s8 bpf2a32[][2] = {
13739c13c20SShubham Bansal 	/* return value from in-kernel function, and exit value from eBPF */
13839c13c20SShubham Bansal 	[BPF_REG_0] = {ARM_R1, ARM_R0},
13939c13c20SShubham Bansal 	/* arguments from eBPF program to in-kernel function */
14039c13c20SShubham Bansal 	[BPF_REG_1] = {ARM_R3, ARM_R2},
14139c13c20SShubham Bansal 	/* Stored on stack scratch space */
142d449ceb1SRussell King 	[BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)},
143d449ceb1SRussell King 	[BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)},
144d449ceb1SRussell King 	[BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)},
145d449ceb1SRussell King 	[BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)},
14639c13c20SShubham Bansal 	/* callee saved registers that in-kernel function will preserve */
14739c13c20SShubham Bansal 	[BPF_REG_6] = {ARM_R5, ARM_R4},
14839c13c20SShubham Bansal 	/* Stored on stack scratch space */
149d449ceb1SRussell King 	[BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
150d449ceb1SRussell King 	[BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
151d449ceb1SRussell King 	[BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
15239c13c20SShubham Bansal 	/* Read only Frame Pointer to access Stack */
153d449ceb1SRussell King 	[BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
15439c13c20SShubham Bansal 	/* Temporary Register for internal BPF JIT, can be used
15539c13c20SShubham Bansal 	 * for constant blindings and others.
15639c13c20SShubham Bansal 	 */
15739c13c20SShubham Bansal 	[TMP_REG_1] = {ARM_R7, ARM_R6},
158bef8968dSRussell King 	[TMP_REG_2] = {ARM_R9, ARM_R8},
15939c13c20SShubham Bansal 	/* Tail call count. Stored on stack scratch space. */
160d449ceb1SRussell King 	[TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)},
16139c13c20SShubham Bansal 	/* temporary register for blinding constants.
16239c13c20SShubham Bansal 	 * Stored on stack scratch space.
16339c13c20SShubham Bansal 	 */
164d449ceb1SRussell King 	[BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
16539c13c20SShubham Bansal };
16639c13c20SShubham Bansal 
16739c13c20SShubham Bansal #define	dst_lo	dst[1]
16839c13c20SShubham Bansal #define dst_hi	dst[0]
16939c13c20SShubham Bansal #define src_lo	src[1]
17039c13c20SShubham Bansal #define src_hi	src[0]
17139c13c20SShubham Bansal 
17239c13c20SShubham Bansal /*
17339c13c20SShubham Bansal  * JIT Context:
17439c13c20SShubham Bansal  *
17539c13c20SShubham Bansal  * prog			:	bpf_prog
17639c13c20SShubham Bansal  * idx			:	index of current last JITed instruction.
17739c13c20SShubham Bansal  * prologue_bytes	:	bytes used in prologue.
17839c13c20SShubham Bansal  * epilogue_offset	:	offset of epilogue starting.
17939c13c20SShubham Bansal  * offsets		:	array of eBPF instruction offsets in
18039c13c20SShubham Bansal  *				JITed code.
18139c13c20SShubham Bansal  * target		:	final JITed code.
18239c13c20SShubham Bansal  * epilogue_bytes	:	no of bytes used in epilogue.
18339c13c20SShubham Bansal  * imm_count		:	no of immediate counts used for global
18439c13c20SShubham Bansal  *				variables.
18539c13c20SShubham Bansal  * imms			:	array of global variable addresses.
186ddecdfceSMircea Gherzan  */
187ddecdfceSMircea Gherzan 
188ddecdfceSMircea Gherzan struct jit_ctx {
18939c13c20SShubham Bansal 	const struct bpf_prog *prog;
19039c13c20SShubham Bansal 	unsigned int idx;
19139c13c20SShubham Bansal 	unsigned int prologue_bytes;
19239c13c20SShubham Bansal 	unsigned int epilogue_offset;
1938c9602d3SRussell King 	unsigned int cpu_architecture;
194ddecdfceSMircea Gherzan 	u32 flags;
195ddecdfceSMircea Gherzan 	u32 *offsets;
196ddecdfceSMircea Gherzan 	u32 *target;
19739c13c20SShubham Bansal 	u32 stack_size;
198ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
199ddecdfceSMircea Gherzan 	u16 epilogue_bytes;
200ddecdfceSMircea Gherzan 	u16 imm_count;
201ddecdfceSMircea Gherzan 	u32 *imms;
202ddecdfceSMircea Gherzan #endif
203ddecdfceSMircea Gherzan };
204ddecdfceSMircea Gherzan 
205ddecdfceSMircea Gherzan /*
2064560cdffSNicolas Schichan  * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
207ddecdfceSMircea Gherzan  * (where the assembly routines like __aeabi_uidiv could cause problems).
208ddecdfceSMircea Gherzan  */
20939c13c20SShubham Bansal static u32 jit_udiv32(u32 dividend, u32 divisor)
210ddecdfceSMircea Gherzan {
211ddecdfceSMircea Gherzan 	return dividend / divisor;
212ddecdfceSMircea Gherzan }
213ddecdfceSMircea Gherzan 
21439c13c20SShubham Bansal static u32 jit_mod32(u32 dividend, u32 divisor)
2154560cdffSNicolas Schichan {
2164560cdffSNicolas Schichan 	return dividend % divisor;
2174560cdffSNicolas Schichan }
2184560cdffSNicolas Schichan 
219ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
220ddecdfceSMircea Gherzan {
2213460743eSBen Dooks 	inst |= (cond << 28);
2223460743eSBen Dooks 	inst = __opcode_to_mem_arm(inst);
2233460743eSBen Dooks 
224ddecdfceSMircea Gherzan 	if (ctx->target != NULL)
2253460743eSBen Dooks 		ctx->target[ctx->idx] = inst;
226ddecdfceSMircea Gherzan 
227ddecdfceSMircea Gherzan 	ctx->idx++;
228ddecdfceSMircea Gherzan }
229ddecdfceSMircea Gherzan 
230ddecdfceSMircea Gherzan /*
231ddecdfceSMircea Gherzan  * Emit an instruction that will be executed unconditionally.
232ddecdfceSMircea Gherzan  */
233ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx)
234ddecdfceSMircea Gherzan {
235ddecdfceSMircea Gherzan 	_emit(ARM_COND_AL, inst, ctx);
236ddecdfceSMircea Gherzan }
237ddecdfceSMircea Gherzan 
23839c13c20SShubham Bansal /*
2391ca3b17bSRussell King  * This is rather horrid, but necessary to convert an integer constant
2401ca3b17bSRussell King  * to an immediate operand for the opcodes, and be able to detect at
2411ca3b17bSRussell King  * build time whether the constant can't be converted (iow, usable in
2421ca3b17bSRussell King  * BUILD_BUG_ON()).
2431ca3b17bSRussell King  */
2441ca3b17bSRussell King #define imm12val(v, s) (rol32(v, (s)) | (s) << 7)
2451ca3b17bSRussell King #define const_imm8m(x)					\
2461ca3b17bSRussell King 	({ int r;					\
2471ca3b17bSRussell King 	   u32 v = (x);					\
2481ca3b17bSRussell King 	   if (!(v & ~0x000000ff))			\
2491ca3b17bSRussell King 		r = imm12val(v, 0);			\
2501ca3b17bSRussell King 	   else if (!(v & ~0xc000003f))			\
2511ca3b17bSRussell King 		r = imm12val(v, 2);			\
2521ca3b17bSRussell King 	   else if (!(v & ~0xf000000f))			\
2531ca3b17bSRussell King 		r = imm12val(v, 4);			\
2541ca3b17bSRussell King 	   else if (!(v & ~0xfc000003))			\
2551ca3b17bSRussell King 		r = imm12val(v, 6);			\
2561ca3b17bSRussell King 	   else if (!(v & ~0xff000000))			\
2571ca3b17bSRussell King 		r = imm12val(v, 8);			\
2581ca3b17bSRussell King 	   else if (!(v & ~0x3fc00000))			\
2591ca3b17bSRussell King 		r = imm12val(v, 10);			\
2601ca3b17bSRussell King 	   else if (!(v & ~0x0ff00000))			\
2611ca3b17bSRussell King 		r = imm12val(v, 12);			\
2621ca3b17bSRussell King 	   else if (!(v & ~0x03fc0000))			\
2631ca3b17bSRussell King 		r = imm12val(v, 14);			\
2641ca3b17bSRussell King 	   else if (!(v & ~0x00ff0000))			\
2651ca3b17bSRussell King 		r = imm12val(v, 16);			\
2661ca3b17bSRussell King 	   else if (!(v & ~0x003fc000))			\
2671ca3b17bSRussell King 		r = imm12val(v, 18);			\
2681ca3b17bSRussell King 	   else if (!(v & ~0x000ff000))			\
2691ca3b17bSRussell King 		r = imm12val(v, 20);			\
2701ca3b17bSRussell King 	   else if (!(v & ~0x0003fc00))			\
2711ca3b17bSRussell King 		r = imm12val(v, 22);			\
2721ca3b17bSRussell King 	   else if (!(v & ~0x0000ff00))			\
2731ca3b17bSRussell King 		r = imm12val(v, 24);			\
2741ca3b17bSRussell King 	   else if (!(v & ~0x00003fc0))			\
2751ca3b17bSRussell King 		r = imm12val(v, 26);			\
2761ca3b17bSRussell King 	   else if (!(v & ~0x00000ff0))			\
2771ca3b17bSRussell King 		r = imm12val(v, 28);			\
2781ca3b17bSRussell King 	   else if (!(v & ~0x000003fc))			\
2791ca3b17bSRussell King 		r = imm12val(v, 30);			\
2801ca3b17bSRussell King 	   else						\
2811ca3b17bSRussell King 		r = -1;					\
2821ca3b17bSRussell King 	   r; })
2831ca3b17bSRussell King 
2841ca3b17bSRussell King /*
28539c13c20SShubham Bansal  * Checks if immediate value can be converted to imm12(12 bits) value.
28639c13c20SShubham Bansal  */
2871ca3b17bSRussell King static int imm8m(u32 x)
288ddecdfceSMircea Gherzan {
28939c13c20SShubham Bansal 	u32 rot;
290ddecdfceSMircea Gherzan 
29139c13c20SShubham Bansal 	for (rot = 0; rot < 16; rot++)
29239c13c20SShubham Bansal 		if ((x & ~ror32(0xff, 2 * rot)) == 0)
29339c13c20SShubham Bansal 			return rol32(x, 2 * rot) | (rot << 8);
29439c13c20SShubham Bansal 	return -1;
295ddecdfceSMircea Gherzan }
296ddecdfceSMircea Gherzan 
2971ca3b17bSRussell King #define imm8m(x) (__builtin_constant_p(x) ? const_imm8m(x) : imm8m(x))
2981ca3b17bSRussell King 
299a8ef95a0SRussell King static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12)
300a8ef95a0SRussell King {
301a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
302a8ef95a0SRussell King 	if (imm12 >= 0)
303a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
304a8ef95a0SRussell King 	else
305a8ef95a0SRussell King 		imm12 = -imm12;
306828e2b90SRussell King 	return op | (imm12 & ARM_INST_LDST__IMM12);
307a8ef95a0SRussell King }
308a8ef95a0SRussell King 
309a8ef95a0SRussell King static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8)
310a8ef95a0SRussell King {
311a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
312a8ef95a0SRussell King 	if (imm8 >= 0)
313a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
314a8ef95a0SRussell King 	else
315a8ef95a0SRussell King 		imm8 = -imm8;
316a8ef95a0SRussell King 	return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f);
317a8ef95a0SRussell King }
318a8ef95a0SRussell King 
319a8ef95a0SRussell King #define ARM_LDR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off)
320a8ef95a0SRussell King #define ARM_LDRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off)
3218c9602d3SRussell King #define ARM_LDRD_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_LDRD_I, rt, rn, off)
322a8ef95a0SRussell King #define ARM_LDRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off)
323a8ef95a0SRussell King 
324a8ef95a0SRussell King #define ARM_STR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off)
325a8ef95a0SRussell King #define ARM_STRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off)
3268c9602d3SRussell King #define ARM_STRD_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_STRD_I, rt, rn, off)
327a8ef95a0SRussell King #define ARM_STRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off)
328a8ef95a0SRussell King 
32939c13c20SShubham Bansal /*
33039c13c20SShubham Bansal  * Initializes the JIT space with undefined instructions.
33139c13c20SShubham Bansal  */
33255309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
33355309dd3SDaniel Borkmann {
334e8b56d55SDaniel Borkmann 	u32 *ptr;
33555309dd3SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
33655309dd3SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
337e8b56d55SDaniel Borkmann 		*ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
33855309dd3SDaniel Borkmann }
33955309dd3SDaniel Borkmann 
340d1220efdSRussell King #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
341d1220efdSRussell King /* EABI requires the stack to be aligned to 64-bit boundaries */
342d1220efdSRussell King #define STACK_ALIGNMENT	8
343d1220efdSRussell King #else
344d1220efdSRussell King /* Stack must be aligned to 32-bit boundaries */
345d1220efdSRussell King #define STACK_ALIGNMENT	4
346d1220efdSRussell King #endif
347ddecdfceSMircea Gherzan 
34839c13c20SShubham Bansal /* total stack size used in JITed code */
34938ca9306SDaniel Borkmann #define _STACK_SIZE	(ctx->prog->aux->stack_depth + SCRATCH_SIZE)
350d1220efdSRussell King #define STACK_SIZE	ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
351ddecdfceSMircea Gherzan 
352ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
353ddecdfceSMircea Gherzan 
354ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx)
355ddecdfceSMircea Gherzan {
35639c13c20SShubham Bansal 	unsigned int i = 0, offset;
357ddecdfceSMircea Gherzan 	u16 imm;
358ddecdfceSMircea Gherzan 
359ddecdfceSMircea Gherzan 	/* on the "fake" run we just count them (duplicates included) */
360ddecdfceSMircea Gherzan 	if (ctx->target == NULL) {
361ddecdfceSMircea Gherzan 		ctx->imm_count++;
362ddecdfceSMircea Gherzan 		return 0;
363ddecdfceSMircea Gherzan 	}
364ddecdfceSMircea Gherzan 
365ddecdfceSMircea Gherzan 	while ((i < ctx->imm_count) && ctx->imms[i]) {
366ddecdfceSMircea Gherzan 		if (ctx->imms[i] == k)
367ddecdfceSMircea Gherzan 			break;
368ddecdfceSMircea Gherzan 		i++;
369ddecdfceSMircea Gherzan 	}
370ddecdfceSMircea Gherzan 
371ddecdfceSMircea Gherzan 	if (ctx->imms[i] == 0)
372ddecdfceSMircea Gherzan 		ctx->imms[i] = k;
373ddecdfceSMircea Gherzan 
374ddecdfceSMircea Gherzan 	/* constants go just after the epilogue */
37539c13c20SShubham Bansal 	offset =  ctx->offsets[ctx->prog->len - 1] * 4;
376ddecdfceSMircea Gherzan 	offset += ctx->prologue_bytes;
377ddecdfceSMircea Gherzan 	offset += ctx->epilogue_bytes;
378ddecdfceSMircea Gherzan 	offset += i * 4;
379ddecdfceSMircea Gherzan 
380ddecdfceSMircea Gherzan 	ctx->target[offset / 4] = k;
381ddecdfceSMircea Gherzan 
382ddecdfceSMircea Gherzan 	/* PC in ARM mode == address of the instruction + 8 */
383ddecdfceSMircea Gherzan 	imm = offset - (8 + ctx->idx * 4);
384ddecdfceSMircea Gherzan 
3850b59d880SNicolas Schichan 	if (imm & ~0xfff) {
3860b59d880SNicolas Schichan 		/*
3870b59d880SNicolas Schichan 		 * literal pool is too far, signal it into flags. we
3880b59d880SNicolas Schichan 		 * can only detect it on the second pass unfortunately.
3890b59d880SNicolas Schichan 		 */
3900b59d880SNicolas Schichan 		ctx->flags |= FLAG_IMM_OVERFLOW;
3910b59d880SNicolas Schichan 		return 0;
3920b59d880SNicolas Schichan 	}
3930b59d880SNicolas Schichan 
394ddecdfceSMircea Gherzan 	return imm;
395ddecdfceSMircea Gherzan }
396ddecdfceSMircea Gherzan 
397ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */
398ddecdfceSMircea Gherzan 
39939c13c20SShubham Bansal static inline int bpf2a32_offset(int bpf_to, int bpf_from,
40039c13c20SShubham Bansal 				 const struct jit_ctx *ctx) {
40139c13c20SShubham Bansal 	int to, from;
40239c13c20SShubham Bansal 
40339c13c20SShubham Bansal 	if (ctx->target == NULL)
40439c13c20SShubham Bansal 		return 0;
40539c13c20SShubham Bansal 	to = ctx->offsets[bpf_to];
40639c13c20SShubham Bansal 	from = ctx->offsets[bpf_from];
40739c13c20SShubham Bansal 
40839c13c20SShubham Bansal 	return to - from - 1;
40939c13c20SShubham Bansal }
41039c13c20SShubham Bansal 
411ddecdfceSMircea Gherzan /*
412ddecdfceSMircea Gherzan  * Move an immediate that's not an imm8m to a core register.
413ddecdfceSMircea Gherzan  */
41439c13c20SShubham Bansal static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
415ddecdfceSMircea Gherzan {
416ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
417ddecdfceSMircea Gherzan 	emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
418ddecdfceSMircea Gherzan #else
419ddecdfceSMircea Gherzan 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
420ddecdfceSMircea Gherzan 	if (val > 0xffff)
421ddecdfceSMircea Gherzan 		emit(ARM_MOVT(rd, val >> 16), ctx);
422ddecdfceSMircea Gherzan #endif
423ddecdfceSMircea Gherzan }
424ddecdfceSMircea Gherzan 
42539c13c20SShubham Bansal static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
426ddecdfceSMircea Gherzan {
427ddecdfceSMircea Gherzan 	int imm12 = imm8m(val);
428ddecdfceSMircea Gherzan 
429ddecdfceSMircea Gherzan 	if (imm12 >= 0)
430ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(rd, imm12), ctx);
431ddecdfceSMircea Gherzan 	else
432ddecdfceSMircea Gherzan 		emit_mov_i_no8m(rd, val, ctx);
433ddecdfceSMircea Gherzan }
434ddecdfceSMircea Gherzan 
435e9062481SRussell King static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
436ddecdfceSMircea Gherzan {
437ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_THUMB)
438ddecdfceSMircea Gherzan 		emit(ARM_BX(tgt_reg), ctx);
439ddecdfceSMircea Gherzan 	else
440ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
441e9062481SRussell King }
442e9062481SRussell King 
443ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
444ddecdfceSMircea Gherzan {
445ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5
446ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
447e9062481SRussell King 	emit_bx_r(tgt_reg, ctx);
448ddecdfceSMircea Gherzan #else
449ddecdfceSMircea Gherzan 	emit(ARM_BLX_R(tgt_reg), ctx);
450ddecdfceSMircea Gherzan #endif
451ddecdfceSMircea Gherzan }
452ddecdfceSMircea Gherzan 
45339c13c20SShubham Bansal static inline int epilogue_offset(const struct jit_ctx *ctx)
454ddecdfceSMircea Gherzan {
45539c13c20SShubham Bansal 	int to, from;
45639c13c20SShubham Bansal 	/* No need for 1st dummy run */
45739c13c20SShubham Bansal 	if (ctx->target == NULL)
45839c13c20SShubham Bansal 		return 0;
45939c13c20SShubham Bansal 	to = ctx->epilogue_offset;
46039c13c20SShubham Bansal 	from = ctx->idx;
46139c13c20SShubham Bansal 
46239c13c20SShubham Bansal 	return to - from - 2;
46339c13c20SShubham Bansal }
46439c13c20SShubham Bansal 
46539c13c20SShubham Bansal static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op)
46639c13c20SShubham Bansal {
4671c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
46839c13c20SShubham Bansal 
469ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7
470ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_IDIVA) {
47139c13c20SShubham Bansal 		if (op == BPF_DIV)
472ddecdfceSMircea Gherzan 			emit(ARM_UDIV(rd, rm, rn), ctx);
4734560cdffSNicolas Schichan 		else {
47439c13c20SShubham Bansal 			emit(ARM_UDIV(ARM_IP, rm, rn), ctx);
47539c13c20SShubham Bansal 			emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
4764560cdffSNicolas Schichan 		}
477ddecdfceSMircea Gherzan 		return;
478ddecdfceSMircea Gherzan 	}
479ddecdfceSMircea Gherzan #endif
48019fc99d0SNicolas Schichan 
48119fc99d0SNicolas Schichan 	/*
48239c13c20SShubham Bansal 	 * For BPF_ALU | BPF_DIV | BPF_K instructions
48339c13c20SShubham Bansal 	 * As ARM_R1 and ARM_R0 contains 1st argument of bpf
48439c13c20SShubham Bansal 	 * function, we need to save it on caller side to save
48539c13c20SShubham Bansal 	 * it from getting destroyed within callee.
48639c13c20SShubham Bansal 	 * After the return from the callee, we restore ARM_R0
48739c13c20SShubham Bansal 	 * ARM_R1.
48819fc99d0SNicolas Schichan 	 */
48939c13c20SShubham Bansal 	if (rn != ARM_R1) {
49039c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
491ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R1, rn), ctx);
49239c13c20SShubham Bansal 	}
49339c13c20SShubham Bansal 	if (rm != ARM_R0) {
49439c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
49519fc99d0SNicolas Schichan 		emit(ARM_MOV_R(ARM_R0, rm), ctx);
49639c13c20SShubham Bansal 	}
497ddecdfceSMircea Gherzan 
49839c13c20SShubham Bansal 	/* Call appropriate function */
49939c13c20SShubham Bansal 	emit_mov_i(ARM_IP, op == BPF_DIV ?
50039c13c20SShubham Bansal 		   (u32)jit_udiv32 : (u32)jit_mod32, ctx);
50139c13c20SShubham Bansal 	emit_blx_r(ARM_IP, ctx);
502ddecdfceSMircea Gherzan 
50339c13c20SShubham Bansal 	/* Save return value */
504ddecdfceSMircea Gherzan 	if (rd != ARM_R0)
505ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(rd, ARM_R0), ctx);
50639c13c20SShubham Bansal 
50739c13c20SShubham Bansal 	/* Restore ARM_R0 and ARM_R1 */
50839c13c20SShubham Bansal 	if (rn != ARM_R1)
50939c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
51039c13c20SShubham Bansal 	if (rm != ARM_R0)
51139c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
512ddecdfceSMircea Gherzan }
513ddecdfceSMircea Gherzan 
51447b9c3bfSRussell King /* Is the translated BPF register on stack? */
51547b9c3bfSRussell King static bool is_stacked(s8 reg)
516ddecdfceSMircea Gherzan {
51747b9c3bfSRussell King 	return reg < 0;
518ddecdfceSMircea Gherzan }
519ddecdfceSMircea Gherzan 
5207a987025SRussell King /* If a BPF register is on the stack (stk is true), load it to the
5217a987025SRussell King  * supplied temporary register and return the temporary register
5227a987025SRussell King  * for subsequent operations, otherwise just use the CPU register.
5237a987025SRussell King  */
5247a987025SRussell King static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx)
5257a987025SRussell King {
5267a987025SRussell King 	if (is_stacked(reg)) {
52796cced4eSRussell King 		emit(ARM_LDR_I(tmp, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
5287a987025SRussell King 		reg = tmp;
5297a987025SRussell King 	}
5307a987025SRussell King 	return reg;
5317a987025SRussell King }
5327a987025SRussell King 
533a6eccac5SRussell King static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp,
534a6eccac5SRussell King 				   struct jit_ctx *ctx)
535a6eccac5SRussell King {
536a6eccac5SRussell King 	if (is_stacked(reg[1])) {
5378c9602d3SRussell King 		if (__LINUX_ARM_ARCH__ >= 6 ||
5388c9602d3SRussell King 		    ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
5398c9602d3SRussell King 			emit(ARM_LDRD_I(tmp[1], ARM_FP,
5408c9602d3SRussell King 					EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5418c9602d3SRussell King 		} else {
5428c9602d3SRussell King 			emit(ARM_LDR_I(tmp[1], ARM_FP,
5438c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5448c9602d3SRussell King 			emit(ARM_LDR_I(tmp[0], ARM_FP,
5458c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
5468c9602d3SRussell King 		}
547a6eccac5SRussell King 		reg = tmp;
548a6eccac5SRussell King 	}
549a6eccac5SRussell King 	return reg;
550a6eccac5SRussell King }
551a6eccac5SRussell King 
5527a987025SRussell King /* If a BPF register is on the stack (stk is true), save the register
5537a987025SRussell King  * back to the stack.  If the source register is not the same, then
5547a987025SRussell King  * move it into the correct register.
5557a987025SRussell King  */
5567a987025SRussell King static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx)
5577a987025SRussell King {
5587a987025SRussell King 	if (is_stacked(reg))
55996cced4eSRussell King 		emit(ARM_STR_I(src, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
5607a987025SRussell King 	else if (reg != src)
5617a987025SRussell King 		emit(ARM_MOV_R(reg, src), ctx);
5627a987025SRussell King }
5637a987025SRussell King 
564a6eccac5SRussell King static void arm_bpf_put_reg64(const s8 *reg, const s8 *src,
565a6eccac5SRussell King 			      struct jit_ctx *ctx)
566a6eccac5SRussell King {
567a6eccac5SRussell King 	if (is_stacked(reg[1])) {
5688c9602d3SRussell King 		if (__LINUX_ARM_ARCH__ >= 6 ||
5698c9602d3SRussell King 		    ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
5708c9602d3SRussell King 			emit(ARM_STRD_I(src[1], ARM_FP,
5718c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5728c9602d3SRussell King 		} else {
5738c9602d3SRussell King 			emit(ARM_STR_I(src[1], ARM_FP,
5748c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5758c9602d3SRussell King 			emit(ARM_STR_I(src[0], ARM_FP,
5768c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
5778c9602d3SRussell King 		}
578a6eccac5SRussell King 	} else {
579a6eccac5SRussell King 		if (reg[1] != src[1])
580a6eccac5SRussell King 			emit(ARM_MOV_R(reg[1], src[1]), ctx);
581a6eccac5SRussell King 		if (reg[0] != src[0])
582a6eccac5SRussell King 			emit(ARM_MOV_R(reg[0], src[0]), ctx);
583a6eccac5SRussell King 	}
584a6eccac5SRussell King }
585a6eccac5SRussell King 
5861c35ba12SRussell King static inline void emit_a32_mov_i(const s8 dst, const u32 val,
58747b9c3bfSRussell King 				  struct jit_ctx *ctx)
588ddecdfceSMircea Gherzan {
5891c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
590ddecdfceSMircea Gherzan 
59147b9c3bfSRussell King 	if (is_stacked(dst)) {
59239c13c20SShubham Bansal 		emit_mov_i(tmp[1], val, ctx);
5937a987025SRussell King 		arm_bpf_put_reg32(dst, tmp[1], ctx);
59439c13c20SShubham Bansal 	} else {
59539c13c20SShubham Bansal 		emit_mov_i(dst, val, ctx);
59639c13c20SShubham Bansal 	}
59739c13c20SShubham Bansal }
59834805931SDaniel Borkmann 
599f9ff5018SRussell King static void emit_a32_mov_i64(const s8 dst[], u64 val, struct jit_ctx *ctx)
600f9ff5018SRussell King {
601f9ff5018SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
602f9ff5018SRussell King 	const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
603f9ff5018SRussell King 
604f9ff5018SRussell King 	emit_mov_i(rd[1], (u32)val, ctx);
605f9ff5018SRussell King 	emit_mov_i(rd[0], val >> 32, ctx);
606f9ff5018SRussell King 
607f9ff5018SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
608f9ff5018SRussell King }
609f9ff5018SRussell King 
61039c13c20SShubham Bansal /* Sign extended move */
611f9ff5018SRussell King static inline void emit_a32_mov_se_i64(const bool is64, const s8 dst[],
61247b9c3bfSRussell King 				       const u32 val, struct jit_ctx *ctx) {
613077513b8SRussell King 	u64 val64 = val;
614ddecdfceSMircea Gherzan 
61539c13c20SShubham Bansal 	if (is64 && (val & (1<<31)))
616077513b8SRussell King 		val64 |= 0xffffffff00000000ULL;
617077513b8SRussell King 	emit_a32_mov_i64(dst, val64, ctx);
61839c13c20SShubham Bansal }
61939c13c20SShubham Bansal 
62039c13c20SShubham Bansal static inline void emit_a32_add_r(const u8 dst, const u8 src,
62139c13c20SShubham Bansal 			      const bool is64, const bool hi,
62239c13c20SShubham Bansal 			      struct jit_ctx *ctx) {
62339c13c20SShubham Bansal 	/* 64 bit :
62439c13c20SShubham Bansal 	 *	adds dst_lo, dst_lo, src_lo
62539c13c20SShubham Bansal 	 *	adc dst_hi, dst_hi, src_hi
62639c13c20SShubham Bansal 	 * 32 bit :
62739c13c20SShubham Bansal 	 *	add dst_lo, dst_lo, src_lo
62839c13c20SShubham Bansal 	 */
62939c13c20SShubham Bansal 	if (!hi && is64)
63039c13c20SShubham Bansal 		emit(ARM_ADDS_R(dst, dst, src), ctx);
63139c13c20SShubham Bansal 	else if (hi && is64)
63239c13c20SShubham Bansal 		emit(ARM_ADC_R(dst, dst, src), ctx);
63339c13c20SShubham Bansal 	else
63439c13c20SShubham Bansal 		emit(ARM_ADD_R(dst, dst, src), ctx);
63539c13c20SShubham Bansal }
63639c13c20SShubham Bansal 
63739c13c20SShubham Bansal static inline void emit_a32_sub_r(const u8 dst, const u8 src,
63839c13c20SShubham Bansal 				  const bool is64, const bool hi,
63939c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
64039c13c20SShubham Bansal 	/* 64 bit :
64139c13c20SShubham Bansal 	 *	subs dst_lo, dst_lo, src_lo
64239c13c20SShubham Bansal 	 *	sbc dst_hi, dst_hi, src_hi
64339c13c20SShubham Bansal 	 * 32 bit :
64439c13c20SShubham Bansal 	 *	sub dst_lo, dst_lo, src_lo
64539c13c20SShubham Bansal 	 */
64639c13c20SShubham Bansal 	if (!hi && is64)
64739c13c20SShubham Bansal 		emit(ARM_SUBS_R(dst, dst, src), ctx);
64839c13c20SShubham Bansal 	else if (hi && is64)
64939c13c20SShubham Bansal 		emit(ARM_SBC_R(dst, dst, src), ctx);
65039c13c20SShubham Bansal 	else
65139c13c20SShubham Bansal 		emit(ARM_SUB_R(dst, dst, src), ctx);
65239c13c20SShubham Bansal }
65339c13c20SShubham Bansal 
65439c13c20SShubham Bansal static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
65539c13c20SShubham Bansal 			      const bool hi, const u8 op, struct jit_ctx *ctx){
65639c13c20SShubham Bansal 	switch (BPF_OP(op)) {
65739c13c20SShubham Bansal 	/* dst = dst + src */
65839c13c20SShubham Bansal 	case BPF_ADD:
65939c13c20SShubham Bansal 		emit_a32_add_r(dst, src, is64, hi, ctx);
66039c13c20SShubham Bansal 		break;
66139c13c20SShubham Bansal 	/* dst = dst - src */
66239c13c20SShubham Bansal 	case BPF_SUB:
66339c13c20SShubham Bansal 		emit_a32_sub_r(dst, src, is64, hi, ctx);
66439c13c20SShubham Bansal 		break;
66539c13c20SShubham Bansal 	/* dst = dst | src */
66639c13c20SShubham Bansal 	case BPF_OR:
66739c13c20SShubham Bansal 		emit(ARM_ORR_R(dst, dst, src), ctx);
66839c13c20SShubham Bansal 		break;
66939c13c20SShubham Bansal 	/* dst = dst & src */
67039c13c20SShubham Bansal 	case BPF_AND:
67139c13c20SShubham Bansal 		emit(ARM_AND_R(dst, dst, src), ctx);
67239c13c20SShubham Bansal 		break;
67339c13c20SShubham Bansal 	/* dst = dst ^ src */
67439c13c20SShubham Bansal 	case BPF_XOR:
67539c13c20SShubham Bansal 		emit(ARM_EOR_R(dst, dst, src), ctx);
67639c13c20SShubham Bansal 		break;
67739c13c20SShubham Bansal 	/* dst = dst * src */
67839c13c20SShubham Bansal 	case BPF_MUL:
67939c13c20SShubham Bansal 		emit(ARM_MUL(dst, dst, src), ctx);
68039c13c20SShubham Bansal 		break;
68139c13c20SShubham Bansal 	/* dst = dst << src */
68239c13c20SShubham Bansal 	case BPF_LSH:
68339c13c20SShubham Bansal 		emit(ARM_LSL_R(dst, dst, src), ctx);
68439c13c20SShubham Bansal 		break;
68539c13c20SShubham Bansal 	/* dst = dst >> src */
68639c13c20SShubham Bansal 	case BPF_RSH:
68739c13c20SShubham Bansal 		emit(ARM_LSR_R(dst, dst, src), ctx);
68839c13c20SShubham Bansal 		break;
68939c13c20SShubham Bansal 	/* dst = dst >> src (signed)*/
69039c13c20SShubham Bansal 	case BPF_ARSH:
69139c13c20SShubham Bansal 		emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
69239c13c20SShubham Bansal 		break;
69339c13c20SShubham Bansal 	}
69439c13c20SShubham Bansal }
69539c13c20SShubham Bansal 
69639c13c20SShubham Bansal /* ALU operation (32 bit)
69739c13c20SShubham Bansal  * dst = dst (op) src
69839c13c20SShubham Bansal  */
6991c35ba12SRussell King static inline void emit_a32_alu_r(const s8 dst, const s8 src,
70039c13c20SShubham Bansal 				  struct jit_ctx *ctx, const bool is64,
70139c13c20SShubham Bansal 				  const bool hi, const u8 op) {
7021c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7037a987025SRussell King 	s8 rn, rd;
70439c13c20SShubham Bansal 
7057a987025SRussell King 	rn = arm_bpf_get_reg32(src, tmp[1], ctx);
7067a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
70739c13c20SShubham Bansal 	/* ALU operation */
7087a987025SRussell King 	emit_alu_r(rd, rn, is64, hi, op, ctx);
7097a987025SRussell King 	arm_bpf_put_reg32(dst, rd, ctx);
71039c13c20SShubham Bansal }
71139c13c20SShubham Bansal 
71239c13c20SShubham Bansal /* ALU operation (64 bit) */
7131c35ba12SRussell King static inline void emit_a32_alu_r64(const bool is64, const s8 dst[],
71447b9c3bfSRussell King 				  const s8 src[], struct jit_ctx *ctx,
71539c13c20SShubham Bansal 				  const u8 op) {
716b18bea2aSRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
717b18bea2aSRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
718b18bea2aSRussell King 	const s8 *rd;
719b18bea2aSRussell King 
720b18bea2aSRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
721b18bea2aSRussell King 	if (is64) {
722b18bea2aSRussell King 		const s8 *rs;
723b18bea2aSRussell King 
724b18bea2aSRussell King 		rs = arm_bpf_get_reg64(src, tmp2, ctx);
725b18bea2aSRussell King 
726b18bea2aSRussell King 		/* ALU operation */
727b18bea2aSRussell King 		emit_alu_r(rd[1], rs[1], true, false, op, ctx);
728b18bea2aSRussell King 		emit_alu_r(rd[0], rs[0], true, true, op, ctx);
729b18bea2aSRussell King 	} else {
730b18bea2aSRussell King 		s8 rs;
731b18bea2aSRussell King 
732b18bea2aSRussell King 		rs = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
733b18bea2aSRussell King 
734b18bea2aSRussell King 		/* ALU operation */
735b18bea2aSRussell King 		emit_alu_r(rd[1], rs, true, false, op, ctx);
736163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
737b18bea2aSRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
738b18bea2aSRussell King 	}
739b18bea2aSRussell King 
740b18bea2aSRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
74139c13c20SShubham Bansal }
74239c13c20SShubham Bansal 
7437a987025SRussell King /* dst = src (4 bytes)*/
7441c35ba12SRussell King static inline void emit_a32_mov_r(const s8 dst, const s8 src,
74539c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
7461c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7477a987025SRussell King 	s8 rt;
74839c13c20SShubham Bansal 
7497a987025SRussell King 	rt = arm_bpf_get_reg32(src, tmp[0], ctx);
7507a987025SRussell King 	arm_bpf_put_reg32(dst, rt, ctx);
75139c13c20SShubham Bansal }
75239c13c20SShubham Bansal 
75339c13c20SShubham Bansal /* dst = src */
7541c35ba12SRussell King static inline void emit_a32_mov_r64(const bool is64, const s8 dst[],
75547b9c3bfSRussell King 				  const s8 src[],
75647b9c3bfSRussell King 				  struct jit_ctx *ctx) {
7578c9602d3SRussell King 	if (!is64) {
75847b9c3bfSRussell King 		emit_a32_mov_r(dst_lo, src_lo, ctx);
759163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
76039c13c20SShubham Bansal 			/* Zero out high 4 bytes */
76147b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
7628c9602d3SRussell King 	} else if (__LINUX_ARM_ARCH__ < 6 &&
7638c9602d3SRussell King 		   ctx->cpu_architecture < CPU_ARCH_ARMv5TE) {
7648c9602d3SRussell King 		/* complete 8 byte move */
7658c9602d3SRussell King 		emit_a32_mov_r(dst_lo, src_lo, ctx);
7668c9602d3SRussell King 		emit_a32_mov_r(dst_hi, src_hi, ctx);
7678c9602d3SRussell King 	} else if (is_stacked(src_lo) && is_stacked(dst_lo)) {
7688c9602d3SRussell King 		const u8 *tmp = bpf2a32[TMP_REG_1];
7698c9602d3SRussell King 
7708c9602d3SRussell King 		emit(ARM_LDRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
7718c9602d3SRussell King 		emit(ARM_STRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
7728c9602d3SRussell King 	} else if (is_stacked(src_lo)) {
7738c9602d3SRussell King 		emit(ARM_LDRD_I(dst[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
7748c9602d3SRussell King 	} else if (is_stacked(dst_lo)) {
7758c9602d3SRussell King 		emit(ARM_STRD_I(src[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
7768c9602d3SRussell King 	} else {
7778c9602d3SRussell King 		emit(ARM_MOV_R(dst[0], src[0]), ctx);
7788c9602d3SRussell King 		emit(ARM_MOV_R(dst[1], src[1]), ctx);
77939c13c20SShubham Bansal 	}
78039c13c20SShubham Bansal }
78139c13c20SShubham Bansal 
78239c13c20SShubham Bansal /* Shift operations */
78347b9c3bfSRussell King static inline void emit_a32_alu_i(const s8 dst, const u32 val,
78439c13c20SShubham Bansal 				struct jit_ctx *ctx, const u8 op) {
7851c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7867a987025SRussell King 	s8 rd;
78739c13c20SShubham Bansal 
7887a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
78939c13c20SShubham Bansal 
79039c13c20SShubham Bansal 	/* Do shift operation */
79139c13c20SShubham Bansal 	switch (op) {
79239c13c20SShubham Bansal 	case BPF_LSH:
79339c13c20SShubham Bansal 		emit(ARM_LSL_I(rd, rd, val), ctx);
79439c13c20SShubham Bansal 		break;
79539c13c20SShubham Bansal 	case BPF_RSH:
79639c13c20SShubham Bansal 		emit(ARM_LSR_I(rd, rd, val), ctx);
79739c13c20SShubham Bansal 		break;
798c648c9c7SLuke Nelson 	case BPF_ARSH:
799c648c9c7SLuke Nelson 		emit(ARM_ASR_I(rd, rd, val), ctx);
800c648c9c7SLuke Nelson 		break;
80139c13c20SShubham Bansal 	case BPF_NEG:
80239c13c20SShubham Bansal 		emit(ARM_RSB_I(rd, rd, val), ctx);
80339c13c20SShubham Bansal 		break;
80439c13c20SShubham Bansal 	}
80539c13c20SShubham Bansal 
8067a987025SRussell King 	arm_bpf_put_reg32(dst, rd, ctx);
80739c13c20SShubham Bansal }
80839c13c20SShubham Bansal 
80939c13c20SShubham Bansal /* dst = ~dst (64 bit) */
81047b9c3bfSRussell King static inline void emit_a32_neg64(const s8 dst[],
81139c13c20SShubham Bansal 				struct jit_ctx *ctx){
8121c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
813a6eccac5SRussell King 	const s8 *rd;
81439c13c20SShubham Bansal 
81539c13c20SShubham Bansal 	/* Setup Operand */
816a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
81739c13c20SShubham Bansal 
81839c13c20SShubham Bansal 	/* Do Negate Operation */
819a6eccac5SRussell King 	emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx);
820a6eccac5SRussell King 	emit(ARM_RSC_I(rd[0], rd[0], 0), ctx);
82139c13c20SShubham Bansal 
822a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
82339c13c20SShubham Bansal }
82439c13c20SShubham Bansal 
82539c13c20SShubham Bansal /* dst = dst << src */
82647b9c3bfSRussell King static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[],
82747b9c3bfSRussell King 				    struct jit_ctx *ctx) {
8281c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8291c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
830a6eccac5SRussell King 	const s8 *rd;
831a6eccac5SRussell King 	s8 rt;
83239c13c20SShubham Bansal 
83339c13c20SShubham Bansal 	/* Setup Operands */
8347a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
835a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
83639c13c20SShubham Bansal 
83739c13c20SShubham Bansal 	/* Do LSH operation */
83839c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
83939c13c20SShubham Bansal 	emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
840a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx);
841a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx);
842a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx);
843a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx);
84439c13c20SShubham Bansal 
8457a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
8467a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
84739c13c20SShubham Bansal }
84839c13c20SShubham Bansal 
84939c13c20SShubham Bansal /* dst = dst >> src (signed)*/
85047b9c3bfSRussell King static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[],
85147b9c3bfSRussell King 				     struct jit_ctx *ctx) {
8521c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8531c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
854a6eccac5SRussell King 	const s8 *rd;
855a6eccac5SRussell King 	s8 rt;
85639c13c20SShubham Bansal 
8577a987025SRussell King 	/* Setup Operands */
8587a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
859a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
86039c13c20SShubham Bansal 
86139c13c20SShubham Bansal 	/* Do the ARSH operation */
86239c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
86339c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
864a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
865a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
866cf48db69SLuke Nelson 	_emit(ARM_COND_PL,
867cf48db69SLuke Nelson 	      ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
868a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx);
8697a987025SRussell King 
8707a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
8717a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
87239c13c20SShubham Bansal }
87339c13c20SShubham Bansal 
87439c13c20SShubham Bansal /* dst = dst >> src */
87547b9c3bfSRussell King static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[],
87647b9c3bfSRussell King 				    struct jit_ctx *ctx) {
8771c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8781c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
879a6eccac5SRussell King 	const s8 *rd;
880a6eccac5SRussell King 	s8 rt;
88139c13c20SShubham Bansal 
8827a987025SRussell King 	/* Setup Operands */
8837a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
884a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
88539c13c20SShubham Bansal 
88668565a1aSWang YanQing 	/* Do RSH operation */
88739c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
88839c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
889a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
890a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
891a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx);
892a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx);
8937a987025SRussell King 
8947a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
8957a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
89639c13c20SShubham Bansal }
89739c13c20SShubham Bansal 
89839c13c20SShubham Bansal /* dst = dst << val */
89947b9c3bfSRussell King static inline void emit_a32_lsh_i64(const s8 dst[],
90039c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx){
9011c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9021c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
903a6eccac5SRussell King 	const s8 *rd;
90439c13c20SShubham Bansal 
9057a987025SRussell King 	/* Setup operands */
906a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
90739c13c20SShubham Bansal 
90839c13c20SShubham Bansal 	/* Do LSH operation */
90939c13c20SShubham Bansal 	if (val < 32) {
910a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx);
911a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx);
912a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx);
91339c13c20SShubham Bansal 	} else {
91439c13c20SShubham Bansal 		if (val == 32)
915a6eccac5SRussell King 			emit(ARM_MOV_R(rd[0], rd[1]), ctx);
91639c13c20SShubham Bansal 		else
917a6eccac5SRussell King 			emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx);
918a6eccac5SRussell King 		emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx);
91939c13c20SShubham Bansal 	}
92039c13c20SShubham Bansal 
921a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
92239c13c20SShubham Bansal }
92339c13c20SShubham Bansal 
92439c13c20SShubham Bansal /* dst = dst >> val */
92547b9c3bfSRussell King static inline void emit_a32_rsh_i64(const s8 dst[],
92639c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx) {
9271c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9281c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
929a6eccac5SRussell King 	const s8 *rd;
93039c13c20SShubham Bansal 
9317a987025SRussell King 	/* Setup operands */
932a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
93339c13c20SShubham Bansal 
93439c13c20SShubham Bansal 	/* Do LSR operation */
935bb9562cfSLuke Nelson 	if (val == 0) {
936bb9562cfSLuke Nelson 		/* An immediate value of 0 encodes a shift amount of 32
937bb9562cfSLuke Nelson 		 * for LSR. To shift by 0, don't do anything.
938bb9562cfSLuke Nelson 		 */
939bb9562cfSLuke Nelson 	} else if (val < 32) {
940a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
941a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
942a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx);
94339c13c20SShubham Bansal 	} else if (val == 32) {
944a6eccac5SRussell King 		emit(ARM_MOV_R(rd[1], rd[0]), ctx);
945a6eccac5SRussell King 		emit(ARM_MOV_I(rd[0], 0), ctx);
94639c13c20SShubham Bansal 	} else {
947a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx);
948a6eccac5SRussell King 		emit(ARM_MOV_I(rd[0], 0), ctx);
94939c13c20SShubham Bansal 	}
95039c13c20SShubham Bansal 
951a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
95239c13c20SShubham Bansal }
95339c13c20SShubham Bansal 
95439c13c20SShubham Bansal /* dst = dst >> val (signed) */
95547b9c3bfSRussell King static inline void emit_a32_arsh_i64(const s8 dst[],
95639c13c20SShubham Bansal 				     const u32 val, struct jit_ctx *ctx){
9571c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9581c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
959a6eccac5SRussell King 	const s8 *rd;
96039c13c20SShubham Bansal 
9617a987025SRussell King 	/* Setup operands */
962a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
96339c13c20SShubham Bansal 
96439c13c20SShubham Bansal 	/* Do ARSH operation */
965bb9562cfSLuke Nelson 	if (val == 0) {
966bb9562cfSLuke Nelson 		/* An immediate value of 0 encodes a shift amount of 32
967bb9562cfSLuke Nelson 		 * for ASR. To shift by 0, don't do anything.
968bb9562cfSLuke Nelson 		 */
969bb9562cfSLuke Nelson 	} else if (val < 32) {
970a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
971a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
972a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx);
97339c13c20SShubham Bansal 	} else if (val == 32) {
974a6eccac5SRussell King 		emit(ARM_MOV_R(rd[1], rd[0]), ctx);
975a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
97639c13c20SShubham Bansal 	} else {
977a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx);
978a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
97939c13c20SShubham Bansal 	}
98039c13c20SShubham Bansal 
981a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
98239c13c20SShubham Bansal }
98339c13c20SShubham Bansal 
98447b9c3bfSRussell King static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[],
98547b9c3bfSRussell King 				    struct jit_ctx *ctx) {
9861c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9871c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
988a6eccac5SRussell King 	const s8 *rd, *rt;
98939c13c20SShubham Bansal 
9907a987025SRussell King 	/* Setup operands for multiplication */
991a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
992a6eccac5SRussell King 	rt = arm_bpf_get_reg64(src, tmp2, ctx);
99339c13c20SShubham Bansal 
99439c13c20SShubham Bansal 	/* Do Multiplication */
995a6eccac5SRussell King 	emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx);
996a6eccac5SRussell King 	emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx);
99739c13c20SShubham Bansal 	emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
99839c13c20SShubham Bansal 
999a6eccac5SRussell King 	emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx);
1000a6eccac5SRussell King 	emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx);
10017a987025SRussell King 
10027a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_IP, ctx);
1003a6eccac5SRussell King 	arm_bpf_put_reg32(dst_hi, rd[0], ctx);
100439c13c20SShubham Bansal }
100539c13c20SShubham Bansal 
10064178417cSLuke Nelson static bool is_ldst_imm(s16 off, const u8 size)
10074178417cSLuke Nelson {
10084178417cSLuke Nelson 	s16 off_max = 0;
10094178417cSLuke Nelson 
10104178417cSLuke Nelson 	switch (size) {
10114178417cSLuke Nelson 	case BPF_B:
10124178417cSLuke Nelson 	case BPF_W:
10134178417cSLuke Nelson 		off_max = 0xfff;
10144178417cSLuke Nelson 		break;
10154178417cSLuke Nelson 	case BPF_H:
10164178417cSLuke Nelson 		off_max = 0xff;
10174178417cSLuke Nelson 		break;
10184178417cSLuke Nelson 	case BPF_DW:
10194178417cSLuke Nelson 		/* Need to make sure off+4 does not overflow. */
10204178417cSLuke Nelson 		off_max = 0xfff - 4;
10214178417cSLuke Nelson 		break;
10224178417cSLuke Nelson 	}
10234178417cSLuke Nelson 	return -off_max <= off && off <= off_max;
10244178417cSLuke Nelson }
10254178417cSLuke Nelson 
102639c13c20SShubham Bansal /* *(size *)(dst + off) = src */
1027c5eae692SRussell King static inline void emit_str_r(const s8 dst, const s8 src[],
10284178417cSLuke Nelson 			      s16 off, struct jit_ctx *ctx, const u8 sz){
10291c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
10307a987025SRussell King 	s8 rd;
103139c13c20SShubham Bansal 
10327a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[1], ctx);
1033c5eae692SRussell King 
10344178417cSLuke Nelson 	if (!is_ldst_imm(off, sz)) {
103547b9c3bfSRussell King 		emit_a32_mov_i(tmp[0], off, ctx);
1036c5eae692SRussell King 		emit(ARM_ADD_R(tmp[0], tmp[0], rd), ctx);
103739c13c20SShubham Bansal 		rd = tmp[0];
1038c5eae692SRussell King 		off = 0;
103939c13c20SShubham Bansal 	}
104039c13c20SShubham Bansal 	switch (sz) {
1041c5eae692SRussell King 	case BPF_B:
1042c5eae692SRussell King 		/* Store a Byte */
1043c5eae692SRussell King 		emit(ARM_STRB_I(src_lo, rd, off), ctx);
104439c13c20SShubham Bansal 		break;
104539c13c20SShubham Bansal 	case BPF_H:
104639c13c20SShubham Bansal 		/* Store a HalfWord */
1047c5eae692SRussell King 		emit(ARM_STRH_I(src_lo, rd, off), ctx);
104839c13c20SShubham Bansal 		break;
1049c5eae692SRussell King 	case BPF_W:
1050c5eae692SRussell King 		/* Store a Word */
1051c5eae692SRussell King 		emit(ARM_STR_I(src_lo, rd, off), ctx);
1052c5eae692SRussell King 		break;
1053c5eae692SRussell King 	case BPF_DW:
1054c5eae692SRussell King 		/* Store a Double Word */
1055c5eae692SRussell King 		emit(ARM_STR_I(src_lo, rd, off), ctx);
1056c5eae692SRussell King 		emit(ARM_STR_I(src_hi, rd, off + 4), ctx);
105739c13c20SShubham Bansal 		break;
105839c13c20SShubham Bansal 	}
105939c13c20SShubham Bansal }
106039c13c20SShubham Bansal 
106139c13c20SShubham Bansal /* dst = *(size*)(src + off) */
106247b9c3bfSRussell King static inline void emit_ldx_r(const s8 dst[], const s8 src,
10634178417cSLuke Nelson 			      s16 off, struct jit_ctx *ctx, const u8 sz){
10641c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
106547b9c3bfSRussell King 	const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
10661c35ba12SRussell King 	s8 rm = src;
106739c13c20SShubham Bansal 
10684178417cSLuke Nelson 	if (!is_ldst_imm(off, sz)) {
106947b9c3bfSRussell King 		emit_a32_mov_i(tmp[0], off, ctx);
107039c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
107139c13c20SShubham Bansal 		rm = tmp[0];
1072ec19e02bSRussell King 		off = 0;
1073ec19e02bSRussell King 	} else if (rd[1] == rm) {
1074ec19e02bSRussell King 		emit(ARM_MOV_R(tmp[0], rm), ctx);
1075ec19e02bSRussell King 		rm = tmp[0];
107639c13c20SShubham Bansal 	}
107739c13c20SShubham Bansal 	switch (sz) {
1078ec19e02bSRussell King 	case BPF_B:
1079ec19e02bSRussell King 		/* Load a Byte */
1080ec19e02bSRussell King 		emit(ARM_LDRB_I(rd[1], rm, off), ctx);
1081163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
1082a6eccac5SRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
108339c13c20SShubham Bansal 		break;
108439c13c20SShubham Bansal 	case BPF_H:
108539c13c20SShubham Bansal 		/* Load a HalfWord */
1086ec19e02bSRussell King 		emit(ARM_LDRH_I(rd[1], rm, off), ctx);
1087163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
1088a6eccac5SRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
108939c13c20SShubham Bansal 		break;
1090ec19e02bSRussell King 	case BPF_W:
1091ec19e02bSRussell King 		/* Load a Word */
1092ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
1093163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
1094a6eccac5SRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
1095ec19e02bSRussell King 		break;
1096ec19e02bSRussell King 	case BPF_DW:
1097ec19e02bSRussell King 		/* Load a Double Word */
1098ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
1099ec19e02bSRussell King 		emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
110039c13c20SShubham Bansal 		break;
110139c13c20SShubham Bansal 	}
1102a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
110339c13c20SShubham Bansal }
110439c13c20SShubham Bansal 
110539c13c20SShubham Bansal /* Arithmatic Operation */
110639c13c20SShubham Bansal static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
1107b85062acSJiong Wang 			     const u8 rn, struct jit_ctx *ctx, u8 op,
1108b85062acSJiong Wang 			     bool is_jmp64) {
110939c13c20SShubham Bansal 	switch (op) {
111039c13c20SShubham Bansal 	case BPF_JSET:
1111b85062acSJiong Wang 		if (is_jmp64) {
111239c13c20SShubham Bansal 			emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
111339c13c20SShubham Bansal 			emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
111439c13c20SShubham Bansal 			emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
1115b85062acSJiong Wang 		} else {
1116b85062acSJiong Wang 			emit(ARM_ANDS_R(ARM_IP, rt, rn), ctx);
1117b85062acSJiong Wang 		}
111839c13c20SShubham Bansal 		break;
111939c13c20SShubham Bansal 	case BPF_JEQ:
112039c13c20SShubham Bansal 	case BPF_JNE:
112139c13c20SShubham Bansal 	case BPF_JGT:
112239c13c20SShubham Bansal 	case BPF_JGE:
112339c13c20SShubham Bansal 	case BPF_JLE:
112439c13c20SShubham Bansal 	case BPF_JLT:
1125b85062acSJiong Wang 		if (is_jmp64) {
112639c13c20SShubham Bansal 			emit(ARM_CMP_R(rd, rm), ctx);
1127b85062acSJiong Wang 			/* Only compare low halve if high halve are equal. */
112839c13c20SShubham Bansal 			_emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
1129b85062acSJiong Wang 		} else {
1130b85062acSJiong Wang 			emit(ARM_CMP_R(rt, rn), ctx);
1131b85062acSJiong Wang 		}
113239c13c20SShubham Bansal 		break;
113339c13c20SShubham Bansal 	case BPF_JSLE:
113439c13c20SShubham Bansal 	case BPF_JSGT:
113539c13c20SShubham Bansal 		emit(ARM_CMP_R(rn, rt), ctx);
1136b85062acSJiong Wang 		if (is_jmp64)
113739c13c20SShubham Bansal 			emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
113839c13c20SShubham Bansal 		break;
113939c13c20SShubham Bansal 	case BPF_JSLT:
114039c13c20SShubham Bansal 	case BPF_JSGE:
114139c13c20SShubham Bansal 		emit(ARM_CMP_R(rt, rn), ctx);
1142b85062acSJiong Wang 		if (is_jmp64)
114339c13c20SShubham Bansal 			emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
114439c13c20SShubham Bansal 		break;
114539c13c20SShubham Bansal 	}
114639c13c20SShubham Bansal }
114739c13c20SShubham Bansal 
114839c13c20SShubham Bansal static int out_offset = -1; /* initialized on the first pass of build_body() */
114939c13c20SShubham Bansal static int emit_bpf_tail_call(struct jit_ctx *ctx)
115039c13c20SShubham Bansal {
115139c13c20SShubham Bansal 
115239c13c20SShubham Bansal 	/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
11531c35ba12SRussell King 	const s8 *r2 = bpf2a32[BPF_REG_2];
11541c35ba12SRussell King 	const s8 *r3 = bpf2a32[BPF_REG_3];
11551c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
11561c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
11571c35ba12SRussell King 	const s8 *tcc = bpf2a32[TCALL_CNT];
1158a6eccac5SRussell King 	const s8 *tc;
115939c13c20SShubham Bansal 	const int idx0 = ctx->idx;
116039c13c20SShubham Bansal #define cur_offset (ctx->idx - idx0)
1161f4483f2cSRussell King #define jmp_offset (out_offset - (cur_offset) - 2)
1162828e2b90SRussell King 	u32 lo, hi;
1163a6eccac5SRussell King 	s8 r_array, r_index;
1164828e2b90SRussell King 	int off;
116539c13c20SShubham Bansal 
116639c13c20SShubham Bansal 	/* if (index >= array->map.max_entries)
116739c13c20SShubham Bansal 	 *	goto out;
116839c13c20SShubham Bansal 	 */
1169828e2b90SRussell King 	BUILD_BUG_ON(offsetof(struct bpf_array, map.max_entries) >
1170828e2b90SRussell King 		     ARM_INST_LDST__IMM12);
117139c13c20SShubham Bansal 	off = offsetof(struct bpf_array, map.max_entries);
1172b5045229SRussell King 	r_array = arm_bpf_get_reg32(r2[1], tmp2[0], ctx);
1173091f0248SRussell King 	/* index is 32-bit for arrays */
11747a987025SRussell King 	r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
1175b5045229SRussell King 	/* array->map.max_entries */
1176b5045229SRussell King 	emit(ARM_LDR_I(tmp[1], r_array, off), ctx);
117739c13c20SShubham Bansal 	/* index >= array->map.max_entries */
11787a987025SRussell King 	emit(ARM_CMP_R(r_index, tmp[1]), ctx);
117939c13c20SShubham Bansal 	_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
118039c13c20SShubham Bansal 
1181b5045229SRussell King 	/* tmp2[0] = array, tmp2[1] = index */
1182aaffd2f5SRussell King 
118339c13c20SShubham Bansal 	/* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
118439c13c20SShubham Bansal 	 *	goto out;
118539c13c20SShubham Bansal 	 * tail_call_cnt++;
118639c13c20SShubham Bansal 	 */
118739c13c20SShubham Bansal 	lo = (u32)MAX_TAIL_CALL_CNT;
118839c13c20SShubham Bansal 	hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
1189a6eccac5SRussell King 	tc = arm_bpf_get_reg64(tcc, tmp, ctx);
1190a6eccac5SRussell King 	emit(ARM_CMP_I(tc[0], hi), ctx);
1191a6eccac5SRussell King 	_emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx);
119239c13c20SShubham Bansal 	_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
1193a6eccac5SRussell King 	emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx);
1194a6eccac5SRussell King 	emit(ARM_ADC_I(tc[0], tc[0], 0), ctx);
1195a6eccac5SRussell King 	arm_bpf_put_reg64(tcc, tmp, ctx);
119639c13c20SShubham Bansal 
119739c13c20SShubham Bansal 	/* prog = array->ptrs[index]
119839c13c20SShubham Bansal 	 * if (prog == NULL)
119939c13c20SShubham Bansal 	 *	goto out;
120039c13c20SShubham Bansal 	 */
1201828e2b90SRussell King 	BUILD_BUG_ON(imm8m(offsetof(struct bpf_array, ptrs)) < 0);
1202828e2b90SRussell King 	off = imm8m(offsetof(struct bpf_array, ptrs));
1203828e2b90SRussell King 	emit(ARM_ADD_I(tmp[1], r_array, off), ctx);
12042b6958efSRussell King 	emit(ARM_LDR_R_SI(tmp[1], tmp[1], r_index, SRTYPE_ASL, 2), ctx);
120539c13c20SShubham Bansal 	emit(ARM_CMP_I(tmp[1], 0), ctx);
120639c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
120739c13c20SShubham Bansal 
120839c13c20SShubham Bansal 	/* goto *(prog->bpf_func + prologue_size); */
1209828e2b90SRussell King 	BUILD_BUG_ON(offsetof(struct bpf_prog, bpf_func) >
1210828e2b90SRussell King 		     ARM_INST_LDST__IMM12);
121139c13c20SShubham Bansal 	off = offsetof(struct bpf_prog, bpf_func);
1212828e2b90SRussell King 	emit(ARM_LDR_I(tmp[1], tmp[1], off), ctx);
121339c13c20SShubham Bansal 	emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
1214e9062481SRussell King 	emit_bx_r(tmp[1], ctx);
121539c13c20SShubham Bansal 
121639c13c20SShubham Bansal 	/* out: */
121739c13c20SShubham Bansal 	if (out_offset == -1)
121839c13c20SShubham Bansal 		out_offset = cur_offset;
121939c13c20SShubham Bansal 	if (cur_offset != out_offset) {
122039c13c20SShubham Bansal 		pr_err_once("tail_call out_offset = %d, expected %d!\n",
122139c13c20SShubham Bansal 			    cur_offset, out_offset);
122239c13c20SShubham Bansal 		return -1;
122339c13c20SShubham Bansal 	}
122439c13c20SShubham Bansal 	return 0;
122539c13c20SShubham Bansal #undef cur_offset
122639c13c20SShubham Bansal #undef jmp_offset
122739c13c20SShubham Bansal }
122839c13c20SShubham Bansal 
122939c13c20SShubham Bansal /* 0xabcd => 0xcdab */
123039c13c20SShubham Bansal static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
123139c13c20SShubham Bansal {
123239c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
12331c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
123439c13c20SShubham Bansal 
123539c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
123639c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
123739c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
123839c13c20SShubham Bansal 	emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
123939c13c20SShubham Bansal #else /* ARMv6+ */
124039c13c20SShubham Bansal 	emit(ARM_REV16(rd, rn), ctx);
124139c13c20SShubham Bansal #endif
124239c13c20SShubham Bansal }
124339c13c20SShubham Bansal 
124439c13c20SShubham Bansal /* 0xabcdefgh => 0xghefcdab */
124539c13c20SShubham Bansal static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
124639c13c20SShubham Bansal {
124739c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
12481c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
124939c13c20SShubham Bansal 
125039c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
125139c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
125239c13c20SShubham Bansal 	emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
125339c13c20SShubham Bansal 
125439c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
125539c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
125639c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
125739c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
125839c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
125939c13c20SShubham Bansal 	emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
126039c13c20SShubham Bansal 	emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
126139c13c20SShubham Bansal 
126239c13c20SShubham Bansal #else /* ARMv6+ */
126339c13c20SShubham Bansal 	emit(ARM_REV(rd, rn), ctx);
126439c13c20SShubham Bansal #endif
126539c13c20SShubham Bansal }
126639c13c20SShubham Bansal 
126739c13c20SShubham Bansal // push the scratch stack register on top of the stack
126896cced4eSRussell King static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx)
126939c13c20SShubham Bansal {
12701c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
127196cced4eSRussell King 	const s8 *rt;
127239c13c20SShubham Bansal 	u16 reg_set = 0;
127339c13c20SShubham Bansal 
127496cced4eSRussell King 	rt = arm_bpf_get_reg64(src, tmp2, ctx);
127539c13c20SShubham Bansal 
127696cced4eSRussell King 	reg_set = (1 << rt[1]) | (1 << rt[0]);
127739c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
127839c13c20SShubham Bansal }
127939c13c20SShubham Bansal 
128039c13c20SShubham Bansal static void build_prologue(struct jit_ctx *ctx)
128139c13c20SShubham Bansal {
1282c4533128SRussell King 	const s8 arm_r0 = bpf2a32[BPF_REG_0][1];
1283c4533128SRussell King 	const s8 *bpf_r1 = bpf2a32[BPF_REG_1];
1284c4533128SRussell King 	const s8 *bpf_fp = bpf2a32[BPF_REG_FP];
12851c35ba12SRussell King 	const s8 *tcc = bpf2a32[TCALL_CNT];
128639c13c20SShubham Bansal 
128739c13c20SShubham Bansal 	/* Save callee saved registers. */
128839c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
128902088d9bSRussell King 	u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
129002088d9bSRussell King 	emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
129139c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
129239c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
129339c13c20SShubham Bansal #else
129402088d9bSRussell King 	emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
129502088d9bSRussell King 	emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
129639c13c20SShubham Bansal #endif
1297c4533128SRussell King 	/* mov r3, #0 */
1298c4533128SRussell King 	/* sub r2, sp, #SCRATCH_SIZE */
1299c4533128SRussell King 	emit(ARM_MOV_I(bpf_r1[0], 0), ctx);
1300c4533128SRussell King 	emit(ARM_SUB_I(bpf_r1[1], ARM_SP, SCRATCH_SIZE), ctx);
130139c13c20SShubham Bansal 
130239c13c20SShubham Bansal 	ctx->stack_size = imm8m(STACK_SIZE);
130339c13c20SShubham Bansal 
130439c13c20SShubham Bansal 	/* Set up function call stack */
130539c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
130639c13c20SShubham Bansal 
130739c13c20SShubham Bansal 	/* Set up BPF prog stack base register */
1308c4533128SRussell King 	emit_a32_mov_r64(true, bpf_fp, bpf_r1, ctx);
130939c13c20SShubham Bansal 
1310c4533128SRussell King 	/* Initialize Tail Count */
1311c4533128SRussell King 	emit(ARM_MOV_I(bpf_r1[1], 0), ctx);
1312c4533128SRussell King 	emit_a32_mov_r64(true, tcc, bpf_r1, ctx);
131339c13c20SShubham Bansal 
131439c13c20SShubham Bansal 	/* Move BPF_CTX to BPF_R1 */
1315c4533128SRussell King 	emit(ARM_MOV_R(bpf_r1[1], arm_r0), ctx);
1316c4533128SRussell King 
131739c13c20SShubham Bansal 	/* end of prologue */
131839c13c20SShubham Bansal }
131939c13c20SShubham Bansal 
132002088d9bSRussell King /* restore callee saved registers. */
132139c13c20SShubham Bansal static void build_epilogue(struct jit_ctx *ctx)
132239c13c20SShubham Bansal {
132339c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
132402088d9bSRussell King 	/* When using frame pointers, some additional registers need to
132502088d9bSRussell King 	 * be loaded. */
132602088d9bSRussell King 	u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
132702088d9bSRussell King 	emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
132839c13c20SShubham Bansal 	emit(ARM_LDM(ARM_SP, reg_set), ctx);
132939c13c20SShubham Bansal #else
133039c13c20SShubham Bansal 	/* Restore callee saved registers. */
133102088d9bSRussell King 	emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
133202088d9bSRussell King 	emit(ARM_POP(CALLEE_POP_MASK), ctx);
133339c13c20SShubham Bansal #endif
133439c13c20SShubham Bansal }
133539c13c20SShubham Bansal 
133639c13c20SShubham Bansal /*
133739c13c20SShubham Bansal  * Convert an eBPF instruction to native instruction, i.e
133839c13c20SShubham Bansal  * JITs an eBPF instruction.
133939c13c20SShubham Bansal  * Returns :
134039c13c20SShubham Bansal  *	0  - Successfully JITed an 8-byte eBPF instruction
134139c13c20SShubham Bansal  *	>0 - Successfully JITed a 16-byte eBPF instruction
134239c13c20SShubham Bansal  *	<0 - Failed to JIT.
134339c13c20SShubham Bansal  */
134439c13c20SShubham Bansal static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
134539c13c20SShubham Bansal {
134639c13c20SShubham Bansal 	const u8 code = insn->code;
13471c35ba12SRussell King 	const s8 *dst = bpf2a32[insn->dst_reg];
13481c35ba12SRussell King 	const s8 *src = bpf2a32[insn->src_reg];
13491c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
13501c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
135139c13c20SShubham Bansal 	const s16 off = insn->off;
135239c13c20SShubham Bansal 	const s32 imm = insn->imm;
135339c13c20SShubham Bansal 	const int i = insn - ctx->prog->insnsi;
135439c13c20SShubham Bansal 	const bool is64 = BPF_CLASS(code) == BPF_ALU64;
1355a6eccac5SRussell King 	const s8 *rd, *rs;
1356a6eccac5SRussell King 	s8 rd_lo, rt, rm, rn;
135739c13c20SShubham Bansal 	s32 jmp_offset;
135839c13c20SShubham Bansal 
135939c13c20SShubham Bansal #define check_imm(bits, imm) do {				\
13602b589a7eSWang YanQing 	if ((imm) >= (1 << ((bits) - 1)) ||			\
13612b589a7eSWang YanQing 	    (imm) < -(1 << ((bits) - 1))) {			\
136239c13c20SShubham Bansal 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
136339c13c20SShubham Bansal 			i, imm, imm);				\
136439c13c20SShubham Bansal 		return -EINVAL;					\
136539c13c20SShubham Bansal 	}							\
136639c13c20SShubham Bansal } while (0)
136739c13c20SShubham Bansal #define check_imm24(imm) check_imm(24, imm)
1368ddecdfceSMircea Gherzan 
136934805931SDaniel Borkmann 	switch (code) {
137039c13c20SShubham Bansal 	/* ALU operations */
1371ddecdfceSMircea Gherzan 
137239c13c20SShubham Bansal 	/* dst = src */
137339c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_K:
137439c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_X:
137539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_K:
137639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_X:
137739c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
137839c13c20SShubham Bansal 		case BPF_X:
1379163541e6SJiong Wang 			if (imm == 1) {
1380163541e6SJiong Wang 				/* Special mov32 for zext */
1381163541e6SJiong Wang 				emit_a32_mov_i(dst_hi, 0, ctx);
1382163541e6SJiong Wang 				break;
1383163541e6SJiong Wang 			}
138447b9c3bfSRussell King 			emit_a32_mov_r64(is64, dst, src, ctx);
138539c13c20SShubham Bansal 			break;
138639c13c20SShubham Bansal 		case BPF_K:
138739c13c20SShubham Bansal 			/* Sign-extend immediate value to destination reg */
1388f9ff5018SRussell King 			emit_a32_mov_se_i64(is64, dst, imm, ctx);
138939c13c20SShubham Bansal 			break;
1390ddecdfceSMircea Gherzan 		}
1391ddecdfceSMircea Gherzan 		break;
139239c13c20SShubham Bansal 	/* dst = dst + src/imm */
139339c13c20SShubham Bansal 	/* dst = dst - src/imm */
139439c13c20SShubham Bansal 	/* dst = dst | src/imm */
139539c13c20SShubham Bansal 	/* dst = dst & src/imm */
139639c13c20SShubham Bansal 	/* dst = dst ^ src/imm */
139739c13c20SShubham Bansal 	/* dst = dst * src/imm */
139839c13c20SShubham Bansal 	/* dst = dst << src */
139939c13c20SShubham Bansal 	/* dst = dst >> src */
140034805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_K:
140134805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_X:
140234805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_K:
140334805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_X:
140434805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_K:
140534805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_X:
140634805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_K:
140734805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_X:
140839c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_K:
140939c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_X:
141039c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_K:
141139c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_X:
141234805931SDaniel Borkmann 	case BPF_ALU | BPF_LSH | BPF_X:
141334805931SDaniel Borkmann 	case BPF_ALU | BPF_RSH | BPF_X:
141439c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_X:
141539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_K:
141639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_X:
141739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_K:
141839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_X:
141939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_K:
142039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_X:
142139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_K:
142239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_X:
142339c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_K:
142439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_X:
142539c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
142639c13c20SShubham Bansal 		case BPF_X:
142747b9c3bfSRussell King 			emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code));
1428ddecdfceSMircea Gherzan 			break;
142939c13c20SShubham Bansal 		case BPF_K:
143039c13c20SShubham Bansal 			/* Move immediate value to the temporary register
143139c13c20SShubham Bansal 			 * and then do the ALU operation on the temporary
143239c13c20SShubham Bansal 			 * register as this will sign-extend the immediate
143339c13c20SShubham Bansal 			 * value into temporary reg and then it would be
143439c13c20SShubham Bansal 			 * safe to do the operation on it.
143539c13c20SShubham Bansal 			 */
1436f9ff5018SRussell King 			emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
143747b9c3bfSRussell King 			emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code));
143839c13c20SShubham Bansal 			break;
143939c13c20SShubham Bansal 		}
144039c13c20SShubham Bansal 		break;
144139c13c20SShubham Bansal 	/* dst = dst / src(imm) */
144239c13c20SShubham Bansal 	/* dst = dst % src(imm) */
144339c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_K:
144439c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_X:
144539c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_K:
144639c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_X:
1447a6eccac5SRussell King 		rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx);
144839c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
144939c13c20SShubham Bansal 		case BPF_X:
14507a987025SRussell King 			rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx);
145139c13c20SShubham Bansal 			break;
145239c13c20SShubham Bansal 		case BPF_K:
145339c13c20SShubham Bansal 			rt = tmp2[0];
145447b9c3bfSRussell King 			emit_a32_mov_i(rt, imm, ctx);
145547b9c3bfSRussell King 			break;
145647b9c3bfSRussell King 		default:
145747b9c3bfSRussell King 			rt = src_lo;
145839c13c20SShubham Bansal 			break;
145939c13c20SShubham Bansal 		}
1460a6eccac5SRussell King 		emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code));
1461a6eccac5SRussell King 		arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
1462163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
146347b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
146439c13c20SShubham Bansal 		break;
146539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_K:
146639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_X:
146739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_K:
146839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_X:
146939c13c20SShubham Bansal 		goto notyet;
147039c13c20SShubham Bansal 	/* dst = dst << imm */
1471c648c9c7SLuke Nelson 	/* dst = dst >> imm */
1472c648c9c7SLuke Nelson 	/* dst = dst >> imm (signed) */
147339c13c20SShubham Bansal 	case BPF_ALU | BPF_LSH | BPF_K:
1474c648c9c7SLuke Nelson 	case BPF_ALU | BPF_RSH | BPF_K:
1475c648c9c7SLuke Nelson 	case BPF_ALU | BPF_ARSH | BPF_K:
147639c13c20SShubham Bansal 		if (unlikely(imm > 31))
147739c13c20SShubham Bansal 			return -EINVAL;
147839c13c20SShubham Bansal 		if (imm)
147947b9c3bfSRussell King 			emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
1480163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
148147b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
148239c13c20SShubham Bansal 		break;
148339c13c20SShubham Bansal 	/* dst = dst << imm */
148439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_K:
148539c13c20SShubham Bansal 		if (unlikely(imm > 63))
148639c13c20SShubham Bansal 			return -EINVAL;
148747b9c3bfSRussell King 		emit_a32_lsh_i64(dst, imm, ctx);
148839c13c20SShubham Bansal 		break;
148939c13c20SShubham Bansal 	/* dst = dst >> imm */
149039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_K:
149139c13c20SShubham Bansal 		if (unlikely(imm > 63))
149239c13c20SShubham Bansal 			return -EINVAL;
149347b9c3bfSRussell King 		emit_a32_rsh_i64(dst, imm, ctx);
149439c13c20SShubham Bansal 		break;
149539c13c20SShubham Bansal 	/* dst = dst << src */
149639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_X:
149747b9c3bfSRussell King 		emit_a32_lsh_r64(dst, src, ctx);
149839c13c20SShubham Bansal 		break;
149939c13c20SShubham Bansal 	/* dst = dst >> src */
150039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_X:
150147b9c3bfSRussell King 		emit_a32_rsh_r64(dst, src, ctx);
150239c13c20SShubham Bansal 		break;
150339c13c20SShubham Bansal 	/* dst = dst >> src (signed) */
150439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_X:
150547b9c3bfSRussell King 		emit_a32_arsh_r64(dst, src, ctx);
150639c13c20SShubham Bansal 		break;
150739c13c20SShubham Bansal 	/* dst = dst >> imm (signed) */
150839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_K:
150939c13c20SShubham Bansal 		if (unlikely(imm > 63))
151039c13c20SShubham Bansal 			return -EINVAL;
151147b9c3bfSRussell King 		emit_a32_arsh_i64(dst, imm, ctx);
151239c13c20SShubham Bansal 		break;
151339c13c20SShubham Bansal 	/* dst = ~dst */
151434805931SDaniel Borkmann 	case BPF_ALU | BPF_NEG:
151547b9c3bfSRussell King 		emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
1516163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
151747b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
1518ddecdfceSMircea Gherzan 		break;
151939c13c20SShubham Bansal 	/* dst = ~dst (64 bit) */
152039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_NEG:
152147b9c3bfSRussell King 		emit_a32_neg64(dst, ctx);
1522ddecdfceSMircea Gherzan 		break;
152339c13c20SShubham Bansal 	/* dst = dst * src/imm */
152439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_X:
152539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_K:
152639c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
152739c13c20SShubham Bansal 		case BPF_X:
152847b9c3bfSRussell King 			emit_a32_mul_r64(dst, src, ctx);
1529ddecdfceSMircea Gherzan 			break;
153039c13c20SShubham Bansal 		case BPF_K:
153139c13c20SShubham Bansal 			/* Move immediate value to the temporary register
153239c13c20SShubham Bansal 			 * and then do the multiplication on it as this
153339c13c20SShubham Bansal 			 * will sign-extend the immediate value into temp
153439c13c20SShubham Bansal 			 * reg then it would be safe to do the operation
153539c13c20SShubham Bansal 			 * on it.
15365bf705b4SNicolas Schichan 			 */
1537f9ff5018SRussell King 			emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
153847b9c3bfSRussell King 			emit_a32_mul_r64(dst, tmp2, ctx);
153939c13c20SShubham Bansal 			break;
15405bf705b4SNicolas Schichan 		}
1541ddecdfceSMircea Gherzan 		break;
154239c13c20SShubham Bansal 	/* dst = htole(dst) */
154339c13c20SShubham Bansal 	/* dst = htobe(dst) */
154439c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_LE:
154539c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_BE:
1546a6eccac5SRussell King 		rd = arm_bpf_get_reg64(dst, tmp, ctx);
154739c13c20SShubham Bansal 		if (BPF_SRC(code) == BPF_FROM_LE)
154839c13c20SShubham Bansal 			goto emit_bswap_uxt;
154939c13c20SShubham Bansal 		switch (imm) {
155039c13c20SShubham Bansal 		case 16:
1551a6eccac5SRussell King 			emit_rev16(rd[1], rd[1], ctx);
155239c13c20SShubham Bansal 			goto emit_bswap_uxt;
155339c13c20SShubham Bansal 		case 32:
1554a6eccac5SRussell King 			emit_rev32(rd[1], rd[1], ctx);
155539c13c20SShubham Bansal 			goto emit_bswap_uxt;
155639c13c20SShubham Bansal 		case 64:
1557a6eccac5SRussell King 			emit_rev32(ARM_LR, rd[1], ctx);
1558a6eccac5SRussell King 			emit_rev32(rd[1], rd[0], ctx);
1559a6eccac5SRussell King 			emit(ARM_MOV_R(rd[0], ARM_LR), ctx);
1560bf0098f2SDaniel Borkmann 			break;
156139c13c20SShubham Bansal 		}
156239c13c20SShubham Bansal 		goto exit;
156339c13c20SShubham Bansal emit_bswap_uxt:
156439c13c20SShubham Bansal 		switch (imm) {
156539c13c20SShubham Bansal 		case 16:
156639c13c20SShubham Bansal 			/* zero-extend 16 bits into 64 bits */
156739c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
156847b9c3bfSRussell King 			emit_a32_mov_i(tmp2[1], 0xffff, ctx);
1569a6eccac5SRussell King 			emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx);
157039c13c20SShubham Bansal #else /* ARMv6+ */
1571a6eccac5SRussell King 			emit(ARM_UXTH(rd[1], rd[1]), ctx);
15721447f93fSNicolas Schichan #endif
1573163541e6SJiong Wang 			if (!ctx->prog->aux->verifier_zext)
1574a6eccac5SRussell King 				emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
15751447f93fSNicolas Schichan 			break;
157639c13c20SShubham Bansal 		case 32:
157739c13c20SShubham Bansal 			/* zero-extend 32 bits into 64 bits */
1578163541e6SJiong Wang 			if (!ctx->prog->aux->verifier_zext)
1579a6eccac5SRussell King 				emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
1580ddecdfceSMircea Gherzan 			break;
158139c13c20SShubham Bansal 		case 64:
158239c13c20SShubham Bansal 			/* nop */
158339c13c20SShubham Bansal 			break;
158439c13c20SShubham Bansal 		}
158539c13c20SShubham Bansal exit:
1586a6eccac5SRussell King 		arm_bpf_put_reg64(dst, rd, ctx);
158739c13c20SShubham Bansal 		break;
158839c13c20SShubham Bansal 	/* dst = imm64 */
158939c13c20SShubham Bansal 	case BPF_LD | BPF_IMM | BPF_DW:
159039c13c20SShubham Bansal 	{
1591f9ff5018SRussell King 		u64 val = (u32)imm | (u64)insn[1].imm << 32;
1592303249abSNicolas Schichan 
1593f9ff5018SRussell King 		emit_a32_mov_i64(dst, val, ctx);
159439c13c20SShubham Bansal 
159539c13c20SShubham Bansal 		return 1;
159639c13c20SShubham Bansal 	}
159739c13c20SShubham Bansal 	/* LDX: dst = *(size *)(src + off) */
159839c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_W:
159939c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_H:
160039c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_B:
160139c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_DW:
16027a987025SRussell King 		rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
160347b9c3bfSRussell King 		emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code));
160439c13c20SShubham Bansal 		break;
160539c13c20SShubham Bansal 	/* ST: *(size *)(dst + off) = imm */
160639c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_W:
160739c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_H:
160839c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_B:
160939c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_DW:
161039c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
161139c13c20SShubham Bansal 		case BPF_DW:
161239c13c20SShubham Bansal 			/* Sign-extend immediate value into temp reg */
1613f9ff5018SRussell King 			emit_a32_mov_se_i64(true, tmp2, imm, ctx);
161439c13c20SShubham Bansal 			break;
161539c13c20SShubham Bansal 		case BPF_W:
161639c13c20SShubham Bansal 		case BPF_H:
161739c13c20SShubham Bansal 		case BPF_B:
161847b9c3bfSRussell King 			emit_a32_mov_i(tmp2[1], imm, ctx);
161939c13c20SShubham Bansal 			break;
162039c13c20SShubham Bansal 		}
1621c5eae692SRussell King 		emit_str_r(dst_lo, tmp2, off, ctx, BPF_SIZE(code));
162239c13c20SShubham Bansal 		break;
162339c13c20SShubham Bansal 	/* STX XADD: lock *(u32 *)(dst + off) += src */
162439c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_W:
162539c13c20SShubham Bansal 	/* STX XADD: lock *(u64 *)(dst + off) += src */
162639c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_DW:
162739c13c20SShubham Bansal 		goto notyet;
162839c13c20SShubham Bansal 	/* STX: *(size *)(dst + off) = src */
162939c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_W:
163039c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_H:
163139c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_B:
163239c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_DW:
1633a6eccac5SRussell King 		rs = arm_bpf_get_reg64(src, tmp2, ctx);
1634c5eae692SRussell King 		emit_str_r(dst_lo, rs, off, ctx, BPF_SIZE(code));
163539c13c20SShubham Bansal 		break;
163639c13c20SShubham Bansal 	/* PC += off if dst == src */
163739c13c20SShubham Bansal 	/* PC += off if dst > src */
163839c13c20SShubham Bansal 	/* PC += off if dst >= src */
163939c13c20SShubham Bansal 	/* PC += off if dst < src */
164039c13c20SShubham Bansal 	/* PC += off if dst <= src */
164139c13c20SShubham Bansal 	/* PC += off if dst != src */
164239c13c20SShubham Bansal 	/* PC += off if dst > src (signed) */
164339c13c20SShubham Bansal 	/* PC += off if dst >= src (signed) */
164439c13c20SShubham Bansal 	/* PC += off if dst < src (signed) */
164539c13c20SShubham Bansal 	/* PC += off if dst <= src (signed) */
164639c13c20SShubham Bansal 	/* PC += off if dst & src */
164739c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_X:
164839c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_X:
164939c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_X:
165039c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_X:
165139c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_X:
165239c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_X:
165339c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_X:
165439c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_X:
165539c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_X:
165639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_X:
165739c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_X:
1658b85062acSJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_X:
1659b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_X:
1660b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_X:
1661b85062acSJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_X:
1662b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_X:
1663b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_X:
1664b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_X:
1665b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_X:
1666b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_X:
1667b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_X:
1668b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_X:
166939c13c20SShubham Bansal 		/* Setup source registers */
16707a987025SRussell King 		rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
16717a987025SRussell King 		rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
167239c13c20SShubham Bansal 		goto go_jmp;
167339c13c20SShubham Bansal 	/* PC += off if dst == imm */
167439c13c20SShubham Bansal 	/* PC += off if dst > imm */
167539c13c20SShubham Bansal 	/* PC += off if dst >= imm */
167639c13c20SShubham Bansal 	/* PC += off if dst < imm */
167739c13c20SShubham Bansal 	/* PC += off if dst <= imm */
167839c13c20SShubham Bansal 	/* PC += off if dst != imm */
167939c13c20SShubham Bansal 	/* PC += off if dst > imm (signed) */
168039c13c20SShubham Bansal 	/* PC += off if dst >= imm (signed) */
168139c13c20SShubham Bansal 	/* PC += off if dst < imm (signed) */
168239c13c20SShubham Bansal 	/* PC += off if dst <= imm (signed) */
168339c13c20SShubham Bansal 	/* PC += off if dst & imm */
168439c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_K:
168539c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_K:
168639c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_K:
168739c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_K:
168839c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_K:
168939c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_K:
169039c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_K:
169139c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_K:
169239c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_K:
169339c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_K:
169439c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_K:
1695b85062acSJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_K:
1696b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_K:
1697b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_K:
1698b85062acSJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_K:
1699b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_K:
1700b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_K:
1701b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_K:
1702b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_K:
1703b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_K:
1704b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_K:
1705b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_K:
170639c13c20SShubham Bansal 		if (off == 0)
170739c13c20SShubham Bansal 			break;
170839c13c20SShubham Bansal 		rm = tmp2[0];
170939c13c20SShubham Bansal 		rn = tmp2[1];
171039c13c20SShubham Bansal 		/* Sign-extend immediate value */
1711f9ff5018SRussell King 		emit_a32_mov_se_i64(true, tmp2, imm, ctx);
171239c13c20SShubham Bansal go_jmp:
171339c13c20SShubham Bansal 		/* Setup destination register */
1714a6eccac5SRussell King 		rd = arm_bpf_get_reg64(dst, tmp, ctx);
171539c13c20SShubham Bansal 
171639c13c20SShubham Bansal 		/* Check for the condition */
1717b85062acSJiong Wang 		emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code),
1718b85062acSJiong Wang 			  BPF_CLASS(code) == BPF_JMP);
171939c13c20SShubham Bansal 
172039c13c20SShubham Bansal 		/* Setup JUMP instruction */
172139c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
172239c13c20SShubham Bansal 		switch (BPF_OP(code)) {
172339c13c20SShubham Bansal 		case BPF_JNE:
172439c13c20SShubham Bansal 		case BPF_JSET:
172539c13c20SShubham Bansal 			_emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
172639c13c20SShubham Bansal 			break;
172739c13c20SShubham Bansal 		case BPF_JEQ:
172839c13c20SShubham Bansal 			_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
172939c13c20SShubham Bansal 			break;
173039c13c20SShubham Bansal 		case BPF_JGT:
173139c13c20SShubham Bansal 			_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
173239c13c20SShubham Bansal 			break;
173339c13c20SShubham Bansal 		case BPF_JGE:
173439c13c20SShubham Bansal 			_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
173539c13c20SShubham Bansal 			break;
173639c13c20SShubham Bansal 		case BPF_JSGT:
173739c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
173839c13c20SShubham Bansal 			break;
173939c13c20SShubham Bansal 		case BPF_JSGE:
174039c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
174139c13c20SShubham Bansal 			break;
174239c13c20SShubham Bansal 		case BPF_JLE:
174339c13c20SShubham Bansal 			_emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
174439c13c20SShubham Bansal 			break;
174539c13c20SShubham Bansal 		case BPF_JLT:
174639c13c20SShubham Bansal 			_emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
174739c13c20SShubham Bansal 			break;
174839c13c20SShubham Bansal 		case BPF_JSLT:
174939c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
175039c13c20SShubham Bansal 			break;
175139c13c20SShubham Bansal 		case BPF_JSLE:
175239c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
175339c13c20SShubham Bansal 			break;
175439c13c20SShubham Bansal 		}
175539c13c20SShubham Bansal 		break;
175639c13c20SShubham Bansal 	/* JMP OFF */
175739c13c20SShubham Bansal 	case BPF_JMP | BPF_JA:
175839c13c20SShubham Bansal 	{
175939c13c20SShubham Bansal 		if (off == 0)
176039c13c20SShubham Bansal 			break;
176139c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
176239c13c20SShubham Bansal 		check_imm24(jmp_offset);
176339c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
176439c13c20SShubham Bansal 		break;
176539c13c20SShubham Bansal 	}
176639c13c20SShubham Bansal 	/* tail call */
176739c13c20SShubham Bansal 	case BPF_JMP | BPF_TAIL_CALL:
176839c13c20SShubham Bansal 		if (emit_bpf_tail_call(ctx))
176939c13c20SShubham Bansal 			return -EFAULT;
177039c13c20SShubham Bansal 		break;
177139c13c20SShubham Bansal 	/* function call */
177239c13c20SShubham Bansal 	case BPF_JMP | BPF_CALL:
177339c13c20SShubham Bansal 	{
17741c35ba12SRussell King 		const s8 *r0 = bpf2a32[BPF_REG_0];
17751c35ba12SRussell King 		const s8 *r1 = bpf2a32[BPF_REG_1];
17761c35ba12SRussell King 		const s8 *r2 = bpf2a32[BPF_REG_2];
17771c35ba12SRussell King 		const s8 *r3 = bpf2a32[BPF_REG_3];
17781c35ba12SRussell King 		const s8 *r4 = bpf2a32[BPF_REG_4];
17791c35ba12SRussell King 		const s8 *r5 = bpf2a32[BPF_REG_5];
178039c13c20SShubham Bansal 		const u32 func = (u32)__bpf_call_base + (u32)imm;
178139c13c20SShubham Bansal 
178247b9c3bfSRussell King 		emit_a32_mov_r64(true, r0, r1, ctx);
178347b9c3bfSRussell King 		emit_a32_mov_r64(true, r1, r2, ctx);
178496cced4eSRussell King 		emit_push_r64(r5, ctx);
178596cced4eSRussell King 		emit_push_r64(r4, ctx);
178696cced4eSRussell King 		emit_push_r64(r3, ctx);
178739c13c20SShubham Bansal 
178847b9c3bfSRussell King 		emit_a32_mov_i(tmp[1], func, ctx);
178939c13c20SShubham Bansal 		emit_blx_r(tmp[1], ctx);
179039c13c20SShubham Bansal 
179139c13c20SShubham Bansal 		emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
179239c13c20SShubham Bansal 		break;
179339c13c20SShubham Bansal 	}
179439c13c20SShubham Bansal 	/* function return */
179539c13c20SShubham Bansal 	case BPF_JMP | BPF_EXIT:
179639c13c20SShubham Bansal 		/* Optimization: when last instruction is EXIT
179739c13c20SShubham Bansal 		 * simply fallthrough to epilogue.
179839c13c20SShubham Bansal 		 */
179939c13c20SShubham Bansal 		if (i == ctx->prog->len - 1)
180039c13c20SShubham Bansal 			break;
180139c13c20SShubham Bansal 		jmp_offset = epilogue_offset(ctx);
180239c13c20SShubham Bansal 		check_imm24(jmp_offset);
180339c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
180439c13c20SShubham Bansal 		break;
180539c13c20SShubham Bansal notyet:
180639c13c20SShubham Bansal 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
180739c13c20SShubham Bansal 		return -EFAULT;
180839c13c20SShubham Bansal 	default:
180939c13c20SShubham Bansal 		pr_err_once("unknown opcode %02x\n", code);
181039c13c20SShubham Bansal 		return -EINVAL;
1811ddecdfceSMircea Gherzan 	}
18120b59d880SNicolas Schichan 
18130b59d880SNicolas Schichan 	if (ctx->flags & FLAG_IMM_OVERFLOW)
18140b59d880SNicolas Schichan 		/*
18150b59d880SNicolas Schichan 		 * this instruction generated an overflow when
18160b59d880SNicolas Schichan 		 * trying to access the literal pool, so
18170b59d880SNicolas Schichan 		 * delegate this filter to the kernel interpreter.
18180b59d880SNicolas Schichan 		 */
18190b59d880SNicolas Schichan 		return -1;
182039c13c20SShubham Bansal 	return 0;
1821ddecdfceSMircea Gherzan }
1822ddecdfceSMircea Gherzan 
182339c13c20SShubham Bansal static int build_body(struct jit_ctx *ctx)
182439c13c20SShubham Bansal {
182539c13c20SShubham Bansal 	const struct bpf_prog *prog = ctx->prog;
182639c13c20SShubham Bansal 	unsigned int i;
182739c13c20SShubham Bansal 
182839c13c20SShubham Bansal 	for (i = 0; i < prog->len; i++) {
182939c13c20SShubham Bansal 		const struct bpf_insn *insn = &(prog->insnsi[i]);
183039c13c20SShubham Bansal 		int ret;
183139c13c20SShubham Bansal 
183239c13c20SShubham Bansal 		ret = build_insn(insn, ctx);
183339c13c20SShubham Bansal 
183439c13c20SShubham Bansal 		/* It's used with loading the 64 bit immediate value. */
183539c13c20SShubham Bansal 		if (ret > 0) {
183639c13c20SShubham Bansal 			i++;
1837ddecdfceSMircea Gherzan 			if (ctx->target == NULL)
183839c13c20SShubham Bansal 				ctx->offsets[i] = ctx->idx;
183939c13c20SShubham Bansal 			continue;
184039c13c20SShubham Bansal 		}
184139c13c20SShubham Bansal 
184239c13c20SShubham Bansal 		if (ctx->target == NULL)
184339c13c20SShubham Bansal 			ctx->offsets[i] = ctx->idx;
184439c13c20SShubham Bansal 
184539c13c20SShubham Bansal 		/* If unsuccesfull, return with error code */
184639c13c20SShubham Bansal 		if (ret)
184739c13c20SShubham Bansal 			return ret;
184839c13c20SShubham Bansal 	}
184939c13c20SShubham Bansal 	return 0;
185039c13c20SShubham Bansal }
185139c13c20SShubham Bansal 
185239c13c20SShubham Bansal static int validate_code(struct jit_ctx *ctx)
185339c13c20SShubham Bansal {
185439c13c20SShubham Bansal 	int i;
185539c13c20SShubham Bansal 
185639c13c20SShubham Bansal 	for (i = 0; i < ctx->idx; i++) {
185739c13c20SShubham Bansal 		if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
185839c13c20SShubham Bansal 			return -1;
185939c13c20SShubham Bansal 	}
1860ddecdfceSMircea Gherzan 
1861ddecdfceSMircea Gherzan 	return 0;
1862ddecdfceSMircea Gherzan }
1863ddecdfceSMircea Gherzan 
186439c13c20SShubham Bansal void bpf_jit_compile(struct bpf_prog *prog)
1865ddecdfceSMircea Gherzan {
186639c13c20SShubham Bansal 	/* Nothing to do here. We support Internal BPF. */
186739c13c20SShubham Bansal }
1868ddecdfceSMircea Gherzan 
1869163541e6SJiong Wang bool bpf_jit_needs_zext(void)
1870163541e6SJiong Wang {
1871163541e6SJiong Wang 	return true;
1872163541e6SJiong Wang }
1873163541e6SJiong Wang 
187439c13c20SShubham Bansal struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
187539c13c20SShubham Bansal {
187639c13c20SShubham Bansal 	struct bpf_prog *tmp, *orig_prog = prog;
187739c13c20SShubham Bansal 	struct bpf_binary_header *header;
187839c13c20SShubham Bansal 	bool tmp_blinded = false;
187939c13c20SShubham Bansal 	struct jit_ctx ctx;
188039c13c20SShubham Bansal 	unsigned int tmp_idx;
188139c13c20SShubham Bansal 	unsigned int image_size;
188239c13c20SShubham Bansal 	u8 *image_ptr;
188339c13c20SShubham Bansal 
188439c13c20SShubham Bansal 	/* If BPF JIT was not enabled then we must fall back to
188539c13c20SShubham Bansal 	 * the interpreter.
188639c13c20SShubham Bansal 	 */
188760b58afcSAlexei Starovoitov 	if (!prog->jit_requested)
188839c13c20SShubham Bansal 		return orig_prog;
188939c13c20SShubham Bansal 
189039c13c20SShubham Bansal 	/* If constant blinding was enabled and we failed during blinding
189139c13c20SShubham Bansal 	 * then we must fall back to the interpreter. Otherwise, we save
189239c13c20SShubham Bansal 	 * the new JITed code.
189339c13c20SShubham Bansal 	 */
189439c13c20SShubham Bansal 	tmp = bpf_jit_blind_constants(prog);
189539c13c20SShubham Bansal 
189639c13c20SShubham Bansal 	if (IS_ERR(tmp))
189739c13c20SShubham Bansal 		return orig_prog;
189839c13c20SShubham Bansal 	if (tmp != prog) {
189939c13c20SShubham Bansal 		tmp_blinded = true;
190039c13c20SShubham Bansal 		prog = tmp;
190139c13c20SShubham Bansal 	}
1902ddecdfceSMircea Gherzan 
1903ddecdfceSMircea Gherzan 	memset(&ctx, 0, sizeof(ctx));
190439c13c20SShubham Bansal 	ctx.prog = prog;
19058c9602d3SRussell King 	ctx.cpu_architecture = cpu_architecture();
1906ddecdfceSMircea Gherzan 
190739c13c20SShubham Bansal 	/* Not able to allocate memory for offsets[] , then
190839c13c20SShubham Bansal 	 * we must fall back to the interpreter
190939c13c20SShubham Bansal 	 */
191039c13c20SShubham Bansal 	ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
191139c13c20SShubham Bansal 	if (ctx.offsets == NULL) {
191239c13c20SShubham Bansal 		prog = orig_prog;
1913ddecdfceSMircea Gherzan 		goto out;
191439c13c20SShubham Bansal 	}
191539c13c20SShubham Bansal 
191639c13c20SShubham Bansal 	/* 1) fake pass to find in the length of the JITed code,
191739c13c20SShubham Bansal 	 * to compute ctx->offsets and other context variables
191839c13c20SShubham Bansal 	 * needed to compute final JITed code.
191939c13c20SShubham Bansal 	 * Also, calculate random starting pointer/start of JITed code
192039c13c20SShubham Bansal 	 * which is prefixed by random number of fault instructions.
192139c13c20SShubham Bansal 	 *
192239c13c20SShubham Bansal 	 * If the first pass fails then there is no chance of it
192339c13c20SShubham Bansal 	 * being successful in the second pass, so just fall back
192439c13c20SShubham Bansal 	 * to the interpreter.
192539c13c20SShubham Bansal 	 */
192639c13c20SShubham Bansal 	if (build_body(&ctx)) {
192739c13c20SShubham Bansal 		prog = orig_prog;
192839c13c20SShubham Bansal 		goto out_off;
192939c13c20SShubham Bansal 	}
1930ddecdfceSMircea Gherzan 
1931ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1932ddecdfceSMircea Gherzan 	build_prologue(&ctx);
1933ddecdfceSMircea Gherzan 	ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1934ddecdfceSMircea Gherzan 
193539c13c20SShubham Bansal 	ctx.epilogue_offset = ctx.idx;
193639c13c20SShubham Bansal 
1937ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1938ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1939ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1940ddecdfceSMircea Gherzan 	ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1941ddecdfceSMircea Gherzan 
1942ddecdfceSMircea Gherzan 	ctx.idx += ctx.imm_count;
1943ddecdfceSMircea Gherzan 	if (ctx.imm_count) {
194439c13c20SShubham Bansal 		ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
194539c13c20SShubham Bansal 		if (ctx.imms == NULL) {
194639c13c20SShubham Bansal 			prog = orig_prog;
194739c13c20SShubham Bansal 			goto out_off;
194839c13c20SShubham Bansal 		}
1949ddecdfceSMircea Gherzan 	}
1950ddecdfceSMircea Gherzan #else
195139c13c20SShubham Bansal 	/* there's nothing about the epilogue on ARMv7 */
1952ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1953ddecdfceSMircea Gherzan #endif
195439c13c20SShubham Bansal 	/* Now we can get the actual image size of the JITed arm code.
195539c13c20SShubham Bansal 	 * Currently, we are not considering the THUMB-2 instructions
195639c13c20SShubham Bansal 	 * for jit, although it can decrease the size of the image.
195739c13c20SShubham Bansal 	 *
195839c13c20SShubham Bansal 	 * As each arm instruction is of length 32bit, we are translating
195939c13c20SShubham Bansal 	 * number of JITed intructions into the size required to store these
196039c13c20SShubham Bansal 	 * JITed code.
196139c13c20SShubham Bansal 	 */
196239c13c20SShubham Bansal 	image_size = sizeof(u32) * ctx.idx;
1963ddecdfceSMircea Gherzan 
196439c13c20SShubham Bansal 	/* Now we know the size of the structure to make */
196539c13c20SShubham Bansal 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
196639c13c20SShubham Bansal 				      sizeof(u32), jit_fill_hole);
196739c13c20SShubham Bansal 	/* Not able to allocate memory for the structure then
196839c13c20SShubham Bansal 	 * we must fall back to the interpretation
196939c13c20SShubham Bansal 	 */
197039c13c20SShubham Bansal 	if (header == NULL) {
197139c13c20SShubham Bansal 		prog = orig_prog;
197239c13c20SShubham Bansal 		goto out_imms;
197339c13c20SShubham Bansal 	}
197439c13c20SShubham Bansal 
197539c13c20SShubham Bansal 	/* 2.) Actual pass to generate final JIT code */
197639c13c20SShubham Bansal 	ctx.target = (u32 *) image_ptr;
1977ddecdfceSMircea Gherzan 	ctx.idx = 0;
197855309dd3SDaniel Borkmann 
1979ddecdfceSMircea Gherzan 	build_prologue(&ctx);
198039c13c20SShubham Bansal 
198139c13c20SShubham Bansal 	/* If building the body of the JITed code fails somehow,
198239c13c20SShubham Bansal 	 * we fall back to the interpretation.
198339c13c20SShubham Bansal 	 */
19840b59d880SNicolas Schichan 	if (build_body(&ctx) < 0) {
198539c13c20SShubham Bansal 		image_ptr = NULL;
19860b59d880SNicolas Schichan 		bpf_jit_binary_free(header);
198739c13c20SShubham Bansal 		prog = orig_prog;
198839c13c20SShubham Bansal 		goto out_imms;
19890b59d880SNicolas Schichan 	}
1990ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1991ddecdfceSMircea Gherzan 
199239c13c20SShubham Bansal 	/* 3.) Extra pass to validate JITed Code */
199339c13c20SShubham Bansal 	if (validate_code(&ctx)) {
199439c13c20SShubham Bansal 		image_ptr = NULL;
199539c13c20SShubham Bansal 		bpf_jit_binary_free(header);
199639c13c20SShubham Bansal 		prog = orig_prog;
199739c13c20SShubham Bansal 		goto out_imms;
199839c13c20SShubham Bansal 	}
1999ebaef649SDaniel Borkmann 	flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
2000ddecdfceSMircea Gherzan 
200139c13c20SShubham Bansal 	if (bpf_jit_enable > 1)
200239c13c20SShubham Bansal 		/* there are 2 passes here */
200339c13c20SShubham Bansal 		bpf_jit_dump(prog->len, image_size, 2, ctx.target);
200439c13c20SShubham Bansal 
200518d405afSDaniel Borkmann 	bpf_jit_binary_lock_ro(header);
200639c13c20SShubham Bansal 	prog->bpf_func = (void *)ctx.target;
200739c13c20SShubham Bansal 	prog->jited = 1;
200839c13c20SShubham Bansal 	prog->jited_len = image_size;
200939c13c20SShubham Bansal 
201039c13c20SShubham Bansal out_imms:
2011ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
2012ddecdfceSMircea Gherzan 	if (ctx.imm_count)
2013ddecdfceSMircea Gherzan 		kfree(ctx.imms);
2014ddecdfceSMircea Gherzan #endif
201539c13c20SShubham Bansal out_off:
2016ddecdfceSMircea Gherzan 	kfree(ctx.offsets);
201739c13c20SShubham Bansal out:
201839c13c20SShubham Bansal 	if (tmp_blinded)
201939c13c20SShubham Bansal 		bpf_jit_prog_release_other(prog, prog == orig_prog ?
202039c13c20SShubham Bansal 					   tmp : orig_prog);
202139c13c20SShubham Bansal 	return prog;
2022ddecdfceSMircea Gherzan }
2023ddecdfceSMircea Gherzan 
2024