sun4m: Pass SPARCCPU to cpu_kick_irq()
[qemu/agraf.git] / linux-user / signal.c
blob15bc4e8f62f8ad5a6b3c226e6d0335f3993fc23b
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
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>
29 #include "qemu.h"
30 #include "qemu-common.h"
31 #include "target_signal.h"
33 //#define DEBUG_SIGNAL
35 static struct target_sigaltstack target_sigaltstack_used = {
36 .ss_sp = 0,
37 .ss_size = 0,
38 .ss_flags = TARGET_SS_DISABLE,
41 static struct target_sigaction sigact_table[TARGET_NSIG];
43 static void host_signal_handler(int host_signum, siginfo_t *info,
44 void *puc);
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,
89 static uint8_t target_to_host_signal_table[_NSIG];
91 static inline int on_sig_stack(unsigned long sp)
93 return (sp - target_sigaltstack_used.ss_sp
94 < target_sigaltstack_used.ss_size);
97 static inline int sas_ss_flags(unsigned long sp)
99 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
100 : on_sig_stack(sp) ? SS_ONSTACK : 0);
103 int host_to_target_signal(int sig)
105 if (sig >= _NSIG)
106 return sig;
107 return host_to_target_signal_table[sig];
110 int target_to_host_signal(int sig)
112 if (sig >= _NSIG)
113 return sig;
114 return target_to_host_signal_table[sig];
117 static inline void target_sigemptyset(target_sigset_t *set)
119 memset(set, 0, sizeof(*set));
122 static inline void target_sigaddset(target_sigset_t *set, int signum)
124 signum--;
125 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126 set->sig[signum / TARGET_NSIG_BPW] |= mask;
129 static inline int target_sigismember(const target_sigset_t *set, int signum)
131 signum--;
132 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
133 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
136 static void host_to_target_sigset_internal(target_sigset_t *d,
137 const sigset_t *s)
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));
148 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
150 target_sigset_t d1;
151 int i;
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]);
158 static void target_to_host_sigset_internal(sigset_t *d,
159 const target_sigset_t *s)
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));
170 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
172 target_sigset_t s1;
173 int i;
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);
180 void host_to_target_old_sigset(abi_ulong *old_sigset,
181 const sigset_t *sigset)
183 target_sigset_t d;
184 host_to_target_sigset(&d, sigset);
185 *old_sigset = d.sig[0];
188 void target_to_host_old_sigset(sigset_t *sigset,
189 const abi_ulong *old_sigset)
191 target_sigset_t d;
192 int i;
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);
200 /* siginfo conversion */
202 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
203 const siginfo_t *info)
205 int sig;
206 sig = host_to_target_signal(info->si_signo);
207 tinfo->si_signo = sig;
208 tinfo->si_errno = 0;
209 tinfo->si_code = info->si_code;
210 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
211 sig == SIGBUS || sig == 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 == SIGIO) {
216 tinfo->_sifields._sigpoll._fd = info->si_fd;
217 } else if (sig >= TARGET_SIGRTMIN) {
218 tinfo->_sifields._rt._pid = info->si_pid;
219 tinfo->_sifields._rt._uid = info->si_uid;
220 /* XXX: potential problem if 64 bit */
221 tinfo->_sifields._rt._sigval.sival_ptr =
222 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
226 static void tswap_siginfo(target_siginfo_t *tinfo,
227 const target_siginfo_t *info)
229 int sig;
230 sig = info->si_signo;
231 tinfo->si_signo = tswap32(sig);
232 tinfo->si_errno = tswap32(info->si_errno);
233 tinfo->si_code = tswap32(info->si_code);
234 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
235 sig == SIGBUS || sig == SIGTRAP) {
236 tinfo->_sifields._sigfault._addr =
237 tswapal(info->_sifields._sigfault._addr);
238 } else if (sig == SIGIO) {
239 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
240 } else if (sig >= TARGET_SIGRTMIN) {
241 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
242 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
243 tinfo->_sifields._rt._sigval.sival_ptr =
244 tswapal(info->_sifields._rt._sigval.sival_ptr);
249 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
251 host_to_target_siginfo_noswap(tinfo, info);
252 tswap_siginfo(tinfo, tinfo);
255 /* XXX: we support only POSIX RT signals are used. */
256 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
257 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
259 info->si_signo = tswap32(tinfo->si_signo);
260 info->si_errno = tswap32(tinfo->si_errno);
261 info->si_code = tswap32(tinfo->si_code);
262 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
263 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
264 info->si_value.sival_ptr =
265 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
268 static int fatal_signal (int sig)
270 switch (sig) {
271 case TARGET_SIGCHLD:
272 case TARGET_SIGURG:
273 case TARGET_SIGWINCH:
274 /* Ignored by default. */
275 return 0;
276 case TARGET_SIGCONT:
277 case TARGET_SIGSTOP:
278 case TARGET_SIGTSTP:
279 case TARGET_SIGTTIN:
280 case TARGET_SIGTTOU:
281 /* Job control signals. */
282 return 0;
283 default:
284 return 1;
288 /* returns 1 if given signal should dump core if not handled */
289 static int core_dump_signal(int sig)
291 switch (sig) {
292 case TARGET_SIGABRT:
293 case TARGET_SIGFPE:
294 case TARGET_SIGILL:
295 case TARGET_SIGQUIT:
296 case TARGET_SIGSEGV:
297 case TARGET_SIGTRAP:
298 case TARGET_SIGBUS:
299 return (1);
300 default:
301 return (0);
305 void signal_init(void)
307 struct sigaction act;
308 struct sigaction oact;
309 int i, j;
310 int host_sig;
312 /* generate signal conversion tables */
313 for(i = 1; i < _NSIG; i++) {
314 if (host_to_target_signal_table[i] == 0)
315 host_to_target_signal_table[i] = i;
317 for(i = 1; i < _NSIG; i++) {
318 j = host_to_target_signal_table[i];
319 target_to_host_signal_table[j] = i;
322 /* set all host signal handlers. ALL signals are blocked during
323 the handlers to serialize them. */
324 memset(sigact_table, 0, sizeof(sigact_table));
326 sigfillset(&act.sa_mask);
327 act.sa_flags = SA_SIGINFO;
328 act.sa_sigaction = host_signal_handler;
329 for(i = 1; i <= TARGET_NSIG; i++) {
330 host_sig = target_to_host_signal(i);
331 sigaction(host_sig, NULL, &oact);
332 if (oact.sa_sigaction == (void *)SIG_IGN) {
333 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
334 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
335 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
337 /* If there's already a handler installed then something has
338 gone horribly wrong, so don't even try to handle that case. */
339 /* Install some handlers for our own use. We need at least
340 SIGSEGV and SIGBUS, to detect exceptions. We can not just
341 trap all signals because it affects syscall interrupt
342 behavior. But do trap all default-fatal signals. */
343 if (fatal_signal (i))
344 sigaction(host_sig, &act, NULL);
348 /* signal queue handling */
350 static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
352 TaskState *ts = env->opaque;
353 struct sigqueue *q = ts->first_free;
354 if (!q)
355 return NULL;
356 ts->first_free = q->next;
357 return q;
360 static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
362 TaskState *ts = env->opaque;
363 q->next = ts->first_free;
364 ts->first_free = q;
367 /* abort execution with signal */
368 static void QEMU_NORETURN force_sig(int target_sig)
370 TaskState *ts = (TaskState *)thread_env->opaque;
371 int host_sig, core_dumped = 0;
372 struct sigaction act;
373 host_sig = target_to_host_signal(target_sig);
374 gdb_signalled(thread_env, target_sig);
376 /* dump core if supported by target binary format */
377 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
378 stop_all_tasks();
379 core_dumped =
380 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
382 if (core_dumped) {
383 /* we already dumped the core of target process, we don't want
384 * a coredump of qemu itself */
385 struct rlimit nodump;
386 getrlimit(RLIMIT_CORE, &nodump);
387 nodump.rlim_cur=0;
388 setrlimit(RLIMIT_CORE, &nodump);
389 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
390 target_sig, strsignal(host_sig), "core dumped" );
393 /* The proper exit code for dying from an uncaught signal is
394 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
395 * a negative value. To get the proper exit code we need to
396 * actually die from an uncaught signal. Here the default signal
397 * handler is installed, we send ourself a signal and we wait for
398 * it to arrive. */
399 sigfillset(&act.sa_mask);
400 act.sa_handler = SIG_DFL;
401 sigaction(host_sig, &act, NULL);
403 /* For some reason raise(host_sig) doesn't send the signal when
404 * statically linked on x86-64. */
405 kill(getpid(), host_sig);
407 /* Make sure the signal isn't masked (just reuse the mask inside
408 of act) */
409 sigdelset(&act.sa_mask, host_sig);
410 sigsuspend(&act.sa_mask);
412 /* unreachable */
413 abort();
416 /* queue a signal so that it will be send to the virtual CPU as soon
417 as possible */
418 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
420 TaskState *ts = env->opaque;
421 struct emulated_sigtable *k;
422 struct sigqueue *q, **pq;
423 abi_ulong handler;
424 int queue;
426 #if defined(DEBUG_SIGNAL)
427 fprintf(stderr, "queue_signal: sig=%d\n",
428 sig);
429 #endif
430 k = &ts->sigtab[sig - 1];
431 queue = gdb_queuesig ();
432 handler = sigact_table[sig - 1]._sa_handler;
433 if (!queue && handler == TARGET_SIG_DFL) {
434 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
435 kill(getpid(),SIGSTOP);
436 return 0;
437 } else
438 /* default handler : ignore some signal. The other are fatal */
439 if (sig != TARGET_SIGCHLD &&
440 sig != TARGET_SIGURG &&
441 sig != TARGET_SIGWINCH &&
442 sig != TARGET_SIGCONT) {
443 force_sig(sig);
444 } else {
445 return 0; /* indicate ignored */
447 } else if (!queue && handler == TARGET_SIG_IGN) {
448 /* ignore signal */
449 return 0;
450 } else if (!queue && handler == TARGET_SIG_ERR) {
451 force_sig(sig);
452 } else {
453 pq = &k->first;
454 if (sig < TARGET_SIGRTMIN) {
455 /* if non real time signal, we queue exactly one signal */
456 if (!k->pending)
457 q = &k->info;
458 else
459 return 0;
460 } else {
461 if (!k->pending) {
462 /* first signal */
463 q = &k->info;
464 } else {
465 q = alloc_sigqueue(env);
466 if (!q)
467 return -EAGAIN;
468 while (*pq != NULL)
469 pq = &(*pq)->next;
472 *pq = q;
473 q->info = *info;
474 q->next = NULL;
475 k->pending = 1;
476 /* signal that a new signal is pending */
477 ts->signal_pending = 1;
478 return 1; /* indicates that the signal was queued */
482 static void host_signal_handler(int host_signum, siginfo_t *info,
483 void *puc)
485 int sig;
486 target_siginfo_t tinfo;
488 /* the CPU emulator uses some host signals to detect exceptions,
489 we forward to it some signals */
490 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
491 && info->si_code > 0) {
492 if (cpu_signal_handler(host_signum, info, puc))
493 return;
496 /* get target signal number */
497 sig = host_to_target_signal(host_signum);
498 if (sig < 1 || sig > TARGET_NSIG)
499 return;
500 #if defined(DEBUG_SIGNAL)
501 fprintf(stderr, "qemu: got signal %d\n", sig);
502 #endif
503 host_to_target_siginfo_noswap(&tinfo, info);
504 if (queue_signal(thread_env, sig, &tinfo) == 1) {
505 /* interrupt the virtual CPU as soon as possible */
506 cpu_exit(thread_env);
510 /* do_sigaltstack() returns target values and errnos. */
511 /* compare linux/kernel/signal.c:do_sigaltstack() */
512 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
514 int ret;
515 struct target_sigaltstack oss;
517 /* XXX: test errors */
518 if(uoss_addr)
520 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
521 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
522 __put_user(sas_ss_flags(sp), &oss.ss_flags);
525 if(uss_addr)
527 struct target_sigaltstack *uss;
528 struct target_sigaltstack ss;
530 ret = -TARGET_EFAULT;
531 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
532 || __get_user(ss.ss_sp, &uss->ss_sp)
533 || __get_user(ss.ss_size, &uss->ss_size)
534 || __get_user(ss.ss_flags, &uss->ss_flags))
535 goto out;
536 unlock_user_struct(uss, uss_addr, 0);
538 ret = -TARGET_EPERM;
539 if (on_sig_stack(sp))
540 goto out;
542 ret = -TARGET_EINVAL;
543 if (ss.ss_flags != TARGET_SS_DISABLE
544 && ss.ss_flags != TARGET_SS_ONSTACK
545 && ss.ss_flags != 0)
546 goto out;
548 if (ss.ss_flags == TARGET_SS_DISABLE) {
549 ss.ss_size = 0;
550 ss.ss_sp = 0;
551 } else {
552 ret = -TARGET_ENOMEM;
553 if (ss.ss_size < MINSIGSTKSZ)
554 goto out;
557 target_sigaltstack_used.ss_sp = ss.ss_sp;
558 target_sigaltstack_used.ss_size = ss.ss_size;
561 if (uoss_addr) {
562 ret = -TARGET_EFAULT;
563 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
564 goto out;
567 ret = 0;
568 out:
569 return ret;
572 /* do_sigaction() return host values and errnos */
573 int do_sigaction(int sig, const struct target_sigaction *act,
574 struct target_sigaction *oact)
576 struct target_sigaction *k;
577 struct sigaction act1;
578 int host_sig;
579 int ret = 0;
581 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
582 return -EINVAL;
583 k = &sigact_table[sig - 1];
584 #if defined(DEBUG_SIGNAL)
585 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
586 sig, act, oact);
587 #endif
588 if (oact) {
589 oact->_sa_handler = tswapal(k->_sa_handler);
590 #if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
591 oact->sa_flags = bswap32(k->sa_flags);
592 #else
593 oact->sa_flags = tswapal(k->sa_flags);
594 #endif
595 #if !defined(TARGET_MIPS)
596 oact->sa_restorer = tswapal(k->sa_restorer);
597 #endif
598 oact->sa_mask = k->sa_mask;
600 if (act) {
601 /* FIXME: This is not threadsafe. */
602 k->_sa_handler = tswapal(act->_sa_handler);
603 #if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
604 k->sa_flags = bswap32(act->sa_flags);
605 #else
606 k->sa_flags = tswapal(act->sa_flags);
607 #endif
608 #if !defined(TARGET_MIPS)
609 k->sa_restorer = tswapal(act->sa_restorer);
610 #endif
611 k->sa_mask = act->sa_mask;
613 /* we update the host linux signal state */
614 host_sig = target_to_host_signal(sig);
615 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
616 sigfillset(&act1.sa_mask);
617 act1.sa_flags = SA_SIGINFO;
618 if (k->sa_flags & TARGET_SA_RESTART)
619 act1.sa_flags |= SA_RESTART;
620 /* NOTE: it is important to update the host kernel signal
621 ignore state to avoid getting unexpected interrupted
622 syscalls */
623 if (k->_sa_handler == TARGET_SIG_IGN) {
624 act1.sa_sigaction = (void *)SIG_IGN;
625 } else if (k->_sa_handler == TARGET_SIG_DFL) {
626 if (fatal_signal (sig))
627 act1.sa_sigaction = host_signal_handler;
628 else
629 act1.sa_sigaction = (void *)SIG_DFL;
630 } else {
631 act1.sa_sigaction = host_signal_handler;
633 ret = sigaction(host_sig, &act1, NULL);
636 return ret;
639 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
640 const target_siginfo_t *info)
642 tswap_siginfo(tinfo, info);
643 return 0;
646 static inline int current_exec_domain_sig(int sig)
648 return /* current->exec_domain && current->exec_domain->signal_invmap
649 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
652 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
654 /* from the Linux kernel */
656 struct target_fpreg {
657 uint16_t significand[4];
658 uint16_t exponent;
661 struct target_fpxreg {
662 uint16_t significand[4];
663 uint16_t exponent;
664 uint16_t padding[3];
667 struct target_xmmreg {
668 abi_ulong element[4];
671 struct target_fpstate {
672 /* Regular FPU environment */
673 abi_ulong cw;
674 abi_ulong sw;
675 abi_ulong tag;
676 abi_ulong ipoff;
677 abi_ulong cssel;
678 abi_ulong dataoff;
679 abi_ulong datasel;
680 struct target_fpreg _st[8];
681 uint16_t status;
682 uint16_t magic; /* 0xffff = regular FPU data only */
684 /* FXSR FPU environment */
685 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
686 abi_ulong mxcsr;
687 abi_ulong reserved;
688 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
689 struct target_xmmreg _xmm[8];
690 abi_ulong padding[56];
693 #define X86_FXSR_MAGIC 0x0000
695 struct target_sigcontext {
696 uint16_t gs, __gsh;
697 uint16_t fs, __fsh;
698 uint16_t es, __esh;
699 uint16_t ds, __dsh;
700 abi_ulong edi;
701 abi_ulong esi;
702 abi_ulong ebp;
703 abi_ulong esp;
704 abi_ulong ebx;
705 abi_ulong edx;
706 abi_ulong ecx;
707 abi_ulong eax;
708 abi_ulong trapno;
709 abi_ulong err;
710 abi_ulong eip;
711 uint16_t cs, __csh;
712 abi_ulong eflags;
713 abi_ulong esp_at_signal;
714 uint16_t ss, __ssh;
715 abi_ulong fpstate; /* pointer */
716 abi_ulong oldmask;
717 abi_ulong cr2;
720 struct target_ucontext {
721 abi_ulong tuc_flags;
722 abi_ulong tuc_link;
723 target_stack_t tuc_stack;
724 struct target_sigcontext tuc_mcontext;
725 target_sigset_t tuc_sigmask; /* mask last for extensibility */
728 struct sigframe
730 abi_ulong pretcode;
731 int sig;
732 struct target_sigcontext sc;
733 struct target_fpstate fpstate;
734 abi_ulong extramask[TARGET_NSIG_WORDS-1];
735 char retcode[8];
738 struct rt_sigframe
740 abi_ulong pretcode;
741 int sig;
742 abi_ulong pinfo;
743 abi_ulong puc;
744 struct target_siginfo info;
745 struct target_ucontext uc;
746 struct target_fpstate fpstate;
747 char retcode[8];
751 * Set up a signal frame.
754 /* XXX: save x87 state */
755 static int
756 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
757 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
759 int err = 0;
760 uint16_t magic;
762 /* already locked in setup_frame() */
763 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
764 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
765 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
766 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
767 err |= __put_user(env->regs[R_EDI], &sc->edi);
768 err |= __put_user(env->regs[R_ESI], &sc->esi);
769 err |= __put_user(env->regs[R_EBP], &sc->ebp);
770 err |= __put_user(env->regs[R_ESP], &sc->esp);
771 err |= __put_user(env->regs[R_EBX], &sc->ebx);
772 err |= __put_user(env->regs[R_EDX], &sc->edx);
773 err |= __put_user(env->regs[R_ECX], &sc->ecx);
774 err |= __put_user(env->regs[R_EAX], &sc->eax);
775 err |= __put_user(env->exception_index, &sc->trapno);
776 err |= __put_user(env->error_code, &sc->err);
777 err |= __put_user(env->eip, &sc->eip);
778 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
779 err |= __put_user(env->eflags, &sc->eflags);
780 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
781 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
783 cpu_x86_fsave(env, fpstate_addr, 1);
784 fpstate->status = fpstate->sw;
785 magic = 0xffff;
786 err |= __put_user(magic, &fpstate->magic);
787 err |= __put_user(fpstate_addr, &sc->fpstate);
789 /* non-iBCS2 extensions.. */
790 err |= __put_user(mask, &sc->oldmask);
791 err |= __put_user(env->cr[2], &sc->cr2);
792 return err;
796 * Determine which stack to use..
799 static inline abi_ulong
800 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
802 unsigned long esp;
804 /* Default to using normal stack */
805 esp = env->regs[R_ESP];
806 /* This is the X/Open sanctioned signal stack switching. */
807 if (ka->sa_flags & TARGET_SA_ONSTACK) {
808 if (sas_ss_flags(esp) == 0)
809 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
812 /* This is the legacy signal stack switching. */
813 else
814 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
815 !(ka->sa_flags & TARGET_SA_RESTORER) &&
816 ka->sa_restorer) {
817 esp = (unsigned long) ka->sa_restorer;
819 return (esp - frame_size) & -8ul;
822 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
823 static void setup_frame(int sig, struct target_sigaction *ka,
824 target_sigset_t *set, CPUX86State *env)
826 abi_ulong frame_addr;
827 struct sigframe *frame;
828 int i, err = 0;
830 frame_addr = get_sigframe(ka, env, sizeof(*frame));
832 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
833 goto give_sigsegv;
835 err |= __put_user(current_exec_domain_sig(sig),
836 &frame->sig);
837 if (err)
838 goto give_sigsegv;
840 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
841 frame_addr + offsetof(struct sigframe, fpstate));
842 if (err)
843 goto give_sigsegv;
845 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
846 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
847 goto give_sigsegv;
850 /* Set up to return from userspace. If provided, use a stub
851 already in userspace. */
852 if (ka->sa_flags & TARGET_SA_RESTORER) {
853 err |= __put_user(ka->sa_restorer, &frame->pretcode);
854 } else {
855 uint16_t val16;
856 abi_ulong retcode_addr;
857 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
858 err |= __put_user(retcode_addr, &frame->pretcode);
859 /* This is popl %eax ; movl $,%eax ; int $0x80 */
860 val16 = 0xb858;
861 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
862 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
863 val16 = 0x80cd;
864 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
867 if (err)
868 goto give_sigsegv;
870 /* Set up registers for signal handler */
871 env->regs[R_ESP] = frame_addr;
872 env->eip = ka->_sa_handler;
874 cpu_x86_load_seg(env, R_DS, __USER_DS);
875 cpu_x86_load_seg(env, R_ES, __USER_DS);
876 cpu_x86_load_seg(env, R_SS, __USER_DS);
877 cpu_x86_load_seg(env, R_CS, __USER_CS);
878 env->eflags &= ~TF_MASK;
880 unlock_user_struct(frame, frame_addr, 1);
882 return;
884 give_sigsegv:
885 unlock_user_struct(frame, frame_addr, 1);
886 if (sig == TARGET_SIGSEGV)
887 ka->_sa_handler = TARGET_SIG_DFL;
888 force_sig(TARGET_SIGSEGV /* , current */);
891 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
892 static void setup_rt_frame(int sig, struct target_sigaction *ka,
893 target_siginfo_t *info,
894 target_sigset_t *set, CPUX86State *env)
896 abi_ulong frame_addr, addr;
897 struct rt_sigframe *frame;
898 int i, err = 0;
900 frame_addr = get_sigframe(ka, env, sizeof(*frame));
902 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
903 goto give_sigsegv;
905 err |= __put_user(current_exec_domain_sig(sig),
906 &frame->sig);
907 addr = frame_addr + offsetof(struct rt_sigframe, info);
908 err |= __put_user(addr, &frame->pinfo);
909 addr = frame_addr + offsetof(struct rt_sigframe, uc);
910 err |= __put_user(addr, &frame->puc);
911 err |= copy_siginfo_to_user(&frame->info, info);
912 if (err)
913 goto give_sigsegv;
915 /* Create the ucontext. */
916 err |= __put_user(0, &frame->uc.tuc_flags);
917 err |= __put_user(0, &frame->uc.tuc_link);
918 err |= __put_user(target_sigaltstack_used.ss_sp,
919 &frame->uc.tuc_stack.ss_sp);
920 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
921 &frame->uc.tuc_stack.ss_flags);
922 err |= __put_user(target_sigaltstack_used.ss_size,
923 &frame->uc.tuc_stack.ss_size);
924 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
925 env, set->sig[0],
926 frame_addr + offsetof(struct rt_sigframe, fpstate));
927 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
928 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
929 goto give_sigsegv;
932 /* Set up to return from userspace. If provided, use a stub
933 already in userspace. */
934 if (ka->sa_flags & TARGET_SA_RESTORER) {
935 err |= __put_user(ka->sa_restorer, &frame->pretcode);
936 } else {
937 uint16_t val16;
938 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
939 err |= __put_user(addr, &frame->pretcode);
940 /* This is movl $,%eax ; int $0x80 */
941 err |= __put_user(0xb8, (char *)(frame->retcode+0));
942 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
943 val16 = 0x80cd;
944 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
947 if (err)
948 goto give_sigsegv;
950 /* Set up registers for signal handler */
951 env->regs[R_ESP] = frame_addr;
952 env->eip = ka->_sa_handler;
954 cpu_x86_load_seg(env, R_DS, __USER_DS);
955 cpu_x86_load_seg(env, R_ES, __USER_DS);
956 cpu_x86_load_seg(env, R_SS, __USER_DS);
957 cpu_x86_load_seg(env, R_CS, __USER_CS);
958 env->eflags &= ~TF_MASK;
960 unlock_user_struct(frame, frame_addr, 1);
962 return;
964 give_sigsegv:
965 unlock_user_struct(frame, frame_addr, 1);
966 if (sig == TARGET_SIGSEGV)
967 ka->_sa_handler = TARGET_SIG_DFL;
968 force_sig(TARGET_SIGSEGV /* , current */);
971 static int
972 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
974 unsigned int err = 0;
975 abi_ulong fpstate_addr;
976 unsigned int tmpflags;
978 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
979 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
980 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
981 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
983 env->regs[R_EDI] = tswapl(sc->edi);
984 env->regs[R_ESI] = tswapl(sc->esi);
985 env->regs[R_EBP] = tswapl(sc->ebp);
986 env->regs[R_ESP] = tswapl(sc->esp);
987 env->regs[R_EBX] = tswapl(sc->ebx);
988 env->regs[R_EDX] = tswapl(sc->edx);
989 env->regs[R_ECX] = tswapl(sc->ecx);
990 env->eip = tswapl(sc->eip);
992 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
993 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
995 tmpflags = tswapl(sc->eflags);
996 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
997 // regs->orig_eax = -1; /* disable syscall checks */
999 fpstate_addr = tswapl(sc->fpstate);
1000 if (fpstate_addr != 0) {
1001 if (!access_ok(VERIFY_READ, fpstate_addr,
1002 sizeof(struct target_fpstate)))
1003 goto badframe;
1004 cpu_x86_frstor(env, fpstate_addr, 1);
1007 *peax = tswapl(sc->eax);
1008 return err;
1009 badframe:
1010 return 1;
1013 long do_sigreturn(CPUX86State *env)
1015 struct sigframe *frame;
1016 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1017 target_sigset_t target_set;
1018 sigset_t set;
1019 int eax, i;
1021 #if defined(DEBUG_SIGNAL)
1022 fprintf(stderr, "do_sigreturn\n");
1023 #endif
1024 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1025 goto badframe;
1026 /* set blocked signals */
1027 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1028 goto badframe;
1029 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1030 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1031 goto badframe;
1034 target_to_host_sigset_internal(&set, &target_set);
1035 sigprocmask(SIG_SETMASK, &set, NULL);
1037 /* restore registers */
1038 if (restore_sigcontext(env, &frame->sc, &eax))
1039 goto badframe;
1040 unlock_user_struct(frame, frame_addr, 0);
1041 return eax;
1043 badframe:
1044 unlock_user_struct(frame, frame_addr, 0);
1045 force_sig(TARGET_SIGSEGV);
1046 return 0;
1049 long do_rt_sigreturn(CPUX86State *env)
1051 abi_ulong frame_addr;
1052 struct rt_sigframe *frame;
1053 sigset_t set;
1054 int eax;
1056 frame_addr = env->regs[R_ESP] - 4;
1057 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1058 goto badframe;
1059 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1060 sigprocmask(SIG_SETMASK, &set, NULL);
1062 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1063 goto badframe;
1065 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1066 get_sp_from_cpustate(env)) == -EFAULT)
1067 goto badframe;
1069 unlock_user_struct(frame, frame_addr, 0);
1070 return eax;
1072 badframe:
1073 unlock_user_struct(frame, frame_addr, 0);
1074 force_sig(TARGET_SIGSEGV);
1075 return 0;
1078 #elif defined(TARGET_ARM)
1080 struct target_sigcontext {
1081 abi_ulong trap_no;
1082 abi_ulong error_code;
1083 abi_ulong oldmask;
1084 abi_ulong arm_r0;
1085 abi_ulong arm_r1;
1086 abi_ulong arm_r2;
1087 abi_ulong arm_r3;
1088 abi_ulong arm_r4;
1089 abi_ulong arm_r5;
1090 abi_ulong arm_r6;
1091 abi_ulong arm_r7;
1092 abi_ulong arm_r8;
1093 abi_ulong arm_r9;
1094 abi_ulong arm_r10;
1095 abi_ulong arm_fp;
1096 abi_ulong arm_ip;
1097 abi_ulong arm_sp;
1098 abi_ulong arm_lr;
1099 abi_ulong arm_pc;
1100 abi_ulong arm_cpsr;
1101 abi_ulong fault_address;
1104 struct target_ucontext_v1 {
1105 abi_ulong tuc_flags;
1106 abi_ulong tuc_link;
1107 target_stack_t tuc_stack;
1108 struct target_sigcontext tuc_mcontext;
1109 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1112 struct target_ucontext_v2 {
1113 abi_ulong tuc_flags;
1114 abi_ulong tuc_link;
1115 target_stack_t tuc_stack;
1116 struct target_sigcontext tuc_mcontext;
1117 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1118 char __unused[128 - sizeof(target_sigset_t)];
1119 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1122 struct target_user_vfp {
1123 uint64_t fpregs[32];
1124 abi_ulong fpscr;
1127 struct target_user_vfp_exc {
1128 abi_ulong fpexc;
1129 abi_ulong fpinst;
1130 abi_ulong fpinst2;
1133 struct target_vfp_sigframe {
1134 abi_ulong magic;
1135 abi_ulong size;
1136 struct target_user_vfp ufp;
1137 struct target_user_vfp_exc ufp_exc;
1138 } __attribute__((__aligned__(8)));
1140 struct target_iwmmxt_sigframe {
1141 abi_ulong magic;
1142 abi_ulong size;
1143 uint64_t regs[16];
1144 /* Note that not all the coprocessor control registers are stored here */
1145 uint32_t wcssf;
1146 uint32_t wcasf;
1147 uint32_t wcgr0;
1148 uint32_t wcgr1;
1149 uint32_t wcgr2;
1150 uint32_t wcgr3;
1151 } __attribute__((__aligned__(8)));
1153 #define TARGET_VFP_MAGIC 0x56465001
1154 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1156 struct sigframe_v1
1158 struct target_sigcontext sc;
1159 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1160 abi_ulong retcode;
1163 struct sigframe_v2
1165 struct target_ucontext_v2 uc;
1166 abi_ulong retcode;
1169 struct rt_sigframe_v1
1171 abi_ulong pinfo;
1172 abi_ulong puc;
1173 struct target_siginfo info;
1174 struct target_ucontext_v1 uc;
1175 abi_ulong retcode;
1178 struct rt_sigframe_v2
1180 struct target_siginfo info;
1181 struct target_ucontext_v2 uc;
1182 abi_ulong retcode;
1185 #define TARGET_CONFIG_CPU_32 1
1188 * For ARM syscalls, we encode the syscall number into the instruction.
1190 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1191 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1194 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1195 * need two 16-bit instructions.
1197 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1198 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1200 static const abi_ulong retcodes[4] = {
1201 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1202 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1206 #define __get_user_error(x,p,e) __get_user(x, p)
1208 static inline int valid_user_regs(CPUARMState *regs)
1210 return 1;
1213 static void
1214 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1215 CPUARMState *env, abi_ulong mask)
1217 __put_user(env->regs[0], &sc->arm_r0);
1218 __put_user(env->regs[1], &sc->arm_r1);
1219 __put_user(env->regs[2], &sc->arm_r2);
1220 __put_user(env->regs[3], &sc->arm_r3);
1221 __put_user(env->regs[4], &sc->arm_r4);
1222 __put_user(env->regs[5], &sc->arm_r5);
1223 __put_user(env->regs[6], &sc->arm_r6);
1224 __put_user(env->regs[7], &sc->arm_r7);
1225 __put_user(env->regs[8], &sc->arm_r8);
1226 __put_user(env->regs[9], &sc->arm_r9);
1227 __put_user(env->regs[10], &sc->arm_r10);
1228 __put_user(env->regs[11], &sc->arm_fp);
1229 __put_user(env->regs[12], &sc->arm_ip);
1230 __put_user(env->regs[13], &sc->arm_sp);
1231 __put_user(env->regs[14], &sc->arm_lr);
1232 __put_user(env->regs[15], &sc->arm_pc);
1233 #ifdef TARGET_CONFIG_CPU_32
1234 __put_user(cpsr_read(env), &sc->arm_cpsr);
1235 #endif
1237 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1238 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1239 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1240 __put_user(mask, &sc->oldmask);
1243 static inline abi_ulong
1244 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1246 unsigned long sp = regs->regs[13];
1249 * This is the X/Open sanctioned signal stack switching.
1251 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1252 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1254 * ATPCS B01 mandates 8-byte alignment
1256 return (sp - framesize) & ~7;
1259 static int
1260 setup_return(CPUARMState *env, struct target_sigaction *ka,
1261 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1263 abi_ulong handler = ka->_sa_handler;
1264 abi_ulong retcode;
1265 int thumb = handler & 1;
1266 uint32_t cpsr = cpsr_read(env);
1268 cpsr &= ~CPSR_IT;
1269 if (thumb) {
1270 cpsr |= CPSR_T;
1271 } else {
1272 cpsr &= ~CPSR_T;
1275 if (ka->sa_flags & TARGET_SA_RESTORER) {
1276 retcode = ka->sa_restorer;
1277 } else {
1278 unsigned int idx = thumb;
1280 if (ka->sa_flags & TARGET_SA_SIGINFO)
1281 idx += 2;
1283 if (__put_user(retcodes[idx], rc))
1284 return 1;
1286 retcode = rc_addr + thumb;
1289 env->regs[0] = usig;
1290 env->regs[13] = frame_addr;
1291 env->regs[14] = retcode;
1292 env->regs[15] = handler & (thumb ? ~1 : ~3);
1293 cpsr_write(env, cpsr, 0xffffffff);
1295 return 0;
1298 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1300 int i;
1301 struct target_vfp_sigframe *vfpframe;
1302 vfpframe = (struct target_vfp_sigframe *)regspace;
1303 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1304 __put_user(sizeof(*vfpframe), &vfpframe->size);
1305 for (i = 0; i < 32; i++) {
1306 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1308 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1309 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1310 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1311 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1312 return (abi_ulong*)(vfpframe+1);
1315 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1316 CPUARMState *env)
1318 int i;
1319 struct target_iwmmxt_sigframe *iwmmxtframe;
1320 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1321 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1322 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1323 for (i = 0; i < 16; i++) {
1324 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1326 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1327 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1328 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1329 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1330 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1331 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1332 return (abi_ulong*)(iwmmxtframe+1);
1335 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1336 target_sigset_t *set, CPUARMState *env)
1338 struct target_sigaltstack stack;
1339 int i;
1340 abi_ulong *regspace;
1342 /* Clear all the bits of the ucontext we don't use. */
1343 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1345 memset(&stack, 0, sizeof(stack));
1346 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1347 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1348 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1349 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1351 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1352 /* Save coprocessor signal frame. */
1353 regspace = uc->tuc_regspace;
1354 if (arm_feature(env, ARM_FEATURE_VFP)) {
1355 regspace = setup_sigframe_v2_vfp(regspace, env);
1357 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1358 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1361 /* Write terminating magic word */
1362 __put_user(0, regspace);
1364 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1365 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1369 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1370 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1371 target_sigset_t *set, CPUARMState *regs)
1373 struct sigframe_v1 *frame;
1374 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1375 int i;
1377 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1378 return;
1380 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1382 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1383 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1384 goto end;
1387 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1388 frame_addr + offsetof(struct sigframe_v1, retcode));
1390 end:
1391 unlock_user_struct(frame, frame_addr, 1);
1394 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1395 target_sigset_t *set, CPUARMState *regs)
1397 struct sigframe_v2 *frame;
1398 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1400 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1401 return;
1403 setup_sigframe_v2(&frame->uc, set, regs);
1405 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1406 frame_addr + offsetof(struct sigframe_v2, retcode));
1408 unlock_user_struct(frame, frame_addr, 1);
1411 static void setup_frame(int usig, struct target_sigaction *ka,
1412 target_sigset_t *set, CPUARMState *regs)
1414 if (get_osversion() >= 0x020612) {
1415 setup_frame_v2(usig, ka, set, regs);
1416 } else {
1417 setup_frame_v1(usig, ka, set, regs);
1421 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1422 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1423 target_siginfo_t *info,
1424 target_sigset_t *set, CPUARMState *env)
1426 struct rt_sigframe_v1 *frame;
1427 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1428 struct target_sigaltstack stack;
1429 int i;
1430 abi_ulong info_addr, uc_addr;
1432 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1433 return /* 1 */;
1435 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1436 __put_user(info_addr, &frame->pinfo);
1437 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1438 __put_user(uc_addr, &frame->puc);
1439 copy_siginfo_to_user(&frame->info, info);
1441 /* Clear all the bits of the ucontext we don't use. */
1442 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1444 memset(&stack, 0, sizeof(stack));
1445 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1446 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1447 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1448 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1450 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1451 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1452 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1453 goto end;
1456 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1457 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1459 env->regs[1] = info_addr;
1460 env->regs[2] = uc_addr;
1462 end:
1463 unlock_user_struct(frame, frame_addr, 1);
1466 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1467 target_siginfo_t *info,
1468 target_sigset_t *set, CPUARMState *env)
1470 struct rt_sigframe_v2 *frame;
1471 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1472 abi_ulong info_addr, uc_addr;
1474 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1475 return /* 1 */;
1477 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1478 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1479 copy_siginfo_to_user(&frame->info, info);
1481 setup_sigframe_v2(&frame->uc, set, env);
1483 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1484 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1486 env->regs[1] = info_addr;
1487 env->regs[2] = uc_addr;
1489 unlock_user_struct(frame, frame_addr, 1);
1492 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1493 target_siginfo_t *info,
1494 target_sigset_t *set, CPUARMState *env)
1496 if (get_osversion() >= 0x020612) {
1497 setup_rt_frame_v2(usig, ka, info, set, env);
1498 } else {
1499 setup_rt_frame_v1(usig, ka, info, set, env);
1503 static int
1504 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1506 int err = 0;
1507 uint32_t cpsr;
1509 __get_user_error(env->regs[0], &sc->arm_r0, err);
1510 __get_user_error(env->regs[1], &sc->arm_r1, err);
1511 __get_user_error(env->regs[2], &sc->arm_r2, err);
1512 __get_user_error(env->regs[3], &sc->arm_r3, err);
1513 __get_user_error(env->regs[4], &sc->arm_r4, err);
1514 __get_user_error(env->regs[5], &sc->arm_r5, err);
1515 __get_user_error(env->regs[6], &sc->arm_r6, err);
1516 __get_user_error(env->regs[7], &sc->arm_r7, err);
1517 __get_user_error(env->regs[8], &sc->arm_r8, err);
1518 __get_user_error(env->regs[9], &sc->arm_r9, err);
1519 __get_user_error(env->regs[10], &sc->arm_r10, err);
1520 __get_user_error(env->regs[11], &sc->arm_fp, err);
1521 __get_user_error(env->regs[12], &sc->arm_ip, err);
1522 __get_user_error(env->regs[13], &sc->arm_sp, err);
1523 __get_user_error(env->regs[14], &sc->arm_lr, err);
1524 __get_user_error(env->regs[15], &sc->arm_pc, err);
1525 #ifdef TARGET_CONFIG_CPU_32
1526 __get_user_error(cpsr, &sc->arm_cpsr, err);
1527 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1528 #endif
1530 err |= !valid_user_regs(env);
1532 return err;
1535 static long do_sigreturn_v1(CPUARMState *env)
1537 abi_ulong frame_addr;
1538 struct sigframe_v1 *frame;
1539 target_sigset_t set;
1540 sigset_t host_set;
1541 int i;
1544 * Since we stacked the signal on a 64-bit boundary,
1545 * then 'sp' should be word aligned here. If it's
1546 * not, then the user is trying to mess with us.
1548 if (env->regs[13] & 7)
1549 goto badframe;
1551 frame_addr = env->regs[13];
1552 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1553 goto badframe;
1555 if (__get_user(set.sig[0], &frame->sc.oldmask))
1556 goto badframe;
1557 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1558 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1559 goto badframe;
1562 target_to_host_sigset_internal(&host_set, &set);
1563 sigprocmask(SIG_SETMASK, &host_set, NULL);
1565 if (restore_sigcontext(env, &frame->sc))
1566 goto badframe;
1568 #if 0
1569 /* Send SIGTRAP if we're single-stepping */
1570 if (ptrace_cancel_bpt(current))
1571 send_sig(SIGTRAP, current, 1);
1572 #endif
1573 unlock_user_struct(frame, frame_addr, 0);
1574 return env->regs[0];
1576 badframe:
1577 unlock_user_struct(frame, frame_addr, 0);
1578 force_sig(TARGET_SIGSEGV /* , current */);
1579 return 0;
1582 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1584 int i;
1585 abi_ulong magic, sz;
1586 uint32_t fpscr, fpexc;
1587 struct target_vfp_sigframe *vfpframe;
1588 vfpframe = (struct target_vfp_sigframe *)regspace;
1590 __get_user(magic, &vfpframe->magic);
1591 __get_user(sz, &vfpframe->size);
1592 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1593 return 0;
1595 for (i = 0; i < 32; i++) {
1596 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1598 __get_user(fpscr, &vfpframe->ufp.fpscr);
1599 vfp_set_fpscr(env, fpscr);
1600 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1601 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1602 * and the exception flag is cleared
1604 fpexc |= (1 << 30);
1605 fpexc &= ~((1 << 31) | (1 << 28));
1606 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1607 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1608 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1609 return (abi_ulong*)(vfpframe + 1);
1612 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1613 abi_ulong *regspace)
1615 int i;
1616 abi_ulong magic, sz;
1617 struct target_iwmmxt_sigframe *iwmmxtframe;
1618 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1620 __get_user(magic, &iwmmxtframe->magic);
1621 __get_user(sz, &iwmmxtframe->size);
1622 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1623 return 0;
1625 for (i = 0; i < 16; i++) {
1626 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1628 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1629 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1630 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1631 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1632 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1633 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1634 return (abi_ulong*)(iwmmxtframe + 1);
1637 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1638 struct target_ucontext_v2 *uc)
1640 sigset_t host_set;
1641 abi_ulong *regspace;
1643 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1644 sigprocmask(SIG_SETMASK, &host_set, NULL);
1646 if (restore_sigcontext(env, &uc->tuc_mcontext))
1647 return 1;
1649 /* Restore coprocessor signal frame */
1650 regspace = uc->tuc_regspace;
1651 if (arm_feature(env, ARM_FEATURE_VFP)) {
1652 regspace = restore_sigframe_v2_vfp(env, regspace);
1653 if (!regspace) {
1654 return 1;
1657 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1658 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1659 if (!regspace) {
1660 return 1;
1664 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1665 return 1;
1667 #if 0
1668 /* Send SIGTRAP if we're single-stepping */
1669 if (ptrace_cancel_bpt(current))
1670 send_sig(SIGTRAP, current, 1);
1671 #endif
1673 return 0;
1676 static long do_sigreturn_v2(CPUARMState *env)
1678 abi_ulong frame_addr;
1679 struct sigframe_v2 *frame;
1682 * Since we stacked the signal on a 64-bit boundary,
1683 * then 'sp' should be word aligned here. If it's
1684 * not, then the user is trying to mess with us.
1686 if (env->regs[13] & 7)
1687 goto badframe;
1689 frame_addr = env->regs[13];
1690 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1691 goto badframe;
1693 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1694 goto badframe;
1696 unlock_user_struct(frame, frame_addr, 0);
1697 return env->regs[0];
1699 badframe:
1700 unlock_user_struct(frame, frame_addr, 0);
1701 force_sig(TARGET_SIGSEGV /* , current */);
1702 return 0;
1705 long do_sigreturn(CPUARMState *env)
1707 if (get_osversion() >= 0x020612) {
1708 return do_sigreturn_v2(env);
1709 } else {
1710 return do_sigreturn_v1(env);
1714 static long do_rt_sigreturn_v1(CPUARMState *env)
1716 abi_ulong frame_addr;
1717 struct rt_sigframe_v1 *frame;
1718 sigset_t host_set;
1721 * Since we stacked the signal on a 64-bit boundary,
1722 * then 'sp' should be word aligned here. If it's
1723 * not, then the user is trying to mess with us.
1725 if (env->regs[13] & 7)
1726 goto badframe;
1728 frame_addr = env->regs[13];
1729 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1730 goto badframe;
1732 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1733 sigprocmask(SIG_SETMASK, &host_set, NULL);
1735 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1736 goto badframe;
1738 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1739 goto badframe;
1741 #if 0
1742 /* Send SIGTRAP if we're single-stepping */
1743 if (ptrace_cancel_bpt(current))
1744 send_sig(SIGTRAP, current, 1);
1745 #endif
1746 unlock_user_struct(frame, frame_addr, 0);
1747 return env->regs[0];
1749 badframe:
1750 unlock_user_struct(frame, frame_addr, 0);
1751 force_sig(TARGET_SIGSEGV /* , current */);
1752 return 0;
1755 static long do_rt_sigreturn_v2(CPUARMState *env)
1757 abi_ulong frame_addr;
1758 struct rt_sigframe_v2 *frame;
1761 * Since we stacked the signal on a 64-bit boundary,
1762 * then 'sp' should be word aligned here. If it's
1763 * not, then the user is trying to mess with us.
1765 if (env->regs[13] & 7)
1766 goto badframe;
1768 frame_addr = env->regs[13];
1769 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1770 goto badframe;
1772 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1773 goto badframe;
1775 unlock_user_struct(frame, frame_addr, 0);
1776 return env->regs[0];
1778 badframe:
1779 unlock_user_struct(frame, frame_addr, 0);
1780 force_sig(TARGET_SIGSEGV /* , current */);
1781 return 0;
1784 long do_rt_sigreturn(CPUARMState *env)
1786 if (get_osversion() >= 0x020612) {
1787 return do_rt_sigreturn_v2(env);
1788 } else {
1789 return do_rt_sigreturn_v1(env);
1793 #elif defined(TARGET_SPARC)
1795 #define __SUNOS_MAXWIN 31
1797 /* This is what SunOS does, so shall I. */
1798 struct target_sigcontext {
1799 abi_ulong sigc_onstack; /* state to restore */
1801 abi_ulong sigc_mask; /* sigmask to restore */
1802 abi_ulong sigc_sp; /* stack pointer */
1803 abi_ulong sigc_pc; /* program counter */
1804 abi_ulong sigc_npc; /* next program counter */
1805 abi_ulong sigc_psr; /* for condition codes etc */
1806 abi_ulong sigc_g1; /* User uses these two registers */
1807 abi_ulong sigc_o0; /* within the trampoline code. */
1809 /* Now comes information regarding the users window set
1810 * at the time of the signal.
1812 abi_ulong sigc_oswins; /* outstanding windows */
1814 /* stack ptrs for each regwin buf */
1815 char *sigc_spbuf[__SUNOS_MAXWIN];
1817 /* Windows to restore after signal */
1818 struct {
1819 abi_ulong locals[8];
1820 abi_ulong ins[8];
1821 } sigc_wbuf[__SUNOS_MAXWIN];
1823 /* A Sparc stack frame */
1824 struct sparc_stackf {
1825 abi_ulong locals[8];
1826 abi_ulong ins[8];
1827 /* It's simpler to treat fp and callers_pc as elements of ins[]
1828 * since we never need to access them ourselves.
1830 char *structptr;
1831 abi_ulong xargs[6];
1832 abi_ulong xxargs[1];
1835 typedef struct {
1836 struct {
1837 abi_ulong psr;
1838 abi_ulong pc;
1839 abi_ulong npc;
1840 abi_ulong y;
1841 abi_ulong u_regs[16]; /* globals and ins */
1842 } si_regs;
1843 int si_mask;
1844 } __siginfo_t;
1846 typedef struct {
1847 abi_ulong si_float_regs[32];
1848 unsigned long si_fsr;
1849 unsigned long si_fpqdepth;
1850 struct {
1851 unsigned long *insn_addr;
1852 unsigned long insn;
1853 } si_fpqueue [16];
1854 } qemu_siginfo_fpu_t;
1857 struct target_signal_frame {
1858 struct sparc_stackf ss;
1859 __siginfo_t info;
1860 abi_ulong fpu_save;
1861 abi_ulong insns[2] __attribute__ ((aligned (8)));
1862 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1863 abi_ulong extra_size; /* Should be 0 */
1864 qemu_siginfo_fpu_t fpu_state;
1866 struct target_rt_signal_frame {
1867 struct sparc_stackf ss;
1868 siginfo_t info;
1869 abi_ulong regs[20];
1870 sigset_t mask;
1871 abi_ulong fpu_save;
1872 unsigned int insns[2];
1873 stack_t stack;
1874 unsigned int extra_size; /* Should be 0 */
1875 qemu_siginfo_fpu_t fpu_state;
1878 #define UREG_O0 16
1879 #define UREG_O6 22
1880 #define UREG_I0 0
1881 #define UREG_I1 1
1882 #define UREG_I2 2
1883 #define UREG_I3 3
1884 #define UREG_I4 4
1885 #define UREG_I5 5
1886 #define UREG_I6 6
1887 #define UREG_I7 7
1888 #define UREG_L0 8
1889 #define UREG_FP UREG_I6
1890 #define UREG_SP UREG_O6
1892 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1893 CPUSPARCState *env,
1894 unsigned long framesize)
1896 abi_ulong sp;
1898 sp = env->regwptr[UREG_FP];
1900 /* This is the X/Open sanctioned signal stack switching. */
1901 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1902 if (!on_sig_stack(sp)
1903 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1904 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1906 return sp - framesize;
1909 static int
1910 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
1912 int err = 0, i;
1914 err |= __put_user(env->psr, &si->si_regs.psr);
1915 err |= __put_user(env->pc, &si->si_regs.pc);
1916 err |= __put_user(env->npc, &si->si_regs.npc);
1917 err |= __put_user(env->y, &si->si_regs.y);
1918 for (i=0; i < 8; i++) {
1919 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1921 for (i=0; i < 8; i++) {
1922 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1924 err |= __put_user(mask, &si->si_mask);
1925 return err;
1928 #if 0
1929 static int
1930 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1931 CPUSPARCState *env, unsigned long mask)
1933 int err = 0;
1935 err |= __put_user(mask, &sc->sigc_mask);
1936 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1937 err |= __put_user(env->pc, &sc->sigc_pc);
1938 err |= __put_user(env->npc, &sc->sigc_npc);
1939 err |= __put_user(env->psr, &sc->sigc_psr);
1940 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1941 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1943 return err;
1945 #endif
1946 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1948 static void setup_frame(int sig, struct target_sigaction *ka,
1949 target_sigset_t *set, CPUSPARCState *env)
1951 abi_ulong sf_addr;
1952 struct target_signal_frame *sf;
1953 int sigframe_size, err, i;
1955 /* 1. Make sure everything is clean */
1956 //synchronize_user_stack();
1958 sigframe_size = NF_ALIGNEDSZ;
1959 sf_addr = get_sigframe(ka, env, sigframe_size);
1961 sf = lock_user(VERIFY_WRITE, sf_addr,
1962 sizeof(struct target_signal_frame), 0);
1963 if (!sf)
1964 goto sigsegv;
1966 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1967 #if 0
1968 if (invalid_frame_pointer(sf, sigframe_size))
1969 goto sigill_and_return;
1970 #endif
1971 /* 2. Save the current process state */
1972 err = setup___siginfo(&sf->info, env, set->sig[0]);
1973 err |= __put_user(0, &sf->extra_size);
1975 //err |= save_fpu_state(regs, &sf->fpu_state);
1976 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1978 err |= __put_user(set->sig[0], &sf->info.si_mask);
1979 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1980 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1983 for (i = 0; i < 8; i++) {
1984 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1986 for (i = 0; i < 8; i++) {
1987 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1989 if (err)
1990 goto sigsegv;
1992 /* 3. signal handler back-trampoline and parameters */
1993 env->regwptr[UREG_FP] = sf_addr;
1994 env->regwptr[UREG_I0] = sig;
1995 env->regwptr[UREG_I1] = sf_addr +
1996 offsetof(struct target_signal_frame, info);
1997 env->regwptr[UREG_I2] = sf_addr +
1998 offsetof(struct target_signal_frame, info);
2000 /* 4. signal handler */
2001 env->pc = ka->_sa_handler;
2002 env->npc = (env->pc + 4);
2003 /* 5. return to kernel instructions */
2004 if (ka->sa_restorer)
2005 env->regwptr[UREG_I7] = ka->sa_restorer;
2006 else {
2007 uint32_t val32;
2009 env->regwptr[UREG_I7] = sf_addr +
2010 offsetof(struct target_signal_frame, insns) - 2 * 4;
2012 /* mov __NR_sigreturn, %g1 */
2013 val32 = 0x821020d8;
2014 err |= __put_user(val32, &sf->insns[0]);
2016 /* t 0x10 */
2017 val32 = 0x91d02010;
2018 err |= __put_user(val32, &sf->insns[1]);
2019 if (err)
2020 goto sigsegv;
2022 /* Flush instruction space. */
2023 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2024 // tb_flush(env);
2026 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2027 return;
2028 #if 0
2029 sigill_and_return:
2030 force_sig(TARGET_SIGILL);
2031 #endif
2032 sigsegv:
2033 //fprintf(stderr, "force_sig\n");
2034 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2035 force_sig(TARGET_SIGSEGV);
2037 static inline int
2038 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
2040 int err;
2041 #if 0
2042 #ifdef CONFIG_SMP
2043 if (current->flags & PF_USEDFPU)
2044 regs->psr &= ~PSR_EF;
2045 #else
2046 if (current == last_task_used_math) {
2047 last_task_used_math = 0;
2048 regs->psr &= ~PSR_EF;
2050 #endif
2051 current->used_math = 1;
2052 current->flags &= ~PF_USEDFPU;
2053 #endif
2054 #if 0
2055 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2056 return -EFAULT;
2057 #endif
2059 /* XXX: incorrect */
2060 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2061 (sizeof(abi_ulong) * 32));
2062 err |= __get_user(env->fsr, &fpu->si_fsr);
2063 #if 0
2064 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2065 if (current->thread.fpqdepth != 0)
2066 err |= __copy_from_user(&current->thread.fpqueue[0],
2067 &fpu->si_fpqueue[0],
2068 ((sizeof(unsigned long) +
2069 (sizeof(unsigned long *)))*16));
2070 #endif
2071 return err;
2075 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2076 target_siginfo_t *info,
2077 target_sigset_t *set, CPUSPARCState *env)
2079 fprintf(stderr, "setup_rt_frame: not implemented\n");
2082 long do_sigreturn(CPUSPARCState *env)
2084 abi_ulong sf_addr;
2085 struct target_signal_frame *sf;
2086 uint32_t up_psr, pc, npc;
2087 target_sigset_t set;
2088 sigset_t host_set;
2089 int err, i;
2091 sf_addr = env->regwptr[UREG_FP];
2092 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2093 goto segv_and_exit;
2094 #if 0
2095 fprintf(stderr, "sigreturn\n");
2096 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2097 #endif
2098 //cpu_dump_state(env, stderr, fprintf, 0);
2100 /* 1. Make sure we are not getting garbage from the user */
2102 if (sf_addr & 3)
2103 goto segv_and_exit;
2105 err = __get_user(pc, &sf->info.si_regs.pc);
2106 err |= __get_user(npc, &sf->info.si_regs.npc);
2108 if ((pc | npc) & 3)
2109 goto segv_and_exit;
2111 /* 2. Restore the state */
2112 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2114 /* User can only change condition codes and FPU enabling in %psr. */
2115 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2116 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2118 env->pc = pc;
2119 env->npc = npc;
2120 err |= __get_user(env->y, &sf->info.si_regs.y);
2121 for (i=0; i < 8; i++) {
2122 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2124 for (i=0; i < 8; i++) {
2125 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2128 /* FIXME: implement FPU save/restore:
2129 * __get_user(fpu_save, &sf->fpu_save);
2130 * if (fpu_save)
2131 * err |= restore_fpu_state(env, fpu_save);
2134 /* This is pretty much atomic, no amount locking would prevent
2135 * the races which exist anyways.
2137 err |= __get_user(set.sig[0], &sf->info.si_mask);
2138 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2139 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2142 target_to_host_sigset_internal(&host_set, &set);
2143 sigprocmask(SIG_SETMASK, &host_set, NULL);
2145 if (err)
2146 goto segv_and_exit;
2147 unlock_user_struct(sf, sf_addr, 0);
2148 return env->regwptr[0];
2150 segv_and_exit:
2151 unlock_user_struct(sf, sf_addr, 0);
2152 force_sig(TARGET_SIGSEGV);
2155 long do_rt_sigreturn(CPUSPARCState *env)
2157 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2158 return -TARGET_ENOSYS;
2161 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2162 #define MC_TSTATE 0
2163 #define MC_PC 1
2164 #define MC_NPC 2
2165 #define MC_Y 3
2166 #define MC_G1 4
2167 #define MC_G2 5
2168 #define MC_G3 6
2169 #define MC_G4 7
2170 #define MC_G5 8
2171 #define MC_G6 9
2172 #define MC_G7 10
2173 #define MC_O0 11
2174 #define MC_O1 12
2175 #define MC_O2 13
2176 #define MC_O3 14
2177 #define MC_O4 15
2178 #define MC_O5 16
2179 #define MC_O6 17
2180 #define MC_O7 18
2181 #define MC_NGREG 19
2183 typedef abi_ulong target_mc_greg_t;
2184 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2186 struct target_mc_fq {
2187 abi_ulong *mcfq_addr;
2188 uint32_t mcfq_insn;
2191 struct target_mc_fpu {
2192 union {
2193 uint32_t sregs[32];
2194 uint64_t dregs[32];
2195 //uint128_t qregs[16];
2196 } mcfpu_fregs;
2197 abi_ulong mcfpu_fsr;
2198 abi_ulong mcfpu_fprs;
2199 abi_ulong mcfpu_gsr;
2200 struct target_mc_fq *mcfpu_fq;
2201 unsigned char mcfpu_qcnt;
2202 unsigned char mcfpu_qentsz;
2203 unsigned char mcfpu_enab;
2205 typedef struct target_mc_fpu target_mc_fpu_t;
2207 typedef struct {
2208 target_mc_gregset_t mc_gregs;
2209 target_mc_greg_t mc_fp;
2210 target_mc_greg_t mc_i7;
2211 target_mc_fpu_t mc_fpregs;
2212 } target_mcontext_t;
2214 struct target_ucontext {
2215 struct target_ucontext *tuc_link;
2216 abi_ulong tuc_flags;
2217 target_sigset_t tuc_sigmask;
2218 target_mcontext_t tuc_mcontext;
2221 /* A V9 register window */
2222 struct target_reg_window {
2223 abi_ulong locals[8];
2224 abi_ulong ins[8];
2227 #define TARGET_STACK_BIAS 2047
2229 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2230 void sparc64_set_context(CPUSPARCState *env)
2232 abi_ulong ucp_addr;
2233 struct target_ucontext *ucp;
2234 target_mc_gregset_t *grp;
2235 abi_ulong pc, npc, tstate;
2236 abi_ulong fp, i7, w_addr;
2237 int err;
2238 unsigned int i;
2240 ucp_addr = env->regwptr[UREG_I0];
2241 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2242 goto do_sigsegv;
2243 grp = &ucp->tuc_mcontext.mc_gregs;
2244 err = __get_user(pc, &((*grp)[MC_PC]));
2245 err |= __get_user(npc, &((*grp)[MC_NPC]));
2246 if (err || ((pc | npc) & 3))
2247 goto do_sigsegv;
2248 if (env->regwptr[UREG_I1]) {
2249 target_sigset_t target_set;
2250 sigset_t set;
2252 if (TARGET_NSIG_WORDS == 1) {
2253 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2254 goto do_sigsegv;
2255 } else {
2256 abi_ulong *src, *dst;
2257 src = ucp->tuc_sigmask.sig;
2258 dst = target_set.sig;
2259 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2260 i++, dst++, src++)
2261 err |= __get_user(*dst, src);
2262 if (err)
2263 goto do_sigsegv;
2265 target_to_host_sigset_internal(&set, &target_set);
2266 sigprocmask(SIG_SETMASK, &set, NULL);
2268 env->pc = pc;
2269 env->npc = npc;
2270 err |= __get_user(env->y, &((*grp)[MC_Y]));
2271 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2272 env->asi = (tstate >> 24) & 0xff;
2273 cpu_put_ccr(env, tstate >> 32);
2274 cpu_put_cwp64(env, tstate & 0x1f);
2275 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2276 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2277 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2278 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2279 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2280 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2281 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2282 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2283 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2284 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2285 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2286 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2287 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2288 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2289 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2291 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2292 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2294 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2295 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2296 abi_ulong) != 0)
2297 goto do_sigsegv;
2298 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2299 abi_ulong) != 0)
2300 goto do_sigsegv;
2301 /* FIXME this does not match how the kernel handles the FPU in
2302 * its sparc64_set_context implementation. In particular the FPU
2303 * is only restored if fenab is non-zero in:
2304 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2306 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2308 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2309 for (i = 0; i < 64; i++, src++) {
2310 if (i & 1) {
2311 err |= __get_user(env->fpr[i/2].l.lower, src);
2312 } else {
2313 err |= __get_user(env->fpr[i/2].l.upper, src);
2317 err |= __get_user(env->fsr,
2318 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2319 err |= __get_user(env->gsr,
2320 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2321 if (err)
2322 goto do_sigsegv;
2323 unlock_user_struct(ucp, ucp_addr, 0);
2324 return;
2325 do_sigsegv:
2326 unlock_user_struct(ucp, ucp_addr, 0);
2327 force_sig(TARGET_SIGSEGV);
2330 void sparc64_get_context(CPUSPARCState *env)
2332 abi_ulong ucp_addr;
2333 struct target_ucontext *ucp;
2334 target_mc_gregset_t *grp;
2335 target_mcontext_t *mcp;
2336 abi_ulong fp, i7, w_addr;
2337 int err;
2338 unsigned int i;
2339 target_sigset_t target_set;
2340 sigset_t set;
2342 ucp_addr = env->regwptr[UREG_I0];
2343 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2344 goto do_sigsegv;
2346 mcp = &ucp->tuc_mcontext;
2347 grp = &mcp->mc_gregs;
2349 /* Skip over the trap instruction, first. */
2350 env->pc = env->npc;
2351 env->npc += 4;
2353 err = 0;
2355 sigprocmask(0, NULL, &set);
2356 host_to_target_sigset_internal(&target_set, &set);
2357 if (TARGET_NSIG_WORDS == 1) {
2358 err |= __put_user(target_set.sig[0],
2359 (abi_ulong *)&ucp->tuc_sigmask);
2360 } else {
2361 abi_ulong *src, *dst;
2362 src = target_set.sig;
2363 dst = ucp->tuc_sigmask.sig;
2364 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2365 i++, dst++, src++)
2366 err |= __put_user(*src, dst);
2367 if (err)
2368 goto do_sigsegv;
2371 /* XXX: tstate must be saved properly */
2372 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2373 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2374 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2375 err |= __put_user(env->y, &((*grp)[MC_Y]));
2376 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2377 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2378 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2379 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2380 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2381 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2382 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2383 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2384 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2385 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2386 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2387 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2388 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2389 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2390 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2392 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2393 fp = i7 = 0;
2394 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2395 abi_ulong) != 0)
2396 goto do_sigsegv;
2397 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2398 abi_ulong) != 0)
2399 goto do_sigsegv;
2400 err |= __put_user(fp, &(mcp->mc_fp));
2401 err |= __put_user(i7, &(mcp->mc_i7));
2404 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2405 for (i = 0; i < 64; i++, dst++) {
2406 if (i & 1) {
2407 err |= __put_user(env->fpr[i/2].l.lower, dst);
2408 } else {
2409 err |= __put_user(env->fpr[i/2].l.upper, dst);
2413 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2414 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2415 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2417 if (err)
2418 goto do_sigsegv;
2419 unlock_user_struct(ucp, ucp_addr, 1);
2420 return;
2421 do_sigsegv:
2422 unlock_user_struct(ucp, ucp_addr, 1);
2423 force_sig(TARGET_SIGSEGV);
2425 #endif
2426 #elif defined(TARGET_ABI_MIPSN64)
2428 # warning signal handling not implemented
2430 static void setup_frame(int sig, struct target_sigaction *ka,
2431 target_sigset_t *set, CPUMIPSState *env)
2433 fprintf(stderr, "setup_frame: not implemented\n");
2436 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2437 target_siginfo_t *info,
2438 target_sigset_t *set, CPUMIPSState *env)
2440 fprintf(stderr, "setup_rt_frame: not implemented\n");
2443 long do_sigreturn(CPUMIPSState *env)
2445 fprintf(stderr, "do_sigreturn: not implemented\n");
2446 return -TARGET_ENOSYS;
2449 long do_rt_sigreturn(CPUMIPSState *env)
2451 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2452 return -TARGET_ENOSYS;
2455 #elif defined(TARGET_ABI_MIPSN32)
2457 # warning signal handling not implemented
2459 static void setup_frame(int sig, struct target_sigaction *ka,
2460 target_sigset_t *set, CPUMIPSState *env)
2462 fprintf(stderr, "setup_frame: not implemented\n");
2465 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2466 target_siginfo_t *info,
2467 target_sigset_t *set, CPUMIPSState *env)
2469 fprintf(stderr, "setup_rt_frame: not implemented\n");
2472 long do_sigreturn(CPUMIPSState *env)
2474 fprintf(stderr, "do_sigreturn: not implemented\n");
2475 return -TARGET_ENOSYS;
2478 long do_rt_sigreturn(CPUMIPSState *env)
2480 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2481 return -TARGET_ENOSYS;
2484 #elif defined(TARGET_ABI_MIPSO32)
2486 struct target_sigcontext {
2487 uint32_t sc_regmask; /* Unused */
2488 uint32_t sc_status;
2489 uint64_t sc_pc;
2490 uint64_t sc_regs[32];
2491 uint64_t sc_fpregs[32];
2492 uint32_t sc_ownedfp; /* Unused */
2493 uint32_t sc_fpc_csr;
2494 uint32_t sc_fpc_eir; /* Unused */
2495 uint32_t sc_used_math;
2496 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2497 uint32_t pad0;
2498 uint64_t sc_mdhi;
2499 uint64_t sc_mdlo;
2500 target_ulong sc_hi1; /* Was sc_cause */
2501 target_ulong sc_lo1; /* Was sc_badvaddr */
2502 target_ulong sc_hi2; /* Was sc_sigset[4] */
2503 target_ulong sc_lo2;
2504 target_ulong sc_hi3;
2505 target_ulong sc_lo3;
2508 struct sigframe {
2509 uint32_t sf_ass[4]; /* argument save space for o32 */
2510 uint32_t sf_code[2]; /* signal trampoline */
2511 struct target_sigcontext sf_sc;
2512 target_sigset_t sf_mask;
2515 struct target_ucontext {
2516 target_ulong tuc_flags;
2517 target_ulong tuc_link;
2518 target_stack_t tuc_stack;
2519 target_ulong pad0;
2520 struct target_sigcontext tuc_mcontext;
2521 target_sigset_t tuc_sigmask;
2524 struct target_rt_sigframe {
2525 uint32_t rs_ass[4]; /* argument save space for o32 */
2526 uint32_t rs_code[2]; /* signal trampoline */
2527 struct target_siginfo rs_info;
2528 struct target_ucontext rs_uc;
2531 /* Install trampoline to jump back from signal handler */
2532 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2534 int err;
2537 * Set up the return code ...
2539 * li v0, __NR__foo_sigreturn
2540 * syscall
2543 err = __put_user(0x24020000 + syscall, tramp + 0);
2544 err |= __put_user(0x0000000c , tramp + 1);
2545 /* flush_cache_sigtramp((unsigned long) tramp); */
2546 return err;
2549 static inline int
2550 setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2552 int err = 0;
2554 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2556 #define save_gp_reg(i) do { \
2557 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2558 } while(0)
2559 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2560 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2561 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2562 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2563 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2564 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2565 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2566 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2567 save_gp_reg(31);
2568 #undef save_gp_reg
2570 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2571 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2573 /* Not used yet, but might be useful if we ever have DSP suppport */
2574 #if 0
2575 if (cpu_has_dsp) {
2576 err |= __put_user(mfhi1(), &sc->sc_hi1);
2577 err |= __put_user(mflo1(), &sc->sc_lo1);
2578 err |= __put_user(mfhi2(), &sc->sc_hi2);
2579 err |= __put_user(mflo2(), &sc->sc_lo2);
2580 err |= __put_user(mfhi3(), &sc->sc_hi3);
2581 err |= __put_user(mflo3(), &sc->sc_lo3);
2582 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2584 /* same with 64 bit */
2585 #ifdef CONFIG_64BIT
2586 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2587 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2588 if (cpu_has_dsp) {
2589 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2590 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2591 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2592 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2593 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2594 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2595 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2597 #endif
2598 #endif
2600 #if 0
2601 err |= __put_user(!!used_math(), &sc->sc_used_math);
2603 if (!used_math())
2604 goto out;
2607 * Save FPU state to signal context. Signal handler will "inherit"
2608 * current FPU state.
2610 preempt_disable();
2612 if (!is_fpu_owner()) {
2613 own_fpu();
2614 restore_fp(current);
2616 err |= save_fp_context(sc);
2618 preempt_enable();
2619 out:
2620 #endif
2621 return err;
2624 static inline int
2625 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2627 int err = 0;
2629 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2631 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2632 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2634 #define restore_gp_reg(i) do { \
2635 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2636 } while(0)
2637 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2638 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2639 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2640 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2641 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2642 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2643 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2644 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2645 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2646 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2647 restore_gp_reg(31);
2648 #undef restore_gp_reg
2650 #if 0
2651 if (cpu_has_dsp) {
2652 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2653 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2654 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2655 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2656 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2657 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2658 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2660 #ifdef CONFIG_64BIT
2661 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2662 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2663 if (cpu_has_dsp) {
2664 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2665 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2666 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2667 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2668 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2669 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2670 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2672 #endif
2674 err |= __get_user(used_math, &sc->sc_used_math);
2675 conditional_used_math(used_math);
2677 preempt_disable();
2679 if (used_math()) {
2680 /* restore fpu context if we have used it before */
2681 own_fpu();
2682 err |= restore_fp_context(sc);
2683 } else {
2684 /* signal handler may have used FPU. Give it up. */
2685 lose_fpu();
2688 preempt_enable();
2689 #endif
2690 return err;
2693 * Determine which stack to use..
2695 static inline abi_ulong
2696 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2698 unsigned long sp;
2700 /* Default to using normal stack */
2701 sp = regs->active_tc.gpr[29];
2704 * FPU emulator may have its own trampoline active just
2705 * above the user stack, 16-bytes before the next lowest
2706 * 16 byte boundary. Try to avoid trashing it.
2708 sp -= 32;
2710 /* This is the X/Open sanctioned signal stack switching. */
2711 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2712 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2715 return (sp - frame_size) & ~7;
2718 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2719 static void setup_frame(int sig, struct target_sigaction * ka,
2720 target_sigset_t *set, CPUMIPSState *regs)
2722 struct sigframe *frame;
2723 abi_ulong frame_addr;
2724 int i;
2726 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2727 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2728 goto give_sigsegv;
2730 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2732 if(setup_sigcontext(regs, &frame->sf_sc))
2733 goto give_sigsegv;
2735 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2736 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2737 goto give_sigsegv;
2741 * Arguments to signal handler:
2743 * a0 = signal number
2744 * a1 = 0 (should be cause)
2745 * a2 = pointer to struct sigcontext
2747 * $25 and PC point to the signal handler, $29 points to the
2748 * struct sigframe.
2750 regs->active_tc.gpr[ 4] = sig;
2751 regs->active_tc.gpr[ 5] = 0;
2752 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2753 regs->active_tc.gpr[29] = frame_addr;
2754 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2755 /* The original kernel code sets CP0_EPC to the handler
2756 * since it returns to userland using eret
2757 * we cannot do this here, and we must set PC directly */
2758 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2759 unlock_user_struct(frame, frame_addr, 1);
2760 return;
2762 give_sigsegv:
2763 unlock_user_struct(frame, frame_addr, 1);
2764 force_sig(TARGET_SIGSEGV/*, current*/);
2767 long do_sigreturn(CPUMIPSState *regs)
2769 struct sigframe *frame;
2770 abi_ulong frame_addr;
2771 sigset_t blocked;
2772 target_sigset_t target_set;
2773 int i;
2775 #if defined(DEBUG_SIGNAL)
2776 fprintf(stderr, "do_sigreturn\n");
2777 #endif
2778 frame_addr = regs->active_tc.gpr[29];
2779 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2780 goto badframe;
2782 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2783 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2784 goto badframe;
2787 target_to_host_sigset_internal(&blocked, &target_set);
2788 sigprocmask(SIG_SETMASK, &blocked, NULL);
2790 if (restore_sigcontext(regs, &frame->sf_sc))
2791 goto badframe;
2793 #if 0
2795 * Don't let your children do this ...
2797 __asm__ __volatile__(
2798 "move\t$29, %0\n\t"
2799 "j\tsyscall_exit"
2800 :/* no outputs */
2801 :"r" (&regs));
2802 /* Unreached */
2803 #endif
2805 regs->active_tc.PC = regs->CP0_EPC;
2806 /* I am not sure this is right, but it seems to work
2807 * maybe a problem with nested signals ? */
2808 regs->CP0_EPC = 0;
2809 return -TARGET_QEMU_ESIGRETURN;
2811 badframe:
2812 force_sig(TARGET_SIGSEGV/*, current*/);
2813 return 0;
2816 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2817 target_siginfo_t *info,
2818 target_sigset_t *set, CPUMIPSState *env)
2820 struct target_rt_sigframe *frame;
2821 abi_ulong frame_addr;
2822 int i;
2824 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2825 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2826 goto give_sigsegv;
2828 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2830 copy_siginfo_to_user(&frame->rs_info, info);
2832 __put_user(0, &frame->rs_uc.tuc_flags);
2833 __put_user(0, &frame->rs_uc.tuc_link);
2834 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2835 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2836 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2837 &frame->rs_uc.tuc_stack.ss_flags);
2839 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2841 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2842 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2846 * Arguments to signal handler:
2848 * a0 = signal number
2849 * a1 = pointer to siginfo_t
2850 * a2 = pointer to struct ucontext
2852 * $25 and PC point to the signal handler, $29 points to the
2853 * struct sigframe.
2855 env->active_tc.gpr[ 4] = sig;
2856 env->active_tc.gpr[ 5] = frame_addr
2857 + offsetof(struct target_rt_sigframe, rs_info);
2858 env->active_tc.gpr[ 6] = frame_addr
2859 + offsetof(struct target_rt_sigframe, rs_uc);
2860 env->active_tc.gpr[29] = frame_addr;
2861 env->active_tc.gpr[31] = frame_addr
2862 + offsetof(struct target_rt_sigframe, rs_code);
2863 /* The original kernel code sets CP0_EPC to the handler
2864 * since it returns to userland using eret
2865 * we cannot do this here, and we must set PC directly */
2866 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2867 unlock_user_struct(frame, frame_addr, 1);
2868 return;
2870 give_sigsegv:
2871 unlock_user_struct(frame, frame_addr, 1);
2872 force_sig(TARGET_SIGSEGV/*, current*/);
2875 long do_rt_sigreturn(CPUMIPSState *env)
2877 struct target_rt_sigframe *frame;
2878 abi_ulong frame_addr;
2879 sigset_t blocked;
2881 #if defined(DEBUG_SIGNAL)
2882 fprintf(stderr, "do_rt_sigreturn\n");
2883 #endif
2884 frame_addr = env->active_tc.gpr[29];
2885 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2886 goto badframe;
2888 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2889 sigprocmask(SIG_SETMASK, &blocked, NULL);
2891 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2892 goto badframe;
2894 if (do_sigaltstack(frame_addr +
2895 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2896 0, get_sp_from_cpustate(env)) == -EFAULT)
2897 goto badframe;
2899 env->active_tc.PC = env->CP0_EPC;
2900 /* I am not sure this is right, but it seems to work
2901 * maybe a problem with nested signals ? */
2902 env->CP0_EPC = 0;
2903 return -TARGET_QEMU_ESIGRETURN;
2905 badframe:
2906 force_sig(TARGET_SIGSEGV/*, current*/);
2907 return 0;
2910 #elif defined(TARGET_SH4)
2913 * code and data structures from linux kernel:
2914 * include/asm-sh/sigcontext.h
2915 * arch/sh/kernel/signal.c
2918 struct target_sigcontext {
2919 target_ulong oldmask;
2921 /* CPU registers */
2922 target_ulong sc_gregs[16];
2923 target_ulong sc_pc;
2924 target_ulong sc_pr;
2925 target_ulong sc_sr;
2926 target_ulong sc_gbr;
2927 target_ulong sc_mach;
2928 target_ulong sc_macl;
2930 /* FPU registers */
2931 target_ulong sc_fpregs[16];
2932 target_ulong sc_xfpregs[16];
2933 unsigned int sc_fpscr;
2934 unsigned int sc_fpul;
2935 unsigned int sc_ownedfp;
2938 struct target_sigframe
2940 struct target_sigcontext sc;
2941 target_ulong extramask[TARGET_NSIG_WORDS-1];
2942 uint16_t retcode[3];
2946 struct target_ucontext {
2947 target_ulong tuc_flags;
2948 struct target_ucontext *tuc_link;
2949 target_stack_t tuc_stack;
2950 struct target_sigcontext tuc_mcontext;
2951 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2954 struct target_rt_sigframe
2956 struct target_siginfo info;
2957 struct target_ucontext uc;
2958 uint16_t retcode[3];
2962 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2963 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2965 static abi_ulong get_sigframe(struct target_sigaction *ka,
2966 unsigned long sp, size_t frame_size)
2968 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2969 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2972 return (sp - frame_size) & -8ul;
2975 static int setup_sigcontext(struct target_sigcontext *sc,
2976 CPUSH4State *regs, unsigned long mask)
2978 int err = 0;
2979 int i;
2981 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2982 COPY(gregs[0]); COPY(gregs[1]);
2983 COPY(gregs[2]); COPY(gregs[3]);
2984 COPY(gregs[4]); COPY(gregs[5]);
2985 COPY(gregs[6]); COPY(gregs[7]);
2986 COPY(gregs[8]); COPY(gregs[9]);
2987 COPY(gregs[10]); COPY(gregs[11]);
2988 COPY(gregs[12]); COPY(gregs[13]);
2989 COPY(gregs[14]); COPY(gregs[15]);
2990 COPY(gbr); COPY(mach);
2991 COPY(macl); COPY(pr);
2992 COPY(sr); COPY(pc);
2993 #undef COPY
2995 for (i=0; i<16; i++) {
2996 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2998 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2999 err |= __put_user(regs->fpul, &sc->sc_fpul);
3001 /* non-iBCS2 extensions.. */
3002 err |= __put_user(mask, &sc->oldmask);
3004 return err;
3007 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3008 target_ulong *r0_p)
3010 unsigned int err = 0;
3011 int i;
3013 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3014 COPY(gregs[1]);
3015 COPY(gregs[2]); COPY(gregs[3]);
3016 COPY(gregs[4]); COPY(gregs[5]);
3017 COPY(gregs[6]); COPY(gregs[7]);
3018 COPY(gregs[8]); COPY(gregs[9]);
3019 COPY(gregs[10]); COPY(gregs[11]);
3020 COPY(gregs[12]); COPY(gregs[13]);
3021 COPY(gregs[14]); COPY(gregs[15]);
3022 COPY(gbr); COPY(mach);
3023 COPY(macl); COPY(pr);
3024 COPY(sr); COPY(pc);
3025 #undef COPY
3027 for (i=0; i<16; i++) {
3028 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3030 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3031 err |= __get_user(regs->fpul, &sc->sc_fpul);
3033 regs->tra = -1; /* disable syscall checks */
3034 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3035 return err;
3038 static void setup_frame(int sig, struct target_sigaction *ka,
3039 target_sigset_t *set, CPUSH4State *regs)
3041 struct target_sigframe *frame;
3042 abi_ulong frame_addr;
3043 int i;
3044 int err = 0;
3045 int signal;
3047 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3048 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3049 goto give_sigsegv;
3051 signal = current_exec_domain_sig(sig);
3053 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3055 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3056 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3059 /* Set up to return from userspace. If provided, use a stub
3060 already in userspace. */
3061 if (ka->sa_flags & TARGET_SA_RESTORER) {
3062 regs->pr = (unsigned long) ka->sa_restorer;
3063 } else {
3064 /* Generate return code (system call to sigreturn) */
3065 err |= __put_user(MOVW(2), &frame->retcode[0]);
3066 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3067 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3068 regs->pr = (unsigned long) frame->retcode;
3071 if (err)
3072 goto give_sigsegv;
3074 /* Set up registers for signal handler */
3075 regs->gregs[15] = frame_addr;
3076 regs->gregs[4] = signal; /* Arg for signal handler */
3077 regs->gregs[5] = 0;
3078 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3079 regs->pc = (unsigned long) ka->_sa_handler;
3081 unlock_user_struct(frame, frame_addr, 1);
3082 return;
3084 give_sigsegv:
3085 unlock_user_struct(frame, frame_addr, 1);
3086 force_sig(TARGET_SIGSEGV);
3089 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3090 target_siginfo_t *info,
3091 target_sigset_t *set, CPUSH4State *regs)
3093 struct target_rt_sigframe *frame;
3094 abi_ulong frame_addr;
3095 int i;
3096 int err = 0;
3097 int signal;
3099 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3100 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3101 goto give_sigsegv;
3103 signal = current_exec_domain_sig(sig);
3105 err |= copy_siginfo_to_user(&frame->info, info);
3107 /* Create the ucontext. */
3108 err |= __put_user(0, &frame->uc.tuc_flags);
3109 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3110 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3111 &frame->uc.tuc_stack.ss_sp);
3112 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3113 &frame->uc.tuc_stack.ss_flags);
3114 err |= __put_user(target_sigaltstack_used.ss_size,
3115 &frame->uc.tuc_stack.ss_size);
3116 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3117 regs, set->sig[0]);
3118 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3119 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3122 /* Set up to return from userspace. If provided, use a stub
3123 already in userspace. */
3124 if (ka->sa_flags & TARGET_SA_RESTORER) {
3125 regs->pr = (unsigned long) ka->sa_restorer;
3126 } else {
3127 /* Generate return code (system call to sigreturn) */
3128 err |= __put_user(MOVW(2), &frame->retcode[0]);
3129 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3130 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3131 regs->pr = (unsigned long) frame->retcode;
3134 if (err)
3135 goto give_sigsegv;
3137 /* Set up registers for signal handler */
3138 regs->gregs[15] = frame_addr;
3139 regs->gregs[4] = signal; /* Arg for signal handler */
3140 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3141 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3142 regs->pc = (unsigned long) ka->_sa_handler;
3144 unlock_user_struct(frame, frame_addr, 1);
3145 return;
3147 give_sigsegv:
3148 unlock_user_struct(frame, frame_addr, 1);
3149 force_sig(TARGET_SIGSEGV);
3152 long do_sigreturn(CPUSH4State *regs)
3154 struct target_sigframe *frame;
3155 abi_ulong frame_addr;
3156 sigset_t blocked;
3157 target_sigset_t target_set;
3158 target_ulong r0;
3159 int i;
3160 int err = 0;
3162 #if defined(DEBUG_SIGNAL)
3163 fprintf(stderr, "do_sigreturn\n");
3164 #endif
3165 frame_addr = regs->gregs[15];
3166 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3167 goto badframe;
3169 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3170 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3171 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3174 if (err)
3175 goto badframe;
3177 target_to_host_sigset_internal(&blocked, &target_set);
3178 sigprocmask(SIG_SETMASK, &blocked, NULL);
3180 if (restore_sigcontext(regs, &frame->sc, &r0))
3181 goto badframe;
3183 unlock_user_struct(frame, frame_addr, 0);
3184 return r0;
3186 badframe:
3187 unlock_user_struct(frame, frame_addr, 0);
3188 force_sig(TARGET_SIGSEGV);
3189 return 0;
3192 long do_rt_sigreturn(CPUSH4State *regs)
3194 struct target_rt_sigframe *frame;
3195 abi_ulong frame_addr;
3196 sigset_t blocked;
3197 target_ulong r0;
3199 #if defined(DEBUG_SIGNAL)
3200 fprintf(stderr, "do_rt_sigreturn\n");
3201 #endif
3202 frame_addr = regs->gregs[15];
3203 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3204 goto badframe;
3206 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3207 sigprocmask(SIG_SETMASK, &blocked, NULL);
3209 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3210 goto badframe;
3212 if (do_sigaltstack(frame_addr +
3213 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3214 0, get_sp_from_cpustate(regs)) == -EFAULT)
3215 goto badframe;
3217 unlock_user_struct(frame, frame_addr, 0);
3218 return r0;
3220 badframe:
3221 unlock_user_struct(frame, frame_addr, 0);
3222 force_sig(TARGET_SIGSEGV);
3223 return 0;
3225 #elif defined(TARGET_MICROBLAZE)
3227 struct target_sigcontext {
3228 struct target_pt_regs regs; /* needs to be first */
3229 uint32_t oldmask;
3232 struct target_stack_t {
3233 abi_ulong ss_sp;
3234 int ss_flags;
3235 unsigned int ss_size;
3238 struct target_ucontext {
3239 abi_ulong tuc_flags;
3240 abi_ulong tuc_link;
3241 struct target_stack_t tuc_stack;
3242 struct target_sigcontext tuc_mcontext;
3243 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3246 /* Signal frames. */
3247 struct target_signal_frame {
3248 struct target_ucontext uc;
3249 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3250 uint32_t tramp[2];
3253 struct rt_signal_frame {
3254 siginfo_t info;
3255 struct ucontext uc;
3256 uint32_t tramp[2];
3259 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3261 __put_user(env->regs[0], &sc->regs.r0);
3262 __put_user(env->regs[1], &sc->regs.r1);
3263 __put_user(env->regs[2], &sc->regs.r2);
3264 __put_user(env->regs[3], &sc->regs.r3);
3265 __put_user(env->regs[4], &sc->regs.r4);
3266 __put_user(env->regs[5], &sc->regs.r5);
3267 __put_user(env->regs[6], &sc->regs.r6);
3268 __put_user(env->regs[7], &sc->regs.r7);
3269 __put_user(env->regs[8], &sc->regs.r8);
3270 __put_user(env->regs[9], &sc->regs.r9);
3271 __put_user(env->regs[10], &sc->regs.r10);
3272 __put_user(env->regs[11], &sc->regs.r11);
3273 __put_user(env->regs[12], &sc->regs.r12);
3274 __put_user(env->regs[13], &sc->regs.r13);
3275 __put_user(env->regs[14], &sc->regs.r14);
3276 __put_user(env->regs[15], &sc->regs.r15);
3277 __put_user(env->regs[16], &sc->regs.r16);
3278 __put_user(env->regs[17], &sc->regs.r17);
3279 __put_user(env->regs[18], &sc->regs.r18);
3280 __put_user(env->regs[19], &sc->regs.r19);
3281 __put_user(env->regs[20], &sc->regs.r20);
3282 __put_user(env->regs[21], &sc->regs.r21);
3283 __put_user(env->regs[22], &sc->regs.r22);
3284 __put_user(env->regs[23], &sc->regs.r23);
3285 __put_user(env->regs[24], &sc->regs.r24);
3286 __put_user(env->regs[25], &sc->regs.r25);
3287 __put_user(env->regs[26], &sc->regs.r26);
3288 __put_user(env->regs[27], &sc->regs.r27);
3289 __put_user(env->regs[28], &sc->regs.r28);
3290 __put_user(env->regs[29], &sc->regs.r29);
3291 __put_user(env->regs[30], &sc->regs.r30);
3292 __put_user(env->regs[31], &sc->regs.r31);
3293 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3296 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3298 __get_user(env->regs[0], &sc->regs.r0);
3299 __get_user(env->regs[1], &sc->regs.r1);
3300 __get_user(env->regs[2], &sc->regs.r2);
3301 __get_user(env->regs[3], &sc->regs.r3);
3302 __get_user(env->regs[4], &sc->regs.r4);
3303 __get_user(env->regs[5], &sc->regs.r5);
3304 __get_user(env->regs[6], &sc->regs.r6);
3305 __get_user(env->regs[7], &sc->regs.r7);
3306 __get_user(env->regs[8], &sc->regs.r8);
3307 __get_user(env->regs[9], &sc->regs.r9);
3308 __get_user(env->regs[10], &sc->regs.r10);
3309 __get_user(env->regs[11], &sc->regs.r11);
3310 __get_user(env->regs[12], &sc->regs.r12);
3311 __get_user(env->regs[13], &sc->regs.r13);
3312 __get_user(env->regs[14], &sc->regs.r14);
3313 __get_user(env->regs[15], &sc->regs.r15);
3314 __get_user(env->regs[16], &sc->regs.r16);
3315 __get_user(env->regs[17], &sc->regs.r17);
3316 __get_user(env->regs[18], &sc->regs.r18);
3317 __get_user(env->regs[19], &sc->regs.r19);
3318 __get_user(env->regs[20], &sc->regs.r20);
3319 __get_user(env->regs[21], &sc->regs.r21);
3320 __get_user(env->regs[22], &sc->regs.r22);
3321 __get_user(env->regs[23], &sc->regs.r23);
3322 __get_user(env->regs[24], &sc->regs.r24);
3323 __get_user(env->regs[25], &sc->regs.r25);
3324 __get_user(env->regs[26], &sc->regs.r26);
3325 __get_user(env->regs[27], &sc->regs.r27);
3326 __get_user(env->regs[28], &sc->regs.r28);
3327 __get_user(env->regs[29], &sc->regs.r29);
3328 __get_user(env->regs[30], &sc->regs.r30);
3329 __get_user(env->regs[31], &sc->regs.r31);
3330 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3333 static abi_ulong get_sigframe(struct target_sigaction *ka,
3334 CPUMBState *env, int frame_size)
3336 abi_ulong sp = env->regs[1];
3338 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3339 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3341 return ((sp - frame_size) & -8UL);
3344 static void setup_frame(int sig, struct target_sigaction *ka,
3345 target_sigset_t *set, CPUMBState *env)
3347 struct target_signal_frame *frame;
3348 abi_ulong frame_addr;
3349 int err = 0;
3350 int i;
3352 frame_addr = get_sigframe(ka, env, sizeof *frame);
3353 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3354 goto badframe;
3356 /* Save the mask. */
3357 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3358 if (err)
3359 goto badframe;
3361 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3362 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3363 goto badframe;
3366 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3368 /* Set up to return from userspace. If provided, use a stub
3369 already in userspace. */
3370 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3371 if (ka->sa_flags & TARGET_SA_RESTORER) {
3372 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3373 } else {
3374 uint32_t t;
3375 /* Note, these encodings are _big endian_! */
3376 /* addi r12, r0, __NR_sigreturn */
3377 t = 0x31800000UL | TARGET_NR_sigreturn;
3378 err |= __put_user(t, frame->tramp + 0);
3379 /* brki r14, 0x8 */
3380 t = 0xb9cc0008UL;
3381 err |= __put_user(t, frame->tramp + 1);
3383 /* Return from sighandler will jump to the tramp.
3384 Negative 8 offset because return is rtsd r15, 8 */
3385 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3388 if (err)
3389 goto badframe;
3391 /* Set up registers for signal handler */
3392 env->regs[1] = frame_addr;
3393 /* Signal handler args: */
3394 env->regs[5] = sig; /* Arg 0: signum */
3395 env->regs[6] = 0;
3396 /* arg 1: sigcontext */
3397 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3399 /* Offset of 4 to handle microblaze rtid r14, 0 */
3400 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3402 unlock_user_struct(frame, frame_addr, 1);
3403 return;
3404 badframe:
3405 unlock_user_struct(frame, frame_addr, 1);
3406 force_sig(TARGET_SIGSEGV);
3409 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3410 target_siginfo_t *info,
3411 target_sigset_t *set, CPUMBState *env)
3413 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3416 long do_sigreturn(CPUMBState *env)
3418 struct target_signal_frame *frame;
3419 abi_ulong frame_addr;
3420 target_sigset_t target_set;
3421 sigset_t set;
3422 int i;
3424 frame_addr = env->regs[R_SP];
3425 /* Make sure the guest isn't playing games. */
3426 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3427 goto badframe;
3429 /* Restore blocked signals */
3430 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3431 goto badframe;
3432 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3433 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3434 goto badframe;
3436 target_to_host_sigset_internal(&set, &target_set);
3437 sigprocmask(SIG_SETMASK, &set, NULL);
3439 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3440 /* We got here through a sigreturn syscall, our path back is via an
3441 rtb insn so setup r14 for that. */
3442 env->regs[14] = env->sregs[SR_PC];
3444 unlock_user_struct(frame, frame_addr, 0);
3445 return env->regs[10];
3446 badframe:
3447 unlock_user_struct(frame, frame_addr, 0);
3448 force_sig(TARGET_SIGSEGV);
3451 long do_rt_sigreturn(CPUMBState *env)
3453 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3454 return -TARGET_ENOSYS;
3457 #elif defined(TARGET_CRIS)
3459 struct target_sigcontext {
3460 struct target_pt_regs regs; /* needs to be first */
3461 uint32_t oldmask;
3462 uint32_t usp; /* usp before stacking this gunk on it */
3465 /* Signal frames. */
3466 struct target_signal_frame {
3467 struct target_sigcontext sc;
3468 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3469 uint8_t retcode[8]; /* Trampoline code. */
3472 struct rt_signal_frame {
3473 siginfo_t *pinfo;
3474 void *puc;
3475 siginfo_t info;
3476 struct ucontext uc;
3477 uint8_t retcode[8]; /* Trampoline code. */
3480 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3482 __put_user(env->regs[0], &sc->regs.r0);
3483 __put_user(env->regs[1], &sc->regs.r1);
3484 __put_user(env->regs[2], &sc->regs.r2);
3485 __put_user(env->regs[3], &sc->regs.r3);
3486 __put_user(env->regs[4], &sc->regs.r4);
3487 __put_user(env->regs[5], &sc->regs.r5);
3488 __put_user(env->regs[6], &sc->regs.r6);
3489 __put_user(env->regs[7], &sc->regs.r7);
3490 __put_user(env->regs[8], &sc->regs.r8);
3491 __put_user(env->regs[9], &sc->regs.r9);
3492 __put_user(env->regs[10], &sc->regs.r10);
3493 __put_user(env->regs[11], &sc->regs.r11);
3494 __put_user(env->regs[12], &sc->regs.r12);
3495 __put_user(env->regs[13], &sc->regs.r13);
3496 __put_user(env->regs[14], &sc->usp);
3497 __put_user(env->regs[15], &sc->regs.acr);
3498 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3499 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3500 __put_user(env->pc, &sc->regs.erp);
3503 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3505 __get_user(env->regs[0], &sc->regs.r0);
3506 __get_user(env->regs[1], &sc->regs.r1);
3507 __get_user(env->regs[2], &sc->regs.r2);
3508 __get_user(env->regs[3], &sc->regs.r3);
3509 __get_user(env->regs[4], &sc->regs.r4);
3510 __get_user(env->regs[5], &sc->regs.r5);
3511 __get_user(env->regs[6], &sc->regs.r6);
3512 __get_user(env->regs[7], &sc->regs.r7);
3513 __get_user(env->regs[8], &sc->regs.r8);
3514 __get_user(env->regs[9], &sc->regs.r9);
3515 __get_user(env->regs[10], &sc->regs.r10);
3516 __get_user(env->regs[11], &sc->regs.r11);
3517 __get_user(env->regs[12], &sc->regs.r12);
3518 __get_user(env->regs[13], &sc->regs.r13);
3519 __get_user(env->regs[14], &sc->usp);
3520 __get_user(env->regs[15], &sc->regs.acr);
3521 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3522 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3523 __get_user(env->pc, &sc->regs.erp);
3526 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3528 abi_ulong sp;
3529 /* Align the stack downwards to 4. */
3530 sp = (env->regs[R_SP] & ~3);
3531 return sp - framesize;
3534 static void setup_frame(int sig, struct target_sigaction *ka,
3535 target_sigset_t *set, CPUCRISState *env)
3537 struct target_signal_frame *frame;
3538 abi_ulong frame_addr;
3539 int err = 0;
3540 int i;
3542 frame_addr = get_sigframe(env, sizeof *frame);
3543 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3544 goto badframe;
3547 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3548 * use this trampoline anymore but it sets it up for GDB.
3549 * In QEMU, using the trampoline simplifies things a bit so we use it.
3551 * This is movu.w __NR_sigreturn, r9; break 13;
3553 err |= __put_user(0x9c5f, frame->retcode+0);
3554 err |= __put_user(TARGET_NR_sigreturn,
3555 frame->retcode+2);
3556 err |= __put_user(0xe93d, frame->retcode+4);
3558 /* Save the mask. */
3559 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3560 if (err)
3561 goto badframe;
3563 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3564 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3565 goto badframe;
3568 setup_sigcontext(&frame->sc, env);
3570 /* Move the stack and setup the arguments for the handler. */
3571 env->regs[R_SP] = frame_addr;
3572 env->regs[10] = sig;
3573 env->pc = (unsigned long) ka->_sa_handler;
3574 /* Link SRP so the guest returns through the trampoline. */
3575 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3577 unlock_user_struct(frame, frame_addr, 1);
3578 return;
3579 badframe:
3580 unlock_user_struct(frame, frame_addr, 1);
3581 force_sig(TARGET_SIGSEGV);
3584 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3585 target_siginfo_t *info,
3586 target_sigset_t *set, CPUCRISState *env)
3588 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3591 long do_sigreturn(CPUCRISState *env)
3593 struct target_signal_frame *frame;
3594 abi_ulong frame_addr;
3595 target_sigset_t target_set;
3596 sigset_t set;
3597 int i;
3599 frame_addr = env->regs[R_SP];
3600 /* Make sure the guest isn't playing games. */
3601 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3602 goto badframe;
3604 /* Restore blocked signals */
3605 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3606 goto badframe;
3607 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3608 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3609 goto badframe;
3611 target_to_host_sigset_internal(&set, &target_set);
3612 sigprocmask(SIG_SETMASK, &set, NULL);
3614 restore_sigcontext(&frame->sc, env);
3615 unlock_user_struct(frame, frame_addr, 0);
3616 return env->regs[10];
3617 badframe:
3618 unlock_user_struct(frame, frame_addr, 0);
3619 force_sig(TARGET_SIGSEGV);
3622 long do_rt_sigreturn(CPUCRISState *env)
3624 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3625 return -TARGET_ENOSYS;
3628 #elif defined(TARGET_OPENRISC)
3630 struct target_sigcontext {
3631 struct target_pt_regs regs;
3632 abi_ulong oldmask;
3633 abi_ulong usp;
3636 struct target_ucontext {
3637 abi_ulong tuc_flags;
3638 abi_ulong tuc_link;
3639 target_stack_t tuc_stack;
3640 struct target_sigcontext tuc_mcontext;
3641 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3644 struct target_rt_sigframe {
3645 abi_ulong pinfo;
3646 uint64_t puc;
3647 struct target_siginfo info;
3648 struct target_sigcontext sc;
3649 struct target_ucontext uc;
3650 unsigned char retcode[16]; /* trampoline code */
3653 /* This is the asm-generic/ucontext.h version */
3654 #if 0
3655 static int restore_sigcontext(CPUOpenRISCState *regs,
3656 struct target_sigcontext *sc)
3658 unsigned int err = 0;
3659 unsigned long old_usp;
3661 /* Alwys make any pending restarted system call return -EINTR */
3662 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3664 /* restore the regs from &sc->regs (same as sc, since regs is first)
3665 * (sc is already checked for VERIFY_READ since the sigframe was
3666 * checked in sys_sigreturn previously)
3669 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3670 goto badframe;
3673 /* make sure the U-flag is set so user-mode cannot fool us */
3675 regs->sr &= ~SR_SM;
3677 /* restore the old USP as it was before we stacked the sc etc.
3678 * (we cannot just pop the sigcontext since we aligned the sp and
3679 * stuff after pushing it)
3682 err |= __get_user(old_usp, &sc->usp);
3683 phx_signal("old_usp 0x%lx", old_usp);
3685 __PHX__ REALLY /* ??? */
3686 wrusp(old_usp);
3687 regs->gpr[1] = old_usp;
3689 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3690 * after this completes, but we don't use that mechanism. maybe we can
3691 * use it now ?
3694 return err;
3696 badframe:
3697 return 1;
3699 #endif
3701 /* Set up a signal frame. */
3703 static int setup_sigcontext(struct target_sigcontext *sc,
3704 CPUOpenRISCState *regs,
3705 unsigned long mask)
3707 int err = 0;
3708 unsigned long usp = regs->gpr[1];
3710 /* copy the regs. they are first in sc so we can use sc directly */
3712 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3714 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3715 the signal handler. The frametype will be restored to its previous
3716 value in restore_sigcontext. */
3717 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3719 /* then some other stuff */
3720 err |= __put_user(mask, &sc->oldmask);
3721 err |= __put_user(usp, &sc->usp); return err;
3724 static inline unsigned long align_sigframe(unsigned long sp)
3726 unsigned long i;
3727 i = sp & ~3UL;
3728 return i;
3731 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3732 CPUOpenRISCState *regs,
3733 size_t frame_size)
3735 unsigned long sp = regs->gpr[1];
3736 int onsigstack = on_sig_stack(sp);
3738 /* redzone */
3739 /* This is the X/Open sanctioned signal stack switching. */
3740 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3741 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3744 sp = align_sigframe(sp - frame_size);
3747 * If we are on the alternate signal stack and would overflow it, don't.
3748 * Return an always-bogus address instead so we will die with SIGSEGV.
3751 if (onsigstack && !likely(on_sig_stack(sp))) {
3752 return -1L;
3755 return sp;
3758 static void setup_frame(int sig, struct target_sigaction *ka,
3759 target_sigset_t *set, CPUOpenRISCState *env)
3761 qemu_log("Not implement.\n");
3764 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3765 target_siginfo_t *info,
3766 target_sigset_t *set, CPUOpenRISCState *env)
3768 int err = 0;
3769 abi_ulong frame_addr;
3770 unsigned long return_ip;
3771 struct target_rt_sigframe *frame;
3772 abi_ulong info_addr, uc_addr;
3774 frame_addr = get_sigframe(ka, env, sizeof *frame);
3776 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3777 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3778 goto give_sigsegv;
3781 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3782 err |= __put_user(info_addr, &frame->pinfo);
3783 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3784 err |= __put_user(uc_addr, &frame->puc);
3786 if (ka->sa_flags & SA_SIGINFO) {
3787 err |= copy_siginfo_to_user(&frame->info, info);
3789 if (err) {
3790 goto give_sigsegv;
3793 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3794 err |= __put_user(0, &frame->uc.tuc_flags);
3795 err |= __put_user(0, &frame->uc.tuc_link);
3796 err |= __put_user(target_sigaltstack_used.ss_sp,
3797 &frame->uc.tuc_stack.ss_sp);
3798 err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3799 err |= __put_user(target_sigaltstack_used.ss_size,
3800 &frame->uc.tuc_stack.ss_size);
3801 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
3803 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3805 if (err) {
3806 goto give_sigsegv;
3809 /* trampoline - the desired return ip is the retcode itself */
3810 return_ip = (unsigned long)&frame->retcode;
3811 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3812 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
3813 err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3814 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3815 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3817 if (err) {
3818 goto give_sigsegv;
3821 /* TODO what is the current->exec_domain stuff and invmap ? */
3823 /* Set up registers for signal handler */
3824 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3825 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3826 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3827 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3828 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3830 /* actually move the usp to reflect the stacked frame */
3831 env->gpr[1] = (unsigned long)frame;
3833 return;
3835 give_sigsegv:
3836 unlock_user_struct(frame, frame_addr, 1);
3837 if (sig == TARGET_SIGSEGV) {
3838 ka->_sa_handler = TARGET_SIG_DFL;
3840 force_sig(TARGET_SIGSEGV);
3843 long do_sigreturn(CPUOpenRISCState *env)
3846 qemu_log("do_sigreturn: not implemented\n");
3847 return -TARGET_ENOSYS;
3850 long do_rt_sigreturn(CPUOpenRISCState *env)
3852 qemu_log("do_rt_sigreturn: not implemented\n");
3853 return -TARGET_ENOSYS;
3855 /* TARGET_OPENRISC */
3857 #elif defined(TARGET_S390X)
3859 #define __NUM_GPRS 16
3860 #define __NUM_FPRS 16
3861 #define __NUM_ACRS 16
3863 #define S390_SYSCALL_SIZE 2
3864 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3866 #define _SIGCONTEXT_NSIG 64
3867 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3868 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3869 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3870 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
3871 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3873 typedef struct {
3874 target_psw_t psw;
3875 target_ulong gprs[__NUM_GPRS];
3876 unsigned int acrs[__NUM_ACRS];
3877 } target_s390_regs_common;
3879 typedef struct {
3880 unsigned int fpc;
3881 double fprs[__NUM_FPRS];
3882 } target_s390_fp_regs;
3884 typedef struct {
3885 target_s390_regs_common regs;
3886 target_s390_fp_regs fpregs;
3887 } target_sigregs;
3889 struct target_sigcontext {
3890 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
3891 target_sigregs *sregs;
3894 typedef struct {
3895 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3896 struct target_sigcontext sc;
3897 target_sigregs sregs;
3898 int signo;
3899 uint8_t retcode[S390_SYSCALL_SIZE];
3900 } sigframe;
3902 struct target_ucontext {
3903 target_ulong tuc_flags;
3904 struct target_ucontext *tuc_link;
3905 target_stack_t tuc_stack;
3906 target_sigregs tuc_mcontext;
3907 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3910 typedef struct {
3911 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3912 uint8_t retcode[S390_SYSCALL_SIZE];
3913 struct target_siginfo info;
3914 struct target_ucontext uc;
3915 } rt_sigframe;
3917 static inline abi_ulong
3918 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
3920 abi_ulong sp;
3922 /* Default to using normal stack */
3923 sp = env->regs[15];
3925 /* This is the X/Open sanctioned signal stack switching. */
3926 if (ka->sa_flags & TARGET_SA_ONSTACK) {
3927 if (!sas_ss_flags(sp)) {
3928 sp = target_sigaltstack_used.ss_sp +
3929 target_sigaltstack_used.ss_size;
3933 /* This is the legacy signal stack switching. */
3934 else if (/* FIXME !user_mode(regs) */ 0 &&
3935 !(ka->sa_flags & TARGET_SA_RESTORER) &&
3936 ka->sa_restorer) {
3937 sp = (abi_ulong) ka->sa_restorer;
3940 return (sp - frame_size) & -8ul;
3943 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
3945 int i;
3946 //save_access_regs(current->thread.acrs); FIXME
3948 /* Copy a 'clean' PSW mask to the user to avoid leaking
3949 information about whether PER is currently on. */
3950 __put_user(env->psw.mask, &sregs->regs.psw.mask);
3951 __put_user(env->psw.addr, &sregs->regs.psw.addr);
3952 for (i = 0; i < 16; i++) {
3953 __put_user(env->regs[i], &sregs->regs.gprs[i]);
3955 for (i = 0; i < 16; i++) {
3956 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3959 * We have to store the fp registers to current->thread.fp_regs
3960 * to merge them with the emulated registers.
3962 //save_fp_regs(&current->thread.fp_regs); FIXME
3963 for (i = 0; i < 16; i++) {
3964 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3968 static void setup_frame(int sig, struct target_sigaction *ka,
3969 target_sigset_t *set, CPUS390XState *env)
3971 sigframe *frame;
3972 abi_ulong frame_addr;
3974 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3975 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3976 (unsigned long long)frame_addr);
3977 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3978 goto give_sigsegv;
3981 qemu_log("%s: 1\n", __FUNCTION__);
3982 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
3983 goto give_sigsegv;
3986 save_sigregs(env, &frame->sregs);
3988 __put_user((abi_ulong)(unsigned long)&frame->sregs,
3989 (abi_ulong *)&frame->sc.sregs);
3991 /* Set up to return from userspace. If provided, use a stub
3992 already in userspace. */
3993 if (ka->sa_flags & TARGET_SA_RESTORER) {
3994 env->regs[14] = (unsigned long)
3995 ka->sa_restorer | PSW_ADDR_AMODE;
3996 } else {
3997 env->regs[14] = (unsigned long)
3998 frame->retcode | PSW_ADDR_AMODE;
3999 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4000 (uint16_t *)(frame->retcode)))
4001 goto give_sigsegv;
4004 /* Set up backchain. */
4005 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4006 goto give_sigsegv;
4009 /* Set up registers for signal handler */
4010 env->regs[15] = frame_addr;
4011 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4013 env->regs[2] = sig; //map_signal(sig);
4014 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4016 /* We forgot to include these in the sigcontext.
4017 To avoid breaking binary compatibility, they are passed as args. */
4018 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4019 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4021 /* Place signal number on stack to allow backtrace from handler. */
4022 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4023 goto give_sigsegv;
4025 unlock_user_struct(frame, frame_addr, 1);
4026 return;
4028 give_sigsegv:
4029 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4030 unlock_user_struct(frame, frame_addr, 1);
4031 force_sig(TARGET_SIGSEGV);
4034 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4035 target_siginfo_t *info,
4036 target_sigset_t *set, CPUS390XState *env)
4038 int i;
4039 rt_sigframe *frame;
4040 abi_ulong frame_addr;
4042 frame_addr = get_sigframe(ka, env, sizeof *frame);
4043 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4044 (unsigned long long)frame_addr);
4045 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4046 goto give_sigsegv;
4049 qemu_log("%s: 1\n", __FUNCTION__);
4050 if (copy_siginfo_to_user(&frame->info, info)) {
4051 goto give_sigsegv;
4054 /* Create the ucontext. */
4055 __put_user(0, &frame->uc.tuc_flags);
4056 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4057 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4058 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4059 &frame->uc.tuc_stack.ss_flags);
4060 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4061 save_sigregs(env, &frame->uc.tuc_mcontext);
4062 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4063 __put_user((abi_ulong)set->sig[i],
4064 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4067 /* Set up to return from userspace. If provided, use a stub
4068 already in userspace. */
4069 if (ka->sa_flags & TARGET_SA_RESTORER) {
4070 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4071 } else {
4072 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4073 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4074 (uint16_t *)(frame->retcode))) {
4075 goto give_sigsegv;
4079 /* Set up backchain. */
4080 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4081 goto give_sigsegv;
4084 /* Set up registers for signal handler */
4085 env->regs[15] = frame_addr;
4086 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4088 env->regs[2] = sig; //map_signal(sig);
4089 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4090 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4091 return;
4093 give_sigsegv:
4094 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4095 unlock_user_struct(frame, frame_addr, 1);
4096 force_sig(TARGET_SIGSEGV);
4099 static int
4100 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4102 int err = 0;
4103 int i;
4105 for (i = 0; i < 16; i++) {
4106 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4109 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4110 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4111 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4112 (unsigned long long)env->psw.addr);
4113 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4114 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4116 for (i = 0; i < 16; i++) {
4117 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4119 for (i = 0; i < 16; i++) {
4120 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4123 return err;
4126 long do_sigreturn(CPUS390XState *env)
4128 sigframe *frame;
4129 abi_ulong frame_addr = env->regs[15];
4130 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4131 (unsigned long long)frame_addr);
4132 target_sigset_t target_set;
4133 sigset_t set;
4135 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4136 goto badframe;
4138 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4139 goto badframe;
4142 target_to_host_sigset_internal(&set, &target_set);
4143 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4145 if (restore_sigregs(env, &frame->sregs)) {
4146 goto badframe;
4149 unlock_user_struct(frame, frame_addr, 0);
4150 return env->regs[2];
4152 badframe:
4153 unlock_user_struct(frame, frame_addr, 0);
4154 force_sig(TARGET_SIGSEGV);
4155 return 0;
4158 long do_rt_sigreturn(CPUS390XState *env)
4160 rt_sigframe *frame;
4161 abi_ulong frame_addr = env->regs[15];
4162 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4163 (unsigned long long)frame_addr);
4164 sigset_t set;
4166 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4167 goto badframe;
4169 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4171 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4173 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4174 goto badframe;
4177 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4178 get_sp_from_cpustate(env)) == -EFAULT) {
4179 goto badframe;
4181 unlock_user_struct(frame, frame_addr, 0);
4182 return env->regs[2];
4184 badframe:
4185 unlock_user_struct(frame, frame_addr, 0);
4186 force_sig(TARGET_SIGSEGV);
4187 return 0;
4190 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4192 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4193 the signal handling is different enough that we haven't implemented
4194 support for PPC64 yet. Hence the restriction above.
4196 There are various #if'd blocks for code for TARGET_PPC64. These
4197 blocks should go away so that we can successfully run 32-bit and
4198 64-bit binaries on a QEMU configured for PPC64. */
4200 /* Size of dummy stack frame allocated when calling signal handler.
4201 See arch/powerpc/include/asm/ptrace.h. */
4202 #if defined(TARGET_PPC64)
4203 #define SIGNAL_FRAMESIZE 128
4204 #else
4205 #define SIGNAL_FRAMESIZE 64
4206 #endif
4208 /* See arch/powerpc/include/asm/sigcontext.h. */
4209 struct target_sigcontext {
4210 target_ulong _unused[4];
4211 int32_t signal;
4212 #if defined(TARGET_PPC64)
4213 int32_t pad0;
4214 #endif
4215 target_ulong handler;
4216 target_ulong oldmask;
4217 target_ulong regs; /* struct pt_regs __user * */
4218 /* TODO: PPC64 includes extra bits here. */
4221 /* Indices for target_mcontext.mc_gregs, below.
4222 See arch/powerpc/include/asm/ptrace.h for details. */
4223 enum {
4224 TARGET_PT_R0 = 0,
4225 TARGET_PT_R1 = 1,
4226 TARGET_PT_R2 = 2,
4227 TARGET_PT_R3 = 3,
4228 TARGET_PT_R4 = 4,
4229 TARGET_PT_R5 = 5,
4230 TARGET_PT_R6 = 6,
4231 TARGET_PT_R7 = 7,
4232 TARGET_PT_R8 = 8,
4233 TARGET_PT_R9 = 9,
4234 TARGET_PT_R10 = 10,
4235 TARGET_PT_R11 = 11,
4236 TARGET_PT_R12 = 12,
4237 TARGET_PT_R13 = 13,
4238 TARGET_PT_R14 = 14,
4239 TARGET_PT_R15 = 15,
4240 TARGET_PT_R16 = 16,
4241 TARGET_PT_R17 = 17,
4242 TARGET_PT_R18 = 18,
4243 TARGET_PT_R19 = 19,
4244 TARGET_PT_R20 = 20,
4245 TARGET_PT_R21 = 21,
4246 TARGET_PT_R22 = 22,
4247 TARGET_PT_R23 = 23,
4248 TARGET_PT_R24 = 24,
4249 TARGET_PT_R25 = 25,
4250 TARGET_PT_R26 = 26,
4251 TARGET_PT_R27 = 27,
4252 TARGET_PT_R28 = 28,
4253 TARGET_PT_R29 = 29,
4254 TARGET_PT_R30 = 30,
4255 TARGET_PT_R31 = 31,
4256 TARGET_PT_NIP = 32,
4257 TARGET_PT_MSR = 33,
4258 TARGET_PT_ORIG_R3 = 34,
4259 TARGET_PT_CTR = 35,
4260 TARGET_PT_LNK = 36,
4261 TARGET_PT_XER = 37,
4262 TARGET_PT_CCR = 38,
4263 /* Yes, there are two registers with #39. One is 64-bit only. */
4264 TARGET_PT_MQ = 39,
4265 TARGET_PT_SOFTE = 39,
4266 TARGET_PT_TRAP = 40,
4267 TARGET_PT_DAR = 41,
4268 TARGET_PT_DSISR = 42,
4269 TARGET_PT_RESULT = 43,
4270 TARGET_PT_REGS_COUNT = 44
4273 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4274 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4275 struct target_mcontext {
4276 target_ulong mc_gregs[48];
4277 /* Includes fpscr. */
4278 uint64_t mc_fregs[33];
4279 target_ulong mc_pad[2];
4280 /* We need to handle Altivec and SPE at the same time, which no
4281 kernel needs to do. Fortunately, the kernel defines this bit to
4282 be Altivec-register-large all the time, rather than trying to
4283 twiddle it based on the specific platform. */
4284 union {
4285 /* SPE vector registers. One extra for SPEFSCR. */
4286 uint32_t spe[33];
4287 /* Altivec vector registers. The packing of VSCR and VRSAVE
4288 varies depending on whether we're PPC64 or not: PPC64 splits
4289 them apart; PPC32 stuffs them together. */
4290 #if defined(TARGET_PPC64)
4291 #define QEMU_NVRREG 34
4292 #else
4293 #define QEMU_NVRREG 33
4294 #endif
4295 ppc_avr_t altivec[QEMU_NVRREG];
4296 #undef QEMU_NVRREG
4297 } mc_vregs __attribute__((__aligned__(16)));
4300 struct target_ucontext {
4301 target_ulong tuc_flags;
4302 target_ulong tuc_link; /* struct ucontext __user * */
4303 struct target_sigaltstack tuc_stack;
4304 #if !defined(TARGET_PPC64)
4305 int32_t tuc_pad[7];
4306 target_ulong tuc_regs; /* struct mcontext __user *
4307 points to uc_mcontext field */
4308 #endif
4309 target_sigset_t tuc_sigmask;
4310 #if defined(TARGET_PPC64)
4311 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4312 struct target_sigcontext tuc_mcontext;
4313 #else
4314 int32_t tuc_maskext[30];
4315 int32_t tuc_pad2[3];
4316 struct target_mcontext tuc_mcontext;
4317 #endif
4320 /* See arch/powerpc/kernel/signal_32.c. */
4321 struct target_sigframe {
4322 struct target_sigcontext sctx;
4323 struct target_mcontext mctx;
4324 int32_t abigap[56];
4327 struct target_rt_sigframe {
4328 struct target_siginfo info;
4329 struct target_ucontext uc;
4330 int32_t abigap[56];
4333 /* We use the mc_pad field for the signal return trampoline. */
4334 #define tramp mc_pad
4336 /* See arch/powerpc/kernel/signal.c. */
4337 static target_ulong get_sigframe(struct target_sigaction *ka,
4338 CPUPPCState *env,
4339 int frame_size)
4341 target_ulong oldsp, newsp;
4343 oldsp = env->gpr[1];
4345 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4346 (sas_ss_flags(oldsp) == 0)) {
4347 oldsp = (target_sigaltstack_used.ss_sp
4348 + target_sigaltstack_used.ss_size);
4351 newsp = (oldsp - frame_size) & ~0xFUL;
4353 return newsp;
4356 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4357 int sigret)
4359 target_ulong msr = env->msr;
4360 int i;
4361 target_ulong ccr = 0;
4363 /* In general, the kernel attempts to be intelligent about what it
4364 needs to save for Altivec/FP/SPE registers. We don't care that
4365 much, so we just go ahead and save everything. */
4367 /* Save general registers. */
4368 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4369 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4370 return 1;
4373 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4374 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4375 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4376 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4377 return 1;
4379 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4380 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4382 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4383 return 1;
4385 /* Save Altivec registers if necessary. */
4386 if (env->insns_flags & PPC_ALTIVEC) {
4387 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4388 ppc_avr_t *avr = &env->avr[i];
4389 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4391 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4392 __put_user(avr->u64[1], &vreg->u64[1])) {
4393 return 1;
4396 /* Set MSR_VR in the saved MSR value to indicate that
4397 frame->mc_vregs contains valid data. */
4398 msr |= MSR_VR;
4399 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4400 &frame->mc_vregs.altivec[32].u32[3]))
4401 return 1;
4404 /* Save floating point registers. */
4405 if (env->insns_flags & PPC_FLOAT) {
4406 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4407 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4408 return 1;
4411 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4412 return 1;
4415 /* Save SPE registers. The kernel only saves the high half. */
4416 if (env->insns_flags & PPC_SPE) {
4417 #if defined(TARGET_PPC64)
4418 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4419 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4420 return 1;
4423 #else
4424 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4425 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4426 return 1;
4429 #endif
4430 /* Set MSR_SPE in the saved MSR value to indicate that
4431 frame->mc_vregs contains valid data. */
4432 msr |= MSR_SPE;
4433 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4434 return 1;
4437 /* Store MSR. */
4438 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4439 return 1;
4441 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4442 if (sigret) {
4443 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4444 __put_user(0x44000002UL, &frame->tramp[1])) {
4445 return 1;
4449 return 0;
4452 static int restore_user_regs(CPUPPCState *env,
4453 struct target_mcontext *frame, int sig)
4455 target_ulong save_r2 = 0;
4456 target_ulong msr;
4457 target_ulong ccr;
4459 int i;
4461 if (!sig) {
4462 save_r2 = env->gpr[2];
4465 /* Restore general registers. */
4466 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4467 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4468 return 1;
4471 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4472 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4473 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4474 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4475 return 1;
4476 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4477 return 1;
4479 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4480 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4483 if (!sig) {
4484 env->gpr[2] = save_r2;
4486 /* Restore MSR. */
4487 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4488 return 1;
4490 /* If doing signal return, restore the previous little-endian mode. */
4491 if (sig)
4492 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4494 /* Restore Altivec registers if necessary. */
4495 if (env->insns_flags & PPC_ALTIVEC) {
4496 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4497 ppc_avr_t *avr = &env->avr[i];
4498 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4500 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4501 __get_user(avr->u64[1], &vreg->u64[1])) {
4502 return 1;
4505 /* Set MSR_VEC in the saved MSR value to indicate that
4506 frame->mc_vregs contains valid data. */
4507 if (__get_user(env->spr[SPR_VRSAVE],
4508 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4509 return 1;
4512 /* Restore floating point registers. */
4513 if (env->insns_flags & PPC_FLOAT) {
4514 uint64_t fpscr;
4515 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4516 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4517 return 1;
4520 if (__get_user(fpscr, &frame->mc_fregs[32]))
4521 return 1;
4522 env->fpscr = (uint32_t) fpscr;
4525 /* Save SPE registers. The kernel only saves the high half. */
4526 if (env->insns_flags & PPC_SPE) {
4527 #if defined(TARGET_PPC64)
4528 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4529 uint32_t hi;
4531 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4532 return 1;
4534 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4536 #else
4537 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4538 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4539 return 1;
4542 #endif
4543 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4544 return 1;
4547 return 0;
4550 static void setup_frame(int sig, struct target_sigaction *ka,
4551 target_sigset_t *set, CPUPPCState *env)
4553 struct target_sigframe *frame;
4554 struct target_sigcontext *sc;
4555 target_ulong frame_addr, newsp;
4556 int err = 0;
4557 int signal;
4559 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4560 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4561 goto sigsegv;
4562 sc = &frame->sctx;
4564 signal = current_exec_domain_sig(sig);
4566 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
4567 err |= __put_user(set->sig[0], &sc->oldmask);
4568 #if defined(TARGET_PPC64)
4569 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4570 #else
4571 err |= __put_user(set->sig[1], &sc->_unused[3]);
4572 #endif
4573 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4574 err |= __put_user(sig, &sc->signal);
4576 /* Save user regs. */
4577 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4579 /* The kernel checks for the presence of a VDSO here. We don't
4580 emulate a vdso, so use a sigreturn system call. */
4581 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4583 /* Turn off all fp exceptions. */
4584 env->fpscr = 0;
4586 /* Create a stack frame for the caller of the handler. */
4587 newsp = frame_addr - SIGNAL_FRAMESIZE;
4588 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4590 if (err)
4591 goto sigsegv;
4593 /* Set up registers for signal handler. */
4594 env->gpr[1] = newsp;
4595 env->gpr[3] = signal;
4596 env->gpr[4] = (target_ulong) h2g(sc);
4597 env->nip = (target_ulong) ka->_sa_handler;
4598 /* Signal handlers are entered in big-endian mode. */
4599 env->msr &= ~MSR_LE;
4601 unlock_user_struct(frame, frame_addr, 1);
4602 return;
4604 sigsegv:
4605 unlock_user_struct(frame, frame_addr, 1);
4606 qemu_log("segfaulting from setup_frame\n");
4607 force_sig(TARGET_SIGSEGV);
4610 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4611 target_siginfo_t *info,
4612 target_sigset_t *set, CPUPPCState *env)
4614 struct target_rt_sigframe *rt_sf;
4615 struct target_mcontext *frame;
4616 target_ulong rt_sf_addr, newsp = 0;
4617 int i, err = 0;
4618 int signal;
4620 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4621 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4622 goto sigsegv;
4624 signal = current_exec_domain_sig(sig);
4626 err |= copy_siginfo_to_user(&rt_sf->info, info);
4628 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4629 err |= __put_user(0, &rt_sf->uc.tuc_link);
4630 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4631 &rt_sf->uc.tuc_stack.ss_sp);
4632 err |= __put_user(sas_ss_flags(env->gpr[1]),
4633 &rt_sf->uc.tuc_stack.ss_flags);
4634 err |= __put_user(target_sigaltstack_used.ss_size,
4635 &rt_sf->uc.tuc_stack.ss_size);
4636 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4637 &rt_sf->uc.tuc_regs);
4638 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4639 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4642 frame = &rt_sf->uc.tuc_mcontext;
4643 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4645 /* The kernel checks for the presence of a VDSO here. We don't
4646 emulate a vdso, so use a sigreturn system call. */
4647 env->lr = (target_ulong) h2g(frame->tramp);
4649 /* Turn off all fp exceptions. */
4650 env->fpscr = 0;
4652 /* Create a stack frame for the caller of the handler. */
4653 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4654 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4656 if (err)
4657 goto sigsegv;
4659 /* Set up registers for signal handler. */
4660 env->gpr[1] = newsp;
4661 env->gpr[3] = (target_ulong) signal;
4662 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4663 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4664 env->gpr[6] = (target_ulong) h2g(rt_sf);
4665 env->nip = (target_ulong) ka->_sa_handler;
4666 /* Signal handlers are entered in big-endian mode. */
4667 env->msr &= ~MSR_LE;
4669 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4670 return;
4672 sigsegv:
4673 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4674 qemu_log("segfaulting from setup_rt_frame\n");
4675 force_sig(TARGET_SIGSEGV);
4679 long do_sigreturn(CPUPPCState *env)
4681 struct target_sigcontext *sc = NULL;
4682 struct target_mcontext *sr = NULL;
4683 target_ulong sr_addr, sc_addr;
4684 sigset_t blocked;
4685 target_sigset_t set;
4687 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4688 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4689 goto sigsegv;
4691 #if defined(TARGET_PPC64)
4692 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4693 #else
4694 if(__get_user(set.sig[0], &sc->oldmask) ||
4695 __get_user(set.sig[1], &sc->_unused[3]))
4696 goto sigsegv;
4697 #endif
4698 target_to_host_sigset_internal(&blocked, &set);
4699 sigprocmask(SIG_SETMASK, &blocked, NULL);
4701 if (__get_user(sr_addr, &sc->regs))
4702 goto sigsegv;
4703 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4704 goto sigsegv;
4705 if (restore_user_regs(env, sr, 1))
4706 goto sigsegv;
4708 unlock_user_struct(sr, sr_addr, 1);
4709 unlock_user_struct(sc, sc_addr, 1);
4710 return -TARGET_QEMU_ESIGRETURN;
4712 sigsegv:
4713 unlock_user_struct(sr, sr_addr, 1);
4714 unlock_user_struct(sc, sc_addr, 1);
4715 qemu_log("segfaulting from do_sigreturn\n");
4716 force_sig(TARGET_SIGSEGV);
4717 return 0;
4720 /* See arch/powerpc/kernel/signal_32.c. */
4721 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4723 struct target_mcontext *mcp;
4724 target_ulong mcp_addr;
4725 sigset_t blocked;
4726 target_sigset_t set;
4728 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4729 sizeof (set)))
4730 return 1;
4732 #if defined(TARGET_PPC64)
4733 fprintf (stderr, "do_setcontext: not implemented\n");
4734 return 0;
4735 #else
4736 if (__get_user(mcp_addr, &ucp->tuc_regs))
4737 return 1;
4739 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4740 return 1;
4742 target_to_host_sigset_internal(&blocked, &set);
4743 sigprocmask(SIG_SETMASK, &blocked, NULL);
4744 if (restore_user_regs(env, mcp, sig))
4745 goto sigsegv;
4747 unlock_user_struct(mcp, mcp_addr, 1);
4748 return 0;
4750 sigsegv:
4751 unlock_user_struct(mcp, mcp_addr, 1);
4752 return 1;
4753 #endif
4756 long do_rt_sigreturn(CPUPPCState *env)
4758 struct target_rt_sigframe *rt_sf = NULL;
4759 target_ulong rt_sf_addr;
4761 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4762 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4763 goto sigsegv;
4765 if (do_setcontext(&rt_sf->uc, env, 1))
4766 goto sigsegv;
4768 do_sigaltstack(rt_sf_addr
4769 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4770 0, env->gpr[1]);
4772 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4773 return -TARGET_QEMU_ESIGRETURN;
4775 sigsegv:
4776 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4777 qemu_log("segfaulting from do_rt_sigreturn\n");
4778 force_sig(TARGET_SIGSEGV);
4779 return 0;
4782 #elif defined(TARGET_M68K)
4784 struct target_sigcontext {
4785 abi_ulong sc_mask;
4786 abi_ulong sc_usp;
4787 abi_ulong sc_d0;
4788 abi_ulong sc_d1;
4789 abi_ulong sc_a0;
4790 abi_ulong sc_a1;
4791 unsigned short sc_sr;
4792 abi_ulong sc_pc;
4795 struct target_sigframe
4797 abi_ulong pretcode;
4798 int sig;
4799 int code;
4800 abi_ulong psc;
4801 char retcode[8];
4802 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4803 struct target_sigcontext sc;
4806 typedef int target_greg_t;
4807 #define TARGET_NGREG 18
4808 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4810 typedef struct target_fpregset {
4811 int f_fpcntl[3];
4812 int f_fpregs[8*3];
4813 } target_fpregset_t;
4815 struct target_mcontext {
4816 int version;
4817 target_gregset_t gregs;
4818 target_fpregset_t fpregs;
4821 #define TARGET_MCONTEXT_VERSION 2
4823 struct target_ucontext {
4824 abi_ulong tuc_flags;
4825 abi_ulong tuc_link;
4826 target_stack_t tuc_stack;
4827 struct target_mcontext tuc_mcontext;
4828 abi_long tuc_filler[80];
4829 target_sigset_t tuc_sigmask;
4832 struct target_rt_sigframe
4834 abi_ulong pretcode;
4835 int sig;
4836 abi_ulong pinfo;
4837 abi_ulong puc;
4838 char retcode[8];
4839 struct target_siginfo info;
4840 struct target_ucontext uc;
4843 static int
4844 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4845 abi_ulong mask)
4847 int err = 0;
4849 err |= __put_user(mask, &sc->sc_mask);
4850 err |= __put_user(env->aregs[7], &sc->sc_usp);
4851 err |= __put_user(env->dregs[0], &sc->sc_d0);
4852 err |= __put_user(env->dregs[1], &sc->sc_d1);
4853 err |= __put_user(env->aregs[0], &sc->sc_a0);
4854 err |= __put_user(env->aregs[1], &sc->sc_a1);
4855 err |= __put_user(env->sr, &sc->sc_sr);
4856 err |= __put_user(env->pc, &sc->sc_pc);
4858 return err;
4861 static int
4862 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
4864 int err = 0;
4865 int temp;
4867 err |= __get_user(env->aregs[7], &sc->sc_usp);
4868 err |= __get_user(env->dregs[1], &sc->sc_d1);
4869 err |= __get_user(env->aregs[0], &sc->sc_a0);
4870 err |= __get_user(env->aregs[1], &sc->sc_a1);
4871 err |= __get_user(env->pc, &sc->sc_pc);
4872 err |= __get_user(temp, &sc->sc_sr);
4873 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4875 *pd0 = tswapl(sc->sc_d0);
4877 return err;
4881 * Determine which stack to use..
4883 static inline abi_ulong
4884 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
4885 size_t frame_size)
4887 unsigned long sp;
4889 sp = regs->aregs[7];
4891 /* This is the X/Open sanctioned signal stack switching. */
4892 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4893 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4896 return ((sp - frame_size) & -8UL);
4899 static void setup_frame(int sig, struct target_sigaction *ka,
4900 target_sigset_t *set, CPUM68KState *env)
4902 struct target_sigframe *frame;
4903 abi_ulong frame_addr;
4904 abi_ulong retcode_addr;
4905 abi_ulong sc_addr;
4906 int err = 0;
4907 int i;
4909 frame_addr = get_sigframe(ka, env, sizeof *frame);
4910 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4911 goto give_sigsegv;
4913 err |= __put_user(sig, &frame->sig);
4915 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4916 err |= __put_user(sc_addr, &frame->psc);
4918 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4919 if (err)
4920 goto give_sigsegv;
4922 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4923 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4924 goto give_sigsegv;
4927 /* Set up to return from userspace. */
4929 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4930 err |= __put_user(retcode_addr, &frame->pretcode);
4932 /* moveq #,d0; trap #0 */
4934 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4935 (long *)(frame->retcode));
4937 if (err)
4938 goto give_sigsegv;
4940 /* Set up to return from userspace */
4942 env->aregs[7] = frame_addr;
4943 env->pc = ka->_sa_handler;
4945 unlock_user_struct(frame, frame_addr, 1);
4946 return;
4948 give_sigsegv:
4949 unlock_user_struct(frame, frame_addr, 1);
4950 force_sig(TARGET_SIGSEGV);
4953 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4954 CPUM68KState *env)
4956 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4957 int err;
4959 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4960 err |= __put_user(env->dregs[0], &gregs[0]);
4961 err |= __put_user(env->dregs[1], &gregs[1]);
4962 err |= __put_user(env->dregs[2], &gregs[2]);
4963 err |= __put_user(env->dregs[3], &gregs[3]);
4964 err |= __put_user(env->dregs[4], &gregs[4]);
4965 err |= __put_user(env->dregs[5], &gregs[5]);
4966 err |= __put_user(env->dregs[6], &gregs[6]);
4967 err |= __put_user(env->dregs[7], &gregs[7]);
4968 err |= __put_user(env->aregs[0], &gregs[8]);
4969 err |= __put_user(env->aregs[1], &gregs[9]);
4970 err |= __put_user(env->aregs[2], &gregs[10]);
4971 err |= __put_user(env->aregs[3], &gregs[11]);
4972 err |= __put_user(env->aregs[4], &gregs[12]);
4973 err |= __put_user(env->aregs[5], &gregs[13]);
4974 err |= __put_user(env->aregs[6], &gregs[14]);
4975 err |= __put_user(env->aregs[7], &gregs[15]);
4976 err |= __put_user(env->pc, &gregs[16]);
4977 err |= __put_user(env->sr, &gregs[17]);
4979 return err;
4982 static inline int target_rt_restore_ucontext(CPUM68KState *env,
4983 struct target_ucontext *uc,
4984 int *pd0)
4986 int temp;
4987 int err;
4988 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4990 err = __get_user(temp, &uc->tuc_mcontext.version);
4991 if (temp != TARGET_MCONTEXT_VERSION)
4992 goto badframe;
4994 /* restore passed registers */
4995 err |= __get_user(env->dregs[0], &gregs[0]);
4996 err |= __get_user(env->dregs[1], &gregs[1]);
4997 err |= __get_user(env->dregs[2], &gregs[2]);
4998 err |= __get_user(env->dregs[3], &gregs[3]);
4999 err |= __get_user(env->dregs[4], &gregs[4]);
5000 err |= __get_user(env->dregs[5], &gregs[5]);
5001 err |= __get_user(env->dregs[6], &gregs[6]);
5002 err |= __get_user(env->dregs[7], &gregs[7]);
5003 err |= __get_user(env->aregs[0], &gregs[8]);
5004 err |= __get_user(env->aregs[1], &gregs[9]);
5005 err |= __get_user(env->aregs[2], &gregs[10]);
5006 err |= __get_user(env->aregs[3], &gregs[11]);
5007 err |= __get_user(env->aregs[4], &gregs[12]);
5008 err |= __get_user(env->aregs[5], &gregs[13]);
5009 err |= __get_user(env->aregs[6], &gregs[14]);
5010 err |= __get_user(env->aregs[7], &gregs[15]);
5011 err |= __get_user(env->pc, &gregs[16]);
5012 err |= __get_user(temp, &gregs[17]);
5013 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5015 *pd0 = env->dregs[0];
5016 return err;
5018 badframe:
5019 return 1;
5022 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5023 target_siginfo_t *info,
5024 target_sigset_t *set, CPUM68KState *env)
5026 struct target_rt_sigframe *frame;
5027 abi_ulong frame_addr;
5028 abi_ulong retcode_addr;
5029 abi_ulong info_addr;
5030 abi_ulong uc_addr;
5031 int err = 0;
5032 int i;
5034 frame_addr = get_sigframe(ka, env, sizeof *frame);
5035 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5036 goto give_sigsegv;
5038 err |= __put_user(sig, &frame->sig);
5040 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5041 err |= __put_user(info_addr, &frame->pinfo);
5043 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5044 err |= __put_user(uc_addr, &frame->puc);
5046 err |= copy_siginfo_to_user(&frame->info, info);
5048 /* Create the ucontext */
5050 err |= __put_user(0, &frame->uc.tuc_flags);
5051 err |= __put_user(0, &frame->uc.tuc_link);
5052 err |= __put_user(target_sigaltstack_used.ss_sp,
5053 &frame->uc.tuc_stack.ss_sp);
5054 err |= __put_user(sas_ss_flags(env->aregs[7]),
5055 &frame->uc.tuc_stack.ss_flags);
5056 err |= __put_user(target_sigaltstack_used.ss_size,
5057 &frame->uc.tuc_stack.ss_size);
5058 err |= target_rt_setup_ucontext(&frame->uc, env);
5060 if (err)
5061 goto give_sigsegv;
5063 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5064 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
5065 goto give_sigsegv;
5068 /* Set up to return from userspace. */
5070 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5071 err |= __put_user(retcode_addr, &frame->pretcode);
5073 /* moveq #,d0; notb d0; trap #0 */
5075 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5076 (long *)(frame->retcode + 0));
5077 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5079 if (err)
5080 goto give_sigsegv;
5082 /* Set up to return from userspace */
5084 env->aregs[7] = frame_addr;
5085 env->pc = ka->_sa_handler;
5087 unlock_user_struct(frame, frame_addr, 1);
5088 return;
5090 give_sigsegv:
5091 unlock_user_struct(frame, frame_addr, 1);
5092 force_sig(TARGET_SIGSEGV);
5095 long do_sigreturn(CPUM68KState *env)
5097 struct target_sigframe *frame;
5098 abi_ulong frame_addr = env->aregs[7] - 4;
5099 target_sigset_t target_set;
5100 sigset_t set;
5101 int d0, i;
5103 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5104 goto badframe;
5106 /* set blocked signals */
5108 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5109 goto badframe;
5111 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5112 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5113 goto badframe;
5116 target_to_host_sigset_internal(&set, &target_set);
5117 sigprocmask(SIG_SETMASK, &set, NULL);
5119 /* restore registers */
5121 if (restore_sigcontext(env, &frame->sc, &d0))
5122 goto badframe;
5124 unlock_user_struct(frame, frame_addr, 0);
5125 return d0;
5127 badframe:
5128 unlock_user_struct(frame, frame_addr, 0);
5129 force_sig(TARGET_SIGSEGV);
5130 return 0;
5133 long do_rt_sigreturn(CPUM68KState *env)
5135 struct target_rt_sigframe *frame;
5136 abi_ulong frame_addr = env->aregs[7] - 4;
5137 target_sigset_t target_set;
5138 sigset_t set;
5139 int d0;
5141 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5142 goto badframe;
5144 target_to_host_sigset_internal(&set, &target_set);
5145 sigprocmask(SIG_SETMASK, &set, NULL);
5147 /* restore registers */
5149 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5150 goto badframe;
5152 if (do_sigaltstack(frame_addr +
5153 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5154 0, get_sp_from_cpustate(env)) == -EFAULT)
5155 goto badframe;
5157 unlock_user_struct(frame, frame_addr, 0);
5158 return d0;
5160 badframe:
5161 unlock_user_struct(frame, frame_addr, 0);
5162 force_sig(TARGET_SIGSEGV);
5163 return 0;
5166 #elif defined(TARGET_ALPHA)
5168 struct target_sigcontext {
5169 abi_long sc_onstack;
5170 abi_long sc_mask;
5171 abi_long sc_pc;
5172 abi_long sc_ps;
5173 abi_long sc_regs[32];
5174 abi_long sc_ownedfp;
5175 abi_long sc_fpregs[32];
5176 abi_ulong sc_fpcr;
5177 abi_ulong sc_fp_control;
5178 abi_ulong sc_reserved1;
5179 abi_ulong sc_reserved2;
5180 abi_ulong sc_ssize;
5181 abi_ulong sc_sbase;
5182 abi_ulong sc_traparg_a0;
5183 abi_ulong sc_traparg_a1;
5184 abi_ulong sc_traparg_a2;
5185 abi_ulong sc_fp_trap_pc;
5186 abi_ulong sc_fp_trigger_sum;
5187 abi_ulong sc_fp_trigger_inst;
5190 struct target_ucontext {
5191 abi_ulong tuc_flags;
5192 abi_ulong tuc_link;
5193 abi_ulong tuc_osf_sigmask;
5194 target_stack_t tuc_stack;
5195 struct target_sigcontext tuc_mcontext;
5196 target_sigset_t tuc_sigmask;
5199 struct target_sigframe {
5200 struct target_sigcontext sc;
5201 unsigned int retcode[3];
5204 struct target_rt_sigframe {
5205 target_siginfo_t info;
5206 struct target_ucontext uc;
5207 unsigned int retcode[3];
5210 #define INSN_MOV_R30_R16 0x47fe0410
5211 #define INSN_LDI_R0 0x201f0000
5212 #define INSN_CALLSYS 0x00000083
5214 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5215 abi_ulong frame_addr, target_sigset_t *set)
5217 int i, err = 0;
5219 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5220 err |= __put_user(set->sig[0], &sc->sc_mask);
5221 err |= __put_user(env->pc, &sc->sc_pc);
5222 err |= __put_user(8, &sc->sc_ps);
5224 for (i = 0; i < 31; ++i) {
5225 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5227 err |= __put_user(0, &sc->sc_regs[31]);
5229 for (i = 0; i < 31; ++i) {
5230 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5232 err |= __put_user(0, &sc->sc_fpregs[31]);
5233 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5235 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5236 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5237 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5239 return err;
5242 static int restore_sigcontext(CPUAlphaState *env,
5243 struct target_sigcontext *sc)
5245 uint64_t fpcr;
5246 int i, err = 0;
5248 err |= __get_user(env->pc, &sc->sc_pc);
5250 for (i = 0; i < 31; ++i) {
5251 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5253 for (i = 0; i < 31; ++i) {
5254 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5257 err |= __get_user(fpcr, &sc->sc_fpcr);
5258 cpu_alpha_store_fpcr(env, fpcr);
5260 return err;
5263 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5264 CPUAlphaState *env,
5265 unsigned long framesize)
5267 abi_ulong sp = env->ir[IR_SP];
5269 /* This is the X/Open sanctioned signal stack switching. */
5270 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5271 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5273 return (sp - framesize) & -32;
5276 static void setup_frame(int sig, struct target_sigaction *ka,
5277 target_sigset_t *set, CPUAlphaState *env)
5279 abi_ulong frame_addr, r26;
5280 struct target_sigframe *frame;
5281 int err = 0;
5283 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5284 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5285 goto give_sigsegv;
5288 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5290 if (ka->sa_restorer) {
5291 r26 = ka->sa_restorer;
5292 } else {
5293 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5294 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5295 &frame->retcode[1]);
5296 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5297 /* imb() */
5298 r26 = frame_addr;
5301 unlock_user_struct(frame, frame_addr, 1);
5303 if (err) {
5304 give_sigsegv:
5305 if (sig == TARGET_SIGSEGV) {
5306 ka->_sa_handler = TARGET_SIG_DFL;
5308 force_sig(TARGET_SIGSEGV);
5311 env->ir[IR_RA] = r26;
5312 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5313 env->ir[IR_A0] = sig;
5314 env->ir[IR_A1] = 0;
5315 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5316 env->ir[IR_SP] = frame_addr;
5319 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5320 target_siginfo_t *info,
5321 target_sigset_t *set, CPUAlphaState *env)
5323 abi_ulong frame_addr, r26;
5324 struct target_rt_sigframe *frame;
5325 int i, err = 0;
5327 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5328 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5329 goto give_sigsegv;
5332 err |= copy_siginfo_to_user(&frame->info, info);
5334 err |= __put_user(0, &frame->uc.tuc_flags);
5335 err |= __put_user(0, &frame->uc.tuc_link);
5336 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5337 err |= __put_user(target_sigaltstack_used.ss_sp,
5338 &frame->uc.tuc_stack.ss_sp);
5339 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5340 &frame->uc.tuc_stack.ss_flags);
5341 err |= __put_user(target_sigaltstack_used.ss_size,
5342 &frame->uc.tuc_stack.ss_size);
5343 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5344 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5345 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5348 if (ka->sa_restorer) {
5349 r26 = ka->sa_restorer;
5350 } else {
5351 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5352 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5353 &frame->retcode[1]);
5354 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5355 /* imb(); */
5356 r26 = frame_addr;
5359 if (err) {
5360 give_sigsegv:
5361 if (sig == TARGET_SIGSEGV) {
5362 ka->_sa_handler = TARGET_SIG_DFL;
5364 force_sig(TARGET_SIGSEGV);
5367 env->ir[IR_RA] = r26;
5368 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5369 env->ir[IR_A0] = sig;
5370 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5371 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5372 env->ir[IR_SP] = frame_addr;
5375 long do_sigreturn(CPUAlphaState *env)
5377 struct target_sigcontext *sc;
5378 abi_ulong sc_addr = env->ir[IR_A0];
5379 target_sigset_t target_set;
5380 sigset_t set;
5382 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5383 goto badframe;
5386 target_sigemptyset(&target_set);
5387 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5388 goto badframe;
5391 target_to_host_sigset_internal(&set, &target_set);
5392 sigprocmask(SIG_SETMASK, &set, NULL);
5394 if (restore_sigcontext(env, sc)) {
5395 goto badframe;
5397 unlock_user_struct(sc, sc_addr, 0);
5398 return env->ir[IR_V0];
5400 badframe:
5401 unlock_user_struct(sc, sc_addr, 0);
5402 force_sig(TARGET_SIGSEGV);
5405 long do_rt_sigreturn(CPUAlphaState *env)
5407 abi_ulong frame_addr = env->ir[IR_A0];
5408 struct target_rt_sigframe *frame;
5409 sigset_t set;
5411 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5412 goto badframe;
5414 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5415 sigprocmask(SIG_SETMASK, &set, NULL);
5417 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5418 goto badframe;
5420 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5421 uc.tuc_stack),
5422 0, env->ir[IR_SP]) == -EFAULT) {
5423 goto badframe;
5426 unlock_user_struct(frame, frame_addr, 0);
5427 return env->ir[IR_V0];
5430 badframe:
5431 unlock_user_struct(frame, frame_addr, 0);
5432 force_sig(TARGET_SIGSEGV);
5435 #else
5437 static void setup_frame(int sig, struct target_sigaction *ka,
5438 target_sigset_t *set, CPUArchState *env)
5440 fprintf(stderr, "setup_frame: not implemented\n");
5443 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5444 target_siginfo_t *info,
5445 target_sigset_t *set, CPUArchState *env)
5447 fprintf(stderr, "setup_rt_frame: not implemented\n");
5450 long do_sigreturn(CPUArchState *env)
5452 fprintf(stderr, "do_sigreturn: not implemented\n");
5453 return -TARGET_ENOSYS;
5456 long do_rt_sigreturn(CPUArchState *env)
5458 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5459 return -TARGET_ENOSYS;
5462 #endif
5464 void process_pending_signals(CPUArchState *cpu_env)
5466 int sig;
5467 abi_ulong handler;
5468 sigset_t set, old_set;
5469 target_sigset_t target_old_set;
5470 struct emulated_sigtable *k;
5471 struct target_sigaction *sa;
5472 struct sigqueue *q;
5473 TaskState *ts = cpu_env->opaque;
5475 if (!ts->signal_pending)
5476 return;
5478 /* FIXME: This is not threadsafe. */
5479 k = ts->sigtab;
5480 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5481 if (k->pending)
5482 goto handle_signal;
5483 k++;
5485 /* if no signal is pending, just return */
5486 ts->signal_pending = 0;
5487 return;
5489 handle_signal:
5490 #ifdef DEBUG_SIGNAL
5491 fprintf(stderr, "qemu: process signal %d\n", sig);
5492 #endif
5493 /* dequeue signal */
5494 q = k->first;
5495 k->first = q->next;
5496 if (!k->first)
5497 k->pending = 0;
5499 sig = gdb_handlesig (cpu_env, sig);
5500 if (!sig) {
5501 sa = NULL;
5502 handler = TARGET_SIG_IGN;
5503 } else {
5504 sa = &sigact_table[sig - 1];
5505 handler = sa->_sa_handler;
5508 if (handler == TARGET_SIG_DFL) {
5509 /* default handler : ignore some signal. The other are job control or fatal */
5510 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5511 kill(getpid(),SIGSTOP);
5512 } else if (sig != TARGET_SIGCHLD &&
5513 sig != TARGET_SIGURG &&
5514 sig != TARGET_SIGWINCH &&
5515 sig != TARGET_SIGCONT) {
5516 force_sig(sig);
5518 } else if (handler == TARGET_SIG_IGN) {
5519 /* ignore sig */
5520 } else if (handler == TARGET_SIG_ERR) {
5521 force_sig(sig);
5522 } else {
5523 /* compute the blocked signals during the handler execution */
5524 target_to_host_sigset(&set, &sa->sa_mask);
5525 /* SA_NODEFER indicates that the current signal should not be
5526 blocked during the handler */
5527 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5528 sigaddset(&set, target_to_host_signal(sig));
5530 /* block signals in the handler using Linux */
5531 sigprocmask(SIG_BLOCK, &set, &old_set);
5532 /* save the previous blocked signal state to restore it at the
5533 end of the signal execution (see do_sigreturn) */
5534 host_to_target_sigset_internal(&target_old_set, &old_set);
5536 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5537 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5539 CPUX86State *env = cpu_env;
5540 if (env->eflags & VM_MASK)
5541 save_v86_state(env);
5543 #endif
5544 /* prepare the stack frame of the virtual CPU */
5545 if (sa->sa_flags & TARGET_SA_SIGINFO)
5546 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5547 else
5548 setup_frame(sig, sa, &target_old_set, cpu_env);
5549 if (sa->sa_flags & TARGET_SA_RESETHAND)
5550 sa->_sa_handler = TARGET_SIG_DFL;
5552 if (q != &k->info)
5553 free_sigqueue(cpu_env, q);