xref: /openbmc/qemu/linux-user/signal.c (revision 1fd6bb44)
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(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(regs->active_tc.PC, &sc->sc_pc);
2532 
2533     __put_user(0, &sc->sc_regs[0]);
2534     for (i = 1; i < 32; ++i) {
2535         err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2536     }
2537 
2538     err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2539     err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2540 
2541     /* Rather than checking for dsp existence, always copy.  The storage
2542        would just be garbage otherwise.  */
2543     err |= __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2544     err |= __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2545     err |= __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2546     err |= __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2547     err |= __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2548     err |= __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2549     {
2550         uint32_t dsp = cpu_rddsp(0x3ff, regs);
2551         err |= __put_user(dsp, &sc->sc_dsp);
2552     }
2553 
2554     err |= __put_user(1, &sc->sc_used_math);
2555 
2556     for (i = 0; i < 32; ++i) {
2557         err |= __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2558     }
2559 
2560     return err;
2561 }
2562 
2563 static inline int
2564 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2565 {
2566     int err = 0;
2567     int i;
2568 
2569     err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2570 
2571     err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2572     err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2573 
2574     for (i = 1; i < 32; ++i) {
2575         err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2576     }
2577 
2578     err |= __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2579     err |= __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2580     err |= __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2581     err |= __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2582     err |= __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2583     err |= __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2584     {
2585         uint32_t dsp;
2586         err |= __get_user(dsp, &sc->sc_dsp);
2587         cpu_wrdsp(dsp, 0x3ff, regs);
2588     }
2589 
2590     for (i = 0; i < 32; ++i) {
2591         err |= __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2592     }
2593 
2594     return err;
2595 }
2596 
2597 /*
2598  * Determine which stack to use..
2599  */
2600 static inline abi_ulong
2601 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2602 {
2603     unsigned long sp;
2604 
2605     /* Default to using normal stack */
2606     sp = regs->active_tc.gpr[29];
2607 
2608     /*
2609      * FPU emulator may have its own trampoline active just
2610      * above the user stack, 16-bytes before the next lowest
2611      * 16 byte boundary.  Try to avoid trashing it.
2612      */
2613     sp -= 32;
2614 
2615     /* This is the X/Open sanctioned signal stack switching.  */
2616     if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2617         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2618     }
2619 
2620     return (sp - frame_size) & ~7;
2621 }
2622 
2623 # if defined(TARGET_ABI_MIPSO32)
2624 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2625 static void setup_frame(int sig, struct target_sigaction * ka,
2626                         target_sigset_t *set, CPUMIPSState *regs)
2627 {
2628     struct sigframe *frame;
2629     abi_ulong frame_addr;
2630     int i;
2631 
2632     frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2633     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2634 	goto give_sigsegv;
2635 
2636     install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2637 
2638     if(setup_sigcontext(regs, &frame->sf_sc))
2639 	goto give_sigsegv;
2640 
2641     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2642 	if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2643 	    goto give_sigsegv;
2644     }
2645 
2646     /*
2647     * Arguments to signal handler:
2648     *
2649     *   a0 = signal number
2650     *   a1 = 0 (should be cause)
2651     *   a2 = pointer to struct sigcontext
2652     *
2653     * $25 and PC point to the signal handler, $29 points to the
2654     * struct sigframe.
2655     */
2656     regs->active_tc.gpr[ 4] = sig;
2657     regs->active_tc.gpr[ 5] = 0;
2658     regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2659     regs->active_tc.gpr[29] = frame_addr;
2660     regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2661     /* The original kernel code sets CP0_EPC to the handler
2662     * since it returns to userland using eret
2663     * we cannot do this here, and we must set PC directly */
2664     regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2665     unlock_user_struct(frame, frame_addr, 1);
2666     return;
2667 
2668 give_sigsegv:
2669     unlock_user_struct(frame, frame_addr, 1);
2670     force_sig(TARGET_SIGSEGV/*, current*/);
2671 }
2672 
2673 long do_sigreturn(CPUMIPSState *regs)
2674 {
2675     struct sigframe *frame;
2676     abi_ulong frame_addr;
2677     sigset_t blocked;
2678     target_sigset_t target_set;
2679     int i;
2680 
2681 #if defined(DEBUG_SIGNAL)
2682     fprintf(stderr, "do_sigreturn\n");
2683 #endif
2684     frame_addr = regs->active_tc.gpr[29];
2685     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2686    	goto badframe;
2687 
2688     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2689    	if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2690 	    goto badframe;
2691     }
2692 
2693     target_to_host_sigset_internal(&blocked, &target_set);
2694     sigprocmask(SIG_SETMASK, &blocked, NULL);
2695 
2696     if (restore_sigcontext(regs, &frame->sf_sc))
2697    	goto badframe;
2698 
2699 #if 0
2700     /*
2701      * Don't let your children do this ...
2702      */
2703     __asm__ __volatile__(
2704    	"move\t$29, %0\n\t"
2705    	"j\tsyscall_exit"
2706    	:/* no outputs */
2707    	:"r" (&regs));
2708     /* Unreached */
2709 #endif
2710 
2711     regs->active_tc.PC = regs->CP0_EPC;
2712     /* I am not sure this is right, but it seems to work
2713     * maybe a problem with nested signals ? */
2714     regs->CP0_EPC = 0;
2715     return -TARGET_QEMU_ESIGRETURN;
2716 
2717 badframe:
2718     force_sig(TARGET_SIGSEGV/*, current*/);
2719     return 0;
2720 }
2721 # endif /* O32 */
2722 
2723 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2724                            target_siginfo_t *info,
2725                            target_sigset_t *set, CPUMIPSState *env)
2726 {
2727     struct target_rt_sigframe *frame;
2728     abi_ulong frame_addr;
2729     int i;
2730 
2731     frame_addr = get_sigframe(ka, env, sizeof(*frame));
2732     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2733 	goto give_sigsegv;
2734 
2735     install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2736 
2737     copy_siginfo_to_user(&frame->rs_info, info);
2738 
2739     __put_user(0, &frame->rs_uc.tuc_flags);
2740     __put_user(0, &frame->rs_uc.tuc_link);
2741     __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2742     __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2743     __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2744                &frame->rs_uc.tuc_stack.ss_flags);
2745 
2746     setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2747 
2748     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2749         __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2750     }
2751 
2752     /*
2753     * Arguments to signal handler:
2754     *
2755     *   a0 = signal number
2756     *   a1 = pointer to siginfo_t
2757     *   a2 = pointer to struct ucontext
2758     *
2759     * $25 and PC point to the signal handler, $29 points to the
2760     * struct sigframe.
2761     */
2762     env->active_tc.gpr[ 4] = sig;
2763     env->active_tc.gpr[ 5] = frame_addr
2764                              + offsetof(struct target_rt_sigframe, rs_info);
2765     env->active_tc.gpr[ 6] = frame_addr
2766                              + offsetof(struct target_rt_sigframe, rs_uc);
2767     env->active_tc.gpr[29] = frame_addr;
2768     env->active_tc.gpr[31] = frame_addr
2769                              + offsetof(struct target_rt_sigframe, rs_code);
2770     /* The original kernel code sets CP0_EPC to the handler
2771     * since it returns to userland using eret
2772     * we cannot do this here, and we must set PC directly */
2773     env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2774     unlock_user_struct(frame, frame_addr, 1);
2775     return;
2776 
2777 give_sigsegv:
2778     unlock_user_struct(frame, frame_addr, 1);
2779     force_sig(TARGET_SIGSEGV/*, current*/);
2780 }
2781 
2782 long do_rt_sigreturn(CPUMIPSState *env)
2783 {
2784     struct target_rt_sigframe *frame;
2785     abi_ulong frame_addr;
2786     sigset_t blocked;
2787 
2788 #if defined(DEBUG_SIGNAL)
2789     fprintf(stderr, "do_rt_sigreturn\n");
2790 #endif
2791     frame_addr = env->active_tc.gpr[29];
2792     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2793    	goto badframe;
2794 
2795     target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2796     sigprocmask(SIG_SETMASK, &blocked, NULL);
2797 
2798     if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2799         goto badframe;
2800 
2801     if (do_sigaltstack(frame_addr +
2802 		       offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2803 		       0, get_sp_from_cpustate(env)) == -EFAULT)
2804         goto badframe;
2805 
2806     env->active_tc.PC = env->CP0_EPC;
2807     /* I am not sure this is right, but it seems to work
2808     * maybe a problem with nested signals ? */
2809     env->CP0_EPC = 0;
2810     return -TARGET_QEMU_ESIGRETURN;
2811 
2812 badframe:
2813     force_sig(TARGET_SIGSEGV/*, current*/);
2814     return 0;
2815 }
2816 
2817 #elif defined(TARGET_SH4)
2818 
2819 /*
2820  * code and data structures from linux kernel:
2821  * include/asm-sh/sigcontext.h
2822  * arch/sh/kernel/signal.c
2823  */
2824 
2825 struct target_sigcontext {
2826     target_ulong  oldmask;
2827 
2828     /* CPU registers */
2829     target_ulong  sc_gregs[16];
2830     target_ulong  sc_pc;
2831     target_ulong  sc_pr;
2832     target_ulong  sc_sr;
2833     target_ulong  sc_gbr;
2834     target_ulong  sc_mach;
2835     target_ulong  sc_macl;
2836 
2837     /* FPU registers */
2838     target_ulong  sc_fpregs[16];
2839     target_ulong  sc_xfpregs[16];
2840     unsigned int sc_fpscr;
2841     unsigned int sc_fpul;
2842     unsigned int sc_ownedfp;
2843 };
2844 
2845 struct target_sigframe
2846 {
2847     struct target_sigcontext sc;
2848     target_ulong extramask[TARGET_NSIG_WORDS-1];
2849     uint16_t retcode[3];
2850 };
2851 
2852 
2853 struct target_ucontext {
2854     target_ulong tuc_flags;
2855     struct target_ucontext *tuc_link;
2856     target_stack_t tuc_stack;
2857     struct target_sigcontext tuc_mcontext;
2858     target_sigset_t tuc_sigmask;	/* mask last for extensibility */
2859 };
2860 
2861 struct target_rt_sigframe
2862 {
2863     struct target_siginfo info;
2864     struct target_ucontext uc;
2865     uint16_t retcode[3];
2866 };
2867 
2868 
2869 #define MOVW(n)  (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2870 #define TRAP_NOARG 0xc310         /* Syscall w/no args (NR in R3) SH3/4 */
2871 
2872 static abi_ulong get_sigframe(struct target_sigaction *ka,
2873                          unsigned long sp, size_t frame_size)
2874 {
2875     if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2876         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2877     }
2878 
2879     return (sp - frame_size) & -8ul;
2880 }
2881 
2882 static int setup_sigcontext(struct target_sigcontext *sc,
2883                             CPUSH4State *regs, unsigned long mask)
2884 {
2885     int err = 0;
2886     int i;
2887 
2888 #define COPY(x)         err |= __put_user(regs->x, &sc->sc_##x)
2889     COPY(gregs[0]); COPY(gregs[1]);
2890     COPY(gregs[2]); COPY(gregs[3]);
2891     COPY(gregs[4]); COPY(gregs[5]);
2892     COPY(gregs[6]); COPY(gregs[7]);
2893     COPY(gregs[8]); COPY(gregs[9]);
2894     COPY(gregs[10]); COPY(gregs[11]);
2895     COPY(gregs[12]); COPY(gregs[13]);
2896     COPY(gregs[14]); COPY(gregs[15]);
2897     COPY(gbr); COPY(mach);
2898     COPY(macl); COPY(pr);
2899     COPY(sr); COPY(pc);
2900 #undef COPY
2901 
2902     for (i=0; i<16; i++) {
2903         err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2904     }
2905     err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2906     err |= __put_user(regs->fpul, &sc->sc_fpul);
2907 
2908     /* non-iBCS2 extensions.. */
2909     err |= __put_user(mask, &sc->oldmask);
2910 
2911     return err;
2912 }
2913 
2914 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
2915                               target_ulong *r0_p)
2916 {
2917     unsigned int err = 0;
2918     int i;
2919 
2920 #define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
2921     COPY(gregs[1]);
2922     COPY(gregs[2]); COPY(gregs[3]);
2923     COPY(gregs[4]); COPY(gregs[5]);
2924     COPY(gregs[6]); COPY(gregs[7]);
2925     COPY(gregs[8]); COPY(gregs[9]);
2926     COPY(gregs[10]); COPY(gregs[11]);
2927     COPY(gregs[12]); COPY(gregs[13]);
2928     COPY(gregs[14]); COPY(gregs[15]);
2929     COPY(gbr); COPY(mach);
2930     COPY(macl); COPY(pr);
2931     COPY(sr); COPY(pc);
2932 #undef COPY
2933 
2934     for (i=0; i<16; i++) {
2935         err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
2936     }
2937     err |= __get_user(regs->fpscr, &sc->sc_fpscr);
2938     err |= __get_user(regs->fpul, &sc->sc_fpul);
2939 
2940     regs->tra = -1;         /* disable syscall checks */
2941     err |= __get_user(*r0_p, &sc->sc_gregs[0]);
2942     return err;
2943 }
2944 
2945 static void setup_frame(int sig, struct target_sigaction *ka,
2946                         target_sigset_t *set, CPUSH4State *regs)
2947 {
2948     struct target_sigframe *frame;
2949     abi_ulong frame_addr;
2950     int i;
2951     int err = 0;
2952     int signal;
2953 
2954     frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2955     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2956 	goto give_sigsegv;
2957 
2958     signal = current_exec_domain_sig(sig);
2959 
2960     err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2961 
2962     for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2963         err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2964     }
2965 
2966     /* Set up to return from userspace.  If provided, use a stub
2967        already in userspace.  */
2968     if (ka->sa_flags & TARGET_SA_RESTORER) {
2969         regs->pr = (unsigned long) ka->sa_restorer;
2970     } else {
2971         /* Generate return code (system call to sigreturn) */
2972         err |= __put_user(MOVW(2), &frame->retcode[0]);
2973         err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2974         err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2975         regs->pr = (unsigned long) frame->retcode;
2976     }
2977 
2978     if (err)
2979         goto give_sigsegv;
2980 
2981     /* Set up registers for signal handler */
2982     regs->gregs[15] = frame_addr;
2983     regs->gregs[4] = signal; /* Arg for signal handler */
2984     regs->gregs[5] = 0;
2985     regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
2986     regs->pc = (unsigned long) ka->_sa_handler;
2987 
2988     unlock_user_struct(frame, frame_addr, 1);
2989     return;
2990 
2991 give_sigsegv:
2992     unlock_user_struct(frame, frame_addr, 1);
2993     force_sig(TARGET_SIGSEGV);
2994 }
2995 
2996 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2997                            target_siginfo_t *info,
2998                            target_sigset_t *set, CPUSH4State *regs)
2999 {
3000     struct target_rt_sigframe *frame;
3001     abi_ulong frame_addr;
3002     int i;
3003     int err = 0;
3004     int signal;
3005 
3006     frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3007     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3008 	goto give_sigsegv;
3009 
3010     signal = current_exec_domain_sig(sig);
3011 
3012     err |= copy_siginfo_to_user(&frame->info, info);
3013 
3014     /* Create the ucontext.  */
3015     err |= __put_user(0, &frame->uc.tuc_flags);
3016     err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3017     err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3018 		      &frame->uc.tuc_stack.ss_sp);
3019     err |= __put_user(sas_ss_flags(regs->gregs[15]),
3020 		      &frame->uc.tuc_stack.ss_flags);
3021     err |= __put_user(target_sigaltstack_used.ss_size,
3022 		      &frame->uc.tuc_stack.ss_size);
3023     err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3024 			    regs, set->sig[0]);
3025     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3026         err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3027     }
3028 
3029     /* Set up to return from userspace.  If provided, use a stub
3030        already in userspace.  */
3031     if (ka->sa_flags & TARGET_SA_RESTORER) {
3032         regs->pr = (unsigned long) ka->sa_restorer;
3033     } else {
3034         /* Generate return code (system call to sigreturn) */
3035         err |= __put_user(MOVW(2), &frame->retcode[0]);
3036         err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3037         err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3038         regs->pr = (unsigned long) frame->retcode;
3039     }
3040 
3041     if (err)
3042         goto give_sigsegv;
3043 
3044     /* Set up registers for signal handler */
3045     regs->gregs[15] = frame_addr;
3046     regs->gregs[4] = signal; /* Arg for signal handler */
3047     regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3048     regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3049     regs->pc = (unsigned long) ka->_sa_handler;
3050 
3051     unlock_user_struct(frame, frame_addr, 1);
3052     return;
3053 
3054 give_sigsegv:
3055     unlock_user_struct(frame, frame_addr, 1);
3056     force_sig(TARGET_SIGSEGV);
3057 }
3058 
3059 long do_sigreturn(CPUSH4State *regs)
3060 {
3061     struct target_sigframe *frame;
3062     abi_ulong frame_addr;
3063     sigset_t blocked;
3064     target_sigset_t target_set;
3065     target_ulong r0;
3066     int i;
3067     int err = 0;
3068 
3069 #if defined(DEBUG_SIGNAL)
3070     fprintf(stderr, "do_sigreturn\n");
3071 #endif
3072     frame_addr = regs->gregs[15];
3073     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3074    	goto badframe;
3075 
3076     err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3077     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3078         err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3079     }
3080 
3081     if (err)
3082         goto badframe;
3083 
3084     target_to_host_sigset_internal(&blocked, &target_set);
3085     sigprocmask(SIG_SETMASK, &blocked, NULL);
3086 
3087     if (restore_sigcontext(regs, &frame->sc, &r0))
3088         goto badframe;
3089 
3090     unlock_user_struct(frame, frame_addr, 0);
3091     return r0;
3092 
3093 badframe:
3094     unlock_user_struct(frame, frame_addr, 0);
3095     force_sig(TARGET_SIGSEGV);
3096     return 0;
3097 }
3098 
3099 long do_rt_sigreturn(CPUSH4State *regs)
3100 {
3101     struct target_rt_sigframe *frame;
3102     abi_ulong frame_addr;
3103     sigset_t blocked;
3104     target_ulong r0;
3105 
3106 #if defined(DEBUG_SIGNAL)
3107     fprintf(stderr, "do_rt_sigreturn\n");
3108 #endif
3109     frame_addr = regs->gregs[15];
3110     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3111    	goto badframe;
3112 
3113     target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3114     sigprocmask(SIG_SETMASK, &blocked, NULL);
3115 
3116     if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3117         goto badframe;
3118 
3119     if (do_sigaltstack(frame_addr +
3120 		       offsetof(struct target_rt_sigframe, uc.tuc_stack),
3121 		       0, get_sp_from_cpustate(regs)) == -EFAULT)
3122         goto badframe;
3123 
3124     unlock_user_struct(frame, frame_addr, 0);
3125     return r0;
3126 
3127 badframe:
3128     unlock_user_struct(frame, frame_addr, 0);
3129     force_sig(TARGET_SIGSEGV);
3130     return 0;
3131 }
3132 #elif defined(TARGET_MICROBLAZE)
3133 
3134 struct target_sigcontext {
3135     struct target_pt_regs regs;  /* needs to be first */
3136     uint32_t oldmask;
3137 };
3138 
3139 struct target_stack_t {
3140     abi_ulong ss_sp;
3141     int ss_flags;
3142     unsigned int ss_size;
3143 };
3144 
3145 struct target_ucontext {
3146     abi_ulong tuc_flags;
3147     abi_ulong tuc_link;
3148     struct target_stack_t tuc_stack;
3149     struct target_sigcontext tuc_mcontext;
3150     uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3151 };
3152 
3153 /* Signal frames. */
3154 struct target_signal_frame {
3155     struct target_ucontext uc;
3156     uint32_t extramask[TARGET_NSIG_WORDS - 1];
3157     uint32_t tramp[2];
3158 };
3159 
3160 struct rt_signal_frame {
3161     siginfo_t info;
3162     struct ucontext uc;
3163     uint32_t tramp[2];
3164 };
3165 
3166 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3167 {
3168     __put_user(env->regs[0], &sc->regs.r0);
3169     __put_user(env->regs[1], &sc->regs.r1);
3170     __put_user(env->regs[2], &sc->regs.r2);
3171     __put_user(env->regs[3], &sc->regs.r3);
3172     __put_user(env->regs[4], &sc->regs.r4);
3173     __put_user(env->regs[5], &sc->regs.r5);
3174     __put_user(env->regs[6], &sc->regs.r6);
3175     __put_user(env->regs[7], &sc->regs.r7);
3176     __put_user(env->regs[8], &sc->regs.r8);
3177     __put_user(env->regs[9], &sc->regs.r9);
3178     __put_user(env->regs[10], &sc->regs.r10);
3179     __put_user(env->regs[11], &sc->regs.r11);
3180     __put_user(env->regs[12], &sc->regs.r12);
3181     __put_user(env->regs[13], &sc->regs.r13);
3182     __put_user(env->regs[14], &sc->regs.r14);
3183     __put_user(env->regs[15], &sc->regs.r15);
3184     __put_user(env->regs[16], &sc->regs.r16);
3185     __put_user(env->regs[17], &sc->regs.r17);
3186     __put_user(env->regs[18], &sc->regs.r18);
3187     __put_user(env->regs[19], &sc->regs.r19);
3188     __put_user(env->regs[20], &sc->regs.r20);
3189     __put_user(env->regs[21], &sc->regs.r21);
3190     __put_user(env->regs[22], &sc->regs.r22);
3191     __put_user(env->regs[23], &sc->regs.r23);
3192     __put_user(env->regs[24], &sc->regs.r24);
3193     __put_user(env->regs[25], &sc->regs.r25);
3194     __put_user(env->regs[26], &sc->regs.r26);
3195     __put_user(env->regs[27], &sc->regs.r27);
3196     __put_user(env->regs[28], &sc->regs.r28);
3197     __put_user(env->regs[29], &sc->regs.r29);
3198     __put_user(env->regs[30], &sc->regs.r30);
3199     __put_user(env->regs[31], &sc->regs.r31);
3200     __put_user(env->sregs[SR_PC], &sc->regs.pc);
3201 }
3202 
3203 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3204 {
3205     __get_user(env->regs[0], &sc->regs.r0);
3206     __get_user(env->regs[1], &sc->regs.r1);
3207     __get_user(env->regs[2], &sc->regs.r2);
3208     __get_user(env->regs[3], &sc->regs.r3);
3209     __get_user(env->regs[4], &sc->regs.r4);
3210     __get_user(env->regs[5], &sc->regs.r5);
3211     __get_user(env->regs[6], &sc->regs.r6);
3212     __get_user(env->regs[7], &sc->regs.r7);
3213     __get_user(env->regs[8], &sc->regs.r8);
3214     __get_user(env->regs[9], &sc->regs.r9);
3215     __get_user(env->regs[10], &sc->regs.r10);
3216     __get_user(env->regs[11], &sc->regs.r11);
3217     __get_user(env->regs[12], &sc->regs.r12);
3218     __get_user(env->regs[13], &sc->regs.r13);
3219     __get_user(env->regs[14], &sc->regs.r14);
3220     __get_user(env->regs[15], &sc->regs.r15);
3221     __get_user(env->regs[16], &sc->regs.r16);
3222     __get_user(env->regs[17], &sc->regs.r17);
3223     __get_user(env->regs[18], &sc->regs.r18);
3224     __get_user(env->regs[19], &sc->regs.r19);
3225     __get_user(env->regs[20], &sc->regs.r20);
3226     __get_user(env->regs[21], &sc->regs.r21);
3227     __get_user(env->regs[22], &sc->regs.r22);
3228     __get_user(env->regs[23], &sc->regs.r23);
3229     __get_user(env->regs[24], &sc->regs.r24);
3230     __get_user(env->regs[25], &sc->regs.r25);
3231     __get_user(env->regs[26], &sc->regs.r26);
3232     __get_user(env->regs[27], &sc->regs.r27);
3233     __get_user(env->regs[28], &sc->regs.r28);
3234     __get_user(env->regs[29], &sc->regs.r29);
3235     __get_user(env->regs[30], &sc->regs.r30);
3236     __get_user(env->regs[31], &sc->regs.r31);
3237     __get_user(env->sregs[SR_PC], &sc->regs.pc);
3238 }
3239 
3240 static abi_ulong get_sigframe(struct target_sigaction *ka,
3241                               CPUMBState *env, int frame_size)
3242 {
3243     abi_ulong sp = env->regs[1];
3244 
3245     if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3246         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3247 
3248     return ((sp - frame_size) & -8UL);
3249 }
3250 
3251 static void setup_frame(int sig, struct target_sigaction *ka,
3252                         target_sigset_t *set, CPUMBState *env)
3253 {
3254     struct target_signal_frame *frame;
3255     abi_ulong frame_addr;
3256     int err = 0;
3257     int i;
3258 
3259     frame_addr = get_sigframe(ka, env, sizeof *frame);
3260     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3261         goto badframe;
3262 
3263     /* Save the mask.  */
3264     err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3265     if (err)
3266         goto badframe;
3267 
3268     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3269         if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3270             goto badframe;
3271     }
3272 
3273     setup_sigcontext(&frame->uc.tuc_mcontext, env);
3274 
3275     /* Set up to return from userspace. If provided, use a stub
3276        already in userspace. */
3277     /* minus 8 is offset to cater for "rtsd r15,8" offset */
3278     if (ka->sa_flags & TARGET_SA_RESTORER) {
3279         env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3280     } else {
3281         uint32_t t;
3282         /* Note, these encodings are _big endian_! */
3283         /* addi r12, r0, __NR_sigreturn */
3284         t = 0x31800000UL | TARGET_NR_sigreturn;
3285         err |= __put_user(t, frame->tramp + 0);
3286         /* brki r14, 0x8 */
3287         t = 0xb9cc0008UL;
3288         err |= __put_user(t, frame->tramp + 1);
3289 
3290         /* Return from sighandler will jump to the tramp.
3291            Negative 8 offset because return is rtsd r15, 8 */
3292         env->regs[15] = ((unsigned long)frame->tramp) - 8;
3293     }
3294 
3295     if (err)
3296         goto badframe;
3297 
3298     /* Set up registers for signal handler */
3299     env->regs[1] = frame_addr;
3300     /* Signal handler args: */
3301     env->regs[5] = sig; /* Arg 0: signum */
3302     env->regs[6] = 0;
3303     /* arg 1: sigcontext */
3304     env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3305 
3306     /* Offset of 4 to handle microblaze rtid r14, 0 */
3307     env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3308 
3309     unlock_user_struct(frame, frame_addr, 1);
3310     return;
3311   badframe:
3312     unlock_user_struct(frame, frame_addr, 1);
3313     force_sig(TARGET_SIGSEGV);
3314 }
3315 
3316 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3317                            target_siginfo_t *info,
3318                            target_sigset_t *set, CPUMBState *env)
3319 {
3320     fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3321 }
3322 
3323 long do_sigreturn(CPUMBState *env)
3324 {
3325     struct target_signal_frame *frame;
3326     abi_ulong frame_addr;
3327     target_sigset_t target_set;
3328     sigset_t set;
3329     int i;
3330 
3331     frame_addr = env->regs[R_SP];
3332     /* Make sure the guest isn't playing games.  */
3333     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3334         goto badframe;
3335 
3336     /* Restore blocked signals */
3337     if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3338         goto badframe;
3339     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3340         if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3341             goto badframe;
3342     }
3343     target_to_host_sigset_internal(&set, &target_set);
3344     sigprocmask(SIG_SETMASK, &set, NULL);
3345 
3346     restore_sigcontext(&frame->uc.tuc_mcontext, env);
3347     /* We got here through a sigreturn syscall, our path back is via an
3348        rtb insn so setup r14 for that.  */
3349     env->regs[14] = env->sregs[SR_PC];
3350 
3351     unlock_user_struct(frame, frame_addr, 0);
3352     return env->regs[10];
3353   badframe:
3354     unlock_user_struct(frame, frame_addr, 0);
3355     force_sig(TARGET_SIGSEGV);
3356 }
3357 
3358 long do_rt_sigreturn(CPUMBState *env)
3359 {
3360     fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3361     return -TARGET_ENOSYS;
3362 }
3363 
3364 #elif defined(TARGET_CRIS)
3365 
3366 struct target_sigcontext {
3367         struct target_pt_regs regs;  /* needs to be first */
3368         uint32_t oldmask;
3369         uint32_t usp;    /* usp before stacking this gunk on it */
3370 };
3371 
3372 /* Signal frames. */
3373 struct target_signal_frame {
3374         struct target_sigcontext sc;
3375         uint32_t extramask[TARGET_NSIG_WORDS - 1];
3376         uint8_t retcode[8];       /* Trampoline code. */
3377 };
3378 
3379 struct rt_signal_frame {
3380         siginfo_t *pinfo;
3381         void *puc;
3382         siginfo_t info;
3383         struct ucontext uc;
3384         uint8_t retcode[8];       /* Trampoline code. */
3385 };
3386 
3387 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3388 {
3389 	__put_user(env->regs[0], &sc->regs.r0);
3390 	__put_user(env->regs[1], &sc->regs.r1);
3391 	__put_user(env->regs[2], &sc->regs.r2);
3392 	__put_user(env->regs[3], &sc->regs.r3);
3393 	__put_user(env->regs[4], &sc->regs.r4);
3394 	__put_user(env->regs[5], &sc->regs.r5);
3395 	__put_user(env->regs[6], &sc->regs.r6);
3396 	__put_user(env->regs[7], &sc->regs.r7);
3397 	__put_user(env->regs[8], &sc->regs.r8);
3398 	__put_user(env->regs[9], &sc->regs.r9);
3399 	__put_user(env->regs[10], &sc->regs.r10);
3400 	__put_user(env->regs[11], &sc->regs.r11);
3401 	__put_user(env->regs[12], &sc->regs.r12);
3402 	__put_user(env->regs[13], &sc->regs.r13);
3403 	__put_user(env->regs[14], &sc->usp);
3404 	__put_user(env->regs[15], &sc->regs.acr);
3405 	__put_user(env->pregs[PR_MOF], &sc->regs.mof);
3406 	__put_user(env->pregs[PR_SRP], &sc->regs.srp);
3407 	__put_user(env->pc, &sc->regs.erp);
3408 }
3409 
3410 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3411 {
3412 	__get_user(env->regs[0], &sc->regs.r0);
3413 	__get_user(env->regs[1], &sc->regs.r1);
3414 	__get_user(env->regs[2], &sc->regs.r2);
3415 	__get_user(env->regs[3], &sc->regs.r3);
3416 	__get_user(env->regs[4], &sc->regs.r4);
3417 	__get_user(env->regs[5], &sc->regs.r5);
3418 	__get_user(env->regs[6], &sc->regs.r6);
3419 	__get_user(env->regs[7], &sc->regs.r7);
3420 	__get_user(env->regs[8], &sc->regs.r8);
3421 	__get_user(env->regs[9], &sc->regs.r9);
3422 	__get_user(env->regs[10], &sc->regs.r10);
3423 	__get_user(env->regs[11], &sc->regs.r11);
3424 	__get_user(env->regs[12], &sc->regs.r12);
3425 	__get_user(env->regs[13], &sc->regs.r13);
3426 	__get_user(env->regs[14], &sc->usp);
3427 	__get_user(env->regs[15], &sc->regs.acr);
3428 	__get_user(env->pregs[PR_MOF], &sc->regs.mof);
3429 	__get_user(env->pregs[PR_SRP], &sc->regs.srp);
3430 	__get_user(env->pc, &sc->regs.erp);
3431 }
3432 
3433 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3434 {
3435 	abi_ulong sp;
3436 	/* Align the stack downwards to 4.  */
3437 	sp = (env->regs[R_SP] & ~3);
3438 	return sp - framesize;
3439 }
3440 
3441 static void setup_frame(int sig, struct target_sigaction *ka,
3442                         target_sigset_t *set, CPUCRISState *env)
3443 {
3444 	struct target_signal_frame *frame;
3445 	abi_ulong frame_addr;
3446 	int err = 0;
3447 	int i;
3448 
3449 	frame_addr = get_sigframe(env, sizeof *frame);
3450 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3451 		goto badframe;
3452 
3453 	/*
3454 	 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3455 	 * use this trampoline anymore but it sets it up for GDB.
3456 	 * In QEMU, using the trampoline simplifies things a bit so we use it.
3457 	 *
3458 	 * This is movu.w __NR_sigreturn, r9; break 13;
3459 	 */
3460 	err |= __put_user(0x9c5f, frame->retcode+0);
3461 	err |= __put_user(TARGET_NR_sigreturn,
3462 			  frame->retcode+2);
3463 	err |= __put_user(0xe93d, frame->retcode+4);
3464 
3465 	/* Save the mask.  */
3466 	err |= __put_user(set->sig[0], &frame->sc.oldmask);
3467 	if (err)
3468 		goto badframe;
3469 
3470 	for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3471 		if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3472 			goto badframe;
3473 	}
3474 
3475 	setup_sigcontext(&frame->sc, env);
3476 
3477 	/* Move the stack and setup the arguments for the handler.  */
3478 	env->regs[R_SP] = frame_addr;
3479 	env->regs[10] = sig;
3480 	env->pc = (unsigned long) ka->_sa_handler;
3481 	/* Link SRP so the guest returns through the trampoline.  */
3482 	env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3483 
3484 	unlock_user_struct(frame, frame_addr, 1);
3485 	return;
3486   badframe:
3487 	unlock_user_struct(frame, frame_addr, 1);
3488 	force_sig(TARGET_SIGSEGV);
3489 }
3490 
3491 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3492                            target_siginfo_t *info,
3493                            target_sigset_t *set, CPUCRISState *env)
3494 {
3495     fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3496 }
3497 
3498 long do_sigreturn(CPUCRISState *env)
3499 {
3500 	struct target_signal_frame *frame;
3501 	abi_ulong frame_addr;
3502 	target_sigset_t target_set;
3503 	sigset_t set;
3504 	int i;
3505 
3506 	frame_addr = env->regs[R_SP];
3507 	/* Make sure the guest isn't playing games.  */
3508 	if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3509 		goto badframe;
3510 
3511 	/* Restore blocked signals */
3512 	if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3513 		goto badframe;
3514 	for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3515 		if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3516 			goto badframe;
3517 	}
3518 	target_to_host_sigset_internal(&set, &target_set);
3519 	sigprocmask(SIG_SETMASK, &set, NULL);
3520 
3521 	restore_sigcontext(&frame->sc, env);
3522 	unlock_user_struct(frame, frame_addr, 0);
3523 	return env->regs[10];
3524   badframe:
3525 	unlock_user_struct(frame, frame_addr, 0);
3526 	force_sig(TARGET_SIGSEGV);
3527 }
3528 
3529 long do_rt_sigreturn(CPUCRISState *env)
3530 {
3531     fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3532     return -TARGET_ENOSYS;
3533 }
3534 
3535 #elif defined(TARGET_OPENRISC)
3536 
3537 struct target_sigcontext {
3538     struct target_pt_regs regs;
3539     abi_ulong oldmask;
3540     abi_ulong usp;
3541 };
3542 
3543 struct target_ucontext {
3544     abi_ulong tuc_flags;
3545     abi_ulong tuc_link;
3546     target_stack_t tuc_stack;
3547     struct target_sigcontext tuc_mcontext;
3548     target_sigset_t tuc_sigmask;   /* mask last for extensibility */
3549 };
3550 
3551 struct target_rt_sigframe {
3552     abi_ulong pinfo;
3553     uint64_t puc;
3554     struct target_siginfo info;
3555     struct target_sigcontext sc;
3556     struct target_ucontext uc;
3557     unsigned char retcode[16];  /* trampoline code */
3558 };
3559 
3560 /* This is the asm-generic/ucontext.h version */
3561 #if 0
3562 static int restore_sigcontext(CPUOpenRISCState *regs,
3563                               struct target_sigcontext *sc)
3564 {
3565     unsigned int err = 0;
3566     unsigned long old_usp;
3567 
3568     /* Alwys make any pending restarted system call return -EINTR */
3569     current_thread_info()->restart_block.fn = do_no_restart_syscall;
3570 
3571     /* restore the regs from &sc->regs (same as sc, since regs is first)
3572      * (sc is already checked for VERIFY_READ since the sigframe was
3573      *  checked in sys_sigreturn previously)
3574      */
3575 
3576     if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3577         goto badframe;
3578     }
3579 
3580     /* make sure the U-flag is set so user-mode cannot fool us */
3581 
3582     regs->sr &= ~SR_SM;
3583 
3584     /* restore the old USP as it was before we stacked the sc etc.
3585      * (we cannot just pop the sigcontext since we aligned the sp and
3586      *  stuff after pushing it)
3587      */
3588 
3589     err |= __get_user(old_usp, &sc->usp);
3590     phx_signal("old_usp 0x%lx", old_usp);
3591 
3592     __PHX__ REALLY           /* ??? */
3593     wrusp(old_usp);
3594     regs->gpr[1] = old_usp;
3595 
3596     /* TODO: the other ports use regs->orig_XX to disable syscall checks
3597      * after this completes, but we don't use that mechanism. maybe we can
3598      * use it now ?
3599      */
3600 
3601     return err;
3602 
3603 badframe:
3604     return 1;
3605 }
3606 #endif
3607 
3608 /* Set up a signal frame.  */
3609 
3610 static int setup_sigcontext(struct target_sigcontext *sc,
3611                             CPUOpenRISCState *regs,
3612                             unsigned long mask)
3613 {
3614     int err = 0;
3615     unsigned long usp = regs->gpr[1];
3616 
3617     /* copy the regs. they are first in sc so we can use sc directly */
3618 
3619     /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3620 
3621     /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3622        the signal handler. The frametype will be restored to its previous
3623        value in restore_sigcontext. */
3624     /*regs->frametype = CRIS_FRAME_NORMAL;*/
3625 
3626     /* then some other stuff */
3627     err |= __put_user(mask, &sc->oldmask);
3628     err |= __put_user(usp, &sc->usp); return err;
3629 }
3630 
3631 static inline unsigned long align_sigframe(unsigned long sp)
3632 {
3633     unsigned long i;
3634     i = sp & ~3UL;
3635     return i;
3636 }
3637 
3638 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3639                                      CPUOpenRISCState *regs,
3640                                      size_t frame_size)
3641 {
3642     unsigned long sp = regs->gpr[1];
3643     int onsigstack = on_sig_stack(sp);
3644 
3645     /* redzone */
3646     /* This is the X/Open sanctioned signal stack switching.  */
3647     if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3648         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3649     }
3650 
3651     sp = align_sigframe(sp - frame_size);
3652 
3653     /*
3654      * If we are on the alternate signal stack and would overflow it, don't.
3655      * Return an always-bogus address instead so we will die with SIGSEGV.
3656      */
3657 
3658     if (onsigstack && !likely(on_sig_stack(sp))) {
3659         return -1L;
3660     }
3661 
3662     return sp;
3663 }
3664 
3665 static void setup_frame(int sig, struct target_sigaction *ka,
3666                         target_sigset_t *set, CPUOpenRISCState *env)
3667 {
3668     qemu_log("Not implement.\n");
3669 }
3670 
3671 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3672                            target_siginfo_t *info,
3673                            target_sigset_t *set, CPUOpenRISCState *env)
3674 {
3675     int err = 0;
3676     abi_ulong frame_addr;
3677     unsigned long return_ip;
3678     struct target_rt_sigframe *frame;
3679     abi_ulong info_addr, uc_addr;
3680 
3681     frame_addr = get_sigframe(ka, env, sizeof *frame);
3682 
3683     frame_addr = get_sigframe(ka, env, sizeof(*frame));
3684     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3685         goto give_sigsegv;
3686     }
3687 
3688     info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3689     err |= __put_user(info_addr, &frame->pinfo);
3690     uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3691     err |= __put_user(uc_addr, &frame->puc);
3692 
3693     if (ka->sa_flags & SA_SIGINFO) {
3694         err |= copy_siginfo_to_user(&frame->info, info);
3695     }
3696     if (err) {
3697         goto give_sigsegv;
3698     }
3699 
3700     /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3701     err |= __put_user(0, &frame->uc.tuc_flags);
3702     err |= __put_user(0, &frame->uc.tuc_link);
3703     err |= __put_user(target_sigaltstack_used.ss_sp,
3704                       &frame->uc.tuc_stack.ss_sp);
3705     err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3706     err |= __put_user(target_sigaltstack_used.ss_size,
3707                       &frame->uc.tuc_stack.ss_size);
3708     err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
3709 
3710     /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3711 
3712     if (err) {
3713         goto give_sigsegv;
3714     }
3715 
3716     /* trampoline - the desired return ip is the retcode itself */
3717     return_ip = (unsigned long)&frame->retcode;
3718     /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3719     err |= __put_user(0xa960, (short *)(frame->retcode + 0));
3720     err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3721     err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3722     err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3723 
3724     if (err) {
3725         goto give_sigsegv;
3726     }
3727 
3728     /* TODO what is the current->exec_domain stuff and invmap ? */
3729 
3730     /* Set up registers for signal handler */
3731     env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3732     env->gpr[9] = (unsigned long)return_ip;     /* what we enter LATER */
3733     env->gpr[3] = (unsigned long)sig;           /* arg 1: signo */
3734     env->gpr[4] = (unsigned long)&frame->info;  /* arg 2: (siginfo_t*) */
3735     env->gpr[5] = (unsigned long)&frame->uc;    /* arg 3: ucontext */
3736 
3737     /* actually move the usp to reflect the stacked frame */
3738     env->gpr[1] = (unsigned long)frame;
3739 
3740     return;
3741 
3742 give_sigsegv:
3743     unlock_user_struct(frame, frame_addr, 1);
3744     if (sig == TARGET_SIGSEGV) {
3745         ka->_sa_handler = TARGET_SIG_DFL;
3746     }
3747     force_sig(TARGET_SIGSEGV);
3748 }
3749 
3750 long do_sigreturn(CPUOpenRISCState *env)
3751 {
3752 
3753     qemu_log("do_sigreturn: not implemented\n");
3754     return -TARGET_ENOSYS;
3755 }
3756 
3757 long do_rt_sigreturn(CPUOpenRISCState *env)
3758 {
3759     qemu_log("do_rt_sigreturn: not implemented\n");
3760     return -TARGET_ENOSYS;
3761 }
3762 /* TARGET_OPENRISC */
3763 
3764 #elif defined(TARGET_S390X)
3765 
3766 #define __NUM_GPRS 16
3767 #define __NUM_FPRS 16
3768 #define __NUM_ACRS 16
3769 
3770 #define S390_SYSCALL_SIZE   2
3771 #define __SIGNAL_FRAMESIZE      160 /* FIXME: 31-bit mode -> 96 */
3772 
3773 #define _SIGCONTEXT_NSIG        64
3774 #define _SIGCONTEXT_NSIG_BPW    64 /* FIXME: 31-bit mode -> 32 */
3775 #define _SIGCONTEXT_NSIG_WORDS  (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3776 #define _SIGMASK_COPY_SIZE    (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3777 #define PSW_ADDR_AMODE            0x0000000000000000UL /* 0x80000000UL for 31-bit */
3778 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3779 
3780 typedef struct {
3781     target_psw_t psw;
3782     target_ulong gprs[__NUM_GPRS];
3783     unsigned int acrs[__NUM_ACRS];
3784 } target_s390_regs_common;
3785 
3786 typedef struct {
3787     unsigned int fpc;
3788     double   fprs[__NUM_FPRS];
3789 } target_s390_fp_regs;
3790 
3791 typedef struct {
3792     target_s390_regs_common regs;
3793     target_s390_fp_regs     fpregs;
3794 } target_sigregs;
3795 
3796 struct target_sigcontext {
3797     target_ulong   oldmask[_SIGCONTEXT_NSIG_WORDS];
3798     target_sigregs *sregs;
3799 };
3800 
3801 typedef struct {
3802     uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3803     struct target_sigcontext sc;
3804     target_sigregs sregs;
3805     int signo;
3806     uint8_t retcode[S390_SYSCALL_SIZE];
3807 } sigframe;
3808 
3809 struct target_ucontext {
3810     target_ulong tuc_flags;
3811     struct target_ucontext *tuc_link;
3812     target_stack_t tuc_stack;
3813     target_sigregs tuc_mcontext;
3814     target_sigset_t tuc_sigmask;   /* mask last for extensibility */
3815 };
3816 
3817 typedef struct {
3818     uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3819     uint8_t retcode[S390_SYSCALL_SIZE];
3820     struct target_siginfo info;
3821     struct target_ucontext uc;
3822 } rt_sigframe;
3823 
3824 static inline abi_ulong
3825 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
3826 {
3827     abi_ulong sp;
3828 
3829     /* Default to using normal stack */
3830     sp = env->regs[15];
3831 
3832     /* This is the X/Open sanctioned signal stack switching.  */
3833     if (ka->sa_flags & TARGET_SA_ONSTACK) {
3834         if (!sas_ss_flags(sp)) {
3835             sp = target_sigaltstack_used.ss_sp +
3836                  target_sigaltstack_used.ss_size;
3837         }
3838     }
3839 
3840     /* This is the legacy signal stack switching. */
3841     else if (/* FIXME !user_mode(regs) */ 0 &&
3842              !(ka->sa_flags & TARGET_SA_RESTORER) &&
3843              ka->sa_restorer) {
3844         sp = (abi_ulong) ka->sa_restorer;
3845     }
3846 
3847     return (sp - frame_size) & -8ul;
3848 }
3849 
3850 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
3851 {
3852     int i;
3853     //save_access_regs(current->thread.acrs); FIXME
3854 
3855     /* Copy a 'clean' PSW mask to the user to avoid leaking
3856        information about whether PER is currently on.  */
3857     __put_user(env->psw.mask, &sregs->regs.psw.mask);
3858     __put_user(env->psw.addr, &sregs->regs.psw.addr);
3859     for (i = 0; i < 16; i++) {
3860         __put_user(env->regs[i], &sregs->regs.gprs[i]);
3861     }
3862     for (i = 0; i < 16; i++) {
3863         __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3864     }
3865     /*
3866      * We have to store the fp registers to current->thread.fp_regs
3867      * to merge them with the emulated registers.
3868      */
3869     //save_fp_regs(&current->thread.fp_regs); FIXME
3870     for (i = 0; i < 16; i++) {
3871         __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3872     }
3873 }
3874 
3875 static void setup_frame(int sig, struct target_sigaction *ka,
3876                         target_sigset_t *set, CPUS390XState *env)
3877 {
3878     sigframe *frame;
3879     abi_ulong frame_addr;
3880 
3881     frame_addr = get_sigframe(ka, env, sizeof(*frame));
3882     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3883              (unsigned long long)frame_addr);
3884     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3885             goto give_sigsegv;
3886     }
3887 
3888     qemu_log("%s: 1\n", __FUNCTION__);
3889     if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
3890               goto give_sigsegv;
3891     }
3892 
3893     save_sigregs(env, &frame->sregs);
3894 
3895     __put_user((abi_ulong)(unsigned long)&frame->sregs,
3896                (abi_ulong *)&frame->sc.sregs);
3897 
3898     /* Set up to return from userspace.  If provided, use a stub
3899        already in userspace.  */
3900     if (ka->sa_flags & TARGET_SA_RESTORER) {
3901             env->regs[14] = (unsigned long)
3902                     ka->sa_restorer | PSW_ADDR_AMODE;
3903     } else {
3904             env->regs[14] = (unsigned long)
3905                     frame->retcode | PSW_ADDR_AMODE;
3906             if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
3907                            (uint16_t *)(frame->retcode)))
3908                     goto give_sigsegv;
3909     }
3910 
3911     /* Set up backchain. */
3912     if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3913             goto give_sigsegv;
3914     }
3915 
3916     /* Set up registers for signal handler */
3917     env->regs[15] = frame_addr;
3918     env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3919 
3920     env->regs[2] = sig; //map_signal(sig);
3921     env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
3922 
3923     /* We forgot to include these in the sigcontext.
3924        To avoid breaking binary compatibility, they are passed as args. */
3925     env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
3926     env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
3927 
3928     /* Place signal number on stack to allow backtrace from handler.  */
3929     if (__put_user(env->regs[2], (int *) &frame->signo)) {
3930             goto give_sigsegv;
3931     }
3932     unlock_user_struct(frame, frame_addr, 1);
3933     return;
3934 
3935 give_sigsegv:
3936     qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3937     unlock_user_struct(frame, frame_addr, 1);
3938     force_sig(TARGET_SIGSEGV);
3939 }
3940 
3941 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3942                            target_siginfo_t *info,
3943                            target_sigset_t *set, CPUS390XState *env)
3944 {
3945     int i;
3946     rt_sigframe *frame;
3947     abi_ulong frame_addr;
3948 
3949     frame_addr = get_sigframe(ka, env, sizeof *frame);
3950     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3951              (unsigned long long)frame_addr);
3952     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3953         goto give_sigsegv;
3954     }
3955 
3956     qemu_log("%s: 1\n", __FUNCTION__);
3957     if (copy_siginfo_to_user(&frame->info, info)) {
3958         goto give_sigsegv;
3959     }
3960 
3961     /* Create the ucontext.  */
3962     __put_user(0, &frame->uc.tuc_flags);
3963     __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
3964     __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
3965     __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3966                       &frame->uc.tuc_stack.ss_flags);
3967     __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
3968     save_sigregs(env, &frame->uc.tuc_mcontext);
3969     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
3970         __put_user((abi_ulong)set->sig[i],
3971         (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
3972     }
3973 
3974     /* Set up to return from userspace.  If provided, use a stub
3975        already in userspace.  */
3976     if (ka->sa_flags & TARGET_SA_RESTORER) {
3977         env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
3978     } else {
3979         env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
3980         if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
3981                        (uint16_t *)(frame->retcode))) {
3982             goto give_sigsegv;
3983         }
3984     }
3985 
3986     /* Set up backchain. */
3987     if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3988         goto give_sigsegv;
3989     }
3990 
3991     /* Set up registers for signal handler */
3992     env->regs[15] = frame_addr;
3993     env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3994 
3995     env->regs[2] = sig; //map_signal(sig);
3996     env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
3997     env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
3998     return;
3999 
4000 give_sigsegv:
4001     qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4002     unlock_user_struct(frame, frame_addr, 1);
4003     force_sig(TARGET_SIGSEGV);
4004 }
4005 
4006 static int
4007 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4008 {
4009     int err = 0;
4010     int i;
4011 
4012     for (i = 0; i < 16; i++) {
4013         err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4014     }
4015 
4016     err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4017     qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4018              __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4019              (unsigned long long)env->psw.addr);
4020     err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4021     /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4022 
4023     for (i = 0; i < 16; i++) {
4024         err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4025     }
4026     for (i = 0; i < 16; i++) {
4027         err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4028     }
4029 
4030     return err;
4031 }
4032 
4033 long do_sigreturn(CPUS390XState *env)
4034 {
4035     sigframe *frame;
4036     abi_ulong frame_addr = env->regs[15];
4037     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4038              (unsigned long long)frame_addr);
4039     target_sigset_t target_set;
4040     sigset_t set;
4041 
4042     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4043         goto badframe;
4044     }
4045     if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4046         goto badframe;
4047     }
4048 
4049     target_to_host_sigset_internal(&set, &target_set);
4050     sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4051 
4052     if (restore_sigregs(env, &frame->sregs)) {
4053         goto badframe;
4054     }
4055 
4056     unlock_user_struct(frame, frame_addr, 0);
4057     return env->regs[2];
4058 
4059 badframe:
4060     unlock_user_struct(frame, frame_addr, 0);
4061     force_sig(TARGET_SIGSEGV);
4062     return 0;
4063 }
4064 
4065 long do_rt_sigreturn(CPUS390XState *env)
4066 {
4067     rt_sigframe *frame;
4068     abi_ulong frame_addr = env->regs[15];
4069     qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4070              (unsigned long long)frame_addr);
4071     sigset_t set;
4072 
4073     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4074         goto badframe;
4075     }
4076     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4077 
4078     sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4079 
4080     if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4081         goto badframe;
4082     }
4083 
4084     if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4085                        get_sp_from_cpustate(env)) == -EFAULT) {
4086         goto badframe;
4087     }
4088     unlock_user_struct(frame, frame_addr, 0);
4089     return env->regs[2];
4090 
4091 badframe:
4092     unlock_user_struct(frame, frame_addr, 0);
4093     force_sig(TARGET_SIGSEGV);
4094     return 0;
4095 }
4096 
4097 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4098 
4099 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4100    the signal handling is different enough that we haven't implemented
4101    support for PPC64 yet.  Hence the restriction above.
4102 
4103    There are various #if'd blocks for code for TARGET_PPC64.  These
4104    blocks should go away so that we can successfully run 32-bit and
4105    64-bit binaries on a QEMU configured for PPC64.  */
4106 
4107 /* Size of dummy stack frame allocated when calling signal handler.
4108    See arch/powerpc/include/asm/ptrace.h.  */
4109 #if defined(TARGET_PPC64)
4110 #define SIGNAL_FRAMESIZE 128
4111 #else
4112 #define SIGNAL_FRAMESIZE 64
4113 #endif
4114 
4115 /* See arch/powerpc/include/asm/sigcontext.h.  */
4116 struct target_sigcontext {
4117     target_ulong _unused[4];
4118     int32_t signal;
4119 #if defined(TARGET_PPC64)
4120     int32_t pad0;
4121 #endif
4122     target_ulong handler;
4123     target_ulong oldmask;
4124     target_ulong regs;      /* struct pt_regs __user * */
4125     /* TODO: PPC64 includes extra bits here.  */
4126 };
4127 
4128 /* Indices for target_mcontext.mc_gregs, below.
4129    See arch/powerpc/include/asm/ptrace.h for details.  */
4130 enum {
4131     TARGET_PT_R0 = 0,
4132     TARGET_PT_R1 = 1,
4133     TARGET_PT_R2 = 2,
4134     TARGET_PT_R3 = 3,
4135     TARGET_PT_R4 = 4,
4136     TARGET_PT_R5 = 5,
4137     TARGET_PT_R6 = 6,
4138     TARGET_PT_R7 = 7,
4139     TARGET_PT_R8 = 8,
4140     TARGET_PT_R9 = 9,
4141     TARGET_PT_R10 = 10,
4142     TARGET_PT_R11 = 11,
4143     TARGET_PT_R12 = 12,
4144     TARGET_PT_R13 = 13,
4145     TARGET_PT_R14 = 14,
4146     TARGET_PT_R15 = 15,
4147     TARGET_PT_R16 = 16,
4148     TARGET_PT_R17 = 17,
4149     TARGET_PT_R18 = 18,
4150     TARGET_PT_R19 = 19,
4151     TARGET_PT_R20 = 20,
4152     TARGET_PT_R21 = 21,
4153     TARGET_PT_R22 = 22,
4154     TARGET_PT_R23 = 23,
4155     TARGET_PT_R24 = 24,
4156     TARGET_PT_R25 = 25,
4157     TARGET_PT_R26 = 26,
4158     TARGET_PT_R27 = 27,
4159     TARGET_PT_R28 = 28,
4160     TARGET_PT_R29 = 29,
4161     TARGET_PT_R30 = 30,
4162     TARGET_PT_R31 = 31,
4163     TARGET_PT_NIP = 32,
4164     TARGET_PT_MSR = 33,
4165     TARGET_PT_ORIG_R3 = 34,
4166     TARGET_PT_CTR = 35,
4167     TARGET_PT_LNK = 36,
4168     TARGET_PT_XER = 37,
4169     TARGET_PT_CCR = 38,
4170     /* Yes, there are two registers with #39.  One is 64-bit only.  */
4171     TARGET_PT_MQ = 39,
4172     TARGET_PT_SOFTE = 39,
4173     TARGET_PT_TRAP = 40,
4174     TARGET_PT_DAR = 41,
4175     TARGET_PT_DSISR = 42,
4176     TARGET_PT_RESULT = 43,
4177     TARGET_PT_REGS_COUNT = 44
4178 };
4179 
4180 /* See arch/powerpc/include/asm/ucontext.h.  Only used for 32-bit PPC;
4181    on 64-bit PPC, sigcontext and mcontext are one and the same.  */
4182 struct target_mcontext {
4183     target_ulong mc_gregs[48];
4184     /* Includes fpscr.  */
4185     uint64_t mc_fregs[33];
4186     target_ulong mc_pad[2];
4187     /* We need to handle Altivec and SPE at the same time, which no
4188        kernel needs to do.  Fortunately, the kernel defines this bit to
4189        be Altivec-register-large all the time, rather than trying to
4190        twiddle it based on the specific platform.  */
4191     union {
4192         /* SPE vector registers.  One extra for SPEFSCR.  */
4193         uint32_t spe[33];
4194         /* Altivec vector registers.  The packing of VSCR and VRSAVE
4195            varies depending on whether we're PPC64 or not: PPC64 splits
4196            them apart; PPC32 stuffs them together.  */
4197 #if defined(TARGET_PPC64)
4198 #define QEMU_NVRREG 34
4199 #else
4200 #define QEMU_NVRREG 33
4201 #endif
4202         ppc_avr_t altivec[QEMU_NVRREG];
4203 #undef QEMU_NVRREG
4204     } mc_vregs __attribute__((__aligned__(16)));
4205 };
4206 
4207 struct target_ucontext {
4208     target_ulong tuc_flags;
4209     target_ulong tuc_link;    /* struct ucontext __user * */
4210     struct target_sigaltstack tuc_stack;
4211 #if !defined(TARGET_PPC64)
4212     int32_t tuc_pad[7];
4213     target_ulong tuc_regs;    /* struct mcontext __user *
4214                                 points to uc_mcontext field */
4215 #endif
4216     target_sigset_t tuc_sigmask;
4217 #if defined(TARGET_PPC64)
4218     target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4219     struct target_sigcontext tuc_mcontext;
4220 #else
4221     int32_t tuc_maskext[30];
4222     int32_t tuc_pad2[3];
4223     struct target_mcontext tuc_mcontext;
4224 #endif
4225 };
4226 
4227 /* See arch/powerpc/kernel/signal_32.c.  */
4228 struct target_sigframe {
4229     struct target_sigcontext sctx;
4230     struct target_mcontext mctx;
4231     int32_t abigap[56];
4232 };
4233 
4234 struct target_rt_sigframe {
4235     struct target_siginfo info;
4236     struct target_ucontext uc;
4237     int32_t abigap[56];
4238 };
4239 
4240 /* We use the mc_pad field for the signal return trampoline.  */
4241 #define tramp mc_pad
4242 
4243 /* See arch/powerpc/kernel/signal.c.  */
4244 static target_ulong get_sigframe(struct target_sigaction *ka,
4245                                  CPUPPCState *env,
4246                                  int frame_size)
4247 {
4248     target_ulong oldsp, newsp;
4249 
4250     oldsp = env->gpr[1];
4251 
4252     if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4253         (sas_ss_flags(oldsp) == 0)) {
4254         oldsp = (target_sigaltstack_used.ss_sp
4255                  + target_sigaltstack_used.ss_size);
4256     }
4257 
4258     newsp = (oldsp - frame_size) & ~0xFUL;
4259 
4260     return newsp;
4261 }
4262 
4263 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4264                           int sigret)
4265 {
4266     target_ulong msr = env->msr;
4267     int i;
4268     target_ulong ccr = 0;
4269 
4270     /* In general, the kernel attempts to be intelligent about what it
4271        needs to save for Altivec/FP/SPE registers.  We don't care that
4272        much, so we just go ahead and save everything.  */
4273 
4274     /* Save general registers.  */
4275     for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4276         if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4277             return 1;
4278         }
4279     }
4280     if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4281         || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4282         || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4283         || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4284         return 1;
4285 
4286     for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4287         ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4288     }
4289     if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4290         return 1;
4291 
4292     /* Save Altivec registers if necessary.  */
4293     if (env->insns_flags & PPC_ALTIVEC) {
4294         for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4295             ppc_avr_t *avr = &env->avr[i];
4296             ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4297 
4298             if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4299                 __put_user(avr->u64[1], &vreg->u64[1])) {
4300                 return 1;
4301             }
4302         }
4303         /* Set MSR_VR in the saved MSR value to indicate that
4304            frame->mc_vregs contains valid data.  */
4305         msr |= MSR_VR;
4306         if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4307                        &frame->mc_vregs.altivec[32].u32[3]))
4308             return 1;
4309     }
4310 
4311     /* Save floating point registers.  */
4312     if (env->insns_flags & PPC_FLOAT) {
4313         for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4314             if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4315                 return 1;
4316             }
4317         }
4318         if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4319             return 1;
4320     }
4321 
4322     /* Save SPE registers.  The kernel only saves the high half.  */
4323     if (env->insns_flags & PPC_SPE) {
4324 #if defined(TARGET_PPC64)
4325         for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4326             if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4327                 return 1;
4328             }
4329         }
4330 #else
4331         for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4332             if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4333                 return 1;
4334             }
4335         }
4336 #endif
4337         /* Set MSR_SPE in the saved MSR value to indicate that
4338            frame->mc_vregs contains valid data.  */
4339         msr |= MSR_SPE;
4340         if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4341             return 1;
4342     }
4343 
4344     /* Store MSR.  */
4345     if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4346         return 1;
4347 
4348     /* Set up the sigreturn trampoline: li r0,sigret; sc.  */
4349     if (sigret) {
4350         if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4351             __put_user(0x44000002UL, &frame->tramp[1])) {
4352             return 1;
4353         }
4354     }
4355 
4356     return 0;
4357 }
4358 
4359 static int restore_user_regs(CPUPPCState *env,
4360                              struct target_mcontext *frame, int sig)
4361 {
4362     target_ulong save_r2 = 0;
4363     target_ulong msr;
4364     target_ulong ccr;
4365 
4366     int i;
4367 
4368     if (!sig) {
4369         save_r2 = env->gpr[2];
4370     }
4371 
4372     /* Restore general registers.  */
4373     for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4374         if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4375             return 1;
4376         }
4377     }
4378     if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4379         || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4380         || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4381         || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4382         return 1;
4383     if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4384         return 1;
4385 
4386     for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4387         env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4388     }
4389 
4390     if (!sig) {
4391         env->gpr[2] = save_r2;
4392     }
4393     /* Restore MSR.  */
4394     if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4395         return 1;
4396 
4397     /* If doing signal return, restore the previous little-endian mode.  */
4398     if (sig)
4399         env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4400 
4401     /* Restore Altivec registers if necessary.  */
4402     if (env->insns_flags & PPC_ALTIVEC) {
4403         for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4404             ppc_avr_t *avr = &env->avr[i];
4405             ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4406 
4407             if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4408                 __get_user(avr->u64[1], &vreg->u64[1])) {
4409                 return 1;
4410             }
4411         }
4412         /* Set MSR_VEC in the saved MSR value to indicate that
4413            frame->mc_vregs contains valid data.  */
4414         if (__get_user(env->spr[SPR_VRSAVE],
4415                        (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4416             return 1;
4417     }
4418 
4419     /* Restore floating point registers.  */
4420     if (env->insns_flags & PPC_FLOAT) {
4421         uint64_t fpscr;
4422         for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4423             if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4424                 return 1;
4425             }
4426         }
4427         if (__get_user(fpscr, &frame->mc_fregs[32]))
4428             return 1;
4429         env->fpscr = (uint32_t) fpscr;
4430     }
4431 
4432     /* Save SPE registers.  The kernel only saves the high half.  */
4433     if (env->insns_flags & PPC_SPE) {
4434 #if defined(TARGET_PPC64)
4435         for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4436             uint32_t hi;
4437 
4438             if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4439                 return 1;
4440             }
4441             env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4442         }
4443 #else
4444         for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4445             if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4446                 return 1;
4447             }
4448         }
4449 #endif
4450         if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4451             return 1;
4452     }
4453 
4454     return 0;
4455 }
4456 
4457 static void setup_frame(int sig, struct target_sigaction *ka,
4458                         target_sigset_t *set, CPUPPCState *env)
4459 {
4460     struct target_sigframe *frame;
4461     struct target_sigcontext *sc;
4462     target_ulong frame_addr, newsp;
4463     int err = 0;
4464     int signal;
4465 
4466     frame_addr = get_sigframe(ka, env, sizeof(*frame));
4467     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4468         goto sigsegv;
4469     sc = &frame->sctx;
4470 
4471     signal = current_exec_domain_sig(sig);
4472 
4473     err |= __put_user(ka->_sa_handler, &sc->handler);
4474     err |= __put_user(set->sig[0], &sc->oldmask);
4475 #if defined(TARGET_PPC64)
4476     err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4477 #else
4478     err |= __put_user(set->sig[1], &sc->_unused[3]);
4479 #endif
4480     err |= __put_user(h2g(&frame->mctx), &sc->regs);
4481     err |= __put_user(sig, &sc->signal);
4482 
4483     /* Save user regs.  */
4484     err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4485 
4486     /* The kernel checks for the presence of a VDSO here.  We don't
4487        emulate a vdso, so use a sigreturn system call.  */
4488     env->lr = (target_ulong) h2g(frame->mctx.tramp);
4489 
4490     /* Turn off all fp exceptions.  */
4491     env->fpscr = 0;
4492 
4493     /* Create a stack frame for the caller of the handler.  */
4494     newsp = frame_addr - SIGNAL_FRAMESIZE;
4495     err |= put_user(env->gpr[1], newsp, target_ulong);
4496 
4497     if (err)
4498         goto sigsegv;
4499 
4500     /* Set up registers for signal handler.  */
4501     env->gpr[1] = newsp;
4502     env->gpr[3] = signal;
4503     env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4504     env->nip = (target_ulong) ka->_sa_handler;
4505     /* Signal handlers are entered in big-endian mode.  */
4506     env->msr &= ~MSR_LE;
4507 
4508     unlock_user_struct(frame, frame_addr, 1);
4509     return;
4510 
4511 sigsegv:
4512     unlock_user_struct(frame, frame_addr, 1);
4513     qemu_log("segfaulting from setup_frame\n");
4514     force_sig(TARGET_SIGSEGV);
4515 }
4516 
4517 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4518                            target_siginfo_t *info,
4519                            target_sigset_t *set, CPUPPCState *env)
4520 {
4521     struct target_rt_sigframe *rt_sf;
4522     struct target_mcontext *frame;
4523     target_ulong rt_sf_addr, newsp = 0;
4524     int i, err = 0;
4525     int signal;
4526 
4527     rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4528     if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4529         goto sigsegv;
4530 
4531     signal = current_exec_domain_sig(sig);
4532 
4533     err |= copy_siginfo_to_user(&rt_sf->info, info);
4534 
4535     err |= __put_user(0, &rt_sf->uc.tuc_flags);
4536     err |= __put_user(0, &rt_sf->uc.tuc_link);
4537     err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4538                       &rt_sf->uc.tuc_stack.ss_sp);
4539     err |= __put_user(sas_ss_flags(env->gpr[1]),
4540                       &rt_sf->uc.tuc_stack.ss_flags);
4541     err |= __put_user(target_sigaltstack_used.ss_size,
4542                       &rt_sf->uc.tuc_stack.ss_size);
4543     err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4544                       &rt_sf->uc.tuc_regs);
4545     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4546         err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4547     }
4548 
4549     frame = &rt_sf->uc.tuc_mcontext;
4550     err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4551 
4552     /* The kernel checks for the presence of a VDSO here.  We don't
4553        emulate a vdso, so use a sigreturn system call.  */
4554     env->lr = (target_ulong) h2g(frame->tramp);
4555 
4556     /* Turn off all fp exceptions.  */
4557     env->fpscr = 0;
4558 
4559     /* Create a stack frame for the caller of the handler.  */
4560     newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4561     err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4562 
4563     if (err)
4564         goto sigsegv;
4565 
4566     /* Set up registers for signal handler.  */
4567     env->gpr[1] = newsp;
4568     env->gpr[3] = (target_ulong) signal;
4569     env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4570     env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4571     env->gpr[6] = (target_ulong) h2g(rt_sf);
4572     env->nip = (target_ulong) ka->_sa_handler;
4573     /* Signal handlers are entered in big-endian mode.  */
4574     env->msr &= ~MSR_LE;
4575 
4576     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4577     return;
4578 
4579 sigsegv:
4580     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4581     qemu_log("segfaulting from setup_rt_frame\n");
4582     force_sig(TARGET_SIGSEGV);
4583 
4584 }
4585 
4586 long do_sigreturn(CPUPPCState *env)
4587 {
4588     struct target_sigcontext *sc = NULL;
4589     struct target_mcontext *sr = NULL;
4590     target_ulong sr_addr, sc_addr;
4591     sigset_t blocked;
4592     target_sigset_t set;
4593 
4594     sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4595     if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4596         goto sigsegv;
4597 
4598 #if defined(TARGET_PPC64)
4599     set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4600 #else
4601     if(__get_user(set.sig[0], &sc->oldmask) ||
4602        __get_user(set.sig[1], &sc->_unused[3]))
4603        goto sigsegv;
4604 #endif
4605     target_to_host_sigset_internal(&blocked, &set);
4606     sigprocmask(SIG_SETMASK, &blocked, NULL);
4607 
4608     if (__get_user(sr_addr, &sc->regs))
4609         goto sigsegv;
4610     if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4611         goto sigsegv;
4612     if (restore_user_regs(env, sr, 1))
4613         goto sigsegv;
4614 
4615     unlock_user_struct(sr, sr_addr, 1);
4616     unlock_user_struct(sc, sc_addr, 1);
4617     return -TARGET_QEMU_ESIGRETURN;
4618 
4619 sigsegv:
4620     unlock_user_struct(sr, sr_addr, 1);
4621     unlock_user_struct(sc, sc_addr, 1);
4622     qemu_log("segfaulting from do_sigreturn\n");
4623     force_sig(TARGET_SIGSEGV);
4624     return 0;
4625 }
4626 
4627 /* See arch/powerpc/kernel/signal_32.c.  */
4628 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4629 {
4630     struct target_mcontext *mcp;
4631     target_ulong mcp_addr;
4632     sigset_t blocked;
4633     target_sigset_t set;
4634 
4635     if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4636                        sizeof (set)))
4637         return 1;
4638 
4639 #if defined(TARGET_PPC64)
4640     fprintf (stderr, "do_setcontext: not implemented\n");
4641     return 0;
4642 #else
4643     if (__get_user(mcp_addr, &ucp->tuc_regs))
4644         return 1;
4645 
4646     if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4647         return 1;
4648 
4649     target_to_host_sigset_internal(&blocked, &set);
4650     sigprocmask(SIG_SETMASK, &blocked, NULL);
4651     if (restore_user_regs(env, mcp, sig))
4652         goto sigsegv;
4653 
4654     unlock_user_struct(mcp, mcp_addr, 1);
4655     return 0;
4656 
4657 sigsegv:
4658     unlock_user_struct(mcp, mcp_addr, 1);
4659     return 1;
4660 #endif
4661 }
4662 
4663 long do_rt_sigreturn(CPUPPCState *env)
4664 {
4665     struct target_rt_sigframe *rt_sf = NULL;
4666     target_ulong rt_sf_addr;
4667 
4668     rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4669     if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4670         goto sigsegv;
4671 
4672     if (do_setcontext(&rt_sf->uc, env, 1))
4673         goto sigsegv;
4674 
4675     do_sigaltstack(rt_sf_addr
4676                    + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4677                    0, env->gpr[1]);
4678 
4679     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4680     return -TARGET_QEMU_ESIGRETURN;
4681 
4682 sigsegv:
4683     unlock_user_struct(rt_sf, rt_sf_addr, 1);
4684     qemu_log("segfaulting from do_rt_sigreturn\n");
4685     force_sig(TARGET_SIGSEGV);
4686     return 0;
4687 }
4688 
4689 #elif defined(TARGET_M68K)
4690 
4691 struct target_sigcontext {
4692     abi_ulong  sc_mask;
4693     abi_ulong  sc_usp;
4694     abi_ulong  sc_d0;
4695     abi_ulong  sc_d1;
4696     abi_ulong  sc_a0;
4697     abi_ulong  sc_a1;
4698     unsigned short sc_sr;
4699     abi_ulong  sc_pc;
4700 };
4701 
4702 struct target_sigframe
4703 {
4704     abi_ulong pretcode;
4705     int sig;
4706     int code;
4707     abi_ulong psc;
4708     char retcode[8];
4709     abi_ulong extramask[TARGET_NSIG_WORDS-1];
4710     struct target_sigcontext sc;
4711 };
4712 
4713 typedef int target_greg_t;
4714 #define TARGET_NGREG 18
4715 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4716 
4717 typedef struct target_fpregset {
4718     int f_fpcntl[3];
4719     int f_fpregs[8*3];
4720 } target_fpregset_t;
4721 
4722 struct target_mcontext {
4723     int version;
4724     target_gregset_t gregs;
4725     target_fpregset_t fpregs;
4726 };
4727 
4728 #define TARGET_MCONTEXT_VERSION 2
4729 
4730 struct target_ucontext {
4731     abi_ulong tuc_flags;
4732     abi_ulong tuc_link;
4733     target_stack_t tuc_stack;
4734     struct target_mcontext tuc_mcontext;
4735     abi_long tuc_filler[80];
4736     target_sigset_t tuc_sigmask;
4737 };
4738 
4739 struct target_rt_sigframe
4740 {
4741     abi_ulong pretcode;
4742     int sig;
4743     abi_ulong pinfo;
4744     abi_ulong puc;
4745     char retcode[8];
4746     struct target_siginfo info;
4747     struct target_ucontext uc;
4748 };
4749 
4750 static int
4751 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4752                  abi_ulong mask)
4753 {
4754     int err = 0;
4755 
4756     err |= __put_user(mask, &sc->sc_mask);
4757     err |= __put_user(env->aregs[7], &sc->sc_usp);
4758     err |= __put_user(env->dregs[0], &sc->sc_d0);
4759     err |= __put_user(env->dregs[1], &sc->sc_d1);
4760     err |= __put_user(env->aregs[0], &sc->sc_a0);
4761     err |= __put_user(env->aregs[1], &sc->sc_a1);
4762     err |= __put_user(env->sr, &sc->sc_sr);
4763     err |= __put_user(env->pc, &sc->sc_pc);
4764 
4765     return err;
4766 }
4767 
4768 static int
4769 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
4770 {
4771     int err = 0;
4772     int temp;
4773 
4774     err |= __get_user(env->aregs[7], &sc->sc_usp);
4775     err |= __get_user(env->dregs[1], &sc->sc_d1);
4776     err |= __get_user(env->aregs[0], &sc->sc_a0);
4777     err |= __get_user(env->aregs[1], &sc->sc_a1);
4778     err |= __get_user(env->pc, &sc->sc_pc);
4779     err |= __get_user(temp, &sc->sc_sr);
4780     env->sr = (env->sr & 0xff00) | (temp & 0xff);
4781 
4782     *pd0 = tswapl(sc->sc_d0);
4783 
4784     return err;
4785 }
4786 
4787 /*
4788  * Determine which stack to use..
4789  */
4790 static inline abi_ulong
4791 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
4792              size_t frame_size)
4793 {
4794     unsigned long sp;
4795 
4796     sp = regs->aregs[7];
4797 
4798     /* This is the X/Open sanctioned signal stack switching.  */
4799     if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4800         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4801     }
4802 
4803     return ((sp - frame_size) & -8UL);
4804 }
4805 
4806 static void setup_frame(int sig, struct target_sigaction *ka,
4807                         target_sigset_t *set, CPUM68KState *env)
4808 {
4809     struct target_sigframe *frame;
4810     abi_ulong frame_addr;
4811     abi_ulong retcode_addr;
4812     abi_ulong sc_addr;
4813     int err = 0;
4814     int i;
4815 
4816     frame_addr = get_sigframe(ka, env, sizeof *frame);
4817     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4818 	goto give_sigsegv;
4819 
4820     err |= __put_user(sig, &frame->sig);
4821 
4822     sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4823     err |= __put_user(sc_addr, &frame->psc);
4824 
4825     err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4826     if (err)
4827 	goto give_sigsegv;
4828 
4829     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4830         if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4831             goto give_sigsegv;
4832     }
4833 
4834     /* Set up to return from userspace.  */
4835 
4836     retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4837     err |= __put_user(retcode_addr, &frame->pretcode);
4838 
4839     /* moveq #,d0; trap #0 */
4840 
4841     err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4842                       (long *)(frame->retcode));
4843 
4844     if (err)
4845         goto give_sigsegv;
4846 
4847     /* Set up to return from userspace */
4848 
4849     env->aregs[7] = frame_addr;
4850     env->pc = ka->_sa_handler;
4851 
4852     unlock_user_struct(frame, frame_addr, 1);
4853     return;
4854 
4855 give_sigsegv:
4856     unlock_user_struct(frame, frame_addr, 1);
4857     force_sig(TARGET_SIGSEGV);
4858 }
4859 
4860 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4861                                            CPUM68KState *env)
4862 {
4863     target_greg_t *gregs = uc->tuc_mcontext.gregs;
4864     int err;
4865 
4866     err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4867     err |= __put_user(env->dregs[0], &gregs[0]);
4868     err |= __put_user(env->dregs[1], &gregs[1]);
4869     err |= __put_user(env->dregs[2], &gregs[2]);
4870     err |= __put_user(env->dregs[3], &gregs[3]);
4871     err |= __put_user(env->dregs[4], &gregs[4]);
4872     err |= __put_user(env->dregs[5], &gregs[5]);
4873     err |= __put_user(env->dregs[6], &gregs[6]);
4874     err |= __put_user(env->dregs[7], &gregs[7]);
4875     err |= __put_user(env->aregs[0], &gregs[8]);
4876     err |= __put_user(env->aregs[1], &gregs[9]);
4877     err |= __put_user(env->aregs[2], &gregs[10]);
4878     err |= __put_user(env->aregs[3], &gregs[11]);
4879     err |= __put_user(env->aregs[4], &gregs[12]);
4880     err |= __put_user(env->aregs[5], &gregs[13]);
4881     err |= __put_user(env->aregs[6], &gregs[14]);
4882     err |= __put_user(env->aregs[7], &gregs[15]);
4883     err |= __put_user(env->pc, &gregs[16]);
4884     err |= __put_user(env->sr, &gregs[17]);
4885 
4886     return err;
4887 }
4888 
4889 static inline int target_rt_restore_ucontext(CPUM68KState *env,
4890                                              struct target_ucontext *uc,
4891                                              int *pd0)
4892 {
4893     int temp;
4894     int err;
4895     target_greg_t *gregs = uc->tuc_mcontext.gregs;
4896 
4897     err = __get_user(temp, &uc->tuc_mcontext.version);
4898     if (temp != TARGET_MCONTEXT_VERSION)
4899         goto badframe;
4900 
4901     /* restore passed registers */
4902     err |= __get_user(env->dregs[0], &gregs[0]);
4903     err |= __get_user(env->dregs[1], &gregs[1]);
4904     err |= __get_user(env->dregs[2], &gregs[2]);
4905     err |= __get_user(env->dregs[3], &gregs[3]);
4906     err |= __get_user(env->dregs[4], &gregs[4]);
4907     err |= __get_user(env->dregs[5], &gregs[5]);
4908     err |= __get_user(env->dregs[6], &gregs[6]);
4909     err |= __get_user(env->dregs[7], &gregs[7]);
4910     err |= __get_user(env->aregs[0], &gregs[8]);
4911     err |= __get_user(env->aregs[1], &gregs[9]);
4912     err |= __get_user(env->aregs[2], &gregs[10]);
4913     err |= __get_user(env->aregs[3], &gregs[11]);
4914     err |= __get_user(env->aregs[4], &gregs[12]);
4915     err |= __get_user(env->aregs[5], &gregs[13]);
4916     err |= __get_user(env->aregs[6], &gregs[14]);
4917     err |= __get_user(env->aregs[7], &gregs[15]);
4918     err |= __get_user(env->pc, &gregs[16]);
4919     err |= __get_user(temp, &gregs[17]);
4920     env->sr = (env->sr & 0xff00) | (temp & 0xff);
4921 
4922     *pd0 = env->dregs[0];
4923     return err;
4924 
4925 badframe:
4926     return 1;
4927 }
4928 
4929 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4930                            target_siginfo_t *info,
4931                            target_sigset_t *set, CPUM68KState *env)
4932 {
4933     struct target_rt_sigframe *frame;
4934     abi_ulong frame_addr;
4935     abi_ulong retcode_addr;
4936     abi_ulong info_addr;
4937     abi_ulong uc_addr;
4938     int err = 0;
4939     int i;
4940 
4941     frame_addr = get_sigframe(ka, env, sizeof *frame);
4942     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4943 	goto give_sigsegv;
4944 
4945     err |= __put_user(sig, &frame->sig);
4946 
4947     info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4948     err |= __put_user(info_addr, &frame->pinfo);
4949 
4950     uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4951     err |= __put_user(uc_addr, &frame->puc);
4952 
4953     err |= copy_siginfo_to_user(&frame->info, info);
4954 
4955     /* Create the ucontext */
4956 
4957     err |= __put_user(0, &frame->uc.tuc_flags);
4958     err |= __put_user(0, &frame->uc.tuc_link);
4959     err |= __put_user(target_sigaltstack_used.ss_sp,
4960                       &frame->uc.tuc_stack.ss_sp);
4961     err |= __put_user(sas_ss_flags(env->aregs[7]),
4962                       &frame->uc.tuc_stack.ss_flags);
4963     err |= __put_user(target_sigaltstack_used.ss_size,
4964                       &frame->uc.tuc_stack.ss_size);
4965     err |= target_rt_setup_ucontext(&frame->uc, env);
4966 
4967     if (err)
4968             goto give_sigsegv;
4969 
4970     for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4971         if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
4972             goto give_sigsegv;
4973     }
4974 
4975     /* Set up to return from userspace.  */
4976 
4977     retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4978     err |= __put_user(retcode_addr, &frame->pretcode);
4979 
4980     /* moveq #,d0; notb d0; trap #0 */
4981 
4982     err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4983                       (long *)(frame->retcode + 0));
4984     err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4985 
4986     if (err)
4987         goto give_sigsegv;
4988 
4989     /* Set up to return from userspace */
4990 
4991     env->aregs[7] = frame_addr;
4992     env->pc = ka->_sa_handler;
4993 
4994     unlock_user_struct(frame, frame_addr, 1);
4995     return;
4996 
4997 give_sigsegv:
4998     unlock_user_struct(frame, frame_addr, 1);
4999     force_sig(TARGET_SIGSEGV);
5000 }
5001 
5002 long do_sigreturn(CPUM68KState *env)
5003 {
5004     struct target_sigframe *frame;
5005     abi_ulong frame_addr = env->aregs[7] - 4;
5006     target_sigset_t target_set;
5007     sigset_t set;
5008     int d0, i;
5009 
5010     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5011         goto badframe;
5012 
5013     /* set blocked signals */
5014 
5015     if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5016         goto badframe;
5017 
5018     for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5019         if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5020             goto badframe;
5021     }
5022 
5023     target_to_host_sigset_internal(&set, &target_set);
5024     sigprocmask(SIG_SETMASK, &set, NULL);
5025 
5026     /* restore registers */
5027 
5028     if (restore_sigcontext(env, &frame->sc, &d0))
5029         goto badframe;
5030 
5031     unlock_user_struct(frame, frame_addr, 0);
5032     return d0;
5033 
5034 badframe:
5035     unlock_user_struct(frame, frame_addr, 0);
5036     force_sig(TARGET_SIGSEGV);
5037     return 0;
5038 }
5039 
5040 long do_rt_sigreturn(CPUM68KState *env)
5041 {
5042     struct target_rt_sigframe *frame;
5043     abi_ulong frame_addr = env->aregs[7] - 4;
5044     target_sigset_t target_set;
5045     sigset_t set;
5046     int d0;
5047 
5048     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5049         goto badframe;
5050 
5051     target_to_host_sigset_internal(&set, &target_set);
5052     sigprocmask(SIG_SETMASK, &set, NULL);
5053 
5054     /* restore registers */
5055 
5056     if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5057         goto badframe;
5058 
5059     if (do_sigaltstack(frame_addr +
5060                        offsetof(struct target_rt_sigframe, uc.tuc_stack),
5061                        0, get_sp_from_cpustate(env)) == -EFAULT)
5062         goto badframe;
5063 
5064     unlock_user_struct(frame, frame_addr, 0);
5065     return d0;
5066 
5067 badframe:
5068     unlock_user_struct(frame, frame_addr, 0);
5069     force_sig(TARGET_SIGSEGV);
5070     return 0;
5071 }
5072 
5073 #elif defined(TARGET_ALPHA)
5074 
5075 struct target_sigcontext {
5076     abi_long sc_onstack;
5077     abi_long sc_mask;
5078     abi_long sc_pc;
5079     abi_long sc_ps;
5080     abi_long sc_regs[32];
5081     abi_long sc_ownedfp;
5082     abi_long sc_fpregs[32];
5083     abi_ulong sc_fpcr;
5084     abi_ulong sc_fp_control;
5085     abi_ulong sc_reserved1;
5086     abi_ulong sc_reserved2;
5087     abi_ulong sc_ssize;
5088     abi_ulong sc_sbase;
5089     abi_ulong sc_traparg_a0;
5090     abi_ulong sc_traparg_a1;
5091     abi_ulong sc_traparg_a2;
5092     abi_ulong sc_fp_trap_pc;
5093     abi_ulong sc_fp_trigger_sum;
5094     abi_ulong sc_fp_trigger_inst;
5095 };
5096 
5097 struct target_ucontext {
5098     abi_ulong tuc_flags;
5099     abi_ulong tuc_link;
5100     abi_ulong tuc_osf_sigmask;
5101     target_stack_t tuc_stack;
5102     struct target_sigcontext tuc_mcontext;
5103     target_sigset_t tuc_sigmask;
5104 };
5105 
5106 struct target_sigframe {
5107     struct target_sigcontext sc;
5108     unsigned int retcode[3];
5109 };
5110 
5111 struct target_rt_sigframe {
5112     target_siginfo_t info;
5113     struct target_ucontext uc;
5114     unsigned int retcode[3];
5115 };
5116 
5117 #define INSN_MOV_R30_R16        0x47fe0410
5118 #define INSN_LDI_R0             0x201f0000
5119 #define INSN_CALLSYS            0x00000083
5120 
5121 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5122                             abi_ulong frame_addr, target_sigset_t *set)
5123 {
5124     int i, err = 0;
5125 
5126     err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5127     err |= __put_user(set->sig[0], &sc->sc_mask);
5128     err |= __put_user(env->pc, &sc->sc_pc);
5129     err |= __put_user(8, &sc->sc_ps);
5130 
5131     for (i = 0; i < 31; ++i) {
5132         err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5133     }
5134     err |= __put_user(0, &sc->sc_regs[31]);
5135 
5136     for (i = 0; i < 31; ++i) {
5137         err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5138     }
5139     err |= __put_user(0, &sc->sc_fpregs[31]);
5140     err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5141 
5142     err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5143     err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5144     err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5145 
5146     return err;
5147 }
5148 
5149 static int restore_sigcontext(CPUAlphaState *env,
5150                               struct target_sigcontext *sc)
5151 {
5152     uint64_t fpcr;
5153     int i, err = 0;
5154 
5155     err |= __get_user(env->pc, &sc->sc_pc);
5156 
5157     for (i = 0; i < 31; ++i) {
5158         err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5159     }
5160     for (i = 0; i < 31; ++i) {
5161         err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5162     }
5163 
5164     err |= __get_user(fpcr, &sc->sc_fpcr);
5165     cpu_alpha_store_fpcr(env, fpcr);
5166 
5167     return err;
5168 }
5169 
5170 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5171                                      CPUAlphaState *env,
5172                                      unsigned long framesize)
5173 {
5174     abi_ulong sp = env->ir[IR_SP];
5175 
5176     /* This is the X/Open sanctioned signal stack switching.  */
5177     if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5178         sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5179     }
5180     return (sp - framesize) & -32;
5181 }
5182 
5183 static void setup_frame(int sig, struct target_sigaction *ka,
5184                         target_sigset_t *set, CPUAlphaState *env)
5185 {
5186     abi_ulong frame_addr, r26;
5187     struct target_sigframe *frame;
5188     int err = 0;
5189 
5190     frame_addr = get_sigframe(ka, env, sizeof(*frame));
5191     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5192         goto give_sigsegv;
5193     }
5194 
5195     err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5196 
5197     if (ka->sa_restorer) {
5198         r26 = ka->sa_restorer;
5199     } else {
5200         err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5201         err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5202                           &frame->retcode[1]);
5203         err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5204         /* imb() */
5205         r26 = frame_addr;
5206     }
5207 
5208     unlock_user_struct(frame, frame_addr, 1);
5209 
5210     if (err) {
5211     give_sigsegv:
5212         if (sig == TARGET_SIGSEGV) {
5213             ka->_sa_handler = TARGET_SIG_DFL;
5214         }
5215         force_sig(TARGET_SIGSEGV);
5216     }
5217 
5218     env->ir[IR_RA] = r26;
5219     env->ir[IR_PV] = env->pc = ka->_sa_handler;
5220     env->ir[IR_A0] = sig;
5221     env->ir[IR_A1] = 0;
5222     env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5223     env->ir[IR_SP] = frame_addr;
5224 }
5225 
5226 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5227                            target_siginfo_t *info,
5228                            target_sigset_t *set, CPUAlphaState *env)
5229 {
5230     abi_ulong frame_addr, r26;
5231     struct target_rt_sigframe *frame;
5232     int i, err = 0;
5233 
5234     frame_addr = get_sigframe(ka, env, sizeof(*frame));
5235     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5236         goto give_sigsegv;
5237     }
5238 
5239     err |= copy_siginfo_to_user(&frame->info, info);
5240 
5241     err |= __put_user(0, &frame->uc.tuc_flags);
5242     err |= __put_user(0, &frame->uc.tuc_link);
5243     err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5244     err |= __put_user(target_sigaltstack_used.ss_sp,
5245                       &frame->uc.tuc_stack.ss_sp);
5246     err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5247                       &frame->uc.tuc_stack.ss_flags);
5248     err |= __put_user(target_sigaltstack_used.ss_size,
5249                       &frame->uc.tuc_stack.ss_size);
5250     err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5251     for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5252         err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5253     }
5254 
5255     if (ka->sa_restorer) {
5256         r26 = ka->sa_restorer;
5257     } else {
5258         err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5259         err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5260                           &frame->retcode[1]);
5261         err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5262         /* imb(); */
5263         r26 = frame_addr;
5264     }
5265 
5266     if (err) {
5267     give_sigsegv:
5268        if (sig == TARGET_SIGSEGV) {
5269             ka->_sa_handler = TARGET_SIG_DFL;
5270         }
5271         force_sig(TARGET_SIGSEGV);
5272     }
5273 
5274     env->ir[IR_RA] = r26;
5275     env->ir[IR_PV] = env->pc = ka->_sa_handler;
5276     env->ir[IR_A0] = sig;
5277     env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5278     env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5279     env->ir[IR_SP] = frame_addr;
5280 }
5281 
5282 long do_sigreturn(CPUAlphaState *env)
5283 {
5284     struct target_sigcontext *sc;
5285     abi_ulong sc_addr = env->ir[IR_A0];
5286     target_sigset_t target_set;
5287     sigset_t set;
5288 
5289     if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5290         goto badframe;
5291     }
5292 
5293     target_sigemptyset(&target_set);
5294     if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5295         goto badframe;
5296     }
5297 
5298     target_to_host_sigset_internal(&set, &target_set);
5299     sigprocmask(SIG_SETMASK, &set, NULL);
5300 
5301     if (restore_sigcontext(env, sc)) {
5302         goto badframe;
5303     }
5304     unlock_user_struct(sc, sc_addr, 0);
5305     return env->ir[IR_V0];
5306 
5307  badframe:
5308     unlock_user_struct(sc, sc_addr, 0);
5309     force_sig(TARGET_SIGSEGV);
5310 }
5311 
5312 long do_rt_sigreturn(CPUAlphaState *env)
5313 {
5314     abi_ulong frame_addr = env->ir[IR_A0];
5315     struct target_rt_sigframe *frame;
5316     sigset_t set;
5317 
5318     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5319         goto badframe;
5320     }
5321     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5322     sigprocmask(SIG_SETMASK, &set, NULL);
5323 
5324     if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5325         goto badframe;
5326     }
5327     if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5328                                              uc.tuc_stack),
5329                        0, env->ir[IR_SP]) == -EFAULT) {
5330         goto badframe;
5331     }
5332 
5333     unlock_user_struct(frame, frame_addr, 0);
5334     return env->ir[IR_V0];
5335 
5336 
5337  badframe:
5338     unlock_user_struct(frame, frame_addr, 0);
5339     force_sig(TARGET_SIGSEGV);
5340 }
5341 
5342 #else
5343 
5344 static void setup_frame(int sig, struct target_sigaction *ka,
5345 			target_sigset_t *set, CPUArchState *env)
5346 {
5347     fprintf(stderr, "setup_frame: not implemented\n");
5348 }
5349 
5350 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5351                            target_siginfo_t *info,
5352 			   target_sigset_t *set, CPUArchState *env)
5353 {
5354     fprintf(stderr, "setup_rt_frame: not implemented\n");
5355 }
5356 
5357 long do_sigreturn(CPUArchState *env)
5358 {
5359     fprintf(stderr, "do_sigreturn: not implemented\n");
5360     return -TARGET_ENOSYS;
5361 }
5362 
5363 long do_rt_sigreturn(CPUArchState *env)
5364 {
5365     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5366     return -TARGET_ENOSYS;
5367 }
5368 
5369 #endif
5370 
5371 void process_pending_signals(CPUArchState *cpu_env)
5372 {
5373     int sig;
5374     abi_ulong handler;
5375     sigset_t set, old_set;
5376     target_sigset_t target_old_set;
5377     struct emulated_sigtable *k;
5378     struct target_sigaction *sa;
5379     struct sigqueue *q;
5380     TaskState *ts = cpu_env->opaque;
5381 
5382     if (!ts->signal_pending)
5383         return;
5384 
5385     /* FIXME: This is not threadsafe.  */
5386     k = ts->sigtab;
5387     for(sig = 1; sig <= TARGET_NSIG; sig++) {
5388         if (k->pending)
5389             goto handle_signal;
5390         k++;
5391     }
5392     /* if no signal is pending, just return */
5393     ts->signal_pending = 0;
5394     return;
5395 
5396  handle_signal:
5397 #ifdef DEBUG_SIGNAL
5398     fprintf(stderr, "qemu: process signal %d\n", sig);
5399 #endif
5400     /* dequeue signal */
5401     q = k->first;
5402     k->first = q->next;
5403     if (!k->first)
5404         k->pending = 0;
5405 
5406     sig = gdb_handlesig (cpu_env, sig);
5407     if (!sig) {
5408         sa = NULL;
5409         handler = TARGET_SIG_IGN;
5410     } else {
5411         sa = &sigact_table[sig - 1];
5412         handler = sa->_sa_handler;
5413     }
5414 
5415     if (handler == TARGET_SIG_DFL) {
5416         /* default handler : ignore some signal. The other are job control or fatal */
5417         if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5418             kill(getpid(),SIGSTOP);
5419         } else if (sig != TARGET_SIGCHLD &&
5420                    sig != TARGET_SIGURG &&
5421                    sig != TARGET_SIGWINCH &&
5422                    sig != TARGET_SIGCONT) {
5423             force_sig(sig);
5424         }
5425     } else if (handler == TARGET_SIG_IGN) {
5426         /* ignore sig */
5427     } else if (handler == TARGET_SIG_ERR) {
5428         force_sig(sig);
5429     } else {
5430         /* compute the blocked signals during the handler execution */
5431         target_to_host_sigset(&set, &sa->sa_mask);
5432         /* SA_NODEFER indicates that the current signal should not be
5433            blocked during the handler */
5434         if (!(sa->sa_flags & TARGET_SA_NODEFER))
5435             sigaddset(&set, target_to_host_signal(sig));
5436 
5437         /* block signals in the handler using Linux */
5438         sigprocmask(SIG_BLOCK, &set, &old_set);
5439         /* save the previous blocked signal state to restore it at the
5440            end of the signal execution (see do_sigreturn) */
5441         host_to_target_sigset_internal(&target_old_set, &old_set);
5442 
5443         /* if the CPU is in VM86 mode, we restore the 32 bit values */
5444 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5445         {
5446             CPUX86State *env = cpu_env;
5447             if (env->eflags & VM_MASK)
5448                 save_v86_state(env);
5449         }
5450 #endif
5451         /* prepare the stack frame of the virtual CPU */
5452 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5453         /* These targets do not have traditional signals.  */
5454         setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5455 #else
5456         if (sa->sa_flags & TARGET_SA_SIGINFO)
5457             setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5458         else
5459             setup_frame(sig, sa, &target_old_set, cpu_env);
5460 #endif
5461 	if (sa->sa_flags & TARGET_SA_RESETHAND)
5462             sa->_sa_handler = TARGET_SIG_DFL;
5463     }
5464     if (q != &k->info)
5465         free_sigqueue(cpu_env, q);
5466 }
5467