xref: /linux/arch/arm64/net/bpf_jit_comp.c (revision b569c1c622c5e60c960a6ae5bd0880e0cdbd56b1)
1e54bcde3SZi Shen Lim /*
2e54bcde3SZi Shen Lim  * BPF JIT compiler for ARM64
3e54bcde3SZi Shen Lim  *
4e54bcde3SZi Shen Lim  * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com>
5e54bcde3SZi Shen Lim  *
6e54bcde3SZi Shen Lim  * This program is free software; you can redistribute it and/or modify
7e54bcde3SZi Shen Lim  * it under the terms of the GNU General Public License version 2 as
8e54bcde3SZi Shen Lim  * published by the Free Software Foundation.
9e54bcde3SZi Shen Lim  *
10e54bcde3SZi Shen Lim  * This program is distributed in the hope that it will be useful,
11e54bcde3SZi Shen Lim  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e54bcde3SZi Shen Lim  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13e54bcde3SZi Shen Lim  * GNU General Public License for more details.
14e54bcde3SZi Shen Lim  *
15e54bcde3SZi Shen Lim  * You should have received a copy of the GNU General Public License
16e54bcde3SZi Shen Lim  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17e54bcde3SZi Shen Lim  */
18e54bcde3SZi Shen Lim 
19e54bcde3SZi Shen Lim #define pr_fmt(fmt) "bpf_jit: " fmt
20e54bcde3SZi Shen Lim 
21e54bcde3SZi Shen Lim #include <linux/filter.h>
22e54bcde3SZi Shen Lim #include <linux/printk.h>
23e54bcde3SZi Shen Lim #include <linux/skbuff.h>
24e54bcde3SZi Shen Lim #include <linux/slab.h>
25*b569c1c6SDaniel Borkmann 
26e54bcde3SZi Shen Lim #include <asm/byteorder.h>
27e54bcde3SZi Shen Lim #include <asm/cacheflush.h>
28*b569c1c6SDaniel Borkmann #include <asm/debug-monitors.h>
29e54bcde3SZi Shen Lim 
30e54bcde3SZi Shen Lim #include "bpf_jit.h"
31e54bcde3SZi Shen Lim 
32e54bcde3SZi Shen Lim int bpf_jit_enable __read_mostly;
33e54bcde3SZi Shen Lim 
34e54bcde3SZi Shen Lim #define TMP_REG_1 (MAX_BPF_REG + 0)
35e54bcde3SZi Shen Lim #define TMP_REG_2 (MAX_BPF_REG + 1)
36e54bcde3SZi Shen Lim 
37e54bcde3SZi Shen Lim /* Map BPF registers to A64 registers */
38e54bcde3SZi Shen Lim static const int bpf2a64[] = {
39e54bcde3SZi Shen Lim 	/* return value from in-kernel function, and exit value from eBPF */
40e54bcde3SZi Shen Lim 	[BPF_REG_0] = A64_R(7),
41e54bcde3SZi Shen Lim 	/* arguments from eBPF program to in-kernel function */
42e54bcde3SZi Shen Lim 	[BPF_REG_1] = A64_R(0),
43e54bcde3SZi Shen Lim 	[BPF_REG_2] = A64_R(1),
44e54bcde3SZi Shen Lim 	[BPF_REG_3] = A64_R(2),
45e54bcde3SZi Shen Lim 	[BPF_REG_4] = A64_R(3),
46e54bcde3SZi Shen Lim 	[BPF_REG_5] = A64_R(4),
47e54bcde3SZi Shen Lim 	/* callee saved registers that in-kernel function will preserve */
48e54bcde3SZi Shen Lim 	[BPF_REG_6] = A64_R(19),
49e54bcde3SZi Shen Lim 	[BPF_REG_7] = A64_R(20),
50e54bcde3SZi Shen Lim 	[BPF_REG_8] = A64_R(21),
51e54bcde3SZi Shen Lim 	[BPF_REG_9] = A64_R(22),
52e54bcde3SZi Shen Lim 	/* read-only frame pointer to access stack */
53e54bcde3SZi Shen Lim 	[BPF_REG_FP] = A64_FP,
54e54bcde3SZi Shen Lim 	/* temporary register for internal BPF JIT */
55e54bcde3SZi Shen Lim 	[TMP_REG_1] = A64_R(23),
56e54bcde3SZi Shen Lim 	[TMP_REG_2] = A64_R(24),
57e54bcde3SZi Shen Lim };
58e54bcde3SZi Shen Lim 
59e54bcde3SZi Shen Lim struct jit_ctx {
60e54bcde3SZi Shen Lim 	const struct bpf_prog *prog;
61e54bcde3SZi Shen Lim 	int idx;
62e54bcde3SZi Shen Lim 	int tmp_used;
63e54bcde3SZi Shen Lim 	int body_offset;
64e54bcde3SZi Shen Lim 	int *offset;
65e54bcde3SZi Shen Lim 	u32 *image;
66e54bcde3SZi Shen Lim };
67e54bcde3SZi Shen Lim 
68e54bcde3SZi Shen Lim static inline void emit(const u32 insn, struct jit_ctx *ctx)
69e54bcde3SZi Shen Lim {
70e54bcde3SZi Shen Lim 	if (ctx->image != NULL)
71e54bcde3SZi Shen Lim 		ctx->image[ctx->idx] = cpu_to_le32(insn);
72e54bcde3SZi Shen Lim 
73e54bcde3SZi Shen Lim 	ctx->idx++;
74e54bcde3SZi Shen Lim }
75e54bcde3SZi Shen Lim 
76e54bcde3SZi Shen Lim static inline void emit_a64_mov_i64(const int reg, const u64 val,
77e54bcde3SZi Shen Lim 				    struct jit_ctx *ctx)
78e54bcde3SZi Shen Lim {
79e54bcde3SZi Shen Lim 	u64 tmp = val;
80e54bcde3SZi Shen Lim 	int shift = 0;
81e54bcde3SZi Shen Lim 
82e54bcde3SZi Shen Lim 	emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx);
83e54bcde3SZi Shen Lim 	tmp >>= 16;
84e54bcde3SZi Shen Lim 	shift += 16;
85e54bcde3SZi Shen Lim 	while (tmp) {
86e54bcde3SZi Shen Lim 		if (tmp & 0xffff)
87e54bcde3SZi Shen Lim 			emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
88e54bcde3SZi Shen Lim 		tmp >>= 16;
89e54bcde3SZi Shen Lim 		shift += 16;
90e54bcde3SZi Shen Lim 	}
91e54bcde3SZi Shen Lim }
92e54bcde3SZi Shen Lim 
93e54bcde3SZi Shen Lim static inline void emit_a64_mov_i(const int is64, const int reg,
94e54bcde3SZi Shen Lim 				  const s32 val, struct jit_ctx *ctx)
95e54bcde3SZi Shen Lim {
96e54bcde3SZi Shen Lim 	u16 hi = val >> 16;
97e54bcde3SZi Shen Lim 	u16 lo = val & 0xffff;
98e54bcde3SZi Shen Lim 
99e54bcde3SZi Shen Lim 	if (hi & 0x8000) {
100e54bcde3SZi Shen Lim 		if (hi == 0xffff) {
101e54bcde3SZi Shen Lim 			emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
102e54bcde3SZi Shen Lim 		} else {
103e54bcde3SZi Shen Lim 			emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
104e54bcde3SZi Shen Lim 			emit(A64_MOVK(is64, reg, lo, 0), ctx);
105e54bcde3SZi Shen Lim 		}
106e54bcde3SZi Shen Lim 	} else {
107e54bcde3SZi Shen Lim 		emit(A64_MOVZ(is64, reg, lo, 0), ctx);
108e54bcde3SZi Shen Lim 		if (hi)
109e54bcde3SZi Shen Lim 			emit(A64_MOVK(is64, reg, hi, 16), ctx);
110e54bcde3SZi Shen Lim 	}
111e54bcde3SZi Shen Lim }
112e54bcde3SZi Shen Lim 
113e54bcde3SZi Shen Lim static inline int bpf2a64_offset(int bpf_to, int bpf_from,
114e54bcde3SZi Shen Lim 				 const struct jit_ctx *ctx)
115e54bcde3SZi Shen Lim {
116e54bcde3SZi Shen Lim 	int to = ctx->offset[bpf_to + 1];
117e54bcde3SZi Shen Lim 	/* -1 to account for the Branch instruction */
118e54bcde3SZi Shen Lim 	int from = ctx->offset[bpf_from + 1] - 1;
119e54bcde3SZi Shen Lim 
120e54bcde3SZi Shen Lim 	return to - from;
121e54bcde3SZi Shen Lim }
122e54bcde3SZi Shen Lim 
123*b569c1c6SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
124*b569c1c6SDaniel Borkmann {
125*b569c1c6SDaniel Borkmann 	u32 *ptr;
126*b569c1c6SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
127*b569c1c6SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
128*b569c1c6SDaniel Borkmann 		*ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
129*b569c1c6SDaniel Borkmann }
130*b569c1c6SDaniel Borkmann 
131e54bcde3SZi Shen Lim static inline int epilogue_offset(const struct jit_ctx *ctx)
132e54bcde3SZi Shen Lim {
133e54bcde3SZi Shen Lim 	int to = ctx->offset[ctx->prog->len - 1];
134e54bcde3SZi Shen Lim 	int from = ctx->idx - ctx->body_offset;
135e54bcde3SZi Shen Lim 
136e54bcde3SZi Shen Lim 	return to - from;
137e54bcde3SZi Shen Lim }
138e54bcde3SZi Shen Lim 
139e54bcde3SZi Shen Lim /* Stack must be multiples of 16B */
140e54bcde3SZi Shen Lim #define STACK_ALIGN(sz) (((sz) + 15) & ~15)
141e54bcde3SZi Shen Lim 
142e54bcde3SZi Shen Lim static void build_prologue(struct jit_ctx *ctx)
143e54bcde3SZi Shen Lim {
144e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
145e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
146e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
147e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
148e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
149e54bcde3SZi Shen Lim 	const u8 ra = bpf2a64[BPF_REG_A];
150e54bcde3SZi Shen Lim 	const u8 rx = bpf2a64[BPF_REG_X];
151e54bcde3SZi Shen Lim 	const u8 tmp1 = bpf2a64[TMP_REG_1];
152e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
153e54bcde3SZi Shen Lim 	int stack_size = MAX_BPF_STACK;
154e54bcde3SZi Shen Lim 
155e54bcde3SZi Shen Lim 	stack_size += 4; /* extra for skb_copy_bits buffer */
156e54bcde3SZi Shen Lim 	stack_size = STACK_ALIGN(stack_size);
157e54bcde3SZi Shen Lim 
158e54bcde3SZi Shen Lim 	/* Save callee-saved register */
159e54bcde3SZi Shen Lim 	emit(A64_PUSH(r6, r7, A64_SP), ctx);
160e54bcde3SZi Shen Lim 	emit(A64_PUSH(r8, r9, A64_SP), ctx);
161e54bcde3SZi Shen Lim 	if (ctx->tmp_used)
162e54bcde3SZi Shen Lim 		emit(A64_PUSH(tmp1, tmp2, A64_SP), ctx);
163e54bcde3SZi Shen Lim 
164e54bcde3SZi Shen Lim 	/* Set up BPF stack */
165e54bcde3SZi Shen Lim 	emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
166e54bcde3SZi Shen Lim 
167e54bcde3SZi Shen Lim 	/* Set up frame pointer */
168e54bcde3SZi Shen Lim 	emit(A64_MOV(1, fp, A64_SP), ctx);
169e54bcde3SZi Shen Lim 
170e54bcde3SZi Shen Lim 	/* Clear registers A and X */
171e54bcde3SZi Shen Lim 	emit_a64_mov_i64(ra, 0, ctx);
172e54bcde3SZi Shen Lim 	emit_a64_mov_i64(rx, 0, ctx);
173e54bcde3SZi Shen Lim }
174e54bcde3SZi Shen Lim 
175e54bcde3SZi Shen Lim static void build_epilogue(struct jit_ctx *ctx)
176e54bcde3SZi Shen Lim {
177e54bcde3SZi Shen Lim 	const u8 r0 = bpf2a64[BPF_REG_0];
178e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
179e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
180e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
181e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
182e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
183e54bcde3SZi Shen Lim 	const u8 tmp1 = bpf2a64[TMP_REG_1];
184e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
185e54bcde3SZi Shen Lim 	int stack_size = MAX_BPF_STACK;
186e54bcde3SZi Shen Lim 
187e54bcde3SZi Shen Lim 	stack_size += 4; /* extra for skb_copy_bits buffer */
188e54bcde3SZi Shen Lim 	stack_size = STACK_ALIGN(stack_size);
189e54bcde3SZi Shen Lim 
190e54bcde3SZi Shen Lim 	/* We're done with BPF stack */
191e54bcde3SZi Shen Lim 	emit(A64_ADD_I(1, A64_SP, A64_SP, stack_size), ctx);
192e54bcde3SZi Shen Lim 
193e54bcde3SZi Shen Lim 	/* Restore callee-saved register */
194e54bcde3SZi Shen Lim 	if (ctx->tmp_used)
195e54bcde3SZi Shen Lim 		emit(A64_POP(tmp1, tmp2, A64_SP), ctx);
196e54bcde3SZi Shen Lim 	emit(A64_POP(r8, r9, A64_SP), ctx);
197e54bcde3SZi Shen Lim 	emit(A64_POP(r6, r7, A64_SP), ctx);
198e54bcde3SZi Shen Lim 
199e54bcde3SZi Shen Lim 	/* Restore frame pointer */
200e54bcde3SZi Shen Lim 	emit(A64_MOV(1, fp, A64_SP), ctx);
201e54bcde3SZi Shen Lim 
202e54bcde3SZi Shen Lim 	/* Set return value */
203e54bcde3SZi Shen Lim 	emit(A64_MOV(1, A64_R(0), r0), ctx);
204e54bcde3SZi Shen Lim 
205e54bcde3SZi Shen Lim 	emit(A64_RET(A64_LR), ctx);
206e54bcde3SZi Shen Lim }
207e54bcde3SZi Shen Lim 
208e54bcde3SZi Shen Lim static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
209e54bcde3SZi Shen Lim {
210e54bcde3SZi Shen Lim 	const u8 code = insn->code;
211e54bcde3SZi Shen Lim 	const u8 dst = bpf2a64[insn->dst_reg];
212e54bcde3SZi Shen Lim 	const u8 src = bpf2a64[insn->src_reg];
213e54bcde3SZi Shen Lim 	const u8 tmp = bpf2a64[TMP_REG_1];
214e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
215e54bcde3SZi Shen Lim 	const s16 off = insn->off;
216e54bcde3SZi Shen Lim 	const s32 imm = insn->imm;
217e54bcde3SZi Shen Lim 	const int i = insn - ctx->prog->insnsi;
218e54bcde3SZi Shen Lim 	const bool is64 = BPF_CLASS(code) == BPF_ALU64;
219e54bcde3SZi Shen Lim 	u8 jmp_cond;
220e54bcde3SZi Shen Lim 	s32 jmp_offset;
221e54bcde3SZi Shen Lim 
222e54bcde3SZi Shen Lim 	switch (code) {
223e54bcde3SZi Shen Lim 	/* dst = src */
224e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_X:
225e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_X:
226e54bcde3SZi Shen Lim 		emit(A64_MOV(is64, dst, src), ctx);
227e54bcde3SZi Shen Lim 		break;
228e54bcde3SZi Shen Lim 	/* dst = dst OP src */
229e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_X:
230e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_X:
231e54bcde3SZi Shen Lim 		emit(A64_ADD(is64, dst, dst, src), ctx);
232e54bcde3SZi Shen Lim 		break;
233e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_X:
234e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_X:
235e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, src), ctx);
236e54bcde3SZi Shen Lim 		break;
237e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_X:
238e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_X:
239e54bcde3SZi Shen Lim 		emit(A64_AND(is64, dst, dst, src), ctx);
240e54bcde3SZi Shen Lim 		break;
241e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_X:
242e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_X:
243e54bcde3SZi Shen Lim 		emit(A64_ORR(is64, dst, dst, src), ctx);
244e54bcde3SZi Shen Lim 		break;
245e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_X:
246e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_X:
247e54bcde3SZi Shen Lim 		emit(A64_EOR(is64, dst, dst, src), ctx);
248e54bcde3SZi Shen Lim 		break;
249e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_X:
250e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_X:
251e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, src), ctx);
252e54bcde3SZi Shen Lim 		break;
253e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_X:
254e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_X:
255e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, dst, dst, src), ctx);
256e54bcde3SZi Shen Lim 		break;
257e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOD | BPF_X:
258e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOD | BPF_X:
259e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
260e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, tmp, dst, src), ctx);
261e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, tmp, tmp, src), ctx);
262e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, tmp), ctx);
263e54bcde3SZi Shen Lim 		break;
264e54bcde3SZi Shen Lim 	/* dst = -dst */
265e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_NEG:
266e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_NEG:
267e54bcde3SZi Shen Lim 		emit(A64_NEG(is64, dst, dst), ctx);
268e54bcde3SZi Shen Lim 		break;
269e54bcde3SZi Shen Lim 	/* dst = BSWAP##imm(dst) */
270e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_LE:
271e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_BE:
272e54bcde3SZi Shen Lim #ifdef CONFIG_CPU_BIG_ENDIAN
273e54bcde3SZi Shen Lim 		if (BPF_SRC(code) == BPF_FROM_BE)
274e54bcde3SZi Shen Lim 			break;
275e54bcde3SZi Shen Lim #else /* !CONFIG_CPU_BIG_ENDIAN */
276e54bcde3SZi Shen Lim 		if (BPF_SRC(code) == BPF_FROM_LE)
277e54bcde3SZi Shen Lim 			break;
278e54bcde3SZi Shen Lim #endif
279e54bcde3SZi Shen Lim 		switch (imm) {
280e54bcde3SZi Shen Lim 		case 16:
281e54bcde3SZi Shen Lim 			emit(A64_REV16(is64, dst, dst), ctx);
282e54bcde3SZi Shen Lim 			break;
283e54bcde3SZi Shen Lim 		case 32:
284e54bcde3SZi Shen Lim 			emit(A64_REV32(is64, dst, dst), ctx);
285e54bcde3SZi Shen Lim 			break;
286e54bcde3SZi Shen Lim 		case 64:
287e54bcde3SZi Shen Lim 			emit(A64_REV64(dst, dst), ctx);
288e54bcde3SZi Shen Lim 			break;
289e54bcde3SZi Shen Lim 		}
290e54bcde3SZi Shen Lim 		break;
291e54bcde3SZi Shen Lim 	/* dst = imm */
292e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_K:
293e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_K:
294e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, dst, imm, ctx);
295e54bcde3SZi Shen Lim 		break;
296e54bcde3SZi Shen Lim 	/* dst = dst OP imm */
297e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_K:
298e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_K:
299e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
300e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
301e54bcde3SZi Shen Lim 		emit(A64_ADD(is64, dst, dst, tmp), ctx);
302e54bcde3SZi Shen Lim 		break;
303e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_K:
304e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_K:
305e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
306e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
307e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, tmp), ctx);
308e54bcde3SZi Shen Lim 		break;
309e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_K:
310e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_K:
311e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
312e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
313e54bcde3SZi Shen Lim 		emit(A64_AND(is64, dst, dst, tmp), ctx);
314e54bcde3SZi Shen Lim 		break;
315e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_K:
316e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_K:
317e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
318e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
319e54bcde3SZi Shen Lim 		emit(A64_ORR(is64, dst, dst, tmp), ctx);
320e54bcde3SZi Shen Lim 		break;
321e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_K:
322e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_K:
323e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
324e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
325e54bcde3SZi Shen Lim 		emit(A64_EOR(is64, dst, dst, tmp), ctx);
326e54bcde3SZi Shen Lim 		break;
327e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_K:
328e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_K:
329e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
330e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
331e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, tmp), ctx);
332e54bcde3SZi Shen Lim 		break;
333e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_K:
334e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_K:
335e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
336e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
337e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, dst, dst, tmp), ctx);
338e54bcde3SZi Shen Lim 		break;
339e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOD | BPF_K:
340e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOD | BPF_K:
341e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
342e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp2, imm, ctx);
343e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
344e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, tmp, tmp, tmp2), ctx);
345e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, tmp), ctx);
346e54bcde3SZi Shen Lim 		break;
347e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_LSH | BPF_K:
348e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_LSH | BPF_K:
349e54bcde3SZi Shen Lim 		emit(A64_LSL(is64, dst, dst, imm), ctx);
350e54bcde3SZi Shen Lim 		break;
351e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_RSH | BPF_K:
352e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_RSH | BPF_K:
353e54bcde3SZi Shen Lim 		emit(A64_LSR(is64, dst, dst, imm), ctx);
354e54bcde3SZi Shen Lim 		break;
355e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ARSH | BPF_K:
356e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ARSH | BPF_K:
357e54bcde3SZi Shen Lim 		emit(A64_ASR(is64, dst, dst, imm), ctx);
358e54bcde3SZi Shen Lim 		break;
359e54bcde3SZi Shen Lim 
360e54bcde3SZi Shen Lim #define check_imm(bits, imm) do {				\
361e54bcde3SZi Shen Lim 	if ((((imm) > 0) && ((imm) >> (bits))) ||		\
362e54bcde3SZi Shen Lim 	    (((imm) < 0) && (~(imm) >> (bits)))) {		\
363e54bcde3SZi Shen Lim 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
364e54bcde3SZi Shen Lim 			i, imm, imm);				\
365e54bcde3SZi Shen Lim 		return -EINVAL;					\
366e54bcde3SZi Shen Lim 	}							\
367e54bcde3SZi Shen Lim } while (0)
368e54bcde3SZi Shen Lim #define check_imm19(imm) check_imm(19, imm)
369e54bcde3SZi Shen Lim #define check_imm26(imm) check_imm(26, imm)
370e54bcde3SZi Shen Lim 
371e54bcde3SZi Shen Lim 	/* JUMP off */
372e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JA:
373e54bcde3SZi Shen Lim 		jmp_offset = bpf2a64_offset(i + off, i, ctx);
374e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
375e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
376e54bcde3SZi Shen Lim 		break;
377e54bcde3SZi Shen Lim 	/* IF (dst COND src) JUMP off */
378e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_X:
379e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_X:
380e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_X:
381e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_X:
382e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_X:
383e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_X:
384e54bcde3SZi Shen Lim 		emit(A64_CMP(1, dst, src), ctx);
385e54bcde3SZi Shen Lim emit_cond_jmp:
386e54bcde3SZi Shen Lim 		jmp_offset = bpf2a64_offset(i + off, i, ctx);
387e54bcde3SZi Shen Lim 		check_imm19(jmp_offset);
388e54bcde3SZi Shen Lim 		switch (BPF_OP(code)) {
389e54bcde3SZi Shen Lim 		case BPF_JEQ:
390e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_EQ;
391e54bcde3SZi Shen Lim 			break;
392e54bcde3SZi Shen Lim 		case BPF_JGT:
393e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_HI;
394e54bcde3SZi Shen Lim 			break;
395e54bcde3SZi Shen Lim 		case BPF_JGE:
396e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_CS;
397e54bcde3SZi Shen Lim 			break;
398e54bcde3SZi Shen Lim 		case BPF_JNE:
399e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_NE;
400e54bcde3SZi Shen Lim 			break;
401e54bcde3SZi Shen Lim 		case BPF_JSGT:
402e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GT;
403e54bcde3SZi Shen Lim 			break;
404e54bcde3SZi Shen Lim 		case BPF_JSGE:
405e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GE;
406e54bcde3SZi Shen Lim 			break;
407e54bcde3SZi Shen Lim 		default:
408e54bcde3SZi Shen Lim 			return -EFAULT;
409e54bcde3SZi Shen Lim 		}
410e54bcde3SZi Shen Lim 		emit(A64_B_(jmp_cond, jmp_offset), ctx);
411e54bcde3SZi Shen Lim 		break;
412e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_X:
413e54bcde3SZi Shen Lim 		emit(A64_TST(1, dst, src), ctx);
414e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
415e54bcde3SZi Shen Lim 	/* IF (dst COND imm) JUMP off */
416e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_K:
417e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_K:
418e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_K:
419e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_K:
420e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_K:
421e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_K:
422e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
423e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, imm, ctx);
424e54bcde3SZi Shen Lim 		emit(A64_CMP(1, dst, tmp), ctx);
425e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
426e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_K:
427e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
428e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, imm, ctx);
429e54bcde3SZi Shen Lim 		emit(A64_TST(1, dst, tmp), ctx);
430e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
431e54bcde3SZi Shen Lim 	/* function call */
432e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_CALL:
433e54bcde3SZi Shen Lim 	{
434e54bcde3SZi Shen Lim 		const u8 r0 = bpf2a64[BPF_REG_0];
435e54bcde3SZi Shen Lim 		const u64 func = (u64)__bpf_call_base + imm;
436e54bcde3SZi Shen Lim 
437e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
438e54bcde3SZi Shen Lim 		emit_a64_mov_i64(tmp, func, ctx);
439e54bcde3SZi Shen Lim 		emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
440e54bcde3SZi Shen Lim 		emit(A64_MOV(1, A64_FP, A64_SP), ctx);
441e54bcde3SZi Shen Lim 		emit(A64_BLR(tmp), ctx);
442e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
443e54bcde3SZi Shen Lim 		emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
444e54bcde3SZi Shen Lim 		break;
445e54bcde3SZi Shen Lim 	}
446e54bcde3SZi Shen Lim 	/* function return */
447e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_EXIT:
448e54bcde3SZi Shen Lim 		if (i == ctx->prog->len - 1)
449e54bcde3SZi Shen Lim 			break;
450e54bcde3SZi Shen Lim 		jmp_offset = epilogue_offset(ctx);
451e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
452e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
453e54bcde3SZi Shen Lim 		break;
454e54bcde3SZi Shen Lim 
455e54bcde3SZi Shen Lim 	/* LDX: dst = *(size *)(src + off) */
456e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_W:
457e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_H:
458e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_B:
459e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_DW:
460e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
461e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, off, ctx);
462e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
463e54bcde3SZi Shen Lim 		case BPF_W:
464e54bcde3SZi Shen Lim 			emit(A64_LDR32(dst, src, tmp), ctx);
465e54bcde3SZi Shen Lim 			break;
466e54bcde3SZi Shen Lim 		case BPF_H:
467e54bcde3SZi Shen Lim 			emit(A64_LDRH(dst, src, tmp), ctx);
468e54bcde3SZi Shen Lim 			break;
469e54bcde3SZi Shen Lim 		case BPF_B:
470e54bcde3SZi Shen Lim 			emit(A64_LDRB(dst, src, tmp), ctx);
471e54bcde3SZi Shen Lim 			break;
472e54bcde3SZi Shen Lim 		case BPF_DW:
473e54bcde3SZi Shen Lim 			emit(A64_LDR64(dst, src, tmp), ctx);
474e54bcde3SZi Shen Lim 			break;
475e54bcde3SZi Shen Lim 		}
476e54bcde3SZi Shen Lim 		break;
477e54bcde3SZi Shen Lim 
478e54bcde3SZi Shen Lim 	/* ST: *(size *)(dst + off) = imm */
479e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_W:
480e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_H:
481e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_B:
482e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_DW:
483e54bcde3SZi Shen Lim 		goto notyet;
484e54bcde3SZi Shen Lim 
485e54bcde3SZi Shen Lim 	/* STX: *(size *)(dst + off) = src */
486e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_W:
487e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_H:
488e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_B:
489e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_DW:
490e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
491e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, off, ctx);
492e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
493e54bcde3SZi Shen Lim 		case BPF_W:
494e54bcde3SZi Shen Lim 			emit(A64_STR32(src, dst, tmp), ctx);
495e54bcde3SZi Shen Lim 			break;
496e54bcde3SZi Shen Lim 		case BPF_H:
497e54bcde3SZi Shen Lim 			emit(A64_STRH(src, dst, tmp), ctx);
498e54bcde3SZi Shen Lim 			break;
499e54bcde3SZi Shen Lim 		case BPF_B:
500e54bcde3SZi Shen Lim 			emit(A64_STRB(src, dst, tmp), ctx);
501e54bcde3SZi Shen Lim 			break;
502e54bcde3SZi Shen Lim 		case BPF_DW:
503e54bcde3SZi Shen Lim 			emit(A64_STR64(src, dst, tmp), ctx);
504e54bcde3SZi Shen Lim 			break;
505e54bcde3SZi Shen Lim 		}
506e54bcde3SZi Shen Lim 		break;
507e54bcde3SZi Shen Lim 	/* STX XADD: lock *(u32 *)(dst + off) += src */
508e54bcde3SZi Shen Lim 	case BPF_STX | BPF_XADD | BPF_W:
509e54bcde3SZi Shen Lim 	/* STX XADD: lock *(u64 *)(dst + off) += src */
510e54bcde3SZi Shen Lim 	case BPF_STX | BPF_XADD | BPF_DW:
511e54bcde3SZi Shen Lim 		goto notyet;
512e54bcde3SZi Shen Lim 
513e54bcde3SZi Shen Lim 	/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */
514e54bcde3SZi Shen Lim 	case BPF_LD | BPF_ABS | BPF_W:
515e54bcde3SZi Shen Lim 	case BPF_LD | BPF_ABS | BPF_H:
516e54bcde3SZi Shen Lim 	case BPF_LD | BPF_ABS | BPF_B:
517e54bcde3SZi Shen Lim 	/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */
518e54bcde3SZi Shen Lim 	case BPF_LD | BPF_IND | BPF_W:
519e54bcde3SZi Shen Lim 	case BPF_LD | BPF_IND | BPF_H:
520e54bcde3SZi Shen Lim 	case BPF_LD | BPF_IND | BPF_B:
521e54bcde3SZi Shen Lim 	{
522e54bcde3SZi Shen Lim 		const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */
523e54bcde3SZi Shen Lim 		const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */
524e54bcde3SZi Shen Lim 		const u8 fp = bpf2a64[BPF_REG_FP];
525e54bcde3SZi Shen Lim 		const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */
526e54bcde3SZi Shen Lim 		const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */
527e54bcde3SZi Shen Lim 		const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */
528e54bcde3SZi Shen Lim 		const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */
529e54bcde3SZi Shen Lim 		const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */
530e54bcde3SZi Shen Lim 		int size;
531e54bcde3SZi Shen Lim 
532e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r1, r6), ctx);
533e54bcde3SZi Shen Lim 		emit_a64_mov_i(0, r2, imm, ctx);
534e54bcde3SZi Shen Lim 		if (BPF_MODE(code) == BPF_IND)
535e54bcde3SZi Shen Lim 			emit(A64_ADD(0, r2, r2, src), ctx);
536e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
537e54bcde3SZi Shen Lim 		case BPF_W:
538e54bcde3SZi Shen Lim 			size = 4;
539e54bcde3SZi Shen Lim 			break;
540e54bcde3SZi Shen Lim 		case BPF_H:
541e54bcde3SZi Shen Lim 			size = 2;
542e54bcde3SZi Shen Lim 			break;
543e54bcde3SZi Shen Lim 		case BPF_B:
544e54bcde3SZi Shen Lim 			size = 1;
545e54bcde3SZi Shen Lim 			break;
546e54bcde3SZi Shen Lim 		default:
547e54bcde3SZi Shen Lim 			return -EINVAL;
548e54bcde3SZi Shen Lim 		}
549e54bcde3SZi Shen Lim 		emit_a64_mov_i64(r3, size, ctx);
550e54bcde3SZi Shen Lim 		emit(A64_ADD_I(1, r4, fp, MAX_BPF_STACK), ctx);
551e54bcde3SZi Shen Lim 		emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx);
552e54bcde3SZi Shen Lim 		emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
553e54bcde3SZi Shen Lim 		emit(A64_MOV(1, A64_FP, A64_SP), ctx);
554e54bcde3SZi Shen Lim 		emit(A64_BLR(r5), ctx);
555e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
556e54bcde3SZi Shen Lim 		emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
557e54bcde3SZi Shen Lim 
558e54bcde3SZi Shen Lim 		jmp_offset = epilogue_offset(ctx);
559e54bcde3SZi Shen Lim 		check_imm19(jmp_offset);
560e54bcde3SZi Shen Lim 		emit(A64_CBZ(1, r0, jmp_offset), ctx);
561e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r5, r0), ctx);
562e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
563e54bcde3SZi Shen Lim 		case BPF_W:
564e54bcde3SZi Shen Lim 			emit(A64_LDR32(r0, r5, A64_ZR), ctx);
565e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN
566e54bcde3SZi Shen Lim 			emit(A64_REV32(0, r0, r0), ctx);
567e54bcde3SZi Shen Lim #endif
568e54bcde3SZi Shen Lim 			break;
569e54bcde3SZi Shen Lim 		case BPF_H:
570e54bcde3SZi Shen Lim 			emit(A64_LDRH(r0, r5, A64_ZR), ctx);
571e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN
572e54bcde3SZi Shen Lim 			emit(A64_REV16(0, r0, r0), ctx);
573e54bcde3SZi Shen Lim #endif
574e54bcde3SZi Shen Lim 			break;
575e54bcde3SZi Shen Lim 		case BPF_B:
576e54bcde3SZi Shen Lim 			emit(A64_LDRB(r0, r5, A64_ZR), ctx);
577e54bcde3SZi Shen Lim 			break;
578e54bcde3SZi Shen Lim 		}
579e54bcde3SZi Shen Lim 		break;
580e54bcde3SZi Shen Lim 	}
581e54bcde3SZi Shen Lim notyet:
582e54bcde3SZi Shen Lim 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
583e54bcde3SZi Shen Lim 		return -EFAULT;
584e54bcde3SZi Shen Lim 
585e54bcde3SZi Shen Lim 	default:
586e54bcde3SZi Shen Lim 		pr_err_once("unknown opcode %02x\n", code);
587e54bcde3SZi Shen Lim 		return -EINVAL;
588e54bcde3SZi Shen Lim 	}
589e54bcde3SZi Shen Lim 
590e54bcde3SZi Shen Lim 	return 0;
591e54bcde3SZi Shen Lim }
592e54bcde3SZi Shen Lim 
593e54bcde3SZi Shen Lim static int build_body(struct jit_ctx *ctx)
594e54bcde3SZi Shen Lim {
595e54bcde3SZi Shen Lim 	const struct bpf_prog *prog = ctx->prog;
596e54bcde3SZi Shen Lim 	int i;
597e54bcde3SZi Shen Lim 
598e54bcde3SZi Shen Lim 	for (i = 0; i < prog->len; i++) {
599e54bcde3SZi Shen Lim 		const struct bpf_insn *insn = &prog->insnsi[i];
600e54bcde3SZi Shen Lim 		int ret;
601e54bcde3SZi Shen Lim 
602e54bcde3SZi Shen Lim 		if (ctx->image == NULL)
603e54bcde3SZi Shen Lim 			ctx->offset[i] = ctx->idx;
604e54bcde3SZi Shen Lim 
605e54bcde3SZi Shen Lim 		ret = build_insn(insn, ctx);
606e54bcde3SZi Shen Lim 		if (ret)
607e54bcde3SZi Shen Lim 			return ret;
608e54bcde3SZi Shen Lim 	}
609e54bcde3SZi Shen Lim 
610e54bcde3SZi Shen Lim 	return 0;
611e54bcde3SZi Shen Lim }
612e54bcde3SZi Shen Lim 
613e54bcde3SZi Shen Lim static inline void bpf_flush_icache(void *start, void *end)
614e54bcde3SZi Shen Lim {
615e54bcde3SZi Shen Lim 	flush_icache_range((unsigned long)start, (unsigned long)end);
616e54bcde3SZi Shen Lim }
617e54bcde3SZi Shen Lim 
618e54bcde3SZi Shen Lim void bpf_jit_compile(struct bpf_prog *prog)
619e54bcde3SZi Shen Lim {
620e54bcde3SZi Shen Lim 	/* Nothing to do here. We support Internal BPF. */
621e54bcde3SZi Shen Lim }
622e54bcde3SZi Shen Lim 
623e54bcde3SZi Shen Lim void bpf_int_jit_compile(struct bpf_prog *prog)
624e54bcde3SZi Shen Lim {
625*b569c1c6SDaniel Borkmann 	struct bpf_binary_header *header;
626e54bcde3SZi Shen Lim 	struct jit_ctx ctx;
627e54bcde3SZi Shen Lim 	int image_size;
628*b569c1c6SDaniel Borkmann 	u8 *image_ptr;
629e54bcde3SZi Shen Lim 
630e54bcde3SZi Shen Lim 	if (!bpf_jit_enable)
631e54bcde3SZi Shen Lim 		return;
632e54bcde3SZi Shen Lim 
633e54bcde3SZi Shen Lim 	if (!prog || !prog->len)
634e54bcde3SZi Shen Lim 		return;
635e54bcde3SZi Shen Lim 
636e54bcde3SZi Shen Lim 	memset(&ctx, 0, sizeof(ctx));
637e54bcde3SZi Shen Lim 	ctx.prog = prog;
638e54bcde3SZi Shen Lim 
639e54bcde3SZi Shen Lim 	ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
640e54bcde3SZi Shen Lim 	if (ctx.offset == NULL)
641e54bcde3SZi Shen Lim 		return;
642e54bcde3SZi Shen Lim 
643e54bcde3SZi Shen Lim 	/* 1. Initial fake pass to compute ctx->idx. */
644e54bcde3SZi Shen Lim 
645e54bcde3SZi Shen Lim 	/* Fake pass to fill in ctx->offset. */
646e54bcde3SZi Shen Lim 	if (build_body(&ctx))
647e54bcde3SZi Shen Lim 		goto out;
648e54bcde3SZi Shen Lim 
649e54bcde3SZi Shen Lim 	build_prologue(&ctx);
650e54bcde3SZi Shen Lim 	build_epilogue(&ctx);
651e54bcde3SZi Shen Lim 
652e54bcde3SZi Shen Lim 	/* Now we know the actual image size. */
653e54bcde3SZi Shen Lim 	image_size = sizeof(u32) * ctx.idx;
654*b569c1c6SDaniel Borkmann 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
655*b569c1c6SDaniel Borkmann 				      sizeof(u32), jit_fill_hole);
656*b569c1c6SDaniel Borkmann 	if (header == NULL)
657e54bcde3SZi Shen Lim 		goto out;
658e54bcde3SZi Shen Lim 
659e54bcde3SZi Shen Lim 	/* 2. Now, the actual pass. */
660e54bcde3SZi Shen Lim 
661*b569c1c6SDaniel Borkmann 	ctx.image = (u32 *)image_ptr;
662e54bcde3SZi Shen Lim 	ctx.idx = 0;
663*b569c1c6SDaniel Borkmann 
664e54bcde3SZi Shen Lim 	build_prologue(&ctx);
665e54bcde3SZi Shen Lim 
666e54bcde3SZi Shen Lim 	ctx.body_offset = ctx.idx;
66760ef0494SDaniel Borkmann 	if (build_body(&ctx)) {
668*b569c1c6SDaniel Borkmann 		bpf_jit_binary_free(header);
669e54bcde3SZi Shen Lim 		goto out;
67060ef0494SDaniel Borkmann 	}
671e54bcde3SZi Shen Lim 
672e54bcde3SZi Shen Lim 	build_epilogue(&ctx);
673e54bcde3SZi Shen Lim 
674e54bcde3SZi Shen Lim 	/* And we're done. */
675e54bcde3SZi Shen Lim 	if (bpf_jit_enable > 1)
676e54bcde3SZi Shen Lim 		bpf_jit_dump(prog->len, image_size, 2, ctx.image);
677e54bcde3SZi Shen Lim 
678e54bcde3SZi Shen Lim 	bpf_flush_icache(ctx.image, ctx.image + ctx.idx);
679*b569c1c6SDaniel Borkmann 
680*b569c1c6SDaniel Borkmann 	set_memory_ro((unsigned long)header, header->pages);
681e54bcde3SZi Shen Lim 	prog->bpf_func = (void *)ctx.image;
682e54bcde3SZi Shen Lim 	prog->jited = 1;
683e54bcde3SZi Shen Lim out:
684e54bcde3SZi Shen Lim 	kfree(ctx.offset);
685e54bcde3SZi Shen Lim }
686e54bcde3SZi Shen Lim 
687e54bcde3SZi Shen Lim void bpf_jit_free(struct bpf_prog *prog)
688e54bcde3SZi Shen Lim {
689*b569c1c6SDaniel Borkmann 	unsigned long addr = (unsigned long)prog->bpf_func & PAGE_MASK;
690*b569c1c6SDaniel Borkmann 	struct bpf_binary_header *header = (void *)addr;
691e54bcde3SZi Shen Lim 
692*b569c1c6SDaniel Borkmann 	if (!prog->jited)
693*b569c1c6SDaniel Borkmann 		goto free_filter;
694*b569c1c6SDaniel Borkmann 
695*b569c1c6SDaniel Borkmann 	set_memory_rw(addr, header->pages);
696*b569c1c6SDaniel Borkmann 	bpf_jit_binary_free(header);
697*b569c1c6SDaniel Borkmann 
698*b569c1c6SDaniel Borkmann free_filter:
699*b569c1c6SDaniel Borkmann 	bpf_prog_unlock_free(prog);
700e54bcde3SZi Shen Lim }
701