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, see <http://www.gnu.org/licenses/>.
26 #include <sys/ucontext.h>
27 #include <sys/resource.h>
30 #include "qemu-common.h"
31 #include "target_signal.h"
33 //#define DEBUG_SIGNAL
35 static struct target_sigaltstack target_sigaltstack_used
= {
38 .ss_flags
= TARGET_SS_DISABLE
,
41 static struct target_sigaction sigact_table
[TARGET_NSIG
];
43 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
46 static uint8_t host_to_target_signal_table
[_NSIG
] = {
47 [SIGHUP
] = TARGET_SIGHUP
,
48 [SIGINT
] = TARGET_SIGINT
,
49 [SIGQUIT
] = TARGET_SIGQUIT
,
50 [SIGILL
] = TARGET_SIGILL
,
51 [SIGTRAP
] = TARGET_SIGTRAP
,
52 [SIGABRT
] = TARGET_SIGABRT
,
53 /* [SIGIOT] = TARGET_SIGIOT,*/
54 [SIGBUS
] = TARGET_SIGBUS
,
55 [SIGFPE
] = TARGET_SIGFPE
,
56 [SIGKILL
] = TARGET_SIGKILL
,
57 [SIGUSR1
] = TARGET_SIGUSR1
,
58 [SIGSEGV
] = TARGET_SIGSEGV
,
59 [SIGUSR2
] = TARGET_SIGUSR2
,
60 [SIGPIPE
] = TARGET_SIGPIPE
,
61 [SIGALRM
] = TARGET_SIGALRM
,
62 [SIGTERM
] = TARGET_SIGTERM
,
64 [SIGSTKFLT
] = TARGET_SIGSTKFLT
,
66 [SIGCHLD
] = TARGET_SIGCHLD
,
67 [SIGCONT
] = TARGET_SIGCONT
,
68 [SIGSTOP
] = TARGET_SIGSTOP
,
69 [SIGTSTP
] = TARGET_SIGTSTP
,
70 [SIGTTIN
] = TARGET_SIGTTIN
,
71 [SIGTTOU
] = TARGET_SIGTTOU
,
72 [SIGURG
] = TARGET_SIGURG
,
73 [SIGXCPU
] = TARGET_SIGXCPU
,
74 [SIGXFSZ
] = TARGET_SIGXFSZ
,
75 [SIGVTALRM
] = TARGET_SIGVTALRM
,
76 [SIGPROF
] = TARGET_SIGPROF
,
77 [SIGWINCH
] = TARGET_SIGWINCH
,
78 [SIGIO
] = TARGET_SIGIO
,
79 [SIGPWR
] = TARGET_SIGPWR
,
80 [SIGSYS
] = TARGET_SIGSYS
,
81 /* next signals stay the same */
82 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
83 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
84 To fix this properly we need to do manual signal delivery multiplexed
85 over a single host signal. */
86 [__SIGRTMIN
] = __SIGRTMAX
,
87 [__SIGRTMAX
] = __SIGRTMIN
,
89 static uint8_t target_to_host_signal_table
[_NSIG
];
91 static inline int on_sig_stack(unsigned long sp
)
93 return (sp
- target_sigaltstack_used
.ss_sp
94 < target_sigaltstack_used
.ss_size
);
97 static inline int sas_ss_flags(unsigned long sp
)
99 return (target_sigaltstack_used
.ss_size
== 0 ? SS_DISABLE
100 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
103 int host_to_target_signal(int sig
)
105 if (sig
< 0 || sig
>= _NSIG
)
107 return host_to_target_signal_table
[sig
];
110 int target_to_host_signal(int sig
)
112 if (sig
< 0 || sig
>= _NSIG
)
114 return target_to_host_signal_table
[sig
];
117 static inline void target_sigemptyset(target_sigset_t
*set
)
119 memset(set
, 0, sizeof(*set
));
122 static inline void target_sigaddset(target_sigset_t
*set
, int signum
)
125 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
126 set
->sig
[signum
/ TARGET_NSIG_BPW
] |= mask
;
129 static inline int target_sigismember(const target_sigset_t
*set
, int signum
)
132 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
133 return ((set
->sig
[signum
/ TARGET_NSIG_BPW
] & mask
) != 0);
136 static void host_to_target_sigset_internal(target_sigset_t
*d
,
140 target_sigemptyset(d
);
141 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
142 if (sigismember(s
, i
)) {
143 target_sigaddset(d
, host_to_target_signal(i
));
148 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
153 host_to_target_sigset_internal(&d1
, s
);
154 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
155 d
->sig
[i
] = tswapal(d1
.sig
[i
]);
158 static void target_to_host_sigset_internal(sigset_t
*d
,
159 const target_sigset_t
*s
)
163 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
164 if (target_sigismember(s
, i
)) {
165 sigaddset(d
, target_to_host_signal(i
));
170 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
175 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
176 s1
.sig
[i
] = tswapal(s
->sig
[i
]);
177 target_to_host_sigset_internal(d
, &s1
);
180 void host_to_target_old_sigset(abi_ulong
*old_sigset
,
181 const sigset_t
*sigset
)
184 host_to_target_sigset(&d
, sigset
);
185 *old_sigset
= d
.sig
[0];
188 void target_to_host_old_sigset(sigset_t
*sigset
,
189 const abi_ulong
*old_sigset
)
194 d
.sig
[0] = *old_sigset
;
195 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
197 target_to_host_sigset(sigset
, &d
);
200 /* siginfo conversion */
202 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
203 const siginfo_t
*info
)
205 int sig
= host_to_target_signal(info
->si_signo
);
206 tinfo
->si_signo
= sig
;
208 tinfo
->si_code
= info
->si_code
;
210 if (sig
== TARGET_SIGILL
|| sig
== TARGET_SIGFPE
|| sig
== TARGET_SIGSEGV
211 || sig
== TARGET_SIGBUS
|| sig
== TARGET_SIGTRAP
) {
212 /* Should never come here, but who knows. The information for
213 the target is irrelevant. */
214 tinfo
->_sifields
._sigfault
._addr
= 0;
215 } else if (sig
== TARGET_SIGIO
) {
216 tinfo
->_sifields
._sigpoll
._band
= info
->si_band
;
217 tinfo
->_sifields
._sigpoll
._fd
= info
->si_fd
;
218 } else if (sig
== TARGET_SIGCHLD
) {
219 tinfo
->_sifields
._sigchld
._pid
= info
->si_pid
;
220 tinfo
->_sifields
._sigchld
._uid
= info
->si_uid
;
221 tinfo
->_sifields
._sigchld
._status
222 = host_to_target_waitstatus(info
->si_status
);
223 tinfo
->_sifields
._sigchld
._utime
= info
->si_utime
;
224 tinfo
->_sifields
._sigchld
._stime
= info
->si_stime
;
225 } else if (sig
>= TARGET_SIGRTMIN
) {
226 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
227 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
228 /* XXX: potential problem if 64 bit */
229 tinfo
->_sifields
._rt
._sigval
.sival_ptr
230 = (abi_ulong
)(unsigned long)info
->si_value
.sival_ptr
;
234 static void tswap_siginfo(target_siginfo_t
*tinfo
,
235 const target_siginfo_t
*info
)
237 int sig
= info
->si_signo
;
238 tinfo
->si_signo
= tswap32(sig
);
239 tinfo
->si_errno
= tswap32(info
->si_errno
);
240 tinfo
->si_code
= tswap32(info
->si_code
);
242 if (sig
== TARGET_SIGILL
|| sig
== TARGET_SIGFPE
|| sig
== TARGET_SIGSEGV
243 || sig
== TARGET_SIGBUS
|| sig
== TARGET_SIGTRAP
) {
244 tinfo
->_sifields
._sigfault
._addr
245 = tswapal(info
->_sifields
._sigfault
._addr
);
246 } else if (sig
== TARGET_SIGIO
) {
247 tinfo
->_sifields
._sigpoll
._band
248 = tswap32(info
->_sifields
._sigpoll
._band
);
249 tinfo
->_sifields
._sigpoll
._fd
= tswap32(info
->_sifields
._sigpoll
._fd
);
250 } else if (sig
== TARGET_SIGCHLD
) {
251 tinfo
->_sifields
._sigchld
._pid
252 = tswap32(info
->_sifields
._sigchld
._pid
);
253 tinfo
->_sifields
._sigchld
._uid
254 = tswap32(info
->_sifields
._sigchld
._uid
);
255 tinfo
->_sifields
._sigchld
._status
256 = tswap32(info
->_sifields
._sigchld
._status
);
257 tinfo
->_sifields
._sigchld
._utime
258 = tswapal(info
->_sifields
._sigchld
._utime
);
259 tinfo
->_sifields
._sigchld
._stime
260 = tswapal(info
->_sifields
._sigchld
._stime
);
261 } else if (sig
>= TARGET_SIGRTMIN
) {
262 tinfo
->_sifields
._rt
._pid
= tswap32(info
->_sifields
._rt
._pid
);
263 tinfo
->_sifields
._rt
._uid
= tswap32(info
->_sifields
._rt
._uid
);
264 tinfo
->_sifields
._rt
._sigval
.sival_ptr
265 = tswapal(info
->_sifields
._rt
._sigval
.sival_ptr
);
270 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
272 host_to_target_siginfo_noswap(tinfo
, info
);
273 tswap_siginfo(tinfo
, tinfo
);
276 /* XXX: we support only POSIX RT signals are used. */
277 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
278 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
280 info
->si_signo
= tswap32(tinfo
->si_signo
);
281 info
->si_errno
= tswap32(tinfo
->si_errno
);
282 info
->si_code
= tswap32(tinfo
->si_code
);
283 info
->si_pid
= tswap32(tinfo
->_sifields
._rt
._pid
);
284 info
->si_uid
= tswap32(tinfo
->_sifields
._rt
._uid
);
285 info
->si_value
.sival_ptr
=
286 (void *)(long)tswapal(tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
289 static int fatal_signal (int sig
)
294 case TARGET_SIGWINCH
:
295 /* Ignored by default. */
302 /* Job control signals. */
309 /* returns 1 if given signal should dump core if not handled */
310 static int core_dump_signal(int sig
)
326 void signal_init(void)
328 struct sigaction act
;
329 struct sigaction oact
;
333 /* generate signal conversion tables */
334 for(i
= 1; i
< _NSIG
; i
++) {
335 if (host_to_target_signal_table
[i
] == 0)
336 host_to_target_signal_table
[i
] = i
;
338 for(i
= 1; i
< _NSIG
; i
++) {
339 j
= host_to_target_signal_table
[i
];
340 target_to_host_signal_table
[j
] = i
;
343 /* set all host signal handlers. ALL signals are blocked during
344 the handlers to serialize them. */
345 memset(sigact_table
, 0, sizeof(sigact_table
));
347 sigfillset(&act
.sa_mask
);
348 act
.sa_flags
= SA_SIGINFO
;
349 act
.sa_sigaction
= host_signal_handler
;
350 for(i
= 1; i
<= TARGET_NSIG
; i
++) {
351 host_sig
= target_to_host_signal(i
);
352 sigaction(host_sig
, NULL
, &oact
);
353 if (oact
.sa_sigaction
== (void *)SIG_IGN
) {
354 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_IGN
;
355 } else if (oact
.sa_sigaction
== (void *)SIG_DFL
) {
356 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_DFL
;
358 /* If there's already a handler installed then something has
359 gone horribly wrong, so don't even try to handle that case. */
360 /* Install some handlers for our own use. We need at least
361 SIGSEGV and SIGBUS, to detect exceptions. We can not just
362 trap all signals because it affects syscall interrupt
363 behavior. But do trap all default-fatal signals. */
364 if (fatal_signal (i
))
365 sigaction(host_sig
, &act
, NULL
);
369 /* signal queue handling */
371 static inline struct sigqueue
*alloc_sigqueue(CPUArchState
*env
)
373 CPUState
*cpu
= ENV_GET_CPU(env
);
374 TaskState
*ts
= cpu
->opaque
;
375 struct sigqueue
*q
= ts
->first_free
;
378 ts
->first_free
= q
->next
;
382 static inline void free_sigqueue(CPUArchState
*env
, struct sigqueue
*q
)
384 CPUState
*cpu
= ENV_GET_CPU(env
);
385 TaskState
*ts
= cpu
->opaque
;
387 q
->next
= ts
->first_free
;
391 /* abort execution with signal */
392 static void QEMU_NORETURN
force_sig(int target_sig
)
394 CPUState
*cpu
= thread_cpu
;
395 CPUArchState
*env
= cpu
->env_ptr
;
396 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
397 int host_sig
, core_dumped
= 0;
398 struct sigaction act
;
399 host_sig
= target_to_host_signal(target_sig
);
400 gdb_signalled(env
, target_sig
);
402 /* dump core if supported by target binary format */
403 if (core_dump_signal(target_sig
) && (ts
->bprm
->core_dump
!= NULL
)) {
406 ((*ts
->bprm
->core_dump
)(target_sig
, env
) == 0);
409 /* we already dumped the core of target process, we don't want
410 * a coredump of qemu itself */
411 struct rlimit nodump
;
412 getrlimit(RLIMIT_CORE
, &nodump
);
414 setrlimit(RLIMIT_CORE
, &nodump
);
415 (void) fprintf(stderr
, "qemu: uncaught target signal %d (%s) - %s\n",
416 target_sig
, strsignal(host_sig
), "core dumped" );
419 /* The proper exit code for dying from an uncaught signal is
420 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
421 * a negative value. To get the proper exit code we need to
422 * actually die from an uncaught signal. Here the default signal
423 * handler is installed, we send ourself a signal and we wait for
425 sigfillset(&act
.sa_mask
);
426 act
.sa_handler
= SIG_DFL
;
428 sigaction(host_sig
, &act
, NULL
);
430 /* For some reason raise(host_sig) doesn't send the signal when
431 * statically linked on x86-64. */
432 kill(getpid(), host_sig
);
434 /* Make sure the signal isn't masked (just reuse the mask inside
436 sigdelset(&act
.sa_mask
, host_sig
);
437 sigsuspend(&act
.sa_mask
);
443 /* queue a signal so that it will be send to the virtual CPU as soon
445 int queue_signal(CPUArchState
*env
, int sig
, target_siginfo_t
*info
)
447 CPUState
*cpu
= ENV_GET_CPU(env
);
448 TaskState
*ts
= cpu
->opaque
;
449 struct emulated_sigtable
*k
;
450 struct sigqueue
*q
, **pq
;
454 #if defined(DEBUG_SIGNAL)
455 fprintf(stderr
, "queue_signal: sig=%d\n",
458 k
= &ts
->sigtab
[sig
- 1];
459 queue
= gdb_queuesig ();
460 handler
= sigact_table
[sig
- 1]._sa_handler
;
461 if (!queue
&& handler
== TARGET_SIG_DFL
) {
462 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
463 kill(getpid(),SIGSTOP
);
466 /* default handler : ignore some signal. The other are fatal */
467 if (sig
!= TARGET_SIGCHLD
&&
468 sig
!= TARGET_SIGURG
&&
469 sig
!= TARGET_SIGWINCH
&&
470 sig
!= TARGET_SIGCONT
) {
473 return 0; /* indicate ignored */
475 } else if (!queue
&& handler
== TARGET_SIG_IGN
) {
478 } else if (!queue
&& handler
== TARGET_SIG_ERR
) {
482 if (sig
< TARGET_SIGRTMIN
) {
483 /* if non real time signal, we queue exactly one signal */
493 q
= alloc_sigqueue(env
);
504 /* signal that a new signal is pending */
505 ts
->signal_pending
= 1;
506 return 1; /* indicates that the signal was queued */
510 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
513 CPUArchState
*env
= thread_cpu
->env_ptr
;
515 target_siginfo_t tinfo
;
517 /* the CPU emulator uses some host signals to detect exceptions,
518 we forward to it some signals */
519 if ((host_signum
== SIGSEGV
|| host_signum
== SIGBUS
)
520 && info
->si_code
> 0) {
521 if (cpu_signal_handler(host_signum
, info
, puc
))
525 /* get target signal number */
526 sig
= host_to_target_signal(host_signum
);
527 if (sig
< 1 || sig
> TARGET_NSIG
)
529 #if defined(DEBUG_SIGNAL)
530 fprintf(stderr
, "qemu: got signal %d\n", sig
);
532 host_to_target_siginfo_noswap(&tinfo
, info
);
533 if (queue_signal(env
, sig
, &tinfo
) == 1) {
534 /* interrupt the virtual CPU as soon as possible */
535 cpu_exit(thread_cpu
);
539 /* do_sigaltstack() returns target values and errnos. */
540 /* compare linux/kernel/signal.c:do_sigaltstack() */
541 abi_long
do_sigaltstack(abi_ulong uss_addr
, abi_ulong uoss_addr
, abi_ulong sp
)
544 struct target_sigaltstack oss
;
546 /* XXX: test errors */
549 __put_user(target_sigaltstack_used
.ss_sp
, &oss
.ss_sp
);
550 __put_user(target_sigaltstack_used
.ss_size
, &oss
.ss_size
);
551 __put_user(sas_ss_flags(sp
), &oss
.ss_flags
);
556 struct target_sigaltstack
*uss
;
557 struct target_sigaltstack ss
;
559 ret
= -TARGET_EFAULT
;
560 if (!lock_user_struct(VERIFY_READ
, uss
, uss_addr
, 1)
561 || __get_user(ss
.ss_sp
, &uss
->ss_sp
)
562 || __get_user(ss
.ss_size
, &uss
->ss_size
)
563 || __get_user(ss
.ss_flags
, &uss
->ss_flags
))
565 unlock_user_struct(uss
, uss_addr
, 0);
568 if (on_sig_stack(sp
))
571 ret
= -TARGET_EINVAL
;
572 if (ss
.ss_flags
!= TARGET_SS_DISABLE
573 && ss
.ss_flags
!= TARGET_SS_ONSTACK
577 if (ss
.ss_flags
== TARGET_SS_DISABLE
) {
581 ret
= -TARGET_ENOMEM
;
582 if (ss
.ss_size
< MINSIGSTKSZ
)
586 target_sigaltstack_used
.ss_sp
= ss
.ss_sp
;
587 target_sigaltstack_used
.ss_size
= ss
.ss_size
;
591 ret
= -TARGET_EFAULT
;
592 if (copy_to_user(uoss_addr
, &oss
, sizeof(oss
)))
601 /* do_sigaction() return host values and errnos */
602 int do_sigaction(int sig
, const struct target_sigaction
*act
,
603 struct target_sigaction
*oact
)
605 struct target_sigaction
*k
;
606 struct sigaction act1
;
610 if (sig
< 1 || sig
> TARGET_NSIG
|| sig
== TARGET_SIGKILL
|| sig
== TARGET_SIGSTOP
)
612 k
= &sigact_table
[sig
- 1];
613 #if defined(DEBUG_SIGNAL)
614 fprintf(stderr
, "sigaction sig=%d act=0x%p, oact=0x%p\n",
618 __put_user(k
->_sa_handler
, &oact
->_sa_handler
);
619 __put_user(k
->sa_flags
, &oact
->sa_flags
);
620 #if !defined(TARGET_MIPS)
621 __put_user(k
->sa_restorer
, &oact
->sa_restorer
);
624 oact
->sa_mask
= k
->sa_mask
;
627 /* FIXME: This is not threadsafe. */
628 __get_user(k
->_sa_handler
, &act
->_sa_handler
);
629 __get_user(k
->sa_flags
, &act
->sa_flags
);
630 #if !defined(TARGET_MIPS)
631 __get_user(k
->sa_restorer
, &act
->sa_restorer
);
633 /* To be swapped in target_to_host_sigset. */
634 k
->sa_mask
= act
->sa_mask
;
636 /* we update the host linux signal state */
637 host_sig
= target_to_host_signal(sig
);
638 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
639 sigfillset(&act1
.sa_mask
);
640 act1
.sa_flags
= SA_SIGINFO
;
641 if (k
->sa_flags
& TARGET_SA_RESTART
)
642 act1
.sa_flags
|= SA_RESTART
;
643 /* NOTE: it is important to update the host kernel signal
644 ignore state to avoid getting unexpected interrupted
646 if (k
->_sa_handler
== TARGET_SIG_IGN
) {
647 act1
.sa_sigaction
= (void *)SIG_IGN
;
648 } else if (k
->_sa_handler
== TARGET_SIG_DFL
) {
649 if (fatal_signal (sig
))
650 act1
.sa_sigaction
= host_signal_handler
;
652 act1
.sa_sigaction
= (void *)SIG_DFL
;
654 act1
.sa_sigaction
= host_signal_handler
;
656 ret
= sigaction(host_sig
, &act1
, NULL
);
662 static inline int copy_siginfo_to_user(target_siginfo_t
*tinfo
,
663 const target_siginfo_t
*info
)
665 tswap_siginfo(tinfo
, info
);
669 static inline int current_exec_domain_sig(int sig
)
671 return /* current->exec_domain && current->exec_domain->signal_invmap
672 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig
;
675 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
677 /* from the Linux kernel */
679 struct target_fpreg
{
680 uint16_t significand
[4];
684 struct target_fpxreg
{
685 uint16_t significand
[4];
690 struct target_xmmreg
{
691 abi_ulong element
[4];
694 struct target_fpstate
{
695 /* Regular FPU environment */
703 struct target_fpreg _st
[8];
705 uint16_t magic
; /* 0xffff = regular FPU data only */
707 /* FXSR FPU environment */
708 abi_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
711 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
712 struct target_xmmreg _xmm
[8];
713 abi_ulong padding
[56];
716 #define X86_FXSR_MAGIC 0x0000
718 struct target_sigcontext
{
736 abi_ulong esp_at_signal
;
738 abi_ulong fpstate
; /* pointer */
743 struct target_ucontext
{
746 target_stack_t tuc_stack
;
747 struct target_sigcontext tuc_mcontext
;
748 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
755 struct target_sigcontext sc
;
756 struct target_fpstate fpstate
;
757 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
767 struct target_siginfo info
;
768 struct target_ucontext uc
;
769 struct target_fpstate fpstate
;
774 * Set up a signal frame.
777 /* XXX: save x87 state */
779 setup_sigcontext(struct target_sigcontext
*sc
, struct target_fpstate
*fpstate
,
780 CPUX86State
*env
, abi_ulong mask
, abi_ulong fpstate_addr
)
782 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
786 /* already locked in setup_frame() */
787 err
|= __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
788 err
|= __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
789 err
|= __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
790 err
|= __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
791 err
|= __put_user(env
->regs
[R_EDI
], &sc
->edi
);
792 err
|= __put_user(env
->regs
[R_ESI
], &sc
->esi
);
793 err
|= __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
794 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp
);
795 err
|= __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
796 err
|= __put_user(env
->regs
[R_EDX
], &sc
->edx
);
797 err
|= __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
798 err
|= __put_user(env
->regs
[R_EAX
], &sc
->eax
);
799 err
|= __put_user(cs
->exception_index
, &sc
->trapno
);
800 err
|= __put_user(env
->error_code
, &sc
->err
);
801 err
|= __put_user(env
->eip
, &sc
->eip
);
802 err
|= __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
803 err
|= __put_user(env
->eflags
, &sc
->eflags
);
804 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
805 err
|= __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
807 cpu_x86_fsave(env
, fpstate_addr
, 1);
808 fpstate
->status
= fpstate
->sw
;
810 err
|= __put_user(magic
, &fpstate
->magic
);
811 err
|= __put_user(fpstate_addr
, &sc
->fpstate
);
813 /* non-iBCS2 extensions.. */
814 err
|= __put_user(mask
, &sc
->oldmask
);
815 err
|= __put_user(env
->cr
[2], &sc
->cr2
);
820 * Determine which stack to use..
823 static inline abi_ulong
824 get_sigframe(struct target_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
828 /* Default to using normal stack */
829 esp
= env
->regs
[R_ESP
];
830 /* This is the X/Open sanctioned signal stack switching. */
831 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
832 if (sas_ss_flags(esp
) == 0)
833 esp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
836 /* This is the legacy signal stack switching. */
838 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
839 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
841 esp
= (unsigned long) ka
->sa_restorer
;
843 return (esp
- frame_size
) & -8ul;
846 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
847 static void setup_frame(int sig
, struct target_sigaction
*ka
,
848 target_sigset_t
*set
, CPUX86State
*env
)
850 abi_ulong frame_addr
;
851 struct sigframe
*frame
;
854 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
856 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
859 err
|= __put_user(current_exec_domain_sig(sig
),
864 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0],
865 frame_addr
+ offsetof(struct sigframe
, fpstate
));
869 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
870 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
874 /* Set up to return from userspace. If provided, use a stub
875 already in userspace. */
876 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
877 err
|= __put_user(ka
->sa_restorer
, &frame
->pretcode
);
880 abi_ulong retcode_addr
;
881 retcode_addr
= frame_addr
+ offsetof(struct sigframe
, retcode
);
882 err
|= __put_user(retcode_addr
, &frame
->pretcode
);
883 /* This is popl %eax ; movl $,%eax ; int $0x80 */
885 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+0));
886 err
|= __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
888 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+6));
894 /* Set up registers for signal handler */
895 env
->regs
[R_ESP
] = frame_addr
;
896 env
->eip
= ka
->_sa_handler
;
898 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
899 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
900 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
901 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
902 env
->eflags
&= ~TF_MASK
;
904 unlock_user_struct(frame
, frame_addr
, 1);
909 unlock_user_struct(frame
, frame_addr
, 1);
910 if (sig
== TARGET_SIGSEGV
)
911 ka
->_sa_handler
= TARGET_SIG_DFL
;
912 force_sig(TARGET_SIGSEGV
/* , current */);
915 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
916 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
917 target_siginfo_t
*info
,
918 target_sigset_t
*set
, CPUX86State
*env
)
920 abi_ulong frame_addr
, addr
;
921 struct rt_sigframe
*frame
;
924 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
926 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
929 err
|= __put_user(current_exec_domain_sig(sig
),
931 addr
= frame_addr
+ offsetof(struct rt_sigframe
, info
);
932 err
|= __put_user(addr
, &frame
->pinfo
);
933 addr
= frame_addr
+ offsetof(struct rt_sigframe
, uc
);
934 err
|= __put_user(addr
, &frame
->puc
);
935 err
|= copy_siginfo_to_user(&frame
->info
, info
);
939 /* Create the ucontext. */
940 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
941 err
|= __put_user(0, &frame
->uc
.tuc_link
);
942 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
943 &frame
->uc
.tuc_stack
.ss_sp
);
944 err
|= __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
945 &frame
->uc
.tuc_stack
.ss_flags
);
946 err
|= __put_user(target_sigaltstack_used
.ss_size
,
947 &frame
->uc
.tuc_stack
.ss_size
);
948 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, &frame
->fpstate
,
950 frame_addr
+ offsetof(struct rt_sigframe
, fpstate
));
951 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
952 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
956 /* Set up to return from userspace. If provided, use a stub
957 already in userspace. */
958 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
959 err
|= __put_user(ka
->sa_restorer
, &frame
->pretcode
);
962 addr
= frame_addr
+ offsetof(struct rt_sigframe
, retcode
);
963 err
|= __put_user(addr
, &frame
->pretcode
);
964 /* This is movl $,%eax ; int $0x80 */
965 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
966 err
|= __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
968 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+5));
974 /* Set up registers for signal handler */
975 env
->regs
[R_ESP
] = frame_addr
;
976 env
->eip
= ka
->_sa_handler
;
978 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
979 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
980 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
981 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
982 env
->eflags
&= ~TF_MASK
;
984 unlock_user_struct(frame
, frame_addr
, 1);
989 unlock_user_struct(frame
, frame_addr
, 1);
990 if (sig
== TARGET_SIGSEGV
)
991 ka
->_sa_handler
= TARGET_SIG_DFL
;
992 force_sig(TARGET_SIGSEGV
/* , current */);
996 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
, int *peax
)
998 unsigned int err
= 0;
999 abi_ulong fpstate_addr
;
1000 unsigned int tmpflags
;
1002 cpu_x86_load_seg(env
, R_GS
, tswap16(sc
->gs
));
1003 cpu_x86_load_seg(env
, R_FS
, tswap16(sc
->fs
));
1004 cpu_x86_load_seg(env
, R_ES
, tswap16(sc
->es
));
1005 cpu_x86_load_seg(env
, R_DS
, tswap16(sc
->ds
));
1007 env
->regs
[R_EDI
] = tswapl(sc
->edi
);
1008 env
->regs
[R_ESI
] = tswapl(sc
->esi
);
1009 env
->regs
[R_EBP
] = tswapl(sc
->ebp
);
1010 env
->regs
[R_ESP
] = tswapl(sc
->esp
);
1011 env
->regs
[R_EBX
] = tswapl(sc
->ebx
);
1012 env
->regs
[R_EDX
] = tswapl(sc
->edx
);
1013 env
->regs
[R_ECX
] = tswapl(sc
->ecx
);
1014 env
->eip
= tswapl(sc
->eip
);
1016 cpu_x86_load_seg(env
, R_CS
, lduw_p(&sc
->cs
) | 3);
1017 cpu_x86_load_seg(env
, R_SS
, lduw_p(&sc
->ss
) | 3);
1019 tmpflags
= tswapl(sc
->eflags
);
1020 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
1021 // regs->orig_eax = -1; /* disable syscall checks */
1023 fpstate_addr
= tswapl(sc
->fpstate
);
1024 if (fpstate_addr
!= 0) {
1025 if (!access_ok(VERIFY_READ
, fpstate_addr
,
1026 sizeof(struct target_fpstate
)))
1028 cpu_x86_frstor(env
, fpstate_addr
, 1);
1031 *peax
= tswapl(sc
->eax
);
1037 long do_sigreturn(CPUX86State
*env
)
1039 struct sigframe
*frame
;
1040 abi_ulong frame_addr
= env
->regs
[R_ESP
] - 8;
1041 target_sigset_t target_set
;
1045 #if defined(DEBUG_SIGNAL)
1046 fprintf(stderr
, "do_sigreturn\n");
1048 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1050 /* set blocked signals */
1051 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
1053 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1054 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
1058 target_to_host_sigset_internal(&set
, &target_set
);
1059 sigprocmask(SIG_SETMASK
, &set
, NULL
);
1061 /* restore registers */
1062 if (restore_sigcontext(env
, &frame
->sc
, &eax
))
1064 unlock_user_struct(frame
, frame_addr
, 0);
1068 unlock_user_struct(frame
, frame_addr
, 0);
1069 force_sig(TARGET_SIGSEGV
);
1073 long do_rt_sigreturn(CPUX86State
*env
)
1075 abi_ulong frame_addr
;
1076 struct rt_sigframe
*frame
;
1080 frame_addr
= env
->regs
[R_ESP
] - 4;
1081 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1083 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
1084 sigprocmask(SIG_SETMASK
, &set
, NULL
);
1086 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
, &eax
))
1089 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe
, uc
.tuc_stack
), 0,
1090 get_sp_from_cpustate(env
)) == -EFAULT
)
1093 unlock_user_struct(frame
, frame_addr
, 0);
1097 unlock_user_struct(frame
, frame_addr
, 0);
1098 force_sig(TARGET_SIGSEGV
);
1102 #elif defined(TARGET_AARCH64)
1104 struct target_sigcontext
{
1105 uint64_t fault_address
;
1106 /* AArch64 registers */
1111 /* 4K reserved for FP/SIMD state and future expansion */
1112 char __reserved
[4096] __attribute__((__aligned__(16)));
1115 struct target_ucontext
{
1116 abi_ulong tuc_flags
;
1118 target_stack_t tuc_stack
;
1119 target_sigset_t tuc_sigmask
;
1120 /* glibc uses a 1024-bit sigset_t */
1121 char __unused
[1024 / 8 - sizeof(target_sigset_t
)];
1122 /* last for future expansion */
1123 struct target_sigcontext tuc_mcontext
;
1127 * Header to be used at the beginning of structures extending the user
1128 * context. Such structures must be placed after the rt_sigframe on the stack
1129 * and be 16-byte aligned. The last structure must be a dummy one with the
1130 * magic and size set to 0.
1132 struct target_aarch64_ctx
{
1137 #define TARGET_FPSIMD_MAGIC 0x46508001
1139 struct target_fpsimd_context
{
1140 struct target_aarch64_ctx head
;
1143 uint64_t vregs
[32 * 2]; /* really uint128_t vregs[32] */
1147 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1148 * user space as it will change with the addition of new context. User space
1149 * should check the magic/size information.
1151 struct target_aux_context
{
1152 struct target_fpsimd_context fpsimd
;
1153 /* additional context to be added before "end" */
1154 struct target_aarch64_ctx end
;
1157 struct target_rt_sigframe
{
1158 struct target_siginfo info
;
1159 struct target_ucontext uc
;
1165 static int target_setup_sigframe(struct target_rt_sigframe
*sf
,
1166 CPUARMState
*env
, target_sigset_t
*set
)
1169 struct target_aux_context
*aux
=
1170 (struct target_aux_context
*)sf
->uc
.tuc_mcontext
.__reserved
;
1172 /* set up the stack frame for unwinding */
1173 __put_user(env
->xregs
[29], &sf
->fp
);
1174 __put_user(env
->xregs
[30], &sf
->lr
);
1176 for (i
= 0; i
< 31; i
++) {
1177 __put_user(env
->xregs
[i
], &sf
->uc
.tuc_mcontext
.regs
[i
]);
1179 __put_user(env
->xregs
[31], &sf
->uc
.tuc_mcontext
.sp
);
1180 __put_user(env
->pc
, &sf
->uc
.tuc_mcontext
.pc
);
1181 __put_user(pstate_read(env
), &sf
->uc
.tuc_mcontext
.pstate
);
1183 __put_user(/*current->thread.fault_address*/ 0,
1184 &sf
->uc
.tuc_mcontext
.fault_address
);
1186 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1187 __put_user(set
->sig
[i
], &sf
->uc
.tuc_sigmask
.sig
[i
]);
1190 for (i
= 0; i
< 32; i
++) {
1191 #ifdef TARGET_WORDS_BIGENDIAN
1192 __put_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1193 __put_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2]);
1195 __put_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2]);
1196 __put_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1199 __put_user(vfp_get_fpsr(env
), &aux
->fpsimd
.fpsr
);
1200 __put_user(vfp_get_fpcr(env
), &aux
->fpsimd
.fpcr
);
1201 __put_user(TARGET_FPSIMD_MAGIC
, &aux
->fpsimd
.head
.magic
);
1202 __put_user(sizeof(struct target_fpsimd_context
),
1203 &aux
->fpsimd
.head
.size
);
1205 /* set the "end" magic */
1206 __put_user(0, &aux
->end
.magic
);
1207 __put_user(0, &aux
->end
.size
);
1212 static int target_restore_sigframe(CPUARMState
*env
,
1213 struct target_rt_sigframe
*sf
)
1217 struct target_aux_context
*aux
=
1218 (struct target_aux_context
*)sf
->uc
.tuc_mcontext
.__reserved
;
1219 uint32_t magic
, size
, fpsr
, fpcr
;
1222 target_to_host_sigset(&set
, &sf
->uc
.tuc_sigmask
);
1223 sigprocmask(SIG_SETMASK
, &set
, NULL
);
1225 for (i
= 0; i
< 31; i
++) {
1226 __get_user(env
->xregs
[i
], &sf
->uc
.tuc_mcontext
.regs
[i
]);
1229 __get_user(env
->xregs
[31], &sf
->uc
.tuc_mcontext
.sp
);
1230 __get_user(env
->pc
, &sf
->uc
.tuc_mcontext
.pc
);
1231 __get_user(pstate
, &sf
->uc
.tuc_mcontext
.pstate
);
1232 pstate_write(env
, pstate
);
1234 __get_user(magic
, &aux
->fpsimd
.head
.magic
);
1235 __get_user(size
, &aux
->fpsimd
.head
.size
);
1237 if (magic
!= TARGET_FPSIMD_MAGIC
1238 || size
!= sizeof(struct target_fpsimd_context
)) {
1242 for (i
= 0; i
< 32; i
++) {
1243 #ifdef TARGET_WORDS_BIGENDIAN
1244 __get_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1245 __get_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2]);
1247 __get_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2]);
1248 __get_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1251 __get_user(fpsr
, &aux
->fpsimd
.fpsr
);
1252 vfp_set_fpsr(env
, fpsr
);
1253 __get_user(fpcr
, &aux
->fpsimd
.fpcr
);
1254 vfp_set_fpcr(env
, fpcr
);
1259 static abi_ulong
get_sigframe(struct target_sigaction
*ka
, CPUARMState
*env
)
1263 sp
= env
->xregs
[31];
1266 * This is the X/Open sanctioned signal stack switching.
1268 if ((ka
->sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
)) {
1269 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1272 sp
= (sp
- sizeof(struct target_rt_sigframe
)) & ~15;
1277 static void target_setup_frame(int usig
, struct target_sigaction
*ka
,
1278 target_siginfo_t
*info
, target_sigset_t
*set
,
1281 struct target_rt_sigframe
*frame
;
1282 abi_ulong frame_addr
, return_addr
;
1284 frame_addr
= get_sigframe(ka
, env
);
1285 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
1289 __put_user(0, &frame
->uc
.tuc_flags
);
1290 __put_user(0, &frame
->uc
.tuc_link
);
1292 __put_user(target_sigaltstack_used
.ss_sp
,
1293 &frame
->uc
.tuc_stack
.ss_sp
);
1294 __put_user(sas_ss_flags(env
->xregs
[31]),
1295 &frame
->uc
.tuc_stack
.ss_flags
);
1296 __put_user(target_sigaltstack_used
.ss_size
,
1297 &frame
->uc
.tuc_stack
.ss_size
);
1298 target_setup_sigframe(frame
, env
, set
);
1299 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1300 return_addr
= ka
->sa_restorer
;
1302 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1303 __put_user(0xd2801168, &frame
->tramp
[0]);
1304 __put_user(0xd4000001, &frame
->tramp
[1]);
1305 return_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, tramp
);
1307 env
->xregs
[0] = usig
;
1308 env
->xregs
[31] = frame_addr
;
1309 env
->xregs
[29] = env
->xregs
[31] + offsetof(struct target_rt_sigframe
, fp
);
1310 env
->pc
= ka
->_sa_handler
;
1311 env
->xregs
[30] = return_addr
;
1313 if (copy_siginfo_to_user(&frame
->info
, info
)) {
1316 env
->xregs
[1] = frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
1317 env
->xregs
[2] = frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
1320 unlock_user_struct(frame
, frame_addr
, 1);
1324 unlock_user_struct(frame
, frame_addr
, 1);
1325 force_sig(TARGET_SIGSEGV
);
1328 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
1329 target_siginfo_t
*info
, target_sigset_t
*set
,
1332 target_setup_frame(sig
, ka
, info
, set
, env
);
1335 static void setup_frame(int sig
, struct target_sigaction
*ka
,
1336 target_sigset_t
*set
, CPUARMState
*env
)
1338 target_setup_frame(sig
, ka
, 0, set
, env
);
1341 long do_rt_sigreturn(CPUARMState
*env
)
1343 struct target_rt_sigframe
*frame
;
1344 abi_ulong frame_addr
= env
->xregs
[31];
1346 if (frame_addr
& 15) {
1350 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
1354 if (target_restore_sigframe(env
, frame
)) {
1358 if (do_sigaltstack(frame_addr
+
1359 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
1360 0, get_sp_from_cpustate(env
)) == -EFAULT
) {
1364 unlock_user_struct(frame
, frame_addr
, 0);
1365 return env
->xregs
[0];
1368 unlock_user_struct(frame
, frame_addr
, 0);
1369 force_sig(TARGET_SIGSEGV
);
1373 long do_sigreturn(CPUARMState
*env
)
1375 return do_rt_sigreturn(env
);
1378 #elif defined(TARGET_ARM)
1380 struct target_sigcontext
{
1382 abi_ulong error_code
;
1401 abi_ulong fault_address
;
1404 struct target_ucontext_v1
{
1405 abi_ulong tuc_flags
;
1407 target_stack_t tuc_stack
;
1408 struct target_sigcontext tuc_mcontext
;
1409 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1412 struct target_ucontext_v2
{
1413 abi_ulong tuc_flags
;
1415 target_stack_t tuc_stack
;
1416 struct target_sigcontext tuc_mcontext
;
1417 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1418 char __unused
[128 - sizeof(target_sigset_t
)];
1419 abi_ulong tuc_regspace
[128] __attribute__((__aligned__(8)));
1422 struct target_user_vfp
{
1423 uint64_t fpregs
[32];
1427 struct target_user_vfp_exc
{
1433 struct target_vfp_sigframe
{
1436 struct target_user_vfp ufp
;
1437 struct target_user_vfp_exc ufp_exc
;
1438 } __attribute__((__aligned__(8)));
1440 struct target_iwmmxt_sigframe
{
1444 /* Note that not all the coprocessor control registers are stored here */
1451 } __attribute__((__aligned__(8)));
1453 #define TARGET_VFP_MAGIC 0x56465001
1454 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1458 struct target_sigcontext sc
;
1459 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
1465 struct target_ucontext_v2 uc
;
1469 struct rt_sigframe_v1
1473 struct target_siginfo info
;
1474 struct target_ucontext_v1 uc
;
1478 struct rt_sigframe_v2
1480 struct target_siginfo info
;
1481 struct target_ucontext_v2 uc
;
1485 #define TARGET_CONFIG_CPU_32 1
1488 * For ARM syscalls, we encode the syscall number into the instruction.
1490 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1491 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1494 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1495 * need two 16-bit instructions.
1497 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1498 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1500 static const abi_ulong retcodes
[4] = {
1501 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
1502 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
1506 #define __get_user_error(x,p,e) __get_user(x, p)
1508 static inline int valid_user_regs(CPUARMState
*regs
)
1514 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1515 CPUARMState
*env
, abi_ulong mask
)
1517 __put_user(env
->regs
[0], &sc
->arm_r0
);
1518 __put_user(env
->regs
[1], &sc
->arm_r1
);
1519 __put_user(env
->regs
[2], &sc
->arm_r2
);
1520 __put_user(env
->regs
[3], &sc
->arm_r3
);
1521 __put_user(env
->regs
[4], &sc
->arm_r4
);
1522 __put_user(env
->regs
[5], &sc
->arm_r5
);
1523 __put_user(env
->regs
[6], &sc
->arm_r6
);
1524 __put_user(env
->regs
[7], &sc
->arm_r7
);
1525 __put_user(env
->regs
[8], &sc
->arm_r8
);
1526 __put_user(env
->regs
[9], &sc
->arm_r9
);
1527 __put_user(env
->regs
[10], &sc
->arm_r10
);
1528 __put_user(env
->regs
[11], &sc
->arm_fp
);
1529 __put_user(env
->regs
[12], &sc
->arm_ip
);
1530 __put_user(env
->regs
[13], &sc
->arm_sp
);
1531 __put_user(env
->regs
[14], &sc
->arm_lr
);
1532 __put_user(env
->regs
[15], &sc
->arm_pc
);
1533 #ifdef TARGET_CONFIG_CPU_32
1534 __put_user(cpsr_read(env
), &sc
->arm_cpsr
);
1537 __put_user(/* current->thread.trap_no */ 0, &sc
->trap_no
);
1538 __put_user(/* current->thread.error_code */ 0, &sc
->error_code
);
1539 __put_user(/* current->thread.address */ 0, &sc
->fault_address
);
1540 __put_user(mask
, &sc
->oldmask
);
1543 static inline abi_ulong
1544 get_sigframe(struct target_sigaction
*ka
, CPUARMState
*regs
, int framesize
)
1546 unsigned long sp
= regs
->regs
[13];
1549 * This is the X/Open sanctioned signal stack switching.
1551 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
))
1552 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1554 * ATPCS B01 mandates 8-byte alignment
1556 return (sp
- framesize
) & ~7;
1560 setup_return(CPUARMState
*env
, struct target_sigaction
*ka
,
1561 abi_ulong
*rc
, abi_ulong frame_addr
, int usig
, abi_ulong rc_addr
)
1563 abi_ulong handler
= ka
->_sa_handler
;
1565 int thumb
= handler
& 1;
1566 uint32_t cpsr
= cpsr_read(env
);
1575 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1576 retcode
= ka
->sa_restorer
;
1578 unsigned int idx
= thumb
;
1580 if (ka
->sa_flags
& TARGET_SA_SIGINFO
)
1583 if (__put_user(retcodes
[idx
], rc
))
1586 retcode
= rc_addr
+ thumb
;
1589 env
->regs
[0] = usig
;
1590 env
->regs
[13] = frame_addr
;
1591 env
->regs
[14] = retcode
;
1592 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
1593 cpsr_write(env
, cpsr
, 0xffffffff);
1598 static abi_ulong
*setup_sigframe_v2_vfp(abi_ulong
*regspace
, CPUARMState
*env
)
1601 struct target_vfp_sigframe
*vfpframe
;
1602 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
1603 __put_user(TARGET_VFP_MAGIC
, &vfpframe
->magic
);
1604 __put_user(sizeof(*vfpframe
), &vfpframe
->size
);
1605 for (i
= 0; i
< 32; i
++) {
1606 __put_user(float64_val(env
->vfp
.regs
[i
]), &vfpframe
->ufp
.fpregs
[i
]);
1608 __put_user(vfp_get_fpscr(env
), &vfpframe
->ufp
.fpscr
);
1609 __put_user(env
->vfp
.xregs
[ARM_VFP_FPEXC
], &vfpframe
->ufp_exc
.fpexc
);
1610 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
1611 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
1612 return (abi_ulong
*)(vfpframe
+1);
1615 static abi_ulong
*setup_sigframe_v2_iwmmxt(abi_ulong
*regspace
,
1619 struct target_iwmmxt_sigframe
*iwmmxtframe
;
1620 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
1621 __put_user(TARGET_IWMMXT_MAGIC
, &iwmmxtframe
->magic
);
1622 __put_user(sizeof(*iwmmxtframe
), &iwmmxtframe
->size
);
1623 for (i
= 0; i
< 16; i
++) {
1624 __put_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
1626 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
1627 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
1628 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
1629 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
1630 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
1631 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
1632 return (abi_ulong
*)(iwmmxtframe
+1);
1635 static void setup_sigframe_v2(struct target_ucontext_v2
*uc
,
1636 target_sigset_t
*set
, CPUARMState
*env
)
1638 struct target_sigaltstack stack
;
1640 abi_ulong
*regspace
;
1642 /* Clear all the bits of the ucontext we don't use. */
1643 memset(uc
, 0, offsetof(struct target_ucontext_v2
, tuc_mcontext
));
1645 memset(&stack
, 0, sizeof(stack
));
1646 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1647 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1648 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1649 memcpy(&uc
->tuc_stack
, &stack
, sizeof(stack
));
1651 setup_sigcontext(&uc
->tuc_mcontext
, env
, set
->sig
[0]);
1652 /* Save coprocessor signal frame. */
1653 regspace
= uc
->tuc_regspace
;
1654 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
1655 regspace
= setup_sigframe_v2_vfp(regspace
, env
);
1657 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
1658 regspace
= setup_sigframe_v2_iwmmxt(regspace
, env
);
1661 /* Write terminating magic word */
1662 __put_user(0, regspace
);
1664 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1665 __put_user(set
->sig
[i
], &uc
->tuc_sigmask
.sig
[i
]);
1669 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1670 static void setup_frame_v1(int usig
, struct target_sigaction
*ka
,
1671 target_sigset_t
*set
, CPUARMState
*regs
)
1673 struct sigframe_v1
*frame
;
1674 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1677 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1680 setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
1682 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1683 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
1687 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1688 frame_addr
+ offsetof(struct sigframe_v1
, retcode
));
1691 unlock_user_struct(frame
, frame_addr
, 1);
1694 static void setup_frame_v2(int usig
, struct target_sigaction
*ka
,
1695 target_sigset_t
*set
, CPUARMState
*regs
)
1697 struct sigframe_v2
*frame
;
1698 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1700 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1703 setup_sigframe_v2(&frame
->uc
, set
, regs
);
1705 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1706 frame_addr
+ offsetof(struct sigframe_v2
, retcode
));
1708 unlock_user_struct(frame
, frame_addr
, 1);
1711 static void setup_frame(int usig
, struct target_sigaction
*ka
,
1712 target_sigset_t
*set
, CPUARMState
*regs
)
1714 if (get_osversion() >= 0x020612) {
1715 setup_frame_v2(usig
, ka
, set
, regs
);
1717 setup_frame_v1(usig
, ka
, set
, regs
);
1721 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1722 static void setup_rt_frame_v1(int usig
, struct target_sigaction
*ka
,
1723 target_siginfo_t
*info
,
1724 target_sigset_t
*set
, CPUARMState
*env
)
1726 struct rt_sigframe_v1
*frame
;
1727 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1728 struct target_sigaltstack stack
;
1730 abi_ulong info_addr
, uc_addr
;
1732 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1735 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, info
);
1736 __put_user(info_addr
, &frame
->pinfo
);
1737 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
);
1738 __put_user(uc_addr
, &frame
->puc
);
1739 copy_siginfo_to_user(&frame
->info
, info
);
1741 /* Clear all the bits of the ucontext we don't use. */
1742 memset(&frame
->uc
, 0, offsetof(struct target_ucontext_v1
, tuc_mcontext
));
1744 memset(&stack
, 0, sizeof(stack
));
1745 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1746 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1747 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1748 memcpy(&frame
->uc
.tuc_stack
, &stack
, sizeof(stack
));
1750 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, set
->sig
[0]);
1751 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1752 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
1756 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1757 frame_addr
+ offsetof(struct rt_sigframe_v1
, retcode
));
1759 env
->regs
[1] = info_addr
;
1760 env
->regs
[2] = uc_addr
;
1763 unlock_user_struct(frame
, frame_addr
, 1);
1766 static void setup_rt_frame_v2(int usig
, struct target_sigaction
*ka
,
1767 target_siginfo_t
*info
,
1768 target_sigset_t
*set
, CPUARMState
*env
)
1770 struct rt_sigframe_v2
*frame
;
1771 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1772 abi_ulong info_addr
, uc_addr
;
1774 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1777 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, info
);
1778 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, uc
);
1779 copy_siginfo_to_user(&frame
->info
, info
);
1781 setup_sigframe_v2(&frame
->uc
, set
, env
);
1783 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1784 frame_addr
+ offsetof(struct rt_sigframe_v2
, retcode
));
1786 env
->regs
[1] = info_addr
;
1787 env
->regs
[2] = uc_addr
;
1789 unlock_user_struct(frame
, frame_addr
, 1);
1792 static void setup_rt_frame(int usig
, struct target_sigaction
*ka
,
1793 target_siginfo_t
*info
,
1794 target_sigset_t
*set
, CPUARMState
*env
)
1796 if (get_osversion() >= 0x020612) {
1797 setup_rt_frame_v2(usig
, ka
, info
, set
, env
);
1799 setup_rt_frame_v1(usig
, ka
, info
, set
, env
);
1804 restore_sigcontext(CPUARMState
*env
, struct target_sigcontext
*sc
)
1809 __get_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
1810 __get_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
1811 __get_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1812 __get_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1813 __get_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1814 __get_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1815 __get_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1816 __get_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1817 __get_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1818 __get_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1819 __get_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1820 __get_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1821 __get_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1822 __get_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1823 __get_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1824 __get_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1825 #ifdef TARGET_CONFIG_CPU_32
1826 __get_user_error(cpsr
, &sc
->arm_cpsr
, err
);
1827 cpsr_write(env
, cpsr
, CPSR_USER
| CPSR_EXEC
);
1830 err
|= !valid_user_regs(env
);
1835 static long do_sigreturn_v1(CPUARMState
*env
)
1837 abi_ulong frame_addr
;
1838 struct sigframe_v1
*frame
= NULL
;
1839 target_sigset_t set
;
1844 * Since we stacked the signal on a 64-bit boundary,
1845 * then 'sp' should be word aligned here. If it's
1846 * not, then the user is trying to mess with us.
1848 frame_addr
= env
->regs
[13];
1849 if (frame_addr
& 7) {
1853 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1856 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
1858 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1859 if (__get_user(set
.sig
[i
], &frame
->extramask
[i
- 1]))
1863 target_to_host_sigset_internal(&host_set
, &set
);
1864 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1866 if (restore_sigcontext(env
, &frame
->sc
))
1870 /* Send SIGTRAP if we're single-stepping */
1871 if (ptrace_cancel_bpt(current
))
1872 send_sig(SIGTRAP
, current
, 1);
1874 unlock_user_struct(frame
, frame_addr
, 0);
1875 return env
->regs
[0];
1878 unlock_user_struct(frame
, frame_addr
, 0);
1879 force_sig(TARGET_SIGSEGV
/* , current */);
1883 static abi_ulong
*restore_sigframe_v2_vfp(CPUARMState
*env
, abi_ulong
*regspace
)
1886 abi_ulong magic
, sz
;
1887 uint32_t fpscr
, fpexc
;
1888 struct target_vfp_sigframe
*vfpframe
;
1889 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
1891 __get_user(magic
, &vfpframe
->magic
);
1892 __get_user(sz
, &vfpframe
->size
);
1893 if (magic
!= TARGET_VFP_MAGIC
|| sz
!= sizeof(*vfpframe
)) {
1896 for (i
= 0; i
< 32; i
++) {
1897 __get_user(float64_val(env
->vfp
.regs
[i
]), &vfpframe
->ufp
.fpregs
[i
]);
1899 __get_user(fpscr
, &vfpframe
->ufp
.fpscr
);
1900 vfp_set_fpscr(env
, fpscr
);
1901 __get_user(fpexc
, &vfpframe
->ufp_exc
.fpexc
);
1902 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1903 * and the exception flag is cleared
1906 fpexc
&= ~((1 << 31) | (1 << 28));
1907 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = fpexc
;
1908 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
1909 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
1910 return (abi_ulong
*)(vfpframe
+ 1);
1913 static abi_ulong
*restore_sigframe_v2_iwmmxt(CPUARMState
*env
,
1914 abi_ulong
*regspace
)
1917 abi_ulong magic
, sz
;
1918 struct target_iwmmxt_sigframe
*iwmmxtframe
;
1919 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
1921 __get_user(magic
, &iwmmxtframe
->magic
);
1922 __get_user(sz
, &iwmmxtframe
->size
);
1923 if (magic
!= TARGET_IWMMXT_MAGIC
|| sz
!= sizeof(*iwmmxtframe
)) {
1926 for (i
= 0; i
< 16; i
++) {
1927 __get_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
1929 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
1930 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
1931 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
1932 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
1933 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
1934 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
1935 return (abi_ulong
*)(iwmmxtframe
+ 1);
1938 static int do_sigframe_return_v2(CPUARMState
*env
, target_ulong frame_addr
,
1939 struct target_ucontext_v2
*uc
)
1942 abi_ulong
*regspace
;
1944 target_to_host_sigset(&host_set
, &uc
->tuc_sigmask
);
1945 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1947 if (restore_sigcontext(env
, &uc
->tuc_mcontext
))
1950 /* Restore coprocessor signal frame */
1951 regspace
= uc
->tuc_regspace
;
1952 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
1953 regspace
= restore_sigframe_v2_vfp(env
, regspace
);
1958 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
1959 regspace
= restore_sigframe_v2_iwmmxt(env
, regspace
);
1965 if (do_sigaltstack(frame_addr
+ offsetof(struct target_ucontext_v2
, tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
1969 /* Send SIGTRAP if we're single-stepping */
1970 if (ptrace_cancel_bpt(current
))
1971 send_sig(SIGTRAP
, current
, 1);
1977 static long do_sigreturn_v2(CPUARMState
*env
)
1979 abi_ulong frame_addr
;
1980 struct sigframe_v2
*frame
= NULL
;
1983 * Since we stacked the signal on a 64-bit boundary,
1984 * then 'sp' should be word aligned here. If it's
1985 * not, then the user is trying to mess with us.
1987 frame_addr
= env
->regs
[13];
1988 if (frame_addr
& 7) {
1992 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1995 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
))
1998 unlock_user_struct(frame
, frame_addr
, 0);
1999 return env
->regs
[0];
2002 unlock_user_struct(frame
, frame_addr
, 0);
2003 force_sig(TARGET_SIGSEGV
/* , current */);
2007 long do_sigreturn(CPUARMState
*env
)
2009 if (get_osversion() >= 0x020612) {
2010 return do_sigreturn_v2(env
);
2012 return do_sigreturn_v1(env
);
2016 static long do_rt_sigreturn_v1(CPUARMState
*env
)
2018 abi_ulong frame_addr
;
2019 struct rt_sigframe_v1
*frame
= NULL
;
2023 * Since we stacked the signal on a 64-bit boundary,
2024 * then 'sp' should be word aligned here. If it's
2025 * not, then the user is trying to mess with us.
2027 frame_addr
= env
->regs
[13];
2028 if (frame_addr
& 7) {
2032 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2035 target_to_host_sigset(&host_set
, &frame
->uc
.tuc_sigmask
);
2036 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
2038 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
))
2041 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
.tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
2045 /* Send SIGTRAP if we're single-stepping */
2046 if (ptrace_cancel_bpt(current
))
2047 send_sig(SIGTRAP
, current
, 1);
2049 unlock_user_struct(frame
, frame_addr
, 0);
2050 return env
->regs
[0];
2053 unlock_user_struct(frame
, frame_addr
, 0);
2054 force_sig(TARGET_SIGSEGV
/* , current */);
2058 static long do_rt_sigreturn_v2(CPUARMState
*env
)
2060 abi_ulong frame_addr
;
2061 struct rt_sigframe_v2
*frame
= NULL
;
2064 * Since we stacked the signal on a 64-bit boundary,
2065 * then 'sp' should be word aligned here. If it's
2066 * not, then the user is trying to mess with us.
2068 frame_addr
= env
->regs
[13];
2069 if (frame_addr
& 7) {
2073 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2076 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
))
2079 unlock_user_struct(frame
, frame_addr
, 0);
2080 return env
->regs
[0];
2083 unlock_user_struct(frame
, frame_addr
, 0);
2084 force_sig(TARGET_SIGSEGV
/* , current */);
2088 long do_rt_sigreturn(CPUARMState
*env
)
2090 if (get_osversion() >= 0x020612) {
2091 return do_rt_sigreturn_v2(env
);
2093 return do_rt_sigreturn_v1(env
);
2097 #elif defined(TARGET_SPARC)
2099 #define __SUNOS_MAXWIN 31
2101 /* This is what SunOS does, so shall I. */
2102 struct target_sigcontext
{
2103 abi_ulong sigc_onstack
; /* state to restore */
2105 abi_ulong sigc_mask
; /* sigmask to restore */
2106 abi_ulong sigc_sp
; /* stack pointer */
2107 abi_ulong sigc_pc
; /* program counter */
2108 abi_ulong sigc_npc
; /* next program counter */
2109 abi_ulong sigc_psr
; /* for condition codes etc */
2110 abi_ulong sigc_g1
; /* User uses these two registers */
2111 abi_ulong sigc_o0
; /* within the trampoline code. */
2113 /* Now comes information regarding the users window set
2114 * at the time of the signal.
2116 abi_ulong sigc_oswins
; /* outstanding windows */
2118 /* stack ptrs for each regwin buf */
2119 char *sigc_spbuf
[__SUNOS_MAXWIN
];
2121 /* Windows to restore after signal */
2123 abi_ulong locals
[8];
2125 } sigc_wbuf
[__SUNOS_MAXWIN
];
2127 /* A Sparc stack frame */
2128 struct sparc_stackf
{
2129 abi_ulong locals
[8];
2131 /* It's simpler to treat fp and callers_pc as elements of ins[]
2132 * since we never need to access them ourselves.
2136 abi_ulong xxargs
[1];
2145 abi_ulong u_regs
[16]; /* globals and ins */
2151 abi_ulong si_float_regs
[32];
2152 unsigned long si_fsr
;
2153 unsigned long si_fpqdepth
;
2155 unsigned long *insn_addr
;
2158 } qemu_siginfo_fpu_t
;
2161 struct target_signal_frame
{
2162 struct sparc_stackf ss
;
2165 abi_ulong insns
[2] __attribute__ ((aligned (8)));
2166 abi_ulong extramask
[TARGET_NSIG_WORDS
- 1];
2167 abi_ulong extra_size
; /* Should be 0 */
2168 qemu_siginfo_fpu_t fpu_state
;
2170 struct target_rt_signal_frame
{
2171 struct sparc_stackf ss
;
2176 unsigned int insns
[2];
2178 unsigned int extra_size
; /* Should be 0 */
2179 qemu_siginfo_fpu_t fpu_state
;
2193 #define UREG_FP UREG_I6
2194 #define UREG_SP UREG_O6
2196 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
2198 unsigned long framesize
)
2202 sp
= env
->regwptr
[UREG_FP
];
2204 /* This is the X/Open sanctioned signal stack switching. */
2205 if (sa
->sa_flags
& TARGET_SA_ONSTACK
) {
2206 if (!on_sig_stack(sp
)
2207 && !((target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
) & 7))
2208 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2210 return sp
- framesize
;
2214 setup___siginfo(__siginfo_t
*si
, CPUSPARCState
*env
, abi_ulong mask
)
2218 err
|= __put_user(env
->psr
, &si
->si_regs
.psr
);
2219 err
|= __put_user(env
->pc
, &si
->si_regs
.pc
);
2220 err
|= __put_user(env
->npc
, &si
->si_regs
.npc
);
2221 err
|= __put_user(env
->y
, &si
->si_regs
.y
);
2222 for (i
=0; i
< 8; i
++) {
2223 err
|= __put_user(env
->gregs
[i
], &si
->si_regs
.u_regs
[i
]);
2225 for (i
=0; i
< 8; i
++) {
2226 err
|= __put_user(env
->regwptr
[UREG_I0
+ i
], &si
->si_regs
.u_regs
[i
+8]);
2228 err
|= __put_user(mask
, &si
->si_mask
);
2234 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
2235 CPUSPARCState
*env
, unsigned long mask
)
2239 err
|= __put_user(mask
, &sc
->sigc_mask
);
2240 err
|= __put_user(env
->regwptr
[UREG_SP
], &sc
->sigc_sp
);
2241 err
|= __put_user(env
->pc
, &sc
->sigc_pc
);
2242 err
|= __put_user(env
->npc
, &sc
->sigc_npc
);
2243 err
|= __put_user(env
->psr
, &sc
->sigc_psr
);
2244 err
|= __put_user(env
->gregs
[1], &sc
->sigc_g1
);
2245 err
|= __put_user(env
->regwptr
[UREG_O0
], &sc
->sigc_o0
);
2250 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2252 static void setup_frame(int sig
, struct target_sigaction
*ka
,
2253 target_sigset_t
*set
, CPUSPARCState
*env
)
2256 struct target_signal_frame
*sf
;
2257 int sigframe_size
, err
, i
;
2259 /* 1. Make sure everything is clean */
2260 //synchronize_user_stack();
2262 sigframe_size
= NF_ALIGNEDSZ
;
2263 sf_addr
= get_sigframe(ka
, env
, sigframe_size
);
2265 sf
= lock_user(VERIFY_WRITE
, sf_addr
,
2266 sizeof(struct target_signal_frame
), 0);
2270 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2272 if (invalid_frame_pointer(sf
, sigframe_size
))
2273 goto sigill_and_return
;
2275 /* 2. Save the current process state */
2276 err
= setup___siginfo(&sf
->info
, env
, set
->sig
[0]);
2277 err
|= __put_user(0, &sf
->extra_size
);
2279 //err |= save_fpu_state(regs, &sf->fpu_state);
2280 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
2282 err
|= __put_user(set
->sig
[0], &sf
->info
.si_mask
);
2283 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
2284 err
|= __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
2287 for (i
= 0; i
< 8; i
++) {
2288 err
|= __put_user(env
->regwptr
[i
+ UREG_L0
], &sf
->ss
.locals
[i
]);
2290 for (i
= 0; i
< 8; i
++) {
2291 err
|= __put_user(env
->regwptr
[i
+ UREG_I0
], &sf
->ss
.ins
[i
]);
2296 /* 3. signal handler back-trampoline and parameters */
2297 env
->regwptr
[UREG_FP
] = sf_addr
;
2298 env
->regwptr
[UREG_I0
] = sig
;
2299 env
->regwptr
[UREG_I1
] = sf_addr
+
2300 offsetof(struct target_signal_frame
, info
);
2301 env
->regwptr
[UREG_I2
] = sf_addr
+
2302 offsetof(struct target_signal_frame
, info
);
2304 /* 4. signal handler */
2305 env
->pc
= ka
->_sa_handler
;
2306 env
->npc
= (env
->pc
+ 4);
2307 /* 5. return to kernel instructions */
2308 if (ka
->sa_restorer
)
2309 env
->regwptr
[UREG_I7
] = ka
->sa_restorer
;
2313 env
->regwptr
[UREG_I7
] = sf_addr
+
2314 offsetof(struct target_signal_frame
, insns
) - 2 * 4;
2316 /* mov __NR_sigreturn, %g1 */
2318 err
|= __put_user(val32
, &sf
->insns
[0]);
2322 err
|= __put_user(val32
, &sf
->insns
[1]);
2326 /* Flush instruction space. */
2327 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2330 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
2334 force_sig(TARGET_SIGILL
);
2337 //fprintf(stderr, "force_sig\n");
2338 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
2339 force_sig(TARGET_SIGSEGV
);
2342 restore_fpu_state(CPUSPARCState
*env
, qemu_siginfo_fpu_t
*fpu
)
2347 if (current
->flags
& PF_USEDFPU
)
2348 regs
->psr
&= ~PSR_EF
;
2350 if (current
== last_task_used_math
) {
2351 last_task_used_math
= 0;
2352 regs
->psr
&= ~PSR_EF
;
2355 current
->used_math
= 1;
2356 current
->flags
&= ~PF_USEDFPU
;
2359 if (verify_area (VERIFY_READ
, fpu
, sizeof(*fpu
)))
2363 /* XXX: incorrect */
2364 err
= copy_from_user(&env
->fpr
[0], fpu
->si_float_regs
[0],
2365 (sizeof(abi_ulong
) * 32));
2366 err
|= __get_user(env
->fsr
, &fpu
->si_fsr
);
2368 err
|= __get_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
2369 if (current
->thread
.fpqdepth
!= 0)
2370 err
|= __copy_from_user(¤t
->thread
.fpqueue
[0],
2371 &fpu
->si_fpqueue
[0],
2372 ((sizeof(unsigned long) +
2373 (sizeof(unsigned long *)))*16));
2379 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
2380 target_siginfo_t
*info
,
2381 target_sigset_t
*set
, CPUSPARCState
*env
)
2383 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2386 long do_sigreturn(CPUSPARCState
*env
)
2389 struct target_signal_frame
*sf
;
2390 uint32_t up_psr
, pc
, npc
;
2391 target_sigset_t set
;
2395 sf_addr
= env
->regwptr
[UREG_FP
];
2396 if (!lock_user_struct(VERIFY_READ
, sf
, sf_addr
, 1))
2399 fprintf(stderr
, "sigreturn\n");
2400 fprintf(stderr
, "sf: %x pc %x fp %x sp %x\n", sf
, env
->pc
, env
->regwptr
[UREG_FP
], env
->regwptr
[UREG_SP
]);
2402 //cpu_dump_state(env, stderr, fprintf, 0);
2404 /* 1. Make sure we are not getting garbage from the user */
2409 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
2410 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
2415 /* 2. Restore the state */
2416 err
|= __get_user(up_psr
, &sf
->info
.si_regs
.psr
);
2418 /* User can only change condition codes and FPU enabling in %psr. */
2419 env
->psr
= (up_psr
& (PSR_ICC
/* | PSR_EF */))
2420 | (env
->psr
& ~(PSR_ICC
/* | PSR_EF */));
2424 err
|= __get_user(env
->y
, &sf
->info
.si_regs
.y
);
2425 for (i
=0; i
< 8; i
++) {
2426 err
|= __get_user(env
->gregs
[i
], &sf
->info
.si_regs
.u_regs
[i
]);
2428 for (i
=0; i
< 8; i
++) {
2429 err
|= __get_user(env
->regwptr
[i
+ UREG_I0
], &sf
->info
.si_regs
.u_regs
[i
+8]);
2432 /* FIXME: implement FPU save/restore:
2433 * __get_user(fpu_save, &sf->fpu_save);
2435 * err |= restore_fpu_state(env, fpu_save);
2438 /* This is pretty much atomic, no amount locking would prevent
2439 * the races which exist anyways.
2441 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
2442 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
2443 err
|= (__get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]));
2446 target_to_host_sigset_internal(&host_set
, &set
);
2447 sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
2451 unlock_user_struct(sf
, sf_addr
, 0);
2452 return env
->regwptr
[0];
2455 unlock_user_struct(sf
, sf_addr
, 0);
2456 force_sig(TARGET_SIGSEGV
);
2459 long do_rt_sigreturn(CPUSPARCState
*env
)
2461 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2462 return -TARGET_ENOSYS
;
2465 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2487 typedef abi_ulong target_mc_greg_t
;
2488 typedef target_mc_greg_t target_mc_gregset_t
[MC_NGREG
];
2490 struct target_mc_fq
{
2491 abi_ulong
*mcfq_addr
;
2495 struct target_mc_fpu
{
2499 //uint128_t qregs[16];
2501 abi_ulong mcfpu_fsr
;
2502 abi_ulong mcfpu_fprs
;
2503 abi_ulong mcfpu_gsr
;
2504 struct target_mc_fq
*mcfpu_fq
;
2505 unsigned char mcfpu_qcnt
;
2506 unsigned char mcfpu_qentsz
;
2507 unsigned char mcfpu_enab
;
2509 typedef struct target_mc_fpu target_mc_fpu_t
;
2512 target_mc_gregset_t mc_gregs
;
2513 target_mc_greg_t mc_fp
;
2514 target_mc_greg_t mc_i7
;
2515 target_mc_fpu_t mc_fpregs
;
2516 } target_mcontext_t
;
2518 struct target_ucontext
{
2519 struct target_ucontext
*tuc_link
;
2520 abi_ulong tuc_flags
;
2521 target_sigset_t tuc_sigmask
;
2522 target_mcontext_t tuc_mcontext
;
2525 /* A V9 register window */
2526 struct target_reg_window
{
2527 abi_ulong locals
[8];
2531 #define TARGET_STACK_BIAS 2047
2533 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2534 void sparc64_set_context(CPUSPARCState
*env
)
2537 struct target_ucontext
*ucp
;
2538 target_mc_gregset_t
*grp
;
2539 abi_ulong pc
, npc
, tstate
;
2540 abi_ulong fp
, i7
, w_addr
;
2544 ucp_addr
= env
->regwptr
[UREG_I0
];
2545 if (!lock_user_struct(VERIFY_READ
, ucp
, ucp_addr
, 1))
2547 grp
= &ucp
->tuc_mcontext
.mc_gregs
;
2548 err
= __get_user(pc
, &((*grp
)[MC_PC
]));
2549 err
|= __get_user(npc
, &((*grp
)[MC_NPC
]));
2550 if (err
|| ((pc
| npc
) & 3))
2552 if (env
->regwptr
[UREG_I1
]) {
2553 target_sigset_t target_set
;
2556 if (TARGET_NSIG_WORDS
== 1) {
2557 if (__get_user(target_set
.sig
[0], &ucp
->tuc_sigmask
.sig
[0]))
2560 abi_ulong
*src
, *dst
;
2561 src
= ucp
->tuc_sigmask
.sig
;
2562 dst
= target_set
.sig
;
2563 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
2564 err
|= __get_user(*dst
, src
);
2569 target_to_host_sigset_internal(&set
, &target_set
);
2570 sigprocmask(SIG_SETMASK
, &set
, NULL
);
2574 err
|= __get_user(env
->y
, &((*grp
)[MC_Y
]));
2575 err
|= __get_user(tstate
, &((*grp
)[MC_TSTATE
]));
2576 env
->asi
= (tstate
>> 24) & 0xff;
2577 cpu_put_ccr(env
, tstate
>> 32);
2578 cpu_put_cwp64(env
, tstate
& 0x1f);
2579 err
|= __get_user(env
->gregs
[1], (&(*grp
)[MC_G1
]));
2580 err
|= __get_user(env
->gregs
[2], (&(*grp
)[MC_G2
]));
2581 err
|= __get_user(env
->gregs
[3], (&(*grp
)[MC_G3
]));
2582 err
|= __get_user(env
->gregs
[4], (&(*grp
)[MC_G4
]));
2583 err
|= __get_user(env
->gregs
[5], (&(*grp
)[MC_G5
]));
2584 err
|= __get_user(env
->gregs
[6], (&(*grp
)[MC_G6
]));
2585 err
|= __get_user(env
->gregs
[7], (&(*grp
)[MC_G7
]));
2586 err
|= __get_user(env
->regwptr
[UREG_I0
], (&(*grp
)[MC_O0
]));
2587 err
|= __get_user(env
->regwptr
[UREG_I1
], (&(*grp
)[MC_O1
]));
2588 err
|= __get_user(env
->regwptr
[UREG_I2
], (&(*grp
)[MC_O2
]));
2589 err
|= __get_user(env
->regwptr
[UREG_I3
], (&(*grp
)[MC_O3
]));
2590 err
|= __get_user(env
->regwptr
[UREG_I4
], (&(*grp
)[MC_O4
]));
2591 err
|= __get_user(env
->regwptr
[UREG_I5
], (&(*grp
)[MC_O5
]));
2592 err
|= __get_user(env
->regwptr
[UREG_I6
], (&(*grp
)[MC_O6
]));
2593 err
|= __get_user(env
->regwptr
[UREG_I7
], (&(*grp
)[MC_O7
]));
2595 err
|= __get_user(fp
, &(ucp
->tuc_mcontext
.mc_fp
));
2596 err
|= __get_user(i7
, &(ucp
->tuc_mcontext
.mc_i7
));
2598 w_addr
= TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
];
2599 if (put_user(fp
, w_addr
+ offsetof(struct target_reg_window
, ins
[6]),
2602 if (put_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
2605 /* FIXME this does not match how the kernel handles the FPU in
2606 * its sparc64_set_context implementation. In particular the FPU
2607 * is only restored if fenab is non-zero in:
2608 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2610 err
|= __get_user(env
->fprs
, &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fprs
));
2612 uint32_t *src
= ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2613 for (i
= 0; i
< 64; i
++, src
++) {
2615 err
|= __get_user(env
->fpr
[i
/2].l
.lower
, src
);
2617 err
|= __get_user(env
->fpr
[i
/2].l
.upper
, src
);
2621 err
|= __get_user(env
->fsr
,
2622 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fsr
));
2623 err
|= __get_user(env
->gsr
,
2624 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_gsr
));
2627 unlock_user_struct(ucp
, ucp_addr
, 0);
2630 unlock_user_struct(ucp
, ucp_addr
, 0);
2631 force_sig(TARGET_SIGSEGV
);
2634 void sparc64_get_context(CPUSPARCState
*env
)
2637 struct target_ucontext
*ucp
;
2638 target_mc_gregset_t
*grp
;
2639 target_mcontext_t
*mcp
;
2640 abi_ulong fp
, i7
, w_addr
;
2643 target_sigset_t target_set
;
2646 ucp_addr
= env
->regwptr
[UREG_I0
];
2647 if (!lock_user_struct(VERIFY_WRITE
, ucp
, ucp_addr
, 0))
2650 mcp
= &ucp
->tuc_mcontext
;
2651 grp
= &mcp
->mc_gregs
;
2653 /* Skip over the trap instruction, first. */
2659 sigprocmask(0, NULL
, &set
);
2660 host_to_target_sigset_internal(&target_set
, &set
);
2661 if (TARGET_NSIG_WORDS
== 1) {
2662 err
|= __put_user(target_set
.sig
[0],
2663 (abi_ulong
*)&ucp
->tuc_sigmask
);
2665 abi_ulong
*src
, *dst
;
2666 src
= target_set
.sig
;
2667 dst
= ucp
->tuc_sigmask
.sig
;
2668 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
2669 err
|= __put_user(*src
, dst
);
2675 /* XXX: tstate must be saved properly */
2676 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2677 err
|= __put_user(env
->pc
, &((*grp
)[MC_PC
]));
2678 err
|= __put_user(env
->npc
, &((*grp
)[MC_NPC
]));
2679 err
|= __put_user(env
->y
, &((*grp
)[MC_Y
]));
2680 err
|= __put_user(env
->gregs
[1], &((*grp
)[MC_G1
]));
2681 err
|= __put_user(env
->gregs
[2], &((*grp
)[MC_G2
]));
2682 err
|= __put_user(env
->gregs
[3], &((*grp
)[MC_G3
]));
2683 err
|= __put_user(env
->gregs
[4], &((*grp
)[MC_G4
]));
2684 err
|= __put_user(env
->gregs
[5], &((*grp
)[MC_G5
]));
2685 err
|= __put_user(env
->gregs
[6], &((*grp
)[MC_G6
]));
2686 err
|= __put_user(env
->gregs
[7], &((*grp
)[MC_G7
]));
2687 err
|= __put_user(env
->regwptr
[UREG_I0
], &((*grp
)[MC_O0
]));
2688 err
|= __put_user(env
->regwptr
[UREG_I1
], &((*grp
)[MC_O1
]));
2689 err
|= __put_user(env
->regwptr
[UREG_I2
], &((*grp
)[MC_O2
]));
2690 err
|= __put_user(env
->regwptr
[UREG_I3
], &((*grp
)[MC_O3
]));
2691 err
|= __put_user(env
->regwptr
[UREG_I4
], &((*grp
)[MC_O4
]));
2692 err
|= __put_user(env
->regwptr
[UREG_I5
], &((*grp
)[MC_O5
]));
2693 err
|= __put_user(env
->regwptr
[UREG_I6
], &((*grp
)[MC_O6
]));
2694 err
|= __put_user(env
->regwptr
[UREG_I7
], &((*grp
)[MC_O7
]));
2696 w_addr
= TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
];
2698 if (get_user(fp
, w_addr
+ offsetof(struct target_reg_window
, ins
[6]),
2701 if (get_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
2704 err
|= __put_user(fp
, &(mcp
->mc_fp
));
2705 err
|= __put_user(i7
, &(mcp
->mc_i7
));
2708 uint32_t *dst
= ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2709 for (i
= 0; i
< 64; i
++, dst
++) {
2711 err
|= __put_user(env
->fpr
[i
/2].l
.lower
, dst
);
2713 err
|= __put_user(env
->fpr
[i
/2].l
.upper
, dst
);
2717 err
|= __put_user(env
->fsr
, &(mcp
->mc_fpregs
.mcfpu_fsr
));
2718 err
|= __put_user(env
->gsr
, &(mcp
->mc_fpregs
.mcfpu_gsr
));
2719 err
|= __put_user(env
->fprs
, &(mcp
->mc_fpregs
.mcfpu_fprs
));
2723 unlock_user_struct(ucp
, ucp_addr
, 1);
2726 unlock_user_struct(ucp
, ucp_addr
, 1);
2727 force_sig(TARGET_SIGSEGV
);
2730 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2732 # if defined(TARGET_ABI_MIPSO32)
2733 struct target_sigcontext
{
2734 uint32_t sc_regmask
; /* Unused */
2737 uint64_t sc_regs
[32];
2738 uint64_t sc_fpregs
[32];
2739 uint32_t sc_ownedfp
; /* Unused */
2740 uint32_t sc_fpc_csr
;
2741 uint32_t sc_fpc_eir
; /* Unused */
2742 uint32_t sc_used_math
;
2743 uint32_t sc_dsp
; /* dsp status, was sc_ssflags */
2747 target_ulong sc_hi1
; /* Was sc_cause */
2748 target_ulong sc_lo1
; /* Was sc_badvaddr */
2749 target_ulong sc_hi2
; /* Was sc_sigset[4] */
2750 target_ulong sc_lo2
;
2751 target_ulong sc_hi3
;
2752 target_ulong sc_lo3
;
2754 # else /* N32 || N64 */
2755 struct target_sigcontext
{
2756 uint64_t sc_regs
[32];
2757 uint64_t sc_fpregs
[32];
2767 uint32_t sc_fpc_csr
;
2768 uint32_t sc_used_math
;
2770 uint32_t sc_reserved
;
2775 uint32_t sf_ass
[4]; /* argument save space for o32 */
2776 uint32_t sf_code
[2]; /* signal trampoline */
2777 struct target_sigcontext sf_sc
;
2778 target_sigset_t sf_mask
;
2781 struct target_ucontext
{
2782 target_ulong tuc_flags
;
2783 target_ulong tuc_link
;
2784 target_stack_t tuc_stack
;
2786 struct target_sigcontext tuc_mcontext
;
2787 target_sigset_t tuc_sigmask
;
2790 struct target_rt_sigframe
{
2791 uint32_t rs_ass
[4]; /* argument save space for o32 */
2792 uint32_t rs_code
[2]; /* signal trampoline */
2793 struct target_siginfo rs_info
;
2794 struct target_ucontext rs_uc
;
2797 /* Install trampoline to jump back from signal handler */
2798 static inline int install_sigtramp(unsigned int *tramp
, unsigned int syscall
)
2803 * Set up the return code ...
2805 * li v0, __NR__foo_sigreturn
2809 err
|= __put_user(0x24020000 + syscall
, tramp
+ 0);
2810 err
|= __put_user(0x0000000c , tramp
+ 1);
2815 setup_sigcontext(CPUMIPSState
*regs
, struct target_sigcontext
*sc
)
2820 err
|= __put_user(exception_resume_pc(regs
), &sc
->sc_pc
);
2821 regs
->hflags
&= ~MIPS_HFLAG_BMASK
;
2823 __put_user(0, &sc
->sc_regs
[0]);
2824 for (i
= 1; i
< 32; ++i
) {
2825 err
|= __put_user(regs
->active_tc
.gpr
[i
], &sc
->sc_regs
[i
]);
2828 err
|= __put_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2829 err
|= __put_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2831 /* Rather than checking for dsp existence, always copy. The storage
2832 would just be garbage otherwise. */
2833 err
|= __put_user(regs
->active_tc
.HI
[1], &sc
->sc_hi1
);
2834 err
|= __put_user(regs
->active_tc
.HI
[2], &sc
->sc_hi2
);
2835 err
|= __put_user(regs
->active_tc
.HI
[3], &sc
->sc_hi3
);
2836 err
|= __put_user(regs
->active_tc
.LO
[1], &sc
->sc_lo1
);
2837 err
|= __put_user(regs
->active_tc
.LO
[2], &sc
->sc_lo2
);
2838 err
|= __put_user(regs
->active_tc
.LO
[3], &sc
->sc_lo3
);
2840 uint32_t dsp
= cpu_rddsp(0x3ff, regs
);
2841 err
|= __put_user(dsp
, &sc
->sc_dsp
);
2844 err
|= __put_user(1, &sc
->sc_used_math
);
2846 for (i
= 0; i
< 32; ++i
) {
2847 err
|= __put_user(regs
->active_fpu
.fpr
[i
].d
, &sc
->sc_fpregs
[i
]);
2854 restore_sigcontext(CPUMIPSState
*regs
, struct target_sigcontext
*sc
)
2859 err
|= __get_user(regs
->CP0_EPC
, &sc
->sc_pc
);
2861 err
|= __get_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2862 err
|= __get_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2864 for (i
= 1; i
< 32; ++i
) {
2865 err
|= __get_user(regs
->active_tc
.gpr
[i
], &sc
->sc_regs
[i
]);
2868 err
|= __get_user(regs
->active_tc
.HI
[1], &sc
->sc_hi1
);
2869 err
|= __get_user(regs
->active_tc
.HI
[2], &sc
->sc_hi2
);
2870 err
|= __get_user(regs
->active_tc
.HI
[3], &sc
->sc_hi3
);
2871 err
|= __get_user(regs
->active_tc
.LO
[1], &sc
->sc_lo1
);
2872 err
|= __get_user(regs
->active_tc
.LO
[2], &sc
->sc_lo2
);
2873 err
|= __get_user(regs
->active_tc
.LO
[3], &sc
->sc_lo3
);
2876 err
|= __get_user(dsp
, &sc
->sc_dsp
);
2877 cpu_wrdsp(dsp
, 0x3ff, regs
);
2880 for (i
= 0; i
< 32; ++i
) {
2881 err
|= __get_user(regs
->active_fpu
.fpr
[i
].d
, &sc
->sc_fpregs
[i
]);
2888 * Determine which stack to use..
2890 static inline abi_ulong
2891 get_sigframe(struct target_sigaction
*ka
, CPUMIPSState
*regs
, size_t frame_size
)
2895 /* Default to using normal stack */
2896 sp
= regs
->active_tc
.gpr
[29];
2899 * FPU emulator may have its own trampoline active just
2900 * above the user stack, 16-bytes before the next lowest
2901 * 16 byte boundary. Try to avoid trashing it.
2905 /* This is the X/Open sanctioned signal stack switching. */
2906 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
2907 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2910 return (sp
- frame_size
) & ~7;
2913 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState
*env
)
2915 if (env
->insn_flags
& (ASE_MIPS16
| ASE_MICROMIPS
)) {
2916 env
->hflags
&= ~MIPS_HFLAG_M16
;
2917 env
->hflags
|= (env
->active_tc
.PC
& 1) << MIPS_HFLAG_M16_SHIFT
;
2918 env
->active_tc
.PC
&= ~(target_ulong
) 1;
2922 # if defined(TARGET_ABI_MIPSO32)
2923 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2924 static void setup_frame(int sig
, struct target_sigaction
* ka
,
2925 target_sigset_t
*set
, CPUMIPSState
*regs
)
2927 struct sigframe
*frame
;
2928 abi_ulong frame_addr
;
2931 frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
2932 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
2935 install_sigtramp(frame
->sf_code
, TARGET_NR_sigreturn
);
2937 if(setup_sigcontext(regs
, &frame
->sf_sc
))
2940 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2941 if(__put_user(set
->sig
[i
], &frame
->sf_mask
.sig
[i
]))
2946 * Arguments to signal handler:
2948 * a0 = signal number
2949 * a1 = 0 (should be cause)
2950 * a2 = pointer to struct sigcontext
2952 * $25 and PC point to the signal handler, $29 points to the
2955 regs
->active_tc
.gpr
[ 4] = sig
;
2956 regs
->active_tc
.gpr
[ 5] = 0;
2957 regs
->active_tc
.gpr
[ 6] = frame_addr
+ offsetof(struct sigframe
, sf_sc
);
2958 regs
->active_tc
.gpr
[29] = frame_addr
;
2959 regs
->active_tc
.gpr
[31] = frame_addr
+ offsetof(struct sigframe
, sf_code
);
2960 /* The original kernel code sets CP0_EPC to the handler
2961 * since it returns to userland using eret
2962 * we cannot do this here, and we must set PC directly */
2963 regs
->active_tc
.PC
= regs
->active_tc
.gpr
[25] = ka
->_sa_handler
;
2964 mips_set_hflags_isa_mode_from_pc(regs
);
2965 unlock_user_struct(frame
, frame_addr
, 1);
2969 unlock_user_struct(frame
, frame_addr
, 1);
2970 force_sig(TARGET_SIGSEGV
/*, current*/);
2973 long do_sigreturn(CPUMIPSState
*regs
)
2975 struct sigframe
*frame
;
2976 abi_ulong frame_addr
;
2978 target_sigset_t target_set
;
2981 #if defined(DEBUG_SIGNAL)
2982 fprintf(stderr
, "do_sigreturn\n");
2984 frame_addr
= regs
->active_tc
.gpr
[29];
2985 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2988 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2989 if(__get_user(target_set
.sig
[i
], &frame
->sf_mask
.sig
[i
]))
2993 target_to_host_sigset_internal(&blocked
, &target_set
);
2994 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
2996 if (restore_sigcontext(regs
, &frame
->sf_sc
))
3001 * Don't let your children do this ...
3003 __asm__
__volatile__(
3011 regs
->active_tc
.PC
= regs
->CP0_EPC
;
3012 mips_set_hflags_isa_mode_from_pc(regs
);
3013 /* I am not sure this is right, but it seems to work
3014 * maybe a problem with nested signals ? */
3016 return -TARGET_QEMU_ESIGRETURN
;
3019 force_sig(TARGET_SIGSEGV
/*, current*/);
3024 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3025 target_siginfo_t
*info
,
3026 target_sigset_t
*set
, CPUMIPSState
*env
)
3028 struct target_rt_sigframe
*frame
;
3029 abi_ulong frame_addr
;
3032 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
3033 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3036 install_sigtramp(frame
->rs_code
, TARGET_NR_rt_sigreturn
);
3038 copy_siginfo_to_user(&frame
->rs_info
, info
);
3040 __put_user(0, &frame
->rs_uc
.tuc_flags
);
3041 __put_user(0, &frame
->rs_uc
.tuc_link
);
3042 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->rs_uc
.tuc_stack
.ss_sp
);
3043 __put_user(target_sigaltstack_used
.ss_size
, &frame
->rs_uc
.tuc_stack
.ss_size
);
3044 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
3045 &frame
->rs_uc
.tuc_stack
.ss_flags
);
3047 setup_sigcontext(env
, &frame
->rs_uc
.tuc_mcontext
);
3049 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3050 __put_user(set
->sig
[i
], &frame
->rs_uc
.tuc_sigmask
.sig
[i
]);
3054 * Arguments to signal handler:
3056 * a0 = signal number
3057 * a1 = pointer to siginfo_t
3058 * a2 = pointer to struct ucontext
3060 * $25 and PC point to the signal handler, $29 points to the
3063 env
->active_tc
.gpr
[ 4] = sig
;
3064 env
->active_tc
.gpr
[ 5] = frame_addr
3065 + offsetof(struct target_rt_sigframe
, rs_info
);
3066 env
->active_tc
.gpr
[ 6] = frame_addr
3067 + offsetof(struct target_rt_sigframe
, rs_uc
);
3068 env
->active_tc
.gpr
[29] = frame_addr
;
3069 env
->active_tc
.gpr
[31] = frame_addr
3070 + offsetof(struct target_rt_sigframe
, rs_code
);
3071 /* The original kernel code sets CP0_EPC to the handler
3072 * since it returns to userland using eret
3073 * we cannot do this here, and we must set PC directly */
3074 env
->active_tc
.PC
= env
->active_tc
.gpr
[25] = ka
->_sa_handler
;
3075 mips_set_hflags_isa_mode_from_pc(env
);
3076 unlock_user_struct(frame
, frame_addr
, 1);
3080 unlock_user_struct(frame
, frame_addr
, 1);
3081 force_sig(TARGET_SIGSEGV
/*, current*/);
3084 long do_rt_sigreturn(CPUMIPSState
*env
)
3086 struct target_rt_sigframe
*frame
;
3087 abi_ulong frame_addr
;
3090 #if defined(DEBUG_SIGNAL)
3091 fprintf(stderr
, "do_rt_sigreturn\n");
3093 frame_addr
= env
->active_tc
.gpr
[29];
3094 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
3097 target_to_host_sigset(&blocked
, &frame
->rs_uc
.tuc_sigmask
);
3098 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3100 if (restore_sigcontext(env
, &frame
->rs_uc
.tuc_mcontext
))
3103 if (do_sigaltstack(frame_addr
+
3104 offsetof(struct target_rt_sigframe
, rs_uc
.tuc_stack
),
3105 0, get_sp_from_cpustate(env
)) == -EFAULT
)
3108 env
->active_tc
.PC
= env
->CP0_EPC
;
3109 mips_set_hflags_isa_mode_from_pc(env
);
3110 /* I am not sure this is right, but it seems to work
3111 * maybe a problem with nested signals ? */
3113 return -TARGET_QEMU_ESIGRETURN
;
3116 force_sig(TARGET_SIGSEGV
/*, current*/);
3120 #elif defined(TARGET_SH4)
3123 * code and data structures from linux kernel:
3124 * include/asm-sh/sigcontext.h
3125 * arch/sh/kernel/signal.c
3128 struct target_sigcontext
{
3129 target_ulong oldmask
;
3132 target_ulong sc_gregs
[16];
3136 target_ulong sc_gbr
;
3137 target_ulong sc_mach
;
3138 target_ulong sc_macl
;
3141 target_ulong sc_fpregs
[16];
3142 target_ulong sc_xfpregs
[16];
3143 unsigned int sc_fpscr
;
3144 unsigned int sc_fpul
;
3145 unsigned int sc_ownedfp
;
3148 struct target_sigframe
3150 struct target_sigcontext sc
;
3151 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
3152 uint16_t retcode
[3];
3156 struct target_ucontext
{
3157 target_ulong tuc_flags
;
3158 struct target_ucontext
*tuc_link
;
3159 target_stack_t tuc_stack
;
3160 struct target_sigcontext tuc_mcontext
;
3161 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
3164 struct target_rt_sigframe
3166 struct target_siginfo info
;
3167 struct target_ucontext uc
;
3168 uint16_t retcode
[3];
3172 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3173 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3175 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3176 unsigned long sp
, size_t frame_size
)
3178 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags(sp
) == 0)) {
3179 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3182 return (sp
- frame_size
) & -8ul;
3185 static int setup_sigcontext(struct target_sigcontext
*sc
,
3186 CPUSH4State
*regs
, unsigned long mask
)
3191 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
3192 COPY(gregs
[0]); COPY(gregs
[1]);
3193 COPY(gregs
[2]); COPY(gregs
[3]);
3194 COPY(gregs
[4]); COPY(gregs
[5]);
3195 COPY(gregs
[6]); COPY(gregs
[7]);
3196 COPY(gregs
[8]); COPY(gregs
[9]);
3197 COPY(gregs
[10]); COPY(gregs
[11]);
3198 COPY(gregs
[12]); COPY(gregs
[13]);
3199 COPY(gregs
[14]); COPY(gregs
[15]);
3200 COPY(gbr
); COPY(mach
);
3201 COPY(macl
); COPY(pr
);
3205 for (i
=0; i
<16; i
++) {
3206 err
|= __put_user(regs
->fregs
[i
], &sc
->sc_fpregs
[i
]);
3208 err
|= __put_user(regs
->fpscr
, &sc
->sc_fpscr
);
3209 err
|= __put_user(regs
->fpul
, &sc
->sc_fpul
);
3211 /* non-iBCS2 extensions.. */
3212 err
|= __put_user(mask
, &sc
->oldmask
);
3217 static int restore_sigcontext(CPUSH4State
*regs
, struct target_sigcontext
*sc
,
3220 unsigned int err
= 0;
3223 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3225 COPY(gregs
[2]); COPY(gregs
[3]);
3226 COPY(gregs
[4]); COPY(gregs
[5]);
3227 COPY(gregs
[6]); COPY(gregs
[7]);
3228 COPY(gregs
[8]); COPY(gregs
[9]);
3229 COPY(gregs
[10]); COPY(gregs
[11]);
3230 COPY(gregs
[12]); COPY(gregs
[13]);
3231 COPY(gregs
[14]); COPY(gregs
[15]);
3232 COPY(gbr
); COPY(mach
);
3233 COPY(macl
); COPY(pr
);
3237 for (i
=0; i
<16; i
++) {
3238 err
|= __get_user(regs
->fregs
[i
], &sc
->sc_fpregs
[i
]);
3240 err
|= __get_user(regs
->fpscr
, &sc
->sc_fpscr
);
3241 err
|= __get_user(regs
->fpul
, &sc
->sc_fpul
);
3243 regs
->tra
= -1; /* disable syscall checks */
3244 err
|= __get_user(*r0_p
, &sc
->sc_gregs
[0]);
3248 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3249 target_sigset_t
*set
, CPUSH4State
*regs
)
3251 struct target_sigframe
*frame
;
3252 abi_ulong frame_addr
;
3257 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
3258 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3261 signal
= current_exec_domain_sig(sig
);
3263 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
3265 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
3266 err
|= __put_user(set
->sig
[i
+ 1], &frame
->extramask
[i
]);
3269 /* Set up to return from userspace. If provided, use a stub
3270 already in userspace. */
3271 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3272 regs
->pr
= (unsigned long) ka
->sa_restorer
;
3274 /* Generate return code (system call to sigreturn) */
3275 err
|= __put_user(MOVW(2), &frame
->retcode
[0]);
3276 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
3277 err
|= __put_user((TARGET_NR_sigreturn
), &frame
->retcode
[2]);
3278 regs
->pr
= (unsigned long) frame
->retcode
;
3284 /* Set up registers for signal handler */
3285 regs
->gregs
[15] = frame_addr
;
3286 regs
->gregs
[4] = signal
; /* Arg for signal handler */
3288 regs
->gregs
[6] = frame_addr
+= offsetof(typeof(*frame
), sc
);
3289 regs
->pc
= (unsigned long) ka
->_sa_handler
;
3291 unlock_user_struct(frame
, frame_addr
, 1);
3295 unlock_user_struct(frame
, frame_addr
, 1);
3296 force_sig(TARGET_SIGSEGV
);
3299 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3300 target_siginfo_t
*info
,
3301 target_sigset_t
*set
, CPUSH4State
*regs
)
3303 struct target_rt_sigframe
*frame
;
3304 abi_ulong frame_addr
;
3309 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
3310 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3313 signal
= current_exec_domain_sig(sig
);
3315 err
|= copy_siginfo_to_user(&frame
->info
, info
);
3317 /* Create the ucontext. */
3318 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
3319 err
|= __put_user(0, (unsigned long *)&frame
->uc
.tuc_link
);
3320 err
|= __put_user((unsigned long)target_sigaltstack_used
.ss_sp
,
3321 &frame
->uc
.tuc_stack
.ss_sp
);
3322 err
|= __put_user(sas_ss_flags(regs
->gregs
[15]),
3323 &frame
->uc
.tuc_stack
.ss_flags
);
3324 err
|= __put_user(target_sigaltstack_used
.ss_size
,
3325 &frame
->uc
.tuc_stack
.ss_size
);
3326 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
,
3328 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3329 err
|= __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
3332 /* Set up to return from userspace. If provided, use a stub
3333 already in userspace. */
3334 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3335 regs
->pr
= (unsigned long) ka
->sa_restorer
;
3337 /* Generate return code (system call to sigreturn) */
3338 err
|= __put_user(MOVW(2), &frame
->retcode
[0]);
3339 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
3340 err
|= __put_user((TARGET_NR_rt_sigreturn
), &frame
->retcode
[2]);
3341 regs
->pr
= (unsigned long) frame
->retcode
;
3347 /* Set up registers for signal handler */
3348 regs
->gregs
[15] = frame_addr
;
3349 regs
->gregs
[4] = signal
; /* Arg for signal handler */
3350 regs
->gregs
[5] = frame_addr
+ offsetof(typeof(*frame
), info
);
3351 regs
->gregs
[6] = frame_addr
+ offsetof(typeof(*frame
), uc
);
3352 regs
->pc
= (unsigned long) ka
->_sa_handler
;
3354 unlock_user_struct(frame
, frame_addr
, 1);
3358 unlock_user_struct(frame
, frame_addr
, 1);
3359 force_sig(TARGET_SIGSEGV
);
3362 long do_sigreturn(CPUSH4State
*regs
)
3364 struct target_sigframe
*frame
;
3365 abi_ulong frame_addr
;
3367 target_sigset_t target_set
;
3372 #if defined(DEBUG_SIGNAL)
3373 fprintf(stderr
, "do_sigreturn\n");
3375 frame_addr
= regs
->gregs
[15];
3376 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
3379 err
|= __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
);
3380 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3381 err
|= (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]));
3387 target_to_host_sigset_internal(&blocked
, &target_set
);
3388 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3390 if (restore_sigcontext(regs
, &frame
->sc
, &r0
))
3393 unlock_user_struct(frame
, frame_addr
, 0);
3397 unlock_user_struct(frame
, frame_addr
, 0);
3398 force_sig(TARGET_SIGSEGV
);
3402 long do_rt_sigreturn(CPUSH4State
*regs
)
3404 struct target_rt_sigframe
*frame
;
3405 abi_ulong frame_addr
;
3409 #if defined(DEBUG_SIGNAL)
3410 fprintf(stderr
, "do_rt_sigreturn\n");
3412 frame_addr
= regs
->gregs
[15];
3413 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
3416 target_to_host_sigset(&blocked
, &frame
->uc
.tuc_sigmask
);
3417 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3419 if (restore_sigcontext(regs
, &frame
->uc
.tuc_mcontext
, &r0
))
3422 if (do_sigaltstack(frame_addr
+
3423 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
3424 0, get_sp_from_cpustate(regs
)) == -EFAULT
)
3427 unlock_user_struct(frame
, frame_addr
, 0);
3431 unlock_user_struct(frame
, frame_addr
, 0);
3432 force_sig(TARGET_SIGSEGV
);
3435 #elif defined(TARGET_MICROBLAZE)
3437 struct target_sigcontext
{
3438 struct target_pt_regs regs
; /* needs to be first */
3442 struct target_stack_t
{
3445 unsigned int ss_size
;
3448 struct target_ucontext
{
3449 abi_ulong tuc_flags
;
3451 struct target_stack_t tuc_stack
;
3452 struct target_sigcontext tuc_mcontext
;
3453 uint32_t tuc_extramask
[TARGET_NSIG_WORDS
- 1];
3456 /* Signal frames. */
3457 struct target_signal_frame
{
3458 struct target_ucontext uc
;
3459 uint32_t extramask
[TARGET_NSIG_WORDS
- 1];
3463 struct rt_signal_frame
{
3469 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUMBState
*env
)
3471 __put_user(env
->regs
[0], &sc
->regs
.r0
);
3472 __put_user(env
->regs
[1], &sc
->regs
.r1
);
3473 __put_user(env
->regs
[2], &sc
->regs
.r2
);
3474 __put_user(env
->regs
[3], &sc
->regs
.r3
);
3475 __put_user(env
->regs
[4], &sc
->regs
.r4
);
3476 __put_user(env
->regs
[5], &sc
->regs
.r5
);
3477 __put_user(env
->regs
[6], &sc
->regs
.r6
);
3478 __put_user(env
->regs
[7], &sc
->regs
.r7
);
3479 __put_user(env
->regs
[8], &sc
->regs
.r8
);
3480 __put_user(env
->regs
[9], &sc
->regs
.r9
);
3481 __put_user(env
->regs
[10], &sc
->regs
.r10
);
3482 __put_user(env
->regs
[11], &sc
->regs
.r11
);
3483 __put_user(env
->regs
[12], &sc
->regs
.r12
);
3484 __put_user(env
->regs
[13], &sc
->regs
.r13
);
3485 __put_user(env
->regs
[14], &sc
->regs
.r14
);
3486 __put_user(env
->regs
[15], &sc
->regs
.r15
);
3487 __put_user(env
->regs
[16], &sc
->regs
.r16
);
3488 __put_user(env
->regs
[17], &sc
->regs
.r17
);
3489 __put_user(env
->regs
[18], &sc
->regs
.r18
);
3490 __put_user(env
->regs
[19], &sc
->regs
.r19
);
3491 __put_user(env
->regs
[20], &sc
->regs
.r20
);
3492 __put_user(env
->regs
[21], &sc
->regs
.r21
);
3493 __put_user(env
->regs
[22], &sc
->regs
.r22
);
3494 __put_user(env
->regs
[23], &sc
->regs
.r23
);
3495 __put_user(env
->regs
[24], &sc
->regs
.r24
);
3496 __put_user(env
->regs
[25], &sc
->regs
.r25
);
3497 __put_user(env
->regs
[26], &sc
->regs
.r26
);
3498 __put_user(env
->regs
[27], &sc
->regs
.r27
);
3499 __put_user(env
->regs
[28], &sc
->regs
.r28
);
3500 __put_user(env
->regs
[29], &sc
->regs
.r29
);
3501 __put_user(env
->regs
[30], &sc
->regs
.r30
);
3502 __put_user(env
->regs
[31], &sc
->regs
.r31
);
3503 __put_user(env
->sregs
[SR_PC
], &sc
->regs
.pc
);
3506 static void restore_sigcontext(struct target_sigcontext
*sc
, CPUMBState
*env
)
3508 __get_user(env
->regs
[0], &sc
->regs
.r0
);
3509 __get_user(env
->regs
[1], &sc
->regs
.r1
);
3510 __get_user(env
->regs
[2], &sc
->regs
.r2
);
3511 __get_user(env
->regs
[3], &sc
->regs
.r3
);
3512 __get_user(env
->regs
[4], &sc
->regs
.r4
);
3513 __get_user(env
->regs
[5], &sc
->regs
.r5
);
3514 __get_user(env
->regs
[6], &sc
->regs
.r6
);
3515 __get_user(env
->regs
[7], &sc
->regs
.r7
);
3516 __get_user(env
->regs
[8], &sc
->regs
.r8
);
3517 __get_user(env
->regs
[9], &sc
->regs
.r9
);
3518 __get_user(env
->regs
[10], &sc
->regs
.r10
);
3519 __get_user(env
->regs
[11], &sc
->regs
.r11
);
3520 __get_user(env
->regs
[12], &sc
->regs
.r12
);
3521 __get_user(env
->regs
[13], &sc
->regs
.r13
);
3522 __get_user(env
->regs
[14], &sc
->regs
.r14
);
3523 __get_user(env
->regs
[15], &sc
->regs
.r15
);
3524 __get_user(env
->regs
[16], &sc
->regs
.r16
);
3525 __get_user(env
->regs
[17], &sc
->regs
.r17
);
3526 __get_user(env
->regs
[18], &sc
->regs
.r18
);
3527 __get_user(env
->regs
[19], &sc
->regs
.r19
);
3528 __get_user(env
->regs
[20], &sc
->regs
.r20
);
3529 __get_user(env
->regs
[21], &sc
->regs
.r21
);
3530 __get_user(env
->regs
[22], &sc
->regs
.r22
);
3531 __get_user(env
->regs
[23], &sc
->regs
.r23
);
3532 __get_user(env
->regs
[24], &sc
->regs
.r24
);
3533 __get_user(env
->regs
[25], &sc
->regs
.r25
);
3534 __get_user(env
->regs
[26], &sc
->regs
.r26
);
3535 __get_user(env
->regs
[27], &sc
->regs
.r27
);
3536 __get_user(env
->regs
[28], &sc
->regs
.r28
);
3537 __get_user(env
->regs
[29], &sc
->regs
.r29
);
3538 __get_user(env
->regs
[30], &sc
->regs
.r30
);
3539 __get_user(env
->regs
[31], &sc
->regs
.r31
);
3540 __get_user(env
->sregs
[SR_PC
], &sc
->regs
.pc
);
3543 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3544 CPUMBState
*env
, int frame_size
)
3546 abi_ulong sp
= env
->regs
[1];
3548 if ((ka
->sa_flags
& SA_ONSTACK
) != 0 && !on_sig_stack(sp
))
3549 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3551 return ((sp
- frame_size
) & -8UL);
3554 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3555 target_sigset_t
*set
, CPUMBState
*env
)
3557 struct target_signal_frame
*frame
;
3558 abi_ulong frame_addr
;
3562 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
3563 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3566 /* Save the mask. */
3567 err
|= __put_user(set
->sig
[0], &frame
->uc
.tuc_mcontext
.oldmask
);
3571 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3572 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
3576 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
);
3578 /* Set up to return from userspace. If provided, use a stub
3579 already in userspace. */
3580 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3581 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3582 env
->regs
[15] = ((unsigned long)ka
->sa_restorer
)-8;
3585 /* Note, these encodings are _big endian_! */
3586 /* addi r12, r0, __NR_sigreturn */
3587 t
= 0x31800000UL
| TARGET_NR_sigreturn
;
3588 err
|= __put_user(t
, frame
->tramp
+ 0);
3591 err
|= __put_user(t
, frame
->tramp
+ 1);
3593 /* Return from sighandler will jump to the tramp.
3594 Negative 8 offset because return is rtsd r15, 8 */
3595 env
->regs
[15] = ((unsigned long)frame
->tramp
) - 8;
3601 /* Set up registers for signal handler */
3602 env
->regs
[1] = frame_addr
;
3603 /* Signal handler args: */
3604 env
->regs
[5] = sig
; /* Arg 0: signum */
3606 /* arg 1: sigcontext */
3607 env
->regs
[7] = frame_addr
+= offsetof(typeof(*frame
), uc
);
3609 /* Offset of 4 to handle microblaze rtid r14, 0 */
3610 env
->sregs
[SR_PC
] = (unsigned long)ka
->_sa_handler
;
3612 unlock_user_struct(frame
, frame_addr
, 1);
3615 unlock_user_struct(frame
, frame_addr
, 1);
3616 force_sig(TARGET_SIGSEGV
);
3619 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3620 target_siginfo_t
*info
,
3621 target_sigset_t
*set
, CPUMBState
*env
)
3623 fprintf(stderr
, "Microblaze setup_rt_frame: not implemented\n");
3626 long do_sigreturn(CPUMBState
*env
)
3628 struct target_signal_frame
*frame
;
3629 abi_ulong frame_addr
;
3630 target_sigset_t target_set
;
3634 frame_addr
= env
->regs
[R_SP
];
3635 /* Make sure the guest isn't playing games. */
3636 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
3639 /* Restore blocked signals */
3640 if (__get_user(target_set
.sig
[0], &frame
->uc
.tuc_mcontext
.oldmask
))
3642 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3643 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
3646 target_to_host_sigset_internal(&set
, &target_set
);
3647 sigprocmask(SIG_SETMASK
, &set
, NULL
);
3649 restore_sigcontext(&frame
->uc
.tuc_mcontext
, env
);
3650 /* We got here through a sigreturn syscall, our path back is via an
3651 rtb insn so setup r14 for that. */
3652 env
->regs
[14] = env
->sregs
[SR_PC
];
3654 unlock_user_struct(frame
, frame_addr
, 0);
3655 return env
->regs
[10];
3657 unlock_user_struct(frame
, frame_addr
, 0);
3658 force_sig(TARGET_SIGSEGV
);
3661 long do_rt_sigreturn(CPUMBState
*env
)
3663 fprintf(stderr
, "Microblaze do_rt_sigreturn: not implemented\n");
3664 return -TARGET_ENOSYS
;
3667 #elif defined(TARGET_CRIS)
3669 struct target_sigcontext
{
3670 struct target_pt_regs regs
; /* needs to be first */
3672 uint32_t usp
; /* usp before stacking this gunk on it */
3675 /* Signal frames. */
3676 struct target_signal_frame
{
3677 struct target_sigcontext sc
;
3678 uint32_t extramask
[TARGET_NSIG_WORDS
- 1];
3679 uint16_t retcode
[4]; /* Trampoline code. */
3682 struct rt_signal_frame
{
3687 uint16_t retcode
[4]; /* Trampoline code. */
3690 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUCRISState
*env
)
3692 __put_user(env
->regs
[0], &sc
->regs
.r0
);
3693 __put_user(env
->regs
[1], &sc
->regs
.r1
);
3694 __put_user(env
->regs
[2], &sc
->regs
.r2
);
3695 __put_user(env
->regs
[3], &sc
->regs
.r3
);
3696 __put_user(env
->regs
[4], &sc
->regs
.r4
);
3697 __put_user(env
->regs
[5], &sc
->regs
.r5
);
3698 __put_user(env
->regs
[6], &sc
->regs
.r6
);
3699 __put_user(env
->regs
[7], &sc
->regs
.r7
);
3700 __put_user(env
->regs
[8], &sc
->regs
.r8
);
3701 __put_user(env
->regs
[9], &sc
->regs
.r9
);
3702 __put_user(env
->regs
[10], &sc
->regs
.r10
);
3703 __put_user(env
->regs
[11], &sc
->regs
.r11
);
3704 __put_user(env
->regs
[12], &sc
->regs
.r12
);
3705 __put_user(env
->regs
[13], &sc
->regs
.r13
);
3706 __put_user(env
->regs
[14], &sc
->usp
);
3707 __put_user(env
->regs
[15], &sc
->regs
.acr
);
3708 __put_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
3709 __put_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
3710 __put_user(env
->pc
, &sc
->regs
.erp
);
3713 static void restore_sigcontext(struct target_sigcontext
*sc
, CPUCRISState
*env
)
3715 __get_user(env
->regs
[0], &sc
->regs
.r0
);
3716 __get_user(env
->regs
[1], &sc
->regs
.r1
);
3717 __get_user(env
->regs
[2], &sc
->regs
.r2
);
3718 __get_user(env
->regs
[3], &sc
->regs
.r3
);
3719 __get_user(env
->regs
[4], &sc
->regs
.r4
);
3720 __get_user(env
->regs
[5], &sc
->regs
.r5
);
3721 __get_user(env
->regs
[6], &sc
->regs
.r6
);
3722 __get_user(env
->regs
[7], &sc
->regs
.r7
);
3723 __get_user(env
->regs
[8], &sc
->regs
.r8
);
3724 __get_user(env
->regs
[9], &sc
->regs
.r9
);
3725 __get_user(env
->regs
[10], &sc
->regs
.r10
);
3726 __get_user(env
->regs
[11], &sc
->regs
.r11
);
3727 __get_user(env
->regs
[12], &sc
->regs
.r12
);
3728 __get_user(env
->regs
[13], &sc
->regs
.r13
);
3729 __get_user(env
->regs
[14], &sc
->usp
);
3730 __get_user(env
->regs
[15], &sc
->regs
.acr
);
3731 __get_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
3732 __get_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
3733 __get_user(env
->pc
, &sc
->regs
.erp
);
3736 static abi_ulong
get_sigframe(CPUCRISState
*env
, int framesize
)
3739 /* Align the stack downwards to 4. */
3740 sp
= (env
->regs
[R_SP
] & ~3);
3741 return sp
- framesize
;
3744 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3745 target_sigset_t
*set
, CPUCRISState
*env
)
3747 struct target_signal_frame
*frame
;
3748 abi_ulong frame_addr
;
3752 frame_addr
= get_sigframe(env
, sizeof *frame
);
3753 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3757 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3758 * use this trampoline anymore but it sets it up for GDB.
3759 * In QEMU, using the trampoline simplifies things a bit so we use it.
3761 * This is movu.w __NR_sigreturn, r9; break 13;
3763 err
|= __put_user(0x9c5f, frame
->retcode
+0);
3764 err
|= __put_user(TARGET_NR_sigreturn
,
3765 frame
->retcode
+ 1);
3766 err
|= __put_user(0xe93d, frame
->retcode
+ 2);
3768 /* Save the mask. */
3769 err
|= __put_user(set
->sig
[0], &frame
->sc
.oldmask
);
3773 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3774 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
3778 setup_sigcontext(&frame
->sc
, env
);
3780 /* Move the stack and setup the arguments for the handler. */
3781 env
->regs
[R_SP
] = frame_addr
;
3782 env
->regs
[10] = sig
;
3783 env
->pc
= (unsigned long) ka
->_sa_handler
;
3784 /* Link SRP so the guest returns through the trampoline. */
3785 env
->pregs
[PR_SRP
] = frame_addr
+ offsetof(typeof(*frame
), retcode
);
3787 unlock_user_struct(frame
, frame_addr
, 1);
3790 unlock_user_struct(frame
, frame_addr
, 1);
3791 force_sig(TARGET_SIGSEGV
);
3794 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3795 target_siginfo_t
*info
,
3796 target_sigset_t
*set
, CPUCRISState
*env
)
3798 fprintf(stderr
, "CRIS setup_rt_frame: not implemented\n");
3801 long do_sigreturn(CPUCRISState
*env
)
3803 struct target_signal_frame
*frame
;
3804 abi_ulong frame_addr
;
3805 target_sigset_t target_set
;
3809 frame_addr
= env
->regs
[R_SP
];
3810 /* Make sure the guest isn't playing games. */
3811 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
3814 /* Restore blocked signals */
3815 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
3817 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3818 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
3821 target_to_host_sigset_internal(&set
, &target_set
);
3822 sigprocmask(SIG_SETMASK
, &set
, NULL
);
3824 restore_sigcontext(&frame
->sc
, env
);
3825 unlock_user_struct(frame
, frame_addr
, 0);
3826 return env
->regs
[10];
3828 unlock_user_struct(frame
, frame_addr
, 0);
3829 force_sig(TARGET_SIGSEGV
);
3832 long do_rt_sigreturn(CPUCRISState
*env
)
3834 fprintf(stderr
, "CRIS do_rt_sigreturn: not implemented\n");
3835 return -TARGET_ENOSYS
;
3838 #elif defined(TARGET_OPENRISC)
3840 struct target_sigcontext
{
3841 struct target_pt_regs regs
;
3846 struct target_ucontext
{
3847 abi_ulong tuc_flags
;
3849 target_stack_t tuc_stack
;
3850 struct target_sigcontext tuc_mcontext
;
3851 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
3854 struct target_rt_sigframe
{
3857 struct target_siginfo info
;
3858 struct target_sigcontext sc
;
3859 struct target_ucontext uc
;
3860 unsigned char retcode
[16]; /* trampoline code */
3863 /* This is the asm-generic/ucontext.h version */
3865 static int restore_sigcontext(CPUOpenRISCState
*regs
,
3866 struct target_sigcontext
*sc
)
3868 unsigned int err
= 0;
3869 unsigned long old_usp
;
3871 /* Alwys make any pending restarted system call return -EINTR */
3872 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
3874 /* restore the regs from &sc->regs (same as sc, since regs is first)
3875 * (sc is already checked for VERIFY_READ since the sigframe was
3876 * checked in sys_sigreturn previously)
3879 if (copy_from_user(regs
, &sc
, sizeof(struct target_pt_regs
))) {
3883 /* make sure the U-flag is set so user-mode cannot fool us */
3887 /* restore the old USP as it was before we stacked the sc etc.
3888 * (we cannot just pop the sigcontext since we aligned the sp and
3889 * stuff after pushing it)
3892 err
|= __get_user(old_usp
, &sc
->usp
);
3893 phx_signal("old_usp 0x%lx", old_usp
);
3895 __PHX__ REALLY
/* ??? */
3897 regs
->gpr
[1] = old_usp
;
3899 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3900 * after this completes, but we don't use that mechanism. maybe we can
3911 /* Set up a signal frame. */
3913 static int setup_sigcontext(struct target_sigcontext
*sc
,
3914 CPUOpenRISCState
*regs
,
3918 unsigned long usp
= regs
->gpr
[1];
3920 /* copy the regs. they are first in sc so we can use sc directly */
3922 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3924 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3925 the signal handler. The frametype will be restored to its previous
3926 value in restore_sigcontext. */
3927 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3929 /* then some other stuff */
3930 err
|= __put_user(mask
, &sc
->oldmask
);
3931 err
|= __put_user(usp
, &sc
->usp
); return err
;
3934 static inline unsigned long align_sigframe(unsigned long sp
)
3941 static inline abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3942 CPUOpenRISCState
*regs
,
3945 unsigned long sp
= regs
->gpr
[1];
3946 int onsigstack
= on_sig_stack(sp
);
3949 /* This is the X/Open sanctioned signal stack switching. */
3950 if ((ka
->sa_flags
& SA_ONSTACK
) != 0 && !onsigstack
) {
3951 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3954 sp
= align_sigframe(sp
- frame_size
);
3957 * If we are on the alternate signal stack and would overflow it, don't.
3958 * Return an always-bogus address instead so we will die with SIGSEGV.
3961 if (onsigstack
&& !likely(on_sig_stack(sp
))) {
3968 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3969 target_sigset_t
*set
, CPUOpenRISCState
*env
)
3971 qemu_log("Not implement.\n");
3974 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3975 target_siginfo_t
*info
,
3976 target_sigset_t
*set
, CPUOpenRISCState
*env
)
3979 abi_ulong frame_addr
;
3980 unsigned long return_ip
;
3981 struct target_rt_sigframe
*frame
;
3982 abi_ulong info_addr
, uc_addr
;
3984 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
3986 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
3987 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
3991 info_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
3992 err
|= __put_user(info_addr
, &frame
->pinfo
);
3993 uc_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
3994 err
|= __put_user(uc_addr
, &frame
->puc
);
3996 if (ka
->sa_flags
& SA_SIGINFO
) {
3997 err
|= copy_siginfo_to_user(&frame
->info
, info
);
4003 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
4004 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
4005 err
|= __put_user(0, &frame
->uc
.tuc_link
);
4006 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
4007 &frame
->uc
.tuc_stack
.ss_sp
);
4008 err
|= __put_user(sas_ss_flags(env
->gpr
[1]), &frame
->uc
.tuc_stack
.ss_flags
);
4009 err
|= __put_user(target_sigaltstack_used
.ss_size
,
4010 &frame
->uc
.tuc_stack
.ss_size
);
4011 err
|= setup_sigcontext(&frame
->sc
, env
, set
->sig
[0]);
4013 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4019 /* trampoline - the desired return ip is the retcode itself */
4020 return_ip
= (unsigned long)&frame
->retcode
;
4021 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
4022 err
|= __put_user(0xa960, (short *)(frame
->retcode
+ 0));
4023 err
|= __put_user(TARGET_NR_rt_sigreturn
, (short *)(frame
->retcode
+ 2));
4024 err
|= __put_user(0x20000001, (unsigned long *)(frame
->retcode
+ 4));
4025 err
|= __put_user(0x15000000, (unsigned long *)(frame
->retcode
+ 8));
4031 /* TODO what is the current->exec_domain stuff and invmap ? */
4033 /* Set up registers for signal handler */
4034 env
->pc
= (unsigned long)ka
->_sa_handler
; /* what we enter NOW */
4035 env
->gpr
[9] = (unsigned long)return_ip
; /* what we enter LATER */
4036 env
->gpr
[3] = (unsigned long)sig
; /* arg 1: signo */
4037 env
->gpr
[4] = (unsigned long)&frame
->info
; /* arg 2: (siginfo_t*) */
4038 env
->gpr
[5] = (unsigned long)&frame
->uc
; /* arg 3: ucontext */
4040 /* actually move the usp to reflect the stacked frame */
4041 env
->gpr
[1] = (unsigned long)frame
;
4046 unlock_user_struct(frame
, frame_addr
, 1);
4047 if (sig
== TARGET_SIGSEGV
) {
4048 ka
->_sa_handler
= TARGET_SIG_DFL
;
4050 force_sig(TARGET_SIGSEGV
);
4053 long do_sigreturn(CPUOpenRISCState
*env
)
4056 qemu_log("do_sigreturn: not implemented\n");
4057 return -TARGET_ENOSYS
;
4060 long do_rt_sigreturn(CPUOpenRISCState
*env
)
4062 qemu_log("do_rt_sigreturn: not implemented\n");
4063 return -TARGET_ENOSYS
;
4065 /* TARGET_OPENRISC */
4067 #elif defined(TARGET_S390X)
4069 #define __NUM_GPRS 16
4070 #define __NUM_FPRS 16
4071 #define __NUM_ACRS 16
4073 #define S390_SYSCALL_SIZE 2
4074 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4076 #define _SIGCONTEXT_NSIG 64
4077 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4078 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4079 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4080 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4081 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4085 target_ulong gprs
[__NUM_GPRS
];
4086 unsigned int acrs
[__NUM_ACRS
];
4087 } target_s390_regs_common
;
4091 double fprs
[__NUM_FPRS
];
4092 } target_s390_fp_regs
;
4095 target_s390_regs_common regs
;
4096 target_s390_fp_regs fpregs
;
4099 struct target_sigcontext
{
4100 target_ulong oldmask
[_SIGCONTEXT_NSIG_WORDS
];
4101 target_sigregs
*sregs
;
4105 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
4106 struct target_sigcontext sc
;
4107 target_sigregs sregs
;
4109 uint8_t retcode
[S390_SYSCALL_SIZE
];
4112 struct target_ucontext
{
4113 target_ulong tuc_flags
;
4114 struct target_ucontext
*tuc_link
;
4115 target_stack_t tuc_stack
;
4116 target_sigregs tuc_mcontext
;
4117 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
4121 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
4122 uint8_t retcode
[S390_SYSCALL_SIZE
];
4123 struct target_siginfo info
;
4124 struct target_ucontext uc
;
4127 static inline abi_ulong
4128 get_sigframe(struct target_sigaction
*ka
, CPUS390XState
*env
, size_t frame_size
)
4132 /* Default to using normal stack */
4135 /* This is the X/Open sanctioned signal stack switching. */
4136 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
4137 if (!sas_ss_flags(sp
)) {
4138 sp
= target_sigaltstack_used
.ss_sp
+
4139 target_sigaltstack_used
.ss_size
;
4143 /* This is the legacy signal stack switching. */
4144 else if (/* FIXME !user_mode(regs) */ 0 &&
4145 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
4147 sp
= (abi_ulong
) ka
->sa_restorer
;
4150 return (sp
- frame_size
) & -8ul;
4153 static void save_sigregs(CPUS390XState
*env
, target_sigregs
*sregs
)
4156 //save_access_regs(current->thread.acrs); FIXME
4158 /* Copy a 'clean' PSW mask to the user to avoid leaking
4159 information about whether PER is currently on. */
4160 __put_user(env
->psw
.mask
, &sregs
->regs
.psw
.mask
);
4161 __put_user(env
->psw
.addr
, &sregs
->regs
.psw
.addr
);
4162 for (i
= 0; i
< 16; i
++) {
4163 __put_user(env
->regs
[i
], &sregs
->regs
.gprs
[i
]);
4165 for (i
= 0; i
< 16; i
++) {
4166 __put_user(env
->aregs
[i
], &sregs
->regs
.acrs
[i
]);
4169 * We have to store the fp registers to current->thread.fp_regs
4170 * to merge them with the emulated registers.
4172 //save_fp_regs(¤t->thread.fp_regs); FIXME
4173 for (i
= 0; i
< 16; i
++) {
4174 __put_user(env
->fregs
[i
].ll
, &sregs
->fpregs
.fprs
[i
]);
4178 static void setup_frame(int sig
, struct target_sigaction
*ka
,
4179 target_sigset_t
*set
, CPUS390XState
*env
)
4182 abi_ulong frame_addr
;
4184 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
4185 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4186 (unsigned long long)frame_addr
);
4187 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
4191 qemu_log("%s: 1\n", __FUNCTION__
);
4192 if (__put_user(set
->sig
[0], &frame
->sc
.oldmask
[0])) {
4196 save_sigregs(env
, &frame
->sregs
);
4198 __put_user((abi_ulong
)(unsigned long)&frame
->sregs
,
4199 (abi_ulong
*)&frame
->sc
.sregs
);
4201 /* Set up to return from userspace. If provided, use a stub
4202 already in userspace. */
4203 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
4204 env
->regs
[14] = (unsigned long)
4205 ka
->sa_restorer
| PSW_ADDR_AMODE
;
4207 env
->regs
[14] = (unsigned long)
4208 frame
->retcode
| PSW_ADDR_AMODE
;
4209 if (__put_user(S390_SYSCALL_OPCODE
| TARGET_NR_sigreturn
,
4210 (uint16_t *)(frame
->retcode
)))
4214 /* Set up backchain. */
4215 if (__put_user(env
->regs
[15], (abi_ulong
*) frame
)) {
4219 /* Set up registers for signal handler */
4220 env
->regs
[15] = frame_addr
;
4221 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
4223 env
->regs
[2] = sig
; //map_signal(sig);
4224 env
->regs
[3] = frame_addr
+= offsetof(typeof(*frame
), sc
);
4226 /* We forgot to include these in the sigcontext.
4227 To avoid breaking binary compatibility, they are passed as args. */
4228 env
->regs
[4] = 0; // FIXME: no clue... current->thread.trap_no;
4229 env
->regs
[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4231 /* Place signal number on stack to allow backtrace from handler. */
4232 if (__put_user(env
->regs
[2], (int *) &frame
->signo
)) {
4235 unlock_user_struct(frame
, frame_addr
, 1);
4239 qemu_log("%s: give_sigsegv\n", __FUNCTION__
);
4240 unlock_user_struct(frame
, frame_addr
, 1);
4241 force_sig(TARGET_SIGSEGV
);
4244 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
4245 target_siginfo_t
*info
,
4246 target_sigset_t
*set
, CPUS390XState
*env
)
4250 abi_ulong frame_addr
;
4252 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
4253 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4254 (unsigned long long)frame_addr
);
4255 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
4259 qemu_log("%s: 1\n", __FUNCTION__
);
4260 if (copy_siginfo_to_user(&frame
->info
, info
)) {
4264 /* Create the ucontext. */
4265 __put_user(0, &frame
->uc
.tuc_flags
);
4266 __put_user((abi_ulong
)0, (abi_ulong
*)&frame
->uc
.tuc_link
);
4267 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->uc
.tuc_stack
.ss_sp
);
4268 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
4269 &frame
->uc
.tuc_stack
.ss_flags
);
4270 __put_user(target_sigaltstack_used
.ss_size
, &frame
->uc
.tuc_stack
.ss_size
);
4271 save_sigregs(env
, &frame
->uc
.tuc_mcontext
);
4272 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
4273 __put_user((abi_ulong
)set
->sig
[i
],
4274 (abi_ulong
*)&frame
->uc
.tuc_sigmask
.sig
[i
]);
4277 /* Set up to return from userspace. If provided, use a stub
4278 already in userspace. */
4279 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
4280 env
->regs
[14] = (unsigned long) ka
->sa_restorer
| PSW_ADDR_AMODE
;
4282 env
->regs
[14] = (unsigned long) frame
->retcode
| PSW_ADDR_AMODE
;
4283 if (__put_user(S390_SYSCALL_OPCODE
| TARGET_NR_rt_sigreturn
,
4284 (uint16_t *)(frame
->retcode
))) {
4289 /* Set up backchain. */
4290 if (__put_user(env
->regs
[15], (abi_ulong
*) frame
)) {
4294 /* Set up registers for signal handler */
4295 env
->regs
[15] = frame_addr
;
4296 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
4298 env
->regs
[2] = sig
; //map_signal(sig);
4299 env
->regs
[3] = frame_addr
+ offsetof(typeof(*frame
), info
);
4300 env
->regs
[4] = frame_addr
+ offsetof(typeof(*frame
), uc
);
4304 qemu_log("%s: give_sigsegv\n", __FUNCTION__
);
4305 unlock_user_struct(frame
, frame_addr
, 1);
4306 force_sig(TARGET_SIGSEGV
);
4310 restore_sigregs(CPUS390XState
*env
, target_sigregs
*sc
)
4315 for (i
= 0; i
< 16; i
++) {
4316 err
|= __get_user(env
->regs
[i
], &sc
->regs
.gprs
[i
]);
4319 err
|= __get_user(env
->psw
.mask
, &sc
->regs
.psw
.mask
);
4320 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4321 __FUNCTION__
, (unsigned long long)sc
->regs
.psw
.addr
,
4322 (unsigned long long)env
->psw
.addr
);
4323 err
|= __get_user(env
->psw
.addr
, &sc
->regs
.psw
.addr
);
4324 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4326 for (i
= 0; i
< 16; i
++) {
4327 err
|= __get_user(env
->aregs
[i
], &sc
->regs
.acrs
[i
]);
4329 for (i
= 0; i
< 16; i
++) {
4330 err
|= __get_user(env
->fregs
[i
].ll
, &sc
->fpregs
.fprs
[i
]);
4336 long do_sigreturn(CPUS390XState
*env
)
4339 abi_ulong frame_addr
= env
->regs
[15];
4340 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4341 (unsigned long long)frame_addr
);
4342 target_sigset_t target_set
;
4345 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
4348 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
[0])) {
4352 target_to_host_sigset_internal(&set
, &target_set
);
4353 sigprocmask(SIG_SETMASK
, &set
, NULL
); /* ~_BLOCKABLE? */
4355 if (restore_sigregs(env
, &frame
->sregs
)) {
4359 unlock_user_struct(frame
, frame_addr
, 0);
4360 return env
->regs
[2];
4363 unlock_user_struct(frame
, frame_addr
, 0);
4364 force_sig(TARGET_SIGSEGV
);
4368 long do_rt_sigreturn(CPUS390XState
*env
)
4371 abi_ulong frame_addr
= env
->regs
[15];
4372 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4373 (unsigned long long)frame_addr
);
4376 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
4379 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
4381 sigprocmask(SIG_SETMASK
, &set
, NULL
); /* ~_BLOCKABLE? */
4383 if (restore_sigregs(env
, &frame
->uc
.tuc_mcontext
)) {
4387 if (do_sigaltstack(frame_addr
+ offsetof(rt_sigframe
, uc
.tuc_stack
), 0,
4388 get_sp_from_cpustate(env
)) == -EFAULT
) {
4391 unlock_user_struct(frame
, frame_addr
, 0);
4392 return env
->regs
[2];
4395 unlock_user_struct(frame
, frame_addr
, 0);
4396 force_sig(TARGET_SIGSEGV
);
4400 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4402 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4403 the signal handling is different enough that we haven't implemented
4404 support for PPC64 yet. Hence the restriction above.
4406 There are various #if'd blocks for code for TARGET_PPC64. These
4407 blocks should go away so that we can successfully run 32-bit and
4408 64-bit binaries on a QEMU configured for PPC64. */
4410 /* Size of dummy stack frame allocated when calling signal handler.
4411 See arch/powerpc/include/asm/ptrace.h. */
4412 #if defined(TARGET_PPC64)
4413 #define SIGNAL_FRAMESIZE 128
4415 #define SIGNAL_FRAMESIZE 64
4418 /* See arch/powerpc/include/asm/sigcontext.h. */
4419 struct target_sigcontext
{
4420 target_ulong _unused
[4];
4422 #if defined(TARGET_PPC64)
4425 target_ulong handler
;
4426 target_ulong oldmask
;
4427 target_ulong regs
; /* struct pt_regs __user * */
4428 /* TODO: PPC64 includes extra bits here. */
4431 /* Indices for target_mcontext.mc_gregs, below.
4432 See arch/powerpc/include/asm/ptrace.h for details. */
4468 TARGET_PT_ORIG_R3
= 34,
4473 /* Yes, there are two registers with #39. One is 64-bit only. */
4475 TARGET_PT_SOFTE
= 39,
4476 TARGET_PT_TRAP
= 40,
4478 TARGET_PT_DSISR
= 42,
4479 TARGET_PT_RESULT
= 43,
4480 TARGET_PT_REGS_COUNT
= 44
4483 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4484 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4485 struct target_mcontext
{
4486 target_ulong mc_gregs
[48];
4487 /* Includes fpscr. */
4488 uint64_t mc_fregs
[33];
4489 target_ulong mc_pad
[2];
4490 /* We need to handle Altivec and SPE at the same time, which no
4491 kernel needs to do. Fortunately, the kernel defines this bit to
4492 be Altivec-register-large all the time, rather than trying to
4493 twiddle it based on the specific platform. */
4495 /* SPE vector registers. One extra for SPEFSCR. */
4497 /* Altivec vector registers. The packing of VSCR and VRSAVE
4498 varies depending on whether we're PPC64 or not: PPC64 splits
4499 them apart; PPC32 stuffs them together. */
4500 #if defined(TARGET_PPC64)
4501 #define QEMU_NVRREG 34
4503 #define QEMU_NVRREG 33
4505 ppc_avr_t altivec
[QEMU_NVRREG
];
4507 } mc_vregs
__attribute__((__aligned__(16)));
4510 struct target_ucontext
{
4511 target_ulong tuc_flags
;
4512 target_ulong tuc_link
; /* struct ucontext __user * */
4513 struct target_sigaltstack tuc_stack
;
4514 #if !defined(TARGET_PPC64)
4516 target_ulong tuc_regs
; /* struct mcontext __user *
4517 points to uc_mcontext field */
4519 target_sigset_t tuc_sigmask
;
4520 #if defined(TARGET_PPC64)
4521 target_sigset_t unused
[15]; /* Allow for uc_sigmask growth */
4522 struct target_sigcontext tuc_mcontext
;
4524 int32_t tuc_maskext
[30];
4525 int32_t tuc_pad2
[3];
4526 struct target_mcontext tuc_mcontext
;
4530 /* See arch/powerpc/kernel/signal_32.c. */
4531 struct target_sigframe
{
4532 struct target_sigcontext sctx
;
4533 struct target_mcontext mctx
;
4537 struct target_rt_sigframe
{
4538 struct target_siginfo info
;
4539 struct target_ucontext uc
;
4543 /* We use the mc_pad field for the signal return trampoline. */
4544 #define tramp mc_pad
4546 /* See arch/powerpc/kernel/signal.c. */
4547 static target_ulong
get_sigframe(struct target_sigaction
*ka
,
4551 target_ulong oldsp
, newsp
;
4553 oldsp
= env
->gpr
[1];
4555 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) &&
4556 (sas_ss_flags(oldsp
) == 0)) {
4557 oldsp
= (target_sigaltstack_used
.ss_sp
4558 + target_sigaltstack_used
.ss_size
);
4561 newsp
= (oldsp
- frame_size
) & ~0xFUL
;
4566 static int save_user_regs(CPUPPCState
*env
, struct target_mcontext
*frame
,
4569 target_ulong msr
= env
->msr
;
4571 target_ulong ccr
= 0;
4573 /* In general, the kernel attempts to be intelligent about what it
4574 needs to save for Altivec/FP/SPE registers. We don't care that
4575 much, so we just go ahead and save everything. */
4577 /* Save general registers. */
4578 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4579 if (__put_user(env
->gpr
[i
], &frame
->mc_gregs
[i
])) {
4583 if (__put_user(env
->nip
, &frame
->mc_gregs
[TARGET_PT_NIP
])
4584 || __put_user(env
->ctr
, &frame
->mc_gregs
[TARGET_PT_CTR
])
4585 || __put_user(env
->lr
, &frame
->mc_gregs
[TARGET_PT_LNK
])
4586 || __put_user(env
->xer
, &frame
->mc_gregs
[TARGET_PT_XER
]))
4589 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
4590 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
4592 if (__put_user(ccr
, &frame
->mc_gregs
[TARGET_PT_CCR
]))
4595 /* Save Altivec registers if necessary. */
4596 if (env
->insns_flags
& PPC_ALTIVEC
) {
4597 for (i
= 0; i
< ARRAY_SIZE(env
->avr
); i
++) {
4598 ppc_avr_t
*avr
= &env
->avr
[i
];
4599 ppc_avr_t
*vreg
= &frame
->mc_vregs
.altivec
[i
];
4601 if (__put_user(avr
->u64
[0], &vreg
->u64
[0]) ||
4602 __put_user(avr
->u64
[1], &vreg
->u64
[1])) {
4606 /* Set MSR_VR in the saved MSR value to indicate that
4607 frame->mc_vregs contains valid data. */
4609 if (__put_user((uint32_t)env
->spr
[SPR_VRSAVE
],
4610 &frame
->mc_vregs
.altivec
[32].u32
[3]))
4614 /* Save floating point registers. */
4615 if (env
->insns_flags
& PPC_FLOAT
) {
4616 for (i
= 0; i
< ARRAY_SIZE(env
->fpr
); i
++) {
4617 if (__put_user(env
->fpr
[i
], &frame
->mc_fregs
[i
])) {
4621 if (__put_user((uint64_t) env
->fpscr
, &frame
->mc_fregs
[32]))
4625 /* Save SPE registers. The kernel only saves the high half. */
4626 if (env
->insns_flags
& PPC_SPE
) {
4627 #if defined(TARGET_PPC64)
4628 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4629 if (__put_user(env
->gpr
[i
] >> 32, &frame
->mc_vregs
.spe
[i
])) {
4634 for (i
= 0; i
< ARRAY_SIZE(env
->gprh
); i
++) {
4635 if (__put_user(env
->gprh
[i
], &frame
->mc_vregs
.spe
[i
])) {
4640 /* Set MSR_SPE in the saved MSR value to indicate that
4641 frame->mc_vregs contains valid data. */
4643 if (__put_user(env
->spe_fscr
, &frame
->mc_vregs
.spe
[32]))
4648 if (__put_user(msr
, &frame
->mc_gregs
[TARGET_PT_MSR
]))
4651 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4653 if (__put_user(0x38000000UL
| sigret
, &frame
->tramp
[0]) ||
4654 __put_user(0x44000002UL
, &frame
->tramp
[1])) {
4662 static int restore_user_regs(CPUPPCState
*env
,
4663 struct target_mcontext
*frame
, int sig
)
4665 target_ulong save_r2
= 0;
4672 save_r2
= env
->gpr
[2];
4675 /* Restore general registers. */
4676 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4677 if (__get_user(env
->gpr
[i
], &frame
->mc_gregs
[i
])) {
4681 if (__get_user(env
->nip
, &frame
->mc_gregs
[TARGET_PT_NIP
])
4682 || __get_user(env
->ctr
, &frame
->mc_gregs
[TARGET_PT_CTR
])
4683 || __get_user(env
->lr
, &frame
->mc_gregs
[TARGET_PT_LNK
])
4684 || __get_user(env
->xer
, &frame
->mc_gregs
[TARGET_PT_XER
]))
4686 if (__get_user(ccr
, &frame
->mc_gregs
[TARGET_PT_CCR
]))
4689 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
4690 env
->crf
[i
] = (ccr
>> (32 - ((i
+ 1) * 4))) & 0xf;
4694 env
->gpr
[2] = save_r2
;
4697 if (__get_user(msr
, &frame
->mc_gregs
[TARGET_PT_MSR
]))
4700 /* If doing signal return, restore the previous little-endian mode. */
4702 env
->msr
= (env
->msr
& ~MSR_LE
) | (msr
& MSR_LE
);
4704 /* Restore Altivec registers if necessary. */
4705 if (env
->insns_flags
& PPC_ALTIVEC
) {
4706 for (i
= 0; i
< ARRAY_SIZE(env
->avr
); i
++) {
4707 ppc_avr_t
*avr
= &env
->avr
[i
];
4708 ppc_avr_t
*vreg
= &frame
->mc_vregs
.altivec
[i
];
4710 if (__get_user(avr
->u64
[0], &vreg
->u64
[0]) ||
4711 __get_user(avr
->u64
[1], &vreg
->u64
[1])) {
4715 /* Set MSR_VEC in the saved MSR value to indicate that
4716 frame->mc_vregs contains valid data. */
4717 if (__get_user(env
->spr
[SPR_VRSAVE
],
4718 (target_ulong
*)(&frame
->mc_vregs
.altivec
[32].u32
[3])))
4722 /* Restore floating point registers. */
4723 if (env
->insns_flags
& PPC_FLOAT
) {
4725 for (i
= 0; i
< ARRAY_SIZE(env
->fpr
); i
++) {
4726 if (__get_user(env
->fpr
[i
], &frame
->mc_fregs
[i
])) {
4730 if (__get_user(fpscr
, &frame
->mc_fregs
[32]))
4732 env
->fpscr
= (uint32_t) fpscr
;
4735 /* Save SPE registers. The kernel only saves the high half. */
4736 if (env
->insns_flags
& PPC_SPE
) {
4737 #if defined(TARGET_PPC64)
4738 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4741 if (__get_user(hi
, &frame
->mc_vregs
.spe
[i
])) {
4744 env
->gpr
[i
] = ((uint64_t)hi
<< 32) | ((uint32_t) env
->gpr
[i
]);
4747 for (i
= 0; i
< ARRAY_SIZE(env
->gprh
); i
++) {
4748 if (__get_user(env
->gprh
[i
], &frame
->mc_vregs
.spe
[i
])) {
4753 if (__get_user(env
->spe_fscr
, &frame
->mc_vregs
.spe
[32]))
4760 static void setup_frame(int sig
, struct target_sigaction
*ka
,
4761 target_sigset_t
*set
, CPUPPCState
*env
)
4763 struct target_sigframe
*frame
;
4764 struct target_sigcontext
*sc
;
4765 target_ulong frame_addr
, newsp
;
4769 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
4770 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
4774 signal
= current_exec_domain_sig(sig
);
4776 err
|= __put_user(ka
->_sa_handler
, &sc
->handler
);
4777 err
|= __put_user(set
->sig
[0], &sc
->oldmask
);
4778 #if defined(TARGET_PPC64)
4779 err
|= __put_user(set
->sig
[0] >> 32, &sc
->_unused
[3]);
4781 err
|= __put_user(set
->sig
[1], &sc
->_unused
[3]);
4783 err
|= __put_user(h2g(&frame
->mctx
), &sc
->regs
);
4784 err
|= __put_user(sig
, &sc
->signal
);
4786 /* Save user regs. */
4787 err
|= save_user_regs(env
, &frame
->mctx
, TARGET_NR_sigreturn
);
4789 /* The kernel checks for the presence of a VDSO here. We don't
4790 emulate a vdso, so use a sigreturn system call. */
4791 env
->lr
= (target_ulong
) h2g(frame
->mctx
.tramp
);
4793 /* Turn off all fp exceptions. */
4796 /* Create a stack frame for the caller of the handler. */
4797 newsp
= frame_addr
- SIGNAL_FRAMESIZE
;
4798 err
|= put_user(env
->gpr
[1], newsp
, target_ulong
);
4803 /* Set up registers for signal handler. */
4804 env
->gpr
[1] = newsp
;
4805 env
->gpr
[3] = signal
;
4806 env
->gpr
[4] = frame_addr
+ offsetof(struct target_sigframe
, sctx
);
4807 env
->nip
= (target_ulong
) ka
->_sa_handler
;
4808 /* Signal handlers are entered in big-endian mode. */
4809 env
->msr
&= ~MSR_LE
;
4811 unlock_user_struct(frame
, frame_addr
, 1);
4815 unlock_user_struct(frame
, frame_addr
, 1);
4816 qemu_log("segfaulting from setup_frame\n");
4817 force_sig(TARGET_SIGSEGV
);
4820 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
4821 target_siginfo_t
*info
,
4822 target_sigset_t
*set
, CPUPPCState
*env
)
4824 struct target_rt_sigframe
*rt_sf
;
4825 struct target_mcontext
*frame
;
4826 target_ulong rt_sf_addr
, newsp
= 0;
4830 rt_sf_addr
= get_sigframe(ka
, env
, sizeof(*rt_sf
));
4831 if (!lock_user_struct(VERIFY_WRITE
, rt_sf
, rt_sf_addr
, 1))
4834 signal
= current_exec_domain_sig(sig
);
4836 err
|= copy_siginfo_to_user(&rt_sf
->info
, info
);
4838 err
|= __put_user(0, &rt_sf
->uc
.tuc_flags
);
4839 err
|= __put_user(0, &rt_sf
->uc
.tuc_link
);
4840 err
|= __put_user((target_ulong
)target_sigaltstack_used
.ss_sp
,
4841 &rt_sf
->uc
.tuc_stack
.ss_sp
);
4842 err
|= __put_user(sas_ss_flags(env
->gpr
[1]),
4843 &rt_sf
->uc
.tuc_stack
.ss_flags
);
4844 err
|= __put_user(target_sigaltstack_used
.ss_size
,
4845 &rt_sf
->uc
.tuc_stack
.ss_size
);
4846 err
|= __put_user(h2g (&rt_sf
->uc
.tuc_mcontext
),
4847 &rt_sf
->uc
.tuc_regs
);
4848 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
4849 err
|= __put_user(set
->sig
[i
], &rt_sf
->uc
.tuc_sigmask
.sig
[i
]);
4852 frame
= &rt_sf
->uc
.tuc_mcontext
;
4853 err
|= save_user_regs(env
, frame
, TARGET_NR_rt_sigreturn
);
4855 /* The kernel checks for the presence of a VDSO here. We don't
4856 emulate a vdso, so use a sigreturn system call. */
4857 env
->lr
= (target_ulong
) h2g(frame
->tramp
);
4859 /* Turn off all fp exceptions. */
4862 /* Create a stack frame for the caller of the handler. */
4863 newsp
= rt_sf_addr
- (SIGNAL_FRAMESIZE
+ 16);
4864 err
|= __put_user(env
->gpr
[1], (target_ulong
*)(uintptr_t) newsp
);
4869 /* Set up registers for signal handler. */
4870 env
->gpr
[1] = newsp
;
4871 env
->gpr
[3] = (target_ulong
) signal
;
4872 env
->gpr
[4] = (target_ulong
) h2g(&rt_sf
->info
);
4873 env
->gpr
[5] = (target_ulong
) h2g(&rt_sf
->uc
);
4874 env
->gpr
[6] = (target_ulong
) h2g(rt_sf
);
4875 env
->nip
= (target_ulong
) ka
->_sa_handler
;
4876 /* Signal handlers are entered in big-endian mode. */
4877 env
->msr
&= ~MSR_LE
;
4879 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4883 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4884 qemu_log("segfaulting from setup_rt_frame\n");
4885 force_sig(TARGET_SIGSEGV
);
4889 long do_sigreturn(CPUPPCState
*env
)
4891 struct target_sigcontext
*sc
= NULL
;
4892 struct target_mcontext
*sr
= NULL
;
4893 target_ulong sr_addr
= 0, sc_addr
;
4895 target_sigset_t set
;
4897 sc_addr
= env
->gpr
[1] + SIGNAL_FRAMESIZE
;
4898 if (!lock_user_struct(VERIFY_READ
, sc
, sc_addr
, 1))
4901 #if defined(TARGET_PPC64)
4902 set
.sig
[0] = sc
->oldmask
+ ((long)(sc
->_unused
[3]) << 32);
4904 if(__get_user(set
.sig
[0], &sc
->oldmask
) ||
4905 __get_user(set
.sig
[1], &sc
->_unused
[3]))
4908 target_to_host_sigset_internal(&blocked
, &set
);
4909 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
4911 if (__get_user(sr_addr
, &sc
->regs
))
4913 if (!lock_user_struct(VERIFY_READ
, sr
, sr_addr
, 1))
4915 if (restore_user_regs(env
, sr
, 1))
4918 unlock_user_struct(sr
, sr_addr
, 1);
4919 unlock_user_struct(sc
, sc_addr
, 1);
4920 return -TARGET_QEMU_ESIGRETURN
;
4923 unlock_user_struct(sr
, sr_addr
, 1);
4924 unlock_user_struct(sc
, sc_addr
, 1);
4925 qemu_log("segfaulting from do_sigreturn\n");
4926 force_sig(TARGET_SIGSEGV
);
4930 /* See arch/powerpc/kernel/signal_32.c. */
4931 static int do_setcontext(struct target_ucontext
*ucp
, CPUPPCState
*env
, int sig
)
4933 struct target_mcontext
*mcp
;
4934 target_ulong mcp_addr
;
4936 target_sigset_t set
;
4938 if (copy_from_user(&set
, h2g(ucp
) + offsetof(struct target_ucontext
, tuc_sigmask
),
4942 #if defined(TARGET_PPC64)
4943 fprintf (stderr
, "do_setcontext: not implemented\n");
4946 if (__get_user(mcp_addr
, &ucp
->tuc_regs
))
4949 if (!lock_user_struct(VERIFY_READ
, mcp
, mcp_addr
, 1))
4952 target_to_host_sigset_internal(&blocked
, &set
);
4953 sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
4954 if (restore_user_regs(env
, mcp
, sig
))
4957 unlock_user_struct(mcp
, mcp_addr
, 1);
4961 unlock_user_struct(mcp
, mcp_addr
, 1);
4966 long do_rt_sigreturn(CPUPPCState
*env
)
4968 struct target_rt_sigframe
*rt_sf
= NULL
;
4969 target_ulong rt_sf_addr
;
4971 rt_sf_addr
= env
->gpr
[1] + SIGNAL_FRAMESIZE
+ 16;
4972 if (!lock_user_struct(VERIFY_READ
, rt_sf
, rt_sf_addr
, 1))
4975 if (do_setcontext(&rt_sf
->uc
, env
, 1))
4978 do_sigaltstack(rt_sf_addr
4979 + offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
4982 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4983 return -TARGET_QEMU_ESIGRETURN
;
4986 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4987 qemu_log("segfaulting from do_rt_sigreturn\n");
4988 force_sig(TARGET_SIGSEGV
);
4992 #elif defined(TARGET_M68K)
4994 struct target_sigcontext
{
5001 unsigned short sc_sr
;
5005 struct target_sigframe
5012 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
5013 struct target_sigcontext sc
;
5016 typedef int target_greg_t
;
5017 #define TARGET_NGREG 18
5018 typedef target_greg_t target_gregset_t
[TARGET_NGREG
];
5020 typedef struct target_fpregset
{
5023 } target_fpregset_t
;
5025 struct target_mcontext
{
5027 target_gregset_t gregs
;
5028 target_fpregset_t fpregs
;
5031 #define TARGET_MCONTEXT_VERSION 2
5033 struct target_ucontext
{
5034 abi_ulong tuc_flags
;
5036 target_stack_t tuc_stack
;
5037 struct target_mcontext tuc_mcontext
;
5038 abi_long tuc_filler
[80];
5039 target_sigset_t tuc_sigmask
;
5042 struct target_rt_sigframe
5049 struct target_siginfo info
;
5050 struct target_ucontext uc
;
5054 setup_sigcontext(struct target_sigcontext
*sc
, CPUM68KState
*env
,
5059 err
|= __put_user(mask
, &sc
->sc_mask
);
5060 err
|= __put_user(env
->aregs
[7], &sc
->sc_usp
);
5061 err
|= __put_user(env
->dregs
[0], &sc
->sc_d0
);
5062 err
|= __put_user(env
->dregs
[1], &sc
->sc_d1
);
5063 err
|= __put_user(env
->aregs
[0], &sc
->sc_a0
);
5064 err
|= __put_user(env
->aregs
[1], &sc
->sc_a1
);
5065 err
|= __put_user(env
->sr
, &sc
->sc_sr
);
5066 err
|= __put_user(env
->pc
, &sc
->sc_pc
);
5072 restore_sigcontext(CPUM68KState
*env
, struct target_sigcontext
*sc
, int *pd0
)
5077 err
|= __get_user(env
->aregs
[7], &sc
->sc_usp
);
5078 err
|= __get_user(env
->dregs
[1], &sc
->sc_d1
);
5079 err
|= __get_user(env
->aregs
[0], &sc
->sc_a0
);
5080 err
|= __get_user(env
->aregs
[1], &sc
->sc_a1
);
5081 err
|= __get_user(env
->pc
, &sc
->sc_pc
);
5082 err
|= __get_user(temp
, &sc
->sc_sr
);
5083 env
->sr
= (env
->sr
& 0xff00) | (temp
& 0xff);
5085 *pd0
= tswapl(sc
->sc_d0
);
5091 * Determine which stack to use..
5093 static inline abi_ulong
5094 get_sigframe(struct target_sigaction
*ka
, CPUM68KState
*regs
,
5099 sp
= regs
->aregs
[7];
5101 /* This is the X/Open sanctioned signal stack switching. */
5102 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
5103 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
5106 return ((sp
- frame_size
) & -8UL);
5109 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5110 target_sigset_t
*set
, CPUM68KState
*env
)
5112 struct target_sigframe
*frame
;
5113 abi_ulong frame_addr
;
5114 abi_ulong retcode_addr
;
5119 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
5120 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
5123 err
|= __put_user(sig
, &frame
->sig
);
5125 sc_addr
= frame_addr
+ offsetof(struct target_sigframe
, sc
);
5126 err
|= __put_user(sc_addr
, &frame
->psc
);
5128 err
|= setup_sigcontext(&frame
->sc
, env
, set
->sig
[0]);
5132 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
5133 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
5137 /* Set up to return from userspace. */
5139 retcode_addr
= frame_addr
+ offsetof(struct target_sigframe
, retcode
);
5140 err
|= __put_user(retcode_addr
, &frame
->pretcode
);
5142 /* moveq #,d0; trap #0 */
5144 err
|= __put_user(0x70004e40 + (TARGET_NR_sigreturn
<< 16),
5145 (long *)(frame
->retcode
));
5150 /* Set up to return from userspace */
5152 env
->aregs
[7] = frame_addr
;
5153 env
->pc
= ka
->_sa_handler
;
5155 unlock_user_struct(frame
, frame_addr
, 1);
5159 unlock_user_struct(frame
, frame_addr
, 1);
5160 force_sig(TARGET_SIGSEGV
);
5163 static inline int target_rt_setup_ucontext(struct target_ucontext
*uc
,
5166 target_greg_t
*gregs
= uc
->tuc_mcontext
.gregs
;
5169 err
= __put_user(TARGET_MCONTEXT_VERSION
, &uc
->tuc_mcontext
.version
);
5170 err
|= __put_user(env
->dregs
[0], &gregs
[0]);
5171 err
|= __put_user(env
->dregs
[1], &gregs
[1]);
5172 err
|= __put_user(env
->dregs
[2], &gregs
[2]);
5173 err
|= __put_user(env
->dregs
[3], &gregs
[3]);
5174 err
|= __put_user(env
->dregs
[4], &gregs
[4]);
5175 err
|= __put_user(env
->dregs
[5], &gregs
[5]);
5176 err
|= __put_user(env
->dregs
[6], &gregs
[6]);
5177 err
|= __put_user(env
->dregs
[7], &gregs
[7]);
5178 err
|= __put_user(env
->aregs
[0], &gregs
[8]);
5179 err
|= __put_user(env
->aregs
[1], &gregs
[9]);
5180 err
|= __put_user(env
->aregs
[2], &gregs
[10]);
5181 err
|= __put_user(env
->aregs
[3], &gregs
[11]);
5182 err
|= __put_user(env
->aregs
[4], &gregs
[12]);
5183 err
|= __put_user(env
->aregs
[5], &gregs
[13]);
5184 err
|= __put_user(env
->aregs
[6], &gregs
[14]);
5185 err
|= __put_user(env
->aregs
[7], &gregs
[15]);
5186 err
|= __put_user(env
->pc
, &gregs
[16]);
5187 err
|= __put_user(env
->sr
, &gregs
[17]);
5192 static inline int target_rt_restore_ucontext(CPUM68KState
*env
,
5193 struct target_ucontext
*uc
,
5198 target_greg_t
*gregs
= uc
->tuc_mcontext
.gregs
;
5200 err
= __get_user(temp
, &uc
->tuc_mcontext
.version
);
5201 if (temp
!= TARGET_MCONTEXT_VERSION
)
5204 /* restore passed registers */
5205 err
|= __get_user(env
->dregs
[0], &gregs
[0]);
5206 err
|= __get_user(env
->dregs
[1], &gregs
[1]);
5207 err
|= __get_user(env
->dregs
[2], &gregs
[2]);
5208 err
|= __get_user(env
->dregs
[3], &gregs
[3]);
5209 err
|= __get_user(env
->dregs
[4], &gregs
[4]);
5210 err
|= __get_user(env
->dregs
[5], &gregs
[5]);
5211 err
|= __get_user(env
->dregs
[6], &gregs
[6]);
5212 err
|= __get_user(env
->dregs
[7], &gregs
[7]);
5213 err
|= __get_user(env
->aregs
[0], &gregs
[8]);
5214 err
|= __get_user(env
->aregs
[1], &gregs
[9]);
5215 err
|= __get_user(env
->aregs
[2], &gregs
[10]);
5216 err
|= __get_user(env
->aregs
[3], &gregs
[11]);
5217 err
|= __get_user(env
->aregs
[4], &gregs
[12]);
5218 err
|= __get_user(env
->aregs
[5], &gregs
[13]);
5219 err
|= __get_user(env
->aregs
[6], &gregs
[14]);
5220 err
|= __get_user(env
->aregs
[7], &gregs
[15]);
5221 err
|= __get_user(env
->pc
, &gregs
[16]);
5222 err
|= __get_user(temp
, &gregs
[17]);
5223 env
->sr
= (env
->sr
& 0xff00) | (temp
& 0xff);
5225 *pd0
= env
->dregs
[0];
5232 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5233 target_siginfo_t
*info
,
5234 target_sigset_t
*set
, CPUM68KState
*env
)
5236 struct target_rt_sigframe
*frame
;
5237 abi_ulong frame_addr
;
5238 abi_ulong retcode_addr
;
5239 abi_ulong info_addr
;
5244 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
5245 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
5248 err
|= __put_user(sig
, &frame
->sig
);
5250 info_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
5251 err
|= __put_user(info_addr
, &frame
->pinfo
);
5253 uc_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
5254 err
|= __put_user(uc_addr
, &frame
->puc
);
5256 err
|= copy_siginfo_to_user(&frame
->info
, info
);
5258 /* Create the ucontext */
5260 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
5261 err
|= __put_user(0, &frame
->uc
.tuc_link
);
5262 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
5263 &frame
->uc
.tuc_stack
.ss_sp
);
5264 err
|= __put_user(sas_ss_flags(env
->aregs
[7]),
5265 &frame
->uc
.tuc_stack
.ss_flags
);
5266 err
|= __put_user(target_sigaltstack_used
.ss_size
,
5267 &frame
->uc
.tuc_stack
.ss_size
);
5268 err
|= target_rt_setup_ucontext(&frame
->uc
, env
);
5273 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
5274 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
5278 /* Set up to return from userspace. */
5280 retcode_addr
= frame_addr
+ offsetof(struct target_sigframe
, retcode
);
5281 err
|= __put_user(retcode_addr
, &frame
->pretcode
);
5283 /* moveq #,d0; notb d0; trap #0 */
5285 err
|= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn
^ 0xff) << 16),
5286 (long *)(frame
->retcode
+ 0));
5287 err
|= __put_user(0x4e40, (short *)(frame
->retcode
+ 4));
5292 /* Set up to return from userspace */
5294 env
->aregs
[7] = frame_addr
;
5295 env
->pc
= ka
->_sa_handler
;
5297 unlock_user_struct(frame
, frame_addr
, 1);
5301 unlock_user_struct(frame
, frame_addr
, 1);
5302 force_sig(TARGET_SIGSEGV
);
5305 long do_sigreturn(CPUM68KState
*env
)
5307 struct target_sigframe
*frame
;
5308 abi_ulong frame_addr
= env
->aregs
[7] - 4;
5309 target_sigset_t target_set
;
5313 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
5316 /* set blocked signals */
5318 if (__get_user(target_set
.sig
[0], &frame
->sc
.sc_mask
))
5321 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
5322 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
5326 target_to_host_sigset_internal(&set
, &target_set
);
5327 sigprocmask(SIG_SETMASK
, &set
, NULL
);
5329 /* restore registers */
5331 if (restore_sigcontext(env
, &frame
->sc
, &d0
))
5334 unlock_user_struct(frame
, frame_addr
, 0);
5338 unlock_user_struct(frame
, frame_addr
, 0);
5339 force_sig(TARGET_SIGSEGV
);
5343 long do_rt_sigreturn(CPUM68KState
*env
)
5345 struct target_rt_sigframe
*frame
;
5346 abi_ulong frame_addr
= env
->aregs
[7] - 4;
5347 target_sigset_t target_set
;
5351 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
5354 target_to_host_sigset_internal(&set
, &target_set
);
5355 sigprocmask(SIG_SETMASK
, &set
, NULL
);
5357 /* restore registers */
5359 if (target_rt_restore_ucontext(env
, &frame
->uc
, &d0
))
5362 if (do_sigaltstack(frame_addr
+
5363 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
5364 0, get_sp_from_cpustate(env
)) == -EFAULT
)
5367 unlock_user_struct(frame
, frame_addr
, 0);
5371 unlock_user_struct(frame
, frame_addr
, 0);
5372 force_sig(TARGET_SIGSEGV
);
5376 #elif defined(TARGET_ALPHA)
5378 struct target_sigcontext
{
5379 abi_long sc_onstack
;
5383 abi_long sc_regs
[32];
5384 abi_long sc_ownedfp
;
5385 abi_long sc_fpregs
[32];
5387 abi_ulong sc_fp_control
;
5388 abi_ulong sc_reserved1
;
5389 abi_ulong sc_reserved2
;
5392 abi_ulong sc_traparg_a0
;
5393 abi_ulong sc_traparg_a1
;
5394 abi_ulong sc_traparg_a2
;
5395 abi_ulong sc_fp_trap_pc
;
5396 abi_ulong sc_fp_trigger_sum
;
5397 abi_ulong sc_fp_trigger_inst
;
5400 struct target_ucontext
{
5401 abi_ulong tuc_flags
;
5403 abi_ulong tuc_osf_sigmask
;
5404 target_stack_t tuc_stack
;
5405 struct target_sigcontext tuc_mcontext
;
5406 target_sigset_t tuc_sigmask
;
5409 struct target_sigframe
{
5410 struct target_sigcontext sc
;
5411 unsigned int retcode
[3];
5414 struct target_rt_sigframe
{
5415 target_siginfo_t info
;
5416 struct target_ucontext uc
;
5417 unsigned int retcode
[3];
5420 #define INSN_MOV_R30_R16 0x47fe0410
5421 #define INSN_LDI_R0 0x201f0000
5422 #define INSN_CALLSYS 0x00000083
5424 static int setup_sigcontext(struct target_sigcontext
*sc
, CPUAlphaState
*env
,
5425 abi_ulong frame_addr
, target_sigset_t
*set
)
5429 err
|= __put_user(on_sig_stack(frame_addr
), &sc
->sc_onstack
);
5430 err
|= __put_user(set
->sig
[0], &sc
->sc_mask
);
5431 err
|= __put_user(env
->pc
, &sc
->sc_pc
);
5432 err
|= __put_user(8, &sc
->sc_ps
);
5434 for (i
= 0; i
< 31; ++i
) {
5435 err
|= __put_user(env
->ir
[i
], &sc
->sc_regs
[i
]);
5437 err
|= __put_user(0, &sc
->sc_regs
[31]);
5439 for (i
= 0; i
< 31; ++i
) {
5440 err
|= __put_user(env
->fir
[i
], &sc
->sc_fpregs
[i
]);
5442 err
|= __put_user(0, &sc
->sc_fpregs
[31]);
5443 err
|= __put_user(cpu_alpha_load_fpcr(env
), &sc
->sc_fpcr
);
5445 err
|= __put_user(0, &sc
->sc_traparg_a0
); /* FIXME */
5446 err
|= __put_user(0, &sc
->sc_traparg_a1
); /* FIXME */
5447 err
|= __put_user(0, &sc
->sc_traparg_a2
); /* FIXME */
5452 static int restore_sigcontext(CPUAlphaState
*env
,
5453 struct target_sigcontext
*sc
)
5458 err
|= __get_user(env
->pc
, &sc
->sc_pc
);
5460 for (i
= 0; i
< 31; ++i
) {
5461 err
|= __get_user(env
->ir
[i
], &sc
->sc_regs
[i
]);
5463 for (i
= 0; i
< 31; ++i
) {
5464 err
|= __get_user(env
->fir
[i
], &sc
->sc_fpregs
[i
]);
5467 err
|= __get_user(fpcr
, &sc
->sc_fpcr
);
5468 cpu_alpha_store_fpcr(env
, fpcr
);
5473 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
5475 unsigned long framesize
)
5477 abi_ulong sp
= env
->ir
[IR_SP
];
5479 /* This is the X/Open sanctioned signal stack switching. */
5480 if ((sa
->sa_flags
& TARGET_SA_ONSTACK
) != 0 && !sas_ss_flags(sp
)) {
5481 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
5483 return (sp
- framesize
) & -32;
5486 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5487 target_sigset_t
*set
, CPUAlphaState
*env
)
5489 abi_ulong frame_addr
, r26
;
5490 struct target_sigframe
*frame
;
5493 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
5494 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5498 err
|= setup_sigcontext(&frame
->sc
, env
, frame_addr
, set
);
5500 if (ka
->sa_restorer
) {
5501 r26
= ka
->sa_restorer
;
5503 err
|= __put_user(INSN_MOV_R30_R16
, &frame
->retcode
[0]);
5504 err
|= __put_user(INSN_LDI_R0
+ TARGET_NR_sigreturn
,
5505 &frame
->retcode
[1]);
5506 err
|= __put_user(INSN_CALLSYS
, &frame
->retcode
[2]);
5511 unlock_user_struct(frame
, frame_addr
, 1);
5515 if (sig
== TARGET_SIGSEGV
) {
5516 ka
->_sa_handler
= TARGET_SIG_DFL
;
5518 force_sig(TARGET_SIGSEGV
);
5521 env
->ir
[IR_RA
] = r26
;
5522 env
->ir
[IR_PV
] = env
->pc
= ka
->_sa_handler
;
5523 env
->ir
[IR_A0
] = sig
;
5525 env
->ir
[IR_A2
] = frame_addr
+ offsetof(struct target_sigframe
, sc
);
5526 env
->ir
[IR_SP
] = frame_addr
;
5529 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5530 target_siginfo_t
*info
,
5531 target_sigset_t
*set
, CPUAlphaState
*env
)
5533 abi_ulong frame_addr
, r26
;
5534 struct target_rt_sigframe
*frame
;
5537 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
5538 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5542 err
|= copy_siginfo_to_user(&frame
->info
, info
);
5544 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
5545 err
|= __put_user(0, &frame
->uc
.tuc_link
);
5546 err
|= __put_user(set
->sig
[0], &frame
->uc
.tuc_osf_sigmask
);
5547 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
5548 &frame
->uc
.tuc_stack
.ss_sp
);
5549 err
|= __put_user(sas_ss_flags(env
->ir
[IR_SP
]),
5550 &frame
->uc
.tuc_stack
.ss_flags
);
5551 err
|= __put_user(target_sigaltstack_used
.ss_size
,
5552 &frame
->uc
.tuc_stack
.ss_size
);
5553 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, frame_addr
, set
);
5554 for (i
= 0; i
< TARGET_NSIG_WORDS
; ++i
) {
5555 err
|= __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
5558 if (ka
->sa_restorer
) {
5559 r26
= ka
->sa_restorer
;
5561 err
|= __put_user(INSN_MOV_R30_R16
, &frame
->retcode
[0]);
5562 err
|= __put_user(INSN_LDI_R0
+ TARGET_NR_rt_sigreturn
,
5563 &frame
->retcode
[1]);
5564 err
|= __put_user(INSN_CALLSYS
, &frame
->retcode
[2]);
5571 if (sig
== TARGET_SIGSEGV
) {
5572 ka
->_sa_handler
= TARGET_SIG_DFL
;
5574 force_sig(TARGET_SIGSEGV
);
5577 env
->ir
[IR_RA
] = r26
;
5578 env
->ir
[IR_PV
] = env
->pc
= ka
->_sa_handler
;
5579 env
->ir
[IR_A0
] = sig
;
5580 env
->ir
[IR_A1
] = frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
5581 env
->ir
[IR_A2
] = frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
5582 env
->ir
[IR_SP
] = frame_addr
;
5585 long do_sigreturn(CPUAlphaState
*env
)
5587 struct target_sigcontext
*sc
;
5588 abi_ulong sc_addr
= env
->ir
[IR_A0
];
5589 target_sigset_t target_set
;
5592 if (!lock_user_struct(VERIFY_READ
, sc
, sc_addr
, 1)) {
5596 target_sigemptyset(&target_set
);
5597 if (__get_user(target_set
.sig
[0], &sc
->sc_mask
)) {
5601 target_to_host_sigset_internal(&set
, &target_set
);
5602 sigprocmask(SIG_SETMASK
, &set
, NULL
);
5604 if (restore_sigcontext(env
, sc
)) {
5607 unlock_user_struct(sc
, sc_addr
, 0);
5608 return env
->ir
[IR_V0
];
5611 unlock_user_struct(sc
, sc_addr
, 0);
5612 force_sig(TARGET_SIGSEGV
);
5615 long do_rt_sigreturn(CPUAlphaState
*env
)
5617 abi_ulong frame_addr
= env
->ir
[IR_A0
];
5618 struct target_rt_sigframe
*frame
;
5621 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
5624 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
5625 sigprocmask(SIG_SETMASK
, &set
, NULL
);
5627 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
)) {
5630 if (do_sigaltstack(frame_addr
+ offsetof(struct target_rt_sigframe
,
5632 0, env
->ir
[IR_SP
]) == -EFAULT
) {
5636 unlock_user_struct(frame
, frame_addr
, 0);
5637 return env
->ir
[IR_V0
];
5641 unlock_user_struct(frame
, frame_addr
, 0);
5642 force_sig(TARGET_SIGSEGV
);
5647 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5648 target_sigset_t
*set
, CPUArchState
*env
)
5650 fprintf(stderr
, "setup_frame: not implemented\n");
5653 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5654 target_siginfo_t
*info
,
5655 target_sigset_t
*set
, CPUArchState
*env
)
5657 fprintf(stderr
, "setup_rt_frame: not implemented\n");
5660 long do_sigreturn(CPUArchState
*env
)
5662 fprintf(stderr
, "do_sigreturn: not implemented\n");
5663 return -TARGET_ENOSYS
;
5666 long do_rt_sigreturn(CPUArchState
*env
)
5668 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
5669 return -TARGET_ENOSYS
;
5674 void process_pending_signals(CPUArchState
*cpu_env
)
5676 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
5679 sigset_t set
, old_set
;
5680 target_sigset_t target_old_set
;
5681 struct emulated_sigtable
*k
;
5682 struct target_sigaction
*sa
;
5684 TaskState
*ts
= cpu
->opaque
;
5686 if (!ts
->signal_pending
)
5689 /* FIXME: This is not threadsafe. */
5691 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
5696 /* if no signal is pending, just return */
5697 ts
->signal_pending
= 0;
5702 fprintf(stderr
, "qemu: process signal %d\n", sig
);
5704 /* dequeue signal */
5710 sig
= gdb_handlesig(cpu
, sig
);
5713 handler
= TARGET_SIG_IGN
;
5715 sa
= &sigact_table
[sig
- 1];
5716 handler
= sa
->_sa_handler
;
5719 if (handler
== TARGET_SIG_DFL
) {
5720 /* default handler : ignore some signal. The other are job control or fatal */
5721 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
5722 kill(getpid(),SIGSTOP
);
5723 } else if (sig
!= TARGET_SIGCHLD
&&
5724 sig
!= TARGET_SIGURG
&&
5725 sig
!= TARGET_SIGWINCH
&&
5726 sig
!= TARGET_SIGCONT
) {
5729 } else if (handler
== TARGET_SIG_IGN
) {
5731 } else if (handler
== TARGET_SIG_ERR
) {
5734 /* compute the blocked signals during the handler execution */
5735 target_to_host_sigset(&set
, &sa
->sa_mask
);
5736 /* SA_NODEFER indicates that the current signal should not be
5737 blocked during the handler */
5738 if (!(sa
->sa_flags
& TARGET_SA_NODEFER
))
5739 sigaddset(&set
, target_to_host_signal(sig
));
5741 /* block signals in the handler using Linux */
5742 sigprocmask(SIG_BLOCK
, &set
, &old_set
);
5743 /* save the previous blocked signal state to restore it at the
5744 end of the signal execution (see do_sigreturn) */
5745 host_to_target_sigset_internal(&target_old_set
, &old_set
);
5747 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5748 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5750 CPUX86State
*env
= cpu_env
;
5751 if (env
->eflags
& VM_MASK
)
5752 save_v86_state(env
);
5755 /* prepare the stack frame of the virtual CPU */
5756 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5757 /* These targets do not have traditional signals. */
5758 setup_rt_frame(sig
, sa
, &q
->info
, &target_old_set
, cpu_env
);
5760 if (sa
->sa_flags
& TARGET_SA_SIGINFO
)
5761 setup_rt_frame(sig
, sa
, &q
->info
, &target_old_set
, cpu_env
);
5763 setup_frame(sig
, sa
, &target_old_set
, cpu_env
);
5765 if (sa
->sa_flags
& TARGET_SA_RESETHAND
)
5766 sa
->_sa_handler
= TARGET_SIG_DFL
;
5769 free_sigqueue(cpu_env
, q
);