xref: /qemu/bsd-user/signal.c (revision 85fc1b5dbf893254471809eef8ec773bb29d4f48)
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 "hw/core/tcg-cpu-ops.h"
25 #include "host-signal.h"
26 
27 /*
28  * Stubbed out routines until we merge signal support from bsd-user
29  * fork.
30  */
31 
32 static struct target_sigaction sigact_table[TARGET_NSIG];
33 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc);
34 
35 /*
36  * The BSD ABIs use the same singal numbers across all the CPU architectures, so
37  * (unlike Linux) these functions are just the identity mapping. This might not
38  * be true for XyzBSD running on AbcBSD, which doesn't currently work.
39  */
40 int host_to_target_signal(int sig)
41 {
42     return sig;
43 }
44 
45 int target_to_host_signal(int sig)
46 {
47     return sig;
48 }
49 
50 /*
51  * Queue a signal so that it will be send to the virtual CPU as soon as
52  * possible.
53  */
54 void queue_signal(CPUArchState *env, int sig, int si_type,
55                   target_siginfo_t *info)
56 {
57     qemu_log_mask(LOG_UNIMP, "No signal queueing, dropping signal %d\n", sig);
58 }
59 
60 static int fatal_signal(int sig)
61 {
62 
63     switch (sig) {
64     case TARGET_SIGCHLD:
65     case TARGET_SIGURG:
66     case TARGET_SIGWINCH:
67     case TARGET_SIGINFO:
68         /* Ignored by default. */
69         return 0;
70     case TARGET_SIGCONT:
71     case TARGET_SIGSTOP:
72     case TARGET_SIGTSTP:
73     case TARGET_SIGTTIN:
74     case TARGET_SIGTTOU:
75         /* Job control signals.  */
76         return 0;
77     default:
78         return 1;
79     }
80 }
81 
82 /*
83  * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
84  * 'force' part is handled in process_pending_signals().
85  */
86 void force_sig_fault(int sig, int code, abi_ulong addr)
87 {
88     CPUState *cpu = thread_cpu;
89     CPUArchState *env = cpu->env_ptr;
90     target_siginfo_t info = {};
91 
92     info.si_signo = sig;
93     info.si_errno = 0;
94     info.si_code = code;
95     info.si_addr = addr;
96     queue_signal(env, sig, QEMU_SI_FAULT, &info);
97 }
98 
99 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
100 {
101 }
102 
103 void signal_init(void)
104 {
105     TaskState *ts = (TaskState *)thread_cpu->opaque;
106     struct sigaction act;
107     struct sigaction oact;
108     int i;
109     int host_sig;
110 
111     /* Set the signal mask from the host mask. */
112     sigprocmask(0, 0, &ts->signal_mask);
113 
114     sigfillset(&act.sa_mask);
115     act.sa_sigaction = host_signal_handler;
116     act.sa_flags = SA_SIGINFO;
117 
118     for (i = 1; i <= TARGET_NSIG; i++) {
119 #ifdef CONFIG_GPROF
120         if (i == TARGET_SIGPROF) {
121             continue;
122         }
123 #endif
124         host_sig = target_to_host_signal(i);
125         sigaction(host_sig, NULL, &oact);
126         if (oact.sa_sigaction == (void *)SIG_IGN) {
127             sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
128         } else if (oact.sa_sigaction == (void *)SIG_DFL) {
129             sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
130         }
131         /*
132          * If there's already a handler installed then something has
133          * gone horribly wrong, so don't even try to handle that case.
134          * Install some handlers for our own use.  We need at least
135          * SIGSEGV and SIGBUS, to detect exceptions.  We can not just
136          * trap all signals because it affects syscall interrupt
137          * behavior.  But do trap all default-fatal signals.
138          */
139         if (fatal_signal(i)) {
140             sigaction(host_sig, &act, NULL);
141         }
142     }
143 }
144 
145 void process_pending_signals(CPUArchState *cpu_env)
146 {
147 }
148 
149 void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
150                            MMUAccessType access_type, bool maperr, uintptr_t ra)
151 {
152     const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
153 
154     if (tcg_ops->record_sigsegv) {
155         tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
156     }
157 
158     force_sig_fault(TARGET_SIGSEGV,
159                     maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
160                     addr);
161     cpu->exception_index = EXCP_INTERRUPT;
162     cpu_loop_exit_restore(cpu, ra);
163 }
164 
165 void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
166                           MMUAccessType access_type, uintptr_t ra)
167 {
168     const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
169 
170     if (tcg_ops->record_sigbus) {
171         tcg_ops->record_sigbus(cpu, addr, access_type, ra);
172     }
173 
174     force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
175     cpu->exception_index = EXCP_INTERRUPT;
176     cpu_loop_exit_restore(cpu, ra);
177 }
178