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>
30 #include "target_signal.h"
32 //#define DEBUG_SIGNAL
34 #define MAX_SIGQUEUE_SIZE 1024
37 struct sigqueue
*next
;
38 target_siginfo_t info
;
41 struct emulated_sigaction
{
42 struct target_sigaction sa
;
43 int pending
; /* true if signal is pending */
44 struct sigqueue
*first
;
45 struct sigqueue info
; /* in order to always have memory for the
46 first signal, we put it here */
49 struct target_sigaltstack target_sigaltstack_used
= {
52 .ss_flags
= TARGET_SS_DISABLE
,
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 on_sig_stack(unsigned long sp
)
104 return (sp
- target_sigaltstack_used
.ss_sp
105 < target_sigaltstack_used
.ss_size
);
108 static inline int sas_ss_flags(unsigned long sp
)
110 return (target_sigaltstack_used
.ss_size
== 0 ? SS_DISABLE
111 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
114 static inline int host_to_target_signal(int sig
)
116 return host_to_target_signal_table
[sig
];
119 static inline int target_to_host_signal(int sig
)
121 return target_to_host_signal_table
[sig
];
124 static void host_to_target_sigset_internal(target_sigset_t
*d
,
128 unsigned long sigmask
;
129 uint32_t target_sigmask
;
131 sigmask
= ((unsigned long *)s
)[0];
133 for(i
= 0; i
< 32; i
++) {
134 if (sigmask
& (1 << i
))
135 target_sigmask
|= 1 << (host_to_target_signal(i
+ 1) - 1);
137 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
138 d
->sig
[0] = target_sigmask
;
139 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++) {
140 d
->sig
[i
] = ((unsigned long *)s
)[i
];
142 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
143 d
->sig
[0] = target_sigmask
;
144 d
->sig
[1] = sigmask
>> 32;
146 #warning host_to_target_sigset
150 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
155 host_to_target_sigset_internal(&d1
, s
);
156 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
157 d
->sig
[i
] = tswapl(d1
.sig
[i
]);
160 void target_to_host_sigset_internal(sigset_t
*d
, const target_sigset_t
*s
)
163 unsigned long sigmask
;
164 abi_ulong target_sigmask
;
166 target_sigmask
= s
->sig
[0];
168 for(i
= 0; i
< 32; i
++) {
169 if (target_sigmask
& (1 << i
))
170 sigmask
|= 1 << (target_to_host_signal(i
+ 1) - 1);
172 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
173 ((unsigned long *)d
)[0] = sigmask
;
174 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++) {
175 ((unsigned long *)d
)[i
] = s
->sig
[i
];
177 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
178 ((unsigned long *)d
)[0] = sigmask
| ((unsigned long)(s
->sig
[1]) << 32);
180 #warning target_to_host_sigset
181 #endif /* TARGET_ABI_BITS */
184 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
189 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
190 s1
.sig
[i
] = tswapl(s
->sig
[i
]);
191 target_to_host_sigset_internal(d
, &s1
);
194 void host_to_target_old_sigset(abi_ulong
*old_sigset
,
195 const sigset_t
*sigset
)
198 host_to_target_sigset(&d
, sigset
);
199 *old_sigset
= d
.sig
[0];
202 void target_to_host_old_sigset(sigset_t
*sigset
,
203 const abi_ulong
*old_sigset
)
208 d
.sig
[0] = *old_sigset
;
209 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
211 target_to_host_sigset(sigset
, &d
);
214 /* siginfo conversion */
216 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
217 const siginfo_t
*info
)
220 sig
= host_to_target_signal(info
->si_signo
);
221 tinfo
->si_signo
= sig
;
224 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
225 sig
== SIGBUS
|| sig
== SIGTRAP
) {
226 /* should never come here, but who knows. The information for
227 the target is irrelevant */
228 tinfo
->_sifields
._sigfault
._addr
= 0;
229 } else if (sig
== SIGIO
) {
230 tinfo
->_sifields
._sigpoll
._fd
= info
->si_fd
;
231 } else if (sig
>= TARGET_SIGRTMIN
) {
232 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
233 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
234 /* XXX: potential problem if 64 bit */
235 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
236 (abi_ulong
)info
->si_value
.sival_ptr
;
240 static void tswap_siginfo(target_siginfo_t
*tinfo
,
241 const target_siginfo_t
*info
)
244 sig
= info
->si_signo
;
245 tinfo
->si_signo
= tswap32(sig
);
246 tinfo
->si_errno
= tswap32(info
->si_errno
);
247 tinfo
->si_code
= tswap32(info
->si_code
);
248 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
249 sig
== SIGBUS
|| sig
== SIGTRAP
) {
250 tinfo
->_sifields
._sigfault
._addr
=
251 tswapl(info
->_sifields
._sigfault
._addr
);
252 } else if (sig
== SIGIO
) {
253 tinfo
->_sifields
._sigpoll
._fd
= tswap32(info
->_sifields
._sigpoll
._fd
);
254 } else if (sig
>= TARGET_SIGRTMIN
) {
255 tinfo
->_sifields
._rt
._pid
= tswap32(info
->_sifields
._rt
._pid
);
256 tinfo
->_sifields
._rt
._uid
= tswap32(info
->_sifields
._rt
._uid
);
257 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
258 tswapl(info
->_sifields
._rt
._sigval
.sival_ptr
);
263 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
265 host_to_target_siginfo_noswap(tinfo
, info
);
266 tswap_siginfo(tinfo
, tinfo
);
269 /* XXX: we support only POSIX RT signals are used. */
270 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
271 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
273 info
->si_signo
= tswap32(tinfo
->si_signo
);
274 info
->si_errno
= tswap32(tinfo
->si_errno
);
275 info
->si_code
= tswap32(tinfo
->si_code
);
276 info
->si_pid
= tswap32(tinfo
->_sifields
._rt
._pid
);
277 info
->si_uid
= tswap32(tinfo
->_sifields
._rt
._uid
);
278 info
->si_value
.sival_ptr
=
279 (void *)tswapl(tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
282 void signal_init(void)
284 struct sigaction act
;
287 /* generate signal conversion tables */
288 for(i
= 1; i
<= 64; i
++) {
289 if (host_to_target_signal_table
[i
] == 0)
290 host_to_target_signal_table
[i
] = i
;
292 for(i
= 1; i
<= 64; i
++) {
293 j
= host_to_target_signal_table
[i
];
294 target_to_host_signal_table
[j
] = i
;
297 /* set all host signal handlers. ALL signals are blocked during
298 the handlers to serialize them. */
299 sigfillset(&act
.sa_mask
);
300 act
.sa_flags
= SA_SIGINFO
;
301 act
.sa_sigaction
= host_signal_handler
;
302 for(i
= 1; i
< NSIG
; i
++) {
303 sigaction(i
, &act
, NULL
);
306 memset(sigact_table
, 0, sizeof(sigact_table
));
308 first_free
= &sigqueue_table
[0];
309 for(i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++)
310 sigqueue_table
[i
].next
= &sigqueue_table
[i
+ 1];
311 sigqueue_table
[MAX_SIGQUEUE_SIZE
- 1].next
= NULL
;
314 /* signal queue handling */
316 static inline struct sigqueue
*alloc_sigqueue(void)
318 struct sigqueue
*q
= first_free
;
321 first_free
= q
->next
;
325 static inline void free_sigqueue(struct sigqueue
*q
)
327 q
->next
= first_free
;
331 /* abort execution with signal */
332 void __attribute((noreturn
)) force_sig(int sig
)
335 host_sig
= target_to_host_signal(sig
);
336 fprintf(stderr
, "qemu: uncaught target signal %d (%s) - exiting\n",
337 sig
, strsignal(host_sig
));
342 struct sigaction act
;
343 sigemptyset(&act
.sa_mask
);
344 act
.sa_flags
= SA_SIGINFO
;
345 act
.sa_sigaction
= SIG_DFL
;
346 sigaction(SIGABRT
, &act
, NULL
);
352 /* queue a signal so that it will be send to the virtual CPU as soon
354 int queue_signal(int sig
, target_siginfo_t
*info
)
356 struct emulated_sigaction
*k
;
357 struct sigqueue
*q
, **pq
;
360 #if defined(DEBUG_SIGNAL)
361 fprintf(stderr
, "queue_signal: sig=%d\n",
364 k
= &sigact_table
[sig
- 1];
365 handler
= k
->sa
._sa_handler
;
366 if (handler
== TARGET_SIG_DFL
) {
367 /* default handler : ignore some signal. The other are fatal */
368 if (sig
!= TARGET_SIGCHLD
&&
369 sig
!= TARGET_SIGURG
&&
370 sig
!= TARGET_SIGWINCH
) {
373 return 0; /* indicate ignored */
375 } else if (handler
== TARGET_SIG_IGN
) {
378 } else if (handler
== TARGET_SIG_ERR
) {
382 if (sig
< TARGET_SIGRTMIN
) {
383 /* if non real time signal, we queue exactly one signal */
393 q
= alloc_sigqueue();
404 /* signal that a new signal is pending */
406 return 1; /* indicates that the signal was queued */
410 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
414 target_siginfo_t tinfo
;
416 /* the CPU emulator uses some host signals to detect exceptions,
417 we we forward to it some signals */
418 if (host_signum
== SIGSEGV
|| host_signum
== SIGBUS
) {
419 if (cpu_signal_handler(host_signum
, info
, puc
))
423 /* get target signal number */
424 sig
= host_to_target_signal(host_signum
);
425 if (sig
< 1 || sig
> TARGET_NSIG
)
427 #if defined(DEBUG_SIGNAL)
428 fprintf(stderr
, "qemu: got signal %d\n", sig
);
430 host_to_target_siginfo_noswap(&tinfo
, info
);
431 if (queue_signal(sig
, &tinfo
) == 1) {
432 /* interrupt the virtual CPU as soon as possible */
433 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
437 /* do_sigaltstack() returns target values and errnos. */
438 int do_sigaltstack(const struct target_sigaltstack
*uss
,
439 struct target_sigaltstack
*uoss
,
443 struct target_sigaltstack oss
;
445 /* XXX: test errors */
448 __put_user(target_sigaltstack_used
.ss_sp
, &oss
.ss_sp
);
449 __put_user(target_sigaltstack_used
.ss_size
, &oss
.ss_size
);
450 __put_user(sas_ss_flags(sp
), &oss
.ss_flags
);
455 struct target_sigaltstack ss
;
457 ret
= -TARGET_EFAULT
;
458 if (!access_ok(VERIFY_READ
, uss
, sizeof(*uss
))
459 || __get_user(ss
.ss_sp
, &uss
->ss_sp
)
460 || __get_user(ss
.ss_size
, &uss
->ss_size
)
461 || __get_user(ss
.ss_flags
, &uss
->ss_flags
))
465 if (on_sig_stack(sp
))
468 ret
= -TARGET_EINVAL
;
469 if (ss
.ss_flags
!= TARGET_SS_DISABLE
470 && ss
.ss_flags
!= TARGET_SS_ONSTACK
474 if (ss
.ss_flags
== TARGET_SS_DISABLE
) {
478 ret
= -TARGET_ENOMEM
;
479 if (ss
.ss_size
< MINSIGSTKSZ
)
483 target_sigaltstack_used
.ss_sp
= ss
.ss_sp
;
484 target_sigaltstack_used
.ss_size
= ss
.ss_size
;
488 ret
= -TARGET_EFAULT
;
489 if (!access_ok(VERIFY_WRITE
, uoss
, sizeof(oss
)))
491 memcpy(uoss
, &oss
, sizeof(oss
));
499 /* do_sigaction() return host values and errnos */
500 int do_sigaction(int sig
, const struct target_sigaction
*act
,
501 struct target_sigaction
*oact
)
503 struct emulated_sigaction
*k
;
504 struct sigaction act1
;
508 if (sig
< 1 || sig
> TARGET_NSIG
|| sig
== SIGKILL
|| sig
== SIGSTOP
)
510 k
= &sigact_table
[sig
- 1];
511 #if defined(DEBUG_SIGNAL)
512 fprintf(stderr
, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
513 sig
, (int)act
, (int)oact
);
516 oact
->_sa_handler
= tswapl(k
->sa
._sa_handler
);
517 oact
->sa_flags
= tswapl(k
->sa
.sa_flags
);
518 #if !defined(TARGET_MIPS)
519 oact
->sa_restorer
= tswapl(k
->sa
.sa_restorer
);
521 oact
->sa_mask
= k
->sa
.sa_mask
;
524 k
->sa
._sa_handler
= tswapl(act
->_sa_handler
);
525 k
->sa
.sa_flags
= tswapl(act
->sa_flags
);
526 #if !defined(TARGET_MIPS)
527 k
->sa
.sa_restorer
= tswapl(act
->sa_restorer
);
529 k
->sa
.sa_mask
= act
->sa_mask
;
531 /* we update the host linux signal state */
532 host_sig
= target_to_host_signal(sig
);
533 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
534 sigfillset(&act1
.sa_mask
);
535 act1
.sa_flags
= SA_SIGINFO
;
536 if (k
->sa
.sa_flags
& TARGET_SA_RESTART
)
537 act1
.sa_flags
|= SA_RESTART
;
538 /* NOTE: it is important to update the host kernel signal
539 ignore state to avoid getting unexpected interrupted
541 if (k
->sa
._sa_handler
== TARGET_SIG_IGN
) {
542 act1
.sa_sigaction
= (void *)SIG_IGN
;
543 } else if (k
->sa
._sa_handler
== TARGET_SIG_DFL
) {
544 act1
.sa_sigaction
= (void *)SIG_DFL
;
546 act1
.sa_sigaction
= host_signal_handler
;
548 ret
= sigaction(host_sig
, &act1
, NULL
);
555 #define offsetof(type, field) ((size_t) &((type *)0)->field)
558 static inline int copy_siginfo_to_user(target_siginfo_t
*tinfo
,
559 const target_siginfo_t
*info
)
561 tswap_siginfo(tinfo
, info
);
567 /* from the Linux kernel */
569 struct target_fpreg
{
570 uint16_t significand
[4];
574 struct target_fpxreg
{
575 uint16_t significand
[4];
580 struct target_xmmreg
{
581 abi_ulong element
[4];
584 struct target_fpstate
{
585 /* Regular FPU environment */
593 struct target_fpreg _st
[8];
595 uint16_t magic
; /* 0xffff = regular FPU data only */
597 /* FXSR FPU environment */
598 abi_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
601 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
602 struct target_xmmreg _xmm
[8];
603 abi_ulong padding
[56];
606 #define X86_FXSR_MAGIC 0x0000
608 struct target_sigcontext
{
626 abi_ulong esp_at_signal
;
628 abi_ulong fpstate
; /* pointer */
633 struct target_ucontext
{
636 target_stack_t tuc_stack
;
637 struct target_sigcontext tuc_mcontext
;
638 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
645 struct target_sigcontext sc
;
646 struct target_fpstate fpstate
;
647 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
657 struct target_siginfo info
;
658 struct target_ucontext uc
;
659 struct target_fpstate fpstate
;
664 * Set up a signal frame.
667 /* XXX: save x87 state */
669 setup_sigcontext(struct target_sigcontext
*sc
, struct target_fpstate
*fpstate
,
670 CPUX86State
*env
, unsigned long mask
)
674 err
|= __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
675 err
|= __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
676 err
|= __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
677 err
|= __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
678 err
|= __put_user(env
->regs
[R_EDI
], &sc
->edi
);
679 err
|= __put_user(env
->regs
[R_ESI
], &sc
->esi
);
680 err
|= __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
681 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp
);
682 err
|= __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
683 err
|= __put_user(env
->regs
[R_EDX
], &sc
->edx
);
684 err
|= __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
685 err
|= __put_user(env
->regs
[R_EAX
], &sc
->eax
);
686 err
|= __put_user(env
->exception_index
, &sc
->trapno
);
687 err
|= __put_user(env
->error_code
, &sc
->err
);
688 err
|= __put_user(env
->eip
, &sc
->eip
);
689 err
|= __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
690 err
|= __put_user(env
->eflags
, &sc
->eflags
);
691 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
692 err
|= __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
694 cpu_x86_fsave(env
, (void *)fpstate
, 1);
695 fpstate
->status
= fpstate
->sw
;
696 err
|= __put_user(0xffff, &fpstate
->magic
);
697 err
|= __put_user(fpstate
, &sc
->fpstate
);
699 /* non-iBCS2 extensions.. */
700 err
|= __put_user(mask
, &sc
->oldmask
);
701 err
|= __put_user(env
->cr
[2], &sc
->cr2
);
706 * Determine which stack to use..
710 get_sigframe(struct emulated_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
714 /* Default to using normal stack */
715 esp
= env
->regs
[R_ESP
];
716 /* This is the X/Open sanctioned signal stack switching. */
717 if (ka
->sa
.sa_flags
& TARGET_SA_ONSTACK
) {
718 if (sas_ss_flags(esp
) == 0)
719 esp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
722 /* This is the legacy signal stack switching. */
724 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
725 !(ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) &&
726 ka
->sa
.sa_restorer
) {
727 esp
= (unsigned long) ka
->sa
.sa_restorer
;
729 return g2h((esp
- frame_size
) & -8ul);
732 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
733 target_sigset_t
*set
, CPUX86State
*env
)
735 struct sigframe
*frame
;
738 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
740 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
742 err
|= __put_user((/*current->exec_domain
743 && current->exec_domain->signal_invmap
745 ? current->exec_domain->signal_invmap[sig]
751 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0]);
755 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
756 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
760 /* Set up to return from userspace. If provided, use a stub
761 already in userspace. */
762 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
763 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
765 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
766 /* This is popl %eax ; movl $,%eax ; int $0x80 */
767 err
|= __put_user(0xb858, (short *)(frame
->retcode
+0));
768 #if defined(TARGET_X86_64)
769 #warning "Fix this !"
771 err
|= __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
773 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+6));
779 /* Set up registers for signal handler */
780 env
->regs
[R_ESP
] = h2g(frame
);
781 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
783 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
784 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
785 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
786 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
787 env
->eflags
&= ~TF_MASK
;
792 if (sig
== TARGET_SIGSEGV
)
793 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
794 force_sig(TARGET_SIGSEGV
/* , current */);
797 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
798 target_siginfo_t
*info
,
799 target_sigset_t
*set
, CPUX86State
*env
)
801 struct rt_sigframe
*frame
;
804 frame
= get_sigframe(ka
, env
, sizeof(*frame
));
806 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
809 err
|= __put_user((/*current->exec_domain
810 && current->exec_domain->signal_invmap
812 ? current->exec_domain->signal_invmap[sig]
815 err
|= __put_user((abi_ulong
)&frame
->info
, &frame
->pinfo
);
816 err
|= __put_user((abi_ulong
)&frame
->uc
, &frame
->puc
);
817 err
|= copy_siginfo_to_user(&frame
->info
, info
);
821 /* Create the ucontext. */
822 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
823 err
|= __put_user(0, &frame
->uc
.tuc_link
);
824 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
825 &frame
->uc
.tuc_stack
.ss_sp
);
826 err
|= __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
827 &frame
->uc
.tuc_stack
.ss_flags
);
828 err
|= __put_user(target_sigaltstack_used
.ss_size
,
829 &frame
->uc
.tuc_stack
.ss_size
);
830 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, &frame
->fpstate
,
832 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
833 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
837 /* Set up to return from userspace. If provided, use a stub
838 already in userspace. */
839 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
840 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
842 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
843 /* This is movl $,%eax ; int $0x80 */
844 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
845 err
|= __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
846 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+5));
852 /* Set up registers for signal handler */
853 env
->regs
[R_ESP
] = (unsigned long) frame
;
854 env
->eip
= (unsigned long) ka
->sa
._sa_handler
;
856 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
857 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
858 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
859 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
860 env
->eflags
&= ~TF_MASK
;
865 if (sig
== TARGET_SIGSEGV
)
866 ka
->sa
._sa_handler
= TARGET_SIG_DFL
;
867 force_sig(TARGET_SIGSEGV
/* , current */);
871 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
, int *peax
)
873 unsigned int err
= 0;
875 cpu_x86_load_seg(env
, R_GS
, lduw(&sc
->gs
));
876 cpu_x86_load_seg(env
, R_FS
, lduw(&sc
->fs
));
877 cpu_x86_load_seg(env
, R_ES
, lduw(&sc
->es
));
878 cpu_x86_load_seg(env
, R_DS
, lduw(&sc
->ds
));
880 env
->regs
[R_EDI
] = ldl(&sc
->edi
);
881 env
->regs
[R_ESI
] = ldl(&sc
->esi
);
882 env
->regs
[R_EBP
] = ldl(&sc
->ebp
);
883 env
->regs
[R_ESP
] = ldl(&sc
->esp
);
884 env
->regs
[R_EBX
] = ldl(&sc
->ebx
);
885 env
->regs
[R_EDX
] = ldl(&sc
->edx
);
886 env
->regs
[R_ECX
] = ldl(&sc
->ecx
);
887 env
->eip
= ldl(&sc
->eip
);
889 cpu_x86_load_seg(env
, R_CS
, lduw(&sc
->cs
) | 3);
890 cpu_x86_load_seg(env
, R_SS
, lduw(&sc
->ss
) | 3);
893 unsigned int tmpflags
;
894 tmpflags
= ldl(&sc
->eflags
);
895 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
896 // regs->orig_eax = -1; /* disable syscall checks */
900 struct _fpstate
* buf
;
901 buf
= (void *)ldl(&sc
->fpstate
);
904 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
907 cpu_x86_frstor(env
, (void *)buf
, 1);
911 *peax
= ldl(&sc
->eax
);
919 long do_sigreturn(CPUX86State
*env
)
921 struct sigframe
*frame
= (struct sigframe
*)g2h(env
->regs
[R_ESP
] - 8);
922 target_sigset_t target_set
;
926 #if defined(DEBUG_SIGNAL)
927 fprintf(stderr
, "do_sigreturn\n");
929 /* set blocked signals */
930 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
932 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
933 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
937 target_to_host_sigset_internal(&set
, &target_set
);
938 sigprocmask(SIG_SETMASK
, &set
, NULL
);
940 /* restore registers */
941 if (restore_sigcontext(env
, &frame
->sc
, &eax
))
946 force_sig(TARGET_SIGSEGV
);
950 long do_rt_sigreturn(CPUX86State
*env
)
952 struct rt_sigframe
*frame
= (struct rt_sigframe
*)g2h(env
->regs
[R_ESP
] - 4);
957 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
960 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
961 sigprocmask(SIG_SETMASK
, &set
, NULL
);
963 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
, &eax
))
966 if (do_sigaltstack(&frame
->uc
.tuc_stack
, NULL
, get_sp_from_cpustate(env
)) == -EFAULT
)
972 force_sig(TARGET_SIGSEGV
);
976 #elif defined(TARGET_ARM)
978 struct target_sigcontext
{
980 abi_ulong error_code
;
999 abi_ulong fault_address
;
1002 struct target_ucontext
{
1003 abi_ulong tuc_flags
;
1005 target_stack_t tuc_stack
;
1006 struct target_sigcontext tuc_mcontext
;
1007 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1012 struct target_sigcontext sc
;
1013 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
1019 struct target_siginfo
*pinfo
;
1021 struct target_siginfo info
;
1022 struct target_ucontext uc
;
1026 #define TARGET_CONFIG_CPU_32 1
1029 * For ARM syscalls, we encode the syscall number into the instruction.
1031 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1032 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1035 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1036 * need two 16-bit instructions.
1038 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1039 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1041 static const abi_ulong retcodes
[4] = {
1042 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
1043 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
1047 #define __put_user_error(x,p,e) __put_user(x, p)
1048 #define __get_user_error(x,p,e) __get_user(x, p)
1050 static inline int valid_user_regs(CPUState
*regs
)
1056 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1057 CPUState
*env
, unsigned long mask
)
1061 __put_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
1062 __put_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
1063 __put_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1064 __put_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1065 __put_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1066 __put_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1067 __put_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1068 __put_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1069 __put_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1070 __put_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1071 __put_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1072 __put_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1073 __put_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1074 __put_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1075 __put_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1076 __put_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1077 #ifdef TARGET_CONFIG_CPU_32
1078 __put_user_error(cpsr_read(env
), &sc
->arm_cpsr
, err
);
1081 __put_user_error(/* current->thread.trap_no */ 0, &sc
->trap_no
, err
);
1082 __put_user_error(/* current->thread.error_code */ 0, &sc
->error_code
, err
);
1083 __put_user_error(/* current->thread.address */ 0, &sc
->fault_address
, err
);
1084 __put_user_error(mask
, &sc
->oldmask
, err
);
1089 static inline void *
1090 get_sigframe(struct emulated_sigaction
*ka
, CPUState
*regs
, int framesize
)
1092 unsigned long sp
= regs
->regs
[13];
1095 * This is the X/Open sanctioned signal stack switching.
1097 if ((ka
->sa
.sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
))
1098 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1100 * ATPCS B01 mandates 8-byte alignment
1102 return g2h((sp
- framesize
) & ~7);
1106 setup_return(CPUState
*env
, struct emulated_sigaction
*ka
,
1107 abi_ulong
*rc
, void *frame
, int usig
)
1109 abi_ulong handler
= (abi_ulong
)ka
->sa
._sa_handler
;
1112 #if defined(TARGET_CONFIG_CPU_32)
1114 abi_ulong cpsr
= env
->cpsr
;
1117 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1119 if (ka
->sa
.sa_flags
& SA_THIRTYTWO
)
1120 cpsr
= (cpsr
& ~MODE_MASK
) | USR_MODE
;
1122 #ifdef CONFIG_ARM_THUMB
1123 if (elf_hwcap
& HWCAP_THUMB
) {
1125 * The LSB of the handler determines if we're going to
1126 * be using THUMB or ARM mode for this signal handler.
1128 thumb
= handler
& 1;
1135 #endif /* CONFIG_ARM_THUMB */
1137 #endif /* TARGET_CONFIG_CPU_32 */
1139 if (ka
->sa
.sa_flags
& TARGET_SA_RESTORER
) {
1140 retcode
= (abi_ulong
)ka
->sa
.sa_restorer
;
1142 unsigned int idx
= thumb
;
1144 if (ka
->sa
.sa_flags
& TARGET_SA_SIGINFO
)
1147 if (__put_user(retcodes
[idx
], rc
))
1150 flush_icache_range((abi_ulong
)rc
,
1151 (abi_ulong
)(rc
+ 1));
1153 retcode
= ((abi_ulong
)rc
) + thumb
;
1156 env
->regs
[0] = usig
;
1157 env
->regs
[13] = h2g(frame
);
1158 env
->regs
[14] = retcode
;
1159 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
1162 #ifdef TARGET_CONFIG_CPU_32
1170 static void setup_frame(int usig
, struct emulated_sigaction
*ka
,
1171 target_sigset_t
*set
, CPUState
*regs
)
1173 struct sigframe
*frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
1176 err
|= setup_sigcontext(&frame
->sc
, /*&frame->fpstate,*/ regs
, set
->sig
[0]);
1178 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1179 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
1184 err
= setup_return(regs
, ka
, &frame
->retcode
, frame
, usig
);
1188 static void setup_rt_frame(int usig
, struct emulated_sigaction
*ka
,
1189 target_siginfo_t
*info
,
1190 target_sigset_t
*set
, CPUState
*env
)
1192 struct rt_sigframe
*frame
= get_sigframe(ka
, env
, sizeof(*frame
));
1193 struct target_sigaltstack stack
;
1196 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
1199 __put_user_error(&frame
->info
, (abi_ulong
*)&frame
->pinfo
, err
);
1200 __put_user_error(&frame
->uc
, (abi_ulong
*)&frame
->puc
, err
);
1201 err
|= copy_siginfo_to_user(&frame
->info
, info
);
1203 /* Clear all the bits of the ucontext we don't use. */
1204 memset(&frame
->uc
, 0, offsetof(struct target_ucontext
, tuc_mcontext
));
1206 memset(&stack
, 0, sizeof(stack
));
1207 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1208 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1209 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1210 if (!access_ok(VERIFY_WRITE
, &frame
->uc
.tuc_stack
, sizeof(stack
)))
1213 memcpy(&frame
->uc
.tuc_stack
, &stack
, sizeof(stack
));
1215 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, /*&frame->fpstate,*/
1217 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1218 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
1223 err
= setup_return(env
, ka
, &frame
->retcode
, frame
, usig
);
1227 * For realtime signals we must also set the second and third
1228 * arguments for the signal handler.
1229 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1231 env
->regs
[1] = (abi_ulong
)frame
->pinfo
;
1232 env
->regs
[2] = (abi_ulong
)frame
->puc
;
1239 restore_sigcontext(CPUState
*env
, struct target_sigcontext
*sc
)
1244 __get_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
1245 __get_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
1246 __get_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1247 __get_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1248 __get_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1249 __get_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1250 __get_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1251 __get_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1252 __get_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1253 __get_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1254 __get_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1255 __get_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1256 __get_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1257 __get_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1258 __get_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1259 __get_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1260 #ifdef TARGET_CONFIG_CPU_32
1261 __get_user_error(cpsr
, &sc
->arm_cpsr
, err
);
1262 cpsr_write(env
, cpsr
, 0xffffffff);
1265 err
|= !valid_user_regs(env
);
1270 long do_sigreturn(CPUState
*env
)
1272 struct sigframe
*frame
;
1273 target_sigset_t set
;
1278 * Since we stacked the signal on a 64-bit boundary,
1279 * then 'sp' should be word aligned here. If it's
1280 * not, then the user is trying to mess with us.
1282 if (env
->regs
[13] & 7)
1285 frame
= (struct sigframe
*)g2h(env
->regs
[13]);
1288 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
1291 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
1293 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1294 if (__get_user(set
.sig
[i
], &frame
->extramask
[i
- 1]))
1298 target_to_host_sigset_internal(&host_set
, &set
);
1299 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1301 if (restore_sigcontext(env
, &frame
->sc
))
1305 /* Send SIGTRAP if we're single-stepping */
1306 if (ptrace_cancel_bpt(current
))
1307 send_sig(SIGTRAP
, current
, 1);
1309 return env
->regs
[0];
1312 force_sig(SIGSEGV
/* , current */);
1316 long do_rt_sigreturn(CPUState
*env
)
1318 struct rt_sigframe
*frame
;
1322 * Since we stacked the signal on a 64-bit boundary,
1323 * then 'sp' should be word aligned here. If it's
1324 * not, then the user is trying to mess with us.
1326 if (env
->regs
[13] & 7)
1329 frame
= (struct rt_sigframe
*)env
->regs
[13];
1332 if (verify_area(VERIFY_READ
, frame
, sizeof (*frame
)))
1335 target_to_host_sigset(&host_set
, &frame
->uc
.tuc_sigmask
);
1336 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1338 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
))
1341 if (do_sigaltstack(&frame
->uc
.tuc_stack
, NULL
, get_sp_from_cpustate(env
)) == -EFAULT
)
1345 /* Send SIGTRAP if we're single-stepping */
1346 if (ptrace_cancel_bpt(current
))
1347 send_sig(SIGTRAP
, current
, 1);
1349 return env
->regs
[0];
1352 force_sig(SIGSEGV
/* , current */);
1356 #elif defined(TARGET_SPARC)
1358 #define __SUNOS_MAXWIN 31
1360 /* This is what SunOS does, so shall I. */
1361 struct target_sigcontext
{
1362 abi_ulong sigc_onstack
; /* state to restore */
1364 abi_ulong sigc_mask
; /* sigmask to restore */
1365 abi_ulong sigc_sp
; /* stack pointer */
1366 abi_ulong sigc_pc
; /* program counter */
1367 abi_ulong sigc_npc
; /* next program counter */
1368 abi_ulong sigc_psr
; /* for condition codes etc */
1369 abi_ulong sigc_g1
; /* User uses these two registers */
1370 abi_ulong sigc_o0
; /* within the trampoline code. */
1372 /* Now comes information regarding the users window set
1373 * at the time of the signal.
1375 abi_ulong sigc_oswins
; /* outstanding windows */
1377 /* stack ptrs for each regwin buf */
1378 char *sigc_spbuf
[__SUNOS_MAXWIN
];
1380 /* Windows to restore after signal */
1382 abi_ulong locals
[8];
1384 } sigc_wbuf
[__SUNOS_MAXWIN
];
1386 /* A Sparc stack frame */
1387 struct sparc_stackf
{
1388 abi_ulong locals
[8];
1390 struct sparc_stackf
*fp
;
1391 abi_ulong callers_pc
;
1394 abi_ulong xxargs
[1];
1403 abi_ulong u_regs
[16]; /* globals and ins */
1409 unsigned long si_float_regs
[32];
1410 unsigned long si_fsr
;
1411 unsigned long si_fpqdepth
;
1413 unsigned long *insn_addr
;
1416 } qemu_siginfo_fpu_t
;
1419 struct target_signal_frame
{
1420 struct sparc_stackf ss
;
1422 qemu_siginfo_fpu_t
*fpu_save
;
1423 abi_ulong insns
[2] __attribute__ ((aligned (8)));
1424 abi_ulong extramask
[TARGET_NSIG_WORDS
- 1];
1425 abi_ulong extra_size
; /* Should be 0 */
1426 qemu_siginfo_fpu_t fpu_state
;
1428 struct target_rt_signal_frame
{
1429 struct sparc_stackf ss
;
1433 qemu_siginfo_fpu_t
*fpu_save
;
1434 unsigned int insns
[2];
1436 unsigned int extra_size
; /* Should be 0 */
1437 qemu_siginfo_fpu_t fpu_state
;
1451 #define UREG_FP UREG_I6
1452 #define UREG_SP UREG_O6
1454 static inline void *get_sigframe(struct emulated_sigaction
*sa
, CPUState
*env
, unsigned long framesize
)
1458 sp
= env
->regwptr
[UREG_FP
];
1460 /* This is the X/Open sanctioned signal stack switching. */
1461 if (sa
->sa
.sa_flags
& TARGET_SA_ONSTACK
) {
1462 if (!on_sig_stack(sp
)
1463 && !((target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
) & 7))
1464 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1466 return g2h(sp
- framesize
);
1470 setup___siginfo(__siginfo_t
*si
, CPUState
*env
, abi_ulong mask
)
1474 err
|= __put_user(env
->psr
, &si
->si_regs
.psr
);
1475 err
|= __put_user(env
->pc
, &si
->si_regs
.pc
);
1476 err
|= __put_user(env
->npc
, &si
->si_regs
.npc
);
1477 err
|= __put_user(env
->y
, &si
->si_regs
.y
);
1478 for (i
=0; i
< 8; i
++) {
1479 err
|= __put_user(env
->gregs
[i
], &si
->si_regs
.u_regs
[i
]);
1481 for (i
=0; i
< 8; i
++) {
1482 err
|= __put_user(env
->regwptr
[UREG_I0
+ i
], &si
->si_regs
.u_regs
[i
+8]);
1484 err
|= __put_user(mask
, &si
->si_mask
);
1490 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1491 CPUState
*env
, unsigned long mask
)
1495 err
|= __put_user(mask
, &sc
->sigc_mask
);
1496 err
|= __put_user(env
->regwptr
[UREG_SP
], &sc
->sigc_sp
);
1497 err
|= __put_user(env
->pc
, &sc
->sigc_pc
);
1498 err
|= __put_user(env
->npc
, &sc
->sigc_npc
);
1499 err
|= __put_user(env
->psr
, &sc
->sigc_psr
);
1500 err
|= __put_user(env
->gregs
[1], &sc
->sigc_g1
);
1501 err
|= __put_user(env
->regwptr
[UREG_O0
], &sc
->sigc_o0
);
1506 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1508 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
1509 target_sigset_t
*set
, CPUState
*env
)
1511 struct target_signal_frame
*sf
;
1512 int sigframe_size
, err
, i
;
1514 /* 1. Make sure everything is clean */
1515 //synchronize_user_stack();
1517 sigframe_size
= NF_ALIGNEDSZ
;
1519 sf
= (struct target_signal_frame
*)
1520 get_sigframe(ka
, env
, sigframe_size
);
1522 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1524 if (invalid_frame_pointer(sf
, sigframe_size
))
1525 goto sigill_and_return
;
1527 /* 2. Save the current process state */
1528 err
= setup___siginfo(&sf
->info
, env
, set
->sig
[0]);
1529 err
|= __put_user(0, &sf
->extra_size
);
1531 //err |= save_fpu_state(regs, &sf->fpu_state);
1532 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1534 err
|= __put_user(set
->sig
[0], &sf
->info
.si_mask
);
1535 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
1536 err
|= __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
1539 for (i
= 0; i
< 8; i
++) {
1540 err
|= __put_user(env
->regwptr
[i
+ UREG_L0
], &sf
->ss
.locals
[i
]);
1542 for (i
= 0; i
< 8; i
++) {
1543 err
|= __put_user(env
->regwptr
[i
+ UREG_I0
], &sf
->ss
.ins
[i
]);
1548 /* 3. signal handler back-trampoline and parameters */
1549 env
->regwptr
[UREG_FP
] = h2g(sf
);
1550 env
->regwptr
[UREG_I0
] = sig
;
1551 env
->regwptr
[UREG_I1
] = h2g(&sf
->info
);
1552 env
->regwptr
[UREG_I2
] = h2g(&sf
->info
);
1554 /* 4. signal handler */
1555 env
->pc
= (unsigned long) ka
->sa
._sa_handler
;
1556 env
->npc
= (env
->pc
+ 4);
1557 /* 5. return to kernel instructions */
1558 if (ka
->sa
.sa_restorer
)
1559 env
->regwptr
[UREG_I7
] = (unsigned long)ka
->sa
.sa_restorer
;
1561 env
->regwptr
[UREG_I7
] = h2g(&(sf
->insns
[0]) - 2);
1563 /* mov __NR_sigreturn, %g1 */
1564 err
|= __put_user(0x821020d8, &sf
->insns
[0]);
1567 err
|= __put_user(0x91d02010, &sf
->insns
[1]);
1571 /* Flush instruction space. */
1572 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1577 //sigill_and_return:
1578 force_sig(TARGET_SIGILL
);
1580 //fprintf(stderr, "force_sig\n");
1581 force_sig(TARGET_SIGSEGV
);
1584 restore_fpu_state(CPUState
*env
, qemu_siginfo_fpu_t
*fpu
)
1589 if (current
->flags
& PF_USEDFPU
)
1590 regs
->psr
&= ~PSR_EF
;
1592 if (current
== last_task_used_math
) {
1593 last_task_used_math
= 0;
1594 regs
->psr
&= ~PSR_EF
;
1597 current
->used_math
= 1;
1598 current
->flags
&= ~PF_USEDFPU
;
1601 if (verify_area (VERIFY_READ
, fpu
, sizeof(*fpu
)))
1606 /* XXX: incorrect */
1607 err
= __copy_from_user(&env
->fpr
[0], &fpu
->si_float_regs
[0],
1608 (sizeof(unsigned long) * 32));
1610 err
|= __get_user(env
->fsr
, &fpu
->si_fsr
);
1612 err
|= __get_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
1613 if (current
->thread
.fpqdepth
!= 0)
1614 err
|= __copy_from_user(¤t
->thread
.fpqueue
[0],
1615 &fpu
->si_fpqueue
[0],
1616 ((sizeof(unsigned long) +
1617 (sizeof(unsigned long *)))*16));
1623 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
1624 target_siginfo_t
*info
,
1625 target_sigset_t
*set
, CPUState
*env
)
1627 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1630 long do_sigreturn(CPUState
*env
)
1632 struct target_signal_frame
*sf
;
1633 uint32_t up_psr
, pc
, npc
;
1634 target_sigset_t set
;
1639 sf
= (struct target_signal_frame
*)g2h(env
->regwptr
[UREG_FP
]);
1641 fprintf(stderr
, "sigreturn\n");
1642 fprintf(stderr
, "sf: %x pc %x fp %x sp %x\n", sf
, env
->pc
, env
->regwptr
[UREG_FP
], env
->regwptr
[UREG_SP
]);
1644 //cpu_dump_state(env, stderr, fprintf, 0);
1646 /* 1. Make sure we are not getting garbage from the user */
1648 if (verify_area (VERIFY_READ
, sf
, sizeof (*sf
)))
1652 if (((uint
) sf
) & 3)
1655 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
1656 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
1661 /* 2. Restore the state */
1662 err
|= __get_user(up_psr
, &sf
->info
.si_regs
.psr
);
1664 /* User can only change condition codes and FPU enabling in %psr. */
1665 env
->psr
= (up_psr
& (PSR_ICC
/* | PSR_EF */))
1666 | (env
->psr
& ~(PSR_ICC
/* | PSR_EF */));
1670 err
|= __get_user(env
->y
, &sf
->info
.si_regs
.y
);
1671 for (i
=0; i
< 8; i
++) {
1672 err
|= __get_user(env
->gregs
[i
], &sf
->info
.si_regs
.u_regs
[i
]);
1674 for (i
=0; i
< 8; i
++) {
1675 err
|= __get_user(env
->regwptr
[i
+ UREG_I0
], &sf
->info
.si_regs
.u_regs
[i
+8]);
1678 err
|= __get_user(fpu_save
, (abi_ulong
*)&sf
->fpu_save
);
1681 // err |= restore_fpu_state(env, fpu_save);
1683 /* This is pretty much atomic, no amount locking would prevent
1684 * the races which exist anyways.
1686 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
1687 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1688 err
|= (__get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]));
1691 target_to_host_sigset_internal(&host_set
, &set
);
1692 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1697 return env
->regwptr
[0];
1700 force_sig(TARGET_SIGSEGV
);
1703 long do_rt_sigreturn(CPUState
*env
)
1705 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
1709 #ifdef TARGET_SPARC64
1731 typedef abi_ulong target_mc_greg_t
;
1732 typedef target_mc_greg_t target_mc_gregset_t
[MC_NGREG
];
1734 struct target_mc_fq
{
1735 abi_ulong
*mcfq_addr
;
1739 struct target_mc_fpu
{
1743 //uint128_t qregs[16];
1745 abi_ulong mcfpu_fsr
;
1746 abi_ulong mcfpu_fprs
;
1747 abi_ulong mcfpu_gsr
;
1748 struct target_mc_fq
*mcfpu_fq
;
1749 unsigned char mcfpu_qcnt
;
1750 unsigned char mcfpu_qentsz
;
1751 unsigned char mcfpu_enab
;
1753 typedef struct target_mc_fpu target_mc_fpu_t
;
1756 target_mc_gregset_t mc_gregs
;
1757 target_mc_greg_t mc_fp
;
1758 target_mc_greg_t mc_i7
;
1759 target_mc_fpu_t mc_fpregs
;
1760 } target_mcontext_t
;
1762 struct target_ucontext
{
1763 struct target_ucontext
*uc_link
;
1765 target_sigset_t uc_sigmask
;
1766 target_mcontext_t uc_mcontext
;
1769 /* A V9 register window */
1770 struct target_reg_window
{
1771 abi_ulong locals
[8];
1775 #define TARGET_STACK_BIAS 2047
1777 /* {set, get}context() needed for 64-bit SparcLinux userland. */
1778 void sparc64_set_context(CPUSPARCState
*env
)
1780 struct target_ucontext
*ucp
= (struct target_ucontext
*)
1781 env
->regwptr
[UREG_I0
];
1782 target_mc_gregset_t
*grp
;
1783 abi_ulong pc
, npc
, tstate
;
1785 unsigned char fenab
;
1788 abi_ulong
*src
, *dst
;
1790 grp
= &ucp
->uc_mcontext
.mc_gregs
;
1791 err
= get_user(pc
, &((*grp
)[MC_PC
]));
1792 err
|= get_user(npc
, &((*grp
)[MC_NPC
]));
1793 if (err
|| ((pc
| npc
) & 3))
1795 if (env
->regwptr
[UREG_I1
]) {
1796 target_sigset_t target_set
;
1799 if (TARGET_NSIG_WORDS
== 1) {
1800 if (get_user(target_set
.sig
[0], &ucp
->uc_sigmask
.sig
[0]))
1803 src
= &ucp
->uc_sigmask
;
1805 for (i
= 0; i
< sizeof(target_sigset_t
) / sizeof(abi_ulong
);
1807 err
|= get_user(dst
, src
);
1811 target_to_host_sigset_internal(&set
, &target_set
);
1812 sigprocmask(SIG_SETMASK
, &set
, NULL
);
1816 err
|= get_user(env
->y
, &((*grp
)[MC_Y
]));
1817 err
|= get_user(tstate
, &((*grp
)[MC_TSTATE
]));
1818 env
->asi
= (tstate
>> 24) & 0xff;
1819 PUT_CCR(env
, tstate
>> 32);
1820 PUT_CWP64(env
, tstate
& 0x1f);
1821 err
|= get_user(env
->gregs
[1], (&(*grp
)[MC_G1
]));
1822 err
|= get_user(env
->gregs
[2], (&(*grp
)[MC_G2
]));
1823 err
|= get_user(env
->gregs
[3], (&(*grp
)[MC_G3
]));
1824 err
|= get_user(env
->gregs
[4], (&(*grp
)[MC_G4
]));
1825 err
|= get_user(env
->gregs
[5], (&(*grp
)[MC_G5
]));
1826 err
|= get_user(env
->gregs
[6], (&(*grp
)[MC_G6
]));
1827 err
|= get_user(env
->gregs
[7], (&(*grp
)[MC_G7
]));
1828 err
|= get_user(env
->regwptr
[UREG_I0
], (&(*grp
)[MC_O0
]));
1829 err
|= get_user(env
->regwptr
[UREG_I1
], (&(*grp
)[MC_O1
]));
1830 err
|= get_user(env
->regwptr
[UREG_I2
], (&(*grp
)[MC_O2
]));
1831 err
|= get_user(env
->regwptr
[UREG_I3
], (&(*grp
)[MC_O3
]));
1832 err
|= get_user(env
->regwptr
[UREG_I4
], (&(*grp
)[MC_O4
]));
1833 err
|= get_user(env
->regwptr
[UREG_I5
], (&(*grp
)[MC_O5
]));
1834 err
|= get_user(env
->regwptr
[UREG_I6
], (&(*grp
)[MC_O6
]));
1835 err
|= get_user(env
->regwptr
[UREG_I7
], (&(*grp
)[MC_O7
]));
1837 err
|= get_user(fp
, &(ucp
->uc_mcontext
.mc_fp
));
1838 err
|= get_user(i7
, &(ucp
->uc_mcontext
.mc_i7
));
1840 (&(((struct target_reg_window
*)(TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
]))->ins
[6])));
1842 (&(((struct target_reg_window
*)(TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
]))->ins
[7])));
1844 err
|= get_user(fenab
, &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_enab
));
1845 err
|= get_user(env
->fprs
, &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fprs
));
1846 src
= &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fregs
);
1848 for (i
= 0; i
< 64; i
++, dst
++, src
++)
1849 err
|= get_user(dst
, src
);
1850 err
|= get_user(env
->fsr
,
1851 &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fsr
));
1852 err
|= get_user(env
->gsr
,
1853 &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_gsr
));
1862 void sparc64_get_context(CPUSPARCState
*env
)
1864 struct target_ucontext
*ucp
= (struct target_ucontext
*)
1865 env
->regwptr
[UREG_I0
];
1866 target_mc_gregset_t
*grp
;
1867 target_mcontext_t
*mcp
;
1871 abi_ulong
*src
, *dst
;
1872 target_sigset_t target_set
;
1875 mcp
= &ucp
->uc_mcontext
;
1876 grp
= &mcp
->mc_gregs
;
1878 /* Skip over the trap instruction, first. */
1884 sigprocmask(0, NULL
, &set
);
1885 host_to_target_sigset_internal(&target_set
, &set
);
1886 if (TARGET_NSIG_WORDS
== 1)
1887 err
|= put_user(target_set
.sig
[0],
1888 (abi_ulong
*)&ucp
->uc_sigmask
);
1891 dst
= &ucp
->uc_sigmask
;
1892 for (i
= 0; i
< sizeof(target_sigset_t
) / sizeof(abi_ulong
);
1894 err
|= put_user(src
, dst
);
1899 err
|= put_user(env
->tstate
, &((*grp
)[MC_TSTATE
]));
1900 err
|= put_user(env
->pc
, &((*grp
)[MC_PC
]));
1901 err
|= put_user(env
->npc
, &((*grp
)[MC_NPC
]));
1902 err
|= put_user(env
->y
, &((*grp
)[MC_Y
]));
1903 err
|= put_user(env
->gregs
[1], &((*grp
)[MC_G1
]));
1904 err
|= put_user(env
->gregs
[2], &((*grp
)[MC_G2
]));
1905 err
|= put_user(env
->gregs
[3], &((*grp
)[MC_G3
]));
1906 err
|= put_user(env
->gregs
[4], &((*grp
)[MC_G4
]));
1907 err
|= put_user(env
->gregs
[5], &((*grp
)[MC_G5
]));
1908 err
|= put_user(env
->gregs
[6], &((*grp
)[MC_G6
]));
1909 err
|= put_user(env
->gregs
[7], &((*grp
)[MC_G7
]));
1910 err
|= put_user(env
->regwptr
[UREG_I0
], &((*grp
)[MC_O0
]));
1911 err
|= put_user(env
->regwptr
[UREG_I1
], &((*grp
)[MC_O1
]));
1912 err
|= put_user(env
->regwptr
[UREG_I2
], &((*grp
)[MC_O2
]));
1913 err
|= put_user(env
->regwptr
[UREG_I3
], &((*grp
)[MC_O3
]));
1914 err
|= put_user(env
->regwptr
[UREG_I4
], &((*grp
)[MC_O4
]));
1915 err
|= put_user(env
->regwptr
[UREG_I5
], &((*grp
)[MC_O5
]));
1916 err
|= put_user(env
->regwptr
[UREG_I6
], &((*grp
)[MC_O6
]));
1917 err
|= put_user(env
->regwptr
[UREG_I7
], &((*grp
)[MC_O7
]));
1920 (&(((struct target_reg_window
*)(TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
]))->ins
[6])));
1922 (&(((struct target_reg_window
*)(TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
]))->ins
[7])));
1923 err
|= put_user(fp
, &(mcp
->mc_fp
));
1924 err
|= put_user(i7
, &(mcp
->mc_i7
));
1927 dst
= &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fregs
);
1928 for (i
= 0; i
< 64; i
++, dst
++, src
++)
1929 err
|= put_user(src
, dst
);
1930 err
|= put_user(env
->fsr
, &(mcp
->mc_fpregs
.mcfpu_fsr
));
1931 err
|= put_user(env
->gsr
, &(mcp
->mc_fpregs
.mcfpu_gsr
));
1932 err
|= put_user(env
->fprs
, &(mcp
->mc_fpregs
.mcfpu_fprs
));
1942 #elif defined(TARGET_ABI_MIPSN64)
1944 # warning signal handling not implemented
1946 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
1947 target_sigset_t
*set
, CPUState
*env
)
1949 fprintf(stderr
, "setup_frame: not implemented\n");
1952 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
1953 target_siginfo_t
*info
,
1954 target_sigset_t
*set
, CPUState
*env
)
1956 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1959 long do_sigreturn(CPUState
*env
)
1961 fprintf(stderr
, "do_sigreturn: not implemented\n");
1965 long do_rt_sigreturn(CPUState
*env
)
1967 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
1971 #elif defined(TARGET_ABI_MIPSN32)
1973 # warning signal handling not implemented
1975 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
1976 target_sigset_t
*set
, CPUState
*env
)
1978 fprintf(stderr
, "setup_frame: not implemented\n");
1981 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
1982 target_siginfo_t
*info
,
1983 target_sigset_t
*set
, CPUState
*env
)
1985 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1988 long do_sigreturn(CPUState
*env
)
1990 fprintf(stderr
, "do_sigreturn: not implemented\n");
1994 long do_rt_sigreturn(CPUState
*env
)
1996 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2000 #elif defined(TARGET_ABI_MIPSO32)
2002 struct target_sigcontext
{
2003 uint32_t sc_regmask
; /* Unused */
2006 uint64_t sc_regs
[32];
2007 uint64_t sc_fpregs
[32];
2008 uint32_t sc_ownedfp
; /* Unused */
2009 uint32_t sc_fpc_csr
;
2010 uint32_t sc_fpc_eir
; /* Unused */
2011 uint32_t sc_used_math
;
2012 uint32_t sc_dsp
; /* dsp status, was sc_ssflags */
2015 target_ulong sc_hi1
; /* Was sc_cause */
2016 target_ulong sc_lo1
; /* Was sc_badvaddr */
2017 target_ulong sc_hi2
; /* Was sc_sigset[4] */
2018 target_ulong sc_lo2
;
2019 target_ulong sc_hi3
;
2020 target_ulong sc_lo3
;
2024 uint32_t sf_ass
[4]; /* argument save space for o32 */
2025 uint32_t sf_code
[2]; /* signal trampoline */
2026 struct target_sigcontext sf_sc
;
2027 target_sigset_t sf_mask
;
2030 /* Install trampoline to jump back from signal handler */
2031 static inline int install_sigtramp(unsigned int *tramp
, unsigned int syscall
)
2036 * Set up the return code ...
2038 * li v0, __NR__foo_sigreturn
2042 err
= __put_user(0x24020000 + syscall
, tramp
+ 0);
2043 err
|= __put_user(0x0000000c , tramp
+ 1);
2044 /* flush_cache_sigtramp((unsigned long) tramp); */
2049 setup_sigcontext(CPUState
*regs
, struct target_sigcontext
*sc
)
2053 err
|= __put_user(regs
->PC
[regs
->current_tc
], &sc
->sc_pc
);
2055 #define save_gp_reg(i) do { \
2056 err |= __put_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
2058 __put_user(0, &sc
->sc_regs
[0]); save_gp_reg(1); save_gp_reg(2);
2059 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2060 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2061 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2062 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2063 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2064 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2065 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2069 err
|= __put_user(regs
->HI
[0][regs
->current_tc
], &sc
->sc_mdhi
);
2070 err
|= __put_user(regs
->LO
[0][regs
->current_tc
], &sc
->sc_mdlo
);
2072 /* Not used yet, but might be useful if we ever have DSP suppport */
2075 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
2076 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
2077 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
2078 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
2079 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
2080 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
2081 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
2083 /* same with 64 bit */
2085 err
|= __put_user(regs
->hi
, &sc
->sc_hi
[0]);
2086 err
|= __put_user(regs
->lo
, &sc
->sc_lo
[0]);
2088 err
|= __put_user(mfhi1(), &sc
->sc_hi
[1]);
2089 err
|= __put_user(mflo1(), &sc
->sc_lo
[1]);
2090 err
|= __put_user(mfhi2(), &sc
->sc_hi
[2]);
2091 err
|= __put_user(mflo2(), &sc
->sc_lo
[2]);
2092 err
|= __put_user(mfhi3(), &sc
->sc_hi
[3]);
2093 err
|= __put_user(mflo3(), &sc
->sc_lo
[3]);
2094 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
2100 err
|= __put_user(!!used_math(), &sc
->sc_used_math
);
2106 * Save FPU state to signal context. Signal handler will "inherit"
2107 * current FPU state.
2111 if (!is_fpu_owner()) {
2113 restore_fp(current
);
2115 err
|= save_fp_context(sc
);
2124 restore_sigcontext(CPUState
*regs
, struct target_sigcontext
*sc
)
2128 err
|= __get_user(regs
->CP0_EPC
, &sc
->sc_pc
);
2130 err
|= __get_user(regs
->HI
[0][regs
->current_tc
], &sc
->sc_mdhi
);
2131 err
|= __get_user(regs
->LO
[0][regs
->current_tc
], &sc
->sc_mdlo
);
2133 #define restore_gp_reg(i) do { \
2134 err |= __get_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
2136 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2137 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2138 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2139 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2140 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2141 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2142 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2143 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2144 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2145 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2147 #undef restore_gp_reg
2151 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
2152 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
2153 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
2154 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
2155 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
2156 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
2157 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
2160 err
|= __get_user(regs
->hi
, &sc
->sc_hi
[0]);
2161 err
|= __get_user(regs
->lo
, &sc
->sc_lo
[0]);
2163 err
|= __get_user(treg
, &sc
->sc_hi
[1]); mthi1(treg
);
2164 err
|= __get_user(treg
, &sc
->sc_lo
[1]); mthi1(treg
);
2165 err
|= __get_user(treg
, &sc
->sc_hi
[2]); mthi2(treg
);
2166 err
|= __get_user(treg
, &sc
->sc_lo
[2]); mthi2(treg
);
2167 err
|= __get_user(treg
, &sc
->sc_hi
[3]); mthi3(treg
);
2168 err
|= __get_user(treg
, &sc
->sc_lo
[3]); mthi3(treg
);
2169 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
2173 err
|= __get_user(used_math
, &sc
->sc_used_math
);
2174 conditional_used_math(used_math
);
2179 /* restore fpu context if we have used it before */
2181 err
|= restore_fp_context(sc
);
2183 /* signal handler may have used FPU. Give it up. */
2192 * Determine which stack to use..
2194 static inline void *
2195 get_sigframe(struct emulated_sigaction
*ka
, CPUState
*regs
, size_t frame_size
)
2199 /* Default to using normal stack */
2200 sp
= regs
->gpr
[29][regs
->current_tc
];
2203 * FPU emulator may have it's own trampoline active just
2204 * above the user stack, 16-bytes before the next lowest
2205 * 16 byte boundary. Try to avoid trashing it.
2209 /* This is the X/Open sanctioned signal stack switching. */
2210 if ((ka
->sa
.sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
2211 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2214 return g2h((sp
- frame_size
) & ~7);
2217 static void setup_frame(int sig
, struct emulated_sigaction
* ka
,
2218 target_sigset_t
*set
, CPUState
*regs
)
2220 struct sigframe
*frame
;
2223 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
2224 if (!access_ok(VERIFY_WRITE
, frame
, sizeof (*frame
)))
2227 install_sigtramp(frame
->sf_code
, TARGET_NR_sigreturn
);
2229 if(setup_sigcontext(regs
, &frame
->sf_sc
))
2232 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2233 if(__put_user(set
->sig
[i
], &frame
->sf_mask
.sig
[i
]))
2238 * Arguments to signal handler:
2240 * a0 = signal number
2241 * a1 = 0 (should be cause)
2242 * a2 = pointer to struct sigcontext
2244 * $25 and PC point to the signal handler, $29 points to the
2247 regs
->gpr
[ 4][regs
->current_tc
] = sig
;
2248 regs
->gpr
[ 5][regs
->current_tc
] = 0;
2249 regs
->gpr
[ 6][regs
->current_tc
] = h2g(&frame
->sf_sc
);
2250 regs
->gpr
[29][regs
->current_tc
] = h2g(frame
);
2251 regs
->gpr
[31][regs
->current_tc
] = h2g(frame
->sf_code
);
2252 /* The original kernel code sets CP0_EPC to the handler
2253 * since it returns to userland using eret
2254 * we cannot do this here, and we must set PC directly */
2255 regs
->PC
[regs
->current_tc
] = regs
->gpr
[25][regs
->current_tc
] = ka
->sa
._sa_handler
;
2259 force_sig(TARGET_SIGSEGV
/*, current*/);
2263 long do_sigreturn(CPUState
*regs
)
2265 struct sigframe
*frame
;
2267 target_sigset_t target_set
;
2270 #if defined(DEBUG_SIGNAL)
2271 fprintf(stderr
, "do_sigreturn\n");
2273 frame
= (struct sigframe
*) regs
->gpr
[29][regs
->current_tc
];
2274 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
2277 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2278 if(__get_user(target_set
.sig
[i
], &frame
->sf_mask
.sig
[i
]))
2282 target_to_host_sigset_internal(&blocked
, &target_set
);
2283 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
2285 if (restore_sigcontext(regs
, &frame
->sf_sc
))
2290 * Don't let your children do this ...
2292 __asm__
__volatile__(
2300 regs
->PC
[regs
->current_tc
] = regs
->CP0_EPC
;
2301 /* I am not sure this is right, but it seems to work
2302 * maybe a problem with nested signals ? */
2307 force_sig(TARGET_SIGSEGV
/*, current*/);
2311 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
2312 target_siginfo_t
*info
,
2313 target_sigset_t
*set
, CPUState
*env
)
2315 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2318 long do_rt_sigreturn(CPUState
*env
)
2320 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2326 static void setup_frame(int sig
, struct emulated_sigaction
*ka
,
2327 target_sigset_t
*set
, CPUState
*env
)
2329 fprintf(stderr
, "setup_frame: not implemented\n");
2332 static void setup_rt_frame(int sig
, struct emulated_sigaction
*ka
,
2333 target_siginfo_t
*info
,
2334 target_sigset_t
*set
, CPUState
*env
)
2336 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2339 long do_sigreturn(CPUState
*env
)
2341 fprintf(stderr
, "do_sigreturn: not implemented\n");
2345 long do_rt_sigreturn(CPUState
*env
)
2347 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2353 void process_pending_signals(void *cpu_env
)
2357 sigset_t set
, old_set
;
2358 target_sigset_t target_old_set
;
2359 struct emulated_sigaction
*k
;
2362 if (!signal_pending
)
2366 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
2371 /* if no signal is pending, just return */
2377 fprintf(stderr
, "qemu: process signal %d\n", sig
);
2379 /* dequeue signal */
2385 sig
= gdb_handlesig (cpu_env
, sig
);
2387 fprintf (stderr
, "Lost signal\n");
2391 handler
= k
->sa
._sa_handler
;
2392 if (handler
== TARGET_SIG_DFL
) {
2393 /* default handler : ignore some signal. The other are fatal */
2394 if (sig
!= TARGET_SIGCHLD
&&
2395 sig
!= TARGET_SIGURG
&&
2396 sig
!= TARGET_SIGWINCH
) {
2399 } else if (handler
== TARGET_SIG_IGN
) {
2401 } else if (handler
== TARGET_SIG_ERR
) {
2404 /* compute the blocked signals during the handler execution */
2405 target_to_host_sigset(&set
, &k
->sa
.sa_mask
);
2406 /* SA_NODEFER indicates that the current signal should not be
2407 blocked during the handler */
2408 if (!(k
->sa
.sa_flags
& TARGET_SA_NODEFER
))
2409 sigaddset(&set
, target_to_host_signal(sig
));
2411 /* block signals in the handler using Linux */
2412 sigprocmask(SIG_BLOCK
, &set
, &old_set
);
2413 /* save the previous blocked signal state to restore it at the
2414 end of the signal execution (see do_sigreturn) */
2415 host_to_target_sigset_internal(&target_old_set
, &old_set
);
2417 /* if the CPU is in VM86 mode, we restore the 32 bit values */
2418 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
2420 CPUX86State
*env
= cpu_env
;
2421 if (env
->eflags
& VM_MASK
)
2422 save_v86_state(env
);
2425 /* prepare the stack frame of the virtual CPU */
2426 if (k
->sa
.sa_flags
& TARGET_SA_SIGINFO
)
2427 setup_rt_frame(sig
, k
, &q
->info
, &target_old_set
, cpu_env
);
2429 setup_frame(sig
, k
, &target_old_set
, cpu_env
);
2430 if (k
->sa
.sa_flags
& TARGET_SA_RESETHAND
)
2431 k
->sa
._sa_handler
= TARGET_SIG_DFL
;