xref: /linux/arch/riscv/net/bpf_jit_comp64.c (revision c0316386fecd0633b08ed87fb707eb42dc900dfe)
1 // SPDX-License-Identifier: GPL-2.0
2 /* BPF JIT compiler for RV64G
3  *
4  * Copyright(c) 2019 Björn Töpel <bjorn.topel@gmail.com>
5  *
6  */
7 
8 #include <linux/bitfield.h>
9 #include <linux/bpf.h>
10 #include <linux/filter.h>
11 #include <linux/memory.h>
12 #include <linux/stop_machine.h>
13 #include <asm/text-patching.h>
14 #include <asm/cfi.h>
15 #include <asm/percpu.h>
16 #include "bpf_jit.h"
17 
18 #define RV_FENTRY_NINSNS 2
19 #define RV_FENTRY_NBYTES (RV_FENTRY_NINSNS * 4)
20 /* imm that allows emit_imm to emit max count insns */
21 #define RV_MAX_COUNT_IMM 0x7FFF7FF7FF7FF7FF
22 /* fentry and TCC init insns will be skipped on tailcall */
23 #define RV_TAILCALL_OFFSET ((RV_FENTRY_NINSNS + 1) * 4)
24 
25 #define RV_REG_TCC RV_REG_T5
26 #define RV_REG_ARENA RV_REG_S7 /* For storing arena_vm_start */
27 
28 static const int regmap[] = {
29 	[BPF_REG_0] =	RV_REG_T6,
30 	[BPF_REG_1] =	RV_REG_A0,
31 	[BPF_REG_2] =	RV_REG_A1,
32 	[BPF_REG_3] =	RV_REG_A2,
33 	[BPF_REG_4] =	RV_REG_A3,
34 	[BPF_REG_5] =	RV_REG_A4,
35 	[BPF_REG_6] =	RV_REG_S1,
36 	[BPF_REG_7] =	RV_REG_S2,
37 	[BPF_REG_8] =	RV_REG_S3,
38 	[BPF_REG_9] =	RV_REG_S4,
39 	[BPF_REG_FP] =	RV_REG_S5,
40 	[BPF_REG_AX] =	RV_REG_T0,
41 };
42 
43 static const int pt_regmap[] = {
44 	[RV_REG_A0] = offsetof(struct pt_regs, a0),
45 	[RV_REG_A1] = offsetof(struct pt_regs, a1),
46 	[RV_REG_A2] = offsetof(struct pt_regs, a2),
47 	[RV_REG_A3] = offsetof(struct pt_regs, a3),
48 	[RV_REG_A4] = offsetof(struct pt_regs, a4),
49 	[RV_REG_S1] = offsetof(struct pt_regs, s1),
50 	[RV_REG_S2] = offsetof(struct pt_regs, s2),
51 	[RV_REG_S3] = offsetof(struct pt_regs, s3),
52 	[RV_REG_S4] = offsetof(struct pt_regs, s4),
53 	[RV_REG_S5] = offsetof(struct pt_regs, s5),
54 	[RV_REG_T0] = offsetof(struct pt_regs, t0),
55 	[RV_REG_T6] = offsetof(struct pt_regs, t6),
56 };
57 
58 enum {
59 	RV_CTX_F_SEEN_CALL =		RV_REG_RA,
60 	RV_CTX_F_SEEN_S1 =		RV_REG_S1,
61 	RV_CTX_F_SEEN_S2 =		RV_REG_S2,
62 	RV_CTX_F_SEEN_S3 =		RV_REG_S3,
63 	RV_CTX_F_SEEN_S4 =		RV_REG_S4,
64 	RV_CTX_F_SEEN_S5 =		RV_REG_S5,
65 };
66 
67 static u8 bpf_to_rv_reg(int bpf_reg, struct rv_jit_context *ctx)
68 {
69 	u8 reg = regmap[bpf_reg];
70 
71 	switch (reg) {
72 	case RV_CTX_F_SEEN_S1:
73 	case RV_CTX_F_SEEN_S2:
74 	case RV_CTX_F_SEEN_S3:
75 	case RV_CTX_F_SEEN_S4:
76 	case RV_CTX_F_SEEN_S5:
77 		__set_bit(reg, &ctx->flags);
78 	}
79 	return reg;
80 };
81 
82 static bool seen_reg(int reg, struct rv_jit_context *ctx)
83 {
84 	switch (reg) {
85 	case RV_CTX_F_SEEN_CALL:
86 	case RV_CTX_F_SEEN_S1:
87 	case RV_CTX_F_SEEN_S2:
88 	case RV_CTX_F_SEEN_S3:
89 	case RV_CTX_F_SEEN_S4:
90 	case RV_CTX_F_SEEN_S5:
91 		return test_bit(reg, &ctx->flags);
92 	}
93 	return false;
94 }
95 
96 static void mark_fp(struct rv_jit_context *ctx)
97 {
98 	__set_bit(RV_CTX_F_SEEN_S5, &ctx->flags);
99 }
100 
101 static void mark_call(struct rv_jit_context *ctx)
102 {
103 	__set_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
104 }
105 
106 static bool is_32b_int(s64 val)
107 {
108 	return -(1L << 31) <= val && val < (1L << 31);
109 }
110 
111 static bool in_auipc_jalr_range(s64 val)
112 {
113 	/*
114 	 * auipc+jalr can reach any signed PC-relative offset in the range
115 	 * [-2^31 - 2^11, 2^31 - 2^11).
116 	 */
117 	return (-(1L << 31) - (1L << 11)) <= val &&
118 		val < ((1L << 31) - (1L << 11));
119 }
120 
121 /* Modify rd pointer to alternate reg to avoid corrupting original reg */
122 static void emit_sextw_alt(u8 *rd, u8 ra, struct rv_jit_context *ctx)
123 {
124 	emit_sextw(ra, *rd, ctx);
125 	*rd = ra;
126 }
127 
128 static void emit_zextw_alt(u8 *rd, u8 ra, struct rv_jit_context *ctx)
129 {
130 	emit_zextw(ra, *rd, ctx);
131 	*rd = ra;
132 }
133 
134 /* Emit fixed-length instructions for address */
135 static int emit_addr(u8 rd, u64 addr, bool extra_pass, struct rv_jit_context *ctx)
136 {
137 	/*
138 	 * Use the ro_insns(RX) to calculate the offset as the BPF program will
139 	 * finally run from this memory region.
140 	 */
141 	u64 ip = (u64)(ctx->ro_insns + ctx->ninsns);
142 	s64 off = addr - ip;
143 	s64 upper = (off + (1 << 11)) >> 12;
144 	s64 lower = off & 0xfff;
145 
146 	if (extra_pass && !in_auipc_jalr_range(off)) {
147 		pr_err("bpf-jit: target offset 0x%llx is out of range\n", off);
148 		return -ERANGE;
149 	}
150 
151 	emit(rv_auipc(rd, upper), ctx);
152 	emit(rv_addi(rd, rd, lower), ctx);
153 	return 0;
154 }
155 
156 /* Emit variable-length instructions for 32-bit and 64-bit imm */
157 static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)
158 {
159 	/* Note that the immediate from the add is sign-extended,
160 	 * which means that we need to compensate this by adding 2^12,
161 	 * when the 12th bit is set. A simpler way of doing this, and
162 	 * getting rid of the check, is to just add 2**11 before the
163 	 * shift. The "Loading a 32-Bit constant" example from the
164 	 * "Computer Organization and Design, RISC-V edition" book by
165 	 * Patterson/Hennessy highlights this fact.
166 	 *
167 	 * This also means that we need to process LSB to MSB.
168 	 */
169 	s64 upper = (val + (1 << 11)) >> 12;
170 	/* Sign-extend lower 12 bits to 64 bits since immediates for li, addiw,
171 	 * and addi are signed and RVC checks will perform signed comparisons.
172 	 */
173 	s64 lower = ((val & 0xfff) << 52) >> 52;
174 	int shift;
175 
176 	if (is_32b_int(val)) {
177 		if (upper)
178 			emit_lui(rd, upper, ctx);
179 
180 		if (!upper) {
181 			emit_li(rd, lower, ctx);
182 			return;
183 		}
184 
185 		emit_addiw(rd, rd, lower, ctx);
186 		return;
187 	}
188 
189 	shift = __ffs(upper);
190 	upper >>= shift;
191 	shift += 12;
192 
193 	emit_imm(rd, upper, ctx);
194 
195 	emit_slli(rd, rd, shift, ctx);
196 	if (lower)
197 		emit_addi(rd, rd, lower, ctx);
198 }
199 
200 static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
201 {
202 	int stack_adjust = ctx->stack_size, store_offset = stack_adjust - 8;
203 
204 	if (seen_reg(RV_REG_RA, ctx)) {
205 		emit_ld(RV_REG_RA, store_offset, RV_REG_SP, ctx);
206 		store_offset -= 8;
207 	}
208 	emit_ld(RV_REG_FP, store_offset, RV_REG_SP, ctx);
209 	store_offset -= 8;
210 	if (seen_reg(RV_REG_S1, ctx)) {
211 		emit_ld(RV_REG_S1, store_offset, RV_REG_SP, ctx);
212 		store_offset -= 8;
213 	}
214 	if (seen_reg(RV_REG_S2, ctx)) {
215 		emit_ld(RV_REG_S2, store_offset, RV_REG_SP, ctx);
216 		store_offset -= 8;
217 	}
218 	if (seen_reg(RV_REG_S3, ctx)) {
219 		emit_ld(RV_REG_S3, store_offset, RV_REG_SP, ctx);
220 		store_offset -= 8;
221 	}
222 	if (seen_reg(RV_REG_S4, ctx)) {
223 		emit_ld(RV_REG_S4, store_offset, RV_REG_SP, ctx);
224 		store_offset -= 8;
225 	}
226 	if (seen_reg(RV_REG_S5, ctx)) {
227 		emit_ld(RV_REG_S5, store_offset, RV_REG_SP, ctx);
228 		store_offset -= 8;
229 	}
230 	if (ctx->arena_vm_start) {
231 		emit_ld(RV_REG_ARENA, store_offset, RV_REG_SP, ctx);
232 		store_offset -= 8;
233 	}
234 
235 	/* restore TCC from stack to RV_REG_TCC */
236 	emit_ld(RV_REG_TCC, ctx->tcc_offset, RV_REG_SP, ctx);
237 
238 	emit_addi(RV_REG_SP, RV_REG_SP, stack_adjust, ctx);
239 	/* Set return value. */
240 	if (!is_tail_call)
241 		emit_addiw(RV_REG_A0, regmap[BPF_REG_0], 0, ctx);
242 	emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
243 		  is_tail_call ? RV_TAILCALL_OFFSET : 0, ctx);
244 }
245 
246 static void emit_bcc(u8 cond, u8 rd, u8 rs, int rvoff,
247 		     struct rv_jit_context *ctx)
248 {
249 	switch (cond) {
250 	case BPF_JEQ:
251 		emit(rv_beq(rd, rs, rvoff >> 1), ctx);
252 		return;
253 	case BPF_JGT:
254 		emit(rv_bltu(rs, rd, rvoff >> 1), ctx);
255 		return;
256 	case BPF_JLT:
257 		emit(rv_bltu(rd, rs, rvoff >> 1), ctx);
258 		return;
259 	case BPF_JGE:
260 		emit(rv_bgeu(rd, rs, rvoff >> 1), ctx);
261 		return;
262 	case BPF_JLE:
263 		emit(rv_bgeu(rs, rd, rvoff >> 1), ctx);
264 		return;
265 	case BPF_JNE:
266 		emit(rv_bne(rd, rs, rvoff >> 1), ctx);
267 		return;
268 	case BPF_JSGT:
269 		emit(rv_blt(rs, rd, rvoff >> 1), ctx);
270 		return;
271 	case BPF_JSLT:
272 		emit(rv_blt(rd, rs, rvoff >> 1), ctx);
273 		return;
274 	case BPF_JSGE:
275 		emit(rv_bge(rd, rs, rvoff >> 1), ctx);
276 		return;
277 	case BPF_JSLE:
278 		emit(rv_bge(rs, rd, rvoff >> 1), ctx);
279 	}
280 }
281 
282 static void emit_branch(u8 cond, u8 rd, u8 rs, int rvoff,
283 			struct rv_jit_context *ctx)
284 {
285 	s64 upper, lower;
286 
287 	if (is_13b_int(rvoff)) {
288 		emit_bcc(cond, rd, rs, rvoff, ctx);
289 		return;
290 	}
291 
292 	/* Adjust for jal */
293 	rvoff -= 4;
294 
295 	/* Transform, e.g.:
296 	 *   bne rd,rs,foo
297 	 * to
298 	 *   beq rd,rs,<.L1>
299 	 *   (auipc foo)
300 	 *   jal(r) foo
301 	 * .L1
302 	 */
303 	cond = invert_bpf_cond(cond);
304 	if (is_21b_int(rvoff)) {
305 		emit_bcc(cond, rd, rs, 8, ctx);
306 		emit(rv_jal(RV_REG_ZERO, rvoff >> 1), ctx);
307 		return;
308 	}
309 
310 	/* 32b No need for an additional rvoff adjustment, since we
311 	 * get that from the auipc at PC', where PC = PC' + 4.
312 	 */
313 	upper = (rvoff + (1 << 11)) >> 12;
314 	lower = rvoff & 0xfff;
315 
316 	emit_bcc(cond, rd, rs, 12, ctx);
317 	emit(rv_auipc(RV_REG_T1, upper), ctx);
318 	emit(rv_jalr(RV_REG_ZERO, RV_REG_T1, lower), ctx);
319 }
320 
321 static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
322 {
323 	int tc_ninsn, off, start_insn = ctx->ninsns;
324 
325 	/* a0: &ctx
326 	 * a1: &array
327 	 * a2: index
328 	 *
329 	 * if (index >= array->map.max_entries)
330 	 *	goto out;
331 	 */
332 	tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
333 		   ctx->offset[0];
334 	emit_zextw(RV_REG_A2, RV_REG_A2, ctx);
335 
336 	off = offsetof(struct bpf_array, map.max_entries);
337 	if (is_12b_check(off, insn))
338 		return -1;
339 	emit(rv_lwu(RV_REG_T1, off, RV_REG_A1), ctx);
340 	off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
341 	emit_branch(BPF_JGE, RV_REG_A2, RV_REG_T1, off, ctx);
342 
343 	/* if (--TCC < 0)
344 	 *     goto out;
345 	 */
346 	emit_ld(RV_REG_TCC, ctx->tcc_offset, RV_REG_SP, ctx);
347 	emit_addi(RV_REG_TCC, RV_REG_TCC, -1, ctx);
348 	off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
349 	emit_branch(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);
350 
351 	/* prog = array->ptrs[index];
352 	 * if (!prog)
353 	 *     goto out;
354 	 */
355 	emit_sh3add(RV_REG_T2, RV_REG_A2, RV_REG_A1, ctx);
356 	off = offsetof(struct bpf_array, ptrs);
357 	if (is_12b_check(off, insn))
358 		return -1;
359 	emit_ld(RV_REG_T2, off, RV_REG_T2, ctx);
360 	off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
361 	emit_branch(BPF_JEQ, RV_REG_T2, RV_REG_ZERO, off, ctx);
362 
363 	/* store updated TCC back to stack */
364 	emit_sd(RV_REG_SP, ctx->tcc_offset, RV_REG_TCC, ctx);
365 
366 	/* goto *(prog->bpf_func + RV_TAILCALL_OFFSET); */
367 	off = offsetof(struct bpf_prog, bpf_func);
368 	if (is_12b_check(off, insn))
369 		return -1;
370 	emit_ld(RV_REG_T3, off, RV_REG_T2, ctx);
371 	__build_epilogue(true, ctx);
372 	return 0;
373 }
374 
375 static void init_regs(u8 *rd, u8 *rs, const struct bpf_insn *insn,
376 		      struct rv_jit_context *ctx)
377 {
378 	u8 code = insn->code;
379 
380 	switch (code) {
381 	case BPF_JMP | BPF_JA:
382 	case BPF_JMP | BPF_CALL:
383 	case BPF_JMP | BPF_EXIT:
384 	case BPF_JMP | BPF_TAIL_CALL:
385 		break;
386 	default:
387 		*rd = bpf_to_rv_reg(insn->dst_reg, ctx);
388 	}
389 
390 	if (code & (BPF_ALU | BPF_X) || code & (BPF_ALU64 | BPF_X) ||
391 	    code & (BPF_JMP | BPF_X) || code & (BPF_JMP32 | BPF_X) ||
392 	    code & BPF_LDX || code & BPF_STX)
393 		*rs = bpf_to_rv_reg(insn->src_reg, ctx);
394 }
395 
396 static int emit_jump_and_link(u8 rd, s64 rvoff, bool fixed_addr,
397 			      struct rv_jit_context *ctx)
398 {
399 	s64 upper, lower;
400 
401 	if (rvoff && fixed_addr && is_21b_int(rvoff)) {
402 		emit(rv_jal(rd, rvoff >> 1), ctx);
403 		return 0;
404 	} else if (in_auipc_jalr_range(rvoff)) {
405 		upper = (rvoff + (1 << 11)) >> 12;
406 		lower = rvoff & 0xfff;
407 		emit(rv_auipc(RV_REG_T1, upper), ctx);
408 		emit(rv_jalr(rd, RV_REG_T1, lower), ctx);
409 		return 0;
410 	}
411 
412 	pr_err("bpf-jit: target offset 0x%llx is out of range\n", rvoff);
413 	return -ERANGE;
414 }
415 
416 static bool is_signed_bpf_cond(u8 cond)
417 {
418 	return cond == BPF_JSGT || cond == BPF_JSLT ||
419 		cond == BPF_JSGE || cond == BPF_JSLE;
420 }
421 
422 static int emit_call(u64 addr, bool fixed_addr, struct rv_jit_context *ctx)
423 {
424 	s64 off = 0;
425 	u64 ip;
426 
427 	if (addr && ctx->insns && ctx->ro_insns) {
428 		/*
429 		 * Use the ro_insns(RX) to calculate the offset as the BPF
430 		 * program will finally run from this memory region.
431 		 */
432 		ip = (u64)(long)(ctx->ro_insns + ctx->ninsns);
433 		off = addr - ip;
434 	}
435 
436 	return emit_jump_and_link(RV_REG_RA, off, fixed_addr, ctx);
437 }
438 
439 static inline void emit_kcfi(u32 hash, struct rv_jit_context *ctx)
440 {
441 	if (IS_ENABLED(CONFIG_CFI))
442 		emit(hash, ctx);
443 }
444 
445 static void emit_ldx_insn(u8 rd, s16 off, u8 rs, u8 size, bool sign_ext,
446 			  struct rv_jit_context *ctx)
447 {
448 	switch (size) {
449 	case BPF_B:
450 		emit(sign_ext ? rv_lb(rd, off, rs) : rv_lbu(rd, off, rs), ctx);
451 		break;
452 	case BPF_H:
453 		emit(sign_ext ? rv_lh(rd, off, rs) : rv_lhu(rd, off, rs), ctx);
454 		break;
455 	case BPF_W:
456 		emit(sign_ext ? rv_lw(rd, off, rs) : rv_lwu(rd, off, rs), ctx);
457 		break;
458 	case BPF_DW:
459 		emit_ld(rd, off, rs, ctx);
460 		break;
461 	}
462 
463 }
464 
465 static void emit_stx_insn(u8 rd, s16 off, u8 rs, u8 size, struct rv_jit_context *ctx)
466 {
467 	switch (size) {
468 	case BPF_B:
469 		emit(rv_sb(rd, off, rs), ctx);
470 		break;
471 	case BPF_H:
472 		emit(rv_sh(rd, off, rs), ctx);
473 		break;
474 	case BPF_W:
475 		emit_sw(rd, off, rs, ctx);
476 		break;
477 	case BPF_DW:
478 		emit_sd(rd, off, rs, ctx);
479 		break;
480 	}
481 }
482 
483 static void emit_ldx(u8 rd, s16 off, u8 rs, u8 size, bool sign_ext,
484 		    struct rv_jit_context *ctx)
485 {
486 	if (is_12b_int(off)) {
487 		ctx->ex_insn_off = ctx->ninsns;
488 		emit_ldx_insn(rd, off, rs, size, sign_ext, ctx);
489 		ctx->ex_jmp_off = ctx->ninsns;
490 		return;
491 	}
492 
493 	emit_imm(RV_REG_T1, off, ctx);
494 	emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
495 	ctx->ex_insn_off = ctx->ninsns;
496 	emit_ldx_insn(rd, 0, RV_REG_T1, size, sign_ext, ctx);
497 	ctx->ex_jmp_off = ctx->ninsns;
498 }
499 
500 static void emit_stack_arg_ldx(u8 rd, s16 off, struct rv_jit_context *ctx)
501 {
502 	int idx = off / 8 - 1;
503 
504 	if (idx < RV_EXTRA_STK_ARGS) {
505 		emit_mv(rd, RV_REG_A5 + idx, ctx);
506 		return;
507 	}
508 
509 	emit_ldx_insn(rd, (idx - RV_EXTRA_STK_ARGS) * 8, RV_REG_FP, BPF_DW, false, ctx);
510 }
511 
512 static void emit_st(u8 rd, s16 off, s32 imm, u8 size, struct rv_jit_context *ctx)
513 {
514 	emit_imm(RV_REG_T1, imm, ctx);
515 	if (is_12b_int(off)) {
516 		ctx->ex_insn_off = ctx->ninsns;
517 		emit_stx_insn(rd, off, RV_REG_T1, size, ctx);
518 		ctx->ex_jmp_off = ctx->ninsns;
519 		return;
520 	}
521 
522 	emit_imm(RV_REG_T2, off, ctx);
523 	emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
524 	ctx->ex_insn_off = ctx->ninsns;
525 	emit_stx_insn(RV_REG_T2, 0, RV_REG_T1, size, ctx);
526 	ctx->ex_jmp_off = ctx->ninsns;
527 }
528 
529 static void emit_stack_arg_st(s16 off, s32 imm, struct rv_jit_context *ctx)
530 {
531 	int idx = -off / 8 - 1;
532 
533 	if (idx < RV_EXTRA_STK_ARGS) {
534 		emit_imm(RV_REG_A5 + idx, imm, ctx);
535 		return;
536 	}
537 
538 	emit_imm(RV_REG_T1, imm, ctx);
539 	emit_stx_insn(RV_REG_SP, (idx - RV_EXTRA_STK_ARGS) * 8, RV_REG_T1, BPF_DW, ctx);
540 }
541 
542 static void emit_stx(u8 rd, s16 off, u8 rs, u8 size, struct rv_jit_context *ctx)
543 {
544 	if (is_12b_int(off)) {
545 		ctx->ex_insn_off = ctx->ninsns;
546 		emit_stx_insn(rd, off, rs, size, ctx);
547 		ctx->ex_jmp_off = ctx->ninsns;
548 		return;
549 	}
550 
551 	emit_imm(RV_REG_T1, off, ctx);
552 	emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
553 	ctx->ex_insn_off = ctx->ninsns;
554 	emit_stx_insn(RV_REG_T1, 0, rs, size, ctx);
555 	ctx->ex_jmp_off = ctx->ninsns;
556 }
557 
558 static void emit_stack_arg_stx(s16 off, u8 rs, struct rv_jit_context *ctx)
559 {
560 	int idx = -off / 8 - 1;
561 
562 	if (idx < RV_EXTRA_STK_ARGS) {
563 		emit_mv(RV_REG_A5 + idx, rs, ctx);
564 		return;
565 	}
566 
567 	emit_stx_insn(RV_REG_SP, (idx - RV_EXTRA_STK_ARGS) * 8, rs, BPF_DW, ctx);
568 }
569 
570 static int emit_atomic_ld_st(u8 rd, u8 rs, const struct bpf_insn *insn,
571 			     struct rv_jit_context *ctx)
572 {
573 	u8 code = insn->code;
574 	s32 imm = insn->imm;
575 	s16 off = insn->off;
576 
577 	switch (imm) {
578 	/* dst_reg = load_acquire(src_reg + off16) */
579 	case BPF_LOAD_ACQ:
580 		if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
581 			emit_add(RV_REG_T2, rs, RV_REG_ARENA, ctx);
582 			rs = RV_REG_T2;
583 		}
584 
585 		emit_ldx(rd, off, rs, BPF_SIZE(code), false, ctx);
586 		emit_fence_r_rw(ctx);
587 
588 		/* If our next insn is a redundant zext, return 1 to tell
589 		 * build_body() to skip it.
590 		 */
591 		if (BPF_SIZE(code) != BPF_DW && insn_is_zext(&insn[1]))
592 			return 1;
593 		break;
594 	/* store_release(dst_reg + off16, src_reg) */
595 	case BPF_STORE_REL:
596 		if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
597 			emit_add(RV_REG_T2, rd, RV_REG_ARENA, ctx);
598 			rd = RV_REG_T2;
599 		}
600 
601 		emit_fence_rw_w(ctx);
602 		emit_stx(rd, off, rs, BPF_SIZE(code), ctx);
603 		break;
604 	default:
605 		pr_err_once("bpf-jit: invalid atomic load/store opcode %02x\n", imm);
606 		return -EINVAL;
607 	}
608 
609 	return 0;
610 }
611 
612 static int emit_atomic_rmw(u8 rd, u8 rs, const struct bpf_insn *insn,
613 			   struct rv_jit_context *ctx)
614 {
615 	u8 code = insn->code;
616 	s16 off = insn->off;
617 	s32 imm = insn->imm;
618 	bool is64 = BPF_SIZE(code) == BPF_DW;
619 
620 	if (BPF_SIZE(code) != BPF_W && BPF_SIZE(code) != BPF_DW) {
621 		pr_err_once("bpf-jit: 1- and 2-byte RMW atomics are not supported\n");
622 		return -EINVAL;
623 	}
624 
625 	if (off) {
626 		if (is_12b_int(off)) {
627 			emit_addi(RV_REG_T1, rd, off, ctx);
628 		} else {
629 			emit_imm(RV_REG_T1, off, ctx);
630 			emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
631 		}
632 		rd = RV_REG_T1;
633 	}
634 
635 	if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
636 		emit_add(RV_REG_T1, rd, RV_REG_ARENA, ctx);
637 		rd = RV_REG_T1;
638 	}
639 
640 	switch (imm) {
641 	/* lock *(u32/u64 *)(dst_reg + off16) <op>= src_reg */
642 	case BPF_ADD:
643 		ctx->ex_insn_off = ctx->ninsns;
644 		emit(is64 ? rv_amoadd_d(RV_REG_ZERO, rs, rd, 0, 0) :
645 		     rv_amoadd_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
646 		ctx->ex_jmp_off = ctx->ninsns;
647 		break;
648 	case BPF_AND:
649 		ctx->ex_insn_off = ctx->ninsns;
650 		emit(is64 ? rv_amoand_d(RV_REG_ZERO, rs, rd, 0, 0) :
651 		     rv_amoand_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
652 		ctx->ex_jmp_off = ctx->ninsns;
653 		break;
654 	case BPF_OR:
655 		ctx->ex_insn_off = ctx->ninsns;
656 		emit(is64 ? rv_amoor_d(RV_REG_ZERO, rs, rd, 0, 0) :
657 		     rv_amoor_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
658 		ctx->ex_jmp_off = ctx->ninsns;
659 		break;
660 	case BPF_XOR:
661 		ctx->ex_insn_off = ctx->ninsns;
662 		emit(is64 ? rv_amoxor_d(RV_REG_ZERO, rs, rd, 0, 0) :
663 		     rv_amoxor_w(RV_REG_ZERO, rs, rd, 0, 0), ctx);
664 		ctx->ex_jmp_off = ctx->ninsns;
665 		break;
666 	/* src_reg = atomic_fetch_<op>(dst_reg + off16, src_reg) */
667 	case BPF_ADD | BPF_FETCH:
668 		ctx->ex_insn_off = ctx->ninsns;
669 		emit(is64 ? rv_amoadd_d(rs, rs, rd, 1, 1) :
670 		     rv_amoadd_w(rs, rs, rd, 1, 1), ctx);
671 		ctx->ex_jmp_off = ctx->ninsns;
672 		if (!is64)
673 			emit_zextw(rs, rs, ctx);
674 		break;
675 	case BPF_AND | BPF_FETCH:
676 		ctx->ex_insn_off = ctx->ninsns;
677 		emit(is64 ? rv_amoand_d(rs, rs, rd, 1, 1) :
678 		     rv_amoand_w(rs, rs, rd, 1, 1), ctx);
679 		ctx->ex_jmp_off = ctx->ninsns;
680 		if (!is64)
681 			emit_zextw(rs, rs, ctx);
682 		break;
683 	case BPF_OR | BPF_FETCH:
684 		ctx->ex_insn_off = ctx->ninsns;
685 		emit(is64 ? rv_amoor_d(rs, rs, rd, 1, 1) :
686 		     rv_amoor_w(rs, rs, rd, 1, 1), ctx);
687 		ctx->ex_jmp_off = ctx->ninsns;
688 		if (!is64)
689 			emit_zextw(rs, rs, ctx);
690 		break;
691 	case BPF_XOR | BPF_FETCH:
692 		ctx->ex_insn_off = ctx->ninsns;
693 		emit(is64 ? rv_amoxor_d(rs, rs, rd, 1, 1) :
694 		     rv_amoxor_w(rs, rs, rd, 1, 1), ctx);
695 		ctx->ex_jmp_off = ctx->ninsns;
696 		if (!is64)
697 			emit_zextw(rs, rs, ctx);
698 		break;
699 	/* src_reg = atomic_xchg(dst_reg + off16, src_reg); */
700 	case BPF_XCHG:
701 		ctx->ex_insn_off = ctx->ninsns;
702 		emit(is64 ? rv_amoswap_d(rs, rs, rd, 1, 1) :
703 		     rv_amoswap_w(rs, rs, rd, 1, 1), ctx);
704 		ctx->ex_jmp_off = ctx->ninsns;
705 		if (!is64)
706 			emit_zextw(rs, rs, ctx);
707 		break;
708 	/* r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg); */
709 	case BPF_CMPXCHG:
710 		emit_cmpxchg(rd, rs, regmap[BPF_REG_0], is64, ctx);
711 		break;
712 	default:
713 		pr_err_once("bpf-jit: invalid atomic RMW opcode %02x\n", imm);
714 		return -EINVAL;
715 	}
716 
717 	return 0;
718 }
719 
720 /*
721  * Sign-extend the register if necessary
722  */
723 static int sign_extend(u8 rd, u8 rs, u8 sz, bool sign, struct rv_jit_context *ctx)
724 {
725 	if (!sign && (sz == 1 || sz == 2)) {
726 		if (rd != rs)
727 			emit_mv(rd, rs, ctx);
728 		return 0;
729 	}
730 
731 	switch (sz) {
732 	case 1:
733 		emit_sextb(rd, rs, ctx);
734 		break;
735 	case 2:
736 		emit_sexth(rd, rs, ctx);
737 		break;
738 	case 4:
739 		emit_sextw(rd, rs, ctx);
740 		break;
741 	case 8:
742 		if (rd != rs)
743 			emit_mv(rd, rs, ctx);
744 		break;
745 	default:
746 		pr_err("bpf-jit: invalid size %d for sign_extend\n", sz);
747 		return -EINVAL;
748 	}
749 
750 	return 0;
751 }
752 
753 #define BPF_FIXUP_OFFSET_MASK   GENMASK(26, 0)
754 #define BPF_FIXUP_REG_MASK      GENMASK(31, 27)
755 #define REG_DONT_CLEAR_MARKER	0	/* RV_REG_ZERO unused in pt_regmap */
756 
757 bool ex_handler_bpf(const struct exception_table_entry *ex,
758 		    struct pt_regs *regs)
759 {
760 	off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
761 	int regs_offset = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
762 
763 	if (regs_offset != REG_DONT_CLEAR_MARKER)
764 		*(unsigned long *)((void *)regs + pt_regmap[regs_offset]) = 0;
765 	regs->epc = (unsigned long)&ex->fixup - offset;
766 
767 	return true;
768 }
769 
770 /* For accesses to BTF pointers, add an entry to the exception table */
771 static int add_exception_handler(const struct bpf_insn *insn, int dst_reg,
772 				 struct rv_jit_context *ctx)
773 {
774 	struct exception_table_entry *ex;
775 	unsigned long pc;
776 	off_t ins_offset;
777 	off_t fixup_offset;
778 
779 	if (!ctx->insns || !ctx->ro_insns || !ctx->prog->aux->extable ||
780 	    ctx->ex_insn_off <= 0 || ctx->ex_jmp_off <= 0)
781 		return 0;
782 
783 	if (BPF_MODE(insn->code) != BPF_PROBE_MEM &&
784 	    BPF_MODE(insn->code) != BPF_PROBE_MEMSX &&
785 	    BPF_MODE(insn->code) != BPF_PROBE_MEM32 &&
786 	    !(BPF_MODE(insn->code) == BPF_PROBE_MEM32SX &&
787 	      BPF_CLASS(insn->code) == BPF_LDX) &&
788 	    BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
789 		return 0;
790 
791 	if (WARN_ON_ONCE(ctx->nexentries >= ctx->prog->aux->num_exentries))
792 		return -EINVAL;
793 
794 	if (WARN_ON_ONCE(ctx->ex_insn_off > ctx->ninsns || ctx->ex_jmp_off > ctx->ninsns))
795 		return -EINVAL;
796 
797 	ex = &ctx->prog->aux->extable[ctx->nexentries];
798 	pc = (unsigned long)&ctx->ro_insns[ctx->ex_insn_off];
799 
800 	/*
801 	 * This is the relative offset of the instruction that may fault from
802 	 * the exception table itself. This will be written to the exception
803 	 * table and if this instruction faults, the destination register will
804 	 * be set to '0' and the execution will jump to the next instruction.
805 	 */
806 	ins_offset = pc - (long)&ex->insn;
807 	if (WARN_ON_ONCE(ins_offset >= 0 || ins_offset < INT_MIN))
808 		return -ERANGE;
809 
810 	/*
811 	 * Since the extable follows the program, the fixup offset is always
812 	 * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
813 	 * to keep things simple, and put the destination register in the upper
814 	 * bits. We don't need to worry about buildtime or runtime sort
815 	 * modifying the upper bits because the table is already sorted, and
816 	 * isn't part of the main exception table.
817 	 *
818 	 * The fixup_offset is set to the next instruction from the instruction
819 	 * that may fault. The execution will jump to this after handling the
820 	 * fault.
821 	 */
822 	fixup_offset = (long)&ex->fixup - (long)&ctx->ro_insns[ctx->ex_jmp_off];
823 	if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, fixup_offset))
824 		return -ERANGE;
825 
826 	/*
827 	 * The offsets above have been calculated using the RO buffer but we
828 	 * need to use the R/W buffer for writes.
829 	 * switch ex to rw buffer for writing.
830 	 */
831 	ex = (void *)ctx->insns + ((void *)ex - (void *)ctx->ro_insns);
832 
833 	ex->insn = ins_offset;
834 
835 	ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, fixup_offset) |
836 		FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
837 	ex->type = EX_TYPE_BPF;
838 
839 	ctx->ex_insn_off = 0;
840 	ctx->ex_jmp_off = 0;
841 	ctx->nexentries++;
842 	return 0;
843 }
844 
845 static int gen_jump_or_nops(void *target, void *ip, u32 *insns, bool is_call)
846 {
847 	s64 rvoff;
848 	struct rv_jit_context ctx;
849 
850 	ctx.ninsns = 0;
851 	ctx.insns = (u16 *)insns;
852 
853 	if (!target) {
854 		emit(rv_nop(), &ctx);
855 		emit(rv_nop(), &ctx);
856 		return 0;
857 	}
858 
859 	rvoff = (s64)(target - ip);
860 	return emit_jump_and_link(is_call ? RV_REG_T0 : RV_REG_ZERO, rvoff, false, &ctx);
861 }
862 
863 int arch_bpf_trampoline_skip(void *nop, void *target)
864 {
865 	u32 old_insn = rv_nop();
866 	u32 new_insn = rv_jal(RV_REG_ZERO, (target - nop) >> 1);
867 	int ret;
868 
869 	if (memcmp(nop, &old_insn, sizeof(old_insn)))
870 		return -EFAULT;
871 
872 	cpus_read_lock();
873 	mutex_lock(&text_mutex);
874 	ret = patch_text(nop, &new_insn, sizeof(new_insn));
875 	mutex_unlock(&text_mutex);
876 	cpus_read_unlock();
877 
878 	return ret;
879 }
880 
881 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
882 		       enum bpf_text_poke_type new_t, void *old_addr,
883 		       void *new_addr)
884 {
885 	u32 old_insns[RV_FENTRY_NINSNS], new_insns[RV_FENTRY_NINSNS];
886 	bool is_call;
887 	int ret;
888 
889 	if (!is_kernel_text((unsigned long)ip) &&
890 	    !is_bpf_text_address((unsigned long)ip))
891 		return -ENOTSUPP;
892 
893 	is_call = old_t == BPF_MOD_CALL;
894 	ret = gen_jump_or_nops(old_addr, ip, old_insns, is_call);
895 	if (ret)
896 		return ret;
897 
898 	if (memcmp(ip, old_insns, RV_FENTRY_NBYTES))
899 		return -EFAULT;
900 
901 	is_call = new_t == BPF_MOD_CALL;
902 	ret = gen_jump_or_nops(new_addr, ip, new_insns, is_call);
903 	if (ret)
904 		return ret;
905 
906 	cpus_read_lock();
907 	mutex_lock(&text_mutex);
908 	if (memcmp(ip, new_insns, RV_FENTRY_NBYTES))
909 		ret = patch_text(ip, new_insns, RV_FENTRY_NBYTES);
910 	mutex_unlock(&text_mutex);
911 	cpus_read_unlock();
912 
913 	return ret;
914 }
915 
916 static void store_args(int nr_arg_slots, int args_off, int stack_args_off,
917 		       struct rv_jit_context *ctx)
918 {
919 	int i;
920 
921 	for (i = 0; i < nr_arg_slots; i++) {
922 		if (i < RV_MAX_REG_ARGS) {
923 			emit_sd(RV_REG_FP, -args_off, RV_REG_A0 + i, ctx);
924 		} else {
925 			emit_ld(RV_REG_T1, stack_args_off +
926 				(i - RV_MAX_REG_ARGS) * 8, RV_REG_FP, ctx);
927 			emit_sd(RV_REG_FP, -args_off, RV_REG_T1, ctx);
928 		}
929 		args_off -= 8;
930 	}
931 }
932 
933 static void restore_args(int nr_reg_args, int args_off, struct rv_jit_context *ctx)
934 {
935 	int i;
936 
937 	for (i = 0; i < nr_reg_args; i++) {
938 		emit_ld(RV_REG_A0 + i, -args_off, RV_REG_FP, ctx);
939 		args_off -= 8;
940 	}
941 }
942 
943 static void restore_stack_args(int nr_stack_args, int args_off, int stk_arg_off,
944 			       struct rv_jit_context *ctx)
945 {
946 	int i;
947 
948 	for (i = 0; i < nr_stack_args; i++) {
949 		emit_ld(RV_REG_T1, -(args_off - RV_MAX_REG_ARGS * 8), RV_REG_FP, ctx);
950 		emit_sd(RV_REG_FP, -stk_arg_off, RV_REG_T1, ctx);
951 		args_off -= 8;
952 		stk_arg_off -= 8;
953 	}
954 }
955 
956 static void emit_store_stack_imm64(u8 reg, int stack_off, u64 imm64,
957 				   struct rv_jit_context *ctx)
958 {
959 	/* Load imm64 into reg and store it at [FP + stack_off]. */
960 	emit_imm(reg, (s64)imm64, ctx);
961 	emit_sd(RV_REG_FP, stack_off, reg, ctx);
962 }
963 
964 static int invoke_bpf_prog(struct bpf_tramp_image *im, struct bpf_tramp_node *node,
965 			   int args_off, int retval_off, int run_ctx_off, bool save_ret,
966 			   struct rv_jit_context *ctx)
967 {
968 	int ret, branch_off;
969 	struct bpf_prog *p = node->link->prog;
970 	int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
971 	void *skip = ctx->ro_insns + ctx->ninsns;
972 
973 	/* nop, patched to a jal over this prog when it is detached */
974 	emit(rv_nop(), ctx);
975 
976 	if (node->cookie)
977 		emit_store_stack_imm64(RV_REG_T1, -run_ctx_off + cookie_off, node->cookie, ctx);
978 	else
979 		emit_sd(RV_REG_FP, -run_ctx_off + cookie_off, RV_REG_ZERO, ctx);
980 
981 	/* arg1: prog */
982 	emit_imm(RV_REG_A0, (const s64)p, ctx);
983 	/* arg2: &run_ctx */
984 	emit_addi(RV_REG_A1, RV_REG_FP, -run_ctx_off, ctx);
985 	ret = emit_call((const u64)bpf_trampoline_enter(p), true, ctx);
986 	if (ret)
987 		return ret;
988 
989 	/* store prog start time */
990 	emit_mv(RV_REG_S1, RV_REG_A0, ctx);
991 
992 	/* if (__bpf_prog_enter(prog) == 0)
993 	 *	goto skip_exec_of_prog;
994 	 */
995 	branch_off = ctx->ninsns;
996 	/* nop reserved for conditional jump */
997 	emit(rv_nop(), ctx);
998 
999 	/* arg1: &args_off */
1000 	emit_addi(RV_REG_A0, RV_REG_FP, -args_off, ctx);
1001 	if (!p->jited)
1002 		/* arg2: progs[i]->insnsi for interpreter */
1003 		emit_imm(RV_REG_A1, (const s64)p->insnsi, ctx);
1004 	ret = emit_call((const u64)p->bpf_func, true, ctx);
1005 	if (ret)
1006 		return ret;
1007 
1008 	if (save_ret) {
1009 		emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
1010 		emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
1011 	}
1012 
1013 	/* update branch with beqz */
1014 	if (ctx->insns) {
1015 		int offset = ninsns_rvoff(ctx->ninsns - branch_off);
1016 		u32 insn = rv_beq(RV_REG_A0, RV_REG_ZERO, offset >> 1);
1017 		*(u32 *)(ctx->insns + branch_off) = insn;
1018 	}
1019 
1020 	/* arg1: prog */
1021 	emit_imm(RV_REG_A0, (const s64)p, ctx);
1022 	/* arg2: prog start time */
1023 	emit_mv(RV_REG_A1, RV_REG_S1, ctx);
1024 	/* arg3: &run_ctx */
1025 	emit_addi(RV_REG_A2, RV_REG_FP, -run_ctx_off, ctx);
1026 	ret = emit_call((const u64)bpf_trampoline_exit(p), true, ctx);
1027 	if (ret)
1028 		return ret;
1029 
1030 	bpf_tramp_image_add_skip(im, p, skip, ctx->ro_insns + ctx->ninsns);
1031 	return 0;
1032 }
1033 
1034 static int invoke_bpf(struct bpf_tramp_image *im, struct bpf_tramp_nodes *tn,
1035 		      int args_off, int retval_off, int run_ctx_off, int func_meta_off,
1036 		      bool save_ret, u64 func_meta, int cookie_off,
1037 		      struct rv_jit_context *ctx)
1038 {
1039 	int i, cur_cookie = (cookie_off - args_off) / 8;
1040 
1041 	for (i = 0; i < tn->nr_nodes; i++) {
1042 		int err;
1043 
1044 		if (bpf_prog_calls_session_cookie(tn->nodes[i])) {
1045 			u64 meta = func_meta | ((u64)cur_cookie << BPF_TRAMP_COOKIE_INDEX_SHIFT);
1046 
1047 			emit_store_stack_imm64(RV_REG_T1, -func_meta_off, meta, ctx);
1048 			cur_cookie--;
1049 		}
1050 		err = invoke_bpf_prog(im, tn->nodes[i], args_off, retval_off,
1051 				      run_ctx_off, save_ret, ctx);
1052 		if (err)
1053 			return err;
1054 	}
1055 	return 0;
1056 }
1057 
1058 static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
1059 					 const struct btf_func_model *m,
1060 					 struct bpf_tramp_nodes *tnodes,
1061 					 void *func_addr, u32 flags,
1062 					 struct rv_jit_context *ctx)
1063 {
1064 	int i, ret, offset;
1065 	int *branches_off = NULL;
1066 	int stack_size = 0, nr_arg_slots = 0;
1067 	int retval_off, args_off, func_meta_off, ip_off;
1068 	int run_ctx_off, sreg_off, stk_arg_off, tcc_off;
1069 	int cookie_off, cookie_cnt;
1070 	struct bpf_tramp_nodes *fentry = &tnodes[BPF_TRAMP_FENTRY];
1071 	struct bpf_tramp_nodes *fexit = &tnodes[BPF_TRAMP_FEXIT];
1072 	struct bpf_tramp_nodes *fmod_ret = &tnodes[BPF_TRAMP_MODIFY_RETURN];
1073 	bool is_struct_ops = is_struct_ops_tramp(fentry);
1074 	void *orig_call = func_addr;
1075 	bool save_ret;
1076 	u64 func_meta;
1077 	u32 insn;
1078 
1079 	/* Two types of generated trampoline stack layout:
1080 	 *
1081 	 * 1. trampoline called from function entry
1082 	 * --------------------------------------
1083 	 * FP + 8	    [ RA to parent func	] return address to parent
1084 	 *					  function
1085 	 * FP + 0	    [ FP of parent func ] frame pointer of parent
1086 	 *					  function
1087 	 * FP - 8           [ T0 to traced func ] return address of traced
1088 	 *					  function
1089 	 * FP - 16	    [ FP of traced func ] frame pointer of traced
1090 	 *					  function
1091 	 * --------------------------------------
1092 	 *
1093 	 * 2. trampoline called directly
1094 	 * --------------------------------------
1095 	 * FP - 8	    [ RA to caller func ] return address to caller
1096 	 *					  function
1097 	 * FP - 16	    [ FP of caller func	] frame pointer of caller
1098 	 *					  function
1099 	 * --------------------------------------
1100 	 *
1101 	 * FP - retval_off  [ return value      ] BPF_TRAMP_F_CALL_ORIG or
1102 	 *					  BPF_TRAMP_F_RET_FENTRY_RET
1103 	 *                  [ argN              ]
1104 	 *                  [ ...               ]
1105 	 * FP - args_off    [ arg1              ]
1106 	 *
1107 	 * FP - func_meta_off [ regs count, etc ]
1108 	 *
1109 	 * FP - ip_off      [ traced func	] BPF_TRAMP_F_IP_ARG
1110 	 *
1111 	 *                  [ stack cookie N    ]
1112 	 *                  [ ...               ]
1113 	 * FP - cookie_off  [ stack cookie 1    ]
1114 	 *
1115 	 * FP - run_ctx_off [ bpf_tramp_run_ctx ]
1116 	 *
1117 	 * FP - sreg_off    [ callee saved reg	]
1118 	 *
1119 	 * FP - tcc_off     [ tail call count	] BPF_TRAMP_F_TAIL_CALL_CTX
1120 	 *
1121 	 *		    [ pads              ] pads for 16 bytes alignment
1122 	 *
1123 	 *		    [ stack_argN        ]
1124 	 *		    [ ...               ]
1125 	 * FP - stk_arg_off [ stack_arg1        ] BPF_TRAMP_F_CALL_ORIG
1126 	 */
1127 
1128 	if (flags & (BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SHARE_IPMODIFY))
1129 		return -ENOTSUPP;
1130 
1131 	if (m->nr_args > MAX_BPF_FUNC_ARGS)
1132 		return -ENOTSUPP;
1133 
1134 	for (i = 0; i < m->nr_args; i++)
1135 		nr_arg_slots += round_up(m->arg_size[i], 8) / 8;
1136 
1137 	/* room of trampoline frame to store return address and frame pointer */
1138 	stack_size += 16;
1139 
1140 	save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
1141 	if (save_ret)
1142 		stack_size += 16; /* Save both RV_REG_T6 (BPF R0) and RV_REG_A0 */
1143 	retval_off = stack_size;
1144 
1145 	stack_size += nr_arg_slots * 8;
1146 	args_off = stack_size;
1147 
1148 	/* function metadata, such as regs count */
1149 	stack_size += 8;
1150 	func_meta_off = stack_size;
1151 
1152 	if (flags & BPF_TRAMP_F_IP_ARG) {
1153 		stack_size += 8;
1154 		ip_off = stack_size;
1155 	}
1156 
1157 	cookie_cnt = bpf_fsession_cookie_cnt(tnodes);
1158 	/* room for session cookies */
1159 	stack_size += cookie_cnt * 8;
1160 	cookie_off = stack_size;
1161 
1162 	stack_size += round_up(sizeof(struct bpf_tramp_run_ctx), 8);
1163 	run_ctx_off = stack_size;
1164 
1165 	stack_size += 8;
1166 	sreg_off = stack_size;
1167 
1168 	if (flags & BPF_TRAMP_F_TAIL_CALL_CTX) {
1169 		stack_size += 8;
1170 		tcc_off = stack_size;
1171 	}
1172 
1173 	if ((flags & BPF_TRAMP_F_CALL_ORIG) && (nr_arg_slots - RV_MAX_REG_ARGS > 0))
1174 		stack_size += (nr_arg_slots - RV_MAX_REG_ARGS) * 8;
1175 
1176 	stack_size = round_up(stack_size, STACK_ALIGN);
1177 
1178 	/* room for args on stack must be at the top of stack */
1179 	stk_arg_off = stack_size;
1180 
1181 	if (!is_struct_ops) {
1182 		/* For the trampoline called from function entry,
1183 		 * the frame of traced function and the frame of
1184 		 * trampoline need to be considered.
1185 		 */
1186 		emit_addi(RV_REG_SP, RV_REG_SP, -16, ctx);
1187 		emit_sd(RV_REG_SP, 8, RV_REG_RA, ctx);
1188 		emit_sd(RV_REG_SP, 0, RV_REG_FP, ctx);
1189 		emit_addi(RV_REG_FP, RV_REG_SP, 16, ctx);
1190 
1191 		emit_addi(RV_REG_SP, RV_REG_SP, -stack_size, ctx);
1192 		emit_sd(RV_REG_SP, stack_size - 8, RV_REG_T0, ctx);
1193 		emit_sd(RV_REG_SP, stack_size - 16, RV_REG_FP, ctx);
1194 		emit_addi(RV_REG_FP, RV_REG_SP, stack_size, ctx);
1195 	} else {
1196 		/* emit kcfi hash */
1197 		emit_kcfi(cfi_get_func_hash(func_addr), ctx);
1198 		/* For the trampoline called directly, just handle
1199 		 * the frame of trampoline.
1200 		 */
1201 		emit_addi(RV_REG_SP, RV_REG_SP, -stack_size, ctx);
1202 		emit_sd(RV_REG_SP, stack_size - 8, RV_REG_RA, ctx);
1203 		emit_sd(RV_REG_SP, stack_size - 16, RV_REG_FP, ctx);
1204 		emit_addi(RV_REG_FP, RV_REG_SP, stack_size, ctx);
1205 	}
1206 
1207 	/* store tail call count */
1208 	if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
1209 		emit_sd(RV_REG_FP, -tcc_off, RV_REG_TCC, ctx);
1210 
1211 	/* callee saved register S1 to pass start time */
1212 	emit_sd(RV_REG_FP, -sreg_off, RV_REG_S1, ctx);
1213 
1214 	/* store ip address of the traced function */
1215 	if (flags & BPF_TRAMP_F_IP_ARG)
1216 		emit_store_stack_imm64(RV_REG_T1, -ip_off, (u64)func_addr, ctx);
1217 
1218 	func_meta = nr_arg_slots;
1219 	emit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx);
1220 
1221 	/*
1222 	 * A direct struct_ops call has its first stack argument at the incoming
1223 	 * SP, which the trampoline keeps as FP. The fentry path pushes the
1224 	 * parent frame first, so its incoming stack arguments start at FP + 16.
1225 	 */
1226 	store_args(nr_arg_slots, args_off, is_struct_ops ? 0 : 16, ctx);
1227 
1228 	if (bpf_fsession_cnt(tnodes)) {
1229 		/* clear all session cookies' value */
1230 		for (i = 0; i < cookie_cnt; i++)
1231 			emit_sd(RV_REG_FP, -cookie_off + 8 * i, RV_REG_ZERO, ctx);
1232 		/* clear return value to make sure fentry always get 0 */
1233 		emit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx);
1234 	}
1235 
1236 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
1237 		emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
1238 		ret = emit_call((const u64)__bpf_tramp_enter, true, ctx);
1239 		if (ret)
1240 			return ret;
1241 	}
1242 
1243 	if (fentry->nr_nodes) {
1244 		ret = invoke_bpf(im, fentry, args_off, retval_off, run_ctx_off, func_meta_off,
1245 				 flags & BPF_TRAMP_F_RET_FENTRY_RET, func_meta, cookie_off, ctx);
1246 		if (ret)
1247 			return ret;
1248 	}
1249 
1250 	if (fmod_ret->nr_nodes) {
1251 		branches_off = kvzalloc_objs(int, fmod_ret->nr_nodes);
1252 		if (!branches_off)
1253 			return -ENOMEM;
1254 
1255 		/* cleanup to avoid garbage return value confusion */
1256 		emit_sd(RV_REG_FP, -retval_off, RV_REG_ZERO, ctx);
1257 		for (i = 0; i < fmod_ret->nr_nodes; i++) {
1258 			ret = invoke_bpf_prog(im, fmod_ret->nodes[i], args_off, retval_off,
1259 					      run_ctx_off, true, ctx);
1260 			if (ret)
1261 				goto out;
1262 			emit_ld(RV_REG_T1, -retval_off, RV_REG_FP, ctx);
1263 			branches_off[i] = ctx->ninsns;
1264 			/* nop reserved for conditional jump */
1265 			emit(rv_nop(), ctx);
1266 		}
1267 	}
1268 
1269 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
1270 		/* skip to actual body of traced function */
1271 		orig_call += RV_FENTRY_NINSNS * 4;
1272 		restore_args(min_t(int, nr_arg_slots, RV_MAX_REG_ARGS), args_off, ctx);
1273 		restore_stack_args(nr_arg_slots - RV_MAX_REG_ARGS, args_off, stk_arg_off, ctx);
1274 		/* restore TCC to RV_REG_TCC before calling the orig bpf func */
1275 		if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
1276 			emit_ld(RV_REG_TCC, -tcc_off, RV_REG_FP, ctx);
1277 		ret = emit_call((const u64)orig_call, true, ctx);
1278 		if (ret)
1279 			goto out;
1280 		/* store updated TCC back to stack after calling the orig bpf func */
1281 		if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
1282 			emit_sd(RV_REG_FP, -tcc_off, RV_REG_TCC, ctx);
1283 		emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx);
1284 		emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx);
1285 	}
1286 
1287 	/* update branches saved in invoke_bpf_mod_ret with bnez */
1288 	for (i = 0; ctx->insns && i < fmod_ret->nr_nodes; i++) {
1289 		offset = ninsns_rvoff(ctx->ninsns - branches_off[i]);
1290 		insn = rv_bne(RV_REG_T1, RV_REG_ZERO, offset >> 1);
1291 		*(u32 *)(ctx->insns + branches_off[i]) = insn;
1292 	}
1293 
1294 	/* set "is_return" flag for fsession */
1295 	func_meta |= (1ULL << BPF_TRAMP_IS_RETURN_SHIFT);
1296 	if (bpf_fsession_cnt(tnodes))
1297 		emit_store_stack_imm64(RV_REG_T1, -func_meta_off, func_meta, ctx);
1298 
1299 	if (fexit->nr_nodes) {
1300 		ret = invoke_bpf(im, fexit, args_off, retval_off, run_ctx_off, func_meta_off,
1301 				 false, func_meta, cookie_off, ctx);
1302 		if (ret)
1303 			goto out;
1304 	}
1305 
1306 	if (flags & BPF_TRAMP_F_CALL_ORIG) {
1307 		emit_imm(RV_REG_A0, ctx->insns ? (const s64)im : RV_MAX_COUNT_IMM, ctx);
1308 		ret = emit_call((const u64)__bpf_tramp_exit, true, ctx);
1309 		if (ret)
1310 			goto out;
1311 	}
1312 
1313 	if (flags & BPF_TRAMP_F_RESTORE_REGS)
1314 		restore_args(min_t(int, nr_arg_slots, RV_MAX_REG_ARGS), args_off, ctx);
1315 
1316 	if (save_ret) {
1317 		emit_ld(regmap[BPF_REG_0], -(retval_off - 8), RV_REG_FP, ctx);
1318 		if (is_struct_ops) {
1319 			ret = sign_extend(RV_REG_A0, regmap[BPF_REG_0], m->ret_size,
1320 					  m->ret_flags & BTF_FMODEL_SIGNED_ARG, ctx);
1321 			if (ret)
1322 				goto out;
1323 		} else {
1324 			emit_ld(RV_REG_A0, -retval_off, RV_REG_FP, ctx);
1325 		}
1326 	}
1327 
1328 	emit_ld(RV_REG_S1, -sreg_off, RV_REG_FP, ctx);
1329 
1330 	/* restore TCC from stack to RV_REG_TCC */
1331 	if (flags & BPF_TRAMP_F_TAIL_CALL_CTX)
1332 		emit_ld(RV_REG_TCC, -tcc_off, RV_REG_FP, ctx);
1333 
1334 	if (!is_struct_ops) {
1335 		/* trampoline called from function entry */
1336 		emit_ld(RV_REG_T0, stack_size - 8, RV_REG_SP, ctx);
1337 		emit_ld(RV_REG_FP, stack_size - 16, RV_REG_SP, ctx);
1338 		emit_addi(RV_REG_SP, RV_REG_SP, stack_size, ctx);
1339 
1340 		emit_ld(RV_REG_RA, 8, RV_REG_SP, ctx);
1341 		emit_ld(RV_REG_FP, 0, RV_REG_SP, ctx);
1342 		emit_addi(RV_REG_SP, RV_REG_SP, 16, ctx);
1343 
1344 		if (flags & BPF_TRAMP_F_SKIP_FRAME)
1345 			/* return to parent function */
1346 			emit_jalr(RV_REG_ZERO, RV_REG_RA, 0, ctx);
1347 		else
1348 			/* return to traced function */
1349 			emit_jalr(RV_REG_ZERO, RV_REG_T0, 0, ctx);
1350 	} else {
1351 		/* trampoline called directly */
1352 		emit_ld(RV_REG_RA, stack_size - 8, RV_REG_SP, ctx);
1353 		emit_ld(RV_REG_FP, stack_size - 16, RV_REG_SP, ctx);
1354 		emit_addi(RV_REG_SP, RV_REG_SP, stack_size, ctx);
1355 
1356 		emit_jalr(RV_REG_ZERO, RV_REG_RA, 0, ctx);
1357 	}
1358 
1359 	ret = ctx->ninsns;
1360 out:
1361 	kvfree(branches_off);
1362 	return ret;
1363 }
1364 
1365 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
1366 			     struct bpf_tramp_nodes *tnodes, void *func_addr)
1367 {
1368 	struct bpf_tramp_image im = {};
1369 	struct rv_jit_context ctx;
1370 	int ret;
1371 
1372 	ctx.ninsns = 0;
1373 	ctx.insns = NULL;
1374 	ctx.ro_insns = NULL;
1375 	ret = __arch_prepare_bpf_trampoline(&im, m, tnodes, func_addr, flags, &ctx);
1376 
1377 	return ret < 0 ? ret : ninsns_rvoff(ctx.ninsns);
1378 }
1379 
1380 void *arch_alloc_bpf_trampoline(unsigned int size)
1381 {
1382 	return bpf_prog_pack_alloc(size, bpf_fill_ill_insns, false);
1383 }
1384 
1385 void arch_free_bpf_trampoline(void *image, unsigned int size)
1386 {
1387 	bpf_prog_pack_free(image, size);
1388 }
1389 
1390 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *ro_image,
1391 				void *ro_image_end, const struct btf_func_model *m,
1392 				u32 flags, struct bpf_tramp_nodes *tnodes,
1393 				void *func_addr)
1394 {
1395 	int ret;
1396 	void *image, *res;
1397 	struct rv_jit_context ctx;
1398 	u32 size = ro_image_end - ro_image;
1399 
1400 	image = kvmalloc(size, GFP_KERNEL);
1401 	if (!image)
1402 		return -ENOMEM;
1403 
1404 	ctx.ninsns = 0;
1405 	ctx.insns = image;
1406 	ctx.ro_insns = ro_image;
1407 	ret = __arch_prepare_bpf_trampoline(im, m, tnodes, func_addr, flags, &ctx);
1408 	if (ret < 0)
1409 		goto out;
1410 
1411 	if (WARN_ON(size < ninsns_rvoff(ctx.ninsns))) {
1412 		ret = -E2BIG;
1413 		goto out;
1414 	}
1415 
1416 	res = bpf_arch_text_copy(ro_image, image, size);
1417 	if (IS_ERR(res)) {
1418 		ret = PTR_ERR(res);
1419 		goto out;
1420 	}
1421 
1422 out:
1423 	kvfree(image);
1424 	return ret < 0 ? ret : size;
1425 }
1426 
1427 int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
1428 		      bool extra_pass)
1429 {
1430 	bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
1431 		    BPF_CLASS(insn->code) == BPF_JMP;
1432 	int s, e, rvoff, ret, i = insn - ctx->prog->insnsi;
1433 	struct bpf_prog_aux *aux = ctx->prog->aux;
1434 	u8 rd = -1, rs = -1, code = insn->code;
1435 	s16 off = insn->off;
1436 	s32 imm = insn->imm;
1437 
1438 	init_regs(&rd, &rs, insn, ctx);
1439 
1440 	switch (code) {
1441 	/* dst = src */
1442 	case BPF_ALU | BPF_MOV | BPF_X:
1443 	case BPF_ALU64 | BPF_MOV | BPF_X:
1444 		if (insn_is_cast_user(insn)) {
1445 			emit_mv(RV_REG_T1, rs, ctx);
1446 			emit_zextw(RV_REG_T1, RV_REG_T1, ctx);
1447 			emit_imm(rd, (ctx->user_vm_start >> 32) << 32, ctx);
1448 			emit(rv_beq(RV_REG_T1, RV_REG_ZERO, 4), ctx);
1449 			emit_or(RV_REG_T1, rd, RV_REG_T1, ctx);
1450 			emit_mv(rd, RV_REG_T1, ctx);
1451 			break;
1452 		} else if (insn_is_mov_percpu_addr(insn)) {
1453 			if (rd != rs)
1454 				emit_mv(rd, rs, ctx);
1455 #ifdef CONFIG_SMP
1456 			/* Load current CPU number in T1 */
1457 			emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu),
1458 				RV_REG_TP, ctx);
1459 			/* Load address of __per_cpu_offset array in T2 */
1460 			emit_addr(RV_REG_T2, (u64)&__per_cpu_offset, extra_pass, ctx);
1461 			/* Get address of __per_cpu_offset[cpu] in T1 */
1462 			emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx);
1463 			/* Load __per_cpu_offset[cpu] in T1 */
1464 			emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx);
1465 			/* Add the offset to Rd */
1466 			emit_add(rd, rd, RV_REG_T1, ctx);
1467 #endif
1468 		}
1469 		if (imm == 1) {
1470 			/* Special mov32 for zext */
1471 			emit_zextw(rd, rd, ctx);
1472 			break;
1473 		}
1474 		switch (insn->off) {
1475 		case 0:
1476 			emit_mv(rd, rs, ctx);
1477 			break;
1478 		case 8:
1479 			emit_sextb(rd, rs, ctx);
1480 			break;
1481 		case 16:
1482 			emit_sexth(rd, rs, ctx);
1483 			break;
1484 		case 32:
1485 			emit_sextw(rd, rs, ctx);
1486 			break;
1487 		}
1488 		if (!is64 && !aux->verifier_zext)
1489 			emit_zextw(rd, rd, ctx);
1490 		break;
1491 
1492 	/* dst = dst OP src */
1493 	case BPF_ALU | BPF_ADD | BPF_X:
1494 	case BPF_ALU64 | BPF_ADD | BPF_X:
1495 		emit_add(rd, rd, rs, ctx);
1496 		if (!is64 && !aux->verifier_zext)
1497 			emit_zextw(rd, rd, ctx);
1498 		break;
1499 	case BPF_ALU | BPF_SUB | BPF_X:
1500 	case BPF_ALU64 | BPF_SUB | BPF_X:
1501 		if (is64)
1502 			emit_sub(rd, rd, rs, ctx);
1503 		else
1504 			emit_subw(rd, rd, rs, ctx);
1505 
1506 		if (!is64 && !aux->verifier_zext)
1507 			emit_zextw(rd, rd, ctx);
1508 		break;
1509 	case BPF_ALU | BPF_AND | BPF_X:
1510 	case BPF_ALU64 | BPF_AND | BPF_X:
1511 		emit_and(rd, rd, rs, ctx);
1512 		if (!is64 && !aux->verifier_zext)
1513 			emit_zextw(rd, rd, ctx);
1514 		break;
1515 	case BPF_ALU | BPF_OR | BPF_X:
1516 	case BPF_ALU64 | BPF_OR | BPF_X:
1517 		emit_or(rd, rd, rs, ctx);
1518 		if (!is64 && !aux->verifier_zext)
1519 			emit_zextw(rd, rd, ctx);
1520 		break;
1521 	case BPF_ALU | BPF_XOR | BPF_X:
1522 	case BPF_ALU64 | BPF_XOR | BPF_X:
1523 		emit_xor(rd, rd, rs, ctx);
1524 		if (!is64 && !aux->verifier_zext)
1525 			emit_zextw(rd, rd, ctx);
1526 		break;
1527 	case BPF_ALU | BPF_MUL | BPF_X:
1528 	case BPF_ALU64 | BPF_MUL | BPF_X:
1529 		emit(is64 ? rv_mul(rd, rd, rs) : rv_mulw(rd, rd, rs), ctx);
1530 		if (!is64 && !aux->verifier_zext)
1531 			emit_zextw(rd, rd, ctx);
1532 		break;
1533 	case BPF_ALU | BPF_DIV | BPF_X:
1534 	case BPF_ALU64 | BPF_DIV | BPF_X:
1535 		if (off)
1536 			emit(is64 ? rv_div(rd, rd, rs) : rv_divw(rd, rd, rs), ctx);
1537 		else
1538 			emit(is64 ? rv_divu(rd, rd, rs) : rv_divuw(rd, rd, rs), ctx);
1539 		if (!is64 && !aux->verifier_zext)
1540 			emit_zextw(rd, rd, ctx);
1541 		break;
1542 	case BPF_ALU | BPF_MOD | BPF_X:
1543 	case BPF_ALU64 | BPF_MOD | BPF_X:
1544 		if (off)
1545 			emit(is64 ? rv_rem(rd, rd, rs) : rv_remw(rd, rd, rs), ctx);
1546 		else
1547 			emit(is64 ? rv_remu(rd, rd, rs) : rv_remuw(rd, rd, rs), ctx);
1548 		if (!is64 && !aux->verifier_zext)
1549 			emit_zextw(rd, rd, ctx);
1550 		break;
1551 	case BPF_ALU | BPF_LSH | BPF_X:
1552 	case BPF_ALU64 | BPF_LSH | BPF_X:
1553 		emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
1554 		if (!is64 && !aux->verifier_zext)
1555 			emit_zextw(rd, rd, ctx);
1556 		break;
1557 	case BPF_ALU | BPF_RSH | BPF_X:
1558 	case BPF_ALU64 | BPF_RSH | BPF_X:
1559 		emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx);
1560 		if (!is64 && !aux->verifier_zext)
1561 			emit_zextw(rd, rd, ctx);
1562 		break;
1563 	case BPF_ALU | BPF_ARSH | BPF_X:
1564 	case BPF_ALU64 | BPF_ARSH | BPF_X:
1565 		emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx);
1566 		if (!is64 && !aux->verifier_zext)
1567 			emit_zextw(rd, rd, ctx);
1568 		break;
1569 
1570 	/* dst = -dst */
1571 	case BPF_ALU | BPF_NEG:
1572 	case BPF_ALU64 | BPF_NEG:
1573 		emit_sub(rd, RV_REG_ZERO, rd, ctx);
1574 		if (!is64 && !aux->verifier_zext)
1575 			emit_zextw(rd, rd, ctx);
1576 		break;
1577 
1578 	/* dst = BSWAP##imm(dst) */
1579 	case BPF_ALU | BPF_END | BPF_FROM_LE:
1580 		switch (imm) {
1581 		case 16:
1582 			emit_zexth(rd, rd, ctx);
1583 			break;
1584 		case 32:
1585 			if (!aux->verifier_zext)
1586 				emit_zextw(rd, rd, ctx);
1587 			break;
1588 		case 64:
1589 			/* Do nothing */
1590 			break;
1591 		}
1592 		break;
1593 	case BPF_ALU | BPF_END | BPF_FROM_BE:
1594 	case BPF_ALU64 | BPF_END | BPF_FROM_LE:
1595 		emit_bswap(rd, imm, ctx);
1596 		break;
1597 
1598 	/* dst = imm */
1599 	case BPF_ALU | BPF_MOV | BPF_K:
1600 	case BPF_ALU64 | BPF_MOV | BPF_K:
1601 		emit_imm(rd, imm, ctx);
1602 		if (!is64 && !aux->verifier_zext)
1603 			emit_zextw(rd, rd, ctx);
1604 		break;
1605 
1606 	/* dst = dst OP imm */
1607 	case BPF_ALU | BPF_ADD | BPF_K:
1608 	case BPF_ALU64 | BPF_ADD | BPF_K:
1609 		if (is_12b_int(imm)) {
1610 			emit_addi(rd, rd, imm, ctx);
1611 		} else {
1612 			emit_imm(RV_REG_T1, imm, ctx);
1613 			emit_add(rd, rd, RV_REG_T1, ctx);
1614 		}
1615 		if (!is64 && !aux->verifier_zext)
1616 			emit_zextw(rd, rd, ctx);
1617 		break;
1618 	case BPF_ALU | BPF_SUB | BPF_K:
1619 	case BPF_ALU64 | BPF_SUB | BPF_K:
1620 		if (is_12b_int(-imm)) {
1621 			emit_addi(rd, rd, -imm, ctx);
1622 		} else {
1623 			emit_imm(RV_REG_T1, imm, ctx);
1624 			emit_sub(rd, rd, RV_REG_T1, ctx);
1625 		}
1626 		if (!is64 && !aux->verifier_zext)
1627 			emit_zextw(rd, rd, ctx);
1628 		break;
1629 	case BPF_ALU | BPF_AND | BPF_K:
1630 	case BPF_ALU64 | BPF_AND | BPF_K:
1631 		if (is_12b_int(imm)) {
1632 			emit_andi(rd, rd, imm, ctx);
1633 		} else {
1634 			emit_imm(RV_REG_T1, imm, ctx);
1635 			emit_and(rd, rd, RV_REG_T1, ctx);
1636 		}
1637 		if (!is64 && !aux->verifier_zext)
1638 			emit_zextw(rd, rd, ctx);
1639 		break;
1640 	case BPF_ALU | BPF_OR | BPF_K:
1641 	case BPF_ALU64 | BPF_OR | BPF_K:
1642 		if (is_12b_int(imm)) {
1643 			emit(rv_ori(rd, rd, imm), ctx);
1644 		} else {
1645 			emit_imm(RV_REG_T1, imm, ctx);
1646 			emit_or(rd, rd, RV_REG_T1, ctx);
1647 		}
1648 		if (!is64 && !aux->verifier_zext)
1649 			emit_zextw(rd, rd, ctx);
1650 		break;
1651 	case BPF_ALU | BPF_XOR | BPF_K:
1652 	case BPF_ALU64 | BPF_XOR | BPF_K:
1653 		if (is_12b_int(imm)) {
1654 			emit(rv_xori(rd, rd, imm), ctx);
1655 		} else {
1656 			emit_imm(RV_REG_T1, imm, ctx);
1657 			emit_xor(rd, rd, RV_REG_T1, ctx);
1658 		}
1659 		if (!is64 && !aux->verifier_zext)
1660 			emit_zextw(rd, rd, ctx);
1661 		break;
1662 	case BPF_ALU | BPF_MUL | BPF_K:
1663 	case BPF_ALU64 | BPF_MUL | BPF_K:
1664 		emit_imm(RV_REG_T1, imm, ctx);
1665 		emit(is64 ? rv_mul(rd, rd, RV_REG_T1) :
1666 		     rv_mulw(rd, rd, RV_REG_T1), ctx);
1667 		if (!is64 && !aux->verifier_zext)
1668 			emit_zextw(rd, rd, ctx);
1669 		break;
1670 	case BPF_ALU | BPF_DIV | BPF_K:
1671 	case BPF_ALU64 | BPF_DIV | BPF_K:
1672 		emit_imm(RV_REG_T1, imm, ctx);
1673 		if (off)
1674 			emit(is64 ? rv_div(rd, rd, RV_REG_T1) :
1675 			     rv_divw(rd, rd, RV_REG_T1), ctx);
1676 		else
1677 			emit(is64 ? rv_divu(rd, rd, RV_REG_T1) :
1678 			     rv_divuw(rd, rd, RV_REG_T1), ctx);
1679 		if (!is64 && !aux->verifier_zext)
1680 			emit_zextw(rd, rd, ctx);
1681 		break;
1682 	case BPF_ALU | BPF_MOD | BPF_K:
1683 	case BPF_ALU64 | BPF_MOD | BPF_K:
1684 		emit_imm(RV_REG_T1, imm, ctx);
1685 		if (off)
1686 			emit(is64 ? rv_rem(rd, rd, RV_REG_T1) :
1687 			     rv_remw(rd, rd, RV_REG_T1), ctx);
1688 		else
1689 			emit(is64 ? rv_remu(rd, rd, RV_REG_T1) :
1690 			     rv_remuw(rd, rd, RV_REG_T1), ctx);
1691 		if (!is64 && !aux->verifier_zext)
1692 			emit_zextw(rd, rd, ctx);
1693 		break;
1694 	case BPF_ALU | BPF_LSH | BPF_K:
1695 	case BPF_ALU64 | BPF_LSH | BPF_K:
1696 		emit_slli(rd, rd, imm, ctx);
1697 
1698 		if (!is64 && !aux->verifier_zext)
1699 			emit_zextw(rd, rd, ctx);
1700 		break;
1701 	case BPF_ALU | BPF_RSH | BPF_K:
1702 	case BPF_ALU64 | BPF_RSH | BPF_K:
1703 		if (is64)
1704 			emit_srli(rd, rd, imm, ctx);
1705 		else
1706 			emit(rv_srliw(rd, rd, imm), ctx);
1707 
1708 		if (!is64 && !aux->verifier_zext)
1709 			emit_zextw(rd, rd, ctx);
1710 		break;
1711 	case BPF_ALU | BPF_ARSH | BPF_K:
1712 	case BPF_ALU64 | BPF_ARSH | BPF_K:
1713 		if (is64)
1714 			emit_srai(rd, rd, imm, ctx);
1715 		else
1716 			emit(rv_sraiw(rd, rd, imm), ctx);
1717 
1718 		if (!is64 && !aux->verifier_zext)
1719 			emit_zextw(rd, rd, ctx);
1720 		break;
1721 
1722 	/* JUMP reg */
1723 	case BPF_JMP | BPF_JA | BPF_X:
1724 		emit_jalr(RV_REG_ZERO, rd, 0, ctx);
1725 		break;
1726 
1727 	/* JUMP off */
1728 	case BPF_JMP | BPF_JA:
1729 	case BPF_JMP32 | BPF_JA:
1730 		if (BPF_CLASS(code) == BPF_JMP)
1731 			rvoff = rv_offset(i, off, ctx);
1732 		else
1733 			rvoff = rv_offset(i, imm, ctx);
1734 		ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
1735 		if (ret)
1736 			return ret;
1737 		break;
1738 
1739 	/* IF (dst COND src) JUMP off */
1740 	case BPF_JMP | BPF_JEQ | BPF_X:
1741 	case BPF_JMP32 | BPF_JEQ | BPF_X:
1742 	case BPF_JMP | BPF_JGT | BPF_X:
1743 	case BPF_JMP32 | BPF_JGT | BPF_X:
1744 	case BPF_JMP | BPF_JLT | BPF_X:
1745 	case BPF_JMP32 | BPF_JLT | BPF_X:
1746 	case BPF_JMP | BPF_JGE | BPF_X:
1747 	case BPF_JMP32 | BPF_JGE | BPF_X:
1748 	case BPF_JMP | BPF_JLE | BPF_X:
1749 	case BPF_JMP32 | BPF_JLE | BPF_X:
1750 	case BPF_JMP | BPF_JNE | BPF_X:
1751 	case BPF_JMP32 | BPF_JNE | BPF_X:
1752 	case BPF_JMP | BPF_JSGT | BPF_X:
1753 	case BPF_JMP32 | BPF_JSGT | BPF_X:
1754 	case BPF_JMP | BPF_JSLT | BPF_X:
1755 	case BPF_JMP32 | BPF_JSLT | BPF_X:
1756 	case BPF_JMP | BPF_JSGE | BPF_X:
1757 	case BPF_JMP32 | BPF_JSGE | BPF_X:
1758 	case BPF_JMP | BPF_JSLE | BPF_X:
1759 	case BPF_JMP32 | BPF_JSLE | BPF_X:
1760 	case BPF_JMP | BPF_JSET | BPF_X:
1761 	case BPF_JMP32 | BPF_JSET | BPF_X:
1762 		rvoff = rv_offset(i, off, ctx);
1763 		if (!is64) {
1764 			s = ctx->ninsns;
1765 			if (is_signed_bpf_cond(BPF_OP(code))) {
1766 				emit_sextw_alt(&rs, RV_REG_T1, ctx);
1767 				emit_sextw_alt(&rd, RV_REG_T2, ctx);
1768 			} else {
1769 				emit_zextw_alt(&rs, RV_REG_T1, ctx);
1770 				emit_zextw_alt(&rd, RV_REG_T2, ctx);
1771 			}
1772 			e = ctx->ninsns;
1773 
1774 			/* Adjust for extra insns */
1775 			rvoff -= ninsns_rvoff(e - s);
1776 		}
1777 
1778 		if (BPF_OP(code) == BPF_JSET) {
1779 			/* Adjust for and */
1780 			rvoff -= 4;
1781 			emit_and(RV_REG_T1, rd, rs, ctx);
1782 			emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
1783 		} else {
1784 			emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
1785 		}
1786 		break;
1787 
1788 	/* IF (dst COND imm) JUMP off */
1789 	case BPF_JMP | BPF_JEQ | BPF_K:
1790 	case BPF_JMP32 | BPF_JEQ | BPF_K:
1791 	case BPF_JMP | BPF_JGT | BPF_K:
1792 	case BPF_JMP32 | BPF_JGT | BPF_K:
1793 	case BPF_JMP | BPF_JLT | BPF_K:
1794 	case BPF_JMP32 | BPF_JLT | BPF_K:
1795 	case BPF_JMP | BPF_JGE | BPF_K:
1796 	case BPF_JMP32 | BPF_JGE | BPF_K:
1797 	case BPF_JMP | BPF_JLE | BPF_K:
1798 	case BPF_JMP32 | BPF_JLE | BPF_K:
1799 	case BPF_JMP | BPF_JNE | BPF_K:
1800 	case BPF_JMP32 | BPF_JNE | BPF_K:
1801 	case BPF_JMP | BPF_JSGT | BPF_K:
1802 	case BPF_JMP32 | BPF_JSGT | BPF_K:
1803 	case BPF_JMP | BPF_JSLT | BPF_K:
1804 	case BPF_JMP32 | BPF_JSLT | BPF_K:
1805 	case BPF_JMP | BPF_JSGE | BPF_K:
1806 	case BPF_JMP32 | BPF_JSGE | BPF_K:
1807 	case BPF_JMP | BPF_JSLE | BPF_K:
1808 	case BPF_JMP32 | BPF_JSLE | BPF_K:
1809 		rvoff = rv_offset(i, off, ctx);
1810 		s = ctx->ninsns;
1811 		if (imm)
1812 			emit_imm(RV_REG_T1, imm, ctx);
1813 		rs = imm ? RV_REG_T1 : RV_REG_ZERO;
1814 		if (!is64) {
1815 			if (is_signed_bpf_cond(BPF_OP(code))) {
1816 				emit_sextw_alt(&rd, RV_REG_T2, ctx);
1817 				/* rs has been sign extended */
1818 			} else {
1819 				emit_zextw_alt(&rd, RV_REG_T2, ctx);
1820 				if (imm)
1821 					emit_zextw(rs, rs, ctx);
1822 			}
1823 		}
1824 		e = ctx->ninsns;
1825 
1826 		/* Adjust for extra insns */
1827 		rvoff -= ninsns_rvoff(e - s);
1828 		emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
1829 		break;
1830 
1831 	case BPF_JMP | BPF_JSET | BPF_K:
1832 	case BPF_JMP32 | BPF_JSET | BPF_K:
1833 		rvoff = rv_offset(i, off, ctx);
1834 		s = ctx->ninsns;
1835 		if (is_12b_int(imm)) {
1836 			emit_andi(RV_REG_T1, rd, imm, ctx);
1837 		} else {
1838 			emit_imm(RV_REG_T1, imm, ctx);
1839 			emit_and(RV_REG_T1, rd, RV_REG_T1, ctx);
1840 		}
1841 		/* For jset32, we should clear the upper 32 bits of t1, but
1842 		 * sign-extension is sufficient here and saves one instruction,
1843 		 * as t1 is used only in comparison against zero.
1844 		 */
1845 		if (!is64 && imm < 0)
1846 			emit_sextw(RV_REG_T1, RV_REG_T1, ctx);
1847 		e = ctx->ninsns;
1848 		rvoff -= ninsns_rvoff(e - s);
1849 		emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
1850 		break;
1851 
1852 	/* function call */
1853 	case BPF_JMP | BPF_CALL:
1854 	{
1855 		bool fixed_addr;
1856 		u64 addr;
1857 
1858 		/* Inline calls to bpf_get_smp_processor_id()
1859 		 *
1860 		 * RV_REG_TP holds the address of the current CPU's task_struct and thread_info is
1861 		 * at offset 0 in task_struct.
1862 		 * Load cpu from thread_info:
1863 		 *     Set R0 to ((struct thread_info *)(RV_REG_TP))->cpu
1864 		 *
1865 		 * This replicates the implementation of raw_smp_processor_id() on RISCV
1866 		 */
1867 		if (insn->src_reg == 0 && insn->imm == BPF_FUNC_get_smp_processor_id) {
1868 			/* Load current CPU number in R0 */
1869 			emit_lw(bpf_to_rv_reg(BPF_REG_0, ctx), offsetof(struct thread_info, cpu),
1870 				RV_REG_TP, ctx);
1871 			break;
1872 		}
1873 
1874 		/* Implement helper call to bpf_get_current_task/_btf() inline */
1875 		if (insn->src_reg == 0 && (insn->imm == BPF_FUNC_get_current_task ||
1876 					   insn->imm == BPF_FUNC_get_current_task_btf)) {
1877 			emit_mv(bpf_to_rv_reg(BPF_REG_0, ctx), RV_REG_TP, ctx);
1878 			break;
1879 		}
1880 
1881 		mark_call(ctx);
1882 		ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
1883 					    &addr, &fixed_addr);
1884 		if (ret < 0)
1885 			return ret;
1886 
1887 		if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
1888 			const struct btf_func_model *fm;
1889 			int idx;
1890 
1891 			fm = bpf_jit_find_kfunc_model(ctx->prog, insn);
1892 			if (!fm)
1893 				return -EINVAL;
1894 
1895 			for (idx = 0; idx < fm->nr_args; idx++) {
1896 				bool sign = fm->arg_flags[idx] & BTF_FMODEL_SIGNED_ARG;
1897 				u8 arg_sz = fm->arg_size[idx];
1898 
1899 				if (arg_sz == 8 || (arg_sz != 4 && !sign))
1900 					continue;
1901 
1902 				if (idx < RV_MAX_REG_ARGS) {
1903 					if (sign_extend(RV_REG_A0 + idx, RV_REG_A0 + idx, arg_sz, sign, ctx))
1904 						return -EINVAL;
1905 				} else {
1906 					emit_ld(RV_REG_T1, (idx - RV_MAX_REG_ARGS) * 8, RV_REG_SP, ctx);
1907 					if (sign_extend(RV_REG_T1, RV_REG_T1, arg_sz, sign, ctx))
1908 						return -EINVAL;
1909 					emit_sd(RV_REG_SP, (idx - RV_MAX_REG_ARGS) * 8, RV_REG_T1, ctx);
1910 				}
1911 			}
1912 		}
1913 
1914 		/* restore TCC to RV_REG_TCC before bpf2bpf call */
1915 		if (aux->tail_call_reachable && insn->src_reg == BPF_PSEUDO_CALL)
1916 			emit_ld(RV_REG_TCC, ctx->tcc_offset, RV_REG_SP, ctx);
1917 
1918 		ret = emit_call(addr, fixed_addr, ctx);
1919 		if (ret)
1920 			return ret;
1921 
1922 		/* store updated TCC back to stack after bpf2bpf call */
1923 		if (aux->tail_call_reachable && insn->src_reg == BPF_PSEUDO_CALL)
1924 			emit_sd(RV_REG_SP, ctx->tcc_offset, RV_REG_TCC, ctx);
1925 
1926 		/*
1927 		 * arch_bpf_timed_may_goto() is emitted by the verifier and
1928 		 * returns its result in BPF_REG_AX instead of BPF_REG_0, so
1929 		 * skip the normal "move return register into R0".
1930 		 */
1931 		if (insn->src_reg != BPF_PSEUDO_CALL && addr != (u64)arch_bpf_timed_may_goto)
1932 			emit_mv(bpf_to_rv_reg(BPF_REG_0, ctx), RV_REG_A0, ctx);
1933 		break;
1934 	}
1935 
1936 	/* indirect call to a bpf subprog via the address in rd */
1937 	case BPF_JMP | BPF_CALL | BPF_X:
1938 		mark_call(ctx);
1939 		emit_jalr(RV_REG_RA, rd, 0, ctx);
1940 		break;
1941 
1942 	/* tail call */
1943 	case BPF_JMP | BPF_TAIL_CALL:
1944 		if (emit_bpf_tail_call(i, ctx))
1945 			return -1;
1946 		break;
1947 
1948 	/* function return */
1949 	case BPF_JMP | BPF_EXIT:
1950 		if (i == ctx->prog->len - 1)
1951 			break;
1952 
1953 		rvoff = epilogue_offset(ctx);
1954 		ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
1955 		if (ret)
1956 			return ret;
1957 		break;
1958 
1959 	/* dst = imm64 */
1960 	case BPF_LD | BPF_IMM | BPF_DW:
1961 	{
1962 		struct bpf_insn insn1 = insn[1];
1963 		u64 imm64;
1964 
1965 		imm64 = (u64)insn1.imm << 32 | (u32)imm;
1966 		if (bpf_pseudo_func(insn)) {
1967 			/* fixed-length insns for extra jit pass */
1968 			ret = emit_addr(rd, imm64, extra_pass, ctx);
1969 			if (ret)
1970 				return ret;
1971 		} else {
1972 			emit_imm(rd, imm64, ctx);
1973 		}
1974 
1975 		return 1;
1976 	}
1977 
1978 	/* LDX: dst = *(unsigned size *)(src + off) */
1979 	case BPF_LDX | BPF_MEM | BPF_B:
1980 	case BPF_LDX | BPF_MEM | BPF_H:
1981 	case BPF_LDX | BPF_MEM | BPF_W:
1982 	case BPF_LDX | BPF_MEM | BPF_DW:
1983 	case BPF_LDX | BPF_PROBE_MEM | BPF_B:
1984 	case BPF_LDX | BPF_PROBE_MEM | BPF_H:
1985 	case BPF_LDX | BPF_PROBE_MEM | BPF_W:
1986 	case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
1987 	/* LDSX: dst = *(signed size *)(src + off) */
1988 	case BPF_LDX | BPF_MEMSX | BPF_B:
1989 	case BPF_LDX | BPF_MEMSX | BPF_H:
1990 	case BPF_LDX | BPF_MEMSX | BPF_W:
1991 	case BPF_LDX | BPF_PROBE_MEMSX | BPF_B:
1992 	case BPF_LDX | BPF_PROBE_MEMSX | BPF_H:
1993 	case BPF_LDX | BPF_PROBE_MEMSX | BPF_W:
1994 	/* LDX | PROBE_MEM32: dst = *(unsigned size *)(src + RV_REG_ARENA + off) */
1995 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_B:
1996 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_H:
1997 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_W:
1998 	case BPF_LDX | BPF_PROBE_MEM32 | BPF_DW:
1999 	/* LDX | PROBE_MEM32SX: dst = *(signed size *)(src + RV_REG_ARENA + off) */
2000 	case BPF_LDX | BPF_PROBE_MEM32SX | BPF_B:
2001 	case BPF_LDX | BPF_PROBE_MEM32SX | BPF_H:
2002 	case BPF_LDX | BPF_PROBE_MEM32SX | BPF_W:
2003 	{
2004 		bool sign_ext;
2005 
2006 		sign_ext = BPF_MODE(insn->code) == BPF_MEMSX ||
2007 			   BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
2008 			   BPF_MODE(insn->code) == BPF_PROBE_MEM32SX;
2009 
2010 		if (BPF_MODE(insn->code) == BPF_PROBE_MEM32 ||
2011 		    BPF_MODE(insn->code) == BPF_PROBE_MEM32SX) {
2012 			emit_add(RV_REG_T2, rs, RV_REG_ARENA, ctx);
2013 			rs = RV_REG_T2;
2014 		}
2015 
2016 		if (is_stack_arg_ldx(insn))
2017 			emit_stack_arg_ldx(rd, off, ctx);
2018 		else
2019 			emit_ldx(rd, off, rs, BPF_SIZE(code), sign_ext, ctx);
2020 
2021 		ret = add_exception_handler(insn, rd, ctx);
2022 		if (ret)
2023 			return ret;
2024 
2025 		if (BPF_SIZE(code) != BPF_DW && insn_is_zext(&insn[1]))
2026 			return 1;
2027 		break;
2028 	}
2029 
2030 	/* speculation barrier */
2031 	case BPF_ST | BPF_NOSPEC:
2032 		break;
2033 
2034 	/* ST: *(size *)(dst + off) = imm */
2035 	case BPF_ST | BPF_MEM | BPF_B:
2036 	case BPF_ST | BPF_MEM | BPF_H:
2037 	case BPF_ST | BPF_MEM | BPF_W:
2038 	case BPF_ST | BPF_MEM | BPF_DW:
2039 	/* ST | PROBE_MEM32: *(size *)(dst + RV_REG_ARENA + off) = imm */
2040 	case BPF_ST | BPF_PROBE_MEM32 | BPF_B:
2041 	case BPF_ST | BPF_PROBE_MEM32 | BPF_H:
2042 	case BPF_ST | BPF_PROBE_MEM32 | BPF_W:
2043 	case BPF_ST | BPF_PROBE_MEM32 | BPF_DW:
2044 		if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
2045 			emit_add(RV_REG_T3, rd, RV_REG_ARENA, ctx);
2046 			rd = RV_REG_T3;
2047 		}
2048 
2049 		if (is_stack_arg_st(insn))
2050 			emit_stack_arg_st(off, imm, ctx);
2051 		else
2052 			emit_st(rd, off, imm, BPF_SIZE(code), ctx);
2053 
2054 		ret = add_exception_handler(insn, REG_DONT_CLEAR_MARKER, ctx);
2055 		if (ret)
2056 			return ret;
2057 		break;
2058 
2059 	/* STX: *(size *)(dst + off) = src */
2060 	case BPF_STX | BPF_MEM | BPF_B:
2061 	case BPF_STX | BPF_MEM | BPF_H:
2062 	case BPF_STX | BPF_MEM | BPF_W:
2063 	case BPF_STX | BPF_MEM | BPF_DW:
2064 	/* STX | PROBE_MEM32: *(size *)(dst + RV_REG_ARENA + off) = src */
2065 	case BPF_STX | BPF_PROBE_MEM32 | BPF_B:
2066 	case BPF_STX | BPF_PROBE_MEM32 | BPF_H:
2067 	case BPF_STX | BPF_PROBE_MEM32 | BPF_W:
2068 	case BPF_STX | BPF_PROBE_MEM32 | BPF_DW:
2069 		if (BPF_MODE(insn->code) == BPF_PROBE_MEM32) {
2070 			emit_add(RV_REG_T2, rd, RV_REG_ARENA, ctx);
2071 			rd = RV_REG_T2;
2072 		}
2073 
2074 		if (is_stack_arg_stx(insn))
2075 			emit_stack_arg_stx(off, rs, ctx);
2076 		else
2077 			emit_stx(rd, off, rs, BPF_SIZE(code), ctx);
2078 
2079 		ret = add_exception_handler(insn, REG_DONT_CLEAR_MARKER, ctx);
2080 		if (ret)
2081 			return ret;
2082 		break;
2083 
2084 	/* Atomics */
2085 	case BPF_STX | BPF_ATOMIC | BPF_B:
2086 	case BPF_STX | BPF_ATOMIC | BPF_H:
2087 	case BPF_STX | BPF_ATOMIC | BPF_W:
2088 	case BPF_STX | BPF_ATOMIC | BPF_DW:
2089 	case BPF_STX | BPF_PROBE_ATOMIC | BPF_B:
2090 	case BPF_STX | BPF_PROBE_ATOMIC | BPF_H:
2091 	case BPF_STX | BPF_PROBE_ATOMIC | BPF_W:
2092 	case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW:
2093 		if (bpf_atomic_is_load_store(insn))
2094 			ret = emit_atomic_ld_st(rd, rs, insn, ctx);
2095 		else
2096 			ret = emit_atomic_rmw(rd, rs, insn, ctx);
2097 
2098 		/* ret can be 1 (skip-zext); extable entry still needs to be added */
2099 		if (ret >= 0) {
2100 			/*
2101 			 * A load-acquire reads into dst_reg, and a read-modify-write
2102 			 * carrying BPF_FETCH reads the old value into src_reg, or into
2103 			 * r0 for a BPF_CMPXCHG. Clear that register on fault, the
2104 			 * remaining atomics have no destination register.
2105 			 */
2106 			int load_reg = bpf_atomic_load_reg(insn);
2107 
2108 			ret = add_exception_handler(insn, load_reg < 0 ?
2109 					REG_DONT_CLEAR_MARKER : regmap[load_reg],
2110 					ctx) ?: ret;
2111 		}
2112 
2113 		if (ret)
2114 			return ret;
2115 		break;
2116 
2117 	default:
2118 		pr_err("bpf-jit: unknown opcode %02x\n", code);
2119 		return -EINVAL;
2120 	}
2121 
2122 	return 0;
2123 }
2124 
2125 void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog)
2126 {
2127 	int i, stack_adjust = 0, store_offset, bpf_stack_adjust = ctx->stack_arg_sz;
2128 
2129 	bpf_stack_adjust += round_up(ctx->prog->aux->stack_depth, STACK_ALIGN);
2130 	if (bpf_stack_adjust)
2131 		mark_fp(ctx);
2132 
2133 	if (seen_reg(RV_REG_RA, ctx))
2134 		stack_adjust += 8;
2135 	stack_adjust += 8; /* RV_REG_FP */
2136 	if (seen_reg(RV_REG_S1, ctx))
2137 		stack_adjust += 8;
2138 	if (seen_reg(RV_REG_S2, ctx))
2139 		stack_adjust += 8;
2140 	if (seen_reg(RV_REG_S3, ctx))
2141 		stack_adjust += 8;
2142 	if (seen_reg(RV_REG_S4, ctx))
2143 		stack_adjust += 8;
2144 	if (seen_reg(RV_REG_S5, ctx))
2145 		stack_adjust += 8;
2146 	if (ctx->arena_vm_start)
2147 		stack_adjust += 8;
2148 	stack_adjust += 8; /* RV_REG_TCC */
2149 
2150 	stack_adjust = round_up(stack_adjust, STACK_ALIGN);
2151 	stack_adjust += bpf_stack_adjust;
2152 
2153 	store_offset = stack_adjust - 8;
2154 
2155 	/* emit kcfi type preamble immediately before the  first insn */
2156 	emit_kcfi(is_subprog ? cfi_bpf_subprog_hash : cfi_bpf_hash, ctx);
2157 
2158 	/* bpf prog starts here as kcfi skipped during prog->bpf_func setup */
2159 
2160 	/* nops reserved for auipc+jalr pair */
2161 	for (i = 0; i < RV_FENTRY_NINSNS; i++)
2162 		emit(rv_nop(), ctx);
2163 
2164 	if (!is_subprog)
2165 		emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
2166 
2167 	/* tailcall starts here, emit insn before it must be fixed */
2168 
2169 	emit_addi(RV_REG_SP, RV_REG_SP, -stack_adjust, ctx);
2170 
2171 	if (seen_reg(RV_REG_RA, ctx)) {
2172 		emit_sd(RV_REG_SP, store_offset, RV_REG_RA, ctx);
2173 		store_offset -= 8;
2174 	}
2175 	emit_sd(RV_REG_SP, store_offset, RV_REG_FP, ctx);
2176 	store_offset -= 8;
2177 	if (seen_reg(RV_REG_S1, ctx)) {
2178 		emit_sd(RV_REG_SP, store_offset, RV_REG_S1, ctx);
2179 		store_offset -= 8;
2180 	}
2181 	if (seen_reg(RV_REG_S2, ctx)) {
2182 		emit_sd(RV_REG_SP, store_offset, RV_REG_S2, ctx);
2183 		store_offset -= 8;
2184 	}
2185 	if (seen_reg(RV_REG_S3, ctx)) {
2186 		emit_sd(RV_REG_SP, store_offset, RV_REG_S3, ctx);
2187 		store_offset -= 8;
2188 	}
2189 	if (seen_reg(RV_REG_S4, ctx)) {
2190 		emit_sd(RV_REG_SP, store_offset, RV_REG_S4, ctx);
2191 		store_offset -= 8;
2192 	}
2193 	if (seen_reg(RV_REG_S5, ctx)) {
2194 		emit_sd(RV_REG_SP, store_offset, RV_REG_S5, ctx);
2195 		store_offset -= 8;
2196 	}
2197 	if (ctx->arena_vm_start) {
2198 		emit_sd(RV_REG_SP, store_offset, RV_REG_ARENA, ctx);
2199 		store_offset -= 8;
2200 	}
2201 
2202 	/* store TCC from RV_REG_TCC to stack */
2203 	emit_sd(RV_REG_SP, store_offset, RV_REG_TCC, ctx);
2204 	ctx->tcc_offset = store_offset;
2205 
2206 	emit_addi(RV_REG_FP, RV_REG_SP, stack_adjust, ctx);
2207 
2208 	if (bpf_stack_adjust)
2209 		emit_addi(RV_REG_S5, RV_REG_SP, bpf_stack_adjust, ctx);
2210 
2211 	ctx->stack_size = stack_adjust;
2212 
2213 	if (ctx->arena_vm_start)
2214 		emit_imm(RV_REG_ARENA, ctx->arena_vm_start, ctx);
2215 }
2216 
2217 void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
2218 {
2219 	__build_epilogue(false, ctx);
2220 }
2221 
2222 bool bpf_jit_supports_kfunc_call(void)
2223 {
2224 	return true;
2225 }
2226 
2227 bool bpf_jit_supports_kfunc_ret_reg_pair(void)
2228 {
2229 	return true;
2230 }
2231 
2232 bool bpf_jit_supports_ptr_xchg(void)
2233 {
2234 	return true;
2235 }
2236 
2237 bool bpf_jit_supports_arena(void)
2238 {
2239 	/*
2240 	 * The arena range tree uses kmalloc_nolock(), which needs
2241 	 * cmpxchg128, provided by ZACAS on riscv.
2242 	 */
2243 #ifdef system_has_cmpxchg128
2244 	return system_has_cmpxchg128();
2245 #else
2246 	return false;
2247 #endif
2248 }
2249 
2250 bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena)
2251 {
2252 	if (in_arena) {
2253 		switch (insn->code) {
2254 		case BPF_STX | BPF_ATOMIC | BPF_W:
2255 		case BPF_STX | BPF_ATOMIC | BPF_DW:
2256 			if (insn->imm == BPF_CMPXCHG)
2257 				return rv_ext_enabled(ZACAS);
2258 			break;
2259 		}
2260 	}
2261 
2262 	return true;
2263 }
2264 
2265 bool bpf_jit_supports_percpu_insn(void)
2266 {
2267 	return true;
2268 }
2269 
2270 bool bpf_jit_inlines_helper_call(s32 imm)
2271 {
2272 	switch (imm) {
2273 	case BPF_FUNC_get_smp_processor_id:
2274 	case BPF_FUNC_get_current_task:
2275 	case BPF_FUNC_get_current_task_btf:
2276 		return true;
2277 	default:
2278 		return false;
2279 	}
2280 }
2281 
2282 bool bpf_jit_supports_fsession(void)
2283 {
2284 	return true;
2285 }
2286 
2287 bool bpf_jit_supports_subprog_tailcalls(void)
2288 {
2289 	return true;
2290 }
2291 
2292 bool bpf_jit_supports_timed_may_goto(void)
2293 {
2294 	return true;
2295 }
2296 
2297 bool bpf_jit_supports_stack_args(void)
2298 {
2299 	return true;
2300 }
2301 
2302 bool bpf_jit_supports_callx(void)
2303 {
2304 	return true;
2305 }
2306