xref: /linux/arch/riscv/net/bpf_jit.h (revision 53597deca0e38c30e6cd4ba2114fa42d2bcd85bb)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Common functionality for RV32 and RV64 BPF JIT compilers
4  *
5  * Copyright (c) 2019 Björn Töpel <bjorn.topel@gmail.com>
6  *
7  */
8 
9 #ifndef _BPF_JIT_H
10 #define _BPF_JIT_H
11 
12 #include <linux/bpf.h>
13 #include <linux/filter.h>
14 
15 /* verify runtime detection extension status */
16 #define rv_ext_enabled(ext) \
17 	(IS_ENABLED(CONFIG_RISCV_ISA_##ext) && riscv_has_extension_likely(RISCV_ISA_EXT_##ext))
18 
19 static inline bool rvc_enabled(void)
20 {
21 	return IS_ENABLED(CONFIG_RISCV_ISA_C);
22 }
23 
24 enum {
25 	RV_REG_ZERO =	0,	/* The constant value 0 */
26 	RV_REG_RA =	1,	/* Return address */
27 	RV_REG_SP =	2,	/* Stack pointer */
28 	RV_REG_GP =	3,	/* Global pointer */
29 	RV_REG_TP =	4,	/* Thread pointer */
30 	RV_REG_T0 =	5,	/* Temporaries */
31 	RV_REG_T1 =	6,
32 	RV_REG_T2 =	7,
33 	RV_REG_FP =	8,	/* Saved register/frame pointer */
34 	RV_REG_S1 =	9,	/* Saved register */
35 	RV_REG_A0 =	10,	/* Function argument/return values */
36 	RV_REG_A1 =	11,	/* Function arguments */
37 	RV_REG_A2 =	12,
38 	RV_REG_A3 =	13,
39 	RV_REG_A4 =	14,
40 	RV_REG_A5 =	15,
41 	RV_REG_A6 =	16,
42 	RV_REG_A7 =	17,
43 	RV_REG_S2 =	18,	/* Saved registers */
44 	RV_REG_S3 =	19,
45 	RV_REG_S4 =	20,
46 	RV_REG_S5 =	21,
47 	RV_REG_S6 =	22,
48 	RV_REG_S7 =	23,
49 	RV_REG_S8 =	24,
50 	RV_REG_S9 =	25,
51 	RV_REG_S10 =	26,
52 	RV_REG_S11 =	27,
53 	RV_REG_T3 =	28,	/* Temporaries */
54 	RV_REG_T4 =	29,
55 	RV_REG_T5 =	30,
56 	RV_REG_T6 =	31,
57 };
58 
59 static inline bool is_creg(u8 reg)
60 {
61 	return (1 << reg) & (BIT(RV_REG_FP) |
62 			     BIT(RV_REG_S1) |
63 			     BIT(RV_REG_A0) |
64 			     BIT(RV_REG_A1) |
65 			     BIT(RV_REG_A2) |
66 			     BIT(RV_REG_A3) |
67 			     BIT(RV_REG_A4) |
68 			     BIT(RV_REG_A5));
69 }
70 
71 struct rv_jit_context {
72 	struct bpf_prog *prog;
73 	u16 *insns;		/* RV insns */
74 	u16 *ro_insns;
75 	int ninsns;
76 	int prologue_len;
77 	int epilogue_offset;
78 	int *offset;		/* BPF to RV */
79 	int nexentries;
80 	int ex_insn_off;
81 	int ex_jmp_off;
82 	unsigned long flags;
83 	int stack_size;
84 	u64 arena_vm_start;
85 	u64 user_vm_start;
86 };
87 
88 /* Convert from ninsns to bytes. */
89 static inline int ninsns_rvoff(int ninsns)
90 {
91 	return ninsns << 1;
92 }
93 
94 struct rv_jit_data {
95 	struct bpf_binary_header *header;
96 	struct bpf_binary_header *ro_header;
97 	u8 *image;
98 	u8 *ro_image;
99 	struct rv_jit_context ctx;
100 };
101 
102 static inline void bpf_fill_ill_insns(void *area, unsigned int size)
103 {
104 	memset(area, 0, size);
105 }
106 
107 /* Emit a 4-byte riscv instruction. */
108 static inline void emit(const u32 insn, struct rv_jit_context *ctx)
109 {
110 	if (ctx->insns) {
111 		ctx->insns[ctx->ninsns] = insn;
112 		ctx->insns[ctx->ninsns + 1] = (insn >> 16);
113 	}
114 
115 	ctx->ninsns += 2;
116 }
117 
118 /* Emit a 2-byte riscv compressed instruction. */
119 static inline void emitc(const u16 insn, struct rv_jit_context *ctx)
120 {
121 	BUILD_BUG_ON(!rvc_enabled());
122 
123 	if (ctx->insns)
124 		ctx->insns[ctx->ninsns] = insn;
125 
126 	ctx->ninsns++;
127 }
128 
129 static inline int epilogue_offset(struct rv_jit_context *ctx)
130 {
131 	int to = ctx->epilogue_offset, from = ctx->ninsns;
132 
133 	return ninsns_rvoff(to - from);
134 }
135 
136 /* Return -1 or inverted cond. */
137 static inline int invert_bpf_cond(u8 cond)
138 {
139 	switch (cond) {
140 	case BPF_JEQ:
141 		return BPF_JNE;
142 	case BPF_JGT:
143 		return BPF_JLE;
144 	case BPF_JLT:
145 		return BPF_JGE;
146 	case BPF_JGE:
147 		return BPF_JLT;
148 	case BPF_JLE:
149 		return BPF_JGT;
150 	case BPF_JNE:
151 		return BPF_JEQ;
152 	case BPF_JSGT:
153 		return BPF_JSLE;
154 	case BPF_JSLT:
155 		return BPF_JSGE;
156 	case BPF_JSGE:
157 		return BPF_JSLT;
158 	case BPF_JSLE:
159 		return BPF_JSGT;
160 	}
161 	return -1;
162 }
163 
164 static inline bool is_6b_int(long val)
165 {
166 	return -(1L << 5) <= val && val < (1L << 5);
167 }
168 
169 static inline bool is_7b_uint(unsigned long val)
170 {
171 	return val < (1UL << 7);
172 }
173 
174 static inline bool is_8b_uint(unsigned long val)
175 {
176 	return val < (1UL << 8);
177 }
178 
179 static inline bool is_9b_uint(unsigned long val)
180 {
181 	return val < (1UL << 9);
182 }
183 
184 static inline bool is_10b_int(long val)
185 {
186 	return -(1L << 9) <= val && val < (1L << 9);
187 }
188 
189 static inline bool is_10b_uint(unsigned long val)
190 {
191 	return val < (1UL << 10);
192 }
193 
194 static inline bool is_12b_int(long val)
195 {
196 	return -(1L << 11) <= val && val < (1L << 11);
197 }
198 
199 static inline int is_12b_check(int off, int insn)
200 {
201 	if (!is_12b_int(off)) {
202 		pr_err("bpf-jit: insn=%d 12b < offset=%d not supported yet!\n",
203 		       insn, (int)off);
204 		return -1;
205 	}
206 	return 0;
207 }
208 
209 static inline bool is_13b_int(long val)
210 {
211 	return -(1L << 12) <= val && val < (1L << 12);
212 }
213 
214 static inline bool is_21b_int(long val)
215 {
216 	return -(1L << 20) <= val && val < (1L << 20);
217 }
218 
219 static inline int rv_offset(int insn, int off, struct rv_jit_context *ctx)
220 {
221 	int from, to;
222 
223 	off++; /* BPF branch is from PC+1, RV is from PC */
224 	from = (insn > 0) ? ctx->offset[insn - 1] : ctx->prologue_len;
225 	to = (insn + off > 0) ? ctx->offset[insn + off - 1] : ctx->prologue_len;
226 	return ninsns_rvoff(to - from);
227 }
228 
229 /* Instruction formats. */
230 
231 static inline u32 rv_r_insn(u8 funct7, u8 rs2, u8 rs1, u8 funct3, u8 rd,
232 			    u8 opcode)
233 {
234 	return (funct7 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
235 		(rd << 7) | opcode;
236 }
237 
238 static inline u32 rv_i_insn(u16 imm11_0, u8 rs1, u8 funct3, u8 rd, u8 opcode)
239 {
240 	return (imm11_0 << 20) | (rs1 << 15) | (funct3 << 12) | (rd << 7) |
241 		opcode;
242 }
243 
244 static inline u32 rv_s_insn(u16 imm11_0, u8 rs2, u8 rs1, u8 funct3, u8 opcode)
245 {
246 	u8 imm11_5 = imm11_0 >> 5, imm4_0 = imm11_0 & 0x1f;
247 
248 	return (imm11_5 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
249 		(imm4_0 << 7) | opcode;
250 }
251 
252 static inline u32 rv_b_insn(u16 imm12_1, u8 rs2, u8 rs1, u8 funct3, u8 opcode)
253 {
254 	u8 imm12 = ((imm12_1 & 0x800) >> 5) | ((imm12_1 & 0x3f0) >> 4);
255 	u8 imm4_1 = ((imm12_1 & 0xf) << 1) | ((imm12_1 & 0x400) >> 10);
256 
257 	return (imm12 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
258 		(imm4_1 << 7) | opcode;
259 }
260 
261 static inline u32 rv_u_insn(u32 imm31_12, u8 rd, u8 opcode)
262 {
263 	return (imm31_12 << 12) | (rd << 7) | opcode;
264 }
265 
266 static inline u32 rv_j_insn(u32 imm20_1, u8 rd, u8 opcode)
267 {
268 	u32 imm;
269 
270 	imm = (imm20_1 & 0x80000) | ((imm20_1 & 0x3ff) << 9) |
271 		((imm20_1 & 0x400) >> 2) | ((imm20_1 & 0x7f800) >> 11);
272 
273 	return (imm << 12) | (rd << 7) | opcode;
274 }
275 
276 static inline u32 rv_amo_insn(u8 funct5, u8 aq, u8 rl, u8 rs2, u8 rs1,
277 			      u8 funct3, u8 rd, u8 opcode)
278 {
279 	u8 funct7 = (funct5 << 2) | (aq << 1) | rl;
280 
281 	return rv_r_insn(funct7, rs2, rs1, funct3, rd, opcode);
282 }
283 
284 /* RISC-V compressed instruction formats. */
285 
286 static inline u16 rv_cr_insn(u8 funct4, u8 rd, u8 rs2, u8 op)
287 {
288 	return (funct4 << 12) | (rd << 7) | (rs2 << 2) | op;
289 }
290 
291 static inline u16 rv_ci_insn(u8 funct3, u32 imm6, u8 rd, u8 op)
292 {
293 	u32 imm;
294 
295 	imm = ((imm6 & 0x20) << 7) | ((imm6 & 0x1f) << 2);
296 	return (funct3 << 13) | (rd << 7) | op | imm;
297 }
298 
299 static inline u16 rv_css_insn(u8 funct3, u32 uimm, u8 rs2, u8 op)
300 {
301 	return (funct3 << 13) | (uimm << 7) | (rs2 << 2) | op;
302 }
303 
304 static inline u16 rv_ciw_insn(u8 funct3, u32 uimm, u8 rd, u8 op)
305 {
306 	return (funct3 << 13) | (uimm << 5) | ((rd & 0x7) << 2) | op;
307 }
308 
309 static inline u16 rv_cl_insn(u8 funct3, u32 imm_hi, u8 rs1, u32 imm_lo, u8 rd,
310 			     u8 op)
311 {
312 	return (funct3 << 13) | (imm_hi << 10) | ((rs1 & 0x7) << 7) |
313 		(imm_lo << 5) | ((rd & 0x7) << 2) | op;
314 }
315 
316 static inline u16 rv_cs_insn(u8 funct3, u32 imm_hi, u8 rs1, u32 imm_lo, u8 rs2,
317 			     u8 op)
318 {
319 	return (funct3 << 13) | (imm_hi << 10) | ((rs1 & 0x7) << 7) |
320 		(imm_lo << 5) | ((rs2 & 0x7) << 2) | op;
321 }
322 
323 static inline u16 rv_ca_insn(u8 funct6, u8 rd, u8 funct2, u8 rs2, u8 op)
324 {
325 	return (funct6 << 10) | ((rd & 0x7) << 7) | (funct2 << 5) |
326 		((rs2 & 0x7) << 2) | op;
327 }
328 
329 static inline u16 rv_cb_insn(u8 funct3, u32 imm6, u8 funct2, u8 rd, u8 op)
330 {
331 	u32 imm;
332 
333 	imm = ((imm6 & 0x20) << 7) | ((imm6 & 0x1f) << 2);
334 	return (funct3 << 13) | (funct2 << 10) | ((rd & 0x7) << 7) | op | imm;
335 }
336 
337 /* Instructions shared by both RV32 and RV64. */
338 
339 static inline u32 rv_addi(u8 rd, u8 rs1, u16 imm11_0)
340 {
341 	return rv_i_insn(imm11_0, rs1, 0, rd, 0x13);
342 }
343 
344 static inline u32 rv_andi(u8 rd, u8 rs1, u16 imm11_0)
345 {
346 	return rv_i_insn(imm11_0, rs1, 7, rd, 0x13);
347 }
348 
349 static inline u32 rv_ori(u8 rd, u8 rs1, u16 imm11_0)
350 {
351 	return rv_i_insn(imm11_0, rs1, 6, rd, 0x13);
352 }
353 
354 static inline u32 rv_xori(u8 rd, u8 rs1, u16 imm11_0)
355 {
356 	return rv_i_insn(imm11_0, rs1, 4, rd, 0x13);
357 }
358 
359 static inline u32 rv_slli(u8 rd, u8 rs1, u16 imm11_0)
360 {
361 	return rv_i_insn(imm11_0, rs1, 1, rd, 0x13);
362 }
363 
364 static inline u32 rv_srli(u8 rd, u8 rs1, u16 imm11_0)
365 {
366 	return rv_i_insn(imm11_0, rs1, 5, rd, 0x13);
367 }
368 
369 static inline u32 rv_srai(u8 rd, u8 rs1, u16 imm11_0)
370 {
371 	return rv_i_insn(0x400 | imm11_0, rs1, 5, rd, 0x13);
372 }
373 
374 static inline u32 rv_lui(u8 rd, u32 imm31_12)
375 {
376 	return rv_u_insn(imm31_12, rd, 0x37);
377 }
378 
379 static inline u32 rv_auipc(u8 rd, u32 imm31_12)
380 {
381 	return rv_u_insn(imm31_12, rd, 0x17);
382 }
383 
384 static inline u32 rv_add(u8 rd, u8 rs1, u8 rs2)
385 {
386 	return rv_r_insn(0, rs2, rs1, 0, rd, 0x33);
387 }
388 
389 static inline u32 rv_sub(u8 rd, u8 rs1, u8 rs2)
390 {
391 	return rv_r_insn(0x20, rs2, rs1, 0, rd, 0x33);
392 }
393 
394 static inline u32 rv_sltu(u8 rd, u8 rs1, u8 rs2)
395 {
396 	return rv_r_insn(0, rs2, rs1, 3, rd, 0x33);
397 }
398 
399 static inline u32 rv_and(u8 rd, u8 rs1, u8 rs2)
400 {
401 	return rv_r_insn(0, rs2, rs1, 7, rd, 0x33);
402 }
403 
404 static inline u32 rv_or(u8 rd, u8 rs1, u8 rs2)
405 {
406 	return rv_r_insn(0, rs2, rs1, 6, rd, 0x33);
407 }
408 
409 static inline u32 rv_xor(u8 rd, u8 rs1, u8 rs2)
410 {
411 	return rv_r_insn(0, rs2, rs1, 4, rd, 0x33);
412 }
413 
414 static inline u32 rv_sll(u8 rd, u8 rs1, u8 rs2)
415 {
416 	return rv_r_insn(0, rs2, rs1, 1, rd, 0x33);
417 }
418 
419 static inline u32 rv_srl(u8 rd, u8 rs1, u8 rs2)
420 {
421 	return rv_r_insn(0, rs2, rs1, 5, rd, 0x33);
422 }
423 
424 static inline u32 rv_sra(u8 rd, u8 rs1, u8 rs2)
425 {
426 	return rv_r_insn(0x20, rs2, rs1, 5, rd, 0x33);
427 }
428 
429 static inline u32 rv_mul(u8 rd, u8 rs1, u8 rs2)
430 {
431 	return rv_r_insn(1, rs2, rs1, 0, rd, 0x33);
432 }
433 
434 static inline u32 rv_mulhu(u8 rd, u8 rs1, u8 rs2)
435 {
436 	return rv_r_insn(1, rs2, rs1, 3, rd, 0x33);
437 }
438 
439 static inline u32 rv_div(u8 rd, u8 rs1, u8 rs2)
440 {
441 	return rv_r_insn(1, rs2, rs1, 4, rd, 0x33);
442 }
443 
444 static inline u32 rv_divu(u8 rd, u8 rs1, u8 rs2)
445 {
446 	return rv_r_insn(1, rs2, rs1, 5, rd, 0x33);
447 }
448 
449 static inline u32 rv_rem(u8 rd, u8 rs1, u8 rs2)
450 {
451 	return rv_r_insn(1, rs2, rs1, 6, rd, 0x33);
452 }
453 
454 static inline u32 rv_remu(u8 rd, u8 rs1, u8 rs2)
455 {
456 	return rv_r_insn(1, rs2, rs1, 7, rd, 0x33);
457 }
458 
459 static inline u32 rv_jal(u8 rd, u32 imm20_1)
460 {
461 	return rv_j_insn(imm20_1, rd, 0x6f);
462 }
463 
464 static inline u32 rv_jalr(u8 rd, u8 rs1, u16 imm11_0)
465 {
466 	return rv_i_insn(imm11_0, rs1, 0, rd, 0x67);
467 }
468 
469 static inline u32 rv_beq(u8 rs1, u8 rs2, u16 imm12_1)
470 {
471 	return rv_b_insn(imm12_1, rs2, rs1, 0, 0x63);
472 }
473 
474 static inline u32 rv_bne(u8 rs1, u8 rs2, u16 imm12_1)
475 {
476 	return rv_b_insn(imm12_1, rs2, rs1, 1, 0x63);
477 }
478 
479 static inline u32 rv_bltu(u8 rs1, u8 rs2, u16 imm12_1)
480 {
481 	return rv_b_insn(imm12_1, rs2, rs1, 6, 0x63);
482 }
483 
484 static inline u32 rv_bgtu(u8 rs1, u8 rs2, u16 imm12_1)
485 {
486 	return rv_bltu(rs2, rs1, imm12_1);
487 }
488 
489 static inline u32 rv_bgeu(u8 rs1, u8 rs2, u16 imm12_1)
490 {
491 	return rv_b_insn(imm12_1, rs2, rs1, 7, 0x63);
492 }
493 
494 static inline u32 rv_bleu(u8 rs1, u8 rs2, u16 imm12_1)
495 {
496 	return rv_bgeu(rs2, rs1, imm12_1);
497 }
498 
499 static inline u32 rv_blt(u8 rs1, u8 rs2, u16 imm12_1)
500 {
501 	return rv_b_insn(imm12_1, rs2, rs1, 4, 0x63);
502 }
503 
504 static inline u32 rv_bgt(u8 rs1, u8 rs2, u16 imm12_1)
505 {
506 	return rv_blt(rs2, rs1, imm12_1);
507 }
508 
509 static inline u32 rv_bge(u8 rs1, u8 rs2, u16 imm12_1)
510 {
511 	return rv_b_insn(imm12_1, rs2, rs1, 5, 0x63);
512 }
513 
514 static inline u32 rv_ble(u8 rs1, u8 rs2, u16 imm12_1)
515 {
516 	return rv_bge(rs2, rs1, imm12_1);
517 }
518 
519 static inline u32 rv_lb(u8 rd, u16 imm11_0, u8 rs1)
520 {
521 	return rv_i_insn(imm11_0, rs1, 0, rd, 0x03);
522 }
523 
524 static inline u32 rv_lh(u8 rd, u16 imm11_0, u8 rs1)
525 {
526 	return rv_i_insn(imm11_0, rs1, 1, rd, 0x03);
527 }
528 
529 static inline u32 rv_lw(u8 rd, u16 imm11_0, u8 rs1)
530 {
531 	return rv_i_insn(imm11_0, rs1, 2, rd, 0x03);
532 }
533 
534 static inline u32 rv_lbu(u8 rd, u16 imm11_0, u8 rs1)
535 {
536 	return rv_i_insn(imm11_0, rs1, 4, rd, 0x03);
537 }
538 
539 static inline u32 rv_lhu(u8 rd, u16 imm11_0, u8 rs1)
540 {
541 	return rv_i_insn(imm11_0, rs1, 5, rd, 0x03);
542 }
543 
544 static inline u32 rv_sb(u8 rs1, u16 imm11_0, u8 rs2)
545 {
546 	return rv_s_insn(imm11_0, rs2, rs1, 0, 0x23);
547 }
548 
549 static inline u32 rv_sh(u8 rs1, u16 imm11_0, u8 rs2)
550 {
551 	return rv_s_insn(imm11_0, rs2, rs1, 1, 0x23);
552 }
553 
554 static inline u32 rv_sw(u8 rs1, u16 imm11_0, u8 rs2)
555 {
556 	return rv_s_insn(imm11_0, rs2, rs1, 2, 0x23);
557 }
558 
559 static inline u32 rv_amoadd_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
560 {
561 	return rv_amo_insn(0, aq, rl, rs2, rs1, 2, rd, 0x2f);
562 }
563 
564 static inline u32 rv_amoand_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
565 {
566 	return rv_amo_insn(0xc, aq, rl, rs2, rs1, 2, rd, 0x2f);
567 }
568 
569 static inline u32 rv_amoor_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
570 {
571 	return rv_amo_insn(0x8, aq, rl, rs2, rs1, 2, rd, 0x2f);
572 }
573 
574 static inline u32 rv_amoxor_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
575 {
576 	return rv_amo_insn(0x4, aq, rl, rs2, rs1, 2, rd, 0x2f);
577 }
578 
579 static inline u32 rv_amoswap_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
580 {
581 	return rv_amo_insn(0x1, aq, rl, rs2, rs1, 2, rd, 0x2f);
582 }
583 
584 static inline u32 rv_lr_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
585 {
586 	return rv_amo_insn(0x2, aq, rl, rs2, rs1, 2, rd, 0x2f);
587 }
588 
589 static inline u32 rv_sc_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
590 {
591 	return rv_amo_insn(0x3, aq, rl, rs2, rs1, 2, rd, 0x2f);
592 }
593 
594 static inline u32 rv_fence(u8 pred, u8 succ)
595 {
596 	u16 imm11_0 = pred << 4 | succ;
597 
598 	return rv_i_insn(imm11_0, 0, 0, 0, 0xf);
599 }
600 
601 static inline void emit_fence_r_rw(struct rv_jit_context *ctx)
602 {
603 	emit(rv_fence(0x2, 0x3), ctx);
604 }
605 
606 static inline void emit_fence_rw_w(struct rv_jit_context *ctx)
607 {
608 	emit(rv_fence(0x3, 0x1), ctx);
609 }
610 
611 static inline void emit_fence_rw_rw(struct rv_jit_context *ctx)
612 {
613 	emit(rv_fence(0x3, 0x3), ctx);
614 }
615 
616 static inline u32 rv_nop(void)
617 {
618 	return rv_i_insn(0, 0, 0, 0, 0x13);
619 }
620 
621 /* RVC instructions. */
622 
623 static inline u16 rvc_addi4spn(u8 rd, u32 imm10)
624 {
625 	u32 imm;
626 
627 	imm = ((imm10 & 0x30) << 2) | ((imm10 & 0x3c0) >> 4) |
628 		((imm10 & 0x4) >> 1) | ((imm10 & 0x8) >> 3);
629 	return rv_ciw_insn(0x0, imm, rd, 0x0);
630 }
631 
632 static inline u16 rvc_lw(u8 rd, u32 imm7, u8 rs1)
633 {
634 	u32 imm_hi, imm_lo;
635 
636 	imm_hi = (imm7 & 0x38) >> 3;
637 	imm_lo = ((imm7 & 0x4) >> 1) | ((imm7 & 0x40) >> 6);
638 	return rv_cl_insn(0x2, imm_hi, rs1, imm_lo, rd, 0x0);
639 }
640 
641 static inline u16 rvc_sw(u8 rs1, u32 imm7, u8 rs2)
642 {
643 	u32 imm_hi, imm_lo;
644 
645 	imm_hi = (imm7 & 0x38) >> 3;
646 	imm_lo = ((imm7 & 0x4) >> 1) | ((imm7 & 0x40) >> 6);
647 	return rv_cs_insn(0x6, imm_hi, rs1, imm_lo, rs2, 0x0);
648 }
649 
650 static inline u16 rvc_addi(u8 rd, u32 imm6)
651 {
652 	return rv_ci_insn(0, imm6, rd, 0x1);
653 }
654 
655 static inline u16 rvc_li(u8 rd, u32 imm6)
656 {
657 	return rv_ci_insn(0x2, imm6, rd, 0x1);
658 }
659 
660 static inline u16 rvc_addi16sp(u32 imm10)
661 {
662 	u32 imm;
663 
664 	imm = ((imm10 & 0x200) >> 4) | (imm10 & 0x10) | ((imm10 & 0x40) >> 3) |
665 		((imm10 & 0x180) >> 6) | ((imm10 & 0x20) >> 5);
666 	return rv_ci_insn(0x3, imm, RV_REG_SP, 0x1);
667 }
668 
669 static inline u16 rvc_lui(u8 rd, u32 imm6)
670 {
671 	return rv_ci_insn(0x3, imm6, rd, 0x1);
672 }
673 
674 static inline u16 rvc_srli(u8 rd, u32 imm6)
675 {
676 	return rv_cb_insn(0x4, imm6, 0, rd, 0x1);
677 }
678 
679 static inline u16 rvc_srai(u8 rd, u32 imm6)
680 {
681 	return rv_cb_insn(0x4, imm6, 0x1, rd, 0x1);
682 }
683 
684 static inline u16 rvc_andi(u8 rd, u32 imm6)
685 {
686 	return rv_cb_insn(0x4, imm6, 0x2, rd, 0x1);
687 }
688 
689 static inline u16 rvc_sub(u8 rd, u8 rs)
690 {
691 	return rv_ca_insn(0x23, rd, 0, rs, 0x1);
692 }
693 
694 static inline u16 rvc_xor(u8 rd, u8 rs)
695 {
696 	return rv_ca_insn(0x23, rd, 0x1, rs, 0x1);
697 }
698 
699 static inline u16 rvc_or(u8 rd, u8 rs)
700 {
701 	return rv_ca_insn(0x23, rd, 0x2, rs, 0x1);
702 }
703 
704 static inline u16 rvc_and(u8 rd, u8 rs)
705 {
706 	return rv_ca_insn(0x23, rd, 0x3, rs, 0x1);
707 }
708 
709 static inline u16 rvc_slli(u8 rd, u32 imm6)
710 {
711 	return rv_ci_insn(0, imm6, rd, 0x2);
712 }
713 
714 static inline u16 rvc_lwsp(u8 rd, u32 imm8)
715 {
716 	u32 imm;
717 
718 	imm = ((imm8 & 0xc0) >> 6) | (imm8 & 0x3c);
719 	return rv_ci_insn(0x2, imm, rd, 0x2);
720 }
721 
722 static inline u16 rvc_jr(u8 rs1)
723 {
724 	return rv_cr_insn(0x8, rs1, RV_REG_ZERO, 0x2);
725 }
726 
727 static inline u16 rvc_mv(u8 rd, u8 rs)
728 {
729 	return rv_cr_insn(0x8, rd, rs, 0x2);
730 }
731 
732 static inline u16 rvc_jalr(u8 rs1)
733 {
734 	return rv_cr_insn(0x9, rs1, RV_REG_ZERO, 0x2);
735 }
736 
737 static inline u16 rvc_add(u8 rd, u8 rs)
738 {
739 	return rv_cr_insn(0x9, rd, rs, 0x2);
740 }
741 
742 static inline u16 rvc_swsp(u32 imm8, u8 rs2)
743 {
744 	u32 imm;
745 
746 	imm = (imm8 & 0x3c) | ((imm8 & 0xc0) >> 6);
747 	return rv_css_insn(0x6, imm, rs2, 0x2);
748 }
749 
750 /* RVZACAS instructions. */
751 static inline u32 rvzacas_amocas_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
752 {
753 	return rv_amo_insn(0x5, aq, rl, rs2, rs1, 2, rd, 0x2f);
754 }
755 
756 static inline u32 rvzacas_amocas_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
757 {
758 	return rv_amo_insn(0x5, aq, rl, rs2, rs1, 3, rd, 0x2f);
759 }
760 
761 /* RVZBA instructions. */
762 static inline u32 rvzba_sh2add(u8 rd, u8 rs1, u8 rs2)
763 {
764 	return rv_r_insn(0x10, rs2, rs1, 0x4, rd, 0x33);
765 }
766 
767 static inline u32 rvzba_sh3add(u8 rd, u8 rs1, u8 rs2)
768 {
769 	return rv_r_insn(0x10, rs2, rs1, 0x6, rd, 0x33);
770 }
771 
772 /* RVZBB instructions. */
773 static inline u32 rvzbb_sextb(u8 rd, u8 rs1)
774 {
775 	return rv_i_insn(0x604, rs1, 1, rd, 0x13);
776 }
777 
778 static inline u32 rvzbb_sexth(u8 rd, u8 rs1)
779 {
780 	return rv_i_insn(0x605, rs1, 1, rd, 0x13);
781 }
782 
783 static inline u32 rvzbb_zexth(u8 rd, u8 rs)
784 {
785 	if (IS_ENABLED(CONFIG_64BIT))
786 		return rv_i_insn(0x80, rs, 4, rd, 0x3b);
787 
788 	return rv_i_insn(0x80, rs, 4, rd, 0x33);
789 }
790 
791 static inline u32 rvzbb_rev8(u8 rd, u8 rs)
792 {
793 	if (IS_ENABLED(CONFIG_64BIT))
794 		return rv_i_insn(0x6b8, rs, 5, rd, 0x13);
795 
796 	return rv_i_insn(0x698, rs, 5, rd, 0x13);
797 }
798 
799 /*
800  * RV64-only instructions.
801  *
802  * These instructions are not available on RV32.  Wrap them below a #if to
803  * ensure that the RV32 JIT doesn't emit any of these instructions.
804  */
805 
806 #if __riscv_xlen == 64
807 
808 static inline u32 rv_addiw(u8 rd, u8 rs1, u16 imm11_0)
809 {
810 	return rv_i_insn(imm11_0, rs1, 0, rd, 0x1b);
811 }
812 
813 static inline u32 rv_slliw(u8 rd, u8 rs1, u16 imm11_0)
814 {
815 	return rv_i_insn(imm11_0, rs1, 1, rd, 0x1b);
816 }
817 
818 static inline u32 rv_srliw(u8 rd, u8 rs1, u16 imm11_0)
819 {
820 	return rv_i_insn(imm11_0, rs1, 5, rd, 0x1b);
821 }
822 
823 static inline u32 rv_sraiw(u8 rd, u8 rs1, u16 imm11_0)
824 {
825 	return rv_i_insn(0x400 | imm11_0, rs1, 5, rd, 0x1b);
826 }
827 
828 static inline u32 rv_addw(u8 rd, u8 rs1, u8 rs2)
829 {
830 	return rv_r_insn(0, rs2, rs1, 0, rd, 0x3b);
831 }
832 
833 static inline u32 rv_subw(u8 rd, u8 rs1, u8 rs2)
834 {
835 	return rv_r_insn(0x20, rs2, rs1, 0, rd, 0x3b);
836 }
837 
838 static inline u32 rv_sllw(u8 rd, u8 rs1, u8 rs2)
839 {
840 	return rv_r_insn(0, rs2, rs1, 1, rd, 0x3b);
841 }
842 
843 static inline u32 rv_srlw(u8 rd, u8 rs1, u8 rs2)
844 {
845 	return rv_r_insn(0, rs2, rs1, 5, rd, 0x3b);
846 }
847 
848 static inline u32 rv_sraw(u8 rd, u8 rs1, u8 rs2)
849 {
850 	return rv_r_insn(0x20, rs2, rs1, 5, rd, 0x3b);
851 }
852 
853 static inline u32 rv_mulw(u8 rd, u8 rs1, u8 rs2)
854 {
855 	return rv_r_insn(1, rs2, rs1, 0, rd, 0x3b);
856 }
857 
858 static inline u32 rv_divw(u8 rd, u8 rs1, u8 rs2)
859 {
860 	return rv_r_insn(1, rs2, rs1, 4, rd, 0x3b);
861 }
862 
863 static inline u32 rv_divuw(u8 rd, u8 rs1, u8 rs2)
864 {
865 	return rv_r_insn(1, rs2, rs1, 5, rd, 0x3b);
866 }
867 
868 static inline u32 rv_remw(u8 rd, u8 rs1, u8 rs2)
869 {
870 	return rv_r_insn(1, rs2, rs1, 6, rd, 0x3b);
871 }
872 
873 static inline u32 rv_remuw(u8 rd, u8 rs1, u8 rs2)
874 {
875 	return rv_r_insn(1, rs2, rs1, 7, rd, 0x3b);
876 }
877 
878 static inline u32 rv_ld(u8 rd, u16 imm11_0, u8 rs1)
879 {
880 	return rv_i_insn(imm11_0, rs1, 3, rd, 0x03);
881 }
882 
883 static inline u32 rv_lwu(u8 rd, u16 imm11_0, u8 rs1)
884 {
885 	return rv_i_insn(imm11_0, rs1, 6, rd, 0x03);
886 }
887 
888 static inline u32 rv_sd(u8 rs1, u16 imm11_0, u8 rs2)
889 {
890 	return rv_s_insn(imm11_0, rs2, rs1, 3, 0x23);
891 }
892 
893 static inline u32 rv_amoadd_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
894 {
895 	return rv_amo_insn(0, aq, rl, rs2, rs1, 3, rd, 0x2f);
896 }
897 
898 static inline u32 rv_amoand_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
899 {
900 	return rv_amo_insn(0xc, aq, rl, rs2, rs1, 3, rd, 0x2f);
901 }
902 
903 static inline u32 rv_amoor_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
904 {
905 	return rv_amo_insn(0x8, aq, rl, rs2, rs1, 3, rd, 0x2f);
906 }
907 
908 static inline u32 rv_amoxor_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
909 {
910 	return rv_amo_insn(0x4, aq, rl, rs2, rs1, 3, rd, 0x2f);
911 }
912 
913 static inline u32 rv_amoswap_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
914 {
915 	return rv_amo_insn(0x1, aq, rl, rs2, rs1, 3, rd, 0x2f);
916 }
917 
918 static inline u32 rv_lr_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
919 {
920 	return rv_amo_insn(0x2, aq, rl, rs2, rs1, 3, rd, 0x2f);
921 }
922 
923 static inline u32 rv_sc_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
924 {
925 	return rv_amo_insn(0x3, aq, rl, rs2, rs1, 3, rd, 0x2f);
926 }
927 
928 /* RV64-only RVC instructions. */
929 
930 static inline u16 rvc_ld(u8 rd, u32 imm8, u8 rs1)
931 {
932 	u32 imm_hi, imm_lo;
933 
934 	imm_hi = (imm8 & 0x38) >> 3;
935 	imm_lo = (imm8 & 0xc0) >> 6;
936 	return rv_cl_insn(0x3, imm_hi, rs1, imm_lo, rd, 0x0);
937 }
938 
939 static inline u16 rvc_sd(u8 rs1, u32 imm8, u8 rs2)
940 {
941 	u32 imm_hi, imm_lo;
942 
943 	imm_hi = (imm8 & 0x38) >> 3;
944 	imm_lo = (imm8 & 0xc0) >> 6;
945 	return rv_cs_insn(0x7, imm_hi, rs1, imm_lo, rs2, 0x0);
946 }
947 
948 static inline u16 rvc_subw(u8 rd, u8 rs)
949 {
950 	return rv_ca_insn(0x27, rd, 0, rs, 0x1);
951 }
952 
953 static inline u16 rvc_addiw(u8 rd, u32 imm6)
954 {
955 	return rv_ci_insn(0x1, imm6, rd, 0x1);
956 }
957 
958 static inline u16 rvc_ldsp(u8 rd, u32 imm9)
959 {
960 	u32 imm;
961 
962 	imm = ((imm9 & 0x1c0) >> 6) | (imm9 & 0x38);
963 	return rv_ci_insn(0x3, imm, rd, 0x2);
964 }
965 
966 static inline u16 rvc_sdsp(u32 imm9, u8 rs2)
967 {
968 	u32 imm;
969 
970 	imm = (imm9 & 0x38) | ((imm9 & 0x1c0) >> 6);
971 	return rv_css_insn(0x7, imm, rs2, 0x2);
972 }
973 
974 /* RV64-only ZBA instructions. */
975 
976 static inline u32 rvzba_zextw(u8 rd, u8 rs1)
977 {
978 	/* add.uw rd, rs1, ZERO */
979 	return rv_r_insn(0x04, RV_REG_ZERO, rs1, 0, rd, 0x3b);
980 }
981 
982 #endif /* __riscv_xlen == 64 */
983 
984 /* Helper functions that emit RVC instructions when possible. */
985 
986 static inline void emit_jalr(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
987 {
988 	if (rvc_enabled() && rd == RV_REG_RA && rs && !imm)
989 		emitc(rvc_jalr(rs), ctx);
990 	else if (rvc_enabled() && !rd && rs && !imm)
991 		emitc(rvc_jr(rs), ctx);
992 	else
993 		emit(rv_jalr(rd, rs, imm), ctx);
994 }
995 
996 static inline void emit_mv(u8 rd, u8 rs, struct rv_jit_context *ctx)
997 {
998 	if (rvc_enabled() && rd && rs)
999 		emitc(rvc_mv(rd, rs), ctx);
1000 	else
1001 		emit(rv_addi(rd, rs, 0), ctx);
1002 }
1003 
1004 static inline void emit_add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1005 {
1006 	if (rvc_enabled() && rd && rd == rs1 && rs2)
1007 		emitc(rvc_add(rd, rs2), ctx);
1008 	else
1009 		emit(rv_add(rd, rs1, rs2), ctx);
1010 }
1011 
1012 static inline void emit_addi(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
1013 {
1014 	if (rvc_enabled() && rd == RV_REG_SP && rd == rs && is_10b_int(imm) && imm && !(imm & 0xf))
1015 		emitc(rvc_addi16sp(imm), ctx);
1016 	else if (rvc_enabled() && is_creg(rd) && rs == RV_REG_SP && is_10b_uint(imm) &&
1017 		 !(imm & 0x3) && imm)
1018 		emitc(rvc_addi4spn(rd, imm), ctx);
1019 	else if (rvc_enabled() && rd && rd == rs && imm && is_6b_int(imm))
1020 		emitc(rvc_addi(rd, imm), ctx);
1021 	else
1022 		emit(rv_addi(rd, rs, imm), ctx);
1023 }
1024 
1025 static inline void emit_li(u8 rd, s32 imm, struct rv_jit_context *ctx)
1026 {
1027 	if (rvc_enabled() && rd && is_6b_int(imm))
1028 		emitc(rvc_li(rd, imm), ctx);
1029 	else
1030 		emit(rv_addi(rd, RV_REG_ZERO, imm), ctx);
1031 }
1032 
1033 static inline void emit_lui(u8 rd, s32 imm, struct rv_jit_context *ctx)
1034 {
1035 	if (rvc_enabled() && rd && rd != RV_REG_SP && is_6b_int(imm) && imm)
1036 		emitc(rvc_lui(rd, imm), ctx);
1037 	else
1038 		emit(rv_lui(rd, imm), ctx);
1039 }
1040 
1041 static inline void emit_slli(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
1042 {
1043 	if (rvc_enabled() && rd && rd == rs && imm && (u32)imm < __riscv_xlen)
1044 		emitc(rvc_slli(rd, imm), ctx);
1045 	else
1046 		emit(rv_slli(rd, rs, imm), ctx);
1047 }
1048 
1049 static inline void emit_andi(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
1050 {
1051 	if (rvc_enabled() && is_creg(rd) && rd == rs && is_6b_int(imm))
1052 		emitc(rvc_andi(rd, imm), ctx);
1053 	else
1054 		emit(rv_andi(rd, rs, imm), ctx);
1055 }
1056 
1057 static inline void emit_srli(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
1058 {
1059 	if (rvc_enabled() && is_creg(rd) && rd == rs && imm && (u32)imm < __riscv_xlen)
1060 		emitc(rvc_srli(rd, imm), ctx);
1061 	else
1062 		emit(rv_srli(rd, rs, imm), ctx);
1063 }
1064 
1065 static inline void emit_srai(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
1066 {
1067 	if (rvc_enabled() && is_creg(rd) && rd == rs && imm && (u32)imm < __riscv_xlen)
1068 		emitc(rvc_srai(rd, imm), ctx);
1069 	else
1070 		emit(rv_srai(rd, rs, imm), ctx);
1071 }
1072 
1073 static inline void emit_sub(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1074 {
1075 	if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
1076 		emitc(rvc_sub(rd, rs2), ctx);
1077 	else
1078 		emit(rv_sub(rd, rs1, rs2), ctx);
1079 }
1080 
1081 static inline void emit_or(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1082 {
1083 	if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
1084 		emitc(rvc_or(rd, rs2), ctx);
1085 	else
1086 		emit(rv_or(rd, rs1, rs2), ctx);
1087 }
1088 
1089 static inline void emit_and(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1090 {
1091 	if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
1092 		emitc(rvc_and(rd, rs2), ctx);
1093 	else
1094 		emit(rv_and(rd, rs1, rs2), ctx);
1095 }
1096 
1097 static inline void emit_xor(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1098 {
1099 	if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
1100 		emitc(rvc_xor(rd, rs2), ctx);
1101 	else
1102 		emit(rv_xor(rd, rs1, rs2), ctx);
1103 }
1104 
1105 static inline void emit_lw(u8 rd, s32 off, u8 rs1, struct rv_jit_context *ctx)
1106 {
1107 	if (rvc_enabled() && rs1 == RV_REG_SP && rd && is_8b_uint(off) && !(off & 0x3))
1108 		emitc(rvc_lwsp(rd, off), ctx);
1109 	else if (rvc_enabled() && is_creg(rd) && is_creg(rs1) && is_7b_uint(off) && !(off & 0x3))
1110 		emitc(rvc_lw(rd, off, rs1), ctx);
1111 	else
1112 		emit(rv_lw(rd, off, rs1), ctx);
1113 }
1114 
1115 static inline void emit_sw(u8 rs1, s32 off, u8 rs2, struct rv_jit_context *ctx)
1116 {
1117 	if (rvc_enabled() && rs1 == RV_REG_SP && is_8b_uint(off) && !(off & 0x3))
1118 		emitc(rvc_swsp(off, rs2), ctx);
1119 	else if (rvc_enabled() && is_creg(rs1) && is_creg(rs2) && is_7b_uint(off) && !(off & 0x3))
1120 		emitc(rvc_sw(rs1, off, rs2), ctx);
1121 	else
1122 		emit(rv_sw(rs1, off, rs2), ctx);
1123 }
1124 
1125 static inline void emit_sh2add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1126 {
1127 	if (rv_ext_enabled(ZBA)) {
1128 		emit(rvzba_sh2add(rd, rs1, rs2), ctx);
1129 		return;
1130 	}
1131 
1132 	emit_slli(rd, rs1, 2, ctx);
1133 	emit_add(rd, rd, rs2, ctx);
1134 }
1135 
1136 static inline void emit_sh3add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1137 {
1138 	if (rv_ext_enabled(ZBA)) {
1139 		emit(rvzba_sh3add(rd, rs1, rs2), ctx);
1140 		return;
1141 	}
1142 
1143 	emit_slli(rd, rs1, 3, ctx);
1144 	emit_add(rd, rd, rs2, ctx);
1145 }
1146 
1147 /* RV64-only helper functions. */
1148 #if __riscv_xlen == 64
1149 
1150 static inline void emit_addiw(u8 rd, u8 rs, s32 imm, struct rv_jit_context *ctx)
1151 {
1152 	if (rvc_enabled() && rd && rd == rs && is_6b_int(imm))
1153 		emitc(rvc_addiw(rd, imm), ctx);
1154 	else
1155 		emit(rv_addiw(rd, rs, imm), ctx);
1156 }
1157 
1158 static inline void emit_ld(u8 rd, s32 off, u8 rs1, struct rv_jit_context *ctx)
1159 {
1160 	if (rvc_enabled() && rs1 == RV_REG_SP && rd && is_9b_uint(off) && !(off & 0x7))
1161 		emitc(rvc_ldsp(rd, off), ctx);
1162 	else if (rvc_enabled() && is_creg(rd) && is_creg(rs1) && is_8b_uint(off) && !(off & 0x7))
1163 		emitc(rvc_ld(rd, off, rs1), ctx);
1164 	else
1165 		emit(rv_ld(rd, off, rs1), ctx);
1166 }
1167 
1168 static inline void emit_sd(u8 rs1, s32 off, u8 rs2, struct rv_jit_context *ctx)
1169 {
1170 	if (rvc_enabled() && rs1 == RV_REG_SP && is_9b_uint(off) && !(off & 0x7))
1171 		emitc(rvc_sdsp(off, rs2), ctx);
1172 	else if (rvc_enabled() && is_creg(rs1) && is_creg(rs2) && is_8b_uint(off) && !(off & 0x7))
1173 		emitc(rvc_sd(rs1, off, rs2), ctx);
1174 	else
1175 		emit(rv_sd(rs1, off, rs2), ctx);
1176 }
1177 
1178 static inline void emit_subw(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
1179 {
1180 	if (rvc_enabled() && is_creg(rd) && rd == rs1 && is_creg(rs2))
1181 		emitc(rvc_subw(rd, rs2), ctx);
1182 	else
1183 		emit(rv_subw(rd, rs1, rs2), ctx);
1184 }
1185 
1186 static inline void emit_sextb(u8 rd, u8 rs, struct rv_jit_context *ctx)
1187 {
1188 	if (rv_ext_enabled(ZBB)) {
1189 		emit(rvzbb_sextb(rd, rs), ctx);
1190 		return;
1191 	}
1192 
1193 	emit_slli(rd, rs, 56, ctx);
1194 	emit_srai(rd, rd, 56, ctx);
1195 }
1196 
1197 static inline void emit_sexth(u8 rd, u8 rs, struct rv_jit_context *ctx)
1198 {
1199 	if (rv_ext_enabled(ZBB)) {
1200 		emit(rvzbb_sexth(rd, rs), ctx);
1201 		return;
1202 	}
1203 
1204 	emit_slli(rd, rs, 48, ctx);
1205 	emit_srai(rd, rd, 48, ctx);
1206 }
1207 
1208 static inline void emit_sextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
1209 {
1210 	emit_addiw(rd, rs, 0, ctx);
1211 }
1212 
1213 static inline void emit_zexth(u8 rd, u8 rs, struct rv_jit_context *ctx)
1214 {
1215 	if (rv_ext_enabled(ZBB)) {
1216 		emit(rvzbb_zexth(rd, rs), ctx);
1217 		return;
1218 	}
1219 
1220 	emit_slli(rd, rs, 48, ctx);
1221 	emit_srli(rd, rd, 48, ctx);
1222 }
1223 
1224 static inline void emit_zextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
1225 {
1226 	if (rv_ext_enabled(ZBA)) {
1227 		emit(rvzba_zextw(rd, rs), ctx);
1228 		return;
1229 	}
1230 
1231 	emit_slli(rd, rs, 32, ctx);
1232 	emit_srli(rd, rd, 32, ctx);
1233 }
1234 
1235 static inline void emit_bswap(u8 rd, s32 imm, struct rv_jit_context *ctx)
1236 {
1237 	if (rv_ext_enabled(ZBB)) {
1238 		int bits = 64 - imm;
1239 
1240 		emit(rvzbb_rev8(rd, rd), ctx);
1241 		if (bits)
1242 			emit_srli(rd, rd, bits, ctx);
1243 		return;
1244 	}
1245 
1246 	emit_li(RV_REG_T2, 0, ctx);
1247 
1248 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1249 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1250 	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
1251 	emit_srli(rd, rd, 8, ctx);
1252 	if (imm == 16)
1253 		goto out_be;
1254 
1255 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1256 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1257 	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
1258 	emit_srli(rd, rd, 8, ctx);
1259 
1260 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1261 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1262 	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
1263 	emit_srli(rd, rd, 8, ctx);
1264 	if (imm == 32)
1265 		goto out_be;
1266 
1267 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1268 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1269 	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
1270 	emit_srli(rd, rd, 8, ctx);
1271 
1272 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1273 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1274 	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
1275 	emit_srli(rd, rd, 8, ctx);
1276 
1277 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1278 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1279 	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
1280 	emit_srli(rd, rd, 8, ctx);
1281 
1282 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1283 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1284 	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
1285 	emit_srli(rd, rd, 8, ctx);
1286 out_be:
1287 	emit_andi(RV_REG_T1, rd, 0xff, ctx);
1288 	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
1289 
1290 	emit_mv(rd, RV_REG_T2, ctx);
1291 }
1292 
1293 static inline void emit_cmpxchg(u8 rd, u8 rs, u8 r0, bool is64, struct rv_jit_context *ctx)
1294 {
1295 	int jmp_offset;
1296 
1297 	if (rv_ext_enabled(ZACAS)) {
1298 		ctx->ex_insn_off = ctx->ninsns;
1299 		emit(is64 ? rvzacas_amocas_d(r0, rs, rd, 1, 1) :
1300 		     rvzacas_amocas_w(r0, rs, rd, 1, 1), ctx);
1301 		ctx->ex_jmp_off = ctx->ninsns;
1302 		if (!is64)
1303 			emit_zextw(r0, r0, ctx);
1304 		return;
1305 	}
1306 
1307 	if (is64)
1308 		emit_mv(RV_REG_T2, r0, ctx);
1309 	else
1310 		emit_addiw(RV_REG_T2, r0, 0, ctx);
1311 	emit(is64 ? rv_lr_d(r0, 0, rd, 0, 0) :
1312 	     rv_lr_w(r0, 0, rd, 0, 0), ctx);
1313 	jmp_offset = ninsns_rvoff(8);
1314 	emit(rv_bne(RV_REG_T2, r0, jmp_offset >> 1), ctx);
1315 	emit(is64 ? rv_sc_d(RV_REG_T3, rs, rd, 0, 1) :
1316 	     rv_sc_w(RV_REG_T3, rs, rd, 0, 1), ctx);
1317 	jmp_offset = ninsns_rvoff(-6);
1318 	emit(rv_bne(RV_REG_T3, 0, jmp_offset >> 1), ctx);
1319 	emit_fence_rw_rw(ctx);
1320 }
1321 
1322 #endif /* __riscv_xlen == 64 */
1323 
1324 void bpf_jit_build_prologue(struct rv_jit_context *ctx, bool is_subprog);
1325 void bpf_jit_build_epilogue(struct rv_jit_context *ctx);
1326 
1327 int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
1328 		      bool extra_pass);
1329 
1330 #endif /* _BPF_JIT_H */
1331