map_hugetlb.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Example of using hugepage memory in a user application using the mmap
  4. * system call with MAP_HUGETLB flag. Before running this program make
  5. * sure the administrator has allocated enough default sized huge pages
  6. * to cover the 256 MB allocation.
  7. *
  8. * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages.
  9. * That means the addresses starting with 0x800000... will need to be
  10. * specified. Specifying a fixed address is not required on ppc64, i386
  11. * or x86_64.
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <sys/mman.h>
  17. #include <fcntl.h>
  18. #define LENGTH (256UL*1024*1024)
  19. #define PROTECTION (PROT_READ | PROT_WRITE)
  20. #ifndef MAP_HUGETLB
  21. #define MAP_HUGETLB 0x40000 /* arch specific */
  22. #endif
  23. #ifndef MAP_HUGE_SHIFT
  24. #define MAP_HUGE_SHIFT 26
  25. #endif
  26. #ifndef MAP_HUGE_MASK
  27. #define MAP_HUGE_MASK 0x3f
  28. #endif
  29. /* Only ia64 requires this */
  30. #ifdef __ia64__
  31. #define ADDR (void *)(0x8000000000000000UL)
  32. #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED)
  33. #else
  34. #define ADDR (void *)(0x0UL)
  35. #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
  36. #endif
  37. static void check_bytes(char *addr)
  38. {
  39. printf("First hex is %x\n", *((unsigned int *)addr));
  40. }
  41. static void write_bytes(char *addr, size_t length)
  42. {
  43. unsigned long i;
  44. for (i = 0; i < length; i++)
  45. *(addr + i) = (char)i;
  46. }
  47. static int read_bytes(char *addr, size_t length)
  48. {
  49. unsigned long i;
  50. check_bytes(addr);
  51. for (i = 0; i < length; i++)
  52. if (*(addr + i) != (char)i) {
  53. printf("Mismatch at %lu\n", i);
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. int main(int argc, char **argv)
  59. {
  60. void *addr;
  61. int ret;
  62. size_t length = LENGTH;
  63. int flags = FLAGS;
  64. int shift = 0;
  65. if (argc > 1)
  66. length = atol(argv[1]) << 20;
  67. if (argc > 2) {
  68. shift = atoi(argv[2]);
  69. if (shift)
  70. flags |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
  71. }
  72. if (shift)
  73. printf("%u kB hugepages\n", 1 << (shift - 10));
  74. else
  75. printf("Default size hugepages\n");
  76. printf("Mapping %lu Mbytes\n", (unsigned long)length >> 20);
  77. addr = mmap(ADDR, length, PROTECTION, flags, -1, 0);
  78. if (addr == MAP_FAILED) {
  79. perror("mmap");
  80. exit(1);
  81. }
  82. printf("Returned address is %p\n", addr);
  83. check_bytes(addr);
  84. write_bytes(addr, length);
  85. ret = read_bytes(addr, length);
  86. /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */
  87. if (munmap(addr, length)) {
  88. perror("munmap");
  89. exit(1);
  90. }
  91. return ret;
  92. }