~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/vm/map_hugetlb.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp