2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
19 #include "qemu/osdep.h"
20 #include "qemu/bitops.h"
21 #include <sys/ucontext.h>
22 #include <sys/resource.h>
25 #include "user-internals.h"
29 #include "signal-common.h"
31 static struct target_sigaction sigact_table[TARGET_NSIG];
33 static void host_signal_handler(int host_signum, siginfo_t *info,
38 * System includes define _NSIG as SIGRTMAX + 1,
39 * but qemu (like the kernel) defines TARGET_NSIG as TARGET_SIGRTMAX
40 * and the first signal is SIGHUP defined as 1
41 * Signal number 0 is reserved for use as kill(pid, 0), to test whether
42 * a process exists without sending it a signal.
45 QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG);
47 static uint8_t host_to_target_signal_table[_NSIG] = {
48 [SIGHUP] = TARGET_SIGHUP,
49 [SIGINT] = TARGET_SIGINT,
50 [SIGQUIT] = TARGET_SIGQUIT,
51 [SIGILL] = TARGET_SIGILL,
52 [SIGTRAP] = TARGET_SIGTRAP,
53 [SIGABRT] = TARGET_SIGABRT,
54 /* [SIGIOT] = TARGET_SIGIOT,*/
55 [SIGBUS] = TARGET_SIGBUS,
56 [SIGFPE] = TARGET_SIGFPE,
57 [SIGKILL] = TARGET_SIGKILL,
58 [SIGUSR1] = TARGET_SIGUSR1,
59 [SIGSEGV] = TARGET_SIGSEGV,
60 [SIGUSR2] = TARGET_SIGUSR2,
61 [SIGPIPE] = TARGET_SIGPIPE,
62 [SIGALRM] = TARGET_SIGALRM,
63 [SIGTERM] = TARGET_SIGTERM,
65 [SIGSTKFLT] = TARGET_SIGSTKFLT,
67 [SIGCHLD] = TARGET_SIGCHLD,
68 [SIGCONT] = TARGET_SIGCONT,
69 [SIGSTOP] = TARGET_SIGSTOP,
70 [SIGTSTP] = TARGET_SIGTSTP,
71 [SIGTTIN] = TARGET_SIGTTIN,
72 [SIGTTOU] = TARGET_SIGTTOU,
73 [SIGURG] = TARGET_SIGURG,
74 [SIGXCPU] = TARGET_SIGXCPU,
75 [SIGXFSZ] = TARGET_SIGXFSZ,
76 [SIGVTALRM] = TARGET_SIGVTALRM,
77 [SIGPROF] = TARGET_SIGPROF,
78 [SIGWINCH] = TARGET_SIGWINCH,
79 [SIGIO] = TARGET_SIGIO,
80 [SIGPWR] = TARGET_SIGPWR,
81 [SIGSYS] = TARGET_SIGSYS,
82 /* next signals stay the same */
85 static uint8_t target_to_host_signal_table[TARGET_NSIG + 1];
87 /* valid sig is between 1 and _NSIG - 1 */
88 int host_to_target_signal(int sig)
90 if (sig < 1 || sig >= _NSIG) {
93 return host_to_target_signal_table[sig];
96 /* valid sig is between 1 and TARGET_NSIG */
97 int target_to_host_signal(int sig)
99 if (sig < 1 || sig > TARGET_NSIG) {
102 return target_to_host_signal_table[sig];
105 static inline void target_sigaddset(target_sigset_t *set, int signum)
108 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
109 set->sig[signum / TARGET_NSIG_BPW] |= mask;
112 static inline int target_sigismember(const target_sigset_t *set, int signum)
115 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
116 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
119 void host_to_target_sigset_internal(target_sigset_t *d,
122 int host_sig, target_sig;
123 target_sigemptyset(d);
124 for (host_sig = 1; host_sig < _NSIG; host_sig++) {
125 target_sig = host_to_target_signal(host_sig);
126 if (target_sig < 1 || target_sig > TARGET_NSIG) {
129 if (sigismember(s, host_sig)) {
130 target_sigaddset(d, target_sig);
135 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
140 host_to_target_sigset_internal(&d1, s);
141 for(i = 0;i < TARGET_NSIG_WORDS; i++)
142 d->sig[i] = tswapal(d1.sig[i]);
145 void target_to_host_sigset_internal(sigset_t *d,
146 const target_sigset_t *s)
148 int host_sig, target_sig;
150 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
151 host_sig = target_to_host_signal(target_sig);
152 if (host_sig < 1 || host_sig >= _NSIG) {
155 if (target_sigismember(s, target_sig)) {
156 sigaddset(d, host_sig);
161 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
166 for(i = 0;i < TARGET_NSIG_WORDS; i++)
167 s1.sig[i] = tswapal(s->sig[i]);
168 target_to_host_sigset_internal(d, &s1);
171 void host_to_target_old_sigset(abi_ulong *old_sigset,
172 const sigset_t *sigset)
175 host_to_target_sigset(&d, sigset);
176 *old_sigset = d.sig[0];
179 void target_to_host_old_sigset(sigset_t *sigset,
180 const abi_ulong *old_sigset)
185 d.sig[0] = *old_sigset;
186 for(i = 1;i < TARGET_NSIG_WORDS; i++)
188 target_to_host_sigset(sigset, &d);
191 int block_signals(void)
193 TaskState *ts = (TaskState *)thread_cpu->opaque;
196 /* It's OK to block everything including SIGSEGV, because we won't
197 * run any further guest code before unblocking signals in
198 * process_pending_signals().
201 sigprocmask(SIG_SETMASK, &set, 0);
203 return qatomic_xchg(&ts->signal_pending, 1);
206 /* Wrapper for sigprocmask function
207 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
208 * are host signal set, not guest ones. Returns -TARGET_ERESTARTSYS if
209 * a signal was already pending and the syscall must be restarted, or
211 * If set is NULL, this is guaranteed not to fail.
213 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
215 TaskState *ts = (TaskState *)thread_cpu->opaque;
218 *oldset = ts->signal_mask;
224 if (block_signals()) {
225 return -TARGET_ERESTARTSYS;
230 sigorset(&ts->signal_mask, &ts->signal_mask, set);
233 for (i = 1; i <= NSIG; ++i) {
234 if (sigismember(set, i)) {
235 sigdelset(&ts->signal_mask, i);
240 ts->signal_mask = *set;
243 g_assert_not_reached();
246 /* Silently ignore attempts to change blocking status of KILL or STOP */
247 sigdelset(&ts->signal_mask, SIGKILL);
248 sigdelset(&ts->signal_mask, SIGSTOP);
253 #if !defined(TARGET_NIOS2)
254 /* Just set the guest's signal mask to the specified value; the
255 * caller is assumed to have called block_signals() already.
257 void set_sigmask(const sigset_t *set)
259 TaskState *ts = (TaskState *)thread_cpu->opaque;
261 ts->signal_mask = *set;
265 /* sigaltstack management */
267 int on_sig_stack(unsigned long sp)
269 TaskState *ts = (TaskState *)thread_cpu->opaque;
271 return (sp - ts->sigaltstack_used.ss_sp
272 < ts->sigaltstack_used.ss_size);
275 int sas_ss_flags(unsigned long sp)
277 TaskState *ts = (TaskState *)thread_cpu->opaque;
279 return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE
280 : on_sig_stack(sp) ? SS_ONSTACK : 0);
283 abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka)
286 * This is the X/Open sanctioned signal stack switching.
288 TaskState *ts = (TaskState *)thread_cpu->opaque;
290 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
291 return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
296 void target_save_altstack(target_stack_t *uss, CPUArchState *env)
298 TaskState *ts = (TaskState *)thread_cpu->opaque;
300 __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp);
301 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags);
302 __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
305 abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env)
307 TaskState *ts = (TaskState *)thread_cpu->opaque;
308 size_t minstacksize = TARGET_MINSIGSTKSZ;
311 #if defined(TARGET_PPC64)
312 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
313 struct image_info *image = ts->info;
314 if (get_ppc64_abi(image) > 1) {
319 __get_user(ss.ss_sp, &uss->ss_sp);
320 __get_user(ss.ss_size, &uss->ss_size);
321 __get_user(ss.ss_flags, &uss->ss_flags);
323 if (on_sig_stack(get_sp_from_cpustate(env))) {
324 return -TARGET_EPERM;
327 switch (ss.ss_flags) {
329 return -TARGET_EINVAL;
331 case TARGET_SS_DISABLE:
336 case TARGET_SS_ONSTACK:
338 if (ss.ss_size < minstacksize) {
339 return -TARGET_ENOMEM;
344 ts->sigaltstack_used.ss_sp = ss.ss_sp;
345 ts->sigaltstack_used.ss_size = ss.ss_size;
349 /* siginfo conversion */
351 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
352 const siginfo_t *info)
354 int sig = host_to_target_signal(info->si_signo);
355 int si_code = info->si_code;
357 tinfo->si_signo = sig;
359 tinfo->si_code = info->si_code;
361 /* This memset serves two purposes:
362 * (1) ensure we don't leak random junk to the guest later
363 * (2) placate false positives from gcc about fields
364 * being used uninitialized if it chooses to inline both this
365 * function and tswap_siginfo() into host_to_target_siginfo().
367 memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
369 /* This is awkward, because we have to use a combination of
370 * the si_code and si_signo to figure out which of the union's
371 * members are valid. (Within the host kernel it is always possible
372 * to tell, but the kernel carefully avoids giving userspace the
373 * high 16 bits of si_code, so we don't have the information to
374 * do this the easy way...) We therefore make our best guess,
375 * bearing in mind that a guest can spoof most of the si_codes
376 * via rt_sigqueueinfo() if it likes.
378 * Once we have made our guess, we record it in the top 16 bits of
379 * the si_code, so that tswap_siginfo() later can use it.
380 * tswap_siginfo() will strip these top bits out before writing
381 * si_code to the guest (sign-extending the lower bits).
388 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
389 * These are the only unspoofable si_code values.
391 tinfo->_sifields._kill._pid = info->si_pid;
392 tinfo->_sifields._kill._uid = info->si_uid;
393 si_type = QEMU_SI_KILL;
396 /* Everything else is spoofable. Make best guess based on signal */
399 tinfo->_sifields._sigchld._pid = info->si_pid;
400 tinfo->_sifields._sigchld._uid = info->si_uid;
401 tinfo->_sifields._sigchld._status = info->si_status;
402 tinfo->_sifields._sigchld._utime = info->si_utime;
403 tinfo->_sifields._sigchld._stime = info->si_stime;
404 si_type = QEMU_SI_CHLD;
407 tinfo->_sifields._sigpoll._band = info->si_band;
408 tinfo->_sifields._sigpoll._fd = info->si_fd;
409 si_type = QEMU_SI_POLL;
412 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
413 tinfo->_sifields._rt._pid = info->si_pid;
414 tinfo->_sifields._rt._uid = info->si_uid;
415 /* XXX: potential problem if 64 bit */
416 tinfo->_sifields._rt._sigval.sival_ptr
417 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
418 si_type = QEMU_SI_RT;
424 tinfo->si_code = deposit32(si_code, 16, 16, si_type);
427 void tswap_siginfo(target_siginfo_t *tinfo,
428 const target_siginfo_t *info)
430 int si_type = extract32(info->si_code, 16, 16);
431 int si_code = sextract32(info->si_code, 0, 16);
433 __put_user(info->si_signo, &tinfo->si_signo);
434 __put_user(info->si_errno, &tinfo->si_errno);
435 __put_user(si_code, &tinfo->si_code);
437 /* We can use our internal marker of which fields in the structure
438 * are valid, rather than duplicating the guesswork of
439 * host_to_target_siginfo_noswap() here.
443 __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
444 __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
447 __put_user(info->_sifields._timer._timer1,
448 &tinfo->_sifields._timer._timer1);
449 __put_user(info->_sifields._timer._timer2,
450 &tinfo->_sifields._timer._timer2);
453 __put_user(info->_sifields._sigpoll._band,
454 &tinfo->_sifields._sigpoll._band);
455 __put_user(info->_sifields._sigpoll._fd,
456 &tinfo->_sifields._sigpoll._fd);
459 __put_user(info->_sifields._sigfault._addr,
460 &tinfo->_sifields._sigfault._addr);
463 __put_user(info->_sifields._sigchld._pid,
464 &tinfo->_sifields._sigchld._pid);
465 __put_user(info->_sifields._sigchld._uid,
466 &tinfo->_sifields._sigchld._uid);
467 __put_user(info->_sifields._sigchld._status,
468 &tinfo->_sifields._sigchld._status);
469 __put_user(info->_sifields._sigchld._utime,
470 &tinfo->_sifields._sigchld._utime);
471 __put_user(info->_sifields._sigchld._stime,
472 &tinfo->_sifields._sigchld._stime);
475 __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
476 __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
477 __put_user(info->_sifields._rt._sigval.sival_ptr,
478 &tinfo->_sifields._rt._sigval.sival_ptr);
481 g_assert_not_reached();
485 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
487 target_siginfo_t tgt_tmp;
488 host_to_target_siginfo_noswap(&tgt_tmp, info);
489 tswap_siginfo(tinfo, &tgt_tmp);
492 /* XXX: we support only POSIX RT signals are used. */
493 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
494 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
496 /* This conversion is used only for the rt_sigqueueinfo syscall,
497 * and so we know that the _rt fields are the valid ones.
501 __get_user(info->si_signo, &tinfo->si_signo);
502 __get_user(info->si_errno, &tinfo->si_errno);
503 __get_user(info->si_code, &tinfo->si_code);
504 __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
505 __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
506 __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
507 info->si_value.sival_ptr = (void *)(long)sival_ptr;
510 static int fatal_signal (int sig)
515 case TARGET_SIGWINCH:
516 /* Ignored by default. */
523 /* Job control signals. */
530 /* returns 1 if given signal should dump core if not handled */
531 static int core_dump_signal(int sig)
547 static void signal_table_init(void)
549 int host_sig, target_sig, count;
552 * Signals are supported starting from TARGET_SIGRTMIN and going up
553 * until we run out of host realtime signals.
554 * glibc at least uses only the lower 2 rt signals and probably
555 * nobody's using the upper ones.
556 * it's why SIGRTMIN (34) is generally greater than __SIGRTMIN (32)
557 * To fix this properly we need to do manual signal delivery multiplexed
558 * over a single host signal.
559 * Attempts for configure "missing" signals via sigaction will be
562 for (host_sig = SIGRTMIN; host_sig <= SIGRTMAX; host_sig++) {
563 target_sig = host_sig - SIGRTMIN + TARGET_SIGRTMIN;
564 if (target_sig <= TARGET_NSIG) {
565 host_to_target_signal_table[host_sig] = target_sig;
569 /* generate signal conversion tables */
570 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
571 target_to_host_signal_table[target_sig] = _NSIG; /* poison */
573 for (host_sig = 1; host_sig < _NSIG; host_sig++) {
574 if (host_to_target_signal_table[host_sig] == 0) {
575 host_to_target_signal_table[host_sig] = host_sig;
577 target_sig = host_to_target_signal_table[host_sig];
578 if (target_sig <= TARGET_NSIG) {
579 target_to_host_signal_table[target_sig] = host_sig;
583 if (trace_event_get_state_backends(TRACE_SIGNAL_TABLE_INIT)) {
584 for (target_sig = 1, count = 0; target_sig <= TARGET_NSIG; target_sig++) {
585 if (target_to_host_signal_table[target_sig] == _NSIG) {
589 trace_signal_table_init(count);
593 void signal_init(void)
595 TaskState *ts = (TaskState *)thread_cpu->opaque;
596 struct sigaction act;
597 struct sigaction oact;
601 /* initialize signal conversion tables */
604 /* Set the signal mask from the host mask. */
605 sigprocmask(0, 0, &ts->signal_mask);
607 sigfillset(&act.sa_mask);
608 act.sa_flags = SA_SIGINFO;
609 act.sa_sigaction = host_signal_handler;
610 for(i = 1; i <= TARGET_NSIG; i++) {
612 if (i == TARGET_SIGPROF) {
616 host_sig = target_to_host_signal(i);
617 sigaction(host_sig, NULL, &oact);
618 if (oact.sa_sigaction == (void *)SIG_IGN) {
619 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
620 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
621 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
623 /* If there's already a handler installed then something has
624 gone horribly wrong, so don't even try to handle that case. */
625 /* Install some handlers for our own use. We need at least
626 SIGSEGV and SIGBUS, to detect exceptions. We can not just
627 trap all signals because it affects syscall interrupt
628 behavior. But do trap all default-fatal signals. */
629 if (fatal_signal (i))
630 sigaction(host_sig, &act, NULL);
634 /* Force a synchronously taken signal. The kernel force_sig() function
635 * also forces the signal to "not blocked, not ignored", but for QEMU
636 * that work is done in process_pending_signals().
638 void force_sig(int sig)
640 CPUState *cpu = thread_cpu;
641 CPUArchState *env = cpu->env_ptr;
642 target_siginfo_t info;
646 info.si_code = TARGET_SI_KERNEL;
647 info._sifields._kill._pid = 0;
648 info._sifields._kill._uid = 0;
649 queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
652 /* Force a SIGSEGV if we couldn't write to memory trying to set
653 * up the signal frame. oldsig is the signal we were trying to handle
654 * at the point of failure.
656 #if !defined(TARGET_RISCV)
657 void force_sigsegv(int oldsig)
659 if (oldsig == SIGSEGV) {
660 /* Make sure we don't try to deliver the signal again; this will
661 * end up with handle_pending_signal() calling dump_core_and_abort().
663 sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
665 force_sig(TARGET_SIGSEGV);
670 /* abort execution with signal */
671 static void QEMU_NORETURN dump_core_and_abort(int target_sig)
673 CPUState *cpu = thread_cpu;
674 CPUArchState *env = cpu->env_ptr;
675 TaskState *ts = (TaskState *)cpu->opaque;
676 int host_sig, core_dumped = 0;
677 struct sigaction act;
679 host_sig = target_to_host_signal(target_sig);
680 trace_user_force_sig(env, target_sig, host_sig);
681 gdb_signalled(env, target_sig);
683 /* dump core if supported by target binary format */
684 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
687 ((*ts->bprm->core_dump)(target_sig, env) == 0);
690 /* we already dumped the core of target process, we don't want
691 * a coredump of qemu itself */
692 struct rlimit nodump;
693 getrlimit(RLIMIT_CORE, &nodump);
695 setrlimit(RLIMIT_CORE, &nodump);
696 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
697 target_sig, strsignal(host_sig), "core dumped" );
700 /* The proper exit code for dying from an uncaught signal is
701 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
702 * a negative value. To get the proper exit code we need to
703 * actually die from an uncaught signal. Here the default signal
704 * handler is installed, we send ourself a signal and we wait for
706 sigfillset(&act.sa_mask);
707 act.sa_handler = SIG_DFL;
709 sigaction(host_sig, &act, NULL);
711 /* For some reason raise(host_sig) doesn't send the signal when
712 * statically linked on x86-64. */
713 kill(getpid(), host_sig);
715 /* Make sure the signal isn't masked (just reuse the mask inside
717 sigdelset(&act.sa_mask, host_sig);
718 sigsuspend(&act.sa_mask);
724 /* queue a signal so that it will be send to the virtual CPU as soon
726 int queue_signal(CPUArchState *env, int sig, int si_type,
727 target_siginfo_t *info)
729 CPUState *cpu = env_cpu(env);
730 TaskState *ts = cpu->opaque;
732 trace_user_queue_signal(env, sig);
734 info->si_code = deposit32(info->si_code, 16, 16, si_type);
736 ts->sync_signal.info = *info;
737 ts->sync_signal.pending = sig;
738 /* signal that a new signal is pending */
739 qatomic_set(&ts->signal_pending, 1);
740 return 1; /* indicates that the signal was queued */
743 #ifndef HAVE_SAFE_SYSCALL
744 static inline void rewind_if_in_safe_syscall(void *puc)
746 /* Default version: never rewind */
750 static void host_signal_handler(int host_signum, siginfo_t *info,
753 CPUArchState *env = thread_cpu->env_ptr;
754 CPUState *cpu = env_cpu(env);
755 TaskState *ts = cpu->opaque;
758 target_siginfo_t tinfo;
759 ucontext_t *uc = puc;
760 struct emulated_sigtable *k;
762 /* the CPU emulator uses some host signals to detect exceptions,
763 we forward to it some signals */
764 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
765 && info->si_code > 0) {
766 if (cpu_signal_handler(host_signum, info, puc))
770 /* get target signal number */
771 sig = host_to_target_signal(host_signum);
772 if (sig < 1 || sig > TARGET_NSIG)
774 trace_user_host_signal(env, host_signum, sig);
776 rewind_if_in_safe_syscall(puc);
778 host_to_target_siginfo_noswap(&tinfo, info);
779 k = &ts->sigtab[sig - 1];
782 ts->signal_pending = 1;
784 /* Block host signals until target signal handler entered. We
785 * can't block SIGSEGV or SIGBUS while we're executing guest
786 * code in case the guest code provokes one in the window between
787 * now and it getting out to the main loop. Signals will be
788 * unblocked again in process_pending_signals().
790 * WARNING: we cannot use sigfillset() here because the uc_sigmask
791 * field is a kernel sigset_t, which is much smaller than the
792 * libc sigset_t which sigfillset() operates on. Using sigfillset()
793 * would write 0xff bytes off the end of the structure and trash
794 * data on the struct.
795 * We can't use sizeof(uc->uc_sigmask) either, because the libc
796 * headers define the struct field with the wrong (too large) type.
798 memset(&uc->uc_sigmask, 0xff, SIGSET_T_SIZE);
799 sigdelset(&uc->uc_sigmask, SIGSEGV);
800 sigdelset(&uc->uc_sigmask, SIGBUS);
802 /* interrupt the virtual CPU as soon as possible */
803 cpu_exit(thread_cpu);
806 /* do_sigaltstack() returns target values and errnos. */
807 /* compare linux/kernel/signal.c:do_sigaltstack() */
808 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr,
811 target_stack_t oss, *uoss = NULL;
812 abi_long ret = -TARGET_EFAULT;
815 /* Verify writability now, but do not alter user memory yet. */
816 if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) {
819 target_save_altstack(&oss, env);
825 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
828 ret = target_restore_altstack(uss, env);
835 memcpy(uoss, &oss, sizeof(oss));
836 unlock_user_struct(uoss, uoss_addr, 1);
843 unlock_user_struct(uoss, uoss_addr, 0);
848 /* do_sigaction() return target values and host errnos */
849 int do_sigaction(int sig, const struct target_sigaction *act,
850 struct target_sigaction *oact, abi_ulong ka_restorer)
852 struct target_sigaction *k;
853 struct sigaction act1;
857 trace_signal_do_sigaction_guest(sig, TARGET_NSIG);
859 if (sig < 1 || sig > TARGET_NSIG) {
860 return -TARGET_EINVAL;
863 if (act && (sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)) {
864 return -TARGET_EINVAL;
867 if (block_signals()) {
868 return -TARGET_ERESTARTSYS;
871 k = &sigact_table[sig - 1];
873 __put_user(k->_sa_handler, &oact->_sa_handler);
874 __put_user(k->sa_flags, &oact->sa_flags);
875 #ifdef TARGET_ARCH_HAS_SA_RESTORER
876 __put_user(k->sa_restorer, &oact->sa_restorer);
879 oact->sa_mask = k->sa_mask;
882 /* FIXME: This is not threadsafe. */
883 __get_user(k->_sa_handler, &act->_sa_handler);
884 __get_user(k->sa_flags, &act->sa_flags);
885 #ifdef TARGET_ARCH_HAS_SA_RESTORER
886 __get_user(k->sa_restorer, &act->sa_restorer);
888 #ifdef TARGET_ARCH_HAS_KA_RESTORER
889 k->ka_restorer = ka_restorer;
891 /* To be swapped in target_to_host_sigset. */
892 k->sa_mask = act->sa_mask;
894 /* we update the host linux signal state */
895 host_sig = target_to_host_signal(sig);
896 trace_signal_do_sigaction_host(host_sig, TARGET_NSIG);
897 if (host_sig > SIGRTMAX) {
898 /* we don't have enough host signals to map all target signals */
899 qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n",
902 * we don't return an error here because some programs try to
903 * register an handler for all possible rt signals even if they
905 * An error here can abort them whereas there can be no problem
906 * to not have the signal available later.
907 * This is the case for golang,
908 * See https://github.com/golang/go/issues/33746
909 * So we silently ignore the error.
913 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
914 sigfillset(&act1.sa_mask);
915 act1.sa_flags = SA_SIGINFO;
916 if (k->sa_flags & TARGET_SA_RESTART)
917 act1.sa_flags |= SA_RESTART;
918 /* NOTE: it is important to update the host kernel signal
919 ignore state to avoid getting unexpected interrupted
921 if (k->_sa_handler == TARGET_SIG_IGN) {
922 act1.sa_sigaction = (void *)SIG_IGN;
923 } else if (k->_sa_handler == TARGET_SIG_DFL) {
924 if (fatal_signal (sig))
925 act1.sa_sigaction = host_signal_handler;
927 act1.sa_sigaction = (void *)SIG_DFL;
929 act1.sa_sigaction = host_signal_handler;
931 ret = sigaction(host_sig, &act1, NULL);
937 static void handle_pending_signal(CPUArchState *cpu_env, int sig,
938 struct emulated_sigtable *k)
940 CPUState *cpu = env_cpu(cpu_env);
943 target_sigset_t target_old_set;
944 struct target_sigaction *sa;
945 TaskState *ts = cpu->opaque;
947 trace_user_handle_signal(cpu_env, sig);
951 sig = gdb_handlesig(cpu, sig);
954 handler = TARGET_SIG_IGN;
956 sa = &sigact_table[sig - 1];
957 handler = sa->_sa_handler;
960 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
961 print_taken_signal(sig, &k->info);
964 if (handler == TARGET_SIG_DFL) {
965 /* default handler : ignore some signal. The other are job control or fatal */
966 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
967 kill(getpid(),SIGSTOP);
968 } else if (sig != TARGET_SIGCHLD &&
969 sig != TARGET_SIGURG &&
970 sig != TARGET_SIGWINCH &&
971 sig != TARGET_SIGCONT) {
972 dump_core_and_abort(sig);
974 } else if (handler == TARGET_SIG_IGN) {
976 } else if (handler == TARGET_SIG_ERR) {
977 dump_core_and_abort(sig);
979 /* compute the blocked signals during the handler execution */
980 sigset_t *blocked_set;
982 target_to_host_sigset(&set, &sa->sa_mask);
983 /* SA_NODEFER indicates that the current signal should not be
984 blocked during the handler */
985 if (!(sa->sa_flags & TARGET_SA_NODEFER))
986 sigaddset(&set, target_to_host_signal(sig));
988 /* save the previous blocked signal state to restore it at the
989 end of the signal execution (see do_sigreturn) */
990 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
992 /* block signals in the handler */
993 blocked_set = ts->in_sigsuspend ?
994 &ts->sigsuspend_mask : &ts->signal_mask;
995 sigorset(&ts->signal_mask, blocked_set, &set);
996 ts->in_sigsuspend = 0;
998 /* if the CPU is in VM86 mode, we restore the 32 bit values */
999 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
1001 CPUX86State *env = cpu_env;
1002 if (env->eflags & VM_MASK)
1003 save_v86_state(env);
1006 /* prepare the stack frame of the virtual CPU */
1007 #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
1008 if (sa->sa_flags & TARGET_SA_SIGINFO) {
1009 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
1011 setup_frame(sig, sa, &target_old_set, cpu_env);
1014 /* These targets do not have traditional signals. */
1015 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
1017 if (sa->sa_flags & TARGET_SA_RESETHAND) {
1018 sa->_sa_handler = TARGET_SIG_DFL;
1023 void process_pending_signals(CPUArchState *cpu_env)
1025 CPUState *cpu = env_cpu(cpu_env);
1027 TaskState *ts = cpu->opaque;
1029 sigset_t *blocked_set;
1031 while (qatomic_read(&ts->signal_pending)) {
1032 /* FIXME: This is not threadsafe. */
1034 sigprocmask(SIG_SETMASK, &set, 0);
1037 sig = ts->sync_signal.pending;
1039 /* Synchronous signals are forced,
1040 * see force_sig_info() and callers in Linux
1041 * Note that not all of our queue_signal() calls in QEMU correspond
1042 * to force_sig_info() calls in Linux (some are send_sig_info()).
1043 * However it seems like a kernel bug to me to allow the process
1044 * to block a synchronous signal since it could then just end up
1045 * looping round and round indefinitely.
1047 if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
1048 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
1049 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
1050 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
1053 handle_pending_signal(cpu_env, sig, &ts->sync_signal);
1056 for (sig = 1; sig <= TARGET_NSIG; sig++) {
1057 blocked_set = ts->in_sigsuspend ?
1058 &ts->sigsuspend_mask : &ts->signal_mask;
1060 if (ts->sigtab[sig - 1].pending &&
1061 (!sigismember(blocked_set,
1062 target_to_host_signal_table[sig]))) {
1063 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
1064 /* Restart scan from the beginning, as handle_pending_signal
1065 * might have resulted in a new synchronous signal (eg SIGSEGV).
1071 /* if no signal is pending, unblock signals and recheck (the act
1072 * of unblocking might cause us to take another host signal which
1073 * will set signal_pending again).
1075 qatomic_set(&ts->signal_pending, 0);
1076 ts->in_sigsuspend = 0;
1077 set = ts->signal_mask;
1078 sigdelset(&set, SIGSEGV);
1079 sigdelset(&set, SIGBUS);
1080 sigprocmask(SIG_SETMASK, &set, 0);
1082 ts->in_sigsuspend = 0;