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