]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/signal.c
bsd-user/signal.c: host_to_target_siginfo_noswap
[mirror_qemu.git] / bsd-user / signal.c
CommitLineData
84778508
BS
1/*
2 * Emulation of BSD signals
3 *
4 * Copyright (c) 2003 - 2008 Fabrice Bellard
1366ef81 5 * Copyright (c) 2013 Stacey Son
84778508
BS
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
8167ee88 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
84778508 19 */
84778508 20
5abfac27 21#include "qemu/osdep.h"
84778508 22#include "qemu.h"
0ef59989 23#include "signal-common.h"
6ddc1abe 24#include "trace.h"
fc9f9bdd 25#include "hw/core/tcg-cpu-ops.h"
85fc1b5d 26#include "host-signal.h"
84778508 27
835b04ed
WL
28/*
29 * Stubbed out routines until we merge signal support from bsd-user
30 * fork.
31 */
32
149076ad
WL
33static struct target_sigaction sigact_table[TARGET_NSIG];
34static void host_signal_handler(int host_sig, siginfo_t *info, void *puc);
35
1366ef81
WL
36/*
37 * The BSD ABIs use the same singal numbers across all the CPU architectures, so
38 * (unlike Linux) these functions are just the identity mapping. This might not
39 * be true for XyzBSD running on AbcBSD, which doesn't currently work.
40 */
41int host_to_target_signal(int sig)
42{
43 return sig;
44}
45
46int target_to_host_signal(int sig)
47{
48 return sig;
49}
50
c34f2aaf
WL
51static bool has_trapno(int tsig)
52{
53 return tsig == TARGET_SIGILL ||
54 tsig == TARGET_SIGFPE ||
55 tsig == TARGET_SIGSEGV ||
56 tsig == TARGET_SIGBUS ||
57 tsig == TARGET_SIGTRAP;
58}
59
60
61/* Siginfo conversion. */
62
63/*
64 * Populate tinfo w/o swapping based on guessing which fields are valid.
65 */
66static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
67 const siginfo_t *info)
68{
69 int sig = host_to_target_signal(info->si_signo);
70 int si_code = info->si_code;
71 int si_type;
72
73 /*
74 * Make sure we that the variable portion of the target siginfo is zeroed
75 * out so we don't leak anything into that.
76 */
77 memset(&tinfo->_reason, 0, sizeof(tinfo->_reason));
78
79 /*
80 * This is awkward, because we have to use a combination of the si_code and
81 * si_signo to figure out which of the union's members are valid.o We
82 * therefore make our best guess.
83 *
84 * Once we have made our guess, we record it in the top 16 bits of
85 * the si_code, so that tswap_siginfo() later can use it.
86 * tswap_siginfo() will strip these top bits out before writing
87 * si_code to the guest (sign-extending the lower bits).
88 */
89 tinfo->si_signo = sig;
90 tinfo->si_errno = info->si_errno;
91 tinfo->si_code = info->si_code;
92 tinfo->si_pid = info->si_pid;
93 tinfo->si_uid = info->si_uid;
94 tinfo->si_status = info->si_status;
95 tinfo->si_addr = (abi_ulong)(unsigned long)info->si_addr;
96 /*
97 * si_value is opaque to kernel. On all FreeBSD platforms,
98 * sizeof(sival_ptr) >= sizeof(sival_int) so the following
99 * always will copy the larger element.
100 */
101 tinfo->si_value.sival_ptr =
102 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
103
104 switch (si_code) {
105 /*
106 * All the SI_xxx codes that are defined here are global to
107 * all the signals (they have values that none of the other,
108 * more specific signal info will set).
109 */
110 case SI_USER:
111 case SI_LWP:
112 case SI_KERNEL:
113 case SI_QUEUE:
114 case SI_ASYNCIO:
115 /*
116 * Only the fixed parts are valid (though FreeBSD doesn't always
117 * set all the fields to non-zero values.
118 */
119 si_type = QEMU_SI_NOINFO;
120 break;
121 case SI_TIMER:
122 tinfo->_reason._timer._timerid = info->_reason._timer._timerid;
123 tinfo->_reason._timer._overrun = info->_reason._timer._overrun;
124 si_type = QEMU_SI_TIMER;
125 break;
126 case SI_MESGQ:
127 tinfo->_reason._mesgq._mqd = info->_reason._mesgq._mqd;
128 si_type = QEMU_SI_MESGQ;
129 break;
130 default:
131 /*
132 * We have to go based on the signal number now to figure out
133 * what's valid.
134 */
135 if (has_trapno(sig)) {
136 tinfo->_reason._fault._trapno = info->_reason._fault._trapno;
137 si_type = QEMU_SI_FAULT;
138 }
139#ifdef TARGET_SIGPOLL
140 /*
141 * FreeBSD never had SIGPOLL, but emulates it for Linux so there's
142 * a chance it may popup in the future.
143 */
144 if (sig == TARGET_SIGPOLL) {
145 tinfo->_reason._poll._band = info->_reason._poll._band;
146 si_type = QEMU_SI_POLL;
147 }
148#endif
149 /*
150 * Unsure that this can actually be generated, and our support for
151 * capsicum is somewhere between weak and non-existant, but if we get
152 * one, then we know what to save.
153 */
154 if (sig == TARGET_SIGTRAP) {
155 tinfo->_reason._capsicum._syscall =
156 info->_reason._capsicum._syscall;
157 si_type = QEMU_SI_CAPSICUM;
158 }
159 break;
160 }
161 tinfo->si_code = deposit32(si_code, 24, 8, si_type);
162}
163
5abfac27
WL
164/*
165 * Queue a signal so that it will be send to the virtual CPU as soon as
166 * possible.
167 */
e32a6301
WL
168void queue_signal(CPUArchState *env, int sig, int si_type,
169 target_siginfo_t *info)
5abfac27
WL
170{
171 qemu_log_mask(LOG_UNIMP, "No signal queueing, dropping signal %d\n", sig);
172}
173
149076ad
WL
174static int fatal_signal(int sig)
175{
176
177 switch (sig) {
178 case TARGET_SIGCHLD:
179 case TARGET_SIGURG:
180 case TARGET_SIGWINCH:
181 case TARGET_SIGINFO:
182 /* Ignored by default. */
183 return 0;
184 case TARGET_SIGCONT:
185 case TARGET_SIGSTOP:
186 case TARGET_SIGTSTP:
187 case TARGET_SIGTTIN:
188 case TARGET_SIGTTOU:
189 /* Job control signals. */
190 return 0;
191 default:
192 return 1;
193 }
194}
195
0ef59989
WL
196/*
197 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
198 * 'force' part is handled in process_pending_signals().
199 */
200void force_sig_fault(int sig, int code, abi_ulong addr)
201{
202 CPUState *cpu = thread_cpu;
203 CPUArchState *env = cpu->env_ptr;
204 target_siginfo_t info = {};
205
206 info.si_signo = sig;
207 info.si_errno = 0;
208 info.si_code = code;
209 info.si_addr = addr;
e32a6301 210 queue_signal(env, sig, QEMU_SI_FAULT, &info);
0ef59989
WL
211}
212
149076ad
WL
213static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
214{
215}
216
84778508
BS
217void signal_init(void)
218{
149076ad
WL
219 TaskState *ts = (TaskState *)thread_cpu->opaque;
220 struct sigaction act;
221 struct sigaction oact;
222 int i;
223 int host_sig;
224
225 /* Set the signal mask from the host mask. */
226 sigprocmask(0, 0, &ts->signal_mask);
227
228 sigfillset(&act.sa_mask);
229 act.sa_sigaction = host_signal_handler;
230 act.sa_flags = SA_SIGINFO;
231
232 for (i = 1; i <= TARGET_NSIG; i++) {
233#ifdef CONFIG_GPROF
234 if (i == TARGET_SIGPROF) {
235 continue;
236 }
237#endif
238 host_sig = target_to_host_signal(i);
239 sigaction(host_sig, NULL, &oact);
240 if (oact.sa_sigaction == (void *)SIG_IGN) {
241 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
242 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
243 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
244 }
245 /*
246 * If there's already a handler installed then something has
247 * gone horribly wrong, so don't even try to handle that case.
248 * Install some handlers for our own use. We need at least
249 * SIGSEGV and SIGBUS, to detect exceptions. We can not just
250 * trap all signals because it affects syscall interrupt
251 * behavior. But do trap all default-fatal signals.
252 */
253 if (fatal_signal(i)) {
254 sigaction(host_sig, &act, NULL);
255 }
256 }
84778508
BS
257}
258
9349b4f9 259void process_pending_signals(CPUArchState *cpu_env)
84778508
BS
260{
261}
835b04ed
WL
262
263void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
264 MMUAccessType access_type, bool maperr, uintptr_t ra)
265{
fc9f9bdd
WL
266 const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
267
268 if (tcg_ops->record_sigsegv) {
269 tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
270 }
271
272 force_sig_fault(TARGET_SIGSEGV,
273 maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
274 addr);
275 cpu->exception_index = EXCP_INTERRUPT;
276 cpu_loop_exit_restore(cpu, ra);
835b04ed
WL
277}
278
279void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
280 MMUAccessType access_type, uintptr_t ra)
281{
cfdee273
WL
282 const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
283
284 if (tcg_ops->record_sigbus) {
285 tcg_ops->record_sigbus(cpu, addr, access_type, ra);
286 }
287
288 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
289 cpu->exception_index = EXCP_INTERRUPT;
290 cpu_loop_exit_restore(cpu, ra);
835b04ed 291}