blob: 666d6e14fb5ec0fb2724277ab4a3a63be5a0b400 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard66fb9762003-03-23 01:06:05 +00002 * Emulation of Linux signals
bellard31e31b82003-02-18 22:55:36 +00003 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
bellard66fb9762003-03-23 01:06:05 +000022#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000023#include <stdarg.h>
bellard2677e102003-04-10 00:03:27 +000024#include <unistd.h>
bellard31e31b82003-02-18 22:55:36 +000025#include <signal.h>
bellard66fb9762003-03-23 01:06:05 +000026#include <errno.h>
bellard31e31b82003-02-18 22:55:36 +000027#include <sys/ucontext.h>
28
bellard0d330192003-04-29 21:10:09 +000029#ifdef __ia64__
30#undef uc_mcontext
31#undef uc_sigmask
32#undef uc_stack
33#undef uc_link
34#endif
35
bellard3ef693a2003-03-23 20:17:16 +000036#include "qemu.h"
bellard66fb9762003-03-23 01:06:05 +000037
38//#define DEBUG_SIGNAL
39
40#define MAX_SIGQUEUE_SIZE 1024
41
42struct sigqueue {
43 struct sigqueue *next;
bellard9de5e442003-03-23 16:49:39 +000044 target_siginfo_t info;
bellard66fb9762003-03-23 01:06:05 +000045};
bellard31e31b82003-02-18 22:55:36 +000046
47struct emulated_sigaction {
48 struct target_sigaction sa;
bellard66fb9762003-03-23 01:06:05 +000049 int pending; /* true if signal is pending */
50 struct sigqueue *first;
51 struct sigqueue info; /* in order to always have memory for the
52 first signal, we put it here */
bellard31e31b82003-02-18 22:55:36 +000053};
54
bellard66fb9762003-03-23 01:06:05 +000055static struct emulated_sigaction sigact_table[TARGET_NSIG];
56static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57static struct sigqueue *first_free; /* first free siginfo queue entry */
58static int signal_pending; /* non zero if a signal may be pending */
bellard31e31b82003-02-18 22:55:36 +000059
bellard66fb9762003-03-23 01:06:05 +000060static void host_signal_handler(int host_signum, siginfo_t *info,
61 void *puc);
62
bellard9e5f5282003-07-13 17:33:54 +000063static uint8_t host_to_target_signal_table[65] = {
64 [SIGHUP] = TARGET_SIGHUP,
65 [SIGINT] = TARGET_SIGINT,
66 [SIGQUIT] = TARGET_SIGQUIT,
67 [SIGILL] = TARGET_SIGILL,
68 [SIGTRAP] = TARGET_SIGTRAP,
69 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000070/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000071 [SIGBUS] = TARGET_SIGBUS,
72 [SIGFPE] = TARGET_SIGFPE,
73 [SIGKILL] = TARGET_SIGKILL,
74 [SIGUSR1] = TARGET_SIGUSR1,
75 [SIGSEGV] = TARGET_SIGSEGV,
76 [SIGUSR2] = TARGET_SIGUSR2,
77 [SIGPIPE] = TARGET_SIGPIPE,
78 [SIGALRM] = TARGET_SIGALRM,
79 [SIGTERM] = TARGET_SIGTERM,
80#ifdef SIGSTKFLT
81 [SIGSTKFLT] = TARGET_SIGSTKFLT,
82#endif
83 [SIGCHLD] = TARGET_SIGCHLD,
84 [SIGCONT] = TARGET_SIGCONT,
85 [SIGSTOP] = TARGET_SIGSTOP,
86 [SIGTSTP] = TARGET_SIGTSTP,
87 [SIGTTIN] = TARGET_SIGTTIN,
88 [SIGTTOU] = TARGET_SIGTTOU,
89 [SIGURG] = TARGET_SIGURG,
90 [SIGXCPU] = TARGET_SIGXCPU,
91 [SIGXFSZ] = TARGET_SIGXFSZ,
92 [SIGVTALRM] = TARGET_SIGVTALRM,
93 [SIGPROF] = TARGET_SIGPROF,
94 [SIGWINCH] = TARGET_SIGWINCH,
95 [SIGIO] = TARGET_SIGIO,
96 [SIGPWR] = TARGET_SIGPWR,
97 [SIGSYS] = TARGET_SIGSYS,
98 /* next signals stay the same */
99};
100static uint8_t target_to_host_signal_table[65];
101
bellard31e31b82003-02-18 22:55:36 +0000102static inline int host_to_target_signal(int sig)
103{
bellard9e5f5282003-07-13 17:33:54 +0000104 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000105}
106
107static inline int target_to_host_signal(int sig)
108{
bellard9e5f5282003-07-13 17:33:54 +0000109 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000110}
111
bellard9e5f5282003-07-13 17:33:54 +0000112void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000113{
114 int i;
bellard9e5f5282003-07-13 17:33:54 +0000115 unsigned long sigmask;
116 uint32_t target_sigmask;
117
118 sigmask = ((unsigned long *)s)[0];
119 target_sigmask = 0;
120 for(i = 0; i < 32; i++) {
121 if (sigmask & (1 << i))
122 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
123 }
124#if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
125 d->sig[0] = tswapl(target_sigmask);
126 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard66fb9762003-03-23 01:06:05 +0000127 d->sig[i] = tswapl(((unsigned long *)s)[i]);
128 }
bellard9e5f5282003-07-13 17:33:54 +0000129#elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
130 d->sig[0] = tswapl(target_sigmask);
131 d->sig[1] = tswapl(sigmask >> 32);
132#else
133#error host_to_target_sigset
134#endif
bellard66fb9762003-03-23 01:06:05 +0000135}
136
bellard9e5f5282003-07-13 17:33:54 +0000137void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000138{
139 int i;
bellard9e5f5282003-07-13 17:33:54 +0000140 unsigned long sigmask;
141 target_ulong target_sigmask;
142
143 target_sigmask = tswapl(s->sig[0]);
144 sigmask = 0;
145 for(i = 0; i < 32; i++) {
146 if (target_sigmask & (1 << i))
147 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
148 }
149#if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
150 ((unsigned long *)d)[0] = sigmask;
151 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard66fb9762003-03-23 01:06:05 +0000152 ((unsigned long *)d)[i] = tswapl(s->sig[i]);
153 }
bellard9e5f5282003-07-13 17:33:54 +0000154#elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard9dfa5b42003-08-11 20:35:58 +0000155 ((unsigned long *)d)[0] = sigmask | ((unsigned long)tswapl(s->sig[1]) << 32);
bellard9e5f5282003-07-13 17:33:54 +0000156#else
157#error target_to_host_sigset
158#endif /* TARGET_LONG_BITS */
bellard66fb9762003-03-23 01:06:05 +0000159}
160
161void host_to_target_old_sigset(target_ulong *old_sigset,
162 const sigset_t *sigset)
163{
bellard9e5f5282003-07-13 17:33:54 +0000164 target_sigset_t d;
165 host_to_target_sigset(&d, sigset);
166 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000167}
168
169void target_to_host_old_sigset(sigset_t *sigset,
170 const target_ulong *old_sigset)
171{
bellard9e5f5282003-07-13 17:33:54 +0000172 target_sigset_t d;
173 int i;
174
175 d.sig[0] = *old_sigset;
176 for(i = 1;i < TARGET_NSIG_WORDS; i++)
177 d.sig[i] = 0;
178 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000179}
180
bellard9de5e442003-03-23 16:49:39 +0000181/* siginfo conversion */
182
183static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
184 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000185{
bellard9de5e442003-03-23 16:49:39 +0000186 int sig;
187 sig = host_to_target_signal(info->si_signo);
188 tinfo->si_signo = sig;
189 tinfo->si_errno = 0;
190 tinfo->si_code = 0;
bellard447db212003-05-10 15:10:36 +0000191 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
192 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000193 /* should never come here, but who knows. The information for
194 the target is irrelevant */
195 tinfo->_sifields._sigfault._addr = 0;
196 } else if (sig >= TARGET_SIGRTMIN) {
197 tinfo->_sifields._rt._pid = info->si_pid;
198 tinfo->_sifields._rt._uid = info->si_uid;
199 /* XXX: potential problem if 64 bit */
200 tinfo->_sifields._rt._sigval.sival_ptr =
201 (target_ulong)info->si_value.sival_ptr;
202 }
bellard66fb9762003-03-23 01:06:05 +0000203}
204
bellard9de5e442003-03-23 16:49:39 +0000205static void tswap_siginfo(target_siginfo_t *tinfo,
206 const target_siginfo_t *info)
207{
208 int sig;
209 sig = info->si_signo;
210 tinfo->si_signo = tswap32(sig);
211 tinfo->si_errno = tswap32(info->si_errno);
212 tinfo->si_code = tswap32(info->si_code);
bellard447db212003-05-10 15:10:36 +0000213 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
214 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000215 tinfo->_sifields._sigfault._addr =
216 tswapl(info->_sifields._sigfault._addr);
217 } else if (sig >= TARGET_SIGRTMIN) {
218 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
219 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
220 tinfo->_sifields._rt._sigval.sival_ptr =
221 tswapl(info->_sifields._rt._sigval.sival_ptr);
222 }
223}
224
225
226void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
227{
228 host_to_target_siginfo_noswap(tinfo, info);
229 tswap_siginfo(tinfo, tinfo);
230}
231
232/* XXX: we support only POSIX RT signals are used. */
233/* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
234void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000235{
236 info->si_signo = tswap32(tinfo->si_signo);
237 info->si_errno = tswap32(tinfo->si_errno);
238 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000239 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
240 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
241 info->si_value.sival_ptr =
242 (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000243}
244
bellard31e31b82003-02-18 22:55:36 +0000245void signal_init(void)
246{
247 struct sigaction act;
bellard9e5f5282003-07-13 17:33:54 +0000248 int i, j;
bellard31e31b82003-02-18 22:55:36 +0000249
bellard9e5f5282003-07-13 17:33:54 +0000250 /* generate signal conversion tables */
251 for(i = 1; i <= 64; i++) {
252 if (host_to_target_signal_table[i] == 0)
253 host_to_target_signal_table[i] = i;
254 }
255 for(i = 1; i <= 64; i++) {
256 j = host_to_target_signal_table[i];
257 target_to_host_signal_table[j] = i;
258 }
259
bellard9de5e442003-03-23 16:49:39 +0000260 /* set all host signal handlers. ALL signals are blocked during
261 the handlers to serialize them. */
262 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000263 act.sa_flags = SA_SIGINFO;
264 act.sa_sigaction = host_signal_handler;
265 for(i = 1; i < NSIG; i++) {
bellardc9087c22003-05-27 23:25:41 +0000266 sigaction(i, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000267 }
268
269 memset(sigact_table, 0, sizeof(sigact_table));
bellard66fb9762003-03-23 01:06:05 +0000270
271 first_free = &sigqueue_table[0];
272 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
273 sigqueue_table[i].next = &sigqueue_table[i + 1];
274 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
bellard31e31b82003-02-18 22:55:36 +0000275}
276
bellard66fb9762003-03-23 01:06:05 +0000277/* signal queue handling */
278
279static inline struct sigqueue *alloc_sigqueue(void)
280{
281 struct sigqueue *q = first_free;
282 if (!q)
283 return NULL;
284 first_free = q->next;
285 return q;
286}
287
288static inline void free_sigqueue(struct sigqueue *q)
289{
290 q->next = first_free;
291 first_free = q;
292}
293
bellard9de5e442003-03-23 16:49:39 +0000294/* abort execution with signal */
295void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000296{
297 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000298 host_sig = target_to_host_signal(sig);
bellardbc8a22c2003-03-30 21:02:40 +0000299 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000300 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000301#if 1
bellard66fb9762003-03-23 01:06:05 +0000302 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000303#else
304 {
305 struct sigaction act;
306 sigemptyset(&act.sa_mask);
307 act.sa_flags = SA_SIGINFO;
308 act.sa_sigaction = SIG_DFL;
309 sigaction(SIGABRT, &act, NULL);
310 abort();
311 }
312#endif
bellard66fb9762003-03-23 01:06:05 +0000313}
314
bellard9de5e442003-03-23 16:49:39 +0000315/* queue a signal so that it will be send to the virtual CPU as soon
316 as possible */
317int queue_signal(int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000318{
bellard66fb9762003-03-23 01:06:05 +0000319 struct emulated_sigaction *k;
bellard9de5e442003-03-23 16:49:39 +0000320 struct sigqueue *q, **pq;
bellard66fb9762003-03-23 01:06:05 +0000321 target_ulong handler;
322
bellard9de5e442003-03-23 16:49:39 +0000323#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000324 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000325 sig);
bellard66fb9762003-03-23 01:06:05 +0000326#endif
bellard9de5e442003-03-23 16:49:39 +0000327 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000328 handler = k->sa._sa_handler;
329 if (handler == TARGET_SIG_DFL) {
330 /* default handler : ignore some signal. The other are fatal */
331 if (sig != TARGET_SIGCHLD &&
332 sig != TARGET_SIGURG &&
333 sig != TARGET_SIGWINCH) {
334 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000335 } else {
336 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000337 }
338 } else if (handler == TARGET_SIG_IGN) {
339 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000340 return 0;
bellard66fb9762003-03-23 01:06:05 +0000341 } else if (handler == TARGET_SIG_ERR) {
342 force_sig(sig);
343 } else {
bellard9de5e442003-03-23 16:49:39 +0000344 pq = &k->first;
345 if (sig < TARGET_SIGRTMIN) {
346 /* if non real time signal, we queue exactly one signal */
347 if (!k->pending)
348 q = &k->info;
349 else
350 return 0;
351 } else {
352 if (!k->pending) {
353 /* first signal */
354 q = &k->info;
355 } else {
356 q = alloc_sigqueue();
357 if (!q)
358 return -EAGAIN;
359 while (*pq != NULL)
360 pq = &(*pq)->next;
361 }
362 }
363 *pq = q;
364 q->info = *info;
365 q->next = NULL;
366 k->pending = 1;
367 /* signal that a new signal is pending */
368 signal_pending = 1;
369 return 1; /* indicates that the signal was queued */
370 }
371}
372
bellard9de5e442003-03-23 16:49:39 +0000373static void host_signal_handler(int host_signum, siginfo_t *info,
374 void *puc)
375{
376 int sig;
377 target_siginfo_t tinfo;
378
379 /* the CPU emulator uses some host signals to detect exceptions,
380 we we forward to it some signals */
bellard58fe2f12004-02-16 22:11:32 +0000381 if (host_signum == SIGSEGV || host_signum == SIGBUS
382#if defined(TARGET_I386) && defined(USE_CODE_COPY)
383 || host_signum == SIGFPE
384#endif
385 ) {
bellardb346ff42003-06-15 20:05:50 +0000386 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000387 return;
388 }
389
390 /* get target signal number */
391 sig = host_to_target_signal(host_signum);
392 if (sig < 1 || sig > TARGET_NSIG)
393 return;
394#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000395 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000396#endif
397 host_to_target_siginfo_noswap(&tinfo, info);
398 if (queue_signal(sig, &tinfo) == 1) {
399 /* interrupt the virtual CPU as soon as possible */
bellard68a79312003-06-30 13:12:32 +0000400 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000401 }
bellard31e31b82003-02-18 22:55:36 +0000402}
403
bellard66fb9762003-03-23 01:06:05 +0000404int do_sigaction(int sig, const struct target_sigaction *act,
405 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000406{
bellard66fb9762003-03-23 01:06:05 +0000407 struct emulated_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000408 struct sigaction act1;
409 int host_sig;
bellard31e31b82003-02-18 22:55:36 +0000410
bellard66fb9762003-03-23 01:06:05 +0000411 if (sig < 1 || sig > TARGET_NSIG)
412 return -EINVAL;
413 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000414#if defined(DEBUG_SIGNAL)
bellard66fb9762003-03-23 01:06:05 +0000415 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
416 sig, (int)act, (int)oact);
417#endif
418 if (oact) {
419 oact->_sa_handler = tswapl(k->sa._sa_handler);
420 oact->sa_flags = tswapl(k->sa.sa_flags);
421 oact->sa_restorer = tswapl(k->sa.sa_restorer);
422 oact->sa_mask = k->sa.sa_mask;
423 }
424 if (act) {
425 k->sa._sa_handler = tswapl(act->_sa_handler);
426 k->sa.sa_flags = tswapl(act->sa_flags);
427 k->sa.sa_restorer = tswapl(act->sa_restorer);
428 k->sa.sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000429
430 /* we update the host linux signal state */
431 host_sig = target_to_host_signal(sig);
432 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
433 sigfillset(&act1.sa_mask);
434 act1.sa_flags = SA_SIGINFO;
435 if (k->sa.sa_flags & TARGET_SA_RESTART)
436 act1.sa_flags |= SA_RESTART;
437 /* NOTE: it is important to update the host kernel signal
438 ignore state to avoid getting unexpected interrupted
439 syscalls */
440 if (k->sa._sa_handler == TARGET_SIG_IGN) {
441 act1.sa_sigaction = (void *)SIG_IGN;
442 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
443 act1.sa_sigaction = (void *)SIG_DFL;
444 } else {
445 act1.sa_sigaction = host_signal_handler;
446 }
447 sigaction(host_sig, &act1, NULL);
448 }
bellard66fb9762003-03-23 01:06:05 +0000449 }
450 return 0;
451}
bellard31e31b82003-02-18 22:55:36 +0000452
bellard43fff232003-07-09 19:31:39 +0000453#define __put_user(x,ptr)\
454({\
455 int size = sizeof(*ptr);\
456 switch(size) {\
457 case 1:\
458 stb(ptr, (typeof(*ptr))(x));\
459 break;\
460 case 2:\
461 stw(ptr, (typeof(*ptr))(x));\
462 break;\
463 case 4:\
464 stl(ptr, (typeof(*ptr))(x));\
465 break;\
466 case 8:\
467 stq(ptr, (typeof(*ptr))(x));\
468 break;\
469 default:\
470 abort();\
471 }\
472 0;\
473})
474
475#define __get_user(x, ptr) \
476({\
477 int size = sizeof(*ptr);\
478 switch(size) {\
479 case 1:\
480 x = (typeof(*ptr))ldub(ptr);\
481 break;\
482 case 2:\
483 x = (typeof(*ptr))lduw(ptr);\
484 break;\
485 case 4:\
486 x = (typeof(*ptr))ldl(ptr);\
487 break;\
488 case 8:\
489 x = (typeof(*ptr))ldq(ptr);\
490 break;\
491 default:\
492 abort();\
493 }\
494 0;\
495})
496
497
498#define __copy_to_user(dst, src, size)\
499({\
500 memcpy(dst, src, size);\
501 0;\
502})
503
504#define __copy_from_user(dst, src, size)\
505({\
506 memcpy(dst, src, size);\
507 0;\
508})
509
510#define __clear_user(dst, size)\
511({\
512 memset(dst, 0, size);\
513 0;\
514})
515
516#ifndef offsetof
517#define offsetof(type, field) ((size_t) &((type *)0)->field)
518#endif
519
520static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
521 const target_siginfo_t *info)
522{
523 tswap_siginfo(tinfo, info);
524 return 0;
525}
526
bellard66fb9762003-03-23 01:06:05 +0000527#ifdef TARGET_I386
528
529/* from the Linux kernel */
530
531struct target_fpreg {
532 uint16_t significand[4];
533 uint16_t exponent;
534};
535
536struct target_fpxreg {
537 uint16_t significand[4];
538 uint16_t exponent;
539 uint16_t padding[3];
540};
541
542struct target_xmmreg {
543 target_ulong element[4];
544};
545
546struct target_fpstate {
547 /* Regular FPU environment */
548 target_ulong cw;
549 target_ulong sw;
550 target_ulong tag;
551 target_ulong ipoff;
552 target_ulong cssel;
553 target_ulong dataoff;
554 target_ulong datasel;
555 struct target_fpreg _st[8];
556 uint16_t status;
557 uint16_t magic; /* 0xffff = regular FPU data only */
558
559 /* FXSR FPU environment */
560 target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
561 target_ulong mxcsr;
562 target_ulong reserved;
563 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
564 struct target_xmmreg _xmm[8];
565 target_ulong padding[56];
566};
567
568#define X86_FXSR_MAGIC 0x0000
569
570struct target_sigcontext {
571 uint16_t gs, __gsh;
572 uint16_t fs, __fsh;
573 uint16_t es, __esh;
574 uint16_t ds, __dsh;
575 target_ulong edi;
576 target_ulong esi;
577 target_ulong ebp;
578 target_ulong esp;
579 target_ulong ebx;
580 target_ulong edx;
581 target_ulong ecx;
582 target_ulong eax;
583 target_ulong trapno;
584 target_ulong err;
585 target_ulong eip;
586 uint16_t cs, __csh;
587 target_ulong eflags;
588 target_ulong esp_at_signal;
589 uint16_t ss, __ssh;
590 target_ulong fpstate; /* pointer */
591 target_ulong oldmask;
592 target_ulong cr2;
593};
594
595typedef struct target_sigaltstack {
596 target_ulong ss_sp;
597 int ss_flags;
598 target_ulong ss_size;
599} target_stack_t;
600
601struct target_ucontext {
602 target_ulong uc_flags;
603 target_ulong uc_link;
604 target_stack_t uc_stack;
605 struct target_sigcontext uc_mcontext;
606 target_sigset_t uc_sigmask; /* mask last for extensibility */
607};
608
609struct sigframe
610{
611 target_ulong pretcode;
612 int sig;
613 struct target_sigcontext sc;
614 struct target_fpstate fpstate;
615 target_ulong extramask[TARGET_NSIG_WORDS-1];
616 char retcode[8];
617};
618
619struct rt_sigframe
620{
621 target_ulong pretcode;
622 int sig;
623 target_ulong pinfo;
624 target_ulong puc;
625 struct target_siginfo info;
626 struct target_ucontext uc;
627 struct target_fpstate fpstate;
628 char retcode[8];
629};
630
631/*
632 * Set up a signal frame.
633 */
634
bellard66fb9762003-03-23 01:06:05 +0000635/* XXX: save x87 state */
636static int
637setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
638 CPUX86State *env, unsigned long mask)
639{
640 int err = 0;
641
bellarda52c7572003-06-21 13:14:12 +0000642 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
643 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
644 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
645 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000646 err |= __put_user(env->regs[R_EDI], &sc->edi);
647 err |= __put_user(env->regs[R_ESI], &sc->esi);
648 err |= __put_user(env->regs[R_EBP], &sc->ebp);
649 err |= __put_user(env->regs[R_ESP], &sc->esp);
650 err |= __put_user(env->regs[R_EBX], &sc->ebx);
651 err |= __put_user(env->regs[R_EDX], &sc->edx);
652 err |= __put_user(env->regs[R_ECX], &sc->ecx);
653 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000654 err |= __put_user(env->exception_index, &sc->trapno);
655 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000656 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000657 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000658 err |= __put_user(env->eflags, &sc->eflags);
659 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000660 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000661
662 cpu_x86_fsave(env, (void *)fpstate, 1);
663 fpstate->status = fpstate->sw;
664 err |= __put_user(0xffff, &fpstate->magic);
665 err |= __put_user(fpstate, &sc->fpstate);
666
bellard66fb9762003-03-23 01:06:05 +0000667 /* non-iBCS2 extensions.. */
668 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000669 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000670 return err;
671}
672
673/*
674 * Determine which stack to use..
675 */
676
677static inline void *
678get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
679{
680 unsigned long esp;
681
682 /* Default to using normal stack */
683 esp = env->regs[R_ESP];
684#if 0
685 /* This is the X/Open sanctioned signal stack switching. */
686 if (ka->sa.sa_flags & SA_ONSTACK) {
687 if (sas_ss_flags(esp) == 0)
688 esp = current->sas_ss_sp + current->sas_ss_size;
689 }
690
691 /* This is the legacy signal stack switching. */
bellarda52c7572003-06-21 13:14:12 +0000692 else
bellard66fb9762003-03-23 01:06:05 +0000693#endif
bellarda52c7572003-06-21 13:14:12 +0000694 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
695 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
696 ka->sa.sa_restorer) {
697 esp = (unsigned long) ka->sa.sa_restorer;
698 }
699 return (void *)((esp - frame_size) & -8ul);
bellard66fb9762003-03-23 01:06:05 +0000700}
701
bellard66fb9762003-03-23 01:06:05 +0000702static void setup_frame(int sig, struct emulated_sigaction *ka,
703 target_sigset_t *set, CPUX86State *env)
704{
705 struct sigframe *frame;
706 int err = 0;
707
708 frame = get_sigframe(ka, env, sizeof(*frame));
709
710#if 0
711 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
712 goto give_sigsegv;
713#endif
714 err |= __put_user((/*current->exec_domain
715 && current->exec_domain->signal_invmap
716 && sig < 32
717 ? current->exec_domain->signal_invmap[sig]
718 : */ sig),
719 &frame->sig);
720 if (err)
721 goto give_sigsegv;
722
723 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
724 if (err)
725 goto give_sigsegv;
726
727 if (TARGET_NSIG_WORDS > 1) {
728 err |= __copy_to_user(frame->extramask, &set->sig[1],
729 sizeof(frame->extramask));
730 }
731 if (err)
732 goto give_sigsegv;
733
734 /* Set up to return from userspace. If provided, use a stub
735 already in userspace. */
736 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
737 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
738 } else {
739 err |= __put_user(frame->retcode, &frame->pretcode);
740 /* This is popl %eax ; movl $,%eax ; int $0x80 */
741 err |= __put_user(0xb858, (short *)(frame->retcode+0));
742 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
743 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
744 }
745
746 if (err)
747 goto give_sigsegv;
748
749 /* Set up registers for signal handler */
750 env->regs[R_ESP] = (unsigned long) frame;
751 env->eip = (unsigned long) ka->sa._sa_handler;
752
753 cpu_x86_load_seg(env, R_DS, __USER_DS);
754 cpu_x86_load_seg(env, R_ES, __USER_DS);
755 cpu_x86_load_seg(env, R_SS, __USER_DS);
756 cpu_x86_load_seg(env, R_CS, __USER_CS);
757 env->eflags &= ~TF_MASK;
758
759 return;
760
761give_sigsegv:
762 if (sig == TARGET_SIGSEGV)
763 ka->sa._sa_handler = TARGET_SIG_DFL;
764 force_sig(TARGET_SIGSEGV /* , current */);
765}
766
bellard9de5e442003-03-23 16:49:39 +0000767static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
768 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000769 target_sigset_t *set, CPUX86State *env)
770{
771 struct rt_sigframe *frame;
772 int err = 0;
773
774 frame = get_sigframe(ka, env, sizeof(*frame));
775
776#if 0
777 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
778 goto give_sigsegv;
779#endif
780
781 err |= __put_user((/*current->exec_domain
782 && current->exec_domain->signal_invmap
783 && sig < 32
784 ? current->exec_domain->signal_invmap[sig]
785 : */sig),
786 &frame->sig);
787 err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
788 err |= __put_user((target_ulong)&frame->uc, &frame->puc);
789 err |= copy_siginfo_to_user(&frame->info, info);
790 if (err)
791 goto give_sigsegv;
792
793 /* Create the ucontext. */
794 err |= __put_user(0, &frame->uc.uc_flags);
795 err |= __put_user(0, &frame->uc.uc_link);
796 err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp);
797 err |= __put_user(/* sas_ss_flags(regs->esp) */ 0,
798 &frame->uc.uc_stack.ss_flags);
799 err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size);
800 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
801 env, set->sig[0]);
802 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
803 if (err)
804 goto give_sigsegv;
805
806 /* Set up to return from userspace. If provided, use a stub
807 already in userspace. */
808 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
809 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
810 } else {
811 err |= __put_user(frame->retcode, &frame->pretcode);
812 /* This is movl $,%eax ; int $0x80 */
813 err |= __put_user(0xb8, (char *)(frame->retcode+0));
814 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
815 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
816 }
817
818 if (err)
819 goto give_sigsegv;
820
821 /* Set up registers for signal handler */
822 env->regs[R_ESP] = (unsigned long) frame;
823 env->eip = (unsigned long) ka->sa._sa_handler;
824
825 cpu_x86_load_seg(env, R_DS, __USER_DS);
826 cpu_x86_load_seg(env, R_ES, __USER_DS);
827 cpu_x86_load_seg(env, R_SS, __USER_DS);
828 cpu_x86_load_seg(env, R_CS, __USER_CS);
829 env->eflags &= ~TF_MASK;
830
831 return;
832
833give_sigsegv:
834 if (sig == TARGET_SIGSEGV)
835 ka->sa._sa_handler = TARGET_SIG_DFL;
836 force_sig(TARGET_SIGSEGV /* , current */);
837}
838
839static int
840restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
841{
842 unsigned int err = 0;
843
bellard66fb9762003-03-23 01:06:05 +0000844 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
845 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
846 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
847 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
848
849 env->regs[R_EDI] = ldl(&sc->edi);
850 env->regs[R_ESI] = ldl(&sc->esi);
851 env->regs[R_EBP] = ldl(&sc->ebp);
852 env->regs[R_ESP] = ldl(&sc->esp);
853 env->regs[R_EBX] = ldl(&sc->ebx);
854 env->regs[R_EDX] = ldl(&sc->edx);
855 env->regs[R_ECX] = ldl(&sc->ecx);
856 env->eip = ldl(&sc->eip);
857
858 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
859 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
860
861 {
862 unsigned int tmpflags;
863 tmpflags = ldl(&sc->eflags);
864 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
865 // regs->orig_eax = -1; /* disable syscall checks */
866 }
867
bellard66fb9762003-03-23 01:06:05 +0000868 {
869 struct _fpstate * buf;
bellarded2dcdf2003-05-29 20:06:27 +0000870 buf = (void *)ldl(&sc->fpstate);
bellard66fb9762003-03-23 01:06:05 +0000871 if (buf) {
bellarded2dcdf2003-05-29 20:06:27 +0000872#if 0
bellard66fb9762003-03-23 01:06:05 +0000873 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
874 goto badframe;
bellarded2dcdf2003-05-29 20:06:27 +0000875#endif
876 cpu_x86_frstor(env, (void *)buf, 1);
bellard66fb9762003-03-23 01:06:05 +0000877 }
878 }
bellarded2dcdf2003-05-29 20:06:27 +0000879
bellard66fb9762003-03-23 01:06:05 +0000880 *peax = ldl(&sc->eax);
881 return err;
882#if 0
883badframe:
884 return 1;
885#endif
886}
887
888long do_sigreturn(CPUX86State *env)
889{
890 struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8);
891 target_sigset_t target_set;
892 sigset_t set;
893 int eax, i;
894
bellard447db212003-05-10 15:10:36 +0000895#if defined(DEBUG_SIGNAL)
896 fprintf(stderr, "do_sigreturn\n");
897#endif
bellard66fb9762003-03-23 01:06:05 +0000898 /* set blocked signals */
899 target_set.sig[0] = frame->sc.oldmask;
900 for(i = 1; i < TARGET_NSIG_WORDS; i++)
901 target_set.sig[i] = frame->extramask[i - 1];
902
903 target_to_host_sigset(&set, &target_set);
904 sigprocmask(SIG_SETMASK, &set, NULL);
905
906 /* restore registers */
907 if (restore_sigcontext(env, &frame->sc, &eax))
908 goto badframe;
909 return eax;
910
911badframe:
912 force_sig(TARGET_SIGSEGV);
913 return 0;
914}
915
916long do_rt_sigreturn(CPUX86State *env)
917{
918 struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
919 target_sigset_t target_set;
920 sigset_t set;
921 // stack_t st;
922 int eax;
923
924#if 0
925 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
926 goto badframe;
927#endif
928 memcpy(&target_set, &frame->uc.uc_sigmask, sizeof(target_sigset_t));
929
930 target_to_host_sigset(&set, &target_set);
931 sigprocmask(SIG_SETMASK, &set, NULL);
932
933 if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax))
934 goto badframe;
935
936#if 0
937 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
938 goto badframe;
939 /* It is more difficult to avoid calling this function than to
940 call it and ignore errors. */
941 do_sigaltstack(&st, NULL, regs->esp);
942#endif
943 return eax;
944
945badframe:
946 force_sig(TARGET_SIGSEGV);
947 return 0;
948}
949
bellard43fff232003-07-09 19:31:39 +0000950#elif defined(TARGET_ARM)
951
952struct target_sigcontext {
953 target_ulong trap_no;
954 target_ulong error_code;
955 target_ulong oldmask;
956 target_ulong arm_r0;
957 target_ulong arm_r1;
958 target_ulong arm_r2;
959 target_ulong arm_r3;
960 target_ulong arm_r4;
961 target_ulong arm_r5;
962 target_ulong arm_r6;
963 target_ulong arm_r7;
964 target_ulong arm_r8;
965 target_ulong arm_r9;
966 target_ulong arm_r10;
967 target_ulong arm_fp;
968 target_ulong arm_ip;
969 target_ulong arm_sp;
970 target_ulong arm_lr;
971 target_ulong arm_pc;
972 target_ulong arm_cpsr;
973 target_ulong fault_address;
974};
975
976typedef struct target_sigaltstack {
977 target_ulong ss_sp;
978 int ss_flags;
979 target_ulong ss_size;
980} target_stack_t;
981
982struct target_ucontext {
983 target_ulong uc_flags;
984 target_ulong uc_link;
985 target_stack_t uc_stack;
986 struct target_sigcontext uc_mcontext;
987 target_sigset_t uc_sigmask; /* mask last for extensibility */
988};
989
990struct sigframe
991{
992 struct target_sigcontext sc;
993 target_ulong extramask[TARGET_NSIG_WORDS-1];
994 target_ulong retcode;
995};
996
997struct rt_sigframe
998{
999 struct target_siginfo *pinfo;
1000 void *puc;
1001 struct target_siginfo info;
1002 struct target_ucontext uc;
1003 target_ulong retcode;
1004};
1005
1006#define TARGET_CONFIG_CPU_32 1
1007
1008/*
1009 * For ARM syscalls, we encode the syscall number into the instruction.
1010 */
1011#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1012#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1013
1014/*
1015 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1016 * need two 16-bit instructions.
1017 */
1018#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1019#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1020
1021static const target_ulong retcodes[4] = {
1022 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1023 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1024};
1025
1026
1027#define __put_user_error(x,p,e) __put_user(x, p)
1028#define __get_user_error(x,p,e) __get_user(x, p)
1029
1030static inline int valid_user_regs(CPUState *regs)
1031{
1032 return 1;
1033}
1034
1035static int
1036setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1037 CPUState *env, unsigned long mask)
1038{
1039 int err = 0;
1040
1041 __put_user_error(env->regs[0], &sc->arm_r0, err);
1042 __put_user_error(env->regs[1], &sc->arm_r1, err);
1043 __put_user_error(env->regs[2], &sc->arm_r2, err);
1044 __put_user_error(env->regs[3], &sc->arm_r3, err);
1045 __put_user_error(env->regs[4], &sc->arm_r4, err);
1046 __put_user_error(env->regs[5], &sc->arm_r5, err);
1047 __put_user_error(env->regs[6], &sc->arm_r6, err);
1048 __put_user_error(env->regs[7], &sc->arm_r7, err);
1049 __put_user_error(env->regs[8], &sc->arm_r8, err);
1050 __put_user_error(env->regs[9], &sc->arm_r9, err);
1051 __put_user_error(env->regs[10], &sc->arm_r10, err);
1052 __put_user_error(env->regs[11], &sc->arm_fp, err);
1053 __put_user_error(env->regs[12], &sc->arm_ip, err);
1054 __put_user_error(env->regs[13], &sc->arm_sp, err);
1055 __put_user_error(env->regs[14], &sc->arm_lr, err);
1056 __put_user_error(env->regs[15], &sc->arm_pc, err);
1057#ifdef TARGET_CONFIG_CPU_32
1058 __put_user_error(env->cpsr, &sc->arm_cpsr, err);
1059#endif
1060
1061 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1062 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1063 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1064 __put_user_error(mask, &sc->oldmask, err);
1065
1066 return err;
1067}
1068
1069static inline void *
1070get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1071{
1072 unsigned long sp = regs->regs[13];
1073
1074#if 0
1075 /*
1076 * This is the X/Open sanctioned signal stack switching.
1077 */
1078 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
1079 sp = current->sas_ss_sp + current->sas_ss_size;
1080#endif
1081 /*
1082 * ATPCS B01 mandates 8-byte alignment
1083 */
1084 return (void *)((sp - framesize) & ~7);
1085}
1086
1087static int
1088setup_return(CPUState *env, struct emulated_sigaction *ka,
1089 target_ulong *rc, void *frame, int usig)
1090{
1091 target_ulong handler = (target_ulong)ka->sa._sa_handler;
1092 target_ulong retcode;
1093 int thumb = 0;
1094#if defined(TARGET_CONFIG_CPU_32)
1095 target_ulong cpsr = env->cpsr;
1096
1097#if 0
1098 /*
1099 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1100 */
1101 if (ka->sa.sa_flags & SA_THIRTYTWO)
1102 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1103
1104#ifdef CONFIG_ARM_THUMB
1105 if (elf_hwcap & HWCAP_THUMB) {
1106 /*
1107 * The LSB of the handler determines if we're going to
1108 * be using THUMB or ARM mode for this signal handler.
1109 */
1110 thumb = handler & 1;
1111
1112 if (thumb)
1113 cpsr |= T_BIT;
1114 else
1115 cpsr &= ~T_BIT;
1116 }
1117#endif
1118#endif
1119#endif /* TARGET_CONFIG_CPU_32 */
1120
1121 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1122 retcode = (target_ulong)ka->sa.sa_restorer;
1123 } else {
1124 unsigned int idx = thumb;
1125
1126 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1127 idx += 2;
1128
1129 if (__put_user(retcodes[idx], rc))
1130 return 1;
1131#if 0
1132 flush_icache_range((target_ulong)rc,
1133 (target_ulong)(rc + 1));
1134#endif
1135 retcode = ((target_ulong)rc) + thumb;
1136 }
1137
1138 env->regs[0] = usig;
1139 env->regs[13] = (target_ulong)frame;
1140 env->regs[14] = retcode;
1141 env->regs[15] = handler & (thumb ? ~1 : ~3);
1142
1143#ifdef TARGET_CONFIG_CPU_32
1144 env->cpsr = cpsr;
1145#endif
1146
1147 return 0;
1148}
1149
1150static void setup_frame(int usig, struct emulated_sigaction *ka,
1151 target_sigset_t *set, CPUState *regs)
1152{
1153 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
1154 int err = 0;
1155
1156 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1157
1158 if (TARGET_NSIG_WORDS > 1) {
1159 err |= __copy_to_user(frame->extramask, &set->sig[1],
1160 sizeof(frame->extramask));
1161 }
1162
1163 if (err == 0)
1164 err = setup_return(regs, ka, &frame->retcode, frame, usig);
1165 // return err;
1166}
1167
1168static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1169 target_siginfo_t *info,
1170 target_sigset_t *set, CPUState *env)
1171{
1172 struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame));
1173 int err = 0;
1174
1175#if 0
1176 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
1177 return 1;
1178#endif
1179 __put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err);
1180 __put_user_error(&frame->uc, (target_ulong *)&frame->puc, err);
1181 err |= copy_siginfo_to_user(&frame->info, info);
1182
1183 /* Clear all the bits of the ucontext we don't use. */
1184 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
1185
1186 err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
1187 env, set->sig[0]);
1188 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
1189
1190 if (err == 0)
1191 err = setup_return(env, ka, &frame->retcode, frame, usig);
1192
1193 if (err == 0) {
1194 /*
1195 * For realtime signals we must also set the second and third
1196 * arguments for the signal handler.
1197 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1198 */
1199 env->regs[1] = (target_ulong)frame->pinfo;
1200 env->regs[2] = (target_ulong)frame->puc;
1201 }
1202
1203 // return err;
1204}
1205
1206static int
1207restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1208{
1209 int err = 0;
1210
1211 __get_user_error(env->regs[0], &sc->arm_r0, err);
1212 __get_user_error(env->regs[1], &sc->arm_r1, err);
1213 __get_user_error(env->regs[2], &sc->arm_r2, err);
1214 __get_user_error(env->regs[3], &sc->arm_r3, err);
1215 __get_user_error(env->regs[4], &sc->arm_r4, err);
1216 __get_user_error(env->regs[5], &sc->arm_r5, err);
1217 __get_user_error(env->regs[6], &sc->arm_r6, err);
1218 __get_user_error(env->regs[7], &sc->arm_r7, err);
1219 __get_user_error(env->regs[8], &sc->arm_r8, err);
1220 __get_user_error(env->regs[9], &sc->arm_r9, err);
1221 __get_user_error(env->regs[10], &sc->arm_r10, err);
1222 __get_user_error(env->regs[11], &sc->arm_fp, err);
1223 __get_user_error(env->regs[12], &sc->arm_ip, err);
1224 __get_user_error(env->regs[13], &sc->arm_sp, err);
1225 __get_user_error(env->regs[14], &sc->arm_lr, err);
1226 __get_user_error(env->regs[15], &sc->arm_pc, err);
1227#ifdef TARGET_CONFIG_CPU_32
1228 __get_user_error(env->cpsr, &sc->arm_cpsr, err);
1229#endif
1230
1231 err |= !valid_user_regs(env);
1232
1233 return err;
1234}
1235
1236long do_sigreturn(CPUState *env)
1237{
1238 struct sigframe *frame;
1239 target_sigset_t set;
1240 sigset_t host_set;
1241
1242 /*
1243 * Since we stacked the signal on a 64-bit boundary,
1244 * then 'sp' should be word aligned here. If it's
1245 * not, then the user is trying to mess with us.
1246 */
1247 if (env->regs[13] & 7)
1248 goto badframe;
1249
1250 frame = (struct sigframe *)env->regs[13];
1251
1252#if 0
1253 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1254 goto badframe;
1255#endif
1256 if (__get_user(set.sig[0], &frame->sc.oldmask)
1257 || (TARGET_NSIG_WORDS > 1
1258 && __copy_from_user(&set.sig[1], &frame->extramask,
1259 sizeof(frame->extramask))))
1260 goto badframe;
1261
1262 target_to_host_sigset(&host_set, &set);
1263 sigprocmask(SIG_SETMASK, &host_set, NULL);
1264
1265 if (restore_sigcontext(env, &frame->sc))
1266 goto badframe;
1267
1268#if 0
1269 /* Send SIGTRAP if we're single-stepping */
1270 if (ptrace_cancel_bpt(current))
1271 send_sig(SIGTRAP, current, 1);
1272#endif
1273 return env->regs[0];
1274
1275badframe:
1276 force_sig(SIGSEGV /* , current */);
1277 return 0;
1278}
1279
1280long do_rt_sigreturn(CPUState *env)
1281{
1282 struct rt_sigframe *frame;
1283 target_sigset_t set;
1284 sigset_t host_set;
1285
1286 /*
1287 * Since we stacked the signal on a 64-bit boundary,
1288 * then 'sp' should be word aligned here. If it's
1289 * not, then the user is trying to mess with us.
1290 */
1291 if (env->regs[13] & 7)
1292 goto badframe;
1293
1294 frame = (struct rt_sigframe *)env->regs[13];
1295
1296#if 0
1297 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1298 goto badframe;
1299#endif
1300 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
1301 goto badframe;
1302
1303 target_to_host_sigset(&host_set, &set);
1304 sigprocmask(SIG_SETMASK, &host_set, NULL);
1305
1306 if (restore_sigcontext(env, &frame->uc.uc_mcontext))
1307 goto badframe;
1308
1309#if 0
1310 /* Send SIGTRAP if we're single-stepping */
1311 if (ptrace_cancel_bpt(current))
1312 send_sig(SIGTRAP, current, 1);
1313#endif
1314 return env->regs[0];
1315
1316badframe:
1317 force_sig(SIGSEGV /* , current */);
1318 return 0;
1319}
1320
bellardb346ff42003-06-15 20:05:50 +00001321#else
1322
1323static void setup_frame(int sig, struct emulated_sigaction *ka,
1324 target_sigset_t *set, CPUState *env)
1325{
1326 fprintf(stderr, "setup_frame: not implemented\n");
1327}
1328
1329static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1330 target_siginfo_t *info,
1331 target_sigset_t *set, CPUState *env)
1332{
1333 fprintf(stderr, "setup_rt_frame: not implemented\n");
1334}
1335
1336long do_sigreturn(CPUState *env)
1337{
1338 fprintf(stderr, "do_sigreturn: not implemented\n");
1339 return -ENOSYS;
1340}
1341
1342long do_rt_sigreturn(CPUState *env)
1343{
1344 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1345 return -ENOSYS;
1346}
1347
bellard66fb9762003-03-23 01:06:05 +00001348#endif
1349
1350void process_pending_signals(void *cpu_env)
1351{
1352 int sig;
1353 target_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00001354 sigset_t set, old_set;
1355 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +00001356 struct emulated_sigaction *k;
1357 struct sigqueue *q;
1358
bellard31e31b82003-02-18 22:55:36 +00001359 if (!signal_pending)
1360 return;
1361
bellard66fb9762003-03-23 01:06:05 +00001362 k = sigact_table;
1363 for(sig = 1; sig <= TARGET_NSIG; sig++) {
1364 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00001365 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00001366 k++;
bellard31e31b82003-02-18 22:55:36 +00001367 }
1368 /* if no signal is pending, just return */
1369 signal_pending = 0;
1370 return;
bellard66fb9762003-03-23 01:06:05 +00001371
bellard31e31b82003-02-18 22:55:36 +00001372 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00001373#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00001374 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00001375#endif
1376 /* dequeue signal */
1377 q = k->first;
1378 k->first = q->next;
1379 if (!k->first)
1380 k->pending = 0;
1381
1382 handler = k->sa._sa_handler;
1383 if (handler == TARGET_SIG_DFL) {
1384 /* default handler : ignore some signal. The other are fatal */
1385 if (sig != TARGET_SIGCHLD &&
1386 sig != TARGET_SIGURG &&
1387 sig != TARGET_SIGWINCH) {
1388 force_sig(sig);
1389 }
1390 } else if (handler == TARGET_SIG_IGN) {
1391 /* ignore sig */
1392 } else if (handler == TARGET_SIG_ERR) {
1393 force_sig(sig);
1394 } else {
bellard9de5e442003-03-23 16:49:39 +00001395 /* compute the blocked signals during the handler execution */
1396 target_to_host_sigset(&set, &k->sa.sa_mask);
1397 /* SA_NODEFER indicates that the current signal should not be
1398 blocked during the handler */
1399 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
1400 sigaddset(&set, target_to_host_signal(sig));
1401
1402 /* block signals in the handler using Linux */
1403 sigprocmask(SIG_BLOCK, &set, &old_set);
1404 /* save the previous blocked signal state to restore it at the
1405 end of the signal execution (see do_sigreturn) */
1406 host_to_target_sigset(&target_old_set, &old_set);
1407
bellardbc8a22c2003-03-30 21:02:40 +00001408 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1409#ifdef TARGET_I386
1410 {
1411 CPUX86State *env = cpu_env;
1412 if (env->eflags & VM_MASK)
1413 save_v86_state(env);
1414 }
1415#endif
bellard9de5e442003-03-23 16:49:39 +00001416 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +00001417 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +00001418 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00001419 else
bellard9de5e442003-03-23 16:49:39 +00001420 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00001421 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
1422 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00001423 }
bellard66fb9762003-03-23 01:06:05 +00001424 if (q != &k->info)
1425 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +00001426}
bellard66fb9762003-03-23 01:06:05 +00001427
1428