Debian Codegraph @

tests/straddle_4GB.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, 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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25
26 #include <hugetlbfs.h>
27
28 #include "hugetests.h"
29
30 int main(int argc, char *argv[])
31 {
32 long hpage_size;
33 int fd;
34 void *p;
35 unsigned long straddle_addr;
36
37 test_init(argc, argv);
38
39 hpage_size = check_hugepagesize();
40
41 if (sizeof(void *) <= 4)
42 TEST_BUG("64-bit only");
43
44 if (hpage_size > FOURGB)
45 CONFIG("Huge page size too large");
46
47 fd = hugetlbfs_unlinked_fd();
48 if (fd < 0)
49 FAIL("hugetlbfs_unlinked_fd()");
50
51 straddle_addr = FOURGB - hpage_size;
52
53 /* We first try to get the mapping without MAP_FIXED */
54 verbose_printf("Mapping without MAP_FIXED at %lx...", straddle_addr);
55 p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
56 MAP_SHARED, fd, 0);
57 if (p == (void *)straddle_addr) {
58 /* These tests irrelevant if we didn't get the
59 * straddle address */
60 verbose_printf("done\n");
61
62 if (test_addr_huge(p) != 1)
63 FAIL("Mapped address is not hugepage");
64
65 if (test_addr_huge(p + hpage_size) != 1)
66 FAIL("Mapped address is not hugepage");
67
68 verbose_printf("Clearing below 4GB...");
69 memset(p, 0, hpage_size);
70 verbose_printf("done\n");
71
72 verbose_printf("Clearing above 4GB...");
73 memset(p + hpage_size, 0, hpage_size);
74 verbose_printf("done\n");
75 } else {
76 verbose_printf("got %p instead, never mind\n", p);
77 munmap(p, 2*hpage_size);
78 }
79
80 verbose_printf("Mapping with MAP_FIXED at %lx...", straddle_addr);
81 p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
82 MAP_SHARED|MAP_FIXED, fd, 0);
83 if (p == MAP_FAILED) {
84 /* this area crosses last low slice and first high slice */
85 unsigned long below_start = FOURGB - 256L*1024*1024;
86 unsigned long above_end = 1024L*1024*1024*1024;
87 if (range_is_mapped(below_start, above_end) == 1) {
88 verbose_printf("region (4G-256M)-1T is not free\n");
89 verbose_printf("mmap() failed: %s\n", strerror(errno));
90 PASS_INCONCLUSIVE();
91 } else
92 FAIL("mmap() FIXED failed: %s\n", strerror(errno));
93 }
94 if (p != (void *)straddle_addr) {
95 verbose_printf("got %p instead\n", p);
96 FAIL("Wrong address with MAP_FIXED");
97 }
98 verbose_printf("done\n");
99
100 if (test_addr_huge(p) != 1)
101 FAIL("Mapped address is not hugepage");
102
103 if (test_addr_huge(p + hpage_size) != 1)
104 FAIL("Mapped address is not hugepage");
105
106 verbose_printf("Clearing below 4GB...");
107 memset(p, 0, hpage_size);
108 verbose_printf("done\n");
109
110 verbose_printf("Clearing above 4GB...");
111 memset(p + hpage_size, 0, hpage_size);
112 verbose_printf("done\n");
113
114 verbose_printf("Tested above 4GB\n");
115
116 PASS();
117 }