xref: /linux/tools/testing/selftests/bpf/progs/jit_probe_mem.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 #include <vmlinux.h>
4 #include <bpf/bpf_tracing.h>
5 #include <bpf/bpf_helpers.h>
6 #include "../bpf_testmod/bpf_testmod_kfunc.h"
7 
8 static struct prog_test_ref_kfunc __kptr *v;
9 long total_sum = -1;
10 
11 SEC("tc")
12 int test_jit_probe_mem(struct __sk_buff *ctx)
13 {
14 	struct prog_test_ref_kfunc *p;
15 	unsigned long zero = 0, sum;
16 
17 	p = bpf_kfunc_call_test_acquire(&zero);
18 	if (!p)
19 		return 1;
20 
21 	p = bpf_kptr_xchg(&v, p);
22 	if (p)
23 		goto release_out;
24 
25 	/* Direct map value access of kptr, should be PTR_UNTRUSTED */
26 	p = v;
27 	if (!p)
28 		return 1;
29 
30 	asm volatile (
31 		"r9 = %[p];"
32 		"%[sum] = 0;"
33 
34 		/* r8 = p->a */
35 		"r8 = *(u32 *)(r9 + 0);"
36 		"%[sum] += r8;"
37 
38 		/* r8 = p->b */
39 		"r8 = *(u32 *)(r9 + 4);"
40 		"%[sum] += r8;"
41 
42 		"r9 += 8;"
43 		/* r9 = p->a */
44 		"r9 = *(u32 *)(r9 - 8);"
45 		"%[sum] += r9;"
46 
47 		: [sum] "=r"(sum)
48 		: [p] "r"(p)
49 		: "r8", "r9"
50 	);
51 
52 	total_sum = sum;
53 	return 0;
54 release_out:
55 	bpf_kfunc_call_test_release(p);
56 	return 1;
57 }
58 
59 char _license[] SEC("license") = "GPL";
60