block/io: bdrv_refresh_limits(): use ERRP_GUARD
[qemu/kevin.git] / linux-user / signal.c
blob73de934c65117c5caa0f2dabe8e29786397fee37
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qemu/bitops.h"
21 #include <sys/ucontext.h>
22 #include <sys/resource.h>
24 #include "qemu.h"
25 #include "trace.h"
26 #include "signal-common.h"
28 static struct target_sigaction sigact_table[TARGET_NSIG];
30 static void host_signal_handler(int host_signum, siginfo_t *info,
31 void *puc);
35 * System includes define _NSIG as SIGRTMAX + 1,
36 * but qemu (like the kernel) defines TARGET_NSIG as TARGET_SIGRTMAX
37 * and the first signal is SIGHUP defined as 1
38 * Signal number 0 is reserved for use as kill(pid, 0), to test whether
39 * a process exists without sending it a signal.
41 QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG);
42 static uint8_t host_to_target_signal_table[_NSIG] = {
43 [SIGHUP] = TARGET_SIGHUP,
44 [SIGINT] = TARGET_SIGINT,
45 [SIGQUIT] = TARGET_SIGQUIT,
46 [SIGILL] = TARGET_SIGILL,
47 [SIGTRAP] = TARGET_SIGTRAP,
48 [SIGABRT] = TARGET_SIGABRT,
49 /* [SIGIOT] = TARGET_SIGIOT,*/
50 [SIGBUS] = TARGET_SIGBUS,
51 [SIGFPE] = TARGET_SIGFPE,
52 [SIGKILL] = TARGET_SIGKILL,
53 [SIGUSR1] = TARGET_SIGUSR1,
54 [SIGSEGV] = TARGET_SIGSEGV,
55 [SIGUSR2] = TARGET_SIGUSR2,
56 [SIGPIPE] = TARGET_SIGPIPE,
57 [SIGALRM] = TARGET_SIGALRM,
58 [SIGTERM] = TARGET_SIGTERM,
59 #ifdef SIGSTKFLT
60 [SIGSTKFLT] = TARGET_SIGSTKFLT,
61 #endif
62 [SIGCHLD] = TARGET_SIGCHLD,
63 [SIGCONT] = TARGET_SIGCONT,
64 [SIGSTOP] = TARGET_SIGSTOP,
65 [SIGTSTP] = TARGET_SIGTSTP,
66 [SIGTTIN] = TARGET_SIGTTIN,
67 [SIGTTOU] = TARGET_SIGTTOU,
68 [SIGURG] = TARGET_SIGURG,
69 [SIGXCPU] = TARGET_SIGXCPU,
70 [SIGXFSZ] = TARGET_SIGXFSZ,
71 [SIGVTALRM] = TARGET_SIGVTALRM,
72 [SIGPROF] = TARGET_SIGPROF,
73 [SIGWINCH] = TARGET_SIGWINCH,
74 [SIGIO] = TARGET_SIGIO,
75 [SIGPWR] = TARGET_SIGPWR,
76 [SIGSYS] = TARGET_SIGSYS,
77 /* next signals stay the same */
80 static uint8_t target_to_host_signal_table[TARGET_NSIG + 1];
82 /* valid sig is between 1 and _NSIG - 1 */
83 int host_to_target_signal(int sig)
85 if (sig < 1 || sig >= _NSIG) {
86 return sig;
88 return host_to_target_signal_table[sig];
91 /* valid sig is between 1 and TARGET_NSIG */
92 int target_to_host_signal(int sig)
94 if (sig < 1 || sig > TARGET_NSIG) {
95 return sig;
97 return target_to_host_signal_table[sig];
100 static inline void target_sigaddset(target_sigset_t *set, int signum)
102 signum--;
103 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
104 set->sig[signum / TARGET_NSIG_BPW] |= mask;
107 static inline int target_sigismember(const target_sigset_t *set, int signum)
109 signum--;
110 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
111 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
114 void host_to_target_sigset_internal(target_sigset_t *d,
115 const sigset_t *s)
117 int host_sig, target_sig;
118 target_sigemptyset(d);
119 for (host_sig = 1; host_sig < _NSIG; host_sig++) {
120 target_sig = host_to_target_signal(host_sig);
121 if (target_sig < 1 || target_sig > TARGET_NSIG) {
122 continue;
124 if (sigismember(s, host_sig)) {
125 target_sigaddset(d, target_sig);
130 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
132 target_sigset_t d1;
133 int i;
135 host_to_target_sigset_internal(&d1, s);
136 for(i = 0;i < TARGET_NSIG_WORDS; i++)
137 d->sig[i] = tswapal(d1.sig[i]);
140 void target_to_host_sigset_internal(sigset_t *d,
141 const target_sigset_t *s)
143 int host_sig, target_sig;
144 sigemptyset(d);
145 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
146 host_sig = target_to_host_signal(target_sig);
147 if (host_sig < 1 || host_sig >= _NSIG) {
148 continue;
150 if (target_sigismember(s, target_sig)) {
151 sigaddset(d, host_sig);
156 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
158 target_sigset_t s1;
159 int i;
161 for(i = 0;i < TARGET_NSIG_WORDS; i++)
162 s1.sig[i] = tswapal(s->sig[i]);
163 target_to_host_sigset_internal(d, &s1);
166 void host_to_target_old_sigset(abi_ulong *old_sigset,
167 const sigset_t *sigset)
169 target_sigset_t d;
170 host_to_target_sigset(&d, sigset);
171 *old_sigset = d.sig[0];
174 void target_to_host_old_sigset(sigset_t *sigset,
175 const abi_ulong *old_sigset)
177 target_sigset_t d;
178 int i;
180 d.sig[0] = *old_sigset;
181 for(i = 1;i < TARGET_NSIG_WORDS; i++)
182 d.sig[i] = 0;
183 target_to_host_sigset(sigset, &d);
186 int block_signals(void)
188 TaskState *ts = (TaskState *)thread_cpu->opaque;
189 sigset_t set;
191 /* It's OK to block everything including SIGSEGV, because we won't
192 * run any further guest code before unblocking signals in
193 * process_pending_signals().
195 sigfillset(&set);
196 sigprocmask(SIG_SETMASK, &set, 0);
198 return qatomic_xchg(&ts->signal_pending, 1);
201 /* Wrapper for sigprocmask function
202 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
203 * are host signal set, not guest ones. Returns -TARGET_ERESTARTSYS if
204 * a signal was already pending and the syscall must be restarted, or
205 * 0 on success.
206 * If set is NULL, this is guaranteed not to fail.
208 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
210 TaskState *ts = (TaskState *)thread_cpu->opaque;
212 if (oldset) {
213 *oldset = ts->signal_mask;
216 if (set) {
217 int i;
219 if (block_signals()) {
220 return -TARGET_ERESTARTSYS;
223 switch (how) {
224 case SIG_BLOCK:
225 sigorset(&ts->signal_mask, &ts->signal_mask, set);
226 break;
227 case SIG_UNBLOCK:
228 for (i = 1; i <= NSIG; ++i) {
229 if (sigismember(set, i)) {
230 sigdelset(&ts->signal_mask, i);
233 break;
234 case SIG_SETMASK:
235 ts->signal_mask = *set;
236 break;
237 default:
238 g_assert_not_reached();
241 /* Silently ignore attempts to change blocking status of KILL or STOP */
242 sigdelset(&ts->signal_mask, SIGKILL);
243 sigdelset(&ts->signal_mask, SIGSTOP);
245 return 0;
248 #if !defined(TARGET_NIOS2)
249 /* Just set the guest's signal mask to the specified value; the
250 * caller is assumed to have called block_signals() already.
252 void set_sigmask(const sigset_t *set)
254 TaskState *ts = (TaskState *)thread_cpu->opaque;
256 ts->signal_mask = *set;
258 #endif
260 /* sigaltstack management */
262 int on_sig_stack(unsigned long sp)
264 TaskState *ts = (TaskState *)thread_cpu->opaque;
266 return (sp - ts->sigaltstack_used.ss_sp
267 < ts->sigaltstack_used.ss_size);
270 int sas_ss_flags(unsigned long sp)
272 TaskState *ts = (TaskState *)thread_cpu->opaque;
274 return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE
275 : on_sig_stack(sp) ? SS_ONSTACK : 0);
278 abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka)
281 * This is the X/Open sanctioned signal stack switching.
283 TaskState *ts = (TaskState *)thread_cpu->opaque;
285 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
286 return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
288 return sp;
291 void target_save_altstack(target_stack_t *uss, CPUArchState *env)
293 TaskState *ts = (TaskState *)thread_cpu->opaque;
295 __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp);
296 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags);
297 __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
300 /* siginfo conversion */
302 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
303 const siginfo_t *info)
305 int sig = host_to_target_signal(info->si_signo);
306 int si_code = info->si_code;
307 int si_type;
308 tinfo->si_signo = sig;
309 tinfo->si_errno = 0;
310 tinfo->si_code = info->si_code;
312 /* This memset serves two purposes:
313 * (1) ensure we don't leak random junk to the guest later
314 * (2) placate false positives from gcc about fields
315 * being used uninitialized if it chooses to inline both this
316 * function and tswap_siginfo() into host_to_target_siginfo().
318 memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
320 /* This is awkward, because we have to use a combination of
321 * the si_code and si_signo to figure out which of the union's
322 * members are valid. (Within the host kernel it is always possible
323 * to tell, but the kernel carefully avoids giving userspace the
324 * high 16 bits of si_code, so we don't have the information to
325 * do this the easy way...) We therefore make our best guess,
326 * bearing in mind that a guest can spoof most of the si_codes
327 * via rt_sigqueueinfo() if it likes.
329 * Once we have made our guess, we record it in the top 16 bits of
330 * the si_code, so that tswap_siginfo() later can use it.
331 * tswap_siginfo() will strip these top bits out before writing
332 * si_code to the guest (sign-extending the lower bits).
335 switch (si_code) {
336 case SI_USER:
337 case SI_TKILL:
338 case SI_KERNEL:
339 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
340 * These are the only unspoofable si_code values.
342 tinfo->_sifields._kill._pid = info->si_pid;
343 tinfo->_sifields._kill._uid = info->si_uid;
344 si_type = QEMU_SI_KILL;
345 break;
346 default:
347 /* Everything else is spoofable. Make best guess based on signal */
348 switch (sig) {
349 case TARGET_SIGCHLD:
350 tinfo->_sifields._sigchld._pid = info->si_pid;
351 tinfo->_sifields._sigchld._uid = info->si_uid;
352 tinfo->_sifields._sigchld._status
353 = host_to_target_waitstatus(info->si_status);
354 tinfo->_sifields._sigchld._utime = info->si_utime;
355 tinfo->_sifields._sigchld._stime = info->si_stime;
356 si_type = QEMU_SI_CHLD;
357 break;
358 case TARGET_SIGIO:
359 tinfo->_sifields._sigpoll._band = info->si_band;
360 tinfo->_sifields._sigpoll._fd = info->si_fd;
361 si_type = QEMU_SI_POLL;
362 break;
363 default:
364 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
365 tinfo->_sifields._rt._pid = info->si_pid;
366 tinfo->_sifields._rt._uid = info->si_uid;
367 /* XXX: potential problem if 64 bit */
368 tinfo->_sifields._rt._sigval.sival_ptr
369 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
370 si_type = QEMU_SI_RT;
371 break;
373 break;
376 tinfo->si_code = deposit32(si_code, 16, 16, si_type);
379 void tswap_siginfo(target_siginfo_t *tinfo,
380 const target_siginfo_t *info)
382 int si_type = extract32(info->si_code, 16, 16);
383 int si_code = sextract32(info->si_code, 0, 16);
385 __put_user(info->si_signo, &tinfo->si_signo);
386 __put_user(info->si_errno, &tinfo->si_errno);
387 __put_user(si_code, &tinfo->si_code);
389 /* We can use our internal marker of which fields in the structure
390 * are valid, rather than duplicating the guesswork of
391 * host_to_target_siginfo_noswap() here.
393 switch (si_type) {
394 case QEMU_SI_KILL:
395 __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
396 __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
397 break;
398 case QEMU_SI_TIMER:
399 __put_user(info->_sifields._timer._timer1,
400 &tinfo->_sifields._timer._timer1);
401 __put_user(info->_sifields._timer._timer2,
402 &tinfo->_sifields._timer._timer2);
403 break;
404 case QEMU_SI_POLL:
405 __put_user(info->_sifields._sigpoll._band,
406 &tinfo->_sifields._sigpoll._band);
407 __put_user(info->_sifields._sigpoll._fd,
408 &tinfo->_sifields._sigpoll._fd);
409 break;
410 case QEMU_SI_FAULT:
411 __put_user(info->_sifields._sigfault._addr,
412 &tinfo->_sifields._sigfault._addr);
413 break;
414 case QEMU_SI_CHLD:
415 __put_user(info->_sifields._sigchld._pid,
416 &tinfo->_sifields._sigchld._pid);
417 __put_user(info->_sifields._sigchld._uid,
418 &tinfo->_sifields._sigchld._uid);
419 __put_user(info->_sifields._sigchld._status,
420 &tinfo->_sifields._sigchld._status);
421 __put_user(info->_sifields._sigchld._utime,
422 &tinfo->_sifields._sigchld._utime);
423 __put_user(info->_sifields._sigchld._stime,
424 &tinfo->_sifields._sigchld._stime);
425 break;
426 case QEMU_SI_RT:
427 __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
428 __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
429 __put_user(info->_sifields._rt._sigval.sival_ptr,
430 &tinfo->_sifields._rt._sigval.sival_ptr);
431 break;
432 default:
433 g_assert_not_reached();
437 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
439 target_siginfo_t tgt_tmp;
440 host_to_target_siginfo_noswap(&tgt_tmp, info);
441 tswap_siginfo(tinfo, &tgt_tmp);
444 /* XXX: we support only POSIX RT signals are used. */
445 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
446 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
448 /* This conversion is used only for the rt_sigqueueinfo syscall,
449 * and so we know that the _rt fields are the valid ones.
451 abi_ulong sival_ptr;
453 __get_user(info->si_signo, &tinfo->si_signo);
454 __get_user(info->si_errno, &tinfo->si_errno);
455 __get_user(info->si_code, &tinfo->si_code);
456 __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
457 __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
458 __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
459 info->si_value.sival_ptr = (void *)(long)sival_ptr;
462 static int fatal_signal (int sig)
464 switch (sig) {
465 case TARGET_SIGCHLD:
466 case TARGET_SIGURG:
467 case TARGET_SIGWINCH:
468 /* Ignored by default. */
469 return 0;
470 case TARGET_SIGCONT:
471 case TARGET_SIGSTOP:
472 case TARGET_SIGTSTP:
473 case TARGET_SIGTTIN:
474 case TARGET_SIGTTOU:
475 /* Job control signals. */
476 return 0;
477 default:
478 return 1;
482 /* returns 1 if given signal should dump core if not handled */
483 static int core_dump_signal(int sig)
485 switch (sig) {
486 case TARGET_SIGABRT:
487 case TARGET_SIGFPE:
488 case TARGET_SIGILL:
489 case TARGET_SIGQUIT:
490 case TARGET_SIGSEGV:
491 case TARGET_SIGTRAP:
492 case TARGET_SIGBUS:
493 return (1);
494 default:
495 return (0);
499 static void signal_table_init(void)
501 int host_sig, target_sig, count;
504 * Signals are supported starting from TARGET_SIGRTMIN and going up
505 * until we run out of host realtime signals.
506 * glibc at least uses only the lower 2 rt signals and probably
507 * nobody's using the upper ones.
508 * it's why SIGRTMIN (34) is generally greater than __SIGRTMIN (32)
509 * To fix this properly we need to do manual signal delivery multiplexed
510 * over a single host signal.
511 * Attempts for configure "missing" signals via sigaction will be
512 * silently ignored.
514 for (host_sig = SIGRTMIN; host_sig <= SIGRTMAX; host_sig++) {
515 target_sig = host_sig - SIGRTMIN + TARGET_SIGRTMIN;
516 if (target_sig <= TARGET_NSIG) {
517 host_to_target_signal_table[host_sig] = target_sig;
521 /* generate signal conversion tables */
522 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
523 target_to_host_signal_table[target_sig] = _NSIG; /* poison */
525 for (host_sig = 1; host_sig < _NSIG; host_sig++) {
526 if (host_to_target_signal_table[host_sig] == 0) {
527 host_to_target_signal_table[host_sig] = host_sig;
529 target_sig = host_to_target_signal_table[host_sig];
530 if (target_sig <= TARGET_NSIG) {
531 target_to_host_signal_table[target_sig] = host_sig;
535 if (trace_event_get_state_backends(TRACE_SIGNAL_TABLE_INIT)) {
536 for (target_sig = 1, count = 0; target_sig <= TARGET_NSIG; target_sig++) {
537 if (target_to_host_signal_table[target_sig] == _NSIG) {
538 count++;
541 trace_signal_table_init(count);
545 void signal_init(void)
547 TaskState *ts = (TaskState *)thread_cpu->opaque;
548 struct sigaction act;
549 struct sigaction oact;
550 int i;
551 int host_sig;
553 /* initialize signal conversion tables */
554 signal_table_init();
556 /* Set the signal mask from the host mask. */
557 sigprocmask(0, 0, &ts->signal_mask);
559 sigfillset(&act.sa_mask);
560 act.sa_flags = SA_SIGINFO;
561 act.sa_sigaction = host_signal_handler;
562 for(i = 1; i <= TARGET_NSIG; i++) {
563 #ifdef CONFIG_GPROF
564 if (i == TARGET_SIGPROF) {
565 continue;
567 #endif
568 host_sig = target_to_host_signal(i);
569 sigaction(host_sig, NULL, &oact);
570 if (oact.sa_sigaction == (void *)SIG_IGN) {
571 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
572 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
573 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
575 /* If there's already a handler installed then something has
576 gone horribly wrong, so don't even try to handle that case. */
577 /* Install some handlers for our own use. We need at least
578 SIGSEGV and SIGBUS, to detect exceptions. We can not just
579 trap all signals because it affects syscall interrupt
580 behavior. But do trap all default-fatal signals. */
581 if (fatal_signal (i))
582 sigaction(host_sig, &act, NULL);
586 /* Force a synchronously taken signal. The kernel force_sig() function
587 * also forces the signal to "not blocked, not ignored", but for QEMU
588 * that work is done in process_pending_signals().
590 void force_sig(int sig)
592 CPUState *cpu = thread_cpu;
593 CPUArchState *env = cpu->env_ptr;
594 target_siginfo_t info;
596 info.si_signo = sig;
597 info.si_errno = 0;
598 info.si_code = TARGET_SI_KERNEL;
599 info._sifields._kill._pid = 0;
600 info._sifields._kill._uid = 0;
601 queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
604 /* Force a SIGSEGV if we couldn't write to memory trying to set
605 * up the signal frame. oldsig is the signal we were trying to handle
606 * at the point of failure.
608 #if !defined(TARGET_RISCV)
609 void force_sigsegv(int oldsig)
611 if (oldsig == SIGSEGV) {
612 /* Make sure we don't try to deliver the signal again; this will
613 * end up with handle_pending_signal() calling dump_core_and_abort().
615 sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
617 force_sig(TARGET_SIGSEGV);
620 #endif
622 /* abort execution with signal */
623 static void QEMU_NORETURN dump_core_and_abort(int target_sig)
625 CPUState *cpu = thread_cpu;
626 CPUArchState *env = cpu->env_ptr;
627 TaskState *ts = (TaskState *)cpu->opaque;
628 int host_sig, core_dumped = 0;
629 struct sigaction act;
631 host_sig = target_to_host_signal(target_sig);
632 trace_user_force_sig(env, target_sig, host_sig);
633 gdb_signalled(env, target_sig);
635 /* dump core if supported by target binary format */
636 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
637 stop_all_tasks();
638 core_dumped =
639 ((*ts->bprm->core_dump)(target_sig, env) == 0);
641 if (core_dumped) {
642 /* we already dumped the core of target process, we don't want
643 * a coredump of qemu itself */
644 struct rlimit nodump;
645 getrlimit(RLIMIT_CORE, &nodump);
646 nodump.rlim_cur=0;
647 setrlimit(RLIMIT_CORE, &nodump);
648 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
649 target_sig, strsignal(host_sig), "core dumped" );
652 /* The proper exit code for dying from an uncaught signal is
653 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
654 * a negative value. To get the proper exit code we need to
655 * actually die from an uncaught signal. Here the default signal
656 * handler is installed, we send ourself a signal and we wait for
657 * it to arrive. */
658 sigfillset(&act.sa_mask);
659 act.sa_handler = SIG_DFL;
660 act.sa_flags = 0;
661 sigaction(host_sig, &act, NULL);
663 /* For some reason raise(host_sig) doesn't send the signal when
664 * statically linked on x86-64. */
665 kill(getpid(), host_sig);
667 /* Make sure the signal isn't masked (just reuse the mask inside
668 of act) */
669 sigdelset(&act.sa_mask, host_sig);
670 sigsuspend(&act.sa_mask);
672 /* unreachable */
673 abort();
676 /* queue a signal so that it will be send to the virtual CPU as soon
677 as possible */
678 int queue_signal(CPUArchState *env, int sig, int si_type,
679 target_siginfo_t *info)
681 CPUState *cpu = env_cpu(env);
682 TaskState *ts = cpu->opaque;
684 trace_user_queue_signal(env, sig);
686 info->si_code = deposit32(info->si_code, 16, 16, si_type);
688 ts->sync_signal.info = *info;
689 ts->sync_signal.pending = sig;
690 /* signal that a new signal is pending */
691 qatomic_set(&ts->signal_pending, 1);
692 return 1; /* indicates that the signal was queued */
695 #ifndef HAVE_SAFE_SYSCALL
696 static inline void rewind_if_in_safe_syscall(void *puc)
698 /* Default version: never rewind */
700 #endif
702 static void host_signal_handler(int host_signum, siginfo_t *info,
703 void *puc)
705 CPUArchState *env = thread_cpu->env_ptr;
706 CPUState *cpu = env_cpu(env);
707 TaskState *ts = cpu->opaque;
709 int sig;
710 target_siginfo_t tinfo;
711 ucontext_t *uc = puc;
712 struct emulated_sigtable *k;
714 /* the CPU emulator uses some host signals to detect exceptions,
715 we forward to it some signals */
716 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
717 && info->si_code > 0) {
718 if (cpu_signal_handler(host_signum, info, puc))
719 return;
722 /* get target signal number */
723 sig = host_to_target_signal(host_signum);
724 if (sig < 1 || sig > TARGET_NSIG)
725 return;
726 trace_user_host_signal(env, host_signum, sig);
728 rewind_if_in_safe_syscall(puc);
730 host_to_target_siginfo_noswap(&tinfo, info);
731 k = &ts->sigtab[sig - 1];
732 k->info = tinfo;
733 k->pending = sig;
734 ts->signal_pending = 1;
736 /* Block host signals until target signal handler entered. We
737 * can't block SIGSEGV or SIGBUS while we're executing guest
738 * code in case the guest code provokes one in the window between
739 * now and it getting out to the main loop. Signals will be
740 * unblocked again in process_pending_signals().
742 * WARNING: we cannot use sigfillset() here because the uc_sigmask
743 * field is a kernel sigset_t, which is much smaller than the
744 * libc sigset_t which sigfillset() operates on. Using sigfillset()
745 * would write 0xff bytes off the end of the structure and trash
746 * data on the struct.
747 * We can't use sizeof(uc->uc_sigmask) either, because the libc
748 * headers define the struct field with the wrong (too large) type.
750 memset(&uc->uc_sigmask, 0xff, SIGSET_T_SIZE);
751 sigdelset(&uc->uc_sigmask, SIGSEGV);
752 sigdelset(&uc->uc_sigmask, SIGBUS);
754 /* interrupt the virtual CPU as soon as possible */
755 cpu_exit(thread_cpu);
758 /* do_sigaltstack() returns target values and errnos. */
759 /* compare linux/kernel/signal.c:do_sigaltstack() */
760 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
762 int ret;
763 struct target_sigaltstack oss;
764 TaskState *ts = (TaskState *)thread_cpu->opaque;
766 /* XXX: test errors */
767 if(uoss_addr)
769 __put_user(ts->sigaltstack_used.ss_sp, &oss.ss_sp);
770 __put_user(ts->sigaltstack_used.ss_size, &oss.ss_size);
771 __put_user(sas_ss_flags(sp), &oss.ss_flags);
774 if(uss_addr)
776 struct target_sigaltstack *uss;
777 struct target_sigaltstack ss;
778 size_t minstacksize = TARGET_MINSIGSTKSZ;
780 #if defined(TARGET_PPC64)
781 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
782 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
783 if (get_ppc64_abi(image) > 1) {
784 minstacksize = 4096;
786 #endif
788 ret = -TARGET_EFAULT;
789 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
790 goto out;
792 __get_user(ss.ss_sp, &uss->ss_sp);
793 __get_user(ss.ss_size, &uss->ss_size);
794 __get_user(ss.ss_flags, &uss->ss_flags);
795 unlock_user_struct(uss, uss_addr, 0);
797 ret = -TARGET_EPERM;
798 if (on_sig_stack(sp))
799 goto out;
801 ret = -TARGET_EINVAL;
802 if (ss.ss_flags != TARGET_SS_DISABLE
803 && ss.ss_flags != TARGET_SS_ONSTACK
804 && ss.ss_flags != 0)
805 goto out;
807 if (ss.ss_flags == TARGET_SS_DISABLE) {
808 ss.ss_size = 0;
809 ss.ss_sp = 0;
810 } else {
811 ret = -TARGET_ENOMEM;
812 if (ss.ss_size < minstacksize) {
813 goto out;
817 ts->sigaltstack_used.ss_sp = ss.ss_sp;
818 ts->sigaltstack_used.ss_size = ss.ss_size;
821 if (uoss_addr) {
822 ret = -TARGET_EFAULT;
823 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
824 goto out;
827 ret = 0;
828 out:
829 return ret;
832 /* do_sigaction() return target values and host errnos */
833 int do_sigaction(int sig, const struct target_sigaction *act,
834 struct target_sigaction *oact)
836 struct target_sigaction *k;
837 struct sigaction act1;
838 int host_sig;
839 int ret = 0;
841 trace_signal_do_sigaction_guest(sig, TARGET_NSIG);
843 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) {
844 return -TARGET_EINVAL;
847 if (block_signals()) {
848 return -TARGET_ERESTARTSYS;
851 k = &sigact_table[sig - 1];
852 if (oact) {
853 __put_user(k->_sa_handler, &oact->_sa_handler);
854 __put_user(k->sa_flags, &oact->sa_flags);
855 #ifdef TARGET_ARCH_HAS_SA_RESTORER
856 __put_user(k->sa_restorer, &oact->sa_restorer);
857 #endif
858 /* Not swapped. */
859 oact->sa_mask = k->sa_mask;
861 if (act) {
862 /* FIXME: This is not threadsafe. */
863 __get_user(k->_sa_handler, &act->_sa_handler);
864 __get_user(k->sa_flags, &act->sa_flags);
865 #ifdef TARGET_ARCH_HAS_SA_RESTORER
866 __get_user(k->sa_restorer, &act->sa_restorer);
867 #endif
868 /* To be swapped in target_to_host_sigset. */
869 k->sa_mask = act->sa_mask;
871 /* we update the host linux signal state */
872 host_sig = target_to_host_signal(sig);
873 trace_signal_do_sigaction_host(host_sig, TARGET_NSIG);
874 if (host_sig > SIGRTMAX) {
875 /* we don't have enough host signals to map all target signals */
876 qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n",
877 sig);
879 * we don't return an error here because some programs try to
880 * register an handler for all possible rt signals even if they
881 * don't need it.
882 * An error here can abort them whereas there can be no problem
883 * to not have the signal available later.
884 * This is the case for golang,
885 * See https://github.com/golang/go/issues/33746
886 * So we silently ignore the error.
888 return 0;
890 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
891 sigfillset(&act1.sa_mask);
892 act1.sa_flags = SA_SIGINFO;
893 if (k->sa_flags & TARGET_SA_RESTART)
894 act1.sa_flags |= SA_RESTART;
895 /* NOTE: it is important to update the host kernel signal
896 ignore state to avoid getting unexpected interrupted
897 syscalls */
898 if (k->_sa_handler == TARGET_SIG_IGN) {
899 act1.sa_sigaction = (void *)SIG_IGN;
900 } else if (k->_sa_handler == TARGET_SIG_DFL) {
901 if (fatal_signal (sig))
902 act1.sa_sigaction = host_signal_handler;
903 else
904 act1.sa_sigaction = (void *)SIG_DFL;
905 } else {
906 act1.sa_sigaction = host_signal_handler;
908 ret = sigaction(host_sig, &act1, NULL);
911 return ret;
914 static void handle_pending_signal(CPUArchState *cpu_env, int sig,
915 struct emulated_sigtable *k)
917 CPUState *cpu = env_cpu(cpu_env);
918 abi_ulong handler;
919 sigset_t set;
920 target_sigset_t target_old_set;
921 struct target_sigaction *sa;
922 TaskState *ts = cpu->opaque;
924 trace_user_handle_signal(cpu_env, sig);
925 /* dequeue signal */
926 k->pending = 0;
928 sig = gdb_handlesig(cpu, sig);
929 if (!sig) {
930 sa = NULL;
931 handler = TARGET_SIG_IGN;
932 } else {
933 sa = &sigact_table[sig - 1];
934 handler = sa->_sa_handler;
937 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
938 print_taken_signal(sig, &k->info);
941 if (handler == TARGET_SIG_DFL) {
942 /* default handler : ignore some signal. The other are job control or fatal */
943 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
944 kill(getpid(),SIGSTOP);
945 } else if (sig != TARGET_SIGCHLD &&
946 sig != TARGET_SIGURG &&
947 sig != TARGET_SIGWINCH &&
948 sig != TARGET_SIGCONT) {
949 dump_core_and_abort(sig);
951 } else if (handler == TARGET_SIG_IGN) {
952 /* ignore sig */
953 } else if (handler == TARGET_SIG_ERR) {
954 dump_core_and_abort(sig);
955 } else {
956 /* compute the blocked signals during the handler execution */
957 sigset_t *blocked_set;
959 target_to_host_sigset(&set, &sa->sa_mask);
960 /* SA_NODEFER indicates that the current signal should not be
961 blocked during the handler */
962 if (!(sa->sa_flags & TARGET_SA_NODEFER))
963 sigaddset(&set, target_to_host_signal(sig));
965 /* save the previous blocked signal state to restore it at the
966 end of the signal execution (see do_sigreturn) */
967 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
969 /* block signals in the handler */
970 blocked_set = ts->in_sigsuspend ?
971 &ts->sigsuspend_mask : &ts->signal_mask;
972 sigorset(&ts->signal_mask, blocked_set, &set);
973 ts->in_sigsuspend = 0;
975 /* if the CPU is in VM86 mode, we restore the 32 bit values */
976 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
978 CPUX86State *env = cpu_env;
979 if (env->eflags & VM_MASK)
980 save_v86_state(env);
982 #endif
983 /* prepare the stack frame of the virtual CPU */
984 #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
985 if (sa->sa_flags & TARGET_SA_SIGINFO) {
986 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
987 } else {
988 setup_frame(sig, sa, &target_old_set, cpu_env);
990 #else
991 /* These targets do not have traditional signals. */
992 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
993 #endif
994 if (sa->sa_flags & TARGET_SA_RESETHAND) {
995 sa->_sa_handler = TARGET_SIG_DFL;
1000 void process_pending_signals(CPUArchState *cpu_env)
1002 CPUState *cpu = env_cpu(cpu_env);
1003 int sig;
1004 TaskState *ts = cpu->opaque;
1005 sigset_t set;
1006 sigset_t *blocked_set;
1008 while (qatomic_read(&ts->signal_pending)) {
1009 /* FIXME: This is not threadsafe. */
1010 sigfillset(&set);
1011 sigprocmask(SIG_SETMASK, &set, 0);
1013 restart_scan:
1014 sig = ts->sync_signal.pending;
1015 if (sig) {
1016 /* Synchronous signals are forced,
1017 * see force_sig_info() and callers in Linux
1018 * Note that not all of our queue_signal() calls in QEMU correspond
1019 * to force_sig_info() calls in Linux (some are send_sig_info()).
1020 * However it seems like a kernel bug to me to allow the process
1021 * to block a synchronous signal since it could then just end up
1022 * looping round and round indefinitely.
1024 if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
1025 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
1026 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
1027 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
1030 handle_pending_signal(cpu_env, sig, &ts->sync_signal);
1033 for (sig = 1; sig <= TARGET_NSIG; sig++) {
1034 blocked_set = ts->in_sigsuspend ?
1035 &ts->sigsuspend_mask : &ts->signal_mask;
1037 if (ts->sigtab[sig - 1].pending &&
1038 (!sigismember(blocked_set,
1039 target_to_host_signal_table[sig]))) {
1040 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
1041 /* Restart scan from the beginning, as handle_pending_signal
1042 * might have resulted in a new synchronous signal (eg SIGSEGV).
1044 goto restart_scan;
1048 /* if no signal is pending, unblock signals and recheck (the act
1049 * of unblocking might cause us to take another host signal which
1050 * will set signal_pending again).
1052 qatomic_set(&ts->signal_pending, 0);
1053 ts->in_sigsuspend = 0;
1054 set = ts->signal_mask;
1055 sigdelset(&set, SIGSEGV);
1056 sigdelset(&set, SIGBUS);
1057 sigprocmask(SIG_SETMASK, &set, 0);
1059 ts->in_sigsuspend = 0;