Debian Codegraph @

tests/huge_below_4GB_normal_above.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 /*
31 * Test rationale:
32 *
33 * Designed to pick up a bug on ppc64 where
34 * touches_hugepage_low_range() could give false positives because of
35 * the peculiar (undefined) behaviour of << for large shifts
36 *
37 * WARNING: The offsets and addresses used within are specifically
38 * calculated to trigger the bug as it existed. Don't mess with them
39 * unless you *really* know what you're doing.
40 */
41
42 int main(int argc, char *argv[])
43 {
44 int page_size;
45 long hpage_size;
46 int fd;
47 void *p, *q;
48 unsigned long lowaddr, highaddr;
49 int err;
50
51 test_init(argc, argv);
52
53 page_size = getpagesize();
54
55 hpage_size = check_hugepagesize();
56
57 if (sizeof(void *) <= 4)
58 IRRELEVANT();
59
60 if (hpage_size > FOURGB)
61 CONFIG("Huge page size is too large");
62
63 fd = hugetlbfs_unlinked_fd();
64 if (fd < 0)
65 FAIL("hugetlbfs_unlinked_fd()");
66
67
68
69 /* We use a low address right below 4GB so we can test for
70 * off-by-one errors */
71 lowaddr = FOURGB - hpage_size;
72 verbose_printf("Mapping hugepage at at %lx...", lowaddr);
73 p = mmap((void *)lowaddr, hpage_size, PROT_READ|PROT_WRITE,
74 MAP_SHARED|MAP_FIXED, fd, 0);
75 if (p == MAP_FAILED) {
76 /* This is last low slice - 256M just before 4G */
77 unsigned long below_start = FOURGB - 256L*1024*1024;
78 unsigned long above_end = FOURGB;
79 if (range_is_mapped(below_start, above_end) == 1) {
80 verbose_printf("region (4G-256M)-4G is not free\n");
81 verbose_printf("mmap() failed: %s\n", strerror(errno));
82 PASS_INCONCLUSIVE();
83 } else
84 FAIL("mmap() huge: %s\n", strerror(errno));
85 }
86 if (p != (void *)lowaddr)
87 FAIL("Wrong address with MAP_FIXED huge");
88 verbose_printf("done\n");
89
90 memset(p, 0, hpage_size);
91
92 err = test_addr_huge(p);
93 if (err != 1)
94 FAIL("Mapped address is not hugepage");
95
96 /* Test for off by one errors */
97 highaddr = FOURGB;
98 verbose_printf("Mapping normal page at %lx...", highaddr);
99 q = mmap((void *)highaddr, page_size, PROT_READ|PROT_WRITE,
100 MAP_SHARED|MAP_FIXED|MAP_ANONYMOUS, 0, 0);
101 if (p == MAP_FAILED) {
102 unsigned long below_start = FOURGB;
103 unsigned long above_end = FOURGB + page_size;
104 if (range_is_mapped(below_start, above_end) == 1) {
105 verbose_printf("region 4G-(4G+page) is not free\n");
106 verbose_printf("mmap() failed: %s\n", strerror(errno));
107 PASS_INCONCLUSIVE();
108 } else
109 FAIL("mmap() normal 1: %s\n", strerror(errno));
110 }
111 if (q != (void *)highaddr)
112 FAIL("Wrong address with MAP_FIXED normal 2");
113 verbose_printf("done\n");
114
115 memset(q, 0, page_size);
116
117 /* Why this address? Well on ppc64, we're working with 256MB
118 * segment numbers, hence >>28. In practice the shift
119 * instructions only start wrapping around with shifts 128 or
120 * greater. */
121 highaddr = ((lowaddr >> 28) + 128) << 28;
122 verbose_printf("Mapping normal page at %lx...", highaddr);
123 q = mmap((void *)highaddr, page_size, PROT_READ|PROT_WRITE,
124 MAP_SHARED|MAP_FIXED|MAP_ANONYMOUS, 0, 0);
125 if (p == MAP_FAILED) {
126 unsigned long below_start = highaddr;
127 unsigned long above_end = highaddr + page_size;
128 if (range_is_mapped(below_start, above_end) == 1) {
129 verbose_printf("region haddr-(haddr+page) not free\n");
130 verbose_printf("mmap() failed: %s\n", strerror(errno));
131 PASS_INCONCLUSIVE();
132 } else
133 FAIL("mmap() normal 2: %s\n", strerror(errno));
134 }
135 if (q != (void *)highaddr)
136 FAIL("Wrong address with MAP_FIXED normal 2");
137 verbose_printf("done\n");
138
139 memset(q, 0, page_size);
140
141 PASS();
142 }