hw/sd.c: Set ILLEGAL_COMMAND for ACMDs in invalid state
[qemu/ar7.git] / linux-user / signal.c
blobded12caa18fc556450d827c8f17458e565ff01d3
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <sys/ucontext.h>
27 #include <sys/resource.h>
29 #include "qemu.h"
30 #include "qemu-common.h"
31 #include "target_signal.h"
33 //#define DEBUG_SIGNAL
35 static struct target_sigaltstack target_sigaltstack_used = {
36 .ss_sp = 0,
37 .ss_size = 0,
38 .ss_flags = TARGET_SS_DISABLE,
41 static struct target_sigaction sigact_table[TARGET_NSIG];
43 static void host_signal_handler(int host_signum, siginfo_t *info,
44 void *puc);
46 static uint8_t host_to_target_signal_table[_NSIG] = {
47 [SIGHUP] = TARGET_SIGHUP,
48 [SIGINT] = TARGET_SIGINT,
49 [SIGQUIT] = TARGET_SIGQUIT,
50 [SIGILL] = TARGET_SIGILL,
51 [SIGTRAP] = TARGET_SIGTRAP,
52 [SIGABRT] = TARGET_SIGABRT,
53 /* [SIGIOT] = TARGET_SIGIOT,*/
54 [SIGBUS] = TARGET_SIGBUS,
55 [SIGFPE] = TARGET_SIGFPE,
56 [SIGKILL] = TARGET_SIGKILL,
57 [SIGUSR1] = TARGET_SIGUSR1,
58 [SIGSEGV] = TARGET_SIGSEGV,
59 [SIGUSR2] = TARGET_SIGUSR2,
60 [SIGPIPE] = TARGET_SIGPIPE,
61 [SIGALRM] = TARGET_SIGALRM,
62 [SIGTERM] = TARGET_SIGTERM,
63 #ifdef SIGSTKFLT
64 [SIGSTKFLT] = TARGET_SIGSTKFLT,
65 #endif
66 [SIGCHLD] = TARGET_SIGCHLD,
67 [SIGCONT] = TARGET_SIGCONT,
68 [SIGSTOP] = TARGET_SIGSTOP,
69 [SIGTSTP] = TARGET_SIGTSTP,
70 [SIGTTIN] = TARGET_SIGTTIN,
71 [SIGTTOU] = TARGET_SIGTTOU,
72 [SIGURG] = TARGET_SIGURG,
73 [SIGXCPU] = TARGET_SIGXCPU,
74 [SIGXFSZ] = TARGET_SIGXFSZ,
75 [SIGVTALRM] = TARGET_SIGVTALRM,
76 [SIGPROF] = TARGET_SIGPROF,
77 [SIGWINCH] = TARGET_SIGWINCH,
78 [SIGIO] = TARGET_SIGIO,
79 [SIGPWR] = TARGET_SIGPWR,
80 [SIGSYS] = TARGET_SIGSYS,
81 /* next signals stay the same */
82 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
83 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
84 To fix this properly we need to do manual signal delivery multiplexed
85 over a single host signal. */
86 [__SIGRTMIN] = __SIGRTMAX,
87 [__SIGRTMAX] = __SIGRTMIN,
89 static uint8_t target_to_host_signal_table[_NSIG];
91 static inline int on_sig_stack(unsigned long sp)
93 return (sp - target_sigaltstack_used.ss_sp
94 < target_sigaltstack_used.ss_size);
97 static inline int sas_ss_flags(unsigned long sp)
99 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
100 : on_sig_stack(sp) ? SS_ONSTACK : 0);
103 int host_to_target_signal(int sig)
105 if (sig >= _NSIG)
106 return sig;
107 return host_to_target_signal_table[sig];
110 int target_to_host_signal(int sig)
112 if (sig >= _NSIG)
113 return sig;
114 return target_to_host_signal_table[sig];
117 static inline void target_sigemptyset(target_sigset_t *set)
119 memset(set, 0, sizeof(*set));
122 static inline void target_sigaddset(target_sigset_t *set, int signum)
124 signum--;
125 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126 set->sig[signum / TARGET_NSIG_BPW] |= mask;
129 static inline int target_sigismember(const target_sigset_t *set, int signum)
131 signum--;
132 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
133 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
136 static void host_to_target_sigset_internal(target_sigset_t *d,
137 const sigset_t *s)
139 int i;
140 target_sigemptyset(d);
141 for (i = 1; i <= TARGET_NSIG; i++) {
142 if (sigismember(s, i)) {
143 target_sigaddset(d, host_to_target_signal(i));
148 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
150 target_sigset_t d1;
151 int i;
153 host_to_target_sigset_internal(&d1, s);
154 for(i = 0;i < TARGET_NSIG_WORDS; i++)
155 d->sig[i] = tswapal(d1.sig[i]);
158 static void target_to_host_sigset_internal(sigset_t *d,
159 const target_sigset_t *s)
161 int i;
162 sigemptyset(d);
163 for (i = 1; i <= TARGET_NSIG; i++) {
164 if (target_sigismember(s, i)) {
165 sigaddset(d, target_to_host_signal(i));
170 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
172 target_sigset_t s1;
173 int i;
175 for(i = 0;i < TARGET_NSIG_WORDS; i++)
176 s1.sig[i] = tswapal(s->sig[i]);
177 target_to_host_sigset_internal(d, &s1);
180 void host_to_target_old_sigset(abi_ulong *old_sigset,
181 const sigset_t *sigset)
183 target_sigset_t d;
184 host_to_target_sigset(&d, sigset);
185 *old_sigset = d.sig[0];
188 void target_to_host_old_sigset(sigset_t *sigset,
189 const abi_ulong *old_sigset)
191 target_sigset_t d;
192 int i;
194 d.sig[0] = *old_sigset;
195 for(i = 1;i < TARGET_NSIG_WORDS; i++)
196 d.sig[i] = 0;
197 target_to_host_sigset(sigset, &d);
200 /* siginfo conversion */
202 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
203 const siginfo_t *info)
205 int sig;
206 sig = host_to_target_signal(info->si_signo);
207 tinfo->si_signo = sig;
208 tinfo->si_errno = 0;
209 tinfo->si_code = info->si_code;
210 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
211 sig == SIGBUS || sig == SIGTRAP) {
212 /* should never come here, but who knows. The information for
213 the target is irrelevant */
214 tinfo->_sifields._sigfault._addr = 0;
215 } else if (sig == SIGIO) {
216 tinfo->_sifields._sigpoll._fd = info->si_fd;
217 } else if (sig >= TARGET_SIGRTMIN) {
218 tinfo->_sifields._rt._pid = info->si_pid;
219 tinfo->_sifields._rt._uid = info->si_uid;
220 /* XXX: potential problem if 64 bit */
221 tinfo->_sifields._rt._sigval.sival_ptr =
222 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
226 static void tswap_siginfo(target_siginfo_t *tinfo,
227 const target_siginfo_t *info)
229 int sig;
230 sig = info->si_signo;
231 tinfo->si_signo = tswap32(sig);
232 tinfo->si_errno = tswap32(info->si_errno);
233 tinfo->si_code = tswap32(info->si_code);
234 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
235 sig == SIGBUS || sig == SIGTRAP) {
236 tinfo->_sifields._sigfault._addr =
237 tswapal(info->_sifields._sigfault._addr);
238 } else if (sig == SIGIO) {
239 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
240 } else if (sig >= TARGET_SIGRTMIN) {
241 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
242 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
243 tinfo->_sifields._rt._sigval.sival_ptr =
244 tswapal(info->_sifields._rt._sigval.sival_ptr);
249 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
251 host_to_target_siginfo_noswap(tinfo, info);
252 tswap_siginfo(tinfo, tinfo);
255 /* XXX: we support only POSIX RT signals are used. */
256 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
257 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
259 info->si_signo = tswap32(tinfo->si_signo);
260 info->si_errno = tswap32(tinfo->si_errno);
261 info->si_code = tswap32(tinfo->si_code);
262 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
263 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
264 info->si_value.sival_ptr =
265 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
268 static int fatal_signal (int sig)
270 switch (sig) {
271 case TARGET_SIGCHLD:
272 case TARGET_SIGURG:
273 case TARGET_SIGWINCH:
274 /* Ignored by default. */
275 return 0;
276 case TARGET_SIGCONT:
277 case TARGET_SIGSTOP:
278 case TARGET_SIGTSTP:
279 case TARGET_SIGTTIN:
280 case TARGET_SIGTTOU:
281 /* Job control signals. */
282 return 0;
283 default:
284 return 1;
288 /* returns 1 if given signal should dump core if not handled */
289 static int core_dump_signal(int sig)
291 switch (sig) {
292 case TARGET_SIGABRT:
293 case TARGET_SIGFPE:
294 case TARGET_SIGILL:
295 case TARGET_SIGQUIT:
296 case TARGET_SIGSEGV:
297 case TARGET_SIGTRAP:
298 case TARGET_SIGBUS:
299 return (1);
300 default:
301 return (0);
305 void signal_init(void)
307 struct sigaction act;
308 struct sigaction oact;
309 int i, j;
310 int host_sig;
312 /* generate signal conversion tables */
313 for(i = 1; i < _NSIG; i++) {
314 if (host_to_target_signal_table[i] == 0)
315 host_to_target_signal_table[i] = i;
317 for(i = 1; i < _NSIG; i++) {
318 j = host_to_target_signal_table[i];
319 target_to_host_signal_table[j] = i;
322 /* set all host signal handlers. ALL signals are blocked during
323 the handlers to serialize them. */
324 memset(sigact_table, 0, sizeof(sigact_table));
326 sigfillset(&act.sa_mask);
327 act.sa_flags = SA_SIGINFO;
328 act.sa_sigaction = host_signal_handler;
329 for(i = 1; i <= TARGET_NSIG; i++) {
330 host_sig = target_to_host_signal(i);
331 sigaction(host_sig, NULL, &oact);
332 if (oact.sa_sigaction == (void *)SIG_IGN) {
333 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
334 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
335 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
337 /* If there's already a handler installed then something has
338 gone horribly wrong, so don't even try to handle that case. */
339 /* Install some handlers for our own use. We need at least
340 SIGSEGV and SIGBUS, to detect exceptions. We can not just
341 trap all signals because it affects syscall interrupt
342 behavior. But do trap all default-fatal signals. */
343 if (fatal_signal (i))
344 sigaction(host_sig, &act, NULL);
348 /* signal queue handling */
350 static inline struct sigqueue *alloc_sigqueue(CPUState *env)
352 TaskState *ts = env->opaque;
353 struct sigqueue *q = ts->first_free;
354 if (!q)
355 return NULL;
356 ts->first_free = q->next;
357 return q;
360 static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
362 TaskState *ts = env->opaque;
363 q->next = ts->first_free;
364 ts->first_free = q;
367 /* abort execution with signal */
368 static void QEMU_NORETURN force_sig(int target_sig)
370 TaskState *ts = (TaskState *)thread_env->opaque;
371 int host_sig, core_dumped = 0;
372 struct sigaction act;
373 host_sig = target_to_host_signal(target_sig);
374 gdb_signalled(thread_env, target_sig);
376 /* dump core if supported by target binary format */
377 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
378 stop_all_tasks();
379 core_dumped =
380 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
382 if (core_dumped) {
383 /* we already dumped the core of target process, we don't want
384 * a coredump of qemu itself */
385 struct rlimit nodump;
386 getrlimit(RLIMIT_CORE, &nodump);
387 nodump.rlim_cur=0;
388 setrlimit(RLIMIT_CORE, &nodump);
389 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
390 target_sig, strsignal(host_sig), "core dumped" );
393 /* The proper exit code for dying from an uncaught signal is
394 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
395 * a negative value. To get the proper exit code we need to
396 * actually die from an uncaught signal. Here the default signal
397 * handler is installed, we send ourself a signal and we wait for
398 * it to arrive. */
399 sigfillset(&act.sa_mask);
400 act.sa_handler = SIG_DFL;
401 sigaction(host_sig, &act, NULL);
403 /* For some reason raise(host_sig) doesn't send the signal when
404 * statically linked on x86-64. */
405 kill(getpid(), host_sig);
407 /* Make sure the signal isn't masked (just reuse the mask inside
408 of act) */
409 sigdelset(&act.sa_mask, host_sig);
410 sigsuspend(&act.sa_mask);
412 /* unreachable */
413 abort();
416 /* queue a signal so that it will be send to the virtual CPU as soon
417 as possible */
418 int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
420 TaskState *ts = env->opaque;
421 struct emulated_sigtable *k;
422 struct sigqueue *q, **pq;
423 abi_ulong handler;
424 int queue;
426 #if defined(DEBUG_SIGNAL)
427 fprintf(stderr, "queue_signal: sig=%d\n",
428 sig);
429 #endif
430 k = &ts->sigtab[sig - 1];
431 queue = gdb_queuesig ();
432 handler = sigact_table[sig - 1]._sa_handler;
433 if (!queue && handler == TARGET_SIG_DFL) {
434 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
435 kill(getpid(),SIGSTOP);
436 return 0;
437 } else
438 /* default handler : ignore some signal. The other are fatal */
439 if (sig != TARGET_SIGCHLD &&
440 sig != TARGET_SIGURG &&
441 sig != TARGET_SIGWINCH &&
442 sig != TARGET_SIGCONT) {
443 force_sig(sig);
444 } else {
445 return 0; /* indicate ignored */
447 } else if (!queue && handler == TARGET_SIG_IGN) {
448 /* ignore signal */
449 return 0;
450 } else if (!queue && handler == TARGET_SIG_ERR) {
451 force_sig(sig);
452 } else {
453 pq = &k->first;
454 if (sig < TARGET_SIGRTMIN) {
455 /* if non real time signal, we queue exactly one signal */
456 if (!k->pending)
457 q = &k->info;
458 else
459 return 0;
460 } else {
461 if (!k->pending) {
462 /* first signal */
463 q = &k->info;
464 } else {
465 q = alloc_sigqueue(env);
466 if (!q)
467 return -EAGAIN;
468 while (*pq != NULL)
469 pq = &(*pq)->next;
472 *pq = q;
473 q->info = *info;
474 q->next = NULL;
475 k->pending = 1;
476 /* signal that a new signal is pending */
477 ts->signal_pending = 1;
478 return 1; /* indicates that the signal was queued */
482 static void host_signal_handler(int host_signum, siginfo_t *info,
483 void *puc)
485 int sig;
486 target_siginfo_t tinfo;
488 /* the CPU emulator uses some host signals to detect exceptions,
489 we forward to it some signals */
490 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
491 && info->si_code > 0) {
492 if (cpu_signal_handler(host_signum, info, puc))
493 return;
496 /* get target signal number */
497 sig = host_to_target_signal(host_signum);
498 if (sig < 1 || sig > TARGET_NSIG)
499 return;
500 #if defined(DEBUG_SIGNAL)
501 fprintf(stderr, "qemu: got signal %d\n", sig);
502 #endif
503 host_to_target_siginfo_noswap(&tinfo, info);
504 if (queue_signal(thread_env, sig, &tinfo) == 1) {
505 /* interrupt the virtual CPU as soon as possible */
506 cpu_exit(thread_env);
510 /* do_sigaltstack() returns target values and errnos. */
511 /* compare linux/kernel/signal.c:do_sigaltstack() */
512 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
514 int ret;
515 struct target_sigaltstack oss;
517 /* XXX: test errors */
518 if(uoss_addr)
520 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
521 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
522 __put_user(sas_ss_flags(sp), &oss.ss_flags);
525 if(uss_addr)
527 struct target_sigaltstack *uss;
528 struct target_sigaltstack ss;
530 ret = -TARGET_EFAULT;
531 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
532 || __get_user(ss.ss_sp, &uss->ss_sp)
533 || __get_user(ss.ss_size, &uss->ss_size)
534 || __get_user(ss.ss_flags, &uss->ss_flags))
535 goto out;
536 unlock_user_struct(uss, uss_addr, 0);
538 ret = -TARGET_EPERM;
539 if (on_sig_stack(sp))
540 goto out;
542 ret = -TARGET_EINVAL;
543 if (ss.ss_flags != TARGET_SS_DISABLE
544 && ss.ss_flags != TARGET_SS_ONSTACK
545 && ss.ss_flags != 0)
546 goto out;
548 if (ss.ss_flags == TARGET_SS_DISABLE) {
549 ss.ss_size = 0;
550 ss.ss_sp = 0;
551 } else {
552 ret = -TARGET_ENOMEM;
553 if (ss.ss_size < MINSIGSTKSZ)
554 goto out;
557 target_sigaltstack_used.ss_sp = ss.ss_sp;
558 target_sigaltstack_used.ss_size = ss.ss_size;
561 if (uoss_addr) {
562 ret = -TARGET_EFAULT;
563 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
564 goto out;
567 ret = 0;
568 out:
569 return ret;
572 /* do_sigaction() return host values and errnos */
573 int do_sigaction(int sig, const struct target_sigaction *act,
574 struct target_sigaction *oact)
576 struct target_sigaction *k;
577 struct sigaction act1;
578 int host_sig;
579 int ret = 0;
581 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
582 return -EINVAL;
583 k = &sigact_table[sig - 1];
584 #if defined(DEBUG_SIGNAL)
585 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
586 sig, act, oact);
587 #endif
588 if (oact) {
589 oact->_sa_handler = tswapal(k->_sa_handler);
590 oact->sa_flags = tswapal(k->sa_flags);
591 #if !defined(TARGET_MIPS)
592 oact->sa_restorer = tswapal(k->sa_restorer);
593 #endif
594 oact->sa_mask = k->sa_mask;
596 if (act) {
597 /* FIXME: This is not threadsafe. */
598 k->_sa_handler = tswapal(act->_sa_handler);
599 k->sa_flags = tswapal(act->sa_flags);
600 #if !defined(TARGET_MIPS)
601 k->sa_restorer = tswapal(act->sa_restorer);
602 #endif
603 k->sa_mask = act->sa_mask;
605 /* we update the host linux signal state */
606 host_sig = target_to_host_signal(sig);
607 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
608 sigfillset(&act1.sa_mask);
609 act1.sa_flags = SA_SIGINFO;
610 if (k->sa_flags & TARGET_SA_RESTART)
611 act1.sa_flags |= SA_RESTART;
612 /* NOTE: it is important to update the host kernel signal
613 ignore state to avoid getting unexpected interrupted
614 syscalls */
615 if (k->_sa_handler == TARGET_SIG_IGN) {
616 act1.sa_sigaction = (void *)SIG_IGN;
617 } else if (k->_sa_handler == TARGET_SIG_DFL) {
618 if (fatal_signal (sig))
619 act1.sa_sigaction = host_signal_handler;
620 else
621 act1.sa_sigaction = (void *)SIG_DFL;
622 } else {
623 act1.sa_sigaction = host_signal_handler;
625 ret = sigaction(host_sig, &act1, NULL);
628 return ret;
631 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
632 const target_siginfo_t *info)
634 tswap_siginfo(tinfo, info);
635 return 0;
638 static inline int current_exec_domain_sig(int sig)
640 return /* current->exec_domain && current->exec_domain->signal_invmap
641 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
644 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
646 /* from the Linux kernel */
648 struct target_fpreg {
649 uint16_t significand[4];
650 uint16_t exponent;
653 struct target_fpxreg {
654 uint16_t significand[4];
655 uint16_t exponent;
656 uint16_t padding[3];
659 struct target_xmmreg {
660 abi_ulong element[4];
663 struct target_fpstate {
664 /* Regular FPU environment */
665 abi_ulong cw;
666 abi_ulong sw;
667 abi_ulong tag;
668 abi_ulong ipoff;
669 abi_ulong cssel;
670 abi_ulong dataoff;
671 abi_ulong datasel;
672 struct target_fpreg _st[8];
673 uint16_t status;
674 uint16_t magic; /* 0xffff = regular FPU data only */
676 /* FXSR FPU environment */
677 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
678 abi_ulong mxcsr;
679 abi_ulong reserved;
680 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
681 struct target_xmmreg _xmm[8];
682 abi_ulong padding[56];
685 #define X86_FXSR_MAGIC 0x0000
687 struct target_sigcontext {
688 uint16_t gs, __gsh;
689 uint16_t fs, __fsh;
690 uint16_t es, __esh;
691 uint16_t ds, __dsh;
692 abi_ulong edi;
693 abi_ulong esi;
694 abi_ulong ebp;
695 abi_ulong esp;
696 abi_ulong ebx;
697 abi_ulong edx;
698 abi_ulong ecx;
699 abi_ulong eax;
700 abi_ulong trapno;
701 abi_ulong err;
702 abi_ulong eip;
703 uint16_t cs, __csh;
704 abi_ulong eflags;
705 abi_ulong esp_at_signal;
706 uint16_t ss, __ssh;
707 abi_ulong fpstate; /* pointer */
708 abi_ulong oldmask;
709 abi_ulong cr2;
712 struct target_ucontext {
713 abi_ulong tuc_flags;
714 abi_ulong tuc_link;
715 target_stack_t tuc_stack;
716 struct target_sigcontext tuc_mcontext;
717 target_sigset_t tuc_sigmask; /* mask last for extensibility */
720 struct sigframe
722 abi_ulong pretcode;
723 int sig;
724 struct target_sigcontext sc;
725 struct target_fpstate fpstate;
726 abi_ulong extramask[TARGET_NSIG_WORDS-1];
727 char retcode[8];
730 struct rt_sigframe
732 abi_ulong pretcode;
733 int sig;
734 abi_ulong pinfo;
735 abi_ulong puc;
736 struct target_siginfo info;
737 struct target_ucontext uc;
738 struct target_fpstate fpstate;
739 char retcode[8];
743 * Set up a signal frame.
746 /* XXX: save x87 state */
747 static int
748 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
749 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
751 int err = 0;
752 uint16_t magic;
754 /* already locked in setup_frame() */
755 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
756 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
757 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
758 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
759 err |= __put_user(env->regs[R_EDI], &sc->edi);
760 err |= __put_user(env->regs[R_ESI], &sc->esi);
761 err |= __put_user(env->regs[R_EBP], &sc->ebp);
762 err |= __put_user(env->regs[R_ESP], &sc->esp);
763 err |= __put_user(env->regs[R_EBX], &sc->ebx);
764 err |= __put_user(env->regs[R_EDX], &sc->edx);
765 err |= __put_user(env->regs[R_ECX], &sc->ecx);
766 err |= __put_user(env->regs[R_EAX], &sc->eax);
767 err |= __put_user(env->exception_index, &sc->trapno);
768 err |= __put_user(env->error_code, &sc->err);
769 err |= __put_user(env->eip, &sc->eip);
770 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
771 err |= __put_user(env->eflags, &sc->eflags);
772 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
773 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
775 cpu_x86_fsave(env, fpstate_addr, 1);
776 fpstate->status = fpstate->sw;
777 magic = 0xffff;
778 err |= __put_user(magic, &fpstate->magic);
779 err |= __put_user(fpstate_addr, &sc->fpstate);
781 /* non-iBCS2 extensions.. */
782 err |= __put_user(mask, &sc->oldmask);
783 err |= __put_user(env->cr[2], &sc->cr2);
784 return err;
788 * Determine which stack to use..
791 static inline abi_ulong
792 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
794 unsigned long esp;
796 /* Default to using normal stack */
797 esp = env->regs[R_ESP];
798 /* This is the X/Open sanctioned signal stack switching. */
799 if (ka->sa_flags & TARGET_SA_ONSTACK) {
800 if (sas_ss_flags(esp) == 0)
801 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
804 /* This is the legacy signal stack switching. */
805 else
806 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
807 !(ka->sa_flags & TARGET_SA_RESTORER) &&
808 ka->sa_restorer) {
809 esp = (unsigned long) ka->sa_restorer;
811 return (esp - frame_size) & -8ul;
814 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
815 static void setup_frame(int sig, struct target_sigaction *ka,
816 target_sigset_t *set, CPUX86State *env)
818 abi_ulong frame_addr;
819 struct sigframe *frame;
820 int i, err = 0;
822 frame_addr = get_sigframe(ka, env, sizeof(*frame));
824 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
825 goto give_sigsegv;
827 err |= __put_user(current_exec_domain_sig(sig),
828 &frame->sig);
829 if (err)
830 goto give_sigsegv;
832 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
833 frame_addr + offsetof(struct sigframe, fpstate));
834 if (err)
835 goto give_sigsegv;
837 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
838 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
839 goto give_sigsegv;
842 /* Set up to return from userspace. If provided, use a stub
843 already in userspace. */
844 if (ka->sa_flags & TARGET_SA_RESTORER) {
845 err |= __put_user(ka->sa_restorer, &frame->pretcode);
846 } else {
847 uint16_t val16;
848 abi_ulong retcode_addr;
849 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
850 err |= __put_user(retcode_addr, &frame->pretcode);
851 /* This is popl %eax ; movl $,%eax ; int $0x80 */
852 val16 = 0xb858;
853 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
854 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
855 val16 = 0x80cd;
856 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
859 if (err)
860 goto give_sigsegv;
862 /* Set up registers for signal handler */
863 env->regs[R_ESP] = frame_addr;
864 env->eip = ka->_sa_handler;
866 cpu_x86_load_seg(env, R_DS, __USER_DS);
867 cpu_x86_load_seg(env, R_ES, __USER_DS);
868 cpu_x86_load_seg(env, R_SS, __USER_DS);
869 cpu_x86_load_seg(env, R_CS, __USER_CS);
870 env->eflags &= ~TF_MASK;
872 unlock_user_struct(frame, frame_addr, 1);
874 return;
876 give_sigsegv:
877 unlock_user_struct(frame, frame_addr, 1);
878 if (sig == TARGET_SIGSEGV)
879 ka->_sa_handler = TARGET_SIG_DFL;
880 force_sig(TARGET_SIGSEGV /* , current */);
883 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
884 static void setup_rt_frame(int sig, struct target_sigaction *ka,
885 target_siginfo_t *info,
886 target_sigset_t *set, CPUX86State *env)
888 abi_ulong frame_addr, addr;
889 struct rt_sigframe *frame;
890 int i, err = 0;
892 frame_addr = get_sigframe(ka, env, sizeof(*frame));
894 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
895 goto give_sigsegv;
897 err |= __put_user(current_exec_domain_sig(sig),
898 &frame->sig);
899 addr = frame_addr + offsetof(struct rt_sigframe, info);
900 err |= __put_user(addr, &frame->pinfo);
901 addr = frame_addr + offsetof(struct rt_sigframe, uc);
902 err |= __put_user(addr, &frame->puc);
903 err |= copy_siginfo_to_user(&frame->info, info);
904 if (err)
905 goto give_sigsegv;
907 /* Create the ucontext. */
908 err |= __put_user(0, &frame->uc.tuc_flags);
909 err |= __put_user(0, &frame->uc.tuc_link);
910 err |= __put_user(target_sigaltstack_used.ss_sp,
911 &frame->uc.tuc_stack.ss_sp);
912 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
913 &frame->uc.tuc_stack.ss_flags);
914 err |= __put_user(target_sigaltstack_used.ss_size,
915 &frame->uc.tuc_stack.ss_size);
916 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
917 env, set->sig[0],
918 frame_addr + offsetof(struct rt_sigframe, fpstate));
919 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
920 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
921 goto give_sigsegv;
924 /* Set up to return from userspace. If provided, use a stub
925 already in userspace. */
926 if (ka->sa_flags & TARGET_SA_RESTORER) {
927 err |= __put_user(ka->sa_restorer, &frame->pretcode);
928 } else {
929 uint16_t val16;
930 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
931 err |= __put_user(addr, &frame->pretcode);
932 /* This is movl $,%eax ; int $0x80 */
933 err |= __put_user(0xb8, (char *)(frame->retcode+0));
934 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
935 val16 = 0x80cd;
936 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
939 if (err)
940 goto give_sigsegv;
942 /* Set up registers for signal handler */
943 env->regs[R_ESP] = frame_addr;
944 env->eip = ka->_sa_handler;
946 cpu_x86_load_seg(env, R_DS, __USER_DS);
947 cpu_x86_load_seg(env, R_ES, __USER_DS);
948 cpu_x86_load_seg(env, R_SS, __USER_DS);
949 cpu_x86_load_seg(env, R_CS, __USER_CS);
950 env->eflags &= ~TF_MASK;
952 unlock_user_struct(frame, frame_addr, 1);
954 return;
956 give_sigsegv:
957 unlock_user_struct(frame, frame_addr, 1);
958 if (sig == TARGET_SIGSEGV)
959 ka->_sa_handler = TARGET_SIG_DFL;
960 force_sig(TARGET_SIGSEGV /* , current */);
963 static int
964 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
966 unsigned int err = 0;
967 abi_ulong fpstate_addr;
968 unsigned int tmpflags;
970 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
971 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
972 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
973 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
975 env->regs[R_EDI] = tswapl(sc->edi);
976 env->regs[R_ESI] = tswapl(sc->esi);
977 env->regs[R_EBP] = tswapl(sc->ebp);
978 env->regs[R_ESP] = tswapl(sc->esp);
979 env->regs[R_EBX] = tswapl(sc->ebx);
980 env->regs[R_EDX] = tswapl(sc->edx);
981 env->regs[R_ECX] = tswapl(sc->ecx);
982 env->eip = tswapl(sc->eip);
984 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
985 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
987 tmpflags = tswapl(sc->eflags);
988 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
989 // regs->orig_eax = -1; /* disable syscall checks */
991 fpstate_addr = tswapl(sc->fpstate);
992 if (fpstate_addr != 0) {
993 if (!access_ok(VERIFY_READ, fpstate_addr,
994 sizeof(struct target_fpstate)))
995 goto badframe;
996 cpu_x86_frstor(env, fpstate_addr, 1);
999 *peax = tswapl(sc->eax);
1000 return err;
1001 badframe:
1002 return 1;
1005 long do_sigreturn(CPUX86State *env)
1007 struct sigframe *frame;
1008 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1009 target_sigset_t target_set;
1010 sigset_t set;
1011 int eax, i;
1013 #if defined(DEBUG_SIGNAL)
1014 fprintf(stderr, "do_sigreturn\n");
1015 #endif
1016 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1017 goto badframe;
1018 /* set blocked signals */
1019 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1020 goto badframe;
1021 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1022 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1023 goto badframe;
1026 target_to_host_sigset_internal(&set, &target_set);
1027 sigprocmask(SIG_SETMASK, &set, NULL);
1029 /* restore registers */
1030 if (restore_sigcontext(env, &frame->sc, &eax))
1031 goto badframe;
1032 unlock_user_struct(frame, frame_addr, 0);
1033 return eax;
1035 badframe:
1036 unlock_user_struct(frame, frame_addr, 0);
1037 force_sig(TARGET_SIGSEGV);
1038 return 0;
1041 long do_rt_sigreturn(CPUX86State *env)
1043 abi_ulong frame_addr;
1044 struct rt_sigframe *frame;
1045 sigset_t set;
1046 int eax;
1048 frame_addr = env->regs[R_ESP] - 4;
1049 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1050 goto badframe;
1051 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1052 sigprocmask(SIG_SETMASK, &set, NULL);
1054 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1055 goto badframe;
1057 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1058 get_sp_from_cpustate(env)) == -EFAULT)
1059 goto badframe;
1061 unlock_user_struct(frame, frame_addr, 0);
1062 return eax;
1064 badframe:
1065 unlock_user_struct(frame, frame_addr, 0);
1066 force_sig(TARGET_SIGSEGV);
1067 return 0;
1070 #elif defined(TARGET_ARM)
1072 struct target_sigcontext {
1073 abi_ulong trap_no;
1074 abi_ulong error_code;
1075 abi_ulong oldmask;
1076 abi_ulong arm_r0;
1077 abi_ulong arm_r1;
1078 abi_ulong arm_r2;
1079 abi_ulong arm_r3;
1080 abi_ulong arm_r4;
1081 abi_ulong arm_r5;
1082 abi_ulong arm_r6;
1083 abi_ulong arm_r7;
1084 abi_ulong arm_r8;
1085 abi_ulong arm_r9;
1086 abi_ulong arm_r10;
1087 abi_ulong arm_fp;
1088 abi_ulong arm_ip;
1089 abi_ulong arm_sp;
1090 abi_ulong arm_lr;
1091 abi_ulong arm_pc;
1092 abi_ulong arm_cpsr;
1093 abi_ulong fault_address;
1096 struct target_ucontext_v1 {
1097 abi_ulong tuc_flags;
1098 abi_ulong tuc_link;
1099 target_stack_t tuc_stack;
1100 struct target_sigcontext tuc_mcontext;
1101 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1104 struct target_ucontext_v2 {
1105 abi_ulong tuc_flags;
1106 abi_ulong tuc_link;
1107 target_stack_t tuc_stack;
1108 struct target_sigcontext tuc_mcontext;
1109 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1110 char __unused[128 - sizeof(target_sigset_t)];
1111 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1114 struct target_user_vfp {
1115 uint64_t fpregs[32];
1116 abi_ulong fpscr;
1119 struct target_user_vfp_exc {
1120 abi_ulong fpexc;
1121 abi_ulong fpinst;
1122 abi_ulong fpinst2;
1125 struct target_vfp_sigframe {
1126 abi_ulong magic;
1127 abi_ulong size;
1128 struct target_user_vfp ufp;
1129 struct target_user_vfp_exc ufp_exc;
1130 } __attribute__((__aligned__(8)));
1132 struct target_iwmmxt_sigframe {
1133 abi_ulong magic;
1134 abi_ulong size;
1135 uint64_t regs[16];
1136 /* Note that not all the coprocessor control registers are stored here */
1137 uint32_t wcssf;
1138 uint32_t wcasf;
1139 uint32_t wcgr0;
1140 uint32_t wcgr1;
1141 uint32_t wcgr2;
1142 uint32_t wcgr3;
1143 } __attribute__((__aligned__(8)));
1145 #define TARGET_VFP_MAGIC 0x56465001
1146 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1148 struct sigframe_v1
1150 struct target_sigcontext sc;
1151 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1152 abi_ulong retcode;
1155 struct sigframe_v2
1157 struct target_ucontext_v2 uc;
1158 abi_ulong retcode;
1161 struct rt_sigframe_v1
1163 abi_ulong pinfo;
1164 abi_ulong puc;
1165 struct target_siginfo info;
1166 struct target_ucontext_v1 uc;
1167 abi_ulong retcode;
1170 struct rt_sigframe_v2
1172 struct target_siginfo info;
1173 struct target_ucontext_v2 uc;
1174 abi_ulong retcode;
1177 #define TARGET_CONFIG_CPU_32 1
1180 * For ARM syscalls, we encode the syscall number into the instruction.
1182 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1183 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1186 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1187 * need two 16-bit instructions.
1189 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1190 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1192 static const abi_ulong retcodes[4] = {
1193 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1194 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1198 #define __get_user_error(x,p,e) __get_user(x, p)
1200 static inline int valid_user_regs(CPUState *regs)
1202 return 1;
1205 static void
1206 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1207 CPUState *env, abi_ulong mask)
1209 __put_user(env->regs[0], &sc->arm_r0);
1210 __put_user(env->regs[1], &sc->arm_r1);
1211 __put_user(env->regs[2], &sc->arm_r2);
1212 __put_user(env->regs[3], &sc->arm_r3);
1213 __put_user(env->regs[4], &sc->arm_r4);
1214 __put_user(env->regs[5], &sc->arm_r5);
1215 __put_user(env->regs[6], &sc->arm_r6);
1216 __put_user(env->regs[7], &sc->arm_r7);
1217 __put_user(env->regs[8], &sc->arm_r8);
1218 __put_user(env->regs[9], &sc->arm_r9);
1219 __put_user(env->regs[10], &sc->arm_r10);
1220 __put_user(env->regs[11], &sc->arm_fp);
1221 __put_user(env->regs[12], &sc->arm_ip);
1222 __put_user(env->regs[13], &sc->arm_sp);
1223 __put_user(env->regs[14], &sc->arm_lr);
1224 __put_user(env->regs[15], &sc->arm_pc);
1225 #ifdef TARGET_CONFIG_CPU_32
1226 __put_user(cpsr_read(env), &sc->arm_cpsr);
1227 #endif
1229 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1230 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1231 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1232 __put_user(mask, &sc->oldmask);
1235 static inline abi_ulong
1236 get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1238 unsigned long sp = regs->regs[13];
1241 * This is the X/Open sanctioned signal stack switching.
1243 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1244 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1246 * ATPCS B01 mandates 8-byte alignment
1248 return (sp - framesize) & ~7;
1251 static int
1252 setup_return(CPUState *env, struct target_sigaction *ka,
1253 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1255 abi_ulong handler = ka->_sa_handler;
1256 abi_ulong retcode;
1257 int thumb = handler & 1;
1258 uint32_t cpsr = cpsr_read(env);
1260 cpsr &= ~CPSR_IT;
1261 if (thumb) {
1262 cpsr |= CPSR_T;
1263 } else {
1264 cpsr &= ~CPSR_T;
1267 if (ka->sa_flags & TARGET_SA_RESTORER) {
1268 retcode = ka->sa_restorer;
1269 } else {
1270 unsigned int idx = thumb;
1272 if (ka->sa_flags & TARGET_SA_SIGINFO)
1273 idx += 2;
1275 if (__put_user(retcodes[idx], rc))
1276 return 1;
1278 retcode = rc_addr + thumb;
1281 env->regs[0] = usig;
1282 env->regs[13] = frame_addr;
1283 env->regs[14] = retcode;
1284 env->regs[15] = handler & (thumb ? ~1 : ~3);
1285 cpsr_write(env, cpsr, 0xffffffff);
1287 return 0;
1290 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUState *env)
1292 int i;
1293 struct target_vfp_sigframe *vfpframe;
1294 vfpframe = (struct target_vfp_sigframe *)regspace;
1295 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1296 __put_user(sizeof(*vfpframe), &vfpframe->size);
1297 for (i = 0; i < 32; i++) {
1298 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1300 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1301 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1302 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1303 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1304 return (abi_ulong*)(vfpframe+1);
1307 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace, CPUState *env)
1309 int i;
1310 struct target_iwmmxt_sigframe *iwmmxtframe;
1311 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1312 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1313 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1314 for (i = 0; i < 16; i++) {
1315 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1317 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1318 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1319 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1320 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1321 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1322 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1323 return (abi_ulong*)(iwmmxtframe+1);
1326 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1327 target_sigset_t *set, CPUState *env)
1329 struct target_sigaltstack stack;
1330 int i;
1331 abi_ulong *regspace;
1333 /* Clear all the bits of the ucontext we don't use. */
1334 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1336 memset(&stack, 0, sizeof(stack));
1337 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1338 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1339 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1340 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1342 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1343 /* Save coprocessor signal frame. */
1344 regspace = uc->tuc_regspace;
1345 if (arm_feature(env, ARM_FEATURE_VFP)) {
1346 regspace = setup_sigframe_v2_vfp(regspace, env);
1348 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1349 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1352 /* Write terminating magic word */
1353 __put_user(0, regspace);
1355 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1356 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1360 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1361 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1362 target_sigset_t *set, CPUState *regs)
1364 struct sigframe_v1 *frame;
1365 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1366 int i;
1368 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1369 return;
1371 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1373 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1374 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1375 goto end;
1378 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1379 frame_addr + offsetof(struct sigframe_v1, retcode));
1381 end:
1382 unlock_user_struct(frame, frame_addr, 1);
1385 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1386 target_sigset_t *set, CPUState *regs)
1388 struct sigframe_v2 *frame;
1389 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1391 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1392 return;
1394 setup_sigframe_v2(&frame->uc, set, regs);
1396 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1397 frame_addr + offsetof(struct sigframe_v2, retcode));
1399 unlock_user_struct(frame, frame_addr, 1);
1402 static void setup_frame(int usig, struct target_sigaction *ka,
1403 target_sigset_t *set, CPUState *regs)
1405 if (get_osversion() >= 0x020612) {
1406 setup_frame_v2(usig, ka, set, regs);
1407 } else {
1408 setup_frame_v1(usig, ka, set, regs);
1412 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1413 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1414 target_siginfo_t *info,
1415 target_sigset_t *set, CPUState *env)
1417 struct rt_sigframe_v1 *frame;
1418 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1419 struct target_sigaltstack stack;
1420 int i;
1421 abi_ulong info_addr, uc_addr;
1423 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1424 return /* 1 */;
1426 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1427 __put_user(info_addr, &frame->pinfo);
1428 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1429 __put_user(uc_addr, &frame->puc);
1430 copy_siginfo_to_user(&frame->info, info);
1432 /* Clear all the bits of the ucontext we don't use. */
1433 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1435 memset(&stack, 0, sizeof(stack));
1436 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1437 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1438 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1439 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1441 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1442 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1443 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1444 goto end;
1447 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1448 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1450 env->regs[1] = info_addr;
1451 env->regs[2] = uc_addr;
1453 end:
1454 unlock_user_struct(frame, frame_addr, 1);
1457 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1458 target_siginfo_t *info,
1459 target_sigset_t *set, CPUState *env)
1461 struct rt_sigframe_v2 *frame;
1462 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1463 abi_ulong info_addr, uc_addr;
1465 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1466 return /* 1 */;
1468 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1469 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1470 copy_siginfo_to_user(&frame->info, info);
1472 setup_sigframe_v2(&frame->uc, set, env);
1474 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1475 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1477 env->regs[1] = info_addr;
1478 env->regs[2] = uc_addr;
1480 unlock_user_struct(frame, frame_addr, 1);
1483 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1484 target_siginfo_t *info,
1485 target_sigset_t *set, CPUState *env)
1487 if (get_osversion() >= 0x020612) {
1488 setup_rt_frame_v2(usig, ka, info, set, env);
1489 } else {
1490 setup_rt_frame_v1(usig, ka, info, set, env);
1494 static int
1495 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1497 int err = 0;
1498 uint32_t cpsr;
1500 __get_user_error(env->regs[0], &sc->arm_r0, err);
1501 __get_user_error(env->regs[1], &sc->arm_r1, err);
1502 __get_user_error(env->regs[2], &sc->arm_r2, err);
1503 __get_user_error(env->regs[3], &sc->arm_r3, err);
1504 __get_user_error(env->regs[4], &sc->arm_r4, err);
1505 __get_user_error(env->regs[5], &sc->arm_r5, err);
1506 __get_user_error(env->regs[6], &sc->arm_r6, err);
1507 __get_user_error(env->regs[7], &sc->arm_r7, err);
1508 __get_user_error(env->regs[8], &sc->arm_r8, err);
1509 __get_user_error(env->regs[9], &sc->arm_r9, err);
1510 __get_user_error(env->regs[10], &sc->arm_r10, err);
1511 __get_user_error(env->regs[11], &sc->arm_fp, err);
1512 __get_user_error(env->regs[12], &sc->arm_ip, err);
1513 __get_user_error(env->regs[13], &sc->arm_sp, err);
1514 __get_user_error(env->regs[14], &sc->arm_lr, err);
1515 __get_user_error(env->regs[15], &sc->arm_pc, err);
1516 #ifdef TARGET_CONFIG_CPU_32
1517 __get_user_error(cpsr, &sc->arm_cpsr, err);
1518 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1519 #endif
1521 err |= !valid_user_regs(env);
1523 return err;
1526 static long do_sigreturn_v1(CPUState *env)
1528 abi_ulong frame_addr;
1529 struct sigframe_v1 *frame;
1530 target_sigset_t set;
1531 sigset_t host_set;
1532 int i;
1535 * Since we stacked the signal on a 64-bit boundary,
1536 * then 'sp' should be word aligned here. If it's
1537 * not, then the user is trying to mess with us.
1539 if (env->regs[13] & 7)
1540 goto badframe;
1542 frame_addr = env->regs[13];
1543 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1544 goto badframe;
1546 if (__get_user(set.sig[0], &frame->sc.oldmask))
1547 goto badframe;
1548 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1549 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1550 goto badframe;
1553 target_to_host_sigset_internal(&host_set, &set);
1554 sigprocmask(SIG_SETMASK, &host_set, NULL);
1556 if (restore_sigcontext(env, &frame->sc))
1557 goto badframe;
1559 #if 0
1560 /* Send SIGTRAP if we're single-stepping */
1561 if (ptrace_cancel_bpt(current))
1562 send_sig(SIGTRAP, current, 1);
1563 #endif
1564 unlock_user_struct(frame, frame_addr, 0);
1565 return env->regs[0];
1567 badframe:
1568 unlock_user_struct(frame, frame_addr, 0);
1569 force_sig(TARGET_SIGSEGV /* , current */);
1570 return 0;
1573 static abi_ulong *restore_sigframe_v2_vfp(CPUState *env, abi_ulong *regspace)
1575 int i;
1576 abi_ulong magic, sz;
1577 uint32_t fpscr, fpexc;
1578 struct target_vfp_sigframe *vfpframe;
1579 vfpframe = (struct target_vfp_sigframe *)regspace;
1581 __get_user(magic, &vfpframe->magic);
1582 __get_user(sz, &vfpframe->size);
1583 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1584 return 0;
1586 for (i = 0; i < 32; i++) {
1587 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1589 __get_user(fpscr, &vfpframe->ufp.fpscr);
1590 vfp_set_fpscr(env, fpscr);
1591 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1592 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1593 * and the exception flag is cleared
1595 fpexc |= (1 << 30);
1596 fpexc &= ~((1 << 31) | (1 << 28));
1597 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1598 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1599 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1600 return (abi_ulong*)(vfpframe + 1);
1603 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUState *env, abi_ulong *regspace)
1605 int i;
1606 abi_ulong magic, sz;
1607 struct target_iwmmxt_sigframe *iwmmxtframe;
1608 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1610 __get_user(magic, &iwmmxtframe->magic);
1611 __get_user(sz, &iwmmxtframe->size);
1612 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1613 return 0;
1615 for (i = 0; i < 16; i++) {
1616 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1618 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1619 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1620 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1621 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1622 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1623 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1624 return (abi_ulong*)(iwmmxtframe + 1);
1627 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1628 struct target_ucontext_v2 *uc)
1630 sigset_t host_set;
1631 abi_ulong *regspace;
1633 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1634 sigprocmask(SIG_SETMASK, &host_set, NULL);
1636 if (restore_sigcontext(env, &uc->tuc_mcontext))
1637 return 1;
1639 /* Restore coprocessor signal frame */
1640 regspace = uc->tuc_regspace;
1641 if (arm_feature(env, ARM_FEATURE_VFP)) {
1642 regspace = restore_sigframe_v2_vfp(env, regspace);
1643 if (!regspace) {
1644 return 1;
1647 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1648 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1649 if (!regspace) {
1650 return 1;
1654 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1655 return 1;
1657 #if 0
1658 /* Send SIGTRAP if we're single-stepping */
1659 if (ptrace_cancel_bpt(current))
1660 send_sig(SIGTRAP, current, 1);
1661 #endif
1663 return 0;
1666 static long do_sigreturn_v2(CPUState *env)
1668 abi_ulong frame_addr;
1669 struct sigframe_v2 *frame;
1672 * Since we stacked the signal on a 64-bit boundary,
1673 * then 'sp' should be word aligned here. If it's
1674 * not, then the user is trying to mess with us.
1676 if (env->regs[13] & 7)
1677 goto badframe;
1679 frame_addr = env->regs[13];
1680 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1681 goto badframe;
1683 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1684 goto badframe;
1686 unlock_user_struct(frame, frame_addr, 0);
1687 return env->regs[0];
1689 badframe:
1690 unlock_user_struct(frame, frame_addr, 0);
1691 force_sig(TARGET_SIGSEGV /* , current */);
1692 return 0;
1695 long do_sigreturn(CPUState *env)
1697 if (get_osversion() >= 0x020612) {
1698 return do_sigreturn_v2(env);
1699 } else {
1700 return do_sigreturn_v1(env);
1704 static long do_rt_sigreturn_v1(CPUState *env)
1706 abi_ulong frame_addr;
1707 struct rt_sigframe_v1 *frame;
1708 sigset_t host_set;
1711 * Since we stacked the signal on a 64-bit boundary,
1712 * then 'sp' should be word aligned here. If it's
1713 * not, then the user is trying to mess with us.
1715 if (env->regs[13] & 7)
1716 goto badframe;
1718 frame_addr = env->regs[13];
1719 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1720 goto badframe;
1722 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1723 sigprocmask(SIG_SETMASK, &host_set, NULL);
1725 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1726 goto badframe;
1728 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1729 goto badframe;
1731 #if 0
1732 /* Send SIGTRAP if we're single-stepping */
1733 if (ptrace_cancel_bpt(current))
1734 send_sig(SIGTRAP, current, 1);
1735 #endif
1736 unlock_user_struct(frame, frame_addr, 0);
1737 return env->regs[0];
1739 badframe:
1740 unlock_user_struct(frame, frame_addr, 0);
1741 force_sig(TARGET_SIGSEGV /* , current */);
1742 return 0;
1745 static long do_rt_sigreturn_v2(CPUState *env)
1747 abi_ulong frame_addr;
1748 struct rt_sigframe_v2 *frame;
1751 * Since we stacked the signal on a 64-bit boundary,
1752 * then 'sp' should be word aligned here. If it's
1753 * not, then the user is trying to mess with us.
1755 if (env->regs[13] & 7)
1756 goto badframe;
1758 frame_addr = env->regs[13];
1759 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1760 goto badframe;
1762 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1763 goto badframe;
1765 unlock_user_struct(frame, frame_addr, 0);
1766 return env->regs[0];
1768 badframe:
1769 unlock_user_struct(frame, frame_addr, 0);
1770 force_sig(TARGET_SIGSEGV /* , current */);
1771 return 0;
1774 long do_rt_sigreturn(CPUState *env)
1776 if (get_osversion() >= 0x020612) {
1777 return do_rt_sigreturn_v2(env);
1778 } else {
1779 return do_rt_sigreturn_v1(env);
1783 #elif defined(TARGET_SPARC)
1785 #define __SUNOS_MAXWIN 31
1787 /* This is what SunOS does, so shall I. */
1788 struct target_sigcontext {
1789 abi_ulong sigc_onstack; /* state to restore */
1791 abi_ulong sigc_mask; /* sigmask to restore */
1792 abi_ulong sigc_sp; /* stack pointer */
1793 abi_ulong sigc_pc; /* program counter */
1794 abi_ulong sigc_npc; /* next program counter */
1795 abi_ulong sigc_psr; /* for condition codes etc */
1796 abi_ulong sigc_g1; /* User uses these two registers */
1797 abi_ulong sigc_o0; /* within the trampoline code. */
1799 /* Now comes information regarding the users window set
1800 * at the time of the signal.
1802 abi_ulong sigc_oswins; /* outstanding windows */
1804 /* stack ptrs for each regwin buf */
1805 char *sigc_spbuf[__SUNOS_MAXWIN];
1807 /* Windows to restore after signal */
1808 struct {
1809 abi_ulong locals[8];
1810 abi_ulong ins[8];
1811 } sigc_wbuf[__SUNOS_MAXWIN];
1813 /* A Sparc stack frame */
1814 struct sparc_stackf {
1815 abi_ulong locals[8];
1816 abi_ulong ins[8];
1817 /* It's simpler to treat fp and callers_pc as elements of ins[]
1818 * since we never need to access them ourselves.
1820 char *structptr;
1821 abi_ulong xargs[6];
1822 abi_ulong xxargs[1];
1825 typedef struct {
1826 struct {
1827 abi_ulong psr;
1828 abi_ulong pc;
1829 abi_ulong npc;
1830 abi_ulong y;
1831 abi_ulong u_regs[16]; /* globals and ins */
1832 } si_regs;
1833 int si_mask;
1834 } __siginfo_t;
1836 typedef struct {
1837 unsigned long si_float_regs [32];
1838 unsigned long si_fsr;
1839 unsigned long si_fpqdepth;
1840 struct {
1841 unsigned long *insn_addr;
1842 unsigned long insn;
1843 } si_fpqueue [16];
1844 } qemu_siginfo_fpu_t;
1847 struct target_signal_frame {
1848 struct sparc_stackf ss;
1849 __siginfo_t info;
1850 abi_ulong fpu_save;
1851 abi_ulong insns[2] __attribute__ ((aligned (8)));
1852 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1853 abi_ulong extra_size; /* Should be 0 */
1854 qemu_siginfo_fpu_t fpu_state;
1856 struct target_rt_signal_frame {
1857 struct sparc_stackf ss;
1858 siginfo_t info;
1859 abi_ulong regs[20];
1860 sigset_t mask;
1861 abi_ulong fpu_save;
1862 unsigned int insns[2];
1863 stack_t stack;
1864 unsigned int extra_size; /* Should be 0 */
1865 qemu_siginfo_fpu_t fpu_state;
1868 #define UREG_O0 16
1869 #define UREG_O6 22
1870 #define UREG_I0 0
1871 #define UREG_I1 1
1872 #define UREG_I2 2
1873 #define UREG_I3 3
1874 #define UREG_I4 4
1875 #define UREG_I5 5
1876 #define UREG_I6 6
1877 #define UREG_I7 7
1878 #define UREG_L0 8
1879 #define UREG_FP UREG_I6
1880 #define UREG_SP UREG_O6
1882 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1883 CPUState *env, unsigned long framesize)
1885 abi_ulong sp;
1887 sp = env->regwptr[UREG_FP];
1889 /* This is the X/Open sanctioned signal stack switching. */
1890 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1891 if (!on_sig_stack(sp)
1892 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1893 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1895 return sp - framesize;
1898 static int
1899 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1901 int err = 0, i;
1903 err |= __put_user(env->psr, &si->si_regs.psr);
1904 err |= __put_user(env->pc, &si->si_regs.pc);
1905 err |= __put_user(env->npc, &si->si_regs.npc);
1906 err |= __put_user(env->y, &si->si_regs.y);
1907 for (i=0; i < 8; i++) {
1908 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1910 for (i=0; i < 8; i++) {
1911 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1913 err |= __put_user(mask, &si->si_mask);
1914 return err;
1917 #if 0
1918 static int
1919 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1920 CPUState *env, unsigned long mask)
1922 int err = 0;
1924 err |= __put_user(mask, &sc->sigc_mask);
1925 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1926 err |= __put_user(env->pc, &sc->sigc_pc);
1927 err |= __put_user(env->npc, &sc->sigc_npc);
1928 err |= __put_user(env->psr, &sc->sigc_psr);
1929 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1930 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1932 return err;
1934 #endif
1935 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1937 static void setup_frame(int sig, struct target_sigaction *ka,
1938 target_sigset_t *set, CPUState *env)
1940 abi_ulong sf_addr;
1941 struct target_signal_frame *sf;
1942 int sigframe_size, err, i;
1944 /* 1. Make sure everything is clean */
1945 //synchronize_user_stack();
1947 sigframe_size = NF_ALIGNEDSZ;
1948 sf_addr = get_sigframe(ka, env, sigframe_size);
1950 sf = lock_user(VERIFY_WRITE, sf_addr,
1951 sizeof(struct target_signal_frame), 0);
1952 if (!sf)
1953 goto sigsegv;
1955 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1956 #if 0
1957 if (invalid_frame_pointer(sf, sigframe_size))
1958 goto sigill_and_return;
1959 #endif
1960 /* 2. Save the current process state */
1961 err = setup___siginfo(&sf->info, env, set->sig[0]);
1962 err |= __put_user(0, &sf->extra_size);
1964 //err |= save_fpu_state(regs, &sf->fpu_state);
1965 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1967 err |= __put_user(set->sig[0], &sf->info.si_mask);
1968 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1969 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1972 for (i = 0; i < 8; i++) {
1973 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1975 for (i = 0; i < 8; i++) {
1976 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1978 if (err)
1979 goto sigsegv;
1981 /* 3. signal handler back-trampoline and parameters */
1982 env->regwptr[UREG_FP] = sf_addr;
1983 env->regwptr[UREG_I0] = sig;
1984 env->regwptr[UREG_I1] = sf_addr +
1985 offsetof(struct target_signal_frame, info);
1986 env->regwptr[UREG_I2] = sf_addr +
1987 offsetof(struct target_signal_frame, info);
1989 /* 4. signal handler */
1990 env->pc = ka->_sa_handler;
1991 env->npc = (env->pc + 4);
1992 /* 5. return to kernel instructions */
1993 if (ka->sa_restorer)
1994 env->regwptr[UREG_I7] = ka->sa_restorer;
1995 else {
1996 uint32_t val32;
1998 env->regwptr[UREG_I7] = sf_addr +
1999 offsetof(struct target_signal_frame, insns) - 2 * 4;
2001 /* mov __NR_sigreturn, %g1 */
2002 val32 = 0x821020d8;
2003 err |= __put_user(val32, &sf->insns[0]);
2005 /* t 0x10 */
2006 val32 = 0x91d02010;
2007 err |= __put_user(val32, &sf->insns[1]);
2008 if (err)
2009 goto sigsegv;
2011 /* Flush instruction space. */
2012 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2013 // tb_flush(env);
2015 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2016 return;
2017 #if 0
2018 sigill_and_return:
2019 force_sig(TARGET_SIGILL);
2020 #endif
2021 sigsegv:
2022 //fprintf(stderr, "force_sig\n");
2023 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2024 force_sig(TARGET_SIGSEGV);
2026 static inline int
2027 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
2029 int err;
2030 #if 0
2031 #ifdef CONFIG_SMP
2032 if (current->flags & PF_USEDFPU)
2033 regs->psr &= ~PSR_EF;
2034 #else
2035 if (current == last_task_used_math) {
2036 last_task_used_math = 0;
2037 regs->psr &= ~PSR_EF;
2039 #endif
2040 current->used_math = 1;
2041 current->flags &= ~PF_USEDFPU;
2042 #endif
2043 #if 0
2044 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2045 return -EFAULT;
2046 #endif
2048 #if 0
2049 /* XXX: incorrect */
2050 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
2051 (sizeof(unsigned long) * 32));
2052 #endif
2053 err |= __get_user(env->fsr, &fpu->si_fsr);
2054 #if 0
2055 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2056 if (current->thread.fpqdepth != 0)
2057 err |= __copy_from_user(&current->thread.fpqueue[0],
2058 &fpu->si_fpqueue[0],
2059 ((sizeof(unsigned long) +
2060 (sizeof(unsigned long *)))*16));
2061 #endif
2062 return err;
2066 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2067 target_siginfo_t *info,
2068 target_sigset_t *set, CPUState *env)
2070 fprintf(stderr, "setup_rt_frame: not implemented\n");
2073 long do_sigreturn(CPUState *env)
2075 abi_ulong sf_addr;
2076 struct target_signal_frame *sf;
2077 uint32_t up_psr, pc, npc;
2078 target_sigset_t set;
2079 sigset_t host_set;
2080 int err, i;
2082 sf_addr = env->regwptr[UREG_FP];
2083 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2084 goto segv_and_exit;
2085 #if 0
2086 fprintf(stderr, "sigreturn\n");
2087 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2088 #endif
2089 //cpu_dump_state(env, stderr, fprintf, 0);
2091 /* 1. Make sure we are not getting garbage from the user */
2093 if (sf_addr & 3)
2094 goto segv_and_exit;
2096 err = __get_user(pc, &sf->info.si_regs.pc);
2097 err |= __get_user(npc, &sf->info.si_regs.npc);
2099 if ((pc | npc) & 3)
2100 goto segv_and_exit;
2102 /* 2. Restore the state */
2103 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2105 /* User can only change condition codes and FPU enabling in %psr. */
2106 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2107 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2109 env->pc = pc;
2110 env->npc = npc;
2111 err |= __get_user(env->y, &sf->info.si_regs.y);
2112 for (i=0; i < 8; i++) {
2113 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2115 for (i=0; i < 8; i++) {
2116 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2119 /* FIXME: implement FPU save/restore:
2120 * __get_user(fpu_save, &sf->fpu_save);
2121 * if (fpu_save)
2122 * err |= restore_fpu_state(env, fpu_save);
2125 /* This is pretty much atomic, no amount locking would prevent
2126 * the races which exist anyways.
2128 err |= __get_user(set.sig[0], &sf->info.si_mask);
2129 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2130 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2133 target_to_host_sigset_internal(&host_set, &set);
2134 sigprocmask(SIG_SETMASK, &host_set, NULL);
2136 if (err)
2137 goto segv_and_exit;
2138 unlock_user_struct(sf, sf_addr, 0);
2139 return env->regwptr[0];
2141 segv_and_exit:
2142 unlock_user_struct(sf, sf_addr, 0);
2143 force_sig(TARGET_SIGSEGV);
2146 long do_rt_sigreturn(CPUState *env)
2148 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2149 return -TARGET_ENOSYS;
2152 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2153 #define MC_TSTATE 0
2154 #define MC_PC 1
2155 #define MC_NPC 2
2156 #define MC_Y 3
2157 #define MC_G1 4
2158 #define MC_G2 5
2159 #define MC_G3 6
2160 #define MC_G4 7
2161 #define MC_G5 8
2162 #define MC_G6 9
2163 #define MC_G7 10
2164 #define MC_O0 11
2165 #define MC_O1 12
2166 #define MC_O2 13
2167 #define MC_O3 14
2168 #define MC_O4 15
2169 #define MC_O5 16
2170 #define MC_O6 17
2171 #define MC_O7 18
2172 #define MC_NGREG 19
2174 typedef abi_ulong target_mc_greg_t;
2175 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2177 struct target_mc_fq {
2178 abi_ulong *mcfq_addr;
2179 uint32_t mcfq_insn;
2182 struct target_mc_fpu {
2183 union {
2184 uint32_t sregs[32];
2185 uint64_t dregs[32];
2186 //uint128_t qregs[16];
2187 } mcfpu_fregs;
2188 abi_ulong mcfpu_fsr;
2189 abi_ulong mcfpu_fprs;
2190 abi_ulong mcfpu_gsr;
2191 struct target_mc_fq *mcfpu_fq;
2192 unsigned char mcfpu_qcnt;
2193 unsigned char mcfpu_qentsz;
2194 unsigned char mcfpu_enab;
2196 typedef struct target_mc_fpu target_mc_fpu_t;
2198 typedef struct {
2199 target_mc_gregset_t mc_gregs;
2200 target_mc_greg_t mc_fp;
2201 target_mc_greg_t mc_i7;
2202 target_mc_fpu_t mc_fpregs;
2203 } target_mcontext_t;
2205 struct target_ucontext {
2206 struct target_ucontext *tuc_link;
2207 abi_ulong tuc_flags;
2208 target_sigset_t tuc_sigmask;
2209 target_mcontext_t tuc_mcontext;
2212 /* A V9 register window */
2213 struct target_reg_window {
2214 abi_ulong locals[8];
2215 abi_ulong ins[8];
2218 #define TARGET_STACK_BIAS 2047
2220 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2221 void sparc64_set_context(CPUSPARCState *env)
2223 abi_ulong ucp_addr;
2224 struct target_ucontext *ucp;
2225 target_mc_gregset_t *grp;
2226 abi_ulong pc, npc, tstate;
2227 abi_ulong fp, i7, w_addr;
2228 int err;
2229 unsigned int i;
2231 ucp_addr = env->regwptr[UREG_I0];
2232 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2233 goto do_sigsegv;
2234 grp = &ucp->tuc_mcontext.mc_gregs;
2235 err = __get_user(pc, &((*grp)[MC_PC]));
2236 err |= __get_user(npc, &((*grp)[MC_NPC]));
2237 if (err || ((pc | npc) & 3))
2238 goto do_sigsegv;
2239 if (env->regwptr[UREG_I1]) {
2240 target_sigset_t target_set;
2241 sigset_t set;
2243 if (TARGET_NSIG_WORDS == 1) {
2244 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2245 goto do_sigsegv;
2246 } else {
2247 abi_ulong *src, *dst;
2248 src = ucp->tuc_sigmask.sig;
2249 dst = target_set.sig;
2250 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2251 i++, dst++, src++)
2252 err |= __get_user(*dst, src);
2253 if (err)
2254 goto do_sigsegv;
2256 target_to_host_sigset_internal(&set, &target_set);
2257 sigprocmask(SIG_SETMASK, &set, NULL);
2259 env->pc = pc;
2260 env->npc = npc;
2261 err |= __get_user(env->y, &((*grp)[MC_Y]));
2262 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2263 env->asi = (tstate >> 24) & 0xff;
2264 cpu_put_ccr(env, tstate >> 32);
2265 cpu_put_cwp64(env, tstate & 0x1f);
2266 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2267 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2268 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2269 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2270 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2271 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2272 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2273 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2274 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2275 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2276 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2277 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2278 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2279 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2280 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2282 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2283 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2285 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2286 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2287 abi_ulong) != 0)
2288 goto do_sigsegv;
2289 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2290 abi_ulong) != 0)
2291 goto do_sigsegv;
2292 /* FIXME this does not match how the kernel handles the FPU in
2293 * its sparc64_set_context implementation. In particular the FPU
2294 * is only restored if fenab is non-zero in:
2295 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2297 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2299 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2300 for (i = 0; i < 64; i++, src++) {
2301 if (i & 1) {
2302 err |= __get_user(env->fpr[i/2].l.lower, src);
2303 } else {
2304 err |= __get_user(env->fpr[i/2].l.upper, src);
2308 err |= __get_user(env->fsr,
2309 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2310 err |= __get_user(env->gsr,
2311 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2312 if (err)
2313 goto do_sigsegv;
2314 unlock_user_struct(ucp, ucp_addr, 0);
2315 return;
2316 do_sigsegv:
2317 unlock_user_struct(ucp, ucp_addr, 0);
2318 force_sig(TARGET_SIGSEGV);
2321 void sparc64_get_context(CPUSPARCState *env)
2323 abi_ulong ucp_addr;
2324 struct target_ucontext *ucp;
2325 target_mc_gregset_t *grp;
2326 target_mcontext_t *mcp;
2327 abi_ulong fp, i7, w_addr;
2328 int err;
2329 unsigned int i;
2330 target_sigset_t target_set;
2331 sigset_t set;
2333 ucp_addr = env->regwptr[UREG_I0];
2334 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2335 goto do_sigsegv;
2337 mcp = &ucp->tuc_mcontext;
2338 grp = &mcp->mc_gregs;
2340 /* Skip over the trap instruction, first. */
2341 env->pc = env->npc;
2342 env->npc += 4;
2344 err = 0;
2346 sigprocmask(0, NULL, &set);
2347 host_to_target_sigset_internal(&target_set, &set);
2348 if (TARGET_NSIG_WORDS == 1) {
2349 err |= __put_user(target_set.sig[0],
2350 (abi_ulong *)&ucp->tuc_sigmask);
2351 } else {
2352 abi_ulong *src, *dst;
2353 src = target_set.sig;
2354 dst = ucp->tuc_sigmask.sig;
2355 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2356 i++, dst++, src++)
2357 err |= __put_user(*src, dst);
2358 if (err)
2359 goto do_sigsegv;
2362 /* XXX: tstate must be saved properly */
2363 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2364 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2365 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2366 err |= __put_user(env->y, &((*grp)[MC_Y]));
2367 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2368 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2369 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2370 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2371 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2372 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2373 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2374 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2375 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2376 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2377 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2378 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2379 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2380 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2381 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2383 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2384 fp = i7 = 0;
2385 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2386 abi_ulong) != 0)
2387 goto do_sigsegv;
2388 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2389 abi_ulong) != 0)
2390 goto do_sigsegv;
2391 err |= __put_user(fp, &(mcp->mc_fp));
2392 err |= __put_user(i7, &(mcp->mc_i7));
2395 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2396 for (i = 0; i < 64; i++, dst++) {
2397 if (i & 1) {
2398 err |= __put_user(env->fpr[i/2].l.lower, dst);
2399 } else {
2400 err |= __put_user(env->fpr[i/2].l.upper, dst);
2404 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2405 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2406 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2408 if (err)
2409 goto do_sigsegv;
2410 unlock_user_struct(ucp, ucp_addr, 1);
2411 return;
2412 do_sigsegv:
2413 unlock_user_struct(ucp, ucp_addr, 1);
2414 force_sig(TARGET_SIGSEGV);
2416 #endif
2417 #elif defined(TARGET_ABI_MIPSN64)
2419 # warning signal handling not implemented
2421 static void setup_frame(int sig, struct target_sigaction *ka,
2422 target_sigset_t *set, CPUState *env)
2424 fprintf(stderr, "setup_frame: not implemented\n");
2427 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2428 target_siginfo_t *info,
2429 target_sigset_t *set, CPUState *env)
2431 fprintf(stderr, "setup_rt_frame: not implemented\n");
2434 long do_sigreturn(CPUState *env)
2436 fprintf(stderr, "do_sigreturn: not implemented\n");
2437 return -TARGET_ENOSYS;
2440 long do_rt_sigreturn(CPUState *env)
2442 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2443 return -TARGET_ENOSYS;
2446 #elif defined(TARGET_ABI_MIPSN32)
2448 # warning signal handling not implemented
2450 static void setup_frame(int sig, struct target_sigaction *ka,
2451 target_sigset_t *set, CPUState *env)
2453 fprintf(stderr, "setup_frame: not implemented\n");
2456 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2457 target_siginfo_t *info,
2458 target_sigset_t *set, CPUState *env)
2460 fprintf(stderr, "setup_rt_frame: not implemented\n");
2463 long do_sigreturn(CPUState *env)
2465 fprintf(stderr, "do_sigreturn: not implemented\n");
2466 return -TARGET_ENOSYS;
2469 long do_rt_sigreturn(CPUState *env)
2471 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2472 return -TARGET_ENOSYS;
2475 #elif defined(TARGET_ABI_MIPSO32)
2477 struct target_sigcontext {
2478 uint32_t sc_regmask; /* Unused */
2479 uint32_t sc_status;
2480 uint64_t sc_pc;
2481 uint64_t sc_regs[32];
2482 uint64_t sc_fpregs[32];
2483 uint32_t sc_ownedfp; /* Unused */
2484 uint32_t sc_fpc_csr;
2485 uint32_t sc_fpc_eir; /* Unused */
2486 uint32_t sc_used_math;
2487 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2488 uint32_t pad0;
2489 uint64_t sc_mdhi;
2490 uint64_t sc_mdlo;
2491 target_ulong sc_hi1; /* Was sc_cause */
2492 target_ulong sc_lo1; /* Was sc_badvaddr */
2493 target_ulong sc_hi2; /* Was sc_sigset[4] */
2494 target_ulong sc_lo2;
2495 target_ulong sc_hi3;
2496 target_ulong sc_lo3;
2499 struct sigframe {
2500 uint32_t sf_ass[4]; /* argument save space for o32 */
2501 uint32_t sf_code[2]; /* signal trampoline */
2502 struct target_sigcontext sf_sc;
2503 target_sigset_t sf_mask;
2506 struct target_ucontext {
2507 target_ulong tuc_flags;
2508 target_ulong tuc_link;
2509 target_stack_t tuc_stack;
2510 target_ulong pad0;
2511 struct target_sigcontext tuc_mcontext;
2512 target_sigset_t tuc_sigmask;
2515 struct target_rt_sigframe {
2516 uint32_t rs_ass[4]; /* argument save space for o32 */
2517 uint32_t rs_code[2]; /* signal trampoline */
2518 struct target_siginfo rs_info;
2519 struct target_ucontext rs_uc;
2522 /* Install trampoline to jump back from signal handler */
2523 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2525 int err;
2528 * Set up the return code ...
2530 * li v0, __NR__foo_sigreturn
2531 * syscall
2534 err = __put_user(0x24020000 + syscall, tramp + 0);
2535 err |= __put_user(0x0000000c , tramp + 1);
2536 /* flush_cache_sigtramp((unsigned long) tramp); */
2537 return err;
2540 static inline int
2541 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2543 int err = 0;
2545 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2547 #define save_gp_reg(i) do { \
2548 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2549 } while(0)
2550 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2551 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2552 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2553 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2554 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2555 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2556 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2557 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2558 save_gp_reg(31);
2559 #undef save_gp_reg
2561 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2562 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2564 /* Not used yet, but might be useful if we ever have DSP suppport */
2565 #if 0
2566 if (cpu_has_dsp) {
2567 err |= __put_user(mfhi1(), &sc->sc_hi1);
2568 err |= __put_user(mflo1(), &sc->sc_lo1);
2569 err |= __put_user(mfhi2(), &sc->sc_hi2);
2570 err |= __put_user(mflo2(), &sc->sc_lo2);
2571 err |= __put_user(mfhi3(), &sc->sc_hi3);
2572 err |= __put_user(mflo3(), &sc->sc_lo3);
2573 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2575 /* same with 64 bit */
2576 #ifdef CONFIG_64BIT
2577 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2578 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2579 if (cpu_has_dsp) {
2580 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2581 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2582 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2583 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2584 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2585 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2586 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2588 #endif
2589 #endif
2591 #if 0
2592 err |= __put_user(!!used_math(), &sc->sc_used_math);
2594 if (!used_math())
2595 goto out;
2598 * Save FPU state to signal context. Signal handler will "inherit"
2599 * current FPU state.
2601 preempt_disable();
2603 if (!is_fpu_owner()) {
2604 own_fpu();
2605 restore_fp(current);
2607 err |= save_fp_context(sc);
2609 preempt_enable();
2610 out:
2611 #endif
2612 return err;
2615 static inline int
2616 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2618 int err = 0;
2620 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2622 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2623 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2625 #define restore_gp_reg(i) do { \
2626 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2627 } while(0)
2628 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2629 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2630 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2631 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2632 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2633 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2634 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2635 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2636 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2637 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2638 restore_gp_reg(31);
2639 #undef restore_gp_reg
2641 #if 0
2642 if (cpu_has_dsp) {
2643 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2644 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2645 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2646 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2647 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2648 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2649 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2651 #ifdef CONFIG_64BIT
2652 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2653 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2654 if (cpu_has_dsp) {
2655 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2656 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2657 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2658 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2659 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2660 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2661 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2663 #endif
2665 err |= __get_user(used_math, &sc->sc_used_math);
2666 conditional_used_math(used_math);
2668 preempt_disable();
2670 if (used_math()) {
2671 /* restore fpu context if we have used it before */
2672 own_fpu();
2673 err |= restore_fp_context(sc);
2674 } else {
2675 /* signal handler may have used FPU. Give it up. */
2676 lose_fpu();
2679 preempt_enable();
2680 #endif
2681 return err;
2684 * Determine which stack to use..
2686 static inline abi_ulong
2687 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2689 unsigned long sp;
2691 /* Default to using normal stack */
2692 sp = regs->active_tc.gpr[29];
2695 * FPU emulator may have it's own trampoline active just
2696 * above the user stack, 16-bytes before the next lowest
2697 * 16 byte boundary. Try to avoid trashing it.
2699 sp -= 32;
2701 /* This is the X/Open sanctioned signal stack switching. */
2702 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2703 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2706 return (sp - frame_size) & ~7;
2709 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2710 static void setup_frame(int sig, struct target_sigaction * ka,
2711 target_sigset_t *set, CPUState *regs)
2713 struct sigframe *frame;
2714 abi_ulong frame_addr;
2715 int i;
2717 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2718 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2719 goto give_sigsegv;
2721 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2723 if(setup_sigcontext(regs, &frame->sf_sc))
2724 goto give_sigsegv;
2726 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2727 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2728 goto give_sigsegv;
2732 * Arguments to signal handler:
2734 * a0 = signal number
2735 * a1 = 0 (should be cause)
2736 * a2 = pointer to struct sigcontext
2738 * $25 and PC point to the signal handler, $29 points to the
2739 * struct sigframe.
2741 regs->active_tc.gpr[ 4] = sig;
2742 regs->active_tc.gpr[ 5] = 0;
2743 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2744 regs->active_tc.gpr[29] = frame_addr;
2745 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2746 /* The original kernel code sets CP0_EPC to the handler
2747 * since it returns to userland using eret
2748 * we cannot do this here, and we must set PC directly */
2749 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2750 unlock_user_struct(frame, frame_addr, 1);
2751 return;
2753 give_sigsegv:
2754 unlock_user_struct(frame, frame_addr, 1);
2755 force_sig(TARGET_SIGSEGV/*, current*/);
2756 return;
2759 long do_sigreturn(CPUState *regs)
2761 struct sigframe *frame;
2762 abi_ulong frame_addr;
2763 sigset_t blocked;
2764 target_sigset_t target_set;
2765 int i;
2767 #if defined(DEBUG_SIGNAL)
2768 fprintf(stderr, "do_sigreturn\n");
2769 #endif
2770 frame_addr = regs->active_tc.gpr[29];
2771 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2772 goto badframe;
2774 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2775 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2776 goto badframe;
2779 target_to_host_sigset_internal(&blocked, &target_set);
2780 sigprocmask(SIG_SETMASK, &blocked, NULL);
2782 if (restore_sigcontext(regs, &frame->sf_sc))
2783 goto badframe;
2785 #if 0
2787 * Don't let your children do this ...
2789 __asm__ __volatile__(
2790 "move\t$29, %0\n\t"
2791 "j\tsyscall_exit"
2792 :/* no outputs */
2793 :"r" (&regs));
2794 /* Unreached */
2795 #endif
2797 regs->active_tc.PC = regs->CP0_EPC;
2798 /* I am not sure this is right, but it seems to work
2799 * maybe a problem with nested signals ? */
2800 regs->CP0_EPC = 0;
2801 return -TARGET_QEMU_ESIGRETURN;
2803 badframe:
2804 force_sig(TARGET_SIGSEGV/*, current*/);
2805 return 0;
2808 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2809 target_siginfo_t *info,
2810 target_sigset_t *set, CPUState *env)
2812 struct target_rt_sigframe *frame;
2813 abi_ulong frame_addr;
2814 int i;
2816 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2817 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2818 goto give_sigsegv;
2820 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2822 copy_siginfo_to_user(&frame->rs_info, info);
2824 __put_user(0, &frame->rs_uc.tuc_flags);
2825 __put_user(0, &frame->rs_uc.tuc_link);
2826 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2827 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2828 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2829 &frame->rs_uc.tuc_stack.ss_flags);
2831 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2833 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2834 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2838 * Arguments to signal handler:
2840 * a0 = signal number
2841 * a1 = pointer to struct siginfo
2842 * a2 = pointer to struct ucontext
2844 * $25 and PC point to the signal handler, $29 points to the
2845 * struct sigframe.
2847 env->active_tc.gpr[ 4] = sig;
2848 env->active_tc.gpr[ 5] = frame_addr
2849 + offsetof(struct target_rt_sigframe, rs_info);
2850 env->active_tc.gpr[ 6] = frame_addr
2851 + offsetof(struct target_rt_sigframe, rs_uc);
2852 env->active_tc.gpr[29] = frame_addr;
2853 env->active_tc.gpr[31] = frame_addr
2854 + offsetof(struct target_rt_sigframe, rs_code);
2855 /* The original kernel code sets CP0_EPC to the handler
2856 * since it returns to userland using eret
2857 * we cannot do this here, and we must set PC directly */
2858 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2859 unlock_user_struct(frame, frame_addr, 1);
2860 return;
2862 give_sigsegv:
2863 unlock_user_struct(frame, frame_addr, 1);
2864 force_sig(TARGET_SIGSEGV/*, current*/);
2865 return;
2868 long do_rt_sigreturn(CPUState *env)
2870 struct target_rt_sigframe *frame;
2871 abi_ulong frame_addr;
2872 sigset_t blocked;
2874 #if defined(DEBUG_SIGNAL)
2875 fprintf(stderr, "do_rt_sigreturn\n");
2876 #endif
2877 frame_addr = env->active_tc.gpr[29];
2878 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2879 goto badframe;
2881 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2882 sigprocmask(SIG_SETMASK, &blocked, NULL);
2884 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2885 goto badframe;
2887 if (do_sigaltstack(frame_addr +
2888 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2889 0, get_sp_from_cpustate(env)) == -EFAULT)
2890 goto badframe;
2892 env->active_tc.PC = env->CP0_EPC;
2893 /* I am not sure this is right, but it seems to work
2894 * maybe a problem with nested signals ? */
2895 env->CP0_EPC = 0;
2896 return -TARGET_QEMU_ESIGRETURN;
2898 badframe:
2899 force_sig(TARGET_SIGSEGV/*, current*/);
2900 return 0;
2903 #elif defined(TARGET_SH4)
2906 * code and data structures from linux kernel:
2907 * include/asm-sh/sigcontext.h
2908 * arch/sh/kernel/signal.c
2911 struct target_sigcontext {
2912 target_ulong oldmask;
2914 /* CPU registers */
2915 target_ulong sc_gregs[16];
2916 target_ulong sc_pc;
2917 target_ulong sc_pr;
2918 target_ulong sc_sr;
2919 target_ulong sc_gbr;
2920 target_ulong sc_mach;
2921 target_ulong sc_macl;
2923 /* FPU registers */
2924 target_ulong sc_fpregs[16];
2925 target_ulong sc_xfpregs[16];
2926 unsigned int sc_fpscr;
2927 unsigned int sc_fpul;
2928 unsigned int sc_ownedfp;
2931 struct target_sigframe
2933 struct target_sigcontext sc;
2934 target_ulong extramask[TARGET_NSIG_WORDS-1];
2935 uint16_t retcode[3];
2939 struct target_ucontext {
2940 target_ulong tuc_flags;
2941 struct target_ucontext *tuc_link;
2942 target_stack_t tuc_stack;
2943 struct target_sigcontext tuc_mcontext;
2944 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2947 struct target_rt_sigframe
2949 struct target_siginfo info;
2950 struct target_ucontext uc;
2951 uint16_t retcode[3];
2955 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2956 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2958 static abi_ulong get_sigframe(struct target_sigaction *ka,
2959 unsigned long sp, size_t frame_size)
2961 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2962 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2965 return (sp - frame_size) & -8ul;
2968 static int setup_sigcontext(struct target_sigcontext *sc,
2969 CPUState *regs, unsigned long mask)
2971 int err = 0;
2972 int i;
2974 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2975 COPY(gregs[0]); COPY(gregs[1]);
2976 COPY(gregs[2]); COPY(gregs[3]);
2977 COPY(gregs[4]); COPY(gregs[5]);
2978 COPY(gregs[6]); COPY(gregs[7]);
2979 COPY(gregs[8]); COPY(gregs[9]);
2980 COPY(gregs[10]); COPY(gregs[11]);
2981 COPY(gregs[12]); COPY(gregs[13]);
2982 COPY(gregs[14]); COPY(gregs[15]);
2983 COPY(gbr); COPY(mach);
2984 COPY(macl); COPY(pr);
2985 COPY(sr); COPY(pc);
2986 #undef COPY
2988 for (i=0; i<16; i++) {
2989 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2991 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2992 err |= __put_user(regs->fpul, &sc->sc_fpul);
2994 /* non-iBCS2 extensions.. */
2995 err |= __put_user(mask, &sc->oldmask);
2997 return err;
3000 static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
3001 target_ulong *r0_p)
3003 unsigned int err = 0;
3004 int i;
3006 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3007 COPY(gregs[1]);
3008 COPY(gregs[2]); COPY(gregs[3]);
3009 COPY(gregs[4]); COPY(gregs[5]);
3010 COPY(gregs[6]); COPY(gregs[7]);
3011 COPY(gregs[8]); COPY(gregs[9]);
3012 COPY(gregs[10]); COPY(gregs[11]);
3013 COPY(gregs[12]); COPY(gregs[13]);
3014 COPY(gregs[14]); COPY(gregs[15]);
3015 COPY(gbr); COPY(mach);
3016 COPY(macl); COPY(pr);
3017 COPY(sr); COPY(pc);
3018 #undef COPY
3020 for (i=0; i<16; i++) {
3021 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3023 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3024 err |= __get_user(regs->fpul, &sc->sc_fpul);
3026 regs->tra = -1; /* disable syscall checks */
3027 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3028 return err;
3031 static void setup_frame(int sig, struct target_sigaction *ka,
3032 target_sigset_t *set, CPUState *regs)
3034 struct target_sigframe *frame;
3035 abi_ulong frame_addr;
3036 int i;
3037 int err = 0;
3038 int signal;
3040 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3041 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3042 goto give_sigsegv;
3044 signal = current_exec_domain_sig(sig);
3046 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3048 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3049 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3052 /* Set up to return from userspace. If provided, use a stub
3053 already in userspace. */
3054 if (ka->sa_flags & TARGET_SA_RESTORER) {
3055 regs->pr = (unsigned long) ka->sa_restorer;
3056 } else {
3057 /* Generate return code (system call to sigreturn) */
3058 err |= __put_user(MOVW(2), &frame->retcode[0]);
3059 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3060 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3061 regs->pr = (unsigned long) frame->retcode;
3064 if (err)
3065 goto give_sigsegv;
3067 /* Set up registers for signal handler */
3068 regs->gregs[15] = frame_addr;
3069 regs->gregs[4] = signal; /* Arg for signal handler */
3070 regs->gregs[5] = 0;
3071 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3072 regs->pc = (unsigned long) ka->_sa_handler;
3074 unlock_user_struct(frame, frame_addr, 1);
3075 return;
3077 give_sigsegv:
3078 unlock_user_struct(frame, frame_addr, 1);
3079 force_sig(TARGET_SIGSEGV);
3082 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3083 target_siginfo_t *info,
3084 target_sigset_t *set, CPUState *regs)
3086 struct target_rt_sigframe *frame;
3087 abi_ulong frame_addr;
3088 int i;
3089 int err = 0;
3090 int signal;
3092 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3093 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3094 goto give_sigsegv;
3096 signal = current_exec_domain_sig(sig);
3098 err |= copy_siginfo_to_user(&frame->info, info);
3100 /* Create the ucontext. */
3101 err |= __put_user(0, &frame->uc.tuc_flags);
3102 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3103 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3104 &frame->uc.tuc_stack.ss_sp);
3105 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3106 &frame->uc.tuc_stack.ss_flags);
3107 err |= __put_user(target_sigaltstack_used.ss_size,
3108 &frame->uc.tuc_stack.ss_size);
3109 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3110 regs, set->sig[0]);
3111 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3112 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3115 /* Set up to return from userspace. If provided, use a stub
3116 already in userspace. */
3117 if (ka->sa_flags & TARGET_SA_RESTORER) {
3118 regs->pr = (unsigned long) ka->sa_restorer;
3119 } else {
3120 /* Generate return code (system call to sigreturn) */
3121 err |= __put_user(MOVW(2), &frame->retcode[0]);
3122 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3123 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3124 regs->pr = (unsigned long) frame->retcode;
3127 if (err)
3128 goto give_sigsegv;
3130 /* Set up registers for signal handler */
3131 regs->gregs[15] = frame_addr;
3132 regs->gregs[4] = signal; /* Arg for signal handler */
3133 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3134 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3135 regs->pc = (unsigned long) ka->_sa_handler;
3137 unlock_user_struct(frame, frame_addr, 1);
3138 return;
3140 give_sigsegv:
3141 unlock_user_struct(frame, frame_addr, 1);
3142 force_sig(TARGET_SIGSEGV);
3145 long do_sigreturn(CPUState *regs)
3147 struct target_sigframe *frame;
3148 abi_ulong frame_addr;
3149 sigset_t blocked;
3150 target_sigset_t target_set;
3151 target_ulong r0;
3152 int i;
3153 int err = 0;
3155 #if defined(DEBUG_SIGNAL)
3156 fprintf(stderr, "do_sigreturn\n");
3157 #endif
3158 frame_addr = regs->gregs[15];
3159 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3160 goto badframe;
3162 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3163 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3164 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3167 if (err)
3168 goto badframe;
3170 target_to_host_sigset_internal(&blocked, &target_set);
3171 sigprocmask(SIG_SETMASK, &blocked, NULL);
3173 if (restore_sigcontext(regs, &frame->sc, &r0))
3174 goto badframe;
3176 unlock_user_struct(frame, frame_addr, 0);
3177 return r0;
3179 badframe:
3180 unlock_user_struct(frame, frame_addr, 0);
3181 force_sig(TARGET_SIGSEGV);
3182 return 0;
3185 long do_rt_sigreturn(CPUState *regs)
3187 struct target_rt_sigframe *frame;
3188 abi_ulong frame_addr;
3189 sigset_t blocked;
3190 target_ulong r0;
3192 #if defined(DEBUG_SIGNAL)
3193 fprintf(stderr, "do_rt_sigreturn\n");
3194 #endif
3195 frame_addr = regs->gregs[15];
3196 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3197 goto badframe;
3199 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3200 sigprocmask(SIG_SETMASK, &blocked, NULL);
3202 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3203 goto badframe;
3205 if (do_sigaltstack(frame_addr +
3206 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3207 0, get_sp_from_cpustate(regs)) == -EFAULT)
3208 goto badframe;
3210 unlock_user_struct(frame, frame_addr, 0);
3211 return r0;
3213 badframe:
3214 unlock_user_struct(frame, frame_addr, 0);
3215 force_sig(TARGET_SIGSEGV);
3216 return 0;
3218 #elif defined(TARGET_MICROBLAZE)
3220 struct target_sigcontext {
3221 struct target_pt_regs regs; /* needs to be first */
3222 uint32_t oldmask;
3225 struct target_stack_t {
3226 abi_ulong ss_sp;
3227 int ss_flags;
3228 unsigned int ss_size;
3231 struct target_ucontext {
3232 abi_ulong tuc_flags;
3233 abi_ulong tuc_link;
3234 struct target_stack_t tuc_stack;
3235 struct target_sigcontext tuc_mcontext;
3236 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3239 /* Signal frames. */
3240 struct target_signal_frame {
3241 struct target_ucontext uc;
3242 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3243 uint32_t tramp[2];
3246 struct rt_signal_frame {
3247 struct siginfo info;
3248 struct ucontext uc;
3249 uint32_t tramp[2];
3252 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3254 __put_user(env->regs[0], &sc->regs.r0);
3255 __put_user(env->regs[1], &sc->regs.r1);
3256 __put_user(env->regs[2], &sc->regs.r2);
3257 __put_user(env->regs[3], &sc->regs.r3);
3258 __put_user(env->regs[4], &sc->regs.r4);
3259 __put_user(env->regs[5], &sc->regs.r5);
3260 __put_user(env->regs[6], &sc->regs.r6);
3261 __put_user(env->regs[7], &sc->regs.r7);
3262 __put_user(env->regs[8], &sc->regs.r8);
3263 __put_user(env->regs[9], &sc->regs.r9);
3264 __put_user(env->regs[10], &sc->regs.r10);
3265 __put_user(env->regs[11], &sc->regs.r11);
3266 __put_user(env->regs[12], &sc->regs.r12);
3267 __put_user(env->regs[13], &sc->regs.r13);
3268 __put_user(env->regs[14], &sc->regs.r14);
3269 __put_user(env->regs[15], &sc->regs.r15);
3270 __put_user(env->regs[16], &sc->regs.r16);
3271 __put_user(env->regs[17], &sc->regs.r17);
3272 __put_user(env->regs[18], &sc->regs.r18);
3273 __put_user(env->regs[19], &sc->regs.r19);
3274 __put_user(env->regs[20], &sc->regs.r20);
3275 __put_user(env->regs[21], &sc->regs.r21);
3276 __put_user(env->regs[22], &sc->regs.r22);
3277 __put_user(env->regs[23], &sc->regs.r23);
3278 __put_user(env->regs[24], &sc->regs.r24);
3279 __put_user(env->regs[25], &sc->regs.r25);
3280 __put_user(env->regs[26], &sc->regs.r26);
3281 __put_user(env->regs[27], &sc->regs.r27);
3282 __put_user(env->regs[28], &sc->regs.r28);
3283 __put_user(env->regs[29], &sc->regs.r29);
3284 __put_user(env->regs[30], &sc->regs.r30);
3285 __put_user(env->regs[31], &sc->regs.r31);
3286 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3289 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3291 __get_user(env->regs[0], &sc->regs.r0);
3292 __get_user(env->regs[1], &sc->regs.r1);
3293 __get_user(env->regs[2], &sc->regs.r2);
3294 __get_user(env->regs[3], &sc->regs.r3);
3295 __get_user(env->regs[4], &sc->regs.r4);
3296 __get_user(env->regs[5], &sc->regs.r5);
3297 __get_user(env->regs[6], &sc->regs.r6);
3298 __get_user(env->regs[7], &sc->regs.r7);
3299 __get_user(env->regs[8], &sc->regs.r8);
3300 __get_user(env->regs[9], &sc->regs.r9);
3301 __get_user(env->regs[10], &sc->regs.r10);
3302 __get_user(env->regs[11], &sc->regs.r11);
3303 __get_user(env->regs[12], &sc->regs.r12);
3304 __get_user(env->regs[13], &sc->regs.r13);
3305 __get_user(env->regs[14], &sc->regs.r14);
3306 __get_user(env->regs[15], &sc->regs.r15);
3307 __get_user(env->regs[16], &sc->regs.r16);
3308 __get_user(env->regs[17], &sc->regs.r17);
3309 __get_user(env->regs[18], &sc->regs.r18);
3310 __get_user(env->regs[19], &sc->regs.r19);
3311 __get_user(env->regs[20], &sc->regs.r20);
3312 __get_user(env->regs[21], &sc->regs.r21);
3313 __get_user(env->regs[22], &sc->regs.r22);
3314 __get_user(env->regs[23], &sc->regs.r23);
3315 __get_user(env->regs[24], &sc->regs.r24);
3316 __get_user(env->regs[25], &sc->regs.r25);
3317 __get_user(env->regs[26], &sc->regs.r26);
3318 __get_user(env->regs[27], &sc->regs.r27);
3319 __get_user(env->regs[28], &sc->regs.r28);
3320 __get_user(env->regs[29], &sc->regs.r29);
3321 __get_user(env->regs[30], &sc->regs.r30);
3322 __get_user(env->regs[31], &sc->regs.r31);
3323 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3326 static abi_ulong get_sigframe(struct target_sigaction *ka,
3327 CPUState *env, int frame_size)
3329 abi_ulong sp = env->regs[1];
3331 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3332 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3334 return ((sp - frame_size) & -8UL);
3337 static void setup_frame(int sig, struct target_sigaction *ka,
3338 target_sigset_t *set, CPUState *env)
3340 struct target_signal_frame *frame;
3341 abi_ulong frame_addr;
3342 int err = 0;
3343 int i;
3345 frame_addr = get_sigframe(ka, env, sizeof *frame);
3346 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3347 goto badframe;
3349 /* Save the mask. */
3350 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3351 if (err)
3352 goto badframe;
3354 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3355 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3356 goto badframe;
3359 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3361 /* Set up to return from userspace. If provided, use a stub
3362 already in userspace. */
3363 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3364 if (ka->sa_flags & TARGET_SA_RESTORER) {
3365 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3366 } else {
3367 uint32_t t;
3368 /* Note, these encodings are _big endian_! */
3369 /* addi r12, r0, __NR_sigreturn */
3370 t = 0x31800000UL | TARGET_NR_sigreturn;
3371 err |= __put_user(t, frame->tramp + 0);
3372 /* brki r14, 0x8 */
3373 t = 0xb9cc0008UL;
3374 err |= __put_user(t, frame->tramp + 1);
3376 /* Return from sighandler will jump to the tramp.
3377 Negative 8 offset because return is rtsd r15, 8 */
3378 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3381 if (err)
3382 goto badframe;
3384 /* Set up registers for signal handler */
3385 env->regs[1] = frame_addr;
3386 /* Signal handler args: */
3387 env->regs[5] = sig; /* Arg 0: signum */
3388 env->regs[6] = 0;
3389 /* arg 1: sigcontext */
3390 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3392 /* Offset of 4 to handle microblaze rtid r14, 0 */
3393 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3395 unlock_user_struct(frame, frame_addr, 1);
3396 return;
3397 badframe:
3398 unlock_user_struct(frame, frame_addr, 1);
3399 force_sig(TARGET_SIGSEGV);
3402 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3403 target_siginfo_t *info,
3404 target_sigset_t *set, CPUState *env)
3406 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3409 long do_sigreturn(CPUState *env)
3411 struct target_signal_frame *frame;
3412 abi_ulong frame_addr;
3413 target_sigset_t target_set;
3414 sigset_t set;
3415 int i;
3417 frame_addr = env->regs[R_SP];
3418 /* Make sure the guest isn't playing games. */
3419 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3420 goto badframe;
3422 /* Restore blocked signals */
3423 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3424 goto badframe;
3425 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3426 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3427 goto badframe;
3429 target_to_host_sigset_internal(&set, &target_set);
3430 sigprocmask(SIG_SETMASK, &set, NULL);
3432 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3433 /* We got here through a sigreturn syscall, our path back is via an
3434 rtb insn so setup r14 for that. */
3435 env->regs[14] = env->sregs[SR_PC];
3437 unlock_user_struct(frame, frame_addr, 0);
3438 return env->regs[10];
3439 badframe:
3440 unlock_user_struct(frame, frame_addr, 0);
3441 force_sig(TARGET_SIGSEGV);
3444 long do_rt_sigreturn(CPUState *env)
3446 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3447 return -TARGET_ENOSYS;
3450 #elif defined(TARGET_CRIS)
3452 struct target_sigcontext {
3453 struct target_pt_regs regs; /* needs to be first */
3454 uint32_t oldmask;
3455 uint32_t usp; /* usp before stacking this gunk on it */
3458 /* Signal frames. */
3459 struct target_signal_frame {
3460 struct target_sigcontext sc;
3461 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3462 uint8_t retcode[8]; /* Trampoline code. */
3465 struct rt_signal_frame {
3466 struct siginfo *pinfo;
3467 void *puc;
3468 struct siginfo info;
3469 struct ucontext uc;
3470 uint8_t retcode[8]; /* Trampoline code. */
3473 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3475 __put_user(env->regs[0], &sc->regs.r0);
3476 __put_user(env->regs[1], &sc->regs.r1);
3477 __put_user(env->regs[2], &sc->regs.r2);
3478 __put_user(env->regs[3], &sc->regs.r3);
3479 __put_user(env->regs[4], &sc->regs.r4);
3480 __put_user(env->regs[5], &sc->regs.r5);
3481 __put_user(env->regs[6], &sc->regs.r6);
3482 __put_user(env->regs[7], &sc->regs.r7);
3483 __put_user(env->regs[8], &sc->regs.r8);
3484 __put_user(env->regs[9], &sc->regs.r9);
3485 __put_user(env->regs[10], &sc->regs.r10);
3486 __put_user(env->regs[11], &sc->regs.r11);
3487 __put_user(env->regs[12], &sc->regs.r12);
3488 __put_user(env->regs[13], &sc->regs.r13);
3489 __put_user(env->regs[14], &sc->usp);
3490 __put_user(env->regs[15], &sc->regs.acr);
3491 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3492 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3493 __put_user(env->pc, &sc->regs.erp);
3496 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3498 __get_user(env->regs[0], &sc->regs.r0);
3499 __get_user(env->regs[1], &sc->regs.r1);
3500 __get_user(env->regs[2], &sc->regs.r2);
3501 __get_user(env->regs[3], &sc->regs.r3);
3502 __get_user(env->regs[4], &sc->regs.r4);
3503 __get_user(env->regs[5], &sc->regs.r5);
3504 __get_user(env->regs[6], &sc->regs.r6);
3505 __get_user(env->regs[7], &sc->regs.r7);
3506 __get_user(env->regs[8], &sc->regs.r8);
3507 __get_user(env->regs[9], &sc->regs.r9);
3508 __get_user(env->regs[10], &sc->regs.r10);
3509 __get_user(env->regs[11], &sc->regs.r11);
3510 __get_user(env->regs[12], &sc->regs.r12);
3511 __get_user(env->regs[13], &sc->regs.r13);
3512 __get_user(env->regs[14], &sc->usp);
3513 __get_user(env->regs[15], &sc->regs.acr);
3514 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3515 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3516 __get_user(env->pc, &sc->regs.erp);
3519 static abi_ulong get_sigframe(CPUState *env, int framesize)
3521 abi_ulong sp;
3522 /* Align the stack downwards to 4. */
3523 sp = (env->regs[R_SP] & ~3);
3524 return sp - framesize;
3527 static void setup_frame(int sig, struct target_sigaction *ka,
3528 target_sigset_t *set, CPUState *env)
3530 struct target_signal_frame *frame;
3531 abi_ulong frame_addr;
3532 int err = 0;
3533 int i;
3535 frame_addr = get_sigframe(env, sizeof *frame);
3536 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3537 goto badframe;
3540 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3541 * use this trampoline anymore but it sets it up for GDB.
3542 * In QEMU, using the trampoline simplifies things a bit so we use it.
3544 * This is movu.w __NR_sigreturn, r9; break 13;
3546 err |= __put_user(0x9c5f, frame->retcode+0);
3547 err |= __put_user(TARGET_NR_sigreturn,
3548 frame->retcode+2);
3549 err |= __put_user(0xe93d, frame->retcode+4);
3551 /* Save the mask. */
3552 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3553 if (err)
3554 goto badframe;
3556 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3557 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3558 goto badframe;
3561 setup_sigcontext(&frame->sc, env);
3563 /* Move the stack and setup the arguments for the handler. */
3564 env->regs[R_SP] = frame_addr;
3565 env->regs[10] = sig;
3566 env->pc = (unsigned long) ka->_sa_handler;
3567 /* Link SRP so the guest returns through the trampoline. */
3568 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3570 unlock_user_struct(frame, frame_addr, 1);
3571 return;
3572 badframe:
3573 unlock_user_struct(frame, frame_addr, 1);
3574 force_sig(TARGET_SIGSEGV);
3577 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3578 target_siginfo_t *info,
3579 target_sigset_t *set, CPUState *env)
3581 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3584 long do_sigreturn(CPUState *env)
3586 struct target_signal_frame *frame;
3587 abi_ulong frame_addr;
3588 target_sigset_t target_set;
3589 sigset_t set;
3590 int i;
3592 frame_addr = env->regs[R_SP];
3593 /* Make sure the guest isn't playing games. */
3594 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3595 goto badframe;
3597 /* Restore blocked signals */
3598 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3599 goto badframe;
3600 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3601 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3602 goto badframe;
3604 target_to_host_sigset_internal(&set, &target_set);
3605 sigprocmask(SIG_SETMASK, &set, NULL);
3607 restore_sigcontext(&frame->sc, env);
3608 unlock_user_struct(frame, frame_addr, 0);
3609 return env->regs[10];
3610 badframe:
3611 unlock_user_struct(frame, frame_addr, 0);
3612 force_sig(TARGET_SIGSEGV);
3615 long do_rt_sigreturn(CPUState *env)
3617 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3618 return -TARGET_ENOSYS;
3621 #elif defined(TARGET_S390X)
3623 #define __NUM_GPRS 16
3624 #define __NUM_FPRS 16
3625 #define __NUM_ACRS 16
3627 #define S390_SYSCALL_SIZE 2
3628 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3630 #define _SIGCONTEXT_NSIG 64
3631 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3632 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3633 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3634 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
3635 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3637 typedef struct {
3638 target_psw_t psw;
3639 target_ulong gprs[__NUM_GPRS];
3640 unsigned int acrs[__NUM_ACRS];
3641 } target_s390_regs_common;
3643 typedef struct {
3644 unsigned int fpc;
3645 double fprs[__NUM_FPRS];
3646 } target_s390_fp_regs;
3648 typedef struct {
3649 target_s390_regs_common regs;
3650 target_s390_fp_regs fpregs;
3651 } target_sigregs;
3653 struct target_sigcontext {
3654 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
3655 target_sigregs *sregs;
3658 typedef struct {
3659 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3660 struct target_sigcontext sc;
3661 target_sigregs sregs;
3662 int signo;
3663 uint8_t retcode[S390_SYSCALL_SIZE];
3664 } sigframe;
3666 struct target_ucontext {
3667 target_ulong tuc_flags;
3668 struct target_ucontext *tuc_link;
3669 target_stack_t tuc_stack;
3670 target_sigregs tuc_mcontext;
3671 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3674 typedef struct {
3675 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3676 uint8_t retcode[S390_SYSCALL_SIZE];
3677 struct target_siginfo info;
3678 struct target_ucontext uc;
3679 } rt_sigframe;
3681 static inline abi_ulong
3682 get_sigframe(struct target_sigaction *ka, CPUState *env, size_t frame_size)
3684 abi_ulong sp;
3686 /* Default to using normal stack */
3687 sp = env->regs[15];
3689 /* This is the X/Open sanctioned signal stack switching. */
3690 if (ka->sa_flags & TARGET_SA_ONSTACK) {
3691 if (!sas_ss_flags(sp)) {
3692 sp = target_sigaltstack_used.ss_sp +
3693 target_sigaltstack_used.ss_size;
3697 /* This is the legacy signal stack switching. */
3698 else if (/* FIXME !user_mode(regs) */ 0 &&
3699 !(ka->sa_flags & TARGET_SA_RESTORER) &&
3700 ka->sa_restorer) {
3701 sp = (abi_ulong) ka->sa_restorer;
3704 return (sp - frame_size) & -8ul;
3707 static void save_sigregs(CPUState *env, target_sigregs *sregs)
3709 int i;
3710 //save_access_regs(current->thread.acrs); FIXME
3712 /* Copy a 'clean' PSW mask to the user to avoid leaking
3713 information about whether PER is currently on. */
3714 __put_user(env->psw.mask, &sregs->regs.psw.mask);
3715 __put_user(env->psw.addr, &sregs->regs.psw.addr);
3716 for (i = 0; i < 16; i++) {
3717 __put_user(env->regs[i], &sregs->regs.gprs[i]);
3719 for (i = 0; i < 16; i++) {
3720 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3723 * We have to store the fp registers to current->thread.fp_regs
3724 * to merge them with the emulated registers.
3726 //save_fp_regs(&current->thread.fp_regs); FIXME
3727 for (i = 0; i < 16; i++) {
3728 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3732 static void setup_frame(int sig, struct target_sigaction *ka,
3733 target_sigset_t *set, CPUState *env)
3735 sigframe *frame;
3736 abi_ulong frame_addr;
3738 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3739 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3740 (unsigned long long)frame_addr);
3741 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3742 goto give_sigsegv;
3745 qemu_log("%s: 1\n", __FUNCTION__);
3746 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
3747 goto give_sigsegv;
3750 save_sigregs(env, &frame->sregs);
3752 __put_user((abi_ulong)(unsigned long)&frame->sregs,
3753 (abi_ulong *)&frame->sc.sregs);
3755 /* Set up to return from userspace. If provided, use a stub
3756 already in userspace. */
3757 if (ka->sa_flags & TARGET_SA_RESTORER) {
3758 env->regs[14] = (unsigned long)
3759 ka->sa_restorer | PSW_ADDR_AMODE;
3760 } else {
3761 env->regs[14] = (unsigned long)
3762 frame->retcode | PSW_ADDR_AMODE;
3763 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
3764 (uint16_t *)(frame->retcode)))
3765 goto give_sigsegv;
3768 /* Set up backchain. */
3769 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3770 goto give_sigsegv;
3773 /* Set up registers for signal handler */
3774 env->regs[15] = frame_addr;
3775 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3777 env->regs[2] = sig; //map_signal(sig);
3778 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
3780 /* We forgot to include these in the sigcontext.
3781 To avoid breaking binary compatibility, they are passed as args. */
3782 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
3783 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
3785 /* Place signal number on stack to allow backtrace from handler. */
3786 if (__put_user(env->regs[2], (int *) &frame->signo)) {
3787 goto give_sigsegv;
3789 unlock_user_struct(frame, frame_addr, 1);
3790 return;
3792 give_sigsegv:
3793 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3794 unlock_user_struct(frame, frame_addr, 1);
3795 force_sig(TARGET_SIGSEGV);
3798 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3799 target_siginfo_t *info,
3800 target_sigset_t *set, CPUState *env)
3802 int i;
3803 rt_sigframe *frame;
3804 abi_ulong frame_addr;
3806 frame_addr = get_sigframe(ka, env, sizeof *frame);
3807 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3808 (unsigned long long)frame_addr);
3809 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3810 goto give_sigsegv;
3813 qemu_log("%s: 1\n", __FUNCTION__);
3814 if (copy_siginfo_to_user(&frame->info, info)) {
3815 goto give_sigsegv;
3818 /* Create the ucontext. */
3819 __put_user(0, &frame->uc.tuc_flags);
3820 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
3821 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
3822 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3823 &frame->uc.tuc_stack.ss_flags);
3824 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
3825 save_sigregs(env, &frame->uc.tuc_mcontext);
3826 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
3827 __put_user((abi_ulong)set->sig[i],
3828 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
3831 /* Set up to return from userspace. If provided, use a stub
3832 already in userspace. */
3833 if (ka->sa_flags & TARGET_SA_RESTORER) {
3834 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
3835 } else {
3836 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
3837 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
3838 (uint16_t *)(frame->retcode))) {
3839 goto give_sigsegv;
3843 /* Set up backchain. */
3844 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3845 goto give_sigsegv;
3848 /* Set up registers for signal handler */
3849 env->regs[15] = frame_addr;
3850 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3852 env->regs[2] = sig; //map_signal(sig);
3853 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
3854 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
3855 return;
3857 give_sigsegv:
3858 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3859 unlock_user_struct(frame, frame_addr, 1);
3860 force_sig(TARGET_SIGSEGV);
3863 static int
3864 restore_sigregs(CPUState *env, target_sigregs *sc)
3866 int err = 0;
3867 int i;
3869 for (i = 0; i < 16; i++) {
3870 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
3873 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
3874 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
3875 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
3876 (unsigned long long)env->psw.addr);
3877 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
3878 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
3880 for (i = 0; i < 16; i++) {
3881 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
3883 for (i = 0; i < 16; i++) {
3884 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
3887 return err;
3890 long do_sigreturn(CPUState *env)
3892 sigframe *frame;
3893 abi_ulong frame_addr = env->regs[15];
3894 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3895 (unsigned long long)frame_addr);
3896 target_sigset_t target_set;
3897 sigset_t set;
3899 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3900 goto badframe;
3902 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
3903 goto badframe;
3906 target_to_host_sigset_internal(&set, &target_set);
3907 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
3909 if (restore_sigregs(env, &frame->sregs)) {
3910 goto badframe;
3913 unlock_user_struct(frame, frame_addr, 0);
3914 return env->regs[2];
3916 badframe:
3917 unlock_user_struct(frame, frame_addr, 0);
3918 force_sig(TARGET_SIGSEGV);
3919 return 0;
3922 long do_rt_sigreturn(CPUState *env)
3924 rt_sigframe *frame;
3925 abi_ulong frame_addr = env->regs[15];
3926 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3927 (unsigned long long)frame_addr);
3928 sigset_t set;
3930 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3931 goto badframe;
3933 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
3935 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
3937 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
3938 goto badframe;
3941 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
3942 get_sp_from_cpustate(env)) == -EFAULT) {
3943 goto badframe;
3945 unlock_user_struct(frame, frame_addr, 0);
3946 return env->regs[2];
3948 badframe:
3949 unlock_user_struct(frame, frame_addr, 0);
3950 force_sig(TARGET_SIGSEGV);
3951 return 0;
3954 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3956 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
3957 the signal handling is different enough that we haven't implemented
3958 support for PPC64 yet. Hence the restriction above.
3960 There are various #if'd blocks for code for TARGET_PPC64. These
3961 blocks should go away so that we can successfully run 32-bit and
3962 64-bit binaries on a QEMU configured for PPC64. */
3964 /* Size of dummy stack frame allocated when calling signal handler.
3965 See arch/powerpc/include/asm/ptrace.h. */
3966 #if defined(TARGET_PPC64)
3967 #define SIGNAL_FRAMESIZE 128
3968 #else
3969 #define SIGNAL_FRAMESIZE 64
3970 #endif
3972 /* See arch/powerpc/include/asm/sigcontext.h. */
3973 struct target_sigcontext {
3974 target_ulong _unused[4];
3975 int32_t signal;
3976 #if defined(TARGET_PPC64)
3977 int32_t pad0;
3978 #endif
3979 target_ulong handler;
3980 target_ulong oldmask;
3981 target_ulong regs; /* struct pt_regs __user * */
3982 /* TODO: PPC64 includes extra bits here. */
3985 /* Indices for target_mcontext.mc_gregs, below.
3986 See arch/powerpc/include/asm/ptrace.h for details. */
3987 enum {
3988 TARGET_PT_R0 = 0,
3989 TARGET_PT_R1 = 1,
3990 TARGET_PT_R2 = 2,
3991 TARGET_PT_R3 = 3,
3992 TARGET_PT_R4 = 4,
3993 TARGET_PT_R5 = 5,
3994 TARGET_PT_R6 = 6,
3995 TARGET_PT_R7 = 7,
3996 TARGET_PT_R8 = 8,
3997 TARGET_PT_R9 = 9,
3998 TARGET_PT_R10 = 10,
3999 TARGET_PT_R11 = 11,
4000 TARGET_PT_R12 = 12,
4001 TARGET_PT_R13 = 13,
4002 TARGET_PT_R14 = 14,
4003 TARGET_PT_R15 = 15,
4004 TARGET_PT_R16 = 16,
4005 TARGET_PT_R17 = 17,
4006 TARGET_PT_R18 = 18,
4007 TARGET_PT_R19 = 19,
4008 TARGET_PT_R20 = 20,
4009 TARGET_PT_R21 = 21,
4010 TARGET_PT_R22 = 22,
4011 TARGET_PT_R23 = 23,
4012 TARGET_PT_R24 = 24,
4013 TARGET_PT_R25 = 25,
4014 TARGET_PT_R26 = 26,
4015 TARGET_PT_R27 = 27,
4016 TARGET_PT_R28 = 28,
4017 TARGET_PT_R29 = 29,
4018 TARGET_PT_R30 = 30,
4019 TARGET_PT_R31 = 31,
4020 TARGET_PT_NIP = 32,
4021 TARGET_PT_MSR = 33,
4022 TARGET_PT_ORIG_R3 = 34,
4023 TARGET_PT_CTR = 35,
4024 TARGET_PT_LNK = 36,
4025 TARGET_PT_XER = 37,
4026 TARGET_PT_CCR = 38,
4027 /* Yes, there are two registers with #39. One is 64-bit only. */
4028 TARGET_PT_MQ = 39,
4029 TARGET_PT_SOFTE = 39,
4030 TARGET_PT_TRAP = 40,
4031 TARGET_PT_DAR = 41,
4032 TARGET_PT_DSISR = 42,
4033 TARGET_PT_RESULT = 43,
4034 TARGET_PT_REGS_COUNT = 44
4037 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4038 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4039 struct target_mcontext {
4040 target_ulong mc_gregs[48];
4041 /* Includes fpscr. */
4042 uint64_t mc_fregs[33];
4043 target_ulong mc_pad[2];
4044 /* We need to handle Altivec and SPE at the same time, which no
4045 kernel needs to do. Fortunately, the kernel defines this bit to
4046 be Altivec-register-large all the time, rather than trying to
4047 twiddle it based on the specific platform. */
4048 union {
4049 /* SPE vector registers. One extra for SPEFSCR. */
4050 uint32_t spe[33];
4051 /* Altivec vector registers. The packing of VSCR and VRSAVE
4052 varies depending on whether we're PPC64 or not: PPC64 splits
4053 them apart; PPC32 stuffs them together. */
4054 #if defined(TARGET_PPC64)
4055 #define QEMU_NVRREG 34
4056 #else
4057 #define QEMU_NVRREG 33
4058 #endif
4059 ppc_avr_t altivec[QEMU_NVRREG];
4060 #undef QEMU_NVRREG
4061 } mc_vregs __attribute__((__aligned__(16)));
4064 struct target_ucontext {
4065 target_ulong tuc_flags;
4066 target_ulong tuc_link; /* struct ucontext __user * */
4067 struct target_sigaltstack tuc_stack;
4068 #if !defined(TARGET_PPC64)
4069 int32_t tuc_pad[7];
4070 target_ulong tuc_regs; /* struct mcontext __user *
4071 points to uc_mcontext field */
4072 #endif
4073 target_sigset_t tuc_sigmask;
4074 #if defined(TARGET_PPC64)
4075 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4076 struct target_sigcontext tuc_mcontext;
4077 #else
4078 int32_t tuc_maskext[30];
4079 int32_t tuc_pad2[3];
4080 struct target_mcontext tuc_mcontext;
4081 #endif
4084 /* See arch/powerpc/kernel/signal_32.c. */
4085 struct target_sigframe {
4086 struct target_sigcontext sctx;
4087 struct target_mcontext mctx;
4088 int32_t abigap[56];
4091 struct target_rt_sigframe {
4092 struct target_siginfo info;
4093 struct target_ucontext uc;
4094 int32_t abigap[56];
4097 /* We use the mc_pad field for the signal return trampoline. */
4098 #define tramp mc_pad
4100 /* See arch/powerpc/kernel/signal.c. */
4101 static target_ulong get_sigframe(struct target_sigaction *ka,
4102 CPUState *env,
4103 int frame_size)
4105 target_ulong oldsp, newsp;
4107 oldsp = env->gpr[1];
4109 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4110 (sas_ss_flags(oldsp))) {
4111 oldsp = (target_sigaltstack_used.ss_sp
4112 + target_sigaltstack_used.ss_size);
4115 newsp = (oldsp - frame_size) & ~0xFUL;
4117 return newsp;
4120 static int save_user_regs(CPUState *env, struct target_mcontext *frame,
4121 int sigret)
4123 target_ulong msr = env->msr;
4124 int i;
4125 target_ulong ccr = 0;
4127 /* In general, the kernel attempts to be intelligent about what it
4128 needs to save for Altivec/FP/SPE registers. We don't care that
4129 much, so we just go ahead and save everything. */
4131 /* Save general registers. */
4132 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4133 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4134 return 1;
4137 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4138 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4139 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4140 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4141 return 1;
4143 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4144 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4146 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4147 return 1;
4149 /* Save Altivec registers if necessary. */
4150 if (env->insns_flags & PPC_ALTIVEC) {
4151 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4152 ppc_avr_t *avr = &env->avr[i];
4153 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4155 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4156 __put_user(avr->u64[1], &vreg->u64[1])) {
4157 return 1;
4160 /* Set MSR_VR in the saved MSR value to indicate that
4161 frame->mc_vregs contains valid data. */
4162 msr |= MSR_VR;
4163 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4164 &frame->mc_vregs.altivec[32].u32[3]))
4165 return 1;
4168 /* Save floating point registers. */
4169 if (env->insns_flags & PPC_FLOAT) {
4170 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4171 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4172 return 1;
4175 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4176 return 1;
4179 /* Save SPE registers. The kernel only saves the high half. */
4180 if (env->insns_flags & PPC_SPE) {
4181 #if defined(TARGET_PPC64)
4182 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4183 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4184 return 1;
4187 #else
4188 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4189 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4190 return 1;
4193 #endif
4194 /* Set MSR_SPE in the saved MSR value to indicate that
4195 frame->mc_vregs contains valid data. */
4196 msr |= MSR_SPE;
4197 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4198 return 1;
4201 /* Store MSR. */
4202 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4203 return 1;
4205 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4206 if (sigret) {
4207 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4208 __put_user(0x44000002UL, &frame->tramp[1])) {
4209 return 1;
4213 return 0;
4216 static int restore_user_regs(CPUState *env,
4217 struct target_mcontext *frame, int sig)
4219 target_ulong save_r2 = 0;
4220 target_ulong msr;
4221 target_ulong ccr;
4223 int i;
4225 if (!sig) {
4226 save_r2 = env->gpr[2];
4229 /* Restore general registers. */
4230 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4231 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4232 return 1;
4235 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4236 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4237 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4238 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4239 return 1;
4240 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4241 return 1;
4243 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4244 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4247 if (!sig) {
4248 env->gpr[2] = save_r2;
4250 /* Restore MSR. */
4251 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4252 return 1;
4254 /* If doing signal return, restore the previous little-endian mode. */
4255 if (sig)
4256 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4258 /* Restore Altivec registers if necessary. */
4259 if (env->insns_flags & PPC_ALTIVEC) {
4260 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4261 ppc_avr_t *avr = &env->avr[i];
4262 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4264 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4265 __get_user(avr->u64[1], &vreg->u64[1])) {
4266 return 1;
4269 /* Set MSR_VEC in the saved MSR value to indicate that
4270 frame->mc_vregs contains valid data. */
4271 if (__get_user(env->spr[SPR_VRSAVE],
4272 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4273 return 1;
4276 /* Restore floating point registers. */
4277 if (env->insns_flags & PPC_FLOAT) {
4278 uint64_t fpscr;
4279 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4280 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4281 return 1;
4284 if (__get_user(fpscr, &frame->mc_fregs[32]))
4285 return 1;
4286 env->fpscr = (uint32_t) fpscr;
4289 /* Save SPE registers. The kernel only saves the high half. */
4290 if (env->insns_flags & PPC_SPE) {
4291 #if defined(TARGET_PPC64)
4292 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4293 uint32_t hi;
4295 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4296 return 1;
4298 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4300 #else
4301 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4302 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4303 return 1;
4306 #endif
4307 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4308 return 1;
4311 return 0;
4314 static void setup_frame(int sig, struct target_sigaction *ka,
4315 target_sigset_t *set, CPUState *env)
4317 struct target_sigframe *frame;
4318 struct target_sigcontext *sc;
4319 target_ulong frame_addr, newsp;
4320 int err = 0;
4321 int signal;
4323 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4324 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4325 goto sigsegv;
4326 sc = &frame->sctx;
4328 signal = current_exec_domain_sig(sig);
4330 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
4331 err |= __put_user(set->sig[0], &sc->oldmask);
4332 #if defined(TARGET_PPC64)
4333 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4334 #else
4335 err |= __put_user(set->sig[1], &sc->_unused[3]);
4336 #endif
4337 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4338 err |= __put_user(sig, &sc->signal);
4340 /* Save user regs. */
4341 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4343 /* The kernel checks for the presence of a VDSO here. We don't
4344 emulate a vdso, so use a sigreturn system call. */
4345 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4347 /* Turn off all fp exceptions. */
4348 env->fpscr = 0;
4350 /* Create a stack frame for the caller of the handler. */
4351 newsp = frame_addr - SIGNAL_FRAMESIZE;
4352 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4354 if (err)
4355 goto sigsegv;
4357 /* Set up registers for signal handler. */
4358 env->gpr[1] = newsp;
4359 env->gpr[3] = signal;
4360 env->gpr[4] = (target_ulong) h2g(sc);
4361 env->nip = (target_ulong) ka->_sa_handler;
4362 /* Signal handlers are entered in big-endian mode. */
4363 env->msr &= ~MSR_LE;
4365 unlock_user_struct(frame, frame_addr, 1);
4366 return;
4368 sigsegv:
4369 unlock_user_struct(frame, frame_addr, 1);
4370 if (logfile)
4371 fprintf (logfile, "segfaulting from setup_frame\n");
4372 force_sig(TARGET_SIGSEGV);
4375 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4376 target_siginfo_t *info,
4377 target_sigset_t *set, CPUState *env)
4379 struct target_rt_sigframe *rt_sf;
4380 struct target_mcontext *frame;
4381 target_ulong rt_sf_addr, newsp = 0;
4382 int i, err = 0;
4383 int signal;
4385 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4386 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4387 goto sigsegv;
4389 signal = current_exec_domain_sig(sig);
4391 err |= copy_siginfo_to_user(&rt_sf->info, info);
4393 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4394 err |= __put_user(0, &rt_sf->uc.tuc_link);
4395 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4396 &rt_sf->uc.tuc_stack.ss_sp);
4397 err |= __put_user(sas_ss_flags(env->gpr[1]),
4398 &rt_sf->uc.tuc_stack.ss_flags);
4399 err |= __put_user(target_sigaltstack_used.ss_size,
4400 &rt_sf->uc.tuc_stack.ss_size);
4401 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4402 &rt_sf->uc.tuc_regs);
4403 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4404 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4407 frame = &rt_sf->uc.tuc_mcontext;
4408 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4410 /* The kernel checks for the presence of a VDSO here. We don't
4411 emulate a vdso, so use a sigreturn system call. */
4412 env->lr = (target_ulong) h2g(frame->tramp);
4414 /* Turn off all fp exceptions. */
4415 env->fpscr = 0;
4417 /* Create a stack frame for the caller of the handler. */
4418 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4419 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4421 if (err)
4422 goto sigsegv;
4424 /* Set up registers for signal handler. */
4425 env->gpr[1] = newsp;
4426 env->gpr[3] = (target_ulong) signal;
4427 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4428 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4429 env->gpr[6] = (target_ulong) h2g(rt_sf);
4430 env->nip = (target_ulong) ka->_sa_handler;
4431 /* Signal handlers are entered in big-endian mode. */
4432 env->msr &= ~MSR_LE;
4434 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4435 return;
4437 sigsegv:
4438 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4439 if (logfile)
4440 fprintf (logfile, "segfaulting from setup_rt_frame\n");
4441 force_sig(TARGET_SIGSEGV);
4445 long do_sigreturn(CPUState *env)
4447 struct target_sigcontext *sc = NULL;
4448 struct target_mcontext *sr = NULL;
4449 target_ulong sr_addr, sc_addr;
4450 sigset_t blocked;
4451 target_sigset_t set;
4453 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4454 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4455 goto sigsegv;
4457 #if defined(TARGET_PPC64)
4458 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4459 #else
4460 if(__get_user(set.sig[0], &sc->oldmask) ||
4461 __get_user(set.sig[1], &sc->_unused[3]))
4462 goto sigsegv;
4463 #endif
4464 target_to_host_sigset_internal(&blocked, &set);
4465 sigprocmask(SIG_SETMASK, &blocked, NULL);
4467 if (__get_user(sr_addr, &sc->regs))
4468 goto sigsegv;
4469 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4470 goto sigsegv;
4471 if (restore_user_regs(env, sr, 1))
4472 goto sigsegv;
4474 unlock_user_struct(sr, sr_addr, 1);
4475 unlock_user_struct(sc, sc_addr, 1);
4476 return -TARGET_QEMU_ESIGRETURN;
4478 sigsegv:
4479 unlock_user_struct(sr, sr_addr, 1);
4480 unlock_user_struct(sc, sc_addr, 1);
4481 if (logfile)
4482 fprintf (logfile, "segfaulting from do_sigreturn\n");
4483 force_sig(TARGET_SIGSEGV);
4484 return 0;
4487 /* See arch/powerpc/kernel/signal_32.c. */
4488 static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
4490 struct target_mcontext *mcp;
4491 target_ulong mcp_addr;
4492 sigset_t blocked;
4493 target_sigset_t set;
4495 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4496 sizeof (set)))
4497 return 1;
4499 #if defined(TARGET_PPC64)
4500 fprintf (stderr, "do_setcontext: not implemented\n");
4501 return 0;
4502 #else
4503 if (__get_user(mcp_addr, &ucp->tuc_regs))
4504 return 1;
4506 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4507 return 1;
4509 target_to_host_sigset_internal(&blocked, &set);
4510 sigprocmask(SIG_SETMASK, &blocked, NULL);
4511 if (restore_user_regs(env, mcp, sig))
4512 goto sigsegv;
4514 unlock_user_struct(mcp, mcp_addr, 1);
4515 return 0;
4517 sigsegv:
4518 unlock_user_struct(mcp, mcp_addr, 1);
4519 return 1;
4520 #endif
4523 long do_rt_sigreturn(CPUState *env)
4525 struct target_rt_sigframe *rt_sf = NULL;
4526 target_ulong rt_sf_addr;
4528 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4529 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4530 goto sigsegv;
4532 if (do_setcontext(&rt_sf->uc, env, 1))
4533 goto sigsegv;
4535 do_sigaltstack(rt_sf_addr
4536 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4537 0, env->gpr[1]);
4539 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4540 return -TARGET_QEMU_ESIGRETURN;
4542 sigsegv:
4543 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4544 if (logfile)
4545 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
4546 force_sig(TARGET_SIGSEGV);
4547 return 0;
4550 #elif defined(TARGET_M68K)
4552 struct target_sigcontext {
4553 abi_ulong sc_mask;
4554 abi_ulong sc_usp;
4555 abi_ulong sc_d0;
4556 abi_ulong sc_d1;
4557 abi_ulong sc_a0;
4558 abi_ulong sc_a1;
4559 unsigned short sc_sr;
4560 abi_ulong sc_pc;
4563 struct target_sigframe
4565 abi_ulong pretcode;
4566 int sig;
4567 int code;
4568 abi_ulong psc;
4569 char retcode[8];
4570 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4571 struct target_sigcontext sc;
4574 typedef int target_greg_t;
4575 #define TARGET_NGREG 18
4576 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4578 typedef struct target_fpregset {
4579 int f_fpcntl[3];
4580 int f_fpregs[8*3];
4581 } target_fpregset_t;
4583 struct target_mcontext {
4584 int version;
4585 target_gregset_t gregs;
4586 target_fpregset_t fpregs;
4589 #define TARGET_MCONTEXT_VERSION 2
4591 struct target_ucontext {
4592 abi_ulong tuc_flags;
4593 abi_ulong tuc_link;
4594 target_stack_t tuc_stack;
4595 struct target_mcontext tuc_mcontext;
4596 abi_long tuc_filler[80];
4597 target_sigset_t tuc_sigmask;
4600 struct target_rt_sigframe
4602 abi_ulong pretcode;
4603 int sig;
4604 abi_ulong pinfo;
4605 abi_ulong puc;
4606 char retcode[8];
4607 struct target_siginfo info;
4608 struct target_ucontext uc;
4611 static int
4612 setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4614 int err = 0;
4616 err |= __put_user(mask, &sc->sc_mask);
4617 err |= __put_user(env->aregs[7], &sc->sc_usp);
4618 err |= __put_user(env->dregs[0], &sc->sc_d0);
4619 err |= __put_user(env->dregs[1], &sc->sc_d1);
4620 err |= __put_user(env->aregs[0], &sc->sc_a0);
4621 err |= __put_user(env->aregs[1], &sc->sc_a1);
4622 err |= __put_user(env->sr, &sc->sc_sr);
4623 err |= __put_user(env->pc, &sc->sc_pc);
4625 return err;
4628 static int
4629 restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4631 int err = 0;
4632 int temp;
4634 err |= __get_user(env->aregs[7], &sc->sc_usp);
4635 err |= __get_user(env->dregs[1], &sc->sc_d1);
4636 err |= __get_user(env->aregs[0], &sc->sc_a0);
4637 err |= __get_user(env->aregs[1], &sc->sc_a1);
4638 err |= __get_user(env->pc, &sc->sc_pc);
4639 err |= __get_user(temp, &sc->sc_sr);
4640 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4642 *pd0 = tswapl(sc->sc_d0);
4644 return err;
4648 * Determine which stack to use..
4650 static inline abi_ulong
4651 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4653 unsigned long sp;
4655 sp = regs->aregs[7];
4657 /* This is the X/Open sanctioned signal stack switching. */
4658 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4659 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4662 return ((sp - frame_size) & -8UL);
4665 static void setup_frame(int sig, struct target_sigaction *ka,
4666 target_sigset_t *set, CPUState *env)
4668 struct target_sigframe *frame;
4669 abi_ulong frame_addr;
4670 abi_ulong retcode_addr;
4671 abi_ulong sc_addr;
4672 int err = 0;
4673 int i;
4675 frame_addr = get_sigframe(ka, env, sizeof *frame);
4676 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4677 goto give_sigsegv;
4679 err |= __put_user(sig, &frame->sig);
4681 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4682 err |= __put_user(sc_addr, &frame->psc);
4684 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4685 if (err)
4686 goto give_sigsegv;
4688 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4689 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4690 goto give_sigsegv;
4693 /* Set up to return from userspace. */
4695 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4696 err |= __put_user(retcode_addr, &frame->pretcode);
4698 /* moveq #,d0; trap #0 */
4700 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4701 (long *)(frame->retcode));
4703 if (err)
4704 goto give_sigsegv;
4706 /* Set up to return from userspace */
4708 env->aregs[7] = frame_addr;
4709 env->pc = ka->_sa_handler;
4711 unlock_user_struct(frame, frame_addr, 1);
4712 return;
4714 give_sigsegv:
4715 unlock_user_struct(frame, frame_addr, 1);
4716 force_sig(TARGET_SIGSEGV);
4719 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4720 CPUState *env)
4722 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4723 int err;
4725 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4726 err |= __put_user(env->dregs[0], &gregs[0]);
4727 err |= __put_user(env->dregs[1], &gregs[1]);
4728 err |= __put_user(env->dregs[2], &gregs[2]);
4729 err |= __put_user(env->dregs[3], &gregs[3]);
4730 err |= __put_user(env->dregs[4], &gregs[4]);
4731 err |= __put_user(env->dregs[5], &gregs[5]);
4732 err |= __put_user(env->dregs[6], &gregs[6]);
4733 err |= __put_user(env->dregs[7], &gregs[7]);
4734 err |= __put_user(env->aregs[0], &gregs[8]);
4735 err |= __put_user(env->aregs[1], &gregs[9]);
4736 err |= __put_user(env->aregs[2], &gregs[10]);
4737 err |= __put_user(env->aregs[3], &gregs[11]);
4738 err |= __put_user(env->aregs[4], &gregs[12]);
4739 err |= __put_user(env->aregs[5], &gregs[13]);
4740 err |= __put_user(env->aregs[6], &gregs[14]);
4741 err |= __put_user(env->aregs[7], &gregs[15]);
4742 err |= __put_user(env->pc, &gregs[16]);
4743 err |= __put_user(env->sr, &gregs[17]);
4745 return err;
4748 static inline int target_rt_restore_ucontext(CPUState *env,
4749 struct target_ucontext *uc,
4750 int *pd0)
4752 int temp;
4753 int err;
4754 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4756 err = __get_user(temp, &uc->tuc_mcontext.version);
4757 if (temp != TARGET_MCONTEXT_VERSION)
4758 goto badframe;
4760 /* restore passed registers */
4761 err |= __get_user(env->dregs[0], &gregs[0]);
4762 err |= __get_user(env->dregs[1], &gregs[1]);
4763 err |= __get_user(env->dregs[2], &gregs[2]);
4764 err |= __get_user(env->dregs[3], &gregs[3]);
4765 err |= __get_user(env->dregs[4], &gregs[4]);
4766 err |= __get_user(env->dregs[5], &gregs[5]);
4767 err |= __get_user(env->dregs[6], &gregs[6]);
4768 err |= __get_user(env->dregs[7], &gregs[7]);
4769 err |= __get_user(env->aregs[0], &gregs[8]);
4770 err |= __get_user(env->aregs[1], &gregs[9]);
4771 err |= __get_user(env->aregs[2], &gregs[10]);
4772 err |= __get_user(env->aregs[3], &gregs[11]);
4773 err |= __get_user(env->aregs[4], &gregs[12]);
4774 err |= __get_user(env->aregs[5], &gregs[13]);
4775 err |= __get_user(env->aregs[6], &gregs[14]);
4776 err |= __get_user(env->aregs[7], &gregs[15]);
4777 err |= __get_user(env->pc, &gregs[16]);
4778 err |= __get_user(temp, &gregs[17]);
4779 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4781 *pd0 = env->dregs[0];
4782 return err;
4784 badframe:
4785 return 1;
4788 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4789 target_siginfo_t *info,
4790 target_sigset_t *set, CPUState *env)
4792 struct target_rt_sigframe *frame;
4793 abi_ulong frame_addr;
4794 abi_ulong retcode_addr;
4795 abi_ulong info_addr;
4796 abi_ulong uc_addr;
4797 int err = 0;
4798 int i;
4800 frame_addr = get_sigframe(ka, env, sizeof *frame);
4801 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4802 goto give_sigsegv;
4804 err |= __put_user(sig, &frame->sig);
4806 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4807 err |= __put_user(info_addr, &frame->pinfo);
4809 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4810 err |= __put_user(uc_addr, &frame->puc);
4812 err |= copy_siginfo_to_user(&frame->info, info);
4814 /* Create the ucontext */
4816 err |= __put_user(0, &frame->uc.tuc_flags);
4817 err |= __put_user(0, &frame->uc.tuc_link);
4818 err |= __put_user(target_sigaltstack_used.ss_sp,
4819 &frame->uc.tuc_stack.ss_sp);
4820 err |= __put_user(sas_ss_flags(env->aregs[7]),
4821 &frame->uc.tuc_stack.ss_flags);
4822 err |= __put_user(target_sigaltstack_used.ss_size,
4823 &frame->uc.tuc_stack.ss_size);
4824 err |= target_rt_setup_ucontext(&frame->uc, env);
4826 if (err)
4827 goto give_sigsegv;
4829 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4830 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
4831 goto give_sigsegv;
4834 /* Set up to return from userspace. */
4836 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4837 err |= __put_user(retcode_addr, &frame->pretcode);
4839 /* moveq #,d0; notb d0; trap #0 */
4841 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4842 (long *)(frame->retcode + 0));
4843 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4845 if (err)
4846 goto give_sigsegv;
4848 /* Set up to return from userspace */
4850 env->aregs[7] = frame_addr;
4851 env->pc = ka->_sa_handler;
4853 unlock_user_struct(frame, frame_addr, 1);
4854 return;
4856 give_sigsegv:
4857 unlock_user_struct(frame, frame_addr, 1);
4858 force_sig(TARGET_SIGSEGV);
4861 long do_sigreturn(CPUState *env)
4863 struct target_sigframe *frame;
4864 abi_ulong frame_addr = env->aregs[7] - 4;
4865 target_sigset_t target_set;
4866 sigset_t set;
4867 int d0, i;
4869 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4870 goto badframe;
4872 /* set blocked signals */
4874 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4875 goto badframe;
4877 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4878 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4879 goto badframe;
4882 target_to_host_sigset_internal(&set, &target_set);
4883 sigprocmask(SIG_SETMASK, &set, NULL);
4885 /* restore registers */
4887 if (restore_sigcontext(env, &frame->sc, &d0))
4888 goto badframe;
4890 unlock_user_struct(frame, frame_addr, 0);
4891 return d0;
4893 badframe:
4894 unlock_user_struct(frame, frame_addr, 0);
4895 force_sig(TARGET_SIGSEGV);
4896 return 0;
4899 long do_rt_sigreturn(CPUState *env)
4901 struct target_rt_sigframe *frame;
4902 abi_ulong frame_addr = env->aregs[7] - 4;
4903 target_sigset_t target_set;
4904 sigset_t set;
4905 int d0;
4907 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4908 goto badframe;
4910 target_to_host_sigset_internal(&set, &target_set);
4911 sigprocmask(SIG_SETMASK, &set, NULL);
4913 /* restore registers */
4915 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4916 goto badframe;
4918 if (do_sigaltstack(frame_addr +
4919 offsetof(struct target_rt_sigframe, uc.tuc_stack),
4920 0, get_sp_from_cpustate(env)) == -EFAULT)
4921 goto badframe;
4923 unlock_user_struct(frame, frame_addr, 0);
4924 return d0;
4926 badframe:
4927 unlock_user_struct(frame, frame_addr, 0);
4928 force_sig(TARGET_SIGSEGV);
4929 return 0;
4932 #elif defined(TARGET_ALPHA)
4934 struct target_sigcontext {
4935 abi_long sc_onstack;
4936 abi_long sc_mask;
4937 abi_long sc_pc;
4938 abi_long sc_ps;
4939 abi_long sc_regs[32];
4940 abi_long sc_ownedfp;
4941 abi_long sc_fpregs[32];
4942 abi_ulong sc_fpcr;
4943 abi_ulong sc_fp_control;
4944 abi_ulong sc_reserved1;
4945 abi_ulong sc_reserved2;
4946 abi_ulong sc_ssize;
4947 abi_ulong sc_sbase;
4948 abi_ulong sc_traparg_a0;
4949 abi_ulong sc_traparg_a1;
4950 abi_ulong sc_traparg_a2;
4951 abi_ulong sc_fp_trap_pc;
4952 abi_ulong sc_fp_trigger_sum;
4953 abi_ulong sc_fp_trigger_inst;
4956 struct target_ucontext {
4957 abi_ulong tuc_flags;
4958 abi_ulong tuc_link;
4959 abi_ulong tuc_osf_sigmask;
4960 target_stack_t tuc_stack;
4961 struct target_sigcontext tuc_mcontext;
4962 target_sigset_t tuc_sigmask;
4965 struct target_sigframe {
4966 struct target_sigcontext sc;
4967 unsigned int retcode[3];
4970 struct target_rt_sigframe {
4971 target_siginfo_t info;
4972 struct target_ucontext uc;
4973 unsigned int retcode[3];
4976 #define INSN_MOV_R30_R16 0x47fe0410
4977 #define INSN_LDI_R0 0x201f0000
4978 #define INSN_CALLSYS 0x00000083
4980 static int setup_sigcontext(struct target_sigcontext *sc, CPUState *env,
4981 abi_ulong frame_addr, target_sigset_t *set)
4983 int i, err = 0;
4985 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
4986 err |= __put_user(set->sig[0], &sc->sc_mask);
4987 err |= __put_user(env->pc, &sc->sc_pc);
4988 err |= __put_user(8, &sc->sc_ps);
4990 for (i = 0; i < 31; ++i) {
4991 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
4993 err |= __put_user(0, &sc->sc_regs[31]);
4995 for (i = 0; i < 31; ++i) {
4996 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
4998 err |= __put_user(0, &sc->sc_fpregs[31]);
4999 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5001 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5002 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5003 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5005 return err;
5008 static int restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
5010 uint64_t fpcr;
5011 int i, err = 0;
5013 err |= __get_user(env->pc, &sc->sc_pc);
5015 for (i = 0; i < 31; ++i) {
5016 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5018 for (i = 0; i < 31; ++i) {
5019 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5022 err |= __get_user(fpcr, &sc->sc_fpcr);
5023 cpu_alpha_store_fpcr(env, fpcr);
5025 return err;
5028 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5029 CPUState *env, unsigned long framesize)
5031 abi_ulong sp = env->ir[IR_SP];
5033 /* This is the X/Open sanctioned signal stack switching. */
5034 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5035 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5037 return (sp - framesize) & -32;
5040 static void setup_frame(int sig, struct target_sigaction *ka,
5041 target_sigset_t *set, CPUState *env)
5043 abi_ulong frame_addr, r26;
5044 struct target_sigframe *frame;
5045 int err = 0;
5047 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5048 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5049 goto give_sigsegv;
5052 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5054 if (ka->sa_restorer) {
5055 r26 = ka->sa_restorer;
5056 } else {
5057 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5058 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5059 &frame->retcode[1]);
5060 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5061 /* imb() */
5062 r26 = frame_addr;
5065 unlock_user_struct(frame, frame_addr, 1);
5067 if (err) {
5068 give_sigsegv:
5069 if (sig == TARGET_SIGSEGV) {
5070 ka->_sa_handler = TARGET_SIG_DFL;
5072 force_sig(TARGET_SIGSEGV);
5075 env->ir[IR_RA] = r26;
5076 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5077 env->ir[IR_A0] = sig;
5078 env->ir[IR_A1] = 0;
5079 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5080 env->ir[IR_SP] = frame_addr;
5083 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5084 target_siginfo_t *info,
5085 target_sigset_t *set, CPUState *env)
5087 abi_ulong frame_addr, r26;
5088 struct target_rt_sigframe *frame;
5089 int i, err = 0;
5091 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5092 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5093 goto give_sigsegv;
5096 err |= copy_siginfo_to_user(&frame->info, info);
5098 err |= __put_user(0, &frame->uc.tuc_flags);
5099 err |= __put_user(0, &frame->uc.tuc_link);
5100 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5101 err |= __put_user(target_sigaltstack_used.ss_sp,
5102 &frame->uc.tuc_stack.ss_sp);
5103 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5104 &frame->uc.tuc_stack.ss_flags);
5105 err |= __put_user(target_sigaltstack_used.ss_size,
5106 &frame->uc.tuc_stack.ss_size);
5107 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5108 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5109 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5112 if (ka->sa_restorer) {
5113 r26 = ka->sa_restorer;
5114 } else {
5115 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5116 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5117 &frame->retcode[1]);
5118 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5119 /* imb(); */
5120 r26 = frame_addr;
5123 if (err) {
5124 give_sigsegv:
5125 if (sig == TARGET_SIGSEGV) {
5126 ka->_sa_handler = TARGET_SIG_DFL;
5128 force_sig(TARGET_SIGSEGV);
5131 env->ir[IR_RA] = r26;
5132 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5133 env->ir[IR_A0] = sig;
5134 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5135 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5136 env->ir[IR_SP] = frame_addr;
5139 long do_sigreturn(CPUState *env)
5141 struct target_sigcontext *sc;
5142 abi_ulong sc_addr = env->ir[IR_A0];
5143 target_sigset_t target_set;
5144 sigset_t set;
5146 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5147 goto badframe;
5150 target_sigemptyset(&target_set);
5151 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5152 goto badframe;
5155 target_to_host_sigset_internal(&set, &target_set);
5156 sigprocmask(SIG_SETMASK, &set, NULL);
5158 if (restore_sigcontext(env, sc)) {
5159 goto badframe;
5161 unlock_user_struct(sc, sc_addr, 0);
5162 return env->ir[IR_V0];
5164 badframe:
5165 unlock_user_struct(sc, sc_addr, 0);
5166 force_sig(TARGET_SIGSEGV);
5169 long do_rt_sigreturn(CPUState *env)
5171 abi_ulong frame_addr = env->ir[IR_A0];
5172 struct target_rt_sigframe *frame;
5173 sigset_t set;
5175 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5176 goto badframe;
5178 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5179 sigprocmask(SIG_SETMASK, &set, NULL);
5181 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5182 goto badframe;
5184 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5185 uc.tuc_stack),
5186 0, env->ir[IR_SP]) == -EFAULT) {
5187 goto badframe;
5190 unlock_user_struct(frame, frame_addr, 0);
5191 return env->ir[IR_V0];
5194 badframe:
5195 unlock_user_struct(frame, frame_addr, 0);
5196 force_sig(TARGET_SIGSEGV);
5199 #else
5201 static void setup_frame(int sig, struct target_sigaction *ka,
5202 target_sigset_t *set, CPUState *env)
5204 fprintf(stderr, "setup_frame: not implemented\n");
5207 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5208 target_siginfo_t *info,
5209 target_sigset_t *set, CPUState *env)
5211 fprintf(stderr, "setup_rt_frame: not implemented\n");
5214 long do_sigreturn(CPUState *env)
5216 fprintf(stderr, "do_sigreturn: not implemented\n");
5217 return -TARGET_ENOSYS;
5220 long do_rt_sigreturn(CPUState *env)
5222 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5223 return -TARGET_ENOSYS;
5226 #endif
5228 void process_pending_signals(CPUState *cpu_env)
5230 int sig;
5231 abi_ulong handler;
5232 sigset_t set, old_set;
5233 target_sigset_t target_old_set;
5234 struct emulated_sigtable *k;
5235 struct target_sigaction *sa;
5236 struct sigqueue *q;
5237 TaskState *ts = cpu_env->opaque;
5239 if (!ts->signal_pending)
5240 return;
5242 /* FIXME: This is not threadsafe. */
5243 k = ts->sigtab;
5244 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5245 if (k->pending)
5246 goto handle_signal;
5247 k++;
5249 /* if no signal is pending, just return */
5250 ts->signal_pending = 0;
5251 return;
5253 handle_signal:
5254 #ifdef DEBUG_SIGNAL
5255 fprintf(stderr, "qemu: process signal %d\n", sig);
5256 #endif
5257 /* dequeue signal */
5258 q = k->first;
5259 k->first = q->next;
5260 if (!k->first)
5261 k->pending = 0;
5263 sig = gdb_handlesig (cpu_env, sig);
5264 if (!sig) {
5265 sa = NULL;
5266 handler = TARGET_SIG_IGN;
5267 } else {
5268 sa = &sigact_table[sig - 1];
5269 handler = sa->_sa_handler;
5272 if (handler == TARGET_SIG_DFL) {
5273 /* default handler : ignore some signal. The other are job control or fatal */
5274 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5275 kill(getpid(),SIGSTOP);
5276 } else if (sig != TARGET_SIGCHLD &&
5277 sig != TARGET_SIGURG &&
5278 sig != TARGET_SIGWINCH &&
5279 sig != TARGET_SIGCONT) {
5280 force_sig(sig);
5282 } else if (handler == TARGET_SIG_IGN) {
5283 /* ignore sig */
5284 } else if (handler == TARGET_SIG_ERR) {
5285 force_sig(sig);
5286 } else {
5287 /* compute the blocked signals during the handler execution */
5288 target_to_host_sigset(&set, &sa->sa_mask);
5289 /* SA_NODEFER indicates that the current signal should not be
5290 blocked during the handler */
5291 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5292 sigaddset(&set, target_to_host_signal(sig));
5294 /* block signals in the handler using Linux */
5295 sigprocmask(SIG_BLOCK, &set, &old_set);
5296 /* save the previous blocked signal state to restore it at the
5297 end of the signal execution (see do_sigreturn) */
5298 host_to_target_sigset_internal(&target_old_set, &old_set);
5300 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5301 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5303 CPUX86State *env = cpu_env;
5304 if (env->eflags & VM_MASK)
5305 save_v86_state(env);
5307 #endif
5308 /* prepare the stack frame of the virtual CPU */
5309 if (sa->sa_flags & TARGET_SA_SIGINFO)
5310 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5311 else
5312 setup_frame(sig, sa, &target_old_set, cpu_env);
5313 if (sa->sa_flags & TARGET_SA_RESETHAND)
5314 sa->_sa_handler = TARGET_SIG_DFL;
5316 if (q != &k->info)
5317 free_sigqueue(cpu_env, q);