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