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
);
654 /* Force a SIGSEGV if we couldn't write to memory trying to set
655 * up the signal frame. oldsig is the signal we were trying to handle
656 * at the point of failure.
658 #if !defined(TARGET_RISCV)
659 void force_sigsegv(int oldsig
)
661 if (oldsig
== SIGSEGV
) {
662 /* Make sure we don't try to deliver the signal again; this will
663 * end up with handle_pending_signal() calling dump_core_and_abort().
665 sigact_table
[oldsig
- 1]._sa_handler
= TARGET_SIG_DFL
;
667 force_sig(TARGET_SIGSEGV
);
672 /* abort execution with signal */
673 static void QEMU_NORETURN
dump_core_and_abort(int target_sig
)
675 CPUState
*cpu
= thread_cpu
;
676 CPUArchState
*env
= cpu
->env_ptr
;
677 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
678 int host_sig
, core_dumped
= 0;
679 struct sigaction act
;
681 host_sig
= target_to_host_signal(target_sig
);
682 trace_user_force_sig(env
, target_sig
, host_sig
);
683 gdb_signalled(env
, target_sig
);
685 /* dump core if supported by target binary format */
686 if (core_dump_signal(target_sig
) && (ts
->bprm
->core_dump
!= NULL
)) {
689 ((*ts
->bprm
->core_dump
)(target_sig
, env
) == 0);
692 /* we already dumped the core of target process, we don't want
693 * a coredump of qemu itself */
694 struct rlimit nodump
;
695 getrlimit(RLIMIT_CORE
, &nodump
);
697 setrlimit(RLIMIT_CORE
, &nodump
);
698 (void) fprintf(stderr
, "qemu: uncaught target signal %d (%s) - %s\n",
699 target_sig
, strsignal(host_sig
), "core dumped" );
702 /* The proper exit code for dying from an uncaught signal is
703 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
704 * a negative value. To get the proper exit code we need to
705 * actually die from an uncaught signal. Here the default signal
706 * handler is installed, we send ourself a signal and we wait for
708 sigfillset(&act
.sa_mask
);
709 act
.sa_handler
= SIG_DFL
;
711 sigaction(host_sig
, &act
, NULL
);
713 /* For some reason raise(host_sig) doesn't send the signal when
714 * statically linked on x86-64. */
715 kill(getpid(), host_sig
);
717 /* Make sure the signal isn't masked (just reuse the mask inside
719 sigdelset(&act
.sa_mask
, host_sig
);
720 sigsuspend(&act
.sa_mask
);
726 /* queue a signal so that it will be send to the virtual CPU as soon
728 int queue_signal(CPUArchState
*env
, int sig
, int si_type
,
729 target_siginfo_t
*info
)
731 CPUState
*cpu
= env_cpu(env
);
732 TaskState
*ts
= cpu
->opaque
;
734 trace_user_queue_signal(env
, sig
);
736 info
->si_code
= deposit32(info
->si_code
, 16, 16, si_type
);
738 ts
->sync_signal
.info
= *info
;
739 ts
->sync_signal
.pending
= sig
;
740 /* signal that a new signal is pending */
741 qatomic_set(&ts
->signal_pending
, 1);
742 return 1; /* indicates that the signal was queued */
745 #ifndef HAVE_SAFE_SYSCALL
746 static inline void rewind_if_in_safe_syscall(void *puc
)
748 /* Default version: never rewind */
752 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
755 CPUArchState
*env
= thread_cpu
->env_ptr
;
756 CPUState
*cpu
= env_cpu(env
);
757 TaskState
*ts
= cpu
->opaque
;
760 target_siginfo_t tinfo
;
761 ucontext_t
*uc
= puc
;
762 struct emulated_sigtable
*k
;
764 /* the CPU emulator uses some host signals to detect exceptions,
765 we forward to it some signals */
766 if ((host_signum
== SIGSEGV
|| host_signum
== SIGBUS
)
767 && info
->si_code
> 0) {
768 if (cpu_signal_handler(host_signum
, info
, puc
))
772 /* get target signal number */
773 sig
= host_to_target_signal(host_signum
);
774 if (sig
< 1 || sig
> TARGET_NSIG
)
776 trace_user_host_signal(env
, host_signum
, sig
);
778 rewind_if_in_safe_syscall(puc
);
780 host_to_target_siginfo_noswap(&tinfo
, info
);
781 k
= &ts
->sigtab
[sig
- 1];
784 ts
->signal_pending
= 1;
786 /* Block host signals until target signal handler entered. We
787 * can't block SIGSEGV or SIGBUS while we're executing guest
788 * code in case the guest code provokes one in the window between
789 * now and it getting out to the main loop. Signals will be
790 * unblocked again in process_pending_signals().
792 * WARNING: we cannot use sigfillset() here because the uc_sigmask
793 * field is a kernel sigset_t, which is much smaller than the
794 * libc sigset_t which sigfillset() operates on. Using sigfillset()
795 * would write 0xff bytes off the end of the structure and trash
796 * data on the struct.
797 * We can't use sizeof(uc->uc_sigmask) either, because the libc
798 * headers define the struct field with the wrong (too large) type.
800 memset(&uc
->uc_sigmask
, 0xff, SIGSET_T_SIZE
);
801 sigdelset(&uc
->uc_sigmask
, SIGSEGV
);
802 sigdelset(&uc
->uc_sigmask
, SIGBUS
);
804 /* interrupt the virtual CPU as soon as possible */
805 cpu_exit(thread_cpu
);
808 /* do_sigaltstack() returns target values and errnos. */
809 /* compare linux/kernel/signal.c:do_sigaltstack() */
810 abi_long
do_sigaltstack(abi_ulong uss_addr
, abi_ulong uoss_addr
,
813 target_stack_t oss
, *uoss
= NULL
;
814 abi_long ret
= -TARGET_EFAULT
;
817 /* Verify writability now, but do not alter user memory yet. */
818 if (!lock_user_struct(VERIFY_WRITE
, uoss
, uoss_addr
, 0)) {
821 target_save_altstack(&oss
, env
);
827 if (!lock_user_struct(VERIFY_READ
, uss
, uss_addr
, 1)) {
830 ret
= target_restore_altstack(uss
, env
);
837 memcpy(uoss
, &oss
, sizeof(oss
));
838 unlock_user_struct(uoss
, uoss_addr
, 1);
845 unlock_user_struct(uoss
, uoss_addr
, 0);
850 /* do_sigaction() return target values and host errnos */
851 int do_sigaction(int sig
, const struct target_sigaction
*act
,
852 struct target_sigaction
*oact
, abi_ulong ka_restorer
)
854 struct target_sigaction
*k
;
855 struct sigaction act1
;
859 trace_signal_do_sigaction_guest(sig
, TARGET_NSIG
);
861 if (sig
< 1 || sig
> TARGET_NSIG
) {
862 return -TARGET_EINVAL
;
865 if (act
&& (sig
== TARGET_SIGKILL
|| sig
== TARGET_SIGSTOP
)) {
866 return -TARGET_EINVAL
;
869 if (block_signals()) {
870 return -TARGET_ERESTARTSYS
;
873 k
= &sigact_table
[sig
- 1];
875 __put_user(k
->_sa_handler
, &oact
->_sa_handler
);
876 __put_user(k
->sa_flags
, &oact
->sa_flags
);
877 #ifdef TARGET_ARCH_HAS_SA_RESTORER
878 __put_user(k
->sa_restorer
, &oact
->sa_restorer
);
881 oact
->sa_mask
= k
->sa_mask
;
884 /* FIXME: This is not threadsafe. */
885 __get_user(k
->_sa_handler
, &act
->_sa_handler
);
886 __get_user(k
->sa_flags
, &act
->sa_flags
);
887 #ifdef TARGET_ARCH_HAS_SA_RESTORER
888 __get_user(k
->sa_restorer
, &act
->sa_restorer
);
890 #ifdef TARGET_ARCH_HAS_KA_RESTORER
891 k
->ka_restorer
= ka_restorer
;
893 /* To be swapped in target_to_host_sigset. */
894 k
->sa_mask
= act
->sa_mask
;
896 /* we update the host linux signal state */
897 host_sig
= target_to_host_signal(sig
);
898 trace_signal_do_sigaction_host(host_sig
, TARGET_NSIG
);
899 if (host_sig
> SIGRTMAX
) {
900 /* we don't have enough host signals to map all target signals */
901 qemu_log_mask(LOG_UNIMP
, "Unsupported target signal #%d, ignored\n",
904 * we don't return an error here because some programs try to
905 * register an handler for all possible rt signals even if they
907 * An error here can abort them whereas there can be no problem
908 * to not have the signal available later.
909 * This is the case for golang,
910 * See https://github.com/golang/go/issues/33746
911 * So we silently ignore the error.
915 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
916 sigfillset(&act1
.sa_mask
);
917 act1
.sa_flags
= SA_SIGINFO
;
918 if (k
->sa_flags
& TARGET_SA_RESTART
)
919 act1
.sa_flags
|= SA_RESTART
;
920 /* NOTE: it is important to update the host kernel signal
921 ignore state to avoid getting unexpected interrupted
923 if (k
->_sa_handler
== TARGET_SIG_IGN
) {
924 act1
.sa_sigaction
= (void *)SIG_IGN
;
925 } else if (k
->_sa_handler
== TARGET_SIG_DFL
) {
926 if (fatal_signal (sig
))
927 act1
.sa_sigaction
= host_signal_handler
;
929 act1
.sa_sigaction
= (void *)SIG_DFL
;
931 act1
.sa_sigaction
= host_signal_handler
;
933 ret
= sigaction(host_sig
, &act1
, NULL
);
939 static void handle_pending_signal(CPUArchState
*cpu_env
, int sig
,
940 struct emulated_sigtable
*k
)
942 CPUState
*cpu
= env_cpu(cpu_env
);
945 target_sigset_t target_old_set
;
946 struct target_sigaction
*sa
;
947 TaskState
*ts
= cpu
->opaque
;
949 trace_user_handle_signal(cpu_env
, sig
);
953 sig
= gdb_handlesig(cpu
, sig
);
956 handler
= TARGET_SIG_IGN
;
958 sa
= &sigact_table
[sig
- 1];
959 handler
= sa
->_sa_handler
;
962 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
963 print_taken_signal(sig
, &k
->info
);
966 if (handler
== TARGET_SIG_DFL
) {
967 /* default handler : ignore some signal. The other are job control or fatal */
968 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
969 kill(getpid(),SIGSTOP
);
970 } else if (sig
!= TARGET_SIGCHLD
&&
971 sig
!= TARGET_SIGURG
&&
972 sig
!= TARGET_SIGWINCH
&&
973 sig
!= TARGET_SIGCONT
) {
974 dump_core_and_abort(sig
);
976 } else if (handler
== TARGET_SIG_IGN
) {
978 } else if (handler
== TARGET_SIG_ERR
) {
979 dump_core_and_abort(sig
);
981 /* compute the blocked signals during the handler execution */
982 sigset_t
*blocked_set
;
984 target_to_host_sigset(&set
, &sa
->sa_mask
);
985 /* SA_NODEFER indicates that the current signal should not be
986 blocked during the handler */
987 if (!(sa
->sa_flags
& TARGET_SA_NODEFER
))
988 sigaddset(&set
, target_to_host_signal(sig
));
990 /* save the previous blocked signal state to restore it at the
991 end of the signal execution (see do_sigreturn) */
992 host_to_target_sigset_internal(&target_old_set
, &ts
->signal_mask
);
994 /* block signals in the handler */
995 blocked_set
= ts
->in_sigsuspend
?
996 &ts
->sigsuspend_mask
: &ts
->signal_mask
;
997 sigorset(&ts
->signal_mask
, blocked_set
, &set
);
998 ts
->in_sigsuspend
= 0;
1000 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1001 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
1003 CPUX86State
*env
= cpu_env
;
1004 if (env
->eflags
& VM_MASK
)
1005 save_v86_state(env
);
1008 /* prepare the stack frame of the virtual CPU */
1009 #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
1010 if (sa
->sa_flags
& TARGET_SA_SIGINFO
) {
1011 setup_rt_frame(sig
, sa
, &k
->info
, &target_old_set
, cpu_env
);
1013 setup_frame(sig
, sa
, &target_old_set
, cpu_env
);
1016 /* These targets do not have traditional signals. */
1017 setup_rt_frame(sig
, sa
, &k
->info
, &target_old_set
, cpu_env
);
1019 if (sa
->sa_flags
& TARGET_SA_RESETHAND
) {
1020 sa
->_sa_handler
= TARGET_SIG_DFL
;
1025 void process_pending_signals(CPUArchState
*cpu_env
)
1027 CPUState
*cpu
= env_cpu(cpu_env
);
1029 TaskState
*ts
= cpu
->opaque
;
1031 sigset_t
*blocked_set
;
1033 while (qatomic_read(&ts
->signal_pending
)) {
1034 /* FIXME: This is not threadsafe. */
1036 sigprocmask(SIG_SETMASK
, &set
, 0);
1039 sig
= ts
->sync_signal
.pending
;
1041 /* Synchronous signals are forced,
1042 * see force_sig_info() and callers in Linux
1043 * Note that not all of our queue_signal() calls in QEMU correspond
1044 * to force_sig_info() calls in Linux (some are send_sig_info()).
1045 * However it seems like a kernel bug to me to allow the process
1046 * to block a synchronous signal since it could then just end up
1047 * looping round and round indefinitely.
1049 if (sigismember(&ts
->signal_mask
, target_to_host_signal_table
[sig
])
1050 || sigact_table
[sig
- 1]._sa_handler
== TARGET_SIG_IGN
) {
1051 sigdelset(&ts
->signal_mask
, target_to_host_signal_table
[sig
]);
1052 sigact_table
[sig
- 1]._sa_handler
= TARGET_SIG_DFL
;
1055 handle_pending_signal(cpu_env
, sig
, &ts
->sync_signal
);
1058 for (sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
1059 blocked_set
= ts
->in_sigsuspend
?
1060 &ts
->sigsuspend_mask
: &ts
->signal_mask
;
1062 if (ts
->sigtab
[sig
- 1].pending
&&
1063 (!sigismember(blocked_set
,
1064 target_to_host_signal_table
[sig
]))) {
1065 handle_pending_signal(cpu_env
, sig
, &ts
->sigtab
[sig
- 1]);
1066 /* Restart scan from the beginning, as handle_pending_signal
1067 * might have resulted in a new synchronous signal (eg SIGSEGV).
1073 /* if no signal is pending, unblock signals and recheck (the act
1074 * of unblocking might cause us to take another host signal which
1075 * will set signal_pending again).
1077 qatomic_set(&ts
->signal_pending
, 0);
1078 ts
->in_sigsuspend
= 0;
1079 set
= ts
->signal_mask
;
1080 sigdelset(&set
, SIGSEGV
);
1081 sigdelset(&set
, SIGBUS
);
1082 sigprocmask(SIG_SETMASK
, &set
, 0);
1084 ts
->in_sigsuspend
= 0;