• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "vm_util.h"
19 
20 #define LENGTH (256UL*1024*1024)
21 #define PROTECTION (PROT_READ | PROT_WRITE)
22 
23 #ifndef MAP_HUGETLB
24 #define MAP_HUGETLB 0x40000 /* arch specific */
25 #endif
26 
27 #ifndef MAP_HUGE_SHIFT
28 #define MAP_HUGE_SHIFT 26
29 #endif
30 
31 #ifndef MAP_HUGE_MASK
32 #define MAP_HUGE_MASK 0x3f
33 #endif
34 
35 /* Only ia64 requires this */
36 #ifdef __ia64__
37 #define ADDR (void *)(0x8000000000000000UL)
38 #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED)
39 #else
40 #define ADDR (void *)(0x0UL)
41 #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
42 #endif
43 
check_bytes(char * addr)44 static void check_bytes(char *addr)
45 {
46 	printf("First hex is %x\n", *((unsigned int *)addr));
47 }
48 
write_bytes(char * addr,size_t length)49 static void write_bytes(char *addr, size_t length)
50 {
51 	unsigned long i;
52 
53 	for (i = 0; i < length; i++)
54 		*(addr + i) = (char)i;
55 }
56 
read_bytes(char * addr,size_t length)57 static int read_bytes(char *addr, size_t length)
58 {
59 	unsigned long i;
60 
61 	check_bytes(addr);
62 	for (i = 0; i < length; i++)
63 		if (*(addr + i) != (char)i) {
64 			printf("Mismatch at %lu\n", i);
65 			return 1;
66 		}
67 	return 0;
68 }
69 
main(int argc,char ** argv)70 int main(int argc, char **argv)
71 {
72 	void *addr;
73 	int ret;
74 	size_t hugepage_size;
75 	size_t length = LENGTH;
76 	int flags = FLAGS;
77 	int shift = 0;
78 
79 	hugepage_size = default_huge_page_size();
80 	/* munmap with fail if the length is not page aligned */
81 	if (hugepage_size > length)
82 		length = hugepage_size;
83 
84 	if (argc > 1)
85 		length = atol(argv[1]) << 20;
86 	if (argc > 2) {
87 		shift = atoi(argv[2]);
88 		if (shift)
89 			flags |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
90 	}
91 
92 	if (shift)
93 		printf("%u kB hugepages\n", 1 << (shift - 10));
94 	else
95 		printf("Default size hugepages\n");
96 	printf("Mapping %lu Mbytes\n", (unsigned long)length >> 20);
97 
98 	addr = mmap(ADDR, length, PROTECTION, flags, -1, 0);
99 	if (addr == MAP_FAILED) {
100 		perror("mmap");
101 		exit(1);
102 	}
103 
104 	printf("Returned address is %p\n", addr);
105 	check_bytes(addr);
106 	write_bytes(addr, length);
107 	ret = read_bytes(addr, length);
108 
109 	/* munmap() length of MAP_HUGETLB memory must be hugepage aligned */
110 	if (munmap(addr, length)) {
111 		perror("munmap");
112 		exit(1);
113 	}
114 
115 	return ret;
116 }
117