xref: /linux/arch/arm64/net/bpf_jit_comp.c (revision df849ba3a88cdf4480decd7008f95ff627387c0f)
1e54bcde3SZi Shen Lim /*
2e54bcde3SZi Shen Lim  * BPF JIT compiler for ARM64
3e54bcde3SZi Shen Lim  *
4251599e1SZi Shen Lim  * Copyright (C) 2014-2015 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>
25b569c1c6SDaniel Borkmann 
26e54bcde3SZi Shen Lim #include <asm/byteorder.h>
27e54bcde3SZi Shen Lim #include <asm/cacheflush.h>
28b569c1c6SDaniel 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 */
53ec0738dbSYang Shi 	[BPF_REG_FP] = A64_R(25),
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;
6351c9fbb1SZi Shen Lim 	int epilogue_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 {
1168eee539dSXi Wang 	int to = ctx->offset[bpf_to];
117e54bcde3SZi Shen Lim 	/* -1 to account for the Branch instruction */
1188eee539dSXi Wang 	int from = ctx->offset[bpf_from] - 1;
119e54bcde3SZi Shen Lim 
120e54bcde3SZi Shen Lim 	return to - from;
121e54bcde3SZi Shen Lim }
122e54bcde3SZi Shen Lim 
123b569c1c6SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
124b569c1c6SDaniel Borkmann {
125b569c1c6SDaniel Borkmann 	u32 *ptr;
126b569c1c6SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
127b569c1c6SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
128b569c1c6SDaniel Borkmann 		*ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
129b569c1c6SDaniel Borkmann }
130b569c1c6SDaniel Borkmann 
131e54bcde3SZi Shen Lim static inline int epilogue_offset(const struct jit_ctx *ctx)
132e54bcde3SZi Shen Lim {
13351c9fbb1SZi Shen Lim 	int to = ctx->epilogue_offset;
13451c9fbb1SZi Shen Lim 	int from = ctx->idx;
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 
142f4b16fceSZi Shen Lim #define _STACK_SIZE \
143f4b16fceSZi Shen Lim 	(MAX_BPF_STACK \
144f4b16fceSZi Shen Lim 	 + 4 /* extra for skb_copy_bits buffer */)
145f4b16fceSZi Shen Lim 
146f4b16fceSZi Shen Lim #define STACK_SIZE STACK_ALIGN(_STACK_SIZE)
147f4b16fceSZi Shen Lim 
148e54bcde3SZi Shen Lim static void build_prologue(struct jit_ctx *ctx)
149e54bcde3SZi Shen Lim {
150e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
151e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
152e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
153e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
154e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
155e54bcde3SZi Shen Lim 	const u8 ra = bpf2a64[BPF_REG_A];
156e54bcde3SZi Shen Lim 	const u8 rx = bpf2a64[BPF_REG_X];
157e54bcde3SZi Shen Lim 	const u8 tmp1 = bpf2a64[TMP_REG_1];
158e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
159e54bcde3SZi Shen Lim 
160ec0738dbSYang Shi 	/*
161ec0738dbSYang Shi 	 * BPF prog stack layout
162ec0738dbSYang Shi 	 *
163ec0738dbSYang Shi 	 *                         high
164ec0738dbSYang Shi 	 * original A64_SP =>   0:+-----+ BPF prologue
165ec0738dbSYang Shi 	 *                        |FP/LR|
166ec0738dbSYang Shi 	 * current A64_FP =>  -16:+-----+
167ec0738dbSYang Shi 	 *                        | ... | callee saved registers
168ec0738dbSYang Shi 	 *                        +-----+
169ec0738dbSYang Shi 	 *                        |     | x25/x26
170f4b16fceSZi Shen Lim 	 * BPF fp register => -80:+-----+ <= (BPF_FP)
171ec0738dbSYang Shi 	 *                        |     |
172ec0738dbSYang Shi 	 *                        | ... | BPF prog stack
173ec0738dbSYang Shi 	 *                        |     |
174f4b16fceSZi Shen Lim 	 *                        +-----+ <= (BPF_FP - MAX_BPF_STACK)
175f4b16fceSZi Shen Lim 	 *                        |RSVD | JIT scratchpad
176f4b16fceSZi Shen Lim 	 * current A64_SP =>      +-----+ <= (BPF_FP - STACK_SIZE)
177ec0738dbSYang Shi 	 *                        |     |
178ec0738dbSYang Shi 	 *                        | ... | Function call stack
179ec0738dbSYang Shi 	 *                        |     |
180ec0738dbSYang Shi 	 *                        +-----+
181ec0738dbSYang Shi 	 *                          low
182ec0738dbSYang Shi 	 *
183ec0738dbSYang Shi 	 */
184ec0738dbSYang Shi 
185ec0738dbSYang Shi 	/* Save FP and LR registers to stay align with ARM64 AAPCS */
186ec0738dbSYang Shi 	emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
187ec0738dbSYang Shi 	emit(A64_MOV(1, A64_FP, A64_SP), ctx);
188ec0738dbSYang Shi 
189e54bcde3SZi Shen Lim 	/* Save callee-saved register */
190e54bcde3SZi Shen Lim 	emit(A64_PUSH(r6, r7, A64_SP), ctx);
191e54bcde3SZi Shen Lim 	emit(A64_PUSH(r8, r9, A64_SP), ctx);
192e54bcde3SZi Shen Lim 	if (ctx->tmp_used)
193e54bcde3SZi Shen Lim 		emit(A64_PUSH(tmp1, tmp2, A64_SP), ctx);
194e54bcde3SZi Shen Lim 
195ec0738dbSYang Shi 	/* Save fp (x25) and x26. SP requires 16 bytes alignment */
196ec0738dbSYang Shi 	emit(A64_PUSH(fp, A64_R(26), A64_SP), ctx);
197e54bcde3SZi Shen Lim 
198ec0738dbSYang Shi 	/* Set up BPF prog stack base register (x25) */
199e54bcde3SZi Shen Lim 	emit(A64_MOV(1, fp, A64_SP), ctx);
200e54bcde3SZi Shen Lim 
201ec0738dbSYang Shi 	/* Set up function call stack */
202f4b16fceSZi Shen Lim 	emit(A64_SUB_I(1, A64_SP, A64_SP, STACK_SIZE), ctx);
2030fcd593bSYang Shi 
204e54bcde3SZi Shen Lim 	/* Clear registers A and X */
205e54bcde3SZi Shen Lim 	emit_a64_mov_i64(ra, 0, ctx);
206e54bcde3SZi Shen Lim 	emit_a64_mov_i64(rx, 0, ctx);
207e54bcde3SZi Shen Lim }
208e54bcde3SZi Shen Lim 
209e54bcde3SZi Shen Lim static void build_epilogue(struct jit_ctx *ctx)
210e54bcde3SZi Shen Lim {
211e54bcde3SZi Shen Lim 	const u8 r0 = bpf2a64[BPF_REG_0];
212e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
213e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
214e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
215e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
216e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
217e54bcde3SZi Shen Lim 	const u8 tmp1 = bpf2a64[TMP_REG_1];
218e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
219e54bcde3SZi Shen Lim 
220e54bcde3SZi Shen Lim 	/* We're done with BPF stack */
221f4b16fceSZi Shen Lim 	emit(A64_ADD_I(1, A64_SP, A64_SP, STACK_SIZE), ctx);
222e54bcde3SZi Shen Lim 
223ec0738dbSYang Shi 	/* Restore fs (x25) and x26 */
224ec0738dbSYang Shi 	emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
225ec0738dbSYang Shi 
226e54bcde3SZi Shen Lim 	/* Restore callee-saved register */
227e54bcde3SZi Shen Lim 	if (ctx->tmp_used)
228e54bcde3SZi Shen Lim 		emit(A64_POP(tmp1, tmp2, A64_SP), ctx);
229e54bcde3SZi Shen Lim 	emit(A64_POP(r8, r9, A64_SP), ctx);
230e54bcde3SZi Shen Lim 	emit(A64_POP(r6, r7, A64_SP), ctx);
231e54bcde3SZi Shen Lim 
232ec0738dbSYang Shi 	/* Restore FP/LR registers */
233ec0738dbSYang Shi 	emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
234e54bcde3SZi Shen Lim 
235e54bcde3SZi Shen Lim 	/* Set return value */
236e54bcde3SZi Shen Lim 	emit(A64_MOV(1, A64_R(0), r0), ctx);
237e54bcde3SZi Shen Lim 
238e54bcde3SZi Shen Lim 	emit(A64_RET(A64_LR), ctx);
239e54bcde3SZi Shen Lim }
240e54bcde3SZi Shen Lim 
24130d3d94cSZi Shen Lim /* JITs an eBPF instruction.
24230d3d94cSZi Shen Lim  * Returns:
24330d3d94cSZi Shen Lim  * 0  - successfully JITed an 8-byte eBPF instruction.
24430d3d94cSZi Shen Lim  * >0 - successfully JITed a 16-byte eBPF instruction.
24530d3d94cSZi Shen Lim  * <0 - failed to JIT.
24630d3d94cSZi Shen Lim  */
247e54bcde3SZi Shen Lim static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
248e54bcde3SZi Shen Lim {
249e54bcde3SZi Shen Lim 	const u8 code = insn->code;
250e54bcde3SZi Shen Lim 	const u8 dst = bpf2a64[insn->dst_reg];
251e54bcde3SZi Shen Lim 	const u8 src = bpf2a64[insn->src_reg];
252e54bcde3SZi Shen Lim 	const u8 tmp = bpf2a64[TMP_REG_1];
253e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
254e54bcde3SZi Shen Lim 	const s16 off = insn->off;
255e54bcde3SZi Shen Lim 	const s32 imm = insn->imm;
256e54bcde3SZi Shen Lim 	const int i = insn - ctx->prog->insnsi;
257e54bcde3SZi Shen Lim 	const bool is64 = BPF_CLASS(code) == BPF_ALU64;
258e54bcde3SZi Shen Lim 	u8 jmp_cond;
259e54bcde3SZi Shen Lim 	s32 jmp_offset;
260e54bcde3SZi Shen Lim 
261251599e1SZi Shen Lim #define check_imm(bits, imm) do {				\
262251599e1SZi Shen Lim 	if ((((imm) > 0) && ((imm) >> (bits))) ||		\
263251599e1SZi Shen Lim 	    (((imm) < 0) && (~(imm) >> (bits)))) {		\
264251599e1SZi Shen Lim 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
265251599e1SZi Shen Lim 			i, imm, imm);				\
266251599e1SZi Shen Lim 		return -EINVAL;					\
267251599e1SZi Shen Lim 	}							\
268251599e1SZi Shen Lim } while (0)
269251599e1SZi Shen Lim #define check_imm19(imm) check_imm(19, imm)
270251599e1SZi Shen Lim #define check_imm26(imm) check_imm(26, imm)
271251599e1SZi Shen Lim 
272e54bcde3SZi Shen Lim 	switch (code) {
273e54bcde3SZi Shen Lim 	/* dst = src */
274e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_X:
275e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_X:
276e54bcde3SZi Shen Lim 		emit(A64_MOV(is64, dst, src), ctx);
277e54bcde3SZi Shen Lim 		break;
278e54bcde3SZi Shen Lim 	/* dst = dst OP src */
279e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_X:
280e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_X:
281e54bcde3SZi Shen Lim 		emit(A64_ADD(is64, dst, dst, src), ctx);
282e54bcde3SZi Shen Lim 		break;
283e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_X:
284e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_X:
285e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, src), ctx);
286e54bcde3SZi Shen Lim 		break;
287e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_X:
288e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_X:
289e54bcde3SZi Shen Lim 		emit(A64_AND(is64, dst, dst, src), ctx);
290e54bcde3SZi Shen Lim 		break;
291e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_X:
292e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_X:
293e54bcde3SZi Shen Lim 		emit(A64_ORR(is64, dst, dst, src), ctx);
294e54bcde3SZi Shen Lim 		break;
295e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_X:
296e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_X:
297e54bcde3SZi Shen Lim 		emit(A64_EOR(is64, dst, dst, src), ctx);
298e54bcde3SZi Shen Lim 		break;
299e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_X:
300e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_X:
301e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, src), ctx);
302e54bcde3SZi Shen Lim 		break;
303e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_X:
304e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_X:
305e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOD | BPF_X:
306e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOD | BPF_X:
307251599e1SZi Shen Lim 	{
308251599e1SZi Shen Lim 		const u8 r0 = bpf2a64[BPF_REG_0];
309251599e1SZi Shen Lim 
310251599e1SZi Shen Lim 		/* if (src == 0) return 0 */
311251599e1SZi Shen Lim 		jmp_offset = 3; /* skip ahead to else path */
312251599e1SZi Shen Lim 		check_imm19(jmp_offset);
313251599e1SZi Shen Lim 		emit(A64_CBNZ(is64, src, jmp_offset), ctx);
314251599e1SZi Shen Lim 		emit(A64_MOVZ(1, r0, 0, 0), ctx);
315251599e1SZi Shen Lim 		jmp_offset = epilogue_offset(ctx);
316251599e1SZi Shen Lim 		check_imm26(jmp_offset);
317251599e1SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
318251599e1SZi Shen Lim 		/* else */
31914e589ffSZi Shen Lim 		switch (BPF_OP(code)) {
32014e589ffSZi Shen Lim 		case BPF_DIV:
321e54bcde3SZi Shen Lim 			emit(A64_UDIV(is64, dst, dst, src), ctx);
322e54bcde3SZi Shen Lim 			break;
32314e589ffSZi Shen Lim 		case BPF_MOD:
324e54bcde3SZi Shen Lim 			ctx->tmp_used = 1;
325e54bcde3SZi Shen Lim 			emit(A64_UDIV(is64, tmp, dst, src), ctx);
326e54bcde3SZi Shen Lim 			emit(A64_MUL(is64, tmp, tmp, src), ctx);
327e54bcde3SZi Shen Lim 			emit(A64_SUB(is64, dst, dst, tmp), ctx);
328e54bcde3SZi Shen Lim 			break;
32914e589ffSZi Shen Lim 		}
33014e589ffSZi Shen Lim 		break;
33114e589ffSZi Shen Lim 	}
332d65a634aSZi Shen Lim 	case BPF_ALU | BPF_LSH | BPF_X:
333d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_LSH | BPF_X:
334d65a634aSZi Shen Lim 		emit(A64_LSLV(is64, dst, dst, src), ctx);
335d65a634aSZi Shen Lim 		break;
336d65a634aSZi Shen Lim 	case BPF_ALU | BPF_RSH | BPF_X:
337d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_RSH | BPF_X:
338d65a634aSZi Shen Lim 		emit(A64_LSRV(is64, dst, dst, src), ctx);
339d65a634aSZi Shen Lim 		break;
340d65a634aSZi Shen Lim 	case BPF_ALU | BPF_ARSH | BPF_X:
341d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_ARSH | BPF_X:
342d65a634aSZi Shen Lim 		emit(A64_ASRV(is64, dst, dst, src), ctx);
343d65a634aSZi Shen Lim 		break;
344e54bcde3SZi Shen Lim 	/* dst = -dst */
345e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_NEG:
346e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_NEG:
347e54bcde3SZi Shen Lim 		emit(A64_NEG(is64, dst, dst), ctx);
348e54bcde3SZi Shen Lim 		break;
349e54bcde3SZi Shen Lim 	/* dst = BSWAP##imm(dst) */
350e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_LE:
351e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_BE:
352e54bcde3SZi Shen Lim #ifdef CONFIG_CPU_BIG_ENDIAN
353e54bcde3SZi Shen Lim 		if (BPF_SRC(code) == BPF_FROM_BE)
354d63903bbSXi Wang 			goto emit_bswap_uxt;
355e54bcde3SZi Shen Lim #else /* !CONFIG_CPU_BIG_ENDIAN */
356e54bcde3SZi Shen Lim 		if (BPF_SRC(code) == BPF_FROM_LE)
357d63903bbSXi Wang 			goto emit_bswap_uxt;
358e54bcde3SZi Shen Lim #endif
359e54bcde3SZi Shen Lim 		switch (imm) {
360e54bcde3SZi Shen Lim 		case 16:
361e54bcde3SZi Shen Lim 			emit(A64_REV16(is64, dst, dst), ctx);
362d63903bbSXi Wang 			/* zero-extend 16 bits into 64 bits */
363d63903bbSXi Wang 			emit(A64_UXTH(is64, dst, dst), ctx);
364e54bcde3SZi Shen Lim 			break;
365e54bcde3SZi Shen Lim 		case 32:
366e54bcde3SZi Shen Lim 			emit(A64_REV32(is64, dst, dst), ctx);
367d63903bbSXi Wang 			/* upper 32 bits already cleared */
368e54bcde3SZi Shen Lim 			break;
369e54bcde3SZi Shen Lim 		case 64:
370e54bcde3SZi Shen Lim 			emit(A64_REV64(dst, dst), ctx);
371e54bcde3SZi Shen Lim 			break;
372e54bcde3SZi Shen Lim 		}
373e54bcde3SZi Shen Lim 		break;
374d63903bbSXi Wang emit_bswap_uxt:
375d63903bbSXi Wang 		switch (imm) {
376d63903bbSXi Wang 		case 16:
377d63903bbSXi Wang 			/* zero-extend 16 bits into 64 bits */
378d63903bbSXi Wang 			emit(A64_UXTH(is64, dst, dst), ctx);
379d63903bbSXi Wang 			break;
380d63903bbSXi Wang 		case 32:
381d63903bbSXi Wang 			/* zero-extend 32 bits into 64 bits */
382d63903bbSXi Wang 			emit(A64_UXTW(is64, dst, dst), ctx);
383d63903bbSXi Wang 			break;
384d63903bbSXi Wang 		case 64:
385d63903bbSXi Wang 			/* nop */
386d63903bbSXi Wang 			break;
387d63903bbSXi Wang 		}
388d63903bbSXi Wang 		break;
389e54bcde3SZi Shen Lim 	/* dst = imm */
390e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_K:
391e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_K:
392e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, dst, imm, ctx);
393e54bcde3SZi Shen Lim 		break;
394e54bcde3SZi Shen Lim 	/* dst = dst OP imm */
395e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_K:
396e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_K:
397e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
398e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
399e54bcde3SZi Shen Lim 		emit(A64_ADD(is64, dst, dst, tmp), ctx);
400e54bcde3SZi Shen Lim 		break;
401e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_K:
402e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_K:
403e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
404e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
405e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, tmp), ctx);
406e54bcde3SZi Shen Lim 		break;
407e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_K:
408e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_K:
409e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
410e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
411e54bcde3SZi Shen Lim 		emit(A64_AND(is64, dst, dst, tmp), ctx);
412e54bcde3SZi Shen Lim 		break;
413e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_K:
414e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_K:
415e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
416e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
417e54bcde3SZi Shen Lim 		emit(A64_ORR(is64, dst, dst, tmp), ctx);
418e54bcde3SZi Shen Lim 		break;
419e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_K:
420e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_K:
421e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
422e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
423e54bcde3SZi Shen Lim 		emit(A64_EOR(is64, dst, dst, tmp), ctx);
424e54bcde3SZi Shen Lim 		break;
425e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_K:
426e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_K:
427e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
428e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
429e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, tmp), ctx);
430e54bcde3SZi Shen Lim 		break;
431e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_K:
432e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_K:
433e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
434e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
435e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, dst, dst, tmp), ctx);
436e54bcde3SZi Shen Lim 		break;
437e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOD | BPF_K:
438e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOD | BPF_K:
439e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
440e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp2, imm, ctx);
441e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
442e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, tmp, tmp, tmp2), ctx);
443e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, tmp), ctx);
444e54bcde3SZi Shen Lim 		break;
445e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_LSH | BPF_K:
446e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_LSH | BPF_K:
447e54bcde3SZi Shen Lim 		emit(A64_LSL(is64, dst, dst, imm), ctx);
448e54bcde3SZi Shen Lim 		break;
449e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_RSH | BPF_K:
450e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_RSH | BPF_K:
451e54bcde3SZi Shen Lim 		emit(A64_LSR(is64, dst, dst, imm), ctx);
452e54bcde3SZi Shen Lim 		break;
453e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ARSH | BPF_K:
454e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ARSH | BPF_K:
455e54bcde3SZi Shen Lim 		emit(A64_ASR(is64, dst, dst, imm), ctx);
456e54bcde3SZi Shen Lim 		break;
457e54bcde3SZi Shen Lim 
458e54bcde3SZi Shen Lim 	/* JUMP off */
459e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JA:
460e54bcde3SZi Shen Lim 		jmp_offset = bpf2a64_offset(i + off, i, ctx);
461e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
462e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
463e54bcde3SZi Shen Lim 		break;
464e54bcde3SZi Shen Lim 	/* IF (dst COND src) JUMP off */
465e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_X:
466e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_X:
467e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_X:
468e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_X:
469e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_X:
470e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_X:
471e54bcde3SZi Shen Lim 		emit(A64_CMP(1, dst, src), ctx);
472e54bcde3SZi Shen Lim emit_cond_jmp:
473e54bcde3SZi Shen Lim 		jmp_offset = bpf2a64_offset(i + off, i, ctx);
474e54bcde3SZi Shen Lim 		check_imm19(jmp_offset);
475e54bcde3SZi Shen Lim 		switch (BPF_OP(code)) {
476e54bcde3SZi Shen Lim 		case BPF_JEQ:
477e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_EQ;
478e54bcde3SZi Shen Lim 			break;
479e54bcde3SZi Shen Lim 		case BPF_JGT:
480e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_HI;
481e54bcde3SZi Shen Lim 			break;
482e54bcde3SZi Shen Lim 		case BPF_JGE:
483e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_CS;
484e54bcde3SZi Shen Lim 			break;
485e54bcde3SZi Shen Lim 		case BPF_JNE:
486e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_NE;
487e54bcde3SZi Shen Lim 			break;
488e54bcde3SZi Shen Lim 		case BPF_JSGT:
489e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GT;
490e54bcde3SZi Shen Lim 			break;
491e54bcde3SZi Shen Lim 		case BPF_JSGE:
492e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GE;
493e54bcde3SZi Shen Lim 			break;
494e54bcde3SZi Shen Lim 		default:
495e54bcde3SZi Shen Lim 			return -EFAULT;
496e54bcde3SZi Shen Lim 		}
497e54bcde3SZi Shen Lim 		emit(A64_B_(jmp_cond, jmp_offset), ctx);
498e54bcde3SZi Shen Lim 		break;
499e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_X:
500e54bcde3SZi Shen Lim 		emit(A64_TST(1, dst, src), ctx);
501e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
502e54bcde3SZi Shen Lim 	/* IF (dst COND imm) JUMP off */
503e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_K:
504e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_K:
505e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_K:
506e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_K:
507e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_K:
508e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_K:
509e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
510e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, imm, ctx);
511e54bcde3SZi Shen Lim 		emit(A64_CMP(1, dst, tmp), ctx);
512e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
513e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_K:
514e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
515e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, imm, ctx);
516e54bcde3SZi Shen Lim 		emit(A64_TST(1, dst, tmp), ctx);
517e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
518e54bcde3SZi Shen Lim 	/* function call */
519e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_CALL:
520e54bcde3SZi Shen Lim 	{
521e54bcde3SZi Shen Lim 		const u8 r0 = bpf2a64[BPF_REG_0];
522e54bcde3SZi Shen Lim 		const u64 func = (u64)__bpf_call_base + imm;
523e54bcde3SZi Shen Lim 
524e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
525e54bcde3SZi Shen Lim 		emit_a64_mov_i64(tmp, func, ctx);
526e54bcde3SZi Shen Lim 		emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
527e54bcde3SZi Shen Lim 		emit(A64_MOV(1, A64_FP, A64_SP), ctx);
528e54bcde3SZi Shen Lim 		emit(A64_BLR(tmp), ctx);
529e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
530e54bcde3SZi Shen Lim 		emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
531e54bcde3SZi Shen Lim 		break;
532e54bcde3SZi Shen Lim 	}
533e54bcde3SZi Shen Lim 	/* function return */
534e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_EXIT:
53551c9fbb1SZi Shen Lim 		/* Optimization: when last instruction is EXIT,
53651c9fbb1SZi Shen Lim 		   simply fallthrough to epilogue. */
537e54bcde3SZi Shen Lim 		if (i == ctx->prog->len - 1)
538e54bcde3SZi Shen Lim 			break;
539e54bcde3SZi Shen Lim 		jmp_offset = epilogue_offset(ctx);
540e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
541e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
542e54bcde3SZi Shen Lim 		break;
543e54bcde3SZi Shen Lim 
54430d3d94cSZi Shen Lim 	/* dst = imm64 */
54530d3d94cSZi Shen Lim 	case BPF_LD | BPF_IMM | BPF_DW:
54630d3d94cSZi Shen Lim 	{
54730d3d94cSZi Shen Lim 		const struct bpf_insn insn1 = insn[1];
54830d3d94cSZi Shen Lim 		u64 imm64;
54930d3d94cSZi Shen Lim 
55030d3d94cSZi Shen Lim 		if (insn1.code != 0 || insn1.src_reg != 0 ||
55130d3d94cSZi Shen Lim 		    insn1.dst_reg != 0 || insn1.off != 0) {
55230d3d94cSZi Shen Lim 			/* Note: verifier in BPF core must catch invalid
55330d3d94cSZi Shen Lim 			 * instructions.
55430d3d94cSZi Shen Lim 			 */
55530d3d94cSZi Shen Lim 			pr_err_once("Invalid BPF_LD_IMM64 instruction\n");
55630d3d94cSZi Shen Lim 			return -EINVAL;
55730d3d94cSZi Shen Lim 		}
55830d3d94cSZi Shen Lim 
5591e4df6b7SXi Wang 		imm64 = (u64)insn1.imm << 32 | (u32)imm;
56030d3d94cSZi Shen Lim 		emit_a64_mov_i64(dst, imm64, ctx);
56130d3d94cSZi Shen Lim 
56230d3d94cSZi Shen Lim 		return 1;
56330d3d94cSZi Shen Lim 	}
56430d3d94cSZi Shen Lim 
565e54bcde3SZi Shen Lim 	/* LDX: dst = *(size *)(src + off) */
566e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_W:
567e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_H:
568e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_B:
569e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_DW:
570e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
571e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, off, ctx);
572e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
573e54bcde3SZi Shen Lim 		case BPF_W:
574e54bcde3SZi Shen Lim 			emit(A64_LDR32(dst, src, tmp), ctx);
575e54bcde3SZi Shen Lim 			break;
576e54bcde3SZi Shen Lim 		case BPF_H:
577e54bcde3SZi Shen Lim 			emit(A64_LDRH(dst, src, tmp), ctx);
578e54bcde3SZi Shen Lim 			break;
579e54bcde3SZi Shen Lim 		case BPF_B:
580e54bcde3SZi Shen Lim 			emit(A64_LDRB(dst, src, tmp), ctx);
581e54bcde3SZi Shen Lim 			break;
582e54bcde3SZi Shen Lim 		case BPF_DW:
583e54bcde3SZi Shen Lim 			emit(A64_LDR64(dst, src, tmp), ctx);
584e54bcde3SZi Shen Lim 			break;
585e54bcde3SZi Shen Lim 		}
586e54bcde3SZi Shen Lim 		break;
587e54bcde3SZi Shen Lim 
588e54bcde3SZi Shen Lim 	/* ST: *(size *)(dst + off) = imm */
589e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_W:
590e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_H:
591e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_B:
592e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_DW:
593*df849ba3SYang Shi 		/* Load imm to a register then store it */
594*df849ba3SYang Shi 		ctx->tmp_used = 1;
595*df849ba3SYang Shi 		emit_a64_mov_i(1, tmp2, off, ctx);
596*df849ba3SYang Shi 		emit_a64_mov_i(1, tmp, imm, ctx);
597*df849ba3SYang Shi 		switch (BPF_SIZE(code)) {
598*df849ba3SYang Shi 		case BPF_W:
599*df849ba3SYang Shi 			emit(A64_STR32(tmp, dst, tmp2), ctx);
600*df849ba3SYang Shi 			break;
601*df849ba3SYang Shi 		case BPF_H:
602*df849ba3SYang Shi 			emit(A64_STRH(tmp, dst, tmp2), ctx);
603*df849ba3SYang Shi 			break;
604*df849ba3SYang Shi 		case BPF_B:
605*df849ba3SYang Shi 			emit(A64_STRB(tmp, dst, tmp2), ctx);
606*df849ba3SYang Shi 			break;
607*df849ba3SYang Shi 		case BPF_DW:
608*df849ba3SYang Shi 			emit(A64_STR64(tmp, dst, tmp2), ctx);
609*df849ba3SYang Shi 			break;
610*df849ba3SYang Shi 		}
611*df849ba3SYang Shi 		break;
612e54bcde3SZi Shen Lim 
613e54bcde3SZi Shen Lim 	/* STX: *(size *)(dst + off) = src */
614e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_W:
615e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_H:
616e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_B:
617e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_DW:
618e54bcde3SZi Shen Lim 		ctx->tmp_used = 1;
619e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, off, ctx);
620e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
621e54bcde3SZi Shen Lim 		case BPF_W:
622e54bcde3SZi Shen Lim 			emit(A64_STR32(src, dst, tmp), ctx);
623e54bcde3SZi Shen Lim 			break;
624e54bcde3SZi Shen Lim 		case BPF_H:
625e54bcde3SZi Shen Lim 			emit(A64_STRH(src, dst, tmp), ctx);
626e54bcde3SZi Shen Lim 			break;
627e54bcde3SZi Shen Lim 		case BPF_B:
628e54bcde3SZi Shen Lim 			emit(A64_STRB(src, dst, tmp), ctx);
629e54bcde3SZi Shen Lim 			break;
630e54bcde3SZi Shen Lim 		case BPF_DW:
631e54bcde3SZi Shen Lim 			emit(A64_STR64(src, dst, tmp), ctx);
632e54bcde3SZi Shen Lim 			break;
633e54bcde3SZi Shen Lim 		}
634e54bcde3SZi Shen Lim 		break;
635e54bcde3SZi Shen Lim 	/* STX XADD: lock *(u32 *)(dst + off) += src */
636e54bcde3SZi Shen Lim 	case BPF_STX | BPF_XADD | BPF_W:
637e54bcde3SZi Shen Lim 	/* STX XADD: lock *(u64 *)(dst + off) += src */
638e54bcde3SZi Shen Lim 	case BPF_STX | BPF_XADD | BPF_DW:
639e54bcde3SZi Shen Lim 		goto notyet;
640e54bcde3SZi Shen Lim 
641e54bcde3SZi Shen Lim 	/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + imm)) */
642e54bcde3SZi Shen Lim 	case BPF_LD | BPF_ABS | BPF_W:
643e54bcde3SZi Shen Lim 	case BPF_LD | BPF_ABS | BPF_H:
644e54bcde3SZi Shen Lim 	case BPF_LD | BPF_ABS | BPF_B:
645e54bcde3SZi Shen Lim 	/* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */
646e54bcde3SZi Shen Lim 	case BPF_LD | BPF_IND | BPF_W:
647e54bcde3SZi Shen Lim 	case BPF_LD | BPF_IND | BPF_H:
648e54bcde3SZi Shen Lim 	case BPF_LD | BPF_IND | BPF_B:
649e54bcde3SZi Shen Lim 	{
650e54bcde3SZi Shen Lim 		const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */
651e54bcde3SZi Shen Lim 		const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */
652e54bcde3SZi Shen Lim 		const u8 fp = bpf2a64[BPF_REG_FP];
653e54bcde3SZi Shen Lim 		const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */
654e54bcde3SZi Shen Lim 		const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */
655e54bcde3SZi Shen Lim 		const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */
656e54bcde3SZi Shen Lim 		const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */
657e54bcde3SZi Shen Lim 		const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */
658e54bcde3SZi Shen Lim 		int size;
659e54bcde3SZi Shen Lim 
660e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r1, r6), ctx);
661e54bcde3SZi Shen Lim 		emit_a64_mov_i(0, r2, imm, ctx);
662e54bcde3SZi Shen Lim 		if (BPF_MODE(code) == BPF_IND)
663e54bcde3SZi Shen Lim 			emit(A64_ADD(0, r2, r2, src), ctx);
664e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
665e54bcde3SZi Shen Lim 		case BPF_W:
666e54bcde3SZi Shen Lim 			size = 4;
667e54bcde3SZi Shen Lim 			break;
668e54bcde3SZi Shen Lim 		case BPF_H:
669e54bcde3SZi Shen Lim 			size = 2;
670e54bcde3SZi Shen Lim 			break;
671e54bcde3SZi Shen Lim 		case BPF_B:
672e54bcde3SZi Shen Lim 			size = 1;
673e54bcde3SZi Shen Lim 			break;
674e54bcde3SZi Shen Lim 		default:
675e54bcde3SZi Shen Lim 			return -EINVAL;
676e54bcde3SZi Shen Lim 		}
677e54bcde3SZi Shen Lim 		emit_a64_mov_i64(r3, size, ctx);
678f4b16fceSZi Shen Lim 		emit(A64_SUB_I(1, r4, fp, STACK_SIZE), ctx);
679e54bcde3SZi Shen Lim 		emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx);
680e54bcde3SZi Shen Lim 		emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
681e54bcde3SZi Shen Lim 		emit(A64_MOV(1, A64_FP, A64_SP), ctx);
682e54bcde3SZi Shen Lim 		emit(A64_BLR(r5), ctx);
683e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
684e54bcde3SZi Shen Lim 		emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
685e54bcde3SZi Shen Lim 
686e54bcde3SZi Shen Lim 		jmp_offset = epilogue_offset(ctx);
687e54bcde3SZi Shen Lim 		check_imm19(jmp_offset);
688e54bcde3SZi Shen Lim 		emit(A64_CBZ(1, r0, jmp_offset), ctx);
689e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r5, r0), ctx);
690e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
691e54bcde3SZi Shen Lim 		case BPF_W:
692e54bcde3SZi Shen Lim 			emit(A64_LDR32(r0, r5, A64_ZR), ctx);
693e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN
694e54bcde3SZi Shen Lim 			emit(A64_REV32(0, r0, r0), ctx);
695e54bcde3SZi Shen Lim #endif
696e54bcde3SZi Shen Lim 			break;
697e54bcde3SZi Shen Lim 		case BPF_H:
698e54bcde3SZi Shen Lim 			emit(A64_LDRH(r0, r5, A64_ZR), ctx);
699e54bcde3SZi Shen Lim #ifndef CONFIG_CPU_BIG_ENDIAN
700e54bcde3SZi Shen Lim 			emit(A64_REV16(0, r0, r0), ctx);
701e54bcde3SZi Shen Lim #endif
702e54bcde3SZi Shen Lim 			break;
703e54bcde3SZi Shen Lim 		case BPF_B:
704e54bcde3SZi Shen Lim 			emit(A64_LDRB(r0, r5, A64_ZR), ctx);
705e54bcde3SZi Shen Lim 			break;
706e54bcde3SZi Shen Lim 		}
707e54bcde3SZi Shen Lim 		break;
708e54bcde3SZi Shen Lim 	}
709e54bcde3SZi Shen Lim notyet:
710e54bcde3SZi Shen Lim 		pr_info_once("*** NOT YET: opcode %02x ***\n", code);
711e54bcde3SZi Shen Lim 		return -EFAULT;
712e54bcde3SZi Shen Lim 
713e54bcde3SZi Shen Lim 	default:
714e54bcde3SZi Shen Lim 		pr_err_once("unknown opcode %02x\n", code);
715e54bcde3SZi Shen Lim 		return -EINVAL;
716e54bcde3SZi Shen Lim 	}
717e54bcde3SZi Shen Lim 
718e54bcde3SZi Shen Lim 	return 0;
719e54bcde3SZi Shen Lim }
720e54bcde3SZi Shen Lim 
721e54bcde3SZi Shen Lim static int build_body(struct jit_ctx *ctx)
722e54bcde3SZi Shen Lim {
723e54bcde3SZi Shen Lim 	const struct bpf_prog *prog = ctx->prog;
724e54bcde3SZi Shen Lim 	int i;
725e54bcde3SZi Shen Lim 
726e54bcde3SZi Shen Lim 	for (i = 0; i < prog->len; i++) {
727e54bcde3SZi Shen Lim 		const struct bpf_insn *insn = &prog->insnsi[i];
728e54bcde3SZi Shen Lim 		int ret;
729e54bcde3SZi Shen Lim 
7308eee539dSXi Wang 		ret = build_insn(insn, ctx);
7318eee539dSXi Wang 
732e54bcde3SZi Shen Lim 		if (ctx->image == NULL)
733e54bcde3SZi Shen Lim 			ctx->offset[i] = ctx->idx;
734e54bcde3SZi Shen Lim 
73530d3d94cSZi Shen Lim 		if (ret > 0) {
73630d3d94cSZi Shen Lim 			i++;
73730d3d94cSZi Shen Lim 			continue;
73830d3d94cSZi Shen Lim 		}
739e54bcde3SZi Shen Lim 		if (ret)
740e54bcde3SZi Shen Lim 			return ret;
741e54bcde3SZi Shen Lim 	}
742e54bcde3SZi Shen Lim 
743e54bcde3SZi Shen Lim 	return 0;
744e54bcde3SZi Shen Lim }
745e54bcde3SZi Shen Lim 
746e54bcde3SZi Shen Lim static inline void bpf_flush_icache(void *start, void *end)
747e54bcde3SZi Shen Lim {
748e54bcde3SZi Shen Lim 	flush_icache_range((unsigned long)start, (unsigned long)end);
749e54bcde3SZi Shen Lim }
750e54bcde3SZi Shen Lim 
751e54bcde3SZi Shen Lim void bpf_jit_compile(struct bpf_prog *prog)
752e54bcde3SZi Shen Lim {
753e54bcde3SZi Shen Lim 	/* Nothing to do here. We support Internal BPF. */
754e54bcde3SZi Shen Lim }
755e54bcde3SZi Shen Lim 
756e54bcde3SZi Shen Lim void bpf_int_jit_compile(struct bpf_prog *prog)
757e54bcde3SZi Shen Lim {
758b569c1c6SDaniel Borkmann 	struct bpf_binary_header *header;
759e54bcde3SZi Shen Lim 	struct jit_ctx ctx;
760e54bcde3SZi Shen Lim 	int image_size;
761b569c1c6SDaniel Borkmann 	u8 *image_ptr;
762e54bcde3SZi Shen Lim 
763e54bcde3SZi Shen Lim 	if (!bpf_jit_enable)
764e54bcde3SZi Shen Lim 		return;
765e54bcde3SZi Shen Lim 
766e54bcde3SZi Shen Lim 	if (!prog || !prog->len)
767e54bcde3SZi Shen Lim 		return;
768e54bcde3SZi Shen Lim 
769e54bcde3SZi Shen Lim 	memset(&ctx, 0, sizeof(ctx));
770e54bcde3SZi Shen Lim 	ctx.prog = prog;
771e54bcde3SZi Shen Lim 
772e54bcde3SZi Shen Lim 	ctx.offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
773e54bcde3SZi Shen Lim 	if (ctx.offset == NULL)
774e54bcde3SZi Shen Lim 		return;
775e54bcde3SZi Shen Lim 
776e54bcde3SZi Shen Lim 	/* 1. Initial fake pass to compute ctx->idx. */
777e54bcde3SZi Shen Lim 
77851c9fbb1SZi Shen Lim 	/* Fake pass to fill in ctx->offset and ctx->tmp_used. */
779e54bcde3SZi Shen Lim 	if (build_body(&ctx))
780e54bcde3SZi Shen Lim 		goto out;
781e54bcde3SZi Shen Lim 
782e54bcde3SZi Shen Lim 	build_prologue(&ctx);
78351c9fbb1SZi Shen Lim 
78451c9fbb1SZi Shen Lim 	ctx.epilogue_offset = ctx.idx;
785e54bcde3SZi Shen Lim 	build_epilogue(&ctx);
786e54bcde3SZi Shen Lim 
787e54bcde3SZi Shen Lim 	/* Now we know the actual image size. */
788e54bcde3SZi Shen Lim 	image_size = sizeof(u32) * ctx.idx;
789b569c1c6SDaniel Borkmann 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
790b569c1c6SDaniel Borkmann 				      sizeof(u32), jit_fill_hole);
791b569c1c6SDaniel Borkmann 	if (header == NULL)
792e54bcde3SZi Shen Lim 		goto out;
793e54bcde3SZi Shen Lim 
794e54bcde3SZi Shen Lim 	/* 2. Now, the actual pass. */
795e54bcde3SZi Shen Lim 
796b569c1c6SDaniel Borkmann 	ctx.image = (u32 *)image_ptr;
797e54bcde3SZi Shen Lim 	ctx.idx = 0;
798b569c1c6SDaniel Borkmann 
799e54bcde3SZi Shen Lim 	build_prologue(&ctx);
800e54bcde3SZi Shen Lim 
80160ef0494SDaniel Borkmann 	if (build_body(&ctx)) {
802b569c1c6SDaniel Borkmann 		bpf_jit_binary_free(header);
803e54bcde3SZi Shen Lim 		goto out;
80460ef0494SDaniel Borkmann 	}
805e54bcde3SZi Shen Lim 
806e54bcde3SZi Shen Lim 	build_epilogue(&ctx);
807e54bcde3SZi Shen Lim 
808e54bcde3SZi Shen Lim 	/* And we're done. */
809e54bcde3SZi Shen Lim 	if (bpf_jit_enable > 1)
810e54bcde3SZi Shen Lim 		bpf_jit_dump(prog->len, image_size, 2, ctx.image);
811e54bcde3SZi Shen Lim 
812c3d4c682SDaniel Borkmann 	bpf_flush_icache(header, ctx.image + ctx.idx);
813b569c1c6SDaniel Borkmann 
814b569c1c6SDaniel Borkmann 	set_memory_ro((unsigned long)header, header->pages);
815e54bcde3SZi Shen Lim 	prog->bpf_func = (void *)ctx.image;
816a91263d5SDaniel Borkmann 	prog->jited = 1;
817e54bcde3SZi Shen Lim out:
818e54bcde3SZi Shen Lim 	kfree(ctx.offset);
819e54bcde3SZi Shen Lim }
820e54bcde3SZi Shen Lim 
821e54bcde3SZi Shen Lim void bpf_jit_free(struct bpf_prog *prog)
822e54bcde3SZi Shen Lim {
823b569c1c6SDaniel Borkmann 	unsigned long addr = (unsigned long)prog->bpf_func & PAGE_MASK;
824b569c1c6SDaniel Borkmann 	struct bpf_binary_header *header = (void *)addr;
825e54bcde3SZi Shen Lim 
826b569c1c6SDaniel Borkmann 	if (!prog->jited)
827b569c1c6SDaniel Borkmann 		goto free_filter;
828b569c1c6SDaniel Borkmann 
829b569c1c6SDaniel Borkmann 	set_memory_rw(addr, header->pages);
830b569c1c6SDaniel Borkmann 	bpf_jit_binary_free(header);
831b569c1c6SDaniel Borkmann 
832b569c1c6SDaniel Borkmann free_filter:
833b569c1c6SDaniel Borkmann 	bpf_prog_unlock_free(prog);
834e54bcde3SZi Shen Lim }
835