Debian Codegraph @

tests/huge_at_4GB_normal_below.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_high_range() falsely reported true for ranges
35 * reaching below 4GB
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;
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 p = mmap((void *)FOURGB, hpage_size, PROT_READ|PROT_WRITE,
68 MAP_SHARED | MAP_FIXED, fd, 0);
69 if (p == MAP_FAILED) {
70 /* slice 0 (high) spans from 4G-1T */
71 unsigned long below_start = FOURGB;
72 unsigned long above_end = 1024L*1024*1024*1024;
73 if (range_is_mapped(below_start, above_end) == 1) {
74 verbose_printf("region 4G-1T is not free\n");
75 verbose_printf("mmap() failed: %s\n", strerror(errno));
76 PASS_INCONCLUSIVE();
77 } else
78 FAIL("mmap() huge: %s\n", strerror(errno));
79 }
80 if (p != (void *)FOURGB)
81 FAIL("Wrong address with MAP_FIXED huge");
82
83 verbose_printf("Mapped hugetlb at %p\n", p);
84
85 memset(p, 0, hpage_size);
86
87 err = test_addr_huge(p);
88 if (err != 1)
89 FAIL("Mapped address is not hugepage");
90
91 /* Test just below 4GB to check for off-by-one errors */
92 lowaddr = FOURGB - page_size;
93 q = mmap((void *)lowaddr, page_size, PROT_READ|PROT_WRITE,
94 MAP_SHARED|MAP_FIXED|MAP_ANONYMOUS, 0, 0);
95 if (p == MAP_FAILED) {
96 unsigned long below_start = FOURGB - page_size;
97 unsigned long above_end = FOURGB;
98 if (range_is_mapped(below_start, above_end) == 1) {
99 verbose_printf("region (4G-page)-4G is not free\n");
100 verbose_printf("mmap() failed: %s\n", strerror(errno));
101 PASS_INCONCLUSIVE();
102 } else
103 FAIL("mmap() normal: %s\n", strerror(errno));
104 }
105 if (q != (void *)lowaddr)
106 FAIL("Wrong address with MAP_FIXED normal");
107
108 memset(q, 0, page_size);
109
110 PASS();
111 }