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 /* XXX: do it properly */
64 static inline int host_to_target_signal(int sig
)
69 static inline int target_to_host_signal(int sig
)
74 void host_to_target_sigset(target_sigset_t
*d
, sigset_t
*s
)
77 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++) {
78 d
->sig
[i
] = tswapl(((unsigned long *)s
)[i
]);
82 void target_to_host_sigset(sigset_t
*d
, target_sigset_t
*s
)
85 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++) {
86 ((unsigned long *)d
)[i
] = tswapl(s
->sig
[i
]);
90 void host_to_target_old_sigset(target_ulong
*old_sigset
,
91 const sigset_t
*sigset
)
93 *old_sigset
= tswap32(*(unsigned long *)sigset
& 0xffffffff);
96 void target_to_host_old_sigset(sigset_t
*sigset
,
97 const target_ulong
*old_sigset
)
100 *(unsigned long *)sigset
= tswapl(*old_sigset
);
103 /* siginfo conversion */
105 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
106 const siginfo_t
*info
)
109 sig
= host_to_target_signal(info
->si_signo
);
110 tinfo
->si_signo
= sig
;
113 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
114 sig
== SIGBUS
|| sig
== SIGTRAP
) {
115 /* should never come here, but who knows. The information for
116 the target is irrelevant */
117 tinfo
->_sifields
._sigfault
._addr
= 0;
118 } else if (sig
>= TARGET_SIGRTMIN
) {
119 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
120 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
121 /* XXX: potential problem if 64 bit */
122 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
123 (target_ulong
)info
->si_value
.sival_ptr
;
127 static void tswap_siginfo(target_siginfo_t
*tinfo
,
128 const target_siginfo_t
*info
)
131 sig
= info
->si_signo
;
132 tinfo
->si_signo
= tswap32(sig
);
133 tinfo
->si_errno
= tswap32(info
->si_errno
);
134 tinfo
->si_code
= tswap32(info
->si_code
);
135 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
136 sig
== SIGBUS
|| sig
== SIGTRAP
) {
137 tinfo
->_sifields
._sigfault
._addr
=
138 tswapl(info
->_sifields
._sigfault
._addr
);
139 } else if (sig
>= TARGET_SIGRTMIN
) {
140 tinfo
->_sifields
._rt
._pid
= tswap32(info
->_sifields
._rt
._pid
);
141 tinfo
->_sifields
._rt
._uid
= tswap32(info
->_sifields
._rt
._uid
);
142 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
143 tswapl(info
->_sifields
._rt
._sigval
.sival_ptr
);
148 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
150 host_to_target_siginfo_noswap(tinfo
, info
);
151 tswap_siginfo(tinfo
, tinfo
);
154 /* XXX: we support only POSIX RT signals are used. */
155 /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
156 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
158 info
->si_signo
= tswap32(tinfo
->si_signo
);
159 info
->si_errno
= tswap32(tinfo
->si_errno
);
160 info
->si_code
= tswap32(tinfo
->si_code
);
161 info
->si_pid
= tswap32(tinfo
->_sifields
._rt
._pid
);
162 info
->si_uid
= tswap32(tinfo
->_sifields
._rt
._uid
);
163 info
->si_value
.sival_ptr
=
164 (void *)tswapl(tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
167 void signal_init(void)
169 struct sigaction act
;
172 /* set all host signal handlers. ALL signals are blocked during
173 the handlers to serialize them. */
174 sigfillset(&act
.sa_mask
);
175 act
.sa_flags
= SA_SIGINFO
;
176 act
.sa_sigaction
= host_signal_handler
;
177 for(i
= 1; i
< NSIG
; i
++) {
178 sigaction(i
, &act
, NULL
);
181 memset(sigact_table
, 0, sizeof(sigact_table
));
183 first_free
= &sigqueue_table
[0];
184 for(i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++)
185 sigqueue_table
[i
].next
= &sigqueue_table
[i
+ 1];
186 sigqueue_table
[MAX_SIGQUEUE_SIZE
- 1].next
= NULL
;
189 /* signal queue handling */
191 static inline struct sigqueue
*alloc_sigqueue(void)
193 struct sigqueue
*q
= first_free
;
196 first_free
= q
->next
;
200 static inline void free_sigqueue(struct sigqueue
*q
)
202 q
->next
= first_free
;
206 /* abort execution with signal */
207 void __attribute((noreturn
)) force_sig(int sig
)
210 host_sig
= target_to_host_signal(sig
);
211 fprintf(stderr
, "qemu: uncaught target signal %d (%s) - exiting\n",
212 sig
, strsignal(host_sig
));
217 struct sigaction act
;
218 sigemptyset(&act
.sa_mask
);
219 act
.sa_flags
= SA_SIGINFO
;
220 act
.sa_sigaction
= SIG_DFL
;
221 sigaction(SIGABRT
, &act
, NULL
);
227 /* queue a signal so that it will be send to the virtual CPU as soon
229 int queue_signal(int sig
, target_siginfo_t
*info
)
231 struct emulated_sigaction
*k
;
232 struct sigqueue
*q
, **pq
;
233 target_ulong handler
;
235 #if defined(DEBUG_SIGNAL)
236 fprintf(stderr
, "queue_signal: sig=%d\n",
239 k
= &sigact_table
[sig
- 1];
240 handler
= k
->sa
._sa_handler
;
241 if (handler
== TARGET_SIG_DFL
) {
242 /* default handler : ignore some signal. The other are fatal */
243 if (sig
!= TARGET_SIGCHLD
&&
244 sig
!= TARGET_SIGURG
&&
245 sig
!= TARGET_SIGWINCH
) {
248 return 0; /* indicate ignored */
250 } else if (handler
== TARGET_SIG_IGN
) {
253 } else if (handler
== TARGET_SIG_ERR
) {
257 if (sig
< TARGET_SIGRTMIN
) {
258 /* if non real time signal, we queue exactly one signal */
268 q
= alloc_sigqueue();
279 /* signal that a new signal is pending */
281 return 1; /* indicates that the signal was queued */
285 #if defined(DEBUG_SIGNAL)
287 static void dump_regs(struct ucontext
*uc
)
290 "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
291 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
292 "EFL=%08x EIP=%08x\n",
293 uc
->uc_mcontext
.gregs
[EAX
],
294 uc
->uc_mcontext
.gregs
[EBX
],
295 uc
->uc_mcontext
.gregs
[ECX
],
296 uc
->uc_mcontext
.gregs
[EDX
],
297 uc
->uc_mcontext
.gregs
[ESI
],
298 uc
->uc_mcontext
.gregs
[EDI
],
299 uc
->uc_mcontext
.gregs
[EBP
],
300 uc
->uc_mcontext
.gregs
[ESP
],
301 uc
->uc_mcontext
.gregs
[EFL
],
302 uc
->uc_mcontext
.gregs
[EIP
]);
305 static void dump_regs(struct ucontext
*uc
)
312 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
316 target_siginfo_t tinfo
;
318 /* the CPU emulator uses some host signals to detect exceptions,
319 we we forward to it some signals */
320 if (host_signum
== SIGSEGV
|| host_signum
== SIGBUS
) {
321 if (cpu_x86_signal_handler(host_signum
, info
, puc
))
325 /* get target signal number */
326 sig
= host_to_target_signal(host_signum
);
327 if (sig
< 1 || sig
> TARGET_NSIG
)
329 #if defined(DEBUG_SIGNAL)
330 fprintf(stderr
, "qemu: got signal %d\n", sig
);
333 host_to_target_siginfo_noswap(&tinfo
, info
);
334 if (queue_signal(sig
, &tinfo
) == 1) {
335 /* interrupt the virtual CPU as soon as possible */
336 cpu_x86_interrupt(global_env
);
340 int do_sigaction(int sig
, const struct target_sigaction
*act
,
341 struct target_sigaction
*oact
)
343 struct emulated_sigaction
*k
;
345 if (sig
< 1 || sig
> TARGET_NSIG
)
347 k
= &sigact_table
[sig
- 1];
348 #if defined(DEBUG_SIGNAL) && 0
349 fprintf(stderr
, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
350 sig
, (int)act
, (int)oact
);
353 oact
->_sa_handler
= tswapl(k
->sa
._sa_handler
);
354 oact
->sa_flags
= tswapl(k
->sa
.sa_flags
);
355 oact
->sa_restorer
= tswapl(k
->sa
.sa_restorer
);
356 oact
->sa_mask
= k
->sa
.sa_mask
;
359 k
->sa
._sa_handler
= tswapl(act
->_sa_handler
);
360 k
->sa
.sa_flags
= tswapl(act
->sa_flags
);
361 k
->sa
.sa_restorer
= tswapl(act
->sa_restorer
);
362 k
->sa
.sa_mask
= act
->sa_mask
;
369 /* from the Linux kernel */
371 struct target_fpreg
{
372 uint16_t significand
[4];
376 struct target_fpxreg
{
377 uint16_t significand
[4];
382 struct target_xmmreg
{
383 target_ulong element
[4];
386 struct target_fpstate
{
387 /* Regular FPU environment */
393 target_ulong dataoff
;
394 target_ulong datasel
;
395 struct target_fpreg _st
[8];
397 uint16_t magic
; /* 0xffff = regular FPU data only */
399 /* FXSR FPU environment */
400 target_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
402 target_ulong reserved
;
403 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
404 struct target_xmmreg _xmm
[8];
405 target_ulong padding
[56];
408 #define X86_FXSR_MAGIC 0x0000
410 struct target_sigcontext
{
428 target_ulong esp_at_signal
;
430 target_ulong fpstate
; /* pointer */
431 target_ulong oldmask
;
435 typedef struct target_sigaltstack
{
438 target_ulong ss_size
;
441 struct target_ucontext
{
442 target_ulong uc_flags
;
443 target_ulong uc_link
;
444 target_stack_t uc_stack
;
445 struct target_sigcontext uc_mcontext
;
446 target_sigset_t uc_sigmask
; /* mask last for extensibility */
451 target_ulong pretcode
;
453 struct target_sigcontext sc
;
454 struct target_fpstate fpstate
;
455 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
461 target_ulong pretcode
;
465 struct target_siginfo info
;
466 struct target_ucontext uc
;
467 struct target_fpstate fpstate
;
472 * Set up a signal frame.
475 #define __put_user(x,ptr)\
477 int size = sizeof(*ptr);\
480 stb(ptr, (typeof(*ptr))(x));\
483 stw(ptr, (typeof(*ptr))(x));\
486 stl(ptr, (typeof(*ptr))(x));\
489 stq(ptr, (typeof(*ptr))(x));\
497 #define get_user(val, ptr) (typeof(*ptr))(*(ptr))
500 #define __copy_to_user(dst, src, size)\
502 memcpy(dst, src, size);\
506 static inline int copy_siginfo_to_user(target_siginfo_t
*tinfo
,
507 const target_siginfo_t
*info
)
509 tswap_siginfo(tinfo
, info
);
513 /* XXX: save x87 state */
515 setup_sigcontext(struct target_sigcontext
*sc
, struct target_fpstate
*fpstate
,
516 CPUX86State
*env
, unsigned long mask
)
520 err
|= __put_user(env
->segs
[R_GS
], (unsigned int *)&sc
->gs
);
521 err
|= __put_user(env
->segs
[R_FS
], (unsigned int *)&sc
->fs
);
522 err
|= __put_user(env
->segs
[R_ES
], (unsigned int *)&sc
->es
);
523 err
|= __put_user(env
->segs
[R_DS
], (unsigned int *)&sc
->ds
);
524 err
|= __put_user(env
->regs
[R_EDI
], &sc
->edi
);
525 err
|= __put_user(env
->regs
[R_ESI
], &sc
->esi
);
526 err
|= __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
527 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp
);
528 err
|= __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
529 err
|= __put_user(env
->regs
[R_EDX
], &sc
->edx
);
530 err
|= __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
531 err
|= __put_user(env
->regs
[R_EAX
], &sc
->eax
);
532 err
|= __put_user(env
->exception_index
, &sc
->trapno
);
533 err
|= __put_user(env
->error_code
, &sc
->err
);
534 err
|= __put_user(env
->eip
, &sc
->eip
);
535 err
|= __put_user(env
->segs
[R_CS
], (unsigned int *)&sc
->cs
);
536 err
|= __put_user(env
->eflags
, &sc
->eflags
);
537 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
538 err
|= __put_user(env
->segs
[R_SS
], (unsigned int *)&sc
->ss
);
540 tmp
= save_i387(fpstate
);
544 err
|= __put_user(tmp
? fpstate
: NULL
, &sc
->fpstate
);
546 err
|= __put_user(0, &sc
->fpstate
);
548 /* non-iBCS2 extensions.. */
549 err
|= __put_user(mask
, &sc
->oldmask
);
550 err
|= __put_user(/*current->thread.cr2*/ 0, &sc
->cr2
);
555 * Determine which stack to use..
559 get_sigframe(struct emulated_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
563 /* Default to using normal stack */
564 esp
= env
->regs
[R_ESP
];
566 /* This is the X/Open sanctioned signal stack switching. */
567 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
568 if (sas_ss_flags(esp
) == 0)
569 esp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
572 /* This is the legacy signal stack switching. */
573 else if ((regs
->xss
& 0xffff) != __USER_DS
&&
574 !(ka
->sa
.sa_flags
& SA_RESTORER
) &&
575 ka
->sa
.sa_restorer
) {
576 esp
= (unsigned long) ka
->sa
.sa_restorer
;
579 return (void *)((esp
- frame_size
) & -8ul);
582 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
583 target_sigset_t
*set
, CPUX86State
*env
)
585 struct sigframe
*frame
;
588 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
591 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
594 err
|= __put_user((/*current->exec_domain
595 && current->exec_domain->signal_invmap
597 ? current->exec_domain->signal_invmap[sig]
603 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0]);
607 if (TARGET_NSIG_WORDS
> 1) {
608 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
609 sizeof(frame
->extramask
));
614 /* Set up to return from userspace. If provided, use a stub
615 already in userspace. */
616 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
617 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
619 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
620 /* This is popl %eax ; movl $,%eax ; int $0x80 */
621 err
|= __put_user(0xb858, (short *)(frame
->retcode
+0));
622 err
|= __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
623 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+6));
629 /* Set up registers for signal handler */
630 env
->regs
[R_ESP
] = (unsigned long) frame
;
631 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
633 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
634 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
635 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
636 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
637 env
->eflags
&= ~TF_MASK
;
642 if (sig
== TARGET_SIGSEGV
)
643 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
644 force_sig(TARGET_SIGSEGV
/* , current */);
647 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
648 target_siginfo_t
*info
,
649 target_sigset_t
*set
, CPUX86State
*env
)
651 struct rt_sigframe
*frame
;
654 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
657 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
661 err
|= __put_user((/*current->exec_domain
662 && current->exec_domain->signal_invmap
664 ? current->exec_domain->signal_invmap[sig]
667 err
|= __put_user((target_ulong
)&frame
->info
, &frame
->pinfo
);
668 err
|= __put_user((target_ulong
)&frame
->uc
, &frame
->puc
);
669 err
|= copy_siginfo_to_user(&frame
->info
, info
);
673 /* Create the ucontext. */
674 err
|= __put_user(0, &frame
->uc
.uc_flags
);
675 err
|= __put_user(0, &frame
->uc
.uc_link
);
676 err
|= __put_user(/*current->sas_ss_sp*/ 0, &frame
->uc
.uc_stack
.ss_sp
);
677 err
|= __put_user(/* sas_ss_flags(regs->esp) */ 0,
678 &frame
->uc
.uc_stack
.ss_flags
);
679 err
|= __put_user(/* current->sas_ss_size */ 0, &frame
->uc
.uc_stack
.ss_size
);
680 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, &frame
->fpstate
,
682 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
686 /* Set up to return from userspace. If provided, use a stub
687 already in userspace. */
688 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
689 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
691 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
692 /* This is movl $,%eax ; int $0x80 */
693 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
694 err
|= __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
695 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+5));
701 /* Set up registers for signal handler */
702 env
->regs
[R_ESP
] = (unsigned long) frame
;
703 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
705 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
706 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
707 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
708 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
709 env
->eflags
&= ~TF_MASK
;
714 if (sig
== TARGET_SIGSEGV
)
715 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
716 force_sig(TARGET_SIGSEGV
/* , current */);
720 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
, int *peax
)
722 unsigned int err
= 0;
726 #define COPY(x) err |= __get_user(regs->x, &sc->x)
728 #define COPY_SEG(seg) \
729 { unsigned short tmp; \
730 err |= __get_user(tmp, &sc->seg); \
731 regs->x##seg = tmp; }
733 #define COPY_SEG_STRICT(seg) \
734 { unsigned short tmp; \
735 err |= __get_user(tmp, &sc->seg); \
736 regs->x##seg = tmp|3; }
738 #define GET_SEG(seg) \
739 { unsigned short tmp; \
740 err |= __get_user(tmp, &sc->seg); \
741 loadsegment(seg,tmp); }
743 cpu_x86_load_seg(env
, R_GS
, lduw(&sc
->gs
));
744 cpu_x86_load_seg(env
, R_FS
, lduw(&sc
->fs
));
745 cpu_x86_load_seg(env
, R_ES
, lduw(&sc
->es
));
746 cpu_x86_load_seg(env
, R_DS
, lduw(&sc
->ds
));
748 env
->regs
[R_EDI
] = ldl(&sc
->edi
);
749 env
->regs
[R_ESI
] = ldl(&sc
->esi
);
750 env
->regs
[R_EBP
] = ldl(&sc
->ebp
);
751 env
->regs
[R_ESP
] = ldl(&sc
->esp
);
752 env
->regs
[R_EBX
] = ldl(&sc
->ebx
);
753 env
->regs
[R_EDX
] = ldl(&sc
->edx
);
754 env
->regs
[R_ECX
] = ldl(&sc
->ecx
);
755 env
->eip
= ldl(&sc
->eip
);
757 cpu_x86_load_seg(env
, R_CS
, lduw(&sc
->cs
) | 3);
758 cpu_x86_load_seg(env
, R_SS
, lduw(&sc
->ss
) | 3);
761 unsigned int tmpflags
;
762 tmpflags
= ldl(&sc
->eflags
);
763 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
764 // regs->orig_eax = -1; /* disable syscall checks */
769 struct _fpstate
* buf
;
770 err
|= __get_user(buf
, &sc
->fpstate
);
772 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
774 err
|= restore_i387(buf
);
778 *peax
= ldl(&sc
->eax
);
786 long do_sigreturn(CPUX86State
*env
)
788 struct sigframe
*frame
= (struct sigframe
*)(env
->regs
[R_ESP
] - 8);
789 target_sigset_t target_set
;
793 #if defined(DEBUG_SIGNAL)
794 fprintf(stderr
, "do_sigreturn\n");
796 /* set blocked signals */
797 target_set
.sig
[0] = frame
->sc
.oldmask
;
798 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++)
799 target_set
.sig
[i
] = frame
->extramask
[i
- 1];
801 target_to_host_sigset(&set
, &target_set
);
802 sigprocmask(SIG_SETMASK
, &set
, NULL
);
804 /* restore registers */
805 if (restore_sigcontext(env
, &frame
->sc
, &eax
))
810 force_sig(TARGET_SIGSEGV
);
814 long do_rt_sigreturn(CPUX86State
*env
)
816 struct rt_sigframe
*frame
= (struct rt_sigframe
*)(env
->regs
[R_ESP
] - 4);
817 target_sigset_t target_set
;
823 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
826 memcpy(&target_set
, &frame
->uc
.uc_sigmask
, sizeof(target_sigset_t
));
828 target_to_host_sigset(&set
, &target_set
);
829 sigprocmask(SIG_SETMASK
, &set
, NULL
);
831 if (restore_sigcontext(env
, &frame
->uc
.uc_mcontext
, &eax
))
835 if (__copy_from_user(&st
, &frame
->uc
.uc_stack
, sizeof(st
)))
837 /* It is more difficult to avoid calling this function than to
838 call it and ignore errors. */
839 do_sigaltstack(&st
, NULL
, regs
->esp
);
844 force_sig(TARGET_SIGSEGV
);
850 void process_pending_signals(void *cpu_env
)
853 target_ulong handler
;
854 sigset_t set
, old_set
;
855 target_sigset_t target_old_set
;
856 struct emulated_sigaction
*k
;
863 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
868 /* if no signal is pending, just return */
874 fprintf(stderr
, "qemu: process signal %d\n", sig
);
882 handler
= k
->sa
._sa_handler
;
883 if (handler
== TARGET_SIG_DFL
) {
884 /* default handler : ignore some signal. The other are fatal */
885 if (sig
!= TARGET_SIGCHLD
&&
886 sig
!= TARGET_SIGURG
&&
887 sig
!= TARGET_SIGWINCH
) {
890 } else if (handler
== TARGET_SIG_IGN
) {
892 } else if (handler
== TARGET_SIG_ERR
) {
895 /* compute the blocked signals during the handler execution */
896 target_to_host_sigset(&set
, &k
->sa
.sa_mask
);
897 /* SA_NODEFER indicates that the current signal should not be
898 blocked during the handler */
899 if (!(k
->sa
.sa_flags
& TARGET_SA_NODEFER
))
900 sigaddset(&set
, target_to_host_signal(sig
));
902 /* block signals in the handler using Linux */
903 sigprocmask(SIG_BLOCK
, &set
, &old_set
);
904 /* save the previous blocked signal state to restore it at the
905 end of the signal execution (see do_sigreturn) */
906 host_to_target_sigset(&target_old_set
, &old_set
);
908 /* if the CPU is in VM86 mode, we restore the 32 bit values */
911 CPUX86State
*env
= cpu_env
;
912 if (env
->eflags
& VM_MASK
)
916 /* prepare the stack frame of the virtual CPU */
917 if (k
->sa
.sa_flags
& TARGET_SA_SIGINFO
)
918 setup_rt_frame(sig
, k
, &q
->info
, &target_old_set
, cpu_env
);
920 setup_frame(sig
, k
, &target_old_set
, cpu_env
);
921 if (k
->sa
.sa_flags
& TARGET_SA_RESETHAND
)
922 k
->sa
._sa_handler
= TARGET_SIG_DFL
;