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>
38 //#define DEBUG_SIGNAL
40 #define MAX_SIGQUEUE_SIZE 1024
43 struct sigqueue *next;
44 target_siginfo_t info;
47 struct emulated_sigaction {
48 struct target_sigaction sa;
49 int pending; /* true if signal is pending */
50 struct sigqueue *first;
51 struct sigqueue info; /* in order to always have memory for the
52 first signal, we put it here */
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 host_to_target_signal(int sig)
104 return host_to_target_signal_table[sig];
107 static inline int target_to_host_signal(int sig)
109 return target_to_host_signal_table[sig];
112 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
115 unsigned long sigmask;
116 uint32_t target_sigmask;
118 sigmask = ((unsigned long *)s)[0];
120 for(i = 0; i < 32; i++) {
121 if (sigmask & (1 << i))
122 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
124 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
125 d->sig[0] = tswapl(target_sigmask);
126 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
127 d->sig[i] = tswapl(((unsigned long *)s)[i]);
129 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
130 d->sig[0] = tswapl(target_sigmask);
131 d->sig[1] = tswapl(sigmask >> 32);
133 #error host_to_target_sigset
137 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
140 unsigned long sigmask;
141 target_ulong target_sigmask;
143 target_sigmask = tswapl(s->sig[0]);
145 for(i = 0; i < 32; i++) {
146 if (target_sigmask & (1 << i))
147 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
149 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
150 ((unsigned long *)d)[0] = sigmask;
151 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
152 ((unsigned long *)d)[i] = tswapl(s->sig[i]);
154 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
155 ((unsigned long *)d)[0] = sigmask | ((unsigned long)tswapl(s->sig[1]) << 32);
157 #error target_to_host_sigset
158 #endif /* TARGET_LONG_BITS */
161 void host_to_target_old_sigset(target_ulong *old_sigset,
162 const sigset_t *sigset)
165 host_to_target_sigset(&d, sigset);
166 *old_sigset = d.sig[0];
169 void target_to_host_old_sigset(sigset_t *sigset,
170 const target_ulong *old_sigset)
175 d.sig[0] = *old_sigset;
176 for(i = 1;i < TARGET_NSIG_WORDS; i++)
178 target_to_host_sigset(sigset, &d);
181 /* siginfo conversion */
183 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
184 const siginfo_t *info)
187 sig = host_to_target_signal(info->si_signo);
188 tinfo->si_signo = sig;
191 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
192 sig == SIGBUS || sig == SIGTRAP) {
193 /* should never come here, but who knows. The information for
194 the target is irrelevant */
195 tinfo->_sifields._sigfault._addr = 0;
196 } else if (sig >= TARGET_SIGRTMIN) {
197 tinfo->_sifields._rt._pid = info->si_pid;
198 tinfo->_sifields._rt._uid = info->si_uid;
199 /* XXX: potential problem if 64 bit */
200 tinfo->_sifields._rt._sigval.sival_ptr =
201 (target_ulong)info->si_value.sival_ptr;
205 static void tswap_siginfo(target_siginfo_t *tinfo,
206 const target_siginfo_t *info)
209 sig = info->si_signo;
210 tinfo->si_signo = tswap32(sig);
211 tinfo->si_errno = tswap32(info->si_errno);
212 tinfo->si_code = tswap32(info->si_code);
213 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
214 sig == SIGBUS || sig == SIGTRAP) {
215 tinfo->_sifields._sigfault._addr =
216 tswapl(info->_sifields._sigfault._addr);
217 } else if (sig >= TARGET_SIGRTMIN) {
218 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
219 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
220 tinfo->_sifields._rt._sigval.sival_ptr =
221 tswapl(info->_sifields._rt._sigval.sival_ptr);
226 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
228 host_to_target_siginfo_noswap(tinfo, info);
229 tswap_siginfo(tinfo, tinfo);
232 /* XXX: we support only POSIX RT signals are used. */
233 /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
234 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
236 info->si_signo = tswap32(tinfo->si_signo);
237 info->si_errno = tswap32(tinfo->si_errno);
238 info->si_code = tswap32(tinfo->si_code);
239 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
240 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
241 info->si_value.sival_ptr =
242 (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
245 void signal_init(void)
247 struct sigaction act;
250 /* generate signal conversion tables */
251 for(i = 1; i <= 64; i++) {
252 if (host_to_target_signal_table[i] == 0)
253 host_to_target_signal_table[i] = i;
255 for(i = 1; i <= 64; i++) {
256 j = host_to_target_signal_table[i];
257 target_to_host_signal_table[j] = i;
260 /* set all host signal handlers. ALL signals are blocked during
261 the handlers to serialize them. */
262 sigfillset(&act.sa_mask);
263 act.sa_flags = SA_SIGINFO;
264 act.sa_sigaction = host_signal_handler;
265 for(i = 1; i < NSIG; i++) {
266 sigaction(i, &act, NULL);
269 memset(sigact_table, 0, sizeof(sigact_table));
271 first_free = &sigqueue_table[0];
272 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
273 sigqueue_table[i].next = &sigqueue_table[i + 1];
274 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
277 /* signal queue handling */
279 static inline struct sigqueue *alloc_sigqueue(void)
281 struct sigqueue *q = first_free;
284 first_free = q->next;
288 static inline void free_sigqueue(struct sigqueue *q)
290 q->next = first_free;
294 /* abort execution with signal */
295 void __attribute((noreturn)) force_sig(int sig)
298 host_sig = target_to_host_signal(sig);
299 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
300 sig, strsignal(host_sig));
305 struct sigaction act;
306 sigemptyset(&act.sa_mask);
307 act.sa_flags = SA_SIGINFO;
308 act.sa_sigaction = SIG_DFL;
309 sigaction(SIGABRT, &act, NULL);
315 /* queue a signal so that it will be send to the virtual CPU as soon
317 int queue_signal(int sig, target_siginfo_t *info)
319 struct emulated_sigaction *k;
320 struct sigqueue *q, **pq;
321 target_ulong handler;
323 #if defined(DEBUG_SIGNAL)
324 fprintf(stderr, "queue_signal: sig=%d\n",
327 k = &sigact_table[sig - 1];
328 handler = k->sa._sa_handler;
329 if (handler == TARGET_SIG_DFL) {
330 /* default handler : ignore some signal. The other are fatal */
331 if (sig != TARGET_SIGCHLD &&
332 sig != TARGET_SIGURG &&
333 sig != TARGET_SIGWINCH) {
336 return 0; /* indicate ignored */
338 } else if (handler == TARGET_SIG_IGN) {
341 } else if (handler == TARGET_SIG_ERR) {
345 if (sig < TARGET_SIGRTMIN) {
346 /* if non real time signal, we queue exactly one signal */
356 q = alloc_sigqueue();
367 /* signal that a new signal is pending */
369 return 1; /* indicates that the signal was queued */
373 #if defined(DEBUG_SIGNAL)
375 static void dump_regs(struct ucontext *uc)
378 "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
379 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
380 "EFL=%08x EIP=%08x\n",
381 uc->uc_mcontext.gregs[EAX],
382 uc->uc_mcontext.gregs[EBX],
383 uc->uc_mcontext.gregs[ECX],
384 uc->uc_mcontext.gregs[EDX],
385 uc->uc_mcontext.gregs[ESI],
386 uc->uc_mcontext.gregs[EDI],
387 uc->uc_mcontext.gregs[EBP],
388 uc->uc_mcontext.gregs[ESP],
389 uc->uc_mcontext.gregs[EFL],
390 uc->uc_mcontext.gregs[EIP]);
393 static void dump_regs(struct ucontext *uc)
400 static void host_signal_handler(int host_signum, siginfo_t *info,
404 target_siginfo_t tinfo;
406 /* the CPU emulator uses some host signals to detect exceptions,
407 we we forward to it some signals */
408 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
409 if (cpu_signal_handler(host_signum, info, puc))
413 /* get target signal number */
414 sig = host_to_target_signal(host_signum);
415 if (sig < 1 || sig > TARGET_NSIG)
417 #if defined(DEBUG_SIGNAL)
418 fprintf(stderr, "qemu: got signal %d\n", sig);
421 host_to_target_siginfo_noswap(&tinfo, info);
422 if (queue_signal(sig, &tinfo) == 1) {
423 /* interrupt the virtual CPU as soon as possible */
424 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
428 int do_sigaction(int sig, const struct target_sigaction *act,
429 struct target_sigaction *oact)
431 struct emulated_sigaction *k;
433 if (sig < 1 || sig > TARGET_NSIG)
435 k = &sigact_table[sig - 1];
436 #if defined(DEBUG_SIGNAL) && 0
437 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
438 sig, (int)act, (int)oact);
441 oact->_sa_handler = tswapl(k->sa._sa_handler);
442 oact->sa_flags = tswapl(k->sa.sa_flags);
443 oact->sa_restorer = tswapl(k->sa.sa_restorer);
444 oact->sa_mask = k->sa.sa_mask;
447 k->sa._sa_handler = tswapl(act->_sa_handler);
448 k->sa.sa_flags = tswapl(act->sa_flags);
449 k->sa.sa_restorer = tswapl(act->sa_restorer);
450 k->sa.sa_mask = act->sa_mask;
455 #define __put_user(x,ptr)\
457 int size = sizeof(*ptr);\
460 stb(ptr, (typeof(*ptr))(x));\
463 stw(ptr, (typeof(*ptr))(x));\
466 stl(ptr, (typeof(*ptr))(x));\
469 stq(ptr, (typeof(*ptr))(x));\
477 #define __get_user(x, ptr) \
479 int size = sizeof(*ptr);\
482 x = (typeof(*ptr))ldub(ptr);\
485 x = (typeof(*ptr))lduw(ptr);\
488 x = (typeof(*ptr))ldl(ptr);\
491 x = (typeof(*ptr))ldq(ptr);\
500 #define __copy_to_user(dst, src, size)\
502 memcpy(dst, src, size);\
506 #define __copy_from_user(dst, src, size)\
508 memcpy(dst, src, size);\
512 #define __clear_user(dst, size)\
514 memset(dst, 0, size);\
519 #define offsetof(type, field) ((size_t) &((type *)0)->field)
522 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
523 const target_siginfo_t *info)
525 tswap_siginfo(tinfo, info);
531 /* from the Linux kernel */
533 struct target_fpreg {
534 uint16_t significand[4];
538 struct target_fpxreg {
539 uint16_t significand[4];
544 struct target_xmmreg {
545 target_ulong element[4];
548 struct target_fpstate {
549 /* Regular FPU environment */
555 target_ulong dataoff;
556 target_ulong datasel;
557 struct target_fpreg _st[8];
559 uint16_t magic; /* 0xffff = regular FPU data only */
561 /* FXSR FPU environment */
562 target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
564 target_ulong reserved;
565 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
566 struct target_xmmreg _xmm[8];
567 target_ulong padding[56];
570 #define X86_FXSR_MAGIC 0x0000
572 struct target_sigcontext {
590 target_ulong esp_at_signal;
592 target_ulong fpstate; /* pointer */
593 target_ulong oldmask;
597 typedef struct target_sigaltstack {
600 target_ulong ss_size;
603 struct target_ucontext {
604 target_ulong uc_flags;
605 target_ulong uc_link;
606 target_stack_t uc_stack;
607 struct target_sigcontext uc_mcontext;
608 target_sigset_t uc_sigmask; /* mask last for extensibility */
613 target_ulong pretcode;
615 struct target_sigcontext sc;
616 struct target_fpstate fpstate;
617 target_ulong extramask[TARGET_NSIG_WORDS-1];
623 target_ulong pretcode;
627 struct target_siginfo info;
628 struct target_ucontext uc;
629 struct target_fpstate fpstate;
634 * Set up a signal frame.
637 /* XXX: save x87 state */
639 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
640 CPUX86State *env, unsigned long mask)
644 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
645 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
646 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
647 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
648 err |= __put_user(env->regs[R_EDI], &sc->edi);
649 err |= __put_user(env->regs[R_ESI], &sc->esi);
650 err |= __put_user(env->regs[R_EBP], &sc->ebp);
651 err |= __put_user(env->regs[R_ESP], &sc->esp);
652 err |= __put_user(env->regs[R_EBX], &sc->ebx);
653 err |= __put_user(env->regs[R_EDX], &sc->edx);
654 err |= __put_user(env->regs[R_ECX], &sc->ecx);
655 err |= __put_user(env->regs[R_EAX], &sc->eax);
656 err |= __put_user(env->exception_index, &sc->trapno);
657 err |= __put_user(env->error_code, &sc->err);
658 err |= __put_user(env->eip, &sc->eip);
659 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
660 err |= __put_user(env->eflags, &sc->eflags);
661 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
662 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
664 cpu_x86_fsave(env, (void *)fpstate, 1);
665 fpstate->status = fpstate->sw;
666 err |= __put_user(0xffff, &fpstate->magic);
667 err |= __put_user(fpstate, &sc->fpstate);
669 /* non-iBCS2 extensions.. */
670 err |= __put_user(mask, &sc->oldmask);
671 err |= __put_user(env->cr[2], &sc->cr2);
676 * Determine which stack to use..
680 get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
684 /* Default to using normal stack */
685 esp = env->regs[R_ESP];
687 /* This is the X/Open sanctioned signal stack switching. */
688 if (ka->sa.sa_flags & SA_ONSTACK) {
689 if (sas_ss_flags(esp) == 0)
690 esp = current->sas_ss_sp + current->sas_ss_size;
693 /* This is the legacy signal stack switching. */
696 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
697 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
698 ka->sa.sa_restorer) {
699 esp = (unsigned long) ka->sa.sa_restorer;
701 return (void *)((esp - frame_size) & -8ul);
704 static void setup_frame(int sig, struct emulated_sigaction *ka,
705 target_sigset_t *set, CPUX86State *env)
707 struct sigframe *frame;
710 frame = get_sigframe(ka, env, sizeof(*frame));
713 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
716 err |= __put_user((/*current->exec_domain
717 && current->exec_domain->signal_invmap
719 ? current->exec_domain->signal_invmap[sig]
725 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
729 if (TARGET_NSIG_WORDS > 1) {
730 err |= __copy_to_user(frame->extramask, &set->sig[1],
731 sizeof(frame->extramask));
736 /* Set up to return from userspace. If provided, use a stub
737 already in userspace. */
738 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
739 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
741 err |= __put_user(frame->retcode, &frame->pretcode);
742 /* This is popl %eax ; movl $,%eax ; int $0x80 */
743 err |= __put_user(0xb858, (short *)(frame->retcode+0));
744 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
745 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
751 /* Set up registers for signal handler */
752 env->regs[R_ESP] = (unsigned long) frame;
753 env->eip = (unsigned long) ka->sa._sa_handler;
755 cpu_x86_load_seg(env, R_DS, __USER_DS);
756 cpu_x86_load_seg(env, R_ES, __USER_DS);
757 cpu_x86_load_seg(env, R_SS, __USER_DS);
758 cpu_x86_load_seg(env, R_CS, __USER_CS);
759 env->eflags &= ~TF_MASK;
764 if (sig == TARGET_SIGSEGV)
765 ka->sa._sa_handler = TARGET_SIG_DFL;
766 force_sig(TARGET_SIGSEGV /* , current */);
769 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
770 target_siginfo_t *info,
771 target_sigset_t *set, CPUX86State *env)
773 struct rt_sigframe *frame;
776 frame = get_sigframe(ka, env, sizeof(*frame));
779 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
783 err |= __put_user((/*current->exec_domain
784 && current->exec_domain->signal_invmap
786 ? current->exec_domain->signal_invmap[sig]
789 err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
790 err |= __put_user((target_ulong)&frame->uc, &frame->puc);
791 err |= copy_siginfo_to_user(&frame->info, info);
795 /* Create the ucontext. */
796 err |= __put_user(0, &frame->uc.uc_flags);
797 err |= __put_user(0, &frame->uc.uc_link);
798 err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp);
799 err |= __put_user(/* sas_ss_flags(regs->esp) */ 0,
800 &frame->uc.uc_stack.ss_flags);
801 err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size);
802 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
804 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
808 /* Set up to return from userspace. If provided, use a stub
809 already in userspace. */
810 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
811 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
813 err |= __put_user(frame->retcode, &frame->pretcode);
814 /* This is movl $,%eax ; int $0x80 */
815 err |= __put_user(0xb8, (char *)(frame->retcode+0));
816 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
817 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
823 /* Set up registers for signal handler */
824 env->regs[R_ESP] = (unsigned long) frame;
825 env->eip = (unsigned long) ka->sa._sa_handler;
827 cpu_x86_load_seg(env, R_DS, __USER_DS);
828 cpu_x86_load_seg(env, R_ES, __USER_DS);
829 cpu_x86_load_seg(env, R_SS, __USER_DS);
830 cpu_x86_load_seg(env, R_CS, __USER_CS);
831 env->eflags &= ~TF_MASK;
836 if (sig == TARGET_SIGSEGV)
837 ka->sa._sa_handler = TARGET_SIG_DFL;
838 force_sig(TARGET_SIGSEGV /* , current */);
842 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
844 unsigned int err = 0;
846 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
847 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
848 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
849 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
851 env->regs[R_EDI] = ldl(&sc->edi);
852 env->regs[R_ESI] = ldl(&sc->esi);
853 env->regs[R_EBP] = ldl(&sc->ebp);
854 env->regs[R_ESP] = ldl(&sc->esp);
855 env->regs[R_EBX] = ldl(&sc->ebx);
856 env->regs[R_EDX] = ldl(&sc->edx);
857 env->regs[R_ECX] = ldl(&sc->ecx);
858 env->eip = ldl(&sc->eip);
860 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
861 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
864 unsigned int tmpflags;
865 tmpflags = ldl(&sc->eflags);
866 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
867 // regs->orig_eax = -1; /* disable syscall checks */
871 struct _fpstate * buf;
872 buf = (void *)ldl(&sc->fpstate);
875 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
878 cpu_x86_frstor(env, (void *)buf, 1);
882 *peax = ldl(&sc->eax);
890 long do_sigreturn(CPUX86State *env)
892 struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8);
893 target_sigset_t target_set;
897 #if defined(DEBUG_SIGNAL)
898 fprintf(stderr, "do_sigreturn\n");
900 /* set blocked signals */
901 target_set.sig[0] = frame->sc.oldmask;
902 for(i = 1; i < TARGET_NSIG_WORDS; i++)
903 target_set.sig[i] = frame->extramask[i - 1];
905 target_to_host_sigset(&set, &target_set);
906 sigprocmask(SIG_SETMASK, &set, NULL);
908 /* restore registers */
909 if (restore_sigcontext(env, &frame->sc, &eax))
914 force_sig(TARGET_SIGSEGV);
918 long do_rt_sigreturn(CPUX86State *env)
920 struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
921 target_sigset_t target_set;
927 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
930 memcpy(&target_set, &frame->uc.uc_sigmask, sizeof(target_sigset_t));
932 target_to_host_sigset(&set, &target_set);
933 sigprocmask(SIG_SETMASK, &set, NULL);
935 if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax))
939 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
941 /* It is more difficult to avoid calling this function than to
942 call it and ignore errors. */
943 do_sigaltstack(&st, NULL, regs->esp);
948 force_sig(TARGET_SIGSEGV);
952 #elif defined(TARGET_ARM)
954 struct target_sigcontext {
955 target_ulong trap_no;
956 target_ulong error_code;
957 target_ulong oldmask;
968 target_ulong arm_r10;
974 target_ulong arm_cpsr;
975 target_ulong fault_address;
978 typedef struct target_sigaltstack {
981 target_ulong ss_size;
984 struct target_ucontext {
985 target_ulong uc_flags;
986 target_ulong uc_link;
987 target_stack_t uc_stack;
988 struct target_sigcontext uc_mcontext;
989 target_sigset_t uc_sigmask; /* mask last for extensibility */
994 struct target_sigcontext sc;
995 target_ulong extramask[TARGET_NSIG_WORDS-1];
996 target_ulong retcode;
1001 struct target_siginfo *pinfo;
1003 struct target_siginfo info;
1004 struct target_ucontext uc;
1005 target_ulong retcode;
1008 #define TARGET_CONFIG_CPU_32 1
1011 * For ARM syscalls, we encode the syscall number into the instruction.
1013 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1014 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1017 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1018 * need two 16-bit instructions.
1020 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1021 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1023 static const target_ulong retcodes[4] = {
1024 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1025 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1029 #define __put_user_error(x,p,e) __put_user(x, p)
1030 #define __get_user_error(x,p,e) __get_user(x, p)
1032 static inline int valid_user_regs(CPUState *regs)
1038 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1039 CPUState *env, unsigned long mask)
1043 __put_user_error(env->regs[0], &sc->arm_r0, err);
1044 __put_user_error(env->regs[1], &sc->arm_r1, err);
1045 __put_user_error(env->regs[2], &sc->arm_r2, err);
1046 __put_user_error(env->regs[3], &sc->arm_r3, err);
1047 __put_user_error(env->regs[4], &sc->arm_r4, err);
1048 __put_user_error(env->regs[5], &sc->arm_r5, err);
1049 __put_user_error(env->regs[6], &sc->arm_r6, err);
1050 __put_user_error(env->regs[7], &sc->arm_r7, err);
1051 __put_user_error(env->regs[8], &sc->arm_r8, err);
1052 __put_user_error(env->regs[9], &sc->arm_r9, err);
1053 __put_user_error(env->regs[10], &sc->arm_r10, err);
1054 __put_user_error(env->regs[11], &sc->arm_fp, err);
1055 __put_user_error(env->regs[12], &sc->arm_ip, err);
1056 __put_user_error(env->regs[13], &sc->arm_sp, err);
1057 __put_user_error(env->regs[14], &sc->arm_lr, err);
1058 __put_user_error(env->regs[15], &sc->arm_pc, err);
1059 #ifdef TARGET_CONFIG_CPU_32
1060 __put_user_error(env->cpsr, &sc->arm_cpsr, err);
1063 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1064 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1065 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1066 __put_user_error(mask, &sc->oldmask, err);
1071 static inline void *
1072 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1074 unsigned long sp = regs->regs[13];
1078 * This is the X/Open sanctioned signal stack switching.
1080 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
1081 sp = current->sas_ss_sp + current->sas_ss_size;
1084 * ATPCS B01 mandates 8-byte alignment
1086 return (void *)((sp - framesize) & ~7);
1090 setup_return(CPUState *env, struct emulated_sigaction *ka,
1091 target_ulong *rc, void *frame, int usig)
1093 target_ulong handler = (target_ulong)ka->sa._sa_handler;
1094 target_ulong retcode;
1096 #if defined(TARGET_CONFIG_CPU_32)
1097 target_ulong cpsr = env->cpsr;
1101 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1103 if (ka->sa.sa_flags & SA_THIRTYTWO)
1104 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1106 #ifdef CONFIG_ARM_THUMB
1107 if (elf_hwcap & HWCAP_THUMB) {
1109 * The LSB of the handler determines if we're going to
1110 * be using THUMB or ARM mode for this signal handler.
1112 thumb = handler & 1;
1121 #endif /* TARGET_CONFIG_CPU_32 */
1123 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1124 retcode = (target_ulong)ka->sa.sa_restorer;
1126 unsigned int idx = thumb;
1128 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1131 if (__put_user(retcodes[idx], rc))
1134 flush_icache_range((target_ulong)rc,
1135 (target_ulong)(rc + 1));
1137 retcode = ((target_ulong)rc) + thumb;
1140 env->regs[0] = usig;
1141 env->regs[13] = (target_ulong)frame;
1142 env->regs[14] = retcode;
1143 env->regs[15] = handler & (thumb ? ~1 : ~3);
1145 #ifdef TARGET_CONFIG_CPU_32
1152 static void setup_frame(int usig, struct emulated_sigaction *ka,
1153 target_sigset_t *set, CPUState *regs)
1155 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
1158 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1160 if (TARGET_NSIG_WORDS > 1) {
1161 err |= __copy_to_user(frame->extramask, &set->sig[1],
1162 sizeof(frame->extramask));
1166 err = setup_return(regs, ka, &frame->retcode, frame, usig);
1170 static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1171 target_siginfo_t *info,
1172 target_sigset_t *set, CPUState *env)
1174 struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame));
1178 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
1181 __put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err);
1182 __put_user_error(&frame->uc, (target_ulong *)&frame->puc, err);
1183 err |= copy_siginfo_to_user(&frame->info, info);
1185 /* Clear all the bits of the ucontext we don't use. */
1186 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
1188 err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
1190 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
1193 err = setup_return(env, ka, &frame->retcode, frame, usig);
1197 * For realtime signals we must also set the second and third
1198 * arguments for the signal handler.
1199 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1201 env->regs[1] = (target_ulong)frame->pinfo;
1202 env->regs[2] = (target_ulong)frame->puc;
1209 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1213 __get_user_error(env->regs[0], &sc->arm_r0, err);
1214 __get_user_error(env->regs[1], &sc->arm_r1, err);
1215 __get_user_error(env->regs[2], &sc->arm_r2, err);
1216 __get_user_error(env->regs[3], &sc->arm_r3, err);
1217 __get_user_error(env->regs[4], &sc->arm_r4, err);
1218 __get_user_error(env->regs[5], &sc->arm_r5, err);
1219 __get_user_error(env->regs[6], &sc->arm_r6, err);
1220 __get_user_error(env->regs[7], &sc->arm_r7, err);
1221 __get_user_error(env->regs[8], &sc->arm_r8, err);
1222 __get_user_error(env->regs[9], &sc->arm_r9, err);
1223 __get_user_error(env->regs[10], &sc->arm_r10, err);
1224 __get_user_error(env->regs[11], &sc->arm_fp, err);
1225 __get_user_error(env->regs[12], &sc->arm_ip, err);
1226 __get_user_error(env->regs[13], &sc->arm_sp, err);
1227 __get_user_error(env->regs[14], &sc->arm_lr, err);
1228 __get_user_error(env->regs[15], &sc->arm_pc, err);
1229 #ifdef TARGET_CONFIG_CPU_32
1230 __get_user_error(env->cpsr, &sc->arm_cpsr, err);
1233 err |= !valid_user_regs(env);
1238 long do_sigreturn(CPUState *env)
1240 struct sigframe *frame;
1241 target_sigset_t set;
1245 * Since we stacked the signal on a 64-bit boundary,
1246 * then 'sp' should be word aligned here. If it's
1247 * not, then the user is trying to mess with us.
1249 if (env->regs[13] & 7)
1252 frame = (struct sigframe *)env->regs[13];
1255 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1258 if (__get_user(set.sig[0], &frame->sc.oldmask)
1259 || (TARGET_NSIG_WORDS > 1
1260 && __copy_from_user(&set.sig[1], &frame->extramask,
1261 sizeof(frame->extramask))))
1264 target_to_host_sigset(&host_set, &set);
1265 sigprocmask(SIG_SETMASK, &host_set, NULL);
1267 if (restore_sigcontext(env, &frame->sc))
1271 /* Send SIGTRAP if we're single-stepping */
1272 if (ptrace_cancel_bpt(current))
1273 send_sig(SIGTRAP, current, 1);
1275 return env->regs[0];
1278 force_sig(SIGSEGV /* , current */);
1282 long do_rt_sigreturn(CPUState *env)
1284 struct rt_sigframe *frame;
1285 target_sigset_t set;
1289 * Since we stacked the signal on a 64-bit boundary,
1290 * then 'sp' should be word aligned here. If it's
1291 * not, then the user is trying to mess with us.
1293 if (env->regs[13] & 7)
1296 frame = (struct rt_sigframe *)env->regs[13];
1299 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1302 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
1305 target_to_host_sigset(&host_set, &set);
1306 sigprocmask(SIG_SETMASK, &host_set, NULL);
1308 if (restore_sigcontext(env, &frame->uc.uc_mcontext))
1312 /* Send SIGTRAP if we're single-stepping */
1313 if (ptrace_cancel_bpt(current))
1314 send_sig(SIGTRAP, current, 1);
1316 return env->regs[0];
1319 force_sig(SIGSEGV /* , current */);
1325 static void setup_frame(int sig, struct emulated_sigaction *ka,
1326 target_sigset_t *set, CPUState *env)
1328 fprintf(stderr, "setup_frame: not implemented\n");
1331 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1332 target_siginfo_t *info,
1333 target_sigset_t *set, CPUState *env)
1335 fprintf(stderr, "setup_rt_frame: not implemented\n");
1338 long do_sigreturn(CPUState *env)
1340 fprintf(stderr, "do_sigreturn: not implemented\n");
1344 long do_rt_sigreturn(CPUState *env)
1346 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1352 void process_pending_signals(void *cpu_env)
1355 target_ulong handler;
1356 sigset_t set, old_set;
1357 target_sigset_t target_old_set;
1358 struct emulated_sigaction *k;
1361 if (!signal_pending)
1365 for(sig = 1; sig <= TARGET_NSIG; sig++) {
1370 /* if no signal is pending, just return */
1376 fprintf(stderr, "qemu: process signal %d\n", sig);
1378 /* dequeue signal */
1384 handler = k->sa._sa_handler;
1385 if (handler == TARGET_SIG_DFL) {
1386 /* default handler : ignore some signal. The other are fatal */
1387 if (sig != TARGET_SIGCHLD &&
1388 sig != TARGET_SIGURG &&
1389 sig != TARGET_SIGWINCH) {
1392 } else if (handler == TARGET_SIG_IGN) {
1394 } else if (handler == TARGET_SIG_ERR) {
1397 /* compute the blocked signals during the handler execution */
1398 target_to_host_sigset(&set, &k->sa.sa_mask);
1399 /* SA_NODEFER indicates that the current signal should not be
1400 blocked during the handler */
1401 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
1402 sigaddset(&set, target_to_host_signal(sig));
1404 /* block signals in the handler using Linux */
1405 sigprocmask(SIG_BLOCK, &set, &old_set);
1406 /* save the previous blocked signal state to restore it at the
1407 end of the signal execution (see do_sigreturn) */
1408 host_to_target_sigset(&target_old_set, &old_set);
1410 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1413 CPUX86State *env = cpu_env;
1414 if (env->eflags & VM_MASK)
1415 save_v86_state(env);
1418 /* prepare the stack frame of the virtual CPU */
1419 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
1420 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
1422 setup_frame(sig, k, &target_old_set, cpu_env);
1423 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
1424 k->sa._sa_handler = TARGET_SIG_DFL;