xref: /linux/arch/arm64/net/bpf_jit_comp.c (revision dda7596c109fc382876118627e29db7607cde35d)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e54bcde3SZi Shen Lim /*
3e54bcde3SZi Shen Lim  * BPF JIT compiler for ARM64
4e54bcde3SZi Shen Lim  *
542ff712bSZi Shen Lim  * Copyright (C) 2014-2016 Zi Shen Lim <zlim.lnx@gmail.com>
6e54bcde3SZi Shen Lim  */
7e54bcde3SZi Shen Lim 
8e54bcde3SZi Shen Lim #define pr_fmt(fmt) "bpf_jit: " fmt
9e54bcde3SZi Shen Lim 
1080083428SJean-Philippe Brucker #include <linux/bitfield.h>
11ddb55992SZi Shen Lim #include <linux/bpf.h>
12e54bcde3SZi Shen Lim #include <linux/filter.h>
13e54bcde3SZi Shen Lim #include <linux/printk.h>
14e54bcde3SZi Shen Lim #include <linux/slab.h>
15b569c1c6SDaniel Borkmann 
16d6e2cc56SMark Rutland #include <asm/asm-extable.h>
17e54bcde3SZi Shen Lim #include <asm/byteorder.h>
18e54bcde3SZi Shen Lim #include <asm/cacheflush.h>
19b569c1c6SDaniel Borkmann #include <asm/debug-monitors.h>
203e00e39dSMark Rutland #include <asm/insn.h>
21d4bbc30bSLaura Abbott #include <asm/set_memory.h>
22e54bcde3SZi Shen Lim 
23e54bcde3SZi Shen Lim #include "bpf_jit.h"
24e54bcde3SZi Shen Lim 
2526eb042eSDaniel Borkmann #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
2626eb042eSDaniel Borkmann #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
27ddb55992SZi Shen Lim #define TCALL_CNT (MAX_BPF_JIT_REG + 2)
287005cadeSDaniel Borkmann #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
29e54bcde3SZi Shen Lim 
30e54bcde3SZi Shen Lim /* Map BPF registers to A64 registers */
31e54bcde3SZi Shen Lim static const int bpf2a64[] = {
32e54bcde3SZi Shen Lim 	/* return value from in-kernel function, and exit value from eBPF */
33e54bcde3SZi Shen Lim 	[BPF_REG_0] = A64_R(7),
34e54bcde3SZi Shen Lim 	/* arguments from eBPF program to in-kernel function */
35e54bcde3SZi Shen Lim 	[BPF_REG_1] = A64_R(0),
36e54bcde3SZi Shen Lim 	[BPF_REG_2] = A64_R(1),
37e54bcde3SZi Shen Lim 	[BPF_REG_3] = A64_R(2),
38e54bcde3SZi Shen Lim 	[BPF_REG_4] = A64_R(3),
39e54bcde3SZi Shen Lim 	[BPF_REG_5] = A64_R(4),
40e54bcde3SZi Shen Lim 	/* callee saved registers that in-kernel function will preserve */
41e54bcde3SZi Shen Lim 	[BPF_REG_6] = A64_R(19),
42e54bcde3SZi Shen Lim 	[BPF_REG_7] = A64_R(20),
43e54bcde3SZi Shen Lim 	[BPF_REG_8] = A64_R(21),
44e54bcde3SZi Shen Lim 	[BPF_REG_9] = A64_R(22),
45e54bcde3SZi Shen Lim 	/* read-only frame pointer to access stack */
46ec0738dbSYang Shi 	[BPF_REG_FP] = A64_R(25),
4706edc59cSChristoph Hellwig 	/* temporary registers for BPF JIT */
484c1cd4fdSYang Shi 	[TMP_REG_1] = A64_R(10),
494c1cd4fdSYang Shi 	[TMP_REG_2] = A64_R(11),
507005cadeSDaniel Borkmann 	[TMP_REG_3] = A64_R(12),
51ddb55992SZi Shen Lim 	/* tail_call_cnt */
52ddb55992SZi Shen Lim 	[TCALL_CNT] = A64_R(26),
5326eb042eSDaniel Borkmann 	/* temporary register for blinding constants */
5426eb042eSDaniel Borkmann 	[BPF_REG_AX] = A64_R(9),
55e54bcde3SZi Shen Lim };
56e54bcde3SZi Shen Lim 
57e54bcde3SZi Shen Lim struct jit_ctx {
58e54bcde3SZi Shen Lim 	const struct bpf_prog *prog;
59e54bcde3SZi Shen Lim 	int idx;
6051c9fbb1SZi Shen Lim 	int epilogue_offset;
61e54bcde3SZi Shen Lim 	int *offset;
6280083428SJean-Philippe Brucker 	int exentry_idx;
63425e1ed7SLuc Van Oostenryck 	__le32 *image;
64f1c9eed7SDaniel Borkmann 	u32 stack_size;
65e54bcde3SZi Shen Lim };
66e54bcde3SZi Shen Lim 
67e54bcde3SZi Shen Lim static inline void emit(const u32 insn, struct jit_ctx *ctx)
68e54bcde3SZi Shen Lim {
69e54bcde3SZi Shen Lim 	if (ctx->image != NULL)
70e54bcde3SZi Shen Lim 		ctx->image[ctx->idx] = cpu_to_le32(insn);
71e54bcde3SZi Shen Lim 
72e54bcde3SZi Shen Lim 	ctx->idx++;
73e54bcde3SZi Shen Lim }
74e54bcde3SZi Shen Lim 
75e54bcde3SZi Shen Lim static inline void emit_a64_mov_i(const int is64, const int reg,
76e54bcde3SZi Shen Lim 				  const s32 val, struct jit_ctx *ctx)
77e54bcde3SZi Shen Lim {
78e54bcde3SZi Shen Lim 	u16 hi = val >> 16;
79e54bcde3SZi Shen Lim 	u16 lo = val & 0xffff;
80e54bcde3SZi Shen Lim 
81e54bcde3SZi Shen Lim 	if (hi & 0x8000) {
82e54bcde3SZi Shen Lim 		if (hi == 0xffff) {
83e54bcde3SZi Shen Lim 			emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
84e54bcde3SZi Shen Lim 		} else {
85e54bcde3SZi Shen Lim 			emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
866d2eea6fSDaniel Borkmann 			if (lo != 0xffff)
87e54bcde3SZi Shen Lim 				emit(A64_MOVK(is64, reg, lo, 0), ctx);
88e54bcde3SZi Shen Lim 		}
89e54bcde3SZi Shen Lim 	} else {
90e54bcde3SZi Shen Lim 		emit(A64_MOVZ(is64, reg, lo, 0), ctx);
91e54bcde3SZi Shen Lim 		if (hi)
92e54bcde3SZi Shen Lim 			emit(A64_MOVK(is64, reg, hi, 16), ctx);
93e54bcde3SZi Shen Lim 	}
94e54bcde3SZi Shen Lim }
95e54bcde3SZi Shen Lim 
966d2eea6fSDaniel Borkmann static int i64_i16_blocks(const u64 val, bool inverse)
976d2eea6fSDaniel Borkmann {
986d2eea6fSDaniel Borkmann 	return (((val >>  0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
996d2eea6fSDaniel Borkmann 	       (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
1006d2eea6fSDaniel Borkmann 	       (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
1016d2eea6fSDaniel Borkmann 	       (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
1026d2eea6fSDaniel Borkmann }
1036d2eea6fSDaniel Borkmann 
1046d2eea6fSDaniel Borkmann static inline void emit_a64_mov_i64(const int reg, const u64 val,
1056d2eea6fSDaniel Borkmann 				    struct jit_ctx *ctx)
1066d2eea6fSDaniel Borkmann {
1076d2eea6fSDaniel Borkmann 	u64 nrm_tmp = val, rev_tmp = ~val;
1086d2eea6fSDaniel Borkmann 	bool inverse;
1096d2eea6fSDaniel Borkmann 	int shift;
1106d2eea6fSDaniel Borkmann 
1116d2eea6fSDaniel Borkmann 	if (!(nrm_tmp >> 32))
1126d2eea6fSDaniel Borkmann 		return emit_a64_mov_i(0, reg, (u32)val, ctx);
1136d2eea6fSDaniel Borkmann 
1146d2eea6fSDaniel Borkmann 	inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
1156d2eea6fSDaniel Borkmann 	shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
1166d2eea6fSDaniel Borkmann 					  (fls64(nrm_tmp) - 1)), 16), 0);
1176d2eea6fSDaniel Borkmann 	if (inverse)
1186d2eea6fSDaniel Borkmann 		emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
1196d2eea6fSDaniel Borkmann 	else
1206d2eea6fSDaniel Borkmann 		emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
1216d2eea6fSDaniel Borkmann 	shift -= 16;
1226d2eea6fSDaniel Borkmann 	while (shift >= 0) {
1236d2eea6fSDaniel Borkmann 		if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
1246d2eea6fSDaniel Borkmann 			emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
1256d2eea6fSDaniel Borkmann 		shift -= 16;
1266d2eea6fSDaniel Borkmann 	}
1276d2eea6fSDaniel Borkmann }
1286d2eea6fSDaniel Borkmann 
1296d2eea6fSDaniel Borkmann /*
130cc2b8ed1SArd Biesheuvel  * Kernel addresses in the vmalloc space use at most 48 bits, and the
131cc2b8ed1SArd Biesheuvel  * remaining bits are guaranteed to be 0x1. So we can compose the address
132cc2b8ed1SArd Biesheuvel  * with a fixed length movn/movk/movk sequence.
1336d2eea6fSDaniel Borkmann  */
1346d2eea6fSDaniel Borkmann static inline void emit_addr_mov_i64(const int reg, const u64 val,
1356d2eea6fSDaniel Borkmann 				     struct jit_ctx *ctx)
1366d2eea6fSDaniel Borkmann {
1376d2eea6fSDaniel Borkmann 	u64 tmp = val;
1386d2eea6fSDaniel Borkmann 	int shift = 0;
1396d2eea6fSDaniel Borkmann 
140cc2b8ed1SArd Biesheuvel 	emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx);
141cc2b8ed1SArd Biesheuvel 	while (shift < 32) {
1426d2eea6fSDaniel Borkmann 		tmp >>= 16;
1436d2eea6fSDaniel Borkmann 		shift += 16;
1446d2eea6fSDaniel Borkmann 		emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
1456d2eea6fSDaniel Borkmann 	}
1466d2eea6fSDaniel Borkmann }
1476d2eea6fSDaniel Borkmann 
14832f6865cSIlias Apalodimas static inline int bpf2a64_offset(int bpf_insn, int off,
149e54bcde3SZi Shen Lim 				 const struct jit_ctx *ctx)
150e54bcde3SZi Shen Lim {
15132f6865cSIlias Apalodimas 	/* BPF JMP offset is relative to the next instruction */
15232f6865cSIlias Apalodimas 	bpf_insn++;
15332f6865cSIlias Apalodimas 	/*
15432f6865cSIlias Apalodimas 	 * Whereas arm64 branch instructions encode the offset
15532f6865cSIlias Apalodimas 	 * from the branch itself, so we must subtract 1 from the
15632f6865cSIlias Apalodimas 	 * instruction offset.
15732f6865cSIlias Apalodimas 	 */
15832f6865cSIlias Apalodimas 	return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1);
159e54bcde3SZi Shen Lim }
160e54bcde3SZi Shen Lim 
161b569c1c6SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
162b569c1c6SDaniel Borkmann {
163425e1ed7SLuc Van Oostenryck 	__le32 *ptr;
164b569c1c6SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
165b569c1c6SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
166b569c1c6SDaniel Borkmann 		*ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
167b569c1c6SDaniel Borkmann }
168b569c1c6SDaniel Borkmann 
169e54bcde3SZi Shen Lim static inline int epilogue_offset(const struct jit_ctx *ctx)
170e54bcde3SZi Shen Lim {
17151c9fbb1SZi Shen Lim 	int to = ctx->epilogue_offset;
17251c9fbb1SZi Shen Lim 	int from = ctx->idx;
173e54bcde3SZi Shen Lim 
174e54bcde3SZi Shen Lim 	return to - from;
175e54bcde3SZi Shen Lim }
176e54bcde3SZi Shen Lim 
177fd868f14SLuke Nelson static bool is_addsub_imm(u32 imm)
178fd868f14SLuke Nelson {
179fd868f14SLuke Nelson 	/* Either imm12 or shifted imm12. */
180fd868f14SLuke Nelson 	return !(imm & ~0xfff) || !(imm & ~0xfff000);
181fd868f14SLuke Nelson }
182fd868f14SLuke Nelson 
183a2284d91SDaniel Borkmann /* Tail call offset to jump into */
184fa76cfe6SMark Brown #if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
185fa76cfe6SMark Brown #define PROLOGUE_OFFSET 8
186fa76cfe6SMark Brown #else
187a2284d91SDaniel Borkmann #define PROLOGUE_OFFSET 7
188fa76cfe6SMark Brown #endif
189ddb55992SZi Shen Lim 
19056ea6a8bSDaniel Borkmann static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf)
191e54bcde3SZi Shen Lim {
192f1c9eed7SDaniel Borkmann 	const struct bpf_prog *prog = ctx->prog;
193e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
194e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
195e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
196e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
197e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
198ddb55992SZi Shen Lim 	const u8 tcc = bpf2a64[TCALL_CNT];
199ddb55992SZi Shen Lim 	const int idx0 = ctx->idx;
200ddb55992SZi Shen Lim 	int cur_offset;
201e54bcde3SZi Shen Lim 
202ec0738dbSYang Shi 	/*
203ec0738dbSYang Shi 	 * BPF prog stack layout
204ec0738dbSYang Shi 	 *
205ec0738dbSYang Shi 	 *                         high
206ec0738dbSYang Shi 	 * original A64_SP =>   0:+-----+ BPF prologue
207ec0738dbSYang Shi 	 *                        |FP/LR|
208ec0738dbSYang Shi 	 * current A64_FP =>  -16:+-----+
209ec0738dbSYang Shi 	 *                        | ... | callee saved registers
2104c1cd4fdSYang Shi 	 * BPF fp register => -64:+-----+ <= (BPF_FP)
211ec0738dbSYang Shi 	 *                        |     |
212ec0738dbSYang Shi 	 *                        | ... | BPF prog stack
213ec0738dbSYang Shi 	 *                        |     |
214f1c9eed7SDaniel Borkmann 	 *                        +-----+ <= (BPF_FP - prog->aux->stack_depth)
21509ece3d0SDaniel Borkmann 	 *                        |RSVD | padding
216f1c9eed7SDaniel Borkmann 	 * current A64_SP =>      +-----+ <= (BPF_FP - ctx->stack_size)
217ec0738dbSYang Shi 	 *                        |     |
218ec0738dbSYang Shi 	 *                        | ... | Function call stack
219ec0738dbSYang Shi 	 *                        |     |
220ec0738dbSYang Shi 	 *                        +-----+
221ec0738dbSYang Shi 	 *                          low
222ec0738dbSYang Shi 	 *
223ec0738dbSYang Shi 	 */
224ec0738dbSYang Shi 
225fa76cfe6SMark Brown 	/* BTI landing pad */
226fa76cfe6SMark Brown 	if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
227fa76cfe6SMark Brown 		emit(A64_BTI_C, ctx);
228fa76cfe6SMark Brown 
229ec0738dbSYang Shi 	/* Save FP and LR registers to stay align with ARM64 AAPCS */
230ec0738dbSYang Shi 	emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
231ec0738dbSYang Shi 	emit(A64_MOV(1, A64_FP, A64_SP), ctx);
232ec0738dbSYang Shi 
233ddb55992SZi Shen Lim 	/* Save callee-saved registers */
234e54bcde3SZi Shen Lim 	emit(A64_PUSH(r6, r7, A64_SP), ctx);
235e54bcde3SZi Shen Lim 	emit(A64_PUSH(r8, r9, A64_SP), ctx);
236ddb55992SZi Shen Lim 	emit(A64_PUSH(fp, tcc, A64_SP), ctx);
237e54bcde3SZi Shen Lim 
238ddb55992SZi Shen Lim 	/* Set up BPF prog stack base register */
239e54bcde3SZi Shen Lim 	emit(A64_MOV(1, fp, A64_SP), ctx);
240e54bcde3SZi Shen Lim 
24156ea6a8bSDaniel Borkmann 	if (!ebpf_from_cbpf) {
242ddb55992SZi Shen Lim 		/* Initialize tail_call_cnt */
243ddb55992SZi Shen Lim 		emit(A64_MOVZ(1, tcc, 0, 0), ctx);
244ddb55992SZi Shen Lim 
245ddb55992SZi Shen Lim 		cur_offset = ctx->idx - idx0;
246ddb55992SZi Shen Lim 		if (cur_offset != PROLOGUE_OFFSET) {
247ddb55992SZi Shen Lim 			pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
248ddb55992SZi Shen Lim 				    cur_offset, PROLOGUE_OFFSET);
249ddb55992SZi Shen Lim 			return -1;
250ddb55992SZi Shen Lim 		}
251fa76cfe6SMark Brown 
252fa76cfe6SMark Brown 		/* BTI landing pad for the tail call, done with a BR */
253fa76cfe6SMark Brown 		if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
254fa76cfe6SMark Brown 			emit(A64_BTI_J, ctx);
25556ea6a8bSDaniel Borkmann 	}
256a2284d91SDaniel Borkmann 
2573f287098STiezhu Yang 	/* Stack must be multiples of 16B */
2583f287098STiezhu Yang 	ctx->stack_size = round_up(prog->aux->stack_depth, 16);
259a2284d91SDaniel Borkmann 
260a2284d91SDaniel Borkmann 	/* Set up function call stack */
261a2284d91SDaniel Borkmann 	emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
262ddb55992SZi Shen Lim 	return 0;
263ddb55992SZi Shen Lim }
264ddb55992SZi Shen Lim 
265ddb55992SZi Shen Lim static int out_offset = -1; /* initialized on the first pass of build_body() */
266ddb55992SZi Shen Lim static int emit_bpf_tail_call(struct jit_ctx *ctx)
267ddb55992SZi Shen Lim {
268ddb55992SZi Shen Lim 	/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
269ddb55992SZi Shen Lim 	const u8 r2 = bpf2a64[BPF_REG_2];
270ddb55992SZi Shen Lim 	const u8 r3 = bpf2a64[BPF_REG_3];
271ddb55992SZi Shen Lim 
272ddb55992SZi Shen Lim 	const u8 tmp = bpf2a64[TMP_REG_1];
273ddb55992SZi Shen Lim 	const u8 prg = bpf2a64[TMP_REG_2];
274ddb55992SZi Shen Lim 	const u8 tcc = bpf2a64[TCALL_CNT];
275ddb55992SZi Shen Lim 	const int idx0 = ctx->idx;
276ddb55992SZi Shen Lim #define cur_offset (ctx->idx - idx0)
277ddb55992SZi Shen Lim #define jmp_offset (out_offset - (cur_offset))
278ddb55992SZi Shen Lim 	size_t off;
279ddb55992SZi Shen Lim 
280ddb55992SZi Shen Lim 	/* if (index >= array->map.max_entries)
281ddb55992SZi Shen Lim 	 *     goto out;
282ddb55992SZi Shen Lim 	 */
283ddb55992SZi Shen Lim 	off = offsetof(struct bpf_array, map.max_entries);
284ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, off, ctx);
285ddb55992SZi Shen Lim 	emit(A64_LDR32(tmp, r2, tmp), ctx);
28616338a9bSDaniel Borkmann 	emit(A64_MOV(0, r3, r3), ctx);
287ddb55992SZi Shen Lim 	emit(A64_CMP(0, r3, tmp), ctx);
28816338a9bSDaniel Borkmann 	emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
289ddb55992SZi Shen Lim 
290ebf7f6f0STiezhu Yang 	/*
291ebf7f6f0STiezhu Yang 	 * if (tail_call_cnt >= MAX_TAIL_CALL_CNT)
292ddb55992SZi Shen Lim 	 *     goto out;
293ddb55992SZi Shen Lim 	 * tail_call_cnt++;
294ddb55992SZi Shen Lim 	 */
295ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
296ddb55992SZi Shen Lim 	emit(A64_CMP(1, tcc, tmp), ctx);
297ebf7f6f0STiezhu Yang 	emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
298ddb55992SZi Shen Lim 	emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
299ddb55992SZi Shen Lim 
300ddb55992SZi Shen Lim 	/* prog = array->ptrs[index];
301ddb55992SZi Shen Lim 	 * if (prog == NULL)
302ddb55992SZi Shen Lim 	 *     goto out;
303ddb55992SZi Shen Lim 	 */
304ddb55992SZi Shen Lim 	off = offsetof(struct bpf_array, ptrs);
305ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, off, ctx);
306d8b54110SDaniel Borkmann 	emit(A64_ADD(1, tmp, r2, tmp), ctx);
307d8b54110SDaniel Borkmann 	emit(A64_LSL(1, prg, r3, 3), ctx);
308d8b54110SDaniel Borkmann 	emit(A64_LDR64(prg, tmp, prg), ctx);
309ddb55992SZi Shen Lim 	emit(A64_CBZ(1, prg, jmp_offset), ctx);
310ddb55992SZi Shen Lim 
311a2284d91SDaniel Borkmann 	/* goto *(prog->bpf_func + prologue_offset); */
312ddb55992SZi Shen Lim 	off = offsetof(struct bpf_prog, bpf_func);
313ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, off, ctx);
314ddb55992SZi Shen Lim 	emit(A64_LDR64(tmp, prg, tmp), ctx);
315ddb55992SZi Shen Lim 	emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
316a2284d91SDaniel Borkmann 	emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
317ddb55992SZi Shen Lim 	emit(A64_BR(tmp), ctx);
318ddb55992SZi Shen Lim 
319ddb55992SZi Shen Lim 	/* out: */
320ddb55992SZi Shen Lim 	if (out_offset == -1)
321ddb55992SZi Shen Lim 		out_offset = cur_offset;
322ddb55992SZi Shen Lim 	if (cur_offset != out_offset) {
323ddb55992SZi Shen Lim 		pr_err_once("tail_call out_offset = %d, expected %d!\n",
324ddb55992SZi Shen Lim 			    cur_offset, out_offset);
325ddb55992SZi Shen Lim 		return -1;
326ddb55992SZi Shen Lim 	}
327ddb55992SZi Shen Lim 	return 0;
328ddb55992SZi Shen Lim #undef cur_offset
329ddb55992SZi Shen Lim #undef jmp_offset
330e54bcde3SZi Shen Lim }
331e54bcde3SZi Shen Lim 
332e54bcde3SZi Shen Lim static void build_epilogue(struct jit_ctx *ctx)
333e54bcde3SZi Shen Lim {
334e54bcde3SZi Shen Lim 	const u8 r0 = bpf2a64[BPF_REG_0];
335e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
336e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
337e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
338e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
339e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
340e54bcde3SZi Shen Lim 
341e54bcde3SZi Shen Lim 	/* We're done with BPF stack */
342f1c9eed7SDaniel Borkmann 	emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
343e54bcde3SZi Shen Lim 
344ec0738dbSYang Shi 	/* Restore fs (x25) and x26 */
345ec0738dbSYang Shi 	emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
346ec0738dbSYang Shi 
347e54bcde3SZi Shen Lim 	/* Restore callee-saved register */
348e54bcde3SZi Shen Lim 	emit(A64_POP(r8, r9, A64_SP), ctx);
349e54bcde3SZi Shen Lim 	emit(A64_POP(r6, r7, A64_SP), ctx);
350e54bcde3SZi Shen Lim 
351ec0738dbSYang Shi 	/* Restore FP/LR registers */
352ec0738dbSYang Shi 	emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
353e54bcde3SZi Shen Lim 
354e54bcde3SZi Shen Lim 	/* Set return value */
355e54bcde3SZi Shen Lim 	emit(A64_MOV(1, A64_R(0), r0), ctx);
356e54bcde3SZi Shen Lim 
357e54bcde3SZi Shen Lim 	emit(A64_RET(A64_LR), ctx);
358e54bcde3SZi Shen Lim }
359e54bcde3SZi Shen Lim 
36080083428SJean-Philippe Brucker #define BPF_FIXUP_OFFSET_MASK	GENMASK(26, 0)
36180083428SJean-Philippe Brucker #define BPF_FIXUP_REG_MASK	GENMASK(31, 27)
36280083428SJean-Philippe Brucker 
363d6e2cc56SMark Rutland bool ex_handler_bpf(const struct exception_table_entry *ex,
36480083428SJean-Philippe Brucker 		    struct pt_regs *regs)
36580083428SJean-Philippe Brucker {
36680083428SJean-Philippe Brucker 	off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
36780083428SJean-Philippe Brucker 	int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
36880083428SJean-Philippe Brucker 
36980083428SJean-Philippe Brucker 	regs->regs[dst_reg] = 0;
37080083428SJean-Philippe Brucker 	regs->pc = (unsigned long)&ex->fixup - offset;
371e8c328d7SMark Rutland 	return true;
37280083428SJean-Philippe Brucker }
37380083428SJean-Philippe Brucker 
37480083428SJean-Philippe Brucker /* For accesses to BTF pointers, add an entry to the exception table */
37580083428SJean-Philippe Brucker static int add_exception_handler(const struct bpf_insn *insn,
37680083428SJean-Philippe Brucker 				 struct jit_ctx *ctx,
37780083428SJean-Philippe Brucker 				 int dst_reg)
37880083428SJean-Philippe Brucker {
37980083428SJean-Philippe Brucker 	off_t offset;
38080083428SJean-Philippe Brucker 	unsigned long pc;
38180083428SJean-Philippe Brucker 	struct exception_table_entry *ex;
38280083428SJean-Philippe Brucker 
38380083428SJean-Philippe Brucker 	if (!ctx->image)
38480083428SJean-Philippe Brucker 		/* First pass */
38580083428SJean-Philippe Brucker 		return 0;
38680083428SJean-Philippe Brucker 
38780083428SJean-Philippe Brucker 	if (BPF_MODE(insn->code) != BPF_PROBE_MEM)
38880083428SJean-Philippe Brucker 		return 0;
38980083428SJean-Philippe Brucker 
39080083428SJean-Philippe Brucker 	if (!ctx->prog->aux->extable ||
39180083428SJean-Philippe Brucker 	    WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
39280083428SJean-Philippe Brucker 		return -EINVAL;
39380083428SJean-Philippe Brucker 
39480083428SJean-Philippe Brucker 	ex = &ctx->prog->aux->extable[ctx->exentry_idx];
39580083428SJean-Philippe Brucker 	pc = (unsigned long)&ctx->image[ctx->idx - 1];
39680083428SJean-Philippe Brucker 
39780083428SJean-Philippe Brucker 	offset = pc - (long)&ex->insn;
39880083428SJean-Philippe Brucker 	if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
39980083428SJean-Philippe Brucker 		return -ERANGE;
40080083428SJean-Philippe Brucker 	ex->insn = offset;
40180083428SJean-Philippe Brucker 
40280083428SJean-Philippe Brucker 	/*
40380083428SJean-Philippe Brucker 	 * Since the extable follows the program, the fixup offset is always
40480083428SJean-Philippe Brucker 	 * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
40580083428SJean-Philippe Brucker 	 * to keep things simple, and put the destination register in the upper
40680083428SJean-Philippe Brucker 	 * bits. We don't need to worry about buildtime or runtime sort
40780083428SJean-Philippe Brucker 	 * modifying the upper bits because the table is already sorted, and
40880083428SJean-Philippe Brucker 	 * isn't part of the main exception table.
40980083428SJean-Philippe Brucker 	 */
41080083428SJean-Philippe Brucker 	offset = (long)&ex->fixup - (pc + AARCH64_INSN_SIZE);
41180083428SJean-Philippe Brucker 	if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, offset))
41280083428SJean-Philippe Brucker 		return -ERANGE;
41380083428SJean-Philippe Brucker 
41480083428SJean-Philippe Brucker 	ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) |
41580083428SJean-Philippe Brucker 		    FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
41680083428SJean-Philippe Brucker 
417d6e2cc56SMark Rutland 	ex->type = EX_TYPE_BPF;
418d6e2cc56SMark Rutland 
41980083428SJean-Philippe Brucker 	ctx->exentry_idx++;
42080083428SJean-Philippe Brucker 	return 0;
42180083428SJean-Philippe Brucker }
42280083428SJean-Philippe Brucker 
42330d3d94cSZi Shen Lim /* JITs an eBPF instruction.
42430d3d94cSZi Shen Lim  * Returns:
42530d3d94cSZi Shen Lim  * 0  - successfully JITed an 8-byte eBPF instruction.
42630d3d94cSZi Shen Lim  * >0 - successfully JITed a 16-byte eBPF instruction.
42730d3d94cSZi Shen Lim  * <0 - failed to JIT.
42830d3d94cSZi Shen Lim  */
4298c11ea5cSDaniel Borkmann static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
4308c11ea5cSDaniel Borkmann 		      bool extra_pass)
431e54bcde3SZi Shen Lim {
432e54bcde3SZi Shen Lim 	const u8 code = insn->code;
433e54bcde3SZi Shen Lim 	const u8 dst = bpf2a64[insn->dst_reg];
434e54bcde3SZi Shen Lim 	const u8 src = bpf2a64[insn->src_reg];
435e54bcde3SZi Shen Lim 	const u8 tmp = bpf2a64[TMP_REG_1];
436e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
4377005cadeSDaniel Borkmann 	const u8 tmp3 = bpf2a64[TMP_REG_3];
438e54bcde3SZi Shen Lim 	const s16 off = insn->off;
439e54bcde3SZi Shen Lim 	const s32 imm = insn->imm;
440e54bcde3SZi Shen Lim 	const int i = insn - ctx->prog->insnsi;
441654b65a0SJiong Wang 	const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
442654b65a0SJiong Wang 			  BPF_CLASS(code) == BPF_JMP;
44385f68fe8SDaniel Borkmann 	const bool isdw = BPF_SIZE(code) == BPF_DW;
44434b8ab09SDaniel Borkmann 	u8 jmp_cond, reg;
445e54bcde3SZi Shen Lim 	s32 jmp_offset;
446fd49591cSLuke Nelson 	u32 a64_insn;
44780083428SJean-Philippe Brucker 	int ret;
448e54bcde3SZi Shen Lim 
449251599e1SZi Shen Lim #define check_imm(bits, imm) do {				\
450251599e1SZi Shen Lim 	if ((((imm) > 0) && ((imm) >> (bits))) ||		\
451251599e1SZi Shen Lim 	    (((imm) < 0) && (~(imm) >> (bits)))) {		\
452251599e1SZi Shen Lim 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
453251599e1SZi Shen Lim 			i, imm, imm);				\
454251599e1SZi Shen Lim 		return -EINVAL;					\
455251599e1SZi Shen Lim 	}							\
456251599e1SZi Shen Lim } while (0)
457251599e1SZi Shen Lim #define check_imm19(imm) check_imm(19, imm)
458251599e1SZi Shen Lim #define check_imm26(imm) check_imm(26, imm)
459251599e1SZi Shen Lim 
460e54bcde3SZi Shen Lim 	switch (code) {
461e54bcde3SZi Shen Lim 	/* dst = src */
462e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_X:
463e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_X:
464e54bcde3SZi Shen Lim 		emit(A64_MOV(is64, dst, src), ctx);
465e54bcde3SZi Shen Lim 		break;
466e54bcde3SZi Shen Lim 	/* dst = dst OP src */
467e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_X:
468e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_X:
469e54bcde3SZi Shen Lim 		emit(A64_ADD(is64, dst, dst, src), ctx);
470e54bcde3SZi Shen Lim 		break;
471e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_X:
472e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_X:
473e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, src), ctx);
474e54bcde3SZi Shen Lim 		break;
475e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_X:
476e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_X:
477e54bcde3SZi Shen Lim 		emit(A64_AND(is64, dst, dst, src), ctx);
478e54bcde3SZi Shen Lim 		break;
479e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_X:
480e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_X:
481e54bcde3SZi Shen Lim 		emit(A64_ORR(is64, dst, dst, src), ctx);
482e54bcde3SZi Shen Lim 		break;
483e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_X:
484e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_X:
485e54bcde3SZi Shen Lim 		emit(A64_EOR(is64, dst, dst, src), ctx);
486e54bcde3SZi Shen Lim 		break;
487e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_X:
488e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_X:
489e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, src), ctx);
490e54bcde3SZi Shen Lim 		break;
491e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_X:
492e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_X:
493e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, dst, dst, src), ctx);
494e54bcde3SZi Shen Lim 		break;
495119220d8STiezhu Yang 	case BPF_ALU | BPF_MOD | BPF_X:
496119220d8STiezhu Yang 	case BPF_ALU64 | BPF_MOD | BPF_X:
497e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, tmp, dst, src), ctx);
498504792e0SJerin Jacob 		emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
499e54bcde3SZi Shen Lim 		break;
500d65a634aSZi Shen Lim 	case BPF_ALU | BPF_LSH | BPF_X:
501d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_LSH | BPF_X:
502d65a634aSZi Shen Lim 		emit(A64_LSLV(is64, dst, dst, src), ctx);
503d65a634aSZi Shen Lim 		break;
504d65a634aSZi Shen Lim 	case BPF_ALU | BPF_RSH | BPF_X:
505d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_RSH | BPF_X:
506d65a634aSZi Shen Lim 		emit(A64_LSRV(is64, dst, dst, src), ctx);
507d65a634aSZi Shen Lim 		break;
508d65a634aSZi Shen Lim 	case BPF_ALU | BPF_ARSH | BPF_X:
509d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_ARSH | BPF_X:
510d65a634aSZi Shen Lim 		emit(A64_ASRV(is64, dst, dst, src), ctx);
511d65a634aSZi Shen Lim 		break;
512e54bcde3SZi Shen Lim 	/* dst = -dst */
513e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_NEG:
514e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_NEG:
515e54bcde3SZi Shen Lim 		emit(A64_NEG(is64, dst, dst), ctx);
516e54bcde3SZi Shen Lim 		break;
517e54bcde3SZi Shen Lim 	/* dst = BSWAP##imm(dst) */
518e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_LE:
519e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_BE:
520e54bcde3SZi Shen Lim #ifdef CONFIG_CPU_BIG_ENDIAN
521e54bcde3SZi Shen Lim 		if (BPF_SRC(code) == BPF_FROM_BE)
522d63903bbSXi Wang 			goto emit_bswap_uxt;
523e54bcde3SZi Shen Lim #else /* !CONFIG_CPU_BIG_ENDIAN */
524e54bcde3SZi Shen Lim 		if (BPF_SRC(code) == BPF_FROM_LE)
525d63903bbSXi Wang 			goto emit_bswap_uxt;
526e54bcde3SZi Shen Lim #endif
527e54bcde3SZi Shen Lim 		switch (imm) {
528e54bcde3SZi Shen Lim 		case 16:
529e54bcde3SZi Shen Lim 			emit(A64_REV16(is64, dst, dst), ctx);
530d63903bbSXi Wang 			/* zero-extend 16 bits into 64 bits */
531d63903bbSXi Wang 			emit(A64_UXTH(is64, dst, dst), ctx);
532e54bcde3SZi Shen Lim 			break;
533e54bcde3SZi Shen Lim 		case 32:
534e54bcde3SZi Shen Lim 			emit(A64_REV32(is64, dst, dst), ctx);
535d63903bbSXi Wang 			/* upper 32 bits already cleared */
536e54bcde3SZi Shen Lim 			break;
537e54bcde3SZi Shen Lim 		case 64:
538e54bcde3SZi Shen Lim 			emit(A64_REV64(dst, dst), ctx);
539e54bcde3SZi Shen Lim 			break;
540e54bcde3SZi Shen Lim 		}
541e54bcde3SZi Shen Lim 		break;
542d63903bbSXi Wang emit_bswap_uxt:
543d63903bbSXi Wang 		switch (imm) {
544d63903bbSXi Wang 		case 16:
545d63903bbSXi Wang 			/* zero-extend 16 bits into 64 bits */
546d63903bbSXi Wang 			emit(A64_UXTH(is64, dst, dst), ctx);
547d63903bbSXi Wang 			break;
548d63903bbSXi Wang 		case 32:
549d63903bbSXi Wang 			/* zero-extend 32 bits into 64 bits */
550d63903bbSXi Wang 			emit(A64_UXTW(is64, dst, dst), ctx);
551d63903bbSXi Wang 			break;
552d63903bbSXi Wang 		case 64:
553d63903bbSXi Wang 			/* nop */
554d63903bbSXi Wang 			break;
555d63903bbSXi Wang 		}
556d63903bbSXi Wang 		break;
557e54bcde3SZi Shen Lim 	/* dst = imm */
558e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_K:
559e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_K:
560e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, dst, imm, ctx);
561e54bcde3SZi Shen Lim 		break;
562e54bcde3SZi Shen Lim 	/* dst = dst OP imm */
563e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_K:
564e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_K:
565fd868f14SLuke Nelson 		if (is_addsub_imm(imm)) {
566fd868f14SLuke Nelson 			emit(A64_ADD_I(is64, dst, dst, imm), ctx);
567fd868f14SLuke Nelson 		} else if (is_addsub_imm(-imm)) {
568fd868f14SLuke Nelson 			emit(A64_SUB_I(is64, dst, dst, -imm), ctx);
569fd868f14SLuke Nelson 		} else {
570e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
571e54bcde3SZi Shen Lim 			emit(A64_ADD(is64, dst, dst, tmp), ctx);
572fd868f14SLuke Nelson 		}
573e54bcde3SZi Shen Lim 		break;
574e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_K:
575e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_K:
576fd868f14SLuke Nelson 		if (is_addsub_imm(imm)) {
577fd868f14SLuke Nelson 			emit(A64_SUB_I(is64, dst, dst, imm), ctx);
578fd868f14SLuke Nelson 		} else if (is_addsub_imm(-imm)) {
579fd868f14SLuke Nelson 			emit(A64_ADD_I(is64, dst, dst, -imm), ctx);
580fd868f14SLuke Nelson 		} else {
581e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
582e54bcde3SZi Shen Lim 			emit(A64_SUB(is64, dst, dst, tmp), ctx);
583fd868f14SLuke Nelson 		}
584e54bcde3SZi Shen Lim 		break;
585e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_K:
586e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_K:
587fd49591cSLuke Nelson 		a64_insn = A64_AND_I(is64, dst, dst, imm);
588fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
589fd49591cSLuke Nelson 			emit(a64_insn, ctx);
590fd49591cSLuke Nelson 		} else {
591e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
592e54bcde3SZi Shen Lim 			emit(A64_AND(is64, dst, dst, tmp), ctx);
593fd49591cSLuke Nelson 		}
594e54bcde3SZi Shen Lim 		break;
595e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_K:
596e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_K:
597fd49591cSLuke Nelson 		a64_insn = A64_ORR_I(is64, dst, dst, imm);
598fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
599fd49591cSLuke Nelson 			emit(a64_insn, ctx);
600fd49591cSLuke Nelson 		} else {
601e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
602e54bcde3SZi Shen Lim 			emit(A64_ORR(is64, dst, dst, tmp), ctx);
603fd49591cSLuke Nelson 		}
604e54bcde3SZi Shen Lim 		break;
605e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_K:
606e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_K:
607fd49591cSLuke Nelson 		a64_insn = A64_EOR_I(is64, dst, dst, imm);
608fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
609fd49591cSLuke Nelson 			emit(a64_insn, ctx);
610fd49591cSLuke Nelson 		} else {
611e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
612e54bcde3SZi Shen Lim 			emit(A64_EOR(is64, dst, dst, tmp), ctx);
613fd49591cSLuke Nelson 		}
614e54bcde3SZi Shen Lim 		break;
615e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_K:
616e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_K:
617e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
618e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, tmp), ctx);
619e54bcde3SZi Shen Lim 		break;
620e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_K:
621e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_K:
622e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
623e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, dst, dst, tmp), ctx);
624e54bcde3SZi Shen Lim 		break;
625e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOD | BPF_K:
626e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOD | BPF_K:
627e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp2, imm, ctx);
628e54bcde3SZi Shen Lim 		emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
629504792e0SJerin Jacob 		emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
630e54bcde3SZi Shen Lim 		break;
631e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_LSH | BPF_K:
632e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_LSH | BPF_K:
633e54bcde3SZi Shen Lim 		emit(A64_LSL(is64, dst, dst, imm), ctx);
634e54bcde3SZi Shen Lim 		break;
635e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_RSH | BPF_K:
636e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_RSH | BPF_K:
637e54bcde3SZi Shen Lim 		emit(A64_LSR(is64, dst, dst, imm), ctx);
638e54bcde3SZi Shen Lim 		break;
639e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ARSH | BPF_K:
640e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ARSH | BPF_K:
641e54bcde3SZi Shen Lim 		emit(A64_ASR(is64, dst, dst, imm), ctx);
642e54bcde3SZi Shen Lim 		break;
643e54bcde3SZi Shen Lim 
644e54bcde3SZi Shen Lim 	/* JUMP off */
645e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JA:
64632f6865cSIlias Apalodimas 		jmp_offset = bpf2a64_offset(i, off, ctx);
647e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
648e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
649e54bcde3SZi Shen Lim 		break;
650e54bcde3SZi Shen Lim 	/* IF (dst COND src) JUMP off */
651e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_X:
652e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_X:
653c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLT | BPF_X:
654e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_X:
655c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLE | BPF_X:
656e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_X:
657e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_X:
658c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLT | BPF_X:
659e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_X:
660c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLE | BPF_X:
661654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_X:
662654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_X:
663654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_X:
664654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_X:
665654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_X:
666654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_X:
667654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_X:
668654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_X:
669654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_X:
670654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_X:
671654b65a0SJiong Wang 		emit(A64_CMP(is64, dst, src), ctx);
672e54bcde3SZi Shen Lim emit_cond_jmp:
67332f6865cSIlias Apalodimas 		jmp_offset = bpf2a64_offset(i, off, ctx);
674e54bcde3SZi Shen Lim 		check_imm19(jmp_offset);
675e54bcde3SZi Shen Lim 		switch (BPF_OP(code)) {
676e54bcde3SZi Shen Lim 		case BPF_JEQ:
677e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_EQ;
678e54bcde3SZi Shen Lim 			break;
679e54bcde3SZi Shen Lim 		case BPF_JGT:
680e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_HI;
681e54bcde3SZi Shen Lim 			break;
682c362b2f3SDaniel Borkmann 		case BPF_JLT:
683c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_CC;
684c362b2f3SDaniel Borkmann 			break;
685e54bcde3SZi Shen Lim 		case BPF_JGE:
686e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_CS;
687e54bcde3SZi Shen Lim 			break;
688c362b2f3SDaniel Borkmann 		case BPF_JLE:
689c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_LS;
690c362b2f3SDaniel Borkmann 			break;
69198397fc5SZi Shen Lim 		case BPF_JSET:
692e54bcde3SZi Shen Lim 		case BPF_JNE:
693e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_NE;
694e54bcde3SZi Shen Lim 			break;
695e54bcde3SZi Shen Lim 		case BPF_JSGT:
696e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GT;
697e54bcde3SZi Shen Lim 			break;
698c362b2f3SDaniel Borkmann 		case BPF_JSLT:
699c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_LT;
700c362b2f3SDaniel Borkmann 			break;
701e54bcde3SZi Shen Lim 		case BPF_JSGE:
702e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GE;
703e54bcde3SZi Shen Lim 			break;
704c362b2f3SDaniel Borkmann 		case BPF_JSLE:
705c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_LE;
706c362b2f3SDaniel Borkmann 			break;
707e54bcde3SZi Shen Lim 		default:
708e54bcde3SZi Shen Lim 			return -EFAULT;
709e54bcde3SZi Shen Lim 		}
710e54bcde3SZi Shen Lim 		emit(A64_B_(jmp_cond, jmp_offset), ctx);
711e54bcde3SZi Shen Lim 		break;
712e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_X:
713654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_X:
714654b65a0SJiong Wang 		emit(A64_TST(is64, dst, src), ctx);
715e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
716e54bcde3SZi Shen Lim 	/* IF (dst COND imm) JUMP off */
717e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_K:
718e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_K:
719c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLT | BPF_K:
720e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_K:
721c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLE | BPF_K:
722e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_K:
723e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_K:
724c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLT | BPF_K:
725e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_K:
726c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLE | BPF_K:
727654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_K:
728654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_K:
729654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_K:
730654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_K:
731654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_K:
732654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_K:
733654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_K:
734654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_K:
735654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_K:
736654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_K:
737fd868f14SLuke Nelson 		if (is_addsub_imm(imm)) {
738fd868f14SLuke Nelson 			emit(A64_CMP_I(is64, dst, imm), ctx);
739fd868f14SLuke Nelson 		} else if (is_addsub_imm(-imm)) {
740fd868f14SLuke Nelson 			emit(A64_CMN_I(is64, dst, -imm), ctx);
741fd868f14SLuke Nelson 		} else {
742654b65a0SJiong Wang 			emit_a64_mov_i(is64, tmp, imm, ctx);
743654b65a0SJiong Wang 			emit(A64_CMP(is64, dst, tmp), ctx);
744fd868f14SLuke Nelson 		}
745e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
746e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_K:
747654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_K:
748fd49591cSLuke Nelson 		a64_insn = A64_TST_I(is64, dst, imm);
749fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
750fd49591cSLuke Nelson 			emit(a64_insn, ctx);
751fd49591cSLuke Nelson 		} else {
752654b65a0SJiong Wang 			emit_a64_mov_i(is64, tmp, imm, ctx);
753654b65a0SJiong Wang 			emit(A64_TST(is64, dst, tmp), ctx);
754fd49591cSLuke Nelson 		}
755e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
756e54bcde3SZi Shen Lim 	/* function call */
757e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_CALL:
758e54bcde3SZi Shen Lim 	{
759e54bcde3SZi Shen Lim 		const u8 r0 = bpf2a64[BPF_REG_0];
7608c11ea5cSDaniel Borkmann 		bool func_addr_fixed;
7618c11ea5cSDaniel Borkmann 		u64 func_addr;
762e54bcde3SZi Shen Lim 
7638c11ea5cSDaniel Borkmann 		ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
7648c11ea5cSDaniel Borkmann 					    &func_addr, &func_addr_fixed);
7658c11ea5cSDaniel Borkmann 		if (ret < 0)
7668c11ea5cSDaniel Borkmann 			return ret;
7678c11ea5cSDaniel Borkmann 		emit_addr_mov_i64(tmp, func_addr, ctx);
768e54bcde3SZi Shen Lim 		emit(A64_BLR(tmp), ctx);
769e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
770e54bcde3SZi Shen Lim 		break;
771e54bcde3SZi Shen Lim 	}
772ddb55992SZi Shen Lim 	/* tail call */
77371189fa9SAlexei Starovoitov 	case BPF_JMP | BPF_TAIL_CALL:
774ddb55992SZi Shen Lim 		if (emit_bpf_tail_call(ctx))
775ddb55992SZi Shen Lim 			return -EFAULT;
776ddb55992SZi Shen Lim 		break;
777e54bcde3SZi Shen Lim 	/* function return */
778e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_EXIT:
77951c9fbb1SZi Shen Lim 		/* Optimization: when last instruction is EXIT,
78051c9fbb1SZi Shen Lim 		   simply fallthrough to epilogue. */
781e54bcde3SZi Shen Lim 		if (i == ctx->prog->len - 1)
782e54bcde3SZi Shen Lim 			break;
783e54bcde3SZi Shen Lim 		jmp_offset = epilogue_offset(ctx);
784e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
785e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
786e54bcde3SZi Shen Lim 		break;
787e54bcde3SZi Shen Lim 
78830d3d94cSZi Shen Lim 	/* dst = imm64 */
78930d3d94cSZi Shen Lim 	case BPF_LD | BPF_IMM | BPF_DW:
79030d3d94cSZi Shen Lim 	{
79130d3d94cSZi Shen Lim 		const struct bpf_insn insn1 = insn[1];
79230d3d94cSZi Shen Lim 		u64 imm64;
79330d3d94cSZi Shen Lim 
7941e4df6b7SXi Wang 		imm64 = (u64)insn1.imm << 32 | (u32)imm;
795e4a41c2cSHou Tao 		if (bpf_pseudo_func(insn))
796e4a41c2cSHou Tao 			emit_addr_mov_i64(dst, imm64, ctx);
797e4a41c2cSHou Tao 		else
79830d3d94cSZi Shen Lim 			emit_a64_mov_i64(dst, imm64, ctx);
79930d3d94cSZi Shen Lim 
80030d3d94cSZi Shen Lim 		return 1;
80130d3d94cSZi Shen Lim 	}
80230d3d94cSZi Shen Lim 
803e54bcde3SZi Shen Lim 	/* LDX: dst = *(size *)(src + off) */
804e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_W:
805e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_H:
806e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_B:
807e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_DW:
80880083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
80980083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_W:
81080083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_H:
81180083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_B:
812e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, off, ctx);
813e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
814e54bcde3SZi Shen Lim 		case BPF_W:
815e54bcde3SZi Shen Lim 			emit(A64_LDR32(dst, src, tmp), ctx);
816e54bcde3SZi Shen Lim 			break;
817e54bcde3SZi Shen Lim 		case BPF_H:
818e54bcde3SZi Shen Lim 			emit(A64_LDRH(dst, src, tmp), ctx);
819e54bcde3SZi Shen Lim 			break;
820e54bcde3SZi Shen Lim 		case BPF_B:
821e54bcde3SZi Shen Lim 			emit(A64_LDRB(dst, src, tmp), ctx);
822e54bcde3SZi Shen Lim 			break;
823e54bcde3SZi Shen Lim 		case BPF_DW:
824e54bcde3SZi Shen Lim 			emit(A64_LDR64(dst, src, tmp), ctx);
825e54bcde3SZi Shen Lim 			break;
826e54bcde3SZi Shen Lim 		}
82780083428SJean-Philippe Brucker 
82880083428SJean-Philippe Brucker 		ret = add_exception_handler(insn, ctx, dst);
82980083428SJean-Philippe Brucker 		if (ret)
83080083428SJean-Philippe Brucker 			return ret;
831e54bcde3SZi Shen Lim 		break;
832e54bcde3SZi Shen Lim 
833f5e81d11SDaniel Borkmann 	/* speculation barrier */
834f5e81d11SDaniel Borkmann 	case BPF_ST | BPF_NOSPEC:
835f5e81d11SDaniel Borkmann 		/*
836f5e81d11SDaniel Borkmann 		 * Nothing required here.
837f5e81d11SDaniel Borkmann 		 *
838f5e81d11SDaniel Borkmann 		 * In case of arm64, we rely on the firmware mitigation of
839f5e81d11SDaniel Borkmann 		 * Speculative Store Bypass as controlled via the ssbd kernel
840f5e81d11SDaniel Borkmann 		 * parameter. Whenever the mitigation is enabled, it works
841f5e81d11SDaniel Borkmann 		 * for all of the kernel code with no need to provide any
842f5e81d11SDaniel Borkmann 		 * additional instructions.
843f5e81d11SDaniel Borkmann 		 */
844f5e81d11SDaniel Borkmann 		break;
845f5e81d11SDaniel Borkmann 
846e54bcde3SZi Shen Lim 	/* ST: *(size *)(dst + off) = imm */
847e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_W:
848e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_H:
849e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_B:
850e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_DW:
851df849ba3SYang Shi 		/* Load imm to a register then store it */
852df849ba3SYang Shi 		emit_a64_mov_i(1, tmp2, off, ctx);
853df849ba3SYang Shi 		emit_a64_mov_i(1, tmp, imm, ctx);
854df849ba3SYang Shi 		switch (BPF_SIZE(code)) {
855df849ba3SYang Shi 		case BPF_W:
856df849ba3SYang Shi 			emit(A64_STR32(tmp, dst, tmp2), ctx);
857df849ba3SYang Shi 			break;
858df849ba3SYang Shi 		case BPF_H:
859df849ba3SYang Shi 			emit(A64_STRH(tmp, dst, tmp2), ctx);
860df849ba3SYang Shi 			break;
861df849ba3SYang Shi 		case BPF_B:
862df849ba3SYang Shi 			emit(A64_STRB(tmp, dst, tmp2), ctx);
863df849ba3SYang Shi 			break;
864df849ba3SYang Shi 		case BPF_DW:
865df849ba3SYang Shi 			emit(A64_STR64(tmp, dst, tmp2), ctx);
866df849ba3SYang Shi 			break;
867df849ba3SYang Shi 		}
868df849ba3SYang Shi 		break;
869e54bcde3SZi Shen Lim 
870e54bcde3SZi Shen Lim 	/* STX: *(size *)(dst + off) = src */
871e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_W:
872e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_H:
873e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_B:
874e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_DW:
875e54bcde3SZi Shen Lim 		emit_a64_mov_i(1, tmp, off, ctx);
876e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
877e54bcde3SZi Shen Lim 		case BPF_W:
878e54bcde3SZi Shen Lim 			emit(A64_STR32(src, dst, tmp), ctx);
879e54bcde3SZi Shen Lim 			break;
880e54bcde3SZi Shen Lim 		case BPF_H:
881e54bcde3SZi Shen Lim 			emit(A64_STRH(src, dst, tmp), ctx);
882e54bcde3SZi Shen Lim 			break;
883e54bcde3SZi Shen Lim 		case BPF_B:
884e54bcde3SZi Shen Lim 			emit(A64_STRB(src, dst, tmp), ctx);
885e54bcde3SZi Shen Lim 			break;
886e54bcde3SZi Shen Lim 		case BPF_DW:
887e54bcde3SZi Shen Lim 			emit(A64_STR64(src, dst, tmp), ctx);
888e54bcde3SZi Shen Lim 			break;
889e54bcde3SZi Shen Lim 		}
890e54bcde3SZi Shen Lim 		break;
89134b8ab09SDaniel Borkmann 
89291c960b0SBrendan Jackman 	case BPF_STX | BPF_ATOMIC | BPF_W:
89391c960b0SBrendan Jackman 	case BPF_STX | BPF_ATOMIC | BPF_DW:
89491c960b0SBrendan Jackman 		if (insn->imm != BPF_ADD) {
89591c960b0SBrendan Jackman 			pr_err_once("unknown atomic op code %02x\n", insn->imm);
89691c960b0SBrendan Jackman 			return -EINVAL;
89791c960b0SBrendan Jackman 		}
89891c960b0SBrendan Jackman 
89991c960b0SBrendan Jackman 		/* STX XADD: lock *(u32 *)(dst + off) += src
90091c960b0SBrendan Jackman 		 * and
90191c960b0SBrendan Jackman 		 * STX XADD: lock *(u64 *)(dst + off) += src
90291c960b0SBrendan Jackman 		 */
90391c960b0SBrendan Jackman 
90434b8ab09SDaniel Borkmann 		if (!off) {
90534b8ab09SDaniel Borkmann 			reg = dst;
90634b8ab09SDaniel Borkmann 		} else {
90785f68fe8SDaniel Borkmann 			emit_a64_mov_i(1, tmp, off, ctx);
90885f68fe8SDaniel Borkmann 			emit(A64_ADD(1, tmp, tmp, dst), ctx);
90934b8ab09SDaniel Borkmann 			reg = tmp;
91034b8ab09SDaniel Borkmann 		}
91134b8ab09SDaniel Borkmann 		if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS)) {
91234b8ab09SDaniel Borkmann 			emit(A64_STADD(isdw, reg, src), ctx);
91334b8ab09SDaniel Borkmann 		} else {
91434b8ab09SDaniel Borkmann 			emit(A64_LDXR(isdw, tmp2, reg), ctx);
91585f68fe8SDaniel Borkmann 			emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
91634b8ab09SDaniel Borkmann 			emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
91785f68fe8SDaniel Borkmann 			jmp_offset = -3;
91885f68fe8SDaniel Borkmann 			check_imm19(jmp_offset);
9197005cadeSDaniel Borkmann 			emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
92034b8ab09SDaniel Borkmann 		}
92185f68fe8SDaniel Borkmann 		break;
922e54bcde3SZi Shen Lim 
923e54bcde3SZi Shen Lim 	default:
924e54bcde3SZi Shen Lim 		pr_err_once("unknown opcode %02x\n", code);
925e54bcde3SZi Shen Lim 		return -EINVAL;
926e54bcde3SZi Shen Lim 	}
927e54bcde3SZi Shen Lim 
928e54bcde3SZi Shen Lim 	return 0;
929e54bcde3SZi Shen Lim }
930e54bcde3SZi Shen Lim 
9318c11ea5cSDaniel Borkmann static int build_body(struct jit_ctx *ctx, bool extra_pass)
932e54bcde3SZi Shen Lim {
933e54bcde3SZi Shen Lim 	const struct bpf_prog *prog = ctx->prog;
934e54bcde3SZi Shen Lim 	int i;
935e54bcde3SZi Shen Lim 
93632f6865cSIlias Apalodimas 	/*
93732f6865cSIlias Apalodimas 	 * - offset[0] offset of the end of prologue,
93832f6865cSIlias Apalodimas 	 *   start of the 1st instruction.
93932f6865cSIlias Apalodimas 	 * - offset[1] - offset of the end of 1st instruction,
94032f6865cSIlias Apalodimas 	 *   start of the 2nd instruction
94132f6865cSIlias Apalodimas 	 * [....]
94232f6865cSIlias Apalodimas 	 * - offset[3] - offset of the end of 3rd instruction,
94332f6865cSIlias Apalodimas 	 *   start of 4th instruction
94432f6865cSIlias Apalodimas 	 */
945e54bcde3SZi Shen Lim 	for (i = 0; i < prog->len; i++) {
946e54bcde3SZi Shen Lim 		const struct bpf_insn *insn = &prog->insnsi[i];
947e54bcde3SZi Shen Lim 		int ret;
948e54bcde3SZi Shen Lim 
94932f6865cSIlias Apalodimas 		if (ctx->image == NULL)
95032f6865cSIlias Apalodimas 			ctx->offset[i] = ctx->idx;
9518c11ea5cSDaniel Borkmann 		ret = build_insn(insn, ctx, extra_pass);
95230d3d94cSZi Shen Lim 		if (ret > 0) {
95330d3d94cSZi Shen Lim 			i++;
954ddc665a4SDaniel Borkmann 			if (ctx->image == NULL)
955ddc665a4SDaniel Borkmann 				ctx->offset[i] = ctx->idx;
95630d3d94cSZi Shen Lim 			continue;
95730d3d94cSZi Shen Lim 		}
958e54bcde3SZi Shen Lim 		if (ret)
959e54bcde3SZi Shen Lim 			return ret;
960e54bcde3SZi Shen Lim 	}
96132f6865cSIlias Apalodimas 	/*
96232f6865cSIlias Apalodimas 	 * offset is allocated with prog->len + 1 so fill in
96332f6865cSIlias Apalodimas 	 * the last element with the offset after the last
96432f6865cSIlias Apalodimas 	 * instruction (end of program)
96532f6865cSIlias Apalodimas 	 */
96632f6865cSIlias Apalodimas 	if (ctx->image == NULL)
96732f6865cSIlias Apalodimas 		ctx->offset[i] = ctx->idx;
968e54bcde3SZi Shen Lim 
969e54bcde3SZi Shen Lim 	return 0;
970e54bcde3SZi Shen Lim }
971e54bcde3SZi Shen Lim 
97242ff712bSZi Shen Lim static int validate_code(struct jit_ctx *ctx)
97342ff712bSZi Shen Lim {
97442ff712bSZi Shen Lim 	int i;
97542ff712bSZi Shen Lim 
97642ff712bSZi Shen Lim 	for (i = 0; i < ctx->idx; i++) {
97742ff712bSZi Shen Lim 		u32 a64_insn = le32_to_cpu(ctx->image[i]);
97842ff712bSZi Shen Lim 
97942ff712bSZi Shen Lim 		if (a64_insn == AARCH64_BREAK_FAULT)
98042ff712bSZi Shen Lim 			return -1;
98142ff712bSZi Shen Lim 	}
98242ff712bSZi Shen Lim 
98380083428SJean-Philippe Brucker 	if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries))
98480083428SJean-Philippe Brucker 		return -1;
98580083428SJean-Philippe Brucker 
98642ff712bSZi Shen Lim 	return 0;
98742ff712bSZi Shen Lim }
98842ff712bSZi Shen Lim 
989e54bcde3SZi Shen Lim static inline void bpf_flush_icache(void *start, void *end)
990e54bcde3SZi Shen Lim {
991e54bcde3SZi Shen Lim 	flush_icache_range((unsigned long)start, (unsigned long)end);
992e54bcde3SZi Shen Lim }
993e54bcde3SZi Shen Lim 
994db496944SAlexei Starovoitov struct arm64_jit_data {
995db496944SAlexei Starovoitov 	struct bpf_binary_header *header;
996db496944SAlexei Starovoitov 	u8 *image;
997db496944SAlexei Starovoitov 	struct jit_ctx ctx;
998db496944SAlexei Starovoitov };
999db496944SAlexei Starovoitov 
1000d1c55ab5SDaniel Borkmann struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
1001e54bcde3SZi Shen Lim {
100280083428SJean-Philippe Brucker 	int image_size, prog_size, extable_size;
100326eb042eSDaniel Borkmann 	struct bpf_prog *tmp, *orig_prog = prog;
1004b569c1c6SDaniel Borkmann 	struct bpf_binary_header *header;
1005db496944SAlexei Starovoitov 	struct arm64_jit_data *jit_data;
100656ea6a8bSDaniel Borkmann 	bool was_classic = bpf_prog_was_classic(prog);
100726eb042eSDaniel Borkmann 	bool tmp_blinded = false;
1008db496944SAlexei Starovoitov 	bool extra_pass = false;
1009e54bcde3SZi Shen Lim 	struct jit_ctx ctx;
1010b569c1c6SDaniel Borkmann 	u8 *image_ptr;
1011e54bcde3SZi Shen Lim 
101260b58afcSAlexei Starovoitov 	if (!prog->jit_requested)
101326eb042eSDaniel Borkmann 		return orig_prog;
101426eb042eSDaniel Borkmann 
101526eb042eSDaniel Borkmann 	tmp = bpf_jit_blind_constants(prog);
101626eb042eSDaniel Borkmann 	/* If blinding was requested and we failed during blinding,
101726eb042eSDaniel Borkmann 	 * we must fall back to the interpreter.
101826eb042eSDaniel Borkmann 	 */
101926eb042eSDaniel Borkmann 	if (IS_ERR(tmp))
102026eb042eSDaniel Borkmann 		return orig_prog;
102126eb042eSDaniel Borkmann 	if (tmp != prog) {
102226eb042eSDaniel Borkmann 		tmp_blinded = true;
102326eb042eSDaniel Borkmann 		prog = tmp;
102426eb042eSDaniel Borkmann 	}
1025e54bcde3SZi Shen Lim 
1026db496944SAlexei Starovoitov 	jit_data = prog->aux->jit_data;
1027db496944SAlexei Starovoitov 	if (!jit_data) {
1028db496944SAlexei Starovoitov 		jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
1029db496944SAlexei Starovoitov 		if (!jit_data) {
1030db496944SAlexei Starovoitov 			prog = orig_prog;
1031db496944SAlexei Starovoitov 			goto out;
1032db496944SAlexei Starovoitov 		}
1033db496944SAlexei Starovoitov 		prog->aux->jit_data = jit_data;
1034db496944SAlexei Starovoitov 	}
1035db496944SAlexei Starovoitov 	if (jit_data->ctx.offset) {
1036db496944SAlexei Starovoitov 		ctx = jit_data->ctx;
1037db496944SAlexei Starovoitov 		image_ptr = jit_data->image;
1038db496944SAlexei Starovoitov 		header = jit_data->header;
1039db496944SAlexei Starovoitov 		extra_pass = true;
104080083428SJean-Philippe Brucker 		prog_size = sizeof(u32) * ctx.idx;
1041db496944SAlexei Starovoitov 		goto skip_init_ctx;
1042db496944SAlexei Starovoitov 	}
1043e54bcde3SZi Shen Lim 	memset(&ctx, 0, sizeof(ctx));
1044e54bcde3SZi Shen Lim 	ctx.prog = prog;
1045e54bcde3SZi Shen Lim 
104632f6865cSIlias Apalodimas 	ctx.offset = kcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
104726eb042eSDaniel Borkmann 	if (ctx.offset == NULL) {
104826eb042eSDaniel Borkmann 		prog = orig_prog;
1049db496944SAlexei Starovoitov 		goto out_off;
105026eb042eSDaniel Borkmann 	}
1051e54bcde3SZi Shen Lim 
105268e4f238SHou Tao 	/*
105368e4f238SHou Tao 	 * 1. Initial fake pass to compute ctx->idx and ctx->offset.
105468e4f238SHou Tao 	 *
105568e4f238SHou Tao 	 * BPF line info needs ctx->offset[i] to be the offset of
105668e4f238SHou Tao 	 * instruction[i] in jited image, so build prologue first.
105768e4f238SHou Tao 	 */
105868e4f238SHou Tao 	if (build_prologue(&ctx, was_classic)) {
105926eb042eSDaniel Borkmann 		prog = orig_prog;
106026eb042eSDaniel Borkmann 		goto out_off;
106126eb042eSDaniel Borkmann 	}
1062e54bcde3SZi Shen Lim 
106368e4f238SHou Tao 	if (build_body(&ctx, extra_pass)) {
1064ddb55992SZi Shen Lim 		prog = orig_prog;
1065ddb55992SZi Shen Lim 		goto out_off;
1066ddb55992SZi Shen Lim 	}
106751c9fbb1SZi Shen Lim 
106851c9fbb1SZi Shen Lim 	ctx.epilogue_offset = ctx.idx;
1069e54bcde3SZi Shen Lim 	build_epilogue(&ctx);
1070e54bcde3SZi Shen Lim 
107180083428SJean-Philippe Brucker 	extable_size = prog->aux->num_exentries *
107280083428SJean-Philippe Brucker 		sizeof(struct exception_table_entry);
107380083428SJean-Philippe Brucker 
1074e54bcde3SZi Shen Lim 	/* Now we know the actual image size. */
107580083428SJean-Philippe Brucker 	prog_size = sizeof(u32) * ctx.idx;
107680083428SJean-Philippe Brucker 	image_size = prog_size + extable_size;
1077b569c1c6SDaniel Borkmann 	header = bpf_jit_binary_alloc(image_size, &image_ptr,
1078b569c1c6SDaniel Borkmann 				      sizeof(u32), jit_fill_hole);
107926eb042eSDaniel Borkmann 	if (header == NULL) {
108026eb042eSDaniel Borkmann 		prog = orig_prog;
108126eb042eSDaniel Borkmann 		goto out_off;
108226eb042eSDaniel Borkmann 	}
1083e54bcde3SZi Shen Lim 
1084e54bcde3SZi Shen Lim 	/* 2. Now, the actual pass. */
1085e54bcde3SZi Shen Lim 
1086425e1ed7SLuc Van Oostenryck 	ctx.image = (__le32 *)image_ptr;
108780083428SJean-Philippe Brucker 	if (extable_size)
108880083428SJean-Philippe Brucker 		prog->aux->extable = (void *)image_ptr + prog_size;
1089db496944SAlexei Starovoitov skip_init_ctx:
1090e54bcde3SZi Shen Lim 	ctx.idx = 0;
109180083428SJean-Philippe Brucker 	ctx.exentry_idx = 0;
1092b569c1c6SDaniel Borkmann 
109356ea6a8bSDaniel Borkmann 	build_prologue(&ctx, was_classic);
1094e54bcde3SZi Shen Lim 
10958c11ea5cSDaniel Borkmann 	if (build_body(&ctx, extra_pass)) {
1096b569c1c6SDaniel Borkmann 		bpf_jit_binary_free(header);
109726eb042eSDaniel Borkmann 		prog = orig_prog;
109826eb042eSDaniel Borkmann 		goto out_off;
109960ef0494SDaniel Borkmann 	}
1100e54bcde3SZi Shen Lim 
1101e54bcde3SZi Shen Lim 	build_epilogue(&ctx);
1102e54bcde3SZi Shen Lim 
110342ff712bSZi Shen Lim 	/* 3. Extra pass to validate JITed code. */
110442ff712bSZi Shen Lim 	if (validate_code(&ctx)) {
110542ff712bSZi Shen Lim 		bpf_jit_binary_free(header);
110626eb042eSDaniel Borkmann 		prog = orig_prog;
110726eb042eSDaniel Borkmann 		goto out_off;
110842ff712bSZi Shen Lim 	}
110942ff712bSZi Shen Lim 
1110e54bcde3SZi Shen Lim 	/* And we're done. */
1111e54bcde3SZi Shen Lim 	if (bpf_jit_enable > 1)
111280083428SJean-Philippe Brucker 		bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
1113e54bcde3SZi Shen Lim 
1114c3d4c682SDaniel Borkmann 	bpf_flush_icache(header, ctx.image + ctx.idx);
1115b569c1c6SDaniel Borkmann 
1116db496944SAlexei Starovoitov 	if (!prog->is_func || extra_pass) {
1117db496944SAlexei Starovoitov 		if (extra_pass && ctx.idx != jit_data->ctx.idx) {
1118db496944SAlexei Starovoitov 			pr_err_once("multi-func JIT bug %d != %d\n",
1119db496944SAlexei Starovoitov 				    ctx.idx, jit_data->ctx.idx);
1120db496944SAlexei Starovoitov 			bpf_jit_binary_free(header);
1121db496944SAlexei Starovoitov 			prog->bpf_func = NULL;
1122db496944SAlexei Starovoitov 			prog->jited = 0;
1123db496944SAlexei Starovoitov 			goto out_off;
1124db496944SAlexei Starovoitov 		}
11259d876e79SDaniel Borkmann 		bpf_jit_binary_lock_ro(header);
1126db496944SAlexei Starovoitov 	} else {
1127db496944SAlexei Starovoitov 		jit_data->ctx = ctx;
1128db496944SAlexei Starovoitov 		jit_data->image = image_ptr;
1129db496944SAlexei Starovoitov 		jit_data->header = header;
1130db496944SAlexei Starovoitov 	}
1131e54bcde3SZi Shen Lim 	prog->bpf_func = (void *)ctx.image;
1132a91263d5SDaniel Borkmann 	prog->jited = 1;
113380083428SJean-Philippe Brucker 	prog->jited_len = prog_size;
113426eb042eSDaniel Borkmann 
1135db496944SAlexei Starovoitov 	if (!prog->is_func || extra_pass) {
1136*dda7596cSHou Tao 		int i;
1137*dda7596cSHou Tao 
1138*dda7596cSHou Tao 		/* offset[prog->len] is the size of program */
1139*dda7596cSHou Tao 		for (i = 0; i <= prog->len; i++)
1140*dda7596cSHou Tao 			ctx.offset[i] *= AARCH64_INSN_SIZE;
114132f6865cSIlias Apalodimas 		bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
114226eb042eSDaniel Borkmann out_off:
1143e54bcde3SZi Shen Lim 		kfree(ctx.offset);
1144db496944SAlexei Starovoitov 		kfree(jit_data);
1145db496944SAlexei Starovoitov 		prog->aux->jit_data = NULL;
1146db496944SAlexei Starovoitov 	}
114726eb042eSDaniel Borkmann out:
114826eb042eSDaniel Borkmann 	if (tmp_blinded)
114926eb042eSDaniel Borkmann 		bpf_jit_prog_release_other(prog, prog == orig_prog ?
115026eb042eSDaniel Borkmann 					   tmp : orig_prog);
1151d1c55ab5SDaniel Borkmann 	return prog;
1152e54bcde3SZi Shen Lim }
115391fc957cSArd Biesheuvel 
1154b5e975d2SHou Tao bool bpf_jit_supports_kfunc_call(void)
1155b5e975d2SHou Tao {
1156b5e975d2SHou Tao 	return true;
1157b5e975d2SHou Tao }
1158b5e975d2SHou Tao 
11595d63ae90SLorenz Bauer u64 bpf_jit_alloc_exec_limit(void)
11605d63ae90SLorenz Bauer {
1161b89ddf4cSRussell King 	return VMALLOC_END - VMALLOC_START;
11625d63ae90SLorenz Bauer }
11635d63ae90SLorenz Bauer 
116491fc957cSArd Biesheuvel void *bpf_jit_alloc_exec(unsigned long size)
116591fc957cSArd Biesheuvel {
1166b89ddf4cSRussell King 	return vmalloc(size);
116791fc957cSArd Biesheuvel }
116891fc957cSArd Biesheuvel 
116991fc957cSArd Biesheuvel void bpf_jit_free_exec(void *addr)
117091fc957cSArd Biesheuvel {
117191fc957cSArd Biesheuvel 	return vfree(addr);
117291fc957cSArd Biesheuvel }
1173