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 static struct target_sigaltstack target_sigaltstack_used
= {
37 .ss_flags
= TARGET_SS_DISABLE
,
40 static struct target_sigaction sigact_table
[TARGET_NSIG
];
42 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
45 static uint8_t host_to_target_signal_table
[65] = {
46 [SIGHUP
] = TARGET_SIGHUP
,
47 [SIGINT
] = TARGET_SIGINT
,
48 [SIGQUIT
] = TARGET_SIGQUIT
,
49 [SIGILL
] = TARGET_SIGILL
,
50 [SIGTRAP
] = TARGET_SIGTRAP
,
51 [SIGABRT
] = TARGET_SIGABRT
,
52 /* [SIGIOT] = TARGET_SIGIOT,*/
53 [SIGBUS
] = TARGET_SIGBUS
,
54 [SIGFPE
] = TARGET_SIGFPE
,
55 [SIGKILL
] = TARGET_SIGKILL
,
56 [SIGUSR1
] = TARGET_SIGUSR1
,
57 [SIGSEGV
] = TARGET_SIGSEGV
,
58 [SIGUSR2
] = TARGET_SIGUSR2
,
59 [SIGPIPE
] = TARGET_SIGPIPE
,
60 [SIGALRM
] = TARGET_SIGALRM
,
61 [SIGTERM
] = TARGET_SIGTERM
,
63 [SIGSTKFLT
] = TARGET_SIGSTKFLT
,
65 [SIGCHLD
] = TARGET_SIGCHLD
,
66 [SIGCONT
] = TARGET_SIGCONT
,
67 [SIGSTOP
] = TARGET_SIGSTOP
,
68 [SIGTSTP
] = TARGET_SIGTSTP
,
69 [SIGTTIN
] = TARGET_SIGTTIN
,
70 [SIGTTOU
] = TARGET_SIGTTOU
,
71 [SIGURG
] = TARGET_SIGURG
,
72 [SIGXCPU
] = TARGET_SIGXCPU
,
73 [SIGXFSZ
] = TARGET_SIGXFSZ
,
74 [SIGVTALRM
] = TARGET_SIGVTALRM
,
75 [SIGPROF
] = TARGET_SIGPROF
,
76 [SIGWINCH
] = TARGET_SIGWINCH
,
77 [SIGIO
] = TARGET_SIGIO
,
78 [SIGPWR
] = TARGET_SIGPWR
,
79 [SIGSYS
] = TARGET_SIGSYS
,
80 /* next signals stay the same */
81 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
82 host libpthread signals. This assumes noone actually uses SIGRTMAX :-/
83 To fix this properly we need to do manual signal delivery multiplexed
84 over a single host signal. */
85 [__SIGRTMIN
] = __SIGRTMAX
,
86 [__SIGRTMAX
] = __SIGRTMIN
,
88 static uint8_t target_to_host_signal_table
[65];
90 static inline int on_sig_stack(unsigned long sp
)
92 return (sp
- target_sigaltstack_used
.ss_sp
93 < target_sigaltstack_used
.ss_size
);
96 static inline int sas_ss_flags(unsigned long sp
)
98 return (target_sigaltstack_used
.ss_size
== 0 ? SS_DISABLE
99 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
102 static inline int host_to_target_signal(int sig
)
106 return host_to_target_signal_table
[sig
];
109 int target_to_host_signal(int sig
)
113 return target_to_host_signal_table
[sig
];
116 static inline void target_sigemptyset(target_sigset_t
*set
)
118 memset(set
, 0, sizeof(*set
));
121 static inline void target_sigaddset(target_sigset_t
*set
, int signum
)
124 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
125 set
->sig
[signum
/ TARGET_NSIG_BPW
] |= mask
;
128 static inline int target_sigismember(const target_sigset_t
*set
, int signum
)
131 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
132 return ((set
->sig
[signum
/ TARGET_NSIG_BPW
] & mask
) != 0);
135 static void host_to_target_sigset_internal(target_sigset_t
*d
,
139 target_sigemptyset(d
);
140 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
141 if (sigismember(s
, i
)) {
142 target_sigaddset(d
, host_to_target_signal(i
));
147 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
152 host_to_target_sigset_internal(&d1
, s
);
153 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
154 d
->sig
[i
] = tswapl(d1
.sig
[i
]);
157 static void target_to_host_sigset_internal(sigset_t
*d
,
158 const target_sigset_t
*s
)
162 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
163 if (target_sigismember(s
, i
)) {
164 sigaddset(d
, target_to_host_signal(i
));
169 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
174 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
175 s1
.sig
[i
] = tswapl(s
->sig
[i
]);
176 target_to_host_sigset_internal(d
, &s1
);
179 void host_to_target_old_sigset(abi_ulong
*old_sigset
,
180 const sigset_t
*sigset
)
183 host_to_target_sigset(&d
, sigset
);
184 *old_sigset
= d
.sig
[0];
187 void target_to_host_old_sigset(sigset_t
*sigset
,
188 const abi_ulong
*old_sigset
)
193 d
.sig
[0] = *old_sigset
;
194 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
196 target_to_host_sigset(sigset
, &d
);
199 /* siginfo conversion */
201 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
202 const siginfo_t
*info
)
205 sig
= host_to_target_signal(info
->si_signo
);
206 tinfo
->si_signo
= sig
;
208 tinfo
->si_code
= info
->si_code
;
209 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
210 sig
== SIGBUS
|| sig
== SIGTRAP
) {
211 /* should never come here, but who knows. The information for
212 the target is irrelevant */
213 tinfo
->_sifields
._sigfault
._addr
= 0;
214 } else if (sig
== SIGIO
) {
215 tinfo
->_sifields
._sigpoll
._fd
= info
->si_fd
;
216 } else if (sig
>= TARGET_SIGRTMIN
) {
217 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
218 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
219 /* XXX: potential problem if 64 bit */
220 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
221 (abi_ulong
)(unsigned long)info
->si_value
.sival_ptr
;
225 static void tswap_siginfo(target_siginfo_t
*tinfo
,
226 const target_siginfo_t
*info
)
229 sig
= info
->si_signo
;
230 tinfo
->si_signo
= tswap32(sig
);
231 tinfo
->si_errno
= tswap32(info
->si_errno
);
232 tinfo
->si_code
= tswap32(info
->si_code
);
233 if (sig
== SIGILL
|| sig
== SIGFPE
|| sig
== SIGSEGV
||
234 sig
== SIGBUS
|| sig
== SIGTRAP
) {
235 tinfo
->_sifields
._sigfault
._addr
=
236 tswapl(info
->_sifields
._sigfault
._addr
);
237 } else if (sig
== SIGIO
) {
238 tinfo
->_sifields
._sigpoll
._fd
= tswap32(info
->_sifields
._sigpoll
._fd
);
239 } else if (sig
>= TARGET_SIGRTMIN
) {
240 tinfo
->_sifields
._rt
._pid
= tswap32(info
->_sifields
._rt
._pid
);
241 tinfo
->_sifields
._rt
._uid
= tswap32(info
->_sifields
._rt
._uid
);
242 tinfo
->_sifields
._rt
._sigval
.sival_ptr
=
243 tswapl(info
->_sifields
._rt
._sigval
.sival_ptr
);
248 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
250 host_to_target_siginfo_noswap(tinfo
, info
);
251 tswap_siginfo(tinfo
, tinfo
);
254 /* XXX: we support only POSIX RT signals are used. */
255 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
256 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
258 info
->si_signo
= tswap32(tinfo
->si_signo
);
259 info
->si_errno
= tswap32(tinfo
->si_errno
);
260 info
->si_code
= tswap32(tinfo
->si_code
);
261 info
->si_pid
= tswap32(tinfo
->_sifields
._rt
._pid
);
262 info
->si_uid
= tswap32(tinfo
->_sifields
._rt
._uid
);
263 info
->si_value
.sival_ptr
=
264 (void *)(long)tswapl(tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
267 void signal_init(void)
269 struct sigaction act
;
270 struct sigaction oact
;
274 /* generate signal conversion tables */
275 for(i
= 1; i
<= 64; i
++) {
276 if (host_to_target_signal_table
[i
] == 0)
277 host_to_target_signal_table
[i
] = i
;
279 for(i
= 1; i
<= 64; i
++) {
280 j
= host_to_target_signal_table
[i
];
281 target_to_host_signal_table
[j
] = i
;
284 /* set all host signal handlers. ALL signals are blocked during
285 the handlers to serialize them. */
286 memset(sigact_table
, 0, sizeof(sigact_table
));
288 sigfillset(&act
.sa_mask
);
289 act
.sa_flags
= SA_SIGINFO
;
290 act
.sa_sigaction
= host_signal_handler
;
291 for(i
= 1; i
<= TARGET_NSIG
; i
++) {
292 host_sig
= target_to_host_signal(i
);
293 sigaction(host_sig
, NULL
, &oact
);
294 if (oact
.sa_sigaction
== (void *)SIG_IGN
) {
295 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_IGN
;
296 } else if (oact
.sa_sigaction
== (void *)SIG_DFL
) {
297 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_DFL
;
299 /* If there's already a handler installed then something has
300 gone horribly wrong, so don't even try to handle that case. */
301 /* Install some handlers for our own use. */
302 if (host_sig
== SIGSEGV
|| host_sig
== SIGBUS
) {
303 sigaction(host_sig
, &act
, NULL
);
308 /* signal queue handling */
310 static inline struct sigqueue
*alloc_sigqueue(CPUState
*env
)
312 TaskState
*ts
= env
->opaque
;
313 struct sigqueue
*q
= ts
->first_free
;
316 ts
->first_free
= q
->next
;
320 static inline void free_sigqueue(CPUState
*env
, struct sigqueue
*q
)
322 TaskState
*ts
= env
->opaque
;
323 q
->next
= ts
->first_free
;
327 /* abort execution with signal */
328 static void __attribute((noreturn
)) force_sig(int sig
)
331 host_sig
= target_to_host_signal(sig
);
332 fprintf(stderr
, "qemu: uncaught target signal %d (%s) - exiting\n",
333 sig
, strsignal(host_sig
));
338 struct sigaction act
;
339 sigemptyset(&act
.sa_mask
);
340 act
.sa_flags
= SA_SIGINFO
;
341 act
.sa_sigaction
= SIG_DFL
;
342 sigaction(SIGABRT
, &act
, NULL
);
348 /* queue a signal so that it will be send to the virtual CPU as soon
350 int queue_signal(CPUState
*env
, int sig
, target_siginfo_t
*info
)
352 TaskState
*ts
= env
->opaque
;
353 struct emulated_sigtable
*k
;
354 struct sigqueue
*q
, **pq
;
357 #if defined(DEBUG_SIGNAL)
358 fprintf(stderr
, "queue_signal: sig=%d\n",
361 k
= &ts
->sigtab
[sig
- 1];
362 handler
= sigact_table
[sig
- 1]._sa_handler
;
363 if (handler
== TARGET_SIG_DFL
) {
364 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
365 kill(getpid(),SIGSTOP
);
368 /* default handler : ignore some signal. The other are fatal */
369 if (sig
!= TARGET_SIGCHLD
&&
370 sig
!= TARGET_SIGURG
&&
371 sig
!= TARGET_SIGWINCH
&&
372 sig
!= TARGET_SIGCONT
) {
375 return 0; /* indicate ignored */
377 } else if (handler
== TARGET_SIG_IGN
) {
380 } else if (handler
== TARGET_SIG_ERR
) {
384 if (sig
< TARGET_SIGRTMIN
) {
385 /* if non real time signal, we queue exactly one signal */
395 q
= alloc_sigqueue(env
);
406 /* signal that a new signal is pending */
407 ts
->signal_pending
= 1;
408 return 1; /* indicates that the signal was queued */
412 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
416 target_siginfo_t tinfo
;
418 /* the CPU emulator uses some host signals to detect exceptions,
419 we we forward to it some signals */
420 if (host_signum
== SIGSEGV
|| host_signum
== SIGBUS
) {
421 if (cpu_signal_handler(host_signum
, info
, puc
))
425 /* get target signal number */
426 sig
= host_to_target_signal(host_signum
);
427 if (sig
< 1 || sig
> TARGET_NSIG
)
429 #if defined(DEBUG_SIGNAL)
430 fprintf(stderr
, "qemu: got signal %d\n", sig
);
432 host_to_target_siginfo_noswap(&tinfo
, info
);
433 if (queue_signal(thread_env
, sig
, &tinfo
) == 1) {
434 /* interrupt the virtual CPU as soon as possible */
435 cpu_interrupt(thread_env
, CPU_INTERRUPT_EXIT
);
439 /* do_sigaltstack() returns target values and errnos. */
440 /* compare linux/kernel/signal.c:do_sigaltstack() */
441 abi_long
do_sigaltstack(abi_ulong uss_addr
, abi_ulong uoss_addr
, abi_ulong sp
)
444 struct target_sigaltstack oss
;
446 /* XXX: test errors */
449 __put_user(target_sigaltstack_used
.ss_sp
, &oss
.ss_sp
);
450 __put_user(target_sigaltstack_used
.ss_size
, &oss
.ss_size
);
451 __put_user(sas_ss_flags(sp
), &oss
.ss_flags
);
456 struct target_sigaltstack
*uss
;
457 struct target_sigaltstack ss
;
459 ret
= -TARGET_EFAULT
;
460 if (!lock_user_struct(VERIFY_READ
, uss
, uss_addr
, 1)
461 || __get_user(ss
.ss_sp
, &uss
->ss_sp
)
462 || __get_user(ss
.ss_size
, &uss
->ss_size
)
463 || __get_user(ss
.ss_flags
, &uss
->ss_flags
))
465 unlock_user_struct(uss
, uss_addr
, 0);
468 if (on_sig_stack(sp
))
471 ret
= -TARGET_EINVAL
;
472 if (ss
.ss_flags
!= TARGET_SS_DISABLE
473 && ss
.ss_flags
!= TARGET_SS_ONSTACK
477 if (ss
.ss_flags
== TARGET_SS_DISABLE
) {
481 ret
= -TARGET_ENOMEM
;
482 if (ss
.ss_size
< MINSIGSTKSZ
)
486 target_sigaltstack_used
.ss_sp
= ss
.ss_sp
;
487 target_sigaltstack_used
.ss_size
= ss
.ss_size
;
491 ret
= -TARGET_EFAULT
;
492 if (copy_to_user(uoss_addr
, &oss
, sizeof(oss
)))
501 /* do_sigaction() return host values and errnos */
502 int do_sigaction(int sig
, const struct target_sigaction
*act
,
503 struct target_sigaction
*oact
)
505 struct target_sigaction
*k
;
506 struct sigaction act1
;
510 if (sig
< 1 || sig
> TARGET_NSIG
|| sig
== TARGET_SIGKILL
|| sig
== TARGET_SIGSTOP
)
512 k
= &sigact_table
[sig
- 1];
513 #if defined(DEBUG_SIGNAL)
514 fprintf(stderr
, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
515 sig
, (int)act
, (int)oact
);
518 oact
->_sa_handler
= tswapl(k
->_sa_handler
);
519 oact
->sa_flags
= tswapl(k
->sa_flags
);
520 #if !defined(TARGET_MIPS)
521 oact
->sa_restorer
= tswapl(k
->sa_restorer
);
523 oact
->sa_mask
= k
->sa_mask
;
526 /* FIXME: This is not threadsafe. */
527 k
->_sa_handler
= tswapl(act
->_sa_handler
);
528 k
->sa_flags
= tswapl(act
->sa_flags
);
529 #if !defined(TARGET_MIPS)
530 k
->sa_restorer
= tswapl(act
->sa_restorer
);
532 k
->sa_mask
= act
->sa_mask
;
534 /* we update the host linux signal state */
535 host_sig
= target_to_host_signal(sig
);
536 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
537 sigfillset(&act1
.sa_mask
);
538 act1
.sa_flags
= SA_SIGINFO
;
539 if (k
->sa_flags
& TARGET_SA_RESTART
)
540 act1
.sa_flags
|= SA_RESTART
;
541 /* NOTE: it is important to update the host kernel signal
542 ignore state to avoid getting unexpected interrupted
544 if (k
->_sa_handler
== TARGET_SIG_IGN
) {
545 act1
.sa_sigaction
= (void *)SIG_IGN
;
546 } else if (k
->_sa_handler
== TARGET_SIG_DFL
) {
547 act1
.sa_sigaction
= (void *)SIG_DFL
;
549 act1
.sa_sigaction
= host_signal_handler
;
551 ret
= sigaction(host_sig
, &act1
, NULL
);
557 static inline int copy_siginfo_to_user(target_siginfo_t
*tinfo
,
558 const target_siginfo_t
*info
)
560 tswap_siginfo(tinfo
, info
);
564 static inline int current_exec_domain_sig(int sig
)
566 return /* current->exec_domain && current->exec_domain->signal_invmap
567 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig
;
570 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
572 /* from the Linux kernel */
574 struct target_fpreg
{
575 uint16_t significand
[4];
579 struct target_fpxreg
{
580 uint16_t significand
[4];
585 struct target_xmmreg
{
586 abi_ulong element
[4];
589 struct target_fpstate
{
590 /* Regular FPU environment */
598 struct target_fpreg _st
[8];
600 uint16_t magic
; /* 0xffff = regular FPU data only */
602 /* FXSR FPU environment */
603 abi_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
606 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
607 struct target_xmmreg _xmm
[8];
608 abi_ulong padding
[56];
611 #define X86_FXSR_MAGIC 0x0000
613 struct target_sigcontext
{
631 abi_ulong esp_at_signal
;
633 abi_ulong fpstate
; /* pointer */
638 struct target_ucontext
{
641 target_stack_t tuc_stack
;
642 struct target_sigcontext tuc_mcontext
;
643 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
650 struct target_sigcontext sc
;
651 struct target_fpstate fpstate
;
652 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
662 struct target_siginfo info
;
663 struct target_ucontext uc
;
664 struct target_fpstate fpstate
;
669 * Set up a signal frame.
672 /* XXX: save x87 state */
674 setup_sigcontext(struct target_sigcontext
*sc
, struct target_fpstate
*fpstate
,
675 CPUX86State
*env
, abi_ulong mask
, abi_ulong fpstate_addr
)
680 /* already locked in setup_frame() */
681 err
|= __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
682 err
|= __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
683 err
|= __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
684 err
|= __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
685 err
|= __put_user(env
->regs
[R_EDI
], &sc
->edi
);
686 err
|= __put_user(env
->regs
[R_ESI
], &sc
->esi
);
687 err
|= __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
688 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp
);
689 err
|= __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
690 err
|= __put_user(env
->regs
[R_EDX
], &sc
->edx
);
691 err
|= __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
692 err
|= __put_user(env
->regs
[R_EAX
], &sc
->eax
);
693 err
|= __put_user(env
->exception_index
, &sc
->trapno
);
694 err
|= __put_user(env
->error_code
, &sc
->err
);
695 err
|= __put_user(env
->eip
, &sc
->eip
);
696 err
|= __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
697 err
|= __put_user(env
->eflags
, &sc
->eflags
);
698 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
699 err
|= __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
701 cpu_x86_fsave(env
, fpstate_addr
, 1);
702 fpstate
->status
= fpstate
->sw
;
704 err
|= __put_user(magic
, &fpstate
->magic
);
705 err
|= __put_user(fpstate_addr
, &sc
->fpstate
);
707 /* non-iBCS2 extensions.. */
708 err
|= __put_user(mask
, &sc
->oldmask
);
709 err
|= __put_user(env
->cr
[2], &sc
->cr2
);
714 * Determine which stack to use..
717 static inline abi_ulong
718 get_sigframe(struct target_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
722 /* Default to using normal stack */
723 esp
= env
->regs
[R_ESP
];
724 /* This is the X/Open sanctioned signal stack switching. */
725 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
726 if (sas_ss_flags(esp
) == 0)
727 esp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
730 /* This is the legacy signal stack switching. */
732 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
733 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
735 esp
= (unsigned long) ka
->sa_restorer
;
737 return (esp
- frame_size
) & -8ul;
740 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
741 static void setup_frame(int sig
, struct target_sigaction
*ka
,
742 target_sigset_t
*set
, CPUX86State
*env
)
744 abi_ulong frame_addr
;
745 struct sigframe
*frame
;
748 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
750 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
753 err
|= __put_user(current_exec_domain_sig(sig
),
758 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0],
759 frame_addr
+ offsetof(struct sigframe
, fpstate
));
763 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
764 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
768 /* Set up to return from userspace. If provided, use a stub
769 already in userspace. */
770 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
771 err
|= __put_user(ka
->sa_restorer
, &frame
->pretcode
);
774 abi_ulong retcode_addr
;
775 retcode_addr
= frame_addr
+ offsetof(struct sigframe
, retcode
);
776 err
|= __put_user(retcode_addr
, &frame
->pretcode
);
777 /* This is popl %eax ; movl $,%eax ; int $0x80 */
779 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+0));
780 err
|= __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
782 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+6));
788 /* Set up registers for signal handler */
789 env
->regs
[R_ESP
] = frame_addr
;
790 env
->eip
= ka
->_sa_handler
;
792 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
793 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
794 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
795 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
796 env
->eflags
&= ~TF_MASK
;
798 unlock_user_struct(frame
, frame_addr
, 1);
803 unlock_user_struct(frame
, frame_addr
, 1);
804 if (sig
== TARGET_SIGSEGV
)
805 ka
->_sa_handler
= TARGET_SIG_DFL
;
806 force_sig(TARGET_SIGSEGV
/* , current */);
809 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
810 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
811 target_siginfo_t
*info
,
812 target_sigset_t
*set
, CPUX86State
*env
)
814 abi_ulong frame_addr
, addr
;
815 struct rt_sigframe
*frame
;
818 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
820 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
823 err
|= __put_user(current_exec_domain_sig(sig
),
825 addr
= frame_addr
+ offsetof(struct rt_sigframe
, info
);
826 err
|= __put_user(addr
, &frame
->pinfo
);
827 addr
= frame_addr
+ offsetof(struct rt_sigframe
, uc
);
828 err
|= __put_user(addr
, &frame
->puc
);
829 err
|= copy_siginfo_to_user(&frame
->info
, info
);
833 /* Create the ucontext. */
834 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
835 err
|= __put_user(0, &frame
->uc
.tuc_link
);
836 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
837 &frame
->uc
.tuc_stack
.ss_sp
);
838 err
|= __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
839 &frame
->uc
.tuc_stack
.ss_flags
);
840 err
|= __put_user(target_sigaltstack_used
.ss_size
,
841 &frame
->uc
.tuc_stack
.ss_size
);
842 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, &frame
->fpstate
,
844 frame_addr
+ offsetof(struct rt_sigframe
, fpstate
));
845 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
846 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
850 /* Set up to return from userspace. If provided, use a stub
851 already in userspace. */
852 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
853 err
|= __put_user(ka
->sa_restorer
, &frame
->pretcode
);
856 addr
= frame_addr
+ offsetof(struct rt_sigframe
, retcode
);
857 err
|= __put_user(addr
, &frame
->pretcode
);
858 /* This is movl $,%eax ; int $0x80 */
859 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
860 err
|= __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
862 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+5));
868 /* Set up registers for signal handler */
869 env
->regs
[R_ESP
] = frame_addr
;
870 env
->eip
= ka
->_sa_handler
;
872 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
873 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
874 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
875 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
876 env
->eflags
&= ~TF_MASK
;
878 unlock_user_struct(frame
, frame_addr
, 1);
883 unlock_user_struct(frame
, frame_addr
, 1);
884 if (sig
== TARGET_SIGSEGV
)
885 ka
->_sa_handler
= TARGET_SIG_DFL
;
886 force_sig(TARGET_SIGSEGV
/* , current */);
890 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
, int *peax
)
892 unsigned int err
= 0;
893 abi_ulong fpstate_addr
;
894 unsigned int tmpflags
;
896 cpu_x86_load_seg(env
, R_GS
, tswap16(sc
->gs
));
897 cpu_x86_load_seg(env
, R_FS
, tswap16(sc
->fs
));
898 cpu_x86_load_seg(env
, R_ES
, tswap16(sc
->es
));
899 cpu_x86_load_seg(env
, R_DS
, tswap16(sc
->ds
));
901 env
->regs
[R_EDI
] = tswapl(sc
->edi
);
902 env
->regs
[R_ESI
] = tswapl(sc
->esi
);
903 env
->regs
[R_EBP
] = tswapl(sc
->ebp
);
904 env
->regs
[R_ESP
] = tswapl(sc
->esp
);
905 env
->regs
[R_EBX
] = tswapl(sc
->ebx
);
906 env
->regs
[R_EDX
] = tswapl(sc
->edx
);
907 env
->regs
[R_ECX
] = tswapl(sc
->ecx
);
908 env
->eip
= tswapl(sc
->eip
);
910 cpu_x86_load_seg(env
, R_CS
, lduw(&sc
->cs
) | 3);
911 cpu_x86_load_seg(env
, R_SS
, lduw(&sc
->ss
) | 3);
913 tmpflags
= tswapl(sc
->eflags
);
914 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
915 // regs->orig_eax = -1; /* disable syscall checks */
917 fpstate_addr
= tswapl(sc
->fpstate
);
918 if (fpstate_addr
!= 0) {
919 if (!access_ok(VERIFY_READ
, fpstate_addr
,
920 sizeof(struct target_fpstate
)))
922 cpu_x86_frstor(env
, fpstate_addr
, 1);
925 *peax
= tswapl(sc
->eax
);
931 long do_sigreturn(CPUX86State
*env
)
933 struct sigframe
*frame
;
934 abi_ulong frame_addr
= env
->regs
[R_ESP
] - 8;
935 target_sigset_t target_set
;
939 #if defined(DEBUG_SIGNAL)
940 fprintf(stderr
, "do_sigreturn\n");
942 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
944 /* set blocked signals */
945 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
947 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
948 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
952 target_to_host_sigset_internal(&set
, &target_set
);
953 sigprocmask(SIG_SETMASK
, &set
, NULL
);
955 /* restore registers */
956 if (restore_sigcontext(env
, &frame
->sc
, &eax
))
958 unlock_user_struct(frame
, frame_addr
, 0);
962 unlock_user_struct(frame
, frame_addr
, 0);
963 force_sig(TARGET_SIGSEGV
);
967 long do_rt_sigreturn(CPUX86State
*env
)
969 abi_ulong frame_addr
;
970 struct rt_sigframe
*frame
;
974 frame_addr
= env
->regs
[R_ESP
] - 4;
975 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
977 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
978 sigprocmask(SIG_SETMASK
, &set
, NULL
);
980 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
, &eax
))
983 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe
, uc
.tuc_stack
), 0,
984 get_sp_from_cpustate(env
)) == -EFAULT
)
987 unlock_user_struct(frame
, frame_addr
, 0);
991 unlock_user_struct(frame
, frame_addr
, 0);
992 force_sig(TARGET_SIGSEGV
);
996 #elif defined(TARGET_ARM)
998 struct target_sigcontext
{
1000 abi_ulong error_code
;
1019 abi_ulong fault_address
;
1022 struct target_ucontext_v1
{
1023 abi_ulong tuc_flags
;
1025 target_stack_t tuc_stack
;
1026 struct target_sigcontext tuc_mcontext
;
1027 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1030 struct target_ucontext_v2
{
1031 abi_ulong tuc_flags
;
1033 target_stack_t tuc_stack
;
1034 struct target_sigcontext tuc_mcontext
;
1035 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1036 char __unused
[128 - sizeof(sigset_t
)];
1037 abi_ulong tuc_regspace
[128] __attribute__((__aligned__(8)));
1042 struct target_sigcontext sc
;
1043 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
1049 struct target_ucontext_v2 uc
;
1053 struct rt_sigframe_v1
1057 struct target_siginfo info
;
1058 struct target_ucontext_v1 uc
;
1062 struct rt_sigframe_v2
1064 struct target_siginfo info
;
1065 struct target_ucontext_v2 uc
;
1069 #define TARGET_CONFIG_CPU_32 1
1072 * For ARM syscalls, we encode the syscall number into the instruction.
1074 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1075 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1078 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1079 * need two 16-bit instructions.
1081 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1082 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1084 static const abi_ulong retcodes
[4] = {
1085 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
1086 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
1090 #define __get_user_error(x,p,e) __get_user(x, p)
1092 static inline int valid_user_regs(CPUState
*regs
)
1098 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1099 CPUState
*env
, abi_ulong mask
)
1101 __put_user(env
->regs
[0], &sc
->arm_r0
);
1102 __put_user(env
->regs
[1], &sc
->arm_r1
);
1103 __put_user(env
->regs
[2], &sc
->arm_r2
);
1104 __put_user(env
->regs
[3], &sc
->arm_r3
);
1105 __put_user(env
->regs
[4], &sc
->arm_r4
);
1106 __put_user(env
->regs
[5], &sc
->arm_r5
);
1107 __put_user(env
->regs
[6], &sc
->arm_r6
);
1108 __put_user(env
->regs
[7], &sc
->arm_r7
);
1109 __put_user(env
->regs
[8], &sc
->arm_r8
);
1110 __put_user(env
->regs
[9], &sc
->arm_r9
);
1111 __put_user(env
->regs
[10], &sc
->arm_r10
);
1112 __put_user(env
->regs
[11], &sc
->arm_fp
);
1113 __put_user(env
->regs
[12], &sc
->arm_ip
);
1114 __put_user(env
->regs
[13], &sc
->arm_sp
);
1115 __put_user(env
->regs
[14], &sc
->arm_lr
);
1116 __put_user(env
->regs
[15], &sc
->arm_pc
);
1117 #ifdef TARGET_CONFIG_CPU_32
1118 __put_user(cpsr_read(env
), &sc
->arm_cpsr
);
1121 __put_user(/* current->thread.trap_no */ 0, &sc
->trap_no
);
1122 __put_user(/* current->thread.error_code */ 0, &sc
->error_code
);
1123 __put_user(/* current->thread.address */ 0, &sc
->fault_address
);
1124 __put_user(mask
, &sc
->oldmask
);
1127 static inline abi_ulong
1128 get_sigframe(struct target_sigaction
*ka
, CPUState
*regs
, int framesize
)
1130 unsigned long sp
= regs
->regs
[13];
1133 * This is the X/Open sanctioned signal stack switching.
1135 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
))
1136 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1138 * ATPCS B01 mandates 8-byte alignment
1140 return (sp
- framesize
) & ~7;
1144 setup_return(CPUState
*env
, struct target_sigaction
*ka
,
1145 abi_ulong
*rc
, abi_ulong frame_addr
, int usig
, abi_ulong rc_addr
)
1147 abi_ulong handler
= ka
->_sa_handler
;
1149 int thumb
= handler
& 1;
1151 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1152 retcode
= ka
->sa_restorer
;
1154 unsigned int idx
= thumb
;
1156 if (ka
->sa_flags
& TARGET_SA_SIGINFO
)
1159 if (__put_user(retcodes
[idx
], rc
))
1162 flush_icache_range((abi_ulong
)rc
,
1163 (abi_ulong
)(rc
+ 1));
1165 retcode
= rc_addr
+ thumb
;
1168 env
->regs
[0] = usig
;
1169 env
->regs
[13] = frame_addr
;
1170 env
->regs
[14] = retcode
;
1171 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
1175 #ifdef TARGET_CONFIG_CPU_32
1183 static void setup_sigframe_v2(struct target_ucontext_v2
*uc
,
1184 target_sigset_t
*set
, CPUState
*env
)
1186 struct target_sigaltstack stack
;
1189 /* Clear all the bits of the ucontext we don't use. */
1190 memset(uc
, 0, offsetof(struct target_ucontext_v2
, tuc_mcontext
));
1192 memset(&stack
, 0, sizeof(stack
));
1193 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1194 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1195 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1196 memcpy(&uc
->tuc_stack
, &stack
, sizeof(stack
));
1198 setup_sigcontext(&uc
->tuc_mcontext
, env
, set
->sig
[0]);
1199 /* FIXME: Save coprocessor signal frame. */
1200 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1201 __put_user(set
->sig
[i
], &uc
->tuc_sigmask
.sig
[i
]);
1205 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1206 static void setup_frame_v1(int usig
, struct target_sigaction
*ka
,
1207 target_sigset_t
*set
, CPUState
*regs
)
1209 struct sigframe_v1
*frame
;
1210 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1213 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1216 setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
1218 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1219 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
1223 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1224 frame_addr
+ offsetof(struct sigframe_v1
, retcode
));
1227 unlock_user_struct(frame
, frame_addr
, 1);
1230 static void setup_frame_v2(int usig
, struct target_sigaction
*ka
,
1231 target_sigset_t
*set
, CPUState
*regs
)
1233 struct sigframe_v2
*frame
;
1234 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1236 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1239 setup_sigframe_v2(&frame
->uc
, set
, regs
);
1241 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1242 frame_addr
+ offsetof(struct sigframe_v2
, retcode
));
1244 unlock_user_struct(frame
, frame_addr
, 1);
1247 static void setup_frame(int usig
, struct target_sigaction
*ka
,
1248 target_sigset_t
*set
, CPUState
*regs
)
1250 if (get_osversion() >= 0x020612) {
1251 setup_frame_v2(usig
, ka
, set
, regs
);
1253 setup_frame_v1(usig
, ka
, set
, regs
);
1257 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1258 static void setup_rt_frame_v1(int usig
, struct target_sigaction
*ka
,
1259 target_siginfo_t
*info
,
1260 target_sigset_t
*set
, CPUState
*env
)
1262 struct rt_sigframe_v1
*frame
;
1263 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1264 struct target_sigaltstack stack
;
1266 abi_ulong info_addr
, uc_addr
;
1268 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1271 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, info
);
1272 __put_user(info_addr
, &frame
->pinfo
);
1273 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
);
1274 __put_user(uc_addr
, &frame
->puc
);
1275 copy_siginfo_to_user(&frame
->info
, info
);
1277 /* Clear all the bits of the ucontext we don't use. */
1278 memset(&frame
->uc
, 0, offsetof(struct target_ucontext_v1
, tuc_mcontext
));
1280 memset(&stack
, 0, sizeof(stack
));
1281 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1282 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1283 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1284 memcpy(&frame
->uc
.tuc_stack
, &stack
, sizeof(stack
));
1286 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, set
->sig
[0]);
1287 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1288 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
1292 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1293 frame_addr
+ offsetof(struct rt_sigframe_v1
, retcode
));
1295 env
->regs
[1] = info_addr
;
1296 env
->regs
[2] = uc_addr
;
1299 unlock_user_struct(frame
, frame_addr
, 1);
1302 static void setup_rt_frame_v2(int usig
, struct target_sigaction
*ka
,
1303 target_siginfo_t
*info
,
1304 target_sigset_t
*set
, CPUState
*env
)
1306 struct rt_sigframe_v2
*frame
;
1307 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1308 abi_ulong info_addr
, uc_addr
;
1310 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1313 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, info
);
1314 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, uc
);
1315 copy_siginfo_to_user(&frame
->info
, info
);
1317 setup_sigframe_v2(&frame
->uc
, set
, env
);
1319 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1320 frame_addr
+ offsetof(struct rt_sigframe_v2
, retcode
));
1322 env
->regs
[1] = info_addr
;
1323 env
->regs
[2] = uc_addr
;
1325 unlock_user_struct(frame
, frame_addr
, 1);
1328 static void setup_rt_frame(int usig
, struct target_sigaction
*ka
,
1329 target_siginfo_t
*info
,
1330 target_sigset_t
*set
, CPUState
*env
)
1332 if (get_osversion() >= 0x020612) {
1333 setup_rt_frame_v2(usig
, ka
, info
, set
, env
);
1335 setup_rt_frame_v1(usig
, ka
, info
, set
, env
);
1340 restore_sigcontext(CPUState
*env
, struct target_sigcontext
*sc
)
1345 __get_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
1346 __get_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
1347 __get_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1348 __get_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1349 __get_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1350 __get_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1351 __get_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1352 __get_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1353 __get_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1354 __get_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1355 __get_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1356 __get_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1357 __get_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1358 __get_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1359 __get_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1360 __get_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1361 #ifdef TARGET_CONFIG_CPU_32
1362 __get_user_error(cpsr
, &sc
->arm_cpsr
, err
);
1363 cpsr_write(env
, cpsr
, CPSR_USER
| CPSR_EXEC
);
1366 err
|= !valid_user_regs(env
);
1371 long do_sigreturn_v1(CPUState
*env
)
1373 abi_ulong frame_addr
;
1374 struct sigframe_v1
*frame
;
1375 target_sigset_t set
;
1380 * Since we stacked the signal on a 64-bit boundary,
1381 * then 'sp' should be word aligned here. If it's
1382 * not, then the user is trying to mess with us.
1384 if (env
->regs
[13] & 7)
1387 frame_addr
= env
->regs
[13];
1388 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1391 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
1393 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1394 if (__get_user(set
.sig
[i
], &frame
->extramask
[i
- 1]))
1398 target_to_host_sigset_internal(&host_set
, &set
);
1399 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1401 if (restore_sigcontext(env
, &frame
->sc
))
1405 /* Send SIGTRAP if we're single-stepping */
1406 if (ptrace_cancel_bpt(current
))
1407 send_sig(SIGTRAP
, current
, 1);
1409 unlock_user_struct(frame
, frame_addr
, 0);
1410 return env
->regs
[0];
1413 unlock_user_struct(frame
, frame_addr
, 0);
1414 force_sig(SIGSEGV
/* , current */);
1418 static int do_sigframe_return_v2(CPUState
*env
, target_ulong frame_addr
,
1419 struct target_ucontext_v2
*uc
)
1423 target_to_host_sigset(&host_set
, &uc
->tuc_sigmask
);
1424 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1426 if (restore_sigcontext(env
, &uc
->tuc_mcontext
))
1429 if (do_sigaltstack(frame_addr
+ offsetof(struct target_ucontext_v2
, tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
1433 /* Send SIGTRAP if we're single-stepping */
1434 if (ptrace_cancel_bpt(current
))
1435 send_sig(SIGTRAP
, current
, 1);
1441 long do_sigreturn_v2(CPUState
*env
)
1443 abi_ulong frame_addr
;
1444 struct sigframe_v2
*frame
;
1447 * Since we stacked the signal on a 64-bit boundary,
1448 * then 'sp' should be word aligned here. If it's
1449 * not, then the user is trying to mess with us.
1451 if (env
->regs
[13] & 7)
1454 frame_addr
= env
->regs
[13];
1455 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1458 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
))
1461 unlock_user_struct(frame
, frame_addr
, 0);
1462 return env
->regs
[0];
1465 unlock_user_struct(frame
, frame_addr
, 0);
1466 force_sig(SIGSEGV
/* , current */);
1470 long do_sigreturn(CPUState
*env
)
1472 if (get_osversion() >= 0x020612) {
1473 return do_sigreturn_v2(env
);
1475 return do_sigreturn_v1(env
);
1479 long do_rt_sigreturn_v1(CPUState
*env
)
1481 abi_ulong frame_addr
;
1482 struct rt_sigframe_v1
*frame
;
1486 * Since we stacked the signal on a 64-bit boundary,
1487 * then 'sp' should be word aligned here. If it's
1488 * not, then the user is trying to mess with us.
1490 if (env
->regs
[13] & 7)
1493 frame_addr
= env
->regs
[13];
1494 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1497 target_to_host_sigset(&host_set
, &frame
->uc
.tuc_sigmask
);
1498 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1500 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
))
1503 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
.tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
1507 /* Send SIGTRAP if we're single-stepping */
1508 if (ptrace_cancel_bpt(current
))
1509 send_sig(SIGTRAP
, current
, 1);
1511 unlock_user_struct(frame
, frame_addr
, 0);
1512 return env
->regs
[0];
1515 unlock_user_struct(frame
, frame_addr
, 0);
1516 force_sig(SIGSEGV
/* , current */);
1520 long do_rt_sigreturn_v2(CPUState
*env
)
1522 abi_ulong frame_addr
;
1523 struct rt_sigframe_v2
*frame
;
1526 * Since we stacked the signal on a 64-bit boundary,
1527 * then 'sp' should be word aligned here. If it's
1528 * not, then the user is trying to mess with us.
1530 if (env
->regs
[13] & 7)
1533 frame_addr
= env
->regs
[13];
1534 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1537 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
))
1540 unlock_user_struct(frame
, frame_addr
, 0);
1541 return env
->regs
[0];
1544 unlock_user_struct(frame
, frame_addr
, 0);
1545 force_sig(SIGSEGV
/* , current */);
1549 long do_rt_sigreturn(CPUState
*env
)
1551 if (get_osversion() >= 0x020612) {
1552 return do_rt_sigreturn_v2(env
);
1554 return do_rt_sigreturn_v1(env
);
1558 #elif defined(TARGET_SPARC)
1560 #define __SUNOS_MAXWIN 31
1562 /* This is what SunOS does, so shall I. */
1563 struct target_sigcontext
{
1564 abi_ulong sigc_onstack
; /* state to restore */
1566 abi_ulong sigc_mask
; /* sigmask to restore */
1567 abi_ulong sigc_sp
; /* stack pointer */
1568 abi_ulong sigc_pc
; /* program counter */
1569 abi_ulong sigc_npc
; /* next program counter */
1570 abi_ulong sigc_psr
; /* for condition codes etc */
1571 abi_ulong sigc_g1
; /* User uses these two registers */
1572 abi_ulong sigc_o0
; /* within the trampoline code. */
1574 /* Now comes information regarding the users window set
1575 * at the time of the signal.
1577 abi_ulong sigc_oswins
; /* outstanding windows */
1579 /* stack ptrs for each regwin buf */
1580 char *sigc_spbuf
[__SUNOS_MAXWIN
];
1582 /* Windows to restore after signal */
1584 abi_ulong locals
[8];
1586 } sigc_wbuf
[__SUNOS_MAXWIN
];
1588 /* A Sparc stack frame */
1589 struct sparc_stackf
{
1590 abi_ulong locals
[8];
1592 struct sparc_stackf
*fp
;
1593 abi_ulong callers_pc
;
1596 abi_ulong xxargs
[1];
1605 abi_ulong u_regs
[16]; /* globals and ins */
1611 unsigned long si_float_regs
[32];
1612 unsigned long si_fsr
;
1613 unsigned long si_fpqdepth
;
1615 unsigned long *insn_addr
;
1618 } qemu_siginfo_fpu_t
;
1621 struct target_signal_frame
{
1622 struct sparc_stackf ss
;
1625 abi_ulong insns
[2] __attribute__ ((aligned (8)));
1626 abi_ulong extramask
[TARGET_NSIG_WORDS
- 1];
1627 abi_ulong extra_size
; /* Should be 0 */
1628 qemu_siginfo_fpu_t fpu_state
;
1630 struct target_rt_signal_frame
{
1631 struct sparc_stackf ss
;
1636 unsigned int insns
[2];
1638 unsigned int extra_size
; /* Should be 0 */
1639 qemu_siginfo_fpu_t fpu_state
;
1653 #define UREG_FP UREG_I6
1654 #define UREG_SP UREG_O6
1656 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
1657 CPUState
*env
, unsigned long framesize
)
1661 sp
= env
->regwptr
[UREG_FP
];
1663 /* This is the X/Open sanctioned signal stack switching. */
1664 if (sa
->sa_flags
& TARGET_SA_ONSTACK
) {
1665 if (!on_sig_stack(sp
)
1666 && !((target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
) & 7))
1667 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1669 return sp
- framesize
;
1673 setup___siginfo(__siginfo_t
*si
, CPUState
*env
, abi_ulong mask
)
1677 err
|= __put_user(env
->psr
, &si
->si_regs
.psr
);
1678 err
|= __put_user(env
->pc
, &si
->si_regs
.pc
);
1679 err
|= __put_user(env
->npc
, &si
->si_regs
.npc
);
1680 err
|= __put_user(env
->y
, &si
->si_regs
.y
);
1681 for (i
=0; i
< 8; i
++) {
1682 err
|= __put_user(env
->gregs
[i
], &si
->si_regs
.u_regs
[i
]);
1684 for (i
=0; i
< 8; i
++) {
1685 err
|= __put_user(env
->regwptr
[UREG_I0
+ i
], &si
->si_regs
.u_regs
[i
+8]);
1687 err
|= __put_user(mask
, &si
->si_mask
);
1693 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1694 CPUState
*env
, unsigned long mask
)
1698 err
|= __put_user(mask
, &sc
->sigc_mask
);
1699 err
|= __put_user(env
->regwptr
[UREG_SP
], &sc
->sigc_sp
);
1700 err
|= __put_user(env
->pc
, &sc
->sigc_pc
);
1701 err
|= __put_user(env
->npc
, &sc
->sigc_npc
);
1702 err
|= __put_user(env
->psr
, &sc
->sigc_psr
);
1703 err
|= __put_user(env
->gregs
[1], &sc
->sigc_g1
);
1704 err
|= __put_user(env
->regwptr
[UREG_O0
], &sc
->sigc_o0
);
1709 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1711 static void setup_frame(int sig
, struct target_sigaction
*ka
,
1712 target_sigset_t
*set
, CPUState
*env
)
1715 struct target_signal_frame
*sf
;
1716 int sigframe_size
, err
, i
;
1718 /* 1. Make sure everything is clean */
1719 //synchronize_user_stack();
1721 sigframe_size
= NF_ALIGNEDSZ
;
1722 sf_addr
= get_sigframe(ka
, env
, sigframe_size
);
1724 sf
= lock_user(VERIFY_WRITE
, sf_addr
,
1725 sizeof(struct target_signal_frame
), 0);
1729 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1731 if (invalid_frame_pointer(sf
, sigframe_size
))
1732 goto sigill_and_return
;
1734 /* 2. Save the current process state */
1735 err
= setup___siginfo(&sf
->info
, env
, set
->sig
[0]);
1736 err
|= __put_user(0, &sf
->extra_size
);
1738 //err |= save_fpu_state(regs, &sf->fpu_state);
1739 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1741 err
|= __put_user(set
->sig
[0], &sf
->info
.si_mask
);
1742 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
1743 err
|= __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
1746 for (i
= 0; i
< 8; i
++) {
1747 err
|= __put_user(env
->regwptr
[i
+ UREG_L0
], &sf
->ss
.locals
[i
]);
1749 for (i
= 0; i
< 8; i
++) {
1750 err
|= __put_user(env
->regwptr
[i
+ UREG_I0
], &sf
->ss
.ins
[i
]);
1755 /* 3. signal handler back-trampoline and parameters */
1756 env
->regwptr
[UREG_FP
] = sf_addr
;
1757 env
->regwptr
[UREG_I0
] = sig
;
1758 env
->regwptr
[UREG_I1
] = sf_addr
+
1759 offsetof(struct target_signal_frame
, info
);
1760 env
->regwptr
[UREG_I2
] = sf_addr
+
1761 offsetof(struct target_signal_frame
, info
);
1763 /* 4. signal handler */
1764 env
->pc
= ka
->_sa_handler
;
1765 env
->npc
= (env
->pc
+ 4);
1766 /* 5. return to kernel instructions */
1767 if (ka
->sa_restorer
)
1768 env
->regwptr
[UREG_I7
] = ka
->sa_restorer
;
1772 env
->regwptr
[UREG_I7
] = sf_addr
+
1773 offsetof(struct target_signal_frame
, insns
) - 2 * 4;
1775 /* mov __NR_sigreturn, %g1 */
1777 err
|= __put_user(val32
, &sf
->insns
[0]);
1781 err
|= __put_user(val32
, &sf
->insns
[1]);
1785 /* Flush instruction space. */
1786 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1789 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
1793 force_sig(TARGET_SIGILL
);
1796 //fprintf(stderr, "force_sig\n");
1797 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
1798 force_sig(TARGET_SIGSEGV
);
1801 restore_fpu_state(CPUState
*env
, qemu_siginfo_fpu_t
*fpu
)
1806 if (current
->flags
& PF_USEDFPU
)
1807 regs
->psr
&= ~PSR_EF
;
1809 if (current
== last_task_used_math
) {
1810 last_task_used_math
= 0;
1811 regs
->psr
&= ~PSR_EF
;
1814 current
->used_math
= 1;
1815 current
->flags
&= ~PF_USEDFPU
;
1818 if (verify_area (VERIFY_READ
, fpu
, sizeof(*fpu
)))
1823 /* XXX: incorrect */
1824 err
= __copy_from_user(&env
->fpr
[0], &fpu
->si_float_regs
[0],
1825 (sizeof(unsigned long) * 32));
1827 err
|= __get_user(env
->fsr
, &fpu
->si_fsr
);
1829 err
|= __get_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
1830 if (current
->thread
.fpqdepth
!= 0)
1831 err
|= __copy_from_user(¤t
->thread
.fpqueue
[0],
1832 &fpu
->si_fpqueue
[0],
1833 ((sizeof(unsigned long) +
1834 (sizeof(unsigned long *)))*16));
1840 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
1841 target_siginfo_t
*info
,
1842 target_sigset_t
*set
, CPUState
*env
)
1844 fprintf(stderr
, "setup_rt_frame: not implemented\n");
1847 long do_sigreturn(CPUState
*env
)
1850 struct target_signal_frame
*sf
;
1851 uint32_t up_psr
, pc
, npc
;
1852 target_sigset_t set
;
1854 abi_ulong fpu_save_addr
;
1857 sf_addr
= env
->regwptr
[UREG_FP
];
1858 if (!lock_user_struct(VERIFY_READ
, sf
, sf_addr
, 1))
1861 fprintf(stderr
, "sigreturn\n");
1862 fprintf(stderr
, "sf: %x pc %x fp %x sp %x\n", sf
, env
->pc
, env
->regwptr
[UREG_FP
], env
->regwptr
[UREG_SP
]);
1864 //cpu_dump_state(env, stderr, fprintf, 0);
1866 /* 1. Make sure we are not getting garbage from the user */
1871 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
1872 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
1877 /* 2. Restore the state */
1878 err
|= __get_user(up_psr
, &sf
->info
.si_regs
.psr
);
1880 /* User can only change condition codes and FPU enabling in %psr. */
1881 env
->psr
= (up_psr
& (PSR_ICC
/* | PSR_EF */))
1882 | (env
->psr
& ~(PSR_ICC
/* | PSR_EF */));
1886 err
|= __get_user(env
->y
, &sf
->info
.si_regs
.y
);
1887 for (i
=0; i
< 8; i
++) {
1888 err
|= __get_user(env
->gregs
[i
], &sf
->info
.si_regs
.u_regs
[i
]);
1890 for (i
=0; i
< 8; i
++) {
1891 err
|= __get_user(env
->regwptr
[i
+ UREG_I0
], &sf
->info
.si_regs
.u_regs
[i
+8]);
1894 err
|= __get_user(fpu_save_addr
, &sf
->fpu_save
);
1897 // err |= restore_fpu_state(env, fpu_save);
1899 /* This is pretty much atomic, no amount locking would prevent
1900 * the races which exist anyways.
1902 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
1903 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1904 err
|= (__get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]));
1907 target_to_host_sigset_internal(&host_set
, &set
);
1908 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1912 unlock_user_struct(sf
, sf_addr
, 0);
1913 return env
->regwptr
[0];
1916 unlock_user_struct(sf
, sf_addr
, 0);
1917 force_sig(TARGET_SIGSEGV
);
1920 long do_rt_sigreturn(CPUState
*env
)
1922 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
1923 return -TARGET_ENOSYS
;
1926 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1948 typedef abi_ulong target_mc_greg_t
;
1949 typedef target_mc_greg_t target_mc_gregset_t
[MC_NGREG
];
1951 struct target_mc_fq
{
1952 abi_ulong
*mcfq_addr
;
1956 struct target_mc_fpu
{
1960 //uint128_t qregs[16];
1962 abi_ulong mcfpu_fsr
;
1963 abi_ulong mcfpu_fprs
;
1964 abi_ulong mcfpu_gsr
;
1965 struct target_mc_fq
*mcfpu_fq
;
1966 unsigned char mcfpu_qcnt
;
1967 unsigned char mcfpu_qentsz
;
1968 unsigned char mcfpu_enab
;
1970 typedef struct target_mc_fpu target_mc_fpu_t
;
1973 target_mc_gregset_t mc_gregs
;
1974 target_mc_greg_t mc_fp
;
1975 target_mc_greg_t mc_i7
;
1976 target_mc_fpu_t mc_fpregs
;
1977 } target_mcontext_t
;
1979 struct target_ucontext
{
1980 struct target_ucontext
*uc_link
;
1982 target_sigset_t uc_sigmask
;
1983 target_mcontext_t uc_mcontext
;
1986 /* A V9 register window */
1987 struct target_reg_window
{
1988 abi_ulong locals
[8];
1992 #define TARGET_STACK_BIAS 2047
1994 /* {set, get}context() needed for 64-bit SparcLinux userland. */
1995 void sparc64_set_context(CPUSPARCState
*env
)
1998 struct target_ucontext
*ucp
;
1999 target_mc_gregset_t
*grp
;
2000 abi_ulong pc
, npc
, tstate
;
2001 abi_ulong fp
, i7
, w_addr
;
2002 unsigned char fenab
;
2006 ucp_addr
= env
->regwptr
[UREG_I0
];
2007 if (!lock_user_struct(VERIFY_READ
, ucp
, ucp_addr
, 1))
2009 grp
= &ucp
->uc_mcontext
.mc_gregs
;
2010 err
= __get_user(pc
, &((*grp
)[MC_PC
]));
2011 err
|= __get_user(npc
, &((*grp
)[MC_NPC
]));
2012 if (err
|| ((pc
| npc
) & 3))
2014 if (env
->regwptr
[UREG_I1
]) {
2015 target_sigset_t target_set
;
2018 if (TARGET_NSIG_WORDS
== 1) {
2019 if (__get_user(target_set
.sig
[0], &ucp
->uc_sigmask
.sig
[0]))
2022 abi_ulong
*src
, *dst
;
2023 src
= ucp
->uc_sigmask
.sig
;
2024 dst
= target_set
.sig
;
2025 for (i
= 0; i
< sizeof(target_sigset_t
) / sizeof(abi_ulong
);
2027 err
|= __get_user(*dst
, src
);
2031 target_to_host_sigset_internal(&set
, &target_set
);
2032 sigprocmask(SIG_SETMASK
, &set
, NULL
);
2036 err
|= __get_user(env
->y
, &((*grp
)[MC_Y
]));
2037 err
|= __get_user(tstate
, &((*grp
)[MC_TSTATE
]));
2038 env
->asi
= (tstate
>> 24) & 0xff;
2039 PUT_CCR(env
, tstate
>> 32);
2040 PUT_CWP64(env
, tstate
& 0x1f);
2041 err
|= __get_user(env
->gregs
[1], (&(*grp
)[MC_G1
]));
2042 err
|= __get_user(env
->gregs
[2], (&(*grp
)[MC_G2
]));
2043 err
|= __get_user(env
->gregs
[3], (&(*grp
)[MC_G3
]));
2044 err
|= __get_user(env
->gregs
[4], (&(*grp
)[MC_G4
]));
2045 err
|= __get_user(env
->gregs
[5], (&(*grp
)[MC_G5
]));
2046 err
|= __get_user(env
->gregs
[6], (&(*grp
)[MC_G6
]));
2047 err
|= __get_user(env
->gregs
[7], (&(*grp
)[MC_G7
]));
2048 err
|= __get_user(env
->regwptr
[UREG_I0
], (&(*grp
)[MC_O0
]));
2049 err
|= __get_user(env
->regwptr
[UREG_I1
], (&(*grp
)[MC_O1
]));
2050 err
|= __get_user(env
->regwptr
[UREG_I2
], (&(*grp
)[MC_O2
]));
2051 err
|= __get_user(env
->regwptr
[UREG_I3
], (&(*grp
)[MC_O3
]));
2052 err
|= __get_user(env
->regwptr
[UREG_I4
], (&(*grp
)[MC_O4
]));
2053 err
|= __get_user(env
->regwptr
[UREG_I5
], (&(*grp
)[MC_O5
]));
2054 err
|= __get_user(env
->regwptr
[UREG_I6
], (&(*grp
)[MC_O6
]));
2055 err
|= __get_user(env
->regwptr
[UREG_I7
], (&(*grp
)[MC_O7
]));
2057 err
|= __get_user(fp
, &(ucp
->uc_mcontext
.mc_fp
));
2058 err
|= __get_user(i7
, &(ucp
->uc_mcontext
.mc_i7
));
2060 w_addr
= TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
];
2061 if (put_user(fp
, w_addr
+ offsetof(struct target_reg_window
, ins
[6]),
2064 if (put_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
2067 err
|= __get_user(fenab
, &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_enab
));
2068 err
|= __get_user(env
->fprs
, &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fprs
));
2070 uint32_t *src
, *dst
;
2071 src
= ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2073 /* XXX: check that the CPU storage is the same as user context */
2074 for (i
= 0; i
< 64; i
++, dst
++, src
++)
2075 err
|= __get_user(*dst
, src
);
2077 err
|= __get_user(env
->fsr
,
2078 &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fsr
));
2079 err
|= __get_user(env
->gsr
,
2080 &(ucp
->uc_mcontext
.mc_fpregs
.mcfpu_gsr
));
2083 unlock_user_struct(ucp
, ucp_addr
, 0);
2086 unlock_user_struct(ucp
, ucp_addr
, 0);
2090 void sparc64_get_context(CPUSPARCState
*env
)
2093 struct target_ucontext
*ucp
;
2094 target_mc_gregset_t
*grp
;
2095 target_mcontext_t
*mcp
;
2096 abi_ulong fp
, i7
, w_addr
;
2099 target_sigset_t target_set
;
2102 ucp_addr
= env
->regwptr
[UREG_I0
];
2103 if (!lock_user_struct(VERIFY_WRITE
, ucp
, ucp_addr
, 0))
2106 mcp
= &ucp
->uc_mcontext
;
2107 grp
= &mcp
->mc_gregs
;
2109 /* Skip over the trap instruction, first. */
2115 sigprocmask(0, NULL
, &set
);
2116 host_to_target_sigset_internal(&target_set
, &set
);
2117 if (TARGET_NSIG_WORDS
== 1) {
2118 err
|= __put_user(target_set
.sig
[0],
2119 (abi_ulong
*)&ucp
->uc_sigmask
);
2121 abi_ulong
*src
, *dst
;
2122 src
= target_set
.sig
;
2123 dst
= ucp
->uc_sigmask
.sig
;
2124 for (i
= 0; i
< sizeof(target_sigset_t
) / sizeof(abi_ulong
);
2126 err
|= __put_user(*src
, dst
);
2131 /* XXX: tstate must be saved properly */
2132 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2133 err
|= __put_user(env
->pc
, &((*grp
)[MC_PC
]));
2134 err
|= __put_user(env
->npc
, &((*grp
)[MC_NPC
]));
2135 err
|= __put_user(env
->y
, &((*grp
)[MC_Y
]));
2136 err
|= __put_user(env
->gregs
[1], &((*grp
)[MC_G1
]));
2137 err
|= __put_user(env
->gregs
[2], &((*grp
)[MC_G2
]));
2138 err
|= __put_user(env
->gregs
[3], &((*grp
)[MC_G3
]));
2139 err
|= __put_user(env
->gregs
[4], &((*grp
)[MC_G4
]));
2140 err
|= __put_user(env
->gregs
[5], &((*grp
)[MC_G5
]));
2141 err
|= __put_user(env
->gregs
[6], &((*grp
)[MC_G6
]));
2142 err
|= __put_user(env
->gregs
[7], &((*grp
)[MC_G7
]));
2143 err
|= __put_user(env
->regwptr
[UREG_I0
], &((*grp
)[MC_O0
]));
2144 err
|= __put_user(env
->regwptr
[UREG_I1
], &((*grp
)[MC_O1
]));
2145 err
|= __put_user(env
->regwptr
[UREG_I2
], &((*grp
)[MC_O2
]));
2146 err
|= __put_user(env
->regwptr
[UREG_I3
], &((*grp
)[MC_O3
]));
2147 err
|= __put_user(env
->regwptr
[UREG_I4
], &((*grp
)[MC_O4
]));
2148 err
|= __put_user(env
->regwptr
[UREG_I5
], &((*grp
)[MC_O5
]));
2149 err
|= __put_user(env
->regwptr
[UREG_I6
], &((*grp
)[MC_O6
]));
2150 err
|= __put_user(env
->regwptr
[UREG_I7
], &((*grp
)[MC_O7
]));
2152 w_addr
= TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
];
2154 if (get_user(fp
, w_addr
+ offsetof(struct target_reg_window
, ins
[6]),
2157 if (get_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
2160 err
|= __put_user(fp
, &(mcp
->mc_fp
));
2161 err
|= __put_user(i7
, &(mcp
->mc_i7
));
2164 uint32_t *src
, *dst
;
2166 dst
= ucp
->uc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2167 /* XXX: check that the CPU storage is the same as user context */
2168 for (i
= 0; i
< 64; i
++, dst
++, src
++)
2169 err
|= __put_user(*src
, dst
);
2171 err
|= __put_user(env
->fsr
, &(mcp
->mc_fpregs
.mcfpu_fsr
));
2172 err
|= __put_user(env
->gsr
, &(mcp
->mc_fpregs
.mcfpu_gsr
));
2173 err
|= __put_user(env
->fprs
, &(mcp
->mc_fpregs
.mcfpu_fprs
));
2177 unlock_user_struct(ucp
, ucp_addr
, 1);
2180 unlock_user_struct(ucp
, ucp_addr
, 1);
2184 #elif defined(TARGET_ABI_MIPSN64)
2186 # warning signal handling not implemented
2188 static void setup_frame(int sig
, struct target_sigaction
*ka
,
2189 target_sigset_t
*set
, CPUState
*env
)
2191 fprintf(stderr
, "setup_frame: not implemented\n");
2194 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
2195 target_siginfo_t
*info
,
2196 target_sigset_t
*set
, CPUState
*env
)
2198 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2201 long do_sigreturn(CPUState
*env
)
2203 fprintf(stderr
, "do_sigreturn: not implemented\n");
2204 return -TARGET_ENOSYS
;
2207 long do_rt_sigreturn(CPUState
*env
)
2209 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2210 return -TARGET_ENOSYS
;
2213 #elif defined(TARGET_ABI_MIPSN32)
2215 # warning signal handling not implemented
2217 static void setup_frame(int sig
, struct target_sigaction
*ka
,
2218 target_sigset_t
*set
, CPUState
*env
)
2220 fprintf(stderr
, "setup_frame: not implemented\n");
2223 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
2224 target_siginfo_t
*info
,
2225 target_sigset_t
*set
, CPUState
*env
)
2227 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2230 long do_sigreturn(CPUState
*env
)
2232 fprintf(stderr
, "do_sigreturn: not implemented\n");
2233 return -TARGET_ENOSYS
;
2236 long do_rt_sigreturn(CPUState
*env
)
2238 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2239 return -TARGET_ENOSYS
;
2242 #elif defined(TARGET_ABI_MIPSO32)
2244 struct target_sigcontext
{
2245 uint32_t sc_regmask
; /* Unused */
2248 uint64_t sc_regs
[32];
2249 uint64_t sc_fpregs
[32];
2250 uint32_t sc_ownedfp
; /* Unused */
2251 uint32_t sc_fpc_csr
;
2252 uint32_t sc_fpc_eir
; /* Unused */
2253 uint32_t sc_used_math
;
2254 uint32_t sc_dsp
; /* dsp status, was sc_ssflags */
2257 target_ulong sc_hi1
; /* Was sc_cause */
2258 target_ulong sc_lo1
; /* Was sc_badvaddr */
2259 target_ulong sc_hi2
; /* Was sc_sigset[4] */
2260 target_ulong sc_lo2
;
2261 target_ulong sc_hi3
;
2262 target_ulong sc_lo3
;
2266 uint32_t sf_ass
[4]; /* argument save space for o32 */
2267 uint32_t sf_code
[2]; /* signal trampoline */
2268 struct target_sigcontext sf_sc
;
2269 target_sigset_t sf_mask
;
2272 /* Install trampoline to jump back from signal handler */
2273 static inline int install_sigtramp(unsigned int *tramp
, unsigned int syscall
)
2278 * Set up the return code ...
2280 * li v0, __NR__foo_sigreturn
2284 err
= __put_user(0x24020000 + syscall
, tramp
+ 0);
2285 err
|= __put_user(0x0000000c , tramp
+ 1);
2286 /* flush_cache_sigtramp((unsigned long) tramp); */
2291 setup_sigcontext(CPUState
*regs
, struct target_sigcontext
*sc
)
2295 err
|= __put_user(regs
->active_tc
.PC
, &sc
->sc_pc
);
2297 #define save_gp_reg(i) do { \
2298 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2300 __put_user(0, &sc
->sc_regs
[0]); save_gp_reg(1); save_gp_reg(2);
2301 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2302 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2303 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2304 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2305 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2306 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2307 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2311 err
|= __put_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2312 err
|= __put_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2314 /* Not used yet, but might be useful if we ever have DSP suppport */
2317 err
|= __put_user(mfhi1(), &sc
->sc_hi1
);
2318 err
|= __put_user(mflo1(), &sc
->sc_lo1
);
2319 err
|= __put_user(mfhi2(), &sc
->sc_hi2
);
2320 err
|= __put_user(mflo2(), &sc
->sc_lo2
);
2321 err
|= __put_user(mfhi3(), &sc
->sc_hi3
);
2322 err
|= __put_user(mflo3(), &sc
->sc_lo3
);
2323 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
2325 /* same with 64 bit */
2327 err
|= __put_user(regs
->hi
, &sc
->sc_hi
[0]);
2328 err
|= __put_user(regs
->lo
, &sc
->sc_lo
[0]);
2330 err
|= __put_user(mfhi1(), &sc
->sc_hi
[1]);
2331 err
|= __put_user(mflo1(), &sc
->sc_lo
[1]);
2332 err
|= __put_user(mfhi2(), &sc
->sc_hi
[2]);
2333 err
|= __put_user(mflo2(), &sc
->sc_lo
[2]);
2334 err
|= __put_user(mfhi3(), &sc
->sc_hi
[3]);
2335 err
|= __put_user(mflo3(), &sc
->sc_lo
[3]);
2336 err
|= __put_user(rddsp(DSP_MASK
), &sc
->sc_dsp
);
2342 err
|= __put_user(!!used_math(), &sc
->sc_used_math
);
2348 * Save FPU state to signal context. Signal handler will "inherit"
2349 * current FPU state.
2353 if (!is_fpu_owner()) {
2355 restore_fp(current
);
2357 err
|= save_fp_context(sc
);
2366 restore_sigcontext(CPUState
*regs
, struct target_sigcontext
*sc
)
2370 err
|= __get_user(regs
->CP0_EPC
, &sc
->sc_pc
);
2372 err
|= __get_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2373 err
|= __get_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2375 #define restore_gp_reg(i) do { \
2376 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2378 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2379 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2380 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2381 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2382 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2383 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2384 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2385 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2386 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2387 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2389 #undef restore_gp_reg
2393 err
|= __get_user(treg
, &sc
->sc_hi1
); mthi1(treg
);
2394 err
|= __get_user(treg
, &sc
->sc_lo1
); mtlo1(treg
);
2395 err
|= __get_user(treg
, &sc
->sc_hi2
); mthi2(treg
);
2396 err
|= __get_user(treg
, &sc
->sc_lo2
); mtlo2(treg
);
2397 err
|= __get_user(treg
, &sc
->sc_hi3
); mthi3(treg
);
2398 err
|= __get_user(treg
, &sc
->sc_lo3
); mtlo3(treg
);
2399 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
2402 err
|= __get_user(regs
->hi
, &sc
->sc_hi
[0]);
2403 err
|= __get_user(regs
->lo
, &sc
->sc_lo
[0]);
2405 err
|= __get_user(treg
, &sc
->sc_hi
[1]); mthi1(treg
);
2406 err
|= __get_user(treg
, &sc
->sc_lo
[1]); mthi1(treg
);
2407 err
|= __get_user(treg
, &sc
->sc_hi
[2]); mthi2(treg
);
2408 err
|= __get_user(treg
, &sc
->sc_lo
[2]); mthi2(treg
);
2409 err
|= __get_user(treg
, &sc
->sc_hi
[3]); mthi3(treg
);
2410 err
|= __get_user(treg
, &sc
->sc_lo
[3]); mthi3(treg
);
2411 err
|= __get_user(treg
, &sc
->sc_dsp
); wrdsp(treg
, DSP_MASK
);
2415 err
|= __get_user(used_math
, &sc
->sc_used_math
);
2416 conditional_used_math(used_math
);
2421 /* restore fpu context if we have used it before */
2423 err
|= restore_fp_context(sc
);
2425 /* signal handler may have used FPU. Give it up. */
2434 * Determine which stack to use..
2436 static inline abi_ulong
2437 get_sigframe(struct target_sigaction
*ka
, CPUState
*regs
, size_t frame_size
)
2441 /* Default to using normal stack */
2442 sp
= regs
->active_tc
.gpr
[29];
2445 * FPU emulator may have it's own trampoline active just
2446 * above the user stack, 16-bytes before the next lowest
2447 * 16 byte boundary. Try to avoid trashing it.
2451 /* This is the X/Open sanctioned signal stack switching. */
2452 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
2453 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2456 return (sp
- frame_size
) & ~7;
2459 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2460 static void setup_frame(int sig
, struct target_sigaction
* ka
,
2461 target_sigset_t
*set
, CPUState
*regs
)
2463 struct sigframe
*frame
;
2464 abi_ulong frame_addr
;
2467 frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
2468 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
2471 install_sigtramp(frame
->sf_code
, TARGET_NR_sigreturn
);
2473 if(setup_sigcontext(regs
, &frame
->sf_sc
))
2476 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2477 if(__put_user(set
->sig
[i
], &frame
->sf_mask
.sig
[i
]))
2482 * Arguments to signal handler:
2484 * a0 = signal number
2485 * a1 = 0 (should be cause)
2486 * a2 = pointer to struct sigcontext
2488 * $25 and PC point to the signal handler, $29 points to the
2491 regs
->active_tc
.gpr
[ 4] = sig
;
2492 regs
->active_tc
.gpr
[ 5] = 0;
2493 regs
->active_tc
.gpr
[ 6] = frame_addr
+ offsetof(struct sigframe
, sf_sc
);
2494 regs
->active_tc
.gpr
[29] = frame_addr
;
2495 regs
->active_tc
.gpr
[31] = frame_addr
+ offsetof(struct sigframe
, sf_code
);
2496 /* The original kernel code sets CP0_EPC to the handler
2497 * since it returns to userland using eret
2498 * we cannot do this here, and we must set PC directly */
2499 regs
->active_tc
.PC
= regs
->active_tc
.gpr
[25] = ka
->_sa_handler
;
2500 unlock_user_struct(frame
, frame_addr
, 1);
2504 unlock_user_struct(frame
, frame_addr
, 1);
2505 force_sig(TARGET_SIGSEGV
/*, current*/);
2509 long do_sigreturn(CPUState
*regs
)
2511 struct sigframe
*frame
;
2512 abi_ulong frame_addr
;
2514 target_sigset_t target_set
;
2517 #if defined(DEBUG_SIGNAL)
2518 fprintf(stderr
, "do_sigreturn\n");
2520 frame_addr
= regs
->active_tc
.gpr
[29];
2521 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2524 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2525 if(__get_user(target_set
.sig
[i
], &frame
->sf_mask
.sig
[i
]))
2529 target_to_host_sigset_internal(&blocked
, &target_set
);
2530 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
2532 if (restore_sigcontext(regs
, &frame
->sf_sc
))
2537 * Don't let your children do this ...
2539 __asm__
__volatile__(
2547 regs
->active_tc
.PC
= regs
->CP0_EPC
;
2548 /* I am not sure this is right, but it seems to work
2549 * maybe a problem with nested signals ? */
2554 force_sig(TARGET_SIGSEGV
/*, current*/);
2558 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
2559 target_siginfo_t
*info
,
2560 target_sigset_t
*set
, CPUState
*env
)
2562 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2565 long do_rt_sigreturn(CPUState
*env
)
2567 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2568 return -TARGET_ENOSYS
;
2571 #elif defined(TARGET_SH4)
2574 * code and data structures from linux kernel:
2575 * include/asm-sh/sigcontext.h
2576 * arch/sh/kernel/signal.c
2579 struct target_sigcontext
{
2580 target_ulong oldmask
;
2583 target_ulong sc_gregs
[16];
2587 target_ulong sc_gbr
;
2588 target_ulong sc_mach
;
2589 target_ulong sc_macl
;
2592 target_ulong sc_fpregs
[16];
2593 target_ulong sc_xfpregs
[16];
2594 unsigned int sc_fpscr
;
2595 unsigned int sc_fpul
;
2596 unsigned int sc_ownedfp
;
2599 struct target_sigframe
2601 struct target_sigcontext sc
;
2602 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
2603 uint16_t retcode
[3];
2607 struct target_ucontext
{
2608 target_ulong uc_flags
;
2609 struct target_ucontext
*uc_link
;
2610 target_stack_t uc_stack
;
2611 struct target_sigcontext uc_mcontext
;
2612 target_sigset_t uc_sigmask
; /* mask last for extensibility */
2615 struct target_rt_sigframe
2617 struct target_siginfo info
;
2618 struct target_ucontext uc
;
2619 uint16_t retcode
[3];
2623 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2624 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2626 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
2627 unsigned long sp
, size_t frame_size
)
2629 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags(sp
) == 0)) {
2630 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2633 return (sp
- frame_size
) & -8ul;
2636 static int setup_sigcontext(struct target_sigcontext
*sc
,
2637 CPUState
*regs
, unsigned long mask
)
2641 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2642 COPY(gregs
[0]); COPY(gregs
[1]);
2643 COPY(gregs
[2]); COPY(gregs
[3]);
2644 COPY(gregs
[4]); COPY(gregs
[5]);
2645 COPY(gregs
[6]); COPY(gregs
[7]);
2646 COPY(gregs
[8]); COPY(gregs
[9]);
2647 COPY(gregs
[10]); COPY(gregs
[11]);
2648 COPY(gregs
[12]); COPY(gregs
[13]);
2649 COPY(gregs
[14]); COPY(gregs
[15]);
2650 COPY(gbr
); COPY(mach
);
2651 COPY(macl
); COPY(pr
);
2655 /* todo: save FPU registers here */
2657 /* non-iBCS2 extensions.. */
2658 err
|= __put_user(mask
, &sc
->oldmask
);
2663 static int restore_sigcontext(struct CPUState
*regs
,
2664 struct target_sigcontext
*sc
)
2666 unsigned int err
= 0;
2668 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2670 COPY(gregs
[2]); COPY(gregs
[3]);
2671 COPY(gregs
[4]); COPY(gregs
[5]);
2672 COPY(gregs
[6]); COPY(gregs
[7]);
2673 COPY(gregs
[8]); COPY(gregs
[9]);
2674 COPY(gregs
[10]); COPY(gregs
[11]);
2675 COPY(gregs
[12]); COPY(gregs
[13]);
2676 COPY(gregs
[14]); COPY(gregs
[15]);
2677 COPY(gbr
); COPY(mach
);
2678 COPY(macl
); COPY(pr
);
2682 /* todo: restore FPU registers here */
2684 regs
->tra
= -1; /* disable syscall checks */
2688 static void setup_frame(int sig
, struct target_sigaction
*ka
,
2689 target_sigset_t
*set
, CPUState
*regs
)
2691 struct target_sigframe
*frame
;
2692 abi_ulong frame_addr
;
2697 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
2698 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
2701 signal
= current_exec_domain_sig(sig
);
2703 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
2705 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
2706 err
|= __put_user(set
->sig
[i
+ 1], &frame
->extramask
[i
]);
2709 /* Set up to return from userspace. If provided, use a stub
2710 already in userspace. */
2711 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
2712 regs
->pr
= (unsigned long) ka
->sa_restorer
;
2714 /* Generate return code (system call to sigreturn) */
2715 err
|= __put_user(MOVW(2), &frame
->retcode
[0]);
2716 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
2717 err
|= __put_user((TARGET_NR_sigreturn
), &frame
->retcode
[2]);
2718 regs
->pr
= (unsigned long) frame
->retcode
;
2724 /* Set up registers for signal handler */
2725 regs
->gregs
[15] = (unsigned long) frame
;
2726 regs
->gregs
[4] = signal
; /* Arg for signal handler */
2728 regs
->gregs
[6] = (unsigned long) &frame
->sc
;
2729 regs
->pc
= (unsigned long) ka
->_sa_handler
;
2731 unlock_user_struct(frame
, frame_addr
, 1);
2735 unlock_user_struct(frame
, frame_addr
, 1);
2739 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
2740 target_siginfo_t
*info
,
2741 target_sigset_t
*set
, CPUState
*regs
)
2743 struct target_rt_sigframe
*frame
;
2744 abi_ulong frame_addr
;
2749 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
2750 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
2753 signal
= current_exec_domain_sig(sig
);
2755 err
|= copy_siginfo_to_user(&frame
->info
, info
);
2757 /* Create the ucontext. */
2758 err
|= __put_user(0, &frame
->uc
.uc_flags
);
2759 err
|= __put_user(0, (unsigned long *)&frame
->uc
.uc_link
);
2760 err
|= __put_user((unsigned long)target_sigaltstack_used
.ss_sp
,
2761 &frame
->uc
.uc_stack
.ss_sp
);
2762 err
|= __put_user(sas_ss_flags(regs
->gregs
[15]),
2763 &frame
->uc
.uc_stack
.ss_flags
);
2764 err
|= __put_user(target_sigaltstack_used
.ss_size
,
2765 &frame
->uc
.uc_stack
.ss_size
);
2766 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
2768 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2769 err
|= __put_user(set
->sig
[i
], &frame
->uc
.uc_sigmask
.sig
[i
]);
2772 /* Set up to return from userspace. If provided, use a stub
2773 already in userspace. */
2774 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
2775 regs
->pr
= (unsigned long) ka
->sa_restorer
;
2777 /* Generate return code (system call to sigreturn) */
2778 err
|= __put_user(MOVW(2), &frame
->retcode
[0]);
2779 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
2780 err
|= __put_user((TARGET_NR_rt_sigreturn
), &frame
->retcode
[2]);
2781 regs
->pr
= (unsigned long) frame
->retcode
;
2787 /* Set up registers for signal handler */
2788 regs
->gregs
[15] = (unsigned long) frame
;
2789 regs
->gregs
[4] = signal
; /* Arg for signal handler */
2790 regs
->gregs
[5] = (unsigned long) &frame
->info
;
2791 regs
->gregs
[6] = (unsigned long) &frame
->uc
;
2792 regs
->pc
= (unsigned long) ka
->_sa_handler
;
2794 unlock_user_struct(frame
, frame_addr
, 1);
2798 unlock_user_struct(frame
, frame_addr
, 1);
2802 long do_sigreturn(CPUState
*regs
)
2804 struct target_sigframe
*frame
;
2805 abi_ulong frame_addr
;
2807 target_sigset_t target_set
;
2811 #if defined(DEBUG_SIGNAL)
2812 fprintf(stderr
, "do_sigreturn\n");
2814 frame_addr
= regs
->gregs
[15];
2815 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2818 err
|= __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
);
2819 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
2820 err
|= (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]));
2826 target_to_host_sigset_internal(&blocked
, &target_set
);
2827 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
2829 if (restore_sigcontext(regs
, &frame
->sc
))
2832 unlock_user_struct(frame
, frame_addr
, 0);
2833 return regs
->gregs
[0];
2836 unlock_user_struct(frame
, frame_addr
, 0);
2837 force_sig(TARGET_SIGSEGV
);
2841 long do_rt_sigreturn(CPUState
*regs
)
2843 struct target_rt_sigframe
*frame
;
2844 abi_ulong frame_addr
;
2847 #if defined(DEBUG_SIGNAL)
2848 fprintf(stderr
, "do_rt_sigreturn\n");
2850 frame_addr
= regs
->gregs
[15];
2851 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2854 target_to_host_sigset(&blocked
, &frame
->uc
.uc_sigmask
);
2855 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
2857 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
2860 if (do_sigaltstack(frame_addr
+
2861 offsetof(struct target_rt_sigframe
, uc
.uc_stack
),
2862 0, get_sp_from_cpustate(regs
)) == -EFAULT
)
2865 unlock_user_struct(frame
, frame_addr
, 0);
2866 return regs
->gregs
[0];
2869 unlock_user_struct(frame
, frame_addr
, 0);
2870 force_sig(TARGET_SIGSEGV
);
2873 #elif defined(TARGET_CRIS)
2875 struct target_sigcontext
{
2876 struct target_pt_regs regs
; /* needs to be first */
2878 uint32_t usp
; /* usp before stacking this gunk on it */
2881 /* Signal frames. */
2882 struct target_signal_frame
{
2883 struct target_sigcontext sc
;
2884 uint32_t extramask
[TARGET_NSIG_WORDS
- 1];
2885 uint8_t retcode
[8]; /* Trampoline code. */
2888 struct rt_signal_frame
{
2889 struct siginfo
*pinfo
;
2891 struct siginfo info
;
2893 uint8_t retcode
[8]; /* Trampoline code. */
2896 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUState
*env
)
2898 __put_user(env
->regs
[0], &sc
->regs
.r0
);
2899 __put_user(env
->regs
[1], &sc
->regs
.r1
);
2900 __put_user(env
->regs
[2], &sc
->regs
.r2
);
2901 __put_user(env
->regs
[3], &sc
->regs
.r3
);
2902 __put_user(env
->regs
[4], &sc
->regs
.r4
);
2903 __put_user(env
->regs
[5], &sc
->regs
.r5
);
2904 __put_user(env
->regs
[6], &sc
->regs
.r6
);
2905 __put_user(env
->regs
[7], &sc
->regs
.r7
);
2906 __put_user(env
->regs
[8], &sc
->regs
.r8
);
2907 __put_user(env
->regs
[9], &sc
->regs
.r9
);
2908 __put_user(env
->regs
[10], &sc
->regs
.r10
);
2909 __put_user(env
->regs
[11], &sc
->regs
.r11
);
2910 __put_user(env
->regs
[12], &sc
->regs
.r12
);
2911 __put_user(env
->regs
[13], &sc
->regs
.r13
);
2912 __put_user(env
->regs
[14], &sc
->usp
);
2913 __put_user(env
->regs
[15], &sc
->regs
.acr
);
2914 __put_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
2915 __put_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
2916 __put_user(env
->pc
, &sc
->regs
.erp
);
2919 static void restore_sigcontext(struct target_sigcontext
*sc
, CPUState
*env
)
2921 __get_user(env
->regs
[0], &sc
->regs
.r0
);
2922 __get_user(env
->regs
[1], &sc
->regs
.r1
);
2923 __get_user(env
->regs
[2], &sc
->regs
.r2
);
2924 __get_user(env
->regs
[3], &sc
->regs
.r3
);
2925 __get_user(env
->regs
[4], &sc
->regs
.r4
);
2926 __get_user(env
->regs
[5], &sc
->regs
.r5
);
2927 __get_user(env
->regs
[6], &sc
->regs
.r6
);
2928 __get_user(env
->regs
[7], &sc
->regs
.r7
);
2929 __get_user(env
->regs
[8], &sc
->regs
.r8
);
2930 __get_user(env
->regs
[9], &sc
->regs
.r9
);
2931 __get_user(env
->regs
[10], &sc
->regs
.r10
);
2932 __get_user(env
->regs
[11], &sc
->regs
.r11
);
2933 __get_user(env
->regs
[12], &sc
->regs
.r12
);
2934 __get_user(env
->regs
[13], &sc
->regs
.r13
);
2935 __get_user(env
->regs
[14], &sc
->usp
);
2936 __get_user(env
->regs
[15], &sc
->regs
.acr
);
2937 __get_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
2938 __get_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
2939 __get_user(env
->pc
, &sc
->regs
.erp
);
2942 static abi_ulong
get_sigframe(CPUState
*env
, int framesize
)
2945 /* Align the stack downwards to 4. */
2946 sp
= (env
->regs
[R_SP
] & ~3);
2947 return sp
- framesize
;
2950 static void setup_frame(int sig
, struct target_sigaction
*ka
,
2951 target_sigset_t
*set
, CPUState
*env
)
2953 struct target_signal_frame
*frame
;
2954 abi_ulong frame_addr
;
2958 frame_addr
= get_sigframe(env
, sizeof *frame
);
2959 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
2963 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2964 * use this trampoline anymore but it sets it up for GDB.
2965 * In QEMU, using the trampoline simplifies things a bit so we use it.
2967 * This is movu.w __NR_sigreturn, r9; break 13;
2969 err
|= __put_user(0x9c5f, frame
->retcode
+0);
2970 err
|= __put_user(TARGET_NR_sigreturn
,
2972 err
|= __put_user(0xe93d, frame
->retcode
+4);
2974 /* Save the mask. */
2975 err
|= __put_user(set
->sig
[0], &frame
->sc
.oldmask
);
2979 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
2980 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
2984 setup_sigcontext(&frame
->sc
, env
);
2986 /* Move the stack and setup the arguments for the handler. */
2987 env
->regs
[R_SP
] = (uint32_t) (unsigned long) frame
;
2988 env
->regs
[10] = sig
;
2989 env
->pc
= (unsigned long) ka
->_sa_handler
;
2990 /* Link SRP so the guest returns through the trampoline. */
2991 env
->pregs
[PR_SRP
] = (uint32_t) (unsigned long) &frame
->retcode
[0];
2993 unlock_user_struct(frame
, frame_addr
, 1);
2996 unlock_user_struct(frame
, frame_addr
, 1);
2997 force_sig(TARGET_SIGSEGV
);
3000 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3001 target_siginfo_t
*info
,
3002 target_sigset_t
*set
, CPUState
*env
)
3004 fprintf(stderr
, "CRIS setup_rt_frame: not implemented\n");
3007 long do_sigreturn(CPUState
*env
)
3009 struct target_signal_frame
*frame
;
3010 abi_ulong frame_addr
;
3011 target_sigset_t target_set
;
3015 frame_addr
= env
->regs
[R_SP
];
3016 /* Make sure the guest isn't playing games. */
3017 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
3020 /* Restore blocked signals */
3021 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
3023 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3024 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
3027 target_to_host_sigset_internal(&set
, &target_set
);
3028 sigprocmask(SIG_SETMASK
, &set
, NULL
);
3030 restore_sigcontext(&frame
->sc
, env
);
3031 unlock_user_struct(frame
, frame_addr
, 0);
3032 return env
->regs
[10];
3034 unlock_user_struct(frame
, frame_addr
, 0);
3035 force_sig(TARGET_SIGSEGV
);
3038 long do_rt_sigreturn(CPUState
*env
)
3040 fprintf(stderr
, "CRIS do_rt_sigreturn: not implemented\n");
3041 return -TARGET_ENOSYS
;
3046 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3047 target_sigset_t
*set
, CPUState
*env
)
3049 fprintf(stderr
, "setup_frame: not implemented\n");
3052 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3053 target_siginfo_t
*info
,
3054 target_sigset_t
*set
, CPUState
*env
)
3056 fprintf(stderr
, "setup_rt_frame: not implemented\n");
3059 long do_sigreturn(CPUState
*env
)
3061 fprintf(stderr
, "do_sigreturn: not implemented\n");
3062 return -TARGET_ENOSYS
;
3065 long do_rt_sigreturn(CPUState
*env
)
3067 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
3068 return -TARGET_ENOSYS
;
3073 void process_pending_signals(CPUState
*cpu_env
)
3077 sigset_t set
, old_set
;
3078 target_sigset_t target_old_set
;
3079 struct emulated_sigtable
*k
;
3080 struct target_sigaction
*sa
;
3082 TaskState
*ts
= cpu_env
->opaque
;
3084 if (!ts
->signal_pending
)
3087 /* FIXME: This is not threadsafe. */
3089 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
3094 /* if no signal is pending, just return */
3095 ts
->signal_pending
= 0;
3100 fprintf(stderr
, "qemu: process signal %d\n", sig
);
3102 /* dequeue signal */
3108 sig
= gdb_handlesig (cpu_env
, sig
);
3110 fprintf (stderr
, "Lost signal\n");
3114 sa
= &sigact_table
[sig
- 1];
3115 handler
= sa
->_sa_handler
;
3116 if (handler
== TARGET_SIG_DFL
) {
3117 /* default handler : ignore some signal. The other are fatal */
3118 if (sig
!= TARGET_SIGCHLD
&&
3119 sig
!= TARGET_SIGURG
&&
3120 sig
!= TARGET_SIGWINCH
) {
3123 } else if (handler
== TARGET_SIG_IGN
) {
3125 } else if (handler
== TARGET_SIG_ERR
) {
3128 /* compute the blocked signals during the handler execution */
3129 target_to_host_sigset(&set
, &sa
->sa_mask
);
3130 /* SA_NODEFER indicates that the current signal should not be
3131 blocked during the handler */
3132 if (!(sa
->sa_flags
& TARGET_SA_NODEFER
))
3133 sigaddset(&set
, target_to_host_signal(sig
));
3135 /* block signals in the handler using Linux */
3136 sigprocmask(SIG_BLOCK
, &set
, &old_set
);
3137 /* save the previous blocked signal state to restore it at the
3138 end of the signal execution (see do_sigreturn) */
3139 host_to_target_sigset_internal(&target_old_set
, &old_set
);
3141 /* if the CPU is in VM86 mode, we restore the 32 bit values */
3142 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
3144 CPUX86State
*env
= cpu_env
;
3145 if (env
->eflags
& VM_MASK
)
3146 save_v86_state(env
);
3149 /* prepare the stack frame of the virtual CPU */
3150 if (sa
->sa_flags
& TARGET_SA_SIGINFO
)
3151 setup_rt_frame(sig
, sa
, &q
->info
, &target_old_set
, cpu_env
);
3153 setup_frame(sig
, sa
, &target_old_set
, cpu_env
);
3154 if (sa
->sa_flags
& TARGET_SA_RESETHAND
)
3155 sa
->_sa_handler
= TARGET_SIG_DFL
;
3158 free_sigqueue(cpu_env
, q
);