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