xref: /linux/arch/arm64/net/bpf_jit_comp.c (revision 36ec807b627b4c0a0a382f0ae48eac7187d14b2b)
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>
13b2ad54e1SXu Kuohai #include <linux/memory.h>
14e54bcde3SZi Shen Lim #include <linux/printk.h>
15e54bcde3SZi Shen Lim #include <linux/slab.h>
16b569c1c6SDaniel Borkmann 
17d6e2cc56SMark Rutland #include <asm/asm-extable.h>
18e54bcde3SZi Shen Lim #include <asm/byteorder.h>
19e54bcde3SZi Shen Lim #include <asm/cacheflush.h>
20b569c1c6SDaniel Borkmann #include <asm/debug-monitors.h>
213e00e39dSMark Rutland #include <asm/insn.h>
22b2ad54e1SXu Kuohai #include <asm/patching.h>
23d4bbc30bSLaura Abbott #include <asm/set_memory.h>
24e54bcde3SZi Shen Lim 
25e54bcde3SZi Shen Lim #include "bpf_jit.h"
26e54bcde3SZi Shen Lim 
2726eb042eSDaniel Borkmann #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
2826eb042eSDaniel Borkmann #define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
29ddb55992SZi Shen Lim #define TCALL_CNT (MAX_BPF_JIT_REG + 2)
307005cadeSDaniel Borkmann #define TMP_REG_3 (MAX_BPF_JIT_REG + 3)
315b3d19b9SXu Kuohai #define FP_BOTTOM (MAX_BPF_JIT_REG + 4)
32339af577SPuranjay Mohan #define ARENA_VM_START (MAX_BPF_JIT_REG + 5)
33e54bcde3SZi Shen Lim 
341902472bSHou Tao #define check_imm(bits, imm) do {				\
351902472bSHou Tao 	if ((((imm) > 0) && ((imm) >> (bits))) ||		\
361902472bSHou Tao 	    (((imm) < 0) && (~(imm) >> (bits)))) {		\
371902472bSHou Tao 		pr_info("[%2d] imm=%d(0x%x) out of range\n",	\
381902472bSHou Tao 			i, imm, imm);				\
391902472bSHou Tao 		return -EINVAL;					\
401902472bSHou Tao 	}							\
411902472bSHou Tao } while (0)
421902472bSHou Tao #define check_imm19(imm) check_imm(19, imm)
431902472bSHou Tao #define check_imm26(imm) check_imm(26, imm)
441902472bSHou Tao 
45e54bcde3SZi Shen Lim /* Map BPF registers to A64 registers */
46e54bcde3SZi Shen Lim static const int bpf2a64[] = {
47e54bcde3SZi Shen Lim 	/* return value from in-kernel function, and exit value from eBPF */
48e54bcde3SZi Shen Lim 	[BPF_REG_0] = A64_R(7),
49e54bcde3SZi Shen Lim 	/* arguments from eBPF program to in-kernel function */
50e54bcde3SZi Shen Lim 	[BPF_REG_1] = A64_R(0),
51e54bcde3SZi Shen Lim 	[BPF_REG_2] = A64_R(1),
52e54bcde3SZi Shen Lim 	[BPF_REG_3] = A64_R(2),
53e54bcde3SZi Shen Lim 	[BPF_REG_4] = A64_R(3),
54e54bcde3SZi Shen Lim 	[BPF_REG_5] = A64_R(4),
55e54bcde3SZi Shen Lim 	/* callee saved registers that in-kernel function will preserve */
56e54bcde3SZi Shen Lim 	[BPF_REG_6] = A64_R(19),
57e54bcde3SZi Shen Lim 	[BPF_REG_7] = A64_R(20),
58e54bcde3SZi Shen Lim 	[BPF_REG_8] = A64_R(21),
59e54bcde3SZi Shen Lim 	[BPF_REG_9] = A64_R(22),
60e54bcde3SZi Shen Lim 	/* read-only frame pointer to access stack */
61ec0738dbSYang Shi 	[BPF_REG_FP] = A64_R(25),
6206edc59cSChristoph Hellwig 	/* temporary registers for BPF JIT */
634c1cd4fdSYang Shi 	[TMP_REG_1] = A64_R(10),
644c1cd4fdSYang Shi 	[TMP_REG_2] = A64_R(11),
657005cadeSDaniel Borkmann 	[TMP_REG_3] = A64_R(12),
66ddb55992SZi Shen Lim 	/* tail_call_cnt */
67ddb55992SZi Shen Lim 	[TCALL_CNT] = A64_R(26),
6826eb042eSDaniel Borkmann 	/* temporary register for blinding constants */
6926eb042eSDaniel Borkmann 	[BPF_REG_AX] = A64_R(9),
705b3d19b9SXu Kuohai 	[FP_BOTTOM] = A64_R(27),
71339af577SPuranjay Mohan 	/* callee saved register for kern_vm_start address */
72339af577SPuranjay Mohan 	[ARENA_VM_START] = A64_R(28),
73e54bcde3SZi Shen Lim };
74e54bcde3SZi Shen Lim 
75e54bcde3SZi Shen Lim struct jit_ctx {
76e54bcde3SZi Shen Lim 	const struct bpf_prog *prog;
77e54bcde3SZi Shen Lim 	int idx;
7851c9fbb1SZi Shen Lim 	int epilogue_offset;
79e54bcde3SZi Shen Lim 	int *offset;
8080083428SJean-Philippe Brucker 	int exentry_idx;
81425e1ed7SLuc Van Oostenryck 	__le32 *image;
821dad391dSPuranjay Mohan 	__le32 *ro_image;
83f1c9eed7SDaniel Borkmann 	u32 stack_size;
845b3d19b9SXu Kuohai 	int fpb_offset;
854dd31243SPuranjay Mohan 	u64 user_vm_start;
86e54bcde3SZi Shen Lim };
87e54bcde3SZi Shen Lim 
88b2ad54e1SXu Kuohai struct bpf_plt {
89b2ad54e1SXu Kuohai 	u32 insn_ldr; /* load target */
90b2ad54e1SXu Kuohai 	u32 insn_br;  /* branch to target */
91b2ad54e1SXu Kuohai 	u64 target;   /* target value */
92b2ad54e1SXu Kuohai };
93b2ad54e1SXu Kuohai 
94b2ad54e1SXu Kuohai #define PLT_TARGET_SIZE   sizeof_field(struct bpf_plt, target)
95b2ad54e1SXu Kuohai #define PLT_TARGET_OFFSET offsetof(struct bpf_plt, target)
96b2ad54e1SXu Kuohai 
97e54bcde3SZi Shen Lim static inline void emit(const u32 insn, struct jit_ctx *ctx)
98e54bcde3SZi Shen Lim {
99e54bcde3SZi Shen Lim 	if (ctx->image != NULL)
100e54bcde3SZi Shen Lim 		ctx->image[ctx->idx] = cpu_to_le32(insn);
101e54bcde3SZi Shen Lim 
102e54bcde3SZi Shen Lim 	ctx->idx++;
103e54bcde3SZi Shen Lim }
104e54bcde3SZi Shen Lim 
105e54bcde3SZi Shen Lim static inline void emit_a64_mov_i(const int is64, const int reg,
106e54bcde3SZi Shen Lim 				  const s32 val, struct jit_ctx *ctx)
107e54bcde3SZi Shen Lim {
108e54bcde3SZi Shen Lim 	u16 hi = val >> 16;
109e54bcde3SZi Shen Lim 	u16 lo = val & 0xffff;
110e54bcde3SZi Shen Lim 
111e54bcde3SZi Shen Lim 	if (hi & 0x8000) {
112e54bcde3SZi Shen Lim 		if (hi == 0xffff) {
113e54bcde3SZi Shen Lim 			emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
114e54bcde3SZi Shen Lim 		} else {
115e54bcde3SZi Shen Lim 			emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
1166d2eea6fSDaniel Borkmann 			if (lo != 0xffff)
117e54bcde3SZi Shen Lim 				emit(A64_MOVK(is64, reg, lo, 0), ctx);
118e54bcde3SZi Shen Lim 		}
119e54bcde3SZi Shen Lim 	} else {
120e54bcde3SZi Shen Lim 		emit(A64_MOVZ(is64, reg, lo, 0), ctx);
121e54bcde3SZi Shen Lim 		if (hi)
122e54bcde3SZi Shen Lim 			emit(A64_MOVK(is64, reg, hi, 16), ctx);
123e54bcde3SZi Shen Lim 	}
124e54bcde3SZi Shen Lim }
125e54bcde3SZi Shen Lim 
1266d2eea6fSDaniel Borkmann static int i64_i16_blocks(const u64 val, bool inverse)
1276d2eea6fSDaniel Borkmann {
1286d2eea6fSDaniel Borkmann 	return (((val >>  0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
1296d2eea6fSDaniel Borkmann 	       (((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
1306d2eea6fSDaniel Borkmann 	       (((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
1316d2eea6fSDaniel Borkmann 	       (((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
1326d2eea6fSDaniel Borkmann }
1336d2eea6fSDaniel Borkmann 
1346d2eea6fSDaniel Borkmann static inline void emit_a64_mov_i64(const int reg, const u64 val,
1356d2eea6fSDaniel Borkmann 				    struct jit_ctx *ctx)
1366d2eea6fSDaniel Borkmann {
1376d2eea6fSDaniel Borkmann 	u64 nrm_tmp = val, rev_tmp = ~val;
1386d2eea6fSDaniel Borkmann 	bool inverse;
1396d2eea6fSDaniel Borkmann 	int shift;
1406d2eea6fSDaniel Borkmann 
1416d2eea6fSDaniel Borkmann 	if (!(nrm_tmp >> 32))
1426d2eea6fSDaniel Borkmann 		return emit_a64_mov_i(0, reg, (u32)val, ctx);
1436d2eea6fSDaniel Borkmann 
1446d2eea6fSDaniel Borkmann 	inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
1456d2eea6fSDaniel Borkmann 	shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
1466d2eea6fSDaniel Borkmann 					  (fls64(nrm_tmp) - 1)), 16), 0);
1476d2eea6fSDaniel Borkmann 	if (inverse)
1486d2eea6fSDaniel Borkmann 		emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
1496d2eea6fSDaniel Borkmann 	else
1506d2eea6fSDaniel Borkmann 		emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
1516d2eea6fSDaniel Borkmann 	shift -= 16;
1526d2eea6fSDaniel Borkmann 	while (shift >= 0) {
1536d2eea6fSDaniel Borkmann 		if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
1546d2eea6fSDaniel Borkmann 			emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
1556d2eea6fSDaniel Borkmann 		shift -= 16;
1566d2eea6fSDaniel Borkmann 	}
1576d2eea6fSDaniel Borkmann }
1586d2eea6fSDaniel Borkmann 
159b2ad54e1SXu Kuohai static inline void emit_bti(u32 insn, struct jit_ctx *ctx)
160b2ad54e1SXu Kuohai {
161b2ad54e1SXu Kuohai 	if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
162b2ad54e1SXu Kuohai 		emit(insn, ctx);
163b2ad54e1SXu Kuohai }
164b2ad54e1SXu Kuohai 
1656d2eea6fSDaniel Borkmann /*
166cc2b8ed1SArd Biesheuvel  * Kernel addresses in the vmalloc space use at most 48 bits, and the
167cc2b8ed1SArd Biesheuvel  * remaining bits are guaranteed to be 0x1. So we can compose the address
168cc2b8ed1SArd Biesheuvel  * with a fixed length movn/movk/movk sequence.
1696d2eea6fSDaniel Borkmann  */
1706d2eea6fSDaniel Borkmann static inline void emit_addr_mov_i64(const int reg, const u64 val,
1716d2eea6fSDaniel Borkmann 				     struct jit_ctx *ctx)
1726d2eea6fSDaniel Borkmann {
1736d2eea6fSDaniel Borkmann 	u64 tmp = val;
1746d2eea6fSDaniel Borkmann 	int shift = 0;
1756d2eea6fSDaniel Borkmann 
176cc2b8ed1SArd Biesheuvel 	emit(A64_MOVN(1, reg, ~tmp & 0xffff, shift), ctx);
177cc2b8ed1SArd Biesheuvel 	while (shift < 32) {
1786d2eea6fSDaniel Borkmann 		tmp >>= 16;
1796d2eea6fSDaniel Borkmann 		shift += 16;
1806d2eea6fSDaniel Borkmann 		emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
1816d2eea6fSDaniel Borkmann 	}
1826d2eea6fSDaniel Borkmann }
1836d2eea6fSDaniel Borkmann 
184efc9909fSXu Kuohai static inline void emit_call(u64 target, struct jit_ctx *ctx)
185efc9909fSXu Kuohai {
186efc9909fSXu Kuohai 	u8 tmp = bpf2a64[TMP_REG_1];
187efc9909fSXu Kuohai 
188efc9909fSXu Kuohai 	emit_addr_mov_i64(tmp, target, ctx);
189efc9909fSXu Kuohai 	emit(A64_BLR(tmp), ctx);
190efc9909fSXu Kuohai }
191efc9909fSXu Kuohai 
19232f6865cSIlias Apalodimas static inline int bpf2a64_offset(int bpf_insn, int off,
193e54bcde3SZi Shen Lim 				 const struct jit_ctx *ctx)
194e54bcde3SZi Shen Lim {
19532f6865cSIlias Apalodimas 	/* BPF JMP offset is relative to the next instruction */
19632f6865cSIlias Apalodimas 	bpf_insn++;
19732f6865cSIlias Apalodimas 	/*
19832f6865cSIlias Apalodimas 	 * Whereas arm64 branch instructions encode the offset
19932f6865cSIlias Apalodimas 	 * from the branch itself, so we must subtract 1 from the
20032f6865cSIlias Apalodimas 	 * instruction offset.
20132f6865cSIlias Apalodimas 	 */
20232f6865cSIlias Apalodimas 	return ctx->offset[bpf_insn + off] - (ctx->offset[bpf_insn] - 1);
203e54bcde3SZi Shen Lim }
204e54bcde3SZi Shen Lim 
205b569c1c6SDaniel Borkmann static void jit_fill_hole(void *area, unsigned int size)
206b569c1c6SDaniel Borkmann {
207425e1ed7SLuc Van Oostenryck 	__le32 *ptr;
208b569c1c6SDaniel Borkmann 	/* We are guaranteed to have aligned memory. */
209b569c1c6SDaniel Borkmann 	for (ptr = area; size >= sizeof(u32); size -= sizeof(u32))
210b569c1c6SDaniel Borkmann 		*ptr++ = cpu_to_le32(AARCH64_BREAK_FAULT);
211b569c1c6SDaniel Borkmann }
212b569c1c6SDaniel Borkmann 
2131dad391dSPuranjay Mohan int bpf_arch_text_invalidate(void *dst, size_t len)
2141dad391dSPuranjay Mohan {
2151dad391dSPuranjay Mohan 	if (!aarch64_insn_set(dst, AARCH64_BREAK_FAULT, len))
2161dad391dSPuranjay Mohan 		return -EINVAL;
2171dad391dSPuranjay Mohan 
2181dad391dSPuranjay Mohan 	return 0;
2191dad391dSPuranjay Mohan }
2201dad391dSPuranjay Mohan 
221e54bcde3SZi Shen Lim static inline int epilogue_offset(const struct jit_ctx *ctx)
222e54bcde3SZi Shen Lim {
22351c9fbb1SZi Shen Lim 	int to = ctx->epilogue_offset;
22451c9fbb1SZi Shen Lim 	int from = ctx->idx;
225e54bcde3SZi Shen Lim 
226e54bcde3SZi Shen Lim 	return to - from;
227e54bcde3SZi Shen Lim }
228e54bcde3SZi Shen Lim 
229fd868f14SLuke Nelson static bool is_addsub_imm(u32 imm)
230fd868f14SLuke Nelson {
231fd868f14SLuke Nelson 	/* Either imm12 or shifted imm12. */
232fd868f14SLuke Nelson 	return !(imm & ~0xfff) || !(imm & ~0xfff000);
233fd868f14SLuke Nelson }
234fd868f14SLuke Nelson 
2357db6c0f1SXu Kuohai /*
2367db6c0f1SXu Kuohai  * There are 3 types of AArch64 LDR/STR (immediate) instruction:
2377db6c0f1SXu Kuohai  * Post-index, Pre-index, Unsigned offset.
2387db6c0f1SXu Kuohai  *
2397db6c0f1SXu Kuohai  * For BPF ldr/str, the "unsigned offset" type is sufficient.
2407db6c0f1SXu Kuohai  *
2417db6c0f1SXu Kuohai  * "Unsigned offset" type LDR(immediate) format:
2427db6c0f1SXu Kuohai  *
2437db6c0f1SXu Kuohai  *    3                   2                   1                   0
2447db6c0f1SXu Kuohai  *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
2457db6c0f1SXu Kuohai  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2467db6c0f1SXu Kuohai  * |x x|1 1 1 0 0 1 0 1|         imm12         |    Rn   |    Rt   |
2477db6c0f1SXu Kuohai  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2487db6c0f1SXu Kuohai  * scale
2497db6c0f1SXu Kuohai  *
2507db6c0f1SXu Kuohai  * "Unsigned offset" type STR(immediate) format:
2517db6c0f1SXu Kuohai  *    3                   2                   1                   0
2527db6c0f1SXu Kuohai  *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
2537db6c0f1SXu Kuohai  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2547db6c0f1SXu Kuohai  * |x x|1 1 1 0 0 1 0 0|         imm12         |    Rn   |    Rt   |
2557db6c0f1SXu Kuohai  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2567db6c0f1SXu Kuohai  * scale
2577db6c0f1SXu Kuohai  *
2587db6c0f1SXu Kuohai  * The offset is calculated from imm12 and scale in the following way:
2597db6c0f1SXu Kuohai  *
2607db6c0f1SXu Kuohai  * offset = (u64)imm12 << scale
2617db6c0f1SXu Kuohai  */
2625b3d19b9SXu Kuohai static bool is_lsi_offset(int offset, int scale)
2637db6c0f1SXu Kuohai {
2647db6c0f1SXu Kuohai 	if (offset < 0)
2657db6c0f1SXu Kuohai 		return false;
2667db6c0f1SXu Kuohai 
2677db6c0f1SXu Kuohai 	if (offset > (0xFFF << scale))
2687db6c0f1SXu Kuohai 		return false;
2697db6c0f1SXu Kuohai 
2707db6c0f1SXu Kuohai 	if (offset & ((1 << scale) - 1))
2717db6c0f1SXu Kuohai 		return false;
2727db6c0f1SXu Kuohai 
2737db6c0f1SXu Kuohai 	return true;
2747db6c0f1SXu Kuohai }
2757db6c0f1SXu Kuohai 
276b2ad54e1SXu Kuohai /* generated prologue:
277b2ad54e1SXu Kuohai  *      bti c // if CONFIG_ARM64_BTI_KERNEL
278b2ad54e1SXu Kuohai  *      mov x9, lr
279b2ad54e1SXu Kuohai  *      nop  // POKE_OFFSET
280b2ad54e1SXu Kuohai  *      paciasp // if CONFIG_ARM64_PTR_AUTH_KERNEL
281b2ad54e1SXu Kuohai  *      stp x29, lr, [sp, #-16]!
282b2ad54e1SXu Kuohai  *      mov x29, sp
283b2ad54e1SXu Kuohai  *      stp x19, x20, [sp, #-16]!
284b2ad54e1SXu Kuohai  *      stp x21, x22, [sp, #-16]!
285b2ad54e1SXu Kuohai  *      stp x25, x26, [sp, #-16]!
286b2ad54e1SXu Kuohai  *      stp x27, x28, [sp, #-16]!
287b2ad54e1SXu Kuohai  *      mov x25, sp
288b2ad54e1SXu Kuohai  *      mov tcc, #0
289b2ad54e1SXu Kuohai  *      // PROLOGUE_OFFSET
290b2ad54e1SXu Kuohai  */
291b2ad54e1SXu Kuohai 
292b2ad54e1SXu Kuohai #define BTI_INSNS (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) ? 1 : 0)
293b2ad54e1SXu Kuohai #define PAC_INSNS (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) ? 1 : 0)
294b2ad54e1SXu Kuohai 
295b2ad54e1SXu Kuohai /* Offset of nop instruction in bpf prog entry to be poked */
296b2ad54e1SXu Kuohai #define POKE_OFFSET (BTI_INSNS + 1)
297b2ad54e1SXu Kuohai 
298a2284d91SDaniel Borkmann /* Tail call offset to jump into */
299b2ad54e1SXu Kuohai #define PROLOGUE_OFFSET (BTI_INSNS + 2 + PAC_INSNS + 8)
300ddb55992SZi Shen Lim 
30122fc0e80SPuranjay Mohan static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf,
302339af577SPuranjay Mohan 			  bool is_exception_cb, u64 arena_vm_start)
303e54bcde3SZi Shen Lim {
304f1c9eed7SDaniel Borkmann 	const struct bpf_prog *prog = ctx->prog;
3059af27da6SKumar Kartikeya Dwivedi 	const bool is_main_prog = !bpf_is_subprog(prog);
306e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
307e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
308e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
309e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
310e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
311ddb55992SZi Shen Lim 	const u8 tcc = bpf2a64[TCALL_CNT];
3125b3d19b9SXu Kuohai 	const u8 fpb = bpf2a64[FP_BOTTOM];
313339af577SPuranjay Mohan 	const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
314ddb55992SZi Shen Lim 	const int idx0 = ctx->idx;
315ddb55992SZi Shen Lim 	int cur_offset;
316e54bcde3SZi Shen Lim 
317ec0738dbSYang Shi 	/*
318ec0738dbSYang Shi 	 * BPF prog stack layout
319ec0738dbSYang Shi 	 *
320ec0738dbSYang Shi 	 *                         high
321ec0738dbSYang Shi 	 * original A64_SP =>   0:+-----+ BPF prologue
322ec0738dbSYang Shi 	 *                        |FP/LR|
323ec0738dbSYang Shi 	 * current A64_FP =>  -16:+-----+
324ec0738dbSYang Shi 	 *                        | ... | callee saved registers
3254c1cd4fdSYang Shi 	 * BPF fp register => -64:+-----+ <= (BPF_FP)
326ec0738dbSYang Shi 	 *                        |     |
327ec0738dbSYang Shi 	 *                        | ... | BPF prog stack
328ec0738dbSYang Shi 	 *                        |     |
329f1c9eed7SDaniel Borkmann 	 *                        +-----+ <= (BPF_FP - prog->aux->stack_depth)
33009ece3d0SDaniel Borkmann 	 *                        |RSVD | padding
331f1c9eed7SDaniel Borkmann 	 * current A64_SP =>      +-----+ <= (BPF_FP - ctx->stack_size)
332ec0738dbSYang Shi 	 *                        |     |
333ec0738dbSYang Shi 	 *                        | ... | Function call stack
334ec0738dbSYang Shi 	 *                        |     |
335ec0738dbSYang Shi 	 *                        +-----+
336ec0738dbSYang Shi 	 *                          low
337ec0738dbSYang Shi 	 *
338ec0738dbSYang Shi 	 */
339ec0738dbSYang Shi 
340a3f25d61SAlexander Duyck 	/* bpf function may be invoked by 3 instruction types:
341a3f25d61SAlexander Duyck 	 * 1. bl, attached via freplace to bpf prog via short jump
342a3f25d61SAlexander Duyck 	 * 2. br, attached via freplace to bpf prog via long jump
343a3f25d61SAlexander Duyck 	 * 3. blr, working as a function pointer, used by emit_call.
344a3f25d61SAlexander Duyck 	 * So BTI_JC should used here to support both br and blr.
345a3f25d61SAlexander Duyck 	 */
346a3f25d61SAlexander Duyck 	emit_bti(A64_BTI_JC, ctx);
347b2ad54e1SXu Kuohai 
348b2ad54e1SXu Kuohai 	emit(A64_MOV(1, A64_R(9), A64_LR), ctx);
349b2ad54e1SXu Kuohai 	emit(A64_NOP, ctx);
350b2ad54e1SXu Kuohai 
35122fc0e80SPuranjay Mohan 	if (!is_exception_cb) {
352042152c2SXu Kuohai 		/* Sign lr */
353042152c2SXu Kuohai 		if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
354042152c2SXu Kuohai 			emit(A64_PACIASP, ctx);
355ec0738dbSYang Shi 		/* Save FP and LR registers to stay align with ARM64 AAPCS */
356ec0738dbSYang Shi 		emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
357ec0738dbSYang Shi 		emit(A64_MOV(1, A64_FP, A64_SP), ctx);
358ec0738dbSYang Shi 
359ddb55992SZi Shen Lim 		/* Save callee-saved registers */
360e54bcde3SZi Shen Lim 		emit(A64_PUSH(r6, r7, A64_SP), ctx);
361e54bcde3SZi Shen Lim 		emit(A64_PUSH(r8, r9, A64_SP), ctx);
362ddb55992SZi Shen Lim 		emit(A64_PUSH(fp, tcc, A64_SP), ctx);
3635b3d19b9SXu Kuohai 		emit(A64_PUSH(fpb, A64_R(28), A64_SP), ctx);
36422fc0e80SPuranjay Mohan 	} else {
36522fc0e80SPuranjay Mohan 		/*
36622fc0e80SPuranjay Mohan 		 * Exception callback receives FP of Main Program as third
36722fc0e80SPuranjay Mohan 		 * parameter
36822fc0e80SPuranjay Mohan 		 */
36922fc0e80SPuranjay Mohan 		emit(A64_MOV(1, A64_FP, A64_R(2)), ctx);
37022fc0e80SPuranjay Mohan 		/*
37122fc0e80SPuranjay Mohan 		 * Main Program already pushed the frame record and the
37222fc0e80SPuranjay Mohan 		 * callee-saved registers. The exception callback will not push
37322fc0e80SPuranjay Mohan 		 * anything and re-use the main program's stack.
37422fc0e80SPuranjay Mohan 		 *
37522fc0e80SPuranjay Mohan 		 * 10 registers are on the stack
37622fc0e80SPuranjay Mohan 		 */
37722fc0e80SPuranjay Mohan 		emit(A64_SUB_I(1, A64_SP, A64_FP, 80), ctx);
37822fc0e80SPuranjay Mohan 	}
379e54bcde3SZi Shen Lim 
380ddb55992SZi Shen Lim 	/* Set up BPF prog stack base register */
381e54bcde3SZi Shen Lim 	emit(A64_MOV(1, fp, A64_SP), ctx);
382e54bcde3SZi Shen Lim 
383d4609a5dSJakub Sitnicki 	if (!ebpf_from_cbpf && is_main_prog) {
384ddb55992SZi Shen Lim 		/* Initialize tail_call_cnt */
385ddb55992SZi Shen Lim 		emit(A64_MOVZ(1, tcc, 0, 0), ctx);
386ddb55992SZi Shen Lim 
387ddb55992SZi Shen Lim 		cur_offset = ctx->idx - idx0;
388ddb55992SZi Shen Lim 		if (cur_offset != PROLOGUE_OFFSET) {
389ddb55992SZi Shen Lim 			pr_err_once("PROLOGUE_OFFSET = %d, expected %d!\n",
390ddb55992SZi Shen Lim 				    cur_offset, PROLOGUE_OFFSET);
391ddb55992SZi Shen Lim 			return -1;
392ddb55992SZi Shen Lim 		}
393fa76cfe6SMark Brown 
394fa76cfe6SMark Brown 		/* BTI landing pad for the tail call, done with a BR */
395b2ad54e1SXu Kuohai 		emit_bti(A64_BTI_J, ctx);
39656ea6a8bSDaniel Borkmann 	}
397a2284d91SDaniel Borkmann 
39822fc0e80SPuranjay Mohan 	/*
39922fc0e80SPuranjay Mohan 	 * Program acting as exception boundary should save all ARM64
40022fc0e80SPuranjay Mohan 	 * Callee-saved registers as the exception callback needs to recover
40122fc0e80SPuranjay Mohan 	 * all ARM64 Callee-saved registers in its epilogue.
40222fc0e80SPuranjay Mohan 	 */
40322fc0e80SPuranjay Mohan 	if (prog->aux->exception_boundary) {
40422fc0e80SPuranjay Mohan 		/*
40522fc0e80SPuranjay Mohan 		 * As we are pushing two more registers, BPF_FP should be moved
40622fc0e80SPuranjay Mohan 		 * 16 bytes
40722fc0e80SPuranjay Mohan 		 */
40822fc0e80SPuranjay Mohan 		emit(A64_SUB_I(1, fp, fp, 16), ctx);
40922fc0e80SPuranjay Mohan 		emit(A64_PUSH(A64_R(23), A64_R(24), A64_SP), ctx);
41022fc0e80SPuranjay Mohan 	}
41122fc0e80SPuranjay Mohan 
4125b3d19b9SXu Kuohai 	emit(A64_SUB_I(1, fpb, fp, ctx->fpb_offset), ctx);
4135b3d19b9SXu Kuohai 
4143f287098STiezhu Yang 	/* Stack must be multiples of 16B */
4153f287098STiezhu Yang 	ctx->stack_size = round_up(prog->aux->stack_depth, 16);
416a2284d91SDaniel Borkmann 
417a2284d91SDaniel Borkmann 	/* Set up function call stack */
418a2284d91SDaniel Borkmann 	emit(A64_SUB_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
419339af577SPuranjay Mohan 
420339af577SPuranjay Mohan 	if (arena_vm_start)
421339af577SPuranjay Mohan 		emit_a64_mov_i64(arena_vm_base, arena_vm_start, ctx);
422339af577SPuranjay Mohan 
423ddb55992SZi Shen Lim 	return 0;
424ddb55992SZi Shen Lim }
425ddb55992SZi Shen Lim 
426ddb55992SZi Shen Lim static int out_offset = -1; /* initialized on the first pass of build_body() */
427ddb55992SZi Shen Lim static int emit_bpf_tail_call(struct jit_ctx *ctx)
428ddb55992SZi Shen Lim {
429ddb55992SZi Shen Lim 	/* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */
430ddb55992SZi Shen Lim 	const u8 r2 = bpf2a64[BPF_REG_2];
431ddb55992SZi Shen Lim 	const u8 r3 = bpf2a64[BPF_REG_3];
432ddb55992SZi Shen Lim 
433ddb55992SZi Shen Lim 	const u8 tmp = bpf2a64[TMP_REG_1];
434ddb55992SZi Shen Lim 	const u8 prg = bpf2a64[TMP_REG_2];
435ddb55992SZi Shen Lim 	const u8 tcc = bpf2a64[TCALL_CNT];
436ddb55992SZi Shen Lim 	const int idx0 = ctx->idx;
437ddb55992SZi Shen Lim #define cur_offset (ctx->idx - idx0)
438ddb55992SZi Shen Lim #define jmp_offset (out_offset - (cur_offset))
439ddb55992SZi Shen Lim 	size_t off;
440ddb55992SZi Shen Lim 
441ddb55992SZi Shen Lim 	/* if (index >= array->map.max_entries)
442ddb55992SZi Shen Lim 	 *     goto out;
443ddb55992SZi Shen Lim 	 */
444ddb55992SZi Shen Lim 	off = offsetof(struct bpf_array, map.max_entries);
445ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, off, ctx);
446ddb55992SZi Shen Lim 	emit(A64_LDR32(tmp, r2, tmp), ctx);
44716338a9bSDaniel Borkmann 	emit(A64_MOV(0, r3, r3), ctx);
448ddb55992SZi Shen Lim 	emit(A64_CMP(0, r3, tmp), ctx);
44916338a9bSDaniel Borkmann 	emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
450ddb55992SZi Shen Lim 
451ebf7f6f0STiezhu Yang 	/*
452ebf7f6f0STiezhu Yang 	 * if (tail_call_cnt >= MAX_TAIL_CALL_CNT)
453ddb55992SZi Shen Lim 	 *     goto out;
454ddb55992SZi Shen Lim 	 * tail_call_cnt++;
455ddb55992SZi Shen Lim 	 */
456ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, MAX_TAIL_CALL_CNT, ctx);
457ddb55992SZi Shen Lim 	emit(A64_CMP(1, tcc, tmp), ctx);
458ebf7f6f0STiezhu Yang 	emit(A64_B_(A64_COND_CS, jmp_offset), ctx);
459ddb55992SZi Shen Lim 	emit(A64_ADD_I(1, tcc, tcc, 1), ctx);
460ddb55992SZi Shen Lim 
461ddb55992SZi Shen Lim 	/* prog = array->ptrs[index];
462ddb55992SZi Shen Lim 	 * if (prog == NULL)
463ddb55992SZi Shen Lim 	 *     goto out;
464ddb55992SZi Shen Lim 	 */
465ddb55992SZi Shen Lim 	off = offsetof(struct bpf_array, ptrs);
466ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, off, ctx);
467d8b54110SDaniel Borkmann 	emit(A64_ADD(1, tmp, r2, tmp), ctx);
468d8b54110SDaniel Borkmann 	emit(A64_LSL(1, prg, r3, 3), ctx);
469d8b54110SDaniel Borkmann 	emit(A64_LDR64(prg, tmp, prg), ctx);
470ddb55992SZi Shen Lim 	emit(A64_CBZ(1, prg, jmp_offset), ctx);
471ddb55992SZi Shen Lim 
472a2284d91SDaniel Borkmann 	/* goto *(prog->bpf_func + prologue_offset); */
473ddb55992SZi Shen Lim 	off = offsetof(struct bpf_prog, bpf_func);
474ddb55992SZi Shen Lim 	emit_a64_mov_i64(tmp, off, ctx);
475ddb55992SZi Shen Lim 	emit(A64_LDR64(tmp, prg, tmp), ctx);
476ddb55992SZi Shen Lim 	emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx);
477a2284d91SDaniel Borkmann 	emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
478ddb55992SZi Shen Lim 	emit(A64_BR(tmp), ctx);
479ddb55992SZi Shen Lim 
480ddb55992SZi Shen Lim 	/* out: */
481ddb55992SZi Shen Lim 	if (out_offset == -1)
482ddb55992SZi Shen Lim 		out_offset = cur_offset;
483ddb55992SZi Shen Lim 	if (cur_offset != out_offset) {
484ddb55992SZi Shen Lim 		pr_err_once("tail_call out_offset = %d, expected %d!\n",
485ddb55992SZi Shen Lim 			    cur_offset, out_offset);
486ddb55992SZi Shen Lim 		return -1;
487ddb55992SZi Shen Lim 	}
488ddb55992SZi Shen Lim 	return 0;
489ddb55992SZi Shen Lim #undef cur_offset
490ddb55992SZi Shen Lim #undef jmp_offset
491e54bcde3SZi Shen Lim }
492e54bcde3SZi Shen Lim 
4931902472bSHou Tao #ifdef CONFIG_ARM64_LSE_ATOMICS
4941902472bSHou Tao static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
4951902472bSHou Tao {
4961902472bSHou Tao 	const u8 code = insn->code;
497e612b5c1SPuranjay Mohan 	const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
4981902472bSHou Tao 	const u8 dst = bpf2a64[insn->dst_reg];
4991902472bSHou Tao 	const u8 src = bpf2a64[insn->src_reg];
5001902472bSHou Tao 	const u8 tmp = bpf2a64[TMP_REG_1];
5011902472bSHou Tao 	const u8 tmp2 = bpf2a64[TMP_REG_2];
5021902472bSHou Tao 	const bool isdw = BPF_SIZE(code) == BPF_DW;
503e612b5c1SPuranjay Mohan 	const bool arena = BPF_MODE(code) == BPF_PROBE_ATOMIC;
5041902472bSHou Tao 	const s16 off = insn->off;
505e612b5c1SPuranjay Mohan 	u8 reg = dst;
5061902472bSHou Tao 
507e612b5c1SPuranjay Mohan 	if (off || arena) {
508e612b5c1SPuranjay Mohan 		if (off) {
5091902472bSHou Tao 			emit_a64_mov_i(1, tmp, off, ctx);
5101902472bSHou Tao 			emit(A64_ADD(1, tmp, tmp, dst), ctx);
5111902472bSHou Tao 			reg = tmp;
5121902472bSHou Tao 		}
513e612b5c1SPuranjay Mohan 		if (arena) {
514e612b5c1SPuranjay Mohan 			emit(A64_ADD(1, tmp, reg, arena_vm_base), ctx);
515e612b5c1SPuranjay Mohan 			reg = tmp;
516e612b5c1SPuranjay Mohan 		}
517e612b5c1SPuranjay Mohan 	}
5181902472bSHou Tao 
5191902472bSHou Tao 	switch (insn->imm) {
5201902472bSHou Tao 	/* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
5211902472bSHou Tao 	case BPF_ADD:
5221902472bSHou Tao 		emit(A64_STADD(isdw, reg, src), ctx);
5231902472bSHou Tao 		break;
5241902472bSHou Tao 	case BPF_AND:
5251902472bSHou Tao 		emit(A64_MVN(isdw, tmp2, src), ctx);
5261902472bSHou Tao 		emit(A64_STCLR(isdw, reg, tmp2), ctx);
5271902472bSHou Tao 		break;
5281902472bSHou Tao 	case BPF_OR:
5291902472bSHou Tao 		emit(A64_STSET(isdw, reg, src), ctx);
5301902472bSHou Tao 		break;
5311902472bSHou Tao 	case BPF_XOR:
5321902472bSHou Tao 		emit(A64_STEOR(isdw, reg, src), ctx);
5331902472bSHou Tao 		break;
5341902472bSHou Tao 	/* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
5351902472bSHou Tao 	case BPF_ADD | BPF_FETCH:
5361902472bSHou Tao 		emit(A64_LDADDAL(isdw, src, reg, src), ctx);
5371902472bSHou Tao 		break;
5381902472bSHou Tao 	case BPF_AND | BPF_FETCH:
5391902472bSHou Tao 		emit(A64_MVN(isdw, tmp2, src), ctx);
5401902472bSHou Tao 		emit(A64_LDCLRAL(isdw, src, reg, tmp2), ctx);
5411902472bSHou Tao 		break;
5421902472bSHou Tao 	case BPF_OR | BPF_FETCH:
5431902472bSHou Tao 		emit(A64_LDSETAL(isdw, src, reg, src), ctx);
5441902472bSHou Tao 		break;
5451902472bSHou Tao 	case BPF_XOR | BPF_FETCH:
5461902472bSHou Tao 		emit(A64_LDEORAL(isdw, src, reg, src), ctx);
5471902472bSHou Tao 		break;
5481902472bSHou Tao 	/* src_reg = atomic_xchg(dst_reg + off, src_reg); */
5491902472bSHou Tao 	case BPF_XCHG:
5501902472bSHou Tao 		emit(A64_SWPAL(isdw, src, reg, src), ctx);
5511902472bSHou Tao 		break;
5521902472bSHou Tao 	/* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
5531902472bSHou Tao 	case BPF_CMPXCHG:
5541902472bSHou Tao 		emit(A64_CASAL(isdw, src, reg, bpf2a64[BPF_REG_0]), ctx);
5551902472bSHou Tao 		break;
5561902472bSHou Tao 	default:
5571902472bSHou Tao 		pr_err_once("unknown atomic op code %02x\n", insn->imm);
5581902472bSHou Tao 		return -EINVAL;
5591902472bSHou Tao 	}
5601902472bSHou Tao 
5611902472bSHou Tao 	return 0;
5621902472bSHou Tao }
5631902472bSHou Tao #else
5641902472bSHou Tao static inline int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
5651902472bSHou Tao {
5661902472bSHou Tao 	return -EINVAL;
5671902472bSHou Tao }
5681902472bSHou Tao #endif
5691902472bSHou Tao 
5701902472bSHou Tao static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
5711902472bSHou Tao {
5721902472bSHou Tao 	const u8 code = insn->code;
5731902472bSHou Tao 	const u8 dst = bpf2a64[insn->dst_reg];
5741902472bSHou Tao 	const u8 src = bpf2a64[insn->src_reg];
5751902472bSHou Tao 	const u8 tmp = bpf2a64[TMP_REG_1];
5761902472bSHou Tao 	const u8 tmp2 = bpf2a64[TMP_REG_2];
5771902472bSHou Tao 	const u8 tmp3 = bpf2a64[TMP_REG_3];
5781902472bSHou Tao 	const int i = insn - ctx->prog->insnsi;
5791902472bSHou Tao 	const s32 imm = insn->imm;
5801902472bSHou Tao 	const s16 off = insn->off;
5811902472bSHou Tao 	const bool isdw = BPF_SIZE(code) == BPF_DW;
5821902472bSHou Tao 	u8 reg;
5831902472bSHou Tao 	s32 jmp_offset;
5841902472bSHou Tao 
585e612b5c1SPuranjay Mohan 	if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
586e612b5c1SPuranjay Mohan 		/* ll_sc based atomics don't support unsafe pointers yet. */
587e612b5c1SPuranjay Mohan 		pr_err_once("unknown atomic opcode %02x\n", code);
588e612b5c1SPuranjay Mohan 		return -EINVAL;
589e612b5c1SPuranjay Mohan 	}
590e612b5c1SPuranjay Mohan 
5911902472bSHou Tao 	if (!off) {
5921902472bSHou Tao 		reg = dst;
5931902472bSHou Tao 	} else {
5941902472bSHou Tao 		emit_a64_mov_i(1, tmp, off, ctx);
5951902472bSHou Tao 		emit(A64_ADD(1, tmp, tmp, dst), ctx);
5961902472bSHou Tao 		reg = tmp;
5971902472bSHou Tao 	}
5981902472bSHou Tao 
5991902472bSHou Tao 	if (imm == BPF_ADD || imm == BPF_AND ||
6001902472bSHou Tao 	    imm == BPF_OR || imm == BPF_XOR) {
6011902472bSHou Tao 		/* lock *(u32/u64 *)(dst_reg + off) <op>= src_reg */
6021902472bSHou Tao 		emit(A64_LDXR(isdw, tmp2, reg), ctx);
6031902472bSHou Tao 		if (imm == BPF_ADD)
6041902472bSHou Tao 			emit(A64_ADD(isdw, tmp2, tmp2, src), ctx);
6051902472bSHou Tao 		else if (imm == BPF_AND)
6061902472bSHou Tao 			emit(A64_AND(isdw, tmp2, tmp2, src), ctx);
6071902472bSHou Tao 		else if (imm == BPF_OR)
6081902472bSHou Tao 			emit(A64_ORR(isdw, tmp2, tmp2, src), ctx);
6091902472bSHou Tao 		else
6101902472bSHou Tao 			emit(A64_EOR(isdw, tmp2, tmp2, src), ctx);
6111902472bSHou Tao 		emit(A64_STXR(isdw, tmp2, reg, tmp3), ctx);
6121902472bSHou Tao 		jmp_offset = -3;
6131902472bSHou Tao 		check_imm19(jmp_offset);
6141902472bSHou Tao 		emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
6151902472bSHou Tao 	} else if (imm == (BPF_ADD | BPF_FETCH) ||
6161902472bSHou Tao 		   imm == (BPF_AND | BPF_FETCH) ||
6171902472bSHou Tao 		   imm == (BPF_OR | BPF_FETCH) ||
6181902472bSHou Tao 		   imm == (BPF_XOR | BPF_FETCH)) {
6191902472bSHou Tao 		/* src_reg = atomic_fetch_<op>(dst_reg + off, src_reg) */
6201902472bSHou Tao 		const u8 ax = bpf2a64[BPF_REG_AX];
6211902472bSHou Tao 
6221902472bSHou Tao 		emit(A64_MOV(isdw, ax, src), ctx);
6231902472bSHou Tao 		emit(A64_LDXR(isdw, src, reg), ctx);
6241902472bSHou Tao 		if (imm == (BPF_ADD | BPF_FETCH))
6251902472bSHou Tao 			emit(A64_ADD(isdw, tmp2, src, ax), ctx);
6261902472bSHou Tao 		else if (imm == (BPF_AND | BPF_FETCH))
6271902472bSHou Tao 			emit(A64_AND(isdw, tmp2, src, ax), ctx);
6281902472bSHou Tao 		else if (imm == (BPF_OR | BPF_FETCH))
6291902472bSHou Tao 			emit(A64_ORR(isdw, tmp2, src, ax), ctx);
6301902472bSHou Tao 		else
6311902472bSHou Tao 			emit(A64_EOR(isdw, tmp2, src, ax), ctx);
6321902472bSHou Tao 		emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
6331902472bSHou Tao 		jmp_offset = -3;
6341902472bSHou Tao 		check_imm19(jmp_offset);
6351902472bSHou Tao 		emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
6361902472bSHou Tao 		emit(A64_DMB_ISH, ctx);
6371902472bSHou Tao 	} else if (imm == BPF_XCHG) {
6381902472bSHou Tao 		/* src_reg = atomic_xchg(dst_reg + off, src_reg); */
6391902472bSHou Tao 		emit(A64_MOV(isdw, tmp2, src), ctx);
6401902472bSHou Tao 		emit(A64_LDXR(isdw, src, reg), ctx);
6411902472bSHou Tao 		emit(A64_STLXR(isdw, tmp2, reg, tmp3), ctx);
6421902472bSHou Tao 		jmp_offset = -2;
6431902472bSHou Tao 		check_imm19(jmp_offset);
6441902472bSHou Tao 		emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
6451902472bSHou Tao 		emit(A64_DMB_ISH, ctx);
6461902472bSHou Tao 	} else if (imm == BPF_CMPXCHG) {
6471902472bSHou Tao 		/* r0 = atomic_cmpxchg(dst_reg + off, r0, src_reg); */
6481902472bSHou Tao 		const u8 r0 = bpf2a64[BPF_REG_0];
6491902472bSHou Tao 
6501902472bSHou Tao 		emit(A64_MOV(isdw, tmp2, r0), ctx);
6511902472bSHou Tao 		emit(A64_LDXR(isdw, r0, reg), ctx);
6521902472bSHou Tao 		emit(A64_EOR(isdw, tmp3, r0, tmp2), ctx);
6531902472bSHou Tao 		jmp_offset = 4;
6541902472bSHou Tao 		check_imm19(jmp_offset);
6551902472bSHou Tao 		emit(A64_CBNZ(isdw, tmp3, jmp_offset), ctx);
6561902472bSHou Tao 		emit(A64_STLXR(isdw, src, reg, tmp3), ctx);
6571902472bSHou Tao 		jmp_offset = -4;
6581902472bSHou Tao 		check_imm19(jmp_offset);
6591902472bSHou Tao 		emit(A64_CBNZ(0, tmp3, jmp_offset), ctx);
6601902472bSHou Tao 		emit(A64_DMB_ISH, ctx);
6611902472bSHou Tao 	} else {
6621902472bSHou Tao 		pr_err_once("unknown atomic op code %02x\n", imm);
6631902472bSHou Tao 		return -EINVAL;
6641902472bSHou Tao 	}
6651902472bSHou Tao 
6661902472bSHou Tao 	return 0;
6671902472bSHou Tao }
6681902472bSHou Tao 
669b2ad54e1SXu Kuohai void dummy_tramp(void);
670b2ad54e1SXu Kuohai 
671b2ad54e1SXu Kuohai asm (
672b2ad54e1SXu Kuohai "	.pushsection .text, \"ax\", @progbits\n"
67333f32e50SNathan Chancellor "	.global dummy_tramp\n"
674b2ad54e1SXu Kuohai "	.type dummy_tramp, %function\n"
675b2ad54e1SXu Kuohai "dummy_tramp:"
676b2ad54e1SXu Kuohai #if IS_ENABLED(CONFIG_ARM64_BTI_KERNEL)
677b2ad54e1SXu Kuohai "	bti j\n" /* dummy_tramp is called via "br x10" */
678b2ad54e1SXu Kuohai #endif
679339ed900SXu Kuohai "	mov x10, x30\n"
680339ed900SXu Kuohai "	mov x30, x9\n"
681b2ad54e1SXu Kuohai "	ret x10\n"
682b2ad54e1SXu Kuohai "	.size dummy_tramp, .-dummy_tramp\n"
683b2ad54e1SXu Kuohai "	.popsection\n"
684b2ad54e1SXu Kuohai );
685b2ad54e1SXu Kuohai 
686b2ad54e1SXu Kuohai /* build a plt initialized like this:
687b2ad54e1SXu Kuohai  *
688b2ad54e1SXu Kuohai  * plt:
689b2ad54e1SXu Kuohai  *      ldr tmp, target
690b2ad54e1SXu Kuohai  *      br tmp
691b2ad54e1SXu Kuohai  * target:
692b2ad54e1SXu Kuohai  *      .quad dummy_tramp
693b2ad54e1SXu Kuohai  *
694b2ad54e1SXu Kuohai  * when a long jump trampoline is attached, target is filled with the
695b2ad54e1SXu Kuohai  * trampoline address, and when the trampoline is removed, target is
696b2ad54e1SXu Kuohai  * restored to dummy_tramp address.
697b2ad54e1SXu Kuohai  */
698b2ad54e1SXu Kuohai static void build_plt(struct jit_ctx *ctx)
699b2ad54e1SXu Kuohai {
700b2ad54e1SXu Kuohai 	const u8 tmp = bpf2a64[TMP_REG_1];
701b2ad54e1SXu Kuohai 	struct bpf_plt *plt = NULL;
702b2ad54e1SXu Kuohai 
703b2ad54e1SXu Kuohai 	/* make sure target is 64-bit aligned */
704b2ad54e1SXu Kuohai 	if ((ctx->idx + PLT_TARGET_OFFSET / AARCH64_INSN_SIZE) % 2)
705b2ad54e1SXu Kuohai 		emit(A64_NOP, ctx);
706b2ad54e1SXu Kuohai 
707b2ad54e1SXu Kuohai 	plt = (struct bpf_plt *)(ctx->image + ctx->idx);
708b2ad54e1SXu Kuohai 	/* plt is called via bl, no BTI needed here */
709b2ad54e1SXu Kuohai 	emit(A64_LDR64LIT(tmp, 2 * AARCH64_INSN_SIZE), ctx);
710b2ad54e1SXu Kuohai 	emit(A64_BR(tmp), ctx);
711b2ad54e1SXu Kuohai 
712b2ad54e1SXu Kuohai 	if (ctx->image)
713b2ad54e1SXu Kuohai 		plt->target = (u64)&dummy_tramp;
714b2ad54e1SXu Kuohai }
715b2ad54e1SXu Kuohai 
71622fc0e80SPuranjay Mohan static void build_epilogue(struct jit_ctx *ctx, bool is_exception_cb)
717e54bcde3SZi Shen Lim {
718e54bcde3SZi Shen Lim 	const u8 r0 = bpf2a64[BPF_REG_0];
719e54bcde3SZi Shen Lim 	const u8 r6 = bpf2a64[BPF_REG_6];
720e54bcde3SZi Shen Lim 	const u8 r7 = bpf2a64[BPF_REG_7];
721e54bcde3SZi Shen Lim 	const u8 r8 = bpf2a64[BPF_REG_8];
722e54bcde3SZi Shen Lim 	const u8 r9 = bpf2a64[BPF_REG_9];
723e54bcde3SZi Shen Lim 	const u8 fp = bpf2a64[BPF_REG_FP];
7245b3d19b9SXu Kuohai 	const u8 fpb = bpf2a64[FP_BOTTOM];
725e54bcde3SZi Shen Lim 
726e54bcde3SZi Shen Lim 	/* We're done with BPF stack */
727f1c9eed7SDaniel Borkmann 	emit(A64_ADD_I(1, A64_SP, A64_SP, ctx->stack_size), ctx);
728e54bcde3SZi Shen Lim 
72922fc0e80SPuranjay Mohan 	/*
73022fc0e80SPuranjay Mohan 	 * Program acting as exception boundary pushes R23 and R24 in addition
73122fc0e80SPuranjay Mohan 	 * to BPF callee-saved registers. Exception callback uses the boundary
73222fc0e80SPuranjay Mohan 	 * program's stack frame, so recover these extra registers in the above
73322fc0e80SPuranjay Mohan 	 * two cases.
73422fc0e80SPuranjay Mohan 	 */
73522fc0e80SPuranjay Mohan 	if (ctx->prog->aux->exception_boundary || is_exception_cb)
73622fc0e80SPuranjay Mohan 		emit(A64_POP(A64_R(23), A64_R(24), A64_SP), ctx);
73722fc0e80SPuranjay Mohan 
7385b3d19b9SXu Kuohai 	/* Restore x27 and x28 */
7395b3d19b9SXu Kuohai 	emit(A64_POP(fpb, A64_R(28), A64_SP), ctx);
740ec0738dbSYang Shi 	/* Restore fs (x25) and x26 */
741ec0738dbSYang Shi 	emit(A64_POP(fp, A64_R(26), A64_SP), ctx);
742ec0738dbSYang Shi 
743e54bcde3SZi Shen Lim 	/* Restore callee-saved register */
744e54bcde3SZi Shen Lim 	emit(A64_POP(r8, r9, A64_SP), ctx);
745e54bcde3SZi Shen Lim 	emit(A64_POP(r6, r7, A64_SP), ctx);
746e54bcde3SZi Shen Lim 
747ec0738dbSYang Shi 	/* Restore FP/LR registers */
748ec0738dbSYang Shi 	emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
749e54bcde3SZi Shen Lim 
750e54bcde3SZi Shen Lim 	/* Set return value */
751e54bcde3SZi Shen Lim 	emit(A64_MOV(1, A64_R(0), r0), ctx);
752e54bcde3SZi Shen Lim 
753042152c2SXu Kuohai 	/* Authenticate lr */
754042152c2SXu Kuohai 	if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
755042152c2SXu Kuohai 		emit(A64_AUTIASP, ctx);
756042152c2SXu Kuohai 
757e54bcde3SZi Shen Lim 	emit(A64_RET(A64_LR), ctx);
758e54bcde3SZi Shen Lim }
759e54bcde3SZi Shen Lim 
76080083428SJean-Philippe Brucker #define BPF_FIXUP_OFFSET_MASK	GENMASK(26, 0)
76180083428SJean-Philippe Brucker #define BPF_FIXUP_REG_MASK	GENMASK(31, 27)
762339af577SPuranjay Mohan #define DONT_CLEAR 5 /* Unused ARM64 register from BPF's POV */
76380083428SJean-Philippe Brucker 
764d6e2cc56SMark Rutland bool ex_handler_bpf(const struct exception_table_entry *ex,
76580083428SJean-Philippe Brucker 		    struct pt_regs *regs)
76680083428SJean-Philippe Brucker {
76780083428SJean-Philippe Brucker 	off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
76880083428SJean-Philippe Brucker 	int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
76980083428SJean-Philippe Brucker 
770339af577SPuranjay Mohan 	if (dst_reg != DONT_CLEAR)
77180083428SJean-Philippe Brucker 		regs->regs[dst_reg] = 0;
77280083428SJean-Philippe Brucker 	regs->pc = (unsigned long)&ex->fixup - offset;
773e8c328d7SMark Rutland 	return true;
77480083428SJean-Philippe Brucker }
77580083428SJean-Philippe Brucker 
77680083428SJean-Philippe Brucker /* For accesses to BTF pointers, add an entry to the exception table */
77780083428SJean-Philippe Brucker static int add_exception_handler(const struct bpf_insn *insn,
77880083428SJean-Philippe Brucker 				 struct jit_ctx *ctx,
77980083428SJean-Philippe Brucker 				 int dst_reg)
78080083428SJean-Philippe Brucker {
7811dad391dSPuranjay Mohan 	off_t ins_offset;
7821dad391dSPuranjay Mohan 	off_t fixup_offset;
78380083428SJean-Philippe Brucker 	unsigned long pc;
78480083428SJean-Philippe Brucker 	struct exception_table_entry *ex;
78580083428SJean-Philippe Brucker 
78680083428SJean-Philippe Brucker 	if (!ctx->image)
78780083428SJean-Philippe Brucker 		/* First pass */
78880083428SJean-Philippe Brucker 		return 0;
78980083428SJean-Philippe Brucker 
790cc88f540SXu Kuohai 	if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
791339af577SPuranjay Mohan 		BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
792e612b5c1SPuranjay Mohan 			BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
793e612b5c1SPuranjay Mohan 				BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
79480083428SJean-Philippe Brucker 		return 0;
79580083428SJean-Philippe Brucker 
79680083428SJean-Philippe Brucker 	if (!ctx->prog->aux->extable ||
79780083428SJean-Philippe Brucker 	    WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
79880083428SJean-Philippe Brucker 		return -EINVAL;
79980083428SJean-Philippe Brucker 
80080083428SJean-Philippe Brucker 	ex = &ctx->prog->aux->extable[ctx->exentry_idx];
8011dad391dSPuranjay Mohan 	pc = (unsigned long)&ctx->ro_image[ctx->idx - 1];
80280083428SJean-Philippe Brucker 
8031dad391dSPuranjay Mohan 	/*
8041dad391dSPuranjay Mohan 	 * This is the relative offset of the instruction that may fault from
8051dad391dSPuranjay Mohan 	 * the exception table itself. This will be written to the exception
8061dad391dSPuranjay Mohan 	 * table and if this instruction faults, the destination register will
8071dad391dSPuranjay Mohan 	 * be set to '0' and the execution will jump to the next instruction.
8081dad391dSPuranjay Mohan 	 */
8091dad391dSPuranjay Mohan 	ins_offset = pc - (long)&ex->insn;
8101dad391dSPuranjay Mohan 	if (WARN_ON_ONCE(ins_offset >= 0 || ins_offset < INT_MIN))
81180083428SJean-Philippe Brucker 		return -ERANGE;
81280083428SJean-Philippe Brucker 
81380083428SJean-Philippe Brucker 	/*
81480083428SJean-Philippe Brucker 	 * Since the extable follows the program, the fixup offset is always
81580083428SJean-Philippe Brucker 	 * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
81680083428SJean-Philippe Brucker 	 * to keep things simple, and put the destination register in the upper
81780083428SJean-Philippe Brucker 	 * bits. We don't need to worry about buildtime or runtime sort
81880083428SJean-Philippe Brucker 	 * modifying the upper bits because the table is already sorted, and
81980083428SJean-Philippe Brucker 	 * isn't part of the main exception table.
8201dad391dSPuranjay Mohan 	 *
8211dad391dSPuranjay Mohan 	 * The fixup_offset is set to the next instruction from the instruction
8221dad391dSPuranjay Mohan 	 * that may fault. The execution will jump to this after handling the
8231dad391dSPuranjay Mohan 	 * fault.
82480083428SJean-Philippe Brucker 	 */
8251dad391dSPuranjay Mohan 	fixup_offset = (long)&ex->fixup - (pc + AARCH64_INSN_SIZE);
8261dad391dSPuranjay Mohan 	if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, fixup_offset))
82780083428SJean-Philippe Brucker 		return -ERANGE;
82880083428SJean-Philippe Brucker 
8291dad391dSPuranjay Mohan 	/*
8301dad391dSPuranjay Mohan 	 * The offsets above have been calculated using the RO buffer but we
8311dad391dSPuranjay Mohan 	 * need to use the R/W buffer for writes.
8321dad391dSPuranjay Mohan 	 * switch ex to rw buffer for writing.
8331dad391dSPuranjay Mohan 	 */
8341dad391dSPuranjay Mohan 	ex = (void *)ctx->image + ((void *)ex - (void *)ctx->ro_image);
8351dad391dSPuranjay Mohan 
8361dad391dSPuranjay Mohan 	ex->insn = ins_offset;
8371dad391dSPuranjay Mohan 
838339af577SPuranjay Mohan 	if (BPF_CLASS(insn->code) != BPF_LDX)
839339af577SPuranjay Mohan 		dst_reg = DONT_CLEAR;
840339af577SPuranjay Mohan 
8411dad391dSPuranjay Mohan 	ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, fixup_offset) |
84280083428SJean-Philippe Brucker 		    FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
84380083428SJean-Philippe Brucker 
844d6e2cc56SMark Rutland 	ex->type = EX_TYPE_BPF;
845d6e2cc56SMark Rutland 
84680083428SJean-Philippe Brucker 	ctx->exentry_idx++;
84780083428SJean-Philippe Brucker 	return 0;
84880083428SJean-Philippe Brucker }
84980083428SJean-Philippe Brucker 
85030d3d94cSZi Shen Lim /* JITs an eBPF instruction.
85130d3d94cSZi Shen Lim  * Returns:
85230d3d94cSZi Shen Lim  * 0  - successfully JITed an 8-byte eBPF instruction.
85330d3d94cSZi Shen Lim  * >0 - successfully JITed a 16-byte eBPF instruction.
85430d3d94cSZi Shen Lim  * <0 - failed to JIT.
85530d3d94cSZi Shen Lim  */
8568c11ea5cSDaniel Borkmann static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
8578c11ea5cSDaniel Borkmann 		      bool extra_pass)
858e54bcde3SZi Shen Lim {
859e54bcde3SZi Shen Lim 	const u8 code = insn->code;
860339af577SPuranjay Mohan 	u8 dst = bpf2a64[insn->dst_reg];
861339af577SPuranjay Mohan 	u8 src = bpf2a64[insn->src_reg];
862e54bcde3SZi Shen Lim 	const u8 tmp = bpf2a64[TMP_REG_1];
863e54bcde3SZi Shen Lim 	const u8 tmp2 = bpf2a64[TMP_REG_2];
8645b3d19b9SXu Kuohai 	const u8 fp = bpf2a64[BPF_REG_FP];
8655b3d19b9SXu Kuohai 	const u8 fpb = bpf2a64[FP_BOTTOM];
866339af577SPuranjay Mohan 	const u8 arena_vm_base = bpf2a64[ARENA_VM_START];
867e54bcde3SZi Shen Lim 	const s16 off = insn->off;
868e54bcde3SZi Shen Lim 	const s32 imm = insn->imm;
869e54bcde3SZi Shen Lim 	const int i = insn - ctx->prog->insnsi;
870654b65a0SJiong Wang 	const bool is64 = BPF_CLASS(code) == BPF_ALU64 ||
871654b65a0SJiong Wang 			  BPF_CLASS(code) == BPF_JMP;
8721902472bSHou Tao 	u8 jmp_cond;
873e54bcde3SZi Shen Lim 	s32 jmp_offset;
874fd49591cSLuke Nelson 	u32 a64_insn;
8755b3d19b9SXu Kuohai 	u8 src_adj;
8765b3d19b9SXu Kuohai 	u8 dst_adj;
8775b3d19b9SXu Kuohai 	int off_adj;
87880083428SJean-Philippe Brucker 	int ret;
879cc88f540SXu Kuohai 	bool sign_extend;
880e54bcde3SZi Shen Lim 
881e54bcde3SZi Shen Lim 	switch (code) {
882e54bcde3SZi Shen Lim 	/* dst = src */
883e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_X:
884e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_X:
8854dd31243SPuranjay Mohan 		if (insn_is_cast_user(insn)) {
8864dd31243SPuranjay Mohan 			emit(A64_MOV(0, tmp, src), ctx); // 32-bit mov clears the upper 32 bits
8874dd31243SPuranjay Mohan 			emit_a64_mov_i(0, dst, ctx->user_vm_start >> 32, ctx);
8884dd31243SPuranjay Mohan 			emit(A64_LSL(1, dst, dst, 32), ctx);
8894dd31243SPuranjay Mohan 			emit(A64_CBZ(1, tmp, 2), ctx);
8904dd31243SPuranjay Mohan 			emit(A64_ORR(1, tmp, dst, tmp), ctx);
8914dd31243SPuranjay Mohan 			emit(A64_MOV(1, dst, tmp), ctx);
8924dd31243SPuranjay Mohan 			break;
8937a4c3222SPuranjay Mohan 		} else if (insn_is_mov_percpu_addr(insn)) {
8947a4c3222SPuranjay Mohan 			if (dst != src)
8957a4c3222SPuranjay Mohan 				emit(A64_MOV(1, dst, src), ctx);
8967a4c3222SPuranjay Mohan 			if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
8977a4c3222SPuranjay Mohan 				emit(A64_MRS_TPIDR_EL2(tmp), ctx);
8987a4c3222SPuranjay Mohan 			else
8997a4c3222SPuranjay Mohan 				emit(A64_MRS_TPIDR_EL1(tmp), ctx);
9007a4c3222SPuranjay Mohan 			emit(A64_ADD(1, dst, dst, tmp), ctx);
9017a4c3222SPuranjay Mohan 			break;
9024dd31243SPuranjay Mohan 		}
903bb0a1d6bSXu Kuohai 		switch (insn->off) {
904bb0a1d6bSXu Kuohai 		case 0:
905e54bcde3SZi Shen Lim 			emit(A64_MOV(is64, dst, src), ctx);
906e54bcde3SZi Shen Lim 			break;
907bb0a1d6bSXu Kuohai 		case 8:
908bb0a1d6bSXu Kuohai 			emit(A64_SXTB(is64, dst, src), ctx);
909bb0a1d6bSXu Kuohai 			break;
910bb0a1d6bSXu Kuohai 		case 16:
911bb0a1d6bSXu Kuohai 			emit(A64_SXTH(is64, dst, src), ctx);
912bb0a1d6bSXu Kuohai 			break;
913bb0a1d6bSXu Kuohai 		case 32:
914bb0a1d6bSXu Kuohai 			emit(A64_SXTW(is64, dst, src), ctx);
915bb0a1d6bSXu Kuohai 			break;
916bb0a1d6bSXu Kuohai 		}
917bb0a1d6bSXu Kuohai 		break;
918e54bcde3SZi Shen Lim 	/* dst = dst OP src */
919e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_X:
920e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_X:
921e54bcde3SZi Shen Lim 		emit(A64_ADD(is64, dst, dst, src), ctx);
922e54bcde3SZi Shen Lim 		break;
923e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_X:
924e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_X:
925e54bcde3SZi Shen Lim 		emit(A64_SUB(is64, dst, dst, src), ctx);
926e54bcde3SZi Shen Lim 		break;
927e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_X:
928e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_X:
929e54bcde3SZi Shen Lim 		emit(A64_AND(is64, dst, dst, src), ctx);
930e54bcde3SZi Shen Lim 		break;
931e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_X:
932e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_X:
933e54bcde3SZi Shen Lim 		emit(A64_ORR(is64, dst, dst, src), ctx);
934e54bcde3SZi Shen Lim 		break;
935e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_X:
936e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_X:
937e54bcde3SZi Shen Lim 		emit(A64_EOR(is64, dst, dst, src), ctx);
938e54bcde3SZi Shen Lim 		break;
939e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_X:
940e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_X:
941e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, src), ctx);
942e54bcde3SZi Shen Lim 		break;
943e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_X:
944e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_X:
94568b18191SXu Kuohai 		if (!off)
946e54bcde3SZi Shen Lim 			emit(A64_UDIV(is64, dst, dst, src), ctx);
94768b18191SXu Kuohai 		else
94868b18191SXu Kuohai 			emit(A64_SDIV(is64, dst, dst, src), ctx);
949e54bcde3SZi Shen Lim 		break;
950119220d8STiezhu Yang 	case BPF_ALU | BPF_MOD | BPF_X:
951119220d8STiezhu Yang 	case BPF_ALU64 | BPF_MOD | BPF_X:
95268b18191SXu Kuohai 		if (!off)
953e54bcde3SZi Shen Lim 			emit(A64_UDIV(is64, tmp, dst, src), ctx);
95468b18191SXu Kuohai 		else
95568b18191SXu Kuohai 			emit(A64_SDIV(is64, tmp, dst, src), ctx);
956504792e0SJerin Jacob 		emit(A64_MSUB(is64, dst, dst, tmp, src), ctx);
957e54bcde3SZi Shen Lim 		break;
958d65a634aSZi Shen Lim 	case BPF_ALU | BPF_LSH | BPF_X:
959d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_LSH | BPF_X:
960d65a634aSZi Shen Lim 		emit(A64_LSLV(is64, dst, dst, src), ctx);
961d65a634aSZi Shen Lim 		break;
962d65a634aSZi Shen Lim 	case BPF_ALU | BPF_RSH | BPF_X:
963d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_RSH | BPF_X:
964d65a634aSZi Shen Lim 		emit(A64_LSRV(is64, dst, dst, src), ctx);
965d65a634aSZi Shen Lim 		break;
966d65a634aSZi Shen Lim 	case BPF_ALU | BPF_ARSH | BPF_X:
967d65a634aSZi Shen Lim 	case BPF_ALU64 | BPF_ARSH | BPF_X:
968d65a634aSZi Shen Lim 		emit(A64_ASRV(is64, dst, dst, src), ctx);
969d65a634aSZi Shen Lim 		break;
970e54bcde3SZi Shen Lim 	/* dst = -dst */
971e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_NEG:
972e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_NEG:
973e54bcde3SZi Shen Lim 		emit(A64_NEG(is64, dst, dst), ctx);
974e54bcde3SZi Shen Lim 		break;
975e54bcde3SZi Shen Lim 	/* dst = BSWAP##imm(dst) */
976e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_LE:
977e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_END | BPF_FROM_BE:
9781104247fSXu Kuohai 	case BPF_ALU64 | BPF_END | BPF_FROM_LE:
979e54bcde3SZi Shen Lim #ifdef CONFIG_CPU_BIG_ENDIAN
9801104247fSXu Kuohai 		if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_BE)
981d63903bbSXi Wang 			goto emit_bswap_uxt;
982e54bcde3SZi Shen Lim #else /* !CONFIG_CPU_BIG_ENDIAN */
9831104247fSXu Kuohai 		if (BPF_CLASS(code) == BPF_ALU && BPF_SRC(code) == BPF_FROM_LE)
984d63903bbSXi Wang 			goto emit_bswap_uxt;
985e54bcde3SZi Shen Lim #endif
986e54bcde3SZi Shen Lim 		switch (imm) {
987e54bcde3SZi Shen Lim 		case 16:
988e54bcde3SZi Shen Lim 			emit(A64_REV16(is64, dst, dst), ctx);
989d63903bbSXi Wang 			/* zero-extend 16 bits into 64 bits */
990d63903bbSXi Wang 			emit(A64_UXTH(is64, dst, dst), ctx);
991e54bcde3SZi Shen Lim 			break;
992e54bcde3SZi Shen Lim 		case 32:
993a51cd6bfSArtem Savkov 			emit(A64_REV32(0, dst, dst), ctx);
994d63903bbSXi Wang 			/* upper 32 bits already cleared */
995e54bcde3SZi Shen Lim 			break;
996e54bcde3SZi Shen Lim 		case 64:
997e54bcde3SZi Shen Lim 			emit(A64_REV64(dst, dst), ctx);
998e54bcde3SZi Shen Lim 			break;
999e54bcde3SZi Shen Lim 		}
1000e54bcde3SZi Shen Lim 		break;
1001d63903bbSXi Wang emit_bswap_uxt:
1002d63903bbSXi Wang 		switch (imm) {
1003d63903bbSXi Wang 		case 16:
1004d63903bbSXi Wang 			/* zero-extend 16 bits into 64 bits */
1005d63903bbSXi Wang 			emit(A64_UXTH(is64, dst, dst), ctx);
1006d63903bbSXi Wang 			break;
1007d63903bbSXi Wang 		case 32:
1008d63903bbSXi Wang 			/* zero-extend 32 bits into 64 bits */
1009d63903bbSXi Wang 			emit(A64_UXTW(is64, dst, dst), ctx);
1010d63903bbSXi Wang 			break;
1011d63903bbSXi Wang 		case 64:
1012d63903bbSXi Wang 			/* nop */
1013d63903bbSXi Wang 			break;
1014d63903bbSXi Wang 		}
1015d63903bbSXi Wang 		break;
1016e54bcde3SZi Shen Lim 	/* dst = imm */
1017e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOV | BPF_K:
1018e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOV | BPF_K:
1019e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, dst, imm, ctx);
1020e54bcde3SZi Shen Lim 		break;
1021e54bcde3SZi Shen Lim 	/* dst = dst OP imm */
1022e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ADD | BPF_K:
1023e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ADD | BPF_K:
1024fd868f14SLuke Nelson 		if (is_addsub_imm(imm)) {
1025fd868f14SLuke Nelson 			emit(A64_ADD_I(is64, dst, dst, imm), ctx);
1026fd868f14SLuke Nelson 		} else if (is_addsub_imm(-imm)) {
1027fd868f14SLuke Nelson 			emit(A64_SUB_I(is64, dst, dst, -imm), ctx);
1028fd868f14SLuke Nelson 		} else {
1029e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
1030e54bcde3SZi Shen Lim 			emit(A64_ADD(is64, dst, dst, tmp), ctx);
1031fd868f14SLuke Nelson 		}
1032e54bcde3SZi Shen Lim 		break;
1033e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_SUB | BPF_K:
1034e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_SUB | BPF_K:
1035fd868f14SLuke Nelson 		if (is_addsub_imm(imm)) {
1036fd868f14SLuke Nelson 			emit(A64_SUB_I(is64, dst, dst, imm), ctx);
1037fd868f14SLuke Nelson 		} else if (is_addsub_imm(-imm)) {
1038fd868f14SLuke Nelson 			emit(A64_ADD_I(is64, dst, dst, -imm), ctx);
1039fd868f14SLuke Nelson 		} else {
1040e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
1041e54bcde3SZi Shen Lim 			emit(A64_SUB(is64, dst, dst, tmp), ctx);
1042fd868f14SLuke Nelson 		}
1043e54bcde3SZi Shen Lim 		break;
1044e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_AND | BPF_K:
1045e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_AND | BPF_K:
1046fd49591cSLuke Nelson 		a64_insn = A64_AND_I(is64, dst, dst, imm);
1047fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
1048fd49591cSLuke Nelson 			emit(a64_insn, ctx);
1049fd49591cSLuke Nelson 		} else {
1050e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
1051e54bcde3SZi Shen Lim 			emit(A64_AND(is64, dst, dst, tmp), ctx);
1052fd49591cSLuke Nelson 		}
1053e54bcde3SZi Shen Lim 		break;
1054e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_OR | BPF_K:
1055e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_OR | BPF_K:
1056fd49591cSLuke Nelson 		a64_insn = A64_ORR_I(is64, dst, dst, imm);
1057fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
1058fd49591cSLuke Nelson 			emit(a64_insn, ctx);
1059fd49591cSLuke Nelson 		} else {
1060e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
1061e54bcde3SZi Shen Lim 			emit(A64_ORR(is64, dst, dst, tmp), ctx);
1062fd49591cSLuke Nelson 		}
1063e54bcde3SZi Shen Lim 		break;
1064e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_XOR | BPF_K:
1065e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_XOR | BPF_K:
1066fd49591cSLuke Nelson 		a64_insn = A64_EOR_I(is64, dst, dst, imm);
1067fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
1068fd49591cSLuke Nelson 			emit(a64_insn, ctx);
1069fd49591cSLuke Nelson 		} else {
1070e54bcde3SZi Shen Lim 			emit_a64_mov_i(is64, tmp, imm, ctx);
1071e54bcde3SZi Shen Lim 			emit(A64_EOR(is64, dst, dst, tmp), ctx);
1072fd49591cSLuke Nelson 		}
1073e54bcde3SZi Shen Lim 		break;
1074e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MUL | BPF_K:
1075e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MUL | BPF_K:
1076e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
1077e54bcde3SZi Shen Lim 		emit(A64_MUL(is64, dst, dst, tmp), ctx);
1078e54bcde3SZi Shen Lim 		break;
1079e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_DIV | BPF_K:
1080e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_DIV | BPF_K:
1081e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp, imm, ctx);
108268b18191SXu Kuohai 		if (!off)
1083e54bcde3SZi Shen Lim 			emit(A64_UDIV(is64, dst, dst, tmp), ctx);
108468b18191SXu Kuohai 		else
108568b18191SXu Kuohai 			emit(A64_SDIV(is64, dst, dst, tmp), ctx);
1086e54bcde3SZi Shen Lim 		break;
1087e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_MOD | BPF_K:
1088e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_MOD | BPF_K:
1089e54bcde3SZi Shen Lim 		emit_a64_mov_i(is64, tmp2, imm, ctx);
109068b18191SXu Kuohai 		if (!off)
1091e54bcde3SZi Shen Lim 			emit(A64_UDIV(is64, tmp, dst, tmp2), ctx);
109268b18191SXu Kuohai 		else
109368b18191SXu Kuohai 			emit(A64_SDIV(is64, tmp, dst, tmp2), ctx);
1094504792e0SJerin Jacob 		emit(A64_MSUB(is64, dst, dst, tmp, tmp2), ctx);
1095e54bcde3SZi Shen Lim 		break;
1096e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_LSH | BPF_K:
1097e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_LSH | BPF_K:
1098e54bcde3SZi Shen Lim 		emit(A64_LSL(is64, dst, dst, imm), ctx);
1099e54bcde3SZi Shen Lim 		break;
1100e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_RSH | BPF_K:
1101e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_RSH | BPF_K:
1102e54bcde3SZi Shen Lim 		emit(A64_LSR(is64, dst, dst, imm), ctx);
1103e54bcde3SZi Shen Lim 		break;
1104e54bcde3SZi Shen Lim 	case BPF_ALU | BPF_ARSH | BPF_K:
1105e54bcde3SZi Shen Lim 	case BPF_ALU64 | BPF_ARSH | BPF_K:
1106e54bcde3SZi Shen Lim 		emit(A64_ASR(is64, dst, dst, imm), ctx);
1107e54bcde3SZi Shen Lim 		break;
1108e54bcde3SZi Shen Lim 
1109e54bcde3SZi Shen Lim 	/* JUMP off */
1110e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JA:
1111c32b6ee5SXu Kuohai 	case BPF_JMP32 | BPF_JA:
1112c32b6ee5SXu Kuohai 		if (BPF_CLASS(code) == BPF_JMP)
111332f6865cSIlias Apalodimas 			jmp_offset = bpf2a64_offset(i, off, ctx);
1114c32b6ee5SXu Kuohai 		else
1115c32b6ee5SXu Kuohai 			jmp_offset = bpf2a64_offset(i, imm, ctx);
1116e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
1117e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
1118e54bcde3SZi Shen Lim 		break;
1119e54bcde3SZi Shen Lim 	/* IF (dst COND src) JUMP off */
1120e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_X:
1121e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_X:
1122c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLT | BPF_X:
1123e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_X:
1124c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLE | BPF_X:
1125e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_X:
1126e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_X:
1127c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLT | BPF_X:
1128e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_X:
1129c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLE | BPF_X:
1130654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_X:
1131654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_X:
1132654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_X:
1133654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_X:
1134654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_X:
1135654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_X:
1136654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_X:
1137654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_X:
1138654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_X:
1139654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_X:
1140654b65a0SJiong Wang 		emit(A64_CMP(is64, dst, src), ctx);
1141e54bcde3SZi Shen Lim emit_cond_jmp:
114232f6865cSIlias Apalodimas 		jmp_offset = bpf2a64_offset(i, off, ctx);
1143e54bcde3SZi Shen Lim 		check_imm19(jmp_offset);
1144e54bcde3SZi Shen Lim 		switch (BPF_OP(code)) {
1145e54bcde3SZi Shen Lim 		case BPF_JEQ:
1146e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_EQ;
1147e54bcde3SZi Shen Lim 			break;
1148e54bcde3SZi Shen Lim 		case BPF_JGT:
1149e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_HI;
1150e54bcde3SZi Shen Lim 			break;
1151c362b2f3SDaniel Borkmann 		case BPF_JLT:
1152c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_CC;
1153c362b2f3SDaniel Borkmann 			break;
1154e54bcde3SZi Shen Lim 		case BPF_JGE:
1155e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_CS;
1156e54bcde3SZi Shen Lim 			break;
1157c362b2f3SDaniel Borkmann 		case BPF_JLE:
1158c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_LS;
1159c362b2f3SDaniel Borkmann 			break;
116098397fc5SZi Shen Lim 		case BPF_JSET:
1161e54bcde3SZi Shen Lim 		case BPF_JNE:
1162e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_NE;
1163e54bcde3SZi Shen Lim 			break;
1164e54bcde3SZi Shen Lim 		case BPF_JSGT:
1165e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GT;
1166e54bcde3SZi Shen Lim 			break;
1167c362b2f3SDaniel Borkmann 		case BPF_JSLT:
1168c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_LT;
1169c362b2f3SDaniel Borkmann 			break;
1170e54bcde3SZi Shen Lim 		case BPF_JSGE:
1171e54bcde3SZi Shen Lim 			jmp_cond = A64_COND_GE;
1172e54bcde3SZi Shen Lim 			break;
1173c362b2f3SDaniel Borkmann 		case BPF_JSLE:
1174c362b2f3SDaniel Borkmann 			jmp_cond = A64_COND_LE;
1175c362b2f3SDaniel Borkmann 			break;
1176e54bcde3SZi Shen Lim 		default:
1177e54bcde3SZi Shen Lim 			return -EFAULT;
1178e54bcde3SZi Shen Lim 		}
1179e54bcde3SZi Shen Lim 		emit(A64_B_(jmp_cond, jmp_offset), ctx);
1180e54bcde3SZi Shen Lim 		break;
1181e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_X:
1182654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_X:
1183654b65a0SJiong Wang 		emit(A64_TST(is64, dst, src), ctx);
1184e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
1185e54bcde3SZi Shen Lim 	/* IF (dst COND imm) JUMP off */
1186e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JEQ | BPF_K:
1187e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGT | BPF_K:
1188c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLT | BPF_K:
1189e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JGE | BPF_K:
1190c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JLE | BPF_K:
1191e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JNE | BPF_K:
1192e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGT | BPF_K:
1193c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLT | BPF_K:
1194e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSGE | BPF_K:
1195c362b2f3SDaniel Borkmann 	case BPF_JMP | BPF_JSLE | BPF_K:
1196654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JEQ | BPF_K:
1197654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGT | BPF_K:
1198654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLT | BPF_K:
1199654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JGE | BPF_K:
1200654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JLE | BPF_K:
1201654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JNE | BPF_K:
1202654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGT | BPF_K:
1203654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLT | BPF_K:
1204654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSGE | BPF_K:
1205654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSLE | BPF_K:
1206fd868f14SLuke Nelson 		if (is_addsub_imm(imm)) {
1207fd868f14SLuke Nelson 			emit(A64_CMP_I(is64, dst, imm), ctx);
1208fd868f14SLuke Nelson 		} else if (is_addsub_imm(-imm)) {
1209fd868f14SLuke Nelson 			emit(A64_CMN_I(is64, dst, -imm), ctx);
1210fd868f14SLuke Nelson 		} else {
1211654b65a0SJiong Wang 			emit_a64_mov_i(is64, tmp, imm, ctx);
1212654b65a0SJiong Wang 			emit(A64_CMP(is64, dst, tmp), ctx);
1213fd868f14SLuke Nelson 		}
1214e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
1215e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_JSET | BPF_K:
1216654b65a0SJiong Wang 	case BPF_JMP32 | BPF_JSET | BPF_K:
1217fd49591cSLuke Nelson 		a64_insn = A64_TST_I(is64, dst, imm);
1218fd49591cSLuke Nelson 		if (a64_insn != AARCH64_BREAK_FAULT) {
1219fd49591cSLuke Nelson 			emit(a64_insn, ctx);
1220fd49591cSLuke Nelson 		} else {
1221654b65a0SJiong Wang 			emit_a64_mov_i(is64, tmp, imm, ctx);
1222654b65a0SJiong Wang 			emit(A64_TST(is64, dst, tmp), ctx);
1223fd49591cSLuke Nelson 		}
1224e54bcde3SZi Shen Lim 		goto emit_cond_jmp;
1225e54bcde3SZi Shen Lim 	/* function call */
1226e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_CALL:
1227e54bcde3SZi Shen Lim 	{
1228e54bcde3SZi Shen Lim 		const u8 r0 = bpf2a64[BPF_REG_0];
12298c11ea5cSDaniel Borkmann 		bool func_addr_fixed;
12308c11ea5cSDaniel Borkmann 		u64 func_addr;
123175fe4c0bSPuranjay Mohan 		u32 cpu_offset;
123275fe4c0bSPuranjay Mohan 
123375fe4c0bSPuranjay Mohan 		/* Implement helper call to bpf_get_smp_processor_id() inline */
123475fe4c0bSPuranjay Mohan 		if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
123575fe4c0bSPuranjay Mohan 			cpu_offset = offsetof(struct thread_info, cpu);
123675fe4c0bSPuranjay Mohan 
123775fe4c0bSPuranjay Mohan 			emit(A64_MRS_SP_EL0(tmp), ctx);
123875fe4c0bSPuranjay Mohan 			if (is_lsi_offset(cpu_offset, 2)) {
123975fe4c0bSPuranjay Mohan 				emit(A64_LDR32I(r0, tmp, cpu_offset), ctx);
124075fe4c0bSPuranjay Mohan 			} else {
124175fe4c0bSPuranjay Mohan 				emit_a64_mov_i(1, tmp2, cpu_offset, ctx);
124275fe4c0bSPuranjay Mohan 				emit(A64_LDR32(r0, tmp, tmp2), ctx);
124375fe4c0bSPuranjay Mohan 			}
124475fe4c0bSPuranjay Mohan 			break;
124575fe4c0bSPuranjay Mohan 		}
1246e54bcde3SZi Shen Lim 
12472bb138cbSPuranjay Mohan 		/* Implement helper call to bpf_get_current_task/_btf() inline */
12482bb138cbSPuranjay Mohan 		if (insn->src_reg == 0 && (insn->imm == BPF_FUNC_get_current_task ||
12492bb138cbSPuranjay Mohan 					   insn->imm == BPF_FUNC_get_current_task_btf)) {
12502bb138cbSPuranjay Mohan 			emit(A64_MRS_SP_EL0(r0), ctx);
12512bb138cbSPuranjay Mohan 			break;
12522bb138cbSPuranjay Mohan 		}
12532bb138cbSPuranjay Mohan 
12548c11ea5cSDaniel Borkmann 		ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
12558c11ea5cSDaniel Borkmann 					    &func_addr, &func_addr_fixed);
12568c11ea5cSDaniel Borkmann 		if (ret < 0)
12578c11ea5cSDaniel Borkmann 			return ret;
1258efc9909fSXu Kuohai 		emit_call(func_addr, ctx);
1259e54bcde3SZi Shen Lim 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
1260e54bcde3SZi Shen Lim 		break;
1261e54bcde3SZi Shen Lim 	}
1262ddb55992SZi Shen Lim 	/* tail call */
126371189fa9SAlexei Starovoitov 	case BPF_JMP | BPF_TAIL_CALL:
1264ddb55992SZi Shen Lim 		if (emit_bpf_tail_call(ctx))
1265ddb55992SZi Shen Lim 			return -EFAULT;
1266ddb55992SZi Shen Lim 		break;
1267e54bcde3SZi Shen Lim 	/* function return */
1268e54bcde3SZi Shen Lim 	case BPF_JMP | BPF_EXIT:
126951c9fbb1SZi Shen Lim 		/* Optimization: when last instruction is EXIT,
127051c9fbb1SZi Shen Lim 		   simply fallthrough to epilogue. */
1271e54bcde3SZi Shen Lim 		if (i == ctx->prog->len - 1)
1272e54bcde3SZi Shen Lim 			break;
1273e54bcde3SZi Shen Lim 		jmp_offset = epilogue_offset(ctx);
1274e54bcde3SZi Shen Lim 		check_imm26(jmp_offset);
1275e54bcde3SZi Shen Lim 		emit(A64_B(jmp_offset), ctx);
1276e54bcde3SZi Shen Lim 		break;
1277e54bcde3SZi Shen Lim 
127830d3d94cSZi Shen Lim 	/* dst = imm64 */
127930d3d94cSZi Shen Lim 	case BPF_LD | BPF_IMM | BPF_DW:
128030d3d94cSZi Shen Lim 	{
128130d3d94cSZi Shen Lim 		const struct bpf_insn insn1 = insn[1];
128230d3d94cSZi Shen Lim 		u64 imm64;
128330d3d94cSZi Shen Lim 
12841e4df6b7SXi Wang 		imm64 = (u64)insn1.imm << 32 | (u32)imm;
1285e4a41c2cSHou Tao 		if (bpf_pseudo_func(insn))
1286e4a41c2cSHou Tao 			emit_addr_mov_i64(dst, imm64, ctx);
1287e4a41c2cSHou Tao 		else
128830d3d94cSZi Shen Lim 			emit_a64_mov_i64(dst, imm64, ctx);
128930d3d94cSZi Shen Lim 
129030d3d94cSZi Shen Lim 		return 1;
129130d3d94cSZi Shen Lim 	}
129230d3d94cSZi Shen Lim 
1293cc88f540SXu Kuohai 	/* LDX: dst = (u64)*(unsigned size *)(src + off) */
1294e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_W:
1295e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_H:
1296e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_B:
1297e54bcde3SZi Shen Lim 	case BPF_LDX | BPF_MEM | BPF_DW:
129880083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
129980083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_W:
130080083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_H:
130180083428SJean-Philippe Brucker 	case BPF_LDX | BPF_PROBE_MEM | BPF_B:
1302cc88f540SXu Kuohai 	/* LDXS: dst_reg = (s64)*(signed size *)(src_reg + off) */
1303cc88f540SXu Kuohai 	case BPF_LDX | BPF_MEMSX | BPF_B:
1304cc88f540SXu Kuohai 	case BPF_LDX | BPF_MEMSX | BPF_H:
1305cc88f540SXu Kuohai 	case BPF_LDX | BPF_MEMSX | BPF_W:
1306cc88f540SXu Kuohai 	case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
1307cc88f540SXu Kuohai 	case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
1308cc88f540SXu Kuohai 	case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
1309339af577SPuranjay Mohan 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_B:
1310339af577SPuranjay Mohan 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
1311339af577SPuranjay Mohan 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
1312339af577SPuranjay Mohan 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
1313339af577SPuranjay Mohan 		if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
1314339af577SPuranjay Mohan 			emit(A64_ADD(1, tmp2, src, arena_vm_base), ctx);
1315339af577SPuranjay Mohan 			src = tmp2;
1316339af577SPuranjay Mohan 		}
1317339af577SPuranjay Mohan 		if (ctx->fpb_offset > 0 && src == fp && BPF_MODE(insn->code) != BPF_PROBE_MEM32) {
13185b3d19b9SXu Kuohai 			src_adj = fpb;
13195b3d19b9SXu Kuohai 			off_adj = off + ctx->fpb_offset;
13205b3d19b9SXu Kuohai 		} else {
13215b3d19b9SXu Kuohai 			src_adj = src;
13225b3d19b9SXu Kuohai 			off_adj = off;
13235b3d19b9SXu Kuohai 		}
1324cc88f540SXu Kuohai 		sign_extend = (BPF_MODE(insn->code) == BPF_MEMSX ||
1325cc88f540SXu Kuohai 				BPF_MODE(insn->code) == BPF_PROBE_MEMSX);
1326e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
1327e54bcde3SZi Shen Lim 		case BPF_W:
13285b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 2)) {
1329cc88f540SXu Kuohai 				if (sign_extend)
1330cc88f540SXu Kuohai 					emit(A64_LDRSWI(dst, src_adj, off_adj), ctx);
1331cc88f540SXu Kuohai 				else
13325b3d19b9SXu Kuohai 					emit(A64_LDR32I(dst, src_adj, off_adj), ctx);
13337db6c0f1SXu Kuohai 			} else {
13347db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1335cc88f540SXu Kuohai 				if (sign_extend)
1336114b5b3bSPuranjay Mohan 					emit(A64_LDRSW(dst, src, tmp), ctx);
1337cc88f540SXu Kuohai 				else
1338e54bcde3SZi Shen Lim 					emit(A64_LDR32(dst, src, tmp), ctx);
13397db6c0f1SXu Kuohai 			}
1340e54bcde3SZi Shen Lim 			break;
1341e54bcde3SZi Shen Lim 		case BPF_H:
13425b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 1)) {
1343cc88f540SXu Kuohai 				if (sign_extend)
1344cc88f540SXu Kuohai 					emit(A64_LDRSHI(dst, src_adj, off_adj), ctx);
1345cc88f540SXu Kuohai 				else
13465b3d19b9SXu Kuohai 					emit(A64_LDRHI(dst, src_adj, off_adj), ctx);
13477db6c0f1SXu Kuohai 			} else {
13487db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1349cc88f540SXu Kuohai 				if (sign_extend)
1350cc88f540SXu Kuohai 					emit(A64_LDRSH(dst, src, tmp), ctx);
1351cc88f540SXu Kuohai 				else
1352e54bcde3SZi Shen Lim 					emit(A64_LDRH(dst, src, tmp), ctx);
13537db6c0f1SXu Kuohai 			}
1354e54bcde3SZi Shen Lim 			break;
1355e54bcde3SZi Shen Lim 		case BPF_B:
13565b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 0)) {
1357cc88f540SXu Kuohai 				if (sign_extend)
1358cc88f540SXu Kuohai 					emit(A64_LDRSBI(dst, src_adj, off_adj), ctx);
1359cc88f540SXu Kuohai 				else
13605b3d19b9SXu Kuohai 					emit(A64_LDRBI(dst, src_adj, off_adj), ctx);
13617db6c0f1SXu Kuohai 			} else {
13627db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1363cc88f540SXu Kuohai 				if (sign_extend)
1364cc88f540SXu Kuohai 					emit(A64_LDRSB(dst, src, tmp), ctx);
1365cc88f540SXu Kuohai 				else
1366e54bcde3SZi Shen Lim 					emit(A64_LDRB(dst, src, tmp), ctx);
13677db6c0f1SXu Kuohai 			}
1368e54bcde3SZi Shen Lim 			break;
1369e54bcde3SZi Shen Lim 		case BPF_DW:
13705b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 3)) {
13715b3d19b9SXu Kuohai 				emit(A64_LDR64I(dst, src_adj, off_adj), ctx);
13727db6c0f1SXu Kuohai 			} else {
13737db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1374e54bcde3SZi Shen Lim 				emit(A64_LDR64(dst, src, tmp), ctx);
13757db6c0f1SXu Kuohai 			}
1376e54bcde3SZi Shen Lim 			break;
1377e54bcde3SZi Shen Lim 		}
137880083428SJean-Philippe Brucker 
137980083428SJean-Philippe Brucker 		ret = add_exception_handler(insn, ctx, dst);
138080083428SJean-Philippe Brucker 		if (ret)
138180083428SJean-Philippe Brucker 			return ret;
1382e54bcde3SZi Shen Lim 		break;
1383e54bcde3SZi Shen Lim 
1384f5e81d11SDaniel Borkmann 	/* speculation barrier */
1385f5e81d11SDaniel Borkmann 	case BPF_ST | BPF_NOSPEC:
1386f5e81d11SDaniel Borkmann 		/*
1387f5e81d11SDaniel Borkmann 		 * Nothing required here.
1388f5e81d11SDaniel Borkmann 		 *
1389f5e81d11SDaniel Borkmann 		 * In case of arm64, we rely on the firmware mitigation of
1390f5e81d11SDaniel Borkmann 		 * Speculative Store Bypass as controlled via the ssbd kernel
1391f5e81d11SDaniel Borkmann 		 * parameter. Whenever the mitigation is enabled, it works
1392f5e81d11SDaniel Borkmann 		 * for all of the kernel code with no need to provide any
1393f5e81d11SDaniel Borkmann 		 * additional instructions.
1394f5e81d11SDaniel Borkmann 		 */
1395f5e81d11SDaniel Borkmann 		break;
1396f5e81d11SDaniel Borkmann 
1397e54bcde3SZi Shen Lim 	/* ST: *(size *)(dst + off) = imm */
1398e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_W:
1399e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_H:
1400e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_B:
1401e54bcde3SZi Shen Lim 	case BPF_ST | BPF_MEM | BPF_DW:
1402339af577SPuranjay Mohan 	case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
1403339af577SPuranjay Mohan 	case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
1404339af577SPuranjay Mohan 	case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
1405339af577SPuranjay Mohan 	case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
1406339af577SPuranjay Mohan 		if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
1407339af577SPuranjay Mohan 			emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx);
1408339af577SPuranjay Mohan 			dst = tmp2;
1409339af577SPuranjay Mohan 		}
1410339af577SPuranjay Mohan 		if (ctx->fpb_offset > 0 && dst == fp && BPF_MODE(insn->code) != BPF_PROBE_MEM32) {
14115b3d19b9SXu Kuohai 			dst_adj = fpb;
14125b3d19b9SXu Kuohai 			off_adj = off + ctx->fpb_offset;
14135b3d19b9SXu Kuohai 		} else {
14145b3d19b9SXu Kuohai 			dst_adj = dst;
14155b3d19b9SXu Kuohai 			off_adj = off;
14165b3d19b9SXu Kuohai 		}
1417df849ba3SYang Shi 		/* Load imm to a register then store it */
1418df849ba3SYang Shi 		emit_a64_mov_i(1, tmp, imm, ctx);
1419df849ba3SYang Shi 		switch (BPF_SIZE(code)) {
1420df849ba3SYang Shi 		case BPF_W:
14215b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 2)) {
14225b3d19b9SXu Kuohai 				emit(A64_STR32I(tmp, dst_adj, off_adj), ctx);
14237db6c0f1SXu Kuohai 			} else {
14247db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp2, off, ctx);
1425df849ba3SYang Shi 				emit(A64_STR32(tmp, dst, tmp2), ctx);
14267db6c0f1SXu Kuohai 			}
1427df849ba3SYang Shi 			break;
1428df849ba3SYang Shi 		case BPF_H:
14295b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 1)) {
14305b3d19b9SXu Kuohai 				emit(A64_STRHI(tmp, dst_adj, off_adj), ctx);
14317db6c0f1SXu Kuohai 			} else {
14327db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp2, off, ctx);
1433df849ba3SYang Shi 				emit(A64_STRH(tmp, dst, tmp2), ctx);
14347db6c0f1SXu Kuohai 			}
1435df849ba3SYang Shi 			break;
1436df849ba3SYang Shi 		case BPF_B:
14375b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 0)) {
14385b3d19b9SXu Kuohai 				emit(A64_STRBI(tmp, dst_adj, off_adj), ctx);
14397db6c0f1SXu Kuohai 			} else {
14407db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp2, off, ctx);
1441df849ba3SYang Shi 				emit(A64_STRB(tmp, dst, tmp2), ctx);
14427db6c0f1SXu Kuohai 			}
1443df849ba3SYang Shi 			break;
1444df849ba3SYang Shi 		case BPF_DW:
14455b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 3)) {
14465b3d19b9SXu Kuohai 				emit(A64_STR64I(tmp, dst_adj, off_adj), ctx);
14477db6c0f1SXu Kuohai 			} else {
14487db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp2, off, ctx);
1449df849ba3SYang Shi 				emit(A64_STR64(tmp, dst, tmp2), ctx);
14507db6c0f1SXu Kuohai 			}
1451df849ba3SYang Shi 			break;
1452df849ba3SYang Shi 		}
1453339af577SPuranjay Mohan 
1454339af577SPuranjay Mohan 		ret = add_exception_handler(insn, ctx, dst);
1455339af577SPuranjay Mohan 		if (ret)
1456339af577SPuranjay Mohan 			return ret;
1457df849ba3SYang Shi 		break;
1458e54bcde3SZi Shen Lim 
1459e54bcde3SZi Shen Lim 	/* STX: *(size *)(dst + off) = src */
1460e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_W:
1461e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_H:
1462e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_B:
1463e54bcde3SZi Shen Lim 	case BPF_STX | BPF_MEM | BPF_DW:
1464339af577SPuranjay Mohan 	case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
1465339af577SPuranjay Mohan 	case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
1466339af577SPuranjay Mohan 	case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
1467339af577SPuranjay Mohan 	case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
1468339af577SPuranjay Mohan 		if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
1469339af577SPuranjay Mohan 			emit(A64_ADD(1, tmp2, dst, arena_vm_base), ctx);
1470339af577SPuranjay Mohan 			dst = tmp2;
1471339af577SPuranjay Mohan 		}
1472339af577SPuranjay Mohan 		if (ctx->fpb_offset > 0 && dst == fp && BPF_MODE(insn->code) != BPF_PROBE_MEM32) {
14735b3d19b9SXu Kuohai 			dst_adj = fpb;
14745b3d19b9SXu Kuohai 			off_adj = off + ctx->fpb_offset;
14755b3d19b9SXu Kuohai 		} else {
14765b3d19b9SXu Kuohai 			dst_adj = dst;
14775b3d19b9SXu Kuohai 			off_adj = off;
14785b3d19b9SXu Kuohai 		}
1479e54bcde3SZi Shen Lim 		switch (BPF_SIZE(code)) {
1480e54bcde3SZi Shen Lim 		case BPF_W:
14815b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 2)) {
14825b3d19b9SXu Kuohai 				emit(A64_STR32I(src, dst_adj, off_adj), ctx);
14837db6c0f1SXu Kuohai 			} else {
14847db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1485e54bcde3SZi Shen Lim 				emit(A64_STR32(src, dst, tmp), ctx);
14867db6c0f1SXu Kuohai 			}
1487e54bcde3SZi Shen Lim 			break;
1488e54bcde3SZi Shen Lim 		case BPF_H:
14895b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 1)) {
14905b3d19b9SXu Kuohai 				emit(A64_STRHI(src, dst_adj, off_adj), ctx);
14917db6c0f1SXu Kuohai 			} else {
14927db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1493e54bcde3SZi Shen Lim 				emit(A64_STRH(src, dst, tmp), ctx);
14947db6c0f1SXu Kuohai 			}
1495e54bcde3SZi Shen Lim 			break;
1496e54bcde3SZi Shen Lim 		case BPF_B:
14975b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 0)) {
14985b3d19b9SXu Kuohai 				emit(A64_STRBI(src, dst_adj, off_adj), ctx);
14997db6c0f1SXu Kuohai 			} else {
15007db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1501e54bcde3SZi Shen Lim 				emit(A64_STRB(src, dst, tmp), ctx);
15027db6c0f1SXu Kuohai 			}
1503e54bcde3SZi Shen Lim 			break;
1504e54bcde3SZi Shen Lim 		case BPF_DW:
15055b3d19b9SXu Kuohai 			if (is_lsi_offset(off_adj, 3)) {
15065b3d19b9SXu Kuohai 				emit(A64_STR64I(src, dst_adj, off_adj), ctx);
15077db6c0f1SXu Kuohai 			} else {
15087db6c0f1SXu Kuohai 				emit_a64_mov_i(1, tmp, off, ctx);
1509e54bcde3SZi Shen Lim 				emit(A64_STR64(src, dst, tmp), ctx);
15107db6c0f1SXu Kuohai 			}
1511e54bcde3SZi Shen Lim 			break;
1512e54bcde3SZi Shen Lim 		}
1513339af577SPuranjay Mohan 
1514339af577SPuranjay Mohan 		ret = add_exception_handler(insn, ctx, dst);
1515339af577SPuranjay Mohan 		if (ret)
1516339af577SPuranjay Mohan 			return ret;
1517e54bcde3SZi Shen Lim 		break;
151834b8ab09SDaniel Borkmann 
151991c960b0SBrendan Jackman 	case BPF_STX | BPF_ATOMIC | BPF_W:
152091c960b0SBrendan Jackman 	case BPF_STX | BPF_ATOMIC | BPF_DW:
1521e612b5c1SPuranjay Mohan 	case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
1522e612b5c1SPuranjay Mohan 	case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
15231902472bSHou Tao 		if (cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
15241902472bSHou Tao 			ret = emit_lse_atomic(insn, ctx);
15251902472bSHou Tao 		else
15261902472bSHou Tao 			ret = emit_ll_sc_atomic(insn, ctx);
15271902472bSHou Tao 		if (ret)
15281902472bSHou Tao 			return ret;
1529e612b5c1SPuranjay Mohan 
1530e612b5c1SPuranjay Mohan 		ret = add_exception_handler(insn, ctx, dst);
1531e612b5c1SPuranjay Mohan 		if (ret)
1532e612b5c1SPuranjay Mohan 			return ret;
153385f68fe8SDaniel Borkmann 		break;
1534e54bcde3SZi Shen Lim 
1535e54bcde3SZi Shen Lim 	default:
1536e54bcde3SZi Shen Lim 		pr_err_once("unknown opcode %02x\n", code);
1537e54bcde3SZi Shen Lim 		return -EINVAL;
1538e54bcde3SZi Shen Lim 	}
1539e54bcde3SZi Shen Lim 
1540e54bcde3SZi Shen Lim 	return 0;
1541e54bcde3SZi Shen Lim }
1542e54bcde3SZi Shen Lim 
15435b3d19b9SXu Kuohai /*
15445b3d19b9SXu Kuohai  * Return 0 if FP may change at runtime, otherwise find the minimum negative
15455b3d19b9SXu Kuohai  * offset to FP, converts it to positive number, and align down to 8 bytes.
15465b3d19b9SXu Kuohai  */
15475b3d19b9SXu Kuohai static int find_fpb_offset(struct bpf_prog *prog)
15485b3d19b9SXu Kuohai {
15495b3d19b9SXu Kuohai 	int i;
15505b3d19b9SXu Kuohai 	int offset = 0;
15515b3d19b9SXu Kuohai 
15525b3d19b9SXu Kuohai 	for (i = 0; i < prog->len; i++) {
15535b3d19b9SXu Kuohai 		const struct bpf_insn *insn = &prog->insnsi[i];
15545b3d19b9SXu Kuohai 		const u8 class = BPF_CLASS(insn->code);
15555b3d19b9SXu Kuohai 		const u8 mode = BPF_MODE(insn->code);
15565b3d19b9SXu Kuohai 		const u8 src = insn->src_reg;
15575b3d19b9SXu Kuohai 		const u8 dst = insn->dst_reg;
15585b3d19b9SXu Kuohai 		const s32 imm = insn->imm;
15595b3d19b9SXu Kuohai 		const s16 off = insn->off;
15605b3d19b9SXu Kuohai 
15615b3d19b9SXu Kuohai 		switch (class) {
15625b3d19b9SXu Kuohai 		case BPF_STX:
15635b3d19b9SXu Kuohai 		case BPF_ST:
15645b3d19b9SXu Kuohai 			/* fp holds atomic operation result */
15655b3d19b9SXu Kuohai 			if (class == BPF_STX && mode == BPF_ATOMIC &&
15665b3d19b9SXu Kuohai 			    ((imm == BPF_XCHG ||
15675b3d19b9SXu Kuohai 			      imm == (BPF_FETCH | BPF_ADD) ||
15685b3d19b9SXu Kuohai 			      imm == (BPF_FETCH | BPF_AND) ||
15695b3d19b9SXu Kuohai 			      imm == (BPF_FETCH | BPF_XOR) ||
15705b3d19b9SXu Kuohai 			      imm == (BPF_FETCH | BPF_OR)) &&
15715b3d19b9SXu Kuohai 			     src == BPF_REG_FP))
15725b3d19b9SXu Kuohai 				return 0;
15735b3d19b9SXu Kuohai 
15745b3d19b9SXu Kuohai 			if (mode == BPF_MEM && dst == BPF_REG_FP &&
15755b3d19b9SXu Kuohai 			    off < offset)
15765b3d19b9SXu Kuohai 				offset = insn->off;
15775b3d19b9SXu Kuohai 			break;
15785b3d19b9SXu Kuohai 
15795b3d19b9SXu Kuohai 		case BPF_JMP32:
15805b3d19b9SXu Kuohai 		case BPF_JMP:
15815b3d19b9SXu Kuohai 			break;
15825b3d19b9SXu Kuohai 
15835b3d19b9SXu Kuohai 		case BPF_LDX:
15845b3d19b9SXu Kuohai 		case BPF_LD:
15855b3d19b9SXu Kuohai 			/* fp holds load result */
15865b3d19b9SXu Kuohai 			if (dst == BPF_REG_FP)
15875b3d19b9SXu Kuohai 				return 0;
15885b3d19b9SXu Kuohai 
15895b3d19b9SXu Kuohai 			if (class == BPF_LDX && mode == BPF_MEM &&
15905b3d19b9SXu Kuohai 			    src == BPF_REG_FP && off < offset)
15915b3d19b9SXu Kuohai 				offset = off;
15925b3d19b9SXu Kuohai 			break;
15935b3d19b9SXu Kuohai 
15945b3d19b9SXu Kuohai 		case BPF_ALU:
15955b3d19b9SXu Kuohai 		case BPF_ALU64:
15965b3d19b9SXu Kuohai 		default:
15975b3d19b9SXu Kuohai 			/* fp holds ALU result */
15985b3d19b9SXu Kuohai 			if (dst == BPF_REG_FP)
15995b3d19b9SXu Kuohai 				return 0;
16005b3d19b9SXu Kuohai 		}
16015b3d19b9SXu Kuohai 	}
16025b3d19b9SXu Kuohai 
16035b3d19b9SXu Kuohai 	if (offset < 0) {
16045b3d19b9SXu Kuohai 		/*
16055b3d19b9SXu Kuohai 		 * safely be converted to a positive 'int', since insn->off
16065b3d19b9SXu Kuohai 		 * is 's16'
16075b3d19b9SXu Kuohai 		 */
16085b3d19b9SXu Kuohai 		offset = -offset;
16095b3d19b9SXu Kuohai 		/* align down to 8 bytes */
16105b3d19b9SXu Kuohai 		offset = ALIGN_DOWN(offset, 8);
16115b3d19b9SXu Kuohai 	}
16125b3d19b9SXu Kuohai 
16135b3d19b9SXu Kuohai 	return offset;
16145b3d19b9SXu Kuohai }
16155b3d19b9SXu Kuohai 
16168c11ea5cSDaniel Borkmann static int build_body(struct jit_ctx *ctx, bool extra_pass)
1617e54bcde3SZi Shen Lim {
1618e54bcde3SZi Shen Lim 	const struct bpf_prog *prog = ctx->prog;
1619e54bcde3SZi Shen Lim 	int i;
1620e54bcde3SZi Shen Lim 
162132f6865cSIlias Apalodimas 	/*
162232f6865cSIlias Apalodimas 	 * - offset[0] offset of the end of prologue,
162332f6865cSIlias Apalodimas 	 *   start of the 1st instruction.
162432f6865cSIlias Apalodimas 	 * - offset[1] - offset of the end of 1st instruction,
162532f6865cSIlias Apalodimas 	 *   start of the 2nd instruction
162632f6865cSIlias Apalodimas 	 * [....]
162732f6865cSIlias Apalodimas 	 * - offset[3] - offset of the end of 3rd instruction,
162832f6865cSIlias Apalodimas 	 *   start of 4th instruction
162932f6865cSIlias Apalodimas 	 */
1630e54bcde3SZi Shen Lim 	for (i = 0; i < prog->len; i++) {
1631e54bcde3SZi Shen Lim 		const struct bpf_insn *insn = &prog->insnsi[i];
1632e54bcde3SZi Shen Lim 		int ret;
1633e54bcde3SZi Shen Lim 
163432f6865cSIlias Apalodimas 		if (ctx->image == NULL)
163532f6865cSIlias Apalodimas 			ctx->offset[i] = ctx->idx;
16368c11ea5cSDaniel Borkmann 		ret = build_insn(insn, ctx, extra_pass);
163730d3d94cSZi Shen Lim 		if (ret > 0) {
163830d3d94cSZi Shen Lim 			i++;
1639ddc665a4SDaniel Borkmann 			if (ctx->image == NULL)
1640ddc665a4SDaniel Borkmann 				ctx->offset[i] = ctx->idx;
164130d3d94cSZi Shen Lim 			continue;
164230d3d94cSZi Shen Lim 		}
1643e54bcde3SZi Shen Lim 		if (ret)
1644e54bcde3SZi Shen Lim 			return ret;
1645e54bcde3SZi Shen Lim 	}
164632f6865cSIlias Apalodimas 	/*
164732f6865cSIlias Apalodimas 	 * offset is allocated with prog->len + 1 so fill in
164832f6865cSIlias Apalodimas 	 * the last element with the offset after the last
164932f6865cSIlias Apalodimas 	 * instruction (end of program)
165032f6865cSIlias Apalodimas 	 */
165132f6865cSIlias Apalodimas 	if (ctx->image == NULL)
165232f6865cSIlias Apalodimas 		ctx->offset[i] = ctx->idx;
1653e54bcde3SZi Shen Lim 
1654e54bcde3SZi Shen Lim 	return 0;
1655e54bcde3SZi Shen Lim }
1656e54bcde3SZi Shen Lim 
165742ff712bSZi Shen Lim static int validate_code(struct jit_ctx *ctx)
165842ff712bSZi Shen Lim {
165942ff712bSZi Shen Lim 	int i;
166042ff712bSZi Shen Lim 
166142ff712bSZi Shen Lim 	for (i = 0; i < ctx->idx; i++) {
166242ff712bSZi Shen Lim 		u32 a64_insn = le32_to_cpu(ctx->image[i]);
166342ff712bSZi Shen Lim 
166442ff712bSZi Shen Lim 		if (a64_insn == AARCH64_BREAK_FAULT)
166542ff712bSZi Shen Lim 			return -1;
166642ff712bSZi Shen Lim 	}
1667efc9909fSXu Kuohai 	return 0;
1668efc9909fSXu Kuohai }
1669efc9909fSXu Kuohai 
1670efc9909fSXu Kuohai static int validate_ctx(struct jit_ctx *ctx)
1671efc9909fSXu Kuohai {
1672efc9909fSXu Kuohai 	if (validate_code(ctx))
1673efc9909fSXu Kuohai 		return -1;
167442ff712bSZi Shen Lim 
167580083428SJean-Philippe Brucker 	if (WARN_ON_ONCE(ctx->exentry_idx != ctx->prog->aux->num_exentries))
167680083428SJean-Philippe Brucker 		return -1;
167780083428SJean-Philippe Brucker 
167842ff712bSZi Shen Lim 	return 0;
167942ff712bSZi Shen Lim }
168042ff712bSZi Shen Lim 
1681e54bcde3SZi Shen Lim static inline void bpf_flush_icache(void *start, void *end)
1682e54bcde3SZi Shen Lim {
1683e54bcde3SZi Shen Lim 	flush_icache_range((unsigned long)start, (unsigned long)end);
1684e54bcde3SZi Shen Lim }
1685e54bcde3SZi Shen Lim 
1686db496944SAlexei Starovoitov struct arm64_jit_data {
1687db496944SAlexei Starovoitov 	struct bpf_binary_header *header;
16881dad391dSPuranjay Mohan 	u8 *ro_image;
16891dad391dSPuranjay Mohan 	struct bpf_binary_header *ro_header;
1690db496944SAlexei Starovoitov 	struct jit_ctx ctx;
1691db496944SAlexei Starovoitov };
1692db496944SAlexei Starovoitov 
1693d1c55ab5SDaniel Borkmann struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
1694e54bcde3SZi Shen Lim {
1695b2ad54e1SXu Kuohai 	int image_size, prog_size, extable_size, extable_align, extable_offset;
169626eb042eSDaniel Borkmann 	struct bpf_prog *tmp, *orig_prog = prog;
1697b569c1c6SDaniel Borkmann 	struct bpf_binary_header *header;
16981dad391dSPuranjay Mohan 	struct bpf_binary_header *ro_header;
1699db496944SAlexei Starovoitov 	struct arm64_jit_data *jit_data;
170056ea6a8bSDaniel Borkmann 	bool was_classic = bpf_prog_was_classic(prog);
170126eb042eSDaniel Borkmann 	bool tmp_blinded = false;
1702db496944SAlexei Starovoitov 	bool extra_pass = false;
1703e54bcde3SZi Shen Lim 	struct jit_ctx ctx;
1704339af577SPuranjay Mohan 	u64 arena_vm_start;
1705b569c1c6SDaniel Borkmann 	u8 *image_ptr;
17061dad391dSPuranjay Mohan 	u8 *ro_image_ptr;
1707e54bcde3SZi Shen Lim 
170860b58afcSAlexei Starovoitov 	if (!prog->jit_requested)
170926eb042eSDaniel Borkmann 		return orig_prog;
171026eb042eSDaniel Borkmann 
171126eb042eSDaniel Borkmann 	tmp = bpf_jit_blind_constants(prog);
171226eb042eSDaniel Borkmann 	/* If blinding was requested and we failed during blinding,
171326eb042eSDaniel Borkmann 	 * we must fall back to the interpreter.
171426eb042eSDaniel Borkmann 	 */
171526eb042eSDaniel Borkmann 	if (IS_ERR(tmp))
171626eb042eSDaniel Borkmann 		return orig_prog;
171726eb042eSDaniel Borkmann 	if (tmp != prog) {
171826eb042eSDaniel Borkmann 		tmp_blinded = true;
171926eb042eSDaniel Borkmann 		prog = tmp;
172026eb042eSDaniel Borkmann 	}
1721e54bcde3SZi Shen Lim 
1722339af577SPuranjay Mohan 	arena_vm_start = bpf_arena_get_kern_vm_start(prog->aux->arena);
1723db496944SAlexei Starovoitov 	jit_data = prog->aux->jit_data;
1724db496944SAlexei Starovoitov 	if (!jit_data) {
1725db496944SAlexei Starovoitov 		jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
1726db496944SAlexei Starovoitov 		if (!jit_data) {
1727db496944SAlexei Starovoitov 			prog = orig_prog;
1728db496944SAlexei Starovoitov 			goto out;
1729db496944SAlexei Starovoitov 		}
1730db496944SAlexei Starovoitov 		prog->aux->jit_data = jit_data;
1731db496944SAlexei Starovoitov 	}
1732db496944SAlexei Starovoitov 	if (jit_data->ctx.offset) {
1733db496944SAlexei Starovoitov 		ctx = jit_data->ctx;
17341dad391dSPuranjay Mohan 		ro_image_ptr = jit_data->ro_image;
17351dad391dSPuranjay Mohan 		ro_header = jit_data->ro_header;
1736db496944SAlexei Starovoitov 		header = jit_data->header;
17371dad391dSPuranjay Mohan 		image_ptr = (void *)header + ((void *)ro_image_ptr
17381dad391dSPuranjay Mohan 						 - (void *)ro_header);
1739db496944SAlexei Starovoitov 		extra_pass = true;
174080083428SJean-Philippe Brucker 		prog_size = sizeof(u32) * ctx.idx;
1741db496944SAlexei Starovoitov 		goto skip_init_ctx;
1742db496944SAlexei Starovoitov 	}
1743e54bcde3SZi Shen Lim 	memset(&ctx, 0, sizeof(ctx));
1744e54bcde3SZi Shen Lim 	ctx.prog = prog;
1745e54bcde3SZi Shen Lim 
174619f68ed6SAijun Sun 	ctx.offset = kvcalloc(prog->len + 1, sizeof(int), GFP_KERNEL);
174726eb042eSDaniel Borkmann 	if (ctx.offset == NULL) {
174826eb042eSDaniel Borkmann 		prog = orig_prog;
1749db496944SAlexei Starovoitov 		goto out_off;
175026eb042eSDaniel Borkmann 	}
1751e54bcde3SZi Shen Lim 
17525b3d19b9SXu Kuohai 	ctx.fpb_offset = find_fpb_offset(prog);
17534dd31243SPuranjay Mohan 	ctx.user_vm_start = bpf_arena_get_user_vm_start(prog->aux->arena);
17545b3d19b9SXu Kuohai 
175568e4f238SHou Tao 	/*
175668e4f238SHou Tao 	 * 1. Initial fake pass to compute ctx->idx and ctx->offset.
175768e4f238SHou Tao 	 *
175868e4f238SHou Tao 	 * BPF line info needs ctx->offset[i] to be the offset of
175968e4f238SHou Tao 	 * instruction[i] in jited image, so build prologue first.
176068e4f238SHou Tao 	 */
1761339af577SPuranjay Mohan 	if (build_prologue(&ctx, was_classic, prog->aux->exception_cb,
1762339af577SPuranjay Mohan 			   arena_vm_start)) {
176326eb042eSDaniel Borkmann 		prog = orig_prog;
176426eb042eSDaniel Borkmann 		goto out_off;
176526eb042eSDaniel Borkmann 	}
1766e54bcde3SZi Shen Lim 
176768e4f238SHou Tao 	if (build_body(&ctx, extra_pass)) {
1768ddb55992SZi Shen Lim 		prog = orig_prog;
1769ddb55992SZi Shen Lim 		goto out_off;
1770ddb55992SZi Shen Lim 	}
177151c9fbb1SZi Shen Lim 
177251c9fbb1SZi Shen Lim 	ctx.epilogue_offset = ctx.idx;
177322fc0e80SPuranjay Mohan 	build_epilogue(&ctx, prog->aux->exception_cb);
1774b2ad54e1SXu Kuohai 	build_plt(&ctx);
1775e54bcde3SZi Shen Lim 
1776b2ad54e1SXu Kuohai 	extable_align = __alignof__(struct exception_table_entry);
177780083428SJean-Philippe Brucker 	extable_size = prog->aux->num_exentries *
177880083428SJean-Philippe Brucker 		sizeof(struct exception_table_entry);
177980083428SJean-Philippe Brucker 
1780e54bcde3SZi Shen Lim 	/* Now we know the actual image size. */
178180083428SJean-Philippe Brucker 	prog_size = sizeof(u32) * ctx.idx;
1782b2ad54e1SXu Kuohai 	/* also allocate space for plt target */
1783b2ad54e1SXu Kuohai 	extable_offset = round_up(prog_size + PLT_TARGET_SIZE, extable_align);
1784b2ad54e1SXu Kuohai 	image_size = extable_offset + extable_size;
17851dad391dSPuranjay Mohan 	ro_header = bpf_jit_binary_pack_alloc(image_size, &ro_image_ptr,
17861dad391dSPuranjay Mohan 					      sizeof(u32), &header, &image_ptr,
17871dad391dSPuranjay Mohan 					      jit_fill_hole);
17881dad391dSPuranjay Mohan 	if (!ro_header) {
178926eb042eSDaniel Borkmann 		prog = orig_prog;
179026eb042eSDaniel Borkmann 		goto out_off;
179126eb042eSDaniel Borkmann 	}
1792e54bcde3SZi Shen Lim 
1793e54bcde3SZi Shen Lim 	/* 2. Now, the actual pass. */
1794e54bcde3SZi Shen Lim 
17951dad391dSPuranjay Mohan 	/*
17961dad391dSPuranjay Mohan 	 * Use the image(RW) for writing the JITed instructions. But also save
17971dad391dSPuranjay Mohan 	 * the ro_image(RX) for calculating the offsets in the image. The RW
17981dad391dSPuranjay Mohan 	 * image will be later copied to the RX image from where the program
17991dad391dSPuranjay Mohan 	 * will run. The bpf_jit_binary_pack_finalize() will do this copy in the
18001dad391dSPuranjay Mohan 	 * final step.
18011dad391dSPuranjay Mohan 	 */
1802425e1ed7SLuc Van Oostenryck 	ctx.image = (__le32 *)image_ptr;
18031dad391dSPuranjay Mohan 	ctx.ro_image = (__le32 *)ro_image_ptr;
180480083428SJean-Philippe Brucker 	if (extable_size)
18051dad391dSPuranjay Mohan 		prog->aux->extable = (void *)ro_image_ptr + extable_offset;
1806db496944SAlexei Starovoitov skip_init_ctx:
1807e54bcde3SZi Shen Lim 	ctx.idx = 0;
180880083428SJean-Philippe Brucker 	ctx.exentry_idx = 0;
1809b569c1c6SDaniel Borkmann 
1810339af577SPuranjay Mohan 	build_prologue(&ctx, was_classic, prog->aux->exception_cb, arena_vm_start);
1811e54bcde3SZi Shen Lim 
18128c11ea5cSDaniel Borkmann 	if (build_body(&ctx, extra_pass)) {
181326eb042eSDaniel Borkmann 		prog = orig_prog;
18141dad391dSPuranjay Mohan 		goto out_free_hdr;
181560ef0494SDaniel Borkmann 	}
1816e54bcde3SZi Shen Lim 
181722fc0e80SPuranjay Mohan 	build_epilogue(&ctx, prog->aux->exception_cb);
1818b2ad54e1SXu Kuohai 	build_plt(&ctx);
1819e54bcde3SZi Shen Lim 
182042ff712bSZi Shen Lim 	/* 3. Extra pass to validate JITed code. */
1821efc9909fSXu Kuohai 	if (validate_ctx(&ctx)) {
182226eb042eSDaniel Borkmann 		prog = orig_prog;
18231dad391dSPuranjay Mohan 		goto out_free_hdr;
182442ff712bSZi Shen Lim 	}
182542ff712bSZi Shen Lim 
1826e54bcde3SZi Shen Lim 	/* And we're done. */
1827e54bcde3SZi Shen Lim 	if (bpf_jit_enable > 1)
182880083428SJean-Philippe Brucker 		bpf_jit_dump(prog->len, prog_size, 2, ctx.image);
1829e54bcde3SZi Shen Lim 
1830db496944SAlexei Starovoitov 	if (!prog->is_func || extra_pass) {
1831db496944SAlexei Starovoitov 		if (extra_pass && ctx.idx != jit_data->ctx.idx) {
1832db496944SAlexei Starovoitov 			pr_err_once("multi-func JIT bug %d != %d\n",
1833db496944SAlexei Starovoitov 				    ctx.idx, jit_data->ctx.idx);
1834db496944SAlexei Starovoitov 			prog->bpf_func = NULL;
1835db496944SAlexei Starovoitov 			prog->jited = 0;
183610f3b29cSEric Dumazet 			prog->jited_len = 0;
18371dad391dSPuranjay Mohan 			goto out_free_hdr;
18381dad391dSPuranjay Mohan 		}
18399919c5c9SRafael Passos 		if (WARN_ON(bpf_jit_binary_pack_finalize(ro_header, header))) {
18401dad391dSPuranjay Mohan 			/* ro_header has been freed */
18411dad391dSPuranjay Mohan 			ro_header = NULL;
18421dad391dSPuranjay Mohan 			prog = orig_prog;
1843db496944SAlexei Starovoitov 			goto out_off;
1844db496944SAlexei Starovoitov 		}
18451dad391dSPuranjay Mohan 		/*
18461dad391dSPuranjay Mohan 		 * The instructions have now been copied to the ROX region from
18471dad391dSPuranjay Mohan 		 * where they will execute. Now the data cache has to be cleaned to
18481dad391dSPuranjay Mohan 		 * the PoU and the I-cache has to be invalidated for the VAs.
18491dad391dSPuranjay Mohan 		 */
18501dad391dSPuranjay Mohan 		bpf_flush_icache(ro_header, ctx.ro_image + ctx.idx);
1851db496944SAlexei Starovoitov 	} else {
1852db496944SAlexei Starovoitov 		jit_data->ctx = ctx;
18531dad391dSPuranjay Mohan 		jit_data->ro_image = ro_image_ptr;
1854db496944SAlexei Starovoitov 		jit_data->header = header;
18551dad391dSPuranjay Mohan 		jit_data->ro_header = ro_header;
1856db496944SAlexei Starovoitov 	}
18571dad391dSPuranjay Mohan 
18581dad391dSPuranjay Mohan 	prog->bpf_func = (void *)ctx.ro_image;
1859a91263d5SDaniel Borkmann 	prog->jited = 1;
186080083428SJean-Philippe Brucker 	prog->jited_len = prog_size;
186126eb042eSDaniel Borkmann 
1862db496944SAlexei Starovoitov 	if (!prog->is_func || extra_pass) {
1863dda7596cSHou Tao 		int i;
1864dda7596cSHou Tao 
1865dda7596cSHou Tao 		/* offset[prog->len] is the size of program */
1866dda7596cSHou Tao 		for (i = 0; i <= prog->len; i++)
1867dda7596cSHou Tao 			ctx.offset[i] *= AARCH64_INSN_SIZE;
186832f6865cSIlias Apalodimas 		bpf_prog_fill_jited_linfo(prog, ctx.offset + 1);
186926eb042eSDaniel Borkmann out_off:
187019f68ed6SAijun Sun 		kvfree(ctx.offset);
1871db496944SAlexei Starovoitov 		kfree(jit_data);
1872db496944SAlexei Starovoitov 		prog->aux->jit_data = NULL;
1873db496944SAlexei Starovoitov 	}
187426eb042eSDaniel Borkmann out:
187526eb042eSDaniel Borkmann 	if (tmp_blinded)
187626eb042eSDaniel Borkmann 		bpf_jit_prog_release_other(prog, prog == orig_prog ?
187726eb042eSDaniel Borkmann 					   tmp : orig_prog);
1878d1c55ab5SDaniel Borkmann 	return prog;
18791dad391dSPuranjay Mohan 
18801dad391dSPuranjay Mohan out_free_hdr:
18811dad391dSPuranjay Mohan 	if (header) {
18821dad391dSPuranjay Mohan 		bpf_arch_text_copy(&ro_header->size, &header->size,
18831dad391dSPuranjay Mohan 				   sizeof(header->size));
18841dad391dSPuranjay Mohan 		bpf_jit_binary_pack_free(ro_header, header);
18851dad391dSPuranjay Mohan 	}
18861dad391dSPuranjay Mohan 	goto out_off;
1887e54bcde3SZi Shen Lim }
188891fc957cSArd Biesheuvel 
1889b5e975d2SHou Tao bool bpf_jit_supports_kfunc_call(void)
1890b5e975d2SHou Tao {
1891b5e975d2SHou Tao 	return true;
1892b5e975d2SHou Tao }
1893b5e975d2SHou Tao 
18941dad391dSPuranjay Mohan void *bpf_arch_text_copy(void *dst, void *src, size_t len)
18951dad391dSPuranjay Mohan {
18961dad391dSPuranjay Mohan 	if (!aarch64_insn_copy(dst, src, len))
18971dad391dSPuranjay Mohan 		return ERR_PTR(-EINVAL);
18981dad391dSPuranjay Mohan 	return dst;
18991dad391dSPuranjay Mohan }
19001dad391dSPuranjay Mohan 
19015d63ae90SLorenz Bauer u64 bpf_jit_alloc_exec_limit(void)
19025d63ae90SLorenz Bauer {
1903b89ddf4cSRussell King 	return VMALLOC_END - VMALLOC_START;
19045d63ae90SLorenz Bauer }
19055d63ae90SLorenz Bauer 
1906d4609a5dSJakub Sitnicki /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
1907d4609a5dSJakub Sitnicki bool bpf_jit_supports_subprog_tailcalls(void)
1908d4609a5dSJakub Sitnicki {
1909d4609a5dSJakub Sitnicki 	return true;
1910d4609a5dSJakub Sitnicki }
1911b2ad54e1SXu Kuohai 
1912efc9909fSXu Kuohai static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_link *l,
1913efc9909fSXu Kuohai 			    int args_off, int retval_off, int run_ctx_off,
1914efc9909fSXu Kuohai 			    bool save_ret)
1915efc9909fSXu Kuohai {
1916aada4766SXu Kuohai 	__le32 *branch;
1917efc9909fSXu Kuohai 	u64 enter_prog;
1918efc9909fSXu Kuohai 	u64 exit_prog;
1919efc9909fSXu Kuohai 	struct bpf_prog *p = l->link.prog;
1920efc9909fSXu Kuohai 	int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
1921efc9909fSXu Kuohai 
1922271de525SMartin KaFai Lau 	enter_prog = (u64)bpf_trampoline_enter(p);
1923271de525SMartin KaFai Lau 	exit_prog = (u64)bpf_trampoline_exit(p);
1924efc9909fSXu Kuohai 
1925efc9909fSXu Kuohai 	if (l->cookie == 0) {
1926efc9909fSXu Kuohai 		/* if cookie is zero, one instruction is enough to store it */
1927efc9909fSXu Kuohai 		emit(A64_STR64I(A64_ZR, A64_SP, run_ctx_off + cookie_off), ctx);
1928efc9909fSXu Kuohai 	} else {
1929efc9909fSXu Kuohai 		emit_a64_mov_i64(A64_R(10), l->cookie, ctx);
1930efc9909fSXu Kuohai 		emit(A64_STR64I(A64_R(10), A64_SP, run_ctx_off + cookie_off),
1931efc9909fSXu Kuohai 		     ctx);
1932efc9909fSXu Kuohai 	}
1933efc9909fSXu Kuohai 
1934efc9909fSXu Kuohai 	/* save p to callee saved register x19 to avoid loading p with mov_i64
1935efc9909fSXu Kuohai 	 * each time.
1936efc9909fSXu Kuohai 	 */
1937efc9909fSXu Kuohai 	emit_addr_mov_i64(A64_R(19), (const u64)p, ctx);
1938efc9909fSXu Kuohai 
1939efc9909fSXu Kuohai 	/* arg1: prog */
1940efc9909fSXu Kuohai 	emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
1941efc9909fSXu Kuohai 	/* arg2: &run_ctx */
1942efc9909fSXu Kuohai 	emit(A64_ADD_I(1, A64_R(1), A64_SP, run_ctx_off), ctx);
1943efc9909fSXu Kuohai 
1944efc9909fSXu Kuohai 	emit_call(enter_prog, ctx);
1945efc9909fSXu Kuohai 
1946dc7d7447SXu Kuohai 	/* save return value to callee saved register x20 */
1947dc7d7447SXu Kuohai 	emit(A64_MOV(1, A64_R(20), A64_R(0)), ctx);
1948dc7d7447SXu Kuohai 
1949efc9909fSXu Kuohai 	/* if (__bpf_prog_enter(prog) == 0)
1950efc9909fSXu Kuohai 	 *         goto skip_exec_of_prog;
1951efc9909fSXu Kuohai 	 */
1952efc9909fSXu Kuohai 	branch = ctx->image + ctx->idx;
1953efc9909fSXu Kuohai 	emit(A64_NOP, ctx);
1954efc9909fSXu Kuohai 
1955efc9909fSXu Kuohai 	emit(A64_ADD_I(1, A64_R(0), A64_SP, args_off), ctx);
1956efc9909fSXu Kuohai 	if (!p->jited)
1957efc9909fSXu Kuohai 		emit_addr_mov_i64(A64_R(1), (const u64)p->insnsi, ctx);
1958efc9909fSXu Kuohai 
1959efc9909fSXu Kuohai 	emit_call((const u64)p->bpf_func, ctx);
1960efc9909fSXu Kuohai 
1961efc9909fSXu Kuohai 	if (save_ret)
1962efc9909fSXu Kuohai 		emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
1963efc9909fSXu Kuohai 
1964efc9909fSXu Kuohai 	if (ctx->image) {
1965efc9909fSXu Kuohai 		int offset = &ctx->image[ctx->idx] - branch;
1966aada4766SXu Kuohai 		*branch = cpu_to_le32(A64_CBZ(1, A64_R(0), offset));
1967efc9909fSXu Kuohai 	}
1968efc9909fSXu Kuohai 
1969efc9909fSXu Kuohai 	/* arg1: prog */
1970efc9909fSXu Kuohai 	emit(A64_MOV(1, A64_R(0), A64_R(19)), ctx);
1971efc9909fSXu Kuohai 	/* arg2: start time */
1972efc9909fSXu Kuohai 	emit(A64_MOV(1, A64_R(1), A64_R(20)), ctx);
1973efc9909fSXu Kuohai 	/* arg3: &run_ctx */
1974efc9909fSXu Kuohai 	emit(A64_ADD_I(1, A64_R(2), A64_SP, run_ctx_off), ctx);
1975efc9909fSXu Kuohai 
1976efc9909fSXu Kuohai 	emit_call(exit_prog, ctx);
1977efc9909fSXu Kuohai }
1978efc9909fSXu Kuohai 
1979efc9909fSXu Kuohai static void invoke_bpf_mod_ret(struct jit_ctx *ctx, struct bpf_tramp_links *tl,
1980efc9909fSXu Kuohai 			       int args_off, int retval_off, int run_ctx_off,
1981aada4766SXu Kuohai 			       __le32 **branches)
1982efc9909fSXu Kuohai {
1983efc9909fSXu Kuohai 	int i;
1984efc9909fSXu Kuohai 
1985efc9909fSXu Kuohai 	/* The first fmod_ret program will receive a garbage return value.
1986efc9909fSXu Kuohai 	 * Set this to 0 to avoid confusing the program.
1987efc9909fSXu Kuohai 	 */
1988efc9909fSXu Kuohai 	emit(A64_STR64I(A64_ZR, A64_SP, retval_off), ctx);
1989efc9909fSXu Kuohai 	for (i = 0; i < tl->nr_links; i++) {
1990efc9909fSXu Kuohai 		invoke_bpf_prog(ctx, tl->links[i], args_off, retval_off,
1991efc9909fSXu Kuohai 				run_ctx_off, true);
1992efc9909fSXu Kuohai 		/* if (*(u64 *)(sp + retval_off) !=  0)
1993efc9909fSXu Kuohai 		 *	goto do_fexit;
1994efc9909fSXu Kuohai 		 */
1995efc9909fSXu Kuohai 		emit(A64_LDR64I(A64_R(10), A64_SP, retval_off), ctx);
1996efc9909fSXu Kuohai 		/* Save the location of branch, and generate a nop.
1997efc9909fSXu Kuohai 		 * This nop will be replaced with a cbnz later.
1998efc9909fSXu Kuohai 		 */
1999efc9909fSXu Kuohai 		branches[i] = ctx->image + ctx->idx;
2000efc9909fSXu Kuohai 		emit(A64_NOP, ctx);
2001efc9909fSXu Kuohai 	}
2002efc9909fSXu Kuohai }
2003efc9909fSXu Kuohai 
200490564f1eSFlorent Revest static void save_args(struct jit_ctx *ctx, int args_off, int nregs)
2005efc9909fSXu Kuohai {
2006efc9909fSXu Kuohai 	int i;
2007efc9909fSXu Kuohai 
200890564f1eSFlorent Revest 	for (i = 0; i < nregs; i++) {
2009efc9909fSXu Kuohai 		emit(A64_STR64I(i, A64_SP, args_off), ctx);
2010efc9909fSXu Kuohai 		args_off += 8;
2011efc9909fSXu Kuohai 	}
2012efc9909fSXu Kuohai }
2013efc9909fSXu Kuohai 
201490564f1eSFlorent Revest static void restore_args(struct jit_ctx *ctx, int args_off, int nregs)
2015efc9909fSXu Kuohai {
2016efc9909fSXu Kuohai 	int i;
2017efc9909fSXu Kuohai 
201890564f1eSFlorent Revest 	for (i = 0; i < nregs; i++) {
2019efc9909fSXu Kuohai 		emit(A64_LDR64I(i, A64_SP, args_off), ctx);
2020efc9909fSXu Kuohai 		args_off += 8;
2021efc9909fSXu Kuohai 	}
2022efc9909fSXu Kuohai }
2023efc9909fSXu Kuohai 
2024efc9909fSXu Kuohai /* Based on the x86's implementation of arch_prepare_bpf_trampoline().
2025efc9909fSXu Kuohai  *
2026efc9909fSXu Kuohai  * bpf prog and function entry before bpf trampoline hooked:
2027efc9909fSXu Kuohai  *   mov x9, lr
2028efc9909fSXu Kuohai  *   nop
2029efc9909fSXu Kuohai  *
2030efc9909fSXu Kuohai  * bpf prog and function entry after bpf trampoline hooked:
2031efc9909fSXu Kuohai  *   mov x9, lr
2032efc9909fSXu Kuohai  *   bl  <bpf_trampoline or plt>
2033efc9909fSXu Kuohai  *
2034efc9909fSXu Kuohai  */
2035efc9909fSXu Kuohai static int prepare_trampoline(struct jit_ctx *ctx, struct bpf_tramp_image *im,
20367a3d9a15SSong Liu 			      struct bpf_tramp_links *tlinks, void *func_addr,
203790564f1eSFlorent Revest 			      int nregs, u32 flags)
2038efc9909fSXu Kuohai {
2039efc9909fSXu Kuohai 	int i;
2040efc9909fSXu Kuohai 	int stack_size;
2041efc9909fSXu Kuohai 	int retaddr_off;
2042efc9909fSXu Kuohai 	int regs_off;
2043efc9909fSXu Kuohai 	int retval_off;
2044efc9909fSXu Kuohai 	int args_off;
204590564f1eSFlorent Revest 	int nregs_off;
2046efc9909fSXu Kuohai 	int ip_off;
2047efc9909fSXu Kuohai 	int run_ctx_off;
2048efc9909fSXu Kuohai 	struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
2049efc9909fSXu Kuohai 	struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
2050efc9909fSXu Kuohai 	struct bpf_tramp_links *fmod_ret = &tlinks[BPF_TRAMP_MODIFY_RETURN];
2051efc9909fSXu Kuohai 	bool save_ret;
2052aada4766SXu Kuohai 	__le32 **branches = NULL;
2053efc9909fSXu Kuohai 
2054efc9909fSXu Kuohai 	/* trampoline stack layout:
2055efc9909fSXu Kuohai 	 *                  [ parent ip         ]
2056efc9909fSXu Kuohai 	 *                  [ FP                ]
2057efc9909fSXu Kuohai 	 * SP + retaddr_off [ self ip           ]
2058efc9909fSXu Kuohai 	 *                  [ FP                ]
2059efc9909fSXu Kuohai 	 *
2060efc9909fSXu Kuohai 	 *                  [ padding           ] align SP to multiples of 16
2061efc9909fSXu Kuohai 	 *
2062efc9909fSXu Kuohai 	 *                  [ x20               ] callee saved reg x20
2063efc9909fSXu Kuohai 	 * SP + regs_off    [ x19               ] callee saved reg x19
2064efc9909fSXu Kuohai 	 *
2065efc9909fSXu Kuohai 	 * SP + retval_off  [ return value      ] BPF_TRAMP_F_CALL_ORIG or
2066efc9909fSXu Kuohai 	 *                                        BPF_TRAMP_F_RET_FENTRY_RET
2067efc9909fSXu Kuohai 	 *
206890564f1eSFlorent Revest 	 *                  [ arg reg N         ]
2069efc9909fSXu Kuohai 	 *                  [ ...               ]
207090564f1eSFlorent Revest 	 * SP + args_off    [ arg reg 1         ]
2071efc9909fSXu Kuohai 	 *
207290564f1eSFlorent Revest 	 * SP + nregs_off   [ arg regs count    ]
2073efc9909fSXu Kuohai 	 *
2074efc9909fSXu Kuohai 	 * SP + ip_off      [ traced function   ] BPF_TRAMP_F_IP_ARG flag
2075efc9909fSXu Kuohai 	 *
2076efc9909fSXu Kuohai 	 * SP + run_ctx_off [ bpf_tramp_run_ctx ]
2077efc9909fSXu Kuohai 	 */
2078efc9909fSXu Kuohai 
2079efc9909fSXu Kuohai 	stack_size = 0;
2080efc9909fSXu Kuohai 	run_ctx_off = stack_size;
2081efc9909fSXu Kuohai 	/* room for bpf_tramp_run_ctx */
2082efc9909fSXu Kuohai 	stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
2083efc9909fSXu Kuohai 
2084efc9909fSXu Kuohai 	ip_off = stack_size;
2085efc9909fSXu Kuohai 	/* room for IP address argument */
2086efc9909fSXu Kuohai 	if (flags & BPF_TRAMP_F_IP_ARG)
2087efc9909fSXu Kuohai 		stack_size += 8;
2088efc9909fSXu Kuohai 
208990564f1eSFlorent Revest 	nregs_off = stack_size;
2090efc9909fSXu Kuohai 	/* room for args count */
2091efc9909fSXu Kuohai 	stack_size += 8;
2092efc9909fSXu Kuohai 
2093efc9909fSXu Kuohai 	args_off = stack_size;
2094efc9909fSXu Kuohai 	/* room for args */
209590564f1eSFlorent Revest 	stack_size += nregs * 8;
2096efc9909fSXu Kuohai 
2097efc9909fSXu Kuohai 	/* room for return value */
2098efc9909fSXu Kuohai 	retval_off = stack_size;
2099efc9909fSXu Kuohai 	save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
2100efc9909fSXu Kuohai 	if (save_ret)
2101efc9909fSXu Kuohai 		stack_size += 8;
2102efc9909fSXu Kuohai 
2103efc9909fSXu Kuohai 	/* room for callee saved registers, currently x19 and x20 are used */
2104efc9909fSXu Kuohai 	regs_off = stack_size;
2105efc9909fSXu Kuohai 	stack_size += 16;
2106efc9909fSXu Kuohai 
2107efc9909fSXu Kuohai 	/* round up to multiples of 16 to avoid SPAlignmentFault */
2108efc9909fSXu Kuohai 	stack_size = round_up(stack_size, 16);
2109efc9909fSXu Kuohai 
2110efc9909fSXu Kuohai 	/* return address locates above FP */
2111efc9909fSXu Kuohai 	retaddr_off = stack_size + 8;
2112efc9909fSXu Kuohai 
2113efc9909fSXu Kuohai 	/* bpf trampoline may be invoked by 3 instruction types:
2114efc9909fSXu Kuohai 	 * 1. bl, attached to bpf prog or kernel function via short jump
2115efc9909fSXu Kuohai 	 * 2. br, attached to bpf prog or kernel function via long jump
2116efc9909fSXu Kuohai 	 * 3. blr, working as a function pointer, used by struct_ops.
2117efc9909fSXu Kuohai 	 * So BTI_JC should used here to support both br and blr.
2118efc9909fSXu Kuohai 	 */
2119efc9909fSXu Kuohai 	emit_bti(A64_BTI_JC, ctx);
2120efc9909fSXu Kuohai 
2121efc9909fSXu Kuohai 	/* frame for parent function */
2122efc9909fSXu Kuohai 	emit(A64_PUSH(A64_FP, A64_R(9), A64_SP), ctx);
2123efc9909fSXu Kuohai 	emit(A64_MOV(1, A64_FP, A64_SP), ctx);
2124efc9909fSXu Kuohai 
2125efc9909fSXu Kuohai 	/* frame for patched function */
2126efc9909fSXu Kuohai 	emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
2127efc9909fSXu Kuohai 	emit(A64_MOV(1, A64_FP, A64_SP), ctx);
2128efc9909fSXu Kuohai 
2129efc9909fSXu Kuohai 	/* allocate stack space */
2130efc9909fSXu Kuohai 	emit(A64_SUB_I(1, A64_SP, A64_SP, stack_size), ctx);
2131efc9909fSXu Kuohai 
2132efc9909fSXu Kuohai 	if (flags & BPF_TRAMP_F_IP_ARG) {
2133efc9909fSXu Kuohai 		/* save ip address of the traced function */
21347a3d9a15SSong Liu 		emit_addr_mov_i64(A64_R(10), (const u64)func_addr, ctx);
2135efc9909fSXu Kuohai 		emit(A64_STR64I(A64_R(10), A64_SP, ip_off), ctx);
2136efc9909fSXu Kuohai 	}
2137efc9909fSXu Kuohai 
213890564f1eSFlorent Revest 	/* save arg regs count*/
213990564f1eSFlorent Revest 	emit(A64_MOVZ(1, A64_R(10), nregs, 0), ctx);
214090564f1eSFlorent Revest 	emit(A64_STR64I(A64_R(10), A64_SP, nregs_off), ctx);
2141efc9909fSXu Kuohai 
214290564f1eSFlorent Revest 	/* save arg regs */
214390564f1eSFlorent Revest 	save_args(ctx, args_off, nregs);
2144efc9909fSXu Kuohai 
2145efc9909fSXu Kuohai 	/* save callee saved registers */
2146efc9909fSXu Kuohai 	emit(A64_STR64I(A64_R(19), A64_SP, regs_off), ctx);
2147efc9909fSXu Kuohai 	emit(A64_STR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
2148efc9909fSXu Kuohai 
2149efc9909fSXu Kuohai 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
2150*19d3c179SPuranjay Mohan 		emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
2151efc9909fSXu Kuohai 		emit_call((const u64)__bpf_tramp_enter, ctx);
2152efc9909fSXu Kuohai 	}
2153efc9909fSXu Kuohai 
2154efc9909fSXu Kuohai 	for (i = 0; i < fentry->nr_links; i++)
2155efc9909fSXu Kuohai 		invoke_bpf_prog(ctx, fentry->links[i], args_off,
2156efc9909fSXu Kuohai 				retval_off, run_ctx_off,
2157efc9909fSXu Kuohai 				flags & BPF_TRAMP_F_RET_FENTRY_RET);
2158efc9909fSXu Kuohai 
2159efc9909fSXu Kuohai 	if (fmod_ret->nr_links) {
2160aada4766SXu Kuohai 		branches = kcalloc(fmod_ret->nr_links, sizeof(__le32 *),
2161efc9909fSXu Kuohai 				   GFP_KERNEL);
2162efc9909fSXu Kuohai 		if (!branches)
2163efc9909fSXu Kuohai 			return -ENOMEM;
2164efc9909fSXu Kuohai 
2165efc9909fSXu Kuohai 		invoke_bpf_mod_ret(ctx, fmod_ret, args_off, retval_off,
2166efc9909fSXu Kuohai 				   run_ctx_off, branches);
2167efc9909fSXu Kuohai 	}
2168efc9909fSXu Kuohai 
2169efc9909fSXu Kuohai 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
217090564f1eSFlorent Revest 		restore_args(ctx, args_off, nregs);
2171efc9909fSXu Kuohai 		/* call original func */
2172efc9909fSXu Kuohai 		emit(A64_LDR64I(A64_R(10), A64_SP, retaddr_off), ctx);
2173738a96c4SXu Kuohai 		emit(A64_ADR(A64_LR, AARCH64_INSN_SIZE * 2), ctx);
2174738a96c4SXu Kuohai 		emit(A64_RET(A64_R(10)), ctx);
2175efc9909fSXu Kuohai 		/* store return value */
2176efc9909fSXu Kuohai 		emit(A64_STR64I(A64_R(0), A64_SP, retval_off), ctx);
2177efc9909fSXu Kuohai 		/* reserve a nop for bpf_tramp_image_put */
217896b0f5adSPuranjay Mohan 		im->ip_after_call = ctx->ro_image + ctx->idx;
2179efc9909fSXu Kuohai 		emit(A64_NOP, ctx);
2180efc9909fSXu Kuohai 	}
2181efc9909fSXu Kuohai 
2182efc9909fSXu Kuohai 	/* update the branches saved in invoke_bpf_mod_ret with cbnz */
2183efc9909fSXu Kuohai 	for (i = 0; i < fmod_ret->nr_links && ctx->image != NULL; i++) {
2184efc9909fSXu Kuohai 		int offset = &ctx->image[ctx->idx] - branches[i];
2185aada4766SXu Kuohai 		*branches[i] = cpu_to_le32(A64_CBNZ(1, A64_R(10), offset));
2186efc9909fSXu Kuohai 	}
2187efc9909fSXu Kuohai 
2188efc9909fSXu Kuohai 	for (i = 0; i < fexit->nr_links; i++)
2189efc9909fSXu Kuohai 		invoke_bpf_prog(ctx, fexit->links[i], args_off, retval_off,
2190efc9909fSXu Kuohai 				run_ctx_off, false);
2191efc9909fSXu Kuohai 
2192efc9909fSXu Kuohai 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
219396b0f5adSPuranjay Mohan 		im->ip_epilogue = ctx->ro_image + ctx->idx;
2194*19d3c179SPuranjay Mohan 		emit_a64_mov_i64(A64_R(0), (const u64)im, ctx);
2195efc9909fSXu Kuohai 		emit_call((const u64)__bpf_tramp_exit, ctx);
2196efc9909fSXu Kuohai 	}
2197efc9909fSXu Kuohai 
2198efc9909fSXu Kuohai 	if (flags & BPF_TRAMP_F_RESTORE_REGS)
219990564f1eSFlorent Revest 		restore_args(ctx, args_off, nregs);
2200efc9909fSXu Kuohai 
2201efc9909fSXu Kuohai 	/* restore callee saved register x19 and x20 */
2202efc9909fSXu Kuohai 	emit(A64_LDR64I(A64_R(19), A64_SP, regs_off), ctx);
2203efc9909fSXu Kuohai 	emit(A64_LDR64I(A64_R(20), A64_SP, regs_off + 8), ctx);
2204efc9909fSXu Kuohai 
2205efc9909fSXu Kuohai 	if (save_ret)
2206efc9909fSXu Kuohai 		emit(A64_LDR64I(A64_R(0), A64_SP, retval_off), ctx);
2207efc9909fSXu Kuohai 
2208efc9909fSXu Kuohai 	/* reset SP  */
2209efc9909fSXu Kuohai 	emit(A64_MOV(1, A64_SP, A64_FP), ctx);
2210efc9909fSXu Kuohai 
2211efc9909fSXu Kuohai 	/* pop frames  */
2212efc9909fSXu Kuohai 	emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
2213efc9909fSXu Kuohai 	emit(A64_POP(A64_FP, A64_R(9), A64_SP), ctx);
2214efc9909fSXu Kuohai 
2215efc9909fSXu Kuohai 	if (flags & BPF_TRAMP_F_SKIP_FRAME) {
2216efc9909fSXu Kuohai 		/* skip patched function, return to parent */
2217efc9909fSXu Kuohai 		emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
2218efc9909fSXu Kuohai 		emit(A64_RET(A64_R(9)), ctx);
2219efc9909fSXu Kuohai 	} else {
2220efc9909fSXu Kuohai 		/* return to patched function */
2221efc9909fSXu Kuohai 		emit(A64_MOV(1, A64_R(10), A64_LR), ctx);
2222efc9909fSXu Kuohai 		emit(A64_MOV(1, A64_LR, A64_R(9)), ctx);
2223efc9909fSXu Kuohai 		emit(A64_RET(A64_R(10)), ctx);
2224efc9909fSXu Kuohai 	}
2225efc9909fSXu Kuohai 
2226efc9909fSXu Kuohai 	kfree(branches);
2227efc9909fSXu Kuohai 
2228efc9909fSXu Kuohai 	return ctx->idx;
2229efc9909fSXu Kuohai }
2230efc9909fSXu Kuohai 
223196d1b7c0SSong Liu static int btf_func_model_nregs(const struct btf_func_model *m)
2232efc9909fSXu Kuohai {
223390564f1eSFlorent Revest 	int nregs = m->nr_args;
223496d1b7c0SSong Liu 	int i;
2235efc9909fSXu Kuohai 
223690564f1eSFlorent Revest 	/* extra registers needed for struct argument */
2237eb707ddeSYonghong Song 	for (i = 0; i < MAX_BPF_FUNC_ARGS; i++) {
223890564f1eSFlorent Revest 		/* The arg_size is at most 16 bytes, enforced by the verifier. */
2239eb707ddeSYonghong Song 		if (m->arg_flags[i] & BTF_FMODEL_STRUCT_ARG)
224090564f1eSFlorent Revest 			nregs += (m->arg_size[i] + 7) / 8 - 1;
2241eb707ddeSYonghong Song 	}
2242eb707ddeSYonghong Song 
224396d1b7c0SSong Liu 	return nregs;
224496d1b7c0SSong Liu }
224596d1b7c0SSong Liu 
224696d1b7c0SSong Liu int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
224796d1b7c0SSong Liu 			     struct bpf_tramp_links *tlinks, void *func_addr)
224896d1b7c0SSong Liu {
224996d1b7c0SSong Liu 	struct jit_ctx ctx = {
225096d1b7c0SSong Liu 		.image = NULL,
225196d1b7c0SSong Liu 		.idx = 0,
225296d1b7c0SSong Liu 	};
225396d1b7c0SSong Liu 	struct bpf_tramp_image im;
225496d1b7c0SSong Liu 	int nregs, ret;
225596d1b7c0SSong Liu 
225696d1b7c0SSong Liu 	nregs = btf_func_model_nregs(m);
225790564f1eSFlorent Revest 	/* the first 8 registers are used for arguments */
225890564f1eSFlorent Revest 	if (nregs > 8)
225990564f1eSFlorent Revest 		return -ENOTSUPP;
226090564f1eSFlorent Revest 
226196d1b7c0SSong Liu 	ret = prepare_trampoline(&ctx, &im, tlinks, func_addr, nregs, flags);
2262efc9909fSXu Kuohai 	if (ret < 0)
2263efc9909fSXu Kuohai 		return ret;
2264efc9909fSXu Kuohai 
226596d1b7c0SSong Liu 	return ret < 0 ? ret : ret * AARCH64_INSN_SIZE;
226696d1b7c0SSong Liu }
2267efc9909fSXu Kuohai 
226896b0f5adSPuranjay Mohan void *arch_alloc_bpf_trampoline(unsigned int size)
226996b0f5adSPuranjay Mohan {
227096b0f5adSPuranjay Mohan 	return bpf_prog_pack_alloc(size, jit_fill_hole);
227196b0f5adSPuranjay Mohan }
227296b0f5adSPuranjay Mohan 
227396b0f5adSPuranjay Mohan void arch_free_bpf_trampoline(void *image, unsigned int size)
227496b0f5adSPuranjay Mohan {
227596b0f5adSPuranjay Mohan 	bpf_prog_pack_free(image, size);
227696b0f5adSPuranjay Mohan }
227796b0f5adSPuranjay Mohan 
2278c733239fSChristophe Leroy int arch_protect_bpf_trampoline(void *image, unsigned int size)
227996b0f5adSPuranjay Mohan {
2280c733239fSChristophe Leroy 	return 0;
228196b0f5adSPuranjay Mohan }
228296b0f5adSPuranjay Mohan 
228396b0f5adSPuranjay Mohan int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
228496b0f5adSPuranjay Mohan 				void *ro_image_end, const struct btf_func_model *m,
228596d1b7c0SSong Liu 				u32 flags, struct bpf_tramp_links *tlinks,
228696d1b7c0SSong Liu 				void *func_addr)
228796d1b7c0SSong Liu {
228896d1b7c0SSong Liu 	int ret, nregs;
228996b0f5adSPuranjay Mohan 	void *image, *tmp;
229096b0f5adSPuranjay Mohan 	u32 size = ro_image_end - ro_image;
229196b0f5adSPuranjay Mohan 
229296b0f5adSPuranjay Mohan 	/* image doesn't need to be in module memory range, so we can
229396b0f5adSPuranjay Mohan 	 * use kvmalloc.
229496b0f5adSPuranjay Mohan 	 */
229596b0f5adSPuranjay Mohan 	image = kvmalloc(size, GFP_KERNEL);
229696b0f5adSPuranjay Mohan 	if (!image)
229796b0f5adSPuranjay Mohan 		return -ENOMEM;
229896b0f5adSPuranjay Mohan 
229996d1b7c0SSong Liu 	struct jit_ctx ctx = {
230096d1b7c0SSong Liu 		.image = image,
230196b0f5adSPuranjay Mohan 		.ro_image = ro_image,
230296d1b7c0SSong Liu 		.idx = 0,
230396d1b7c0SSong Liu 	};
230496d1b7c0SSong Liu 
230596d1b7c0SSong Liu 	nregs = btf_func_model_nregs(m);
230696d1b7c0SSong Liu 	/* the first 8 registers are used for arguments */
230796d1b7c0SSong Liu 	if (nregs > 8)
230896d1b7c0SSong Liu 		return -ENOTSUPP;
230996d1b7c0SSong Liu 
231096b0f5adSPuranjay Mohan 	jit_fill_hole(image, (unsigned int)(ro_image_end - ro_image));
23117a3d9a15SSong Liu 	ret = prepare_trampoline(&ctx, im, tlinks, func_addr, nregs, flags);
2312efc9909fSXu Kuohai 
231396b0f5adSPuranjay Mohan 	if (ret > 0 && validate_code(&ctx) < 0) {
2314efc9909fSXu Kuohai 		ret = -EINVAL;
231596b0f5adSPuranjay Mohan 		goto out;
231696b0f5adSPuranjay Mohan 	}
2317efc9909fSXu Kuohai 
2318efc9909fSXu Kuohai 	if (ret > 0)
2319efc9909fSXu Kuohai 		ret *= AARCH64_INSN_SIZE;
2320efc9909fSXu Kuohai 
232196b0f5adSPuranjay Mohan 	tmp = bpf_arch_text_copy(ro_image, image, size);
232296b0f5adSPuranjay Mohan 	if (IS_ERR(tmp)) {
232396b0f5adSPuranjay Mohan 		ret = PTR_ERR(tmp);
232496b0f5adSPuranjay Mohan 		goto out;
232596b0f5adSPuranjay Mohan 	}
232696b0f5adSPuranjay Mohan 
232796b0f5adSPuranjay Mohan 	bpf_flush_icache(ro_image, ro_image + size);
232896b0f5adSPuranjay Mohan out:
232996b0f5adSPuranjay Mohan 	kvfree(image);
2330efc9909fSXu Kuohai 	return ret;
2331efc9909fSXu Kuohai }
2332efc9909fSXu Kuohai 
2333b2ad54e1SXu Kuohai static bool is_long_jump(void *ip, void *target)
2334b2ad54e1SXu Kuohai {
2335b2ad54e1SXu Kuohai 	long offset;
2336b2ad54e1SXu Kuohai 
2337b2ad54e1SXu Kuohai 	/* NULL target means this is a NOP */
2338b2ad54e1SXu Kuohai 	if (!target)
2339b2ad54e1SXu Kuohai 		return false;
2340b2ad54e1SXu Kuohai 
2341b2ad54e1SXu Kuohai 	offset = (long)target - (long)ip;
2342b2ad54e1SXu Kuohai 	return offset < -SZ_128M || offset >= SZ_128M;
2343b2ad54e1SXu Kuohai }
2344b2ad54e1SXu Kuohai 
2345b2ad54e1SXu Kuohai static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip,
2346b2ad54e1SXu Kuohai 			     void *addr, void *plt, u32 *insn)
2347b2ad54e1SXu Kuohai {
2348b2ad54e1SXu Kuohai 	void *target;
2349b2ad54e1SXu Kuohai 
2350b2ad54e1SXu Kuohai 	if (!addr) {
2351b2ad54e1SXu Kuohai 		*insn = aarch64_insn_gen_nop();
2352b2ad54e1SXu Kuohai 		return 0;
2353b2ad54e1SXu Kuohai 	}
2354b2ad54e1SXu Kuohai 
2355b2ad54e1SXu Kuohai 	if (is_long_jump(ip, addr))
2356b2ad54e1SXu Kuohai 		target = plt;
2357b2ad54e1SXu Kuohai 	else
2358b2ad54e1SXu Kuohai 		target = addr;
2359b2ad54e1SXu Kuohai 
2360b2ad54e1SXu Kuohai 	*insn = aarch64_insn_gen_branch_imm((unsigned long)ip,
2361b2ad54e1SXu Kuohai 					    (unsigned long)target,
2362b2ad54e1SXu Kuohai 					    type);
2363b2ad54e1SXu Kuohai 
2364b2ad54e1SXu Kuohai 	return *insn != AARCH64_BREAK_FAULT ? 0 : -EFAULT;
2365b2ad54e1SXu Kuohai }
2366b2ad54e1SXu Kuohai 
2367b2ad54e1SXu Kuohai /* Replace the branch instruction from @ip to @old_addr in a bpf prog or a bpf
2368b2ad54e1SXu Kuohai  * trampoline with the branch instruction from @ip to @new_addr. If @old_addr
2369b2ad54e1SXu Kuohai  * or @new_addr is NULL, the old or new instruction is NOP.
2370b2ad54e1SXu Kuohai  *
2371b2ad54e1SXu Kuohai  * When @ip is the bpf prog entry, a bpf trampoline is being attached or
2372b2ad54e1SXu Kuohai  * detached. Since bpf trampoline and bpf prog are allocated separately with
2373b2ad54e1SXu Kuohai  * vmalloc, the address distance may exceed 128MB, the maximum branch range.
2374b2ad54e1SXu Kuohai  * So long jump should be handled.
2375b2ad54e1SXu Kuohai  *
2376b2ad54e1SXu Kuohai  * When a bpf prog is constructed, a plt pointing to empty trampoline
2377b2ad54e1SXu Kuohai  * dummy_tramp is placed at the end:
2378b2ad54e1SXu Kuohai  *
2379b2ad54e1SXu Kuohai  *      bpf_prog:
2380b2ad54e1SXu Kuohai  *              mov x9, lr
2381b2ad54e1SXu Kuohai  *              nop // patchsite
2382b2ad54e1SXu Kuohai  *              ...
2383b2ad54e1SXu Kuohai  *              ret
2384b2ad54e1SXu Kuohai  *
2385b2ad54e1SXu Kuohai  *      plt:
2386b2ad54e1SXu Kuohai  *              ldr x10, target
2387b2ad54e1SXu Kuohai  *              br x10
2388b2ad54e1SXu Kuohai  *      target:
2389b2ad54e1SXu Kuohai  *              .quad dummy_tramp // plt target
2390b2ad54e1SXu Kuohai  *
2391b2ad54e1SXu Kuohai  * This is also the state when no trampoline is attached.
2392b2ad54e1SXu Kuohai  *
2393b2ad54e1SXu Kuohai  * When a short-jump bpf trampoline is attached, the patchsite is patched
2394b2ad54e1SXu Kuohai  * to a bl instruction to the trampoline directly:
2395b2ad54e1SXu Kuohai  *
2396b2ad54e1SXu Kuohai  *      bpf_prog:
2397b2ad54e1SXu Kuohai  *              mov x9, lr
2398b2ad54e1SXu Kuohai  *              bl <short-jump bpf trampoline address> // patchsite
2399b2ad54e1SXu Kuohai  *              ...
2400b2ad54e1SXu Kuohai  *              ret
2401b2ad54e1SXu Kuohai  *
2402b2ad54e1SXu Kuohai  *      plt:
2403b2ad54e1SXu Kuohai  *              ldr x10, target
2404b2ad54e1SXu Kuohai  *              br x10
2405b2ad54e1SXu Kuohai  *      target:
2406b2ad54e1SXu Kuohai  *              .quad dummy_tramp // plt target
2407b2ad54e1SXu Kuohai  *
2408b2ad54e1SXu Kuohai  * When a long-jump bpf trampoline is attached, the plt target is filled with
2409b2ad54e1SXu Kuohai  * the trampoline address and the patchsite is patched to a bl instruction to
2410b2ad54e1SXu Kuohai  * the plt:
2411b2ad54e1SXu Kuohai  *
2412b2ad54e1SXu Kuohai  *      bpf_prog:
2413b2ad54e1SXu Kuohai  *              mov x9, lr
2414b2ad54e1SXu Kuohai  *              bl plt // patchsite
2415b2ad54e1SXu Kuohai  *              ...
2416b2ad54e1SXu Kuohai  *              ret
2417b2ad54e1SXu Kuohai  *
2418b2ad54e1SXu Kuohai  *      plt:
2419b2ad54e1SXu Kuohai  *              ldr x10, target
2420b2ad54e1SXu Kuohai  *              br x10
2421b2ad54e1SXu Kuohai  *      target:
2422b2ad54e1SXu Kuohai  *              .quad <long-jump bpf trampoline address> // plt target
2423b2ad54e1SXu Kuohai  *
2424b2ad54e1SXu Kuohai  * The dummy_tramp is used to prevent another CPU from jumping to unknown
2425b2ad54e1SXu Kuohai  * locations during the patching process, making the patching process easier.
2426b2ad54e1SXu Kuohai  */
2427b2ad54e1SXu Kuohai int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
2428b2ad54e1SXu Kuohai 		       void *old_addr, void *new_addr)
2429b2ad54e1SXu Kuohai {
2430b2ad54e1SXu Kuohai 	int ret;
2431b2ad54e1SXu Kuohai 	u32 old_insn;
2432b2ad54e1SXu Kuohai 	u32 new_insn;
2433b2ad54e1SXu Kuohai 	u32 replaced;
2434b2ad54e1SXu Kuohai 	struct bpf_plt *plt = NULL;
2435b2ad54e1SXu Kuohai 	unsigned long size = 0UL;
2436b2ad54e1SXu Kuohai 	unsigned long offset = ~0UL;
2437b2ad54e1SXu Kuohai 	enum aarch64_insn_branch_type branch_type;
2438b2ad54e1SXu Kuohai 	char namebuf[KSYM_NAME_LEN];
2439b2ad54e1SXu Kuohai 	void *image = NULL;
2440b2ad54e1SXu Kuohai 	u64 plt_target = 0ULL;
2441b2ad54e1SXu Kuohai 	bool poking_bpf_entry;
2442b2ad54e1SXu Kuohai 
2443b2ad54e1SXu Kuohai 	if (!__bpf_address_lookup((unsigned long)ip, &size, &offset, namebuf))
2444b2ad54e1SXu Kuohai 		/* Only poking bpf text is supported. Since kernel function
2445b2ad54e1SXu Kuohai 		 * entry is set up by ftrace, we reply on ftrace to poke kernel
2446b2ad54e1SXu Kuohai 		 * functions.
2447b2ad54e1SXu Kuohai 		 */
2448b2ad54e1SXu Kuohai 		return -ENOTSUPP;
2449b2ad54e1SXu Kuohai 
2450b2ad54e1SXu Kuohai 	image = ip - offset;
2451b2ad54e1SXu Kuohai 	/* zero offset means we're poking bpf prog entry */
2452b2ad54e1SXu Kuohai 	poking_bpf_entry = (offset == 0UL);
2453b2ad54e1SXu Kuohai 
2454b2ad54e1SXu Kuohai 	/* bpf prog entry, find plt and the real patchsite */
2455b2ad54e1SXu Kuohai 	if (poking_bpf_entry) {
2456b2ad54e1SXu Kuohai 		/* plt locates at the end of bpf prog */
2457b2ad54e1SXu Kuohai 		plt = image + size - PLT_TARGET_OFFSET;
2458b2ad54e1SXu Kuohai 
2459b2ad54e1SXu Kuohai 		/* skip to the nop instruction in bpf prog entry:
2460b2ad54e1SXu Kuohai 		 * bti c // if BTI enabled
2461b2ad54e1SXu Kuohai 		 * mov x9, x30
2462b2ad54e1SXu Kuohai 		 * nop
2463b2ad54e1SXu Kuohai 		 */
2464b2ad54e1SXu Kuohai 		ip = image + POKE_OFFSET * AARCH64_INSN_SIZE;
2465b2ad54e1SXu Kuohai 	}
2466b2ad54e1SXu Kuohai 
2467b2ad54e1SXu Kuohai 	/* long jump is only possible at bpf prog entry */
2468b2ad54e1SXu Kuohai 	if (WARN_ON((is_long_jump(ip, new_addr) || is_long_jump(ip, old_addr)) &&
2469b2ad54e1SXu Kuohai 		    !poking_bpf_entry))
2470b2ad54e1SXu Kuohai 		return -EINVAL;
2471b2ad54e1SXu Kuohai 
2472b2ad54e1SXu Kuohai 	if (poke_type == BPF_MOD_CALL)
2473b2ad54e1SXu Kuohai 		branch_type = AARCH64_INSN_BRANCH_LINK;
2474b2ad54e1SXu Kuohai 	else
2475b2ad54e1SXu Kuohai 		branch_type = AARCH64_INSN_BRANCH_NOLINK;
2476b2ad54e1SXu Kuohai 
2477b2ad54e1SXu Kuohai 	if (gen_branch_or_nop(branch_type, ip, old_addr, plt, &old_insn) < 0)
2478b2ad54e1SXu Kuohai 		return -EFAULT;
2479b2ad54e1SXu Kuohai 
2480b2ad54e1SXu Kuohai 	if (gen_branch_or_nop(branch_type, ip, new_addr, plt, &new_insn) < 0)
2481b2ad54e1SXu Kuohai 		return -EFAULT;
2482b2ad54e1SXu Kuohai 
2483b2ad54e1SXu Kuohai 	if (is_long_jump(ip, new_addr))
2484b2ad54e1SXu Kuohai 		plt_target = (u64)new_addr;
2485b2ad54e1SXu Kuohai 	else if (is_long_jump(ip, old_addr))
2486b2ad54e1SXu Kuohai 		/* if the old target is a long jump and the new target is not,
2487b2ad54e1SXu Kuohai 		 * restore the plt target to dummy_tramp, so there is always a
2488b2ad54e1SXu Kuohai 		 * legal and harmless address stored in plt target, and we'll
2489b2ad54e1SXu Kuohai 		 * never jump from plt to an unknown place.
2490b2ad54e1SXu Kuohai 		 */
2491b2ad54e1SXu Kuohai 		plt_target = (u64)&dummy_tramp;
2492b2ad54e1SXu Kuohai 
2493b2ad54e1SXu Kuohai 	if (plt_target) {
2494b2ad54e1SXu Kuohai 		/* non-zero plt_target indicates we're patching a bpf prog,
2495b2ad54e1SXu Kuohai 		 * which is read only.
2496b2ad54e1SXu Kuohai 		 */
2497b2ad54e1SXu Kuohai 		if (set_memory_rw(PAGE_MASK & ((uintptr_t)&plt->target), 1))
2498b2ad54e1SXu Kuohai 			return -EFAULT;
2499b2ad54e1SXu Kuohai 		WRITE_ONCE(plt->target, plt_target);
2500b2ad54e1SXu Kuohai 		set_memory_ro(PAGE_MASK & ((uintptr_t)&plt->target), 1);
2501b2ad54e1SXu Kuohai 		/* since plt target points to either the new trampoline
2502b2ad54e1SXu Kuohai 		 * or dummy_tramp, even if another CPU reads the old plt
2503b2ad54e1SXu Kuohai 		 * target value before fetching the bl instruction to plt,
2504b2ad54e1SXu Kuohai 		 * it will be brought back by dummy_tramp, so no barrier is
2505b2ad54e1SXu Kuohai 		 * required here.
2506b2ad54e1SXu Kuohai 		 */
2507b2ad54e1SXu Kuohai 	}
2508b2ad54e1SXu Kuohai 
2509b2ad54e1SXu Kuohai 	/* if the old target and the new target are both long jumps, no
2510b2ad54e1SXu Kuohai 	 * patching is required
2511b2ad54e1SXu Kuohai 	 */
2512b2ad54e1SXu Kuohai 	if (old_insn == new_insn)
2513b2ad54e1SXu Kuohai 		return 0;
2514b2ad54e1SXu Kuohai 
2515b2ad54e1SXu Kuohai 	mutex_lock(&text_mutex);
2516b2ad54e1SXu Kuohai 	if (aarch64_insn_read(ip, &replaced)) {
2517b2ad54e1SXu Kuohai 		ret = -EFAULT;
2518b2ad54e1SXu Kuohai 		goto out;
2519b2ad54e1SXu Kuohai 	}
2520b2ad54e1SXu Kuohai 
2521b2ad54e1SXu Kuohai 	if (replaced != old_insn) {
2522b2ad54e1SXu Kuohai 		ret = -EFAULT;
2523b2ad54e1SXu Kuohai 		goto out;
2524b2ad54e1SXu Kuohai 	}
2525b2ad54e1SXu Kuohai 
2526b2ad54e1SXu Kuohai 	/* We call aarch64_insn_patch_text_nosync() to replace instruction
2527b2ad54e1SXu Kuohai 	 * atomically, so no other CPUs will fetch a half-new and half-old
2528b2ad54e1SXu Kuohai 	 * instruction. But there is chance that another CPU executes the
2529b2ad54e1SXu Kuohai 	 * old instruction after the patching operation finishes (e.g.,
2530b2ad54e1SXu Kuohai 	 * pipeline not flushed, or icache not synchronized yet).
2531b2ad54e1SXu Kuohai 	 *
2532b2ad54e1SXu Kuohai 	 * 1. when a new trampoline is attached, it is not a problem for
2533b2ad54e1SXu Kuohai 	 *    different CPUs to jump to different trampolines temporarily.
2534b2ad54e1SXu Kuohai 	 *
2535b2ad54e1SXu Kuohai 	 * 2. when an old trampoline is freed, we should wait for all other
2536b2ad54e1SXu Kuohai 	 *    CPUs to exit the trampoline and make sure the trampoline is no
2537b2ad54e1SXu Kuohai 	 *    longer reachable, since bpf_tramp_image_put() function already
2538b2ad54e1SXu Kuohai 	 *    uses percpu_ref and task-based rcu to do the sync, no need to call
2539b2ad54e1SXu Kuohai 	 *    the sync version here, see bpf_tramp_image_put() for details.
2540b2ad54e1SXu Kuohai 	 */
2541b2ad54e1SXu Kuohai 	ret = aarch64_insn_patch_text_nosync(ip, new_insn);
2542b2ad54e1SXu Kuohai out:
2543b2ad54e1SXu Kuohai 	mutex_unlock(&text_mutex);
2544b2ad54e1SXu Kuohai 
2545b2ad54e1SXu Kuohai 	return ret;
2546b2ad54e1SXu Kuohai }
254718a45f12SHou Tao 
254818a45f12SHou Tao bool bpf_jit_supports_ptr_xchg(void)
254918a45f12SHou Tao {
255018a45f12SHou Tao 	return true;
255118a45f12SHou Tao }
255222fc0e80SPuranjay Mohan 
255322fc0e80SPuranjay Mohan bool bpf_jit_supports_exceptions(void)
255422fc0e80SPuranjay Mohan {
255522fc0e80SPuranjay Mohan 	/* We unwind through both kernel frames starting from within bpf_throw
255622fc0e80SPuranjay Mohan 	 * call and BPF frames. Therefore we require FP unwinder to be enabled
255722fc0e80SPuranjay Mohan 	 * to walk kernel frames and reach BPF frames in the stack trace.
255822fc0e80SPuranjay Mohan 	 * ARM64 kernel is aways compiled with CONFIG_FRAME_POINTER=y
255922fc0e80SPuranjay Mohan 	 */
256022fc0e80SPuranjay Mohan 	return true;
256122fc0e80SPuranjay Mohan }
25621dad391dSPuranjay Mohan 
25634dd31243SPuranjay Mohan bool bpf_jit_supports_arena(void)
25644dd31243SPuranjay Mohan {
25654dd31243SPuranjay Mohan 	return true;
25664dd31243SPuranjay Mohan }
25674dd31243SPuranjay Mohan 
2568e612b5c1SPuranjay Mohan bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
2569e612b5c1SPuranjay Mohan {
2570e612b5c1SPuranjay Mohan 	if (!in_arena)
2571e612b5c1SPuranjay Mohan 		return true;
2572e612b5c1SPuranjay Mohan 	switch (insn->code) {
2573e612b5c1SPuranjay Mohan 	case BPF_STX | BPF_ATOMIC | BPF_W:
2574e612b5c1SPuranjay Mohan 	case BPF_STX | BPF_ATOMIC | BPF_DW:
2575e612b5c1SPuranjay Mohan 		if (!cpus_have_cap(ARM64_HAS_LSE_ATOMICS))
2576e612b5c1SPuranjay Mohan 			return false;
2577e612b5c1SPuranjay Mohan 	}
2578e612b5c1SPuranjay Mohan 	return true;
2579e612b5c1SPuranjay Mohan }
2580e612b5c1SPuranjay Mohan 
25817a4c3222SPuranjay Mohan bool bpf_jit_supports_percpu_insn(void)
25827a4c3222SPuranjay Mohan {
25837a4c3222SPuranjay Mohan 	return true;
25847a4c3222SPuranjay Mohan }
25857a4c3222SPuranjay Mohan 
258675fe4c0bSPuranjay Mohan bool bpf_jit_inlines_helper_call(s32 imm)
258775fe4c0bSPuranjay Mohan {
258875fe4c0bSPuranjay Mohan 	switch (imm) {
258975fe4c0bSPuranjay Mohan 	case BPF_FUNC_get_smp_processor_id:
25902bb138cbSPuranjay Mohan 	case BPF_FUNC_get_current_task:
25912bb138cbSPuranjay Mohan 	case BPF_FUNC_get_current_task_btf:
259275fe4c0bSPuranjay Mohan 		return true;
259375fe4c0bSPuranjay Mohan 	default:
259475fe4c0bSPuranjay Mohan 		return false;
259575fe4c0bSPuranjay Mohan 	}
259675fe4c0bSPuranjay Mohan }
259775fe4c0bSPuranjay Mohan 
25981dad391dSPuranjay Mohan void bpf_jit_free(struct bpf_prog *prog)
25991dad391dSPuranjay Mohan {
26001dad391dSPuranjay Mohan 	if (prog->jited) {
26011dad391dSPuranjay Mohan 		struct arm64_jit_data *jit_data = prog->aux->jit_data;
26021dad391dSPuranjay Mohan 		struct bpf_binary_header *hdr;
26031dad391dSPuranjay Mohan 
26041dad391dSPuranjay Mohan 		/*
26051dad391dSPuranjay Mohan 		 * If we fail the final pass of JIT (from jit_subprogs),
26061dad391dSPuranjay Mohan 		 * the program may not be finalized yet. Call finalize here
26071dad391dSPuranjay Mohan 		 * before freeing it.
26081dad391dSPuranjay Mohan 		 */
26091dad391dSPuranjay Mohan 		if (jit_data) {
26101dad391dSPuranjay Mohan 			bpf_arch_text_copy(&jit_data->ro_header->size, &jit_data->header->size,
26111dad391dSPuranjay Mohan 					   sizeof(jit_data->header->size));
26121dad391dSPuranjay Mohan 			kfree(jit_data);
26131dad391dSPuranjay Mohan 		}
26141dad391dSPuranjay Mohan 		hdr = bpf_jit_binary_pack_hdr(prog);
26151dad391dSPuranjay Mohan 		bpf_jit_binary_pack_free(hdr, NULL);
26161dad391dSPuranjay Mohan 		WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog));
26171dad391dSPuranjay Mohan 	}
26181dad391dSPuranjay Mohan 
26191dad391dSPuranjay Mohan 	bpf_prog_unlock_free(prog);
26201dad391dSPuranjay Mohan }
2621