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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
29 #include <sys/ucontext.h>
32 #include "target_signal.h"
34 //#define DEBUG_SIGNAL
36 static struct target_sigaltstack target_sigaltstack_used = {
39 .ss_flags = TARGET_SS_DISABLE,
42 static struct target_sigaction sigact_table[TARGET_NSIG];
44 static void host_signal_handler(int host_signum, siginfo_t *info,
47 static uint8_t host_to_target_signal_table[65] = {
48 [SIGHUP] = TARGET_SIGHUP,
49 [SIGINT] = TARGET_SIGINT,
50 [SIGQUIT] = TARGET_SIGQUIT,
51 [SIGILL] = TARGET_SIGILL,
52 [SIGTRAP] = TARGET_SIGTRAP,
53 [SIGABRT] = TARGET_SIGABRT,
54 /* [SIGIOT] = TARGET_SIGIOT,*/
55 [SIGBUS] = TARGET_SIGBUS,
56 [SIGFPE] = TARGET_SIGFPE,
57 [SIGKILL] = TARGET_SIGKILL,
58 [SIGUSR1] = TARGET_SIGUSR1,
59 [SIGSEGV] = TARGET_SIGSEGV,
60 [SIGUSR2] = TARGET_SIGUSR2,
61 [SIGPIPE] = TARGET_SIGPIPE,
62 [SIGALRM] = TARGET_SIGALRM,
63 [SIGTERM] = TARGET_SIGTERM,
65 [SIGSTKFLT] = TARGET_SIGSTKFLT,
67 [SIGCHLD] = TARGET_SIGCHLD,
68 [SIGCONT] = TARGET_SIGCONT,
69 [SIGSTOP] = TARGET_SIGSTOP,
70 [SIGTSTP] = TARGET_SIGTSTP,
71 [SIGTTIN] = TARGET_SIGTTIN,
72 [SIGTTOU] = TARGET_SIGTTOU,
73 [SIGURG] = TARGET_SIGURG,
74 [SIGXCPU] = TARGET_SIGXCPU,
75 [SIGXFSZ] = TARGET_SIGXFSZ,
76 [SIGVTALRM] = TARGET_SIGVTALRM,
77 [SIGPROF] = TARGET_SIGPROF,
78 [SIGWINCH] = TARGET_SIGWINCH,
79 [SIGIO] = TARGET_SIGIO,
80 [SIGPWR] = TARGET_SIGPWR,
81 [SIGSYS] = TARGET_SIGSYS,
82 /* next signals stay the same */
83 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
84 host libpthread signals. This assumes noone actually uses SIGRTMAX :-/
85 To fix this properly we need to do manual signal delivery multiplexed
86 over a single host signal. */
87 [__SIGRTMIN] = __SIGRTMAX,
88 [__SIGRTMAX] = __SIGRTMIN,
90 static uint8_t target_to_host_signal_table[65];
92 static inline int on_sig_stack(unsigned long sp)
94 return (sp - target_sigaltstack_used.ss_sp
95 < target_sigaltstack_used.ss_size);
98 static inline int sas_ss_flags(unsigned long sp)
100 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
101 : on_sig_stack(sp) ? SS_ONSTACK : 0);
104 static inline int host_to_target_signal(int sig)
108 return host_to_target_signal_table[sig];
111 int target_to_host_signal(int sig)
115 return target_to_host_signal_table[sig];
118 static inline void target_sigemptyset(target_sigset_t *set)
120 memset(set, 0, sizeof(*set));
123 static inline void target_sigaddset(target_sigset_t *set, int signum)
126 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
127 set->sig[signum / TARGET_NSIG_BPW] |= mask;
130 static inline int target_sigismember(const target_sigset_t *set, int signum)
133 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
134 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
137 static void host_to_target_sigset_internal(target_sigset_t *d,
141 target_sigemptyset(d);
142 for (i = 1; i <= TARGET_NSIG; i++) {
143 if (sigismember(s, i)) {
144 target_sigaddset(d, host_to_target_signal(i));
149 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
154 host_to_target_sigset_internal(&d1, s);
155 for(i = 0;i < TARGET_NSIG_WORDS; i++)
156 d->sig[i] = tswapl(d1.sig[i]);
159 static void target_to_host_sigset_internal(sigset_t *d,
160 const target_sigset_t *s)
164 for (i = 1; i <= TARGET_NSIG; i++) {
165 if (target_sigismember(s, i)) {
166 sigaddset(d, target_to_host_signal(i));
171 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
176 for(i = 0;i < TARGET_NSIG_WORDS; i++)
177 s1.sig[i] = tswapl(s->sig[i]);
178 target_to_host_sigset_internal(d, &s1);
181 void host_to_target_old_sigset(abi_ulong *old_sigset,
182 const sigset_t *sigset)
185 host_to_target_sigset(&d, sigset);
186 *old_sigset = d.sig[0];
189 void target_to_host_old_sigset(sigset_t *sigset,
190 const abi_ulong *old_sigset)
195 d.sig[0] = *old_sigset;
196 for(i = 1;i < TARGET_NSIG_WORDS; i++)
198 target_to_host_sigset(sigset, &d);
201 /* siginfo conversion */
203 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
204 const siginfo_t *info)
207 sig = host_to_target_signal(info->si_signo);
208 tinfo->si_signo = sig;
210 tinfo->si_code = info->si_code;
211 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
212 sig == SIGBUS || sig == SIGTRAP) {
213 /* should never come here, but who knows. The information for
214 the target is irrelevant */
215 tinfo->_sifields._sigfault._addr = 0;
216 } else if (sig == SIGIO) {
217 tinfo->_sifields._sigpoll._fd = info->si_fd;
218 } else if (sig >= TARGET_SIGRTMIN) {
219 tinfo->_sifields._rt._pid = info->si_pid;
220 tinfo->_sifields._rt._uid = info->si_uid;
221 /* XXX: potential problem if 64 bit */
222 tinfo->_sifields._rt._sigval.sival_ptr =
223 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
227 static void tswap_siginfo(target_siginfo_t *tinfo,
228 const target_siginfo_t *info)
231 sig = info->si_signo;
232 tinfo->si_signo = tswap32(sig);
233 tinfo->si_errno = tswap32(info->si_errno);
234 tinfo->si_code = tswap32(info->si_code);
235 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
236 sig == SIGBUS || sig == SIGTRAP) {
237 tinfo->_sifields._sigfault._addr =
238 tswapl(info->_sifields._sigfault._addr);
239 } else if (sig == SIGIO) {
240 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
241 } else if (sig >= TARGET_SIGRTMIN) {
242 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
243 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
244 tinfo->_sifields._rt._sigval.sival_ptr =
245 tswapl(info->_sifields._rt._sigval.sival_ptr);
250 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
252 host_to_target_siginfo_noswap(tinfo, info);
253 tswap_siginfo(tinfo, tinfo);
256 /* XXX: we support only POSIX RT signals are used. */
257 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
258 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
260 info->si_signo = tswap32(tinfo->si_signo);
261 info->si_errno = tswap32(tinfo->si_errno);
262 info->si_code = tswap32(tinfo->si_code);
263 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
264 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
265 info->si_value.sival_ptr =
266 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
269 static int fatal_signal (int sig)
274 case TARGET_SIGWINCH:
275 /* Ignored by default. */
282 /* Job control signals. */
289 void signal_init(void)
291 struct sigaction act;
292 struct sigaction oact;
296 /* generate signal conversion tables */
297 for(i = 1; i <= 64; i++) {
298 if (host_to_target_signal_table[i] == 0)
299 host_to_target_signal_table[i] = i;
301 for(i = 1; i <= 64; i++) {
302 j = host_to_target_signal_table[i];
303 target_to_host_signal_table[j] = i;
306 /* set all host signal handlers. ALL signals are blocked during
307 the handlers to serialize them. */
308 memset(sigact_table, 0, sizeof(sigact_table));
310 sigfillset(&act.sa_mask);
311 act.sa_flags = SA_SIGINFO;
312 act.sa_sigaction = host_signal_handler;
313 for(i = 1; i <= TARGET_NSIG; i++) {
314 host_sig = target_to_host_signal(i);
315 sigaction(host_sig, NULL, &oact);
316 if (oact.sa_sigaction == (void *)SIG_IGN) {
317 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
318 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
319 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
321 /* If there's already a handler installed then something has
322 gone horribly wrong, so don't even try to handle that case. */
323 /* Install some handlers for our own use. We need at least
324 SIGSEGV and SIGBUS, to detect exceptions. We can not just
325 trap all signals because it affects syscall interrupt
326 behavior. But do trap all default-fatal signals. */
327 if (fatal_signal (i))
328 sigaction(host_sig, &act, NULL);
332 /* signal queue handling */
334 static inline struct sigqueue *alloc_sigqueue(CPUState *env)
336 TaskState *ts = env->opaque;
337 struct sigqueue *q = ts->first_free;
340 ts->first_free = q->next;
344 static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
346 TaskState *ts = env->opaque;
347 q->next = ts->first_free;
351 /* abort execution with signal */
352 static void __attribute((noreturn)) force_sig(int sig)
355 struct sigaction act;
356 host_sig = target_to_host_signal(sig);
357 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
358 sig, strsignal(host_sig));
359 gdb_signalled(thread_env, sig);
361 /* The proper exit code for dieing from an uncaught signal is
362 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
363 * a negative value. To get the proper exit code we need to
364 * actually die from an uncaught signal. Here the default signal
365 * handler is installed, we send ourself a signal and we wait for
367 sigfillset(&act.sa_mask);
368 act.sa_handler = SIG_DFL;
369 sigaction(host_sig, &act, NULL);
371 /* For some reason raise(host_sig) doesn't send the signal when
372 * statically linked on x86-64. */
373 kill(getpid(), host_sig);
375 /* Make sure the signal isn't masked (just reuse the mask inside
377 sigdelset(&act.sa_mask, host_sig);
378 sigsuspend(&act.sa_mask);
385 /* queue a signal so that it will be send to the virtual CPU as soon
387 int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
389 TaskState *ts = env->opaque;
390 struct emulated_sigtable *k;
391 struct sigqueue *q, **pq;
395 #if defined(DEBUG_SIGNAL)
396 fprintf(stderr, "queue_signal: sig=%d\n",
399 k = &ts->sigtab[sig - 1];
400 queue = gdb_queuesig ();
401 handler = sigact_table[sig - 1]._sa_handler;
402 if (!queue && handler == TARGET_SIG_DFL) {
403 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
404 kill(getpid(),SIGSTOP);
407 /* default handler : ignore some signal. The other are fatal */
408 if (sig != TARGET_SIGCHLD &&
409 sig != TARGET_SIGURG &&
410 sig != TARGET_SIGWINCH &&
411 sig != TARGET_SIGCONT) {
414 return 0; /* indicate ignored */
416 } else if (!queue && handler == TARGET_SIG_IGN) {
419 } else if (!queue && handler == TARGET_SIG_ERR) {
423 if (sig < TARGET_SIGRTMIN) {
424 /* if non real time signal, we queue exactly one signal */
434 q = alloc_sigqueue(env);
445 /* signal that a new signal is pending */
446 ts->signal_pending = 1;
447 return 1; /* indicates that the signal was queued */
451 static void host_signal_handler(int host_signum, siginfo_t *info,
455 target_siginfo_t tinfo;
457 /* the CPU emulator uses some host signals to detect exceptions,
458 we forward to it some signals */
459 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
460 && info->si_code > 0) {
461 if (cpu_signal_handler(host_signum, info, puc))
465 /* get target signal number */
466 sig = host_to_target_signal(host_signum);
467 if (sig < 1 || sig > TARGET_NSIG)
469 #if defined(DEBUG_SIGNAL)
470 fprintf(stderr, "qemu: got signal %d\n", sig);
472 host_to_target_siginfo_noswap(&tinfo, info);
473 if (queue_signal(thread_env, sig, &tinfo) == 1) {
474 /* interrupt the virtual CPU as soon as possible */
475 cpu_interrupt(thread_env, CPU_INTERRUPT_EXIT);
479 /* do_sigaltstack() returns target values and errnos. */
480 /* compare linux/kernel/signal.c:do_sigaltstack() */
481 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
484 struct target_sigaltstack oss;
486 /* XXX: test errors */
489 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
490 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
491 __put_user(sas_ss_flags(sp), &oss.ss_flags);
496 struct target_sigaltstack *uss;
497 struct target_sigaltstack ss;
499 ret = -TARGET_EFAULT;
500 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
501 || __get_user(ss.ss_sp, &uss->ss_sp)
502 || __get_user(ss.ss_size, &uss->ss_size)
503 || __get_user(ss.ss_flags, &uss->ss_flags))
505 unlock_user_struct(uss, uss_addr, 0);
508 if (on_sig_stack(sp))
511 ret = -TARGET_EINVAL;
512 if (ss.ss_flags != TARGET_SS_DISABLE
513 && ss.ss_flags != TARGET_SS_ONSTACK
517 if (ss.ss_flags == TARGET_SS_DISABLE) {
521 ret = -TARGET_ENOMEM;
522 if (ss.ss_size < MINSIGSTKSZ)
526 target_sigaltstack_used.ss_sp = ss.ss_sp;
527 target_sigaltstack_used.ss_size = ss.ss_size;
531 ret = -TARGET_EFAULT;
532 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
541 /* do_sigaction() return host values and errnos */
542 int do_sigaction(int sig, const struct target_sigaction *act,
543 struct target_sigaction *oact)
545 struct target_sigaction *k;
546 struct sigaction act1;
550 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
552 k = &sigact_table[sig - 1];
553 #if defined(DEBUG_SIGNAL)
554 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
555 sig, (int)act, (int)oact);
558 oact->_sa_handler = tswapl(k->_sa_handler);
559 oact->sa_flags = tswapl(k->sa_flags);
560 #if !defined(TARGET_MIPS)
561 oact->sa_restorer = tswapl(k->sa_restorer);
563 oact->sa_mask = k->sa_mask;
566 /* FIXME: This is not threadsafe. */
567 k->_sa_handler = tswapl(act->_sa_handler);
568 k->sa_flags = tswapl(act->sa_flags);
569 #if !defined(TARGET_MIPS)
570 k->sa_restorer = tswapl(act->sa_restorer);
572 k->sa_mask = act->sa_mask;
574 /* we update the host linux signal state */
575 host_sig = target_to_host_signal(sig);
576 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
577 sigfillset(&act1.sa_mask);
578 act1.sa_flags = SA_SIGINFO;
579 if (k->sa_flags & TARGET_SA_RESTART)
580 act1.sa_flags |= SA_RESTART;
581 /* NOTE: it is important to update the host kernel signal
582 ignore state to avoid getting unexpected interrupted
584 if (k->_sa_handler == TARGET_SIG_IGN) {
585 act1.sa_sigaction = (void *)SIG_IGN;
586 } else if (k->_sa_handler == TARGET_SIG_DFL) {
587 if (fatal_signal (sig))
588 act1.sa_sigaction = host_signal_handler;
590 act1.sa_sigaction = (void *)SIG_DFL;
592 act1.sa_sigaction = host_signal_handler;
594 ret = sigaction(host_sig, &act1, NULL);
600 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
601 const target_siginfo_t *info)
603 tswap_siginfo(tinfo, info);
607 static inline int current_exec_domain_sig(int sig)
609 return /* current->exec_domain && current->exec_domain->signal_invmap
610 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
613 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
615 /* from the Linux kernel */
617 struct target_fpreg {
618 uint16_t significand[4];
622 struct target_fpxreg {
623 uint16_t significand[4];
628 struct target_xmmreg {
629 abi_ulong element[4];
632 struct target_fpstate {
633 /* Regular FPU environment */
641 struct target_fpreg _st[8];
643 uint16_t magic; /* 0xffff = regular FPU data only */
645 /* FXSR FPU environment */
646 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
649 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
650 struct target_xmmreg _xmm[8];
651 abi_ulong padding[56];
654 #define X86_FXSR_MAGIC 0x0000
656 struct target_sigcontext {
674 abi_ulong esp_at_signal;
676 abi_ulong fpstate; /* pointer */
681 struct target_ucontext {
684 target_stack_t tuc_stack;
685 struct target_sigcontext tuc_mcontext;
686 target_sigset_t tuc_sigmask; /* mask last for extensibility */
693 struct target_sigcontext sc;
694 struct target_fpstate fpstate;
695 abi_ulong extramask[TARGET_NSIG_WORDS-1];
705 struct target_siginfo info;
706 struct target_ucontext uc;
707 struct target_fpstate fpstate;
712 * Set up a signal frame.
715 /* XXX: save x87 state */
717 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
718 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
723 /* already locked in setup_frame() */
724 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
725 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
726 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
727 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
728 err |= __put_user(env->regs[R_EDI], &sc->edi);
729 err |= __put_user(env->regs[R_ESI], &sc->esi);
730 err |= __put_user(env->regs[R_EBP], &sc->ebp);
731 err |= __put_user(env->regs[R_ESP], &sc->esp);
732 err |= __put_user(env->regs[R_EBX], &sc->ebx);
733 err |= __put_user(env->regs[R_EDX], &sc->edx);
734 err |= __put_user(env->regs[R_ECX], &sc->ecx);
735 err |= __put_user(env->regs[R_EAX], &sc->eax);
736 err |= __put_user(env->exception_index, &sc->trapno);
737 err |= __put_user(env->error_code, &sc->err);
738 err |= __put_user(env->eip, &sc->eip);
739 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
740 err |= __put_user(env->eflags, &sc->eflags);
741 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
742 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
744 cpu_x86_fsave(env, fpstate_addr, 1);
745 fpstate->status = fpstate->sw;
747 err |= __put_user(magic, &fpstate->magic);
748 err |= __put_user(fpstate_addr, &sc->fpstate);
750 /* non-iBCS2 extensions.. */
751 err |= __put_user(mask, &sc->oldmask);
752 err |= __put_user(env->cr[2], &sc->cr2);
757 * Determine which stack to use..
760 static inline abi_ulong
761 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
765 /* Default to using normal stack */
766 esp = env->regs[R_ESP];
767 /* This is the X/Open sanctioned signal stack switching. */
768 if (ka->sa_flags & TARGET_SA_ONSTACK) {
769 if (sas_ss_flags(esp) == 0)
770 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
773 /* This is the legacy signal stack switching. */
775 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
776 !(ka->sa_flags & TARGET_SA_RESTORER) &&
778 esp = (unsigned long) ka->sa_restorer;
780 return (esp - frame_size) & -8ul;
783 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
784 static void setup_frame(int sig, struct target_sigaction *ka,
785 target_sigset_t *set, CPUX86State *env)
787 abi_ulong frame_addr;
788 struct sigframe *frame;
791 frame_addr = get_sigframe(ka, env, sizeof(*frame));
793 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
796 err |= __put_user(current_exec_domain_sig(sig),
801 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
802 frame_addr + offsetof(struct sigframe, fpstate));
806 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
807 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
811 /* Set up to return from userspace. If provided, use a stub
812 already in userspace. */
813 if (ka->sa_flags & TARGET_SA_RESTORER) {
814 err |= __put_user(ka->sa_restorer, &frame->pretcode);
817 abi_ulong retcode_addr;
818 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
819 err |= __put_user(retcode_addr, &frame->pretcode);
820 /* This is popl %eax ; movl $,%eax ; int $0x80 */
822 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
823 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
825 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
831 /* Set up registers for signal handler */
832 env->regs[R_ESP] = frame_addr;
833 env->eip = ka->_sa_handler;
835 cpu_x86_load_seg(env, R_DS, __USER_DS);
836 cpu_x86_load_seg(env, R_ES, __USER_DS);
837 cpu_x86_load_seg(env, R_SS, __USER_DS);
838 cpu_x86_load_seg(env, R_CS, __USER_CS);
839 env->eflags &= ~TF_MASK;
841 unlock_user_struct(frame, frame_addr, 1);
846 unlock_user_struct(frame, frame_addr, 1);
847 if (sig == TARGET_SIGSEGV)
848 ka->_sa_handler = TARGET_SIG_DFL;
849 force_sig(TARGET_SIGSEGV /* , current */);
852 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
853 static void setup_rt_frame(int sig, struct target_sigaction *ka,
854 target_siginfo_t *info,
855 target_sigset_t *set, CPUX86State *env)
857 abi_ulong frame_addr, addr;
858 struct rt_sigframe *frame;
861 frame_addr = get_sigframe(ka, env, sizeof(*frame));
863 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
866 err |= __put_user(current_exec_domain_sig(sig),
868 addr = frame_addr + offsetof(struct rt_sigframe, info);
869 err |= __put_user(addr, &frame->pinfo);
870 addr = frame_addr + offsetof(struct rt_sigframe, uc);
871 err |= __put_user(addr, &frame->puc);
872 err |= copy_siginfo_to_user(&frame->info, info);
876 /* Create the ucontext. */
877 err |= __put_user(0, &frame->uc.tuc_flags);
878 err |= __put_user(0, &frame->uc.tuc_link);
879 err |= __put_user(target_sigaltstack_used.ss_sp,
880 &frame->uc.tuc_stack.ss_sp);
881 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
882 &frame->uc.tuc_stack.ss_flags);
883 err |= __put_user(target_sigaltstack_used.ss_size,
884 &frame->uc.tuc_stack.ss_size);
885 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
887 frame_addr + offsetof(struct rt_sigframe, fpstate));
888 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
889 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
893 /* Set up to return from userspace. If provided, use a stub
894 already in userspace. */
895 if (ka->sa_flags & TARGET_SA_RESTORER) {
896 err |= __put_user(ka->sa_restorer, &frame->pretcode);
899 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
900 err |= __put_user(addr, &frame->pretcode);
901 /* This is movl $,%eax ; int $0x80 */
902 err |= __put_user(0xb8, (char *)(frame->retcode+0));
903 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
905 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
911 /* Set up registers for signal handler */
912 env->regs[R_ESP] = frame_addr;
913 env->eip = ka->_sa_handler;
915 cpu_x86_load_seg(env, R_DS, __USER_DS);
916 cpu_x86_load_seg(env, R_ES, __USER_DS);
917 cpu_x86_load_seg(env, R_SS, __USER_DS);
918 cpu_x86_load_seg(env, R_CS, __USER_CS);
919 env->eflags &= ~TF_MASK;
921 unlock_user_struct(frame, frame_addr, 1);
926 unlock_user_struct(frame, frame_addr, 1);
927 if (sig == TARGET_SIGSEGV)
928 ka->_sa_handler = TARGET_SIG_DFL;
929 force_sig(TARGET_SIGSEGV /* , current */);
933 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
935 unsigned int err = 0;
936 abi_ulong fpstate_addr;
937 unsigned int tmpflags;
939 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
940 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
941 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
942 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
944 env->regs[R_EDI] = tswapl(sc->edi);
945 env->regs[R_ESI] = tswapl(sc->esi);
946 env->regs[R_EBP] = tswapl(sc->ebp);
947 env->regs[R_ESP] = tswapl(sc->esp);
948 env->regs[R_EBX] = tswapl(sc->ebx);
949 env->regs[R_EDX] = tswapl(sc->edx);
950 env->regs[R_ECX] = tswapl(sc->ecx);
951 env->eip = tswapl(sc->eip);
953 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
954 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
956 tmpflags = tswapl(sc->eflags);
957 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
958 // regs->orig_eax = -1; /* disable syscall checks */
960 fpstate_addr = tswapl(sc->fpstate);
961 if (fpstate_addr != 0) {
962 if (!access_ok(VERIFY_READ, fpstate_addr,
963 sizeof(struct target_fpstate)))
965 cpu_x86_frstor(env, fpstate_addr, 1);
968 *peax = tswapl(sc->eax);
974 long do_sigreturn(CPUX86State *env)
976 struct sigframe *frame;
977 abi_ulong frame_addr = env->regs[R_ESP] - 8;
978 target_sigset_t target_set;
982 #if defined(DEBUG_SIGNAL)
983 fprintf(stderr, "do_sigreturn\n");
985 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
987 /* set blocked signals */
988 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
990 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
991 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
995 target_to_host_sigset_internal(&set, &target_set);
996 sigprocmask(SIG_SETMASK, &set, NULL);
998 /* restore registers */
999 if (restore_sigcontext(env, &frame->sc, &eax))
1001 unlock_user_struct(frame, frame_addr, 0);
1005 unlock_user_struct(frame, frame_addr, 0);
1006 force_sig(TARGET_SIGSEGV);
1010 long do_rt_sigreturn(CPUX86State *env)
1012 abi_ulong frame_addr;
1013 struct rt_sigframe *frame;
1017 frame_addr = env->regs[R_ESP] - 4;
1018 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1020 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1021 sigprocmask(SIG_SETMASK, &set, NULL);
1023 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1026 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1027 get_sp_from_cpustate(env)) == -EFAULT)
1030 unlock_user_struct(frame, frame_addr, 0);
1034 unlock_user_struct(frame, frame_addr, 0);
1035 force_sig(TARGET_SIGSEGV);
1039 #elif defined(TARGET_ARM)
1041 struct target_sigcontext {
1043 abi_ulong error_code;
1062 abi_ulong fault_address;
1065 struct target_ucontext_v1 {
1066 abi_ulong tuc_flags;
1068 target_stack_t tuc_stack;
1069 struct target_sigcontext tuc_mcontext;
1070 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1073 struct target_ucontext_v2 {
1074 abi_ulong tuc_flags;
1076 target_stack_t tuc_stack;
1077 struct target_sigcontext tuc_mcontext;
1078 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1079 char __unused[128 - sizeof(sigset_t)];
1080 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1085 struct target_sigcontext sc;
1086 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1092 struct target_ucontext_v2 uc;
1096 struct rt_sigframe_v1
1100 struct target_siginfo info;
1101 struct target_ucontext_v1 uc;
1105 struct rt_sigframe_v2
1107 struct target_siginfo info;
1108 struct target_ucontext_v2 uc;
1112 #define TARGET_CONFIG_CPU_32 1
1115 * For ARM syscalls, we encode the syscall number into the instruction.
1117 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1118 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1121 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1122 * need two 16-bit instructions.
1124 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1125 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1127 static const abi_ulong retcodes[4] = {
1128 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1129 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1133 #define __get_user_error(x,p,e) __get_user(x, p)
1135 static inline int valid_user_regs(CPUState *regs)
1141 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1142 CPUState *env, abi_ulong mask)
1144 __put_user(env->regs[0], &sc->arm_r0);
1145 __put_user(env->regs[1], &sc->arm_r1);
1146 __put_user(env->regs[2], &sc->arm_r2);
1147 __put_user(env->regs[3], &sc->arm_r3);
1148 __put_user(env->regs[4], &sc->arm_r4);
1149 __put_user(env->regs[5], &sc->arm_r5);
1150 __put_user(env->regs[6], &sc->arm_r6);
1151 __put_user(env->regs[7], &sc->arm_r7);
1152 __put_user(env->regs[8], &sc->arm_r8);
1153 __put_user(env->regs[9], &sc->arm_r9);
1154 __put_user(env->regs[10], &sc->arm_r10);
1155 __put_user(env->regs[11], &sc->arm_fp);
1156 __put_user(env->regs[12], &sc->arm_ip);
1157 __put_user(env->regs[13], &sc->arm_sp);
1158 __put_user(env->regs[14], &sc->arm_lr);
1159 __put_user(env->regs[15], &sc->arm_pc);
1160 #ifdef TARGET_CONFIG_CPU_32
1161 __put_user(cpsr_read(env), &sc->arm_cpsr);
1164 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1165 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1166 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1167 __put_user(mask, &sc->oldmask);
1170 static inline abi_ulong
1171 get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1173 unsigned long sp = regs->regs[13];
1176 * This is the X/Open sanctioned signal stack switching.
1178 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1179 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1181 * ATPCS B01 mandates 8-byte alignment
1183 return (sp - framesize) & ~7;
1187 setup_return(CPUState *env, struct target_sigaction *ka,
1188 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1190 abi_ulong handler = ka->_sa_handler;
1192 int thumb = handler & 1;
1194 if (ka->sa_flags & TARGET_SA_RESTORER) {
1195 retcode = ka->sa_restorer;
1197 unsigned int idx = thumb;
1199 if (ka->sa_flags & TARGET_SA_SIGINFO)
1202 if (__put_user(retcodes[idx], rc))
1205 flush_icache_range((abi_ulong)rc,
1206 (abi_ulong)(rc + 1));
1208 retcode = rc_addr + thumb;
1211 env->regs[0] = usig;
1212 env->regs[13] = frame_addr;
1213 env->regs[14] = retcode;
1214 env->regs[15] = handler & (thumb ? ~1 : ~3);
1218 #ifdef TARGET_CONFIG_CPU_32
1226 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1227 target_sigset_t *set, CPUState *env)
1229 struct target_sigaltstack stack;
1232 /* Clear all the bits of the ucontext we don't use. */
1233 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1235 memset(&stack, 0, sizeof(stack));
1236 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1237 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1238 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1239 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1241 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1242 /* FIXME: Save coprocessor signal frame. */
1243 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1244 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1248 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1249 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1250 target_sigset_t *set, CPUState *regs)
1252 struct sigframe_v1 *frame;
1253 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1256 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1259 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1261 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1262 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1266 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1267 frame_addr + offsetof(struct sigframe_v1, retcode));
1270 unlock_user_struct(frame, frame_addr, 1);
1273 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1274 target_sigset_t *set, CPUState *regs)
1276 struct sigframe_v2 *frame;
1277 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1279 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1282 setup_sigframe_v2(&frame->uc, set, regs);
1284 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1285 frame_addr + offsetof(struct sigframe_v2, retcode));
1287 unlock_user_struct(frame, frame_addr, 1);
1290 static void setup_frame(int usig, struct target_sigaction *ka,
1291 target_sigset_t *set, CPUState *regs)
1293 if (get_osversion() >= 0x020612) {
1294 setup_frame_v2(usig, ka, set, regs);
1296 setup_frame_v1(usig, ka, set, regs);
1300 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1301 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1302 target_siginfo_t *info,
1303 target_sigset_t *set, CPUState *env)
1305 struct rt_sigframe_v1 *frame;
1306 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1307 struct target_sigaltstack stack;
1309 abi_ulong info_addr, uc_addr;
1311 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1314 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1315 __put_user(info_addr, &frame->pinfo);
1316 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1317 __put_user(uc_addr, &frame->puc);
1318 copy_siginfo_to_user(&frame->info, info);
1320 /* Clear all the bits of the ucontext we don't use. */
1321 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1323 memset(&stack, 0, sizeof(stack));
1324 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1325 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1326 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1327 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1329 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1330 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1331 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1335 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1336 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1338 env->regs[1] = info_addr;
1339 env->regs[2] = uc_addr;
1342 unlock_user_struct(frame, frame_addr, 1);
1345 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1346 target_siginfo_t *info,
1347 target_sigset_t *set, CPUState *env)
1349 struct rt_sigframe_v2 *frame;
1350 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1351 abi_ulong info_addr, uc_addr;
1353 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1356 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1357 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1358 copy_siginfo_to_user(&frame->info, info);
1360 setup_sigframe_v2(&frame->uc, set, env);
1362 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1363 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1365 env->regs[1] = info_addr;
1366 env->regs[2] = uc_addr;
1368 unlock_user_struct(frame, frame_addr, 1);
1371 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1372 target_siginfo_t *info,
1373 target_sigset_t *set, CPUState *env)
1375 if (get_osversion() >= 0x020612) {
1376 setup_rt_frame_v2(usig, ka, info, set, env);
1378 setup_rt_frame_v1(usig, ka, info, set, env);
1383 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1388 __get_user_error(env->regs[0], &sc->arm_r0, err);
1389 __get_user_error(env->regs[1], &sc->arm_r1, err);
1390 __get_user_error(env->regs[2], &sc->arm_r2, err);
1391 __get_user_error(env->regs[3], &sc->arm_r3, err);
1392 __get_user_error(env->regs[4], &sc->arm_r4, err);
1393 __get_user_error(env->regs[5], &sc->arm_r5, err);
1394 __get_user_error(env->regs[6], &sc->arm_r6, err);
1395 __get_user_error(env->regs[7], &sc->arm_r7, err);
1396 __get_user_error(env->regs[8], &sc->arm_r8, err);
1397 __get_user_error(env->regs[9], &sc->arm_r9, err);
1398 __get_user_error(env->regs[10], &sc->arm_r10, err);
1399 __get_user_error(env->regs[11], &sc->arm_fp, err);
1400 __get_user_error(env->regs[12], &sc->arm_ip, err);
1401 __get_user_error(env->regs[13], &sc->arm_sp, err);
1402 __get_user_error(env->regs[14], &sc->arm_lr, err);
1403 __get_user_error(env->regs[15], &sc->arm_pc, err);
1404 #ifdef TARGET_CONFIG_CPU_32
1405 __get_user_error(cpsr, &sc->arm_cpsr, err);
1406 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1409 err |= !valid_user_regs(env);
1414 long do_sigreturn_v1(CPUState *env)
1416 abi_ulong frame_addr;
1417 struct sigframe_v1 *frame;
1418 target_sigset_t set;
1423 * Since we stacked the signal on a 64-bit boundary,
1424 * then 'sp' should be word aligned here. If it's
1425 * not, then the user is trying to mess with us.
1427 if (env->regs[13] & 7)
1430 frame_addr = env->regs[13];
1431 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1434 if (__get_user(set.sig[0], &frame->sc.oldmask))
1436 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1437 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1441 target_to_host_sigset_internal(&host_set, &set);
1442 sigprocmask(SIG_SETMASK, &host_set, NULL);
1444 if (restore_sigcontext(env, &frame->sc))
1448 /* Send SIGTRAP if we're single-stepping */
1449 if (ptrace_cancel_bpt(current))
1450 send_sig(SIGTRAP, current, 1);
1452 unlock_user_struct(frame, frame_addr, 0);
1453 return env->regs[0];
1456 unlock_user_struct(frame, frame_addr, 0);
1457 force_sig(SIGSEGV /* , current */);
1461 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1462 struct target_ucontext_v2 *uc)
1466 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1467 sigprocmask(SIG_SETMASK, &host_set, NULL);
1469 if (restore_sigcontext(env, &uc->tuc_mcontext))
1472 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1476 /* Send SIGTRAP if we're single-stepping */
1477 if (ptrace_cancel_bpt(current))
1478 send_sig(SIGTRAP, current, 1);
1484 long do_sigreturn_v2(CPUState *env)
1486 abi_ulong frame_addr;
1487 struct sigframe_v2 *frame;
1490 * Since we stacked the signal on a 64-bit boundary,
1491 * then 'sp' should be word aligned here. If it's
1492 * not, then the user is trying to mess with us.
1494 if (env->regs[13] & 7)
1497 frame_addr = env->regs[13];
1498 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1501 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1504 unlock_user_struct(frame, frame_addr, 0);
1505 return env->regs[0];
1508 unlock_user_struct(frame, frame_addr, 0);
1509 force_sig(SIGSEGV /* , current */);
1513 long do_sigreturn(CPUState *env)
1515 if (get_osversion() >= 0x020612) {
1516 return do_sigreturn_v2(env);
1518 return do_sigreturn_v1(env);
1522 long do_rt_sigreturn_v1(CPUState *env)
1524 abi_ulong frame_addr;
1525 struct rt_sigframe_v1 *frame;
1529 * Since we stacked the signal on a 64-bit boundary,
1530 * then 'sp' should be word aligned here. If it's
1531 * not, then the user is trying to mess with us.
1533 if (env->regs[13] & 7)
1536 frame_addr = env->regs[13];
1537 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1540 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1541 sigprocmask(SIG_SETMASK, &host_set, NULL);
1543 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1546 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1550 /* Send SIGTRAP if we're single-stepping */
1551 if (ptrace_cancel_bpt(current))
1552 send_sig(SIGTRAP, current, 1);
1554 unlock_user_struct(frame, frame_addr, 0);
1555 return env->regs[0];
1558 unlock_user_struct(frame, frame_addr, 0);
1559 force_sig(SIGSEGV /* , current */);
1563 long do_rt_sigreturn_v2(CPUState *env)
1565 abi_ulong frame_addr;
1566 struct rt_sigframe_v2 *frame;
1569 * Since we stacked the signal on a 64-bit boundary,
1570 * then 'sp' should be word aligned here. If it's
1571 * not, then the user is trying to mess with us.
1573 if (env->regs[13] & 7)
1576 frame_addr = env->regs[13];
1577 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1580 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1583 unlock_user_struct(frame, frame_addr, 0);
1584 return env->regs[0];
1587 unlock_user_struct(frame, frame_addr, 0);
1588 force_sig(SIGSEGV /* , current */);
1592 long do_rt_sigreturn(CPUState *env)
1594 if (get_osversion() >= 0x020612) {
1595 return do_rt_sigreturn_v2(env);
1597 return do_rt_sigreturn_v1(env);
1601 #elif defined(TARGET_SPARC)
1603 #define __SUNOS_MAXWIN 31
1605 /* This is what SunOS does, so shall I. */
1606 struct target_sigcontext {
1607 abi_ulong sigc_onstack; /* state to restore */
1609 abi_ulong sigc_mask; /* sigmask to restore */
1610 abi_ulong sigc_sp; /* stack pointer */
1611 abi_ulong sigc_pc; /* program counter */
1612 abi_ulong sigc_npc; /* next program counter */
1613 abi_ulong sigc_psr; /* for condition codes etc */
1614 abi_ulong sigc_g1; /* User uses these two registers */
1615 abi_ulong sigc_o0; /* within the trampoline code. */
1617 /* Now comes information regarding the users window set
1618 * at the time of the signal.
1620 abi_ulong sigc_oswins; /* outstanding windows */
1622 /* stack ptrs for each regwin buf */
1623 char *sigc_spbuf[__SUNOS_MAXWIN];
1625 /* Windows to restore after signal */
1627 abi_ulong locals[8];
1629 } sigc_wbuf[__SUNOS_MAXWIN];
1631 /* A Sparc stack frame */
1632 struct sparc_stackf {
1633 abi_ulong locals[8];
1635 struct sparc_stackf *fp;
1636 abi_ulong callers_pc;
1639 abi_ulong xxargs[1];
1648 abi_ulong u_regs[16]; /* globals and ins */
1654 unsigned long si_float_regs [32];
1655 unsigned long si_fsr;
1656 unsigned long si_fpqdepth;
1658 unsigned long *insn_addr;
1661 } qemu_siginfo_fpu_t;
1664 struct target_signal_frame {
1665 struct sparc_stackf ss;
1668 abi_ulong insns[2] __attribute__ ((aligned (8)));
1669 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1670 abi_ulong extra_size; /* Should be 0 */
1671 qemu_siginfo_fpu_t fpu_state;
1673 struct target_rt_signal_frame {
1674 struct sparc_stackf ss;
1679 unsigned int insns[2];
1681 unsigned int extra_size; /* Should be 0 */
1682 qemu_siginfo_fpu_t fpu_state;
1696 #define UREG_FP UREG_I6
1697 #define UREG_SP UREG_O6
1699 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1700 CPUState *env, unsigned long framesize)
1704 sp = env->regwptr[UREG_FP];
1706 /* This is the X/Open sanctioned signal stack switching. */
1707 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1708 if (!on_sig_stack(sp)
1709 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1710 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1712 return sp - framesize;
1716 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1720 err |= __put_user(env->psr, &si->si_regs.psr);
1721 err |= __put_user(env->pc, &si->si_regs.pc);
1722 err |= __put_user(env->npc, &si->si_regs.npc);
1723 err |= __put_user(env->y, &si->si_regs.y);
1724 for (i=0; i < 8; i++) {
1725 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1727 for (i=0; i < 8; i++) {
1728 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1730 err |= __put_user(mask, &si->si_mask);
1736 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1737 CPUState *env, unsigned long mask)
1741 err |= __put_user(mask, &sc->sigc_mask);
1742 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1743 err |= __put_user(env->pc, &sc->sigc_pc);
1744 err |= __put_user(env->npc, &sc->sigc_npc);
1745 err |= __put_user(env->psr, &sc->sigc_psr);
1746 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1747 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1752 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1754 static void setup_frame(int sig, struct target_sigaction *ka,
1755 target_sigset_t *set, CPUState *env)
1758 struct target_signal_frame *sf;
1759 int sigframe_size, err, i;
1761 /* 1. Make sure everything is clean */
1762 //synchronize_user_stack();
1764 sigframe_size = NF_ALIGNEDSZ;
1765 sf_addr = get_sigframe(ka, env, sigframe_size);
1767 sf = lock_user(VERIFY_WRITE, sf_addr,
1768 sizeof(struct target_signal_frame), 0);
1772 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1774 if (invalid_frame_pointer(sf, sigframe_size))
1775 goto sigill_and_return;
1777 /* 2. Save the current process state */
1778 err = setup___siginfo(&sf->info, env, set->sig[0]);
1779 err |= __put_user(0, &sf->extra_size);
1781 //err |= save_fpu_state(regs, &sf->fpu_state);
1782 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1784 err |= __put_user(set->sig[0], &sf->info.si_mask);
1785 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1786 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1789 for (i = 0; i < 8; i++) {
1790 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1792 for (i = 0; i < 8; i++) {
1793 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1798 /* 3. signal handler back-trampoline and parameters */
1799 env->regwptr[UREG_FP] = sf_addr;
1800 env->regwptr[UREG_I0] = sig;
1801 env->regwptr[UREG_I1] = sf_addr +
1802 offsetof(struct target_signal_frame, info);
1803 env->regwptr[UREG_I2] = sf_addr +
1804 offsetof(struct target_signal_frame, info);
1806 /* 4. signal handler */
1807 env->pc = ka->_sa_handler;
1808 env->npc = (env->pc + 4);
1809 /* 5. return to kernel instructions */
1810 if (ka->sa_restorer)
1811 env->regwptr[UREG_I7] = ka->sa_restorer;
1815 env->regwptr[UREG_I7] = sf_addr +
1816 offsetof(struct target_signal_frame, insns) - 2 * 4;
1818 /* mov __NR_sigreturn, %g1 */
1820 err |= __put_user(val32, &sf->insns[0]);
1824 err |= __put_user(val32, &sf->insns[1]);
1828 /* Flush instruction space. */
1829 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1832 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1836 force_sig(TARGET_SIGILL);
1839 //fprintf(stderr, "force_sig\n");
1840 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1841 force_sig(TARGET_SIGSEGV);
1844 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
1849 if (current->flags & PF_USEDFPU)
1850 regs->psr &= ~PSR_EF;
1852 if (current == last_task_used_math) {
1853 last_task_used_math = 0;
1854 regs->psr &= ~PSR_EF;
1857 current->used_math = 1;
1858 current->flags &= ~PF_USEDFPU;
1861 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1866 /* XXX: incorrect */
1867 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1868 (sizeof(unsigned long) * 32));
1870 err |= __get_user(env->fsr, &fpu->si_fsr);
1872 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1873 if (current->thread.fpqdepth != 0)
1874 err |= __copy_from_user(¤t->thread.fpqueue[0],
1875 &fpu->si_fpqueue[0],
1876 ((sizeof(unsigned long) +
1877 (sizeof(unsigned long *)))*16));
1883 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1884 target_siginfo_t *info,
1885 target_sigset_t *set, CPUState *env)
1887 fprintf(stderr, "setup_rt_frame: not implemented\n");
1890 long do_sigreturn(CPUState *env)
1893 struct target_signal_frame *sf;
1894 uint32_t up_psr, pc, npc;
1895 target_sigset_t set;
1897 abi_ulong fpu_save_addr;
1900 sf_addr = env->regwptr[UREG_FP];
1901 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1904 fprintf(stderr, "sigreturn\n");
1905 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1907 //cpu_dump_state(env, stderr, fprintf, 0);
1909 /* 1. Make sure we are not getting garbage from the user */
1914 err = __get_user(pc, &sf->info.si_regs.pc);
1915 err |= __get_user(npc, &sf->info.si_regs.npc);
1920 /* 2. Restore the state */
1921 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1923 /* User can only change condition codes and FPU enabling in %psr. */
1924 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1925 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1929 err |= __get_user(env->y, &sf->info.si_regs.y);
1930 for (i=0; i < 8; i++) {
1931 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1933 for (i=0; i < 8; i++) {
1934 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1937 err |= __get_user(fpu_save_addr, &sf->fpu_save);
1940 // err |= restore_fpu_state(env, fpu_save);
1942 /* This is pretty much atomic, no amount locking would prevent
1943 * the races which exist anyways.
1945 err |= __get_user(set.sig[0], &sf->info.si_mask);
1946 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1947 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1950 target_to_host_sigset_internal(&host_set, &set);
1951 sigprocmask(SIG_SETMASK, &host_set, NULL);
1955 unlock_user_struct(sf, sf_addr, 0);
1956 return env->regwptr[0];
1959 unlock_user_struct(sf, sf_addr, 0);
1960 force_sig(TARGET_SIGSEGV);
1963 long do_rt_sigreturn(CPUState *env)
1965 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1966 return -TARGET_ENOSYS;
1969 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1991 typedef abi_ulong target_mc_greg_t;
1992 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1994 struct target_mc_fq {
1995 abi_ulong *mcfq_addr;
1999 struct target_mc_fpu {
2003 //uint128_t qregs[16];
2005 abi_ulong mcfpu_fsr;
2006 abi_ulong mcfpu_fprs;
2007 abi_ulong mcfpu_gsr;
2008 struct target_mc_fq *mcfpu_fq;
2009 unsigned char mcfpu_qcnt;
2010 unsigned char mcfpu_qentsz;
2011 unsigned char mcfpu_enab;
2013 typedef struct target_mc_fpu target_mc_fpu_t;
2016 target_mc_gregset_t mc_gregs;
2017 target_mc_greg_t mc_fp;
2018 target_mc_greg_t mc_i7;
2019 target_mc_fpu_t mc_fpregs;
2020 } target_mcontext_t;
2022 struct target_ucontext {
2023 struct target_ucontext *uc_link;
2025 target_sigset_t uc_sigmask;
2026 target_mcontext_t uc_mcontext;
2029 /* A V9 register window */
2030 struct target_reg_window {
2031 abi_ulong locals[8];
2035 #define TARGET_STACK_BIAS 2047
2037 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2038 void sparc64_set_context(CPUSPARCState *env)
2041 struct target_ucontext *ucp;
2042 target_mc_gregset_t *grp;
2043 abi_ulong pc, npc, tstate;
2044 abi_ulong fp, i7, w_addr;
2045 unsigned char fenab;
2049 ucp_addr = env->regwptr[UREG_I0];
2050 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2052 grp = &ucp->uc_mcontext.mc_gregs;
2053 err = __get_user(pc, &((*grp)[MC_PC]));
2054 err |= __get_user(npc, &((*grp)[MC_NPC]));
2055 if (err || ((pc | npc) & 3))
2057 if (env->regwptr[UREG_I1]) {
2058 target_sigset_t target_set;
2061 if (TARGET_NSIG_WORDS == 1) {
2062 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
2065 abi_ulong *src, *dst;
2066 src = ucp->uc_sigmask.sig;
2067 dst = target_set.sig;
2068 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2070 err |= __get_user(*dst, src);
2074 target_to_host_sigset_internal(&set, &target_set);
2075 sigprocmask(SIG_SETMASK, &set, NULL);
2079 err |= __get_user(env->y, &((*grp)[MC_Y]));
2080 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2081 env->asi = (tstate >> 24) & 0xff;
2082 PUT_CCR(env, tstate >> 32);
2083 PUT_CWP64(env, tstate & 0x1f);
2084 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2085 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2086 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2087 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2088 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2089 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2090 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2091 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2092 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2093 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2094 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2095 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2096 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2097 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2098 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2100 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2101 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
2103 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2104 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2107 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2110 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2111 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
2113 uint32_t *src, *dst;
2114 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2116 /* XXX: check that the CPU storage is the same as user context */
2117 for (i = 0; i < 64; i++, dst++, src++)
2118 err |= __get_user(*dst, src);
2120 err |= __get_user(env->fsr,
2121 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2122 err |= __get_user(env->gsr,
2123 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
2126 unlock_user_struct(ucp, ucp_addr, 0);
2129 unlock_user_struct(ucp, ucp_addr, 0);
2133 void sparc64_get_context(CPUSPARCState *env)
2136 struct target_ucontext *ucp;
2137 target_mc_gregset_t *grp;
2138 target_mcontext_t *mcp;
2139 abi_ulong fp, i7, w_addr;
2142 target_sigset_t target_set;
2145 ucp_addr = env->regwptr[UREG_I0];
2146 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2149 mcp = &ucp->uc_mcontext;
2150 grp = &mcp->mc_gregs;
2152 /* Skip over the trap instruction, first. */
2158 sigprocmask(0, NULL, &set);
2159 host_to_target_sigset_internal(&target_set, &set);
2160 if (TARGET_NSIG_WORDS == 1) {
2161 err |= __put_user(target_set.sig[0],
2162 (abi_ulong *)&ucp->uc_sigmask);
2164 abi_ulong *src, *dst;
2165 src = target_set.sig;
2166 dst = ucp->uc_sigmask.sig;
2167 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2169 err |= __put_user(*src, dst);
2174 /* XXX: tstate must be saved properly */
2175 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2176 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2177 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2178 err |= __put_user(env->y, &((*grp)[MC_Y]));
2179 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2180 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2181 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2182 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2183 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2184 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2185 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2186 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2187 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2188 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2189 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2190 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2191 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2192 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2193 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2195 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2197 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2200 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2203 err |= __put_user(fp, &(mcp->mc_fp));
2204 err |= __put_user(i7, &(mcp->mc_i7));
2207 uint32_t *src, *dst;
2209 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2210 /* XXX: check that the CPU storage is the same as user context */
2211 for (i = 0; i < 64; i++, dst++, src++)
2212 err |= __put_user(*src, dst);
2214 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2215 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2216 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2220 unlock_user_struct(ucp, ucp_addr, 1);
2223 unlock_user_struct(ucp, ucp_addr, 1);
2227 #elif defined(TARGET_ABI_MIPSN64)
2229 # warning signal handling not implemented
2231 static void setup_frame(int sig, struct target_sigaction *ka,
2232 target_sigset_t *set, CPUState *env)
2234 fprintf(stderr, "setup_frame: not implemented\n");
2237 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2238 target_siginfo_t *info,
2239 target_sigset_t *set, CPUState *env)
2241 fprintf(stderr, "setup_rt_frame: not implemented\n");
2244 long do_sigreturn(CPUState *env)
2246 fprintf(stderr, "do_sigreturn: not implemented\n");
2247 return -TARGET_ENOSYS;
2250 long do_rt_sigreturn(CPUState *env)
2252 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2253 return -TARGET_ENOSYS;
2256 #elif defined(TARGET_ABI_MIPSN32)
2258 # warning signal handling not implemented
2260 static void setup_frame(int sig, struct target_sigaction *ka,
2261 target_sigset_t *set, CPUState *env)
2263 fprintf(stderr, "setup_frame: not implemented\n");
2266 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2267 target_siginfo_t *info,
2268 target_sigset_t *set, CPUState *env)
2270 fprintf(stderr, "setup_rt_frame: not implemented\n");
2273 long do_sigreturn(CPUState *env)
2275 fprintf(stderr, "do_sigreturn: not implemented\n");
2276 return -TARGET_ENOSYS;
2279 long do_rt_sigreturn(CPUState *env)
2281 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2282 return -TARGET_ENOSYS;
2285 #elif defined(TARGET_ABI_MIPSO32)
2287 struct target_sigcontext {
2288 uint32_t sc_regmask; /* Unused */
2291 uint64_t sc_regs[32];
2292 uint64_t sc_fpregs[32];
2293 uint32_t sc_ownedfp; /* Unused */
2294 uint32_t sc_fpc_csr;
2295 uint32_t sc_fpc_eir; /* Unused */
2296 uint32_t sc_used_math;
2297 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2300 target_ulong sc_hi1; /* Was sc_cause */
2301 target_ulong sc_lo1; /* Was sc_badvaddr */
2302 target_ulong sc_hi2; /* Was sc_sigset[4] */
2303 target_ulong sc_lo2;
2304 target_ulong sc_hi3;
2305 target_ulong sc_lo3;
2309 uint32_t sf_ass[4]; /* argument save space for o32 */
2310 uint32_t sf_code[2]; /* signal trampoline */
2311 struct target_sigcontext sf_sc;
2312 target_sigset_t sf_mask;
2315 /* Install trampoline to jump back from signal handler */
2316 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2321 * Set up the return code ...
2323 * li v0, __NR__foo_sigreturn
2327 err = __put_user(0x24020000 + syscall, tramp + 0);
2328 err |= __put_user(0x0000000c , tramp + 1);
2329 /* flush_cache_sigtramp((unsigned long) tramp); */
2334 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2338 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2340 #define save_gp_reg(i) do { \
2341 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2343 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2344 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2345 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2346 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2347 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2348 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2349 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2350 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2354 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2355 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2357 /* Not used yet, but might be useful if we ever have DSP suppport */
2360 err |= __put_user(mfhi1(), &sc->sc_hi1);
2361 err |= __put_user(mflo1(), &sc->sc_lo1);
2362 err |= __put_user(mfhi2(), &sc->sc_hi2);
2363 err |= __put_user(mflo2(), &sc->sc_lo2);
2364 err |= __put_user(mfhi3(), &sc->sc_hi3);
2365 err |= __put_user(mflo3(), &sc->sc_lo3);
2366 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2368 /* same with 64 bit */
2370 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2371 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2373 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2374 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2375 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2376 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2377 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2378 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2379 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2385 err |= __put_user(!!used_math(), &sc->sc_used_math);
2391 * Save FPU state to signal context. Signal handler will "inherit"
2392 * current FPU state.
2396 if (!is_fpu_owner()) {
2398 restore_fp(current);
2400 err |= save_fp_context(sc);
2409 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2413 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2415 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2416 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2418 #define restore_gp_reg(i) do { \
2419 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2421 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2422 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2423 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2424 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2425 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2426 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2427 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2428 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2429 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2430 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2432 #undef restore_gp_reg
2436 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2437 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2438 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2439 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2440 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2441 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2442 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2445 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2446 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2448 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2449 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2450 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2451 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2452 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2453 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2454 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2458 err |= __get_user(used_math, &sc->sc_used_math);
2459 conditional_used_math(used_math);
2464 /* restore fpu context if we have used it before */
2466 err |= restore_fp_context(sc);
2468 /* signal handler may have used FPU. Give it up. */
2477 * Determine which stack to use..
2479 static inline abi_ulong
2480 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2484 /* Default to using normal stack */
2485 sp = regs->active_tc.gpr[29];
2488 * FPU emulator may have it's own trampoline active just
2489 * above the user stack, 16-bytes before the next lowest
2490 * 16 byte boundary. Try to avoid trashing it.
2494 /* This is the X/Open sanctioned signal stack switching. */
2495 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2496 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2499 return (sp - frame_size) & ~7;
2502 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2503 static void setup_frame(int sig, struct target_sigaction * ka,
2504 target_sigset_t *set, CPUState *regs)
2506 struct sigframe *frame;
2507 abi_ulong frame_addr;
2510 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2511 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2514 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2516 if(setup_sigcontext(regs, &frame->sf_sc))
2519 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2520 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2525 * Arguments to signal handler:
2527 * a0 = signal number
2528 * a1 = 0 (should be cause)
2529 * a2 = pointer to struct sigcontext
2531 * $25 and PC point to the signal handler, $29 points to the
2534 regs->active_tc.gpr[ 4] = sig;
2535 regs->active_tc.gpr[ 5] = 0;
2536 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2537 regs->active_tc.gpr[29] = frame_addr;
2538 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2539 /* The original kernel code sets CP0_EPC to the handler
2540 * since it returns to userland using eret
2541 * we cannot do this here, and we must set PC directly */
2542 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2543 unlock_user_struct(frame, frame_addr, 1);
2547 unlock_user_struct(frame, frame_addr, 1);
2548 force_sig(TARGET_SIGSEGV/*, current*/);
2552 long do_sigreturn(CPUState *regs)
2554 struct sigframe *frame;
2555 abi_ulong frame_addr;
2557 target_sigset_t target_set;
2560 #if defined(DEBUG_SIGNAL)
2561 fprintf(stderr, "do_sigreturn\n");
2563 frame_addr = regs->active_tc.gpr[29];
2564 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2567 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2568 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2572 target_to_host_sigset_internal(&blocked, &target_set);
2573 sigprocmask(SIG_SETMASK, &blocked, NULL);
2575 if (restore_sigcontext(regs, &frame->sf_sc))
2580 * Don't let your children do this ...
2582 __asm__ __volatile__(
2590 regs->active_tc.PC = regs->CP0_EPC;
2591 /* I am not sure this is right, but it seems to work
2592 * maybe a problem with nested signals ? */
2597 force_sig(TARGET_SIGSEGV/*, current*/);
2601 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2602 target_siginfo_t *info,
2603 target_sigset_t *set, CPUState *env)
2605 fprintf(stderr, "setup_rt_frame: not implemented\n");
2608 long do_rt_sigreturn(CPUState *env)
2610 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2611 return -TARGET_ENOSYS;
2614 #elif defined(TARGET_SH4)
2617 * code and data structures from linux kernel:
2618 * include/asm-sh/sigcontext.h
2619 * arch/sh/kernel/signal.c
2622 struct target_sigcontext {
2623 target_ulong oldmask;
2626 target_ulong sc_gregs[16];
2630 target_ulong sc_gbr;
2631 target_ulong sc_mach;
2632 target_ulong sc_macl;
2635 target_ulong sc_fpregs[16];
2636 target_ulong sc_xfpregs[16];
2637 unsigned int sc_fpscr;
2638 unsigned int sc_fpul;
2639 unsigned int sc_ownedfp;
2642 struct target_sigframe
2644 struct target_sigcontext sc;
2645 target_ulong extramask[TARGET_NSIG_WORDS-1];
2646 uint16_t retcode[3];
2650 struct target_ucontext {
2651 target_ulong uc_flags;
2652 struct target_ucontext *uc_link;
2653 target_stack_t uc_stack;
2654 struct target_sigcontext uc_mcontext;
2655 target_sigset_t uc_sigmask; /* mask last for extensibility */
2658 struct target_rt_sigframe
2660 struct target_siginfo info;
2661 struct target_ucontext uc;
2662 uint16_t retcode[3];
2666 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2667 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2669 static abi_ulong get_sigframe(struct target_sigaction *ka,
2670 unsigned long sp, size_t frame_size)
2672 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2673 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2676 return (sp - frame_size) & -8ul;
2679 static int setup_sigcontext(struct target_sigcontext *sc,
2680 CPUState *regs, unsigned long mask)
2684 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2685 COPY(gregs[0]); COPY(gregs[1]);
2686 COPY(gregs[2]); COPY(gregs[3]);
2687 COPY(gregs[4]); COPY(gregs[5]);
2688 COPY(gregs[6]); COPY(gregs[7]);
2689 COPY(gregs[8]); COPY(gregs[9]);
2690 COPY(gregs[10]); COPY(gregs[11]);
2691 COPY(gregs[12]); COPY(gregs[13]);
2692 COPY(gregs[14]); COPY(gregs[15]);
2693 COPY(gbr); COPY(mach);
2694 COPY(macl); COPY(pr);
2698 /* todo: save FPU registers here */
2700 /* non-iBCS2 extensions.. */
2701 err |= __put_user(mask, &sc->oldmask);
2706 static int restore_sigcontext(struct CPUState *regs,
2707 struct target_sigcontext *sc)
2709 unsigned int err = 0;
2711 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2713 COPY(gregs[2]); COPY(gregs[3]);
2714 COPY(gregs[4]); COPY(gregs[5]);
2715 COPY(gregs[6]); COPY(gregs[7]);
2716 COPY(gregs[8]); COPY(gregs[9]);
2717 COPY(gregs[10]); COPY(gregs[11]);
2718 COPY(gregs[12]); COPY(gregs[13]);
2719 COPY(gregs[14]); COPY(gregs[15]);
2720 COPY(gbr); COPY(mach);
2721 COPY(macl); COPY(pr);
2725 /* todo: restore FPU registers here */
2727 regs->tra = -1; /* disable syscall checks */
2731 static void setup_frame(int sig, struct target_sigaction *ka,
2732 target_sigset_t *set, CPUState *regs)
2734 struct target_sigframe *frame;
2735 abi_ulong frame_addr;
2740 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2741 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2744 signal = current_exec_domain_sig(sig);
2746 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2748 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2749 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2752 /* Set up to return from userspace. If provided, use a stub
2753 already in userspace. */
2754 if (ka->sa_flags & TARGET_SA_RESTORER) {
2755 regs->pr = (unsigned long) ka->sa_restorer;
2757 /* Generate return code (system call to sigreturn) */
2758 err |= __put_user(MOVW(2), &frame->retcode[0]);
2759 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2760 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2761 regs->pr = (unsigned long) frame->retcode;
2767 /* Set up registers for signal handler */
2768 regs->gregs[15] = (unsigned long) frame;
2769 regs->gregs[4] = signal; /* Arg for signal handler */
2771 regs->gregs[6] = (unsigned long) &frame->sc;
2772 regs->pc = (unsigned long) ka->_sa_handler;
2774 unlock_user_struct(frame, frame_addr, 1);
2778 unlock_user_struct(frame, frame_addr, 1);
2782 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2783 target_siginfo_t *info,
2784 target_sigset_t *set, CPUState *regs)
2786 struct target_rt_sigframe *frame;
2787 abi_ulong frame_addr;
2792 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2793 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2796 signal = current_exec_domain_sig(sig);
2798 err |= copy_siginfo_to_user(&frame->info, info);
2800 /* Create the ucontext. */
2801 err |= __put_user(0, &frame->uc.uc_flags);
2802 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2803 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
2804 &frame->uc.uc_stack.ss_sp);
2805 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2806 &frame->uc.uc_stack.ss_flags);
2807 err |= __put_user(target_sigaltstack_used.ss_size,
2808 &frame->uc.uc_stack.ss_size);
2809 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2811 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2812 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2815 /* Set up to return from userspace. If provided, use a stub
2816 already in userspace. */
2817 if (ka->sa_flags & TARGET_SA_RESTORER) {
2818 regs->pr = (unsigned long) ka->sa_restorer;
2820 /* Generate return code (system call to sigreturn) */
2821 err |= __put_user(MOVW(2), &frame->retcode[0]);
2822 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2823 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2824 regs->pr = (unsigned long) frame->retcode;
2830 /* Set up registers for signal handler */
2831 regs->gregs[15] = (unsigned long) frame;
2832 regs->gregs[4] = signal; /* Arg for signal handler */
2833 regs->gregs[5] = (unsigned long) &frame->info;
2834 regs->gregs[6] = (unsigned long) &frame->uc;
2835 regs->pc = (unsigned long) ka->_sa_handler;
2837 unlock_user_struct(frame, frame_addr, 1);
2841 unlock_user_struct(frame, frame_addr, 1);
2845 long do_sigreturn(CPUState *regs)
2847 struct target_sigframe *frame;
2848 abi_ulong frame_addr;
2850 target_sigset_t target_set;
2854 #if defined(DEBUG_SIGNAL)
2855 fprintf(stderr, "do_sigreturn\n");
2857 frame_addr = regs->gregs[15];
2858 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2861 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2862 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2863 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2869 target_to_host_sigset_internal(&blocked, &target_set);
2870 sigprocmask(SIG_SETMASK, &blocked, NULL);
2872 if (restore_sigcontext(regs, &frame->sc))
2875 unlock_user_struct(frame, frame_addr, 0);
2876 return regs->gregs[0];
2879 unlock_user_struct(frame, frame_addr, 0);
2880 force_sig(TARGET_SIGSEGV);
2884 long do_rt_sigreturn(CPUState *regs)
2886 struct target_rt_sigframe *frame;
2887 abi_ulong frame_addr;
2890 #if defined(DEBUG_SIGNAL)
2891 fprintf(stderr, "do_rt_sigreturn\n");
2893 frame_addr = regs->gregs[15];
2894 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2897 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2898 sigprocmask(SIG_SETMASK, &blocked, NULL);
2900 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2903 if (do_sigaltstack(frame_addr +
2904 offsetof(struct target_rt_sigframe, uc.uc_stack),
2905 0, get_sp_from_cpustate(regs)) == -EFAULT)
2908 unlock_user_struct(frame, frame_addr, 0);
2909 return regs->gregs[0];
2912 unlock_user_struct(frame, frame_addr, 0);
2913 force_sig(TARGET_SIGSEGV);
2916 #elif defined(TARGET_CRIS)
2918 struct target_sigcontext {
2919 struct target_pt_regs regs; /* needs to be first */
2921 uint32_t usp; /* usp before stacking this gunk on it */
2924 /* Signal frames. */
2925 struct target_signal_frame {
2926 struct target_sigcontext sc;
2927 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2928 uint8_t retcode[8]; /* Trampoline code. */
2931 struct rt_signal_frame {
2932 struct siginfo *pinfo;
2934 struct siginfo info;
2936 uint8_t retcode[8]; /* Trampoline code. */
2939 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2941 __put_user(env->regs[0], &sc->regs.r0);
2942 __put_user(env->regs[1], &sc->regs.r1);
2943 __put_user(env->regs[2], &sc->regs.r2);
2944 __put_user(env->regs[3], &sc->regs.r3);
2945 __put_user(env->regs[4], &sc->regs.r4);
2946 __put_user(env->regs[5], &sc->regs.r5);
2947 __put_user(env->regs[6], &sc->regs.r6);
2948 __put_user(env->regs[7], &sc->regs.r7);
2949 __put_user(env->regs[8], &sc->regs.r8);
2950 __put_user(env->regs[9], &sc->regs.r9);
2951 __put_user(env->regs[10], &sc->regs.r10);
2952 __put_user(env->regs[11], &sc->regs.r11);
2953 __put_user(env->regs[12], &sc->regs.r12);
2954 __put_user(env->regs[13], &sc->regs.r13);
2955 __put_user(env->regs[14], &sc->usp);
2956 __put_user(env->regs[15], &sc->regs.acr);
2957 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2958 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2959 __put_user(env->pc, &sc->regs.erp);
2962 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2964 __get_user(env->regs[0], &sc->regs.r0);
2965 __get_user(env->regs[1], &sc->regs.r1);
2966 __get_user(env->regs[2], &sc->regs.r2);
2967 __get_user(env->regs[3], &sc->regs.r3);
2968 __get_user(env->regs[4], &sc->regs.r4);
2969 __get_user(env->regs[5], &sc->regs.r5);
2970 __get_user(env->regs[6], &sc->regs.r6);
2971 __get_user(env->regs[7], &sc->regs.r7);
2972 __get_user(env->regs[8], &sc->regs.r8);
2973 __get_user(env->regs[9], &sc->regs.r9);
2974 __get_user(env->regs[10], &sc->regs.r10);
2975 __get_user(env->regs[11], &sc->regs.r11);
2976 __get_user(env->regs[12], &sc->regs.r12);
2977 __get_user(env->regs[13], &sc->regs.r13);
2978 __get_user(env->regs[14], &sc->usp);
2979 __get_user(env->regs[15], &sc->regs.acr);
2980 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2981 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2982 __get_user(env->pc, &sc->regs.erp);
2985 static abi_ulong get_sigframe(CPUState *env, int framesize)
2988 /* Align the stack downwards to 4. */
2989 sp = (env->regs[R_SP] & ~3);
2990 return sp - framesize;
2993 static void setup_frame(int sig, struct target_sigaction *ka,
2994 target_sigset_t *set, CPUState *env)
2996 struct target_signal_frame *frame;
2997 abi_ulong frame_addr;
3001 frame_addr = get_sigframe(env, sizeof *frame);
3002 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3006 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3007 * use this trampoline anymore but it sets it up for GDB.
3008 * In QEMU, using the trampoline simplifies things a bit so we use it.
3010 * This is movu.w __NR_sigreturn, r9; break 13;
3012 err |= __put_user(0x9c5f, frame->retcode+0);
3013 err |= __put_user(TARGET_NR_sigreturn,
3015 err |= __put_user(0xe93d, frame->retcode+4);
3017 /* Save the mask. */
3018 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3022 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3023 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3027 setup_sigcontext(&frame->sc, env);
3029 /* Move the stack and setup the arguments for the handler. */
3030 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
3031 env->regs[10] = sig;
3032 env->pc = (unsigned long) ka->_sa_handler;
3033 /* Link SRP so the guest returns through the trampoline. */
3034 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
3036 unlock_user_struct(frame, frame_addr, 1);
3039 unlock_user_struct(frame, frame_addr, 1);
3040 force_sig(TARGET_SIGSEGV);
3043 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3044 target_siginfo_t *info,
3045 target_sigset_t *set, CPUState *env)
3047 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3050 long do_sigreturn(CPUState *env)
3052 struct target_signal_frame *frame;
3053 abi_ulong frame_addr;
3054 target_sigset_t target_set;
3058 frame_addr = env->regs[R_SP];
3059 /* Make sure the guest isn't playing games. */
3060 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3063 /* Restore blocked signals */
3064 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3066 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3067 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3070 target_to_host_sigset_internal(&set, &target_set);
3071 sigprocmask(SIG_SETMASK, &set, NULL);
3073 restore_sigcontext(&frame->sc, env);
3074 unlock_user_struct(frame, frame_addr, 0);
3075 return env->regs[10];
3077 unlock_user_struct(frame, frame_addr, 0);
3078 force_sig(TARGET_SIGSEGV);
3081 long do_rt_sigreturn(CPUState *env)
3083 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3084 return -TARGET_ENOSYS;
3089 static void setup_frame(int sig, struct target_sigaction *ka,
3090 target_sigset_t *set, CPUState *env)
3092 fprintf(stderr, "setup_frame: not implemented\n");
3095 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3096 target_siginfo_t *info,
3097 target_sigset_t *set, CPUState *env)
3099 fprintf(stderr, "setup_rt_frame: not implemented\n");
3102 long do_sigreturn(CPUState *env)
3104 fprintf(stderr, "do_sigreturn: not implemented\n");
3105 return -TARGET_ENOSYS;
3108 long do_rt_sigreturn(CPUState *env)
3110 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
3111 return -TARGET_ENOSYS;
3116 void process_pending_signals(CPUState *cpu_env)
3120 sigset_t set, old_set;
3121 target_sigset_t target_old_set;
3122 struct emulated_sigtable *k;
3123 struct target_sigaction *sa;
3125 TaskState *ts = cpu_env->opaque;
3127 if (!ts->signal_pending)
3130 /* FIXME: This is not threadsafe. */
3132 for(sig = 1; sig <= TARGET_NSIG; sig++) {
3137 /* if no signal is pending, just return */
3138 ts->signal_pending = 0;
3143 fprintf(stderr, "qemu: process signal %d\n", sig);
3145 /* dequeue signal */
3151 sig = gdb_handlesig (cpu_env, sig);
3154 handler = TARGET_SIG_IGN;
3156 sa = &sigact_table[sig - 1];
3157 handler = sa->_sa_handler;
3160 if (handler == TARGET_SIG_DFL) {
3161 /* default handler : ignore some signal. The other are job control or fatal */
3162 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
3163 kill(getpid(),SIGSTOP);
3164 } else if (sig != TARGET_SIGCHLD &&
3165 sig != TARGET_SIGURG &&
3166 sig != TARGET_SIGWINCH &&
3167 sig != TARGET_SIGCONT) {
3170 } else if (handler == TARGET_SIG_IGN) {
3172 } else if (handler == TARGET_SIG_ERR) {
3175 /* compute the blocked signals during the handler execution */
3176 target_to_host_sigset(&set, &sa->sa_mask);
3177 /* SA_NODEFER indicates that the current signal should not be
3178 blocked during the handler */
3179 if (!(sa->sa_flags & TARGET_SA_NODEFER))
3180 sigaddset(&set, target_to_host_signal(sig));
3182 /* block signals in the handler using Linux */
3183 sigprocmask(SIG_BLOCK, &set, &old_set);
3184 /* save the previous blocked signal state to restore it at the
3185 end of the signal execution (see do_sigreturn) */
3186 host_to_target_sigset_internal(&target_old_set, &old_set);
3188 /* if the CPU is in VM86 mode, we restore the 32 bit values */
3189 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
3191 CPUX86State *env = cpu_env;
3192 if (env->eflags & VM_MASK)
3193 save_v86_state(env);
3196 /* prepare the stack frame of the virtual CPU */
3197 if (sa->sa_flags & TARGET_SA_SIGINFO)
3198 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
3200 setup_frame(sig, sa, &target_old_set, cpu_env);
3201 if (sa->sa_flags & TARGET_SA_RESETHAND)
3202 sa->_sa_handler = TARGET_SIG_DFL;
3205 free_sigqueue(cpu_env, q);