xref: /openbmc/qemu/linux-user/signal.c (revision 1b111dc1)
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, see <http://www.gnu.org/licenses/>.
18  */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <sys/ucontext.h>
27 #include <sys/resource.h>
28 
29 #include "qemu.h"
30 #include "qemu-common.h"
31 #include "target_signal.h"
32 
33 //#define DEBUG_SIGNAL
34 
35 static struct target_sigaltstack target_sigaltstack_used = {
36     .ss_sp = 0,
37     .ss_size = 0,
38     .ss_flags = TARGET_SS_DISABLE,
39 };
40 
41 static struct target_sigaction sigact_table[TARGET_NSIG];
42 
43 static void host_signal_handler(int host_signum, siginfo_t *info,
44                                 void *puc);
45 
46 static uint8_t host_to_target_signal_table[_NSIG] = {
47     [SIGHUP] = TARGET_SIGHUP,
48     [SIGINT] = TARGET_SIGINT,
49     [SIGQUIT] = TARGET_SIGQUIT,
50     [SIGILL] = TARGET_SIGILL,
51     [SIGTRAP] = TARGET_SIGTRAP,
52     [SIGABRT] = TARGET_SIGABRT,
53 /*    [SIGIOT] = TARGET_SIGIOT,*/
54     [SIGBUS] = TARGET_SIGBUS,
55     [SIGFPE] = TARGET_SIGFPE,
56     [SIGKILL] = TARGET_SIGKILL,
57     [SIGUSR1] = TARGET_SIGUSR1,
58     [SIGSEGV] = TARGET_SIGSEGV,
59     [SIGUSR2] = TARGET_SIGUSR2,
60     [SIGPIPE] = TARGET_SIGPIPE,
61     [SIGALRM] = TARGET_SIGALRM,
62     [SIGTERM] = TARGET_SIGTERM,
63 #ifdef SIGSTKFLT
64     [SIGSTKFLT] = TARGET_SIGSTKFLT,
65 #endif
66     [SIGCHLD] = TARGET_SIGCHLD,
67     [SIGCONT] = TARGET_SIGCONT,
68     [SIGSTOP] = TARGET_SIGSTOP,
69     [SIGTSTP] = TARGET_SIGTSTP,
70     [SIGTTIN] = TARGET_SIGTTIN,
71     [SIGTTOU] = TARGET_SIGTTOU,
72     [SIGURG] = TARGET_SIGURG,
73     [SIGXCPU] = TARGET_SIGXCPU,
74     [SIGXFSZ] = TARGET_SIGXFSZ,
75     [SIGVTALRM] = TARGET_SIGVTALRM,
76     [SIGPROF] = TARGET_SIGPROF,
77     [SIGWINCH] = TARGET_SIGWINCH,
78     [SIGIO] = TARGET_SIGIO,
79     [SIGPWR] = TARGET_SIGPWR,
80     [SIGSYS] = TARGET_SIGSYS,
81     /* next signals stay the same */
82     /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
83        host libpthread signals.  This assumes no one actually uses SIGRTMAX :-/
84        To fix this properly we need to do manual signal delivery multiplexed
85        over a single host signal.  */
86     [__SIGRTMIN] = __SIGRTMAX,
87     [__SIGRTMAX] = __SIGRTMIN,
88 };
89 static uint8_t target_to_host_signal_table[_NSIG];
90 
91 static inline int on_sig_stack(unsigned long sp)
92 {
93     return (sp - target_sigaltstack_used.ss_sp
94             < target_sigaltstack_used.ss_size);
95 }
96 
97 static inline int sas_ss_flags(unsigned long sp)
98 {
99     return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
100             : on_sig_stack(sp) ? SS_ONSTACK : 0);
101 }
102 
103 int host_to_target_signal(int sig)
104 {
105     if (sig < 0 || sig >= _NSIG)
106         return sig;
107     return host_to_target_signal_table[sig];
108 }
109 
110 int target_to_host_signal(int sig)
111 {
112     if (sig < 0 || sig >= _NSIG)
113         return sig;
114     return target_to_host_signal_table[sig];
115 }
116 
117 static inline void target_sigemptyset(target_sigset_t *set)
118 {
119     memset(set, 0, sizeof(*set));
120 }
121 
122 static inline void target_sigaddset(target_sigset_t *set, int signum)
123 {
124     signum--;
125     abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126     set->sig[signum / TARGET_NSIG_BPW] |= mask;
127 }
128 
129 static inline int target_sigismember(const target_sigset_t *set, int signum)
130 {
131     signum--;
132     abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
133     return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
134 }
135 
136 static void host_to_target_sigset_internal(target_sigset_t *d,
137                                            const sigset_t *s)
138 {
139     int i;
140     target_sigemptyset(d);
141     for (i = 1; i <= TARGET_NSIG; i++) {
142         if (sigismember(s, i)) {
143             target_sigaddset(d, host_to_target_signal(i));
144         }
145     }
146 }
147 
148 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
149 {
150     target_sigset_t d1;
151     int i;
152 
153     host_to_target_sigset_internal(&d1, s);
154     for(i = 0;i < TARGET_NSIG_WORDS; i++)
155         d->sig[i] = tswapal(d1.sig[i]);
156 }
157 
158 static void target_to_host_sigset_internal(sigset_t *d,
159                                            const target_sigset_t *s)
160 {
161     int i;
162     sigemptyset(d);
163     for (i = 1; i <= TARGET_NSIG; i++) {
164         if (target_sigismember(s, i)) {
165             sigaddset(d, target_to_host_signal(i));
166         }
167      }
168 }
169 
170 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
171 {
172     target_sigset_t s1;
173     int i;
174 
175     for(i = 0;i < TARGET_NSIG_WORDS; i++)
176         s1.sig[i] = tswapal(s->sig[i]);
177     target_to_host_sigset_internal(d, &s1);
178 }
179 
180 void host_to_target_old_sigset(abi_ulong *old_sigset,
181                                const sigset_t *sigset)
182 {
183     target_sigset_t d;
184     host_to_target_sigset(&d, sigset);
185     *old_sigset = d.sig[0];
186 }
187 
188 void target_to_host_old_sigset(sigset_t *sigset,
189                                const abi_ulong *old_sigset)
190 {
191     target_sigset_t d;
192     int i;
193 
194     d.sig[0] = *old_sigset;
195     for(i = 1;i < TARGET_NSIG_WORDS; i++)
196         d.sig[i] = 0;
197     target_to_host_sigset(sigset, &d);
198 }
199 
200 /* siginfo conversion */
201 
202 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
203                                                  const siginfo_t *info)
204 {
205     int sig = host_to_target_signal(info->si_signo);
206     tinfo->si_signo = sig;
207     tinfo->si_errno = 0;
208     tinfo->si_code = info->si_code;
209 
210     if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
211         || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
212         /* Should never come here, but who knows. The information for
213            the target is irrelevant.  */
214         tinfo->_sifields._sigfault._addr = 0;
215     } else if (sig == TARGET_SIGIO) {
216         tinfo->_sifields._sigpoll._band = info->si_band;
217 	tinfo->_sifields._sigpoll._fd = info->si_fd;
218     } else if (sig == TARGET_SIGCHLD) {
219         tinfo->_sifields._sigchld._pid = info->si_pid;
220         tinfo->_sifields._sigchld._uid = info->si_uid;
221         tinfo->_sifields._sigchld._status
222             = host_to_target_waitstatus(info->si_status);
223         tinfo->_sifields._sigchld._utime = info->si_utime;
224         tinfo->_sifields._sigchld._stime = info->si_stime;
225     } else if (sig >= TARGET_SIGRTMIN) {
226         tinfo->_sifields._rt._pid = info->si_pid;
227         tinfo->_sifields._rt._uid = info->si_uid;
228         /* XXX: potential problem if 64 bit */
229         tinfo->_sifields._rt._sigval.sival_ptr
230             = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
231     }
232 }
233 
234 static void tswap_siginfo(target_siginfo_t *tinfo,
235                           const target_siginfo_t *info)
236 {
237     int sig = info->si_signo;
238     tinfo->si_signo = tswap32(sig);
239     tinfo->si_errno = tswap32(info->si_errno);
240     tinfo->si_code = tswap32(info->si_code);
241 
242     if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
243         || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
244         tinfo->_sifields._sigfault._addr
245             = tswapal(info->_sifields._sigfault._addr);
246     } else if (sig == TARGET_SIGIO) {
247         tinfo->_sifields._sigpoll._band
248             = tswap32(info->_sifields._sigpoll._band);
249         tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
250     } else if (sig == TARGET_SIGCHLD) {
251         tinfo->_sifields._sigchld._pid
252             = tswap32(info->_sifields._sigchld._pid);
253         tinfo->_sifields._sigchld._uid
254             = tswap32(info->_sifields._sigchld._uid);
255         tinfo->_sifields._sigchld._status
256             = tswap32(info->_sifields._sigchld._status);
257         tinfo->_sifields._sigchld._utime
258             = tswapal(info->_sifields._sigchld._utime);
259         tinfo->_sifields._sigchld._stime
260             = tswapal(info->_sifields._sigchld._stime);
261     } else if (sig >= TARGET_SIGRTMIN) {
262         tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
263         tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
264         tinfo->_sifields._rt._sigval.sival_ptr
265             = tswapal(info->_sifields._rt._sigval.sival_ptr);
266     }
267 }
268 
269 
270 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
271 {
272     host_to_target_siginfo_noswap(tinfo, info);
273     tswap_siginfo(tinfo, tinfo);
274 }
275 
276 /* XXX: we support only POSIX RT signals are used. */
277 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
278 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
279 {
280     info->si_signo = tswap32(tinfo->si_signo);
281     info->si_errno = tswap32(tinfo->si_errno);
282     info->si_code = tswap32(tinfo->si_code);
283     info->si_pid = tswap32(tinfo->_sifields._rt._pid);
284     info->si_uid = tswap32(tinfo->_sifields._rt._uid);
285     info->si_value.sival_ptr =
286             (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
287 }
288 
289 static int fatal_signal (int sig)
290 {
291     switch (sig) {
292     case TARGET_SIGCHLD:
293     case TARGET_SIGURG:
294     case TARGET_SIGWINCH:
295         /* Ignored by default.  */
296         return 0;
297     case TARGET_SIGCONT:
298     case TARGET_SIGSTOP:
299     case TARGET_SIGTSTP:
300     case TARGET_SIGTTIN:
301     case TARGET_SIGTTOU:
302         /* Job control signals.  */
303         return 0;
304     default:
305         return 1;
306     }
307 }
308 
309 /* returns 1 if given signal should dump core if not handled */
310 static int core_dump_signal(int sig)
311 {
312     switch (sig) {
313     case TARGET_SIGABRT:
314     case TARGET_SIGFPE:
315     case TARGET_SIGILL:
316     case TARGET_SIGQUIT:
317     case TARGET_SIGSEGV:
318     case TARGET_SIGTRAP:
319     case TARGET_SIGBUS:
320         return (1);
321     default:
322         return (0);
323     }
324 }
325 
326 void signal_init(void)
327 {
328     struct sigaction act;
329     struct sigaction oact;
330     int i, j;
331     int host_sig;
332 
333     /* generate signal conversion tables */
334     for(i = 1; i < _NSIG; i++) {
335         if (host_to_target_signal_table[i] == 0)
336             host_to_target_signal_table[i] = i;
337     }
338     for(i = 1; i < _NSIG; i++) {
339         j = host_to_target_signal_table[i];
340         target_to_host_signal_table[j] = i;
341     }
342 
343     /* set all host signal handlers. ALL signals are blocked during
344        the handlers to serialize them. */
345     memset(sigact_table, 0, sizeof(sigact_table));
346 
347     sigfillset(&act.sa_mask);
348     act.sa_flags = SA_SIGINFO;
349     act.sa_sigaction = host_signal_handler;
350     for(i = 1; i <= TARGET_NSIG; i++) {
351         host_sig = target_to_host_signal(i);
352         sigaction(host_sig, NULL, &oact);
353         if (oact.sa_sigaction == (void *)SIG_IGN) {
354             sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
355         } else if (oact.sa_sigaction == (void *)SIG_DFL) {
356             sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
357         }
358         /* If there's already a handler installed then something has
359            gone horribly wrong, so don't even try to handle that case.  */
360         /* Install some handlers for our own use.  We need at least
361            SIGSEGV and SIGBUS, to detect exceptions.  We can not just
362            trap all signals because it affects syscall interrupt
363            behavior.  But do trap all default-fatal signals.  */
364         if (fatal_signal (i))
365             sigaction(host_sig, &act, NULL);
366     }
367 }
368 
369 /* signal queue handling */
370 
371 static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
372 {
373     TaskState *ts = env->opaque;
374     struct sigqueue *q = ts->first_free;
375     if (!q)
376         return NULL;
377     ts->first_free = q->next;
378     return q;
379 }
380 
381 static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
382 {
383     TaskState *ts = env->opaque;
384     q->next = ts->first_free;
385     ts->first_free = q;
386 }
387 
388 /* abort execution with signal */
389 static void QEMU_NORETURN force_sig(int target_sig)
390 {
391     CPUArchState *env = thread_cpu->env_ptr;
392     TaskState *ts = (TaskState *)env->opaque;
393     int host_sig, core_dumped = 0;
394     struct sigaction act;
395     host_sig = target_to_host_signal(target_sig);
396     gdb_signalled(env, target_sig);
397 
398     /* dump core if supported by target binary format */
399     if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
400         stop_all_tasks();
401         core_dumped =
402             ((*ts->bprm->core_dump)(target_sig, env) == 0);
403     }
404     if (core_dumped) {
405         /* we already dumped the core of target process, we don't want
406          * a coredump of qemu itself */
407         struct rlimit nodump;
408         getrlimit(RLIMIT_CORE, &nodump);
409         nodump.rlim_cur=0;
410         setrlimit(RLIMIT_CORE, &nodump);
411         (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
412             target_sig, strsignal(host_sig), "core dumped" );
413     }
414 
415     /* The proper exit code for dying from an uncaught signal is
416      * -<signal>.  The kernel doesn't allow exit() or _exit() to pass
417      * a negative value.  To get the proper exit code we need to
418      * actually die from an uncaught signal.  Here the default signal
419      * handler is installed, we send ourself a signal and we wait for
420      * it to arrive. */
421     sigfillset(&act.sa_mask);
422     act.sa_handler = SIG_DFL;
423     sigaction(host_sig, &act, NULL);
424 
425     /* For some reason raise(host_sig) doesn't send the signal when
426      * statically linked on x86-64. */
427     kill(getpid(), host_sig);
428 
429     /* Make sure the signal isn't masked (just reuse the mask inside
430     of act) */
431     sigdelset(&act.sa_mask, host_sig);
432     sigsuspend(&act.sa_mask);
433 
434     /* unreachable */
435     abort();
436 }
437 
438 /* queue a signal so that it will be send to the virtual CPU as soon
439    as possible */
440 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
441 {
442     TaskState *ts = env->opaque;
443     struct emulated_sigtable *k;
444     struct sigqueue *q, **pq;
445     abi_ulong handler;
446     int queue;
447 
448 #if defined(DEBUG_SIGNAL)
449     fprintf(stderr, "queue_signal: sig=%d\n",
450             sig);
451 #endif
452     k = &ts->sigtab[sig - 1];
453     queue = gdb_queuesig ();
454     handler = sigact_table[sig - 1]._sa_handler;
455     if (!queue && handler == TARGET_SIG_DFL) {
456         if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
457             kill(getpid(),SIGSTOP);
458             return 0;
459         } else
460         /* default handler : ignore some signal. The other are fatal */
461         if (sig != TARGET_SIGCHLD &&
462             sig != TARGET_SIGURG &&
463             sig != TARGET_SIGWINCH &&
464             sig != TARGET_SIGCONT) {
465             force_sig(sig);
466         } else {
467             return 0; /* indicate ignored */
468         }
469     } else if (!queue && handler == TARGET_SIG_IGN) {
470         /* ignore signal */
471         return 0;
472     } else if (!queue && handler == TARGET_SIG_ERR) {
473         force_sig(sig);
474     } else {
475         pq = &k->first;
476         if (sig < TARGET_SIGRTMIN) {
477             /* if non real time signal, we queue exactly one signal */
478             if (!k->pending)
479                 q = &k->info;
480             else
481                 return 0;
482         } else {
483             if (!k->pending) {
484                 /* first signal */
485                 q = &k->info;
486             } else {
487                 q = alloc_sigqueue(env);
488                 if (!q)
489                     return -EAGAIN;
490                 while (*pq != NULL)
491                     pq = &(*pq)->next;
492             }
493         }
494         *pq = q;
495         q->info = *info;
496         q->next = NULL;
497         k->pending = 1;
498         /* signal that a new signal is pending */
499         ts->signal_pending = 1;
500         return 1; /* indicates that the signal was queued */
501     }
502 }
503 
504 static void host_signal_handler(int host_signum, siginfo_t *info,
505                                 void *puc)
506 {
507     CPUArchState *env = thread_cpu->env_ptr;
508     int sig;
509     target_siginfo_t tinfo;
510 
511     /* the CPU emulator uses some host signals to detect exceptions,
512        we forward to it some signals */
513     if ((host_signum == SIGSEGV || host_signum == SIGBUS)
514         && info->si_code > 0) {
515         if (cpu_signal_handler(host_signum, info, puc))
516             return;
517     }
518 
519     /* get target signal number */
520     sig = host_to_target_signal(host_signum);
521     if (sig < 1 || sig > TARGET_NSIG)
522         return;
523 #if defined(DEBUG_SIGNAL)
524     fprintf(stderr, "qemu: got signal %d\n", sig);
525 #endif
526     host_to_target_siginfo_noswap(&tinfo, info);
527     if (queue_signal(env, sig, &tinfo) == 1) {
528         /* interrupt the virtual CPU as soon as possible */
529         cpu_exit(thread_cpu);
530     }
531 }
532 
533 /* do_sigaltstack() returns target values and errnos. */
534 /* compare linux/kernel/signal.c:do_sigaltstack() */
535 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
536 {
537     int ret;
538     struct target_sigaltstack oss;
539 
540     /* XXX: test errors */
541     if(uoss_addr)
542     {
543         __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
544         __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
545         __put_user(sas_ss_flags(sp), &oss.ss_flags);
546     }
547 
548     if(uss_addr)
549     {
550         struct target_sigaltstack *uss;
551         struct target_sigaltstack ss;
552 
553 	ret = -TARGET_EFAULT;
554         if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
555 	    || __get_user(ss.ss_sp, &uss->ss_sp)
556 	    || __get_user(ss.ss_size, &uss->ss_size)
557 	    || __get_user(ss.ss_flags, &uss->ss_flags))
558             goto out;
559         unlock_user_struct(uss, uss_addr, 0);
560 
561 	ret = -TARGET_EPERM;
562 	if (on_sig_stack(sp))
563             goto out;
564 
565 	ret = -TARGET_EINVAL;
566 	if (ss.ss_flags != TARGET_SS_DISABLE
567             && ss.ss_flags != TARGET_SS_ONSTACK
568             && ss.ss_flags != 0)
569             goto out;
570 
571 	if (ss.ss_flags == TARGET_SS_DISABLE) {
572             ss.ss_size = 0;
573             ss.ss_sp = 0;
574 	} else {
575             ret = -TARGET_ENOMEM;
576             if (ss.ss_size < MINSIGSTKSZ)
577                 goto out;
578 	}
579 
580         target_sigaltstack_used.ss_sp = ss.ss_sp;
581         target_sigaltstack_used.ss_size = ss.ss_size;
582     }
583 
584     if (uoss_addr) {
585         ret = -TARGET_EFAULT;
586         if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
587             goto out;
588     }
589 
590     ret = 0;
591 out:
592     return ret;
593 }
594 
595 /* do_sigaction() return host values and errnos */
596 int do_sigaction(int sig, const struct target_sigaction *act,
597                  struct target_sigaction *oact)
598 {
599     struct target_sigaction *k;
600     struct sigaction act1;
601     int host_sig;
602     int ret = 0;
603 
604     if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
605         return -EINVAL;
606     k = &sigact_table[sig - 1];
607 #if defined(DEBUG_SIGNAL)
608     fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
609             sig, act, oact);
610 #endif
611     if (oact) {
612         __put_user(k->_sa_handler, &oact->_sa_handler);
613         __put_user(k->sa_flags, &oact->sa_flags);
614 #if !defined(TARGET_MIPS)
615         __put_user(k->sa_restorer, &oact->sa_restorer);
616 #endif
617         /* Not swapped.  */
618         oact->sa_mask = k->sa_mask;
619     }
620     if (act) {
621         /* FIXME: This is not threadsafe.  */
622         __get_user(k->_sa_handler, &act->_sa_handler);
623         __get_user(k->sa_flags, &act->sa_flags);
624 #if !defined(TARGET_MIPS)
625         __get_user(k->sa_restorer, &act->sa_restorer);
626 #endif
627         /* To be swapped in target_to_host_sigset.  */
628         k->sa_mask = act->sa_mask;
629 
630         /* we update the host linux signal state */
631         host_sig = target_to_host_signal(sig);
632         if (host_sig != SIGSEGV && host_sig != SIGBUS) {
633             sigfillset(&act1.sa_mask);
634             act1.sa_flags = SA_SIGINFO;
635             if (k->sa_flags & TARGET_SA_RESTART)
636                 act1.sa_flags |= SA_RESTART;
637             /* NOTE: it is important to update the host kernel signal
638                ignore state to avoid getting unexpected interrupted
639                syscalls */
640             if (k->_sa_handler == TARGET_SIG_IGN) {
641                 act1.sa_sigaction = (void *)SIG_IGN;
642             } else if (k->_sa_handler == TARGET_SIG_DFL) {
643                 if (fatal_signal (sig))
644                     act1.sa_sigaction = host_signal_handler;
645                 else
646                     act1.sa_sigaction = (void *)SIG_DFL;
647             } else {
648                 act1.sa_sigaction = host_signal_handler;
649             }
650             ret = sigaction(host_sig, &act1, NULL);
651         }
652     }
653     return ret;
654 }
655 
656 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
657                                        const target_siginfo_t *info)
658 {
659     tswap_siginfo(tinfo, info);
660     return 0;
661 }
662 
663 static inline int current_exec_domain_sig(int sig)
664 {
665     return /* current->exec_domain && current->exec_domain->signal_invmap
666 	      && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
667 }
668 
669 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
670 
671 /* from the Linux kernel */
672 
673 struct target_fpreg {
674 	uint16_t significand[4];
675 	uint16_t exponent;
676 };
677 
678 struct target_fpxreg {
679 	uint16_t significand[4];
680 	uint16_t exponent;
681 	uint16_t padding[3];
682 };
683 
684 struct target_xmmreg {
685 	abi_ulong element[4];
686 };
687 
688 struct target_fpstate {
689 	/* Regular FPU environment */
690         abi_ulong       cw;
691         abi_ulong       sw;
692         abi_ulong       tag;
693         abi_ulong       ipoff;
694         abi_ulong       cssel;
695         abi_ulong       dataoff;
696         abi_ulong       datasel;
697 	struct target_fpreg	_st[8];
698 	uint16_t	status;
699 	uint16_t	magic;		/* 0xffff = regular FPU data only */
700 
701 	/* FXSR FPU environment */
702         abi_ulong       _fxsr_env[6];   /* FXSR FPU env is ignored */
703         abi_ulong       mxcsr;
704         abi_ulong       reserved;
705 	struct target_fpxreg	_fxsr_st[8];	/* FXSR FPU reg data is ignored */
706 	struct target_xmmreg	_xmm[8];
707         abi_ulong       padding[56];
708 };
709 
710 #define X86_FXSR_MAGIC		0x0000
711 
712 struct target_sigcontext {
713 	uint16_t gs, __gsh;
714 	uint16_t fs, __fsh;
715 	uint16_t es, __esh;
716 	uint16_t ds, __dsh;
717         abi_ulong edi;
718         abi_ulong esi;
719         abi_ulong ebp;
720         abi_ulong esp;
721         abi_ulong ebx;
722         abi_ulong edx;
723         abi_ulong ecx;
724         abi_ulong eax;
725         abi_ulong trapno;
726         abi_ulong err;
727         abi_ulong eip;
728 	uint16_t cs, __csh;
729         abi_ulong eflags;
730         abi_ulong esp_at_signal;
731 	uint16_t ss, __ssh;
732         abi_ulong fpstate; /* pointer */
733         abi_ulong oldmask;
734         abi_ulong cr2;
735 };
736 
737 struct target_ucontext {
738         abi_ulong         tuc_flags;
739         abi_ulong         tuc_link;
740 	target_stack_t	  tuc_stack;
741 	struct target_sigcontext tuc_mcontext;
742 	target_sigset_t	  tuc_sigmask;	/* mask last for extensibility */
743 };
744 
745 struct sigframe
746 {
747     abi_ulong pretcode;
748     int sig;
749     struct target_sigcontext sc;
750     struct target_fpstate fpstate;
751     abi_ulong extramask[TARGET_NSIG_WORDS-1];
752     char retcode[8];
753 };
754 
755 struct rt_sigframe
756 {
757     abi_ulong pretcode;
758     int sig;
759     abi_ulong pinfo;
760     abi_ulong puc;
761     struct target_siginfo info;
762     struct target_ucontext uc;
763     struct target_fpstate fpstate;
764     char retcode[8];
765 };
766 
767 /*
768  * Set up a signal frame.
769  */
770 
771 /* XXX: save x87 state */
772 static int
773 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
774 		 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
775 {
776 	int err = 0;
777         uint16_t magic;
778 
779 	/* already locked in setup_frame() */
780 	err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
781 	err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
782 	err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
783 	err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
784 	err |= __put_user(env->regs[R_EDI], &sc->edi);
785 	err |= __put_user(env->regs[R_ESI], &sc->esi);
786 	err |= __put_user(env->regs[R_EBP], &sc->ebp);
787 	err |= __put_user(env->regs[R_ESP], &sc->esp);
788 	err |= __put_user(env->regs[R_EBX], &sc->ebx);
789 	err |= __put_user(env->regs[R_EDX], &sc->edx);
790 	err |= __put_user(env->regs[R_ECX], &sc->ecx);
791 	err |= __put_user(env->regs[R_EAX], &sc->eax);
792 	err |= __put_user(env->exception_index, &sc->trapno);
793 	err |= __put_user(env->error_code, &sc->err);
794 	err |= __put_user(env->eip, &sc->eip);
795 	err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
796 	err |= __put_user(env->eflags, &sc->eflags);
797 	err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
798 	err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
799 
800         cpu_x86_fsave(env, fpstate_addr, 1);
801         fpstate->status = fpstate->sw;
802         magic = 0xffff;
803         err |= __put_user(magic, &fpstate->magic);
804         err |= __put_user(fpstate_addr, &sc->fpstate);
805 
806 	/* non-iBCS2 extensions.. */
807 	err |= __put_user(mask, &sc->oldmask);
808 	err |= __put_user(env->cr[2], &sc->cr2);
809 	return err;
810 }
811 
812 /*
813  * Determine which stack to use..
814  */
815 
816 static inline abi_ulong
817 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
818 {
819 	unsigned long esp;
820 
821 	/* Default to using normal stack */
822 	esp = env->regs[R_ESP];
823 	/* This is the X/Open sanctioned signal stack switching.  */
824         if (ka->sa_flags & TARGET_SA_ONSTACK) {
825             if (sas_ss_flags(esp) == 0)
826                 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
827         }
828 
829 	/* This is the legacy signal stack switching. */
830 	else
831         if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
832             !(ka->sa_flags & TARGET_SA_RESTORER) &&
833             ka->sa_restorer) {
834             esp = (unsigned long) ka->sa_restorer;
835 	}
836         return (esp - frame_size) & -8ul;
837 }
838 
839 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
840 static void setup_frame(int sig, struct target_sigaction *ka,
841 			target_sigset_t *set, CPUX86State *env)
842 {
843 	abi_ulong frame_addr;
844 	struct sigframe *frame;
845 	int i, err = 0;
846 
847 	frame_addr = get_sigframe(ka, env, sizeof(*frame));
848 
849 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
850 		goto give_sigsegv;
851 
852 	err |= __put_user(current_exec_domain_sig(sig),
853 		          &frame->sig);
854 	if (err)
855 		goto give_sigsegv;
856 
857 	setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
858                          frame_addr + offsetof(struct sigframe, fpstate));
859 	if (err)
860 		goto give_sigsegv;
861 
862         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
863             if (__put_user(set->sig[i], &frame->extramask[i - 1]))
864                 goto give_sigsegv;
865         }
866 
867 	/* Set up to return from userspace.  If provided, use a stub
868 	   already in userspace.  */
869 	if (ka->sa_flags & TARGET_SA_RESTORER) {
870 		err |= __put_user(ka->sa_restorer, &frame->pretcode);
871 	} else {
872                 uint16_t val16;
873                 abi_ulong retcode_addr;
874                 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
875 		err |= __put_user(retcode_addr, &frame->pretcode);
876 		/* This is popl %eax ; movl $,%eax ; int $0x80 */
877                 val16 = 0xb858;
878 		err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
879 		err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
880                 val16 = 0x80cd;
881 		err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
882 	}
883 
884 	if (err)
885 		goto give_sigsegv;
886 
887 	/* Set up registers for signal handler */
888 	env->regs[R_ESP] = frame_addr;
889 	env->eip = ka->_sa_handler;
890 
891         cpu_x86_load_seg(env, R_DS, __USER_DS);
892         cpu_x86_load_seg(env, R_ES, __USER_DS);
893         cpu_x86_load_seg(env, R_SS, __USER_DS);
894         cpu_x86_load_seg(env, R_CS, __USER_CS);
895 	env->eflags &= ~TF_MASK;
896 
897 	unlock_user_struct(frame, frame_addr, 1);
898 
899 	return;
900 
901 give_sigsegv:
902 	unlock_user_struct(frame, frame_addr, 1);
903 	if (sig == TARGET_SIGSEGV)
904 		ka->_sa_handler = TARGET_SIG_DFL;
905 	force_sig(TARGET_SIGSEGV /* , current */);
906 }
907 
908 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
909 static void setup_rt_frame(int sig, struct target_sigaction *ka,
910                            target_siginfo_t *info,
911 			   target_sigset_t *set, CPUX86State *env)
912 {
913         abi_ulong frame_addr, addr;
914 	struct rt_sigframe *frame;
915 	int i, err = 0;
916 
917 	frame_addr = get_sigframe(ka, env, sizeof(*frame));
918 
919 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
920 		goto give_sigsegv;
921 
922 	err |= __put_user(current_exec_domain_sig(sig),
923 			  &frame->sig);
924         addr = frame_addr + offsetof(struct rt_sigframe, info);
925 	err |= __put_user(addr, &frame->pinfo);
926         addr = frame_addr + offsetof(struct rt_sigframe, uc);
927 	err |= __put_user(addr, &frame->puc);
928 	err |= copy_siginfo_to_user(&frame->info, info);
929 	if (err)
930 		goto give_sigsegv;
931 
932 	/* Create the ucontext.  */
933 	err |= __put_user(0, &frame->uc.tuc_flags);
934 	err |= __put_user(0, &frame->uc.tuc_link);
935 	err |= __put_user(target_sigaltstack_used.ss_sp,
936 			  &frame->uc.tuc_stack.ss_sp);
937 	err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
938 			  &frame->uc.tuc_stack.ss_flags);
939 	err |= __put_user(target_sigaltstack_used.ss_size,
940 			  &frame->uc.tuc_stack.ss_size);
941 	err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
942 			        env, set->sig[0],
943                                 frame_addr + offsetof(struct rt_sigframe, fpstate));
944         for(i = 0; i < TARGET_NSIG_WORDS; i++) {
945             if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
946                 goto give_sigsegv;
947         }
948 
949 	/* Set up to return from userspace.  If provided, use a stub
950 	   already in userspace.  */
951 	if (ka->sa_flags & TARGET_SA_RESTORER) {
952 		err |= __put_user(ka->sa_restorer, &frame->pretcode);
953 	} else {
954                 uint16_t val16;
955                 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
956 		err |= __put_user(addr, &frame->pretcode);
957 		/* This is movl $,%eax ; int $0x80 */
958                 err |= __put_user(0xb8, (char *)(frame->retcode+0));
959 		err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
960                 val16 = 0x80cd;
961                 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
962 	}
963 
964 	if (err)
965 		goto give_sigsegv;
966 
967 	/* Set up registers for signal handler */
968 	env->regs[R_ESP] = frame_addr;
969 	env->eip = ka->_sa_handler;
970 
971         cpu_x86_load_seg(env, R_DS, __USER_DS);
972         cpu_x86_load_seg(env, R_ES, __USER_DS);
973         cpu_x86_load_seg(env, R_SS, __USER_DS);
974         cpu_x86_load_seg(env, R_CS, __USER_CS);
975 	env->eflags &= ~TF_MASK;
976 
977 	unlock_user_struct(frame, frame_addr, 1);
978 
979 	return;
980 
981 give_sigsegv:
982 	unlock_user_struct(frame, frame_addr, 1);
983 	if (sig == TARGET_SIGSEGV)
984 		ka->_sa_handler = TARGET_SIG_DFL;
985 	force_sig(TARGET_SIGSEGV /* , current */);
986 }
987 
988 static int
989 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
990 {
991 	unsigned int err = 0;
992         abi_ulong fpstate_addr;
993         unsigned int tmpflags;
994 
995         cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
996         cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
997         cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
998         cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
999 
1000         env->regs[R_EDI] = tswapl(sc->edi);
1001         env->regs[R_ESI] = tswapl(sc->esi);
1002         env->regs[R_EBP] = tswapl(sc->ebp);
1003         env->regs[R_ESP] = tswapl(sc->esp);
1004         env->regs[R_EBX] = tswapl(sc->ebx);
1005         env->regs[R_EDX] = tswapl(sc->edx);
1006         env->regs[R_ECX] = tswapl(sc->ecx);
1007         env->eip = tswapl(sc->eip);
1008 
1009         cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1010         cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1011 
1012         tmpflags = tswapl(sc->eflags);
1013         env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1014         //		regs->orig_eax = -1;		/* disable syscall checks */
1015 
1016         fpstate_addr = tswapl(sc->fpstate);
1017 	if (fpstate_addr != 0) {
1018                 if (!access_ok(VERIFY_READ, fpstate_addr,
1019                                sizeof(struct target_fpstate)))
1020                         goto badframe;
1021                 cpu_x86_frstor(env, fpstate_addr, 1);
1022 	}
1023 
1024         *peax = tswapl(sc->eax);
1025 	return err;
1026 badframe:
1027 	return 1;
1028 }
1029 
1030 long do_sigreturn(CPUX86State *env)
1031 {
1032     struct sigframe *frame;
1033     abi_ulong frame_addr = env->regs[R_ESP] - 8;
1034     target_sigset_t target_set;
1035     sigset_t set;
1036     int eax, i;
1037 
1038 #if defined(DEBUG_SIGNAL)
1039     fprintf(stderr, "do_sigreturn\n");
1040 #endif
1041     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1042         goto badframe;
1043     /* set blocked signals */
1044     if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1045         goto badframe;
1046     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1047         if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1048             goto badframe;
1049     }
1050 
1051     target_to_host_sigset_internal(&set, &target_set);
1052     sigprocmask(SIG_SETMASK, &set, NULL);
1053 
1054     /* restore registers */
1055     if (restore_sigcontext(env, &frame->sc, &eax))
1056         goto badframe;
1057     unlock_user_struct(frame, frame_addr, 0);
1058     return eax;
1059 
1060 badframe:
1061     unlock_user_struct(frame, frame_addr, 0);
1062     force_sig(TARGET_SIGSEGV);
1063     return 0;
1064 }
1065 
1066 long do_rt_sigreturn(CPUX86State *env)
1067 {
1068         abi_ulong frame_addr;
1069 	struct rt_sigframe *frame;
1070         sigset_t set;
1071 	int eax;
1072 
1073         frame_addr = env->regs[R_ESP] - 4;
1074         if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1075                 goto badframe;
1076         target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1077         sigprocmask(SIG_SETMASK, &set, NULL);
1078 
1079 	if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1080 		goto badframe;
1081 
1082 	if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1083                            get_sp_from_cpustate(env)) == -EFAULT)
1084 		goto badframe;
1085 
1086         unlock_user_struct(frame, frame_addr, 0);
1087 	return eax;
1088 
1089 badframe:
1090         unlock_user_struct(frame, frame_addr, 0);
1091         force_sig(TARGET_SIGSEGV);
1092 	return 0;
1093 }
1094 
1095 #elif defined(TARGET_AARCH64)
1096 
1097 struct target_sigcontext {
1098     uint64_t fault_address;
1099     /* AArch64 registers */
1100     uint64_t regs[31];
1101     uint64_t sp;
1102     uint64_t pc;
1103     uint64_t pstate;
1104     /* 4K reserved for FP/SIMD state and future expansion */
1105     char __reserved[4096] __attribute__((__aligned__(16)));
1106 };
1107 
1108 struct target_ucontext {
1109     abi_ulong tuc_flags;
1110     abi_ulong tuc_link;
1111     target_stack_t tuc_stack;
1112     target_sigset_t tuc_sigmask;
1113     /* glibc uses a 1024-bit sigset_t */
1114     char __unused[1024 / 8 - sizeof(target_sigset_t)];
1115     /* last for future expansion */
1116     struct target_sigcontext tuc_mcontext;
1117 };
1118 
1119 /*
1120  * Header to be used at the beginning of structures extending the user
1121  * context. Such structures must be placed after the rt_sigframe on the stack
1122  * and be 16-byte aligned. The last structure must be a dummy one with the
1123  * magic and size set to 0.
1124  */
1125 struct target_aarch64_ctx {
1126     uint32_t magic;
1127     uint32_t size;
1128 };
1129 
1130 #define TARGET_FPSIMD_MAGIC 0x46508001
1131 
1132 struct target_fpsimd_context {
1133     struct target_aarch64_ctx head;
1134     uint32_t fpsr;
1135     uint32_t fpcr;
1136     uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1137 };
1138 
1139 /*
1140  * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1141  * user space as it will change with the addition of new context. User space
1142  * should check the magic/size information.
1143  */
1144 struct target_aux_context {
1145     struct target_fpsimd_context fpsimd;
1146     /* additional context to be added before "end" */
1147     struct target_aarch64_ctx end;
1148 };
1149 
1150 struct target_rt_sigframe {
1151     struct target_siginfo info;
1152     struct target_ucontext uc;
1153     uint64_t fp;
1154     uint64_t lr;
1155     uint32_t tramp[2];
1156 };
1157 
1158 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1159                                  CPUARMState *env, target_sigset_t *set)
1160 {
1161     int i;
1162     struct target_aux_context *aux =
1163         (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1164 
1165     /* set up the stack frame for unwinding */
1166     __put_user(env->xregs[29], &sf->fp);
1167     __put_user(env->xregs[30], &sf->lr);
1168 
1169     for (i = 0; i < 31; i++) {
1170         __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1171     }
1172     __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1173     __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
1174     __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
1175 
1176     __put_user(/*current->thread.fault_address*/ 0,
1177             &sf->uc.tuc_mcontext.fault_address);
1178 
1179     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1180         __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1181     }
1182 
1183     for (i = 0; i < 32; i++) {
1184 #ifdef TARGET_WORDS_BIGENDIAN
1185         __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1186         __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1187 #else
1188         __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1189         __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1190 #endif
1191     }
1192     __put_user(/*env->fpsr*/0, &aux->fpsimd.fpsr);
1193     __put_user(/*env->fpcr*/0, &aux->fpsimd.fpcr);
1194     __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1195     __put_user(sizeof(struct target_fpsimd_context),
1196             &aux->fpsimd.head.size);
1197 
1198     /* set the "end" magic */
1199     __put_user(0, &aux->end.magic);
1200     __put_user(0, &aux->end.size);
1201 
1202     return 0;
1203 }
1204 
1205 static int target_restore_sigframe(CPUARMState *env,
1206                                    struct target_rt_sigframe *sf)
1207 {
1208     sigset_t set;
1209     int i;
1210     struct target_aux_context *aux =
1211         (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1212     uint32_t magic, size;
1213     uint64_t pstate;
1214 
1215     target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1216     sigprocmask(SIG_SETMASK, &set, NULL);
1217 
1218     for (i = 0; i < 31; i++) {
1219         __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1220     }
1221 
1222     __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1223     __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1224     __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1225     pstate_write(env, pstate);
1226 
1227     __get_user(magic, &aux->fpsimd.head.magic);
1228     __get_user(size, &aux->fpsimd.head.size);
1229 
1230     if (magic != TARGET_FPSIMD_MAGIC
1231         || size != sizeof(struct target_fpsimd_context)) {
1232         return 1;
1233     }
1234 
1235     for (i = 0; i < 32 * 2; i++) {
1236         __get_user(env->vfp.regs[i], &aux->fpsimd.vregs[i]);
1237     }
1238 
1239     return 0;
1240 }
1241 
1242 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1243 {
1244     abi_ulong sp;
1245 
1246     sp = env->xregs[31];
1247 
1248     /*
1249      * This is the X/Open sanctioned signal stack switching.
1250      */
1251     if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
1252         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1253     }
1254 
1255     sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1256 
1257     return sp;
1258 }
1259 
1260 static void target_setup_frame(int usig, struct target_sigaction *ka,
1261                                target_siginfo_t *info, target_sigset_t *set,
1262                                CPUARMState *env)
1263 {
1264     struct target_rt_sigframe *frame;
1265     abi_ulong frame_addr;
1266 
1267     frame_addr = get_sigframe(ka, env);
1268     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1269         goto give_sigsegv;
1270     }
1271 
1272     __put_user(0, &frame->uc.tuc_flags);
1273     __put_user(0, &frame->uc.tuc_link);
1274 
1275     __put_user(target_sigaltstack_used.ss_sp,
1276                       &frame->uc.tuc_stack.ss_sp);
1277     __put_user(sas_ss_flags(env->xregs[31]),
1278                       &frame->uc.tuc_stack.ss_flags);
1279     __put_user(target_sigaltstack_used.ss_size,
1280                       &frame->uc.tuc_stack.ss_size);
1281     target_setup_sigframe(frame, env, set);
1282     /* mov x8,#__NR_rt_sigreturn; svc #0 */
1283     __put_user(0xd2801168, &frame->tramp[0]);
1284     __put_user(0xd4000001, &frame->tramp[1]);
1285     env->xregs[0] = usig;
1286     env->xregs[31] = frame_addr;
1287     env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1288     env->pc = ka->_sa_handler;
1289     env->xregs[30] = env->xregs[31] +
1290         offsetof(struct target_rt_sigframe, tramp);
1291     if (info) {
1292         if (copy_siginfo_to_user(&frame->info, info)) {
1293             goto give_sigsegv;
1294         }
1295         env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1296         env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1297     }
1298 
1299     unlock_user_struct(frame, frame_addr, 1);
1300     return;
1301 
1302  give_sigsegv:
1303     unlock_user_struct(frame, frame_addr, 1);
1304     force_sig(TARGET_SIGSEGV);
1305 }
1306 
1307 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1308                            target_siginfo_t *info, target_sigset_t *set,
1309                            CPUARMState *env)
1310 {
1311     target_setup_frame(sig, ka, info, set, env);
1312 }
1313 
1314 static void setup_frame(int sig, struct target_sigaction *ka,
1315                         target_sigset_t *set, CPUARMState *env)
1316 {
1317     target_setup_frame(sig, ka, 0, set, env);
1318 }
1319 
1320 long do_rt_sigreturn(CPUARMState *env)
1321 {
1322     struct target_rt_sigframe *frame;
1323     abi_ulong frame_addr = env->xregs[31];
1324 
1325     if (frame_addr & 15) {
1326         goto badframe;
1327     }
1328 
1329     if  (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1330         goto badframe;
1331     }
1332 
1333     if (target_restore_sigframe(env, frame)) {
1334         goto badframe;
1335     }
1336 
1337     if (do_sigaltstack(frame_addr +
1338             offsetof(struct target_rt_sigframe, uc.tuc_stack),
1339             0, get_sp_from_cpustate(env)) == -EFAULT) {
1340         goto badframe;
1341     }
1342 
1343     unlock_user_struct(frame, frame_addr, 0);
1344     return env->xregs[0];
1345 
1346  badframe:
1347     unlock_user_struct(frame, frame_addr, 0);
1348     force_sig(TARGET_SIGSEGV);
1349     return 0;
1350 }
1351 
1352 long do_sigreturn(CPUARMState *env)
1353 {
1354     return do_rt_sigreturn(env);
1355 }
1356 
1357 #elif defined(TARGET_ARM)
1358 
1359 struct target_sigcontext {
1360 	abi_ulong trap_no;
1361 	abi_ulong error_code;
1362 	abi_ulong oldmask;
1363 	abi_ulong arm_r0;
1364 	abi_ulong arm_r1;
1365 	abi_ulong arm_r2;
1366 	abi_ulong arm_r3;
1367 	abi_ulong arm_r4;
1368 	abi_ulong arm_r5;
1369 	abi_ulong arm_r6;
1370 	abi_ulong arm_r7;
1371 	abi_ulong arm_r8;
1372 	abi_ulong arm_r9;
1373 	abi_ulong arm_r10;
1374 	abi_ulong arm_fp;
1375 	abi_ulong arm_ip;
1376 	abi_ulong arm_sp;
1377 	abi_ulong arm_lr;
1378 	abi_ulong arm_pc;
1379 	abi_ulong arm_cpsr;
1380 	abi_ulong fault_address;
1381 };
1382 
1383 struct target_ucontext_v1 {
1384     abi_ulong tuc_flags;
1385     abi_ulong tuc_link;
1386     target_stack_t tuc_stack;
1387     struct target_sigcontext tuc_mcontext;
1388     target_sigset_t  tuc_sigmask;	/* mask last for extensibility */
1389 };
1390 
1391 struct target_ucontext_v2 {
1392     abi_ulong tuc_flags;
1393     abi_ulong tuc_link;
1394     target_stack_t tuc_stack;
1395     struct target_sigcontext tuc_mcontext;
1396     target_sigset_t  tuc_sigmask;	/* mask last for extensibility */
1397     char __unused[128 - sizeof(target_sigset_t)];
1398     abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1399 };
1400 
1401 struct target_user_vfp {
1402     uint64_t fpregs[32];
1403     abi_ulong fpscr;
1404 };
1405 
1406 struct target_user_vfp_exc {
1407     abi_ulong fpexc;
1408     abi_ulong fpinst;
1409     abi_ulong fpinst2;
1410 };
1411 
1412 struct target_vfp_sigframe {
1413     abi_ulong magic;
1414     abi_ulong size;
1415     struct target_user_vfp ufp;
1416     struct target_user_vfp_exc ufp_exc;
1417 } __attribute__((__aligned__(8)));
1418 
1419 struct target_iwmmxt_sigframe {
1420     abi_ulong magic;
1421     abi_ulong size;
1422     uint64_t regs[16];
1423     /* Note that not all the coprocessor control registers are stored here */
1424     uint32_t wcssf;
1425     uint32_t wcasf;
1426     uint32_t wcgr0;
1427     uint32_t wcgr1;
1428     uint32_t wcgr2;
1429     uint32_t wcgr3;
1430 } __attribute__((__aligned__(8)));
1431 
1432 #define TARGET_VFP_MAGIC 0x56465001
1433 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1434 
1435 struct sigframe_v1
1436 {
1437     struct target_sigcontext sc;
1438     abi_ulong extramask[TARGET_NSIG_WORDS-1];
1439     abi_ulong retcode;
1440 };
1441 
1442 struct sigframe_v2
1443 {
1444     struct target_ucontext_v2 uc;
1445     abi_ulong retcode;
1446 };
1447 
1448 struct rt_sigframe_v1
1449 {
1450     abi_ulong pinfo;
1451     abi_ulong puc;
1452     struct target_siginfo info;
1453     struct target_ucontext_v1 uc;
1454     abi_ulong retcode;
1455 };
1456 
1457 struct rt_sigframe_v2
1458 {
1459     struct target_siginfo info;
1460     struct target_ucontext_v2 uc;
1461     abi_ulong retcode;
1462 };
1463 
1464 #define TARGET_CONFIG_CPU_32 1
1465 
1466 /*
1467  * For ARM syscalls, we encode the syscall number into the instruction.
1468  */
1469 #define SWI_SYS_SIGRETURN	(0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1470 #define SWI_SYS_RT_SIGRETURN	(0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1471 
1472 /*
1473  * For Thumb syscalls, we pass the syscall number via r7.  We therefore
1474  * need two 16-bit instructions.
1475  */
1476 #define SWI_THUMB_SIGRETURN	(0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1477 #define SWI_THUMB_RT_SIGRETURN	(0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1478 
1479 static const abi_ulong retcodes[4] = {
1480 	SWI_SYS_SIGRETURN,	SWI_THUMB_SIGRETURN,
1481 	SWI_SYS_RT_SIGRETURN,	SWI_THUMB_RT_SIGRETURN
1482 };
1483 
1484 
1485 #define __get_user_error(x,p,e) __get_user(x, p)
1486 
1487 static inline int valid_user_regs(CPUARMState *regs)
1488 {
1489     return 1;
1490 }
1491 
1492 static void
1493 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1494                  CPUARMState *env, abi_ulong mask)
1495 {
1496 	__put_user(env->regs[0], &sc->arm_r0);
1497 	__put_user(env->regs[1], &sc->arm_r1);
1498 	__put_user(env->regs[2], &sc->arm_r2);
1499 	__put_user(env->regs[3], &sc->arm_r3);
1500 	__put_user(env->regs[4], &sc->arm_r4);
1501 	__put_user(env->regs[5], &sc->arm_r5);
1502 	__put_user(env->regs[6], &sc->arm_r6);
1503 	__put_user(env->regs[7], &sc->arm_r7);
1504 	__put_user(env->regs[8], &sc->arm_r8);
1505 	__put_user(env->regs[9], &sc->arm_r9);
1506 	__put_user(env->regs[10], &sc->arm_r10);
1507 	__put_user(env->regs[11], &sc->arm_fp);
1508 	__put_user(env->regs[12], &sc->arm_ip);
1509 	__put_user(env->regs[13], &sc->arm_sp);
1510 	__put_user(env->regs[14], &sc->arm_lr);
1511 	__put_user(env->regs[15], &sc->arm_pc);
1512 #ifdef TARGET_CONFIG_CPU_32
1513 	__put_user(cpsr_read(env), &sc->arm_cpsr);
1514 #endif
1515 
1516 	__put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1517 	__put_user(/* current->thread.error_code */ 0, &sc->error_code);
1518 	__put_user(/* current->thread.address */ 0, &sc->fault_address);
1519 	__put_user(mask, &sc->oldmask);
1520 }
1521 
1522 static inline abi_ulong
1523 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1524 {
1525 	unsigned long sp = regs->regs[13];
1526 
1527 	/*
1528 	 * This is the X/Open sanctioned signal stack switching.
1529 	 */
1530 	if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1531             sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1532 	/*
1533 	 * ATPCS B01 mandates 8-byte alignment
1534 	 */
1535 	return (sp - framesize) & ~7;
1536 }
1537 
1538 static int
1539 setup_return(CPUARMState *env, struct target_sigaction *ka,
1540 	     abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1541 {
1542 	abi_ulong handler = ka->_sa_handler;
1543 	abi_ulong retcode;
1544 	int thumb = handler & 1;
1545 	uint32_t cpsr = cpsr_read(env);
1546 
1547 	cpsr &= ~CPSR_IT;
1548 	if (thumb) {
1549 		cpsr |= CPSR_T;
1550 	} else {
1551 		cpsr &= ~CPSR_T;
1552 	}
1553 
1554 	if (ka->sa_flags & TARGET_SA_RESTORER) {
1555 		retcode = ka->sa_restorer;
1556 	} else {
1557 		unsigned int idx = thumb;
1558 
1559 		if (ka->sa_flags & TARGET_SA_SIGINFO)
1560 			idx += 2;
1561 
1562 		if (__put_user(retcodes[idx], rc))
1563 			return 1;
1564 
1565 		retcode = rc_addr + thumb;
1566 	}
1567 
1568 	env->regs[0] = usig;
1569 	env->regs[13] = frame_addr;
1570 	env->regs[14] = retcode;
1571 	env->regs[15] = handler & (thumb ? ~1 : ~3);
1572 	cpsr_write(env, cpsr, 0xffffffff);
1573 
1574 	return 0;
1575 }
1576 
1577 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1578 {
1579     int i;
1580     struct target_vfp_sigframe *vfpframe;
1581     vfpframe = (struct target_vfp_sigframe *)regspace;
1582     __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1583     __put_user(sizeof(*vfpframe), &vfpframe->size);
1584     for (i = 0; i < 32; i++) {
1585         __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1586     }
1587     __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1588     __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1589     __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1590     __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1591     return (abi_ulong*)(vfpframe+1);
1592 }
1593 
1594 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1595                                            CPUARMState *env)
1596 {
1597     int i;
1598     struct target_iwmmxt_sigframe *iwmmxtframe;
1599     iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1600     __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1601     __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1602     for (i = 0; i < 16; i++) {
1603         __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1604     }
1605     __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1606     __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1607     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1608     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1609     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1610     __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1611     return (abi_ulong*)(iwmmxtframe+1);
1612 }
1613 
1614 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1615                               target_sigset_t *set, CPUARMState *env)
1616 {
1617     struct target_sigaltstack stack;
1618     int i;
1619     abi_ulong *regspace;
1620 
1621     /* Clear all the bits of the ucontext we don't use.  */
1622     memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1623 
1624     memset(&stack, 0, sizeof(stack));
1625     __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1626     __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1627     __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1628     memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1629 
1630     setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1631     /* Save coprocessor signal frame.  */
1632     regspace = uc->tuc_regspace;
1633     if (arm_feature(env, ARM_FEATURE_VFP)) {
1634         regspace = setup_sigframe_v2_vfp(regspace, env);
1635     }
1636     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1637         regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1638     }
1639 
1640     /* Write terminating magic word */
1641     __put_user(0, regspace);
1642 
1643     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1644         __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1645     }
1646 }
1647 
1648 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1649 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1650                            target_sigset_t *set, CPUARMState *regs)
1651 {
1652 	struct sigframe_v1 *frame;
1653 	abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1654 	int i;
1655 
1656 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1657 		return;
1658 
1659 	setup_sigcontext(&frame->sc, regs, set->sig[0]);
1660 
1661         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1662             if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1663                 goto end;
1664 	}
1665 
1666         setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1667                      frame_addr + offsetof(struct sigframe_v1, retcode));
1668 
1669 end:
1670 	unlock_user_struct(frame, frame_addr, 1);
1671 }
1672 
1673 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1674                            target_sigset_t *set, CPUARMState *regs)
1675 {
1676 	struct sigframe_v2 *frame;
1677 	abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1678 
1679 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1680 		return;
1681 
1682         setup_sigframe_v2(&frame->uc, set, regs);
1683 
1684         setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1685                      frame_addr + offsetof(struct sigframe_v2, retcode));
1686 
1687 	unlock_user_struct(frame, frame_addr, 1);
1688 }
1689 
1690 static void setup_frame(int usig, struct target_sigaction *ka,
1691                         target_sigset_t *set, CPUARMState *regs)
1692 {
1693     if (get_osversion() >= 0x020612) {
1694         setup_frame_v2(usig, ka, set, regs);
1695     } else {
1696         setup_frame_v1(usig, ka, set, regs);
1697     }
1698 }
1699 
1700 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1701 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1702                               target_siginfo_t *info,
1703                               target_sigset_t *set, CPUARMState *env)
1704 {
1705 	struct rt_sigframe_v1 *frame;
1706 	abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1707 	struct target_sigaltstack stack;
1708 	int i;
1709         abi_ulong info_addr, uc_addr;
1710 
1711 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1712             return /* 1 */;
1713 
1714         info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1715 	__put_user(info_addr, &frame->pinfo);
1716         uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1717 	__put_user(uc_addr, &frame->puc);
1718 	copy_siginfo_to_user(&frame->info, info);
1719 
1720 	/* Clear all the bits of the ucontext we don't use.  */
1721 	memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1722 
1723         memset(&stack, 0, sizeof(stack));
1724         __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1725         __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1726         __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1727         memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1728 
1729 	setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1730         for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1731             if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1732                 goto end;
1733         }
1734 
1735         setup_return(env, ka, &frame->retcode, frame_addr, usig,
1736                      frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1737 
1738         env->regs[1] = info_addr;
1739         env->regs[2] = uc_addr;
1740 
1741 end:
1742 	unlock_user_struct(frame, frame_addr, 1);
1743 }
1744 
1745 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1746                               target_siginfo_t *info,
1747                               target_sigset_t *set, CPUARMState *env)
1748 {
1749 	struct rt_sigframe_v2 *frame;
1750 	abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1751         abi_ulong info_addr, uc_addr;
1752 
1753 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1754             return /* 1 */;
1755 
1756         info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1757         uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1758 	copy_siginfo_to_user(&frame->info, info);
1759 
1760         setup_sigframe_v2(&frame->uc, set, env);
1761 
1762         setup_return(env, ka, &frame->retcode, frame_addr, usig,
1763                      frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1764 
1765         env->regs[1] = info_addr;
1766         env->regs[2] = uc_addr;
1767 
1768 	unlock_user_struct(frame, frame_addr, 1);
1769 }
1770 
1771 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1772                            target_siginfo_t *info,
1773                            target_sigset_t *set, CPUARMState *env)
1774 {
1775     if (get_osversion() >= 0x020612) {
1776         setup_rt_frame_v2(usig, ka, info, set, env);
1777     } else {
1778         setup_rt_frame_v1(usig, ka, info, set, env);
1779     }
1780 }
1781 
1782 static int
1783 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1784 {
1785 	int err = 0;
1786         uint32_t cpsr;
1787 
1788 	__get_user_error(env->regs[0], &sc->arm_r0, err);
1789 	__get_user_error(env->regs[1], &sc->arm_r1, err);
1790 	__get_user_error(env->regs[2], &sc->arm_r2, err);
1791 	__get_user_error(env->regs[3], &sc->arm_r3, err);
1792 	__get_user_error(env->regs[4], &sc->arm_r4, err);
1793 	__get_user_error(env->regs[5], &sc->arm_r5, err);
1794 	__get_user_error(env->regs[6], &sc->arm_r6, err);
1795 	__get_user_error(env->regs[7], &sc->arm_r7, err);
1796 	__get_user_error(env->regs[8], &sc->arm_r8, err);
1797 	__get_user_error(env->regs[9], &sc->arm_r9, err);
1798 	__get_user_error(env->regs[10], &sc->arm_r10, err);
1799 	__get_user_error(env->regs[11], &sc->arm_fp, err);
1800 	__get_user_error(env->regs[12], &sc->arm_ip, err);
1801 	__get_user_error(env->regs[13], &sc->arm_sp, err);
1802 	__get_user_error(env->regs[14], &sc->arm_lr, err);
1803 	__get_user_error(env->regs[15], &sc->arm_pc, err);
1804 #ifdef TARGET_CONFIG_CPU_32
1805 	__get_user_error(cpsr, &sc->arm_cpsr, err);
1806         cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1807 #endif
1808 
1809 	err |= !valid_user_regs(env);
1810 
1811 	return err;
1812 }
1813 
1814 static long do_sigreturn_v1(CPUARMState *env)
1815 {
1816         abi_ulong frame_addr;
1817         struct sigframe_v1 *frame = NULL;
1818 	target_sigset_t set;
1819         sigset_t host_set;
1820         int i;
1821 
1822 	/*
1823 	 * Since we stacked the signal on a 64-bit boundary,
1824 	 * then 'sp' should be word aligned here.  If it's
1825 	 * not, then the user is trying to mess with us.
1826 	 */
1827         frame_addr = env->regs[13];
1828         if (frame_addr & 7) {
1829             goto badframe;
1830         }
1831 
1832 	if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1833                 goto badframe;
1834 
1835 	if (__get_user(set.sig[0], &frame->sc.oldmask))
1836             goto badframe;
1837         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1838             if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1839                 goto badframe;
1840         }
1841 
1842         target_to_host_sigset_internal(&host_set, &set);
1843         sigprocmask(SIG_SETMASK, &host_set, NULL);
1844 
1845 	if (restore_sigcontext(env, &frame->sc))
1846 		goto badframe;
1847 
1848 #if 0
1849 	/* Send SIGTRAP if we're single-stepping */
1850 	if (ptrace_cancel_bpt(current))
1851 		send_sig(SIGTRAP, current, 1);
1852 #endif
1853 	unlock_user_struct(frame, frame_addr, 0);
1854         return env->regs[0];
1855 
1856 badframe:
1857 	unlock_user_struct(frame, frame_addr, 0);
1858         force_sig(TARGET_SIGSEGV /* , current */);
1859 	return 0;
1860 }
1861 
1862 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1863 {
1864     int i;
1865     abi_ulong magic, sz;
1866     uint32_t fpscr, fpexc;
1867     struct target_vfp_sigframe *vfpframe;
1868     vfpframe = (struct target_vfp_sigframe *)regspace;
1869 
1870     __get_user(magic, &vfpframe->magic);
1871     __get_user(sz, &vfpframe->size);
1872     if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1873         return 0;
1874     }
1875     for (i = 0; i < 32; i++) {
1876         __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1877     }
1878     __get_user(fpscr, &vfpframe->ufp.fpscr);
1879     vfp_set_fpscr(env, fpscr);
1880     __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1881     /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1882      * and the exception flag is cleared
1883      */
1884     fpexc |= (1 << 30);
1885     fpexc &= ~((1 << 31) | (1 << 28));
1886     env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1887     __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1888     __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1889     return (abi_ulong*)(vfpframe + 1);
1890 }
1891 
1892 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1893                                              abi_ulong *regspace)
1894 {
1895     int i;
1896     abi_ulong magic, sz;
1897     struct target_iwmmxt_sigframe *iwmmxtframe;
1898     iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1899 
1900     __get_user(magic, &iwmmxtframe->magic);
1901     __get_user(sz, &iwmmxtframe->size);
1902     if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1903         return 0;
1904     }
1905     for (i = 0; i < 16; i++) {
1906         __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1907     }
1908     __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1909     __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1910     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1911     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1912     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1913     __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1914     return (abi_ulong*)(iwmmxtframe + 1);
1915 }
1916 
1917 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1918                                  struct target_ucontext_v2 *uc)
1919 {
1920     sigset_t host_set;
1921     abi_ulong *regspace;
1922 
1923     target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1924     sigprocmask(SIG_SETMASK, &host_set, NULL);
1925 
1926     if (restore_sigcontext(env, &uc->tuc_mcontext))
1927         return 1;
1928 
1929     /* Restore coprocessor signal frame */
1930     regspace = uc->tuc_regspace;
1931     if (arm_feature(env, ARM_FEATURE_VFP)) {
1932         regspace = restore_sigframe_v2_vfp(env, regspace);
1933         if (!regspace) {
1934             return 1;
1935         }
1936     }
1937     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1938         regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1939         if (!regspace) {
1940             return 1;
1941         }
1942     }
1943 
1944     if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1945         return 1;
1946 
1947 #if 0
1948     /* Send SIGTRAP if we're single-stepping */
1949     if (ptrace_cancel_bpt(current))
1950             send_sig(SIGTRAP, current, 1);
1951 #endif
1952 
1953     return 0;
1954 }
1955 
1956 static long do_sigreturn_v2(CPUARMState *env)
1957 {
1958         abi_ulong frame_addr;
1959         struct sigframe_v2 *frame = NULL;
1960 
1961 	/*
1962 	 * Since we stacked the signal on a 64-bit boundary,
1963 	 * then 'sp' should be word aligned here.  If it's
1964 	 * not, then the user is trying to mess with us.
1965 	 */
1966         frame_addr = env->regs[13];
1967         if (frame_addr & 7) {
1968             goto badframe;
1969         }
1970 
1971 	if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1972                 goto badframe;
1973 
1974         if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1975                 goto badframe;
1976 
1977 	unlock_user_struct(frame, frame_addr, 0);
1978 	return env->regs[0];
1979 
1980 badframe:
1981 	unlock_user_struct(frame, frame_addr, 0);
1982         force_sig(TARGET_SIGSEGV /* , current */);
1983 	return 0;
1984 }
1985 
1986 long do_sigreturn(CPUARMState *env)
1987 {
1988     if (get_osversion() >= 0x020612) {
1989         return do_sigreturn_v2(env);
1990     } else {
1991         return do_sigreturn_v1(env);
1992     }
1993 }
1994 
1995 static long do_rt_sigreturn_v1(CPUARMState *env)
1996 {
1997         abi_ulong frame_addr;
1998         struct rt_sigframe_v1 *frame = NULL;
1999         sigset_t host_set;
2000 
2001 	/*
2002 	 * Since we stacked the signal on a 64-bit boundary,
2003 	 * then 'sp' should be word aligned here.  If it's
2004 	 * not, then the user is trying to mess with us.
2005 	 */
2006         frame_addr = env->regs[13];
2007         if (frame_addr & 7) {
2008             goto badframe;
2009         }
2010 
2011 	if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2012                 goto badframe;
2013 
2014         target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2015         sigprocmask(SIG_SETMASK, &host_set, NULL);
2016 
2017 	if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
2018 		goto badframe;
2019 
2020 	if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2021 		goto badframe;
2022 
2023 #if 0
2024 	/* Send SIGTRAP if we're single-stepping */
2025 	if (ptrace_cancel_bpt(current))
2026 		send_sig(SIGTRAP, current, 1);
2027 #endif
2028 	unlock_user_struct(frame, frame_addr, 0);
2029 	return env->regs[0];
2030 
2031 badframe:
2032 	unlock_user_struct(frame, frame_addr, 0);
2033         force_sig(TARGET_SIGSEGV /* , current */);
2034 	return 0;
2035 }
2036 
2037 static long do_rt_sigreturn_v2(CPUARMState *env)
2038 {
2039         abi_ulong frame_addr;
2040         struct rt_sigframe_v2 *frame = NULL;
2041 
2042 	/*
2043 	 * Since we stacked the signal on a 64-bit boundary,
2044 	 * then 'sp' should be word aligned here.  If it's
2045 	 * not, then the user is trying to mess with us.
2046 	 */
2047         frame_addr = env->regs[13];
2048         if (frame_addr & 7) {
2049             goto badframe;
2050         }
2051 
2052 	if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2053                 goto badframe;
2054 
2055         if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2056                 goto badframe;
2057 
2058 	unlock_user_struct(frame, frame_addr, 0);
2059 	return env->regs[0];
2060 
2061 badframe:
2062 	unlock_user_struct(frame, frame_addr, 0);
2063         force_sig(TARGET_SIGSEGV /* , current */);
2064 	return 0;
2065 }
2066 
2067 long do_rt_sigreturn(CPUARMState *env)
2068 {
2069     if (get_osversion() >= 0x020612) {
2070         return do_rt_sigreturn_v2(env);
2071     } else {
2072         return do_rt_sigreturn_v1(env);
2073     }
2074 }
2075 
2076 #elif defined(TARGET_SPARC)
2077 
2078 #define __SUNOS_MAXWIN   31
2079 
2080 /* This is what SunOS does, so shall I. */
2081 struct target_sigcontext {
2082         abi_ulong sigc_onstack;      /* state to restore */
2083 
2084         abi_ulong sigc_mask;         /* sigmask to restore */
2085         abi_ulong sigc_sp;           /* stack pointer */
2086         abi_ulong sigc_pc;           /* program counter */
2087         abi_ulong sigc_npc;          /* next program counter */
2088         abi_ulong sigc_psr;          /* for condition codes etc */
2089         abi_ulong sigc_g1;           /* User uses these two registers */
2090         abi_ulong sigc_o0;           /* within the trampoline code. */
2091 
2092         /* Now comes information regarding the users window set
2093          * at the time of the signal.
2094          */
2095         abi_ulong sigc_oswins;       /* outstanding windows */
2096 
2097         /* stack ptrs for each regwin buf */
2098         char *sigc_spbuf[__SUNOS_MAXWIN];
2099 
2100         /* Windows to restore after signal */
2101         struct {
2102                 abi_ulong locals[8];
2103                 abi_ulong ins[8];
2104         } sigc_wbuf[__SUNOS_MAXWIN];
2105 };
2106 /* A Sparc stack frame */
2107 struct sparc_stackf {
2108         abi_ulong locals[8];
2109         abi_ulong ins[8];
2110         /* It's simpler to treat fp and callers_pc as elements of ins[]
2111          * since we never need to access them ourselves.
2112          */
2113         char *structptr;
2114         abi_ulong xargs[6];
2115         abi_ulong xxargs[1];
2116 };
2117 
2118 typedef struct {
2119         struct {
2120                 abi_ulong psr;
2121                 abi_ulong pc;
2122                 abi_ulong npc;
2123                 abi_ulong y;
2124                 abi_ulong u_regs[16]; /* globals and ins */
2125         }               si_regs;
2126         int             si_mask;
2127 } __siginfo_t;
2128 
2129 typedef struct {
2130         abi_ulong       si_float_regs[32];
2131         unsigned   long si_fsr;
2132         unsigned   long si_fpqdepth;
2133         struct {
2134                 unsigned long *insn_addr;
2135                 unsigned long insn;
2136         } si_fpqueue [16];
2137 } qemu_siginfo_fpu_t;
2138 
2139 
2140 struct target_signal_frame {
2141 	struct sparc_stackf	ss;
2142 	__siginfo_t		info;
2143 	abi_ulong               fpu_save;
2144 	abi_ulong		insns[2] __attribute__ ((aligned (8)));
2145 	abi_ulong		extramask[TARGET_NSIG_WORDS - 1];
2146 	abi_ulong		extra_size; /* Should be 0 */
2147 	qemu_siginfo_fpu_t	fpu_state;
2148 };
2149 struct target_rt_signal_frame {
2150 	struct sparc_stackf	ss;
2151 	siginfo_t		info;
2152 	abi_ulong		regs[20];
2153 	sigset_t		mask;
2154 	abi_ulong               fpu_save;
2155 	unsigned int		insns[2];
2156 	stack_t			stack;
2157 	unsigned int		extra_size; /* Should be 0 */
2158 	qemu_siginfo_fpu_t	fpu_state;
2159 };
2160 
2161 #define UREG_O0        16
2162 #define UREG_O6        22
2163 #define UREG_I0        0
2164 #define UREG_I1        1
2165 #define UREG_I2        2
2166 #define UREG_I3        3
2167 #define UREG_I4        4
2168 #define UREG_I5        5
2169 #define UREG_I6        6
2170 #define UREG_I7        7
2171 #define UREG_L0	       8
2172 #define UREG_FP        UREG_I6
2173 #define UREG_SP        UREG_O6
2174 
2175 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2176                                      CPUSPARCState *env,
2177                                      unsigned long framesize)
2178 {
2179 	abi_ulong sp;
2180 
2181 	sp = env->regwptr[UREG_FP];
2182 
2183 	/* This is the X/Open sanctioned signal stack switching.  */
2184 	if (sa->sa_flags & TARGET_SA_ONSTACK) {
2185             if (!on_sig_stack(sp)
2186                 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2187                 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2188 	}
2189 	return sp - framesize;
2190 }
2191 
2192 static int
2193 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2194 {
2195 	int err = 0, i;
2196 
2197 	err |= __put_user(env->psr, &si->si_regs.psr);
2198 	err |= __put_user(env->pc, &si->si_regs.pc);
2199 	err |= __put_user(env->npc, &si->si_regs.npc);
2200 	err |= __put_user(env->y, &si->si_regs.y);
2201 	for (i=0; i < 8; i++) {
2202 		err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2203 	}
2204 	for (i=0; i < 8; i++) {
2205 		err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2206 	}
2207 	err |= __put_user(mask, &si->si_mask);
2208 	return err;
2209 }
2210 
2211 #if 0
2212 static int
2213 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2214                  CPUSPARCState *env, unsigned long mask)
2215 {
2216 	int err = 0;
2217 
2218 	err |= __put_user(mask, &sc->sigc_mask);
2219 	err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2220 	err |= __put_user(env->pc, &sc->sigc_pc);
2221 	err |= __put_user(env->npc, &sc->sigc_npc);
2222 	err |= __put_user(env->psr, &sc->sigc_psr);
2223 	err |= __put_user(env->gregs[1], &sc->sigc_g1);
2224 	err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2225 
2226 	return err;
2227 }
2228 #endif
2229 #define NF_ALIGNEDSZ  (((sizeof(struct target_signal_frame) + 7) & (~7)))
2230 
2231 static void setup_frame(int sig, struct target_sigaction *ka,
2232                         target_sigset_t *set, CPUSPARCState *env)
2233 {
2234         abi_ulong sf_addr;
2235 	struct target_signal_frame *sf;
2236 	int sigframe_size, err, i;
2237 
2238 	/* 1. Make sure everything is clean */
2239 	//synchronize_user_stack();
2240 
2241         sigframe_size = NF_ALIGNEDSZ;
2242 	sf_addr = get_sigframe(ka, env, sigframe_size);
2243 
2244         sf = lock_user(VERIFY_WRITE, sf_addr,
2245                        sizeof(struct target_signal_frame), 0);
2246         if (!sf)
2247 		goto sigsegv;
2248 
2249 	//fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2250 #if 0
2251 	if (invalid_frame_pointer(sf, sigframe_size))
2252 		goto sigill_and_return;
2253 #endif
2254 	/* 2. Save the current process state */
2255 	err = setup___siginfo(&sf->info, env, set->sig[0]);
2256 	err |= __put_user(0, &sf->extra_size);
2257 
2258 	//err |= save_fpu_state(regs, &sf->fpu_state);
2259 	//err |= __put_user(&sf->fpu_state, &sf->fpu_save);
2260 
2261 	err |= __put_user(set->sig[0], &sf->info.si_mask);
2262 	for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2263 		err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
2264 	}
2265 
2266 	for (i = 0; i < 8; i++) {
2267 	  	err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2268 	}
2269 	for (i = 0; i < 8; i++) {
2270 	  	err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2271 	}
2272 	if (err)
2273 		goto sigsegv;
2274 
2275 	/* 3. signal handler back-trampoline and parameters */
2276 	env->regwptr[UREG_FP] = sf_addr;
2277 	env->regwptr[UREG_I0] = sig;
2278 	env->regwptr[UREG_I1] = sf_addr +
2279                 offsetof(struct target_signal_frame, info);
2280 	env->regwptr[UREG_I2] = sf_addr +
2281                 offsetof(struct target_signal_frame, info);
2282 
2283 	/* 4. signal handler */
2284 	env->pc = ka->_sa_handler;
2285 	env->npc = (env->pc + 4);
2286 	/* 5. return to kernel instructions */
2287 	if (ka->sa_restorer)
2288 		env->regwptr[UREG_I7] = ka->sa_restorer;
2289 	else {
2290                 uint32_t val32;
2291 
2292 		env->regwptr[UREG_I7] = sf_addr +
2293                         offsetof(struct target_signal_frame, insns) - 2 * 4;
2294 
2295 		/* mov __NR_sigreturn, %g1 */
2296                 val32 = 0x821020d8;
2297 		err |= __put_user(val32, &sf->insns[0]);
2298 
2299 		/* t 0x10 */
2300                 val32 = 0x91d02010;
2301 		err |= __put_user(val32, &sf->insns[1]);
2302 		if (err)
2303 			goto sigsegv;
2304 
2305 		/* Flush instruction space. */
2306 		//flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2307                 //		tb_flush(env);
2308 	}
2309         unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2310 	return;
2311 #if 0
2312 sigill_and_return:
2313 	force_sig(TARGET_SIGILL);
2314 #endif
2315 sigsegv:
2316 	//fprintf(stderr, "force_sig\n");
2317         unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2318 	force_sig(TARGET_SIGSEGV);
2319 }
2320 static inline int
2321 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
2322 {
2323         int err;
2324 #if 0
2325 #ifdef CONFIG_SMP
2326         if (current->flags & PF_USEDFPU)
2327                 regs->psr &= ~PSR_EF;
2328 #else
2329         if (current == last_task_used_math) {
2330                 last_task_used_math = 0;
2331                 regs->psr &= ~PSR_EF;
2332         }
2333 #endif
2334         current->used_math = 1;
2335         current->flags &= ~PF_USEDFPU;
2336 #endif
2337 #if 0
2338         if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2339                 return -EFAULT;
2340 #endif
2341 
2342         /* XXX: incorrect */
2343         err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2344                              (sizeof(abi_ulong) * 32));
2345         err |= __get_user(env->fsr, &fpu->si_fsr);
2346 #if 0
2347         err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2348         if (current->thread.fpqdepth != 0)
2349                 err |= __copy_from_user(&current->thread.fpqueue[0],
2350                                         &fpu->si_fpqueue[0],
2351                                         ((sizeof(unsigned long) +
2352                                         (sizeof(unsigned long *)))*16));
2353 #endif
2354         return err;
2355 }
2356 
2357 
2358 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2359                            target_siginfo_t *info,
2360                            target_sigset_t *set, CPUSPARCState *env)
2361 {
2362     fprintf(stderr, "setup_rt_frame: not implemented\n");
2363 }
2364 
2365 long do_sigreturn(CPUSPARCState *env)
2366 {
2367         abi_ulong sf_addr;
2368         struct target_signal_frame *sf;
2369         uint32_t up_psr, pc, npc;
2370         target_sigset_t set;
2371         sigset_t host_set;
2372         int err, i;
2373 
2374         sf_addr = env->regwptr[UREG_FP];
2375         if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2376                 goto segv_and_exit;
2377 #if 0
2378 	fprintf(stderr, "sigreturn\n");
2379 	fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2380 #endif
2381 	//cpu_dump_state(env, stderr, fprintf, 0);
2382 
2383         /* 1. Make sure we are not getting garbage from the user */
2384 
2385         if (sf_addr & 3)
2386                 goto segv_and_exit;
2387 
2388         err = __get_user(pc,  &sf->info.si_regs.pc);
2389         err |= __get_user(npc, &sf->info.si_regs.npc);
2390 
2391         if ((pc | npc) & 3)
2392                 goto segv_and_exit;
2393 
2394         /* 2. Restore the state */
2395         err |= __get_user(up_psr, &sf->info.si_regs.psr);
2396 
2397         /* User can only change condition codes and FPU enabling in %psr. */
2398         env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2399                   | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2400 
2401 	env->pc = pc;
2402 	env->npc = npc;
2403         err |= __get_user(env->y, &sf->info.si_regs.y);
2404 	for (i=0; i < 8; i++) {
2405 		err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2406 	}
2407 	for (i=0; i < 8; i++) {
2408 		err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2409 	}
2410 
2411         /* FIXME: implement FPU save/restore:
2412          * __get_user(fpu_save, &sf->fpu_save);
2413          * if (fpu_save)
2414          *        err |= restore_fpu_state(env, fpu_save);
2415          */
2416 
2417         /* This is pretty much atomic, no amount locking would prevent
2418          * the races which exist anyways.
2419          */
2420         err |= __get_user(set.sig[0], &sf->info.si_mask);
2421         for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2422             err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2423         }
2424 
2425         target_to_host_sigset_internal(&host_set, &set);
2426         sigprocmask(SIG_SETMASK, &host_set, NULL);
2427 
2428         if (err)
2429                 goto segv_and_exit;
2430         unlock_user_struct(sf, sf_addr, 0);
2431         return env->regwptr[0];
2432 
2433 segv_and_exit:
2434         unlock_user_struct(sf, sf_addr, 0);
2435 	force_sig(TARGET_SIGSEGV);
2436 }
2437 
2438 long do_rt_sigreturn(CPUSPARCState *env)
2439 {
2440     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2441     return -TARGET_ENOSYS;
2442 }
2443 
2444 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2445 #define MC_TSTATE 0
2446 #define MC_PC 1
2447 #define MC_NPC 2
2448 #define MC_Y 3
2449 #define MC_G1 4
2450 #define MC_G2 5
2451 #define MC_G3 6
2452 #define MC_G4 7
2453 #define MC_G5 8
2454 #define MC_G6 9
2455 #define MC_G7 10
2456 #define MC_O0 11
2457 #define MC_O1 12
2458 #define MC_O2 13
2459 #define MC_O3 14
2460 #define MC_O4 15
2461 #define MC_O5 16
2462 #define MC_O6 17
2463 #define MC_O7 18
2464 #define MC_NGREG 19
2465 
2466 typedef abi_ulong target_mc_greg_t;
2467 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2468 
2469 struct target_mc_fq {
2470     abi_ulong *mcfq_addr;
2471     uint32_t mcfq_insn;
2472 };
2473 
2474 struct target_mc_fpu {
2475     union {
2476         uint32_t sregs[32];
2477         uint64_t dregs[32];
2478         //uint128_t qregs[16];
2479     } mcfpu_fregs;
2480     abi_ulong mcfpu_fsr;
2481     abi_ulong mcfpu_fprs;
2482     abi_ulong mcfpu_gsr;
2483     struct target_mc_fq *mcfpu_fq;
2484     unsigned char mcfpu_qcnt;
2485     unsigned char mcfpu_qentsz;
2486     unsigned char mcfpu_enab;
2487 };
2488 typedef struct target_mc_fpu target_mc_fpu_t;
2489 
2490 typedef struct {
2491     target_mc_gregset_t mc_gregs;
2492     target_mc_greg_t mc_fp;
2493     target_mc_greg_t mc_i7;
2494     target_mc_fpu_t mc_fpregs;
2495 } target_mcontext_t;
2496 
2497 struct target_ucontext {
2498     struct target_ucontext *tuc_link;
2499     abi_ulong tuc_flags;
2500     target_sigset_t tuc_sigmask;
2501     target_mcontext_t tuc_mcontext;
2502 };
2503 
2504 /* A V9 register window */
2505 struct target_reg_window {
2506     abi_ulong locals[8];
2507     abi_ulong ins[8];
2508 };
2509 
2510 #define TARGET_STACK_BIAS 2047
2511 
2512 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2513 void sparc64_set_context(CPUSPARCState *env)
2514 {
2515     abi_ulong ucp_addr;
2516     struct target_ucontext *ucp;
2517     target_mc_gregset_t *grp;
2518     abi_ulong pc, npc, tstate;
2519     abi_ulong fp, i7, w_addr;
2520     int err;
2521     unsigned int i;
2522 
2523     ucp_addr = env->regwptr[UREG_I0];
2524     if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2525         goto do_sigsegv;
2526     grp  = &ucp->tuc_mcontext.mc_gregs;
2527     err  = __get_user(pc, &((*grp)[MC_PC]));
2528     err |= __get_user(npc, &((*grp)[MC_NPC]));
2529     if (err || ((pc | npc) & 3))
2530         goto do_sigsegv;
2531     if (env->regwptr[UREG_I1]) {
2532         target_sigset_t target_set;
2533         sigset_t set;
2534 
2535         if (TARGET_NSIG_WORDS == 1) {
2536             if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2537                 goto do_sigsegv;
2538         } else {
2539             abi_ulong *src, *dst;
2540             src = ucp->tuc_sigmask.sig;
2541             dst = target_set.sig;
2542             for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2543                  i++, dst++, src++)
2544                 err |= __get_user(*dst, src);
2545             if (err)
2546                 goto do_sigsegv;
2547         }
2548         target_to_host_sigset_internal(&set, &target_set);
2549         sigprocmask(SIG_SETMASK, &set, NULL);
2550     }
2551     env->pc = pc;
2552     env->npc = npc;
2553     err |= __get_user(env->y, &((*grp)[MC_Y]));
2554     err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2555     env->asi = (tstate >> 24) & 0xff;
2556     cpu_put_ccr(env, tstate >> 32);
2557     cpu_put_cwp64(env, tstate & 0x1f);
2558     err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2559     err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2560     err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2561     err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2562     err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2563     err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2564     err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2565     err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2566     err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2567     err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2568     err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2569     err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2570     err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2571     err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2572     err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2573 
2574     err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2575     err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2576 
2577     w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2578     if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2579                  abi_ulong) != 0)
2580         goto do_sigsegv;
2581     if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2582                  abi_ulong) != 0)
2583         goto do_sigsegv;
2584     /* FIXME this does not match how the kernel handles the FPU in
2585      * its sparc64_set_context implementation. In particular the FPU
2586      * is only restored if fenab is non-zero in:
2587      *   __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2588      */
2589     err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2590     {
2591         uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2592         for (i = 0; i < 64; i++, src++) {
2593             if (i & 1) {
2594                 err |= __get_user(env->fpr[i/2].l.lower, src);
2595             } else {
2596                 err |= __get_user(env->fpr[i/2].l.upper, src);
2597             }
2598         }
2599     }
2600     err |= __get_user(env->fsr,
2601                       &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2602     err |= __get_user(env->gsr,
2603                       &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2604     if (err)
2605         goto do_sigsegv;
2606     unlock_user_struct(ucp, ucp_addr, 0);
2607     return;
2608  do_sigsegv:
2609     unlock_user_struct(ucp, ucp_addr, 0);
2610     force_sig(TARGET_SIGSEGV);
2611 }
2612 
2613 void sparc64_get_context(CPUSPARCState *env)
2614 {
2615     abi_ulong ucp_addr;
2616     struct target_ucontext *ucp;
2617     target_mc_gregset_t *grp;
2618     target_mcontext_t *mcp;
2619     abi_ulong fp, i7, w_addr;
2620     int err;
2621     unsigned int i;
2622     target_sigset_t target_set;
2623     sigset_t set;
2624 
2625     ucp_addr = env->regwptr[UREG_I0];
2626     if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2627         goto do_sigsegv;
2628 
2629     mcp = &ucp->tuc_mcontext;
2630     grp = &mcp->mc_gregs;
2631 
2632     /* Skip over the trap instruction, first. */
2633     env->pc = env->npc;
2634     env->npc += 4;
2635 
2636     err = 0;
2637 
2638     sigprocmask(0, NULL, &set);
2639     host_to_target_sigset_internal(&target_set, &set);
2640     if (TARGET_NSIG_WORDS == 1) {
2641         err |= __put_user(target_set.sig[0],
2642                           (abi_ulong *)&ucp->tuc_sigmask);
2643     } else {
2644         abi_ulong *src, *dst;
2645         src = target_set.sig;
2646         dst = ucp->tuc_sigmask.sig;
2647         for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2648              i++, dst++, src++)
2649             err |= __put_user(*src, dst);
2650         if (err)
2651             goto do_sigsegv;
2652     }
2653 
2654     /* XXX: tstate must be saved properly */
2655     //    err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2656     err |= __put_user(env->pc, &((*grp)[MC_PC]));
2657     err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2658     err |= __put_user(env->y, &((*grp)[MC_Y]));
2659     err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2660     err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2661     err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2662     err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2663     err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2664     err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2665     err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2666     err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2667     err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2668     err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2669     err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2670     err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2671     err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2672     err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2673     err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2674 
2675     w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2676     fp = i7 = 0;
2677     if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2678                  abi_ulong) != 0)
2679         goto do_sigsegv;
2680     if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2681                  abi_ulong) != 0)
2682         goto do_sigsegv;
2683     err |= __put_user(fp, &(mcp->mc_fp));
2684     err |= __put_user(i7, &(mcp->mc_i7));
2685 
2686     {
2687         uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2688         for (i = 0; i < 64; i++, dst++) {
2689             if (i & 1) {
2690                 err |= __put_user(env->fpr[i/2].l.lower, dst);
2691             } else {
2692                 err |= __put_user(env->fpr[i/2].l.upper, dst);
2693             }
2694         }
2695     }
2696     err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2697     err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2698     err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2699 
2700     if (err)
2701         goto do_sigsegv;
2702     unlock_user_struct(ucp, ucp_addr, 1);
2703     return;
2704  do_sigsegv:
2705     unlock_user_struct(ucp, ucp_addr, 1);
2706     force_sig(TARGET_SIGSEGV);
2707 }
2708 #endif
2709 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2710 
2711 # if defined(TARGET_ABI_MIPSO32)
2712 struct target_sigcontext {
2713     uint32_t   sc_regmask;     /* Unused */
2714     uint32_t   sc_status;
2715     uint64_t   sc_pc;
2716     uint64_t   sc_regs[32];
2717     uint64_t   sc_fpregs[32];
2718     uint32_t   sc_ownedfp;     /* Unused */
2719     uint32_t   sc_fpc_csr;
2720     uint32_t   sc_fpc_eir;     /* Unused */
2721     uint32_t   sc_used_math;
2722     uint32_t   sc_dsp;         /* dsp status, was sc_ssflags */
2723     uint32_t   pad0;
2724     uint64_t   sc_mdhi;
2725     uint64_t   sc_mdlo;
2726     target_ulong   sc_hi1;         /* Was sc_cause */
2727     target_ulong   sc_lo1;         /* Was sc_badvaddr */
2728     target_ulong   sc_hi2;         /* Was sc_sigset[4] */
2729     target_ulong   sc_lo2;
2730     target_ulong   sc_hi3;
2731     target_ulong   sc_lo3;
2732 };
2733 # else /* N32 || N64 */
2734 struct target_sigcontext {
2735     uint64_t sc_regs[32];
2736     uint64_t sc_fpregs[32];
2737     uint64_t sc_mdhi;
2738     uint64_t sc_hi1;
2739     uint64_t sc_hi2;
2740     uint64_t sc_hi3;
2741     uint64_t sc_mdlo;
2742     uint64_t sc_lo1;
2743     uint64_t sc_lo2;
2744     uint64_t sc_lo3;
2745     uint64_t sc_pc;
2746     uint32_t sc_fpc_csr;
2747     uint32_t sc_used_math;
2748     uint32_t sc_dsp;
2749     uint32_t sc_reserved;
2750 };
2751 # endif /* O32 */
2752 
2753 struct sigframe {
2754     uint32_t sf_ass[4];			/* argument save space for o32 */
2755     uint32_t sf_code[2];			/* signal trampoline */
2756     struct target_sigcontext sf_sc;
2757     target_sigset_t sf_mask;
2758 };
2759 
2760 struct target_ucontext {
2761     target_ulong tuc_flags;
2762     target_ulong tuc_link;
2763     target_stack_t tuc_stack;
2764     target_ulong pad0;
2765     struct target_sigcontext tuc_mcontext;
2766     target_sigset_t tuc_sigmask;
2767 };
2768 
2769 struct target_rt_sigframe {
2770     uint32_t rs_ass[4];               /* argument save space for o32 */
2771     uint32_t rs_code[2];              /* signal trampoline */
2772     struct target_siginfo rs_info;
2773     struct target_ucontext rs_uc;
2774 };
2775 
2776 /* Install trampoline to jump back from signal handler */
2777 static inline int install_sigtramp(unsigned int *tramp,   unsigned int syscall)
2778 {
2779     int err = 0;
2780 
2781     /*
2782      * Set up the return code ...
2783      *
2784      *         li      v0, __NR__foo_sigreturn
2785      *         syscall
2786      */
2787 
2788     err |= __put_user(0x24020000 + syscall, tramp + 0);
2789     err |= __put_user(0x0000000c          , tramp + 1);
2790     return err;
2791 }
2792 
2793 static inline int
2794 setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2795 {
2796     int err = 0;
2797     int i;
2798 
2799     err |= __put_user(exception_resume_pc(regs), &sc->sc_pc);
2800     regs->hflags &= ~MIPS_HFLAG_BMASK;
2801 
2802     __put_user(0, &sc->sc_regs[0]);
2803     for (i = 1; i < 32; ++i) {
2804         err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2805     }
2806 
2807     err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2808     err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2809 
2810     /* Rather than checking for dsp existence, always copy.  The storage
2811        would just be garbage otherwise.  */
2812     err |= __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2813     err |= __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2814     err |= __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2815     err |= __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2816     err |= __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2817     err |= __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2818     {
2819         uint32_t dsp = cpu_rddsp(0x3ff, regs);
2820         err |= __put_user(dsp, &sc->sc_dsp);
2821     }
2822 
2823     err |= __put_user(1, &sc->sc_used_math);
2824 
2825     for (i = 0; i < 32; ++i) {
2826         err |= __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2827     }
2828 
2829     return err;
2830 }
2831 
2832 static inline int
2833 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2834 {
2835     int err = 0;
2836     int i;
2837 
2838     err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2839 
2840     err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2841     err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2842 
2843     for (i = 1; i < 32; ++i) {
2844         err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2845     }
2846 
2847     err |= __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2848     err |= __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2849     err |= __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2850     err |= __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2851     err |= __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2852     err |= __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2853     {
2854         uint32_t dsp;
2855         err |= __get_user(dsp, &sc->sc_dsp);
2856         cpu_wrdsp(dsp, 0x3ff, regs);
2857     }
2858 
2859     for (i = 0; i < 32; ++i) {
2860         err |= __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2861     }
2862 
2863     return err;
2864 }
2865 
2866 /*
2867  * Determine which stack to use..
2868  */
2869 static inline abi_ulong
2870 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2871 {
2872     unsigned long sp;
2873 
2874     /* Default to using normal stack */
2875     sp = regs->active_tc.gpr[29];
2876 
2877     /*
2878      * FPU emulator may have its own trampoline active just
2879      * above the user stack, 16-bytes before the next lowest
2880      * 16 byte boundary.  Try to avoid trashing it.
2881      */
2882     sp -= 32;
2883 
2884     /* This is the X/Open sanctioned signal stack switching.  */
2885     if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2886         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2887     }
2888 
2889     return (sp - frame_size) & ~7;
2890 }
2891 
2892 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2893 {
2894     if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2895         env->hflags &= ~MIPS_HFLAG_M16;
2896         env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2897         env->active_tc.PC &= ~(target_ulong) 1;
2898     }
2899 }
2900 
2901 # if defined(TARGET_ABI_MIPSO32)
2902 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2903 static void setup_frame(int sig, struct target_sigaction * ka,
2904                         target_sigset_t *set, CPUMIPSState *regs)
2905 {
2906     struct sigframe *frame;
2907     abi_ulong frame_addr;
2908     int i;
2909 
2910     frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2911     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2912 	goto give_sigsegv;
2913 
2914     install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2915 
2916     if(setup_sigcontext(regs, &frame->sf_sc))
2917 	goto give_sigsegv;
2918 
2919     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2920 	if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2921 	    goto give_sigsegv;
2922     }
2923 
2924     /*
2925     * Arguments to signal handler:
2926     *
2927     *   a0 = signal number
2928     *   a1 = 0 (should be cause)
2929     *   a2 = pointer to struct sigcontext
2930     *
2931     * $25 and PC point to the signal handler, $29 points to the
2932     * struct sigframe.
2933     */
2934     regs->active_tc.gpr[ 4] = sig;
2935     regs->active_tc.gpr[ 5] = 0;
2936     regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2937     regs->active_tc.gpr[29] = frame_addr;
2938     regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2939     /* The original kernel code sets CP0_EPC to the handler
2940     * since it returns to userland using eret
2941     * we cannot do this here, and we must set PC directly */
2942     regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2943     mips_set_hflags_isa_mode_from_pc(regs);
2944     unlock_user_struct(frame, frame_addr, 1);
2945     return;
2946 
2947 give_sigsegv:
2948     unlock_user_struct(frame, frame_addr, 1);
2949     force_sig(TARGET_SIGSEGV/*, current*/);
2950 }
2951 
2952 long do_sigreturn(CPUMIPSState *regs)
2953 {
2954     struct sigframe *frame;
2955     abi_ulong frame_addr;
2956     sigset_t blocked;
2957     target_sigset_t target_set;
2958     int i;
2959 
2960 #if defined(DEBUG_SIGNAL)
2961     fprintf(stderr, "do_sigreturn\n");
2962 #endif
2963     frame_addr = regs->active_tc.gpr[29];
2964     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2965    	goto badframe;
2966 
2967     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2968    	if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2969 	    goto badframe;
2970     }
2971 
2972     target_to_host_sigset_internal(&blocked, &target_set);
2973     sigprocmask(SIG_SETMASK, &blocked, NULL);
2974 
2975     if (restore_sigcontext(regs, &frame->sf_sc))
2976    	goto badframe;
2977 
2978 #if 0
2979     /*
2980      * Don't let your children do this ...
2981      */
2982     __asm__ __volatile__(
2983    	"move\t$29, %0\n\t"
2984    	"j\tsyscall_exit"
2985    	:/* no outputs */
2986    	:"r" (&regs));
2987     /* Unreached */
2988 #endif
2989 
2990     regs->active_tc.PC = regs->CP0_EPC;
2991     mips_set_hflags_isa_mode_from_pc(regs);
2992     /* I am not sure this is right, but it seems to work
2993     * maybe a problem with nested signals ? */
2994     regs->CP0_EPC = 0;
2995     return -TARGET_QEMU_ESIGRETURN;
2996 
2997 badframe:
2998     force_sig(TARGET_SIGSEGV/*, current*/);
2999     return 0;
3000 }
3001 # endif /* O32 */
3002 
3003 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3004                            target_siginfo_t *info,
3005                            target_sigset_t *set, CPUMIPSState *env)
3006 {
3007     struct target_rt_sigframe *frame;
3008     abi_ulong frame_addr;
3009     int i;
3010 
3011     frame_addr = get_sigframe(ka, env, sizeof(*frame));
3012     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3013 	goto give_sigsegv;
3014 
3015     install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3016 
3017     copy_siginfo_to_user(&frame->rs_info, info);
3018 
3019     __put_user(0, &frame->rs_uc.tuc_flags);
3020     __put_user(0, &frame->rs_uc.tuc_link);
3021     __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3022     __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3023     __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3024                &frame->rs_uc.tuc_stack.ss_flags);
3025 
3026     setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3027 
3028     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3029         __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3030     }
3031 
3032     /*
3033     * Arguments to signal handler:
3034     *
3035     *   a0 = signal number
3036     *   a1 = pointer to siginfo_t
3037     *   a2 = pointer to struct ucontext
3038     *
3039     * $25 and PC point to the signal handler, $29 points to the
3040     * struct sigframe.
3041     */
3042     env->active_tc.gpr[ 4] = sig;
3043     env->active_tc.gpr[ 5] = frame_addr
3044                              + offsetof(struct target_rt_sigframe, rs_info);
3045     env->active_tc.gpr[ 6] = frame_addr
3046                              + offsetof(struct target_rt_sigframe, rs_uc);
3047     env->active_tc.gpr[29] = frame_addr;
3048     env->active_tc.gpr[31] = frame_addr
3049                              + offsetof(struct target_rt_sigframe, rs_code);
3050     /* The original kernel code sets CP0_EPC to the handler
3051     * since it returns to userland using eret
3052     * we cannot do this here, and we must set PC directly */
3053     env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3054     mips_set_hflags_isa_mode_from_pc(env);
3055     unlock_user_struct(frame, frame_addr, 1);
3056     return;
3057 
3058 give_sigsegv:
3059     unlock_user_struct(frame, frame_addr, 1);
3060     force_sig(TARGET_SIGSEGV/*, current*/);
3061 }
3062 
3063 long do_rt_sigreturn(CPUMIPSState *env)
3064 {
3065     struct target_rt_sigframe *frame;
3066     abi_ulong frame_addr;
3067     sigset_t blocked;
3068 
3069 #if defined(DEBUG_SIGNAL)
3070     fprintf(stderr, "do_rt_sigreturn\n");
3071 #endif
3072     frame_addr = env->active_tc.gpr[29];
3073     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3074    	goto badframe;
3075 
3076     target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3077     sigprocmask(SIG_SETMASK, &blocked, NULL);
3078 
3079     if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
3080         goto badframe;
3081 
3082     if (do_sigaltstack(frame_addr +
3083 		       offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3084 		       0, get_sp_from_cpustate(env)) == -EFAULT)
3085         goto badframe;
3086 
3087     env->active_tc.PC = env->CP0_EPC;
3088     mips_set_hflags_isa_mode_from_pc(env);
3089     /* I am not sure this is right, but it seems to work
3090     * maybe a problem with nested signals ? */
3091     env->CP0_EPC = 0;
3092     return -TARGET_QEMU_ESIGRETURN;
3093 
3094 badframe:
3095     force_sig(TARGET_SIGSEGV/*, current*/);
3096     return 0;
3097 }
3098 
3099 #elif defined(TARGET_SH4)
3100 
3101 /*
3102  * code and data structures from linux kernel:
3103  * include/asm-sh/sigcontext.h
3104  * arch/sh/kernel/signal.c
3105  */
3106 
3107 struct target_sigcontext {
3108     target_ulong  oldmask;
3109 
3110     /* CPU registers */
3111     target_ulong  sc_gregs[16];
3112     target_ulong  sc_pc;
3113     target_ulong  sc_pr;
3114     target_ulong  sc_sr;
3115     target_ulong  sc_gbr;
3116     target_ulong  sc_mach;
3117     target_ulong  sc_macl;
3118 
3119     /* FPU registers */
3120     target_ulong  sc_fpregs[16];
3121     target_ulong  sc_xfpregs[16];
3122     unsigned int sc_fpscr;
3123     unsigned int sc_fpul;
3124     unsigned int sc_ownedfp;
3125 };
3126 
3127 struct target_sigframe
3128 {
3129     struct target_sigcontext sc;
3130     target_ulong extramask[TARGET_NSIG_WORDS-1];
3131     uint16_t retcode[3];
3132 };
3133 
3134 
3135 struct target_ucontext {
3136     target_ulong tuc_flags;
3137     struct target_ucontext *tuc_link;
3138     target_stack_t tuc_stack;
3139     struct target_sigcontext tuc_mcontext;
3140     target_sigset_t tuc_sigmask;	/* mask last for extensibility */
3141 };
3142 
3143 struct target_rt_sigframe
3144 {
3145     struct target_siginfo info;
3146     struct target_ucontext uc;
3147     uint16_t retcode[3];
3148 };
3149 
3150 
3151 #define MOVW(n)  (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3152 #define TRAP_NOARG 0xc310         /* Syscall w/no args (NR in R3) SH3/4 */
3153 
3154 static abi_ulong get_sigframe(struct target_sigaction *ka,
3155                          unsigned long sp, size_t frame_size)
3156 {
3157     if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3158         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3159     }
3160 
3161     return (sp - frame_size) & -8ul;
3162 }
3163 
3164 static int setup_sigcontext(struct target_sigcontext *sc,
3165                             CPUSH4State *regs, unsigned long mask)
3166 {
3167     int err = 0;
3168     int i;
3169 
3170 #define COPY(x)         err |= __put_user(regs->x, &sc->sc_##x)
3171     COPY(gregs[0]); COPY(gregs[1]);
3172     COPY(gregs[2]); COPY(gregs[3]);
3173     COPY(gregs[4]); COPY(gregs[5]);
3174     COPY(gregs[6]); COPY(gregs[7]);
3175     COPY(gregs[8]); COPY(gregs[9]);
3176     COPY(gregs[10]); COPY(gregs[11]);
3177     COPY(gregs[12]); COPY(gregs[13]);
3178     COPY(gregs[14]); COPY(gregs[15]);
3179     COPY(gbr); COPY(mach);
3180     COPY(macl); COPY(pr);
3181     COPY(sr); COPY(pc);
3182 #undef COPY
3183 
3184     for (i=0; i<16; i++) {
3185         err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3186     }
3187     err |= __put_user(regs->fpscr, &sc->sc_fpscr);
3188     err |= __put_user(regs->fpul, &sc->sc_fpul);
3189 
3190     /* non-iBCS2 extensions.. */
3191     err |= __put_user(mask, &sc->oldmask);
3192 
3193     return err;
3194 }
3195 
3196 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3197                               target_ulong *r0_p)
3198 {
3199     unsigned int err = 0;
3200     int i;
3201 
3202 #define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
3203     COPY(gregs[1]);
3204     COPY(gregs[2]); COPY(gregs[3]);
3205     COPY(gregs[4]); COPY(gregs[5]);
3206     COPY(gregs[6]); COPY(gregs[7]);
3207     COPY(gregs[8]); COPY(gregs[9]);
3208     COPY(gregs[10]); COPY(gregs[11]);
3209     COPY(gregs[12]); COPY(gregs[13]);
3210     COPY(gregs[14]); COPY(gregs[15]);
3211     COPY(gbr); COPY(mach);
3212     COPY(macl); COPY(pr);
3213     COPY(sr); COPY(pc);
3214 #undef COPY
3215 
3216     for (i=0; i<16; i++) {
3217         err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3218     }
3219     err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3220     err |= __get_user(regs->fpul, &sc->sc_fpul);
3221 
3222     regs->tra = -1;         /* disable syscall checks */
3223     err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3224     return err;
3225 }
3226 
3227 static void setup_frame(int sig, struct target_sigaction *ka,
3228                         target_sigset_t *set, CPUSH4State *regs)
3229 {
3230     struct target_sigframe *frame;
3231     abi_ulong frame_addr;
3232     int i;
3233     int err = 0;
3234     int signal;
3235 
3236     frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3237     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3238 	goto give_sigsegv;
3239 
3240     signal = current_exec_domain_sig(sig);
3241 
3242     err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3243 
3244     for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3245         err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3246     }
3247 
3248     /* Set up to return from userspace.  If provided, use a stub
3249        already in userspace.  */
3250     if (ka->sa_flags & TARGET_SA_RESTORER) {
3251         regs->pr = (unsigned long) ka->sa_restorer;
3252     } else {
3253         /* Generate return code (system call to sigreturn) */
3254         err |= __put_user(MOVW(2), &frame->retcode[0]);
3255         err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3256         err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3257         regs->pr = (unsigned long) frame->retcode;
3258     }
3259 
3260     if (err)
3261         goto give_sigsegv;
3262 
3263     /* Set up registers for signal handler */
3264     regs->gregs[15] = frame_addr;
3265     regs->gregs[4] = signal; /* Arg for signal handler */
3266     regs->gregs[5] = 0;
3267     regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3268     regs->pc = (unsigned long) ka->_sa_handler;
3269 
3270     unlock_user_struct(frame, frame_addr, 1);
3271     return;
3272 
3273 give_sigsegv:
3274     unlock_user_struct(frame, frame_addr, 1);
3275     force_sig(TARGET_SIGSEGV);
3276 }
3277 
3278 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3279                            target_siginfo_t *info,
3280                            target_sigset_t *set, CPUSH4State *regs)
3281 {
3282     struct target_rt_sigframe *frame;
3283     abi_ulong frame_addr;
3284     int i;
3285     int err = 0;
3286     int signal;
3287 
3288     frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3289     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3290 	goto give_sigsegv;
3291 
3292     signal = current_exec_domain_sig(sig);
3293 
3294     err |= copy_siginfo_to_user(&frame->info, info);
3295 
3296     /* Create the ucontext.  */
3297     err |= __put_user(0, &frame->uc.tuc_flags);
3298     err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3299     err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3300 		      &frame->uc.tuc_stack.ss_sp);
3301     err |= __put_user(sas_ss_flags(regs->gregs[15]),
3302 		      &frame->uc.tuc_stack.ss_flags);
3303     err |= __put_user(target_sigaltstack_used.ss_size,
3304 		      &frame->uc.tuc_stack.ss_size);
3305     err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3306 			    regs, set->sig[0]);
3307     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3308         err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3309     }
3310 
3311     /* Set up to return from userspace.  If provided, use a stub
3312        already in userspace.  */
3313     if (ka->sa_flags & TARGET_SA_RESTORER) {
3314         regs->pr = (unsigned long) ka->sa_restorer;
3315     } else {
3316         /* Generate return code (system call to sigreturn) */
3317         err |= __put_user(MOVW(2), &frame->retcode[0]);
3318         err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3319         err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3320         regs->pr = (unsigned long) frame->retcode;
3321     }
3322 
3323     if (err)
3324         goto give_sigsegv;
3325 
3326     /* Set up registers for signal handler */
3327     regs->gregs[15] = frame_addr;
3328     regs->gregs[4] = signal; /* Arg for signal handler */
3329     regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3330     regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3331     regs->pc = (unsigned long) ka->_sa_handler;
3332 
3333     unlock_user_struct(frame, frame_addr, 1);
3334     return;
3335 
3336 give_sigsegv:
3337     unlock_user_struct(frame, frame_addr, 1);
3338     force_sig(TARGET_SIGSEGV);
3339 }
3340 
3341 long do_sigreturn(CPUSH4State *regs)
3342 {
3343     struct target_sigframe *frame;
3344     abi_ulong frame_addr;
3345     sigset_t blocked;
3346     target_sigset_t target_set;
3347     target_ulong r0;
3348     int i;
3349     int err = 0;
3350 
3351 #if defined(DEBUG_SIGNAL)
3352     fprintf(stderr, "do_sigreturn\n");
3353 #endif
3354     frame_addr = regs->gregs[15];
3355     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3356    	goto badframe;
3357 
3358     err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3359     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3360         err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3361     }
3362 
3363     if (err)
3364         goto badframe;
3365 
3366     target_to_host_sigset_internal(&blocked, &target_set);
3367     sigprocmask(SIG_SETMASK, &blocked, NULL);
3368 
3369     if (restore_sigcontext(regs, &frame->sc, &r0))
3370         goto badframe;
3371 
3372     unlock_user_struct(frame, frame_addr, 0);
3373     return r0;
3374 
3375 badframe:
3376     unlock_user_struct(frame, frame_addr, 0);
3377     force_sig(TARGET_SIGSEGV);
3378     return 0;
3379 }
3380 
3381 long do_rt_sigreturn(CPUSH4State *regs)
3382 {
3383     struct target_rt_sigframe *frame;
3384     abi_ulong frame_addr;
3385     sigset_t blocked;
3386     target_ulong r0;
3387 
3388 #if defined(DEBUG_SIGNAL)
3389     fprintf(stderr, "do_rt_sigreturn\n");
3390 #endif
3391     frame_addr = regs->gregs[15];
3392     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3393    	goto badframe;
3394 
3395     target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3396     sigprocmask(SIG_SETMASK, &blocked, NULL);
3397 
3398     if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3399         goto badframe;
3400 
3401     if (do_sigaltstack(frame_addr +
3402 		       offsetof(struct target_rt_sigframe, uc.tuc_stack),
3403 		       0, get_sp_from_cpustate(regs)) == -EFAULT)
3404         goto badframe;
3405 
3406     unlock_user_struct(frame, frame_addr, 0);
3407     return r0;
3408 
3409 badframe:
3410     unlock_user_struct(frame, frame_addr, 0);
3411     force_sig(TARGET_SIGSEGV);
3412     return 0;
3413 }
3414 #elif defined(TARGET_MICROBLAZE)
3415 
3416 struct target_sigcontext {
3417     struct target_pt_regs regs;  /* needs to be first */
3418     uint32_t oldmask;
3419 };
3420 
3421 struct target_stack_t {
3422     abi_ulong ss_sp;
3423     int ss_flags;
3424     unsigned int ss_size;
3425 };
3426 
3427 struct target_ucontext {
3428     abi_ulong tuc_flags;
3429     abi_ulong tuc_link;
3430     struct target_stack_t tuc_stack;
3431     struct target_sigcontext tuc_mcontext;
3432     uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3433 };
3434 
3435 /* Signal frames. */
3436 struct target_signal_frame {
3437     struct target_ucontext uc;
3438     uint32_t extramask[TARGET_NSIG_WORDS - 1];
3439     uint32_t tramp[2];
3440 };
3441 
3442 struct rt_signal_frame {
3443     siginfo_t info;
3444     struct ucontext uc;
3445     uint32_t tramp[2];
3446 };
3447 
3448 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3449 {
3450     __put_user(env->regs[0], &sc->regs.r0);
3451     __put_user(env->regs[1], &sc->regs.r1);
3452     __put_user(env->regs[2], &sc->regs.r2);
3453     __put_user(env->regs[3], &sc->regs.r3);
3454     __put_user(env->regs[4], &sc->regs.r4);
3455     __put_user(env->regs[5], &sc->regs.r5);
3456     __put_user(env->regs[6], &sc->regs.r6);
3457     __put_user(env->regs[7], &sc->regs.r7);
3458     __put_user(env->regs[8], &sc->regs.r8);
3459     __put_user(env->regs[9], &sc->regs.r9);
3460     __put_user(env->regs[10], &sc->regs.r10);
3461     __put_user(env->regs[11], &sc->regs.r11);
3462     __put_user(env->regs[12], &sc->regs.r12);
3463     __put_user(env->regs[13], &sc->regs.r13);
3464     __put_user(env->regs[14], &sc->regs.r14);
3465     __put_user(env->regs[15], &sc->regs.r15);
3466     __put_user(env->regs[16], &sc->regs.r16);
3467     __put_user(env->regs[17], &sc->regs.r17);
3468     __put_user(env->regs[18], &sc->regs.r18);
3469     __put_user(env->regs[19], &sc->regs.r19);
3470     __put_user(env->regs[20], &sc->regs.r20);
3471     __put_user(env->regs[21], &sc->regs.r21);
3472     __put_user(env->regs[22], &sc->regs.r22);
3473     __put_user(env->regs[23], &sc->regs.r23);
3474     __put_user(env->regs[24], &sc->regs.r24);
3475     __put_user(env->regs[25], &sc->regs.r25);
3476     __put_user(env->regs[26], &sc->regs.r26);
3477     __put_user(env->regs[27], &sc->regs.r27);
3478     __put_user(env->regs[28], &sc->regs.r28);
3479     __put_user(env->regs[29], &sc->regs.r29);
3480     __put_user(env->regs[30], &sc->regs.r30);
3481     __put_user(env->regs[31], &sc->regs.r31);
3482     __put_user(env->sregs[SR_PC], &sc->regs.pc);
3483 }
3484 
3485 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3486 {
3487     __get_user(env->regs[0], &sc->regs.r0);
3488     __get_user(env->regs[1], &sc->regs.r1);
3489     __get_user(env->regs[2], &sc->regs.r2);
3490     __get_user(env->regs[3], &sc->regs.r3);
3491     __get_user(env->regs[4], &sc->regs.r4);
3492     __get_user(env->regs[5], &sc->regs.r5);
3493     __get_user(env->regs[6], &sc->regs.r6);
3494     __get_user(env->regs[7], &sc->regs.r7);
3495     __get_user(env->regs[8], &sc->regs.r8);
3496     __get_user(env->regs[9], &sc->regs.r9);
3497     __get_user(env->regs[10], &sc->regs.r10);
3498     __get_user(env->regs[11], &sc->regs.r11);
3499     __get_user(env->regs[12], &sc->regs.r12);
3500     __get_user(env->regs[13], &sc->regs.r13);
3501     __get_user(env->regs[14], &sc->regs.r14);
3502     __get_user(env->regs[15], &sc->regs.r15);
3503     __get_user(env->regs[16], &sc->regs.r16);
3504     __get_user(env->regs[17], &sc->regs.r17);
3505     __get_user(env->regs[18], &sc->regs.r18);
3506     __get_user(env->regs[19], &sc->regs.r19);
3507     __get_user(env->regs[20], &sc->regs.r20);
3508     __get_user(env->regs[21], &sc->regs.r21);
3509     __get_user(env->regs[22], &sc->regs.r22);
3510     __get_user(env->regs[23], &sc->regs.r23);
3511     __get_user(env->regs[24], &sc->regs.r24);
3512     __get_user(env->regs[25], &sc->regs.r25);
3513     __get_user(env->regs[26], &sc->regs.r26);
3514     __get_user(env->regs[27], &sc->regs.r27);
3515     __get_user(env->regs[28], &sc->regs.r28);
3516     __get_user(env->regs[29], &sc->regs.r29);
3517     __get_user(env->regs[30], &sc->regs.r30);
3518     __get_user(env->regs[31], &sc->regs.r31);
3519     __get_user(env->sregs[SR_PC], &sc->regs.pc);
3520 }
3521 
3522 static abi_ulong get_sigframe(struct target_sigaction *ka,
3523                               CPUMBState *env, int frame_size)
3524 {
3525     abi_ulong sp = env->regs[1];
3526 
3527     if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3528         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3529 
3530     return ((sp - frame_size) & -8UL);
3531 }
3532 
3533 static void setup_frame(int sig, struct target_sigaction *ka,
3534                         target_sigset_t *set, CPUMBState *env)
3535 {
3536     struct target_signal_frame *frame;
3537     abi_ulong frame_addr;
3538     int err = 0;
3539     int i;
3540 
3541     frame_addr = get_sigframe(ka, env, sizeof *frame);
3542     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3543         goto badframe;
3544 
3545     /* Save the mask.  */
3546     err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3547     if (err)
3548         goto badframe;
3549 
3550     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3551         if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3552             goto badframe;
3553     }
3554 
3555     setup_sigcontext(&frame->uc.tuc_mcontext, env);
3556 
3557     /* Set up to return from userspace. If provided, use a stub
3558        already in userspace. */
3559     /* minus 8 is offset to cater for "rtsd r15,8" offset */
3560     if (ka->sa_flags & TARGET_SA_RESTORER) {
3561         env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3562     } else {
3563         uint32_t t;
3564         /* Note, these encodings are _big endian_! */
3565         /* addi r12, r0, __NR_sigreturn */
3566         t = 0x31800000UL | TARGET_NR_sigreturn;
3567         err |= __put_user(t, frame->tramp + 0);
3568         /* brki r14, 0x8 */
3569         t = 0xb9cc0008UL;
3570         err |= __put_user(t, frame->tramp + 1);
3571 
3572         /* Return from sighandler will jump to the tramp.
3573            Negative 8 offset because return is rtsd r15, 8 */
3574         env->regs[15] = ((unsigned long)frame->tramp) - 8;
3575     }
3576 
3577     if (err)
3578         goto badframe;
3579 
3580     /* Set up registers for signal handler */
3581     env->regs[1] = frame_addr;
3582     /* Signal handler args: */
3583     env->regs[5] = sig; /* Arg 0: signum */
3584     env->regs[6] = 0;
3585     /* arg 1: sigcontext */
3586     env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3587 
3588     /* Offset of 4 to handle microblaze rtid r14, 0 */
3589     env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3590 
3591     unlock_user_struct(frame, frame_addr, 1);
3592     return;
3593   badframe:
3594     unlock_user_struct(frame, frame_addr, 1);
3595     force_sig(TARGET_SIGSEGV);
3596 }
3597 
3598 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3599                            target_siginfo_t *info,
3600                            target_sigset_t *set, CPUMBState *env)
3601 {
3602     fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3603 }
3604 
3605 long do_sigreturn(CPUMBState *env)
3606 {
3607     struct target_signal_frame *frame;
3608     abi_ulong frame_addr;
3609     target_sigset_t target_set;
3610     sigset_t set;
3611     int i;
3612 
3613     frame_addr = env->regs[R_SP];
3614     /* Make sure the guest isn't playing games.  */
3615     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3616         goto badframe;
3617 
3618     /* Restore blocked signals */
3619     if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3620         goto badframe;
3621     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3622         if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3623             goto badframe;
3624     }
3625     target_to_host_sigset_internal(&set, &target_set);
3626     sigprocmask(SIG_SETMASK, &set, NULL);
3627 
3628     restore_sigcontext(&frame->uc.tuc_mcontext, env);
3629     /* We got here through a sigreturn syscall, our path back is via an
3630        rtb insn so setup r14 for that.  */
3631     env->regs[14] = env->sregs[SR_PC];
3632 
3633     unlock_user_struct(frame, frame_addr, 0);
3634     return env->regs[10];
3635   badframe:
3636     unlock_user_struct(frame, frame_addr, 0);
3637     force_sig(TARGET_SIGSEGV);
3638 }
3639 
3640 long do_rt_sigreturn(CPUMBState *env)
3641 {
3642     fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3643     return -TARGET_ENOSYS;
3644 }
3645 
3646 #elif defined(TARGET_CRIS)
3647 
3648 struct target_sigcontext {
3649         struct target_pt_regs regs;  /* needs to be first */
3650         uint32_t oldmask;
3651         uint32_t usp;    /* usp before stacking this gunk on it */
3652 };
3653 
3654 /* Signal frames. */
3655 struct target_signal_frame {
3656         struct target_sigcontext sc;
3657         uint32_t extramask[TARGET_NSIG_WORDS - 1];
3658         uint8_t retcode[8];       /* Trampoline code. */
3659 };
3660 
3661 struct rt_signal_frame {
3662         siginfo_t *pinfo;
3663         void *puc;
3664         siginfo_t info;
3665         struct ucontext uc;
3666         uint8_t retcode[8];       /* Trampoline code. */
3667 };
3668 
3669 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3670 {
3671 	__put_user(env->regs[0], &sc->regs.r0);
3672 	__put_user(env->regs[1], &sc->regs.r1);
3673 	__put_user(env->regs[2], &sc->regs.r2);
3674 	__put_user(env->regs[3], &sc->regs.r3);
3675 	__put_user(env->regs[4], &sc->regs.r4);
3676 	__put_user(env->regs[5], &sc->regs.r5);
3677 	__put_user(env->regs[6], &sc->regs.r6);
3678 	__put_user(env->regs[7], &sc->regs.r7);
3679 	__put_user(env->regs[8], &sc->regs.r8);
3680 	__put_user(env->regs[9], &sc->regs.r9);
3681 	__put_user(env->regs[10], &sc->regs.r10);
3682 	__put_user(env->regs[11], &sc->regs.r11);
3683 	__put_user(env->regs[12], &sc->regs.r12);
3684 	__put_user(env->regs[13], &sc->regs.r13);
3685 	__put_user(env->regs[14], &sc->usp);
3686 	__put_user(env->regs[15], &sc->regs.acr);
3687 	__put_user(env->pregs[PR_MOF], &sc->regs.mof);
3688 	__put_user(env->pregs[PR_SRP], &sc->regs.srp);
3689 	__put_user(env->pc, &sc->regs.erp);
3690 }
3691 
3692 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3693 {
3694 	__get_user(env->regs[0], &sc->regs.r0);
3695 	__get_user(env->regs[1], &sc->regs.r1);
3696 	__get_user(env->regs[2], &sc->regs.r2);
3697 	__get_user(env->regs[3], &sc->regs.r3);
3698 	__get_user(env->regs[4], &sc->regs.r4);
3699 	__get_user(env->regs[5], &sc->regs.r5);
3700 	__get_user(env->regs[6], &sc->regs.r6);
3701 	__get_user(env->regs[7], &sc->regs.r7);
3702 	__get_user(env->regs[8], &sc->regs.r8);
3703 	__get_user(env->regs[9], &sc->regs.r9);
3704 	__get_user(env->regs[10], &sc->regs.r10);
3705 	__get_user(env->regs[11], &sc->regs.r11);
3706 	__get_user(env->regs[12], &sc->regs.r12);
3707 	__get_user(env->regs[13], &sc->regs.r13);
3708 	__get_user(env->regs[14], &sc->usp);
3709 	__get_user(env->regs[15], &sc->regs.acr);
3710 	__get_user(env->pregs[PR_MOF], &sc->regs.mof);
3711 	__get_user(env->pregs[PR_SRP], &sc->regs.srp);
3712 	__get_user(env->pc, &sc->regs.erp);
3713 }
3714 
3715 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3716 {
3717 	abi_ulong sp;
3718 	/* Align the stack downwards to 4.  */
3719 	sp = (env->regs[R_SP] & ~3);
3720 	return sp - framesize;
3721 }
3722 
3723 static void setup_frame(int sig, struct target_sigaction *ka,
3724                         target_sigset_t *set, CPUCRISState *env)
3725 {
3726 	struct target_signal_frame *frame;
3727 	abi_ulong frame_addr;
3728 	int err = 0;
3729 	int i;
3730 
3731 	frame_addr = get_sigframe(env, sizeof *frame);
3732 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3733 		goto badframe;
3734 
3735 	/*
3736 	 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3737 	 * use this trampoline anymore but it sets it up for GDB.
3738 	 * In QEMU, using the trampoline simplifies things a bit so we use it.
3739 	 *
3740 	 * This is movu.w __NR_sigreturn, r9; break 13;
3741 	 */
3742 	err |= __put_user(0x9c5f, frame->retcode+0);
3743 	err |= __put_user(TARGET_NR_sigreturn,
3744 			  frame->retcode+2);
3745 	err |= __put_user(0xe93d, frame->retcode+4);
3746 
3747 	/* Save the mask.  */
3748 	err |= __put_user(set->sig[0], &frame->sc.oldmask);
3749 	if (err)
3750 		goto badframe;
3751 
3752 	for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3753 		if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3754 			goto badframe;
3755 	}
3756 
3757 	setup_sigcontext(&frame->sc, env);
3758 
3759 	/* Move the stack and setup the arguments for the handler.  */
3760 	env->regs[R_SP] = frame_addr;
3761 	env->regs[10] = sig;
3762 	env->pc = (unsigned long) ka->_sa_handler;
3763 	/* Link SRP so the guest returns through the trampoline.  */
3764 	env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3765 
3766 	unlock_user_struct(frame, frame_addr, 1);
3767 	return;
3768   badframe:
3769 	unlock_user_struct(frame, frame_addr, 1);
3770 	force_sig(TARGET_SIGSEGV);
3771 }
3772 
3773 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3774                            target_siginfo_t *info,
3775                            target_sigset_t *set, CPUCRISState *env)
3776 {
3777     fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3778 }
3779 
3780 long do_sigreturn(CPUCRISState *env)
3781 {
3782 	struct target_signal_frame *frame;
3783 	abi_ulong frame_addr;
3784 	target_sigset_t target_set;
3785 	sigset_t set;
3786 	int i;
3787 
3788 	frame_addr = env->regs[R_SP];
3789 	/* Make sure the guest isn't playing games.  */
3790 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3791 		goto badframe;
3792 
3793 	/* Restore blocked signals */
3794 	if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3795 		goto badframe;
3796 	for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3797 		if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3798 			goto badframe;
3799 	}
3800 	target_to_host_sigset_internal(&set, &target_set);
3801 	sigprocmask(SIG_SETMASK, &set, NULL);
3802 
3803 	restore_sigcontext(&frame->sc, env);
3804 	unlock_user_struct(frame, frame_addr, 0);
3805 	return env->regs[10];
3806   badframe:
3807 	unlock_user_struct(frame, frame_addr, 0);
3808 	force_sig(TARGET_SIGSEGV);
3809 }
3810 
3811 long do_rt_sigreturn(CPUCRISState *env)
3812 {
3813     fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3814     return -TARGET_ENOSYS;
3815 }
3816 
3817 #elif defined(TARGET_OPENRISC)
3818 
3819 struct target_sigcontext {
3820     struct target_pt_regs regs;
3821     abi_ulong oldmask;
3822     abi_ulong usp;
3823 };
3824 
3825 struct target_ucontext {
3826     abi_ulong tuc_flags;
3827     abi_ulong tuc_link;
3828     target_stack_t tuc_stack;
3829     struct target_sigcontext tuc_mcontext;
3830     target_sigset_t tuc_sigmask;   /* mask last for extensibility */
3831 };
3832 
3833 struct target_rt_sigframe {
3834     abi_ulong pinfo;
3835     uint64_t puc;
3836     struct target_siginfo info;
3837     struct target_sigcontext sc;
3838     struct target_ucontext uc;
3839     unsigned char retcode[16];  /* trampoline code */
3840 };
3841 
3842 /* This is the asm-generic/ucontext.h version */
3843 #if 0
3844 static int restore_sigcontext(CPUOpenRISCState *regs,
3845                               struct target_sigcontext *sc)
3846 {
3847     unsigned int err = 0;
3848     unsigned long old_usp;
3849 
3850     /* Alwys make any pending restarted system call return -EINTR */
3851     current_thread_info()->restart_block.fn = do_no_restart_syscall;
3852 
3853     /* restore the regs from &sc->regs (same as sc, since regs is first)
3854      * (sc is already checked for VERIFY_READ since the sigframe was
3855      *  checked in sys_sigreturn previously)
3856      */
3857 
3858     if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3859         goto badframe;
3860     }
3861 
3862     /* make sure the U-flag is set so user-mode cannot fool us */
3863 
3864     regs->sr &= ~SR_SM;
3865 
3866     /* restore the old USP as it was before we stacked the sc etc.
3867      * (we cannot just pop the sigcontext since we aligned the sp and
3868      *  stuff after pushing it)
3869      */
3870 
3871     err |= __get_user(old_usp, &sc->usp);
3872     phx_signal("old_usp 0x%lx", old_usp);
3873 
3874     __PHX__ REALLY           /* ??? */
3875     wrusp(old_usp);
3876     regs->gpr[1] = old_usp;
3877 
3878     /* TODO: the other ports use regs->orig_XX to disable syscall checks
3879      * after this completes, but we don't use that mechanism. maybe we can
3880      * use it now ?
3881      */
3882 
3883     return err;
3884 
3885 badframe:
3886     return 1;
3887 }
3888 #endif
3889 
3890 /* Set up a signal frame.  */
3891 
3892 static int setup_sigcontext(struct target_sigcontext *sc,
3893                             CPUOpenRISCState *regs,
3894                             unsigned long mask)
3895 {
3896     int err = 0;
3897     unsigned long usp = regs->gpr[1];
3898 
3899     /* copy the regs. they are first in sc so we can use sc directly */
3900 
3901     /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3902 
3903     /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3904        the signal handler. The frametype will be restored to its previous
3905        value in restore_sigcontext. */
3906     /*regs->frametype = CRIS_FRAME_NORMAL;*/
3907 
3908     /* then some other stuff */
3909     err |= __put_user(mask, &sc->oldmask);
3910     err |= __put_user(usp, &sc->usp); return err;
3911 }
3912 
3913 static inline unsigned long align_sigframe(unsigned long sp)
3914 {
3915     unsigned long i;
3916     i = sp & ~3UL;
3917     return i;
3918 }
3919 
3920 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3921                                      CPUOpenRISCState *regs,
3922                                      size_t frame_size)
3923 {
3924     unsigned long sp = regs->gpr[1];
3925     int onsigstack = on_sig_stack(sp);
3926 
3927     /* redzone */
3928     /* This is the X/Open sanctioned signal stack switching.  */
3929     if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3930         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3931     }
3932 
3933     sp = align_sigframe(sp - frame_size);
3934 
3935     /*
3936      * If we are on the alternate signal stack and would overflow it, don't.
3937      * Return an always-bogus address instead so we will die with SIGSEGV.
3938      */
3939 
3940     if (onsigstack && !likely(on_sig_stack(sp))) {
3941         return -1L;
3942     }
3943 
3944     return sp;
3945 }
3946 
3947 static void setup_frame(int sig, struct target_sigaction *ka,
3948                         target_sigset_t *set, CPUOpenRISCState *env)
3949 {
3950     qemu_log("Not implement.\n");
3951 }
3952 
3953 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3954                            target_siginfo_t *info,
3955                            target_sigset_t *set, CPUOpenRISCState *env)
3956 {
3957     int err = 0;
3958     abi_ulong frame_addr;
3959     unsigned long return_ip;
3960     struct target_rt_sigframe *frame;
3961     abi_ulong info_addr, uc_addr;
3962 
3963     frame_addr = get_sigframe(ka, env, sizeof *frame);
3964 
3965     frame_addr = get_sigframe(ka, env, sizeof(*frame));
3966     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3967         goto give_sigsegv;
3968     }
3969 
3970     info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3971     err |= __put_user(info_addr, &frame->pinfo);
3972     uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3973     err |= __put_user(uc_addr, &frame->puc);
3974 
3975     if (ka->sa_flags & SA_SIGINFO) {
3976         err |= copy_siginfo_to_user(&frame->info, info);
3977     }
3978     if (err) {
3979         goto give_sigsegv;
3980     }
3981 
3982     /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3983     err |= __put_user(0, &frame->uc.tuc_flags);
3984     err |= __put_user(0, &frame->uc.tuc_link);
3985     err |= __put_user(target_sigaltstack_used.ss_sp,
3986                       &frame->uc.tuc_stack.ss_sp);
3987     err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3988     err |= __put_user(target_sigaltstack_used.ss_size,
3989                       &frame->uc.tuc_stack.ss_size);
3990     err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
3991 
3992     /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3993 
3994     if (err) {
3995         goto give_sigsegv;
3996     }
3997 
3998     /* trampoline - the desired return ip is the retcode itself */
3999     return_ip = (unsigned long)&frame->retcode;
4000     /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
4001     err |= __put_user(0xa960, (short *)(frame->retcode + 0));
4002     err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
4003     err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
4004     err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
4005 
4006     if (err) {
4007         goto give_sigsegv;
4008     }
4009 
4010     /* TODO what is the current->exec_domain stuff and invmap ? */
4011 
4012     /* Set up registers for signal handler */
4013     env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
4014     env->gpr[9] = (unsigned long)return_ip;     /* what we enter LATER */
4015     env->gpr[3] = (unsigned long)sig;           /* arg 1: signo */
4016     env->gpr[4] = (unsigned long)&frame->info;  /* arg 2: (siginfo_t*) */
4017     env->gpr[5] = (unsigned long)&frame->uc;    /* arg 3: ucontext */
4018 
4019     /* actually move the usp to reflect the stacked frame */
4020     env->gpr[1] = (unsigned long)frame;
4021 
4022     return;
4023 
4024 give_sigsegv:
4025     unlock_user_struct(frame, frame_addr, 1);
4026     if (sig == TARGET_SIGSEGV) {
4027         ka->_sa_handler = TARGET_SIG_DFL;
4028     }
4029     force_sig(TARGET_SIGSEGV);
4030 }
4031 
4032 long do_sigreturn(CPUOpenRISCState *env)
4033 {
4034 
4035     qemu_log("do_sigreturn: not implemented\n");
4036     return -TARGET_ENOSYS;
4037 }
4038 
4039 long do_rt_sigreturn(CPUOpenRISCState *env)
4040 {
4041     qemu_log("do_rt_sigreturn: not implemented\n");
4042     return -TARGET_ENOSYS;
4043 }
4044 /* TARGET_OPENRISC */
4045 
4046 #elif defined(TARGET_S390X)
4047 
4048 #define __NUM_GPRS 16
4049 #define __NUM_FPRS 16
4050 #define __NUM_ACRS 16
4051 
4052 #define S390_SYSCALL_SIZE   2
4053 #define __SIGNAL_FRAMESIZE      160 /* FIXME: 31-bit mode -> 96 */
4054 
4055 #define _SIGCONTEXT_NSIG        64
4056 #define _SIGCONTEXT_NSIG_BPW    64 /* FIXME: 31-bit mode -> 32 */
4057 #define _SIGCONTEXT_NSIG_WORDS  (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4058 #define _SIGMASK_COPY_SIZE    (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4059 #define PSW_ADDR_AMODE            0x0000000000000000UL /* 0x80000000UL for 31-bit */
4060 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4061 
4062 typedef struct {
4063     target_psw_t psw;
4064     target_ulong gprs[__NUM_GPRS];
4065     unsigned int acrs[__NUM_ACRS];
4066 } target_s390_regs_common;
4067 
4068 typedef struct {
4069     unsigned int fpc;
4070     double   fprs[__NUM_FPRS];
4071 } target_s390_fp_regs;
4072 
4073 typedef struct {
4074     target_s390_regs_common regs;
4075     target_s390_fp_regs     fpregs;
4076 } target_sigregs;
4077 
4078 struct target_sigcontext {
4079     target_ulong   oldmask[_SIGCONTEXT_NSIG_WORDS];
4080     target_sigregs *sregs;
4081 };
4082 
4083 typedef struct {
4084     uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4085     struct target_sigcontext sc;
4086     target_sigregs sregs;
4087     int signo;
4088     uint8_t retcode[S390_SYSCALL_SIZE];
4089 } sigframe;
4090 
4091 struct target_ucontext {
4092     target_ulong tuc_flags;
4093     struct target_ucontext *tuc_link;
4094     target_stack_t tuc_stack;
4095     target_sigregs tuc_mcontext;
4096     target_sigset_t tuc_sigmask;   /* mask last for extensibility */
4097 };
4098 
4099 typedef struct {
4100     uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4101     uint8_t retcode[S390_SYSCALL_SIZE];
4102     struct target_siginfo info;
4103     struct target_ucontext uc;
4104 } rt_sigframe;
4105 
4106 static inline abi_ulong
4107 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4108 {
4109     abi_ulong sp;
4110 
4111     /* Default to using normal stack */
4112     sp = env->regs[15];
4113 
4114     /* This is the X/Open sanctioned signal stack switching.  */
4115     if (ka->sa_flags & TARGET_SA_ONSTACK) {
4116         if (!sas_ss_flags(sp)) {
4117             sp = target_sigaltstack_used.ss_sp +
4118                  target_sigaltstack_used.ss_size;
4119         }
4120     }
4121 
4122     /* This is the legacy signal stack switching. */
4123     else if (/* FIXME !user_mode(regs) */ 0 &&
4124              !(ka->sa_flags & TARGET_SA_RESTORER) &&
4125              ka->sa_restorer) {
4126         sp = (abi_ulong) ka->sa_restorer;
4127     }
4128 
4129     return (sp - frame_size) & -8ul;
4130 }
4131 
4132 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4133 {
4134     int i;
4135     //save_access_regs(current->thread.acrs); FIXME
4136 
4137     /* Copy a 'clean' PSW mask to the user to avoid leaking
4138        information about whether PER is currently on.  */
4139     __put_user(env->psw.mask, &sregs->regs.psw.mask);
4140     __put_user(env->psw.addr, &sregs->regs.psw.addr);
4141     for (i = 0; i < 16; i++) {
4142         __put_user(env->regs[i], &sregs->regs.gprs[i]);
4143     }
4144     for (i = 0; i < 16; i++) {
4145         __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4146     }
4147     /*
4148      * We have to store the fp registers to current->thread.fp_regs
4149      * to merge them with the emulated registers.
4150      */
4151     //save_fp_regs(&current->thread.fp_regs); FIXME
4152     for (i = 0; i < 16; i++) {
4153         __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
4154     }
4155 }
4156 
4157 static void setup_frame(int sig, struct target_sigaction *ka,
4158                         target_sigset_t *set, CPUS390XState *env)
4159 {
4160     sigframe *frame;
4161     abi_ulong frame_addr;
4162 
4163     frame_addr = get_sigframe(ka, env, sizeof(*frame));
4164     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4165              (unsigned long long)frame_addr);
4166     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4167             goto give_sigsegv;
4168     }
4169 
4170     qemu_log("%s: 1\n", __FUNCTION__);
4171     if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
4172               goto give_sigsegv;
4173     }
4174 
4175     save_sigregs(env, &frame->sregs);
4176 
4177     __put_user((abi_ulong)(unsigned long)&frame->sregs,
4178                (abi_ulong *)&frame->sc.sregs);
4179 
4180     /* Set up to return from userspace.  If provided, use a stub
4181        already in userspace.  */
4182     if (ka->sa_flags & TARGET_SA_RESTORER) {
4183             env->regs[14] = (unsigned long)
4184                     ka->sa_restorer | PSW_ADDR_AMODE;
4185     } else {
4186             env->regs[14] = (unsigned long)
4187                     frame->retcode | PSW_ADDR_AMODE;
4188             if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4189                            (uint16_t *)(frame->retcode)))
4190                     goto give_sigsegv;
4191     }
4192 
4193     /* Set up backchain. */
4194     if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4195             goto give_sigsegv;
4196     }
4197 
4198     /* Set up registers for signal handler */
4199     env->regs[15] = frame_addr;
4200     env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4201 
4202     env->regs[2] = sig; //map_signal(sig);
4203     env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4204 
4205     /* We forgot to include these in the sigcontext.
4206        To avoid breaking binary compatibility, they are passed as args. */
4207     env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4208     env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4209 
4210     /* Place signal number on stack to allow backtrace from handler.  */
4211     if (__put_user(env->regs[2], (int *) &frame->signo)) {
4212             goto give_sigsegv;
4213     }
4214     unlock_user_struct(frame, frame_addr, 1);
4215     return;
4216 
4217 give_sigsegv:
4218     qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4219     unlock_user_struct(frame, frame_addr, 1);
4220     force_sig(TARGET_SIGSEGV);
4221 }
4222 
4223 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4224                            target_siginfo_t *info,
4225                            target_sigset_t *set, CPUS390XState *env)
4226 {
4227     int i;
4228     rt_sigframe *frame;
4229     abi_ulong frame_addr;
4230 
4231     frame_addr = get_sigframe(ka, env, sizeof *frame);
4232     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4233              (unsigned long long)frame_addr);
4234     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4235         goto give_sigsegv;
4236     }
4237 
4238     qemu_log("%s: 1\n", __FUNCTION__);
4239     if (copy_siginfo_to_user(&frame->info, info)) {
4240         goto give_sigsegv;
4241     }
4242 
4243     /* Create the ucontext.  */
4244     __put_user(0, &frame->uc.tuc_flags);
4245     __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4246     __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4247     __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4248                       &frame->uc.tuc_stack.ss_flags);
4249     __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4250     save_sigregs(env, &frame->uc.tuc_mcontext);
4251     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4252         __put_user((abi_ulong)set->sig[i],
4253         (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4254     }
4255 
4256     /* Set up to return from userspace.  If provided, use a stub
4257        already in userspace.  */
4258     if (ka->sa_flags & TARGET_SA_RESTORER) {
4259         env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4260     } else {
4261         env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4262         if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4263                        (uint16_t *)(frame->retcode))) {
4264             goto give_sigsegv;
4265         }
4266     }
4267 
4268     /* Set up backchain. */
4269     if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4270         goto give_sigsegv;
4271     }
4272 
4273     /* Set up registers for signal handler */
4274     env->regs[15] = frame_addr;
4275     env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4276 
4277     env->regs[2] = sig; //map_signal(sig);
4278     env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4279     env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4280     return;
4281 
4282 give_sigsegv:
4283     qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4284     unlock_user_struct(frame, frame_addr, 1);
4285     force_sig(TARGET_SIGSEGV);
4286 }
4287 
4288 static int
4289 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4290 {
4291     int err = 0;
4292     int i;
4293 
4294     for (i = 0; i < 16; i++) {
4295         err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4296     }
4297 
4298     err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4299     qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4300              __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4301              (unsigned long long)env->psw.addr);
4302     err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4303     /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4304 
4305     for (i = 0; i < 16; i++) {
4306         err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4307     }
4308     for (i = 0; i < 16; i++) {
4309         err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4310     }
4311 
4312     return err;
4313 }
4314 
4315 long do_sigreturn(CPUS390XState *env)
4316 {
4317     sigframe *frame;
4318     abi_ulong frame_addr = env->regs[15];
4319     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4320              (unsigned long long)frame_addr);
4321     target_sigset_t target_set;
4322     sigset_t set;
4323 
4324     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4325         goto badframe;
4326     }
4327     if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4328         goto badframe;
4329     }
4330 
4331     target_to_host_sigset_internal(&set, &target_set);
4332     sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4333 
4334     if (restore_sigregs(env, &frame->sregs)) {
4335         goto badframe;
4336     }
4337 
4338     unlock_user_struct(frame, frame_addr, 0);
4339     return env->regs[2];
4340 
4341 badframe:
4342     unlock_user_struct(frame, frame_addr, 0);
4343     force_sig(TARGET_SIGSEGV);
4344     return 0;
4345 }
4346 
4347 long do_rt_sigreturn(CPUS390XState *env)
4348 {
4349     rt_sigframe *frame;
4350     abi_ulong frame_addr = env->regs[15];
4351     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4352              (unsigned long long)frame_addr);
4353     sigset_t set;
4354 
4355     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4356         goto badframe;
4357     }
4358     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4359 
4360     sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4361 
4362     if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4363         goto badframe;
4364     }
4365 
4366     if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4367                        get_sp_from_cpustate(env)) == -EFAULT) {
4368         goto badframe;
4369     }
4370     unlock_user_struct(frame, frame_addr, 0);
4371     return env->regs[2];
4372 
4373 badframe:
4374     unlock_user_struct(frame, frame_addr, 0);
4375     force_sig(TARGET_SIGSEGV);
4376     return 0;
4377 }
4378 
4379 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4380 
4381 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4382    the signal handling is different enough that we haven't implemented
4383    support for PPC64 yet.  Hence the restriction above.
4384 
4385    There are various #if'd blocks for code for TARGET_PPC64.  These
4386    blocks should go away so that we can successfully run 32-bit and
4387    64-bit binaries on a QEMU configured for PPC64.  */
4388 
4389 /* Size of dummy stack frame allocated when calling signal handler.
4390    See arch/powerpc/include/asm/ptrace.h.  */
4391 #if defined(TARGET_PPC64)
4392 #define SIGNAL_FRAMESIZE 128
4393 #else
4394 #define SIGNAL_FRAMESIZE 64
4395 #endif
4396 
4397 /* See arch/powerpc/include/asm/sigcontext.h.  */
4398 struct target_sigcontext {
4399     target_ulong _unused[4];
4400     int32_t signal;
4401 #if defined(TARGET_PPC64)
4402     int32_t pad0;
4403 #endif
4404     target_ulong handler;
4405     target_ulong oldmask;
4406     target_ulong regs;      /* struct pt_regs __user * */
4407     /* TODO: PPC64 includes extra bits here.  */
4408 };
4409 
4410 /* Indices for target_mcontext.mc_gregs, below.
4411    See arch/powerpc/include/asm/ptrace.h for details.  */
4412 enum {
4413     TARGET_PT_R0 = 0,
4414     TARGET_PT_R1 = 1,
4415     TARGET_PT_R2 = 2,
4416     TARGET_PT_R3 = 3,
4417     TARGET_PT_R4 = 4,
4418     TARGET_PT_R5 = 5,
4419     TARGET_PT_R6 = 6,
4420     TARGET_PT_R7 = 7,
4421     TARGET_PT_R8 = 8,
4422     TARGET_PT_R9 = 9,
4423     TARGET_PT_R10 = 10,
4424     TARGET_PT_R11 = 11,
4425     TARGET_PT_R12 = 12,
4426     TARGET_PT_R13 = 13,
4427     TARGET_PT_R14 = 14,
4428     TARGET_PT_R15 = 15,
4429     TARGET_PT_R16 = 16,
4430     TARGET_PT_R17 = 17,
4431     TARGET_PT_R18 = 18,
4432     TARGET_PT_R19 = 19,
4433     TARGET_PT_R20 = 20,
4434     TARGET_PT_R21 = 21,
4435     TARGET_PT_R22 = 22,
4436     TARGET_PT_R23 = 23,
4437     TARGET_PT_R24 = 24,
4438     TARGET_PT_R25 = 25,
4439     TARGET_PT_R26 = 26,
4440     TARGET_PT_R27 = 27,
4441     TARGET_PT_R28 = 28,
4442     TARGET_PT_R29 = 29,
4443     TARGET_PT_R30 = 30,
4444     TARGET_PT_R31 = 31,
4445     TARGET_PT_NIP = 32,
4446     TARGET_PT_MSR = 33,
4447     TARGET_PT_ORIG_R3 = 34,
4448     TARGET_PT_CTR = 35,
4449     TARGET_PT_LNK = 36,
4450     TARGET_PT_XER = 37,
4451     TARGET_PT_CCR = 38,
4452     /* Yes, there are two registers with #39.  One is 64-bit only.  */
4453     TARGET_PT_MQ = 39,
4454     TARGET_PT_SOFTE = 39,
4455     TARGET_PT_TRAP = 40,
4456     TARGET_PT_DAR = 41,
4457     TARGET_PT_DSISR = 42,
4458     TARGET_PT_RESULT = 43,
4459     TARGET_PT_REGS_COUNT = 44
4460 };
4461 
4462 /* See arch/powerpc/include/asm/ucontext.h.  Only used for 32-bit PPC;
4463    on 64-bit PPC, sigcontext and mcontext are one and the same.  */
4464 struct target_mcontext {
4465     target_ulong mc_gregs[48];
4466     /* Includes fpscr.  */
4467     uint64_t mc_fregs[33];
4468     target_ulong mc_pad[2];
4469     /* We need to handle Altivec and SPE at the same time, which no
4470        kernel needs to do.  Fortunately, the kernel defines this bit to
4471        be Altivec-register-large all the time, rather than trying to
4472        twiddle it based on the specific platform.  */
4473     union {
4474         /* SPE vector registers.  One extra for SPEFSCR.  */
4475         uint32_t spe[33];
4476         /* Altivec vector registers.  The packing of VSCR and VRSAVE
4477            varies depending on whether we're PPC64 or not: PPC64 splits
4478            them apart; PPC32 stuffs them together.  */
4479 #if defined(TARGET_PPC64)
4480 #define QEMU_NVRREG 34
4481 #else
4482 #define QEMU_NVRREG 33
4483 #endif
4484         ppc_avr_t altivec[QEMU_NVRREG];
4485 #undef QEMU_NVRREG
4486     } mc_vregs __attribute__((__aligned__(16)));
4487 };
4488 
4489 struct target_ucontext {
4490     target_ulong tuc_flags;
4491     target_ulong tuc_link;    /* struct ucontext __user * */
4492     struct target_sigaltstack tuc_stack;
4493 #if !defined(TARGET_PPC64)
4494     int32_t tuc_pad[7];
4495     target_ulong tuc_regs;    /* struct mcontext __user *
4496                                 points to uc_mcontext field */
4497 #endif
4498     target_sigset_t tuc_sigmask;
4499 #if defined(TARGET_PPC64)
4500     target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4501     struct target_sigcontext tuc_mcontext;
4502 #else
4503     int32_t tuc_maskext[30];
4504     int32_t tuc_pad2[3];
4505     struct target_mcontext tuc_mcontext;
4506 #endif
4507 };
4508 
4509 /* See arch/powerpc/kernel/signal_32.c.  */
4510 struct target_sigframe {
4511     struct target_sigcontext sctx;
4512     struct target_mcontext mctx;
4513     int32_t abigap[56];
4514 };
4515 
4516 struct target_rt_sigframe {
4517     struct target_siginfo info;
4518     struct target_ucontext uc;
4519     int32_t abigap[56];
4520 };
4521 
4522 /* We use the mc_pad field for the signal return trampoline.  */
4523 #define tramp mc_pad
4524 
4525 /* See arch/powerpc/kernel/signal.c.  */
4526 static target_ulong get_sigframe(struct target_sigaction *ka,
4527                                  CPUPPCState *env,
4528                                  int frame_size)
4529 {
4530     target_ulong oldsp, newsp;
4531 
4532     oldsp = env->gpr[1];
4533 
4534     if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4535         (sas_ss_flags(oldsp) == 0)) {
4536         oldsp = (target_sigaltstack_used.ss_sp
4537                  + target_sigaltstack_used.ss_size);
4538     }
4539 
4540     newsp = (oldsp - frame_size) & ~0xFUL;
4541 
4542     return newsp;
4543 }
4544 
4545 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4546                           int sigret)
4547 {
4548     target_ulong msr = env->msr;
4549     int i;
4550     target_ulong ccr = 0;
4551 
4552     /* In general, the kernel attempts to be intelligent about what it
4553        needs to save for Altivec/FP/SPE registers.  We don't care that
4554        much, so we just go ahead and save everything.  */
4555 
4556     /* Save general registers.  */
4557     for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4558         if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4559             return 1;
4560         }
4561     }
4562     if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4563         || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4564         || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4565         || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4566         return 1;
4567 
4568     for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4569         ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4570     }
4571     if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4572         return 1;
4573 
4574     /* Save Altivec registers if necessary.  */
4575     if (env->insns_flags & PPC_ALTIVEC) {
4576         for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4577             ppc_avr_t *avr = &env->avr[i];
4578             ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4579 
4580             if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4581                 __put_user(avr->u64[1], &vreg->u64[1])) {
4582                 return 1;
4583             }
4584         }
4585         /* Set MSR_VR in the saved MSR value to indicate that
4586            frame->mc_vregs contains valid data.  */
4587         msr |= MSR_VR;
4588         if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4589                        &frame->mc_vregs.altivec[32].u32[3]))
4590             return 1;
4591     }
4592 
4593     /* Save floating point registers.  */
4594     if (env->insns_flags & PPC_FLOAT) {
4595         for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4596             if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4597                 return 1;
4598             }
4599         }
4600         if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4601             return 1;
4602     }
4603 
4604     /* Save SPE registers.  The kernel only saves the high half.  */
4605     if (env->insns_flags & PPC_SPE) {
4606 #if defined(TARGET_PPC64)
4607         for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4608             if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4609                 return 1;
4610             }
4611         }
4612 #else
4613         for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4614             if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4615                 return 1;
4616             }
4617         }
4618 #endif
4619         /* Set MSR_SPE in the saved MSR value to indicate that
4620            frame->mc_vregs contains valid data.  */
4621         msr |= MSR_SPE;
4622         if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4623             return 1;
4624     }
4625 
4626     /* Store MSR.  */
4627     if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4628         return 1;
4629 
4630     /* Set up the sigreturn trampoline: li r0,sigret; sc.  */
4631     if (sigret) {
4632         if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4633             __put_user(0x44000002UL, &frame->tramp[1])) {
4634             return 1;
4635         }
4636     }
4637 
4638     return 0;
4639 }
4640 
4641 static int restore_user_regs(CPUPPCState *env,
4642                              struct target_mcontext *frame, int sig)
4643 {
4644     target_ulong save_r2 = 0;
4645     target_ulong msr;
4646     target_ulong ccr;
4647 
4648     int i;
4649 
4650     if (!sig) {
4651         save_r2 = env->gpr[2];
4652     }
4653 
4654     /* Restore general registers.  */
4655     for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4656         if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4657             return 1;
4658         }
4659     }
4660     if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4661         || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4662         || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4663         || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4664         return 1;
4665     if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4666         return 1;
4667 
4668     for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4669         env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4670     }
4671 
4672     if (!sig) {
4673         env->gpr[2] = save_r2;
4674     }
4675     /* Restore MSR.  */
4676     if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4677         return 1;
4678 
4679     /* If doing signal return, restore the previous little-endian mode.  */
4680     if (sig)
4681         env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4682 
4683     /* Restore Altivec registers if necessary.  */
4684     if (env->insns_flags & PPC_ALTIVEC) {
4685         for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4686             ppc_avr_t *avr = &env->avr[i];
4687             ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4688 
4689             if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4690                 __get_user(avr->u64[1], &vreg->u64[1])) {
4691                 return 1;
4692             }
4693         }
4694         /* Set MSR_VEC in the saved MSR value to indicate that
4695            frame->mc_vregs contains valid data.  */
4696         if (__get_user(env->spr[SPR_VRSAVE],
4697                        (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4698             return 1;
4699     }
4700 
4701     /* Restore floating point registers.  */
4702     if (env->insns_flags & PPC_FLOAT) {
4703         uint64_t fpscr;
4704         for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4705             if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4706                 return 1;
4707             }
4708         }
4709         if (__get_user(fpscr, &frame->mc_fregs[32]))
4710             return 1;
4711         env->fpscr = (uint32_t) fpscr;
4712     }
4713 
4714     /* Save SPE registers.  The kernel only saves the high half.  */
4715     if (env->insns_flags & PPC_SPE) {
4716 #if defined(TARGET_PPC64)
4717         for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4718             uint32_t hi;
4719 
4720             if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4721                 return 1;
4722             }
4723             env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4724         }
4725 #else
4726         for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4727             if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4728                 return 1;
4729             }
4730         }
4731 #endif
4732         if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4733             return 1;
4734     }
4735 
4736     return 0;
4737 }
4738 
4739 static void setup_frame(int sig, struct target_sigaction *ka,
4740                         target_sigset_t *set, CPUPPCState *env)
4741 {
4742     struct target_sigframe *frame;
4743     struct target_sigcontext *sc;
4744     target_ulong frame_addr, newsp;
4745     int err = 0;
4746     int signal;
4747 
4748     frame_addr = get_sigframe(ka, env, sizeof(*frame));
4749     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4750         goto sigsegv;
4751     sc = &frame->sctx;
4752 
4753     signal = current_exec_domain_sig(sig);
4754 
4755     err |= __put_user(ka->_sa_handler, &sc->handler);
4756     err |= __put_user(set->sig[0], &sc->oldmask);
4757 #if defined(TARGET_PPC64)
4758     err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4759 #else
4760     err |= __put_user(set->sig[1], &sc->_unused[3]);
4761 #endif
4762     err |= __put_user(h2g(&frame->mctx), &sc->regs);
4763     err |= __put_user(sig, &sc->signal);
4764 
4765     /* Save user regs.  */
4766     err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4767 
4768     /* The kernel checks for the presence of a VDSO here.  We don't
4769        emulate a vdso, so use a sigreturn system call.  */
4770     env->lr = (target_ulong) h2g(frame->mctx.tramp);
4771 
4772     /* Turn off all fp exceptions.  */
4773     env->fpscr = 0;
4774 
4775     /* Create a stack frame for the caller of the handler.  */
4776     newsp = frame_addr - SIGNAL_FRAMESIZE;
4777     err |= put_user(env->gpr[1], newsp, target_ulong);
4778 
4779     if (err)
4780         goto sigsegv;
4781 
4782     /* Set up registers for signal handler.  */
4783     env->gpr[1] = newsp;
4784     env->gpr[3] = signal;
4785     env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4786     env->nip = (target_ulong) ka->_sa_handler;
4787     /* Signal handlers are entered in big-endian mode.  */
4788     env->msr &= ~MSR_LE;
4789 
4790     unlock_user_struct(frame, frame_addr, 1);
4791     return;
4792 
4793 sigsegv:
4794     unlock_user_struct(frame, frame_addr, 1);
4795     qemu_log("segfaulting from setup_frame\n");
4796     force_sig(TARGET_SIGSEGV);
4797 }
4798 
4799 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4800                            target_siginfo_t *info,
4801                            target_sigset_t *set, CPUPPCState *env)
4802 {
4803     struct target_rt_sigframe *rt_sf;
4804     struct target_mcontext *frame;
4805     target_ulong rt_sf_addr, newsp = 0;
4806     int i, err = 0;
4807     int signal;
4808 
4809     rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4810     if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4811         goto sigsegv;
4812 
4813     signal = current_exec_domain_sig(sig);
4814 
4815     err |= copy_siginfo_to_user(&rt_sf->info, info);
4816 
4817     err |= __put_user(0, &rt_sf->uc.tuc_flags);
4818     err |= __put_user(0, &rt_sf->uc.tuc_link);
4819     err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4820                       &rt_sf->uc.tuc_stack.ss_sp);
4821     err |= __put_user(sas_ss_flags(env->gpr[1]),
4822                       &rt_sf->uc.tuc_stack.ss_flags);
4823     err |= __put_user(target_sigaltstack_used.ss_size,
4824                       &rt_sf->uc.tuc_stack.ss_size);
4825     err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4826                       &rt_sf->uc.tuc_regs);
4827     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4828         err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4829     }
4830 
4831     frame = &rt_sf->uc.tuc_mcontext;
4832     err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4833 
4834     /* The kernel checks for the presence of a VDSO here.  We don't
4835        emulate a vdso, so use a sigreturn system call.  */
4836     env->lr = (target_ulong) h2g(frame->tramp);
4837 
4838     /* Turn off all fp exceptions.  */
4839     env->fpscr = 0;
4840 
4841     /* Create a stack frame for the caller of the handler.  */
4842     newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4843     err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4844 
4845     if (err)
4846         goto sigsegv;
4847 
4848     /* Set up registers for signal handler.  */
4849     env->gpr[1] = newsp;
4850     env->gpr[3] = (target_ulong) signal;
4851     env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4852     env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4853     env->gpr[6] = (target_ulong) h2g(rt_sf);
4854     env->nip = (target_ulong) ka->_sa_handler;
4855     /* Signal handlers are entered in big-endian mode.  */
4856     env->msr &= ~MSR_LE;
4857 
4858     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4859     return;
4860 
4861 sigsegv:
4862     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4863     qemu_log("segfaulting from setup_rt_frame\n");
4864     force_sig(TARGET_SIGSEGV);
4865 
4866 }
4867 
4868 long do_sigreturn(CPUPPCState *env)
4869 {
4870     struct target_sigcontext *sc = NULL;
4871     struct target_mcontext *sr = NULL;
4872     target_ulong sr_addr = 0, sc_addr;
4873     sigset_t blocked;
4874     target_sigset_t set;
4875 
4876     sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4877     if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4878         goto sigsegv;
4879 
4880 #if defined(TARGET_PPC64)
4881     set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4882 #else
4883     if(__get_user(set.sig[0], &sc->oldmask) ||
4884        __get_user(set.sig[1], &sc->_unused[3]))
4885        goto sigsegv;
4886 #endif
4887     target_to_host_sigset_internal(&blocked, &set);
4888     sigprocmask(SIG_SETMASK, &blocked, NULL);
4889 
4890     if (__get_user(sr_addr, &sc->regs))
4891         goto sigsegv;
4892     if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4893         goto sigsegv;
4894     if (restore_user_regs(env, sr, 1))
4895         goto sigsegv;
4896 
4897     unlock_user_struct(sr, sr_addr, 1);
4898     unlock_user_struct(sc, sc_addr, 1);
4899     return -TARGET_QEMU_ESIGRETURN;
4900 
4901 sigsegv:
4902     unlock_user_struct(sr, sr_addr, 1);
4903     unlock_user_struct(sc, sc_addr, 1);
4904     qemu_log("segfaulting from do_sigreturn\n");
4905     force_sig(TARGET_SIGSEGV);
4906     return 0;
4907 }
4908 
4909 /* See arch/powerpc/kernel/signal_32.c.  */
4910 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4911 {
4912     struct target_mcontext *mcp;
4913     target_ulong mcp_addr;
4914     sigset_t blocked;
4915     target_sigset_t set;
4916 
4917     if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4918                        sizeof (set)))
4919         return 1;
4920 
4921 #if defined(TARGET_PPC64)
4922     fprintf (stderr, "do_setcontext: not implemented\n");
4923     return 0;
4924 #else
4925     if (__get_user(mcp_addr, &ucp->tuc_regs))
4926         return 1;
4927 
4928     if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4929         return 1;
4930 
4931     target_to_host_sigset_internal(&blocked, &set);
4932     sigprocmask(SIG_SETMASK, &blocked, NULL);
4933     if (restore_user_regs(env, mcp, sig))
4934         goto sigsegv;
4935 
4936     unlock_user_struct(mcp, mcp_addr, 1);
4937     return 0;
4938 
4939 sigsegv:
4940     unlock_user_struct(mcp, mcp_addr, 1);
4941     return 1;
4942 #endif
4943 }
4944 
4945 long do_rt_sigreturn(CPUPPCState *env)
4946 {
4947     struct target_rt_sigframe *rt_sf = NULL;
4948     target_ulong rt_sf_addr;
4949 
4950     rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4951     if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4952         goto sigsegv;
4953 
4954     if (do_setcontext(&rt_sf->uc, env, 1))
4955         goto sigsegv;
4956 
4957     do_sigaltstack(rt_sf_addr
4958                    + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4959                    0, env->gpr[1]);
4960 
4961     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4962     return -TARGET_QEMU_ESIGRETURN;
4963 
4964 sigsegv:
4965     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4966     qemu_log("segfaulting from do_rt_sigreturn\n");
4967     force_sig(TARGET_SIGSEGV);
4968     return 0;
4969 }
4970 
4971 #elif defined(TARGET_M68K)
4972 
4973 struct target_sigcontext {
4974     abi_ulong  sc_mask;
4975     abi_ulong  sc_usp;
4976     abi_ulong  sc_d0;
4977     abi_ulong  sc_d1;
4978     abi_ulong  sc_a0;
4979     abi_ulong  sc_a1;
4980     unsigned short sc_sr;
4981     abi_ulong  sc_pc;
4982 };
4983 
4984 struct target_sigframe
4985 {
4986     abi_ulong pretcode;
4987     int sig;
4988     int code;
4989     abi_ulong psc;
4990     char retcode[8];
4991     abi_ulong extramask[TARGET_NSIG_WORDS-1];
4992     struct target_sigcontext sc;
4993 };
4994 
4995 typedef int target_greg_t;
4996 #define TARGET_NGREG 18
4997 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4998 
4999 typedef struct target_fpregset {
5000     int f_fpcntl[3];
5001     int f_fpregs[8*3];
5002 } target_fpregset_t;
5003 
5004 struct target_mcontext {
5005     int version;
5006     target_gregset_t gregs;
5007     target_fpregset_t fpregs;
5008 };
5009 
5010 #define TARGET_MCONTEXT_VERSION 2
5011 
5012 struct target_ucontext {
5013     abi_ulong tuc_flags;
5014     abi_ulong tuc_link;
5015     target_stack_t tuc_stack;
5016     struct target_mcontext tuc_mcontext;
5017     abi_long tuc_filler[80];
5018     target_sigset_t tuc_sigmask;
5019 };
5020 
5021 struct target_rt_sigframe
5022 {
5023     abi_ulong pretcode;
5024     int sig;
5025     abi_ulong pinfo;
5026     abi_ulong puc;
5027     char retcode[8];
5028     struct target_siginfo info;
5029     struct target_ucontext uc;
5030 };
5031 
5032 static int
5033 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
5034                  abi_ulong mask)
5035 {
5036     int err = 0;
5037 
5038     err |= __put_user(mask, &sc->sc_mask);
5039     err |= __put_user(env->aregs[7], &sc->sc_usp);
5040     err |= __put_user(env->dregs[0], &sc->sc_d0);
5041     err |= __put_user(env->dregs[1], &sc->sc_d1);
5042     err |= __put_user(env->aregs[0], &sc->sc_a0);
5043     err |= __put_user(env->aregs[1], &sc->sc_a1);
5044     err |= __put_user(env->sr, &sc->sc_sr);
5045     err |= __put_user(env->pc, &sc->sc_pc);
5046 
5047     return err;
5048 }
5049 
5050 static int
5051 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
5052 {
5053     int err = 0;
5054     int temp;
5055 
5056     err |= __get_user(env->aregs[7], &sc->sc_usp);
5057     err |= __get_user(env->dregs[1], &sc->sc_d1);
5058     err |= __get_user(env->aregs[0], &sc->sc_a0);
5059     err |= __get_user(env->aregs[1], &sc->sc_a1);
5060     err |= __get_user(env->pc, &sc->sc_pc);
5061     err |= __get_user(temp, &sc->sc_sr);
5062     env->sr = (env->sr & 0xff00) | (temp & 0xff);
5063 
5064     *pd0 = tswapl(sc->sc_d0);
5065 
5066     return err;
5067 }
5068 
5069 /*
5070  * Determine which stack to use..
5071  */
5072 static inline abi_ulong
5073 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5074              size_t frame_size)
5075 {
5076     unsigned long sp;
5077 
5078     sp = regs->aregs[7];
5079 
5080     /* This is the X/Open sanctioned signal stack switching.  */
5081     if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5082         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5083     }
5084 
5085     return ((sp - frame_size) & -8UL);
5086 }
5087 
5088 static void setup_frame(int sig, struct target_sigaction *ka,
5089                         target_sigset_t *set, CPUM68KState *env)
5090 {
5091     struct target_sigframe *frame;
5092     abi_ulong frame_addr;
5093     abi_ulong retcode_addr;
5094     abi_ulong sc_addr;
5095     int err = 0;
5096     int i;
5097 
5098     frame_addr = get_sigframe(ka, env, sizeof *frame);
5099     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5100 	goto give_sigsegv;
5101 
5102     err |= __put_user(sig, &frame->sig);
5103 
5104     sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5105     err |= __put_user(sc_addr, &frame->psc);
5106 
5107     err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
5108     if (err)
5109 	goto give_sigsegv;
5110 
5111     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5112         if (__put_user(set->sig[i], &frame->extramask[i - 1]))
5113             goto give_sigsegv;
5114     }
5115 
5116     /* Set up to return from userspace.  */
5117 
5118     retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5119     err |= __put_user(retcode_addr, &frame->pretcode);
5120 
5121     /* moveq #,d0; trap #0 */
5122 
5123     err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5124                       (long *)(frame->retcode));
5125 
5126     if (err)
5127         goto give_sigsegv;
5128 
5129     /* Set up to return from userspace */
5130 
5131     env->aregs[7] = frame_addr;
5132     env->pc = ka->_sa_handler;
5133 
5134     unlock_user_struct(frame, frame_addr, 1);
5135     return;
5136 
5137 give_sigsegv:
5138     unlock_user_struct(frame, frame_addr, 1);
5139     force_sig(TARGET_SIGSEGV);
5140 }
5141 
5142 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5143                                            CPUM68KState *env)
5144 {
5145     target_greg_t *gregs = uc->tuc_mcontext.gregs;
5146     int err;
5147 
5148     err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5149     err |= __put_user(env->dregs[0], &gregs[0]);
5150     err |= __put_user(env->dregs[1], &gregs[1]);
5151     err |= __put_user(env->dregs[2], &gregs[2]);
5152     err |= __put_user(env->dregs[3], &gregs[3]);
5153     err |= __put_user(env->dregs[4], &gregs[4]);
5154     err |= __put_user(env->dregs[5], &gregs[5]);
5155     err |= __put_user(env->dregs[6], &gregs[6]);
5156     err |= __put_user(env->dregs[7], &gregs[7]);
5157     err |= __put_user(env->aregs[0], &gregs[8]);
5158     err |= __put_user(env->aregs[1], &gregs[9]);
5159     err |= __put_user(env->aregs[2], &gregs[10]);
5160     err |= __put_user(env->aregs[3], &gregs[11]);
5161     err |= __put_user(env->aregs[4], &gregs[12]);
5162     err |= __put_user(env->aregs[5], &gregs[13]);
5163     err |= __put_user(env->aregs[6], &gregs[14]);
5164     err |= __put_user(env->aregs[7], &gregs[15]);
5165     err |= __put_user(env->pc, &gregs[16]);
5166     err |= __put_user(env->sr, &gregs[17]);
5167 
5168     return err;
5169 }
5170 
5171 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5172                                              struct target_ucontext *uc,
5173                                              int *pd0)
5174 {
5175     int temp;
5176     int err;
5177     target_greg_t *gregs = uc->tuc_mcontext.gregs;
5178 
5179     err = __get_user(temp, &uc->tuc_mcontext.version);
5180     if (temp != TARGET_MCONTEXT_VERSION)
5181         goto badframe;
5182 
5183     /* restore passed registers */
5184     err |= __get_user(env->dregs[0], &gregs[0]);
5185     err |= __get_user(env->dregs[1], &gregs[1]);
5186     err |= __get_user(env->dregs[2], &gregs[2]);
5187     err |= __get_user(env->dregs[3], &gregs[3]);
5188     err |= __get_user(env->dregs[4], &gregs[4]);
5189     err |= __get_user(env->dregs[5], &gregs[5]);
5190     err |= __get_user(env->dregs[6], &gregs[6]);
5191     err |= __get_user(env->dregs[7], &gregs[7]);
5192     err |= __get_user(env->aregs[0], &gregs[8]);
5193     err |= __get_user(env->aregs[1], &gregs[9]);
5194     err |= __get_user(env->aregs[2], &gregs[10]);
5195     err |= __get_user(env->aregs[3], &gregs[11]);
5196     err |= __get_user(env->aregs[4], &gregs[12]);
5197     err |= __get_user(env->aregs[5], &gregs[13]);
5198     err |= __get_user(env->aregs[6], &gregs[14]);
5199     err |= __get_user(env->aregs[7], &gregs[15]);
5200     err |= __get_user(env->pc, &gregs[16]);
5201     err |= __get_user(temp, &gregs[17]);
5202     env->sr = (env->sr & 0xff00) | (temp & 0xff);
5203 
5204     *pd0 = env->dregs[0];
5205     return err;
5206 
5207 badframe:
5208     return 1;
5209 }
5210 
5211 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5212                            target_siginfo_t *info,
5213                            target_sigset_t *set, CPUM68KState *env)
5214 {
5215     struct target_rt_sigframe *frame;
5216     abi_ulong frame_addr;
5217     abi_ulong retcode_addr;
5218     abi_ulong info_addr;
5219     abi_ulong uc_addr;
5220     int err = 0;
5221     int i;
5222 
5223     frame_addr = get_sigframe(ka, env, sizeof *frame);
5224     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5225 	goto give_sigsegv;
5226 
5227     err |= __put_user(sig, &frame->sig);
5228 
5229     info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5230     err |= __put_user(info_addr, &frame->pinfo);
5231 
5232     uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5233     err |= __put_user(uc_addr, &frame->puc);
5234 
5235     err |= copy_siginfo_to_user(&frame->info, info);
5236 
5237     /* Create the ucontext */
5238 
5239     err |= __put_user(0, &frame->uc.tuc_flags);
5240     err |= __put_user(0, &frame->uc.tuc_link);
5241     err |= __put_user(target_sigaltstack_used.ss_sp,
5242                       &frame->uc.tuc_stack.ss_sp);
5243     err |= __put_user(sas_ss_flags(env->aregs[7]),
5244                       &frame->uc.tuc_stack.ss_flags);
5245     err |= __put_user(target_sigaltstack_used.ss_size,
5246                       &frame->uc.tuc_stack.ss_size);
5247     err |= target_rt_setup_ucontext(&frame->uc, env);
5248 
5249     if (err)
5250             goto give_sigsegv;
5251 
5252     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5253         if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
5254             goto give_sigsegv;
5255     }
5256 
5257     /* Set up to return from userspace.  */
5258 
5259     retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5260     err |= __put_user(retcode_addr, &frame->pretcode);
5261 
5262     /* moveq #,d0; notb d0; trap #0 */
5263 
5264     err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5265                       (long *)(frame->retcode + 0));
5266     err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5267 
5268     if (err)
5269         goto give_sigsegv;
5270 
5271     /* Set up to return from userspace */
5272 
5273     env->aregs[7] = frame_addr;
5274     env->pc = ka->_sa_handler;
5275 
5276     unlock_user_struct(frame, frame_addr, 1);
5277     return;
5278 
5279 give_sigsegv:
5280     unlock_user_struct(frame, frame_addr, 1);
5281     force_sig(TARGET_SIGSEGV);
5282 }
5283 
5284 long do_sigreturn(CPUM68KState *env)
5285 {
5286     struct target_sigframe *frame;
5287     abi_ulong frame_addr = env->aregs[7] - 4;
5288     target_sigset_t target_set;
5289     sigset_t set;
5290     int d0, i;
5291 
5292     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5293         goto badframe;
5294 
5295     /* set blocked signals */
5296 
5297     if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5298         goto badframe;
5299 
5300     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5301         if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5302             goto badframe;
5303     }
5304 
5305     target_to_host_sigset_internal(&set, &target_set);
5306     sigprocmask(SIG_SETMASK, &set, NULL);
5307 
5308     /* restore registers */
5309 
5310     if (restore_sigcontext(env, &frame->sc, &d0))
5311         goto badframe;
5312 
5313     unlock_user_struct(frame, frame_addr, 0);
5314     return d0;
5315 
5316 badframe:
5317     unlock_user_struct(frame, frame_addr, 0);
5318     force_sig(TARGET_SIGSEGV);
5319     return 0;
5320 }
5321 
5322 long do_rt_sigreturn(CPUM68KState *env)
5323 {
5324     struct target_rt_sigframe *frame;
5325     abi_ulong frame_addr = env->aregs[7] - 4;
5326     target_sigset_t target_set;
5327     sigset_t set;
5328     int d0;
5329 
5330     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5331         goto badframe;
5332 
5333     target_to_host_sigset_internal(&set, &target_set);
5334     sigprocmask(SIG_SETMASK, &set, NULL);
5335 
5336     /* restore registers */
5337 
5338     if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5339         goto badframe;
5340 
5341     if (do_sigaltstack(frame_addr +
5342                        offsetof(struct target_rt_sigframe, uc.tuc_stack),
5343                        0, get_sp_from_cpustate(env)) == -EFAULT)
5344         goto badframe;
5345 
5346     unlock_user_struct(frame, frame_addr, 0);
5347     return d0;
5348 
5349 badframe:
5350     unlock_user_struct(frame, frame_addr, 0);
5351     force_sig(TARGET_SIGSEGV);
5352     return 0;
5353 }
5354 
5355 #elif defined(TARGET_ALPHA)
5356 
5357 struct target_sigcontext {
5358     abi_long sc_onstack;
5359     abi_long sc_mask;
5360     abi_long sc_pc;
5361     abi_long sc_ps;
5362     abi_long sc_regs[32];
5363     abi_long sc_ownedfp;
5364     abi_long sc_fpregs[32];
5365     abi_ulong sc_fpcr;
5366     abi_ulong sc_fp_control;
5367     abi_ulong sc_reserved1;
5368     abi_ulong sc_reserved2;
5369     abi_ulong sc_ssize;
5370     abi_ulong sc_sbase;
5371     abi_ulong sc_traparg_a0;
5372     abi_ulong sc_traparg_a1;
5373     abi_ulong sc_traparg_a2;
5374     abi_ulong sc_fp_trap_pc;
5375     abi_ulong sc_fp_trigger_sum;
5376     abi_ulong sc_fp_trigger_inst;
5377 };
5378 
5379 struct target_ucontext {
5380     abi_ulong tuc_flags;
5381     abi_ulong tuc_link;
5382     abi_ulong tuc_osf_sigmask;
5383     target_stack_t tuc_stack;
5384     struct target_sigcontext tuc_mcontext;
5385     target_sigset_t tuc_sigmask;
5386 };
5387 
5388 struct target_sigframe {
5389     struct target_sigcontext sc;
5390     unsigned int retcode[3];
5391 };
5392 
5393 struct target_rt_sigframe {
5394     target_siginfo_t info;
5395     struct target_ucontext uc;
5396     unsigned int retcode[3];
5397 };
5398 
5399 #define INSN_MOV_R30_R16        0x47fe0410
5400 #define INSN_LDI_R0             0x201f0000
5401 #define INSN_CALLSYS            0x00000083
5402 
5403 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5404                             abi_ulong frame_addr, target_sigset_t *set)
5405 {
5406     int i, err = 0;
5407 
5408     err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5409     err |= __put_user(set->sig[0], &sc->sc_mask);
5410     err |= __put_user(env->pc, &sc->sc_pc);
5411     err |= __put_user(8, &sc->sc_ps);
5412 
5413     for (i = 0; i < 31; ++i) {
5414         err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5415     }
5416     err |= __put_user(0, &sc->sc_regs[31]);
5417 
5418     for (i = 0; i < 31; ++i) {
5419         err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5420     }
5421     err |= __put_user(0, &sc->sc_fpregs[31]);
5422     err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5423 
5424     err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5425     err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5426     err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5427 
5428     return err;
5429 }
5430 
5431 static int restore_sigcontext(CPUAlphaState *env,
5432                               struct target_sigcontext *sc)
5433 {
5434     uint64_t fpcr;
5435     int i, err = 0;
5436 
5437     err |= __get_user(env->pc, &sc->sc_pc);
5438 
5439     for (i = 0; i < 31; ++i) {
5440         err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5441     }
5442     for (i = 0; i < 31; ++i) {
5443         err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5444     }
5445 
5446     err |= __get_user(fpcr, &sc->sc_fpcr);
5447     cpu_alpha_store_fpcr(env, fpcr);
5448 
5449     return err;
5450 }
5451 
5452 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5453                                      CPUAlphaState *env,
5454                                      unsigned long framesize)
5455 {
5456     abi_ulong sp = env->ir[IR_SP];
5457 
5458     /* This is the X/Open sanctioned signal stack switching.  */
5459     if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5460         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5461     }
5462     return (sp - framesize) & -32;
5463 }
5464 
5465 static void setup_frame(int sig, struct target_sigaction *ka,
5466                         target_sigset_t *set, CPUAlphaState *env)
5467 {
5468     abi_ulong frame_addr, r26;
5469     struct target_sigframe *frame;
5470     int err = 0;
5471 
5472     frame_addr = get_sigframe(ka, env, sizeof(*frame));
5473     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5474         goto give_sigsegv;
5475     }
5476 
5477     err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5478 
5479     if (ka->sa_restorer) {
5480         r26 = ka->sa_restorer;
5481     } else {
5482         err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5483         err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5484                           &frame->retcode[1]);
5485         err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5486         /* imb() */
5487         r26 = frame_addr;
5488     }
5489 
5490     unlock_user_struct(frame, frame_addr, 1);
5491 
5492     if (err) {
5493     give_sigsegv:
5494         if (sig == TARGET_SIGSEGV) {
5495             ka->_sa_handler = TARGET_SIG_DFL;
5496         }
5497         force_sig(TARGET_SIGSEGV);
5498     }
5499 
5500     env->ir[IR_RA] = r26;
5501     env->ir[IR_PV] = env->pc = ka->_sa_handler;
5502     env->ir[IR_A0] = sig;
5503     env->ir[IR_A1] = 0;
5504     env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5505     env->ir[IR_SP] = frame_addr;
5506 }
5507 
5508 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5509                            target_siginfo_t *info,
5510                            target_sigset_t *set, CPUAlphaState *env)
5511 {
5512     abi_ulong frame_addr, r26;
5513     struct target_rt_sigframe *frame;
5514     int i, err = 0;
5515 
5516     frame_addr = get_sigframe(ka, env, sizeof(*frame));
5517     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5518         goto give_sigsegv;
5519     }
5520 
5521     err |= copy_siginfo_to_user(&frame->info, info);
5522 
5523     err |= __put_user(0, &frame->uc.tuc_flags);
5524     err |= __put_user(0, &frame->uc.tuc_link);
5525     err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5526     err |= __put_user(target_sigaltstack_used.ss_sp,
5527                       &frame->uc.tuc_stack.ss_sp);
5528     err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5529                       &frame->uc.tuc_stack.ss_flags);
5530     err |= __put_user(target_sigaltstack_used.ss_size,
5531                       &frame->uc.tuc_stack.ss_size);
5532     err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5533     for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5534         err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5535     }
5536 
5537     if (ka->sa_restorer) {
5538         r26 = ka->sa_restorer;
5539     } else {
5540         err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5541         err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5542                           &frame->retcode[1]);
5543         err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5544         /* imb(); */
5545         r26 = frame_addr;
5546     }
5547 
5548     if (err) {
5549     give_sigsegv:
5550        if (sig == TARGET_SIGSEGV) {
5551             ka->_sa_handler = TARGET_SIG_DFL;
5552         }
5553         force_sig(TARGET_SIGSEGV);
5554     }
5555 
5556     env->ir[IR_RA] = r26;
5557     env->ir[IR_PV] = env->pc = ka->_sa_handler;
5558     env->ir[IR_A0] = sig;
5559     env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5560     env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5561     env->ir[IR_SP] = frame_addr;
5562 }
5563 
5564 long do_sigreturn(CPUAlphaState *env)
5565 {
5566     struct target_sigcontext *sc;
5567     abi_ulong sc_addr = env->ir[IR_A0];
5568     target_sigset_t target_set;
5569     sigset_t set;
5570 
5571     if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5572         goto badframe;
5573     }
5574 
5575     target_sigemptyset(&target_set);
5576     if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5577         goto badframe;
5578     }
5579 
5580     target_to_host_sigset_internal(&set, &target_set);
5581     sigprocmask(SIG_SETMASK, &set, NULL);
5582 
5583     if (restore_sigcontext(env, sc)) {
5584         goto badframe;
5585     }
5586     unlock_user_struct(sc, sc_addr, 0);
5587     return env->ir[IR_V0];
5588 
5589  badframe:
5590     unlock_user_struct(sc, sc_addr, 0);
5591     force_sig(TARGET_SIGSEGV);
5592 }
5593 
5594 long do_rt_sigreturn(CPUAlphaState *env)
5595 {
5596     abi_ulong frame_addr = env->ir[IR_A0];
5597     struct target_rt_sigframe *frame;
5598     sigset_t set;
5599 
5600     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5601         goto badframe;
5602     }
5603     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5604     sigprocmask(SIG_SETMASK, &set, NULL);
5605 
5606     if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5607         goto badframe;
5608     }
5609     if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5610                                              uc.tuc_stack),
5611                        0, env->ir[IR_SP]) == -EFAULT) {
5612         goto badframe;
5613     }
5614 
5615     unlock_user_struct(frame, frame_addr, 0);
5616     return env->ir[IR_V0];
5617 
5618 
5619  badframe:
5620     unlock_user_struct(frame, frame_addr, 0);
5621     force_sig(TARGET_SIGSEGV);
5622 }
5623 
5624 #else
5625 
5626 static void setup_frame(int sig, struct target_sigaction *ka,
5627 			target_sigset_t *set, CPUArchState *env)
5628 {
5629     fprintf(stderr, "setup_frame: not implemented\n");
5630 }
5631 
5632 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5633                            target_siginfo_t *info,
5634 			   target_sigset_t *set, CPUArchState *env)
5635 {
5636     fprintf(stderr, "setup_rt_frame: not implemented\n");
5637 }
5638 
5639 long do_sigreturn(CPUArchState *env)
5640 {
5641     fprintf(stderr, "do_sigreturn: not implemented\n");
5642     return -TARGET_ENOSYS;
5643 }
5644 
5645 long do_rt_sigreturn(CPUArchState *env)
5646 {
5647     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5648     return -TARGET_ENOSYS;
5649 }
5650 
5651 #endif
5652 
5653 void process_pending_signals(CPUArchState *cpu_env)
5654 {
5655     CPUState *cpu = ENV_GET_CPU(cpu_env);
5656     int sig;
5657     abi_ulong handler;
5658     sigset_t set, old_set;
5659     target_sigset_t target_old_set;
5660     struct emulated_sigtable *k;
5661     struct target_sigaction *sa;
5662     struct sigqueue *q;
5663     TaskState *ts = cpu_env->opaque;
5664 
5665     if (!ts->signal_pending)
5666         return;
5667 
5668     /* FIXME: This is not threadsafe.  */
5669     k = ts->sigtab;
5670     for(sig = 1; sig <= TARGET_NSIG; sig++) {
5671         if (k->pending)
5672             goto handle_signal;
5673         k++;
5674     }
5675     /* if no signal is pending, just return */
5676     ts->signal_pending = 0;
5677     return;
5678 
5679  handle_signal:
5680 #ifdef DEBUG_SIGNAL
5681     fprintf(stderr, "qemu: process signal %d\n", sig);
5682 #endif
5683     /* dequeue signal */
5684     q = k->first;
5685     k->first = q->next;
5686     if (!k->first)
5687         k->pending = 0;
5688 
5689     sig = gdb_handlesig(cpu, sig);
5690     if (!sig) {
5691         sa = NULL;
5692         handler = TARGET_SIG_IGN;
5693     } else {
5694         sa = &sigact_table[sig - 1];
5695         handler = sa->_sa_handler;
5696     }
5697 
5698     if (handler == TARGET_SIG_DFL) {
5699         /* default handler : ignore some signal. The other are job control or fatal */
5700         if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5701             kill(getpid(),SIGSTOP);
5702         } else if (sig != TARGET_SIGCHLD &&
5703                    sig != TARGET_SIGURG &&
5704                    sig != TARGET_SIGWINCH &&
5705                    sig != TARGET_SIGCONT) {
5706             force_sig(sig);
5707         }
5708     } else if (handler == TARGET_SIG_IGN) {
5709         /* ignore sig */
5710     } else if (handler == TARGET_SIG_ERR) {
5711         force_sig(sig);
5712     } else {
5713         /* compute the blocked signals during the handler execution */
5714         target_to_host_sigset(&set, &sa->sa_mask);
5715         /* SA_NODEFER indicates that the current signal should not be
5716            blocked during the handler */
5717         if (!(sa->sa_flags & TARGET_SA_NODEFER))
5718             sigaddset(&set, target_to_host_signal(sig));
5719 
5720         /* block signals in the handler using Linux */
5721         sigprocmask(SIG_BLOCK, &set, &old_set);
5722         /* save the previous blocked signal state to restore it at the
5723            end of the signal execution (see do_sigreturn) */
5724         host_to_target_sigset_internal(&target_old_set, &old_set);
5725 
5726         /* if the CPU is in VM86 mode, we restore the 32 bit values */
5727 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5728         {
5729             CPUX86State *env = cpu_env;
5730             if (env->eflags & VM_MASK)
5731                 save_v86_state(env);
5732         }
5733 #endif
5734         /* prepare the stack frame of the virtual CPU */
5735 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5736         /* These targets do not have traditional signals.  */
5737         setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5738 #else
5739         if (sa->sa_flags & TARGET_SA_SIGINFO)
5740             setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5741         else
5742             setup_frame(sig, sa, &target_old_set, cpu_env);
5743 #endif
5744 	if (sa->sa_flags & TARGET_SA_RESETHAND)
5745             sa->_sa_handler = TARGET_SIG_DFL;
5746     }
5747     if (q != &k->info)
5748         free_sigqueue(cpu_env, q);
5749 }
5750