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