1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Common functionality for RV32 and RV64 BPF JIT compilers
5 * Copyright (c) 2019 Björn Töpel <bjorn.topel@gmail.com>
12 #include <linux/bpf.h>
13 #include <linux/filter.h>
14 #include <asm/cacheflush.h>
16 static inline bool rvc_enabled(void)
18 return IS_ENABLED(CONFIG_RISCV_ISA_C
);
21 static inline bool rvzba_enabled(void)
23 return IS_ENABLED(CONFIG_RISCV_ISA_ZBA
) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBA
);
26 static inline bool rvzbb_enabled(void)
28 return IS_ENABLED(CONFIG_RISCV_ISA_ZBB
) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB
);
32 RV_REG_ZERO
= 0, /* The constant value 0 */
33 RV_REG_RA
= 1, /* Return address */
34 RV_REG_SP
= 2, /* Stack pointer */
35 RV_REG_GP
= 3, /* Global pointer */
36 RV_REG_TP
= 4, /* Thread pointer */
37 RV_REG_T0
= 5, /* Temporaries */
40 RV_REG_FP
= 8, /* Saved register/frame pointer */
41 RV_REG_S1
= 9, /* Saved register */
42 RV_REG_A0
= 10, /* Function argument/return values */
43 RV_REG_A1
= 11, /* Function arguments */
50 RV_REG_S2
= 18, /* Saved registers */
60 RV_REG_T3
= 28, /* Temporaries */
66 static inline bool is_creg(u8 reg
)
68 return (1 << reg
) & (BIT(RV_REG_FP
) |
78 struct rv_jit_context
{
79 struct bpf_prog
*prog
;
80 u16
*insns
; /* RV insns */
85 int *offset
; /* BPF to RV */
93 /* Convert from ninsns to bytes. */
94 static inline int ninsns_rvoff(int ninsns
)
100 struct bpf_binary_header
*header
;
101 struct bpf_binary_header
*ro_header
;
104 struct rv_jit_context ctx
;
107 static inline void bpf_fill_ill_insns(void *area
, unsigned int size
)
109 memset(area
, 0, size
);
112 static inline void bpf_flush_icache(void *start
, void *end
)
114 flush_icache_range((unsigned long)start
, (unsigned long)end
);
117 /* Emit a 4-byte riscv instruction. */
118 static inline void emit(const u32 insn
, struct rv_jit_context
*ctx
)
121 ctx
->insns
[ctx
->ninsns
] = insn
;
122 ctx
->insns
[ctx
->ninsns
+ 1] = (insn
>> 16);
128 /* Emit a 2-byte riscv compressed instruction. */
129 static inline void emitc(const u16 insn
, struct rv_jit_context
*ctx
)
131 BUILD_BUG_ON(!rvc_enabled());
134 ctx
->insns
[ctx
->ninsns
] = insn
;
139 static inline int epilogue_offset(struct rv_jit_context
*ctx
)
141 int to
= ctx
->epilogue_offset
, from
= ctx
->ninsns
;
143 return ninsns_rvoff(to
- from
);
146 /* Return -1 or inverted cond. */
147 static inline int invert_bpf_cond(u8 cond
)
174 static inline bool is_6b_int(long val
)
176 return -(1L << 5) <= val
&& val
< (1L << 5);
179 static inline bool is_7b_uint(unsigned long val
)
181 return val
< (1UL << 7);
184 static inline bool is_8b_uint(unsigned long val
)
186 return val
< (1UL << 8);
189 static inline bool is_9b_uint(unsigned long val
)
191 return val
< (1UL << 9);
194 static inline bool is_10b_int(long val
)
196 return -(1L << 9) <= val
&& val
< (1L << 9);
199 static inline bool is_10b_uint(unsigned long val
)
201 return val
< (1UL << 10);
204 static inline bool is_12b_int(long val
)
206 return -(1L << 11) <= val
&& val
< (1L << 11);
209 static inline int is_12b_check(int off
, int insn
)
211 if (!is_12b_int(off
)) {
212 pr_err("bpf-jit: insn=%d 12b < offset=%d not supported yet!\n",
219 static inline bool is_13b_int(long val
)
221 return -(1L << 12) <= val
&& val
< (1L << 12);
224 static inline bool is_21b_int(long val
)
226 return -(1L << 20) <= val
&& val
< (1L << 20);
229 static inline int rv_offset(int insn
, int off
, struct rv_jit_context
*ctx
)
233 off
++; /* BPF branch is from PC+1, RV is from PC */
234 from
= (insn
> 0) ? ctx
->offset
[insn
- 1] : ctx
->prologue_len
;
235 to
= (insn
+ off
> 0) ? ctx
->offset
[insn
+ off
- 1] : ctx
->prologue_len
;
236 return ninsns_rvoff(to
- from
);
239 /* Instruction formats. */
241 static inline u32
rv_r_insn(u8 funct7
, u8 rs2
, u8 rs1
, u8 funct3
, u8 rd
,
244 return (funct7
<< 25) | (rs2
<< 20) | (rs1
<< 15) | (funct3
<< 12) |
248 static inline u32
rv_i_insn(u16 imm11_0
, u8 rs1
, u8 funct3
, u8 rd
, u8 opcode
)
250 return (imm11_0
<< 20) | (rs1
<< 15) | (funct3
<< 12) | (rd
<< 7) |
254 static inline u32
rv_s_insn(u16 imm11_0
, u8 rs2
, u8 rs1
, u8 funct3
, u8 opcode
)
256 u8 imm11_5
= imm11_0
>> 5, imm4_0
= imm11_0
& 0x1f;
258 return (imm11_5
<< 25) | (rs2
<< 20) | (rs1
<< 15) | (funct3
<< 12) |
259 (imm4_0
<< 7) | opcode
;
262 static inline u32
rv_b_insn(u16 imm12_1
, u8 rs2
, u8 rs1
, u8 funct3
, u8 opcode
)
264 u8 imm12
= ((imm12_1
& 0x800) >> 5) | ((imm12_1
& 0x3f0) >> 4);
265 u8 imm4_1
= ((imm12_1
& 0xf) << 1) | ((imm12_1
& 0x400) >> 10);
267 return (imm12
<< 25) | (rs2
<< 20) | (rs1
<< 15) | (funct3
<< 12) |
268 (imm4_1
<< 7) | opcode
;
271 static inline u32
rv_u_insn(u32 imm31_12
, u8 rd
, u8 opcode
)
273 return (imm31_12
<< 12) | (rd
<< 7) | opcode
;
276 static inline u32
rv_j_insn(u32 imm20_1
, u8 rd
, u8 opcode
)
280 imm
= (imm20_1
& 0x80000) | ((imm20_1
& 0x3ff) << 9) |
281 ((imm20_1
& 0x400) >> 2) | ((imm20_1
& 0x7f800) >> 11);
283 return (imm
<< 12) | (rd
<< 7) | opcode
;
286 static inline u32
rv_amo_insn(u8 funct5
, u8 aq
, u8 rl
, u8 rs2
, u8 rs1
,
287 u8 funct3
, u8 rd
, u8 opcode
)
289 u8 funct7
= (funct5
<< 2) | (aq
<< 1) | rl
;
291 return rv_r_insn(funct7
, rs2
, rs1
, funct3
, rd
, opcode
);
294 /* RISC-V compressed instruction formats. */
296 static inline u16
rv_cr_insn(u8 funct4
, u8 rd
, u8 rs2
, u8 op
)
298 return (funct4
<< 12) | (rd
<< 7) | (rs2
<< 2) | op
;
301 static inline u16
rv_ci_insn(u8 funct3
, u32 imm6
, u8 rd
, u8 op
)
305 imm
= ((imm6
& 0x20) << 7) | ((imm6
& 0x1f) << 2);
306 return (funct3
<< 13) | (rd
<< 7) | op
| imm
;
309 static inline u16
rv_css_insn(u8 funct3
, u32 uimm
, u8 rs2
, u8 op
)
311 return (funct3
<< 13) | (uimm
<< 7) | (rs2
<< 2) | op
;
314 static inline u16
rv_ciw_insn(u8 funct3
, u32 uimm
, u8 rd
, u8 op
)
316 return (funct3
<< 13) | (uimm
<< 5) | ((rd
& 0x7) << 2) | op
;
319 static inline u16
rv_cl_insn(u8 funct3
, u32 imm_hi
, u8 rs1
, u32 imm_lo
, u8 rd
,
322 return (funct3
<< 13) | (imm_hi
<< 10) | ((rs1
& 0x7) << 7) |
323 (imm_lo
<< 5) | ((rd
& 0x7) << 2) | op
;
326 static inline u16
rv_cs_insn(u8 funct3
, u32 imm_hi
, u8 rs1
, u32 imm_lo
, u8 rs2
,
329 return (funct3
<< 13) | (imm_hi
<< 10) | ((rs1
& 0x7) << 7) |
330 (imm_lo
<< 5) | ((rs2
& 0x7) << 2) | op
;
333 static inline u16
rv_ca_insn(u8 funct6
, u8 rd
, u8 funct2
, u8 rs2
, u8 op
)
335 return (funct6
<< 10) | ((rd
& 0x7) << 7) | (funct2
<< 5) |
336 ((rs2
& 0x7) << 2) | op
;
339 static inline u16
rv_cb_insn(u8 funct3
, u32 imm6
, u8 funct2
, u8 rd
, u8 op
)
343 imm
= ((imm6
& 0x20) << 7) | ((imm6
& 0x1f) << 2);
344 return (funct3
<< 13) | (funct2
<< 10) | ((rd
& 0x7) << 7) | op
| imm
;
347 /* Instructions shared by both RV32 and RV64. */
349 static inline u32
rv_addi(u8 rd
, u8 rs1
, u16 imm11_0
)
351 return rv_i_insn(imm11_0
, rs1
, 0, rd
, 0x13);
354 static inline u32
rv_andi(u8 rd
, u8 rs1
, u16 imm11_0
)
356 return rv_i_insn(imm11_0
, rs1
, 7, rd
, 0x13);
359 static inline u32
rv_ori(u8 rd
, u8 rs1
, u16 imm11_0
)
361 return rv_i_insn(imm11_0
, rs1
, 6, rd
, 0x13);
364 static inline u32
rv_xori(u8 rd
, u8 rs1
, u16 imm11_0
)
366 return rv_i_insn(imm11_0
, rs1
, 4, rd
, 0x13);
369 static inline u32
rv_slli(u8 rd
, u8 rs1
, u16 imm11_0
)
371 return rv_i_insn(imm11_0
, rs1
, 1, rd
, 0x13);
374 static inline u32
rv_srli(u8 rd
, u8 rs1
, u16 imm11_0
)
376 return rv_i_insn(imm11_0
, rs1
, 5, rd
, 0x13);
379 static inline u32
rv_srai(u8 rd
, u8 rs1
, u16 imm11_0
)
381 return rv_i_insn(0x400 | imm11_0
, rs1
, 5, rd
, 0x13);
384 static inline u32
rv_lui(u8 rd
, u32 imm31_12
)
386 return rv_u_insn(imm31_12
, rd
, 0x37);
389 static inline u32
rv_auipc(u8 rd
, u32 imm31_12
)
391 return rv_u_insn(imm31_12
, rd
, 0x17);
394 static inline u32
rv_add(u8 rd
, u8 rs1
, u8 rs2
)
396 return rv_r_insn(0, rs2
, rs1
, 0, rd
, 0x33);
399 static inline u32
rv_sub(u8 rd
, u8 rs1
, u8 rs2
)
401 return rv_r_insn(0x20, rs2
, rs1
, 0, rd
, 0x33);
404 static inline u32
rv_sltu(u8 rd
, u8 rs1
, u8 rs2
)
406 return rv_r_insn(0, rs2
, rs1
, 3, rd
, 0x33);
409 static inline u32
rv_and(u8 rd
, u8 rs1
, u8 rs2
)
411 return rv_r_insn(0, rs2
, rs1
, 7, rd
, 0x33);
414 static inline u32
rv_or(u8 rd
, u8 rs1
, u8 rs2
)
416 return rv_r_insn(0, rs2
, rs1
, 6, rd
, 0x33);
419 static inline u32
rv_xor(u8 rd
, u8 rs1
, u8 rs2
)
421 return rv_r_insn(0, rs2
, rs1
, 4, rd
, 0x33);
424 static inline u32
rv_sll(u8 rd
, u8 rs1
, u8 rs2
)
426 return rv_r_insn(0, rs2
, rs1
, 1, rd
, 0x33);
429 static inline u32
rv_srl(u8 rd
, u8 rs1
, u8 rs2
)
431 return rv_r_insn(0, rs2
, rs1
, 5, rd
, 0x33);
434 static inline u32
rv_sra(u8 rd
, u8 rs1
, u8 rs2
)
436 return rv_r_insn(0x20, rs2
, rs1
, 5, rd
, 0x33);
439 static inline u32
rv_mul(u8 rd
, u8 rs1
, u8 rs2
)
441 return rv_r_insn(1, rs2
, rs1
, 0, rd
, 0x33);
444 static inline u32
rv_mulhu(u8 rd
, u8 rs1
, u8 rs2
)
446 return rv_r_insn(1, rs2
, rs1
, 3, rd
, 0x33);
449 static inline u32
rv_div(u8 rd
, u8 rs1
, u8 rs2
)
451 return rv_r_insn(1, rs2
, rs1
, 4, rd
, 0x33);
454 static inline u32
rv_divu(u8 rd
, u8 rs1
, u8 rs2
)
456 return rv_r_insn(1, rs2
, rs1
, 5, rd
, 0x33);
459 static inline u32
rv_rem(u8 rd
, u8 rs1
, u8 rs2
)
461 return rv_r_insn(1, rs2
, rs1
, 6, rd
, 0x33);
464 static inline u32
rv_remu(u8 rd
, u8 rs1
, u8 rs2
)
466 return rv_r_insn(1, rs2
, rs1
, 7, rd
, 0x33);
469 static inline u32
rv_jal(u8 rd
, u32 imm20_1
)
471 return rv_j_insn(imm20_1
, rd
, 0x6f);
474 static inline u32
rv_jalr(u8 rd
, u8 rs1
, u16 imm11_0
)
476 return rv_i_insn(imm11_0
, rs1
, 0, rd
, 0x67);
479 static inline u32
rv_beq(u8 rs1
, u8 rs2
, u16 imm12_1
)
481 return rv_b_insn(imm12_1
, rs2
, rs1
, 0, 0x63);
484 static inline u32
rv_bne(u8 rs1
, u8 rs2
, u16 imm12_1
)
486 return rv_b_insn(imm12_1
, rs2
, rs1
, 1, 0x63);
489 static inline u32
rv_bltu(u8 rs1
, u8 rs2
, u16 imm12_1
)
491 return rv_b_insn(imm12_1
, rs2
, rs1
, 6, 0x63);
494 static inline u32
rv_bgtu(u8 rs1
, u8 rs2
, u16 imm12_1
)
496 return rv_bltu(rs2
, rs1
, imm12_1
);
499 static inline u32
rv_bgeu(u8 rs1
, u8 rs2
, u16 imm12_1
)
501 return rv_b_insn(imm12_1
, rs2
, rs1
, 7, 0x63);
504 static inline u32
rv_bleu(u8 rs1
, u8 rs2
, u16 imm12_1
)
506 return rv_bgeu(rs2
, rs1
, imm12_1
);
509 static inline u32
rv_blt(u8 rs1
, u8 rs2
, u16 imm12_1
)
511 return rv_b_insn(imm12_1
, rs2
, rs1
, 4, 0x63);
514 static inline u32
rv_bgt(u8 rs1
, u8 rs2
, u16 imm12_1
)
516 return rv_blt(rs2
, rs1
, imm12_1
);
519 static inline u32
rv_bge(u8 rs1
, u8 rs2
, u16 imm12_1
)
521 return rv_b_insn(imm12_1
, rs2
, rs1
, 5, 0x63);
524 static inline u32
rv_ble(u8 rs1
, u8 rs2
, u16 imm12_1
)
526 return rv_bge(rs2
, rs1
, imm12_1
);
529 static inline u32
rv_lb(u8 rd
, u16 imm11_0
, u8 rs1
)
531 return rv_i_insn(imm11_0
, rs1
, 0, rd
, 0x03);
534 static inline u32
rv_lh(u8 rd
, u16 imm11_0
, u8 rs1
)
536 return rv_i_insn(imm11_0
, rs1
, 1, rd
, 0x03);
539 static inline u32
rv_lw(u8 rd
, u16 imm11_0
, u8 rs1
)
541 return rv_i_insn(imm11_0
, rs1
, 2, rd
, 0x03);
544 static inline u32
rv_lbu(u8 rd
, u16 imm11_0
, u8 rs1
)
546 return rv_i_insn(imm11_0
, rs1
, 4, rd
, 0x03);
549 static inline u32
rv_lhu(u8 rd
, u16 imm11_0
, u8 rs1
)
551 return rv_i_insn(imm11_0
, rs1
, 5, rd
, 0x03);
554 static inline u32
rv_sb(u8 rs1
, u16 imm11_0
, u8 rs2
)
556 return rv_s_insn(imm11_0
, rs2
, rs1
, 0, 0x23);
559 static inline u32
rv_sh(u8 rs1
, u16 imm11_0
, u8 rs2
)
561 return rv_s_insn(imm11_0
, rs2
, rs1
, 1, 0x23);
564 static inline u32
rv_sw(u8 rs1
, u16 imm11_0
, u8 rs2
)
566 return rv_s_insn(imm11_0
, rs2
, rs1
, 2, 0x23);
569 static inline u32
rv_amoadd_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
571 return rv_amo_insn(0, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
574 static inline u32
rv_amoand_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
576 return rv_amo_insn(0xc, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
579 static inline u32
rv_amoor_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
581 return rv_amo_insn(0x8, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
584 static inline u32
rv_amoxor_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
586 return rv_amo_insn(0x4, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
589 static inline u32
rv_amoswap_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
591 return rv_amo_insn(0x1, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
594 static inline u32
rv_lr_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
596 return rv_amo_insn(0x2, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
599 static inline u32
rv_sc_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
601 return rv_amo_insn(0x3, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
604 static inline u32
rv_fence(u8 pred
, u8 succ
)
606 u16 imm11_0
= pred
<< 4 | succ
;
608 return rv_i_insn(imm11_0
, 0, 0, 0, 0xf);
611 static inline u32
rv_nop(void)
613 return rv_i_insn(0, 0, 0, 0, 0x13);
616 /* RVC instructions. */
618 static inline u16
rvc_addi4spn(u8 rd
, u32 imm10
)
622 imm
= ((imm10
& 0x30) << 2) | ((imm10
& 0x3c0) >> 4) |
623 ((imm10
& 0x4) >> 1) | ((imm10
& 0x8) >> 3);
624 return rv_ciw_insn(0x0, imm
, rd
, 0x0);
627 static inline u16
rvc_lw(u8 rd
, u32 imm7
, u8 rs1
)
631 imm_hi
= (imm7
& 0x38) >> 3;
632 imm_lo
= ((imm7
& 0x4) >> 1) | ((imm7
& 0x40) >> 6);
633 return rv_cl_insn(0x2, imm_hi
, rs1
, imm_lo
, rd
, 0x0);
636 static inline u16
rvc_sw(u8 rs1
, u32 imm7
, u8 rs2
)
640 imm_hi
= (imm7
& 0x38) >> 3;
641 imm_lo
= ((imm7
& 0x4) >> 1) | ((imm7
& 0x40) >> 6);
642 return rv_cs_insn(0x6, imm_hi
, rs1
, imm_lo
, rs2
, 0x0);
645 static inline u16
rvc_addi(u8 rd
, u32 imm6
)
647 return rv_ci_insn(0, imm6
, rd
, 0x1);
650 static inline u16
rvc_li(u8 rd
, u32 imm6
)
652 return rv_ci_insn(0x2, imm6
, rd
, 0x1);
655 static inline u16
rvc_addi16sp(u32 imm10
)
659 imm
= ((imm10
& 0x200) >> 4) | (imm10
& 0x10) | ((imm10
& 0x40) >> 3) |
660 ((imm10
& 0x180) >> 6) | ((imm10
& 0x20) >> 5);
661 return rv_ci_insn(0x3, imm
, RV_REG_SP
, 0x1);
664 static inline u16
rvc_lui(u8 rd
, u32 imm6
)
666 return rv_ci_insn(0x3, imm6
, rd
, 0x1);
669 static inline u16
rvc_srli(u8 rd
, u32 imm6
)
671 return rv_cb_insn(0x4, imm6
, 0, rd
, 0x1);
674 static inline u16
rvc_srai(u8 rd
, u32 imm6
)
676 return rv_cb_insn(0x4, imm6
, 0x1, rd
, 0x1);
679 static inline u16
rvc_andi(u8 rd
, u32 imm6
)
681 return rv_cb_insn(0x4, imm6
, 0x2, rd
, 0x1);
684 static inline u16
rvc_sub(u8 rd
, u8 rs
)
686 return rv_ca_insn(0x23, rd
, 0, rs
, 0x1);
689 static inline u16
rvc_xor(u8 rd
, u8 rs
)
691 return rv_ca_insn(0x23, rd
, 0x1, rs
, 0x1);
694 static inline u16
rvc_or(u8 rd
, u8 rs
)
696 return rv_ca_insn(0x23, rd
, 0x2, rs
, 0x1);
699 static inline u16
rvc_and(u8 rd
, u8 rs
)
701 return rv_ca_insn(0x23, rd
, 0x3, rs
, 0x1);
704 static inline u16
rvc_slli(u8 rd
, u32 imm6
)
706 return rv_ci_insn(0, imm6
, rd
, 0x2);
709 static inline u16
rvc_lwsp(u8 rd
, u32 imm8
)
713 imm
= ((imm8
& 0xc0) >> 6) | (imm8
& 0x3c);
714 return rv_ci_insn(0x2, imm
, rd
, 0x2);
717 static inline u16
rvc_jr(u8 rs1
)
719 return rv_cr_insn(0x8, rs1
, RV_REG_ZERO
, 0x2);
722 static inline u16
rvc_mv(u8 rd
, u8 rs
)
724 return rv_cr_insn(0x8, rd
, rs
, 0x2);
727 static inline u16
rvc_jalr(u8 rs1
)
729 return rv_cr_insn(0x9, rs1
, RV_REG_ZERO
, 0x2);
732 static inline u16
rvc_add(u8 rd
, u8 rs
)
734 return rv_cr_insn(0x9, rd
, rs
, 0x2);
737 static inline u16
rvc_swsp(u32 imm8
, u8 rs2
)
741 imm
= (imm8
& 0x3c) | ((imm8
& 0xc0) >> 6);
742 return rv_css_insn(0x6, imm
, rs2
, 0x2);
745 /* RVZBA instructions. */
746 static inline u32
rvzba_sh2add(u8 rd
, u8 rs1
, u8 rs2
)
748 return rv_r_insn(0x10, rs2
, rs1
, 0x4, rd
, 0x33);
751 static inline u32
rvzba_sh3add(u8 rd
, u8 rs1
, u8 rs2
)
753 return rv_r_insn(0x10, rs2
, rs1
, 0x6, rd
, 0x33);
756 /* RVZBB instructions. */
757 static inline u32
rvzbb_sextb(u8 rd
, u8 rs1
)
759 return rv_i_insn(0x604, rs1
, 1, rd
, 0x13);
762 static inline u32
rvzbb_sexth(u8 rd
, u8 rs1
)
764 return rv_i_insn(0x605, rs1
, 1, rd
, 0x13);
767 static inline u32
rvzbb_zexth(u8 rd
, u8 rs
)
769 if (IS_ENABLED(CONFIG_64BIT
))
770 return rv_i_insn(0x80, rs
, 4, rd
, 0x3b);
772 return rv_i_insn(0x80, rs
, 4, rd
, 0x33);
775 static inline u32
rvzbb_rev8(u8 rd
, u8 rs
)
777 if (IS_ENABLED(CONFIG_64BIT
))
778 return rv_i_insn(0x6b8, rs
, 5, rd
, 0x13);
780 return rv_i_insn(0x698, rs
, 5, rd
, 0x13);
784 * RV64-only instructions.
786 * These instructions are not available on RV32. Wrap them below a #if to
787 * ensure that the RV32 JIT doesn't emit any of these instructions.
790 #if __riscv_xlen == 64
792 static inline u32
rv_addiw(u8 rd
, u8 rs1
, u16 imm11_0
)
794 return rv_i_insn(imm11_0
, rs1
, 0, rd
, 0x1b);
797 static inline u32
rv_slliw(u8 rd
, u8 rs1
, u16 imm11_0
)
799 return rv_i_insn(imm11_0
, rs1
, 1, rd
, 0x1b);
802 static inline u32
rv_srliw(u8 rd
, u8 rs1
, u16 imm11_0
)
804 return rv_i_insn(imm11_0
, rs1
, 5, rd
, 0x1b);
807 static inline u32
rv_sraiw(u8 rd
, u8 rs1
, u16 imm11_0
)
809 return rv_i_insn(0x400 | imm11_0
, rs1
, 5, rd
, 0x1b);
812 static inline u32
rv_addw(u8 rd
, u8 rs1
, u8 rs2
)
814 return rv_r_insn(0, rs2
, rs1
, 0, rd
, 0x3b);
817 static inline u32
rv_subw(u8 rd
, u8 rs1
, u8 rs2
)
819 return rv_r_insn(0x20, rs2
, rs1
, 0, rd
, 0x3b);
822 static inline u32
rv_sllw(u8 rd
, u8 rs1
, u8 rs2
)
824 return rv_r_insn(0, rs2
, rs1
, 1, rd
, 0x3b);
827 static inline u32
rv_srlw(u8 rd
, u8 rs1
, u8 rs2
)
829 return rv_r_insn(0, rs2
, rs1
, 5, rd
, 0x3b);
832 static inline u32
rv_sraw(u8 rd
, u8 rs1
, u8 rs2
)
834 return rv_r_insn(0x20, rs2
, rs1
, 5, rd
, 0x3b);
837 static inline u32
rv_mulw(u8 rd
, u8 rs1
, u8 rs2
)
839 return rv_r_insn(1, rs2
, rs1
, 0, rd
, 0x3b);
842 static inline u32
rv_divw(u8 rd
, u8 rs1
, u8 rs2
)
844 return rv_r_insn(1, rs2
, rs1
, 4, rd
, 0x3b);
847 static inline u32
rv_divuw(u8 rd
, u8 rs1
, u8 rs2
)
849 return rv_r_insn(1, rs2
, rs1
, 5, rd
, 0x3b);
852 static inline u32
rv_remw(u8 rd
, u8 rs1
, u8 rs2
)
854 return rv_r_insn(1, rs2
, rs1
, 6, rd
, 0x3b);
857 static inline u32
rv_remuw(u8 rd
, u8 rs1
, u8 rs2
)
859 return rv_r_insn(1, rs2
, rs1
, 7, rd
, 0x3b);
862 static inline u32
rv_ld(u8 rd
, u16 imm11_0
, u8 rs1
)
864 return rv_i_insn(imm11_0
, rs1
, 3, rd
, 0x03);
867 static inline u32
rv_lwu(u8 rd
, u16 imm11_0
, u8 rs1
)
869 return rv_i_insn(imm11_0
, rs1
, 6, rd
, 0x03);
872 static inline u32
rv_sd(u8 rs1
, u16 imm11_0
, u8 rs2
)
874 return rv_s_insn(imm11_0
, rs2
, rs1
, 3, 0x23);
877 static inline u32
rv_amoadd_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
879 return rv_amo_insn(0, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
882 static inline u32
rv_amoand_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
884 return rv_amo_insn(0xc, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
887 static inline u32
rv_amoor_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
889 return rv_amo_insn(0x8, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
892 static inline u32
rv_amoxor_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
894 return rv_amo_insn(0x4, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
897 static inline u32
rv_amoswap_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
899 return rv_amo_insn(0x1, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
902 static inline u32
rv_lr_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
904 return rv_amo_insn(0x2, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
907 static inline u32
rv_sc_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
909 return rv_amo_insn(0x3, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
912 /* RV64-only RVC instructions. */
914 static inline u16
rvc_ld(u8 rd
, u32 imm8
, u8 rs1
)
918 imm_hi
= (imm8
& 0x38) >> 3;
919 imm_lo
= (imm8
& 0xc0) >> 6;
920 return rv_cl_insn(0x3, imm_hi
, rs1
, imm_lo
, rd
, 0x0);
923 static inline u16
rvc_sd(u8 rs1
, u32 imm8
, u8 rs2
)
927 imm_hi
= (imm8
& 0x38) >> 3;
928 imm_lo
= (imm8
& 0xc0) >> 6;
929 return rv_cs_insn(0x7, imm_hi
, rs1
, imm_lo
, rs2
, 0x0);
932 static inline u16
rvc_subw(u8 rd
, u8 rs
)
934 return rv_ca_insn(0x27, rd
, 0, rs
, 0x1);
937 static inline u16
rvc_addiw(u8 rd
, u32 imm6
)
939 return rv_ci_insn(0x1, imm6
, rd
, 0x1);
942 static inline u16
rvc_ldsp(u8 rd
, u32 imm9
)
946 imm
= ((imm9
& 0x1c0) >> 6) | (imm9
& 0x38);
947 return rv_ci_insn(0x3, imm
, rd
, 0x2);
950 static inline u16
rvc_sdsp(u32 imm9
, u8 rs2
)
954 imm
= (imm9
& 0x38) | ((imm9
& 0x1c0) >> 6);
955 return rv_css_insn(0x7, imm
, rs2
, 0x2);
958 /* RV64-only ZBA instructions. */
960 static inline u32
rvzba_zextw(u8 rd
, u8 rs1
)
962 /* add.uw rd, rs1, ZERO */
963 return rv_r_insn(0x04, RV_REG_ZERO
, rs1
, 0, rd
, 0x3b);
966 #endif /* __riscv_xlen == 64 */
968 /* Helper functions that emit RVC instructions when possible. */
970 static inline void emit_jalr(u8 rd
, u8 rs
, s32 imm
, struct rv_jit_context
*ctx
)
972 if (rvc_enabled() && rd
== RV_REG_RA
&& rs
&& !imm
)
973 emitc(rvc_jalr(rs
), ctx
);
974 else if (rvc_enabled() && !rd
&& rs
&& !imm
)
975 emitc(rvc_jr(rs
), ctx
);
977 emit(rv_jalr(rd
, rs
, imm
), ctx
);
980 static inline void emit_mv(u8 rd
, u8 rs
, struct rv_jit_context
*ctx
)
982 if (rvc_enabled() && rd
&& rs
)
983 emitc(rvc_mv(rd
, rs
), ctx
);
985 emit(rv_addi(rd
, rs
, 0), ctx
);
988 static inline void emit_add(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
990 if (rvc_enabled() && rd
&& rd
== rs1
&& rs2
)
991 emitc(rvc_add(rd
, rs2
), ctx
);
993 emit(rv_add(rd
, rs1
, rs2
), ctx
);
996 static inline void emit_addi(u8 rd
, u8 rs
, s32 imm
, struct rv_jit_context
*ctx
)
998 if (rvc_enabled() && rd
== RV_REG_SP
&& rd
== rs
&& is_10b_int(imm
) && imm
&& !(imm
& 0xf))
999 emitc(rvc_addi16sp(imm
), ctx
);
1000 else if (rvc_enabled() && is_creg(rd
) && rs
== RV_REG_SP
&& is_10b_uint(imm
) &&
1001 !(imm
& 0x3) && imm
)
1002 emitc(rvc_addi4spn(rd
, imm
), ctx
);
1003 else if (rvc_enabled() && rd
&& rd
== rs
&& imm
&& is_6b_int(imm
))
1004 emitc(rvc_addi(rd
, imm
), ctx
);
1006 emit(rv_addi(rd
, rs
, imm
), ctx
);
1009 static inline void emit_li(u8 rd
, s32 imm
, struct rv_jit_context
*ctx
)
1011 if (rvc_enabled() && rd
&& is_6b_int(imm
))
1012 emitc(rvc_li(rd
, imm
), ctx
);
1014 emit(rv_addi(rd
, RV_REG_ZERO
, imm
), ctx
);
1017 static inline void emit_lui(u8 rd
, s32 imm
, struct rv_jit_context
*ctx
)
1019 if (rvc_enabled() && rd
&& rd
!= RV_REG_SP
&& is_6b_int(imm
) && imm
)
1020 emitc(rvc_lui(rd
, imm
), ctx
);
1022 emit(rv_lui(rd
, imm
), ctx
);
1025 static inline void emit_slli(u8 rd
, u8 rs
, s32 imm
, struct rv_jit_context
*ctx
)
1027 if (rvc_enabled() && rd
&& rd
== rs
&& imm
&& (u32
)imm
< __riscv_xlen
)
1028 emitc(rvc_slli(rd
, imm
), ctx
);
1030 emit(rv_slli(rd
, rs
, imm
), ctx
);
1033 static inline void emit_andi(u8 rd
, u8 rs
, s32 imm
, struct rv_jit_context
*ctx
)
1035 if (rvc_enabled() && is_creg(rd
) && rd
== rs
&& is_6b_int(imm
))
1036 emitc(rvc_andi(rd
, imm
), ctx
);
1038 emit(rv_andi(rd
, rs
, imm
), ctx
);
1041 static inline void emit_srli(u8 rd
, u8 rs
, s32 imm
, struct rv_jit_context
*ctx
)
1043 if (rvc_enabled() && is_creg(rd
) && rd
== rs
&& imm
&& (u32
)imm
< __riscv_xlen
)
1044 emitc(rvc_srli(rd
, imm
), ctx
);
1046 emit(rv_srli(rd
, rs
, imm
), ctx
);
1049 static inline void emit_srai(u8 rd
, u8 rs
, s32 imm
, struct rv_jit_context
*ctx
)
1051 if (rvc_enabled() && is_creg(rd
) && rd
== rs
&& imm
&& (u32
)imm
< __riscv_xlen
)
1052 emitc(rvc_srai(rd
, imm
), ctx
);
1054 emit(rv_srai(rd
, rs
, imm
), ctx
);
1057 static inline void emit_sub(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
1059 if (rvc_enabled() && is_creg(rd
) && rd
== rs1
&& is_creg(rs2
))
1060 emitc(rvc_sub(rd
, rs2
), ctx
);
1062 emit(rv_sub(rd
, rs1
, rs2
), ctx
);
1065 static inline void emit_or(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
1067 if (rvc_enabled() && is_creg(rd
) && rd
== rs1
&& is_creg(rs2
))
1068 emitc(rvc_or(rd
, rs2
), ctx
);
1070 emit(rv_or(rd
, rs1
, rs2
), ctx
);
1073 static inline void emit_and(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
1075 if (rvc_enabled() && is_creg(rd
) && rd
== rs1
&& is_creg(rs2
))
1076 emitc(rvc_and(rd
, rs2
), ctx
);
1078 emit(rv_and(rd
, rs1
, rs2
), ctx
);
1081 static inline void emit_xor(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
1083 if (rvc_enabled() && is_creg(rd
) && rd
== rs1
&& is_creg(rs2
))
1084 emitc(rvc_xor(rd
, rs2
), ctx
);
1086 emit(rv_xor(rd
, rs1
, rs2
), ctx
);
1089 static inline void emit_lw(u8 rd
, s32 off
, u8 rs1
, struct rv_jit_context
*ctx
)
1091 if (rvc_enabled() && rs1
== RV_REG_SP
&& rd
&& is_8b_uint(off
) && !(off
& 0x3))
1092 emitc(rvc_lwsp(rd
, off
), ctx
);
1093 else if (rvc_enabled() && is_creg(rd
) && is_creg(rs1
) && is_7b_uint(off
) && !(off
& 0x3))
1094 emitc(rvc_lw(rd
, off
, rs1
), ctx
);
1096 emit(rv_lw(rd
, off
, rs1
), ctx
);
1099 static inline void emit_sw(u8 rs1
, s32 off
, u8 rs2
, struct rv_jit_context
*ctx
)
1101 if (rvc_enabled() && rs1
== RV_REG_SP
&& is_8b_uint(off
) && !(off
& 0x3))
1102 emitc(rvc_swsp(off
, rs2
), ctx
);
1103 else if (rvc_enabled() && is_creg(rs1
) && is_creg(rs2
) && is_7b_uint(off
) && !(off
& 0x3))
1104 emitc(rvc_sw(rs1
, off
, rs2
), ctx
);
1106 emit(rv_sw(rs1
, off
, rs2
), ctx
);
1109 static inline void emit_sh2add(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
1111 if (rvzba_enabled()) {
1112 emit(rvzba_sh2add(rd
, rs1
, rs2
), ctx
);
1116 emit_slli(rd
, rs1
, 2, ctx
);
1117 emit_add(rd
, rd
, rs2
, ctx
);
1120 static inline void emit_sh3add(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
1122 if (rvzba_enabled()) {
1123 emit(rvzba_sh3add(rd
, rs1
, rs2
), ctx
);
1127 emit_slli(rd
, rs1
, 3, ctx
);
1128 emit_add(rd
, rd
, rs2
, ctx
);
1131 /* RV64-only helper functions. */
1132 #if __riscv_xlen == 64
1134 static inline void emit_addiw(u8 rd
, u8 rs
, s32 imm
, struct rv_jit_context
*ctx
)
1136 if (rvc_enabled() && rd
&& rd
== rs
&& is_6b_int(imm
))
1137 emitc(rvc_addiw(rd
, imm
), ctx
);
1139 emit(rv_addiw(rd
, rs
, imm
), ctx
);
1142 static inline void emit_ld(u8 rd
, s32 off
, u8 rs1
, struct rv_jit_context
*ctx
)
1144 if (rvc_enabled() && rs1
== RV_REG_SP
&& rd
&& is_9b_uint(off
) && !(off
& 0x7))
1145 emitc(rvc_ldsp(rd
, off
), ctx
);
1146 else if (rvc_enabled() && is_creg(rd
) && is_creg(rs1
) && is_8b_uint(off
) && !(off
& 0x7))
1147 emitc(rvc_ld(rd
, off
, rs1
), ctx
);
1149 emit(rv_ld(rd
, off
, rs1
), ctx
);
1152 static inline void emit_sd(u8 rs1
, s32 off
, u8 rs2
, struct rv_jit_context
*ctx
)
1154 if (rvc_enabled() && rs1
== RV_REG_SP
&& is_9b_uint(off
) && !(off
& 0x7))
1155 emitc(rvc_sdsp(off
, rs2
), ctx
);
1156 else if (rvc_enabled() && is_creg(rs1
) && is_creg(rs2
) && is_8b_uint(off
) && !(off
& 0x7))
1157 emitc(rvc_sd(rs1
, off
, rs2
), ctx
);
1159 emit(rv_sd(rs1
, off
, rs2
), ctx
);
1162 static inline void emit_subw(u8 rd
, u8 rs1
, u8 rs2
, struct rv_jit_context
*ctx
)
1164 if (rvc_enabled() && is_creg(rd
) && rd
== rs1
&& is_creg(rs2
))
1165 emitc(rvc_subw(rd
, rs2
), ctx
);
1167 emit(rv_subw(rd
, rs1
, rs2
), ctx
);
1170 static inline void emit_sextb(u8 rd
, u8 rs
, struct rv_jit_context
*ctx
)
1172 if (rvzbb_enabled()) {
1173 emit(rvzbb_sextb(rd
, rs
), ctx
);
1177 emit_slli(rd
, rs
, 56, ctx
);
1178 emit_srai(rd
, rd
, 56, ctx
);
1181 static inline void emit_sexth(u8 rd
, u8 rs
, struct rv_jit_context
*ctx
)
1183 if (rvzbb_enabled()) {
1184 emit(rvzbb_sexth(rd
, rs
), ctx
);
1188 emit_slli(rd
, rs
, 48, ctx
);
1189 emit_srai(rd
, rd
, 48, ctx
);
1192 static inline void emit_sextw(u8 rd
, u8 rs
, struct rv_jit_context
*ctx
)
1194 emit_addiw(rd
, rs
, 0, ctx
);
1197 static inline void emit_zexth(u8 rd
, u8 rs
, struct rv_jit_context
*ctx
)
1199 if (rvzbb_enabled()) {
1200 emit(rvzbb_zexth(rd
, rs
), ctx
);
1204 emit_slli(rd
, rs
, 48, ctx
);
1205 emit_srli(rd
, rd
, 48, ctx
);
1208 static inline void emit_zextw(u8 rd
, u8 rs
, struct rv_jit_context
*ctx
)
1210 if (rvzba_enabled()) {
1211 emit(rvzba_zextw(rd
, rs
), ctx
);
1215 emit_slli(rd
, rs
, 32, ctx
);
1216 emit_srli(rd
, rd
, 32, ctx
);
1219 static inline void emit_bswap(u8 rd
, s32 imm
, struct rv_jit_context
*ctx
)
1221 if (rvzbb_enabled()) {
1222 int bits
= 64 - imm
;
1224 emit(rvzbb_rev8(rd
, rd
), ctx
);
1226 emit_srli(rd
, rd
, bits
, ctx
);
1230 emit_li(RV_REG_T2
, 0, ctx
);
1232 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1233 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1234 emit_slli(RV_REG_T2
, RV_REG_T2
, 8, ctx
);
1235 emit_srli(rd
, rd
, 8, ctx
);
1239 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1240 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1241 emit_slli(RV_REG_T2
, RV_REG_T2
, 8, ctx
);
1242 emit_srli(rd
, rd
, 8, ctx
);
1244 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1245 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1246 emit_slli(RV_REG_T2
, RV_REG_T2
, 8, ctx
);
1247 emit_srli(rd
, rd
, 8, ctx
);
1251 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1252 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1253 emit_slli(RV_REG_T2
, RV_REG_T2
, 8, ctx
);
1254 emit_srli(rd
, rd
, 8, ctx
);
1256 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1257 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1258 emit_slli(RV_REG_T2
, RV_REG_T2
, 8, ctx
);
1259 emit_srli(rd
, rd
, 8, ctx
);
1261 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1262 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1263 emit_slli(RV_REG_T2
, RV_REG_T2
, 8, ctx
);
1264 emit_srli(rd
, rd
, 8, ctx
);
1266 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1267 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1268 emit_slli(RV_REG_T2
, RV_REG_T2
, 8, ctx
);
1269 emit_srli(rd
, rd
, 8, ctx
);
1271 emit_andi(RV_REG_T1
, rd
, 0xff, ctx
);
1272 emit_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
, ctx
);
1274 emit_mv(rd
, RV_REG_T2
, ctx
);
1277 #endif /* __riscv_xlen == 64 */
1279 void bpf_jit_build_prologue(struct rv_jit_context
*ctx
, bool is_subprog
);
1280 void bpf_jit_build_epilogue(struct rv_jit_context
*ctx
);
1282 int bpf_jit_emit_insn(const struct bpf_insn
*insn
, struct rv_jit_context
*ctx
,
1285 #endif /* _BPF_JIT_H */