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>
12 #define RV_REG_TCC RV_REG_A6
13 #define RV_REG_TCC_SAVED RV_REG_S6 /* Store A6 in S6 if program do calls */
15 static const int regmap
[] = {
16 [BPF_REG_0
] = RV_REG_A5
,
17 [BPF_REG_1
] = RV_REG_A0
,
18 [BPF_REG_2
] = RV_REG_A1
,
19 [BPF_REG_3
] = RV_REG_A2
,
20 [BPF_REG_4
] = RV_REG_A3
,
21 [BPF_REG_5
] = RV_REG_A4
,
22 [BPF_REG_6
] = RV_REG_S1
,
23 [BPF_REG_7
] = RV_REG_S2
,
24 [BPF_REG_8
] = RV_REG_S3
,
25 [BPF_REG_9
] = RV_REG_S4
,
26 [BPF_REG_FP
] = RV_REG_S5
,
27 [BPF_REG_AX
] = RV_REG_T0
,
31 RV_CTX_F_SEEN_TAIL_CALL
= 0,
32 RV_CTX_F_SEEN_CALL
= RV_REG_RA
,
33 RV_CTX_F_SEEN_S1
= RV_REG_S1
,
34 RV_CTX_F_SEEN_S2
= RV_REG_S2
,
35 RV_CTX_F_SEEN_S3
= RV_REG_S3
,
36 RV_CTX_F_SEEN_S4
= RV_REG_S4
,
37 RV_CTX_F_SEEN_S5
= RV_REG_S5
,
38 RV_CTX_F_SEEN_S6
= RV_REG_S6
,
41 static u8
bpf_to_rv_reg(int bpf_reg
, struct rv_jit_context
*ctx
)
43 u8 reg
= regmap
[bpf_reg
];
46 case RV_CTX_F_SEEN_S1
:
47 case RV_CTX_F_SEEN_S2
:
48 case RV_CTX_F_SEEN_S3
:
49 case RV_CTX_F_SEEN_S4
:
50 case RV_CTX_F_SEEN_S5
:
51 case RV_CTX_F_SEEN_S6
:
52 __set_bit(reg
, &ctx
->flags
);
57 static bool seen_reg(int reg
, struct rv_jit_context
*ctx
)
60 case RV_CTX_F_SEEN_CALL
:
61 case RV_CTX_F_SEEN_S1
:
62 case RV_CTX_F_SEEN_S2
:
63 case RV_CTX_F_SEEN_S3
:
64 case RV_CTX_F_SEEN_S4
:
65 case RV_CTX_F_SEEN_S5
:
66 case RV_CTX_F_SEEN_S6
:
67 return test_bit(reg
, &ctx
->flags
);
72 static void mark_fp(struct rv_jit_context
*ctx
)
74 __set_bit(RV_CTX_F_SEEN_S5
, &ctx
->flags
);
77 static void mark_call(struct rv_jit_context
*ctx
)
79 __set_bit(RV_CTX_F_SEEN_CALL
, &ctx
->flags
);
82 static bool seen_call(struct rv_jit_context
*ctx
)
84 return test_bit(RV_CTX_F_SEEN_CALL
, &ctx
->flags
);
87 static void mark_tail_call(struct rv_jit_context
*ctx
)
89 __set_bit(RV_CTX_F_SEEN_TAIL_CALL
, &ctx
->flags
);
92 static bool seen_tail_call(struct rv_jit_context
*ctx
)
94 return test_bit(RV_CTX_F_SEEN_TAIL_CALL
, &ctx
->flags
);
97 static u8
rv_tail_call_reg(struct rv_jit_context
*ctx
)
101 if (seen_call(ctx
)) {
102 __set_bit(RV_CTX_F_SEEN_S6
, &ctx
->flags
);
108 static bool is_32b_int(s64 val
)
110 return -(1L << 31) <= val
&& val
< (1L << 31);
113 static void emit_imm(u8 rd
, s64 val
, struct rv_jit_context
*ctx
)
115 /* Note that the immediate from the add is sign-extended,
116 * which means that we need to compensate this by adding 2^12,
117 * when the 12th bit is set. A simpler way of doing this, and
118 * getting rid of the check, is to just add 2**11 before the
119 * shift. The "Loading a 32-Bit constant" example from the
120 * "Computer Organization and Design, RISC-V edition" book by
121 * Patterson/Hennessy highlights this fact.
123 * This also means that we need to process LSB to MSB.
125 s64 upper
= (val
+ (1 << 11)) >> 12, lower
= val
& 0xfff;
128 if (is_32b_int(val
)) {
130 emit(rv_lui(rd
, upper
), ctx
);
133 emit(rv_addi(rd
, RV_REG_ZERO
, lower
), ctx
);
137 emit(rv_addiw(rd
, rd
, lower
), ctx
);
141 shift
= __ffs(upper
);
145 emit_imm(rd
, upper
, ctx
);
147 emit(rv_slli(rd
, rd
, shift
), ctx
);
149 emit(rv_addi(rd
, rd
, lower
), ctx
);
152 static void __build_epilogue(bool is_tail_call
, struct rv_jit_context
*ctx
)
154 int stack_adjust
= ctx
->stack_size
, store_offset
= stack_adjust
- 8;
156 if (seen_reg(RV_REG_RA
, ctx
)) {
157 emit(rv_ld(RV_REG_RA
, store_offset
, RV_REG_SP
), ctx
);
160 emit(rv_ld(RV_REG_FP
, store_offset
, RV_REG_SP
), ctx
);
162 if (seen_reg(RV_REG_S1
, ctx
)) {
163 emit(rv_ld(RV_REG_S1
, store_offset
, RV_REG_SP
), ctx
);
166 if (seen_reg(RV_REG_S2
, ctx
)) {
167 emit(rv_ld(RV_REG_S2
, store_offset
, RV_REG_SP
), ctx
);
170 if (seen_reg(RV_REG_S3
, ctx
)) {
171 emit(rv_ld(RV_REG_S3
, store_offset
, RV_REG_SP
), ctx
);
174 if (seen_reg(RV_REG_S4
, ctx
)) {
175 emit(rv_ld(RV_REG_S4
, store_offset
, RV_REG_SP
), ctx
);
178 if (seen_reg(RV_REG_S5
, ctx
)) {
179 emit(rv_ld(RV_REG_S5
, store_offset
, RV_REG_SP
), ctx
);
182 if (seen_reg(RV_REG_S6
, ctx
)) {
183 emit(rv_ld(RV_REG_S6
, store_offset
, RV_REG_SP
), ctx
);
187 emit(rv_addi(RV_REG_SP
, RV_REG_SP
, stack_adjust
), ctx
);
188 /* Set return value. */
190 emit(rv_addi(RV_REG_A0
, RV_REG_A5
, 0), ctx
);
191 emit(rv_jalr(RV_REG_ZERO
, is_tail_call
? RV_REG_T3
: RV_REG_RA
,
192 is_tail_call
? 4 : 0), /* skip TCC init */
196 static void emit_bcc(u8 cond
, u8 rd
, u8 rs
, int rvoff
,
197 struct rv_jit_context
*ctx
)
201 emit(rv_beq(rd
, rs
, rvoff
>> 1), ctx
);
204 emit(rv_bltu(rs
, rd
, rvoff
>> 1), ctx
);
207 emit(rv_bltu(rd
, rs
, rvoff
>> 1), ctx
);
210 emit(rv_bgeu(rd
, rs
, rvoff
>> 1), ctx
);
213 emit(rv_bgeu(rs
, rd
, rvoff
>> 1), ctx
);
216 emit(rv_bne(rd
, rs
, rvoff
>> 1), ctx
);
219 emit(rv_blt(rs
, rd
, rvoff
>> 1), ctx
);
222 emit(rv_blt(rd
, rs
, rvoff
>> 1), ctx
);
225 emit(rv_bge(rd
, rs
, rvoff
>> 1), ctx
);
228 emit(rv_bge(rs
, rd
, rvoff
>> 1), ctx
);
232 static void emit_branch(u8 cond
, u8 rd
, u8 rs
, int rvoff
,
233 struct rv_jit_context
*ctx
)
237 if (is_13b_int(rvoff
)) {
238 emit_bcc(cond
, rd
, rs
, rvoff
, ctx
);
253 cond
= invert_bpf_cond(cond
);
254 if (is_21b_int(rvoff
)) {
255 emit_bcc(cond
, rd
, rs
, 8, ctx
);
256 emit(rv_jal(RV_REG_ZERO
, rvoff
>> 1), ctx
);
260 /* 32b No need for an additional rvoff adjustment, since we
261 * get that from the auipc at PC', where PC = PC' + 4.
263 upper
= (rvoff
+ (1 << 11)) >> 12;
264 lower
= rvoff
& 0xfff;
266 emit_bcc(cond
, rd
, rs
, 12, ctx
);
267 emit(rv_auipc(RV_REG_T1
, upper
), ctx
);
268 emit(rv_jalr(RV_REG_ZERO
, RV_REG_T1
, lower
), ctx
);
271 static void emit_zext_32(u8 reg
, struct rv_jit_context
*ctx
)
273 emit(rv_slli(reg
, reg
, 32), ctx
);
274 emit(rv_srli(reg
, reg
, 32), ctx
);
277 static int emit_bpf_tail_call(int insn
, struct rv_jit_context
*ctx
)
279 int tc_ninsn
, off
, start_insn
= ctx
->ninsns
;
280 u8 tcc
= rv_tail_call_reg(ctx
);
286 * if (index >= array->map.max_entries)
289 tc_ninsn
= insn
? ctx
->offset
[insn
] - ctx
->offset
[insn
- 1] :
291 emit_zext_32(RV_REG_A2
, ctx
);
293 off
= offsetof(struct bpf_array
, map
.max_entries
);
294 if (is_12b_check(off
, insn
))
296 emit(rv_lwu(RV_REG_T1
, off
, RV_REG_A1
), ctx
);
297 off
= (tc_ninsn
- (ctx
->ninsns
- start_insn
)) << 2;
298 emit_branch(BPF_JGE
, RV_REG_A2
, RV_REG_T1
, off
, ctx
);
303 emit(rv_addi(RV_REG_T1
, tcc
, -1), ctx
);
304 off
= (tc_ninsn
- (ctx
->ninsns
- start_insn
)) << 2;
305 emit_branch(BPF_JSLT
, tcc
, RV_REG_ZERO
, off
, ctx
);
307 /* prog = array->ptrs[index];
311 emit(rv_slli(RV_REG_T2
, RV_REG_A2
, 3), ctx
);
312 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_A1
), ctx
);
313 off
= offsetof(struct bpf_array
, ptrs
);
314 if (is_12b_check(off
, insn
))
316 emit(rv_ld(RV_REG_T2
, off
, RV_REG_T2
), ctx
);
317 off
= (tc_ninsn
- (ctx
->ninsns
- start_insn
)) << 2;
318 emit_branch(BPF_JEQ
, RV_REG_T2
, RV_REG_ZERO
, off
, ctx
);
320 /* goto *(prog->bpf_func + 4); */
321 off
= offsetof(struct bpf_prog
, bpf_func
);
322 if (is_12b_check(off
, insn
))
324 emit(rv_ld(RV_REG_T3
, off
, RV_REG_T2
), ctx
);
325 emit(rv_addi(RV_REG_TCC
, RV_REG_T1
, 0), ctx
);
326 __build_epilogue(true, ctx
);
330 static void init_regs(u8
*rd
, u8
*rs
, const struct bpf_insn
*insn
,
331 struct rv_jit_context
*ctx
)
333 u8 code
= insn
->code
;
336 case BPF_JMP
| BPF_JA
:
337 case BPF_JMP
| BPF_CALL
:
338 case BPF_JMP
| BPF_EXIT
:
339 case BPF_JMP
| BPF_TAIL_CALL
:
342 *rd
= bpf_to_rv_reg(insn
->dst_reg
, ctx
);
345 if (code
& (BPF_ALU
| BPF_X
) || code
& (BPF_ALU64
| BPF_X
) ||
346 code
& (BPF_JMP
| BPF_X
) || code
& (BPF_JMP32
| BPF_X
) ||
347 code
& BPF_LDX
|| code
& BPF_STX
)
348 *rs
= bpf_to_rv_reg(insn
->src_reg
, ctx
);
351 static void emit_zext_32_rd_rs(u8
*rd
, u8
*rs
, struct rv_jit_context
*ctx
)
353 emit(rv_addi(RV_REG_T2
, *rd
, 0), ctx
);
354 emit_zext_32(RV_REG_T2
, ctx
);
355 emit(rv_addi(RV_REG_T1
, *rs
, 0), ctx
);
356 emit_zext_32(RV_REG_T1
, ctx
);
361 static void emit_sext_32_rd_rs(u8
*rd
, u8
*rs
, struct rv_jit_context
*ctx
)
363 emit(rv_addiw(RV_REG_T2
, *rd
, 0), ctx
);
364 emit(rv_addiw(RV_REG_T1
, *rs
, 0), ctx
);
369 static void emit_zext_32_rd_t1(u8
*rd
, struct rv_jit_context
*ctx
)
371 emit(rv_addi(RV_REG_T2
, *rd
, 0), ctx
);
372 emit_zext_32(RV_REG_T2
, ctx
);
373 emit_zext_32(RV_REG_T1
, ctx
);
377 static void emit_sext_32_rd(u8
*rd
, struct rv_jit_context
*ctx
)
379 emit(rv_addiw(RV_REG_T2
, *rd
, 0), ctx
);
383 static void emit_jump_and_link(u8 rd
, s64 rvoff
, bool force_jalr
,
384 struct rv_jit_context
*ctx
)
388 if (rvoff
&& is_21b_int(rvoff
) && !force_jalr
) {
389 emit(rv_jal(rd
, rvoff
>> 1), ctx
);
393 upper
= (rvoff
+ (1 << 11)) >> 12;
394 lower
= rvoff
& 0xfff;
395 emit(rv_auipc(RV_REG_T1
, upper
), ctx
);
396 emit(rv_jalr(rd
, RV_REG_T1
, lower
), ctx
);
399 static bool is_signed_bpf_cond(u8 cond
)
401 return cond
== BPF_JSGT
|| cond
== BPF_JSLT
||
402 cond
== BPF_JSGE
|| cond
== BPF_JSLE
;
405 static int emit_call(bool fixed
, u64 addr
, struct rv_jit_context
*ctx
)
411 if (addr
&& ctx
->insns
) {
412 ip
= (u64
)(long)(ctx
->insns
+ ctx
->ninsns
);
414 if (!is_32b_int(off
)) {
415 pr_err("bpf-jit: target call addr %pK is out of range\n",
421 emit_jump_and_link(RV_REG_RA
, off
, !fixed
, ctx
);
422 rd
= bpf_to_rv_reg(BPF_REG_0
, ctx
);
423 emit(rv_addi(rd
, RV_REG_A0
, 0), ctx
);
427 int bpf_jit_emit_insn(const struct bpf_insn
*insn
, struct rv_jit_context
*ctx
,
430 bool is64
= BPF_CLASS(insn
->code
) == BPF_ALU64
||
431 BPF_CLASS(insn
->code
) == BPF_JMP
;
432 int s
, e
, rvoff
, i
= insn
- ctx
->prog
->insnsi
;
433 struct bpf_prog_aux
*aux
= ctx
->prog
->aux
;
434 u8 rd
= -1, rs
= -1, code
= insn
->code
;
438 init_regs(&rd
, &rs
, insn
, ctx
);
442 case BPF_ALU
| BPF_MOV
| BPF_X
:
443 case BPF_ALU64
| BPF_MOV
| BPF_X
:
445 /* Special mov32 for zext */
446 emit_zext_32(rd
, ctx
);
449 emit(is64
? rv_addi(rd
, rs
, 0) : rv_addiw(rd
, rs
, 0), ctx
);
450 if (!is64
&& !aux
->verifier_zext
)
451 emit_zext_32(rd
, ctx
);
454 /* dst = dst OP src */
455 case BPF_ALU
| BPF_ADD
| BPF_X
:
456 case BPF_ALU64
| BPF_ADD
| BPF_X
:
457 emit(is64
? rv_add(rd
, rd
, rs
) : rv_addw(rd
, rd
, rs
), ctx
);
458 if (!is64
&& !aux
->verifier_zext
)
459 emit_zext_32(rd
, ctx
);
461 case BPF_ALU
| BPF_SUB
| BPF_X
:
462 case BPF_ALU64
| BPF_SUB
| BPF_X
:
463 emit(is64
? rv_sub(rd
, rd
, rs
) : rv_subw(rd
, rd
, rs
), ctx
);
464 if (!is64
&& !aux
->verifier_zext
)
465 emit_zext_32(rd
, ctx
);
467 case BPF_ALU
| BPF_AND
| BPF_X
:
468 case BPF_ALU64
| BPF_AND
| BPF_X
:
469 emit(rv_and(rd
, rd
, rs
), ctx
);
470 if (!is64
&& !aux
->verifier_zext
)
471 emit_zext_32(rd
, ctx
);
473 case BPF_ALU
| BPF_OR
| BPF_X
:
474 case BPF_ALU64
| BPF_OR
| BPF_X
:
475 emit(rv_or(rd
, rd
, rs
), ctx
);
476 if (!is64
&& !aux
->verifier_zext
)
477 emit_zext_32(rd
, ctx
);
479 case BPF_ALU
| BPF_XOR
| BPF_X
:
480 case BPF_ALU64
| BPF_XOR
| BPF_X
:
481 emit(rv_xor(rd
, rd
, rs
), ctx
);
482 if (!is64
&& !aux
->verifier_zext
)
483 emit_zext_32(rd
, ctx
);
485 case BPF_ALU
| BPF_MUL
| BPF_X
:
486 case BPF_ALU64
| BPF_MUL
| BPF_X
:
487 emit(is64
? rv_mul(rd
, rd
, rs
) : rv_mulw(rd
, rd
, rs
), ctx
);
488 if (!is64
&& !aux
->verifier_zext
)
489 emit_zext_32(rd
, ctx
);
491 case BPF_ALU
| BPF_DIV
| BPF_X
:
492 case BPF_ALU64
| BPF_DIV
| BPF_X
:
493 emit(is64
? rv_divu(rd
, rd
, rs
) : rv_divuw(rd
, rd
, rs
), ctx
);
494 if (!is64
&& !aux
->verifier_zext
)
495 emit_zext_32(rd
, ctx
);
497 case BPF_ALU
| BPF_MOD
| BPF_X
:
498 case BPF_ALU64
| BPF_MOD
| BPF_X
:
499 emit(is64
? rv_remu(rd
, rd
, rs
) : rv_remuw(rd
, rd
, rs
), ctx
);
500 if (!is64
&& !aux
->verifier_zext
)
501 emit_zext_32(rd
, ctx
);
503 case BPF_ALU
| BPF_LSH
| BPF_X
:
504 case BPF_ALU64
| BPF_LSH
| BPF_X
:
505 emit(is64
? rv_sll(rd
, rd
, rs
) : rv_sllw(rd
, rd
, rs
), ctx
);
507 emit_zext_32(rd
, ctx
);
509 case BPF_ALU
| BPF_RSH
| BPF_X
:
510 case BPF_ALU64
| BPF_RSH
| BPF_X
:
511 emit(is64
? rv_srl(rd
, rd
, rs
) : rv_srlw(rd
, rd
, rs
), ctx
);
512 if (!is64
&& !aux
->verifier_zext
)
513 emit_zext_32(rd
, ctx
);
515 case BPF_ALU
| BPF_ARSH
| BPF_X
:
516 case BPF_ALU64
| BPF_ARSH
| BPF_X
:
517 emit(is64
? rv_sra(rd
, rd
, rs
) : rv_sraw(rd
, rd
, rs
), ctx
);
518 if (!is64
&& !aux
->verifier_zext
)
519 emit_zext_32(rd
, ctx
);
523 case BPF_ALU
| BPF_NEG
:
524 case BPF_ALU64
| BPF_NEG
:
525 emit(is64
? rv_sub(rd
, RV_REG_ZERO
, rd
) :
526 rv_subw(rd
, RV_REG_ZERO
, rd
), ctx
);
527 if (!is64
&& !aux
->verifier_zext
)
528 emit_zext_32(rd
, ctx
);
531 /* dst = BSWAP##imm(dst) */
532 case BPF_ALU
| BPF_END
| BPF_FROM_LE
:
534 int shift
= 64 - imm
;
536 emit(rv_slli(rd
, rd
, shift
), ctx
);
537 emit(rv_srli(rd
, rd
, shift
), ctx
);
540 case BPF_ALU
| BPF_END
| BPF_FROM_BE
:
541 emit(rv_addi(RV_REG_T2
, RV_REG_ZERO
, 0), ctx
);
543 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
544 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
545 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
546 emit(rv_srli(rd
, rd
, 8), ctx
);
550 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
551 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
552 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
553 emit(rv_srli(rd
, rd
, 8), ctx
);
555 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
556 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
557 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
558 emit(rv_srli(rd
, rd
, 8), ctx
);
562 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
563 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
564 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
565 emit(rv_srli(rd
, rd
, 8), ctx
);
567 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
568 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
569 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
570 emit(rv_srli(rd
, rd
, 8), ctx
);
572 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
573 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
574 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
575 emit(rv_srli(rd
, rd
, 8), ctx
);
577 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
578 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
579 emit(rv_slli(RV_REG_T2
, RV_REG_T2
, 8), ctx
);
580 emit(rv_srli(rd
, rd
, 8), ctx
);
582 emit(rv_andi(RV_REG_T1
, rd
, 0xff), ctx
);
583 emit(rv_add(RV_REG_T2
, RV_REG_T2
, RV_REG_T1
), ctx
);
585 emit(rv_addi(rd
, RV_REG_T2
, 0), ctx
);
589 case BPF_ALU
| BPF_MOV
| BPF_K
:
590 case BPF_ALU64
| BPF_MOV
| BPF_K
:
591 emit_imm(rd
, imm
, ctx
);
592 if (!is64
&& !aux
->verifier_zext
)
593 emit_zext_32(rd
, ctx
);
596 /* dst = dst OP imm */
597 case BPF_ALU
| BPF_ADD
| BPF_K
:
598 case BPF_ALU64
| BPF_ADD
| BPF_K
:
599 if (is_12b_int(imm
)) {
600 emit(is64
? rv_addi(rd
, rd
, imm
) :
601 rv_addiw(rd
, rd
, imm
), ctx
);
603 emit_imm(RV_REG_T1
, imm
, ctx
);
604 emit(is64
? rv_add(rd
, rd
, RV_REG_T1
) :
605 rv_addw(rd
, rd
, RV_REG_T1
), ctx
);
607 if (!is64
&& !aux
->verifier_zext
)
608 emit_zext_32(rd
, ctx
);
610 case BPF_ALU
| BPF_SUB
| BPF_K
:
611 case BPF_ALU64
| BPF_SUB
| BPF_K
:
612 if (is_12b_int(-imm
)) {
613 emit(is64
? rv_addi(rd
, rd
, -imm
) :
614 rv_addiw(rd
, rd
, -imm
), ctx
);
616 emit_imm(RV_REG_T1
, imm
, ctx
);
617 emit(is64
? rv_sub(rd
, rd
, RV_REG_T1
) :
618 rv_subw(rd
, rd
, RV_REG_T1
), ctx
);
620 if (!is64
&& !aux
->verifier_zext
)
621 emit_zext_32(rd
, ctx
);
623 case BPF_ALU
| BPF_AND
| BPF_K
:
624 case BPF_ALU64
| BPF_AND
| BPF_K
:
625 if (is_12b_int(imm
)) {
626 emit(rv_andi(rd
, rd
, imm
), ctx
);
628 emit_imm(RV_REG_T1
, imm
, ctx
);
629 emit(rv_and(rd
, rd
, RV_REG_T1
), ctx
);
631 if (!is64
&& !aux
->verifier_zext
)
632 emit_zext_32(rd
, ctx
);
634 case BPF_ALU
| BPF_OR
| BPF_K
:
635 case BPF_ALU64
| BPF_OR
| BPF_K
:
636 if (is_12b_int(imm
)) {
637 emit(rv_ori(rd
, rd
, imm
), ctx
);
639 emit_imm(RV_REG_T1
, imm
, ctx
);
640 emit(rv_or(rd
, rd
, RV_REG_T1
), ctx
);
642 if (!is64
&& !aux
->verifier_zext
)
643 emit_zext_32(rd
, ctx
);
645 case BPF_ALU
| BPF_XOR
| BPF_K
:
646 case BPF_ALU64
| BPF_XOR
| BPF_K
:
647 if (is_12b_int(imm
)) {
648 emit(rv_xori(rd
, rd
, imm
), ctx
);
650 emit_imm(RV_REG_T1
, imm
, ctx
);
651 emit(rv_xor(rd
, rd
, RV_REG_T1
), ctx
);
653 if (!is64
&& !aux
->verifier_zext
)
654 emit_zext_32(rd
, ctx
);
656 case BPF_ALU
| BPF_MUL
| BPF_K
:
657 case BPF_ALU64
| BPF_MUL
| BPF_K
:
658 emit_imm(RV_REG_T1
, imm
, ctx
);
659 emit(is64
? rv_mul(rd
, rd
, RV_REG_T1
) :
660 rv_mulw(rd
, rd
, RV_REG_T1
), ctx
);
661 if (!is64
&& !aux
->verifier_zext
)
662 emit_zext_32(rd
, ctx
);
664 case BPF_ALU
| BPF_DIV
| BPF_K
:
665 case BPF_ALU64
| BPF_DIV
| BPF_K
:
666 emit_imm(RV_REG_T1
, imm
, ctx
);
667 emit(is64
? rv_divu(rd
, rd
, RV_REG_T1
) :
668 rv_divuw(rd
, rd
, RV_REG_T1
), ctx
);
669 if (!is64
&& !aux
->verifier_zext
)
670 emit_zext_32(rd
, ctx
);
672 case BPF_ALU
| BPF_MOD
| BPF_K
:
673 case BPF_ALU64
| BPF_MOD
| BPF_K
:
674 emit_imm(RV_REG_T1
, imm
, ctx
);
675 emit(is64
? rv_remu(rd
, rd
, RV_REG_T1
) :
676 rv_remuw(rd
, rd
, RV_REG_T1
), ctx
);
677 if (!is64
&& !aux
->verifier_zext
)
678 emit_zext_32(rd
, ctx
);
680 case BPF_ALU
| BPF_LSH
| BPF_K
:
681 case BPF_ALU64
| BPF_LSH
| BPF_K
:
682 emit(is64
? rv_slli(rd
, rd
, imm
) : rv_slliw(rd
, rd
, imm
), ctx
);
684 emit_zext_32(rd
, ctx
);
686 case BPF_ALU
| BPF_RSH
| BPF_K
:
687 case BPF_ALU64
| BPF_RSH
| BPF_K
:
688 emit(is64
? rv_srli(rd
, rd
, imm
) : rv_srliw(rd
, rd
, imm
), ctx
);
690 emit_zext_32(rd
, ctx
);
692 case BPF_ALU
| BPF_ARSH
| BPF_K
:
693 case BPF_ALU64
| BPF_ARSH
| BPF_K
:
694 emit(is64
? rv_srai(rd
, rd
, imm
) : rv_sraiw(rd
, rd
, imm
), ctx
);
696 emit_zext_32(rd
, ctx
);
700 case BPF_JMP
| BPF_JA
:
701 rvoff
= rv_offset(i
, off
, ctx
);
702 emit_jump_and_link(RV_REG_ZERO
, rvoff
, false, ctx
);
705 /* IF (dst COND src) JUMP off */
706 case BPF_JMP
| BPF_JEQ
| BPF_X
:
707 case BPF_JMP32
| BPF_JEQ
| BPF_X
:
708 case BPF_JMP
| BPF_JGT
| BPF_X
:
709 case BPF_JMP32
| BPF_JGT
| BPF_X
:
710 case BPF_JMP
| BPF_JLT
| BPF_X
:
711 case BPF_JMP32
| BPF_JLT
| BPF_X
:
712 case BPF_JMP
| BPF_JGE
| BPF_X
:
713 case BPF_JMP32
| BPF_JGE
| BPF_X
:
714 case BPF_JMP
| BPF_JLE
| BPF_X
:
715 case BPF_JMP32
| BPF_JLE
| BPF_X
:
716 case BPF_JMP
| BPF_JNE
| BPF_X
:
717 case BPF_JMP32
| BPF_JNE
| BPF_X
:
718 case BPF_JMP
| BPF_JSGT
| BPF_X
:
719 case BPF_JMP32
| BPF_JSGT
| BPF_X
:
720 case BPF_JMP
| BPF_JSLT
| BPF_X
:
721 case BPF_JMP32
| BPF_JSLT
| BPF_X
:
722 case BPF_JMP
| BPF_JSGE
| BPF_X
:
723 case BPF_JMP32
| BPF_JSGE
| BPF_X
:
724 case BPF_JMP
| BPF_JSLE
| BPF_X
:
725 case BPF_JMP32
| BPF_JSLE
| BPF_X
:
726 case BPF_JMP
| BPF_JSET
| BPF_X
:
727 case BPF_JMP32
| BPF_JSET
| BPF_X
:
728 rvoff
= rv_offset(i
, off
, ctx
);
731 if (is_signed_bpf_cond(BPF_OP(code
)))
732 emit_sext_32_rd_rs(&rd
, &rs
, ctx
);
734 emit_zext_32_rd_rs(&rd
, &rs
, ctx
);
737 /* Adjust for extra insns */
738 rvoff
-= (e
- s
) << 2;
741 if (BPF_OP(code
) == BPF_JSET
) {
744 emit(rv_and(RV_REG_T1
, rd
, rs
), ctx
);
745 emit_branch(BPF_JNE
, RV_REG_T1
, RV_REG_ZERO
, rvoff
,
748 emit_branch(BPF_OP(code
), rd
, rs
, rvoff
, ctx
);
752 /* IF (dst COND imm) JUMP off */
753 case BPF_JMP
| BPF_JEQ
| BPF_K
:
754 case BPF_JMP32
| BPF_JEQ
| BPF_K
:
755 case BPF_JMP
| BPF_JGT
| BPF_K
:
756 case BPF_JMP32
| BPF_JGT
| BPF_K
:
757 case BPF_JMP
| BPF_JLT
| BPF_K
:
758 case BPF_JMP32
| BPF_JLT
| BPF_K
:
759 case BPF_JMP
| BPF_JGE
| BPF_K
:
760 case BPF_JMP32
| BPF_JGE
| BPF_K
:
761 case BPF_JMP
| BPF_JLE
| BPF_K
:
762 case BPF_JMP32
| BPF_JLE
| BPF_K
:
763 case BPF_JMP
| BPF_JNE
| BPF_K
:
764 case BPF_JMP32
| BPF_JNE
| BPF_K
:
765 case BPF_JMP
| BPF_JSGT
| BPF_K
:
766 case BPF_JMP32
| BPF_JSGT
| BPF_K
:
767 case BPF_JMP
| BPF_JSLT
| BPF_K
:
768 case BPF_JMP32
| BPF_JSLT
| BPF_K
:
769 case BPF_JMP
| BPF_JSGE
| BPF_K
:
770 case BPF_JMP32
| BPF_JSGE
| BPF_K
:
771 case BPF_JMP
| BPF_JSLE
| BPF_K
:
772 case BPF_JMP32
| BPF_JSLE
| BPF_K
:
773 case BPF_JMP
| BPF_JSET
| BPF_K
:
774 case BPF_JMP32
| BPF_JSET
| BPF_K
:
775 rvoff
= rv_offset(i
, off
, ctx
);
777 emit_imm(RV_REG_T1
, imm
, ctx
);
779 if (is_signed_bpf_cond(BPF_OP(code
)))
780 emit_sext_32_rd(&rd
, ctx
);
782 emit_zext_32_rd_t1(&rd
, ctx
);
786 /* Adjust for extra insns */
787 rvoff
-= (e
- s
) << 2;
789 if (BPF_OP(code
) == BPF_JSET
) {
792 emit(rv_and(RV_REG_T1
, rd
, RV_REG_T1
), ctx
);
793 emit_branch(BPF_JNE
, RV_REG_T1
, RV_REG_ZERO
, rvoff
,
796 emit_branch(BPF_OP(code
), rd
, RV_REG_T1
, rvoff
, ctx
);
801 case BPF_JMP
| BPF_CALL
:
808 ret
= bpf_jit_get_func_addr(ctx
->prog
, insn
, extra_pass
, &addr
,
812 ret
= emit_call(fixed
, addr
, ctx
);
818 case BPF_JMP
| BPF_TAIL_CALL
:
819 if (emit_bpf_tail_call(i
, ctx
))
823 /* function return */
824 case BPF_JMP
| BPF_EXIT
:
825 if (i
== ctx
->prog
->len
- 1)
828 rvoff
= epilogue_offset(ctx
);
829 emit_jump_and_link(RV_REG_ZERO
, rvoff
, false, ctx
);
833 case BPF_LD
| BPF_IMM
| BPF_DW
:
835 struct bpf_insn insn1
= insn
[1];
838 imm64
= (u64
)insn1
.imm
<< 32 | (u32
)imm
;
839 emit_imm(rd
, imm64
, ctx
);
843 /* LDX: dst = *(size *)(src + off) */
844 case BPF_LDX
| BPF_MEM
| BPF_B
:
845 if (is_12b_int(off
)) {
846 emit(rv_lbu(rd
, off
, rs
), ctx
);
850 emit_imm(RV_REG_T1
, off
, ctx
);
851 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
852 emit(rv_lbu(rd
, 0, RV_REG_T1
), ctx
);
853 if (insn_is_zext(&insn
[1]))
856 case BPF_LDX
| BPF_MEM
| BPF_H
:
857 if (is_12b_int(off
)) {
858 emit(rv_lhu(rd
, off
, rs
), ctx
);
862 emit_imm(RV_REG_T1
, off
, ctx
);
863 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
864 emit(rv_lhu(rd
, 0, RV_REG_T1
), ctx
);
865 if (insn_is_zext(&insn
[1]))
868 case BPF_LDX
| BPF_MEM
| BPF_W
:
869 if (is_12b_int(off
)) {
870 emit(rv_lwu(rd
, off
, rs
), ctx
);
874 emit_imm(RV_REG_T1
, off
, ctx
);
875 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
876 emit(rv_lwu(rd
, 0, RV_REG_T1
), ctx
);
877 if (insn_is_zext(&insn
[1]))
880 case BPF_LDX
| BPF_MEM
| BPF_DW
:
881 if (is_12b_int(off
)) {
882 emit(rv_ld(rd
, off
, rs
), ctx
);
886 emit_imm(RV_REG_T1
, off
, ctx
);
887 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rs
), ctx
);
888 emit(rv_ld(rd
, 0, RV_REG_T1
), ctx
);
891 /* ST: *(size *)(dst + off) = imm */
892 case BPF_ST
| BPF_MEM
| BPF_B
:
893 emit_imm(RV_REG_T1
, imm
, ctx
);
894 if (is_12b_int(off
)) {
895 emit(rv_sb(rd
, off
, RV_REG_T1
), ctx
);
899 emit_imm(RV_REG_T2
, off
, ctx
);
900 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
901 emit(rv_sb(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
904 case BPF_ST
| BPF_MEM
| BPF_H
:
905 emit_imm(RV_REG_T1
, imm
, ctx
);
906 if (is_12b_int(off
)) {
907 emit(rv_sh(rd
, off
, RV_REG_T1
), ctx
);
911 emit_imm(RV_REG_T2
, off
, ctx
);
912 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
913 emit(rv_sh(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
915 case BPF_ST
| BPF_MEM
| BPF_W
:
916 emit_imm(RV_REG_T1
, imm
, ctx
);
917 if (is_12b_int(off
)) {
918 emit(rv_sw(rd
, off
, RV_REG_T1
), ctx
);
922 emit_imm(RV_REG_T2
, off
, ctx
);
923 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
924 emit(rv_sw(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
926 case BPF_ST
| BPF_MEM
| BPF_DW
:
927 emit_imm(RV_REG_T1
, imm
, ctx
);
928 if (is_12b_int(off
)) {
929 emit(rv_sd(rd
, off
, RV_REG_T1
), ctx
);
933 emit_imm(RV_REG_T2
, off
, ctx
);
934 emit(rv_add(RV_REG_T2
, RV_REG_T2
, rd
), ctx
);
935 emit(rv_sd(RV_REG_T2
, 0, RV_REG_T1
), ctx
);
938 /* STX: *(size *)(dst + off) = src */
939 case BPF_STX
| BPF_MEM
| BPF_B
:
940 if (is_12b_int(off
)) {
941 emit(rv_sb(rd
, off
, rs
), ctx
);
945 emit_imm(RV_REG_T1
, off
, ctx
);
946 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
947 emit(rv_sb(RV_REG_T1
, 0, rs
), ctx
);
949 case BPF_STX
| BPF_MEM
| BPF_H
:
950 if (is_12b_int(off
)) {
951 emit(rv_sh(rd
, off
, rs
), ctx
);
955 emit_imm(RV_REG_T1
, off
, ctx
);
956 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
957 emit(rv_sh(RV_REG_T1
, 0, rs
), ctx
);
959 case BPF_STX
| BPF_MEM
| BPF_W
:
960 if (is_12b_int(off
)) {
961 emit(rv_sw(rd
, off
, rs
), ctx
);
965 emit_imm(RV_REG_T1
, off
, ctx
);
966 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
967 emit(rv_sw(RV_REG_T1
, 0, rs
), ctx
);
969 case BPF_STX
| BPF_MEM
| BPF_DW
:
970 if (is_12b_int(off
)) {
971 emit(rv_sd(rd
, off
, rs
), ctx
);
975 emit_imm(RV_REG_T1
, off
, ctx
);
976 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
977 emit(rv_sd(RV_REG_T1
, 0, rs
), ctx
);
979 /* STX XADD: lock *(u32 *)(dst + off) += src */
980 case BPF_STX
| BPF_XADD
| BPF_W
:
981 /* STX XADD: lock *(u64 *)(dst + off) += src */
982 case BPF_STX
| BPF_XADD
| BPF_DW
:
984 if (is_12b_int(off
)) {
985 emit(rv_addi(RV_REG_T1
, rd
, off
), ctx
);
987 emit_imm(RV_REG_T1
, off
, ctx
);
988 emit(rv_add(RV_REG_T1
, RV_REG_T1
, rd
), ctx
);
994 emit(BPF_SIZE(code
) == BPF_W
?
995 rv_amoadd_w(RV_REG_ZERO
, rs
, rd
, 0, 0) :
996 rv_amoadd_d(RV_REG_ZERO
, rs
, rd
, 0, 0), ctx
);
999 pr_err("bpf-jit: unknown opcode %02x\n", code
);
1006 void bpf_jit_build_prologue(struct rv_jit_context
*ctx
)
1008 int stack_adjust
= 0, store_offset
, bpf_stack_adjust
;
1010 bpf_stack_adjust
= round_up(ctx
->prog
->aux
->stack_depth
, 16);
1011 if (bpf_stack_adjust
)
1014 if (seen_reg(RV_REG_RA
, ctx
))
1016 stack_adjust
+= 8; /* RV_REG_FP */
1017 if (seen_reg(RV_REG_S1
, ctx
))
1019 if (seen_reg(RV_REG_S2
, ctx
))
1021 if (seen_reg(RV_REG_S3
, ctx
))
1023 if (seen_reg(RV_REG_S4
, ctx
))
1025 if (seen_reg(RV_REG_S5
, ctx
))
1027 if (seen_reg(RV_REG_S6
, ctx
))
1030 stack_adjust
= round_up(stack_adjust
, 16);
1031 stack_adjust
+= bpf_stack_adjust
;
1033 store_offset
= stack_adjust
- 8;
1035 /* First instruction is always setting the tail-call-counter
1036 * (TCC) register. This instruction is skipped for tail calls.
1038 emit(rv_addi(RV_REG_TCC
, RV_REG_ZERO
, MAX_TAIL_CALL_CNT
), ctx
);
1040 emit(rv_addi(RV_REG_SP
, RV_REG_SP
, -stack_adjust
), ctx
);
1042 if (seen_reg(RV_REG_RA
, ctx
)) {
1043 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_RA
), ctx
);
1046 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_FP
), ctx
);
1048 if (seen_reg(RV_REG_S1
, ctx
)) {
1049 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S1
), ctx
);
1052 if (seen_reg(RV_REG_S2
, ctx
)) {
1053 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S2
), ctx
);
1056 if (seen_reg(RV_REG_S3
, ctx
)) {
1057 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S3
), ctx
);
1060 if (seen_reg(RV_REG_S4
, ctx
)) {
1061 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S4
), ctx
);
1064 if (seen_reg(RV_REG_S5
, ctx
)) {
1065 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S5
), ctx
);
1068 if (seen_reg(RV_REG_S6
, ctx
)) {
1069 emit(rv_sd(RV_REG_SP
, store_offset
, RV_REG_S6
), ctx
);
1073 emit(rv_addi(RV_REG_FP
, RV_REG_SP
, stack_adjust
), ctx
);
1075 if (bpf_stack_adjust
)
1076 emit(rv_addi(RV_REG_S5
, RV_REG_SP
, bpf_stack_adjust
), ctx
);
1078 /* Program contains calls and tail calls, so RV_REG_TCC need
1079 * to be saved across calls.
1081 if (seen_tail_call(ctx
) && seen_call(ctx
))
1082 emit(rv_addi(RV_REG_TCC_SAVED
, RV_REG_TCC
, 0), ctx
);
1084 ctx
->stack_size
= stack_adjust
;
1087 void bpf_jit_build_epilogue(struct rv_jit_context
*ctx
)
1089 __build_epilogue(false, ctx
);
1092 void *bpf_jit_alloc_exec(unsigned long size
)
1094 return __vmalloc_node_range(size
, PAGE_SIZE
, BPF_JIT_REGION_START
,
1095 BPF_JIT_REGION_END
, GFP_KERNEL
,
1096 PAGE_KERNEL_EXEC
, 0, NUMA_NO_NODE
,
1097 __builtin_return_address(0));
1100 void bpf_jit_free_exec(void *addr
)