xref: /openbmc/linux/arch/arm/net/bpf_jit_32.c (revision f4483f2cc1fdc03488c8a1452e545545ae5bda93)
1ddecdfceSMircea Gherzan /*
239c13c20SShubham Bansal  * Just-In-Time compiler for eBPF filters on 32bit ARM
3ddecdfceSMircea Gherzan  *
439c13c20SShubham Bansal  * Copyright (c) 2017 Shubham Bansal <illusionist.neo@gmail.com>
5ddecdfceSMircea Gherzan  * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
6ddecdfceSMircea Gherzan  *
7ddecdfceSMircea Gherzan  * This program is free software; you can redistribute it and/or modify it
8ddecdfceSMircea Gherzan  * under the terms of the GNU General Public License as published by the
9ddecdfceSMircea Gherzan  * Free Software Foundation; version 2 of the License.
10ddecdfceSMircea Gherzan  */
11ddecdfceSMircea Gherzan 
1239c13c20SShubham Bansal #include <linux/bpf.h>
13ddecdfceSMircea Gherzan #include <linux/bitops.h>
14ddecdfceSMircea Gherzan #include <linux/compiler.h>
15ddecdfceSMircea Gherzan #include <linux/errno.h>
16ddecdfceSMircea Gherzan #include <linux/filter.h>
17ddecdfceSMircea Gherzan #include <linux/netdevice.h>
18ddecdfceSMircea Gherzan #include <linux/string.h>
19ddecdfceSMircea Gherzan #include <linux/slab.h>
20bf0098f2SDaniel Borkmann #include <linux/if_vlan.h>
21e8b56d55SDaniel Borkmann 
22ddecdfceSMircea Gherzan #include <asm/cacheflush.h>
23ddecdfceSMircea Gherzan #include <asm/hwcap.h>
243460743eSBen Dooks #include <asm/opcodes.h>
25ddecdfceSMircea Gherzan 
26ddecdfceSMircea Gherzan #include "bpf_jit_32.h"
27ddecdfceSMircea Gherzan 
2839c13c20SShubham Bansal int bpf_jit_enable __read_mostly;
2939c13c20SShubham Bansal 
3039c13c20SShubham Bansal #define STACK_OFFSET(k)	(k)
3139c13c20SShubham Bansal #define TMP_REG_1	(MAX_BPF_JIT_REG + 0)	/* TEMP Register 1 */
3239c13c20SShubham Bansal #define TMP_REG_2	(MAX_BPF_JIT_REG + 1)	/* TEMP Register 2 */
3339c13c20SShubham Bansal #define TCALL_CNT	(MAX_BPF_JIT_REG + 2)	/* Tail Call Count */
3439c13c20SShubham Bansal 
3539c13c20SShubham Bansal /* Flags used for JIT optimization */
3639c13c20SShubham Bansal #define SEEN_CALL	(1 << 0)
3739c13c20SShubham Bansal 
3839c13c20SShubham Bansal #define FLAG_IMM_OVERFLOW	(1 << 0)
3939c13c20SShubham Bansal 
40ddecdfceSMircea Gherzan /*
4139c13c20SShubham Bansal  * Map eBPF registers to ARM 32bit registers or stack scratch space.
42ddecdfceSMircea Gherzan  *
4339c13c20SShubham Bansal  * 1. First argument is passed using the arm 32bit registers and rest of the
4439c13c20SShubham Bansal  * arguments are passed on stack scratch space.
4539c13c20SShubham Bansal  * 2. First callee-saved arugument is mapped to arm 32 bit registers and rest
4639c13c20SShubham Bansal  * arguments are mapped to scratch space on stack.
4739c13c20SShubham Bansal  * 3. We need two 64 bit temp registers to do complex operations on eBPF
4839c13c20SShubham Bansal  * registers.
4939c13c20SShubham Bansal  *
5039c13c20SShubham Bansal  * As the eBPF registers are all 64 bit registers and arm has only 32 bit
5139c13c20SShubham Bansal  * registers, we have to map each eBPF registers with two arm 32 bit regs or
5239c13c20SShubham Bansal  * scratch memory space and we have to build eBPF 64 bit register from those.
5339c13c20SShubham Bansal  *
5439c13c20SShubham Bansal  */
5539c13c20SShubham Bansal static const u8 bpf2a32[][2] = {
5639c13c20SShubham Bansal 	/* return value from in-kernel function, and exit value from eBPF */
5739c13c20SShubham Bansal 	[BPF_REG_0] = {ARM_R1, ARM_R0},
5839c13c20SShubham Bansal 	/* arguments from eBPF program to in-kernel function */
5939c13c20SShubham Bansal 	[BPF_REG_1] = {ARM_R3, ARM_R2},
6039c13c20SShubham Bansal 	/* Stored on stack scratch space */
6139c13c20SShubham Bansal 	[BPF_REG_2] = {STACK_OFFSET(0), STACK_OFFSET(4)},
6239c13c20SShubham Bansal 	[BPF_REG_3] = {STACK_OFFSET(8), STACK_OFFSET(12)},
6339c13c20SShubham Bansal 	[BPF_REG_4] = {STACK_OFFSET(16), STACK_OFFSET(20)},
6439c13c20SShubham Bansal 	[BPF_REG_5] = {STACK_OFFSET(24), STACK_OFFSET(28)},
6539c13c20SShubham Bansal 	/* callee saved registers that in-kernel function will preserve */
6639c13c20SShubham Bansal 	[BPF_REG_6] = {ARM_R5, ARM_R4},
6739c13c20SShubham Bansal 	/* Stored on stack scratch space */
6839c13c20SShubham Bansal 	[BPF_REG_7] = {STACK_OFFSET(32), STACK_OFFSET(36)},
6939c13c20SShubham Bansal 	[BPF_REG_8] = {STACK_OFFSET(40), STACK_OFFSET(44)},
7039c13c20SShubham Bansal 	[BPF_REG_9] = {STACK_OFFSET(48), STACK_OFFSET(52)},
7139c13c20SShubham Bansal 	/* Read only Frame Pointer to access Stack */
7239c13c20SShubham Bansal 	[BPF_REG_FP] = {STACK_OFFSET(56), STACK_OFFSET(60)},
7339c13c20SShubham Bansal 	/* Temporary Register for internal BPF JIT, can be used
7439c13c20SShubham Bansal 	 * for constant blindings and others.
7539c13c20SShubham Bansal 	 */
7639c13c20SShubham Bansal 	[TMP_REG_1] = {ARM_R7, ARM_R6},
7739c13c20SShubham Bansal 	[TMP_REG_2] = {ARM_R10, ARM_R8},
7839c13c20SShubham Bansal 	/* Tail call count. Stored on stack scratch space. */
7939c13c20SShubham Bansal 	[TCALL_CNT] = {STACK_OFFSET(64), STACK_OFFSET(68)},
8039c13c20SShubham Bansal 	/* temporary register for blinding constants.
8139c13c20SShubham Bansal 	 * Stored on stack scratch space.
8239c13c20SShubham Bansal 	 */
8339c13c20SShubham Bansal 	[BPF_REG_AX] = {STACK_OFFSET(72), STACK_OFFSET(76)},
8439c13c20SShubham Bansal };
8539c13c20SShubham Bansal 
8639c13c20SShubham Bansal #define	dst_lo	dst[1]
8739c13c20SShubham Bansal #define dst_hi	dst[0]
8839c13c20SShubham Bansal #define src_lo	src[1]
8939c13c20SShubham Bansal #define src_hi	src[0]
9039c13c20SShubham Bansal 
9139c13c20SShubham Bansal /*
9239c13c20SShubham Bansal  * JIT Context:
9339c13c20SShubham Bansal  *
9439c13c20SShubham Bansal  * prog			:	bpf_prog
9539c13c20SShubham Bansal  * idx			:	index of current last JITed instruction.
9639c13c20SShubham Bansal  * prologue_bytes	:	bytes used in prologue.
9739c13c20SShubham Bansal  * epilogue_offset	:	offset of epilogue starting.
9839c13c20SShubham Bansal  * seen			:	bit mask used for JIT optimization.
9939c13c20SShubham Bansal  * offsets		:	array of eBPF instruction offsets in
10039c13c20SShubham Bansal  *				JITed code.
10139c13c20SShubham Bansal  * target		:	final JITed code.
10239c13c20SShubham Bansal  * epilogue_bytes	:	no of bytes used in epilogue.
10339c13c20SShubham Bansal  * imm_count		:	no of immediate counts used for global
10439c13c20SShubham Bansal  *				variables.
10539c13c20SShubham Bansal  * imms			:	array of global variable addresses.
106ddecdfceSMircea Gherzan  */
107ddecdfceSMircea Gherzan 
108ddecdfceSMircea Gherzan struct jit_ctx {
10939c13c20SShubham Bansal 	const struct bpf_prog *prog;
11039c13c20SShubham Bansal 	unsigned int idx;
11139c13c20SShubham Bansal 	unsigned int prologue_bytes;
11239c13c20SShubham Bansal 	unsigned int epilogue_offset;
113ddecdfceSMircea Gherzan 	u32 seen;
114ddecdfceSMircea Gherzan 	u32 flags;
115ddecdfceSMircea Gherzan 	u32 *offsets;
116ddecdfceSMircea Gherzan 	u32 *target;
11739c13c20SShubham Bansal 	u32 stack_size;
118ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
119ddecdfceSMircea Gherzan 	u16 epilogue_bytes;
120ddecdfceSMircea Gherzan 	u16 imm_count;
121ddecdfceSMircea Gherzan 	u32 *imms;
122ddecdfceSMircea Gherzan #endif
123ddecdfceSMircea Gherzan };
124ddecdfceSMircea Gherzan 
125ddecdfceSMircea Gherzan /*
1264560cdffSNicolas Schichan  * Wrappers which handle both OABI and EABI and assures Thumb2 interworking
127ddecdfceSMircea Gherzan  * (where the assembly routines like __aeabi_uidiv could cause problems).
128ddecdfceSMircea Gherzan  */
12939c13c20SShubham Bansal static u32 jit_udiv32(u32 dividend, u32 divisor)
130ddecdfceSMircea Gherzan {
131ddecdfceSMircea Gherzan 	return dividend / divisor;
132ddecdfceSMircea Gherzan }
133ddecdfceSMircea Gherzan 
13439c13c20SShubham Bansal static u32 jit_mod32(u32 dividend, u32 divisor)
1354560cdffSNicolas Schichan {
1364560cdffSNicolas Schichan 	return dividend % divisor;
1374560cdffSNicolas Schichan }
1384560cdffSNicolas Schichan 
139ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
140ddecdfceSMircea Gherzan {
1413460743eSBen Dooks 	inst |= (cond << 28);
1423460743eSBen Dooks 	inst = __opcode_to_mem_arm(inst);
1433460743eSBen Dooks 
144ddecdfceSMircea Gherzan 	if (ctx->target != NULL)
1453460743eSBen Dooks 		ctx->target[ctx->idx] = inst;
146ddecdfceSMircea Gherzan 
147ddecdfceSMircea Gherzan 	ctx->idx++;
148ddecdfceSMircea Gherzan }
149ddecdfceSMircea Gherzan 
150ddecdfceSMircea Gherzan /*
151ddecdfceSMircea Gherzan  * Emit an instruction that will be executed unconditionally.
152ddecdfceSMircea Gherzan  */
153ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx)
154ddecdfceSMircea Gherzan {
155ddecdfceSMircea Gherzan 	_emit(ARM_COND_AL, inst, ctx);
156ddecdfceSMircea Gherzan }
157ddecdfceSMircea Gherzan 
15839c13c20SShubham Bansal /*
15939c13c20SShubham Bansal  * Checks if immediate value can be converted to imm12(12 bits) value.
16039c13c20SShubham Bansal  */
16139c13c20SShubham Bansal static int16_t imm8m(u32 x)
162ddecdfceSMircea Gherzan {
16339c13c20SShubham Bansal 	u32 rot;
164ddecdfceSMircea Gherzan 
16539c13c20SShubham Bansal 	for (rot = 0; rot < 16; rot++)
16639c13c20SShubham Bansal 		if ((x & ~ror32(0xff, 2 * rot)) == 0)
16739c13c20SShubham Bansal 			return rol32(x, 2 * rot) | (rot << 8);
16839c13c20SShubham Bansal 	return -1;
169ddecdfceSMircea Gherzan }
170ddecdfceSMircea Gherzan 
17139c13c20SShubham Bansal /*
17239c13c20SShubham Bansal  * Initializes the JIT space with undefined instructions.
17339c13c20SShubham Bansal  */
17455309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
17555309dd3SDaniel Borkmann {
176e8b56d55SDaniel Borkmann 	u32 *ptr;
17755309dd3SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
17855309dd3SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
179e8b56d55SDaniel Borkmann 		*ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
18055309dd3SDaniel Borkmann }
18155309dd3SDaniel Borkmann 
18239c13c20SShubham Bansal /* Stack must be multiples of 16 Bytes */
18339c13c20SShubham Bansal #define STACK_ALIGN(sz) (((sz) + 3) & ~3)
184ddecdfceSMircea Gherzan 
18539c13c20SShubham Bansal /* Stack space for BPF_REG_2, BPF_REG_3, BPF_REG_4,
18639c13c20SShubham Bansal  * BPF_REG_5, BPF_REG_7, BPF_REG_8, BPF_REG_9,
18739c13c20SShubham Bansal  * BPF_REG_FP and Tail call counts.
18839c13c20SShubham Bansal  */
18939c13c20SShubham Bansal #define SCRATCH_SIZE 80
190ddecdfceSMircea Gherzan 
19139c13c20SShubham Bansal /* total stack size used in JITed code */
19239c13c20SShubham Bansal #define _STACK_SIZE \
19339c13c20SShubham Bansal 	(ctx->prog->aux->stack_depth + \
19439c13c20SShubham Bansal 	 + SCRATCH_SIZE + \
19539c13c20SShubham Bansal 	 + 4 /* extra for skb_copy_bits buffer */)
196ddecdfceSMircea Gherzan 
19739c13c20SShubham Bansal #define STACK_SIZE STACK_ALIGN(_STACK_SIZE)
198ddecdfceSMircea Gherzan 
19939c13c20SShubham Bansal /* Get the offset of eBPF REGISTERs stored on scratch space. */
20039c13c20SShubham Bansal #define STACK_VAR(off) (STACK_SIZE-off-4)
201ddecdfceSMircea Gherzan 
20239c13c20SShubham Bansal /* Offset of skb_copy_bits buffer */
20339c13c20SShubham Bansal #define SKB_BUFFER STACK_VAR(SCRATCH_SIZE)
204ddecdfceSMircea Gherzan 
205ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
206ddecdfceSMircea Gherzan 
207ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx)
208ddecdfceSMircea Gherzan {
20939c13c20SShubham Bansal 	unsigned int i = 0, offset;
210ddecdfceSMircea Gherzan 	u16 imm;
211ddecdfceSMircea Gherzan 
212ddecdfceSMircea Gherzan 	/* on the "fake" run we just count them (duplicates included) */
213ddecdfceSMircea Gherzan 	if (ctx->target == NULL) {
214ddecdfceSMircea Gherzan 		ctx->imm_count++;
215ddecdfceSMircea Gherzan 		return 0;
216ddecdfceSMircea Gherzan 	}
217ddecdfceSMircea Gherzan 
218ddecdfceSMircea Gherzan 	while ((i < ctx->imm_count) && ctx->imms[i]) {
219ddecdfceSMircea Gherzan 		if (ctx->imms[i] == k)
220ddecdfceSMircea Gherzan 			break;
221ddecdfceSMircea Gherzan 		i++;
222ddecdfceSMircea Gherzan 	}
223ddecdfceSMircea Gherzan 
224ddecdfceSMircea Gherzan 	if (ctx->imms[i] == 0)
225ddecdfceSMircea Gherzan 		ctx->imms[i] = k;
226ddecdfceSMircea Gherzan 
227ddecdfceSMircea Gherzan 	/* constants go just after the epilogue */
22839c13c20SShubham Bansal 	offset =  ctx->offsets[ctx->prog->len - 1] * 4;
229ddecdfceSMircea Gherzan 	offset += ctx->prologue_bytes;
230ddecdfceSMircea Gherzan 	offset += ctx->epilogue_bytes;
231ddecdfceSMircea Gherzan 	offset += i * 4;
232ddecdfceSMircea Gherzan 
233ddecdfceSMircea Gherzan 	ctx->target[offset / 4] = k;
234ddecdfceSMircea Gherzan 
235ddecdfceSMircea Gherzan 	/* PC in ARM mode == address of the instruction + 8 */
236ddecdfceSMircea Gherzan 	imm = offset - (8 + ctx->idx * 4);
237ddecdfceSMircea Gherzan 
2380b59d880SNicolas Schichan 	if (imm & ~0xfff) {
2390b59d880SNicolas Schichan 		/*
2400b59d880SNicolas Schichan 		 * literal pool is too far, signal it into flags. we
2410b59d880SNicolas Schichan 		 * can only detect it on the second pass unfortunately.
2420b59d880SNicolas Schichan 		 */
2430b59d880SNicolas Schichan 		ctx->flags |= FLAG_IMM_OVERFLOW;
2440b59d880SNicolas Schichan 		return 0;
2450b59d880SNicolas Schichan 	}
2460b59d880SNicolas Schichan 
247ddecdfceSMircea Gherzan 	return imm;
248ddecdfceSMircea Gherzan }
249ddecdfceSMircea Gherzan 
250ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */
251ddecdfceSMircea Gherzan 
25239c13c20SShubham Bansal static inline int bpf2a32_offset(int bpf_to, int bpf_from,
25339c13c20SShubham Bansal 				 const struct jit_ctx *ctx) {
25439c13c20SShubham Bansal 	int to, from;
25539c13c20SShubham Bansal 
25639c13c20SShubham Bansal 	if (ctx->target == NULL)
25739c13c20SShubham Bansal 		return 0;
25839c13c20SShubham Bansal 	to = ctx->offsets[bpf_to];
25939c13c20SShubham Bansal 	from = ctx->offsets[bpf_from];
26039c13c20SShubham Bansal 
26139c13c20SShubham Bansal 	return to - from - 1;
26239c13c20SShubham Bansal }
26339c13c20SShubham Bansal 
264ddecdfceSMircea Gherzan /*
265ddecdfceSMircea Gherzan  * Move an immediate that's not an imm8m to a core register.
266ddecdfceSMircea Gherzan  */
26739c13c20SShubham Bansal static inline void emit_mov_i_no8m(const u8 rd, u32 val, struct jit_ctx *ctx)
268ddecdfceSMircea Gherzan {
269ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
270ddecdfceSMircea Gherzan 	emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
271ddecdfceSMircea Gherzan #else
272ddecdfceSMircea Gherzan 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
273ddecdfceSMircea Gherzan 	if (val > 0xffff)
274ddecdfceSMircea Gherzan 		emit(ARM_MOVT(rd, val >> 16), ctx);
275ddecdfceSMircea Gherzan #endif
276ddecdfceSMircea Gherzan }
277ddecdfceSMircea Gherzan 
27839c13c20SShubham Bansal static inline void emit_mov_i(const u8 rd, u32 val, struct jit_ctx *ctx)
279ddecdfceSMircea Gherzan {
280ddecdfceSMircea Gherzan 	int imm12 = imm8m(val);
281ddecdfceSMircea Gherzan 
282ddecdfceSMircea Gherzan 	if (imm12 >= 0)
283ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(rd, imm12), ctx);
284ddecdfceSMircea Gherzan 	else
285ddecdfceSMircea Gherzan 		emit_mov_i_no8m(rd, val, ctx);
286ddecdfceSMircea Gherzan }
287ddecdfceSMircea Gherzan 
288e9062481SRussell King static void emit_bx_r(u8 tgt_reg, struct jit_ctx *ctx)
289e9062481SRussell King {
290e9062481SRussell King 	if (elf_hwcap & HWCAP_THUMB)
291e9062481SRussell King 		emit(ARM_BX(tgt_reg), ctx);
292e9062481SRussell King 	else
293e9062481SRussell King 		emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
294e9062481SRussell King }
295e9062481SRussell King 
296ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
297ddecdfceSMircea Gherzan {
29839c13c20SShubham Bansal 	ctx->seen |= SEEN_CALL;
299ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5
300ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
301e9062481SRussell King 	emit_bx_r(tgt_reg, ctx);
302ddecdfceSMircea Gherzan #else
303ddecdfceSMircea Gherzan 	emit(ARM_BLX_R(tgt_reg), ctx);
304ddecdfceSMircea Gherzan #endif
305ddecdfceSMircea Gherzan }
306ddecdfceSMircea Gherzan 
30739c13c20SShubham Bansal static inline int epilogue_offset(const struct jit_ctx *ctx)
308ddecdfceSMircea Gherzan {
30939c13c20SShubham Bansal 	int to, from;
31039c13c20SShubham Bansal 	/* No need for 1st dummy run */
31139c13c20SShubham Bansal 	if (ctx->target == NULL)
31239c13c20SShubham Bansal 		return 0;
31339c13c20SShubham Bansal 	to = ctx->epilogue_offset;
31439c13c20SShubham Bansal 	from = ctx->idx;
31539c13c20SShubham Bansal 
31639c13c20SShubham Bansal 	return to - from - 2;
31739c13c20SShubham Bansal }
31839c13c20SShubham Bansal 
31939c13c20SShubham Bansal static inline void emit_udivmod(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx, u8 op)
32039c13c20SShubham Bansal {
32139c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
32239c13c20SShubham Bansal 	s32 jmp_offset;
32339c13c20SShubham Bansal 
32439c13c20SShubham Bansal 	/* checks if divisor is zero or not. If it is, then
32539c13c20SShubham Bansal 	 * exit directly.
32639c13c20SShubham Bansal 	 */
32739c13c20SShubham Bansal 	emit(ARM_CMP_I(rn, 0), ctx);
32839c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_MOV_I(ARM_R0, 0), ctx);
32939c13c20SShubham Bansal 	jmp_offset = epilogue_offset(ctx);
33039c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
331ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7
332ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_IDIVA) {
33339c13c20SShubham Bansal 		if (op == BPF_DIV)
334ddecdfceSMircea Gherzan 			emit(ARM_UDIV(rd, rm, rn), ctx);
3354560cdffSNicolas Schichan 		else {
33639c13c20SShubham Bansal 			emit(ARM_UDIV(ARM_IP, rm, rn), ctx);
33739c13c20SShubham Bansal 			emit(ARM_MLS(rd, rn, ARM_IP, rm), ctx);
3384560cdffSNicolas Schichan 		}
339ddecdfceSMircea Gherzan 		return;
340ddecdfceSMircea Gherzan 	}
341ddecdfceSMircea Gherzan #endif
34219fc99d0SNicolas Schichan 
34319fc99d0SNicolas Schichan 	/*
34439c13c20SShubham Bansal 	 * For BPF_ALU | BPF_DIV | BPF_K instructions
34539c13c20SShubham Bansal 	 * As ARM_R1 and ARM_R0 contains 1st argument of bpf
34639c13c20SShubham Bansal 	 * function, we need to save it on caller side to save
34739c13c20SShubham Bansal 	 * it from getting destroyed within callee.
34839c13c20SShubham Bansal 	 * After the return from the callee, we restore ARM_R0
34939c13c20SShubham Bansal 	 * ARM_R1.
35019fc99d0SNicolas Schichan 	 */
35139c13c20SShubham Bansal 	if (rn != ARM_R1) {
35239c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[0], ARM_R1), ctx);
353ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R1, rn), ctx);
35439c13c20SShubham Bansal 	}
35539c13c20SShubham Bansal 	if (rm != ARM_R0) {
35639c13c20SShubham Bansal 		emit(ARM_MOV_R(tmp[1], ARM_R0), ctx);
35719fc99d0SNicolas Schichan 		emit(ARM_MOV_R(ARM_R0, rm), ctx);
35839c13c20SShubham Bansal 	}
359ddecdfceSMircea Gherzan 
36039c13c20SShubham Bansal 	/* Call appropriate function */
361ddecdfceSMircea Gherzan 	ctx->seen |= SEEN_CALL;
36239c13c20SShubham Bansal 	emit_mov_i(ARM_IP, op == BPF_DIV ?
36339c13c20SShubham Bansal 		   (u32)jit_udiv32 : (u32)jit_mod32, ctx);
36439c13c20SShubham Bansal 	emit_blx_r(ARM_IP, ctx);
365ddecdfceSMircea Gherzan 
36639c13c20SShubham Bansal 	/* Save return value */
367ddecdfceSMircea Gherzan 	if (rd != ARM_R0)
368ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(rd, ARM_R0), ctx);
36939c13c20SShubham Bansal 
37039c13c20SShubham Bansal 	/* Restore ARM_R0 and ARM_R1 */
37139c13c20SShubham Bansal 	if (rn != ARM_R1)
37239c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R1, tmp[0]), ctx);
37339c13c20SShubham Bansal 	if (rm != ARM_R0)
37439c13c20SShubham Bansal 		emit(ARM_MOV_R(ARM_R0, tmp[1]), ctx);
375ddecdfceSMircea Gherzan }
376ddecdfceSMircea Gherzan 
37739c13c20SShubham Bansal /* Checks whether BPF register is on scratch stack space or not. */
37839c13c20SShubham Bansal static inline bool is_on_stack(u8 bpf_reg)
379ddecdfceSMircea Gherzan {
38039c13c20SShubham Bansal 	static u8 stack_regs[] = {BPF_REG_AX, BPF_REG_3, BPF_REG_4, BPF_REG_5,
38139c13c20SShubham Bansal 				BPF_REG_7, BPF_REG_8, BPF_REG_9, TCALL_CNT,
38239c13c20SShubham Bansal 				BPF_REG_2, BPF_REG_FP};
38339c13c20SShubham Bansal 	int i, reg_len = sizeof(stack_regs);
384ddecdfceSMircea Gherzan 
38539c13c20SShubham Bansal 	for (i = 0 ; i < reg_len ; i++) {
38639c13c20SShubham Bansal 		if (bpf_reg == stack_regs[i])
38739c13c20SShubham Bansal 			return true;
38839c13c20SShubham Bansal 	}
38939c13c20SShubham Bansal 	return false;
390ddecdfceSMircea Gherzan }
391ddecdfceSMircea Gherzan 
39239c13c20SShubham Bansal static inline void emit_a32_mov_i(const u8 dst, const u32 val,
39339c13c20SShubham Bansal 				  bool dstk, struct jit_ctx *ctx)
394ddecdfceSMircea Gherzan {
39539c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
396ddecdfceSMircea Gherzan 
39739c13c20SShubham Bansal 	if (dstk) {
39839c13c20SShubham Bansal 		emit_mov_i(tmp[1], val, ctx);
39939c13c20SShubham Bansal 		emit(ARM_STR_I(tmp[1], ARM_SP, STACK_VAR(dst)), ctx);
40039c13c20SShubham Bansal 	} else {
40139c13c20SShubham Bansal 		emit_mov_i(dst, val, ctx);
40239c13c20SShubham Bansal 	}
40339c13c20SShubham Bansal }
40434805931SDaniel Borkmann 
40539c13c20SShubham Bansal /* Sign extended move */
40639c13c20SShubham Bansal static inline void emit_a32_mov_i64(const bool is64, const u8 dst[],
40739c13c20SShubham Bansal 				  const u32 val, bool dstk,
40839c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
40939c13c20SShubham Bansal 	u32 hi = 0;
410ddecdfceSMircea Gherzan 
41139c13c20SShubham Bansal 	if (is64 && (val & (1<<31)))
41239c13c20SShubham Bansal 		hi = (u32)~0;
41339c13c20SShubham Bansal 	emit_a32_mov_i(dst_lo, val, dstk, ctx);
41439c13c20SShubham Bansal 	emit_a32_mov_i(dst_hi, hi, dstk, ctx);
41539c13c20SShubham Bansal }
41639c13c20SShubham Bansal 
41739c13c20SShubham Bansal static inline void emit_a32_add_r(const u8 dst, const u8 src,
41839c13c20SShubham Bansal 			      const bool is64, const bool hi,
41939c13c20SShubham Bansal 			      struct jit_ctx *ctx) {
42039c13c20SShubham Bansal 	/* 64 bit :
42139c13c20SShubham Bansal 	 *	adds dst_lo, dst_lo, src_lo
42239c13c20SShubham Bansal 	 *	adc dst_hi, dst_hi, src_hi
42339c13c20SShubham Bansal 	 * 32 bit :
42439c13c20SShubham Bansal 	 *	add dst_lo, dst_lo, src_lo
42539c13c20SShubham Bansal 	 */
42639c13c20SShubham Bansal 	if (!hi && is64)
42739c13c20SShubham Bansal 		emit(ARM_ADDS_R(dst, dst, src), ctx);
42839c13c20SShubham Bansal 	else if (hi && is64)
42939c13c20SShubham Bansal 		emit(ARM_ADC_R(dst, dst, src), ctx);
43039c13c20SShubham Bansal 	else
43139c13c20SShubham Bansal 		emit(ARM_ADD_R(dst, dst, src), ctx);
43239c13c20SShubham Bansal }
43339c13c20SShubham Bansal 
43439c13c20SShubham Bansal static inline void emit_a32_sub_r(const u8 dst, const u8 src,
43539c13c20SShubham Bansal 				  const bool is64, const bool hi,
43639c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
43739c13c20SShubham Bansal 	/* 64 bit :
43839c13c20SShubham Bansal 	 *	subs dst_lo, dst_lo, src_lo
43939c13c20SShubham Bansal 	 *	sbc dst_hi, dst_hi, src_hi
44039c13c20SShubham Bansal 	 * 32 bit :
44139c13c20SShubham Bansal 	 *	sub dst_lo, dst_lo, src_lo
44239c13c20SShubham Bansal 	 */
44339c13c20SShubham Bansal 	if (!hi && is64)
44439c13c20SShubham Bansal 		emit(ARM_SUBS_R(dst, dst, src), ctx);
44539c13c20SShubham Bansal 	else if (hi && is64)
44639c13c20SShubham Bansal 		emit(ARM_SBC_R(dst, dst, src), ctx);
44739c13c20SShubham Bansal 	else
44839c13c20SShubham Bansal 		emit(ARM_SUB_R(dst, dst, src), ctx);
44939c13c20SShubham Bansal }
45039c13c20SShubham Bansal 
45139c13c20SShubham Bansal static inline void emit_alu_r(const u8 dst, const u8 src, const bool is64,
45239c13c20SShubham Bansal 			      const bool hi, const u8 op, struct jit_ctx *ctx){
45339c13c20SShubham Bansal 	switch (BPF_OP(op)) {
45439c13c20SShubham Bansal 	/* dst = dst + src */
45539c13c20SShubham Bansal 	case BPF_ADD:
45639c13c20SShubham Bansal 		emit_a32_add_r(dst, src, is64, hi, ctx);
45739c13c20SShubham Bansal 		break;
45839c13c20SShubham Bansal 	/* dst = dst - src */
45939c13c20SShubham Bansal 	case BPF_SUB:
46039c13c20SShubham Bansal 		emit_a32_sub_r(dst, src, is64, hi, ctx);
46139c13c20SShubham Bansal 		break;
46239c13c20SShubham Bansal 	/* dst = dst | src */
46339c13c20SShubham Bansal 	case BPF_OR:
46439c13c20SShubham Bansal 		emit(ARM_ORR_R(dst, dst, src), ctx);
46539c13c20SShubham Bansal 		break;
46639c13c20SShubham Bansal 	/* dst = dst & src */
46739c13c20SShubham Bansal 	case BPF_AND:
46839c13c20SShubham Bansal 		emit(ARM_AND_R(dst, dst, src), ctx);
46939c13c20SShubham Bansal 		break;
47039c13c20SShubham Bansal 	/* dst = dst ^ src */
47139c13c20SShubham Bansal 	case BPF_XOR:
47239c13c20SShubham Bansal 		emit(ARM_EOR_R(dst, dst, src), ctx);
47339c13c20SShubham Bansal 		break;
47439c13c20SShubham Bansal 	/* dst = dst * src */
47539c13c20SShubham Bansal 	case BPF_MUL:
47639c13c20SShubham Bansal 		emit(ARM_MUL(dst, dst, src), ctx);
47739c13c20SShubham Bansal 		break;
47839c13c20SShubham Bansal 	/* dst = dst << src */
47939c13c20SShubham Bansal 	case BPF_LSH:
48039c13c20SShubham Bansal 		emit(ARM_LSL_R(dst, dst, src), ctx);
48139c13c20SShubham Bansal 		break;
48239c13c20SShubham Bansal 	/* dst = dst >> src */
48339c13c20SShubham Bansal 	case BPF_RSH:
48439c13c20SShubham Bansal 		emit(ARM_LSR_R(dst, dst, src), ctx);
48539c13c20SShubham Bansal 		break;
48639c13c20SShubham Bansal 	/* dst = dst >> src (signed)*/
48739c13c20SShubham Bansal 	case BPF_ARSH:
48839c13c20SShubham Bansal 		emit(ARM_MOV_SR(dst, dst, SRTYPE_ASR, src), ctx);
48939c13c20SShubham Bansal 		break;
49039c13c20SShubham Bansal 	}
49139c13c20SShubham Bansal }
49239c13c20SShubham Bansal 
49339c13c20SShubham Bansal /* ALU operation (32 bit)
49439c13c20SShubham Bansal  * dst = dst (op) src
49539c13c20SShubham Bansal  */
49639c13c20SShubham Bansal static inline void emit_a32_alu_r(const u8 dst, const u8 src,
49739c13c20SShubham Bansal 				  bool dstk, bool sstk,
49839c13c20SShubham Bansal 				  struct jit_ctx *ctx, const bool is64,
49939c13c20SShubham Bansal 				  const bool hi, const u8 op) {
50039c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
50139c13c20SShubham Bansal 	u8 rn = sstk ? tmp[1] : src;
50239c13c20SShubham Bansal 
50339c13c20SShubham Bansal 	if (sstk)
50439c13c20SShubham Bansal 		emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src)), ctx);
50539c13c20SShubham Bansal 
50639c13c20SShubham Bansal 	/* ALU operation */
50739c13c20SShubham Bansal 	if (dstk) {
50839c13c20SShubham Bansal 		emit(ARM_LDR_I(tmp[0], ARM_SP, STACK_VAR(dst)), ctx);
50939c13c20SShubham Bansal 		emit_alu_r(tmp[0], rn, is64, hi, op, ctx);
51039c13c20SShubham Bansal 		emit(ARM_STR_I(tmp[0], ARM_SP, STACK_VAR(dst)), ctx);
51139c13c20SShubham Bansal 	} else {
51239c13c20SShubham Bansal 		emit_alu_r(dst, rn, is64, hi, op, ctx);
51339c13c20SShubham Bansal 	}
51439c13c20SShubham Bansal }
51539c13c20SShubham Bansal 
51639c13c20SShubham Bansal /* ALU operation (64 bit) */
51739c13c20SShubham Bansal static inline void emit_a32_alu_r64(const bool is64, const u8 dst[],
51839c13c20SShubham Bansal 				  const u8 src[], bool dstk,
51939c13c20SShubham Bansal 				  bool sstk, struct jit_ctx *ctx,
52039c13c20SShubham Bansal 				  const u8 op) {
52139c13c20SShubham Bansal 	emit_a32_alu_r(dst_lo, src_lo, dstk, sstk, ctx, is64, false, op);
52239c13c20SShubham Bansal 	if (is64)
52339c13c20SShubham Bansal 		emit_a32_alu_r(dst_hi, src_hi, dstk, sstk, ctx, is64, true, op);
52439c13c20SShubham Bansal 	else
52539c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
52639c13c20SShubham Bansal }
52739c13c20SShubham Bansal 
52839c13c20SShubham Bansal /* dst = imm (4 bytes)*/
52939c13c20SShubham Bansal static inline void emit_a32_mov_r(const u8 dst, const u8 src,
53039c13c20SShubham Bansal 				  bool dstk, bool sstk,
53139c13c20SShubham Bansal 				  struct jit_ctx *ctx) {
53239c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
53339c13c20SShubham Bansal 	u8 rt = sstk ? tmp[0] : src;
53439c13c20SShubham Bansal 
53539c13c20SShubham Bansal 	if (sstk)
53639c13c20SShubham Bansal 		emit(ARM_LDR_I(tmp[0], ARM_SP, STACK_VAR(src)), ctx);
53739c13c20SShubham Bansal 	if (dstk)
53839c13c20SShubham Bansal 		emit(ARM_STR_I(rt, ARM_SP, STACK_VAR(dst)), ctx);
53939c13c20SShubham Bansal 	else
54039c13c20SShubham Bansal 		emit(ARM_MOV_R(dst, rt), ctx);
54139c13c20SShubham Bansal }
54239c13c20SShubham Bansal 
54339c13c20SShubham Bansal /* dst = src */
54439c13c20SShubham Bansal static inline void emit_a32_mov_r64(const bool is64, const u8 dst[],
54539c13c20SShubham Bansal 				  const u8 src[], bool dstk,
54639c13c20SShubham Bansal 				  bool sstk, struct jit_ctx *ctx) {
54739c13c20SShubham Bansal 	emit_a32_mov_r(dst_lo, src_lo, dstk, sstk, ctx);
54839c13c20SShubham Bansal 	if (is64) {
54939c13c20SShubham Bansal 		/* complete 8 byte move */
55039c13c20SShubham Bansal 		emit_a32_mov_r(dst_hi, src_hi, dstk, sstk, ctx);
55139c13c20SShubham Bansal 	} else {
55239c13c20SShubham Bansal 		/* Zero out high 4 bytes */
55339c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
55439c13c20SShubham Bansal 	}
55539c13c20SShubham Bansal }
55639c13c20SShubham Bansal 
55739c13c20SShubham Bansal /* Shift operations */
55839c13c20SShubham Bansal static inline void emit_a32_alu_i(const u8 dst, const u32 val, bool dstk,
55939c13c20SShubham Bansal 				struct jit_ctx *ctx, const u8 op) {
56039c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
56139c13c20SShubham Bansal 	u8 rd = dstk ? tmp[0] : dst;
56239c13c20SShubham Bansal 
56339c13c20SShubham Bansal 	if (dstk)
56439c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst)), ctx);
56539c13c20SShubham Bansal 
56639c13c20SShubham Bansal 	/* Do shift operation */
56739c13c20SShubham Bansal 	switch (op) {
56839c13c20SShubham Bansal 	case BPF_LSH:
56939c13c20SShubham Bansal 		emit(ARM_LSL_I(rd, rd, val), ctx);
57039c13c20SShubham Bansal 		break;
57139c13c20SShubham Bansal 	case BPF_RSH:
57239c13c20SShubham Bansal 		emit(ARM_LSR_I(rd, rd, val), ctx);
57339c13c20SShubham Bansal 		break;
57439c13c20SShubham Bansal 	case BPF_NEG:
57539c13c20SShubham Bansal 		emit(ARM_RSB_I(rd, rd, val), ctx);
57639c13c20SShubham Bansal 		break;
57739c13c20SShubham Bansal 	}
57839c13c20SShubham Bansal 
57939c13c20SShubham Bansal 	if (dstk)
58039c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst)), ctx);
58139c13c20SShubham Bansal }
58239c13c20SShubham Bansal 
58339c13c20SShubham Bansal /* dst = ~dst (64 bit) */
58439c13c20SShubham Bansal static inline void emit_a32_neg64(const u8 dst[], bool dstk,
58539c13c20SShubham Bansal 				struct jit_ctx *ctx){
58639c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
58739c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst[1];
58839c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst[0];
58939c13c20SShubham Bansal 
59039c13c20SShubham Bansal 	/* Setup Operand */
59139c13c20SShubham Bansal 	if (dstk) {
59239c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
59339c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
59439c13c20SShubham Bansal 	}
59539c13c20SShubham Bansal 
59639c13c20SShubham Bansal 	/* Do Negate Operation */
59739c13c20SShubham Bansal 	emit(ARM_RSBS_I(rd, rd, 0), ctx);
59839c13c20SShubham Bansal 	emit(ARM_RSC_I(rm, rm, 0), ctx);
59939c13c20SShubham Bansal 
60039c13c20SShubham Bansal 	if (dstk) {
60139c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
60239c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
60339c13c20SShubham Bansal 	}
60439c13c20SShubham Bansal }
60539c13c20SShubham Bansal 
60639c13c20SShubham Bansal /* dst = dst << src */
60739c13c20SShubham Bansal static inline void emit_a32_lsh_r64(const u8 dst[], const u8 src[], bool dstk,
60839c13c20SShubham Bansal 				    bool sstk, struct jit_ctx *ctx) {
60939c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
61039c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
61139c13c20SShubham Bansal 
61239c13c20SShubham Bansal 	/* Setup Operands */
61339c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
61439c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
61539c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
61639c13c20SShubham Bansal 
61739c13c20SShubham Bansal 	if (sstk)
61839c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
61939c13c20SShubham Bansal 	if (dstk) {
62039c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
62139c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
62239c13c20SShubham Bansal 	}
62339c13c20SShubham Bansal 
62439c13c20SShubham Bansal 	/* Do LSH operation */
62539c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_IP, rt, 32), ctx);
62639c13c20SShubham Bansal 	emit(ARM_RSB_I(tmp2[0], rt, 32), ctx);
62739c13c20SShubham Bansal 	/* As we are using ARM_LR */
62839c13c20SShubham Bansal 	ctx->seen |= SEEN_CALL;
62939c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rm, SRTYPE_ASL, rt), ctx);
63039c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rd, SRTYPE_ASL, ARM_IP), ctx);
63139c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_IP, ARM_LR, rd, SRTYPE_LSR, tmp2[0]), ctx);
63239c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rd, SRTYPE_ASL, rt), ctx);
63339c13c20SShubham Bansal 
63439c13c20SShubham Bansal 	if (dstk) {
63539c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_LR, ARM_SP, STACK_VAR(dst_lo)), ctx);
63639c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_hi)), ctx);
63739c13c20SShubham Bansal 	} else {
63839c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_LR), ctx);
63939c13c20SShubham Bansal 		emit(ARM_MOV_R(rm, ARM_IP), ctx);
64039c13c20SShubham Bansal 	}
64139c13c20SShubham Bansal }
64239c13c20SShubham Bansal 
64339c13c20SShubham Bansal /* dst = dst >> src (signed)*/
64439c13c20SShubham Bansal static inline void emit_a32_arsh_r64(const u8 dst[], const u8 src[], bool dstk,
64539c13c20SShubham Bansal 				    bool sstk, struct jit_ctx *ctx) {
64639c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
64739c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
64839c13c20SShubham Bansal 	/* Setup Operands */
64939c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
65039c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
65139c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
65239c13c20SShubham Bansal 
65339c13c20SShubham Bansal 	if (sstk)
65439c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
65539c13c20SShubham Bansal 	if (dstk) {
65639c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
65739c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
65839c13c20SShubham Bansal 	}
65939c13c20SShubham Bansal 
66039c13c20SShubham Bansal 	/* Do the ARSH operation */
66139c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
66239c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
66339c13c20SShubham Bansal 	/* As we are using ARM_LR */
66439c13c20SShubham Bansal 	ctx->seen |= SEEN_CALL;
66539c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rd, SRTYPE_LSR, rt), ctx);
66639c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_ASL, ARM_IP), ctx);
66739c13c20SShubham Bansal 	_emit(ARM_COND_MI, ARM_B(0), ctx);
66839c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_ASR, tmp2[0]), ctx);
66939c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_IP, rm, SRTYPE_ASR, rt), ctx);
67039c13c20SShubham Bansal 	if (dstk) {
67139c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_LR, ARM_SP, STACK_VAR(dst_lo)), ctx);
67239c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_hi)), ctx);
67339c13c20SShubham Bansal 	} else {
67439c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_LR), ctx);
67539c13c20SShubham Bansal 		emit(ARM_MOV_R(rm, ARM_IP), ctx);
67639c13c20SShubham Bansal 	}
67739c13c20SShubham Bansal }
67839c13c20SShubham Bansal 
67939c13c20SShubham Bansal /* dst = dst >> src */
68039c13c20SShubham Bansal static inline void emit_a32_lsr_r64(const u8 dst[], const u8 src[], bool dstk,
68139c13c20SShubham Bansal 				     bool sstk, struct jit_ctx *ctx) {
68239c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
68339c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
68439c13c20SShubham Bansal 	/* Setup Operands */
68539c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
68639c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
68739c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
68839c13c20SShubham Bansal 
68939c13c20SShubham Bansal 	if (sstk)
69039c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
69139c13c20SShubham Bansal 	if (dstk) {
69239c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
69339c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
69439c13c20SShubham Bansal 	}
69539c13c20SShubham Bansal 
69639c13c20SShubham Bansal 	/* Do LSH operation */
69739c13c20SShubham Bansal 	emit(ARM_RSB_I(ARM_IP, rt, 32), ctx);
69839c13c20SShubham Bansal 	emit(ARM_SUBS_I(tmp2[0], rt, 32), ctx);
69939c13c20SShubham Bansal 	/* As we are using ARM_LR */
70039c13c20SShubham Bansal 	ctx->seen |= SEEN_CALL;
70139c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_LR, rd, SRTYPE_LSR, rt), ctx);
70239c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_ASL, ARM_IP), ctx);
70339c13c20SShubham Bansal 	emit(ARM_ORR_SR(ARM_LR, ARM_LR, rm, SRTYPE_LSR, tmp2[0]), ctx);
70439c13c20SShubham Bansal 	emit(ARM_MOV_SR(ARM_IP, rm, SRTYPE_LSR, rt), ctx);
70539c13c20SShubham Bansal 	if (dstk) {
70639c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_LR, ARM_SP, STACK_VAR(dst_lo)), ctx);
70739c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_hi)), ctx);
70839c13c20SShubham Bansal 	} else {
70939c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_LR), ctx);
71039c13c20SShubham Bansal 		emit(ARM_MOV_R(rm, ARM_IP), ctx);
71139c13c20SShubham Bansal 	}
71239c13c20SShubham Bansal }
71339c13c20SShubham Bansal 
71439c13c20SShubham Bansal /* dst = dst << val */
71539c13c20SShubham Bansal static inline void emit_a32_lsh_i64(const u8 dst[], bool dstk,
71639c13c20SShubham Bansal 				     const u32 val, struct jit_ctx *ctx){
71739c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
71839c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
71939c13c20SShubham Bansal 	/* Setup operands */
72039c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
72139c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
72239c13c20SShubham Bansal 
72339c13c20SShubham Bansal 	if (dstk) {
72439c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
72539c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
72639c13c20SShubham Bansal 	}
72739c13c20SShubham Bansal 
72839c13c20SShubham Bansal 	/* Do LSH operation */
72939c13c20SShubham Bansal 	if (val < 32) {
73039c13c20SShubham Bansal 		emit(ARM_MOV_SI(tmp2[0], rm, SRTYPE_ASL, val), ctx);
73139c13c20SShubham Bansal 		emit(ARM_ORR_SI(rm, tmp2[0], rd, SRTYPE_LSR, 32 - val), ctx);
73239c13c20SShubham Bansal 		emit(ARM_MOV_SI(rd, rd, SRTYPE_ASL, val), ctx);
73339c13c20SShubham Bansal 	} else {
73439c13c20SShubham Bansal 		if (val == 32)
73539c13c20SShubham Bansal 			emit(ARM_MOV_R(rm, rd), ctx);
73639c13c20SShubham Bansal 		else
73739c13c20SShubham Bansal 			emit(ARM_MOV_SI(rm, rd, SRTYPE_ASL, val - 32), ctx);
73839c13c20SShubham Bansal 		emit(ARM_EOR_R(rd, rd, rd), ctx);
73939c13c20SShubham Bansal 	}
74039c13c20SShubham Bansal 
74139c13c20SShubham Bansal 	if (dstk) {
74239c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
74339c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
74439c13c20SShubham Bansal 	}
74539c13c20SShubham Bansal }
74639c13c20SShubham Bansal 
74739c13c20SShubham Bansal /* dst = dst >> val */
74839c13c20SShubham Bansal static inline void emit_a32_lsr_i64(const u8 dst[], bool dstk,
74939c13c20SShubham Bansal 				    const u32 val, struct jit_ctx *ctx) {
75039c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
75139c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
75239c13c20SShubham Bansal 	/* Setup operands */
75339c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
75439c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
75539c13c20SShubham Bansal 
75639c13c20SShubham Bansal 	if (dstk) {
75739c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
75839c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
75939c13c20SShubham Bansal 	}
76039c13c20SShubham Bansal 
76139c13c20SShubham Bansal 	/* Do LSR operation */
76239c13c20SShubham Bansal 	if (val < 32) {
76339c13c20SShubham Bansal 		emit(ARM_MOV_SI(tmp2[1], rd, SRTYPE_LSR, val), ctx);
76439c13c20SShubham Bansal 		emit(ARM_ORR_SI(rd, tmp2[1], rm, SRTYPE_ASL, 32 - val), ctx);
76539c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_LSR, val), ctx);
76639c13c20SShubham Bansal 	} else if (val == 32) {
76739c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, rm), ctx);
76839c13c20SShubham Bansal 		emit(ARM_MOV_I(rm, 0), ctx);
76939c13c20SShubham Bansal 	} else {
77039c13c20SShubham Bansal 		emit(ARM_MOV_SI(rd, rm, SRTYPE_LSR, val - 32), ctx);
77139c13c20SShubham Bansal 		emit(ARM_MOV_I(rm, 0), ctx);
77239c13c20SShubham Bansal 	}
77339c13c20SShubham Bansal 
77439c13c20SShubham Bansal 	if (dstk) {
77539c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
77639c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
77739c13c20SShubham Bansal 	}
77839c13c20SShubham Bansal }
77939c13c20SShubham Bansal 
78039c13c20SShubham Bansal /* dst = dst >> val (signed) */
78139c13c20SShubham Bansal static inline void emit_a32_arsh_i64(const u8 dst[], bool dstk,
78239c13c20SShubham Bansal 				     const u32 val, struct jit_ctx *ctx){
78339c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
78439c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
78539c13c20SShubham Bansal 	 /* Setup operands */
78639c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
78739c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
78839c13c20SShubham Bansal 
78939c13c20SShubham Bansal 	if (dstk) {
79039c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
79139c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
79239c13c20SShubham Bansal 	}
79339c13c20SShubham Bansal 
79439c13c20SShubham Bansal 	/* Do ARSH operation */
79539c13c20SShubham Bansal 	if (val < 32) {
79639c13c20SShubham Bansal 		emit(ARM_MOV_SI(tmp2[1], rd, SRTYPE_LSR, val), ctx);
79739c13c20SShubham Bansal 		emit(ARM_ORR_SI(rd, tmp2[1], rm, SRTYPE_ASL, 32 - val), ctx);
79839c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_ASR, val), ctx);
79939c13c20SShubham Bansal 	} else if (val == 32) {
80039c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, rm), ctx);
80139c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_ASR, 31), ctx);
80239c13c20SShubham Bansal 	} else {
80339c13c20SShubham Bansal 		emit(ARM_MOV_SI(rd, rm, SRTYPE_ASR, val - 32), ctx);
80439c13c20SShubham Bansal 		emit(ARM_MOV_SI(rm, rm, SRTYPE_ASR, 31), ctx);
80539c13c20SShubham Bansal 	}
80639c13c20SShubham Bansal 
80739c13c20SShubham Bansal 	if (dstk) {
80839c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
80939c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
81039c13c20SShubham Bansal 	}
81139c13c20SShubham Bansal }
81239c13c20SShubham Bansal 
81339c13c20SShubham Bansal static inline void emit_a32_mul_r64(const u8 dst[], const u8 src[], bool dstk,
81439c13c20SShubham Bansal 				    bool sstk, struct jit_ctx *ctx) {
81539c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
81639c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
81739c13c20SShubham Bansal 	/* Setup operands for multiplication */
81839c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst_lo;
81939c13c20SShubham Bansal 	u8 rm = dstk ? tmp[0] : dst_hi;
82039c13c20SShubham Bansal 	u8 rt = sstk ? tmp2[1] : src_lo;
82139c13c20SShubham Bansal 	u8 rn = sstk ? tmp2[0] : src_hi;
82239c13c20SShubham Bansal 
82339c13c20SShubham Bansal 	if (dstk) {
82439c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
82539c13c20SShubham Bansal 		emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
82639c13c20SShubham Bansal 	}
82739c13c20SShubham Bansal 	if (sstk) {
82839c13c20SShubham Bansal 		emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)), ctx);
82939c13c20SShubham Bansal 		emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_hi)), ctx);
83039c13c20SShubham Bansal 	}
83139c13c20SShubham Bansal 
83239c13c20SShubham Bansal 	/* Do Multiplication */
83339c13c20SShubham Bansal 	emit(ARM_MUL(ARM_IP, rd, rn), ctx);
83439c13c20SShubham Bansal 	emit(ARM_MUL(ARM_LR, rm, rt), ctx);
83539c13c20SShubham Bansal 	/* As we are using ARM_LR */
83639c13c20SShubham Bansal 	ctx->seen |= SEEN_CALL;
83739c13c20SShubham Bansal 	emit(ARM_ADD_R(ARM_LR, ARM_IP, ARM_LR), ctx);
83839c13c20SShubham Bansal 
83939c13c20SShubham Bansal 	emit(ARM_UMULL(ARM_IP, rm, rd, rt), ctx);
84039c13c20SShubham Bansal 	emit(ARM_ADD_R(rm, ARM_LR, rm), ctx);
84139c13c20SShubham Bansal 	if (dstk) {
84239c13c20SShubham Bansal 		emit(ARM_STR_I(ARM_IP, ARM_SP, STACK_VAR(dst_lo)), ctx);
84339c13c20SShubham Bansal 		emit(ARM_STR_I(rm, ARM_SP, STACK_VAR(dst_hi)), ctx);
84439c13c20SShubham Bansal 	} else {
84539c13c20SShubham Bansal 		emit(ARM_MOV_R(rd, ARM_IP), ctx);
84639c13c20SShubham Bansal 	}
84739c13c20SShubham Bansal }
84839c13c20SShubham Bansal 
84939c13c20SShubham Bansal /* *(size *)(dst + off) = src */
85039c13c20SShubham Bansal static inline void emit_str_r(const u8 dst, const u8 src, bool dstk,
85139c13c20SShubham Bansal 			      const s32 off, struct jit_ctx *ctx, const u8 sz){
85239c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
85339c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst;
85439c13c20SShubham Bansal 
85539c13c20SShubham Bansal 	if (dstk)
85639c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst)), ctx);
85739c13c20SShubham Bansal 	if (off) {
85839c13c20SShubham Bansal 		emit_a32_mov_i(tmp[0], off, false, ctx);
85939c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], rd, tmp[0]), ctx);
86039c13c20SShubham Bansal 		rd = tmp[0];
86139c13c20SShubham Bansal 	}
86239c13c20SShubham Bansal 	switch (sz) {
86339c13c20SShubham Bansal 	case BPF_W:
86439c13c20SShubham Bansal 		/* Store a Word */
86539c13c20SShubham Bansal 		emit(ARM_STR_I(src, rd, 0), ctx);
86639c13c20SShubham Bansal 		break;
86739c13c20SShubham Bansal 	case BPF_H:
86839c13c20SShubham Bansal 		/* Store a HalfWord */
86939c13c20SShubham Bansal 		emit(ARM_STRH_I(src, rd, 0), ctx);
87039c13c20SShubham Bansal 		break;
87139c13c20SShubham Bansal 	case BPF_B:
87239c13c20SShubham Bansal 		/* Store a Byte */
87339c13c20SShubham Bansal 		emit(ARM_STRB_I(src, rd, 0), ctx);
87439c13c20SShubham Bansal 		break;
87539c13c20SShubham Bansal 	}
87639c13c20SShubham Bansal }
87739c13c20SShubham Bansal 
87839c13c20SShubham Bansal /* dst = *(size*)(src + off) */
87939c13c20SShubham Bansal static inline void emit_ldx_r(const u8 dst, const u8 src, bool dstk,
88039c13c20SShubham Bansal 			      const s32 off, struct jit_ctx *ctx, const u8 sz){
88139c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
88239c13c20SShubham Bansal 	u8 rd = dstk ? tmp[1] : dst;
88339c13c20SShubham Bansal 	u8 rm = src;
88439c13c20SShubham Bansal 
88539c13c20SShubham Bansal 	if (off) {
88639c13c20SShubham Bansal 		emit_a32_mov_i(tmp[0], off, false, ctx);
88739c13c20SShubham Bansal 		emit(ARM_ADD_R(tmp[0], tmp[0], src), ctx);
88839c13c20SShubham Bansal 		rm = tmp[0];
88939c13c20SShubham Bansal 	}
89039c13c20SShubham Bansal 	switch (sz) {
89139c13c20SShubham Bansal 	case BPF_W:
89239c13c20SShubham Bansal 		/* Load a Word */
89339c13c20SShubham Bansal 		emit(ARM_LDR_I(rd, rm, 0), ctx);
89439c13c20SShubham Bansal 		break;
89539c13c20SShubham Bansal 	case BPF_H:
89639c13c20SShubham Bansal 		/* Load a HalfWord */
89739c13c20SShubham Bansal 		emit(ARM_LDRH_I(rd, rm, 0), ctx);
89839c13c20SShubham Bansal 		break;
89939c13c20SShubham Bansal 	case BPF_B:
90039c13c20SShubham Bansal 		/* Load a Byte */
90139c13c20SShubham Bansal 		emit(ARM_LDRB_I(rd, rm, 0), ctx);
90239c13c20SShubham Bansal 		break;
90339c13c20SShubham Bansal 	}
90439c13c20SShubham Bansal 	if (dstk)
90539c13c20SShubham Bansal 		emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst)), ctx);
90639c13c20SShubham Bansal }
90739c13c20SShubham Bansal 
90839c13c20SShubham Bansal /* Arithmatic Operation */
90939c13c20SShubham Bansal static inline void emit_ar_r(const u8 rd, const u8 rt, const u8 rm,
91039c13c20SShubham Bansal 			     const u8 rn, struct jit_ctx *ctx, u8 op) {
91139c13c20SShubham Bansal 	switch (op) {
91239c13c20SShubham Bansal 	case BPF_JSET:
91339c13c20SShubham Bansal 		ctx->seen |= SEEN_CALL;
91439c13c20SShubham Bansal 		emit(ARM_AND_R(ARM_IP, rt, rn), ctx);
91539c13c20SShubham Bansal 		emit(ARM_AND_R(ARM_LR, rd, rm), ctx);
91639c13c20SShubham Bansal 		emit(ARM_ORRS_R(ARM_IP, ARM_LR, ARM_IP), ctx);
91739c13c20SShubham Bansal 		break;
91839c13c20SShubham Bansal 	case BPF_JEQ:
91939c13c20SShubham Bansal 	case BPF_JNE:
92039c13c20SShubham Bansal 	case BPF_JGT:
92139c13c20SShubham Bansal 	case BPF_JGE:
92239c13c20SShubham Bansal 	case BPF_JLE:
92339c13c20SShubham Bansal 	case BPF_JLT:
92439c13c20SShubham Bansal 		emit(ARM_CMP_R(rd, rm), ctx);
92539c13c20SShubham Bansal 		_emit(ARM_COND_EQ, ARM_CMP_R(rt, rn), ctx);
92639c13c20SShubham Bansal 		break;
92739c13c20SShubham Bansal 	case BPF_JSLE:
92839c13c20SShubham Bansal 	case BPF_JSGT:
92939c13c20SShubham Bansal 		emit(ARM_CMP_R(rn, rt), ctx);
93039c13c20SShubham Bansal 		emit(ARM_SBCS_R(ARM_IP, rm, rd), ctx);
93139c13c20SShubham Bansal 		break;
93239c13c20SShubham Bansal 	case BPF_JSLT:
93339c13c20SShubham Bansal 	case BPF_JSGE:
93439c13c20SShubham Bansal 		emit(ARM_CMP_R(rt, rn), ctx);
93539c13c20SShubham Bansal 		emit(ARM_SBCS_R(ARM_IP, rd, rm), ctx);
93639c13c20SShubham Bansal 		break;
93739c13c20SShubham Bansal 	}
93839c13c20SShubham Bansal }
93939c13c20SShubham Bansal 
94039c13c20SShubham Bansal static int out_offset = -1; /* initialized on the first pass of build_body() */
94139c13c20SShubham Bansal static int emit_bpf_tail_call(struct jit_ctx *ctx)
94239c13c20SShubham Bansal {
94339c13c20SShubham Bansal 
94439c13c20SShubham Bansal 	/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
94539c13c20SShubham Bansal 	const u8 *r2 = bpf2a32[BPF_REG_2];
94639c13c20SShubham Bansal 	const u8 *r3 = bpf2a32[BPF_REG_3];
94739c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
94839c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
94939c13c20SShubham Bansal 	const u8 *tcc = bpf2a32[TCALL_CNT];
95039c13c20SShubham Bansal 	const int idx0 = ctx->idx;
95139c13c20SShubham Bansal #define cur_offset (ctx->idx - idx0)
952*f4483f2cSRussell King #define jmp_offset (out_offset - (cur_offset) - 2)
95339c13c20SShubham Bansal 	u32 off, lo, hi;
95439c13c20SShubham Bansal 
95539c13c20SShubham Bansal 	/* if (index >= array->map.max_entries)
95639c13c20SShubham Bansal 	 *	goto out;
95739c13c20SShubham Bansal 	 */
95839c13c20SShubham Bansal 	off = offsetof(struct bpf_array, map.max_entries);
95939c13c20SShubham Bansal 	/* array->map.max_entries */
96039c13c20SShubham Bansal 	emit_a32_mov_i(tmp[1], off, false, ctx);
96139c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r2[1])), ctx);
96239c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp2[1], tmp[1]), ctx);
96339c13c20SShubham Bansal 	/* index (64 bit) */
96439c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r3[1])), ctx);
96539c13c20SShubham Bansal 	/* index >= array->map.max_entries */
96639c13c20SShubham Bansal 	emit(ARM_CMP_R(tmp2[1], tmp[1]), ctx);
96739c13c20SShubham Bansal 	_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
96839c13c20SShubham Bansal 
96939c13c20SShubham Bansal 	/* if (tail_call_cnt > MAX_TAIL_CALL_CNT)
97039c13c20SShubham Bansal 	 *	goto out;
97139c13c20SShubham Bansal 	 * tail_call_cnt++;
97239c13c20SShubham Bansal 	 */
97339c13c20SShubham Bansal 	lo = (u32)MAX_TAIL_CALL_CNT;
97439c13c20SShubham Bansal 	hi = (u32)((u64)MAX_TAIL_CALL_CNT >> 32);
97539c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp[1], ARM_SP, STACK_VAR(tcc[1])), ctx);
97639c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp[0], ARM_SP, STACK_VAR(tcc[0])), ctx);
97739c13c20SShubham Bansal 	emit(ARM_CMP_I(tmp[0], hi), ctx);
97839c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_CMP_I(tmp[1], lo), ctx);
97939c13c20SShubham Bansal 	_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
98039c13c20SShubham Bansal 	emit(ARM_ADDS_I(tmp[1], tmp[1], 1), ctx);
98139c13c20SShubham Bansal 	emit(ARM_ADC_I(tmp[0], tmp[0], 0), ctx);
98239c13c20SShubham Bansal 	emit(ARM_STR_I(tmp[1], ARM_SP, STACK_VAR(tcc[1])), ctx);
98339c13c20SShubham Bansal 	emit(ARM_STR_I(tmp[0], ARM_SP, STACK_VAR(tcc[0])), ctx);
98439c13c20SShubham Bansal 
98539c13c20SShubham Bansal 	/* prog = array->ptrs[index]
98639c13c20SShubham Bansal 	 * if (prog == NULL)
98739c13c20SShubham Bansal 	 *	goto out;
98839c13c20SShubham Bansal 	 */
98939c13c20SShubham Bansal 	off = offsetof(struct bpf_array, ptrs);
99039c13c20SShubham Bansal 	emit_a32_mov_i(tmp[1], off, false, ctx);
99139c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r2[1])), ctx);
99239c13c20SShubham Bansal 	emit(ARM_ADD_R(tmp[1], tmp2[1], tmp[1]), ctx);
99339c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(r3[1])), ctx);
99439c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp[0], tmp2[1], SRTYPE_ASL, 2), ctx);
99539c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp[1], tmp[0]), ctx);
99639c13c20SShubham Bansal 	emit(ARM_CMP_I(tmp[1], 0), ctx);
99739c13c20SShubham Bansal 	_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
99839c13c20SShubham Bansal 
99939c13c20SShubham Bansal 	/* goto *(prog->bpf_func + prologue_size); */
100039c13c20SShubham Bansal 	off = offsetof(struct bpf_prog, bpf_func);
100139c13c20SShubham Bansal 	emit_a32_mov_i(tmp2[1], off, false, ctx);
100239c13c20SShubham Bansal 	emit(ARM_LDR_R(tmp[1], tmp[1], tmp2[1]), ctx);
100339c13c20SShubham Bansal 	emit(ARM_ADD_I(tmp[1], tmp[1], ctx->prologue_bytes), ctx);
1004e9062481SRussell King 	emit_bx_r(tmp[1], ctx);
100539c13c20SShubham Bansal 
100639c13c20SShubham Bansal 	/* out: */
100739c13c20SShubham Bansal 	if (out_offset == -1)
100839c13c20SShubham Bansal 		out_offset = cur_offset;
100939c13c20SShubham Bansal 	if (cur_offset != out_offset) {
101039c13c20SShubham Bansal 		pr_err_once("tail_call out_offset = %d, expected %d!\n",
101139c13c20SShubham Bansal 			    cur_offset, out_offset);
101239c13c20SShubham Bansal 		return -1;
101339c13c20SShubham Bansal 	}
101439c13c20SShubham Bansal 	return 0;
101539c13c20SShubham Bansal #undef cur_offset
101639c13c20SShubham Bansal #undef jmp_offset
101739c13c20SShubham Bansal }
101839c13c20SShubham Bansal 
101939c13c20SShubham Bansal /* 0xabcd => 0xcdab */
102039c13c20SShubham Bansal static inline void emit_rev16(const u8 rd, const u8 rn, struct jit_ctx *ctx)
102139c13c20SShubham Bansal {
102239c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
102339c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
102439c13c20SShubham Bansal 
102539c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
102639c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 8), ctx);
102739c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
102839c13c20SShubham Bansal 	emit(ARM_ORR_SI(rd, tmp2[0], tmp2[1], SRTYPE_LSL, 8), ctx);
102939c13c20SShubham Bansal #else /* ARMv6+ */
103039c13c20SShubham Bansal 	emit(ARM_REV16(rd, rn), ctx);
103139c13c20SShubham Bansal #endif
103239c13c20SShubham Bansal }
103339c13c20SShubham Bansal 
103439c13c20SShubham Bansal /* 0xabcdefgh => 0xghefcdab */
103539c13c20SShubham Bansal static inline void emit_rev32(const u8 rd, const u8 rn, struct jit_ctx *ctx)
103639c13c20SShubham Bansal {
103739c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
103839c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
103939c13c20SShubham Bansal 
104039c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], rn, 0xff), ctx);
104139c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 24), ctx);
104239c13c20SShubham Bansal 	emit(ARM_ORR_SI(ARM_IP, tmp2[0], tmp2[1], SRTYPE_LSL, 24), ctx);
104339c13c20SShubham Bansal 
104439c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[1], rn, SRTYPE_LSR, 8), ctx);
104539c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[1], tmp2[1], 0xff), ctx);
104639c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], rn, SRTYPE_LSR, 16), ctx);
104739c13c20SShubham Bansal 	emit(ARM_AND_I(tmp2[0], tmp2[0], 0xff), ctx);
104839c13c20SShubham Bansal 	emit(ARM_MOV_SI(tmp2[0], tmp2[0], SRTYPE_LSL, 8), ctx);
104939c13c20SShubham Bansal 	emit(ARM_ORR_SI(tmp2[0], tmp2[0], tmp2[1], SRTYPE_LSL, 16), ctx);
105039c13c20SShubham Bansal 	emit(ARM_ORR_R(rd, ARM_IP, tmp2[0]), ctx);
105139c13c20SShubham Bansal 
105239c13c20SShubham Bansal #else /* ARMv6+ */
105339c13c20SShubham Bansal 	emit(ARM_REV(rd, rn), ctx);
105439c13c20SShubham Bansal #endif
105539c13c20SShubham Bansal }
105639c13c20SShubham Bansal 
105739c13c20SShubham Bansal // push the scratch stack register on top of the stack
105839c13c20SShubham Bansal static inline void emit_push_r64(const u8 src[], const u8 shift,
105939c13c20SShubham Bansal 		struct jit_ctx *ctx)
106039c13c20SShubham Bansal {
106139c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
106239c13c20SShubham Bansal 	u16 reg_set = 0;
106339c13c20SShubham Bansal 
106439c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[1], ARM_SP, STACK_VAR(src[1]+shift)), ctx);
106539c13c20SShubham Bansal 	emit(ARM_LDR_I(tmp2[0], ARM_SP, STACK_VAR(src[0]+shift)), ctx);
106639c13c20SShubham Bansal 
106739c13c20SShubham Bansal 	reg_set = (1 << tmp2[1]) | (1 << tmp2[0]);
106839c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
106939c13c20SShubham Bansal }
107039c13c20SShubham Bansal 
107139c13c20SShubham Bansal static void build_prologue(struct jit_ctx *ctx)
107239c13c20SShubham Bansal {
107339c13c20SShubham Bansal 	const u8 r0 = bpf2a32[BPF_REG_0][1];
107439c13c20SShubham Bansal 	const u8 r2 = bpf2a32[BPF_REG_1][1];
107539c13c20SShubham Bansal 	const u8 r3 = bpf2a32[BPF_REG_1][0];
107639c13c20SShubham Bansal 	const u8 r4 = bpf2a32[BPF_REG_6][1];
107739c13c20SShubham Bansal 	const u8 r5 = bpf2a32[BPF_REG_6][0];
107839c13c20SShubham Bansal 	const u8 r6 = bpf2a32[TMP_REG_1][1];
107939c13c20SShubham Bansal 	const u8 r7 = bpf2a32[TMP_REG_1][0];
108039c13c20SShubham Bansal 	const u8 r8 = bpf2a32[TMP_REG_2][1];
108139c13c20SShubham Bansal 	const u8 r10 = bpf2a32[TMP_REG_2][0];
108239c13c20SShubham Bansal 	const u8 fplo = bpf2a32[BPF_REG_FP][1];
108339c13c20SShubham Bansal 	const u8 fphi = bpf2a32[BPF_REG_FP][0];
108439c13c20SShubham Bansal 	const u8 sp = ARM_SP;
108539c13c20SShubham Bansal 	const u8 *tcc = bpf2a32[TCALL_CNT];
108639c13c20SShubham Bansal 
108739c13c20SShubham Bansal 	u16 reg_set = 0;
108839c13c20SShubham Bansal 
108939c13c20SShubham Bansal 	/*
109039c13c20SShubham Bansal 	 * eBPF prog stack layout
109139c13c20SShubham Bansal 	 *
109239c13c20SShubham Bansal 	 *                         high
109339c13c20SShubham Bansal 	 * original ARM_SP =>     +-----+ eBPF prologue
109439c13c20SShubham Bansal 	 *                        |FP/LR|
109539c13c20SShubham Bansal 	 * current ARM_FP =>      +-----+
109639c13c20SShubham Bansal 	 *                        | ... | callee saved registers
109739c13c20SShubham Bansal 	 * eBPF fp register =>    +-----+ <= (BPF_FP)
109839c13c20SShubham Bansal 	 *                        | ... | eBPF JIT scratch space
109939c13c20SShubham Bansal 	 *                        |     | eBPF prog stack
110039c13c20SShubham Bansal 	 *                        +-----+
110139c13c20SShubham Bansal 	 *			  |RSVD | JIT scratchpad
110239c13c20SShubham Bansal 	 * current A64_SP =>      +-----+ <= (BPF_FP - STACK_SIZE)
110339c13c20SShubham Bansal 	 *                        |     |
110439c13c20SShubham Bansal 	 *                        | ... | Function call stack
110539c13c20SShubham Bansal 	 *                        |     |
110639c13c20SShubham Bansal 	 *                        +-----+
110739c13c20SShubham Bansal 	 *                          low
110839c13c20SShubham Bansal 	 */
110939c13c20SShubham Bansal 
111039c13c20SShubham Bansal 	/* Save callee saved registers. */
111139c13c20SShubham Bansal 	reg_set |= (1<<r4) | (1<<r5) | (1<<r6) | (1<<r7) | (1<<r8) | (1<<r10);
111239c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
111339c13c20SShubham Bansal 	reg_set |= (1<<ARM_FP) | (1<<ARM_IP) | (1<<ARM_LR) | (1<<ARM_PC);
111439c13c20SShubham Bansal 	emit(ARM_MOV_R(ARM_IP, sp), ctx);
111539c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
111639c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
111739c13c20SShubham Bansal #else
111839c13c20SShubham Bansal 	/* Check if call instruction exists in BPF body */
111939c13c20SShubham Bansal 	if (ctx->seen & SEEN_CALL)
112039c13c20SShubham Bansal 		reg_set |= (1<<ARM_LR);
112139c13c20SShubham Bansal 	emit(ARM_PUSH(reg_set), ctx);
112239c13c20SShubham Bansal #endif
112339c13c20SShubham Bansal 	/* Save frame pointer for later */
112439c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_IP, sp, SCRATCH_SIZE), ctx);
112539c13c20SShubham Bansal 
112639c13c20SShubham Bansal 	ctx->stack_size = imm8m(STACK_SIZE);
112739c13c20SShubham Bansal 
112839c13c20SShubham Bansal 	/* Set up function call stack */
112939c13c20SShubham Bansal 	emit(ARM_SUB_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
113039c13c20SShubham Bansal 
113139c13c20SShubham Bansal 	/* Set up BPF prog stack base register */
113239c13c20SShubham Bansal 	emit_a32_mov_r(fplo, ARM_IP, true, false, ctx);
113339c13c20SShubham Bansal 	emit_a32_mov_i(fphi, 0, true, ctx);
113439c13c20SShubham Bansal 
113539c13c20SShubham Bansal 	/* mov r4, 0 */
113639c13c20SShubham Bansal 	emit(ARM_MOV_I(r4, 0), ctx);
113739c13c20SShubham Bansal 
113839c13c20SShubham Bansal 	/* Move BPF_CTX to BPF_R1 */
113939c13c20SShubham Bansal 	emit(ARM_MOV_R(r3, r4), ctx);
114039c13c20SShubham Bansal 	emit(ARM_MOV_R(r2, r0), ctx);
114139c13c20SShubham Bansal 	/* Initialize Tail Count */
114239c13c20SShubham Bansal 	emit(ARM_STR_I(r4, ARM_SP, STACK_VAR(tcc[0])), ctx);
114339c13c20SShubham Bansal 	emit(ARM_STR_I(r4, ARM_SP, STACK_VAR(tcc[1])), ctx);
114439c13c20SShubham Bansal 	/* end of prologue */
114539c13c20SShubham Bansal }
114639c13c20SShubham Bansal 
114739c13c20SShubham Bansal static void build_epilogue(struct jit_ctx *ctx)
114839c13c20SShubham Bansal {
114939c13c20SShubham Bansal 	const u8 r4 = bpf2a32[BPF_REG_6][1];
115039c13c20SShubham Bansal 	const u8 r5 = bpf2a32[BPF_REG_6][0];
115139c13c20SShubham Bansal 	const u8 r6 = bpf2a32[TMP_REG_1][1];
115239c13c20SShubham Bansal 	const u8 r7 = bpf2a32[TMP_REG_1][0];
115339c13c20SShubham Bansal 	const u8 r8 = bpf2a32[TMP_REG_2][1];
115439c13c20SShubham Bansal 	const u8 r10 = bpf2a32[TMP_REG_2][0];
115539c13c20SShubham Bansal 	u16 reg_set = 0;
115639c13c20SShubham Bansal 
115739c13c20SShubham Bansal 	/* unwind function call stack */
115839c13c20SShubham Bansal 	emit(ARM_ADD_I(ARM_SP, ARM_SP, ctx->stack_size), ctx);
115939c13c20SShubham Bansal 
116039c13c20SShubham Bansal 	/* restore callee saved registers. */
116139c13c20SShubham Bansal 	reg_set |= (1<<r4) | (1<<r5) | (1<<r6) | (1<<r7) | (1<<r8) | (1<<r10);
116239c13c20SShubham Bansal #ifdef CONFIG_FRAME_POINTER
116339c13c20SShubham Bansal 	/* the first instruction of the prologue was: mov ip, sp */
116439c13c20SShubham Bansal 	reg_set |= (1<<ARM_FP) | (1<<ARM_SP) | (1<<ARM_PC);
116539c13c20SShubham Bansal 	emit(ARM_LDM(ARM_SP, reg_set), ctx);
116639c13c20SShubham Bansal #else
116739c13c20SShubham Bansal 	if (ctx->seen & SEEN_CALL)
116839c13c20SShubham Bansal 		reg_set |= (1<<ARM_PC);
116939c13c20SShubham Bansal 	/* Restore callee saved registers. */
117039c13c20SShubham Bansal 	emit(ARM_POP(reg_set), ctx);
117139c13c20SShubham Bansal 	/* Return back to the callee function */
117239c13c20SShubham Bansal 	if (!(ctx->seen & SEEN_CALL))
1173e9062481SRussell King 		emit_bx_r(ARM_LR, ctx);
117439c13c20SShubham Bansal #endif
117539c13c20SShubham Bansal }
117639c13c20SShubham Bansal 
117739c13c20SShubham Bansal /*
117839c13c20SShubham Bansal  * Convert an eBPF instruction to native instruction, i.e
117939c13c20SShubham Bansal  * JITs an eBPF instruction.
118039c13c20SShubham Bansal  * Returns :
118139c13c20SShubham Bansal  *	0  - Successfully JITed an 8-byte eBPF instruction
118239c13c20SShubham Bansal  *	>0 - Successfully JITed a 16-byte eBPF instruction
118339c13c20SShubham Bansal  *	<0 - Failed to JIT.
118439c13c20SShubham Bansal  */
118539c13c20SShubham Bansal static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
118639c13c20SShubham Bansal {
118739c13c20SShubham Bansal 	const u8 code = insn->code;
118839c13c20SShubham Bansal 	const u8 *dst = bpf2a32[insn->dst_reg];
118939c13c20SShubham Bansal 	const u8 *src = bpf2a32[insn->src_reg];
119039c13c20SShubham Bansal 	const u8 *tmp = bpf2a32[TMP_REG_1];
119139c13c20SShubham Bansal 	const u8 *tmp2 = bpf2a32[TMP_REG_2];
119239c13c20SShubham Bansal 	const s16 off = insn->off;
119339c13c20SShubham Bansal 	const s32 imm = insn->imm;
119439c13c20SShubham Bansal 	const int i = insn - ctx->prog->insnsi;
119539c13c20SShubham Bansal 	const bool is64 = BPF_CLASS(code) == BPF_ALU64;
119639c13c20SShubham Bansal 	const bool dstk = is_on_stack(insn->dst_reg);
119739c13c20SShubham Bansal 	const bool sstk = is_on_stack(insn->src_reg);
119839c13c20SShubham Bansal 	u8 rd, rt, rm, rn;
119939c13c20SShubham Bansal 	s32 jmp_offset;
120039c13c20SShubham Bansal 
120139c13c20SShubham Bansal #define check_imm(bits, imm) do {				\
120239c13c20SShubham Bansal 	if ((((imm) > 0) && ((imm) >> (bits))) ||		\
120339c13c20SShubham Bansal 	    (((imm) < 0) && (~(imm) >> (bits)))) {		\
120439c13c20SShubham Bansal 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
120539c13c20SShubham Bansal 			i, imm, imm);				\
120639c13c20SShubham Bansal 		return -EINVAL;					\
120739c13c20SShubham Bansal 	}							\
120839c13c20SShubham Bansal } while (0)
120939c13c20SShubham Bansal #define check_imm24(imm) check_imm(24, imm)
1210ddecdfceSMircea Gherzan 
121134805931SDaniel Borkmann 	switch (code) {
121239c13c20SShubham Bansal 	/* ALU operations */
1213ddecdfceSMircea Gherzan 
121439c13c20SShubham Bansal 	/* dst = src */
121539c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_K:
121639c13c20SShubham Bansal 	case BPF_ALU | BPF_MOV | BPF_X:
121739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_K:
121839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOV | BPF_X:
121939c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
122039c13c20SShubham Bansal 		case BPF_X:
122139c13c20SShubham Bansal 			emit_a32_mov_r64(is64, dst, src, dstk, sstk, ctx);
122239c13c20SShubham Bansal 			break;
122339c13c20SShubham Bansal 		case BPF_K:
122439c13c20SShubham Bansal 			/* Sign-extend immediate value to destination reg */
122539c13c20SShubham Bansal 			emit_a32_mov_i64(is64, dst, imm, dstk, ctx);
122639c13c20SShubham Bansal 			break;
1227ddecdfceSMircea Gherzan 		}
1228ddecdfceSMircea Gherzan 		break;
122939c13c20SShubham Bansal 	/* dst = dst + src/imm */
123039c13c20SShubham Bansal 	/* dst = dst - src/imm */
123139c13c20SShubham Bansal 	/* dst = dst | src/imm */
123239c13c20SShubham Bansal 	/* dst = dst & src/imm */
123339c13c20SShubham Bansal 	/* dst = dst ^ src/imm */
123439c13c20SShubham Bansal 	/* dst = dst * src/imm */
123539c13c20SShubham Bansal 	/* dst = dst << src */
123639c13c20SShubham Bansal 	/* dst = dst >> src */
123734805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_K:
123834805931SDaniel Borkmann 	case BPF_ALU | BPF_ADD | BPF_X:
123934805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_K:
124034805931SDaniel Borkmann 	case BPF_ALU | BPF_SUB | BPF_X:
124134805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_K:
124234805931SDaniel Borkmann 	case BPF_ALU | BPF_OR | BPF_X:
124334805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_K:
124434805931SDaniel Borkmann 	case BPF_ALU | BPF_AND | BPF_X:
124539c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_K:
124639c13c20SShubham Bansal 	case BPF_ALU | BPF_XOR | BPF_X:
124739c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_K:
124839c13c20SShubham Bansal 	case BPF_ALU | BPF_MUL | BPF_X:
124934805931SDaniel Borkmann 	case BPF_ALU | BPF_LSH | BPF_X:
125034805931SDaniel Borkmann 	case BPF_ALU | BPF_RSH | BPF_X:
125139c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_K:
125239c13c20SShubham Bansal 	case BPF_ALU | BPF_ARSH | BPF_X:
125339c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_K:
125439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ADD | BPF_X:
125539c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_K:
125639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_SUB | BPF_X:
125739c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_K:
125839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_OR | BPF_X:
125939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_K:
126039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_AND | BPF_X:
126139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_K:
126239c13c20SShubham Bansal 	case BPF_ALU64 | BPF_XOR | BPF_X:
126339c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
126439c13c20SShubham Bansal 		case BPF_X:
126539c13c20SShubham Bansal 			emit_a32_alu_r64(is64, dst, src, dstk, sstk,
126639c13c20SShubham Bansal 					 ctx, BPF_OP(code));
1267ddecdfceSMircea Gherzan 			break;
126839c13c20SShubham Bansal 		case BPF_K:
126939c13c20SShubham Bansal 			/* Move immediate value to the temporary register
127039c13c20SShubham Bansal 			 * and then do the ALU operation on the temporary
127139c13c20SShubham Bansal 			 * register as this will sign-extend the immediate
127239c13c20SShubham Bansal 			 * value into temporary reg and then it would be
127339c13c20SShubham Bansal 			 * safe to do the operation on it.
127439c13c20SShubham Bansal 			 */
127539c13c20SShubham Bansal 			emit_a32_mov_i64(is64, tmp2, imm, false, ctx);
127639c13c20SShubham Bansal 			emit_a32_alu_r64(is64, dst, tmp2, dstk, false,
127739c13c20SShubham Bansal 					 ctx, BPF_OP(code));
127839c13c20SShubham Bansal 			break;
127939c13c20SShubham Bansal 		}
128039c13c20SShubham Bansal 		break;
128139c13c20SShubham Bansal 	/* dst = dst / src(imm) */
128239c13c20SShubham Bansal 	/* dst = dst % src(imm) */
128339c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_K:
128439c13c20SShubham Bansal 	case BPF_ALU | BPF_DIV | BPF_X:
128539c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_K:
128639c13c20SShubham Bansal 	case BPF_ALU | BPF_MOD | BPF_X:
128739c13c20SShubham Bansal 		rt = src_lo;
128839c13c20SShubham Bansal 		rd = dstk ? tmp2[1] : dst_lo;
128939c13c20SShubham Bansal 		if (dstk)
129039c13c20SShubham Bansal 			emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
129139c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
129239c13c20SShubham Bansal 		case BPF_X:
129339c13c20SShubham Bansal 			rt = sstk ? tmp2[0] : rt;
129439c13c20SShubham Bansal 			if (sstk)
129539c13c20SShubham Bansal 				emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(src_lo)),
129639c13c20SShubham Bansal 				     ctx);
129739c13c20SShubham Bansal 			break;
129839c13c20SShubham Bansal 		case BPF_K:
129939c13c20SShubham Bansal 			rt = tmp2[0];
130039c13c20SShubham Bansal 			emit_a32_mov_i(rt, imm, false, ctx);
130139c13c20SShubham Bansal 			break;
130239c13c20SShubham Bansal 		}
130339c13c20SShubham Bansal 		emit_udivmod(rd, rd, rt, ctx, BPF_OP(code));
130439c13c20SShubham Bansal 		if (dstk)
130539c13c20SShubham Bansal 			emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_lo)), ctx);
130639c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
130739c13c20SShubham Bansal 		break;
130839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_K:
130939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_DIV | BPF_X:
131039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_K:
131139c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MOD | BPF_X:
131239c13c20SShubham Bansal 		goto notyet;
131339c13c20SShubham Bansal 	/* dst = dst >> imm */
131439c13c20SShubham Bansal 	/* dst = dst << imm */
131539c13c20SShubham Bansal 	case BPF_ALU | BPF_RSH | BPF_K:
131639c13c20SShubham Bansal 	case BPF_ALU | BPF_LSH | BPF_K:
131739c13c20SShubham Bansal 		if (unlikely(imm > 31))
131839c13c20SShubham Bansal 			return -EINVAL;
131939c13c20SShubham Bansal 		if (imm)
132039c13c20SShubham Bansal 			emit_a32_alu_i(dst_lo, imm, dstk, ctx, BPF_OP(code));
132139c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
132239c13c20SShubham Bansal 		break;
132339c13c20SShubham Bansal 	/* dst = dst << imm */
132439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_K:
132539c13c20SShubham Bansal 		if (unlikely(imm > 63))
132639c13c20SShubham Bansal 			return -EINVAL;
132739c13c20SShubham Bansal 		emit_a32_lsh_i64(dst, dstk, imm, ctx);
132839c13c20SShubham Bansal 		break;
132939c13c20SShubham Bansal 	/* dst = dst >> imm */
133039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_K:
133139c13c20SShubham Bansal 		if (unlikely(imm > 63))
133239c13c20SShubham Bansal 			return -EINVAL;
133339c13c20SShubham Bansal 		emit_a32_lsr_i64(dst, dstk, imm, ctx);
133439c13c20SShubham Bansal 		break;
133539c13c20SShubham Bansal 	/* dst = dst << src */
133639c13c20SShubham Bansal 	case BPF_ALU64 | BPF_LSH | BPF_X:
133739c13c20SShubham Bansal 		emit_a32_lsh_r64(dst, src, dstk, sstk, ctx);
133839c13c20SShubham Bansal 		break;
133939c13c20SShubham Bansal 	/* dst = dst >> src */
134039c13c20SShubham Bansal 	case BPF_ALU64 | BPF_RSH | BPF_X:
134139c13c20SShubham Bansal 		emit_a32_lsr_r64(dst, src, dstk, sstk, ctx);
134239c13c20SShubham Bansal 		break;
134339c13c20SShubham Bansal 	/* dst = dst >> src (signed) */
134439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_X:
134539c13c20SShubham Bansal 		emit_a32_arsh_r64(dst, src, dstk, sstk, ctx);
134639c13c20SShubham Bansal 		break;
134739c13c20SShubham Bansal 	/* dst = dst >> imm (signed) */
134839c13c20SShubham Bansal 	case BPF_ALU64 | BPF_ARSH | BPF_K:
134939c13c20SShubham Bansal 		if (unlikely(imm > 63))
135039c13c20SShubham Bansal 			return -EINVAL;
135139c13c20SShubham Bansal 		emit_a32_arsh_i64(dst, dstk, imm, ctx);
135239c13c20SShubham Bansal 		break;
135339c13c20SShubham Bansal 	/* dst = ~dst */
135434805931SDaniel Borkmann 	case BPF_ALU | BPF_NEG:
135539c13c20SShubham Bansal 		emit_a32_alu_i(dst_lo, 0, dstk, ctx, BPF_OP(code));
135639c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, 0, dstk, ctx);
1357ddecdfceSMircea Gherzan 		break;
135839c13c20SShubham Bansal 	/* dst = ~dst (64 bit) */
135939c13c20SShubham Bansal 	case BPF_ALU64 | BPF_NEG:
136039c13c20SShubham Bansal 		emit_a32_neg64(dst, dstk, ctx);
1361ddecdfceSMircea Gherzan 		break;
136239c13c20SShubham Bansal 	/* dst = dst * src/imm */
136339c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_X:
136439c13c20SShubham Bansal 	case BPF_ALU64 | BPF_MUL | BPF_K:
136539c13c20SShubham Bansal 		switch (BPF_SRC(code)) {
136639c13c20SShubham Bansal 		case BPF_X:
136739c13c20SShubham Bansal 			emit_a32_mul_r64(dst, src, dstk, sstk, ctx);
1368ddecdfceSMircea Gherzan 			break;
136939c13c20SShubham Bansal 		case BPF_K:
137039c13c20SShubham Bansal 			/* Move immediate value to the temporary register
137139c13c20SShubham Bansal 			 * and then do the multiplication on it as this
137239c13c20SShubham Bansal 			 * will sign-extend the immediate value into temp
137339c13c20SShubham Bansal 			 * reg then it would be safe to do the operation
137439c13c20SShubham Bansal 			 * on it.
13755bf705b4SNicolas Schichan 			 */
137639c13c20SShubham Bansal 			emit_a32_mov_i64(is64, tmp2, imm, false, ctx);
137739c13c20SShubham Bansal 			emit_a32_mul_r64(dst, tmp2, dstk, false, ctx);
137839c13c20SShubham Bansal 			break;
13795bf705b4SNicolas Schichan 		}
1380ddecdfceSMircea Gherzan 		break;
138139c13c20SShubham Bansal 	/* dst = htole(dst) */
138239c13c20SShubham Bansal 	/* dst = htobe(dst) */
138339c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_LE:
138439c13c20SShubham Bansal 	case BPF_ALU | BPF_END | BPF_FROM_BE:
138539c13c20SShubham Bansal 		rd = dstk ? tmp[0] : dst_hi;
138639c13c20SShubham Bansal 		rt = dstk ? tmp[1] : dst_lo;
138739c13c20SShubham Bansal 		if (dstk) {
138839c13c20SShubham Bansal 			emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(dst_lo)), ctx);
138939c13c20SShubham Bansal 			emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_hi)), ctx);
1390c18fe54bSNicolas Schichan 		}
139139c13c20SShubham Bansal 		if (BPF_SRC(code) == BPF_FROM_LE)
139239c13c20SShubham Bansal 			goto emit_bswap_uxt;
139339c13c20SShubham Bansal 		switch (imm) {
139439c13c20SShubham Bansal 		case 16:
139539c13c20SShubham Bansal 			emit_rev16(rt, rt, ctx);
139639c13c20SShubham Bansal 			goto emit_bswap_uxt;
139739c13c20SShubham Bansal 		case 32:
139839c13c20SShubham Bansal 			emit_rev32(rt, rt, ctx);
139939c13c20SShubham Bansal 			goto emit_bswap_uxt;
140039c13c20SShubham Bansal 		case 64:
140139c13c20SShubham Bansal 			/* Because of the usage of ARM_LR */
140239c13c20SShubham Bansal 			ctx->seen |= SEEN_CALL;
140339c13c20SShubham Bansal 			emit_rev32(ARM_LR, rt, ctx);
140439c13c20SShubham Bansal 			emit_rev32(rt, rd, ctx);
140539c13c20SShubham Bansal 			emit(ARM_MOV_R(rd, ARM_LR), ctx);
1406bf0098f2SDaniel Borkmann 			break;
140739c13c20SShubham Bansal 		}
140839c13c20SShubham Bansal 		goto exit;
140939c13c20SShubham Bansal emit_bswap_uxt:
141039c13c20SShubham Bansal 		switch (imm) {
141139c13c20SShubham Bansal 		case 16:
141239c13c20SShubham Bansal 			/* zero-extend 16 bits into 64 bits */
141339c13c20SShubham Bansal #if __LINUX_ARM_ARCH__ < 6
141439c13c20SShubham Bansal 			emit_a32_mov_i(tmp2[1], 0xffff, false, ctx);
141539c13c20SShubham Bansal 			emit(ARM_AND_R(rt, rt, tmp2[1]), ctx);
141639c13c20SShubham Bansal #else /* ARMv6+ */
141739c13c20SShubham Bansal 			emit(ARM_UXTH(rt, rt), ctx);
14181447f93fSNicolas Schichan #endif
141939c13c20SShubham Bansal 			emit(ARM_EOR_R(rd, rd, rd), ctx);
14201447f93fSNicolas Schichan 			break;
142139c13c20SShubham Bansal 		case 32:
142239c13c20SShubham Bansal 			/* zero-extend 32 bits into 64 bits */
142339c13c20SShubham Bansal 			emit(ARM_EOR_R(rd, rd, rd), ctx);
1424ddecdfceSMircea Gherzan 			break;
142539c13c20SShubham Bansal 		case 64:
142639c13c20SShubham Bansal 			/* nop */
142739c13c20SShubham Bansal 			break;
142839c13c20SShubham Bansal 		}
142939c13c20SShubham Bansal exit:
143039c13c20SShubham Bansal 		if (dstk) {
143139c13c20SShubham Bansal 			emit(ARM_STR_I(rt, ARM_SP, STACK_VAR(dst_lo)), ctx);
143239c13c20SShubham Bansal 			emit(ARM_STR_I(rd, ARM_SP, STACK_VAR(dst_hi)), ctx);
143339c13c20SShubham Bansal 		}
143439c13c20SShubham Bansal 		break;
143539c13c20SShubham Bansal 	/* dst = imm64 */
143639c13c20SShubham Bansal 	case BPF_LD | BPF_IMM | BPF_DW:
143739c13c20SShubham Bansal 	{
143839c13c20SShubham Bansal 		const struct bpf_insn insn1 = insn[1];
143939c13c20SShubham Bansal 		u32 hi, lo = imm;
1440303249abSNicolas Schichan 
144139c13c20SShubham Bansal 		hi = insn1.imm;
144239c13c20SShubham Bansal 		emit_a32_mov_i(dst_lo, lo, dstk, ctx);
144339c13c20SShubham Bansal 		emit_a32_mov_i(dst_hi, hi, dstk, ctx);
144439c13c20SShubham Bansal 
144539c13c20SShubham Bansal 		return 1;
144639c13c20SShubham Bansal 	}
144739c13c20SShubham Bansal 	/* LDX: dst = *(size *)(src + off) */
144839c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_W:
144939c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_H:
145039c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_B:
145139c13c20SShubham Bansal 	case BPF_LDX | BPF_MEM | BPF_DW:
145239c13c20SShubham Bansal 		rn = sstk ? tmp2[1] : src_lo;
145339c13c20SShubham Bansal 		if (sstk)
145439c13c20SShubham Bansal 			emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_lo)), ctx);
145539c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
145639c13c20SShubham Bansal 		case BPF_W:
145739c13c20SShubham Bansal 			/* Load a Word */
145839c13c20SShubham Bansal 		case BPF_H:
145939c13c20SShubham Bansal 			/* Load a Half-Word */
146039c13c20SShubham Bansal 		case BPF_B:
146139c13c20SShubham Bansal 			/* Load a Byte */
146239c13c20SShubham Bansal 			emit_ldx_r(dst_lo, rn, dstk, off, ctx, BPF_SIZE(code));
146339c13c20SShubham Bansal 			emit_a32_mov_i(dst_hi, 0, dstk, ctx);
1464303249abSNicolas Schichan 			break;
146539c13c20SShubham Bansal 		case BPF_DW:
146639c13c20SShubham Bansal 			/* Load a double word */
146739c13c20SShubham Bansal 			emit_ldx_r(dst_lo, rn, dstk, off, ctx, BPF_W);
146839c13c20SShubham Bansal 			emit_ldx_r(dst_hi, rn, dstk, off+4, ctx, BPF_W);
146939c13c20SShubham Bansal 			break;
147039c13c20SShubham Bansal 		}
147139c13c20SShubham Bansal 		break;
147239c13c20SShubham Bansal 	/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */
147339c13c20SShubham Bansal 	case BPF_LD | BPF_ABS | BPF_W:
147439c13c20SShubham Bansal 	case BPF_LD | BPF_ABS | BPF_H:
147539c13c20SShubham Bansal 	case BPF_LD | BPF_ABS | BPF_B:
147639c13c20SShubham Bansal 	/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */
147739c13c20SShubham Bansal 	case BPF_LD | BPF_IND | BPF_W:
147839c13c20SShubham Bansal 	case BPF_LD | BPF_IND | BPF_H:
147939c13c20SShubham Bansal 	case BPF_LD | BPF_IND | BPF_B:
148039c13c20SShubham Bansal 	{
148139c13c20SShubham Bansal 		const u8 r4 = bpf2a32[BPF_REG_6][1]; /* r4 = ptr to sk_buff */
148239c13c20SShubham Bansal 		const u8 r0 = bpf2a32[BPF_REG_0][1]; /*r0: struct sk_buff *skb*/
148339c13c20SShubham Bansal 						     /* rtn value */
148439c13c20SShubham Bansal 		const u8 r1 = bpf2a32[BPF_REG_0][0]; /* r1: int k */
148539c13c20SShubham Bansal 		const u8 r2 = bpf2a32[BPF_REG_1][1]; /* r2: unsigned int size */
148639c13c20SShubham Bansal 		const u8 r3 = bpf2a32[BPF_REG_1][0]; /* r3: void *buffer */
148739c13c20SShubham Bansal 		const u8 r6 = bpf2a32[TMP_REG_1][1]; /* r6: void *(*func)(..) */
148839c13c20SShubham Bansal 		int size;
148939c13c20SShubham Bansal 
149039c13c20SShubham Bansal 		/* Setting up first argument */
149139c13c20SShubham Bansal 		emit(ARM_MOV_R(r0, r4), ctx);
149239c13c20SShubham Bansal 
149339c13c20SShubham Bansal 		/* Setting up second argument */
149439c13c20SShubham Bansal 		emit_a32_mov_i(r1, imm, false, ctx);
149539c13c20SShubham Bansal 		if (BPF_MODE(code) == BPF_IND)
149639c13c20SShubham Bansal 			emit_a32_alu_r(r1, src_lo, false, sstk, ctx,
149739c13c20SShubham Bansal 				       false, false, BPF_ADD);
149839c13c20SShubham Bansal 
149939c13c20SShubham Bansal 		/* Setting up third argument */
150039c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
150139c13c20SShubham Bansal 		case BPF_W:
150239c13c20SShubham Bansal 			size = 4;
150339c13c20SShubham Bansal 			break;
150439c13c20SShubham Bansal 		case BPF_H:
150539c13c20SShubham Bansal 			size = 2;
150639c13c20SShubham Bansal 			break;
150739c13c20SShubham Bansal 		case BPF_B:
150839c13c20SShubham Bansal 			size = 1;
150924e737c1SNicolas Schichan 			break;
1510ddecdfceSMircea Gherzan 		default:
151139c13c20SShubham Bansal 			return -EINVAL;
151239c13c20SShubham Bansal 		}
151339c13c20SShubham Bansal 		emit_a32_mov_i(r2, size, false, ctx);
151439c13c20SShubham Bansal 
151539c13c20SShubham Bansal 		/* Setting up fourth argument */
151639c13c20SShubham Bansal 		emit(ARM_ADD_I(r3, ARM_SP, imm8m(SKB_BUFFER)), ctx);
151739c13c20SShubham Bansal 
151839c13c20SShubham Bansal 		/* Setting up function pointer to call */
151939c13c20SShubham Bansal 		emit_a32_mov_i(r6, (unsigned int)bpf_load_pointer, false, ctx);
152039c13c20SShubham Bansal 		emit_blx_r(r6, ctx);
152139c13c20SShubham Bansal 
152239c13c20SShubham Bansal 		emit(ARM_EOR_R(r1, r1, r1), ctx);
152339c13c20SShubham Bansal 		/* Check if return address is NULL or not.
152439c13c20SShubham Bansal 		 * if NULL then jump to epilogue
152539c13c20SShubham Bansal 		 * else continue to load the value from retn address
152639c13c20SShubham Bansal 		 */
152739c13c20SShubham Bansal 		emit(ARM_CMP_I(r0, 0), ctx);
152839c13c20SShubham Bansal 		jmp_offset = epilogue_offset(ctx);
152939c13c20SShubham Bansal 		check_imm24(jmp_offset);
153039c13c20SShubham Bansal 		_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
153139c13c20SShubham Bansal 
153239c13c20SShubham Bansal 		/* Load value from the address */
153339c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
153439c13c20SShubham Bansal 		case BPF_W:
153539c13c20SShubham Bansal 			emit(ARM_LDR_I(r0, r0, 0), ctx);
153639c13c20SShubham Bansal 			emit_rev32(r0, r0, ctx);
153739c13c20SShubham Bansal 			break;
153839c13c20SShubham Bansal 		case BPF_H:
153939c13c20SShubham Bansal 			emit(ARM_LDRH_I(r0, r0, 0), ctx);
154039c13c20SShubham Bansal 			emit_rev16(r0, r0, ctx);
154139c13c20SShubham Bansal 			break;
154239c13c20SShubham Bansal 		case BPF_B:
154339c13c20SShubham Bansal 			emit(ARM_LDRB_I(r0, r0, 0), ctx);
154439c13c20SShubham Bansal 			/* No need to reverse */
154539c13c20SShubham Bansal 			break;
154639c13c20SShubham Bansal 		}
154739c13c20SShubham Bansal 		break;
154839c13c20SShubham Bansal 	}
154939c13c20SShubham Bansal 	/* ST: *(size *)(dst + off) = imm */
155039c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_W:
155139c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_H:
155239c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_B:
155339c13c20SShubham Bansal 	case BPF_ST | BPF_MEM | BPF_DW:
155439c13c20SShubham Bansal 		switch (BPF_SIZE(code)) {
155539c13c20SShubham Bansal 		case BPF_DW:
155639c13c20SShubham Bansal 			/* Sign-extend immediate value into temp reg */
155739c13c20SShubham Bansal 			emit_a32_mov_i64(true, tmp2, imm, false, ctx);
155839c13c20SShubham Bansal 			emit_str_r(dst_lo, tmp2[1], dstk, off, ctx, BPF_W);
155939c13c20SShubham Bansal 			emit_str_r(dst_lo, tmp2[0], dstk, off+4, ctx, BPF_W);
156039c13c20SShubham Bansal 			break;
156139c13c20SShubham Bansal 		case BPF_W:
156239c13c20SShubham Bansal 		case BPF_H:
156339c13c20SShubham Bansal 		case BPF_B:
156439c13c20SShubham Bansal 			emit_a32_mov_i(tmp2[1], imm, false, ctx);
156539c13c20SShubham Bansal 			emit_str_r(dst_lo, tmp2[1], dstk, off, ctx,
156639c13c20SShubham Bansal 				   BPF_SIZE(code));
156739c13c20SShubham Bansal 			break;
156839c13c20SShubham Bansal 		}
156939c13c20SShubham Bansal 		break;
157039c13c20SShubham Bansal 	/* STX XADD: lock *(u32 *)(dst + off) += src */
157139c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_W:
157239c13c20SShubham Bansal 	/* STX XADD: lock *(u64 *)(dst + off) += src */
157339c13c20SShubham Bansal 	case BPF_STX | BPF_XADD | BPF_DW:
157439c13c20SShubham Bansal 		goto notyet;
157539c13c20SShubham Bansal 	/* STX: *(size *)(dst + off) = src */
157639c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_W:
157739c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_H:
157839c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_B:
157939c13c20SShubham Bansal 	case BPF_STX | BPF_MEM | BPF_DW:
158039c13c20SShubham Bansal 	{
158139c13c20SShubham Bansal 		u8 sz = BPF_SIZE(code);
158239c13c20SShubham Bansal 
158339c13c20SShubham Bansal 		rn = sstk ? tmp2[1] : src_lo;
158439c13c20SShubham Bansal 		rm = sstk ? tmp2[0] : src_hi;
158539c13c20SShubham Bansal 		if (sstk) {
158639c13c20SShubham Bansal 			emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_lo)), ctx);
158739c13c20SShubham Bansal 			emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(src_hi)), ctx);
158839c13c20SShubham Bansal 		}
158939c13c20SShubham Bansal 
159039c13c20SShubham Bansal 		/* Store the value */
159139c13c20SShubham Bansal 		if (BPF_SIZE(code) == BPF_DW) {
159239c13c20SShubham Bansal 			emit_str_r(dst_lo, rn, dstk, off, ctx, BPF_W);
159339c13c20SShubham Bansal 			emit_str_r(dst_lo, rm, dstk, off+4, ctx, BPF_W);
159439c13c20SShubham Bansal 		} else {
159539c13c20SShubham Bansal 			emit_str_r(dst_lo, rn, dstk, off, ctx, sz);
159639c13c20SShubham Bansal 		}
159739c13c20SShubham Bansal 		break;
159839c13c20SShubham Bansal 	}
159939c13c20SShubham Bansal 	/* PC += off if dst == src */
160039c13c20SShubham Bansal 	/* PC += off if dst > src */
160139c13c20SShubham Bansal 	/* PC += off if dst >= src */
160239c13c20SShubham Bansal 	/* PC += off if dst < src */
160339c13c20SShubham Bansal 	/* PC += off if dst <= src */
160439c13c20SShubham Bansal 	/* PC += off if dst != src */
160539c13c20SShubham Bansal 	/* PC += off if dst > src (signed) */
160639c13c20SShubham Bansal 	/* PC += off if dst >= src (signed) */
160739c13c20SShubham Bansal 	/* PC += off if dst < src (signed) */
160839c13c20SShubham Bansal 	/* PC += off if dst <= src (signed) */
160939c13c20SShubham Bansal 	/* PC += off if dst & src */
161039c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_X:
161139c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_X:
161239c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_X:
161339c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_X:
161439c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_X:
161539c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_X:
161639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_X:
161739c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_X:
161839c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_X:
161939c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_X:
162039c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_X:
162139c13c20SShubham Bansal 		/* Setup source registers */
162239c13c20SShubham Bansal 		rm = sstk ? tmp2[0] : src_hi;
162339c13c20SShubham Bansal 		rn = sstk ? tmp2[1] : src_lo;
162439c13c20SShubham Bansal 		if (sstk) {
162539c13c20SShubham Bansal 			emit(ARM_LDR_I(rn, ARM_SP, STACK_VAR(src_lo)), ctx);
162639c13c20SShubham Bansal 			emit(ARM_LDR_I(rm, ARM_SP, STACK_VAR(src_hi)), ctx);
162739c13c20SShubham Bansal 		}
162839c13c20SShubham Bansal 		goto go_jmp;
162939c13c20SShubham Bansal 	/* PC += off if dst == imm */
163039c13c20SShubham Bansal 	/* PC += off if dst > imm */
163139c13c20SShubham Bansal 	/* PC += off if dst >= imm */
163239c13c20SShubham Bansal 	/* PC += off if dst < imm */
163339c13c20SShubham Bansal 	/* PC += off if dst <= imm */
163439c13c20SShubham Bansal 	/* PC += off if dst != imm */
163539c13c20SShubham Bansal 	/* PC += off if dst > imm (signed) */
163639c13c20SShubham Bansal 	/* PC += off if dst >= imm (signed) */
163739c13c20SShubham Bansal 	/* PC += off if dst < imm (signed) */
163839c13c20SShubham Bansal 	/* PC += off if dst <= imm (signed) */
163939c13c20SShubham Bansal 	/* PC += off if dst & imm */
164039c13c20SShubham Bansal 	case BPF_JMP | BPF_JEQ | BPF_K:
164139c13c20SShubham Bansal 	case BPF_JMP | BPF_JGT | BPF_K:
164239c13c20SShubham Bansal 	case BPF_JMP | BPF_JGE | BPF_K:
164339c13c20SShubham Bansal 	case BPF_JMP | BPF_JNE | BPF_K:
164439c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGT | BPF_K:
164539c13c20SShubham Bansal 	case BPF_JMP | BPF_JSGE | BPF_K:
164639c13c20SShubham Bansal 	case BPF_JMP | BPF_JSET | BPF_K:
164739c13c20SShubham Bansal 	case BPF_JMP | BPF_JLT | BPF_K:
164839c13c20SShubham Bansal 	case BPF_JMP | BPF_JLE | BPF_K:
164939c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLT | BPF_K:
165039c13c20SShubham Bansal 	case BPF_JMP | BPF_JSLE | BPF_K:
165139c13c20SShubham Bansal 		if (off == 0)
165239c13c20SShubham Bansal 			break;
165339c13c20SShubham Bansal 		rm = tmp2[0];
165439c13c20SShubham Bansal 		rn = tmp2[1];
165539c13c20SShubham Bansal 		/* Sign-extend immediate value */
165639c13c20SShubham Bansal 		emit_a32_mov_i64(true, tmp2, imm, false, ctx);
165739c13c20SShubham Bansal go_jmp:
165839c13c20SShubham Bansal 		/* Setup destination register */
165939c13c20SShubham Bansal 		rd = dstk ? tmp[0] : dst_hi;
166039c13c20SShubham Bansal 		rt = dstk ? tmp[1] : dst_lo;
166139c13c20SShubham Bansal 		if (dstk) {
166239c13c20SShubham Bansal 			emit(ARM_LDR_I(rt, ARM_SP, STACK_VAR(dst_lo)), ctx);
166339c13c20SShubham Bansal 			emit(ARM_LDR_I(rd, ARM_SP, STACK_VAR(dst_hi)), ctx);
166439c13c20SShubham Bansal 		}
166539c13c20SShubham Bansal 
166639c13c20SShubham Bansal 		/* Check for the condition */
166739c13c20SShubham Bansal 		emit_ar_r(rd, rt, rm, rn, ctx, BPF_OP(code));
166839c13c20SShubham Bansal 
166939c13c20SShubham Bansal 		/* Setup JUMP instruction */
167039c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
167139c13c20SShubham Bansal 		switch (BPF_OP(code)) {
167239c13c20SShubham Bansal 		case BPF_JNE:
167339c13c20SShubham Bansal 		case BPF_JSET:
167439c13c20SShubham Bansal 			_emit(ARM_COND_NE, ARM_B(jmp_offset), ctx);
167539c13c20SShubham Bansal 			break;
167639c13c20SShubham Bansal 		case BPF_JEQ:
167739c13c20SShubham Bansal 			_emit(ARM_COND_EQ, ARM_B(jmp_offset), ctx);
167839c13c20SShubham Bansal 			break;
167939c13c20SShubham Bansal 		case BPF_JGT:
168039c13c20SShubham Bansal 			_emit(ARM_COND_HI, ARM_B(jmp_offset), ctx);
168139c13c20SShubham Bansal 			break;
168239c13c20SShubham Bansal 		case BPF_JGE:
168339c13c20SShubham Bansal 			_emit(ARM_COND_CS, ARM_B(jmp_offset), ctx);
168439c13c20SShubham Bansal 			break;
168539c13c20SShubham Bansal 		case BPF_JSGT:
168639c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
168739c13c20SShubham Bansal 			break;
168839c13c20SShubham Bansal 		case BPF_JSGE:
168939c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
169039c13c20SShubham Bansal 			break;
169139c13c20SShubham Bansal 		case BPF_JLE:
169239c13c20SShubham Bansal 			_emit(ARM_COND_LS, ARM_B(jmp_offset), ctx);
169339c13c20SShubham Bansal 			break;
169439c13c20SShubham Bansal 		case BPF_JLT:
169539c13c20SShubham Bansal 			_emit(ARM_COND_CC, ARM_B(jmp_offset), ctx);
169639c13c20SShubham Bansal 			break;
169739c13c20SShubham Bansal 		case BPF_JSLT:
169839c13c20SShubham Bansal 			_emit(ARM_COND_LT, ARM_B(jmp_offset), ctx);
169939c13c20SShubham Bansal 			break;
170039c13c20SShubham Bansal 		case BPF_JSLE:
170139c13c20SShubham Bansal 			_emit(ARM_COND_GE, ARM_B(jmp_offset), ctx);
170239c13c20SShubham Bansal 			break;
170339c13c20SShubham Bansal 		}
170439c13c20SShubham Bansal 		break;
170539c13c20SShubham Bansal 	/* JMP OFF */
170639c13c20SShubham Bansal 	case BPF_JMP | BPF_JA:
170739c13c20SShubham Bansal 	{
170839c13c20SShubham Bansal 		if (off == 0)
170939c13c20SShubham Bansal 			break;
171039c13c20SShubham Bansal 		jmp_offset = bpf2a32_offset(i+off, i, ctx);
171139c13c20SShubham Bansal 		check_imm24(jmp_offset);
171239c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
171339c13c20SShubham Bansal 		break;
171439c13c20SShubham Bansal 	}
171539c13c20SShubham Bansal 	/* tail call */
171639c13c20SShubham Bansal 	case BPF_JMP | BPF_TAIL_CALL:
171739c13c20SShubham Bansal 		if (emit_bpf_tail_call(ctx))
171839c13c20SShubham Bansal 			return -EFAULT;
171939c13c20SShubham Bansal 		break;
172039c13c20SShubham Bansal 	/* function call */
172139c13c20SShubham Bansal 	case BPF_JMP | BPF_CALL:
172239c13c20SShubham Bansal 	{
172339c13c20SShubham Bansal 		const u8 *r0 = bpf2a32[BPF_REG_0];
172439c13c20SShubham Bansal 		const u8 *r1 = bpf2a32[BPF_REG_1];
172539c13c20SShubham Bansal 		const u8 *r2 = bpf2a32[BPF_REG_2];
172639c13c20SShubham Bansal 		const u8 *r3 = bpf2a32[BPF_REG_3];
172739c13c20SShubham Bansal 		const u8 *r4 = bpf2a32[BPF_REG_4];
172839c13c20SShubham Bansal 		const u8 *r5 = bpf2a32[BPF_REG_5];
172939c13c20SShubham Bansal 		const u32 func = (u32)__bpf_call_base + (u32)imm;
173039c13c20SShubham Bansal 
173139c13c20SShubham Bansal 		emit_a32_mov_r64(true, r0, r1, false, false, ctx);
173239c13c20SShubham Bansal 		emit_a32_mov_r64(true, r1, r2, false, true, ctx);
173339c13c20SShubham Bansal 		emit_push_r64(r5, 0, ctx);
173439c13c20SShubham Bansal 		emit_push_r64(r4, 8, ctx);
173539c13c20SShubham Bansal 		emit_push_r64(r3, 16, ctx);
173639c13c20SShubham Bansal 
173739c13c20SShubham Bansal 		emit_a32_mov_i(tmp[1], func, false, ctx);
173839c13c20SShubham Bansal 		emit_blx_r(tmp[1], ctx);
173939c13c20SShubham Bansal 
174039c13c20SShubham Bansal 		emit(ARM_ADD_I(ARM_SP, ARM_SP, imm8m(24)), ctx); // callee clean
174139c13c20SShubham Bansal 		break;
174239c13c20SShubham Bansal 	}
174339c13c20SShubham Bansal 	/* function return */
174439c13c20SShubham Bansal 	case BPF_JMP | BPF_EXIT:
174539c13c20SShubham Bansal 		/* Optimization: when last instruction is EXIT
174639c13c20SShubham Bansal 		 * simply fallthrough to epilogue.
174739c13c20SShubham Bansal 		 */
174839c13c20SShubham Bansal 		if (i == ctx->prog->len - 1)
174939c13c20SShubham Bansal 			break;
175039c13c20SShubham Bansal 		jmp_offset = epilogue_offset(ctx);
175139c13c20SShubham Bansal 		check_imm24(jmp_offset);
175239c13c20SShubham Bansal 		emit(ARM_B(jmp_offset), ctx);
175339c13c20SShubham Bansal 		break;
175439c13c20SShubham Bansal notyet:
175539c13c20SShubham Bansal 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
175639c13c20SShubham Bansal 		return -EFAULT;
175739c13c20SShubham Bansal 	default:
175839c13c20SShubham Bansal 		pr_err_once("unknown opcode %02x\n", code);
175939c13c20SShubham Bansal 		return -EINVAL;
1760ddecdfceSMircea Gherzan 	}
17610b59d880SNicolas Schichan 
17620b59d880SNicolas Schichan 	if (ctx->flags & FLAG_IMM_OVERFLOW)
17630b59d880SNicolas Schichan 		/*
17640b59d880SNicolas Schichan 		 * this instruction generated an overflow when
17650b59d880SNicolas Schichan 		 * trying to access the literal pool, so
17660b59d880SNicolas Schichan 		 * delegate this filter to the kernel interpreter.
17670b59d880SNicolas Schichan 		 */
17680b59d880SNicolas Schichan 		return -1;
176939c13c20SShubham Bansal 	return 0;
1770ddecdfceSMircea Gherzan }
1771ddecdfceSMircea Gherzan 
177239c13c20SShubham Bansal static int build_body(struct jit_ctx *ctx)
177339c13c20SShubham Bansal {
177439c13c20SShubham Bansal 	const struct bpf_prog *prog = ctx->prog;
177539c13c20SShubham Bansal 	unsigned int i;
177639c13c20SShubham Bansal 
177739c13c20SShubham Bansal 	for (i = 0; i < prog->len; i++) {
177839c13c20SShubham Bansal 		const struct bpf_insn *insn = &(prog->insnsi[i]);
177939c13c20SShubham Bansal 		int ret;
178039c13c20SShubham Bansal 
178139c13c20SShubham Bansal 		ret = build_insn(insn, ctx);
178239c13c20SShubham Bansal 
178339c13c20SShubham Bansal 		/* It's used with loading the 64 bit immediate value. */
178439c13c20SShubham Bansal 		if (ret > 0) {
178539c13c20SShubham Bansal 			i++;
1786ddecdfceSMircea Gherzan 			if (ctx->target == NULL)
178739c13c20SShubham Bansal 				ctx->offsets[i] = ctx->idx;
178839c13c20SShubham Bansal 			continue;
178939c13c20SShubham Bansal 		}
179039c13c20SShubham Bansal 
179139c13c20SShubham Bansal 		if (ctx->target == NULL)
179239c13c20SShubham Bansal 			ctx->offsets[i] = ctx->idx;
179339c13c20SShubham Bansal 
179439c13c20SShubham Bansal 		/* If unsuccesfull, return with error code */
179539c13c20SShubham Bansal 		if (ret)
179639c13c20SShubham Bansal 			return ret;
179739c13c20SShubham Bansal 	}
179839c13c20SShubham Bansal 	return 0;
179939c13c20SShubham Bansal }
180039c13c20SShubham Bansal 
180139c13c20SShubham Bansal static int validate_code(struct jit_ctx *ctx)
180239c13c20SShubham Bansal {
180339c13c20SShubham Bansal 	int i;
180439c13c20SShubham Bansal 
180539c13c20SShubham Bansal 	for (i = 0; i < ctx->idx; i++) {
180639c13c20SShubham Bansal 		if (ctx->target[i] == __opcode_to_mem_arm(ARM_INST_UDF))
180739c13c20SShubham Bansal 			return -1;
180839c13c20SShubham Bansal 	}
1809ddecdfceSMircea Gherzan 
1810ddecdfceSMircea Gherzan 	return 0;
1811ddecdfceSMircea Gherzan }
1812ddecdfceSMircea Gherzan 
181339c13c20SShubham Bansal void bpf_jit_compile(struct bpf_prog *prog)
1814ddecdfceSMircea Gherzan {
181539c13c20SShubham Bansal 	/* Nothing to do here. We support Internal BPF. */
181639c13c20SShubham Bansal }
1817ddecdfceSMircea Gherzan 
181839c13c20SShubham Bansal struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
181939c13c20SShubham Bansal {
182039c13c20SShubham Bansal 	struct bpf_prog *tmp, *orig_prog = prog;
182139c13c20SShubham Bansal 	struct bpf_binary_header *header;
182239c13c20SShubham Bansal 	bool tmp_blinded = false;
182339c13c20SShubham Bansal 	struct jit_ctx ctx;
182439c13c20SShubham Bansal 	unsigned int tmp_idx;
182539c13c20SShubham Bansal 	unsigned int image_size;
182639c13c20SShubham Bansal 	u8 *image_ptr;
182739c13c20SShubham Bansal 
182839c13c20SShubham Bansal 	/* If BPF JIT was not enabled then we must fall back to
182939c13c20SShubham Bansal 	 * the interpreter.
183039c13c20SShubham Bansal 	 */
1831ddecdfceSMircea Gherzan 	if (!bpf_jit_enable)
183239c13c20SShubham Bansal 		return orig_prog;
183339c13c20SShubham Bansal 
183439c13c20SShubham Bansal 	/* If constant blinding was enabled and we failed during blinding
183539c13c20SShubham Bansal 	 * then we must fall back to the interpreter. Otherwise, we save
183639c13c20SShubham Bansal 	 * the new JITed code.
183739c13c20SShubham Bansal 	 */
183839c13c20SShubham Bansal 	tmp = bpf_jit_blind_constants(prog);
183939c13c20SShubham Bansal 
184039c13c20SShubham Bansal 	if (IS_ERR(tmp))
184139c13c20SShubham Bansal 		return orig_prog;
184239c13c20SShubham Bansal 	if (tmp != prog) {
184339c13c20SShubham Bansal 		tmp_blinded = true;
184439c13c20SShubham Bansal 		prog = tmp;
184539c13c20SShubham Bansal 	}
1846ddecdfceSMircea Gherzan 
1847ddecdfceSMircea Gherzan 	memset(&ctx, 0, sizeof(ctx));
184839c13c20SShubham Bansal 	ctx.prog = prog;
1849ddecdfceSMircea Gherzan 
185039c13c20SShubham Bansal 	/* Not able to allocate memory for offsets[] , then
185139c13c20SShubham Bansal 	 * we must fall back to the interpreter
185239c13c20SShubham Bansal 	 */
185339c13c20SShubham Bansal 	ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
185439c13c20SShubham Bansal 	if (ctx.offsets == NULL) {
185539c13c20SShubham Bansal 		prog = orig_prog;
1856ddecdfceSMircea Gherzan 		goto out;
185739c13c20SShubham Bansal 	}
185839c13c20SShubham Bansal 
185939c13c20SShubham Bansal 	/* 1) fake pass to find in the length of the JITed code,
186039c13c20SShubham Bansal 	 * to compute ctx->offsets and other context variables
186139c13c20SShubham Bansal 	 * needed to compute final JITed code.
186239c13c20SShubham Bansal 	 * Also, calculate random starting pointer/start of JITed code
186339c13c20SShubham Bansal 	 * which is prefixed by random number of fault instructions.
186439c13c20SShubham Bansal 	 *
186539c13c20SShubham Bansal 	 * If the first pass fails then there is no chance of it
186639c13c20SShubham Bansal 	 * being successful in the second pass, so just fall back
186739c13c20SShubham Bansal 	 * to the interpreter.
186839c13c20SShubham Bansal 	 */
186939c13c20SShubham Bansal 	if (build_body(&ctx)) {
187039c13c20SShubham Bansal 		prog = orig_prog;
187139c13c20SShubham Bansal 		goto out_off;
187239c13c20SShubham Bansal 	}
1873ddecdfceSMircea Gherzan 
1874ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1875ddecdfceSMircea Gherzan 	build_prologue(&ctx);
1876ddecdfceSMircea Gherzan 	ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
1877ddecdfceSMircea Gherzan 
187839c13c20SShubham Bansal 	ctx.epilogue_offset = ctx.idx;
187939c13c20SShubham Bansal 
1880ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1881ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
1882ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1883ddecdfceSMircea Gherzan 	ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
1884ddecdfceSMircea Gherzan 
1885ddecdfceSMircea Gherzan 	ctx.idx += ctx.imm_count;
1886ddecdfceSMircea Gherzan 	if (ctx.imm_count) {
188739c13c20SShubham Bansal 		ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
188839c13c20SShubham Bansal 		if (ctx.imms == NULL) {
188939c13c20SShubham Bansal 			prog = orig_prog;
189039c13c20SShubham Bansal 			goto out_off;
189139c13c20SShubham Bansal 		}
1892ddecdfceSMircea Gherzan 	}
1893ddecdfceSMircea Gherzan #else
189439c13c20SShubham Bansal 	/* there's nothing about the epilogue on ARMv7 */
1895ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1896ddecdfceSMircea Gherzan #endif
189739c13c20SShubham Bansal 	/* Now we can get the actual image size of the JITed arm code.
189839c13c20SShubham Bansal 	 * Currently, we are not considering the THUMB-2 instructions
189939c13c20SShubham Bansal 	 * for jit, although it can decrease the size of the image.
190039c13c20SShubham Bansal 	 *
190139c13c20SShubham Bansal 	 * As each arm instruction is of length 32bit, we are translating
190239c13c20SShubham Bansal 	 * number of JITed intructions into the size required to store these
190339c13c20SShubham Bansal 	 * JITed code.
190439c13c20SShubham Bansal 	 */
190539c13c20SShubham Bansal 	image_size = sizeof(u32) * ctx.idx;
1906ddecdfceSMircea Gherzan 
190739c13c20SShubham Bansal 	/* Now we know the size of the structure to make */
190839c13c20SShubham Bansal 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
190939c13c20SShubham Bansal 				      sizeof(u32), jit_fill_hole);
191039c13c20SShubham Bansal 	/* Not able to allocate memory for the structure then
191139c13c20SShubham Bansal 	 * we must fall back to the interpretation
191239c13c20SShubham Bansal 	 */
191339c13c20SShubham Bansal 	if (header == NULL) {
191439c13c20SShubham Bansal 		prog = orig_prog;
191539c13c20SShubham Bansal 		goto out_imms;
191639c13c20SShubham Bansal 	}
191739c13c20SShubham Bansal 
191839c13c20SShubham Bansal 	/* 2.) Actual pass to generate final JIT code */
191939c13c20SShubham Bansal 	ctx.target = (u32 *) image_ptr;
1920ddecdfceSMircea Gherzan 	ctx.idx = 0;
192155309dd3SDaniel Borkmann 
1922ddecdfceSMircea Gherzan 	build_prologue(&ctx);
192339c13c20SShubham Bansal 
192439c13c20SShubham Bansal 	/* If building the body of the JITed code fails somehow,
192539c13c20SShubham Bansal 	 * we fall back to the interpretation.
192639c13c20SShubham Bansal 	 */
19270b59d880SNicolas Schichan 	if (build_body(&ctx) < 0) {
192839c13c20SShubham Bansal 		image_ptr = NULL;
19290b59d880SNicolas Schichan 		bpf_jit_binary_free(header);
193039c13c20SShubham Bansal 		prog = orig_prog;
193139c13c20SShubham Bansal 		goto out_imms;
19320b59d880SNicolas Schichan 	}
1933ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1934ddecdfceSMircea Gherzan 
193539c13c20SShubham Bansal 	/* 3.) Extra pass to validate JITed Code */
193639c13c20SShubham Bansal 	if (validate_code(&ctx)) {
193739c13c20SShubham Bansal 		image_ptr = NULL;
193839c13c20SShubham Bansal 		bpf_jit_binary_free(header);
193939c13c20SShubham Bansal 		prog = orig_prog;
194039c13c20SShubham Bansal 		goto out_imms;
194139c13c20SShubham Bansal 	}
1942ebaef649SDaniel Borkmann 	flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
1943ddecdfceSMircea Gherzan 
194439c13c20SShubham Bansal 	if (bpf_jit_enable > 1)
194539c13c20SShubham Bansal 		/* there are 2 passes here */
194639c13c20SShubham Bansal 		bpf_jit_dump(prog->len, image_size, 2, ctx.target);
194739c13c20SShubham Bansal 
194839c13c20SShubham Bansal 	set_memory_ro((unsigned long)header, header->pages);
194939c13c20SShubham Bansal 	prog->bpf_func = (void *)ctx.target;
195039c13c20SShubham Bansal 	prog->jited = 1;
195139c13c20SShubham Bansal 	prog->jited_len = image_size;
195239c13c20SShubham Bansal 
195339c13c20SShubham Bansal out_imms:
1954ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1955ddecdfceSMircea Gherzan 	if (ctx.imm_count)
1956ddecdfceSMircea Gherzan 		kfree(ctx.imms);
1957ddecdfceSMircea Gherzan #endif
195839c13c20SShubham Bansal out_off:
1959ddecdfceSMircea Gherzan 	kfree(ctx.offsets);
196039c13c20SShubham Bansal out:
196139c13c20SShubham Bansal 	if (tmp_blinded)
196239c13c20SShubham Bansal 		bpf_jit_prog_release_other(prog, prog == orig_prog ?
196339c13c20SShubham Bansal 					   tmp : orig_prog);
196439c13c20SShubham Bansal 	return prog;
1965ddecdfceSMircea Gherzan }
1966ddecdfceSMircea Gherzan 
1967