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>
31 //#define DEBUG_SIGNAL
33 #define MAX_SIGQUEUE_SIZE 1024
36 struct sigqueue *next;
37 target_siginfo_t info;
40 struct emulated_sigaction {
41 struct target_sigaction sa;
42 int pending; /* true if signal is pending */
43 struct sigqueue *first;
44 struct sigqueue info; /* in order to always have memory for the
45 first signal, we put it here */
48 static struct emulated_sigaction sigact_table[TARGET_NSIG];
49 static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
50 static struct sigqueue *first_free; /* first free siginfo queue entry */
51 static int signal_pending; /* non zero if a signal may be pending */
53 static void host_signal_handler(int host_signum, siginfo_t *info,
56 static uint8_t host_to_target_signal_table[65] = {
57 [SIGHUP] = TARGET_SIGHUP,
58 [SIGINT] = TARGET_SIGINT,
59 [SIGQUIT] = TARGET_SIGQUIT,
60 [SIGILL] = TARGET_SIGILL,
61 [SIGTRAP] = TARGET_SIGTRAP,
62 [SIGABRT] = TARGET_SIGABRT,
63 /* [SIGIOT] = TARGET_SIGIOT,*/
64 [SIGBUS] = TARGET_SIGBUS,
65 [SIGFPE] = TARGET_SIGFPE,
66 [SIGKILL] = TARGET_SIGKILL,
67 [SIGUSR1] = TARGET_SIGUSR1,
68 [SIGSEGV] = TARGET_SIGSEGV,
69 [SIGUSR2] = TARGET_SIGUSR2,
70 [SIGPIPE] = TARGET_SIGPIPE,
71 [SIGALRM] = TARGET_SIGALRM,
72 [SIGTERM] = TARGET_SIGTERM,
74 [SIGSTKFLT] = TARGET_SIGSTKFLT,
76 [SIGCHLD] = TARGET_SIGCHLD,
77 [SIGCONT] = TARGET_SIGCONT,
78 [SIGSTOP] = TARGET_SIGSTOP,
79 [SIGTSTP] = TARGET_SIGTSTP,
80 [SIGTTIN] = TARGET_SIGTTIN,
81 [SIGTTOU] = TARGET_SIGTTOU,
82 [SIGURG] = TARGET_SIGURG,
83 [SIGXCPU] = TARGET_SIGXCPU,
84 [SIGXFSZ] = TARGET_SIGXFSZ,
85 [SIGVTALRM] = TARGET_SIGVTALRM,
86 [SIGPROF] = TARGET_SIGPROF,
87 [SIGWINCH] = TARGET_SIGWINCH,
88 [SIGIO] = TARGET_SIGIO,
89 [SIGPWR] = TARGET_SIGPWR,
90 [SIGSYS] = TARGET_SIGSYS,
91 /* next signals stay the same */
93 static uint8_t target_to_host_signal_table[65];
95 static inline int host_to_target_signal(int sig)
97 return host_to_target_signal_table[sig];
100 static inline int target_to_host_signal(int sig)
102 return target_to_host_signal_table[sig];
105 static void host_to_target_sigset_internal(target_sigset_t *d,
109 unsigned long sigmask;
110 uint32_t target_sigmask;
112 sigmask = ((unsigned long *)s)[0];
114 for(i = 0; i < 32; i++) {
115 if (sigmask & (1 << i))
116 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
118 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
119 d->sig[0] = target_sigmask;
120 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
121 d->sig[i] = ((unsigned long *)s)[i];
123 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
124 d->sig[0] = target_sigmask;
125 d->sig[1] = sigmask >> 32;
127 #warning host_to_target_sigset
131 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
136 host_to_target_sigset_internal(&d1, s);
137 for(i = 0;i < TARGET_NSIG_WORDS; i++)
138 __put_user(d1.sig[i], &d->sig[i]);
141 void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
144 unsigned long sigmask;
145 target_ulong target_sigmask;
147 target_sigmask = s->sig[0];
149 for(i = 0; i < 32; i++) {
150 if (target_sigmask & (1 << i))
151 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
153 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
154 ((unsigned long *)d)[0] = sigmask;
155 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
156 ((unsigned long *)d)[i] = s->sig[i];
158 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
159 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
161 #warning target_to_host_sigset
162 #endif /* TARGET_LONG_BITS */
165 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
170 for(i = 0;i < TARGET_NSIG_WORDS; i++)
171 __get_user(s1.sig[i], &s->sig[i]);
172 target_to_host_sigset_internal(d, &s1);
175 void host_to_target_old_sigset(target_ulong *old_sigset,
176 const sigset_t *sigset)
179 host_to_target_sigset(&d, sigset);
180 *old_sigset = d.sig[0];
183 void target_to_host_old_sigset(sigset_t *sigset,
184 const target_ulong *old_sigset)
189 d.sig[0] = *old_sigset;
190 for(i = 1;i < TARGET_NSIG_WORDS; i++)
192 target_to_host_sigset(sigset, &d);
195 /* siginfo conversion */
197 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
198 const siginfo_t *info)
201 sig = host_to_target_signal(info->si_signo);
202 tinfo->si_signo = sig;
205 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
206 sig == SIGBUS || sig == SIGTRAP) {
207 /* should never come here, but who knows. The information for
208 the target is irrelevant */
209 tinfo->_sifields._sigfault._addr = 0;
210 } else if (sig >= TARGET_SIGRTMIN) {
211 tinfo->_sifields._rt._pid = info->si_pid;
212 tinfo->_sifields._rt._uid = info->si_uid;
213 /* XXX: potential problem if 64 bit */
214 tinfo->_sifields._rt._sigval.sival_ptr =
215 (target_ulong)info->si_value.sival_ptr;
219 static void tswap_siginfo(target_siginfo_t *tinfo,
220 const target_siginfo_t *info)
223 sig = info->si_signo;
224 tinfo->si_signo = tswap32(sig);
225 tinfo->si_errno = tswap32(info->si_errno);
226 tinfo->si_code = tswap32(info->si_code);
227 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
228 sig == SIGBUS || sig == SIGTRAP) {
229 tinfo->_sifields._sigfault._addr =
230 tswapl(info->_sifields._sigfault._addr);
231 } else if (sig >= TARGET_SIGRTMIN) {
232 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
233 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
234 tinfo->_sifields._rt._sigval.sival_ptr =
235 tswapl(info->_sifields._rt._sigval.sival_ptr);
240 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
242 host_to_target_siginfo_noswap(tinfo, info);
243 tswap_siginfo(tinfo, tinfo);
246 /* XXX: we support only POSIX RT signals are used. */
247 /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
248 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
250 info->si_signo = tswap32(tinfo->si_signo);
251 info->si_errno = tswap32(tinfo->si_errno);
252 info->si_code = tswap32(tinfo->si_code);
253 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
254 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
255 info->si_value.sival_ptr =
256 (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
259 void signal_init(void)
261 struct sigaction act;
264 /* generate signal conversion tables */
265 for(i = 1; i <= 64; i++) {
266 if (host_to_target_signal_table[i] == 0)
267 host_to_target_signal_table[i] = i;
269 for(i = 1; i <= 64; i++) {
270 j = host_to_target_signal_table[i];
271 target_to_host_signal_table[j] = i;
274 /* set all host signal handlers. ALL signals are blocked during
275 the handlers to serialize them. */
276 sigfillset(&act.sa_mask);
277 act.sa_flags = SA_SIGINFO;
278 act.sa_sigaction = host_signal_handler;
279 for(i = 1; i < NSIG; i++) {
280 sigaction(i, &act, NULL);
283 memset(sigact_table, 0, sizeof(sigact_table));
285 first_free = &sigqueue_table[0];
286 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
287 sigqueue_table[i].next = &sigqueue_table[i + 1];
288 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
291 /* signal queue handling */
293 static inline struct sigqueue *alloc_sigqueue(void)
295 struct sigqueue *q = first_free;
298 first_free = q->next;
302 static inline void free_sigqueue(struct sigqueue *q)
304 q->next = first_free;
308 /* abort execution with signal */
309 void __attribute((noreturn)) force_sig(int sig)
312 host_sig = target_to_host_signal(sig);
313 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
314 sig, strsignal(host_sig));
319 struct sigaction act;
320 sigemptyset(&act.sa_mask);
321 act.sa_flags = SA_SIGINFO;
322 act.sa_sigaction = SIG_DFL;
323 sigaction(SIGABRT, &act, NULL);
329 /* queue a signal so that it will be send to the virtual CPU as soon
331 int queue_signal(int sig, target_siginfo_t *info)
333 struct emulated_sigaction *k;
334 struct sigqueue *q, **pq;
335 target_ulong handler;
337 #if defined(DEBUG_SIGNAL)
338 fprintf(stderr, "queue_signal: sig=%d\n",
341 k = &sigact_table[sig - 1];
342 handler = k->sa._sa_handler;
343 if (handler == TARGET_SIG_DFL) {
344 /* default handler : ignore some signal. The other are fatal */
345 if (sig != TARGET_SIGCHLD &&
346 sig != TARGET_SIGURG &&
347 sig != TARGET_SIGWINCH) {
350 return 0; /* indicate ignored */
352 } else if (handler == TARGET_SIG_IGN) {
355 } else if (handler == TARGET_SIG_ERR) {
359 if (sig < TARGET_SIGRTMIN) {
360 /* if non real time signal, we queue exactly one signal */
370 q = alloc_sigqueue();
381 /* signal that a new signal is pending */
383 return 1; /* indicates that the signal was queued */
387 static void host_signal_handler(int host_signum, siginfo_t *info,
391 target_siginfo_t tinfo;
393 /* the CPU emulator uses some host signals to detect exceptions,
394 we we forward to it some signals */
395 if (host_signum == SIGSEGV || host_signum == SIGBUS
396 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
397 || host_signum == SIGFPE
400 if (cpu_signal_handler(host_signum, info, puc))
404 /* get target signal number */
405 sig = host_to_target_signal(host_signum);
406 if (sig < 1 || sig > TARGET_NSIG)
408 #if defined(DEBUG_SIGNAL)
409 fprintf(stderr, "qemu: got signal %d\n", sig);
411 host_to_target_siginfo_noswap(&tinfo, info);
412 if (queue_signal(sig, &tinfo) == 1) {
413 /* interrupt the virtual CPU as soon as possible */
414 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
418 int do_sigaction(int sig, const struct target_sigaction *act,
419 struct target_sigaction *oact)
421 struct emulated_sigaction *k;
422 struct sigaction act1;
425 if (sig < 1 || sig > TARGET_NSIG)
427 k = &sigact_table[sig - 1];
428 #if defined(DEBUG_SIGNAL)
429 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
430 sig, (int)act, (int)oact);
433 oact->_sa_handler = tswapl(k->sa._sa_handler);
434 oact->sa_flags = tswapl(k->sa.sa_flags);
435 oact->sa_restorer = tswapl(k->sa.sa_restorer);
436 oact->sa_mask = k->sa.sa_mask;
439 k->sa._sa_handler = tswapl(act->_sa_handler);
440 k->sa.sa_flags = tswapl(act->sa_flags);
441 k->sa.sa_restorer = tswapl(act->sa_restorer);
442 k->sa.sa_mask = act->sa_mask;
444 /* we update the host linux signal state */
445 host_sig = target_to_host_signal(sig);
446 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
447 sigfillset(&act1.sa_mask);
448 act1.sa_flags = SA_SIGINFO;
449 if (k->sa.sa_flags & TARGET_SA_RESTART)
450 act1.sa_flags |= SA_RESTART;
451 /* NOTE: it is important to update the host kernel signal
452 ignore state to avoid getting unexpected interrupted
454 if (k->sa._sa_handler == TARGET_SIG_IGN) {
455 act1.sa_sigaction = (void *)SIG_IGN;
456 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
457 act1.sa_sigaction = (void *)SIG_DFL;
459 act1.sa_sigaction = host_signal_handler;
461 sigaction(host_sig, &act1, NULL);
468 #define offsetof(type, field) ((size_t) &((type *)0)->field)
471 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
472 const target_siginfo_t *info)
474 tswap_siginfo(tinfo, info);
480 /* from the Linux kernel */
482 struct target_fpreg {
483 uint16_t significand[4];
487 struct target_fpxreg {
488 uint16_t significand[4];
493 struct target_xmmreg {
494 target_ulong element[4];
497 struct target_fpstate {
498 /* Regular FPU environment */
504 target_ulong dataoff;
505 target_ulong datasel;
506 struct target_fpreg _st[8];
508 uint16_t magic; /* 0xffff = regular FPU data only */
510 /* FXSR FPU environment */
511 target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
513 target_ulong reserved;
514 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
515 struct target_xmmreg _xmm[8];
516 target_ulong padding[56];
519 #define X86_FXSR_MAGIC 0x0000
521 struct target_sigcontext {
539 target_ulong esp_at_signal;
541 target_ulong fpstate; /* pointer */
542 target_ulong oldmask;
546 typedef struct target_sigaltstack {
549 target_ulong ss_size;
552 struct target_ucontext {
553 target_ulong tuc_flags;
554 target_ulong tuc_link;
555 target_stack_t tuc_stack;
556 struct target_sigcontext tuc_mcontext;
557 target_sigset_t tuc_sigmask; /* mask last for extensibility */
562 target_ulong pretcode;
564 struct target_sigcontext sc;
565 struct target_fpstate fpstate;
566 target_ulong extramask[TARGET_NSIG_WORDS-1];
572 target_ulong pretcode;
576 struct target_siginfo info;
577 struct target_ucontext uc;
578 struct target_fpstate fpstate;
583 * Set up a signal frame.
586 /* XXX: save x87 state */
588 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
589 CPUX86State *env, unsigned long mask)
593 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
594 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
595 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
596 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
597 err |= __put_user(env->regs[R_EDI], &sc->edi);
598 err |= __put_user(env->regs[R_ESI], &sc->esi);
599 err |= __put_user(env->regs[R_EBP], &sc->ebp);
600 err |= __put_user(env->regs[R_ESP], &sc->esp);
601 err |= __put_user(env->regs[R_EBX], &sc->ebx);
602 err |= __put_user(env->regs[R_EDX], &sc->edx);
603 err |= __put_user(env->regs[R_ECX], &sc->ecx);
604 err |= __put_user(env->regs[R_EAX], &sc->eax);
605 err |= __put_user(env->exception_index, &sc->trapno);
606 err |= __put_user(env->error_code, &sc->err);
607 err |= __put_user(env->eip, &sc->eip);
608 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
609 err |= __put_user(env->eflags, &sc->eflags);
610 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
611 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
613 cpu_x86_fsave(env, (void *)fpstate, 1);
614 fpstate->status = fpstate->sw;
615 err |= __put_user(0xffff, &fpstate->magic);
616 err |= __put_user(fpstate, &sc->fpstate);
618 /* non-iBCS2 extensions.. */
619 err |= __put_user(mask, &sc->oldmask);
620 err |= __put_user(env->cr[2], &sc->cr2);
625 * Determine which stack to use..
629 get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
633 /* Default to using normal stack */
634 esp = env->regs[R_ESP];
636 /* This is the X/Open sanctioned signal stack switching. */
637 if (ka->sa.sa_flags & SA_ONSTACK) {
638 if (sas_ss_flags(esp) == 0)
639 esp = current->sas_ss_sp + current->sas_ss_size;
642 /* This is the legacy signal stack switching. */
645 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
646 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
647 ka->sa.sa_restorer) {
648 esp = (unsigned long) ka->sa.sa_restorer;
650 return (void *)((esp - frame_size) & -8ul);
653 static void setup_frame(int sig, struct emulated_sigaction *ka,
654 target_sigset_t *set, CPUX86State *env)
656 struct sigframe *frame;
659 frame = get_sigframe(ka, env, sizeof(*frame));
661 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
663 err |= __put_user((/*current->exec_domain
664 && current->exec_domain->signal_invmap
666 ? current->exec_domain->signal_invmap[sig]
672 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
676 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
677 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
681 /* Set up to return from userspace. If provided, use a stub
682 already in userspace. */
683 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
684 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
686 err |= __put_user(frame->retcode, &frame->pretcode);
687 /* This is popl %eax ; movl $,%eax ; int $0x80 */
688 err |= __put_user(0xb858, (short *)(frame->retcode+0));
689 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
690 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
696 /* Set up registers for signal handler */
697 env->regs[R_ESP] = (unsigned long) frame;
698 env->eip = (unsigned long) ka->sa._sa_handler;
700 cpu_x86_load_seg(env, R_DS, __USER_DS);
701 cpu_x86_load_seg(env, R_ES, __USER_DS);
702 cpu_x86_load_seg(env, R_SS, __USER_DS);
703 cpu_x86_load_seg(env, R_CS, __USER_CS);
704 env->eflags &= ~TF_MASK;
709 if (sig == TARGET_SIGSEGV)
710 ka->sa._sa_handler = TARGET_SIG_DFL;
711 force_sig(TARGET_SIGSEGV /* , current */);
714 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
715 target_siginfo_t *info,
716 target_sigset_t *set, CPUX86State *env)
718 struct rt_sigframe *frame;
721 frame = get_sigframe(ka, env, sizeof(*frame));
723 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
726 err |= __put_user((/*current->exec_domain
727 && current->exec_domain->signal_invmap
729 ? current->exec_domain->signal_invmap[sig]
732 err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
733 err |= __put_user((target_ulong)&frame->uc, &frame->puc);
734 err |= copy_siginfo_to_user(&frame->info, info);
738 /* Create the ucontext. */
739 err |= __put_user(0, &frame->uc.tuc_flags);
740 err |= __put_user(0, &frame->uc.tuc_link);
741 err |= __put_user(/*current->sas_ss_sp*/ 0,
742 &frame->uc.tuc_stack.ss_sp);
743 err |= __put_user(/* sas_ss_flags(regs->esp) */ 0,
744 &frame->uc.tuc_stack.ss_flags);
745 err |= __put_user(/* current->sas_ss_size */ 0,
746 &frame->uc.tuc_stack.ss_size);
747 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
749 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
750 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
754 /* Set up to return from userspace. If provided, use a stub
755 already in userspace. */
756 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
757 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
759 err |= __put_user(frame->retcode, &frame->pretcode);
760 /* This is movl $,%eax ; int $0x80 */
761 err |= __put_user(0xb8, (char *)(frame->retcode+0));
762 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
763 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
769 /* Set up registers for signal handler */
770 env->regs[R_ESP] = (unsigned long) frame;
771 env->eip = (unsigned long) ka->sa._sa_handler;
773 cpu_x86_load_seg(env, R_DS, __USER_DS);
774 cpu_x86_load_seg(env, R_ES, __USER_DS);
775 cpu_x86_load_seg(env, R_SS, __USER_DS);
776 cpu_x86_load_seg(env, R_CS, __USER_CS);
777 env->eflags &= ~TF_MASK;
782 if (sig == TARGET_SIGSEGV)
783 ka->sa._sa_handler = TARGET_SIG_DFL;
784 force_sig(TARGET_SIGSEGV /* , current */);
788 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
790 unsigned int err = 0;
792 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
793 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
794 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
795 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
797 env->regs[R_EDI] = ldl(&sc->edi);
798 env->regs[R_ESI] = ldl(&sc->esi);
799 env->regs[R_EBP] = ldl(&sc->ebp);
800 env->regs[R_ESP] = ldl(&sc->esp);
801 env->regs[R_EBX] = ldl(&sc->ebx);
802 env->regs[R_EDX] = ldl(&sc->edx);
803 env->regs[R_ECX] = ldl(&sc->ecx);
804 env->eip = ldl(&sc->eip);
806 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
807 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
810 unsigned int tmpflags;
811 tmpflags = ldl(&sc->eflags);
812 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
813 // regs->orig_eax = -1; /* disable syscall checks */
817 struct _fpstate * buf;
818 buf = (void *)ldl(&sc->fpstate);
821 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
824 cpu_x86_frstor(env, (void *)buf, 1);
828 *peax = ldl(&sc->eax);
836 long do_sigreturn(CPUX86State *env)
838 struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8);
839 target_sigset_t target_set;
843 #if defined(DEBUG_SIGNAL)
844 fprintf(stderr, "do_sigreturn\n");
846 /* set blocked signals */
847 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
849 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
850 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
854 target_to_host_sigset_internal(&set, &target_set);
855 sigprocmask(SIG_SETMASK, &set, NULL);
857 /* restore registers */
858 if (restore_sigcontext(env, &frame->sc, &eax))
863 force_sig(TARGET_SIGSEGV);
867 long do_rt_sigreturn(CPUX86State *env)
869 struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
875 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
878 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
879 sigprocmask(SIG_SETMASK, &set, NULL);
881 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
885 if (__copy_from_user(&st, &frame->uc.tuc_stack, sizeof(st)))
887 /* It is more difficult to avoid calling this function than to
888 call it and ignore errors. */
889 do_sigaltstack(&st, NULL, regs->esp);
894 force_sig(TARGET_SIGSEGV);
898 #elif defined(TARGET_ARM)
900 struct target_sigcontext {
901 target_ulong trap_no;
902 target_ulong error_code;
903 target_ulong oldmask;
914 target_ulong arm_r10;
920 target_ulong arm_cpsr;
921 target_ulong fault_address;
924 typedef struct target_sigaltstack {
927 target_ulong ss_size;
930 struct target_ucontext {
931 target_ulong tuc_flags;
932 target_ulong tuc_link;
933 target_stack_t tuc_stack;
934 struct target_sigcontext tuc_mcontext;
935 target_sigset_t tuc_sigmask; /* mask last for extensibility */
940 struct target_sigcontext sc;
941 target_ulong extramask[TARGET_NSIG_WORDS-1];
942 target_ulong retcode;
947 struct target_siginfo *pinfo;
949 struct target_siginfo info;
950 struct target_ucontext uc;
951 target_ulong retcode;
954 #define TARGET_CONFIG_CPU_32 1
957 * For ARM syscalls, we encode the syscall number into the instruction.
959 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
960 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
963 * For Thumb syscalls, we pass the syscall number via r7. We therefore
964 * need two 16-bit instructions.
966 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
967 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
969 static const target_ulong retcodes[4] = {
970 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
971 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
975 #define __put_user_error(x,p,e) __put_user(x, p)
976 #define __get_user_error(x,p,e) __get_user(x, p)
978 static inline int valid_user_regs(CPUState *regs)
984 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
985 CPUState *env, unsigned long mask)
989 __put_user_error(env->regs[0], &sc->arm_r0, err);
990 __put_user_error(env->regs[1], &sc->arm_r1, err);
991 __put_user_error(env->regs[2], &sc->arm_r2, err);
992 __put_user_error(env->regs[3], &sc->arm_r3, err);
993 __put_user_error(env->regs[4], &sc->arm_r4, err);
994 __put_user_error(env->regs[5], &sc->arm_r5, err);
995 __put_user_error(env->regs[6], &sc->arm_r6, err);
996 __put_user_error(env->regs[7], &sc->arm_r7, err);
997 __put_user_error(env->regs[8], &sc->arm_r8, err);
998 __put_user_error(env->regs[9], &sc->arm_r9, err);
999 __put_user_error(env->regs[10], &sc->arm_r10, err);
1000 __put_user_error(env->regs[11], &sc->arm_fp, err);
1001 __put_user_error(env->regs[12], &sc->arm_ip, err);
1002 __put_user_error(env->regs[13], &sc->arm_sp, err);
1003 __put_user_error(env->regs[14], &sc->arm_lr, err);
1004 __put_user_error(env->regs[15], &sc->arm_pc, err);
1005 #ifdef TARGET_CONFIG_CPU_32
1006 __put_user_error(env->cpsr, &sc->arm_cpsr, err);
1009 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1010 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1011 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1012 __put_user_error(mask, &sc->oldmask, err);
1017 static inline void *
1018 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1020 unsigned long sp = regs->regs[13];
1024 * This is the X/Open sanctioned signal stack switching.
1026 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
1027 sp = current->sas_ss_sp + current->sas_ss_size;
1030 * ATPCS B01 mandates 8-byte alignment
1032 return (void *)((sp - framesize) & ~7);
1036 setup_return(CPUState *env, struct emulated_sigaction *ka,
1037 target_ulong *rc, void *frame, int usig)
1039 target_ulong handler = (target_ulong)ka->sa._sa_handler;
1040 target_ulong retcode;
1042 #if defined(TARGET_CONFIG_CPU_32)
1043 target_ulong cpsr = env->cpsr;
1047 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1049 if (ka->sa.sa_flags & SA_THIRTYTWO)
1050 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1052 #ifdef CONFIG_ARM_THUMB
1053 if (elf_hwcap & HWCAP_THUMB) {
1055 * The LSB of the handler determines if we're going to
1056 * be using THUMB or ARM mode for this signal handler.
1058 thumb = handler & 1;
1067 #endif /* TARGET_CONFIG_CPU_32 */
1069 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1070 retcode = (target_ulong)ka->sa.sa_restorer;
1072 unsigned int idx = thumb;
1074 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1077 if (__put_user(retcodes[idx], rc))
1080 flush_icache_range((target_ulong)rc,
1081 (target_ulong)(rc + 1));
1083 retcode = ((target_ulong)rc) + thumb;
1086 env->regs[0] = usig;
1087 env->regs[13] = (target_ulong)frame;
1088 env->regs[14] = retcode;
1089 env->regs[15] = handler & (thumb ? ~1 : ~3);
1091 #ifdef TARGET_CONFIG_CPU_32
1098 static void setup_frame(int usig, struct emulated_sigaction *ka,
1099 target_sigset_t *set, CPUState *regs)
1101 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
1104 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1106 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1107 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1112 err = setup_return(regs, ka, &frame->retcode, frame, usig);
1116 static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1117 target_siginfo_t *info,
1118 target_sigset_t *set, CPUState *env)
1120 struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame));
1123 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
1126 __put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err);
1127 __put_user_error(&frame->uc, (target_ulong *)&frame->puc, err);
1128 err |= copy_siginfo_to_user(&frame->info, info);
1130 /* Clear all the bits of the ucontext we don't use. */
1131 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
1133 err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
1135 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1136 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1141 err = setup_return(env, ka, &frame->retcode, frame, usig);
1145 * For realtime signals we must also set the second and third
1146 * arguments for the signal handler.
1149 env->regs[1] = (target_ulong)frame->pinfo;
1150 env->regs[2] = (target_ulong)frame->puc;
1157 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1161 __get_user_error(env->regs[0], &sc->arm_r0, err);
1162 __get_user_error(env->regs[1], &sc->arm_r1, err);
1163 __get_user_error(env->regs[2], &sc->arm_r2, err);
1164 __get_user_error(env->regs[3], &sc->arm_r3, err);
1165 __get_user_error(env->regs[4], &sc->arm_r4, err);
1166 __get_user_error(env->regs[5], &sc->arm_r5, err);
1167 __get_user_error(env->regs[6], &sc->arm_r6, err);
1168 __get_user_error(env->regs[7], &sc->arm_r7, err);
1169 __get_user_error(env->regs[8], &sc->arm_r8, err);
1170 __get_user_error(env->regs[9], &sc->arm_r9, err);
1171 __get_user_error(env->regs[10], &sc->arm_r10, err);
1172 __get_user_error(env->regs[11], &sc->arm_fp, err);
1173 __get_user_error(env->regs[12], &sc->arm_ip, err);
1174 __get_user_error(env->regs[13], &sc->arm_sp, err);
1175 __get_user_error(env->regs[14], &sc->arm_lr, err);
1176 __get_user_error(env->regs[15], &sc->arm_pc, err);
1177 #ifdef TARGET_CONFIG_CPU_32
1178 __get_user_error(env->cpsr, &sc->arm_cpsr, err);
1181 err |= !valid_user_regs(env);
1186 long do_sigreturn(CPUState *env)
1188 struct sigframe *frame;
1189 target_sigset_t set;
1194 * Since we stacked the signal on a 64-bit boundary,
1195 * then 'sp' should be word aligned here. If it's
1196 * not, then the user is trying to mess with us.
1198 if (env->regs[13] & 7)
1201 frame = (struct sigframe *)env->regs[13];
1204 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1207 if (__get_user(set.sig[0], &frame->sc.oldmask))
1209 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1210 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1214 target_to_host_sigset_internal(&host_set, &set);
1215 sigprocmask(SIG_SETMASK, &host_set, NULL);
1217 if (restore_sigcontext(env, &frame->sc))
1221 /* Send SIGTRAP if we're single-stepping */
1222 if (ptrace_cancel_bpt(current))
1223 send_sig(SIGTRAP, current, 1);
1225 return env->regs[0];
1228 force_sig(SIGSEGV /* , current */);
1232 long do_rt_sigreturn(CPUState *env)
1234 struct rt_sigframe *frame;
1238 * Since we stacked the signal on a 64-bit boundary,
1239 * then 'sp' should be word aligned here. If it's
1240 * not, then the user is trying to mess with us.
1242 if (env->regs[13] & 7)
1245 frame = (struct rt_sigframe *)env->regs[13];
1248 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1251 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1252 sigprocmask(SIG_SETMASK, &host_set, NULL);
1254 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1258 /* Send SIGTRAP if we're single-stepping */
1259 if (ptrace_cancel_bpt(current))
1260 send_sig(SIGTRAP, current, 1);
1262 return env->regs[0];
1265 force_sig(SIGSEGV /* , current */);
1269 #elif defined(TARGET_SPARC)
1271 #define __SUNOS_MAXWIN 31
1273 /* This is what SunOS does, so shall I. */
1274 struct target_sigcontext {
1275 target_ulong sigc_onstack; /* state to restore */
1277 target_ulong sigc_mask; /* sigmask to restore */
1278 target_ulong sigc_sp; /* stack pointer */
1279 target_ulong sigc_pc; /* program counter */
1280 target_ulong sigc_npc; /* next program counter */
1281 target_ulong sigc_psr; /* for condition codes etc */
1282 target_ulong sigc_g1; /* User uses these two registers */
1283 target_ulong sigc_o0; /* within the trampoline code. */
1285 /* Now comes information regarding the users window set
1286 * at the time of the signal.
1288 target_ulong sigc_oswins; /* outstanding windows */
1290 /* stack ptrs for each regwin buf */
1291 char *sigc_spbuf[__SUNOS_MAXWIN];
1293 /* Windows to restore after signal */
1295 target_ulong locals[8];
1296 target_ulong ins[8];
1297 } sigc_wbuf[__SUNOS_MAXWIN];
1299 /* A Sparc stack frame */
1300 struct sparc_stackf {
1301 target_ulong locals[8];
1302 target_ulong ins[6];
1303 struct sparc_stackf *fp;
1304 target_ulong callers_pc;
1306 target_ulong xargs[6];
1307 target_ulong xxargs[1];
1316 target_ulong u_regs[16]; /* globals and ins */
1322 unsigned long si_float_regs [32];
1323 unsigned long si_fsr;
1324 unsigned long si_fpqdepth;
1326 unsigned long *insn_addr;
1332 struct target_signal_frame {
1333 struct sparc_stackf ss;
1335 __siginfo_fpu_t *fpu_save;
1336 target_ulong insns[2] __attribute__ ((aligned (8)));
1337 target_ulong extramask[TARGET_NSIG_WORDS - 1];
1338 target_ulong extra_size; /* Should be 0 */
1339 __siginfo_fpu_t fpu_state;
1341 struct target_rt_signal_frame {
1342 struct sparc_stackf ss;
1344 target_ulong regs[20];
1346 __siginfo_fpu_t *fpu_save;
1347 unsigned int insns[2];
1349 unsigned int extra_size; /* Should be 0 */
1350 __siginfo_fpu_t fpu_state;
1361 #define UREG_FP UREG_I6
1362 #define UREG_SP UREG_O6
1364 static inline void *get_sigframe(struct emulated_sigaction *sa, CPUState *env, unsigned long framesize)
1368 sp = env->regwptr[UREG_FP];
1371 /* This is the X/Open sanctioned signal stack switching. */
1372 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1373 if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
1374 sp = current->sas_ss_sp + current->sas_ss_size;
1377 return (void *)(sp - framesize);
1381 setup___siginfo(__siginfo_t *si, CPUState *env, target_ulong mask)
1385 err |= __put_user(env->psr, &si->si_regs.psr);
1386 err |= __put_user(env->pc, &si->si_regs.pc);
1387 err |= __put_user(env->npc, &si->si_regs.npc);
1388 err |= __put_user(env->y, &si->si_regs.y);
1389 for (i=0; i < 8; i++) {
1390 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1392 for (i=0; i < 8; i++) {
1393 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1395 err |= __put_user(mask, &si->si_mask);
1401 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1402 CPUState *env, unsigned long mask)
1406 err |= __put_user(mask, &sc->sigc_mask);
1407 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1408 err |= __put_user(env->pc, &sc->sigc_pc);
1409 err |= __put_user(env->npc, &sc->sigc_npc);
1410 err |= __put_user(env->psr, &sc->sigc_psr);
1411 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1412 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1417 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1419 static void setup_frame(int sig, struct emulated_sigaction *ka,
1420 target_sigset_t *set, CPUState *env)
1422 struct target_signal_frame *sf;
1423 int sigframe_size, err, i;
1425 /* 1. Make sure everything is clean */
1426 //synchronize_user_stack();
1428 sigframe_size = NF_ALIGNEDSZ;
1430 sf = (struct target_signal_frame *)
1431 get_sigframe(ka, env, sigframe_size);
1433 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1435 if (invalid_frame_pointer(sf, sigframe_size))
1436 goto sigill_and_return;
1438 /* 2. Save the current process state */
1439 err = setup___siginfo(&sf->info, env, set->sig[0]);
1440 err |= __put_user(0, &sf->extra_size);
1442 //err |= save_fpu_state(regs, &sf->fpu_state);
1443 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1445 err |= __put_user(set->sig[0], &sf->info.si_mask);
1446 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1447 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1450 for (i = 0; i < 8; i++) {
1451 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1453 for (i = 0; i < 8; i++) {
1454 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1459 /* 3. signal handler back-trampoline and parameters */
1460 env->regwptr[UREG_FP] = (target_ulong) sf;
1461 env->regwptr[UREG_I0] = sig;
1462 env->regwptr[UREG_I1] = (target_ulong) &sf->info;
1463 env->regwptr[UREG_I2] = (target_ulong) &sf->info;
1465 /* 4. signal handler */
1466 env->pc = (unsigned long) ka->sa._sa_handler;
1467 env->npc = (env->pc + 4);
1468 /* 5. return to kernel instructions */
1469 if (ka->sa.sa_restorer)
1470 env->regwptr[UREG_I7] = (unsigned long)ka->sa.sa_restorer;
1472 env->regwptr[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2);
1474 /* mov __NR_sigreturn, %g1 */
1475 err |= __put_user(0x821020d8, &sf->insns[0]);
1478 err |= __put_user(0x91d02010, &sf->insns[1]);
1482 /* Flush instruction space. */
1483 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1488 //sigill_and_return:
1489 force_sig(TARGET_SIGILL);
1491 //fprintf(stderr, "force_sig\n");
1492 force_sig(TARGET_SIGSEGV);
1495 restore_fpu_state(CPUState *env, __siginfo_fpu_t *fpu)
1500 if (current->flags & PF_USEDFPU)
1501 regs->psr &= ~PSR_EF;
1503 if (current == last_task_used_math) {
1504 last_task_used_math = 0;
1505 regs->psr &= ~PSR_EF;
1508 current->used_math = 1;
1509 current->flags &= ~PF_USEDFPU;
1512 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1516 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1517 (sizeof(unsigned long) * 32));
1518 err |= __get_user(env->fsr, &fpu->si_fsr);
1520 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1521 if (current->thread.fpqdepth != 0)
1522 err |= __copy_from_user(¤t->thread.fpqueue[0],
1523 &fpu->si_fpqueue[0],
1524 ((sizeof(unsigned long) +
1525 (sizeof(unsigned long *)))*16));
1531 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1532 target_siginfo_t *info,
1533 target_sigset_t *set, CPUState *env)
1535 fprintf(stderr, "setup_rt_frame: not implemented\n");
1538 long do_sigreturn(CPUState *env)
1540 struct target_signal_frame *sf;
1541 uint32_t up_psr, pc, npc;
1542 target_sigset_t set;
1544 target_ulong fpu_save;
1547 sf = (struct target_signal_frame *) env->regwptr[UREG_FP];
1549 fprintf(stderr, "sigreturn\n");
1550 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1552 //cpu_dump_state(env, stderr, fprintf, 0);
1554 /* 1. Make sure we are not getting garbage from the user */
1556 if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
1560 if (((uint) sf) & 3)
1563 err = __get_user(pc, &sf->info.si_regs.pc);
1564 err |= __get_user(npc, &sf->info.si_regs.npc);
1569 /* 2. Restore the state */
1570 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1572 /* User can only change condition codes and FPU enabling in %psr. */
1573 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1574 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1578 err |= __get_user(env->y, &sf->info.si_regs.y);
1579 for (i=0; i < 8; i++) {
1580 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1582 for (i=0; i < 8; i++) {
1583 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1586 err |= __get_user(fpu_save, (target_ulong *)&sf->fpu_save);
1589 // err |= restore_fpu_state(env, fpu_save);
1591 /* This is pretty much atomic, no amount locking would prevent
1592 * the races which exist anyways.
1594 err |= __get_user(set.sig[0], &sf->info.si_mask);
1595 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1596 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1599 target_to_host_sigset_internal(&host_set, &set);
1600 sigprocmask(SIG_SETMASK, &host_set, NULL);
1605 return env->regwptr[0];
1608 force_sig(TARGET_SIGSEGV);
1611 long do_rt_sigreturn(CPUState *env)
1613 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1620 static void setup_frame(int sig, struct emulated_sigaction *ka,
1621 target_sigset_t *set, CPUState *env)
1623 fprintf(stderr, "setup_frame: not implemented\n");
1626 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1627 target_siginfo_t *info,
1628 target_sigset_t *set, CPUState *env)
1630 fprintf(stderr, "setup_rt_frame: not implemented\n");
1633 long do_sigreturn(CPUState *env)
1635 fprintf(stderr, "do_sigreturn: not implemented\n");
1639 long do_rt_sigreturn(CPUState *env)
1641 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1647 void process_pending_signals(void *cpu_env)
1650 target_ulong handler;
1651 sigset_t set, old_set;
1652 target_sigset_t target_old_set;
1653 struct emulated_sigaction *k;
1656 if (!signal_pending)
1660 for(sig = 1; sig <= TARGET_NSIG; sig++) {
1665 /* if no signal is pending, just return */
1671 fprintf(stderr, "qemu: process signal %d\n", sig);
1673 /* dequeue signal */
1679 sig = gdb_handlesig (cpu_env, sig);
1681 fprintf (stderr, "Lost signal\n");
1685 handler = k->sa._sa_handler;
1686 if (handler == TARGET_SIG_DFL) {
1687 /* default handler : ignore some signal. The other are fatal */
1688 if (sig != TARGET_SIGCHLD &&
1689 sig != TARGET_SIGURG &&
1690 sig != TARGET_SIGWINCH) {
1693 } else if (handler == TARGET_SIG_IGN) {
1695 } else if (handler == TARGET_SIG_ERR) {
1698 /* compute the blocked signals during the handler execution */
1699 target_to_host_sigset(&set, &k->sa.sa_mask);
1700 /* SA_NODEFER indicates that the current signal should not be
1701 blocked during the handler */
1702 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
1703 sigaddset(&set, target_to_host_signal(sig));
1705 /* block signals in the handler using Linux */
1706 sigprocmask(SIG_BLOCK, &set, &old_set);
1707 /* save the previous blocked signal state to restore it at the
1708 end of the signal execution (see do_sigreturn) */
1709 host_to_target_sigset_internal(&target_old_set, &old_set);
1711 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1714 CPUX86State *env = cpu_env;
1715 if (env->eflags & VM_MASK)
1716 save_v86_state(env);
1719 /* prepare the stack frame of the virtual CPU */
1720 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
1721 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
1723 setup_frame(sig, k, &target_old_set, cpu_env);
1724 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
1725 k->sa._sa_handler = TARGET_SIG_DFL;