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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/ucontext.h>
30 #include "target_signal.h"
32 //#define DEBUG_SIGNAL
34 #define MAX_SIGQUEUE_SIZE 1024
37 struct sigqueue *next;
38 target_siginfo_t info;
41 struct emulated_sigaction {
42 struct target_sigaction sa;
43 int pending; /* true if signal is pending */
44 struct sigqueue *first;
45 struct sigqueue info; /* in order to always have memory for the
46 first signal, we put it here */
49 struct target_sigaltstack target_sigaltstack_used = {
52 .ss_flags = TARGET_SS_DISABLE,
55 static struct emulated_sigaction sigact_table[TARGET_NSIG];
56 static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57 static struct sigqueue *first_free; /* first free siginfo queue entry */
58 static int signal_pending; /* non zero if a signal may be pending */
60 static void host_signal_handler(int host_signum, siginfo_t *info,
63 static uint8_t host_to_target_signal_table[65] = {
64 [SIGHUP] = TARGET_SIGHUP,
65 [SIGINT] = TARGET_SIGINT,
66 [SIGQUIT] = TARGET_SIGQUIT,
67 [SIGILL] = TARGET_SIGILL,
68 [SIGTRAP] = TARGET_SIGTRAP,
69 [SIGABRT] = TARGET_SIGABRT,
70 /* [SIGIOT] = TARGET_SIGIOT,*/
71 [SIGBUS] = TARGET_SIGBUS,
72 [SIGFPE] = TARGET_SIGFPE,
73 [SIGKILL] = TARGET_SIGKILL,
74 [SIGUSR1] = TARGET_SIGUSR1,
75 [SIGSEGV] = TARGET_SIGSEGV,
76 [SIGUSR2] = TARGET_SIGUSR2,
77 [SIGPIPE] = TARGET_SIGPIPE,
78 [SIGALRM] = TARGET_SIGALRM,
79 [SIGTERM] = TARGET_SIGTERM,
81 [SIGSTKFLT] = TARGET_SIGSTKFLT,
83 [SIGCHLD] = TARGET_SIGCHLD,
84 [SIGCONT] = TARGET_SIGCONT,
85 [SIGSTOP] = TARGET_SIGSTOP,
86 [SIGTSTP] = TARGET_SIGTSTP,
87 [SIGTTIN] = TARGET_SIGTTIN,
88 [SIGTTOU] = TARGET_SIGTTOU,
89 [SIGURG] = TARGET_SIGURG,
90 [SIGXCPU] = TARGET_SIGXCPU,
91 [SIGXFSZ] = TARGET_SIGXFSZ,
92 [SIGVTALRM] = TARGET_SIGVTALRM,
93 [SIGPROF] = TARGET_SIGPROF,
94 [SIGWINCH] = TARGET_SIGWINCH,
95 [SIGIO] = TARGET_SIGIO,
96 [SIGPWR] = TARGET_SIGPWR,
97 [SIGSYS] = TARGET_SIGSYS,
98 /* next signals stay the same */
100 static uint8_t target_to_host_signal_table[65];
102 static inline int on_sig_stack(unsigned long sp)
104 return (sp - target_sigaltstack_used.ss_sp
105 < target_sigaltstack_used.ss_size);
108 static inline int sas_ss_flags(unsigned long sp)
110 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
111 : on_sig_stack(sp) ? SS_ONSTACK : 0);
114 static inline int host_to_target_signal(int sig)
118 return host_to_target_signal_table[sig];
121 int target_to_host_signal(int sig)
125 return target_to_host_signal_table[sig];
128 static inline void target_sigemptyset(target_sigset_t *set)
130 memset(set, 0, sizeof(*set));
133 static inline void target_sigaddset(target_sigset_t *set, int signum)
136 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
137 set->sig[signum / TARGET_NSIG_BPW] |= mask;
140 static inline int target_sigismember(const target_sigset_t *set, int signum)
143 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
144 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
147 static void host_to_target_sigset_internal(target_sigset_t *d,
151 target_sigemptyset(d);
152 for (i = 1; i <= TARGET_NSIG; i++) {
153 if (sigismember(s, i)) {
154 target_sigaddset(d, host_to_target_signal(i));
159 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
164 host_to_target_sigset_internal(&d1, s);
165 for(i = 0;i < TARGET_NSIG_WORDS; i++)
166 d->sig[i] = tswapl(d1.sig[i]);
169 void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
173 for (i = 1; i <= TARGET_NSIG; i++) {
174 if (target_sigismember(s, i)) {
175 sigaddset(d, target_to_host_signal(i));
180 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
185 for(i = 0;i < TARGET_NSIG_WORDS; i++)
186 s1.sig[i] = tswapl(s->sig[i]);
187 target_to_host_sigset_internal(d, &s1);
190 void host_to_target_old_sigset(abi_ulong *old_sigset,
191 const sigset_t *sigset)
194 host_to_target_sigset(&d, sigset);
195 *old_sigset = d.sig[0];
198 void target_to_host_old_sigset(sigset_t *sigset,
199 const abi_ulong *old_sigset)
204 d.sig[0] = *old_sigset;
205 for(i = 1;i < TARGET_NSIG_WORDS; i++)
207 target_to_host_sigset(sigset, &d);
210 /* siginfo conversion */
212 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
213 const siginfo_t *info)
216 sig = host_to_target_signal(info->si_signo);
217 tinfo->si_signo = sig;
220 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
221 sig == SIGBUS || sig == SIGTRAP) {
222 /* should never come here, but who knows. The information for
223 the target is irrelevant */
224 tinfo->_sifields._sigfault._addr = 0;
225 } else if (sig == SIGIO) {
226 tinfo->_sifields._sigpoll._fd = info->si_fd;
227 } else if (sig >= TARGET_SIGRTMIN) {
228 tinfo->_sifields._rt._pid = info->si_pid;
229 tinfo->_sifields._rt._uid = info->si_uid;
230 /* XXX: potential problem if 64 bit */
231 tinfo->_sifields._rt._sigval.sival_ptr =
232 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
236 static void tswap_siginfo(target_siginfo_t *tinfo,
237 const target_siginfo_t *info)
240 sig = info->si_signo;
241 tinfo->si_signo = tswap32(sig);
242 tinfo->si_errno = tswap32(info->si_errno);
243 tinfo->si_code = tswap32(info->si_code);
244 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
245 sig == SIGBUS || sig == SIGTRAP) {
246 tinfo->_sifields._sigfault._addr =
247 tswapl(info->_sifields._sigfault._addr);
248 } else if (sig == SIGIO) {
249 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
250 } else if (sig >= TARGET_SIGRTMIN) {
251 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
252 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
253 tinfo->_sifields._rt._sigval.sival_ptr =
254 tswapl(info->_sifields._rt._sigval.sival_ptr);
259 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
261 host_to_target_siginfo_noswap(tinfo, info);
262 tswap_siginfo(tinfo, tinfo);
265 /* XXX: we support only POSIX RT signals are used. */
266 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
267 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
269 info->si_signo = tswap32(tinfo->si_signo);
270 info->si_errno = tswap32(tinfo->si_errno);
271 info->si_code = tswap32(tinfo->si_code);
272 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
273 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
274 info->si_value.sival_ptr =
275 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
278 void signal_init(void)
280 struct sigaction act;
283 /* generate signal conversion tables */
284 for(i = 1; i <= 64; i++) {
285 if (host_to_target_signal_table[i] == 0)
286 host_to_target_signal_table[i] = i;
288 for(i = 1; i <= 64; i++) {
289 j = host_to_target_signal_table[i];
290 target_to_host_signal_table[j] = i;
293 /* set all host signal handlers. ALL signals are blocked during
294 the handlers to serialize them. */
295 sigfillset(&act.sa_mask);
296 act.sa_flags = SA_SIGINFO;
297 act.sa_sigaction = host_signal_handler;
298 for(i = 1; i < NSIG; i++) {
299 sigaction(i, &act, NULL);
302 memset(sigact_table, 0, sizeof(sigact_table));
304 first_free = &sigqueue_table[0];
305 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
306 sigqueue_table[i].next = &sigqueue_table[i + 1];
307 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
310 /* signal queue handling */
312 static inline struct sigqueue *alloc_sigqueue(void)
314 struct sigqueue *q = first_free;
317 first_free = q->next;
321 static inline void free_sigqueue(struct sigqueue *q)
323 q->next = first_free;
327 /* abort execution with signal */
328 void __attribute((noreturn)) force_sig(int sig)
331 host_sig = target_to_host_signal(sig);
332 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
333 sig, strsignal(host_sig));
338 struct sigaction act;
339 sigemptyset(&act.sa_mask);
340 act.sa_flags = SA_SIGINFO;
341 act.sa_sigaction = SIG_DFL;
342 sigaction(SIGABRT, &act, NULL);
348 /* queue a signal so that it will be send to the virtual CPU as soon
350 int queue_signal(int sig, target_siginfo_t *info)
352 struct emulated_sigaction *k;
353 struct sigqueue *q, **pq;
356 #if defined(DEBUG_SIGNAL)
357 fprintf(stderr, "queue_signal: sig=%d\n",
360 k = &sigact_table[sig - 1];
361 handler = k->sa._sa_handler;
362 if (handler == TARGET_SIG_DFL) {
363 /* default handler : ignore some signal. The other are fatal */
364 if (sig != TARGET_SIGCHLD &&
365 sig != TARGET_SIGURG &&
366 sig != TARGET_SIGWINCH) {
369 return 0; /* indicate ignored */
371 } else if (handler == TARGET_SIG_IGN) {
374 } else if (handler == TARGET_SIG_ERR) {
378 if (sig < TARGET_SIGRTMIN) {
379 /* if non real time signal, we queue exactly one signal */
389 q = alloc_sigqueue();
400 /* signal that a new signal is pending */
402 return 1; /* indicates that the signal was queued */
406 static void host_signal_handler(int host_signum, siginfo_t *info,
410 target_siginfo_t tinfo;
412 /* the CPU emulator uses some host signals to detect exceptions,
413 we we forward to it some signals */
414 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
415 if (cpu_signal_handler(host_signum, info, puc))
419 /* get target signal number */
420 sig = host_to_target_signal(host_signum);
421 if (sig < 1 || sig > TARGET_NSIG)
423 #if defined(DEBUG_SIGNAL)
424 fprintf(stderr, "qemu: got signal %d\n", sig);
426 host_to_target_siginfo_noswap(&tinfo, info);
427 if (queue_signal(sig, &tinfo) == 1) {
428 /* interrupt the virtual CPU as soon as possible */
429 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
433 /* do_sigaltstack() returns target values and errnos. */
434 /* compare linux/kernel/signal.c:do_sigaltstack() */
435 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
438 struct target_sigaltstack oss;
440 /* XXX: test errors */
443 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
444 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
445 __put_user(sas_ss_flags(sp), &oss.ss_flags);
450 struct target_sigaltstack *uss;
451 struct target_sigaltstack ss;
453 ret = -TARGET_EFAULT;
454 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
455 || __get_user(ss.ss_sp, &uss->ss_sp)
456 || __get_user(ss.ss_size, &uss->ss_size)
457 || __get_user(ss.ss_flags, &uss->ss_flags))
459 unlock_user_struct(uss, uss_addr, 0);
462 if (on_sig_stack(sp))
465 ret = -TARGET_EINVAL;
466 if (ss.ss_flags != TARGET_SS_DISABLE
467 && ss.ss_flags != TARGET_SS_ONSTACK
471 if (ss.ss_flags == TARGET_SS_DISABLE) {
475 ret = -TARGET_ENOMEM;
476 if (ss.ss_size < MINSIGSTKSZ)
480 target_sigaltstack_used.ss_sp = ss.ss_sp;
481 target_sigaltstack_used.ss_size = ss.ss_size;
485 ret = -TARGET_EFAULT;
486 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
495 /* do_sigaction() return host values and errnos */
496 int do_sigaction(int sig, const struct target_sigaction *act,
497 struct target_sigaction *oact)
499 struct emulated_sigaction *k;
500 struct sigaction act1;
504 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
506 k = &sigact_table[sig - 1];
507 #if defined(DEBUG_SIGNAL)
508 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
509 sig, (int)act, (int)oact);
512 oact->_sa_handler = tswapl(k->sa._sa_handler);
513 oact->sa_flags = tswapl(k->sa.sa_flags);
514 #if !defined(TARGET_MIPS)
515 oact->sa_restorer = tswapl(k->sa.sa_restorer);
517 oact->sa_mask = k->sa.sa_mask;
520 k->sa._sa_handler = tswapl(act->_sa_handler);
521 k->sa.sa_flags = tswapl(act->sa_flags);
522 #if !defined(TARGET_MIPS)
523 k->sa.sa_restorer = tswapl(act->sa_restorer);
525 k->sa.sa_mask = act->sa_mask;
527 /* we update the host linux signal state */
528 host_sig = target_to_host_signal(sig);
529 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
530 sigfillset(&act1.sa_mask);
531 act1.sa_flags = SA_SIGINFO;
532 if (k->sa.sa_flags & TARGET_SA_RESTART)
533 act1.sa_flags |= SA_RESTART;
534 /* NOTE: it is important to update the host kernel signal
535 ignore state to avoid getting unexpected interrupted
537 if (k->sa._sa_handler == TARGET_SIG_IGN) {
538 act1.sa_sigaction = (void *)SIG_IGN;
539 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
540 act1.sa_sigaction = (void *)SIG_DFL;
542 act1.sa_sigaction = host_signal_handler;
544 ret = sigaction(host_sig, &act1, NULL);
551 #define offsetof(type, field) ((size_t) &((type *)0)->field)
554 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
555 const target_siginfo_t *info)
557 tswap_siginfo(tinfo, info);
561 static inline int current_exec_domain_sig(int sig)
563 return /* current->exec_domain && current->exec_domain->signal_invmap
564 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
567 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
569 /* from the Linux kernel */
571 struct target_fpreg {
572 uint16_t significand[4];
576 struct target_fpxreg {
577 uint16_t significand[4];
582 struct target_xmmreg {
583 abi_ulong element[4];
586 struct target_fpstate {
587 /* Regular FPU environment */
595 struct target_fpreg _st[8];
597 uint16_t magic; /* 0xffff = regular FPU data only */
599 /* FXSR FPU environment */
600 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
603 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
604 struct target_xmmreg _xmm[8];
605 abi_ulong padding[56];
608 #define X86_FXSR_MAGIC 0x0000
610 struct target_sigcontext {
628 abi_ulong esp_at_signal;
630 abi_ulong fpstate; /* pointer */
635 struct target_ucontext {
638 target_stack_t tuc_stack;
639 struct target_sigcontext tuc_mcontext;
640 target_sigset_t tuc_sigmask; /* mask last for extensibility */
647 struct target_sigcontext sc;
648 struct target_fpstate fpstate;
649 abi_ulong extramask[TARGET_NSIG_WORDS-1];
659 struct target_siginfo info;
660 struct target_ucontext uc;
661 struct target_fpstate fpstate;
666 * Set up a signal frame.
669 /* XXX: save x87 state */
671 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
672 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
677 /* already locked in setup_frame() */
678 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
679 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
680 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
681 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
682 err |= __put_user(env->regs[R_EDI], &sc->edi);
683 err |= __put_user(env->regs[R_ESI], &sc->esi);
684 err |= __put_user(env->regs[R_EBP], &sc->ebp);
685 err |= __put_user(env->regs[R_ESP], &sc->esp);
686 err |= __put_user(env->regs[R_EBX], &sc->ebx);
687 err |= __put_user(env->regs[R_EDX], &sc->edx);
688 err |= __put_user(env->regs[R_ECX], &sc->ecx);
689 err |= __put_user(env->regs[R_EAX], &sc->eax);
690 err |= __put_user(env->exception_index, &sc->trapno);
691 err |= __put_user(env->error_code, &sc->err);
692 err |= __put_user(env->eip, &sc->eip);
693 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
694 err |= __put_user(env->eflags, &sc->eflags);
695 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
696 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
698 cpu_x86_fsave(env, fpstate_addr, 1);
699 fpstate->status = fpstate->sw;
701 err |= __put_user(magic, &fpstate->magic);
702 err |= __put_user(fpstate_addr, &sc->fpstate);
704 /* non-iBCS2 extensions.. */
705 err |= __put_user(mask, &sc->oldmask);
706 err |= __put_user(env->cr[2], &sc->cr2);
711 * Determine which stack to use..
714 static inline abi_ulong
715 get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
719 /* Default to using normal stack */
720 esp = env->regs[R_ESP];
721 /* This is the X/Open sanctioned signal stack switching. */
722 if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
723 if (sas_ss_flags(esp) == 0)
724 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
727 /* This is the legacy signal stack switching. */
729 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
730 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
731 ka->sa.sa_restorer) {
732 esp = (unsigned long) ka->sa.sa_restorer;
734 return (esp - frame_size) & -8ul;
737 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
738 static void setup_frame(int sig, struct emulated_sigaction *ka,
739 target_sigset_t *set, CPUX86State *env)
741 abi_ulong frame_addr;
742 struct sigframe *frame;
745 frame_addr = get_sigframe(ka, env, sizeof(*frame));
747 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
750 err |= __put_user(current_exec_domain_sig(sig),
755 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
756 frame_addr + offsetof(struct sigframe, fpstate));
760 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
761 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
765 /* Set up to return from userspace. If provided, use a stub
766 already in userspace. */
767 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
768 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
771 abi_ulong retcode_addr;
772 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
773 err |= __put_user(retcode_addr, &frame->pretcode);
774 /* This is popl %eax ; movl $,%eax ; int $0x80 */
776 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
777 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
779 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
785 /* Set up registers for signal handler */
786 env->regs[R_ESP] = frame_addr;
787 env->eip = ka->sa._sa_handler;
789 cpu_x86_load_seg(env, R_DS, __USER_DS);
790 cpu_x86_load_seg(env, R_ES, __USER_DS);
791 cpu_x86_load_seg(env, R_SS, __USER_DS);
792 cpu_x86_load_seg(env, R_CS, __USER_CS);
793 env->eflags &= ~TF_MASK;
795 unlock_user_struct(frame, frame_addr, 1);
800 unlock_user_struct(frame, frame_addr, 1);
801 if (sig == TARGET_SIGSEGV)
802 ka->sa._sa_handler = TARGET_SIG_DFL;
803 force_sig(TARGET_SIGSEGV /* , current */);
806 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
807 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
808 target_siginfo_t *info,
809 target_sigset_t *set, CPUX86State *env)
811 abi_ulong frame_addr, addr;
812 struct rt_sigframe *frame;
815 frame_addr = get_sigframe(ka, env, sizeof(*frame));
817 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
820 err |= __put_user(current_exec_domain_sig(sig),
822 addr = frame_addr + offsetof(struct rt_sigframe, info);
823 err |= __put_user(addr, &frame->pinfo);
824 addr = frame_addr + offsetof(struct rt_sigframe, uc);
825 err |= __put_user(addr, &frame->puc);
826 err |= copy_siginfo_to_user(&frame->info, info);
830 /* Create the ucontext. */
831 err |= __put_user(0, &frame->uc.tuc_flags);
832 err |= __put_user(0, &frame->uc.tuc_link);
833 err |= __put_user(target_sigaltstack_used.ss_sp,
834 &frame->uc.tuc_stack.ss_sp);
835 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
836 &frame->uc.tuc_stack.ss_flags);
837 err |= __put_user(target_sigaltstack_used.ss_size,
838 &frame->uc.tuc_stack.ss_size);
839 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
841 frame_addr + offsetof(struct rt_sigframe, fpstate));
842 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
843 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
847 /* Set up to return from userspace. If provided, use a stub
848 already in userspace. */
849 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
850 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
853 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
854 err |= __put_user(addr, &frame->pretcode);
855 /* This is movl $,%eax ; int $0x80 */
856 err |= __put_user(0xb8, (char *)(frame->retcode+0));
857 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
859 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
865 /* Set up registers for signal handler */
866 env->regs[R_ESP] = frame_addr;
867 env->eip = ka->sa._sa_handler;
869 cpu_x86_load_seg(env, R_DS, __USER_DS);
870 cpu_x86_load_seg(env, R_ES, __USER_DS);
871 cpu_x86_load_seg(env, R_SS, __USER_DS);
872 cpu_x86_load_seg(env, R_CS, __USER_CS);
873 env->eflags &= ~TF_MASK;
875 unlock_user_struct(frame, frame_addr, 1);
880 unlock_user_struct(frame, frame_addr, 1);
881 if (sig == TARGET_SIGSEGV)
882 ka->sa._sa_handler = TARGET_SIG_DFL;
883 force_sig(TARGET_SIGSEGV /* , current */);
887 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
889 unsigned int err = 0;
890 abi_ulong fpstate_addr;
891 unsigned int tmpflags;
893 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
894 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
895 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
896 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
898 env->regs[R_EDI] = tswapl(sc->edi);
899 env->regs[R_ESI] = tswapl(sc->esi);
900 env->regs[R_EBP] = tswapl(sc->ebp);
901 env->regs[R_ESP] = tswapl(sc->esp);
902 env->regs[R_EBX] = tswapl(sc->ebx);
903 env->regs[R_EDX] = tswapl(sc->edx);
904 env->regs[R_ECX] = tswapl(sc->ecx);
905 env->eip = tswapl(sc->eip);
907 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
908 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
910 tmpflags = tswapl(sc->eflags);
911 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
912 // regs->orig_eax = -1; /* disable syscall checks */
914 fpstate_addr = tswapl(sc->fpstate);
915 if (fpstate_addr != 0) {
916 if (!access_ok(VERIFY_READ, fpstate_addr,
917 sizeof(struct target_fpstate)))
919 cpu_x86_frstor(env, fpstate_addr, 1);
922 *peax = tswapl(sc->eax);
928 long do_sigreturn(CPUX86State *env)
930 struct sigframe *frame;
931 abi_ulong frame_addr = env->regs[R_ESP] - 8;
932 target_sigset_t target_set;
936 #if defined(DEBUG_SIGNAL)
937 fprintf(stderr, "do_sigreturn\n");
939 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
941 /* set blocked signals */
942 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
944 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
945 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
949 target_to_host_sigset_internal(&set, &target_set);
950 sigprocmask(SIG_SETMASK, &set, NULL);
952 /* restore registers */
953 if (restore_sigcontext(env, &frame->sc, &eax))
955 unlock_user_struct(frame, frame_addr, 0);
959 unlock_user_struct(frame, frame_addr, 0);
960 force_sig(TARGET_SIGSEGV);
964 long do_rt_sigreturn(CPUX86State *env)
966 abi_ulong frame_addr;
967 struct rt_sigframe *frame;
971 frame_addr = env->regs[R_ESP] - 4;
972 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
974 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
975 sigprocmask(SIG_SETMASK, &set, NULL);
977 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
980 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
981 get_sp_from_cpustate(env)) == -EFAULT)
984 unlock_user_struct(frame, frame_addr, 0);
988 unlock_user_struct(frame, frame_addr, 0);
989 force_sig(TARGET_SIGSEGV);
993 #elif defined(TARGET_ARM)
995 struct target_sigcontext {
997 abi_ulong error_code;
1016 abi_ulong fault_address;
1019 struct target_ucontext_v1 {
1020 abi_ulong tuc_flags;
1022 target_stack_t tuc_stack;
1023 struct target_sigcontext tuc_mcontext;
1024 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1027 struct target_ucontext_v2 {
1028 abi_ulong tuc_flags;
1030 target_stack_t tuc_stack;
1031 struct target_sigcontext tuc_mcontext;
1032 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1033 char __unused[128 - sizeof(sigset_t)];
1034 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1039 struct target_sigcontext sc;
1040 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1046 struct target_ucontext_v2 uc;
1050 struct rt_sigframe_v1
1054 struct target_siginfo info;
1055 struct target_ucontext_v1 uc;
1059 struct rt_sigframe_v2
1061 struct target_siginfo info;
1062 struct target_ucontext_v2 uc;
1066 #define TARGET_CONFIG_CPU_32 1
1069 * For ARM syscalls, we encode the syscall number into the instruction.
1071 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1072 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1075 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1076 * need two 16-bit instructions.
1078 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1079 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1081 static const abi_ulong retcodes[4] = {
1082 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1083 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1087 #define __get_user_error(x,p,e) __get_user(x, p)
1089 static inline int valid_user_regs(CPUState *regs)
1095 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1096 CPUState *env, abi_ulong mask)
1098 __put_user(env->regs[0], &sc->arm_r0);
1099 __put_user(env->regs[1], &sc->arm_r1);
1100 __put_user(env->regs[2], &sc->arm_r2);
1101 __put_user(env->regs[3], &sc->arm_r3);
1102 __put_user(env->regs[4], &sc->arm_r4);
1103 __put_user(env->regs[5], &sc->arm_r5);
1104 __put_user(env->regs[6], &sc->arm_r6);
1105 __put_user(env->regs[7], &sc->arm_r7);
1106 __put_user(env->regs[8], &sc->arm_r8);
1107 __put_user(env->regs[9], &sc->arm_r9);
1108 __put_user(env->regs[10], &sc->arm_r10);
1109 __put_user(env->regs[11], &sc->arm_fp);
1110 __put_user(env->regs[12], &sc->arm_ip);
1111 __put_user(env->regs[13], &sc->arm_sp);
1112 __put_user(env->regs[14], &sc->arm_lr);
1113 __put_user(env->regs[15], &sc->arm_pc);
1114 #ifdef TARGET_CONFIG_CPU_32
1115 __put_user(cpsr_read(env), &sc->arm_cpsr);
1118 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1119 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1120 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1121 __put_user(mask, &sc->oldmask);
1124 static inline abi_ulong
1125 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1127 unsigned long sp = regs->regs[13];
1130 * This is the X/Open sanctioned signal stack switching.
1132 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1133 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1135 * ATPCS B01 mandates 8-byte alignment
1137 return (sp - framesize) & ~7;
1141 setup_return(CPUState *env, struct emulated_sigaction *ka,
1142 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1144 abi_ulong handler = ka->sa._sa_handler;
1146 int thumb = handler & 1;
1148 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1149 retcode = ka->sa.sa_restorer;
1151 unsigned int idx = thumb;
1153 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1156 if (__put_user(retcodes[idx], rc))
1159 flush_icache_range((abi_ulong)rc,
1160 (abi_ulong)(rc + 1));
1162 retcode = rc_addr + thumb;
1165 env->regs[0] = usig;
1166 env->regs[13] = frame_addr;
1167 env->regs[14] = retcode;
1168 env->regs[15] = handler & (thumb ? ~1 : ~3);
1172 #ifdef TARGET_CONFIG_CPU_32
1180 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1181 target_sigset_t *set, CPUState *env)
1183 struct target_sigaltstack stack;
1186 /* Clear all the bits of the ucontext we don't use. */
1187 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1189 memset(&stack, 0, sizeof(stack));
1190 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1191 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1192 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1193 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1195 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1196 /* FIXME: Save coprocessor signal frame. */
1197 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1198 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1202 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1203 static void setup_frame_v1(int usig, struct emulated_sigaction *ka,
1204 target_sigset_t *set, CPUState *regs)
1206 struct sigframe_v1 *frame;
1207 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1210 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1213 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1215 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1216 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1220 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1221 frame_addr + offsetof(struct sigframe_v1, retcode));
1224 unlock_user_struct(frame, frame_addr, 1);
1227 static void setup_frame_v2(int usig, struct emulated_sigaction *ka,
1228 target_sigset_t *set, CPUState *regs)
1230 struct sigframe_v2 *frame;
1231 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1233 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1236 setup_sigframe_v2(&frame->uc, set, regs);
1238 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1239 frame_addr + offsetof(struct sigframe_v2, retcode));
1241 unlock_user_struct(frame, frame_addr, 1);
1244 static void setup_frame(int usig, struct emulated_sigaction *ka,
1245 target_sigset_t *set, CPUState *regs)
1247 if (get_osversion() >= 0x020612) {
1248 setup_frame_v2(usig, ka, set, regs);
1250 setup_frame_v1(usig, ka, set, regs);
1254 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1255 static void setup_rt_frame_v1(int usig, struct emulated_sigaction *ka,
1256 target_siginfo_t *info,
1257 target_sigset_t *set, CPUState *env)
1259 struct rt_sigframe_v1 *frame;
1260 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1261 struct target_sigaltstack stack;
1263 abi_ulong info_addr, uc_addr;
1265 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1268 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1269 __put_user(info_addr, &frame->pinfo);
1270 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1271 __put_user(uc_addr, &frame->puc);
1272 copy_siginfo_to_user(&frame->info, info);
1274 /* Clear all the bits of the ucontext we don't use. */
1275 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1277 memset(&stack, 0, sizeof(stack));
1278 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1279 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1280 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1281 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1283 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1284 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1285 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1289 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1290 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1292 env->regs[1] = info_addr;
1293 env->regs[2] = uc_addr;
1296 unlock_user_struct(frame, frame_addr, 1);
1299 static void setup_rt_frame_v2(int usig, struct emulated_sigaction *ka,
1300 target_siginfo_t *info,
1301 target_sigset_t *set, CPUState *env)
1303 struct rt_sigframe_v2 *frame;
1304 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1305 abi_ulong info_addr, uc_addr;
1307 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1310 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1311 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1312 copy_siginfo_to_user(&frame->info, info);
1314 setup_sigframe_v2(&frame->uc, set, env);
1316 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1317 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1319 env->regs[1] = info_addr;
1320 env->regs[2] = uc_addr;
1322 unlock_user_struct(frame, frame_addr, 1);
1325 static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1326 target_siginfo_t *info,
1327 target_sigset_t *set, CPUState *env)
1329 if (get_osversion() >= 0x020612) {
1330 setup_rt_frame_v2(usig, ka, info, set, env);
1332 setup_rt_frame_v1(usig, ka, info, set, env);
1337 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1342 __get_user_error(env->regs[0], &sc->arm_r0, err);
1343 __get_user_error(env->regs[1], &sc->arm_r1, err);
1344 __get_user_error(env->regs[2], &sc->arm_r2, err);
1345 __get_user_error(env->regs[3], &sc->arm_r3, err);
1346 __get_user_error(env->regs[4], &sc->arm_r4, err);
1347 __get_user_error(env->regs[5], &sc->arm_r5, err);
1348 __get_user_error(env->regs[6], &sc->arm_r6, err);
1349 __get_user_error(env->regs[7], &sc->arm_r7, err);
1350 __get_user_error(env->regs[8], &sc->arm_r8, err);
1351 __get_user_error(env->regs[9], &sc->arm_r9, err);
1352 __get_user_error(env->regs[10], &sc->arm_r10, err);
1353 __get_user_error(env->regs[11], &sc->arm_fp, err);
1354 __get_user_error(env->regs[12], &sc->arm_ip, err);
1355 __get_user_error(env->regs[13], &sc->arm_sp, err);
1356 __get_user_error(env->regs[14], &sc->arm_lr, err);
1357 __get_user_error(env->regs[15], &sc->arm_pc, err);
1358 #ifdef TARGET_CONFIG_CPU_32
1359 __get_user_error(cpsr, &sc->arm_cpsr, err);
1360 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1363 err |= !valid_user_regs(env);
1368 long do_sigreturn_v1(CPUState *env)
1370 abi_ulong frame_addr;
1371 struct sigframe_v1 *frame;
1372 target_sigset_t set;
1377 * Since we stacked the signal on a 64-bit boundary,
1378 * then 'sp' should be word aligned here. If it's
1379 * not, then the user is trying to mess with us.
1381 if (env->regs[13] & 7)
1384 frame_addr = env->regs[13];
1385 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1388 if (__get_user(set.sig[0], &frame->sc.oldmask))
1390 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1391 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1395 target_to_host_sigset_internal(&host_set, &set);
1396 sigprocmask(SIG_SETMASK, &host_set, NULL);
1398 if (restore_sigcontext(env, &frame->sc))
1402 /* Send SIGTRAP if we're single-stepping */
1403 if (ptrace_cancel_bpt(current))
1404 send_sig(SIGTRAP, current, 1);
1406 unlock_user_struct(frame, frame_addr, 0);
1407 return env->regs[0];
1410 unlock_user_struct(frame, frame_addr, 0);
1411 force_sig(SIGSEGV /* , current */);
1415 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1416 struct target_ucontext_v2 *uc)
1420 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1421 sigprocmask(SIG_SETMASK, &host_set, NULL);
1423 if (restore_sigcontext(env, &uc->tuc_mcontext))
1426 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1430 /* Send SIGTRAP if we're single-stepping */
1431 if (ptrace_cancel_bpt(current))
1432 send_sig(SIGTRAP, current, 1);
1438 long do_sigreturn_v2(CPUState *env)
1440 abi_ulong frame_addr;
1441 struct sigframe_v2 *frame;
1444 * Since we stacked the signal on a 64-bit boundary,
1445 * then 'sp' should be word aligned here. If it's
1446 * not, then the user is trying to mess with us.
1448 if (env->regs[13] & 7)
1451 frame_addr = env->regs[13];
1452 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1455 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1458 unlock_user_struct(frame, frame_addr, 0);
1459 return env->regs[0];
1462 unlock_user_struct(frame, frame_addr, 0);
1463 force_sig(SIGSEGV /* , current */);
1467 long do_sigreturn(CPUState *env)
1469 if (get_osversion() >= 0x020612) {
1470 return do_sigreturn_v2(env);
1472 return do_sigreturn_v1(env);
1476 long do_rt_sigreturn_v1(CPUState *env)
1478 abi_ulong frame_addr;
1479 struct rt_sigframe_v1 *frame;
1483 * Since we stacked the signal on a 64-bit boundary,
1484 * then 'sp' should be word aligned here. If it's
1485 * not, then the user is trying to mess with us.
1487 if (env->regs[13] & 7)
1490 frame_addr = env->regs[13];
1491 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1494 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1495 sigprocmask(SIG_SETMASK, &host_set, NULL);
1497 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1500 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1504 /* Send SIGTRAP if we're single-stepping */
1505 if (ptrace_cancel_bpt(current))
1506 send_sig(SIGTRAP, current, 1);
1508 unlock_user_struct(frame, frame_addr, 0);
1509 return env->regs[0];
1512 unlock_user_struct(frame, frame_addr, 0);
1513 force_sig(SIGSEGV /* , current */);
1517 long do_rt_sigreturn_v2(CPUState *env)
1519 abi_ulong frame_addr;
1520 struct rt_sigframe_v2 *frame;
1523 * Since we stacked the signal on a 64-bit boundary,
1524 * then 'sp' should be word aligned here. If it's
1525 * not, then the user is trying to mess with us.
1527 if (env->regs[13] & 7)
1530 frame_addr = env->regs[13];
1531 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1534 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1537 unlock_user_struct(frame, frame_addr, 0);
1538 return env->regs[0];
1541 unlock_user_struct(frame, frame_addr, 0);
1542 force_sig(SIGSEGV /* , current */);
1546 long do_rt_sigreturn(CPUState *env)
1548 if (get_osversion() >= 0x020612) {
1549 return do_rt_sigreturn_v2(env);
1551 return do_rt_sigreturn_v1(env);
1555 #elif defined(TARGET_SPARC)
1557 #define __SUNOS_MAXWIN 31
1559 /* This is what SunOS does, so shall I. */
1560 struct target_sigcontext {
1561 abi_ulong sigc_onstack; /* state to restore */
1563 abi_ulong sigc_mask; /* sigmask to restore */
1564 abi_ulong sigc_sp; /* stack pointer */
1565 abi_ulong sigc_pc; /* program counter */
1566 abi_ulong sigc_npc; /* next program counter */
1567 abi_ulong sigc_psr; /* for condition codes etc */
1568 abi_ulong sigc_g1; /* User uses these two registers */
1569 abi_ulong sigc_o0; /* within the trampoline code. */
1571 /* Now comes information regarding the users window set
1572 * at the time of the signal.
1574 abi_ulong sigc_oswins; /* outstanding windows */
1576 /* stack ptrs for each regwin buf */
1577 char *sigc_spbuf[__SUNOS_MAXWIN];
1579 /* Windows to restore after signal */
1581 abi_ulong locals[8];
1583 } sigc_wbuf[__SUNOS_MAXWIN];
1585 /* A Sparc stack frame */
1586 struct sparc_stackf {
1587 abi_ulong locals[8];
1589 struct sparc_stackf *fp;
1590 abi_ulong callers_pc;
1593 abi_ulong xxargs[1];
1602 abi_ulong u_regs[16]; /* globals and ins */
1608 unsigned long si_float_regs [32];
1609 unsigned long si_fsr;
1610 unsigned long si_fpqdepth;
1612 unsigned long *insn_addr;
1615 } qemu_siginfo_fpu_t;
1618 struct target_signal_frame {
1619 struct sparc_stackf ss;
1622 abi_ulong insns[2] __attribute__ ((aligned (8)));
1623 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1624 abi_ulong extra_size; /* Should be 0 */
1625 qemu_siginfo_fpu_t fpu_state;
1627 struct target_rt_signal_frame {
1628 struct sparc_stackf ss;
1633 unsigned int insns[2];
1635 unsigned int extra_size; /* Should be 0 */
1636 qemu_siginfo_fpu_t fpu_state;
1650 #define UREG_FP UREG_I6
1651 #define UREG_SP UREG_O6
1653 static inline abi_ulong get_sigframe(struct emulated_sigaction *sa,
1654 CPUState *env, unsigned long framesize)
1658 sp = env->regwptr[UREG_FP];
1660 /* This is the X/Open sanctioned signal stack switching. */
1661 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1662 if (!on_sig_stack(sp)
1663 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1664 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1666 return sp - framesize;
1670 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1674 err |= __put_user(env->psr, &si->si_regs.psr);
1675 err |= __put_user(env->pc, &si->si_regs.pc);
1676 err |= __put_user(env->npc, &si->si_regs.npc);
1677 err |= __put_user(env->y, &si->si_regs.y);
1678 for (i=0; i < 8; i++) {
1679 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1681 for (i=0; i < 8; i++) {
1682 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1684 err |= __put_user(mask, &si->si_mask);
1690 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1691 CPUState *env, unsigned long mask)
1695 err |= __put_user(mask, &sc->sigc_mask);
1696 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1697 err |= __put_user(env->pc, &sc->sigc_pc);
1698 err |= __put_user(env->npc, &sc->sigc_npc);
1699 err |= __put_user(env->psr, &sc->sigc_psr);
1700 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1701 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1706 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1708 static void setup_frame(int sig, struct emulated_sigaction *ka,
1709 target_sigset_t *set, CPUState *env)
1712 struct target_signal_frame *sf;
1713 int sigframe_size, err, i;
1715 /* 1. Make sure everything is clean */
1716 //synchronize_user_stack();
1718 sigframe_size = NF_ALIGNEDSZ;
1719 sf_addr = get_sigframe(ka, env, sigframe_size);
1721 sf = lock_user(VERIFY_WRITE, sf_addr,
1722 sizeof(struct target_signal_frame), 0);
1726 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1728 if (invalid_frame_pointer(sf, sigframe_size))
1729 goto sigill_and_return;
1731 /* 2. Save the current process state */
1732 err = setup___siginfo(&sf->info, env, set->sig[0]);
1733 err |= __put_user(0, &sf->extra_size);
1735 //err |= save_fpu_state(regs, &sf->fpu_state);
1736 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1738 err |= __put_user(set->sig[0], &sf->info.si_mask);
1739 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1740 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1743 for (i = 0; i < 8; i++) {
1744 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1746 for (i = 0; i < 8; i++) {
1747 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1752 /* 3. signal handler back-trampoline and parameters */
1753 env->regwptr[UREG_FP] = sf_addr;
1754 env->regwptr[UREG_I0] = sig;
1755 env->regwptr[UREG_I1] = sf_addr +
1756 offsetof(struct target_signal_frame, info);
1757 env->regwptr[UREG_I2] = sf_addr +
1758 offsetof(struct target_signal_frame, info);
1760 /* 4. signal handler */
1761 env->pc = ka->sa._sa_handler;
1762 env->npc = (env->pc + 4);
1763 /* 5. return to kernel instructions */
1764 if (ka->sa.sa_restorer)
1765 env->regwptr[UREG_I7] = ka->sa.sa_restorer;
1769 env->regwptr[UREG_I7] = sf_addr +
1770 offsetof(struct target_signal_frame, insns) - 2 * 4;
1772 /* mov __NR_sigreturn, %g1 */
1774 err |= __put_user(val32, &sf->insns[0]);
1778 err |= __put_user(val32, &sf->insns[1]);
1782 /* Flush instruction space. */
1783 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1786 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1790 force_sig(TARGET_SIGILL);
1793 //fprintf(stderr, "force_sig\n");
1794 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1795 force_sig(TARGET_SIGSEGV);
1798 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
1803 if (current->flags & PF_USEDFPU)
1804 regs->psr &= ~PSR_EF;
1806 if (current == last_task_used_math) {
1807 last_task_used_math = 0;
1808 regs->psr &= ~PSR_EF;
1811 current->used_math = 1;
1812 current->flags &= ~PF_USEDFPU;
1815 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1820 /* XXX: incorrect */
1821 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1822 (sizeof(unsigned long) * 32));
1824 err |= __get_user(env->fsr, &fpu->si_fsr);
1826 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1827 if (current->thread.fpqdepth != 0)
1828 err |= __copy_from_user(¤t->thread.fpqueue[0],
1829 &fpu->si_fpqueue[0],
1830 ((sizeof(unsigned long) +
1831 (sizeof(unsigned long *)))*16));
1837 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1838 target_siginfo_t *info,
1839 target_sigset_t *set, CPUState *env)
1841 fprintf(stderr, "setup_rt_frame: not implemented\n");
1844 long do_sigreturn(CPUState *env)
1847 struct target_signal_frame *sf;
1848 uint32_t up_psr, pc, npc;
1849 target_sigset_t set;
1851 abi_ulong fpu_save_addr;
1854 sf_addr = env->regwptr[UREG_FP];
1855 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1858 fprintf(stderr, "sigreturn\n");
1859 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1861 //cpu_dump_state(env, stderr, fprintf, 0);
1863 /* 1. Make sure we are not getting garbage from the user */
1868 err = __get_user(pc, &sf->info.si_regs.pc);
1869 err |= __get_user(npc, &sf->info.si_regs.npc);
1874 /* 2. Restore the state */
1875 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1877 /* User can only change condition codes and FPU enabling in %psr. */
1878 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1879 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1883 err |= __get_user(env->y, &sf->info.si_regs.y);
1884 for (i=0; i < 8; i++) {
1885 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1887 for (i=0; i < 8; i++) {
1888 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1891 err |= __get_user(fpu_save_addr, &sf->fpu_save);
1894 // err |= restore_fpu_state(env, fpu_save);
1896 /* This is pretty much atomic, no amount locking would prevent
1897 * the races which exist anyways.
1899 err |= __get_user(set.sig[0], &sf->info.si_mask);
1900 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1901 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1904 target_to_host_sigset_internal(&host_set, &set);
1905 sigprocmask(SIG_SETMASK, &host_set, NULL);
1909 unlock_user_struct(sf, sf_addr, 0);
1910 return env->regwptr[0];
1913 unlock_user_struct(sf, sf_addr, 0);
1914 force_sig(TARGET_SIGSEGV);
1917 long do_rt_sigreturn(CPUState *env)
1919 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1920 return -TARGET_ENOSYS;
1923 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1945 typedef abi_ulong target_mc_greg_t;
1946 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1948 struct target_mc_fq {
1949 abi_ulong *mcfq_addr;
1953 struct target_mc_fpu {
1957 //uint128_t qregs[16];
1959 abi_ulong mcfpu_fsr;
1960 abi_ulong mcfpu_fprs;
1961 abi_ulong mcfpu_gsr;
1962 struct target_mc_fq *mcfpu_fq;
1963 unsigned char mcfpu_qcnt;
1964 unsigned char mcfpu_qentsz;
1965 unsigned char mcfpu_enab;
1967 typedef struct target_mc_fpu target_mc_fpu_t;
1970 target_mc_gregset_t mc_gregs;
1971 target_mc_greg_t mc_fp;
1972 target_mc_greg_t mc_i7;
1973 target_mc_fpu_t mc_fpregs;
1974 } target_mcontext_t;
1976 struct target_ucontext {
1977 struct target_ucontext *uc_link;
1979 target_sigset_t uc_sigmask;
1980 target_mcontext_t uc_mcontext;
1983 /* A V9 register window */
1984 struct target_reg_window {
1985 abi_ulong locals[8];
1989 #define TARGET_STACK_BIAS 2047
1991 /* {set, get}context() needed for 64-bit SparcLinux userland. */
1992 void sparc64_set_context(CPUSPARCState *env)
1995 struct target_ucontext *ucp;
1996 target_mc_gregset_t *grp;
1997 abi_ulong pc, npc, tstate;
1998 abi_ulong fp, i7, w_addr;
1999 unsigned char fenab;
2003 ucp_addr = env->regwptr[UREG_I0];
2004 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2006 grp = &ucp->uc_mcontext.mc_gregs;
2007 err = __get_user(pc, &((*grp)[MC_PC]));
2008 err |= __get_user(npc, &((*grp)[MC_NPC]));
2009 if (err || ((pc | npc) & 3))
2011 if (env->regwptr[UREG_I1]) {
2012 target_sigset_t target_set;
2015 if (TARGET_NSIG_WORDS == 1) {
2016 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
2019 abi_ulong *src, *dst;
2020 src = ucp->uc_sigmask.sig;
2021 dst = target_set.sig;
2022 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2024 err |= __get_user(*dst, src);
2028 target_to_host_sigset_internal(&set, &target_set);
2029 sigprocmask(SIG_SETMASK, &set, NULL);
2033 err |= __get_user(env->y, &((*grp)[MC_Y]));
2034 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2035 env->asi = (tstate >> 24) & 0xff;
2036 PUT_CCR(env, tstate >> 32);
2037 PUT_CWP64(env, tstate & 0x1f);
2038 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2039 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2040 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2041 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2042 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2043 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2044 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2045 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2046 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2047 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2048 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2049 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2050 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2051 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2052 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2054 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2055 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
2057 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2058 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2061 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2064 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2065 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
2067 uint32_t *src, *dst;
2068 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2070 /* XXX: check that the CPU storage is the same as user context */
2071 for (i = 0; i < 64; i++, dst++, src++)
2072 err |= __get_user(*dst, src);
2074 err |= __get_user(env->fsr,
2075 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2076 err |= __get_user(env->gsr,
2077 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
2080 unlock_user_struct(ucp, ucp_addr, 0);
2083 unlock_user_struct(ucp, ucp_addr, 0);
2087 void sparc64_get_context(CPUSPARCState *env)
2090 struct target_ucontext *ucp;
2091 target_mc_gregset_t *grp;
2092 target_mcontext_t *mcp;
2093 abi_ulong fp, i7, w_addr;
2096 target_sigset_t target_set;
2099 ucp_addr = env->regwptr[UREG_I0];
2100 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2103 mcp = &ucp->uc_mcontext;
2104 grp = &mcp->mc_gregs;
2106 /* Skip over the trap instruction, first. */
2112 sigprocmask(0, NULL, &set);
2113 host_to_target_sigset_internal(&target_set, &set);
2114 if (TARGET_NSIG_WORDS == 1) {
2115 err |= __put_user(target_set.sig[0],
2116 (abi_ulong *)&ucp->uc_sigmask);
2118 abi_ulong *src, *dst;
2119 src = target_set.sig;
2120 dst = ucp->uc_sigmask.sig;
2121 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2123 err |= __put_user(*src, dst);
2128 /* XXX: tstate must be saved properly */
2129 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2130 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2131 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2132 err |= __put_user(env->y, &((*grp)[MC_Y]));
2133 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2134 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2135 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2136 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2137 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2138 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2139 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2140 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2141 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2142 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2143 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2144 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2145 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2146 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2147 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2149 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2151 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2154 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2157 err |= __put_user(fp, &(mcp->mc_fp));
2158 err |= __put_user(i7, &(mcp->mc_i7));
2161 uint32_t *src, *dst;
2163 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2164 /* XXX: check that the CPU storage is the same as user context */
2165 for (i = 0; i < 64; i++, dst++, src++)
2166 err |= __put_user(*src, dst);
2168 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2169 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2170 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2174 unlock_user_struct(ucp, ucp_addr, 1);
2177 unlock_user_struct(ucp, ucp_addr, 1);
2181 #elif defined(TARGET_ABI_MIPSN64)
2183 # warning signal handling not implemented
2185 static void setup_frame(int sig, struct emulated_sigaction *ka,
2186 target_sigset_t *set, CPUState *env)
2188 fprintf(stderr, "setup_frame: not implemented\n");
2191 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2192 target_siginfo_t *info,
2193 target_sigset_t *set, CPUState *env)
2195 fprintf(stderr, "setup_rt_frame: not implemented\n");
2198 long do_sigreturn(CPUState *env)
2200 fprintf(stderr, "do_sigreturn: not implemented\n");
2201 return -TARGET_ENOSYS;
2204 long do_rt_sigreturn(CPUState *env)
2206 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2207 return -TARGET_ENOSYS;
2210 #elif defined(TARGET_ABI_MIPSN32)
2212 # warning signal handling not implemented
2214 static void setup_frame(int sig, struct emulated_sigaction *ka,
2215 target_sigset_t *set, CPUState *env)
2217 fprintf(stderr, "setup_frame: not implemented\n");
2220 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2221 target_siginfo_t *info,
2222 target_sigset_t *set, CPUState *env)
2224 fprintf(stderr, "setup_rt_frame: not implemented\n");
2227 long do_sigreturn(CPUState *env)
2229 fprintf(stderr, "do_sigreturn: not implemented\n");
2230 return -TARGET_ENOSYS;
2233 long do_rt_sigreturn(CPUState *env)
2235 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2236 return -TARGET_ENOSYS;
2239 #elif defined(TARGET_ABI_MIPSO32)
2241 struct target_sigcontext {
2242 uint32_t sc_regmask; /* Unused */
2245 uint64_t sc_regs[32];
2246 uint64_t sc_fpregs[32];
2247 uint32_t sc_ownedfp; /* Unused */
2248 uint32_t sc_fpc_csr;
2249 uint32_t sc_fpc_eir; /* Unused */
2250 uint32_t sc_used_math;
2251 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2254 target_ulong sc_hi1; /* Was sc_cause */
2255 target_ulong sc_lo1; /* Was sc_badvaddr */
2256 target_ulong sc_hi2; /* Was sc_sigset[4] */
2257 target_ulong sc_lo2;
2258 target_ulong sc_hi3;
2259 target_ulong sc_lo3;
2263 uint32_t sf_ass[4]; /* argument save space for o32 */
2264 uint32_t sf_code[2]; /* signal trampoline */
2265 struct target_sigcontext sf_sc;
2266 target_sigset_t sf_mask;
2269 /* Install trampoline to jump back from signal handler */
2270 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2275 * Set up the return code ...
2277 * li v0, __NR__foo_sigreturn
2281 err = __put_user(0x24020000 + syscall, tramp + 0);
2282 err |= __put_user(0x0000000c , tramp + 1);
2283 /* flush_cache_sigtramp((unsigned long) tramp); */
2288 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2292 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
2294 #define save_gp_reg(i) do { \
2295 err |= __put_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
2297 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2298 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2299 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2300 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2301 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2302 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2303 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2304 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2308 err |= __put_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2309 err |= __put_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
2311 /* Not used yet, but might be useful if we ever have DSP suppport */
2314 err |= __put_user(mfhi1(), &sc->sc_hi1);
2315 err |= __put_user(mflo1(), &sc->sc_lo1);
2316 err |= __put_user(mfhi2(), &sc->sc_hi2);
2317 err |= __put_user(mflo2(), &sc->sc_lo2);
2318 err |= __put_user(mfhi3(), &sc->sc_hi3);
2319 err |= __put_user(mflo3(), &sc->sc_lo3);
2320 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2322 /* same with 64 bit */
2324 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2325 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2327 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2328 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2329 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2330 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2331 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2332 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2333 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2339 err |= __put_user(!!used_math(), &sc->sc_used_math);
2345 * Save FPU state to signal context. Signal handler will "inherit"
2346 * current FPU state.
2350 if (!is_fpu_owner()) {
2352 restore_fp(current);
2354 err |= save_fp_context(sc);
2363 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2367 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2369 err |= __get_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2370 err |= __get_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
2372 #define restore_gp_reg(i) do { \
2373 err |= __get_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
2375 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2376 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2377 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2378 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2379 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2380 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2381 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2382 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2383 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2384 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2386 #undef restore_gp_reg
2390 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2391 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2392 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2393 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2394 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2395 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2396 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2399 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2400 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2402 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2403 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2404 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2405 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2406 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2407 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2408 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2412 err |= __get_user(used_math, &sc->sc_used_math);
2413 conditional_used_math(used_math);
2418 /* restore fpu context if we have used it before */
2420 err |= restore_fp_context(sc);
2422 /* signal handler may have used FPU. Give it up. */
2431 * Determine which stack to use..
2433 static inline abi_ulong
2434 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2438 /* Default to using normal stack */
2439 sp = regs->gpr[regs->current_tc][29];
2442 * FPU emulator may have it's own trampoline active just
2443 * above the user stack, 16-bytes before the next lowest
2444 * 16 byte boundary. Try to avoid trashing it.
2448 /* This is the X/Open sanctioned signal stack switching. */
2449 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2450 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2453 return (sp - frame_size) & ~7;
2456 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2457 static void setup_frame(int sig, struct emulated_sigaction * ka,
2458 target_sigset_t *set, CPUState *regs)
2460 struct sigframe *frame;
2461 abi_ulong frame_addr;
2464 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2465 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2468 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2470 if(setup_sigcontext(regs, &frame->sf_sc))
2473 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2474 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2479 * Arguments to signal handler:
2481 * a0 = signal number
2482 * a1 = 0 (should be cause)
2483 * a2 = pointer to struct sigcontext
2485 * $25 and PC point to the signal handler, $29 points to the
2488 regs->gpr[regs->current_tc][ 4] = sig;
2489 regs->gpr[regs->current_tc][ 5] = 0;
2490 regs->gpr[regs->current_tc][ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2491 regs->gpr[regs->current_tc][29] = frame_addr;
2492 regs->gpr[regs->current_tc][31] = frame_addr + offsetof(struct sigframe, sf_code);
2493 /* The original kernel code sets CP0_EPC to the handler
2494 * since it returns to userland using eret
2495 * we cannot do this here, and we must set PC directly */
2496 regs->PC[regs->current_tc] = regs->gpr[regs->current_tc][25] = ka->sa._sa_handler;
2497 unlock_user_struct(frame, frame_addr, 1);
2501 unlock_user_struct(frame, frame_addr, 1);
2502 force_sig(TARGET_SIGSEGV/*, current*/);
2506 long do_sigreturn(CPUState *regs)
2508 struct sigframe *frame;
2509 abi_ulong frame_addr;
2511 target_sigset_t target_set;
2514 #if defined(DEBUG_SIGNAL)
2515 fprintf(stderr, "do_sigreturn\n");
2517 frame_addr = regs->gpr[regs->current_tc][29];
2518 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2521 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2522 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2526 target_to_host_sigset_internal(&blocked, &target_set);
2527 sigprocmask(SIG_SETMASK, &blocked, NULL);
2529 if (restore_sigcontext(regs, &frame->sf_sc))
2534 * Don't let your children do this ...
2536 __asm__ __volatile__(
2544 regs->PC[regs->current_tc] = regs->CP0_EPC;
2545 /* I am not sure this is right, but it seems to work
2546 * maybe a problem with nested signals ? */
2551 force_sig(TARGET_SIGSEGV/*, current*/);
2555 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2556 target_siginfo_t *info,
2557 target_sigset_t *set, CPUState *env)
2559 fprintf(stderr, "setup_rt_frame: not implemented\n");
2562 long do_rt_sigreturn(CPUState *env)
2564 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2565 return -TARGET_ENOSYS;
2568 #elif defined(TARGET_SH4)
2571 * code and data structures from linux kernel:
2572 * include/asm-sh/sigcontext.h
2573 * arch/sh/kernel/signal.c
2576 struct target_sigcontext {
2577 target_ulong oldmask;
2580 target_ulong sc_gregs[16];
2584 target_ulong sc_gbr;
2585 target_ulong sc_mach;
2586 target_ulong sc_macl;
2589 target_ulong sc_fpregs[16];
2590 target_ulong sc_xfpregs[16];
2591 unsigned int sc_fpscr;
2592 unsigned int sc_fpul;
2593 unsigned int sc_ownedfp;
2596 struct target_sigframe
2598 struct target_sigcontext sc;
2599 target_ulong extramask[TARGET_NSIG_WORDS-1];
2600 uint16_t retcode[3];
2604 struct target_ucontext {
2605 target_ulong uc_flags;
2606 struct target_ucontext *uc_link;
2607 target_stack_t uc_stack;
2608 struct target_sigcontext uc_mcontext;
2609 target_sigset_t uc_sigmask; /* mask last for extensibility */
2612 struct target_rt_sigframe
2614 struct target_siginfo info;
2615 struct target_ucontext uc;
2616 uint16_t retcode[3];
2620 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2621 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2623 static abi_ulong get_sigframe(struct emulated_sigaction *ka,
2624 unsigned long sp, size_t frame_size)
2626 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2627 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2630 return (sp - frame_size) & -8ul;
2633 static int setup_sigcontext(struct target_sigcontext *sc,
2634 CPUState *regs, unsigned long mask)
2638 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2639 COPY(gregs[0]); COPY(gregs[1]);
2640 COPY(gregs[2]); COPY(gregs[3]);
2641 COPY(gregs[4]); COPY(gregs[5]);
2642 COPY(gregs[6]); COPY(gregs[7]);
2643 COPY(gregs[8]); COPY(gregs[9]);
2644 COPY(gregs[10]); COPY(gregs[11]);
2645 COPY(gregs[12]); COPY(gregs[13]);
2646 COPY(gregs[14]); COPY(gregs[15]);
2647 COPY(gbr); COPY(mach);
2648 COPY(macl); COPY(pr);
2652 /* todo: save FPU registers here */
2654 /* non-iBCS2 extensions.. */
2655 err |= __put_user(mask, &sc->oldmask);
2660 static int restore_sigcontext(struct CPUState *regs,
2661 struct target_sigcontext *sc)
2663 unsigned int err = 0;
2665 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2667 COPY(gregs[2]); COPY(gregs[3]);
2668 COPY(gregs[4]); COPY(gregs[5]);
2669 COPY(gregs[6]); COPY(gregs[7]);
2670 COPY(gregs[8]); COPY(gregs[9]);
2671 COPY(gregs[10]); COPY(gregs[11]);
2672 COPY(gregs[12]); COPY(gregs[13]);
2673 COPY(gregs[14]); COPY(gregs[15]);
2674 COPY(gbr); COPY(mach);
2675 COPY(macl); COPY(pr);
2679 /* todo: restore FPU registers here */
2681 regs->tra = -1; /* disable syscall checks */
2685 static void setup_frame(int sig, struct emulated_sigaction *ka,
2686 target_sigset_t *set, CPUState *regs)
2688 struct target_sigframe *frame;
2689 abi_ulong frame_addr;
2694 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2695 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2698 signal = current_exec_domain_sig(sig);
2700 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2702 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2703 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2706 /* Set up to return from userspace. If provided, use a stub
2707 already in userspace. */
2708 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2709 regs->pr = (unsigned long) ka->sa.sa_restorer;
2711 /* Generate return code (system call to sigreturn) */
2712 err |= __put_user(MOVW(2), &frame->retcode[0]);
2713 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2714 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2715 regs->pr = (unsigned long) frame->retcode;
2721 /* Set up registers for signal handler */
2722 regs->gregs[15] = (unsigned long) frame;
2723 regs->gregs[4] = signal; /* Arg for signal handler */
2725 regs->gregs[6] = (unsigned long) &frame->sc;
2726 regs->pc = (unsigned long) ka->sa._sa_handler;
2728 unlock_user_struct(frame, frame_addr, 1);
2732 unlock_user_struct(frame, frame_addr, 1);
2736 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2737 target_siginfo_t *info,
2738 target_sigset_t *set, CPUState *regs)
2740 struct target_rt_sigframe *frame;
2741 abi_ulong frame_addr;
2746 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2747 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2750 signal = current_exec_domain_sig(sig);
2752 err |= copy_siginfo_to_user(&frame->info, info);
2754 /* Create the ucontext. */
2755 err |= __put_user(0, &frame->uc.uc_flags);
2756 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2757 err |= __put_user((void *)target_sigaltstack_used.ss_sp,
2758 &frame->uc.uc_stack.ss_sp);
2759 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2760 &frame->uc.uc_stack.ss_flags);
2761 err |= __put_user(target_sigaltstack_used.ss_size,
2762 &frame->uc.uc_stack.ss_size);
2763 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2765 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2766 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2769 /* Set up to return from userspace. If provided, use a stub
2770 already in userspace. */
2771 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2772 regs->pr = (unsigned long) ka->sa.sa_restorer;
2774 /* Generate return code (system call to sigreturn) */
2775 err |= __put_user(MOVW(2), &frame->retcode[0]);
2776 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2777 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2778 regs->pr = (unsigned long) frame->retcode;
2784 /* Set up registers for signal handler */
2785 regs->gregs[15] = (unsigned long) frame;
2786 regs->gregs[4] = signal; /* Arg for signal handler */
2787 regs->gregs[5] = (unsigned long) &frame->info;
2788 regs->gregs[6] = (unsigned long) &frame->uc;
2789 regs->pc = (unsigned long) ka->sa._sa_handler;
2791 unlock_user_struct(frame, frame_addr, 1);
2795 unlock_user_struct(frame, frame_addr, 1);
2799 long do_sigreturn(CPUState *regs)
2801 struct target_sigframe *frame;
2802 abi_ulong frame_addr;
2804 target_sigset_t target_set;
2808 #if defined(DEBUG_SIGNAL)
2809 fprintf(stderr, "do_sigreturn\n");
2811 frame_addr = regs->gregs[15];
2812 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2815 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2816 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2817 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2823 target_to_host_sigset_internal(&blocked, &target_set);
2824 sigprocmask(SIG_SETMASK, &blocked, NULL);
2826 if (restore_sigcontext(regs, &frame->sc))
2829 unlock_user_struct(frame, frame_addr, 0);
2830 return regs->gregs[0];
2833 unlock_user_struct(frame, frame_addr, 0);
2834 force_sig(TARGET_SIGSEGV);
2838 long do_rt_sigreturn(CPUState *regs)
2840 struct target_rt_sigframe *frame;
2841 abi_ulong frame_addr;
2844 #if defined(DEBUG_SIGNAL)
2845 fprintf(stderr, "do_rt_sigreturn\n");
2847 frame_addr = regs->gregs[15];
2848 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2851 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2852 sigprocmask(SIG_SETMASK, &blocked, NULL);
2854 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2857 if (do_sigaltstack(frame_addr +
2858 offsetof(struct target_rt_sigframe, uc.uc_stack),
2859 0, get_sp_from_cpustate(regs)) == -EFAULT)
2862 unlock_user_struct(frame, frame_addr, 0);
2863 return regs->gregs[0];
2866 unlock_user_struct(frame, frame_addr, 0);
2867 force_sig(TARGET_SIGSEGV);
2870 #elif defined(TARGET_CRIS)
2872 struct target_sigcontext {
2873 struct target_pt_regs regs; /* needs to be first */
2875 uint32_t usp; /* usp before stacking this gunk on it */
2878 /* Signal frames. */
2879 struct target_signal_frame {
2880 struct target_sigcontext sc;
2881 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2882 uint8_t retcode[8]; /* Trampoline code. */
2885 struct rt_signal_frame {
2886 struct siginfo *pinfo;
2888 struct siginfo info;
2890 uint8_t retcode[8]; /* Trampoline code. */
2893 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2895 __put_user(env->regs[0], &sc->regs.r0);
2896 __put_user(env->regs[1], &sc->regs.r1);
2897 __put_user(env->regs[2], &sc->regs.r2);
2898 __put_user(env->regs[3], &sc->regs.r3);
2899 __put_user(env->regs[4], &sc->regs.r4);
2900 __put_user(env->regs[5], &sc->regs.r5);
2901 __put_user(env->regs[6], &sc->regs.r6);
2902 __put_user(env->regs[7], &sc->regs.r7);
2903 __put_user(env->regs[8], &sc->regs.r8);
2904 __put_user(env->regs[9], &sc->regs.r9);
2905 __put_user(env->regs[10], &sc->regs.r10);
2906 __put_user(env->regs[11], &sc->regs.r11);
2907 __put_user(env->regs[12], &sc->regs.r12);
2908 __put_user(env->regs[13], &sc->regs.r13);
2909 __put_user(env->regs[14], &sc->usp);
2910 __put_user(env->regs[15], &sc->regs.acr);
2911 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2912 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2913 __put_user(env->pc, &sc->regs.erp);
2916 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2918 __get_user(env->regs[0], &sc->regs.r0);
2919 __get_user(env->regs[1], &sc->regs.r1);
2920 __get_user(env->regs[2], &sc->regs.r2);
2921 __get_user(env->regs[3], &sc->regs.r3);
2922 __get_user(env->regs[4], &sc->regs.r4);
2923 __get_user(env->regs[5], &sc->regs.r5);
2924 __get_user(env->regs[6], &sc->regs.r6);
2925 __get_user(env->regs[7], &sc->regs.r7);
2926 __get_user(env->regs[8], &sc->regs.r8);
2927 __get_user(env->regs[9], &sc->regs.r9);
2928 __get_user(env->regs[10], &sc->regs.r10);
2929 __get_user(env->regs[11], &sc->regs.r11);
2930 __get_user(env->regs[12], &sc->regs.r12);
2931 __get_user(env->regs[13], &sc->regs.r13);
2932 __get_user(env->regs[14], &sc->usp);
2933 __get_user(env->regs[15], &sc->regs.acr);
2934 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2935 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2936 __get_user(env->pc, &sc->regs.erp);
2939 static abi_ulong get_sigframe(CPUState *env, int framesize)
2942 /* Align the stack downwards to 4. */
2943 sp = (env->regs[R_SP] & ~3);
2944 return sp - framesize;
2947 static void setup_frame(int sig, struct emulated_sigaction *ka,
2948 target_sigset_t *set, CPUState *env)
2950 struct target_signal_frame *frame;
2951 abi_ulong frame_addr;
2955 frame_addr = get_sigframe(env, sizeof *frame);
2956 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2960 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2961 * use this trampoline anymore but it sets it up for GDB.
2962 * In QEMU, using the trampoline simplifies things a bit so we use it.
2964 * This is movu.w __NR_sigreturn, r9; break 13;
2966 err |= __put_user(0x9c5f, frame->retcode+0);
2967 err |= __put_user(TARGET_NR_sigreturn,
2969 err |= __put_user(0xe93d, frame->retcode+4);
2971 /* Save the mask. */
2972 err |= __put_user(set->sig[0], &frame->sc.oldmask);
2976 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2977 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2981 setup_sigcontext(&frame->sc, env);
2983 /* Move the stack and setup the arguments for the handler. */
2984 env->regs[R_SP] = (uint32_t) frame;
2985 env->regs[10] = sig;
2986 env->pc = (unsigned long) ka->sa._sa_handler;
2987 /* Link SRP so the guest returns through the trampoline. */
2988 env->pregs[PR_SRP] = (uint32_t) &frame->retcode[0];
2990 unlock_user_struct(frame, frame_addr, 1);
2993 unlock_user_struct(frame, frame_addr, 1);
2994 force_sig(TARGET_SIGSEGV);
2997 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2998 target_siginfo_t *info,
2999 target_sigset_t *set, CPUState *env)
3001 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3004 long do_sigreturn(CPUState *env)
3006 struct target_signal_frame *frame;
3007 abi_ulong frame_addr;
3008 target_sigset_t target_set;
3012 frame_addr = env->regs[R_SP];
3013 /* Make sure the guest isn't playing games. */
3014 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3017 /* Restore blocked signals */
3018 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3020 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3021 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3024 target_to_host_sigset_internal(&set, &target_set);
3025 sigprocmask(SIG_SETMASK, &set, NULL);
3027 restore_sigcontext(&frame->sc, env);
3028 /* Compensate for the syscall return path advancing brk. */
3031 unlock_user_struct(frame, frame_addr, 0);
3032 return env->regs[10];
3034 unlock_user_struct(frame, frame_addr, 0);
3035 force_sig(TARGET_SIGSEGV);
3038 long do_rt_sigreturn(CPUState *env)
3040 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3041 return -TARGET_ENOSYS;
3046 static void setup_frame(int sig, struct emulated_sigaction *ka,
3047 target_sigset_t *set, CPUState *env)
3049 fprintf(stderr, "setup_frame: not implemented\n");
3052 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
3053 target_siginfo_t *info,
3054 target_sigset_t *set, CPUState *env)
3056 fprintf(stderr, "setup_rt_frame: not implemented\n");
3059 long do_sigreturn(CPUState *env)
3061 fprintf(stderr, "do_sigreturn: not implemented\n");
3062 return -TARGET_ENOSYS;
3065 long do_rt_sigreturn(CPUState *env)
3067 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
3068 return -TARGET_ENOSYS;
3073 void process_pending_signals(void *cpu_env)
3077 sigset_t set, old_set;
3078 target_sigset_t target_old_set;
3079 struct emulated_sigaction *k;
3082 if (!signal_pending)
3086 for(sig = 1; sig <= TARGET_NSIG; sig++) {
3091 /* if no signal is pending, just return */
3097 fprintf(stderr, "qemu: process signal %d\n", sig);
3099 /* dequeue signal */
3105 sig = gdb_handlesig (cpu_env, sig);
3107 fprintf (stderr, "Lost signal\n");
3111 handler = k->sa._sa_handler;
3112 if (handler == TARGET_SIG_DFL) {
3113 /* default handler : ignore some signal. The other are fatal */
3114 if (sig != TARGET_SIGCHLD &&
3115 sig != TARGET_SIGURG &&
3116 sig != TARGET_SIGWINCH) {
3119 } else if (handler == TARGET_SIG_IGN) {
3121 } else if (handler == TARGET_SIG_ERR) {
3124 /* compute the blocked signals during the handler execution */
3125 target_to_host_sigset(&set, &k->sa.sa_mask);
3126 /* SA_NODEFER indicates that the current signal should not be
3127 blocked during the handler */
3128 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
3129 sigaddset(&set, target_to_host_signal(sig));
3131 /* block signals in the handler using Linux */
3132 sigprocmask(SIG_BLOCK, &set, &old_set);
3133 /* save the previous blocked signal state to restore it at the
3134 end of the signal execution (see do_sigreturn) */
3135 host_to_target_sigset_internal(&target_old_set, &old_set);
3137 /* if the CPU is in VM86 mode, we restore the 32 bit values */
3138 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
3140 CPUX86State *env = cpu_env;
3141 if (env->eflags & VM_MASK)
3142 save_v86_state(env);
3145 /* prepare the stack frame of the virtual CPU */
3146 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
3147 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
3149 setup_frame(sig, k, &target_old_set, cpu_env);
3150 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
3151 k->sa._sa_handler = TARGET_SIG_DFL;