xref: /openbmc/linux/arch/arm/net/bpf_jit_32.c (revision 1447f93f22add339ee63d1a7015309d9fbfa47e8)
1ddecdfceSMircea Gherzan /*
2ddecdfceSMircea Gherzan  * Just-In-Time compiler for BPF filters on 32bit ARM
3ddecdfceSMircea Gherzan  *
4ddecdfceSMircea Gherzan  * Copyright (c) 2011 Mircea Gherzan <mgherzan@gmail.com>
5ddecdfceSMircea Gherzan  *
6ddecdfceSMircea Gherzan  * This program is free software; you can redistribute it and/or modify it
7ddecdfceSMircea Gherzan  * under the terms of the GNU General Public License as published by the
8ddecdfceSMircea Gherzan  * Free Software Foundation; version 2 of the License.
9ddecdfceSMircea Gherzan  */
10ddecdfceSMircea Gherzan 
11ddecdfceSMircea Gherzan #include <linux/bitops.h>
12ddecdfceSMircea Gherzan #include <linux/compiler.h>
13ddecdfceSMircea Gherzan #include <linux/errno.h>
14ddecdfceSMircea Gherzan #include <linux/filter.h>
15ddecdfceSMircea Gherzan #include <linux/netdevice.h>
16ddecdfceSMircea Gherzan #include <linux/string.h>
17ddecdfceSMircea Gherzan #include <linux/slab.h>
18bf0098f2SDaniel Borkmann #include <linux/if_vlan.h>
19e8b56d55SDaniel Borkmann 
20ddecdfceSMircea Gherzan #include <asm/cacheflush.h>
21ddecdfceSMircea Gherzan #include <asm/hwcap.h>
223460743eSBen Dooks #include <asm/opcodes.h>
23ddecdfceSMircea Gherzan 
24ddecdfceSMircea Gherzan #include "bpf_jit_32.h"
25ddecdfceSMircea Gherzan 
26ddecdfceSMircea Gherzan /*
27ddecdfceSMircea Gherzan  * ABI:
28ddecdfceSMircea Gherzan  *
29ddecdfceSMircea Gherzan  * r0	scratch register
30ddecdfceSMircea Gherzan  * r4	BPF register A
31ddecdfceSMircea Gherzan  * r5	BPF register X
32ddecdfceSMircea Gherzan  * r6	pointer to the skb
33ddecdfceSMircea Gherzan  * r7	skb->data
34ddecdfceSMircea Gherzan  * r8	skb_headlen(skb)
35ddecdfceSMircea Gherzan  */
36ddecdfceSMircea Gherzan 
37ddecdfceSMircea Gherzan #define r_scratch	ARM_R0
38ddecdfceSMircea Gherzan /* r1-r3 are (also) used for the unaligned loads on the non-ARMv7 slowpath */
39ddecdfceSMircea Gherzan #define r_off		ARM_R1
40ddecdfceSMircea Gherzan #define r_A		ARM_R4
41ddecdfceSMircea Gherzan #define r_X		ARM_R5
42ddecdfceSMircea Gherzan #define r_skb		ARM_R6
43ddecdfceSMircea Gherzan #define r_skb_data	ARM_R7
44ddecdfceSMircea Gherzan #define r_skb_hl	ARM_R8
45ddecdfceSMircea Gherzan 
46ddecdfceSMircea Gherzan #define SCRATCH_SP_OFFSET	0
47fe15f3f1SSchichan Nicolas #define SCRATCH_OFF(k)		(SCRATCH_SP_OFFSET + 4 * (k))
48ddecdfceSMircea Gherzan 
49ddecdfceSMircea Gherzan #define SEEN_MEM		((1 << BPF_MEMWORDS) - 1)
50ddecdfceSMircea Gherzan #define SEEN_MEM_WORD(k)	(1 << (k))
51ddecdfceSMircea Gherzan #define SEEN_X			(1 << BPF_MEMWORDS)
52ddecdfceSMircea Gherzan #define SEEN_CALL		(1 << (BPF_MEMWORDS + 1))
53ddecdfceSMircea Gherzan #define SEEN_SKB		(1 << (BPF_MEMWORDS + 2))
54ddecdfceSMircea Gherzan #define SEEN_DATA		(1 << (BPF_MEMWORDS + 3))
55ddecdfceSMircea Gherzan 
56ddecdfceSMircea Gherzan #define FLAG_NEED_X_RESET	(1 << 0)
570b59d880SNicolas Schichan #define FLAG_IMM_OVERFLOW	(1 << 1)
58ddecdfceSMircea Gherzan 
59ddecdfceSMircea Gherzan struct jit_ctx {
607ae457c1SAlexei Starovoitov 	const struct bpf_prog *skf;
61ddecdfceSMircea Gherzan 	unsigned idx;
62ddecdfceSMircea Gherzan 	unsigned prologue_bytes;
63ddecdfceSMircea Gherzan 	int ret0_fp_idx;
64ddecdfceSMircea Gherzan 	u32 seen;
65ddecdfceSMircea Gherzan 	u32 flags;
66ddecdfceSMircea Gherzan 	u32 *offsets;
67ddecdfceSMircea Gherzan 	u32 *target;
68ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
69ddecdfceSMircea Gherzan 	u16 epilogue_bytes;
70ddecdfceSMircea Gherzan 	u16 imm_count;
71ddecdfceSMircea Gherzan 	u32 *imms;
72ddecdfceSMircea Gherzan #endif
73ddecdfceSMircea Gherzan };
74ddecdfceSMircea Gherzan 
75ddecdfceSMircea Gherzan int bpf_jit_enable __read_mostly;
76ddecdfceSMircea Gherzan 
776d715e30SNicolas Schichan static inline int call_neg_helper(struct sk_buff *skb, int offset, void *ret,
786d715e30SNicolas Schichan 		      unsigned int size)
796d715e30SNicolas Schichan {
806d715e30SNicolas Schichan 	void *ptr = bpf_internal_load_pointer_neg_helper(skb, offset, size);
816d715e30SNicolas Schichan 
826d715e30SNicolas Schichan 	if (!ptr)
836d715e30SNicolas Schichan 		return -EFAULT;
846d715e30SNicolas Schichan 	memcpy(ret, ptr, size);
856d715e30SNicolas Schichan 	return 0;
866d715e30SNicolas Schichan }
876d715e30SNicolas Schichan 
886d715e30SNicolas Schichan static u64 jit_get_skb_b(struct sk_buff *skb, int offset)
89ddecdfceSMircea Gherzan {
90ddecdfceSMircea Gherzan 	u8 ret;
91ddecdfceSMircea Gherzan 	int err;
92ddecdfceSMircea Gherzan 
936d715e30SNicolas Schichan 	if (offset < 0)
946d715e30SNicolas Schichan 		err = call_neg_helper(skb, offset, &ret, 1);
956d715e30SNicolas Schichan 	else
96ddecdfceSMircea Gherzan 		err = skb_copy_bits(skb, offset, &ret, 1);
97ddecdfceSMircea Gherzan 
98ddecdfceSMircea Gherzan 	return (u64)err << 32 | ret;
99ddecdfceSMircea Gherzan }
100ddecdfceSMircea Gherzan 
1016d715e30SNicolas Schichan static u64 jit_get_skb_h(struct sk_buff *skb, int offset)
102ddecdfceSMircea Gherzan {
103ddecdfceSMircea Gherzan 	u16 ret;
104ddecdfceSMircea Gherzan 	int err;
105ddecdfceSMircea Gherzan 
1066d715e30SNicolas Schichan 	if (offset < 0)
1076d715e30SNicolas Schichan 		err = call_neg_helper(skb, offset, &ret, 2);
1086d715e30SNicolas Schichan 	else
109ddecdfceSMircea Gherzan 		err = skb_copy_bits(skb, offset, &ret, 2);
110ddecdfceSMircea Gherzan 
111ddecdfceSMircea Gherzan 	return (u64)err << 32 | ntohs(ret);
112ddecdfceSMircea Gherzan }
113ddecdfceSMircea Gherzan 
1146d715e30SNicolas Schichan static u64 jit_get_skb_w(struct sk_buff *skb, int offset)
115ddecdfceSMircea Gherzan {
116ddecdfceSMircea Gherzan 	u32 ret;
117ddecdfceSMircea Gherzan 	int err;
118ddecdfceSMircea Gherzan 
1196d715e30SNicolas Schichan 	if (offset < 0)
1206d715e30SNicolas Schichan 		err = call_neg_helper(skb, offset, &ret, 4);
1216d715e30SNicolas Schichan 	else
122ddecdfceSMircea Gherzan 		err = skb_copy_bits(skb, offset, &ret, 4);
123ddecdfceSMircea Gherzan 
124ddecdfceSMircea Gherzan 	return (u64)err << 32 | ntohl(ret);
125ddecdfceSMircea Gherzan }
126ddecdfceSMircea Gherzan 
127ddecdfceSMircea Gherzan /*
128ddecdfceSMircea Gherzan  * Wrapper that handles both OABI and EABI and assures Thumb2 interworking
129ddecdfceSMircea Gherzan  * (where the assembly routines like __aeabi_uidiv could cause problems).
130ddecdfceSMircea Gherzan  */
131ddecdfceSMircea Gherzan static u32 jit_udiv(u32 dividend, u32 divisor)
132ddecdfceSMircea Gherzan {
133ddecdfceSMircea Gherzan 	return dividend / divisor;
134ddecdfceSMircea Gherzan }
135ddecdfceSMircea Gherzan 
136ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
137ddecdfceSMircea Gherzan {
1383460743eSBen Dooks 	inst |= (cond << 28);
1393460743eSBen Dooks 	inst = __opcode_to_mem_arm(inst);
1403460743eSBen Dooks 
141ddecdfceSMircea Gherzan 	if (ctx->target != NULL)
1423460743eSBen Dooks 		ctx->target[ctx->idx] = inst;
143ddecdfceSMircea Gherzan 
144ddecdfceSMircea Gherzan 	ctx->idx++;
145ddecdfceSMircea Gherzan }
146ddecdfceSMircea Gherzan 
147ddecdfceSMircea Gherzan /*
148ddecdfceSMircea Gherzan  * Emit an instruction that will be executed unconditionally.
149ddecdfceSMircea Gherzan  */
150ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx)
151ddecdfceSMircea Gherzan {
152ddecdfceSMircea Gherzan 	_emit(ARM_COND_AL, inst, ctx);
153ddecdfceSMircea Gherzan }
154ddecdfceSMircea Gherzan 
155ddecdfceSMircea Gherzan static u16 saved_regs(struct jit_ctx *ctx)
156ddecdfceSMircea Gherzan {
157ddecdfceSMircea Gherzan 	u16 ret = 0;
158ddecdfceSMircea Gherzan 
159ddecdfceSMircea Gherzan 	if ((ctx->skf->len > 1) ||
16034805931SDaniel Borkmann 	    (ctx->skf->insns[0].code == (BPF_RET | BPF_A)))
161ddecdfceSMircea Gherzan 		ret |= 1 << r_A;
162ddecdfceSMircea Gherzan 
163ddecdfceSMircea Gherzan #ifdef CONFIG_FRAME_POINTER
164ddecdfceSMircea Gherzan 	ret |= (1 << ARM_FP) | (1 << ARM_IP) | (1 << ARM_LR) | (1 << ARM_PC);
165ddecdfceSMircea Gherzan #else
166ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_CALL)
167ddecdfceSMircea Gherzan 		ret |= 1 << ARM_LR;
168ddecdfceSMircea Gherzan #endif
169ddecdfceSMircea Gherzan 	if (ctx->seen & (SEEN_DATA | SEEN_SKB))
170ddecdfceSMircea Gherzan 		ret |= 1 << r_skb;
171ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_DATA)
172ddecdfceSMircea Gherzan 		ret |= (1 << r_skb_data) | (1 << r_skb_hl);
173ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_X)
174ddecdfceSMircea Gherzan 		ret |= 1 << r_X;
175ddecdfceSMircea Gherzan 
176ddecdfceSMircea Gherzan 	return ret;
177ddecdfceSMircea Gherzan }
178ddecdfceSMircea Gherzan 
179ddecdfceSMircea Gherzan static inline int mem_words_used(struct jit_ctx *ctx)
180ddecdfceSMircea Gherzan {
181ddecdfceSMircea Gherzan 	/* yes, we do waste some stack space IF there are "holes" in the set" */
182ddecdfceSMircea Gherzan 	return fls(ctx->seen & SEEN_MEM);
183ddecdfceSMircea Gherzan }
184ddecdfceSMircea Gherzan 
185ddecdfceSMircea Gherzan static inline bool is_load_to_a(u16 inst)
186ddecdfceSMircea Gherzan {
187ddecdfceSMircea Gherzan 	switch (inst) {
18834805931SDaniel Borkmann 	case BPF_LD | BPF_W | BPF_LEN:
18934805931SDaniel Borkmann 	case BPF_LD | BPF_W | BPF_ABS:
19034805931SDaniel Borkmann 	case BPF_LD | BPF_H | BPF_ABS:
19134805931SDaniel Borkmann 	case BPF_LD | BPF_B | BPF_ABS:
192ddecdfceSMircea Gherzan 		return true;
193ddecdfceSMircea Gherzan 	default:
194ddecdfceSMircea Gherzan 		return false;
195ddecdfceSMircea Gherzan 	}
196ddecdfceSMircea Gherzan }
197ddecdfceSMircea Gherzan 
19855309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
19955309dd3SDaniel Borkmann {
200e8b56d55SDaniel Borkmann 	u32 *ptr;
20155309dd3SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
20255309dd3SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
203e8b56d55SDaniel Borkmann 		*ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
20455309dd3SDaniel Borkmann }
20555309dd3SDaniel Borkmann 
206ddecdfceSMircea Gherzan static void build_prologue(struct jit_ctx *ctx)
207ddecdfceSMircea Gherzan {
208ddecdfceSMircea Gherzan 	u16 reg_set = saved_regs(ctx);
209ddecdfceSMircea Gherzan 	u16 first_inst = ctx->skf->insns[0].code;
210ddecdfceSMircea Gherzan 	u16 off;
211ddecdfceSMircea Gherzan 
212ddecdfceSMircea Gherzan #ifdef CONFIG_FRAME_POINTER
213ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
214ddecdfceSMircea Gherzan 	emit(ARM_PUSH(reg_set), ctx);
215ddecdfceSMircea Gherzan 	emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
216ddecdfceSMircea Gherzan #else
217ddecdfceSMircea Gherzan 	if (reg_set)
218ddecdfceSMircea Gherzan 		emit(ARM_PUSH(reg_set), ctx);
219ddecdfceSMircea Gherzan #endif
220ddecdfceSMircea Gherzan 
221ddecdfceSMircea Gherzan 	if (ctx->seen & (SEEN_DATA | SEEN_SKB))
222ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(r_skb, ARM_R0), ctx);
223ddecdfceSMircea Gherzan 
224ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_DATA) {
225ddecdfceSMircea Gherzan 		off = offsetof(struct sk_buff, data);
226ddecdfceSMircea Gherzan 		emit(ARM_LDR_I(r_skb_data, r_skb, off), ctx);
227ddecdfceSMircea Gherzan 		/* headlen = len - data_len */
228ddecdfceSMircea Gherzan 		off = offsetof(struct sk_buff, len);
229ddecdfceSMircea Gherzan 		emit(ARM_LDR_I(r_skb_hl, r_skb, off), ctx);
230ddecdfceSMircea Gherzan 		off = offsetof(struct sk_buff, data_len);
231ddecdfceSMircea Gherzan 		emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
232ddecdfceSMircea Gherzan 		emit(ARM_SUB_R(r_skb_hl, r_skb_hl, r_scratch), ctx);
233ddecdfceSMircea Gherzan 	}
234ddecdfceSMircea Gherzan 
235ddecdfceSMircea Gherzan 	if (ctx->flags & FLAG_NEED_X_RESET)
236ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(r_X, 0), ctx);
237ddecdfceSMircea Gherzan 
238ddecdfceSMircea Gherzan 	/* do not leak kernel data to userspace */
23934805931SDaniel Borkmann 	if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst)))
240ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(r_A, 0), ctx);
241ddecdfceSMircea Gherzan 
242ddecdfceSMircea Gherzan 	/* stack space for the BPF_MEM words */
243ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_MEM)
244ddecdfceSMircea Gherzan 		emit(ARM_SUB_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
245ddecdfceSMircea Gherzan }
246ddecdfceSMircea Gherzan 
247ddecdfceSMircea Gherzan static void build_epilogue(struct jit_ctx *ctx)
248ddecdfceSMircea Gherzan {
249ddecdfceSMircea Gherzan 	u16 reg_set = saved_regs(ctx);
250ddecdfceSMircea Gherzan 
251ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_MEM)
252ddecdfceSMircea Gherzan 		emit(ARM_ADD_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
253ddecdfceSMircea Gherzan 
254ddecdfceSMircea Gherzan 	reg_set &= ~(1 << ARM_LR);
255ddecdfceSMircea Gherzan 
256ddecdfceSMircea Gherzan #ifdef CONFIG_FRAME_POINTER
257ddecdfceSMircea Gherzan 	/* the first instruction of the prologue was: mov ip, sp */
258ddecdfceSMircea Gherzan 	reg_set &= ~(1 << ARM_IP);
259ddecdfceSMircea Gherzan 	reg_set |= (1 << ARM_SP);
260ddecdfceSMircea Gherzan 	emit(ARM_LDM(ARM_SP, reg_set), ctx);
261ddecdfceSMircea Gherzan #else
262ddecdfceSMircea Gherzan 	if (reg_set) {
263ddecdfceSMircea Gherzan 		if (ctx->seen & SEEN_CALL)
264ddecdfceSMircea Gherzan 			reg_set |= 1 << ARM_PC;
265ddecdfceSMircea Gherzan 		emit(ARM_POP(reg_set), ctx);
266ddecdfceSMircea Gherzan 	}
267ddecdfceSMircea Gherzan 
268ddecdfceSMircea Gherzan 	if (!(ctx->seen & SEEN_CALL))
269ddecdfceSMircea Gherzan 		emit(ARM_BX(ARM_LR), ctx);
270ddecdfceSMircea Gherzan #endif
271ddecdfceSMircea Gherzan }
272ddecdfceSMircea Gherzan 
273ddecdfceSMircea Gherzan static int16_t imm8m(u32 x)
274ddecdfceSMircea Gherzan {
275ddecdfceSMircea Gherzan 	u32 rot;
276ddecdfceSMircea Gherzan 
277ddecdfceSMircea Gherzan 	for (rot = 0; rot < 16; rot++)
278ddecdfceSMircea Gherzan 		if ((x & ~ror32(0xff, 2 * rot)) == 0)
279ddecdfceSMircea Gherzan 			return rol32(x, 2 * rot) | (rot << 8);
280ddecdfceSMircea Gherzan 
281ddecdfceSMircea Gherzan 	return -1;
282ddecdfceSMircea Gherzan }
283ddecdfceSMircea Gherzan 
284ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
285ddecdfceSMircea Gherzan 
286ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx)
287ddecdfceSMircea Gherzan {
288ddecdfceSMircea Gherzan 	unsigned i = 0, offset;
289ddecdfceSMircea Gherzan 	u16 imm;
290ddecdfceSMircea Gherzan 
291ddecdfceSMircea Gherzan 	/* on the "fake" run we just count them (duplicates included) */
292ddecdfceSMircea Gherzan 	if (ctx->target == NULL) {
293ddecdfceSMircea Gherzan 		ctx->imm_count++;
294ddecdfceSMircea Gherzan 		return 0;
295ddecdfceSMircea Gherzan 	}
296ddecdfceSMircea Gherzan 
297ddecdfceSMircea Gherzan 	while ((i < ctx->imm_count) && ctx->imms[i]) {
298ddecdfceSMircea Gherzan 		if (ctx->imms[i] == k)
299ddecdfceSMircea Gherzan 			break;
300ddecdfceSMircea Gherzan 		i++;
301ddecdfceSMircea Gherzan 	}
302ddecdfceSMircea Gherzan 
303ddecdfceSMircea Gherzan 	if (ctx->imms[i] == 0)
304ddecdfceSMircea Gherzan 		ctx->imms[i] = k;
305ddecdfceSMircea Gherzan 
306ddecdfceSMircea Gherzan 	/* constants go just after the epilogue */
307ddecdfceSMircea Gherzan 	offset =  ctx->offsets[ctx->skf->len];
308ddecdfceSMircea Gherzan 	offset += ctx->prologue_bytes;
309ddecdfceSMircea Gherzan 	offset += ctx->epilogue_bytes;
310ddecdfceSMircea Gherzan 	offset += i * 4;
311ddecdfceSMircea Gherzan 
312ddecdfceSMircea Gherzan 	ctx->target[offset / 4] = k;
313ddecdfceSMircea Gherzan 
314ddecdfceSMircea Gherzan 	/* PC in ARM mode == address of the instruction + 8 */
315ddecdfceSMircea Gherzan 	imm = offset - (8 + ctx->idx * 4);
316ddecdfceSMircea Gherzan 
3170b59d880SNicolas Schichan 	if (imm & ~0xfff) {
3180b59d880SNicolas Schichan 		/*
3190b59d880SNicolas Schichan 		 * literal pool is too far, signal it into flags. we
3200b59d880SNicolas Schichan 		 * can only detect it on the second pass unfortunately.
3210b59d880SNicolas Schichan 		 */
3220b59d880SNicolas Schichan 		ctx->flags |= FLAG_IMM_OVERFLOW;
3230b59d880SNicolas Schichan 		return 0;
3240b59d880SNicolas Schichan 	}
3250b59d880SNicolas Schichan 
326ddecdfceSMircea Gherzan 	return imm;
327ddecdfceSMircea Gherzan }
328ddecdfceSMircea Gherzan 
329ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */
330ddecdfceSMircea Gherzan 
331ddecdfceSMircea Gherzan /*
332ddecdfceSMircea Gherzan  * Move an immediate that's not an imm8m to a core register.
333ddecdfceSMircea Gherzan  */
334ddecdfceSMircea Gherzan static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)
335ddecdfceSMircea Gherzan {
336ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
337ddecdfceSMircea Gherzan 	emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
338ddecdfceSMircea Gherzan #else
339ddecdfceSMircea Gherzan 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
340ddecdfceSMircea Gherzan 	if (val > 0xffff)
341ddecdfceSMircea Gherzan 		emit(ARM_MOVT(rd, val >> 16), ctx);
342ddecdfceSMircea Gherzan #endif
343ddecdfceSMircea Gherzan }
344ddecdfceSMircea Gherzan 
345ddecdfceSMircea Gherzan static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)
346ddecdfceSMircea Gherzan {
347ddecdfceSMircea Gherzan 	int imm12 = imm8m(val);
348ddecdfceSMircea Gherzan 
349ddecdfceSMircea Gherzan 	if (imm12 >= 0)
350ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(rd, imm12), ctx);
351ddecdfceSMircea Gherzan 	else
352ddecdfceSMircea Gherzan 		emit_mov_i_no8m(rd, val, ctx);
353ddecdfceSMircea Gherzan }
354ddecdfceSMircea Gherzan 
355ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 6
356ddecdfceSMircea Gherzan 
357ddecdfceSMircea Gherzan static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
358ddecdfceSMircea Gherzan {
359ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R3, r_addr, 1), ctx);
360ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
361ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 3), ctx);
362ddecdfceSMircea Gherzan 	_emit(cond, ARM_LSL_I(ARM_R3, ARM_R3, 16), ctx);
363ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R0, r_addr, 2), ctx);
364ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_S(ARM_R3, ARM_R3, ARM_R1, SRTYPE_LSL, 24), ctx);
365ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_R(ARM_R3, ARM_R3, ARM_R2), ctx);
366ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_S(r_res, ARM_R3, ARM_R0, SRTYPE_LSL, 8), ctx);
367ddecdfceSMircea Gherzan }
368ddecdfceSMircea Gherzan 
369ddecdfceSMircea Gherzan static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
370ddecdfceSMircea Gherzan {
371ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
372ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 1), ctx);
373ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_S(r_res, ARM_R2, ARM_R1, SRTYPE_LSL, 8), ctx);
374ddecdfceSMircea Gherzan }
375ddecdfceSMircea Gherzan 
376ddecdfceSMircea Gherzan static inline void emit_swap16(u8 r_dst, u8 r_src, struct jit_ctx *ctx)
377ddecdfceSMircea Gherzan {
378462738f4SNicolas Schichan 	/* r_dst = (r_src << 8) | (r_src >> 8) */
379462738f4SNicolas Schichan 	emit(ARM_LSL_I(ARM_R1, r_src, 8), ctx);
380462738f4SNicolas Schichan 	emit(ARM_ORR_S(r_dst, ARM_R1, r_src, SRTYPE_LSR, 8), ctx);
381462738f4SNicolas Schichan 
382462738f4SNicolas Schichan 	/*
383462738f4SNicolas Schichan 	 * we need to mask out the bits set in r_dst[23:16] due to
384462738f4SNicolas Schichan 	 * the first shift instruction.
385462738f4SNicolas Schichan 	 *
386462738f4SNicolas Schichan 	 * note that 0x8ff is the encoded immediate 0x00ff0000.
387462738f4SNicolas Schichan 	 */
388462738f4SNicolas Schichan 	emit(ARM_BIC_I(r_dst, r_dst, 0x8ff), ctx);
389ddecdfceSMircea Gherzan }
390ddecdfceSMircea Gherzan 
391ddecdfceSMircea Gherzan #else  /* ARMv6+ */
392ddecdfceSMircea Gherzan 
393ddecdfceSMircea Gherzan static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
394ddecdfceSMircea Gherzan {
395ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDR_I(r_res, r_addr, 0), ctx);
396ddecdfceSMircea Gherzan #ifdef __LITTLE_ENDIAN
397ddecdfceSMircea Gherzan 	_emit(cond, ARM_REV(r_res, r_res), ctx);
398ddecdfceSMircea Gherzan #endif
399ddecdfceSMircea Gherzan }
400ddecdfceSMircea Gherzan 
401ddecdfceSMircea Gherzan static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
402ddecdfceSMircea Gherzan {
403ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRH_I(r_res, r_addr, 0), ctx);
404ddecdfceSMircea Gherzan #ifdef __LITTLE_ENDIAN
405ddecdfceSMircea Gherzan 	_emit(cond, ARM_REV16(r_res, r_res), ctx);
406ddecdfceSMircea Gherzan #endif
407ddecdfceSMircea Gherzan }
408ddecdfceSMircea Gherzan 
409ddecdfceSMircea Gherzan static inline void emit_swap16(u8 r_dst __maybe_unused,
410ddecdfceSMircea Gherzan 			       u8 r_src __maybe_unused,
411ddecdfceSMircea Gherzan 			       struct jit_ctx *ctx __maybe_unused)
412ddecdfceSMircea Gherzan {
413ddecdfceSMircea Gherzan #ifdef __LITTLE_ENDIAN
414ddecdfceSMircea Gherzan 	emit(ARM_REV16(r_dst, r_src), ctx);
415ddecdfceSMircea Gherzan #endif
416ddecdfceSMircea Gherzan }
417ddecdfceSMircea Gherzan 
418ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ < 6 */
419ddecdfceSMircea Gherzan 
420ddecdfceSMircea Gherzan 
421ddecdfceSMircea Gherzan /* Compute the immediate value for a PC-relative branch. */
422ddecdfceSMircea Gherzan static inline u32 b_imm(unsigned tgt, struct jit_ctx *ctx)
423ddecdfceSMircea Gherzan {
424ddecdfceSMircea Gherzan 	u32 imm;
425ddecdfceSMircea Gherzan 
426ddecdfceSMircea Gherzan 	if (ctx->target == NULL)
427ddecdfceSMircea Gherzan 		return 0;
428ddecdfceSMircea Gherzan 	/*
429ddecdfceSMircea Gherzan 	 * BPF allows only forward jumps and the offset of the target is
430ddecdfceSMircea Gherzan 	 * still the one computed during the first pass.
431ddecdfceSMircea Gherzan 	 */
432ddecdfceSMircea Gherzan 	imm  = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx * 4 + 8);
433ddecdfceSMircea Gherzan 
434ddecdfceSMircea Gherzan 	return imm >> 2;
435ddecdfceSMircea Gherzan }
436ddecdfceSMircea Gherzan 
437ddecdfceSMircea Gherzan #define OP_IMM3(op, r1, r2, imm_val, ctx)				\
438ddecdfceSMircea Gherzan 	do {								\
439ddecdfceSMircea Gherzan 		imm12 = imm8m(imm_val);					\
440ddecdfceSMircea Gherzan 		if (imm12 < 0) {					\
441ddecdfceSMircea Gherzan 			emit_mov_i_no8m(r_scratch, imm_val, ctx);	\
442ddecdfceSMircea Gherzan 			emit(op ## _R((r1), (r2), r_scratch), ctx);	\
443ddecdfceSMircea Gherzan 		} else {						\
444ddecdfceSMircea Gherzan 			emit(op ## _I((r1), (r2), imm12), ctx);		\
445ddecdfceSMircea Gherzan 		}							\
446ddecdfceSMircea Gherzan 	} while (0)
447ddecdfceSMircea Gherzan 
448ddecdfceSMircea Gherzan static inline void emit_err_ret(u8 cond, struct jit_ctx *ctx)
449ddecdfceSMircea Gherzan {
450ddecdfceSMircea Gherzan 	if (ctx->ret0_fp_idx >= 0) {
451ddecdfceSMircea Gherzan 		_emit(cond, ARM_B(b_imm(ctx->ret0_fp_idx, ctx)), ctx);
452ddecdfceSMircea Gherzan 		/* NOP to keep the size constant between passes */
453ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R0, ARM_R0), ctx);
454ddecdfceSMircea Gherzan 	} else {
455ddecdfceSMircea Gherzan 		_emit(cond, ARM_MOV_I(ARM_R0, 0), ctx);
456ddecdfceSMircea Gherzan 		_emit(cond, ARM_B(b_imm(ctx->skf->len, ctx)), ctx);
457ddecdfceSMircea Gherzan 	}
458ddecdfceSMircea Gherzan }
459ddecdfceSMircea Gherzan 
460ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
461ddecdfceSMircea Gherzan {
462ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5
463ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
464ddecdfceSMircea Gherzan 
465ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_THUMB)
466ddecdfceSMircea Gherzan 		emit(ARM_BX(tgt_reg), ctx);
467ddecdfceSMircea Gherzan 	else
468ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
469ddecdfceSMircea Gherzan #else
470ddecdfceSMircea Gherzan 	emit(ARM_BLX_R(tgt_reg), ctx);
471ddecdfceSMircea Gherzan #endif
472ddecdfceSMircea Gherzan }
473ddecdfceSMircea Gherzan 
474ddecdfceSMircea Gherzan static inline void emit_udiv(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx)
475ddecdfceSMircea Gherzan {
476ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7
477ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_IDIVA) {
478ddecdfceSMircea Gherzan 		emit(ARM_UDIV(rd, rm, rn), ctx);
479ddecdfceSMircea Gherzan 		return;
480ddecdfceSMircea Gherzan 	}
481ddecdfceSMircea Gherzan #endif
48219fc99d0SNicolas Schichan 
48319fc99d0SNicolas Schichan 	/*
48419fc99d0SNicolas Schichan 	 * For BPF_ALU | BPF_DIV | BPF_K instructions, rm is ARM_R4
48519fc99d0SNicolas Schichan 	 * (r_A) and rn is ARM_R0 (r_scratch) so load rn first into
48619fc99d0SNicolas Schichan 	 * ARM_R1 to avoid accidentally overwriting ARM_R0 with rm
48719fc99d0SNicolas Schichan 	 * before using it as a source for ARM_R1.
48819fc99d0SNicolas Schichan 	 *
48919fc99d0SNicolas Schichan 	 * For BPF_ALU | BPF_DIV | BPF_X rm is ARM_R4 (r_A) and rn is
49019fc99d0SNicolas Schichan 	 * ARM_R5 (r_X) so there is no particular register overlap
49119fc99d0SNicolas Schichan 	 * issues.
49219fc99d0SNicolas Schichan 	 */
493ddecdfceSMircea Gherzan 	if (rn != ARM_R1)
494ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R1, rn), ctx);
49519fc99d0SNicolas Schichan 	if (rm != ARM_R0)
49619fc99d0SNicolas Schichan 		emit(ARM_MOV_R(ARM_R0, rm), ctx);
497ddecdfceSMircea Gherzan 
498ddecdfceSMircea Gherzan 	ctx->seen |= SEEN_CALL;
499ddecdfceSMircea Gherzan 	emit_mov_i(ARM_R3, (u32)jit_udiv, ctx);
500ddecdfceSMircea Gherzan 	emit_blx_r(ARM_R3, ctx);
501ddecdfceSMircea Gherzan 
502ddecdfceSMircea Gherzan 	if (rd != ARM_R0)
503ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(rd, ARM_R0), ctx);
504ddecdfceSMircea Gherzan }
505ddecdfceSMircea Gherzan 
506ddecdfceSMircea Gherzan static inline void update_on_xread(struct jit_ctx *ctx)
507ddecdfceSMircea Gherzan {
508ddecdfceSMircea Gherzan 	if (!(ctx->seen & SEEN_X))
509ddecdfceSMircea Gherzan 		ctx->flags |= FLAG_NEED_X_RESET;
510ddecdfceSMircea Gherzan 
511ddecdfceSMircea Gherzan 	ctx->seen |= SEEN_X;
512ddecdfceSMircea Gherzan }
513ddecdfceSMircea Gherzan 
514ddecdfceSMircea Gherzan static int build_body(struct jit_ctx *ctx)
515ddecdfceSMircea Gherzan {
516ddecdfceSMircea Gherzan 	void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
5177ae457c1SAlexei Starovoitov 	const struct bpf_prog *prog = ctx->skf;
518ddecdfceSMircea Gherzan 	const struct sock_filter *inst;
519ddecdfceSMircea Gherzan 	unsigned i, load_order, off, condt;
520ddecdfceSMircea Gherzan 	int imm12;
521ddecdfceSMircea Gherzan 	u32 k;
522ddecdfceSMircea Gherzan 
523ddecdfceSMircea Gherzan 	for (i = 0; i < prog->len; i++) {
52434805931SDaniel Borkmann 		u16 code;
52534805931SDaniel Borkmann 
526ddecdfceSMircea Gherzan 		inst = &(prog->insns[i]);
527ddecdfceSMircea Gherzan 		/* K as an immediate value operand */
528ddecdfceSMircea Gherzan 		k = inst->k;
52934805931SDaniel Borkmann 		code = bpf_anc_helper(inst);
530ddecdfceSMircea Gherzan 
531ddecdfceSMircea Gherzan 		/* compute offsets only in the fake pass */
532ddecdfceSMircea Gherzan 		if (ctx->target == NULL)
533ddecdfceSMircea Gherzan 			ctx->offsets[i] = ctx->idx * 4;
534ddecdfceSMircea Gherzan 
53534805931SDaniel Borkmann 		switch (code) {
53634805931SDaniel Borkmann 		case BPF_LD | BPF_IMM:
537ddecdfceSMircea Gherzan 			emit_mov_i(r_A, k, ctx);
538ddecdfceSMircea Gherzan 			break;
53934805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_LEN:
540ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
541ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
542ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_skb,
543ddecdfceSMircea Gherzan 				       offsetof(struct sk_buff, len)), ctx);
544ddecdfceSMircea Gherzan 			break;
54534805931SDaniel Borkmann 		case BPF_LD | BPF_MEM:
546ddecdfceSMircea Gherzan 			/* A = scratch[k] */
547ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_MEM_WORD(k);
548ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
549ddecdfceSMircea Gherzan 			break;
55034805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_ABS:
551ddecdfceSMircea Gherzan 			load_order = 2;
552ddecdfceSMircea Gherzan 			goto load;
55334805931SDaniel Borkmann 		case BPF_LD | BPF_H | BPF_ABS:
554ddecdfceSMircea Gherzan 			load_order = 1;
555ddecdfceSMircea Gherzan 			goto load;
55634805931SDaniel Borkmann 		case BPF_LD | BPF_B | BPF_ABS:
557ddecdfceSMircea Gherzan 			load_order = 0;
558ddecdfceSMircea Gherzan load:
559ddecdfceSMircea Gherzan 			emit_mov_i(r_off, k, ctx);
560ddecdfceSMircea Gherzan load_common:
561ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_DATA | SEEN_CALL;
562ddecdfceSMircea Gherzan 
563ddecdfceSMircea Gherzan 			if (load_order > 0) {
564ddecdfceSMircea Gherzan 				emit(ARM_SUB_I(r_scratch, r_skb_hl,
565ddecdfceSMircea Gherzan 					       1 << load_order), ctx);
566ddecdfceSMircea Gherzan 				emit(ARM_CMP_R(r_scratch, r_off), ctx);
5677aed35cbSNicolas Schichan 				condt = ARM_COND_GE;
568ddecdfceSMircea Gherzan 			} else {
569ddecdfceSMircea Gherzan 				emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
570ddecdfceSMircea Gherzan 				condt = ARM_COND_HI;
571ddecdfceSMircea Gherzan 			}
572ddecdfceSMircea Gherzan 
5736d715e30SNicolas Schichan 			/*
5746d715e30SNicolas Schichan 			 * test for negative offset, only if we are
5756d715e30SNicolas Schichan 			 * currently scheduled to take the fast
5766d715e30SNicolas Schichan 			 * path. this will update the flags so that
5776d715e30SNicolas Schichan 			 * the slowpath instruction are ignored if the
5786d715e30SNicolas Schichan 			 * offset is negative.
5796d715e30SNicolas Schichan 			 *
5806d715e30SNicolas Schichan 			 * for loard_order == 0 the HI condition will
5816d715e30SNicolas Schichan 			 * make loads at offset 0 take the slow path too.
5826d715e30SNicolas Schichan 			 */
5836d715e30SNicolas Schichan 			_emit(condt, ARM_CMP_I(r_off, 0), ctx);
5846d715e30SNicolas Schichan 
585ddecdfceSMircea Gherzan 			_emit(condt, ARM_ADD_R(r_scratch, r_off, r_skb_data),
586ddecdfceSMircea Gherzan 			      ctx);
587ddecdfceSMircea Gherzan 
588ddecdfceSMircea Gherzan 			if (load_order == 0)
589ddecdfceSMircea Gherzan 				_emit(condt, ARM_LDRB_I(r_A, r_scratch, 0),
590ddecdfceSMircea Gherzan 				      ctx);
591ddecdfceSMircea Gherzan 			else if (load_order == 1)
592ddecdfceSMircea Gherzan 				emit_load_be16(condt, r_A, r_scratch, ctx);
593ddecdfceSMircea Gherzan 			else if (load_order == 2)
594ddecdfceSMircea Gherzan 				emit_load_be32(condt, r_A, r_scratch, ctx);
595ddecdfceSMircea Gherzan 
596ddecdfceSMircea Gherzan 			_emit(condt, ARM_B(b_imm(i + 1, ctx)), ctx);
597ddecdfceSMircea Gherzan 
598ddecdfceSMircea Gherzan 			/* the slowpath */
599ddecdfceSMircea Gherzan 			emit_mov_i(ARM_R3, (u32)load_func[load_order], ctx);
600ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
601ddecdfceSMircea Gherzan 			/* the offset is already in R1 */
602ddecdfceSMircea Gherzan 			emit_blx_r(ARM_R3, ctx);
603ddecdfceSMircea Gherzan 			/* check the result of skb_copy_bits */
604ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(ARM_R1, 0), ctx);
605ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_NE, ctx);
606ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(r_A, ARM_R0), ctx);
607ddecdfceSMircea Gherzan 			break;
60834805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_IND:
609ddecdfceSMircea Gherzan 			load_order = 2;
610ddecdfceSMircea Gherzan 			goto load_ind;
61134805931SDaniel Borkmann 		case BPF_LD | BPF_H | BPF_IND:
612ddecdfceSMircea Gherzan 			load_order = 1;
613ddecdfceSMircea Gherzan 			goto load_ind;
61434805931SDaniel Borkmann 		case BPF_LD | BPF_B | BPF_IND:
615ddecdfceSMircea Gherzan 			load_order = 0;
616ddecdfceSMircea Gherzan load_ind:
617ddecdfceSMircea Gherzan 			OP_IMM3(ARM_ADD, r_off, r_X, k, ctx);
618ddecdfceSMircea Gherzan 			goto load_common;
61934805931SDaniel Borkmann 		case BPF_LDX | BPF_IMM:
620ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X;
621ddecdfceSMircea Gherzan 			emit_mov_i(r_X, k, ctx);
622ddecdfceSMircea Gherzan 			break;
62334805931SDaniel Borkmann 		case BPF_LDX | BPF_W | BPF_LEN:
624ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X | SEEN_SKB;
625ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_X, r_skb,
626ddecdfceSMircea Gherzan 				       offsetof(struct sk_buff, len)), ctx);
627ddecdfceSMircea Gherzan 			break;
62834805931SDaniel Borkmann 		case BPF_LDX | BPF_MEM:
629ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X | SEEN_MEM_WORD(k);
630ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
631ddecdfceSMircea Gherzan 			break;
63234805931SDaniel Borkmann 		case BPF_LDX | BPF_B | BPF_MSH:
633ddecdfceSMircea Gherzan 			/* x = ((*(frame + k)) & 0xf) << 2; */
634ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X | SEEN_DATA | SEEN_CALL;
635ddecdfceSMircea Gherzan 			/* the interpreter should deal with the negative K */
63645549a68SChen Gang 			if ((int)k < 0)
637ddecdfceSMircea Gherzan 				return -1;
638ddecdfceSMircea Gherzan 			/* offset in r1: we might have to take the slow path */
639ddecdfceSMircea Gherzan 			emit_mov_i(r_off, k, ctx);
640ddecdfceSMircea Gherzan 			emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
641ddecdfceSMircea Gherzan 
642ddecdfceSMircea Gherzan 			/* load in r0: common with the slowpath */
643ddecdfceSMircea Gherzan 			_emit(ARM_COND_HI, ARM_LDRB_R(ARM_R0, r_skb_data,
644ddecdfceSMircea Gherzan 						      ARM_R1), ctx);
645ddecdfceSMircea Gherzan 			/*
646ddecdfceSMircea Gherzan 			 * emit_mov_i() might generate one or two instructions,
647ddecdfceSMircea Gherzan 			 * the same holds for emit_blx_r()
648ddecdfceSMircea Gherzan 			 */
649ddecdfceSMircea Gherzan 			_emit(ARM_COND_HI, ARM_B(b_imm(i + 1, ctx) - 2), ctx);
650ddecdfceSMircea Gherzan 
651ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
652ddecdfceSMircea Gherzan 			/* r_off is r1 */
653ddecdfceSMircea Gherzan 			emit_mov_i(ARM_R3, (u32)jit_get_skb_b, ctx);
654ddecdfceSMircea Gherzan 			emit_blx_r(ARM_R3, ctx);
655ddecdfceSMircea Gherzan 			/* check the return value of skb_copy_bits */
656ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(ARM_R1, 0), ctx);
657ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_NE, ctx);
658ddecdfceSMircea Gherzan 
659ddecdfceSMircea Gherzan 			emit(ARM_AND_I(r_X, ARM_R0, 0x00f), ctx);
660ddecdfceSMircea Gherzan 			emit(ARM_LSL_I(r_X, r_X, 2), ctx);
661ddecdfceSMircea Gherzan 			break;
66234805931SDaniel Borkmann 		case BPF_ST:
663ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_MEM_WORD(k);
664ddecdfceSMircea Gherzan 			emit(ARM_STR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
665ddecdfceSMircea Gherzan 			break;
66634805931SDaniel Borkmann 		case BPF_STX:
667ddecdfceSMircea Gherzan 			update_on_xread(ctx);
668ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_MEM_WORD(k);
669ddecdfceSMircea Gherzan 			emit(ARM_STR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
670ddecdfceSMircea Gherzan 			break;
67134805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_K:
672ddecdfceSMircea Gherzan 			/* A += K */
673ddecdfceSMircea Gherzan 			OP_IMM3(ARM_ADD, r_A, r_A, k, ctx);
674ddecdfceSMircea Gherzan 			break;
67534805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_X:
676ddecdfceSMircea Gherzan 			update_on_xread(ctx);
677ddecdfceSMircea Gherzan 			emit(ARM_ADD_R(r_A, r_A, r_X), ctx);
678ddecdfceSMircea Gherzan 			break;
67934805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_K:
680ddecdfceSMircea Gherzan 			/* A -= K */
681ddecdfceSMircea Gherzan 			OP_IMM3(ARM_SUB, r_A, r_A, k, ctx);
682ddecdfceSMircea Gherzan 			break;
68334805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_X:
684ddecdfceSMircea Gherzan 			update_on_xread(ctx);
685ddecdfceSMircea Gherzan 			emit(ARM_SUB_R(r_A, r_A, r_X), ctx);
686ddecdfceSMircea Gherzan 			break;
68734805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_K:
688ddecdfceSMircea Gherzan 			/* A *= K */
689ddecdfceSMircea Gherzan 			emit_mov_i(r_scratch, k, ctx);
690ddecdfceSMircea Gherzan 			emit(ARM_MUL(r_A, r_A, r_scratch), ctx);
691ddecdfceSMircea Gherzan 			break;
69234805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_X:
693ddecdfceSMircea Gherzan 			update_on_xread(ctx);
694ddecdfceSMircea Gherzan 			emit(ARM_MUL(r_A, r_A, r_X), ctx);
695ddecdfceSMircea Gherzan 			break;
69634805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_K:
697aee636c4SEric Dumazet 			if (k == 1)
698aee636c4SEric Dumazet 				break;
699ddecdfceSMircea Gherzan 			emit_mov_i(r_scratch, k, ctx);
700aee636c4SEric Dumazet 			emit_udiv(r_A, r_A, r_scratch, ctx);
701ddecdfceSMircea Gherzan 			break;
70234805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_X:
703ddecdfceSMircea Gherzan 			update_on_xread(ctx);
704ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(r_X, 0), ctx);
705ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_EQ, ctx);
706ddecdfceSMircea Gherzan 			emit_udiv(r_A, r_A, r_X, ctx);
707ddecdfceSMircea Gherzan 			break;
70834805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_K:
709ddecdfceSMircea Gherzan 			/* A |= K */
710ddecdfceSMircea Gherzan 			OP_IMM3(ARM_ORR, r_A, r_A, k, ctx);
711ddecdfceSMircea Gherzan 			break;
71234805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_X:
713ddecdfceSMircea Gherzan 			update_on_xread(ctx);
714ddecdfceSMircea Gherzan 			emit(ARM_ORR_R(r_A, r_A, r_X), ctx);
715ddecdfceSMircea Gherzan 			break;
71634805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_K:
7173cbe2041SDaniel Borkmann 			/* A ^= K; */
7183cbe2041SDaniel Borkmann 			OP_IMM3(ARM_EOR, r_A, r_A, k, ctx);
7193cbe2041SDaniel Borkmann 			break;
72034805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_ALU_XOR_X:
72134805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_X:
7223cbe2041SDaniel Borkmann 			/* A ^= X */
7233cbe2041SDaniel Borkmann 			update_on_xread(ctx);
7243cbe2041SDaniel Borkmann 			emit(ARM_EOR_R(r_A, r_A, r_X), ctx);
7253cbe2041SDaniel Borkmann 			break;
72634805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_K:
727ddecdfceSMircea Gherzan 			/* A &= K */
728ddecdfceSMircea Gherzan 			OP_IMM3(ARM_AND, r_A, r_A, k, ctx);
729ddecdfceSMircea Gherzan 			break;
73034805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_X:
731ddecdfceSMircea Gherzan 			update_on_xread(ctx);
732ddecdfceSMircea Gherzan 			emit(ARM_AND_R(r_A, r_A, r_X), ctx);
733ddecdfceSMircea Gherzan 			break;
73434805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_K:
735ddecdfceSMircea Gherzan 			if (unlikely(k > 31))
736ddecdfceSMircea Gherzan 				return -1;
737ddecdfceSMircea Gherzan 			emit(ARM_LSL_I(r_A, r_A, k), ctx);
738ddecdfceSMircea Gherzan 			break;
73934805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_X:
740ddecdfceSMircea Gherzan 			update_on_xread(ctx);
741ddecdfceSMircea Gherzan 			emit(ARM_LSL_R(r_A, r_A, r_X), ctx);
742ddecdfceSMircea Gherzan 			break;
74334805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_K:
744ddecdfceSMircea Gherzan 			if (unlikely(k > 31))
745ddecdfceSMircea Gherzan 				return -1;
746ddecdfceSMircea Gherzan 			emit(ARM_LSR_I(r_A, r_A, k), ctx);
747ddecdfceSMircea Gherzan 			break;
74834805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_X:
749ddecdfceSMircea Gherzan 			update_on_xread(ctx);
750ddecdfceSMircea Gherzan 			emit(ARM_LSR_R(r_A, r_A, r_X), ctx);
751ddecdfceSMircea Gherzan 			break;
75234805931SDaniel Borkmann 		case BPF_ALU | BPF_NEG:
753ddecdfceSMircea Gherzan 			/* A = -A */
754ddecdfceSMircea Gherzan 			emit(ARM_RSB_I(r_A, r_A, 0), ctx);
755ddecdfceSMircea Gherzan 			break;
75634805931SDaniel Borkmann 		case BPF_JMP | BPF_JA:
757ddecdfceSMircea Gherzan 			/* pc += K */
758ddecdfceSMircea Gherzan 			emit(ARM_B(b_imm(i + k + 1, ctx)), ctx);
759ddecdfceSMircea Gherzan 			break;
76034805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_K:
761ddecdfceSMircea Gherzan 			/* pc += (A == K) ? pc->jt : pc->jf */
762ddecdfceSMircea Gherzan 			condt  = ARM_COND_EQ;
763ddecdfceSMircea Gherzan 			goto cmp_imm;
76434805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_K:
765ddecdfceSMircea Gherzan 			/* pc += (A > K) ? pc->jt : pc->jf */
766ddecdfceSMircea Gherzan 			condt  = ARM_COND_HI;
767ddecdfceSMircea Gherzan 			goto cmp_imm;
76834805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_K:
769ddecdfceSMircea Gherzan 			/* pc += (A >= K) ? pc->jt : pc->jf */
770ddecdfceSMircea Gherzan 			condt  = ARM_COND_HS;
771ddecdfceSMircea Gherzan cmp_imm:
772ddecdfceSMircea Gherzan 			imm12 = imm8m(k);
773ddecdfceSMircea Gherzan 			if (imm12 < 0) {
774ddecdfceSMircea Gherzan 				emit_mov_i_no8m(r_scratch, k, ctx);
775ddecdfceSMircea Gherzan 				emit(ARM_CMP_R(r_A, r_scratch), ctx);
776ddecdfceSMircea Gherzan 			} else {
777ddecdfceSMircea Gherzan 				emit(ARM_CMP_I(r_A, imm12), ctx);
778ddecdfceSMircea Gherzan 			}
779ddecdfceSMircea Gherzan cond_jump:
780ddecdfceSMircea Gherzan 			if (inst->jt)
781ddecdfceSMircea Gherzan 				_emit(condt, ARM_B(b_imm(i + inst->jt + 1,
782ddecdfceSMircea Gherzan 						   ctx)), ctx);
783ddecdfceSMircea Gherzan 			if (inst->jf)
784ddecdfceSMircea Gherzan 				_emit(condt ^ 1, ARM_B(b_imm(i + inst->jf + 1,
785ddecdfceSMircea Gherzan 							     ctx)), ctx);
786ddecdfceSMircea Gherzan 			break;
78734805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_X:
788ddecdfceSMircea Gherzan 			/* pc += (A == X) ? pc->jt : pc->jf */
789ddecdfceSMircea Gherzan 			condt   = ARM_COND_EQ;
790ddecdfceSMircea Gherzan 			goto cmp_x;
79134805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_X:
792ddecdfceSMircea Gherzan 			/* pc += (A > X) ? pc->jt : pc->jf */
793ddecdfceSMircea Gherzan 			condt   = ARM_COND_HI;
794ddecdfceSMircea Gherzan 			goto cmp_x;
79534805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_X:
796ddecdfceSMircea Gherzan 			/* pc += (A >= X) ? pc->jt : pc->jf */
797ddecdfceSMircea Gherzan 			condt   = ARM_COND_CS;
798ddecdfceSMircea Gherzan cmp_x:
799ddecdfceSMircea Gherzan 			update_on_xread(ctx);
800ddecdfceSMircea Gherzan 			emit(ARM_CMP_R(r_A, r_X), ctx);
801ddecdfceSMircea Gherzan 			goto cond_jump;
80234805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_K:
803ddecdfceSMircea Gherzan 			/* pc += (A & K) ? pc->jt : pc->jf */
804ddecdfceSMircea Gherzan 			condt  = ARM_COND_NE;
805ddecdfceSMircea Gherzan 			/* not set iff all zeroes iff Z==1 iff EQ */
806ddecdfceSMircea Gherzan 
807ddecdfceSMircea Gherzan 			imm12 = imm8m(k);
808ddecdfceSMircea Gherzan 			if (imm12 < 0) {
809ddecdfceSMircea Gherzan 				emit_mov_i_no8m(r_scratch, k, ctx);
810ddecdfceSMircea Gherzan 				emit(ARM_TST_R(r_A, r_scratch), ctx);
811ddecdfceSMircea Gherzan 			} else {
812ddecdfceSMircea Gherzan 				emit(ARM_TST_I(r_A, imm12), ctx);
813ddecdfceSMircea Gherzan 			}
814ddecdfceSMircea Gherzan 			goto cond_jump;
81534805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_X:
816ddecdfceSMircea Gherzan 			/* pc += (A & X) ? pc->jt : pc->jf */
817ddecdfceSMircea Gherzan 			update_on_xread(ctx);
818ddecdfceSMircea Gherzan 			condt  = ARM_COND_NE;
819ddecdfceSMircea Gherzan 			emit(ARM_TST_R(r_A, r_X), ctx);
820ddecdfceSMircea Gherzan 			goto cond_jump;
82134805931SDaniel Borkmann 		case BPF_RET | BPF_A:
822ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(ARM_R0, r_A), ctx);
823ddecdfceSMircea Gherzan 			goto b_epilogue;
82434805931SDaniel Borkmann 		case BPF_RET | BPF_K:
825ddecdfceSMircea Gherzan 			if ((k == 0) && (ctx->ret0_fp_idx < 0))
826ddecdfceSMircea Gherzan 				ctx->ret0_fp_idx = i;
827ddecdfceSMircea Gherzan 			emit_mov_i(ARM_R0, k, ctx);
828ddecdfceSMircea Gherzan b_epilogue:
829ddecdfceSMircea Gherzan 			if (i != ctx->skf->len - 1)
830ddecdfceSMircea Gherzan 				emit(ARM_B(b_imm(prog->len, ctx)), ctx);
831ddecdfceSMircea Gherzan 			break;
83234805931SDaniel Borkmann 		case BPF_MISC | BPF_TAX:
833ddecdfceSMircea Gherzan 			/* X = A */
834ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X;
835ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(r_X, r_A), ctx);
836ddecdfceSMircea Gherzan 			break;
83734805931SDaniel Borkmann 		case BPF_MISC | BPF_TXA:
838ddecdfceSMircea Gherzan 			/* A = X */
839ddecdfceSMircea Gherzan 			update_on_xread(ctx);
840ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(r_A, r_X), ctx);
841ddecdfceSMircea Gherzan 			break;
84234805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_PROTOCOL:
843ddecdfceSMircea Gherzan 			/* A = ntohs(skb->protocol) */
844ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
845ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
846ddecdfceSMircea Gherzan 						  protocol) != 2);
847ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, protocol);
848ddecdfceSMircea Gherzan 			emit(ARM_LDRH_I(r_scratch, r_skb, off), ctx);
849ddecdfceSMircea Gherzan 			emit_swap16(r_A, r_scratch, ctx);
850ddecdfceSMircea Gherzan 			break;
85134805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_CPU:
852ddecdfceSMircea Gherzan 			/* r_scratch = current_thread_info() */
853ddecdfceSMircea Gherzan 			OP_IMM3(ARM_BIC, r_scratch, ARM_SP, THREAD_SIZE - 1, ctx);
854ddecdfceSMircea Gherzan 			/* A = current_thread_info()->cpu */
855ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4);
856ddecdfceSMircea Gherzan 			off = offsetof(struct thread_info, cpu);
857ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
858ddecdfceSMircea Gherzan 			break;
85934805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_IFINDEX:
860ddecdfceSMircea Gherzan 			/* A = skb->dev->ifindex */
861ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
862ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, dev);
863ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
864ddecdfceSMircea Gherzan 
865ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(r_scratch, 0), ctx);
866ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_EQ, ctx);
867ddecdfceSMircea Gherzan 
868ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
869ddecdfceSMircea Gherzan 						  ifindex) != 4);
870ddecdfceSMircea Gherzan 			off = offsetof(struct net_device, ifindex);
871ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
872ddecdfceSMircea Gherzan 			break;
87334805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_MARK:
874ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
875ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
876ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, mark);
877ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_skb, off), ctx);
878ddecdfceSMircea Gherzan 			break;
87934805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_RXHASH:
880ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
88161b905daSTom Herbert 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
88261b905daSTom Herbert 			off = offsetof(struct sk_buff, hash);
883ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_skb, off), ctx);
884ddecdfceSMircea Gherzan 			break;
88534805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_VLAN_TAG:
88634805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
887bf0098f2SDaniel Borkmann 			ctx->seen |= SEEN_SKB;
888bf0098f2SDaniel Borkmann 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
889bf0098f2SDaniel Borkmann 			off = offsetof(struct sk_buff, vlan_tci);
890bf0098f2SDaniel Borkmann 			emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
89134805931SDaniel Borkmann 			if (code == (BPF_ANC | SKF_AD_VLAN_TAG))
892c18fe54bSNicolas Schichan 				OP_IMM3(ARM_AND, r_A, r_A, ~VLAN_TAG_PRESENT, ctx);
893c18fe54bSNicolas Schichan 			else {
894c18fe54bSNicolas Schichan 				OP_IMM3(ARM_LSR, r_A, r_A, 12, ctx);
895c18fe54bSNicolas Schichan 				OP_IMM3(ARM_AND, r_A, r_A, 0x1, ctx);
896c18fe54bSNicolas Schichan 			}
897bf0098f2SDaniel Borkmann 			break;
898*1447f93fSNicolas Schichan 		case BPF_ANC | SKF_AD_PKTTYPE:
899*1447f93fSNicolas Schichan 			ctx->seen |= SEEN_SKB;
900*1447f93fSNicolas Schichan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
901*1447f93fSNicolas Schichan 						  __pkt_type_offset[0]) != 1);
902*1447f93fSNicolas Schichan 			off = PKT_TYPE_OFFSET();
903*1447f93fSNicolas Schichan 			emit(ARM_LDRB_I(r_A, r_skb, off), ctx);
904*1447f93fSNicolas Schichan 			emit(ARM_AND_I(r_A, r_A, PKT_TYPE_MAX), ctx);
905*1447f93fSNicolas Schichan #ifdef __BIG_ENDIAN_BITFIELD
906*1447f93fSNicolas Schichan 			emit(ARM_LSR_I(r_A, r_A, 5), ctx);
907*1447f93fSNicolas Schichan #endif
908*1447f93fSNicolas Schichan 			break;
90934805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_QUEUE:
910ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
911ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
912ddecdfceSMircea Gherzan 						  queue_mapping) != 2);
913ddecdfceSMircea Gherzan 			BUILD_BUG_ON(offsetof(struct sk_buff,
914ddecdfceSMircea Gherzan 					      queue_mapping) > 0xff);
915ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, queue_mapping);
916ddecdfceSMircea Gherzan 			emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
917ddecdfceSMircea Gherzan 			break;
91824e737c1SNicolas Schichan 		case BPF_LDX | BPF_W | BPF_ABS:
91924e737c1SNicolas Schichan 			/*
92024e737c1SNicolas Schichan 			 * load a 32bit word from struct seccomp_data.
92124e737c1SNicolas Schichan 			 * seccomp_check_filter() will already have checked
92224e737c1SNicolas Schichan 			 * that k is 32bit aligned and lies within the
92324e737c1SNicolas Schichan 			 * struct seccomp_data.
92424e737c1SNicolas Schichan 			 */
92524e737c1SNicolas Schichan 			ctx->seen |= SEEN_SKB;
92624e737c1SNicolas Schichan 			emit(ARM_LDR_I(r_A, r_skb, k), ctx);
92724e737c1SNicolas Schichan 			break;
928ddecdfceSMircea Gherzan 		default:
929ddecdfceSMircea Gherzan 			return -1;
930ddecdfceSMircea Gherzan 		}
9310b59d880SNicolas Schichan 
9320b59d880SNicolas Schichan 		if (ctx->flags & FLAG_IMM_OVERFLOW)
9330b59d880SNicolas Schichan 			/*
9340b59d880SNicolas Schichan 			 * this instruction generated an overflow when
9350b59d880SNicolas Schichan 			 * trying to access the literal pool, so
9360b59d880SNicolas Schichan 			 * delegate this filter to the kernel interpreter.
9370b59d880SNicolas Schichan 			 */
9380b59d880SNicolas Schichan 			return -1;
939ddecdfceSMircea Gherzan 	}
940ddecdfceSMircea Gherzan 
941ddecdfceSMircea Gherzan 	/* compute offsets only during the first pass */
942ddecdfceSMircea Gherzan 	if (ctx->target == NULL)
943ddecdfceSMircea Gherzan 		ctx->offsets[i] = ctx->idx * 4;
944ddecdfceSMircea Gherzan 
945ddecdfceSMircea Gherzan 	return 0;
946ddecdfceSMircea Gherzan }
947ddecdfceSMircea Gherzan 
948ddecdfceSMircea Gherzan 
9497ae457c1SAlexei Starovoitov void bpf_jit_compile(struct bpf_prog *fp)
950ddecdfceSMircea Gherzan {
95155309dd3SDaniel Borkmann 	struct bpf_binary_header *header;
952ddecdfceSMircea Gherzan 	struct jit_ctx ctx;
953ddecdfceSMircea Gherzan 	unsigned tmp_idx;
954ddecdfceSMircea Gherzan 	unsigned alloc_size;
95555309dd3SDaniel Borkmann 	u8 *target_ptr;
956ddecdfceSMircea Gherzan 
957ddecdfceSMircea Gherzan 	if (!bpf_jit_enable)
958ddecdfceSMircea Gherzan 		return;
959ddecdfceSMircea Gherzan 
960ddecdfceSMircea Gherzan 	memset(&ctx, 0, sizeof(ctx));
961ddecdfceSMircea Gherzan 	ctx.skf		= fp;
962ddecdfceSMircea Gherzan 	ctx.ret0_fp_idx = -1;
963ddecdfceSMircea Gherzan 
96489c2e009SSchichan Nicolas 	ctx.offsets = kzalloc(4 * (ctx.skf->len + 1), GFP_KERNEL);
965ddecdfceSMircea Gherzan 	if (ctx.offsets == NULL)
966ddecdfceSMircea Gherzan 		return;
967ddecdfceSMircea Gherzan 
968ddecdfceSMircea Gherzan 	/* fake pass to fill in the ctx->seen */
969ddecdfceSMircea Gherzan 	if (unlikely(build_body(&ctx)))
970ddecdfceSMircea Gherzan 		goto out;
971ddecdfceSMircea Gherzan 
972ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
973ddecdfceSMircea Gherzan 	build_prologue(&ctx);
974ddecdfceSMircea Gherzan 	ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
975ddecdfceSMircea Gherzan 
976ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
977ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
978ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
979ddecdfceSMircea Gherzan 	ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
980ddecdfceSMircea Gherzan 
981ddecdfceSMircea Gherzan 	ctx.idx += ctx.imm_count;
982ddecdfceSMircea Gherzan 	if (ctx.imm_count) {
98389c2e009SSchichan Nicolas 		ctx.imms = kzalloc(4 * ctx.imm_count, GFP_KERNEL);
984ddecdfceSMircea Gherzan 		if (ctx.imms == NULL)
985ddecdfceSMircea Gherzan 			goto out;
986ddecdfceSMircea Gherzan 	}
987ddecdfceSMircea Gherzan #else
988ddecdfceSMircea Gherzan 	/* there's nothing after the epilogue on ARMv7 */
989ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
990ddecdfceSMircea Gherzan #endif
991ddecdfceSMircea Gherzan 	alloc_size = 4 * ctx.idx;
99255309dd3SDaniel Borkmann 	header = bpf_jit_binary_alloc(alloc_size, &target_ptr,
99355309dd3SDaniel Borkmann 				      4, jit_fill_hole);
99455309dd3SDaniel Borkmann 	if (header == NULL)
995ddecdfceSMircea Gherzan 		goto out;
996ddecdfceSMircea Gherzan 
99755309dd3SDaniel Borkmann 	ctx.target = (u32 *) target_ptr;
998ddecdfceSMircea Gherzan 	ctx.idx = 0;
99955309dd3SDaniel Borkmann 
1000ddecdfceSMircea Gherzan 	build_prologue(&ctx);
10010b59d880SNicolas Schichan 	if (build_body(&ctx) < 0) {
10020b59d880SNicolas Schichan #if __LINUX_ARM_ARCH__ < 7
10030b59d880SNicolas Schichan 		if (ctx.imm_count)
10040b59d880SNicolas Schichan 			kfree(ctx.imms);
10050b59d880SNicolas Schichan #endif
10060b59d880SNicolas Schichan 		bpf_jit_binary_free(header);
10070b59d880SNicolas Schichan 		goto out;
10080b59d880SNicolas Schichan 	}
1009ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
1010ddecdfceSMircea Gherzan 
1011ddecdfceSMircea Gherzan 	flush_icache_range((u32)ctx.target, (u32)(ctx.target + ctx.idx));
1012ddecdfceSMircea Gherzan 
1013ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
1014ddecdfceSMircea Gherzan 	if (ctx.imm_count)
1015ddecdfceSMircea Gherzan 		kfree(ctx.imms);
1016ddecdfceSMircea Gherzan #endif
1017ddecdfceSMircea Gherzan 
1018ddecdfceSMircea Gherzan 	if (bpf_jit_enable > 1)
101979617801SDaniel Borkmann 		/* there are 2 passes here */
102079617801SDaniel Borkmann 		bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
1021ddecdfceSMircea Gherzan 
102255309dd3SDaniel Borkmann 	set_memory_ro((unsigned long)header, header->pages);
1023ddecdfceSMircea Gherzan 	fp->bpf_func = (void *)ctx.target;
1024286aad3cSDaniel Borkmann 	fp->jited = true;
1025ddecdfceSMircea Gherzan out:
1026ddecdfceSMircea Gherzan 	kfree(ctx.offsets);
1027ddecdfceSMircea Gherzan 	return;
1028ddecdfceSMircea Gherzan }
1029ddecdfceSMircea Gherzan 
10307ae457c1SAlexei Starovoitov void bpf_jit_free(struct bpf_prog *fp)
1031ddecdfceSMircea Gherzan {
103255309dd3SDaniel Borkmann 	unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
103355309dd3SDaniel Borkmann 	struct bpf_binary_header *header = (void *)addr;
103460a3b225SDaniel Borkmann 
103555309dd3SDaniel Borkmann 	if (!fp->jited)
103655309dd3SDaniel Borkmann 		goto free_filter;
103755309dd3SDaniel Borkmann 
103855309dd3SDaniel Borkmann 	set_memory_rw(addr, header->pages);
103955309dd3SDaniel Borkmann 	bpf_jit_binary_free(header);
104055309dd3SDaniel Borkmann 
104155309dd3SDaniel Borkmann free_filter:
104260a3b225SDaniel Borkmann 	bpf_prog_unlock_free(fp);
1043ddecdfceSMircea Gherzan }
1044