]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/signal.c
full system SPARC emulation (Blue Swirl)
[mirror_qemu.git] / linux-user / signal.c
CommitLineData
31e31b8a 1/*
66fb9763 2 * Emulation of Linux signals
31e31b8a
FB
3 *
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>
66fb9763 22#include <string.h>
31e31b8a 23#include <stdarg.h>
2677e107 24#include <unistd.h>
31e31b8a 25#include <signal.h>
66fb9763 26#include <errno.h>
31e31b8a
FB
27#include <sys/ucontext.h>
28
0d330196
FB
29#ifdef __ia64__
30#undef uc_mcontext
31#undef uc_sigmask
32#undef uc_stack
33#undef uc_link
34#endif
35
3ef693a0 36#include "qemu.h"
66fb9763
FB
37
38//#define DEBUG_SIGNAL
39
40#define MAX_SIGQUEUE_SIZE 1024
41
42struct sigqueue {
43 struct sigqueue *next;
9de5e440 44 target_siginfo_t info;
66fb9763 45};
31e31b8a
FB
46
47struct emulated_sigaction {
48 struct target_sigaction sa;
66fb9763
FB
49 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 */
31e31b8a
FB
53};
54
66fb9763
FB
55static 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 */
31e31b8a 59
66fb9763
FB
60static void host_signal_handler(int host_signum, siginfo_t *info,
61 void *puc);
62
9e5f5284
FB
63static 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,
01e3b763 70/* [SIGIOT] = TARGET_SIGIOT,*/
9e5f5284
FB
71 [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
31e31b8a
FB
102static inline int host_to_target_signal(int sig)
103{
9e5f5284 104 return host_to_target_signal_table[sig];
31e31b8a
FB
105}
106
107static inline int target_to_host_signal(int sig)
108{
9e5f5284 109 return target_to_host_signal_table[sig];
31e31b8a
FB
110}
111
9231944d
FB
112static void host_to_target_sigset_internal(target_sigset_t *d,
113 const sigset_t *s)
66fb9763
FB
114{
115 int i;
9e5f5284
FB
116 unsigned long sigmask;
117 uint32_t target_sigmask;
118
119 sigmask = ((unsigned long *)s)[0];
120 target_sigmask = 0;
121 for(i = 0; i < 32; i++) {
122 if (sigmask & (1 << i))
123 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
124 }
125#if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
9231944d 126 d->sig[0] = target_sigmask;
9e5f5284 127 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
9231944d 128 d->sig[i] = ((unsigned long *)s)[i];
66fb9763 129 }
9e5f5284 130#elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
9231944d
FB
131 d->sig[0] = target_sigmask;
132 d->sig[1] = sigmask >> 32;
9e5f5284
FB
133#else
134#error host_to_target_sigset
135#endif
66fb9763
FB
136}
137
9231944d
FB
138void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
139{
140 target_sigset_t d1;
141 int i;
142
143 host_to_target_sigset_internal(&d1, s);
144 for(i = 0;i < TARGET_NSIG_WORDS; i++)
145 __put_user(d1.sig[i], &d->sig[i]);
146}
147
148void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
66fb9763
FB
149{
150 int i;
9e5f5284
FB
151 unsigned long sigmask;
152 target_ulong target_sigmask;
153
9231944d 154 target_sigmask = s->sig[0];
9e5f5284
FB
155 sigmask = 0;
156 for(i = 0; i < 32; i++) {
157 if (target_sigmask & (1 << i))
158 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
159 }
160#if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
161 ((unsigned long *)d)[0] = sigmask;
162 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
9231944d 163 ((unsigned long *)d)[i] = s->sig[i];
66fb9763 164 }
9e5f5284 165#elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
9231944d 166 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
9e5f5284
FB
167#else
168#error target_to_host_sigset
169#endif /* TARGET_LONG_BITS */
66fb9763
FB
170}
171
9231944d
FB
172void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
173{
174 target_sigset_t s1;
175 int i;
176
177 for(i = 0;i < TARGET_NSIG_WORDS; i++)
178 __get_user(s1.sig[i], &s->sig[i]);
179 target_to_host_sigset_internal(d, &s1);
180}
181
66fb9763
FB
182void host_to_target_old_sigset(target_ulong *old_sigset,
183 const sigset_t *sigset)
184{
9e5f5284
FB
185 target_sigset_t d;
186 host_to_target_sigset(&d, sigset);
187 *old_sigset = d.sig[0];
66fb9763
FB
188}
189
190void target_to_host_old_sigset(sigset_t *sigset,
191 const target_ulong *old_sigset)
192{
9e5f5284
FB
193 target_sigset_t d;
194 int i;
195
196 d.sig[0] = *old_sigset;
197 for(i = 1;i < TARGET_NSIG_WORDS; i++)
198 d.sig[i] = 0;
199 target_to_host_sigset(sigset, &d);
66fb9763
FB
200}
201
9de5e440
FB
202/* siginfo conversion */
203
204static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
205 const siginfo_t *info)
66fb9763 206{
9de5e440
FB
207 int sig;
208 sig = host_to_target_signal(info->si_signo);
209 tinfo->si_signo = sig;
210 tinfo->si_errno = 0;
211 tinfo->si_code = 0;
447db213
FB
212 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
213 sig == SIGBUS || sig == SIGTRAP) {
9de5e440
FB
214 /* should never come here, but who knows. The information for
215 the target is irrelevant */
216 tinfo->_sifields._sigfault._addr = 0;
217 } else if (sig >= TARGET_SIGRTMIN) {
218 tinfo->_sifields._rt._pid = info->si_pid;
219 tinfo->_sifields._rt._uid = info->si_uid;
220 /* XXX: potential problem if 64 bit */
221 tinfo->_sifields._rt._sigval.sival_ptr =
222 (target_ulong)info->si_value.sival_ptr;
223 }
224}
225
226static void tswap_siginfo(target_siginfo_t *tinfo,
227 const target_siginfo_t *info)
228{
229 int sig;
230 sig = info->si_signo;
231 tinfo->si_signo = tswap32(sig);
66fb9763
FB
232 tinfo->si_errno = tswap32(info->si_errno);
233 tinfo->si_code = tswap32(info->si_code);
447db213
FB
234 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
235 sig == SIGBUS || sig == SIGTRAP) {
9de5e440
FB
236 tinfo->_sifields._sigfault._addr =
237 tswapl(info->_sifields._sigfault._addr);
238 } else if (sig >= TARGET_SIGRTMIN) {
239 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
240 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
241 tinfo->_sifields._rt._sigval.sival_ptr =
242 tswapl(info->_sifields._rt._sigval.sival_ptr);
243 }
244}
245
246
247void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
248{
249 host_to_target_siginfo_noswap(tinfo, info);
250 tswap_siginfo(tinfo, tinfo);
66fb9763
FB
251}
252
9de5e440
FB
253/* XXX: we support only POSIX RT signals are used. */
254/* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
255void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
66fb9763
FB
256{
257 info->si_signo = tswap32(tinfo->si_signo);
258 info->si_errno = tswap32(tinfo->si_errno);
259 info->si_code = tswap32(tinfo->si_code);
9de5e440
FB
260 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
261 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
262 info->si_value.sival_ptr =
263 (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
66fb9763
FB
264}
265
31e31b8a
FB
266void signal_init(void)
267{
268 struct sigaction act;
9e5f5284 269 int i, j;
31e31b8a 270
9e5f5284
FB
271 /* generate signal conversion tables */
272 for(i = 1; i <= 64; i++) {
273 if (host_to_target_signal_table[i] == 0)
274 host_to_target_signal_table[i] = i;
275 }
276 for(i = 1; i <= 64; i++) {
277 j = host_to_target_signal_table[i];
278 target_to_host_signal_table[j] = i;
279 }
280
9de5e440
FB
281 /* set all host signal handlers. ALL signals are blocked during
282 the handlers to serialize them. */
283 sigfillset(&act.sa_mask);
31e31b8a
FB
284 act.sa_flags = SA_SIGINFO;
285 act.sa_sigaction = host_signal_handler;
286 for(i = 1; i < NSIG; i++) {
c9087c2a 287 sigaction(i, &act, NULL);
31e31b8a
FB
288 }
289
290 memset(sigact_table, 0, sizeof(sigact_table));
66fb9763
FB
291
292 first_free = &sigqueue_table[0];
293 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
294 sigqueue_table[i].next = &sigqueue_table[i + 1];
295 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
296}
297
298/* signal queue handling */
299
300static inline struct sigqueue *alloc_sigqueue(void)
301{
302 struct sigqueue *q = first_free;
303 if (!q)
304 return NULL;
305 first_free = q->next;
306 return q;
31e31b8a
FB
307}
308
66fb9763
FB
309static inline void free_sigqueue(struct sigqueue *q)
310{
311 q->next = first_free;
312 first_free = q;
313}
314
9de5e440
FB
315/* abort execution with signal */
316void __attribute((noreturn)) force_sig(int sig)
66fb9763
FB
317{
318 int host_sig;
66fb9763 319 host_sig = target_to_host_signal(sig);
bc8a22cc 320 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
66fb9763 321 sig, strsignal(host_sig));
9de5e440 322#if 1
66fb9763 323 _exit(-host_sig);
9de5e440
FB
324#else
325 {
326 struct sigaction act;
327 sigemptyset(&act.sa_mask);
328 act.sa_flags = SA_SIGINFO;
329 act.sa_sigaction = SIG_DFL;
330 sigaction(SIGABRT, &act, NULL);
331 abort();
332 }
333#endif
66fb9763
FB
334}
335
9de5e440
FB
336/* queue a signal so that it will be send to the virtual CPU as soon
337 as possible */
338int queue_signal(int sig, target_siginfo_t *info)
31e31b8a 339{
66fb9763 340 struct emulated_sigaction *k;
9de5e440 341 struct sigqueue *q, **pq;
66fb9763
FB
342 target_ulong handler;
343
9de5e440 344#if defined(DEBUG_SIGNAL)
bc8a22cc 345 fprintf(stderr, "queue_signal: sig=%d\n",
9de5e440 346 sig);
66fb9763 347#endif
9de5e440 348 k = &sigact_table[sig - 1];
66fb9763
FB
349 handler = k->sa._sa_handler;
350 if (handler == TARGET_SIG_DFL) {
351 /* default handler : ignore some signal. The other are fatal */
352 if (sig != TARGET_SIGCHLD &&
353 sig != TARGET_SIGURG &&
354 sig != TARGET_SIGWINCH) {
355 force_sig(sig);
9de5e440
FB
356 } else {
357 return 0; /* indicate ignored */
66fb9763
FB
358 }
359 } else if (handler == TARGET_SIG_IGN) {
360 /* ignore signal */
9de5e440 361 return 0;
66fb9763
FB
362 } else if (handler == TARGET_SIG_ERR) {
363 force_sig(sig);
364 } else {
9de5e440
FB
365 pq = &k->first;
366 if (sig < TARGET_SIGRTMIN) {
367 /* if non real time signal, we queue exactly one signal */
368 if (!k->pending)
369 q = &k->info;
370 else
371 return 0;
372 } else {
373 if (!k->pending) {
374 /* first signal */
375 q = &k->info;
376 } else {
377 q = alloc_sigqueue();
378 if (!q)
379 return -EAGAIN;
380 while (*pq != NULL)
381 pq = &(*pq)->next;
382 }
383 }
384 *pq = q;
385 q->info = *info;
386 q->next = NULL;
387 k->pending = 1;
388 /* signal that a new signal is pending */
389 signal_pending = 1;
390 return 1; /* indicates that the signal was queued */
391 }
392}
393
9de5e440
FB
394static void host_signal_handler(int host_signum, siginfo_t *info,
395 void *puc)
396{
397 int sig;
398 target_siginfo_t tinfo;
399
400 /* the CPU emulator uses some host signals to detect exceptions,
401 we we forward to it some signals */
58fe2f10
FB
402 if (host_signum == SIGSEGV || host_signum == SIGBUS
403#if defined(TARGET_I386) && defined(USE_CODE_COPY)
404 || host_signum == SIGFPE
405#endif
406 ) {
b346ff46 407 if (cpu_signal_handler(host_signum, info, puc))
9de5e440
FB
408 return;
409 }
410
411 /* get target signal number */
412 sig = host_to_target_signal(host_signum);
413 if (sig < 1 || sig > TARGET_NSIG)
414 return;
415#if defined(DEBUG_SIGNAL)
bc8a22cc 416 fprintf(stderr, "qemu: got signal %d\n", sig);
9de5e440
FB
417#endif
418 host_to_target_siginfo_noswap(&tinfo, info);
419 if (queue_signal(sig, &tinfo) == 1) {
420 /* interrupt the virtual CPU as soon as possible */
68a79315 421 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
66fb9763
FB
422 }
423}
424
425int do_sigaction(int sig, const struct target_sigaction *act,
426 struct target_sigaction *oact)
427{
428 struct emulated_sigaction *k;
773b93ee
FB
429 struct sigaction act1;
430 int host_sig;
66fb9763
FB
431
432 if (sig < 1 || sig > TARGET_NSIG)
433 return -EINVAL;
434 k = &sigact_table[sig - 1];
773b93ee 435#if defined(DEBUG_SIGNAL)
66fb9763
FB
436 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
437 sig, (int)act, (int)oact);
438#endif
439 if (oact) {
440 oact->_sa_handler = tswapl(k->sa._sa_handler);
441 oact->sa_flags = tswapl(k->sa.sa_flags);
442 oact->sa_restorer = tswapl(k->sa.sa_restorer);
443 oact->sa_mask = k->sa.sa_mask;
444 }
445 if (act) {
446 k->sa._sa_handler = tswapl(act->_sa_handler);
447 k->sa.sa_flags = tswapl(act->sa_flags);
448 k->sa.sa_restorer = tswapl(act->sa_restorer);
449 k->sa.sa_mask = act->sa_mask;
773b93ee
FB
450
451 /* we update the host linux signal state */
452 host_sig = target_to_host_signal(sig);
453 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
454 sigfillset(&act1.sa_mask);
455 act1.sa_flags = SA_SIGINFO;
456 if (k->sa.sa_flags & TARGET_SA_RESTART)
457 act1.sa_flags |= SA_RESTART;
458 /* NOTE: it is important to update the host kernel signal
459 ignore state to avoid getting unexpected interrupted
460 syscalls */
461 if (k->sa._sa_handler == TARGET_SIG_IGN) {
462 act1.sa_sigaction = (void *)SIG_IGN;
463 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
464 act1.sa_sigaction = (void *)SIG_DFL;
465 } else {
466 act1.sa_sigaction = host_signal_handler;
467 }
468 sigaction(host_sig, &act1, NULL);
469 }
66fb9763
FB
470 }
471 return 0;
472}
473
43fff238
FB
474#ifndef offsetof
475#define offsetof(type, field) ((size_t) &((type *)0)->field)
476#endif
477
478static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
479 const target_siginfo_t *info)
480{
481 tswap_siginfo(tinfo, info);
482 return 0;
483}
484
66fb9763
FB
485#ifdef TARGET_I386
486
487/* from the Linux kernel */
488
489struct target_fpreg {
490 uint16_t significand[4];
491 uint16_t exponent;
492};
493
494struct target_fpxreg {
495 uint16_t significand[4];
496 uint16_t exponent;
497 uint16_t padding[3];
498};
499
500struct target_xmmreg {
501 target_ulong element[4];
502};
503
504struct target_fpstate {
505 /* Regular FPU environment */
506 target_ulong cw;
507 target_ulong sw;
508 target_ulong tag;
509 target_ulong ipoff;
510 target_ulong cssel;
511 target_ulong dataoff;
512 target_ulong datasel;
513 struct target_fpreg _st[8];
514 uint16_t status;
515 uint16_t magic; /* 0xffff = regular FPU data only */
516
517 /* FXSR FPU environment */
518 target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
519 target_ulong mxcsr;
520 target_ulong reserved;
521 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
522 struct target_xmmreg _xmm[8];
523 target_ulong padding[56];
524};
525
526#define X86_FXSR_MAGIC 0x0000
527
528struct target_sigcontext {
529 uint16_t gs, __gsh;
530 uint16_t fs, __fsh;
531 uint16_t es, __esh;
532 uint16_t ds, __dsh;
533 target_ulong edi;
534 target_ulong esi;
535 target_ulong ebp;
536 target_ulong esp;
537 target_ulong ebx;
538 target_ulong edx;
539 target_ulong ecx;
540 target_ulong eax;
541 target_ulong trapno;
542 target_ulong err;
543 target_ulong eip;
544 uint16_t cs, __csh;
545 target_ulong eflags;
546 target_ulong esp_at_signal;
547 uint16_t ss, __ssh;
548 target_ulong fpstate; /* pointer */
549 target_ulong oldmask;
550 target_ulong cr2;
551};
552
553typedef struct target_sigaltstack {
554 target_ulong ss_sp;
555 int ss_flags;
556 target_ulong ss_size;
557} target_stack_t;
558
559struct target_ucontext {
560 target_ulong uc_flags;
561 target_ulong uc_link;
562 target_stack_t uc_stack;
563 struct target_sigcontext uc_mcontext;
564 target_sigset_t uc_sigmask; /* mask last for extensibility */
565};
566
567struct sigframe
568{
569 target_ulong pretcode;
570 int sig;
571 struct target_sigcontext sc;
572 struct target_fpstate fpstate;
573 target_ulong extramask[TARGET_NSIG_WORDS-1];
574 char retcode[8];
575};
576
577struct rt_sigframe
578{
579 target_ulong pretcode;
580 int sig;
581 target_ulong pinfo;
582 target_ulong puc;
583 struct target_siginfo info;
584 struct target_ucontext uc;
585 struct target_fpstate fpstate;
586 char retcode[8];
587};
588
589/*
590 * Set up a signal frame.
591 */
592
66fb9763
FB
593/* XXX: save x87 state */
594static int
595setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
596 CPUX86State *env, unsigned long mask)
597{
598 int err = 0;
599
a52c757c
FB
600 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
601 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
602 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
603 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
66fb9763
FB
604 err |= __put_user(env->regs[R_EDI], &sc->edi);
605 err |= __put_user(env->regs[R_ESI], &sc->esi);
606 err |= __put_user(env->regs[R_EBP], &sc->ebp);
607 err |= __put_user(env->regs[R_ESP], &sc->esp);
608 err |= __put_user(env->regs[R_EBX], &sc->ebx);
609 err |= __put_user(env->regs[R_EDX], &sc->edx);
610 err |= __put_user(env->regs[R_ECX], &sc->ecx);
611 err |= __put_user(env->regs[R_EAX], &sc->eax);
66099dd9
FB
612 err |= __put_user(env->exception_index, &sc->trapno);
613 err |= __put_user(env->error_code, &sc->err);
66fb9763 614 err |= __put_user(env->eip, &sc->eip);
a52c757c 615 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
66fb9763
FB
616 err |= __put_user(env->eflags, &sc->eflags);
617 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
a52c757c 618 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
ed2dcdf6
FB
619
620 cpu_x86_fsave(env, (void *)fpstate, 1);
621 fpstate->status = fpstate->sw;
622 err |= __put_user(0xffff, &fpstate->magic);
623 err |= __put_user(fpstate, &sc->fpstate);
624
66fb9763
FB
625 /* non-iBCS2 extensions.. */
626 err |= __put_user(mask, &sc->oldmask);
a52c757c 627 err |= __put_user(env->cr[2], &sc->cr2);
66fb9763 628 return err;
31e31b8a
FB
629}
630
66fb9763
FB
631/*
632 * Determine which stack to use..
633 */
31e31b8a 634
66fb9763
FB
635static inline void *
636get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
31e31b8a 637{
66fb9763
FB
638 unsigned long esp;
639
640 /* Default to using normal stack */
641 esp = env->regs[R_ESP];
642#if 0
643 /* This is the X/Open sanctioned signal stack switching. */
644 if (ka->sa.sa_flags & SA_ONSTACK) {
645 if (sas_ss_flags(esp) == 0)
646 esp = current->sas_ss_sp + current->sas_ss_size;
647 }
648
649 /* This is the legacy signal stack switching. */
a52c757c 650 else
66fb9763 651#endif
a52c757c
FB
652 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
653 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
654 ka->sa.sa_restorer) {
655 esp = (unsigned long) ka->sa.sa_restorer;
656 }
657 return (void *)((esp - frame_size) & -8ul);
66fb9763
FB
658}
659
66fb9763
FB
660static void setup_frame(int sig, struct emulated_sigaction *ka,
661 target_sigset_t *set, CPUX86State *env)
662{
663 struct sigframe *frame;
9231944d 664 int i, err = 0;
66fb9763
FB
665
666 frame = get_sigframe(ka, env, sizeof(*frame));
667
66fb9763
FB
668 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
669 goto give_sigsegv;
66fb9763
FB
670 err |= __put_user((/*current->exec_domain
671 && current->exec_domain->signal_invmap
672 && sig < 32
673 ? current->exec_domain->signal_invmap[sig]
674 : */ sig),
675 &frame->sig);
676 if (err)
677 goto give_sigsegv;
678
679 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
680 if (err)
681 goto give_sigsegv;
682
9231944d
FB
683 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
684 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
685 goto give_sigsegv;
686 }
66fb9763
FB
687
688 /* Set up to return from userspace. If provided, use a stub
689 already in userspace. */
690 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
691 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
692 } else {
693 err |= __put_user(frame->retcode, &frame->pretcode);
694 /* This is popl %eax ; movl $,%eax ; int $0x80 */
695 err |= __put_user(0xb858, (short *)(frame->retcode+0));
696 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
697 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
698 }
699
700 if (err)
701 goto give_sigsegv;
702
703 /* Set up registers for signal handler */
704 env->regs[R_ESP] = (unsigned long) frame;
705 env->eip = (unsigned long) ka->sa._sa_handler;
706
707 cpu_x86_load_seg(env, R_DS, __USER_DS);
708 cpu_x86_load_seg(env, R_ES, __USER_DS);
709 cpu_x86_load_seg(env, R_SS, __USER_DS);
710 cpu_x86_load_seg(env, R_CS, __USER_CS);
711 env->eflags &= ~TF_MASK;
712
713 return;
714
715give_sigsegv:
716 if (sig == TARGET_SIGSEGV)
717 ka->sa._sa_handler = TARGET_SIG_DFL;
718 force_sig(TARGET_SIGSEGV /* , current */);
719}
720
9de5e440
FB
721static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
722 target_siginfo_t *info,
66fb9763
FB
723 target_sigset_t *set, CPUX86State *env)
724{
725 struct rt_sigframe *frame;
9231944d 726 int i, err = 0;
66fb9763
FB
727
728 frame = get_sigframe(ka, env, sizeof(*frame));
729
66fb9763
FB
730 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
731 goto give_sigsegv;
66fb9763
FB
732
733 err |= __put_user((/*current->exec_domain
734 && current->exec_domain->signal_invmap
735 && sig < 32
736 ? current->exec_domain->signal_invmap[sig]
737 : */sig),
738 &frame->sig);
739 err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
740 err |= __put_user((target_ulong)&frame->uc, &frame->puc);
741 err |= copy_siginfo_to_user(&frame->info, info);
742 if (err)
743 goto give_sigsegv;
31e31b8a 744
66fb9763
FB
745 /* Create the ucontext. */
746 err |= __put_user(0, &frame->uc.uc_flags);
747 err |= __put_user(0, &frame->uc.uc_link);
748 err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp);
749 err |= __put_user(/* sas_ss_flags(regs->esp) */ 0,
750 &frame->uc.uc_stack.ss_flags);
751 err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size);
752 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
753 env, set->sig[0]);
9231944d
FB
754 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
755 if (__put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]))
756 goto give_sigsegv;
757 }
31e31b8a 758
66fb9763
FB
759 /* Set up to return from userspace. If provided, use a stub
760 already in userspace. */
761 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
762 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
763 } else {
764 err |= __put_user(frame->retcode, &frame->pretcode);
765 /* This is movl $,%eax ; int $0x80 */
766 err |= __put_user(0xb8, (char *)(frame->retcode+0));
767 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
768 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
769 }
770
771 if (err)
772 goto give_sigsegv;
773
774 /* Set up registers for signal handler */
775 env->regs[R_ESP] = (unsigned long) frame;
776 env->eip = (unsigned long) ka->sa._sa_handler;
777
778 cpu_x86_load_seg(env, R_DS, __USER_DS);
779 cpu_x86_load_seg(env, R_ES, __USER_DS);
780 cpu_x86_load_seg(env, R_SS, __USER_DS);
781 cpu_x86_load_seg(env, R_CS, __USER_CS);
782 env->eflags &= ~TF_MASK;
783
784 return;
785
786give_sigsegv:
787 if (sig == TARGET_SIGSEGV)
788 ka->sa._sa_handler = TARGET_SIG_DFL;
789 force_sig(TARGET_SIGSEGV /* , current */);
790}
791
792static int
793restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
794{
795 unsigned int err = 0;
796
66fb9763
FB
797 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
798 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
799 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
800 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
801
802 env->regs[R_EDI] = ldl(&sc->edi);
803 env->regs[R_ESI] = ldl(&sc->esi);
804 env->regs[R_EBP] = ldl(&sc->ebp);
805 env->regs[R_ESP] = ldl(&sc->esp);
806 env->regs[R_EBX] = ldl(&sc->ebx);
807 env->regs[R_EDX] = ldl(&sc->edx);
808 env->regs[R_ECX] = ldl(&sc->ecx);
809 env->eip = ldl(&sc->eip);
810
811 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
812 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
813
814 {
815 unsigned int tmpflags;
816 tmpflags = ldl(&sc->eflags);
817 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
818 // regs->orig_eax = -1; /* disable syscall checks */
819 }
820
66fb9763
FB
821 {
822 struct _fpstate * buf;
ed2dcdf6 823 buf = (void *)ldl(&sc->fpstate);
66fb9763 824 if (buf) {
ed2dcdf6 825#if 0
66fb9763
FB
826 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
827 goto badframe;
ed2dcdf6
FB
828#endif
829 cpu_x86_frstor(env, (void *)buf, 1);
66fb9763
FB
830 }
831 }
ed2dcdf6 832
66fb9763
FB
833 *peax = ldl(&sc->eax);
834 return err;
835#if 0
836badframe:
837 return 1;
838#endif
839}
840
841long do_sigreturn(CPUX86State *env)
842{
843 struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8);
844 target_sigset_t target_set;
845 sigset_t set;
846 int eax, i;
847
447db213
FB
848#if defined(DEBUG_SIGNAL)
849 fprintf(stderr, "do_sigreturn\n");
850#endif
66fb9763 851 /* set blocked signals */
9231944d
FB
852 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
853 goto badframe;
854 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
855 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
856 goto badframe;
857 }
66fb9763 858
9231944d 859 target_to_host_sigset_internal(&set, &target_set);
66fb9763
FB
860 sigprocmask(SIG_SETMASK, &set, NULL);
861
862 /* restore registers */
863 if (restore_sigcontext(env, &frame->sc, &eax))
864 goto badframe;
865 return eax;
866
867badframe:
868 force_sig(TARGET_SIGSEGV);
869 return 0;
870}
871
872long do_rt_sigreturn(CPUX86State *env)
873{
874 struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
66fb9763
FB
875 sigset_t set;
876 // stack_t st;
877 int eax;
878
879#if 0
880 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
881 goto badframe;
882#endif
9231944d 883 target_to_host_sigset(&set, &frame->uc.uc_sigmask);
66fb9763
FB
884 sigprocmask(SIG_SETMASK, &set, NULL);
885
886 if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax))
887 goto badframe;
888
889#if 0
890 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
891 goto badframe;
892 /* It is more difficult to avoid calling this function than to
893 call it and ignore errors. */
894 do_sigaltstack(&st, NULL, regs->esp);
895#endif
896 return eax;
897
898badframe:
899 force_sig(TARGET_SIGSEGV);
900 return 0;
901}
902
43fff238
FB
903#elif defined(TARGET_ARM)
904
905struct target_sigcontext {
906 target_ulong trap_no;
907 target_ulong error_code;
908 target_ulong oldmask;
909 target_ulong arm_r0;
910 target_ulong arm_r1;
911 target_ulong arm_r2;
912 target_ulong arm_r3;
913 target_ulong arm_r4;
914 target_ulong arm_r5;
915 target_ulong arm_r6;
916 target_ulong arm_r7;
917 target_ulong arm_r8;
918 target_ulong arm_r9;
919 target_ulong arm_r10;
920 target_ulong arm_fp;
921 target_ulong arm_ip;
922 target_ulong arm_sp;
923 target_ulong arm_lr;
924 target_ulong arm_pc;
925 target_ulong arm_cpsr;
926 target_ulong fault_address;
927};
928
929typedef struct target_sigaltstack {
930 target_ulong ss_sp;
931 int ss_flags;
932 target_ulong ss_size;
933} target_stack_t;
934
935struct target_ucontext {
936 target_ulong uc_flags;
937 target_ulong uc_link;
938 target_stack_t uc_stack;
939 struct target_sigcontext uc_mcontext;
940 target_sigset_t uc_sigmask; /* mask last for extensibility */
941};
942
943struct sigframe
944{
945 struct target_sigcontext sc;
946 target_ulong extramask[TARGET_NSIG_WORDS-1];
947 target_ulong retcode;
948};
949
950struct rt_sigframe
951{
952 struct target_siginfo *pinfo;
953 void *puc;
954 struct target_siginfo info;
955 struct target_ucontext uc;
956 target_ulong retcode;
957};
958
959#define TARGET_CONFIG_CPU_32 1
960
961/*
962 * For ARM syscalls, we encode the syscall number into the instruction.
963 */
964#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
965#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
966
967/*
968 * For Thumb syscalls, we pass the syscall number via r7. We therefore
969 * need two 16-bit instructions.
970 */
971#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
972#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
973
974static const target_ulong retcodes[4] = {
975 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
976 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
977};
978
979
980#define __put_user_error(x,p,e) __put_user(x, p)
981#define __get_user_error(x,p,e) __get_user(x, p)
982
983static inline int valid_user_regs(CPUState *regs)
984{
985 return 1;
986}
987
988static int
989setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
990 CPUState *env, unsigned long mask)
991{
992 int err = 0;
993
994 __put_user_error(env->regs[0], &sc->arm_r0, err);
995 __put_user_error(env->regs[1], &sc->arm_r1, err);
996 __put_user_error(env->regs[2], &sc->arm_r2, err);
997 __put_user_error(env->regs[3], &sc->arm_r3, err);
998 __put_user_error(env->regs[4], &sc->arm_r4, err);
999 __put_user_error(env->regs[5], &sc->arm_r5, err);
1000 __put_user_error(env->regs[6], &sc->arm_r6, err);
1001 __put_user_error(env->regs[7], &sc->arm_r7, err);
1002 __put_user_error(env->regs[8], &sc->arm_r8, err);
1003 __put_user_error(env->regs[9], &sc->arm_r9, err);
1004 __put_user_error(env->regs[10], &sc->arm_r10, err);
1005 __put_user_error(env->regs[11], &sc->arm_fp, err);
1006 __put_user_error(env->regs[12], &sc->arm_ip, err);
1007 __put_user_error(env->regs[13], &sc->arm_sp, err);
1008 __put_user_error(env->regs[14], &sc->arm_lr, err);
1009 __put_user_error(env->regs[15], &sc->arm_pc, err);
1010#ifdef TARGET_CONFIG_CPU_32
1011 __put_user_error(env->cpsr, &sc->arm_cpsr, err);
1012#endif
1013
1014 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1015 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1016 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1017 __put_user_error(mask, &sc->oldmask, err);
1018
1019 return err;
1020}
1021
1022static inline void *
1023get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1024{
1025 unsigned long sp = regs->regs[13];
1026
1027#if 0
1028 /*
1029 * This is the X/Open sanctioned signal stack switching.
1030 */
1031 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
1032 sp = current->sas_ss_sp + current->sas_ss_size;
1033#endif
1034 /*
1035 * ATPCS B01 mandates 8-byte alignment
1036 */
1037 return (void *)((sp - framesize) & ~7);
1038}
1039
1040static int
1041setup_return(CPUState *env, struct emulated_sigaction *ka,
1042 target_ulong *rc, void *frame, int usig)
1043{
1044 target_ulong handler = (target_ulong)ka->sa._sa_handler;
1045 target_ulong retcode;
1046 int thumb = 0;
1047#if defined(TARGET_CONFIG_CPU_32)
1048 target_ulong cpsr = env->cpsr;
1049
1050#if 0
1051 /*
1052 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1053 */
1054 if (ka->sa.sa_flags & SA_THIRTYTWO)
1055 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1056
1057#ifdef CONFIG_ARM_THUMB
1058 if (elf_hwcap & HWCAP_THUMB) {
1059 /*
1060 * The LSB of the handler determines if we're going to
1061 * be using THUMB or ARM mode for this signal handler.
1062 */
1063 thumb = handler & 1;
1064
1065 if (thumb)
1066 cpsr |= T_BIT;
1067 else
1068 cpsr &= ~T_BIT;
1069 }
1070#endif
1071#endif
1072#endif /* TARGET_CONFIG_CPU_32 */
1073
1074 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1075 retcode = (target_ulong)ka->sa.sa_restorer;
1076 } else {
1077 unsigned int idx = thumb;
1078
1079 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1080 idx += 2;
1081
1082 if (__put_user(retcodes[idx], rc))
1083 return 1;
1084#if 0
1085 flush_icache_range((target_ulong)rc,
1086 (target_ulong)(rc + 1));
1087#endif
1088 retcode = ((target_ulong)rc) + thumb;
1089 }
1090
1091 env->regs[0] = usig;
1092 env->regs[13] = (target_ulong)frame;
1093 env->regs[14] = retcode;
1094 env->regs[15] = handler & (thumb ? ~1 : ~3);
1095
1096#ifdef TARGET_CONFIG_CPU_32
1097 env->cpsr = cpsr;
1098#endif
1099
1100 return 0;
1101}
1102
1103static void setup_frame(int usig, struct emulated_sigaction *ka,
1104 target_sigset_t *set, CPUState *regs)
1105{
1106 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
9231944d 1107 int i, err = 0;
43fff238
FB
1108
1109 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1110
9231944d
FB
1111 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1112 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1113 return;
43fff238
FB
1114 }
1115
1116 if (err == 0)
1117 err = setup_return(regs, ka, &frame->retcode, frame, usig);
1118 // return err;
1119}
1120
1121static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1122 target_siginfo_t *info,
1123 target_sigset_t *set, CPUState *env)
1124{
1125 struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame));
9231944d 1126 int i, err = 0;
43fff238 1127
43fff238 1128 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
edf779ff
FB
1129 return /* 1 */;
1130
43fff238
FB
1131 __put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err);
1132 __put_user_error(&frame->uc, (target_ulong *)&frame->puc, err);
1133 err |= copy_siginfo_to_user(&frame->info, info);
1134
1135 /* Clear all the bits of the ucontext we don't use. */
1136 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
1137
1138 err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
1139 env, set->sig[0]);
9231944d
FB
1140 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1141 if (__put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]))
1142 return;
1143 }
43fff238
FB
1144
1145 if (err == 0)
1146 err = setup_return(env, ka, &frame->retcode, frame, usig);
1147
1148 if (err == 0) {
1149 /*
1150 * For realtime signals we must also set the second and third
1151 * arguments for the signal handler.
1152 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1153 */
1154 env->regs[1] = (target_ulong)frame->pinfo;
1155 env->regs[2] = (target_ulong)frame->puc;
1156 }
1157
1158 // return err;
1159}
1160
1161static int
1162restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1163{
1164 int err = 0;
1165
1166 __get_user_error(env->regs[0], &sc->arm_r0, err);
1167 __get_user_error(env->regs[1], &sc->arm_r1, err);
1168 __get_user_error(env->regs[2], &sc->arm_r2, err);
1169 __get_user_error(env->regs[3], &sc->arm_r3, err);
1170 __get_user_error(env->regs[4], &sc->arm_r4, err);
1171 __get_user_error(env->regs[5], &sc->arm_r5, err);
1172 __get_user_error(env->regs[6], &sc->arm_r6, err);
1173 __get_user_error(env->regs[7], &sc->arm_r7, err);
1174 __get_user_error(env->regs[8], &sc->arm_r8, err);
1175 __get_user_error(env->regs[9], &sc->arm_r9, err);
1176 __get_user_error(env->regs[10], &sc->arm_r10, err);
1177 __get_user_error(env->regs[11], &sc->arm_fp, err);
1178 __get_user_error(env->regs[12], &sc->arm_ip, err);
1179 __get_user_error(env->regs[13], &sc->arm_sp, err);
1180 __get_user_error(env->regs[14], &sc->arm_lr, err);
1181 __get_user_error(env->regs[15], &sc->arm_pc, err);
1182#ifdef TARGET_CONFIG_CPU_32
1183 __get_user_error(env->cpsr, &sc->arm_cpsr, err);
1184#endif
1185
1186 err |= !valid_user_regs(env);
1187
1188 return err;
1189}
1190
1191long do_sigreturn(CPUState *env)
1192{
1193 struct sigframe *frame;
1194 target_sigset_t set;
1195 sigset_t host_set;
9231944d 1196 int i;
43fff238
FB
1197
1198 /*
1199 * Since we stacked the signal on a 64-bit boundary,
1200 * then 'sp' should be word aligned here. If it's
1201 * not, then the user is trying to mess with us.
1202 */
1203 if (env->regs[13] & 7)
1204 goto badframe;
1205
1206 frame = (struct sigframe *)env->regs[13];
1207
1208#if 0
1209 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1210 goto badframe;
1211#endif
9231944d
FB
1212 if (__get_user(set.sig[0], &frame->sc.oldmask))
1213 goto badframe;
1214 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1215 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1216 goto badframe;
1217 }
43fff238 1218
9231944d 1219 target_to_host_sigset_internal(&host_set, &set);
43fff238
FB
1220 sigprocmask(SIG_SETMASK, &host_set, NULL);
1221
1222 if (restore_sigcontext(env, &frame->sc))
1223 goto badframe;
1224
1225#if 0
1226 /* Send SIGTRAP if we're single-stepping */
1227 if (ptrace_cancel_bpt(current))
1228 send_sig(SIGTRAP, current, 1);
1229#endif
1230 return env->regs[0];
1231
1232badframe:
1233 force_sig(SIGSEGV /* , current */);
1234 return 0;
1235}
1236
1237long do_rt_sigreturn(CPUState *env)
1238{
1239 struct rt_sigframe *frame;
43fff238
FB
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 rt_sigframe *)env->regs[13];
1251
1252#if 0
1253 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1254 goto badframe;
1255#endif
9231944d 1256 target_to_host_sigset(&host_set, &frame->uc.uc_sigmask);
43fff238
FB
1257 sigprocmask(SIG_SETMASK, &host_set, NULL);
1258
1259 if (restore_sigcontext(env, &frame->uc.uc_mcontext))
1260 goto badframe;
1261
1262#if 0
1263 /* Send SIGTRAP if we're single-stepping */
1264 if (ptrace_cancel_bpt(current))
1265 send_sig(SIGTRAP, current, 1);
1266#endif
1267 return env->regs[0];
1268
1269badframe:
1270 force_sig(SIGSEGV /* , current */);
1271 return 0;
1272}
1273
b346ff46
FB
1274#else
1275
1276static void setup_frame(int sig, struct emulated_sigaction *ka,
1277 target_sigset_t *set, CPUState *env)
1278{
1279 fprintf(stderr, "setup_frame: not implemented\n");
1280}
1281
1282static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1283 target_siginfo_t *info,
1284 target_sigset_t *set, CPUState *env)
1285{
1286 fprintf(stderr, "setup_rt_frame: not implemented\n");
1287}
1288
1289long do_sigreturn(CPUState *env)
1290{
1291 fprintf(stderr, "do_sigreturn: not implemented\n");
1292 return -ENOSYS;
1293}
1294
1295long do_rt_sigreturn(CPUState *env)
1296{
1297 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1298 return -ENOSYS;
1299}
1300
66fb9763
FB
1301#endif
1302
1303void process_pending_signals(void *cpu_env)
1304{
1305 int sig;
1306 target_ulong handler;
9de5e440
FB
1307 sigset_t set, old_set;
1308 target_sigset_t target_old_set;
66fb9763
FB
1309 struct emulated_sigaction *k;
1310 struct sigqueue *q;
1311
31e31b8a
FB
1312 if (!signal_pending)
1313 return;
1314
66fb9763
FB
1315 k = sigact_table;
1316 for(sig = 1; sig <= TARGET_NSIG; sig++) {
1317 if (k->pending)
31e31b8a 1318 goto handle_signal;
66fb9763 1319 k++;
31e31b8a
FB
1320 }
1321 /* if no signal is pending, just return */
1322 signal_pending = 0;
1323 return;
66fb9763 1324
31e31b8a 1325 handle_signal:
66fb9763 1326#ifdef DEBUG_SIGNAL
bc8a22cc 1327 fprintf(stderr, "qemu: process signal %d\n", sig);
66fb9763
FB
1328#endif
1329 /* dequeue signal */
1330 q = k->first;
1331 k->first = q->next;
1332 if (!k->first)
1333 k->pending = 0;
1334
1335 handler = k->sa._sa_handler;
1336 if (handler == TARGET_SIG_DFL) {
1337 /* default handler : ignore some signal. The other are fatal */
1338 if (sig != TARGET_SIGCHLD &&
1339 sig != TARGET_SIGURG &&
1340 sig != TARGET_SIGWINCH) {
1341 force_sig(sig);
1342 }
1343 } else if (handler == TARGET_SIG_IGN) {
1344 /* ignore sig */
1345 } else if (handler == TARGET_SIG_ERR) {
1346 force_sig(sig);
1347 } else {
9de5e440
FB
1348 /* compute the blocked signals during the handler execution */
1349 target_to_host_sigset(&set, &k->sa.sa_mask);
1350 /* SA_NODEFER indicates that the current signal should not be
1351 blocked during the handler */
1352 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
1353 sigaddset(&set, target_to_host_signal(sig));
1354
1355 /* block signals in the handler using Linux */
1356 sigprocmask(SIG_BLOCK, &set, &old_set);
1357 /* save the previous blocked signal state to restore it at the
1358 end of the signal execution (see do_sigreturn) */
9231944d 1359 host_to_target_sigset_internal(&target_old_set, &old_set);
9de5e440 1360
bc8a22cc
FB
1361 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1362#ifdef TARGET_I386
1363 {
1364 CPUX86State *env = cpu_env;
1365 if (env->eflags & VM_MASK)
1366 save_v86_state(env);
1367 }
1368#endif
9de5e440 1369 /* prepare the stack frame of the virtual CPU */
66fb9763 1370 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
9de5e440 1371 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
66fb9763 1372 else
9de5e440 1373 setup_frame(sig, k, &target_old_set, cpu_env);
66fb9763
FB
1374 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
1375 k->sa._sa_handler = TARGET_SIG_DFL;
31e31b8a 1376 }
66fb9763
FB
1377 if (q != &k->info)
1378 free_sigqueue(q);
1379}
31e31b8a
FB
1380
1381