1 /* 2 * Emulation of Linux signals 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 */ 19 #include "qemu/osdep.h" 20 #include "qemu/bitops.h" 21 #include "qemu/cutils.h" 22 #include "gdbstub/user.h" 23 #include "exec/page-protection.h" 24 #include "accel/tcg/cpu-ops.h" 25 26 #include <sys/ucontext.h> 27 #include <sys/resource.h> 28 29 #include "qemu.h" 30 #include "user-internals.h" 31 #include "strace.h" 32 #include "loader.h" 33 #include "trace.h" 34 #include "signal-common.h" 35 #include "host-signal.h" 36 #include "user/cpu_loop.h" 37 #include "user/page-protection.h" 38 #include "user/safe-syscall.h" 39 #include "user/signal.h" 40 #include "tcg/tcg.h" 41 42 /* target_siginfo_t must fit in gdbstub's siginfo save area. */ 43 QEMU_BUILD_BUG_ON(sizeof(target_siginfo_t) > MAX_SIGINFO_LENGTH); 44 45 static struct target_sigaction sigact_table[TARGET_NSIG]; 46 47 static void host_signal_handler(int host_signum, siginfo_t *info, 48 void *puc); 49 50 /* Fallback addresses into sigtramp page. */ 51 abi_ulong default_sigreturn; 52 abi_ulong default_rt_sigreturn; 53 abi_ulong vdso_sigreturn_region_start; 54 abi_ulong vdso_sigreturn_region_end; 55 56 /* 57 * System includes define _NSIG as SIGRTMAX + 1, but qemu (like the kernel) 58 * defines TARGET_NSIG as TARGET_SIGRTMAX and the first signal is 1. 59 * Signal number 0 is reserved for use as kill(pid, 0), to test whether 60 * a process exists without sending it a signal. 61 */ 62 #ifdef __SIGRTMAX 63 QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG); 64 #endif 65 static uint8_t host_to_target_signal_table[_NSIG] = { 66 #define MAKE_SIG_ENTRY(sig) [sig] = TARGET_##sig, 67 MAKE_SIGNAL_LIST 68 #undef MAKE_SIG_ENTRY 69 }; 70 71 static uint8_t target_to_host_signal_table[TARGET_NSIG + 1]; 72 73 /* valid sig is between 1 and _NSIG - 1 */ 74 int host_to_target_signal(int sig) 75 { 76 if (sig < 1) { 77 return sig; 78 } 79 if (sig >= _NSIG) { 80 return TARGET_NSIG + 1; 81 } 82 return host_to_target_signal_table[sig]; 83 } 84 85 /* valid sig is between 1 and TARGET_NSIG */ 86 int target_to_host_signal(int sig) 87 { 88 if (sig < 1) { 89 return sig; 90 } 91 if (sig > TARGET_NSIG) { 92 return _NSIG; 93 } 94 return target_to_host_signal_table[sig]; 95 } 96 97 static inline void target_sigaddset(target_sigset_t *set, int signum) 98 { 99 signum--; 100 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); 101 set->sig[signum / TARGET_NSIG_BPW] |= mask; 102 } 103 104 static inline int target_sigismember(const target_sigset_t *set, int signum) 105 { 106 signum--; 107 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); 108 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0); 109 } 110 111 void host_to_target_sigset_internal(target_sigset_t *d, 112 const sigset_t *s) 113 { 114 int host_sig, target_sig; 115 target_sigemptyset(d); 116 for (host_sig = 1; host_sig < _NSIG; host_sig++) { 117 target_sig = host_to_target_signal(host_sig); 118 if (target_sig < 1 || target_sig > TARGET_NSIG) { 119 continue; 120 } 121 if (sigismember(s, host_sig)) { 122 target_sigaddset(d, target_sig); 123 } 124 } 125 } 126 127 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s) 128 { 129 target_sigset_t d1; 130 int i; 131 132 host_to_target_sigset_internal(&d1, s); 133 for(i = 0;i < TARGET_NSIG_WORDS; i++) 134 d->sig[i] = tswapal(d1.sig[i]); 135 } 136 137 void target_to_host_sigset_internal(sigset_t *d, 138 const target_sigset_t *s) 139 { 140 int host_sig, target_sig; 141 sigemptyset(d); 142 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) { 143 host_sig = target_to_host_signal(target_sig); 144 if (host_sig < 1 || host_sig >= _NSIG) { 145 continue; 146 } 147 if (target_sigismember(s, target_sig)) { 148 sigaddset(d, host_sig); 149 } 150 } 151 } 152 153 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s) 154 { 155 target_sigset_t s1; 156 int i; 157 158 for(i = 0;i < TARGET_NSIG_WORDS; i++) 159 s1.sig[i] = tswapal(s->sig[i]); 160 target_to_host_sigset_internal(d, &s1); 161 } 162 163 void host_to_target_old_sigset(abi_ulong *old_sigset, 164 const sigset_t *sigset) 165 { 166 target_sigset_t d; 167 host_to_target_sigset(&d, sigset); 168 *old_sigset = d.sig[0]; 169 } 170 171 void target_to_host_old_sigset(sigset_t *sigset, 172 const abi_ulong *old_sigset) 173 { 174 target_sigset_t d; 175 int i; 176 177 d.sig[0] = *old_sigset; 178 for(i = 1;i < TARGET_NSIG_WORDS; i++) 179 d.sig[i] = 0; 180 target_to_host_sigset(sigset, &d); 181 } 182 183 int block_signals(void) 184 { 185 TaskState *ts = get_task_state(thread_cpu); 186 sigset_t set; 187 188 /* It's OK to block everything including SIGSEGV, because we won't 189 * run any further guest code before unblocking signals in 190 * process_pending_signals(). 191 */ 192 sigfillset(&set); 193 sigprocmask(SIG_SETMASK, &set, 0); 194 195 return qatomic_xchg(&ts->signal_pending, 1); 196 } 197 198 /* Wrapper for sigprocmask function 199 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset 200 * are host signal set, not guest ones. Returns -QEMU_ERESTARTSYS if 201 * a signal was already pending and the syscall must be restarted, or 202 * 0 on success. 203 * If set is NULL, this is guaranteed not to fail. 204 */ 205 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset) 206 { 207 TaskState *ts = get_task_state(thread_cpu); 208 209 if (oldset) { 210 *oldset = ts->signal_mask; 211 } 212 213 if (set) { 214 int i; 215 216 if (block_signals()) { 217 return -QEMU_ERESTARTSYS; 218 } 219 220 switch (how) { 221 case SIG_BLOCK: 222 sigorset(&ts->signal_mask, &ts->signal_mask, set); 223 break; 224 case SIG_UNBLOCK: 225 for (i = 1; i <= NSIG; ++i) { 226 if (sigismember(set, i)) { 227 sigdelset(&ts->signal_mask, i); 228 } 229 } 230 break; 231 case SIG_SETMASK: 232 ts->signal_mask = *set; 233 break; 234 default: 235 g_assert_not_reached(); 236 } 237 238 /* Silently ignore attempts to change blocking status of KILL or STOP */ 239 sigdelset(&ts->signal_mask, SIGKILL); 240 sigdelset(&ts->signal_mask, SIGSTOP); 241 } 242 return 0; 243 } 244 245 /* Just set the guest's signal mask to the specified value; the 246 * caller is assumed to have called block_signals() already. 247 */ 248 void set_sigmask(const sigset_t *set) 249 { 250 TaskState *ts = get_task_state(thread_cpu); 251 252 ts->signal_mask = *set; 253 } 254 255 /* sigaltstack management */ 256 257 int on_sig_stack(unsigned long sp) 258 { 259 TaskState *ts = get_task_state(thread_cpu); 260 261 return (sp - ts->sigaltstack_used.ss_sp 262 < ts->sigaltstack_used.ss_size); 263 } 264 265 int sas_ss_flags(unsigned long sp) 266 { 267 TaskState *ts = get_task_state(thread_cpu); 268 269 return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE 270 : on_sig_stack(sp) ? SS_ONSTACK : 0); 271 } 272 273 abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka) 274 { 275 /* 276 * This is the X/Open sanctioned signal stack switching. 277 */ 278 TaskState *ts = get_task_state(thread_cpu); 279 280 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) { 281 return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size; 282 } 283 return sp; 284 } 285 286 void target_save_altstack(target_stack_t *uss, CPUArchState *env) 287 { 288 TaskState *ts = get_task_state(thread_cpu); 289 290 __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp); 291 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags); 292 __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size); 293 } 294 295 abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env) 296 { 297 TaskState *ts = get_task_state(thread_cpu); 298 size_t minstacksize = TARGET_MINSIGSTKSZ; 299 target_stack_t ss; 300 301 #if defined(TARGET_PPC64) 302 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */ 303 struct image_info *image = ts->info; 304 if (get_ppc64_abi(image) > 1) { 305 minstacksize = 4096; 306 } 307 #endif 308 309 __get_user(ss.ss_sp, &uss->ss_sp); 310 __get_user(ss.ss_size, &uss->ss_size); 311 __get_user(ss.ss_flags, &uss->ss_flags); 312 313 if (on_sig_stack(get_sp_from_cpustate(env))) { 314 return -TARGET_EPERM; 315 } 316 317 switch (ss.ss_flags) { 318 default: 319 return -TARGET_EINVAL; 320 321 case TARGET_SS_DISABLE: 322 ss.ss_size = 0; 323 ss.ss_sp = 0; 324 break; 325 326 case TARGET_SS_ONSTACK: 327 case 0: 328 if (ss.ss_size < minstacksize) { 329 return -TARGET_ENOMEM; 330 } 331 break; 332 } 333 334 ts->sigaltstack_used.ss_sp = ss.ss_sp; 335 ts->sigaltstack_used.ss_size = ss.ss_size; 336 return 0; 337 } 338 339 /* siginfo conversion */ 340 341 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, 342 const siginfo_t *info) 343 { 344 int sig = host_to_target_signal(info->si_signo); 345 int si_code = info->si_code; 346 int si_type; 347 tinfo->si_signo = sig; 348 tinfo->si_errno = 0; 349 tinfo->si_code = info->si_code; 350 351 /* This memset serves two purposes: 352 * (1) ensure we don't leak random junk to the guest later 353 * (2) placate false positives from gcc about fields 354 * being used uninitialized if it chooses to inline both this 355 * function and tswap_siginfo() into host_to_target_siginfo(). 356 */ 357 memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad)); 358 359 /* This is awkward, because we have to use a combination of 360 * the si_code and si_signo to figure out which of the union's 361 * members are valid. (Within the host kernel it is always possible 362 * to tell, but the kernel carefully avoids giving userspace the 363 * high 16 bits of si_code, so we don't have the information to 364 * do this the easy way...) We therefore make our best guess, 365 * bearing in mind that a guest can spoof most of the si_codes 366 * via rt_sigqueueinfo() if it likes. 367 * 368 * Once we have made our guess, we record it in the top 16 bits of 369 * the si_code, so that tswap_siginfo() later can use it. 370 * tswap_siginfo() will strip these top bits out before writing 371 * si_code to the guest (sign-extending the lower bits). 372 */ 373 374 switch (si_code) { 375 case SI_USER: 376 case SI_TKILL: 377 case SI_KERNEL: 378 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel. 379 * These are the only unspoofable si_code values. 380 */ 381 tinfo->_sifields._kill._pid = info->si_pid; 382 tinfo->_sifields._kill._uid = info->si_uid; 383 si_type = QEMU_SI_KILL; 384 break; 385 default: 386 /* Everything else is spoofable. Make best guess based on signal */ 387 switch (sig) { 388 case TARGET_SIGCHLD: 389 tinfo->_sifields._sigchld._pid = info->si_pid; 390 tinfo->_sifields._sigchld._uid = info->si_uid; 391 if (si_code == CLD_EXITED) 392 tinfo->_sifields._sigchld._status = info->si_status; 393 else 394 tinfo->_sifields._sigchld._status 395 = host_to_target_signal(info->si_status & 0x7f) 396 | (info->si_status & ~0x7f); 397 tinfo->_sifields._sigchld._utime = info->si_utime; 398 tinfo->_sifields._sigchld._stime = info->si_stime; 399 si_type = QEMU_SI_CHLD; 400 break; 401 case TARGET_SIGIO: 402 tinfo->_sifields._sigpoll._band = info->si_band; 403 tinfo->_sifields._sigpoll._fd = info->si_fd; 404 si_type = QEMU_SI_POLL; 405 break; 406 default: 407 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */ 408 tinfo->_sifields._rt._pid = info->si_pid; 409 tinfo->_sifields._rt._uid = info->si_uid; 410 /* XXX: potential problem if 64 bit */ 411 tinfo->_sifields._rt._sigval.sival_ptr 412 = (abi_ulong)(unsigned long)info->si_value.sival_ptr; 413 si_type = QEMU_SI_RT; 414 break; 415 } 416 break; 417 } 418 419 tinfo->si_code = deposit32(si_code, 16, 16, si_type); 420 } 421 422 static void tswap_siginfo(target_siginfo_t *tinfo, 423 const target_siginfo_t *info) 424 { 425 int si_type = extract32(info->si_code, 16, 16); 426 int si_code = sextract32(info->si_code, 0, 16); 427 428 __put_user(info->si_signo, &tinfo->si_signo); 429 __put_user(info->si_errno, &tinfo->si_errno); 430 __put_user(si_code, &tinfo->si_code); 431 432 /* We can use our internal marker of which fields in the structure 433 * are valid, rather than duplicating the guesswork of 434 * host_to_target_siginfo_noswap() here. 435 */ 436 switch (si_type) { 437 case QEMU_SI_KILL: 438 __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid); 439 __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid); 440 break; 441 case QEMU_SI_TIMER: 442 __put_user(info->_sifields._timer._timer1, 443 &tinfo->_sifields._timer._timer1); 444 __put_user(info->_sifields._timer._timer2, 445 &tinfo->_sifields._timer._timer2); 446 break; 447 case QEMU_SI_POLL: 448 __put_user(info->_sifields._sigpoll._band, 449 &tinfo->_sifields._sigpoll._band); 450 __put_user(info->_sifields._sigpoll._fd, 451 &tinfo->_sifields._sigpoll._fd); 452 break; 453 case QEMU_SI_FAULT: 454 __put_user(info->_sifields._sigfault._addr, 455 &tinfo->_sifields._sigfault._addr); 456 break; 457 case QEMU_SI_CHLD: 458 __put_user(info->_sifields._sigchld._pid, 459 &tinfo->_sifields._sigchld._pid); 460 __put_user(info->_sifields._sigchld._uid, 461 &tinfo->_sifields._sigchld._uid); 462 __put_user(info->_sifields._sigchld._status, 463 &tinfo->_sifields._sigchld._status); 464 __put_user(info->_sifields._sigchld._utime, 465 &tinfo->_sifields._sigchld._utime); 466 __put_user(info->_sifields._sigchld._stime, 467 &tinfo->_sifields._sigchld._stime); 468 break; 469 case QEMU_SI_RT: 470 __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid); 471 __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid); 472 __put_user(info->_sifields._rt._sigval.sival_ptr, 473 &tinfo->_sifields._rt._sigval.sival_ptr); 474 break; 475 default: 476 g_assert_not_reached(); 477 } 478 } 479 480 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info) 481 { 482 target_siginfo_t tgt_tmp; 483 host_to_target_siginfo_noswap(&tgt_tmp, info); 484 tswap_siginfo(tinfo, &tgt_tmp); 485 } 486 487 /* XXX: we support only POSIX RT signals are used. */ 488 /* XXX: find a solution for 64 bit (additional malloced data is needed) */ 489 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo) 490 { 491 /* This conversion is used only for the rt_sigqueueinfo syscall, 492 * and so we know that the _rt fields are the valid ones. 493 */ 494 abi_ulong sival_ptr; 495 496 __get_user(info->si_signo, &tinfo->si_signo); 497 __get_user(info->si_errno, &tinfo->si_errno); 498 __get_user(info->si_code, &tinfo->si_code); 499 __get_user(info->si_pid, &tinfo->_sifields._rt._pid); 500 __get_user(info->si_uid, &tinfo->_sifields._rt._uid); 501 __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr); 502 info->si_value.sival_ptr = (void *)(long)sival_ptr; 503 } 504 505 /* returns 1 if given signal should dump core if not handled */ 506 static int core_dump_signal(int sig) 507 { 508 switch (sig) { 509 case TARGET_SIGABRT: 510 case TARGET_SIGFPE: 511 case TARGET_SIGILL: 512 case TARGET_SIGQUIT: 513 case TARGET_SIGSEGV: 514 case TARGET_SIGTRAP: 515 case TARGET_SIGBUS: 516 return (1); 517 default: 518 return (0); 519 } 520 } 521 522 int host_interrupt_signal; 523 524 static void signal_table_init(const char *rtsig_map) 525 { 526 int hsig, tsig, count; 527 528 if (rtsig_map) { 529 /* 530 * Map host RT signals to target RT signals according to the 531 * user-provided specification. 532 */ 533 const char *s = rtsig_map; 534 535 while (true) { 536 int i; 537 538 if (qemu_strtoi(s, &s, 10, &tsig) || *s++ != ' ') { 539 fprintf(stderr, "Malformed target signal in QEMU_RTSIG_MAP\n"); 540 exit(EXIT_FAILURE); 541 } 542 if (qemu_strtoi(s, &s, 10, &hsig) || *s++ != ' ') { 543 fprintf(stderr, "Malformed host signal in QEMU_RTSIG_MAP\n"); 544 exit(EXIT_FAILURE); 545 } 546 if (qemu_strtoi(s, &s, 10, &count) || (*s && *s != ',')) { 547 fprintf(stderr, "Malformed signal count in QEMU_RTSIG_MAP\n"); 548 exit(EXIT_FAILURE); 549 } 550 551 for (i = 0; i < count; i++, tsig++, hsig++) { 552 if (tsig < TARGET_SIGRTMIN || tsig > TARGET_NSIG) { 553 fprintf(stderr, "%d is not a target rt signal\n", tsig); 554 exit(EXIT_FAILURE); 555 } 556 if (hsig < SIGRTMIN || hsig > SIGRTMAX) { 557 fprintf(stderr, "%d is not a host rt signal\n", hsig); 558 exit(EXIT_FAILURE); 559 } 560 if (host_to_target_signal_table[hsig]) { 561 fprintf(stderr, "%d already maps %d\n", 562 hsig, host_to_target_signal_table[hsig]); 563 exit(EXIT_FAILURE); 564 } 565 host_to_target_signal_table[hsig] = tsig; 566 } 567 568 if (*s) { 569 s++; 570 } else { 571 break; 572 } 573 } 574 } else { 575 /* 576 * Default host-to-target RT signal mapping. 577 * 578 * Signals are supported starting from TARGET_SIGRTMIN and going up 579 * until we run out of host realtime signals. Glibc uses the lower 2 580 * RT signals and (hopefully) nobody uses the upper ones. 581 * This is why SIGRTMIN (34) is generally greater than __SIGRTMIN (32). 582 * To fix this properly we would need to do manual signal delivery 583 * multiplexed over a single host signal. 584 * Attempts for configure "missing" signals via sigaction will be 585 * silently ignored. 586 * 587 * Reserve two signals for internal usage (see below). 588 */ 589 590 hsig = SIGRTMIN + 2; 591 for (tsig = TARGET_SIGRTMIN; 592 hsig <= SIGRTMAX && tsig <= TARGET_NSIG; 593 hsig++, tsig++) { 594 host_to_target_signal_table[hsig] = tsig; 595 } 596 } 597 598 /* 599 * Remap the target SIGABRT, so that we can distinguish host abort 600 * from guest abort. When the guest registers a signal handler or 601 * calls raise(SIGABRT), the host will raise SIG_RTn. If the guest 602 * arrives at dump_core_and_abort(), we will map back to host SIGABRT 603 * so that the parent (native or emulated) sees the correct signal. 604 * Finally, also map host to guest SIGABRT so that the emulated 605 * parent sees the correct mapping from wait status. 606 */ 607 608 host_to_target_signal_table[SIGABRT] = 0; 609 for (hsig = SIGRTMIN; hsig <= SIGRTMAX; hsig++) { 610 if (!host_to_target_signal_table[hsig]) { 611 if (host_interrupt_signal) { 612 host_to_target_signal_table[hsig] = TARGET_SIGABRT; 613 break; 614 } else { 615 host_interrupt_signal = hsig; 616 } 617 } 618 } 619 if (hsig > SIGRTMAX) { 620 fprintf(stderr, 621 "No rt signals left for interrupt and SIGABRT mapping\n"); 622 exit(EXIT_FAILURE); 623 } 624 625 /* Invert the mapping that has already been assigned. */ 626 for (hsig = 1; hsig < _NSIG; hsig++) { 627 tsig = host_to_target_signal_table[hsig]; 628 if (tsig) { 629 if (target_to_host_signal_table[tsig]) { 630 fprintf(stderr, "%d is already mapped to %d\n", 631 tsig, target_to_host_signal_table[tsig]); 632 exit(EXIT_FAILURE); 633 } 634 target_to_host_signal_table[tsig] = hsig; 635 } 636 } 637 638 host_to_target_signal_table[SIGABRT] = TARGET_SIGABRT; 639 640 /* Map everything else out-of-bounds. */ 641 for (hsig = 1; hsig < _NSIG; hsig++) { 642 if (host_to_target_signal_table[hsig] == 0) { 643 host_to_target_signal_table[hsig] = TARGET_NSIG + 1; 644 } 645 } 646 for (count = 0, tsig = 1; tsig <= TARGET_NSIG; tsig++) { 647 if (target_to_host_signal_table[tsig] == 0) { 648 target_to_host_signal_table[tsig] = _NSIG; 649 count++; 650 } 651 } 652 653 trace_signal_table_init(count); 654 } 655 656 void signal_init(const char *rtsig_map) 657 { 658 TaskState *ts = get_task_state(thread_cpu); 659 struct sigaction act, oact; 660 661 /* initialize signal conversion tables */ 662 signal_table_init(rtsig_map); 663 664 /* Set the signal mask from the host mask. */ 665 sigprocmask(0, 0, &ts->signal_mask); 666 667 sigfillset(&act.sa_mask); 668 act.sa_flags = SA_SIGINFO; 669 act.sa_sigaction = host_signal_handler; 670 671 /* 672 * A parent process may configure ignored signals, but all other 673 * signals are default. For any target signals that have no host 674 * mapping, set to ignore. For all core_dump_signal, install our 675 * host signal handler so that we may invoke dump_core_and_abort. 676 * This includes SIGSEGV and SIGBUS, which are also need our signal 677 * handler for paging and exceptions. 678 */ 679 for (int tsig = 1; tsig <= TARGET_NSIG; tsig++) { 680 int hsig = target_to_host_signal(tsig); 681 abi_ptr thand = TARGET_SIG_IGN; 682 683 if (hsig >= _NSIG) { 684 continue; 685 } 686 687 /* As we force remap SIGABRT, cannot probe and install in one step. */ 688 if (tsig == TARGET_SIGABRT) { 689 sigaction(SIGABRT, NULL, &oact); 690 sigaction(hsig, &act, NULL); 691 } else { 692 struct sigaction *iact = core_dump_signal(tsig) ? &act : NULL; 693 sigaction(hsig, iact, &oact); 694 } 695 696 if (oact.sa_sigaction != (void *)SIG_IGN) { 697 thand = TARGET_SIG_DFL; 698 } 699 sigact_table[tsig - 1]._sa_handler = thand; 700 } 701 702 sigaction(host_interrupt_signal, &act, NULL); 703 } 704 705 /* Force a synchronously taken signal. The kernel force_sig() function 706 * also forces the signal to "not blocked, not ignored", but for QEMU 707 * that work is done in process_pending_signals(). 708 */ 709 void force_sig(int sig) 710 { 711 CPUState *cpu = thread_cpu; 712 target_siginfo_t info = {}; 713 714 info.si_signo = sig; 715 info.si_errno = 0; 716 info.si_code = TARGET_SI_KERNEL; 717 info._sifields._kill._pid = 0; 718 info._sifields._kill._uid = 0; 719 queue_signal(cpu_env(cpu), info.si_signo, QEMU_SI_KILL, &info); 720 } 721 722 /* 723 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the 724 * 'force' part is handled in process_pending_signals(). 725 */ 726 void force_sig_fault(int sig, int code, abi_ulong addr) 727 { 728 CPUState *cpu = thread_cpu; 729 target_siginfo_t info = {}; 730 731 info.si_signo = sig; 732 info.si_errno = 0; 733 info.si_code = code; 734 info._sifields._sigfault._addr = addr; 735 queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, &info); 736 } 737 738 /* Force a SIGSEGV if we couldn't write to memory trying to set 739 * up the signal frame. oldsig is the signal we were trying to handle 740 * at the point of failure. 741 */ 742 #if !defined(TARGET_RISCV) 743 void force_sigsegv(int oldsig) 744 { 745 if (oldsig == SIGSEGV) { 746 /* Make sure we don't try to deliver the signal again; this will 747 * end up with handle_pending_signal() calling dump_core_and_abort(). 748 */ 749 sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL; 750 } 751 force_sig(TARGET_SIGSEGV); 752 } 753 #endif 754 755 void cpu_loop_exit_sigsegv(CPUState *cpu, vaddr addr, 756 MMUAccessType access_type, bool maperr, uintptr_t ra) 757 { 758 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops; 759 760 if (tcg_ops->record_sigsegv) { 761 tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra); 762 } 763 764 force_sig_fault(TARGET_SIGSEGV, 765 maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR, 766 addr); 767 cpu->exception_index = EXCP_INTERRUPT; 768 cpu_loop_exit_restore(cpu, ra); 769 } 770 771 void cpu_loop_exit_sigbus(CPUState *cpu, vaddr addr, 772 MMUAccessType access_type, uintptr_t ra) 773 { 774 const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops; 775 776 if (tcg_ops->record_sigbus) { 777 tcg_ops->record_sigbus(cpu, addr, access_type, ra); 778 } 779 780 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr); 781 cpu->exception_index = EXCP_INTERRUPT; 782 cpu_loop_exit_restore(cpu, ra); 783 } 784 785 /* abort execution with signal */ 786 static G_NORETURN 787 void die_with_signal(int host_sig) 788 { 789 struct sigaction act = { 790 .sa_handler = SIG_DFL, 791 }; 792 793 /* 794 * The proper exit code for dying from an uncaught signal is -<signal>. 795 * The kernel doesn't allow exit() or _exit() to pass a negative value. 796 * To get the proper exit code we need to actually die from an uncaught 797 * signal. Here the default signal handler is installed, we send 798 * the signal and we wait for it to arrive. 799 */ 800 sigfillset(&act.sa_mask); 801 sigaction(host_sig, &act, NULL); 802 803 kill(getpid(), host_sig); 804 805 /* Make sure the signal isn't masked (reusing the mask inside of act). */ 806 sigdelset(&act.sa_mask, host_sig); 807 sigsuspend(&act.sa_mask); 808 809 /* unreachable */ 810 _exit(EXIT_FAILURE); 811 } 812 813 static G_NORETURN 814 void dump_core_and_abort(CPUArchState *env, int target_sig) 815 { 816 CPUState *cpu = env_cpu(env); 817 TaskState *ts = get_task_state(cpu); 818 int host_sig, core_dumped = 0; 819 820 /* On exit, undo the remapping of SIGABRT. */ 821 if (target_sig == TARGET_SIGABRT) { 822 host_sig = SIGABRT; 823 } else { 824 host_sig = target_to_host_signal(target_sig); 825 } 826 trace_user_dump_core_and_abort(env, target_sig, host_sig); 827 gdb_signalled(env, target_sig); 828 829 /* dump core if supported by target binary format */ 830 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) { 831 stop_all_tasks(); 832 core_dumped = 833 ((*ts->bprm->core_dump)(target_sig, env) == 0); 834 } 835 if (core_dumped) { 836 /* we already dumped the core of target process, we don't want 837 * a coredump of qemu itself */ 838 struct rlimit nodump; 839 getrlimit(RLIMIT_CORE, &nodump); 840 nodump.rlim_cur=0; 841 setrlimit(RLIMIT_CORE, &nodump); 842 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n", 843 target_sig, strsignal(host_sig), "core dumped" ); 844 } 845 846 preexit_cleanup(env, 128 + target_sig); 847 die_with_signal(host_sig); 848 } 849 850 /* queue a signal so that it will be send to the virtual CPU as soon 851 as possible */ 852 void queue_signal(CPUArchState *env, int sig, int si_type, 853 target_siginfo_t *info) 854 { 855 CPUState *cpu = env_cpu(env); 856 TaskState *ts = get_task_state(cpu); 857 858 trace_user_queue_signal(env, sig); 859 860 info->si_code = deposit32(info->si_code, 16, 16, si_type); 861 862 ts->sync_signal.info = *info; 863 ts->sync_signal.pending = sig; 864 /* signal that a new signal is pending */ 865 qatomic_set(&ts->signal_pending, 1); 866 } 867 868 869 /* Adjust the signal context to rewind out of safe-syscall if we're in it */ 870 static inline void rewind_if_in_safe_syscall(void *puc) 871 { 872 host_sigcontext *uc = (host_sigcontext *)puc; 873 uintptr_t pcreg = host_signal_pc(uc); 874 875 if (pcreg > (uintptr_t)safe_syscall_start 876 && pcreg < (uintptr_t)safe_syscall_end) { 877 host_signal_set_pc(uc, (uintptr_t)safe_syscall_start); 878 } 879 } 880 881 static G_NORETURN 882 void die_from_signal(siginfo_t *info) 883 { 884 char sigbuf[4], codebuf[12]; 885 const char *sig, *code = NULL; 886 887 switch (info->si_signo) { 888 case SIGSEGV: 889 sig = "SEGV"; 890 switch (info->si_code) { 891 case SEGV_MAPERR: 892 code = "MAPERR"; 893 break; 894 case SEGV_ACCERR: 895 code = "ACCERR"; 896 break; 897 } 898 break; 899 case SIGBUS: 900 sig = "BUS"; 901 switch (info->si_code) { 902 case BUS_ADRALN: 903 code = "ADRALN"; 904 break; 905 case BUS_ADRERR: 906 code = "ADRERR"; 907 break; 908 } 909 break; 910 case SIGILL: 911 sig = "ILL"; 912 switch (info->si_code) { 913 case ILL_ILLOPC: 914 code = "ILLOPC"; 915 break; 916 case ILL_ILLOPN: 917 code = "ILLOPN"; 918 break; 919 case ILL_ILLADR: 920 code = "ILLADR"; 921 break; 922 case ILL_PRVOPC: 923 code = "PRVOPC"; 924 break; 925 case ILL_PRVREG: 926 code = "PRVREG"; 927 break; 928 case ILL_COPROC: 929 code = "COPROC"; 930 break; 931 } 932 break; 933 case SIGFPE: 934 sig = "FPE"; 935 switch (info->si_code) { 936 case FPE_INTDIV: 937 code = "INTDIV"; 938 break; 939 case FPE_INTOVF: 940 code = "INTOVF"; 941 break; 942 } 943 break; 944 case SIGTRAP: 945 sig = "TRAP"; 946 break; 947 default: 948 snprintf(sigbuf, sizeof(sigbuf), "%d", info->si_signo); 949 sig = sigbuf; 950 break; 951 } 952 if (code == NULL) { 953 snprintf(codebuf, sizeof(sigbuf), "%d", info->si_code); 954 code = codebuf; 955 } 956 957 error_report("QEMU internal SIG%s {code=%s, addr=%p}", 958 sig, code, info->si_addr); 959 die_with_signal(info->si_signo); 960 } 961 962 static void host_sigsegv_handler(CPUState *cpu, siginfo_t *info, 963 host_sigcontext *uc) 964 { 965 uintptr_t host_addr = (uintptr_t)info->si_addr; 966 /* 967 * Convert forcefully to guest address space: addresses outside 968 * reserved_va are still valid to report via SEGV_MAPERR. 969 */ 970 bool is_valid = h2g_valid(host_addr); 971 abi_ptr guest_addr = h2g_nocheck(host_addr); 972 uintptr_t pc = host_signal_pc(uc); 973 bool is_write = host_signal_write(info, uc); 974 MMUAccessType access_type = adjust_signal_pc(&pc, is_write); 975 bool maperr; 976 977 /* If this was a write to a TB protected page, restart. */ 978 if (is_write 979 && is_valid 980 && info->si_code == SEGV_ACCERR 981 && handle_sigsegv_accerr_write(cpu, host_signal_mask(uc), 982 pc, guest_addr)) { 983 return; 984 } 985 986 /* 987 * If the access was not on behalf of the guest, within the executable 988 * mapping of the generated code buffer, then it is a host bug. 989 */ 990 if (access_type != MMU_INST_FETCH 991 && !in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) { 992 die_from_signal(info); 993 } 994 995 maperr = true; 996 if (is_valid && info->si_code == SEGV_ACCERR) { 997 /* 998 * With reserved_va, the whole address space is PROT_NONE, 999 * which means that we may get ACCERR when we want MAPERR. 1000 */ 1001 if (page_get_flags(guest_addr) & PAGE_VALID) { 1002 maperr = false; 1003 } else { 1004 info->si_code = SEGV_MAPERR; 1005 } 1006 } 1007 1008 sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL); 1009 cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc); 1010 } 1011 1012 static uintptr_t host_sigbus_handler(CPUState *cpu, siginfo_t *info, 1013 host_sigcontext *uc) 1014 { 1015 uintptr_t pc = host_signal_pc(uc); 1016 bool is_write = host_signal_write(info, uc); 1017 MMUAccessType access_type = adjust_signal_pc(&pc, is_write); 1018 1019 /* 1020 * If the access was not on behalf of the guest, within the executable 1021 * mapping of the generated code buffer, then it is a host bug. 1022 */ 1023 if (!in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) { 1024 die_from_signal(info); 1025 } 1026 1027 if (info->si_code == BUS_ADRALN) { 1028 uintptr_t host_addr = (uintptr_t)info->si_addr; 1029 abi_ptr guest_addr = h2g_nocheck(host_addr); 1030 1031 sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL); 1032 cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc); 1033 } 1034 return pc; 1035 } 1036 1037 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) 1038 { 1039 CPUState *cpu = thread_cpu; 1040 CPUArchState *env = cpu_env(cpu); 1041 TaskState *ts = get_task_state(cpu); 1042 target_siginfo_t tinfo; 1043 host_sigcontext *uc = puc; 1044 struct emulated_sigtable *k; 1045 int guest_sig; 1046 uintptr_t pc = 0; 1047 bool sync_sig = false; 1048 void *sigmask; 1049 1050 if (host_sig == host_interrupt_signal) { 1051 ts->signal_pending = 1; 1052 cpu_exit(thread_cpu); 1053 return; 1054 } 1055 1056 /* 1057 * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special 1058 * handling wrt signal blocking and unwinding. Non-spoofed SIGILL, 1059 * SIGFPE, SIGTRAP are always host bugs. 1060 */ 1061 if (info->si_code > 0) { 1062 switch (host_sig) { 1063 case SIGSEGV: 1064 /* Only returns on handle_sigsegv_accerr_write success. */ 1065 host_sigsegv_handler(cpu, info, uc); 1066 return; 1067 case SIGBUS: 1068 pc = host_sigbus_handler(cpu, info, uc); 1069 sync_sig = true; 1070 break; 1071 case SIGILL: 1072 case SIGFPE: 1073 case SIGTRAP: 1074 die_from_signal(info); 1075 } 1076 } 1077 1078 /* get target signal number */ 1079 guest_sig = host_to_target_signal(host_sig); 1080 if (guest_sig < 1 || guest_sig > TARGET_NSIG) { 1081 return; 1082 } 1083 trace_user_host_signal(env, host_sig, guest_sig); 1084 1085 host_to_target_siginfo_noswap(&tinfo, info); 1086 k = &ts->sigtab[guest_sig - 1]; 1087 k->info = tinfo; 1088 k->pending = guest_sig; 1089 ts->signal_pending = 1; 1090 1091 /* 1092 * For synchronous signals, unwind the cpu state to the faulting 1093 * insn and then exit back to the main loop so that the signal 1094 * is delivered immediately. 1095 */ 1096 if (sync_sig) { 1097 cpu->exception_index = EXCP_INTERRUPT; 1098 cpu_loop_exit_restore(cpu, pc); 1099 } 1100 1101 rewind_if_in_safe_syscall(puc); 1102 1103 /* 1104 * Block host signals until target signal handler entered. We 1105 * can't block SIGSEGV or SIGBUS while we're executing guest 1106 * code in case the guest code provokes one in the window between 1107 * now and it getting out to the main loop. Signals will be 1108 * unblocked again in process_pending_signals(). 1109 * 1110 * WARNING: we cannot use sigfillset() here because the sigmask 1111 * field is a kernel sigset_t, which is much smaller than the 1112 * libc sigset_t which sigfillset() operates on. Using sigfillset() 1113 * would write 0xff bytes off the end of the structure and trash 1114 * data on the struct. 1115 */ 1116 sigmask = host_signal_mask(uc); 1117 memset(sigmask, 0xff, SIGSET_T_SIZE); 1118 sigdelset(sigmask, SIGSEGV); 1119 sigdelset(sigmask, SIGBUS); 1120 1121 /* interrupt the virtual CPU as soon as possible */ 1122 cpu_exit(thread_cpu); 1123 } 1124 1125 /* do_sigaltstack() returns target values and errnos. */ 1126 /* compare linux/kernel/signal.c:do_sigaltstack() */ 1127 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, 1128 CPUArchState *env) 1129 { 1130 target_stack_t oss, *uoss = NULL; 1131 abi_long ret = -TARGET_EFAULT; 1132 1133 if (uoss_addr) { 1134 /* Verify writability now, but do not alter user memory yet. */ 1135 if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) { 1136 goto out; 1137 } 1138 target_save_altstack(&oss, env); 1139 } 1140 1141 if (uss_addr) { 1142 target_stack_t *uss; 1143 1144 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) { 1145 goto out; 1146 } 1147 ret = target_restore_altstack(uss, env); 1148 if (ret) { 1149 goto out; 1150 } 1151 } 1152 1153 if (uoss_addr) { 1154 memcpy(uoss, &oss, sizeof(oss)); 1155 unlock_user_struct(uoss, uoss_addr, 1); 1156 uoss = NULL; 1157 } 1158 ret = 0; 1159 1160 out: 1161 if (uoss) { 1162 unlock_user_struct(uoss, uoss_addr, 0); 1163 } 1164 return ret; 1165 } 1166 1167 /* do_sigaction() return target values and host errnos */ 1168 int do_sigaction(int sig, const struct target_sigaction *act, 1169 struct target_sigaction *oact, abi_ulong ka_restorer) 1170 { 1171 struct target_sigaction *k; 1172 int host_sig; 1173 int ret = 0; 1174 1175 trace_signal_do_sigaction_guest(sig, TARGET_NSIG); 1176 1177 if (sig < 1 || sig > TARGET_NSIG) { 1178 return -TARGET_EINVAL; 1179 } 1180 1181 if (act && (sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)) { 1182 return -TARGET_EINVAL; 1183 } 1184 1185 if (block_signals()) { 1186 return -QEMU_ERESTARTSYS; 1187 } 1188 1189 k = &sigact_table[sig - 1]; 1190 if (oact) { 1191 __put_user(k->_sa_handler, &oact->_sa_handler); 1192 __put_user(k->sa_flags, &oact->sa_flags); 1193 #ifdef TARGET_ARCH_HAS_SA_RESTORER 1194 __put_user(k->sa_restorer, &oact->sa_restorer); 1195 #endif 1196 /* Not swapped. */ 1197 oact->sa_mask = k->sa_mask; 1198 } 1199 if (act) { 1200 __get_user(k->_sa_handler, &act->_sa_handler); 1201 __get_user(k->sa_flags, &act->sa_flags); 1202 #ifdef TARGET_ARCH_HAS_SA_RESTORER 1203 __get_user(k->sa_restorer, &act->sa_restorer); 1204 #endif 1205 #ifdef TARGET_ARCH_HAS_KA_RESTORER 1206 k->ka_restorer = ka_restorer; 1207 #endif 1208 /* To be swapped in target_to_host_sigset. */ 1209 k->sa_mask = act->sa_mask; 1210 1211 /* we update the host linux signal state */ 1212 host_sig = target_to_host_signal(sig); 1213 trace_signal_do_sigaction_host(host_sig, TARGET_NSIG); 1214 if (host_sig > SIGRTMAX) { 1215 /* we don't have enough host signals to map all target signals */ 1216 qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n", 1217 sig); 1218 /* 1219 * we don't return an error here because some programs try to 1220 * register an handler for all possible rt signals even if they 1221 * don't need it. 1222 * An error here can abort them whereas there can be no problem 1223 * to not have the signal available later. 1224 * This is the case for golang, 1225 * See https://github.com/golang/go/issues/33746 1226 * So we silently ignore the error. 1227 */ 1228 return 0; 1229 } 1230 if (host_sig != SIGSEGV && host_sig != SIGBUS) { 1231 struct sigaction act1; 1232 1233 sigfillset(&act1.sa_mask); 1234 act1.sa_flags = SA_SIGINFO; 1235 if (k->_sa_handler == TARGET_SIG_IGN) { 1236 /* 1237 * It is important to update the host kernel signal ignore 1238 * state to avoid getting unexpected interrupted syscalls. 1239 */ 1240 act1.sa_sigaction = (void *)SIG_IGN; 1241 } else if (k->_sa_handler == TARGET_SIG_DFL) { 1242 if (core_dump_signal(sig)) { 1243 act1.sa_sigaction = host_signal_handler; 1244 } else { 1245 act1.sa_sigaction = (void *)SIG_DFL; 1246 } 1247 } else { 1248 act1.sa_sigaction = host_signal_handler; 1249 if (k->sa_flags & TARGET_SA_RESTART) { 1250 act1.sa_flags |= SA_RESTART; 1251 } 1252 } 1253 ret = sigaction(host_sig, &act1, NULL); 1254 } 1255 } 1256 return ret; 1257 } 1258 1259 static void handle_pending_signal(CPUArchState *cpu_env, int sig, 1260 struct emulated_sigtable *k) 1261 { 1262 CPUState *cpu = env_cpu(cpu_env); 1263 abi_ulong handler; 1264 sigset_t set; 1265 target_siginfo_t unswapped; 1266 target_sigset_t target_old_set; 1267 struct target_sigaction *sa; 1268 TaskState *ts = get_task_state(cpu); 1269 1270 trace_user_handle_signal(cpu_env, sig); 1271 /* dequeue signal */ 1272 k->pending = 0; 1273 1274 /* 1275 * Writes out siginfo values byteswapped, accordingly to the target. 1276 * It also cleans the si_type from si_code making it correct for 1277 * the target. We must hold on to the original unswapped copy for 1278 * strace below, because si_type is still required there. 1279 */ 1280 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { 1281 unswapped = k->info; 1282 } 1283 tswap_siginfo(&k->info, &k->info); 1284 1285 sig = gdb_handlesig(cpu, sig, NULL, &k->info, sizeof(k->info)); 1286 if (!sig) { 1287 sa = NULL; 1288 handler = TARGET_SIG_IGN; 1289 } else { 1290 sa = &sigact_table[sig - 1]; 1291 handler = sa->_sa_handler; 1292 } 1293 1294 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { 1295 print_taken_signal(sig, &unswapped); 1296 } 1297 1298 if (handler == TARGET_SIG_DFL) { 1299 /* default handler : ignore some signal. The other are job control or fatal */ 1300 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) { 1301 kill(getpid(),SIGSTOP); 1302 } else if (sig != TARGET_SIGCHLD && 1303 sig != TARGET_SIGURG && 1304 sig != TARGET_SIGWINCH && 1305 sig != TARGET_SIGCONT) { 1306 dump_core_and_abort(cpu_env, sig); 1307 } 1308 } else if (handler == TARGET_SIG_IGN) { 1309 /* ignore sig */ 1310 } else if (handler == TARGET_SIG_ERR) { 1311 dump_core_and_abort(cpu_env, sig); 1312 } else { 1313 /* compute the blocked signals during the handler execution */ 1314 sigset_t *blocked_set; 1315 1316 target_to_host_sigset(&set, &sa->sa_mask); 1317 /* SA_NODEFER indicates that the current signal should not be 1318 blocked during the handler */ 1319 if (!(sa->sa_flags & TARGET_SA_NODEFER)) 1320 sigaddset(&set, target_to_host_signal(sig)); 1321 1322 /* save the previous blocked signal state to restore it at the 1323 end of the signal execution (see do_sigreturn) */ 1324 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask); 1325 1326 /* block signals in the handler */ 1327 blocked_set = ts->in_sigsuspend ? 1328 &ts->sigsuspend_mask : &ts->signal_mask; 1329 sigorset(&ts->signal_mask, blocked_set, &set); 1330 ts->in_sigsuspend = 0; 1331 1332 /* if the CPU is in VM86 mode, we restore the 32 bit values */ 1333 #if defined(TARGET_I386) && !defined(TARGET_X86_64) 1334 { 1335 CPUX86State *env = cpu_env; 1336 if (env->eflags & VM_MASK) 1337 save_v86_state(env); 1338 } 1339 #endif 1340 /* prepare the stack frame of the virtual CPU */ 1341 #if defined(TARGET_ARCH_HAS_SETUP_FRAME) 1342 if (sa->sa_flags & TARGET_SA_SIGINFO) { 1343 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env); 1344 } else { 1345 setup_frame(sig, sa, &target_old_set, cpu_env); 1346 } 1347 #else 1348 /* These targets do not have traditional signals. */ 1349 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env); 1350 #endif 1351 if (sa->sa_flags & TARGET_SA_RESETHAND) { 1352 sa->_sa_handler = TARGET_SIG_DFL; 1353 } 1354 } 1355 } 1356 1357 void process_pending_signals(CPUArchState *cpu_env) 1358 { 1359 CPUState *cpu = env_cpu(cpu_env); 1360 int sig; 1361 TaskState *ts = get_task_state(cpu); 1362 sigset_t set; 1363 sigset_t *blocked_set; 1364 1365 while (qatomic_read(&ts->signal_pending)) { 1366 sigfillset(&set); 1367 sigprocmask(SIG_SETMASK, &set, 0); 1368 1369 restart_scan: 1370 sig = ts->sync_signal.pending; 1371 if (sig) { 1372 /* Synchronous signals are forced, 1373 * see force_sig_info() and callers in Linux 1374 * Note that not all of our queue_signal() calls in QEMU correspond 1375 * to force_sig_info() calls in Linux (some are send_sig_info()). 1376 * However it seems like a kernel bug to me to allow the process 1377 * to block a synchronous signal since it could then just end up 1378 * looping round and round indefinitely. 1379 */ 1380 if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig]) 1381 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) { 1382 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]); 1383 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL; 1384 } 1385 1386 handle_pending_signal(cpu_env, sig, &ts->sync_signal); 1387 } 1388 1389 for (sig = 1; sig <= TARGET_NSIG; sig++) { 1390 blocked_set = ts->in_sigsuspend ? 1391 &ts->sigsuspend_mask : &ts->signal_mask; 1392 1393 if (ts->sigtab[sig - 1].pending && 1394 (!sigismember(blocked_set, 1395 target_to_host_signal_table[sig]))) { 1396 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]); 1397 /* Restart scan from the beginning, as handle_pending_signal 1398 * might have resulted in a new synchronous signal (eg SIGSEGV). 1399 */ 1400 goto restart_scan; 1401 } 1402 } 1403 1404 /* if no signal is pending, unblock signals and recheck (the act 1405 * of unblocking might cause us to take another host signal which 1406 * will set signal_pending again). 1407 */ 1408 qatomic_set(&ts->signal_pending, 0); 1409 ts->in_sigsuspend = 0; 1410 set = ts->signal_mask; 1411 sigdelset(&set, SIGSEGV); 1412 sigdelset(&set, SIGBUS); 1413 sigprocmask(SIG_SETMASK, &set, 0); 1414 } 1415 ts->in_sigsuspend = 0; 1416 } 1417 1418 int process_sigsuspend_mask(sigset_t **pset, target_ulong sigset, 1419 target_ulong sigsize) 1420 { 1421 TaskState *ts = get_task_state(thread_cpu); 1422 sigset_t *host_set = &ts->sigsuspend_mask; 1423 target_sigset_t *target_sigset; 1424 1425 if (sigsize != sizeof(*target_sigset)) { 1426 /* Like the kernel, we enforce correct size sigsets */ 1427 return -TARGET_EINVAL; 1428 } 1429 1430 target_sigset = lock_user(VERIFY_READ, sigset, sigsize, 1); 1431 if (!target_sigset) { 1432 return -TARGET_EFAULT; 1433 } 1434 target_to_host_sigset(host_set, target_sigset); 1435 unlock_user(target_sigset, sigset, 0); 1436 1437 *pset = host_set; 1438 return 0; 1439 } 1440