Skip to content

Commit 0fd1fd0

Browse files
Pu Lehuiborkmann
authored andcommitted
riscv, bpf: Factor out emit_call for kernel and bpf context
The current emit_call function is not suitable for kernel function call as it store return value to bpf R0 register. We can separate it out for common use. Meanwhile, simplify judgment logic, that is, fixed function address can use jal or auipc+jalr, while the unfixed can use only auipc+jalr. Signed-off-by: Pu Lehui <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Björn Töpel <[email protected]> Acked-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5e57fb7 commit 0fd1fd0

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

arch/riscv/net/bpf_jit_comp64.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,12 @@ static void emit_sext_32_rd(u8 *rd, struct rv_jit_context *ctx)
428428
*rd = RV_REG_T2;
429429
}
430430

431-
static int emit_jump_and_link(u8 rd, s64 rvoff, bool force_jalr,
431+
static int emit_jump_and_link(u8 rd, s64 rvoff, bool fixed_addr,
432432
struct rv_jit_context *ctx)
433433
{
434434
s64 upper, lower;
435435

436-
if (rvoff && is_21b_int(rvoff) && !force_jalr) {
436+
if (rvoff && fixed_addr && is_21b_int(rvoff)) {
437437
emit(rv_jal(rd, rvoff >> 1), ctx);
438438
return 0;
439439
} else if (in_auipc_jalr_range(rvoff)) {
@@ -454,24 +454,17 @@ static bool is_signed_bpf_cond(u8 cond)
454454
cond == BPF_JSGE || cond == BPF_JSLE;
455455
}
456456

457-
static int emit_call(bool fixed, u64 addr, struct rv_jit_context *ctx)
457+
static int emit_call(u64 addr, bool fixed_addr, struct rv_jit_context *ctx)
458458
{
459459
s64 off = 0;
460460
u64 ip;
461-
u8 rd;
462-
int ret;
463461

464462
if (addr && ctx->insns) {
465463
ip = (u64)(long)(ctx->insns + ctx->ninsns);
466464
off = addr - ip;
467465
}
468466

469-
ret = emit_jump_and_link(RV_REG_RA, off, !fixed, ctx);
470-
if (ret)
471-
return ret;
472-
rd = bpf_to_rv_reg(BPF_REG_0, ctx);
473-
emit_mv(rd, RV_REG_A0, ctx);
474-
return 0;
467+
return emit_jump_and_link(RV_REG_RA, off, fixed_addr, ctx);
475468
}
476469

477470
static void emit_atomic(u8 rd, u8 rs, s16 off, s32 imm, bool is64,
@@ -913,7 +906,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
913906
/* JUMP off */
914907
case BPF_JMP | BPF_JA:
915908
rvoff = rv_offset(i, off, ctx);
916-
ret = emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
909+
ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
917910
if (ret)
918911
return ret;
919912
break;
@@ -1032,17 +1025,20 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
10321025
/* function call */
10331026
case BPF_JMP | BPF_CALL:
10341027
{
1035-
bool fixed;
1028+
bool fixed_addr;
10361029
u64 addr;
10371030

10381031
mark_call(ctx);
1039-
ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass, &addr,
1040-
&fixed);
1032+
ret = bpf_jit_get_func_addr(ctx->prog, insn, extra_pass,
1033+
&addr, &fixed_addr);
10411034
if (ret < 0)
10421035
return ret;
1043-
ret = emit_call(fixed, addr, ctx);
1036+
1037+
ret = emit_call(addr, fixed_addr, ctx);
10441038
if (ret)
10451039
return ret;
1040+
1041+
emit_mv(bpf_to_rv_reg(BPF_REG_0, ctx), RV_REG_A0, ctx);
10461042
break;
10471043
}
10481044
/* tail call */
@@ -1057,7 +1053,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
10571053
break;
10581054

10591055
rvoff = epilogue_offset(ctx);
1060-
ret = emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
1056+
ret = emit_jump_and_link(RV_REG_ZERO, rvoff, true, ctx);
10611057
if (ret)
10621058
return ret;
10631059
break;

0 commit comments

Comments
 (0)