1e4f7ac90SAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
2e4f7ac90SAndrii Nakryiko /* Copyright (c) 2021 Facebook */
3e4f7ac90SAndrii Nakryiko
4e4f7ac90SAndrii Nakryiko #include <linux/bpf.h>
5e4f7ac90SAndrii Nakryiko #define BPF_NO_GLOBAL_DATA
6e4f7ac90SAndrii Nakryiko #include <bpf/bpf_helpers.h>
7e4f7ac90SAndrii Nakryiko
8e4f7ac90SAndrii Nakryiko char LICENSE[] SEC("license") = "GPL";
9e4f7ac90SAndrii Nakryiko
10e4f7ac90SAndrii Nakryiko struct {
11e4f7ac90SAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
12e4f7ac90SAndrii Nakryiko __type(key, int);
13e4f7ac90SAndrii Nakryiko __type(value, int);
14e4f7ac90SAndrii Nakryiko __uint(max_entries, 1);
15e4f7ac90SAndrii Nakryiko } my_pid_map SEC(".maps");
16e4f7ac90SAndrii Nakryiko
17e4f7ac90SAndrii Nakryiko struct {
18e4f7ac90SAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
19e4f7ac90SAndrii Nakryiko __type(key, int);
20e4f7ac90SAndrii Nakryiko __type(value, int);
21e4f7ac90SAndrii Nakryiko __uint(max_entries, 1);
22e4f7ac90SAndrii Nakryiko } res_map SEC(".maps");
23e4f7ac90SAndrii Nakryiko
24e4f7ac90SAndrii Nakryiko volatile int my_pid_var = 0;
25e4f7ac90SAndrii Nakryiko volatile int res_var = 0;
26e4f7ac90SAndrii Nakryiko
27e4f7ac90SAndrii Nakryiko SEC("tp/raw_syscalls/sys_enter")
handle_legacy(void * ctx)28e4f7ac90SAndrii Nakryiko int handle_legacy(void *ctx)
29e4f7ac90SAndrii Nakryiko {
30e4f7ac90SAndrii Nakryiko int zero = 0, *my_pid, cur_pid, *my_res;
31e4f7ac90SAndrii Nakryiko
32e4f7ac90SAndrii Nakryiko my_pid = bpf_map_lookup_elem(&my_pid_map, &zero);
33e4f7ac90SAndrii Nakryiko if (!my_pid)
34e4f7ac90SAndrii Nakryiko return 1;
35e4f7ac90SAndrii Nakryiko
36e4f7ac90SAndrii Nakryiko cur_pid = bpf_get_current_pid_tgid() >> 32;
37e4f7ac90SAndrii Nakryiko if (cur_pid != *my_pid)
38e4f7ac90SAndrii Nakryiko return 1;
39e4f7ac90SAndrii Nakryiko
40e4f7ac90SAndrii Nakryiko my_res = bpf_map_lookup_elem(&res_map, &zero);
41e4f7ac90SAndrii Nakryiko if (!my_res)
42e4f7ac90SAndrii Nakryiko return 1;
43e4f7ac90SAndrii Nakryiko
44e4f7ac90SAndrii Nakryiko if (*my_res == 0)
45e4f7ac90SAndrii Nakryiko /* use bpf_printk() in combination with BPF_NO_GLOBAL_DATA to
46e4f7ac90SAndrii Nakryiko * force .rodata.str1.1 section that previously caused
47e4f7ac90SAndrii Nakryiko * problems on old kernels due to libbpf always tried to
48e4f7ac90SAndrii Nakryiko * create a global data map for it
49e4f7ac90SAndrii Nakryiko */
50e4f7ac90SAndrii Nakryiko bpf_printk("Legacy-case bpf_printk test, pid %d\n", cur_pid);
51e4f7ac90SAndrii Nakryiko *my_res = 1;
52e4f7ac90SAndrii Nakryiko
53e4f7ac90SAndrii Nakryiko return *my_res;
54e4f7ac90SAndrii Nakryiko }
55e4f7ac90SAndrii Nakryiko
56e4f7ac90SAndrii Nakryiko SEC("tp/raw_syscalls/sys_enter")
handle_modern(void * ctx)57e4f7ac90SAndrii Nakryiko int handle_modern(void *ctx)
58e4f7ac90SAndrii Nakryiko {
59*c8ed6685SAndrii Nakryiko int cur_pid;
60e4f7ac90SAndrii Nakryiko
61e4f7ac90SAndrii Nakryiko cur_pid = bpf_get_current_pid_tgid() >> 32;
62e4f7ac90SAndrii Nakryiko if (cur_pid != my_pid_var)
63e4f7ac90SAndrii Nakryiko return 1;
64e4f7ac90SAndrii Nakryiko
65e4f7ac90SAndrii Nakryiko if (res_var == 0)
66e4f7ac90SAndrii Nakryiko /* we need bpf_printk() to validate libbpf logic around unused
67e4f7ac90SAndrii Nakryiko * global maps and legacy kernels; see comment in handle_legacy()
68e4f7ac90SAndrii Nakryiko */
69e4f7ac90SAndrii Nakryiko bpf_printk("Modern-case bpf_printk test, pid %d\n", cur_pid);
70e4f7ac90SAndrii Nakryiko res_var = 1;
71e4f7ac90SAndrii Nakryiko
72e4f7ac90SAndrii Nakryiko return res_var;
73e4f7ac90SAndrii Nakryiko }
74