blob: 0eefe6193253ba5c6aef9dcb94eadc077e66f4d4 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/* BPF JIT compiler for RV64G
3 *
4 * Copyright(c) 2019 Björn Töpel <bjorn.topel@gmail.com>
5 *
6 */
7
8#include <linux/bpf.h>
9#include <linux/filter.h>
10#include <asm/cacheflush.h>
11
12enum {
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 */
19 RV_REG_T1 = 6,
20 RV_REG_T2 = 7,
21 RV_REG_FP = 8,
22 RV_REG_S1 = 9, /* Saved registers */
23 RV_REG_A0 = 10, /* Function argument/return values */
24 RV_REG_A1 = 11, /* Function arguments */
25 RV_REG_A2 = 12,
26 RV_REG_A3 = 13,
27 RV_REG_A4 = 14,
28 RV_REG_A5 = 15,
29 RV_REG_A6 = 16,
30 RV_REG_A7 = 17,
31 RV_REG_S2 = 18, /* Saved registers */
32 RV_REG_S3 = 19,
33 RV_REG_S4 = 20,
34 RV_REG_S5 = 21,
35 RV_REG_S6 = 22,
36 RV_REG_S7 = 23,
37 RV_REG_S8 = 24,
38 RV_REG_S9 = 25,
39 RV_REG_S10 = 26,
40 RV_REG_S11 = 27,
41 RV_REG_T3 = 28, /* Temporaries */
42 RV_REG_T4 = 29,
43 RV_REG_T5 = 30,
44 RV_REG_T6 = 31,
45};
46
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 */
49
50static 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,
63};
64
65enum {
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,
74};
75
76struct rv_jit_context {
77 struct bpf_prog *prog;
78 u32 *insns; /* RV insns */
79 int ninsns;
80 int epilogue_offset;
81 int *offset; /* BPF to RV */
82 unsigned long flags;
83 int stack_size;
84};
85
86struct rv_jit_data {
87 struct bpf_binary_header *header;
88 u8 *image;
89 struct rv_jit_context ctx;
90};
91
92static u8 bpf_to_rv_reg(int bpf_reg, struct rv_jit_context *ctx)
93{
94 u8 reg = regmap[bpf_reg];
95
96 switch (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);
104 }
105 return reg;
106};
107
108static bool seen_reg(int reg, struct rv_jit_context *ctx)
109{
110 switch (reg) {
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);
119 }
120 return false;
121}
122
123static void mark_fp(struct rv_jit_context *ctx)
124{
125 __set_bit(RV_CTX_F_SEEN_S5, &ctx->flags);
126}
127
128static void mark_call(struct rv_jit_context *ctx)
129{
130 __set_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
131}
132
133static bool seen_call(struct rv_jit_context *ctx)
134{
135 return test_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
136}
137
138static void mark_tail_call(struct rv_jit_context *ctx)
139{
140 __set_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
141}
142
143static bool seen_tail_call(struct rv_jit_context *ctx)
144{
145 return test_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
146}
147
148static u8 rv_tail_call_reg(struct rv_jit_context *ctx)
149{
150 mark_tail_call(ctx);
151
152 if (seen_call(ctx)) {
153 __set_bit(RV_CTX_F_SEEN_S6, &ctx->flags);
154 return RV_REG_S6;
155 }
156 return RV_REG_A6;
157}
158
159static void emit(const u32 insn, struct rv_jit_context *ctx)
160{
161 if (ctx->insns)
162 ctx->insns[ctx->ninsns] = insn;
163
164 ctx->ninsns++;
165}
166
167static u32 rv_r_insn(u8 funct7, u8 rs2, u8 rs1, u8 funct3, u8 rd, u8 opcode)
168{
169 return (funct7 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
170 (rd << 7) | opcode;
171}
172
173static u32 rv_i_insn(u16 imm11_0, u8 rs1, u8 funct3, u8 rd, u8 opcode)
174{
175 return (imm11_0 << 20) | (rs1 << 15) | (funct3 << 12) | (rd << 7) |
176 opcode;
177}
178
179static u32 rv_s_insn(u16 imm11_0, u8 rs2, u8 rs1, u8 funct3, u8 opcode)
180{
181 u8 imm11_5 = imm11_0 >> 5, imm4_0 = imm11_0 & 0x1f;
182
183 return (imm11_5 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
184 (imm4_0 << 7) | opcode;
185}
186
187static u32 rv_sb_insn(u16 imm12_1, u8 rs2, u8 rs1, u8 funct3, u8 opcode)
188{
189 u8 imm12 = ((imm12_1 & 0x800) >> 5) | ((imm12_1 & 0x3f0) >> 4);
190 u8 imm4_1 = ((imm12_1 & 0xf) << 1) | ((imm12_1 & 0x400) >> 10);
191
192 return (imm12 << 25) | (rs2 << 20) | (rs1 << 15) | (funct3 << 12) |
193 (imm4_1 << 7) | opcode;
194}
195
196static u32 rv_u_insn(u32 imm31_12, u8 rd, u8 opcode)
197{
198 return (imm31_12 << 12) | (rd << 7) | opcode;
199}
200
201static u32 rv_uj_insn(u32 imm20_1, u8 rd, u8 opcode)
202{
203 u32 imm;
204
205 imm = (imm20_1 & 0x80000) | ((imm20_1 & 0x3ff) << 9) |
206 ((imm20_1 & 0x400) >> 2) | ((imm20_1 & 0x7f800) >> 11);
207
208 return (imm << 12) | (rd << 7) | opcode;
209}
210
211static u32 rv_amo_insn(u8 funct5, u8 aq, u8 rl, u8 rs2, u8 rs1,
212 u8 funct3, u8 rd, u8 opcode)
213{
214 u8 funct7 = (funct5 << 2) | (aq << 1) | rl;
215
216 return rv_r_insn(funct7, rs2, rs1, funct3, rd, opcode);
217}
218
219static u32 rv_addiw(u8 rd, u8 rs1, u16 imm11_0)
220{
221 return rv_i_insn(imm11_0, rs1, 0, rd, 0x1b);
222}
223
224static u32 rv_addi(u8 rd, u8 rs1, u16 imm11_0)
225{
226 return rv_i_insn(imm11_0, rs1, 0, rd, 0x13);
227}
228
229static u32 rv_addw(u8 rd, u8 rs1, u8 rs2)
230{
231 return rv_r_insn(0, rs2, rs1, 0, rd, 0x3b);
232}
233
234static u32 rv_add(u8 rd, u8 rs1, u8 rs2)
235{
236 return rv_r_insn(0, rs2, rs1, 0, rd, 0x33);
237}
238
239static u32 rv_subw(u8 rd, u8 rs1, u8 rs2)
240{
241 return rv_r_insn(0x20, rs2, rs1, 0, rd, 0x3b);
242}
243
244static u32 rv_sub(u8 rd, u8 rs1, u8 rs2)
245{
246 return rv_r_insn(0x20, rs2, rs1, 0, rd, 0x33);
247}
248
249static u32 rv_and(u8 rd, u8 rs1, u8 rs2)
250{
251 return rv_r_insn(0, rs2, rs1, 7, rd, 0x33);
252}
253
254static u32 rv_or(u8 rd, u8 rs1, u8 rs2)
255{
256 return rv_r_insn(0, rs2, rs1, 6, rd, 0x33);
257}
258
259static u32 rv_xor(u8 rd, u8 rs1, u8 rs2)
260{
261 return rv_r_insn(0, rs2, rs1, 4, rd, 0x33);
262}
263
264static u32 rv_mulw(u8 rd, u8 rs1, u8 rs2)
265{
266 return rv_r_insn(1, rs2, rs1, 0, rd, 0x3b);
267}
268
269static u32 rv_mul(u8 rd, u8 rs1, u8 rs2)
270{
271 return rv_r_insn(1, rs2, rs1, 0, rd, 0x33);
272}
273
274static u32 rv_divuw(u8 rd, u8 rs1, u8 rs2)
275{
276 return rv_r_insn(1, rs2, rs1, 5, rd, 0x3b);
277}
278
279static u32 rv_divu(u8 rd, u8 rs1, u8 rs2)
280{
281 return rv_r_insn(1, rs2, rs1, 5, rd, 0x33);
282}
283
284static u32 rv_remuw(u8 rd, u8 rs1, u8 rs2)
285{
286 return rv_r_insn(1, rs2, rs1, 7, rd, 0x3b);
287}
288
289static u32 rv_remu(u8 rd, u8 rs1, u8 rs2)
290{
291 return rv_r_insn(1, rs2, rs1, 7, rd, 0x33);
292}
293
294static u32 rv_sllw(u8 rd, u8 rs1, u8 rs2)
295{
296 return rv_r_insn(0, rs2, rs1, 1, rd, 0x3b);
297}
298
299static u32 rv_sll(u8 rd, u8 rs1, u8 rs2)
300{
301 return rv_r_insn(0, rs2, rs1, 1, rd, 0x33);
302}
303
304static u32 rv_srlw(u8 rd, u8 rs1, u8 rs2)
305{
306 return rv_r_insn(0, rs2, rs1, 5, rd, 0x3b);
307}
308
309static u32 rv_srl(u8 rd, u8 rs1, u8 rs2)
310{
311 return rv_r_insn(0, rs2, rs1, 5, rd, 0x33);
312}
313
314static u32 rv_sraw(u8 rd, u8 rs1, u8 rs2)
315{
316 return rv_r_insn(0x20, rs2, rs1, 5, rd, 0x3b);
317}
318
319static u32 rv_sra(u8 rd, u8 rs1, u8 rs2)
320{
321 return rv_r_insn(0x20, rs2, rs1, 5, rd, 0x33);
322}
323
324static u32 rv_lui(u8 rd, u32 imm31_12)
325{
326 return rv_u_insn(imm31_12, rd, 0x37);
327}
328
329static u32 rv_slli(u8 rd, u8 rs1, u16 imm11_0)
330{
331 return rv_i_insn(imm11_0, rs1, 1, rd, 0x13);
332}
333
334static u32 rv_andi(u8 rd, u8 rs1, u16 imm11_0)
335{
336 return rv_i_insn(imm11_0, rs1, 7, rd, 0x13);
337}
338
339static u32 rv_ori(u8 rd, u8 rs1, u16 imm11_0)
340{
341 return rv_i_insn(imm11_0, rs1, 6, rd, 0x13);
342}
343
344static u32 rv_xori(u8 rd, u8 rs1, u16 imm11_0)
345{
346 return rv_i_insn(imm11_0, rs1, 4, rd, 0x13);
347}
348
349static u32 rv_slliw(u8 rd, u8 rs1, u16 imm11_0)
350{
351 return rv_i_insn(imm11_0, rs1, 1, rd, 0x1b);
352}
353
354static u32 rv_srliw(u8 rd, u8 rs1, u16 imm11_0)
355{
356 return rv_i_insn(imm11_0, rs1, 5, rd, 0x1b);
357}
358
359static u32 rv_srli(u8 rd, u8 rs1, u16 imm11_0)
360{
361 return rv_i_insn(imm11_0, rs1, 5, rd, 0x13);
362}
363
364static u32 rv_sraiw(u8 rd, u8 rs1, u16 imm11_0)
365{
366 return rv_i_insn(0x400 | imm11_0, rs1, 5, rd, 0x1b);
367}
368
369static u32 rv_srai(u8 rd, u8 rs1, u16 imm11_0)
370{
371 return rv_i_insn(0x400 | imm11_0, rs1, 5, rd, 0x13);
372}
373
374static u32 rv_jal(u8 rd, u32 imm20_1)
375{
376 return rv_uj_insn(imm20_1, rd, 0x6f);
377}
378
379static u32 rv_jalr(u8 rd, u8 rs1, u16 imm11_0)
380{
381 return rv_i_insn(imm11_0, rs1, 0, rd, 0x67);
382}
383
384static u32 rv_beq(u8 rs1, u8 rs2, u16 imm12_1)
385{
386 return rv_sb_insn(imm12_1, rs2, rs1, 0, 0x63);
387}
388
389static u32 rv_bltu(u8 rs1, u8 rs2, u16 imm12_1)
390{
391 return rv_sb_insn(imm12_1, rs2, rs1, 6, 0x63);
392}
393
394static u32 rv_bgeu(u8 rs1, u8 rs2, u16 imm12_1)
395{
396 return rv_sb_insn(imm12_1, rs2, rs1, 7, 0x63);
397}
398
399static u32 rv_bne(u8 rs1, u8 rs2, u16 imm12_1)
400{
401 return rv_sb_insn(imm12_1, rs2, rs1, 1, 0x63);
402}
403
404static u32 rv_blt(u8 rs1, u8 rs2, u16 imm12_1)
405{
406 return rv_sb_insn(imm12_1, rs2, rs1, 4, 0x63);
407}
408
409static u32 rv_bge(u8 rs1, u8 rs2, u16 imm12_1)
410{
411 return rv_sb_insn(imm12_1, rs2, rs1, 5, 0x63);
412}
413
414static u32 rv_sb(u8 rs1, u16 imm11_0, u8 rs2)
415{
416 return rv_s_insn(imm11_0, rs2, rs1, 0, 0x23);
417}
418
419static u32 rv_sh(u8 rs1, u16 imm11_0, u8 rs2)
420{
421 return rv_s_insn(imm11_0, rs2, rs1, 1, 0x23);
422}
423
424static u32 rv_sw(u8 rs1, u16 imm11_0, u8 rs2)
425{
426 return rv_s_insn(imm11_0, rs2, rs1, 2, 0x23);
427}
428
429static u32 rv_sd(u8 rs1, u16 imm11_0, u8 rs2)
430{
431 return rv_s_insn(imm11_0, rs2, rs1, 3, 0x23);
432}
433
434static u32 rv_lbu(u8 rd, u16 imm11_0, u8 rs1)
435{
436 return rv_i_insn(imm11_0, rs1, 4, rd, 0x03);
437}
438
439static u32 rv_lhu(u8 rd, u16 imm11_0, u8 rs1)
440{
441 return rv_i_insn(imm11_0, rs1, 5, rd, 0x03);
442}
443
444static u32 rv_lwu(u8 rd, u16 imm11_0, u8 rs1)
445{
446 return rv_i_insn(imm11_0, rs1, 6, rd, 0x03);
447}
448
449static u32 rv_ld(u8 rd, u16 imm11_0, u8 rs1)
450{
451 return rv_i_insn(imm11_0, rs1, 3, rd, 0x03);
452}
453
454static u32 rv_amoadd_w(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
455{
456 return rv_amo_insn(0, aq, rl, rs2, rs1, 2, rd, 0x2f);
457}
458
459static u32 rv_amoadd_d(u8 rd, u8 rs2, u8 rs1, u8 aq, u8 rl)
460{
461 return rv_amo_insn(0, aq, rl, rs2, rs1, 3, rd, 0x2f);
462}
463
464static bool is_12b_int(s64 val)
465{
466 return -(1 << 11) <= val && val < (1 << 11);
467}
468
469static bool is_13b_int(s64 val)
470{
471 return -(1 << 12) <= val && val < (1 << 12);
472}
473
474static bool is_21b_int(s64 val)
475{
476 return -(1L << 20) <= val && val < (1L << 20);
477}
478
479static bool is_32b_int(s64 val)
480{
481 return -(1L << 31) <= val && val < (1L << 31);
482}
483
484static int is_12b_check(int off, int insn)
485{
486 if (!is_12b_int(off)) {
487 pr_err("bpf-jit: insn=%d offset=%d not supported yet!\n",
488 insn, (int)off);
489 return -1;
490 }
491 return 0;
492}
493
494static int is_13b_check(int off, int insn)
495{
496 if (!is_13b_int(off)) {
497 pr_err("bpf-jit: insn=%d offset=%d not supported yet!\n",
498 insn, (int)off);
499 return -1;
500 }
501 return 0;
502}
503
504static int is_21b_check(int off, int insn)
505{
506 if (!is_21b_int(off)) {
507 pr_err("bpf-jit: insn=%d offset=%d not supported yet!\n",
508 insn, (int)off);
509 return -1;
510 }
511 return 0;
512}
513
514static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)
515{
516 /* Note that the immediate from the add is sign-extended,
517 * which means that we need to compensate this by adding 2^12,
518 * when the 12th bit is set. A simpler way of doing this, and
519 * getting rid of the check, is to just add 2**11 before the
520 * shift. The "Loading a 32-Bit constant" example from the
521 * "Computer Organization and Design, RISC-V edition" book by
522 * Patterson/Hennessy highlights this fact.
523 *
524 * This also means that we need to process LSB to MSB.
525 */
526 s64 upper = (val + (1 << 11)) >> 12, lower = val & 0xfff;
527 int shift;
528
529 if (is_32b_int(val)) {
530 if (upper)
531 emit(rv_lui(rd, upper), ctx);
532
533 if (!upper) {
534 emit(rv_addi(rd, RV_REG_ZERO, lower), ctx);
535 return;
536 }
537
538 emit(rv_addiw(rd, rd, lower), ctx);
539 return;
540 }
541
542 shift = __ffs(upper);
543 upper >>= shift;
544 shift += 12;
545
546 emit_imm(rd, upper, ctx);
547
548 emit(rv_slli(rd, rd, shift), ctx);
549 if (lower)
550 emit(rv_addi(rd, rd, lower), ctx);
551}
552
553static int rv_offset(int bpf_to, int bpf_from, struct rv_jit_context *ctx)
554{
555 int from = ctx->offset[bpf_from] - 1, to = ctx->offset[bpf_to];
556
557 return (to - from) << 2;
558}
559
560static int epilogue_offset(struct rv_jit_context *ctx)
561{
562 int to = ctx->epilogue_offset, from = ctx->ninsns;
563
564 return (to - from) << 2;
565}
566
567static void __build_epilogue(u8 reg, struct rv_jit_context *ctx)
568{
569 int stack_adjust = ctx->stack_size, store_offset = stack_adjust - 8;
570
571 if (seen_reg(RV_REG_RA, ctx)) {
572 emit(rv_ld(RV_REG_RA, store_offset, RV_REG_SP), ctx);
573 store_offset -= 8;
574 }
575 emit(rv_ld(RV_REG_FP, store_offset, RV_REG_SP), ctx);
576 store_offset -= 8;
577 if (seen_reg(RV_REG_S1, ctx)) {
578 emit(rv_ld(RV_REG_S1, store_offset, RV_REG_SP), ctx);
579 store_offset -= 8;
580 }
581 if (seen_reg(RV_REG_S2, ctx)) {
582 emit(rv_ld(RV_REG_S2, store_offset, RV_REG_SP), ctx);
583 store_offset -= 8;
584 }
585 if (seen_reg(RV_REG_S3, ctx)) {
586 emit(rv_ld(RV_REG_S3, store_offset, RV_REG_SP), ctx);
587 store_offset -= 8;
588 }
589 if (seen_reg(RV_REG_S4, ctx)) {
590 emit(rv_ld(RV_REG_S4, store_offset, RV_REG_SP), ctx);
591 store_offset -= 8;
592 }
593 if (seen_reg(RV_REG_S5, ctx)) {
594 emit(rv_ld(RV_REG_S5, store_offset, RV_REG_SP), ctx);
595 store_offset -= 8;
596 }
597 if (seen_reg(RV_REG_S6, ctx)) {
598 emit(rv_ld(RV_REG_S6, store_offset, RV_REG_SP), ctx);
599 store_offset -= 8;
600 }
601
602 emit(rv_addi(RV_REG_SP, RV_REG_SP, stack_adjust), ctx);
603 /* Set return value. */
604 if (reg == RV_REG_RA)
605 emit(rv_addi(RV_REG_A0, RV_REG_A5, 0), ctx);
606 emit(rv_jalr(RV_REG_ZERO, reg, 0), ctx);
607}
608
609static void emit_zext_32(u8 reg, struct rv_jit_context *ctx)
610{
611 emit(rv_slli(reg, reg, 32), ctx);
612 emit(rv_srli(reg, reg, 32), ctx);
613}
614
615static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
616{
617 int tc_ninsn, off, start_insn = ctx->ninsns;
618 u8 tcc = rv_tail_call_reg(ctx);
619
620 /* a0: &ctx
621 * a1: &array
622 * a2: index
623 *
624 * if (index >= array->map.max_entries)
625 * goto out;
626 */
627 tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
628 ctx->offset[0];
629 emit_zext_32(RV_REG_A2, ctx);
630
631 off = offsetof(struct bpf_array, map.max_entries);
632 if (is_12b_check(off, insn))
633 return -1;
634 emit(rv_lwu(RV_REG_T1, off, RV_REG_A1), ctx);
635 off = (tc_ninsn - (ctx->ninsns - start_insn)) << 2;
636 if (is_13b_check(off, insn))
637 return -1;
638 emit(rv_bgeu(RV_REG_A2, RV_REG_T1, off >> 1), ctx);
639
640 /* if (TCC-- < 0)
641 * goto out;
642 */
643 emit(rv_addi(RV_REG_T1, tcc, -1), ctx);
644 off = (tc_ninsn - (ctx->ninsns - start_insn)) << 2;
645 if (is_13b_check(off, insn))
646 return -1;
647 emit(rv_blt(tcc, RV_REG_ZERO, off >> 1), ctx);
648
649 /* prog = array->ptrs[index];
650 * if (!prog)
651 * goto out;
652 */
653 emit(rv_slli(RV_REG_T2, RV_REG_A2, 3), ctx);
654 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_A1), ctx);
655 off = offsetof(struct bpf_array, ptrs);
656 if (is_12b_check(off, insn))
657 return -1;
658 emit(rv_ld(RV_REG_T2, off, RV_REG_T2), ctx);
659 off = (tc_ninsn - (ctx->ninsns - start_insn)) << 2;
660 if (is_13b_check(off, insn))
661 return -1;
662 emit(rv_beq(RV_REG_T2, RV_REG_ZERO, off >> 1), ctx);
663
664 /* goto *(prog->bpf_func + 4); */
665 off = offsetof(struct bpf_prog, bpf_func);
666 if (is_12b_check(off, insn))
667 return -1;
668 emit(rv_ld(RV_REG_T3, off, RV_REG_T2), ctx);
669 emit(rv_addi(RV_REG_T3, RV_REG_T3, 4), ctx);
670 emit(rv_addi(RV_REG_TCC, RV_REG_T1, 0), ctx);
671 __build_epilogue(RV_REG_T3, ctx);
672 return 0;
673}
674
675static void init_regs(u8 *rd, u8 *rs, const struct bpf_insn *insn,
676 struct rv_jit_context *ctx)
677{
678 u8 code = insn->code;
679
680 switch (code) {
681 case BPF_JMP | BPF_JA:
682 case BPF_JMP | BPF_CALL:
683 case BPF_JMP | BPF_EXIT:
684 case BPF_JMP | BPF_TAIL_CALL:
685 break;
686 default:
687 *rd = bpf_to_rv_reg(insn->dst_reg, ctx);
688 }
689
690 if (code & (BPF_ALU | BPF_X) || code & (BPF_ALU64 | BPF_X) ||
691 code & (BPF_JMP | BPF_X) || code & (BPF_JMP32 | BPF_X) ||
692 code & BPF_LDX || code & BPF_STX)
693 *rs = bpf_to_rv_reg(insn->src_reg, ctx);
694}
695
696static int rv_offset_check(int *rvoff, s16 off, int insn,
697 struct rv_jit_context *ctx)
698{
699 *rvoff = rv_offset(insn + off, insn, ctx);
700 return is_13b_check(*rvoff, insn);
701}
702
703static void emit_zext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
704{
705 emit(rv_addi(RV_REG_T2, *rd, 0), ctx);
706 emit_zext_32(RV_REG_T2, ctx);
707 emit(rv_addi(RV_REG_T1, *rs, 0), ctx);
708 emit_zext_32(RV_REG_T1, ctx);
709 *rd = RV_REG_T2;
710 *rs = RV_REG_T1;
711}
712
713static void emit_sext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
714{
715 emit(rv_addiw(RV_REG_T2, *rd, 0), ctx);
716 emit(rv_addiw(RV_REG_T1, *rs, 0), ctx);
717 *rd = RV_REG_T2;
718 *rs = RV_REG_T1;
719}
720
721static void emit_zext_32_rd_t1(u8 *rd, struct rv_jit_context *ctx)
722{
723 emit(rv_addi(RV_REG_T2, *rd, 0), ctx);
724 emit_zext_32(RV_REG_T2, ctx);
725 emit_zext_32(RV_REG_T1, ctx);
726 *rd = RV_REG_T2;
727}
728
729static void emit_sext_32_rd(u8 *rd, struct rv_jit_context *ctx)
730{
731 emit(rv_addiw(RV_REG_T2, *rd, 0), ctx);
732 *rd = RV_REG_T2;
733}
734
735static int emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
736 bool extra_pass)
737{
738 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
739 BPF_CLASS(insn->code) == BPF_JMP;
740 struct bpf_prog_aux *aux = ctx->prog->aux;
741 int rvoff, i = insn - ctx->prog->insnsi;
742 u8 rd = -1, rs = -1, code = insn->code;
743 s16 off = insn->off;
744 s32 imm = insn->imm;
745
746 init_regs(&rd, &rs, insn, ctx);
747
748 switch (code) {
749 /* dst = src */
750 case BPF_ALU | BPF_MOV | BPF_X:
751 case BPF_ALU64 | BPF_MOV | BPF_X:
752 if (imm == 1) {
753 /* Special mov32 for zext */
754 emit_zext_32(rd, ctx);
755 break;
756 }
757 emit(is64 ? rv_addi(rd, rs, 0) : rv_addiw(rd, rs, 0), ctx);
758 if (!is64 && !aux->verifier_zext)
759 emit_zext_32(rd, ctx);
760 break;
761
762 /* dst = dst OP src */
763 case BPF_ALU | BPF_ADD | BPF_X:
764 case BPF_ALU64 | BPF_ADD | BPF_X:
765 emit(is64 ? rv_add(rd, rd, rs) : rv_addw(rd, rd, rs), ctx);
766 if (!is64 && !aux->verifier_zext)
767 emit_zext_32(rd, ctx);
768 break;
769 case BPF_ALU | BPF_SUB | BPF_X:
770 case BPF_ALU64 | BPF_SUB | BPF_X:
771 emit(is64 ? rv_sub(rd, rd, rs) : rv_subw(rd, rd, rs), ctx);
772 if (!is64 && !aux->verifier_zext)
773 emit_zext_32(rd, ctx);
774 break;
775 case BPF_ALU | BPF_AND | BPF_X:
776 case BPF_ALU64 | BPF_AND | BPF_X:
777 emit(rv_and(rd, rd, rs), ctx);
778 if (!is64 && !aux->verifier_zext)
779 emit_zext_32(rd, ctx);
780 break;
781 case BPF_ALU | BPF_OR | BPF_X:
782 case BPF_ALU64 | BPF_OR | BPF_X:
783 emit(rv_or(rd, rd, rs), ctx);
784 if (!is64 && !aux->verifier_zext)
785 emit_zext_32(rd, ctx);
786 break;
787 case BPF_ALU | BPF_XOR | BPF_X:
788 case BPF_ALU64 | BPF_XOR | BPF_X:
789 emit(rv_xor(rd, rd, rs), ctx);
790 if (!is64 && !aux->verifier_zext)
791 emit_zext_32(rd, ctx);
792 break;
793 case BPF_ALU | BPF_MUL | BPF_X:
794 case BPF_ALU64 | BPF_MUL | BPF_X:
795 emit(is64 ? rv_mul(rd, rd, rs) : rv_mulw(rd, rd, rs), ctx);
796 if (!is64 && !aux->verifier_zext)
797 emit_zext_32(rd, ctx);
798 break;
799 case BPF_ALU | BPF_DIV | BPF_X:
800 case BPF_ALU64 | BPF_DIV | BPF_X:
801 emit(is64 ? rv_divu(rd, rd, rs) : rv_divuw(rd, rd, rs), ctx);
802 if (!is64 && !aux->verifier_zext)
803 emit_zext_32(rd, ctx);
804 break;
805 case BPF_ALU | BPF_MOD | BPF_X:
806 case BPF_ALU64 | BPF_MOD | BPF_X:
807 emit(is64 ? rv_remu(rd, rd, rs) : rv_remuw(rd, rd, rs), ctx);
808 if (!is64 && !aux->verifier_zext)
809 emit_zext_32(rd, ctx);
810 break;
811 case BPF_ALU | BPF_LSH | BPF_X:
812 case BPF_ALU64 | BPF_LSH | BPF_X:
813 emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
814 if (!is64)
815 emit_zext_32(rd, ctx);
816 break;
817 case BPF_ALU | BPF_RSH | BPF_X:
818 case BPF_ALU64 | BPF_RSH | BPF_X:
819 emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx);
820 if (!is64 && !aux->verifier_zext)
821 emit_zext_32(rd, ctx);
822 break;
823 case BPF_ALU | BPF_ARSH | BPF_X:
824 case BPF_ALU64 | BPF_ARSH | BPF_X:
825 emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx);
826 if (!is64 && !aux->verifier_zext)
827 emit_zext_32(rd, ctx);
828 break;
829
830 /* dst = -dst */
831 case BPF_ALU | BPF_NEG:
832 case BPF_ALU64 | BPF_NEG:
833 emit(is64 ? rv_sub(rd, RV_REG_ZERO, rd) :
834 rv_subw(rd, RV_REG_ZERO, rd), ctx);
835 if (!is64 && !aux->verifier_zext)
836 emit_zext_32(rd, ctx);
837 break;
838
839 /* dst = BSWAP##imm(dst) */
840 case BPF_ALU | BPF_END | BPF_FROM_LE:
841 {
842 int shift = 64 - imm;
843
844 emit(rv_slli(rd, rd, shift), ctx);
845 emit(rv_srli(rd, rd, shift), ctx);
846 break;
847 }
848 case BPF_ALU | BPF_END | BPF_FROM_BE:
849 emit(rv_addi(RV_REG_T2, RV_REG_ZERO, 0), ctx);
850
851 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
852 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
853 emit(rv_slli(RV_REG_T2, RV_REG_T2, 8), ctx);
854 emit(rv_srli(rd, rd, 8), ctx);
855 if (imm == 16)
856 goto out_be;
857
858 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
859 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
860 emit(rv_slli(RV_REG_T2, RV_REG_T2, 8), ctx);
861 emit(rv_srli(rd, rd, 8), ctx);
862
863 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
864 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
865 emit(rv_slli(RV_REG_T2, RV_REG_T2, 8), ctx);
866 emit(rv_srli(rd, rd, 8), ctx);
867 if (imm == 32)
868 goto out_be;
869
870 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
871 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
872 emit(rv_slli(RV_REG_T2, RV_REG_T2, 8), ctx);
873 emit(rv_srli(rd, rd, 8), ctx);
874
875 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
876 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
877 emit(rv_slli(RV_REG_T2, RV_REG_T2, 8), ctx);
878 emit(rv_srli(rd, rd, 8), ctx);
879
880 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
881 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
882 emit(rv_slli(RV_REG_T2, RV_REG_T2, 8), ctx);
883 emit(rv_srli(rd, rd, 8), ctx);
884
885 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
886 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
887 emit(rv_slli(RV_REG_T2, RV_REG_T2, 8), ctx);
888 emit(rv_srli(rd, rd, 8), ctx);
889out_be:
890 emit(rv_andi(RV_REG_T1, rd, 0xff), ctx);
891 emit(rv_add(RV_REG_T2, RV_REG_T2, RV_REG_T1), ctx);
892
893 emit(rv_addi(rd, RV_REG_T2, 0), ctx);
894 break;
895
896 /* dst = imm */
897 case BPF_ALU | BPF_MOV | BPF_K:
898 case BPF_ALU64 | BPF_MOV | BPF_K:
899 emit_imm(rd, imm, ctx);
900 if (!is64 && !aux->verifier_zext)
901 emit_zext_32(rd, ctx);
902 break;
903
904 /* dst = dst OP imm */
905 case BPF_ALU | BPF_ADD | BPF_K:
906 case BPF_ALU64 | BPF_ADD | BPF_K:
907 if (is_12b_int(imm)) {
908 emit(is64 ? rv_addi(rd, rd, imm) :
909 rv_addiw(rd, rd, imm), ctx);
910 } else {
911 emit_imm(RV_REG_T1, imm, ctx);
912 emit(is64 ? rv_add(rd, rd, RV_REG_T1) :
913 rv_addw(rd, rd, RV_REG_T1), ctx);
914 }
915 if (!is64 && !aux->verifier_zext)
916 emit_zext_32(rd, ctx);
917 break;
918 case BPF_ALU | BPF_SUB | BPF_K:
919 case BPF_ALU64 | BPF_SUB | BPF_K:
920 if (is_12b_int(-imm)) {
921 emit(is64 ? rv_addi(rd, rd, -imm) :
922 rv_addiw(rd, rd, -imm), ctx);
923 } else {
924 emit_imm(RV_REG_T1, imm, ctx);
925 emit(is64 ? rv_sub(rd, rd, RV_REG_T1) :
926 rv_subw(rd, rd, RV_REG_T1), ctx);
927 }
928 if (!is64 && !aux->verifier_zext)
929 emit_zext_32(rd, ctx);
930 break;
931 case BPF_ALU | BPF_AND | BPF_K:
932 case BPF_ALU64 | BPF_AND | BPF_K:
933 if (is_12b_int(imm)) {
934 emit(rv_andi(rd, rd, imm), ctx);
935 } else {
936 emit_imm(RV_REG_T1, imm, ctx);
937 emit(rv_and(rd, rd, RV_REG_T1), ctx);
938 }
939 if (!is64 && !aux->verifier_zext)
940 emit_zext_32(rd, ctx);
941 break;
942 case BPF_ALU | BPF_OR | BPF_K:
943 case BPF_ALU64 | BPF_OR | BPF_K:
944 if (is_12b_int(imm)) {
945 emit(rv_ori(rd, rd, imm), ctx);
946 } else {
947 emit_imm(RV_REG_T1, imm, ctx);
948 emit(rv_or(rd, rd, RV_REG_T1), ctx);
949 }
950 if (!is64 && !aux->verifier_zext)
951 emit_zext_32(rd, ctx);
952 break;
953 case BPF_ALU | BPF_XOR | BPF_K:
954 case BPF_ALU64 | BPF_XOR | BPF_K:
955 if (is_12b_int(imm)) {
956 emit(rv_xori(rd, rd, imm), ctx);
957 } else {
958 emit_imm(RV_REG_T1, imm, ctx);
959 emit(rv_xor(rd, rd, RV_REG_T1), ctx);
960 }
961 if (!is64 && !aux->verifier_zext)
962 emit_zext_32(rd, ctx);
963 break;
964 case BPF_ALU | BPF_MUL | BPF_K:
965 case BPF_ALU64 | BPF_MUL | BPF_K:
966 emit_imm(RV_REG_T1, imm, ctx);
967 emit(is64 ? rv_mul(rd, rd, RV_REG_T1) :
968 rv_mulw(rd, rd, RV_REG_T1), ctx);
969 if (!is64 && !aux->verifier_zext)
970 emit_zext_32(rd, ctx);
971 break;
972 case BPF_ALU | BPF_DIV | BPF_K:
973 case BPF_ALU64 | BPF_DIV | BPF_K:
974 emit_imm(RV_REG_T1, imm, ctx);
975 emit(is64 ? rv_divu(rd, rd, RV_REG_T1) :
976 rv_divuw(rd, rd, RV_REG_T1), ctx);
977 if (!is64 && !aux->verifier_zext)
978 emit_zext_32(rd, ctx);
979 break;
980 case BPF_ALU | BPF_MOD | BPF_K:
981 case BPF_ALU64 | BPF_MOD | BPF_K:
982 emit_imm(RV_REG_T1, imm, ctx);
983 emit(is64 ? rv_remu(rd, rd, RV_REG_T1) :
984 rv_remuw(rd, rd, RV_REG_T1), ctx);
985 if (!is64 && !aux->verifier_zext)
986 emit_zext_32(rd, ctx);
987 break;
988 case BPF_ALU | BPF_LSH | BPF_K:
989 case BPF_ALU64 | BPF_LSH | BPF_K:
990 emit(is64 ? rv_slli(rd, rd, imm) : rv_slliw(rd, rd, imm), ctx);
991 if (!is64)
992 emit_zext_32(rd, ctx);
993 break;
994 case BPF_ALU | BPF_RSH | BPF_K:
995 case BPF_ALU64 | BPF_RSH | BPF_K:
996 emit(is64 ? rv_srli(rd, rd, imm) : rv_srliw(rd, rd, imm), ctx);
997 if (!is64)
998 emit_zext_32(rd, ctx);
999 break;
1000 case BPF_ALU | BPF_ARSH | BPF_K:
1001 case BPF_ALU64 | BPF_ARSH | BPF_K:
1002 emit(is64 ? rv_srai(rd, rd, imm) : rv_sraiw(rd, rd, imm), ctx);
1003 if (!is64)
1004 emit_zext_32(rd, ctx);
1005 break;
1006
1007 /* JUMP off */
1008 case BPF_JMP | BPF_JA:
1009 rvoff = rv_offset(i + off, i, ctx);
1010 if (!is_21b_int(rvoff)) {
1011 pr_err("bpf-jit: insn=%d offset=%d not supported yet!\n",
1012 i, rvoff);
1013 return -1;
1014 }
1015
1016 emit(rv_jal(RV_REG_ZERO, rvoff >> 1), ctx);
1017 break;
1018
1019 /* IF (dst COND src) JUMP off */
1020 case BPF_JMP | BPF_JEQ | BPF_X:
1021 case BPF_JMP32 | BPF_JEQ | BPF_X:
1022 if (rv_offset_check(&rvoff, off, i, ctx))
1023 return -1;
1024 if (!is64)
1025 emit_zext_32_rd_rs(&rd, &rs, ctx);
1026 emit(rv_beq(rd, rs, rvoff >> 1), ctx);
1027 break;
1028 case BPF_JMP | BPF_JGT | BPF_X:
1029 case BPF_JMP32 | BPF_JGT | BPF_X:
1030 if (rv_offset_check(&rvoff, off, i, ctx))
1031 return -1;
1032 if (!is64)
1033 emit_zext_32_rd_rs(&rd, &rs, ctx);
1034 emit(rv_bltu(rs, rd, rvoff >> 1), ctx);
1035 break;
1036 case BPF_JMP | BPF_JLT | BPF_X:
1037 case BPF_JMP32 | BPF_JLT | BPF_X:
1038 if (rv_offset_check(&rvoff, off, i, ctx))
1039 return -1;
1040 if (!is64)
1041 emit_zext_32_rd_rs(&rd, &rs, ctx);
1042 emit(rv_bltu(rd, rs, rvoff >> 1), ctx);
1043 break;
1044 case BPF_JMP | BPF_JGE | BPF_X:
1045 case BPF_JMP32 | BPF_JGE | BPF_X:
1046 if (rv_offset_check(&rvoff, off, i, ctx))
1047 return -1;
1048 if (!is64)
1049 emit_zext_32_rd_rs(&rd, &rs, ctx);
1050 emit(rv_bgeu(rd, rs, rvoff >> 1), ctx);
1051 break;
1052 case BPF_JMP | BPF_JLE | BPF_X:
1053 case BPF_JMP32 | BPF_JLE | BPF_X:
1054 if (rv_offset_check(&rvoff, off, i, ctx))
1055 return -1;
1056 if (!is64)
1057 emit_zext_32_rd_rs(&rd, &rs, ctx);
1058 emit(rv_bgeu(rs, rd, rvoff >> 1), ctx);
1059 break;
1060 case BPF_JMP | BPF_JNE | BPF_X:
1061 case BPF_JMP32 | BPF_JNE | BPF_X:
1062 if (rv_offset_check(&rvoff, off, i, ctx))
1063 return -1;
1064 if (!is64)
1065 emit_zext_32_rd_rs(&rd, &rs, ctx);
1066 emit(rv_bne(rd, rs, rvoff >> 1), ctx);
1067 break;
1068 case BPF_JMP | BPF_JSGT | BPF_X:
1069 case BPF_JMP32 | BPF_JSGT | BPF_X:
1070 if (rv_offset_check(&rvoff, off, i, ctx))
1071 return -1;
1072 if (!is64)
1073 emit_sext_32_rd_rs(&rd, &rs, ctx);
1074 emit(rv_blt(rs, rd, rvoff >> 1), ctx);
1075 break;
1076 case BPF_JMP | BPF_JSLT | BPF_X:
1077 case BPF_JMP32 | BPF_JSLT | BPF_X:
1078 if (rv_offset_check(&rvoff, off, i, ctx))
1079 return -1;
1080 if (!is64)
1081 emit_sext_32_rd_rs(&rd, &rs, ctx);
1082 emit(rv_blt(rd, rs, rvoff >> 1), ctx);
1083 break;
1084 case BPF_JMP | BPF_JSGE | BPF_X:
1085 case BPF_JMP32 | BPF_JSGE | BPF_X:
1086 if (rv_offset_check(&rvoff, off, i, ctx))
1087 return -1;
1088 if (!is64)
1089 emit_sext_32_rd_rs(&rd, &rs, ctx);
1090 emit(rv_bge(rd, rs, rvoff >> 1), ctx);
1091 break;
1092 case BPF_JMP | BPF_JSLE | BPF_X:
1093 case BPF_JMP32 | BPF_JSLE | BPF_X:
1094 if (rv_offset_check(&rvoff, off, i, ctx))
1095 return -1;
1096 if (!is64)
1097 emit_sext_32_rd_rs(&rd, &rs, ctx);
1098 emit(rv_bge(rs, rd, rvoff >> 1), ctx);
1099 break;
1100 case BPF_JMP | BPF_JSET | BPF_X:
1101 case BPF_JMP32 | BPF_JSET | BPF_X:
1102 if (rv_offset_check(&rvoff, off, i, ctx))
1103 return -1;
1104 if (!is64)
1105 emit_zext_32_rd_rs(&rd, &rs, ctx);
1106 emit(rv_and(RV_REG_T1, rd, rs), ctx);
1107 emit(rv_bne(RV_REG_T1, RV_REG_ZERO, rvoff >> 1), ctx);
1108 break;
1109
1110 /* IF (dst COND imm) JUMP off */
1111 case BPF_JMP | BPF_JEQ | BPF_K:
1112 case BPF_JMP32 | BPF_JEQ | BPF_K:
1113 if (rv_offset_check(&rvoff, off, i, ctx))
1114 return -1;
1115 emit_imm(RV_REG_T1, imm, ctx);
1116 if (!is64)
1117 emit_zext_32_rd_t1(&rd, ctx);
1118 emit(rv_beq(rd, RV_REG_T1, rvoff >> 1), ctx);
1119 break;
1120 case BPF_JMP | BPF_JGT | BPF_K:
1121 case BPF_JMP32 | BPF_JGT | BPF_K:
1122 if (rv_offset_check(&rvoff, off, i, ctx))
1123 return -1;
1124 emit_imm(RV_REG_T1, imm, ctx);
1125 if (!is64)
1126 emit_zext_32_rd_t1(&rd, ctx);
1127 emit(rv_bltu(RV_REG_T1, rd, rvoff >> 1), ctx);
1128 break;
1129 case BPF_JMP | BPF_JLT | BPF_K:
1130 case BPF_JMP32 | BPF_JLT | BPF_K:
1131 if (rv_offset_check(&rvoff, off, i, ctx))
1132 return -1;
1133 emit_imm(RV_REG_T1, imm, ctx);
1134 if (!is64)
1135 emit_zext_32_rd_t1(&rd, ctx);
1136 emit(rv_bltu(rd, RV_REG_T1, rvoff >> 1), ctx);
1137 break;
1138 case BPF_JMP | BPF_JGE | BPF_K:
1139 case BPF_JMP32 | BPF_JGE | BPF_K:
1140 if (rv_offset_check(&rvoff, off, i, ctx))
1141 return -1;
1142 emit_imm(RV_REG_T1, imm, ctx);
1143 if (!is64)
1144 emit_zext_32_rd_t1(&rd, ctx);
1145 emit(rv_bgeu(rd, RV_REG_T1, rvoff >> 1), ctx);
1146 break;
1147 case BPF_JMP | BPF_JLE | BPF_K:
1148 case BPF_JMP32 | BPF_JLE | BPF_K:
1149 if (rv_offset_check(&rvoff, off, i, ctx))
1150 return -1;
1151 emit_imm(RV_REG_T1, imm, ctx);
1152 if (!is64)
1153 emit_zext_32_rd_t1(&rd, ctx);
1154 emit(rv_bgeu(RV_REG_T1, rd, rvoff >> 1), ctx);
1155 break;
1156 case BPF_JMP | BPF_JNE | BPF_K:
1157 case BPF_JMP32 | BPF_JNE | BPF_K:
1158 if (rv_offset_check(&rvoff, off, i, ctx))
1159 return -1;
1160 emit_imm(RV_REG_T1, imm, ctx);
1161 if (!is64)
1162 emit_zext_32_rd_t1(&rd, ctx);
1163 emit(rv_bne(rd, RV_REG_T1, rvoff >> 1), ctx);
1164 break;
1165 case BPF_JMP | BPF_JSGT | BPF_K:
1166 case BPF_JMP32 | BPF_JSGT | BPF_K:
1167 if (rv_offset_check(&rvoff, off, i, ctx))
1168 return -1;
1169 emit_imm(RV_REG_T1, imm, ctx);
1170 if (!is64)
1171 emit_sext_32_rd(&rd, ctx);
1172 emit(rv_blt(RV_REG_T1, rd, rvoff >> 1), ctx);
1173 break;
1174 case BPF_JMP | BPF_JSLT | BPF_K:
1175 case BPF_JMP32 | BPF_JSLT | BPF_K:
1176 if (rv_offset_check(&rvoff, off, i, ctx))
1177 return -1;
1178 emit_imm(RV_REG_T1, imm, ctx);
1179 if (!is64)
1180 emit_sext_32_rd(&rd, ctx);
1181 emit(rv_blt(rd, RV_REG_T1, rvoff >> 1), ctx);
1182 break;
1183 case BPF_JMP | BPF_JSGE | BPF_K:
1184 case BPF_JMP32 | BPF_JSGE | BPF_K:
1185 if (rv_offset_check(&rvoff, off, i, ctx))
1186 return -1;
1187 emit_imm(RV_REG_T1, imm, ctx);
1188 if (!is64)
1189 emit_sext_32_rd(&rd, ctx);
1190 emit(rv_bge(rd, RV_REG_T1, rvoff >> 1), ctx);
1191 break;
1192 case BPF_JMP | BPF_JSLE | BPF_K:
1193 case BPF_JMP32 | BPF_JSLE | BPF_K:
1194 if (rv_offset_check(&rvoff, off, i, ctx))
1195 return -1;
1196 emit_imm(RV_REG_T1, imm, ctx);
1197 if (!is64)
1198 emit_sext_32_rd(&rd, ctx);
1199 emit(rv_bge(RV_REG_T1, rd, rvoff >> 1), ctx);
1200 break;
1201 case BPF_JMP | BPF_JSET | BPF_K:
1202 case BPF_JMP32 | BPF_JSET | BPF_K:
1203 if (rv_offset_check(&rvoff, off, i, ctx))
1204 return -1;
1205 emit_imm(RV_REG_T1, imm, ctx);
1206 if (!is64)
1207 emit_zext_32_rd_t1(&rd, ctx);
1208 emit(rv_and(RV_REG_T1, rd, RV_REG_T1), ctx);
1209 emit(rv_bne(RV_REG_T1, RV_REG_ZERO, rvoff >> 1), ctx);
1210 break;
1211
1212 /* function call */
1213 case BPF_JMP | BPF_CALL:
1214 {
1215 bool fixed;
1216 int i, ret;
1217 u64 addr;
1218
1219 mark_call(ctx);
1220 ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &addr,
1221 &fixed);
1222 if (ret < 0)
1223 return ret;
1224 if (fixed) {
1225 emit_imm(RV_REG_T1, addr, ctx);
1226 } else {
1227 i = ctx->ninsns;
1228 emit_imm(RV_REG_T1, addr, ctx);
1229 for (i = ctx->ninsns - i; i < 8; i++) {
1230 /* nop */
1231 emit(rv_addi(RV_REG_ZERO, RV_REG_ZERO, 0),
1232 ctx);
1233 }
1234 }
1235 emit(rv_jalr(RV_REG_RA, RV_REG_T1, 0), ctx);
1236 rd = bpf_to_rv_reg(BPF_REG_0, ctx);
1237 emit(rv_addi(rd, RV_REG_A0, 0), ctx);
1238 break;
1239 }
1240 /* tail call */
1241 case BPF_JMP | BPF_TAIL_CALL:
1242 if (emit_bpf_tail_call(i, ctx))
1243 return -1;
1244 break;
1245
1246 /* function return */
1247 case BPF_JMP | BPF_EXIT:
1248 if (i == ctx->prog->len - 1)
1249 break;
1250
1251 rvoff = epilogue_offset(ctx);
1252 if (is_21b_check(rvoff, i))
1253 return -1;
1254 emit(rv_jal(RV_REG_ZERO, rvoff >> 1), ctx);
1255 break;
1256
1257 /* dst = imm64 */
1258 case BPF_LD | BPF_IMM | BPF_DW:
1259 {
1260 struct bpf_insn insn1 = insn[1];
1261 u64 imm64;
1262
1263 imm64 = (u64)insn1.imm << 32 | (u32)imm;
1264 emit_imm(rd, imm64, ctx);
1265 return 1;
1266 }
1267
1268 /* LDX: dst = *(size *)(src + off) */
1269 case BPF_LDX | BPF_MEM | BPF_B:
1270 if (is_12b_int(off)) {
1271 emit(rv_lbu(rd, off, rs), ctx);
1272 break;
1273 }
1274
1275 emit_imm(RV_REG_T1, off, ctx);
1276 emit(rv_add(RV_REG_T1, RV_REG_T1, rs), ctx);
1277 emit(rv_lbu(rd, 0, RV_REG_T1), ctx);
1278 if (insn_is_zext(&insn[1]))
1279 return 1;
1280 break;
1281 case BPF_LDX | BPF_MEM | BPF_H:
1282 if (is_12b_int(off)) {
1283 emit(rv_lhu(rd, off, rs), ctx);
1284 break;
1285 }
1286
1287 emit_imm(RV_REG_T1, off, ctx);
1288 emit(rv_add(RV_REG_T1, RV_REG_T1, rs), ctx);
1289 emit(rv_lhu(rd, 0, RV_REG_T1), ctx);
1290 if (insn_is_zext(&insn[1]))
1291 return 1;
1292 break;
1293 case BPF_LDX | BPF_MEM | BPF_W:
1294 if (is_12b_int(off)) {
1295 emit(rv_lwu(rd, off, rs), ctx);
1296 break;
1297 }
1298
1299 emit_imm(RV_REG_T1, off, ctx);
1300 emit(rv_add(RV_REG_T1, RV_REG_T1, rs), ctx);
1301 emit(rv_lwu(rd, 0, RV_REG_T1), ctx);
1302 if (insn_is_zext(&insn[1]))
1303 return 1;
1304 break;
1305 case BPF_LDX | BPF_MEM | BPF_DW:
1306 if (is_12b_int(off)) {
1307 emit(rv_ld(rd, off, rs), ctx);
1308 break;
1309 }
1310
1311 emit_imm(RV_REG_T1, off, ctx);
1312 emit(rv_add(RV_REG_T1, RV_REG_T1, rs), ctx);
1313 emit(rv_ld(rd, 0, RV_REG_T1), ctx);
1314 break;
1315
1316 /* speculation barrier */
1317 case BPF_ST | BPF_NOSPEC:
1318 break;
1319
1320 /* ST: *(size *)(dst + off) = imm */
1321 case BPF_ST | BPF_MEM | BPF_B:
1322 emit_imm(RV_REG_T1, imm, ctx);
1323 if (is_12b_int(off)) {
1324 emit(rv_sb(rd, off, RV_REG_T1), ctx);
1325 break;
1326 }
1327
1328 emit_imm(RV_REG_T2, off, ctx);
1329 emit(rv_add(RV_REG_T2, RV_REG_T2, rd), ctx);
1330 emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
1331 break;
1332
1333 case BPF_ST | BPF_MEM | BPF_H:
1334 emit_imm(RV_REG_T1, imm, ctx);
1335 if (is_12b_int(off)) {
1336 emit(rv_sh(rd, off, RV_REG_T1), ctx);
1337 break;
1338 }
1339
1340 emit_imm(RV_REG_T2, off, ctx);
1341 emit(rv_add(RV_REG_T2, RV_REG_T2, rd), ctx);
1342 emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
1343 break;
1344 case BPF_ST | BPF_MEM | BPF_W:
1345 emit_imm(RV_REG_T1, imm, ctx);
1346 if (is_12b_int(off)) {
1347 emit(rv_sw(rd, off, RV_REG_T1), ctx);
1348 break;
1349 }
1350
1351 emit_imm(RV_REG_T2, off, ctx);
1352 emit(rv_add(RV_REG_T2, RV_REG_T2, rd), ctx);
1353 emit(rv_sw(RV_REG_T2, 0, RV_REG_T1), ctx);
1354 break;
1355 case BPF_ST | BPF_MEM | BPF_DW:
1356 emit_imm(RV_REG_T1, imm, ctx);
1357 if (is_12b_int(off)) {
1358 emit(rv_sd(rd, off, RV_REG_T1), ctx);
1359 break;
1360 }
1361
1362 emit_imm(RV_REG_T2, off, ctx);
1363 emit(rv_add(RV_REG_T2, RV_REG_T2, rd), ctx);
1364 emit(rv_sd(RV_REG_T2, 0, RV_REG_T1), ctx);
1365 break;
1366
1367 /* STX: *(size *)(dst + off) = src */
1368 case BPF_STX | BPF_MEM | BPF_B:
1369 if (is_12b_int(off)) {
1370 emit(rv_sb(rd, off, rs), ctx);
1371 break;
1372 }
1373
1374 emit_imm(RV_REG_T1, off, ctx);
1375 emit(rv_add(RV_REG_T1, RV_REG_T1, rd), ctx);
1376 emit(rv_sb(RV_REG_T1, 0, rs), ctx);
1377 break;
1378 case BPF_STX | BPF_MEM | BPF_H:
1379 if (is_12b_int(off)) {
1380 emit(rv_sh(rd, off, rs), ctx);
1381 break;
1382 }
1383
1384 emit_imm(RV_REG_T1, off, ctx);
1385 emit(rv_add(RV_REG_T1, RV_REG_T1, rd), ctx);
1386 emit(rv_sh(RV_REG_T1, 0, rs), ctx);
1387 break;
1388 case BPF_STX | BPF_MEM | BPF_W:
1389 if (is_12b_int(off)) {
1390 emit(rv_sw(rd, off, rs), ctx);
1391 break;
1392 }
1393
1394 emit_imm(RV_REG_T1, off, ctx);
1395 emit(rv_add(RV_REG_T1, RV_REG_T1, rd), ctx);
1396 emit(rv_sw(RV_REG_T1, 0, rs), ctx);
1397 break;
1398 case BPF_STX | BPF_MEM | BPF_DW:
1399 if (is_12b_int(off)) {
1400 emit(rv_sd(rd, off, rs), ctx);
1401 break;
1402 }
1403
1404 emit_imm(RV_REG_T1, off, ctx);
1405 emit(rv_add(RV_REG_T1, RV_REG_T1, rd), ctx);
1406 emit(rv_sd(RV_REG_T1, 0, rs), ctx);
1407 break;
1408 /* STX XADD: lock *(u32 *)(dst + off) += src */
1409 case BPF_STX | BPF_XADD | BPF_W:
1410 /* STX XADD: lock *(u64 *)(dst + off) += src */
1411 case BPF_STX | BPF_XADD | BPF_DW:
1412 if (off) {
1413 if (is_12b_int(off)) {
1414 emit(rv_addi(RV_REG_T1, rd, off), ctx);
1415 } else {
1416 emit_imm(RV_REG_T1, off, ctx);
1417 emit(rv_add(RV_REG_T1, RV_REG_T1, rd), ctx);
1418 }
1419
1420 rd = RV_REG_T1;
1421 }
1422
1423 emit(BPF_SIZE(code) == BPF_W ?
1424 rv_amoadd_w(RV_REG_ZERO, rs, rd, 0, 0) :
1425 rv_amoadd_d(RV_REG_ZERO, rs, rd, 0, 0), ctx);
1426 break;
1427 default:
1428 pr_err("bpf-jit: unknown opcode %02x\n", code);
1429 return -EINVAL;
1430 }
1431
1432 return 0;
1433}
1434
1435static void build_prologue(struct rv_jit_context *ctx)
1436{
1437 int stack_adjust = 0, store_offset, bpf_stack_adjust;
1438
1439 bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
1440 if (bpf_stack_adjust)
1441 mark_fp(ctx);
1442
1443 if (seen_reg(RV_REG_RA, ctx))
1444 stack_adjust += 8;
1445 stack_adjust += 8; /* RV_REG_FP */
1446 if (seen_reg(RV_REG_S1, ctx))
1447 stack_adjust += 8;
1448 if (seen_reg(RV_REG_S2, ctx))
1449 stack_adjust += 8;
1450 if (seen_reg(RV_REG_S3, ctx))
1451 stack_adjust += 8;
1452 if (seen_reg(RV_REG_S4, ctx))
1453 stack_adjust += 8;
1454 if (seen_reg(RV_REG_S5, ctx))
1455 stack_adjust += 8;
1456 if (seen_reg(RV_REG_S6, ctx))
1457 stack_adjust += 8;
1458
1459 stack_adjust = round_up(stack_adjust, 16);
1460 stack_adjust += bpf_stack_adjust;
1461
1462 store_offset = stack_adjust - 8;
1463
1464 /* First instruction is always setting the tail-call-counter
1465 * (TCC) register. This instruction is skipped for tail calls.
1466 */
1467 emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
1468
1469 emit(rv_addi(RV_REG_SP, RV_REG_SP, -stack_adjust), ctx);
1470
1471 if (seen_reg(RV_REG_RA, ctx)) {
1472 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_RA), ctx);
1473 store_offset -= 8;
1474 }
1475 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_FP), ctx);
1476 store_offset -= 8;
1477 if (seen_reg(RV_REG_S1, ctx)) {
1478 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_S1), ctx);
1479 store_offset -= 8;
1480 }
1481 if (seen_reg(RV_REG_S2, ctx)) {
1482 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_S2), ctx);
1483 store_offset -= 8;
1484 }
1485 if (seen_reg(RV_REG_S3, ctx)) {
1486 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_S3), ctx);
1487 store_offset -= 8;
1488 }
1489 if (seen_reg(RV_REG_S4, ctx)) {
1490 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_S4), ctx);
1491 store_offset -= 8;
1492 }
1493 if (seen_reg(RV_REG_S5, ctx)) {
1494 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_S5), ctx);
1495 store_offset -= 8;
1496 }
1497 if (seen_reg(RV_REG_S6, ctx)) {
1498 emit(rv_sd(RV_REG_SP, store_offset, RV_REG_S6), ctx);
1499 store_offset -= 8;
1500 }
1501
1502 emit(rv_addi(RV_REG_FP, RV_REG_SP, stack_adjust), ctx);
1503
1504 if (bpf_stack_adjust)
1505 emit(rv_addi(RV_REG_S5, RV_REG_SP, bpf_stack_adjust), ctx);
1506
1507 /* Program contains calls and tail calls, so RV_REG_TCC need
1508 * to be saved across calls.
1509 */
1510 if (seen_tail_call(ctx) && seen_call(ctx))
1511 emit(rv_addi(RV_REG_TCC_SAVED, RV_REG_TCC, 0), ctx);
1512
1513 ctx->stack_size = stack_adjust;
1514}
1515
1516static void build_epilogue(struct rv_jit_context *ctx)
1517{
1518 __build_epilogue(RV_REG_RA, ctx);
1519}
1520
1521static int build_body(struct rv_jit_context *ctx, bool extra_pass)
1522{
1523 const struct bpf_prog *prog = ctx->prog;
1524 int i;
1525
1526 for (i = 0; i < prog->len; i++) {
1527 const struct bpf_insn *insn = &prog->insnsi[i];
1528 int ret;
1529
1530 ret = emit_insn(insn, ctx, extra_pass);
1531 if (ret > 0) {
1532 i++;
1533 if (ctx->insns == NULL)
1534 ctx->offset[i] = ctx->ninsns;
1535 continue;
1536 }
1537 if (ctx->insns == NULL)
1538 ctx->offset[i] = ctx->ninsns;
1539 if (ret)
1540 return ret;
1541 }
1542 return 0;
1543}
1544
1545static void bpf_fill_ill_insns(void *area, unsigned int size)
1546{
1547 memset(area, 0, size);
1548}
1549
1550static void bpf_flush_icache(void *start, void *end)
1551{
1552 flush_icache_range((unsigned long)start, (unsigned long)end);
1553}
1554
1555bool bpf_jit_needs_zext(void)
1556{
1557 return true;
1558}
1559
1560struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
1561{
1562 bool tmp_blinded = false, extra_pass = false;
1563 struct bpf_prog *tmp, *orig_prog = prog;
1564 struct rv_jit_data *jit_data;
1565 struct rv_jit_context *ctx;
1566 unsigned int image_size;
1567
1568 if (!prog->jit_requested)
1569 return orig_prog;
1570
1571 tmp = bpf_jit_blind_constants(prog);
1572 if (IS_ERR(tmp))
1573 return orig_prog;
1574 if (tmp != prog) {
1575 tmp_blinded = true;
1576 prog = tmp;
1577 }
1578
1579 jit_data = prog->aux->jit_data;
1580 if (!jit_data) {
1581 jit_data = kzalloc(sizeof(*jit_data), GFP_KERNEL);
1582 if (!jit_data) {
1583 prog = orig_prog;
1584 goto out;
1585 }
1586 prog->aux->jit_data = jit_data;
1587 }
1588
1589 ctx = &jit_data->ctx;
1590
1591 if (ctx->offset) {
1592 extra_pass = true;
1593 image_size = sizeof(u32) * ctx->ninsns;
1594 goto skip_init_ctx;
1595 }
1596
1597 ctx->prog = prog;
1598 ctx->offset = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
1599 if (!ctx->offset) {
1600 prog = orig_prog;
1601 goto out_offset;
1602 }
1603
1604 /* First pass generates the ctx->offset, but does not emit an image. */
1605 if (build_body(ctx, extra_pass)) {
1606 prog = orig_prog;
1607 goto out_offset;
1608 }
1609 build_prologue(ctx);
1610 ctx->epilogue_offset = ctx->ninsns;
1611 build_epilogue(ctx);
1612
1613 /* Allocate image, now that we know the size. */
1614 image_size = sizeof(u32) * ctx->ninsns;
1615 jit_data->header = bpf_jit_binary_alloc(image_size, &jit_data->image,
1616 sizeof(u32),
1617 bpf_fill_ill_insns);
1618 if (!jit_data->header) {
1619 prog = orig_prog;
1620 goto out_offset;
1621 }
1622
1623 /* Second, real pass, that acutally emits the image. */
1624 ctx->insns = (u32 *)jit_data->image;
1625skip_init_ctx:
1626 ctx->ninsns = 0;
1627
1628 build_prologue(ctx);
1629 if (build_body(ctx, extra_pass)) {
1630 bpf_jit_binary_free(jit_data->header);
1631 prog = orig_prog;
1632 goto out_offset;
1633 }
1634 build_epilogue(ctx);
1635
1636 if (bpf_jit_enable > 1)
1637 bpf_jit_dump(prog->len, image_size, 2, ctx->insns);
1638
1639 prog->bpf_func = (void *)ctx->insns;
1640 prog->jited = 1;
1641 prog->jited_len = image_size;
1642
1643 bpf_flush_icache(jit_data->header, ctx->insns + ctx->ninsns);
1644
1645 if (!prog->is_func || extra_pass) {
1646out_offset:
1647 kfree(ctx->offset);
1648 kfree(jit_data);
1649 prog->aux->jit_data = NULL;
1650 }
1651out:
1652 if (tmp_blinded)
1653 bpf_jit_prog_release_other(prog, prog == orig_prog ?
1654 tmp : orig_prog);
1655 return prog;
1656}