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 d
->sig
[i
] = tswapl(d1
.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 s1
.sig
[i
] = tswapl(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
|| sig
== SIGKILL
|| sig
== SIGSTOP
)
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 #if !defined(TARGET_MIPS)
436 oact
->sa_restorer
= tswapl(k
->sa
.sa_restorer
);
438 oact
->sa_mask
= k
->sa
.sa_mask
;
441 k
->sa
._sa_handler
= tswapl(act
->_sa_handler
);
442 k
->sa
.sa_flags
= tswapl(act
->sa_flags
);
443 #if !defined(TARGET_MIPS)
444 k
->sa
.sa_restorer
= tswapl(act
->sa_restorer
);
446 k
->sa
.sa_mask
= act
->sa_mask
;
448 /* we update the host linux signal state */
449 host_sig
= target_to_host_signal(sig
);
450 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
451 sigfillset(&act1
.sa_mask
);
452 act1
.sa_flags
= SA_SIGINFO
;
453 if (k
->sa
.sa_flags
& TARGET_SA_RESTART
)
454 act1
.sa_flags
|= SA_RESTART
;
455 /* NOTE: it is important to update the host kernel signal
456 ignore state to avoid getting unexpected interrupted
458 if (k
->sa
._sa_handler
== TARGET_SIG_IGN
) {
459 act1
.sa_sigaction
= (void *)SIG_IGN
;
460 } else if (k
->sa
._sa_handler
== TARGET_SIG_DFL
) {
461 act1
.sa_sigaction
= (void *)SIG_DFL
;
463 act1
.sa_sigaction
= host_signal_handler
;
465 sigaction(host_sig
, &act1
, NULL
);
472 #define offsetof(type, field) ((size_t) &((type *)0)->field)
475 static inline int copy_siginfo_to_user(target_siginfo_t
*tinfo
,
476 const target_siginfo_t
*info
)
478 tswap_siginfo(tinfo
, info
);
484 /* from the Linux kernel */
486 struct target_fpreg
{
487 uint16_t significand
[4];
491 struct target_fpxreg
{
492 uint16_t significand
[4];
497 struct target_xmmreg
{
498 target_ulong element
[4];
501 struct target_fpstate
{
502 /* Regular FPU environment */
508 target_ulong dataoff
;
509 target_ulong datasel
;
510 struct target_fpreg _st
[8];
512 uint16_t magic
; /* 0xffff = regular FPU data only */
514 /* FXSR FPU environment */
515 target_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
517 target_ulong reserved
;
518 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
519 struct target_xmmreg _xmm
[8];
520 target_ulong padding
[56];
523 #define X86_FXSR_MAGIC 0x0000
525 struct target_sigcontext
{
543 target_ulong esp_at_signal
;
545 target_ulong fpstate
; /* pointer */
546 target_ulong oldmask
;
550 typedef struct target_sigaltstack
{
553 target_ulong ss_size
;
556 struct target_ucontext
{
557 target_ulong tuc_flags
;
558 target_ulong tuc_link
;
559 target_stack_t tuc_stack
;
560 struct target_sigcontext tuc_mcontext
;
561 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
566 target_ulong pretcode
;
568 struct target_sigcontext sc
;
569 struct target_fpstate fpstate
;
570 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
576 target_ulong pretcode
;
580 struct target_siginfo info
;
581 struct target_ucontext uc
;
582 struct target_fpstate fpstate
;
587 * Set up a signal frame.
590 /* XXX: save x87 state */
592 setup_sigcontext(struct target_sigcontext
*sc
, struct target_fpstate
*fpstate
,
593 CPUX86State
*env
, unsigned long mask
)
597 err
|= __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
598 err
|= __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
599 err
|= __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
600 err
|= __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
601 err
|= __put_user(env
->regs
[R_EDI
], &sc
->edi
);
602 err
|= __put_user(env
->regs
[R_ESI
], &sc
->esi
);
603 err
|= __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
604 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp
);
605 err
|= __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
606 err
|= __put_user(env
->regs
[R_EDX
], &sc
->edx
);
607 err
|= __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
608 err
|= __put_user(env
->regs
[R_EAX
], &sc
->eax
);
609 err
|= __put_user(env
->exception_index
, &sc
->trapno
);
610 err
|= __put_user(env
->error_code
, &sc
->err
);
611 err
|= __put_user(env
->eip
, &sc
->eip
);
612 err
|= __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
613 err
|= __put_user(env
->eflags
, &sc
->eflags
);
614 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
615 err
|= __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
617 cpu_x86_fsave(env
, (void *)fpstate
, 1);
618 fpstate
->status
= fpstate
->sw
;
619 err
|= __put_user(0xffff, &fpstate
->magic
);
620 err
|= __put_user(fpstate
, &sc
->fpstate
);
622 /* non-iBCS2 extensions.. */
623 err
|= __put_user(mask
, &sc
->oldmask
);
624 err
|= __put_user(env
->cr
[2], &sc
->cr2
);
629 * Determine which stack to use..
633 get_sigframe(struct emulated_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
637 /* Default to using normal stack */
638 esp
= env
->regs
[R_ESP
];
640 /* This is the X/Open sanctioned signal stack switching. */
641 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
642 if (sas_ss_flags(esp
) == 0)
643 esp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
646 /* This is the legacy signal stack switching. */
649 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
650 !(ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) &&
651 ka
->sa
.sa_restorer
) {
652 esp
= (unsigned long) ka
->sa
.sa_restorer
;
654 return g2h((esp
- frame_size
) & -8ul);
657 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
658 target_sigset_t
*set
, CPUX86State
*env
)
660 struct sigframe
*frame
;
663 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
665 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
667 err
|= __put_user((/*current->exec_domain
668 && current->exec_domain->signal_invmap
670 ? current->exec_domain->signal_invmap[sig]
676 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0]);
680 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
681 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
685 /* Set up to return from userspace. If provided, use a stub
686 already in userspace. */
687 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
688 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
690 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
691 /* This is popl %eax ; movl $,%eax ; int $0x80 */
692 err
|= __put_user(0xb858, (short *)(frame
->retcode
+0));
693 #if defined(TARGET_X86_64)
694 #warning "Fix this !"
696 err
|= __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
698 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+6));
704 /* Set up registers for signal handler */
705 env
->regs
[R_ESP
] = h2g(frame
);
706 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
708 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
709 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
710 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
711 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
712 env
->eflags
&= ~TF_MASK
;
717 if (sig
== TARGET_SIGSEGV
)
718 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
719 force_sig(TARGET_SIGSEGV
/* , current */);
722 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
723 target_siginfo_t
*info
,
724 target_sigset_t
*set
, CPUX86State
*env
)
726 struct rt_sigframe
*frame
;
729 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
731 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
734 err
|= __put_user((/*current->exec_domain
735 && current->exec_domain->signal_invmap
737 ? current->exec_domain->signal_invmap[sig]
740 err
|= __put_user((target_ulong
)&frame
->info
, &frame
->pinfo
);
741 err
|= __put_user((target_ulong
)&frame
->uc
, &frame
->puc
);
742 err
|= copy_siginfo_to_user(&frame
->info
, info
);
746 /* Create the ucontext. */
747 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
748 err
|= __put_user(0, &frame
->uc
.tuc_link
);
749 err
|= __put_user(/*current->sas_ss_sp*/ 0,
750 &frame
->uc
.tuc_stack
.ss_sp
);
751 err
|= __put_user(/* sas_ss_flags(regs->esp) */ 0,
752 &frame
->uc
.tuc_stack
.ss_flags
);
753 err
|= __put_user(/* current->sas_ss_size */ 0,
754 &frame
->uc
.tuc_stack
.ss_size
);
755 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, &frame
->fpstate
,
757 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
758 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
762 /* Set up to return from userspace. If provided, use a stub
763 already in userspace. */
764 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
765 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
767 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
768 /* This is movl $,%eax ; int $0x80 */
769 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
770 err
|= __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
771 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+5));
777 /* Set up registers for signal handler */
778 env
->regs
[R_ESP
] = (unsigned long) frame
;
779 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
781 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
782 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
783 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
784 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
785 env
->eflags
&= ~TF_MASK
;
790 if (sig
== TARGET_SIGSEGV
)
791 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
792 force_sig(TARGET_SIGSEGV
/* , current */);
796 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
, int *peax
)
798 unsigned int err
= 0;
800 cpu_x86_load_seg(env
, R_GS
, lduw(&sc
->gs
));
801 cpu_x86_load_seg(env
, R_FS
, lduw(&sc
->fs
));
802 cpu_x86_load_seg(env
, R_ES
, lduw(&sc
->es
));
803 cpu_x86_load_seg(env
, R_DS
, lduw(&sc
->ds
));
805 env
->regs
[R_EDI
] = ldl(&sc
->edi
);
806 env
->regs
[R_ESI
] = ldl(&sc
->esi
);
807 env
->regs
[R_EBP
] = ldl(&sc
->ebp
);
808 env
->regs
[R_ESP
] = ldl(&sc
->esp
);
809 env
->regs
[R_EBX
] = ldl(&sc
->ebx
);
810 env
->regs
[R_EDX
] = ldl(&sc
->edx
);
811 env
->regs
[R_ECX
] = ldl(&sc
->ecx
);
812 env
->eip
= ldl(&sc
->eip
);
814 cpu_x86_load_seg(env
, R_CS
, lduw(&sc
->cs
) | 3);
815 cpu_x86_load_seg(env
, R_SS
, lduw(&sc
->ss
) | 3);
818 unsigned int tmpflags
;
819 tmpflags
= ldl(&sc
->eflags
);
820 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
821 // regs->orig_eax = -1; /* disable syscall checks */
825 struct _fpstate
* buf
;
826 buf
= (void *)ldl(&sc
->fpstate
);
829 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
832 cpu_x86_frstor(env
, (void *)buf
, 1);
836 *peax
= ldl(&sc
->eax
);
844 long do_sigreturn(CPUX86State
*env
)
846 struct sigframe
*frame
= (struct sigframe
*)g2h(env
->regs
[R_ESP
] - 8);
847 target_sigset_t target_set
;
851 #if defined(DEBUG_SIGNAL)
852 fprintf(stderr
, "do_sigreturn\n");
854 /* set blocked signals */
855 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
857 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
858 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
862 target_to_host_sigset_internal(&set
, &target_set
);
863 sigprocmask(SIG_SETMASK
, &set
, NULL
);
865 /* restore registers */
866 if (restore_sigcontext(env
, &frame
->sc
, &eax
))
871 force_sig(TARGET_SIGSEGV
);
875 long do_rt_sigreturn(CPUX86State
*env
)
877 struct rt_sigframe
*frame
= (struct rt_sigframe
*)g2h(env
->regs
[R_ESP
] - 4);
883 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
886 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
887 sigprocmask(SIG_SETMASK
, &set
, NULL
);
889 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
, &eax
))
893 if (__copy_from_user(&st
, &frame
->uc
.tuc_stack
, sizeof(st
)))
895 /* It is more difficult to avoid calling this function than to
896 call it and ignore errors. */
897 do_sigaltstack(&st
, NULL
, regs
->esp
);
902 force_sig(TARGET_SIGSEGV
);
906 #elif defined(TARGET_ARM)
908 struct target_sigcontext
{
909 target_ulong trap_no
;
910 target_ulong error_code
;
911 target_ulong oldmask
;
922 target_ulong arm_r10
;
928 target_ulong arm_cpsr
;
929 target_ulong fault_address
;
932 typedef struct target_sigaltstack
{
935 target_ulong ss_size
;
938 struct target_ucontext
{
939 target_ulong tuc_flags
;
940 target_ulong tuc_link
;
941 target_stack_t tuc_stack
;
942 struct target_sigcontext tuc_mcontext
;
943 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
948 struct target_sigcontext sc
;
949 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
950 target_ulong retcode
;
955 struct target_siginfo
*pinfo
;
957 struct target_siginfo info
;
958 struct target_ucontext uc
;
959 target_ulong retcode
;
962 #define TARGET_CONFIG_CPU_32 1
965 * For ARM syscalls, we encode the syscall number into the instruction.
967 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
968 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
971 * For Thumb syscalls, we pass the syscall number via r7. We therefore
972 * need two 16-bit instructions.
974 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
975 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
977 static const target_ulong retcodes
[4] = {
978 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
979 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
983 #define __put_user_error(x,p,e) __put_user(x, p)
984 #define __get_user_error(x,p,e) __get_user(x, p)
986 static inline int valid_user_regs(CPUState
*regs
)
992 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
993 CPUState
*env
, unsigned long mask
)
997 __put_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
998 __put_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
999 __put_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1000 __put_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1001 __put_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1002 __put_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1003 __put_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1004 __put_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1005 __put_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1006 __put_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1007 __put_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1008 __put_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1009 __put_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1010 __put_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1011 __put_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1012 __put_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1013 #ifdef TARGET_CONFIG_CPU_32
1014 __put_user_error(cpsr_read(env
), &sc
->arm_cpsr
, err
);
1017 __put_user_error(/* current->thread.trap_no */ 0, &sc
->trap_no
, err
);
1018 __put_user_error(/* current->thread.error_code */ 0, &sc
->error_code
, err
);
1019 __put_user_error(/* current->thread.address */ 0, &sc
->fault_address
, err
);
1020 __put_user_error(mask
, &sc
->oldmask
, err
);
1025 static inline void *
1026 get_sigframe(struct emulated_sigaction
*ka
, CPUState
*regs
, int framesize
)
1028 unsigned long sp
= regs
->regs
[13];
1032 * This is the X/Open sanctioned signal stack switching.
1034 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
))
1035 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
1038 * ATPCS B01 mandates 8-byte alignment
1040 return g2h((sp
- framesize
) & ~7);
1044 setup_return(CPUState
*env
, struct emulated_sigaction
*ka
,
1045 target_ulong
*rc
, void *frame
, int usig
)
1047 target_ulong handler
= (target_ulong
)ka
->sa
._sa_handler
;
1048 target_ulong retcode
;
1050 #if defined(TARGET_CONFIG_CPU_32)
1052 target_ulong cpsr
= env
->cpsr
;
1055 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1057 if (ka
->sa
.sa_flags
& SA_THIRTYTWO
)
1058 cpsr
= (cpsr
& ~MODE_MASK
) | USR_MODE
;
1060 #ifdef CONFIG_ARM_THUMB
1061 if (elf_hwcap
& HWCAP_THUMB
) {
1063 * The LSB of the handler determines if we're going to
1064 * be using THUMB or ARM mode for this signal handler.
1066 thumb
= handler
& 1;
1075 #endif /* TARGET_CONFIG_CPU_32 */
1077 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
1078 retcode
= (target_ulong
)ka
->sa
.sa_restorer
;
1080 unsigned int idx
= thumb
;
1082 if (ka
->sa
.sa_flags
& TARGET_SA_SIGINFO
)
1085 if (__put_user(retcodes
[idx
], rc
))
1088 flush_icache_range((target_ulong
)rc
,
1089 (target_ulong
)(rc
+ 1));
1091 retcode
= ((target_ulong
)rc
) + thumb
;
1094 env
->regs
[0] = usig
;
1095 env
->regs
[13] = h2g(frame
);
1096 env
->regs
[14] = retcode
;
1097 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
1100 #ifdef TARGET_CONFIG_CPU_32
1108 static void setup_frame(int usig
, struct emulated_sigaction
*ka
,
1109 target_sigset_t
*set
, CPUState
*regs
)
1111 struct sigframe
*frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
1114 err
|= setup_sigcontext(&frame
->sc
, /*&frame->fpstate,*/ regs
, set
->sig
[0]);
1116 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1117 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
1122 err
= setup_return(regs
, ka
, &frame
->retcode
, frame
, usig
);
1126 static void setup_rt_frame(int usig
, struct emulated_sigaction
*ka
,
1127 target_siginfo_t
*info
,
1128 target_sigset_t
*set
, CPUState
*env
)
1130 struct rt_sigframe
*frame
= get_sigframe(ka
, env
, sizeof(*frame
));
1133 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
1136 __put_user_error(&frame
->info
, (target_ulong
*)&frame
->pinfo
, err
);
1137 __put_user_error(&frame
->uc
, (target_ulong
*)&frame
->puc
, err
);
1138 err
|= copy_siginfo_to_user(&frame
->info
, info
);
1140 /* Clear all the bits of the ucontext we don't use. */
1141 memset(&frame
->uc
, 0, offsetof(struct target_ucontext
, tuc_mcontext
));
1143 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, /*&frame->fpstate,*/
1145 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1146 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
1151 err
= setup_return(env
, ka
, &frame
->retcode
, frame
, usig
);
1155 * For realtime signals we must also set the second and third
1156 * arguments for the signal handler.
1157 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1159 env
->regs
[1] = (target_ulong
)frame
->pinfo
;
1160 env
->regs
[2] = (target_ulong
)frame
->puc
;
1167 restore_sigcontext(CPUState
*env
, struct target_sigcontext
*sc
)
1172 __get_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
1173 __get_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
1174 __get_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1175 __get_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1176 __get_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1177 __get_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1178 __get_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1179 __get_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1180 __get_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1181 __get_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1182 __get_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1183 __get_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1184 __get_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1185 __get_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1186 __get_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1187 __get_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1188 #ifdef TARGET_CONFIG_CPU_32
1189 __get_user_error(cpsr
, &sc
->arm_cpsr
, err
);
1190 cpsr_write(env
, cpsr
, 0xffffffff);
1193 err
|= !valid_user_regs(env
);
1198 long do_sigreturn(CPUState
*env
)
1200 struct sigframe
*frame
;
1201 target_sigset_t set
;
1206 * Since we stacked the signal on a 64-bit boundary,
1207 * then 'sp' should be word aligned here. If it's
1208 * not, then the user is trying to mess with us.
1210 if (env
->regs
[13] & 7)
1213 frame
= (struct sigframe
*)g2h(env
->regs
[13]);
1216 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
1219 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
1221 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1222 if (__get_user(set
.sig
[i
], &frame
->extramask
[i
- 1]))
1226 target_to_host_sigset_internal(&host_set
, &set
);
1227 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1229 if (restore_sigcontext(env
, &frame
->sc
))
1233 /* Send SIGTRAP if we're single-stepping */
1234 if (ptrace_cancel_bpt(current
))
1235 send_sig(SIGTRAP
, current
, 1);
1237 return env
->regs
[0];
1240 force_sig(SIGSEGV
/* , current */);
1244 long do_rt_sigreturn(CPUState
*env
)
1246 struct rt_sigframe
*frame
;
1250 * Since we stacked the signal on a 64-bit boundary,
1251 * then 'sp' should be word aligned here. If it's
1252 * not, then the user is trying to mess with us.
1254 if (env
->regs
[13] & 7)
1257 frame
= (struct rt_sigframe
*)env
->regs
[13];
1260 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
1263 target_to_host_sigset(&host_set
, &frame
->uc
.tuc_sigmask
);
1264 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1266 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
))
1270 /* Send SIGTRAP if we're single-stepping */
1271 if (ptrace_cancel_bpt(current
))
1272 send_sig(SIGTRAP
, current
, 1);
1274 return env
->regs
[0];
1277 force_sig(SIGSEGV
/* , current */);
1281 #elif defined(TARGET_SPARC)
1283 #define __SUNOS_MAXWIN 31
1285 /* This is what SunOS does, so shall I. */
1286 struct target_sigcontext
{
1287 target_ulong sigc_onstack
; /* state to restore */
1289 target_ulong sigc_mask
; /* sigmask to restore */
1290 target_ulong sigc_sp
; /* stack pointer */
1291 target_ulong sigc_pc
; /* program counter */
1292 target_ulong sigc_npc
; /* next program counter */
1293 target_ulong sigc_psr
; /* for condition codes etc */
1294 target_ulong sigc_g1
; /* User uses these two registers */
1295 target_ulong sigc_o0
; /* within the trampoline code. */
1297 /* Now comes information regarding the users window set
1298 * at the time of the signal.
1300 target_ulong sigc_oswins
; /* outstanding windows */
1302 /* stack ptrs for each regwin buf */
1303 char *sigc_spbuf
[__SUNOS_MAXWIN
];
1305 /* Windows to restore after signal */
1307 target_ulong locals
[8];
1308 target_ulong ins
[8];
1309 } sigc_wbuf
[__SUNOS_MAXWIN
];
1311 /* A Sparc stack frame */
1312 struct sparc_stackf
{
1313 target_ulong locals
[8];
1314 target_ulong ins
[6];
1315 struct sparc_stackf
*fp
;
1316 target_ulong callers_pc
;
1318 target_ulong xargs
[6];
1319 target_ulong xxargs
[1];
1328 target_ulong u_regs
[16]; /* globals and ins */
1334 unsigned long si_float_regs
[32];
1335 unsigned long si_fsr
;
1336 unsigned long si_fpqdepth
;
1338 unsigned long *insn_addr
;
1341 } qemu_siginfo_fpu_t
;
1344 struct target_signal_frame
{
1345 struct sparc_stackf ss
;
1347 qemu_siginfo_fpu_t
*fpu_save
;
1348 target_ulong insns
[2] __attribute__ ((aligned (8)));
1349 target_ulong extramask
[TARGET_NSIG_WORDS
- 1];
1350 target_ulong extra_size
; /* Should be 0 */
1351 qemu_siginfo_fpu_t fpu_state
;
1353 struct target_rt_signal_frame
{
1354 struct sparc_stackf ss
;
1356 target_ulong regs
[20];
1358 qemu_siginfo_fpu_t
*fpu_save
;
1359 unsigned int insns
[2];
1361 unsigned int extra_size
; /* Should be 0 */
1362 qemu_siginfo_fpu_t fpu_state
;
1373 #define UREG_FP UREG_I6
1374 #define UREG_SP UREG_O6
1376 static inline void *get_sigframe(struct emulated_sigaction
*sa
, CPUState
*env
, unsigned long framesize
)
1380 sp
= env
->regwptr
[UREG_FP
];
1383 /* This is the X/Open sanctioned signal stack switching. */
1384 if (sa
->sa_flags
& TARGET_SA_ONSTACK
) {
1385 if (!on_sig_stack(sp
) && !((current
->sas_ss_sp
+ current
->sas_ss_size
) & 7))
1386 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
1389 return g2h(sp
- framesize
);
1393 setup___siginfo(__siginfo_t
*si
, CPUState
*env
, target_ulong mask
)
1397 err
|= __put_user(env
->psr
, &si
->si_regs
.psr
);
1398 err
|= __put_user(env
->pc
, &si
->si_regs
.pc
);
1399 err
|= __put_user(env
->npc
, &si
->si_regs
.npc
);
1400 err
|= __put_user(env
->y
, &si
->si_regs
.y
);
1401 for (i
=0; i
< 8; i
++) {
1402 err
|= __put_user(env
->gregs
[i
], &si
->si_regs
.u_regs
[i
]);
1404 for (i
=0; i
< 8; i
++) {
1405 err
|= __put_user(env
->regwptr
[UREG_I0
+ i
], &si
->si_regs
.u_regs
[i
+8]);
1407 err
|= __put_user(mask
, &si
->si_mask
);
1413 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1414 CPUState
*env
, unsigned long mask
)
1418 err
|= __put_user(mask
, &sc
->sigc_mask
);
1419 err
|= __put_user(env
->regwptr
[UREG_SP
], &sc
->sigc_sp
);
1420 err
|= __put_user(env
->pc
, &sc
->sigc_pc
);
1421 err
|= __put_user(env
->npc
, &sc
->sigc_npc
);
1422 err
|= __put_user(env
->psr
, &sc
->sigc_psr
);
1423 err
|= __put_user(env
->gregs
[1], &sc
->sigc_g1
);
1424 err
|= __put_user(env
->regwptr
[UREG_O0
], &sc
->sigc_o0
);
1429 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1431 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
1432 target_sigset_t
*set
, CPUState
*env
)
1434 struct target_signal_frame
*sf
;
1435 int sigframe_size
, err
, i
;
1437 /* 1. Make sure everything is clean */
1438 //synchronize_user_stack();
1440 sigframe_size
= NF_ALIGNEDSZ
;
1442 sf
= (struct target_signal_frame
*)
1443 get_sigframe(ka
, env
, sigframe_size
);
1445 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1447 if (invalid_frame_pointer(sf
, sigframe_size
))
1448 goto sigill_and_return
;
1450 /* 2. Save the current process state */
1451 err
= setup___siginfo(&sf
->info
, env
, set
->sig
[0]);
1452 err
|= __put_user(0, &sf
->extra_size
);
1454 //err |= save_fpu_state(regs, &sf->fpu_state);
1455 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1457 err
|= __put_user(set
->sig
[0], &sf
->info
.si_mask
);
1458 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
1459 err
|= __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
1462 for (i
= 0; i
< 8; i
++) {
1463 err
|= __put_user(env
->regwptr
[i
+ UREG_L0
], &sf
->ss
.locals
[i
]);
1465 for (i
= 0; i
< 8; i
++) {
1466 err
|= __put_user(env
->regwptr
[i
+ UREG_I0
], &sf
->ss
.ins
[i
]);
1471 /* 3. signal handler back-trampoline and parameters */
1472 env
->regwptr
[UREG_FP
] = h2g(sf
);
1473 env
->regwptr
[UREG_I0
] = sig
;
1474 env
->regwptr
[UREG_I1
] = h2g(&sf
->info
);
1475 env
->regwptr
[UREG_I2
] = h2g(&sf
->info
);
1477 /* 4. signal handler */
1478 env
->pc
= (unsigned long) ka
->sa
._sa_handler
;
1479 env
->npc
= (env
->pc
+ 4);
1480 /* 5. return to kernel instructions */
1481 if (ka
->sa
.sa_restorer
)
1482 env
->regwptr
[UREG_I7
] = (unsigned long)ka
->sa
.sa_restorer
;
1484 env
->regwptr
[UREG_I7
] = h2g(&(sf
->insns
[0]) - 2);
1486 /* mov __NR_sigreturn, %g1 */
1487 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
1490 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
1494 /* Flush instruction space. */
1495 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1500 //sigill_and_return:
1501 force_sig(TARGET_SIGILL
);
1503 //fprintf(stderr, "force_sig\n");
1504 force_sig(TARGET_SIGSEGV
);
1507 restore_fpu_state(CPUState
*env
, qemu_siginfo_fpu_t
*fpu
)
1512 if (current
->flags
& PF_USEDFPU
)
1513 regs
->psr
&= ~PSR_EF
;
1515 if (current
== last_task_used_math
) {
1516 last_task_used_math
= 0;
1517 regs
->psr
&= ~PSR_EF
;
1520 current
->used_math
= 1;
1521 current
->flags
&= ~PF_USEDFPU
;
1524 if (verify_area (VERIFY_READ
, fpu
, sizeof(*fpu
)))
1529 /* XXX: incorrect */
1530 err
= __copy_from_user(&env
->fpr
[0], &fpu
->si_float_regs
[0],
1531 (sizeof(unsigned long) * 32));
1533 err
|= __get_user(env
->fsr
, &fpu
->si_fsr
);
1535 err
|= __get_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
1536 if (current
->thread
.fpqdepth
!= 0)
1537 err
|= __copy_from_user(¤t
->thread
.fpqueue
[0],
1538 &fpu
->si_fpqueue
[0],
1539 ((sizeof(unsigned long) +
1540 (sizeof(unsigned long *)))*16));
1546 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
1547 target_siginfo_t
*info
,
1548 target_sigset_t
*set
, CPUState
*env
)
1550 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1553 long do_sigreturn(CPUState
*env
)
1555 struct target_signal_frame
*sf
;
1556 uint32_t up_psr
, pc
, npc
;
1557 target_sigset_t set
;
1559 target_ulong fpu_save
;
1562 sf
= (struct target_signal_frame
*)g2h(env
->regwptr
[UREG_FP
]);
1564 fprintf(stderr
, "sigreturn\n");
1565 fprintf(stderr
, "sf: %x pc %x fp %x sp %x\n", sf
, env
->pc
, env
->regwptr
[UREG_FP
], env
->regwptr
[UREG_SP
]);
1567 //cpu_dump_state(env, stderr, fprintf, 0);
1569 /* 1. Make sure we are not getting garbage from the user */
1571 if (verify_area (VERIFY_READ
, sf
, sizeof (*sf
)))
1575 if (((uint
) sf
) & 3)
1578 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
1579 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
1584 /* 2. Restore the state */
1585 err
|= __get_user(up_psr
, &sf
->info
.si_regs
.psr
);
1587 /* User can only change condition codes and FPU enabling in %psr. */
1588 env
->psr
= (up_psr
& (PSR_ICC
/* | PSR_EF */))
1589 | (env
->psr
& ~(PSR_ICC
/* | PSR_EF */));
1593 err
|= __get_user(env
->y
, &sf
->info
.si_regs
.y
);
1594 for (i
=0; i
< 8; i
++) {
1595 err
|= __get_user(env
->gregs
[i
], &sf
->info
.si_regs
.u_regs
[i
]);
1597 for (i
=0; i
< 8; i
++) {
1598 err
|= __get_user(env
->regwptr
[i
+ UREG_I0
], &sf
->info
.si_regs
.u_regs
[i
+8]);
1601 err
|= __get_user(fpu_save
, (target_ulong
*)&sf
->fpu_save
);
1604 // err |= restore_fpu_state(env, fpu_save);
1606 /* This is pretty much atomic, no amount locking would prevent
1607 * the races which exist anyways.
1609 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
1610 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1611 err
|= (__get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]));
1614 target_to_host_sigset_internal(&host_set
, &set
);
1615 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1620 return env
->regwptr
[0];
1623 force_sig(TARGET_SIGSEGV
);
1626 long do_rt_sigreturn(CPUState
*env
)
1628 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
1632 #elif defined(TARGET_MIPS)
1634 struct target_sigcontext
{
1635 uint32_t sc_regmask
; /* Unused */
1638 uint64_t sc_regs
[32];
1639 uint64_t sc_fpregs
[32];
1640 uint32_t sc_ownedfp
; /* Unused */
1641 uint32_t sc_fpc_csr
;
1642 uint32_t sc_fpc_eir
; /* Unused */
1643 uint32_t sc_used_math
;
1644 uint32_t sc_dsp
; /* dsp status, was sc_ssflags */
1647 target_ulong sc_hi1
; /* Was sc_cause */
1648 target_ulong sc_lo1
; /* Was sc_badvaddr */
1649 target_ulong sc_hi2
; /* Was sc_sigset[4] */
1650 target_ulong sc_lo2
;
1651 target_ulong sc_hi3
;
1652 target_ulong sc_lo3
;
1656 uint32_t sf_ass
[4]; /* argument save space for o32 */
1657 uint32_t sf_code
[2]; /* signal trampoline */
1658 struct target_sigcontext sf_sc
;
1659 target_sigset_t sf_mask
;
1662 /* Install trampoline to jump back from signal handler */
1663 static inline int install_sigtramp(unsigned int *tramp
, unsigned int syscall
)
1668 * Set up the return code ...
1670 * li v0, __NR__foo_sigreturn
1674 err
= __put_user(0x24020000 + syscall
, tramp
+ 0);
1675 err
|= __put_user(0x0000000c , tramp
+ 1);
1676 /* flush_cache_sigtramp((unsigned long) tramp); */
1681 setup_sigcontext(CPUState
*regs
, struct target_sigcontext
*sc
)
1685 err
|= __put_user(regs
->PC
, &sc
->sc_pc
);
1687 #define save_gp_reg(i) do { \
1688 err |= __put_user(regs->gpr[i], &sc->sc_regs[i]); \
1690 __put_user(0, &sc
->sc_regs
[0]); save_gp_reg(1); save_gp_reg(2);
1691 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
1692 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
1693 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
1694 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
1695 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
1696 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
1697 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
1701 err
|= __put_user(regs
->HI
, &sc
->sc_mdhi
);
1702 err
|= __put_user(regs
->LO
, &sc
->sc_mdlo
);
1704 /* Not used yet, but might be useful if we ever have DSP suppport */
1707 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
1708 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
1709 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
1710 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
1711 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
1712 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
1713 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
1715 /* same with 64 bit */
1717 err
|= __put_user(regs
->hi
, &sc
->sc_hi
[0]);
1718 err
|= __put_user(regs
->lo
, &sc
->sc_lo
[0]);
1720 err
|= __put_user(mfhi1(), &sc
->sc_hi
[1]);
1721 err
|= __put_user(mflo1(), &sc
->sc_lo
[1]);
1722 err
|= __put_user(mfhi2(), &sc
->sc_hi
[2]);
1723 err
|= __put_user(mflo2(), &sc
->sc_lo
[2]);
1724 err
|= __put_user(mfhi3(), &sc
->sc_hi
[3]);
1725 err
|= __put_user(mflo3(), &sc
->sc_lo
[3]);
1726 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
1735 err
|= __put_user(!!used_math(), &sc
->sc_used_math
);
1741 * Save FPU state to signal context. Signal handler will "inherit"
1742 * current FPU state.
1746 if (!is_fpu_owner()) {
1748 restore_fp(current
);
1750 err
|= save_fp_context(sc
);
1759 restore_sigcontext(CPUState
*regs
, struct target_sigcontext
*sc
)
1763 err
|= __get_user(regs
->CP0_EPC
, &sc
->sc_pc
);
1765 err
|= __get_user(regs
->HI
, &sc
->sc_mdhi
);
1766 err
|= __get_user(regs
->LO
, &sc
->sc_mdlo
);
1768 #define restore_gp_reg(i) do { \
1769 err |= __get_user(regs->gpr[i], &sc->sc_regs[i]); \
1771 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
1772 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
1773 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
1774 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
1775 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
1776 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
1777 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
1778 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
1779 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
1780 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
1782 #undef restore_gp_reg
1786 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
1787 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
1788 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
1789 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
1790 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
1791 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
1792 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
1795 err
|= __get_user(regs
->hi
, &sc
->sc_hi
[0]);
1796 err
|= __get_user(regs
->lo
, &sc
->sc_lo
[0]);
1798 err
|= __get_user(treg
, &sc
->sc_hi
[1]); mthi1(treg
);
1799 err
|= __get_user(treg
, &sc
->sc_lo
[1]); mthi1(treg
);
1800 err
|= __get_user(treg
, &sc
->sc_hi
[2]); mthi2(treg
);
1801 err
|= __get_user(treg
, &sc
->sc_lo
[2]); mthi2(treg
);
1802 err
|= __get_user(treg
, &sc
->sc_hi
[3]); mthi3(treg
);
1803 err
|= __get_user(treg
, &sc
->sc_lo
[3]); mthi3(treg
);
1804 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
1808 err
|= __get_user(used_math
, &sc
->sc_used_math
);
1809 conditional_used_math(used_math
);
1814 /* restore fpu context if we have used it before */
1816 err
|= restore_fp_context(sc
);
1818 /* signal handler may have used FPU. Give it up. */
1827 * Determine which stack to use..
1829 static inline void *
1830 get_sigframe(struct emulated_sigaction
*ka
, CPUState
*regs
, size_t frame_size
)
1834 /* Default to using normal stack */
1838 * FPU emulator may have it's own trampoline active just
1839 * above the user stack, 16-bytes before the next lowest
1840 * 16 byte boundary. Try to avoid trashing it.
1845 /* This is the X/Open sanctioned signal stack switching. */
1846 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) && (sas_ss_flags (sp
) == 0))
1847 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
1850 return g2h((sp
- frame_size
) & ~7);
1853 static void setup_frame(int sig
, struct emulated_sigaction
* ka
,
1854 target_sigset_t
*set
, CPUState
*regs
)
1856 struct sigframe
*frame
;
1859 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
1860 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
1863 install_sigtramp(frame
->sf_code
, TARGET_NR_sigreturn
);
1865 if(setup_sigcontext(regs
, &frame
->sf_sc
))
1868 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1869 if(__put_user(set
->sig
[i
], &frame
->sf_mask
.sig
[i
]))
1874 * Arguments to signal handler:
1876 * a0 = signal number
1877 * a1 = 0 (should be cause)
1878 * a2 = pointer to struct sigcontext
1880 * $25 and PC point to the signal handler, $29 points to the
1883 regs
->gpr
[ 4] = sig
;
1885 regs
->gpr
[ 6] = h2g(&frame
->sf_sc
);
1886 regs
->gpr
[29] = h2g(frame
);
1887 regs
->gpr
[31] = h2g(frame
->sf_code
);
1888 /* The original kernel code sets CP0_EPC to the handler
1889 * since it returns to userland using eret
1890 * we cannot do this here, and we must set PC directly */
1891 regs
->PC
= regs
->gpr
[25] = ka
->sa
._sa_handler
;
1895 force_sig(TARGET_SIGSEGV
/*, current*/);
1899 long do_sigreturn(CPUState
*regs
)
1901 struct sigframe
*frame
;
1903 target_sigset_t target_set
;
1906 #if defined(DEBUG_SIGNAL)
1907 fprintf(stderr
, "do_sigreturn\n");
1909 frame
= (struct sigframe
*) regs
->gpr
[29];
1910 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
1913 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1914 if(__get_user(target_set
.sig
[i
], &frame
->sf_mask
.sig
[i
]))
1918 target_to_host_sigset_internal(&blocked
, &target_set
);
1919 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
1921 if (restore_sigcontext(regs
, &frame
->sf_sc
))
1926 * Don't let your children do this ...
1928 __asm__
__volatile__(
1936 regs
->PC
= regs
->CP0_EPC
;
1937 /* I am not sure this is right, but it seems to work
1938 * maybe a problem with nested signals ? */
1943 force_sig(TARGET_SIGSEGV
/*, current*/);
1948 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
1949 target_siginfo_t
*info
,
1950 target_sigset_t
*set
, CPUState
*env
)
1952 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1955 long do_rt_sigreturn(CPUState
*env
)
1957 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
1963 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
1964 target_sigset_t
*set
, CPUState
*env
)
1966 fprintf(stderr
, "setup_frame: not implemented\n");
1969 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
1970 target_siginfo_t
*info
,
1971 target_sigset_t
*set
, CPUState
*env
)
1973 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1976 long do_sigreturn(CPUState
*env
)
1978 fprintf(stderr
, "do_sigreturn: not implemented\n");
1982 long do_rt_sigreturn(CPUState
*env
)
1984 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
1990 void process_pending_signals(void *cpu_env
)
1993 target_ulong handler
;
1994 sigset_t set
, old_set
;
1995 target_sigset_t target_old_set
;
1996 struct emulated_sigaction
*k
;
1999 if (!signal_pending
)
2003 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
2008 /* if no signal is pending, just return */
2014 fprintf(stderr
, "qemu: process signal %d\n", sig
);
2016 /* dequeue signal */
2022 sig
= gdb_handlesig (cpu_env
, sig
);
2024 fprintf (stderr
, "Lost signal\n");
2028 handler
= k
->sa
._sa_handler
;
2029 if (handler
== TARGET_SIG_DFL
) {
2030 /* default handler : ignore some signal. The other are fatal */
2031 if (sig
!= TARGET_SIGCHLD
&&
2032 sig
!= TARGET_SIGURG
&&
2033 sig
!= TARGET_SIGWINCH
) {
2036 } else if (handler
== TARGET_SIG_IGN
) {
2038 } else if (handler
== TARGET_SIG_ERR
) {
2041 /* compute the blocked signals during the handler execution */
2042 target_to_host_sigset(&set
, &k
->sa
.sa_mask
);
2043 /* SA_NODEFER indicates that the current signal should not be
2044 blocked during the handler */
2045 if (!(k
->sa
.sa_flags
& TARGET_SA_NODEFER
))
2046 sigaddset(&set
, target_to_host_signal(sig
));
2048 /* block signals in the handler using Linux */
2049 sigprocmask(SIG_BLOCK
, &set
, &old_set
);
2050 /* save the previous blocked signal state to restore it at the
2051 end of the signal execution (see do_sigreturn) */
2052 host_to_target_sigset_internal(&target_old_set
, &old_set
);
2054 /* if the CPU is in VM86 mode, we restore the 32 bit values */
2055 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
2057 CPUX86State
*env
= cpu_env
;
2058 if (env
->eflags
& VM_MASK
)
2059 save_v86_state(env
);
2062 /* prepare the stack frame of the virtual CPU */
2063 if (k
->sa
.sa_flags
& TARGET_SA_SIGINFO
)
2064 setup_rt_frame(sig
, k
, &q
->info
, &target_old_set
, cpu_env
);
2066 setup_frame(sig
, k
, &target_old_set
, cpu_env
);
2067 if (k
->sa
.sa_flags
& TARGET_SA_RESETHAND
)
2068 k
->sa
._sa_handler
= TARGET_SIG_DFL
;