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 "gdbstub/user.h"
22 #include "hw/core/tcg-cpu-ops.h"
24 #include <sys/ucontext.h>
25 #include <sys/resource.h>
28 #include "user-internals.h"
32 #include "signal-common.h"
33 #include "host-signal.h"
34 #include "user/safe-syscall.h"
36 static struct target_sigaction sigact_table
[TARGET_NSIG
];
38 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
41 /* Fallback addresses into sigtramp page. */
42 abi_ulong default_sigreturn
;
43 abi_ulong default_rt_sigreturn
;
46 * System includes define _NSIG as SIGRTMAX + 1,
47 * but qemu (like the kernel) defines TARGET_NSIG as TARGET_SIGRTMAX
48 * and the first signal is SIGHUP defined as 1
49 * Signal number 0 is reserved for use as kill(pid, 0), to test whether
50 * a process exists without sending it a signal.
53 QEMU_BUILD_BUG_ON(__SIGRTMAX
+ 1 != _NSIG
);
55 static uint8_t host_to_target_signal_table
[_NSIG
] = {
56 #define MAKE_SIG_ENTRY(sig) [sig] = TARGET_##sig,
59 /* next signals stay the same */
62 static uint8_t target_to_host_signal_table
[TARGET_NSIG
+ 1];
64 /* valid sig is between 1 and _NSIG - 1 */
65 int host_to_target_signal(int sig
)
67 if (sig
< 1 || sig
>= _NSIG
) {
70 return host_to_target_signal_table
[sig
];
73 /* valid sig is between 1 and TARGET_NSIG */
74 int target_to_host_signal(int sig
)
76 if (sig
< 1 || sig
> TARGET_NSIG
) {
79 return target_to_host_signal_table
[sig
];
82 static inline void target_sigaddset(target_sigset_t
*set
, int signum
)
85 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
86 set
->sig
[signum
/ TARGET_NSIG_BPW
] |= mask
;
89 static inline int target_sigismember(const target_sigset_t
*set
, int signum
)
92 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
93 return ((set
->sig
[signum
/ TARGET_NSIG_BPW
] & mask
) != 0);
96 void host_to_target_sigset_internal(target_sigset_t
*d
,
99 int host_sig
, target_sig
;
100 target_sigemptyset(d
);
101 for (host_sig
= 1; host_sig
< _NSIG
; host_sig
++) {
102 target_sig
= host_to_target_signal(host_sig
);
103 if (target_sig
< 1 || target_sig
> TARGET_NSIG
) {
106 if (sigismember(s
, host_sig
)) {
107 target_sigaddset(d
, target_sig
);
112 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
117 host_to_target_sigset_internal(&d1
, s
);
118 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
119 d
->sig
[i
] = tswapal(d1
.sig
[i
]);
122 void target_to_host_sigset_internal(sigset_t
*d
,
123 const target_sigset_t
*s
)
125 int host_sig
, target_sig
;
127 for (target_sig
= 1; target_sig
<= TARGET_NSIG
; target_sig
++) {
128 host_sig
= target_to_host_signal(target_sig
);
129 if (host_sig
< 1 || host_sig
>= _NSIG
) {
132 if (target_sigismember(s
, target_sig
)) {
133 sigaddset(d
, host_sig
);
138 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
143 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
144 s1
.sig
[i
] = tswapal(s
->sig
[i
]);
145 target_to_host_sigset_internal(d
, &s1
);
148 void host_to_target_old_sigset(abi_ulong
*old_sigset
,
149 const sigset_t
*sigset
)
152 host_to_target_sigset(&d
, sigset
);
153 *old_sigset
= d
.sig
[0];
156 void target_to_host_old_sigset(sigset_t
*sigset
,
157 const abi_ulong
*old_sigset
)
162 d
.sig
[0] = *old_sigset
;
163 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
165 target_to_host_sigset(sigset
, &d
);
168 int block_signals(void)
170 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
173 /* It's OK to block everything including SIGSEGV, because we won't
174 * run any further guest code before unblocking signals in
175 * process_pending_signals().
178 sigprocmask(SIG_SETMASK
, &set
, 0);
180 return qatomic_xchg(&ts
->signal_pending
, 1);
183 /* Wrapper for sigprocmask function
184 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
185 * are host signal set, not guest ones. Returns -QEMU_ERESTARTSYS if
186 * a signal was already pending and the syscall must be restarted, or
188 * If set is NULL, this is guaranteed not to fail.
190 int do_sigprocmask(int how
, const sigset_t
*set
, sigset_t
*oldset
)
192 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
195 *oldset
= ts
->signal_mask
;
201 if (block_signals()) {
202 return -QEMU_ERESTARTSYS
;
207 sigorset(&ts
->signal_mask
, &ts
->signal_mask
, set
);
210 for (i
= 1; i
<= NSIG
; ++i
) {
211 if (sigismember(set
, i
)) {
212 sigdelset(&ts
->signal_mask
, i
);
217 ts
->signal_mask
= *set
;
220 g_assert_not_reached();
223 /* Silently ignore attempts to change blocking status of KILL or STOP */
224 sigdelset(&ts
->signal_mask
, SIGKILL
);
225 sigdelset(&ts
->signal_mask
, SIGSTOP
);
230 /* Just set the guest's signal mask to the specified value; the
231 * caller is assumed to have called block_signals() already.
233 void set_sigmask(const sigset_t
*set
)
235 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
237 ts
->signal_mask
= *set
;
240 /* sigaltstack management */
242 int on_sig_stack(unsigned long sp
)
244 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
246 return (sp
- ts
->sigaltstack_used
.ss_sp
247 < ts
->sigaltstack_used
.ss_size
);
250 int sas_ss_flags(unsigned long sp
)
252 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
254 return (ts
->sigaltstack_used
.ss_size
== 0 ? SS_DISABLE
255 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
258 abi_ulong
target_sigsp(abi_ulong sp
, struct target_sigaction
*ka
)
261 * This is the X/Open sanctioned signal stack switching.
263 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
265 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
)) {
266 return ts
->sigaltstack_used
.ss_sp
+ ts
->sigaltstack_used
.ss_size
;
271 void target_save_altstack(target_stack_t
*uss
, CPUArchState
*env
)
273 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
275 __put_user(ts
->sigaltstack_used
.ss_sp
, &uss
->ss_sp
);
276 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &uss
->ss_flags
);
277 __put_user(ts
->sigaltstack_used
.ss_size
, &uss
->ss_size
);
280 abi_long
target_restore_altstack(target_stack_t
*uss
, CPUArchState
*env
)
282 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
283 size_t minstacksize
= TARGET_MINSIGSTKSZ
;
286 #if defined(TARGET_PPC64)
287 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
288 struct image_info
*image
= ts
->info
;
289 if (get_ppc64_abi(image
) > 1) {
294 __get_user(ss
.ss_sp
, &uss
->ss_sp
);
295 __get_user(ss
.ss_size
, &uss
->ss_size
);
296 __get_user(ss
.ss_flags
, &uss
->ss_flags
);
298 if (on_sig_stack(get_sp_from_cpustate(env
))) {
299 return -TARGET_EPERM
;
302 switch (ss
.ss_flags
) {
304 return -TARGET_EINVAL
;
306 case TARGET_SS_DISABLE
:
311 case TARGET_SS_ONSTACK
:
313 if (ss
.ss_size
< minstacksize
) {
314 return -TARGET_ENOMEM
;
319 ts
->sigaltstack_used
.ss_sp
= ss
.ss_sp
;
320 ts
->sigaltstack_used
.ss_size
= ss
.ss_size
;
324 /* siginfo conversion */
326 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
327 const siginfo_t
*info
)
329 int sig
= host_to_target_signal(info
->si_signo
);
330 int si_code
= info
->si_code
;
332 tinfo
->si_signo
= sig
;
334 tinfo
->si_code
= info
->si_code
;
336 /* This memset serves two purposes:
337 * (1) ensure we don't leak random junk to the guest later
338 * (2) placate false positives from gcc about fields
339 * being used uninitialized if it chooses to inline both this
340 * function and tswap_siginfo() into host_to_target_siginfo().
342 memset(tinfo
->_sifields
._pad
, 0, sizeof(tinfo
->_sifields
._pad
));
344 /* This is awkward, because we have to use a combination of
345 * the si_code and si_signo to figure out which of the union's
346 * members are valid. (Within the host kernel it is always possible
347 * to tell, but the kernel carefully avoids giving userspace the
348 * high 16 bits of si_code, so we don't have the information to
349 * do this the easy way...) We therefore make our best guess,
350 * bearing in mind that a guest can spoof most of the si_codes
351 * via rt_sigqueueinfo() if it likes.
353 * Once we have made our guess, we record it in the top 16 bits of
354 * the si_code, so that tswap_siginfo() later can use it.
355 * tswap_siginfo() will strip these top bits out before writing
356 * si_code to the guest (sign-extending the lower bits).
363 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
364 * These are the only unspoofable si_code values.
366 tinfo
->_sifields
._kill
._pid
= info
->si_pid
;
367 tinfo
->_sifields
._kill
._uid
= info
->si_uid
;
368 si_type
= QEMU_SI_KILL
;
371 /* Everything else is spoofable. Make best guess based on signal */
374 tinfo
->_sifields
._sigchld
._pid
= info
->si_pid
;
375 tinfo
->_sifields
._sigchld
._uid
= info
->si_uid
;
376 if (si_code
== CLD_EXITED
)
377 tinfo
->_sifields
._sigchld
._status
= info
->si_status
;
379 tinfo
->_sifields
._sigchld
._status
380 = host_to_target_signal(info
->si_status
& 0x7f)
381 | (info
->si_status
& ~0x7f);
382 tinfo
->_sifields
._sigchld
._utime
= info
->si_utime
;
383 tinfo
->_sifields
._sigchld
._stime
= info
->si_stime
;
384 si_type
= QEMU_SI_CHLD
;
387 tinfo
->_sifields
._sigpoll
._band
= info
->si_band
;
388 tinfo
->_sifields
._sigpoll
._fd
= info
->si_fd
;
389 si_type
= QEMU_SI_POLL
;
392 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
393 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
394 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
395 /* XXX: potential problem if 64 bit */
396 tinfo
->_sifields
._rt
._sigval
.sival_ptr
397 = (abi_ulong
)(unsigned long)info
->si_value
.sival_ptr
;
398 si_type
= QEMU_SI_RT
;
404 tinfo
->si_code
= deposit32(si_code
, 16, 16, si_type
);
407 void tswap_siginfo(target_siginfo_t
*tinfo
,
408 const target_siginfo_t
*info
)
410 int si_type
= extract32(info
->si_code
, 16, 16);
411 int si_code
= sextract32(info
->si_code
, 0, 16);
413 __put_user(info
->si_signo
, &tinfo
->si_signo
);
414 __put_user(info
->si_errno
, &tinfo
->si_errno
);
415 __put_user(si_code
, &tinfo
->si_code
);
417 /* We can use our internal marker of which fields in the structure
418 * are valid, rather than duplicating the guesswork of
419 * host_to_target_siginfo_noswap() here.
423 __put_user(info
->_sifields
._kill
._pid
, &tinfo
->_sifields
._kill
._pid
);
424 __put_user(info
->_sifields
._kill
._uid
, &tinfo
->_sifields
._kill
._uid
);
427 __put_user(info
->_sifields
._timer
._timer1
,
428 &tinfo
->_sifields
._timer
._timer1
);
429 __put_user(info
->_sifields
._timer
._timer2
,
430 &tinfo
->_sifields
._timer
._timer2
);
433 __put_user(info
->_sifields
._sigpoll
._band
,
434 &tinfo
->_sifields
._sigpoll
._band
);
435 __put_user(info
->_sifields
._sigpoll
._fd
,
436 &tinfo
->_sifields
._sigpoll
._fd
);
439 __put_user(info
->_sifields
._sigfault
._addr
,
440 &tinfo
->_sifields
._sigfault
._addr
);
443 __put_user(info
->_sifields
._sigchld
._pid
,
444 &tinfo
->_sifields
._sigchld
._pid
);
445 __put_user(info
->_sifields
._sigchld
._uid
,
446 &tinfo
->_sifields
._sigchld
._uid
);
447 __put_user(info
->_sifields
._sigchld
._status
,
448 &tinfo
->_sifields
._sigchld
._status
);
449 __put_user(info
->_sifields
._sigchld
._utime
,
450 &tinfo
->_sifields
._sigchld
._utime
);
451 __put_user(info
->_sifields
._sigchld
._stime
,
452 &tinfo
->_sifields
._sigchld
._stime
);
455 __put_user(info
->_sifields
._rt
._pid
, &tinfo
->_sifields
._rt
._pid
);
456 __put_user(info
->_sifields
._rt
._uid
, &tinfo
->_sifields
._rt
._uid
);
457 __put_user(info
->_sifields
._rt
._sigval
.sival_ptr
,
458 &tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
461 g_assert_not_reached();
465 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
467 target_siginfo_t tgt_tmp
;
468 host_to_target_siginfo_noswap(&tgt_tmp
, info
);
469 tswap_siginfo(tinfo
, &tgt_tmp
);
472 /* XXX: we support only POSIX RT signals are used. */
473 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
474 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
476 /* This conversion is used only for the rt_sigqueueinfo syscall,
477 * and so we know that the _rt fields are the valid ones.
481 __get_user(info
->si_signo
, &tinfo
->si_signo
);
482 __get_user(info
->si_errno
, &tinfo
->si_errno
);
483 __get_user(info
->si_code
, &tinfo
->si_code
);
484 __get_user(info
->si_pid
, &tinfo
->_sifields
._rt
._pid
);
485 __get_user(info
->si_uid
, &tinfo
->_sifields
._rt
._uid
);
486 __get_user(sival_ptr
, &tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
487 info
->si_value
.sival_ptr
= (void *)(long)sival_ptr
;
490 static int fatal_signal (int sig
)
495 case TARGET_SIGWINCH
:
496 /* Ignored by default. */
503 /* Job control signals. */
510 /* returns 1 if given signal should dump core if not handled */
511 static int core_dump_signal(int sig
)
527 static void signal_table_init(void)
529 int host_sig
, target_sig
, count
;
532 * Signals are supported starting from TARGET_SIGRTMIN and going up
533 * until we run out of host realtime signals.
534 * glibc at least uses only the lower 2 rt signals and probably
535 * nobody's using the upper ones.
536 * it's why SIGRTMIN (34) is generally greater than __SIGRTMIN (32)
537 * To fix this properly we need to do manual signal delivery multiplexed
538 * over a single host signal.
539 * Attempts for configure "missing" signals via sigaction will be
542 for (host_sig
= SIGRTMIN
; host_sig
<= SIGRTMAX
; host_sig
++) {
543 target_sig
= host_sig
- SIGRTMIN
+ TARGET_SIGRTMIN
;
544 if (target_sig
<= TARGET_NSIG
) {
545 host_to_target_signal_table
[host_sig
] = target_sig
;
549 /* generate signal conversion tables */
550 for (target_sig
= 1; target_sig
<= TARGET_NSIG
; target_sig
++) {
551 target_to_host_signal_table
[target_sig
] = _NSIG
; /* poison */
553 for (host_sig
= 1; host_sig
< _NSIG
; host_sig
++) {
554 if (host_to_target_signal_table
[host_sig
] == 0) {
555 host_to_target_signal_table
[host_sig
] = host_sig
;
557 target_sig
= host_to_target_signal_table
[host_sig
];
558 if (target_sig
<= TARGET_NSIG
) {
559 target_to_host_signal_table
[target_sig
] = host_sig
;
563 if (trace_event_get_state_backends(TRACE_SIGNAL_TABLE_INIT
)) {
564 for (target_sig
= 1, count
= 0; target_sig
<= TARGET_NSIG
; target_sig
++) {
565 if (target_to_host_signal_table
[target_sig
] == _NSIG
) {
569 trace_signal_table_init(count
);
573 void signal_init(void)
575 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
576 struct sigaction act
;
577 struct sigaction oact
;
581 /* initialize signal conversion tables */
584 /* Set the signal mask from the host mask. */
585 sigprocmask(0, 0, &ts
->signal_mask
);
587 sigfillset(&act
.sa_mask
);
588 act
.sa_flags
= SA_SIGINFO
;
589 act
.sa_sigaction
= host_signal_handler
;
590 for(i
= 1; i
<= TARGET_NSIG
; i
++) {
591 host_sig
= target_to_host_signal(i
);
592 sigaction(host_sig
, NULL
, &oact
);
593 if (oact
.sa_sigaction
== (void *)SIG_IGN
) {
594 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_IGN
;
595 } else if (oact
.sa_sigaction
== (void *)SIG_DFL
) {
596 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_DFL
;
598 /* If there's already a handler installed then something has
599 gone horribly wrong, so don't even try to handle that case. */
600 /* Install some handlers for our own use. We need at least
601 SIGSEGV and SIGBUS, to detect exceptions. We can not just
602 trap all signals because it affects syscall interrupt
603 behavior. But do trap all default-fatal signals. */
604 if (fatal_signal (i
))
605 sigaction(host_sig
, &act
, NULL
);
609 /* Force a synchronously taken signal. The kernel force_sig() function
610 * also forces the signal to "not blocked, not ignored", but for QEMU
611 * that work is done in process_pending_signals().
613 void force_sig(int sig
)
615 CPUState
*cpu
= thread_cpu
;
616 CPUArchState
*env
= cpu_env(cpu
);
617 target_siginfo_t info
= {};
621 info
.si_code
= TARGET_SI_KERNEL
;
622 info
._sifields
._kill
._pid
= 0;
623 info
._sifields
._kill
._uid
= 0;
624 queue_signal(env
, info
.si_signo
, QEMU_SI_KILL
, &info
);
628 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
629 * 'force' part is handled in process_pending_signals().
631 void force_sig_fault(int sig
, int code
, abi_ulong addr
)
633 CPUState
*cpu
= thread_cpu
;
634 CPUArchState
*env
= cpu_env(cpu
);
635 target_siginfo_t info
= {};
640 info
._sifields
._sigfault
._addr
= addr
;
641 queue_signal(env
, sig
, QEMU_SI_FAULT
, &info
);
644 /* Force a SIGSEGV if we couldn't write to memory trying to set
645 * up the signal frame. oldsig is the signal we were trying to handle
646 * at the point of failure.
648 #if !defined(TARGET_RISCV)
649 void force_sigsegv(int oldsig
)
651 if (oldsig
== SIGSEGV
) {
652 /* Make sure we don't try to deliver the signal again; this will
653 * end up with handle_pending_signal() calling dump_core_and_abort().
655 sigact_table
[oldsig
- 1]._sa_handler
= TARGET_SIG_DFL
;
657 force_sig(TARGET_SIGSEGV
);
661 void cpu_loop_exit_sigsegv(CPUState
*cpu
, target_ulong addr
,
662 MMUAccessType access_type
, bool maperr
, uintptr_t ra
)
664 const struct TCGCPUOps
*tcg_ops
= CPU_GET_CLASS(cpu
)->tcg_ops
;
666 if (tcg_ops
->record_sigsegv
) {
667 tcg_ops
->record_sigsegv(cpu
, addr
, access_type
, maperr
, ra
);
670 force_sig_fault(TARGET_SIGSEGV
,
671 maperr
? TARGET_SEGV_MAPERR
: TARGET_SEGV_ACCERR
,
673 cpu
->exception_index
= EXCP_INTERRUPT
;
674 cpu_loop_exit_restore(cpu
, ra
);
677 void cpu_loop_exit_sigbus(CPUState
*cpu
, target_ulong addr
,
678 MMUAccessType access_type
, uintptr_t ra
)
680 const struct TCGCPUOps
*tcg_ops
= CPU_GET_CLASS(cpu
)->tcg_ops
;
682 if (tcg_ops
->record_sigbus
) {
683 tcg_ops
->record_sigbus(cpu
, addr
, access_type
, ra
);
686 force_sig_fault(TARGET_SIGBUS
, TARGET_BUS_ADRALN
, addr
);
687 cpu
->exception_index
= EXCP_INTERRUPT
;
688 cpu_loop_exit_restore(cpu
, ra
);
691 /* abort execution with signal */
693 void dump_core_and_abort(CPUArchState
*env
, int target_sig
)
695 CPUState
*cpu
= env_cpu(env
);
696 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
697 int host_sig
, core_dumped
= 0;
698 struct sigaction act
;
700 host_sig
= target_to_host_signal(target_sig
);
701 trace_user_dump_core_and_abort(env
, target_sig
, host_sig
);
702 gdb_signalled(env
, target_sig
);
704 /* dump core if supported by target binary format */
705 if (core_dump_signal(target_sig
) && (ts
->bprm
->core_dump
!= NULL
)) {
708 ((*ts
->bprm
->core_dump
)(target_sig
, env
) == 0);
711 /* we already dumped the core of target process, we don't want
712 * a coredump of qemu itself */
713 struct rlimit nodump
;
714 getrlimit(RLIMIT_CORE
, &nodump
);
716 setrlimit(RLIMIT_CORE
, &nodump
);
717 (void) fprintf(stderr
, "qemu: uncaught target signal %d (%s) - %s\n",
718 target_sig
, strsignal(host_sig
), "core dumped" );
721 preexit_cleanup(env
, 128 + target_sig
);
723 /* The proper exit code for dying from an uncaught signal is
724 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
725 * a negative value. To get the proper exit code we need to
726 * actually die from an uncaught signal. Here the default signal
727 * handler is installed, we send ourself a signal and we wait for
729 sigfillset(&act
.sa_mask
);
730 act
.sa_handler
= SIG_DFL
;
732 sigaction(host_sig
, &act
, NULL
);
734 /* For some reason raise(host_sig) doesn't send the signal when
735 * statically linked on x86-64. */
736 kill(getpid(), host_sig
);
738 /* Make sure the signal isn't masked (just reuse the mask inside
740 sigdelset(&act
.sa_mask
, host_sig
);
741 sigsuspend(&act
.sa_mask
);
747 /* queue a signal so that it will be send to the virtual CPU as soon
749 void queue_signal(CPUArchState
*env
, int sig
, int si_type
,
750 target_siginfo_t
*info
)
752 CPUState
*cpu
= env_cpu(env
);
753 TaskState
*ts
= cpu
->opaque
;
755 trace_user_queue_signal(env
, sig
);
757 info
->si_code
= deposit32(info
->si_code
, 16, 16, si_type
);
759 ts
->sync_signal
.info
= *info
;
760 ts
->sync_signal
.pending
= sig
;
761 /* signal that a new signal is pending */
762 qatomic_set(&ts
->signal_pending
, 1);
766 /* Adjust the signal context to rewind out of safe-syscall if we're in it */
767 static inline void rewind_if_in_safe_syscall(void *puc
)
769 host_sigcontext
*uc
= (host_sigcontext
*)puc
;
770 uintptr_t pcreg
= host_signal_pc(uc
);
772 if (pcreg
> (uintptr_t)safe_syscall_start
773 && pcreg
< (uintptr_t)safe_syscall_end
) {
774 host_signal_set_pc(uc
, (uintptr_t)safe_syscall_start
);
778 static void host_signal_handler(int host_sig
, siginfo_t
*info
, void *puc
)
780 CPUState
*cpu
= thread_cpu
;
781 CPUArchState
*env
= cpu_env(cpu
);
782 TaskState
*ts
= cpu
->opaque
;
783 target_siginfo_t tinfo
;
784 host_sigcontext
*uc
= puc
;
785 struct emulated_sigtable
*k
;
788 bool sync_sig
= false;
789 void *sigmask
= host_signal_mask(uc
);
792 * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
793 * handling wrt signal blocking and unwinding.
795 if ((host_sig
== SIGSEGV
|| host_sig
== SIGBUS
) && info
->si_code
> 0) {
796 MMUAccessType access_type
;
801 host_addr
= (uintptr_t)info
->si_addr
;
804 * Convert forcefully to guest address space: addresses outside
805 * reserved_va are still valid to report via SEGV_MAPERR.
807 guest_addr
= h2g_nocheck(host_addr
);
809 pc
= host_signal_pc(uc
);
810 is_write
= host_signal_write(info
, uc
);
811 access_type
= adjust_signal_pc(&pc
, is_write
);
813 if (host_sig
== SIGSEGV
) {
816 if (info
->si_code
== SEGV_ACCERR
&& h2g_valid(host_addr
)) {
817 /* If this was a write to a TB protected page, restart. */
819 handle_sigsegv_accerr_write(cpu
, sigmask
, pc
, guest_addr
)) {
824 * With reserved_va, the whole address space is PROT_NONE,
825 * which means that we may get ACCERR when we want MAPERR.
827 if (page_get_flags(guest_addr
) & PAGE_VALID
) {
830 info
->si_code
= SEGV_MAPERR
;
834 sigprocmask(SIG_SETMASK
, sigmask
, NULL
);
835 cpu_loop_exit_sigsegv(cpu
, guest_addr
, access_type
, maperr
, pc
);
837 sigprocmask(SIG_SETMASK
, sigmask
, NULL
);
838 if (info
->si_code
== BUS_ADRALN
) {
839 cpu_loop_exit_sigbus(cpu
, guest_addr
, access_type
, pc
);
846 /* get target signal number */
847 guest_sig
= host_to_target_signal(host_sig
);
848 if (guest_sig
< 1 || guest_sig
> TARGET_NSIG
) {
851 trace_user_host_signal(env
, host_sig
, guest_sig
);
853 host_to_target_siginfo_noswap(&tinfo
, info
);
854 k
= &ts
->sigtab
[guest_sig
- 1];
856 k
->pending
= guest_sig
;
857 ts
->signal_pending
= 1;
860 * For synchronous signals, unwind the cpu state to the faulting
861 * insn and then exit back to the main loop so that the signal
862 * is delivered immediately.
865 cpu
->exception_index
= EXCP_INTERRUPT
;
866 cpu_loop_exit_restore(cpu
, pc
);
869 rewind_if_in_safe_syscall(puc
);
872 * Block host signals until target signal handler entered. We
873 * can't block SIGSEGV or SIGBUS while we're executing guest
874 * code in case the guest code provokes one in the window between
875 * now and it getting out to the main loop. Signals will be
876 * unblocked again in process_pending_signals().
878 * WARNING: we cannot use sigfillset() here because the sigmask
879 * field is a kernel sigset_t, which is much smaller than the
880 * libc sigset_t which sigfillset() operates on. Using sigfillset()
881 * would write 0xff bytes off the end of the structure and trash
882 * data on the struct.
884 memset(sigmask
, 0xff, SIGSET_T_SIZE
);
885 sigdelset(sigmask
, SIGSEGV
);
886 sigdelset(sigmask
, SIGBUS
);
888 /* interrupt the virtual CPU as soon as possible */
889 cpu_exit(thread_cpu
);
892 /* do_sigaltstack() returns target values and errnos. */
893 /* compare linux/kernel/signal.c:do_sigaltstack() */
894 abi_long
do_sigaltstack(abi_ulong uss_addr
, abi_ulong uoss_addr
,
897 target_stack_t oss
, *uoss
= NULL
;
898 abi_long ret
= -TARGET_EFAULT
;
901 /* Verify writability now, but do not alter user memory yet. */
902 if (!lock_user_struct(VERIFY_WRITE
, uoss
, uoss_addr
, 0)) {
905 target_save_altstack(&oss
, env
);
911 if (!lock_user_struct(VERIFY_READ
, uss
, uss_addr
, 1)) {
914 ret
= target_restore_altstack(uss
, env
);
921 memcpy(uoss
, &oss
, sizeof(oss
));
922 unlock_user_struct(uoss
, uoss_addr
, 1);
929 unlock_user_struct(uoss
, uoss_addr
, 0);
934 /* do_sigaction() return target values and host errnos */
935 int do_sigaction(int sig
, const struct target_sigaction
*act
,
936 struct target_sigaction
*oact
, abi_ulong ka_restorer
)
938 struct target_sigaction
*k
;
939 struct sigaction act1
;
943 trace_signal_do_sigaction_guest(sig
, TARGET_NSIG
);
945 if (sig
< 1 || sig
> TARGET_NSIG
) {
946 return -TARGET_EINVAL
;
949 if (act
&& (sig
== TARGET_SIGKILL
|| sig
== TARGET_SIGSTOP
)) {
950 return -TARGET_EINVAL
;
953 if (block_signals()) {
954 return -QEMU_ERESTARTSYS
;
957 k
= &sigact_table
[sig
- 1];
959 __put_user(k
->_sa_handler
, &oact
->_sa_handler
);
960 __put_user(k
->sa_flags
, &oact
->sa_flags
);
961 #ifdef TARGET_ARCH_HAS_SA_RESTORER
962 __put_user(k
->sa_restorer
, &oact
->sa_restorer
);
965 oact
->sa_mask
= k
->sa_mask
;
968 __get_user(k
->_sa_handler
, &act
->_sa_handler
);
969 __get_user(k
->sa_flags
, &act
->sa_flags
);
970 #ifdef TARGET_ARCH_HAS_SA_RESTORER
971 __get_user(k
->sa_restorer
, &act
->sa_restorer
);
973 #ifdef TARGET_ARCH_HAS_KA_RESTORER
974 k
->ka_restorer
= ka_restorer
;
976 /* To be swapped in target_to_host_sigset. */
977 k
->sa_mask
= act
->sa_mask
;
979 /* we update the host linux signal state */
980 host_sig
= target_to_host_signal(sig
);
981 trace_signal_do_sigaction_host(host_sig
, TARGET_NSIG
);
982 if (host_sig
> SIGRTMAX
) {
983 /* we don't have enough host signals to map all target signals */
984 qemu_log_mask(LOG_UNIMP
, "Unsupported target signal #%d, ignored\n",
987 * we don't return an error here because some programs try to
988 * register an handler for all possible rt signals even if they
990 * An error here can abort them whereas there can be no problem
991 * to not have the signal available later.
992 * This is the case for golang,
993 * See https://github.com/golang/go/issues/33746
994 * So we silently ignore the error.
998 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
999 sigfillset(&act1
.sa_mask
);
1000 act1
.sa_flags
= SA_SIGINFO
;
1001 if (k
->sa_flags
& TARGET_SA_RESTART
)
1002 act1
.sa_flags
|= SA_RESTART
;
1003 /* NOTE: it is important to update the host kernel signal
1004 ignore state to avoid getting unexpected interrupted
1006 if (k
->_sa_handler
== TARGET_SIG_IGN
) {
1007 act1
.sa_sigaction
= (void *)SIG_IGN
;
1008 } else if (k
->_sa_handler
== TARGET_SIG_DFL
) {
1009 if (fatal_signal (sig
))
1010 act1
.sa_sigaction
= host_signal_handler
;
1012 act1
.sa_sigaction
= (void *)SIG_DFL
;
1014 act1
.sa_sigaction
= host_signal_handler
;
1016 ret
= sigaction(host_sig
, &act1
, NULL
);
1022 static void handle_pending_signal(CPUArchState
*cpu_env
, int sig
,
1023 struct emulated_sigtable
*k
)
1025 CPUState
*cpu
= env_cpu(cpu_env
);
1028 target_sigset_t target_old_set
;
1029 struct target_sigaction
*sa
;
1030 TaskState
*ts
= cpu
->opaque
;
1032 trace_user_handle_signal(cpu_env
, sig
);
1033 /* dequeue signal */
1036 sig
= gdb_handlesig(cpu
, sig
);
1039 handler
= TARGET_SIG_IGN
;
1041 sa
= &sigact_table
[sig
- 1];
1042 handler
= sa
->_sa_handler
;
1045 if (unlikely(qemu_loglevel_mask(LOG_STRACE
))) {
1046 print_taken_signal(sig
, &k
->info
);
1049 if (handler
== TARGET_SIG_DFL
) {
1050 /* default handler : ignore some signal. The other are job control or fatal */
1051 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
1052 kill(getpid(),SIGSTOP
);
1053 } else if (sig
!= TARGET_SIGCHLD
&&
1054 sig
!= TARGET_SIGURG
&&
1055 sig
!= TARGET_SIGWINCH
&&
1056 sig
!= TARGET_SIGCONT
) {
1057 dump_core_and_abort(cpu_env
, sig
);
1059 } else if (handler
== TARGET_SIG_IGN
) {
1061 } else if (handler
== TARGET_SIG_ERR
) {
1062 dump_core_and_abort(cpu_env
, sig
);
1064 /* compute the blocked signals during the handler execution */
1065 sigset_t
*blocked_set
;
1067 target_to_host_sigset(&set
, &sa
->sa_mask
);
1068 /* SA_NODEFER indicates that the current signal should not be
1069 blocked during the handler */
1070 if (!(sa
->sa_flags
& TARGET_SA_NODEFER
))
1071 sigaddset(&set
, target_to_host_signal(sig
));
1073 /* save the previous blocked signal state to restore it at the
1074 end of the signal execution (see do_sigreturn) */
1075 host_to_target_sigset_internal(&target_old_set
, &ts
->signal_mask
);
1077 /* block signals in the handler */
1078 blocked_set
= ts
->in_sigsuspend
?
1079 &ts
->sigsuspend_mask
: &ts
->signal_mask
;
1080 sigorset(&ts
->signal_mask
, blocked_set
, &set
);
1081 ts
->in_sigsuspend
= 0;
1083 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1084 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
1086 CPUX86State
*env
= cpu_env
;
1087 if (env
->eflags
& VM_MASK
)
1088 save_v86_state(env
);
1091 /* prepare the stack frame of the virtual CPU */
1092 #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
1093 if (sa
->sa_flags
& TARGET_SA_SIGINFO
) {
1094 setup_rt_frame(sig
, sa
, &k
->info
, &target_old_set
, cpu_env
);
1096 setup_frame(sig
, sa
, &target_old_set
, cpu_env
);
1099 /* These targets do not have traditional signals. */
1100 setup_rt_frame(sig
, sa
, &k
->info
, &target_old_set
, cpu_env
);
1102 if (sa
->sa_flags
& TARGET_SA_RESETHAND
) {
1103 sa
->_sa_handler
= TARGET_SIG_DFL
;
1108 void process_pending_signals(CPUArchState
*cpu_env
)
1110 CPUState
*cpu
= env_cpu(cpu_env
);
1112 TaskState
*ts
= cpu
->opaque
;
1114 sigset_t
*blocked_set
;
1116 while (qatomic_read(&ts
->signal_pending
)) {
1118 sigprocmask(SIG_SETMASK
, &set
, 0);
1121 sig
= ts
->sync_signal
.pending
;
1123 /* Synchronous signals are forced,
1124 * see force_sig_info() and callers in Linux
1125 * Note that not all of our queue_signal() calls in QEMU correspond
1126 * to force_sig_info() calls in Linux (some are send_sig_info()).
1127 * However it seems like a kernel bug to me to allow the process
1128 * to block a synchronous signal since it could then just end up
1129 * looping round and round indefinitely.
1131 if (sigismember(&ts
->signal_mask
, target_to_host_signal_table
[sig
])
1132 || sigact_table
[sig
- 1]._sa_handler
== TARGET_SIG_IGN
) {
1133 sigdelset(&ts
->signal_mask
, target_to_host_signal_table
[sig
]);
1134 sigact_table
[sig
- 1]._sa_handler
= TARGET_SIG_DFL
;
1137 handle_pending_signal(cpu_env
, sig
, &ts
->sync_signal
);
1140 for (sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
1141 blocked_set
= ts
->in_sigsuspend
?
1142 &ts
->sigsuspend_mask
: &ts
->signal_mask
;
1144 if (ts
->sigtab
[sig
- 1].pending
&&
1145 (!sigismember(blocked_set
,
1146 target_to_host_signal_table
[sig
]))) {
1147 handle_pending_signal(cpu_env
, sig
, &ts
->sigtab
[sig
- 1]);
1148 /* Restart scan from the beginning, as handle_pending_signal
1149 * might have resulted in a new synchronous signal (eg SIGSEGV).
1155 /* if no signal is pending, unblock signals and recheck (the act
1156 * of unblocking might cause us to take another host signal which
1157 * will set signal_pending again).
1159 qatomic_set(&ts
->signal_pending
, 0);
1160 ts
->in_sigsuspend
= 0;
1161 set
= ts
->signal_mask
;
1162 sigdelset(&set
, SIGSEGV
);
1163 sigdelset(&set
, SIGBUS
);
1164 sigprocmask(SIG_SETMASK
, &set
, 0);
1166 ts
->in_sigsuspend
= 0;
1169 int process_sigsuspend_mask(sigset_t
**pset
, target_ulong sigset
,
1170 target_ulong sigsize
)
1172 TaskState
*ts
= (TaskState
*)thread_cpu
->opaque
;
1173 sigset_t
*host_set
= &ts
->sigsuspend_mask
;
1174 target_sigset_t
*target_sigset
;
1176 if (sigsize
!= sizeof(*target_sigset
)) {
1177 /* Like the kernel, we enforce correct size sigsets */
1178 return -TARGET_EINVAL
;
1181 target_sigset
= lock_user(VERIFY_READ
, sigset
, sigsize
, 1);
1182 if (!target_sigset
) {
1183 return -TARGET_EFAULT
;
1185 target_to_host_sigset(host_set
, target_sigset
);
1186 unlock_user(target_sigset
, sigset
, 0);