xref: /linux/tools/testing/selftests/bpf/progs/test_mmap.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
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(map_flags, BPF_F_MMAPABLE | BPF_F_RDONLY_PROG);
13 	__type(key, __u32);
14 	__type(value, char);
15 } rdonly_map SEC(".maps");
16 
17 struct {
18 	__uint(type, BPF_MAP_TYPE_ARRAY);
19 	__uint(map_flags, BPF_F_MMAPABLE);
20 	__type(key, __u32);
21 	__type(value, __u64);
22 } data_map SEC(".maps");
23 
24 __u64 in_val = 0;
25 __u64 out_val = 0;
26 
27 SEC("raw_tracepoint/sys_enter")
28 int test_mmap(void *ctx)
29 {
30 	int zero = 0, one = 1, two = 2, far = 1500;
31 	__u64 val, *p;
32 
33 	out_val = in_val;
34 
35 	/* data_map[2] = in_val; */
36 	bpf_map_update_elem(&data_map, &two, (const void *)&in_val, 0);
37 
38 	/* data_map[1] = data_map[0] * 2; */
39 	p = bpf_map_lookup_elem(&data_map, &zero);
40 	if (p) {
41 		val = (*p) * 2;
42 		bpf_map_update_elem(&data_map, &one, &val, 0);
43 	}
44 
45 	/* data_map[far] = in_val * 3; */
46 	val = in_val * 3;
47 	bpf_map_update_elem(&data_map, &far, &val, 0);
48 
49 	return 0;
50 }
51 
52