Debian Codegraph @

tests/icache-hygiene.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 /* Test rationale:
21 *
22 * Older ppc64 kernels don't properly flush dcache to icache before
23 * giving a cleared page to userspace. With some exceedingly hairy
24 * code, this attempts to test for this bug.
25 *
26 * This test will never trigger (obviously) on machines with coherent
27 * icache and dcache (including x86 and POWER5). On any given run,
28 * even on a buggy kernel there's a chance the bug won't trigger -
29 * either because we don't get the same physical page back when we
30 * remap, or because the icache happens to get flushed in the interim.
31 */
32
33 #define _GNU_SOURCE
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <setjmp.h>
39 #include <unistd.h>
40 #include <signal.h>
41 #include <sys/mman.h>
42 #include <ucontext.h>
43
44 #include <hugetlbfs.h>
45
46 #include "hugetests.h"
47
48 #define COPY_SIZE 128
49 #define NUM_REPETITIONS 64 /* Seems to be enough to trigger reliably */
50
51 static long hpage_size;
52
53 static void cacheflush(void *p)
54 {
55 #if defined(__powerpc__)
56 asm volatile("dcbst 0,%0; sync; icbi 0,%0; isync" : : "r"(p));
57 #elif defined(__arm__) || defined(__aarch64__)
58 __clear_cache(p, p + COPY_SIZE);
59 #endif
60 }
61
62 static void jumpfunc(int copy, void *p)
63 {
64 /* gcc bug workaround: if there is exactly one &&label
65 * construct in the function, gcc assumes the computed goto
66 * goes there, leading to the complete elision of the goto in
67 * this case */
68 void *l = &&dummy;
69 l = &&jumplabel;
70
71 if (copy) {
72 memcpy(p, l, COPY_SIZE);
73 cacheflush(p);
74 }
75
76 goto *p;
77 dummy:
78 printf("unreachable?\n");
79
80 jumplabel:
81 return;
82 }
83
84 static sigjmp_buf sig_escape;
85 static void *sig_expected;
86
87 static void sig_handler(int signum, siginfo_t *si, void *uc)
88 {
89 #if defined(__powerpc__) || defined(__powerpc64__) || defined(__ia64__) || \
90 defined(__s390__) || defined(__s390x__) || defined(__sparc__) || \
91 defined(__aarch64__) || (defined(__riscv) && __riscv_xlen == 64 || __loongarch64)
92 /* On powerpc, ia64, s390 and Aarch64, 0 bytes are an illegal
93 * instruction, so, if the icache is cleared properly, we SIGILL
94 * as soon as we jump into the cleared page */
95 if (signum == SIGILL) {
96 verbose_printf("SIGILL at %p (sig_expected=%p)\n", si->si_addr,
97 sig_expected);
98 if (si->si_addr == sig_expected) {
99 siglongjmp(sig_escape, 1);
100 }
101 FAIL("SIGILL somewhere unexpected");
102 }
103 #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__)
104 /* On x86, zero bytes form a valid instruction:
105 * add %al,(%eax) (i386)
106 * or add %al,(%rax) (x86_64)
107 *
108 * So, behaviour depends on the contents of [ER]AX, which in
109 * turn depends on the details of code generation. If [ER]AX
110 * contains a valid pointer, we will execute the instruction
111 * repeatedly until we run off that hugepage and get a SIGBUS
112 * on the second, truncated page. If [ER]AX does not contain
113 * a valid pointer, we will SEGV on the first instruction in
114 * the cleared page. We check for both possibilities
115 * below.
116 *
117 * On 32 bit ARM, zero bytes are interpreted as follows:
118 * andeq r0, r0, r0 (ARM state, 4 bytes)
119 * movs r0, r0 (Thumb state, 2 bytes)
120 *
121 * So, we only expect to run off the end of the huge page and
122 * generate a SIGBUS. */
123 if (signum == SIGBUS) {
124 verbose_printf("SIGBUS at %p (sig_expected=%p)\n", si->si_addr,
125 sig_expected);
126 if (sig_expected
127 && (ALIGN((unsigned long)sig_expected, gethugepagesize())
128 == (unsigned long)si->si_addr)) {
129 siglongjmp(sig_escape, 2);
130 }
131 FAIL("SIGBUS somewhere unexpected");
132 }
133 #if defined(__x86_64__) || defined(__i386__)
134 if (signum == SIGSEGV) {
135 #ifdef __x86_64__
136 void *pc = (void *)((ucontext_t *)uc)->uc_mcontext.gregs[REG_RIP];
137 #else
138 void *pc = (void *)((ucontext_t *)uc)->uc_mcontext.gregs[REG_EIP];
139 #endif
140
141 verbose_printf("SIGSEGV at %p, PC=%p (sig_expected=%p)\n",
142 si->si_addr, pc, sig_expected);
143 if (sig_expected == pc) {
144 siglongjmp(sig_escape, 1);
145 }
146 FAIL("SIGSEGV somewhere unexpected");
147 }
148 #endif
149 #else
150 #error Need to setup signal conditions for this arch
151 #endif
152 }
153
154 static void test_once(int fd)
155 {
156 void *p, *q;
157
158 if (ftruncate(fd, 0) < 0)
159 FAIL("ftruncate failed: %s", strerror(errno));
160
161 if (sigsetjmp(sig_escape, 1)) {
162 sig_expected = NULL;
163 if (ftruncate(fd, 0) < 0)
164 FAIL("ftruncate failed: %s", strerror(errno));
165 return;
166 }
167
168 p = mmap(NULL, 2*hpage_size, PROT_READ|PROT_WRITE|PROT_EXEC,
169 MAP_SHARED, fd, 0);
170 if (p == MAP_FAILED)
171 FAIL("mmap() 1: %s", strerror(errno));
172
173 if (ftruncate(fd, hpage_size) < 0)
174 FAIL("ftruncate failed: %s", strerror(errno));
175
176 q = p + hpage_size - COPY_SIZE;
177
178 jumpfunc(1, q);
179
180 if (ftruncate(fd, 0) < 0)
181 FAIL("ftruncate failed: %s", strerror(errno));
182 p = mmap(p, hpage_size, PROT_READ|PROT_WRITE|PROT_EXEC,
183 MAP_SHARED|MAP_FIXED, fd, 0);
184 if (p == MAP_FAILED)
185 FAIL("mmap() 2: %s", strerror(errno));
186
187 q = p + hpage_size - COPY_SIZE;
188 sig_expected = q;
189
190 jumpfunc(0, q); /* This should blow up */
191
192 FAIL("icache unclean");
193 }
194
195 int main(int argc, char *argv[])
196 {
197 int fd;
198 int err;
199 int i;
200
201 test_init(argc, argv);
202
203 struct sigaction sa = {
204 .sa_sigaction = sig_handler,
205 .sa_flags = SA_SIGINFO,
206 };
207
208 hpage_size = check_hugepagesize();
209
210 err = sigaction(SIGILL, &sa, NULL);
211 if (err)
212 FAIL("Can't install SIGILL handler: %s", strerror(errno));
213
214 err = sigaction(SIGBUS, &sa, NULL);
215 if (err)
216 FAIL("Can't install SIGBUS handler: %s", strerror(errno));
217
218 err = sigaction(SIGSEGV, &sa, NULL);
219 if (err)
220 FAIL("Can't install SIGSEGV handler: %s", strerror(errno));
221
222 fd = hugetlbfs_unlinked_fd();
223 if (fd < 0)
224 CONFIG("Couldn't get hugepage fd");
225
226 for (i = 0; i < NUM_REPETITIONS; i++)
227 test_once(fd);
228
229 PASS();
230 }