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