virtual_address_range.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2017, Anshuman Khandual, IBM Corp.
  3. * Licensed under GPLv2.
  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. * Maximum address range mapped with a single mmap()
  17. * call is little bit more than 16GB. Hence 16GB is
  18. * chosen as the single chunk size for address space
  19. * mapping.
  20. */
  21. #define MAP_CHUNK_SIZE 17179869184UL /* 16GB */
  22. /*
  23. * Address space till 128TB is mapped without any hint
  24. * and is enabled by default. Address space beyond 128TB
  25. * till 512TB is obtained by passing hint address as the
  26. * first argument into mmap() system call.
  27. *
  28. * The process heap address space is divided into two
  29. * different areas one below 128TB and one above 128TB
  30. * till it reaches 512TB. One with size 128TB and the
  31. * other being 384TB.
  32. *
  33. * On Arm64 the address space is 256TB and no high mappings
  34. * are supported so far.
  35. */
  36. #define NR_CHUNKS_128TB 8192UL /* Number of 16GB chunks for 128TB */
  37. #define NR_CHUNKS_256TB (NR_CHUNKS_128TB * 2UL)
  38. #define NR_CHUNKS_384TB (NR_CHUNKS_128TB * 3UL)
  39. #define ADDR_MARK_128TB (1UL << 47) /* First address beyond 128TB */
  40. #define ADDR_MARK_256TB (1UL << 48) /* First address beyond 256TB */
  41. #ifdef __aarch64__
  42. #define HIGH_ADDR_MARK ADDR_MARK_256TB
  43. #define HIGH_ADDR_SHIFT 49
  44. #define NR_CHUNKS_LOW NR_CHUNKS_256TB
  45. #define NR_CHUNKS_HIGH 0
  46. #else
  47. #define HIGH_ADDR_MARK ADDR_MARK_128TB
  48. #define HIGH_ADDR_SHIFT 48
  49. #define NR_CHUNKS_LOW NR_CHUNKS_128TB
  50. #define NR_CHUNKS_HIGH NR_CHUNKS_384TB
  51. #endif
  52. static char *hind_addr(void)
  53. {
  54. int bits = HIGH_ADDR_SHIFT + rand() % (63 - HIGH_ADDR_SHIFT);
  55. return (char *) (1UL << bits);
  56. }
  57. static int validate_addr(char *ptr, int high_addr)
  58. {
  59. unsigned long addr = (unsigned long) ptr;
  60. if (high_addr) {
  61. if (addr < HIGH_ADDR_MARK) {
  62. printf("Bad address %lx\n", addr);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. if (addr > HIGH_ADDR_MARK) {
  68. printf("Bad address %lx\n", addr);
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. static int validate_lower_address_hint(void)
  74. {
  75. char *ptr;
  76. ptr = mmap((void *) (1UL << 45), MAP_CHUNK_SIZE, PROT_READ |
  77. PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  78. if (ptr == MAP_FAILED)
  79. return 0;
  80. return 1;
  81. }
  82. int main(int argc, char *argv[])
  83. {
  84. char *ptr[NR_CHUNKS_LOW];
  85. char *hptr[NR_CHUNKS_HIGH];
  86. char *hint;
  87. unsigned long i, lchunks, hchunks;
  88. for (i = 0; i < NR_CHUNKS_LOW; i++) {
  89. ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
  90. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  91. if (ptr[i] == MAP_FAILED) {
  92. if (validate_lower_address_hint())
  93. return 1;
  94. break;
  95. }
  96. if (validate_addr(ptr[i], 0))
  97. return 1;
  98. }
  99. lchunks = i;
  100. for (i = 0; i < NR_CHUNKS_HIGH; i++) {
  101. hint = hind_addr();
  102. hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE,
  103. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  104. if (hptr[i] == MAP_FAILED)
  105. break;
  106. if (validate_addr(hptr[i], 1))
  107. return 1;
  108. }
  109. hchunks = i;
  110. for (i = 0; i < lchunks; i++)
  111. munmap(ptr[i], MAP_CHUNK_SIZE);
  112. for (i = 0; i < hchunks; i++)
  113. munmap(hptr[i], MAP_CHUNK_SIZE);
  114. return 0;
  115. }