summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/arena_kfunc_jit.c
blob: b5a01cbc33a735c8127e6553878e3b3a415e9c8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */

/*
 * Verify the JIT-emitted rebase sequences for __arena and __arena__nullable
 * kfunc arguments. The capture kfuncs take the argument without
 * dereferencing it, so these tests pin only the emitted code.
 */
#define BPF_NO_KFUNC_PROTOTYPES
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "bpf_experimental.h"
#include <bpf_arena_common.h>
#include "../test_kmods/bpf_testmod_kfunc.h"

struct {
	__uint(type, BPF_MAP_TYPE_ARENA);
	__uint(map_flags, BPF_F_MMAPABLE);
	__uint(max_entries, 1);
} arena SEC(".maps");

/* volatile to force the scalar reloads below */
volatile u64 stash;

#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)

SEC("syscall")
__arch_x86_64
__jited("...")
__jited("	movl	%edi, %edi")
__jited("	addq	%r12, %rdi")
__jited("...")
__jited("	callq	{{.*}}")
__arch_arm64
__jited("...")
__jited("	add	x0, x28, w0, uxtw")
__jited("	{{(bl|mov)	.*}}")
__success
int arena_arg_jit_rebase(void *ctx)
{
	stash = (u64)bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
	bpf_kfunc_arena_cap_test((u64 *)stash);
	return 0;
}

SEC("syscall")
__arch_x86_64
__jited("...")
__jited("	movl	%edi, %edi")
__jited("	testl	%edi, %edi")
__jited("	je	L0")
__jited("	addq	%r12, %rdi")
__jited("L0:	callq	{{.*}}")
__arch_arm64
__jited("...")
__jited("	mov	w0, w0")
__jited("	cbz	w0, L0")
__jited("	add	x0, x28, w0, uxtw")
__jited("L0:	{{.*}}")
__success
int arena_arg_jit_nullable(void *ctx)
{
	stash = (u64)bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
	bpf_kfunc_arena_cap_nullable_test((u64 *)stash);
	return 0;
}

SEC("syscall")
__arch_x86_64
__jited("...")
__jited("	movl	%edi, %edi")
__jited("	addq	%r12, %rdi")
__jited("	movl	%esi, %esi")
__jited("	addq	%r12, %rsi")
__jited("	movl	%edx, %edx")
__jited("	addq	%r12, %rdx")
__jited("	movl	%ecx, %ecx")
__jited("	addq	%r12, %rcx")
__jited("	movl	%r8d, %r8d")
__jited("	testl	%r8d, %r8d")
__jited("	je	L0")
__jited("	addq	%r12, %r8")
__jited("L0:	callq	{{.*}}")
__arch_arm64
__jited("...")
__jited("	add	x0, x28, w0, uxtw")
__jited("	add	x1, x28, w1, uxtw")
__jited("	add	x2, x28, w2, uxtw")
__jited("	add	x3, x28, w3, uxtw")
__jited("	mov	w4, w4")
__jited("	cbz	w4, L0")
__jited("	add	x4, x28, w4, uxtw")
__jited("L0:	{{.*}}")
__success
int arena_arg_jit_args5(void *ctx)
{
	u64 __arena *val;

	val = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
	if (!val)
		return 1;

	val[0] = 1;
	val[1] = 2;
	val[2] = 4;
	val[3] = 8;
	val[4] = 16;

	bpf_kfunc_arena_args5_test((u64 *)&val[0], (u64 *)&val[1],
				   (u64 *)&val[2], (u64 *)&val[3],
				   (u64 *)&val[4]);
	return 0;
}

#endif /* __BPF_FEATURE_ADDR_SPACE_CAST */

char _license[] SEC("license") = "GPL";