1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk}) 4 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de) 5 * Copyright (C) 2004 PathScale, Inc 6 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 7 */ 8 9 #include <stdlib.h> 10 #include <stdarg.h> 11 #include <stdbool.h> 12 #include <errno.h> 13 #include <signal.h> 14 #include <string.h> 15 #include <strings.h> 16 #include <as-layout.h> 17 #include <kern_util.h> 18 #include <os.h> 19 #include <skas.h> 20 #include <sysdep/mcontext.h> 21 #include <um_malloc.h> 22 #include <sys/ucontext.h> 23 #include <timetravel.h> 24 #include "internal.h" 25 26 void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *, void *mc) = { 27 [SIGTRAP] = relay_signal, 28 [SIGFPE] = relay_signal, 29 [SIGILL] = relay_signal, 30 [SIGWINCH] = winch, 31 [SIGBUS] = relay_signal, 32 [SIGSEGV] = segv_handler, 33 [SIGIO] = sigio_handler, 34 [SIGCHLD] = sigchld_handler, 35 }; 36 37 static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc) 38 { 39 struct uml_pt_regs r; 40 41 r.is_user = 0; 42 if (sig == SIGSEGV) { 43 /* For segfaults, we want the data from the sigcontext. */ 44 get_regs_from_mc(&r, mc); 45 GET_FAULTINFO_FROM_MC(r.faultinfo, mc); 46 } 47 48 /* enable signals if sig isn't IRQ signal */ 49 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGCHLD)) 50 unblock_signals_trace(); 51 52 (*sig_info[sig])(sig, si, &r, mc); 53 } 54 55 /* 56 * These are the asynchronous signals. SIGPROF is excluded because we want to 57 * be able to profile all of UML, not just the non-critical sections. If 58 * profiling is not thread-safe, then that is not my problem. We can disable 59 * profiling when SMP is enabled in that case. 60 */ 61 #define SIGIO_BIT 0 62 #define SIGIO_MASK (1 << SIGIO_BIT) 63 64 #define SIGALRM_BIT 1 65 #define SIGALRM_MASK (1 << SIGALRM_BIT) 66 67 #define SIGCHLD_BIT 2 68 #define SIGCHLD_MASK (1 << SIGCHLD_BIT) 69 70 __thread int signals_enabled; 71 #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT) 72 static int signals_blocked, signals_blocked_pending; 73 #endif 74 static __thread unsigned int signals_pending; 75 static __thread unsigned int signals_active; 76 77 static void sig_handler(int sig, struct siginfo *si, mcontext_t *mc) 78 { 79 int enabled = signals_enabled; 80 81 #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT) 82 if ((signals_blocked || 83 __atomic_load_n(&signals_blocked_pending, __ATOMIC_SEQ_CST)) && 84 (sig == SIGIO)) { 85 /* increment so unblock will do another round */ 86 __atomic_add_fetch(&signals_blocked_pending, 1, 87 __ATOMIC_SEQ_CST); 88 return; 89 } 90 #endif 91 92 if (!enabled && (sig == SIGIO)) { 93 /* 94 * In TT_MODE_EXTERNAL, need to still call time-travel 95 * handlers. This will mark signals_pending by itself 96 * (only if necessary.) 97 * Note we won't get here if signals are hard-blocked 98 * (which is handled above), in that case the hard- 99 * unblock will handle things. 100 */ 101 if (time_travel_mode == TT_MODE_EXTERNAL) 102 sigio_run_timetravel_handlers(); 103 else 104 signals_pending |= SIGIO_MASK; 105 return; 106 } 107 108 if (!enabled && (sig == SIGCHLD)) { 109 signals_pending |= SIGCHLD_MASK; 110 return; 111 } 112 113 block_signals_trace(); 114 115 sig_handler_common(sig, si, mc); 116 117 um_set_signals_trace(enabled); 118 } 119 120 static void timer_real_alarm_handler(mcontext_t *mc) 121 { 122 struct uml_pt_regs regs; 123 124 if (mc != NULL) 125 get_regs_from_mc(®s, mc); 126 else 127 memset(®s, 0, sizeof(regs)); 128 timer_handler(SIGALRM, NULL, ®s); 129 } 130 131 static void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc) 132 { 133 int enabled; 134 135 enabled = signals_enabled; 136 if (!signals_enabled) { 137 signals_pending |= SIGALRM_MASK; 138 return; 139 } 140 141 block_signals_trace(); 142 143 signals_active |= SIGALRM_MASK; 144 145 timer_real_alarm_handler(mc); 146 147 signals_active &= ~SIGALRM_MASK; 148 149 um_set_signals_trace(enabled); 150 } 151 152 void deliver_alarm(void) { 153 timer_alarm_handler(SIGALRM, NULL, NULL); 154 } 155 156 void timer_set_signal_handler(void) 157 { 158 set_handler(SIGALRM); 159 } 160 161 int timer_alarm_pending(void) 162 { 163 return !!(signals_pending & SIGALRM_MASK); 164 } 165 166 void set_sigstack(void *sig_stack, int size) 167 { 168 stack_t stack = { 169 .ss_flags = 0, 170 .ss_sp = sig_stack, 171 .ss_size = size 172 }; 173 174 if (sigaltstack(&stack, NULL) != 0) 175 panic("enabling signal stack failed, errno = %d\n", errno); 176 } 177 178 static void sigusr1_handler(int sig, struct siginfo *unused_si, mcontext_t *mc) 179 { 180 uml_pm_wake(); 181 } 182 183 void register_pm_wake_signal(void) 184 { 185 set_handler(SIGUSR1); 186 } 187 188 static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = { 189 [SIGSEGV] = sig_handler, 190 [SIGBUS] = sig_handler, 191 [SIGILL] = sig_handler, 192 [SIGFPE] = sig_handler, 193 [SIGTRAP] = sig_handler, 194 195 [SIGIO] = sig_handler, 196 [SIGWINCH] = sig_handler, 197 /* SIGCHLD is only actually registered in seccomp mode. */ 198 [SIGCHLD] = sig_handler, 199 [SIGALRM] = timer_alarm_handler, 200 201 [SIGUSR1] = sigusr1_handler, 202 }; 203 204 static void hard_handler(int sig, siginfo_t *si, void *p) 205 { 206 ucontext_t *uc = p; 207 mcontext_t *mc = &uc->uc_mcontext; 208 int save_errno = errno; 209 210 (*handlers[sig])(sig, (struct siginfo *)si, mc); 211 212 errno = save_errno; 213 } 214 215 void set_handler(int sig) 216 { 217 struct sigaction action; 218 int flags = SA_SIGINFO | SA_ONSTACK; 219 sigset_t sig_mask; 220 221 action.sa_sigaction = hard_handler; 222 223 /* block irq ones */ 224 sigemptyset(&action.sa_mask); 225 sigaddset(&action.sa_mask, SIGIO); 226 sigaddset(&action.sa_mask, SIGWINCH); 227 sigaddset(&action.sa_mask, SIGALRM); 228 if (using_seccomp) 229 sigaddset(&action.sa_mask, SIGCHLD); 230 231 if (sig == SIGSEGV) 232 flags |= SA_NODEFER; 233 234 if (sigismember(&action.sa_mask, sig)) 235 flags |= SA_RESTART; /* if it's an irq signal */ 236 237 action.sa_flags = flags; 238 action.sa_restorer = NULL; 239 if (sigaction(sig, &action, NULL) < 0) 240 panic("sigaction failed - errno = %d\n", errno); 241 242 sigemptyset(&sig_mask); 243 sigaddset(&sig_mask, sig); 244 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0) 245 panic("sigprocmask failed - errno = %d\n", errno); 246 } 247 248 void send_sigio_to_self(void) 249 { 250 kill(os_getpid(), SIGIO); 251 } 252 253 int change_sig(int signal, int on) 254 { 255 sigset_t sigset; 256 257 sigemptyset(&sigset); 258 sigaddset(&sigset, signal); 259 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0) 260 return -errno; 261 262 return 0; 263 } 264 265 static inline void __block_signals(void) 266 { 267 if (!signals_enabled) 268 return; 269 270 os_local_ipi_disable(); 271 barrier(); 272 signals_enabled = 0; 273 } 274 275 static inline void __unblock_signals(void) 276 { 277 if (signals_enabled) 278 return; 279 280 signals_enabled = 1; 281 barrier(); 282 os_local_ipi_enable(); 283 } 284 285 void block_signals(void) 286 { 287 __block_signals(); 288 /* 289 * This must return with signals disabled, so this barrier 290 * ensures that writes are flushed out before the return. 291 * This might matter if gcc figures out how to inline this and 292 * decides to shuffle this code into the caller. 293 */ 294 barrier(); 295 } 296 297 void unblock_signals(void) 298 { 299 int save_pending; 300 301 if (signals_enabled == 1) 302 return; 303 304 __unblock_signals(); 305 306 #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT) 307 deliver_time_travel_irqs(); 308 #endif 309 310 /* 311 * We loop because the IRQ handler returns with interrupts off. So, 312 * interrupts may have arrived and we need to re-enable them and 313 * recheck signals_pending. 314 */ 315 while (1) { 316 /* 317 * Save and reset save_pending after enabling signals. This 318 * way, signals_pending won't be changed while we're reading it. 319 * 320 * Setting signals_enabled and reading signals_pending must 321 * happen in this order, so have the barrier here. 322 */ 323 barrier(); 324 325 save_pending = signals_pending; 326 if (save_pending == 0) 327 return; 328 329 signals_pending = 0; 330 331 /* 332 * We have pending interrupts, so disable signals, as the 333 * handlers expect them off when they are called. They will 334 * be enabled again above. We need to trace this, as we're 335 * expected to be enabling interrupts already, but any more 336 * tracing that happens inside the handlers we call for the 337 * pending signals will mess up the tracing state. 338 */ 339 __block_signals(); 340 um_trace_signals_off(); 341 342 /* 343 * Deal with SIGIO first because the alarm handler might 344 * schedule, leaving the pending SIGIO stranded until we come 345 * back here. 346 * 347 * SIGIO's handler doesn't use siginfo or mcontext, 348 * so they can be NULL. 349 */ 350 if (save_pending & SIGIO_MASK) 351 sig_handler_common(SIGIO, NULL, NULL); 352 353 if (save_pending & SIGCHLD_MASK) { 354 struct uml_pt_regs regs = {}; 355 356 sigchld_handler(SIGCHLD, NULL, ®s, NULL); 357 } 358 359 /* Do not reenter the handler */ 360 361 if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK))) 362 timer_real_alarm_handler(NULL); 363 364 /* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */ 365 366 if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK)) 367 return; 368 369 /* Re-enable signals and trace that we're doing so. */ 370 um_trace_signals_on(); 371 __unblock_signals(); 372 } 373 } 374 375 int um_get_signals(void) 376 { 377 return signals_enabled; 378 } 379 380 int um_set_signals(int enable) 381 { 382 int ret; 383 if (signals_enabled == enable) 384 return enable; 385 386 ret = signals_enabled; 387 if (enable) 388 unblock_signals(); 389 else block_signals(); 390 391 return ret; 392 } 393 394 int um_set_signals_trace(int enable) 395 { 396 int ret; 397 if (signals_enabled == enable) 398 return enable; 399 400 ret = signals_enabled; 401 if (enable) 402 unblock_signals_trace(); 403 else 404 block_signals_trace(); 405 406 return ret; 407 } 408 409 #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT) 410 void mark_sigio_pending(void) 411 { 412 /* 413 * It would seem that this should be atomic so 414 * it isn't a read-modify-write with a signal 415 * that could happen in the middle, losing the 416 * value set by the signal. 417 * 418 * However, this function is only called when in 419 * time-travel=ext simulation mode, in which case 420 * the only signal ever pending is SIGIO, which 421 * is blocked while this can be called, and the 422 * timer signal (SIGALRM) cannot happen. 423 */ 424 signals_pending |= SIGIO_MASK; 425 } 426 427 void block_signals_hard(void) 428 { 429 signals_blocked++; 430 barrier(); 431 } 432 433 void unblock_signals_hard(void) 434 { 435 static bool unblocking; 436 437 if (!signals_blocked) 438 panic("unblocking signals while not blocked"); 439 440 if (--signals_blocked) 441 return; 442 /* 443 * Must be set to 0 before we check pending so the 444 * SIGIO handler will run as normal unless we're still 445 * going to process signals_blocked_pending. 446 */ 447 barrier(); 448 449 /* 450 * Note that block_signals_hard()/unblock_signals_hard() can be called 451 * within the unblock_signals()/sigio_run_timetravel_handlers() below. 452 * This would still be prone to race conditions since it's actually a 453 * call _within_ e.g. vu_req_read_message(), where we observed this 454 * issue, which loops. Thus, if the inner call handles the recorded 455 * pending signals, we can get out of the inner call with the real 456 * signal hander no longer blocked, and still have a race. Thus don't 457 * handle unblocking in the inner call, if it happens, but only in 458 * the outermost call - 'unblocking' serves as an ownership for the 459 * signals_blocked_pending decrement. 460 */ 461 if (unblocking) 462 return; 463 unblocking = true; 464 465 while (__atomic_load_n(&signals_blocked_pending, __ATOMIC_SEQ_CST)) { 466 if (signals_enabled) { 467 /* signals are enabled so we can touch this */ 468 signals_pending |= SIGIO_MASK; 469 /* 470 * this is a bit inefficient, but that's 471 * not really important 472 */ 473 block_signals(); 474 unblock_signals(); 475 } else { 476 /* 477 * we need to run time-travel handlers even 478 * if not enabled 479 */ 480 sigio_run_timetravel_handlers(); 481 } 482 483 /* 484 * The decrement of signals_blocked_pending must be atomic so 485 * that the signal handler will either happen before or after 486 * the decrement, not during a read-modify-write: 487 * - If it happens before, it can increment it and we'll 488 * decrement it and do another round in the loop. 489 * - If it happens after it'll see 0 for both signals_blocked 490 * and signals_blocked_pending and thus run the handler as 491 * usual (subject to signals_enabled, but that's unrelated.) 492 * 493 * Note that a call to unblock_signals_hard() within the calls 494 * to unblock_signals() or sigio_run_timetravel_handlers() above 495 * will do nothing due to the 'unblocking' state, so this cannot 496 * underflow as the only one decrementing will be the outermost 497 * one. 498 */ 499 if (__atomic_sub_fetch(&signals_blocked_pending, 1, 500 __ATOMIC_SEQ_CST) < 0) 501 panic("signals_blocked_pending underflow"); 502 } 503 504 unblocking = false; 505 } 506 #endif 507