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