1 // SPDX-License-Identifier: GPL-2.0
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.
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
19 #define LENGTH (256UL*1024*1024)
20 #define PROTECTION (PROT_READ | PROT_WRITE)
23 #define MAP_HUGETLB 0x40000 /* arch specific */
26 /* Only ia64 requires this */
28 #define ADDR (void *)(0x8000000000000000UL)
29 #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED)
31 #define ADDR (void *)(0x0UL)
32 #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
35 static void check_bytes(char *addr)
37 printf("First hex is %x\n", *((unsigned int *)addr));
40 static void write_bytes(char *addr)
44 for (i = 0; i < LENGTH; i++)
45 *(addr + i) = (char)i;
48 static int read_bytes(char *addr)
53 for (i = 0; i < LENGTH; i++)
54 if (*(addr + i) != (char)i) {
55 printf("Mismatch at %lu\n", i);
66 addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, -1, 0);
67 if (addr == MAP_FAILED) {
72 printf("Returned address is %p\n", addr);
75 ret = read_bytes(addr);
77 /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */
78 if (munmap(addr, LENGTH)) {