1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2019 ARM Limited */ 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <signal.h> 7 #include <string.h> 8 #include <unistd.h> 9 #include <assert.h> 10 #include <sys/auxv.h> 11 #include <linux/auxvec.h> 12 #include <ucontext.h> 13 14 #include <asm/unistd.h> 15 16 #include <kselftest.h> 17 18 #include "test_signals.h" 19 #include "test_signals_utils.h" 20 #include "testcases/testcases.h" 21 22 23 extern struct tdescr *current; 24 25 static int sig_copyctx = SIGTRAP; 26 27 static char const *const feats_names[FMAX_END] = { 28 " SSBS ", 29 " SVE ", 30 " SME ", 31 " FA64 ", 32 " SME2 ", 33 " GCS ", 34 }; 35 36 #define MAX_FEATS_SZ 128 37 static char feats_string[MAX_FEATS_SZ]; 38 39 static inline char *feats_to_string(unsigned long feats) 40 { 41 size_t flen = MAX_FEATS_SZ - 1; 42 43 feats_string[0] = '\0'; 44 45 for (int i = 0; i < FMAX_END; i++) { 46 if (feats & (1UL << i)) { 47 size_t tlen = strlen(feats_names[i]); 48 49 assert(flen > tlen); 50 flen -= tlen; 51 strncat(feats_string, feats_names[i], flen); 52 } 53 } 54 55 return feats_string; 56 } 57 58 static void unblock_signal(int signum) 59 { 60 sigset_t sset; 61 62 sigemptyset(&sset); 63 sigaddset(&sset, signum); 64 sigprocmask(SIG_UNBLOCK, &sset, NULL); 65 } 66 67 static void default_result(struct tdescr *td, bool force_exit) 68 { 69 if (td->result == KSFT_SKIP) { 70 fprintf(stderr, "==>> completed. SKIP.\n"); 71 } else if (td->pass) { 72 fprintf(stderr, "==>> completed. PASS(1)\n"); 73 td->result = KSFT_PASS; 74 } else { 75 fprintf(stdout, "==>> completed. FAIL(0)\n"); 76 td->result = KSFT_FAIL; 77 } 78 79 if (force_exit) 80 exit(td->result); 81 } 82 83 /* 84 * The following handle_signal_* helpers are used by main default_handler 85 * and are meant to return true when signal is handled successfully: 86 * when false is returned instead, it means that the signal was somehow 87 * unexpected in that context and it was NOT handled; default_handler will 88 * take care of such unexpected situations. 89 */ 90 91 static bool handle_signal_unsupported(struct tdescr *td, 92 siginfo_t *si, void *uc) 93 { 94 if (feats_ok(td)) 95 return false; 96 97 /* Mangling PC to avoid loops on original SIGILL */ 98 ((ucontext_t *)uc)->uc_mcontext.pc += 4; 99 100 if (!td->initialized) { 101 fprintf(stderr, 102 "Got SIG_UNSUPP @test_init. Ignore.\n"); 103 } else { 104 fprintf(stderr, 105 "-- RX SIG_UNSUPP on unsupported feat...OK\n"); 106 td->pass = 1; 107 default_result(current, 1); 108 } 109 110 return true; 111 } 112 113 static bool handle_signal_trigger(struct tdescr *td, 114 siginfo_t *si, void *uc) 115 { 116 td->triggered = 1; 117 /* ->run was asserted NON-NULL in test_setup() already */ 118 td->run(td, si, uc); 119 120 return true; 121 } 122 123 static bool handle_signal_ok(struct tdescr *td, 124 siginfo_t *si, void *uc) 125 { 126 /* 127 * it's a bug in the test code when this assert fail: 128 * if sig_trig was defined, it must have been used before getting here. 129 */ 130 assert(!td->sig_trig || td->triggered); 131 fprintf(stderr, 132 "SIG_OK -- SP:0x%llX si_addr@:%p si_code:%d token@:%p offset:%ld\n", 133 ((ucontext_t *)uc)->uc_mcontext.sp, 134 si->si_addr, si->si_code, td->token, td->token - si->si_addr); 135 /* 136 * fake_sigreturn tests, which have sanity_enabled=1, set, at the very 137 * last time, the token field to the SP address used to place the fake 138 * sigframe: so token==0 means we never made it to the end, 139 * segfaulting well-before, and the test is possibly broken. 140 */ 141 if (!td->sanity_disabled && !td->token) { 142 fprintf(stdout, 143 "current->token ZEROED...test is probably broken!\n"); 144 abort(); 145 } 146 /* 147 * Trying to narrow down the SEGV to the ones generated by Kernel itself 148 * via arm64_notify_segfault(). This is a best-effort check anyway, and 149 * the si_code check may need to change if this aspect of the kernel 150 * ABI changes. 151 */ 152 if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) { 153 fprintf(stdout, 154 "si_code != SEGV_ACCERR...test is probably broken!\n"); 155 abort(); 156 } 157 td->pass = 1; 158 /* 159 * Some tests can lead to SEGV loops: in such a case we want to 160 * terminate immediately exiting straight away; some others are not 161 * supposed to outlive the signal handler code, due to the content of 162 * the fake sigframe which caused the signal itself. 163 */ 164 default_result(current, 1); 165 166 return true; 167 } 168 169 static bool handle_signal_copyctx(struct tdescr *td, 170 siginfo_t *si, void *uc_in) 171 { 172 ucontext_t *uc = uc_in; 173 struct _aarch64_ctx *head; 174 struct extra_context *extra, *copied_extra; 175 size_t offset = 0; 176 size_t to_copy; 177 178 ASSERT_GOOD_CONTEXT(uc); 179 180 /* Mangling PC to avoid loops on original BRK instr */ 181 uc->uc_mcontext.pc += 4; 182 183 /* 184 * Check for an preserve any extra data too with fixups. 185 */ 186 head = (struct _aarch64_ctx *)uc->uc_mcontext.__reserved; 187 head = get_header(head, EXTRA_MAGIC, td->live_sz, &offset); 188 if (head) { 189 extra = (struct extra_context *)head; 190 191 /* 192 * The extra buffer must be immediately after the 193 * extra_context and a 16 byte terminator. Include it 194 * in the copy, this was previously validated in 195 * ASSERT_GOOD_CONTEXT(). 196 */ 197 to_copy = __builtin_offsetof(ucontext_t, 198 uc_mcontext.__reserved); 199 to_copy += offset + sizeof(struct extra_context) + 16; 200 to_copy += extra->size; 201 copied_extra = (struct extra_context *)&(td->live_uc->uc_mcontext.__reserved[offset]); 202 } else { 203 copied_extra = NULL; 204 to_copy = sizeof(ucontext_t); 205 } 206 207 if (to_copy > td->live_sz) { 208 fprintf(stderr, 209 "Not enough space to grab context, %lu/%lu bytes\n", 210 td->live_sz, to_copy); 211 return false; 212 } 213 214 memcpy(td->live_uc, uc, to_copy); 215 216 /* 217 * If there was any EXTRA_CONTEXT fix up the size to be the 218 * struct extra_context and the following terminator record, 219 * this means that the rest of the code does not need to have 220 * special handling for the record and we don't need to fix up 221 * datap for the new location. 222 */ 223 if (copied_extra) 224 copied_extra->head.size = sizeof(*copied_extra) + 16; 225 226 td->live_uc_valid = 1; 227 fprintf(stderr, 228 "%lu byte GOOD CONTEXT grabbed from sig_copyctx handler\n", 229 to_copy); 230 231 return true; 232 } 233 234 static void default_handler(int signum, siginfo_t *si, void *uc) 235 { 236 if (current->sig_unsupp && signum == current->sig_unsupp && 237 handle_signal_unsupported(current, si, uc)) { 238 fprintf(stderr, "Handled SIG_UNSUPP\n"); 239 } else if (current->sig_trig && signum == current->sig_trig && 240 handle_signal_trigger(current, si, uc)) { 241 fprintf(stderr, "Handled SIG_TRIG\n"); 242 } else if (current->sig_ok && signum == current->sig_ok && 243 handle_signal_ok(current, si, uc)) { 244 fprintf(stderr, "Handled SIG_OK\n"); 245 } else if (signum == sig_copyctx && current->live_uc && 246 handle_signal_copyctx(current, si, uc)) { 247 fprintf(stderr, "Handled SIG_COPYCTX\n"); 248 } else { 249 if (signum == SIGALRM && current->timeout) { 250 fprintf(stderr, "-- Timeout !\n"); 251 } else { 252 fprintf(stderr, 253 "-- RX UNEXPECTED SIGNAL: %d code %d address %p\n", 254 signum, si->si_code, si->si_addr); 255 } 256 default_result(current, 1); 257 } 258 } 259 260 static int default_setup(struct tdescr *td) 261 { 262 struct sigaction sa; 263 264 sa.sa_sigaction = default_handler; 265 sa.sa_flags = SA_SIGINFO | SA_RESTART; 266 sa.sa_flags |= td->sa_flags; 267 sigemptyset(&sa.sa_mask); 268 /* uncatchable signals naturally skipped ... */ 269 for (int sig = 1; sig < 32; sig++) 270 sigaction(sig, &sa, NULL); 271 /* 272 * RT Signals default disposition is Term but they cannot be 273 * generated by the Kernel in response to our tests; so just catch 274 * them all and report them as UNEXPECTED signals. 275 */ 276 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++) 277 sigaction(sig, &sa, NULL); 278 279 /* just in case...unblock explicitly all we need */ 280 if (td->sig_trig) 281 unblock_signal(td->sig_trig); 282 if (td->sig_ok) 283 unblock_signal(td->sig_ok); 284 if (td->sig_unsupp) 285 unblock_signal(td->sig_unsupp); 286 287 if (td->timeout) { 288 unblock_signal(SIGALRM); 289 alarm(td->timeout); 290 } 291 fprintf(stderr, "Registered handlers for all signals.\n"); 292 293 return 1; 294 } 295 296 static inline int default_trigger(struct tdescr *td) 297 { 298 return !raise(td->sig_trig); 299 } 300 301 int test_init(struct tdescr *td) 302 { 303 if (td->sig_trig == sig_copyctx) { 304 fprintf(stdout, 305 "Signal %d is RESERVED, cannot be used as a trigger. Aborting\n", 306 sig_copyctx); 307 return 0; 308 } 309 /* just in case */ 310 unblock_signal(sig_copyctx); 311 312 td->minsigstksz = getauxval(AT_MINSIGSTKSZ); 313 if (!td->minsigstksz) 314 td->minsigstksz = MINSIGSTKSZ; 315 fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz); 316 317 if (td->feats_required || td->feats_incompatible) { 318 td->feats_supported = 0; 319 /* 320 * Checking for CPU required features using both the 321 * auxval and the arm64 MRS Emulation to read sysregs. 322 */ 323 if (getauxval(AT_HWCAP) & HWCAP_SSBS) 324 td->feats_supported |= FEAT_SSBS; 325 if (getauxval(AT_HWCAP) & HWCAP_SVE) 326 td->feats_supported |= FEAT_SVE; 327 if (getauxval(AT_HWCAP2) & HWCAP2_SME) 328 td->feats_supported |= FEAT_SME; 329 if (getauxval(AT_HWCAP2) & HWCAP2_SME_FA64) 330 td->feats_supported |= FEAT_SME_FA64; 331 if (getauxval(AT_HWCAP2) & HWCAP2_SME2) 332 td->feats_supported |= FEAT_SME2; 333 if (getauxval(AT_HWCAP) & HWCAP_GCS) 334 td->feats_supported |= FEAT_GCS; 335 if (feats_ok(td)) { 336 if (td->feats_required & td->feats_supported) 337 fprintf(stderr, 338 "Required Features: [%s] supported\n", 339 feats_to_string(td->feats_required & 340 td->feats_supported)); 341 if (!(td->feats_incompatible & td->feats_supported)) 342 fprintf(stderr, 343 "Incompatible Features: [%s] absent\n", 344 feats_to_string(td->feats_incompatible)); 345 } else { 346 if ((td->feats_required & td->feats_supported) != 347 td->feats_supported) 348 fprintf(stderr, 349 "Required Features: [%s] NOT supported\n", 350 feats_to_string(td->feats_required & 351 ~td->feats_supported)); 352 if (td->feats_incompatible & td->feats_supported) 353 fprintf(stderr, 354 "Incompatible Features: [%s] supported\n", 355 feats_to_string(td->feats_incompatible & 356 ~td->feats_supported)); 357 358 359 td->result = KSFT_SKIP; 360 return 0; 361 } 362 } 363 364 /* Perform test specific additional initialization */ 365 if (td->init && !td->init(td)) { 366 fprintf(stderr, "FAILED Testcase initialization.\n"); 367 return 0; 368 } 369 td->initialized = 1; 370 fprintf(stderr, "Testcase initialized.\n"); 371 372 return 1; 373 } 374 375 int test_setup(struct tdescr *td) 376 { 377 /* assert core invariants symptom of a rotten testcase */ 378 assert(current); 379 assert(td); 380 assert(td->name); 381 assert(td->run); 382 383 /* Default result is FAIL if test setup fails */ 384 td->result = KSFT_FAIL; 385 if (td->setup) 386 return td->setup(td); 387 else 388 return default_setup(td); 389 } 390 391 int test_run(struct tdescr *td) 392 { 393 if (td->trigger) 394 return td->trigger(td); 395 else if (td->sig_trig) 396 return default_trigger(td); 397 else 398 return td->run(td, NULL, NULL); 399 } 400 401 void test_result(struct tdescr *td) 402 { 403 if (td->initialized && td->result != KSFT_SKIP && td->check_result) 404 td->check_result(td); 405 default_result(td, 0); 406 } 407 408 void test_cleanup(struct tdescr *td) 409 { 410 if (td->cleanup) 411 td->cleanup(td); 412 } 413