xref: /openbmc/linux/arch/arm/net/bpf_jit_32.c (revision 0b59d8806a31bb0267b3a461e8fef20c727bdbf6)
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)
57*0b59d880SNicolas 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 
77ddecdfceSMircea Gherzan static u64 jit_get_skb_b(struct sk_buff *skb, unsigned offset)
78ddecdfceSMircea Gherzan {
79ddecdfceSMircea Gherzan 	u8 ret;
80ddecdfceSMircea Gherzan 	int err;
81ddecdfceSMircea Gherzan 
82ddecdfceSMircea Gherzan 	err = skb_copy_bits(skb, offset, &ret, 1);
83ddecdfceSMircea Gherzan 
84ddecdfceSMircea Gherzan 	return (u64)err << 32 | ret;
85ddecdfceSMircea Gherzan }
86ddecdfceSMircea Gherzan 
87ddecdfceSMircea Gherzan static u64 jit_get_skb_h(struct sk_buff *skb, unsigned offset)
88ddecdfceSMircea Gherzan {
89ddecdfceSMircea Gherzan 	u16 ret;
90ddecdfceSMircea Gherzan 	int err;
91ddecdfceSMircea Gherzan 
92ddecdfceSMircea Gherzan 	err = skb_copy_bits(skb, offset, &ret, 2);
93ddecdfceSMircea Gherzan 
94ddecdfceSMircea Gherzan 	return (u64)err << 32 | ntohs(ret);
95ddecdfceSMircea Gherzan }
96ddecdfceSMircea Gherzan 
97ddecdfceSMircea Gherzan static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
98ddecdfceSMircea Gherzan {
99ddecdfceSMircea Gherzan 	u32 ret;
100ddecdfceSMircea Gherzan 	int err;
101ddecdfceSMircea Gherzan 
102ddecdfceSMircea Gherzan 	err = skb_copy_bits(skb, offset, &ret, 4);
103ddecdfceSMircea Gherzan 
104ddecdfceSMircea Gherzan 	return (u64)err << 32 | ntohl(ret);
105ddecdfceSMircea Gherzan }
106ddecdfceSMircea Gherzan 
107ddecdfceSMircea Gherzan /*
108ddecdfceSMircea Gherzan  * Wrapper that handles both OABI and EABI and assures Thumb2 interworking
109ddecdfceSMircea Gherzan  * (where the assembly routines like __aeabi_uidiv could cause problems).
110ddecdfceSMircea Gherzan  */
111ddecdfceSMircea Gherzan static u32 jit_udiv(u32 dividend, u32 divisor)
112ddecdfceSMircea Gherzan {
113ddecdfceSMircea Gherzan 	return dividend / divisor;
114ddecdfceSMircea Gherzan }
115ddecdfceSMircea Gherzan 
116ddecdfceSMircea Gherzan static inline void _emit(int cond, u32 inst, struct jit_ctx *ctx)
117ddecdfceSMircea Gherzan {
1183460743eSBen Dooks 	inst |= (cond << 28);
1193460743eSBen Dooks 	inst = __opcode_to_mem_arm(inst);
1203460743eSBen Dooks 
121ddecdfceSMircea Gherzan 	if (ctx->target != NULL)
1223460743eSBen Dooks 		ctx->target[ctx->idx] = inst;
123ddecdfceSMircea Gherzan 
124ddecdfceSMircea Gherzan 	ctx->idx++;
125ddecdfceSMircea Gherzan }
126ddecdfceSMircea Gherzan 
127ddecdfceSMircea Gherzan /*
128ddecdfceSMircea Gherzan  * Emit an instruction that will be executed unconditionally.
129ddecdfceSMircea Gherzan  */
130ddecdfceSMircea Gherzan static inline void emit(u32 inst, struct jit_ctx *ctx)
131ddecdfceSMircea Gherzan {
132ddecdfceSMircea Gherzan 	_emit(ARM_COND_AL, inst, ctx);
133ddecdfceSMircea Gherzan }
134ddecdfceSMircea Gherzan 
135ddecdfceSMircea Gherzan static u16 saved_regs(struct jit_ctx *ctx)
136ddecdfceSMircea Gherzan {
137ddecdfceSMircea Gherzan 	u16 ret = 0;
138ddecdfceSMircea Gherzan 
139ddecdfceSMircea Gherzan 	if ((ctx->skf->len > 1) ||
14034805931SDaniel Borkmann 	    (ctx->skf->insns[0].code == (BPF_RET | BPF_A)))
141ddecdfceSMircea Gherzan 		ret |= 1 << r_A;
142ddecdfceSMircea Gherzan 
143ddecdfceSMircea Gherzan #ifdef CONFIG_FRAME_POINTER
144ddecdfceSMircea Gherzan 	ret |= (1 << ARM_FP) | (1 << ARM_IP) | (1 << ARM_LR) | (1 << ARM_PC);
145ddecdfceSMircea Gherzan #else
146ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_CALL)
147ddecdfceSMircea Gherzan 		ret |= 1 << ARM_LR;
148ddecdfceSMircea Gherzan #endif
149ddecdfceSMircea Gherzan 	if (ctx->seen & (SEEN_DATA | SEEN_SKB))
150ddecdfceSMircea Gherzan 		ret |= 1 << r_skb;
151ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_DATA)
152ddecdfceSMircea Gherzan 		ret |= (1 << r_skb_data) | (1 << r_skb_hl);
153ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_X)
154ddecdfceSMircea Gherzan 		ret |= 1 << r_X;
155ddecdfceSMircea Gherzan 
156ddecdfceSMircea Gherzan 	return ret;
157ddecdfceSMircea Gherzan }
158ddecdfceSMircea Gherzan 
159ddecdfceSMircea Gherzan static inline int mem_words_used(struct jit_ctx *ctx)
160ddecdfceSMircea Gherzan {
161ddecdfceSMircea Gherzan 	/* yes, we do waste some stack space IF there are "holes" in the set" */
162ddecdfceSMircea Gherzan 	return fls(ctx->seen & SEEN_MEM);
163ddecdfceSMircea Gherzan }
164ddecdfceSMircea Gherzan 
165ddecdfceSMircea Gherzan static inline bool is_load_to_a(u16 inst)
166ddecdfceSMircea Gherzan {
167ddecdfceSMircea Gherzan 	switch (inst) {
16834805931SDaniel Borkmann 	case BPF_LD | BPF_W | BPF_LEN:
16934805931SDaniel Borkmann 	case BPF_LD | BPF_W | BPF_ABS:
17034805931SDaniel Borkmann 	case BPF_LD | BPF_H | BPF_ABS:
17134805931SDaniel Borkmann 	case BPF_LD | BPF_B | BPF_ABS:
172ddecdfceSMircea Gherzan 		return true;
173ddecdfceSMircea Gherzan 	default:
174ddecdfceSMircea Gherzan 		return false;
175ddecdfceSMircea Gherzan 	}
176ddecdfceSMircea Gherzan }
177ddecdfceSMircea Gherzan 
17855309dd3SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
17955309dd3SDaniel Borkmann {
180e8b56d55SDaniel Borkmann 	u32 *ptr;
18155309dd3SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
18255309dd3SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
183e8b56d55SDaniel Borkmann 		*ptr++ = __opcode_to_mem_arm(ARM_INST_UDF);
18455309dd3SDaniel Borkmann }
18555309dd3SDaniel Borkmann 
186ddecdfceSMircea Gherzan static void build_prologue(struct jit_ctx *ctx)
187ddecdfceSMircea Gherzan {
188ddecdfceSMircea Gherzan 	u16 reg_set = saved_regs(ctx);
189ddecdfceSMircea Gherzan 	u16 first_inst = ctx->skf->insns[0].code;
190ddecdfceSMircea Gherzan 	u16 off;
191ddecdfceSMircea Gherzan 
192ddecdfceSMircea Gherzan #ifdef CONFIG_FRAME_POINTER
193ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_IP, ARM_SP), ctx);
194ddecdfceSMircea Gherzan 	emit(ARM_PUSH(reg_set), ctx);
195ddecdfceSMircea Gherzan 	emit(ARM_SUB_I(ARM_FP, ARM_IP, 4), ctx);
196ddecdfceSMircea Gherzan #else
197ddecdfceSMircea Gherzan 	if (reg_set)
198ddecdfceSMircea Gherzan 		emit(ARM_PUSH(reg_set), ctx);
199ddecdfceSMircea Gherzan #endif
200ddecdfceSMircea Gherzan 
201ddecdfceSMircea Gherzan 	if (ctx->seen & (SEEN_DATA | SEEN_SKB))
202ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(r_skb, ARM_R0), ctx);
203ddecdfceSMircea Gherzan 
204ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_DATA) {
205ddecdfceSMircea Gherzan 		off = offsetof(struct sk_buff, data);
206ddecdfceSMircea Gherzan 		emit(ARM_LDR_I(r_skb_data, r_skb, off), ctx);
207ddecdfceSMircea Gherzan 		/* headlen = len - data_len */
208ddecdfceSMircea Gherzan 		off = offsetof(struct sk_buff, len);
209ddecdfceSMircea Gherzan 		emit(ARM_LDR_I(r_skb_hl, r_skb, off), ctx);
210ddecdfceSMircea Gherzan 		off = offsetof(struct sk_buff, data_len);
211ddecdfceSMircea Gherzan 		emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
212ddecdfceSMircea Gherzan 		emit(ARM_SUB_R(r_skb_hl, r_skb_hl, r_scratch), ctx);
213ddecdfceSMircea Gherzan 	}
214ddecdfceSMircea Gherzan 
215ddecdfceSMircea Gherzan 	if (ctx->flags & FLAG_NEED_X_RESET)
216ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(r_X, 0), ctx);
217ddecdfceSMircea Gherzan 
218ddecdfceSMircea Gherzan 	/* do not leak kernel data to userspace */
21934805931SDaniel Borkmann 	if ((first_inst != (BPF_RET | BPF_K)) && !(is_load_to_a(first_inst)))
220ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(r_A, 0), ctx);
221ddecdfceSMircea Gherzan 
222ddecdfceSMircea Gherzan 	/* stack space for the BPF_MEM words */
223ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_MEM)
224ddecdfceSMircea Gherzan 		emit(ARM_SUB_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
225ddecdfceSMircea Gherzan }
226ddecdfceSMircea Gherzan 
227ddecdfceSMircea Gherzan static void build_epilogue(struct jit_ctx *ctx)
228ddecdfceSMircea Gherzan {
229ddecdfceSMircea Gherzan 	u16 reg_set = saved_regs(ctx);
230ddecdfceSMircea Gherzan 
231ddecdfceSMircea Gherzan 	if (ctx->seen & SEEN_MEM)
232ddecdfceSMircea Gherzan 		emit(ARM_ADD_I(ARM_SP, ARM_SP, mem_words_used(ctx) * 4), ctx);
233ddecdfceSMircea Gherzan 
234ddecdfceSMircea Gherzan 	reg_set &= ~(1 << ARM_LR);
235ddecdfceSMircea Gherzan 
236ddecdfceSMircea Gherzan #ifdef CONFIG_FRAME_POINTER
237ddecdfceSMircea Gherzan 	/* the first instruction of the prologue was: mov ip, sp */
238ddecdfceSMircea Gherzan 	reg_set &= ~(1 << ARM_IP);
239ddecdfceSMircea Gherzan 	reg_set |= (1 << ARM_SP);
240ddecdfceSMircea Gherzan 	emit(ARM_LDM(ARM_SP, reg_set), ctx);
241ddecdfceSMircea Gherzan #else
242ddecdfceSMircea Gherzan 	if (reg_set) {
243ddecdfceSMircea Gherzan 		if (ctx->seen & SEEN_CALL)
244ddecdfceSMircea Gherzan 			reg_set |= 1 << ARM_PC;
245ddecdfceSMircea Gherzan 		emit(ARM_POP(reg_set), ctx);
246ddecdfceSMircea Gherzan 	}
247ddecdfceSMircea Gherzan 
248ddecdfceSMircea Gherzan 	if (!(ctx->seen & SEEN_CALL))
249ddecdfceSMircea Gherzan 		emit(ARM_BX(ARM_LR), ctx);
250ddecdfceSMircea Gherzan #endif
251ddecdfceSMircea Gherzan }
252ddecdfceSMircea Gherzan 
253ddecdfceSMircea Gherzan static int16_t imm8m(u32 x)
254ddecdfceSMircea Gherzan {
255ddecdfceSMircea Gherzan 	u32 rot;
256ddecdfceSMircea Gherzan 
257ddecdfceSMircea Gherzan 	for (rot = 0; rot < 16; rot++)
258ddecdfceSMircea Gherzan 		if ((x & ~ror32(0xff, 2 * rot)) == 0)
259ddecdfceSMircea Gherzan 			return rol32(x, 2 * rot) | (rot << 8);
260ddecdfceSMircea Gherzan 
261ddecdfceSMircea Gherzan 	return -1;
262ddecdfceSMircea Gherzan }
263ddecdfceSMircea Gherzan 
264ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
265ddecdfceSMircea Gherzan 
266ddecdfceSMircea Gherzan static u16 imm_offset(u32 k, struct jit_ctx *ctx)
267ddecdfceSMircea Gherzan {
268ddecdfceSMircea Gherzan 	unsigned i = 0, offset;
269ddecdfceSMircea Gherzan 	u16 imm;
270ddecdfceSMircea Gherzan 
271ddecdfceSMircea Gherzan 	/* on the "fake" run we just count them (duplicates included) */
272ddecdfceSMircea Gherzan 	if (ctx->target == NULL) {
273ddecdfceSMircea Gherzan 		ctx->imm_count++;
274ddecdfceSMircea Gherzan 		return 0;
275ddecdfceSMircea Gherzan 	}
276ddecdfceSMircea Gherzan 
277ddecdfceSMircea Gherzan 	while ((i < ctx->imm_count) && ctx->imms[i]) {
278ddecdfceSMircea Gherzan 		if (ctx->imms[i] == k)
279ddecdfceSMircea Gherzan 			break;
280ddecdfceSMircea Gherzan 		i++;
281ddecdfceSMircea Gherzan 	}
282ddecdfceSMircea Gherzan 
283ddecdfceSMircea Gherzan 	if (ctx->imms[i] == 0)
284ddecdfceSMircea Gherzan 		ctx->imms[i] = k;
285ddecdfceSMircea Gherzan 
286ddecdfceSMircea Gherzan 	/* constants go just after the epilogue */
287ddecdfceSMircea Gherzan 	offset =  ctx->offsets[ctx->skf->len];
288ddecdfceSMircea Gherzan 	offset += ctx->prologue_bytes;
289ddecdfceSMircea Gherzan 	offset += ctx->epilogue_bytes;
290ddecdfceSMircea Gherzan 	offset += i * 4;
291ddecdfceSMircea Gherzan 
292ddecdfceSMircea Gherzan 	ctx->target[offset / 4] = k;
293ddecdfceSMircea Gherzan 
294ddecdfceSMircea Gherzan 	/* PC in ARM mode == address of the instruction + 8 */
295ddecdfceSMircea Gherzan 	imm = offset - (8 + ctx->idx * 4);
296ddecdfceSMircea Gherzan 
297*0b59d880SNicolas Schichan 	if (imm & ~0xfff) {
298*0b59d880SNicolas Schichan 		/*
299*0b59d880SNicolas Schichan 		 * literal pool is too far, signal it into flags. we
300*0b59d880SNicolas Schichan 		 * can only detect it on the second pass unfortunately.
301*0b59d880SNicolas Schichan 		 */
302*0b59d880SNicolas Schichan 		ctx->flags |= FLAG_IMM_OVERFLOW;
303*0b59d880SNicolas Schichan 		return 0;
304*0b59d880SNicolas Schichan 	}
305*0b59d880SNicolas Schichan 
306ddecdfceSMircea Gherzan 	return imm;
307ddecdfceSMircea Gherzan }
308ddecdfceSMircea Gherzan 
309ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ */
310ddecdfceSMircea Gherzan 
311ddecdfceSMircea Gherzan /*
312ddecdfceSMircea Gherzan  * Move an immediate that's not an imm8m to a core register.
313ddecdfceSMircea Gherzan  */
314ddecdfceSMircea Gherzan static inline void emit_mov_i_no8m(int rd, u32 val, struct jit_ctx *ctx)
315ddecdfceSMircea Gherzan {
316ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
317ddecdfceSMircea Gherzan 	emit(ARM_LDR_I(rd, ARM_PC, imm_offset(val, ctx)), ctx);
318ddecdfceSMircea Gherzan #else
319ddecdfceSMircea Gherzan 	emit(ARM_MOVW(rd, val & 0xffff), ctx);
320ddecdfceSMircea Gherzan 	if (val > 0xffff)
321ddecdfceSMircea Gherzan 		emit(ARM_MOVT(rd, val >> 16), ctx);
322ddecdfceSMircea Gherzan #endif
323ddecdfceSMircea Gherzan }
324ddecdfceSMircea Gherzan 
325ddecdfceSMircea Gherzan static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx)
326ddecdfceSMircea Gherzan {
327ddecdfceSMircea Gherzan 	int imm12 = imm8m(val);
328ddecdfceSMircea Gherzan 
329ddecdfceSMircea Gherzan 	if (imm12 >= 0)
330ddecdfceSMircea Gherzan 		emit(ARM_MOV_I(rd, imm12), ctx);
331ddecdfceSMircea Gherzan 	else
332ddecdfceSMircea Gherzan 		emit_mov_i_no8m(rd, val, ctx);
333ddecdfceSMircea Gherzan }
334ddecdfceSMircea Gherzan 
335ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 6
336ddecdfceSMircea Gherzan 
337ddecdfceSMircea Gherzan static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
338ddecdfceSMircea Gherzan {
339ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R3, r_addr, 1), ctx);
340ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
341ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 3), ctx);
342ddecdfceSMircea Gherzan 	_emit(cond, ARM_LSL_I(ARM_R3, ARM_R3, 16), ctx);
343ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R0, r_addr, 2), ctx);
344ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_S(ARM_R3, ARM_R3, ARM_R1, SRTYPE_LSL, 24), ctx);
345ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_R(ARM_R3, ARM_R3, ARM_R2), ctx);
346ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_S(r_res, ARM_R3, ARM_R0, SRTYPE_LSL, 8), ctx);
347ddecdfceSMircea Gherzan }
348ddecdfceSMircea Gherzan 
349ddecdfceSMircea Gherzan static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
350ddecdfceSMircea Gherzan {
351ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R1, r_addr, 0), ctx);
352ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRB_I(ARM_R2, r_addr, 1), ctx);
353ddecdfceSMircea Gherzan 	_emit(cond, ARM_ORR_S(r_res, ARM_R2, ARM_R1, SRTYPE_LSL, 8), ctx);
354ddecdfceSMircea Gherzan }
355ddecdfceSMircea Gherzan 
356ddecdfceSMircea Gherzan static inline void emit_swap16(u8 r_dst, u8 r_src, struct jit_ctx *ctx)
357ddecdfceSMircea Gherzan {
358462738f4SNicolas Schichan 	/* r_dst = (r_src << 8) | (r_src >> 8) */
359462738f4SNicolas Schichan 	emit(ARM_LSL_I(ARM_R1, r_src, 8), ctx);
360462738f4SNicolas Schichan 	emit(ARM_ORR_S(r_dst, ARM_R1, r_src, SRTYPE_LSR, 8), ctx);
361462738f4SNicolas Schichan 
362462738f4SNicolas Schichan 	/*
363462738f4SNicolas Schichan 	 * we need to mask out the bits set in r_dst[23:16] due to
364462738f4SNicolas Schichan 	 * the first shift instruction.
365462738f4SNicolas Schichan 	 *
366462738f4SNicolas Schichan 	 * note that 0x8ff is the encoded immediate 0x00ff0000.
367462738f4SNicolas Schichan 	 */
368462738f4SNicolas Schichan 	emit(ARM_BIC_I(r_dst, r_dst, 0x8ff), ctx);
369ddecdfceSMircea Gherzan }
370ddecdfceSMircea Gherzan 
371ddecdfceSMircea Gherzan #else  /* ARMv6+ */
372ddecdfceSMircea Gherzan 
373ddecdfceSMircea Gherzan static void emit_load_be32(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
374ddecdfceSMircea Gherzan {
375ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDR_I(r_res, r_addr, 0), ctx);
376ddecdfceSMircea Gherzan #ifdef __LITTLE_ENDIAN
377ddecdfceSMircea Gherzan 	_emit(cond, ARM_REV(r_res, r_res), ctx);
378ddecdfceSMircea Gherzan #endif
379ddecdfceSMircea Gherzan }
380ddecdfceSMircea Gherzan 
381ddecdfceSMircea Gherzan static void emit_load_be16(u8 cond, u8 r_res, u8 r_addr, struct jit_ctx *ctx)
382ddecdfceSMircea Gherzan {
383ddecdfceSMircea Gherzan 	_emit(cond, ARM_LDRH_I(r_res, r_addr, 0), ctx);
384ddecdfceSMircea Gherzan #ifdef __LITTLE_ENDIAN
385ddecdfceSMircea Gherzan 	_emit(cond, ARM_REV16(r_res, r_res), ctx);
386ddecdfceSMircea Gherzan #endif
387ddecdfceSMircea Gherzan }
388ddecdfceSMircea Gherzan 
389ddecdfceSMircea Gherzan static inline void emit_swap16(u8 r_dst __maybe_unused,
390ddecdfceSMircea Gherzan 			       u8 r_src __maybe_unused,
391ddecdfceSMircea Gherzan 			       struct jit_ctx *ctx __maybe_unused)
392ddecdfceSMircea Gherzan {
393ddecdfceSMircea Gherzan #ifdef __LITTLE_ENDIAN
394ddecdfceSMircea Gherzan 	emit(ARM_REV16(r_dst, r_src), ctx);
395ddecdfceSMircea Gherzan #endif
396ddecdfceSMircea Gherzan }
397ddecdfceSMircea Gherzan 
398ddecdfceSMircea Gherzan #endif /* __LINUX_ARM_ARCH__ < 6 */
399ddecdfceSMircea Gherzan 
400ddecdfceSMircea Gherzan 
401ddecdfceSMircea Gherzan /* Compute the immediate value for a PC-relative branch. */
402ddecdfceSMircea Gherzan static inline u32 b_imm(unsigned tgt, struct jit_ctx *ctx)
403ddecdfceSMircea Gherzan {
404ddecdfceSMircea Gherzan 	u32 imm;
405ddecdfceSMircea Gherzan 
406ddecdfceSMircea Gherzan 	if (ctx->target == NULL)
407ddecdfceSMircea Gherzan 		return 0;
408ddecdfceSMircea Gherzan 	/*
409ddecdfceSMircea Gherzan 	 * BPF allows only forward jumps and the offset of the target is
410ddecdfceSMircea Gherzan 	 * still the one computed during the first pass.
411ddecdfceSMircea Gherzan 	 */
412ddecdfceSMircea Gherzan 	imm  = ctx->offsets[tgt] + ctx->prologue_bytes - (ctx->idx * 4 + 8);
413ddecdfceSMircea Gherzan 
414ddecdfceSMircea Gherzan 	return imm >> 2;
415ddecdfceSMircea Gherzan }
416ddecdfceSMircea Gherzan 
417ddecdfceSMircea Gherzan #define OP_IMM3(op, r1, r2, imm_val, ctx)				\
418ddecdfceSMircea Gherzan 	do {								\
419ddecdfceSMircea Gherzan 		imm12 = imm8m(imm_val);					\
420ddecdfceSMircea Gherzan 		if (imm12 < 0) {					\
421ddecdfceSMircea Gherzan 			emit_mov_i_no8m(r_scratch, imm_val, ctx);	\
422ddecdfceSMircea Gherzan 			emit(op ## _R((r1), (r2), r_scratch), ctx);	\
423ddecdfceSMircea Gherzan 		} else {						\
424ddecdfceSMircea Gherzan 			emit(op ## _I((r1), (r2), imm12), ctx);		\
425ddecdfceSMircea Gherzan 		}							\
426ddecdfceSMircea Gherzan 	} while (0)
427ddecdfceSMircea Gherzan 
428ddecdfceSMircea Gherzan static inline void emit_err_ret(u8 cond, struct jit_ctx *ctx)
429ddecdfceSMircea Gherzan {
430ddecdfceSMircea Gherzan 	if (ctx->ret0_fp_idx >= 0) {
431ddecdfceSMircea Gherzan 		_emit(cond, ARM_B(b_imm(ctx->ret0_fp_idx, ctx)), ctx);
432ddecdfceSMircea Gherzan 		/* NOP to keep the size constant between passes */
433ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R0, ARM_R0), ctx);
434ddecdfceSMircea Gherzan 	} else {
435ddecdfceSMircea Gherzan 		_emit(cond, ARM_MOV_I(ARM_R0, 0), ctx);
436ddecdfceSMircea Gherzan 		_emit(cond, ARM_B(b_imm(ctx->skf->len, ctx)), ctx);
437ddecdfceSMircea Gherzan 	}
438ddecdfceSMircea Gherzan }
439ddecdfceSMircea Gherzan 
440ddecdfceSMircea Gherzan static inline void emit_blx_r(u8 tgt_reg, struct jit_ctx *ctx)
441ddecdfceSMircea Gherzan {
442ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 5
443ddecdfceSMircea Gherzan 	emit(ARM_MOV_R(ARM_LR, ARM_PC), ctx);
444ddecdfceSMircea Gherzan 
445ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_THUMB)
446ddecdfceSMircea Gherzan 		emit(ARM_BX(tgt_reg), ctx);
447ddecdfceSMircea Gherzan 	else
448ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_PC, tgt_reg), ctx);
449ddecdfceSMircea Gherzan #else
450ddecdfceSMircea Gherzan 	emit(ARM_BLX_R(tgt_reg), ctx);
451ddecdfceSMircea Gherzan #endif
452ddecdfceSMircea Gherzan }
453ddecdfceSMircea Gherzan 
454ddecdfceSMircea Gherzan static inline void emit_udiv(u8 rd, u8 rm, u8 rn, struct jit_ctx *ctx)
455ddecdfceSMircea Gherzan {
456ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ == 7
457ddecdfceSMircea Gherzan 	if (elf_hwcap & HWCAP_IDIVA) {
458ddecdfceSMircea Gherzan 		emit(ARM_UDIV(rd, rm, rn), ctx);
459ddecdfceSMircea Gherzan 		return;
460ddecdfceSMircea Gherzan 	}
461ddecdfceSMircea Gherzan #endif
46219fc99d0SNicolas Schichan 
46319fc99d0SNicolas Schichan 	/*
46419fc99d0SNicolas Schichan 	 * For BPF_ALU | BPF_DIV | BPF_K instructions, rm is ARM_R4
46519fc99d0SNicolas Schichan 	 * (r_A) and rn is ARM_R0 (r_scratch) so load rn first into
46619fc99d0SNicolas Schichan 	 * ARM_R1 to avoid accidentally overwriting ARM_R0 with rm
46719fc99d0SNicolas Schichan 	 * before using it as a source for ARM_R1.
46819fc99d0SNicolas Schichan 	 *
46919fc99d0SNicolas Schichan 	 * For BPF_ALU | BPF_DIV | BPF_X rm is ARM_R4 (r_A) and rn is
47019fc99d0SNicolas Schichan 	 * ARM_R5 (r_X) so there is no particular register overlap
47119fc99d0SNicolas Schichan 	 * issues.
47219fc99d0SNicolas Schichan 	 */
473ddecdfceSMircea Gherzan 	if (rn != ARM_R1)
474ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(ARM_R1, rn), ctx);
47519fc99d0SNicolas Schichan 	if (rm != ARM_R0)
47619fc99d0SNicolas Schichan 		emit(ARM_MOV_R(ARM_R0, rm), ctx);
477ddecdfceSMircea Gherzan 
478ddecdfceSMircea Gherzan 	ctx->seen |= SEEN_CALL;
479ddecdfceSMircea Gherzan 	emit_mov_i(ARM_R3, (u32)jit_udiv, ctx);
480ddecdfceSMircea Gherzan 	emit_blx_r(ARM_R3, ctx);
481ddecdfceSMircea Gherzan 
482ddecdfceSMircea Gherzan 	if (rd != ARM_R0)
483ddecdfceSMircea Gherzan 		emit(ARM_MOV_R(rd, ARM_R0), ctx);
484ddecdfceSMircea Gherzan }
485ddecdfceSMircea Gherzan 
486ddecdfceSMircea Gherzan static inline void update_on_xread(struct jit_ctx *ctx)
487ddecdfceSMircea Gherzan {
488ddecdfceSMircea Gherzan 	if (!(ctx->seen & SEEN_X))
489ddecdfceSMircea Gherzan 		ctx->flags |= FLAG_NEED_X_RESET;
490ddecdfceSMircea Gherzan 
491ddecdfceSMircea Gherzan 	ctx->seen |= SEEN_X;
492ddecdfceSMircea Gherzan }
493ddecdfceSMircea Gherzan 
494ddecdfceSMircea Gherzan static int build_body(struct jit_ctx *ctx)
495ddecdfceSMircea Gherzan {
496ddecdfceSMircea Gherzan 	void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
4977ae457c1SAlexei Starovoitov 	const struct bpf_prog *prog = ctx->skf;
498ddecdfceSMircea Gherzan 	const struct sock_filter *inst;
499ddecdfceSMircea Gherzan 	unsigned i, load_order, off, condt;
500ddecdfceSMircea Gherzan 	int imm12;
501ddecdfceSMircea Gherzan 	u32 k;
502ddecdfceSMircea Gherzan 
503ddecdfceSMircea Gherzan 	for (i = 0; i < prog->len; i++) {
50434805931SDaniel Borkmann 		u16 code;
50534805931SDaniel Borkmann 
506ddecdfceSMircea Gherzan 		inst = &(prog->insns[i]);
507ddecdfceSMircea Gherzan 		/* K as an immediate value operand */
508ddecdfceSMircea Gherzan 		k = inst->k;
50934805931SDaniel Borkmann 		code = bpf_anc_helper(inst);
510ddecdfceSMircea Gherzan 
511ddecdfceSMircea Gherzan 		/* compute offsets only in the fake pass */
512ddecdfceSMircea Gherzan 		if (ctx->target == NULL)
513ddecdfceSMircea Gherzan 			ctx->offsets[i] = ctx->idx * 4;
514ddecdfceSMircea Gherzan 
51534805931SDaniel Borkmann 		switch (code) {
51634805931SDaniel Borkmann 		case BPF_LD | BPF_IMM:
517ddecdfceSMircea Gherzan 			emit_mov_i(r_A, k, ctx);
518ddecdfceSMircea Gherzan 			break;
51934805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_LEN:
520ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
521ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
522ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_skb,
523ddecdfceSMircea Gherzan 				       offsetof(struct sk_buff, len)), ctx);
524ddecdfceSMircea Gherzan 			break;
52534805931SDaniel Borkmann 		case BPF_LD | BPF_MEM:
526ddecdfceSMircea Gherzan 			/* A = scratch[k] */
527ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_MEM_WORD(k);
528ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
529ddecdfceSMircea Gherzan 			break;
53034805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_ABS:
531ddecdfceSMircea Gherzan 			load_order = 2;
532ddecdfceSMircea Gherzan 			goto load;
53334805931SDaniel Borkmann 		case BPF_LD | BPF_H | BPF_ABS:
534ddecdfceSMircea Gherzan 			load_order = 1;
535ddecdfceSMircea Gherzan 			goto load;
53634805931SDaniel Borkmann 		case BPF_LD | BPF_B | BPF_ABS:
537ddecdfceSMircea Gherzan 			load_order = 0;
538ddecdfceSMircea Gherzan load:
539ddecdfceSMircea Gherzan 			/* the interpreter will deal with the negative K */
540ddecdfceSMircea Gherzan 			if ((int)k < 0)
541ddecdfceSMircea Gherzan 				return -ENOTSUPP;
542ddecdfceSMircea Gherzan 			emit_mov_i(r_off, k, ctx);
543ddecdfceSMircea Gherzan load_common:
544ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_DATA | SEEN_CALL;
545ddecdfceSMircea Gherzan 
546ddecdfceSMircea Gherzan 			if (load_order > 0) {
547ddecdfceSMircea Gherzan 				emit(ARM_SUB_I(r_scratch, r_skb_hl,
548ddecdfceSMircea Gherzan 					       1 << load_order), ctx);
549ddecdfceSMircea Gherzan 				emit(ARM_CMP_R(r_scratch, r_off), ctx);
550ddecdfceSMircea Gherzan 				condt = ARM_COND_HS;
551ddecdfceSMircea Gherzan 			} else {
552ddecdfceSMircea Gherzan 				emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
553ddecdfceSMircea Gherzan 				condt = ARM_COND_HI;
554ddecdfceSMircea Gherzan 			}
555ddecdfceSMircea Gherzan 
556ddecdfceSMircea Gherzan 			_emit(condt, ARM_ADD_R(r_scratch, r_off, r_skb_data),
557ddecdfceSMircea Gherzan 			      ctx);
558ddecdfceSMircea Gherzan 
559ddecdfceSMircea Gherzan 			if (load_order == 0)
560ddecdfceSMircea Gherzan 				_emit(condt, ARM_LDRB_I(r_A, r_scratch, 0),
561ddecdfceSMircea Gherzan 				      ctx);
562ddecdfceSMircea Gherzan 			else if (load_order == 1)
563ddecdfceSMircea Gherzan 				emit_load_be16(condt, r_A, r_scratch, ctx);
564ddecdfceSMircea Gherzan 			else if (load_order == 2)
565ddecdfceSMircea Gherzan 				emit_load_be32(condt, r_A, r_scratch, ctx);
566ddecdfceSMircea Gherzan 
567ddecdfceSMircea Gherzan 			_emit(condt, ARM_B(b_imm(i + 1, ctx)), ctx);
568ddecdfceSMircea Gherzan 
569ddecdfceSMircea Gherzan 			/* the slowpath */
570ddecdfceSMircea Gherzan 			emit_mov_i(ARM_R3, (u32)load_func[load_order], ctx);
571ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
572ddecdfceSMircea Gherzan 			/* the offset is already in R1 */
573ddecdfceSMircea Gherzan 			emit_blx_r(ARM_R3, ctx);
574ddecdfceSMircea Gherzan 			/* check the result of skb_copy_bits */
575ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(ARM_R1, 0), ctx);
576ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_NE, ctx);
577ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(r_A, ARM_R0), ctx);
578ddecdfceSMircea Gherzan 			break;
57934805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_IND:
580ddecdfceSMircea Gherzan 			load_order = 2;
581ddecdfceSMircea Gherzan 			goto load_ind;
58234805931SDaniel Borkmann 		case BPF_LD | BPF_H | BPF_IND:
583ddecdfceSMircea Gherzan 			load_order = 1;
584ddecdfceSMircea Gherzan 			goto load_ind;
58534805931SDaniel Borkmann 		case BPF_LD | BPF_B | BPF_IND:
586ddecdfceSMircea Gherzan 			load_order = 0;
587ddecdfceSMircea Gherzan load_ind:
588ddecdfceSMircea Gherzan 			OP_IMM3(ARM_ADD, r_off, r_X, k, ctx);
589ddecdfceSMircea Gherzan 			goto load_common;
59034805931SDaniel Borkmann 		case BPF_LDX | BPF_IMM:
591ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X;
592ddecdfceSMircea Gherzan 			emit_mov_i(r_X, k, ctx);
593ddecdfceSMircea Gherzan 			break;
59434805931SDaniel Borkmann 		case BPF_LDX | BPF_W | BPF_LEN:
595ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X | SEEN_SKB;
596ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_X, r_skb,
597ddecdfceSMircea Gherzan 				       offsetof(struct sk_buff, len)), ctx);
598ddecdfceSMircea Gherzan 			break;
59934805931SDaniel Borkmann 		case BPF_LDX | BPF_MEM:
600ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X | SEEN_MEM_WORD(k);
601ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
602ddecdfceSMircea Gherzan 			break;
60334805931SDaniel Borkmann 		case BPF_LDX | BPF_B | BPF_MSH:
604ddecdfceSMircea Gherzan 			/* x = ((*(frame + k)) & 0xf) << 2; */
605ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X | SEEN_DATA | SEEN_CALL;
606ddecdfceSMircea Gherzan 			/* the interpreter should deal with the negative K */
60745549a68SChen Gang 			if ((int)k < 0)
608ddecdfceSMircea Gherzan 				return -1;
609ddecdfceSMircea Gherzan 			/* offset in r1: we might have to take the slow path */
610ddecdfceSMircea Gherzan 			emit_mov_i(r_off, k, ctx);
611ddecdfceSMircea Gherzan 			emit(ARM_CMP_R(r_skb_hl, r_off), ctx);
612ddecdfceSMircea Gherzan 
613ddecdfceSMircea Gherzan 			/* load in r0: common with the slowpath */
614ddecdfceSMircea Gherzan 			_emit(ARM_COND_HI, ARM_LDRB_R(ARM_R0, r_skb_data,
615ddecdfceSMircea Gherzan 						      ARM_R1), ctx);
616ddecdfceSMircea Gherzan 			/*
617ddecdfceSMircea Gherzan 			 * emit_mov_i() might generate one or two instructions,
618ddecdfceSMircea Gherzan 			 * the same holds for emit_blx_r()
619ddecdfceSMircea Gherzan 			 */
620ddecdfceSMircea Gherzan 			_emit(ARM_COND_HI, ARM_B(b_imm(i + 1, ctx) - 2), ctx);
621ddecdfceSMircea Gherzan 
622ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(ARM_R0, r_skb), ctx);
623ddecdfceSMircea Gherzan 			/* r_off is r1 */
624ddecdfceSMircea Gherzan 			emit_mov_i(ARM_R3, (u32)jit_get_skb_b, ctx);
625ddecdfceSMircea Gherzan 			emit_blx_r(ARM_R3, ctx);
626ddecdfceSMircea Gherzan 			/* check the return value of skb_copy_bits */
627ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(ARM_R1, 0), ctx);
628ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_NE, ctx);
629ddecdfceSMircea Gherzan 
630ddecdfceSMircea Gherzan 			emit(ARM_AND_I(r_X, ARM_R0, 0x00f), ctx);
631ddecdfceSMircea Gherzan 			emit(ARM_LSL_I(r_X, r_X, 2), ctx);
632ddecdfceSMircea Gherzan 			break;
63334805931SDaniel Borkmann 		case BPF_ST:
634ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_MEM_WORD(k);
635ddecdfceSMircea Gherzan 			emit(ARM_STR_I(r_A, ARM_SP, SCRATCH_OFF(k)), ctx);
636ddecdfceSMircea Gherzan 			break;
63734805931SDaniel Borkmann 		case BPF_STX:
638ddecdfceSMircea Gherzan 			update_on_xread(ctx);
639ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_MEM_WORD(k);
640ddecdfceSMircea Gherzan 			emit(ARM_STR_I(r_X, ARM_SP, SCRATCH_OFF(k)), ctx);
641ddecdfceSMircea Gherzan 			break;
64234805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_K:
643ddecdfceSMircea Gherzan 			/* A += K */
644ddecdfceSMircea Gherzan 			OP_IMM3(ARM_ADD, r_A, r_A, k, ctx);
645ddecdfceSMircea Gherzan 			break;
64634805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_X:
647ddecdfceSMircea Gherzan 			update_on_xread(ctx);
648ddecdfceSMircea Gherzan 			emit(ARM_ADD_R(r_A, r_A, r_X), ctx);
649ddecdfceSMircea Gherzan 			break;
65034805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_K:
651ddecdfceSMircea Gherzan 			/* A -= K */
652ddecdfceSMircea Gherzan 			OP_IMM3(ARM_SUB, r_A, r_A, k, ctx);
653ddecdfceSMircea Gherzan 			break;
65434805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_X:
655ddecdfceSMircea Gherzan 			update_on_xread(ctx);
656ddecdfceSMircea Gherzan 			emit(ARM_SUB_R(r_A, r_A, r_X), ctx);
657ddecdfceSMircea Gherzan 			break;
65834805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_K:
659ddecdfceSMircea Gherzan 			/* A *= K */
660ddecdfceSMircea Gherzan 			emit_mov_i(r_scratch, k, ctx);
661ddecdfceSMircea Gherzan 			emit(ARM_MUL(r_A, r_A, r_scratch), ctx);
662ddecdfceSMircea Gherzan 			break;
66334805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_X:
664ddecdfceSMircea Gherzan 			update_on_xread(ctx);
665ddecdfceSMircea Gherzan 			emit(ARM_MUL(r_A, r_A, r_X), ctx);
666ddecdfceSMircea Gherzan 			break;
66734805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_K:
668aee636c4SEric Dumazet 			if (k == 1)
669aee636c4SEric Dumazet 				break;
670ddecdfceSMircea Gherzan 			emit_mov_i(r_scratch, k, ctx);
671aee636c4SEric Dumazet 			emit_udiv(r_A, r_A, r_scratch, ctx);
672ddecdfceSMircea Gherzan 			break;
67334805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_X:
674ddecdfceSMircea Gherzan 			update_on_xread(ctx);
675ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(r_X, 0), ctx);
676ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_EQ, ctx);
677ddecdfceSMircea Gherzan 			emit_udiv(r_A, r_A, r_X, ctx);
678ddecdfceSMircea Gherzan 			break;
67934805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_K:
680ddecdfceSMircea Gherzan 			/* A |= K */
681ddecdfceSMircea Gherzan 			OP_IMM3(ARM_ORR, r_A, r_A, k, ctx);
682ddecdfceSMircea Gherzan 			break;
68334805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_X:
684ddecdfceSMircea Gherzan 			update_on_xread(ctx);
685ddecdfceSMircea Gherzan 			emit(ARM_ORR_R(r_A, r_A, r_X), ctx);
686ddecdfceSMircea Gherzan 			break;
68734805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_K:
6883cbe2041SDaniel Borkmann 			/* A ^= K; */
6893cbe2041SDaniel Borkmann 			OP_IMM3(ARM_EOR, r_A, r_A, k, ctx);
6903cbe2041SDaniel Borkmann 			break;
69134805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_ALU_XOR_X:
69234805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_X:
6933cbe2041SDaniel Borkmann 			/* A ^= X */
6943cbe2041SDaniel Borkmann 			update_on_xread(ctx);
6953cbe2041SDaniel Borkmann 			emit(ARM_EOR_R(r_A, r_A, r_X), ctx);
6963cbe2041SDaniel Borkmann 			break;
69734805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_K:
698ddecdfceSMircea Gherzan 			/* A &= K */
699ddecdfceSMircea Gherzan 			OP_IMM3(ARM_AND, r_A, r_A, k, ctx);
700ddecdfceSMircea Gherzan 			break;
70134805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_X:
702ddecdfceSMircea Gherzan 			update_on_xread(ctx);
703ddecdfceSMircea Gherzan 			emit(ARM_AND_R(r_A, r_A, r_X), ctx);
704ddecdfceSMircea Gherzan 			break;
70534805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_K:
706ddecdfceSMircea Gherzan 			if (unlikely(k > 31))
707ddecdfceSMircea Gherzan 				return -1;
708ddecdfceSMircea Gherzan 			emit(ARM_LSL_I(r_A, r_A, k), ctx);
709ddecdfceSMircea Gherzan 			break;
71034805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_X:
711ddecdfceSMircea Gherzan 			update_on_xread(ctx);
712ddecdfceSMircea Gherzan 			emit(ARM_LSL_R(r_A, r_A, r_X), ctx);
713ddecdfceSMircea Gherzan 			break;
71434805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_K:
715ddecdfceSMircea Gherzan 			if (unlikely(k > 31))
716ddecdfceSMircea Gherzan 				return -1;
717ddecdfceSMircea Gherzan 			emit(ARM_LSR_I(r_A, r_A, k), ctx);
718ddecdfceSMircea Gherzan 			break;
71934805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_X:
720ddecdfceSMircea Gherzan 			update_on_xread(ctx);
721ddecdfceSMircea Gherzan 			emit(ARM_LSR_R(r_A, r_A, r_X), ctx);
722ddecdfceSMircea Gherzan 			break;
72334805931SDaniel Borkmann 		case BPF_ALU | BPF_NEG:
724ddecdfceSMircea Gherzan 			/* A = -A */
725ddecdfceSMircea Gherzan 			emit(ARM_RSB_I(r_A, r_A, 0), ctx);
726ddecdfceSMircea Gherzan 			break;
72734805931SDaniel Borkmann 		case BPF_JMP | BPF_JA:
728ddecdfceSMircea Gherzan 			/* pc += K */
729ddecdfceSMircea Gherzan 			emit(ARM_B(b_imm(i + k + 1, ctx)), ctx);
730ddecdfceSMircea Gherzan 			break;
73134805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_K:
732ddecdfceSMircea Gherzan 			/* pc += (A == K) ? pc->jt : pc->jf */
733ddecdfceSMircea Gherzan 			condt  = ARM_COND_EQ;
734ddecdfceSMircea Gherzan 			goto cmp_imm;
73534805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_K:
736ddecdfceSMircea Gherzan 			/* pc += (A > K) ? pc->jt : pc->jf */
737ddecdfceSMircea Gherzan 			condt  = ARM_COND_HI;
738ddecdfceSMircea Gherzan 			goto cmp_imm;
73934805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_K:
740ddecdfceSMircea Gherzan 			/* pc += (A >= K) ? pc->jt : pc->jf */
741ddecdfceSMircea Gherzan 			condt  = ARM_COND_HS;
742ddecdfceSMircea Gherzan cmp_imm:
743ddecdfceSMircea Gherzan 			imm12 = imm8m(k);
744ddecdfceSMircea Gherzan 			if (imm12 < 0) {
745ddecdfceSMircea Gherzan 				emit_mov_i_no8m(r_scratch, k, ctx);
746ddecdfceSMircea Gherzan 				emit(ARM_CMP_R(r_A, r_scratch), ctx);
747ddecdfceSMircea Gherzan 			} else {
748ddecdfceSMircea Gherzan 				emit(ARM_CMP_I(r_A, imm12), ctx);
749ddecdfceSMircea Gherzan 			}
750ddecdfceSMircea Gherzan cond_jump:
751ddecdfceSMircea Gherzan 			if (inst->jt)
752ddecdfceSMircea Gherzan 				_emit(condt, ARM_B(b_imm(i + inst->jt + 1,
753ddecdfceSMircea Gherzan 						   ctx)), ctx);
754ddecdfceSMircea Gherzan 			if (inst->jf)
755ddecdfceSMircea Gherzan 				_emit(condt ^ 1, ARM_B(b_imm(i + inst->jf + 1,
756ddecdfceSMircea Gherzan 							     ctx)), ctx);
757ddecdfceSMircea Gherzan 			break;
75834805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_X:
759ddecdfceSMircea Gherzan 			/* pc += (A == X) ? pc->jt : pc->jf */
760ddecdfceSMircea Gherzan 			condt   = ARM_COND_EQ;
761ddecdfceSMircea Gherzan 			goto cmp_x;
76234805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_X:
763ddecdfceSMircea Gherzan 			/* pc += (A > X) ? pc->jt : pc->jf */
764ddecdfceSMircea Gherzan 			condt   = ARM_COND_HI;
765ddecdfceSMircea Gherzan 			goto cmp_x;
76634805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_X:
767ddecdfceSMircea Gherzan 			/* pc += (A >= X) ? pc->jt : pc->jf */
768ddecdfceSMircea Gherzan 			condt   = ARM_COND_CS;
769ddecdfceSMircea Gherzan cmp_x:
770ddecdfceSMircea Gherzan 			update_on_xread(ctx);
771ddecdfceSMircea Gherzan 			emit(ARM_CMP_R(r_A, r_X), ctx);
772ddecdfceSMircea Gherzan 			goto cond_jump;
77334805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_K:
774ddecdfceSMircea Gherzan 			/* pc += (A & K) ? pc->jt : pc->jf */
775ddecdfceSMircea Gherzan 			condt  = ARM_COND_NE;
776ddecdfceSMircea Gherzan 			/* not set iff all zeroes iff Z==1 iff EQ */
777ddecdfceSMircea Gherzan 
778ddecdfceSMircea Gherzan 			imm12 = imm8m(k);
779ddecdfceSMircea Gherzan 			if (imm12 < 0) {
780ddecdfceSMircea Gherzan 				emit_mov_i_no8m(r_scratch, k, ctx);
781ddecdfceSMircea Gherzan 				emit(ARM_TST_R(r_A, r_scratch), ctx);
782ddecdfceSMircea Gherzan 			} else {
783ddecdfceSMircea Gherzan 				emit(ARM_TST_I(r_A, imm12), ctx);
784ddecdfceSMircea Gherzan 			}
785ddecdfceSMircea Gherzan 			goto cond_jump;
78634805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_X:
787ddecdfceSMircea Gherzan 			/* pc += (A & X) ? pc->jt : pc->jf */
788ddecdfceSMircea Gherzan 			update_on_xread(ctx);
789ddecdfceSMircea Gherzan 			condt  = ARM_COND_NE;
790ddecdfceSMircea Gherzan 			emit(ARM_TST_R(r_A, r_X), ctx);
791ddecdfceSMircea Gherzan 			goto cond_jump;
79234805931SDaniel Borkmann 		case BPF_RET | BPF_A:
793ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(ARM_R0, r_A), ctx);
794ddecdfceSMircea Gherzan 			goto b_epilogue;
79534805931SDaniel Borkmann 		case BPF_RET | BPF_K:
796ddecdfceSMircea Gherzan 			if ((k == 0) && (ctx->ret0_fp_idx < 0))
797ddecdfceSMircea Gherzan 				ctx->ret0_fp_idx = i;
798ddecdfceSMircea Gherzan 			emit_mov_i(ARM_R0, k, ctx);
799ddecdfceSMircea Gherzan b_epilogue:
800ddecdfceSMircea Gherzan 			if (i != ctx->skf->len - 1)
801ddecdfceSMircea Gherzan 				emit(ARM_B(b_imm(prog->len, ctx)), ctx);
802ddecdfceSMircea Gherzan 			break;
80334805931SDaniel Borkmann 		case BPF_MISC | BPF_TAX:
804ddecdfceSMircea Gherzan 			/* X = A */
805ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_X;
806ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(r_X, r_A), ctx);
807ddecdfceSMircea Gherzan 			break;
80834805931SDaniel Borkmann 		case BPF_MISC | BPF_TXA:
809ddecdfceSMircea Gherzan 			/* A = X */
810ddecdfceSMircea Gherzan 			update_on_xread(ctx);
811ddecdfceSMircea Gherzan 			emit(ARM_MOV_R(r_A, r_X), ctx);
812ddecdfceSMircea Gherzan 			break;
81334805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_PROTOCOL:
814ddecdfceSMircea Gherzan 			/* A = ntohs(skb->protocol) */
815ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
816ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
817ddecdfceSMircea Gherzan 						  protocol) != 2);
818ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, protocol);
819ddecdfceSMircea Gherzan 			emit(ARM_LDRH_I(r_scratch, r_skb, off), ctx);
820ddecdfceSMircea Gherzan 			emit_swap16(r_A, r_scratch, ctx);
821ddecdfceSMircea Gherzan 			break;
82234805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_CPU:
823ddecdfceSMircea Gherzan 			/* r_scratch = current_thread_info() */
824ddecdfceSMircea Gherzan 			OP_IMM3(ARM_BIC, r_scratch, ARM_SP, THREAD_SIZE - 1, ctx);
825ddecdfceSMircea Gherzan 			/* A = current_thread_info()->cpu */
826ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct thread_info, cpu) != 4);
827ddecdfceSMircea Gherzan 			off = offsetof(struct thread_info, cpu);
828ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
829ddecdfceSMircea Gherzan 			break;
83034805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_IFINDEX:
831ddecdfceSMircea Gherzan 			/* A = skb->dev->ifindex */
832ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
833ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, dev);
834ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_scratch, r_skb, off), ctx);
835ddecdfceSMircea Gherzan 
836ddecdfceSMircea Gherzan 			emit(ARM_CMP_I(r_scratch, 0), ctx);
837ddecdfceSMircea Gherzan 			emit_err_ret(ARM_COND_EQ, ctx);
838ddecdfceSMircea Gherzan 
839ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct net_device,
840ddecdfceSMircea Gherzan 						  ifindex) != 4);
841ddecdfceSMircea Gherzan 			off = offsetof(struct net_device, ifindex);
842ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_scratch, off), ctx);
843ddecdfceSMircea Gherzan 			break;
84434805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_MARK:
845ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
846ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
847ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, mark);
848ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_skb, off), ctx);
849ddecdfceSMircea Gherzan 			break;
85034805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_RXHASH:
851ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
85261b905daSTom Herbert 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
85361b905daSTom Herbert 			off = offsetof(struct sk_buff, hash);
854ddecdfceSMircea Gherzan 			emit(ARM_LDR_I(r_A, r_skb, off), ctx);
855ddecdfceSMircea Gherzan 			break;
85634805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_VLAN_TAG:
85734805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_VLAN_TAG_PRESENT:
858bf0098f2SDaniel Borkmann 			ctx->seen |= SEEN_SKB;
859bf0098f2SDaniel Borkmann 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
860bf0098f2SDaniel Borkmann 			off = offsetof(struct sk_buff, vlan_tci);
861bf0098f2SDaniel Borkmann 			emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
86234805931SDaniel Borkmann 			if (code == (BPF_ANC | SKF_AD_VLAN_TAG))
863bf0098f2SDaniel Borkmann 				OP_IMM3(ARM_AND, r_A, r_A, VLAN_VID_MASK, ctx);
864bf0098f2SDaniel Borkmann 			else
865bf0098f2SDaniel Borkmann 				OP_IMM3(ARM_AND, r_A, r_A, VLAN_TAG_PRESENT, ctx);
866bf0098f2SDaniel Borkmann 			break;
86734805931SDaniel Borkmann 		case BPF_ANC | SKF_AD_QUEUE:
868ddecdfceSMircea Gherzan 			ctx->seen |= SEEN_SKB;
869ddecdfceSMircea Gherzan 			BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff,
870ddecdfceSMircea Gherzan 						  queue_mapping) != 2);
871ddecdfceSMircea Gherzan 			BUILD_BUG_ON(offsetof(struct sk_buff,
872ddecdfceSMircea Gherzan 					      queue_mapping) > 0xff);
873ddecdfceSMircea Gherzan 			off = offsetof(struct sk_buff, queue_mapping);
874ddecdfceSMircea Gherzan 			emit(ARM_LDRH_I(r_A, r_skb, off), ctx);
875ddecdfceSMircea Gherzan 			break;
876ddecdfceSMircea Gherzan 		default:
877ddecdfceSMircea Gherzan 			return -1;
878ddecdfceSMircea Gherzan 		}
879*0b59d880SNicolas Schichan 
880*0b59d880SNicolas Schichan 		if (ctx->flags & FLAG_IMM_OVERFLOW)
881*0b59d880SNicolas Schichan 			/*
882*0b59d880SNicolas Schichan 			 * this instruction generated an overflow when
883*0b59d880SNicolas Schichan 			 * trying to access the literal pool, so
884*0b59d880SNicolas Schichan 			 * delegate this filter to the kernel interpreter.
885*0b59d880SNicolas Schichan 			 */
886*0b59d880SNicolas Schichan 			return -1;
887ddecdfceSMircea Gherzan 	}
888ddecdfceSMircea Gherzan 
889ddecdfceSMircea Gherzan 	/* compute offsets only during the first pass */
890ddecdfceSMircea Gherzan 	if (ctx->target == NULL)
891ddecdfceSMircea Gherzan 		ctx->offsets[i] = ctx->idx * 4;
892ddecdfceSMircea Gherzan 
893ddecdfceSMircea Gherzan 	return 0;
894ddecdfceSMircea Gherzan }
895ddecdfceSMircea Gherzan 
896ddecdfceSMircea Gherzan 
8977ae457c1SAlexei Starovoitov void bpf_jit_compile(struct bpf_prog *fp)
898ddecdfceSMircea Gherzan {
89955309dd3SDaniel Borkmann 	struct bpf_binary_header *header;
900ddecdfceSMircea Gherzan 	struct jit_ctx ctx;
901ddecdfceSMircea Gherzan 	unsigned tmp_idx;
902ddecdfceSMircea Gherzan 	unsigned alloc_size;
90355309dd3SDaniel Borkmann 	u8 *target_ptr;
904ddecdfceSMircea Gherzan 
905ddecdfceSMircea Gherzan 	if (!bpf_jit_enable)
906ddecdfceSMircea Gherzan 		return;
907ddecdfceSMircea Gherzan 
908ddecdfceSMircea Gherzan 	memset(&ctx, 0, sizeof(ctx));
909ddecdfceSMircea Gherzan 	ctx.skf		= fp;
910ddecdfceSMircea Gherzan 	ctx.ret0_fp_idx = -1;
911ddecdfceSMircea Gherzan 
91289c2e009SSchichan Nicolas 	ctx.offsets = kzalloc(4 * (ctx.skf->len + 1), GFP_KERNEL);
913ddecdfceSMircea Gherzan 	if (ctx.offsets == NULL)
914ddecdfceSMircea Gherzan 		return;
915ddecdfceSMircea Gherzan 
916ddecdfceSMircea Gherzan 	/* fake pass to fill in the ctx->seen */
917ddecdfceSMircea Gherzan 	if (unlikely(build_body(&ctx)))
918ddecdfceSMircea Gherzan 		goto out;
919ddecdfceSMircea Gherzan 
920ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
921ddecdfceSMircea Gherzan 	build_prologue(&ctx);
922ddecdfceSMircea Gherzan 	ctx.prologue_bytes = (ctx.idx - tmp_idx) * 4;
923ddecdfceSMircea Gherzan 
924ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
925ddecdfceSMircea Gherzan 	tmp_idx = ctx.idx;
926ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
927ddecdfceSMircea Gherzan 	ctx.epilogue_bytes = (ctx.idx - tmp_idx) * 4;
928ddecdfceSMircea Gherzan 
929ddecdfceSMircea Gherzan 	ctx.idx += ctx.imm_count;
930ddecdfceSMircea Gherzan 	if (ctx.imm_count) {
93189c2e009SSchichan Nicolas 		ctx.imms = kzalloc(4 * ctx.imm_count, GFP_KERNEL);
932ddecdfceSMircea Gherzan 		if (ctx.imms == NULL)
933ddecdfceSMircea Gherzan 			goto out;
934ddecdfceSMircea Gherzan 	}
935ddecdfceSMircea Gherzan #else
936ddecdfceSMircea Gherzan 	/* there's nothing after the epilogue on ARMv7 */
937ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
938ddecdfceSMircea Gherzan #endif
939ddecdfceSMircea Gherzan 	alloc_size = 4 * ctx.idx;
94055309dd3SDaniel Borkmann 	header = bpf_jit_binary_alloc(alloc_size, &target_ptr,
94155309dd3SDaniel Borkmann 				      4, jit_fill_hole);
94255309dd3SDaniel Borkmann 	if (header == NULL)
943ddecdfceSMircea Gherzan 		goto out;
944ddecdfceSMircea Gherzan 
94555309dd3SDaniel Borkmann 	ctx.target = (u32 *) target_ptr;
946ddecdfceSMircea Gherzan 	ctx.idx = 0;
94755309dd3SDaniel Borkmann 
948ddecdfceSMircea Gherzan 	build_prologue(&ctx);
949*0b59d880SNicolas Schichan 	if (build_body(&ctx) < 0) {
950*0b59d880SNicolas Schichan #if __LINUX_ARM_ARCH__ < 7
951*0b59d880SNicolas Schichan 		if (ctx.imm_count)
952*0b59d880SNicolas Schichan 			kfree(ctx.imms);
953*0b59d880SNicolas Schichan #endif
954*0b59d880SNicolas Schichan 		bpf_jit_binary_free(header);
955*0b59d880SNicolas Schichan 		goto out;
956*0b59d880SNicolas Schichan 	}
957ddecdfceSMircea Gherzan 	build_epilogue(&ctx);
958ddecdfceSMircea Gherzan 
959ddecdfceSMircea Gherzan 	flush_icache_range((u32)ctx.target, (u32)(ctx.target + ctx.idx));
960ddecdfceSMircea Gherzan 
961ddecdfceSMircea Gherzan #if __LINUX_ARM_ARCH__ < 7
962ddecdfceSMircea Gherzan 	if (ctx.imm_count)
963ddecdfceSMircea Gherzan 		kfree(ctx.imms);
964ddecdfceSMircea Gherzan #endif
965ddecdfceSMircea Gherzan 
966ddecdfceSMircea Gherzan 	if (bpf_jit_enable > 1)
96779617801SDaniel Borkmann 		/* there are 2 passes here */
96879617801SDaniel Borkmann 		bpf_jit_dump(fp->len, alloc_size, 2, ctx.target);
969ddecdfceSMircea Gherzan 
97055309dd3SDaniel Borkmann 	set_memory_ro((unsigned long)header, header->pages);
971ddecdfceSMircea Gherzan 	fp->bpf_func = (void *)ctx.target;
972286aad3cSDaniel Borkmann 	fp->jited = true;
973ddecdfceSMircea Gherzan out:
974ddecdfceSMircea Gherzan 	kfree(ctx.offsets);
975ddecdfceSMircea Gherzan 	return;
976ddecdfceSMircea Gherzan }
977ddecdfceSMircea Gherzan 
9787ae457c1SAlexei Starovoitov void bpf_jit_free(struct bpf_prog *fp)
979ddecdfceSMircea Gherzan {
98055309dd3SDaniel Borkmann 	unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
98155309dd3SDaniel Borkmann 	struct bpf_binary_header *header = (void *)addr;
98260a3b225SDaniel Borkmann 
98355309dd3SDaniel Borkmann 	if (!fp->jited)
98455309dd3SDaniel Borkmann 		goto free_filter;
98555309dd3SDaniel Borkmann 
98655309dd3SDaniel Borkmann 	set_memory_rw(addr, header->pages);
98755309dd3SDaniel Borkmann 	bpf_jit_binary_free(header);
98855309dd3SDaniel Borkmann 
98955309dd3SDaniel Borkmann free_filter:
99060a3b225SDaniel Borkmann 	bpf_prog_unlock_free(fp);
991ddecdfceSMircea Gherzan }
992