2 * Copyright 2017, Anshuman Khandual, IBM Corp.
3 * Licensed under GPLv2.
5 * Works on architectures which support 128TB virtual
6 * address range and beyond.
18 * Maximum address range mapped with a single mmap()
19 * call is little bit more than 16GB. Hence 16GB is
20 * chosen as the single chunk size for address space
23 #define MAP_CHUNK_SIZE 17179869184UL /* 16GB */
26 * Address space till 128TB is mapped without any hint
27 * and is enabled by default. Address space beyond 128TB
28 * till 512TB is obtained by passing hint address as the
29 * first argument into mmap() system call.
31 * The process heap address space is divided into two
32 * different areas one below 128TB and one above 128TB
33 * till it reaches 512TB. One with size 128TB and the
36 #define NR_CHUNKS_128TB 8192UL /* Number of 16GB chunks for 128TB */
37 #define NR_CHUNKS_384TB 24576UL /* Number of 16GB chunks for 384TB */
39 #define ADDR_MARK_128TB (1UL << 47) /* First address beyond 128TB */
41 static char *hind_addr(void)
43 int bits = 48 + rand() % 15;
45 return (char *) (1UL << bits);
48 static int validate_addr(char *ptr, int high_addr)
50 unsigned long addr = (unsigned long) ptr;
53 if (addr < ADDR_MARK_128TB) {
54 printf("Bad address %lx\n", addr);
60 if (addr > ADDR_MARK_128TB) {
61 printf("Bad address %lx\n", addr);
67 static int validate_lower_address_hint(void)
71 ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
72 PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
74 if (ptr == MAP_FAILED)
80 int main(int argc, char *argv[])
82 char *ptr[NR_CHUNKS_128TB];
83 char *hptr[NR_CHUNKS_384TB];
85 unsigned long i, lchunks, hchunks;
87 for (i = 0; i < NR_CHUNKS_128TB; i++) {
88 ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
89 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
91 if (ptr[i] == MAP_FAILED) {
92 if (validate_lower_address_hint())
97 if (validate_addr(ptr[i], 0))
102 for (i = 0; i < NR_CHUNKS_384TB; i++) {
104 hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
105 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
107 if (hptr[i] == MAP_FAILED)
110 if (validate_addr(hptr[i], 1))
115 for (i = 0; i < lchunks; i++)
116 munmap(ptr[i], MAP_CHUNK_SIZE);
118 for (i = 0; i < hchunks; i++)
119 munmap(hptr[i], MAP_CHUNK_SIZE);