Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / Documentation / vm / hugepage-mmap.c
blobdb0dd9a33d549387d301790af8339d3d1b106264
1 /*
2 * hugepage-mmap:
4 * Example of using huge page memory in a user application using the mmap
5 * system call. Before running this application, make sure that the
6 * administrator has mounted the hugetlbfs filesystem (on some directory
7 * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
8 * example, the app is requesting memory of size 256MB that is backed by
9 * huge pages.
11 * For the ia64 architecture, the Linux kernel reserves Region number 4 for
12 * huge pages. That means that if one requires a fixed address, a huge page
13 * aligned address starting with 0x800000... will be required. If a fixed
14 * address is not required, the kernel will select an address in the proper
15 * range.
16 * Other architectures, such as ppc64, i386 or x86_64 are not so constrained.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <fcntl.h>
25 #define FILE_NAME "/mnt/hugepagefile"
26 #define LENGTH (256UL*1024*1024)
27 #define PROTECTION (PROT_READ | PROT_WRITE)
29 /* Only ia64 requires this */
30 #ifdef __ia64__
31 #define ADDR (void *)(0x8000000000000000UL)
32 #define FLAGS (MAP_SHARED | MAP_FIXED)
33 #else
34 #define ADDR (void *)(0x0UL)
35 #define FLAGS (MAP_SHARED)
36 #endif
38 static void check_bytes(char *addr)
40 printf("First hex is %x\n", *((unsigned int *)addr));
43 static void write_bytes(char *addr)
45 unsigned long i;
47 for (i = 0; i < LENGTH; i++)
48 *(addr + i) = (char)i;
51 static void read_bytes(char *addr)
53 unsigned long i;
55 check_bytes(addr);
56 for (i = 0; i < LENGTH; i++)
57 if (*(addr + i) != (char)i) {
58 printf("Mismatch at %lu\n", i);
59 break;
63 int main(void)
65 void *addr;
66 int fd;
68 fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
69 if (fd < 0) {
70 perror("Open failed");
71 exit(1);
74 addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
75 if (addr == MAP_FAILED) {
76 perror("mmap");
77 unlink(FILE_NAME);
78 exit(1);
81 printf("Returned address is %p\n", addr);
82 check_bytes(addr);
83 write_bytes(addr);
84 read_bytes(addr);
86 munmap(addr, LENGTH);
87 close(fd);
88 unlink(FILE_NAME);
90 return 0;