Import kernel headers
[qemu.git] / linux-user / signal.c
blob11b25be7b8414e9f7a628a4cc2209c044a92a1de
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 noone 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] = tswapl(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] = tswapl(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 tswapl(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 tswapl(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)tswapl(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 = tswapl(k->_sa_handler);
590 oact->sa_flags = tswapl(k->sa_flags);
591 #if !defined(TARGET_MIPS)
592 oact->sa_restorer = tswapl(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 = tswapl(act->_sa_handler);
599 k->sa_flags = tswapl(act->sa_flags);
600 #if !defined(TARGET_MIPS)
601 k->sa_restorer = tswapl(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(&sc->cs) | 3);
985 cpu_x86_load_seg(env, R_SS, lduw(&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;
1277 #if 0
1278 flush_icache_range((abi_ulong)rc,
1279 (abi_ulong)(rc + 1));
1280 #endif
1281 retcode = rc_addr + thumb;
1284 env->regs[0] = usig;
1285 env->regs[13] = frame_addr;
1286 env->regs[14] = retcode;
1287 env->regs[15] = handler & (thumb ? ~1 : ~3);
1288 cpsr_write(env, cpsr, 0xffffffff);
1290 return 0;
1293 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUState *env)
1295 int i;
1296 struct target_vfp_sigframe *vfpframe;
1297 vfpframe = (struct target_vfp_sigframe *)regspace;
1298 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1299 __put_user(sizeof(*vfpframe), &vfpframe->size);
1300 for (i = 0; i < 32; i++) {
1301 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1303 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1304 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1305 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1306 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1307 return (abi_ulong*)(vfpframe+1);
1310 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace, CPUState *env)
1312 int i;
1313 struct target_iwmmxt_sigframe *iwmmxtframe;
1314 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1315 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1316 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1317 for (i = 0; i < 16; i++) {
1318 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1320 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1321 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1322 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1323 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1324 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1325 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1326 return (abi_ulong*)(iwmmxtframe+1);
1329 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1330 target_sigset_t *set, CPUState *env)
1332 struct target_sigaltstack stack;
1333 int i;
1334 abi_ulong *regspace;
1336 /* Clear all the bits of the ucontext we don't use. */
1337 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1339 memset(&stack, 0, sizeof(stack));
1340 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1341 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1342 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1343 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1345 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1346 /* Save coprocessor signal frame. */
1347 regspace = uc->tuc_regspace;
1348 if (arm_feature(env, ARM_FEATURE_VFP)) {
1349 regspace = setup_sigframe_v2_vfp(regspace, env);
1351 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1352 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1355 /* Write terminating magic word */
1356 __put_user(0, regspace);
1358 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1359 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1363 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1364 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1365 target_sigset_t *set, CPUState *regs)
1367 struct sigframe_v1 *frame;
1368 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1369 int i;
1371 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1372 return;
1374 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1376 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1377 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1378 goto end;
1381 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1382 frame_addr + offsetof(struct sigframe_v1, retcode));
1384 end:
1385 unlock_user_struct(frame, frame_addr, 1);
1388 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1389 target_sigset_t *set, CPUState *regs)
1391 struct sigframe_v2 *frame;
1392 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1394 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1395 return;
1397 setup_sigframe_v2(&frame->uc, set, regs);
1399 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1400 frame_addr + offsetof(struct sigframe_v2, retcode));
1402 unlock_user_struct(frame, frame_addr, 1);
1405 static void setup_frame(int usig, struct target_sigaction *ka,
1406 target_sigset_t *set, CPUState *regs)
1408 if (get_osversion() >= 0x020612) {
1409 setup_frame_v2(usig, ka, set, regs);
1410 } else {
1411 setup_frame_v1(usig, ka, set, regs);
1415 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1416 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1417 target_siginfo_t *info,
1418 target_sigset_t *set, CPUState *env)
1420 struct rt_sigframe_v1 *frame;
1421 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1422 struct target_sigaltstack stack;
1423 int i;
1424 abi_ulong info_addr, uc_addr;
1426 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1427 return /* 1 */;
1429 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1430 __put_user(info_addr, &frame->pinfo);
1431 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1432 __put_user(uc_addr, &frame->puc);
1433 copy_siginfo_to_user(&frame->info, info);
1435 /* Clear all the bits of the ucontext we don't use. */
1436 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1438 memset(&stack, 0, sizeof(stack));
1439 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1440 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1441 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1442 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1444 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1445 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1446 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1447 goto end;
1450 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1451 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1453 env->regs[1] = info_addr;
1454 env->regs[2] = uc_addr;
1456 end:
1457 unlock_user_struct(frame, frame_addr, 1);
1460 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1461 target_siginfo_t *info,
1462 target_sigset_t *set, CPUState *env)
1464 struct rt_sigframe_v2 *frame;
1465 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1466 abi_ulong info_addr, uc_addr;
1468 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1469 return /* 1 */;
1471 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1472 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1473 copy_siginfo_to_user(&frame->info, info);
1475 setup_sigframe_v2(&frame->uc, set, env);
1477 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1478 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1480 env->regs[1] = info_addr;
1481 env->regs[2] = uc_addr;
1483 unlock_user_struct(frame, frame_addr, 1);
1486 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1487 target_siginfo_t *info,
1488 target_sigset_t *set, CPUState *env)
1490 if (get_osversion() >= 0x020612) {
1491 setup_rt_frame_v2(usig, ka, info, set, env);
1492 } else {
1493 setup_rt_frame_v1(usig, ka, info, set, env);
1497 static int
1498 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1500 int err = 0;
1501 uint32_t cpsr;
1503 __get_user_error(env->regs[0], &sc->arm_r0, err);
1504 __get_user_error(env->regs[1], &sc->arm_r1, err);
1505 __get_user_error(env->regs[2], &sc->arm_r2, err);
1506 __get_user_error(env->regs[3], &sc->arm_r3, err);
1507 __get_user_error(env->regs[4], &sc->arm_r4, err);
1508 __get_user_error(env->regs[5], &sc->arm_r5, err);
1509 __get_user_error(env->regs[6], &sc->arm_r6, err);
1510 __get_user_error(env->regs[7], &sc->arm_r7, err);
1511 __get_user_error(env->regs[8], &sc->arm_r8, err);
1512 __get_user_error(env->regs[9], &sc->arm_r9, err);
1513 __get_user_error(env->regs[10], &sc->arm_r10, err);
1514 __get_user_error(env->regs[11], &sc->arm_fp, err);
1515 __get_user_error(env->regs[12], &sc->arm_ip, err);
1516 __get_user_error(env->regs[13], &sc->arm_sp, err);
1517 __get_user_error(env->regs[14], &sc->arm_lr, err);
1518 __get_user_error(env->regs[15], &sc->arm_pc, err);
1519 #ifdef TARGET_CONFIG_CPU_32
1520 __get_user_error(cpsr, &sc->arm_cpsr, err);
1521 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1522 #endif
1524 err |= !valid_user_regs(env);
1526 return err;
1529 static long do_sigreturn_v1(CPUState *env)
1531 abi_ulong frame_addr;
1532 struct sigframe_v1 *frame;
1533 target_sigset_t set;
1534 sigset_t host_set;
1535 int i;
1538 * Since we stacked the signal on a 64-bit boundary,
1539 * then 'sp' should be word aligned here. If it's
1540 * not, then the user is trying to mess with us.
1542 if (env->regs[13] & 7)
1543 goto badframe;
1545 frame_addr = env->regs[13];
1546 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1547 goto badframe;
1549 if (__get_user(set.sig[0], &frame->sc.oldmask))
1550 goto badframe;
1551 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1552 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1553 goto badframe;
1556 target_to_host_sigset_internal(&host_set, &set);
1557 sigprocmask(SIG_SETMASK, &host_set, NULL);
1559 if (restore_sigcontext(env, &frame->sc))
1560 goto badframe;
1562 #if 0
1563 /* Send SIGTRAP if we're single-stepping */
1564 if (ptrace_cancel_bpt(current))
1565 send_sig(SIGTRAP, current, 1);
1566 #endif
1567 unlock_user_struct(frame, frame_addr, 0);
1568 return env->regs[0];
1570 badframe:
1571 unlock_user_struct(frame, frame_addr, 0);
1572 force_sig(TARGET_SIGSEGV /* , current */);
1573 return 0;
1576 static abi_ulong *restore_sigframe_v2_vfp(CPUState *env, abi_ulong *regspace)
1578 int i;
1579 abi_ulong magic, sz;
1580 uint32_t fpscr, fpexc;
1581 struct target_vfp_sigframe *vfpframe;
1582 vfpframe = (struct target_vfp_sigframe *)regspace;
1584 __get_user(magic, &vfpframe->magic);
1585 __get_user(sz, &vfpframe->size);
1586 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1587 return 0;
1589 for (i = 0; i < 32; i++) {
1590 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1592 __get_user(fpscr, &vfpframe->ufp.fpscr);
1593 vfp_set_fpscr(env, fpscr);
1594 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1595 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1596 * and the exception flag is cleared
1598 fpexc |= (1 << 30);
1599 fpexc &= ~((1 << 31) | (1 << 28));
1600 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1601 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1602 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1603 return (abi_ulong*)(vfpframe + 1);
1606 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUState *env, abi_ulong *regspace)
1608 int i;
1609 abi_ulong magic, sz;
1610 struct target_iwmmxt_sigframe *iwmmxtframe;
1611 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1613 __get_user(magic, &iwmmxtframe->magic);
1614 __get_user(sz, &iwmmxtframe->size);
1615 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1616 return 0;
1618 for (i = 0; i < 16; i++) {
1619 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1621 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1622 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1623 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1624 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1625 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1626 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1627 return (abi_ulong*)(iwmmxtframe + 1);
1630 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1631 struct target_ucontext_v2 *uc)
1633 sigset_t host_set;
1634 abi_ulong *regspace;
1636 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1637 sigprocmask(SIG_SETMASK, &host_set, NULL);
1639 if (restore_sigcontext(env, &uc->tuc_mcontext))
1640 return 1;
1642 /* Restore coprocessor signal frame */
1643 regspace = uc->tuc_regspace;
1644 if (arm_feature(env, ARM_FEATURE_VFP)) {
1645 regspace = restore_sigframe_v2_vfp(env, regspace);
1646 if (!regspace) {
1647 return 1;
1650 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1651 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1652 if (!regspace) {
1653 return 1;
1657 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1658 return 1;
1660 #if 0
1661 /* Send SIGTRAP if we're single-stepping */
1662 if (ptrace_cancel_bpt(current))
1663 send_sig(SIGTRAP, current, 1);
1664 #endif
1666 return 0;
1669 static long do_sigreturn_v2(CPUState *env)
1671 abi_ulong frame_addr;
1672 struct sigframe_v2 *frame;
1675 * Since we stacked the signal on a 64-bit boundary,
1676 * then 'sp' should be word aligned here. If it's
1677 * not, then the user is trying to mess with us.
1679 if (env->regs[13] & 7)
1680 goto badframe;
1682 frame_addr = env->regs[13];
1683 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1684 goto badframe;
1686 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1687 goto badframe;
1689 unlock_user_struct(frame, frame_addr, 0);
1690 return env->regs[0];
1692 badframe:
1693 unlock_user_struct(frame, frame_addr, 0);
1694 force_sig(TARGET_SIGSEGV /* , current */);
1695 return 0;
1698 long do_sigreturn(CPUState *env)
1700 if (get_osversion() >= 0x020612) {
1701 return do_sigreturn_v2(env);
1702 } else {
1703 return do_sigreturn_v1(env);
1707 static long do_rt_sigreturn_v1(CPUState *env)
1709 abi_ulong frame_addr;
1710 struct rt_sigframe_v1 *frame;
1711 sigset_t host_set;
1714 * Since we stacked the signal on a 64-bit boundary,
1715 * then 'sp' should be word aligned here. If it's
1716 * not, then the user is trying to mess with us.
1718 if (env->regs[13] & 7)
1719 goto badframe;
1721 frame_addr = env->regs[13];
1722 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1723 goto badframe;
1725 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1726 sigprocmask(SIG_SETMASK, &host_set, NULL);
1728 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1729 goto badframe;
1731 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1732 goto badframe;
1734 #if 0
1735 /* Send SIGTRAP if we're single-stepping */
1736 if (ptrace_cancel_bpt(current))
1737 send_sig(SIGTRAP, current, 1);
1738 #endif
1739 unlock_user_struct(frame, frame_addr, 0);
1740 return env->regs[0];
1742 badframe:
1743 unlock_user_struct(frame, frame_addr, 0);
1744 force_sig(TARGET_SIGSEGV /* , current */);
1745 return 0;
1748 static long do_rt_sigreturn_v2(CPUState *env)
1750 abi_ulong frame_addr;
1751 struct rt_sigframe_v2 *frame;
1754 * Since we stacked the signal on a 64-bit boundary,
1755 * then 'sp' should be word aligned here. If it's
1756 * not, then the user is trying to mess with us.
1758 if (env->regs[13] & 7)
1759 goto badframe;
1761 frame_addr = env->regs[13];
1762 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1763 goto badframe;
1765 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1766 goto badframe;
1768 unlock_user_struct(frame, frame_addr, 0);
1769 return env->regs[0];
1771 badframe:
1772 unlock_user_struct(frame, frame_addr, 0);
1773 force_sig(TARGET_SIGSEGV /* , current */);
1774 return 0;
1777 long do_rt_sigreturn(CPUState *env)
1779 if (get_osversion() >= 0x020612) {
1780 return do_rt_sigreturn_v2(env);
1781 } else {
1782 return do_rt_sigreturn_v1(env);
1786 #elif defined(TARGET_SPARC)
1788 #define __SUNOS_MAXWIN 31
1790 /* This is what SunOS does, so shall I. */
1791 struct target_sigcontext {
1792 abi_ulong sigc_onstack; /* state to restore */
1794 abi_ulong sigc_mask; /* sigmask to restore */
1795 abi_ulong sigc_sp; /* stack pointer */
1796 abi_ulong sigc_pc; /* program counter */
1797 abi_ulong sigc_npc; /* next program counter */
1798 abi_ulong sigc_psr; /* for condition codes etc */
1799 abi_ulong sigc_g1; /* User uses these two registers */
1800 abi_ulong sigc_o0; /* within the trampoline code. */
1802 /* Now comes information regarding the users window set
1803 * at the time of the signal.
1805 abi_ulong sigc_oswins; /* outstanding windows */
1807 /* stack ptrs for each regwin buf */
1808 char *sigc_spbuf[__SUNOS_MAXWIN];
1810 /* Windows to restore after signal */
1811 struct {
1812 abi_ulong locals[8];
1813 abi_ulong ins[8];
1814 } sigc_wbuf[__SUNOS_MAXWIN];
1816 /* A Sparc stack frame */
1817 struct sparc_stackf {
1818 abi_ulong locals[8];
1819 abi_ulong ins[8];
1820 /* It's simpler to treat fp and callers_pc as elements of ins[]
1821 * since we never need to access them ourselves.
1823 char *structptr;
1824 abi_ulong xargs[6];
1825 abi_ulong xxargs[1];
1828 typedef struct {
1829 struct {
1830 abi_ulong psr;
1831 abi_ulong pc;
1832 abi_ulong npc;
1833 abi_ulong y;
1834 abi_ulong u_regs[16]; /* globals and ins */
1835 } si_regs;
1836 int si_mask;
1837 } __siginfo_t;
1839 typedef struct {
1840 unsigned long si_float_regs [32];
1841 unsigned long si_fsr;
1842 unsigned long si_fpqdepth;
1843 struct {
1844 unsigned long *insn_addr;
1845 unsigned long insn;
1846 } si_fpqueue [16];
1847 } qemu_siginfo_fpu_t;
1850 struct target_signal_frame {
1851 struct sparc_stackf ss;
1852 __siginfo_t info;
1853 abi_ulong fpu_save;
1854 abi_ulong insns[2] __attribute__ ((aligned (8)));
1855 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1856 abi_ulong extra_size; /* Should be 0 */
1857 qemu_siginfo_fpu_t fpu_state;
1859 struct target_rt_signal_frame {
1860 struct sparc_stackf ss;
1861 siginfo_t info;
1862 abi_ulong regs[20];
1863 sigset_t mask;
1864 abi_ulong fpu_save;
1865 unsigned int insns[2];
1866 stack_t stack;
1867 unsigned int extra_size; /* Should be 0 */
1868 qemu_siginfo_fpu_t fpu_state;
1871 #define UREG_O0 16
1872 #define UREG_O6 22
1873 #define UREG_I0 0
1874 #define UREG_I1 1
1875 #define UREG_I2 2
1876 #define UREG_I3 3
1877 #define UREG_I4 4
1878 #define UREG_I5 5
1879 #define UREG_I6 6
1880 #define UREG_I7 7
1881 #define UREG_L0 8
1882 #define UREG_FP UREG_I6
1883 #define UREG_SP UREG_O6
1885 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1886 CPUState *env, unsigned long framesize)
1888 abi_ulong sp;
1890 sp = env->regwptr[UREG_FP];
1892 /* This is the X/Open sanctioned signal stack switching. */
1893 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1894 if (!on_sig_stack(sp)
1895 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1896 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1898 return sp - framesize;
1901 static int
1902 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1904 int err = 0, i;
1906 err |= __put_user(env->psr, &si->si_regs.psr);
1907 err |= __put_user(env->pc, &si->si_regs.pc);
1908 err |= __put_user(env->npc, &si->si_regs.npc);
1909 err |= __put_user(env->y, &si->si_regs.y);
1910 for (i=0; i < 8; i++) {
1911 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1913 for (i=0; i < 8; i++) {
1914 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1916 err |= __put_user(mask, &si->si_mask);
1917 return err;
1920 #if 0
1921 static int
1922 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1923 CPUState *env, unsigned long mask)
1925 int err = 0;
1927 err |= __put_user(mask, &sc->sigc_mask);
1928 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1929 err |= __put_user(env->pc, &sc->sigc_pc);
1930 err |= __put_user(env->npc, &sc->sigc_npc);
1931 err |= __put_user(env->psr, &sc->sigc_psr);
1932 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1933 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1935 return err;
1937 #endif
1938 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1940 static void setup_frame(int sig, struct target_sigaction *ka,
1941 target_sigset_t *set, CPUState *env)
1943 abi_ulong sf_addr;
1944 struct target_signal_frame *sf;
1945 int sigframe_size, err, i;
1947 /* 1. Make sure everything is clean */
1948 //synchronize_user_stack();
1950 sigframe_size = NF_ALIGNEDSZ;
1951 sf_addr = get_sigframe(ka, env, sigframe_size);
1953 sf = lock_user(VERIFY_WRITE, sf_addr,
1954 sizeof(struct target_signal_frame), 0);
1955 if (!sf)
1956 goto sigsegv;
1958 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1959 #if 0
1960 if (invalid_frame_pointer(sf, sigframe_size))
1961 goto sigill_and_return;
1962 #endif
1963 /* 2. Save the current process state */
1964 err = setup___siginfo(&sf->info, env, set->sig[0]);
1965 err |= __put_user(0, &sf->extra_size);
1967 //err |= save_fpu_state(regs, &sf->fpu_state);
1968 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1970 err |= __put_user(set->sig[0], &sf->info.si_mask);
1971 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1972 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1975 for (i = 0; i < 8; i++) {
1976 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1978 for (i = 0; i < 8; i++) {
1979 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1981 if (err)
1982 goto sigsegv;
1984 /* 3. signal handler back-trampoline and parameters */
1985 env->regwptr[UREG_FP] = sf_addr;
1986 env->regwptr[UREG_I0] = sig;
1987 env->regwptr[UREG_I1] = sf_addr +
1988 offsetof(struct target_signal_frame, info);
1989 env->regwptr[UREG_I2] = sf_addr +
1990 offsetof(struct target_signal_frame, info);
1992 /* 4. signal handler */
1993 env->pc = ka->_sa_handler;
1994 env->npc = (env->pc + 4);
1995 /* 5. return to kernel instructions */
1996 if (ka->sa_restorer)
1997 env->regwptr[UREG_I7] = ka->sa_restorer;
1998 else {
1999 uint32_t val32;
2001 env->regwptr[UREG_I7] = sf_addr +
2002 offsetof(struct target_signal_frame, insns) - 2 * 4;
2004 /* mov __NR_sigreturn, %g1 */
2005 val32 = 0x821020d8;
2006 err |= __put_user(val32, &sf->insns[0]);
2008 /* t 0x10 */
2009 val32 = 0x91d02010;
2010 err |= __put_user(val32, &sf->insns[1]);
2011 if (err)
2012 goto sigsegv;
2014 /* Flush instruction space. */
2015 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2016 // tb_flush(env);
2018 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2019 return;
2020 #if 0
2021 sigill_and_return:
2022 force_sig(TARGET_SIGILL);
2023 #endif
2024 sigsegv:
2025 //fprintf(stderr, "force_sig\n");
2026 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2027 force_sig(TARGET_SIGSEGV);
2029 static inline int
2030 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
2032 int err;
2033 #if 0
2034 #ifdef CONFIG_SMP
2035 if (current->flags & PF_USEDFPU)
2036 regs->psr &= ~PSR_EF;
2037 #else
2038 if (current == last_task_used_math) {
2039 last_task_used_math = 0;
2040 regs->psr &= ~PSR_EF;
2042 #endif
2043 current->used_math = 1;
2044 current->flags &= ~PF_USEDFPU;
2045 #endif
2046 #if 0
2047 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2048 return -EFAULT;
2049 #endif
2051 #if 0
2052 /* XXX: incorrect */
2053 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
2054 (sizeof(unsigned long) * 32));
2055 #endif
2056 err |= __get_user(env->fsr, &fpu->si_fsr);
2057 #if 0
2058 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2059 if (current->thread.fpqdepth != 0)
2060 err |= __copy_from_user(&current->thread.fpqueue[0],
2061 &fpu->si_fpqueue[0],
2062 ((sizeof(unsigned long) +
2063 (sizeof(unsigned long *)))*16));
2064 #endif
2065 return err;
2069 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2070 target_siginfo_t *info,
2071 target_sigset_t *set, CPUState *env)
2073 fprintf(stderr, "setup_rt_frame: not implemented\n");
2076 long do_sigreturn(CPUState *env)
2078 abi_ulong sf_addr;
2079 struct target_signal_frame *sf;
2080 uint32_t up_psr, pc, npc;
2081 target_sigset_t set;
2082 sigset_t host_set;
2083 abi_ulong fpu_save_addr;
2084 int err, i;
2086 sf_addr = env->regwptr[UREG_FP];
2087 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2088 goto segv_and_exit;
2089 #if 0
2090 fprintf(stderr, "sigreturn\n");
2091 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2092 #endif
2093 //cpu_dump_state(env, stderr, fprintf, 0);
2095 /* 1. Make sure we are not getting garbage from the user */
2097 if (sf_addr & 3)
2098 goto segv_and_exit;
2100 err = __get_user(pc, &sf->info.si_regs.pc);
2101 err |= __get_user(npc, &sf->info.si_regs.npc);
2103 if ((pc | npc) & 3)
2104 goto segv_and_exit;
2106 /* 2. Restore the state */
2107 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2109 /* User can only change condition codes and FPU enabling in %psr. */
2110 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2111 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2113 env->pc = pc;
2114 env->npc = npc;
2115 err |= __get_user(env->y, &sf->info.si_regs.y);
2116 for (i=0; i < 8; i++) {
2117 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2119 for (i=0; i < 8; i++) {
2120 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2123 err |= __get_user(fpu_save_addr, &sf->fpu_save);
2125 //if (fpu_save)
2126 // err |= restore_fpu_state(env, fpu_save);
2128 /* This is pretty much atomic, no amount locking would prevent
2129 * the races which exist anyways.
2131 err |= __get_user(set.sig[0], &sf->info.si_mask);
2132 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2133 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2136 target_to_host_sigset_internal(&host_set, &set);
2137 sigprocmask(SIG_SETMASK, &host_set, NULL);
2139 if (err)
2140 goto segv_and_exit;
2141 unlock_user_struct(sf, sf_addr, 0);
2142 return env->regwptr[0];
2144 segv_and_exit:
2145 unlock_user_struct(sf, sf_addr, 0);
2146 force_sig(TARGET_SIGSEGV);
2149 long do_rt_sigreturn(CPUState *env)
2151 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2152 return -TARGET_ENOSYS;
2155 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2156 #define MC_TSTATE 0
2157 #define MC_PC 1
2158 #define MC_NPC 2
2159 #define MC_Y 3
2160 #define MC_G1 4
2161 #define MC_G2 5
2162 #define MC_G3 6
2163 #define MC_G4 7
2164 #define MC_G5 8
2165 #define MC_G6 9
2166 #define MC_G7 10
2167 #define MC_O0 11
2168 #define MC_O1 12
2169 #define MC_O2 13
2170 #define MC_O3 14
2171 #define MC_O4 15
2172 #define MC_O5 16
2173 #define MC_O6 17
2174 #define MC_O7 18
2175 #define MC_NGREG 19
2177 typedef abi_ulong target_mc_greg_t;
2178 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2180 struct target_mc_fq {
2181 abi_ulong *mcfq_addr;
2182 uint32_t mcfq_insn;
2185 struct target_mc_fpu {
2186 union {
2187 uint32_t sregs[32];
2188 uint64_t dregs[32];
2189 //uint128_t qregs[16];
2190 } mcfpu_fregs;
2191 abi_ulong mcfpu_fsr;
2192 abi_ulong mcfpu_fprs;
2193 abi_ulong mcfpu_gsr;
2194 struct target_mc_fq *mcfpu_fq;
2195 unsigned char mcfpu_qcnt;
2196 unsigned char mcfpu_qentsz;
2197 unsigned char mcfpu_enab;
2199 typedef struct target_mc_fpu target_mc_fpu_t;
2201 typedef struct {
2202 target_mc_gregset_t mc_gregs;
2203 target_mc_greg_t mc_fp;
2204 target_mc_greg_t mc_i7;
2205 target_mc_fpu_t mc_fpregs;
2206 } target_mcontext_t;
2208 struct target_ucontext {
2209 struct target_ucontext *tuc_link;
2210 abi_ulong tuc_flags;
2211 target_sigset_t tuc_sigmask;
2212 target_mcontext_t tuc_mcontext;
2215 /* A V9 register window */
2216 struct target_reg_window {
2217 abi_ulong locals[8];
2218 abi_ulong ins[8];
2221 #define TARGET_STACK_BIAS 2047
2223 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2224 void sparc64_set_context(CPUSPARCState *env)
2226 abi_ulong ucp_addr;
2227 struct target_ucontext *ucp;
2228 target_mc_gregset_t *grp;
2229 abi_ulong pc, npc, tstate;
2230 abi_ulong fp, i7, w_addr;
2231 unsigned char fenab;
2232 int err;
2233 unsigned int i;
2235 ucp_addr = env->regwptr[UREG_I0];
2236 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2237 goto do_sigsegv;
2238 grp = &ucp->tuc_mcontext.mc_gregs;
2239 err = __get_user(pc, &((*grp)[MC_PC]));
2240 err |= __get_user(npc, &((*grp)[MC_NPC]));
2241 if (err || ((pc | npc) & 3))
2242 goto do_sigsegv;
2243 if (env->regwptr[UREG_I1]) {
2244 target_sigset_t target_set;
2245 sigset_t set;
2247 if (TARGET_NSIG_WORDS == 1) {
2248 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2249 goto do_sigsegv;
2250 } else {
2251 abi_ulong *src, *dst;
2252 src = ucp->tuc_sigmask.sig;
2253 dst = target_set.sig;
2254 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2255 i++, dst++, src++)
2256 err |= __get_user(*dst, src);
2257 if (err)
2258 goto do_sigsegv;
2260 target_to_host_sigset_internal(&set, &target_set);
2261 sigprocmask(SIG_SETMASK, &set, NULL);
2263 env->pc = pc;
2264 env->npc = npc;
2265 err |= __get_user(env->y, &((*grp)[MC_Y]));
2266 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2267 env->asi = (tstate >> 24) & 0xff;
2268 cpu_put_ccr(env, tstate >> 32);
2269 cpu_put_cwp64(env, tstate & 0x1f);
2270 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2271 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2272 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2273 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2274 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2275 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2276 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2277 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2278 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2279 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2280 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2281 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2282 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2283 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2284 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2286 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2287 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2289 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2290 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2291 abi_ulong) != 0)
2292 goto do_sigsegv;
2293 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2294 abi_ulong) != 0)
2295 goto do_sigsegv;
2296 err |= __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, *dst;
2300 src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2301 dst = env->fpr;
2302 /* XXX: check that the CPU storage is the same as user context */
2303 for (i = 0; i < 64; i++, dst++, src++)
2304 err |= __get_user(*dst, src);
2306 err |= __get_user(env->fsr,
2307 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2308 err |= __get_user(env->gsr,
2309 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2310 if (err)
2311 goto do_sigsegv;
2312 unlock_user_struct(ucp, ucp_addr, 0);
2313 return;
2314 do_sigsegv:
2315 unlock_user_struct(ucp, ucp_addr, 0);
2316 force_sig(TARGET_SIGSEGV);
2319 void sparc64_get_context(CPUSPARCState *env)
2321 abi_ulong ucp_addr;
2322 struct target_ucontext *ucp;
2323 target_mc_gregset_t *grp;
2324 target_mcontext_t *mcp;
2325 abi_ulong fp, i7, w_addr;
2326 int err;
2327 unsigned int i;
2328 target_sigset_t target_set;
2329 sigset_t set;
2331 ucp_addr = env->regwptr[UREG_I0];
2332 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2333 goto do_sigsegv;
2335 mcp = &ucp->tuc_mcontext;
2336 grp = &mcp->mc_gregs;
2338 /* Skip over the trap instruction, first. */
2339 env->pc = env->npc;
2340 env->npc += 4;
2342 err = 0;
2344 sigprocmask(0, NULL, &set);
2345 host_to_target_sigset_internal(&target_set, &set);
2346 if (TARGET_NSIG_WORDS == 1) {
2347 err |= __put_user(target_set.sig[0],
2348 (abi_ulong *)&ucp->tuc_sigmask);
2349 } else {
2350 abi_ulong *src, *dst;
2351 src = target_set.sig;
2352 dst = ucp->tuc_sigmask.sig;
2353 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2354 i++, dst++, src++)
2355 err |= __put_user(*src, dst);
2356 if (err)
2357 goto do_sigsegv;
2360 /* XXX: tstate must be saved properly */
2361 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2362 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2363 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2364 err |= __put_user(env->y, &((*grp)[MC_Y]));
2365 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2366 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2367 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2368 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2369 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2370 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2371 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2372 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2373 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2374 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2375 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2376 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2377 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2378 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2379 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2381 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2382 fp = i7 = 0;
2383 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2384 abi_ulong) != 0)
2385 goto do_sigsegv;
2386 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2387 abi_ulong) != 0)
2388 goto do_sigsegv;
2389 err |= __put_user(fp, &(mcp->mc_fp));
2390 err |= __put_user(i7, &(mcp->mc_i7));
2393 uint32_t *src, *dst;
2394 src = env->fpr;
2395 dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2396 /* XXX: check that the CPU storage is the same as user context */
2397 for (i = 0; i < 64; i++, dst++, src++)
2398 err |= __put_user(*src, dst);
2400 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2401 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2402 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2404 if (err)
2405 goto do_sigsegv;
2406 unlock_user_struct(ucp, ucp_addr, 1);
2407 return;
2408 do_sigsegv:
2409 unlock_user_struct(ucp, ucp_addr, 1);
2410 force_sig(TARGET_SIGSEGV);
2412 #endif
2413 #elif defined(TARGET_ABI_MIPSN64)
2415 # warning signal handling not implemented
2417 static void setup_frame(int sig, struct target_sigaction *ka,
2418 target_sigset_t *set, CPUState *env)
2420 fprintf(stderr, "setup_frame: not implemented\n");
2423 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2424 target_siginfo_t *info,
2425 target_sigset_t *set, CPUState *env)
2427 fprintf(stderr, "setup_rt_frame: not implemented\n");
2430 long do_sigreturn(CPUState *env)
2432 fprintf(stderr, "do_sigreturn: not implemented\n");
2433 return -TARGET_ENOSYS;
2436 long do_rt_sigreturn(CPUState *env)
2438 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2439 return -TARGET_ENOSYS;
2442 #elif defined(TARGET_ABI_MIPSN32)
2444 # warning signal handling not implemented
2446 static void setup_frame(int sig, struct target_sigaction *ka,
2447 target_sigset_t *set, CPUState *env)
2449 fprintf(stderr, "setup_frame: not implemented\n");
2452 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2453 target_siginfo_t *info,
2454 target_sigset_t *set, CPUState *env)
2456 fprintf(stderr, "setup_rt_frame: not implemented\n");
2459 long do_sigreturn(CPUState *env)
2461 fprintf(stderr, "do_sigreturn: not implemented\n");
2462 return -TARGET_ENOSYS;
2465 long do_rt_sigreturn(CPUState *env)
2467 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2468 return -TARGET_ENOSYS;
2471 #elif defined(TARGET_ABI_MIPSO32)
2473 struct target_sigcontext {
2474 uint32_t sc_regmask; /* Unused */
2475 uint32_t sc_status;
2476 uint64_t sc_pc;
2477 uint64_t sc_regs[32];
2478 uint64_t sc_fpregs[32];
2479 uint32_t sc_ownedfp; /* Unused */
2480 uint32_t sc_fpc_csr;
2481 uint32_t sc_fpc_eir; /* Unused */
2482 uint32_t sc_used_math;
2483 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2484 uint32_t pad0;
2485 uint64_t sc_mdhi;
2486 uint64_t sc_mdlo;
2487 target_ulong sc_hi1; /* Was sc_cause */
2488 target_ulong sc_lo1; /* Was sc_badvaddr */
2489 target_ulong sc_hi2; /* Was sc_sigset[4] */
2490 target_ulong sc_lo2;
2491 target_ulong sc_hi3;
2492 target_ulong sc_lo3;
2495 struct sigframe {
2496 uint32_t sf_ass[4]; /* argument save space for o32 */
2497 uint32_t sf_code[2]; /* signal trampoline */
2498 struct target_sigcontext sf_sc;
2499 target_sigset_t sf_mask;
2502 struct target_ucontext {
2503 target_ulong tuc_flags;
2504 target_ulong tuc_link;
2505 target_stack_t tuc_stack;
2506 target_ulong pad0;
2507 struct target_sigcontext tuc_mcontext;
2508 target_sigset_t tuc_sigmask;
2511 struct target_rt_sigframe {
2512 uint32_t rs_ass[4]; /* argument save space for o32 */
2513 uint32_t rs_code[2]; /* signal trampoline */
2514 struct target_siginfo rs_info;
2515 struct target_ucontext rs_uc;
2518 /* Install trampoline to jump back from signal handler */
2519 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2521 int err;
2524 * Set up the return code ...
2526 * li v0, __NR__foo_sigreturn
2527 * syscall
2530 err = __put_user(0x24020000 + syscall, tramp + 0);
2531 err |= __put_user(0x0000000c , tramp + 1);
2532 /* flush_cache_sigtramp((unsigned long) tramp); */
2533 return err;
2536 static inline int
2537 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2539 int err = 0;
2541 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2543 #define save_gp_reg(i) do { \
2544 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2545 } while(0)
2546 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2547 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2548 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2549 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2550 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2551 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2552 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2553 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2554 save_gp_reg(31);
2555 #undef save_gp_reg
2557 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2558 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2560 /* Not used yet, but might be useful if we ever have DSP suppport */
2561 #if 0
2562 if (cpu_has_dsp) {
2563 err |= __put_user(mfhi1(), &sc->sc_hi1);
2564 err |= __put_user(mflo1(), &sc->sc_lo1);
2565 err |= __put_user(mfhi2(), &sc->sc_hi2);
2566 err |= __put_user(mflo2(), &sc->sc_lo2);
2567 err |= __put_user(mfhi3(), &sc->sc_hi3);
2568 err |= __put_user(mflo3(), &sc->sc_lo3);
2569 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2571 /* same with 64 bit */
2572 #ifdef CONFIG_64BIT
2573 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2574 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2575 if (cpu_has_dsp) {
2576 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2577 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2578 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2579 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2580 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2581 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2582 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2584 #endif
2585 #endif
2587 #if 0
2588 err |= __put_user(!!used_math(), &sc->sc_used_math);
2590 if (!used_math())
2591 goto out;
2594 * Save FPU state to signal context. Signal handler will "inherit"
2595 * current FPU state.
2597 preempt_disable();
2599 if (!is_fpu_owner()) {
2600 own_fpu();
2601 restore_fp(current);
2603 err |= save_fp_context(sc);
2605 preempt_enable();
2606 out:
2607 #endif
2608 return err;
2611 static inline int
2612 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2614 int err = 0;
2616 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2618 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2619 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2621 #define restore_gp_reg(i) do { \
2622 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2623 } while(0)
2624 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2625 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2626 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2627 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2628 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2629 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2630 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2631 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2632 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2633 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2634 restore_gp_reg(31);
2635 #undef restore_gp_reg
2637 #if 0
2638 if (cpu_has_dsp) {
2639 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2640 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2641 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2642 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2643 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2644 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2645 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2647 #ifdef CONFIG_64BIT
2648 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2649 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2650 if (cpu_has_dsp) {
2651 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2652 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2653 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2654 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2655 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2656 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2657 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2659 #endif
2661 err |= __get_user(used_math, &sc->sc_used_math);
2662 conditional_used_math(used_math);
2664 preempt_disable();
2666 if (used_math()) {
2667 /* restore fpu context if we have used it before */
2668 own_fpu();
2669 err |= restore_fp_context(sc);
2670 } else {
2671 /* signal handler may have used FPU. Give it up. */
2672 lose_fpu();
2675 preempt_enable();
2676 #endif
2677 return err;
2680 * Determine which stack to use..
2682 static inline abi_ulong
2683 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2685 unsigned long sp;
2687 /* Default to using normal stack */
2688 sp = regs->active_tc.gpr[29];
2691 * FPU emulator may have it's own trampoline active just
2692 * above the user stack, 16-bytes before the next lowest
2693 * 16 byte boundary. Try to avoid trashing it.
2695 sp -= 32;
2697 /* This is the X/Open sanctioned signal stack switching. */
2698 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2699 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2702 return (sp - frame_size) & ~7;
2705 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2706 static void setup_frame(int sig, struct target_sigaction * ka,
2707 target_sigset_t *set, CPUState *regs)
2709 struct sigframe *frame;
2710 abi_ulong frame_addr;
2711 int i;
2713 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2714 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2715 goto give_sigsegv;
2717 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2719 if(setup_sigcontext(regs, &frame->sf_sc))
2720 goto give_sigsegv;
2722 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2723 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2724 goto give_sigsegv;
2728 * Arguments to signal handler:
2730 * a0 = signal number
2731 * a1 = 0 (should be cause)
2732 * a2 = pointer to struct sigcontext
2734 * $25 and PC point to the signal handler, $29 points to the
2735 * struct sigframe.
2737 regs->active_tc.gpr[ 4] = sig;
2738 regs->active_tc.gpr[ 5] = 0;
2739 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2740 regs->active_tc.gpr[29] = frame_addr;
2741 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2742 /* The original kernel code sets CP0_EPC to the handler
2743 * since it returns to userland using eret
2744 * we cannot do this here, and we must set PC directly */
2745 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2746 unlock_user_struct(frame, frame_addr, 1);
2747 return;
2749 give_sigsegv:
2750 unlock_user_struct(frame, frame_addr, 1);
2751 force_sig(TARGET_SIGSEGV/*, current*/);
2752 return;
2755 long do_sigreturn(CPUState *regs)
2757 struct sigframe *frame;
2758 abi_ulong frame_addr;
2759 sigset_t blocked;
2760 target_sigset_t target_set;
2761 int i;
2763 #if defined(DEBUG_SIGNAL)
2764 fprintf(stderr, "do_sigreturn\n");
2765 #endif
2766 frame_addr = regs->active_tc.gpr[29];
2767 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2768 goto badframe;
2770 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2771 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2772 goto badframe;
2775 target_to_host_sigset_internal(&blocked, &target_set);
2776 sigprocmask(SIG_SETMASK, &blocked, NULL);
2778 if (restore_sigcontext(regs, &frame->sf_sc))
2779 goto badframe;
2781 #if 0
2783 * Don't let your children do this ...
2785 __asm__ __volatile__(
2786 "move\t$29, %0\n\t"
2787 "j\tsyscall_exit"
2788 :/* no outputs */
2789 :"r" (&regs));
2790 /* Unreached */
2791 #endif
2793 regs->active_tc.PC = regs->CP0_EPC;
2794 /* I am not sure this is right, but it seems to work
2795 * maybe a problem with nested signals ? */
2796 regs->CP0_EPC = 0;
2797 return -TARGET_QEMU_ESIGRETURN;
2799 badframe:
2800 force_sig(TARGET_SIGSEGV/*, current*/);
2801 return 0;
2804 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2805 target_siginfo_t *info,
2806 target_sigset_t *set, CPUState *env)
2808 struct target_rt_sigframe *frame;
2809 abi_ulong frame_addr;
2810 int i;
2812 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2813 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2814 goto give_sigsegv;
2816 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2818 copy_siginfo_to_user(&frame->rs_info, info);
2820 __put_user(0, &frame->rs_uc.tuc_flags);
2821 __put_user(0, &frame->rs_uc.tuc_link);
2822 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2823 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2824 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2825 &frame->rs_uc.tuc_stack.ss_flags);
2827 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2829 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2830 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2834 * Arguments to signal handler:
2836 * a0 = signal number
2837 * a1 = pointer to struct siginfo
2838 * a2 = pointer to struct ucontext
2840 * $25 and PC point to the signal handler, $29 points to the
2841 * struct sigframe.
2843 env->active_tc.gpr[ 4] = sig;
2844 env->active_tc.gpr[ 5] = frame_addr
2845 + offsetof(struct target_rt_sigframe, rs_info);
2846 env->active_tc.gpr[ 6] = frame_addr
2847 + offsetof(struct target_rt_sigframe, rs_uc);
2848 env->active_tc.gpr[29] = frame_addr;
2849 env->active_tc.gpr[31] = frame_addr
2850 + offsetof(struct target_rt_sigframe, rs_code);
2851 /* The original kernel code sets CP0_EPC to the handler
2852 * since it returns to userland using eret
2853 * we cannot do this here, and we must set PC directly */
2854 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2855 unlock_user_struct(frame, frame_addr, 1);
2856 return;
2858 give_sigsegv:
2859 unlock_user_struct(frame, frame_addr, 1);
2860 force_sig(TARGET_SIGSEGV/*, current*/);
2861 return;
2864 long do_rt_sigreturn(CPUState *env)
2866 struct target_rt_sigframe *frame;
2867 abi_ulong frame_addr;
2868 sigset_t blocked;
2870 #if defined(DEBUG_SIGNAL)
2871 fprintf(stderr, "do_rt_sigreturn\n");
2872 #endif
2873 frame_addr = env->active_tc.gpr[29];
2874 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2875 goto badframe;
2877 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2878 sigprocmask(SIG_SETMASK, &blocked, NULL);
2880 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2881 goto badframe;
2883 if (do_sigaltstack(frame_addr +
2884 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2885 0, get_sp_from_cpustate(env)) == -EFAULT)
2886 goto badframe;
2888 env->active_tc.PC = env->CP0_EPC;
2889 /* I am not sure this is right, but it seems to work
2890 * maybe a problem with nested signals ? */
2891 env->CP0_EPC = 0;
2892 return -TARGET_QEMU_ESIGRETURN;
2894 badframe:
2895 force_sig(TARGET_SIGSEGV/*, current*/);
2896 return 0;
2899 #elif defined(TARGET_SH4)
2902 * code and data structures from linux kernel:
2903 * include/asm-sh/sigcontext.h
2904 * arch/sh/kernel/signal.c
2907 struct target_sigcontext {
2908 target_ulong oldmask;
2910 /* CPU registers */
2911 target_ulong sc_gregs[16];
2912 target_ulong sc_pc;
2913 target_ulong sc_pr;
2914 target_ulong sc_sr;
2915 target_ulong sc_gbr;
2916 target_ulong sc_mach;
2917 target_ulong sc_macl;
2919 /* FPU registers */
2920 target_ulong sc_fpregs[16];
2921 target_ulong sc_xfpregs[16];
2922 unsigned int sc_fpscr;
2923 unsigned int sc_fpul;
2924 unsigned int sc_ownedfp;
2927 struct target_sigframe
2929 struct target_sigcontext sc;
2930 target_ulong extramask[TARGET_NSIG_WORDS-1];
2931 uint16_t retcode[3];
2935 struct target_ucontext {
2936 target_ulong tuc_flags;
2937 struct target_ucontext *tuc_link;
2938 target_stack_t tuc_stack;
2939 struct target_sigcontext tuc_mcontext;
2940 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2943 struct target_rt_sigframe
2945 struct target_siginfo info;
2946 struct target_ucontext uc;
2947 uint16_t retcode[3];
2951 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2952 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2954 static abi_ulong get_sigframe(struct target_sigaction *ka,
2955 unsigned long sp, size_t frame_size)
2957 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2958 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2961 return (sp - frame_size) & -8ul;
2964 static int setup_sigcontext(struct target_sigcontext *sc,
2965 CPUState *regs, unsigned long mask)
2967 int err = 0;
2968 int i;
2970 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2971 COPY(gregs[0]); COPY(gregs[1]);
2972 COPY(gregs[2]); COPY(gregs[3]);
2973 COPY(gregs[4]); COPY(gregs[5]);
2974 COPY(gregs[6]); COPY(gregs[7]);
2975 COPY(gregs[8]); COPY(gregs[9]);
2976 COPY(gregs[10]); COPY(gregs[11]);
2977 COPY(gregs[12]); COPY(gregs[13]);
2978 COPY(gregs[14]); COPY(gregs[15]);
2979 COPY(gbr); COPY(mach);
2980 COPY(macl); COPY(pr);
2981 COPY(sr); COPY(pc);
2982 #undef COPY
2984 for (i=0; i<16; i++) {
2985 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2987 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2988 err |= __put_user(regs->fpul, &sc->sc_fpul);
2990 /* non-iBCS2 extensions.. */
2991 err |= __put_user(mask, &sc->oldmask);
2993 return err;
2996 static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
2997 target_ulong *r0_p)
2999 unsigned int err = 0;
3000 int i;
3002 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3003 COPY(gregs[1]);
3004 COPY(gregs[2]); COPY(gregs[3]);
3005 COPY(gregs[4]); COPY(gregs[5]);
3006 COPY(gregs[6]); COPY(gregs[7]);
3007 COPY(gregs[8]); COPY(gregs[9]);
3008 COPY(gregs[10]); COPY(gregs[11]);
3009 COPY(gregs[12]); COPY(gregs[13]);
3010 COPY(gregs[14]); COPY(gregs[15]);
3011 COPY(gbr); COPY(mach);
3012 COPY(macl); COPY(pr);
3013 COPY(sr); COPY(pc);
3014 #undef COPY
3016 for (i=0; i<16; i++) {
3017 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3019 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3020 err |= __get_user(regs->fpul, &sc->sc_fpul);
3022 regs->tra = -1; /* disable syscall checks */
3023 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3024 return err;
3027 static void setup_frame(int sig, struct target_sigaction *ka,
3028 target_sigset_t *set, CPUState *regs)
3030 struct target_sigframe *frame;
3031 abi_ulong frame_addr;
3032 int i;
3033 int err = 0;
3034 int signal;
3036 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3037 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3038 goto give_sigsegv;
3040 signal = current_exec_domain_sig(sig);
3042 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3044 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3045 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3048 /* Set up to return from userspace. If provided, use a stub
3049 already in userspace. */
3050 if (ka->sa_flags & TARGET_SA_RESTORER) {
3051 regs->pr = (unsigned long) ka->sa_restorer;
3052 } else {
3053 /* Generate return code (system call to sigreturn) */
3054 err |= __put_user(MOVW(2), &frame->retcode[0]);
3055 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3056 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3057 regs->pr = (unsigned long) frame->retcode;
3060 if (err)
3061 goto give_sigsegv;
3063 /* Set up registers for signal handler */
3064 regs->gregs[15] = (unsigned long) frame;
3065 regs->gregs[4] = signal; /* Arg for signal handler */
3066 regs->gregs[5] = 0;
3067 regs->gregs[6] = (unsigned long) &frame->sc;
3068 regs->pc = (unsigned long) ka->_sa_handler;
3070 unlock_user_struct(frame, frame_addr, 1);
3071 return;
3073 give_sigsegv:
3074 unlock_user_struct(frame, frame_addr, 1);
3075 force_sig(TARGET_SIGSEGV);
3078 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3079 target_siginfo_t *info,
3080 target_sigset_t *set, CPUState *regs)
3082 struct target_rt_sigframe *frame;
3083 abi_ulong frame_addr;
3084 int i;
3085 int err = 0;
3086 int signal;
3088 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3089 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3090 goto give_sigsegv;
3092 signal = current_exec_domain_sig(sig);
3094 err |= copy_siginfo_to_user(&frame->info, info);
3096 /* Create the ucontext. */
3097 err |= __put_user(0, &frame->uc.tuc_flags);
3098 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3099 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3100 &frame->uc.tuc_stack.ss_sp);
3101 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3102 &frame->uc.tuc_stack.ss_flags);
3103 err |= __put_user(target_sigaltstack_used.ss_size,
3104 &frame->uc.tuc_stack.ss_size);
3105 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3106 regs, set->sig[0]);
3107 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3108 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3111 /* Set up to return from userspace. If provided, use a stub
3112 already in userspace. */
3113 if (ka->sa_flags & TARGET_SA_RESTORER) {
3114 regs->pr = (unsigned long) ka->sa_restorer;
3115 } else {
3116 /* Generate return code (system call to sigreturn) */
3117 err |= __put_user(MOVW(2), &frame->retcode[0]);
3118 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3119 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3120 regs->pr = (unsigned long) frame->retcode;
3123 if (err)
3124 goto give_sigsegv;
3126 /* Set up registers for signal handler */
3127 regs->gregs[15] = (unsigned long) frame;
3128 regs->gregs[4] = signal; /* Arg for signal handler */
3129 regs->gregs[5] = (unsigned long) &frame->info;
3130 regs->gregs[6] = (unsigned long) &frame->uc;
3131 regs->pc = (unsigned long) ka->_sa_handler;
3133 unlock_user_struct(frame, frame_addr, 1);
3134 return;
3136 give_sigsegv:
3137 unlock_user_struct(frame, frame_addr, 1);
3138 force_sig(TARGET_SIGSEGV);
3141 long do_sigreturn(CPUState *regs)
3143 struct target_sigframe *frame;
3144 abi_ulong frame_addr;
3145 sigset_t blocked;
3146 target_sigset_t target_set;
3147 target_ulong r0;
3148 int i;
3149 int err = 0;
3151 #if defined(DEBUG_SIGNAL)
3152 fprintf(stderr, "do_sigreturn\n");
3153 #endif
3154 frame_addr = regs->gregs[15];
3155 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3156 goto badframe;
3158 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3159 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3160 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3163 if (err)
3164 goto badframe;
3166 target_to_host_sigset_internal(&blocked, &target_set);
3167 sigprocmask(SIG_SETMASK, &blocked, NULL);
3169 if (restore_sigcontext(regs, &frame->sc, &r0))
3170 goto badframe;
3172 unlock_user_struct(frame, frame_addr, 0);
3173 return r0;
3175 badframe:
3176 unlock_user_struct(frame, frame_addr, 0);
3177 force_sig(TARGET_SIGSEGV);
3178 return 0;
3181 long do_rt_sigreturn(CPUState *regs)
3183 struct target_rt_sigframe *frame;
3184 abi_ulong frame_addr;
3185 sigset_t blocked;
3186 target_ulong r0;
3188 #if defined(DEBUG_SIGNAL)
3189 fprintf(stderr, "do_rt_sigreturn\n");
3190 #endif
3191 frame_addr = regs->gregs[15];
3192 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3193 goto badframe;
3195 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3196 sigprocmask(SIG_SETMASK, &blocked, NULL);
3198 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3199 goto badframe;
3201 if (do_sigaltstack(frame_addr +
3202 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3203 0, get_sp_from_cpustate(regs)) == -EFAULT)
3204 goto badframe;
3206 unlock_user_struct(frame, frame_addr, 0);
3207 return r0;
3209 badframe:
3210 unlock_user_struct(frame, frame_addr, 0);
3211 force_sig(TARGET_SIGSEGV);
3212 return 0;
3214 #elif defined(TARGET_MICROBLAZE)
3216 struct target_sigcontext {
3217 struct target_pt_regs regs; /* needs to be first */
3218 uint32_t oldmask;
3221 struct target_stack_t {
3222 abi_ulong ss_sp;
3223 int ss_flags;
3224 unsigned int ss_size;
3227 struct target_ucontext {
3228 abi_ulong tuc_flags;
3229 abi_ulong tuc_link;
3230 struct target_stack_t tuc_stack;
3231 struct target_sigcontext tuc_mcontext;
3232 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3235 /* Signal frames. */
3236 struct target_signal_frame {
3237 struct target_ucontext uc;
3238 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3239 uint32_t tramp[2];
3242 struct rt_signal_frame {
3243 struct siginfo info;
3244 struct ucontext uc;
3245 uint32_t tramp[2];
3248 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3250 __put_user(env->regs[0], &sc->regs.r0);
3251 __put_user(env->regs[1], &sc->regs.r1);
3252 __put_user(env->regs[2], &sc->regs.r2);
3253 __put_user(env->regs[3], &sc->regs.r3);
3254 __put_user(env->regs[4], &sc->regs.r4);
3255 __put_user(env->regs[5], &sc->regs.r5);
3256 __put_user(env->regs[6], &sc->regs.r6);
3257 __put_user(env->regs[7], &sc->regs.r7);
3258 __put_user(env->regs[8], &sc->regs.r8);
3259 __put_user(env->regs[9], &sc->regs.r9);
3260 __put_user(env->regs[10], &sc->regs.r10);
3261 __put_user(env->regs[11], &sc->regs.r11);
3262 __put_user(env->regs[12], &sc->regs.r12);
3263 __put_user(env->regs[13], &sc->regs.r13);
3264 __put_user(env->regs[14], &sc->regs.r14);
3265 __put_user(env->regs[15], &sc->regs.r15);
3266 __put_user(env->regs[16], &sc->regs.r16);
3267 __put_user(env->regs[17], &sc->regs.r17);
3268 __put_user(env->regs[18], &sc->regs.r18);
3269 __put_user(env->regs[19], &sc->regs.r19);
3270 __put_user(env->regs[20], &sc->regs.r20);
3271 __put_user(env->regs[21], &sc->regs.r21);
3272 __put_user(env->regs[22], &sc->regs.r22);
3273 __put_user(env->regs[23], &sc->regs.r23);
3274 __put_user(env->regs[24], &sc->regs.r24);
3275 __put_user(env->regs[25], &sc->regs.r25);
3276 __put_user(env->regs[26], &sc->regs.r26);
3277 __put_user(env->regs[27], &sc->regs.r27);
3278 __put_user(env->regs[28], &sc->regs.r28);
3279 __put_user(env->regs[29], &sc->regs.r29);
3280 __put_user(env->regs[30], &sc->regs.r30);
3281 __put_user(env->regs[31], &sc->regs.r31);
3282 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3285 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3287 __get_user(env->regs[0], &sc->regs.r0);
3288 __get_user(env->regs[1], &sc->regs.r1);
3289 __get_user(env->regs[2], &sc->regs.r2);
3290 __get_user(env->regs[3], &sc->regs.r3);
3291 __get_user(env->regs[4], &sc->regs.r4);
3292 __get_user(env->regs[5], &sc->regs.r5);
3293 __get_user(env->regs[6], &sc->regs.r6);
3294 __get_user(env->regs[7], &sc->regs.r7);
3295 __get_user(env->regs[8], &sc->regs.r8);
3296 __get_user(env->regs[9], &sc->regs.r9);
3297 __get_user(env->regs[10], &sc->regs.r10);
3298 __get_user(env->regs[11], &sc->regs.r11);
3299 __get_user(env->regs[12], &sc->regs.r12);
3300 __get_user(env->regs[13], &sc->regs.r13);
3301 __get_user(env->regs[14], &sc->regs.r14);
3302 __get_user(env->regs[15], &sc->regs.r15);
3303 __get_user(env->regs[16], &sc->regs.r16);
3304 __get_user(env->regs[17], &sc->regs.r17);
3305 __get_user(env->regs[18], &sc->regs.r18);
3306 __get_user(env->regs[19], &sc->regs.r19);
3307 __get_user(env->regs[20], &sc->regs.r20);
3308 __get_user(env->regs[21], &sc->regs.r21);
3309 __get_user(env->regs[22], &sc->regs.r22);
3310 __get_user(env->regs[23], &sc->regs.r23);
3311 __get_user(env->regs[24], &sc->regs.r24);
3312 __get_user(env->regs[25], &sc->regs.r25);
3313 __get_user(env->regs[26], &sc->regs.r26);
3314 __get_user(env->regs[27], &sc->regs.r27);
3315 __get_user(env->regs[28], &sc->regs.r28);
3316 __get_user(env->regs[29], &sc->regs.r29);
3317 __get_user(env->regs[30], &sc->regs.r30);
3318 __get_user(env->regs[31], &sc->regs.r31);
3319 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3322 static abi_ulong get_sigframe(struct target_sigaction *ka,
3323 CPUState *env, int frame_size)
3325 abi_ulong sp = env->regs[1];
3327 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3328 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3330 return ((sp - frame_size) & -8UL);
3333 static void setup_frame(int sig, struct target_sigaction *ka,
3334 target_sigset_t *set, CPUState *env)
3336 struct target_signal_frame *frame;
3337 abi_ulong frame_addr;
3338 int err = 0;
3339 int i;
3341 frame_addr = get_sigframe(ka, env, sizeof *frame);
3342 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3343 goto badframe;
3345 /* Save the mask. */
3346 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3347 if (err)
3348 goto badframe;
3350 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3351 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3352 goto badframe;
3355 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3357 /* Set up to return from userspace. If provided, use a stub
3358 already in userspace. */
3359 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3360 if (ka->sa_flags & TARGET_SA_RESTORER) {
3361 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3362 } else {
3363 uint32_t t;
3364 /* Note, these encodings are _big endian_! */
3365 /* addi r12, r0, __NR_sigreturn */
3366 t = 0x31800000UL | TARGET_NR_sigreturn;
3367 err |= __put_user(t, frame->tramp + 0);
3368 /* brki r14, 0x8 */
3369 t = 0xb9cc0008UL;
3370 err |= __put_user(t, frame->tramp + 1);
3372 /* Return from sighandler will jump to the tramp.
3373 Negative 8 offset because return is rtsd r15, 8 */
3374 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3377 if (err)
3378 goto badframe;
3380 /* Set up registers for signal handler */
3381 env->regs[1] = (unsigned long) frame;
3382 /* Signal handler args: */
3383 env->regs[5] = sig; /* Arg 0: signum */
3384 env->regs[6] = 0;
3385 env->regs[7] = (unsigned long) &frame->uc; /* arg 1: sigcontext */
3387 /* Offset of 4 to handle microblaze rtid r14, 0 */
3388 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3390 unlock_user_struct(frame, frame_addr, 1);
3391 return;
3392 badframe:
3393 unlock_user_struct(frame, frame_addr, 1);
3394 force_sig(TARGET_SIGSEGV);
3397 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3398 target_siginfo_t *info,
3399 target_sigset_t *set, CPUState *env)
3401 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3404 long do_sigreturn(CPUState *env)
3406 struct target_signal_frame *frame;
3407 abi_ulong frame_addr;
3408 target_sigset_t target_set;
3409 sigset_t set;
3410 int i;
3412 frame_addr = env->regs[R_SP];
3413 /* Make sure the guest isn't playing games. */
3414 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3415 goto badframe;
3417 /* Restore blocked signals */
3418 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3419 goto badframe;
3420 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3421 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3422 goto badframe;
3424 target_to_host_sigset_internal(&set, &target_set);
3425 sigprocmask(SIG_SETMASK, &set, NULL);
3427 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3428 /* We got here through a sigreturn syscall, our path back is via an
3429 rtb insn so setup r14 for that. */
3430 env->regs[14] = env->sregs[SR_PC];
3432 unlock_user_struct(frame, frame_addr, 0);
3433 return env->regs[10];
3434 badframe:
3435 unlock_user_struct(frame, frame_addr, 0);
3436 force_sig(TARGET_SIGSEGV);
3439 long do_rt_sigreturn(CPUState *env)
3441 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3442 return -TARGET_ENOSYS;
3445 #elif defined(TARGET_CRIS)
3447 struct target_sigcontext {
3448 struct target_pt_regs regs; /* needs to be first */
3449 uint32_t oldmask;
3450 uint32_t usp; /* usp before stacking this gunk on it */
3453 /* Signal frames. */
3454 struct target_signal_frame {
3455 struct target_sigcontext sc;
3456 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3457 uint8_t retcode[8]; /* Trampoline code. */
3460 struct rt_signal_frame {
3461 struct siginfo *pinfo;
3462 void *puc;
3463 struct siginfo info;
3464 struct ucontext uc;
3465 uint8_t retcode[8]; /* Trampoline code. */
3468 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3470 __put_user(env->regs[0], &sc->regs.r0);
3471 __put_user(env->regs[1], &sc->regs.r1);
3472 __put_user(env->regs[2], &sc->regs.r2);
3473 __put_user(env->regs[3], &sc->regs.r3);
3474 __put_user(env->regs[4], &sc->regs.r4);
3475 __put_user(env->regs[5], &sc->regs.r5);
3476 __put_user(env->regs[6], &sc->regs.r6);
3477 __put_user(env->regs[7], &sc->regs.r7);
3478 __put_user(env->regs[8], &sc->regs.r8);
3479 __put_user(env->regs[9], &sc->regs.r9);
3480 __put_user(env->regs[10], &sc->regs.r10);
3481 __put_user(env->regs[11], &sc->regs.r11);
3482 __put_user(env->regs[12], &sc->regs.r12);
3483 __put_user(env->regs[13], &sc->regs.r13);
3484 __put_user(env->regs[14], &sc->usp);
3485 __put_user(env->regs[15], &sc->regs.acr);
3486 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3487 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3488 __put_user(env->pc, &sc->regs.erp);
3491 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3493 __get_user(env->regs[0], &sc->regs.r0);
3494 __get_user(env->regs[1], &sc->regs.r1);
3495 __get_user(env->regs[2], &sc->regs.r2);
3496 __get_user(env->regs[3], &sc->regs.r3);
3497 __get_user(env->regs[4], &sc->regs.r4);
3498 __get_user(env->regs[5], &sc->regs.r5);
3499 __get_user(env->regs[6], &sc->regs.r6);
3500 __get_user(env->regs[7], &sc->regs.r7);
3501 __get_user(env->regs[8], &sc->regs.r8);
3502 __get_user(env->regs[9], &sc->regs.r9);
3503 __get_user(env->regs[10], &sc->regs.r10);
3504 __get_user(env->regs[11], &sc->regs.r11);
3505 __get_user(env->regs[12], &sc->regs.r12);
3506 __get_user(env->regs[13], &sc->regs.r13);
3507 __get_user(env->regs[14], &sc->usp);
3508 __get_user(env->regs[15], &sc->regs.acr);
3509 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3510 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3511 __get_user(env->pc, &sc->regs.erp);
3514 static abi_ulong get_sigframe(CPUState *env, int framesize)
3516 abi_ulong sp;
3517 /* Align the stack downwards to 4. */
3518 sp = (env->regs[R_SP] & ~3);
3519 return sp - framesize;
3522 static void setup_frame(int sig, struct target_sigaction *ka,
3523 target_sigset_t *set, CPUState *env)
3525 struct target_signal_frame *frame;
3526 abi_ulong frame_addr;
3527 int err = 0;
3528 int i;
3530 frame_addr = get_sigframe(env, sizeof *frame);
3531 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3532 goto badframe;
3535 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3536 * use this trampoline anymore but it sets it up for GDB.
3537 * In QEMU, using the trampoline simplifies things a bit so we use it.
3539 * This is movu.w __NR_sigreturn, r9; break 13;
3541 err |= __put_user(0x9c5f, frame->retcode+0);
3542 err |= __put_user(TARGET_NR_sigreturn,
3543 frame->retcode+2);
3544 err |= __put_user(0xe93d, frame->retcode+4);
3546 /* Save the mask. */
3547 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3548 if (err)
3549 goto badframe;
3551 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3552 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3553 goto badframe;
3556 setup_sigcontext(&frame->sc, env);
3558 /* Move the stack and setup the arguments for the handler. */
3559 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
3560 env->regs[10] = sig;
3561 env->pc = (unsigned long) ka->_sa_handler;
3562 /* Link SRP so the guest returns through the trampoline. */
3563 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
3565 unlock_user_struct(frame, frame_addr, 1);
3566 return;
3567 badframe:
3568 unlock_user_struct(frame, frame_addr, 1);
3569 force_sig(TARGET_SIGSEGV);
3572 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3573 target_siginfo_t *info,
3574 target_sigset_t *set, CPUState *env)
3576 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3579 long do_sigreturn(CPUState *env)
3581 struct target_signal_frame *frame;
3582 abi_ulong frame_addr;
3583 target_sigset_t target_set;
3584 sigset_t set;
3585 int i;
3587 frame_addr = env->regs[R_SP];
3588 /* Make sure the guest isn't playing games. */
3589 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3590 goto badframe;
3592 /* Restore blocked signals */
3593 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3594 goto badframe;
3595 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3596 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3597 goto badframe;
3599 target_to_host_sigset_internal(&set, &target_set);
3600 sigprocmask(SIG_SETMASK, &set, NULL);
3602 restore_sigcontext(&frame->sc, env);
3603 unlock_user_struct(frame, frame_addr, 0);
3604 return env->regs[10];
3605 badframe:
3606 unlock_user_struct(frame, frame_addr, 0);
3607 force_sig(TARGET_SIGSEGV);
3610 long do_rt_sigreturn(CPUState *env)
3612 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3613 return -TARGET_ENOSYS;
3616 #elif defined(TARGET_S390X)
3618 #define __NUM_GPRS 16
3619 #define __NUM_FPRS 16
3620 #define __NUM_ACRS 16
3622 #define S390_SYSCALL_SIZE 2
3623 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3625 #define _SIGCONTEXT_NSIG 64
3626 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3627 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3628 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3629 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
3630 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3632 typedef struct {
3633 target_psw_t psw;
3634 target_ulong gprs[__NUM_GPRS];
3635 unsigned int acrs[__NUM_ACRS];
3636 } target_s390_regs_common;
3638 typedef struct {
3639 unsigned int fpc;
3640 double fprs[__NUM_FPRS];
3641 } target_s390_fp_regs;
3643 typedef struct {
3644 target_s390_regs_common regs;
3645 target_s390_fp_regs fpregs;
3646 } target_sigregs;
3648 struct target_sigcontext {
3649 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
3650 target_sigregs *sregs;
3653 typedef struct {
3654 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3655 struct target_sigcontext sc;
3656 target_sigregs sregs;
3657 int signo;
3658 uint8_t retcode[S390_SYSCALL_SIZE];
3659 } sigframe;
3661 struct target_ucontext {
3662 target_ulong uc_flags;
3663 struct target_ucontext *uc_link;
3664 target_stack_t uc_stack;
3665 target_sigregs uc_mcontext;
3666 target_sigset_t uc_sigmask; /* mask last for extensibility */
3669 typedef struct {
3670 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3671 uint8_t retcode[S390_SYSCALL_SIZE];
3672 struct target_siginfo info;
3673 struct target_ucontext uc;
3674 } rt_sigframe;
3676 static inline abi_ulong
3677 get_sigframe(struct target_sigaction *ka, CPUState *env, size_t frame_size)
3679 abi_ulong sp;
3681 /* Default to using normal stack */
3682 sp = env->regs[15];
3684 /* This is the X/Open sanctioned signal stack switching. */
3685 if (ka->sa_flags & TARGET_SA_ONSTACK) {
3686 if (!sas_ss_flags(sp)) {
3687 sp = target_sigaltstack_used.ss_sp +
3688 target_sigaltstack_used.ss_size;
3692 /* This is the legacy signal stack switching. */
3693 else if (/* FIXME !user_mode(regs) */ 0 &&
3694 !(ka->sa_flags & TARGET_SA_RESTORER) &&
3695 ka->sa_restorer) {
3696 sp = (abi_ulong) ka->sa_restorer;
3699 return (sp - frame_size) & -8ul;
3702 static void save_sigregs(CPUState *env, target_sigregs *sregs)
3704 int i;
3705 //save_access_regs(current->thread.acrs); FIXME
3707 /* Copy a 'clean' PSW mask to the user to avoid leaking
3708 information about whether PER is currently on. */
3709 __put_user(env->psw.mask, &sregs->regs.psw.mask);
3710 __put_user(env->psw.addr, &sregs->regs.psw.addr);
3711 for (i = 0; i < 16; i++) {
3712 __put_user(env->regs[i], &sregs->regs.gprs[i]);
3714 for (i = 0; i < 16; i++) {
3715 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3718 * We have to store the fp registers to current->thread.fp_regs
3719 * to merge them with the emulated registers.
3721 //save_fp_regs(&current->thread.fp_regs); FIXME
3722 for (i = 0; i < 16; i++) {
3723 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3727 static void setup_frame(int sig, struct target_sigaction *ka,
3728 target_sigset_t *set, CPUState *env)
3730 sigframe *frame;
3731 abi_ulong frame_addr;
3733 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3734 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3735 (unsigned long long)frame_addr);
3736 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3737 goto give_sigsegv;
3740 qemu_log("%s: 1\n", __FUNCTION__);
3741 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
3742 goto give_sigsegv;
3745 save_sigregs(env, &frame->sregs);
3747 __put_user((abi_ulong)(unsigned long)&frame->sregs,
3748 (abi_ulong *)&frame->sc.sregs);
3750 /* Set up to return from userspace. If provided, use a stub
3751 already in userspace. */
3752 if (ka->sa_flags & TARGET_SA_RESTORER) {
3753 env->regs[14] = (unsigned long)
3754 ka->sa_restorer | PSW_ADDR_AMODE;
3755 } else {
3756 env->regs[14] = (unsigned long)
3757 frame->retcode | PSW_ADDR_AMODE;
3758 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
3759 (uint16_t *)(frame->retcode)))
3760 goto give_sigsegv;
3763 /* Set up backchain. */
3764 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3765 goto give_sigsegv;
3768 /* Set up registers for signal handler */
3769 env->regs[15] = (target_ulong)(unsigned long) frame;
3770 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3772 env->regs[2] = sig; //map_signal(sig);
3773 env->regs[3] = (target_ulong)(unsigned long) &frame->sc;
3775 /* We forgot to include these in the sigcontext.
3776 To avoid breaking binary compatibility, they are passed as args. */
3777 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
3778 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
3780 /* Place signal number on stack to allow backtrace from handler. */
3781 if (__put_user(env->regs[2], (int *) &frame->signo)) {
3782 goto give_sigsegv;
3784 unlock_user_struct(frame, frame_addr, 1);
3785 return;
3787 give_sigsegv:
3788 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3789 unlock_user_struct(frame, frame_addr, 1);
3790 force_sig(TARGET_SIGSEGV);
3793 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3794 target_siginfo_t *info,
3795 target_sigset_t *set, CPUState *env)
3797 int i;
3798 rt_sigframe *frame;
3799 abi_ulong frame_addr;
3801 frame_addr = get_sigframe(ka, env, sizeof *frame);
3802 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3803 (unsigned long long)frame_addr);
3804 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3805 goto give_sigsegv;
3808 qemu_log("%s: 1\n", __FUNCTION__);
3809 if (copy_siginfo_to_user(&frame->info, info)) {
3810 goto give_sigsegv;
3813 /* Create the ucontext. */
3814 __put_user(0, &frame->uc.uc_flags);
3815 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.uc_link);
3816 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.uc_stack.ss_sp);
3817 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3818 &frame->uc.uc_stack.ss_flags);
3819 __put_user(target_sigaltstack_used.ss_size, &frame->uc.uc_stack.ss_size);
3820 save_sigregs(env, &frame->uc.uc_mcontext);
3821 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
3822 __put_user((abi_ulong)set->sig[i],
3823 (abi_ulong *)&frame->uc.uc_sigmask.sig[i]);
3826 /* Set up to return from userspace. If provided, use a stub
3827 already in userspace. */
3828 if (ka->sa_flags & TARGET_SA_RESTORER) {
3829 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
3830 } else {
3831 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
3832 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
3833 (uint16_t *)(frame->retcode))) {
3834 goto give_sigsegv;
3838 /* Set up backchain. */
3839 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3840 goto give_sigsegv;
3843 /* Set up registers for signal handler */
3844 env->regs[15] = (target_ulong)(unsigned long) frame;
3845 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3847 env->regs[2] = sig; //map_signal(sig);
3848 env->regs[3] = (target_ulong)(unsigned long) &frame->info;
3849 env->regs[4] = (target_ulong)(unsigned long) &frame->uc;
3850 return;
3852 give_sigsegv:
3853 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3854 unlock_user_struct(frame, frame_addr, 1);
3855 force_sig(TARGET_SIGSEGV);
3858 static int
3859 restore_sigregs(CPUState *env, target_sigregs *sc)
3861 int err = 0;
3862 int i;
3864 for (i = 0; i < 16; i++) {
3865 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
3868 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
3869 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
3870 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
3871 (unsigned long long)env->psw.addr);
3872 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
3873 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
3875 for (i = 0; i < 16; i++) {
3876 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
3878 for (i = 0; i < 16; i++) {
3879 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
3882 return err;
3885 long do_sigreturn(CPUState *env)
3887 sigframe *frame;
3888 abi_ulong frame_addr = env->regs[15];
3889 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3890 (unsigned long long)frame_addr);
3891 target_sigset_t target_set;
3892 sigset_t set;
3894 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3895 goto badframe;
3897 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
3898 goto badframe;
3901 target_to_host_sigset_internal(&set, &target_set);
3902 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
3904 if (restore_sigregs(env, &frame->sregs)) {
3905 goto badframe;
3908 unlock_user_struct(frame, frame_addr, 0);
3909 return env->regs[2];
3911 badframe:
3912 unlock_user_struct(frame, frame_addr, 0);
3913 force_sig(TARGET_SIGSEGV);
3914 return 0;
3917 long do_rt_sigreturn(CPUState *env)
3919 rt_sigframe *frame;
3920 abi_ulong frame_addr = env->regs[15];
3921 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3922 (unsigned long long)frame_addr);
3923 sigset_t set;
3925 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3926 goto badframe;
3928 target_to_host_sigset(&set, &frame->uc.uc_sigmask);
3930 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
3932 if (restore_sigregs(env, &frame->uc.uc_mcontext)) {
3933 goto badframe;
3936 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.uc_stack), 0,
3937 get_sp_from_cpustate(env)) == -EFAULT) {
3938 goto badframe;
3940 unlock_user_struct(frame, frame_addr, 0);
3941 return env->regs[2];
3943 badframe:
3944 unlock_user_struct(frame, frame_addr, 0);
3945 force_sig(TARGET_SIGSEGV);
3946 return 0;
3949 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3951 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
3952 the signal handling is different enough that we haven't implemented
3953 support for PPC64 yet. Hence the restriction above.
3955 There are various #if'd blocks for code for TARGET_PPC64. These
3956 blocks should go away so that we can successfully run 32-bit and
3957 64-bit binaries on a QEMU configured for PPC64. */
3959 /* Size of dummy stack frame allocated when calling signal handler.
3960 See arch/powerpc/include/asm/ptrace.h. */
3961 #if defined(TARGET_PPC64)
3962 #define SIGNAL_FRAMESIZE 128
3963 #else
3964 #define SIGNAL_FRAMESIZE 64
3965 #endif
3967 /* See arch/powerpc/include/asm/sigcontext.h. */
3968 struct target_sigcontext {
3969 target_ulong _unused[4];
3970 int32_t signal;
3971 #if defined(TARGET_PPC64)
3972 int32_t pad0;
3973 #endif
3974 target_ulong handler;
3975 target_ulong oldmask;
3976 target_ulong regs; /* struct pt_regs __user * */
3977 /* TODO: PPC64 includes extra bits here. */
3980 /* Indices for target_mcontext.mc_gregs, below.
3981 See arch/powerpc/include/asm/ptrace.h for details. */
3982 enum {
3983 TARGET_PT_R0 = 0,
3984 TARGET_PT_R1 = 1,
3985 TARGET_PT_R2 = 2,
3986 TARGET_PT_R3 = 3,
3987 TARGET_PT_R4 = 4,
3988 TARGET_PT_R5 = 5,
3989 TARGET_PT_R6 = 6,
3990 TARGET_PT_R7 = 7,
3991 TARGET_PT_R8 = 8,
3992 TARGET_PT_R9 = 9,
3993 TARGET_PT_R10 = 10,
3994 TARGET_PT_R11 = 11,
3995 TARGET_PT_R12 = 12,
3996 TARGET_PT_R13 = 13,
3997 TARGET_PT_R14 = 14,
3998 TARGET_PT_R15 = 15,
3999 TARGET_PT_R16 = 16,
4000 TARGET_PT_R17 = 17,
4001 TARGET_PT_R18 = 18,
4002 TARGET_PT_R19 = 19,
4003 TARGET_PT_R20 = 20,
4004 TARGET_PT_R21 = 21,
4005 TARGET_PT_R22 = 22,
4006 TARGET_PT_R23 = 23,
4007 TARGET_PT_R24 = 24,
4008 TARGET_PT_R25 = 25,
4009 TARGET_PT_R26 = 26,
4010 TARGET_PT_R27 = 27,
4011 TARGET_PT_R28 = 28,
4012 TARGET_PT_R29 = 29,
4013 TARGET_PT_R30 = 30,
4014 TARGET_PT_R31 = 31,
4015 TARGET_PT_NIP = 32,
4016 TARGET_PT_MSR = 33,
4017 TARGET_PT_ORIG_R3 = 34,
4018 TARGET_PT_CTR = 35,
4019 TARGET_PT_LNK = 36,
4020 TARGET_PT_XER = 37,
4021 TARGET_PT_CCR = 38,
4022 /* Yes, there are two registers with #39. One is 64-bit only. */
4023 TARGET_PT_MQ = 39,
4024 TARGET_PT_SOFTE = 39,
4025 TARGET_PT_TRAP = 40,
4026 TARGET_PT_DAR = 41,
4027 TARGET_PT_DSISR = 42,
4028 TARGET_PT_RESULT = 43,
4029 TARGET_PT_REGS_COUNT = 44
4032 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4033 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4034 struct target_mcontext {
4035 target_ulong mc_gregs[48];
4036 /* Includes fpscr. */
4037 uint64_t mc_fregs[33];
4038 target_ulong mc_pad[2];
4039 /* We need to handle Altivec and SPE at the same time, which no
4040 kernel needs to do. Fortunately, the kernel defines this bit to
4041 be Altivec-register-large all the time, rather than trying to
4042 twiddle it based on the specific platform. */
4043 union {
4044 /* SPE vector registers. One extra for SPEFSCR. */
4045 uint32_t spe[33];
4046 /* Altivec vector registers. The packing of VSCR and VRSAVE
4047 varies depending on whether we're PPC64 or not: PPC64 splits
4048 them apart; PPC32 stuffs them together. */
4049 #if defined(TARGET_PPC64)
4050 #define QEMU_NVRREG 34
4051 #else
4052 #define QEMU_NVRREG 33
4053 #endif
4054 ppc_avr_t altivec[QEMU_NVRREG];
4055 #undef QEMU_NVRREG
4056 } mc_vregs __attribute__((__aligned__(16)));
4059 struct target_ucontext {
4060 target_ulong tuc_flags;
4061 target_ulong tuc_link; /* struct ucontext __user * */
4062 struct target_sigaltstack tuc_stack;
4063 #if !defined(TARGET_PPC64)
4064 int32_t tuc_pad[7];
4065 target_ulong tuc_regs; /* struct mcontext __user *
4066 points to uc_mcontext field */
4067 #endif
4068 target_sigset_t tuc_sigmask;
4069 #if defined(TARGET_PPC64)
4070 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4071 struct target_sigcontext tuc_mcontext;
4072 #else
4073 int32_t tuc_maskext[30];
4074 int32_t tuc_pad2[3];
4075 struct target_mcontext tuc_mcontext;
4076 #endif
4079 /* See arch/powerpc/kernel/signal_32.c. */
4080 struct target_sigframe {
4081 struct target_sigcontext sctx;
4082 struct target_mcontext mctx;
4083 int32_t abigap[56];
4086 struct target_rt_sigframe {
4087 struct target_siginfo info;
4088 struct target_ucontext uc;
4089 int32_t abigap[56];
4092 /* We use the mc_pad field for the signal return trampoline. */
4093 #define tramp mc_pad
4095 /* See arch/powerpc/kernel/signal.c. */
4096 static target_ulong get_sigframe(struct target_sigaction *ka,
4097 CPUState *env,
4098 int frame_size)
4100 target_ulong oldsp, newsp;
4102 oldsp = env->gpr[1];
4104 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4105 (sas_ss_flags(oldsp))) {
4106 oldsp = (target_sigaltstack_used.ss_sp
4107 + target_sigaltstack_used.ss_size);
4110 newsp = (oldsp - frame_size) & ~0xFUL;
4112 return newsp;
4115 static int save_user_regs(CPUState *env, struct target_mcontext *frame,
4116 int sigret)
4118 target_ulong msr = env->msr;
4119 int i;
4120 target_ulong ccr = 0;
4122 /* In general, the kernel attempts to be intelligent about what it
4123 needs to save for Altivec/FP/SPE registers. We don't care that
4124 much, so we just go ahead and save everything. */
4126 /* Save general registers. */
4127 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4128 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4129 return 1;
4132 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4133 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4134 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4135 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4136 return 1;
4138 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4139 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4141 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4142 return 1;
4144 /* Save Altivec registers if necessary. */
4145 if (env->insns_flags & PPC_ALTIVEC) {
4146 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4147 ppc_avr_t *avr = &env->avr[i];
4148 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4150 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4151 __put_user(avr->u64[1], &vreg->u64[1])) {
4152 return 1;
4155 /* Set MSR_VR in the saved MSR value to indicate that
4156 frame->mc_vregs contains valid data. */
4157 msr |= MSR_VR;
4158 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4159 &frame->mc_vregs.altivec[32].u32[3]))
4160 return 1;
4163 /* Save floating point registers. */
4164 if (env->insns_flags & PPC_FLOAT) {
4165 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4166 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4167 return 1;
4170 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4171 return 1;
4174 /* Save SPE registers. The kernel only saves the high half. */
4175 if (env->insns_flags & PPC_SPE) {
4176 #if defined(TARGET_PPC64)
4177 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4178 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4179 return 1;
4182 #else
4183 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4184 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4185 return 1;
4188 #endif
4189 /* Set MSR_SPE in the saved MSR value to indicate that
4190 frame->mc_vregs contains valid data. */
4191 msr |= MSR_SPE;
4192 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4193 return 1;
4196 /* Store MSR. */
4197 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4198 return 1;
4200 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4201 if (sigret) {
4202 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4203 __put_user(0x44000002UL, &frame->tramp[1])) {
4204 return 1;
4208 return 0;
4211 static int restore_user_regs(CPUState *env,
4212 struct target_mcontext *frame, int sig)
4214 target_ulong save_r2 = 0;
4215 target_ulong msr;
4216 target_ulong ccr;
4218 int i;
4220 if (!sig) {
4221 save_r2 = env->gpr[2];
4224 /* Restore general registers. */
4225 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4226 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4227 return 1;
4230 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4231 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4232 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4233 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4234 return 1;
4235 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4236 return 1;
4238 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4239 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4242 if (!sig) {
4243 env->gpr[2] = save_r2;
4245 /* Restore MSR. */
4246 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4247 return 1;
4249 /* If doing signal return, restore the previous little-endian mode. */
4250 if (sig)
4251 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4253 /* Restore Altivec registers if necessary. */
4254 if (env->insns_flags & PPC_ALTIVEC) {
4255 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4256 ppc_avr_t *avr = &env->avr[i];
4257 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4259 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4260 __get_user(avr->u64[1], &vreg->u64[1])) {
4261 return 1;
4264 /* Set MSR_VEC in the saved MSR value to indicate that
4265 frame->mc_vregs contains valid data. */
4266 if (__get_user(env->spr[SPR_VRSAVE],
4267 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4268 return 1;
4271 /* Restore floating point registers. */
4272 if (env->insns_flags & PPC_FLOAT) {
4273 uint64_t fpscr;
4274 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4275 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4276 return 1;
4279 if (__get_user(fpscr, &frame->mc_fregs[32]))
4280 return 1;
4281 env->fpscr = (uint32_t) fpscr;
4284 /* Save SPE registers. The kernel only saves the high half. */
4285 if (env->insns_flags & PPC_SPE) {
4286 #if defined(TARGET_PPC64)
4287 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4288 uint32_t hi;
4290 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4291 return 1;
4293 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4295 #else
4296 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4297 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4298 return 1;
4301 #endif
4302 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4303 return 1;
4306 return 0;
4309 static void setup_frame(int sig, struct target_sigaction *ka,
4310 target_sigset_t *set, CPUState *env)
4312 struct target_sigframe *frame;
4313 struct target_sigcontext *sc;
4314 target_ulong frame_addr, newsp;
4315 int err = 0;
4316 int signal;
4318 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4319 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4320 goto sigsegv;
4321 sc = &frame->sctx;
4323 signal = current_exec_domain_sig(sig);
4325 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
4326 err |= __put_user(set->sig[0], &sc->oldmask);
4327 #if defined(TARGET_PPC64)
4328 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4329 #else
4330 err |= __put_user(set->sig[1], &sc->_unused[3]);
4331 #endif
4332 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4333 err |= __put_user(sig, &sc->signal);
4335 /* Save user regs. */
4336 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4338 /* The kernel checks for the presence of a VDSO here. We don't
4339 emulate a vdso, so use a sigreturn system call. */
4340 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4342 /* Turn off all fp exceptions. */
4343 env->fpscr = 0;
4345 /* Create a stack frame for the caller of the handler. */
4346 newsp = frame_addr - SIGNAL_FRAMESIZE;
4347 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4349 if (err)
4350 goto sigsegv;
4352 /* Set up registers for signal handler. */
4353 env->gpr[1] = newsp;
4354 env->gpr[3] = signal;
4355 env->gpr[4] = (target_ulong) h2g(sc);
4356 env->nip = (target_ulong) ka->_sa_handler;
4357 /* Signal handlers are entered in big-endian mode. */
4358 env->msr &= ~MSR_LE;
4360 unlock_user_struct(frame, frame_addr, 1);
4361 return;
4363 sigsegv:
4364 unlock_user_struct(frame, frame_addr, 1);
4365 if (logfile)
4366 fprintf (logfile, "segfaulting from setup_frame\n");
4367 force_sig(TARGET_SIGSEGV);
4370 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4371 target_siginfo_t *info,
4372 target_sigset_t *set, CPUState *env)
4374 struct target_rt_sigframe *rt_sf;
4375 struct target_mcontext *frame;
4376 target_ulong rt_sf_addr, newsp = 0;
4377 int i, err = 0;
4378 int signal;
4380 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4381 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4382 goto sigsegv;
4384 signal = current_exec_domain_sig(sig);
4386 err |= copy_siginfo_to_user(&rt_sf->info, info);
4388 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4389 err |= __put_user(0, &rt_sf->uc.tuc_link);
4390 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4391 &rt_sf->uc.tuc_stack.ss_sp);
4392 err |= __put_user(sas_ss_flags(env->gpr[1]),
4393 &rt_sf->uc.tuc_stack.ss_flags);
4394 err |= __put_user(target_sigaltstack_used.ss_size,
4395 &rt_sf->uc.tuc_stack.ss_size);
4396 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4397 &rt_sf->uc.tuc_regs);
4398 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4399 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4402 frame = &rt_sf->uc.tuc_mcontext;
4403 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4405 /* The kernel checks for the presence of a VDSO here. We don't
4406 emulate a vdso, so use a sigreturn system call. */
4407 env->lr = (target_ulong) h2g(frame->tramp);
4409 /* Turn off all fp exceptions. */
4410 env->fpscr = 0;
4412 /* Create a stack frame for the caller of the handler. */
4413 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4414 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4416 if (err)
4417 goto sigsegv;
4419 /* Set up registers for signal handler. */
4420 env->gpr[1] = newsp;
4421 env->gpr[3] = (target_ulong) signal;
4422 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4423 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4424 env->gpr[6] = (target_ulong) h2g(rt_sf);
4425 env->nip = (target_ulong) ka->_sa_handler;
4426 /* Signal handlers are entered in big-endian mode. */
4427 env->msr &= ~MSR_LE;
4429 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4430 return;
4432 sigsegv:
4433 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4434 if (logfile)
4435 fprintf (logfile, "segfaulting from setup_rt_frame\n");
4436 force_sig(TARGET_SIGSEGV);
4440 long do_sigreturn(CPUState *env)
4442 struct target_sigcontext *sc = NULL;
4443 struct target_mcontext *sr = NULL;
4444 target_ulong sr_addr, sc_addr;
4445 sigset_t blocked;
4446 target_sigset_t set;
4448 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4449 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4450 goto sigsegv;
4452 #if defined(TARGET_PPC64)
4453 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4454 #else
4455 if(__get_user(set.sig[0], &sc->oldmask) ||
4456 __get_user(set.sig[1], &sc->_unused[3]))
4457 goto sigsegv;
4458 #endif
4459 target_to_host_sigset_internal(&blocked, &set);
4460 sigprocmask(SIG_SETMASK, &blocked, NULL);
4462 if (__get_user(sr_addr, &sc->regs))
4463 goto sigsegv;
4464 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4465 goto sigsegv;
4466 if (restore_user_regs(env, sr, 1))
4467 goto sigsegv;
4469 unlock_user_struct(sr, sr_addr, 1);
4470 unlock_user_struct(sc, sc_addr, 1);
4471 return -TARGET_QEMU_ESIGRETURN;
4473 sigsegv:
4474 unlock_user_struct(sr, sr_addr, 1);
4475 unlock_user_struct(sc, sc_addr, 1);
4476 if (logfile)
4477 fprintf (logfile, "segfaulting from do_sigreturn\n");
4478 force_sig(TARGET_SIGSEGV);
4479 return 0;
4482 /* See arch/powerpc/kernel/signal_32.c. */
4483 static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
4485 struct target_mcontext *mcp;
4486 target_ulong mcp_addr;
4487 sigset_t blocked;
4488 target_sigset_t set;
4490 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4491 sizeof (set)))
4492 return 1;
4494 #if defined(TARGET_PPC64)
4495 fprintf (stderr, "do_setcontext: not implemented\n");
4496 return 0;
4497 #else
4498 if (__get_user(mcp_addr, &ucp->tuc_regs))
4499 return 1;
4501 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4502 return 1;
4504 target_to_host_sigset_internal(&blocked, &set);
4505 sigprocmask(SIG_SETMASK, &blocked, NULL);
4506 if (restore_user_regs(env, mcp, sig))
4507 goto sigsegv;
4509 unlock_user_struct(mcp, mcp_addr, 1);
4510 return 0;
4512 sigsegv:
4513 unlock_user_struct(mcp, mcp_addr, 1);
4514 return 1;
4515 #endif
4518 long do_rt_sigreturn(CPUState *env)
4520 struct target_rt_sigframe *rt_sf = NULL;
4521 target_ulong rt_sf_addr;
4523 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4524 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4525 goto sigsegv;
4527 if (do_setcontext(&rt_sf->uc, env, 1))
4528 goto sigsegv;
4530 do_sigaltstack(rt_sf_addr
4531 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4532 0, env->gpr[1]);
4534 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4535 return -TARGET_QEMU_ESIGRETURN;
4537 sigsegv:
4538 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4539 if (logfile)
4540 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
4541 force_sig(TARGET_SIGSEGV);
4542 return 0;
4545 #elif defined(TARGET_M68K)
4547 struct target_sigcontext {
4548 abi_ulong sc_mask;
4549 abi_ulong sc_usp;
4550 abi_ulong sc_d0;
4551 abi_ulong sc_d1;
4552 abi_ulong sc_a0;
4553 abi_ulong sc_a1;
4554 unsigned short sc_sr;
4555 abi_ulong sc_pc;
4558 struct target_sigframe
4560 abi_ulong pretcode;
4561 int sig;
4562 int code;
4563 abi_ulong psc;
4564 char retcode[8];
4565 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4566 struct target_sigcontext sc;
4569 typedef int target_greg_t;
4570 #define TARGET_NGREG 18
4571 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4573 typedef struct target_fpregset {
4574 int f_fpcntl[3];
4575 int f_fpregs[8*3];
4576 } target_fpregset_t;
4578 struct target_mcontext {
4579 int version;
4580 target_gregset_t gregs;
4581 target_fpregset_t fpregs;
4584 #define TARGET_MCONTEXT_VERSION 2
4586 struct target_ucontext {
4587 abi_ulong tuc_flags;
4588 abi_ulong tuc_link;
4589 target_stack_t tuc_stack;
4590 struct target_mcontext tuc_mcontext;
4591 abi_long tuc_filler[80];
4592 target_sigset_t tuc_sigmask;
4595 struct target_rt_sigframe
4597 abi_ulong pretcode;
4598 int sig;
4599 abi_ulong pinfo;
4600 abi_ulong puc;
4601 char retcode[8];
4602 struct target_siginfo info;
4603 struct target_ucontext uc;
4606 static int
4607 setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4609 int err = 0;
4611 err |= __put_user(mask, &sc->sc_mask);
4612 err |= __put_user(env->aregs[7], &sc->sc_usp);
4613 err |= __put_user(env->dregs[0], &sc->sc_d0);
4614 err |= __put_user(env->dregs[1], &sc->sc_d1);
4615 err |= __put_user(env->aregs[0], &sc->sc_a0);
4616 err |= __put_user(env->aregs[1], &sc->sc_a1);
4617 err |= __put_user(env->sr, &sc->sc_sr);
4618 err |= __put_user(env->pc, &sc->sc_pc);
4620 return err;
4623 static int
4624 restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4626 int err = 0;
4627 int temp;
4629 err |= __get_user(env->aregs[7], &sc->sc_usp);
4630 err |= __get_user(env->dregs[1], &sc->sc_d1);
4631 err |= __get_user(env->aregs[0], &sc->sc_a0);
4632 err |= __get_user(env->aregs[1], &sc->sc_a1);
4633 err |= __get_user(env->pc, &sc->sc_pc);
4634 err |= __get_user(temp, &sc->sc_sr);
4635 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4637 *pd0 = tswapl(sc->sc_d0);
4639 return err;
4643 * Determine which stack to use..
4645 static inline abi_ulong
4646 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4648 unsigned long sp;
4650 sp = regs->aregs[7];
4652 /* This is the X/Open sanctioned signal stack switching. */
4653 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4654 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4657 return ((sp - frame_size) & -8UL);
4660 static void setup_frame(int sig, struct target_sigaction *ka,
4661 target_sigset_t *set, CPUState *env)
4663 struct target_sigframe *frame;
4664 abi_ulong frame_addr;
4665 abi_ulong retcode_addr;
4666 abi_ulong sc_addr;
4667 int err = 0;
4668 int i;
4670 frame_addr = get_sigframe(ka, env, sizeof *frame);
4671 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4672 goto give_sigsegv;
4674 err |= __put_user(sig, &frame->sig);
4676 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4677 err |= __put_user(sc_addr, &frame->psc);
4679 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4680 if (err)
4681 goto give_sigsegv;
4683 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4684 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4685 goto give_sigsegv;
4688 /* Set up to return from userspace. */
4690 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4691 err |= __put_user(retcode_addr, &frame->pretcode);
4693 /* moveq #,d0; trap #0 */
4695 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4696 (long *)(frame->retcode));
4698 if (err)
4699 goto give_sigsegv;
4701 /* Set up to return from userspace */
4703 env->aregs[7] = frame_addr;
4704 env->pc = ka->_sa_handler;
4706 unlock_user_struct(frame, frame_addr, 1);
4707 return;
4709 give_sigsegv:
4710 unlock_user_struct(frame, frame_addr, 1);
4711 force_sig(TARGET_SIGSEGV);
4714 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4715 CPUState *env)
4717 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4718 int err;
4720 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4721 err |= __put_user(env->dregs[0], &gregs[0]);
4722 err |= __put_user(env->dregs[1], &gregs[1]);
4723 err |= __put_user(env->dregs[2], &gregs[2]);
4724 err |= __put_user(env->dregs[3], &gregs[3]);
4725 err |= __put_user(env->dregs[4], &gregs[4]);
4726 err |= __put_user(env->dregs[5], &gregs[5]);
4727 err |= __put_user(env->dregs[6], &gregs[6]);
4728 err |= __put_user(env->dregs[7], &gregs[7]);
4729 err |= __put_user(env->aregs[0], &gregs[8]);
4730 err |= __put_user(env->aregs[1], &gregs[9]);
4731 err |= __put_user(env->aregs[2], &gregs[10]);
4732 err |= __put_user(env->aregs[3], &gregs[11]);
4733 err |= __put_user(env->aregs[4], &gregs[12]);
4734 err |= __put_user(env->aregs[5], &gregs[13]);
4735 err |= __put_user(env->aregs[6], &gregs[14]);
4736 err |= __put_user(env->aregs[7], &gregs[15]);
4737 err |= __put_user(env->pc, &gregs[16]);
4738 err |= __put_user(env->sr, &gregs[17]);
4740 return err;
4743 static inline int target_rt_restore_ucontext(CPUState *env,
4744 struct target_ucontext *uc,
4745 int *pd0)
4747 int temp;
4748 int err;
4749 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4751 err = __get_user(temp, &uc->tuc_mcontext.version);
4752 if (temp != TARGET_MCONTEXT_VERSION)
4753 goto badframe;
4755 /* restore passed registers */
4756 err |= __get_user(env->dregs[0], &gregs[0]);
4757 err |= __get_user(env->dregs[1], &gregs[1]);
4758 err |= __get_user(env->dregs[2], &gregs[2]);
4759 err |= __get_user(env->dregs[3], &gregs[3]);
4760 err |= __get_user(env->dregs[4], &gregs[4]);
4761 err |= __get_user(env->dregs[5], &gregs[5]);
4762 err |= __get_user(env->dregs[6], &gregs[6]);
4763 err |= __get_user(env->dregs[7], &gregs[7]);
4764 err |= __get_user(env->aregs[0], &gregs[8]);
4765 err |= __get_user(env->aregs[1], &gregs[9]);
4766 err |= __get_user(env->aregs[2], &gregs[10]);
4767 err |= __get_user(env->aregs[3], &gregs[11]);
4768 err |= __get_user(env->aregs[4], &gregs[12]);
4769 err |= __get_user(env->aregs[5], &gregs[13]);
4770 err |= __get_user(env->aregs[6], &gregs[14]);
4771 err |= __get_user(env->aregs[7], &gregs[15]);
4772 err |= __get_user(env->pc, &gregs[16]);
4773 err |= __get_user(temp, &gregs[17]);
4774 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4776 *pd0 = env->dregs[0];
4777 return err;
4779 badframe:
4780 return 1;
4783 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4784 target_siginfo_t *info,
4785 target_sigset_t *set, CPUState *env)
4787 struct target_rt_sigframe *frame;
4788 abi_ulong frame_addr;
4789 abi_ulong retcode_addr;
4790 abi_ulong info_addr;
4791 abi_ulong uc_addr;
4792 int err = 0;
4793 int i;
4795 frame_addr = get_sigframe(ka, env, sizeof *frame);
4796 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4797 goto give_sigsegv;
4799 err |= __put_user(sig, &frame->sig);
4801 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4802 err |= __put_user(info_addr, &frame->pinfo);
4804 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4805 err |= __put_user(uc_addr, &frame->puc);
4807 err |= copy_siginfo_to_user(&frame->info, info);
4809 /* Create the ucontext */
4811 err |= __put_user(0, &frame->uc.tuc_flags);
4812 err |= __put_user(0, &frame->uc.tuc_link);
4813 err |= __put_user(target_sigaltstack_used.ss_sp,
4814 &frame->uc.tuc_stack.ss_sp);
4815 err |= __put_user(sas_ss_flags(env->aregs[7]),
4816 &frame->uc.tuc_stack.ss_flags);
4817 err |= __put_user(target_sigaltstack_used.ss_size,
4818 &frame->uc.tuc_stack.ss_size);
4819 err |= target_rt_setup_ucontext(&frame->uc, env);
4821 if (err)
4822 goto give_sigsegv;
4824 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4825 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
4826 goto give_sigsegv;
4829 /* Set up to return from userspace. */
4831 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4832 err |= __put_user(retcode_addr, &frame->pretcode);
4834 /* moveq #,d0; notb d0; trap #0 */
4836 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4837 (long *)(frame->retcode + 0));
4838 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4840 if (err)
4841 goto give_sigsegv;
4843 /* Set up to return from userspace */
4845 env->aregs[7] = frame_addr;
4846 env->pc = ka->_sa_handler;
4848 unlock_user_struct(frame, frame_addr, 1);
4849 return;
4851 give_sigsegv:
4852 unlock_user_struct(frame, frame_addr, 1);
4853 force_sig(TARGET_SIGSEGV);
4856 long do_sigreturn(CPUState *env)
4858 struct target_sigframe *frame;
4859 abi_ulong frame_addr = env->aregs[7] - 4;
4860 target_sigset_t target_set;
4861 sigset_t set;
4862 int d0, i;
4864 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4865 goto badframe;
4867 /* set blocked signals */
4869 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4870 goto badframe;
4872 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4873 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4874 goto badframe;
4877 target_to_host_sigset_internal(&set, &target_set);
4878 sigprocmask(SIG_SETMASK, &set, NULL);
4880 /* restore registers */
4882 if (restore_sigcontext(env, &frame->sc, &d0))
4883 goto badframe;
4885 unlock_user_struct(frame, frame_addr, 0);
4886 return d0;
4888 badframe:
4889 unlock_user_struct(frame, frame_addr, 0);
4890 force_sig(TARGET_SIGSEGV);
4891 return 0;
4894 long do_rt_sigreturn(CPUState *env)
4896 struct target_rt_sigframe *frame;
4897 abi_ulong frame_addr = env->aregs[7] - 4;
4898 target_sigset_t target_set;
4899 sigset_t set;
4900 int d0;
4902 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4903 goto badframe;
4905 target_to_host_sigset_internal(&set, &target_set);
4906 sigprocmask(SIG_SETMASK, &set, NULL);
4908 /* restore registers */
4910 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4911 goto badframe;
4913 if (do_sigaltstack(frame_addr +
4914 offsetof(struct target_rt_sigframe, uc.tuc_stack),
4915 0, get_sp_from_cpustate(env)) == -EFAULT)
4916 goto badframe;
4918 unlock_user_struct(frame, frame_addr, 0);
4919 return d0;
4921 badframe:
4922 unlock_user_struct(frame, frame_addr, 0);
4923 force_sig(TARGET_SIGSEGV);
4924 return 0;
4927 #elif defined(TARGET_ALPHA)
4929 struct target_sigcontext {
4930 abi_long sc_onstack;
4931 abi_long sc_mask;
4932 abi_long sc_pc;
4933 abi_long sc_ps;
4934 abi_long sc_regs[32];
4935 abi_long sc_ownedfp;
4936 abi_long sc_fpregs[32];
4937 abi_ulong sc_fpcr;
4938 abi_ulong sc_fp_control;
4939 abi_ulong sc_reserved1;
4940 abi_ulong sc_reserved2;
4941 abi_ulong sc_ssize;
4942 abi_ulong sc_sbase;
4943 abi_ulong sc_traparg_a0;
4944 abi_ulong sc_traparg_a1;
4945 abi_ulong sc_traparg_a2;
4946 abi_ulong sc_fp_trap_pc;
4947 abi_ulong sc_fp_trigger_sum;
4948 abi_ulong sc_fp_trigger_inst;
4951 struct target_ucontext {
4952 abi_ulong tuc_flags;
4953 abi_ulong tuc_link;
4954 abi_ulong tuc_osf_sigmask;
4955 target_stack_t tuc_stack;
4956 struct target_sigcontext tuc_mcontext;
4957 target_sigset_t tuc_sigmask;
4960 struct target_sigframe {
4961 struct target_sigcontext sc;
4962 unsigned int retcode[3];
4965 struct target_rt_sigframe {
4966 target_siginfo_t info;
4967 struct target_ucontext uc;
4968 unsigned int retcode[3];
4971 #define INSN_MOV_R30_R16 0x47fe0410
4972 #define INSN_LDI_R0 0x201f0000
4973 #define INSN_CALLSYS 0x00000083
4975 static int setup_sigcontext(struct target_sigcontext *sc, CPUState *env,
4976 abi_ulong frame_addr, target_sigset_t *set)
4978 int i, err = 0;
4980 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
4981 err |= __put_user(set->sig[0], &sc->sc_mask);
4982 err |= __put_user(env->pc, &sc->sc_pc);
4983 err |= __put_user(8, &sc->sc_ps);
4985 for (i = 0; i < 31; ++i) {
4986 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
4988 err |= __put_user(0, &sc->sc_regs[31]);
4990 for (i = 0; i < 31; ++i) {
4991 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
4993 err |= __put_user(0, &sc->sc_fpregs[31]);
4994 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
4996 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
4997 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
4998 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5000 return err;
5003 static int restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
5005 uint64_t fpcr;
5006 int i, err = 0;
5008 err |= __get_user(env->pc, &sc->sc_pc);
5010 for (i = 0; i < 31; ++i) {
5011 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5013 for (i = 0; i < 31; ++i) {
5014 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5017 err |= __get_user(fpcr, &sc->sc_fpcr);
5018 cpu_alpha_store_fpcr(env, fpcr);
5020 return err;
5023 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5024 CPUState *env, unsigned long framesize)
5026 abi_ulong sp = env->ir[IR_SP];
5028 /* This is the X/Open sanctioned signal stack switching. */
5029 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5030 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5032 return (sp - framesize) & -32;
5035 static void setup_frame(int sig, struct target_sigaction *ka,
5036 target_sigset_t *set, CPUState *env)
5038 abi_ulong frame_addr, r26;
5039 struct target_sigframe *frame;
5040 int err = 0;
5042 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5043 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5044 goto give_sigsegv;
5047 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5049 if (ka->sa_restorer) {
5050 r26 = ka->sa_restorer;
5051 } else {
5052 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5053 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5054 &frame->retcode[1]);
5055 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5056 /* imb() */
5057 r26 = frame_addr;
5060 unlock_user_struct(frame, frame_addr, 1);
5062 if (err) {
5063 give_sigsegv:
5064 if (sig == TARGET_SIGSEGV) {
5065 ka->_sa_handler = TARGET_SIG_DFL;
5067 force_sig(TARGET_SIGSEGV);
5070 env->ir[IR_RA] = r26;
5071 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5072 env->ir[IR_A0] = sig;
5073 env->ir[IR_A1] = 0;
5074 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5075 env->ir[IR_SP] = frame_addr;
5078 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5079 target_siginfo_t *info,
5080 target_sigset_t *set, CPUState *env)
5082 abi_ulong frame_addr, r26;
5083 struct target_rt_sigframe *frame;
5084 int i, err = 0;
5086 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5087 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5088 goto give_sigsegv;
5091 err |= copy_siginfo_to_user(&frame->info, info);
5093 err |= __put_user(0, &frame->uc.tuc_flags);
5094 err |= __put_user(0, &frame->uc.tuc_link);
5095 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5096 err |= __put_user(target_sigaltstack_used.ss_sp,
5097 &frame->uc.tuc_stack.ss_sp);
5098 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5099 &frame->uc.tuc_stack.ss_flags);
5100 err |= __put_user(target_sigaltstack_used.ss_size,
5101 &frame->uc.tuc_stack.ss_size);
5102 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5103 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5104 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5107 if (ka->sa_restorer) {
5108 r26 = ka->sa_restorer;
5109 } else {
5110 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5111 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5112 &frame->retcode[1]);
5113 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5114 /* imb(); */
5115 r26 = frame_addr;
5118 if (err) {
5119 give_sigsegv:
5120 if (sig == TARGET_SIGSEGV) {
5121 ka->_sa_handler = TARGET_SIG_DFL;
5123 force_sig(TARGET_SIGSEGV);
5126 env->ir[IR_RA] = r26;
5127 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5128 env->ir[IR_A0] = sig;
5129 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5130 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5131 env->ir[IR_SP] = frame_addr;
5134 long do_sigreturn(CPUState *env)
5136 struct target_sigcontext *sc;
5137 abi_ulong sc_addr = env->ir[IR_A0];
5138 target_sigset_t target_set;
5139 sigset_t set;
5141 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5142 goto badframe;
5145 target_sigemptyset(&target_set);
5146 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5147 goto badframe;
5150 target_to_host_sigset_internal(&set, &target_set);
5151 sigprocmask(SIG_SETMASK, &set, NULL);
5153 if (restore_sigcontext(env, sc)) {
5154 goto badframe;
5156 unlock_user_struct(sc, sc_addr, 0);
5157 return env->ir[IR_V0];
5159 badframe:
5160 unlock_user_struct(sc, sc_addr, 0);
5161 force_sig(TARGET_SIGSEGV);
5164 long do_rt_sigreturn(CPUState *env)
5166 abi_ulong frame_addr = env->ir[IR_A0];
5167 struct target_rt_sigframe *frame;
5168 sigset_t set;
5170 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5171 goto badframe;
5173 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5174 sigprocmask(SIG_SETMASK, &set, NULL);
5176 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5177 goto badframe;
5179 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5180 uc.tuc_stack),
5181 0, env->ir[IR_SP]) == -EFAULT) {
5182 goto badframe;
5185 unlock_user_struct(frame, frame_addr, 0);
5186 return env->ir[IR_V0];
5189 badframe:
5190 unlock_user_struct(frame, frame_addr, 0);
5191 force_sig(TARGET_SIGSEGV);
5194 #else
5196 static void setup_frame(int sig, struct target_sigaction *ka,
5197 target_sigset_t *set, CPUState *env)
5199 fprintf(stderr, "setup_frame: not implemented\n");
5202 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5203 target_siginfo_t *info,
5204 target_sigset_t *set, CPUState *env)
5206 fprintf(stderr, "setup_rt_frame: not implemented\n");
5209 long do_sigreturn(CPUState *env)
5211 fprintf(stderr, "do_sigreturn: not implemented\n");
5212 return -TARGET_ENOSYS;
5215 long do_rt_sigreturn(CPUState *env)
5217 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5218 return -TARGET_ENOSYS;
5221 #endif
5223 void process_pending_signals(CPUState *cpu_env)
5225 int sig;
5226 abi_ulong handler;
5227 sigset_t set, old_set;
5228 target_sigset_t target_old_set;
5229 struct emulated_sigtable *k;
5230 struct target_sigaction *sa;
5231 struct sigqueue *q;
5232 TaskState *ts = cpu_env->opaque;
5234 if (!ts->signal_pending)
5235 return;
5237 /* FIXME: This is not threadsafe. */
5238 k = ts->sigtab;
5239 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5240 if (k->pending)
5241 goto handle_signal;
5242 k++;
5244 /* if no signal is pending, just return */
5245 ts->signal_pending = 0;
5246 return;
5248 handle_signal:
5249 #ifdef DEBUG_SIGNAL
5250 fprintf(stderr, "qemu: process signal %d\n", sig);
5251 #endif
5252 /* dequeue signal */
5253 q = k->first;
5254 k->first = q->next;
5255 if (!k->first)
5256 k->pending = 0;
5258 sig = gdb_handlesig (cpu_env, sig);
5259 if (!sig) {
5260 sa = NULL;
5261 handler = TARGET_SIG_IGN;
5262 } else {
5263 sa = &sigact_table[sig - 1];
5264 handler = sa->_sa_handler;
5267 if (handler == TARGET_SIG_DFL) {
5268 /* default handler : ignore some signal. The other are job control or fatal */
5269 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5270 kill(getpid(),SIGSTOP);
5271 } else if (sig != TARGET_SIGCHLD &&
5272 sig != TARGET_SIGURG &&
5273 sig != TARGET_SIGWINCH &&
5274 sig != TARGET_SIGCONT) {
5275 force_sig(sig);
5277 } else if (handler == TARGET_SIG_IGN) {
5278 /* ignore sig */
5279 } else if (handler == TARGET_SIG_ERR) {
5280 force_sig(sig);
5281 } else {
5282 /* compute the blocked signals during the handler execution */
5283 target_to_host_sigset(&set, &sa->sa_mask);
5284 /* SA_NODEFER indicates that the current signal should not be
5285 blocked during the handler */
5286 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5287 sigaddset(&set, target_to_host_signal(sig));
5289 /* block signals in the handler using Linux */
5290 sigprocmask(SIG_BLOCK, &set, &old_set);
5291 /* save the previous blocked signal state to restore it at the
5292 end of the signal execution (see do_sigreturn) */
5293 host_to_target_sigset_internal(&target_old_set, &old_set);
5295 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5296 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5298 CPUX86State *env = cpu_env;
5299 if (env->eflags & VM_MASK)
5300 save_v86_state(env);
5302 #endif
5303 /* prepare the stack frame of the virtual CPU */
5304 if (sa->sa_flags & TARGET_SA_SIGINFO)
5305 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5306 else
5307 setup_frame(sig, sa, &target_old_set, cpu_env);
5308 if (sa->sa_flags & TARGET_SA_RESETHAND)
5309 sa->_sa_handler = TARGET_SIG_DFL;
5311 if (q != &k->info)
5312 free_sigqueue(cpu_env, q);