xref: /linux/tools/testing/selftests/bpf/progs/test_ksyms_btf.c (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Google */
3 
4 #include "vmlinux.h"
5 
6 #include <bpf/bpf_helpers.h>
7 
8 __u64 out__runqueues_addr = -1;
9 __u64 out__bpf_prog_active_addr = -1;
10 
11 __u32 out__rq_cpu = -1; /* percpu struct fields */
12 int out__bpf_prog_active = -1; /* percpu int */
13 
14 __u32 out__this_rq_cpu = -1;
15 int out__this_bpf_prog_active = -1;
16 
17 __u32 out__cpu_0_rq_cpu = -1; /* cpu_rq(0)->cpu */
18 
19 extern const struct rq runqueues __ksym; /* struct type global var. */
20 extern const int bpf_prog_active __ksym; /* int type global var. */
21 
22 SEC("raw_tp/sys_enter")
23 int handler(const void *ctx)
24 {
25 	struct rq *rq;
26 	int *active;
27 	__u32 cpu;
28 
29 	out__runqueues_addr = (__u64)&runqueues;
30 	out__bpf_prog_active_addr = (__u64)&bpf_prog_active;
31 
32 	cpu = bpf_get_smp_processor_id();
33 
34 	/* test bpf_per_cpu_ptr() */
35 	rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, cpu);
36 	if (rq)
37 		out__rq_cpu = rq->cpu;
38 	active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
39 	if (active)
40 		out__bpf_prog_active = *active;
41 
42 	rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 0);
43 	if (rq) /* should always be valid, but we can't spare the check. */
44 		out__cpu_0_rq_cpu = rq->cpu;
45 
46 	/* test bpf_this_cpu_ptr */
47 	rq = (struct rq *)bpf_this_cpu_ptr(&runqueues);
48 	out__this_rq_cpu = rq->cpu;
49 	active = (int *)bpf_this_cpu_ptr(&bpf_prog_active);
50 	out__this_bpf_prog_active = *active;
51 
52 	return 0;
53 }
54 
55 char _license[] SEC("license") = "GPL";
56