1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _TESTCASES_MMAP_TEST_H
3 #define _TESTCASES_MMAP_TEST_H
4 #include <sys/mman.h>
5 #include <sys/resource.h>
6 #include <stddef.h>
7 
8 #define TOP_DOWN 0
9 #define BOTTOM_UP 1
10 
11 struct addresses {
12 	int *no_hint;
13 	int *on_37_addr;
14 	int *on_38_addr;
15 	int *on_46_addr;
16 	int *on_47_addr;
17 	int *on_55_addr;
18 	int *on_56_addr;
19 };
20 
21 static inline void do_mmaps(struct addresses *mmap_addresses)
22 {
23 	/*
24 	 * Place all of the hint addresses on the boundaries of mmap
25 	 * sv39, sv48, sv57
26 	 * User addresses end at 1<<38, 1<<47, 1<<56 respectively
27 	 */
28 	void *on_37_bits = (void *)(1UL << 37);
29 	void *on_38_bits = (void *)(1UL << 38);
30 	void *on_46_bits = (void *)(1UL << 46);
31 	void *on_47_bits = (void *)(1UL << 47);
32 	void *on_55_bits = (void *)(1UL << 55);
33 	void *on_56_bits = (void *)(1UL << 56);
34 
35 	int prot = PROT_READ | PROT_WRITE;
36 	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
37 
38 	mmap_addresses->no_hint =
39 		mmap(NULL, 5 * sizeof(int), prot, flags, 0, 0);
40 	mmap_addresses->on_37_addr =
41 		mmap(on_37_bits, 5 * sizeof(int), prot, flags, 0, 0);
42 	mmap_addresses->on_38_addr =
43 		mmap(on_38_bits, 5 * sizeof(int), prot, flags, 0, 0);
44 	mmap_addresses->on_46_addr =
45 		mmap(on_46_bits, 5 * sizeof(int), prot, flags, 0, 0);
46 	mmap_addresses->on_47_addr =
47 		mmap(on_47_bits, 5 * sizeof(int), prot, flags, 0, 0);
48 	mmap_addresses->on_55_addr =
49 		mmap(on_55_bits, 5 * sizeof(int), prot, flags, 0, 0);
50 	mmap_addresses->on_56_addr =
51 		mmap(on_56_bits, 5 * sizeof(int), prot, flags, 0, 0);
52 }
53 
54 static inline int memory_layout(void)
55 {
56 	int prot = PROT_READ | PROT_WRITE;
57 	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
58 
59 	void *value1 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
60 	void *value2 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
61 
62 	return value2 > value1;
63 }
64 #endif /* _TESTCASES_MMAP_TEST_H */
65