staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / vm / hugepage-mmap.c
blob93f9e7b813314e6637843f46d5042ebf6ddf2481
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * hugepage-mmap:
5 * Example of using huge page memory in a user application using the mmap
6 * system call. Before running this application, make sure that the
7 * administrator has mounted the hugetlbfs filesystem (on some directory
8 * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
9 * example, the app is requesting memory of size 256MB that is backed by
10 * huge pages.
12 * For the ia64 architecture, the Linux kernel reserves Region number 4 for
13 * huge pages. That means that if one requires a fixed address, a huge page
14 * aligned address starting with 0x800000... will be required. If a fixed
15 * address is not required, the kernel will select an address in the proper
16 * range.
17 * Other architectures, such as ppc64, i386 or x86_64 are not so constrained.
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <sys/mman.h>
24 #include <fcntl.h>
26 #define FILE_NAME "huge/hugepagefile"
27 #define LENGTH (256UL*1024*1024)
28 #define PROTECTION (PROT_READ | PROT_WRITE)
30 /* Only ia64 requires this */
31 #ifdef __ia64__
32 #define ADDR (void *)(0x8000000000000000UL)
33 #define FLAGS (MAP_SHARED | MAP_FIXED)
34 #else
35 #define ADDR (void *)(0x0UL)
36 #define FLAGS (MAP_SHARED)
37 #endif
39 static void check_bytes(char *addr)
41 printf("First hex is %x\n", *((unsigned int *)addr));
44 static void write_bytes(char *addr)
46 unsigned long i;
48 for (i = 0; i < LENGTH; i++)
49 *(addr + i) = (char)i;
52 static int read_bytes(char *addr)
54 unsigned long i;
56 check_bytes(addr);
57 for (i = 0; i < LENGTH; i++)
58 if (*(addr + i) != (char)i) {
59 printf("Mismatch at %lu\n", i);
60 return 1;
62 return 0;
65 int main(void)
67 void *addr;
68 int fd, ret;
70 fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
71 if (fd < 0) {
72 perror("Open failed");
73 exit(1);
76 addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
77 if (addr == MAP_FAILED) {
78 perror("mmap");
79 unlink(FILE_NAME);
80 exit(1);
83 printf("Returned address is %p\n", addr);
84 check_bytes(addr);
85 write_bytes(addr);
86 ret = read_bytes(addr);
88 munmap(addr, LENGTH);
89 close(fd);
90 unlink(FILE_NAME);
92 return ret;