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 // Only works on 64 bit
22 #if __riscv_xlen == 64
23 static inline void do_mmaps(struct addresses *mmap_addresses)
24 {
25 	/*
26 	 * Place all of the hint addresses on the boundaries of mmap
27 	 * sv39, sv48, sv57
28 	 * User addresses end at 1<<38, 1<<47, 1<<56 respectively
29 	 */
30 	void *on_37_bits = (void *)(1UL << 37);
31 	void *on_38_bits = (void *)(1UL << 38);
32 	void *on_46_bits = (void *)(1UL << 46);
33 	void *on_47_bits = (void *)(1UL << 47);
34 	void *on_55_bits = (void *)(1UL << 55);
35 	void *on_56_bits = (void *)(1UL << 56);
36 
37 	int prot = PROT_READ | PROT_WRITE;
38 	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
39 
40 	mmap_addresses->no_hint =
41 		mmap(NULL, 5 * sizeof(int), prot, flags, 0, 0);
42 	mmap_addresses->on_37_addr =
43 		mmap(on_37_bits, 5 * sizeof(int), prot, flags, 0, 0);
44 	mmap_addresses->on_38_addr =
45 		mmap(on_38_bits, 5 * sizeof(int), prot, flags, 0, 0);
46 	mmap_addresses->on_46_addr =
47 		mmap(on_46_bits, 5 * sizeof(int), prot, flags, 0, 0);
48 	mmap_addresses->on_47_addr =
49 		mmap(on_47_bits, 5 * sizeof(int), prot, flags, 0, 0);
50 	mmap_addresses->on_55_addr =
51 		mmap(on_55_bits, 5 * sizeof(int), prot, flags, 0, 0);
52 	mmap_addresses->on_56_addr =
53 		mmap(on_56_bits, 5 * sizeof(int), prot, flags, 0, 0);
54 }
55 #endif /* __riscv_xlen == 64 */
56 
57 static inline int memory_layout(void)
58 {
59 	int prot = PROT_READ | PROT_WRITE;
60 	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
61 
62 	void *value1 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
63 	void *value2 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
64 
65 	return value2 > value1;
66 }
67 #endif /* _TESTCASES_MMAP_TEST_H */
68