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 "exec/gdbstub.h"
23 #include <sys/ucontext.h>
24 #include <sys/resource.h>
27 #include "user-internals.h"
31 #include "signal-common.h"
33 static struct target_sigaction sigact_table
[TARGET_NSIG
];
35 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
40 * System includes define _NSIG as SIGRTMAX + 1,
41 * but qemu (like the kernel) defines TARGET_NSIG as TARGET_SIGRTMAX
42 * and the first signal is SIGHUP defined as 1
43 * Signal number 0 is reserved for use as kill(pid, 0), to test whether
44 * a process exists without sending it a signal.
47 QEMU_BUILD_BUG_ON(__SIGRTMAX
+ 1 != _NSIG
);
49 static uint8_t host_to_target_signal_table
[_NSIG
] = {
50 [SIGHUP
] = TARGET_SIGHUP
,
51 [SIGINT
] = TARGET_SIGINT
,
52 [SIGQUIT
] = TARGET_SIGQUIT
,
53 [SIGILL
] = TARGET_SIGILL
,
54 [SIGTRAP
] = TARGET_SIGTRAP
,
55 [SIGABRT
] = TARGET_SIGABRT
,
56 /* [SIGIOT] = TARGET_SIGIOT,*/
57 [SIGBUS
] = TARGET_SIGBUS
,
58 [SIGFPE
] = TARGET_SIGFPE
,
59 [SIGKILL
] = TARGET_SIGKILL
,
60 [SIGUSR1
] = TARGET_SIGUSR1
,
61 [SIGSEGV
] = TARGET_SIGSEGV
,
62 [SIGUSR2
] = TARGET_SIGUSR2
,
63 [SIGPIPE
] = TARGET_SIGPIPE
,
64 [SIGALRM
] = TARGET_SIGALRM
,
65 [SIGTERM
] = TARGET_SIGTERM
,
67 [SIGSTKFLT
] = TARGET_SIGSTKFLT
,
69 [SIGCHLD
] = TARGET_SIGCHLD
,
70 [SIGCONT
] = TARGET_SIGCONT
,
71 [SIGSTOP
] = TARGET_SIGSTOP
,
72 [SIGTSTP
] = TARGET_SIGTSTP
,
73 [SIGTTIN
] = TARGET_SIGTTIN
,
74 [SIGTTOU
] = TARGET_SIGTTOU
,
75 [SIGURG
] = TARGET_SIGURG
,
76 [SIGXCPU
] = TARGET_SIGXCPU
,
77 [SIGXFSZ
] = TARGET_SIGXFSZ
,
78 [SIGVTALRM
] = TARGET_SIGVTALRM
,
79 [SIGPROF
] = TARGET_SIGPROF
,
80 [SIGWINCH
] = TARGET_SIGWINCH
,
81 [SIGIO
] = TARGET_SIGIO
,
82 [SIGPWR
] = TARGET_SIGPWR
,
83 [SIGSYS
] = TARGET_SIGSYS
,
84 /* next signals stay the same */
87 static uint8_t target_to_host_signal_table
[TARGET_NSIG
+ 1];
89 /* valid sig is between 1 and _NSIG - 1 */
90 int host_to_target_signal(int sig
)
92 if (sig
< 1 || sig
>= _NSIG
) {
95 return host_to_target_signal_table
[sig
];
98 /* valid sig is between 1 and TARGET_NSIG */
99 int target_to_host_signal(int sig
)
101 if (sig
< 1 || sig
> TARGET_NSIG
) {
104 return target_to_host_signal_table
[sig
];
107 static inline void target_sigaddset(target_sigset_t
*set
, int signum
)
110 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
111 set
->sig
[signum
/ TARGET_NSIG_BPW
] |= mask
;
114 static inline int target_sigismember(const target_sigset_t
*set
, int signum
)
117 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
118 return ((set
->sig
[signum
/ TARGET_NSIG_BPW
] & mask
) != 0);
121 void host_to_target_sigset_internal(target_sigset_t
*d
,
124 int host_sig
, target_sig
;
125 target_sigemptyset(d
);
126 for (host_sig
= 1; host_sig
< _NSIG
; host_sig
++) {
127 target_sig
= host_to_target_signal(host_sig
);
128 if (target_sig
< 1 || target_sig
> TARGET_NSIG
) {
131 if (sigismember(s
, host_sig
)) {
132 target_sigaddset(d
, target_sig
);
137 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
142 host_to_target_sigset_internal(&d1
, s
);
143 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
144 d
->sig
[i
] = tswapal(d1
.sig
[i
]);
147 void target_to_host_sigset_internal(sigset_t
*d
,
148 const target_sigset_t
*s
)
150 int host_sig
, target_sig
;
152 for (target_sig
= 1; target_sig
<= TARGET_NSIG
; target_sig
++) {
153 host_sig
= target_to_host_signal(target_sig
);
154 if (host_sig
< 1 || host_sig
>= _NSIG
) {
157 if (target_sigismember(s
, target_sig
)) {
158 sigaddset(d
, host_sig
);
163 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
168 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
169 s1
.sig
[i
] = tswapal(s
->sig
[i
]);
170 target_to_host_sigset_internal(d
, &s1
);
173 void host_to_target_old_sigset(abi_ulong
*old_sigset
,
174 const sigset_t
*sigset
)
177 host_to_target_sigset(&d
, sigset
);
178 *old_sigset
= d
.sig
[0];
181 void target_to_host_old_sigset(sigset_t
*sigset
,
182 const abi_ulong
*old_sigset
)
187 d
.sig
[0] = *old_sigset
;
188 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
190 target_to_host_sigset(sigset
, &d
);
193 int block_signals(void)
195 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
198 /* It's OK to block everything including SIGSEGV, because we won't
199 * run any further guest code before unblocking signals in
200 * process_pending_signals().
203 sigprocmask(SIG_SETMASK
, &set
, 0);
205 return qatomic_xchg(&ts
->signal_pending
, 1);
208 /* Wrapper for sigprocmask function
209 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
210 * are host signal set, not guest ones. Returns -TARGET_ERESTARTSYS if
211 * a signal was already pending and the syscall must be restarted, or
213 * If set is NULL, this is guaranteed not to fail.
215 int do_sigprocmask(int how
, const sigset_t
*set
, sigset_t
*oldset
)
217 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
220 *oldset
= ts
->signal_mask
;
226 if (block_signals()) {
227 return -TARGET_ERESTARTSYS
;
232 sigorset(&ts
->signal_mask
, &ts
->signal_mask
, set
);
235 for (i
= 1; i
<= NSIG
; ++i
) {
236 if (sigismember(set
, i
)) {
237 sigdelset(&ts
->signal_mask
, i
);
242 ts
->signal_mask
= *set
;
245 g_assert_not_reached();
248 /* Silently ignore attempts to change blocking status of KILL or STOP */
249 sigdelset(&ts
->signal_mask
, SIGKILL
);
250 sigdelset(&ts
->signal_mask
, SIGSTOP
);
255 #if !defined(TARGET_NIOS2)
256 /* Just set the guest's signal mask to the specified value; the
257 * caller is assumed to have called block_signals() already.
259 void set_sigmask(const sigset_t
*set
)
261 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
263 ts
->signal_mask
= *set
;
267 /* sigaltstack management */
269 int on_sig_stack(unsigned long sp
)
271 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
273 return (sp
- ts
->sigaltstack_used
.ss_sp
274 < ts
->sigaltstack_used
.ss_size
);
277 int sas_ss_flags(unsigned long sp
)
279 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
281 return (ts
->sigaltstack_used
.ss_size
== 0 ? SS_DISABLE
282 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
285 abi_ulong
target_sigsp(abi_ulong sp
, struct target_sigaction
*ka
)
288 * This is the X/Open sanctioned signal stack switching.
290 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
292 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
)) {
293 return ts
->sigaltstack_used
.ss_sp
+ ts
->sigaltstack_used
.ss_size
;
298 void target_save_altstack(target_stack_t
*uss
, CPUArchState
*env
)
300 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
302 __put_user(ts
->sigaltstack_used
.ss_sp
, &uss
->ss_sp
);
303 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &uss
->ss_flags
);
304 __put_user(ts
->sigaltstack_used
.ss_size
, &uss
->ss_size
);
307 abi_long
target_restore_altstack(target_stack_t
*uss
, CPUArchState
*env
)
309 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
310 size_t minstacksize
= TARGET_MINSIGSTKSZ
;
313 #if defined(TARGET_PPC64)
314 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
315 struct image_info
*image
= ts
->info
;
316 if (get_ppc64_abi(image
) > 1) {
321 __get_user(ss
.ss_sp
, &uss
->ss_sp
);
322 __get_user(ss
.ss_size
, &uss
->ss_size
);
323 __get_user(ss
.ss_flags
, &uss
->ss_flags
);
325 if (on_sig_stack(get_sp_from_cpustate(env
))) {
326 return -TARGET_EPERM
;
329 switch (ss
.ss_flags
) {
331 return -TARGET_EINVAL
;
333 case TARGET_SS_DISABLE
:
338 case TARGET_SS_ONSTACK
:
340 if (ss
.ss_size
< minstacksize
) {
341 return -TARGET_ENOMEM
;
346 ts
->sigaltstack_used
.ss_sp
= ss
.ss_sp
;
347 ts
->sigaltstack_used
.ss_size
= ss
.ss_size
;
351 /* siginfo conversion */
353 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
354 const siginfo_t
*info
)
356 int sig
= host_to_target_signal(info
->si_signo
);
357 int si_code
= info
->si_code
;
359 tinfo
->si_signo
= sig
;
361 tinfo
->si_code
= info
->si_code
;
363 /* This memset serves two purposes:
364 * (1) ensure we don't leak random junk to the guest later
365 * (2) placate false positives from gcc about fields
366 * being used uninitialized if it chooses to inline both this
367 * function and tswap_siginfo() into host_to_target_siginfo().
369 memset(tinfo
->_sifields
._pad
, 0, sizeof(tinfo
->_sifields
._pad
));
371 /* This is awkward, because we have to use a combination of
372 * the si_code and si_signo to figure out which of the union's
373 * members are valid. (Within the host kernel it is always possible
374 * to tell, but the kernel carefully avoids giving userspace the
375 * high 16 bits of si_code, so we don't have the information to
376 * do this the easy way...) We therefore make our best guess,
377 * bearing in mind that a guest can spoof most of the si_codes
378 * via rt_sigqueueinfo() if it likes.
380 * Once we have made our guess, we record it in the top 16 bits of
381 * the si_code, so that tswap_siginfo() later can use it.
382 * tswap_siginfo() will strip these top bits out before writing
383 * si_code to the guest (sign-extending the lower bits).
390 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
391 * These are the only unspoofable si_code values.
393 tinfo
->_sifields
._kill
._pid
= info
->si_pid
;
394 tinfo
->_sifields
._kill
._uid
= info
->si_uid
;
395 si_type
= QEMU_SI_KILL
;
398 /* Everything else is spoofable. Make best guess based on signal */
401 tinfo
->_sifields
._sigchld
._pid
= info
->si_pid
;
402 tinfo
->_sifields
._sigchld
._uid
= info
->si_uid
;
403 tinfo
->_sifields
._sigchld
._status
= info
->si_status
;
404 tinfo
->_sifields
._sigchld
._utime
= info
->si_utime
;
405 tinfo
->_sifields
._sigchld
._stime
= info
->si_stime
;
406 si_type
= QEMU_SI_CHLD
;
409 tinfo
->_sifields
._sigpoll
._band
= info
->si_band
;
410 tinfo
->_sifields
._sigpoll
._fd
= info
->si_fd
;
411 si_type
= QEMU_SI_POLL
;
414 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
415 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
416 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
417 /* XXX: potential problem if 64 bit */
418 tinfo
->_sifields
._rt
._sigval
.sival_ptr
419 = (abi_ulong
)(unsigned long)info
->si_value
.sival_ptr
;
420 si_type
= QEMU_SI_RT
;
426 tinfo
->si_code
= deposit32(si_code
, 16, 16, si_type
);
429 void tswap_siginfo(target_siginfo_t
*tinfo
,
430 const target_siginfo_t
*info
)
432 int si_type
= extract32(info
->si_code
, 16, 16);
433 int si_code
= sextract32(info
->si_code
, 0, 16);
435 __put_user(info
->si_signo
, &tinfo
->si_signo
);
436 __put_user(info
->si_errno
, &tinfo
->si_errno
);
437 __put_user(si_code
, &tinfo
->si_code
);
439 /* We can use our internal marker of which fields in the structure
440 * are valid, rather than duplicating the guesswork of
441 * host_to_target_siginfo_noswap() here.
445 __put_user(info
->_sifields
._kill
._pid
, &tinfo
->_sifields
._kill
._pid
);
446 __put_user(info
->_sifields
._kill
._uid
, &tinfo
->_sifields
._kill
._uid
);
449 __put_user(info
->_sifields
._timer
._timer1
,
450 &tinfo
->_sifields
._timer
._timer1
);
451 __put_user(info
->_sifields
._timer
._timer2
,
452 &tinfo
->_sifields
._timer
._timer2
);
455 __put_user(info
->_sifields
._sigpoll
._band
,
456 &tinfo
->_sifields
._sigpoll
._band
);
457 __put_user(info
->_sifields
._sigpoll
._fd
,
458 &tinfo
->_sifields
._sigpoll
._fd
);
461 __put_user(info
->_sifields
._sigfault
._addr
,
462 &tinfo
->_sifields
._sigfault
._addr
);
465 __put_user(info
->_sifields
._sigchld
._pid
,
466 &tinfo
->_sifields
._sigchld
._pid
);
467 __put_user(info
->_sifields
._sigchld
._uid
,
468 &tinfo
->_sifields
._sigchld
._uid
);
469 __put_user(info
->_sifields
._sigchld
._status
,
470 &tinfo
->_sifields
._sigchld
._status
);
471 __put_user(info
->_sifields
._sigchld
._utime
,
472 &tinfo
->_sifields
._sigchld
._utime
);
473 __put_user(info
->_sifields
._sigchld
._stime
,
474 &tinfo
->_sifields
._sigchld
._stime
);
477 __put_user(info
->_sifields
._rt
._pid
, &tinfo
->_sifields
._rt
._pid
);
478 __put_user(info
->_sifields
._rt
._uid
, &tinfo
->_sifields
._rt
._uid
);
479 __put_user(info
->_sifields
._rt
._sigval
.sival_ptr
,
480 &tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
483 g_assert_not_reached();
487 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
489 target_siginfo_t tgt_tmp
;
490 host_to_target_siginfo_noswap(&tgt_tmp
, info
);
491 tswap_siginfo(tinfo
, &tgt_tmp
);
494 /* XXX: we support only POSIX RT signals are used. */
495 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
496 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
498 /* This conversion is used only for the rt_sigqueueinfo syscall,
499 * and so we know that the _rt fields are the valid ones.
503 __get_user(info
->si_signo
, &tinfo
->si_signo
);
504 __get_user(info
->si_errno
, &tinfo
->si_errno
);
505 __get_user(info
->si_code
, &tinfo
->si_code
);
506 __get_user(info
->si_pid
, &tinfo
->_sifields
._rt
._pid
);
507 __get_user(info
->si_uid
, &tinfo
->_sifields
._rt
._uid
);
508 __get_user(sival_ptr
, &tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
509 info
->si_value
.sival_ptr
= (void *)(long)sival_ptr
;
512 static int fatal_signal (int sig
)
517 case TARGET_SIGWINCH
:
518 /* Ignored by default. */
525 /* Job control signals. */
532 /* returns 1 if given signal should dump core if not handled */
533 static int core_dump_signal(int sig
)
549 static void signal_table_init(void)
551 int host_sig
, target_sig
, count
;
554 * Signals are supported starting from TARGET_SIGRTMIN and going up
555 * until we run out of host realtime signals.
556 * glibc at least uses only the lower 2 rt signals and probably
557 * nobody's using the upper ones.
558 * it's why SIGRTMIN (34) is generally greater than __SIGRTMIN (32)
559 * To fix this properly we need to do manual signal delivery multiplexed
560 * over a single host signal.
561 * Attempts for configure "missing" signals via sigaction will be
564 for (host_sig
= SIGRTMIN
; host_sig
<= SIGRTMAX
; host_sig
++) {
565 target_sig
= host_sig
- SIGRTMIN
+ TARGET_SIGRTMIN
;
566 if (target_sig
<= TARGET_NSIG
) {
567 host_to_target_signal_table
[host_sig
] = target_sig
;
571 /* generate signal conversion tables */
572 for (target_sig
= 1; target_sig
<= TARGET_NSIG
; target_sig
++) {
573 target_to_host_signal_table
[target_sig
] = _NSIG
; /* poison */
575 for (host_sig
= 1; host_sig
< _NSIG
; host_sig
++) {
576 if (host_to_target_signal_table
[host_sig
] == 0) {
577 host_to_target_signal_table
[host_sig
] = host_sig
;
579 target_sig
= host_to_target_signal_table
[host_sig
];
580 if (target_sig
<= TARGET_NSIG
) {
581 target_to_host_signal_table
[target_sig
] = host_sig
;
585 if (trace_event_get_state_backends(TRACE_SIGNAL_TABLE_INIT
)) {
586 for (target_sig
= 1, count
= 0; target_sig
<= TARGET_NSIG
; target_sig
++) {
587 if (target_to_host_signal_table
[target_sig
] == _NSIG
) {
591 trace_signal_table_init(count
);
595 void signal_init(void)
597 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
598 struct sigaction act
;
599 struct sigaction oact
;
603 /* initialize signal conversion tables */
606 /* Set the signal mask from the host mask. */
607 sigprocmask(0, 0, &ts
->signal_mask
);
609 sigfillset(&act
.sa_mask
);
610 act
.sa_flags
= SA_SIGINFO
;
611 act
.sa_sigaction
= host_signal_handler
;
612 for(i
= 1; i
<= TARGET_NSIG
; i
++) {
614 if (i
== TARGET_SIGPROF
) {
618 host_sig
= target_to_host_signal(i
);
619 sigaction(host_sig
, NULL
, &oact
);
620 if (oact
.sa_sigaction
== (void *)SIG_IGN
) {
621 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_IGN
;
622 } else if (oact
.sa_sigaction
== (void *)SIG_DFL
) {
623 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_DFL
;
625 /* If there's already a handler installed then something has
626 gone horribly wrong, so don't even try to handle that case. */
627 /* Install some handlers for our own use. We need at least
628 SIGSEGV and SIGBUS, to detect exceptions. We can not just
629 trap all signals because it affects syscall interrupt
630 behavior. But do trap all default-fatal signals. */
631 if (fatal_signal (i
))
632 sigaction(host_sig
, &act
, NULL
);
636 /* Force a synchronously taken signal. The kernel force_sig() function
637 * also forces the signal to "not blocked, not ignored", but for QEMU
638 * that work is done in process_pending_signals().
640 void force_sig(int sig
)
642 CPUState
*cpu
= thread_cpu
;
643 CPUArchState
*env
= cpu
->env_ptr
;
644 target_siginfo_t info
= {};
648 info
.si_code
= TARGET_SI_KERNEL
;
649 info
._sifields
._kill
._pid
= 0;
650 info
._sifields
._kill
._uid
= 0;
651 queue_signal(env
, info
.si_signo
, QEMU_SI_KILL
, &info
);
655 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
656 * 'force' part is handled in process_pending_signals().
658 void force_sig_fault(int sig
, int code
, abi_ulong addr
)
660 CPUState
*cpu
= thread_cpu
;
661 CPUArchState
*env
= cpu
->env_ptr
;
662 target_siginfo_t info
= {};
667 info
._sifields
._sigfault
._addr
= addr
;
668 queue_signal(env
, sig
, QEMU_SI_FAULT
, &info
);
671 /* Force a SIGSEGV if we couldn't write to memory trying to set
672 * up the signal frame. oldsig is the signal we were trying to handle
673 * at the point of failure.
675 #if !defined(TARGET_RISCV)
676 void force_sigsegv(int oldsig
)
678 if (oldsig
== SIGSEGV
) {
679 /* Make sure we don't try to deliver the signal again; this will
680 * end up with handle_pending_signal() calling dump_core_and_abort().
682 sigact_table
[oldsig
- 1]._sa_handler
= TARGET_SIG_DFL
;
684 force_sig(TARGET_SIGSEGV
);
689 /* abort execution with signal */
690 static void QEMU_NORETURN
dump_core_and_abort(int target_sig
)
692 CPUState
*cpu
= thread_cpu
;
693 CPUArchState
*env
= cpu
->env_ptr
;
694 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
695 int host_sig
, core_dumped
= 0;
696 struct sigaction act
;
698 host_sig
= target_to_host_signal(target_sig
);
699 trace_user_force_sig(env
, target_sig
, host_sig
);
700 gdb_signalled(env
, target_sig
);
702 /* dump core if supported by target binary format */
703 if (core_dump_signal(target_sig
) && (ts
->bprm
->core_dump
!= NULL
)) {
706 ((*ts
->bprm
->core_dump
)(target_sig
, env
) == 0);
709 /* we already dumped the core of target process, we don't want
710 * a coredump of qemu itself */
711 struct rlimit nodump
;
712 getrlimit(RLIMIT_CORE
, &nodump
);
714 setrlimit(RLIMIT_CORE
, &nodump
);
715 (void) fprintf(stderr
, "qemu: uncaught target signal %d (%s) - %s\n",
716 target_sig
, strsignal(host_sig
), "core dumped" );
719 /* The proper exit code for dying from an uncaught signal is
720 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
721 * a negative value. To get the proper exit code we need to
722 * actually die from an uncaught signal. Here the default signal
723 * handler is installed, we send ourself a signal and we wait for
725 sigfillset(&act
.sa_mask
);
726 act
.sa_handler
= SIG_DFL
;
728 sigaction(host_sig
, &act
, NULL
);
730 /* For some reason raise(host_sig) doesn't send the signal when
731 * statically linked on x86-64. */
732 kill(getpid(), host_sig
);
734 /* Make sure the signal isn't masked (just reuse the mask inside
736 sigdelset(&act
.sa_mask
, host_sig
);
737 sigsuspend(&act
.sa_mask
);
743 /* queue a signal so that it will be send to the virtual CPU as soon
745 int queue_signal(CPUArchState
*env
, int sig
, int si_type
,
746 target_siginfo_t
*info
)
748 CPUState
*cpu
= env_cpu(env
);
749 TaskState
*ts
= cpu
->opaque
;
751 trace_user_queue_signal(env
, sig
);
753 info
->si_code
= deposit32(info
->si_code
, 16, 16, si_type
);
755 ts
->sync_signal
.info
= *info
;
756 ts
->sync_signal
.pending
= sig
;
757 /* signal that a new signal is pending */
758 qatomic_set(&ts
->signal_pending
, 1);
759 return 1; /* indicates that the signal was queued */
762 #ifndef HAVE_SAFE_SYSCALL
763 static inline void rewind_if_in_safe_syscall(void *puc
)
765 /* Default version: never rewind */
769 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
772 CPUArchState
*env
= thread_cpu
->env_ptr
;
773 CPUState
*cpu
= env_cpu(env
);
774 TaskState
*ts
= cpu
->opaque
;
777 target_siginfo_t tinfo
;
778 ucontext_t
*uc
= puc
;
779 struct emulated_sigtable
*k
;
781 /* the CPU emulator uses some host signals to detect exceptions,
782 we forward to it some signals */
783 if ((host_signum
== SIGSEGV
|| host_signum
== SIGBUS
)
784 && info
->si_code
> 0) {
785 if (cpu_signal_handler(host_signum
, info
, puc
))
789 /* get target signal number */
790 sig
= host_to_target_signal(host_signum
);
791 if (sig
< 1 || sig
> TARGET_NSIG
)
793 trace_user_host_signal(env
, host_signum
, sig
);
795 rewind_if_in_safe_syscall(puc
);
797 host_to_target_siginfo_noswap(&tinfo
, info
);
798 k
= &ts
->sigtab
[sig
- 1];
801 ts
->signal_pending
= 1;
803 /* Block host signals until target signal handler entered. We
804 * can't block SIGSEGV or SIGBUS while we're executing guest
805 * code in case the guest code provokes one in the window between
806 * now and it getting out to the main loop. Signals will be
807 * unblocked again in process_pending_signals().
809 * WARNING: we cannot use sigfillset() here because the uc_sigmask
810 * field is a kernel sigset_t, which is much smaller than the
811 * libc sigset_t which sigfillset() operates on. Using sigfillset()
812 * would write 0xff bytes off the end of the structure and trash
813 * data on the struct.
814 * We can't use sizeof(uc->uc_sigmask) either, because the libc
815 * headers define the struct field with the wrong (too large) type.
817 memset(&uc
->uc_sigmask
, 0xff, SIGSET_T_SIZE
);
818 sigdelset(&uc
->uc_sigmask
, SIGSEGV
);
819 sigdelset(&uc
->uc_sigmask
, SIGBUS
);
821 /* interrupt the virtual CPU as soon as possible */
822 cpu_exit(thread_cpu
);
825 /* do_sigaltstack() returns target values and errnos. */
826 /* compare linux/kernel/signal.c:do_sigaltstack() */
827 abi_long
do_sigaltstack(abi_ulong uss_addr
, abi_ulong uoss_addr
,
830 target_stack_t oss
, *uoss
= NULL
;
831 abi_long ret
= -TARGET_EFAULT
;
834 /* Verify writability now, but do not alter user memory yet. */
835 if (!lock_user_struct(VERIFY_WRITE
, uoss
, uoss_addr
, 0)) {
838 target_save_altstack(&oss
, env
);
844 if (!lock_user_struct(VERIFY_READ
, uss
, uss_addr
, 1)) {
847 ret
= target_restore_altstack(uss
, env
);
854 memcpy(uoss
, &oss
, sizeof(oss
));
855 unlock_user_struct(uoss
, uoss_addr
, 1);
862 unlock_user_struct(uoss
, uoss_addr
, 0);
867 /* do_sigaction() return target values and host errnos */
868 int do_sigaction(int sig
, const struct target_sigaction
*act
,
869 struct target_sigaction
*oact
, abi_ulong ka_restorer
)
871 struct target_sigaction
*k
;
872 struct sigaction act1
;
876 trace_signal_do_sigaction_guest(sig
, TARGET_NSIG
);
878 if (sig
< 1 || sig
> TARGET_NSIG
) {
879 return -TARGET_EINVAL
;
882 if (act
&& (sig
== TARGET_SIGKILL
|| sig
== TARGET_SIGSTOP
)) {
883 return -TARGET_EINVAL
;
886 if (block_signals()) {
887 return -TARGET_ERESTARTSYS
;
890 k
= &sigact_table
[sig
- 1];
892 __put_user(k
->_sa_handler
, &oact
->_sa_handler
);
893 __put_user(k
->sa_flags
, &oact
->sa_flags
);
894 #ifdef TARGET_ARCH_HAS_SA_RESTORER
895 __put_user(k
->sa_restorer
, &oact
->sa_restorer
);
898 oact
->sa_mask
= k
->sa_mask
;
901 /* FIXME: This is not threadsafe. */
902 __get_user(k
->_sa_handler
, &act
->_sa_handler
);
903 __get_user(k
->sa_flags
, &act
->sa_flags
);
904 #ifdef TARGET_ARCH_HAS_SA_RESTORER
905 __get_user(k
->sa_restorer
, &act
->sa_restorer
);
907 #ifdef TARGET_ARCH_HAS_KA_RESTORER
908 k
->ka_restorer
= ka_restorer
;
910 /* To be swapped in target_to_host_sigset. */
911 k
->sa_mask
= act
->sa_mask
;
913 /* we update the host linux signal state */
914 host_sig
= target_to_host_signal(sig
);
915 trace_signal_do_sigaction_host(host_sig
, TARGET_NSIG
);
916 if (host_sig
> SIGRTMAX
) {
917 /* we don't have enough host signals to map all target signals */
918 qemu_log_mask(LOG_UNIMP
, "Unsupported target signal #%d, ignored\n",
921 * we don't return an error here because some programs try to
922 * register an handler for all possible rt signals even if they
924 * An error here can abort them whereas there can be no problem
925 * to not have the signal available later.
926 * This is the case for golang,
927 * See https://github.com/golang/go/issues/33746
928 * So we silently ignore the error.
932 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
933 sigfillset(&act1
.sa_mask
);
934 act1
.sa_flags
= SA_SIGINFO
;
935 if (k
->sa_flags
& TARGET_SA_RESTART
)
936 act1
.sa_flags
|= SA_RESTART
;
937 /* NOTE: it is important to update the host kernel signal
938 ignore state to avoid getting unexpected interrupted
940 if (k
->_sa_handler
== TARGET_SIG_IGN
) {
941 act1
.sa_sigaction
= (void *)SIG_IGN
;
942 } else if (k
->_sa_handler
== TARGET_SIG_DFL
) {
943 if (fatal_signal (sig
))
944 act1
.sa_sigaction
= host_signal_handler
;
946 act1
.sa_sigaction
= (void *)SIG_DFL
;
948 act1
.sa_sigaction
= host_signal_handler
;
950 ret
= sigaction(host_sig
, &act1
, NULL
);
956 static void handle_pending_signal(CPUArchState
*cpu_env
, int sig
,
957 struct emulated_sigtable
*k
)
959 CPUState
*cpu
= env_cpu(cpu_env
);
962 target_sigset_t target_old_set
;
963 struct target_sigaction
*sa
;
964 TaskState
*ts
= cpu
->opaque
;
966 trace_user_handle_signal(cpu_env
, sig
);
970 sig
= gdb_handlesig(cpu
, sig
);
973 handler
= TARGET_SIG_IGN
;
975 sa
= &sigact_table
[sig
- 1];
976 handler
= sa
->_sa_handler
;
979 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
980 print_taken_signal(sig
, &k
->info
);
983 if (handler
== TARGET_SIG_DFL
) {
984 /* default handler : ignore some signal. The other are job control or fatal */
985 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
986 kill(getpid(),SIGSTOP
);
987 } else if (sig
!= TARGET_SIGCHLD
&&
988 sig
!= TARGET_SIGURG
&&
989 sig
!= TARGET_SIGWINCH
&&
990 sig
!= TARGET_SIGCONT
) {
991 dump_core_and_abort(sig
);
993 } else if (handler
== TARGET_SIG_IGN
) {
995 } else if (handler
== TARGET_SIG_ERR
) {
996 dump_core_and_abort(sig
);
998 /* compute the blocked signals during the handler execution */
999 sigset_t
*blocked_set
;
1001 target_to_host_sigset(&set
, &sa
->sa_mask
);
1002 /* SA_NODEFER indicates that the current signal should not be
1003 blocked during the handler */
1004 if (!(sa
->sa_flags
& TARGET_SA_NODEFER
))
1005 sigaddset(&set
, target_to_host_signal(sig
));
1007 /* save the previous blocked signal state to restore it at the
1008 end of the signal execution (see do_sigreturn) */
1009 host_to_target_sigset_internal(&target_old_set
, &ts
->signal_mask
);
1011 /* block signals in the handler */
1012 blocked_set
= ts
->in_sigsuspend
?
1013 &ts
->sigsuspend_mask
: &ts
->signal_mask
;
1014 sigorset(&ts
->signal_mask
, blocked_set
, &set
);
1015 ts
->in_sigsuspend
= 0;
1017 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1018 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
1020 CPUX86State
*env
= cpu_env
;
1021 if (env
->eflags
& VM_MASK
)
1022 save_v86_state(env
);
1025 /* prepare the stack frame of the virtual CPU */
1026 #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
1027 if (sa
->sa_flags
& TARGET_SA_SIGINFO
) {
1028 setup_rt_frame(sig
, sa
, &k
->info
, &target_old_set
, cpu_env
);
1030 setup_frame(sig
, sa
, &target_old_set
, cpu_env
);
1033 /* These targets do not have traditional signals. */
1034 setup_rt_frame(sig
, sa
, &k
->info
, &target_old_set
, cpu_env
);
1036 if (sa
->sa_flags
& TARGET_SA_RESETHAND
) {
1037 sa
->_sa_handler
= TARGET_SIG_DFL
;
1042 void process_pending_signals(CPUArchState
*cpu_env
)
1044 CPUState
*cpu
= env_cpu(cpu_env
);
1046 TaskState
*ts
= cpu
->opaque
;
1048 sigset_t
*blocked_set
;
1050 while (qatomic_read(&ts
->signal_pending
)) {
1051 /* FIXME: This is not threadsafe. */
1053 sigprocmask(SIG_SETMASK
, &set
, 0);
1056 sig
= ts
->sync_signal
.pending
;
1058 /* Synchronous signals are forced,
1059 * see force_sig_info() and callers in Linux
1060 * Note that not all of our queue_signal() calls in QEMU correspond
1061 * to force_sig_info() calls in Linux (some are send_sig_info()).
1062 * However it seems like a kernel bug to me to allow the process
1063 * to block a synchronous signal since it could then just end up
1064 * looping round and round indefinitely.
1066 if (sigismember(&ts
->signal_mask
, target_to_host_signal_table
[sig
])
1067 || sigact_table
[sig
- 1]._sa_handler
== TARGET_SIG_IGN
) {
1068 sigdelset(&ts
->signal_mask
, target_to_host_signal_table
[sig
]);
1069 sigact_table
[sig
- 1]._sa_handler
= TARGET_SIG_DFL
;
1072 handle_pending_signal(cpu_env
, sig
, &ts
->sync_signal
);
1075 for (sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
1076 blocked_set
= ts
->in_sigsuspend
?
1077 &ts
->sigsuspend_mask
: &ts
->signal_mask
;
1079 if (ts
->sigtab
[sig
- 1].pending
&&
1080 (!sigismember(blocked_set
,
1081 target_to_host_signal_table
[sig
]))) {
1082 handle_pending_signal(cpu_env
, sig
, &ts
->sigtab
[sig
- 1]);
1083 /* Restart scan from the beginning, as handle_pending_signal
1084 * might have resulted in a new synchronous signal (eg SIGSEGV).
1090 /* if no signal is pending, unblock signals and recheck (the act
1091 * of unblocking might cause us to take another host signal which
1092 * will set signal_pending again).
1094 qatomic_set(&ts
->signal_pending
, 0);
1095 ts
->in_sigsuspend
= 0;
1096 set
= ts
->signal_mask
;
1097 sigdelset(&set
, SIGSEGV
);
1098 sigdelset(&set
, SIGBUS
);
1099 sigprocmask(SIG_SETMASK
, &set
, 0);
1101 ts
->in_sigsuspend
= 0;