xref: /openbmc/linux/tools/testing/selftests/bpf/progs/test_ksyms_btf.c (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
12c2f6abeSHao Luo // SPDX-License-Identifier: GPL-2.0
22c2f6abeSHao Luo /* Copyright (c) 2020 Google */
32c2f6abeSHao Luo 
42c2f6abeSHao Luo #include "vmlinux.h"
52c2f6abeSHao Luo 
62c2f6abeSHao Luo #include <bpf/bpf_helpers.h>
72c2f6abeSHao Luo 
82c2f6abeSHao Luo __u64 out__runqueues_addr = -1;
92c2f6abeSHao Luo __u64 out__bpf_prog_active_addr = -1;
102c2f6abeSHao Luo 
11*00dc73e4SHao Luo __u32 out__rq_cpu = -1; /* percpu struct fields */
12*00dc73e4SHao Luo int out__bpf_prog_active = -1; /* percpu int */
13*00dc73e4SHao Luo 
14*00dc73e4SHao Luo __u32 out__this_rq_cpu = -1;
15*00dc73e4SHao Luo int out__this_bpf_prog_active = -1;
16*00dc73e4SHao Luo 
17*00dc73e4SHao Luo __u32 out__cpu_0_rq_cpu = -1; /* cpu_rq(0)->cpu */
18*00dc73e4SHao Luo 
192c2f6abeSHao Luo extern const struct rq runqueues __ksym; /* struct type global var. */
202c2f6abeSHao Luo extern const int bpf_prog_active __ksym; /* int type global var. */
212c2f6abeSHao Luo 
222c2f6abeSHao Luo SEC("raw_tp/sys_enter")
handler(const void * ctx)232c2f6abeSHao Luo int handler(const void *ctx)
242c2f6abeSHao Luo {
25*00dc73e4SHao Luo 	struct rq *rq;
26*00dc73e4SHao Luo 	int *active;
27*00dc73e4SHao Luo 	__u32 cpu;
28*00dc73e4SHao Luo 
292c2f6abeSHao Luo 	out__runqueues_addr = (__u64)&runqueues;
302c2f6abeSHao Luo 	out__bpf_prog_active_addr = (__u64)&bpf_prog_active;
312c2f6abeSHao Luo 
32*00dc73e4SHao Luo 	cpu = bpf_get_smp_processor_id();
33*00dc73e4SHao Luo 
34*00dc73e4SHao Luo 	/* test bpf_per_cpu_ptr() */
35*00dc73e4SHao Luo 	rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, cpu);
36*00dc73e4SHao Luo 	if (rq)
37*00dc73e4SHao Luo 		out__rq_cpu = rq->cpu;
38*00dc73e4SHao Luo 	active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
39*00dc73e4SHao Luo 	if (active)
40*00dc73e4SHao Luo 		out__bpf_prog_active = *active;
41*00dc73e4SHao Luo 
42*00dc73e4SHao Luo 	rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 0);
43*00dc73e4SHao Luo 	if (rq) /* should always be valid, but we can't spare the check. */
44*00dc73e4SHao Luo 		out__cpu_0_rq_cpu = rq->cpu;
45*00dc73e4SHao Luo 
46*00dc73e4SHao Luo 	/* test bpf_this_cpu_ptr */
47*00dc73e4SHao Luo 	rq = (struct rq *)bpf_this_cpu_ptr(&runqueues);
48*00dc73e4SHao Luo 	out__this_rq_cpu = rq->cpu;
49*00dc73e4SHao Luo 	active = (int *)bpf_this_cpu_ptr(&bpf_prog_active);
50*00dc73e4SHao Luo 	out__this_bpf_prog_active = *active;
51*00dc73e4SHao Luo 
522c2f6abeSHao Luo 	return 0;
532c2f6abeSHao Luo }
542c2f6abeSHao Luo 
552c2f6abeSHao Luo char _license[] SEC("license") = "GPL";
56