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/>.
19 #include "qemu/osdep.h"
20 #include <sys/ucontext.h>
21 #include <sys/resource.h>
24 #include "qemu-common.h"
25 #include "target_signal.h"
28 static struct target_sigaltstack target_sigaltstack_used
= {
31 .ss_flags
= TARGET_SS_DISABLE
,
34 static struct target_sigaction sigact_table
[TARGET_NSIG
];
36 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
39 static uint8_t host_to_target_signal_table
[_NSIG
] = {
40 [SIGHUP
] = TARGET_SIGHUP
,
41 [SIGINT
] = TARGET_SIGINT
,
42 [SIGQUIT
] = TARGET_SIGQUIT
,
43 [SIGILL
] = TARGET_SIGILL
,
44 [SIGTRAP
] = TARGET_SIGTRAP
,
45 [SIGABRT
] = TARGET_SIGABRT
,
46 /* [SIGIOT] = TARGET_SIGIOT,*/
47 [SIGBUS
] = TARGET_SIGBUS
,
48 [SIGFPE
] = TARGET_SIGFPE
,
49 [SIGKILL
] = TARGET_SIGKILL
,
50 [SIGUSR1
] = TARGET_SIGUSR1
,
51 [SIGSEGV
] = TARGET_SIGSEGV
,
52 [SIGUSR2
] = TARGET_SIGUSR2
,
53 [SIGPIPE
] = TARGET_SIGPIPE
,
54 [SIGALRM
] = TARGET_SIGALRM
,
55 [SIGTERM
] = TARGET_SIGTERM
,
57 [SIGSTKFLT
] = TARGET_SIGSTKFLT
,
59 [SIGCHLD
] = TARGET_SIGCHLD
,
60 [SIGCONT
] = TARGET_SIGCONT
,
61 [SIGSTOP
] = TARGET_SIGSTOP
,
62 [SIGTSTP
] = TARGET_SIGTSTP
,
63 [SIGTTIN
] = TARGET_SIGTTIN
,
64 [SIGTTOU
] = TARGET_SIGTTOU
,
65 [SIGURG
] = TARGET_SIGURG
,
66 [SIGXCPU
] = TARGET_SIGXCPU
,
67 [SIGXFSZ
] = TARGET_SIGXFSZ
,
68 [SIGVTALRM
] = TARGET_SIGVTALRM
,
69 [SIGPROF
] = TARGET_SIGPROF
,
70 [SIGWINCH
] = TARGET_SIGWINCH
,
71 [SIGIO
] = TARGET_SIGIO
,
72 [SIGPWR
] = TARGET_SIGPWR
,
73 [SIGSYS
] = TARGET_SIGSYS
,
74 /* next signals stay the same */
75 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
76 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
77 To fix this properly we need to do manual signal delivery multiplexed
78 over a single host signal. */
79 [__SIGRTMIN
] = __SIGRTMAX
,
80 [__SIGRTMAX
] = __SIGRTMIN
,
82 static uint8_t target_to_host_signal_table
[_NSIG
];
84 static inline int on_sig_stack(unsigned long sp
)
86 return (sp
- target_sigaltstack_used
.ss_sp
87 < target_sigaltstack_used
.ss_size
);
90 static inline int sas_ss_flags(unsigned long sp
)
92 return (target_sigaltstack_used
.ss_size
== 0 ? SS_DISABLE
93 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
96 int host_to_target_signal(int sig
)
98 if (sig
< 0 || sig
>= _NSIG
)
100 return host_to_target_signal_table
[sig
];
103 int target_to_host_signal(int sig
)
105 if (sig
< 0 || sig
>= _NSIG
)
107 return target_to_host_signal_table
[sig
];
110 static inline void target_sigemptyset(target_sigset_t
*set
)
112 memset(set
, 0, sizeof(*set
));
115 static inline void target_sigaddset(target_sigset_t
*set
, int signum
)
118 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
119 set
->sig
[signum
/ TARGET_NSIG_BPW
] |= mask
;
122 static inline int target_sigismember(const target_sigset_t
*set
, int signum
)
125 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
126 return ((set
->sig
[signum
/ TARGET_NSIG_BPW
] & mask
) != 0);
129 static void host_to_target_sigset_internal(target_sigset_t
*d
,
133 target_sigemptyset(d
);
134 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
135 if (sigismember(s
, i
)) {
136 target_sigaddset(d
, host_to_target_signal(i
));
141 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
146 host_to_target_sigset_internal(&d1
, s
);
147 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
148 d
->sig
[i
] = tswapal(d1
.sig
[i
]);
151 static void target_to_host_sigset_internal(sigset_t
*d
,
152 const target_sigset_t
*s
)
156 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
157 if (target_sigismember(s
, i
)) {
158 sigaddset(d
, target_to_host_signal(i
));
163 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
168 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
169 s1
.sig
[i
] = tswapal(s
->sig
[i
]);
170 target_to_host_sigset_internal(d
, &s1
);
173 void host_to_target_old_sigset(abi_ulong
*old_sigset
,
174 const sigset_t
*sigset
)
177 host_to_target_sigset(&d
, sigset
);
178 *old_sigset
= d
.sig
[0];
181 void target_to_host_old_sigset(sigset_t
*sigset
,
182 const abi_ulong
*old_sigset
)
187 d
.sig
[0] = *old_sigset
;
188 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
190 target_to_host_sigset(sigset
, &d
);
193 /* Wrapper for sigprocmask function
194 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
195 * are host signal set, not guest ones. This wraps the sigprocmask host calls
196 * that should be protected (calls originated from guest)
198 int do_sigprocmask(int how
, const sigset_t
*set
, sigset_t
*oldset
)
202 sigset_t
*temp
= NULL
;
203 CPUState
*cpu
= thread_cpu
;
204 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
205 bool segv_was_blocked
= ts
->sigsegv_blocked
;
208 bool has_sigsegv
= sigismember(set
, SIGSEGV
);
212 sigdelset(temp
, SIGSEGV
);
217 ts
->sigsegv_blocked
= true;
222 ts
->sigsegv_blocked
= false;
226 ts
->sigsegv_blocked
= has_sigsegv
;
229 g_assert_not_reached();
233 ret
= sigprocmask(how
, temp
, oldset
);
235 if (oldset
&& segv_was_blocked
) {
236 sigaddset(oldset
, SIGSEGV
);
242 /* siginfo conversion */
244 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
245 const siginfo_t
*info
)
247 int sig
= host_to_target_signal(info
->si_signo
);
248 tinfo
->si_signo
= sig
;
250 tinfo
->si_code
= info
->si_code
;
252 if (sig
== TARGET_SIGILL
|| sig
== TARGET_SIGFPE
|| sig
== TARGET_SIGSEGV
253 || sig
== TARGET_SIGBUS
|| sig
== TARGET_SIGTRAP
) {
254 /* Should never come here, but who knows. The information for
255 the target is irrelevant. */
256 tinfo
->_sifields
._sigfault
._addr
= 0;
257 } else if (sig
== TARGET_SIGIO
) {
258 tinfo
->_sifields
._sigpoll
._band
= info
->si_band
;
259 tinfo
->_sifields
._sigpoll
._fd
= info
->si_fd
;
260 } else if (sig
== TARGET_SIGCHLD
) {
261 tinfo
->_sifields
._sigchld
._pid
= info
->si_pid
;
262 tinfo
->_sifields
._sigchld
._uid
= info
->si_uid
;
263 tinfo
->_sifields
._sigchld
._status
264 = host_to_target_waitstatus(info
->si_status
);
265 tinfo
->_sifields
._sigchld
._utime
= info
->si_utime
;
266 tinfo
->_sifields
._sigchld
._stime
= info
->si_stime
;
267 } else if (sig
>= TARGET_SIGRTMIN
) {
268 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
269 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
270 /* XXX: potential problem if 64 bit */
271 tinfo
->_sifields
._rt
._sigval
.sival_ptr
272 = (abi_ulong
)(unsigned long)info
->si_value
.sival_ptr
;
276 static void tswap_siginfo(target_siginfo_t
*tinfo
,
277 const target_siginfo_t
*info
)
279 int sig
= info
->si_signo
;
280 tinfo
->si_signo
= tswap32(sig
);
281 tinfo
->si_errno
= tswap32(info
->si_errno
);
282 tinfo
->si_code
= tswap32(info
->si_code
);
284 if (sig
== TARGET_SIGILL
|| sig
== TARGET_SIGFPE
|| sig
== TARGET_SIGSEGV
285 || sig
== TARGET_SIGBUS
|| sig
== TARGET_SIGTRAP
) {
286 tinfo
->_sifields
._sigfault
._addr
287 = tswapal(info
->_sifields
._sigfault
._addr
);
288 } else if (sig
== TARGET_SIGIO
) {
289 tinfo
->_sifields
._sigpoll
._band
290 = tswap32(info
->_sifields
._sigpoll
._band
);
291 tinfo
->_sifields
._sigpoll
._fd
= tswap32(info
->_sifields
._sigpoll
._fd
);
292 } else if (sig
== TARGET_SIGCHLD
) {
293 tinfo
->_sifields
._sigchld
._pid
294 = tswap32(info
->_sifields
._sigchld
._pid
);
295 tinfo
->_sifields
._sigchld
._uid
296 = tswap32(info
->_sifields
._sigchld
._uid
);
297 tinfo
->_sifields
._sigchld
._status
298 = tswap32(info
->_sifields
._sigchld
._status
);
299 tinfo
->_sifields
._sigchld
._utime
300 = tswapal(info
->_sifields
._sigchld
._utime
);
301 tinfo
->_sifields
._sigchld
._stime
302 = tswapal(info
->_sifields
._sigchld
._stime
);
303 } else if (sig
>= TARGET_SIGRTMIN
) {
304 tinfo
->_sifields
._rt
._pid
= tswap32(info
->_sifields
._rt
._pid
);
305 tinfo
->_sifields
._rt
._uid
= tswap32(info
->_sifields
._rt
._uid
);
306 tinfo
->_sifields
._rt
._sigval
.sival_ptr
307 = tswapal(info
->_sifields
._rt
._sigval
.sival_ptr
);
312 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
314 host_to_target_siginfo_noswap(tinfo
, info
);
315 tswap_siginfo(tinfo
, tinfo
);
318 /* XXX: we support only POSIX RT signals are used. */
319 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
320 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
322 info
->si_signo
= tswap32(tinfo
->si_signo
);
323 info
->si_errno
= tswap32(tinfo
->si_errno
);
324 info
->si_code
= tswap32(tinfo
->si_code
);
325 info
->si_pid
= tswap32(tinfo
->_sifields
._rt
._pid
);
326 info
->si_uid
= tswap32(tinfo
->_sifields
._rt
._uid
);
327 info
->si_value
.sival_ptr
=
328 (void *)(long)tswapal(tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
331 static int fatal_signal (int sig
)
336 case TARGET_SIGWINCH
:
337 /* Ignored by default. */
344 /* Job control signals. */
351 /* returns 1 if given signal should dump core if not handled */
352 static int core_dump_signal(int sig
)
368 void signal_init(void)
370 struct sigaction act
;
371 struct sigaction oact
;
375 /* generate signal conversion tables */
376 for(i
= 1; i
< _NSIG
; i
++) {
377 if (host_to_target_signal_table
[i
] == 0)
378 host_to_target_signal_table
[i
] = i
;
380 for(i
= 1; i
< _NSIG
; i
++) {
381 j
= host_to_target_signal_table
[i
];
382 target_to_host_signal_table
[j
] = i
;
385 /* set all host signal handlers. ALL signals are blocked during
386 the handlers to serialize them. */
387 memset(sigact_table
, 0, sizeof(sigact_table
));
389 sigfillset(&act
.sa_mask
);
390 act
.sa_flags
= SA_SIGINFO
;
391 act
.sa_sigaction
= host_signal_handler
;
392 for(i
= 1; i
<= TARGET_NSIG
; i
++) {
393 host_sig
= target_to_host_signal(i
);
394 sigaction(host_sig
, NULL
, &oact
);
395 if (oact
.sa_sigaction
== (void *)SIG_IGN
) {
396 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_IGN
;
397 } else if (oact
.sa_sigaction
== (void *)SIG_DFL
) {
398 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_DFL
;
400 /* If there's already a handler installed then something has
401 gone horribly wrong, so don't even try to handle that case. */
402 /* Install some handlers for our own use. We need at least
403 SIGSEGV and SIGBUS, to detect exceptions. We can not just
404 trap all signals because it affects syscall interrupt
405 behavior. But do trap all default-fatal signals. */
406 if (fatal_signal (i
))
407 sigaction(host_sig
, &act
, NULL
);
411 /* signal queue handling */
413 static inline struct sigqueue
*alloc_sigqueue(CPUArchState
*env
)
415 CPUState
*cpu
= ENV_GET_CPU(env
);
416 TaskState
*ts
= cpu
->opaque
;
417 struct sigqueue
*q
= ts
->first_free
;
420 ts
->first_free
= q
->next
;
424 static inline void free_sigqueue(CPUArchState
*env
, struct sigqueue
*q
)
426 CPUState
*cpu
= ENV_GET_CPU(env
);
427 TaskState
*ts
= cpu
->opaque
;
429 q
->next
= ts
->first_free
;
433 /* abort execution with signal */
434 static void QEMU_NORETURN
force_sig(int target_sig
)
436 CPUState
*cpu
= thread_cpu
;
437 CPUArchState
*env
= cpu
->env_ptr
;
438 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
439 int host_sig
, core_dumped
= 0;
440 struct sigaction act
;
442 host_sig
= target_to_host_signal(target_sig
);
443 trace_user_force_sig(env
, target_sig
, host_sig
);
444 gdb_signalled(env
, target_sig
);
446 /* dump core if supported by target binary format */
447 if (core_dump_signal(target_sig
) && (ts
->bprm
->core_dump
!= NULL
)) {
450 ((*ts
->bprm
->core_dump
)(target_sig
, env
) == 0);
453 /* we already dumped the core of target process, we don't want
454 * a coredump of qemu itself */
455 struct rlimit nodump
;
456 getrlimit(RLIMIT_CORE
, &nodump
);
458 setrlimit(RLIMIT_CORE
, &nodump
);
459 (void) fprintf(stderr
, "qemu: uncaught target signal %d (%s) - %s\n",
460 target_sig
, strsignal(host_sig
), "core dumped" );
463 /* The proper exit code for dying from an uncaught signal is
464 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
465 * a negative value. To get the proper exit code we need to
466 * actually die from an uncaught signal. Here the default signal
467 * handler is installed, we send ourself a signal and we wait for
469 sigfillset(&act
.sa_mask
);
470 act
.sa_handler
= SIG_DFL
;
472 sigaction(host_sig
, &act
, NULL
);
474 /* For some reason raise(host_sig) doesn't send the signal when
475 * statically linked on x86-64. */
476 kill(getpid(), host_sig
);
478 /* Make sure the signal isn't masked (just reuse the mask inside
480 sigdelset(&act
.sa_mask
, host_sig
);
481 sigsuspend(&act
.sa_mask
);
487 /* queue a signal so that it will be send to the virtual CPU as soon
489 int queue_signal(CPUArchState
*env
, int sig
, target_siginfo_t
*info
)
491 CPUState
*cpu
= ENV_GET_CPU(env
);
492 TaskState
*ts
= cpu
->opaque
;
493 struct emulated_sigtable
*k
;
494 struct sigqueue
*q
, **pq
;
498 trace_user_queue_signal(env
, sig
);
499 k
= &ts
->sigtab
[sig
- 1];
500 queue
= gdb_queuesig ();
501 handler
= sigact_table
[sig
- 1]._sa_handler
;
503 if (ts
->sigsegv_blocked
&& sig
== TARGET_SIGSEGV
) {
504 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
505 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
506 * because it got a real MMU fault). A blocked SIGSEGV in that
507 * situation is treated as if using the default handler. This is
508 * not correct if some other process has randomly sent us a SIGSEGV
509 * via kill(), but that is not easy to distinguish at this point,
510 * so we assume it doesn't happen.
512 handler
= TARGET_SIG_DFL
;
515 if (!queue
&& handler
== TARGET_SIG_DFL
) {
516 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
517 kill(getpid(),SIGSTOP
);
520 /* default handler : ignore some signal. The other are fatal */
521 if (sig
!= TARGET_SIGCHLD
&&
522 sig
!= TARGET_SIGURG
&&
523 sig
!= TARGET_SIGWINCH
&&
524 sig
!= TARGET_SIGCONT
) {
527 return 0; /* indicate ignored */
529 } else if (!queue
&& handler
== TARGET_SIG_IGN
) {
532 } else if (!queue
&& handler
== TARGET_SIG_ERR
) {
536 if (sig
< TARGET_SIGRTMIN
) {
537 /* if non real time signal, we queue exactly one signal */
547 q
= alloc_sigqueue(env
);
558 /* signal that a new signal is pending */
559 ts
->signal_pending
= 1;
560 return 1; /* indicates that the signal was queued */
564 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
567 CPUArchState
*env
= thread_cpu
->env_ptr
;
569 target_siginfo_t tinfo
;
571 /* the CPU emulator uses some host signals to detect exceptions,
572 we forward to it some signals */
573 if ((host_signum
== SIGSEGV
|| host_signum
== SIGBUS
)
574 && info
->si_code
> 0) {
575 if (cpu_signal_handler(host_signum
, info
, puc
))
579 /* get target signal number */
580 sig
= host_to_target_signal(host_signum
);
581 if (sig
< 1 || sig
> TARGET_NSIG
)
583 trace_user_host_signal(env
, host_signum
, sig
);
584 host_to_target_siginfo_noswap(&tinfo
, info
);
585 if (queue_signal(env
, sig
, &tinfo
) == 1) {
586 /* interrupt the virtual CPU as soon as possible */
587 cpu_exit(thread_cpu
);
591 /* do_sigaltstack() returns target values and errnos. */
592 /* compare linux/kernel/signal.c:do_sigaltstack() */
593 abi_long
do_sigaltstack(abi_ulong uss_addr
, abi_ulong uoss_addr
, abi_ulong sp
)
596 struct target_sigaltstack oss
;
598 /* XXX: test errors */
601 __put_user(target_sigaltstack_used
.ss_sp
, &oss
.ss_sp
);
602 __put_user(target_sigaltstack_used
.ss_size
, &oss
.ss_size
);
603 __put_user(sas_ss_flags(sp
), &oss
.ss_flags
);
608 struct target_sigaltstack
*uss
;
609 struct target_sigaltstack ss
;
610 size_t minstacksize
= TARGET_MINSIGSTKSZ
;
612 #if defined(TARGET_PPC64)
613 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
614 struct image_info
*image
= ((TaskState
*)thread_cpu
->opaque
)->info
;
615 if (get_ppc64_abi(image
) > 1) {
620 ret
= -TARGET_EFAULT
;
621 if (!lock_user_struct(VERIFY_READ
, uss
, uss_addr
, 1)) {
624 __get_user(ss
.ss_sp
, &uss
->ss_sp
);
625 __get_user(ss
.ss_size
, &uss
->ss_size
);
626 __get_user(ss
.ss_flags
, &uss
->ss_flags
);
627 unlock_user_struct(uss
, uss_addr
, 0);
630 if (on_sig_stack(sp
))
633 ret
= -TARGET_EINVAL
;
634 if (ss
.ss_flags
!= TARGET_SS_DISABLE
635 && ss
.ss_flags
!= TARGET_SS_ONSTACK
639 if (ss
.ss_flags
== TARGET_SS_DISABLE
) {
643 ret
= -TARGET_ENOMEM
;
644 if (ss
.ss_size
< minstacksize
) {
649 target_sigaltstack_used
.ss_sp
= ss
.ss_sp
;
650 target_sigaltstack_used
.ss_size
= ss
.ss_size
;
654 ret
= -TARGET_EFAULT
;
655 if (copy_to_user(uoss_addr
, &oss
, sizeof(oss
)))
664 /* do_sigaction() return host values and errnos */
665 int do_sigaction(int sig
, const struct target_sigaction
*act
,
666 struct target_sigaction
*oact
)
668 struct target_sigaction
*k
;
669 struct sigaction act1
;
673 if (sig
< 1 || sig
> TARGET_NSIG
|| sig
== TARGET_SIGKILL
|| sig
== TARGET_SIGSTOP
)
675 k
= &sigact_table
[sig
- 1];
677 __put_user(k
->_sa_handler
, &oact
->_sa_handler
);
678 __put_user(k
->sa_flags
, &oact
->sa_flags
);
679 #if !defined(TARGET_MIPS)
680 __put_user(k
->sa_restorer
, &oact
->sa_restorer
);
683 oact
->sa_mask
= k
->sa_mask
;
686 /* FIXME: This is not threadsafe. */
687 __get_user(k
->_sa_handler
, &act
->_sa_handler
);
688 __get_user(k
->sa_flags
, &act
->sa_flags
);
689 #if !defined(TARGET_MIPS)
690 __get_user(k
->sa_restorer
, &act
->sa_restorer
);
692 /* To be swapped in target_to_host_sigset. */
693 k
->sa_mask
= act
->sa_mask
;
695 /* we update the host linux signal state */
696 host_sig
= target_to_host_signal(sig
);
697 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
698 sigfillset(&act1
.sa_mask
);
699 act1
.sa_flags
= SA_SIGINFO
;
700 if (k
->sa_flags
& TARGET_SA_RESTART
)
701 act1
.sa_flags
|= SA_RESTART
;
702 /* NOTE: it is important to update the host kernel signal
703 ignore state to avoid getting unexpected interrupted
705 if (k
->_sa_handler
== TARGET_SIG_IGN
) {
706 act1
.sa_sigaction
= (void *)SIG_IGN
;
707 } else if (k
->_sa_handler
== TARGET_SIG_DFL
) {
708 if (fatal_signal (sig
))
709 act1
.sa_sigaction
= host_signal_handler
;
711 act1
.sa_sigaction
= (void *)SIG_DFL
;
713 act1
.sa_sigaction
= host_signal_handler
;
715 ret
= sigaction(host_sig
, &act1
, NULL
);
721 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
723 /* from the Linux kernel */
725 struct target_fpreg
{
726 uint16_t significand
[4];
730 struct target_fpxreg
{
731 uint16_t significand
[4];
736 struct target_xmmreg
{
737 abi_ulong element
[4];
740 struct target_fpstate
{
741 /* Regular FPU environment */
749 struct target_fpreg _st
[8];
751 uint16_t magic
; /* 0xffff = regular FPU data only */
753 /* FXSR FPU environment */
754 abi_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
757 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
758 struct target_xmmreg _xmm
[8];
759 abi_ulong padding
[56];
762 #define X86_FXSR_MAGIC 0x0000
764 struct target_sigcontext
{
782 abi_ulong esp_at_signal
;
784 abi_ulong fpstate
; /* pointer */
789 struct target_ucontext
{
792 target_stack_t tuc_stack
;
793 struct target_sigcontext tuc_mcontext
;
794 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
801 struct target_sigcontext sc
;
802 struct target_fpstate fpstate
;
803 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
813 struct target_siginfo info
;
814 struct target_ucontext uc
;
815 struct target_fpstate fpstate
;
820 * Set up a signal frame.
823 /* XXX: save x87 state */
824 static void setup_sigcontext(struct target_sigcontext
*sc
,
825 struct target_fpstate
*fpstate
, CPUX86State
*env
, abi_ulong mask
,
826 abi_ulong fpstate_addr
)
828 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
831 /* already locked in setup_frame() */
832 __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
833 __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
834 __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
835 __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
836 __put_user(env
->regs
[R_EDI
], &sc
->edi
);
837 __put_user(env
->regs
[R_ESI
], &sc
->esi
);
838 __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
839 __put_user(env
->regs
[R_ESP
], &sc
->esp
);
840 __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
841 __put_user(env
->regs
[R_EDX
], &sc
->edx
);
842 __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
843 __put_user(env
->regs
[R_EAX
], &sc
->eax
);
844 __put_user(cs
->exception_index
, &sc
->trapno
);
845 __put_user(env
->error_code
, &sc
->err
);
846 __put_user(env
->eip
, &sc
->eip
);
847 __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
848 __put_user(env
->eflags
, &sc
->eflags
);
849 __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
850 __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
852 cpu_x86_fsave(env
, fpstate_addr
, 1);
853 fpstate
->status
= fpstate
->sw
;
855 __put_user(magic
, &fpstate
->magic
);
856 __put_user(fpstate_addr
, &sc
->fpstate
);
858 /* non-iBCS2 extensions.. */
859 __put_user(mask
, &sc
->oldmask
);
860 __put_user(env
->cr
[2], &sc
->cr2
);
864 * Determine which stack to use..
867 static inline abi_ulong
868 get_sigframe(struct target_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
872 /* Default to using normal stack */
873 esp
= env
->regs
[R_ESP
];
874 /* This is the X/Open sanctioned signal stack switching. */
875 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
876 if (sas_ss_flags(esp
) == 0) {
877 esp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
881 /* This is the legacy signal stack switching. */
882 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
883 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
885 esp
= (unsigned long) ka
->sa_restorer
;
888 return (esp
- frame_size
) & -8ul;
891 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
892 static void setup_frame(int sig
, struct target_sigaction
*ka
,
893 target_sigset_t
*set
, CPUX86State
*env
)
895 abi_ulong frame_addr
;
896 struct sigframe
*frame
;
899 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
900 trace_user_setup_frame(env
, frame_addr
);
902 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
905 __put_user(sig
, &frame
->sig
);
907 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0],
908 frame_addr
+ offsetof(struct sigframe
, fpstate
));
910 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
911 __put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]);
914 /* Set up to return from userspace. If provided, use a stub
915 already in userspace. */
916 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
917 __put_user(ka
->sa_restorer
, &frame
->pretcode
);
920 abi_ulong retcode_addr
;
921 retcode_addr
= frame_addr
+ offsetof(struct sigframe
, retcode
);
922 __put_user(retcode_addr
, &frame
->pretcode
);
923 /* This is popl %eax ; movl $,%eax ; int $0x80 */
925 __put_user(val16
, (uint16_t *)(frame
->retcode
+0));
926 __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
928 __put_user(val16
, (uint16_t *)(frame
->retcode
+6));
932 /* Set up registers for signal handler */
933 env
->regs
[R_ESP
] = frame_addr
;
934 env
->eip
= ka
->_sa_handler
;
936 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
937 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
938 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
939 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
940 env
->eflags
&= ~TF_MASK
;
942 unlock_user_struct(frame
, frame_addr
, 1);
947 if (sig
== TARGET_SIGSEGV
) {
948 ka
->_sa_handler
= TARGET_SIG_DFL
;
950 force_sig(TARGET_SIGSEGV
/* , current */);
953 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
954 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
955 target_siginfo_t
*info
,
956 target_sigset_t
*set
, CPUX86State
*env
)
958 abi_ulong frame_addr
, addr
;
959 struct rt_sigframe
*frame
;
962 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
963 trace_user_setup_rt_frame(env
, frame_addr
);
965 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
968 __put_user(sig
, &frame
->sig
);
969 addr
= frame_addr
+ offsetof(struct rt_sigframe
, info
);
970 __put_user(addr
, &frame
->pinfo
);
971 addr
= frame_addr
+ offsetof(struct rt_sigframe
, uc
);
972 __put_user(addr
, &frame
->puc
);
973 tswap_siginfo(&frame
->info
, info
);
975 /* Create the ucontext. */
976 __put_user(0, &frame
->uc
.tuc_flags
);
977 __put_user(0, &frame
->uc
.tuc_link
);
978 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->uc
.tuc_stack
.ss_sp
);
979 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
980 &frame
->uc
.tuc_stack
.ss_flags
);
981 __put_user(target_sigaltstack_used
.ss_size
,
982 &frame
->uc
.tuc_stack
.ss_size
);
983 setup_sigcontext(&frame
->uc
.tuc_mcontext
, &frame
->fpstate
, env
,
984 set
->sig
[0], frame_addr
+ offsetof(struct rt_sigframe
, fpstate
));
986 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
987 __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
990 /* Set up to return from userspace. If provided, use a stub
991 already in userspace. */
992 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
993 __put_user(ka
->sa_restorer
, &frame
->pretcode
);
996 addr
= frame_addr
+ offsetof(struct rt_sigframe
, retcode
);
997 __put_user(addr
, &frame
->pretcode
);
998 /* This is movl $,%eax ; int $0x80 */
999 __put_user(0xb8, (char *)(frame
->retcode
+0));
1000 __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
1002 __put_user(val16
, (uint16_t *)(frame
->retcode
+5));
1005 /* Set up registers for signal handler */
1006 env
->regs
[R_ESP
] = frame_addr
;
1007 env
->eip
= ka
->_sa_handler
;
1009 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1010 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1011 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1012 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1013 env
->eflags
&= ~TF_MASK
;
1015 unlock_user_struct(frame
, frame_addr
, 1);
1020 if (sig
== TARGET_SIGSEGV
) {
1021 ka
->_sa_handler
= TARGET_SIG_DFL
;
1023 force_sig(TARGET_SIGSEGV
/* , current */);
1027 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
)
1029 unsigned int err
= 0;
1030 abi_ulong fpstate_addr
;
1031 unsigned int tmpflags
;
1033 cpu_x86_load_seg(env
, R_GS
, tswap16(sc
->gs
));
1034 cpu_x86_load_seg(env
, R_FS
, tswap16(sc
->fs
));
1035 cpu_x86_load_seg(env
, R_ES
, tswap16(sc
->es
));
1036 cpu_x86_load_seg(env
, R_DS
, tswap16(sc
->ds
));
1038 env
->regs
[R_EDI
] = tswapl(sc
->edi
);
1039 env
->regs
[R_ESI
] = tswapl(sc
->esi
);
1040 env
->regs
[R_EBP
] = tswapl(sc
->ebp
);
1041 env
->regs
[R_ESP
] = tswapl(sc
->esp
);
1042 env
->regs
[R_EBX
] = tswapl(sc
->ebx
);
1043 env
->regs
[R_EDX
] = tswapl(sc
->edx
);
1044 env
->regs
[R_ECX
] = tswapl(sc
->ecx
);
1045 env
->regs
[R_EAX
] = tswapl(sc
->eax
);
1046 env
->eip
= tswapl(sc
->eip
);
1048 cpu_x86_load_seg(env
, R_CS
, lduw_p(&sc
->cs
) | 3);
1049 cpu_x86_load_seg(env
, R_SS
, lduw_p(&sc
->ss
) | 3);
1051 tmpflags
= tswapl(sc
->eflags
);
1052 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
1053 // regs->orig_eax = -1; /* disable syscall checks */
1055 fpstate_addr
= tswapl(sc
->fpstate
);
1056 if (fpstate_addr
!= 0) {
1057 if (!access_ok(VERIFY_READ
, fpstate_addr
,
1058 sizeof(struct target_fpstate
)))
1060 cpu_x86_frstor(env
, fpstate_addr
, 1);
1068 long do_sigreturn(CPUX86State
*env
)
1070 struct sigframe
*frame
;
1071 abi_ulong frame_addr
= env
->regs
[R_ESP
] - 8;
1072 target_sigset_t target_set
;
1076 trace_user_do_sigreturn(env
, frame_addr
);
1077 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1079 /* set blocked signals */
1080 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
);
1081 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1082 __get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]);
1085 target_to_host_sigset_internal(&set
, &target_set
);
1086 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
1088 /* restore registers */
1089 if (restore_sigcontext(env
, &frame
->sc
))
1091 unlock_user_struct(frame
, frame_addr
, 0);
1092 return -TARGET_QEMU_ESIGRETURN
;
1095 unlock_user_struct(frame
, frame_addr
, 0);
1096 force_sig(TARGET_SIGSEGV
);
1100 long do_rt_sigreturn(CPUX86State
*env
)
1102 abi_ulong frame_addr
;
1103 struct rt_sigframe
*frame
;
1106 frame_addr
= env
->regs
[R_ESP
] - 4;
1107 trace_user_do_rt_sigreturn(env
, frame_addr
);
1108 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1110 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
1111 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
1113 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
)) {
1117 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe
, uc
.tuc_stack
), 0,
1118 get_sp_from_cpustate(env
)) == -EFAULT
) {
1122 unlock_user_struct(frame
, frame_addr
, 0);
1123 return -TARGET_QEMU_ESIGRETURN
;
1126 unlock_user_struct(frame
, frame_addr
, 0);
1127 force_sig(TARGET_SIGSEGV
);
1131 #elif defined(TARGET_AARCH64)
1133 struct target_sigcontext
{
1134 uint64_t fault_address
;
1135 /* AArch64 registers */
1140 /* 4K reserved for FP/SIMD state and future expansion */
1141 char __reserved
[4096] __attribute__((__aligned__(16)));
1144 struct target_ucontext
{
1145 abi_ulong tuc_flags
;
1147 target_stack_t tuc_stack
;
1148 target_sigset_t tuc_sigmask
;
1149 /* glibc uses a 1024-bit sigset_t */
1150 char __unused
[1024 / 8 - sizeof(target_sigset_t
)];
1151 /* last for future expansion */
1152 struct target_sigcontext tuc_mcontext
;
1156 * Header to be used at the beginning of structures extending the user
1157 * context. Such structures must be placed after the rt_sigframe on the stack
1158 * and be 16-byte aligned. The last structure must be a dummy one with the
1159 * magic and size set to 0.
1161 struct target_aarch64_ctx
{
1166 #define TARGET_FPSIMD_MAGIC 0x46508001
1168 struct target_fpsimd_context
{
1169 struct target_aarch64_ctx head
;
1172 uint64_t vregs
[32 * 2]; /* really uint128_t vregs[32] */
1176 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1177 * user space as it will change with the addition of new context. User space
1178 * should check the magic/size information.
1180 struct target_aux_context
{
1181 struct target_fpsimd_context fpsimd
;
1182 /* additional context to be added before "end" */
1183 struct target_aarch64_ctx end
;
1186 struct target_rt_sigframe
{
1187 struct target_siginfo info
;
1188 struct target_ucontext uc
;
1194 static int target_setup_sigframe(struct target_rt_sigframe
*sf
,
1195 CPUARMState
*env
, target_sigset_t
*set
)
1198 struct target_aux_context
*aux
=
1199 (struct target_aux_context
*)sf
->uc
.tuc_mcontext
.__reserved
;
1201 /* set up the stack frame for unwinding */
1202 __put_user(env
->xregs
[29], &sf
->fp
);
1203 __put_user(env
->xregs
[30], &sf
->lr
);
1205 for (i
= 0; i
< 31; i
++) {
1206 __put_user(env
->xregs
[i
], &sf
->uc
.tuc_mcontext
.regs
[i
]);
1208 __put_user(env
->xregs
[31], &sf
->uc
.tuc_mcontext
.sp
);
1209 __put_user(env
->pc
, &sf
->uc
.tuc_mcontext
.pc
);
1210 __put_user(pstate_read(env
), &sf
->uc
.tuc_mcontext
.pstate
);
1212 __put_user(env
->exception
.vaddress
, &sf
->uc
.tuc_mcontext
.fault_address
);
1214 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1215 __put_user(set
->sig
[i
], &sf
->uc
.tuc_sigmask
.sig
[i
]);
1218 for (i
= 0; i
< 32; i
++) {
1219 #ifdef TARGET_WORDS_BIGENDIAN
1220 __put_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1221 __put_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2]);
1223 __put_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2]);
1224 __put_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1227 __put_user(vfp_get_fpsr(env
), &aux
->fpsimd
.fpsr
);
1228 __put_user(vfp_get_fpcr(env
), &aux
->fpsimd
.fpcr
);
1229 __put_user(TARGET_FPSIMD_MAGIC
, &aux
->fpsimd
.head
.magic
);
1230 __put_user(sizeof(struct target_fpsimd_context
),
1231 &aux
->fpsimd
.head
.size
);
1233 /* set the "end" magic */
1234 __put_user(0, &aux
->end
.magic
);
1235 __put_user(0, &aux
->end
.size
);
1240 static int target_restore_sigframe(CPUARMState
*env
,
1241 struct target_rt_sigframe
*sf
)
1245 struct target_aux_context
*aux
=
1246 (struct target_aux_context
*)sf
->uc
.tuc_mcontext
.__reserved
;
1247 uint32_t magic
, size
, fpsr
, fpcr
;
1250 target_to_host_sigset(&set
, &sf
->uc
.tuc_sigmask
);
1251 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
1253 for (i
= 0; i
< 31; i
++) {
1254 __get_user(env
->xregs
[i
], &sf
->uc
.tuc_mcontext
.regs
[i
]);
1257 __get_user(env
->xregs
[31], &sf
->uc
.tuc_mcontext
.sp
);
1258 __get_user(env
->pc
, &sf
->uc
.tuc_mcontext
.pc
);
1259 __get_user(pstate
, &sf
->uc
.tuc_mcontext
.pstate
);
1260 pstate_write(env
, pstate
);
1262 __get_user(magic
, &aux
->fpsimd
.head
.magic
);
1263 __get_user(size
, &aux
->fpsimd
.head
.size
);
1265 if (magic
!= TARGET_FPSIMD_MAGIC
1266 || size
!= sizeof(struct target_fpsimd_context
)) {
1270 for (i
= 0; i
< 32; i
++) {
1271 #ifdef TARGET_WORDS_BIGENDIAN
1272 __get_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1273 __get_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2]);
1275 __get_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2]);
1276 __get_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1279 __get_user(fpsr
, &aux
->fpsimd
.fpsr
);
1280 vfp_set_fpsr(env
, fpsr
);
1281 __get_user(fpcr
, &aux
->fpsimd
.fpcr
);
1282 vfp_set_fpcr(env
, fpcr
);
1287 static abi_ulong
get_sigframe(struct target_sigaction
*ka
, CPUARMState
*env
)
1291 sp
= env
->xregs
[31];
1294 * This is the X/Open sanctioned signal stack switching.
1296 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
)) {
1297 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1300 sp
= (sp
- sizeof(struct target_rt_sigframe
)) & ~15;
1305 static void target_setup_frame(int usig
, struct target_sigaction
*ka
,
1306 target_siginfo_t
*info
, target_sigset_t
*set
,
1309 struct target_rt_sigframe
*frame
;
1310 abi_ulong frame_addr
, return_addr
;
1312 frame_addr
= get_sigframe(ka
, env
);
1313 trace_user_setup_frame(env
, frame_addr
);
1314 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
1318 __put_user(0, &frame
->uc
.tuc_flags
);
1319 __put_user(0, &frame
->uc
.tuc_link
);
1321 __put_user(target_sigaltstack_used
.ss_sp
,
1322 &frame
->uc
.tuc_stack
.ss_sp
);
1323 __put_user(sas_ss_flags(env
->xregs
[31]),
1324 &frame
->uc
.tuc_stack
.ss_flags
);
1325 __put_user(target_sigaltstack_used
.ss_size
,
1326 &frame
->uc
.tuc_stack
.ss_size
);
1327 target_setup_sigframe(frame
, env
, set
);
1328 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1329 return_addr
= ka
->sa_restorer
;
1331 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1332 __put_user(0xd2801168, &frame
->tramp
[0]);
1333 __put_user(0xd4000001, &frame
->tramp
[1]);
1334 return_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, tramp
);
1336 env
->xregs
[0] = usig
;
1337 env
->xregs
[31] = frame_addr
;
1338 env
->xregs
[29] = env
->xregs
[31] + offsetof(struct target_rt_sigframe
, fp
);
1339 env
->pc
= ka
->_sa_handler
;
1340 env
->xregs
[30] = return_addr
;
1342 tswap_siginfo(&frame
->info
, info
);
1343 env
->xregs
[1] = frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
1344 env
->xregs
[2] = frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
1347 unlock_user_struct(frame
, frame_addr
, 1);
1351 unlock_user_struct(frame
, frame_addr
, 1);
1352 force_sig(TARGET_SIGSEGV
);
1355 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
1356 target_siginfo_t
*info
, target_sigset_t
*set
,
1359 target_setup_frame(sig
, ka
, info
, set
, env
);
1362 static void setup_frame(int sig
, struct target_sigaction
*ka
,
1363 target_sigset_t
*set
, CPUARMState
*env
)
1365 target_setup_frame(sig
, ka
, 0, set
, env
);
1368 long do_rt_sigreturn(CPUARMState
*env
)
1370 struct target_rt_sigframe
*frame
= NULL
;
1371 abi_ulong frame_addr
= env
->xregs
[31];
1373 trace_user_do_rt_sigreturn(env
, frame_addr
);
1374 if (frame_addr
& 15) {
1378 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
1382 if (target_restore_sigframe(env
, frame
)) {
1386 if (do_sigaltstack(frame_addr
+
1387 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
1388 0, get_sp_from_cpustate(env
)) == -EFAULT
) {
1392 unlock_user_struct(frame
, frame_addr
, 0);
1393 return -TARGET_QEMU_ESIGRETURN
;
1396 unlock_user_struct(frame
, frame_addr
, 0);
1397 force_sig(TARGET_SIGSEGV
);
1401 long do_sigreturn(CPUARMState
*env
)
1403 return do_rt_sigreturn(env
);
1406 #elif defined(TARGET_ARM)
1408 struct target_sigcontext
{
1410 abi_ulong error_code
;
1429 abi_ulong fault_address
;
1432 struct target_ucontext_v1
{
1433 abi_ulong tuc_flags
;
1435 target_stack_t tuc_stack
;
1436 struct target_sigcontext tuc_mcontext
;
1437 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1440 struct target_ucontext_v2
{
1441 abi_ulong tuc_flags
;
1443 target_stack_t tuc_stack
;
1444 struct target_sigcontext tuc_mcontext
;
1445 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1446 char __unused
[128 - sizeof(target_sigset_t
)];
1447 abi_ulong tuc_regspace
[128] __attribute__((__aligned__(8)));
1450 struct target_user_vfp
{
1451 uint64_t fpregs
[32];
1455 struct target_user_vfp_exc
{
1461 struct target_vfp_sigframe
{
1464 struct target_user_vfp ufp
;
1465 struct target_user_vfp_exc ufp_exc
;
1466 } __attribute__((__aligned__(8)));
1468 struct target_iwmmxt_sigframe
{
1472 /* Note that not all the coprocessor control registers are stored here */
1479 } __attribute__((__aligned__(8)));
1481 #define TARGET_VFP_MAGIC 0x56465001
1482 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1486 struct target_sigcontext sc
;
1487 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
1493 struct target_ucontext_v2 uc
;
1497 struct rt_sigframe_v1
1501 struct target_siginfo info
;
1502 struct target_ucontext_v1 uc
;
1506 struct rt_sigframe_v2
1508 struct target_siginfo info
;
1509 struct target_ucontext_v2 uc
;
1513 #define TARGET_CONFIG_CPU_32 1
1516 * For ARM syscalls, we encode the syscall number into the instruction.
1518 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1519 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1522 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1523 * need two 16-bit instructions.
1525 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1526 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1528 static const abi_ulong retcodes
[4] = {
1529 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
1530 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
1534 static inline int valid_user_regs(CPUARMState
*regs
)
1540 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1541 CPUARMState
*env
, abi_ulong mask
)
1543 __put_user(env
->regs
[0], &sc
->arm_r0
);
1544 __put_user(env
->regs
[1], &sc
->arm_r1
);
1545 __put_user(env
->regs
[2], &sc
->arm_r2
);
1546 __put_user(env
->regs
[3], &sc
->arm_r3
);
1547 __put_user(env
->regs
[4], &sc
->arm_r4
);
1548 __put_user(env
->regs
[5], &sc
->arm_r5
);
1549 __put_user(env
->regs
[6], &sc
->arm_r6
);
1550 __put_user(env
->regs
[7], &sc
->arm_r7
);
1551 __put_user(env
->regs
[8], &sc
->arm_r8
);
1552 __put_user(env
->regs
[9], &sc
->arm_r9
);
1553 __put_user(env
->regs
[10], &sc
->arm_r10
);
1554 __put_user(env
->regs
[11], &sc
->arm_fp
);
1555 __put_user(env
->regs
[12], &sc
->arm_ip
);
1556 __put_user(env
->regs
[13], &sc
->arm_sp
);
1557 __put_user(env
->regs
[14], &sc
->arm_lr
);
1558 __put_user(env
->regs
[15], &sc
->arm_pc
);
1559 #ifdef TARGET_CONFIG_CPU_32
1560 __put_user(cpsr_read(env
), &sc
->arm_cpsr
);
1563 __put_user(/* current->thread.trap_no */ 0, &sc
->trap_no
);
1564 __put_user(/* current->thread.error_code */ 0, &sc
->error_code
);
1565 __put_user(/* current->thread.address */ 0, &sc
->fault_address
);
1566 __put_user(mask
, &sc
->oldmask
);
1569 static inline abi_ulong
1570 get_sigframe(struct target_sigaction
*ka
, CPUARMState
*regs
, int framesize
)
1572 unsigned long sp
= regs
->regs
[13];
1575 * This is the X/Open sanctioned signal stack switching.
1577 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
)) {
1578 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1581 * ATPCS B01 mandates 8-byte alignment
1583 return (sp
- framesize
) & ~7;
1587 setup_return(CPUARMState
*env
, struct target_sigaction
*ka
,
1588 abi_ulong
*rc
, abi_ulong frame_addr
, int usig
, abi_ulong rc_addr
)
1590 abi_ulong handler
= ka
->_sa_handler
;
1592 int thumb
= handler
& 1;
1593 uint32_t cpsr
= cpsr_read(env
);
1602 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1603 retcode
= ka
->sa_restorer
;
1605 unsigned int idx
= thumb
;
1607 if (ka
->sa_flags
& TARGET_SA_SIGINFO
) {
1611 __put_user(retcodes
[idx
], rc
);
1613 retcode
= rc_addr
+ thumb
;
1616 env
->regs
[0] = usig
;
1617 env
->regs
[13] = frame_addr
;
1618 env
->regs
[14] = retcode
;
1619 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
1620 cpsr_write(env
, cpsr
, CPSR_IT
| CPSR_T
, CPSRWriteByInstr
);
1623 static abi_ulong
*setup_sigframe_v2_vfp(abi_ulong
*regspace
, CPUARMState
*env
)
1626 struct target_vfp_sigframe
*vfpframe
;
1627 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
1628 __put_user(TARGET_VFP_MAGIC
, &vfpframe
->magic
);
1629 __put_user(sizeof(*vfpframe
), &vfpframe
->size
);
1630 for (i
= 0; i
< 32; i
++) {
1631 __put_user(float64_val(env
->vfp
.regs
[i
]), &vfpframe
->ufp
.fpregs
[i
]);
1633 __put_user(vfp_get_fpscr(env
), &vfpframe
->ufp
.fpscr
);
1634 __put_user(env
->vfp
.xregs
[ARM_VFP_FPEXC
], &vfpframe
->ufp_exc
.fpexc
);
1635 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
1636 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
1637 return (abi_ulong
*)(vfpframe
+1);
1640 static abi_ulong
*setup_sigframe_v2_iwmmxt(abi_ulong
*regspace
,
1644 struct target_iwmmxt_sigframe
*iwmmxtframe
;
1645 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
1646 __put_user(TARGET_IWMMXT_MAGIC
, &iwmmxtframe
->magic
);
1647 __put_user(sizeof(*iwmmxtframe
), &iwmmxtframe
->size
);
1648 for (i
= 0; i
< 16; i
++) {
1649 __put_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
1651 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
1652 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
1653 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
1654 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
1655 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
1656 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
1657 return (abi_ulong
*)(iwmmxtframe
+1);
1660 static void setup_sigframe_v2(struct target_ucontext_v2
*uc
,
1661 target_sigset_t
*set
, CPUARMState
*env
)
1663 struct target_sigaltstack stack
;
1665 abi_ulong
*regspace
;
1667 /* Clear all the bits of the ucontext we don't use. */
1668 memset(uc
, 0, offsetof(struct target_ucontext_v2
, tuc_mcontext
));
1670 memset(&stack
, 0, sizeof(stack
));
1671 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1672 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1673 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1674 memcpy(&uc
->tuc_stack
, &stack
, sizeof(stack
));
1676 setup_sigcontext(&uc
->tuc_mcontext
, env
, set
->sig
[0]);
1677 /* Save coprocessor signal frame. */
1678 regspace
= uc
->tuc_regspace
;
1679 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
1680 regspace
= setup_sigframe_v2_vfp(regspace
, env
);
1682 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
1683 regspace
= setup_sigframe_v2_iwmmxt(regspace
, env
);
1686 /* Write terminating magic word */
1687 __put_user(0, regspace
);
1689 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1690 __put_user(set
->sig
[i
], &uc
->tuc_sigmask
.sig
[i
]);
1694 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1695 static void setup_frame_v1(int usig
, struct target_sigaction
*ka
,
1696 target_sigset_t
*set
, CPUARMState
*regs
)
1698 struct sigframe_v1
*frame
;
1699 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1702 trace_user_setup_frame(regs
, frame_addr
);
1703 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
1707 setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
1709 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1710 __put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]);
1713 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1714 frame_addr
+ offsetof(struct sigframe_v1
, retcode
));
1716 unlock_user_struct(frame
, frame_addr
, 1);
1719 static void setup_frame_v2(int usig
, struct target_sigaction
*ka
,
1720 target_sigset_t
*set
, CPUARMState
*regs
)
1722 struct sigframe_v2
*frame
;
1723 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1725 trace_user_setup_frame(regs
, frame_addr
);
1726 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
1730 setup_sigframe_v2(&frame
->uc
, set
, regs
);
1732 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1733 frame_addr
+ offsetof(struct sigframe_v2
, retcode
));
1735 unlock_user_struct(frame
, frame_addr
, 1);
1738 static void setup_frame(int usig
, struct target_sigaction
*ka
,
1739 target_sigset_t
*set
, CPUARMState
*regs
)
1741 if (get_osversion() >= 0x020612) {
1742 setup_frame_v2(usig
, ka
, set
, regs
);
1744 setup_frame_v1(usig
, ka
, set
, regs
);
1748 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1749 static void setup_rt_frame_v1(int usig
, struct target_sigaction
*ka
,
1750 target_siginfo_t
*info
,
1751 target_sigset_t
*set
, CPUARMState
*env
)
1753 struct rt_sigframe_v1
*frame
;
1754 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1755 struct target_sigaltstack stack
;
1757 abi_ulong info_addr
, uc_addr
;
1759 trace_user_setup_rt_frame(env
, frame_addr
);
1760 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
1764 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, info
);
1765 __put_user(info_addr
, &frame
->pinfo
);
1766 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
);
1767 __put_user(uc_addr
, &frame
->puc
);
1768 tswap_siginfo(&frame
->info
, info
);
1770 /* Clear all the bits of the ucontext we don't use. */
1771 memset(&frame
->uc
, 0, offsetof(struct target_ucontext_v1
, tuc_mcontext
));
1773 memset(&stack
, 0, sizeof(stack
));
1774 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1775 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1776 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1777 memcpy(&frame
->uc
.tuc_stack
, &stack
, sizeof(stack
));
1779 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, set
->sig
[0]);
1780 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1781 __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
1784 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1785 frame_addr
+ offsetof(struct rt_sigframe_v1
, retcode
));
1787 env
->regs
[1] = info_addr
;
1788 env
->regs
[2] = uc_addr
;
1790 unlock_user_struct(frame
, frame_addr
, 1);
1793 static void setup_rt_frame_v2(int usig
, struct target_sigaction
*ka
,
1794 target_siginfo_t
*info
,
1795 target_sigset_t
*set
, CPUARMState
*env
)
1797 struct rt_sigframe_v2
*frame
;
1798 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1799 abi_ulong info_addr
, uc_addr
;
1801 trace_user_setup_rt_frame(env
, frame_addr
);
1802 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
1806 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, info
);
1807 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, uc
);
1808 tswap_siginfo(&frame
->info
, info
);
1810 setup_sigframe_v2(&frame
->uc
, set
, env
);
1812 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1813 frame_addr
+ offsetof(struct rt_sigframe_v2
, retcode
));
1815 env
->regs
[1] = info_addr
;
1816 env
->regs
[2] = uc_addr
;
1818 unlock_user_struct(frame
, frame_addr
, 1);
1821 static void setup_rt_frame(int usig
, struct target_sigaction
*ka
,
1822 target_siginfo_t
*info
,
1823 target_sigset_t
*set
, CPUARMState
*env
)
1825 if (get_osversion() >= 0x020612) {
1826 setup_rt_frame_v2(usig
, ka
, info
, set
, env
);
1828 setup_rt_frame_v1(usig
, ka
, info
, set
, env
);
1833 restore_sigcontext(CPUARMState
*env
, struct target_sigcontext
*sc
)
1838 __get_user(env
->regs
[0], &sc
->arm_r0
);
1839 __get_user(env
->regs
[1], &sc
->arm_r1
);
1840 __get_user(env
->regs
[2], &sc
->arm_r2
);
1841 __get_user(env
->regs
[3], &sc
->arm_r3
);
1842 __get_user(env
->regs
[4], &sc
->arm_r4
);
1843 __get_user(env
->regs
[5], &sc
->arm_r5
);
1844 __get_user(env
->regs
[6], &sc
->arm_r6
);
1845 __get_user(env
->regs
[7], &sc
->arm_r7
);
1846 __get_user(env
->regs
[8], &sc
->arm_r8
);
1847 __get_user(env
->regs
[9], &sc
->arm_r9
);
1848 __get_user(env
->regs
[10], &sc
->arm_r10
);
1849 __get_user(env
->regs
[11], &sc
->arm_fp
);
1850 __get_user(env
->regs
[12], &sc
->arm_ip
);
1851 __get_user(env
->regs
[13], &sc
->arm_sp
);
1852 __get_user(env
->regs
[14], &sc
->arm_lr
);
1853 __get_user(env
->regs
[15], &sc
->arm_pc
);
1854 #ifdef TARGET_CONFIG_CPU_32
1855 __get_user(cpsr
, &sc
->arm_cpsr
);
1856 cpsr_write(env
, cpsr
, CPSR_USER
| CPSR_EXEC
, CPSRWriteByInstr
);
1859 err
|= !valid_user_regs(env
);
1864 static long do_sigreturn_v1(CPUARMState
*env
)
1866 abi_ulong frame_addr
;
1867 struct sigframe_v1
*frame
= NULL
;
1868 target_sigset_t set
;
1873 * Since we stacked the signal on a 64-bit boundary,
1874 * then 'sp' should be word aligned here. If it's
1875 * not, then the user is trying to mess with us.
1877 frame_addr
= env
->regs
[13];
1878 trace_user_do_sigreturn(env
, frame_addr
);
1879 if (frame_addr
& 7) {
1883 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
1887 __get_user(set
.sig
[0], &frame
->sc
.oldmask
);
1888 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1889 __get_user(set
.sig
[i
], &frame
->extramask
[i
- 1]);
1892 target_to_host_sigset_internal(&host_set
, &set
);
1893 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1895 if (restore_sigcontext(env
, &frame
->sc
)) {
1900 /* Send SIGTRAP if we're single-stepping */
1901 if (ptrace_cancel_bpt(current
))
1902 send_sig(SIGTRAP
, current
, 1);
1904 unlock_user_struct(frame
, frame_addr
, 0);
1905 return -TARGET_QEMU_ESIGRETURN
;
1908 force_sig(TARGET_SIGSEGV
/* , current */);
1912 static abi_ulong
*restore_sigframe_v2_vfp(CPUARMState
*env
, abi_ulong
*regspace
)
1915 abi_ulong magic
, sz
;
1916 uint32_t fpscr
, fpexc
;
1917 struct target_vfp_sigframe
*vfpframe
;
1918 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
1920 __get_user(magic
, &vfpframe
->magic
);
1921 __get_user(sz
, &vfpframe
->size
);
1922 if (magic
!= TARGET_VFP_MAGIC
|| sz
!= sizeof(*vfpframe
)) {
1925 for (i
= 0; i
< 32; i
++) {
1926 __get_user(float64_val(env
->vfp
.regs
[i
]), &vfpframe
->ufp
.fpregs
[i
]);
1928 __get_user(fpscr
, &vfpframe
->ufp
.fpscr
);
1929 vfp_set_fpscr(env
, fpscr
);
1930 __get_user(fpexc
, &vfpframe
->ufp_exc
.fpexc
);
1931 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1932 * and the exception flag is cleared
1935 fpexc
&= ~((1 << 31) | (1 << 28));
1936 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = fpexc
;
1937 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
1938 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
1939 return (abi_ulong
*)(vfpframe
+ 1);
1942 static abi_ulong
*restore_sigframe_v2_iwmmxt(CPUARMState
*env
,
1943 abi_ulong
*regspace
)
1946 abi_ulong magic
, sz
;
1947 struct target_iwmmxt_sigframe
*iwmmxtframe
;
1948 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
1950 __get_user(magic
, &iwmmxtframe
->magic
);
1951 __get_user(sz
, &iwmmxtframe
->size
);
1952 if (magic
!= TARGET_IWMMXT_MAGIC
|| sz
!= sizeof(*iwmmxtframe
)) {
1955 for (i
= 0; i
< 16; i
++) {
1956 __get_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
1958 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
1959 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
1960 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
1961 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
1962 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
1963 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
1964 return (abi_ulong
*)(iwmmxtframe
+ 1);
1967 static int do_sigframe_return_v2(CPUARMState
*env
, target_ulong frame_addr
,
1968 struct target_ucontext_v2
*uc
)
1971 abi_ulong
*regspace
;
1973 target_to_host_sigset(&host_set
, &uc
->tuc_sigmask
);
1974 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1976 if (restore_sigcontext(env
, &uc
->tuc_mcontext
))
1979 /* Restore coprocessor signal frame */
1980 regspace
= uc
->tuc_regspace
;
1981 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
1982 regspace
= restore_sigframe_v2_vfp(env
, regspace
);
1987 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
1988 regspace
= restore_sigframe_v2_iwmmxt(env
, regspace
);
1994 if (do_sigaltstack(frame_addr
+ offsetof(struct target_ucontext_v2
, tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
1998 /* Send SIGTRAP if we're single-stepping */
1999 if (ptrace_cancel_bpt(current
))
2000 send_sig(SIGTRAP
, current
, 1);
2006 static long do_sigreturn_v2(CPUARMState
*env
)
2008 abi_ulong frame_addr
;
2009 struct sigframe_v2
*frame
= NULL
;
2012 * Since we stacked the signal on a 64-bit boundary,
2013 * then 'sp' should be word aligned here. If it's
2014 * not, then the user is trying to mess with us.
2016 frame_addr
= env
->regs
[13];
2017 trace_user_do_sigreturn(env
, frame_addr
);
2018 if (frame_addr
& 7) {
2022 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
2026 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
)) {
2030 unlock_user_struct(frame
, frame_addr
, 0);
2031 return -TARGET_QEMU_ESIGRETURN
;
2034 unlock_user_struct(frame
, frame_addr
, 0);
2035 force_sig(TARGET_SIGSEGV
/* , current */);
2039 long do_sigreturn(CPUARMState
*env
)
2041 if (get_osversion() >= 0x020612) {
2042 return do_sigreturn_v2(env
);
2044 return do_sigreturn_v1(env
);
2048 static long do_rt_sigreturn_v1(CPUARMState
*env
)
2050 abi_ulong frame_addr
;
2051 struct rt_sigframe_v1
*frame
= NULL
;
2055 * Since we stacked the signal on a 64-bit boundary,
2056 * then 'sp' should be word aligned here. If it's
2057 * not, then the user is trying to mess with us.
2059 frame_addr
= env
->regs
[13];
2060 trace_user_do_rt_sigreturn(env
, frame_addr
);
2061 if (frame_addr
& 7) {
2065 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
2069 target_to_host_sigset(&host_set
, &frame
->uc
.tuc_sigmask
);
2070 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
2072 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
)) {
2076 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
.tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
2080 /* Send SIGTRAP if we're single-stepping */
2081 if (ptrace_cancel_bpt(current
))
2082 send_sig(SIGTRAP
, current
, 1);
2084 unlock_user_struct(frame
, frame_addr
, 0);
2085 return -TARGET_QEMU_ESIGRETURN
;
2088 unlock_user_struct(frame
, frame_addr
, 0);
2089 force_sig(TARGET_SIGSEGV
/* , current */);
2093 static long do_rt_sigreturn_v2(CPUARMState
*env
)
2095 abi_ulong frame_addr
;
2096 struct rt_sigframe_v2
*frame
= NULL
;
2099 * Since we stacked the signal on a 64-bit boundary,
2100 * then 'sp' should be word aligned here. If it's
2101 * not, then the user is trying to mess with us.
2103 frame_addr
= env
->regs
[13];
2104 trace_user_do_rt_sigreturn(env
, frame_addr
);
2105 if (frame_addr
& 7) {
2109 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
2113 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
)) {
2117 unlock_user_struct(frame
, frame_addr
, 0);
2118 return -TARGET_QEMU_ESIGRETURN
;
2121 unlock_user_struct(frame
, frame_addr
, 0);
2122 force_sig(TARGET_SIGSEGV
/* , current */);
2126 long do_rt_sigreturn(CPUARMState
*env
)
2128 if (get_osversion() >= 0x020612) {
2129 return do_rt_sigreturn_v2(env
);
2131 return do_rt_sigreturn_v1(env
);
2135 #elif defined(TARGET_SPARC)
2137 #define __SUNOS_MAXWIN 31
2139 /* This is what SunOS does, so shall I. */
2140 struct target_sigcontext
{
2141 abi_ulong sigc_onstack
; /* state to restore */
2143 abi_ulong sigc_mask
; /* sigmask to restore */
2144 abi_ulong sigc_sp
; /* stack pointer */
2145 abi_ulong sigc_pc
; /* program counter */
2146 abi_ulong sigc_npc
; /* next program counter */
2147 abi_ulong sigc_psr
; /* for condition codes etc */
2148 abi_ulong sigc_g1
; /* User uses these two registers */
2149 abi_ulong sigc_o0
; /* within the trampoline code. */
2151 /* Now comes information regarding the users window set
2152 * at the time of the signal.
2154 abi_ulong sigc_oswins
; /* outstanding windows */
2156 /* stack ptrs for each regwin buf */
2157 char *sigc_spbuf
[__SUNOS_MAXWIN
];
2159 /* Windows to restore after signal */
2161 abi_ulong locals
[8];
2163 } sigc_wbuf
[__SUNOS_MAXWIN
];
2165 /* A Sparc stack frame */
2166 struct sparc_stackf
{
2167 abi_ulong locals
[8];
2169 /* It's simpler to treat fp and callers_pc as elements of ins[]
2170 * since we never need to access them ourselves.
2174 abi_ulong xxargs
[1];
2183 abi_ulong u_regs
[16]; /* globals and ins */
2189 abi_ulong si_float_regs
[32];
2190 unsigned long si_fsr
;
2191 unsigned long si_fpqdepth
;
2193 unsigned long *insn_addr
;
2196 } qemu_siginfo_fpu_t
;
2199 struct target_signal_frame
{
2200 struct sparc_stackf ss
;
2203 abi_ulong insns
[2] __attribute__ ((aligned (8)));
2204 abi_ulong extramask
[TARGET_NSIG_WORDS
- 1];
2205 abi_ulong extra_size
; /* Should be 0 */
2206 qemu_siginfo_fpu_t fpu_state
;
2208 struct target_rt_signal_frame
{
2209 struct sparc_stackf ss
;
2214 unsigned int insns
[2];
2216 unsigned int extra_size
; /* Should be 0 */
2217 qemu_siginfo_fpu_t fpu_state
;
2231 #define UREG_FP UREG_I6
2232 #define UREG_SP UREG_O6
2234 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
2236 unsigned long framesize
)
2240 sp
= env
->regwptr
[UREG_FP
];
2242 /* This is the X/Open sanctioned signal stack switching. */
2243 if (sa
->sa_flags
& TARGET_SA_ONSTACK
) {
2244 if (!on_sig_stack(sp
)
2245 && !((target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
) & 7)) {
2246 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2249 return sp
- framesize
;
2253 setup___siginfo(__siginfo_t
*si
, CPUSPARCState
*env
, abi_ulong mask
)
2257 __put_user(env
->psr
, &si
->si_regs
.psr
);
2258 __put_user(env
->pc
, &si
->si_regs
.pc
);
2259 __put_user(env
->npc
, &si
->si_regs
.npc
);
2260 __put_user(env
->y
, &si
->si_regs
.y
);
2261 for (i
=0; i
< 8; i
++) {
2262 __put_user(env
->gregs
[i
], &si
->si_regs
.u_regs
[i
]);
2264 for (i
=0; i
< 8; i
++) {
2265 __put_user(env
->regwptr
[UREG_I0
+ i
], &si
->si_regs
.u_regs
[i
+8]);
2267 __put_user(mask
, &si
->si_mask
);
2273 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
2274 CPUSPARCState
*env
, unsigned long mask
)
2278 __put_user(mask
, &sc
->sigc_mask
);
2279 __put_user(env
->regwptr
[UREG_SP
], &sc
->sigc_sp
);
2280 __put_user(env
->pc
, &sc
->sigc_pc
);
2281 __put_user(env
->npc
, &sc
->sigc_npc
);
2282 __put_user(env
->psr
, &sc
->sigc_psr
);
2283 __put_user(env
->gregs
[1], &sc
->sigc_g1
);
2284 __put_user(env
->regwptr
[UREG_O0
], &sc
->sigc_o0
);
2289 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2291 static void setup_frame(int sig
, struct target_sigaction
*ka
,
2292 target_sigset_t
*set
, CPUSPARCState
*env
)
2295 struct target_signal_frame
*sf
;
2296 int sigframe_size
, err
, i
;
2298 /* 1. Make sure everything is clean */
2299 //synchronize_user_stack();
2301 sigframe_size
= NF_ALIGNEDSZ
;
2302 sf_addr
= get_sigframe(ka
, env
, sigframe_size
);
2303 trace_user_setup_frame(env
, sf_addr
);
2305 sf
= lock_user(VERIFY_WRITE
, sf_addr
,
2306 sizeof(struct target_signal_frame
), 0);
2311 if (invalid_frame_pointer(sf
, sigframe_size
))
2312 goto sigill_and_return
;
2314 /* 2. Save the current process state */
2315 err
= setup___siginfo(&sf
->info
, env
, set
->sig
[0]);
2316 __put_user(0, &sf
->extra_size
);
2318 //save_fpu_state(regs, &sf->fpu_state);
2319 //__put_user(&sf->fpu_state, &sf->fpu_save);
2321 __put_user(set
->sig
[0], &sf
->info
.si_mask
);
2322 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
2323 __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
2326 for (i
= 0; i
< 8; i
++) {
2327 __put_user(env
->regwptr
[i
+ UREG_L0
], &sf
->ss
.locals
[i
]);
2329 for (i
= 0; i
< 8; i
++) {
2330 __put_user(env
->regwptr
[i
+ UREG_I0
], &sf
->ss
.ins
[i
]);
2335 /* 3. signal handler back-trampoline and parameters */
2336 env
->regwptr
[UREG_FP
] = sf_addr
;
2337 env
->regwptr
[UREG_I0
] = sig
;
2338 env
->regwptr
[UREG_I1
] = sf_addr
+
2339 offsetof(struct target_signal_frame
, info
);
2340 env
->regwptr
[UREG_I2
] = sf_addr
+
2341 offsetof(struct target_signal_frame
, info
);
2343 /* 4. signal handler */
2344 env
->pc
= ka
->_sa_handler
;
2345 env
->npc
= (env
->pc
+ 4);
2346 /* 5. return to kernel instructions */
2347 if (ka
->sa_restorer
) {
2348 env
->regwptr
[UREG_I7
] = ka
->sa_restorer
;
2352 env
->regwptr
[UREG_I7
] = sf_addr
+
2353 offsetof(struct target_signal_frame
, insns
) - 2 * 4;
2355 /* mov __NR_sigreturn, %g1 */
2357 __put_user(val32
, &sf
->insns
[0]);
2361 __put_user(val32
, &sf
->insns
[1]);
2365 /* Flush instruction space. */
2366 // flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2369 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
2373 force_sig(TARGET_SIGILL
);
2376 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
2377 force_sig(TARGET_SIGSEGV
);
2380 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
2381 target_siginfo_t
*info
,
2382 target_sigset_t
*set
, CPUSPARCState
*env
)
2384 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2387 long do_sigreturn(CPUSPARCState
*env
)
2390 struct target_signal_frame
*sf
;
2391 uint32_t up_psr
, pc
, npc
;
2392 target_sigset_t set
;
2396 sf_addr
= env
->regwptr
[UREG_FP
];
2397 trace_user_do_sigreturn(env
, sf_addr
);
2398 if (!lock_user_struct(VERIFY_READ
, sf
, sf_addr
, 1)) {
2402 /* 1. Make sure we are not getting garbage from the user */
2407 __get_user(pc
, &sf
->info
.si_regs
.pc
);
2408 __get_user(npc
, &sf
->info
.si_regs
.npc
);
2410 if ((pc
| npc
) & 3) {
2414 /* 2. Restore the state */
2415 __get_user(up_psr
, &sf
->info
.si_regs
.psr
);
2417 /* User can only change condition codes and FPU enabling in %psr. */
2418 env
->psr
= (up_psr
& (PSR_ICC
/* | PSR_EF */))
2419 | (env
->psr
& ~(PSR_ICC
/* | PSR_EF */));
2423 __get_user(env
->y
, &sf
->info
.si_regs
.y
);
2424 for (i
=0; i
< 8; i
++) {
2425 __get_user(env
->gregs
[i
], &sf
->info
.si_regs
.u_regs
[i
]);
2427 for (i
=0; i
< 8; i
++) {
2428 __get_user(env
->regwptr
[i
+ UREG_I0
], &sf
->info
.si_regs
.u_regs
[i
+8]);
2431 /* FIXME: implement FPU save/restore:
2432 * __get_user(fpu_save, &sf->fpu_save);
2434 * err |= restore_fpu_state(env, fpu_save);
2437 /* This is pretty much atomic, no amount locking would prevent
2438 * the races which exist anyways.
2440 __get_user(set
.sig
[0], &sf
->info
.si_mask
);
2441 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
2442 __get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]);
2445 target_to_host_sigset_internal(&host_set
, &set
);
2446 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
2451 unlock_user_struct(sf
, sf_addr
, 0);
2452 return -TARGET_QEMU_ESIGRETURN
;
2455 unlock_user_struct(sf
, sf_addr
, 0);
2456 force_sig(TARGET_SIGSEGV
);
2459 long do_rt_sigreturn(CPUSPARCState
*env
)
2461 trace_user_do_rt_sigreturn(env
, 0);
2462 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2463 return -TARGET_ENOSYS
;
2466 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2488 typedef abi_ulong target_mc_greg_t
;
2489 typedef target_mc_greg_t target_mc_gregset_t
[MC_NGREG
];
2491 struct target_mc_fq
{
2492 abi_ulong
*mcfq_addr
;
2496 struct target_mc_fpu
{
2500 //uint128_t qregs[16];
2502 abi_ulong mcfpu_fsr
;
2503 abi_ulong mcfpu_fprs
;
2504 abi_ulong mcfpu_gsr
;
2505 struct target_mc_fq
*mcfpu_fq
;
2506 unsigned char mcfpu_qcnt
;
2507 unsigned char mcfpu_qentsz
;
2508 unsigned char mcfpu_enab
;
2510 typedef struct target_mc_fpu target_mc_fpu_t
;
2513 target_mc_gregset_t mc_gregs
;
2514 target_mc_greg_t mc_fp
;
2515 target_mc_greg_t mc_i7
;
2516 target_mc_fpu_t mc_fpregs
;
2517 } target_mcontext_t
;
2519 struct target_ucontext
{
2520 struct target_ucontext
*tuc_link
;
2521 abi_ulong tuc_flags
;
2522 target_sigset_t tuc_sigmask
;
2523 target_mcontext_t tuc_mcontext
;
2526 /* A V9 register window */
2527 struct target_reg_window
{
2528 abi_ulong locals
[8];
2532 #define TARGET_STACK_BIAS 2047
2534 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2535 void sparc64_set_context(CPUSPARCState
*env
)
2538 struct target_ucontext
*ucp
;
2539 target_mc_gregset_t
*grp
;
2540 abi_ulong pc
, npc
, tstate
;
2541 abi_ulong fp
, i7
, w_addr
;
2544 ucp_addr
= env
->regwptr
[UREG_I0
];
2545 if (!lock_user_struct(VERIFY_READ
, ucp
, ucp_addr
, 1)) {
2548 grp
= &ucp
->tuc_mcontext
.mc_gregs
;
2549 __get_user(pc
, &((*grp
)[MC_PC
]));
2550 __get_user(npc
, &((*grp
)[MC_NPC
]));
2551 if ((pc
| npc
) & 3) {
2554 if (env
->regwptr
[UREG_I1
]) {
2555 target_sigset_t target_set
;
2558 if (TARGET_NSIG_WORDS
== 1) {
2559 __get_user(target_set
.sig
[0], &ucp
->tuc_sigmask
.sig
[0]);
2561 abi_ulong
*src
, *dst
;
2562 src
= ucp
->tuc_sigmask
.sig
;
2563 dst
= target_set
.sig
;
2564 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
2565 __get_user(*dst
, src
);
2568 target_to_host_sigset_internal(&set
, &target_set
);
2569 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
2573 __get_user(env
->y
, &((*grp
)[MC_Y
]));
2574 __get_user(tstate
, &((*grp
)[MC_TSTATE
]));
2575 env
->asi
= (tstate
>> 24) & 0xff;
2576 cpu_put_ccr(env
, tstate
>> 32);
2577 cpu_put_cwp64(env
, tstate
& 0x1f);
2578 __get_user(env
->gregs
[1], (&(*grp
)[MC_G1
]));
2579 __get_user(env
->gregs
[2], (&(*grp
)[MC_G2
]));
2580 __get_user(env
->gregs
[3], (&(*grp
)[MC_G3
]));
2581 __get_user(env
->gregs
[4], (&(*grp
)[MC_G4
]));
2582 __get_user(env
->gregs
[5], (&(*grp
)[MC_G5
]));
2583 __get_user(env
->gregs
[6], (&(*grp
)[MC_G6
]));
2584 __get_user(env
->gregs
[7], (&(*grp
)[MC_G7
]));
2585 __get_user(env
->regwptr
[UREG_I0
], (&(*grp
)[MC_O0
]));
2586 __get_user(env
->regwptr
[UREG_I1
], (&(*grp
)[MC_O1
]));
2587 __get_user(env
->regwptr
[UREG_I2
], (&(*grp
)[MC_O2
]));
2588 __get_user(env
->regwptr
[UREG_I3
], (&(*grp
)[MC_O3
]));
2589 __get_user(env
->regwptr
[UREG_I4
], (&(*grp
)[MC_O4
]));
2590 __get_user(env
->regwptr
[UREG_I5
], (&(*grp
)[MC_O5
]));
2591 __get_user(env
->regwptr
[UREG_I6
], (&(*grp
)[MC_O6
]));
2592 __get_user(env
->regwptr
[UREG_I7
], (&(*grp
)[MC_O7
]));
2594 __get_user(fp
, &(ucp
->tuc_mcontext
.mc_fp
));
2595 __get_user(i7
, &(ucp
->tuc_mcontext
.mc_i7
));
2597 w_addr
= TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
];
2598 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]),
2606 /* FIXME this does not match how the kernel handles the FPU in
2607 * its sparc64_set_context implementation. In particular the FPU
2608 * is only restored if fenab is non-zero in:
2609 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2611 __get_user(env
->fprs
, &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fprs
));
2613 uint32_t *src
= ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2614 for (i
= 0; i
< 64; i
++, src
++) {
2616 __get_user(env
->fpr
[i
/2].l
.lower
, src
);
2618 __get_user(env
->fpr
[i
/2].l
.upper
, src
);
2622 __get_user(env
->fsr
,
2623 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fsr
));
2624 __get_user(env
->gsr
,
2625 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_gsr
));
2626 unlock_user_struct(ucp
, ucp_addr
, 0);
2629 unlock_user_struct(ucp
, ucp_addr
, 0);
2630 force_sig(TARGET_SIGSEGV
);
2633 void sparc64_get_context(CPUSPARCState
*env
)
2636 struct target_ucontext
*ucp
;
2637 target_mc_gregset_t
*grp
;
2638 target_mcontext_t
*mcp
;
2639 abi_ulong fp
, i7
, w_addr
;
2642 target_sigset_t target_set
;
2645 ucp_addr
= env
->regwptr
[UREG_I0
];
2646 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 do_sigprocmask(0, NULL
, &set
);
2660 host_to_target_sigset_internal(&target_set
, &set
);
2661 if (TARGET_NSIG_WORDS
== 1) {
2662 __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 __put_user(*src
, dst
);
2675 /* XXX: tstate must be saved properly */
2676 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2677 __put_user(env
->pc
, &((*grp
)[MC_PC
]));
2678 __put_user(env
->npc
, &((*grp
)[MC_NPC
]));
2679 __put_user(env
->y
, &((*grp
)[MC_Y
]));
2680 __put_user(env
->gregs
[1], &((*grp
)[MC_G1
]));
2681 __put_user(env
->gregs
[2], &((*grp
)[MC_G2
]));
2682 __put_user(env
->gregs
[3], &((*grp
)[MC_G3
]));
2683 __put_user(env
->gregs
[4], &((*grp
)[MC_G4
]));
2684 __put_user(env
->gregs
[5], &((*grp
)[MC_G5
]));
2685 __put_user(env
->gregs
[6], &((*grp
)[MC_G6
]));
2686 __put_user(env
->gregs
[7], &((*grp
)[MC_G7
]));
2687 __put_user(env
->regwptr
[UREG_I0
], &((*grp
)[MC_O0
]));
2688 __put_user(env
->regwptr
[UREG_I1
], &((*grp
)[MC_O1
]));
2689 __put_user(env
->regwptr
[UREG_I2
], &((*grp
)[MC_O2
]));
2690 __put_user(env
->regwptr
[UREG_I3
], &((*grp
)[MC_O3
]));
2691 __put_user(env
->regwptr
[UREG_I4
], &((*grp
)[MC_O4
]));
2692 __put_user(env
->regwptr
[UREG_I5
], &((*grp
)[MC_O5
]));
2693 __put_user(env
->regwptr
[UREG_I6
], &((*grp
)[MC_O6
]));
2694 __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]),
2702 if (get_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
2706 __put_user(fp
, &(mcp
->mc_fp
));
2707 __put_user(i7
, &(mcp
->mc_i7
));
2710 uint32_t *dst
= ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2711 for (i
= 0; i
< 64; i
++, dst
++) {
2713 __put_user(env
->fpr
[i
/2].l
.lower
, dst
);
2715 __put_user(env
->fpr
[i
/2].l
.upper
, dst
);
2719 __put_user(env
->fsr
, &(mcp
->mc_fpregs
.mcfpu_fsr
));
2720 __put_user(env
->gsr
, &(mcp
->mc_fpregs
.mcfpu_gsr
));
2721 __put_user(env
->fprs
, &(mcp
->mc_fpregs
.mcfpu_fprs
));
2725 unlock_user_struct(ucp
, ucp_addr
, 1);
2728 unlock_user_struct(ucp
, ucp_addr
, 1);
2729 force_sig(TARGET_SIGSEGV
);
2732 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2734 # if defined(TARGET_ABI_MIPSO32)
2735 struct target_sigcontext
{
2736 uint32_t sc_regmask
; /* Unused */
2739 uint64_t sc_regs
[32];
2740 uint64_t sc_fpregs
[32];
2741 uint32_t sc_ownedfp
; /* Unused */
2742 uint32_t sc_fpc_csr
;
2743 uint32_t sc_fpc_eir
; /* Unused */
2744 uint32_t sc_used_math
;
2745 uint32_t sc_dsp
; /* dsp status, was sc_ssflags */
2749 target_ulong sc_hi1
; /* Was sc_cause */
2750 target_ulong sc_lo1
; /* Was sc_badvaddr */
2751 target_ulong sc_hi2
; /* Was sc_sigset[4] */
2752 target_ulong sc_lo2
;
2753 target_ulong sc_hi3
;
2754 target_ulong sc_lo3
;
2756 # else /* N32 || N64 */
2757 struct target_sigcontext
{
2758 uint64_t sc_regs
[32];
2759 uint64_t sc_fpregs
[32];
2769 uint32_t sc_fpc_csr
;
2770 uint32_t sc_used_math
;
2772 uint32_t sc_reserved
;
2777 uint32_t sf_ass
[4]; /* argument save space for o32 */
2778 uint32_t sf_code
[2]; /* signal trampoline */
2779 struct target_sigcontext sf_sc
;
2780 target_sigset_t sf_mask
;
2783 struct target_ucontext
{
2784 target_ulong tuc_flags
;
2785 target_ulong tuc_link
;
2786 target_stack_t tuc_stack
;
2788 struct target_sigcontext tuc_mcontext
;
2789 target_sigset_t tuc_sigmask
;
2792 struct target_rt_sigframe
{
2793 uint32_t rs_ass
[4]; /* argument save space for o32 */
2794 uint32_t rs_code
[2]; /* signal trampoline */
2795 struct target_siginfo rs_info
;
2796 struct target_ucontext rs_uc
;
2799 /* Install trampoline to jump back from signal handler */
2800 static inline int install_sigtramp(unsigned int *tramp
, unsigned int syscall
)
2805 * Set up the return code ...
2807 * li v0, __NR__foo_sigreturn
2811 __put_user(0x24020000 + syscall
, tramp
+ 0);
2812 __put_user(0x0000000c , tramp
+ 1);
2816 static inline void setup_sigcontext(CPUMIPSState
*regs
,
2817 struct target_sigcontext
*sc
)
2821 __put_user(exception_resume_pc(regs
), &sc
->sc_pc
);
2822 regs
->hflags
&= ~MIPS_HFLAG_BMASK
;
2824 __put_user(0, &sc
->sc_regs
[0]);
2825 for (i
= 1; i
< 32; ++i
) {
2826 __put_user(regs
->active_tc
.gpr
[i
], &sc
->sc_regs
[i
]);
2829 __put_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2830 __put_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2832 /* Rather than checking for dsp existence, always copy. The storage
2833 would just be garbage otherwise. */
2834 __put_user(regs
->active_tc
.HI
[1], &sc
->sc_hi1
);
2835 __put_user(regs
->active_tc
.HI
[2], &sc
->sc_hi2
);
2836 __put_user(regs
->active_tc
.HI
[3], &sc
->sc_hi3
);
2837 __put_user(regs
->active_tc
.LO
[1], &sc
->sc_lo1
);
2838 __put_user(regs
->active_tc
.LO
[2], &sc
->sc_lo2
);
2839 __put_user(regs
->active_tc
.LO
[3], &sc
->sc_lo3
);
2841 uint32_t dsp
= cpu_rddsp(0x3ff, regs
);
2842 __put_user(dsp
, &sc
->sc_dsp
);
2845 __put_user(1, &sc
->sc_used_math
);
2847 for (i
= 0; i
< 32; ++i
) {
2848 __put_user(regs
->active_fpu
.fpr
[i
].d
, &sc
->sc_fpregs
[i
]);
2853 restore_sigcontext(CPUMIPSState
*regs
, struct target_sigcontext
*sc
)
2857 __get_user(regs
->CP0_EPC
, &sc
->sc_pc
);
2859 __get_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2860 __get_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2862 for (i
= 1; i
< 32; ++i
) {
2863 __get_user(regs
->active_tc
.gpr
[i
], &sc
->sc_regs
[i
]);
2866 __get_user(regs
->active_tc
.HI
[1], &sc
->sc_hi1
);
2867 __get_user(regs
->active_tc
.HI
[2], &sc
->sc_hi2
);
2868 __get_user(regs
->active_tc
.HI
[3], &sc
->sc_hi3
);
2869 __get_user(regs
->active_tc
.LO
[1], &sc
->sc_lo1
);
2870 __get_user(regs
->active_tc
.LO
[2], &sc
->sc_lo2
);
2871 __get_user(regs
->active_tc
.LO
[3], &sc
->sc_lo3
);
2874 __get_user(dsp
, &sc
->sc_dsp
);
2875 cpu_wrdsp(dsp
, 0x3ff, regs
);
2878 for (i
= 0; i
< 32; ++i
) {
2879 __get_user(regs
->active_fpu
.fpr
[i
].d
, &sc
->sc_fpregs
[i
]);
2884 * Determine which stack to use..
2886 static inline abi_ulong
2887 get_sigframe(struct target_sigaction
*ka
, CPUMIPSState
*regs
, size_t frame_size
)
2891 /* Default to using normal stack */
2892 sp
= regs
->active_tc
.gpr
[29];
2895 * FPU emulator may have its own trampoline active just
2896 * above the user stack, 16-bytes before the next lowest
2897 * 16 byte boundary. Try to avoid trashing it.
2901 /* This is the X/Open sanctioned signal stack switching. */
2902 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
2903 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2906 return (sp
- frame_size
) & ~7;
2909 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState
*env
)
2911 if (env
->insn_flags
& (ASE_MIPS16
| ASE_MICROMIPS
)) {
2912 env
->hflags
&= ~MIPS_HFLAG_M16
;
2913 env
->hflags
|= (env
->active_tc
.PC
& 1) << MIPS_HFLAG_M16_SHIFT
;
2914 env
->active_tc
.PC
&= ~(target_ulong
) 1;
2918 # if defined(TARGET_ABI_MIPSO32)
2919 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2920 static void setup_frame(int sig
, struct target_sigaction
* ka
,
2921 target_sigset_t
*set
, CPUMIPSState
*regs
)
2923 struct sigframe
*frame
;
2924 abi_ulong frame_addr
;
2927 frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
2928 trace_user_setup_frame(regs
, frame_addr
);
2929 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
2933 install_sigtramp(frame
->sf_code
, TARGET_NR_sigreturn
);
2935 setup_sigcontext(regs
, &frame
->sf_sc
);
2937 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2938 __put_user(set
->sig
[i
], &frame
->sf_mask
.sig
[i
]);
2942 * Arguments to signal handler:
2944 * a0 = signal number
2945 * a1 = 0 (should be cause)
2946 * a2 = pointer to struct sigcontext
2948 * $25 and PC point to the signal handler, $29 points to the
2951 regs
->active_tc
.gpr
[ 4] = sig
;
2952 regs
->active_tc
.gpr
[ 5] = 0;
2953 regs
->active_tc
.gpr
[ 6] = frame_addr
+ offsetof(struct sigframe
, sf_sc
);
2954 regs
->active_tc
.gpr
[29] = frame_addr
;
2955 regs
->active_tc
.gpr
[31] = frame_addr
+ offsetof(struct sigframe
, sf_code
);
2956 /* The original kernel code sets CP0_EPC to the handler
2957 * since it returns to userland using eret
2958 * we cannot do this here, and we must set PC directly */
2959 regs
->active_tc
.PC
= regs
->active_tc
.gpr
[25] = ka
->_sa_handler
;
2960 mips_set_hflags_isa_mode_from_pc(regs
);
2961 unlock_user_struct(frame
, frame_addr
, 1);
2965 force_sig(TARGET_SIGSEGV
/*, current*/);
2968 long do_sigreturn(CPUMIPSState
*regs
)
2970 struct sigframe
*frame
;
2971 abi_ulong frame_addr
;
2973 target_sigset_t target_set
;
2976 frame_addr
= regs
->active_tc
.gpr
[29];
2977 trace_user_do_sigreturn(regs
, frame_addr
);
2978 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2981 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
2982 __get_user(target_set
.sig
[i
], &frame
->sf_mask
.sig
[i
]);
2985 target_to_host_sigset_internal(&blocked
, &target_set
);
2986 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
2988 restore_sigcontext(regs
, &frame
->sf_sc
);
2992 * Don't let your children do this ...
2994 __asm__
__volatile__(
3002 regs
->active_tc
.PC
= regs
->CP0_EPC
;
3003 mips_set_hflags_isa_mode_from_pc(regs
);
3004 /* I am not sure this is right, but it seems to work
3005 * maybe a problem with nested signals ? */
3007 return -TARGET_QEMU_ESIGRETURN
;
3010 force_sig(TARGET_SIGSEGV
/*, current*/);
3015 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3016 target_siginfo_t
*info
,
3017 target_sigset_t
*set
, CPUMIPSState
*env
)
3019 struct target_rt_sigframe
*frame
;
3020 abi_ulong frame_addr
;
3023 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
3024 trace_user_setup_rt_frame(env
, frame_addr
);
3025 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
3029 install_sigtramp(frame
->rs_code
, TARGET_NR_rt_sigreturn
);
3031 tswap_siginfo(&frame
->rs_info
, info
);
3033 __put_user(0, &frame
->rs_uc
.tuc_flags
);
3034 __put_user(0, &frame
->rs_uc
.tuc_link
);
3035 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->rs_uc
.tuc_stack
.ss_sp
);
3036 __put_user(target_sigaltstack_used
.ss_size
, &frame
->rs_uc
.tuc_stack
.ss_size
);
3037 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
3038 &frame
->rs_uc
.tuc_stack
.ss_flags
);
3040 setup_sigcontext(env
, &frame
->rs_uc
.tuc_mcontext
);
3042 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3043 __put_user(set
->sig
[i
], &frame
->rs_uc
.tuc_sigmask
.sig
[i
]);
3047 * Arguments to signal handler:
3049 * a0 = signal number
3050 * a1 = pointer to siginfo_t
3051 * a2 = pointer to struct ucontext
3053 * $25 and PC point to the signal handler, $29 points to the
3056 env
->active_tc
.gpr
[ 4] = sig
;
3057 env
->active_tc
.gpr
[ 5] = frame_addr
3058 + offsetof(struct target_rt_sigframe
, rs_info
);
3059 env
->active_tc
.gpr
[ 6] = frame_addr
3060 + offsetof(struct target_rt_sigframe
, rs_uc
);
3061 env
->active_tc
.gpr
[29] = frame_addr
;
3062 env
->active_tc
.gpr
[31] = frame_addr
3063 + offsetof(struct target_rt_sigframe
, rs_code
);
3064 /* The original kernel code sets CP0_EPC to the handler
3065 * since it returns to userland using eret
3066 * we cannot do this here, and we must set PC directly */
3067 env
->active_tc
.PC
= env
->active_tc
.gpr
[25] = ka
->_sa_handler
;
3068 mips_set_hflags_isa_mode_from_pc(env
);
3069 unlock_user_struct(frame
, frame_addr
, 1);
3073 unlock_user_struct(frame
, frame_addr
, 1);
3074 force_sig(TARGET_SIGSEGV
/*, current*/);
3077 long do_rt_sigreturn(CPUMIPSState
*env
)
3079 struct target_rt_sigframe
*frame
;
3080 abi_ulong frame_addr
;
3083 frame_addr
= env
->active_tc
.gpr
[29];
3084 trace_user_do_rt_sigreturn(env
, frame_addr
);
3085 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
3089 target_to_host_sigset(&blocked
, &frame
->rs_uc
.tuc_sigmask
);
3090 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3092 restore_sigcontext(env
, &frame
->rs_uc
.tuc_mcontext
);
3094 if (do_sigaltstack(frame_addr
+
3095 offsetof(struct target_rt_sigframe
, rs_uc
.tuc_stack
),
3096 0, get_sp_from_cpustate(env
)) == -EFAULT
)
3099 env
->active_tc
.PC
= env
->CP0_EPC
;
3100 mips_set_hflags_isa_mode_from_pc(env
);
3101 /* I am not sure this is right, but it seems to work
3102 * maybe a problem with nested signals ? */
3104 return -TARGET_QEMU_ESIGRETURN
;
3107 force_sig(TARGET_SIGSEGV
/*, current*/);
3111 #elif defined(TARGET_SH4)
3114 * code and data structures from linux kernel:
3115 * include/asm-sh/sigcontext.h
3116 * arch/sh/kernel/signal.c
3119 struct target_sigcontext
{
3120 target_ulong oldmask
;
3123 target_ulong sc_gregs
[16];
3127 target_ulong sc_gbr
;
3128 target_ulong sc_mach
;
3129 target_ulong sc_macl
;
3132 target_ulong sc_fpregs
[16];
3133 target_ulong sc_xfpregs
[16];
3134 unsigned int sc_fpscr
;
3135 unsigned int sc_fpul
;
3136 unsigned int sc_ownedfp
;
3139 struct target_sigframe
3141 struct target_sigcontext sc
;
3142 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
3143 uint16_t retcode
[3];
3147 struct target_ucontext
{
3148 target_ulong tuc_flags
;
3149 struct target_ucontext
*tuc_link
;
3150 target_stack_t tuc_stack
;
3151 struct target_sigcontext tuc_mcontext
;
3152 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
3155 struct target_rt_sigframe
3157 struct target_siginfo info
;
3158 struct target_ucontext uc
;
3159 uint16_t retcode
[3];
3163 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3164 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3166 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3167 unsigned long sp
, size_t frame_size
)
3169 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags(sp
) == 0)) {
3170 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3173 return (sp
- frame_size
) & -8ul;
3176 static void setup_sigcontext(struct target_sigcontext
*sc
,
3177 CPUSH4State
*regs
, unsigned long mask
)
3181 #define COPY(x) __put_user(regs->x, &sc->sc_##x)
3182 COPY(gregs
[0]); COPY(gregs
[1]);
3183 COPY(gregs
[2]); COPY(gregs
[3]);
3184 COPY(gregs
[4]); COPY(gregs
[5]);
3185 COPY(gregs
[6]); COPY(gregs
[7]);
3186 COPY(gregs
[8]); COPY(gregs
[9]);
3187 COPY(gregs
[10]); COPY(gregs
[11]);
3188 COPY(gregs
[12]); COPY(gregs
[13]);
3189 COPY(gregs
[14]); COPY(gregs
[15]);
3190 COPY(gbr
); COPY(mach
);
3191 COPY(macl
); COPY(pr
);
3195 for (i
=0; i
<16; i
++) {
3196 __put_user(regs
->fregs
[i
], &sc
->sc_fpregs
[i
]);
3198 __put_user(regs
->fpscr
, &sc
->sc_fpscr
);
3199 __put_user(regs
->fpul
, &sc
->sc_fpul
);
3201 /* non-iBCS2 extensions.. */
3202 __put_user(mask
, &sc
->oldmask
);
3205 static void restore_sigcontext(CPUSH4State
*regs
, struct target_sigcontext
*sc
)
3209 #define COPY(x) __get_user(regs->x, &sc->sc_##x)
3210 COPY(gregs
[0]); COPY(gregs
[1]);
3211 COPY(gregs
[2]); COPY(gregs
[3]);
3212 COPY(gregs
[4]); COPY(gregs
[5]);
3213 COPY(gregs
[6]); COPY(gregs
[7]);
3214 COPY(gregs
[8]); COPY(gregs
[9]);
3215 COPY(gregs
[10]); COPY(gregs
[11]);
3216 COPY(gregs
[12]); COPY(gregs
[13]);
3217 COPY(gregs
[14]); COPY(gregs
[15]);
3218 COPY(gbr
); COPY(mach
);
3219 COPY(macl
); COPY(pr
);
3223 for (i
=0; i
<16; i
++) {
3224 __get_user(regs
->fregs
[i
], &sc
->sc_fpregs
[i
]);
3226 __get_user(regs
->fpscr
, &sc
->sc_fpscr
);
3227 __get_user(regs
->fpul
, &sc
->sc_fpul
);
3229 regs
->tra
= -1; /* disable syscall checks */
3232 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3233 target_sigset_t
*set
, CPUSH4State
*regs
)
3235 struct target_sigframe
*frame
;
3236 abi_ulong frame_addr
;
3239 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
3240 trace_user_setup_frame(regs
, frame_addr
);
3241 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
3245 setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
3247 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
3248 __put_user(set
->sig
[i
+ 1], &frame
->extramask
[i
]);
3251 /* Set up to return from userspace. If provided, use a stub
3252 already in userspace. */
3253 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3254 regs
->pr
= (unsigned long) ka
->sa_restorer
;
3256 /* Generate return code (system call to sigreturn) */
3257 abi_ulong retcode_addr
= frame_addr
+
3258 offsetof(struct target_sigframe
, retcode
);
3259 __put_user(MOVW(2), &frame
->retcode
[0]);
3260 __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
3261 __put_user((TARGET_NR_sigreturn
), &frame
->retcode
[2]);
3262 regs
->pr
= (unsigned long) retcode_addr
;
3265 /* Set up registers for signal handler */
3266 regs
->gregs
[15] = frame_addr
;
3267 regs
->gregs
[4] = sig
; /* Arg for signal handler */
3269 regs
->gregs
[6] = frame_addr
+= offsetof(typeof(*frame
), sc
);
3270 regs
->pc
= (unsigned long) ka
->_sa_handler
;
3272 unlock_user_struct(frame
, frame_addr
, 1);
3276 unlock_user_struct(frame
, frame_addr
, 1);
3277 force_sig(TARGET_SIGSEGV
);
3280 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3281 target_siginfo_t
*info
,
3282 target_sigset_t
*set
, CPUSH4State
*regs
)
3284 struct target_rt_sigframe
*frame
;
3285 abi_ulong frame_addr
;
3288 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
3289 trace_user_setup_rt_frame(regs
, frame_addr
);
3290 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
3294 tswap_siginfo(&frame
->info
, info
);
3296 /* Create the ucontext. */
3297 __put_user(0, &frame
->uc
.tuc_flags
);
3298 __put_user(0, (unsigned long *)&frame
->uc
.tuc_link
);
3299 __put_user((unsigned long)target_sigaltstack_used
.ss_sp
,
3300 &frame
->uc
.tuc_stack
.ss_sp
);
3301 __put_user(sas_ss_flags(regs
->gregs
[15]),
3302 &frame
->uc
.tuc_stack
.ss_flags
);
3303 __put_user(target_sigaltstack_used
.ss_size
,
3304 &frame
->uc
.tuc_stack
.ss_size
);
3305 setup_sigcontext(&frame
->uc
.tuc_mcontext
,
3307 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3308 __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
3311 /* Set up to return from userspace. If provided, use a stub
3312 already in userspace. */
3313 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3314 regs
->pr
= (unsigned long) ka
->sa_restorer
;
3316 /* Generate return code (system call to sigreturn) */
3317 abi_ulong retcode_addr
= frame_addr
+
3318 offsetof(struct target_rt_sigframe
, retcode
);
3319 __put_user(MOVW(2), &frame
->retcode
[0]);
3320 __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
3321 __put_user((TARGET_NR_rt_sigreturn
), &frame
->retcode
[2]);
3322 regs
->pr
= (unsigned long) retcode_addr
;
3325 /* Set up registers for signal handler */
3326 regs
->gregs
[15] = frame_addr
;
3327 regs
->gregs
[4] = sig
; /* Arg for signal handler */
3328 regs
->gregs
[5] = frame_addr
+ offsetof(typeof(*frame
), info
);
3329 regs
->gregs
[6] = frame_addr
+ offsetof(typeof(*frame
), uc
);
3330 regs
->pc
= (unsigned long) ka
->_sa_handler
;
3332 unlock_user_struct(frame
, frame_addr
, 1);
3336 unlock_user_struct(frame
, frame_addr
, 1);
3337 force_sig(TARGET_SIGSEGV
);
3340 long do_sigreturn(CPUSH4State
*regs
)
3342 struct target_sigframe
*frame
;
3343 abi_ulong frame_addr
;
3345 target_sigset_t target_set
;
3349 frame_addr
= regs
->gregs
[15];
3350 trace_user_do_sigreturn(regs
, frame_addr
);
3351 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
3355 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
);
3356 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3357 __get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]);
3363 target_to_host_sigset_internal(&blocked
, &target_set
);
3364 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3366 restore_sigcontext(regs
, &frame
->sc
);
3368 unlock_user_struct(frame
, frame_addr
, 0);
3369 return -TARGET_QEMU_ESIGRETURN
;
3372 unlock_user_struct(frame
, frame_addr
, 0);
3373 force_sig(TARGET_SIGSEGV
);
3377 long do_rt_sigreturn(CPUSH4State
*regs
)
3379 struct target_rt_sigframe
*frame
;
3380 abi_ulong frame_addr
;
3383 frame_addr
= regs
->gregs
[15];
3384 trace_user_do_rt_sigreturn(regs
, frame_addr
);
3385 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
3389 target_to_host_sigset(&blocked
, &frame
->uc
.tuc_sigmask
);
3390 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3392 restore_sigcontext(regs
, &frame
->uc
.tuc_mcontext
);
3394 if (do_sigaltstack(frame_addr
+
3395 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
3396 0, get_sp_from_cpustate(regs
)) == -EFAULT
) {
3400 unlock_user_struct(frame
, frame_addr
, 0);
3401 return -TARGET_QEMU_ESIGRETURN
;
3404 unlock_user_struct(frame
, frame_addr
, 0);
3405 force_sig(TARGET_SIGSEGV
);
3408 #elif defined(TARGET_MICROBLAZE)
3410 struct target_sigcontext
{
3411 struct target_pt_regs regs
; /* needs to be first */
3415 struct target_stack_t
{
3418 unsigned int ss_size
;
3421 struct target_ucontext
{
3422 abi_ulong tuc_flags
;
3424 struct target_stack_t tuc_stack
;
3425 struct target_sigcontext tuc_mcontext
;
3426 uint32_t tuc_extramask
[TARGET_NSIG_WORDS
- 1];
3429 /* Signal frames. */
3430 struct target_signal_frame
{
3431 struct target_ucontext uc
;
3432 uint32_t extramask
[TARGET_NSIG_WORDS
- 1];
3436 struct rt_signal_frame
{
3442 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUMBState
*env
)
3444 __put_user(env
->regs
[0], &sc
->regs
.r0
);
3445 __put_user(env
->regs
[1], &sc
->regs
.r1
);
3446 __put_user(env
->regs
[2], &sc
->regs
.r2
);
3447 __put_user(env
->regs
[3], &sc
->regs
.r3
);
3448 __put_user(env
->regs
[4], &sc
->regs
.r4
);
3449 __put_user(env
->regs
[5], &sc
->regs
.r5
);
3450 __put_user(env
->regs
[6], &sc
->regs
.r6
);
3451 __put_user(env
->regs
[7], &sc
->regs
.r7
);
3452 __put_user(env
->regs
[8], &sc
->regs
.r8
);
3453 __put_user(env
->regs
[9], &sc
->regs
.r9
);
3454 __put_user(env
->regs
[10], &sc
->regs
.r10
);
3455 __put_user(env
->regs
[11], &sc
->regs
.r11
);
3456 __put_user(env
->regs
[12], &sc
->regs
.r12
);
3457 __put_user(env
->regs
[13], &sc
->regs
.r13
);
3458 __put_user(env
->regs
[14], &sc
->regs
.r14
);
3459 __put_user(env
->regs
[15], &sc
->regs
.r15
);
3460 __put_user(env
->regs
[16], &sc
->regs
.r16
);
3461 __put_user(env
->regs
[17], &sc
->regs
.r17
);
3462 __put_user(env
->regs
[18], &sc
->regs
.r18
);
3463 __put_user(env
->regs
[19], &sc
->regs
.r19
);
3464 __put_user(env
->regs
[20], &sc
->regs
.r20
);
3465 __put_user(env
->regs
[21], &sc
->regs
.r21
);
3466 __put_user(env
->regs
[22], &sc
->regs
.r22
);
3467 __put_user(env
->regs
[23], &sc
->regs
.r23
);
3468 __put_user(env
->regs
[24], &sc
->regs
.r24
);
3469 __put_user(env
->regs
[25], &sc
->regs
.r25
);
3470 __put_user(env
->regs
[26], &sc
->regs
.r26
);
3471 __put_user(env
->regs
[27], &sc
->regs
.r27
);
3472 __put_user(env
->regs
[28], &sc
->regs
.r28
);
3473 __put_user(env
->regs
[29], &sc
->regs
.r29
);
3474 __put_user(env
->regs
[30], &sc
->regs
.r30
);
3475 __put_user(env
->regs
[31], &sc
->regs
.r31
);
3476 __put_user(env
->sregs
[SR_PC
], &sc
->regs
.pc
);
3479 static void restore_sigcontext(struct target_sigcontext
*sc
, CPUMBState
*env
)
3481 __get_user(env
->regs
[0], &sc
->regs
.r0
);
3482 __get_user(env
->regs
[1], &sc
->regs
.r1
);
3483 __get_user(env
->regs
[2], &sc
->regs
.r2
);
3484 __get_user(env
->regs
[3], &sc
->regs
.r3
);
3485 __get_user(env
->regs
[4], &sc
->regs
.r4
);
3486 __get_user(env
->regs
[5], &sc
->regs
.r5
);
3487 __get_user(env
->regs
[6], &sc
->regs
.r6
);
3488 __get_user(env
->regs
[7], &sc
->regs
.r7
);
3489 __get_user(env
->regs
[8], &sc
->regs
.r8
);
3490 __get_user(env
->regs
[9], &sc
->regs
.r9
);
3491 __get_user(env
->regs
[10], &sc
->regs
.r10
);
3492 __get_user(env
->regs
[11], &sc
->regs
.r11
);
3493 __get_user(env
->regs
[12], &sc
->regs
.r12
);
3494 __get_user(env
->regs
[13], &sc
->regs
.r13
);
3495 __get_user(env
->regs
[14], &sc
->regs
.r14
);
3496 __get_user(env
->regs
[15], &sc
->regs
.r15
);
3497 __get_user(env
->regs
[16], &sc
->regs
.r16
);
3498 __get_user(env
->regs
[17], &sc
->regs
.r17
);
3499 __get_user(env
->regs
[18], &sc
->regs
.r18
);
3500 __get_user(env
->regs
[19], &sc
->regs
.r19
);
3501 __get_user(env
->regs
[20], &sc
->regs
.r20
);
3502 __get_user(env
->regs
[21], &sc
->regs
.r21
);
3503 __get_user(env
->regs
[22], &sc
->regs
.r22
);
3504 __get_user(env
->regs
[23], &sc
->regs
.r23
);
3505 __get_user(env
->regs
[24], &sc
->regs
.r24
);
3506 __get_user(env
->regs
[25], &sc
->regs
.r25
);
3507 __get_user(env
->regs
[26], &sc
->regs
.r26
);
3508 __get_user(env
->regs
[27], &sc
->regs
.r27
);
3509 __get_user(env
->regs
[28], &sc
->regs
.r28
);
3510 __get_user(env
->regs
[29], &sc
->regs
.r29
);
3511 __get_user(env
->regs
[30], &sc
->regs
.r30
);
3512 __get_user(env
->regs
[31], &sc
->regs
.r31
);
3513 __get_user(env
->sregs
[SR_PC
], &sc
->regs
.pc
);
3516 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3517 CPUMBState
*env
, int frame_size
)
3519 abi_ulong sp
= env
->regs
[1];
3521 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) != 0 && !on_sig_stack(sp
)) {
3522 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3525 return ((sp
- frame_size
) & -8UL);
3528 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3529 target_sigset_t
*set
, CPUMBState
*env
)
3531 struct target_signal_frame
*frame
;
3532 abi_ulong frame_addr
;
3535 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
3536 trace_user_setup_frame(env
, frame_addr
);
3537 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3540 /* Save the mask. */
3541 __put_user(set
->sig
[0], &frame
->uc
.tuc_mcontext
.oldmask
);
3543 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3544 __put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]);
3547 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
);
3549 /* Set up to return from userspace. If provided, use a stub
3550 already in userspace. */
3551 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3552 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3553 env
->regs
[15] = ((unsigned long)ka
->sa_restorer
)-8;
3556 /* Note, these encodings are _big endian_! */
3557 /* addi r12, r0, __NR_sigreturn */
3558 t
= 0x31800000UL
| TARGET_NR_sigreturn
;
3559 __put_user(t
, frame
->tramp
+ 0);
3562 __put_user(t
, frame
->tramp
+ 1);
3564 /* Return from sighandler will jump to the tramp.
3565 Negative 8 offset because return is rtsd r15, 8 */
3566 env
->regs
[15] = ((unsigned long)frame
->tramp
) - 8;
3569 /* Set up registers for signal handler */
3570 env
->regs
[1] = frame_addr
;
3571 /* Signal handler args: */
3572 env
->regs
[5] = sig
; /* Arg 0: signum */
3574 /* arg 1: sigcontext */
3575 env
->regs
[7] = frame_addr
+= offsetof(typeof(*frame
), uc
);
3577 /* Offset of 4 to handle microblaze rtid r14, 0 */
3578 env
->sregs
[SR_PC
] = (unsigned long)ka
->_sa_handler
;
3580 unlock_user_struct(frame
, frame_addr
, 1);
3583 force_sig(TARGET_SIGSEGV
);
3586 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3587 target_siginfo_t
*info
,
3588 target_sigset_t
*set
, CPUMBState
*env
)
3590 fprintf(stderr
, "Microblaze setup_rt_frame: not implemented\n");
3593 long do_sigreturn(CPUMBState
*env
)
3595 struct target_signal_frame
*frame
;
3596 abi_ulong frame_addr
;
3597 target_sigset_t target_set
;
3601 frame_addr
= env
->regs
[R_SP
];
3602 trace_user_do_sigreturn(env
, frame_addr
);
3603 /* Make sure the guest isn't playing games. */
3604 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
3607 /* Restore blocked signals */
3608 __get_user(target_set
.sig
[0], &frame
->uc
.tuc_mcontext
.oldmask
);
3609 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3610 __get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]);
3612 target_to_host_sigset_internal(&set
, &target_set
);
3613 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
3615 restore_sigcontext(&frame
->uc
.tuc_mcontext
, env
);
3616 /* We got here through a sigreturn syscall, our path back is via an
3617 rtb insn so setup r14 for that. */
3618 env
->regs
[14] = env
->sregs
[SR_PC
];
3620 unlock_user_struct(frame
, frame_addr
, 0);
3621 return env
->regs
[10];
3623 force_sig(TARGET_SIGSEGV
);
3626 long do_rt_sigreturn(CPUMBState
*env
)
3628 trace_user_do_rt_sigreturn(env
, 0);
3629 fprintf(stderr
, "Microblaze do_rt_sigreturn: not implemented\n");
3630 return -TARGET_ENOSYS
;
3633 #elif defined(TARGET_CRIS)
3635 struct target_sigcontext
{
3636 struct target_pt_regs regs
; /* needs to be first */
3638 uint32_t usp
; /* usp before stacking this gunk on it */
3641 /* Signal frames. */
3642 struct target_signal_frame
{
3643 struct target_sigcontext sc
;
3644 uint32_t extramask
[TARGET_NSIG_WORDS
- 1];
3645 uint16_t retcode
[4]; /* Trampoline code. */
3648 struct rt_signal_frame
{
3653 uint16_t retcode
[4]; /* Trampoline code. */
3656 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUCRISState
*env
)
3658 __put_user(env
->regs
[0], &sc
->regs
.r0
);
3659 __put_user(env
->regs
[1], &sc
->regs
.r1
);
3660 __put_user(env
->regs
[2], &sc
->regs
.r2
);
3661 __put_user(env
->regs
[3], &sc
->regs
.r3
);
3662 __put_user(env
->regs
[4], &sc
->regs
.r4
);
3663 __put_user(env
->regs
[5], &sc
->regs
.r5
);
3664 __put_user(env
->regs
[6], &sc
->regs
.r6
);
3665 __put_user(env
->regs
[7], &sc
->regs
.r7
);
3666 __put_user(env
->regs
[8], &sc
->regs
.r8
);
3667 __put_user(env
->regs
[9], &sc
->regs
.r9
);
3668 __put_user(env
->regs
[10], &sc
->regs
.r10
);
3669 __put_user(env
->regs
[11], &sc
->regs
.r11
);
3670 __put_user(env
->regs
[12], &sc
->regs
.r12
);
3671 __put_user(env
->regs
[13], &sc
->regs
.r13
);
3672 __put_user(env
->regs
[14], &sc
->usp
);
3673 __put_user(env
->regs
[15], &sc
->regs
.acr
);
3674 __put_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
3675 __put_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
3676 __put_user(env
->pc
, &sc
->regs
.erp
);
3679 static void restore_sigcontext(struct target_sigcontext
*sc
, CPUCRISState
*env
)
3681 __get_user(env
->regs
[0], &sc
->regs
.r0
);
3682 __get_user(env
->regs
[1], &sc
->regs
.r1
);
3683 __get_user(env
->regs
[2], &sc
->regs
.r2
);
3684 __get_user(env
->regs
[3], &sc
->regs
.r3
);
3685 __get_user(env
->regs
[4], &sc
->regs
.r4
);
3686 __get_user(env
->regs
[5], &sc
->regs
.r5
);
3687 __get_user(env
->regs
[6], &sc
->regs
.r6
);
3688 __get_user(env
->regs
[7], &sc
->regs
.r7
);
3689 __get_user(env
->regs
[8], &sc
->regs
.r8
);
3690 __get_user(env
->regs
[9], &sc
->regs
.r9
);
3691 __get_user(env
->regs
[10], &sc
->regs
.r10
);
3692 __get_user(env
->regs
[11], &sc
->regs
.r11
);
3693 __get_user(env
->regs
[12], &sc
->regs
.r12
);
3694 __get_user(env
->regs
[13], &sc
->regs
.r13
);
3695 __get_user(env
->regs
[14], &sc
->usp
);
3696 __get_user(env
->regs
[15], &sc
->regs
.acr
);
3697 __get_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
3698 __get_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
3699 __get_user(env
->pc
, &sc
->regs
.erp
);
3702 static abi_ulong
get_sigframe(CPUCRISState
*env
, int framesize
)
3705 /* Align the stack downwards to 4. */
3706 sp
= (env
->regs
[R_SP
] & ~3);
3707 return sp
- framesize
;
3710 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3711 target_sigset_t
*set
, CPUCRISState
*env
)
3713 struct target_signal_frame
*frame
;
3714 abi_ulong frame_addr
;
3717 frame_addr
= get_sigframe(env
, sizeof *frame
);
3718 trace_user_setup_frame(env
, frame_addr
);
3719 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3723 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3724 * use this trampoline anymore but it sets it up for GDB.
3725 * In QEMU, using the trampoline simplifies things a bit so we use it.
3727 * This is movu.w __NR_sigreturn, r9; break 13;
3729 __put_user(0x9c5f, frame
->retcode
+0);
3730 __put_user(TARGET_NR_sigreturn
,
3731 frame
->retcode
+ 1);
3732 __put_user(0xe93d, frame
->retcode
+ 2);
3734 /* Save the mask. */
3735 __put_user(set
->sig
[0], &frame
->sc
.oldmask
);
3737 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3738 __put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]);
3741 setup_sigcontext(&frame
->sc
, env
);
3743 /* Move the stack and setup the arguments for the handler. */
3744 env
->regs
[R_SP
] = frame_addr
;
3745 env
->regs
[10] = sig
;
3746 env
->pc
= (unsigned long) ka
->_sa_handler
;
3747 /* Link SRP so the guest returns through the trampoline. */
3748 env
->pregs
[PR_SRP
] = frame_addr
+ offsetof(typeof(*frame
), retcode
);
3750 unlock_user_struct(frame
, frame_addr
, 1);
3753 force_sig(TARGET_SIGSEGV
);
3756 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3757 target_siginfo_t
*info
,
3758 target_sigset_t
*set
, CPUCRISState
*env
)
3760 fprintf(stderr
, "CRIS setup_rt_frame: not implemented\n");
3763 long do_sigreturn(CPUCRISState
*env
)
3765 struct target_signal_frame
*frame
;
3766 abi_ulong frame_addr
;
3767 target_sigset_t target_set
;
3771 frame_addr
= env
->regs
[R_SP
];
3772 trace_user_do_sigreturn(env
, frame_addr
);
3773 /* Make sure the guest isn't playing games. */
3774 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1)) {
3778 /* Restore blocked signals */
3779 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
);
3780 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3781 __get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]);
3783 target_to_host_sigset_internal(&set
, &target_set
);
3784 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
3786 restore_sigcontext(&frame
->sc
, env
);
3787 unlock_user_struct(frame
, frame_addr
, 0);
3788 return -TARGET_QEMU_ESIGRETURN
;
3790 force_sig(TARGET_SIGSEGV
);
3793 long do_rt_sigreturn(CPUCRISState
*env
)
3795 trace_user_do_rt_sigreturn(env
, 0);
3796 fprintf(stderr
, "CRIS do_rt_sigreturn: not implemented\n");
3797 return -TARGET_ENOSYS
;
3800 #elif defined(TARGET_OPENRISC)
3802 struct target_sigcontext
{
3803 struct target_pt_regs regs
;
3808 struct target_ucontext
{
3809 abi_ulong tuc_flags
;
3811 target_stack_t tuc_stack
;
3812 struct target_sigcontext tuc_mcontext
;
3813 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
3816 struct target_rt_sigframe
{
3819 struct target_siginfo info
;
3820 struct target_sigcontext sc
;
3821 struct target_ucontext uc
;
3822 unsigned char retcode
[16]; /* trampoline code */
3825 /* This is the asm-generic/ucontext.h version */
3827 static int restore_sigcontext(CPUOpenRISCState
*regs
,
3828 struct target_sigcontext
*sc
)
3830 unsigned int err
= 0;
3831 unsigned long old_usp
;
3833 /* Alwys make any pending restarted system call return -EINTR */
3834 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
3836 /* restore the regs from &sc->regs (same as sc, since regs is first)
3837 * (sc is already checked for VERIFY_READ since the sigframe was
3838 * checked in sys_sigreturn previously)
3841 if (copy_from_user(regs
, &sc
, sizeof(struct target_pt_regs
))) {
3845 /* make sure the U-flag is set so user-mode cannot fool us */
3849 /* restore the old USP as it was before we stacked the sc etc.
3850 * (we cannot just pop the sigcontext since we aligned the sp and
3851 * stuff after pushing it)
3854 __get_user(old_usp
, &sc
->usp
);
3855 phx_signal("old_usp 0x%lx", old_usp
);
3857 __PHX__ REALLY
/* ??? */
3859 regs
->gpr
[1] = old_usp
;
3861 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3862 * after this completes, but we don't use that mechanism. maybe we can
3873 /* Set up a signal frame. */
3875 static void setup_sigcontext(struct target_sigcontext
*sc
,
3876 CPUOpenRISCState
*regs
,
3879 unsigned long usp
= regs
->gpr
[1];
3881 /* copy the regs. they are first in sc so we can use sc directly */
3883 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3885 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3886 the signal handler. The frametype will be restored to its previous
3887 value in restore_sigcontext. */
3888 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3890 /* then some other stuff */
3891 __put_user(mask
, &sc
->oldmask
);
3892 __put_user(usp
, &sc
->usp
);
3895 static inline unsigned long align_sigframe(unsigned long sp
)
3902 static inline abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3903 CPUOpenRISCState
*regs
,
3906 unsigned long sp
= regs
->gpr
[1];
3907 int onsigstack
= on_sig_stack(sp
);
3910 /* This is the X/Open sanctioned signal stack switching. */
3911 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) != 0 && !onsigstack
) {
3912 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3915 sp
= align_sigframe(sp
- frame_size
);
3918 * If we are on the alternate signal stack and would overflow it, don't.
3919 * Return an always-bogus address instead so we will die with SIGSEGV.
3922 if (onsigstack
&& !likely(on_sig_stack(sp
))) {
3929 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3930 target_siginfo_t
*info
,
3931 target_sigset_t
*set
, CPUOpenRISCState
*env
)
3934 abi_ulong frame_addr
;
3935 unsigned long return_ip
;
3936 struct target_rt_sigframe
*frame
;
3937 abi_ulong info_addr
, uc_addr
;
3939 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
3940 trace_user_setup_rt_frame(env
, frame_addr
);
3941 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
3945 info_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
3946 __put_user(info_addr
, &frame
->pinfo
);
3947 uc_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
3948 __put_user(uc_addr
, &frame
->puc
);
3950 if (ka
->sa_flags
& SA_SIGINFO
) {
3951 tswap_siginfo(&frame
->info
, info
);
3954 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3955 __put_user(0, &frame
->uc
.tuc_flags
);
3956 __put_user(0, &frame
->uc
.tuc_link
);
3957 __put_user(target_sigaltstack_used
.ss_sp
,
3958 &frame
->uc
.tuc_stack
.ss_sp
);
3959 __put_user(sas_ss_flags(env
->gpr
[1]), &frame
->uc
.tuc_stack
.ss_flags
);
3960 __put_user(target_sigaltstack_used
.ss_size
,
3961 &frame
->uc
.tuc_stack
.ss_size
);
3962 setup_sigcontext(&frame
->sc
, env
, set
->sig
[0]);
3964 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3966 /* trampoline - the desired return ip is the retcode itself */
3967 return_ip
= (unsigned long)&frame
->retcode
;
3968 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3969 __put_user(0xa960, (short *)(frame
->retcode
+ 0));
3970 __put_user(TARGET_NR_rt_sigreturn
, (short *)(frame
->retcode
+ 2));
3971 __put_user(0x20000001, (unsigned long *)(frame
->retcode
+ 4));
3972 __put_user(0x15000000, (unsigned long *)(frame
->retcode
+ 8));
3978 /* TODO what is the current->exec_domain stuff and invmap ? */
3980 /* Set up registers for signal handler */
3981 env
->pc
= (unsigned long)ka
->_sa_handler
; /* what we enter NOW */
3982 env
->gpr
[9] = (unsigned long)return_ip
; /* what we enter LATER */
3983 env
->gpr
[3] = (unsigned long)sig
; /* arg 1: signo */
3984 env
->gpr
[4] = (unsigned long)&frame
->info
; /* arg 2: (siginfo_t*) */
3985 env
->gpr
[5] = (unsigned long)&frame
->uc
; /* arg 3: ucontext */
3987 /* actually move the usp to reflect the stacked frame */
3988 env
->gpr
[1] = (unsigned long)frame
;
3993 unlock_user_struct(frame
, frame_addr
, 1);
3994 if (sig
== TARGET_SIGSEGV
) {
3995 ka
->_sa_handler
= TARGET_SIG_DFL
;
3997 force_sig(TARGET_SIGSEGV
);
4000 long do_sigreturn(CPUOpenRISCState
*env
)
4002 trace_user_do_sigreturn(env
, 0);
4003 fprintf(stderr
, "do_sigreturn: not implemented\n");
4004 return -TARGET_ENOSYS
;
4007 long do_rt_sigreturn(CPUOpenRISCState
*env
)
4009 trace_user_do_rt_sigreturn(env
, 0);
4010 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
4011 return -TARGET_ENOSYS
;
4013 /* TARGET_OPENRISC */
4015 #elif defined(TARGET_S390X)
4017 #define __NUM_GPRS 16
4018 #define __NUM_FPRS 16
4019 #define __NUM_ACRS 16
4021 #define S390_SYSCALL_SIZE 2
4022 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4024 #define _SIGCONTEXT_NSIG 64
4025 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4026 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4027 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4028 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4029 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4033 target_ulong gprs
[__NUM_GPRS
];
4034 unsigned int acrs
[__NUM_ACRS
];
4035 } target_s390_regs_common
;
4039 double fprs
[__NUM_FPRS
];
4040 } target_s390_fp_regs
;
4043 target_s390_regs_common regs
;
4044 target_s390_fp_regs fpregs
;
4047 struct target_sigcontext
{
4048 target_ulong oldmask
[_SIGCONTEXT_NSIG_WORDS
];
4049 target_sigregs
*sregs
;
4053 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
4054 struct target_sigcontext sc
;
4055 target_sigregs sregs
;
4057 uint8_t retcode
[S390_SYSCALL_SIZE
];
4060 struct target_ucontext
{
4061 target_ulong tuc_flags
;
4062 struct target_ucontext
*tuc_link
;
4063 target_stack_t tuc_stack
;
4064 target_sigregs tuc_mcontext
;
4065 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
4069 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
4070 uint8_t retcode
[S390_SYSCALL_SIZE
];
4071 struct target_siginfo info
;
4072 struct target_ucontext uc
;
4075 static inline abi_ulong
4076 get_sigframe(struct target_sigaction
*ka
, CPUS390XState
*env
, size_t frame_size
)
4080 /* Default to using normal stack */
4083 /* This is the X/Open sanctioned signal stack switching. */
4084 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
4085 if (!sas_ss_flags(sp
)) {
4086 sp
= target_sigaltstack_used
.ss_sp
+
4087 target_sigaltstack_used
.ss_size
;
4091 /* This is the legacy signal stack switching. */
4092 else if (/* FIXME !user_mode(regs) */ 0 &&
4093 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
4095 sp
= (abi_ulong
) ka
->sa_restorer
;
4098 return (sp
- frame_size
) & -8ul;
4101 static void save_sigregs(CPUS390XState
*env
, target_sigregs
*sregs
)
4104 //save_access_regs(current->thread.acrs); FIXME
4106 /* Copy a 'clean' PSW mask to the user to avoid leaking
4107 information about whether PER is currently on. */
4108 __put_user(env
->psw
.mask
, &sregs
->regs
.psw
.mask
);
4109 __put_user(env
->psw
.addr
, &sregs
->regs
.psw
.addr
);
4110 for (i
= 0; i
< 16; i
++) {
4111 __put_user(env
->regs
[i
], &sregs
->regs
.gprs
[i
]);
4113 for (i
= 0; i
< 16; i
++) {
4114 __put_user(env
->aregs
[i
], &sregs
->regs
.acrs
[i
]);
4117 * We have to store the fp registers to current->thread.fp_regs
4118 * to merge them with the emulated registers.
4120 //save_fp_regs(¤t->thread.fp_regs); FIXME
4121 for (i
= 0; i
< 16; i
++) {
4122 __put_user(get_freg(env
, i
)->ll
, &sregs
->fpregs
.fprs
[i
]);
4126 static void setup_frame(int sig
, struct target_sigaction
*ka
,
4127 target_sigset_t
*set
, CPUS390XState
*env
)
4130 abi_ulong frame_addr
;
4132 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
4133 trace_user_setup_frame(env
, frame_addr
);
4134 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
4138 __put_user(set
->sig
[0], &frame
->sc
.oldmask
[0]);
4140 save_sigregs(env
, &frame
->sregs
);
4142 __put_user((abi_ulong
)(unsigned long)&frame
->sregs
,
4143 (abi_ulong
*)&frame
->sc
.sregs
);
4145 /* Set up to return from userspace. If provided, use a stub
4146 already in userspace. */
4147 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
4148 env
->regs
[14] = (unsigned long)
4149 ka
->sa_restorer
| PSW_ADDR_AMODE
;
4151 env
->regs
[14] = (unsigned long)
4152 frame
->retcode
| PSW_ADDR_AMODE
;
4153 __put_user(S390_SYSCALL_OPCODE
| TARGET_NR_sigreturn
,
4154 (uint16_t *)(frame
->retcode
));
4157 /* Set up backchain. */
4158 __put_user(env
->regs
[15], (abi_ulong
*) frame
);
4160 /* Set up registers for signal handler */
4161 env
->regs
[15] = frame_addr
;
4162 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
4164 env
->regs
[2] = sig
; //map_signal(sig);
4165 env
->regs
[3] = frame_addr
+= offsetof(typeof(*frame
), sc
);
4167 /* We forgot to include these in the sigcontext.
4168 To avoid breaking binary compatibility, they are passed as args. */
4169 env
->regs
[4] = 0; // FIXME: no clue... current->thread.trap_no;
4170 env
->regs
[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4172 /* Place signal number on stack to allow backtrace from handler. */
4173 __put_user(env
->regs
[2], (int *) &frame
->signo
);
4174 unlock_user_struct(frame
, frame_addr
, 1);
4178 force_sig(TARGET_SIGSEGV
);
4181 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
4182 target_siginfo_t
*info
,
4183 target_sigset_t
*set
, CPUS390XState
*env
)
4187 abi_ulong frame_addr
;
4189 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
4190 trace_user_setup_rt_frame(env
, frame_addr
);
4191 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
4195 tswap_siginfo(&frame
->info
, info
);
4197 /* Create the ucontext. */
4198 __put_user(0, &frame
->uc
.tuc_flags
);
4199 __put_user((abi_ulong
)0, (abi_ulong
*)&frame
->uc
.tuc_link
);
4200 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->uc
.tuc_stack
.ss_sp
);
4201 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
4202 &frame
->uc
.tuc_stack
.ss_flags
);
4203 __put_user(target_sigaltstack_used
.ss_size
, &frame
->uc
.tuc_stack
.ss_size
);
4204 save_sigregs(env
, &frame
->uc
.tuc_mcontext
);
4205 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
4206 __put_user((abi_ulong
)set
->sig
[i
],
4207 (abi_ulong
*)&frame
->uc
.tuc_sigmask
.sig
[i
]);
4210 /* Set up to return from userspace. If provided, use a stub
4211 already in userspace. */
4212 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
4213 env
->regs
[14] = (unsigned long) ka
->sa_restorer
| PSW_ADDR_AMODE
;
4215 env
->regs
[14] = (unsigned long) frame
->retcode
| PSW_ADDR_AMODE
;
4216 __put_user(S390_SYSCALL_OPCODE
| TARGET_NR_rt_sigreturn
,
4217 (uint16_t *)(frame
->retcode
));
4220 /* Set up backchain. */
4221 __put_user(env
->regs
[15], (abi_ulong
*) frame
);
4223 /* Set up registers for signal handler */
4224 env
->regs
[15] = frame_addr
;
4225 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
4227 env
->regs
[2] = sig
; //map_signal(sig);
4228 env
->regs
[3] = frame_addr
+ offsetof(typeof(*frame
), info
);
4229 env
->regs
[4] = frame_addr
+ offsetof(typeof(*frame
), uc
);
4233 force_sig(TARGET_SIGSEGV
);
4237 restore_sigregs(CPUS390XState
*env
, target_sigregs
*sc
)
4242 for (i
= 0; i
< 16; i
++) {
4243 __get_user(env
->regs
[i
], &sc
->regs
.gprs
[i
]);
4246 __get_user(env
->psw
.mask
, &sc
->regs
.psw
.mask
);
4247 trace_user_s390x_restore_sigregs(env
, (unsigned long long)sc
->regs
.psw
.addr
,
4248 (unsigned long long)env
->psw
.addr
);
4249 __get_user(env
->psw
.addr
, &sc
->regs
.psw
.addr
);
4250 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4252 for (i
= 0; i
< 16; i
++) {
4253 __get_user(env
->aregs
[i
], &sc
->regs
.acrs
[i
]);
4255 for (i
= 0; i
< 16; i
++) {
4256 __get_user(get_freg(env
, i
)->ll
, &sc
->fpregs
.fprs
[i
]);
4262 long do_sigreturn(CPUS390XState
*env
)
4265 abi_ulong frame_addr
= env
->regs
[15];
4266 target_sigset_t target_set
;
4269 trace_user_do_sigreturn(env
, frame_addr
);
4270 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
4273 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
[0]);
4275 target_to_host_sigset_internal(&set
, &target_set
);
4276 do_sigprocmask(SIG_SETMASK
, &set
, NULL
); /* ~_BLOCKABLE? */
4278 if (restore_sigregs(env
, &frame
->sregs
)) {
4282 unlock_user_struct(frame
, frame_addr
, 0);
4283 return -TARGET_QEMU_ESIGRETURN
;
4286 force_sig(TARGET_SIGSEGV
);
4290 long do_rt_sigreturn(CPUS390XState
*env
)
4293 abi_ulong frame_addr
= env
->regs
[15];
4296 trace_user_do_rt_sigreturn(env
, frame_addr
);
4297 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
4300 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
4302 do_sigprocmask(SIG_SETMASK
, &set
, NULL
); /* ~_BLOCKABLE? */
4304 if (restore_sigregs(env
, &frame
->uc
.tuc_mcontext
)) {
4308 if (do_sigaltstack(frame_addr
+ offsetof(rt_sigframe
, uc
.tuc_stack
), 0,
4309 get_sp_from_cpustate(env
)) == -EFAULT
) {
4312 unlock_user_struct(frame
, frame_addr
, 0);
4313 return -TARGET_QEMU_ESIGRETURN
;
4316 unlock_user_struct(frame
, frame_addr
, 0);
4317 force_sig(TARGET_SIGSEGV
);
4321 #elif defined(TARGET_PPC)
4323 /* Size of dummy stack frame allocated when calling signal handler.
4324 See arch/powerpc/include/asm/ptrace.h. */
4325 #if defined(TARGET_PPC64)
4326 #define SIGNAL_FRAMESIZE 128
4328 #define SIGNAL_FRAMESIZE 64
4331 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4332 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4333 struct target_mcontext
{
4334 target_ulong mc_gregs
[48];
4335 /* Includes fpscr. */
4336 uint64_t mc_fregs
[33];
4337 target_ulong mc_pad
[2];
4338 /* We need to handle Altivec and SPE at the same time, which no
4339 kernel needs to do. Fortunately, the kernel defines this bit to
4340 be Altivec-register-large all the time, rather than trying to
4341 twiddle it based on the specific platform. */
4343 /* SPE vector registers. One extra for SPEFSCR. */
4345 /* Altivec vector registers. The packing of VSCR and VRSAVE
4346 varies depending on whether we're PPC64 or not: PPC64 splits
4347 them apart; PPC32 stuffs them together. */
4348 #if defined(TARGET_PPC64)
4349 #define QEMU_NVRREG 34
4351 #define QEMU_NVRREG 33
4353 ppc_avr_t altivec
[QEMU_NVRREG
];
4355 } mc_vregs
__attribute__((__aligned__(16)));
4358 /* See arch/powerpc/include/asm/sigcontext.h. */
4359 struct target_sigcontext
{
4360 target_ulong _unused
[4];
4362 #if defined(TARGET_PPC64)
4365 target_ulong handler
;
4366 target_ulong oldmask
;
4367 target_ulong regs
; /* struct pt_regs __user * */
4368 #if defined(TARGET_PPC64)
4369 struct target_mcontext mcontext
;
4373 /* Indices for target_mcontext.mc_gregs, below.
4374 See arch/powerpc/include/asm/ptrace.h for details. */
4410 TARGET_PT_ORIG_R3
= 34,
4415 /* Yes, there are two registers with #39. One is 64-bit only. */
4417 TARGET_PT_SOFTE
= 39,
4418 TARGET_PT_TRAP
= 40,
4420 TARGET_PT_DSISR
= 42,
4421 TARGET_PT_RESULT
= 43,
4422 TARGET_PT_REGS_COUNT
= 44
4426 struct target_ucontext
{
4427 target_ulong tuc_flags
;
4428 target_ulong tuc_link
; /* struct ucontext __user * */
4429 struct target_sigaltstack tuc_stack
;
4430 #if !defined(TARGET_PPC64)
4432 target_ulong tuc_regs
; /* struct mcontext __user *
4433 points to uc_mcontext field */
4435 target_sigset_t tuc_sigmask
;
4436 #if defined(TARGET_PPC64)
4437 target_sigset_t unused
[15]; /* Allow for uc_sigmask growth */
4438 struct target_sigcontext tuc_sigcontext
;
4440 int32_t tuc_maskext
[30];
4441 int32_t tuc_pad2
[3];
4442 struct target_mcontext tuc_mcontext
;
4446 /* See arch/powerpc/kernel/signal_32.c. */
4447 struct target_sigframe
{
4448 struct target_sigcontext sctx
;
4449 struct target_mcontext mctx
;
4453 #if defined(TARGET_PPC64)
4455 #define TARGET_TRAMP_SIZE 6
4457 struct target_rt_sigframe
{
4458 /* sys_rt_sigreturn requires the ucontext be the first field */
4459 struct target_ucontext uc
;
4460 target_ulong _unused
[2];
4461 uint32_t trampoline
[TARGET_TRAMP_SIZE
];
4462 target_ulong pinfo
; /* struct siginfo __user * */
4463 target_ulong puc
; /* void __user * */
4464 struct target_siginfo info
;
4465 /* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
4467 } __attribute__((aligned(16)));
4471 struct target_rt_sigframe
{
4472 struct target_siginfo info
;
4473 struct target_ucontext uc
;
4479 #if defined(TARGET_PPC64)
4481 struct target_func_ptr
{
4488 /* We use the mc_pad field for the signal return trampoline. */
4489 #define tramp mc_pad
4491 /* See arch/powerpc/kernel/signal.c. */
4492 static target_ulong
get_sigframe(struct target_sigaction
*ka
,
4496 target_ulong oldsp
, newsp
;
4498 oldsp
= env
->gpr
[1];
4500 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) &&
4501 (sas_ss_flags(oldsp
) == 0)) {
4502 oldsp
= (target_sigaltstack_used
.ss_sp
4503 + target_sigaltstack_used
.ss_size
);
4506 newsp
= (oldsp
- frame_size
) & ~0xFUL
;
4511 static void save_user_regs(CPUPPCState
*env
, struct target_mcontext
*frame
)
4513 target_ulong msr
= env
->msr
;
4515 target_ulong ccr
= 0;
4517 /* In general, the kernel attempts to be intelligent about what it
4518 needs to save for Altivec/FP/SPE registers. We don't care that
4519 much, so we just go ahead and save everything. */
4521 /* Save general registers. */
4522 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4523 __put_user(env
->gpr
[i
], &frame
->mc_gregs
[i
]);
4525 __put_user(env
->nip
, &frame
->mc_gregs
[TARGET_PT_NIP
]);
4526 __put_user(env
->ctr
, &frame
->mc_gregs
[TARGET_PT_CTR
]);
4527 __put_user(env
->lr
, &frame
->mc_gregs
[TARGET_PT_LNK
]);
4528 __put_user(env
->xer
, &frame
->mc_gregs
[TARGET_PT_XER
]);
4530 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
4531 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
4533 __put_user(ccr
, &frame
->mc_gregs
[TARGET_PT_CCR
]);
4535 /* Save Altivec registers if necessary. */
4536 if (env
->insns_flags
& PPC_ALTIVEC
) {
4537 for (i
= 0; i
< ARRAY_SIZE(env
->avr
); i
++) {
4538 ppc_avr_t
*avr
= &env
->avr
[i
];
4539 ppc_avr_t
*vreg
= &frame
->mc_vregs
.altivec
[i
];
4541 __put_user(avr
->u64
[0], &vreg
->u64
[0]);
4542 __put_user(avr
->u64
[1], &vreg
->u64
[1]);
4544 /* Set MSR_VR in the saved MSR value to indicate that
4545 frame->mc_vregs contains valid data. */
4547 __put_user((uint32_t)env
->spr
[SPR_VRSAVE
],
4548 &frame
->mc_vregs
.altivec
[32].u32
[3]);
4551 /* Save floating point registers. */
4552 if (env
->insns_flags
& PPC_FLOAT
) {
4553 for (i
= 0; i
< ARRAY_SIZE(env
->fpr
); i
++) {
4554 __put_user(env
->fpr
[i
], &frame
->mc_fregs
[i
]);
4556 __put_user((uint64_t) env
->fpscr
, &frame
->mc_fregs
[32]);
4559 /* Save SPE registers. The kernel only saves the high half. */
4560 if (env
->insns_flags
& PPC_SPE
) {
4561 #if defined(TARGET_PPC64)
4562 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4563 __put_user(env
->gpr
[i
] >> 32, &frame
->mc_vregs
.spe
[i
]);
4566 for (i
= 0; i
< ARRAY_SIZE(env
->gprh
); i
++) {
4567 __put_user(env
->gprh
[i
], &frame
->mc_vregs
.spe
[i
]);
4570 /* Set MSR_SPE in the saved MSR value to indicate that
4571 frame->mc_vregs contains valid data. */
4573 __put_user(env
->spe_fscr
, &frame
->mc_vregs
.spe
[32]);
4577 __put_user(msr
, &frame
->mc_gregs
[TARGET_PT_MSR
]);
4580 static void encode_trampoline(int sigret
, uint32_t *tramp
)
4582 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4584 __put_user(0x38000000 | sigret
, &tramp
[0]);
4585 __put_user(0x44000002, &tramp
[1]);
4589 static void restore_user_regs(CPUPPCState
*env
,
4590 struct target_mcontext
*frame
, int sig
)
4592 target_ulong save_r2
= 0;
4599 save_r2
= env
->gpr
[2];
4602 /* Restore general registers. */
4603 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4604 __get_user(env
->gpr
[i
], &frame
->mc_gregs
[i
]);
4606 __get_user(env
->nip
, &frame
->mc_gregs
[TARGET_PT_NIP
]);
4607 __get_user(env
->ctr
, &frame
->mc_gregs
[TARGET_PT_CTR
]);
4608 __get_user(env
->lr
, &frame
->mc_gregs
[TARGET_PT_LNK
]);
4609 __get_user(env
->xer
, &frame
->mc_gregs
[TARGET_PT_XER
]);
4610 __get_user(ccr
, &frame
->mc_gregs
[TARGET_PT_CCR
]);
4612 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
4613 env
->crf
[i
] = (ccr
>> (32 - ((i
+ 1) * 4))) & 0xf;
4617 env
->gpr
[2] = save_r2
;
4620 __get_user(msr
, &frame
->mc_gregs
[TARGET_PT_MSR
]);
4622 /* If doing signal return, restore the previous little-endian mode. */
4624 env
->msr
= (env
->msr
& ~MSR_LE
) | (msr
& MSR_LE
);
4626 /* Restore Altivec registers if necessary. */
4627 if (env
->insns_flags
& PPC_ALTIVEC
) {
4628 for (i
= 0; i
< ARRAY_SIZE(env
->avr
); i
++) {
4629 ppc_avr_t
*avr
= &env
->avr
[i
];
4630 ppc_avr_t
*vreg
= &frame
->mc_vregs
.altivec
[i
];
4632 __get_user(avr
->u64
[0], &vreg
->u64
[0]);
4633 __get_user(avr
->u64
[1], &vreg
->u64
[1]);
4635 /* Set MSR_VEC in the saved MSR value to indicate that
4636 frame->mc_vregs contains valid data. */
4637 __get_user(env
->spr
[SPR_VRSAVE
],
4638 (target_ulong
*)(&frame
->mc_vregs
.altivec
[32].u32
[3]));
4641 /* Restore floating point registers. */
4642 if (env
->insns_flags
& PPC_FLOAT
) {
4644 for (i
= 0; i
< ARRAY_SIZE(env
->fpr
); i
++) {
4645 __get_user(env
->fpr
[i
], &frame
->mc_fregs
[i
]);
4647 __get_user(fpscr
, &frame
->mc_fregs
[32]);
4648 env
->fpscr
= (uint32_t) fpscr
;
4651 /* Save SPE registers. The kernel only saves the high half. */
4652 if (env
->insns_flags
& PPC_SPE
) {
4653 #if defined(TARGET_PPC64)
4654 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4657 __get_user(hi
, &frame
->mc_vregs
.spe
[i
]);
4658 env
->gpr
[i
] = ((uint64_t)hi
<< 32) | ((uint32_t) env
->gpr
[i
]);
4661 for (i
= 0; i
< ARRAY_SIZE(env
->gprh
); i
++) {
4662 __get_user(env
->gprh
[i
], &frame
->mc_vregs
.spe
[i
]);
4665 __get_user(env
->spe_fscr
, &frame
->mc_vregs
.spe
[32]);
4669 static void setup_frame(int sig
, struct target_sigaction
*ka
,
4670 target_sigset_t
*set
, CPUPPCState
*env
)
4672 struct target_sigframe
*frame
;
4673 struct target_sigcontext
*sc
;
4674 target_ulong frame_addr
, newsp
;
4676 #if defined(TARGET_PPC64)
4677 struct image_info
*image
= ((TaskState
*)thread_cpu
->opaque
)->info
;
4680 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
4681 trace_user_setup_frame(env
, frame_addr
);
4682 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
4686 __put_user(ka
->_sa_handler
, &sc
->handler
);
4687 __put_user(set
->sig
[0], &sc
->oldmask
);
4688 #if TARGET_ABI_BITS == 64
4689 __put_user(set
->sig
[0] >> 32, &sc
->_unused
[3]);
4691 __put_user(set
->sig
[1], &sc
->_unused
[3]);
4693 __put_user(h2g(&frame
->mctx
), &sc
->regs
);
4694 __put_user(sig
, &sc
->signal
);
4696 /* Save user regs. */
4697 save_user_regs(env
, &frame
->mctx
);
4699 /* Construct the trampoline code on the stack. */
4700 encode_trampoline(TARGET_NR_sigreturn
, (uint32_t *)&frame
->mctx
.tramp
);
4702 /* The kernel checks for the presence of a VDSO here. We don't
4703 emulate a vdso, so use a sigreturn system call. */
4704 env
->lr
= (target_ulong
) h2g(frame
->mctx
.tramp
);
4706 /* Turn off all fp exceptions. */
4709 /* Create a stack frame for the caller of the handler. */
4710 newsp
= frame_addr
- SIGNAL_FRAMESIZE
;
4711 err
|= put_user(env
->gpr
[1], newsp
, target_ulong
);
4716 /* Set up registers for signal handler. */
4717 env
->gpr
[1] = newsp
;
4719 env
->gpr
[4] = frame_addr
+ offsetof(struct target_sigframe
, sctx
);
4721 #if defined(TARGET_PPC64)
4722 if (get_ppc64_abi(image
) < 2) {
4723 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4724 struct target_func_ptr
*handler
=
4725 (struct target_func_ptr
*)g2h(ka
->_sa_handler
);
4726 env
->nip
= tswapl(handler
->entry
);
4727 env
->gpr
[2] = tswapl(handler
->toc
);
4729 /* ELFv2 PPC64 function pointers are entry points, but R12
4730 * must also be set */
4731 env
->nip
= tswapl((target_ulong
) ka
->_sa_handler
);
4732 env
->gpr
[12] = env
->nip
;
4735 env
->nip
= (target_ulong
) ka
->_sa_handler
;
4738 /* Signal handlers are entered in big-endian mode. */
4739 env
->msr
&= ~MSR_LE
;
4741 unlock_user_struct(frame
, frame_addr
, 1);
4745 unlock_user_struct(frame
, frame_addr
, 1);
4746 force_sig(TARGET_SIGSEGV
);
4749 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
4750 target_siginfo_t
*info
,
4751 target_sigset_t
*set
, CPUPPCState
*env
)
4753 struct target_rt_sigframe
*rt_sf
;
4754 uint32_t *trampptr
= 0;
4755 struct target_mcontext
*mctx
= 0;
4756 target_ulong rt_sf_addr
, newsp
= 0;
4758 #if defined(TARGET_PPC64)
4759 struct image_info
*image
= ((TaskState
*)thread_cpu
->opaque
)->info
;
4762 rt_sf_addr
= get_sigframe(ka
, env
, sizeof(*rt_sf
));
4763 if (!lock_user_struct(VERIFY_WRITE
, rt_sf
, rt_sf_addr
, 1))
4766 tswap_siginfo(&rt_sf
->info
, info
);
4768 __put_user(0, &rt_sf
->uc
.tuc_flags
);
4769 __put_user(0, &rt_sf
->uc
.tuc_link
);
4770 __put_user((target_ulong
)target_sigaltstack_used
.ss_sp
,
4771 &rt_sf
->uc
.tuc_stack
.ss_sp
);
4772 __put_user(sas_ss_flags(env
->gpr
[1]),
4773 &rt_sf
->uc
.tuc_stack
.ss_flags
);
4774 __put_user(target_sigaltstack_used
.ss_size
,
4775 &rt_sf
->uc
.tuc_stack
.ss_size
);
4776 #if !defined(TARGET_PPC64)
4777 __put_user(h2g (&rt_sf
->uc
.tuc_mcontext
),
4778 &rt_sf
->uc
.tuc_regs
);
4780 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
4781 __put_user(set
->sig
[i
], &rt_sf
->uc
.tuc_sigmask
.sig
[i
]);
4784 #if defined(TARGET_PPC64)
4785 mctx
= &rt_sf
->uc
.tuc_sigcontext
.mcontext
;
4786 trampptr
= &rt_sf
->trampoline
[0];
4788 mctx
= &rt_sf
->uc
.tuc_mcontext
;
4789 trampptr
= (uint32_t *)&rt_sf
->uc
.tuc_mcontext
.tramp
;
4792 save_user_regs(env
, mctx
);
4793 encode_trampoline(TARGET_NR_rt_sigreturn
, trampptr
);
4795 /* The kernel checks for the presence of a VDSO here. We don't
4796 emulate a vdso, so use a sigreturn system call. */
4797 env
->lr
= (target_ulong
) h2g(trampptr
);
4799 /* Turn off all fp exceptions. */
4802 /* Create a stack frame for the caller of the handler. */
4803 newsp
= rt_sf_addr
- (SIGNAL_FRAMESIZE
+ 16);
4804 err
|= put_user(env
->gpr
[1], newsp
, target_ulong
);
4809 /* Set up registers for signal handler. */
4810 env
->gpr
[1] = newsp
;
4811 env
->gpr
[3] = (target_ulong
) sig
;
4812 env
->gpr
[4] = (target_ulong
) h2g(&rt_sf
->info
);
4813 env
->gpr
[5] = (target_ulong
) h2g(&rt_sf
->uc
);
4814 env
->gpr
[6] = (target_ulong
) h2g(rt_sf
);
4816 #if defined(TARGET_PPC64)
4817 if (get_ppc64_abi(image
) < 2) {
4818 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4819 struct target_func_ptr
*handler
=
4820 (struct target_func_ptr
*)g2h(ka
->_sa_handler
);
4821 env
->nip
= tswapl(handler
->entry
);
4822 env
->gpr
[2] = tswapl(handler
->toc
);
4824 /* ELFv2 PPC64 function pointers are entry points, but R12
4825 * must also be set */
4826 env
->nip
= tswapl((target_ulong
) ka
->_sa_handler
);
4827 env
->gpr
[12] = env
->nip
;
4830 env
->nip
= (target_ulong
) ka
->_sa_handler
;
4833 /* Signal handlers are entered in big-endian mode. */
4834 env
->msr
&= ~MSR_LE
;
4836 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4840 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4841 force_sig(TARGET_SIGSEGV
);
4845 long do_sigreturn(CPUPPCState
*env
)
4847 struct target_sigcontext
*sc
= NULL
;
4848 struct target_mcontext
*sr
= NULL
;
4849 target_ulong sr_addr
= 0, sc_addr
;
4851 target_sigset_t set
;
4853 sc_addr
= env
->gpr
[1] + SIGNAL_FRAMESIZE
;
4854 if (!lock_user_struct(VERIFY_READ
, sc
, sc_addr
, 1))
4857 #if defined(TARGET_PPC64)
4858 set
.sig
[0] = sc
->oldmask
+ ((uint64_t)(sc
->_unused
[3]) << 32);
4860 __get_user(set
.sig
[0], &sc
->oldmask
);
4861 __get_user(set
.sig
[1], &sc
->_unused
[3]);
4863 target_to_host_sigset_internal(&blocked
, &set
);
4864 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
4866 __get_user(sr_addr
, &sc
->regs
);
4867 if (!lock_user_struct(VERIFY_READ
, sr
, sr_addr
, 1))
4869 restore_user_regs(env
, sr
, 1);
4871 unlock_user_struct(sr
, sr_addr
, 1);
4872 unlock_user_struct(sc
, sc_addr
, 1);
4873 return -TARGET_QEMU_ESIGRETURN
;
4876 unlock_user_struct(sr
, sr_addr
, 1);
4877 unlock_user_struct(sc
, sc_addr
, 1);
4878 force_sig(TARGET_SIGSEGV
);
4882 /* See arch/powerpc/kernel/signal_32.c. */
4883 static int do_setcontext(struct target_ucontext
*ucp
, CPUPPCState
*env
, int sig
)
4885 struct target_mcontext
*mcp
;
4886 target_ulong mcp_addr
;
4888 target_sigset_t set
;
4890 if (copy_from_user(&set
, h2g(ucp
) + offsetof(struct target_ucontext
, tuc_sigmask
),
4894 #if defined(TARGET_PPC64)
4895 mcp_addr
= h2g(ucp
) +
4896 offsetof(struct target_ucontext
, tuc_sigcontext
.mcontext
);
4898 __get_user(mcp_addr
, &ucp
->tuc_regs
);
4901 if (!lock_user_struct(VERIFY_READ
, mcp
, mcp_addr
, 1))
4904 target_to_host_sigset_internal(&blocked
, &set
);
4905 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
4906 restore_user_regs(env
, mcp
, sig
);
4908 unlock_user_struct(mcp
, mcp_addr
, 1);
4912 long do_rt_sigreturn(CPUPPCState
*env
)
4914 struct target_rt_sigframe
*rt_sf
= NULL
;
4915 target_ulong rt_sf_addr
;
4917 rt_sf_addr
= env
->gpr
[1] + SIGNAL_FRAMESIZE
+ 16;
4918 if (!lock_user_struct(VERIFY_READ
, rt_sf
, rt_sf_addr
, 1))
4921 if (do_setcontext(&rt_sf
->uc
, env
, 1))
4924 do_sigaltstack(rt_sf_addr
4925 + offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
4928 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4929 return -TARGET_QEMU_ESIGRETURN
;
4932 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4933 force_sig(TARGET_SIGSEGV
);
4937 #elif defined(TARGET_M68K)
4939 struct target_sigcontext
{
4946 unsigned short sc_sr
;
4950 struct target_sigframe
4957 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
4958 struct target_sigcontext sc
;
4961 typedef int target_greg_t
;
4962 #define TARGET_NGREG 18
4963 typedef target_greg_t target_gregset_t
[TARGET_NGREG
];
4965 typedef struct target_fpregset
{
4968 } target_fpregset_t
;
4970 struct target_mcontext
{
4972 target_gregset_t gregs
;
4973 target_fpregset_t fpregs
;
4976 #define TARGET_MCONTEXT_VERSION 2
4978 struct target_ucontext
{
4979 abi_ulong tuc_flags
;
4981 target_stack_t tuc_stack
;
4982 struct target_mcontext tuc_mcontext
;
4983 abi_long tuc_filler
[80];
4984 target_sigset_t tuc_sigmask
;
4987 struct target_rt_sigframe
4994 struct target_siginfo info
;
4995 struct target_ucontext uc
;
4998 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUM68KState
*env
,
5001 __put_user(mask
, &sc
->sc_mask
);
5002 __put_user(env
->aregs
[7], &sc
->sc_usp
);
5003 __put_user(env
->dregs
[0], &sc
->sc_d0
);
5004 __put_user(env
->dregs
[1], &sc
->sc_d1
);
5005 __put_user(env
->aregs
[0], &sc
->sc_a0
);
5006 __put_user(env
->aregs
[1], &sc
->sc_a1
);
5007 __put_user(env
->sr
, &sc
->sc_sr
);
5008 __put_user(env
->pc
, &sc
->sc_pc
);
5012 restore_sigcontext(CPUM68KState
*env
, struct target_sigcontext
*sc
)
5016 __get_user(env
->aregs
[7], &sc
->sc_usp
);
5017 __get_user(env
->dregs
[0], &sc
->sc_d0
);
5018 __get_user(env
->dregs
[1], &sc
->sc_d1
);
5019 __get_user(env
->aregs
[0], &sc
->sc_a0
);
5020 __get_user(env
->aregs
[1], &sc
->sc_a1
);
5021 __get_user(env
->pc
, &sc
->sc_pc
);
5022 __get_user(temp
, &sc
->sc_sr
);
5023 env
->sr
= (env
->sr
& 0xff00) | (temp
& 0xff);
5027 * Determine which stack to use..
5029 static inline abi_ulong
5030 get_sigframe(struct target_sigaction
*ka
, CPUM68KState
*regs
,
5035 sp
= regs
->aregs
[7];
5037 /* This is the X/Open sanctioned signal stack switching. */
5038 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
5039 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
5042 return ((sp
- frame_size
) & -8UL);
5045 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5046 target_sigset_t
*set
, CPUM68KState
*env
)
5048 struct target_sigframe
*frame
;
5049 abi_ulong frame_addr
;
5050 abi_ulong retcode_addr
;
5054 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
5055 trace_user_setup_frame(env
, frame_addr
);
5056 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5060 __put_user(sig
, &frame
->sig
);
5062 sc_addr
= frame_addr
+ offsetof(struct target_sigframe
, sc
);
5063 __put_user(sc_addr
, &frame
->psc
);
5065 setup_sigcontext(&frame
->sc
, env
, set
->sig
[0]);
5067 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
5068 __put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]);
5071 /* Set up to return from userspace. */
5073 retcode_addr
= frame_addr
+ offsetof(struct target_sigframe
, retcode
);
5074 __put_user(retcode_addr
, &frame
->pretcode
);
5076 /* moveq #,d0; trap #0 */
5078 __put_user(0x70004e40 + (TARGET_NR_sigreturn
<< 16),
5079 (uint32_t *)(frame
->retcode
));
5081 /* Set up to return from userspace */
5083 env
->aregs
[7] = frame_addr
;
5084 env
->pc
= ka
->_sa_handler
;
5086 unlock_user_struct(frame
, frame_addr
, 1);
5090 force_sig(TARGET_SIGSEGV
);
5093 static inline int target_rt_setup_ucontext(struct target_ucontext
*uc
,
5096 target_greg_t
*gregs
= uc
->tuc_mcontext
.gregs
;
5098 __put_user(TARGET_MCONTEXT_VERSION
, &uc
->tuc_mcontext
.version
);
5099 __put_user(env
->dregs
[0], &gregs
[0]);
5100 __put_user(env
->dregs
[1], &gregs
[1]);
5101 __put_user(env
->dregs
[2], &gregs
[2]);
5102 __put_user(env
->dregs
[3], &gregs
[3]);
5103 __put_user(env
->dregs
[4], &gregs
[4]);
5104 __put_user(env
->dregs
[5], &gregs
[5]);
5105 __put_user(env
->dregs
[6], &gregs
[6]);
5106 __put_user(env
->dregs
[7], &gregs
[7]);
5107 __put_user(env
->aregs
[0], &gregs
[8]);
5108 __put_user(env
->aregs
[1], &gregs
[9]);
5109 __put_user(env
->aregs
[2], &gregs
[10]);
5110 __put_user(env
->aregs
[3], &gregs
[11]);
5111 __put_user(env
->aregs
[4], &gregs
[12]);
5112 __put_user(env
->aregs
[5], &gregs
[13]);
5113 __put_user(env
->aregs
[6], &gregs
[14]);
5114 __put_user(env
->aregs
[7], &gregs
[15]);
5115 __put_user(env
->pc
, &gregs
[16]);
5116 __put_user(env
->sr
, &gregs
[17]);
5121 static inline int target_rt_restore_ucontext(CPUM68KState
*env
,
5122 struct target_ucontext
*uc
)
5125 target_greg_t
*gregs
= uc
->tuc_mcontext
.gregs
;
5127 __get_user(temp
, &uc
->tuc_mcontext
.version
);
5128 if (temp
!= TARGET_MCONTEXT_VERSION
)
5131 /* restore passed registers */
5132 __get_user(env
->dregs
[0], &gregs
[0]);
5133 __get_user(env
->dregs
[1], &gregs
[1]);
5134 __get_user(env
->dregs
[2], &gregs
[2]);
5135 __get_user(env
->dregs
[3], &gregs
[3]);
5136 __get_user(env
->dregs
[4], &gregs
[4]);
5137 __get_user(env
->dregs
[5], &gregs
[5]);
5138 __get_user(env
->dregs
[6], &gregs
[6]);
5139 __get_user(env
->dregs
[7], &gregs
[7]);
5140 __get_user(env
->aregs
[0], &gregs
[8]);
5141 __get_user(env
->aregs
[1], &gregs
[9]);
5142 __get_user(env
->aregs
[2], &gregs
[10]);
5143 __get_user(env
->aregs
[3], &gregs
[11]);
5144 __get_user(env
->aregs
[4], &gregs
[12]);
5145 __get_user(env
->aregs
[5], &gregs
[13]);
5146 __get_user(env
->aregs
[6], &gregs
[14]);
5147 __get_user(env
->aregs
[7], &gregs
[15]);
5148 __get_user(env
->pc
, &gregs
[16]);
5149 __get_user(temp
, &gregs
[17]);
5150 env
->sr
= (env
->sr
& 0xff00) | (temp
& 0xff);
5158 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5159 target_siginfo_t
*info
,
5160 target_sigset_t
*set
, CPUM68KState
*env
)
5162 struct target_rt_sigframe
*frame
;
5163 abi_ulong frame_addr
;
5164 abi_ulong retcode_addr
;
5165 abi_ulong info_addr
;
5170 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
5171 trace_user_setup_rt_frame(env
, frame_addr
);
5172 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5176 __put_user(sig
, &frame
->sig
);
5178 info_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
5179 __put_user(info_addr
, &frame
->pinfo
);
5181 uc_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
5182 __put_user(uc_addr
, &frame
->puc
);
5184 tswap_siginfo(&frame
->info
, info
);
5186 /* Create the ucontext */
5188 __put_user(0, &frame
->uc
.tuc_flags
);
5189 __put_user(0, &frame
->uc
.tuc_link
);
5190 __put_user(target_sigaltstack_used
.ss_sp
,
5191 &frame
->uc
.tuc_stack
.ss_sp
);
5192 __put_user(sas_ss_flags(env
->aregs
[7]),
5193 &frame
->uc
.tuc_stack
.ss_flags
);
5194 __put_user(target_sigaltstack_used
.ss_size
,
5195 &frame
->uc
.tuc_stack
.ss_size
);
5196 err
|= target_rt_setup_ucontext(&frame
->uc
, env
);
5201 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
5202 __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
5205 /* Set up to return from userspace. */
5207 retcode_addr
= frame_addr
+ offsetof(struct target_sigframe
, retcode
);
5208 __put_user(retcode_addr
, &frame
->pretcode
);
5210 /* moveq #,d0; notb d0; trap #0 */
5212 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn
^ 0xff) << 16),
5213 (uint32_t *)(frame
->retcode
+ 0));
5214 __put_user(0x4e40, (uint16_t *)(frame
->retcode
+ 4));
5219 /* Set up to return from userspace */
5221 env
->aregs
[7] = frame_addr
;
5222 env
->pc
= ka
->_sa_handler
;
5224 unlock_user_struct(frame
, frame_addr
, 1);
5228 unlock_user_struct(frame
, frame_addr
, 1);
5229 force_sig(TARGET_SIGSEGV
);
5232 long do_sigreturn(CPUM68KState
*env
)
5234 struct target_sigframe
*frame
;
5235 abi_ulong frame_addr
= env
->aregs
[7] - 4;
5236 target_sigset_t target_set
;
5240 trace_user_do_sigreturn(env
, frame_addr
);
5241 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
5244 /* set blocked signals */
5246 __get_user(target_set
.sig
[0], &frame
->sc
.sc_mask
);
5248 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
5249 __get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]);
5252 target_to_host_sigset_internal(&set
, &target_set
);
5253 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5255 /* restore registers */
5257 restore_sigcontext(env
, &frame
->sc
);
5259 unlock_user_struct(frame
, frame_addr
, 0);
5260 return -TARGET_QEMU_ESIGRETURN
;
5263 force_sig(TARGET_SIGSEGV
);
5267 long do_rt_sigreturn(CPUM68KState
*env
)
5269 struct target_rt_sigframe
*frame
;
5270 abi_ulong frame_addr
= env
->aregs
[7] - 4;
5271 target_sigset_t target_set
;
5274 trace_user_do_rt_sigreturn(env
, frame_addr
);
5275 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
5278 target_to_host_sigset_internal(&set
, &target_set
);
5279 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5281 /* restore registers */
5283 if (target_rt_restore_ucontext(env
, &frame
->uc
))
5286 if (do_sigaltstack(frame_addr
+
5287 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
5288 0, get_sp_from_cpustate(env
)) == -EFAULT
)
5291 unlock_user_struct(frame
, frame_addr
, 0);
5292 return -TARGET_QEMU_ESIGRETURN
;
5295 unlock_user_struct(frame
, frame_addr
, 0);
5296 force_sig(TARGET_SIGSEGV
);
5300 #elif defined(TARGET_ALPHA)
5302 struct target_sigcontext
{
5303 abi_long sc_onstack
;
5307 abi_long sc_regs
[32];
5308 abi_long sc_ownedfp
;
5309 abi_long sc_fpregs
[32];
5311 abi_ulong sc_fp_control
;
5312 abi_ulong sc_reserved1
;
5313 abi_ulong sc_reserved2
;
5316 abi_ulong sc_traparg_a0
;
5317 abi_ulong sc_traparg_a1
;
5318 abi_ulong sc_traparg_a2
;
5319 abi_ulong sc_fp_trap_pc
;
5320 abi_ulong sc_fp_trigger_sum
;
5321 abi_ulong sc_fp_trigger_inst
;
5324 struct target_ucontext
{
5325 abi_ulong tuc_flags
;
5327 abi_ulong tuc_osf_sigmask
;
5328 target_stack_t tuc_stack
;
5329 struct target_sigcontext tuc_mcontext
;
5330 target_sigset_t tuc_sigmask
;
5333 struct target_sigframe
{
5334 struct target_sigcontext sc
;
5335 unsigned int retcode
[3];
5338 struct target_rt_sigframe
{
5339 target_siginfo_t info
;
5340 struct target_ucontext uc
;
5341 unsigned int retcode
[3];
5344 #define INSN_MOV_R30_R16 0x47fe0410
5345 #define INSN_LDI_R0 0x201f0000
5346 #define INSN_CALLSYS 0x00000083
5348 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUAlphaState
*env
,
5349 abi_ulong frame_addr
, target_sigset_t
*set
)
5353 __put_user(on_sig_stack(frame_addr
), &sc
->sc_onstack
);
5354 __put_user(set
->sig
[0], &sc
->sc_mask
);
5355 __put_user(env
->pc
, &sc
->sc_pc
);
5356 __put_user(8, &sc
->sc_ps
);
5358 for (i
= 0; i
< 31; ++i
) {
5359 __put_user(env
->ir
[i
], &sc
->sc_regs
[i
]);
5361 __put_user(0, &sc
->sc_regs
[31]);
5363 for (i
= 0; i
< 31; ++i
) {
5364 __put_user(env
->fir
[i
], &sc
->sc_fpregs
[i
]);
5366 __put_user(0, &sc
->sc_fpregs
[31]);
5367 __put_user(cpu_alpha_load_fpcr(env
), &sc
->sc_fpcr
);
5369 __put_user(0, &sc
->sc_traparg_a0
); /* FIXME */
5370 __put_user(0, &sc
->sc_traparg_a1
); /* FIXME */
5371 __put_user(0, &sc
->sc_traparg_a2
); /* FIXME */
5374 static void restore_sigcontext(CPUAlphaState
*env
,
5375 struct target_sigcontext
*sc
)
5380 __get_user(env
->pc
, &sc
->sc_pc
);
5382 for (i
= 0; i
< 31; ++i
) {
5383 __get_user(env
->ir
[i
], &sc
->sc_regs
[i
]);
5385 for (i
= 0; i
< 31; ++i
) {
5386 __get_user(env
->fir
[i
], &sc
->sc_fpregs
[i
]);
5389 __get_user(fpcr
, &sc
->sc_fpcr
);
5390 cpu_alpha_store_fpcr(env
, fpcr
);
5393 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
5395 unsigned long framesize
)
5397 abi_ulong sp
= env
->ir
[IR_SP
];
5399 /* This is the X/Open sanctioned signal stack switching. */
5400 if ((sa
->sa_flags
& TARGET_SA_ONSTACK
) != 0 && !sas_ss_flags(sp
)) {
5401 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
5403 return (sp
- framesize
) & -32;
5406 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5407 target_sigset_t
*set
, CPUAlphaState
*env
)
5409 abi_ulong frame_addr
, r26
;
5410 struct target_sigframe
*frame
;
5413 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
5414 trace_user_setup_frame(env
, frame_addr
);
5415 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5419 setup_sigcontext(&frame
->sc
, env
, frame_addr
, set
);
5421 if (ka
->sa_restorer
) {
5422 r26
= ka
->sa_restorer
;
5424 __put_user(INSN_MOV_R30_R16
, &frame
->retcode
[0]);
5425 __put_user(INSN_LDI_R0
+ TARGET_NR_sigreturn
,
5426 &frame
->retcode
[1]);
5427 __put_user(INSN_CALLSYS
, &frame
->retcode
[2]);
5432 unlock_user_struct(frame
, frame_addr
, 1);
5436 if (sig
== TARGET_SIGSEGV
) {
5437 ka
->_sa_handler
= TARGET_SIG_DFL
;
5439 force_sig(TARGET_SIGSEGV
);
5442 env
->ir
[IR_RA
] = r26
;
5443 env
->ir
[IR_PV
] = env
->pc
= ka
->_sa_handler
;
5444 env
->ir
[IR_A0
] = sig
;
5446 env
->ir
[IR_A2
] = frame_addr
+ offsetof(struct target_sigframe
, sc
);
5447 env
->ir
[IR_SP
] = frame_addr
;
5450 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5451 target_siginfo_t
*info
,
5452 target_sigset_t
*set
, CPUAlphaState
*env
)
5454 abi_ulong frame_addr
, r26
;
5455 struct target_rt_sigframe
*frame
;
5458 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
5459 trace_user_setup_rt_frame(env
, frame_addr
);
5460 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5464 tswap_siginfo(&frame
->info
, info
);
5466 __put_user(0, &frame
->uc
.tuc_flags
);
5467 __put_user(0, &frame
->uc
.tuc_link
);
5468 __put_user(set
->sig
[0], &frame
->uc
.tuc_osf_sigmask
);
5469 __put_user(target_sigaltstack_used
.ss_sp
,
5470 &frame
->uc
.tuc_stack
.ss_sp
);
5471 __put_user(sas_ss_flags(env
->ir
[IR_SP
]),
5472 &frame
->uc
.tuc_stack
.ss_flags
);
5473 __put_user(target_sigaltstack_used
.ss_size
,
5474 &frame
->uc
.tuc_stack
.ss_size
);
5475 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, frame_addr
, set
);
5476 for (i
= 0; i
< TARGET_NSIG_WORDS
; ++i
) {
5477 __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
5480 if (ka
->sa_restorer
) {
5481 r26
= ka
->sa_restorer
;
5483 __put_user(INSN_MOV_R30_R16
, &frame
->retcode
[0]);
5484 __put_user(INSN_LDI_R0
+ TARGET_NR_rt_sigreturn
,
5485 &frame
->retcode
[1]);
5486 __put_user(INSN_CALLSYS
, &frame
->retcode
[2]);
5493 if (sig
== TARGET_SIGSEGV
) {
5494 ka
->_sa_handler
= TARGET_SIG_DFL
;
5496 force_sig(TARGET_SIGSEGV
);
5499 env
->ir
[IR_RA
] = r26
;
5500 env
->ir
[IR_PV
] = env
->pc
= ka
->_sa_handler
;
5501 env
->ir
[IR_A0
] = sig
;
5502 env
->ir
[IR_A1
] = frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
5503 env
->ir
[IR_A2
] = frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
5504 env
->ir
[IR_SP
] = frame_addr
;
5507 long do_sigreturn(CPUAlphaState
*env
)
5509 struct target_sigcontext
*sc
;
5510 abi_ulong sc_addr
= env
->ir
[IR_A0
];
5511 target_sigset_t target_set
;
5514 if (!lock_user_struct(VERIFY_READ
, sc
, sc_addr
, 1)) {
5518 target_sigemptyset(&target_set
);
5519 __get_user(target_set
.sig
[0], &sc
->sc_mask
);
5521 target_to_host_sigset_internal(&set
, &target_set
);
5522 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5524 restore_sigcontext(env
, sc
);
5525 unlock_user_struct(sc
, sc_addr
, 0);
5526 return -TARGET_QEMU_ESIGRETURN
;
5529 force_sig(TARGET_SIGSEGV
);
5532 long do_rt_sigreturn(CPUAlphaState
*env
)
5534 abi_ulong frame_addr
= env
->ir
[IR_A0
];
5535 struct target_rt_sigframe
*frame
;
5538 trace_user_do_rt_sigreturn(env
, frame_addr
);
5539 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
5542 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
5543 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5545 restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
);
5546 if (do_sigaltstack(frame_addr
+ offsetof(struct target_rt_sigframe
,
5548 0, env
->ir
[IR_SP
]) == -EFAULT
) {
5552 unlock_user_struct(frame
, frame_addr
, 0);
5553 return -TARGET_QEMU_ESIGRETURN
;
5557 unlock_user_struct(frame
, frame_addr
, 0);
5558 force_sig(TARGET_SIGSEGV
);
5561 #elif defined(TARGET_TILEGX)
5563 struct target_sigcontext
{
5565 /* General-purpose registers. */
5566 abi_ulong gregs
[56];
5568 abi_ulong __gregs
[53];
5569 abi_ulong tp
; /* Aliases gregs[TREG_TP]. */
5570 abi_ulong sp
; /* Aliases gregs[TREG_SP]. */
5571 abi_ulong lr
; /* Aliases gregs[TREG_LR]. */
5574 abi_ulong pc
; /* Program counter. */
5575 abi_ulong ics
; /* In Interrupt Critical Section? */
5576 abi_ulong faultnum
; /* Fault number. */
5580 struct target_ucontext
{
5581 abi_ulong tuc_flags
;
5583 target_stack_t tuc_stack
;
5584 struct target_sigcontext tuc_mcontext
;
5585 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
5588 struct target_rt_sigframe
{
5589 unsigned char save_area
[16]; /* caller save area */
5590 struct target_siginfo info
;
5591 struct target_ucontext uc
;
5594 static void setup_sigcontext(struct target_sigcontext
*sc
,
5595 CPUArchState
*env
, int signo
)
5599 for (i
= 0; i
< TILEGX_R_COUNT
; ++i
) {
5600 __put_user(env
->regs
[i
], &sc
->gregs
[i
]);
5603 __put_user(env
->pc
, &sc
->pc
);
5604 __put_user(0, &sc
->ics
);
5605 __put_user(signo
, &sc
->faultnum
);
5608 static void restore_sigcontext(CPUTLGState
*env
, struct target_sigcontext
*sc
)
5612 for (i
= 0; i
< TILEGX_R_COUNT
; ++i
) {
5613 __get_user(env
->regs
[i
], &sc
->gregs
[i
]);
5616 __get_user(env
->pc
, &sc
->pc
);
5619 static abi_ulong
get_sigframe(struct target_sigaction
*ka
, CPUArchState
*env
,
5622 unsigned long sp
= env
->regs
[TILEGX_R_SP
];
5624 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- frame_size
))) {
5628 if ((ka
->sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
)) {
5629 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
5637 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5638 target_siginfo_t
*info
,
5639 target_sigset_t
*set
, CPUArchState
*env
)
5641 abi_ulong frame_addr
;
5642 struct target_rt_sigframe
*frame
;
5643 unsigned long restorer
;
5645 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
5646 trace_user_setup_rt_frame(env
, frame_addr
);
5647 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5651 /* Always write at least the signal number for the stack backtracer. */
5652 if (ka
->sa_flags
& TARGET_SA_SIGINFO
) {
5653 /* At sigreturn time, restore the callee-save registers too. */
5654 tswap_siginfo(&frame
->info
, info
);
5655 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
5657 __put_user(info
->si_signo
, &frame
->info
.si_signo
);
5660 /* Create the ucontext. */
5661 __put_user(0, &frame
->uc
.tuc_flags
);
5662 __put_user(0, &frame
->uc
.tuc_link
);
5663 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->uc
.tuc_stack
.ss_sp
);
5664 __put_user(sas_ss_flags(env
->regs
[TILEGX_R_SP
]),
5665 &frame
->uc
.tuc_stack
.ss_flags
);
5666 __put_user(target_sigaltstack_used
.ss_size
, &frame
->uc
.tuc_stack
.ss_size
);
5667 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, info
->si_signo
);
5669 restorer
= (unsigned long) do_rt_sigreturn
;
5670 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
5671 restorer
= (unsigned long) ka
->sa_restorer
;
5673 env
->pc
= (unsigned long) ka
->_sa_handler
;
5674 env
->regs
[TILEGX_R_SP
] = (unsigned long) frame
;
5675 env
->regs
[TILEGX_R_LR
] = restorer
;
5676 env
->regs
[0] = (unsigned long) sig
;
5677 env
->regs
[1] = (unsigned long) &frame
->info
;
5678 env
->regs
[2] = (unsigned long) &frame
->uc
;
5679 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
5681 unlock_user_struct(frame
, frame_addr
, 1);
5685 if (sig
== TARGET_SIGSEGV
) {
5686 ka
->_sa_handler
= TARGET_SIG_DFL
;
5688 force_sig(TARGET_SIGSEGV
/* , current */);
5691 long do_rt_sigreturn(CPUTLGState
*env
)
5693 abi_ulong frame_addr
= env
->regs
[TILEGX_R_SP
];
5694 struct target_rt_sigframe
*frame
;
5697 trace_user_do_rt_sigreturn(env
, frame_addr
);
5698 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
5701 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
5702 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5704 restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
);
5705 if (do_sigaltstack(frame_addr
+ offsetof(struct target_rt_sigframe
,
5707 0, env
->regs
[TILEGX_R_SP
]) == -EFAULT
) {
5711 unlock_user_struct(frame
, frame_addr
, 0);
5712 return -TARGET_QEMU_ESIGRETURN
;
5716 unlock_user_struct(frame
, frame_addr
, 0);
5717 force_sig(TARGET_SIGSEGV
);
5722 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5723 target_sigset_t
*set
, CPUArchState
*env
)
5725 fprintf(stderr
, "setup_frame: not implemented\n");
5728 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5729 target_siginfo_t
*info
,
5730 target_sigset_t
*set
, CPUArchState
*env
)
5732 fprintf(stderr
, "setup_rt_frame: not implemented\n");
5735 long do_sigreturn(CPUArchState
*env
)
5737 fprintf(stderr
, "do_sigreturn: not implemented\n");
5738 return -TARGET_ENOSYS
;
5741 long do_rt_sigreturn(CPUArchState
*env
)
5743 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
5744 return -TARGET_ENOSYS
;
5749 void process_pending_signals(CPUArchState
*cpu_env
)
5751 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
5754 sigset_t set
, old_set
;
5755 target_sigset_t target_old_set
;
5756 struct emulated_sigtable
*k
;
5757 struct target_sigaction
*sa
;
5759 TaskState
*ts
= cpu
->opaque
;
5761 if (!ts
->signal_pending
)
5764 /* FIXME: This is not threadsafe. */
5766 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
5771 /* if no signal is pending, just return */
5772 ts
->signal_pending
= 0;
5776 trace_user_handle_signal(cpu_env
, sig
);
5777 /* dequeue signal */
5783 sig
= gdb_handlesig(cpu
, sig
);
5786 handler
= TARGET_SIG_IGN
;
5788 sa
= &sigact_table
[sig
- 1];
5789 handler
= sa
->_sa_handler
;
5792 if (ts
->sigsegv_blocked
&& sig
== TARGET_SIGSEGV
) {
5793 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5794 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5795 * because it got a real MMU fault), and treat as if default handler.
5797 handler
= TARGET_SIG_DFL
;
5800 if (handler
== TARGET_SIG_DFL
) {
5801 /* default handler : ignore some signal. The other are job control or fatal */
5802 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
5803 kill(getpid(),SIGSTOP
);
5804 } else if (sig
!= TARGET_SIGCHLD
&&
5805 sig
!= TARGET_SIGURG
&&
5806 sig
!= TARGET_SIGWINCH
&&
5807 sig
!= TARGET_SIGCONT
) {
5810 } else if (handler
== TARGET_SIG_IGN
) {
5812 } else if (handler
== TARGET_SIG_ERR
) {
5815 /* compute the blocked signals during the handler execution */
5816 target_to_host_sigset(&set
, &sa
->sa_mask
);
5817 /* SA_NODEFER indicates that the current signal should not be
5818 blocked during the handler */
5819 if (!(sa
->sa_flags
& TARGET_SA_NODEFER
))
5820 sigaddset(&set
, target_to_host_signal(sig
));
5822 /* block signals in the handler using Linux */
5823 do_sigprocmask(SIG_BLOCK
, &set
, &old_set
);
5824 /* save the previous blocked signal state to restore it at the
5825 end of the signal execution (see do_sigreturn) */
5826 host_to_target_sigset_internal(&target_old_set
, &old_set
);
5828 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5829 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5831 CPUX86State
*env
= cpu_env
;
5832 if (env
->eflags
& VM_MASK
)
5833 save_v86_state(env
);
5836 /* prepare the stack frame of the virtual CPU */
5837 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \
5838 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
5839 /* These targets do not have traditional signals. */
5840 setup_rt_frame(sig
, sa
, &q
->info
, &target_old_set
, cpu_env
);
5842 if (sa
->sa_flags
& TARGET_SA_SIGINFO
)
5843 setup_rt_frame(sig
, sa
, &q
->info
, &target_old_set
, cpu_env
);
5845 setup_frame(sig
, sa
, &target_old_set
, cpu_env
);
5847 if (sa
->sa_flags
& TARGET_SA_RESETHAND
)
5848 sa
->_sa_handler
= TARGET_SIG_DFL
;
5851 free_sigqueue(cpu_env
, q
);