xref: /openbmc/linux/arch/arm/net/bpf_jit_32.c (revision fc386ba7)
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)
3979e3445bSJohan Almbladh  *                        | ... | caller-saved registers
4079e3445bSJohan Almbladh  *                        +-----+
4179e3445bSJohan Almbladh  *                        | ... | arguments passed on stack
4279e3445bSJohan Almbladh  * ARM_SP during call =>  +-----|
4370ec3a6cSRussell King  *                        |     |
4470ec3a6cSRussell King  *                        | ... | Function call stack
4570ec3a6cSRussell King  *                        |     |
4670ec3a6cSRussell King  *                        +-----+
4770ec3a6cSRussell King  *                          low
480005e55aSRussell King  *
490005e55aSRussell King  * The callee saved registers depends on whether frame pointers are enabled.
500005e55aSRussell King  * With frame pointers (to be compliant with the ABI):
510005e55aSRussell King  *
520005e55aSRussell King  *                              high
53bef8968dSRussell King  * original ARM_SP =>     +--------------+ \
540005e55aSRussell King  *                        |      pc      | |
55bef8968dSRussell King  * current ARM_FP =>      +--------------+ } callee saved registers
56bef8968dSRussell King  *                        |r4-r9,fp,ip,lr| |
57bef8968dSRussell King  *                        +--------------+ /
580005e55aSRussell King  *                              low
590005e55aSRussell King  *
600005e55aSRussell King  * Without frame pointers:
610005e55aSRussell King  *
620005e55aSRussell King  *                              high
63bef8968dSRussell King  * original ARM_SP =>     +--------------+
64bef8968dSRussell King  *                        |  r4-r9,fp,lr | callee saved registers
65bef8968dSRussell King  * current ARM_FP =>      +--------------+
660005e55aSRussell King  *                              low
6702088d9bSRussell King  *
6802088d9bSRussell King  * When popping registers off the stack at the end of a BPF function, we
6902088d9bSRussell King  * reference them via the current ARM_FP register.
7079e3445bSJohan Almbladh  *
7179e3445bSJohan Almbladh  * Some eBPF operations are implemented via a call to a helper function.
7279e3445bSJohan Almbladh  * Such calls are "invisible" in the eBPF code, so it is up to the calling
7379e3445bSJohan Almbladh  * program to preserve any caller-saved ARM registers during the call. The
7479e3445bSJohan Almbladh  * JIT emits code to push and pop those registers onto the stack, immediately
7579e3445bSJohan Almbladh  * above the callee stack frame.
7670ec3a6cSRussell King  */
7702088d9bSRussell King #define CALLEE_MASK	(1 << ARM_R4 | 1 << ARM_R5 | 1 << ARM_R6 | \
78bef8968dSRussell King 			 1 << ARM_R7 | 1 << ARM_R8 | 1 << ARM_R9 | \
7902088d9bSRussell King 			 1 << ARM_FP)
8002088d9bSRussell King #define CALLEE_PUSH_MASK (CALLEE_MASK | 1 << ARM_LR)
8102088d9bSRussell King #define CALLEE_POP_MASK  (CALLEE_MASK | 1 << ARM_PC)
8270ec3a6cSRussell King 
8379e3445bSJohan Almbladh #define CALLER_MASK	(1 << ARM_R0 | 1 << ARM_R1 | 1 << ARM_R2 | 1 << ARM_R3)
8479e3445bSJohan Almbladh 
85d449ceb1SRussell King enum {
86d449ceb1SRussell King 	/* Stack layout - these are offsets from (top of stack - 4) */
87d449ceb1SRussell King 	BPF_R2_HI,
88d449ceb1SRussell King 	BPF_R2_LO,
89d449ceb1SRussell King 	BPF_R3_HI,
90d449ceb1SRussell King 	BPF_R3_LO,
91d449ceb1SRussell King 	BPF_R4_HI,
92d449ceb1SRussell King 	BPF_R4_LO,
93d449ceb1SRussell King 	BPF_R5_HI,
94d449ceb1SRussell King 	BPF_R5_LO,
95d449ceb1SRussell King 	BPF_R7_HI,
96d449ceb1SRussell King 	BPF_R7_LO,
97d449ceb1SRussell King 	BPF_R8_HI,
98d449ceb1SRussell King 	BPF_R8_LO,
99d449ceb1SRussell King 	BPF_R9_HI,
100d449ceb1SRussell King 	BPF_R9_LO,
101d449ceb1SRussell King 	BPF_FP_HI,
102d449ceb1SRussell King 	BPF_FP_LO,
103d449ceb1SRussell King 	BPF_TC_HI,
104d449ceb1SRussell King 	BPF_TC_LO,
105d449ceb1SRussell King 	BPF_AX_HI,
106d449ceb1SRussell King 	BPF_AX_LO,
107d449ceb1SRussell King 	/* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
108d449ceb1SRussell King 	 * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
109d449ceb1SRussell King 	 * BPF_REG_FP and Tail call counts.
110d449ceb1SRussell King 	 */
111d449ceb1SRussell King 	BPF_JIT_SCRATCH_REGS,
112d449ceb1SRussell King };
113d449ceb1SRussell King 
1141c35ba12SRussell King /*
1151c35ba12SRussell King  * Negative "register" values indicate the register is stored on the stack
1161c35ba12SRussell King  * and are the offset from the top of the eBPF JIT scratch space.
1171c35ba12SRussell King  */
1181c35ba12SRussell King #define STACK_OFFSET(k)	(-4 - (k) * 4)
119d449ceb1SRussell King #define SCRATCH_SIZE	(BPF_JIT_SCRATCH_REGS * 4)
120d449ceb1SRussell King 
12196cced4eSRussell King #ifdef CONFIG_FRAME_POINTER
12296cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) ((x) - 4 * hweight16(CALLEE_PUSH_MASK) - 4)
12396cced4eSRussell King #else
12496cced4eSRussell King #define EBPF_SCRATCH_TO_ARM_FP(x) (x)
12596cced4eSRussell King #endif
12696cced4eSRussell King 
12739c13c20SShubham Bansal #define TMP_REG_1	(MAX_BPF_JIT_REG + 0)	/* TEMP Register 1 */
12839c13c20SShubham Bansal #define TMP_REG_2	(MAX_BPF_JIT_REG + 1)	/* TEMP Register 2 */
12939c13c20SShubham Bansal #define TCALL_CNT	(MAX_BPF_JIT_REG + 2)	/* Tail Call Count */
13039c13c20SShubham Bansal 
13139c13c20SShubham Bansal #define FLAG_IMM_OVERFLOW	(1 << 0)
13239c13c20SShubham Bansal 
133ddecdfceSMircea Gherzan /*
13439c13c20SShubham Bansal  * Map eBPF registers to ARM 32bit registers or stack scratch space.
135ddecdfceSMircea Gherzan  *
13639c13c20SShubham Bansal  * 1. First argument is passed using the arm 32bit registers and rest of the
13739c13c20SShubham Bansal  * arguments are passed on stack scratch space.
1382b589a7eSWang YanQing  * 2. First callee-saved argument is mapped to arm 32 bit registers and rest
13939c13c20SShubham Bansal  * arguments are mapped to scratch space on stack.
14039c13c20SShubham Bansal  * 3. We need two 64 bit temp registers to do complex operations on eBPF
14139c13c20SShubham Bansal  * registers.
14239c13c20SShubham Bansal  *
14339c13c20SShubham Bansal  * As the eBPF registers are all 64 bit registers and arm has only 32 bit
14439c13c20SShubham Bansal  * registers, we have to map each eBPF registers with two arm 32 bit regs or
14539c13c20SShubham Bansal  * scratch memory space and we have to build eBPF 64 bit register from those.
14639c13c20SShubham Bansal  *
14739c13c20SShubham Bansal  */
1481c35ba12SRussell King static const s8 bpf2a32[][2] = {
14939c13c20SShubham Bansal 	/* return value from in-kernel function, and exit value from eBPF */
15039c13c20SShubham Bansal 	[BPF_REG_0] = {ARM_R1, ARM_R0},
15139c13c20SShubham Bansal 	/* arguments from eBPF program to in-kernel function */
15239c13c20SShubham Bansal 	[BPF_REG_1] = {ARM_R3, ARM_R2},
15339c13c20SShubham Bansal 	/* Stored on stack scratch space */
154d449ceb1SRussell King 	[BPF_REG_2] = {STACK_OFFSET(BPF_R2_HI), STACK_OFFSET(BPF_R2_LO)},
155d449ceb1SRussell King 	[BPF_REG_3] = {STACK_OFFSET(BPF_R3_HI), STACK_OFFSET(BPF_R3_LO)},
156d449ceb1SRussell King 	[BPF_REG_4] = {STACK_OFFSET(BPF_R4_HI), STACK_OFFSET(BPF_R4_LO)},
157d449ceb1SRussell King 	[BPF_REG_5] = {STACK_OFFSET(BPF_R5_HI), STACK_OFFSET(BPF_R5_LO)},
15839c13c20SShubham Bansal 	/* callee saved registers that in-kernel function will preserve */
15939c13c20SShubham Bansal 	[BPF_REG_6] = {ARM_R5, ARM_R4},
16039c13c20SShubham Bansal 	/* Stored on stack scratch space */
161d449ceb1SRussell King 	[BPF_REG_7] = {STACK_OFFSET(BPF_R7_HI), STACK_OFFSET(BPF_R7_LO)},
162d449ceb1SRussell King 	[BPF_REG_8] = {STACK_OFFSET(BPF_R8_HI), STACK_OFFSET(BPF_R8_LO)},
163d449ceb1SRussell King 	[BPF_REG_9] = {STACK_OFFSET(BPF_R9_HI), STACK_OFFSET(BPF_R9_LO)},
16439c13c20SShubham Bansal 	/* Read only Frame Pointer to access Stack */
165d449ceb1SRussell King 	[BPF_REG_FP] = {STACK_OFFSET(BPF_FP_HI), STACK_OFFSET(BPF_FP_LO)},
16606edc59cSChristoph Hellwig 	/* Temporary Register for BPF JIT, can be used
16739c13c20SShubham Bansal 	 * for constant blindings and others.
16839c13c20SShubham Bansal 	 */
16939c13c20SShubham Bansal 	[TMP_REG_1] = {ARM_R7, ARM_R6},
170bef8968dSRussell King 	[TMP_REG_2] = {ARM_R9, ARM_R8},
17139c13c20SShubham Bansal 	/* Tail call count. Stored on stack scratch space. */
172d449ceb1SRussell King 	[TCALL_CNT] = {STACK_OFFSET(BPF_TC_HI), STACK_OFFSET(BPF_TC_LO)},
17339c13c20SShubham Bansal 	/* temporary register for blinding constants.
17439c13c20SShubham Bansal 	 * Stored on stack scratch space.
17539c13c20SShubham Bansal 	 */
176d449ceb1SRussell King 	[BPF_REG_AX] = {STACK_OFFSET(BPF_AX_HI), STACK_OFFSET(BPF_AX_LO)},
17739c13c20SShubham Bansal };
17839c13c20SShubham Bansal 
17939c13c20SShubham Bansal #define	dst_lo	dst[1]
18039c13c20SShubham Bansal #define dst_hi	dst[0]
18139c13c20SShubham Bansal #define src_lo	src[1]
18239c13c20SShubham Bansal #define src_hi	src[0]
18339c13c20SShubham Bansal 
18439c13c20SShubham Bansal /*
18539c13c20SShubham Bansal  * JIT Context:
18639c13c20SShubham Bansal  *
18739c13c20SShubham Bansal  * prog			:	bpf_prog
18839c13c20SShubham Bansal  * idx			:	index of current last JITed instruction.
18939c13c20SShubham Bansal  * prologue_bytes	:	bytes used in prologue.
19039c13c20SShubham Bansal  * epilogue_offset	:	offset of epilogue starting.
19139c13c20SShubham Bansal  * offsets		:	array of eBPF instruction offsets in
19239c13c20SShubham Bansal  *				JITed code.
19339c13c20SShubham Bansal  * target		:	final JITed code.
19439c13c20SShubham Bansal  * epilogue_bytes	:	no of bytes used in epilogue.
19539c13c20SShubham Bansal  * imm_count		:	no of immediate counts used for global
19639c13c20SShubham Bansal  *				variables.
19739c13c20SShubham Bansal  * imms			:	array of global variable addresses.
198ddecdfceSMircea Gherzan  */
199ddecdfceSMircea Gherzan 
200ddecdfceSMircea Gherzan struct jit_ctx {
20139c13c20SShubham Bansal 	const struct bpf_prog *prog;
20239c13c20SShubham Bansal 	unsigned int idx;
20339c13c20SShubham Bansal 	unsigned int prologue_bytes;
20439c13c20SShubham Bansal 	unsigned int epilogue_offset;
2058c9602d3SRussell King 	unsigned int cpu_architecture;
206ddecdfceSMircea Gherzan 	u32 flags;
207ddecdfceSMircea Gherzan 	u32 *offsets;
208ddecdfceSMircea Gherzan 	u32 *target;
20939c13c20SShubham Bansal 	u32 stack_size;
210ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
211ddecdfceSMircea Gherzan 	u16 epilogue_bytes;
212ddecdfceSMircea Gherzan 	u16 imm_count;
213ddecdfceSMircea Gherzan 	u32 *imms;
214ddecdfceSMircea Gherzan #endif
215ddecdfceSMircea Gherzan };
216ddecdfceSMircea Gherzan 
217ddecdfceSMircea Gherzan /*
2184560cdffSNicolas Schichan  * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
219ddecdfceSMircea Gherzan  * (where the assembly routines like __aeabi_uidiv could cause problems).
220ddecdfceSMircea Gherzan  */
jit_udiv32(u32 dividend,u32 divisor)22139c13c20SShubham Bansal static u32 jit_udiv32(u32 dividend, u32 divisor)
222ddecdfceSMircea Gherzan {
223ddecdfceSMircea Gherzan 	return dividend / divisor;
224ddecdfceSMircea Gherzan }
225ddecdfceSMircea Gherzan 
jit_mod32(u32 dividend,u32 divisor)22639c13c20SShubham Bansal static u32 jit_mod32(u32 dividend, u32 divisor)
2274560cdffSNicolas Schichan {
2284560cdffSNicolas Schichan 	return dividend % divisor;
2294560cdffSNicolas Schichan }
2304560cdffSNicolas Schichan 
_emit(int cond,u32 inst,struct jit_ctx * ctx)231ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
232ddecdfceSMircea Gherzan {
2333460743eSBen Dooks 	inst |= (cond << 28);
2343460743eSBen Dooks 	inst = __opcode_to_mem_arm(inst);
2353460743eSBen Dooks 
236ddecdfceSMircea Gherzan 	if (ctx->target != NULL)
2373460743eSBen Dooks 		ctx->target[ctx->idx] = inst;
238ddecdfceSMircea Gherzan 
239ddecdfceSMircea Gherzan 	ctx->idx++;
240ddecdfceSMircea Gherzan }
241ddecdfceSMircea Gherzan 
242ddecdfceSMircea Gherzan /*
243ddecdfceSMircea Gherzan  * Emit an instruction that will be executed unconditionally.
244ddecdfceSMircea Gherzan  */
emit(u32 inst,struct jit_ctx * ctx)245ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx)
246ddecdfceSMircea Gherzan {
247ddecdfceSMircea Gherzan 	_emit(ARM_COND_AL, inst, ctx);
248ddecdfceSMircea Gherzan }
249ddecdfceSMircea Gherzan 
25039c13c20SShubham Bansal /*
2511ca3b17bSRussell King  * This is rather horrid, but necessary to convert an integer constant
2521ca3b17bSRussell King  * to an immediate operand for the opcodes, and be able to detect at
2531ca3b17bSRussell King  * build time whether the constant can't be converted (iow, usable in
2541ca3b17bSRussell King  * BUILD_BUG_ON()).
2551ca3b17bSRussell King  */
2561ca3b17bSRussell King #define imm12val(v, s) (rol32(v, (s)) | (s) << 7)
2571ca3b17bSRussell King #define const_imm8m(x)					\
2581ca3b17bSRussell King 	({ int r;					\
2591ca3b17bSRussell King 	   u32 v = (x);					\
2601ca3b17bSRussell King 	   if (!(v & ~0x000000ff))			\
2611ca3b17bSRussell King 		r = imm12val(v, 0);			\
2621ca3b17bSRussell King 	   else if (!(v & ~0xc000003f))			\
2631ca3b17bSRussell King 		r = imm12val(v, 2);			\
2641ca3b17bSRussell King 	   else if (!(v & ~0xf000000f))			\
2651ca3b17bSRussell King 		r = imm12val(v, 4);			\
2661ca3b17bSRussell King 	   else if (!(v & ~0xfc000003))			\
2671ca3b17bSRussell King 		r = imm12val(v, 6);			\
2681ca3b17bSRussell King 	   else if (!(v & ~0xff000000))			\
2691ca3b17bSRussell King 		r = imm12val(v, 8);			\
2701ca3b17bSRussell King 	   else if (!(v & ~0x3fc00000))			\
2711ca3b17bSRussell King 		r = imm12val(v, 10);			\
2721ca3b17bSRussell King 	   else if (!(v & ~0x0ff00000))			\
2731ca3b17bSRussell King 		r = imm12val(v, 12);			\
2741ca3b17bSRussell King 	   else if (!(v & ~0x03fc0000))			\
2751ca3b17bSRussell King 		r = imm12val(v, 14);			\
2761ca3b17bSRussell King 	   else if (!(v & ~0x00ff0000))			\
2771ca3b17bSRussell King 		r = imm12val(v, 16);			\
2781ca3b17bSRussell King 	   else if (!(v & ~0x003fc000))			\
2791ca3b17bSRussell King 		r = imm12val(v, 18);			\
2801ca3b17bSRussell King 	   else if (!(v & ~0x000ff000))			\
2811ca3b17bSRussell King 		r = imm12val(v, 20);			\
2821ca3b17bSRussell King 	   else if (!(v & ~0x0003fc00))			\
2831ca3b17bSRussell King 		r = imm12val(v, 22);			\
2841ca3b17bSRussell King 	   else if (!(v & ~0x0000ff00))			\
2851ca3b17bSRussell King 		r = imm12val(v, 24);			\
2861ca3b17bSRussell King 	   else if (!(v & ~0x00003fc0))			\
2871ca3b17bSRussell King 		r = imm12val(v, 26);			\
2881ca3b17bSRussell King 	   else if (!(v & ~0x00000ff0))			\
2891ca3b17bSRussell King 		r = imm12val(v, 28);			\
2901ca3b17bSRussell King 	   else if (!(v & ~0x000003fc))			\
2911ca3b17bSRussell King 		r = imm12val(v, 30);			\
2921ca3b17bSRussell King 	   else						\
2931ca3b17bSRussell King 		r = -1;					\
2941ca3b17bSRussell King 	   r; })
2951ca3b17bSRussell King 
2961ca3b17bSRussell King /*
29739c13c20SShubham Bansal  * Checks if immediate value can be converted to imm12(12 bits) value.
29839c13c20SShubham Bansal  */
imm8m(u32 x)2991ca3b17bSRussell King static int imm8m(u32 x)
300ddecdfceSMircea Gherzan {
30139c13c20SShubham Bansal 	u32 rot;
302ddecdfceSMircea Gherzan 
30339c13c20SShubham Bansal 	for (rot = 0; rot < 16; rot++)
30439c13c20SShubham Bansal 		if ((x & ~ror32(0xff, 2 * rot)) == 0)
30539c13c20SShubham Bansal 			return rol32(x, 2 * rot) | (rot << 8);
30639c13c20SShubham Bansal 	return -1;
307ddecdfceSMircea Gherzan }
308ddecdfceSMircea Gherzan 
3091ca3b17bSRussell King #define imm8m(x) (__builtin_constant_p(x) ? const_imm8m(x) : imm8m(x))
3101ca3b17bSRussell King 
arm_bpf_ldst_imm12(u32 op,u8 rt,u8 rn,s16 imm12)311a8ef95a0SRussell King static u32 arm_bpf_ldst_imm12(u32 op, u8 rt, u8 rn, s16 imm12)
312a8ef95a0SRussell King {
313a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
314a8ef95a0SRussell King 	if (imm12 >= 0)
315a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
316a8ef95a0SRussell King 	else
317a8ef95a0SRussell King 		imm12 = -imm12;
318828e2b90SRussell King 	return op | (imm12 & ARM_INST_LDST__IMM12);
319a8ef95a0SRussell King }
320a8ef95a0SRussell King 
arm_bpf_ldst_imm8(u32 op,u8 rt,u8 rn,s16 imm8)321a8ef95a0SRussell King static u32 arm_bpf_ldst_imm8(u32 op, u8 rt, u8 rn, s16 imm8)
322a8ef95a0SRussell King {
323a8ef95a0SRussell King 	op |= rt << 12 | rn << 16;
324a8ef95a0SRussell King 	if (imm8 >= 0)
325a8ef95a0SRussell King 		op |= ARM_INST_LDST__U;
326a8ef95a0SRussell King 	else
327a8ef95a0SRussell King 		imm8 = -imm8;
328a8ef95a0SRussell King 	return op | (imm8 & 0xf0) << 4 | (imm8 & 0x0f);
329a8ef95a0SRussell King }
330a8ef95a0SRussell King 
331a8ef95a0SRussell King #define ARM_LDR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDR_I, rt, rn, off)
332a8ef95a0SRussell King #define ARM_LDRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_LDRB_I, rt, rn, off)
3338c9602d3SRussell King #define ARM_LDRD_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_LDRD_I, rt, rn, off)
334a8ef95a0SRussell King #define ARM_LDRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_LDRH_I, rt, rn, off)
335a8ef95a0SRussell King 
336a8ef95a0SRussell King #define ARM_STR_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STR_I, rt, rn, off)
337a8ef95a0SRussell King #define ARM_STRB_I(rt, rn, off)	arm_bpf_ldst_imm12(ARM_INST_STRB_I, rt, rn, off)
3388c9602d3SRussell King #define ARM_STRD_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_STRD_I, rt, rn, off)
339a8ef95a0SRussell King #define ARM_STRH_I(rt, rn, off)	arm_bpf_ldst_imm8(ARM_INST_STRH_I, rt, rn, off)
340a8ef95a0SRussell King 
34139c13c20SShubham Bansal /*
34239c13c20SShubham Bansal  * Initializes the JIT space with undefined instructions.
34339c13c20SShubham Bansal  */
jit_fill_hole(void * area,unsigned int size)34455309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
34555309dd3SDaniel Borkmann {
346e8b56d55SDaniel Borkmann 	u32 *ptr;
34755309dd3SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
34855309dd3SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
349e8b56d55SDaniel Borkmann 		*ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
35055309dd3SDaniel Borkmann }
35155309dd3SDaniel Borkmann 
352d1220efdSRussell King #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
353d1220efdSRussell King /* EABI requires the stack to be aligned to 64-bit boundaries */
354d1220efdSRussell King #define STACK_ALIGNMENT	8
355d1220efdSRussell King #else
356d1220efdSRussell King /* Stack must be aligned to 32-bit boundaries */
357d1220efdSRussell King #define STACK_ALIGNMENT	4
358d1220efdSRussell King #endif
359ddecdfceSMircea Gherzan 
36039c13c20SShubham Bansal /* total stack size used in JITed code */
36138ca9306SDaniel Borkmann #define _STACK_SIZE	(ctx->prog->aux->stack_depth + SCRATCH_SIZE)
362d1220efdSRussell King #define STACK_SIZE	ALIGN(_STACK_SIZE, STACK_ALIGNMENT)
363ddecdfceSMircea Gherzan 
364ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
365ddecdfceSMircea Gherzan 
imm_offset(u32 k,struct jit_ctx * ctx)366ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx)
367ddecdfceSMircea Gherzan {
36839c13c20SShubham Bansal 	unsigned int i = 0, offset;
369ddecdfceSMircea Gherzan 	u16 imm;
370ddecdfceSMircea Gherzan 
371ddecdfceSMircea Gherzan 	/* on the "fake" run we just count them (duplicates included) */
372ddecdfceSMircea Gherzan 	if (ctx->target == NULL) {
373ddecdfceSMircea Gherzan 		ctx->imm_count++;
374ddecdfceSMircea Gherzan 		return 0;
375ddecdfceSMircea Gherzan 	}
376ddecdfceSMircea Gherzan 
377ddecdfceSMircea Gherzan 	while ((i < ctx->imm_count) && ctx->imms[i]) {
378ddecdfceSMircea Gherzan 		if (ctx->imms[i] == k)
379ddecdfceSMircea Gherzan 			break;
380ddecdfceSMircea Gherzan 		i++;
381ddecdfceSMircea Gherzan 	}
382ddecdfceSMircea Gherzan 
383ddecdfceSMircea Gherzan 	if (ctx->imms[i] == 0)
384ddecdfceSMircea Gherzan 		ctx->imms[i] = k;
385ddecdfceSMircea Gherzan 
386ddecdfceSMircea Gherzan 	/* constants go just after the epilogue */
38739c13c20SShubham Bansal 	offset =  ctx->offsets[ctx->prog->len - 1] * 4;
388ddecdfceSMircea Gherzan 	offset += ctx->prologue_bytes;
389ddecdfceSMircea Gherzan 	offset += ctx->epilogue_bytes;
390ddecdfceSMircea Gherzan 	offset += i * 4;
391ddecdfceSMircea Gherzan 
392ddecdfceSMircea Gherzan 	ctx->target[offset / 4] = k;
393ddecdfceSMircea Gherzan 
394ddecdfceSMircea Gherzan 	/* PC in ARM mode == address of the instruction + 8 */
395ddecdfceSMircea Gherzan 	imm = offset - (8 + ctx->idx * 4);
396ddecdfceSMircea Gherzan 
3970b59d880SNicolas Schichan 	if (imm & ~0xfff) {
3980b59d880SNicolas Schichan 		/*
3990b59d880SNicolas Schichan 		 * literal pool is too far, signal it into flags. we
4000b59d880SNicolas Schichan 		 * can only detect it on the second pass unfortunately.
4010b59d880SNicolas Schichan 		 */
4020b59d880SNicolas Schichan 		ctx->flags |= FLAG_IMM_OVERFLOW;
4030b59d880SNicolas Schichan 		return 0;
4040b59d880SNicolas Schichan 	}
4050b59d880SNicolas Schichan 
406ddecdfceSMircea Gherzan 	return imm;
407ddecdfceSMircea Gherzan }
408ddecdfceSMircea Gherzan 
409ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */
410ddecdfceSMircea Gherzan 
bpf2a32_offset(int bpf_to,int bpf_from,const struct jit_ctx * ctx)41139c13c20SShubham Bansal static inline int bpf2a32_offset(int bpf_to, int bpf_from,
41239c13c20SShubham Bansal 				 const struct jit_ctx *ctx) {
41339c13c20SShubham Bansal 	int to, from;
41439c13c20SShubham Bansal 
41539c13c20SShubham Bansal 	if (ctx->target == NULL)
41639c13c20SShubham Bansal 		return 0;
41739c13c20SShubham Bansal 	to = ctx->offsets[bpf_to];
41839c13c20SShubham Bansal 	from = ctx->offsets[bpf_from];
41939c13c20SShubham Bansal 
42039c13c20SShubham Bansal 	return to - from - 1;
42139c13c20SShubham Bansal }
42239c13c20SShubham Bansal 
423ddecdfceSMircea Gherzan /*
424ddecdfceSMircea Gherzan  * Move an immediate that's not an imm8m to a core register.
425ddecdfceSMircea Gherzan  */
emit_mov_i_no8m(const u8 rd,u32 val,struct jit_ctx * ctx)42639c13c20SShubham Bansal static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
427ddecdfceSMircea Gherzan {
428ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
429ddecdfceSMircea Gherzan 	emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
430ddecdfceSMircea Gherzan #else
431ddecdfceSMircea Gherzan 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
432ddecdfceSMircea Gherzan 	if (val > 0xffff)
433ddecdfceSMircea Gherzan 		emit(ARM_MOVT(rd, val >> 16), ctx);
434ddecdfceSMircea Gherzan #endif
435ddecdfceSMircea Gherzan }
436ddecdfceSMircea Gherzan 
emit_mov_i(const u8 rd,u32 val,struct jit_ctx * ctx)43739c13c20SShubham Bansal static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
438ddecdfceSMircea Gherzan {
439ddecdfceSMircea Gherzan 	int imm12 = imm8m(val);
440ddecdfceSMircea Gherzan 
441ddecdfceSMircea Gherzan 	if (imm12 >= 0)
442ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(rd, imm12), ctx);
443ddecdfceSMircea Gherzan 	else
444ddecdfceSMircea Gherzan 		emit_mov_i_no8m(rd, val, ctx);
445ddecdfceSMircea Gherzan }
446ddecdfceSMircea Gherzan 
emit_bx_r(u8 tgt_reg,struct jit_ctx * ctx)447e9062481SRussell King static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
448ddecdfceSMircea Gherzan {
449ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_THUMB)
450ddecdfceSMircea Gherzan 		emit(ARM_BX(tgt_reg), ctx);
451ddecdfceSMircea Gherzan 	else
452ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
453e9062481SRussell King }
454e9062481SRussell King 
emit_blx_r(u8 tgt_reg,struct jit_ctx * ctx)455ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
456ddecdfceSMircea Gherzan {
457ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5
458ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
459e9062481SRussell King 	emit_bx_r(tgt_reg, ctx);
460ddecdfceSMircea Gherzan #else
461ddecdfceSMircea Gherzan 	emit(ARM_BLX_R(tgt_reg), ctx);
462ddecdfceSMircea Gherzan #endif
463ddecdfceSMircea Gherzan }
464ddecdfceSMircea Gherzan 
epilogue_offset(const struct jit_ctx * ctx)46539c13c20SShubham Bansal static inline int epilogue_offset(const struct jit_ctx *ctx)
466ddecdfceSMircea Gherzan {
46739c13c20SShubham Bansal 	int to, from;
46839c13c20SShubham Bansal 	/* No need for 1st dummy run */
46939c13c20SShubham Bansal 	if (ctx->target == NULL)
47039c13c20SShubham Bansal 		return 0;
47139c13c20SShubham Bansal 	to = ctx->epilogue_offset;
47239c13c20SShubham Bansal 	from = ctx->idx;
47339c13c20SShubham Bansal 
47439c13c20SShubham Bansal 	return to - from - 2;
47539c13c20SShubham Bansal }
47639c13c20SShubham Bansal 
emit_udivmod(u8 rd,u8 rm,u8 rn,struct jit_ctx * ctx,u8 op)47739c13c20SShubham Bansal static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op)
47839c13c20SShubham Bansal {
47979e3445bSJohan Almbladh 	const int exclude_mask = BIT(ARM_R0) | BIT(ARM_R1);
4801c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
48139c13c20SShubham Bansal 
482ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7
483ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_IDIVA) {
48439c13c20SShubham Bansal 		if (op == BPF_DIV)
485ddecdfceSMircea Gherzan 			emit(ARM_UDIV(rd, rm, rn), ctx);
4864560cdffSNicolas Schichan 		else {
48739c13c20SShubham Bansal 			emit(ARM_UDIV(ARM_IP, rm, rn), ctx);
48839c13c20SShubham Bansal 			emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
4894560cdffSNicolas Schichan 		}
490ddecdfceSMircea Gherzan 		return;
491ddecdfceSMircea Gherzan 	}
492ddecdfceSMircea Gherzan #endif
49319fc99d0SNicolas Schichan 
49419fc99d0SNicolas Schichan 	/*
49539c13c20SShubham Bansal 	 * For BPF_ALU | BPF_DIV | BPF_K instructions
49639c13c20SShubham Bansal 	 * As ARM_R1 and ARM_R0 contains 1st argument of bpf
49739c13c20SShubham Bansal 	 * function, we need to save it on caller side to save
49839c13c20SShubham Bansal 	 * it from getting destroyed within callee.
49939c13c20SShubham Bansal 	 * After the return from the callee, we restore ARM_R0
50039c13c20SShubham Bansal 	 * ARM_R1.
50119fc99d0SNicolas Schichan 	 */
50239c13c20SShubham Bansal 	if (rn != ARM_R1) {
50339c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
504ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R1, rn), ctx);
50539c13c20SShubham Bansal 	}
50639c13c20SShubham Bansal 	if (rm != ARM_R0) {
50739c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
50819fc99d0SNicolas Schichan 		emit(ARM_MOV_R(ARM_R0, rm), ctx);
50939c13c20SShubham Bansal 	}
510ddecdfceSMircea Gherzan 
51179e3445bSJohan Almbladh 	/* Push caller-saved registers on stack */
51279e3445bSJohan Almbladh 	emit(ARM_PUSH(CALLER_MASK & ~exclude_mask), ctx);
51379e3445bSJohan Almbladh 
51439c13c20SShubham Bansal 	/* Call appropriate function */
51539c13c20SShubham Bansal 	emit_mov_i(ARM_IP, op == BPF_DIV ?
51639c13c20SShubham Bansal 		   (u32)jit_udiv32 : (u32)jit_mod32, ctx);
51739c13c20SShubham Bansal 	emit_blx_r(ARM_IP, ctx);
518ddecdfceSMircea Gherzan 
51979e3445bSJohan Almbladh 	/* Restore caller-saved registers from stack */
52079e3445bSJohan Almbladh 	emit(ARM_POP(CALLER_MASK & ~exclude_mask), ctx);
52179e3445bSJohan Almbladh 
52239c13c20SShubham Bansal 	/* Save return value */
523ddecdfceSMircea Gherzan 	if (rd != ARM_R0)
524ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(rd, ARM_R0), ctx);
52539c13c20SShubham Bansal 
52639c13c20SShubham Bansal 	/* Restore ARM_R0 and ARM_R1 */
52739c13c20SShubham Bansal 	if (rn != ARM_R1)
52839c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
52939c13c20SShubham Bansal 	if (rm != ARM_R0)
53039c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
531ddecdfceSMircea Gherzan }
532ddecdfceSMircea Gherzan 
53347b9c3bfSRussell King /* Is the translated BPF register on stack? */
is_stacked(s8 reg)53447b9c3bfSRussell King static bool is_stacked(s8 reg)
535ddecdfceSMircea Gherzan {
53647b9c3bfSRussell King 	return reg < 0;
537ddecdfceSMircea Gherzan }
538ddecdfceSMircea Gherzan 
5397a987025SRussell King /* If a BPF register is on the stack (stk is true), load it to the
5407a987025SRussell King  * supplied temporary register and return the temporary register
5417a987025SRussell King  * for subsequent operations, otherwise just use the CPU register.
5427a987025SRussell King  */
arm_bpf_get_reg32(s8 reg,s8 tmp,struct jit_ctx * ctx)5437a987025SRussell King static s8 arm_bpf_get_reg32(s8 reg, s8 tmp, struct jit_ctx *ctx)
5447a987025SRussell King {
5457a987025SRussell King 	if (is_stacked(reg)) {
54696cced4eSRussell King 		emit(ARM_LDR_I(tmp, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
5477a987025SRussell King 		reg = tmp;
5487a987025SRussell King 	}
5497a987025SRussell King 	return reg;
5507a987025SRussell King }
5517a987025SRussell King 
arm_bpf_get_reg64(const s8 * reg,const s8 * tmp,struct jit_ctx * ctx)552a6eccac5SRussell King static const s8 *arm_bpf_get_reg64(const s8 *reg, const s8 *tmp,
553a6eccac5SRussell King 				   struct jit_ctx *ctx)
554a6eccac5SRussell King {
555a6eccac5SRussell King 	if (is_stacked(reg[1])) {
5568c9602d3SRussell King 		if (__LINUX_ARM_ARCH__ >= 6 ||
5578c9602d3SRussell King 		    ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
5588c9602d3SRussell King 			emit(ARM_LDRD_I(tmp[1], ARM_FP,
5598c9602d3SRussell King 					EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5608c9602d3SRussell King 		} else {
5618c9602d3SRussell King 			emit(ARM_LDR_I(tmp[1], ARM_FP,
5628c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5638c9602d3SRussell King 			emit(ARM_LDR_I(tmp[0], ARM_FP,
5648c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
5658c9602d3SRussell King 		}
566a6eccac5SRussell King 		reg = tmp;
567a6eccac5SRussell King 	}
568a6eccac5SRussell King 	return reg;
569a6eccac5SRussell King }
570a6eccac5SRussell King 
5717a987025SRussell King /* If a BPF register is on the stack (stk is true), save the register
5727a987025SRussell King  * back to the stack.  If the source register is not the same, then
5737a987025SRussell King  * move it into the correct register.
5747a987025SRussell King  */
arm_bpf_put_reg32(s8 reg,s8 src,struct jit_ctx * ctx)5757a987025SRussell King static void arm_bpf_put_reg32(s8 reg, s8 src, struct jit_ctx *ctx)
5767a987025SRussell King {
5777a987025SRussell King 	if (is_stacked(reg))
57896cced4eSRussell King 		emit(ARM_STR_I(src, ARM_FP, EBPF_SCRATCH_TO_ARM_FP(reg)), ctx);
5797a987025SRussell King 	else if (reg != src)
5807a987025SRussell King 		emit(ARM_MOV_R(reg, src), ctx);
5817a987025SRussell King }
5827a987025SRussell King 
arm_bpf_put_reg64(const s8 * reg,const s8 * src,struct jit_ctx * ctx)583a6eccac5SRussell King static void arm_bpf_put_reg64(const s8 *reg, const s8 *src,
584a6eccac5SRussell King 			      struct jit_ctx *ctx)
585a6eccac5SRussell King {
586a6eccac5SRussell King 	if (is_stacked(reg[1])) {
5878c9602d3SRussell King 		if (__LINUX_ARM_ARCH__ >= 6 ||
5888c9602d3SRussell King 		    ctx->cpu_architecture >= CPU_ARCH_ARMv5TE) {
5898c9602d3SRussell King 			emit(ARM_STRD_I(src[1], ARM_FP,
5908c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5918c9602d3SRussell King 		} else {
5928c9602d3SRussell King 			emit(ARM_STR_I(src[1], ARM_FP,
5938c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[1])), ctx);
5948c9602d3SRussell King 			emit(ARM_STR_I(src[0], ARM_FP,
5958c9602d3SRussell King 				       EBPF_SCRATCH_TO_ARM_FP(reg[0])), ctx);
5968c9602d3SRussell King 		}
597a6eccac5SRussell King 	} else {
598a6eccac5SRussell King 		if (reg[1] != src[1])
599a6eccac5SRussell King 			emit(ARM_MOV_R(reg[1], src[1]), ctx);
600a6eccac5SRussell King 		if (reg[0] != src[0])
601a6eccac5SRussell King 			emit(ARM_MOV_R(reg[0], src[0]), ctx);
602a6eccac5SRussell King 	}
603a6eccac5SRussell King }
604a6eccac5SRussell King 
emit_a32_mov_i(const s8 dst,const u32 val,struct jit_ctx * ctx)6051c35ba12SRussell King static inline void emit_a32_mov_i(const s8 dst, const u32 val,
60647b9c3bfSRussell King 				  struct jit_ctx *ctx)
607ddecdfceSMircea Gherzan {
6081c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
609ddecdfceSMircea Gherzan 
61047b9c3bfSRussell King 	if (is_stacked(dst)) {
61139c13c20SShubham Bansal 		emit_mov_i(tmp[1], val, ctx);
6127a987025SRussell King 		arm_bpf_put_reg32(dst, tmp[1], ctx);
61339c13c20SShubham Bansal 	} else {
61439c13c20SShubham Bansal 		emit_mov_i(dst, val, ctx);
61539c13c20SShubham Bansal 	}
61639c13c20SShubham Bansal }
61734805931SDaniel Borkmann 
emit_a32_mov_i64(const s8 dst[],u64 val,struct jit_ctx * ctx)618f9ff5018SRussell King static void emit_a32_mov_i64(const s8 dst[], u64 val, struct jit_ctx *ctx)
619f9ff5018SRussell King {
620f9ff5018SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
621f9ff5018SRussell King 	const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
622f9ff5018SRussell King 
623f9ff5018SRussell King 	emit_mov_i(rd[1], (u32)val, ctx);
624f9ff5018SRussell King 	emit_mov_i(rd[0], val >> 32, ctx);
625f9ff5018SRussell King 
626f9ff5018SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
627f9ff5018SRussell King }
628f9ff5018SRussell King 
62939c13c20SShubham Bansal /* Sign extended move */
emit_a32_mov_se_i64(const bool is64,const s8 dst[],const u32 val,struct jit_ctx * ctx)630f9ff5018SRussell King static inline void emit_a32_mov_se_i64(const bool is64, const s8 dst[],
63147b9c3bfSRussell King 				       const u32 val, struct jit_ctx *ctx) {
632077513b8SRussell King 	u64 val64 = val;
633ddecdfceSMircea Gherzan 
63439c13c20SShubham Bansal 	if (is64 && (val & (1<<31)))
635077513b8SRussell King 		val64 |= 0xffffffff00000000ULL;
636077513b8SRussell King 	emit_a32_mov_i64(dst, val64, ctx);
63739c13c20SShubham Bansal }
63839c13c20SShubham Bansal 
emit_a32_add_r(const u8 dst,const u8 src,const bool is64,const bool hi,struct jit_ctx * ctx)63939c13c20SShubham Bansal static inline void emit_a32_add_r(const u8 dst, const u8 src,
64039c13c20SShubham Bansal 			      const bool is64, const bool hi,
64139c13c20SShubham Bansal 			      struct jit_ctx *ctx) {
64239c13c20SShubham Bansal 	/* 64 bit :
64339c13c20SShubham Bansal 	 *	adds dst_lo, dst_lo, src_lo
64439c13c20SShubham Bansal 	 *	adc dst_hi, dst_hi, src_hi
64539c13c20SShubham Bansal 	 * 32 bit :
64639c13c20SShubham Bansal 	 *	add dst_lo, dst_lo, src_lo
64739c13c20SShubham Bansal 	 */
64839c13c20SShubham Bansal 	if (!hi && is64)
64939c13c20SShubham Bansal 		emit(ARM_ADDS_R(dst, dst, src), ctx);
65039c13c20SShubham Bansal 	else if (hi && is64)
65139c13c20SShubham Bansal 		emit(ARM_ADC_R(dst, dst, src), ctx);
65239c13c20SShubham Bansal 	else
65339c13c20SShubham Bansal 		emit(ARM_ADD_R(dst, dst, src), ctx);
65439c13c20SShubham Bansal }
65539c13c20SShubham Bansal 
emit_a32_sub_r(const u8 dst,const u8 src,const bool is64,const bool hi,struct jit_ctx * ctx)65639c13c20SShubham Bansal static inline void emit_a32_sub_r(const u8 dst, const u8 src,
65739c13c20SShubham Bansal 				  const bool is64, const bool hi,
65839c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
65939c13c20SShubham Bansal 	/* 64 bit :
66039c13c20SShubham Bansal 	 *	subs dst_lo, dst_lo, src_lo
66139c13c20SShubham Bansal 	 *	sbc dst_hi, dst_hi, src_hi
66239c13c20SShubham Bansal 	 * 32 bit :
66339c13c20SShubham Bansal 	 *	sub dst_lo, dst_lo, src_lo
66439c13c20SShubham Bansal 	 */
66539c13c20SShubham Bansal 	if (!hi && is64)
66639c13c20SShubham Bansal 		emit(ARM_SUBS_R(dst, dst, src), ctx);
66739c13c20SShubham Bansal 	else if (hi && is64)
66839c13c20SShubham Bansal 		emit(ARM_SBC_R(dst, dst, src), ctx);
66939c13c20SShubham Bansal 	else
67039c13c20SShubham Bansal 		emit(ARM_SUB_R(dst, dst, src), ctx);
67139c13c20SShubham Bansal }
67239c13c20SShubham Bansal 
emit_alu_r(const u8 dst,const u8 src,const bool is64,const bool hi,const u8 op,struct jit_ctx * ctx)67339c13c20SShubham Bansal static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
67439c13c20SShubham Bansal 			      const bool hi, const u8 op, struct jit_ctx *ctx){
67539c13c20SShubham Bansal 	switch (BPF_OP(op)) {
67639c13c20SShubham Bansal 	/* dst = dst + src */
67739c13c20SShubham Bansal 	case BPF_ADD:
67839c13c20SShubham Bansal 		emit_a32_add_r(dst, src, is64, hi, ctx);
67939c13c20SShubham Bansal 		break;
68039c13c20SShubham Bansal 	/* dst = dst - src */
68139c13c20SShubham Bansal 	case BPF_SUB:
68239c13c20SShubham Bansal 		emit_a32_sub_r(dst, src, is64, hi, ctx);
68339c13c20SShubham Bansal 		break;
68439c13c20SShubham Bansal 	/* dst = dst | src */
68539c13c20SShubham Bansal 	case BPF_OR:
68639c13c20SShubham Bansal 		emit(ARM_ORR_R(dst, dst, src), ctx);
68739c13c20SShubham Bansal 		break;
68839c13c20SShubham Bansal 	/* dst = dst & src */
68939c13c20SShubham Bansal 	case BPF_AND:
69039c13c20SShubham Bansal 		emit(ARM_AND_R(dst, dst, src), ctx);
69139c13c20SShubham Bansal 		break;
69239c13c20SShubham Bansal 	/* dst = dst ^ src */
69339c13c20SShubham Bansal 	case BPF_XOR:
69439c13c20SShubham Bansal 		emit(ARM_EOR_R(dst, dst, src), ctx);
69539c13c20SShubham Bansal 		break;
69639c13c20SShubham Bansal 	/* dst = dst * src */
69739c13c20SShubham Bansal 	case BPF_MUL:
69839c13c20SShubham Bansal 		emit(ARM_MUL(dst, dst, src), ctx);
69939c13c20SShubham Bansal 		break;
70039c13c20SShubham Bansal 	/* dst = dst << src */
70139c13c20SShubham Bansal 	case BPF_LSH:
70239c13c20SShubham Bansal 		emit(ARM_LSL_R(dst, dst, src), ctx);
70339c13c20SShubham Bansal 		break;
70439c13c20SShubham Bansal 	/* dst = dst >> src */
70539c13c20SShubham Bansal 	case BPF_RSH:
70639c13c20SShubham Bansal 		emit(ARM_LSR_R(dst, dst, src), ctx);
70739c13c20SShubham Bansal 		break;
70839c13c20SShubham Bansal 	/* dst = dst >> src (signed)*/
70939c13c20SShubham Bansal 	case BPF_ARSH:
71039c13c20SShubham Bansal 		emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
71139c13c20SShubham Bansal 		break;
71239c13c20SShubham Bansal 	}
71339c13c20SShubham Bansal }
71439c13c20SShubham Bansal 
71539c13c20SShubham Bansal /* ALU operation (64 bit) */
emit_a32_alu_r64(const bool is64,const s8 dst[],const s8 src[],struct jit_ctx * ctx,const u8 op)7161c35ba12SRussell King static inline void emit_a32_alu_r64(const bool is64, const s8 dst[],
71747b9c3bfSRussell King 				  const s8 src[], struct jit_ctx *ctx,
71839c13c20SShubham Bansal 				  const u8 op) {
719b18bea2aSRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
720b18bea2aSRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
721b18bea2aSRussell King 	const s8 *rd;
722b18bea2aSRussell King 
723b18bea2aSRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
724b18bea2aSRussell King 	if (is64) {
725b18bea2aSRussell King 		const s8 *rs;
726b18bea2aSRussell King 
727b18bea2aSRussell King 		rs = arm_bpf_get_reg64(src, tmp2, ctx);
728b18bea2aSRussell King 
729b18bea2aSRussell King 		/* ALU operation */
730b18bea2aSRussell King 		emit_alu_r(rd[1], rs[1], true, false, op, ctx);
731b18bea2aSRussell King 		emit_alu_r(rd[0], rs[0], true, true, op, ctx);
732b18bea2aSRussell King 	} else {
733b18bea2aSRussell King 		s8 rs;
734b18bea2aSRussell King 
735b18bea2aSRussell King 		rs = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
736b18bea2aSRussell King 
737b18bea2aSRussell King 		/* ALU operation */
738b18bea2aSRussell King 		emit_alu_r(rd[1], rs, true, false, op, ctx);
739163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
740b18bea2aSRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
741b18bea2aSRussell King 	}
742b18bea2aSRussell King 
743b18bea2aSRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
74439c13c20SShubham Bansal }
74539c13c20SShubham Bansal 
7467a987025SRussell King /* dst = src (4 bytes)*/
emit_a32_mov_r(const s8 dst,const s8 src,struct jit_ctx * ctx)7471c35ba12SRussell King static inline void emit_a32_mov_r(const s8 dst, const s8 src,
74839c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
7491c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7507a987025SRussell King 	s8 rt;
75139c13c20SShubham Bansal 
7527a987025SRussell King 	rt = arm_bpf_get_reg32(src, tmp[0], ctx);
7537a987025SRussell King 	arm_bpf_put_reg32(dst, rt, ctx);
75439c13c20SShubham Bansal }
75539c13c20SShubham Bansal 
75639c13c20SShubham Bansal /* dst = src */
emit_a32_mov_r64(const bool is64,const s8 dst[],const s8 src[],struct jit_ctx * ctx)7571c35ba12SRussell King static inline void emit_a32_mov_r64(const bool is64, const s8 dst[],
75847b9c3bfSRussell King 				  const s8 src[],
75947b9c3bfSRussell King 				  struct jit_ctx *ctx) {
7608c9602d3SRussell King 	if (!is64) {
76147b9c3bfSRussell King 		emit_a32_mov_r(dst_lo, src_lo, ctx);
762163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
76339c13c20SShubham Bansal 			/* Zero out high 4 bytes */
76447b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
7658c9602d3SRussell King 	} else if (__LINUX_ARM_ARCH__ < 6 &&
7668c9602d3SRussell King 		   ctx->cpu_architecture < CPU_ARCH_ARMv5TE) {
7678c9602d3SRussell King 		/* complete 8 byte move */
7688c9602d3SRussell King 		emit_a32_mov_r(dst_lo, src_lo, ctx);
7698c9602d3SRussell King 		emit_a32_mov_r(dst_hi, src_hi, ctx);
7708c9602d3SRussell King 	} else if (is_stacked(src_lo) && is_stacked(dst_lo)) {
7718c9602d3SRussell King 		const u8 *tmp = bpf2a32[TMP_REG_1];
7728c9602d3SRussell King 
7738c9602d3SRussell King 		emit(ARM_LDRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
7748c9602d3SRussell King 		emit(ARM_STRD_I(tmp[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
7758c9602d3SRussell King 	} else if (is_stacked(src_lo)) {
7768c9602d3SRussell King 		emit(ARM_LDRD_I(dst[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(src_lo)), ctx);
7778c9602d3SRussell King 	} else if (is_stacked(dst_lo)) {
7788c9602d3SRussell King 		emit(ARM_STRD_I(src[1], ARM_FP, EBPF_SCRATCH_TO_ARM_FP(dst_lo)), ctx);
7798c9602d3SRussell King 	} else {
7808c9602d3SRussell King 		emit(ARM_MOV_R(dst[0], src[0]), ctx);
7818c9602d3SRussell King 		emit(ARM_MOV_R(dst[1], src[1]), ctx);
78239c13c20SShubham Bansal 	}
78339c13c20SShubham Bansal }
78439c13c20SShubham Bansal 
78539c13c20SShubham Bansal /* Shift operations */
emit_a32_alu_i(const s8 dst,const u32 val,struct jit_ctx * ctx,const u8 op)78647b9c3bfSRussell King static inline void emit_a32_alu_i(const s8 dst, const u32 val,
78739c13c20SShubham Bansal 				struct jit_ctx *ctx, const u8 op) {
7881c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
7897a987025SRussell King 	s8 rd;
79039c13c20SShubham Bansal 
7917a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[0], ctx);
79239c13c20SShubham Bansal 
79339c13c20SShubham Bansal 	/* Do shift operation */
79439c13c20SShubham Bansal 	switch (op) {
79539c13c20SShubham Bansal 	case BPF_LSH:
79639c13c20SShubham Bansal 		emit(ARM_LSL_I(rd, rd, val), ctx);
79739c13c20SShubham Bansal 		break;
79839c13c20SShubham Bansal 	case BPF_RSH:
79939c13c20SShubham Bansal 		emit(ARM_LSR_I(rd, rd, val), ctx);
80039c13c20SShubham Bansal 		break;
801c648c9c7SLuke Nelson 	case BPF_ARSH:
802c648c9c7SLuke Nelson 		emit(ARM_ASR_I(rd, rd, val), ctx);
803c648c9c7SLuke Nelson 		break;
80439c13c20SShubham Bansal 	case BPF_NEG:
80539c13c20SShubham Bansal 		emit(ARM_RSB_I(rd, rd, val), ctx);
80639c13c20SShubham Bansal 		break;
80739c13c20SShubham Bansal 	}
80839c13c20SShubham Bansal 
8097a987025SRussell King 	arm_bpf_put_reg32(dst, rd, ctx);
81039c13c20SShubham Bansal }
81139c13c20SShubham Bansal 
81239c13c20SShubham Bansal /* dst = ~dst (64 bit) */
emit_a32_neg64(const s8 dst[],struct jit_ctx * ctx)81347b9c3bfSRussell King static inline void emit_a32_neg64(const s8 dst[],
81439c13c20SShubham Bansal 				struct jit_ctx *ctx){
8151c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
816a6eccac5SRussell King 	const s8 *rd;
81739c13c20SShubham Bansal 
81839c13c20SShubham Bansal 	/* Setup Operand */
819a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
82039c13c20SShubham Bansal 
82139c13c20SShubham Bansal 	/* Do Negate Operation */
822a6eccac5SRussell King 	emit(ARM_RSBS_I(rd[1], rd[1], 0), ctx);
823a6eccac5SRussell King 	emit(ARM_RSC_I(rd[0], rd[0], 0), ctx);
82439c13c20SShubham Bansal 
825a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
82639c13c20SShubham Bansal }
82739c13c20SShubham Bansal 
82839c13c20SShubham Bansal /* dst = dst << src */
emit_a32_lsh_r64(const s8 dst[],const s8 src[],struct jit_ctx * ctx)82947b9c3bfSRussell King static inline void emit_a32_lsh_r64(const s8 dst[], const s8 src[],
83047b9c3bfSRussell King 				    struct jit_ctx *ctx) {
8311c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8321c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
833a6eccac5SRussell King 	const s8 *rd;
834a6eccac5SRussell King 	s8 rt;
83539c13c20SShubham Bansal 
83639c13c20SShubham Bansal 	/* Setup Operands */
8377a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
838a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
83939c13c20SShubham Bansal 
84039c13c20SShubham Bansal 	/* Do LSH operation */
84139c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
84239c13c20SShubham Bansal 	emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
843a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[0], SRTYPE_ASL, rt), ctx);
844a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[1], SRTYPE_ASL, ARM_IP), ctx);
845a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd[1], SRTYPE_LSR, tmp2[0]), ctx);
846a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_ASL, rt), ctx);
84739c13c20SShubham Bansal 
8487a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
8497a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
85039c13c20SShubham Bansal }
85139c13c20SShubham Bansal 
85239c13c20SShubham Bansal /* dst = dst >> src (signed)*/
emit_a32_arsh_r64(const s8 dst[],const s8 src[],struct jit_ctx * ctx)85347b9c3bfSRussell King static inline void emit_a32_arsh_r64(const s8 dst[], const s8 src[],
85447b9c3bfSRussell King 				     struct jit_ctx *ctx) {
8551c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8561c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
857a6eccac5SRussell King 	const s8 *rd;
858a6eccac5SRussell King 	s8 rt;
85939c13c20SShubham Bansal 
8607a987025SRussell King 	/* Setup Operands */
8617a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
862a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
86339c13c20SShubham Bansal 
86439c13c20SShubham Bansal 	/* Do the ARSH operation */
86539c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
86639c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
867a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
868a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
869cf48db69SLuke Nelson 	_emit(ARM_COND_PL,
870cf48db69SLuke Nelson 	      ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASR, tmp2[0]), ctx);
871a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_ASR, rt), ctx);
8727a987025SRussell King 
8737a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
8747a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
87539c13c20SShubham Bansal }
87639c13c20SShubham Bansal 
87739c13c20SShubham Bansal /* dst = dst >> src */
emit_a32_rsh_r64(const s8 dst[],const s8 src[],struct jit_ctx * ctx)87847b9c3bfSRussell King static inline void emit_a32_rsh_r64(const s8 dst[], const s8 src[],
87947b9c3bfSRussell King 				    struct jit_ctx *ctx) {
8801c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
8811c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
882a6eccac5SRussell King 	const s8 *rd;
883a6eccac5SRussell King 	s8 rt;
88439c13c20SShubham Bansal 
8857a987025SRussell King 	/* Setup Operands */
8867a987025SRussell King 	rt = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
887a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
88839c13c20SShubham Bansal 
88968565a1aSWang YanQing 	/* Do RSH operation */
89039c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
89139c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
892a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_LR, rd[1], SRTYPE_LSR, rt), ctx);
893a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_ASL, ARM_IP), ctx);
894a6eccac5SRussell King 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd[0], SRTYPE_LSR, tmp2[0]), ctx);
895a6eccac5SRussell King 	emit(ARM_MOV_SR(ARM_IP, rd[0], SRTYPE_LSR, rt), ctx);
8967a987025SRussell King 
8977a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_LR, ctx);
8987a987025SRussell King 	arm_bpf_put_reg32(dst_hi, ARM_IP, ctx);
89939c13c20SShubham Bansal }
90039c13c20SShubham Bansal 
90139c13c20SShubham Bansal /* dst = dst << val */
emit_a32_lsh_i64(const s8 dst[],const u32 val,struct jit_ctx * ctx)90247b9c3bfSRussell King static inline void emit_a32_lsh_i64(const s8 dst[],
90339c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx){
9041c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9051c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
906a6eccac5SRussell King 	const s8 *rd;
90739c13c20SShubham Bansal 
9087a987025SRussell King 	/* Setup operands */
909a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
91039c13c20SShubham Bansal 
91139c13c20SShubham Bansal 	/* Do LSH operation */
91239c13c20SShubham Bansal 	if (val < 32) {
913a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[0], rd[0], SRTYPE_ASL, val), ctx);
914a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[0], tmp2[0], rd[1], SRTYPE_LSR, 32 - val), ctx);
915a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[1], SRTYPE_ASL, val), ctx);
91639c13c20SShubham Bansal 	} else {
91739c13c20SShubham Bansal 		if (val == 32)
918a6eccac5SRussell King 			emit(ARM_MOV_R(rd[0], rd[1]), ctx);
91939c13c20SShubham Bansal 		else
920a6eccac5SRussell King 			emit(ARM_MOV_SI(rd[0], rd[1], SRTYPE_ASL, val - 32), ctx);
921a6eccac5SRussell King 		emit(ARM_EOR_R(rd[1], rd[1], rd[1]), ctx);
92239c13c20SShubham Bansal 	}
92339c13c20SShubham Bansal 
924a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
92539c13c20SShubham Bansal }
92639c13c20SShubham Bansal 
92739c13c20SShubham Bansal /* dst = dst >> val */
emit_a32_rsh_i64(const s8 dst[],const u32 val,struct jit_ctx * ctx)92847b9c3bfSRussell King static inline void emit_a32_rsh_i64(const s8 dst[],
92939c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx) {
9301c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9311c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
932a6eccac5SRussell King 	const s8 *rd;
93339c13c20SShubham Bansal 
9347a987025SRussell King 	/* Setup operands */
935a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
93639c13c20SShubham Bansal 
93739c13c20SShubham Bansal 	/* Do LSR operation */
938bb9562cfSLuke Nelson 	if (val == 0) {
939bb9562cfSLuke Nelson 		/* An immediate value of 0 encodes a shift amount of 32
940bb9562cfSLuke Nelson 		 * for LSR. To shift by 0, don't do anything.
941bb9562cfSLuke Nelson 		 */
942bb9562cfSLuke Nelson 	} else if (val < 32) {
943a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
944a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
945a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_LSR, val), ctx);
94639c13c20SShubham Bansal 	} else if (val == 32) {
947a6eccac5SRussell King 		emit(ARM_MOV_R(rd[1], rd[0]), ctx);
948a6eccac5SRussell King 		emit(ARM_MOV_I(rd[0], 0), ctx);
94939c13c20SShubham Bansal 	} else {
950a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_LSR, val - 32), ctx);
951a6eccac5SRussell King 		emit(ARM_MOV_I(rd[0], 0), ctx);
95239c13c20SShubham Bansal 	}
95339c13c20SShubham Bansal 
954a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
95539c13c20SShubham Bansal }
95639c13c20SShubham Bansal 
95739c13c20SShubham Bansal /* dst = dst >> val (signed) */
emit_a32_arsh_i64(const s8 dst[],const u32 val,struct jit_ctx * ctx)95847b9c3bfSRussell King static inline void emit_a32_arsh_i64(const s8 dst[],
95939c13c20SShubham Bansal 				     const u32 val, struct jit_ctx *ctx){
9601c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9611c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
962a6eccac5SRussell King 	const s8 *rd;
96339c13c20SShubham Bansal 
9647a987025SRussell King 	/* Setup operands */
965a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
96639c13c20SShubham Bansal 
96739c13c20SShubham Bansal 	/* Do ARSH operation */
968bb9562cfSLuke Nelson 	if (val == 0) {
969bb9562cfSLuke Nelson 		/* An immediate value of 0 encodes a shift amount of 32
970bb9562cfSLuke Nelson 		 * for ASR. To shift by 0, don't do anything.
971bb9562cfSLuke Nelson 		 */
972bb9562cfSLuke Nelson 	} else if (val < 32) {
973a6eccac5SRussell King 		emit(ARM_MOV_SI(tmp2[1], rd[1], SRTYPE_LSR, val), ctx);
974a6eccac5SRussell King 		emit(ARM_ORR_SI(rd[1], tmp2[1], rd[0], SRTYPE_ASL, 32 - val), ctx);
975a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, val), ctx);
97639c13c20SShubham Bansal 	} else if (val == 32) {
977a6eccac5SRussell King 		emit(ARM_MOV_R(rd[1], rd[0]), ctx);
978a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
97939c13c20SShubham Bansal 	} else {
980a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[1], rd[0], SRTYPE_ASR, val - 32), ctx);
981a6eccac5SRussell King 		emit(ARM_MOV_SI(rd[0], rd[0], SRTYPE_ASR, 31), ctx);
98239c13c20SShubham Bansal 	}
98339c13c20SShubham Bansal 
984a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
98539c13c20SShubham Bansal }
98639c13c20SShubham Bansal 
emit_a32_mul_r64(const s8 dst[],const s8 src[],struct jit_ctx * ctx)98747b9c3bfSRussell King static inline void emit_a32_mul_r64(const s8 dst[], const s8 src[],
98847b9c3bfSRussell King 				    struct jit_ctx *ctx) {
9891c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
9901c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
991a6eccac5SRussell King 	const s8 *rd, *rt;
99239c13c20SShubham Bansal 
9937a987025SRussell King 	/* Setup operands for multiplication */
994a6eccac5SRussell King 	rd = arm_bpf_get_reg64(dst, tmp, ctx);
995a6eccac5SRussell King 	rt = arm_bpf_get_reg64(src, tmp2, ctx);
99639c13c20SShubham Bansal 
99739c13c20SShubham Bansal 	/* Do Multiplication */
998a6eccac5SRussell King 	emit(ARM_MUL(ARM_IP, rd[1], rt[0]), ctx);
999a6eccac5SRussell King 	emit(ARM_MUL(ARM_LR, rd[0], rt[1]), ctx);
100039c13c20SShubham Bansal 	emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
100139c13c20SShubham Bansal 
1002a6eccac5SRussell King 	emit(ARM_UMULL(ARM_IP, rd[0], rd[1], rt[1]), ctx);
1003a6eccac5SRussell King 	emit(ARM_ADD_R(rd[0], ARM_LR, rd[0]), ctx);
10047a987025SRussell King 
10057a987025SRussell King 	arm_bpf_put_reg32(dst_lo, ARM_IP, ctx);
1006a6eccac5SRussell King 	arm_bpf_put_reg32(dst_hi, rd[0], ctx);
100739c13c20SShubham Bansal }
100839c13c20SShubham Bansal 
is_ldst_imm(s16 off,const u8 size)10094178417cSLuke Nelson static bool is_ldst_imm(s16 off, const u8 size)
10104178417cSLuke Nelson {
10114178417cSLuke Nelson 	s16 off_max = 0;
10124178417cSLuke Nelson 
10134178417cSLuke Nelson 	switch (size) {
10144178417cSLuke Nelson 	case BPF_B:
10154178417cSLuke Nelson 	case BPF_W:
10164178417cSLuke Nelson 		off_max = 0xfff;
10174178417cSLuke Nelson 		break;
10184178417cSLuke Nelson 	case BPF_H:
10194178417cSLuke Nelson 		off_max = 0xff;
10204178417cSLuke Nelson 		break;
10214178417cSLuke Nelson 	case BPF_DW:
10224178417cSLuke Nelson 		/* Need to make sure off+4 does not overflow. */
10234178417cSLuke Nelson 		off_max = 0xfff - 4;
10244178417cSLuke Nelson 		break;
10254178417cSLuke Nelson 	}
10264178417cSLuke Nelson 	return -off_max <= off && off <= off_max;
10274178417cSLuke Nelson }
10284178417cSLuke Nelson 
102939c13c20SShubham Bansal /* *(size *)(dst + off) = src */
emit_str_r(const s8 dst,const s8 src[],s16 off,struct jit_ctx * ctx,const u8 sz)1030c5eae692SRussell King static inline void emit_str_r(const s8 dst, const s8 src[],
10314178417cSLuke Nelson 			      s16 off, struct jit_ctx *ctx, const u8 sz){
10321c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
10337a987025SRussell King 	s8 rd;
103439c13c20SShubham Bansal 
10357a987025SRussell King 	rd = arm_bpf_get_reg32(dst, tmp[1], ctx);
1036c5eae692SRussell King 
10374178417cSLuke Nelson 	if (!is_ldst_imm(off, sz)) {
103847b9c3bfSRussell King 		emit_a32_mov_i(tmp[0], off, ctx);
1039c5eae692SRussell King 		emit(ARM_ADD_R(tmp[0], tmp[0], rd), ctx);
104039c13c20SShubham Bansal 		rd = tmp[0];
1041c5eae692SRussell King 		off = 0;
104239c13c20SShubham Bansal 	}
104339c13c20SShubham Bansal 	switch (sz) {
1044c5eae692SRussell King 	case BPF_B:
1045c5eae692SRussell King 		/* Store a Byte */
1046c5eae692SRussell King 		emit(ARM_STRB_I(src_lo, rd, off), ctx);
104739c13c20SShubham Bansal 		break;
104839c13c20SShubham Bansal 	case BPF_H:
104939c13c20SShubham Bansal 		/* Store a HalfWord */
1050c5eae692SRussell King 		emit(ARM_STRH_I(src_lo, rd, off), ctx);
105139c13c20SShubham Bansal 		break;
1052c5eae692SRussell King 	case BPF_W:
1053c5eae692SRussell King 		/* Store a Word */
1054c5eae692SRussell King 		emit(ARM_STR_I(src_lo, rd, off), ctx);
1055c5eae692SRussell King 		break;
1056c5eae692SRussell King 	case BPF_DW:
1057c5eae692SRussell King 		/* Store a Double Word */
1058c5eae692SRussell King 		emit(ARM_STR_I(src_lo, rd, off), ctx);
1059c5eae692SRussell King 		emit(ARM_STR_I(src_hi, rd, off + 4), ctx);
106039c13c20SShubham Bansal 		break;
106139c13c20SShubham Bansal 	}
106239c13c20SShubham Bansal }
106339c13c20SShubham Bansal 
106439c13c20SShubham Bansal /* dst = *(size*)(src + off) */
emit_ldx_r(const s8 dst[],const s8 src,s16 off,struct jit_ctx * ctx,const u8 sz)106547b9c3bfSRussell King static inline void emit_ldx_r(const s8 dst[], const s8 src,
10664178417cSLuke Nelson 			      s16 off, struct jit_ctx *ctx, const u8 sz){
10671c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
106847b9c3bfSRussell King 	const s8 *rd = is_stacked(dst_lo) ? tmp : dst;
10691c35ba12SRussell King 	s8 rm = src;
107039c13c20SShubham Bansal 
10714178417cSLuke Nelson 	if (!is_ldst_imm(off, sz)) {
107247b9c3bfSRussell King 		emit_a32_mov_i(tmp[0], off, ctx);
107339c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
107439c13c20SShubham Bansal 		rm = tmp[0];
1075ec19e02bSRussell King 		off = 0;
1076ec19e02bSRussell King 	} else if (rd[1] == rm) {
1077ec19e02bSRussell King 		emit(ARM_MOV_R(tmp[0], rm), ctx);
1078ec19e02bSRussell King 		rm = tmp[0];
107939c13c20SShubham Bansal 	}
108039c13c20SShubham Bansal 	switch (sz) {
1081ec19e02bSRussell King 	case BPF_B:
1082ec19e02bSRussell King 		/* Load a Byte */
1083ec19e02bSRussell King 		emit(ARM_LDRB_I(rd[1], rm, off), ctx);
1084163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
1085a6eccac5SRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
108639c13c20SShubham Bansal 		break;
108739c13c20SShubham Bansal 	case BPF_H:
108839c13c20SShubham Bansal 		/* Load a HalfWord */
1089ec19e02bSRussell King 		emit(ARM_LDRH_I(rd[1], rm, off), ctx);
1090163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
1091a6eccac5SRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
109239c13c20SShubham Bansal 		break;
1093ec19e02bSRussell King 	case BPF_W:
1094ec19e02bSRussell King 		/* Load a Word */
1095ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
1096163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
1097a6eccac5SRussell King 			emit_a32_mov_i(rd[0], 0, ctx);
1098ec19e02bSRussell King 		break;
1099ec19e02bSRussell King 	case BPF_DW:
1100ec19e02bSRussell King 		/* Load a Double Word */
1101ec19e02bSRussell King 		emit(ARM_LDR_I(rd[1], rm, off), ctx);
1102ec19e02bSRussell King 		emit(ARM_LDR_I(rd[0], rm, off + 4), ctx);
110339c13c20SShubham Bansal 		break;
110439c13c20SShubham Bansal 	}
1105a6eccac5SRussell King 	arm_bpf_put_reg64(dst, rd, ctx);
110639c13c20SShubham Bansal }
110739c13c20SShubham Bansal 
110839c13c20SShubham Bansal /* Arithmatic Operation */
emit_ar_r(const u8 rd,const u8 rt,const u8 rm,const u8 rn,struct jit_ctx * ctx,u8 op,bool is_jmp64)110939c13c20SShubham Bansal static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
1110b85062acSJiong Wang 			     const u8 rn, struct jit_ctx *ctx, u8 op,
1111b85062acSJiong Wang 			     bool is_jmp64) {
111239c13c20SShubham Bansal 	switch (op) {
111339c13c20SShubham Bansal 	case BPF_JSET:
1114b85062acSJiong Wang 		if (is_jmp64) {
111539c13c20SShubham Bansal 			emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
111639c13c20SShubham Bansal 			emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
111739c13c20SShubham Bansal 			emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
1118b85062acSJiong Wang 		} else {
1119b85062acSJiong Wang 			emit(ARM_ANDS_R(ARM_IP, rt, rn), ctx);
1120b85062acSJiong Wang 		}
112139c13c20SShubham Bansal 		break;
112239c13c20SShubham Bansal 	case BPF_JEQ:
112339c13c20SShubham Bansal 	case BPF_JNE:
112439c13c20SShubham Bansal 	case BPF_JGT:
112539c13c20SShubham Bansal 	case BPF_JGE:
112639c13c20SShubham Bansal 	case BPF_JLE:
112739c13c20SShubham Bansal 	case BPF_JLT:
1128b85062acSJiong Wang 		if (is_jmp64) {
112939c13c20SShubham Bansal 			emit(ARM_CMP_R(rd, rm), ctx);
1130b85062acSJiong Wang 			/* Only compare low halve if high halve are equal. */
113139c13c20SShubham Bansal 			_emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
1132b85062acSJiong Wang 		} else {
1133b85062acSJiong Wang 			emit(ARM_CMP_R(rt, rn), ctx);
1134b85062acSJiong Wang 		}
113539c13c20SShubham Bansal 		break;
113639c13c20SShubham Bansal 	case BPF_JSLE:
113739c13c20SShubham Bansal 	case BPF_JSGT:
113839c13c20SShubham Bansal 		emit(ARM_CMP_R(rn, rt), ctx);
1139b85062acSJiong Wang 		if (is_jmp64)
114039c13c20SShubham Bansal 			emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
114139c13c20SShubham Bansal 		break;
114239c13c20SShubham Bansal 	case BPF_JSLT:
114339c13c20SShubham Bansal 	case BPF_JSGE:
114439c13c20SShubham Bansal 		emit(ARM_CMP_R(rt, rn), ctx);
1145b85062acSJiong Wang 		if (is_jmp64)
114639c13c20SShubham Bansal 			emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
114739c13c20SShubham Bansal 		break;
114839c13c20SShubham Bansal 	}
114939c13c20SShubham Bansal }
115039c13c20SShubham Bansal 
115139c13c20SShubham Bansal static int out_offset = -1; /* initialized on the first pass of build_body() */
emit_bpf_tail_call(struct jit_ctx * ctx)115239c13c20SShubham Bansal static int emit_bpf_tail_call(struct jit_ctx *ctx)
115339c13c20SShubham Bansal {
115439c13c20SShubham Bansal 
115539c13c20SShubham Bansal 	/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
11561c35ba12SRussell King 	const s8 *r2 = bpf2a32[BPF_REG_2];
11571c35ba12SRussell King 	const s8 *r3 = bpf2a32[BPF_REG_3];
11581c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
11591c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
11601c35ba12SRussell King 	const s8 *tcc = bpf2a32[TCALL_CNT];
1161a6eccac5SRussell King 	const s8 *tc;
116239c13c20SShubham Bansal 	const int idx0 = ctx->idx;
116339c13c20SShubham Bansal #define cur_offset (ctx->idx - idx0)
1164f4483f2cSRussell King #define jmp_offset (out_offset - (cur_offset) - 2)
1165828e2b90SRussell King 	u32 lo, hi;
1166a6eccac5SRussell King 	s8 r_array, r_index;
1167828e2b90SRussell King 	int off;
116839c13c20SShubham Bansal 
116939c13c20SShubham Bansal 	/* if (index >= array->map.max_entries)
117039c13c20SShubham Bansal 	 *	goto out;
117139c13c20SShubham Bansal 	 */
1172828e2b90SRussell King 	BUILD_BUG_ON(offsetof(struct bpf_array, map.max_entries) >
1173828e2b90SRussell King 		     ARM_INST_LDST__IMM12);
117439c13c20SShubham Bansal 	off = offsetof(struct bpf_array, map.max_entries);
1175b5045229SRussell King 	r_array = arm_bpf_get_reg32(r2[1], tmp2[0], ctx);
1176091f0248SRussell King 	/* index is 32-bit for arrays */
11777a987025SRussell King 	r_index = arm_bpf_get_reg32(r3[1], tmp2[1], ctx);
1178b5045229SRussell King 	/* array->map.max_entries */
1179b5045229SRussell King 	emit(ARM_LDR_I(tmp[1], r_array, off), ctx);
118039c13c20SShubham Bansal 	/* index >= array->map.max_entries */
11817a987025SRussell King 	emit(ARM_CMP_R(r_index, tmp[1]), ctx);
118239c13c20SShubham Bansal 	_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
118339c13c20SShubham Bansal 
1184b5045229SRussell King 	/* tmp2[0] = array, tmp2[1] = index */
1185aaffd2f5SRussell King 
1186ebf7f6f0STiezhu Yang 	/*
1187ebf7f6f0STiezhu Yang 	 * if (tail_call_cnt >= MAX_TAIL_CALL_CNT)
118839c13c20SShubham Bansal 	 *	goto out;
118939c13c20SShubham Bansal 	 * tail_call_cnt++;
119039c13c20SShubham Bansal 	 */
119139c13c20SShubham Bansal 	lo = (u32)MAX_TAIL_CALL_CNT;
119239c13c20SShubham Bansal 	hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
1193a6eccac5SRussell King 	tc = arm_bpf_get_reg64(tcc, tmp, ctx);
1194a6eccac5SRussell King 	emit(ARM_CMP_I(tc[0], hi), ctx);
1195a6eccac5SRussell King 	_emit(ARM_COND_EQ, ARM_CMP_I(tc[1], lo), ctx);
1196ebf7f6f0STiezhu Yang 	_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
1197a6eccac5SRussell King 	emit(ARM_ADDS_I(tc[1], tc[1], 1), ctx);
1198a6eccac5SRussell King 	emit(ARM_ADC_I(tc[0], tc[0], 0), ctx);
1199a6eccac5SRussell King 	arm_bpf_put_reg64(tcc, tmp, ctx);
120039c13c20SShubham Bansal 
120139c13c20SShubham Bansal 	/* prog = array->ptrs[index]
120239c13c20SShubham Bansal 	 * if (prog == NULL)
120339c13c20SShubham Bansal 	 *	goto out;
120439c13c20SShubham Bansal 	 */
1205828e2b90SRussell King 	BUILD_BUG_ON(imm8m(offsetof(struct bpf_array, ptrs)) < 0);
1206828e2b90SRussell King 	off = imm8m(offsetof(struct bpf_array, ptrs));
1207828e2b90SRussell King 	emit(ARM_ADD_I(tmp[1], r_array, off), ctx);
12082b6958efSRussell King 	emit(ARM_LDR_R_SI(tmp[1], tmp[1], r_index, SRTYPE_ASL, 2), ctx);
120939c13c20SShubham Bansal 	emit(ARM_CMP_I(tmp[1], 0), ctx);
121039c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
121139c13c20SShubham Bansal 
121239c13c20SShubham Bansal 	/* goto *(prog->bpf_func + prologue_size); */
1213828e2b90SRussell King 	BUILD_BUG_ON(offsetof(struct bpf_prog, bpf_func) >
1214828e2b90SRussell King 		     ARM_INST_LDST__IMM12);
121539c13c20SShubham Bansal 	off = offsetof(struct bpf_prog, bpf_func);
1216828e2b90SRussell King 	emit(ARM_LDR_I(tmp[1], tmp[1], off), ctx);
121739c13c20SShubham Bansal 	emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
1218e9062481SRussell King 	emit_bx_r(tmp[1], ctx);
121939c13c20SShubham Bansal 
122039c13c20SShubham Bansal 	/* out: */
122139c13c20SShubham Bansal 	if (out_offset == -1)
122239c13c20SShubham Bansal 		out_offset = cur_offset;
122339c13c20SShubham Bansal 	if (cur_offset != out_offset) {
122439c13c20SShubham Bansal 		pr_err_once("tail_call out_offset = %d, expected %d!\n",
122539c13c20SShubham Bansal 			    cur_offset, out_offset);
122639c13c20SShubham Bansal 		return -1;
122739c13c20SShubham Bansal 	}
122839c13c20SShubham Bansal 	return 0;
122939c13c20SShubham Bansal #undef cur_offset
123039c13c20SShubham Bansal #undef jmp_offset
123139c13c20SShubham Bansal }
123239c13c20SShubham Bansal 
123339c13c20SShubham Bansal /* 0xabcd => 0xcdab */
emit_rev16(const u8 rd,const u8 rn,struct jit_ctx * ctx)123439c13c20SShubham Bansal static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
123539c13c20SShubham Bansal {
123639c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
12371c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
123839c13c20SShubham Bansal 
123939c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
124039c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
124139c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
124239c13c20SShubham Bansal 	emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
124339c13c20SShubham Bansal #else /* ARMv6+ */
124439c13c20SShubham Bansal 	emit(ARM_REV16(rd, rn), ctx);
124539c13c20SShubham Bansal #endif
124639c13c20SShubham Bansal }
124739c13c20SShubham Bansal 
124839c13c20SShubham Bansal /* 0xabcdefgh => 0xghefcdab */
emit_rev32(const u8 rd,const u8 rn,struct jit_ctx * ctx)124939c13c20SShubham Bansal static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
125039c13c20SShubham Bansal {
125139c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
12521c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
125339c13c20SShubham Bansal 
125439c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
125539c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
125639c13c20SShubham Bansal 	emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
125739c13c20SShubham Bansal 
125839c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
125939c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
126039c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
126139c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
126239c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
126339c13c20SShubham Bansal 	emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
126439c13c20SShubham Bansal 	emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
126539c13c20SShubham Bansal 
126639c13c20SShubham Bansal #else /* ARMv6+ */
126739c13c20SShubham Bansal 	emit(ARM_REV(rd, rn), ctx);
126839c13c20SShubham Bansal #endif
126939c13c20SShubham Bansal }
127039c13c20SShubham Bansal 
127139c13c20SShubham Bansal // push the scratch stack register on top of the stack
emit_push_r64(const s8 src[],struct jit_ctx * ctx)127296cced4eSRussell King static inline void emit_push_r64(const s8 src[], struct jit_ctx *ctx)
127339c13c20SShubham Bansal {
12741c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
127596cced4eSRussell King 	const s8 *rt;
127639c13c20SShubham Bansal 	u16 reg_set = 0;
127739c13c20SShubham Bansal 
127896cced4eSRussell King 	rt = arm_bpf_get_reg64(src, tmp2, ctx);
127939c13c20SShubham Bansal 
128096cced4eSRussell King 	reg_set = (1 << rt[1]) | (1 << rt[0]);
128139c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
128239c13c20SShubham Bansal }
128339c13c20SShubham Bansal 
build_prologue(struct jit_ctx * ctx)128439c13c20SShubham Bansal static void build_prologue(struct jit_ctx *ctx)
128539c13c20SShubham Bansal {
1286c4533128SRussell King 	const s8 arm_r0 = bpf2a32[BPF_REG_0][1];
1287c4533128SRussell King 	const s8 *bpf_r1 = bpf2a32[BPF_REG_1];
1288c4533128SRussell King 	const s8 *bpf_fp = bpf2a32[BPF_REG_FP];
12891c35ba12SRussell King 	const s8 *tcc = bpf2a32[TCALL_CNT];
129039c13c20SShubham Bansal 
129139c13c20SShubham Bansal 	/* Save callee saved registers. */
129239c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
129302088d9bSRussell King 	u16 reg_set = CALLEE_PUSH_MASK | 1 << ARM_IP | 1 << ARM_PC;
129402088d9bSRussell King 	emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
129539c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
129639c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
129739c13c20SShubham Bansal #else
129802088d9bSRussell King 	emit(ARM_PUSH(CALLEE_PUSH_MASK), ctx);
129902088d9bSRussell King 	emit(ARM_MOV_R(ARM_FP, ARM_SP), ctx);
130039c13c20SShubham Bansal #endif
1301c4533128SRussell King 	/* mov r3, #0 */
1302c4533128SRussell King 	/* sub r2, sp, #SCRATCH_SIZE */
1303c4533128SRussell King 	emit(ARM_MOV_I(bpf_r1[0], 0), ctx);
1304c4533128SRussell King 	emit(ARM_SUB_I(bpf_r1[1], ARM_SP, SCRATCH_SIZE), ctx);
130539c13c20SShubham Bansal 
130639c13c20SShubham Bansal 	ctx->stack_size = imm8m(STACK_SIZE);
130739c13c20SShubham Bansal 
130839c13c20SShubham Bansal 	/* Set up function call stack */
130939c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
131039c13c20SShubham Bansal 
131139c13c20SShubham Bansal 	/* Set up BPF prog stack base register */
1312c4533128SRussell King 	emit_a32_mov_r64(true, bpf_fp, bpf_r1, ctx);
131339c13c20SShubham Bansal 
1314c4533128SRussell King 	/* Initialize Tail Count */
1315c4533128SRussell King 	emit(ARM_MOV_I(bpf_r1[1], 0), ctx);
1316c4533128SRussell King 	emit_a32_mov_r64(true, tcc, bpf_r1, ctx);
131739c13c20SShubham Bansal 
131839c13c20SShubham Bansal 	/* Move BPF_CTX to BPF_R1 */
1319c4533128SRussell King 	emit(ARM_MOV_R(bpf_r1[1], arm_r0), ctx);
1320c4533128SRussell King 
132139c13c20SShubham Bansal 	/* end of prologue */
132239c13c20SShubham Bansal }
132339c13c20SShubham Bansal 
132402088d9bSRussell King /* restore callee saved registers. */
build_epilogue(struct jit_ctx * ctx)132539c13c20SShubham Bansal static void build_epilogue(struct jit_ctx *ctx)
132639c13c20SShubham Bansal {
132739c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
132802088d9bSRussell King 	/* When using frame pointers, some additional registers need to
132902088d9bSRussell King 	 * be loaded. */
133002088d9bSRussell King 	u16 reg_set = CALLEE_POP_MASK | 1 << ARM_SP;
133102088d9bSRussell King 	emit(ARM_SUB_I(ARM_SP, ARM_FP, hweight16(reg_set) * 4), ctx);
133239c13c20SShubham Bansal 	emit(ARM_LDM(ARM_SP, reg_set), ctx);
133339c13c20SShubham Bansal #else
133439c13c20SShubham Bansal 	/* Restore callee saved registers. */
133502088d9bSRussell King 	emit(ARM_MOV_R(ARM_SP, ARM_FP), ctx);
133602088d9bSRussell King 	emit(ARM_POP(CALLEE_POP_MASK), ctx);
133739c13c20SShubham Bansal #endif
133839c13c20SShubham Bansal }
133939c13c20SShubham Bansal 
134039c13c20SShubham Bansal /*
134139c13c20SShubham Bansal  * Convert an eBPF instruction to native instruction, i.e
134239c13c20SShubham Bansal  * JITs an eBPF instruction.
134339c13c20SShubham Bansal  * Returns :
134439c13c20SShubham Bansal  *	0  - Successfully JITed an 8-byte eBPF instruction
134539c13c20SShubham Bansal  *	>0 - Successfully JITed a 16-byte eBPF instruction
134639c13c20SShubham Bansal  *	<0 - Failed to JIT.
134739c13c20SShubham Bansal  */
build_insn(const struct bpf_insn * insn,struct jit_ctx * ctx)134839c13c20SShubham Bansal static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
134939c13c20SShubham Bansal {
135039c13c20SShubham Bansal 	const u8 code = insn->code;
13511c35ba12SRussell King 	const s8 *dst = bpf2a32[insn->dst_reg];
13521c35ba12SRussell King 	const s8 *src = bpf2a32[insn->src_reg];
13531c35ba12SRussell King 	const s8 *tmp = bpf2a32[TMP_REG_1];
13541c35ba12SRussell King 	const s8 *tmp2 = bpf2a32[TMP_REG_2];
135539c13c20SShubham Bansal 	const s16 off = insn->off;
135639c13c20SShubham Bansal 	const s32 imm = insn->imm;
135739c13c20SShubham Bansal 	const int i = insn - ctx->prog->insnsi;
135839c13c20SShubham Bansal 	const bool is64 = BPF_CLASS(code) == BPF_ALU64;
1359a6eccac5SRussell King 	const s8 *rd, *rs;
1360a6eccac5SRussell King 	s8 rd_lo, rt, rm, rn;
136139c13c20SShubham Bansal 	s32 jmp_offset;
136239c13c20SShubham Bansal 
136339c13c20SShubham Bansal #define check_imm(bits, imm) do {				\
13642b589a7eSWang YanQing 	if ((imm) >= (1 << ((bits) - 1)) ||			\
13652b589a7eSWang YanQing 	    (imm) < -(1 << ((bits) - 1))) {			\
136639c13c20SShubham Bansal 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
136739c13c20SShubham Bansal 			i, imm, imm);				\
136839c13c20SShubham Bansal 		return -EINVAL;					\
136939c13c20SShubham Bansal 	}							\
137039c13c20SShubham Bansal } while (0)
137139c13c20SShubham Bansal #define check_imm24(imm) check_imm(24, imm)
1372ddecdfceSMircea Gherzan 
137334805931SDaniel Borkmann 	switch (code) {
137439c13c20SShubham Bansal 	/* ALU operations */
1375ddecdfceSMircea Gherzan 
137639c13c20SShubham Bansal 	/* dst = src */
137739c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_K:
137839c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_X:
137939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_K:
138039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_X:
138139c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
138239c13c20SShubham Bansal 		case BPF_X:
1383163541e6SJiong Wang 			if (imm == 1) {
1384163541e6SJiong Wang 				/* Special mov32 for zext */
1385163541e6SJiong Wang 				emit_a32_mov_i(dst_hi, 0, ctx);
1386163541e6SJiong Wang 				break;
1387163541e6SJiong Wang 			}
138847b9c3bfSRussell King 			emit_a32_mov_r64(is64, dst, src, ctx);
138939c13c20SShubham Bansal 			break;
139039c13c20SShubham Bansal 		case BPF_K:
139139c13c20SShubham Bansal 			/* Sign-extend immediate value to destination reg */
1392f9ff5018SRussell King 			emit_a32_mov_se_i64(is64, dst, imm, ctx);
139339c13c20SShubham Bansal 			break;
1394ddecdfceSMircea Gherzan 		}
1395ddecdfceSMircea Gherzan 		break;
139639c13c20SShubham Bansal 	/* dst = dst + src/imm */
139739c13c20SShubham Bansal 	/* dst = dst - src/imm */
139839c13c20SShubham Bansal 	/* dst = dst | src/imm */
139939c13c20SShubham Bansal 	/* dst = dst & src/imm */
140039c13c20SShubham Bansal 	/* dst = dst ^ src/imm */
140139c13c20SShubham Bansal 	/* dst = dst * src/imm */
140239c13c20SShubham Bansal 	/* dst = dst << src */
140339c13c20SShubham Bansal 	/* dst = dst >> src */
140434805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_K:
140534805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_X:
140634805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_K:
140734805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_X:
140834805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_K:
140934805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_X:
141034805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_K:
141134805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_X:
141239c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_K:
141339c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_X:
141439c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_K:
141539c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_X:
141634805931SDaniel Borkmann 	case BPF_ALU | BPF_LSH | BPF_X:
141734805931SDaniel Borkmann 	case BPF_ALU | BPF_RSH | BPF_X:
141839c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_X:
141939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_K:
142039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_X:
142139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_K:
142239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_X:
142339c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_K:
142439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_X:
142539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_K:
142639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_X:
142739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_K:
142839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_X:
142939c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
143039c13c20SShubham Bansal 		case BPF_X:
143147b9c3bfSRussell King 			emit_a32_alu_r64(is64, dst, src, ctx, BPF_OP(code));
1432ddecdfceSMircea Gherzan 			break;
143339c13c20SShubham Bansal 		case BPF_K:
143439c13c20SShubham Bansal 			/* Move immediate value to the temporary register
143539c13c20SShubham Bansal 			 * and then do the ALU operation on the temporary
143639c13c20SShubham Bansal 			 * register as this will sign-extend the immediate
143739c13c20SShubham Bansal 			 * value into temporary reg and then it would be
143839c13c20SShubham Bansal 			 * safe to do the operation on it.
143939c13c20SShubham Bansal 			 */
1440f9ff5018SRussell King 			emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
144147b9c3bfSRussell King 			emit_a32_alu_r64(is64, dst, tmp2, ctx, BPF_OP(code));
144239c13c20SShubham Bansal 			break;
144339c13c20SShubham Bansal 		}
144439c13c20SShubham Bansal 		break;
144539c13c20SShubham Bansal 	/* dst = dst / src(imm) */
144639c13c20SShubham Bansal 	/* dst = dst % src(imm) */
144739c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_K:
144839c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_X:
144939c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_K:
145039c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_X:
1451a6eccac5SRussell King 		rd_lo = arm_bpf_get_reg32(dst_lo, tmp2[1], ctx);
145239c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
145339c13c20SShubham Bansal 		case BPF_X:
14547a987025SRussell King 			rt = arm_bpf_get_reg32(src_lo, tmp2[0], ctx);
145539c13c20SShubham Bansal 			break;
145639c13c20SShubham Bansal 		case BPF_K:
145739c13c20SShubham Bansal 			rt = tmp2[0];
145847b9c3bfSRussell King 			emit_a32_mov_i(rt, imm, ctx);
145947b9c3bfSRussell King 			break;
146047b9c3bfSRussell King 		default:
146147b9c3bfSRussell King 			rt = src_lo;
146239c13c20SShubham Bansal 			break;
146339c13c20SShubham Bansal 		}
1464a6eccac5SRussell King 		emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code));
1465a6eccac5SRussell King 		arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
1466163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
146747b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
146839c13c20SShubham Bansal 		break;
146939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_K:
147039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_X:
147139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_K:
147239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_X:
147339c13c20SShubham Bansal 		goto notyet;
147439c13c20SShubham Bansal 	/* dst = dst << imm */
1475c648c9c7SLuke Nelson 	/* dst = dst >> imm */
1476c648c9c7SLuke Nelson 	/* dst = dst >> imm (signed) */
147739c13c20SShubham Bansal 	case BPF_ALU | BPF_LSH | BPF_K:
1478c648c9c7SLuke Nelson 	case BPF_ALU | BPF_RSH | BPF_K:
1479c648c9c7SLuke Nelson 	case BPF_ALU | BPF_ARSH | BPF_K:
148039c13c20SShubham Bansal 		if (unlikely(imm > 31))
148139c13c20SShubham Bansal 			return -EINVAL;
148239c13c20SShubham Bansal 		if (imm)
148347b9c3bfSRussell King 			emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
1484163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
148547b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
148639c13c20SShubham Bansal 		break;
148739c13c20SShubham Bansal 	/* dst = dst << imm */
148839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_K:
148939c13c20SShubham Bansal 		if (unlikely(imm > 63))
149039c13c20SShubham Bansal 			return -EINVAL;
149147b9c3bfSRussell King 		emit_a32_lsh_i64(dst, imm, ctx);
149239c13c20SShubham Bansal 		break;
149339c13c20SShubham Bansal 	/* dst = dst >> imm */
149439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_K:
149539c13c20SShubham Bansal 		if (unlikely(imm > 63))
149639c13c20SShubham Bansal 			return -EINVAL;
149747b9c3bfSRussell King 		emit_a32_rsh_i64(dst, imm, ctx);
149839c13c20SShubham Bansal 		break;
149939c13c20SShubham Bansal 	/* dst = dst << src */
150039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_X:
150147b9c3bfSRussell King 		emit_a32_lsh_r64(dst, src, ctx);
150239c13c20SShubham Bansal 		break;
150339c13c20SShubham Bansal 	/* dst = dst >> src */
150439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_X:
150547b9c3bfSRussell King 		emit_a32_rsh_r64(dst, src, ctx);
150639c13c20SShubham Bansal 		break;
150739c13c20SShubham Bansal 	/* dst = dst >> src (signed) */
150839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_X:
150947b9c3bfSRussell King 		emit_a32_arsh_r64(dst, src, ctx);
151039c13c20SShubham Bansal 		break;
151139c13c20SShubham Bansal 	/* dst = dst >> imm (signed) */
151239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_K:
151339c13c20SShubham Bansal 		if (unlikely(imm > 63))
151439c13c20SShubham Bansal 			return -EINVAL;
151547b9c3bfSRussell King 		emit_a32_arsh_i64(dst, imm, ctx);
151639c13c20SShubham Bansal 		break;
151739c13c20SShubham Bansal 	/* dst = ~dst */
151834805931SDaniel Borkmann 	case BPF_ALU | BPF_NEG:
151947b9c3bfSRussell King 		emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
1520163541e6SJiong Wang 		if (!ctx->prog->aux->verifier_zext)
152147b9c3bfSRussell King 			emit_a32_mov_i(dst_hi, 0, ctx);
1522ddecdfceSMircea Gherzan 		break;
152339c13c20SShubham Bansal 	/* dst = ~dst (64 bit) */
152439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_NEG:
152547b9c3bfSRussell King 		emit_a32_neg64(dst, ctx);
1526ddecdfceSMircea Gherzan 		break;
152739c13c20SShubham Bansal 	/* dst = dst * src/imm */
152839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_X:
152939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_K:
153039c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
153139c13c20SShubham Bansal 		case BPF_X:
153247b9c3bfSRussell King 			emit_a32_mul_r64(dst, src, ctx);
1533ddecdfceSMircea Gherzan 			break;
153439c13c20SShubham Bansal 		case BPF_K:
153539c13c20SShubham Bansal 			/* Move immediate value to the temporary register
153639c13c20SShubham Bansal 			 * and then do the multiplication on it as this
153739c13c20SShubham Bansal 			 * will sign-extend the immediate value into temp
153839c13c20SShubham Bansal 			 * reg then it would be safe to do the operation
153939c13c20SShubham Bansal 			 * on it.
15405bf705b4SNicolas Schichan 			 */
1541f9ff5018SRussell King 			emit_a32_mov_se_i64(is64, tmp2, imm, ctx);
154247b9c3bfSRussell King 			emit_a32_mul_r64(dst, tmp2, ctx);
154339c13c20SShubham Bansal 			break;
15445bf705b4SNicolas Schichan 		}
1545ddecdfceSMircea Gherzan 		break;
154639c13c20SShubham Bansal 	/* dst = htole(dst) */
154739c13c20SShubham Bansal 	/* dst = htobe(dst) */
154839c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_LE:
154939c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_BE:
1550a6eccac5SRussell King 		rd = arm_bpf_get_reg64(dst, tmp, ctx);
155139c13c20SShubham Bansal 		if (BPF_SRC(code) == BPF_FROM_LE)
155239c13c20SShubham Bansal 			goto emit_bswap_uxt;
155339c13c20SShubham Bansal 		switch (imm) {
155439c13c20SShubham Bansal 		case 16:
1555a6eccac5SRussell King 			emit_rev16(rd[1], rd[1], ctx);
155639c13c20SShubham Bansal 			goto emit_bswap_uxt;
155739c13c20SShubham Bansal 		case 32:
1558a6eccac5SRussell King 			emit_rev32(rd[1], rd[1], ctx);
155939c13c20SShubham Bansal 			goto emit_bswap_uxt;
156039c13c20SShubham Bansal 		case 64:
1561a6eccac5SRussell King 			emit_rev32(ARM_LR, rd[1], ctx);
1562a6eccac5SRussell King 			emit_rev32(rd[1], rd[0], ctx);
1563a6eccac5SRussell King 			emit(ARM_MOV_R(rd[0], ARM_LR), ctx);
1564bf0098f2SDaniel Borkmann 			break;
156539c13c20SShubham Bansal 		}
156639c13c20SShubham Bansal 		goto exit;
156739c13c20SShubham Bansal emit_bswap_uxt:
156839c13c20SShubham Bansal 		switch (imm) {
156939c13c20SShubham Bansal 		case 16:
157039c13c20SShubham Bansal 			/* zero-extend 16 bits into 64 bits */
157139c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
157247b9c3bfSRussell King 			emit_a32_mov_i(tmp2[1], 0xffff, ctx);
1573a6eccac5SRussell King 			emit(ARM_AND_R(rd[1], rd[1], tmp2[1]), ctx);
157439c13c20SShubham Bansal #else /* ARMv6+ */
1575a6eccac5SRussell King 			emit(ARM_UXTH(rd[1], rd[1]), ctx);
15761447f93fSNicolas Schichan #endif
1577163541e6SJiong Wang 			if (!ctx->prog->aux->verifier_zext)
1578a6eccac5SRussell King 				emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
15791447f93fSNicolas Schichan 			break;
158039c13c20SShubham Bansal 		case 32:
158139c13c20SShubham Bansal 			/* zero-extend 32 bits into 64 bits */
1582163541e6SJiong Wang 			if (!ctx->prog->aux->verifier_zext)
1583a6eccac5SRussell King 				emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
1584ddecdfceSMircea Gherzan 			break;
158539c13c20SShubham Bansal 		case 64:
158639c13c20SShubham Bansal 			/* nop */
158739c13c20SShubham Bansal 			break;
158839c13c20SShubham Bansal 		}
158939c13c20SShubham Bansal exit:
1590a6eccac5SRussell King 		arm_bpf_put_reg64(dst, rd, ctx);
159139c13c20SShubham Bansal 		break;
159239c13c20SShubham Bansal 	/* dst = imm64 */
159339c13c20SShubham Bansal 	case BPF_LD | BPF_IMM | BPF_DW:
159439c13c20SShubham Bansal 	{
1595f9ff5018SRussell King 		u64 val = (u32)imm | (u64)insn[1].imm << 32;
1596303249abSNicolas Schichan 
1597f9ff5018SRussell King 		emit_a32_mov_i64(dst, val, ctx);
159839c13c20SShubham Bansal 
159939c13c20SShubham Bansal 		return 1;
160039c13c20SShubham Bansal 	}
160139c13c20SShubham Bansal 	/* LDX: dst = *(size *)(src + off) */
160239c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_W:
160339c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_H:
160439c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_B:
160539c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_DW:
16067a987025SRussell King 		rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
160747b9c3bfSRussell King 		emit_ldx_r(dst, rn, off, ctx, BPF_SIZE(code));
160839c13c20SShubham Bansal 		break;
1609f5e81d11SDaniel Borkmann 	/* speculation barrier */
1610f5e81d11SDaniel Borkmann 	case BPF_ST | BPF_NOSPEC:
1611f5e81d11SDaniel Borkmann 		break;
161239c13c20SShubham Bansal 	/* ST: *(size *)(dst + off) = imm */
161339c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_W:
161439c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_H:
161539c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_B:
161639c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_DW:
161739c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
161839c13c20SShubham Bansal 		case BPF_DW:
161939c13c20SShubham Bansal 			/* Sign-extend immediate value into temp reg */
1620f9ff5018SRussell King 			emit_a32_mov_se_i64(true, tmp2, imm, ctx);
162139c13c20SShubham Bansal 			break;
162239c13c20SShubham Bansal 		case BPF_W:
162339c13c20SShubham Bansal 		case BPF_H:
162439c13c20SShubham Bansal 		case BPF_B:
162547b9c3bfSRussell King 			emit_a32_mov_i(tmp2[1], imm, ctx);
162639c13c20SShubham Bansal 			break;
162739c13c20SShubham Bansal 		}
1628c5eae692SRussell King 		emit_str_r(dst_lo, tmp2, off, ctx, BPF_SIZE(code));
162939c13c20SShubham Bansal 		break;
163091c960b0SBrendan Jackman 	/* Atomic ops */
163191c960b0SBrendan Jackman 	case BPF_STX | BPF_ATOMIC | BPF_W:
163291c960b0SBrendan Jackman 	case BPF_STX | BPF_ATOMIC | BPF_DW:
163339c13c20SShubham Bansal 		goto notyet;
163439c13c20SShubham Bansal 	/* STX: *(size *)(dst + off) = src */
163539c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_W:
163639c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_H:
163739c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_B:
163839c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_DW:
1639a6eccac5SRussell King 		rs = arm_bpf_get_reg64(src, tmp2, ctx);
1640c5eae692SRussell King 		emit_str_r(dst_lo, rs, off, ctx, BPF_SIZE(code));
164139c13c20SShubham Bansal 		break;
164239c13c20SShubham Bansal 	/* PC += off if dst == src */
164339c13c20SShubham Bansal 	/* PC += off if dst > src */
164439c13c20SShubham Bansal 	/* PC += off if dst >= src */
164539c13c20SShubham Bansal 	/* PC += off if dst < src */
164639c13c20SShubham Bansal 	/* PC += off if dst <= src */
164739c13c20SShubham Bansal 	/* PC += off if dst != src */
164839c13c20SShubham Bansal 	/* PC += off if dst > src (signed) */
164939c13c20SShubham Bansal 	/* PC += off if dst >= src (signed) */
165039c13c20SShubham Bansal 	/* PC += off if dst < src (signed) */
165139c13c20SShubham Bansal 	/* PC += off if dst <= src (signed) */
165239c13c20SShubham Bansal 	/* PC += off if dst & src */
165339c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_X:
165439c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_X:
165539c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_X:
165639c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_X:
165739c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_X:
165839c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_X:
165939c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_X:
166039c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_X:
166139c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_X:
166239c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_X:
166339c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_X:
1664b85062acSJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_X:
1665b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_X:
1666b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_X:
1667b85062acSJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_X:
1668b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_X:
1669b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_X:
1670b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_X:
1671b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_X:
1672b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_X:
1673b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_X:
1674b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_X:
167539c13c20SShubham Bansal 		/* Setup source registers */
16767a987025SRussell King 		rm = arm_bpf_get_reg32(src_hi, tmp2[0], ctx);
16777a987025SRussell King 		rn = arm_bpf_get_reg32(src_lo, tmp2[1], ctx);
167839c13c20SShubham Bansal 		goto go_jmp;
167939c13c20SShubham Bansal 	/* PC += off if dst == imm */
168039c13c20SShubham Bansal 	/* PC += off if dst > imm */
168139c13c20SShubham Bansal 	/* PC += off if dst >= imm */
168239c13c20SShubham Bansal 	/* PC += off if dst < imm */
168339c13c20SShubham Bansal 	/* PC += off if dst <= imm */
168439c13c20SShubham Bansal 	/* PC += off if dst != imm */
168539c13c20SShubham Bansal 	/* PC += off if dst > imm (signed) */
168639c13c20SShubham Bansal 	/* PC += off if dst >= imm (signed) */
168739c13c20SShubham Bansal 	/* PC += off if dst < imm (signed) */
168839c13c20SShubham Bansal 	/* PC += off if dst <= imm (signed) */
168939c13c20SShubham Bansal 	/* PC += off if dst & imm */
169039c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_K:
169139c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_K:
169239c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_K:
169339c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_K:
169439c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_K:
169539c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_K:
169639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_K:
169739c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_K:
169839c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_K:
169939c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_K:
170039c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_K:
1701b85062acSJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_K:
1702b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_K:
1703b85062acSJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_K:
1704b85062acSJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_K:
1705b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_K:
1706b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_K:
1707b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_K:
1708b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_K:
1709b85062acSJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_K:
1710b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_K:
1711b85062acSJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_K:
171239c13c20SShubham Bansal 		if (off == 0)
171339c13c20SShubham Bansal 			break;
171439c13c20SShubham Bansal 		rm = tmp2[0];
171539c13c20SShubham Bansal 		rn = tmp2[1];
171639c13c20SShubham Bansal 		/* Sign-extend immediate value */
1717f9ff5018SRussell King 		emit_a32_mov_se_i64(true, tmp2, imm, ctx);
171839c13c20SShubham Bansal go_jmp:
171939c13c20SShubham Bansal 		/* Setup destination register */
1720a6eccac5SRussell King 		rd = arm_bpf_get_reg64(dst, tmp, ctx);
172139c13c20SShubham Bansal 
172239c13c20SShubham Bansal 		/* Check for the condition */
1723b85062acSJiong Wang 		emit_ar_r(rd[0], rd[1], rm, rn, ctx, BPF_OP(code),
1724b85062acSJiong Wang 			  BPF_CLASS(code) == BPF_JMP);
172539c13c20SShubham Bansal 
172639c13c20SShubham Bansal 		/* Setup JUMP instruction */
172739c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
172839c13c20SShubham Bansal 		switch (BPF_OP(code)) {
172939c13c20SShubham Bansal 		case BPF_JNE:
173039c13c20SShubham Bansal 		case BPF_JSET:
173139c13c20SShubham Bansal 			_emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
173239c13c20SShubham Bansal 			break;
173339c13c20SShubham Bansal 		case BPF_JEQ:
173439c13c20SShubham Bansal 			_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
173539c13c20SShubham Bansal 			break;
173639c13c20SShubham Bansal 		case BPF_JGT:
173739c13c20SShubham Bansal 			_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
173839c13c20SShubham Bansal 			break;
173939c13c20SShubham Bansal 		case BPF_JGE:
174039c13c20SShubham Bansal 			_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
174139c13c20SShubham Bansal 			break;
174239c13c20SShubham Bansal 		case BPF_JSGT:
174339c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
174439c13c20SShubham Bansal 			break;
174539c13c20SShubham Bansal 		case BPF_JSGE:
174639c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
174739c13c20SShubham Bansal 			break;
174839c13c20SShubham Bansal 		case BPF_JLE:
174939c13c20SShubham Bansal 			_emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
175039c13c20SShubham Bansal 			break;
175139c13c20SShubham Bansal 		case BPF_JLT:
175239c13c20SShubham Bansal 			_emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
175339c13c20SShubham Bansal 			break;
175439c13c20SShubham Bansal 		case BPF_JSLT:
175539c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
175639c13c20SShubham Bansal 			break;
175739c13c20SShubham Bansal 		case BPF_JSLE:
175839c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
175939c13c20SShubham Bansal 			break;
176039c13c20SShubham Bansal 		}
176139c13c20SShubham Bansal 		break;
176239c13c20SShubham Bansal 	/* JMP OFF */
176339c13c20SShubham Bansal 	case BPF_JMP | BPF_JA:
176439c13c20SShubham Bansal 	{
176539c13c20SShubham Bansal 		if (off == 0)
176639c13c20SShubham Bansal 			break;
176739c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
176839c13c20SShubham Bansal 		check_imm24(jmp_offset);
176939c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
177039c13c20SShubham Bansal 		break;
177139c13c20SShubham Bansal 	}
177239c13c20SShubham Bansal 	/* tail call */
177339c13c20SShubham Bansal 	case BPF_JMP | BPF_TAIL_CALL:
177439c13c20SShubham Bansal 		if (emit_bpf_tail_call(ctx))
177539c13c20SShubham Bansal 			return -EFAULT;
177639c13c20SShubham Bansal 		break;
177739c13c20SShubham Bansal 	/* function call */
177839c13c20SShubham Bansal 	case BPF_JMP | BPF_CALL:
177939c13c20SShubham Bansal 	{
17801c35ba12SRussell King 		const s8 *r0 = bpf2a32[BPF_REG_0];
17811c35ba12SRussell King 		const s8 *r1 = bpf2a32[BPF_REG_1];
17821c35ba12SRussell King 		const s8 *r2 = bpf2a32[BPF_REG_2];
17831c35ba12SRussell King 		const s8 *r3 = bpf2a32[BPF_REG_3];
17841c35ba12SRussell King 		const s8 *r4 = bpf2a32[BPF_REG_4];
17851c35ba12SRussell King 		const s8 *r5 = bpf2a32[BPF_REG_5];
178639c13c20SShubham Bansal 		const u32 func = (u32)__bpf_call_base + (u32)imm;
178739c13c20SShubham Bansal 
178847b9c3bfSRussell King 		emit_a32_mov_r64(true, r0, r1, ctx);
178947b9c3bfSRussell King 		emit_a32_mov_r64(true, r1, r2, ctx);
179096cced4eSRussell King 		emit_push_r64(r5, ctx);
179196cced4eSRussell King 		emit_push_r64(r4, ctx);
179296cced4eSRussell King 		emit_push_r64(r3, ctx);
179339c13c20SShubham Bansal 
179447b9c3bfSRussell King 		emit_a32_mov_i(tmp[1], func, ctx);
179539c13c20SShubham Bansal 		emit_blx_r(tmp[1], ctx);
179639c13c20SShubham Bansal 
179739c13c20SShubham Bansal 		emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
179839c13c20SShubham Bansal 		break;
179939c13c20SShubham Bansal 	}
180039c13c20SShubham Bansal 	/* function return */
180139c13c20SShubham Bansal 	case BPF_JMP | BPF_EXIT:
180239c13c20SShubham Bansal 		/* Optimization: when last instruction is EXIT
180339c13c20SShubham Bansal 		 * simply fallthrough to epilogue.
180439c13c20SShubham Bansal 		 */
180539c13c20SShubham Bansal 		if (i == ctx->prog->len - 1)
180639c13c20SShubham Bansal 			break;
180739c13c20SShubham Bansal 		jmp_offset = epilogue_offset(ctx);
180839c13c20SShubham Bansal 		check_imm24(jmp_offset);
180939c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
181039c13c20SShubham Bansal 		break;
181139c13c20SShubham Bansal notyet:
181239c13c20SShubham Bansal 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
181339c13c20SShubham Bansal 		return -EFAULT;
181439c13c20SShubham Bansal 	default:
181539c13c20SShubham Bansal 		pr_err_once("unknown opcode %02x\n", code);
181639c13c20SShubham Bansal 		return -EINVAL;
1817ddecdfceSMircea Gherzan 	}
18180b59d880SNicolas Schichan 
18190b59d880SNicolas Schichan 	if (ctx->flags & FLAG_IMM_OVERFLOW)
18200b59d880SNicolas Schichan 		/*
18210b59d880SNicolas Schichan 		 * this instruction generated an overflow when
18220b59d880SNicolas Schichan 		 * trying to access the literal pool, so
18230b59d880SNicolas Schichan 		 * delegate this filter to the kernel interpreter.
18240b59d880SNicolas Schichan 		 */
18250b59d880SNicolas Schichan 		return -1;
182639c13c20SShubham Bansal 	return 0;
1827ddecdfceSMircea Gherzan }
1828ddecdfceSMircea Gherzan 
build_body(struct jit_ctx * ctx)182939c13c20SShubham Bansal static int build_body(struct jit_ctx *ctx)
183039c13c20SShubham Bansal {
183139c13c20SShubham Bansal 	const struct bpf_prog *prog = ctx->prog;
183239c13c20SShubham Bansal 	unsigned int i;
183339c13c20SShubham Bansal 
183439c13c20SShubham Bansal 	for (i = 0; i < prog->len; i++) {
183539c13c20SShubham Bansal 		const struct bpf_insn *insn = &(prog->insnsi[i]);
183639c13c20SShubham Bansal 		int ret;
183739c13c20SShubham Bansal 
183839c13c20SShubham Bansal 		ret = build_insn(insn, ctx);
183939c13c20SShubham Bansal 
184039c13c20SShubham Bansal 		/* It's used with loading the 64 bit immediate value. */
184139c13c20SShubham Bansal 		if (ret > 0) {
184239c13c20SShubham Bansal 			i++;
1843ddecdfceSMircea Gherzan 			if (ctx->target == NULL)
184439c13c20SShubham Bansal 				ctx->offsets[i] = ctx->idx;
184539c13c20SShubham Bansal 			continue;
184639c13c20SShubham Bansal 		}
184739c13c20SShubham Bansal 
184839c13c20SShubham Bansal 		if (ctx->target == NULL)
184939c13c20SShubham Bansal 			ctx->offsets[i] = ctx->idx;
185039c13c20SShubham Bansal 
1851*d8dc09a4SJulia Lawall 		/* If unsuccesful, return with error code */
185239c13c20SShubham Bansal 		if (ret)
185339c13c20SShubham Bansal 			return ret;
185439c13c20SShubham Bansal 	}
185539c13c20SShubham Bansal 	return 0;
185639c13c20SShubham Bansal }
185739c13c20SShubham Bansal 
validate_code(struct jit_ctx * ctx)185839c13c20SShubham Bansal static int validate_code(struct jit_ctx *ctx)
185939c13c20SShubham Bansal {
186039c13c20SShubham Bansal 	int i;
186139c13c20SShubham Bansal 
186239c13c20SShubham Bansal 	for (i = 0; i < ctx->idx; i++) {
186339c13c20SShubham Bansal 		if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
186439c13c20SShubham Bansal 			return -1;
186539c13c20SShubham Bansal 	}
1866ddecdfceSMircea Gherzan 
1867ddecdfceSMircea Gherzan 	return 0;
1868ddecdfceSMircea Gherzan }
1869ddecdfceSMircea Gherzan 
bpf_jit_needs_zext(void)1870163541e6SJiong Wang bool bpf_jit_needs_zext(void)
1871163541e6SJiong Wang {
1872163541e6SJiong Wang 	return true;
1873163541e6SJiong Wang }
1874163541e6SJiong Wang 
bpf_int_jit_compile(struct bpf_prog * prog)187539c13c20SShubham Bansal struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
187639c13c20SShubham Bansal {
187739c13c20SShubham Bansal 	struct bpf_prog *tmp, *orig_prog = prog;
187839c13c20SShubham Bansal 	struct bpf_binary_header *header;
187939c13c20SShubham Bansal 	bool tmp_blinded = false;
188039c13c20SShubham Bansal 	struct jit_ctx ctx;
188139c13c20SShubham Bansal 	unsigned int tmp_idx;
188239c13c20SShubham Bansal 	unsigned int image_size;
188339c13c20SShubham Bansal 	u8 *image_ptr;
188439c13c20SShubham Bansal 
188539c13c20SShubham Bansal 	/* If BPF JIT was not enabled then we must fall back to
188639c13c20SShubham Bansal 	 * the interpreter.
188739c13c20SShubham Bansal 	 */
188860b58afcSAlexei Starovoitov 	if (!prog->jit_requested)
188939c13c20SShubham Bansal 		return orig_prog;
189039c13c20SShubham Bansal 
189139c13c20SShubham Bansal 	/* If constant blinding was enabled and we failed during blinding
189239c13c20SShubham Bansal 	 * then we must fall back to the interpreter. Otherwise, we save
189339c13c20SShubham Bansal 	 * the new JITed code.
189439c13c20SShubham Bansal 	 */
189539c13c20SShubham Bansal 	tmp = bpf_jit_blind_constants(prog);
189639c13c20SShubham Bansal 
189739c13c20SShubham Bansal 	if (IS_ERR(tmp))
189839c13c20SShubham Bansal 		return orig_prog;
189939c13c20SShubham Bansal 	if (tmp != prog) {
190039c13c20SShubham Bansal 		tmp_blinded = true;
190139c13c20SShubham Bansal 		prog = tmp;
190239c13c20SShubham Bansal 	}
1903ddecdfceSMircea Gherzan 
1904ddecdfceSMircea Gherzan 	memset(&ctx, 0, sizeof(ctx));
190539c13c20SShubham Bansal 	ctx.prog = prog;
19068c9602d3SRussell King 	ctx.cpu_architecture = cpu_architecture();
1907ddecdfceSMircea Gherzan 
190839c13c20SShubham Bansal 	/* Not able to allocate memory for offsets[] , then
190939c13c20SShubham Bansal 	 * we must fall back to the interpreter
191039c13c20SShubham Bansal 	 */
191139c13c20SShubham Bansal 	ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
191239c13c20SShubham Bansal 	if (ctx.offsets == NULL) {
191339c13c20SShubham Bansal 		prog = orig_prog;
1914ddecdfceSMircea Gherzan 		goto out;
191539c13c20SShubham Bansal 	}
191639c13c20SShubham Bansal 
191739c13c20SShubham Bansal 	/* 1) fake pass to find in the length of the JITed code,
191839c13c20SShubham Bansal 	 * to compute ctx->offsets and other context variables
191939c13c20SShubham Bansal 	 * needed to compute final JITed code.
192039c13c20SShubham Bansal 	 * Also, calculate random starting pointer/start of JITed code
192139c13c20SShubham Bansal 	 * which is prefixed by random number of fault instructions.
192239c13c20SShubham Bansal 	 *
192339c13c20SShubham Bansal 	 * If the first pass fails then there is no chance of it
192439c13c20SShubham Bansal 	 * being successful in the second pass, so just fall back
192539c13c20SShubham Bansal 	 * to the interpreter.
192639c13c20SShubham Bansal 	 */
192739c13c20SShubham Bansal 	if (build_body(&ctx)) {
192839c13c20SShubham Bansal 		prog = orig_prog;
192939c13c20SShubham Bansal 		goto out_off;
193039c13c20SShubham Bansal 	}
1931ddecdfceSMircea Gherzan 
1932ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1933ddecdfceSMircea Gherzan 	build_prologue(&ctx);
1934ddecdfceSMircea Gherzan 	ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1935ddecdfceSMircea Gherzan 
193639c13c20SShubham Bansal 	ctx.epilogue_offset = ctx.idx;
193739c13c20SShubham Bansal 
1938ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1939ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1940ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1941ddecdfceSMircea Gherzan 	ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1942ddecdfceSMircea Gherzan 
1943ddecdfceSMircea Gherzan 	ctx.idx += ctx.imm_count;
1944ddecdfceSMircea Gherzan 	if (ctx.imm_count) {
194539c13c20SShubham Bansal 		ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
194639c13c20SShubham Bansal 		if (ctx.imms == NULL) {
194739c13c20SShubham Bansal 			prog = orig_prog;
194839c13c20SShubham Bansal 			goto out_off;
194939c13c20SShubham Bansal 		}
1950ddecdfceSMircea Gherzan 	}
1951ddecdfceSMircea Gherzan #else
195239c13c20SShubham Bansal 	/* there's nothing about the epilogue on ARMv7 */
1953ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1954ddecdfceSMircea Gherzan #endif
195539c13c20SShubham Bansal 	/* Now we can get the actual image size of the JITed arm code.
195639c13c20SShubham Bansal 	 * Currently, we are not considering the THUMB-2 instructions
195739c13c20SShubham Bansal 	 * for jit, although it can decrease the size of the image.
195839c13c20SShubham Bansal 	 *
195939c13c20SShubham Bansal 	 * As each arm instruction is of length 32bit, we are translating
1960*d8dc09a4SJulia Lawall 	 * number of JITed instructions into the size required to store these
196139c13c20SShubham Bansal 	 * JITed code.
196239c13c20SShubham Bansal 	 */
196339c13c20SShubham Bansal 	image_size = sizeof(u32) * ctx.idx;
1964ddecdfceSMircea Gherzan 
196539c13c20SShubham Bansal 	/* Now we know the size of the structure to make */
196639c13c20SShubham Bansal 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
196739c13c20SShubham Bansal 				      sizeof(u32), jit_fill_hole);
196839c13c20SShubham Bansal 	/* Not able to allocate memory for the structure then
196939c13c20SShubham Bansal 	 * we must fall back to the interpretation
197039c13c20SShubham Bansal 	 */
197139c13c20SShubham Bansal 	if (header == NULL) {
197239c13c20SShubham Bansal 		prog = orig_prog;
197339c13c20SShubham Bansal 		goto out_imms;
197439c13c20SShubham Bansal 	}
197539c13c20SShubham Bansal 
197639c13c20SShubham Bansal 	/* 2.) Actual pass to generate final JIT code */
197739c13c20SShubham Bansal 	ctx.target = (u32 *) image_ptr;
1978ddecdfceSMircea Gherzan 	ctx.idx = 0;
197955309dd3SDaniel Borkmann 
1980ddecdfceSMircea Gherzan 	build_prologue(&ctx);
198139c13c20SShubham Bansal 
198239c13c20SShubham Bansal 	/* If building the body of the JITed code fails somehow,
198339c13c20SShubham Bansal 	 * we fall back to the interpretation.
198439c13c20SShubham Bansal 	 */
19850b59d880SNicolas Schichan 	if (build_body(&ctx) < 0) {
198639c13c20SShubham Bansal 		image_ptr = NULL;
19870b59d880SNicolas Schichan 		bpf_jit_binary_free(header);
198839c13c20SShubham Bansal 		prog = orig_prog;
198939c13c20SShubham Bansal 		goto out_imms;
19900b59d880SNicolas Schichan 	}
1991ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1992ddecdfceSMircea Gherzan 
199339c13c20SShubham Bansal 	/* 3.) Extra pass to validate JITed Code */
199439c13c20SShubham Bansal 	if (validate_code(&ctx)) {
199539c13c20SShubham Bansal 		image_ptr = NULL;
199639c13c20SShubham Bansal 		bpf_jit_binary_free(header);
199739c13c20SShubham Bansal 		prog = orig_prog;
199839c13c20SShubham Bansal 		goto out_imms;
199939c13c20SShubham Bansal 	}
2000ebaef649SDaniel Borkmann 	flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
2001ddecdfceSMircea Gherzan 
200239c13c20SShubham Bansal 	if (bpf_jit_enable > 1)
200339c13c20SShubham Bansal 		/* there are 2 passes here */
200439c13c20SShubham Bansal 		bpf_jit_dump(prog->len, image_size, 2, ctx.target);
200539c13c20SShubham Bansal 
200618d405afSDaniel Borkmann 	bpf_jit_binary_lock_ro(header);
200739c13c20SShubham Bansal 	prog->bpf_func = (void *)ctx.target;
200839c13c20SShubham Bansal 	prog->jited = 1;
200939c13c20SShubham Bansal 	prog->jited_len = image_size;
201039c13c20SShubham Bansal 
201139c13c20SShubham Bansal out_imms:
2012ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
2013ddecdfceSMircea Gherzan 	if (ctx.imm_count)
2014ddecdfceSMircea Gherzan 		kfree(ctx.imms);
2015ddecdfceSMircea Gherzan #endif
201639c13c20SShubham Bansal out_off:
2017ddecdfceSMircea Gherzan 	kfree(ctx.offsets);
201839c13c20SShubham Bansal out:
201939c13c20SShubham Bansal 	if (tmp_blinded)
202039c13c20SShubham Bansal 		bpf_jit_prog_release_other(prog, prog == orig_prog ?
202139c13c20SShubham Bansal 					   tmp : orig_prog);
202239c13c20SShubham Bansal 	return prog;
2023ddecdfceSMircea Gherzan }
2024ddecdfceSMircea Gherzan 
2025