1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
3 
4 #include <linux/bpf.h>
5 #include <stdint.h>
6 #include <bpf/bpf_helpers.h>
7 
8 char _license[] SEC("license") = "GPL";
9 
10 struct {
11 	__uint(type, BPF_MAP_TYPE_ARRAY);
12 	__uint(max_entries, 4096);
13 	__uint(map_flags, BPF_F_MMAPABLE | BPF_F_RDONLY_PROG);
14 	__type(key, __u32);
15 	__type(value, char);
16 } rdonly_map SEC(".maps");
17 
18 struct {
19 	__uint(type, BPF_MAP_TYPE_ARRAY);
20 	__uint(max_entries, 512 * 4); /* at least 4 pages of data */
21 	__uint(map_flags, BPF_F_MMAPABLE);
22 	__type(key, __u32);
23 	__type(value, __u64);
24 } data_map SEC(".maps");
25 
26 __u64 in_val = 0;
27 __u64 out_val = 0;
28 
29 SEC("raw_tracepoint/sys_enter")
test_mmap(void * ctx)30 int test_mmap(void *ctx)
31 {
32 	int zero = 0, one = 1, two = 2, far = 1500;
33 	__u64 val, *p;
34 
35 	out_val = in_val;
36 
37 	/* data_map[2] = in_val; */
38 	bpf_map_update_elem(&data_map, &two, (const void *)&in_val, 0);
39 
40 	/* data_map[1] = data_map[0] * 2; */
41 	p = bpf_map_lookup_elem(&data_map, &zero);
42 	if (p) {
43 		val = (*p) * 2;
44 		bpf_map_update_elem(&data_map, &one, &val, 0);
45 	}
46 
47 	/* data_map[far] = in_val * 3; */
48 	val = in_val * 3;
49 	bpf_map_update_elem(&data_map, &far, &val, 0);
50 
51 	return 0;
52 }
53 
54