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 static void host_to_target_sigset_internal(target_sigset_t
*d
,
116 unsigned long sigmask
;
117 uint32_t target_sigmask
;
119 sigmask
= ((unsigned long *)s
)[0];
121 for(i
= 0; i
< 32; i
++) {
122 if (sigmask
& (1 << i
))
123 target_sigmask
|= 1 << (host_to_target_signal(i
+ 1) - 1);
125 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
126 d
->sig
[0] = target_sigmask
;
127 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++) {
128 d
->sig
[i
] = ((unsigned long *)s
)[i
];
130 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
131 d
->sig
[0] = target_sigmask
;
132 d
->sig
[1] = sigmask
>> 32;
134 #error host_to_target_sigset
138 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
143 host_to_target_sigset_internal(&d1
, s
);
144 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
145 __put_user(d1
.sig
[i
], &d
->sig
[i
]);
148 void target_to_host_sigset_internal(sigset_t
*d
, const target_sigset_t
*s
)
151 unsigned long sigmask
;
152 target_ulong target_sigmask
;
154 target_sigmask
= s
->sig
[0];
156 for(i
= 0; i
< 32; i
++) {
157 if (target_sigmask
& (1 << i
))
158 sigmask
|= 1 << (target_to_host_signal(i
+ 1) - 1);
160 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
161 ((unsigned long *)d
)[0] = sigmask
;
162 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++) {
163 ((unsigned long *)d
)[i
] = s
->sig
[i
];
165 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
166 ((unsigned long *)d
)[0] = sigmask
| ((unsigned long)(s
->sig
[1]) << 32);
168 #error target_to_host_sigset
169 #endif /* TARGET_LONG_BITS */
172 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
177 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
178 __get_user(s1
.sig
[i
], &s
->sig
[i
]);
179 target_to_host_sigset_internal(d
, &s1
);
182 void host_to_target_old_sigset(target_ulong
*old_sigset
,
183 const sigset_t
*sigset
)
186 host_to_target_sigset(&d
, sigset
);
187 *old_sigset
= d
.sig
[0];
190 void target_to_host_old_sigset(sigset_t
*sigset
,
191 const target_ulong
*old_sigset
)
196 d
.sig
[0] = *old_sigset
;
197 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
199 target_to_host_sigset(sigset
, &d
);
202 /* siginfo conversion */
204 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
205 const siginfo_t
*info
)
208 sig
= host_to_target_signal(info
->si_signo
);
209 tinfo
->si_signo
= sig
;
212 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
213 sig
== SIGBUS
|| sig
== SIGTRAP
) {
214 /* should never come here, but who knows. The information for
215 the target is irrelevant */
216 tinfo
->_sifields
._sigfault
._addr
= 0;
217 } else if (sig
>= TARGET_SIGRTMIN
) {
218 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
219 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
220 /* XXX: potential problem if 64 bit */
221 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
222 (target_ulong
)info
->si_value
.sival_ptr
;
226 static void tswap_siginfo(target_siginfo_t
*tinfo
,
227 const target_siginfo_t
*info
)
230 sig
= info
->si_signo
;
231 tinfo
->si_signo
= tswap32(sig
);
232 tinfo
->si_errno
= tswap32(info
->si_errno
);
233 tinfo
->si_code
= tswap32(info
->si_code
);
234 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
235 sig
== SIGBUS
|| sig
== SIGTRAP
) {
236 tinfo
->_sifields
._sigfault
._addr
=
237 tswapl(info
->_sifields
._sigfault
._addr
);
238 } else if (sig
>= TARGET_SIGRTMIN
) {
239 tinfo
->_sifields
._rt
._pid
= tswap32(info
->_sifields
._rt
._pid
);
240 tinfo
->_sifields
._rt
._uid
= tswap32(info
->_sifields
._rt
._uid
);
241 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
242 tswapl(info
->_sifields
._rt
._sigval
.sival_ptr
);
247 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
249 host_to_target_siginfo_noswap(tinfo
, info
);
250 tswap_siginfo(tinfo
, tinfo
);
253 /* XXX: we support only POSIX RT signals are used. */
254 /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
255 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
257 info
->si_signo
= tswap32(tinfo
->si_signo
);
258 info
->si_errno
= tswap32(tinfo
->si_errno
);
259 info
->si_code
= tswap32(tinfo
->si_code
);
260 info
->si_pid
= tswap32(tinfo
->_sifields
._rt
._pid
);
261 info
->si_uid
= tswap32(tinfo
->_sifields
._rt
._uid
);
262 info
->si_value
.sival_ptr
=
263 (void *)tswapl(tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
266 void signal_init(void)
268 struct sigaction act
;
271 /* generate signal conversion tables */
272 for(i
= 1; i
<= 64; i
++) {
273 if (host_to_target_signal_table
[i
] == 0)
274 host_to_target_signal_table
[i
] = i
;
276 for(i
= 1; i
<= 64; i
++) {
277 j
= host_to_target_signal_table
[i
];
278 target_to_host_signal_table
[j
] = i
;
281 /* set all host signal handlers. ALL signals are blocked during
282 the handlers to serialize them. */
283 sigfillset(&act
.sa_mask
);
284 act
.sa_flags
= SA_SIGINFO
;
285 act
.sa_sigaction
= host_signal_handler
;
286 for(i
= 1; i
< NSIG
; i
++) {
287 sigaction(i
, &act
, NULL
);
290 memset(sigact_table
, 0, sizeof(sigact_table
));
292 first_free
= &sigqueue_table
[0];
293 for(i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++)
294 sigqueue_table
[i
].next
= &sigqueue_table
[i
+ 1];
295 sigqueue_table
[MAX_SIGQUEUE_SIZE
- 1].next
= NULL
;
298 /* signal queue handling */
300 static inline struct sigqueue
*alloc_sigqueue(void)
302 struct sigqueue
*q
= first_free
;
305 first_free
= q
->next
;
309 static inline void free_sigqueue(struct sigqueue
*q
)
311 q
->next
= first_free
;
315 /* abort execution with signal */
316 void __attribute((noreturn
)) force_sig(int sig
)
319 host_sig
= target_to_host_signal(sig
);
320 fprintf(stderr
, "qemu: uncaught target signal %d (%s) - exiting\n",
321 sig
, strsignal(host_sig
));
326 struct sigaction act
;
327 sigemptyset(&act
.sa_mask
);
328 act
.sa_flags
= SA_SIGINFO
;
329 act
.sa_sigaction
= SIG_DFL
;
330 sigaction(SIGABRT
, &act
, NULL
);
336 /* queue a signal so that it will be send to the virtual CPU as soon
338 int queue_signal(int sig
, target_siginfo_t
*info
)
340 struct emulated_sigaction
*k
;
341 struct sigqueue
*q
, **pq
;
342 target_ulong handler
;
344 #if defined(DEBUG_SIGNAL)
345 fprintf(stderr
, "queue_signal: sig=%d\n",
348 k
= &sigact_table
[sig
- 1];
349 handler
= k
->sa
._sa_handler
;
350 if (handler
== TARGET_SIG_DFL
) {
351 /* default handler : ignore some signal. The other are fatal */
352 if (sig
!= TARGET_SIGCHLD
&&
353 sig
!= TARGET_SIGURG
&&
354 sig
!= TARGET_SIGWINCH
) {
357 return 0; /* indicate ignored */
359 } else if (handler
== TARGET_SIG_IGN
) {
362 } else if (handler
== TARGET_SIG_ERR
) {
366 if (sig
< TARGET_SIGRTMIN
) {
367 /* if non real time signal, we queue exactly one signal */
377 q
= alloc_sigqueue();
388 /* signal that a new signal is pending */
390 return 1; /* indicates that the signal was queued */
394 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
398 target_siginfo_t tinfo
;
400 /* the CPU emulator uses some host signals to detect exceptions,
401 we we forward to it some signals */
402 if (host_signum
== SIGSEGV
|| host_signum
== SIGBUS
403 #if defined(TARGET_I386) && defined(USE_CODE_COPY)
404 || host_signum
== SIGFPE
407 if (cpu_signal_handler(host_signum
, info
, puc
))
411 /* get target signal number */
412 sig
= host_to_target_signal(host_signum
);
413 if (sig
< 1 || sig
> TARGET_NSIG
)
415 #if defined(DEBUG_SIGNAL)
416 fprintf(stderr
, "qemu: got signal %d\n", sig
);
418 host_to_target_siginfo_noswap(&tinfo
, info
);
419 if (queue_signal(sig
, &tinfo
) == 1) {
420 /* interrupt the virtual CPU as soon as possible */
421 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
425 int do_sigaction(int sig
, const struct target_sigaction
*act
,
426 struct target_sigaction
*oact
)
428 struct emulated_sigaction
*k
;
429 struct sigaction act1
;
432 if (sig
< 1 || sig
> TARGET_NSIG
)
434 k
= &sigact_table
[sig
- 1];
435 #if defined(DEBUG_SIGNAL)
436 fprintf(stderr
, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
437 sig
, (int)act
, (int)oact
);
440 oact
->_sa_handler
= tswapl(k
->sa
._sa_handler
);
441 oact
->sa_flags
= tswapl(k
->sa
.sa_flags
);
442 oact
->sa_restorer
= tswapl(k
->sa
.sa_restorer
);
443 oact
->sa_mask
= k
->sa
.sa_mask
;
446 k
->sa
._sa_handler
= tswapl(act
->_sa_handler
);
447 k
->sa
.sa_flags
= tswapl(act
->sa_flags
);
448 k
->sa
.sa_restorer
= tswapl(act
->sa_restorer
);
449 k
->sa
.sa_mask
= act
->sa_mask
;
451 /* we update the host linux signal state */
452 host_sig
= target_to_host_signal(sig
);
453 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
454 sigfillset(&act1
.sa_mask
);
455 act1
.sa_flags
= SA_SIGINFO
;
456 if (k
->sa
.sa_flags
& TARGET_SA_RESTART
)
457 act1
.sa_flags
|= SA_RESTART
;
458 /* NOTE: it is important to update the host kernel signal
459 ignore state to avoid getting unexpected interrupted
461 if (k
->sa
._sa_handler
== TARGET_SIG_IGN
) {
462 act1
.sa_sigaction
= (void *)SIG_IGN
;
463 } else if (k
->sa
._sa_handler
== TARGET_SIG_DFL
) {
464 act1
.sa_sigaction
= (void *)SIG_DFL
;
466 act1
.sa_sigaction
= host_signal_handler
;
468 sigaction(host_sig
, &act1
, NULL
);
475 #define offsetof(type, field) ((size_t) &((type *)0)->field)
478 static inline int copy_siginfo_to_user(target_siginfo_t
*tinfo
,
479 const target_siginfo_t
*info
)
481 tswap_siginfo(tinfo
, info
);
487 /* from the Linux kernel */
489 struct target_fpreg
{
490 uint16_t significand
[4];
494 struct target_fpxreg
{
495 uint16_t significand
[4];
500 struct target_xmmreg
{
501 target_ulong element
[4];
504 struct target_fpstate
{
505 /* Regular FPU environment */
511 target_ulong dataoff
;
512 target_ulong datasel
;
513 struct target_fpreg _st
[8];
515 uint16_t magic
; /* 0xffff = regular FPU data only */
517 /* FXSR FPU environment */
518 target_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
520 target_ulong reserved
;
521 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
522 struct target_xmmreg _xmm
[8];
523 target_ulong padding
[56];
526 #define X86_FXSR_MAGIC 0x0000
528 struct target_sigcontext
{
546 target_ulong esp_at_signal
;
548 target_ulong fpstate
; /* pointer */
549 target_ulong oldmask
;
553 typedef struct target_sigaltstack
{
556 target_ulong ss_size
;
559 struct target_ucontext
{
560 target_ulong uc_flags
;
561 target_ulong uc_link
;
562 target_stack_t uc_stack
;
563 struct target_sigcontext uc_mcontext
;
564 target_sigset_t uc_sigmask
; /* mask last for extensibility */
569 target_ulong pretcode
;
571 struct target_sigcontext sc
;
572 struct target_fpstate fpstate
;
573 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
579 target_ulong pretcode
;
583 struct target_siginfo info
;
584 struct target_ucontext uc
;
585 struct target_fpstate fpstate
;
590 * Set up a signal frame.
593 /* XXX: save x87 state */
595 setup_sigcontext(struct target_sigcontext
*sc
, struct target_fpstate
*fpstate
,
596 CPUX86State
*env
, unsigned long mask
)
600 err
|= __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
601 err
|= __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
602 err
|= __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
603 err
|= __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
604 err
|= __put_user(env
->regs
[R_EDI
], &sc
->edi
);
605 err
|= __put_user(env
->regs
[R_ESI
], &sc
->esi
);
606 err
|= __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
607 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp
);
608 err
|= __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
609 err
|= __put_user(env
->regs
[R_EDX
], &sc
->edx
);
610 err
|= __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
611 err
|= __put_user(env
->regs
[R_EAX
], &sc
->eax
);
612 err
|= __put_user(env
->exception_index
, &sc
->trapno
);
613 err
|= __put_user(env
->error_code
, &sc
->err
);
614 err
|= __put_user(env
->eip
, &sc
->eip
);
615 err
|= __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
616 err
|= __put_user(env
->eflags
, &sc
->eflags
);
617 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
618 err
|= __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
620 cpu_x86_fsave(env
, (void *)fpstate
, 1);
621 fpstate
->status
= fpstate
->sw
;
622 err
|= __put_user(0xffff, &fpstate
->magic
);
623 err
|= __put_user(fpstate
, &sc
->fpstate
);
625 /* non-iBCS2 extensions.. */
626 err
|= __put_user(mask
, &sc
->oldmask
);
627 err
|= __put_user(env
->cr
[2], &sc
->cr2
);
632 * Determine which stack to use..
636 get_sigframe(struct emulated_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
640 /* Default to using normal stack */
641 esp
= env
->regs
[R_ESP
];
643 /* This is the X/Open sanctioned signal stack switching. */
644 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
645 if (sas_ss_flags(esp
) == 0)
646 esp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
649 /* This is the legacy signal stack switching. */
652 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
653 !(ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) &&
654 ka
->sa
.sa_restorer
) {
655 esp
= (unsigned long) ka
->sa
.sa_restorer
;
657 return (void *)((esp
- frame_size
) & -8ul);
660 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
661 target_sigset_t
*set
, CPUX86State
*env
)
663 struct sigframe
*frame
;
666 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
668 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
670 err
|= __put_user((/*current->exec_domain
671 && current->exec_domain->signal_invmap
673 ? current->exec_domain->signal_invmap[sig]
679 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0]);
683 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
684 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
688 /* Set up to return from userspace. If provided, use a stub
689 already in userspace. */
690 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
691 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
693 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
694 /* This is popl %eax ; movl $,%eax ; int $0x80 */
695 err
|= __put_user(0xb858, (short *)(frame
->retcode
+0));
696 err
|= __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
697 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+6));
703 /* Set up registers for signal handler */
704 env
->regs
[R_ESP
] = (unsigned long) frame
;
705 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
707 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
708 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
709 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
710 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
711 env
->eflags
&= ~TF_MASK
;
716 if (sig
== TARGET_SIGSEGV
)
717 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
718 force_sig(TARGET_SIGSEGV
/* , current */);
721 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
722 target_siginfo_t
*info
,
723 target_sigset_t
*set
, CPUX86State
*env
)
725 struct rt_sigframe
*frame
;
728 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
730 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
733 err
|= __put_user((/*current->exec_domain
734 && current->exec_domain->signal_invmap
736 ? current->exec_domain->signal_invmap[sig]
739 err
|= __put_user((target_ulong
)&frame
->info
, &frame
->pinfo
);
740 err
|= __put_user((target_ulong
)&frame
->uc
, &frame
->puc
);
741 err
|= copy_siginfo_to_user(&frame
->info
, info
);
745 /* Create the ucontext. */
746 err
|= __put_user(0, &frame
->uc
.uc_flags
);
747 err
|= __put_user(0, &frame
->uc
.uc_link
);
748 err
|= __put_user(/*current->sas_ss_sp*/ 0, &frame
->uc
.uc_stack
.ss_sp
);
749 err
|= __put_user(/* sas_ss_flags(regs->esp) */ 0,
750 &frame
->uc
.uc_stack
.ss_flags
);
751 err
|= __put_user(/* current->sas_ss_size */ 0, &frame
->uc
.uc_stack
.ss_size
);
752 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, &frame
->fpstate
,
754 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
755 if (__put_user(set
->sig
[i
], &frame
->uc
.uc_sigmask
.sig
[i
]))
759 /* Set up to return from userspace. If provided, use a stub
760 already in userspace. */
761 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
762 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
764 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
765 /* This is movl $,%eax ; int $0x80 */
766 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
767 err
|= __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
768 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+5));
774 /* Set up registers for signal handler */
775 env
->regs
[R_ESP
] = (unsigned long) frame
;
776 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
778 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
779 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
780 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
781 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
782 env
->eflags
&= ~TF_MASK
;
787 if (sig
== TARGET_SIGSEGV
)
788 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
789 force_sig(TARGET_SIGSEGV
/* , current */);
793 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
, int *peax
)
795 unsigned int err
= 0;
797 cpu_x86_load_seg(env
, R_GS
, lduw(&sc
->gs
));
798 cpu_x86_load_seg(env
, R_FS
, lduw(&sc
->fs
));
799 cpu_x86_load_seg(env
, R_ES
, lduw(&sc
->es
));
800 cpu_x86_load_seg(env
, R_DS
, lduw(&sc
->ds
));
802 env
->regs
[R_EDI
] = ldl(&sc
->edi
);
803 env
->regs
[R_ESI
] = ldl(&sc
->esi
);
804 env
->regs
[R_EBP
] = ldl(&sc
->ebp
);
805 env
->regs
[R_ESP
] = ldl(&sc
->esp
);
806 env
->regs
[R_EBX
] = ldl(&sc
->ebx
);
807 env
->regs
[R_EDX
] = ldl(&sc
->edx
);
808 env
->regs
[R_ECX
] = ldl(&sc
->ecx
);
809 env
->eip
= ldl(&sc
->eip
);
811 cpu_x86_load_seg(env
, R_CS
, lduw(&sc
->cs
) | 3);
812 cpu_x86_load_seg(env
, R_SS
, lduw(&sc
->ss
) | 3);
815 unsigned int tmpflags
;
816 tmpflags
= ldl(&sc
->eflags
);
817 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
818 // regs->orig_eax = -1; /* disable syscall checks */
822 struct _fpstate
* buf
;
823 buf
= (void *)ldl(&sc
->fpstate
);
826 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
829 cpu_x86_frstor(env
, (void *)buf
, 1);
833 *peax
= ldl(&sc
->eax
);
841 long do_sigreturn(CPUX86State
*env
)
843 struct sigframe
*frame
= (struct sigframe
*)(env
->regs
[R_ESP
] - 8);
844 target_sigset_t target_set
;
848 #if defined(DEBUG_SIGNAL)
849 fprintf(stderr
, "do_sigreturn\n");
851 /* set blocked signals */
852 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
854 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
855 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
859 target_to_host_sigset_internal(&set
, &target_set
);
860 sigprocmask(SIG_SETMASK
, &set
, NULL
);
862 /* restore registers */
863 if (restore_sigcontext(env
, &frame
->sc
, &eax
))
868 force_sig(TARGET_SIGSEGV
);
872 long do_rt_sigreturn(CPUX86State
*env
)
874 struct rt_sigframe
*frame
= (struct rt_sigframe
*)(env
->regs
[R_ESP
] - 4);
880 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
883 target_to_host_sigset(&set
, &frame
->uc
.uc_sigmask
);
884 sigprocmask(SIG_SETMASK
, &set
, NULL
);
886 if (restore_sigcontext(env
, &frame
->uc
.uc_mcontext
, &eax
))
890 if (__copy_from_user(&st
, &frame
->uc
.uc_stack
, sizeof(st
)))
892 /* It is more difficult to avoid calling this function than to
893 call it and ignore errors. */
894 do_sigaltstack(&st
, NULL
, regs
->esp
);
899 force_sig(TARGET_SIGSEGV
);
903 #elif defined(TARGET_ARM)
905 struct target_sigcontext
{
906 target_ulong trap_no
;
907 target_ulong error_code
;
908 target_ulong oldmask
;
919 target_ulong arm_r10
;
925 target_ulong arm_cpsr
;
926 target_ulong fault_address
;
929 typedef struct target_sigaltstack
{
932 target_ulong ss_size
;
935 struct target_ucontext
{
936 target_ulong uc_flags
;
937 target_ulong uc_link
;
938 target_stack_t uc_stack
;
939 struct target_sigcontext uc_mcontext
;
940 target_sigset_t uc_sigmask
; /* mask last for extensibility */
945 struct target_sigcontext sc
;
946 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
947 target_ulong retcode
;
952 struct target_siginfo
*pinfo
;
954 struct target_siginfo info
;
955 struct target_ucontext uc
;
956 target_ulong retcode
;
959 #define TARGET_CONFIG_CPU_32 1
962 * For ARM syscalls, we encode the syscall number into the instruction.
964 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
965 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
968 * For Thumb syscalls, we pass the syscall number via r7. We therefore
969 * need two 16-bit instructions.
971 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
972 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
974 static const target_ulong retcodes
[4] = {
975 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
976 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
980 #define __put_user_error(x,p,e) __put_user(x, p)
981 #define __get_user_error(x,p,e) __get_user(x, p)
983 static inline int valid_user_regs(CPUState
*regs
)
989 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
990 CPUState
*env
, unsigned long mask
)
994 __put_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
995 __put_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
996 __put_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
997 __put_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
998 __put_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
999 __put_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1000 __put_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1001 __put_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1002 __put_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1003 __put_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1004 __put_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1005 __put_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1006 __put_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1007 __put_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1008 __put_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1009 __put_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1010 #ifdef TARGET_CONFIG_CPU_32
1011 __put_user_error(env
->cpsr
, &sc
->arm_cpsr
, err
);
1014 __put_user_error(/* current->thread.trap_no */ 0, &sc
->trap_no
, err
);
1015 __put_user_error(/* current->thread.error_code */ 0, &sc
->error_code
, err
);
1016 __put_user_error(/* current->thread.address */ 0, &sc
->fault_address
, err
);
1017 __put_user_error(mask
, &sc
->oldmask
, err
);
1022 static inline void *
1023 get_sigframe(struct emulated_sigaction
*ka
, CPUState
*regs
, int framesize
)
1025 unsigned long sp
= regs
->regs
[13];
1029 * This is the X/Open sanctioned signal stack switching.
1031 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
))
1032 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
1035 * ATPCS B01 mandates 8-byte alignment
1037 return (void *)((sp
- framesize
) & ~7);
1041 setup_return(CPUState
*env
, struct emulated_sigaction
*ka
,
1042 target_ulong
*rc
, void *frame
, int usig
)
1044 target_ulong handler
= (target_ulong
)ka
->sa
._sa_handler
;
1045 target_ulong retcode
;
1047 #if defined(TARGET_CONFIG_CPU_32)
1048 target_ulong cpsr
= env
->cpsr
;
1052 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1054 if (ka
->sa
.sa_flags
& SA_THIRTYTWO
)
1055 cpsr
= (cpsr
& ~MODE_MASK
) | USR_MODE
;
1057 #ifdef CONFIG_ARM_THUMB
1058 if (elf_hwcap
& HWCAP_THUMB
) {
1060 * The LSB of the handler determines if we're going to
1061 * be using THUMB or ARM mode for this signal handler.
1063 thumb
= handler
& 1;
1072 #endif /* TARGET_CONFIG_CPU_32 */
1074 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
1075 retcode
= (target_ulong
)ka
->sa
.sa_restorer
;
1077 unsigned int idx
= thumb
;
1079 if (ka
->sa
.sa_flags
& TARGET_SA_SIGINFO
)
1082 if (__put_user(retcodes
[idx
], rc
))
1085 flush_icache_range((target_ulong
)rc
,
1086 (target_ulong
)(rc
+ 1));
1088 retcode
= ((target_ulong
)rc
) + thumb
;
1091 env
->regs
[0] = usig
;
1092 env
->regs
[13] = (target_ulong
)frame
;
1093 env
->regs
[14] = retcode
;
1094 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
1096 #ifdef TARGET_CONFIG_CPU_32
1103 static void setup_frame(int usig
, struct emulated_sigaction
*ka
,
1104 target_sigset_t
*set
, CPUState
*regs
)
1106 struct sigframe
*frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
1109 err
|= setup_sigcontext(&frame
->sc
, /*&frame->fpstate,*/ regs
, set
->sig
[0]);
1111 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1112 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
1117 err
= setup_return(regs
, ka
, &frame
->retcode
, frame
, usig
);
1121 static void setup_rt_frame(int usig
, struct emulated_sigaction
*ka
,
1122 target_siginfo_t
*info
,
1123 target_sigset_t
*set
, CPUState
*env
)
1125 struct rt_sigframe
*frame
= get_sigframe(ka
, env
, sizeof(*frame
));
1128 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
1131 __put_user_error(&frame
->info
, (target_ulong
*)&frame
->pinfo
, err
);
1132 __put_user_error(&frame
->uc
, (target_ulong
*)&frame
->puc
, err
);
1133 err
|= copy_siginfo_to_user(&frame
->info
, info
);
1135 /* Clear all the bits of the ucontext we don't use. */
1136 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
1138 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, /*&frame->fpstate,*/
1140 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1141 if (__put_user(set
->sig
[i
], &frame
->uc
.uc_sigmask
.sig
[i
]))
1146 err
= setup_return(env
, ka
, &frame
->retcode
, frame
, usig
);
1150 * For realtime signals we must also set the second and third
1151 * arguments for the signal handler.
1152 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1154 env
->regs
[1] = (target_ulong
)frame
->pinfo
;
1155 env
->regs
[2] = (target_ulong
)frame
->puc
;
1162 restore_sigcontext(CPUState
*env
, struct target_sigcontext
*sc
)
1166 __get_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
1167 __get_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
1168 __get_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1169 __get_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1170 __get_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1171 __get_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1172 __get_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1173 __get_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1174 __get_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1175 __get_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1176 __get_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1177 __get_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1178 __get_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1179 __get_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1180 __get_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1181 __get_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1182 #ifdef TARGET_CONFIG_CPU_32
1183 __get_user_error(env
->cpsr
, &sc
->arm_cpsr
, err
);
1186 err
|= !valid_user_regs(env
);
1191 long do_sigreturn(CPUState
*env
)
1193 struct sigframe
*frame
;
1194 target_sigset_t set
;
1199 * Since we stacked the signal on a 64-bit boundary,
1200 * then 'sp' should be word aligned here. If it's
1201 * not, then the user is trying to mess with us.
1203 if (env
->regs
[13] & 7)
1206 frame
= (struct sigframe
*)env
->regs
[13];
1209 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
1212 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
1214 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1215 if (__get_user(set
.sig
[i
], &frame
->extramask
[i
- 1]))
1219 target_to_host_sigset_internal(&host_set
, &set
);
1220 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1222 if (restore_sigcontext(env
, &frame
->sc
))
1226 /* Send SIGTRAP if we're single-stepping */
1227 if (ptrace_cancel_bpt(current
))
1228 send_sig(SIGTRAP
, current
, 1);
1230 return env
->regs
[0];
1233 force_sig(SIGSEGV
/* , current */);
1237 long do_rt_sigreturn(CPUState
*env
)
1239 struct rt_sigframe
*frame
;
1243 * Since we stacked the signal on a 64-bit boundary,
1244 * then 'sp' should be word aligned here. If it's
1245 * not, then the user is trying to mess with us.
1247 if (env
->regs
[13] & 7)
1250 frame
= (struct rt_sigframe
*)env
->regs
[13];
1253 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
1256 target_to_host_sigset(&host_set
, &frame
->uc
.uc_sigmask
);
1257 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1259 if (restore_sigcontext(env
, &frame
->uc
.uc_mcontext
))
1263 /* Send SIGTRAP if we're single-stepping */
1264 if (ptrace_cancel_bpt(current
))
1265 send_sig(SIGTRAP
, current
, 1);
1267 return env
->regs
[0];
1270 force_sig(SIGSEGV
/* , current */);
1276 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
1277 target_sigset_t
*set
, CPUState
*env
)
1279 fprintf(stderr
, "setup_frame: not implemented\n");
1282 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
1283 target_siginfo_t
*info
,
1284 target_sigset_t
*set
, CPUState
*env
)
1286 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1289 long do_sigreturn(CPUState
*env
)
1291 fprintf(stderr
, "do_sigreturn: not implemented\n");
1295 long do_rt_sigreturn(CPUState
*env
)
1297 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
1303 void process_pending_signals(void *cpu_env
)
1306 target_ulong handler
;
1307 sigset_t set
, old_set
;
1308 target_sigset_t target_old_set
;
1309 struct emulated_sigaction
*k
;
1312 if (!signal_pending
)
1316 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
1321 /* if no signal is pending, just return */
1327 fprintf(stderr
, "qemu: process signal %d\n", sig
);
1329 /* dequeue signal */
1335 handler
= k
->sa
._sa_handler
;
1336 if (handler
== TARGET_SIG_DFL
) {
1337 /* default handler : ignore some signal. The other are fatal */
1338 if (sig
!= TARGET_SIGCHLD
&&
1339 sig
!= TARGET_SIGURG
&&
1340 sig
!= TARGET_SIGWINCH
) {
1343 } else if (handler
== TARGET_SIG_IGN
) {
1345 } else if (handler
== TARGET_SIG_ERR
) {
1348 /* compute the blocked signals during the handler execution */
1349 target_to_host_sigset(&set
, &k
->sa
.sa_mask
);
1350 /* SA_NODEFER indicates that the current signal should not be
1351 blocked during the handler */
1352 if (!(k
->sa
.sa_flags
& TARGET_SA_NODEFER
))
1353 sigaddset(&set
, target_to_host_signal(sig
));
1355 /* block signals in the handler using Linux */
1356 sigprocmask(SIG_BLOCK
, &set
, &old_set
);
1357 /* save the previous blocked signal state to restore it at the
1358 end of the signal execution (see do_sigreturn) */
1359 host_to_target_sigset_internal(&target_old_set
, &old_set
);
1361 /* if the CPU is in VM86 mode, we restore the 32 bit values */
1364 CPUX86State
*env
= cpu_env
;
1365 if (env
->eflags
& VM_MASK
)
1366 save_v86_state(env
);
1369 /* prepare the stack frame of the virtual CPU */
1370 if (k
->sa
.sa_flags
& TARGET_SA_SIGINFO
)
1371 setup_rt_frame(sig
, k
, &q
->info
, &target_old_set
, cpu_env
);
1373 setup_frame(sig
, k
, &target_old_set
, cpu_env
);
1374 if (k
->sa
.sa_flags
& TARGET_SA_RESETHAND
)
1375 k
->sa
._sa_handler
= TARGET_SIG_DFL
;