xref: /qemu/linux-user/signal.c (revision 540635ba650bd2529ce55c4135cd594a5fa109b5)
1 /*
2  *  Emulation of Linux signals
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>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <errno.h>
27 #include <sys/ucontext.h>
28 
29 #include "target_signal.h"
30 #include "qemu.h"
31 
32 //#define DEBUG_SIGNAL
33 
34 #define MAX_SIGQUEUE_SIZE 1024
35 
36 struct sigqueue {
37     struct sigqueue *next;
38     target_siginfo_t info;
39 };
40 
41 struct emulated_sigaction {
42     struct target_sigaction sa;
43     int pending; /* true if signal is pending */
44     struct sigqueue *first;
45     struct sigqueue info; /* in order to always have memory for the
46                              first signal, we put it here */
47 };
48 
49 struct target_sigaltstack target_sigaltstack_used = {
50     .ss_sp = 0,
51     .ss_size = 0,
52     .ss_flags = TARGET_SS_DISABLE,
53 };
54 
55 static struct emulated_sigaction sigact_table[TARGET_NSIG];
56 static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57 static struct sigqueue *first_free; /* first free siginfo queue entry */
58 static int signal_pending; /* non zero if a signal may be pending */
59 
60 static void host_signal_handler(int host_signum, siginfo_t *info,
61                                 void *puc);
62 
63 static 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,
70 /*    [SIGIOT] = TARGET_SIGIOT,*/
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 };
100 static uint8_t target_to_host_signal_table[65];
101 
102 static inline int on_sig_stack(unsigned long sp)
103 {
104     return (sp - target_sigaltstack_used.ss_sp
105             < target_sigaltstack_used.ss_size);
106 }
107 
108 static inline int sas_ss_flags(unsigned long sp)
109 {
110     return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
111             : on_sig_stack(sp) ? SS_ONSTACK : 0);
112 }
113 
114 static inline int host_to_target_signal(int sig)
115 {
116     return host_to_target_signal_table[sig];
117 }
118 
119 static inline int target_to_host_signal(int sig)
120 {
121     return target_to_host_signal_table[sig];
122 }
123 
124 static void host_to_target_sigset_internal(target_sigset_t *d,
125                                            const sigset_t *s)
126 {
127     int i;
128     unsigned long sigmask;
129     uint32_t target_sigmask;
130 
131     sigmask = ((unsigned long *)s)[0];
132     target_sigmask = 0;
133     for(i = 0; i < 32; i++) {
134         if (sigmask & (1 << i))
135             target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
136     }
137 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
138     d->sig[0] = target_sigmask;
139     for(i = 1;i < TARGET_NSIG_WORDS; i++) {
140         d->sig[i] = ((unsigned long *)s)[i];
141     }
142 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
143     d->sig[0] = target_sigmask;
144     d->sig[1] = sigmask >> 32;
145 #else
146 #warning host_to_target_sigset
147 #endif
148 }
149 
150 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
151 {
152     target_sigset_t d1;
153     int i;
154 
155     host_to_target_sigset_internal(&d1, s);
156     for(i = 0;i < TARGET_NSIG_WORDS; i++)
157         d->sig[i] = tswapl(d1.sig[i]);
158 }
159 
160 void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
161 {
162     int i;
163     unsigned long sigmask;
164     target_ulong target_sigmask;
165 
166     target_sigmask = s->sig[0];
167     sigmask = 0;
168     for(i = 0; i < 32; i++) {
169         if (target_sigmask & (1 << i))
170             sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
171     }
172 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
173     ((unsigned long *)d)[0] = sigmask;
174     for(i = 1;i < TARGET_NSIG_WORDS; i++) {
175         ((unsigned long *)d)[i] = s->sig[i];
176     }
177 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
178     ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
179 #else
180 #warning target_to_host_sigset
181 #endif /* TARGET_LONG_BITS */
182 }
183 
184 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
185 {
186     target_sigset_t s1;
187     int i;
188 
189     for(i = 0;i < TARGET_NSIG_WORDS; i++)
190         s1.sig[i] = tswapl(s->sig[i]);
191     target_to_host_sigset_internal(d, &s1);
192 }
193 
194 void host_to_target_old_sigset(target_ulong *old_sigset,
195                                const sigset_t *sigset)
196 {
197     target_sigset_t d;
198     host_to_target_sigset(&d, sigset);
199     *old_sigset = d.sig[0];
200 }
201 
202 void target_to_host_old_sigset(sigset_t *sigset,
203                                const target_ulong *old_sigset)
204 {
205     target_sigset_t d;
206     int i;
207 
208     d.sig[0] = *old_sigset;
209     for(i = 1;i < TARGET_NSIG_WORDS; i++)
210         d.sig[i] = 0;
211     target_to_host_sigset(sigset, &d);
212 }
213 
214 /* siginfo conversion */
215 
216 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
217                                                  const siginfo_t *info)
218 {
219     int sig;
220     sig = host_to_target_signal(info->si_signo);
221     tinfo->si_signo = sig;
222     tinfo->si_errno = 0;
223     tinfo->si_code = 0;
224     if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
225         sig == SIGBUS || sig == SIGTRAP) {
226         /* should never come here, but who knows. The information for
227            the target is irrelevant */
228         tinfo->_sifields._sigfault._addr = 0;
229     } else if (sig == SIGIO) {
230 	tinfo->_sifields._sigpoll._fd = info->si_fd;
231     } else if (sig >= TARGET_SIGRTMIN) {
232         tinfo->_sifields._rt._pid = info->si_pid;
233         tinfo->_sifields._rt._uid = info->si_uid;
234         /* XXX: potential problem if 64 bit */
235         tinfo->_sifields._rt._sigval.sival_ptr =
236             (target_ulong)info->si_value.sival_ptr;
237     }
238 }
239 
240 static void tswap_siginfo(target_siginfo_t *tinfo,
241                           const target_siginfo_t *info)
242 {
243     int sig;
244     sig = info->si_signo;
245     tinfo->si_signo = tswap32(sig);
246     tinfo->si_errno = tswap32(info->si_errno);
247     tinfo->si_code = tswap32(info->si_code);
248     if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
249         sig == SIGBUS || sig == SIGTRAP) {
250         tinfo->_sifields._sigfault._addr =
251             tswapl(info->_sifields._sigfault._addr);
252     } else if (sig == SIGIO) {
253 	tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
254     } else if (sig >= TARGET_SIGRTMIN) {
255         tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
256         tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
257         tinfo->_sifields._rt._sigval.sival_ptr =
258             tswapl(info->_sifields._rt._sigval.sival_ptr);
259     }
260 }
261 
262 
263 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
264 {
265     host_to_target_siginfo_noswap(tinfo, info);
266     tswap_siginfo(tinfo, tinfo);
267 }
268 
269 /* XXX: we support only POSIX RT signals are used. */
270 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
271 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
272 {
273     info->si_signo = tswap32(tinfo->si_signo);
274     info->si_errno = tswap32(tinfo->si_errno);
275     info->si_code = tswap32(tinfo->si_code);
276     info->si_pid = tswap32(tinfo->_sifields._rt._pid);
277     info->si_uid = tswap32(tinfo->_sifields._rt._uid);
278     info->si_value.sival_ptr =
279         (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
280 }
281 
282 void signal_init(void)
283 {
284     struct sigaction act;
285     int i, j;
286 
287     /* generate signal conversion tables */
288     for(i = 1; i <= 64; i++) {
289         if (host_to_target_signal_table[i] == 0)
290             host_to_target_signal_table[i] = i;
291     }
292     for(i = 1; i <= 64; i++) {
293         j = host_to_target_signal_table[i];
294         target_to_host_signal_table[j] = i;
295     }
296 
297     /* set all host signal handlers. ALL signals are blocked during
298        the handlers to serialize them. */
299     sigfillset(&act.sa_mask);
300     act.sa_flags = SA_SIGINFO;
301     act.sa_sigaction = host_signal_handler;
302     for(i = 1; i < NSIG; i++) {
303         sigaction(i, &act, NULL);
304     }
305 
306     memset(sigact_table, 0, sizeof(sigact_table));
307 
308     first_free = &sigqueue_table[0];
309     for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
310         sigqueue_table[i].next = &sigqueue_table[i + 1];
311     sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
312 }
313 
314 /* signal queue handling */
315 
316 static inline struct sigqueue *alloc_sigqueue(void)
317 {
318     struct sigqueue *q = first_free;
319     if (!q)
320         return NULL;
321     first_free = q->next;
322     return q;
323 }
324 
325 static inline void free_sigqueue(struct sigqueue *q)
326 {
327     q->next = first_free;
328     first_free = q;
329 }
330 
331 /* abort execution with signal */
332 void __attribute((noreturn)) force_sig(int sig)
333 {
334     int host_sig;
335     host_sig = target_to_host_signal(sig);
336     fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
337             sig, strsignal(host_sig));
338 #if 1
339     _exit(-host_sig);
340 #else
341     {
342         struct sigaction act;
343         sigemptyset(&act.sa_mask);
344         act.sa_flags = SA_SIGINFO;
345         act.sa_sigaction = SIG_DFL;
346         sigaction(SIGABRT, &act, NULL);
347         abort();
348     }
349 #endif
350 }
351 
352 /* queue a signal so that it will be send to the virtual CPU as soon
353    as possible */
354 int queue_signal(int sig, target_siginfo_t *info)
355 {
356     struct emulated_sigaction *k;
357     struct sigqueue *q, **pq;
358     target_ulong handler;
359 
360 #if defined(DEBUG_SIGNAL)
361     fprintf(stderr, "queue_signal: sig=%d\n",
362             sig);
363 #endif
364     k = &sigact_table[sig - 1];
365     handler = k->sa._sa_handler;
366     if (handler == TARGET_SIG_DFL) {
367         /* default handler : ignore some signal. The other are fatal */
368         if (sig != TARGET_SIGCHLD &&
369             sig != TARGET_SIGURG &&
370             sig != TARGET_SIGWINCH) {
371             force_sig(sig);
372         } else {
373             return 0; /* indicate ignored */
374         }
375     } else if (handler == TARGET_SIG_IGN) {
376         /* ignore signal */
377         return 0;
378     } else if (handler == TARGET_SIG_ERR) {
379         force_sig(sig);
380     } else {
381         pq = &k->first;
382         if (sig < TARGET_SIGRTMIN) {
383             /* if non real time signal, we queue exactly one signal */
384             if (!k->pending)
385                 q = &k->info;
386             else
387                 return 0;
388         } else {
389             if (!k->pending) {
390                 /* first signal */
391                 q = &k->info;
392             } else {
393                 q = alloc_sigqueue();
394                 if (!q)
395                     return -EAGAIN;
396                 while (*pq != NULL)
397                     pq = &(*pq)->next;
398             }
399         }
400         *pq = q;
401         q->info = *info;
402         q->next = NULL;
403         k->pending = 1;
404         /* signal that a new signal is pending */
405         signal_pending = 1;
406         return 1; /* indicates that the signal was queued */
407     }
408 }
409 
410 static void host_signal_handler(int host_signum, siginfo_t *info,
411                                 void *puc)
412 {
413     int sig;
414     target_siginfo_t tinfo;
415 
416     /* the CPU emulator uses some host signals to detect exceptions,
417        we we forward to it some signals */
418     if (host_signum == SIGSEGV || host_signum == SIGBUS
419 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
420         || host_signum == SIGFPE
421 #endif
422         ) {
423         if (cpu_signal_handler(host_signum, info, puc))
424             return;
425     }
426 
427     /* get target signal number */
428     sig = host_to_target_signal(host_signum);
429     if (sig < 1 || sig > TARGET_NSIG)
430         return;
431 #if defined(DEBUG_SIGNAL)
432     fprintf(stderr, "qemu: got signal %d\n", sig);
433 #endif
434     host_to_target_siginfo_noswap(&tinfo, info);
435     if (queue_signal(sig, &tinfo) == 1) {
436         /* interrupt the virtual CPU as soon as possible */
437         cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
438     }
439 }
440 
441 int do_sigaltstack(const struct target_sigaltstack *uss,
442                    struct target_sigaltstack *uoss,
443                    target_ulong sp)
444 {
445     int ret;
446     struct target_sigaltstack oss;
447 
448     /* XXX: test errors */
449     if(uoss)
450     {
451         __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
452         __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
453         __put_user(sas_ss_flags(sp), &oss.ss_flags);
454     }
455 
456     if(uss)
457     {
458 	struct target_sigaltstack ss;
459 
460 	ret = -EFAULT;
461 	if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
462 	    || __get_user(ss.ss_sp, &uss->ss_sp)
463 	    || __get_user(ss.ss_size, &uss->ss_size)
464 	    || __get_user(ss.ss_flags, &uss->ss_flags))
465             goto out;
466 
467 	ret = -EPERM;
468 	if (on_sig_stack(sp))
469             goto out;
470 
471 	ret = -EINVAL;
472 	if (ss.ss_flags != TARGET_SS_DISABLE
473             && ss.ss_flags != TARGET_SS_ONSTACK
474             && ss.ss_flags != 0)
475             goto out;
476 
477 	if (ss.ss_flags == TARGET_SS_DISABLE) {
478             ss.ss_size = 0;
479             ss.ss_sp = 0;
480 	} else {
481             ret = -ENOMEM;
482             if (ss.ss_size < MINSIGSTKSZ)
483                 goto out;
484 	}
485 
486         target_sigaltstack_used.ss_sp = ss.ss_sp;
487         target_sigaltstack_used.ss_size = ss.ss_size;
488     }
489 
490     if (uoss) {
491         ret = -EFAULT;
492         if (!access_ok(VERIFY_WRITE, uoss, sizeof(oss)))
493             goto out;
494         memcpy(uoss, &oss, sizeof(oss));
495     }
496 
497     ret = 0;
498 out:
499     return ret;
500 }
501 
502 int do_sigaction(int sig, const struct target_sigaction *act,
503                  struct target_sigaction *oact)
504 {
505     struct emulated_sigaction *k;
506     struct sigaction act1;
507     int host_sig;
508 
509     if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
510         return -EINVAL;
511     k = &sigact_table[sig - 1];
512 #if defined(DEBUG_SIGNAL)
513     fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
514             sig, (int)act, (int)oact);
515 #endif
516     if (oact) {
517         oact->_sa_handler = tswapl(k->sa._sa_handler);
518         oact->sa_flags = tswapl(k->sa.sa_flags);
519 #if !defined(TARGET_MIPS)
520         oact->sa_restorer = tswapl(k->sa.sa_restorer);
521 #endif
522         oact->sa_mask = k->sa.sa_mask;
523     }
524     if (act) {
525         k->sa._sa_handler = tswapl(act->_sa_handler);
526         k->sa.sa_flags = tswapl(act->sa_flags);
527 #if !defined(TARGET_MIPS)
528         k->sa.sa_restorer = tswapl(act->sa_restorer);
529 #endif
530         k->sa.sa_mask = act->sa_mask;
531 
532         /* we update the host linux signal state */
533         host_sig = target_to_host_signal(sig);
534         if (host_sig != SIGSEGV && host_sig != SIGBUS) {
535             sigfillset(&act1.sa_mask);
536             act1.sa_flags = SA_SIGINFO;
537             if (k->sa.sa_flags & TARGET_SA_RESTART)
538                 act1.sa_flags |= SA_RESTART;
539             /* NOTE: it is important to update the host kernel signal
540                ignore state to avoid getting unexpected interrupted
541                syscalls */
542             if (k->sa._sa_handler == TARGET_SIG_IGN) {
543                 act1.sa_sigaction = (void *)SIG_IGN;
544             } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
545                 act1.sa_sigaction = (void *)SIG_DFL;
546             } else {
547                 act1.sa_sigaction = host_signal_handler;
548             }
549             sigaction(host_sig, &act1, NULL);
550         }
551     }
552     return 0;
553 }
554 
555 #ifndef offsetof
556 #define offsetof(type, field) ((size_t) &((type *)0)->field)
557 #endif
558 
559 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
560                                        const target_siginfo_t *info)
561 {
562     tswap_siginfo(tinfo, info);
563     return 0;
564 }
565 
566 #ifdef TARGET_I386
567 
568 /* from the Linux kernel */
569 
570 struct target_fpreg {
571 	uint16_t significand[4];
572 	uint16_t exponent;
573 };
574 
575 struct target_fpxreg {
576 	uint16_t significand[4];
577 	uint16_t exponent;
578 	uint16_t padding[3];
579 };
580 
581 struct target_xmmreg {
582 	target_ulong element[4];
583 };
584 
585 struct target_fpstate {
586 	/* Regular FPU environment */
587 	target_ulong 	cw;
588 	target_ulong	sw;
589 	target_ulong	tag;
590 	target_ulong	ipoff;
591 	target_ulong	cssel;
592 	target_ulong	dataoff;
593 	target_ulong	datasel;
594 	struct target_fpreg	_st[8];
595 	uint16_t	status;
596 	uint16_t	magic;		/* 0xffff = regular FPU data only */
597 
598 	/* FXSR FPU environment */
599 	target_ulong	_fxsr_env[6];	/* FXSR FPU env is ignored */
600 	target_ulong	mxcsr;
601 	target_ulong	reserved;
602 	struct target_fpxreg	_fxsr_st[8];	/* FXSR FPU reg data is ignored */
603 	struct target_xmmreg	_xmm[8];
604 	target_ulong	padding[56];
605 };
606 
607 #define X86_FXSR_MAGIC		0x0000
608 
609 struct target_sigcontext {
610 	uint16_t gs, __gsh;
611 	uint16_t fs, __fsh;
612 	uint16_t es, __esh;
613 	uint16_t ds, __dsh;
614 	target_ulong edi;
615 	target_ulong esi;
616 	target_ulong ebp;
617 	target_ulong esp;
618 	target_ulong ebx;
619 	target_ulong edx;
620 	target_ulong ecx;
621 	target_ulong eax;
622 	target_ulong trapno;
623 	target_ulong err;
624 	target_ulong eip;
625 	uint16_t cs, __csh;
626 	target_ulong eflags;
627 	target_ulong esp_at_signal;
628 	uint16_t ss, __ssh;
629         target_ulong fpstate; /* pointer */
630 	target_ulong oldmask;
631 	target_ulong cr2;
632 };
633 
634 struct target_ucontext {
635         target_ulong	  tuc_flags;
636 	target_ulong      tuc_link;
637 	target_stack_t	  tuc_stack;
638 	struct target_sigcontext tuc_mcontext;
639 	target_sigset_t	  tuc_sigmask;	/* mask last for extensibility */
640 };
641 
642 struct sigframe
643 {
644     target_ulong pretcode;
645     int sig;
646     struct target_sigcontext sc;
647     struct target_fpstate fpstate;
648     target_ulong extramask[TARGET_NSIG_WORDS-1];
649     char retcode[8];
650 };
651 
652 struct rt_sigframe
653 {
654     target_ulong pretcode;
655     int sig;
656     target_ulong pinfo;
657     target_ulong puc;
658     struct target_siginfo info;
659     struct target_ucontext uc;
660     struct target_fpstate fpstate;
661     char retcode[8];
662 };
663 
664 /*
665  * Set up a signal frame.
666  */
667 
668 /* XXX: save x87 state */
669 static int
670 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
671 		 CPUX86State *env, unsigned long mask)
672 {
673 	int err = 0;
674 
675 	err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
676 	err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
677 	err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
678 	err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
679 	err |= __put_user(env->regs[R_EDI], &sc->edi);
680 	err |= __put_user(env->regs[R_ESI], &sc->esi);
681 	err |= __put_user(env->regs[R_EBP], &sc->ebp);
682 	err |= __put_user(env->regs[R_ESP], &sc->esp);
683 	err |= __put_user(env->regs[R_EBX], &sc->ebx);
684 	err |= __put_user(env->regs[R_EDX], &sc->edx);
685 	err |= __put_user(env->regs[R_ECX], &sc->ecx);
686 	err |= __put_user(env->regs[R_EAX], &sc->eax);
687 	err |= __put_user(env->exception_index, &sc->trapno);
688 	err |= __put_user(env->error_code, &sc->err);
689 	err |= __put_user(env->eip, &sc->eip);
690 	err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
691 	err |= __put_user(env->eflags, &sc->eflags);
692 	err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
693 	err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
694 
695         cpu_x86_fsave(env, (void *)fpstate, 1);
696         fpstate->status = fpstate->sw;
697         err |= __put_user(0xffff, &fpstate->magic);
698         err |= __put_user(fpstate, &sc->fpstate);
699 
700 	/* non-iBCS2 extensions.. */
701 	err |= __put_user(mask, &sc->oldmask);
702 	err |= __put_user(env->cr[2], &sc->cr2);
703 	return err;
704 }
705 
706 /*
707  * Determine which stack to use..
708  */
709 
710 static inline void *
711 get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
712 {
713 	unsigned long esp;
714 
715 	/* Default to using normal stack */
716 	esp = env->regs[R_ESP];
717 	/* This is the X/Open sanctioned signal stack switching.  */
718         if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
719             if (sas_ss_flags(esp) == 0)
720                 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
721         }
722 
723 	/* This is the legacy signal stack switching. */
724 	else
725         if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
726             !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
727             ka->sa.sa_restorer) {
728             esp = (unsigned long) ka->sa.sa_restorer;
729 	}
730         return g2h((esp - frame_size) & -8ul);
731 }
732 
733 static void setup_frame(int sig, struct emulated_sigaction *ka,
734 			target_sigset_t *set, CPUX86State *env)
735 {
736 	struct sigframe *frame;
737 	int i, err = 0;
738 
739 	frame = get_sigframe(ka, env, sizeof(*frame));
740 
741 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
742 		goto give_sigsegv;
743 	err |= __put_user((/*current->exec_domain
744 		           && current->exec_domain->signal_invmap
745 		           && sig < 32
746 		           ? current->exec_domain->signal_invmap[sig]
747 		           : */ sig),
748 		          &frame->sig);
749 	if (err)
750 		goto give_sigsegv;
751 
752 	setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
753 	if (err)
754 		goto give_sigsegv;
755 
756         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
757             if (__put_user(set->sig[i], &frame->extramask[i - 1]))
758                 goto give_sigsegv;
759         }
760 
761 	/* Set up to return from userspace.  If provided, use a stub
762 	   already in userspace.  */
763 	if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
764 		err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
765 	} else {
766 		err |= __put_user(frame->retcode, &frame->pretcode);
767 		/* This is popl %eax ; movl $,%eax ; int $0x80 */
768 		err |= __put_user(0xb858, (short *)(frame->retcode+0));
769 #if defined(TARGET_X86_64)
770 #warning "Fix this !"
771 #else
772 		err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
773 #endif
774 		err |= __put_user(0x80cd, (short *)(frame->retcode+6));
775 	}
776 
777 	if (err)
778 		goto give_sigsegv;
779 
780 	/* Set up registers for signal handler */
781 	env->regs[R_ESP] = h2g(frame);
782 	env->eip = (unsigned long) ka->sa._sa_handler;
783 
784         cpu_x86_load_seg(env, R_DS, __USER_DS);
785         cpu_x86_load_seg(env, R_ES, __USER_DS);
786         cpu_x86_load_seg(env, R_SS, __USER_DS);
787         cpu_x86_load_seg(env, R_CS, __USER_CS);
788 	env->eflags &= ~TF_MASK;
789 
790 	return;
791 
792 give_sigsegv:
793 	if (sig == TARGET_SIGSEGV)
794 		ka->sa._sa_handler = TARGET_SIG_DFL;
795 	force_sig(TARGET_SIGSEGV /* , current */);
796 }
797 
798 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
799                            target_siginfo_t *info,
800 			   target_sigset_t *set, CPUX86State *env)
801 {
802 	struct rt_sigframe *frame;
803 	int i, err = 0;
804 
805 	frame = get_sigframe(ka, env, sizeof(*frame));
806 
807 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
808 		goto give_sigsegv;
809 
810 	err |= __put_user((/*current->exec_domain
811 		    	   && current->exec_domain->signal_invmap
812 		    	   && sig < 32
813 		    	   ? current->exec_domain->signal_invmap[sig]
814 			   : */sig),
815 			  &frame->sig);
816 	err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
817 	err |= __put_user((target_ulong)&frame->uc, &frame->puc);
818 	err |= copy_siginfo_to_user(&frame->info, info);
819 	if (err)
820 		goto give_sigsegv;
821 
822 	/* Create the ucontext.  */
823 	err |= __put_user(0, &frame->uc.tuc_flags);
824 	err |= __put_user(0, &frame->uc.tuc_link);
825 	err |= __put_user(target_sigaltstack_used.ss_sp,
826 			  &frame->uc.tuc_stack.ss_sp);
827 	err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
828 			  &frame->uc.tuc_stack.ss_flags);
829 	err |= __put_user(target_sigaltstack_used.ss_size,
830 			  &frame->uc.tuc_stack.ss_size);
831 	err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
832 			        env, set->sig[0]);
833         for(i = 0; i < TARGET_NSIG_WORDS; i++) {
834             if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
835                 goto give_sigsegv;
836         }
837 
838 	/* Set up to return from userspace.  If provided, use a stub
839 	   already in userspace.  */
840 	if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
841 		err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
842 	} else {
843 		err |= __put_user(frame->retcode, &frame->pretcode);
844 		/* This is movl $,%eax ; int $0x80 */
845 		err |= __put_user(0xb8, (char *)(frame->retcode+0));
846 		err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
847 		err |= __put_user(0x80cd, (short *)(frame->retcode+5));
848 	}
849 
850 	if (err)
851 		goto give_sigsegv;
852 
853 	/* Set up registers for signal handler */
854 	env->regs[R_ESP] = (unsigned long) frame;
855 	env->eip = (unsigned long) ka->sa._sa_handler;
856 
857         cpu_x86_load_seg(env, R_DS, __USER_DS);
858         cpu_x86_load_seg(env, R_ES, __USER_DS);
859         cpu_x86_load_seg(env, R_SS, __USER_DS);
860         cpu_x86_load_seg(env, R_CS, __USER_CS);
861 	env->eflags &= ~TF_MASK;
862 
863 	return;
864 
865 give_sigsegv:
866 	if (sig == TARGET_SIGSEGV)
867 		ka->sa._sa_handler = TARGET_SIG_DFL;
868 	force_sig(TARGET_SIGSEGV /* , current */);
869 }
870 
871 static int
872 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
873 {
874 	unsigned int err = 0;
875 
876         cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
877         cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
878         cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
879         cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
880 
881         env->regs[R_EDI] = ldl(&sc->edi);
882         env->regs[R_ESI] = ldl(&sc->esi);
883         env->regs[R_EBP] = ldl(&sc->ebp);
884         env->regs[R_ESP] = ldl(&sc->esp);
885         env->regs[R_EBX] = ldl(&sc->ebx);
886         env->regs[R_EDX] = ldl(&sc->edx);
887         env->regs[R_ECX] = ldl(&sc->ecx);
888         env->eip = ldl(&sc->eip);
889 
890         cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
891         cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
892 
893 	{
894 		unsigned int tmpflags;
895                 tmpflags = ldl(&sc->eflags);
896 		env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
897                 //		regs->orig_eax = -1;		/* disable syscall checks */
898 	}
899 
900 	{
901 		struct _fpstate * buf;
902                 buf = (void *)ldl(&sc->fpstate);
903 		if (buf) {
904 #if 0
905 			if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
906 				goto badframe;
907 #endif
908                         cpu_x86_frstor(env, (void *)buf, 1);
909 		}
910 	}
911 
912         *peax = ldl(&sc->eax);
913 	return err;
914 #if 0
915 badframe:
916 	return 1;
917 #endif
918 }
919 
920 long do_sigreturn(CPUX86State *env)
921 {
922     struct sigframe *frame = (struct sigframe *)g2h(env->regs[R_ESP] - 8);
923     target_sigset_t target_set;
924     sigset_t set;
925     int eax, i;
926 
927 #if defined(DEBUG_SIGNAL)
928     fprintf(stderr, "do_sigreturn\n");
929 #endif
930     /* set blocked signals */
931     if (__get_user(target_set.sig[0], &frame->sc.oldmask))
932         goto badframe;
933     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
934         if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
935             goto badframe;
936     }
937 
938     target_to_host_sigset_internal(&set, &target_set);
939     sigprocmask(SIG_SETMASK, &set, NULL);
940 
941     /* restore registers */
942     if (restore_sigcontext(env, &frame->sc, &eax))
943         goto badframe;
944     return eax;
945 
946 badframe:
947     force_sig(TARGET_SIGSEGV);
948     return 0;
949 }
950 
951 long do_rt_sigreturn(CPUX86State *env)
952 {
953 	struct rt_sigframe *frame = (struct rt_sigframe *)g2h(env->regs[R_ESP] - 4);
954         sigset_t set;
955 	int eax;
956 
957 #if 0
958 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
959 		goto badframe;
960 #endif
961         target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
962         sigprocmask(SIG_SETMASK, &set, NULL);
963 
964 	if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
965 		goto badframe;
966 
967 	if (do_sigaltstack(&frame->uc.tuc_stack, NULL, get_sp_from_cpustate(env)) == -EFAULT)
968 		goto badframe;
969 
970 	return eax;
971 
972 badframe:
973 	force_sig(TARGET_SIGSEGV);
974 	return 0;
975 }
976 
977 #elif defined(TARGET_ARM)
978 
979 struct target_sigcontext {
980 	target_ulong trap_no;
981 	target_ulong error_code;
982 	target_ulong oldmask;
983 	target_ulong arm_r0;
984 	target_ulong arm_r1;
985 	target_ulong arm_r2;
986 	target_ulong arm_r3;
987 	target_ulong arm_r4;
988 	target_ulong arm_r5;
989 	target_ulong arm_r6;
990 	target_ulong arm_r7;
991 	target_ulong arm_r8;
992 	target_ulong arm_r9;
993 	target_ulong arm_r10;
994 	target_ulong arm_fp;
995 	target_ulong arm_ip;
996 	target_ulong arm_sp;
997 	target_ulong arm_lr;
998 	target_ulong arm_pc;
999 	target_ulong arm_cpsr;
1000 	target_ulong fault_address;
1001 };
1002 
1003 struct target_ucontext {
1004     target_ulong tuc_flags;
1005     target_ulong tuc_link;
1006     target_stack_t tuc_stack;
1007     struct target_sigcontext tuc_mcontext;
1008     target_sigset_t  tuc_sigmask;	/* mask last for extensibility */
1009 };
1010 
1011 struct sigframe
1012 {
1013     struct target_sigcontext sc;
1014     target_ulong extramask[TARGET_NSIG_WORDS-1];
1015     target_ulong retcode;
1016 };
1017 
1018 struct rt_sigframe
1019 {
1020     struct target_siginfo *pinfo;
1021     void *puc;
1022     struct target_siginfo info;
1023     struct target_ucontext uc;
1024     target_ulong retcode;
1025 };
1026 
1027 #define TARGET_CONFIG_CPU_32 1
1028 
1029 /*
1030  * For ARM syscalls, we encode the syscall number into the instruction.
1031  */
1032 #define SWI_SYS_SIGRETURN	(0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1033 #define SWI_SYS_RT_SIGRETURN	(0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1034 
1035 /*
1036  * For Thumb syscalls, we pass the syscall number via r7.  We therefore
1037  * need two 16-bit instructions.
1038  */
1039 #define SWI_THUMB_SIGRETURN	(0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1040 #define SWI_THUMB_RT_SIGRETURN	(0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1041 
1042 static const target_ulong retcodes[4] = {
1043 	SWI_SYS_SIGRETURN,	SWI_THUMB_SIGRETURN,
1044 	SWI_SYS_RT_SIGRETURN,	SWI_THUMB_RT_SIGRETURN
1045 };
1046 
1047 
1048 #define __put_user_error(x,p,e) __put_user(x, p)
1049 #define __get_user_error(x,p,e) __get_user(x, p)
1050 
1051 static inline int valid_user_regs(CPUState *regs)
1052 {
1053     return 1;
1054 }
1055 
1056 static int
1057 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1058 		 CPUState *env, unsigned long mask)
1059 {
1060 	int err = 0;
1061 
1062 	__put_user_error(env->regs[0], &sc->arm_r0, err);
1063 	__put_user_error(env->regs[1], &sc->arm_r1, err);
1064 	__put_user_error(env->regs[2], &sc->arm_r2, err);
1065 	__put_user_error(env->regs[3], &sc->arm_r3, err);
1066 	__put_user_error(env->regs[4], &sc->arm_r4, err);
1067 	__put_user_error(env->regs[5], &sc->arm_r5, err);
1068 	__put_user_error(env->regs[6], &sc->arm_r6, err);
1069 	__put_user_error(env->regs[7], &sc->arm_r7, err);
1070 	__put_user_error(env->regs[8], &sc->arm_r8, err);
1071 	__put_user_error(env->regs[9], &sc->arm_r9, err);
1072 	__put_user_error(env->regs[10], &sc->arm_r10, err);
1073 	__put_user_error(env->regs[11], &sc->arm_fp, err);
1074 	__put_user_error(env->regs[12], &sc->arm_ip, err);
1075 	__put_user_error(env->regs[13], &sc->arm_sp, err);
1076 	__put_user_error(env->regs[14], &sc->arm_lr, err);
1077 	__put_user_error(env->regs[15], &sc->arm_pc, err);
1078 #ifdef TARGET_CONFIG_CPU_32
1079 	__put_user_error(cpsr_read(env), &sc->arm_cpsr, err);
1080 #endif
1081 
1082 	__put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1083 	__put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1084 	__put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1085 	__put_user_error(mask, &sc->oldmask, err);
1086 
1087 	return err;
1088 }
1089 
1090 static inline void *
1091 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1092 {
1093 	unsigned long sp = regs->regs[13];
1094 
1095 	/*
1096 	 * This is the X/Open sanctioned signal stack switching.
1097 	 */
1098 	if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1099             sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1100 	/*
1101 	 * ATPCS B01 mandates 8-byte alignment
1102 	 */
1103 	return g2h((sp - framesize) & ~7);
1104 }
1105 
1106 static int
1107 setup_return(CPUState *env, struct emulated_sigaction *ka,
1108 	     target_ulong *rc, void *frame, int usig)
1109 {
1110 	target_ulong handler = (target_ulong)ka->sa._sa_handler;
1111 	target_ulong retcode;
1112 	int thumb = 0;
1113 #if defined(TARGET_CONFIG_CPU_32)
1114 #if 0
1115 	target_ulong cpsr = env->cpsr;
1116 
1117 	/*
1118 	 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1119 	 */
1120 	if (ka->sa.sa_flags & SA_THIRTYTWO)
1121 		cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1122 
1123 #ifdef CONFIG_ARM_THUMB
1124 	if (elf_hwcap & HWCAP_THUMB) {
1125 		/*
1126 		 * The LSB of the handler determines if we're going to
1127 		 * be using THUMB or ARM mode for this signal handler.
1128 		 */
1129 		thumb = handler & 1;
1130 
1131 		if (thumb)
1132 			cpsr |= T_BIT;
1133 		else
1134 			cpsr &= ~T_BIT;
1135 	}
1136 #endif /* CONFIG_ARM_THUMB */
1137 #endif /* 0 */
1138 #endif /* TARGET_CONFIG_CPU_32 */
1139 
1140 	if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1141 		retcode = (target_ulong)ka->sa.sa_restorer;
1142 	} else {
1143 		unsigned int idx = thumb;
1144 
1145 		if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1146 			idx += 2;
1147 
1148 		if (__put_user(retcodes[idx], rc))
1149 			return 1;
1150 #if 0
1151 		flush_icache_range((target_ulong)rc,
1152 				   (target_ulong)(rc + 1));
1153 #endif
1154 		retcode = ((target_ulong)rc) + thumb;
1155 	}
1156 
1157 	env->regs[0] = usig;
1158 	env->regs[13] = h2g(frame);
1159 	env->regs[14] = retcode;
1160 	env->regs[15] = handler & (thumb ? ~1 : ~3);
1161 
1162 #if 0
1163 #ifdef TARGET_CONFIG_CPU_32
1164 	env->cpsr = cpsr;
1165 #endif
1166 #endif
1167 
1168 	return 0;
1169 }
1170 
1171 static void setup_frame(int usig, struct emulated_sigaction *ka,
1172 			target_sigset_t *set, CPUState *regs)
1173 {
1174 	struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
1175 	int i, err = 0;
1176 
1177 	err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1178 
1179         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1180             if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1181                 return;
1182 	}
1183 
1184 	if (err == 0)
1185             err = setup_return(regs, ka, &frame->retcode, frame, usig);
1186         //	return err;
1187 }
1188 
1189 static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1190                            target_siginfo_t *info,
1191 			   target_sigset_t *set, CPUState *env)
1192 {
1193 	struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame));
1194 	struct target_sigaltstack stack;
1195 	int i, err = 0;
1196 
1197 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
1198             return /* 1 */;
1199 
1200 	__put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err);
1201 	__put_user_error(&frame->uc, (target_ulong *)&frame->puc, err);
1202 	err |= copy_siginfo_to_user(&frame->info, info);
1203 
1204 	/* Clear all the bits of the ucontext we don't use.  */
1205 	memset(&frame->uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
1206 
1207         memset(&stack, 0, sizeof(stack));
1208         __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1209         __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1210         __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1211         if (!access_ok(VERIFY_WRITE, &frame->uc.tuc_stack, sizeof(stack)))
1212             err = 1;
1213         else
1214             memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1215 
1216 	err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
1217 				env, set->sig[0]);
1218         for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1219             if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1220                 return;
1221         }
1222 
1223 	if (err == 0)
1224 		err = setup_return(env, ka, &frame->retcode, frame, usig);
1225 
1226 	if (err == 0) {
1227 		/*
1228 		 * For realtime signals we must also set the second and third
1229 		 * arguments for the signal handler.
1230 		 *   -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1231 		 */
1232             env->regs[1] = (target_ulong)frame->pinfo;
1233             env->regs[2] = (target_ulong)frame->puc;
1234 	}
1235 
1236         //	return err;
1237 }
1238 
1239 static int
1240 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1241 {
1242 	int err = 0;
1243         uint32_t cpsr;
1244 
1245 	__get_user_error(env->regs[0], &sc->arm_r0, err);
1246 	__get_user_error(env->regs[1], &sc->arm_r1, err);
1247 	__get_user_error(env->regs[2], &sc->arm_r2, err);
1248 	__get_user_error(env->regs[3], &sc->arm_r3, err);
1249 	__get_user_error(env->regs[4], &sc->arm_r4, err);
1250 	__get_user_error(env->regs[5], &sc->arm_r5, err);
1251 	__get_user_error(env->regs[6], &sc->arm_r6, err);
1252 	__get_user_error(env->regs[7], &sc->arm_r7, err);
1253 	__get_user_error(env->regs[8], &sc->arm_r8, err);
1254 	__get_user_error(env->regs[9], &sc->arm_r9, err);
1255 	__get_user_error(env->regs[10], &sc->arm_r10, err);
1256 	__get_user_error(env->regs[11], &sc->arm_fp, err);
1257 	__get_user_error(env->regs[12], &sc->arm_ip, err);
1258 	__get_user_error(env->regs[13], &sc->arm_sp, err);
1259 	__get_user_error(env->regs[14], &sc->arm_lr, err);
1260 	__get_user_error(env->regs[15], &sc->arm_pc, err);
1261 #ifdef TARGET_CONFIG_CPU_32
1262 	__get_user_error(cpsr, &sc->arm_cpsr, err);
1263         cpsr_write(env, cpsr, 0xffffffff);
1264 #endif
1265 
1266 	err |= !valid_user_regs(env);
1267 
1268 	return err;
1269 }
1270 
1271 long do_sigreturn(CPUState *env)
1272 {
1273 	struct sigframe *frame;
1274 	target_sigset_t set;
1275         sigset_t host_set;
1276         int i;
1277 
1278 	/*
1279 	 * Since we stacked the signal on a 64-bit boundary,
1280 	 * then 'sp' should be word aligned here.  If it's
1281 	 * not, then the user is trying to mess with us.
1282 	 */
1283 	if (env->regs[13] & 7)
1284 		goto badframe;
1285 
1286 	frame = (struct sigframe *)g2h(env->regs[13]);
1287 
1288 #if 0
1289 	if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1290 		goto badframe;
1291 #endif
1292 	if (__get_user(set.sig[0], &frame->sc.oldmask))
1293             goto badframe;
1294         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1295             if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1296                 goto badframe;
1297         }
1298 
1299         target_to_host_sigset_internal(&host_set, &set);
1300         sigprocmask(SIG_SETMASK, &host_set, NULL);
1301 
1302 	if (restore_sigcontext(env, &frame->sc))
1303 		goto badframe;
1304 
1305 #if 0
1306 	/* Send SIGTRAP if we're single-stepping */
1307 	if (ptrace_cancel_bpt(current))
1308 		send_sig(SIGTRAP, current, 1);
1309 #endif
1310 	return env->regs[0];
1311 
1312 badframe:
1313         force_sig(SIGSEGV /* , current */);
1314 	return 0;
1315 }
1316 
1317 long do_rt_sigreturn(CPUState *env)
1318 {
1319 	struct rt_sigframe *frame;
1320         sigset_t host_set;
1321 
1322 	/*
1323 	 * Since we stacked the signal on a 64-bit boundary,
1324 	 * then 'sp' should be word aligned here.  If it's
1325 	 * not, then the user is trying to mess with us.
1326 	 */
1327 	if (env->regs[13] & 7)
1328 		goto badframe;
1329 
1330 	frame = (struct rt_sigframe *)env->regs[13];
1331 
1332 #if 0
1333 	if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1334 		goto badframe;
1335 #endif
1336         target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1337         sigprocmask(SIG_SETMASK, &host_set, NULL);
1338 
1339 	if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1340 		goto badframe;
1341 
1342 	if (do_sigaltstack(&frame->uc.tuc_stack, NULL, get_sp_from_cpustate(env)) == -EFAULT)
1343 		goto badframe;
1344 
1345 #if 0
1346 	/* Send SIGTRAP if we're single-stepping */
1347 	if (ptrace_cancel_bpt(current))
1348 		send_sig(SIGTRAP, current, 1);
1349 #endif
1350 	return env->regs[0];
1351 
1352 badframe:
1353         force_sig(SIGSEGV /* , current */);
1354 	return 0;
1355 }
1356 
1357 #elif defined(TARGET_SPARC)
1358 
1359 #define __SUNOS_MAXWIN   31
1360 
1361 /* This is what SunOS does, so shall I. */
1362 struct target_sigcontext {
1363         target_ulong sigc_onstack;      /* state to restore */
1364 
1365         target_ulong sigc_mask;         /* sigmask to restore */
1366         target_ulong sigc_sp;           /* stack pointer */
1367         target_ulong sigc_pc;           /* program counter */
1368         target_ulong sigc_npc;          /* next program counter */
1369         target_ulong sigc_psr;          /* for condition codes etc */
1370         target_ulong sigc_g1;           /* User uses these two registers */
1371         target_ulong sigc_o0;           /* within the trampoline code. */
1372 
1373         /* Now comes information regarding the users window set
1374          * at the time of the signal.
1375          */
1376         target_ulong sigc_oswins;       /* outstanding windows */
1377 
1378         /* stack ptrs for each regwin buf */
1379         char *sigc_spbuf[__SUNOS_MAXWIN];
1380 
1381         /* Windows to restore after signal */
1382         struct {
1383                 target_ulong locals[8];
1384                 target_ulong ins[8];
1385         } sigc_wbuf[__SUNOS_MAXWIN];
1386 };
1387 /* A Sparc stack frame */
1388 struct sparc_stackf {
1389         target_ulong locals[8];
1390         target_ulong ins[6];
1391         struct sparc_stackf *fp;
1392         target_ulong callers_pc;
1393         char *structptr;
1394         target_ulong xargs[6];
1395         target_ulong xxargs[1];
1396 };
1397 
1398 typedef struct {
1399         struct {
1400                 target_ulong psr;
1401                 target_ulong pc;
1402                 target_ulong npc;
1403                 target_ulong y;
1404                 target_ulong u_regs[16]; /* globals and ins */
1405         }               si_regs;
1406         int             si_mask;
1407 } __siginfo_t;
1408 
1409 typedef struct {
1410         unsigned   long si_float_regs [32];
1411         unsigned   long si_fsr;
1412         unsigned   long si_fpqdepth;
1413         struct {
1414                 unsigned long *insn_addr;
1415                 unsigned long insn;
1416         } si_fpqueue [16];
1417 } qemu_siginfo_fpu_t;
1418 
1419 
1420 struct target_signal_frame {
1421 	struct sparc_stackf	ss;
1422 	__siginfo_t		info;
1423 	qemu_siginfo_fpu_t 	*fpu_save;
1424 	target_ulong		insns[2] __attribute__ ((aligned (8)));
1425 	target_ulong		extramask[TARGET_NSIG_WORDS - 1];
1426 	target_ulong		extra_size; /* Should be 0 */
1427 	qemu_siginfo_fpu_t	fpu_state;
1428 };
1429 struct target_rt_signal_frame {
1430 	struct sparc_stackf	ss;
1431 	siginfo_t		info;
1432 	target_ulong		regs[20];
1433 	sigset_t		mask;
1434 	qemu_siginfo_fpu_t 	*fpu_save;
1435 	unsigned int		insns[2];
1436 	stack_t			stack;
1437 	unsigned int		extra_size; /* Should be 0 */
1438 	qemu_siginfo_fpu_t	fpu_state;
1439 };
1440 
1441 #define UREG_O0        16
1442 #define UREG_O6        22
1443 #define UREG_I0        0
1444 #define UREG_I1        1
1445 #define UREG_I2        2
1446 #define UREG_I6        6
1447 #define UREG_I7        7
1448 #define UREG_L0	       8
1449 #define UREG_FP        UREG_I6
1450 #define UREG_SP        UREG_O6
1451 
1452 static inline void *get_sigframe(struct emulated_sigaction *sa, CPUState *env, unsigned long framesize)
1453 {
1454 	unsigned long sp;
1455 
1456 	sp = env->regwptr[UREG_FP];
1457 
1458 	/* This is the X/Open sanctioned signal stack switching.  */
1459 	if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1460             if (!on_sig_stack(sp)
1461                 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1462                 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1463 	}
1464 	return g2h(sp - framesize);
1465 }
1466 
1467 static int
1468 setup___siginfo(__siginfo_t *si, CPUState *env, target_ulong mask)
1469 {
1470 	int err = 0, i;
1471 
1472 	err |= __put_user(env->psr, &si->si_regs.psr);
1473 	err |= __put_user(env->pc, &si->si_regs.pc);
1474 	err |= __put_user(env->npc, &si->si_regs.npc);
1475 	err |= __put_user(env->y, &si->si_regs.y);
1476 	for (i=0; i < 8; i++) {
1477 		err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1478 	}
1479 	for (i=0; i < 8; i++) {
1480 		err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1481 	}
1482 	err |= __put_user(mask, &si->si_mask);
1483 	return err;
1484 }
1485 
1486 #if 0
1487 static int
1488 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1489 		 CPUState *env, unsigned long mask)
1490 {
1491 	int err = 0;
1492 
1493 	err |= __put_user(mask, &sc->sigc_mask);
1494 	err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1495 	err |= __put_user(env->pc, &sc->sigc_pc);
1496 	err |= __put_user(env->npc, &sc->sigc_npc);
1497 	err |= __put_user(env->psr, &sc->sigc_psr);
1498 	err |= __put_user(env->gregs[1], &sc->sigc_g1);
1499 	err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1500 
1501 	return err;
1502 }
1503 #endif
1504 #define NF_ALIGNEDSZ  (((sizeof(struct target_signal_frame) + 7) & (~7)))
1505 
1506 static void setup_frame(int sig, struct emulated_sigaction *ka,
1507 			target_sigset_t *set, CPUState *env)
1508 {
1509 	struct target_signal_frame *sf;
1510 	int sigframe_size, err, i;
1511 
1512 	/* 1. Make sure everything is clean */
1513 	//synchronize_user_stack();
1514 
1515         sigframe_size = NF_ALIGNEDSZ;
1516 
1517 	sf = (struct target_signal_frame *)
1518 		get_sigframe(ka, env, sigframe_size);
1519 
1520 	//fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1521 #if 0
1522 	if (invalid_frame_pointer(sf, sigframe_size))
1523 		goto sigill_and_return;
1524 #endif
1525 	/* 2. Save the current process state */
1526 	err = setup___siginfo(&sf->info, env, set->sig[0]);
1527 	err |= __put_user(0, &sf->extra_size);
1528 
1529 	//err |= save_fpu_state(regs, &sf->fpu_state);
1530 	//err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1531 
1532 	err |= __put_user(set->sig[0], &sf->info.si_mask);
1533 	for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1534 		err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1535 	}
1536 
1537 	for (i = 0; i < 8; i++) {
1538 	  	err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1539 	}
1540 	for (i = 0; i < 8; i++) {
1541 	  	err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1542 	}
1543 	if (err)
1544 		goto sigsegv;
1545 
1546 	/* 3. signal handler back-trampoline and parameters */
1547 	env->regwptr[UREG_FP] = h2g(sf);
1548 	env->regwptr[UREG_I0] = sig;
1549 	env->regwptr[UREG_I1] = h2g(&sf->info);
1550 	env->regwptr[UREG_I2] = h2g(&sf->info);
1551 
1552 	/* 4. signal handler */
1553 	env->pc = (unsigned long) ka->sa._sa_handler;
1554 	env->npc = (env->pc + 4);
1555 	/* 5. return to kernel instructions */
1556 	if (ka->sa.sa_restorer)
1557 		env->regwptr[UREG_I7] = (unsigned long)ka->sa.sa_restorer;
1558 	else {
1559 		env->regwptr[UREG_I7] = h2g(&(sf->insns[0]) - 2);
1560 
1561 		/* mov __NR_sigreturn, %g1 */
1562 		err |= __put_user(0x821020d8, &sf->insns[0]);
1563 
1564 		/* t 0x10 */
1565 		err |= __put_user(0x91d02010, &sf->insns[1]);
1566 		if (err)
1567 			goto sigsegv;
1568 
1569 		/* Flush instruction space. */
1570 		//flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1571                 //		tb_flush(env);
1572 	}
1573 	return;
1574 
1575         //sigill_and_return:
1576 	force_sig(TARGET_SIGILL);
1577 sigsegv:
1578 	//fprintf(stderr, "force_sig\n");
1579 	force_sig(TARGET_SIGSEGV);
1580 }
1581 static inline int
1582 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
1583 {
1584         int err;
1585 #if 0
1586 #ifdef CONFIG_SMP
1587         if (current->flags & PF_USEDFPU)
1588                 regs->psr &= ~PSR_EF;
1589 #else
1590         if (current == last_task_used_math) {
1591                 last_task_used_math = 0;
1592                 regs->psr &= ~PSR_EF;
1593         }
1594 #endif
1595         current->used_math = 1;
1596         current->flags &= ~PF_USEDFPU;
1597 #endif
1598 #if 0
1599         if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1600                 return -EFAULT;
1601 #endif
1602 
1603 #if 0
1604         /* XXX: incorrect */
1605         err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1606 	                             (sizeof(unsigned long) * 32));
1607 #endif
1608         err |= __get_user(env->fsr, &fpu->si_fsr);
1609 #if 0
1610         err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1611         if (current->thread.fpqdepth != 0)
1612                 err |= __copy_from_user(&current->thread.fpqueue[0],
1613                                         &fpu->si_fpqueue[0],
1614                                         ((sizeof(unsigned long) +
1615                                         (sizeof(unsigned long *)))*16));
1616 #endif
1617         return err;
1618 }
1619 
1620 
1621 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1622                            target_siginfo_t *info,
1623 			   target_sigset_t *set, CPUState *env)
1624 {
1625     fprintf(stderr, "setup_rt_frame: not implemented\n");
1626 }
1627 
1628 long do_sigreturn(CPUState *env)
1629 {
1630         struct target_signal_frame *sf;
1631         uint32_t up_psr, pc, npc;
1632         target_sigset_t set;
1633         sigset_t host_set;
1634         target_ulong fpu_save;
1635         int err, i;
1636 
1637         sf = (struct target_signal_frame *)g2h(env->regwptr[UREG_FP]);
1638 #if 0
1639 	fprintf(stderr, "sigreturn\n");
1640 	fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1641 #endif
1642 	//cpu_dump_state(env, stderr, fprintf, 0);
1643 
1644         /* 1. Make sure we are not getting garbage from the user */
1645 #if 0
1646         if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
1647                 goto segv_and_exit;
1648 #endif
1649 
1650         if (((uint) sf) & 3)
1651                 goto segv_and_exit;
1652 
1653         err = __get_user(pc,  &sf->info.si_regs.pc);
1654         err |= __get_user(npc, &sf->info.si_regs.npc);
1655 
1656         if ((pc | npc) & 3)
1657                 goto segv_and_exit;
1658 
1659         /* 2. Restore the state */
1660         err |= __get_user(up_psr, &sf->info.si_regs.psr);
1661 
1662         /* User can only change condition codes and FPU enabling in %psr. */
1663         env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1664                   | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1665 
1666 	env->pc = pc;
1667 	env->npc = npc;
1668         err |= __get_user(env->y, &sf->info.si_regs.y);
1669 	for (i=0; i < 8; i++) {
1670 		err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1671 	}
1672 	for (i=0; i < 8; i++) {
1673 		err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1674 	}
1675 
1676         err |= __get_user(fpu_save, (target_ulong *)&sf->fpu_save);
1677 
1678         //if (fpu_save)
1679         //        err |= restore_fpu_state(env, fpu_save);
1680 
1681         /* This is pretty much atomic, no amount locking would prevent
1682          * the races which exist anyways.
1683          */
1684         err |= __get_user(set.sig[0], &sf->info.si_mask);
1685         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1686             err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1687         }
1688 
1689         target_to_host_sigset_internal(&host_set, &set);
1690         sigprocmask(SIG_SETMASK, &host_set, NULL);
1691 
1692         if (err)
1693                 goto segv_and_exit;
1694 
1695         return env->regwptr[0];
1696 
1697 segv_and_exit:
1698 	force_sig(TARGET_SIGSEGV);
1699 }
1700 
1701 long do_rt_sigreturn(CPUState *env)
1702 {
1703     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1704     return -ENOSYS;
1705 }
1706 
1707 #elif defined(TARGET_MIPS64)
1708 
1709 # warning signal handling not implemented
1710 
1711 static void setup_frame(int sig, struct emulated_sigaction *ka,
1712 			target_sigset_t *set, CPUState *env)
1713 {
1714     fprintf(stderr, "setup_frame: not implemented\n");
1715 }
1716 
1717 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1718                            target_siginfo_t *info,
1719 			   target_sigset_t *set, CPUState *env)
1720 {
1721     fprintf(stderr, "setup_rt_frame: not implemented\n");
1722 }
1723 
1724 long do_sigreturn(CPUState *env)
1725 {
1726     fprintf(stderr, "do_sigreturn: not implemented\n");
1727     return -ENOSYS;
1728 }
1729 
1730 long do_rt_sigreturn(CPUState *env)
1731 {
1732     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1733     return -ENOSYS;
1734 }
1735 
1736 #elif defined(TARGET_MIPSN32)
1737 
1738 # warning signal handling not implemented
1739 
1740 static void setup_frame(int sig, struct emulated_sigaction *ka,
1741 			target_sigset_t *set, CPUState *env)
1742 {
1743     fprintf(stderr, "setup_frame: not implemented\n");
1744 }
1745 
1746 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1747                            target_siginfo_t *info,
1748 			   target_sigset_t *set, CPUState *env)
1749 {
1750     fprintf(stderr, "setup_rt_frame: not implemented\n");
1751 }
1752 
1753 long do_sigreturn(CPUState *env)
1754 {
1755     fprintf(stderr, "do_sigreturn: not implemented\n");
1756     return -ENOSYS;
1757 }
1758 
1759 long do_rt_sigreturn(CPUState *env)
1760 {
1761     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1762     return -ENOSYS;
1763 }
1764 
1765 #elif defined(TARGET_MIPS)
1766 
1767 struct target_sigcontext {
1768     uint32_t   sc_regmask;     /* Unused */
1769     uint32_t   sc_status;
1770     uint64_t   sc_pc;
1771     uint64_t   sc_regs[32];
1772     uint64_t   sc_fpregs[32];
1773     uint32_t   sc_ownedfp;     /* Unused */
1774     uint32_t   sc_fpc_csr;
1775     uint32_t   sc_fpc_eir;     /* Unused */
1776     uint32_t   sc_used_math;
1777     uint32_t   sc_dsp;         /* dsp status, was sc_ssflags */
1778     uint64_t   sc_mdhi;
1779     uint64_t   sc_mdlo;
1780     target_ulong   sc_hi1;         /* Was sc_cause */
1781     target_ulong   sc_lo1;         /* Was sc_badvaddr */
1782     target_ulong   sc_hi2;         /* Was sc_sigset[4] */
1783     target_ulong   sc_lo2;
1784     target_ulong   sc_hi3;
1785     target_ulong   sc_lo3;
1786 };
1787 
1788 struct sigframe {
1789     uint32_t sf_ass[4];			/* argument save space for o32 */
1790     uint32_t sf_code[2];			/* signal trampoline */
1791     struct target_sigcontext sf_sc;
1792     target_sigset_t sf_mask;
1793 };
1794 
1795 /* Install trampoline to jump back from signal handler */
1796 static inline int install_sigtramp(unsigned int *tramp,   unsigned int syscall)
1797 {
1798     int err;
1799 
1800     /*
1801     * Set up the return code ...
1802     *
1803     *         li      v0, __NR__foo_sigreturn
1804     *         syscall
1805     */
1806 
1807     err = __put_user(0x24020000 + syscall, tramp + 0);
1808     err |= __put_user(0x0000000c          , tramp + 1);
1809     /* flush_cache_sigtramp((unsigned long) tramp); */
1810     return err;
1811 }
1812 
1813 static inline int
1814 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
1815 {
1816     int err = 0;
1817 
1818     err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
1819 
1820 #define save_gp_reg(i) do {   							\
1821         err |= __put_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]);	\
1822     } while(0)
1823     __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
1824     save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
1825     save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
1826     save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
1827     save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
1828     save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
1829     save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
1830     save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
1831     save_gp_reg(31);
1832 #undef save_gp_reg
1833 
1834     err |= __put_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
1835     err |= __put_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
1836 
1837     /* Not used yet, but might be useful if we ever have DSP suppport */
1838 #if 0
1839     if (cpu_has_dsp) {
1840 	err |= __put_user(mfhi1(), &sc->sc_hi1);
1841 	err |= __put_user(mflo1(), &sc->sc_lo1);
1842 	err |= __put_user(mfhi2(), &sc->sc_hi2);
1843 	err |= __put_user(mflo2(), &sc->sc_lo2);
1844 	err |= __put_user(mfhi3(), &sc->sc_hi3);
1845 	err |= __put_user(mflo3(), &sc->sc_lo3);
1846 	err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
1847     }
1848     /* same with 64 bit */
1849 #ifdef CONFIG_64BIT
1850     err |= __put_user(regs->hi, &sc->sc_hi[0]);
1851     err |= __put_user(regs->lo, &sc->sc_lo[0]);
1852     if (cpu_has_dsp) {
1853 	err |= __put_user(mfhi1(), &sc->sc_hi[1]);
1854 	err |= __put_user(mflo1(), &sc->sc_lo[1]);
1855 	err |= __put_user(mfhi2(), &sc->sc_hi[2]);
1856 	err |= __put_user(mflo2(), &sc->sc_lo[2]);
1857 	err |= __put_user(mfhi3(), &sc->sc_hi[3]);
1858 	err |= __put_user(mflo3(), &sc->sc_lo[3]);
1859 	err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
1860     }
1861 #endif
1862 #endif
1863 
1864 #if 0
1865     err |= __put_user(!!used_math(), &sc->sc_used_math);
1866 
1867     if (!used_math())
1868 	goto out;
1869 
1870     /*
1871     * Save FPU state to signal context.  Signal handler will "inherit"
1872     * current FPU state.
1873     */
1874     preempt_disable();
1875 
1876     if (!is_fpu_owner()) {
1877 	own_fpu();
1878 	restore_fp(current);
1879     }
1880     err |= save_fp_context(sc);
1881 
1882     preempt_enable();
1883     out:
1884 #endif
1885     return err;
1886 }
1887 
1888 static inline int
1889 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
1890 {
1891     int err = 0;
1892 
1893     err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
1894 
1895     err |= __get_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
1896     err |= __get_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
1897 
1898 #define restore_gp_reg(i) do {   							\
1899         err |= __get_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]);		\
1900     } while(0)
1901     restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
1902     restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
1903     restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
1904     restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
1905     restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
1906     restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
1907     restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
1908     restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
1909     restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
1910     restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
1911     restore_gp_reg(31);
1912 #undef restore_gp_reg
1913 
1914 #if 0
1915     if (cpu_has_dsp) {
1916 	err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
1917 	err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
1918 	err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
1919 	err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
1920 	err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
1921 	err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
1922 	err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
1923     }
1924 #ifdef CONFIG_64BIT
1925     err |= __get_user(regs->hi, &sc->sc_hi[0]);
1926     err |= __get_user(regs->lo, &sc->sc_lo[0]);
1927     if (cpu_has_dsp) {
1928 	err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
1929 	err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
1930 	err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
1931 	err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
1932 	err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
1933 	err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
1934 	err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
1935     }
1936 #endif
1937 
1938     err |= __get_user(used_math, &sc->sc_used_math);
1939     conditional_used_math(used_math);
1940 
1941     preempt_disable();
1942 
1943     if (used_math()) {
1944 	/* restore fpu context if we have used it before */
1945 	own_fpu();
1946 	err |= restore_fp_context(sc);
1947     } else {
1948 	/* signal handler may have used FPU.  Give it up. */
1949 	lose_fpu();
1950     }
1951 
1952     preempt_enable();
1953 #endif
1954     return err;
1955 }
1956 /*
1957  * Determine which stack to use..
1958  */
1959 static inline void *
1960 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
1961 {
1962     unsigned long sp;
1963 
1964     /* Default to using normal stack */
1965     sp = regs->gpr[29][regs->current_tc];
1966 
1967     /*
1968      * FPU emulator may have it's own trampoline active just
1969      * above the user stack, 16-bytes before the next lowest
1970      * 16 byte boundary.  Try to avoid trashing it.
1971      */
1972     sp -= 32;
1973 
1974     /* This is the X/Open sanctioned signal stack switching.  */
1975     if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
1976         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1977     }
1978 
1979     return g2h((sp - frame_size) & ~7);
1980 }
1981 
1982 static void setup_frame(int sig, struct emulated_sigaction * ka,
1983    		target_sigset_t *set, CPUState *regs)
1984 {
1985     struct sigframe *frame;
1986     int i;
1987 
1988     frame = get_sigframe(ka, regs, sizeof(*frame));
1989     if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
1990 	goto give_sigsegv;
1991 
1992     install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
1993 
1994     if(setup_sigcontext(regs, &frame->sf_sc))
1995 	goto give_sigsegv;
1996 
1997     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1998 	if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
1999 	    goto give_sigsegv;
2000     }
2001 
2002     /*
2003     * Arguments to signal handler:
2004     *
2005     *   a0 = signal number
2006     *   a1 = 0 (should be cause)
2007     *   a2 = pointer to struct sigcontext
2008     *
2009     * $25 and PC point to the signal handler, $29 points to the
2010     * struct sigframe.
2011     */
2012     regs->gpr[ 4][regs->current_tc] = sig;
2013     regs->gpr[ 5][regs->current_tc] = 0;
2014     regs->gpr[ 6][regs->current_tc] = h2g(&frame->sf_sc);
2015     regs->gpr[29][regs->current_tc] = h2g(frame);
2016     regs->gpr[31][regs->current_tc] = h2g(frame->sf_code);
2017     /* The original kernel code sets CP0_EPC to the handler
2018     * since it returns to userland using eret
2019     * we cannot do this here, and we must set PC directly */
2020     regs->PC[regs->current_tc] = regs->gpr[25][regs->current_tc] = ka->sa._sa_handler;
2021     return;
2022 
2023 give_sigsegv:
2024     force_sig(TARGET_SIGSEGV/*, current*/);
2025     return;
2026 }
2027 
2028 long do_sigreturn(CPUState *regs)
2029 {
2030     struct sigframe *frame;
2031     sigset_t blocked;
2032     target_sigset_t target_set;
2033     int i;
2034 
2035 #if defined(DEBUG_SIGNAL)
2036     fprintf(stderr, "do_sigreturn\n");
2037 #endif
2038     frame = (struct sigframe *) regs->gpr[29][regs->current_tc];
2039     if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
2040    	goto badframe;
2041 
2042     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2043    	if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2044 	    goto badframe;
2045     }
2046 
2047     target_to_host_sigset_internal(&blocked, &target_set);
2048     sigprocmask(SIG_SETMASK, &blocked, NULL);
2049 
2050     if (restore_sigcontext(regs, &frame->sf_sc))
2051    	goto badframe;
2052 
2053 #if 0
2054     /*
2055      * Don't let your children do this ...
2056      */
2057     __asm__ __volatile__(
2058    	"move\t$29, %0\n\t"
2059    	"j\tsyscall_exit"
2060    	:/* no outputs */
2061    	:"r" (&regs));
2062     /* Unreached */
2063 #endif
2064 
2065     regs->PC[regs->current_tc] = regs->CP0_EPC;
2066     /* I am not sure this is right, but it seems to work
2067     * maybe a problem with nested signals ? */
2068     regs->CP0_EPC = 0;
2069     return 0;
2070 
2071 badframe:
2072     force_sig(TARGET_SIGSEGV/*, current*/);
2073     return 0;
2074 }
2075 
2076 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2077                            target_siginfo_t *info,
2078 			   target_sigset_t *set, CPUState *env)
2079 {
2080     fprintf(stderr, "setup_rt_frame: not implemented\n");
2081 }
2082 
2083 long do_rt_sigreturn(CPUState *env)
2084 {
2085     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2086     return -ENOSYS;
2087 }
2088 
2089 #else
2090 
2091 static void setup_frame(int sig, struct emulated_sigaction *ka,
2092 			target_sigset_t *set, CPUState *env)
2093 {
2094     fprintf(stderr, "setup_frame: not implemented\n");
2095 }
2096 
2097 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2098                            target_siginfo_t *info,
2099 			   target_sigset_t *set, CPUState *env)
2100 {
2101     fprintf(stderr, "setup_rt_frame: not implemented\n");
2102 }
2103 
2104 long do_sigreturn(CPUState *env)
2105 {
2106     fprintf(stderr, "do_sigreturn: not implemented\n");
2107     return -ENOSYS;
2108 }
2109 
2110 long do_rt_sigreturn(CPUState *env)
2111 {
2112     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2113     return -ENOSYS;
2114 }
2115 
2116 #endif
2117 
2118 void process_pending_signals(void *cpu_env)
2119 {
2120     int sig;
2121     target_ulong handler;
2122     sigset_t set, old_set;
2123     target_sigset_t target_old_set;
2124     struct emulated_sigaction *k;
2125     struct sigqueue *q;
2126 
2127     if (!signal_pending)
2128         return;
2129 
2130     k = sigact_table;
2131     for(sig = 1; sig <= TARGET_NSIG; sig++) {
2132         if (k->pending)
2133             goto handle_signal;
2134         k++;
2135     }
2136     /* if no signal is pending, just return */
2137     signal_pending = 0;
2138     return;
2139 
2140  handle_signal:
2141 #ifdef DEBUG_SIGNAL
2142     fprintf(stderr, "qemu: process signal %d\n", sig);
2143 #endif
2144     /* dequeue signal */
2145     q = k->first;
2146     k->first = q->next;
2147     if (!k->first)
2148         k->pending = 0;
2149 
2150     sig = gdb_handlesig (cpu_env, sig);
2151     if (!sig) {
2152         fprintf (stderr, "Lost signal\n");
2153         abort();
2154     }
2155 
2156     handler = k->sa._sa_handler;
2157     if (handler == TARGET_SIG_DFL) {
2158         /* default handler : ignore some signal. The other are fatal */
2159         if (sig != TARGET_SIGCHLD &&
2160             sig != TARGET_SIGURG &&
2161             sig != TARGET_SIGWINCH) {
2162             force_sig(sig);
2163         }
2164     } else if (handler == TARGET_SIG_IGN) {
2165         /* ignore sig */
2166     } else if (handler == TARGET_SIG_ERR) {
2167         force_sig(sig);
2168     } else {
2169         /* compute the blocked signals during the handler execution */
2170         target_to_host_sigset(&set, &k->sa.sa_mask);
2171         /* SA_NODEFER indicates that the current signal should not be
2172            blocked during the handler */
2173         if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
2174             sigaddset(&set, target_to_host_signal(sig));
2175 
2176         /* block signals in the handler using Linux */
2177         sigprocmask(SIG_BLOCK, &set, &old_set);
2178         /* save the previous blocked signal state to restore it at the
2179            end of the signal execution (see do_sigreturn) */
2180         host_to_target_sigset_internal(&target_old_set, &old_set);
2181 
2182         /* if the CPU is in VM86 mode, we restore the 32 bit values */
2183 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
2184         {
2185             CPUX86State *env = cpu_env;
2186             if (env->eflags & VM_MASK)
2187                 save_v86_state(env);
2188         }
2189 #endif
2190         /* prepare the stack frame of the virtual CPU */
2191         if (k->sa.sa_flags & TARGET_SA_SIGINFO)
2192             setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
2193         else
2194             setup_frame(sig, k, &target_old_set, cpu_env);
2195 	if (k->sa.sa_flags & TARGET_SA_RESETHAND)
2196             k->sa._sa_handler = TARGET_SIG_DFL;
2197     }
2198     if (q != &k->info)
2199         free_sigqueue(q);
2200 }
2201