1 /* 2 * Emulation of BSD signals 3 * 4 * Copyright (c) 2003 - 2008 Fabrice Bellard 5 * Copyright (c) 2013 Stacey Son 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "qemu/osdep.h" 22 #include "qemu/log.h" 23 #include "qemu.h" 24 #include "user/cpu_loop.h" 25 #include "exec/page-protection.h" 26 #include "user/page-protection.h" 27 #include "user/signal.h" 28 #include "user/tswap-target.h" 29 #include "gdbstub/user.h" 30 #include "signal-common.h" 31 #include "trace.h" 32 #include "hw/core/tcg-cpu-ops.h" 33 #include "host-signal.h" 34 35 /* target_siginfo_t must fit in gdbstub's siginfo save area. */ 36 QEMU_BUILD_BUG_ON(sizeof(target_siginfo_t) > MAX_SIGINFO_LENGTH); 37 38 static struct target_sigaction sigact_table[TARGET_NSIG]; 39 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc); 40 static void target_to_host_sigset_internal(sigset_t *d, 41 const target_sigset_t *s); 42 43 static inline int on_sig_stack(TaskState *ts, unsigned long sp) 44 { 45 return sp - ts->sigaltstack_used.ss_sp < ts->sigaltstack_used.ss_size; 46 } 47 48 static inline int sas_ss_flags(TaskState *ts, unsigned long sp) 49 { 50 return ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE : 51 on_sig_stack(ts, sp) ? SS_ONSTACK : 0; 52 } 53 54 int host_interrupt_signal = SIGRTMAX; 55 56 /* 57 * The BSD ABIs use the same signal numbers across all the CPU architectures, so 58 * (unlike Linux) these functions are just the identity mapping. This might not 59 * be true for XyzBSD running on AbcBSD, which doesn't currently work. 60 */ 61 int host_to_target_signal(int sig) 62 { 63 return sig; 64 } 65 66 int target_to_host_signal(int sig) 67 { 68 return sig; 69 } 70 71 static inline void target_sigemptyset(target_sigset_t *set) 72 { 73 memset(set, 0, sizeof(*set)); 74 } 75 76 static inline void target_sigaddset(target_sigset_t *set, int signum) 77 { 78 signum--; 79 uint32_t mask = (uint32_t)1 << (signum % TARGET_NSIG_BPW); 80 set->__bits[signum / TARGET_NSIG_BPW] |= mask; 81 } 82 83 static inline int target_sigismember(const target_sigset_t *set, int signum) 84 { 85 signum--; 86 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); 87 return (set->__bits[signum / TARGET_NSIG_BPW] & mask) != 0; 88 } 89 90 /* Adjust the signal context to rewind out of safe-syscall if we're in it */ 91 static inline void rewind_if_in_safe_syscall(void *puc) 92 { 93 ucontext_t *uc = (ucontext_t *)puc; 94 uintptr_t pcreg = host_signal_pc(uc); 95 96 if (pcreg > (uintptr_t)safe_syscall_start 97 && pcreg < (uintptr_t)safe_syscall_end) { 98 host_signal_set_pc(uc, (uintptr_t)safe_syscall_start); 99 } 100 } 101 102 /* 103 * Note: The following take advantage of the BSD signal property that all 104 * signals are available on all architectures. 105 */ 106 static void host_to_target_sigset_internal(target_sigset_t *d, 107 const sigset_t *s) 108 { 109 int i; 110 111 target_sigemptyset(d); 112 for (i = 1; i <= NSIG; i++) { 113 if (sigismember(s, i)) { 114 target_sigaddset(d, host_to_target_signal(i)); 115 } 116 } 117 } 118 119 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s) 120 { 121 target_sigset_t d1; 122 int i; 123 124 host_to_target_sigset_internal(&d1, s); 125 for (i = 0; i < _SIG_WORDS; i++) { 126 d->__bits[i] = tswap32(d1.__bits[i]); 127 } 128 } 129 130 static void target_to_host_sigset_internal(sigset_t *d, 131 const target_sigset_t *s) 132 { 133 int i; 134 135 sigemptyset(d); 136 for (i = 1; i <= TARGET_NSIG; i++) { 137 if (target_sigismember(s, i)) { 138 sigaddset(d, target_to_host_signal(i)); 139 } 140 } 141 } 142 143 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s) 144 { 145 target_sigset_t s1; 146 int i; 147 148 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 149 s1.__bits[i] = tswap32(s->__bits[i]); 150 } 151 target_to_host_sigset_internal(d, &s1); 152 } 153 154 static bool has_trapno(int tsig) 155 { 156 return tsig == TARGET_SIGILL || 157 tsig == TARGET_SIGFPE || 158 tsig == TARGET_SIGSEGV || 159 tsig == TARGET_SIGBUS || 160 tsig == TARGET_SIGTRAP; 161 } 162 163 /* Siginfo conversion. */ 164 165 /* 166 * Populate tinfo w/o swapping based on guessing which fields are valid. 167 */ 168 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, 169 const siginfo_t *info) 170 { 171 int sig = host_to_target_signal(info->si_signo); 172 int si_code = info->si_code; 173 int si_type; 174 175 /* 176 * Make sure we that the variable portion of the target siginfo is zeroed 177 * out so we don't leak anything into that. 178 */ 179 memset(&tinfo->_reason, 0, sizeof(tinfo->_reason)); 180 181 /* 182 * This is awkward, because we have to use a combination of the si_code and 183 * si_signo to figure out which of the union's members are valid.o We 184 * therefore make our best guess. 185 * 186 * Once we have made our guess, we record it in the top 16 bits of 187 * the si_code, so that tswap_siginfo() later can use it. 188 * tswap_siginfo() will strip these top bits out before writing 189 * si_code to the guest (sign-extending the lower bits). 190 */ 191 tinfo->si_signo = sig; 192 tinfo->si_errno = info->si_errno; 193 tinfo->si_code = info->si_code; 194 tinfo->si_pid = info->si_pid; 195 tinfo->si_uid = info->si_uid; 196 tinfo->si_status = info->si_status; 197 tinfo->si_addr = (abi_ulong)(unsigned long)info->si_addr; 198 /* 199 * si_value is opaque to kernel. On all FreeBSD platforms, 200 * sizeof(sival_ptr) >= sizeof(sival_int) so the following 201 * always will copy the larger element. 202 */ 203 tinfo->si_value.sival_ptr = 204 (abi_ulong)(unsigned long)info->si_value.sival_ptr; 205 206 switch (si_code) { 207 /* 208 * All the SI_xxx codes that are defined here are global to 209 * all the signals (they have values that none of the other, 210 * more specific signal info will set). 211 */ 212 case SI_USER: 213 case SI_LWP: 214 case SI_KERNEL: 215 case SI_QUEUE: 216 case SI_ASYNCIO: 217 /* 218 * Only the fixed parts are valid (though FreeBSD doesn't always 219 * set all the fields to non-zero values. 220 */ 221 si_type = QEMU_SI_NOINFO; 222 break; 223 case SI_TIMER: 224 tinfo->_reason._timer._timerid = info->_reason._timer._timerid; 225 tinfo->_reason._timer._overrun = info->_reason._timer._overrun; 226 si_type = QEMU_SI_TIMER; 227 break; 228 case SI_MESGQ: 229 tinfo->_reason._mesgq._mqd = info->_reason._mesgq._mqd; 230 si_type = QEMU_SI_MESGQ; 231 break; 232 default: 233 /* 234 * We have to go based on the signal number now to figure out 235 * what's valid. 236 */ 237 si_type = QEMU_SI_NOINFO; 238 if (has_trapno(sig)) { 239 tinfo->_reason._fault._trapno = info->_reason._fault._trapno; 240 si_type = QEMU_SI_FAULT; 241 } 242 #ifdef TARGET_SIGPOLL 243 /* 244 * FreeBSD never had SIGPOLL, but emulates it for Linux so there's 245 * a chance it may popup in the future. 246 */ 247 if (sig == TARGET_SIGPOLL) { 248 tinfo->_reason._poll._band = info->_reason._poll._band; 249 si_type = QEMU_SI_POLL; 250 } 251 #endif 252 /* 253 * Unsure that this can actually be generated, and our support for 254 * capsicum is somewhere between weak and non-existent, but if we get 255 * one, then we know what to save. 256 */ 257 #ifdef QEMU_SI_CAPSICUM 258 if (sig == TARGET_SIGTRAP) { 259 tinfo->_reason._capsicum._syscall = 260 info->_reason._capsicum._syscall; 261 si_type = QEMU_SI_CAPSICUM; 262 } 263 #endif 264 break; 265 } 266 tinfo->si_code = deposit32(si_code, 24, 8, si_type); 267 } 268 269 static void tswap_siginfo(target_siginfo_t *tinfo, const target_siginfo_t *info) 270 { 271 int si_type = extract32(info->si_code, 24, 8); 272 int si_code = sextract32(info->si_code, 0, 24); 273 274 __put_user(info->si_signo, &tinfo->si_signo); 275 __put_user(info->si_errno, &tinfo->si_errno); 276 __put_user(si_code, &tinfo->si_code); /* Zero out si_type, it's internal */ 277 __put_user(info->si_pid, &tinfo->si_pid); 278 __put_user(info->si_uid, &tinfo->si_uid); 279 __put_user(info->si_status, &tinfo->si_status); 280 __put_user(info->si_addr, &tinfo->si_addr); 281 /* 282 * Unswapped, because we passed it through mostly untouched. si_value is 283 * opaque to the kernel, so we didn't bother with potentially wasting cycles 284 * to swap it into host byte order. 285 */ 286 tinfo->si_value.sival_ptr = info->si_value.sival_ptr; 287 288 /* 289 * We can use our internal marker of which fields in the structure 290 * are valid, rather than duplicating the guesswork of 291 * host_to_target_siginfo_noswap() here. 292 */ 293 switch (si_type) { 294 case QEMU_SI_NOINFO: /* No additional info */ 295 break; 296 case QEMU_SI_FAULT: 297 __put_user(info->_reason._fault._trapno, 298 &tinfo->_reason._fault._trapno); 299 break; 300 case QEMU_SI_TIMER: 301 __put_user(info->_reason._timer._timerid, 302 &tinfo->_reason._timer._timerid); 303 __put_user(info->_reason._timer._overrun, 304 &tinfo->_reason._timer._overrun); 305 break; 306 case QEMU_SI_MESGQ: 307 __put_user(info->_reason._mesgq._mqd, &tinfo->_reason._mesgq._mqd); 308 break; 309 case QEMU_SI_POLL: 310 /* Note: Not generated on FreeBSD */ 311 __put_user(info->_reason._poll._band, &tinfo->_reason._poll._band); 312 break; 313 #ifdef QEMU_SI_CAPSICUM 314 case QEMU_SI_CAPSICUM: 315 __put_user(info->_reason._capsicum._syscall, 316 &tinfo->_reason._capsicum._syscall); 317 break; 318 #endif 319 default: 320 g_assert_not_reached(); 321 } 322 } 323 324 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info) 325 { 326 host_to_target_siginfo_noswap(tinfo, info); 327 tswap_siginfo(tinfo, tinfo); 328 } 329 330 int block_signals(void) 331 { 332 TaskState *ts = get_task_state(thread_cpu); 333 sigset_t set; 334 335 /* 336 * It's OK to block everything including SIGSEGV, because we won't run any 337 * further guest code before unblocking signals in 338 * process_pending_signals(). We depend on the FreeBSD behavior here where 339 * this will only affect this thread's signal mask. We don't use 340 * pthread_sigmask which might seem more correct because that routine also 341 * does odd things with SIGCANCEL to implement pthread_cancel(). 342 */ 343 sigfillset(&set); 344 sigprocmask(SIG_SETMASK, &set, 0); 345 346 return qatomic_xchg(&ts->signal_pending, 1); 347 } 348 349 /* Returns 1 if given signal should dump core if not handled. */ 350 static int core_dump_signal(int sig) 351 { 352 switch (sig) { 353 case TARGET_SIGABRT: 354 case TARGET_SIGFPE: 355 case TARGET_SIGILL: 356 case TARGET_SIGQUIT: 357 case TARGET_SIGSEGV: 358 case TARGET_SIGTRAP: 359 case TARGET_SIGBUS: 360 return 1; 361 default: 362 return 0; 363 } 364 } 365 366 /* Abort execution with signal. */ 367 static G_NORETURN 368 void dump_core_and_abort(int target_sig) 369 { 370 CPUState *cpu = thread_cpu; 371 CPUArchState *env = cpu_env(cpu); 372 TaskState *ts = get_task_state(cpu); 373 int core_dumped = 0; 374 int host_sig; 375 struct sigaction act; 376 377 host_sig = target_to_host_signal(target_sig); 378 gdb_signalled(env, target_sig); 379 380 /* Dump core if supported by target binary format */ 381 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) { 382 stop_all_tasks(); 383 core_dumped = 384 ((*ts->bprm->core_dump)(target_sig, env) == 0); 385 } 386 if (core_dumped) { 387 struct rlimit nodump; 388 389 /* 390 * We already dumped the core of target process, we don't want 391 * a coredump of qemu itself. 392 */ 393 getrlimit(RLIMIT_CORE, &nodump); 394 nodump.rlim_cur = 0; 395 setrlimit(RLIMIT_CORE, &nodump); 396 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) " 397 "- %s\n", target_sig, strsignal(host_sig), "core dumped"); 398 } 399 400 /* 401 * The proper exit code for dying from an uncaught signal is 402 * -<signal>. The kernel doesn't allow exit() or _exit() to pass 403 * a negative value. To get the proper exit code we need to 404 * actually die from an uncaught signal. Here the default signal 405 * handler is installed, we send ourself a signal and we wait for 406 * it to arrive. 407 */ 408 memset(&act, 0, sizeof(act)); 409 sigfillset(&act.sa_mask); 410 act.sa_handler = SIG_DFL; 411 sigaction(host_sig, &act, NULL); 412 413 kill(getpid(), host_sig); 414 415 /* 416 * Make sure the signal isn't masked (just reuse the mask inside 417 * of act). 418 */ 419 sigdelset(&act.sa_mask, host_sig); 420 sigsuspend(&act.sa_mask); 421 422 /* unreachable */ 423 abort(); 424 } 425 426 /* 427 * Queue a signal so that it will be send to the virtual CPU as soon as 428 * possible. 429 */ 430 void queue_signal(CPUArchState *env, int sig, int si_type, 431 target_siginfo_t *info) 432 { 433 CPUState *cpu = env_cpu(env); 434 TaskState *ts = get_task_state(cpu); 435 436 trace_user_queue_signal(env, sig); 437 438 info->si_code = deposit32(info->si_code, 24, 8, si_type); 439 440 ts->sync_signal.info = *info; 441 ts->sync_signal.pending = sig; 442 /* Signal that a new signal is pending. */ 443 qatomic_set(&ts->signal_pending, 1); 444 return; 445 } 446 447 static int fatal_signal(int sig) 448 { 449 450 switch (sig) { 451 case TARGET_SIGCHLD: 452 case TARGET_SIGURG: 453 case TARGET_SIGWINCH: 454 case TARGET_SIGINFO: 455 /* Ignored by default. */ 456 return 0; 457 case TARGET_SIGCONT: 458 case TARGET_SIGSTOP: 459 case TARGET_SIGTSTP: 460 case TARGET_SIGTTIN: 461 case TARGET_SIGTTOU: 462 /* Job control signals. */ 463 return 0; 464 default: 465 return 1; 466 } 467 } 468 469 /* 470 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the 471 * 'force' part is handled in process_pending_signals(). 472 */ 473 void force_sig_fault(int sig, int code, abi_ulong addr) 474 { 475 CPUState *cpu = thread_cpu; 476 target_siginfo_t info = {}; 477 478 info.si_signo = sig; 479 info.si_errno = 0; 480 info.si_code = code; 481 info.si_addr = addr; 482 queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, &info); 483 } 484 485 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) 486 { 487 CPUState *cpu = thread_cpu; 488 TaskState *ts = get_task_state(cpu); 489 target_siginfo_t tinfo; 490 ucontext_t *uc = puc; 491 struct emulated_sigtable *k; 492 int guest_sig; 493 uintptr_t pc = 0; 494 bool sync_sig = false; 495 496 if (host_sig == host_interrupt_signal) { 497 ts->signal_pending = 1; 498 cpu_exit(thread_cpu); 499 return; 500 } 501 502 /* 503 * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special 504 * handling wrt signal blocking and unwinding. 505 */ 506 if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) { 507 MMUAccessType access_type; 508 uintptr_t host_addr; 509 abi_ptr guest_addr; 510 bool is_write; 511 512 host_addr = (uintptr_t)info->si_addr; 513 514 /* 515 * Convert forcefully to guest address space: addresses outside 516 * reserved_va are still valid to report via SEGV_MAPERR. 517 */ 518 guest_addr = h2g_nocheck(host_addr); 519 520 pc = host_signal_pc(uc); 521 is_write = host_signal_write(info, uc); 522 access_type = adjust_signal_pc(&pc, is_write); 523 524 if (host_sig == SIGSEGV) { 525 bool maperr = true; 526 527 if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) { 528 /* If this was a write to a TB protected page, restart. */ 529 if (is_write && 530 handle_sigsegv_accerr_write(cpu, &uc->uc_sigmask, 531 pc, guest_addr)) { 532 return; 533 } 534 535 /* 536 * With reserved_va, the whole address space is PROT_NONE, 537 * which means that we may get ACCERR when we want MAPERR. 538 */ 539 if (page_get_flags(guest_addr) & PAGE_VALID) { 540 maperr = false; 541 } else { 542 info->si_code = SEGV_MAPERR; 543 } 544 } 545 546 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); 547 cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc); 548 } else { 549 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); 550 if (info->si_code == BUS_ADRALN) { 551 cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc); 552 } 553 } 554 555 sync_sig = true; 556 } 557 558 /* Get the target signal number. */ 559 guest_sig = host_to_target_signal(host_sig); 560 if (guest_sig < 1 || guest_sig > TARGET_NSIG) { 561 return; 562 } 563 trace_user_host_signal(cpu, host_sig, guest_sig); 564 565 host_to_target_siginfo_noswap(&tinfo, info); 566 567 k = &ts->sigtab[guest_sig - 1]; 568 k->info = tinfo; 569 k->pending = guest_sig; 570 ts->signal_pending = 1; 571 572 /* 573 * For synchronous signals, unwind the cpu state to the faulting 574 * insn and then exit back to the main loop so that the signal 575 * is delivered immediately. 576 */ 577 if (sync_sig) { 578 cpu->exception_index = EXCP_INTERRUPT; 579 cpu_loop_exit_restore(cpu, pc); 580 } 581 582 rewind_if_in_safe_syscall(puc); 583 584 /* 585 * Block host signals until target signal handler entered. We 586 * can't block SIGSEGV or SIGBUS while we're executing guest 587 * code in case the guest code provokes one in the window between 588 * now and it getting out to the main loop. Signals will be 589 * unblocked again in process_pending_signals(). 590 */ 591 sigfillset(&uc->uc_sigmask); 592 sigdelset(&uc->uc_sigmask, SIGSEGV); 593 sigdelset(&uc->uc_sigmask, SIGBUS); 594 595 /* Interrupt the virtual CPU as soon as possible. */ 596 cpu_exit(thread_cpu); 597 } 598 599 /* do_sigaltstack() returns target values and errnos. */ 600 /* compare to kern/kern_sig.c sys_sigaltstack() and kern_sigaltstack() */ 601 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp) 602 { 603 TaskState *ts = get_task_state(thread_cpu); 604 int ret; 605 target_stack_t oss; 606 607 if (uoss_addr) { 608 /* Save current signal stack params */ 609 oss.ss_sp = tswapl(ts->sigaltstack_used.ss_sp); 610 oss.ss_size = tswapl(ts->sigaltstack_used.ss_size); 611 oss.ss_flags = tswapl(sas_ss_flags(ts, sp)); 612 } 613 614 if (uss_addr) { 615 target_stack_t *uss; 616 target_stack_t ss; 617 size_t minstacksize = TARGET_MINSIGSTKSZ; 618 619 ret = -TARGET_EFAULT; 620 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) { 621 goto out; 622 } 623 __get_user(ss.ss_sp, &uss->ss_sp); 624 __get_user(ss.ss_size, &uss->ss_size); 625 __get_user(ss.ss_flags, &uss->ss_flags); 626 unlock_user_struct(uss, uss_addr, 0); 627 628 ret = -TARGET_EPERM; 629 if (on_sig_stack(ts, sp)) { 630 goto out; 631 } 632 633 ret = -TARGET_EINVAL; 634 if (ss.ss_flags != TARGET_SS_DISABLE 635 && ss.ss_flags != TARGET_SS_ONSTACK 636 && ss.ss_flags != 0) { 637 goto out; 638 } 639 640 if (ss.ss_flags == TARGET_SS_DISABLE) { 641 ss.ss_size = 0; 642 ss.ss_sp = 0; 643 } else { 644 ret = -TARGET_ENOMEM; 645 if (ss.ss_size < minstacksize) { 646 goto out; 647 } 648 } 649 650 ts->sigaltstack_used.ss_sp = ss.ss_sp; 651 ts->sigaltstack_used.ss_size = ss.ss_size; 652 } 653 654 if (uoss_addr) { 655 ret = -TARGET_EFAULT; 656 if (copy_to_user(uoss_addr, &oss, sizeof(oss))) { 657 goto out; 658 } 659 } 660 661 ret = 0; 662 out: 663 return ret; 664 } 665 666 /* do_sigaction() return host values and errnos */ 667 int do_sigaction(int sig, const struct target_sigaction *act, 668 struct target_sigaction *oact) 669 { 670 struct target_sigaction *k; 671 struct sigaction act1; 672 int host_sig; 673 int ret = 0; 674 675 if (sig < 1 || sig > TARGET_NSIG) { 676 return -TARGET_EINVAL; 677 } 678 679 if ((sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) && 680 act != NULL && act->_sa_handler != TARGET_SIG_DFL) { 681 return -TARGET_EINVAL; 682 } 683 684 if (block_signals()) { 685 return -TARGET_ERESTART; 686 } 687 688 k = &sigact_table[sig - 1]; 689 if (oact) { 690 oact->_sa_handler = tswapal(k->_sa_handler); 691 oact->sa_flags = tswap32(k->sa_flags); 692 oact->sa_mask = k->sa_mask; 693 } 694 if (act) { 695 k->_sa_handler = tswapal(act->_sa_handler); 696 k->sa_flags = tswap32(act->sa_flags); 697 k->sa_mask = act->sa_mask; 698 699 /* Update the host signal state. */ 700 host_sig = target_to_host_signal(sig); 701 if (host_sig != SIGSEGV && host_sig != SIGBUS) { 702 memset(&act1, 0, sizeof(struct sigaction)); 703 sigfillset(&act1.sa_mask); 704 act1.sa_flags = SA_SIGINFO; 705 if (k->sa_flags & TARGET_SA_RESTART) { 706 act1.sa_flags |= SA_RESTART; 707 } 708 /* 709 * Note: It is important to update the host kernel signal mask to 710 * avoid getting unexpected interrupted system calls. 711 */ 712 if (k->_sa_handler == TARGET_SIG_IGN) { 713 act1.sa_sigaction = (void *)SIG_IGN; 714 } else if (k->_sa_handler == TARGET_SIG_DFL) { 715 if (fatal_signal(sig)) { 716 act1.sa_sigaction = host_signal_handler; 717 } else { 718 act1.sa_sigaction = (void *)SIG_DFL; 719 } 720 } else { 721 act1.sa_sigaction = host_signal_handler; 722 } 723 ret = sigaction(host_sig, &act1, NULL); 724 } 725 } 726 return ret; 727 } 728 729 static inline abi_ulong get_sigframe(struct target_sigaction *ka, 730 CPUArchState *env, size_t frame_size) 731 { 732 TaskState *ts = get_task_state(thread_cpu); 733 abi_ulong sp; 734 735 /* Use default user stack */ 736 sp = get_sp_from_cpustate(env); 737 738 if ((ka->sa_flags & TARGET_SA_ONSTACK) && sas_ss_flags(ts, sp) == 0) { 739 sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size; 740 } 741 742 return ROUND_DOWN(sp - frame_size, TARGET_SIGSTACK_ALIGN); 743 } 744 745 /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */ 746 747 static void setup_frame(int sig, int code, struct target_sigaction *ka, 748 target_sigset_t *set, target_siginfo_t *tinfo, CPUArchState *env) 749 { 750 struct target_sigframe *frame; 751 abi_ulong frame_addr; 752 int i; 753 754 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 755 trace_user_setup_frame(env, frame_addr); 756 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 757 unlock_user_struct(frame, frame_addr, 1); 758 dump_core_and_abort(TARGET_SIGILL); 759 return; 760 } 761 762 memset(frame, 0, sizeof(*frame)); 763 setup_sigframe_arch(env, frame_addr, frame, 0); 764 765 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 766 __put_user(set->__bits[i], &frame->sf_uc.uc_sigmask.__bits[i]); 767 } 768 769 if (tinfo) { 770 frame->sf_si.si_signo = tinfo->si_signo; 771 frame->sf_si.si_errno = tinfo->si_errno; 772 frame->sf_si.si_code = tinfo->si_code; 773 frame->sf_si.si_pid = tinfo->si_pid; 774 frame->sf_si.si_uid = tinfo->si_uid; 775 frame->sf_si.si_status = tinfo->si_status; 776 frame->sf_si.si_addr = tinfo->si_addr; 777 /* see host_to_target_siginfo_noswap() for more details */ 778 frame->sf_si.si_value.sival_ptr = tinfo->si_value.sival_ptr; 779 /* 780 * At this point, whatever is in the _reason union is complete 781 * and in target order, so just copy the whole thing over, even 782 * if it's too large for this specific signal. 783 * host_to_target_siginfo_noswap() and tswap_siginfo() have ensured 784 * that's so. 785 */ 786 memcpy(&frame->sf_si._reason, &tinfo->_reason, 787 sizeof(tinfo->_reason)); 788 } 789 790 set_sigtramp_args(env, sig, frame, frame_addr, ka); 791 792 unlock_user_struct(frame, frame_addr, 1); 793 } 794 795 static int reset_signal_mask(target_ucontext_t *ucontext) 796 { 797 int i; 798 sigset_t blocked; 799 target_sigset_t target_set; 800 TaskState *ts = get_task_state(thread_cpu); 801 802 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 803 __get_user(target_set.__bits[i], &ucontext->uc_sigmask.__bits[i]); 804 } 805 target_to_host_sigset_internal(&blocked, &target_set); 806 ts->signal_mask = blocked; 807 808 return 0; 809 } 810 811 /* See sys/$M/$M/exec_machdep.c sigreturn() */ 812 long do_sigreturn(CPUArchState *env, abi_ulong addr) 813 { 814 long ret; 815 abi_ulong target_ucontext; 816 target_ucontext_t *ucontext = NULL; 817 818 /* Get the target ucontext address from the stack frame */ 819 ret = get_ucontext_sigreturn(env, addr, &target_ucontext); 820 if (is_error(ret)) { 821 return ret; 822 } 823 trace_user_do_sigreturn(env, addr); 824 if (!lock_user_struct(VERIFY_READ, ucontext, target_ucontext, 0)) { 825 goto badframe; 826 } 827 828 /* Set the register state back to before the signal. */ 829 if (set_mcontext(env, &ucontext->uc_mcontext, 1)) { 830 goto badframe; 831 } 832 833 /* And reset the signal mask. */ 834 if (reset_signal_mask(ucontext)) { 835 goto badframe; 836 } 837 838 unlock_user_struct(ucontext, target_ucontext, 0); 839 return -TARGET_EJUSTRETURN; 840 841 badframe: 842 if (ucontext != NULL) { 843 unlock_user_struct(ucontext, target_ucontext, 0); 844 } 845 return -TARGET_EFAULT; 846 } 847 848 void signal_init(void) 849 { 850 TaskState *ts = get_task_state(thread_cpu); 851 struct sigaction act; 852 struct sigaction oact; 853 int i; 854 int host_sig; 855 856 /* Set the signal mask from the host mask. */ 857 sigprocmask(0, 0, &ts->signal_mask); 858 859 sigfillset(&act.sa_mask); 860 act.sa_sigaction = host_signal_handler; 861 act.sa_flags = SA_SIGINFO; 862 863 for (i = 1; i <= TARGET_NSIG; i++) { 864 host_sig = target_to_host_signal(i); 865 if (host_sig == host_interrupt_signal) { 866 continue; 867 } 868 sigaction(host_sig, NULL, &oact); 869 if (oact.sa_sigaction == (void *)SIG_IGN) { 870 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN; 871 } else if (oact.sa_sigaction == (void *)SIG_DFL) { 872 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL; 873 } 874 /* 875 * If there's already a handler installed then something has 876 * gone horribly wrong, so don't even try to handle that case. 877 * Install some handlers for our own use. We need at least 878 * SIGSEGV and SIGBUS, to detect exceptions. We can not just 879 * trap all signals because it affects syscall interrupt 880 * behavior. But do trap all default-fatal signals. 881 */ 882 if (fatal_signal(i)) { 883 sigaction(host_sig, &act, NULL); 884 } 885 } 886 sigaction(host_interrupt_signal, &act, NULL); 887 } 888 889 static void handle_pending_signal(CPUArchState *env, int sig, 890 struct emulated_sigtable *k) 891 { 892 CPUState *cpu = env_cpu(env); 893 TaskState *ts = get_task_state(cpu); 894 struct target_sigaction *sa; 895 int code; 896 sigset_t set; 897 abi_ulong handler; 898 target_siginfo_t tinfo; 899 target_sigset_t target_old_set; 900 901 trace_user_handle_signal(env, sig); 902 903 k->pending = 0; 904 905 sig = gdb_handlesig(cpu, sig, NULL, &k->info, sizeof(k->info)); 906 if (!sig) { 907 sa = NULL; 908 handler = TARGET_SIG_IGN; 909 } else { 910 sa = &sigact_table[sig - 1]; 911 handler = sa->_sa_handler; 912 } 913 914 if (do_strace) { 915 print_taken_signal(sig, &k->info); 916 } 917 918 if (handler == TARGET_SIG_DFL) { 919 /* 920 * default handler : ignore some signal. The other are job 921 * control or fatal. 922 */ 923 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || 924 sig == TARGET_SIGTTOU) { 925 kill(getpid(), SIGSTOP); 926 } else if (sig != TARGET_SIGCHLD && sig != TARGET_SIGURG && 927 sig != TARGET_SIGINFO && sig != TARGET_SIGWINCH && 928 sig != TARGET_SIGCONT) { 929 dump_core_and_abort(sig); 930 } 931 } else if (handler == TARGET_SIG_IGN) { 932 /* ignore sig */ 933 } else if (handler == TARGET_SIG_ERR) { 934 dump_core_and_abort(sig); 935 } else { 936 /* compute the blocked signals during the handler execution */ 937 sigset_t *blocked_set; 938 939 target_to_host_sigset(&set, &sa->sa_mask); 940 /* 941 * SA_NODEFER indicates that the current signal should not be 942 * blocked during the handler. 943 */ 944 if (!(sa->sa_flags & TARGET_SA_NODEFER)) { 945 sigaddset(&set, target_to_host_signal(sig)); 946 } 947 948 /* 949 * Save the previous blocked signal state to restore it at the 950 * end of the signal execution (see do_sigreturn). 951 */ 952 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask); 953 954 blocked_set = ts->in_sigsuspend ? 955 &ts->sigsuspend_mask : &ts->signal_mask; 956 sigorset(&ts->signal_mask, blocked_set, &set); 957 ts->in_sigsuspend = false; 958 sigprocmask(SIG_SETMASK, &ts->signal_mask, NULL); 959 960 /* XXX VM86 on x86 ??? */ 961 962 code = k->info.si_code; /* From host, so no si_type */ 963 /* prepare the stack frame of the virtual CPU */ 964 if (sa->sa_flags & TARGET_SA_SIGINFO) { 965 tswap_siginfo(&tinfo, &k->info); 966 setup_frame(sig, code, sa, &target_old_set, &tinfo, env); 967 } else { 968 setup_frame(sig, code, sa, &target_old_set, NULL, env); 969 } 970 if (sa->sa_flags & TARGET_SA_RESETHAND) { 971 sa->_sa_handler = TARGET_SIG_DFL; 972 } 973 } 974 } 975 976 void process_pending_signals(CPUArchState *env) 977 { 978 CPUState *cpu = env_cpu(env); 979 int sig; 980 sigset_t *blocked_set, set; 981 struct emulated_sigtable *k; 982 TaskState *ts = get_task_state(cpu); 983 984 while (qatomic_read(&ts->signal_pending)) { 985 sigfillset(&set); 986 sigprocmask(SIG_SETMASK, &set, 0); 987 988 restart_scan: 989 sig = ts->sync_signal.pending; 990 if (sig) { 991 /* 992 * Synchronous signals are forced by the emulated CPU in some way. 993 * If they are set to ignore, restore the default handler (see 994 * sys/kern_sig.c trapsignal() and execsigs() for this behavior) 995 * though maybe this is done only when forcing exit for non SIGCHLD. 996 */ 997 if (sigismember(&ts->signal_mask, target_to_host_signal(sig)) || 998 sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) { 999 sigdelset(&ts->signal_mask, target_to_host_signal(sig)); 1000 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL; 1001 } 1002 handle_pending_signal(env, sig, &ts->sync_signal); 1003 } 1004 1005 k = ts->sigtab; 1006 for (sig = 1; sig <= TARGET_NSIG; sig++, k++) { 1007 blocked_set = ts->in_sigsuspend ? 1008 &ts->sigsuspend_mask : &ts->signal_mask; 1009 if (k->pending && 1010 !sigismember(blocked_set, target_to_host_signal(sig))) { 1011 handle_pending_signal(env, sig, k); 1012 /* 1013 * Restart scan from the beginning, as handle_pending_signal 1014 * might have resulted in a new synchronous signal (eg SIGSEGV). 1015 */ 1016 goto restart_scan; 1017 } 1018 } 1019 1020 /* 1021 * Unblock signals and check one more time. Unblocking signals may cause 1022 * us to take another host signal, which will set signal_pending again. 1023 */ 1024 qatomic_set(&ts->signal_pending, 0); 1025 ts->in_sigsuspend = false; 1026 set = ts->signal_mask; 1027 sigdelset(&set, SIGSEGV); 1028 sigdelset(&set, SIGBUS); 1029 sigprocmask(SIG_SETMASK, &set, 0); 1030 } 1031 ts->in_sigsuspend = false; 1032 } 1033 1034 void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr, 1035 MMUAccessType access_type, bool maperr, uintptr_t ra) 1036 { 1037 const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; 1038 1039 if (tcg_ops->record_sigsegv) { 1040 tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra); 1041 } 1042 1043 force_sig_fault(TARGET_SIGSEGV, 1044 maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR, 1045 addr); 1046 cpu->exception_index = EXCP_INTERRUPT; 1047 cpu_loop_exit_restore(cpu, ra); 1048 } 1049 1050 void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, 1051 MMUAccessType access_type, uintptr_t ra) 1052 { 1053 const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; 1054 1055 if (tcg_ops->record_sigbus) { 1056 tcg_ops->record_sigbus(cpu, addr, access_type, ra); 1057 } 1058 1059 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr); 1060 cpu->exception_index = EXCP_INTERRUPT; 1061 cpu_loop_exit_restore(cpu, ra); 1062 } 1063