| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 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); |
| 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 |
} |