The BPF callx instruction allows indirect calls to BPF subprograms through function pointers. Unlike direct calls, where the target subprogram is known at JIT compilation time, callx requires the target address to be obtained at runtime. Add callx instruction support to the PowerPC BPF JIT for PPC64 ELF ABI v2. Implement handling of BPF_CALL | BPF_X instructions by loading the target register into the count register (CTR) using mtctr and invoking the target subprogram using bctrl. Move the return value from r3 into BPF_REG_0 after the call. Setting SEEN_FUNC ensures the Link Register (LR) is preserved by the prologue and epilogue. Add bpf_jit_supports_callx() in bpf_jit_comp.c to indicate that CALLX is supported when CONFIG_PPC64_ELF_ABI_V2 is enabled. Signed-off-by: Shivani Nittor --- arch/powerpc/net/bpf_jit_comp.c | 5 +++++ arch/powerpc/net/bpf_jit_comp64.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c index ce2966c52ac5..42762dc24198 100644 --- a/arch/powerpc/net/bpf_jit_comp.c +++ b/arch/powerpc/net/bpf_jit_comp.c @@ -564,6 +564,11 @@ bool bpf_jit_supports_subprog_tailcalls(void) return IS_ENABLED(CONFIG_PPC64); } +bool bpf_jit_supports_callx(void) +{ + return IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2); +} + bool bpf_jit_supports_timed_may_goto(void) { return IS_ENABLED(CONFIG_PPC64); diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c index c1e86fe0bd79..e5fda9cbb44b 100644 --- a/arch/powerpc/net/bpf_jit_comp64.c +++ b/arch/powerpc/net/bpf_jit_comp64.c @@ -1745,6 +1745,27 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code /* else fall through to the epilogue */ break; + /* + * Indirect call to a BPF subprogram + * Supported only on PPC64 ELF ABI v2 (gated via + * bpf_jit_supports_callx()). + * + * Note: The verifier guarantees that the target is a JITed BPF + * subprog, whose prologue does not depend on r12 (the ELFv2 + * global entry point convention): r2 is reloaded from + * paca->kernel_toc, or isn't used with PCREL. So the target + * only needs to be in CTR, and r12 need not be set up. + */ + case BPF_JMP | BPF_CALL | BPF_X: + ctx->seen |= SEEN_FUNC; + + EMIT(PPC_RAW_MTCTR(dst_reg)); + EMIT(PPC_RAW_BCTRL()); + + /* Move return value from r3 to BPF_REG_0 */ + EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_0), _R3)); + break; + /* * Call kernel helper or bpf function */ -- 2.52.0