blob: 603630b6f3c5bd699a9a40095f1236ec40d189c6 [file] [log] [blame]
Björn Töpel2353ecc2019-02-05 13:41:22 +01001// 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
Tong Tiangen252c7652021-10-27 11:18:22 +00008#include <linux/bitfield.h>
Björn Töpel2353ecc2019-02-05 13:41:22 +01009#include <linux/bpf.h>
10#include <linux/filter.h>
Luke Nelsonca6cb542020-03-04 21:02:04 -080011#include "bpf_jit.h"
Björn Töpel2353ecc2019-02-05 13:41:22 +010012
13#define RV_REG_TCC RV_REG_A6
14#define RV_REG_TCC_SAVED RV_REG_S6 /* Store A6 in S6 if program do calls */
15
16static const int regmap[] = {
17 [BPF_REG_0] = RV_REG_A5,
18 [BPF_REG_1] = RV_REG_A0,
19 [BPF_REG_2] = RV_REG_A1,
20 [BPF_REG_3] = RV_REG_A2,
21 [BPF_REG_4] = RV_REG_A3,
22 [BPF_REG_5] = RV_REG_A4,
23 [BPF_REG_6] = RV_REG_S1,
24 [BPF_REG_7] = RV_REG_S2,
25 [BPF_REG_8] = RV_REG_S3,
26 [BPF_REG_9] = RV_REG_S4,
27 [BPF_REG_FP] = RV_REG_S5,
28 [BPF_REG_AX] = RV_REG_T0,
29};
30
Tong Tiangen252c7652021-10-27 11:18:22 +000031static const int pt_regmap[] = {
32 [RV_REG_A0] = offsetof(struct pt_regs, a0),
33 [RV_REG_A1] = offsetof(struct pt_regs, a1),
34 [RV_REG_A2] = offsetof(struct pt_regs, a2),
35 [RV_REG_A3] = offsetof(struct pt_regs, a3),
36 [RV_REG_A4] = offsetof(struct pt_regs, a4),
37 [RV_REG_A5] = offsetof(struct pt_regs, a5),
38 [RV_REG_S1] = offsetof(struct pt_regs, s1),
39 [RV_REG_S2] = offsetof(struct pt_regs, s2),
40 [RV_REG_S3] = offsetof(struct pt_regs, s3),
41 [RV_REG_S4] = offsetof(struct pt_regs, s4),
42 [RV_REG_S5] = offsetof(struct pt_regs, s5),
43 [RV_REG_T0] = offsetof(struct pt_regs, t0),
44};
45
Björn Töpel2353ecc2019-02-05 13:41:22 +010046enum {
47 RV_CTX_F_SEEN_TAIL_CALL = 0,
48 RV_CTX_F_SEEN_CALL = RV_REG_RA,
49 RV_CTX_F_SEEN_S1 = RV_REG_S1,
50 RV_CTX_F_SEEN_S2 = RV_REG_S2,
51 RV_CTX_F_SEEN_S3 = RV_REG_S3,
52 RV_CTX_F_SEEN_S4 = RV_REG_S4,
53 RV_CTX_F_SEEN_S5 = RV_REG_S5,
54 RV_CTX_F_SEEN_S6 = RV_REG_S6,
55};
56
Björn Töpel2353ecc2019-02-05 13:41:22 +010057static u8 bpf_to_rv_reg(int bpf_reg, struct rv_jit_context *ctx)
58{
59 u8 reg = regmap[bpf_reg];
60
61 switch (reg) {
62 case RV_CTX_F_SEEN_S1:
63 case RV_CTX_F_SEEN_S2:
64 case RV_CTX_F_SEEN_S3:
65 case RV_CTX_F_SEEN_S4:
66 case RV_CTX_F_SEEN_S5:
67 case RV_CTX_F_SEEN_S6:
68 __set_bit(reg, &ctx->flags);
69 }
70 return reg;
71};
72
73static bool seen_reg(int reg, struct rv_jit_context *ctx)
74{
75 switch (reg) {
76 case RV_CTX_F_SEEN_CALL:
77 case RV_CTX_F_SEEN_S1:
78 case RV_CTX_F_SEEN_S2:
79 case RV_CTX_F_SEEN_S3:
80 case RV_CTX_F_SEEN_S4:
81 case RV_CTX_F_SEEN_S5:
82 case RV_CTX_F_SEEN_S6:
83 return test_bit(reg, &ctx->flags);
84 }
85 return false;
86}
87
Björn Töpelf1003b72019-12-16 10:13:35 +010088static void mark_fp(struct rv_jit_context *ctx)
89{
90 __set_bit(RV_CTX_F_SEEN_S5, &ctx->flags);
91}
92
Björn Töpel2353ecc2019-02-05 13:41:22 +010093static void mark_call(struct rv_jit_context *ctx)
94{
95 __set_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
96}
97
98static bool seen_call(struct rv_jit_context *ctx)
99{
100 return test_bit(RV_CTX_F_SEEN_CALL, &ctx->flags);
101}
102
103static void mark_tail_call(struct rv_jit_context *ctx)
104{
105 __set_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
106}
107
108static bool seen_tail_call(struct rv_jit_context *ctx)
109{
110 return test_bit(RV_CTX_F_SEEN_TAIL_CALL, &ctx->flags);
111}
112
113static u8 rv_tail_call_reg(struct rv_jit_context *ctx)
114{
115 mark_tail_call(ctx);
116
117 if (seen_call(ctx)) {
118 __set_bit(RV_CTX_F_SEEN_S6, &ctx->flags);
119 return RV_REG_S6;
120 }
121 return RV_REG_A6;
122}
123
Björn Töpel2353ecc2019-02-05 13:41:22 +0100124static bool is_32b_int(s64 val)
125{
126 return -(1L << 31) <= val && val < (1L << 31);
127}
128
Luke Nelson489553d2020-04-06 22:16:04 +0000129static bool in_auipc_jalr_range(s64 val)
130{
131 /*
132 * auipc+jalr can reach any signed PC-relative offset in the range
133 * [-2^31 - 2^11, 2^31 - 2^11).
134 */
135 return (-(1L << 31) - (1L << 11)) <= val &&
136 val < ((1L << 31) - (1L << 11));
137}
138
Björn Töpel2353ecc2019-02-05 13:41:22 +0100139static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)
140{
141 /* Note that the immediate from the add is sign-extended,
142 * which means that we need to compensate this by adding 2^12,
143 * when the 12th bit is set. A simpler way of doing this, and
144 * getting rid of the check, is to just add 2**11 before the
145 * shift. The "Loading a 32-Bit constant" example from the
146 * "Computer Organization and Design, RISC-V edition" book by
147 * Patterson/Hennessy highlights this fact.
148 *
149 * This also means that we need to process LSB to MSB.
150 */
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700151 s64 upper = (val + (1 << 11)) >> 12;
152 /* Sign-extend lower 12 bits to 64 bits since immediates for li, addiw,
153 * and addi are signed and RVC checks will perform signed comparisons.
154 */
155 s64 lower = ((val & 0xfff) << 52) >> 52;
Björn Töpel2353ecc2019-02-05 13:41:22 +0100156 int shift;
157
158 if (is_32b_int(val)) {
159 if (upper)
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700160 emit_lui(rd, upper, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100161
162 if (!upper) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700163 emit_li(rd, lower, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100164 return;
165 }
166
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700167 emit_addiw(rd, rd, lower, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100168 return;
169 }
170
171 shift = __ffs(upper);
172 upper >>= shift;
173 shift += 12;
174
175 emit_imm(rd, upper, ctx);
176
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700177 emit_slli(rd, rd, shift, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100178 if (lower)
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700179 emit_addi(rd, rd, lower, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100180}
181
Björn Töpelfe8322b2019-12-16 10:13:39 +0100182static void __build_epilogue(bool is_tail_call, struct rv_jit_context *ctx)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100183{
184 int stack_adjust = ctx->stack_size, store_offset = stack_adjust - 8;
185
186 if (seen_reg(RV_REG_RA, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700187 emit_ld(RV_REG_RA, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100188 store_offset -= 8;
189 }
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700190 emit_ld(RV_REG_FP, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100191 store_offset -= 8;
192 if (seen_reg(RV_REG_S1, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700193 emit_ld(RV_REG_S1, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100194 store_offset -= 8;
195 }
196 if (seen_reg(RV_REG_S2, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700197 emit_ld(RV_REG_S2, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100198 store_offset -= 8;
199 }
200 if (seen_reg(RV_REG_S3, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700201 emit_ld(RV_REG_S3, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100202 store_offset -= 8;
203 }
204 if (seen_reg(RV_REG_S4, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700205 emit_ld(RV_REG_S4, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100206 store_offset -= 8;
207 }
208 if (seen_reg(RV_REG_S5, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700209 emit_ld(RV_REG_S5, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100210 store_offset -= 8;
211 }
212 if (seen_reg(RV_REG_S6, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700213 emit_ld(RV_REG_S6, store_offset, RV_REG_SP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100214 store_offset -= 8;
215 }
216
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700217 emit_addi(RV_REG_SP, RV_REG_SP, stack_adjust, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100218 /* Set return value. */
Björn Töpelfe8322b2019-12-16 10:13:39 +0100219 if (!is_tail_call)
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700220 emit_mv(RV_REG_A0, RV_REG_A5, ctx);
221 emit_jalr(RV_REG_ZERO, is_tail_call ? RV_REG_T3 : RV_REG_RA,
222 is_tail_call ? 4 : 0, /* skip TCC init */
223 ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100224}
225
Björn Töpel7d1ef132019-12-16 10:13:36 +0100226static void emit_bcc(u8 cond, u8 rd, u8 rs, int rvoff,
227 struct rv_jit_context *ctx)
228{
229 switch (cond) {
230 case BPF_JEQ:
231 emit(rv_beq(rd, rs, rvoff >> 1), ctx);
232 return;
233 case BPF_JGT:
234 emit(rv_bltu(rs, rd, rvoff >> 1), ctx);
235 return;
236 case BPF_JLT:
237 emit(rv_bltu(rd, rs, rvoff >> 1), ctx);
238 return;
239 case BPF_JGE:
240 emit(rv_bgeu(rd, rs, rvoff >> 1), ctx);
241 return;
242 case BPF_JLE:
243 emit(rv_bgeu(rs, rd, rvoff >> 1), ctx);
244 return;
245 case BPF_JNE:
246 emit(rv_bne(rd, rs, rvoff >> 1), ctx);
247 return;
248 case BPF_JSGT:
249 emit(rv_blt(rs, rd, rvoff >> 1), ctx);
250 return;
251 case BPF_JSLT:
252 emit(rv_blt(rd, rs, rvoff >> 1), ctx);
253 return;
254 case BPF_JSGE:
255 emit(rv_bge(rd, rs, rvoff >> 1), ctx);
256 return;
257 case BPF_JSLE:
258 emit(rv_bge(rs, rd, rvoff >> 1), ctx);
259 }
260}
261
262static void emit_branch(u8 cond, u8 rd, u8 rs, int rvoff,
263 struct rv_jit_context *ctx)
264{
265 s64 upper, lower;
266
267 if (is_13b_int(rvoff)) {
268 emit_bcc(cond, rd, rs, rvoff, ctx);
269 return;
270 }
271
272 /* Adjust for jal */
273 rvoff -= 4;
274
275 /* Transform, e.g.:
276 * bne rd,rs,foo
277 * to
278 * beq rd,rs,<.L1>
279 * (auipc foo)
280 * jal(r) foo
281 * .L1
282 */
283 cond = invert_bpf_cond(cond);
284 if (is_21b_int(rvoff)) {
285 emit_bcc(cond, rd, rs, 8, ctx);
286 emit(rv_jal(RV_REG_ZERO, rvoff >> 1), ctx);
287 return;
288 }
289
290 /* 32b No need for an additional rvoff adjustment, since we
291 * get that from the auipc at PC', where PC = PC' + 4.
292 */
293 upper = (rvoff + (1 << 11)) >> 12;
294 lower = rvoff & 0xfff;
295
296 emit_bcc(cond, rd, rs, 12, ctx);
297 emit(rv_auipc(RV_REG_T1, upper), ctx);
298 emit(rv_jalr(RV_REG_ZERO, RV_REG_T1, lower), ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100299}
300
301static void emit_zext_32(u8 reg, struct rv_jit_context *ctx)
302{
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700303 emit_slli(reg, reg, 32, ctx);
304 emit_srli(reg, reg, 32, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100305}
306
307static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
308{
309 int tc_ninsn, off, start_insn = ctx->ninsns;
310 u8 tcc = rv_tail_call_reg(ctx);
311
312 /* a0: &ctx
313 * a1: &array
314 * a2: index
315 *
316 * if (index >= array->map.max_entries)
317 * goto out;
318 */
319 tc_ninsn = insn ? ctx->offset[insn] - ctx->offset[insn - 1] :
320 ctx->offset[0];
321 emit_zext_32(RV_REG_A2, ctx);
322
323 off = offsetof(struct bpf_array, map.max_entries);
324 if (is_12b_check(off, insn))
325 return -1;
326 emit(rv_lwu(RV_REG_T1, off, RV_REG_A1), ctx);
Luke Nelsonbfabff32020-07-20 19:52:38 -0700327 off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
Björn Töpel29d92ed2019-12-16 10:13:37 +0100328 emit_branch(BPF_JGE, RV_REG_A2, RV_REG_T1, off, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100329
Tiezhu Yangebf7f6f2021-11-05 09:30:00 +0800330 /* if (--TCC < 0)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100331 * goto out;
332 */
Tiezhu Yangebf7f6f2021-11-05 09:30:00 +0800333 emit_addi(RV_REG_TCC, tcc, -1, ctx);
Luke Nelsonbfabff32020-07-20 19:52:38 -0700334 off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
Tiezhu Yangebf7f6f2021-11-05 09:30:00 +0800335 emit_branch(BPF_JSLT, RV_REG_TCC, RV_REG_ZERO, off, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100336
337 /* prog = array->ptrs[index];
338 * if (!prog)
339 * goto out;
340 */
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700341 emit_slli(RV_REG_T2, RV_REG_A2, 3, ctx);
342 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_A1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100343 off = offsetof(struct bpf_array, ptrs);
344 if (is_12b_check(off, insn))
345 return -1;
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700346 emit_ld(RV_REG_T2, off, RV_REG_T2, ctx);
Luke Nelsonbfabff32020-07-20 19:52:38 -0700347 off = ninsns_rvoff(tc_ninsn - (ctx->ninsns - start_insn));
Björn Töpel29d92ed2019-12-16 10:13:37 +0100348 emit_branch(BPF_JEQ, RV_REG_T2, RV_REG_ZERO, off, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100349
350 /* goto *(prog->bpf_func + 4); */
351 off = offsetof(struct bpf_prog, bpf_func);
352 if (is_12b_check(off, insn))
353 return -1;
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700354 emit_ld(RV_REG_T3, off, RV_REG_T2, ctx);
Björn Töpelfe8322b2019-12-16 10:13:39 +0100355 __build_epilogue(true, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100356 return 0;
357}
358
359static void init_regs(u8 *rd, u8 *rs, const struct bpf_insn *insn,
360 struct rv_jit_context *ctx)
361{
362 u8 code = insn->code;
363
364 switch (code) {
365 case BPF_JMP | BPF_JA:
366 case BPF_JMP | BPF_CALL:
367 case BPF_JMP | BPF_EXIT:
368 case BPF_JMP | BPF_TAIL_CALL:
369 break;
370 default:
371 *rd = bpf_to_rv_reg(insn->dst_reg, ctx);
372 }
373
374 if (code & (BPF_ALU | BPF_X) || code & (BPF_ALU64 | BPF_X) ||
375 code & (BPF_JMP | BPF_X) || code & (BPF_JMP32 | BPF_X) ||
376 code & BPF_LDX || code & BPF_STX)
377 *rs = bpf_to_rv_reg(insn->src_reg, ctx);
378}
379
Björn Töpel2353ecc2019-02-05 13:41:22 +0100380static void emit_zext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
381{
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700382 emit_mv(RV_REG_T2, *rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100383 emit_zext_32(RV_REG_T2, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700384 emit_mv(RV_REG_T1, *rs, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100385 emit_zext_32(RV_REG_T1, ctx);
386 *rd = RV_REG_T2;
387 *rs = RV_REG_T1;
388}
389
390static void emit_sext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
391{
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700392 emit_addiw(RV_REG_T2, *rd, 0, ctx);
393 emit_addiw(RV_REG_T1, *rs, 0, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100394 *rd = RV_REG_T2;
395 *rs = RV_REG_T1;
396}
397
398static void emit_zext_32_rd_t1(u8 *rd, struct rv_jit_context *ctx)
399{
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700400 emit_mv(RV_REG_T2, *rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100401 emit_zext_32(RV_REG_T2, ctx);
402 emit_zext_32(RV_REG_T1, ctx);
403 *rd = RV_REG_T2;
404}
405
406static void emit_sext_32_rd(u8 *rd, struct rv_jit_context *ctx)
407{
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700408 emit_addiw(RV_REG_T2, *rd, 0, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100409 *rd = RV_REG_T2;
410}
411
Luke Nelson489553d2020-04-06 22:16:04 +0000412static int emit_jump_and_link(u8 rd, s64 rvoff, bool force_jalr,
413 struct rv_jit_context *ctx)
Björn Töpel33203c02019-12-16 10:13:38 +0100414{
415 s64 upper, lower;
416
Björn Töpele368b642019-12-16 10:13:41 +0100417 if (rvoff && is_21b_int(rvoff) && !force_jalr) {
Björn Töpel33203c02019-12-16 10:13:38 +0100418 emit(rv_jal(rd, rvoff >> 1), ctx);
Luke Nelson489553d2020-04-06 22:16:04 +0000419 return 0;
420 } else if (in_auipc_jalr_range(rvoff)) {
421 upper = (rvoff + (1 << 11)) >> 12;
422 lower = rvoff & 0xfff;
423 emit(rv_auipc(RV_REG_T1, upper), ctx);
424 emit(rv_jalr(rd, RV_REG_T1, lower), ctx);
425 return 0;
Björn Töpel33203c02019-12-16 10:13:38 +0100426 }
427
Luke Nelson489553d2020-04-06 22:16:04 +0000428 pr_err("bpf-jit: target offset 0x%llx is out of range\n", rvoff);
429 return -ERANGE;
Björn Töpel33203c02019-12-16 10:13:38 +0100430}
431
Björn Töpel7d1ef132019-12-16 10:13:36 +0100432static bool is_signed_bpf_cond(u8 cond)
433{
434 return cond == BPF_JSGT || cond == BPF_JSLT ||
435 cond == BPF_JSGE || cond == BPF_JSLE;
436}
437
Björn Töpele368b642019-12-16 10:13:41 +0100438static int emit_call(bool fixed, u64 addr, struct rv_jit_context *ctx)
439{
440 s64 off = 0;
441 u64 ip;
442 u8 rd;
Luke Nelson489553d2020-04-06 22:16:04 +0000443 int ret;
Björn Töpele368b642019-12-16 10:13:41 +0100444
445 if (addr && ctx->insns) {
446 ip = (u64)(long)(ctx->insns + ctx->ninsns);
447 off = addr - ip;
Björn Töpele368b642019-12-16 10:13:41 +0100448 }
449
Luke Nelson489553d2020-04-06 22:16:04 +0000450 ret = emit_jump_and_link(RV_REG_RA, off, !fixed, ctx);
451 if (ret)
452 return ret;
Björn Töpele368b642019-12-16 10:13:41 +0100453 rd = bpf_to_rv_reg(BPF_REG_0, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700454 emit_mv(rd, RV_REG_A0, ctx);
Björn Töpele368b642019-12-16 10:13:41 +0100455 return 0;
456}
457
Tong Tiangen252c7652021-10-27 11:18:22 +0000458#define BPF_FIXUP_OFFSET_MASK GENMASK(26, 0)
459#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
460
461int rv_bpf_fixup_exception(const struct exception_table_entry *ex,
Björn Töpelf47d4ff2021-11-03 12:54:53 +0100462 struct pt_regs *regs);
463int rv_bpf_fixup_exception(const struct exception_table_entry *ex,
Tong Tiangen252c7652021-10-27 11:18:22 +0000464 struct pt_regs *regs)
465{
466 off_t offset = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
467 int regs_offset = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
468
469 *(unsigned long *)((void *)regs + pt_regmap[regs_offset]) = 0;
470 regs->epc = (unsigned long)&ex->fixup - offset;
471
472 return 1;
473}
474
475/* For accesses to BTF pointers, add an entry to the exception table */
476static int add_exception_handler(const struct bpf_insn *insn,
477 struct rv_jit_context *ctx,
478 int dst_reg, int insn_len)
479{
480 struct exception_table_entry *ex;
481 unsigned long pc;
482 off_t offset;
483
484 if (!ctx->insns || !ctx->prog->aux->extable || BPF_MODE(insn->code) != BPF_PROBE_MEM)
485 return 0;
486
487 if (WARN_ON_ONCE(ctx->nexentries >= ctx->prog->aux->num_exentries))
488 return -EINVAL;
489
490 if (WARN_ON_ONCE(insn_len > ctx->ninsns))
491 return -EINVAL;
492
493 if (WARN_ON_ONCE(!rvc_enabled() && insn_len == 1))
494 return -EINVAL;
495
496 ex = &ctx->prog->aux->extable[ctx->nexentries];
497 pc = (unsigned long)&ctx->insns[ctx->ninsns - insn_len];
498
499 offset = pc - (long)&ex->insn;
500 if (WARN_ON_ONCE(offset >= 0 || offset < INT_MIN))
501 return -ERANGE;
502 ex->insn = pc;
503
504 /*
505 * Since the extable follows the program, the fixup offset is always
506 * negative and limited to BPF_JIT_REGION_SIZE. Store a positive value
507 * to keep things simple, and put the destination register in the upper
508 * bits. We don't need to worry about buildtime or runtime sort
509 * modifying the upper bits because the table is already sorted, and
510 * isn't part of the main exception table.
511 */
512 offset = (long)&ex->fixup - (pc + insn_len * sizeof(u16));
513 if (!FIELD_FIT(BPF_FIXUP_OFFSET_MASK, offset))
514 return -ERANGE;
515
516 ex->fixup = FIELD_PREP(BPF_FIXUP_OFFSET_MASK, offset) |
517 FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
518
519 ctx->nexentries++;
520 return 0;
521}
522
Luke Nelsonca6cb542020-03-04 21:02:04 -0800523int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
524 bool extra_pass)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100525{
526 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 ||
527 BPF_CLASS(insn->code) == BPF_JMP;
Luke Nelson489553d2020-04-06 22:16:04 +0000528 int s, e, rvoff, ret, i = insn - ctx->prog->insnsi;
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100529 struct bpf_prog_aux *aux = ctx->prog->aux;
Björn Töpel2353ecc2019-02-05 13:41:22 +0100530 u8 rd = -1, rs = -1, code = insn->code;
531 s16 off = insn->off;
532 s32 imm = insn->imm;
533
534 init_regs(&rd, &rs, insn, ctx);
535
536 switch (code) {
537 /* dst = src */
538 case BPF_ALU | BPF_MOV | BPF_X:
539 case BPF_ALU64 | BPF_MOV | BPF_X:
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100540 if (imm == 1) {
541 /* Special mov32 for zext */
542 emit_zext_32(rd, ctx);
543 break;
544 }
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700545 emit_mv(rd, rs, ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100546 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100547 emit_zext_32(rd, ctx);
548 break;
549
550 /* dst = dst OP src */
551 case BPF_ALU | BPF_ADD | BPF_X:
552 case BPF_ALU64 | BPF_ADD | BPF_X:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700553 emit_add(rd, rd, rs, ctx);
Luke Nelson46dd3d72019-07-04 17:18:02 -0700554 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700555 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100556 break;
557 case BPF_ALU | BPF_SUB | BPF_X:
558 case BPF_ALU64 | BPF_SUB | BPF_X:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700559 if (is64)
560 emit_sub(rd, rd, rs, ctx);
561 else
562 emit_subw(rd, rd, rs, ctx);
563
Luke Nelson46dd3d72019-07-04 17:18:02 -0700564 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700565 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100566 break;
567 case BPF_ALU | BPF_AND | BPF_X:
568 case BPF_ALU64 | BPF_AND | BPF_X:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700569 emit_and(rd, rd, rs, ctx);
Luke Nelson46dd3d72019-07-04 17:18:02 -0700570 if (!is64 && !aux->verifier_zext)
Björn Töpelfe121ee2019-05-21 15:46:22 +0200571 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100572 break;
573 case BPF_ALU | BPF_OR | BPF_X:
574 case BPF_ALU64 | BPF_OR | BPF_X:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700575 emit_or(rd, rd, rs, ctx);
Luke Nelson46dd3d72019-07-04 17:18:02 -0700576 if (!is64 && !aux->verifier_zext)
Björn Töpelfe121ee2019-05-21 15:46:22 +0200577 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100578 break;
579 case BPF_ALU | BPF_XOR | BPF_X:
580 case BPF_ALU64 | BPF_XOR | BPF_X:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700581 emit_xor(rd, rd, rs, ctx);
Luke Nelson46dd3d72019-07-04 17:18:02 -0700582 if (!is64 && !aux->verifier_zext)
Björn Töpelfe121ee2019-05-21 15:46:22 +0200583 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100584 break;
585 case BPF_ALU | BPF_MUL | BPF_X:
586 case BPF_ALU64 | BPF_MUL | BPF_X:
587 emit(is64 ? rv_mul(rd, rd, rs) : rv_mulw(rd, rd, rs), ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100588 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100589 emit_zext_32(rd, ctx);
590 break;
591 case BPF_ALU | BPF_DIV | BPF_X:
592 case BPF_ALU64 | BPF_DIV | BPF_X:
593 emit(is64 ? rv_divu(rd, rd, rs) : rv_divuw(rd, rd, rs), ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100594 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100595 emit_zext_32(rd, ctx);
596 break;
597 case BPF_ALU | BPF_MOD | BPF_X:
598 case BPF_ALU64 | BPF_MOD | BPF_X:
599 emit(is64 ? rv_remu(rd, rd, rs) : rv_remuw(rd, rd, rs), ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100600 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100601 emit_zext_32(rd, ctx);
602 break;
603 case BPF_ALU | BPF_LSH | BPF_X:
604 case BPF_ALU64 | BPF_LSH | BPF_X:
605 emit(is64 ? rv_sll(rd, rd, rs) : rv_sllw(rd, rd, rs), ctx);
Luke Nelson0224b2a2020-05-05 17:03:17 -0700606 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700607 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100608 break;
609 case BPF_ALU | BPF_RSH | BPF_X:
610 case BPF_ALU64 | BPF_RSH | BPF_X:
611 emit(is64 ? rv_srl(rd, rd, rs) : rv_srlw(rd, rd, rs), ctx);
Luke Nelson46dd3d72019-07-04 17:18:02 -0700612 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700613 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100614 break;
615 case BPF_ALU | BPF_ARSH | BPF_X:
616 case BPF_ALU64 | BPF_ARSH | BPF_X:
617 emit(is64 ? rv_sra(rd, rd, rs) : rv_sraw(rd, rd, rs), ctx);
Luke Nelson46dd3d72019-07-04 17:18:02 -0700618 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700619 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100620 break;
621
622 /* dst = -dst */
623 case BPF_ALU | BPF_NEG:
624 case BPF_ALU64 | BPF_NEG:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700625 emit_sub(rd, RV_REG_ZERO, rd, ctx);
Luke Nelson46dd3d72019-07-04 17:18:02 -0700626 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700627 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100628 break;
629
630 /* dst = BSWAP##imm(dst) */
631 case BPF_ALU | BPF_END | BPF_FROM_LE:
Luke Nelson21a099a2020-05-05 17:03:18 -0700632 switch (imm) {
633 case 16:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700634 emit_slli(rd, rd, 48, ctx);
635 emit_srli(rd, rd, 48, ctx);
Luke Nelson21a099a2020-05-05 17:03:18 -0700636 break;
637 case 32:
638 if (!aux->verifier_zext)
639 emit_zext_32(rd, ctx);
640 break;
641 case 64:
642 /* Do nothing */
643 break;
644 }
Björn Töpel2353ecc2019-02-05 13:41:22 +0100645 break;
Luke Nelson21a099a2020-05-05 17:03:18 -0700646
Björn Töpel2353ecc2019-02-05 13:41:22 +0100647 case BPF_ALU | BPF_END | BPF_FROM_BE:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700648 emit_li(RV_REG_T2, 0, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100649
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700650 emit_andi(RV_REG_T1, rd, 0xff, ctx);
651 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
652 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
653 emit_srli(rd, rd, 8, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100654 if (imm == 16)
655 goto out_be;
656
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700657 emit_andi(RV_REG_T1, rd, 0xff, ctx);
658 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
659 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
660 emit_srli(rd, rd, 8, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100661
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700662 emit_andi(RV_REG_T1, rd, 0xff, ctx);
663 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
664 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
665 emit_srli(rd, rd, 8, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100666 if (imm == 32)
667 goto out_be;
668
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700669 emit_andi(RV_REG_T1, rd, 0xff, ctx);
670 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
671 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
672 emit_srli(rd, rd, 8, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100673
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700674 emit_andi(RV_REG_T1, rd, 0xff, ctx);
675 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
676 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
677 emit_srli(rd, rd, 8, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100678
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700679 emit_andi(RV_REG_T1, rd, 0xff, ctx);
680 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
681 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
682 emit_srli(rd, rd, 8, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100683
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700684 emit_andi(RV_REG_T1, rd, 0xff, ctx);
685 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
686 emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
687 emit_srli(rd, rd, 8, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100688out_be:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700689 emit_andi(RV_REG_T1, rd, 0xff, ctx);
690 emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100691
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700692 emit_mv(rd, RV_REG_T2, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100693 break;
694
695 /* dst = imm */
696 case BPF_ALU | BPF_MOV | BPF_K:
697 case BPF_ALU64 | BPF_MOV | BPF_K:
698 emit_imm(rd, imm, ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100699 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100700 emit_zext_32(rd, ctx);
701 break;
702
703 /* dst = dst OP imm */
704 case BPF_ALU | BPF_ADD | BPF_K:
705 case BPF_ALU64 | BPF_ADD | BPF_K:
706 if (is_12b_int(imm)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700707 emit_addi(rd, rd, imm, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100708 } else {
709 emit_imm(RV_REG_T1, imm, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700710 emit_add(rd, rd, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100711 }
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100712 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100713 emit_zext_32(rd, ctx);
714 break;
715 case BPF_ALU | BPF_SUB | BPF_K:
716 case BPF_ALU64 | BPF_SUB | BPF_K:
717 if (is_12b_int(-imm)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700718 emit_addi(rd, rd, -imm, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100719 } else {
720 emit_imm(RV_REG_T1, imm, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700721 emit_sub(rd, rd, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100722 }
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100723 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100724 emit_zext_32(rd, ctx);
725 break;
726 case BPF_ALU | BPF_AND | BPF_K:
727 case BPF_ALU64 | BPF_AND | BPF_K:
728 if (is_12b_int(imm)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700729 emit_andi(rd, rd, imm, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100730 } else {
731 emit_imm(RV_REG_T1, imm, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700732 emit_and(rd, rd, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100733 }
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100734 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100735 emit_zext_32(rd, ctx);
736 break;
737 case BPF_ALU | BPF_OR | BPF_K:
738 case BPF_ALU64 | BPF_OR | BPF_K:
739 if (is_12b_int(imm)) {
740 emit(rv_ori(rd, rd, imm), ctx);
741 } else {
742 emit_imm(RV_REG_T1, imm, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700743 emit_or(rd, rd, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100744 }
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100745 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100746 emit_zext_32(rd, ctx);
747 break;
748 case BPF_ALU | BPF_XOR | BPF_K:
749 case BPF_ALU64 | BPF_XOR | BPF_K:
750 if (is_12b_int(imm)) {
751 emit(rv_xori(rd, rd, imm), ctx);
752 } else {
753 emit_imm(RV_REG_T1, imm, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700754 emit_xor(rd, rd, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100755 }
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100756 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100757 emit_zext_32(rd, ctx);
758 break;
759 case BPF_ALU | BPF_MUL | BPF_K:
760 case BPF_ALU64 | BPF_MUL | BPF_K:
761 emit_imm(RV_REG_T1, imm, ctx);
762 emit(is64 ? rv_mul(rd, rd, RV_REG_T1) :
763 rv_mulw(rd, rd, RV_REG_T1), ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100764 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100765 emit_zext_32(rd, ctx);
766 break;
767 case BPF_ALU | BPF_DIV | BPF_K:
768 case BPF_ALU64 | BPF_DIV | BPF_K:
769 emit_imm(RV_REG_T1, imm, ctx);
770 emit(is64 ? rv_divu(rd, rd, RV_REG_T1) :
771 rv_divuw(rd, rd, RV_REG_T1), ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100772 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100773 emit_zext_32(rd, ctx);
774 break;
775 case BPF_ALU | BPF_MOD | BPF_K:
776 case BPF_ALU64 | BPF_MOD | BPF_K:
777 emit_imm(RV_REG_T1, imm, ctx);
778 emit(is64 ? rv_remu(rd, rd, RV_REG_T1) :
779 rv_remuw(rd, rd, RV_REG_T1), ctx);
Jiong Wang66d0d5a2019-05-24 23:25:27 +0100780 if (!is64 && !aux->verifier_zext)
Björn Töpel2353ecc2019-02-05 13:41:22 +0100781 emit_zext_32(rd, ctx);
782 break;
783 case BPF_ALU | BPF_LSH | BPF_K:
784 case BPF_ALU64 | BPF_LSH | BPF_K:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700785 emit_slli(rd, rd, imm, ctx);
786
Luke Nelson0224b2a2020-05-05 17:03:17 -0700787 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700788 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100789 break;
790 case BPF_ALU | BPF_RSH | BPF_K:
791 case BPF_ALU64 | BPF_RSH | BPF_K:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700792 if (is64)
793 emit_srli(rd, rd, imm, ctx);
794 else
795 emit(rv_srliw(rd, rd, imm), ctx);
796
Luke Nelson0224b2a2020-05-05 17:03:17 -0700797 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700798 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100799 break;
800 case BPF_ALU | BPF_ARSH | BPF_K:
801 case BPF_ALU64 | BPF_ARSH | BPF_K:
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700802 if (is64)
803 emit_srai(rd, rd, imm, ctx);
804 else
805 emit(rv_sraiw(rd, rd, imm), ctx);
806
Luke Nelson0224b2a2020-05-05 17:03:17 -0700807 if (!is64 && !aux->verifier_zext)
Luke Nelson1e692f02019-05-30 15:29:22 -0700808 emit_zext_32(rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100809 break;
810
811 /* JUMP off */
812 case BPF_JMP | BPF_JA:
Björn Töpel7d1ef132019-12-16 10:13:36 +0100813 rvoff = rv_offset(i, off, ctx);
Luke Nelson489553d2020-04-06 22:16:04 +0000814 ret = emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
815 if (ret)
816 return ret;
Björn Töpel2353ecc2019-02-05 13:41:22 +0100817 break;
818
819 /* IF (dst COND src) JUMP off */
820 case BPF_JMP | BPF_JEQ | BPF_X:
821 case BPF_JMP32 | BPF_JEQ | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100822 case BPF_JMP | BPF_JGT | BPF_X:
823 case BPF_JMP32 | BPF_JGT | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100824 case BPF_JMP | BPF_JLT | BPF_X:
825 case BPF_JMP32 | BPF_JLT | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100826 case BPF_JMP | BPF_JGE | BPF_X:
827 case BPF_JMP32 | BPF_JGE | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100828 case BPF_JMP | BPF_JLE | BPF_X:
829 case BPF_JMP32 | BPF_JLE | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100830 case BPF_JMP | BPF_JNE | BPF_X:
831 case BPF_JMP32 | BPF_JNE | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100832 case BPF_JMP | BPF_JSGT | BPF_X:
833 case BPF_JMP32 | BPF_JSGT | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100834 case BPF_JMP | BPF_JSLT | BPF_X:
835 case BPF_JMP32 | BPF_JSLT | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100836 case BPF_JMP | BPF_JSGE | BPF_X:
837 case BPF_JMP32 | BPF_JSGE | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100838 case BPF_JMP | BPF_JSLE | BPF_X:
839 case BPF_JMP32 | BPF_JSLE | BPF_X:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100840 case BPF_JMP | BPF_JSET | BPF_X:
841 case BPF_JMP32 | BPF_JSET | BPF_X:
Björn Töpel7d1ef132019-12-16 10:13:36 +0100842 rvoff = rv_offset(i, off, ctx);
843 if (!is64) {
844 s = ctx->ninsns;
845 if (is_signed_bpf_cond(BPF_OP(code)))
846 emit_sext_32_rd_rs(&rd, &rs, ctx);
847 else
848 emit_zext_32_rd_rs(&rd, &rs, ctx);
849 e = ctx->ninsns;
850
851 /* Adjust for extra insns */
Luke Nelsonbfabff32020-07-20 19:52:38 -0700852 rvoff -= ninsns_rvoff(e - s);
Björn Töpel7d1ef132019-12-16 10:13:36 +0100853 }
854
855 if (BPF_OP(code) == BPF_JSET) {
856 /* Adjust for and */
857 rvoff -= 4;
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700858 emit_and(RV_REG_T1, rd, rs, ctx);
Björn Töpel7d1ef132019-12-16 10:13:36 +0100859 emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff,
860 ctx);
861 } else {
862 emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
863 }
Björn Töpel2353ecc2019-02-05 13:41:22 +0100864 break;
865
866 /* IF (dst COND imm) JUMP off */
867 case BPF_JMP | BPF_JEQ | BPF_K:
868 case BPF_JMP32 | BPF_JEQ | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100869 case BPF_JMP | BPF_JGT | BPF_K:
870 case BPF_JMP32 | BPF_JGT | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100871 case BPF_JMP | BPF_JLT | BPF_K:
872 case BPF_JMP32 | BPF_JLT | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100873 case BPF_JMP | BPF_JGE | BPF_K:
874 case BPF_JMP32 | BPF_JGE | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100875 case BPF_JMP | BPF_JLE | BPF_K:
876 case BPF_JMP32 | BPF_JLE | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100877 case BPF_JMP | BPF_JNE | BPF_K:
878 case BPF_JMP32 | BPF_JNE | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100879 case BPF_JMP | BPF_JSGT | BPF_K:
880 case BPF_JMP32 | BPF_JSGT | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100881 case BPF_JMP | BPF_JSLT | BPF_K:
882 case BPF_JMP32 | BPF_JSLT | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100883 case BPF_JMP | BPF_JSGE | BPF_K:
884 case BPF_JMP32 | BPF_JSGE | BPF_K:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100885 case BPF_JMP | BPF_JSLE | BPF_K:
886 case BPF_JMP32 | BPF_JSLE | BPF_K:
Björn Töpel7d1ef132019-12-16 10:13:36 +0100887 rvoff = rv_offset(i, off, ctx);
888 s = ctx->ninsns;
Luke Nelsonca349a62020-05-05 17:03:19 -0700889 if (imm) {
890 emit_imm(RV_REG_T1, imm, ctx);
891 rs = RV_REG_T1;
892 } else {
893 /* If imm is 0, simply use zero register. */
894 rs = RV_REG_ZERO;
895 }
Björn Töpel7d1ef132019-12-16 10:13:36 +0100896 if (!is64) {
897 if (is_signed_bpf_cond(BPF_OP(code)))
898 emit_sext_32_rd(&rd, ctx);
899 else
900 emit_zext_32_rd_t1(&rd, ctx);
901 }
902 e = ctx->ninsns;
903
904 /* Adjust for extra insns */
Luke Nelsonbfabff32020-07-20 19:52:38 -0700905 rvoff -= ninsns_rvoff(e - s);
Luke Nelson073ca6a2020-05-05 17:03:20 -0700906 emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
907 break;
Björn Töpel7d1ef132019-12-16 10:13:36 +0100908
Luke Nelson073ca6a2020-05-05 17:03:20 -0700909 case BPF_JMP | BPF_JSET | BPF_K:
910 case BPF_JMP32 | BPF_JSET | BPF_K:
911 rvoff = rv_offset(i, off, ctx);
912 s = ctx->ninsns;
913 if (is_12b_int(imm)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700914 emit_andi(RV_REG_T1, rd, imm, ctx);
Björn Töpel7d1ef132019-12-16 10:13:36 +0100915 } else {
Luke Nelson073ca6a2020-05-05 17:03:20 -0700916 emit_imm(RV_REG_T1, imm, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700917 emit_and(RV_REG_T1, rd, RV_REG_T1, ctx);
Björn Töpel7d1ef132019-12-16 10:13:36 +0100918 }
Luke Nelson073ca6a2020-05-05 17:03:20 -0700919 /* For jset32, we should clear the upper 32 bits of t1, but
920 * sign-extension is sufficient here and saves one instruction,
921 * as t1 is used only in comparison against zero.
922 */
923 if (!is64 && imm < 0)
Luke Nelson18a4d8c2020-07-20 19:52:40 -0700924 emit_addiw(RV_REG_T1, RV_REG_T1, 0, ctx);
Luke Nelson073ca6a2020-05-05 17:03:20 -0700925 e = ctx->ninsns;
Luke Nelsonbfabff32020-07-20 19:52:38 -0700926 rvoff -= ninsns_rvoff(e - s);
Luke Nelson073ca6a2020-05-05 17:03:20 -0700927 emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +0100928 break;
929
930 /* function call */
931 case BPF_JMP | BPF_CALL:
932 {
933 bool fixed;
Björn Töpel2353ecc2019-02-05 13:41:22 +0100934 u64 addr;
935
936 mark_call(ctx);
937 ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &addr,
938 &fixed);
939 if (ret < 0)
940 return ret;
Björn Töpele368b642019-12-16 10:13:41 +0100941 ret = emit_call(fixed, addr, ctx);
942 if (ret)
943 return ret;
Björn Töpel2353ecc2019-02-05 13:41:22 +0100944 break;
945 }
946 /* tail call */
947 case BPF_JMP | BPF_TAIL_CALL:
948 if (emit_bpf_tail_call(i, ctx))
949 return -1;
950 break;
951
952 /* function return */
953 case BPF_JMP | BPF_EXIT:
954 if (i == ctx->prog->len - 1)
955 break;
956
957 rvoff = epilogue_offset(ctx);
Luke Nelson489553d2020-04-06 22:16:04 +0000958 ret = emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
959 if (ret)
960 return ret;
Björn Töpel2353ecc2019-02-05 13:41:22 +0100961 break;
962
963 /* dst = imm64 */
964 case BPF_LD | BPF_IMM | BPF_DW:
965 {
966 struct bpf_insn insn1 = insn[1];
967 u64 imm64;
968
969 imm64 = (u64)insn1.imm << 32 | (u32)imm;
970 emit_imm(rd, imm64, ctx);
971 return 1;
972 }
973
974 /* LDX: dst = *(size *)(src + off) */
975 case BPF_LDX | BPF_MEM | BPF_B:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100976 case BPF_LDX | BPF_MEM | BPF_H:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100977 case BPF_LDX | BPF_MEM | BPF_W:
Björn Töpel2353ecc2019-02-05 13:41:22 +0100978 case BPF_LDX | BPF_MEM | BPF_DW:
Tong Tiangen252c7652021-10-27 11:18:22 +0000979 case BPF_LDX | BPF_PROBE_MEM | BPF_B:
980 case BPF_LDX | BPF_PROBE_MEM | BPF_H:
981 case BPF_LDX | BPF_PROBE_MEM | BPF_W:
982 case BPF_LDX | BPF_PROBE_MEM | BPF_DW:
983 {
984 int insn_len, insns_start;
985
986 switch (BPF_SIZE(code)) {
987 case BPF_B:
988 if (is_12b_int(off)) {
989 insns_start = ctx->ninsns;
990 emit(rv_lbu(rd, off, rs), ctx);
991 insn_len = ctx->ninsns - insns_start;
992 break;
993 }
994
995 emit_imm(RV_REG_T1, off, ctx);
996 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
997 insns_start = ctx->ninsns;
998 emit(rv_lbu(rd, 0, RV_REG_T1), ctx);
999 insn_len = ctx->ninsns - insns_start;
1000 if (insn_is_zext(&insn[1]))
1001 return 1;
1002 break;
1003 case BPF_H:
1004 if (is_12b_int(off)) {
1005 insns_start = ctx->ninsns;
1006 emit(rv_lhu(rd, off, rs), ctx);
1007 insn_len = ctx->ninsns - insns_start;
1008 break;
1009 }
1010
1011 emit_imm(RV_REG_T1, off, ctx);
1012 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
1013 insns_start = ctx->ninsns;
1014 emit(rv_lhu(rd, 0, RV_REG_T1), ctx);
1015 insn_len = ctx->ninsns - insns_start;
1016 if (insn_is_zext(&insn[1]))
1017 return 1;
1018 break;
1019 case BPF_W:
1020 if (is_12b_int(off)) {
1021 insns_start = ctx->ninsns;
1022 emit(rv_lwu(rd, off, rs), ctx);
1023 insn_len = ctx->ninsns - insns_start;
1024 break;
1025 }
1026
1027 emit_imm(RV_REG_T1, off, ctx);
1028 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
1029 insns_start = ctx->ninsns;
1030 emit(rv_lwu(rd, 0, RV_REG_T1), ctx);
1031 insn_len = ctx->ninsns - insns_start;
1032 if (insn_is_zext(&insn[1]))
1033 return 1;
1034 break;
1035 case BPF_DW:
1036 if (is_12b_int(off)) {
1037 insns_start = ctx->ninsns;
1038 emit_ld(rd, off, rs, ctx);
1039 insn_len = ctx->ninsns - insns_start;
1040 break;
1041 }
1042
1043 emit_imm(RV_REG_T1, off, ctx);
1044 emit_add(RV_REG_T1, RV_REG_T1, rs, ctx);
1045 insns_start = ctx->ninsns;
1046 emit_ld(rd, 0, RV_REG_T1, ctx);
1047 insn_len = ctx->ninsns - insns_start;
Björn Töpel2353ecc2019-02-05 13:41:22 +01001048 break;
1049 }
1050
Tong Tiangen252c7652021-10-27 11:18:22 +00001051 ret = add_exception_handler(insn, ctx, rd, insn_len);
1052 if (ret)
1053 return ret;
Björn Töpel2353ecc2019-02-05 13:41:22 +01001054 break;
Tong Tiangen252c7652021-10-27 11:18:22 +00001055 }
Daniel Borkmannf5e81d12021-07-13 08:18:31 +00001056 /* speculation barrier */
1057 case BPF_ST | BPF_NOSPEC:
1058 break;
1059
Björn Töpel2353ecc2019-02-05 13:41:22 +01001060 /* ST: *(size *)(dst + off) = imm */
1061 case BPF_ST | BPF_MEM | BPF_B:
1062 emit_imm(RV_REG_T1, imm, ctx);
1063 if (is_12b_int(off)) {
1064 emit(rv_sb(rd, off, RV_REG_T1), ctx);
1065 break;
1066 }
1067
1068 emit_imm(RV_REG_T2, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001069 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001070 emit(rv_sb(RV_REG_T2, 0, RV_REG_T1), ctx);
1071 break;
1072
1073 case BPF_ST | BPF_MEM | BPF_H:
1074 emit_imm(RV_REG_T1, imm, ctx);
1075 if (is_12b_int(off)) {
1076 emit(rv_sh(rd, off, RV_REG_T1), ctx);
1077 break;
1078 }
1079
1080 emit_imm(RV_REG_T2, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001081 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001082 emit(rv_sh(RV_REG_T2, 0, RV_REG_T1), ctx);
1083 break;
1084 case BPF_ST | BPF_MEM | BPF_W:
1085 emit_imm(RV_REG_T1, imm, ctx);
1086 if (is_12b_int(off)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001087 emit_sw(rd, off, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001088 break;
1089 }
1090
1091 emit_imm(RV_REG_T2, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001092 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
1093 emit_sw(RV_REG_T2, 0, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001094 break;
1095 case BPF_ST | BPF_MEM | BPF_DW:
1096 emit_imm(RV_REG_T1, imm, ctx);
1097 if (is_12b_int(off)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001098 emit_sd(rd, off, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001099 break;
1100 }
1101
1102 emit_imm(RV_REG_T2, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001103 emit_add(RV_REG_T2, RV_REG_T2, rd, ctx);
1104 emit_sd(RV_REG_T2, 0, RV_REG_T1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001105 break;
1106
1107 /* STX: *(size *)(dst + off) = src */
1108 case BPF_STX | BPF_MEM | BPF_B:
1109 if (is_12b_int(off)) {
1110 emit(rv_sb(rd, off, rs), ctx);
1111 break;
1112 }
1113
1114 emit_imm(RV_REG_T1, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001115 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001116 emit(rv_sb(RV_REG_T1, 0, rs), ctx);
1117 break;
1118 case BPF_STX | BPF_MEM | BPF_H:
1119 if (is_12b_int(off)) {
1120 emit(rv_sh(rd, off, rs), ctx);
1121 break;
1122 }
1123
1124 emit_imm(RV_REG_T1, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001125 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001126 emit(rv_sh(RV_REG_T1, 0, rs), ctx);
1127 break;
1128 case BPF_STX | BPF_MEM | BPF_W:
1129 if (is_12b_int(off)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001130 emit_sw(rd, off, rs, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001131 break;
1132 }
1133
1134 emit_imm(RV_REG_T1, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001135 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
1136 emit_sw(RV_REG_T1, 0, rs, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001137 break;
1138 case BPF_STX | BPF_MEM | BPF_DW:
1139 if (is_12b_int(off)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001140 emit_sd(rd, off, rs, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001141 break;
1142 }
1143
1144 emit_imm(RV_REG_T1, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001145 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
1146 emit_sd(RV_REG_T1, 0, rs, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001147 break;
Brendan Jackman91c960b2021-01-14 18:17:44 +00001148 case BPF_STX | BPF_ATOMIC | BPF_W:
1149 case BPF_STX | BPF_ATOMIC | BPF_DW:
1150 if (insn->imm != BPF_ADD) {
1151 pr_err("bpf-jit: not supported: atomic operation %02x ***\n",
1152 insn->imm);
1153 return -EINVAL;
1154 }
1155
1156 /* atomic_add: lock *(u32 *)(dst + off) += src
1157 * atomic_add: lock *(u64 *)(dst + off) += src
1158 */
1159
Björn Töpel2353ecc2019-02-05 13:41:22 +01001160 if (off) {
1161 if (is_12b_int(off)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001162 emit_addi(RV_REG_T1, rd, off, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001163 } else {
1164 emit_imm(RV_REG_T1, off, ctx);
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001165 emit_add(RV_REG_T1, RV_REG_T1, rd, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001166 }
1167
1168 rd = RV_REG_T1;
1169 }
1170
1171 emit(BPF_SIZE(code) == BPF_W ?
1172 rv_amoadd_w(RV_REG_ZERO, rs, rd, 0, 0) :
1173 rv_amoadd_d(RV_REG_ZERO, rs, rd, 0, 0), ctx);
1174 break;
1175 default:
1176 pr_err("bpf-jit: unknown opcode %02x\n", code);
1177 return -EINVAL;
1178 }
1179
1180 return 0;
1181}
1182
Luke Nelsonca6cb542020-03-04 21:02:04 -08001183void bpf_jit_build_prologue(struct rv_jit_context *ctx)
Björn Töpel2353ecc2019-02-05 13:41:22 +01001184{
1185 int stack_adjust = 0, store_offset, bpf_stack_adjust;
1186
Björn Töpelf1003b72019-12-16 10:13:35 +01001187 bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
1188 if (bpf_stack_adjust)
1189 mark_fp(ctx);
1190
Björn Töpel2353ecc2019-02-05 13:41:22 +01001191 if (seen_reg(RV_REG_RA, ctx))
1192 stack_adjust += 8;
1193 stack_adjust += 8; /* RV_REG_FP */
1194 if (seen_reg(RV_REG_S1, ctx))
1195 stack_adjust += 8;
1196 if (seen_reg(RV_REG_S2, ctx))
1197 stack_adjust += 8;
1198 if (seen_reg(RV_REG_S3, ctx))
1199 stack_adjust += 8;
1200 if (seen_reg(RV_REG_S4, ctx))
1201 stack_adjust += 8;
1202 if (seen_reg(RV_REG_S5, ctx))
1203 stack_adjust += 8;
1204 if (seen_reg(RV_REG_S6, ctx))
1205 stack_adjust += 8;
1206
1207 stack_adjust = round_up(stack_adjust, 16);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001208 stack_adjust += bpf_stack_adjust;
1209
1210 store_offset = stack_adjust - 8;
1211
1212 /* First instruction is always setting the tail-call-counter
1213 * (TCC) register. This instruction is skipped for tail calls.
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001214 * Force using a 4-byte (non-compressed) instruction.
Björn Töpel2353ecc2019-02-05 13:41:22 +01001215 */
1216 emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
1217
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001218 emit_addi(RV_REG_SP, RV_REG_SP, -stack_adjust, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001219
1220 if (seen_reg(RV_REG_RA, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001221 emit_sd(RV_REG_SP, store_offset, RV_REG_RA, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001222 store_offset -= 8;
1223 }
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001224 emit_sd(RV_REG_SP, store_offset, RV_REG_FP, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001225 store_offset -= 8;
1226 if (seen_reg(RV_REG_S1, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001227 emit_sd(RV_REG_SP, store_offset, RV_REG_S1, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001228 store_offset -= 8;
1229 }
1230 if (seen_reg(RV_REG_S2, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001231 emit_sd(RV_REG_SP, store_offset, RV_REG_S2, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001232 store_offset -= 8;
1233 }
1234 if (seen_reg(RV_REG_S3, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001235 emit_sd(RV_REG_SP, store_offset, RV_REG_S3, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001236 store_offset -= 8;
1237 }
1238 if (seen_reg(RV_REG_S4, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001239 emit_sd(RV_REG_SP, store_offset, RV_REG_S4, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001240 store_offset -= 8;
1241 }
1242 if (seen_reg(RV_REG_S5, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001243 emit_sd(RV_REG_SP, store_offset, RV_REG_S5, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001244 store_offset -= 8;
1245 }
1246 if (seen_reg(RV_REG_S6, ctx)) {
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001247 emit_sd(RV_REG_SP, store_offset, RV_REG_S6, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001248 store_offset -= 8;
1249 }
1250
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001251 emit_addi(RV_REG_FP, RV_REG_SP, stack_adjust, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001252
1253 if (bpf_stack_adjust)
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001254 emit_addi(RV_REG_S5, RV_REG_SP, bpf_stack_adjust, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001255
1256 /* Program contains calls and tail calls, so RV_REG_TCC need
1257 * to be saved across calls.
1258 */
1259 if (seen_tail_call(ctx) && seen_call(ctx))
Luke Nelson18a4d8c2020-07-20 19:52:40 -07001260 emit_mv(RV_REG_TCC_SAVED, RV_REG_TCC, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001261
1262 ctx->stack_size = stack_adjust;
1263}
1264
Luke Nelsonca6cb542020-03-04 21:02:04 -08001265void bpf_jit_build_epilogue(struct rv_jit_context *ctx)
Björn Töpel2353ecc2019-02-05 13:41:22 +01001266{
Björn Töpelfe8322b2019-12-16 10:13:39 +01001267 __build_epilogue(false, ctx);
Björn Töpel2353ecc2019-02-05 13:41:22 +01001268}