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