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