initial sparc64 support
[qemu] / linux-user / signal.c
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 #ifdef __ia64__
30 #undef uc_mcontext
31 #undef uc_sigmask
32 #undef uc_stack
33 #undef uc_link
34 #endif 
35
36 #include "qemu.h"
37
38 //#define DEBUG_SIGNAL
39
40 #define MAX_SIGQUEUE_SIZE 1024
41
42 struct sigqueue {
43     struct sigqueue *next;
44     target_siginfo_t info;
45 };
46
47 struct emulated_sigaction {
48     struct target_sigaction sa;
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 */
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 host_to_target_signal(int sig)
103 {
104     return host_to_target_signal_table[sig];
105 }
106
107 static inline int target_to_host_signal(int sig)
108 {
109     return target_to_host_signal_table[sig];
110 }
111
112 static void host_to_target_sigset_internal(target_sigset_t *d, 
113                                            const sigset_t *s)
114 {
115     int i;
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
126     d->sig[0] = target_sigmask;
127     for(i = 1;i < TARGET_NSIG_WORDS; i++) {
128         d->sig[i] = ((unsigned long *)s)[i];
129     }
130 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
131     d->sig[0] = target_sigmask;
132     d->sig[1] = sigmask >> 32;
133 #else
134 #warning host_to_target_sigset
135 #endif
136 }
137
138 void 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
148 void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
149 {
150     int i;
151     unsigned long sigmask;
152     target_ulong target_sigmask;
153
154     target_sigmask = s->sig[0];
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++) {
163         ((unsigned long *)d)[i] = s->sig[i];
164     }
165 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
166     ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
167 #else
168 #warning target_to_host_sigset
169 #endif /* TARGET_LONG_BITS */
170 }
171
172 void 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     
182 void host_to_target_old_sigset(target_ulong *old_sigset, 
183                                const sigset_t *sigset)
184 {
185     target_sigset_t d;
186     host_to_target_sigset(&d, sigset);
187     *old_sigset = d.sig[0];
188 }
189
190 void target_to_host_old_sigset(sigset_t *sigset, 
191                                const target_ulong *old_sigset)
192 {
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);
200 }
201
202 /* siginfo conversion */
203
204 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, 
205                                                  const siginfo_t *info)
206 {
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;
212     if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || 
213         sig == SIGBUS || sig == SIGTRAP) {
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
226 static 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);
232     tinfo->si_errno = tswap32(info->si_errno);
233     tinfo->si_code = tswap32(info->si_code);
234     if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || 
235         sig == SIGBUS || sig == SIGTRAP) {
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
247 void 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);
251 }
252
253 /* XXX: we support only POSIX RT signals are used. */
254 /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
255 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
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);
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);
264 }
265
266 void signal_init(void)
267 {
268     struct sigaction act;
269     int i, j;
270
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         
281     /* set all host signal handlers. ALL signals are blocked during
282        the handlers to serialize them. */
283     sigfillset(&act.sa_mask);
284     act.sa_flags = SA_SIGINFO;
285     act.sa_sigaction = host_signal_handler;
286     for(i = 1; i < NSIG; i++) {
287         sigaction(i, &act, NULL);
288     }
289     
290     memset(sigact_table, 0, sizeof(sigact_table));
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
300 static 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;
307 }
308
309 static inline void free_sigqueue(struct sigqueue *q)
310 {
311     q->next = first_free;
312     first_free = q;
313 }
314
315 /* abort execution with signal */
316 void __attribute((noreturn)) force_sig(int sig)
317 {
318     int host_sig;
319     host_sig = target_to_host_signal(sig);
320     fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n", 
321             sig, strsignal(host_sig));
322 #if 1
323     _exit(-host_sig);
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
334 }
335
336 /* queue a signal so that it will be send to the virtual CPU as soon
337    as possible */
338 int queue_signal(int sig, target_siginfo_t *info)
339 {
340     struct emulated_sigaction *k;
341     struct sigqueue *q, **pq;
342     target_ulong handler;
343
344 #if defined(DEBUG_SIGNAL)
345     fprintf(stderr, "queue_signal: sig=%d\n", 
346             sig);
347 #endif
348     k = &sigact_table[sig - 1];
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);
356         } else {
357             return 0; /* indicate ignored */
358         }
359     } else if (handler == TARGET_SIG_IGN) {
360         /* ignore signal */
361         return 0;
362     } else if (handler == TARGET_SIG_ERR) {
363         force_sig(sig);
364     } else {
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
394 static 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 */
402     if (host_signum == SIGSEGV || host_signum == SIGBUS 
403 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
404         || host_signum == SIGFPE
405 #endif
406         ) {
407         if (cpu_signal_handler(host_signum, info, puc))
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)
416     fprintf(stderr, "qemu: got signal %d\n", sig);
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 */
421         cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
422     }
423 }
424
425 int do_sigaction(int sig, const struct target_sigaction *act,
426                  struct target_sigaction *oact)
427 {
428     struct emulated_sigaction *k;
429     struct sigaction act1;
430     int host_sig;
431
432     if (sig < 1 || sig > TARGET_NSIG)
433         return -EINVAL;
434     k = &sigact_table[sig - 1];
435 #if defined(DEBUG_SIGNAL)
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;
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         }
470     }
471     return 0;
472 }
473
474 #ifndef offsetof
475 #define offsetof(type, field) ((size_t) &((type *)0)->field)
476 #endif
477
478 static 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
485 #ifdef TARGET_I386
486
487 /* from the Linux kernel */
488
489 struct target_fpreg {
490         uint16_t significand[4];
491         uint16_t exponent;
492 };
493
494 struct target_fpxreg {
495         uint16_t significand[4];
496         uint16_t exponent;
497         uint16_t padding[3];
498 };
499
500 struct target_xmmreg {
501         target_ulong element[4];
502 };
503
504 struct 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
528 struct 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
553 typedef struct target_sigaltstack {
554         target_ulong ss_sp;
555         int ss_flags;
556         target_ulong ss_size;
557 } target_stack_t;
558
559 struct 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
567 struct 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
577 struct 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
593 /* XXX: save x87 state */
594 static int
595 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
596                  CPUX86State *env, unsigned long mask)
597 {
598         int err = 0;
599
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);
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);
612         err |= __put_user(env->exception_index, &sc->trapno);
613         err |= __put_user(env->error_code, &sc->err);
614         err |= __put_user(env->eip, &sc->eip);
615         err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
616         err |= __put_user(env->eflags, &sc->eflags);
617         err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
618         err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
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
625         /* non-iBCS2 extensions.. */
626         err |= __put_user(mask, &sc->oldmask);
627         err |= __put_user(env->cr[2], &sc->cr2);
628         return err;
629 }
630
631 /*
632  * Determine which stack to use..
633  */
634
635 static inline void *
636 get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
637 {
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. */
650         else 
651 #endif
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);
658 }
659
660 static void setup_frame(int sig, struct emulated_sigaction *ka,
661                         target_sigset_t *set, CPUX86State *env)
662 {
663         struct sigframe *frame;
664         int i, err = 0;
665
666         frame = get_sigframe(ka, env, sizeof(*frame));
667
668         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
669                 goto give_sigsegv;
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
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         }
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
715 give_sigsegv:
716         if (sig == TARGET_SIGSEGV)
717                 ka->sa._sa_handler = TARGET_SIG_DFL;
718         force_sig(TARGET_SIGSEGV /* , current */);
719 }
720
721 static void setup_rt_frame(int sig, struct emulated_sigaction *ka, 
722                            target_siginfo_t *info,
723                            target_sigset_t *set, CPUX86State *env)
724 {
725         struct rt_sigframe *frame;
726         int i, err = 0;
727
728         frame = get_sigframe(ka, env, sizeof(*frame));
729
730         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
731                 goto give_sigsegv;
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;
744
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]);
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         }
758
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
786 give_sigsegv:
787         if (sig == TARGET_SIGSEGV)
788                 ka->sa._sa_handler = TARGET_SIG_DFL;
789         force_sig(TARGET_SIGSEGV /* , current */);
790 }
791
792 static int
793 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
794 {
795         unsigned int err = 0;
796
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
821         {
822                 struct _fpstate * buf;
823                 buf = (void *)ldl(&sc->fpstate);
824                 if (buf) {
825 #if 0
826                         if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
827                                 goto badframe;
828 #endif
829                         cpu_x86_frstor(env, (void *)buf, 1);
830                 }
831         }
832
833         *peax = ldl(&sc->eax);
834         return err;
835 #if 0
836 badframe:
837         return 1;
838 #endif
839 }
840
841 long 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
848 #if defined(DEBUG_SIGNAL)
849     fprintf(stderr, "do_sigreturn\n");
850 #endif
851     /* set blocked signals */
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     }
858
859     target_to_host_sigset_internal(&set, &target_set);
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
867 badframe:
868     force_sig(TARGET_SIGSEGV);
869     return 0;
870 }
871
872 long do_rt_sigreturn(CPUX86State *env)
873 {
874         struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
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
883         target_to_host_sigset(&set, &frame->uc.uc_sigmask);
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
898 badframe:
899         force_sig(TARGET_SIGSEGV);
900         return 0;
901 }
902
903 #elif defined(TARGET_ARM)
904
905 struct 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
929 typedef struct target_sigaltstack {
930         target_ulong ss_sp;
931         int ss_flags;
932         target_ulong ss_size;
933 } target_stack_t;
934
935 struct 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
943 struct sigframe
944 {
945     struct target_sigcontext sc;
946     target_ulong extramask[TARGET_NSIG_WORDS-1];
947     target_ulong retcode;
948 };
949
950 struct 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
974 static 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
983 static inline int valid_user_regs(CPUState *regs)
984 {
985     return 1;
986 }
987
988 static int
989 setup_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
1022 static inline void *
1023 get_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
1040 static int
1041 setup_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
1103 static 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));
1107         int i, err = 0;
1108
1109         err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1110
1111         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1112             if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1113                 return;
1114         }
1115
1116         if (err == 0)
1117             err = setup_return(regs, ka, &frame->retcode, frame, usig);
1118         //      return err;
1119 }
1120
1121 static 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));
1126         int i, err = 0;
1127
1128         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
1129             return /* 1 */;
1130
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]);
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         }
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
1161 static int
1162 restore_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
1191 long do_sigreturn(CPUState *env)
1192 {
1193         struct sigframe *frame;
1194         target_sigset_t set;
1195         sigset_t host_set;
1196         int i;
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
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         }
1218
1219         target_to_host_sigset_internal(&host_set, &set);
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
1232 badframe:
1233         force_sig(SIGSEGV /* , current */);
1234         return 0;
1235 }
1236
1237 long do_rt_sigreturn(CPUState *env)
1238 {
1239         struct rt_sigframe *frame;
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
1256         target_to_host_sigset(&host_set, &frame->uc.uc_sigmask);
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
1269 badframe:
1270         force_sig(SIGSEGV /* , current */);
1271         return 0;
1272 }
1273
1274 #elif defined(TARGET_SPARC)
1275
1276 #define __SUNOS_MAXWIN   31
1277
1278 /* This is what SunOS does, so shall I. */
1279 struct target_sigcontext {
1280         target_ulong sigc_onstack;      /* state to restore */
1281
1282         target_ulong sigc_mask;         /* sigmask to restore */
1283         target_ulong sigc_sp;           /* stack pointer */
1284         target_ulong sigc_pc;           /* program counter */
1285         target_ulong sigc_npc;          /* next program counter */
1286         target_ulong sigc_psr;          /* for condition codes etc */
1287         target_ulong sigc_g1;           /* User uses these two registers */
1288         target_ulong sigc_o0;           /* within the trampoline code. */
1289
1290         /* Now comes information regarding the users window set
1291          * at the time of the signal.
1292          */
1293         target_ulong sigc_oswins;       /* outstanding windows */
1294
1295         /* stack ptrs for each regwin buf */
1296         char *sigc_spbuf[__SUNOS_MAXWIN];
1297
1298         /* Windows to restore after signal */
1299         struct {
1300                 target_ulong locals[8];
1301                 target_ulong ins[8];
1302         } sigc_wbuf[__SUNOS_MAXWIN];
1303 };
1304 /* A Sparc stack frame */
1305 struct sparc_stackf {
1306         target_ulong locals[8];
1307         target_ulong ins[6];
1308         struct sparc_stackf *fp;
1309         target_ulong callers_pc;
1310         char *structptr;
1311         target_ulong xargs[6];
1312         target_ulong xxargs[1];
1313 };
1314
1315 typedef struct {
1316         struct {
1317                 target_ulong psr;
1318                 target_ulong pc;
1319                 target_ulong npc;
1320                 target_ulong y;
1321                 target_ulong u_regs[16]; /* globals and ins */
1322         }               si_regs;
1323         int             si_mask;
1324 } __siginfo_t;
1325
1326 typedef struct {
1327         unsigned   long si_float_regs [32];
1328         unsigned   long si_fsr;
1329         unsigned   long si_fpqdepth;
1330         struct {
1331                 unsigned long *insn_addr;
1332                 unsigned long insn;
1333         } si_fpqueue [16];
1334 } __siginfo_fpu_t;
1335
1336
1337 struct target_signal_frame {
1338         struct sparc_stackf     ss;
1339         __siginfo_t             info;
1340         __siginfo_fpu_t         *fpu_save;
1341         target_ulong            insns[2] __attribute__ ((aligned (8)));
1342         target_ulong            extramask[TARGET_NSIG_WORDS - 1];
1343         target_ulong            extra_size; /* Should be 0 */
1344         __siginfo_fpu_t         fpu_state;
1345 };
1346 struct target_rt_signal_frame {
1347         struct sparc_stackf     ss;
1348         siginfo_t               info;
1349         target_ulong            regs[20];
1350         sigset_t                mask;
1351         __siginfo_fpu_t         *fpu_save;
1352         unsigned int            insns[2];
1353         stack_t                 stack;
1354         unsigned int            extra_size; /* Should be 0 */
1355         __siginfo_fpu_t         fpu_state;
1356 };
1357
1358 #define UREG_O0        16
1359 #define UREG_O6        22
1360 #define UREG_I0        0
1361 #define UREG_I1        1
1362 #define UREG_I2        2
1363 #define UREG_I6        6
1364 #define UREG_I7        7
1365 #define UREG_L0        8
1366 #define UREG_FP        UREG_I6
1367 #define UREG_SP        UREG_O6
1368
1369 static inline void *get_sigframe(struct emulated_sigaction *sa, CPUState *env, unsigned long framesize)
1370 {
1371         unsigned long sp;
1372
1373         sp = env->regwptr[UREG_FP];
1374 #if 0
1375
1376         /* This is the X/Open sanctioned signal stack switching.  */
1377         if (sa->sa_flags & TARGET_SA_ONSTACK) {
1378                 if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
1379                         sp = current->sas_ss_sp + current->sas_ss_size;
1380         }
1381 #endif
1382         return (void *)(sp - framesize);
1383 }
1384
1385 static int
1386 setup___siginfo(__siginfo_t *si, CPUState *env, target_ulong mask)
1387 {
1388         int err = 0, i;
1389
1390         err |= __put_user(env->psr, &si->si_regs.psr);
1391         err |= __put_user(env->pc, &si->si_regs.pc);
1392         err |= __put_user(env->npc, &si->si_regs.npc);
1393         err |= __put_user(env->y, &si->si_regs.y);
1394         for (i=0; i < 8; i++) {
1395                 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1396         }
1397         for (i=0; i < 8; i++) {
1398                 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1399         }
1400         err |= __put_user(mask, &si->si_mask);
1401         return err;
1402 }
1403
1404 #if 0
1405 static int
1406 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1407                  CPUState *env, unsigned long mask)
1408 {
1409         int err = 0;
1410
1411         err |= __put_user(mask, &sc->sigc_mask);
1412         err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1413         err |= __put_user(env->pc, &sc->sigc_pc);
1414         err |= __put_user(env->npc, &sc->sigc_npc);
1415         err |= __put_user(env->psr, &sc->sigc_psr);
1416         err |= __put_user(env->gregs[1], &sc->sigc_g1);
1417         err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1418
1419         return err;
1420 }
1421 #endif
1422 #define NF_ALIGNEDSZ  (((sizeof(struct target_signal_frame) + 7) & (~7)))
1423
1424 static void setup_frame(int sig, struct emulated_sigaction *ka,
1425                         target_sigset_t *set, CPUState *env)
1426 {
1427         struct target_signal_frame *sf;
1428         int sigframe_size, err, i;
1429
1430         /* 1. Make sure everything is clean */
1431         //synchronize_user_stack();
1432
1433         sigframe_size = NF_ALIGNEDSZ;
1434
1435         sf = (struct target_signal_frame *)
1436                 get_sigframe(ka, env, sigframe_size);
1437
1438         //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1439 #if 0
1440         if (invalid_frame_pointer(sf, sigframe_size))
1441                 goto sigill_and_return;
1442 #endif
1443         /* 2. Save the current process state */
1444         err = setup___siginfo(&sf->info, env, set->sig[0]);
1445         err |= __put_user(0, &sf->extra_size);
1446
1447         //err |= save_fpu_state(regs, &sf->fpu_state);
1448         //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1449
1450         err |= __put_user(set->sig[0], &sf->info.si_mask);
1451         for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1452                 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1453         }
1454
1455         for (i = 0; i < 8; i++) {
1456                 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1457         }
1458         for (i = 0; i < 8; i++) {
1459                 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1460         }
1461         if (err)
1462                 goto sigsegv;
1463
1464         /* 3. signal handler back-trampoline and parameters */
1465         env->regwptr[UREG_FP] = (target_ulong) sf;
1466         env->regwptr[UREG_I0] = sig;
1467         env->regwptr[UREG_I1] = (target_ulong) &sf->info;
1468         env->regwptr[UREG_I2] = (target_ulong) &sf->info;
1469
1470         /* 4. signal handler */
1471         env->pc = (unsigned long) ka->sa._sa_handler;
1472         env->npc = (env->pc + 4);
1473         /* 5. return to kernel instructions */
1474         if (ka->sa.sa_restorer)
1475                 env->regwptr[UREG_I7] = (unsigned long)ka->sa.sa_restorer;
1476         else {
1477                 env->regwptr[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
1478
1479                 /* mov __NR_sigreturn, %g1 */
1480                 err |= __put_user(0x821020d8, &sf->insns[0]);
1481
1482                 /* t 0x10 */
1483                 err |= __put_user(0x91d02010, &sf->insns[1]);
1484                 if (err)
1485                         goto sigsegv;
1486
1487                 /* Flush instruction space. */
1488                 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1489                 //              tb_flush(env);
1490         }
1491         return;
1492
1493         //sigill_and_return:
1494         force_sig(TARGET_SIGILL);
1495 sigsegv:
1496         //fprintf(stderr, "force_sig\n");
1497         force_sig(TARGET_SIGSEGV);
1498 }
1499 static inline int
1500 restore_fpu_state(CPUState *env, __siginfo_fpu_t *fpu)
1501 {
1502         int err;
1503 #if 0
1504 #ifdef CONFIG_SMP
1505         if (current->flags & PF_USEDFPU)
1506                 regs->psr &= ~PSR_EF;
1507 #else
1508         if (current == last_task_used_math) {
1509                 last_task_used_math = 0;
1510                 regs->psr &= ~PSR_EF;
1511         }
1512 #endif
1513         current->used_math = 1;
1514         current->flags &= ~PF_USEDFPU;
1515 #endif
1516 #if 0
1517         if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1518                 return -EFAULT;
1519 #endif
1520
1521         err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1522                                      (sizeof(unsigned long) * 32));
1523         err |= __get_user(env->fsr, &fpu->si_fsr);
1524 #if 0
1525         err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1526         if (current->thread.fpqdepth != 0)
1527                 err |= __copy_from_user(&current->thread.fpqueue[0],
1528                                         &fpu->si_fpqueue[0],
1529                                         ((sizeof(unsigned long) +
1530                                         (sizeof(unsigned long *)))*16));
1531 #endif
1532         return err;
1533 }
1534
1535
1536 static void setup_rt_frame(int sig, struct emulated_sigaction *ka, 
1537                            target_siginfo_t *info,
1538                            target_sigset_t *set, CPUState *env)
1539 {
1540     fprintf(stderr, "setup_rt_frame: not implemented\n");
1541 }
1542
1543 long do_sigreturn(CPUState *env)
1544 {
1545         struct target_signal_frame *sf;
1546         uint32_t up_psr, pc, npc;
1547         target_sigset_t set;
1548         sigset_t host_set;
1549         target_ulong fpu_save;
1550         int err, i;
1551
1552         sf = (struct target_signal_frame *) env->regwptr[UREG_FP];
1553 #if 0
1554         fprintf(stderr, "sigreturn\n");
1555         fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1556 #endif
1557         //cpu_dump_state(env, stderr, fprintf, 0);
1558
1559         /* 1. Make sure we are not getting garbage from the user */
1560 #if 0
1561         if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
1562                 goto segv_and_exit;
1563 #endif
1564
1565         if (((uint) sf) & 3)
1566                 goto segv_and_exit;
1567
1568         err = __get_user(pc,  &sf->info.si_regs.pc);
1569         err |= __get_user(npc, &sf->info.si_regs.npc);
1570
1571         if ((pc | npc) & 3)
1572                 goto segv_and_exit;
1573
1574         /* 2. Restore the state */
1575         err |= __get_user(up_psr, &sf->info.si_regs.psr);
1576
1577         /* User can only change condition codes and FPU enabling in %psr. */
1578         env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1579                   | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1580
1581         env->pc = pc;
1582         env->npc = npc;
1583         err |= __get_user(env->y, &sf->info.si_regs.y);
1584         for (i=0; i < 8; i++) {
1585                 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1586         }
1587         for (i=0; i < 8; i++) {
1588                 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1589         }
1590
1591         err |= __get_user(fpu_save, (target_ulong *)&sf->fpu_save);
1592
1593         //if (fpu_save)
1594         //        err |= restore_fpu_state(env, fpu_save);
1595
1596         /* This is pretty much atomic, no amount locking would prevent
1597          * the races which exist anyways.
1598          */
1599         err |= __get_user(set.sig[0], &sf->info.si_mask);
1600         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1601             err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1602         }
1603
1604         target_to_host_sigset_internal(&host_set, &set);
1605         sigprocmask(SIG_SETMASK, &host_set, NULL);
1606
1607         if (err)
1608                 goto segv_and_exit;
1609
1610         return env->regwptr[0];
1611
1612 segv_and_exit:
1613         force_sig(TARGET_SIGSEGV);
1614 }
1615
1616 long do_rt_sigreturn(CPUState *env)
1617 {
1618     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1619     return -ENOSYS;
1620 }
1621
1622
1623 #else
1624
1625 static void setup_frame(int sig, struct emulated_sigaction *ka,
1626                         target_sigset_t *set, CPUState *env)
1627 {
1628     fprintf(stderr, "setup_frame: not implemented\n");
1629 }
1630
1631 static void setup_rt_frame(int sig, struct emulated_sigaction *ka, 
1632                            target_siginfo_t *info,
1633                            target_sigset_t *set, CPUState *env)
1634 {
1635     fprintf(stderr, "setup_rt_frame: not implemented\n");
1636 }
1637
1638 long do_sigreturn(CPUState *env)
1639 {
1640     fprintf(stderr, "do_sigreturn: not implemented\n");
1641     return -ENOSYS;
1642 }
1643
1644 long do_rt_sigreturn(CPUState *env)
1645 {
1646     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1647     return -ENOSYS;
1648 }
1649
1650 #endif
1651
1652 void process_pending_signals(void *cpu_env)
1653 {
1654     int sig;
1655     target_ulong handler;
1656     sigset_t set, old_set;
1657     target_sigset_t target_old_set;
1658     struct emulated_sigaction *k;
1659     struct sigqueue *q;
1660     
1661     if (!signal_pending)
1662         return;
1663
1664     k = sigact_table;
1665     for(sig = 1; sig <= TARGET_NSIG; sig++) {
1666         if (k->pending)
1667             goto handle_signal;
1668         k++;
1669     }
1670     /* if no signal is pending, just return */
1671     signal_pending = 0;
1672     return;
1673
1674  handle_signal:
1675 #ifdef DEBUG_SIGNAL
1676     fprintf(stderr, "qemu: process signal %d\n", sig);
1677 #endif
1678     /* dequeue signal */
1679     q = k->first;
1680     k->first = q->next;
1681     if (!k->first)
1682         k->pending = 0;
1683
1684     handler = k->sa._sa_handler;
1685     if (handler == TARGET_SIG_DFL) {
1686         /* default handler : ignore some signal. The other are fatal */
1687         if (sig != TARGET_SIGCHLD && 
1688             sig != TARGET_SIGURG && 
1689             sig != TARGET_SIGWINCH) {
1690             force_sig(sig);
1691         }
1692     } else if (handler == TARGET_SIG_IGN) {
1693         /* ignore sig */
1694     } else if (handler == TARGET_SIG_ERR) {
1695         force_sig(sig);
1696     } else {
1697         /* compute the blocked signals during the handler execution */
1698         target_to_host_sigset(&set, &k->sa.sa_mask);
1699         /* SA_NODEFER indicates that the current signal should not be
1700            blocked during the handler */
1701         if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
1702             sigaddset(&set, target_to_host_signal(sig));
1703         
1704         /* block signals in the handler using Linux */
1705         sigprocmask(SIG_BLOCK, &set, &old_set);
1706         /* save the previous blocked signal state to restore it at the
1707            end of the signal execution (see do_sigreturn) */
1708         host_to_target_sigset_internal(&target_old_set, &old_set);
1709
1710         /* if the CPU is in VM86 mode, we restore the 32 bit values */
1711 #ifdef TARGET_I386
1712         {
1713             CPUX86State *env = cpu_env;
1714             if (env->eflags & VM_MASK)
1715                 save_v86_state(env);
1716         }
1717 #endif
1718         /* prepare the stack frame of the virtual CPU */
1719         if (k->sa.sa_flags & TARGET_SA_SIGINFO)
1720             setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
1721         else
1722             setup_frame(sig, k, &target_old_set, cpu_env);
1723         if (k->sa.sa_flags & TARGET_SA_RESETHAND)
1724             k->sa._sa_handler = TARGET_SIG_DFL;
1725     }
1726     if (q != &k->info)
1727         free_sigqueue(q);
1728 }
1729
1730