| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <sys/mman.h> |
| 26 |
|
| 27 |
#include <hugetlbfs.h> |
| 28 |
|
| 29 |
#include "hugetests.h" |
| 30 |
|
| 31 |
#define RANDOM_CONSTANT 0x1234ABCD |
| 32 |
|
| 33 |
long hpage_size; |
| 34 |
|
| 35 |
void do_readback(void *p, size_t size, const char *stage) |
| 36 |
{ |
| 37 |
unsigned int *q = p; |
| 38 |
int i; |
| 39 |
|
| 40 |
verbose_printf("do_readback(%p, 0x%lx, \"%s\")\n", p, |
| 41 |
(unsigned long)size, stage); |
| 42 |
|
| 43 |
for (i = 0; i < (size / sizeof(*q)); i++) { |
| 44 |
q[i] = RANDOM_CONSTANT ^ i; |
| 45 |
} |
| 46 |
|
| 47 |
for (i = 0; i < (size / sizeof(*q)); i++) { |
| 48 |
if (q[i] != (RANDOM_CONSTANT ^ i)) |
| 49 |
FAIL("Stage \"%s\": Mismatch at offset 0x%x: 0x%x instead of 0x%x", |
| 50 |
stage, i, q[i], RANDOM_CONSTANT ^ i); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void do_remap(int fd, void *target) |
| 55 |
{ |
| 56 |
void *a, *b; |
| 57 |
int rc; |
| 58 |
|
| 59 |
a = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 60 |
if (a == MAP_FAILED) |
| 61 |
FAIL("mmap(huge page): %s", strerror(errno)); |
| 62 |
|
| 63 |
verbose_printf("Huge base mapping at %p\n", a); |
| 64 |
|
| 65 |
do_readback(a, hpage_size, "base huge"); |
| 66 |
|
| 67 |
verbose_printf("Attempting mremap(MAYMOVE|FIXED) to %p...", target); |
| 68 |
|
| 69 |
b = mremap(a, hpage_size, hpage_size, MREMAP_MAYMOVE | MREMAP_FIXED, |
| 70 |
target); |
| 71 |
|
| 72 |
if (b != MAP_FAILED) { |
| 73 |
verbose_printf("testing..."); |
| 74 |
do_readback(b, hpage_size, "remapped"); |
| 75 |
verbose_printf("ok\n"); |
| 76 |
|
| 77 |
} else { |
| 78 |
verbose_printf("disallowed (%s)\n", strerror(errno)); |
| 79 |
b = a; |
| 80 |
} |
| 81 |
|
| 82 |
rc = munmap(b, hpage_size); |
| 83 |
if (rc != 0) |
| 84 |
FAIL("munmap(after remap): %s", strerror(errno)); |
| 85 |
} |
| 86 |
|
| 87 |
void *map_align(size_t size, size_t align) |
| 88 |
{ |
| 89 |
unsigned long xsize = size + align - getpagesize(); |
| 90 |
size_t t; |
| 91 |
void *p, *q; |
| 92 |
int rc; |
| 93 |
|
| 94 |
p = mmap(NULL, xsize, PROT_READ|PROT_WRITE, |
| 95 |
MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 96 |
if (p == MAP_FAILED) |
| 97 |
FAIL("mmap(): %s", strerror(errno)); |
| 98 |
|
| 99 |
q = PALIGN(p, align); |
| 100 |
|
| 101 |
t = q - p; |
| 102 |
if (t) { |
| 103 |
rc = munmap(p, t); |
| 104 |
if (rc != 0) |
| 105 |
FAIL("munmap(lower aligning): %s", strerror(errno)); |
| 106 |
} |
| 107 |
|
| 108 |
t = p + xsize - (q + size); |
| 109 |
if (t) { |
| 110 |
rc = munmap(q + size, t); |
| 111 |
if (rc != 0) |
| 112 |
FAIL("munmap(upper aligning): %s", strerror(errno)); |
| 113 |
} |
| 114 |
|
| 115 |
return q; |
| 116 |
} |
| 117 |
|
| 118 |
int main(int argc, char *argv[]) |
| 119 |
{ |
| 120 |
int fd, rc; |
| 121 |
void *p; |
| 122 |
|
| 123 |
test_init(argc, argv); |
| 124 |
|
| 125 |
hpage_size = check_hugepagesize(); |
| 126 |
|
| 127 |
fd = hugetlbfs_unlinked_fd(); |
| 128 |
if (fd < 0) |
| 129 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 130 |
|
| 131 |
p = map_align(3*hpage_size, hpage_size); |
| 132 |
|
| 133 |
rc = munmap(p, hpage_size); |
| 134 |
if (rc != 0) |
| 135 |
FAIL("munmap() low portion: %s", strerror(errno)); |
| 136 |
|
| 137 |
rc = munmap(p + 2*hpage_size, hpage_size); |
| 138 |
if (rc != 0) |
| 139 |
FAIL("munmap() high portion: %s", strerror(errno)); |
| 140 |
|
| 141 |
p = p + hpage_size; |
| 142 |
|
| 143 |
verbose_printf("Normal mapping at %p\n", p); |
| 144 |
|
| 145 |
do_readback(p, hpage_size, "base normal page"); |
| 146 |
|
| 147 |
do_remap(fd, p - hpage_size); |
| 148 |
do_remap(fd, p + hpage_size); |
| 149 |
|
| 150 |
PASS(); |
| 151 |
} |