1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2017, Anshuman Khandual, IBM Corp.
4 *
5 * Works on architectures which support 128TB virtual
6 * address range and beyond.
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <sys/mman.h>
14 #include <sys/time.h>
15
16 /*
17 * Maximum address range mapped with a single mmap()
18 * call is little bit more than 1GB. Hence 1GB is
19 * chosen as the single chunk size for address space
20 * mapping.
21 */
22
23 #define SZ_1GB (1024 * 1024 * 1024UL)
24 #define SZ_1TB (1024 * 1024 * 1024 * 1024UL)
25
26 #define MAP_CHUNK_SIZE SZ_1GB
27
28 /*
29 * Address space till 128TB is mapped without any hint
30 * and is enabled by default. Address space beyond 128TB
31 * till 512TB is obtained by passing hint address as the
32 * first argument into mmap() system call.
33 *
34 * The process heap address space is divided into two
35 * different areas one below 128TB and one above 128TB
36 * till it reaches 512TB. One with size 128TB and the
37 * other being 384TB.
38 *
39 * On Arm64 the address space is 256TB and support for
40 * high mappings up to 4PB virtual address space has
41 * been added.
42 */
43
44 #define NR_CHUNKS_128TB ((128 * SZ_1TB) / MAP_CHUNK_SIZE) /* Number of chunks for 128TB */
45 #define NR_CHUNKS_256TB (NR_CHUNKS_128TB * 2UL)
46 #define NR_CHUNKS_384TB (NR_CHUNKS_128TB * 3UL)
47 #define NR_CHUNKS_3840TB (NR_CHUNKS_128TB * 30UL)
48
49 #define ADDR_MARK_128TB (1UL << 47) /* First address beyond 128TB */
50 #define ADDR_MARK_256TB (1UL << 48) /* First address beyond 256TB */
51
52 #ifdef __aarch64__
53 #define HIGH_ADDR_MARK ADDR_MARK_256TB
54 #define HIGH_ADDR_SHIFT 49
55 #define NR_CHUNKS_LOW NR_CHUNKS_256TB
56 #define NR_CHUNKS_HIGH NR_CHUNKS_3840TB
57 #else
58 #define HIGH_ADDR_MARK ADDR_MARK_128TB
59 #define HIGH_ADDR_SHIFT 48
60 #define NR_CHUNKS_LOW NR_CHUNKS_128TB
61 #define NR_CHUNKS_HIGH NR_CHUNKS_384TB
62 #endif
63
hind_addr(void)64 static char *hind_addr(void)
65 {
66 int bits = HIGH_ADDR_SHIFT + rand() % (63 - HIGH_ADDR_SHIFT);
67
68 return (char *) (1UL << bits);
69 }
70
validate_addr(char * ptr,int high_addr)71 static int validate_addr(char *ptr, int high_addr)
72 {
73 unsigned long addr = (unsigned long) ptr;
74
75 if (high_addr) {
76 if (addr < HIGH_ADDR_MARK) {
77 printf("Bad address %lx\n", addr);
78 return 1;
79 }
80 return 0;
81 }
82
83 if (addr > HIGH_ADDR_MARK) {
84 printf("Bad address %lx\n", addr);
85 return 1;
86 }
87 return 0;
88 }
89
validate_lower_address_hint(void)90 static int validate_lower_address_hint(void)
91 {
92 char *ptr;
93
94 ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
95 PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
96
97 if (ptr == MAP_FAILED)
98 return 0;
99
100 return 1;
101 }
102
main(int argc,char * argv[])103 int main(int argc, char *argv[])
104 {
105 char *ptr[NR_CHUNKS_LOW];
106 char **hptr;
107 char *hint;
108 unsigned long i, lchunks, hchunks;
109
110 for (i = 0; i < NR_CHUNKS_LOW; i++) {
111 ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
112 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
113
114 if (ptr[i] == MAP_FAILED) {
115 if (validate_lower_address_hint())
116 return 1;
117 break;
118 }
119
120 if (validate_addr(ptr[i], 0))
121 return 1;
122 }
123 lchunks = i;
124 hptr = (char **) calloc(NR_CHUNKS_HIGH, sizeof(char *));
125 if (hptr == NULL)
126 return 1;
127
128 for (i = 0; i < NR_CHUNKS_HIGH; i++) {
129 hint = hind_addr();
130 hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
131 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
132
133 if (hptr[i] == MAP_FAILED)
134 break;
135
136 if (validate_addr(hptr[i], 1))
137 return 1;
138 }
139 hchunks = i;
140
141 for (i = 0; i < lchunks; i++)
142 munmap(ptr[i], MAP_CHUNK_SIZE);
143
144 for (i = 0; i < hchunks; i++)
145 munmap(hptr[i], MAP_CHUNK_SIZE);
146
147 free(hptr);
148 return 0;
149 }
150