Debian Codegraph @

tests/mremap-fixed-normal-near-huge.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2009 David Gibson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 page_size, 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(void *target)
55 {
56 void *a, *b;
57 int rc;
58
59 a = mmap(NULL, page_size, PROT_READ|PROT_WRITE,
60 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
61 if (a == MAP_FAILED)
62 FAIL("mmap(normal page): %s", strerror(errno));
63
64 verbose_printf("Normal base mapping at %p\n", a);
65
66 do_readback(a, page_size, "base normal");
67
68 verbose_printf("Attempting mremap(MAYMOVE|FIXED) to %p...", target);
69
70 b = mremap(a, page_size, page_size, MREMAP_MAYMOVE | MREMAP_FIXED,
71 target);
72
73 if (b != MAP_FAILED) {
74 verbose_printf("testing...");
75 do_readback(b, page_size, "remapped");
76 verbose_printf("ok\n");
77 } else {
78 verbose_printf("disallowed (%s)\n", strerror(errno));
79 b = a;
80 }
81
82 rc = munmap(b, page_size);
83 if (rc != 0)
84 FAIL("munmap(after remap): %s", strerror(errno));
85 }
86
87 int main(int argc, char *argv[])
88 {
89 int fd, rc;
90 void *p;
91
92 test_init(argc, argv);
93
94 hpage_size = check_hugepagesize();
95 page_size = getpagesize();
96
97 fd = hugetlbfs_unlinked_fd();
98 if (fd < 0)
99 FAIL("hugetlbfs_unlinked_fd()");
100
101 p = mmap(NULL, 3*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
102 fd, 0);
103 if (p == MAP_FAILED)
104 FAIL("mmap(): %s", strerror(errno));
105
106 rc = munmap(p, hpage_size);
107 if (rc != 0)
108 FAIL("munmap() low hpage: %s", strerror(errno));
109
110 rc = munmap(p + 2*hpage_size, hpage_size);
111 if (rc != 0)
112 FAIL("munmap() high hpage: %s", strerror(errno));
113
114 p = p + hpage_size;
115
116 verbose_printf("Hugepage mapping at %p\n", p);
117
118 do_readback(p, hpage_size, "base hugepage");
119
120 do_remap(p - page_size);
121 do_remap(p + hpage_size);
122
123 PASS();
124 }