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 19 #define LENGTH (256UL*1024*1024) 20 #define PROTECTION (PROT_READ | PROT_WRITE) 21 22 /* Only ia64 requires this */ 23 #ifdef __ia64__ 24 #define ADDR (void *)(0x8000000000000000UL) 25 #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED) 26 #else 27 #define ADDR (void *)(0x0UL) 28 #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB) 29 #endif 30 31 static void check_bytes(char *addr) 32 { 33 printf("First hex is %x\n", *((unsigned int *)addr)); 34 } 35 36 static void write_bytes(char *addr, size_t length) 37 { 38 unsigned long i; 39 40 for (i = 0; i < length; i++) 41 *(addr + i) = (char)i; 42 } 43 44 static int read_bytes(char *addr, size_t length) 45 { 46 unsigned long i; 47 48 check_bytes(addr); 49 for (i = 0; i < length; i++) 50 if (*(addr + i) != (char)i) { 51 printf("Mismatch at %lu\n", i); 52 return 1; 53 } 54 return 0; 55 } 56 57 int main(int argc, char **argv) 58 { 59 void *addr; 60 int ret; 61 size_t length = LENGTH; 62 int flags = FLAGS; 63 int shift = 0; 64 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 73 if (shift) 74 printf("%u kB hugepages\n", 1 << (shift - 10)); 75 else 76 printf("Default size hugepages\n"); 77 printf("Mapping %lu Mbytes\n", (unsigned long)length >> 20); 78 79 addr = mmap(ADDR, length, PROTECTION, flags, -1, 0); 80 if (addr == MAP_FAILED) { 81 perror("mmap"); 82 exit(1); 83 } 84 85 printf("Returned address is %p\n", addr); 86 check_bytes(addr); 87 write_bytes(addr, length); 88 ret = read_bytes(addr, length); 89 90 /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */ 91 if (munmap(addr, length)) { 92 perror("munmap"); 93 exit(1); 94 } 95 96 return ret; 97 } 98