1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2020 Facebook
3
4#include "vmlinux.h"
5#include <bpf/bpf_helpers.h>
6#include <bpf/bpf_core_read.h>
7
8char _license[] SEC("license") = "GPL";
9
10/* shuffled layout for relocatable (CO-RE) reads */
11struct callback_head___shuffled {
12 void (*func)(struct callback_head___shuffled *head);
13 struct callback_head___shuffled *next;
14};
15
16struct callback_head k_probe_in = {};
17struct callback_head___shuffled k_core_in = {};
18
19struct callback_head *u_probe_in = 0;
20struct callback_head___shuffled *u_core_in = 0;
21
22long k_probe_out = 0;
23long u_probe_out = 0;
24
25long k_core_out = 0;
26long u_core_out = 0;
27
28int my_pid = 0;
29
30SEC("raw_tracepoint/sys_enter")
31int handler(void *ctx)
32{
33 int pid = bpf_get_current_pid_tgid() >> 32;
34
35 if (my_pid != pid)
36 return 0;
37
38 /* next pointers for kernel address space have to be initialized from
39 * BPF side, user-space mmaped addresses are stil user-space addresses
40 */
41 k_probe_in.next = &k_probe_in;
42 __builtin_preserve_access_index(({k_core_in.next = &k_core_in;}));
43
44 k_probe_out = (long)BPF_PROBE_READ(&k_probe_in, next, next, func);
45 k_core_out = (long)BPF_CORE_READ(&k_core_in, next, next, func);
46 u_probe_out = (long)BPF_PROBE_READ_USER(u_probe_in, next, next, func);
47 u_core_out = (long)BPF_CORE_READ_USER(u_core_in, next, next, func);
48
49 return 0;
50}
51

source code of linux/tools/testing/selftests/bpf/progs/test_core_read_macros.c