125818556STejun Heo // SPDX-License-Identifier: GPL-2.0
225818556STejun Heo /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
325818556STejun Heo
425818556STejun Heo /*
525818556STejun Heo * Verify the JIT-emitted rebase sequences for __arena and __arena__nullable
625818556STejun Heo * kfunc arguments. The capture kfuncs take the argument without
725818556STejun Heo * dereferencing it, so these tests pin only the emitted code.
825818556STejun Heo */
925818556STejun Heo #define BPF_NO_KFUNC_PROTOTYPES
1025818556STejun Heo #include <vmlinux.h>
1125818556STejun Heo #include <bpf/bpf_helpers.h>
1225818556STejun Heo #include "bpf_misc.h"
1325818556STejun Heo #include "bpf_experimental.h"
1425818556STejun Heo #include <bpf_arena_common.h>
1525818556STejun Heo #include "../test_kmods/bpf_testmod_kfunc.h"
1625818556STejun Heo
1725818556STejun Heo struct {
1825818556STejun Heo __uint(type, BPF_MAP_TYPE_ARENA);
1925818556STejun Heo __uint(map_flags, BPF_F_MMAPABLE);
2025818556STejun Heo __uint(max_entries, 1);
2125818556STejun Heo } arena SEC(".maps");
2225818556STejun Heo
2325818556STejun Heo /* volatile to force the scalar reloads below */
2425818556STejun Heo volatile u64 stash;
2525818556STejun Heo
2625818556STejun Heo #if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
2725818556STejun Heo
2825818556STejun Heo SEC("syscall")
2925818556STejun Heo __arch_x86_64
3025818556STejun Heo __jited("...")
3125818556STejun Heo __jited(" movl %edi, %edi")
3225818556STejun Heo __jited(" addq %r12, %rdi")
3325818556STejun Heo __jited("...")
3425818556STejun Heo __jited(" callq {{.*}}")
35*1c5bc60fSPuranjay Mohan __arch_arm64
36*1c5bc60fSPuranjay Mohan __jited("...")
37*1c5bc60fSPuranjay Mohan __jited(" add x0, x28, w0, uxtw")
38*1c5bc60fSPuranjay Mohan __jited(" {{(bl|mov) .*}}")
3925818556STejun Heo __success
arena_arg_jit_rebase(void * ctx)4025818556STejun Heo int arena_arg_jit_rebase(void *ctx)
4125818556STejun Heo {
4225818556STejun Heo stash = (u64)bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
4325818556STejun Heo bpf_kfunc_arena_cap_test((u64 *)stash);
4425818556STejun Heo return 0;
4525818556STejun Heo }
4625818556STejun Heo
4725818556STejun Heo SEC("syscall")
4825818556STejun Heo __arch_x86_64
4925818556STejun Heo __jited("...")
5025818556STejun Heo __jited(" movl %edi, %edi")
5125818556STejun Heo __jited(" testl %edi, %edi")
5225818556STejun Heo __jited(" je L0")
5325818556STejun Heo __jited(" addq %r12, %rdi")
5425818556STejun Heo __jited("L0: callq {{.*}}")
55*1c5bc60fSPuranjay Mohan __arch_arm64
56*1c5bc60fSPuranjay Mohan __jited("...")
57*1c5bc60fSPuranjay Mohan __jited(" mov w0, w0")
58*1c5bc60fSPuranjay Mohan __jited(" cbz w0, L0")
59*1c5bc60fSPuranjay Mohan __jited(" add x0, x28, w0, uxtw")
60*1c5bc60fSPuranjay Mohan __jited("L0: {{.*}}")
6125818556STejun Heo __success
arena_arg_jit_nullable(void * ctx)6225818556STejun Heo int arena_arg_jit_nullable(void *ctx)
6325818556STejun Heo {
6425818556STejun Heo stash = (u64)bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
6525818556STejun Heo bpf_kfunc_arena_cap_nullable_test((u64 *)stash);
6625818556STejun Heo return 0;
6725818556STejun Heo }
6825818556STejun Heo
6925818556STejun Heo SEC("syscall")
7025818556STejun Heo __arch_x86_64
7125818556STejun Heo __jited("...")
7225818556STejun Heo __jited(" movl %edi, %edi")
7325818556STejun Heo __jited(" addq %r12, %rdi")
7425818556STejun Heo __jited(" movl %esi, %esi")
7525818556STejun Heo __jited(" addq %r12, %rsi")
7625818556STejun Heo __jited(" movl %edx, %edx")
7725818556STejun Heo __jited(" addq %r12, %rdx")
7825818556STejun Heo __jited(" movl %ecx, %ecx")
7925818556STejun Heo __jited(" addq %r12, %rcx")
8025818556STejun Heo __jited(" movl %r8d, %r8d")
8125818556STejun Heo __jited(" testl %r8d, %r8d")
8225818556STejun Heo __jited(" je L0")
8325818556STejun Heo __jited(" addq %r12, %r8")
8425818556STejun Heo __jited("L0: callq {{.*}}")
85*1c5bc60fSPuranjay Mohan __arch_arm64
86*1c5bc60fSPuranjay Mohan __jited("...")
87*1c5bc60fSPuranjay Mohan __jited(" add x0, x28, w0, uxtw")
88*1c5bc60fSPuranjay Mohan __jited(" add x1, x28, w1, uxtw")
89*1c5bc60fSPuranjay Mohan __jited(" add x2, x28, w2, uxtw")
90*1c5bc60fSPuranjay Mohan __jited(" add x3, x28, w3, uxtw")
91*1c5bc60fSPuranjay Mohan __jited(" mov w4, w4")
92*1c5bc60fSPuranjay Mohan __jited(" cbz w4, L0")
93*1c5bc60fSPuranjay Mohan __jited(" add x4, x28, w4, uxtw")
94*1c5bc60fSPuranjay Mohan __jited("L0: {{.*}}")
9525818556STejun Heo __success
arena_arg_jit_args5(void * ctx)9625818556STejun Heo int arena_arg_jit_args5(void *ctx)
9725818556STejun Heo {
9825818556STejun Heo u64 __arena *val;
9925818556STejun Heo
10025818556STejun Heo val = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
10125818556STejun Heo if (!val)
10225818556STejun Heo return 1;
10325818556STejun Heo
10425818556STejun Heo val[0] = 1;
10525818556STejun Heo val[1] = 2;
10625818556STejun Heo val[2] = 4;
10725818556STejun Heo val[3] = 8;
10825818556STejun Heo val[4] = 16;
10925818556STejun Heo
11025818556STejun Heo bpf_kfunc_arena_args5_test((u64 *)&val[0], (u64 *)&val[1],
11125818556STejun Heo (u64 *)&val[2], (u64 *)&val[3],
11225818556STejun Heo (u64 *)&val[4]);
11325818556STejun Heo return 0;
11425818556STejun Heo }
11525818556STejun Heo
11625818556STejun Heo #endif /* __BPF_FEATURE_ADDR_SPACE_CAST */
11725818556STejun Heo
11825818556STejun Heo char _license[] SEC("license") = "GPL";
119