]> git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/signal.c
5c94bd02e38b9eac31e5fa7ae65cd2d31de43074
[mirror_qemu.git] / bsd-user / signal.c
1 /*
2 * Emulation of BSD signals
3 *
4 * Copyright (c) 2003 - 2008 Fabrice Bellard
5 * Copyright (c) 2013 Stacey Son
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "qemu/osdep.h"
22 #include "qemu.h"
23 #include "signal-common.h"
24 #include "trace.h"
25 #include "hw/core/tcg-cpu-ops.h"
26 #include "host-signal.h"
27
28 /*
29 * Stubbed out routines until we merge signal support from bsd-user
30 * fork.
31 */
32
33 static struct target_sigaction sigact_table[TARGET_NSIG];
34 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc);
35 static void target_to_host_sigset_internal(sigset_t *d,
36 const target_sigset_t *s);
37
38 static inline int on_sig_stack(TaskState *ts, unsigned long sp)
39 {
40 return sp - ts->sigaltstack_used.ss_sp < ts->sigaltstack_used.ss_size;
41 }
42
43 static inline int sas_ss_flags(TaskState *ts, unsigned long sp)
44 {
45 return ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE :
46 on_sig_stack(ts, sp) ? SS_ONSTACK : 0;
47 }
48
49 /*
50 * The BSD ABIs use the same singal numbers across all the CPU architectures, so
51 * (unlike Linux) these functions are just the identity mapping. This might not
52 * be true for XyzBSD running on AbcBSD, which doesn't currently work.
53 */
54 int host_to_target_signal(int sig)
55 {
56 return sig;
57 }
58
59 int target_to_host_signal(int sig)
60 {
61 return sig;
62 }
63
64 static inline void target_sigemptyset(target_sigset_t *set)
65 {
66 memset(set, 0, sizeof(*set));
67 }
68
69 static inline void target_sigaddset(target_sigset_t *set, int signum)
70 {
71 signum--;
72 uint32_t mask = (uint32_t)1 << (signum % TARGET_NSIG_BPW);
73 set->__bits[signum / TARGET_NSIG_BPW] |= mask;
74 }
75
76 static inline int target_sigismember(const target_sigset_t *set, int signum)
77 {
78 signum--;
79 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
80 return (set->__bits[signum / TARGET_NSIG_BPW] & mask) != 0;
81 }
82
83 /* Adjust the signal context to rewind out of safe-syscall if we're in it */
84 static inline void rewind_if_in_safe_syscall(void *puc)
85 {
86 ucontext_t *uc = (ucontext_t *)puc;
87 uintptr_t pcreg = host_signal_pc(uc);
88
89 if (pcreg > (uintptr_t)safe_syscall_start
90 && pcreg < (uintptr_t)safe_syscall_end) {
91 host_signal_set_pc(uc, (uintptr_t)safe_syscall_start);
92 }
93 }
94
95 /*
96 * Note: The following take advantage of the BSD signal property that all
97 * signals are available on all architectures.
98 */
99 static void host_to_target_sigset_internal(target_sigset_t *d,
100 const sigset_t *s)
101 {
102 int i;
103
104 target_sigemptyset(d);
105 for (i = 1; i <= NSIG; i++) {
106 if (sigismember(s, i)) {
107 target_sigaddset(d, host_to_target_signal(i));
108 }
109 }
110 }
111
112 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
113 {
114 target_sigset_t d1;
115 int i;
116
117 host_to_target_sigset_internal(&d1, s);
118 for (i = 0; i < _SIG_WORDS; i++) {
119 d->__bits[i] = tswap32(d1.__bits[i]);
120 }
121 }
122
123 static void target_to_host_sigset_internal(sigset_t *d,
124 const target_sigset_t *s)
125 {
126 int i;
127
128 sigemptyset(d);
129 for (i = 1; i <= TARGET_NSIG; i++) {
130 if (target_sigismember(s, i)) {
131 sigaddset(d, target_to_host_signal(i));
132 }
133 }
134 }
135
136 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
137 {
138 target_sigset_t s1;
139 int i;
140
141 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
142 s1.__bits[i] = tswap32(s->__bits[i]);
143 }
144 target_to_host_sigset_internal(d, &s1);
145 }
146
147 static bool has_trapno(int tsig)
148 {
149 return tsig == TARGET_SIGILL ||
150 tsig == TARGET_SIGFPE ||
151 tsig == TARGET_SIGSEGV ||
152 tsig == TARGET_SIGBUS ||
153 tsig == TARGET_SIGTRAP;
154 }
155
156 /* Siginfo conversion. */
157
158 /*
159 * Populate tinfo w/o swapping based on guessing which fields are valid.
160 */
161 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
162 const siginfo_t *info)
163 {
164 int sig = host_to_target_signal(info->si_signo);
165 int si_code = info->si_code;
166 int si_type;
167
168 /*
169 * Make sure we that the variable portion of the target siginfo is zeroed
170 * out so we don't leak anything into that.
171 */
172 memset(&tinfo->_reason, 0, sizeof(tinfo->_reason));
173
174 /*
175 * This is awkward, because we have to use a combination of the si_code and
176 * si_signo to figure out which of the union's members are valid.o We
177 * therefore make our best guess.
178 *
179 * Once we have made our guess, we record it in the top 16 bits of
180 * the si_code, so that tswap_siginfo() later can use it.
181 * tswap_siginfo() will strip these top bits out before writing
182 * si_code to the guest (sign-extending the lower bits).
183 */
184 tinfo->si_signo = sig;
185 tinfo->si_errno = info->si_errno;
186 tinfo->si_code = info->si_code;
187 tinfo->si_pid = info->si_pid;
188 tinfo->si_uid = info->si_uid;
189 tinfo->si_status = info->si_status;
190 tinfo->si_addr = (abi_ulong)(unsigned long)info->si_addr;
191 /*
192 * si_value is opaque to kernel. On all FreeBSD platforms,
193 * sizeof(sival_ptr) >= sizeof(sival_int) so the following
194 * always will copy the larger element.
195 */
196 tinfo->si_value.sival_ptr =
197 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
198
199 switch (si_code) {
200 /*
201 * All the SI_xxx codes that are defined here are global to
202 * all the signals (they have values that none of the other,
203 * more specific signal info will set).
204 */
205 case SI_USER:
206 case SI_LWP:
207 case SI_KERNEL:
208 case SI_QUEUE:
209 case SI_ASYNCIO:
210 /*
211 * Only the fixed parts are valid (though FreeBSD doesn't always
212 * set all the fields to non-zero values.
213 */
214 si_type = QEMU_SI_NOINFO;
215 break;
216 case SI_TIMER:
217 tinfo->_reason._timer._timerid = info->_reason._timer._timerid;
218 tinfo->_reason._timer._overrun = info->_reason._timer._overrun;
219 si_type = QEMU_SI_TIMER;
220 break;
221 case SI_MESGQ:
222 tinfo->_reason._mesgq._mqd = info->_reason._mesgq._mqd;
223 si_type = QEMU_SI_MESGQ;
224 break;
225 default:
226 /*
227 * We have to go based on the signal number now to figure out
228 * what's valid.
229 */
230 if (has_trapno(sig)) {
231 tinfo->_reason._fault._trapno = info->_reason._fault._trapno;
232 si_type = QEMU_SI_FAULT;
233 }
234 #ifdef TARGET_SIGPOLL
235 /*
236 * FreeBSD never had SIGPOLL, but emulates it for Linux so there's
237 * a chance it may popup in the future.
238 */
239 if (sig == TARGET_SIGPOLL) {
240 tinfo->_reason._poll._band = info->_reason._poll._band;
241 si_type = QEMU_SI_POLL;
242 }
243 #endif
244 /*
245 * Unsure that this can actually be generated, and our support for
246 * capsicum is somewhere between weak and non-existant, but if we get
247 * one, then we know what to save.
248 */
249 if (sig == TARGET_SIGTRAP) {
250 tinfo->_reason._capsicum._syscall =
251 info->_reason._capsicum._syscall;
252 si_type = QEMU_SI_CAPSICUM;
253 }
254 break;
255 }
256 tinfo->si_code = deposit32(si_code, 24, 8, si_type);
257 }
258
259 static void tswap_siginfo(target_siginfo_t *tinfo, const target_siginfo_t *info)
260 {
261 int si_type = extract32(info->si_code, 24, 8);
262 int si_code = sextract32(info->si_code, 0, 24);
263
264 __put_user(info->si_signo, &tinfo->si_signo);
265 __put_user(info->si_errno, &tinfo->si_errno);
266 __put_user(si_code, &tinfo->si_code); /* Zero out si_type, it's internal */
267 __put_user(info->si_pid, &tinfo->si_pid);
268 __put_user(info->si_uid, &tinfo->si_uid);
269 __put_user(info->si_status, &tinfo->si_status);
270 __put_user(info->si_addr, &tinfo->si_addr);
271 /*
272 * Unswapped, because we passed it through mostly untouched. si_value is
273 * opaque to the kernel, so we didn't bother with potentially wasting cycles
274 * to swap it into host byte order.
275 */
276 tinfo->si_value.sival_ptr = info->si_value.sival_ptr;
277
278 /*
279 * We can use our internal marker of which fields in the structure
280 * are valid, rather than duplicating the guesswork of
281 * host_to_target_siginfo_noswap() here.
282 */
283 switch (si_type) {
284 case QEMU_SI_NOINFO: /* No additional info */
285 break;
286 case QEMU_SI_FAULT:
287 __put_user(info->_reason._fault._trapno,
288 &tinfo->_reason._fault._trapno);
289 break;
290 case QEMU_SI_TIMER:
291 __put_user(info->_reason._timer._timerid,
292 &tinfo->_reason._timer._timerid);
293 __put_user(info->_reason._timer._overrun,
294 &tinfo->_reason._timer._overrun);
295 break;
296 case QEMU_SI_MESGQ:
297 __put_user(info->_reason._mesgq._mqd, &tinfo->_reason._mesgq._mqd);
298 break;
299 case QEMU_SI_POLL:
300 /* Note: Not generated on FreeBSD */
301 __put_user(info->_reason._poll._band, &tinfo->_reason._poll._band);
302 break;
303 case QEMU_SI_CAPSICUM:
304 __put_user(info->_reason._capsicum._syscall,
305 &tinfo->_reason._capsicum._syscall);
306 break;
307 default:
308 g_assert_not_reached();
309 }
310 }
311
312 int block_signals(void)
313 {
314 TaskState *ts = (TaskState *)thread_cpu->opaque;
315 sigset_t set;
316
317 /*
318 * It's OK to block everything including SIGSEGV, because we won't run any
319 * further guest code before unblocking signals in
320 * process_pending_signals(). We depend on the FreeBSD behaivor here where
321 * this will only affect this thread's signal mask. We don't use
322 * pthread_sigmask which might seem more correct because that routine also
323 * does odd things with SIGCANCEL to implement pthread_cancel().
324 */
325 sigfillset(&set);
326 sigprocmask(SIG_SETMASK, &set, 0);
327
328 return qatomic_xchg(&ts->signal_pending, 1);
329 }
330
331 /* Returns 1 if given signal should dump core if not handled. */
332 static int core_dump_signal(int sig)
333 {
334 switch (sig) {
335 case TARGET_SIGABRT:
336 case TARGET_SIGFPE:
337 case TARGET_SIGILL:
338 case TARGET_SIGQUIT:
339 case TARGET_SIGSEGV:
340 case TARGET_SIGTRAP:
341 case TARGET_SIGBUS:
342 return 1;
343 default:
344 return 0;
345 }
346 }
347
348 /* Abort execution with signal. */
349 static void QEMU_NORETURN dump_core_and_abort(int target_sig)
350 {
351 CPUArchState *env = thread_cpu->env_ptr;
352 CPUState *cpu = env_cpu(env);
353 TaskState *ts = cpu->opaque;
354 int core_dumped = 0;
355 int host_sig;
356 struct sigaction act;
357
358 host_sig = target_to_host_signal(target_sig);
359 gdb_signalled(env, target_sig);
360
361 /* Dump core if supported by target binary format */
362 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
363 stop_all_tasks();
364 core_dumped =
365 ((*ts->bprm->core_dump)(target_sig, env) == 0);
366 }
367 if (core_dumped) {
368 struct rlimit nodump;
369
370 /*
371 * We already dumped the core of target process, we don't want
372 * a coredump of qemu itself.
373 */
374 getrlimit(RLIMIT_CORE, &nodump);
375 nodump.rlim_cur = 0;
376 setrlimit(RLIMIT_CORE, &nodump);
377 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) "
378 "- %s\n", target_sig, strsignal(host_sig), "core dumped");
379 }
380
381 /*
382 * The proper exit code for dying from an uncaught signal is
383 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
384 * a negative value. To get the proper exit code we need to
385 * actually die from an uncaught signal. Here the default signal
386 * handler is installed, we send ourself a signal and we wait for
387 * it to arrive.
388 */
389 memset(&act, 0, sizeof(act));
390 sigfillset(&act.sa_mask);
391 act.sa_handler = SIG_DFL;
392 sigaction(host_sig, &act, NULL);
393
394 kill(getpid(), host_sig);
395
396 /*
397 * Make sure the signal isn't masked (just reuse the mask inside
398 * of act).
399 */
400 sigdelset(&act.sa_mask, host_sig);
401 sigsuspend(&act.sa_mask);
402
403 /* unreachable */
404 abort();
405 }
406
407 /*
408 * Queue a signal so that it will be send to the virtual CPU as soon as
409 * possible.
410 */
411 void queue_signal(CPUArchState *env, int sig, int si_type,
412 target_siginfo_t *info)
413 {
414 CPUState *cpu = env_cpu(env);
415 TaskState *ts = cpu->opaque;
416
417 trace_user_queue_signal(env, sig);
418
419 info->si_code = deposit32(info->si_code, 24, 8, si_type);
420
421 ts->sync_signal.info = *info;
422 ts->sync_signal.pending = sig;
423 /* Signal that a new signal is pending. */
424 qatomic_set(&ts->signal_pending, 1);
425 return;
426 }
427
428 static int fatal_signal(int sig)
429 {
430
431 switch (sig) {
432 case TARGET_SIGCHLD:
433 case TARGET_SIGURG:
434 case TARGET_SIGWINCH:
435 case TARGET_SIGINFO:
436 /* Ignored by default. */
437 return 0;
438 case TARGET_SIGCONT:
439 case TARGET_SIGSTOP:
440 case TARGET_SIGTSTP:
441 case TARGET_SIGTTIN:
442 case TARGET_SIGTTOU:
443 /* Job control signals. */
444 return 0;
445 default:
446 return 1;
447 }
448 }
449
450 /*
451 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
452 * 'force' part is handled in process_pending_signals().
453 */
454 void force_sig_fault(int sig, int code, abi_ulong addr)
455 {
456 CPUState *cpu = thread_cpu;
457 CPUArchState *env = cpu->env_ptr;
458 target_siginfo_t info = {};
459
460 info.si_signo = sig;
461 info.si_errno = 0;
462 info.si_code = code;
463 info.si_addr = addr;
464 queue_signal(env, sig, QEMU_SI_FAULT, &info);
465 }
466
467 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
468 {
469 CPUArchState *env = thread_cpu->env_ptr;
470 CPUState *cpu = env_cpu(env);
471 TaskState *ts = cpu->opaque;
472 target_siginfo_t tinfo;
473 ucontext_t *uc = puc;
474 struct emulated_sigtable *k;
475 int guest_sig;
476 uintptr_t pc = 0;
477 bool sync_sig = false;
478
479 /*
480 * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
481 * handling wrt signal blocking and unwinding.
482 */
483 if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) {
484 MMUAccessType access_type;
485 uintptr_t host_addr;
486 abi_ptr guest_addr;
487 bool is_write;
488
489 host_addr = (uintptr_t)info->si_addr;
490
491 /*
492 * Convert forcefully to guest address space: addresses outside
493 * reserved_va are still valid to report via SEGV_MAPERR.
494 */
495 guest_addr = h2g_nocheck(host_addr);
496
497 pc = host_signal_pc(uc);
498 is_write = host_signal_write(info, uc);
499 access_type = adjust_signal_pc(&pc, is_write);
500
501 if (host_sig == SIGSEGV) {
502 bool maperr = true;
503
504 if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) {
505 /* If this was a write to a TB protected page, restart. */
506 if (is_write &&
507 handle_sigsegv_accerr_write(cpu, &uc->uc_sigmask,
508 pc, guest_addr)) {
509 return;
510 }
511
512 /*
513 * With reserved_va, the whole address space is PROT_NONE,
514 * which means that we may get ACCERR when we want MAPERR.
515 */
516 if (page_get_flags(guest_addr) & PAGE_VALID) {
517 maperr = false;
518 } else {
519 info->si_code = SEGV_MAPERR;
520 }
521 }
522
523 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
524 cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc);
525 } else {
526 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
527 if (info->si_code == BUS_ADRALN) {
528 cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc);
529 }
530 }
531
532 sync_sig = true;
533 }
534
535 /* Get the target signal number. */
536 guest_sig = host_to_target_signal(host_sig);
537 if (guest_sig < 1 || guest_sig > TARGET_NSIG) {
538 return;
539 }
540 trace_user_host_signal(cpu, host_sig, guest_sig);
541
542 host_to_target_siginfo_noswap(&tinfo, info);
543
544 k = &ts->sigtab[guest_sig - 1];
545 k->info = tinfo;
546 k->pending = guest_sig;
547 ts->signal_pending = 1;
548
549 /*
550 * For synchronous signals, unwind the cpu state to the faulting
551 * insn and then exit back to the main loop so that the signal
552 * is delivered immediately.
553 */
554 if (sync_sig) {
555 cpu->exception_index = EXCP_INTERRUPT;
556 cpu_loop_exit_restore(cpu, pc);
557 }
558
559 rewind_if_in_safe_syscall(puc);
560
561 /*
562 * Block host signals until target signal handler entered. We
563 * can't block SIGSEGV or SIGBUS while we're executing guest
564 * code in case the guest code provokes one in the window between
565 * now and it getting out to the main loop. Signals will be
566 * unblocked again in process_pending_signals().
567 */
568 sigfillset(&uc->uc_sigmask);
569 sigdelset(&uc->uc_sigmask, SIGSEGV);
570 sigdelset(&uc->uc_sigmask, SIGBUS);
571
572 /* Interrupt the virtual CPU as soon as possible. */
573 cpu_exit(thread_cpu);
574 }
575
576 /* do_sigaction() return host values and errnos */
577 int do_sigaction(int sig, const struct target_sigaction *act,
578 struct target_sigaction *oact)
579 {
580 struct target_sigaction *k;
581 struct sigaction act1;
582 int host_sig;
583 int ret = 0;
584
585 if (sig < 1 || sig > TARGET_NSIG) {
586 return -TARGET_EINVAL;
587 }
588
589 if ((sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) &&
590 act != NULL && act->_sa_handler != TARGET_SIG_DFL) {
591 return -TARGET_EINVAL;
592 }
593
594 if (block_signals()) {
595 return -TARGET_ERESTART;
596 }
597
598 k = &sigact_table[sig - 1];
599 if (oact) {
600 oact->_sa_handler = tswapal(k->_sa_handler);
601 oact->sa_flags = tswap32(k->sa_flags);
602 oact->sa_mask = k->sa_mask;
603 }
604 if (act) {
605 k->_sa_handler = tswapal(act->_sa_handler);
606 k->sa_flags = tswap32(act->sa_flags);
607 k->sa_mask = act->sa_mask;
608
609 /* Update the host signal state. */
610 host_sig = target_to_host_signal(sig);
611 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
612 memset(&act1, 0, sizeof(struct sigaction));
613 sigfillset(&act1.sa_mask);
614 act1.sa_flags = SA_SIGINFO;
615 if (k->sa_flags & TARGET_SA_RESTART) {
616 act1.sa_flags |= SA_RESTART;
617 }
618 /*
619 * Note: It is important to update the host kernel signal mask to
620 * avoid getting unexpected interrupted system calls.
621 */
622 if (k->_sa_handler == TARGET_SIG_IGN) {
623 act1.sa_sigaction = (void *)SIG_IGN;
624 } else if (k->_sa_handler == TARGET_SIG_DFL) {
625 if (fatal_signal(sig)) {
626 act1.sa_sigaction = host_signal_handler;
627 } else {
628 act1.sa_sigaction = (void *)SIG_DFL;
629 }
630 } else {
631 act1.sa_sigaction = host_signal_handler;
632 }
633 ret = sigaction(host_sig, &act1, NULL);
634 }
635 }
636 return ret;
637 }
638
639 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
640 CPUArchState *env, size_t frame_size)
641 {
642 TaskState *ts = (TaskState *)thread_cpu->opaque;
643 abi_ulong sp;
644
645 /* Use default user stack */
646 sp = get_sp_from_cpustate(env);
647
648 if ((ka->sa_flags & TARGET_SA_ONSTACK) && sas_ss_flags(ts, sp) == 0) {
649 sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
650 }
651
652 /* TODO: make this a target_arch function / define */
653 #if defined(TARGET_ARM)
654 return (sp - frame_size) & ~7;
655 #elif defined(TARGET_AARCH64)
656 return (sp - frame_size) & ~15;
657 #else
658 return sp - frame_size;
659 #endif
660 }
661
662 /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
663
664 static void setup_frame(int sig, int code, struct target_sigaction *ka,
665 target_sigset_t *set, target_siginfo_t *tinfo, CPUArchState *env)
666 {
667 struct target_sigframe *frame;
668 abi_ulong frame_addr;
669 int i;
670
671 frame_addr = get_sigframe(ka, env, sizeof(*frame));
672 trace_user_setup_frame(env, frame_addr);
673 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
674 unlock_user_struct(frame, frame_addr, 1);
675 dump_core_and_abort(TARGET_SIGILL);
676 return;
677 }
678
679 memset(frame, 0, sizeof(*frame));
680 setup_sigframe_arch(env, frame_addr, frame, 0);
681
682 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
683 __put_user(set->__bits[i], &frame->sf_uc.uc_sigmask.__bits[i]);
684 }
685
686 if (tinfo) {
687 frame->sf_si.si_signo = tinfo->si_signo;
688 frame->sf_si.si_errno = tinfo->si_errno;
689 frame->sf_si.si_code = tinfo->si_code;
690 frame->sf_si.si_pid = tinfo->si_pid;
691 frame->sf_si.si_uid = tinfo->si_uid;
692 frame->sf_si.si_status = tinfo->si_status;
693 frame->sf_si.si_addr = tinfo->si_addr;
694 /* see host_to_target_siginfo_noswap() for more details */
695 frame->sf_si.si_value.sival_ptr = tinfo->si_value.sival_ptr;
696 /*
697 * At this point, whatever is in the _reason union is complete
698 * and in target order, so just copy the whole thing over, even
699 * if it's too large for this specific signal.
700 * host_to_target_siginfo_noswap() and tswap_siginfo() have ensured
701 * that's so.
702 */
703 memcpy(&frame->sf_si._reason, &tinfo->_reason,
704 sizeof(tinfo->_reason));
705 }
706
707 set_sigtramp_args(env, sig, frame, frame_addr, ka);
708
709 unlock_user_struct(frame, frame_addr, 1);
710 }
711
712 static int reset_signal_mask(target_ucontext_t *ucontext)
713 {
714 int i;
715 sigset_t blocked;
716 target_sigset_t target_set;
717 TaskState *ts = (TaskState *)thread_cpu->opaque;
718
719 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
720 if (__get_user(target_set.__bits[i],
721 &ucontext->uc_sigmask.__bits[i])) {
722 return -TARGET_EFAULT;
723 }
724 }
725 target_to_host_sigset_internal(&blocked, &target_set);
726 ts->signal_mask = blocked;
727
728 return 0;
729 }
730
731 /* See sys/$M/$M/exec_machdep.c sigreturn() */
732 long do_sigreturn(CPUArchState *env, abi_ulong addr)
733 {
734 long ret;
735 abi_ulong target_ucontext;
736 target_ucontext_t *ucontext = NULL;
737
738 /* Get the target ucontext address from the stack frame */
739 ret = get_ucontext_sigreturn(env, addr, &target_ucontext);
740 if (is_error(ret)) {
741 return ret;
742 }
743 trace_user_do_sigreturn(env, addr);
744 if (!lock_user_struct(VERIFY_READ, ucontext, target_ucontext, 0)) {
745 goto badframe;
746 }
747
748 /* Set the register state back to before the signal. */
749 if (set_mcontext(env, &ucontext->uc_mcontext, 1)) {
750 goto badframe;
751 }
752
753 /* And reset the signal mask. */
754 if (reset_signal_mask(ucontext)) {
755 goto badframe;
756 }
757
758 unlock_user_struct(ucontext, target_ucontext, 0);
759 return -TARGET_EJUSTRETURN;
760
761 badframe:
762 if (ucontext != NULL) {
763 unlock_user_struct(ucontext, target_ucontext, 0);
764 }
765 return -TARGET_EFAULT;
766 }
767
768 void signal_init(void)
769 {
770 TaskState *ts = (TaskState *)thread_cpu->opaque;
771 struct sigaction act;
772 struct sigaction oact;
773 int i;
774 int host_sig;
775
776 /* Set the signal mask from the host mask. */
777 sigprocmask(0, 0, &ts->signal_mask);
778
779 sigfillset(&act.sa_mask);
780 act.sa_sigaction = host_signal_handler;
781 act.sa_flags = SA_SIGINFO;
782
783 for (i = 1; i <= TARGET_NSIG; i++) {
784 #ifdef CONFIG_GPROF
785 if (i == TARGET_SIGPROF) {
786 continue;
787 }
788 #endif
789 host_sig = target_to_host_signal(i);
790 sigaction(host_sig, NULL, &oact);
791 if (oact.sa_sigaction == (void *)SIG_IGN) {
792 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
793 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
794 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
795 }
796 /*
797 * If there's already a handler installed then something has
798 * gone horribly wrong, so don't even try to handle that case.
799 * Install some handlers for our own use. We need at least
800 * SIGSEGV and SIGBUS, to detect exceptions. We can not just
801 * trap all signals because it affects syscall interrupt
802 * behavior. But do trap all default-fatal signals.
803 */
804 if (fatal_signal(i)) {
805 sigaction(host_sig, &act, NULL);
806 }
807 }
808 }
809
810 static void handle_pending_signal(CPUArchState *env, int sig,
811 struct emulated_sigtable *k)
812 {
813 CPUState *cpu = env_cpu(env);
814 TaskState *ts = cpu->opaque;
815 struct target_sigaction *sa;
816 int code;
817 sigset_t set;
818 abi_ulong handler;
819 target_siginfo_t tinfo;
820 target_sigset_t target_old_set;
821
822 trace_user_handle_signal(env, sig);
823
824 k->pending = 0;
825
826 sig = gdb_handlesig(cpu, sig);
827 if (!sig) {
828 sa = NULL;
829 handler = TARGET_SIG_IGN;
830 } else {
831 sa = &sigact_table[sig - 1];
832 handler = sa->_sa_handler;
833 }
834
835 if (do_strace) {
836 print_taken_signal(sig, &k->info);
837 }
838
839 if (handler == TARGET_SIG_DFL) {
840 /*
841 * default handler : ignore some signal. The other are job
842 * control or fatal.
843 */
844 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN ||
845 sig == TARGET_SIGTTOU) {
846 kill(getpid(), SIGSTOP);
847 } else if (sig != TARGET_SIGCHLD && sig != TARGET_SIGURG &&
848 sig != TARGET_SIGINFO && sig != TARGET_SIGWINCH &&
849 sig != TARGET_SIGCONT) {
850 dump_core_and_abort(sig);
851 }
852 } else if (handler == TARGET_SIG_IGN) {
853 /* ignore sig */
854 } else if (handler == TARGET_SIG_ERR) {
855 dump_core_and_abort(sig);
856 } else {
857 /* compute the blocked signals during the handler execution */
858 sigset_t *blocked_set;
859
860 target_to_host_sigset(&set, &sa->sa_mask);
861 /*
862 * SA_NODEFER indicates that the current signal should not be
863 * blocked during the handler.
864 */
865 if (!(sa->sa_flags & TARGET_SA_NODEFER)) {
866 sigaddset(&set, target_to_host_signal(sig));
867 }
868
869 /*
870 * Save the previous blocked signal state to restore it at the
871 * end of the signal execution (see do_sigreturn).
872 */
873 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
874
875 blocked_set = ts->in_sigsuspend ?
876 &ts->sigsuspend_mask : &ts->signal_mask;
877 sigorset(&ts->signal_mask, blocked_set, &set);
878 ts->in_sigsuspend = false;
879 sigprocmask(SIG_SETMASK, &ts->signal_mask, NULL);
880
881 /* XXX VM86 on x86 ??? */
882
883 code = k->info.si_code; /* From host, so no si_type */
884 /* prepare the stack frame of the virtual CPU */
885 if (sa->sa_flags & TARGET_SA_SIGINFO) {
886 tswap_siginfo(&tinfo, &k->info);
887 setup_frame(sig, code, sa, &target_old_set, &tinfo, env);
888 } else {
889 setup_frame(sig, code, sa, &target_old_set, NULL, env);
890 }
891 if (sa->sa_flags & TARGET_SA_RESETHAND) {
892 sa->_sa_handler = TARGET_SIG_DFL;
893 }
894 }
895 }
896
897 void process_pending_signals(CPUArchState *env)
898 {
899 CPUState *cpu = env_cpu(env);
900 int sig;
901 sigset_t *blocked_set, set;
902 struct emulated_sigtable *k;
903 TaskState *ts = cpu->opaque;
904
905 while (qatomic_read(&ts->signal_pending)) {
906 sigfillset(&set);
907 sigprocmask(SIG_SETMASK, &set, 0);
908
909 restart_scan:
910 sig = ts->sync_signal.pending;
911 if (sig) {
912 /*
913 * Synchronous signals are forced by the emulated CPU in some way.
914 * If they are set to ignore, restore the default handler (see
915 * sys/kern_sig.c trapsignal() and execsigs() for this behavior)
916 * though maybe this is done only when forcing exit for non SIGCHLD.
917 */
918 if (sigismember(&ts->signal_mask, target_to_host_signal(sig)) ||
919 sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
920 sigdelset(&ts->signal_mask, target_to_host_signal(sig));
921 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
922 }
923 handle_pending_signal(env, sig, &ts->sync_signal);
924 }
925
926 k = ts->sigtab;
927 for (sig = 1; sig <= TARGET_NSIG; sig++, k++) {
928 blocked_set = ts->in_sigsuspend ?
929 &ts->sigsuspend_mask : &ts->signal_mask;
930 if (k->pending &&
931 !sigismember(blocked_set, target_to_host_signal(sig))) {
932 handle_pending_signal(env, sig, k);
933 /*
934 * Restart scan from the beginning, as handle_pending_signal
935 * might have resulted in a new synchronous signal (eg SIGSEGV).
936 */
937 goto restart_scan;
938 }
939 }
940
941 /*
942 * Unblock signals and check one more time. Unblocking signals may cause
943 * us to take another host signal, which will set signal_pending again.
944 */
945 qatomic_set(&ts->signal_pending, 0);
946 ts->in_sigsuspend = false;
947 set = ts->signal_mask;
948 sigdelset(&set, SIGSEGV);
949 sigdelset(&set, SIGBUS);
950 sigprocmask(SIG_SETMASK, &set, 0);
951 }
952 ts->in_sigsuspend = false;
953 }
954
955 void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
956 MMUAccessType access_type, bool maperr, uintptr_t ra)
957 {
958 const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
959
960 if (tcg_ops->record_sigsegv) {
961 tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
962 }
963
964 force_sig_fault(TARGET_SIGSEGV,
965 maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
966 addr);
967 cpu->exception_index = EXCP_INTERRUPT;
968 cpu_loop_exit_restore(cpu, ra);
969 }
970
971 void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
972 MMUAccessType access_type, uintptr_t ra)
973 {
974 const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
975
976 if (tcg_ops->record_sigbus) {
977 tcg_ops->record_sigbus(cpu, addr, access_type, ra);
978 }
979
980 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
981 cpu->exception_index = EXCP_INTERRUPT;
982 cpu_loop_exit_restore(cpu, ra);
983 }