1 // SPDX-License-Identifier: GPL-2.0
2 /* BPF JIT compiler for RV64G
4 * Copyright(c) 2019 Björn Töpel <bjorn.topel@gmail.com>
9 #include <linux/filter.h>
10 #include <asm/cacheflush.h>
13 RV_REG_ZERO
= 0, /* The constant value 0 */
14 RV_REG_RA
= 1, /* Return address */
15 RV_REG_SP
= 2, /* Stack pointer */
16 RV_REG_GP
= 3, /* Global pointer */
17 RV_REG_TP
= 4, /* Thread pointer */
18 RV_REG_T0
= 5, /* Temporaries */
22 RV_REG_S1
= 9, /* Saved registers */
23 RV_REG_A0
= 10, /* Function argument/return values */
24 RV_REG_A1
= 11, /* Function arguments */
31 RV_REG_S2
= 18, /* Saved registers */
41 RV_REG_T3
= 28, /* Temporaries */
47 #define RV_REG_TCC RV_REG_A6
48 #define RV_REG_TCC_SAVED RV_REG_S6 /* Store A6 in S6 if program do calls */
50 static const int regmap
[] = {
51 [BPF_REG_0
] = RV_REG_A5
,
52 [BPF_REG_1
] = RV_REG_A0
,
53 [BPF_REG_2
] = RV_REG_A1
,
54 [BPF_REG_3
] = RV_REG_A2
,
55 [BPF_REG_4
] = RV_REG_A3
,
56 [BPF_REG_5
] = RV_REG_A4
,
57 [BPF_REG_6
] = RV_REG_S1
,
58 [BPF_REG_7
] = RV_REG_S2
,
59 [BPF_REG_8
] = RV_REG_S3
,
60 [BPF_REG_9
] = RV_REG_S4
,
61 [BPF_REG_FP
] = RV_REG_S5
,
62 [BPF_REG_AX
] = RV_REG_T0
,
66 RV_CTX_F_SEEN_TAIL_CALL
= 0,
67 RV_CTX_F_SEEN_CALL
= RV_REG_RA
,
68 RV_CTX_F_SEEN_S1
= RV_REG_S1
,
69 RV_CTX_F_SEEN_S2
= RV_REG_S2
,
70 RV_CTX_F_SEEN_S3
= RV_REG_S3
,
71 RV_CTX_F_SEEN_S4
= RV_REG_S4
,
72 RV_CTX_F_SEEN_S5
= RV_REG_S5
,
73 RV_CTX_F_SEEN_S6
= RV_REG_S6
,
76 struct rv_jit_context
{
77 struct bpf_prog
*prog
;
78 u32
*insns
; /* RV insns */
81 int *offset
; /* BPF to RV */
87 struct bpf_binary_header
*header
;
89 struct rv_jit_context ctx
;
92 static u8
bpf_to_rv_reg(int bpf_reg
, struct rv_jit_context
*ctx
)
94 u8 reg
= regmap
[bpf_reg
];
97 case RV_CTX_F_SEEN_S1
:
98 case RV_CTX_F_SEEN_S2
:
99 case RV_CTX_F_SEEN_S3
:
100 case RV_CTX_F_SEEN_S4
:
101 case RV_CTX_F_SEEN_S5
:
102 case RV_CTX_F_SEEN_S6
:
103 __set_bit(reg
, &ctx
->flags
);
108 static bool seen_reg(int reg
, struct rv_jit_context
*ctx
)
111 case RV_CTX_F_SEEN_CALL
:
112 case RV_CTX_F_SEEN_S1
:
113 case RV_CTX_F_SEEN_S2
:
114 case RV_CTX_F_SEEN_S3
:
115 case RV_CTX_F_SEEN_S4
:
116 case RV_CTX_F_SEEN_S5
:
117 case RV_CTX_F_SEEN_S6
:
118 return test_bit(reg
, &ctx
->flags
);
123 static void mark_fp(struct rv_jit_context
*ctx
)
125 __set_bit(RV_CTX_F_SEEN_S5
, &ctx
->flags
);
128 static void mark_call(struct rv_jit_context
*ctx
)
130 __set_bit(RV_CTX_F_SEEN_CALL
, &ctx
->flags
);
133 static bool seen_call(struct rv_jit_context
*ctx
)
135 return test_bit(RV_CTX_F_SEEN_CALL
, &ctx
->flags
);
138 static void mark_tail_call(struct rv_jit_context
*ctx
)
140 __set_bit(RV_CTX_F_SEEN_TAIL_CALL
, &ctx
->flags
);
143 static bool seen_tail_call(struct rv_jit_context
*ctx
)
145 return test_bit(RV_CTX_F_SEEN_TAIL_CALL
, &ctx
->flags
);
148 static u8
rv_tail_call_reg(struct rv_jit_context
*ctx
)
152 if (seen_call(ctx
)) {
153 __set_bit(RV_CTX_F_SEEN_S6
, &ctx
->flags
);
159 static void emit(const u32 insn
, struct rv_jit_context
*ctx
)
162 ctx
->insns
[ctx
->ninsns
] = insn
;
167 static u32
rv_r_insn(u8 funct7
, u8 rs2
, u8 rs1
, u8 funct3
, u8 rd
, u8 opcode
)
169 return (funct7
<< 25) | (rs2
<< 20) | (rs1
<< 15) | (funct3
<< 12) |
173 static u32
rv_i_insn(u16 imm11_0
, u8 rs1
, u8 funct3
, u8 rd
, u8 opcode
)
175 return (imm11_0
<< 20) | (rs1
<< 15) | (funct3
<< 12) | (rd
<< 7) |
179 static u32
rv_s_insn(u16 imm11_0
, u8 rs2
, u8 rs1
, u8 funct3
, u8 opcode
)
181 u8 imm11_5
= imm11_0
>> 5, imm4_0
= imm11_0
& 0x1f;
183 return (imm11_5
<< 25) | (rs2
<< 20) | (rs1
<< 15) | (funct3
<< 12) |
184 (imm4_0
<< 7) | opcode
;
187 static u32
rv_sb_insn(u16 imm12_1
, u8 rs2
, u8 rs1
, u8 funct3
, u8 opcode
)
189 u8 imm12
= ((imm12_1
& 0x800) >> 5) | ((imm12_1
& 0x3f0) >> 4);
190 u8 imm4_1
= ((imm12_1
& 0xf) << 1) | ((imm12_1
& 0x400) >> 10);
192 return (imm12
<< 25) | (rs2
<< 20) | (rs1
<< 15) | (funct3
<< 12) |
193 (imm4_1
<< 7) | opcode
;
196 static u32
rv_u_insn(u32 imm31_12
, u8 rd
, u8 opcode
)
198 return (imm31_12
<< 12) | (rd
<< 7) | opcode
;
201 static u32
rv_uj_insn(u32 imm20_1
, u8 rd
, u8 opcode
)
205 imm
= (imm20_1
& 0x80000) | ((imm20_1
& 0x3ff) << 9) |
206 ((imm20_1
& 0x400) >> 2) | ((imm20_1
& 0x7f800) >> 11);
208 return (imm
<< 12) | (rd
<< 7) | opcode
;
211 static u32
rv_amo_insn(u8 funct5
, u8 aq
, u8 rl
, u8 rs2
, u8 rs1
,
212 u8 funct3
, u8 rd
, u8 opcode
)
214 u8 funct7
= (funct5
<< 2) | (aq
<< 1) | rl
;
216 return rv_r_insn(funct7
, rs2
, rs1
, funct3
, rd
, opcode
);
219 static u32
rv_addiw(u8 rd
, u8 rs1
, u16 imm11_0
)
221 return rv_i_insn(imm11_0
, rs1
, 0, rd
, 0x1b);
224 static u32
rv_addi(u8 rd
, u8 rs1
, u16 imm11_0
)
226 return rv_i_insn(imm11_0
, rs1
, 0, rd
, 0x13);
229 static u32
rv_addw(u8 rd
, u8 rs1
, u8 rs2
)
231 return rv_r_insn(0, rs2
, rs1
, 0, rd
, 0x3b);
234 static u32
rv_add(u8 rd
, u8 rs1
, u8 rs2
)
236 return rv_r_insn(0, rs2
, rs1
, 0, rd
, 0x33);
239 static u32
rv_subw(u8 rd
, u8 rs1
, u8 rs2
)
241 return rv_r_insn(0x20, rs2
, rs1
, 0, rd
, 0x3b);
244 static u32
rv_sub(u8 rd
, u8 rs1
, u8 rs2
)
246 return rv_r_insn(0x20, rs2
, rs1
, 0, rd
, 0x33);
249 static u32
rv_and(u8 rd
, u8 rs1
, u8 rs2
)
251 return rv_r_insn(0, rs2
, rs1
, 7, rd
, 0x33);
254 static u32
rv_or(u8 rd
, u8 rs1
, u8 rs2
)
256 return rv_r_insn(0, rs2
, rs1
, 6, rd
, 0x33);
259 static u32
rv_xor(u8 rd
, u8 rs1
, u8 rs2
)
261 return rv_r_insn(0, rs2
, rs1
, 4, rd
, 0x33);
264 static u32
rv_mulw(u8 rd
, u8 rs1
, u8 rs2
)
266 return rv_r_insn(1, rs2
, rs1
, 0, rd
, 0x3b);
269 static u32
rv_mul(u8 rd
, u8 rs1
, u8 rs2
)
271 return rv_r_insn(1, rs2
, rs1
, 0, rd
, 0x33);
274 static u32
rv_divuw(u8 rd
, u8 rs1
, u8 rs2
)
276 return rv_r_insn(1, rs2
, rs1
, 5, rd
, 0x3b);
279 static u32
rv_divu(u8 rd
, u8 rs1
, u8 rs2
)
281 return rv_r_insn(1, rs2
, rs1
, 5, rd
, 0x33);
284 static u32
rv_remuw(u8 rd
, u8 rs1
, u8 rs2
)
286 return rv_r_insn(1, rs2
, rs1
, 7, rd
, 0x3b);
289 static u32
rv_remu(u8 rd
, u8 rs1
, u8 rs2
)
291 return rv_r_insn(1, rs2
, rs1
, 7, rd
, 0x33);
294 static u32
rv_sllw(u8 rd
, u8 rs1
, u8 rs2
)
296 return rv_r_insn(0, rs2
, rs1
, 1, rd
, 0x3b);
299 static u32
rv_sll(u8 rd
, u8 rs1
, u8 rs2
)
301 return rv_r_insn(0, rs2
, rs1
, 1, rd
, 0x33);
304 static u32
rv_srlw(u8 rd
, u8 rs1
, u8 rs2
)
306 return rv_r_insn(0, rs2
, rs1
, 5, rd
, 0x3b);
309 static u32
rv_srl(u8 rd
, u8 rs1
, u8 rs2
)
311 return rv_r_insn(0, rs2
, rs1
, 5, rd
, 0x33);
314 static u32
rv_sraw(u8 rd
, u8 rs1
, u8 rs2
)
316 return rv_r_insn(0x20, rs2
, rs1
, 5, rd
, 0x3b);
319 static u32
rv_sra(u8 rd
, u8 rs1
, u8 rs2
)
321 return rv_r_insn(0x20, rs2
, rs1
, 5, rd
, 0x33);
324 static u32
rv_lui(u8 rd
, u32 imm31_12
)
326 return rv_u_insn(imm31_12
, rd
, 0x37);
329 static u32
rv_slli(u8 rd
, u8 rs1
, u16 imm11_0
)
331 return rv_i_insn(imm11_0
, rs1
, 1, rd
, 0x13);
334 static u32
rv_andi(u8 rd
, u8 rs1
, u16 imm11_0
)
336 return rv_i_insn(imm11_0
, rs1
, 7, rd
, 0x13);
339 static u32
rv_ori(u8 rd
, u8 rs1
, u16 imm11_0
)
341 return rv_i_insn(imm11_0
, rs1
, 6, rd
, 0x13);
344 static u32
rv_xori(u8 rd
, u8 rs1
, u16 imm11_0
)
346 return rv_i_insn(imm11_0
, rs1
, 4, rd
, 0x13);
349 static u32
rv_slliw(u8 rd
, u8 rs1
, u16 imm11_0
)
351 return rv_i_insn(imm11_0
, rs1
, 1, rd
, 0x1b);
354 static u32
rv_srliw(u8 rd
, u8 rs1
, u16 imm11_0
)
356 return rv_i_insn(imm11_0
, rs1
, 5, rd
, 0x1b);
359 static u32
rv_srli(u8 rd
, u8 rs1
, u16 imm11_0
)
361 return rv_i_insn(imm11_0
, rs1
, 5, rd
, 0x13);
364 static u32
rv_sraiw(u8 rd
, u8 rs1
, u16 imm11_0
)
366 return rv_i_insn(0x400 | imm11_0
, rs1
, 5, rd
, 0x1b);
369 static u32
rv_srai(u8 rd
, u8 rs1
, u16 imm11_0
)
371 return rv_i_insn(0x400 | imm11_0
, rs1
, 5, rd
, 0x13);
374 static u32
rv_jal(u8 rd
, u32 imm20_1
)
376 return rv_uj_insn(imm20_1
, rd
, 0x6f);
379 static u32
rv_jalr(u8 rd
, u8 rs1
, u16 imm11_0
)
381 return rv_i_insn(imm11_0
, rs1
, 0, rd
, 0x67);
384 static u32
rv_beq(u8 rs1
, u8 rs2
, u16 imm12_1
)
386 return rv_sb_insn(imm12_1
, rs2
, rs1
, 0, 0x63);
389 static u32
rv_bltu(u8 rs1
, u8 rs2
, u16 imm12_1
)
391 return rv_sb_insn(imm12_1
, rs2
, rs1
, 6, 0x63);
394 static u32
rv_bgeu(u8 rs1
, u8 rs2
, u16 imm12_1
)
396 return rv_sb_insn(imm12_1
, rs2
, rs1
, 7, 0x63);
399 static u32
rv_bne(u8 rs1
, u8 rs2
, u16 imm12_1
)
401 return rv_sb_insn(imm12_1
, rs2
, rs1
, 1, 0x63);
404 static u32
rv_blt(u8 rs1
, u8 rs2
, u16 imm12_1
)
406 return rv_sb_insn(imm12_1
, rs2
, rs1
, 4, 0x63);
409 static u32
rv_bge(u8 rs1
, u8 rs2
, u16 imm12_1
)
411 return rv_sb_insn(imm12_1
, rs2
, rs1
, 5, 0x63);
414 static u32
rv_sb(u8 rs1
, u16 imm11_0
, u8 rs2
)
416 return rv_s_insn(imm11_0
, rs2
, rs1
, 0, 0x23);
419 static u32
rv_sh(u8 rs1
, u16 imm11_0
, u8 rs2
)
421 return rv_s_insn(imm11_0
, rs2
, rs1
, 1, 0x23);
424 static u32
rv_sw(u8 rs1
, u16 imm11_0
, u8 rs2
)
426 return rv_s_insn(imm11_0
, rs2
, rs1
, 2, 0x23);
429 static u32
rv_sd(u8 rs1
, u16 imm11_0
, u8 rs2
)
431 return rv_s_insn(imm11_0
, rs2
, rs1
, 3, 0x23);
434 static u32
rv_lbu(u8 rd
, u16 imm11_0
, u8 rs1
)
436 return rv_i_insn(imm11_0
, rs1
, 4, rd
, 0x03);
439 static u32
rv_lhu(u8 rd
, u16 imm11_0
, u8 rs1
)
441 return rv_i_insn(imm11_0
, rs1
, 5, rd
, 0x03);
444 static u32
rv_lwu(u8 rd
, u16 imm11_0
, u8 rs1
)
446 return rv_i_insn(imm11_0
, rs1
, 6, rd
, 0x03);
449 static u32
rv_ld(u8 rd
, u16 imm11_0
, u8 rs1
)
451 return rv_i_insn(imm11_0
, rs1
, 3, rd
, 0x03);
454 static u32
rv_amoadd_w(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
456 return rv_amo_insn(0, aq
, rl
, rs2
, rs1
, 2, rd
, 0x2f);
459 static u32
rv_amoadd_d(u8 rd
, u8 rs2
, u8 rs1
, u8 aq
, u8 rl
)
461 return rv_amo_insn(0, aq
, rl
, rs2
, rs1
, 3, rd
, 0x2f);
464 static u32
rv_auipc(u8 rd
, u32 imm31_12
)
466 return rv_u_insn(imm31_12
, rd
, 0x17);
469 static bool is_12b_int(s64 val
)
471 return -(1 << 11) <= val
&& val
< (1 << 11);
474 static bool is_13b_int(s64 val
)
476 return -(1 << 12) <= val
&& val
< (1 << 12);
479 static bool is_21b_int(s64 val
)
481 return -(1L << 20) <= val
&& val
< (1L << 20);
484 static bool is_32b_int(s64 val
)
486 return -(1L << 31) <= val
&& val
< (1L << 31);
489 static int is_12b_check(int off
, int insn
)
491 if (!is_12b_int(off
)) {
492 pr_err("bpf-jit: insn=%d 12b < offset=%d not supported yet!\n",
499 static void emit_imm(u8 rd
, s64 val
, struct rv_jit_context
*ctx
)
501 /* Note that the immediate from the add is sign-extended,
502 * which means that we need to compensate this by adding 2^12,
503 * when the 12th bit is set. A simpler way of doing this, and
504 * getting rid of the check, is to just add 2**11 before the
505 * shift. The "Loading a 32-Bit constant" example from the
506 * "Computer Organization and Design, RISC-V edition" book by
507 * Patterson/Hennessy highlights this fact.
509 * This also means that we need to process LSB to MSB.
511 s64 upper
= (val
+ (1 << 11)) >> 12, lower
= val
& 0xfff;
514 if (is_32b_int(val
)) {
516 emit(rv_lui(rd
, upper
), ctx
);
519 emit(rv_addi(rd
, RV_REG_ZERO
, lower
), ctx
);
523 emit(rv_addiw(rd
, rd
, lower
), ctx
);
527 shift
= __ffs(upper
);
531 emit_imm(rd
, upper
, ctx
);
533 emit(rv_slli(rd
, rd
, shift
), ctx
);
535 emit(rv_addi(rd
, rd
, lower
), ctx
);
538 static int rv_offset(int insn
, int off
, struct rv_jit_context
*ctx
)
542 off
++; /* BPF branch is from PC+1, RV is from PC */
543 from
= (insn
> 0) ? ctx
->offset
[insn
- 1] : 0;
544 to
= (insn
+ off
> 0) ? ctx
->offset
[insn
+ off
- 1] : 0;
545 return (to
- from
) << 2;
548 static int epilogue_offset(struct rv_jit_context
*ctx
)
550 int to
= ctx
->epilogue_offset
, from
= ctx
->ninsns
;
552 return (to
- from
) << 2;
555 static void __build_epilogue(bool is_tail_call
, struct rv_jit_context
*ctx
)
557 int stack_adjust
= ctx
->stack_size
, store_offset
= stack_adjust
- 8;
559 if (seen_reg(RV_REG_RA
, ctx
)) {
560 emit(rv_ld(RV_REG_RA
, store_offset
, RV_REG_SP
), ctx
);
563 emit(rv_ld(RV_REG_FP
, store_offset
, RV_REG_SP
), ctx
);
565 if (seen_reg(RV_REG_S1
, ctx
)) {
566 emit(rv_ld(RV_REG_S1
, store_offset
, RV_REG_SP
), ctx
);
569 if (seen_reg(RV_REG_S2
, ctx
)) {
570 emit(rv_ld(RV_REG_S2
, store_offset
, RV_REG_SP
), ctx
);
573 if (seen_reg(RV_REG_S3
, ctx
)) {
574 emit(rv_ld(RV_REG_S3
, store_offset
, RV_REG_SP
), ctx
);
577 if (seen_reg(RV_REG_S4
, ctx
)) {
578 emit(rv_ld(RV_REG_S4
, store_offset
, RV_REG_SP
), ctx
);
581 if (seen_reg(RV_REG_S5
, ctx
)) {
582 emit(rv_ld(RV_REG_S5
, store_offset
, RV_REG_SP
), ctx
);
585 if (seen_reg(RV_REG_S6
, ctx
)) {
586 emit(rv_ld(RV_REG_S6
, store_offset
, RV_REG_SP
), ctx
);
590 emit(rv_addi(RV_REG_SP
, RV_REG_SP
, stack_adjust
), ctx
);
591 /* Set return value. */
593 emit(rv_addi(RV_REG_A0
, RV_REG_A5
, 0), ctx
);
594 emit(rv_jalr(RV_REG_ZERO
, is_tail_call
? RV_REG_T3
: RV_REG_RA
,
595 is_tail_call
? 4 : 0), /* skip TCC init */
599 /* return -1 or inverted cond */
600 static int invert_bpf_cond(u8 cond
)
627 static void emit_bcc(u8 cond
, u8 rd
, u8 rs
, int rvoff
,
628 struct rv_jit_context
*ctx
)
632 emit(rv_beq(rd
, rs
, rvoff
>> 1), ctx
);
635 emit(rv_bltu(rs
, rd
, rvoff
>> 1), ctx
);
638 emit(rv_bltu(rd
, rs
, rvoff
>> 1), ctx
);
641 emit(rv_bgeu(rd
, rs
, rvoff
>> 1), ctx
);
644 emit(rv_bgeu(rs
, rd
, rvoff
>> 1), ctx
);
647 emit(rv_bne(rd
, rs
, rvoff
>> 1), ctx
);
650 emit(rv_blt(rs
, rd
, rvoff
>> 1), ctx
);
653 emit(rv_blt(rd
, rs
, rvoff
>> 1), ctx
);
656 emit(rv_bge(rd
, rs
, rvoff
>> 1), ctx
);
659 emit(rv_bge(rs
, rd
, rvoff
>> 1), ctx
);
663 static void emit_branch(u8 cond
, u8 rd
, u8 rs
, int rvoff
,
664 struct rv_jit_context
*ctx
)
668 if (is_13b_int(rvoff
)) {
669 emit_bcc(cond
, rd
, rs
, rvoff
, ctx
);
684 cond
= invert_bpf_cond(cond
);
685 if (is_21b_int(rvoff
)) {
686 emit_bcc(cond
, rd
, rs
, 8, ctx
);
687 emit(rv_jal(RV_REG_ZERO
, rvoff
>> 1), ctx
);
691 /* 32b No need for an additional rvoff adjustment, since we
692 * get that from the auipc at PC', where PC = PC' + 4.
694 upper
= (rvoff
+ (1 << 11)) >> 12;
695 lower
= rvoff
& 0xfff;
697 emit_bcc(cond
, rd
, rs
, 12, ctx
);
698 emit(rv_auipc(RV_REG_T1
, upper
), ctx
);
699 emit(rv_jalr(RV_REG_ZERO
, RV_REG_T1
, lower
), ctx
);
702 static void emit_zext_32(u8 reg
, struct rv_jit_context
*ctx
)
704 emit(rv_slli(reg
, reg
, 32), ctx
);
705 emit(rv_srli(reg
, reg
, 32), ctx
);
708 static int emit_bpf_tail_call(int insn
, struct rv_jit_context
*ctx
)
710 int tc_ninsn
, off
, start_insn
= ctx
->ninsns
;
711 u8 tcc
= rv_tail_call_reg(ctx
);
717 * if (index >= array->map.max_entries)
720 tc_ninsn
= insn
? ctx
->offset
[insn
] - ctx
->offset
[insn
- 1] :
722 emit_zext_32(RV_REG_A2
, ctx
);
724 off
= offsetof(struct bpf_array
, map
.max_entries
);
725 if (is_12b_check(off
, insn
))
727 emit(rv_lwu(RV_REG_T1
, off
, RV_REG_A1
), ctx
);
728 off
= (tc_ninsn
- (ctx
->ninsns
- start_insn
)) << 2;
729 emit_branch(BPF_JGE
, RV_REG_A2
, RV_REG_T1
, off
, ctx
);
734 emit(rv_addi(RV_REG_T1
, tcc
, -1), ctx
);
735 off
= (tc_ninsn
- (ctx
->ninsns
- start_insn
)) << 2;
736 emit_branch(BPF_JSLT
, tcc
, RV_REG_ZERO
, off
, ctx
);
738 /* prog = array->ptrs[index];
742 emit(rv_slli(RV_REG_T2
, RV_REG_A2
, 3), ctx
);
743 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_A1
), ctx
);
744 off
= offsetof(struct bpf_array
, ptrs
);
745 if (is_12b_check(off
, insn
))
747 emit(rv_ld(RV_REG_T2
, off
, RV_REG_T2
), ctx
);
748 off
= (tc_ninsn
- (ctx
->ninsns
- start_insn
)) << 2;
749 emit_branch(BPF_JEQ
, RV_REG_T2
, RV_REG_ZERO
, off
, ctx
);
751 /* goto *(prog->bpf_func + 4); */
752 off
= offsetof(struct bpf_prog
, bpf_func
);
753 if (is_12b_check(off
, insn
))
755 emit(rv_ld(RV_REG_T3
, off
, RV_REG_T2
), ctx
);
756 emit(rv_addi(RV_REG_TCC
, RV_REG_T1
, 0), ctx
);
757 __build_epilogue(true, ctx
);
761 static void init_regs(u8
*rd
, u8
*rs
, const struct bpf_insn
*insn
,
762 struct rv_jit_context
*ctx
)
764 u8 code
= insn
->code
;
767 case BPF_JMP
| BPF_JA
:
768 case BPF_JMP
| BPF_CALL
:
769 case BPF_JMP
| BPF_EXIT
:
770 case BPF_JMP
| BPF_TAIL_CALL
:
773 *rd
= bpf_to_rv_reg(insn
->dst_reg
, ctx
);
776 if (code
& (BPF_ALU
| BPF_X
) || code
& (BPF_ALU64
| BPF_X
) ||
777 code
& (BPF_JMP
| BPF_X
) || code
& (BPF_JMP32
| BPF_X
) ||
778 code
& BPF_LDX
|| code
& BPF_STX
)
779 *rs
= bpf_to_rv_reg(insn
->src_reg
, ctx
);
782 static void emit_zext_32_rd_rs(u8
*rd
, u8
*rs
, struct rv_jit_context
*ctx
)
784 emit(rv_addi(RV_REG_T2
, *rd
, 0), ctx
);
785 emit_zext_32(RV_REG_T2
, ctx
);
786 emit(rv_addi(RV_REG_T1
, *rs
, 0), ctx
);
787 emit_zext_32(RV_REG_T1
, ctx
);
792 static void emit_sext_32_rd_rs(u8
*rd
, u8
*rs
, struct rv_jit_context
*ctx
)
794 emit(rv_addiw(RV_REG_T2
, *rd
, 0), ctx
);
795 emit(rv_addiw(RV_REG_T1
, *rs
, 0), ctx
);
800 static void emit_zext_32_rd_t1(u8
*rd
, struct rv_jit_context
*ctx
)
802 emit(rv_addi(RV_REG_T2
, *rd
, 0), ctx
);
803 emit_zext_32(RV_REG_T2
, ctx
);
804 emit_zext_32(RV_REG_T1
, ctx
);
808 static void emit_sext_32_rd(u8
*rd
, struct rv_jit_context
*ctx
)
810 emit(rv_addiw(RV_REG_T2
, *rd
, 0), ctx
);
814 static void emit_jump_and_link(u8 rd
, s64 rvoff
, bool force_jalr
,
815 struct rv_jit_context
*ctx
)
819 if (rvoff
&& is_21b_int(rvoff
) && !force_jalr
) {
820 emit(rv_jal(rd
, rvoff
>> 1), ctx
);
824 upper
= (rvoff
+ (1 << 11)) >> 12;
825 lower
= rvoff
& 0xfff;
826 emit(rv_auipc(RV_REG_T1
, upper
), ctx
);
827 emit(rv_jalr(rd
, RV_REG_T1
, lower
), ctx
);
830 static bool is_signed_bpf_cond(u8 cond
)
832 return cond
== BPF_JSGT
|| cond
== BPF_JSLT
||
833 cond
== BPF_JSGE
|| cond
== BPF_JSLE
;
836 static int emit_call(bool fixed
, u64 addr
, struct rv_jit_context
*ctx
)
842 if (addr
&& ctx
->insns
) {
843 ip
= (u64
)(long)(ctx
->insns
+ ctx
->ninsns
);
845 if (!is_32b_int(off
)) {
846 pr_err("bpf-jit: target call addr %pK is out of range\n",
852 emit_jump_and_link(RV_REG_RA
, off
, !fixed
, ctx
);
853 rd
= bpf_to_rv_reg(BPF_REG_0
, ctx
);
854 emit(rv_addi(rd
, RV_REG_A0
, 0), ctx
);
858 static int emit_insn(const struct bpf_insn
*insn
, struct rv_jit_context
*ctx
,
861 bool is64
= BPF_CLASS(insn
->code
) == BPF_ALU64
||
862 BPF_CLASS(insn
->code
) == BPF_JMP
;
863 int s
, e
, rvoff
, i
= insn
- ctx
->prog
->insnsi
;
864 struct bpf_prog_aux
*aux
= ctx
->prog
->aux
;
865 u8 rd
= -1, rs
= -1, code
= insn
->code
;
869 init_regs(&rd
, &rs
, insn
, ctx
);
873 case BPF_ALU
| BPF_MOV
| BPF_X
:
874 case BPF_ALU64
| BPF_MOV
| BPF_X
:
876 /* Special mov32 for zext */
877 emit_zext_32(rd
, ctx
);
880 emit(is64
? rv_addi(rd
, rs
, 0) : rv_addiw(rd
, rs
, 0), ctx
);
881 if (!is64
&& !aux
->verifier_zext
)
882 emit_zext_32(rd
, ctx
);
885 /* dst = dst OP src */
886 case BPF_ALU
| BPF_ADD
| BPF_X
:
887 case BPF_ALU64
| BPF_ADD
| BPF_X
:
888 emit(is64
? rv_add(rd
, rd
, rs
) : rv_addw(rd
, rd
, rs
), ctx
);
889 if (!is64
&& !aux
->verifier_zext
)
890 emit_zext_32(rd
, ctx
);
892 case BPF_ALU
| BPF_SUB
| BPF_X
:
893 case BPF_ALU64
| BPF_SUB
| BPF_X
:
894 emit(is64
? rv_sub(rd
, rd
, rs
) : rv_subw(rd
, rd
, rs
), ctx
);
895 if (!is64
&& !aux
->verifier_zext
)
896 emit_zext_32(rd
, ctx
);
898 case BPF_ALU
| BPF_AND
| BPF_X
:
899 case BPF_ALU64
| BPF_AND
| BPF_X
:
900 emit(rv_and(rd
, rd
, rs
), ctx
);
901 if (!is64
&& !aux
->verifier_zext
)
902 emit_zext_32(rd
, ctx
);
904 case BPF_ALU
| BPF_OR
| BPF_X
:
905 case BPF_ALU64
| BPF_OR
| BPF_X
:
906 emit(rv_or(rd
, rd
, rs
), ctx
);
907 if (!is64
&& !aux
->verifier_zext
)
908 emit_zext_32(rd
, ctx
);
910 case BPF_ALU
| BPF_XOR
| BPF_X
:
911 case BPF_ALU64
| BPF_XOR
| BPF_X
:
912 emit(rv_xor(rd
, rd
, rs
), ctx
);
913 if (!is64
&& !aux
->verifier_zext
)
914 emit_zext_32(rd
, ctx
);
916 case BPF_ALU
| BPF_MUL
| BPF_X
:
917 case BPF_ALU64
| BPF_MUL
| BPF_X
:
918 emit(is64
? rv_mul(rd
, rd
, rs
) : rv_mulw(rd
, rd
, rs
), ctx
);
919 if (!is64
&& !aux
->verifier_zext
)
920 emit_zext_32(rd
, ctx
);
922 case BPF_ALU
| BPF_DIV
| BPF_X
:
923 case BPF_ALU64
| BPF_DIV
| BPF_X
:
924 emit(is64
? rv_divu(rd
, rd
, rs
) : rv_divuw(rd
, rd
, rs
), ctx
);
925 if (!is64
&& !aux
->verifier_zext
)
926 emit_zext_32(rd
, ctx
);
928 case BPF_ALU
| BPF_MOD
| BPF_X
:
929 case BPF_ALU64
| BPF_MOD
| BPF_X
:
930 emit(is64
? rv_remu(rd
, rd
, rs
) : rv_remuw(rd
, rd
, rs
), ctx
);
931 if (!is64
&& !aux
->verifier_zext
)
932 emit_zext_32(rd
, ctx
);
934 case BPF_ALU
| BPF_LSH
| BPF_X
:
935 case BPF_ALU64
| BPF_LSH
| BPF_X
:
936 emit(is64
? rv_sll(rd
, rd
, rs
) : rv_sllw(rd
, rd
, rs
), ctx
);
938 emit_zext_32(rd
, ctx
);
940 case BPF_ALU
| BPF_RSH
| BPF_X
:
941 case BPF_ALU64
| BPF_RSH
| BPF_X
:
942 emit(is64
? rv_srl(rd
, rd
, rs
) : rv_srlw(rd
, rd
, rs
), ctx
);
943 if (!is64
&& !aux
->verifier_zext
)
944 emit_zext_32(rd
, ctx
);
946 case BPF_ALU
| BPF_ARSH
| BPF_X
:
947 case BPF_ALU64
| BPF_ARSH
| BPF_X
:
948 emit(is64
? rv_sra(rd
, rd
, rs
) : rv_sraw(rd
, rd
, rs
), ctx
);
949 if (!is64
&& !aux
->verifier_zext
)
950 emit_zext_32(rd
, ctx
);
954 case BPF_ALU
| BPF_NEG
:
955 case BPF_ALU64
| BPF_NEG
:
956 emit(is64
? rv_sub(rd
, RV_REG_ZERO
, rd
) :
957 rv_subw(rd
, RV_REG_ZERO
, rd
), ctx
);
958 if (!is64
&& !aux
->verifier_zext
)
959 emit_zext_32(rd
, ctx
);
962 /* dst = BSWAP##imm(dst) */
963 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
965 int shift
= 64 - imm
;
967 emit(rv_slli(rd
, rd
, shift
), ctx
);
968 emit(rv_srli(rd
, rd
, shift
), ctx
);
971 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
972 emit(rv_addi(RV_REG_T2
, RV_REG_ZERO
, 0), ctx
);
974 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
975 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
976 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
977 emit(rv_srli(rd
, rd
, 8), ctx
);
981 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
982 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
983 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
984 emit(rv_srli(rd
, rd
, 8), ctx
);
986 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
987 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
988 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
989 emit(rv_srli(rd
, rd
, 8), ctx
);
993 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
994 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
995 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
996 emit(rv_srli(rd
, rd
, 8), ctx
);
998 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
999 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
1000 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
1001 emit(rv_srli(rd
, rd
, 8), ctx
);
1003 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
1004 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
1005 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
1006 emit(rv_srli(rd
, rd
, 8), ctx
);
1008 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
1009 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
1010 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
1011 emit(rv_srli(rd
, rd
, 8), ctx
);
1013 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
1014 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
1016 emit(rv_addi(rd
, RV_REG_T2
, 0), ctx
);
1020 case BPF_ALU
| BPF_MOV
| BPF_K
:
1021 case BPF_ALU64
| BPF_MOV
| BPF_K
:
1022 emit_imm(rd
, imm
, ctx
);
1023 if (!is64
&& !aux
->verifier_zext
)
1024 emit_zext_32(rd
, ctx
);
1027 /* dst = dst OP imm */
1028 case BPF_ALU
| BPF_ADD
| BPF_K
:
1029 case BPF_ALU64
| BPF_ADD
| BPF_K
:
1030 if (is_12b_int(imm
)) {
1031 emit(is64
? rv_addi(rd
, rd
, imm
) :
1032 rv_addiw(rd
, rd
, imm
), ctx
);
1034 emit_imm(RV_REG_T1
, imm
, ctx
);
1035 emit(is64
? rv_add(rd
, rd
, RV_REG_T1
) :
1036 rv_addw(rd
, rd
, RV_REG_T1
), ctx
);
1038 if (!is64
&& !aux
->verifier_zext
)
1039 emit_zext_32(rd
, ctx
);
1041 case BPF_ALU
| BPF_SUB
| BPF_K
:
1042 case BPF_ALU64
| BPF_SUB
| BPF_K
:
1043 if (is_12b_int(-imm
)) {
1044 emit(is64
? rv_addi(rd
, rd
, -imm
) :
1045 rv_addiw(rd
, rd
, -imm
), ctx
);
1047 emit_imm(RV_REG_T1
, imm
, ctx
);
1048 emit(is64
? rv_sub(rd
, rd
, RV_REG_T1
) :
1049 rv_subw(rd
, rd
, RV_REG_T1
), ctx
);
1051 if (!is64
&& !aux
->verifier_zext
)
1052 emit_zext_32(rd
, ctx
);
1054 case BPF_ALU
| BPF_AND
| BPF_K
:
1055 case BPF_ALU64
| BPF_AND
| BPF_K
:
1056 if (is_12b_int(imm
)) {
1057 emit(rv_andi(rd
, rd
, imm
), ctx
);
1059 emit_imm(RV_REG_T1
, imm
, ctx
);
1060 emit(rv_and(rd
, rd
, RV_REG_T1
), ctx
);
1062 if (!is64
&& !aux
->verifier_zext
)
1063 emit_zext_32(rd
, ctx
);
1065 case BPF_ALU
| BPF_OR
| BPF_K
:
1066 case BPF_ALU64
| BPF_OR
| BPF_K
:
1067 if (is_12b_int(imm
)) {
1068 emit(rv_ori(rd
, rd
, imm
), ctx
);
1070 emit_imm(RV_REG_T1
, imm
, ctx
);
1071 emit(rv_or(rd
, rd
, RV_REG_T1
), ctx
);
1073 if (!is64
&& !aux
->verifier_zext
)
1074 emit_zext_32(rd
, ctx
);
1076 case BPF_ALU
| BPF_XOR
| BPF_K
:
1077 case BPF_ALU64
| BPF_XOR
| BPF_K
:
1078 if (is_12b_int(imm
)) {
1079 emit(rv_xori(rd
, rd
, imm
), ctx
);
1081 emit_imm(RV_REG_T1
, imm
, ctx
);
1082 emit(rv_xor(rd
, rd
, RV_REG_T1
), ctx
);
1084 if (!is64
&& !aux
->verifier_zext
)
1085 emit_zext_32(rd
, ctx
);
1087 case BPF_ALU
| BPF_MUL
| BPF_K
:
1088 case BPF_ALU64
| BPF_MUL
| BPF_K
:
1089 emit_imm(RV_REG_T1
, imm
, ctx
);
1090 emit(is64
? rv_mul(rd
, rd
, RV_REG_T1
) :
1091 rv_mulw(rd
, rd
, RV_REG_T1
), ctx
);
1092 if (!is64
&& !aux
->verifier_zext
)
1093 emit_zext_32(rd
, ctx
);
1095 case BPF_ALU
| BPF_DIV
| BPF_K
:
1096 case BPF_ALU64
| BPF_DIV
| BPF_K
:
1097 emit_imm(RV_REG_T1
, imm
, ctx
);
1098 emit(is64
? rv_divu(rd
, rd
, RV_REG_T1
) :
1099 rv_divuw(rd
, rd
, RV_REG_T1
), ctx
);
1100 if (!is64
&& !aux
->verifier_zext
)
1101 emit_zext_32(rd
, ctx
);
1103 case BPF_ALU
| BPF_MOD
| BPF_K
:
1104 case BPF_ALU64
| BPF_MOD
| BPF_K
:
1105 emit_imm(RV_REG_T1
, imm
, ctx
);
1106 emit(is64
? rv_remu(rd
, rd
, RV_REG_T1
) :
1107 rv_remuw(rd
, rd
, RV_REG_T1
), ctx
);
1108 if (!is64
&& !aux
->verifier_zext
)
1109 emit_zext_32(rd
, ctx
);
1111 case BPF_ALU
| BPF_LSH
| BPF_K
:
1112 case BPF_ALU64
| BPF_LSH
| BPF_K
:
1113 emit(is64
? rv_slli(rd
, rd
, imm
) : rv_slliw(rd
, rd
, imm
), ctx
);
1115 emit_zext_32(rd
, ctx
);
1117 case BPF_ALU
| BPF_RSH
| BPF_K
:
1118 case BPF_ALU64
| BPF_RSH
| BPF_K
:
1119 emit(is64
? rv_srli(rd
, rd
, imm
) : rv_srliw(rd
, rd
, imm
), ctx
);
1121 emit_zext_32(rd
, ctx
);
1123 case BPF_ALU
| BPF_ARSH
| BPF_K
:
1124 case BPF_ALU64
| BPF_ARSH
| BPF_K
:
1125 emit(is64
? rv_srai(rd
, rd
, imm
) : rv_sraiw(rd
, rd
, imm
), ctx
);
1127 emit_zext_32(rd
, ctx
);
1131 case BPF_JMP
| BPF_JA
:
1132 rvoff
= rv_offset(i
, off
, ctx
);
1133 emit_jump_and_link(RV_REG_ZERO
, rvoff
, false, ctx
);
1136 /* IF (dst COND src) JUMP off */
1137 case BPF_JMP
| BPF_JEQ
| BPF_X
:
1138 case BPF_JMP32
| BPF_JEQ
| BPF_X
:
1139 case BPF_JMP
| BPF_JGT
| BPF_X
:
1140 case BPF_JMP32
| BPF_JGT
| BPF_X
:
1141 case BPF_JMP
| BPF_JLT
| BPF_X
:
1142 case BPF_JMP32
| BPF_JLT
| BPF_X
:
1143 case BPF_JMP
| BPF_JGE
| BPF_X
:
1144 case BPF_JMP32
| BPF_JGE
| BPF_X
:
1145 case BPF_JMP
| BPF_JLE
| BPF_X
:
1146 case BPF_JMP32
| BPF_JLE
| BPF_X
:
1147 case BPF_JMP
| BPF_JNE
| BPF_X
:
1148 case BPF_JMP32
| BPF_JNE
| BPF_X
:
1149 case BPF_JMP
| BPF_JSGT
| BPF_X
:
1150 case BPF_JMP32
| BPF_JSGT
| BPF_X
:
1151 case BPF_JMP
| BPF_JSLT
| BPF_X
:
1152 case BPF_JMP32
| BPF_JSLT
| BPF_X
:
1153 case BPF_JMP
| BPF_JSGE
| BPF_X
:
1154 case BPF_JMP32
| BPF_JSGE
| BPF_X
:
1155 case BPF_JMP
| BPF_JSLE
| BPF_X
:
1156 case BPF_JMP32
| BPF_JSLE
| BPF_X
:
1157 case BPF_JMP
| BPF_JSET
| BPF_X
:
1158 case BPF_JMP32
| BPF_JSET
| BPF_X
:
1159 rvoff
= rv_offset(i
, off
, ctx
);
1162 if (is_signed_bpf_cond(BPF_OP(code
)))
1163 emit_sext_32_rd_rs(&rd
, &rs
, ctx
);
1165 emit_zext_32_rd_rs(&rd
, &rs
, ctx
);
1168 /* Adjust for extra insns */
1169 rvoff
-= (e
- s
) << 2;
1172 if (BPF_OP(code
) == BPF_JSET
) {
1173 /* Adjust for and */
1175 emit(rv_and(RV_REG_T1
, rd
, rs
), ctx
);
1176 emit_branch(BPF_JNE
, RV_REG_T1
, RV_REG_ZERO
, rvoff
,
1179 emit_branch(BPF_OP(code
), rd
, rs
, rvoff
, ctx
);
1183 /* IF (dst COND imm) JUMP off */
1184 case BPF_JMP
| BPF_JEQ
| BPF_K
:
1185 case BPF_JMP32
| BPF_JEQ
| BPF_K
:
1186 case BPF_JMP
| BPF_JGT
| BPF_K
:
1187 case BPF_JMP32
| BPF_JGT
| BPF_K
:
1188 case BPF_JMP
| BPF_JLT
| BPF_K
:
1189 case BPF_JMP32
| BPF_JLT
| BPF_K
:
1190 case BPF_JMP
| BPF_JGE
| BPF_K
:
1191 case BPF_JMP32
| BPF_JGE
| BPF_K
:
1192 case BPF_JMP
| BPF_JLE
| BPF_K
:
1193 case BPF_JMP32
| BPF_JLE
| BPF_K
:
1194 case BPF_JMP
| BPF_JNE
| BPF_K
:
1195 case BPF_JMP32
| BPF_JNE
| BPF_K
:
1196 case BPF_JMP
| BPF_JSGT
| BPF_K
:
1197 case BPF_JMP32
| BPF_JSGT
| BPF_K
:
1198 case BPF_JMP
| BPF_JSLT
| BPF_K
:
1199 case BPF_JMP32
| BPF_JSLT
| BPF_K
:
1200 case BPF_JMP
| BPF_JSGE
| BPF_K
:
1201 case BPF_JMP32
| BPF_JSGE
| BPF_K
:
1202 case BPF_JMP
| BPF_JSLE
| BPF_K
:
1203 case BPF_JMP32
| BPF_JSLE
| BPF_K
:
1204 case BPF_JMP
| BPF_JSET
| BPF_K
:
1205 case BPF_JMP32
| BPF_JSET
| BPF_K
:
1206 rvoff
= rv_offset(i
, off
, ctx
);
1208 emit_imm(RV_REG_T1
, imm
, ctx
);
1210 if (is_signed_bpf_cond(BPF_OP(code
)))
1211 emit_sext_32_rd(&rd
, ctx
);
1213 emit_zext_32_rd_t1(&rd
, ctx
);
1217 /* Adjust for extra insns */
1218 rvoff
-= (e
- s
) << 2;
1220 if (BPF_OP(code
) == BPF_JSET
) {
1221 /* Adjust for and */
1223 emit(rv_and(RV_REG_T1
, rd
, RV_REG_T1
), ctx
);
1224 emit_branch(BPF_JNE
, RV_REG_T1
, RV_REG_ZERO
, rvoff
,
1227 emit_branch(BPF_OP(code
), rd
, RV_REG_T1
, rvoff
, ctx
);
1232 case BPF_JMP
| BPF_CALL
:
1239 ret
= bpf_jit_get_func_addr(ctx
->prog
, insn
, extra_pass
, &addr
,
1243 ret
= emit_call(fixed
, addr
, ctx
);
1249 case BPF_JMP
| BPF_TAIL_CALL
:
1250 if (emit_bpf_tail_call(i
, ctx
))
1254 /* function return */
1255 case BPF_JMP
| BPF_EXIT
:
1256 if (i
== ctx
->prog
->len
- 1)
1259 rvoff
= epilogue_offset(ctx
);
1260 emit_jump_and_link(RV_REG_ZERO
, rvoff
, false, ctx
);
1264 case BPF_LD
| BPF_IMM
| BPF_DW
:
1266 struct bpf_insn insn1
= insn
[1];
1269 imm64
= (u64
)insn1
.imm
<< 32 | (u32
)imm
;
1270 emit_imm(rd
, imm64
, ctx
);
1274 /* LDX: dst = *(size *)(src + off) */
1275 case BPF_LDX
| BPF_MEM
| BPF_B
:
1276 if (is_12b_int(off
)) {
1277 emit(rv_lbu(rd
, off
, rs
), ctx
);
1281 emit_imm(RV_REG_T1
, off
, ctx
);
1282 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
1283 emit(rv_lbu(rd
, 0, RV_REG_T1
), ctx
);
1284 if (insn_is_zext(&insn
[1]))
1287 case BPF_LDX
| BPF_MEM
| BPF_H
:
1288 if (is_12b_int(off
)) {
1289 emit(rv_lhu(rd
, off
, rs
), ctx
);
1293 emit_imm(RV_REG_T1
, off
, ctx
);
1294 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
1295 emit(rv_lhu(rd
, 0, RV_REG_T1
), ctx
);
1296 if (insn_is_zext(&insn
[1]))
1299 case BPF_LDX
| BPF_MEM
| BPF_W
:
1300 if (is_12b_int(off
)) {
1301 emit(rv_lwu(rd
, off
, rs
), ctx
);
1305 emit_imm(RV_REG_T1
, off
, ctx
);
1306 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
1307 emit(rv_lwu(rd
, 0, RV_REG_T1
), ctx
);
1308 if (insn_is_zext(&insn
[1]))
1311 case BPF_LDX
| BPF_MEM
| BPF_DW
:
1312 if (is_12b_int(off
)) {
1313 emit(rv_ld(rd
, off
, rs
), ctx
);
1317 emit_imm(RV_REG_T1
, off
, ctx
);
1318 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
1319 emit(rv_ld(rd
, 0, RV_REG_T1
), ctx
);
1322 /* ST: *(size *)(dst + off) = imm */
1323 case BPF_ST
| BPF_MEM
| BPF_B
:
1324 emit_imm(RV_REG_T1
, imm
, ctx
);
1325 if (is_12b_int(off
)) {
1326 emit(rv_sb(rd
, off
, RV_REG_T1
), ctx
);
1330 emit_imm(RV_REG_T2
, off
, ctx
);
1331 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
1332 emit(rv_sb(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
1335 case BPF_ST
| BPF_MEM
| BPF_H
:
1336 emit_imm(RV_REG_T1
, imm
, ctx
);
1337 if (is_12b_int(off
)) {
1338 emit(rv_sh(rd
, off
, RV_REG_T1
), ctx
);
1342 emit_imm(RV_REG_T2
, off
, ctx
);
1343 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
1344 emit(rv_sh(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
1346 case BPF_ST
| BPF_MEM
| BPF_W
:
1347 emit_imm(RV_REG_T1
, imm
, ctx
);
1348 if (is_12b_int(off
)) {
1349 emit(rv_sw(rd
, off
, RV_REG_T1
), ctx
);
1353 emit_imm(RV_REG_T2
, off
, ctx
);
1354 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
1355 emit(rv_sw(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
1357 case BPF_ST
| BPF_MEM
| BPF_DW
:
1358 emit_imm(RV_REG_T1
, imm
, ctx
);
1359 if (is_12b_int(off
)) {
1360 emit(rv_sd(rd
, off
, RV_REG_T1
), ctx
);
1364 emit_imm(RV_REG_T2
, off
, ctx
);
1365 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
1366 emit(rv_sd(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
1369 /* STX: *(size *)(dst + off) = src */
1370 case BPF_STX
| BPF_MEM
| BPF_B
:
1371 if (is_12b_int(off
)) {
1372 emit(rv_sb(rd
, off
, rs
), ctx
);
1376 emit_imm(RV_REG_T1
, off
, ctx
);
1377 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
1378 emit(rv_sb(RV_REG_T1
, 0, rs
), ctx
);
1380 case BPF_STX
| BPF_MEM
| BPF_H
:
1381 if (is_12b_int(off
)) {
1382 emit(rv_sh(rd
, off
, rs
), ctx
);
1386 emit_imm(RV_REG_T1
, off
, ctx
);
1387 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
1388 emit(rv_sh(RV_REG_T1
, 0, rs
), ctx
);
1390 case BPF_STX
| BPF_MEM
| BPF_W
:
1391 if (is_12b_int(off
)) {
1392 emit(rv_sw(rd
, off
, rs
), ctx
);
1396 emit_imm(RV_REG_T1
, off
, ctx
);
1397 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
1398 emit(rv_sw(RV_REG_T1
, 0, rs
), ctx
);
1400 case BPF_STX
| BPF_MEM
| BPF_DW
:
1401 if (is_12b_int(off
)) {
1402 emit(rv_sd(rd
, off
, rs
), ctx
);
1406 emit_imm(RV_REG_T1
, off
, ctx
);
1407 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
1408 emit(rv_sd(RV_REG_T1
, 0, rs
), ctx
);
1410 /* STX XADD: lock *(u32 *)(dst + off) += src */
1411 case BPF_STX
| BPF_XADD
| BPF_W
:
1412 /* STX XADD: lock *(u64 *)(dst + off) += src */
1413 case BPF_STX
| BPF_XADD
| BPF_DW
:
1415 if (is_12b_int(off
)) {
1416 emit(rv_addi(RV_REG_T1
, rd
, off
), ctx
);
1418 emit_imm(RV_REG_T1
, off
, ctx
);
1419 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
1425 emit(BPF_SIZE(code
) == BPF_W
?
1426 rv_amoadd_w(RV_REG_ZERO
, rs
, rd
, 0, 0) :
1427 rv_amoadd_d(RV_REG_ZERO
, rs
, rd
, 0, 0), ctx
);
1430 pr_err("bpf-jit: unknown opcode %02x\n", code
);
1437 static void build_prologue(struct rv_jit_context
*ctx
)
1439 int stack_adjust
= 0, store_offset
, bpf_stack_adjust
;
1441 bpf_stack_adjust
= round_up(ctx
->prog
->aux
->stack_depth
, 16);
1442 if (bpf_stack_adjust
)
1445 if (seen_reg(RV_REG_RA
, ctx
))
1447 stack_adjust
+= 8; /* RV_REG_FP */
1448 if (seen_reg(RV_REG_S1
, ctx
))
1450 if (seen_reg(RV_REG_S2
, ctx
))
1452 if (seen_reg(RV_REG_S3
, ctx
))
1454 if (seen_reg(RV_REG_S4
, ctx
))
1456 if (seen_reg(RV_REG_S5
, ctx
))
1458 if (seen_reg(RV_REG_S6
, ctx
))
1461 stack_adjust
= round_up(stack_adjust
, 16);
1462 stack_adjust
+= bpf_stack_adjust
;
1464 store_offset
= stack_adjust
- 8;
1466 /* First instruction is always setting the tail-call-counter
1467 * (TCC) register. This instruction is skipped for tail calls.
1469 emit(rv_addi(RV_REG_TCC
, RV_REG_ZERO
, MAX_TAIL_CALL_CNT
), ctx
);
1471 emit(rv_addi(RV_REG_SP
, RV_REG_SP
, -stack_adjust
), ctx
);
1473 if (seen_reg(RV_REG_RA
, ctx
)) {
1474 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_RA
), ctx
);
1477 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_FP
), ctx
);
1479 if (seen_reg(RV_REG_S1
, ctx
)) {
1480 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S1
), ctx
);
1483 if (seen_reg(RV_REG_S2
, ctx
)) {
1484 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S2
), ctx
);
1487 if (seen_reg(RV_REG_S3
, ctx
)) {
1488 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S3
), ctx
);
1491 if (seen_reg(RV_REG_S4
, ctx
)) {
1492 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S4
), ctx
);
1495 if (seen_reg(RV_REG_S5
, ctx
)) {
1496 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S5
), ctx
);
1499 if (seen_reg(RV_REG_S6
, ctx
)) {
1500 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S6
), ctx
);
1504 emit(rv_addi(RV_REG_FP
, RV_REG_SP
, stack_adjust
), ctx
);
1506 if (bpf_stack_adjust
)
1507 emit(rv_addi(RV_REG_S5
, RV_REG_SP
, bpf_stack_adjust
), ctx
);
1509 /* Program contains calls and tail calls, so RV_REG_TCC need
1510 * to be saved across calls.
1512 if (seen_tail_call(ctx
) && seen_call(ctx
))
1513 emit(rv_addi(RV_REG_TCC_SAVED
, RV_REG_TCC
, 0), ctx
);
1515 ctx
->stack_size
= stack_adjust
;
1518 static void build_epilogue(struct rv_jit_context
*ctx
)
1520 __build_epilogue(false, ctx
);
1523 static int build_body(struct rv_jit_context
*ctx
, bool extra_pass
, int *offset
)
1525 const struct bpf_prog
*prog
= ctx
->prog
;
1528 for (i
= 0; i
< prog
->len
; i
++) {
1529 const struct bpf_insn
*insn
= &prog
->insnsi
[i
];
1532 ret
= emit_insn(insn
, ctx
, extra_pass
);
1536 offset
[i
] = ctx
->ninsns
;
1540 offset
[i
] = ctx
->ninsns
;
1547 static void bpf_fill_ill_insns(void *area
, unsigned int size
)
1549 memset(area
, 0, size
);
1552 static void bpf_flush_icache(void *start
, void *end
)
1554 flush_icache_range((unsigned long)start
, (unsigned long)end
);
1557 bool bpf_jit_needs_zext(void)
1562 struct bpf_prog
*bpf_int_jit_compile(struct bpf_prog
*prog
)
1564 bool tmp_blinded
= false, extra_pass
= false;
1565 struct bpf_prog
*tmp
, *orig_prog
= prog
;
1566 int pass
= 0, prev_ninsns
= 0, i
;
1567 struct rv_jit_data
*jit_data
;
1568 unsigned int image_size
= 0;
1569 struct rv_jit_context
*ctx
;
1571 if (!prog
->jit_requested
)
1574 tmp
= bpf_jit_blind_constants(prog
);
1582 jit_data
= prog
->aux
->jit_data
;
1584 jit_data
= kzalloc(sizeof(*jit_data
), GFP_KERNEL
);
1589 prog
->aux
->jit_data
= jit_data
;
1592 ctx
= &jit_data
->ctx
;
1596 image_size
= sizeof(u32
) * ctx
->ninsns
;
1601 ctx
->offset
= kcalloc(prog
->len
, sizeof(int), GFP_KERNEL
);
1606 for (i
= 0; i
< prog
->len
; i
++) {
1608 ctx
->offset
[i
] = prev_ninsns
;
1611 for (i
= 0; i
< 16; i
++) {
1614 if (build_body(ctx
, extra_pass
, ctx
->offset
)) {
1618 build_prologue(ctx
);
1619 ctx
->epilogue_offset
= ctx
->ninsns
;
1620 build_epilogue(ctx
);
1622 if (ctx
->ninsns
== prev_ninsns
) {
1623 if (jit_data
->header
)
1626 image_size
= sizeof(u32
) * ctx
->ninsns
;
1628 bpf_jit_binary_alloc(image_size
,
1631 bpf_fill_ill_insns
);
1632 if (!jit_data
->header
) {
1637 ctx
->insns
= (u32
*)jit_data
->image
;
1638 /* Now, when the image is allocated, the image
1639 * can potentially shrink more (auipc/jalr ->
1643 prev_ninsns
= ctx
->ninsns
;
1647 pr_err("bpf-jit: image did not converge in <%d passes!\n", i
);
1648 bpf_jit_binary_free(jit_data
->header
);
1657 build_prologue(ctx
);
1658 if (build_body(ctx
, extra_pass
, NULL
)) {
1659 bpf_jit_binary_free(jit_data
->header
);
1663 build_epilogue(ctx
);
1665 if (bpf_jit_enable
> 1)
1666 bpf_jit_dump(prog
->len
, image_size
, pass
, ctx
->insns
);
1668 prog
->bpf_func
= (void *)ctx
->insns
;
1670 prog
->jited_len
= image_size
;
1672 bpf_flush_icache(jit_data
->header
, ctx
->insns
+ ctx
->ninsns
);
1674 if (!prog
->is_func
|| extra_pass
) {
1678 prog
->aux
->jit_data
= NULL
;
1682 bpf_jit_prog_release_other(prog
, prog
== orig_prog
?
1687 void *bpf_jit_alloc_exec(unsigned long size
)
1689 return __vmalloc_node_range(size
, PAGE_SIZE
, BPF_JIT_REGION_START
,
1690 BPF_JIT_REGION_END
, GFP_KERNEL
,
1691 PAGE_KERNEL_EXEC
, 0, NUMA_NO_NODE
,
1692 __builtin_return_address(0));
1695 void bpf_jit_free_exec(void *addr
)