Root/
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 void 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 | break; |
56 | } |
57 | } |
58 | |
59 | int main(void) |
60 | { |
61 | void *addr; |
62 | |
63 | addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0); |
64 | if (addr == MAP_FAILED) { |
65 | perror("mmap"); |
66 | exit(1); |
67 | } |
68 | |
69 | printf("Returned address is %p\n", addr); |
70 | check_bytes(addr); |
71 | write_bytes(addr); |
72 | read_bytes(addr); |
73 | |
74 | munmap(addr, LENGTH); |
75 | |
76 | return 0; |
77 | } |
78 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9