]> www.infradead.org Git - users/hch/misc.git/commitdiff
powerpc64/bpf: Implement bpf_addr_space_cast instruction
authorSaket Kumar Bhaskar <skb99@linux.ibm.com>
Thu, 4 Sep 2025 10:08:33 +0000 (15:38 +0530)
committerMadhavan Srinivasan <maddy@linux.ibm.com>
Sat, 6 Sep 2025 10:19:43 +0000 (15:49 +0530)
LLVM generates bpf_addr_space_cast instruction while translating
pointers between native (zero) address space and
__attribute__((address_space(N))). The addr_space=0 is reserved as
bpf_arena address space.

rY = addr_space_cast(rX, 0, 1) is processed by the verifier and
converted to normal 32-bit move: wX = wY.

rY = addr_space_cast(rX, 1, 0) : used to convert a bpf arena pointer to
a pointer in the userspace vma. This has to be converted by the JIT.

PPC_RAW_RLDICL_DOT, a variant of PPC_RAW_RLDICL is introduced to set
condition register as well.

Reviewed-by: Hari Bathini <hbathini@linux.ibm.com>
Tested-by: Venkat Rao Bagalkote <venkat88@linux.ibm.com>
Signed-off-by: Saket Kumar Bhaskar <skb99@linux.ibm.com>
Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
Link: https://patch.msgid.link/20250904100835.1100423-3-skb99@linux.ibm.com
arch/powerpc/include/asm/ppc-opcode.h
arch/powerpc/net/bpf_jit.h
arch/powerpc/net/bpf_jit_comp.c
arch/powerpc/net/bpf_jit_comp64.c

index 8053b24afc39567e363775fb62f4c65bd36c1a0a..55ca49d183196fc4143a7f861ad5029e0cc2b349 100644 (file)
                                        (0x54000001 | ___PPC_RA(d) | ___PPC_RS(a) | __PPC_SH(i) | __PPC_MB(mb) | __PPC_ME(me))
 #define PPC_RAW_RLWIMI(d, a, i, mb, me) (0x50000000 | ___PPC_RA(d) | ___PPC_RS(a) | __PPC_SH(i) | __PPC_MB(mb) | __PPC_ME(me))
 #define PPC_RAW_RLDICL(d, a, i, mb)     (0x78000000 | ___PPC_RA(d) | ___PPC_RS(a) | __PPC_SH64(i) | __PPC_MB64(mb))
+#define PPC_RAW_RLDICL_DOT(d, a, i, mb) (0x78000000 | ___PPC_RA(d) | ___PPC_RS(a) | __PPC_SH64(i) | __PPC_MB64(mb) | 0x1)
 #define PPC_RAW_RLDICR(d, a, i, me)     (0x78000004 | ___PPC_RA(d) | ___PPC_RS(a) | __PPC_SH64(i) | __PPC_ME64(me))
 
 /* slwi = rlwinm Rx, Ry, n, 0, 31-n */
index b4a5f58b7e3d482b443fd2fc419e8536f822ab53..8334cd667bba1e7d45c270d84ccf86f4c181c5d8 100644 (file)
@@ -165,6 +165,7 @@ struct codegen_context {
        unsigned int exentry_idx;
        unsigned int alt_exit_addr;
        u64 arena_vm_start;
+       u64 user_vm_start;
 };
 
 #define bpf_to_ppc(r)  (ctx->b2p[r])
index 7d070232159f2bd8d365930e771b16d72bdbfb4b..cfa84cab0a1814238737880b1d341d5ae8c16ad0 100644 (file)
@@ -205,6 +205,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
        /* Make sure that the stack is quadword aligned. */
        cgctx.stack_size = round_up(fp->aux->stack_depth, 16);
        cgctx.arena_vm_start = bpf_arena_get_kern_vm_start(fp->aux->arena);
+       cgctx.user_vm_start = bpf_arena_get_user_vm_start(fp->aux->arena);
 
        /* Scouting faux-generate pass 0 */
        if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false)) {
@@ -439,6 +440,11 @@ bool bpf_jit_supports_kfunc_call(void)
        return true;
 }
 
+bool bpf_jit_supports_arena(void)
+{
+       return IS_ENABLED(CONFIG_PPC64);
+}
+
 bool bpf_jit_supports_far_kfunc_call(void)
 {
        return IS_ENABLED(CONFIG_PPC64);
index 569619f1b31c34e65f7a67f7a41a2f38076a1da4..76efb47f02a675e378a9e6d79bc0ddea97a8d473 100644 (file)
@@ -812,6 +812,16 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
                 */
                case BPF_ALU | BPF_MOV | BPF_X: /* (u32) dst = src */
                case BPF_ALU64 | BPF_MOV | BPF_X: /* dst = src */
+
+                       if (insn_is_cast_user(&insn[i])) {
+                               EMIT(PPC_RAW_RLDICL_DOT(tmp1_reg, src_reg, 0, 32));
+                               PPC_LI64(dst_reg, (ctx->user_vm_start & 0xffffffff00000000UL));
+                               PPC_BCC_SHORT(COND_EQ, (ctx->idx + 2) * 4);
+                               EMIT(PPC_RAW_OR(tmp1_reg, dst_reg, tmp1_reg));
+                               EMIT(PPC_RAW_MR(dst_reg, tmp1_reg));
+                               break;
+                       }
+
                        if (imm == 1) {
                                /* special mov32 for zext */
                                EMIT(PPC_RAW_RLWINM(dst_reg, dst_reg, 0, 0, 31));