]>
Commit | Line | Data |
---|---|---|
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 |
33 | static struct target_sigaction sigact_table[TARGET_NSIG]; |
34 | static void host_signal_handler(int host_sig, siginfo_t *info, void *puc); | |
c93cbac1 WL |
35 | static void target_to_host_sigset_internal(sigset_t *d, |
36 | const target_sigset_t *s); | |
37 | ||
46f4f76d WL |
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 | } | |
149076ad | 48 | |
1366ef81 WL |
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 | ||
c93cbac1 WL |
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 | ||
aae57ac3 WL |
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 | ||
c93cbac1 WL |
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 | ||
c34f2aaf WL |
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 | ||
c34f2aaf WL |
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 | ||
08eb66d5 WL |
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 | ||
37714547 WL |
312 | /* Returns 1 if given signal should dump core if not handled. */ |
313 | static int core_dump_signal(int sig) | |
314 | { | |
315 | switch (sig) { | |
316 | case TARGET_SIGABRT: | |
317 | case TARGET_SIGFPE: | |
318 | case TARGET_SIGILL: | |
319 | case TARGET_SIGQUIT: | |
320 | case TARGET_SIGSEGV: | |
321 | case TARGET_SIGTRAP: | |
322 | case TARGET_SIGBUS: | |
323 | return 1; | |
324 | default: | |
325 | return 0; | |
326 | } | |
327 | } | |
328 | ||
329 | /* Abort execution with signal. */ | |
330 | static void QEMU_NORETURN dump_core_and_abort(int target_sig) | |
331 | { | |
332 | CPUArchState *env = thread_cpu->env_ptr; | |
333 | CPUState *cpu = env_cpu(env); | |
334 | TaskState *ts = cpu->opaque; | |
335 | int core_dumped = 0; | |
336 | int host_sig; | |
337 | struct sigaction act; | |
338 | ||
339 | host_sig = target_to_host_signal(target_sig); | |
340 | gdb_signalled(env, target_sig); | |
341 | ||
342 | /* Dump core if supported by target binary format */ | |
343 | if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) { | |
344 | stop_all_tasks(); | |
345 | core_dumped = | |
346 | ((*ts->bprm->core_dump)(target_sig, env) == 0); | |
347 | } | |
348 | if (core_dumped) { | |
349 | struct rlimit nodump; | |
350 | ||
351 | /* | |
352 | * We already dumped the core of target process, we don't want | |
353 | * a coredump of qemu itself. | |
354 | */ | |
355 | getrlimit(RLIMIT_CORE, &nodump); | |
356 | nodump.rlim_cur = 0; | |
357 | setrlimit(RLIMIT_CORE, &nodump); | |
358 | (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) " | |
359 | "- %s\n", target_sig, strsignal(host_sig), "core dumped"); | |
360 | } | |
361 | ||
362 | /* | |
363 | * The proper exit code for dying from an uncaught signal is | |
364 | * -<signal>. The kernel doesn't allow exit() or _exit() to pass | |
365 | * a negative value. To get the proper exit code we need to | |
366 | * actually die from an uncaught signal. Here the default signal | |
367 | * handler is installed, we send ourself a signal and we wait for | |
368 | * it to arrive. | |
369 | */ | |
370 | memset(&act, 0, sizeof(act)); | |
371 | sigfillset(&act.sa_mask); | |
372 | act.sa_handler = SIG_DFL; | |
373 | sigaction(host_sig, &act, NULL); | |
374 | ||
375 | kill(getpid(), host_sig); | |
376 | ||
377 | /* | |
378 | * Make sure the signal isn't masked (just reuse the mask inside | |
379 | * of act). | |
380 | */ | |
381 | sigdelset(&act.sa_mask, host_sig); | |
382 | sigsuspend(&act.sa_mask); | |
383 | ||
384 | /* unreachable */ | |
385 | abort(); | |
386 | } | |
387 | ||
5abfac27 WL |
388 | /* |
389 | * Queue a signal so that it will be send to the virtual CPU as soon as | |
390 | * possible. | |
391 | */ | |
e32a6301 WL |
392 | void queue_signal(CPUArchState *env, int sig, int si_type, |
393 | target_siginfo_t *info) | |
5abfac27 | 394 | { |
38be620c WL |
395 | CPUState *cpu = env_cpu(env); |
396 | TaskState *ts = cpu->opaque; | |
397 | ||
398 | trace_user_queue_signal(env, sig); | |
399 | ||
400 | info->si_code = deposit32(info->si_code, 24, 8, si_type); | |
401 | ||
402 | ts->sync_signal.info = *info; | |
403 | ts->sync_signal.pending = sig; | |
404 | /* Signal that a new signal is pending. */ | |
405 | qatomic_set(&ts->signal_pending, 1); | |
406 | return; | |
5abfac27 WL |
407 | } |
408 | ||
149076ad WL |
409 | static int fatal_signal(int sig) |
410 | { | |
411 | ||
412 | switch (sig) { | |
413 | case TARGET_SIGCHLD: | |
414 | case TARGET_SIGURG: | |
415 | case TARGET_SIGWINCH: | |
416 | case TARGET_SIGINFO: | |
417 | /* Ignored by default. */ | |
418 | return 0; | |
419 | case TARGET_SIGCONT: | |
420 | case TARGET_SIGSTOP: | |
421 | case TARGET_SIGTSTP: | |
422 | case TARGET_SIGTTIN: | |
423 | case TARGET_SIGTTOU: | |
424 | /* Job control signals. */ | |
425 | return 0; | |
426 | default: | |
427 | return 1; | |
428 | } | |
429 | } | |
430 | ||
0ef59989 WL |
431 | /* |
432 | * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the | |
433 | * 'force' part is handled in process_pending_signals(). | |
434 | */ | |
435 | void force_sig_fault(int sig, int code, abi_ulong addr) | |
436 | { | |
437 | CPUState *cpu = thread_cpu; | |
438 | CPUArchState *env = cpu->env_ptr; | |
439 | target_siginfo_t info = {}; | |
440 | ||
441 | info.si_signo = sig; | |
442 | info.si_errno = 0; | |
443 | info.si_code = code; | |
444 | info.si_addr = addr; | |
e32a6301 | 445 | queue_signal(env, sig, QEMU_SI_FAULT, &info); |
0ef59989 WL |
446 | } |
447 | ||
149076ad WL |
448 | static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) |
449 | { | |
e625c7ef WL |
450 | CPUArchState *env = thread_cpu->env_ptr; |
451 | CPUState *cpu = env_cpu(env); | |
452 | TaskState *ts = cpu->opaque; | |
453 | target_siginfo_t tinfo; | |
454 | ucontext_t *uc = puc; | |
455 | struct emulated_sigtable *k; | |
456 | int guest_sig; | |
457 | uintptr_t pc = 0; | |
458 | bool sync_sig = false; | |
459 | ||
460 | /* | |
461 | * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special | |
462 | * handling wrt signal blocking and unwinding. | |
463 | */ | |
464 | if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) { | |
465 | MMUAccessType access_type; | |
466 | uintptr_t host_addr; | |
467 | abi_ptr guest_addr; | |
468 | bool is_write; | |
469 | ||
470 | host_addr = (uintptr_t)info->si_addr; | |
471 | ||
472 | /* | |
473 | * Convert forcefully to guest address space: addresses outside | |
474 | * reserved_va are still valid to report via SEGV_MAPERR. | |
475 | */ | |
476 | guest_addr = h2g_nocheck(host_addr); | |
477 | ||
478 | pc = host_signal_pc(uc); | |
479 | is_write = host_signal_write(info, uc); | |
480 | access_type = adjust_signal_pc(&pc, is_write); | |
481 | ||
482 | if (host_sig == SIGSEGV) { | |
483 | bool maperr = true; | |
484 | ||
485 | if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) { | |
486 | /* If this was a write to a TB protected page, restart. */ | |
487 | if (is_write && | |
488 | handle_sigsegv_accerr_write(cpu, &uc->uc_sigmask, | |
489 | pc, guest_addr)) { | |
490 | return; | |
491 | } | |
492 | ||
493 | /* | |
494 | * With reserved_va, the whole address space is PROT_NONE, | |
495 | * which means that we may get ACCERR when we want MAPERR. | |
496 | */ | |
497 | if (page_get_flags(guest_addr) & PAGE_VALID) { | |
498 | maperr = false; | |
499 | } else { | |
500 | info->si_code = SEGV_MAPERR; | |
501 | } | |
502 | } | |
503 | ||
504 | sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); | |
505 | cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc); | |
506 | } else { | |
507 | sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); | |
508 | if (info->si_code == BUS_ADRALN) { | |
509 | cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc); | |
510 | } | |
511 | } | |
512 | ||
513 | sync_sig = true; | |
514 | } | |
515 | ||
516 | /* Get the target signal number. */ | |
517 | guest_sig = host_to_target_signal(host_sig); | |
518 | if (guest_sig < 1 || guest_sig > TARGET_NSIG) { | |
519 | return; | |
520 | } | |
521 | trace_user_host_signal(cpu, host_sig, guest_sig); | |
522 | ||
523 | host_to_target_siginfo_noswap(&tinfo, info); | |
524 | ||
525 | k = &ts->sigtab[guest_sig - 1]; | |
526 | k->info = tinfo; | |
527 | k->pending = guest_sig; | |
528 | ts->signal_pending = 1; | |
529 | ||
530 | /* | |
531 | * For synchronous signals, unwind the cpu state to the faulting | |
532 | * insn and then exit back to the main loop so that the signal | |
533 | * is delivered immediately. | |
534 | */ | |
535 | if (sync_sig) { | |
536 | cpu->exception_index = EXCP_INTERRUPT; | |
537 | cpu_loop_exit_restore(cpu, pc); | |
538 | } | |
539 | ||
540 | rewind_if_in_safe_syscall(puc); | |
541 | ||
542 | /* | |
543 | * Block host signals until target signal handler entered. We | |
544 | * can't block SIGSEGV or SIGBUS while we're executing guest | |
545 | * code in case the guest code provokes one in the window between | |
546 | * now and it getting out to the main loop. Signals will be | |
547 | * unblocked again in process_pending_signals(). | |
548 | */ | |
549 | sigfillset(&uc->uc_sigmask); | |
550 | sigdelset(&uc->uc_sigmask, SIGSEGV); | |
551 | sigdelset(&uc->uc_sigmask, SIGBUS); | |
552 | ||
553 | /* Interrupt the virtual CPU as soon as possible. */ | |
554 | cpu_exit(thread_cpu); | |
149076ad WL |
555 | } |
556 | ||
46f4f76d WL |
557 | static inline abi_ulong get_sigframe(struct target_sigaction *ka, |
558 | CPUArchState *env, size_t frame_size) | |
559 | { | |
560 | TaskState *ts = (TaskState *)thread_cpu->opaque; | |
561 | abi_ulong sp; | |
562 | ||
563 | /* Use default user stack */ | |
564 | sp = get_sp_from_cpustate(env); | |
565 | ||
566 | if ((ka->sa_flags & TARGET_SA_ONSTACK) && sas_ss_flags(ts, sp) == 0) { | |
567 | sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size; | |
568 | } | |
569 | ||
570 | /* TODO: make this a target_arch function / define */ | |
571 | #if defined(TARGET_ARM) | |
572 | return (sp - frame_size) & ~7; | |
573 | #elif defined(TARGET_AARCH64) | |
574 | return (sp - frame_size) & ~15; | |
575 | #else | |
576 | return sp - frame_size; | |
577 | #endif | |
578 | } | |
579 | ||
580 | /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */ | |
581 | ||
582 | static void setup_frame(int sig, int code, struct target_sigaction *ka, | |
583 | target_sigset_t *set, target_siginfo_t *tinfo, CPUArchState *env) | |
584 | { | |
585 | struct target_sigframe *frame; | |
586 | abi_ulong frame_addr; | |
587 | int i; | |
588 | ||
589 | frame_addr = get_sigframe(ka, env, sizeof(*frame)); | |
590 | trace_user_setup_frame(env, frame_addr); | |
591 | if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { | |
592 | unlock_user_struct(frame, frame_addr, 1); | |
593 | dump_core_and_abort(TARGET_SIGILL); | |
594 | return; | |
595 | } | |
596 | ||
597 | memset(frame, 0, sizeof(*frame)); | |
598 | setup_sigframe_arch(env, frame_addr, frame, 0); | |
599 | ||
600 | for (i = 0; i < TARGET_NSIG_WORDS; i++) { | |
601 | __put_user(set->__bits[i], &frame->sf_uc.uc_sigmask.__bits[i]); | |
602 | } | |
603 | ||
604 | if (tinfo) { | |
605 | frame->sf_si.si_signo = tinfo->si_signo; | |
606 | frame->sf_si.si_errno = tinfo->si_errno; | |
607 | frame->sf_si.si_code = tinfo->si_code; | |
608 | frame->sf_si.si_pid = tinfo->si_pid; | |
609 | frame->sf_si.si_uid = tinfo->si_uid; | |
610 | frame->sf_si.si_status = tinfo->si_status; | |
611 | frame->sf_si.si_addr = tinfo->si_addr; | |
612 | /* see host_to_target_siginfo_noswap() for more details */ | |
613 | frame->sf_si.si_value.sival_ptr = tinfo->si_value.sival_ptr; | |
614 | /* | |
615 | * At this point, whatever is in the _reason union is complete | |
616 | * and in target order, so just copy the whole thing over, even | |
617 | * if it's too large for this specific signal. | |
618 | * host_to_target_siginfo_noswap() and tswap_siginfo() have ensured | |
619 | * that's so. | |
620 | */ | |
621 | memcpy(&frame->sf_si._reason, &tinfo->_reason, | |
622 | sizeof(tinfo->_reason)); | |
623 | } | |
624 | ||
625 | set_sigtramp_args(env, sig, frame, frame_addr, ka); | |
626 | ||
627 | unlock_user_struct(frame, frame_addr, 1); | |
628 | } | |
629 | ||
84778508 BS |
630 | void signal_init(void) |
631 | { | |
149076ad WL |
632 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
633 | struct sigaction act; | |
634 | struct sigaction oact; | |
635 | int i; | |
636 | int host_sig; | |
637 | ||
638 | /* Set the signal mask from the host mask. */ | |
639 | sigprocmask(0, 0, &ts->signal_mask); | |
640 | ||
641 | sigfillset(&act.sa_mask); | |
642 | act.sa_sigaction = host_signal_handler; | |
643 | act.sa_flags = SA_SIGINFO; | |
644 | ||
645 | for (i = 1; i <= TARGET_NSIG; i++) { | |
646 | #ifdef CONFIG_GPROF | |
647 | if (i == TARGET_SIGPROF) { | |
648 | continue; | |
649 | } | |
650 | #endif | |
651 | host_sig = target_to_host_signal(i); | |
652 | sigaction(host_sig, NULL, &oact); | |
653 | if (oact.sa_sigaction == (void *)SIG_IGN) { | |
654 | sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN; | |
655 | } else if (oact.sa_sigaction == (void *)SIG_DFL) { | |
656 | sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL; | |
657 | } | |
658 | /* | |
659 | * If there's already a handler installed then something has | |
660 | * gone horribly wrong, so don't even try to handle that case. | |
661 | * Install some handlers for our own use. We need at least | |
662 | * SIGSEGV and SIGBUS, to detect exceptions. We can not just | |
663 | * trap all signals because it affects syscall interrupt | |
664 | * behavior. But do trap all default-fatal signals. | |
665 | */ | |
666 | if (fatal_signal(i)) { | |
667 | sigaction(host_sig, &act, NULL); | |
668 | } | |
669 | } | |
84778508 BS |
670 | } |
671 | ||
6c6d4b56 WL |
672 | static void handle_pending_signal(CPUArchState *env, int sig, |
673 | struct emulated_sigtable *k) | |
674 | { | |
675 | CPUState *cpu = env_cpu(env); | |
676 | TaskState *ts = cpu->opaque; | |
677 | struct target_sigaction *sa; | |
678 | int code; | |
679 | sigset_t set; | |
680 | abi_ulong handler; | |
681 | target_siginfo_t tinfo; | |
682 | target_sigset_t target_old_set; | |
683 | ||
684 | trace_user_handle_signal(env, sig); | |
685 | ||
686 | k->pending = 0; | |
687 | ||
688 | sig = gdb_handlesig(cpu, sig); | |
689 | if (!sig) { | |
690 | sa = NULL; | |
691 | handler = TARGET_SIG_IGN; | |
692 | } else { | |
693 | sa = &sigact_table[sig - 1]; | |
694 | handler = sa->_sa_handler; | |
695 | } | |
696 | ||
697 | if (do_strace) { | |
698 | print_taken_signal(sig, &k->info); | |
699 | } | |
700 | ||
701 | if (handler == TARGET_SIG_DFL) { | |
702 | /* | |
703 | * default handler : ignore some signal. The other are job | |
704 | * control or fatal. | |
705 | */ | |
706 | if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || | |
707 | sig == TARGET_SIGTTOU) { | |
708 | kill(getpid(), SIGSTOP); | |
709 | } else if (sig != TARGET_SIGCHLD && sig != TARGET_SIGURG && | |
710 | sig != TARGET_SIGINFO && sig != TARGET_SIGWINCH && | |
711 | sig != TARGET_SIGCONT) { | |
712 | dump_core_and_abort(sig); | |
713 | } | |
714 | } else if (handler == TARGET_SIG_IGN) { | |
715 | /* ignore sig */ | |
716 | } else if (handler == TARGET_SIG_ERR) { | |
717 | dump_core_and_abort(sig); | |
718 | } else { | |
719 | /* compute the blocked signals during the handler execution */ | |
720 | sigset_t *blocked_set; | |
721 | ||
722 | target_to_host_sigset(&set, &sa->sa_mask); | |
723 | /* | |
724 | * SA_NODEFER indicates that the current signal should not be | |
725 | * blocked during the handler. | |
726 | */ | |
727 | if (!(sa->sa_flags & TARGET_SA_NODEFER)) { | |
728 | sigaddset(&set, target_to_host_signal(sig)); | |
729 | } | |
730 | ||
731 | /* | |
732 | * Save the previous blocked signal state to restore it at the | |
733 | * end of the signal execution (see do_sigreturn). | |
734 | */ | |
735 | host_to_target_sigset_internal(&target_old_set, &ts->signal_mask); | |
736 | ||
737 | blocked_set = ts->in_sigsuspend ? | |
738 | &ts->sigsuspend_mask : &ts->signal_mask; | |
739 | sigorset(&ts->signal_mask, blocked_set, &set); | |
740 | ts->in_sigsuspend = false; | |
741 | sigprocmask(SIG_SETMASK, &ts->signal_mask, NULL); | |
742 | ||
743 | /* XXX VM86 on x86 ??? */ | |
744 | ||
745 | code = k->info.si_code; /* From host, so no si_type */ | |
746 | /* prepare the stack frame of the virtual CPU */ | |
747 | if (sa->sa_flags & TARGET_SA_SIGINFO) { | |
748 | tswap_siginfo(&tinfo, &k->info); | |
749 | setup_frame(sig, code, sa, &target_old_set, &tinfo, env); | |
750 | } else { | |
751 | setup_frame(sig, code, sa, &target_old_set, NULL, env); | |
752 | } | |
753 | if (sa->sa_flags & TARGET_SA_RESETHAND) { | |
754 | sa->_sa_handler = TARGET_SIG_DFL; | |
755 | } | |
756 | } | |
757 | } | |
758 | ||
d7acd317 | 759 | void process_pending_signals(CPUArchState *env) |
84778508 | 760 | { |
d7acd317 WL |
761 | CPUState *cpu = env_cpu(env); |
762 | int sig; | |
763 | sigset_t *blocked_set, set; | |
764 | struct emulated_sigtable *k; | |
765 | TaskState *ts = cpu->opaque; | |
766 | ||
767 | while (qatomic_read(&ts->signal_pending)) { | |
768 | sigfillset(&set); | |
769 | sigprocmask(SIG_SETMASK, &set, 0); | |
770 | ||
771 | restart_scan: | |
772 | sig = ts->sync_signal.pending; | |
773 | if (sig) { | |
774 | /* | |
775 | * Synchronous signals are forced by the emulated CPU in some way. | |
776 | * If they are set to ignore, restore the default handler (see | |
777 | * sys/kern_sig.c trapsignal() and execsigs() for this behavior) | |
778 | * though maybe this is done only when forcing exit for non SIGCHLD. | |
779 | */ | |
780 | if (sigismember(&ts->signal_mask, target_to_host_signal(sig)) || | |
781 | sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) { | |
782 | sigdelset(&ts->signal_mask, target_to_host_signal(sig)); | |
783 | sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL; | |
784 | } | |
785 | handle_pending_signal(env, sig, &ts->sync_signal); | |
786 | } | |
787 | ||
788 | k = ts->sigtab; | |
789 | for (sig = 1; sig <= TARGET_NSIG; sig++, k++) { | |
790 | blocked_set = ts->in_sigsuspend ? | |
791 | &ts->sigsuspend_mask : &ts->signal_mask; | |
792 | if (k->pending && | |
793 | !sigismember(blocked_set, target_to_host_signal(sig))) { | |
794 | handle_pending_signal(env, sig, k); | |
795 | /* | |
796 | * Restart scan from the beginning, as handle_pending_signal | |
797 | * might have resulted in a new synchronous signal (eg SIGSEGV). | |
798 | */ | |
799 | goto restart_scan; | |
800 | } | |
801 | } | |
802 | ||
803 | /* | |
804 | * Unblock signals and check one more time. Unblocking signals may cause | |
805 | * us to take another host signal, which will set signal_pending again. | |
806 | */ | |
807 | qatomic_set(&ts->signal_pending, 0); | |
808 | ts->in_sigsuspend = false; | |
809 | set = ts->signal_mask; | |
810 | sigdelset(&set, SIGSEGV); | |
811 | sigdelset(&set, SIGBUS); | |
812 | sigprocmask(SIG_SETMASK, &set, 0); | |
813 | } | |
814 | ts->in_sigsuspend = false; | |
84778508 | 815 | } |
835b04ed WL |
816 | |
817 | void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr, | |
818 | MMUAccessType access_type, bool maperr, uintptr_t ra) | |
819 | { | |
fc9f9bdd WL |
820 | const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; |
821 | ||
822 | if (tcg_ops->record_sigsegv) { | |
823 | tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra); | |
824 | } | |
825 | ||
826 | force_sig_fault(TARGET_SIGSEGV, | |
827 | maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR, | |
828 | addr); | |
829 | cpu->exception_index = EXCP_INTERRUPT; | |
830 | cpu_loop_exit_restore(cpu, ra); | |
835b04ed WL |
831 | } |
832 | ||
833 | void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, | |
834 | MMUAccessType access_type, uintptr_t ra) | |
835 | { | |
cfdee273 WL |
836 | const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; |
837 | ||
838 | if (tcg_ops->record_sigbus) { | |
839 | tcg_ops->record_sigbus(cpu, addr, access_type, ra); | |
840 | } | |
841 | ||
842 | force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr); | |
843 | cpu->exception_index = EXCP_INTERRUPT; | |
844 | cpu_loop_exit_restore(cpu, ra); | |
835b04ed | 845 | } |