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