ARM: linux-user: Restore iWMMXT state from ucontext on sigreturn
[qemu.git] / linux-user / signal.c
blobc846b8c7d43a6edd220cc2de60972a609b982e60
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 <signal.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <sys/ucontext.h>
28 #include <sys/resource.h>
30 #include "qemu.h"
31 #include "qemu-common.h"
32 #include "target_signal.h"
34 //#define DEBUG_SIGNAL
36 static struct target_sigaltstack target_sigaltstack_used = {
37 .ss_sp = 0,
38 .ss_size = 0,
39 .ss_flags = TARGET_SS_DISABLE,
42 static struct target_sigaction sigact_table[TARGET_NSIG];
44 static void host_signal_handler(int host_signum, siginfo_t *info,
45 void *puc);
47 static uint8_t host_to_target_signal_table[_NSIG] = {
48 [SIGHUP] = TARGET_SIGHUP,
49 [SIGINT] = TARGET_SIGINT,
50 [SIGQUIT] = TARGET_SIGQUIT,
51 [SIGILL] = TARGET_SIGILL,
52 [SIGTRAP] = TARGET_SIGTRAP,
53 [SIGABRT] = TARGET_SIGABRT,
54 /* [SIGIOT] = TARGET_SIGIOT,*/
55 [SIGBUS] = TARGET_SIGBUS,
56 [SIGFPE] = TARGET_SIGFPE,
57 [SIGKILL] = TARGET_SIGKILL,
58 [SIGUSR1] = TARGET_SIGUSR1,
59 [SIGSEGV] = TARGET_SIGSEGV,
60 [SIGUSR2] = TARGET_SIGUSR2,
61 [SIGPIPE] = TARGET_SIGPIPE,
62 [SIGALRM] = TARGET_SIGALRM,
63 [SIGTERM] = TARGET_SIGTERM,
64 #ifdef SIGSTKFLT
65 [SIGSTKFLT] = TARGET_SIGSTKFLT,
66 #endif
67 [SIGCHLD] = TARGET_SIGCHLD,
68 [SIGCONT] = TARGET_SIGCONT,
69 [SIGSTOP] = TARGET_SIGSTOP,
70 [SIGTSTP] = TARGET_SIGTSTP,
71 [SIGTTIN] = TARGET_SIGTTIN,
72 [SIGTTOU] = TARGET_SIGTTOU,
73 [SIGURG] = TARGET_SIGURG,
74 [SIGXCPU] = TARGET_SIGXCPU,
75 [SIGXFSZ] = TARGET_SIGXFSZ,
76 [SIGVTALRM] = TARGET_SIGVTALRM,
77 [SIGPROF] = TARGET_SIGPROF,
78 [SIGWINCH] = TARGET_SIGWINCH,
79 [SIGIO] = TARGET_SIGIO,
80 [SIGPWR] = TARGET_SIGPWR,
81 [SIGSYS] = TARGET_SIGSYS,
82 /* next signals stay the same */
83 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
84 host libpthread signals. This assumes noone actually uses SIGRTMAX :-/
85 To fix this properly we need to do manual signal delivery multiplexed
86 over a single host signal. */
87 [__SIGRTMIN] = __SIGRTMAX,
88 [__SIGRTMAX] = __SIGRTMIN,
90 static uint8_t target_to_host_signal_table[_NSIG];
92 static inline int on_sig_stack(unsigned long sp)
94 return (sp - target_sigaltstack_used.ss_sp
95 < target_sigaltstack_used.ss_size);
98 static inline int sas_ss_flags(unsigned long sp)
100 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
101 : on_sig_stack(sp) ? SS_ONSTACK : 0);
104 int host_to_target_signal(int sig)
106 if (sig >= _NSIG)
107 return sig;
108 return host_to_target_signal_table[sig];
111 int target_to_host_signal(int sig)
113 if (sig >= _NSIG)
114 return sig;
115 return target_to_host_signal_table[sig];
118 static inline void target_sigemptyset(target_sigset_t *set)
120 memset(set, 0, sizeof(*set));
123 static inline void target_sigaddset(target_sigset_t *set, int signum)
125 signum--;
126 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
127 set->sig[signum / TARGET_NSIG_BPW] |= mask;
130 static inline int target_sigismember(const target_sigset_t *set, int signum)
132 signum--;
133 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
134 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
137 static void host_to_target_sigset_internal(target_sigset_t *d,
138 const sigset_t *s)
140 int i;
141 target_sigemptyset(d);
142 for (i = 1; i <= TARGET_NSIG; i++) {
143 if (sigismember(s, i)) {
144 target_sigaddset(d, host_to_target_signal(i));
149 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
151 target_sigset_t d1;
152 int i;
154 host_to_target_sigset_internal(&d1, s);
155 for(i = 0;i < TARGET_NSIG_WORDS; i++)
156 d->sig[i] = tswapl(d1.sig[i]);
159 static void target_to_host_sigset_internal(sigset_t *d,
160 const target_sigset_t *s)
162 int i;
163 sigemptyset(d);
164 for (i = 1; i <= TARGET_NSIG; i++) {
165 if (target_sigismember(s, i)) {
166 sigaddset(d, target_to_host_signal(i));
171 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
173 target_sigset_t s1;
174 int i;
176 for(i = 0;i < TARGET_NSIG_WORDS; i++)
177 s1.sig[i] = tswapl(s->sig[i]);
178 target_to_host_sigset_internal(d, &s1);
181 void host_to_target_old_sigset(abi_ulong *old_sigset,
182 const sigset_t *sigset)
184 target_sigset_t d;
185 host_to_target_sigset(&d, sigset);
186 *old_sigset = d.sig[0];
189 void target_to_host_old_sigset(sigset_t *sigset,
190 const abi_ulong *old_sigset)
192 target_sigset_t d;
193 int i;
195 d.sig[0] = *old_sigset;
196 for(i = 1;i < TARGET_NSIG_WORDS; i++)
197 d.sig[i] = 0;
198 target_to_host_sigset(sigset, &d);
201 /* siginfo conversion */
203 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
204 const siginfo_t *info)
206 int sig;
207 sig = host_to_target_signal(info->si_signo);
208 tinfo->si_signo = sig;
209 tinfo->si_errno = 0;
210 tinfo->si_code = info->si_code;
211 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
212 sig == SIGBUS || sig == SIGTRAP) {
213 /* should never come here, but who knows. The information for
214 the target is irrelevant */
215 tinfo->_sifields._sigfault._addr = 0;
216 } else if (sig == SIGIO) {
217 tinfo->_sifields._sigpoll._fd = info->si_fd;
218 } else if (sig >= TARGET_SIGRTMIN) {
219 tinfo->_sifields._rt._pid = info->si_pid;
220 tinfo->_sifields._rt._uid = info->si_uid;
221 /* XXX: potential problem if 64 bit */
222 tinfo->_sifields._rt._sigval.sival_ptr =
223 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
227 static void tswap_siginfo(target_siginfo_t *tinfo,
228 const target_siginfo_t *info)
230 int sig;
231 sig = info->si_signo;
232 tinfo->si_signo = tswap32(sig);
233 tinfo->si_errno = tswap32(info->si_errno);
234 tinfo->si_code = tswap32(info->si_code);
235 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
236 sig == SIGBUS || sig == SIGTRAP) {
237 tinfo->_sifields._sigfault._addr =
238 tswapl(info->_sifields._sigfault._addr);
239 } else if (sig == SIGIO) {
240 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
241 } else if (sig >= TARGET_SIGRTMIN) {
242 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
243 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
244 tinfo->_sifields._rt._sigval.sival_ptr =
245 tswapl(info->_sifields._rt._sigval.sival_ptr);
250 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
252 host_to_target_siginfo_noswap(tinfo, info);
253 tswap_siginfo(tinfo, tinfo);
256 /* XXX: we support only POSIX RT signals are used. */
257 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
258 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
260 info->si_signo = tswap32(tinfo->si_signo);
261 info->si_errno = tswap32(tinfo->si_errno);
262 info->si_code = tswap32(tinfo->si_code);
263 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
264 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
265 info->si_value.sival_ptr =
266 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
269 static int fatal_signal (int sig)
271 switch (sig) {
272 case TARGET_SIGCHLD:
273 case TARGET_SIGURG:
274 case TARGET_SIGWINCH:
275 /* Ignored by default. */
276 return 0;
277 case TARGET_SIGCONT:
278 case TARGET_SIGSTOP:
279 case TARGET_SIGTSTP:
280 case TARGET_SIGTTIN:
281 case TARGET_SIGTTOU:
282 /* Job control signals. */
283 return 0;
284 default:
285 return 1;
289 /* returns 1 if given signal should dump core if not handled */
290 static int core_dump_signal(int sig)
292 switch (sig) {
293 case TARGET_SIGABRT:
294 case TARGET_SIGFPE:
295 case TARGET_SIGILL:
296 case TARGET_SIGQUIT:
297 case TARGET_SIGSEGV:
298 case TARGET_SIGTRAP:
299 case TARGET_SIGBUS:
300 return (1);
301 default:
302 return (0);
306 void signal_init(void)
308 struct sigaction act;
309 struct sigaction oact;
310 int i, j;
311 int host_sig;
313 /* generate signal conversion tables */
314 for(i = 1; i < _NSIG; i++) {
315 if (host_to_target_signal_table[i] == 0)
316 host_to_target_signal_table[i] = i;
318 for(i = 1; i < _NSIG; i++) {
319 j = host_to_target_signal_table[i];
320 target_to_host_signal_table[j] = i;
323 /* set all host signal handlers. ALL signals are blocked during
324 the handlers to serialize them. */
325 memset(sigact_table, 0, sizeof(sigact_table));
327 sigfillset(&act.sa_mask);
328 act.sa_flags = SA_SIGINFO;
329 act.sa_sigaction = host_signal_handler;
330 for(i = 1; i <= TARGET_NSIG; i++) {
331 host_sig = target_to_host_signal(i);
332 sigaction(host_sig, NULL, &oact);
333 if (oact.sa_sigaction == (void *)SIG_IGN) {
334 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
335 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
336 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
338 /* If there's already a handler installed then something has
339 gone horribly wrong, so don't even try to handle that case. */
340 /* Install some handlers for our own use. We need at least
341 SIGSEGV and SIGBUS, to detect exceptions. We can not just
342 trap all signals because it affects syscall interrupt
343 behavior. But do trap all default-fatal signals. */
344 if (fatal_signal (i))
345 sigaction(host_sig, &act, NULL);
349 /* signal queue handling */
351 static inline struct sigqueue *alloc_sigqueue(CPUState *env)
353 TaskState *ts = env->opaque;
354 struct sigqueue *q = ts->first_free;
355 if (!q)
356 return NULL;
357 ts->first_free = q->next;
358 return q;
361 static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
363 TaskState *ts = env->opaque;
364 q->next = ts->first_free;
365 ts->first_free = q;
368 /* abort execution with signal */
369 static void QEMU_NORETURN force_sig(int target_sig)
371 TaskState *ts = (TaskState *)thread_env->opaque;
372 int host_sig, core_dumped = 0;
373 struct sigaction act;
374 host_sig = target_to_host_signal(target_sig);
375 gdb_signalled(thread_env, target_sig);
377 /* dump core if supported by target binary format */
378 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
379 stop_all_tasks();
380 core_dumped =
381 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
383 if (core_dumped) {
384 /* we already dumped the core of target process, we don't want
385 * a coredump of qemu itself */
386 struct rlimit nodump;
387 getrlimit(RLIMIT_CORE, &nodump);
388 nodump.rlim_cur=0;
389 setrlimit(RLIMIT_CORE, &nodump);
390 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
391 target_sig, strsignal(host_sig), "core dumped" );
394 /* The proper exit code for dieing from an uncaught signal is
395 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
396 * a negative value. To get the proper exit code we need to
397 * actually die from an uncaught signal. Here the default signal
398 * handler is installed, we send ourself a signal and we wait for
399 * it to arrive. */
400 sigfillset(&act.sa_mask);
401 act.sa_handler = SIG_DFL;
402 sigaction(host_sig, &act, NULL);
404 /* For some reason raise(host_sig) doesn't send the signal when
405 * statically linked on x86-64. */
406 kill(getpid(), host_sig);
408 /* Make sure the signal isn't masked (just reuse the mask inside
409 of act) */
410 sigdelset(&act.sa_mask, host_sig);
411 sigsuspend(&act.sa_mask);
413 /* unreachable */
414 abort();
417 /* queue a signal so that it will be send to the virtual CPU as soon
418 as possible */
419 int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
421 TaskState *ts = env->opaque;
422 struct emulated_sigtable *k;
423 struct sigqueue *q, **pq;
424 abi_ulong handler;
425 int queue;
427 #if defined(DEBUG_SIGNAL)
428 fprintf(stderr, "queue_signal: sig=%d\n",
429 sig);
430 #endif
431 k = &ts->sigtab[sig - 1];
432 queue = gdb_queuesig ();
433 handler = sigact_table[sig - 1]._sa_handler;
434 if (!queue && handler == TARGET_SIG_DFL) {
435 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
436 kill(getpid(),SIGSTOP);
437 return 0;
438 } else
439 /* default handler : ignore some signal. The other are fatal */
440 if (sig != TARGET_SIGCHLD &&
441 sig != TARGET_SIGURG &&
442 sig != TARGET_SIGWINCH &&
443 sig != TARGET_SIGCONT) {
444 force_sig(sig);
445 } else {
446 return 0; /* indicate ignored */
448 } else if (!queue && handler == TARGET_SIG_IGN) {
449 /* ignore signal */
450 return 0;
451 } else if (!queue && handler == TARGET_SIG_ERR) {
452 force_sig(sig);
453 } else {
454 pq = &k->first;
455 if (sig < TARGET_SIGRTMIN) {
456 /* if non real time signal, we queue exactly one signal */
457 if (!k->pending)
458 q = &k->info;
459 else
460 return 0;
461 } else {
462 if (!k->pending) {
463 /* first signal */
464 q = &k->info;
465 } else {
466 q = alloc_sigqueue(env);
467 if (!q)
468 return -EAGAIN;
469 while (*pq != NULL)
470 pq = &(*pq)->next;
473 *pq = q;
474 q->info = *info;
475 q->next = NULL;
476 k->pending = 1;
477 /* signal that a new signal is pending */
478 ts->signal_pending = 1;
479 return 1; /* indicates that the signal was queued */
483 static void host_signal_handler(int host_signum, siginfo_t *info,
484 void *puc)
486 int sig;
487 target_siginfo_t tinfo;
489 /* the CPU emulator uses some host signals to detect exceptions,
490 we forward to it some signals */
491 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
492 && info->si_code > 0) {
493 if (cpu_signal_handler(host_signum, info, puc))
494 return;
497 /* get target signal number */
498 sig = host_to_target_signal(host_signum);
499 if (sig < 1 || sig > TARGET_NSIG)
500 return;
501 #if defined(DEBUG_SIGNAL)
502 fprintf(stderr, "qemu: got signal %d\n", sig);
503 #endif
504 host_to_target_siginfo_noswap(&tinfo, info);
505 if (queue_signal(thread_env, sig, &tinfo) == 1) {
506 /* interrupt the virtual CPU as soon as possible */
507 cpu_exit(thread_env);
511 /* do_sigaltstack() returns target values and errnos. */
512 /* compare linux/kernel/signal.c:do_sigaltstack() */
513 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
515 int ret;
516 struct target_sigaltstack oss;
518 /* XXX: test errors */
519 if(uoss_addr)
521 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
522 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
523 __put_user(sas_ss_flags(sp), &oss.ss_flags);
526 if(uss_addr)
528 struct target_sigaltstack *uss;
529 struct target_sigaltstack ss;
531 ret = -TARGET_EFAULT;
532 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
533 || __get_user(ss.ss_sp, &uss->ss_sp)
534 || __get_user(ss.ss_size, &uss->ss_size)
535 || __get_user(ss.ss_flags, &uss->ss_flags))
536 goto out;
537 unlock_user_struct(uss, uss_addr, 0);
539 ret = -TARGET_EPERM;
540 if (on_sig_stack(sp))
541 goto out;
543 ret = -TARGET_EINVAL;
544 if (ss.ss_flags != TARGET_SS_DISABLE
545 && ss.ss_flags != TARGET_SS_ONSTACK
546 && ss.ss_flags != 0)
547 goto out;
549 if (ss.ss_flags == TARGET_SS_DISABLE) {
550 ss.ss_size = 0;
551 ss.ss_sp = 0;
552 } else {
553 ret = -TARGET_ENOMEM;
554 if (ss.ss_size < MINSIGSTKSZ)
555 goto out;
558 target_sigaltstack_used.ss_sp = ss.ss_sp;
559 target_sigaltstack_used.ss_size = ss.ss_size;
562 if (uoss_addr) {
563 ret = -TARGET_EFAULT;
564 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
565 goto out;
568 ret = 0;
569 out:
570 return ret;
573 /* do_sigaction() return host values and errnos */
574 int do_sigaction(int sig, const struct target_sigaction *act,
575 struct target_sigaction *oact)
577 struct target_sigaction *k;
578 struct sigaction act1;
579 int host_sig;
580 int ret = 0;
582 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
583 return -EINVAL;
584 k = &sigact_table[sig - 1];
585 #if defined(DEBUG_SIGNAL)
586 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
587 sig, act, oact);
588 #endif
589 if (oact) {
590 oact->_sa_handler = tswapl(k->_sa_handler);
591 oact->sa_flags = tswapl(k->sa_flags);
592 #if !defined(TARGET_MIPS)
593 oact->sa_restorer = tswapl(k->sa_restorer);
594 #endif
595 oact->sa_mask = k->sa_mask;
597 if (act) {
598 /* FIXME: This is not threadsafe. */
599 k->_sa_handler = tswapl(act->_sa_handler);
600 k->sa_flags = tswapl(act->sa_flags);
601 #if !defined(TARGET_MIPS)
602 k->sa_restorer = tswapl(act->sa_restorer);
603 #endif
604 k->sa_mask = act->sa_mask;
606 /* we update the host linux signal state */
607 host_sig = target_to_host_signal(sig);
608 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
609 sigfillset(&act1.sa_mask);
610 act1.sa_flags = SA_SIGINFO;
611 if (k->sa_flags & TARGET_SA_RESTART)
612 act1.sa_flags |= SA_RESTART;
613 /* NOTE: it is important to update the host kernel signal
614 ignore state to avoid getting unexpected interrupted
615 syscalls */
616 if (k->_sa_handler == TARGET_SIG_IGN) {
617 act1.sa_sigaction = (void *)SIG_IGN;
618 } else if (k->_sa_handler == TARGET_SIG_DFL) {
619 if (fatal_signal (sig))
620 act1.sa_sigaction = host_signal_handler;
621 else
622 act1.sa_sigaction = (void *)SIG_DFL;
623 } else {
624 act1.sa_sigaction = host_signal_handler;
626 ret = sigaction(host_sig, &act1, NULL);
629 return ret;
632 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
633 const target_siginfo_t *info)
635 tswap_siginfo(tinfo, info);
636 return 0;
639 static inline int current_exec_domain_sig(int sig)
641 return /* current->exec_domain && current->exec_domain->signal_invmap
642 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
645 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
647 /* from the Linux kernel */
649 struct target_fpreg {
650 uint16_t significand[4];
651 uint16_t exponent;
654 struct target_fpxreg {
655 uint16_t significand[4];
656 uint16_t exponent;
657 uint16_t padding[3];
660 struct target_xmmreg {
661 abi_ulong element[4];
664 struct target_fpstate {
665 /* Regular FPU environment */
666 abi_ulong cw;
667 abi_ulong sw;
668 abi_ulong tag;
669 abi_ulong ipoff;
670 abi_ulong cssel;
671 abi_ulong dataoff;
672 abi_ulong datasel;
673 struct target_fpreg _st[8];
674 uint16_t status;
675 uint16_t magic; /* 0xffff = regular FPU data only */
677 /* FXSR FPU environment */
678 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
679 abi_ulong mxcsr;
680 abi_ulong reserved;
681 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
682 struct target_xmmreg _xmm[8];
683 abi_ulong padding[56];
686 #define X86_FXSR_MAGIC 0x0000
688 struct target_sigcontext {
689 uint16_t gs, __gsh;
690 uint16_t fs, __fsh;
691 uint16_t es, __esh;
692 uint16_t ds, __dsh;
693 abi_ulong edi;
694 abi_ulong esi;
695 abi_ulong ebp;
696 abi_ulong esp;
697 abi_ulong ebx;
698 abi_ulong edx;
699 abi_ulong ecx;
700 abi_ulong eax;
701 abi_ulong trapno;
702 abi_ulong err;
703 abi_ulong eip;
704 uint16_t cs, __csh;
705 abi_ulong eflags;
706 abi_ulong esp_at_signal;
707 uint16_t ss, __ssh;
708 abi_ulong fpstate; /* pointer */
709 abi_ulong oldmask;
710 abi_ulong cr2;
713 struct target_ucontext {
714 abi_ulong tuc_flags;
715 abi_ulong tuc_link;
716 target_stack_t tuc_stack;
717 struct target_sigcontext tuc_mcontext;
718 target_sigset_t tuc_sigmask; /* mask last for extensibility */
721 struct sigframe
723 abi_ulong pretcode;
724 int sig;
725 struct target_sigcontext sc;
726 struct target_fpstate fpstate;
727 abi_ulong extramask[TARGET_NSIG_WORDS-1];
728 char retcode[8];
731 struct rt_sigframe
733 abi_ulong pretcode;
734 int sig;
735 abi_ulong pinfo;
736 abi_ulong puc;
737 struct target_siginfo info;
738 struct target_ucontext uc;
739 struct target_fpstate fpstate;
740 char retcode[8];
744 * Set up a signal frame.
747 /* XXX: save x87 state */
748 static int
749 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
750 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
752 int err = 0;
753 uint16_t magic;
755 /* already locked in setup_frame() */
756 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
757 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
758 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
759 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
760 err |= __put_user(env->regs[R_EDI], &sc->edi);
761 err |= __put_user(env->regs[R_ESI], &sc->esi);
762 err |= __put_user(env->regs[R_EBP], &sc->ebp);
763 err |= __put_user(env->regs[R_ESP], &sc->esp);
764 err |= __put_user(env->regs[R_EBX], &sc->ebx);
765 err |= __put_user(env->regs[R_EDX], &sc->edx);
766 err |= __put_user(env->regs[R_ECX], &sc->ecx);
767 err |= __put_user(env->regs[R_EAX], &sc->eax);
768 err |= __put_user(env->exception_index, &sc->trapno);
769 err |= __put_user(env->error_code, &sc->err);
770 err |= __put_user(env->eip, &sc->eip);
771 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
772 err |= __put_user(env->eflags, &sc->eflags);
773 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
774 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
776 cpu_x86_fsave(env, fpstate_addr, 1);
777 fpstate->status = fpstate->sw;
778 magic = 0xffff;
779 err |= __put_user(magic, &fpstate->magic);
780 err |= __put_user(fpstate_addr, &sc->fpstate);
782 /* non-iBCS2 extensions.. */
783 err |= __put_user(mask, &sc->oldmask);
784 err |= __put_user(env->cr[2], &sc->cr2);
785 return err;
789 * Determine which stack to use..
792 static inline abi_ulong
793 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
795 unsigned long esp;
797 /* Default to using normal stack */
798 esp = env->regs[R_ESP];
799 /* This is the X/Open sanctioned signal stack switching. */
800 if (ka->sa_flags & TARGET_SA_ONSTACK) {
801 if (sas_ss_flags(esp) == 0)
802 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
805 /* This is the legacy signal stack switching. */
806 else
807 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
808 !(ka->sa_flags & TARGET_SA_RESTORER) &&
809 ka->sa_restorer) {
810 esp = (unsigned long) ka->sa_restorer;
812 return (esp - frame_size) & -8ul;
815 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
816 static void setup_frame(int sig, struct target_sigaction *ka,
817 target_sigset_t *set, CPUX86State *env)
819 abi_ulong frame_addr;
820 struct sigframe *frame;
821 int i, err = 0;
823 frame_addr = get_sigframe(ka, env, sizeof(*frame));
825 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
826 goto give_sigsegv;
828 err |= __put_user(current_exec_domain_sig(sig),
829 &frame->sig);
830 if (err)
831 goto give_sigsegv;
833 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
834 frame_addr + offsetof(struct sigframe, fpstate));
835 if (err)
836 goto give_sigsegv;
838 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
839 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
840 goto give_sigsegv;
843 /* Set up to return from userspace. If provided, use a stub
844 already in userspace. */
845 if (ka->sa_flags & TARGET_SA_RESTORER) {
846 err |= __put_user(ka->sa_restorer, &frame->pretcode);
847 } else {
848 uint16_t val16;
849 abi_ulong retcode_addr;
850 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
851 err |= __put_user(retcode_addr, &frame->pretcode);
852 /* This is popl %eax ; movl $,%eax ; int $0x80 */
853 val16 = 0xb858;
854 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
855 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
856 val16 = 0x80cd;
857 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
860 if (err)
861 goto give_sigsegv;
863 /* Set up registers for signal handler */
864 env->regs[R_ESP] = frame_addr;
865 env->eip = ka->_sa_handler;
867 cpu_x86_load_seg(env, R_DS, __USER_DS);
868 cpu_x86_load_seg(env, R_ES, __USER_DS);
869 cpu_x86_load_seg(env, R_SS, __USER_DS);
870 cpu_x86_load_seg(env, R_CS, __USER_CS);
871 env->eflags &= ~TF_MASK;
873 unlock_user_struct(frame, frame_addr, 1);
875 return;
877 give_sigsegv:
878 unlock_user_struct(frame, frame_addr, 1);
879 if (sig == TARGET_SIGSEGV)
880 ka->_sa_handler = TARGET_SIG_DFL;
881 force_sig(TARGET_SIGSEGV /* , current */);
884 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
885 static void setup_rt_frame(int sig, struct target_sigaction *ka,
886 target_siginfo_t *info,
887 target_sigset_t *set, CPUX86State *env)
889 abi_ulong frame_addr, addr;
890 struct rt_sigframe *frame;
891 int i, err = 0;
893 frame_addr = get_sigframe(ka, env, sizeof(*frame));
895 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
896 goto give_sigsegv;
898 err |= __put_user(current_exec_domain_sig(sig),
899 &frame->sig);
900 addr = frame_addr + offsetof(struct rt_sigframe, info);
901 err |= __put_user(addr, &frame->pinfo);
902 addr = frame_addr + offsetof(struct rt_sigframe, uc);
903 err |= __put_user(addr, &frame->puc);
904 err |= copy_siginfo_to_user(&frame->info, info);
905 if (err)
906 goto give_sigsegv;
908 /* Create the ucontext. */
909 err |= __put_user(0, &frame->uc.tuc_flags);
910 err |= __put_user(0, &frame->uc.tuc_link);
911 err |= __put_user(target_sigaltstack_used.ss_sp,
912 &frame->uc.tuc_stack.ss_sp);
913 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
914 &frame->uc.tuc_stack.ss_flags);
915 err |= __put_user(target_sigaltstack_used.ss_size,
916 &frame->uc.tuc_stack.ss_size);
917 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
918 env, set->sig[0],
919 frame_addr + offsetof(struct rt_sigframe, fpstate));
920 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
921 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
922 goto give_sigsegv;
925 /* Set up to return from userspace. If provided, use a stub
926 already in userspace. */
927 if (ka->sa_flags & TARGET_SA_RESTORER) {
928 err |= __put_user(ka->sa_restorer, &frame->pretcode);
929 } else {
930 uint16_t val16;
931 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
932 err |= __put_user(addr, &frame->pretcode);
933 /* This is movl $,%eax ; int $0x80 */
934 err |= __put_user(0xb8, (char *)(frame->retcode+0));
935 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
936 val16 = 0x80cd;
937 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
940 if (err)
941 goto give_sigsegv;
943 /* Set up registers for signal handler */
944 env->regs[R_ESP] = frame_addr;
945 env->eip = ka->_sa_handler;
947 cpu_x86_load_seg(env, R_DS, __USER_DS);
948 cpu_x86_load_seg(env, R_ES, __USER_DS);
949 cpu_x86_load_seg(env, R_SS, __USER_DS);
950 cpu_x86_load_seg(env, R_CS, __USER_CS);
951 env->eflags &= ~TF_MASK;
953 unlock_user_struct(frame, frame_addr, 1);
955 return;
957 give_sigsegv:
958 unlock_user_struct(frame, frame_addr, 1);
959 if (sig == TARGET_SIGSEGV)
960 ka->_sa_handler = TARGET_SIG_DFL;
961 force_sig(TARGET_SIGSEGV /* , current */);
964 static int
965 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
967 unsigned int err = 0;
968 abi_ulong fpstate_addr;
969 unsigned int tmpflags;
971 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
972 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
973 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
974 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
976 env->regs[R_EDI] = tswapl(sc->edi);
977 env->regs[R_ESI] = tswapl(sc->esi);
978 env->regs[R_EBP] = tswapl(sc->ebp);
979 env->regs[R_ESP] = tswapl(sc->esp);
980 env->regs[R_EBX] = tswapl(sc->ebx);
981 env->regs[R_EDX] = tswapl(sc->edx);
982 env->regs[R_ECX] = tswapl(sc->ecx);
983 env->eip = tswapl(sc->eip);
985 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
986 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
988 tmpflags = tswapl(sc->eflags);
989 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
990 // regs->orig_eax = -1; /* disable syscall checks */
992 fpstate_addr = tswapl(sc->fpstate);
993 if (fpstate_addr != 0) {
994 if (!access_ok(VERIFY_READ, fpstate_addr,
995 sizeof(struct target_fpstate)))
996 goto badframe;
997 cpu_x86_frstor(env, fpstate_addr, 1);
1000 *peax = tswapl(sc->eax);
1001 return err;
1002 badframe:
1003 return 1;
1006 long do_sigreturn(CPUX86State *env)
1008 struct sigframe *frame;
1009 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1010 target_sigset_t target_set;
1011 sigset_t set;
1012 int eax, i;
1014 #if defined(DEBUG_SIGNAL)
1015 fprintf(stderr, "do_sigreturn\n");
1016 #endif
1017 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1018 goto badframe;
1019 /* set blocked signals */
1020 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1021 goto badframe;
1022 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1023 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1024 goto badframe;
1027 target_to_host_sigset_internal(&set, &target_set);
1028 sigprocmask(SIG_SETMASK, &set, NULL);
1030 /* restore registers */
1031 if (restore_sigcontext(env, &frame->sc, &eax))
1032 goto badframe;
1033 unlock_user_struct(frame, frame_addr, 0);
1034 return eax;
1036 badframe:
1037 unlock_user_struct(frame, frame_addr, 0);
1038 force_sig(TARGET_SIGSEGV);
1039 return 0;
1042 long do_rt_sigreturn(CPUX86State *env)
1044 abi_ulong frame_addr;
1045 struct rt_sigframe *frame;
1046 sigset_t set;
1047 int eax;
1049 frame_addr = env->regs[R_ESP] - 4;
1050 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1051 goto badframe;
1052 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1053 sigprocmask(SIG_SETMASK, &set, NULL);
1055 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1056 goto badframe;
1058 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1059 get_sp_from_cpustate(env)) == -EFAULT)
1060 goto badframe;
1062 unlock_user_struct(frame, frame_addr, 0);
1063 return eax;
1065 badframe:
1066 unlock_user_struct(frame, frame_addr, 0);
1067 force_sig(TARGET_SIGSEGV);
1068 return 0;
1071 #elif defined(TARGET_ARM)
1073 struct target_sigcontext {
1074 abi_ulong trap_no;
1075 abi_ulong error_code;
1076 abi_ulong oldmask;
1077 abi_ulong arm_r0;
1078 abi_ulong arm_r1;
1079 abi_ulong arm_r2;
1080 abi_ulong arm_r3;
1081 abi_ulong arm_r4;
1082 abi_ulong arm_r5;
1083 abi_ulong arm_r6;
1084 abi_ulong arm_r7;
1085 abi_ulong arm_r8;
1086 abi_ulong arm_r9;
1087 abi_ulong arm_r10;
1088 abi_ulong arm_fp;
1089 abi_ulong arm_ip;
1090 abi_ulong arm_sp;
1091 abi_ulong arm_lr;
1092 abi_ulong arm_pc;
1093 abi_ulong arm_cpsr;
1094 abi_ulong fault_address;
1097 struct target_ucontext_v1 {
1098 abi_ulong tuc_flags;
1099 abi_ulong tuc_link;
1100 target_stack_t tuc_stack;
1101 struct target_sigcontext tuc_mcontext;
1102 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1105 struct target_ucontext_v2 {
1106 abi_ulong tuc_flags;
1107 abi_ulong tuc_link;
1108 target_stack_t tuc_stack;
1109 struct target_sigcontext tuc_mcontext;
1110 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1111 char __unused[128 - sizeof(target_sigset_t)];
1112 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1115 struct target_user_vfp {
1116 uint64_t fpregs[32];
1117 abi_ulong fpscr;
1120 struct target_user_vfp_exc {
1121 abi_ulong fpexc;
1122 abi_ulong fpinst;
1123 abi_ulong fpinst2;
1126 struct target_vfp_sigframe {
1127 abi_ulong magic;
1128 abi_ulong size;
1129 struct target_user_vfp ufp;
1130 struct target_user_vfp_exc ufp_exc;
1131 } __attribute__((__aligned__(8)));
1133 struct target_iwmmxt_sigframe {
1134 abi_ulong magic;
1135 abi_ulong size;
1136 uint64_t regs[16];
1137 /* Note that not all the coprocessor control registers are stored here */
1138 uint32_t wcssf;
1139 uint32_t wcasf;
1140 uint32_t wcgr0;
1141 uint32_t wcgr1;
1142 uint32_t wcgr2;
1143 uint32_t wcgr3;
1144 } __attribute__((__aligned__(8)));
1146 #define TARGET_VFP_MAGIC 0x56465001
1147 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1149 struct sigframe_v1
1151 struct target_sigcontext sc;
1152 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1153 abi_ulong retcode;
1156 struct sigframe_v2
1158 struct target_ucontext_v2 uc;
1159 abi_ulong retcode;
1162 struct rt_sigframe_v1
1164 abi_ulong pinfo;
1165 abi_ulong puc;
1166 struct target_siginfo info;
1167 struct target_ucontext_v1 uc;
1168 abi_ulong retcode;
1171 struct rt_sigframe_v2
1173 struct target_siginfo info;
1174 struct target_ucontext_v2 uc;
1175 abi_ulong retcode;
1178 #define TARGET_CONFIG_CPU_32 1
1181 * For ARM syscalls, we encode the syscall number into the instruction.
1183 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1184 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1187 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1188 * need two 16-bit instructions.
1190 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1191 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1193 static const abi_ulong retcodes[4] = {
1194 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1195 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1199 #define __get_user_error(x,p,e) __get_user(x, p)
1201 static inline int valid_user_regs(CPUState *regs)
1203 return 1;
1206 static void
1207 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1208 CPUState *env, abi_ulong mask)
1210 __put_user(env->regs[0], &sc->arm_r0);
1211 __put_user(env->regs[1], &sc->arm_r1);
1212 __put_user(env->regs[2], &sc->arm_r2);
1213 __put_user(env->regs[3], &sc->arm_r3);
1214 __put_user(env->regs[4], &sc->arm_r4);
1215 __put_user(env->regs[5], &sc->arm_r5);
1216 __put_user(env->regs[6], &sc->arm_r6);
1217 __put_user(env->regs[7], &sc->arm_r7);
1218 __put_user(env->regs[8], &sc->arm_r8);
1219 __put_user(env->regs[9], &sc->arm_r9);
1220 __put_user(env->regs[10], &sc->arm_r10);
1221 __put_user(env->regs[11], &sc->arm_fp);
1222 __put_user(env->regs[12], &sc->arm_ip);
1223 __put_user(env->regs[13], &sc->arm_sp);
1224 __put_user(env->regs[14], &sc->arm_lr);
1225 __put_user(env->regs[15], &sc->arm_pc);
1226 #ifdef TARGET_CONFIG_CPU_32
1227 __put_user(cpsr_read(env), &sc->arm_cpsr);
1228 #endif
1230 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1231 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1232 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1233 __put_user(mask, &sc->oldmask);
1236 static inline abi_ulong
1237 get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1239 unsigned long sp = regs->regs[13];
1242 * This is the X/Open sanctioned signal stack switching.
1244 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1245 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1247 * ATPCS B01 mandates 8-byte alignment
1249 return (sp - framesize) & ~7;
1252 static int
1253 setup_return(CPUState *env, struct target_sigaction *ka,
1254 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1256 abi_ulong handler = ka->_sa_handler;
1257 abi_ulong retcode;
1258 int thumb = handler & 1;
1260 if (ka->sa_flags & TARGET_SA_RESTORER) {
1261 retcode = ka->sa_restorer;
1262 } else {
1263 unsigned int idx = thumb;
1265 if (ka->sa_flags & TARGET_SA_SIGINFO)
1266 idx += 2;
1268 if (__put_user(retcodes[idx], rc))
1269 return 1;
1270 #if 0
1271 flush_icache_range((abi_ulong)rc,
1272 (abi_ulong)(rc + 1));
1273 #endif
1274 retcode = rc_addr + thumb;
1277 env->regs[0] = usig;
1278 env->regs[13] = frame_addr;
1279 env->regs[14] = retcode;
1280 env->regs[15] = handler & (thumb ? ~1 : ~3);
1281 env->thumb = thumb;
1283 #if 0
1284 #ifdef TARGET_CONFIG_CPU_32
1285 env->cpsr = cpsr;
1286 #endif
1287 #endif
1289 return 0;
1292 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUState *env)
1294 int i;
1295 struct target_vfp_sigframe *vfpframe;
1296 vfpframe = (struct target_vfp_sigframe *)regspace;
1297 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1298 __put_user(sizeof(*vfpframe), &vfpframe->size);
1299 for (i = 0; i < 32; i++) {
1300 __put_user(env->vfp.regs[i], &vfpframe->ufp.fpregs[i]);
1302 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1303 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1304 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1305 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1306 return (abi_ulong*)(vfpframe+1);
1309 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace, CPUState *env)
1311 int i;
1312 struct target_iwmmxt_sigframe *iwmmxtframe;
1313 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1314 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1315 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1316 for (i = 0; i < 16; i++) {
1317 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1319 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1320 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1321 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1322 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1323 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1324 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1325 return (abi_ulong*)(iwmmxtframe+1);
1328 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1329 target_sigset_t *set, CPUState *env)
1331 struct target_sigaltstack stack;
1332 int i;
1333 abi_ulong *regspace;
1335 /* Clear all the bits of the ucontext we don't use. */
1336 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1338 memset(&stack, 0, sizeof(stack));
1339 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1340 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1341 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1342 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1344 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1345 /* Save coprocessor signal frame. */
1346 regspace = uc->tuc_regspace;
1347 if (arm_feature(env, ARM_FEATURE_VFP)) {
1348 regspace = setup_sigframe_v2_vfp(regspace, env);
1350 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1351 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1354 /* Write terminating magic word */
1355 __put_user(0, regspace);
1357 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1358 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1362 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1363 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1364 target_sigset_t *set, CPUState *regs)
1366 struct sigframe_v1 *frame;
1367 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1368 int i;
1370 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1371 return;
1373 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1375 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1376 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1377 goto end;
1380 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1381 frame_addr + offsetof(struct sigframe_v1, retcode));
1383 end:
1384 unlock_user_struct(frame, frame_addr, 1);
1387 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1388 target_sigset_t *set, CPUState *regs)
1390 struct sigframe_v2 *frame;
1391 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1393 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1394 return;
1396 setup_sigframe_v2(&frame->uc, set, regs);
1398 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1399 frame_addr + offsetof(struct sigframe_v2, retcode));
1401 unlock_user_struct(frame, frame_addr, 1);
1404 static void setup_frame(int usig, struct target_sigaction *ka,
1405 target_sigset_t *set, CPUState *regs)
1407 if (get_osversion() >= 0x020612) {
1408 setup_frame_v2(usig, ka, set, regs);
1409 } else {
1410 setup_frame_v1(usig, ka, set, regs);
1414 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1415 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1416 target_siginfo_t *info,
1417 target_sigset_t *set, CPUState *env)
1419 struct rt_sigframe_v1 *frame;
1420 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1421 struct target_sigaltstack stack;
1422 int i;
1423 abi_ulong info_addr, uc_addr;
1425 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1426 return /* 1 */;
1428 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1429 __put_user(info_addr, &frame->pinfo);
1430 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1431 __put_user(uc_addr, &frame->puc);
1432 copy_siginfo_to_user(&frame->info, info);
1434 /* Clear all the bits of the ucontext we don't use. */
1435 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1437 memset(&stack, 0, sizeof(stack));
1438 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1439 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1440 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1441 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1443 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1444 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1445 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1446 goto end;
1449 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1450 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1452 env->regs[1] = info_addr;
1453 env->regs[2] = uc_addr;
1455 end:
1456 unlock_user_struct(frame, frame_addr, 1);
1459 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1460 target_siginfo_t *info,
1461 target_sigset_t *set, CPUState *env)
1463 struct rt_sigframe_v2 *frame;
1464 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1465 abi_ulong info_addr, uc_addr;
1467 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1468 return /* 1 */;
1470 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1471 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1472 copy_siginfo_to_user(&frame->info, info);
1474 setup_sigframe_v2(&frame->uc, set, env);
1476 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1477 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1479 env->regs[1] = info_addr;
1480 env->regs[2] = uc_addr;
1482 unlock_user_struct(frame, frame_addr, 1);
1485 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1486 target_siginfo_t *info,
1487 target_sigset_t *set, CPUState *env)
1489 if (get_osversion() >= 0x020612) {
1490 setup_rt_frame_v2(usig, ka, info, set, env);
1491 } else {
1492 setup_rt_frame_v1(usig, ka, info, set, env);
1496 static int
1497 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1499 int err = 0;
1500 uint32_t cpsr;
1502 __get_user_error(env->regs[0], &sc->arm_r0, err);
1503 __get_user_error(env->regs[1], &sc->arm_r1, err);
1504 __get_user_error(env->regs[2], &sc->arm_r2, err);
1505 __get_user_error(env->regs[3], &sc->arm_r3, err);
1506 __get_user_error(env->regs[4], &sc->arm_r4, err);
1507 __get_user_error(env->regs[5], &sc->arm_r5, err);
1508 __get_user_error(env->regs[6], &sc->arm_r6, err);
1509 __get_user_error(env->regs[7], &sc->arm_r7, err);
1510 __get_user_error(env->regs[8], &sc->arm_r8, err);
1511 __get_user_error(env->regs[9], &sc->arm_r9, err);
1512 __get_user_error(env->regs[10], &sc->arm_r10, err);
1513 __get_user_error(env->regs[11], &sc->arm_fp, err);
1514 __get_user_error(env->regs[12], &sc->arm_ip, err);
1515 __get_user_error(env->regs[13], &sc->arm_sp, err);
1516 __get_user_error(env->regs[14], &sc->arm_lr, err);
1517 __get_user_error(env->regs[15], &sc->arm_pc, err);
1518 #ifdef TARGET_CONFIG_CPU_32
1519 __get_user_error(cpsr, &sc->arm_cpsr, err);
1520 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1521 #endif
1523 err |= !valid_user_regs(env);
1525 return err;
1528 static long do_sigreturn_v1(CPUState *env)
1530 abi_ulong frame_addr;
1531 struct sigframe_v1 *frame;
1532 target_sigset_t set;
1533 sigset_t host_set;
1534 int i;
1537 * Since we stacked the signal on a 64-bit boundary,
1538 * then 'sp' should be word aligned here. If it's
1539 * not, then the user is trying to mess with us.
1541 if (env->regs[13] & 7)
1542 goto badframe;
1544 frame_addr = env->regs[13];
1545 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1546 goto badframe;
1548 if (__get_user(set.sig[0], &frame->sc.oldmask))
1549 goto badframe;
1550 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1551 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1552 goto badframe;
1555 target_to_host_sigset_internal(&host_set, &set);
1556 sigprocmask(SIG_SETMASK, &host_set, NULL);
1558 if (restore_sigcontext(env, &frame->sc))
1559 goto badframe;
1561 #if 0
1562 /* Send SIGTRAP if we're single-stepping */
1563 if (ptrace_cancel_bpt(current))
1564 send_sig(SIGTRAP, current, 1);
1565 #endif
1566 unlock_user_struct(frame, frame_addr, 0);
1567 return env->regs[0];
1569 badframe:
1570 unlock_user_struct(frame, frame_addr, 0);
1571 force_sig(TARGET_SIGSEGV /* , current */);
1572 return 0;
1575 static abi_ulong *restore_sigframe_v2_vfp(CPUState *env, abi_ulong *regspace)
1577 int i;
1578 abi_ulong magic, sz;
1579 uint32_t fpscr, fpexc;
1580 struct target_vfp_sigframe *vfpframe;
1581 vfpframe = (struct target_vfp_sigframe *)regspace;
1583 __get_user(magic, &vfpframe->magic);
1584 __get_user(sz, &vfpframe->size);
1585 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1586 return 0;
1588 for (i = 0; i < 32; i++) {
1589 __get_user(env->vfp.regs[i], &vfpframe->ufp.fpregs[i]);
1591 __get_user(fpscr, &vfpframe->ufp.fpscr);
1592 vfp_set_fpscr(env, fpscr);
1593 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1594 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1595 * and the exception flag is cleared
1597 fpexc |= (1 << 30);
1598 fpexc &= ~((1 << 31) | (1 << 28));
1599 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1600 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1601 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1602 return (abi_ulong*)(vfpframe + 1);
1605 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUState *env, abi_ulong *regspace)
1607 int i;
1608 abi_ulong magic, sz;
1609 struct target_iwmmxt_sigframe *iwmmxtframe;
1610 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1612 __get_user(magic, &iwmmxtframe->magic);
1613 __get_user(sz, &iwmmxtframe->size);
1614 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1615 return 0;
1617 for (i = 0; i < 16; i++) {
1618 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1620 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1621 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1622 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1623 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1624 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1625 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1626 return (abi_ulong*)(iwmmxtframe + 1);
1629 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1630 struct target_ucontext_v2 *uc)
1632 sigset_t host_set;
1633 abi_ulong *regspace;
1635 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1636 sigprocmask(SIG_SETMASK, &host_set, NULL);
1638 if (restore_sigcontext(env, &uc->tuc_mcontext))
1639 return 1;
1641 /* Restore coprocessor signal frame */
1642 regspace = uc->tuc_regspace;
1643 if (arm_feature(env, ARM_FEATURE_VFP)) {
1644 regspace = restore_sigframe_v2_vfp(env, regspace);
1645 if (!regspace) {
1646 return 1;
1649 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1650 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1651 if (!regspace) {
1652 return 1;
1656 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1657 return 1;
1659 #if 0
1660 /* Send SIGTRAP if we're single-stepping */
1661 if (ptrace_cancel_bpt(current))
1662 send_sig(SIGTRAP, current, 1);
1663 #endif
1665 return 0;
1668 static long do_sigreturn_v2(CPUState *env)
1670 abi_ulong frame_addr;
1671 struct sigframe_v2 *frame;
1674 * Since we stacked the signal on a 64-bit boundary,
1675 * then 'sp' should be word aligned here. If it's
1676 * not, then the user is trying to mess with us.
1678 if (env->regs[13] & 7)
1679 goto badframe;
1681 frame_addr = env->regs[13];
1682 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1683 goto badframe;
1685 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1686 goto badframe;
1688 unlock_user_struct(frame, frame_addr, 0);
1689 return env->regs[0];
1691 badframe:
1692 unlock_user_struct(frame, frame_addr, 0);
1693 force_sig(TARGET_SIGSEGV /* , current */);
1694 return 0;
1697 long do_sigreturn(CPUState *env)
1699 if (get_osversion() >= 0x020612) {
1700 return do_sigreturn_v2(env);
1701 } else {
1702 return do_sigreturn_v1(env);
1706 static long do_rt_sigreturn_v1(CPUState *env)
1708 abi_ulong frame_addr;
1709 struct rt_sigframe_v1 *frame;
1710 sigset_t host_set;
1713 * Since we stacked the signal on a 64-bit boundary,
1714 * then 'sp' should be word aligned here. If it's
1715 * not, then the user is trying to mess with us.
1717 if (env->regs[13] & 7)
1718 goto badframe;
1720 frame_addr = env->regs[13];
1721 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1722 goto badframe;
1724 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1725 sigprocmask(SIG_SETMASK, &host_set, NULL);
1727 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1728 goto badframe;
1730 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1731 goto badframe;
1733 #if 0
1734 /* Send SIGTRAP if we're single-stepping */
1735 if (ptrace_cancel_bpt(current))
1736 send_sig(SIGTRAP, current, 1);
1737 #endif
1738 unlock_user_struct(frame, frame_addr, 0);
1739 return env->regs[0];
1741 badframe:
1742 unlock_user_struct(frame, frame_addr, 0);
1743 force_sig(TARGET_SIGSEGV /* , current */);
1744 return 0;
1747 static long do_rt_sigreturn_v2(CPUState *env)
1749 abi_ulong frame_addr;
1750 struct rt_sigframe_v2 *frame;
1753 * Since we stacked the signal on a 64-bit boundary,
1754 * then 'sp' should be word aligned here. If it's
1755 * not, then the user is trying to mess with us.
1757 if (env->regs[13] & 7)
1758 goto badframe;
1760 frame_addr = env->regs[13];
1761 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1762 goto badframe;
1764 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1765 goto badframe;
1767 unlock_user_struct(frame, frame_addr, 0);
1768 return env->regs[0];
1770 badframe:
1771 unlock_user_struct(frame, frame_addr, 0);
1772 force_sig(TARGET_SIGSEGV /* , current */);
1773 return 0;
1776 long do_rt_sigreturn(CPUState *env)
1778 if (get_osversion() >= 0x020612) {
1779 return do_rt_sigreturn_v2(env);
1780 } else {
1781 return do_rt_sigreturn_v1(env);
1785 #elif defined(TARGET_SPARC)
1787 #define __SUNOS_MAXWIN 31
1789 /* This is what SunOS does, so shall I. */
1790 struct target_sigcontext {
1791 abi_ulong sigc_onstack; /* state to restore */
1793 abi_ulong sigc_mask; /* sigmask to restore */
1794 abi_ulong sigc_sp; /* stack pointer */
1795 abi_ulong sigc_pc; /* program counter */
1796 abi_ulong sigc_npc; /* next program counter */
1797 abi_ulong sigc_psr; /* for condition codes etc */
1798 abi_ulong sigc_g1; /* User uses these two registers */
1799 abi_ulong sigc_o0; /* within the trampoline code. */
1801 /* Now comes information regarding the users window set
1802 * at the time of the signal.
1804 abi_ulong sigc_oswins; /* outstanding windows */
1806 /* stack ptrs for each regwin buf */
1807 char *sigc_spbuf[__SUNOS_MAXWIN];
1809 /* Windows to restore after signal */
1810 struct {
1811 abi_ulong locals[8];
1812 abi_ulong ins[8];
1813 } sigc_wbuf[__SUNOS_MAXWIN];
1815 /* A Sparc stack frame */
1816 struct sparc_stackf {
1817 abi_ulong locals[8];
1818 abi_ulong ins[6];
1819 struct sparc_stackf *fp;
1820 abi_ulong callers_pc;
1821 char *structptr;
1822 abi_ulong xargs[6];
1823 abi_ulong xxargs[1];
1826 typedef struct {
1827 struct {
1828 abi_ulong psr;
1829 abi_ulong pc;
1830 abi_ulong npc;
1831 abi_ulong y;
1832 abi_ulong u_regs[16]; /* globals and ins */
1833 } si_regs;
1834 int si_mask;
1835 } __siginfo_t;
1837 typedef struct {
1838 unsigned long si_float_regs [32];
1839 unsigned long si_fsr;
1840 unsigned long si_fpqdepth;
1841 struct {
1842 unsigned long *insn_addr;
1843 unsigned long insn;
1844 } si_fpqueue [16];
1845 } qemu_siginfo_fpu_t;
1848 struct target_signal_frame {
1849 struct sparc_stackf ss;
1850 __siginfo_t info;
1851 abi_ulong fpu_save;
1852 abi_ulong insns[2] __attribute__ ((aligned (8)));
1853 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1854 abi_ulong extra_size; /* Should be 0 */
1855 qemu_siginfo_fpu_t fpu_state;
1857 struct target_rt_signal_frame {
1858 struct sparc_stackf ss;
1859 siginfo_t info;
1860 abi_ulong regs[20];
1861 sigset_t mask;
1862 abi_ulong fpu_save;
1863 unsigned int insns[2];
1864 stack_t stack;
1865 unsigned int extra_size; /* Should be 0 */
1866 qemu_siginfo_fpu_t fpu_state;
1869 #define UREG_O0 16
1870 #define UREG_O6 22
1871 #define UREG_I0 0
1872 #define UREG_I1 1
1873 #define UREG_I2 2
1874 #define UREG_I3 3
1875 #define UREG_I4 4
1876 #define UREG_I5 5
1877 #define UREG_I6 6
1878 #define UREG_I7 7
1879 #define UREG_L0 8
1880 #define UREG_FP UREG_I6
1881 #define UREG_SP UREG_O6
1883 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1884 CPUState *env, unsigned long framesize)
1886 abi_ulong sp;
1888 sp = env->regwptr[UREG_FP];
1890 /* This is the X/Open sanctioned signal stack switching. */
1891 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1892 if (!on_sig_stack(sp)
1893 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1894 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1896 return sp - framesize;
1899 static int
1900 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1902 int err = 0, i;
1904 err |= __put_user(env->psr, &si->si_regs.psr);
1905 err |= __put_user(env->pc, &si->si_regs.pc);
1906 err |= __put_user(env->npc, &si->si_regs.npc);
1907 err |= __put_user(env->y, &si->si_regs.y);
1908 for (i=0; i < 8; i++) {
1909 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1911 for (i=0; i < 8; i++) {
1912 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1914 err |= __put_user(mask, &si->si_mask);
1915 return err;
1918 #if 0
1919 static int
1920 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1921 CPUState *env, unsigned long mask)
1923 int err = 0;
1925 err |= __put_user(mask, &sc->sigc_mask);
1926 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1927 err |= __put_user(env->pc, &sc->sigc_pc);
1928 err |= __put_user(env->npc, &sc->sigc_npc);
1929 err |= __put_user(env->psr, &sc->sigc_psr);
1930 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1931 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1933 return err;
1935 #endif
1936 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1938 static void setup_frame(int sig, struct target_sigaction *ka,
1939 target_sigset_t *set, CPUState *env)
1941 abi_ulong sf_addr;
1942 struct target_signal_frame *sf;
1943 int sigframe_size, err, i;
1945 /* 1. Make sure everything is clean */
1946 //synchronize_user_stack();
1948 sigframe_size = NF_ALIGNEDSZ;
1949 sf_addr = get_sigframe(ka, env, sigframe_size);
1951 sf = lock_user(VERIFY_WRITE, sf_addr,
1952 sizeof(struct target_signal_frame), 0);
1953 if (!sf)
1954 goto sigsegv;
1956 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1957 #if 0
1958 if (invalid_frame_pointer(sf, sigframe_size))
1959 goto sigill_and_return;
1960 #endif
1961 /* 2. Save the current process state */
1962 err = setup___siginfo(&sf->info, env, set->sig[0]);
1963 err |= __put_user(0, &sf->extra_size);
1965 //err |= save_fpu_state(regs, &sf->fpu_state);
1966 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1968 err |= __put_user(set->sig[0], &sf->info.si_mask);
1969 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1970 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1973 for (i = 0; i < 8; i++) {
1974 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1976 for (i = 0; i < 8; i++) {
1977 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1979 if (err)
1980 goto sigsegv;
1982 /* 3. signal handler back-trampoline and parameters */
1983 env->regwptr[UREG_FP] = sf_addr;
1984 env->regwptr[UREG_I0] = sig;
1985 env->regwptr[UREG_I1] = sf_addr +
1986 offsetof(struct target_signal_frame, info);
1987 env->regwptr[UREG_I2] = sf_addr +
1988 offsetof(struct target_signal_frame, info);
1990 /* 4. signal handler */
1991 env->pc = ka->_sa_handler;
1992 env->npc = (env->pc + 4);
1993 /* 5. return to kernel instructions */
1994 if (ka->sa_restorer)
1995 env->regwptr[UREG_I7] = ka->sa_restorer;
1996 else {
1997 uint32_t val32;
1999 env->regwptr[UREG_I7] = sf_addr +
2000 offsetof(struct target_signal_frame, insns) - 2 * 4;
2002 /* mov __NR_sigreturn, %g1 */
2003 val32 = 0x821020d8;
2004 err |= __put_user(val32, &sf->insns[0]);
2006 /* t 0x10 */
2007 val32 = 0x91d02010;
2008 err |= __put_user(val32, &sf->insns[1]);
2009 if (err)
2010 goto sigsegv;
2012 /* Flush instruction space. */
2013 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2014 // tb_flush(env);
2016 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2017 return;
2018 #if 0
2019 sigill_and_return:
2020 force_sig(TARGET_SIGILL);
2021 #endif
2022 sigsegv:
2023 //fprintf(stderr, "force_sig\n");
2024 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2025 force_sig(TARGET_SIGSEGV);
2027 static inline int
2028 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
2030 int err;
2031 #if 0
2032 #ifdef CONFIG_SMP
2033 if (current->flags & PF_USEDFPU)
2034 regs->psr &= ~PSR_EF;
2035 #else
2036 if (current == last_task_used_math) {
2037 last_task_used_math = 0;
2038 regs->psr &= ~PSR_EF;
2040 #endif
2041 current->used_math = 1;
2042 current->flags &= ~PF_USEDFPU;
2043 #endif
2044 #if 0
2045 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2046 return -EFAULT;
2047 #endif
2049 #if 0
2050 /* XXX: incorrect */
2051 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
2052 (sizeof(unsigned long) * 32));
2053 #endif
2054 err |= __get_user(env->fsr, &fpu->si_fsr);
2055 #if 0
2056 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2057 if (current->thread.fpqdepth != 0)
2058 err |= __copy_from_user(&current->thread.fpqueue[0],
2059 &fpu->si_fpqueue[0],
2060 ((sizeof(unsigned long) +
2061 (sizeof(unsigned long *)))*16));
2062 #endif
2063 return err;
2067 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2068 target_siginfo_t *info,
2069 target_sigset_t *set, CPUState *env)
2071 fprintf(stderr, "setup_rt_frame: not implemented\n");
2074 long do_sigreturn(CPUState *env)
2076 abi_ulong sf_addr;
2077 struct target_signal_frame *sf;
2078 uint32_t up_psr, pc, npc;
2079 target_sigset_t set;
2080 sigset_t host_set;
2081 abi_ulong fpu_save_addr;
2082 int err, i;
2084 sf_addr = env->regwptr[UREG_FP];
2085 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2086 goto segv_and_exit;
2087 #if 0
2088 fprintf(stderr, "sigreturn\n");
2089 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2090 #endif
2091 //cpu_dump_state(env, stderr, fprintf, 0);
2093 /* 1. Make sure we are not getting garbage from the user */
2095 if (sf_addr & 3)
2096 goto segv_and_exit;
2098 err = __get_user(pc, &sf->info.si_regs.pc);
2099 err |= __get_user(npc, &sf->info.si_regs.npc);
2101 if ((pc | npc) & 3)
2102 goto segv_and_exit;
2104 /* 2. Restore the state */
2105 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2107 /* User can only change condition codes and FPU enabling in %psr. */
2108 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2109 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2111 env->pc = pc;
2112 env->npc = npc;
2113 err |= __get_user(env->y, &sf->info.si_regs.y);
2114 for (i=0; i < 8; i++) {
2115 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2117 for (i=0; i < 8; i++) {
2118 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2121 err |= __get_user(fpu_save_addr, &sf->fpu_save);
2123 //if (fpu_save)
2124 // err |= restore_fpu_state(env, fpu_save);
2126 /* This is pretty much atomic, no amount locking would prevent
2127 * the races which exist anyways.
2129 err |= __get_user(set.sig[0], &sf->info.si_mask);
2130 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2131 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2134 target_to_host_sigset_internal(&host_set, &set);
2135 sigprocmask(SIG_SETMASK, &host_set, NULL);
2137 if (err)
2138 goto segv_and_exit;
2139 unlock_user_struct(sf, sf_addr, 0);
2140 return env->regwptr[0];
2142 segv_and_exit:
2143 unlock_user_struct(sf, sf_addr, 0);
2144 force_sig(TARGET_SIGSEGV);
2147 long do_rt_sigreturn(CPUState *env)
2149 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2150 return -TARGET_ENOSYS;
2153 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2154 #define MC_TSTATE 0
2155 #define MC_PC 1
2156 #define MC_NPC 2
2157 #define MC_Y 3
2158 #define MC_G1 4
2159 #define MC_G2 5
2160 #define MC_G3 6
2161 #define MC_G4 7
2162 #define MC_G5 8
2163 #define MC_G6 9
2164 #define MC_G7 10
2165 #define MC_O0 11
2166 #define MC_O1 12
2167 #define MC_O2 13
2168 #define MC_O3 14
2169 #define MC_O4 15
2170 #define MC_O5 16
2171 #define MC_O6 17
2172 #define MC_O7 18
2173 #define MC_NGREG 19
2175 typedef abi_ulong target_mc_greg_t;
2176 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2178 struct target_mc_fq {
2179 abi_ulong *mcfq_addr;
2180 uint32_t mcfq_insn;
2183 struct target_mc_fpu {
2184 union {
2185 uint32_t sregs[32];
2186 uint64_t dregs[32];
2187 //uint128_t qregs[16];
2188 } mcfpu_fregs;
2189 abi_ulong mcfpu_fsr;
2190 abi_ulong mcfpu_fprs;
2191 abi_ulong mcfpu_gsr;
2192 struct target_mc_fq *mcfpu_fq;
2193 unsigned char mcfpu_qcnt;
2194 unsigned char mcfpu_qentsz;
2195 unsigned char mcfpu_enab;
2197 typedef struct target_mc_fpu target_mc_fpu_t;
2199 typedef struct {
2200 target_mc_gregset_t mc_gregs;
2201 target_mc_greg_t mc_fp;
2202 target_mc_greg_t mc_i7;
2203 target_mc_fpu_t mc_fpregs;
2204 } target_mcontext_t;
2206 struct target_ucontext {
2207 struct target_ucontext *tuc_link;
2208 abi_ulong tuc_flags;
2209 target_sigset_t tuc_sigmask;
2210 target_mcontext_t tuc_mcontext;
2213 /* A V9 register window */
2214 struct target_reg_window {
2215 abi_ulong locals[8];
2216 abi_ulong ins[8];
2219 #define TARGET_STACK_BIAS 2047
2221 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2222 void sparc64_set_context(CPUSPARCState *env)
2224 abi_ulong ucp_addr;
2225 struct target_ucontext *ucp;
2226 target_mc_gregset_t *grp;
2227 abi_ulong pc, npc, tstate;
2228 abi_ulong fp, i7, w_addr;
2229 unsigned char fenab;
2230 int err;
2231 unsigned int i;
2233 ucp_addr = env->regwptr[UREG_I0];
2234 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2235 goto do_sigsegv;
2236 grp = &ucp->tuc_mcontext.mc_gregs;
2237 err = __get_user(pc, &((*grp)[MC_PC]));
2238 err |= __get_user(npc, &((*grp)[MC_NPC]));
2239 if (err || ((pc | npc) & 3))
2240 goto do_sigsegv;
2241 if (env->regwptr[UREG_I1]) {
2242 target_sigset_t target_set;
2243 sigset_t set;
2245 if (TARGET_NSIG_WORDS == 1) {
2246 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2247 goto do_sigsegv;
2248 } else {
2249 abi_ulong *src, *dst;
2250 src = ucp->tuc_sigmask.sig;
2251 dst = target_set.sig;
2252 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2253 i++, dst++, src++)
2254 err |= __get_user(*dst, src);
2255 if (err)
2256 goto do_sigsegv;
2258 target_to_host_sigset_internal(&set, &target_set);
2259 sigprocmask(SIG_SETMASK, &set, NULL);
2261 env->pc = pc;
2262 env->npc = npc;
2263 err |= __get_user(env->y, &((*grp)[MC_Y]));
2264 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2265 env->asi = (tstate >> 24) & 0xff;
2266 cpu_put_ccr(env, tstate >> 32);
2267 cpu_put_cwp64(env, tstate & 0x1f);
2268 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2269 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2270 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2271 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2272 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2273 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2274 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2275 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2276 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2277 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2278 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2279 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2280 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2281 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2282 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2284 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2285 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2287 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2288 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2289 abi_ulong) != 0)
2290 goto do_sigsegv;
2291 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2292 abi_ulong) != 0)
2293 goto do_sigsegv;
2294 err |= __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2295 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2297 uint32_t *src, *dst;
2298 src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2299 dst = env->fpr;
2300 /* XXX: check that the CPU storage is the same as user context */
2301 for (i = 0; i < 64; i++, dst++, src++)
2302 err |= __get_user(*dst, src);
2304 err |= __get_user(env->fsr,
2305 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2306 err |= __get_user(env->gsr,
2307 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2308 if (err)
2309 goto do_sigsegv;
2310 unlock_user_struct(ucp, ucp_addr, 0);
2311 return;
2312 do_sigsegv:
2313 unlock_user_struct(ucp, ucp_addr, 0);
2314 force_sig(TARGET_SIGSEGV);
2317 void sparc64_get_context(CPUSPARCState *env)
2319 abi_ulong ucp_addr;
2320 struct target_ucontext *ucp;
2321 target_mc_gregset_t *grp;
2322 target_mcontext_t *mcp;
2323 abi_ulong fp, i7, w_addr;
2324 int err;
2325 unsigned int i;
2326 target_sigset_t target_set;
2327 sigset_t set;
2329 ucp_addr = env->regwptr[UREG_I0];
2330 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2331 goto do_sigsegv;
2333 mcp = &ucp->tuc_mcontext;
2334 grp = &mcp->mc_gregs;
2336 /* Skip over the trap instruction, first. */
2337 env->pc = env->npc;
2338 env->npc += 4;
2340 err = 0;
2342 sigprocmask(0, NULL, &set);
2343 host_to_target_sigset_internal(&target_set, &set);
2344 if (TARGET_NSIG_WORDS == 1) {
2345 err |= __put_user(target_set.sig[0],
2346 (abi_ulong *)&ucp->tuc_sigmask);
2347 } else {
2348 abi_ulong *src, *dst;
2349 src = target_set.sig;
2350 dst = ucp->tuc_sigmask.sig;
2351 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2352 i++, dst++, src++)
2353 err |= __put_user(*src, dst);
2354 if (err)
2355 goto do_sigsegv;
2358 /* XXX: tstate must be saved properly */
2359 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2360 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2361 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2362 err |= __put_user(env->y, &((*grp)[MC_Y]));
2363 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2364 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2365 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2366 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2367 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2368 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2369 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2370 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2371 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2372 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2373 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2374 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2375 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2376 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2377 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2379 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2380 fp = i7 = 0;
2381 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2382 abi_ulong) != 0)
2383 goto do_sigsegv;
2384 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2385 abi_ulong) != 0)
2386 goto do_sigsegv;
2387 err |= __put_user(fp, &(mcp->mc_fp));
2388 err |= __put_user(i7, &(mcp->mc_i7));
2391 uint32_t *src, *dst;
2392 src = env->fpr;
2393 dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2394 /* XXX: check that the CPU storage is the same as user context */
2395 for (i = 0; i < 64; i++, dst++, src++)
2396 err |= __put_user(*src, dst);
2398 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2399 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2400 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2402 if (err)
2403 goto do_sigsegv;
2404 unlock_user_struct(ucp, ucp_addr, 1);
2405 return;
2406 do_sigsegv:
2407 unlock_user_struct(ucp, ucp_addr, 1);
2408 force_sig(TARGET_SIGSEGV);
2410 #endif
2411 #elif defined(TARGET_ABI_MIPSN64)
2413 # warning signal handling not implemented
2415 static void setup_frame(int sig, struct target_sigaction *ka,
2416 target_sigset_t *set, CPUState *env)
2418 fprintf(stderr, "setup_frame: not implemented\n");
2421 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2422 target_siginfo_t *info,
2423 target_sigset_t *set, CPUState *env)
2425 fprintf(stderr, "setup_rt_frame: not implemented\n");
2428 long do_sigreturn(CPUState *env)
2430 fprintf(stderr, "do_sigreturn: not implemented\n");
2431 return -TARGET_ENOSYS;
2434 long do_rt_sigreturn(CPUState *env)
2436 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2437 return -TARGET_ENOSYS;
2440 #elif defined(TARGET_ABI_MIPSN32)
2442 # warning signal handling not implemented
2444 static void setup_frame(int sig, struct target_sigaction *ka,
2445 target_sigset_t *set, CPUState *env)
2447 fprintf(stderr, "setup_frame: not implemented\n");
2450 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2451 target_siginfo_t *info,
2452 target_sigset_t *set, CPUState *env)
2454 fprintf(stderr, "setup_rt_frame: not implemented\n");
2457 long do_sigreturn(CPUState *env)
2459 fprintf(stderr, "do_sigreturn: not implemented\n");
2460 return -TARGET_ENOSYS;
2463 long do_rt_sigreturn(CPUState *env)
2465 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2466 return -TARGET_ENOSYS;
2469 #elif defined(TARGET_ABI_MIPSO32)
2471 struct target_sigcontext {
2472 uint32_t sc_regmask; /* Unused */
2473 uint32_t sc_status;
2474 uint64_t sc_pc;
2475 uint64_t sc_regs[32];
2476 uint64_t sc_fpregs[32];
2477 uint32_t sc_ownedfp; /* Unused */
2478 uint32_t sc_fpc_csr;
2479 uint32_t sc_fpc_eir; /* Unused */
2480 uint32_t sc_used_math;
2481 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2482 uint32_t pad0;
2483 uint64_t sc_mdhi;
2484 uint64_t sc_mdlo;
2485 target_ulong sc_hi1; /* Was sc_cause */
2486 target_ulong sc_lo1; /* Was sc_badvaddr */
2487 target_ulong sc_hi2; /* Was sc_sigset[4] */
2488 target_ulong sc_lo2;
2489 target_ulong sc_hi3;
2490 target_ulong sc_lo3;
2493 struct sigframe {
2494 uint32_t sf_ass[4]; /* argument save space for o32 */
2495 uint32_t sf_code[2]; /* signal trampoline */
2496 struct target_sigcontext sf_sc;
2497 target_sigset_t sf_mask;
2500 struct target_ucontext {
2501 target_ulong tuc_flags;
2502 target_ulong tuc_link;
2503 target_stack_t tuc_stack;
2504 target_ulong pad0;
2505 struct target_sigcontext tuc_mcontext;
2506 target_sigset_t tuc_sigmask;
2509 struct target_rt_sigframe {
2510 uint32_t rs_ass[4]; /* argument save space for o32 */
2511 uint32_t rs_code[2]; /* signal trampoline */
2512 struct target_siginfo rs_info;
2513 struct target_ucontext rs_uc;
2516 /* Install trampoline to jump back from signal handler */
2517 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2519 int err;
2522 * Set up the return code ...
2524 * li v0, __NR__foo_sigreturn
2525 * syscall
2528 err = __put_user(0x24020000 + syscall, tramp + 0);
2529 err |= __put_user(0x0000000c , tramp + 1);
2530 /* flush_cache_sigtramp((unsigned long) tramp); */
2531 return err;
2534 static inline int
2535 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2537 int err = 0;
2539 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2541 #define save_gp_reg(i) do { \
2542 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2543 } while(0)
2544 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2545 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2546 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2547 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2548 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2549 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2550 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2551 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2552 save_gp_reg(31);
2553 #undef save_gp_reg
2555 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2556 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2558 /* Not used yet, but might be useful if we ever have DSP suppport */
2559 #if 0
2560 if (cpu_has_dsp) {
2561 err |= __put_user(mfhi1(), &sc->sc_hi1);
2562 err |= __put_user(mflo1(), &sc->sc_lo1);
2563 err |= __put_user(mfhi2(), &sc->sc_hi2);
2564 err |= __put_user(mflo2(), &sc->sc_lo2);
2565 err |= __put_user(mfhi3(), &sc->sc_hi3);
2566 err |= __put_user(mflo3(), &sc->sc_lo3);
2567 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2569 /* same with 64 bit */
2570 #ifdef CONFIG_64BIT
2571 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2572 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2573 if (cpu_has_dsp) {
2574 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2575 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2576 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2577 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2578 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2579 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2580 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2582 #endif
2583 #endif
2585 #if 0
2586 err |= __put_user(!!used_math(), &sc->sc_used_math);
2588 if (!used_math())
2589 goto out;
2592 * Save FPU state to signal context. Signal handler will "inherit"
2593 * current FPU state.
2595 preempt_disable();
2597 if (!is_fpu_owner()) {
2598 own_fpu();
2599 restore_fp(current);
2601 err |= save_fp_context(sc);
2603 preempt_enable();
2604 out:
2605 #endif
2606 return err;
2609 static inline int
2610 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2612 int err = 0;
2614 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2616 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2617 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2619 #define restore_gp_reg(i) do { \
2620 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2621 } while(0)
2622 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2623 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2624 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2625 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2626 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2627 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2628 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2629 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2630 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2631 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2632 restore_gp_reg(31);
2633 #undef restore_gp_reg
2635 #if 0
2636 if (cpu_has_dsp) {
2637 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2638 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2639 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2640 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2641 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2642 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2643 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2645 #ifdef CONFIG_64BIT
2646 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2647 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2648 if (cpu_has_dsp) {
2649 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2650 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2651 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2652 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2653 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2654 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2655 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2657 #endif
2659 err |= __get_user(used_math, &sc->sc_used_math);
2660 conditional_used_math(used_math);
2662 preempt_disable();
2664 if (used_math()) {
2665 /* restore fpu context if we have used it before */
2666 own_fpu();
2667 err |= restore_fp_context(sc);
2668 } else {
2669 /* signal handler may have used FPU. Give it up. */
2670 lose_fpu();
2673 preempt_enable();
2674 #endif
2675 return err;
2678 * Determine which stack to use..
2680 static inline abi_ulong
2681 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2683 unsigned long sp;
2685 /* Default to using normal stack */
2686 sp = regs->active_tc.gpr[29];
2689 * FPU emulator may have it's own trampoline active just
2690 * above the user stack, 16-bytes before the next lowest
2691 * 16 byte boundary. Try to avoid trashing it.
2693 sp -= 32;
2695 /* This is the X/Open sanctioned signal stack switching. */
2696 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2697 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2700 return (sp - frame_size) & ~7;
2703 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2704 static void setup_frame(int sig, struct target_sigaction * ka,
2705 target_sigset_t *set, CPUState *regs)
2707 struct sigframe *frame;
2708 abi_ulong frame_addr;
2709 int i;
2711 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2712 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2713 goto give_sigsegv;
2715 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2717 if(setup_sigcontext(regs, &frame->sf_sc))
2718 goto give_sigsegv;
2720 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2721 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2722 goto give_sigsegv;
2726 * Arguments to signal handler:
2728 * a0 = signal number
2729 * a1 = 0 (should be cause)
2730 * a2 = pointer to struct sigcontext
2732 * $25 and PC point to the signal handler, $29 points to the
2733 * struct sigframe.
2735 regs->active_tc.gpr[ 4] = sig;
2736 regs->active_tc.gpr[ 5] = 0;
2737 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2738 regs->active_tc.gpr[29] = frame_addr;
2739 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2740 /* The original kernel code sets CP0_EPC to the handler
2741 * since it returns to userland using eret
2742 * we cannot do this here, and we must set PC directly */
2743 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2744 unlock_user_struct(frame, frame_addr, 1);
2745 return;
2747 give_sigsegv:
2748 unlock_user_struct(frame, frame_addr, 1);
2749 force_sig(TARGET_SIGSEGV/*, current*/);
2750 return;
2753 long do_sigreturn(CPUState *regs)
2755 struct sigframe *frame;
2756 abi_ulong frame_addr;
2757 sigset_t blocked;
2758 target_sigset_t target_set;
2759 int i;
2761 #if defined(DEBUG_SIGNAL)
2762 fprintf(stderr, "do_sigreturn\n");
2763 #endif
2764 frame_addr = regs->active_tc.gpr[29];
2765 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2766 goto badframe;
2768 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2769 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2770 goto badframe;
2773 target_to_host_sigset_internal(&blocked, &target_set);
2774 sigprocmask(SIG_SETMASK, &blocked, NULL);
2776 if (restore_sigcontext(regs, &frame->sf_sc))
2777 goto badframe;
2779 #if 0
2781 * Don't let your children do this ...
2783 __asm__ __volatile__(
2784 "move\t$29, %0\n\t"
2785 "j\tsyscall_exit"
2786 :/* no outputs */
2787 :"r" (&regs));
2788 /* Unreached */
2789 #endif
2791 regs->active_tc.PC = regs->CP0_EPC;
2792 /* I am not sure this is right, but it seems to work
2793 * maybe a problem with nested signals ? */
2794 regs->CP0_EPC = 0;
2795 return -TARGET_QEMU_ESIGRETURN;
2797 badframe:
2798 force_sig(TARGET_SIGSEGV/*, current*/);
2799 return 0;
2802 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2803 target_siginfo_t *info,
2804 target_sigset_t *set, CPUState *env)
2806 struct target_rt_sigframe *frame;
2807 abi_ulong frame_addr;
2808 int i;
2810 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2811 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2812 goto give_sigsegv;
2814 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2816 copy_siginfo_to_user(&frame->rs_info, info);
2818 __put_user(0, &frame->rs_uc.tuc_flags);
2819 __put_user(0, &frame->rs_uc.tuc_link);
2820 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2821 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2822 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2823 &frame->rs_uc.tuc_stack.ss_flags);
2825 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2827 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2828 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2832 * Arguments to signal handler:
2834 * a0 = signal number
2835 * a1 = pointer to struct siginfo
2836 * a2 = pointer to struct ucontext
2838 * $25 and PC point to the signal handler, $29 points to the
2839 * struct sigframe.
2841 env->active_tc.gpr[ 4] = sig;
2842 env->active_tc.gpr[ 5] = frame_addr
2843 + offsetof(struct target_rt_sigframe, rs_info);
2844 env->active_tc.gpr[ 6] = frame_addr
2845 + offsetof(struct target_rt_sigframe, rs_uc);
2846 env->active_tc.gpr[29] = frame_addr;
2847 env->active_tc.gpr[31] = frame_addr
2848 + offsetof(struct target_rt_sigframe, rs_code);
2849 /* The original kernel code sets CP0_EPC to the handler
2850 * since it returns to userland using eret
2851 * we cannot do this here, and we must set PC directly */
2852 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2853 unlock_user_struct(frame, frame_addr, 1);
2854 return;
2856 give_sigsegv:
2857 unlock_user_struct(frame, frame_addr, 1);
2858 force_sig(TARGET_SIGSEGV/*, current*/);
2859 return;
2862 long do_rt_sigreturn(CPUState *env)
2864 struct target_rt_sigframe *frame;
2865 abi_ulong frame_addr;
2866 sigset_t blocked;
2868 #if defined(DEBUG_SIGNAL)
2869 fprintf(stderr, "do_rt_sigreturn\n");
2870 #endif
2871 frame_addr = env->active_tc.gpr[29];
2872 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2873 goto badframe;
2875 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2876 sigprocmask(SIG_SETMASK, &blocked, NULL);
2878 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2879 goto badframe;
2881 if (do_sigaltstack(frame_addr +
2882 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2883 0, get_sp_from_cpustate(env)) == -EFAULT)
2884 goto badframe;
2886 env->active_tc.PC = env->CP0_EPC;
2887 /* I am not sure this is right, but it seems to work
2888 * maybe a problem with nested signals ? */
2889 env->CP0_EPC = 0;
2890 return -TARGET_QEMU_ESIGRETURN;
2892 badframe:
2893 force_sig(TARGET_SIGSEGV/*, current*/);
2894 return 0;
2897 #elif defined(TARGET_SH4)
2900 * code and data structures from linux kernel:
2901 * include/asm-sh/sigcontext.h
2902 * arch/sh/kernel/signal.c
2905 struct target_sigcontext {
2906 target_ulong oldmask;
2908 /* CPU registers */
2909 target_ulong sc_gregs[16];
2910 target_ulong sc_pc;
2911 target_ulong sc_pr;
2912 target_ulong sc_sr;
2913 target_ulong sc_gbr;
2914 target_ulong sc_mach;
2915 target_ulong sc_macl;
2917 /* FPU registers */
2918 target_ulong sc_fpregs[16];
2919 target_ulong sc_xfpregs[16];
2920 unsigned int sc_fpscr;
2921 unsigned int sc_fpul;
2922 unsigned int sc_ownedfp;
2925 struct target_sigframe
2927 struct target_sigcontext sc;
2928 target_ulong extramask[TARGET_NSIG_WORDS-1];
2929 uint16_t retcode[3];
2933 struct target_ucontext {
2934 target_ulong tuc_flags;
2935 struct target_ucontext *tuc_link;
2936 target_stack_t tuc_stack;
2937 struct target_sigcontext tuc_mcontext;
2938 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2941 struct target_rt_sigframe
2943 struct target_siginfo info;
2944 struct target_ucontext uc;
2945 uint16_t retcode[3];
2949 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2950 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2952 static abi_ulong get_sigframe(struct target_sigaction *ka,
2953 unsigned long sp, size_t frame_size)
2955 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2956 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2959 return (sp - frame_size) & -8ul;
2962 static int setup_sigcontext(struct target_sigcontext *sc,
2963 CPUState *regs, unsigned long mask)
2965 int err = 0;
2966 int i;
2968 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2969 COPY(gregs[0]); COPY(gregs[1]);
2970 COPY(gregs[2]); COPY(gregs[3]);
2971 COPY(gregs[4]); COPY(gregs[5]);
2972 COPY(gregs[6]); COPY(gregs[7]);
2973 COPY(gregs[8]); COPY(gregs[9]);
2974 COPY(gregs[10]); COPY(gregs[11]);
2975 COPY(gregs[12]); COPY(gregs[13]);
2976 COPY(gregs[14]); COPY(gregs[15]);
2977 COPY(gbr); COPY(mach);
2978 COPY(macl); COPY(pr);
2979 COPY(sr); COPY(pc);
2980 #undef COPY
2982 for (i=0; i<16; i++) {
2983 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2985 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2986 err |= __put_user(regs->fpul, &sc->sc_fpul);
2988 /* non-iBCS2 extensions.. */
2989 err |= __put_user(mask, &sc->oldmask);
2991 return err;
2994 static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
2995 target_ulong *r0_p)
2997 unsigned int err = 0;
2998 int i;
3000 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3001 COPY(gregs[1]);
3002 COPY(gregs[2]); COPY(gregs[3]);
3003 COPY(gregs[4]); COPY(gregs[5]);
3004 COPY(gregs[6]); COPY(gregs[7]);
3005 COPY(gregs[8]); COPY(gregs[9]);
3006 COPY(gregs[10]); COPY(gregs[11]);
3007 COPY(gregs[12]); COPY(gregs[13]);
3008 COPY(gregs[14]); COPY(gregs[15]);
3009 COPY(gbr); COPY(mach);
3010 COPY(macl); COPY(pr);
3011 COPY(sr); COPY(pc);
3012 #undef COPY
3014 for (i=0; i<16; i++) {
3015 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3017 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3018 err |= __get_user(regs->fpul, &sc->sc_fpul);
3020 regs->tra = -1; /* disable syscall checks */
3021 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3022 return err;
3025 static void setup_frame(int sig, struct target_sigaction *ka,
3026 target_sigset_t *set, CPUState *regs)
3028 struct target_sigframe *frame;
3029 abi_ulong frame_addr;
3030 int i;
3031 int err = 0;
3032 int signal;
3034 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3035 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3036 goto give_sigsegv;
3038 signal = current_exec_domain_sig(sig);
3040 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3042 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3043 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3046 /* Set up to return from userspace. If provided, use a stub
3047 already in userspace. */
3048 if (ka->sa_flags & TARGET_SA_RESTORER) {
3049 regs->pr = (unsigned long) ka->sa_restorer;
3050 } else {
3051 /* Generate return code (system call to sigreturn) */
3052 err |= __put_user(MOVW(2), &frame->retcode[0]);
3053 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3054 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3055 regs->pr = (unsigned long) frame->retcode;
3058 if (err)
3059 goto give_sigsegv;
3061 /* Set up registers for signal handler */
3062 regs->gregs[15] = (unsigned long) frame;
3063 regs->gregs[4] = signal; /* Arg for signal handler */
3064 regs->gregs[5] = 0;
3065 regs->gregs[6] = (unsigned long) &frame->sc;
3066 regs->pc = (unsigned long) ka->_sa_handler;
3068 unlock_user_struct(frame, frame_addr, 1);
3069 return;
3071 give_sigsegv:
3072 unlock_user_struct(frame, frame_addr, 1);
3073 force_sig(TARGET_SIGSEGV);
3076 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3077 target_siginfo_t *info,
3078 target_sigset_t *set, CPUState *regs)
3080 struct target_rt_sigframe *frame;
3081 abi_ulong frame_addr;
3082 int i;
3083 int err = 0;
3084 int signal;
3086 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3087 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3088 goto give_sigsegv;
3090 signal = current_exec_domain_sig(sig);
3092 err |= copy_siginfo_to_user(&frame->info, info);
3094 /* Create the ucontext. */
3095 err |= __put_user(0, &frame->uc.tuc_flags);
3096 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3097 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3098 &frame->uc.tuc_stack.ss_sp);
3099 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3100 &frame->uc.tuc_stack.ss_flags);
3101 err |= __put_user(target_sigaltstack_used.ss_size,
3102 &frame->uc.tuc_stack.ss_size);
3103 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3104 regs, set->sig[0]);
3105 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3106 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3109 /* Set up to return from userspace. If provided, use a stub
3110 already in userspace. */
3111 if (ka->sa_flags & TARGET_SA_RESTORER) {
3112 regs->pr = (unsigned long) ka->sa_restorer;
3113 } else {
3114 /* Generate return code (system call to sigreturn) */
3115 err |= __put_user(MOVW(2), &frame->retcode[0]);
3116 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3117 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3118 regs->pr = (unsigned long) frame->retcode;
3121 if (err)
3122 goto give_sigsegv;
3124 /* Set up registers for signal handler */
3125 regs->gregs[15] = (unsigned long) frame;
3126 regs->gregs[4] = signal; /* Arg for signal handler */
3127 regs->gregs[5] = (unsigned long) &frame->info;
3128 regs->gregs[6] = (unsigned long) &frame->uc;
3129 regs->pc = (unsigned long) ka->_sa_handler;
3131 unlock_user_struct(frame, frame_addr, 1);
3132 return;
3134 give_sigsegv:
3135 unlock_user_struct(frame, frame_addr, 1);
3136 force_sig(TARGET_SIGSEGV);
3139 long do_sigreturn(CPUState *regs)
3141 struct target_sigframe *frame;
3142 abi_ulong frame_addr;
3143 sigset_t blocked;
3144 target_sigset_t target_set;
3145 target_ulong r0;
3146 int i;
3147 int err = 0;
3149 #if defined(DEBUG_SIGNAL)
3150 fprintf(stderr, "do_sigreturn\n");
3151 #endif
3152 frame_addr = regs->gregs[15];
3153 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3154 goto badframe;
3156 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3157 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3158 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3161 if (err)
3162 goto badframe;
3164 target_to_host_sigset_internal(&blocked, &target_set);
3165 sigprocmask(SIG_SETMASK, &blocked, NULL);
3167 if (restore_sigcontext(regs, &frame->sc, &r0))
3168 goto badframe;
3170 unlock_user_struct(frame, frame_addr, 0);
3171 return r0;
3173 badframe:
3174 unlock_user_struct(frame, frame_addr, 0);
3175 force_sig(TARGET_SIGSEGV);
3176 return 0;
3179 long do_rt_sigreturn(CPUState *regs)
3181 struct target_rt_sigframe *frame;
3182 abi_ulong frame_addr;
3183 sigset_t blocked;
3184 target_ulong r0;
3186 #if defined(DEBUG_SIGNAL)
3187 fprintf(stderr, "do_rt_sigreturn\n");
3188 #endif
3189 frame_addr = regs->gregs[15];
3190 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3191 goto badframe;
3193 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3194 sigprocmask(SIG_SETMASK, &blocked, NULL);
3196 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3197 goto badframe;
3199 if (do_sigaltstack(frame_addr +
3200 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3201 0, get_sp_from_cpustate(regs)) == -EFAULT)
3202 goto badframe;
3204 unlock_user_struct(frame, frame_addr, 0);
3205 return r0;
3207 badframe:
3208 unlock_user_struct(frame, frame_addr, 0);
3209 force_sig(TARGET_SIGSEGV);
3210 return 0;
3212 #elif defined(TARGET_MICROBLAZE)
3214 struct target_sigcontext {
3215 struct target_pt_regs regs; /* needs to be first */
3216 uint32_t oldmask;
3219 struct target_stack_t {
3220 abi_ulong ss_sp;
3221 int ss_flags;
3222 unsigned int ss_size;
3225 struct target_ucontext {
3226 abi_ulong tuc_flags;
3227 abi_ulong tuc_link;
3228 struct target_stack_t tuc_stack;
3229 struct target_sigcontext tuc_mcontext;
3230 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3233 /* Signal frames. */
3234 struct target_signal_frame {
3235 struct target_ucontext uc;
3236 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3237 uint32_t tramp[2];
3240 struct rt_signal_frame {
3241 struct siginfo info;
3242 struct ucontext uc;
3243 uint32_t tramp[2];
3246 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3248 __put_user(env->regs[0], &sc->regs.r0);
3249 __put_user(env->regs[1], &sc->regs.r1);
3250 __put_user(env->regs[2], &sc->regs.r2);
3251 __put_user(env->regs[3], &sc->regs.r3);
3252 __put_user(env->regs[4], &sc->regs.r4);
3253 __put_user(env->regs[5], &sc->regs.r5);
3254 __put_user(env->regs[6], &sc->regs.r6);
3255 __put_user(env->regs[7], &sc->regs.r7);
3256 __put_user(env->regs[8], &sc->regs.r8);
3257 __put_user(env->regs[9], &sc->regs.r9);
3258 __put_user(env->regs[10], &sc->regs.r10);
3259 __put_user(env->regs[11], &sc->regs.r11);
3260 __put_user(env->regs[12], &sc->regs.r12);
3261 __put_user(env->regs[13], &sc->regs.r13);
3262 __put_user(env->regs[14], &sc->regs.r14);
3263 __put_user(env->regs[15], &sc->regs.r15);
3264 __put_user(env->regs[16], &sc->regs.r16);
3265 __put_user(env->regs[17], &sc->regs.r17);
3266 __put_user(env->regs[18], &sc->regs.r18);
3267 __put_user(env->regs[19], &sc->regs.r19);
3268 __put_user(env->regs[20], &sc->regs.r20);
3269 __put_user(env->regs[21], &sc->regs.r21);
3270 __put_user(env->regs[22], &sc->regs.r22);
3271 __put_user(env->regs[23], &sc->regs.r23);
3272 __put_user(env->regs[24], &sc->regs.r24);
3273 __put_user(env->regs[25], &sc->regs.r25);
3274 __put_user(env->regs[26], &sc->regs.r26);
3275 __put_user(env->regs[27], &sc->regs.r27);
3276 __put_user(env->regs[28], &sc->regs.r28);
3277 __put_user(env->regs[29], &sc->regs.r29);
3278 __put_user(env->regs[30], &sc->regs.r30);
3279 __put_user(env->regs[31], &sc->regs.r31);
3280 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3283 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3285 __get_user(env->regs[0], &sc->regs.r0);
3286 __get_user(env->regs[1], &sc->regs.r1);
3287 __get_user(env->regs[2], &sc->regs.r2);
3288 __get_user(env->regs[3], &sc->regs.r3);
3289 __get_user(env->regs[4], &sc->regs.r4);
3290 __get_user(env->regs[5], &sc->regs.r5);
3291 __get_user(env->regs[6], &sc->regs.r6);
3292 __get_user(env->regs[7], &sc->regs.r7);
3293 __get_user(env->regs[8], &sc->regs.r8);
3294 __get_user(env->regs[9], &sc->regs.r9);
3295 __get_user(env->regs[10], &sc->regs.r10);
3296 __get_user(env->regs[11], &sc->regs.r11);
3297 __get_user(env->regs[12], &sc->regs.r12);
3298 __get_user(env->regs[13], &sc->regs.r13);
3299 __get_user(env->regs[14], &sc->regs.r14);
3300 __get_user(env->regs[15], &sc->regs.r15);
3301 __get_user(env->regs[16], &sc->regs.r16);
3302 __get_user(env->regs[17], &sc->regs.r17);
3303 __get_user(env->regs[18], &sc->regs.r18);
3304 __get_user(env->regs[19], &sc->regs.r19);
3305 __get_user(env->regs[20], &sc->regs.r20);
3306 __get_user(env->regs[21], &sc->regs.r21);
3307 __get_user(env->regs[22], &sc->regs.r22);
3308 __get_user(env->regs[23], &sc->regs.r23);
3309 __get_user(env->regs[24], &sc->regs.r24);
3310 __get_user(env->regs[25], &sc->regs.r25);
3311 __get_user(env->regs[26], &sc->regs.r26);
3312 __get_user(env->regs[27], &sc->regs.r27);
3313 __get_user(env->regs[28], &sc->regs.r28);
3314 __get_user(env->regs[29], &sc->regs.r29);
3315 __get_user(env->regs[30], &sc->regs.r30);
3316 __get_user(env->regs[31], &sc->regs.r31);
3317 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3320 static abi_ulong get_sigframe(struct target_sigaction *ka,
3321 CPUState *env, int frame_size)
3323 abi_ulong sp = env->regs[1];
3325 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3326 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3328 return ((sp - frame_size) & -8UL);
3331 static void setup_frame(int sig, struct target_sigaction *ka,
3332 target_sigset_t *set, CPUState *env)
3334 struct target_signal_frame *frame;
3335 abi_ulong frame_addr;
3336 int err = 0;
3337 int i;
3339 frame_addr = get_sigframe(ka, env, sizeof *frame);
3340 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3341 goto badframe;
3343 /* Save the mask. */
3344 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3345 if (err)
3346 goto badframe;
3348 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3349 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3350 goto badframe;
3353 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3355 /* Set up to return from userspace. If provided, use a stub
3356 already in userspace. */
3357 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3358 if (ka->sa_flags & TARGET_SA_RESTORER) {
3359 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3360 } else {
3361 uint32_t t;
3362 /* Note, these encodings are _big endian_! */
3363 /* addi r12, r0, __NR_sigreturn */
3364 t = 0x31800000UL | TARGET_NR_sigreturn;
3365 err |= __put_user(t, frame->tramp + 0);
3366 /* brki r14, 0x8 */
3367 t = 0xb9cc0008UL;
3368 err |= __put_user(t, frame->tramp + 1);
3370 /* Return from sighandler will jump to the tramp.
3371 Negative 8 offset because return is rtsd r15, 8 */
3372 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3375 if (err)
3376 goto badframe;
3378 /* Set up registers for signal handler */
3379 env->regs[1] = (unsigned long) frame;
3380 /* Signal handler args: */
3381 env->regs[5] = sig; /* Arg 0: signum */
3382 env->regs[6] = 0;
3383 env->regs[7] = (unsigned long) &frame->uc; /* arg 1: sigcontext */
3385 /* Offset of 4 to handle microblaze rtid r14, 0 */
3386 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3388 unlock_user_struct(frame, frame_addr, 1);
3389 return;
3390 badframe:
3391 unlock_user_struct(frame, frame_addr, 1);
3392 force_sig(TARGET_SIGSEGV);
3395 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3396 target_siginfo_t *info,
3397 target_sigset_t *set, CPUState *env)
3399 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3402 long do_sigreturn(CPUState *env)
3404 struct target_signal_frame *frame;
3405 abi_ulong frame_addr;
3406 target_sigset_t target_set;
3407 sigset_t set;
3408 int i;
3410 frame_addr = env->regs[R_SP];
3411 /* Make sure the guest isn't playing games. */
3412 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3413 goto badframe;
3415 /* Restore blocked signals */
3416 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3417 goto badframe;
3418 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3419 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3420 goto badframe;
3422 target_to_host_sigset_internal(&set, &target_set);
3423 sigprocmask(SIG_SETMASK, &set, NULL);
3425 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3426 /* We got here through a sigreturn syscall, our path back is via an
3427 rtb insn so setup r14 for that. */
3428 env->regs[14] = env->sregs[SR_PC];
3430 unlock_user_struct(frame, frame_addr, 0);
3431 return env->regs[10];
3432 badframe:
3433 unlock_user_struct(frame, frame_addr, 0);
3434 force_sig(TARGET_SIGSEGV);
3437 long do_rt_sigreturn(CPUState *env)
3439 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3440 return -TARGET_ENOSYS;
3443 #elif defined(TARGET_CRIS)
3445 struct target_sigcontext {
3446 struct target_pt_regs regs; /* needs to be first */
3447 uint32_t oldmask;
3448 uint32_t usp; /* usp before stacking this gunk on it */
3451 /* Signal frames. */
3452 struct target_signal_frame {
3453 struct target_sigcontext sc;
3454 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3455 uint8_t retcode[8]; /* Trampoline code. */
3458 struct rt_signal_frame {
3459 struct siginfo *pinfo;
3460 void *puc;
3461 struct siginfo info;
3462 struct ucontext uc;
3463 uint8_t retcode[8]; /* Trampoline code. */
3466 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3468 __put_user(env->regs[0], &sc->regs.r0);
3469 __put_user(env->regs[1], &sc->regs.r1);
3470 __put_user(env->regs[2], &sc->regs.r2);
3471 __put_user(env->regs[3], &sc->regs.r3);
3472 __put_user(env->regs[4], &sc->regs.r4);
3473 __put_user(env->regs[5], &sc->regs.r5);
3474 __put_user(env->regs[6], &sc->regs.r6);
3475 __put_user(env->regs[7], &sc->regs.r7);
3476 __put_user(env->regs[8], &sc->regs.r8);
3477 __put_user(env->regs[9], &sc->regs.r9);
3478 __put_user(env->regs[10], &sc->regs.r10);
3479 __put_user(env->regs[11], &sc->regs.r11);
3480 __put_user(env->regs[12], &sc->regs.r12);
3481 __put_user(env->regs[13], &sc->regs.r13);
3482 __put_user(env->regs[14], &sc->usp);
3483 __put_user(env->regs[15], &sc->regs.acr);
3484 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3485 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3486 __put_user(env->pc, &sc->regs.erp);
3489 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3491 __get_user(env->regs[0], &sc->regs.r0);
3492 __get_user(env->regs[1], &sc->regs.r1);
3493 __get_user(env->regs[2], &sc->regs.r2);
3494 __get_user(env->regs[3], &sc->regs.r3);
3495 __get_user(env->regs[4], &sc->regs.r4);
3496 __get_user(env->regs[5], &sc->regs.r5);
3497 __get_user(env->regs[6], &sc->regs.r6);
3498 __get_user(env->regs[7], &sc->regs.r7);
3499 __get_user(env->regs[8], &sc->regs.r8);
3500 __get_user(env->regs[9], &sc->regs.r9);
3501 __get_user(env->regs[10], &sc->regs.r10);
3502 __get_user(env->regs[11], &sc->regs.r11);
3503 __get_user(env->regs[12], &sc->regs.r12);
3504 __get_user(env->regs[13], &sc->regs.r13);
3505 __get_user(env->regs[14], &sc->usp);
3506 __get_user(env->regs[15], &sc->regs.acr);
3507 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3508 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3509 __get_user(env->pc, &sc->regs.erp);
3512 static abi_ulong get_sigframe(CPUState *env, int framesize)
3514 abi_ulong sp;
3515 /* Align the stack downwards to 4. */
3516 sp = (env->regs[R_SP] & ~3);
3517 return sp - framesize;
3520 static void setup_frame(int sig, struct target_sigaction *ka,
3521 target_sigset_t *set, CPUState *env)
3523 struct target_signal_frame *frame;
3524 abi_ulong frame_addr;
3525 int err = 0;
3526 int i;
3528 frame_addr = get_sigframe(env, sizeof *frame);
3529 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3530 goto badframe;
3533 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3534 * use this trampoline anymore but it sets it up for GDB.
3535 * In QEMU, using the trampoline simplifies things a bit so we use it.
3537 * This is movu.w __NR_sigreturn, r9; break 13;
3539 err |= __put_user(0x9c5f, frame->retcode+0);
3540 err |= __put_user(TARGET_NR_sigreturn,
3541 frame->retcode+2);
3542 err |= __put_user(0xe93d, frame->retcode+4);
3544 /* Save the mask. */
3545 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3546 if (err)
3547 goto badframe;
3549 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3550 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3551 goto badframe;
3554 setup_sigcontext(&frame->sc, env);
3556 /* Move the stack and setup the arguments for the handler. */
3557 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
3558 env->regs[10] = sig;
3559 env->pc = (unsigned long) ka->_sa_handler;
3560 /* Link SRP so the guest returns through the trampoline. */
3561 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
3563 unlock_user_struct(frame, frame_addr, 1);
3564 return;
3565 badframe:
3566 unlock_user_struct(frame, frame_addr, 1);
3567 force_sig(TARGET_SIGSEGV);
3570 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3571 target_siginfo_t *info,
3572 target_sigset_t *set, CPUState *env)
3574 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3577 long do_sigreturn(CPUState *env)
3579 struct target_signal_frame *frame;
3580 abi_ulong frame_addr;
3581 target_sigset_t target_set;
3582 sigset_t set;
3583 int i;
3585 frame_addr = env->regs[R_SP];
3586 /* Make sure the guest isn't playing games. */
3587 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3588 goto badframe;
3590 /* Restore blocked signals */
3591 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3592 goto badframe;
3593 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3594 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3595 goto badframe;
3597 target_to_host_sigset_internal(&set, &target_set);
3598 sigprocmask(SIG_SETMASK, &set, NULL);
3600 restore_sigcontext(&frame->sc, env);
3601 unlock_user_struct(frame, frame_addr, 0);
3602 return env->regs[10];
3603 badframe:
3604 unlock_user_struct(frame, frame_addr, 0);
3605 force_sig(TARGET_SIGSEGV);
3608 long do_rt_sigreturn(CPUState *env)
3610 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3611 return -TARGET_ENOSYS;
3614 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3616 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
3617 the signal handling is different enough that we haven't implemented
3618 support for PPC64 yet. Hence the restriction above.
3620 There are various #if'd blocks for code for TARGET_PPC64. These
3621 blocks should go away so that we can successfully run 32-bit and
3622 64-bit binaries on a QEMU configured for PPC64. */
3624 /* Size of dummy stack frame allocated when calling signal handler.
3625 See arch/powerpc/include/asm/ptrace.h. */
3626 #if defined(TARGET_PPC64)
3627 #define SIGNAL_FRAMESIZE 128
3628 #else
3629 #define SIGNAL_FRAMESIZE 64
3630 #endif
3632 /* See arch/powerpc/include/asm/sigcontext.h. */
3633 struct target_sigcontext {
3634 target_ulong _unused[4];
3635 int32_t signal;
3636 #if defined(TARGET_PPC64)
3637 int32_t pad0;
3638 #endif
3639 target_ulong handler;
3640 target_ulong oldmask;
3641 target_ulong regs; /* struct pt_regs __user * */
3642 /* TODO: PPC64 includes extra bits here. */
3645 /* Indices for target_mcontext.mc_gregs, below.
3646 See arch/powerpc/include/asm/ptrace.h for details. */
3647 enum {
3648 TARGET_PT_R0 = 0,
3649 TARGET_PT_R1 = 1,
3650 TARGET_PT_R2 = 2,
3651 TARGET_PT_R3 = 3,
3652 TARGET_PT_R4 = 4,
3653 TARGET_PT_R5 = 5,
3654 TARGET_PT_R6 = 6,
3655 TARGET_PT_R7 = 7,
3656 TARGET_PT_R8 = 8,
3657 TARGET_PT_R9 = 9,
3658 TARGET_PT_R10 = 10,
3659 TARGET_PT_R11 = 11,
3660 TARGET_PT_R12 = 12,
3661 TARGET_PT_R13 = 13,
3662 TARGET_PT_R14 = 14,
3663 TARGET_PT_R15 = 15,
3664 TARGET_PT_R16 = 16,
3665 TARGET_PT_R17 = 17,
3666 TARGET_PT_R18 = 18,
3667 TARGET_PT_R19 = 19,
3668 TARGET_PT_R20 = 20,
3669 TARGET_PT_R21 = 21,
3670 TARGET_PT_R22 = 22,
3671 TARGET_PT_R23 = 23,
3672 TARGET_PT_R24 = 24,
3673 TARGET_PT_R25 = 25,
3674 TARGET_PT_R26 = 26,
3675 TARGET_PT_R27 = 27,
3676 TARGET_PT_R28 = 28,
3677 TARGET_PT_R29 = 29,
3678 TARGET_PT_R30 = 30,
3679 TARGET_PT_R31 = 31,
3680 TARGET_PT_NIP = 32,
3681 TARGET_PT_MSR = 33,
3682 TARGET_PT_ORIG_R3 = 34,
3683 TARGET_PT_CTR = 35,
3684 TARGET_PT_LNK = 36,
3685 TARGET_PT_XER = 37,
3686 TARGET_PT_CCR = 38,
3687 /* Yes, there are two registers with #39. One is 64-bit only. */
3688 TARGET_PT_MQ = 39,
3689 TARGET_PT_SOFTE = 39,
3690 TARGET_PT_TRAP = 40,
3691 TARGET_PT_DAR = 41,
3692 TARGET_PT_DSISR = 42,
3693 TARGET_PT_RESULT = 43,
3694 TARGET_PT_REGS_COUNT = 44
3697 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
3698 on 64-bit PPC, sigcontext and mcontext are one and the same. */
3699 struct target_mcontext {
3700 target_ulong mc_gregs[48];
3701 /* Includes fpscr. */
3702 uint64_t mc_fregs[33];
3703 target_ulong mc_pad[2];
3704 /* We need to handle Altivec and SPE at the same time, which no
3705 kernel needs to do. Fortunately, the kernel defines this bit to
3706 be Altivec-register-large all the time, rather than trying to
3707 twiddle it based on the specific platform. */
3708 union {
3709 /* SPE vector registers. One extra for SPEFSCR. */
3710 uint32_t spe[33];
3711 /* Altivec vector registers. The packing of VSCR and VRSAVE
3712 varies depending on whether we're PPC64 or not: PPC64 splits
3713 them apart; PPC32 stuffs them together. */
3714 #if defined(TARGET_PPC64)
3715 #define QEMU_NVRREG 34
3716 #else
3717 #define QEMU_NVRREG 33
3718 #endif
3719 ppc_avr_t altivec[QEMU_NVRREG];
3720 #undef QEMU_NVRREG
3721 } mc_vregs __attribute__((__aligned__(16)));
3724 struct target_ucontext {
3725 target_ulong tuc_flags;
3726 target_ulong tuc_link; /* struct ucontext __user * */
3727 struct target_sigaltstack tuc_stack;
3728 #if !defined(TARGET_PPC64)
3729 int32_t tuc_pad[7];
3730 target_ulong tuc_regs; /* struct mcontext __user *
3731 points to uc_mcontext field */
3732 #endif
3733 target_sigset_t tuc_sigmask;
3734 #if defined(TARGET_PPC64)
3735 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
3736 struct target_sigcontext tuc_mcontext;
3737 #else
3738 int32_t tuc_maskext[30];
3739 int32_t tuc_pad2[3];
3740 struct target_mcontext tuc_mcontext;
3741 #endif
3744 /* See arch/powerpc/kernel/signal_32.c. */
3745 struct target_sigframe {
3746 struct target_sigcontext sctx;
3747 struct target_mcontext mctx;
3748 int32_t abigap[56];
3751 struct target_rt_sigframe {
3752 struct target_siginfo info;
3753 struct target_ucontext uc;
3754 int32_t abigap[56];
3757 /* We use the mc_pad field for the signal return trampoline. */
3758 #define tramp mc_pad
3760 /* See arch/powerpc/kernel/signal.c. */
3761 static target_ulong get_sigframe(struct target_sigaction *ka,
3762 CPUState *env,
3763 int frame_size)
3765 target_ulong oldsp, newsp;
3767 oldsp = env->gpr[1];
3769 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
3770 (sas_ss_flags(oldsp))) {
3771 oldsp = (target_sigaltstack_used.ss_sp
3772 + target_sigaltstack_used.ss_size);
3775 newsp = (oldsp - frame_size) & ~0xFUL;
3777 return newsp;
3780 static int save_user_regs(CPUState *env, struct target_mcontext *frame,
3781 int sigret)
3783 target_ulong msr = env->msr;
3784 int i;
3785 target_ulong ccr = 0;
3787 /* In general, the kernel attempts to be intelligent about what it
3788 needs to save for Altivec/FP/SPE registers. We don't care that
3789 much, so we just go ahead and save everything. */
3791 /* Save general registers. */
3792 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3793 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
3794 return 1;
3797 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3798 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3799 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3800 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3801 return 1;
3803 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3804 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
3806 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3807 return 1;
3809 /* Save Altivec registers if necessary. */
3810 if (env->insns_flags & PPC_ALTIVEC) {
3811 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3812 ppc_avr_t *avr = &env->avr[i];
3813 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3815 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
3816 __put_user(avr->u64[1], &vreg->u64[1])) {
3817 return 1;
3820 /* Set MSR_VR in the saved MSR value to indicate that
3821 frame->mc_vregs contains valid data. */
3822 msr |= MSR_VR;
3823 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
3824 &frame->mc_vregs.altivec[32].u32[3]))
3825 return 1;
3828 /* Save floating point registers. */
3829 if (env->insns_flags & PPC_FLOAT) {
3830 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3831 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
3832 return 1;
3835 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
3836 return 1;
3839 /* Save SPE registers. The kernel only saves the high half. */
3840 if (env->insns_flags & PPC_SPE) {
3841 #if defined(TARGET_PPC64)
3842 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3843 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
3844 return 1;
3847 #else
3848 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3849 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3850 return 1;
3853 #endif
3854 /* Set MSR_SPE in the saved MSR value to indicate that
3855 frame->mc_vregs contains valid data. */
3856 msr |= MSR_SPE;
3857 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3858 return 1;
3861 /* Store MSR. */
3862 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3863 return 1;
3865 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
3866 if (sigret) {
3867 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
3868 __put_user(0x44000002UL, &frame->tramp[1])) {
3869 return 1;
3873 return 0;
3876 static int restore_user_regs(CPUState *env,
3877 struct target_mcontext *frame, int sig)
3879 target_ulong save_r2 = 0;
3880 target_ulong msr;
3881 target_ulong ccr;
3883 int i;
3885 if (!sig) {
3886 save_r2 = env->gpr[2];
3889 /* Restore general registers. */
3890 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3891 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
3892 return 1;
3895 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3896 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3897 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3898 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3899 return 1;
3900 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3901 return 1;
3903 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3904 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
3907 if (!sig) {
3908 env->gpr[2] = save_r2;
3910 /* Restore MSR. */
3911 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3912 return 1;
3914 /* If doing signal return, restore the previous little-endian mode. */
3915 if (sig)
3916 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
3918 /* Restore Altivec registers if necessary. */
3919 if (env->insns_flags & PPC_ALTIVEC) {
3920 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3921 ppc_avr_t *avr = &env->avr[i];
3922 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3924 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
3925 __get_user(avr->u64[1], &vreg->u64[1])) {
3926 return 1;
3929 /* Set MSR_VEC in the saved MSR value to indicate that
3930 frame->mc_vregs contains valid data. */
3931 if (__get_user(env->spr[SPR_VRSAVE],
3932 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
3933 return 1;
3936 /* Restore floating point registers. */
3937 if (env->insns_flags & PPC_FLOAT) {
3938 uint64_t fpscr;
3939 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3940 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
3941 return 1;
3944 if (__get_user(fpscr, &frame->mc_fregs[32]))
3945 return 1;
3946 env->fpscr = (uint32_t) fpscr;
3949 /* Save SPE registers. The kernel only saves the high half. */
3950 if (env->insns_flags & PPC_SPE) {
3951 #if defined(TARGET_PPC64)
3952 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3953 uint32_t hi;
3955 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
3956 return 1;
3958 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
3960 #else
3961 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3962 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3963 return 1;
3966 #endif
3967 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3968 return 1;
3971 return 0;
3974 static void setup_frame(int sig, struct target_sigaction *ka,
3975 target_sigset_t *set, CPUState *env)
3977 struct target_sigframe *frame;
3978 struct target_sigcontext *sc;
3979 target_ulong frame_addr, newsp;
3980 int err = 0;
3981 int signal;
3983 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3984 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3985 goto sigsegv;
3986 sc = &frame->sctx;
3988 signal = current_exec_domain_sig(sig);
3990 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
3991 err |= __put_user(set->sig[0], &sc->oldmask);
3992 #if defined(TARGET_PPC64)
3993 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
3994 #else
3995 err |= __put_user(set->sig[1], &sc->_unused[3]);
3996 #endif
3997 err |= __put_user(h2g(&frame->mctx), &sc->regs);
3998 err |= __put_user(sig, &sc->signal);
4000 /* Save user regs. */
4001 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4003 /* The kernel checks for the presence of a VDSO here. We don't
4004 emulate a vdso, so use a sigreturn system call. */
4005 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4007 /* Turn off all fp exceptions. */
4008 env->fpscr = 0;
4010 /* Create a stack frame for the caller of the handler. */
4011 newsp = frame_addr - SIGNAL_FRAMESIZE;
4012 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4014 if (err)
4015 goto sigsegv;
4017 /* Set up registers for signal handler. */
4018 env->gpr[1] = newsp;
4019 env->gpr[3] = signal;
4020 env->gpr[4] = (target_ulong) h2g(sc);
4021 env->nip = (target_ulong) ka->_sa_handler;
4022 /* Signal handlers are entered in big-endian mode. */
4023 env->msr &= ~MSR_LE;
4025 unlock_user_struct(frame, frame_addr, 1);
4026 return;
4028 sigsegv:
4029 unlock_user_struct(frame, frame_addr, 1);
4030 if (logfile)
4031 fprintf (logfile, "segfaulting from setup_frame\n");
4032 force_sig(TARGET_SIGSEGV);
4035 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4036 target_siginfo_t *info,
4037 target_sigset_t *set, CPUState *env)
4039 struct target_rt_sigframe *rt_sf;
4040 struct target_mcontext *frame;
4041 target_ulong rt_sf_addr, newsp = 0;
4042 int i, err = 0;
4043 int signal;
4045 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4046 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4047 goto sigsegv;
4049 signal = current_exec_domain_sig(sig);
4051 err |= copy_siginfo_to_user(&rt_sf->info, info);
4053 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4054 err |= __put_user(0, &rt_sf->uc.tuc_link);
4055 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4056 &rt_sf->uc.tuc_stack.ss_sp);
4057 err |= __put_user(sas_ss_flags(env->gpr[1]),
4058 &rt_sf->uc.tuc_stack.ss_flags);
4059 err |= __put_user(target_sigaltstack_used.ss_size,
4060 &rt_sf->uc.tuc_stack.ss_size);
4061 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4062 &rt_sf->uc.tuc_regs);
4063 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4064 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4067 frame = &rt_sf->uc.tuc_mcontext;
4068 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4070 /* The kernel checks for the presence of a VDSO here. We don't
4071 emulate a vdso, so use a sigreturn system call. */
4072 env->lr = (target_ulong) h2g(frame->tramp);
4074 /* Turn off all fp exceptions. */
4075 env->fpscr = 0;
4077 /* Create a stack frame for the caller of the handler. */
4078 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4079 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4081 if (err)
4082 goto sigsegv;
4084 /* Set up registers for signal handler. */
4085 env->gpr[1] = newsp;
4086 env->gpr[3] = (target_ulong) signal;
4087 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4088 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4089 env->gpr[6] = (target_ulong) h2g(rt_sf);
4090 env->nip = (target_ulong) ka->_sa_handler;
4091 /* Signal handlers are entered in big-endian mode. */
4092 env->msr &= ~MSR_LE;
4094 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4095 return;
4097 sigsegv:
4098 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4099 if (logfile)
4100 fprintf (logfile, "segfaulting from setup_rt_frame\n");
4101 force_sig(TARGET_SIGSEGV);
4105 long do_sigreturn(CPUState *env)
4107 struct target_sigcontext *sc = NULL;
4108 struct target_mcontext *sr = NULL;
4109 target_ulong sr_addr, sc_addr;
4110 sigset_t blocked;
4111 target_sigset_t set;
4113 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4114 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4115 goto sigsegv;
4117 #if defined(TARGET_PPC64)
4118 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4119 #else
4120 if(__get_user(set.sig[0], &sc->oldmask) ||
4121 __get_user(set.sig[1], &sc->_unused[3]))
4122 goto sigsegv;
4123 #endif
4124 target_to_host_sigset_internal(&blocked, &set);
4125 sigprocmask(SIG_SETMASK, &blocked, NULL);
4127 if (__get_user(sr_addr, &sc->regs))
4128 goto sigsegv;
4129 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4130 goto sigsegv;
4131 if (restore_user_regs(env, sr, 1))
4132 goto sigsegv;
4134 unlock_user_struct(sr, sr_addr, 1);
4135 unlock_user_struct(sc, sc_addr, 1);
4136 return -TARGET_QEMU_ESIGRETURN;
4138 sigsegv:
4139 unlock_user_struct(sr, sr_addr, 1);
4140 unlock_user_struct(sc, sc_addr, 1);
4141 if (logfile)
4142 fprintf (logfile, "segfaulting from do_sigreturn\n");
4143 force_sig(TARGET_SIGSEGV);
4144 return 0;
4147 /* See arch/powerpc/kernel/signal_32.c. */
4148 static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
4150 struct target_mcontext *mcp;
4151 target_ulong mcp_addr;
4152 sigset_t blocked;
4153 target_sigset_t set;
4155 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4156 sizeof (set)))
4157 return 1;
4159 #if defined(TARGET_PPC64)
4160 fprintf (stderr, "do_setcontext: not implemented\n");
4161 return 0;
4162 #else
4163 if (__get_user(mcp_addr, &ucp->tuc_regs))
4164 return 1;
4166 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4167 return 1;
4169 target_to_host_sigset_internal(&blocked, &set);
4170 sigprocmask(SIG_SETMASK, &blocked, NULL);
4171 if (restore_user_regs(env, mcp, sig))
4172 goto sigsegv;
4174 unlock_user_struct(mcp, mcp_addr, 1);
4175 return 0;
4177 sigsegv:
4178 unlock_user_struct(mcp, mcp_addr, 1);
4179 return 1;
4180 #endif
4183 long do_rt_sigreturn(CPUState *env)
4185 struct target_rt_sigframe *rt_sf = NULL;
4186 target_ulong rt_sf_addr;
4188 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4189 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4190 goto sigsegv;
4192 if (do_setcontext(&rt_sf->uc, env, 1))
4193 goto sigsegv;
4195 do_sigaltstack(rt_sf_addr
4196 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4197 0, env->gpr[1]);
4199 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4200 return -TARGET_QEMU_ESIGRETURN;
4202 sigsegv:
4203 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4204 if (logfile)
4205 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
4206 force_sig(TARGET_SIGSEGV);
4207 return 0;
4210 #elif defined(TARGET_M68K)
4212 struct target_sigcontext {
4213 abi_ulong sc_mask;
4214 abi_ulong sc_usp;
4215 abi_ulong sc_d0;
4216 abi_ulong sc_d1;
4217 abi_ulong sc_a0;
4218 abi_ulong sc_a1;
4219 unsigned short sc_sr;
4220 abi_ulong sc_pc;
4223 struct target_sigframe
4225 abi_ulong pretcode;
4226 int sig;
4227 int code;
4228 abi_ulong psc;
4229 char retcode[8];
4230 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4231 struct target_sigcontext sc;
4234 typedef int target_greg_t;
4235 #define TARGET_NGREG 18
4236 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4238 typedef struct target_fpregset {
4239 int f_fpcntl[3];
4240 int f_fpregs[8*3];
4241 } target_fpregset_t;
4243 struct target_mcontext {
4244 int version;
4245 target_gregset_t gregs;
4246 target_fpregset_t fpregs;
4249 #define TARGET_MCONTEXT_VERSION 2
4251 struct target_ucontext {
4252 abi_ulong tuc_flags;
4253 abi_ulong tuc_link;
4254 target_stack_t tuc_stack;
4255 struct target_mcontext tuc_mcontext;
4256 abi_long tuc_filler[80];
4257 target_sigset_t tuc_sigmask;
4260 struct target_rt_sigframe
4262 abi_ulong pretcode;
4263 int sig;
4264 abi_ulong pinfo;
4265 abi_ulong puc;
4266 char retcode[8];
4267 struct target_siginfo info;
4268 struct target_ucontext uc;
4271 static int
4272 setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4274 int err = 0;
4276 err |= __put_user(mask, &sc->sc_mask);
4277 err |= __put_user(env->aregs[7], &sc->sc_usp);
4278 err |= __put_user(env->dregs[0], &sc->sc_d0);
4279 err |= __put_user(env->dregs[1], &sc->sc_d1);
4280 err |= __put_user(env->aregs[0], &sc->sc_a0);
4281 err |= __put_user(env->aregs[1], &sc->sc_a1);
4282 err |= __put_user(env->sr, &sc->sc_sr);
4283 err |= __put_user(env->pc, &sc->sc_pc);
4285 return err;
4288 static int
4289 restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4291 int err = 0;
4292 int temp;
4294 err |= __get_user(env->aregs[7], &sc->sc_usp);
4295 err |= __get_user(env->dregs[1], &sc->sc_d1);
4296 err |= __get_user(env->aregs[0], &sc->sc_a0);
4297 err |= __get_user(env->aregs[1], &sc->sc_a1);
4298 err |= __get_user(env->pc, &sc->sc_pc);
4299 err |= __get_user(temp, &sc->sc_sr);
4300 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4302 *pd0 = tswapl(sc->sc_d0);
4304 return err;
4308 * Determine which stack to use..
4310 static inline abi_ulong
4311 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4313 unsigned long sp;
4315 sp = regs->aregs[7];
4317 /* This is the X/Open sanctioned signal stack switching. */
4318 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4319 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4322 return ((sp - frame_size) & -8UL);
4325 static void setup_frame(int sig, struct target_sigaction *ka,
4326 target_sigset_t *set, CPUState *env)
4328 struct target_sigframe *frame;
4329 abi_ulong frame_addr;
4330 abi_ulong retcode_addr;
4331 abi_ulong sc_addr;
4332 int err = 0;
4333 int i;
4335 frame_addr = get_sigframe(ka, env, sizeof *frame);
4336 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4337 goto give_sigsegv;
4339 err |= __put_user(sig, &frame->sig);
4341 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4342 err |= __put_user(sc_addr, &frame->psc);
4344 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4345 if (err)
4346 goto give_sigsegv;
4348 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4349 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4350 goto give_sigsegv;
4353 /* Set up to return from userspace. */
4355 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4356 err |= __put_user(retcode_addr, &frame->pretcode);
4358 /* moveq #,d0; trap #0 */
4360 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4361 (long *)(frame->retcode));
4363 if (err)
4364 goto give_sigsegv;
4366 /* Set up to return from userspace */
4368 env->aregs[7] = frame_addr;
4369 env->pc = ka->_sa_handler;
4371 unlock_user_struct(frame, frame_addr, 1);
4372 return;
4374 give_sigsegv:
4375 unlock_user_struct(frame, frame_addr, 1);
4376 force_sig(TARGET_SIGSEGV);
4379 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4380 CPUState *env)
4382 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4383 int err;
4385 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4386 err |= __put_user(env->dregs[0], &gregs[0]);
4387 err |= __put_user(env->dregs[1], &gregs[1]);
4388 err |= __put_user(env->dregs[2], &gregs[2]);
4389 err |= __put_user(env->dregs[3], &gregs[3]);
4390 err |= __put_user(env->dregs[4], &gregs[4]);
4391 err |= __put_user(env->dregs[5], &gregs[5]);
4392 err |= __put_user(env->dregs[6], &gregs[6]);
4393 err |= __put_user(env->dregs[7], &gregs[7]);
4394 err |= __put_user(env->aregs[0], &gregs[8]);
4395 err |= __put_user(env->aregs[1], &gregs[9]);
4396 err |= __put_user(env->aregs[2], &gregs[10]);
4397 err |= __put_user(env->aregs[3], &gregs[11]);
4398 err |= __put_user(env->aregs[4], &gregs[12]);
4399 err |= __put_user(env->aregs[5], &gregs[13]);
4400 err |= __put_user(env->aregs[6], &gregs[14]);
4401 err |= __put_user(env->aregs[7], &gregs[15]);
4402 err |= __put_user(env->pc, &gregs[16]);
4403 err |= __put_user(env->sr, &gregs[17]);
4405 return err;
4408 static inline int target_rt_restore_ucontext(CPUState *env,
4409 struct target_ucontext *uc,
4410 int *pd0)
4412 int temp;
4413 int err;
4414 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4416 err = __get_user(temp, &uc->tuc_mcontext.version);
4417 if (temp != TARGET_MCONTEXT_VERSION)
4418 goto badframe;
4420 /* restore passed registers */
4421 err |= __get_user(env->dregs[0], &gregs[0]);
4422 err |= __get_user(env->dregs[1], &gregs[1]);
4423 err |= __get_user(env->dregs[2], &gregs[2]);
4424 err |= __get_user(env->dregs[3], &gregs[3]);
4425 err |= __get_user(env->dregs[4], &gregs[4]);
4426 err |= __get_user(env->dregs[5], &gregs[5]);
4427 err |= __get_user(env->dregs[6], &gregs[6]);
4428 err |= __get_user(env->dregs[7], &gregs[7]);
4429 err |= __get_user(env->aregs[0], &gregs[8]);
4430 err |= __get_user(env->aregs[1], &gregs[9]);
4431 err |= __get_user(env->aregs[2], &gregs[10]);
4432 err |= __get_user(env->aregs[3], &gregs[11]);
4433 err |= __get_user(env->aregs[4], &gregs[12]);
4434 err |= __get_user(env->aregs[5], &gregs[13]);
4435 err |= __get_user(env->aregs[6], &gregs[14]);
4436 err |= __get_user(env->aregs[7], &gregs[15]);
4437 err |= __get_user(env->pc, &gregs[16]);
4438 err |= __get_user(temp, &gregs[17]);
4439 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4441 *pd0 = env->dregs[0];
4442 return err;
4444 badframe:
4445 return 1;
4448 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4449 target_siginfo_t *info,
4450 target_sigset_t *set, CPUState *env)
4452 struct target_rt_sigframe *frame;
4453 abi_ulong frame_addr;
4454 abi_ulong retcode_addr;
4455 abi_ulong info_addr;
4456 abi_ulong uc_addr;
4457 int err = 0;
4458 int i;
4460 frame_addr = get_sigframe(ka, env, sizeof *frame);
4461 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4462 goto give_sigsegv;
4464 err |= __put_user(sig, &frame->sig);
4466 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4467 err |= __put_user(info_addr, &frame->pinfo);
4469 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4470 err |= __put_user(uc_addr, &frame->puc);
4472 err |= copy_siginfo_to_user(&frame->info, info);
4474 /* Create the ucontext */
4476 err |= __put_user(0, &frame->uc.tuc_flags);
4477 err |= __put_user(0, &frame->uc.tuc_link);
4478 err |= __put_user(target_sigaltstack_used.ss_sp,
4479 &frame->uc.tuc_stack.ss_sp);
4480 err |= __put_user(sas_ss_flags(env->aregs[7]),
4481 &frame->uc.tuc_stack.ss_flags);
4482 err |= __put_user(target_sigaltstack_used.ss_size,
4483 &frame->uc.tuc_stack.ss_size);
4484 err |= target_rt_setup_ucontext(&frame->uc, env);
4486 if (err)
4487 goto give_sigsegv;
4489 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4490 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
4491 goto give_sigsegv;
4494 /* Set up to return from userspace. */
4496 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4497 err |= __put_user(retcode_addr, &frame->pretcode);
4499 /* moveq #,d0; notb d0; trap #0 */
4501 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4502 (long *)(frame->retcode + 0));
4503 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4505 if (err)
4506 goto give_sigsegv;
4508 /* Set up to return from userspace */
4510 env->aregs[7] = frame_addr;
4511 env->pc = ka->_sa_handler;
4513 unlock_user_struct(frame, frame_addr, 1);
4514 return;
4516 give_sigsegv:
4517 unlock_user_struct(frame, frame_addr, 1);
4518 force_sig(TARGET_SIGSEGV);
4521 long do_sigreturn(CPUState *env)
4523 struct target_sigframe *frame;
4524 abi_ulong frame_addr = env->aregs[7] - 4;
4525 target_sigset_t target_set;
4526 sigset_t set;
4527 int d0, i;
4529 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4530 goto badframe;
4532 /* set blocked signals */
4534 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4535 goto badframe;
4537 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4538 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4539 goto badframe;
4542 target_to_host_sigset_internal(&set, &target_set);
4543 sigprocmask(SIG_SETMASK, &set, NULL);
4545 /* restore registers */
4547 if (restore_sigcontext(env, &frame->sc, &d0))
4548 goto badframe;
4550 unlock_user_struct(frame, frame_addr, 0);
4551 return d0;
4553 badframe:
4554 unlock_user_struct(frame, frame_addr, 0);
4555 force_sig(TARGET_SIGSEGV);
4556 return 0;
4559 long do_rt_sigreturn(CPUState *env)
4561 struct target_rt_sigframe *frame;
4562 abi_ulong frame_addr = env->aregs[7] - 4;
4563 target_sigset_t target_set;
4564 sigset_t set;
4565 int d0;
4567 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4568 goto badframe;
4570 target_to_host_sigset_internal(&set, &target_set);
4571 sigprocmask(SIG_SETMASK, &set, NULL);
4573 /* restore registers */
4575 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4576 goto badframe;
4578 if (do_sigaltstack(frame_addr +
4579 offsetof(struct target_rt_sigframe, uc.tuc_stack),
4580 0, get_sp_from_cpustate(env)) == -EFAULT)
4581 goto badframe;
4583 unlock_user_struct(frame, frame_addr, 0);
4584 return d0;
4586 badframe:
4587 unlock_user_struct(frame, frame_addr, 0);
4588 force_sig(TARGET_SIGSEGV);
4589 return 0;
4592 #elif defined(TARGET_ALPHA)
4594 struct target_sigcontext {
4595 abi_long sc_onstack;
4596 abi_long sc_mask;
4597 abi_long sc_pc;
4598 abi_long sc_ps;
4599 abi_long sc_regs[32];
4600 abi_long sc_ownedfp;
4601 abi_long sc_fpregs[32];
4602 abi_ulong sc_fpcr;
4603 abi_ulong sc_fp_control;
4604 abi_ulong sc_reserved1;
4605 abi_ulong sc_reserved2;
4606 abi_ulong sc_ssize;
4607 abi_ulong sc_sbase;
4608 abi_ulong sc_traparg_a0;
4609 abi_ulong sc_traparg_a1;
4610 abi_ulong sc_traparg_a2;
4611 abi_ulong sc_fp_trap_pc;
4612 abi_ulong sc_fp_trigger_sum;
4613 abi_ulong sc_fp_trigger_inst;
4616 struct target_ucontext {
4617 abi_ulong tuc_flags;
4618 abi_ulong tuc_link;
4619 abi_ulong tuc_osf_sigmask;
4620 target_stack_t tuc_stack;
4621 struct target_sigcontext tuc_mcontext;
4622 target_sigset_t tuc_sigmask;
4625 struct target_sigframe {
4626 struct target_sigcontext sc;
4627 unsigned int retcode[3];
4630 struct target_rt_sigframe {
4631 target_siginfo_t info;
4632 struct target_ucontext uc;
4633 unsigned int retcode[3];
4636 #define INSN_MOV_R30_R16 0x47fe0410
4637 #define INSN_LDI_R0 0x201f0000
4638 #define INSN_CALLSYS 0x00000083
4640 static int setup_sigcontext(struct target_sigcontext *sc, CPUState *env,
4641 abi_ulong frame_addr, target_sigset_t *set)
4643 int i, err = 0;
4645 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
4646 err |= __put_user(set->sig[0], &sc->sc_mask);
4647 err |= __put_user(env->pc, &sc->sc_pc);
4648 err |= __put_user(8, &sc->sc_ps);
4650 for (i = 0; i < 31; ++i) {
4651 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
4653 err |= __put_user(0, &sc->sc_regs[31]);
4655 for (i = 0; i < 31; ++i) {
4656 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
4658 err |= __put_user(0, &sc->sc_fpregs[31]);
4659 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
4661 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
4662 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
4663 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
4665 return err;
4668 static int restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
4670 uint64_t fpcr;
4671 int i, err = 0;
4673 err |= __get_user(env->pc, &sc->sc_pc);
4675 for (i = 0; i < 31; ++i) {
4676 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
4678 for (i = 0; i < 31; ++i) {
4679 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
4682 err |= __get_user(fpcr, &sc->sc_fpcr);
4683 cpu_alpha_store_fpcr(env, fpcr);
4685 return err;
4688 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
4689 CPUState *env, unsigned long framesize)
4691 abi_ulong sp = env->ir[IR_SP];
4693 /* This is the X/Open sanctioned signal stack switching. */
4694 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
4695 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4697 return (sp - framesize) & -32;
4700 static void setup_frame(int sig, struct target_sigaction *ka,
4701 target_sigset_t *set, CPUState *env)
4703 abi_ulong frame_addr, r26;
4704 struct target_sigframe *frame;
4705 int err = 0;
4707 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4708 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4709 goto give_sigsegv;
4712 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
4714 if (ka->sa_restorer) {
4715 r26 = ka->sa_restorer;
4716 } else {
4717 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4718 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
4719 &frame->retcode[1]);
4720 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4721 /* imb() */
4722 r26 = frame_addr;
4725 unlock_user_struct(frame, frame_addr, 1);
4727 if (err) {
4728 give_sigsegv:
4729 if (sig == TARGET_SIGSEGV) {
4730 ka->_sa_handler = TARGET_SIG_DFL;
4732 force_sig(TARGET_SIGSEGV);
4735 env->ir[IR_RA] = r26;
4736 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4737 env->ir[IR_A0] = sig;
4738 env->ir[IR_A1] = 0;
4739 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
4740 env->ir[IR_SP] = frame_addr;
4743 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4744 target_siginfo_t *info,
4745 target_sigset_t *set, CPUState *env)
4747 abi_ulong frame_addr, r26;
4748 struct target_rt_sigframe *frame;
4749 int i, err = 0;
4751 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4752 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4753 goto give_sigsegv;
4756 err |= copy_siginfo_to_user(&frame->info, info);
4758 err |= __put_user(0, &frame->uc.tuc_flags);
4759 err |= __put_user(0, &frame->uc.tuc_link);
4760 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
4761 err |= __put_user(target_sigaltstack_used.ss_sp,
4762 &frame->uc.tuc_stack.ss_sp);
4763 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
4764 &frame->uc.tuc_stack.ss_flags);
4765 err |= __put_user(target_sigaltstack_used.ss_size,
4766 &frame->uc.tuc_stack.ss_size);
4767 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
4768 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
4769 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
4772 if (ka->sa_restorer) {
4773 r26 = ka->sa_restorer;
4774 } else {
4775 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4776 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
4777 &frame->retcode[1]);
4778 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4779 /* imb(); */
4780 r26 = frame_addr;
4783 if (err) {
4784 give_sigsegv:
4785 if (sig == TARGET_SIGSEGV) {
4786 ka->_sa_handler = TARGET_SIG_DFL;
4788 force_sig(TARGET_SIGSEGV);
4791 env->ir[IR_RA] = r26;
4792 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4793 env->ir[IR_A0] = sig;
4794 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
4795 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
4796 env->ir[IR_SP] = frame_addr;
4799 long do_sigreturn(CPUState *env)
4801 struct target_sigcontext *sc;
4802 abi_ulong sc_addr = env->ir[IR_A0];
4803 target_sigset_t target_set;
4804 sigset_t set;
4806 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
4807 goto badframe;
4810 target_sigemptyset(&target_set);
4811 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
4812 goto badframe;
4815 target_to_host_sigset_internal(&set, &target_set);
4816 sigprocmask(SIG_SETMASK, &set, NULL);
4818 if (restore_sigcontext(env, sc)) {
4819 goto badframe;
4821 unlock_user_struct(sc, sc_addr, 0);
4822 return env->ir[IR_V0];
4824 badframe:
4825 unlock_user_struct(sc, sc_addr, 0);
4826 force_sig(TARGET_SIGSEGV);
4829 long do_rt_sigreturn(CPUState *env)
4831 abi_ulong frame_addr = env->ir[IR_A0];
4832 struct target_rt_sigframe *frame;
4833 sigset_t set;
4835 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4836 goto badframe;
4838 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4839 sigprocmask(SIG_SETMASK, &set, NULL);
4841 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
4842 goto badframe;
4844 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
4845 uc.tuc_stack),
4846 0, env->ir[IR_SP]) == -EFAULT) {
4847 goto badframe;
4850 unlock_user_struct(frame, frame_addr, 0);
4851 return env->ir[IR_V0];
4854 badframe:
4855 unlock_user_struct(frame, frame_addr, 0);
4856 force_sig(TARGET_SIGSEGV);
4859 #else
4861 static void setup_frame(int sig, struct target_sigaction *ka,
4862 target_sigset_t *set, CPUState *env)
4864 fprintf(stderr, "setup_frame: not implemented\n");
4867 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4868 target_siginfo_t *info,
4869 target_sigset_t *set, CPUState *env)
4871 fprintf(stderr, "setup_rt_frame: not implemented\n");
4874 long do_sigreturn(CPUState *env)
4876 fprintf(stderr, "do_sigreturn: not implemented\n");
4877 return -TARGET_ENOSYS;
4880 long do_rt_sigreturn(CPUState *env)
4882 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
4883 return -TARGET_ENOSYS;
4886 #endif
4888 void process_pending_signals(CPUState *cpu_env)
4890 int sig;
4891 abi_ulong handler;
4892 sigset_t set, old_set;
4893 target_sigset_t target_old_set;
4894 struct emulated_sigtable *k;
4895 struct target_sigaction *sa;
4896 struct sigqueue *q;
4897 TaskState *ts = cpu_env->opaque;
4899 if (!ts->signal_pending)
4900 return;
4902 /* FIXME: This is not threadsafe. */
4903 k = ts->sigtab;
4904 for(sig = 1; sig <= TARGET_NSIG; sig++) {
4905 if (k->pending)
4906 goto handle_signal;
4907 k++;
4909 /* if no signal is pending, just return */
4910 ts->signal_pending = 0;
4911 return;
4913 handle_signal:
4914 #ifdef DEBUG_SIGNAL
4915 fprintf(stderr, "qemu: process signal %d\n", sig);
4916 #endif
4917 /* dequeue signal */
4918 q = k->first;
4919 k->first = q->next;
4920 if (!k->first)
4921 k->pending = 0;
4923 sig = gdb_handlesig (cpu_env, sig);
4924 if (!sig) {
4925 sa = NULL;
4926 handler = TARGET_SIG_IGN;
4927 } else {
4928 sa = &sigact_table[sig - 1];
4929 handler = sa->_sa_handler;
4932 if (handler == TARGET_SIG_DFL) {
4933 /* default handler : ignore some signal. The other are job control or fatal */
4934 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
4935 kill(getpid(),SIGSTOP);
4936 } else if (sig != TARGET_SIGCHLD &&
4937 sig != TARGET_SIGURG &&
4938 sig != TARGET_SIGWINCH &&
4939 sig != TARGET_SIGCONT) {
4940 force_sig(sig);
4942 } else if (handler == TARGET_SIG_IGN) {
4943 /* ignore sig */
4944 } else if (handler == TARGET_SIG_ERR) {
4945 force_sig(sig);
4946 } else {
4947 /* compute the blocked signals during the handler execution */
4948 target_to_host_sigset(&set, &sa->sa_mask);
4949 /* SA_NODEFER indicates that the current signal should not be
4950 blocked during the handler */
4951 if (!(sa->sa_flags & TARGET_SA_NODEFER))
4952 sigaddset(&set, target_to_host_signal(sig));
4954 /* block signals in the handler using Linux */
4955 sigprocmask(SIG_BLOCK, &set, &old_set);
4956 /* save the previous blocked signal state to restore it at the
4957 end of the signal execution (see do_sigreturn) */
4958 host_to_target_sigset_internal(&target_old_set, &old_set);
4960 /* if the CPU is in VM86 mode, we restore the 32 bit values */
4961 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
4963 CPUX86State *env = cpu_env;
4964 if (env->eflags & VM_MASK)
4965 save_v86_state(env);
4967 #endif
4968 /* prepare the stack frame of the virtual CPU */
4969 if (sa->sa_flags & TARGET_SA_SIGINFO)
4970 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
4971 else
4972 setup_frame(sig, sa, &target_old_set, cpu_env);
4973 if (sa->sa_flags & TARGET_SA_RESETHAND)
4974 sa->_sa_handler = TARGET_SIG_DFL;
4976 if (q != &k->info)
4977 free_sigqueue(cpu_env, q);