2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include <sys/ucontext.h>
27 #include <sys/resource.h>
30 #include "qemu-common.h"
31 #include "target_signal.h"
33 //#define DEBUG_SIGNAL
35 static struct target_sigaltstack target_sigaltstack_used
= {
38 .ss_flags
= TARGET_SS_DISABLE
,
41 static struct target_sigaction sigact_table
[TARGET_NSIG
];
43 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
46 static uint8_t host_to_target_signal_table
[_NSIG
] = {
47 [SIGHUP
] = TARGET_SIGHUP
,
48 [SIGINT
] = TARGET_SIGINT
,
49 [SIGQUIT
] = TARGET_SIGQUIT
,
50 [SIGILL
] = TARGET_SIGILL
,
51 [SIGTRAP
] = TARGET_SIGTRAP
,
52 [SIGABRT
] = TARGET_SIGABRT
,
53 /* [SIGIOT] = TARGET_SIGIOT,*/
54 [SIGBUS
] = TARGET_SIGBUS
,
55 [SIGFPE
] = TARGET_SIGFPE
,
56 [SIGKILL
] = TARGET_SIGKILL
,
57 [SIGUSR1
] = TARGET_SIGUSR1
,
58 [SIGSEGV
] = TARGET_SIGSEGV
,
59 [SIGUSR2
] = TARGET_SIGUSR2
,
60 [SIGPIPE
] = TARGET_SIGPIPE
,
61 [SIGALRM
] = TARGET_SIGALRM
,
62 [SIGTERM
] = TARGET_SIGTERM
,
64 [SIGSTKFLT
] = TARGET_SIGSTKFLT
,
66 [SIGCHLD
] = TARGET_SIGCHLD
,
67 [SIGCONT
] = TARGET_SIGCONT
,
68 [SIGSTOP
] = TARGET_SIGSTOP
,
69 [SIGTSTP
] = TARGET_SIGTSTP
,
70 [SIGTTIN
] = TARGET_SIGTTIN
,
71 [SIGTTOU
] = TARGET_SIGTTOU
,
72 [SIGURG
] = TARGET_SIGURG
,
73 [SIGXCPU
] = TARGET_SIGXCPU
,
74 [SIGXFSZ
] = TARGET_SIGXFSZ
,
75 [SIGVTALRM
] = TARGET_SIGVTALRM
,
76 [SIGPROF
] = TARGET_SIGPROF
,
77 [SIGWINCH
] = TARGET_SIGWINCH
,
78 [SIGIO
] = TARGET_SIGIO
,
79 [SIGPWR
] = TARGET_SIGPWR
,
80 [SIGSYS
] = TARGET_SIGSYS
,
81 /* next signals stay the same */
82 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
83 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
84 To fix this properly we need to do manual signal delivery multiplexed
85 over a single host signal. */
86 [__SIGRTMIN
] = __SIGRTMAX
,
87 [__SIGRTMAX
] = __SIGRTMIN
,
89 static uint8_t target_to_host_signal_table
[_NSIG
];
91 static inline int on_sig_stack(unsigned long sp
)
93 return (sp
- target_sigaltstack_used
.ss_sp
94 < target_sigaltstack_used
.ss_size
);
97 static inline int sas_ss_flags(unsigned long sp
)
99 return (target_sigaltstack_used
.ss_size
== 0 ? SS_DISABLE
100 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
103 int host_to_target_signal(int sig
)
105 if (sig
< 0 || sig
>= _NSIG
)
107 return host_to_target_signal_table
[sig
];
110 int target_to_host_signal(int sig
)
112 if (sig
< 0 || sig
>= _NSIG
)
114 return target_to_host_signal_table
[sig
];
117 static inline void target_sigemptyset(target_sigset_t
*set
)
119 memset(set
, 0, sizeof(*set
));
122 static inline void target_sigaddset(target_sigset_t
*set
, int signum
)
125 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
126 set
->sig
[signum
/ TARGET_NSIG_BPW
] |= mask
;
129 static inline int target_sigismember(const target_sigset_t
*set
, int signum
)
132 abi_ulong mask
= (abi_ulong
)1 << (signum
% TARGET_NSIG_BPW
);
133 return ((set
->sig
[signum
/ TARGET_NSIG_BPW
] & mask
) != 0);
136 static void host_to_target_sigset_internal(target_sigset_t
*d
,
140 target_sigemptyset(d
);
141 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
142 if (sigismember(s
, i
)) {
143 target_sigaddset(d
, host_to_target_signal(i
));
148 void host_to_target_sigset(target_sigset_t
*d
, const sigset_t
*s
)
153 host_to_target_sigset_internal(&d1
, s
);
154 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
155 d
->sig
[i
] = tswapal(d1
.sig
[i
]);
158 static void target_to_host_sigset_internal(sigset_t
*d
,
159 const target_sigset_t
*s
)
163 for (i
= 1; i
<= TARGET_NSIG
; i
++) {
164 if (target_sigismember(s
, i
)) {
165 sigaddset(d
, target_to_host_signal(i
));
170 void target_to_host_sigset(sigset_t
*d
, const target_sigset_t
*s
)
175 for(i
= 0;i
< TARGET_NSIG_WORDS
; i
++)
176 s1
.sig
[i
] = tswapal(s
->sig
[i
]);
177 target_to_host_sigset_internal(d
, &s1
);
180 void host_to_target_old_sigset(abi_ulong
*old_sigset
,
181 const sigset_t
*sigset
)
184 host_to_target_sigset(&d
, sigset
);
185 *old_sigset
= d
.sig
[0];
188 void target_to_host_old_sigset(sigset_t
*sigset
,
189 const abi_ulong
*old_sigset
)
194 d
.sig
[0] = *old_sigset
;
195 for(i
= 1;i
< TARGET_NSIG_WORDS
; i
++)
197 target_to_host_sigset(sigset
, &d
);
200 /* Wrapper for sigprocmask function
201 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
202 * are host signal set, not guest ones. This wraps the sigprocmask host calls
203 * that should be protected (calls originated from guest)
205 int do_sigprocmask(int how
, const sigset_t
*set
, sigset_t
*oldset
)
209 sigset_t
*temp
= NULL
;
210 CPUState
*cpu
= thread_cpu
;
211 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
212 bool segv_was_blocked
= ts
->sigsegv_blocked
;
215 bool has_sigsegv
= sigismember(set
, SIGSEGV
);
219 sigdelset(temp
, SIGSEGV
);
224 ts
->sigsegv_blocked
= true;
229 ts
->sigsegv_blocked
= false;
233 ts
->sigsegv_blocked
= has_sigsegv
;
236 g_assert_not_reached();
240 ret
= sigprocmask(how
, temp
, oldset
);
242 if (oldset
&& segv_was_blocked
) {
243 sigaddset(oldset
, SIGSEGV
);
249 /* siginfo conversion */
251 static inline void host_to_target_siginfo_noswap(target_siginfo_t
*tinfo
,
252 const siginfo_t
*info
)
254 int sig
= host_to_target_signal(info
->si_signo
);
255 tinfo
->si_signo
= sig
;
257 tinfo
->si_code
= info
->si_code
;
259 if (sig
== TARGET_SIGILL
|| sig
== TARGET_SIGFPE
|| sig
== TARGET_SIGSEGV
260 || sig
== TARGET_SIGBUS
|| sig
== TARGET_SIGTRAP
) {
261 /* Should never come here, but who knows. The information for
262 the target is irrelevant. */
263 tinfo
->_sifields
._sigfault
._addr
= 0;
264 } else if (sig
== TARGET_SIGIO
) {
265 tinfo
->_sifields
._sigpoll
._band
= info
->si_band
;
266 tinfo
->_sifields
._sigpoll
._fd
= info
->si_fd
;
267 } else if (sig
== TARGET_SIGCHLD
) {
268 tinfo
->_sifields
._sigchld
._pid
= info
->si_pid
;
269 tinfo
->_sifields
._sigchld
._uid
= info
->si_uid
;
270 tinfo
->_sifields
._sigchld
._status
271 = host_to_target_waitstatus(info
->si_status
);
272 tinfo
->_sifields
._sigchld
._utime
= info
->si_utime
;
273 tinfo
->_sifields
._sigchld
._stime
= info
->si_stime
;
274 } else if (sig
>= TARGET_SIGRTMIN
) {
275 tinfo
->_sifields
._rt
._pid
= info
->si_pid
;
276 tinfo
->_sifields
._rt
._uid
= info
->si_uid
;
277 /* XXX: potential problem if 64 bit */
278 tinfo
->_sifields
._rt
._sigval
.sival_ptr
279 = (abi_ulong
)(unsigned long)info
->si_value
.sival_ptr
;
283 static void tswap_siginfo(target_siginfo_t
*tinfo
,
284 const target_siginfo_t
*info
)
286 int sig
= info
->si_signo
;
287 tinfo
->si_signo
= tswap32(sig
);
288 tinfo
->si_errno
= tswap32(info
->si_errno
);
289 tinfo
->si_code
= tswap32(info
->si_code
);
291 if (sig
== TARGET_SIGILL
|| sig
== TARGET_SIGFPE
|| sig
== TARGET_SIGSEGV
292 || sig
== TARGET_SIGBUS
|| sig
== TARGET_SIGTRAP
) {
293 tinfo
->_sifields
._sigfault
._addr
294 = tswapal(info
->_sifields
._sigfault
._addr
);
295 } else if (sig
== TARGET_SIGIO
) {
296 tinfo
->_sifields
._sigpoll
._band
297 = tswap32(info
->_sifields
._sigpoll
._band
);
298 tinfo
->_sifields
._sigpoll
._fd
= tswap32(info
->_sifields
._sigpoll
._fd
);
299 } else if (sig
== TARGET_SIGCHLD
) {
300 tinfo
->_sifields
._sigchld
._pid
301 = tswap32(info
->_sifields
._sigchld
._pid
);
302 tinfo
->_sifields
._sigchld
._uid
303 = tswap32(info
->_sifields
._sigchld
._uid
);
304 tinfo
->_sifields
._sigchld
._status
305 = tswap32(info
->_sifields
._sigchld
._status
);
306 tinfo
->_sifields
._sigchld
._utime
307 = tswapal(info
->_sifields
._sigchld
._utime
);
308 tinfo
->_sifields
._sigchld
._stime
309 = tswapal(info
->_sifields
._sigchld
._stime
);
310 } else if (sig
>= TARGET_SIGRTMIN
) {
311 tinfo
->_sifields
._rt
._pid
= tswap32(info
->_sifields
._rt
._pid
);
312 tinfo
->_sifields
._rt
._uid
= tswap32(info
->_sifields
._rt
._uid
);
313 tinfo
->_sifields
._rt
._sigval
.sival_ptr
314 = tswapal(info
->_sifields
._rt
._sigval
.sival_ptr
);
319 void host_to_target_siginfo(target_siginfo_t
*tinfo
, const siginfo_t
*info
)
321 host_to_target_siginfo_noswap(tinfo
, info
);
322 tswap_siginfo(tinfo
, tinfo
);
325 /* XXX: we support only POSIX RT signals are used. */
326 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
327 void target_to_host_siginfo(siginfo_t
*info
, const target_siginfo_t
*tinfo
)
329 info
->si_signo
= tswap32(tinfo
->si_signo
);
330 info
->si_errno
= tswap32(tinfo
->si_errno
);
331 info
->si_code
= tswap32(tinfo
->si_code
);
332 info
->si_pid
= tswap32(tinfo
->_sifields
._rt
._pid
);
333 info
->si_uid
= tswap32(tinfo
->_sifields
._rt
._uid
);
334 info
->si_value
.sival_ptr
=
335 (void *)(long)tswapal(tinfo
->_sifields
._rt
._sigval
.sival_ptr
);
338 static int fatal_signal (int sig
)
343 case TARGET_SIGWINCH
:
344 /* Ignored by default. */
351 /* Job control signals. */
358 /* returns 1 if given signal should dump core if not handled */
359 static int core_dump_signal(int sig
)
375 void signal_init(void)
377 struct sigaction act
;
378 struct sigaction oact
;
382 /* generate signal conversion tables */
383 for(i
= 1; i
< _NSIG
; i
++) {
384 if (host_to_target_signal_table
[i
] == 0)
385 host_to_target_signal_table
[i
] = i
;
387 for(i
= 1; i
< _NSIG
; i
++) {
388 j
= host_to_target_signal_table
[i
];
389 target_to_host_signal_table
[j
] = i
;
392 /* set all host signal handlers. ALL signals are blocked during
393 the handlers to serialize them. */
394 memset(sigact_table
, 0, sizeof(sigact_table
));
396 sigfillset(&act
.sa_mask
);
397 act
.sa_flags
= SA_SIGINFO
;
398 act
.sa_sigaction
= host_signal_handler
;
399 for(i
= 1; i
<= TARGET_NSIG
; i
++) {
400 host_sig
= target_to_host_signal(i
);
401 sigaction(host_sig
, NULL
, &oact
);
402 if (oact
.sa_sigaction
== (void *)SIG_IGN
) {
403 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_IGN
;
404 } else if (oact
.sa_sigaction
== (void *)SIG_DFL
) {
405 sigact_table
[i
- 1]._sa_handler
= TARGET_SIG_DFL
;
407 /* If there's already a handler installed then something has
408 gone horribly wrong, so don't even try to handle that case. */
409 /* Install some handlers for our own use. We need at least
410 SIGSEGV and SIGBUS, to detect exceptions. We can not just
411 trap all signals because it affects syscall interrupt
412 behavior. But do trap all default-fatal signals. */
413 if (fatal_signal (i
))
414 sigaction(host_sig
, &act
, NULL
);
418 /* signal queue handling */
420 static inline struct sigqueue
*alloc_sigqueue(CPUArchState
*env
)
422 CPUState
*cpu
= ENV_GET_CPU(env
);
423 TaskState
*ts
= cpu
->opaque
;
424 struct sigqueue
*q
= ts
->first_free
;
427 ts
->first_free
= q
->next
;
431 static inline void free_sigqueue(CPUArchState
*env
, struct sigqueue
*q
)
433 CPUState
*cpu
= ENV_GET_CPU(env
);
434 TaskState
*ts
= cpu
->opaque
;
436 q
->next
= ts
->first_free
;
440 /* abort execution with signal */
441 static void QEMU_NORETURN
force_sig(int target_sig
)
443 CPUState
*cpu
= thread_cpu
;
444 CPUArchState
*env
= cpu
->env_ptr
;
445 TaskState
*ts
= (TaskState
*)cpu
->opaque
;
446 int host_sig
, core_dumped
= 0;
447 struct sigaction act
;
448 host_sig
= target_to_host_signal(target_sig
);
449 gdb_signalled(env
, target_sig
);
451 /* dump core if supported by target binary format */
452 if (core_dump_signal(target_sig
) && (ts
->bprm
->core_dump
!= NULL
)) {
455 ((*ts
->bprm
->core_dump
)(target_sig
, env
) == 0);
458 /* we already dumped the core of target process, we don't want
459 * a coredump of qemu itself */
460 struct rlimit nodump
;
461 getrlimit(RLIMIT_CORE
, &nodump
);
463 setrlimit(RLIMIT_CORE
, &nodump
);
464 (void) fprintf(stderr
, "qemu: uncaught target signal %d (%s) - %s\n",
465 target_sig
, strsignal(host_sig
), "core dumped" );
468 /* The proper exit code for dying from an uncaught signal is
469 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
470 * a negative value. To get the proper exit code we need to
471 * actually die from an uncaught signal. Here the default signal
472 * handler is installed, we send ourself a signal and we wait for
474 sigfillset(&act
.sa_mask
);
475 act
.sa_handler
= SIG_DFL
;
477 sigaction(host_sig
, &act
, NULL
);
479 /* For some reason raise(host_sig) doesn't send the signal when
480 * statically linked on x86-64. */
481 kill(getpid(), host_sig
);
483 /* Make sure the signal isn't masked (just reuse the mask inside
485 sigdelset(&act
.sa_mask
, host_sig
);
486 sigsuspend(&act
.sa_mask
);
492 /* queue a signal so that it will be send to the virtual CPU as soon
494 int queue_signal(CPUArchState
*env
, int sig
, target_siginfo_t
*info
)
496 CPUState
*cpu
= ENV_GET_CPU(env
);
497 TaskState
*ts
= cpu
->opaque
;
498 struct emulated_sigtable
*k
;
499 struct sigqueue
*q
, **pq
;
503 #if defined(DEBUG_SIGNAL)
504 fprintf(stderr
, "queue_signal: sig=%d\n",
507 k
= &ts
->sigtab
[sig
- 1];
508 queue
= gdb_queuesig ();
509 handler
= sigact_table
[sig
- 1]._sa_handler
;
511 if (ts
->sigsegv_blocked
&& sig
== TARGET_SIGSEGV
) {
512 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
513 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
514 * because it got a real MMU fault). A blocked SIGSEGV in that
515 * situation is treated as if using the default handler. This is
516 * not correct if some other process has randomly sent us a SIGSEGV
517 * via kill(), but that is not easy to distinguish at this point,
518 * so we assume it doesn't happen.
520 handler
= TARGET_SIG_DFL
;
523 if (!queue
&& handler
== TARGET_SIG_DFL
) {
524 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
525 kill(getpid(),SIGSTOP
);
528 /* default handler : ignore some signal. The other are fatal */
529 if (sig
!= TARGET_SIGCHLD
&&
530 sig
!= TARGET_SIGURG
&&
531 sig
!= TARGET_SIGWINCH
&&
532 sig
!= TARGET_SIGCONT
) {
535 return 0; /* indicate ignored */
537 } else if (!queue
&& handler
== TARGET_SIG_IGN
) {
540 } else if (!queue
&& handler
== TARGET_SIG_ERR
) {
544 if (sig
< TARGET_SIGRTMIN
) {
545 /* if non real time signal, we queue exactly one signal */
555 q
= alloc_sigqueue(env
);
566 /* signal that a new signal is pending */
567 ts
->signal_pending
= 1;
568 return 1; /* indicates that the signal was queued */
572 static void host_signal_handler(int host_signum
, siginfo_t
*info
,
575 CPUArchState
*env
= thread_cpu
->env_ptr
;
577 target_siginfo_t tinfo
;
579 /* the CPU emulator uses some host signals to detect exceptions,
580 we forward to it some signals */
581 if ((host_signum
== SIGSEGV
|| host_signum
== SIGBUS
)
582 && info
->si_code
> 0) {
583 if (cpu_signal_handler(host_signum
, info
, puc
))
587 /* get target signal number */
588 sig
= host_to_target_signal(host_signum
);
589 if (sig
< 1 || sig
> TARGET_NSIG
)
591 #if defined(DEBUG_SIGNAL)
592 fprintf(stderr
, "qemu: got signal %d\n", sig
);
594 host_to_target_siginfo_noswap(&tinfo
, info
);
595 if (queue_signal(env
, sig
, &tinfo
) == 1) {
596 /* interrupt the virtual CPU as soon as possible */
597 cpu_exit(thread_cpu
);
601 /* do_sigaltstack() returns target values and errnos. */
602 /* compare linux/kernel/signal.c:do_sigaltstack() */
603 abi_long
do_sigaltstack(abi_ulong uss_addr
, abi_ulong uoss_addr
, abi_ulong sp
)
606 struct target_sigaltstack oss
;
608 /* XXX: test errors */
611 __put_user(target_sigaltstack_used
.ss_sp
, &oss
.ss_sp
);
612 __put_user(target_sigaltstack_used
.ss_size
, &oss
.ss_size
);
613 __put_user(sas_ss_flags(sp
), &oss
.ss_flags
);
618 struct target_sigaltstack
*uss
;
619 struct target_sigaltstack ss
;
621 ret
= -TARGET_EFAULT
;
622 if (!lock_user_struct(VERIFY_READ
, uss
, uss_addr
, 1)
623 || __get_user(ss
.ss_sp
, &uss
->ss_sp
)
624 || __get_user(ss
.ss_size
, &uss
->ss_size
)
625 || __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
< MINSIGSTKSZ
)
648 target_sigaltstack_used
.ss_sp
= ss
.ss_sp
;
649 target_sigaltstack_used
.ss_size
= ss
.ss_size
;
653 ret
= -TARGET_EFAULT
;
654 if (copy_to_user(uoss_addr
, &oss
, sizeof(oss
)))
663 /* do_sigaction() return host values and errnos */
664 int do_sigaction(int sig
, const struct target_sigaction
*act
,
665 struct target_sigaction
*oact
)
667 struct target_sigaction
*k
;
668 struct sigaction act1
;
672 if (sig
< 1 || sig
> TARGET_NSIG
|| sig
== TARGET_SIGKILL
|| sig
== TARGET_SIGSTOP
)
674 k
= &sigact_table
[sig
- 1];
675 #if defined(DEBUG_SIGNAL)
676 fprintf(stderr
, "sigaction sig=%d act=0x%p, oact=0x%p\n",
680 __put_user(k
->_sa_handler
, &oact
->_sa_handler
);
681 __put_user(k
->sa_flags
, &oact
->sa_flags
);
682 #if !defined(TARGET_MIPS)
683 __put_user(k
->sa_restorer
, &oact
->sa_restorer
);
686 oact
->sa_mask
= k
->sa_mask
;
689 /* FIXME: This is not threadsafe. */
690 __get_user(k
->_sa_handler
, &act
->_sa_handler
);
691 __get_user(k
->sa_flags
, &act
->sa_flags
);
692 #if !defined(TARGET_MIPS)
693 __get_user(k
->sa_restorer
, &act
->sa_restorer
);
695 /* To be swapped in target_to_host_sigset. */
696 k
->sa_mask
= act
->sa_mask
;
698 /* we update the host linux signal state */
699 host_sig
= target_to_host_signal(sig
);
700 if (host_sig
!= SIGSEGV
&& host_sig
!= SIGBUS
) {
701 sigfillset(&act1
.sa_mask
);
702 act1
.sa_flags
= SA_SIGINFO
;
703 if (k
->sa_flags
& TARGET_SA_RESTART
)
704 act1
.sa_flags
|= SA_RESTART
;
705 /* NOTE: it is important to update the host kernel signal
706 ignore state to avoid getting unexpected interrupted
708 if (k
->_sa_handler
== TARGET_SIG_IGN
) {
709 act1
.sa_sigaction
= (void *)SIG_IGN
;
710 } else if (k
->_sa_handler
== TARGET_SIG_DFL
) {
711 if (fatal_signal (sig
))
712 act1
.sa_sigaction
= host_signal_handler
;
714 act1
.sa_sigaction
= (void *)SIG_DFL
;
716 act1
.sa_sigaction
= host_signal_handler
;
718 ret
= sigaction(host_sig
, &act1
, NULL
);
724 static inline int copy_siginfo_to_user(target_siginfo_t
*tinfo
,
725 const target_siginfo_t
*info
)
727 tswap_siginfo(tinfo
, info
);
731 static inline int current_exec_domain_sig(int sig
)
733 return /* current->exec_domain && current->exec_domain->signal_invmap
734 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig
;
737 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
739 /* from the Linux kernel */
741 struct target_fpreg
{
742 uint16_t significand
[4];
746 struct target_fpxreg
{
747 uint16_t significand
[4];
752 struct target_xmmreg
{
753 abi_ulong element
[4];
756 struct target_fpstate
{
757 /* Regular FPU environment */
765 struct target_fpreg _st
[8];
767 uint16_t magic
; /* 0xffff = regular FPU data only */
769 /* FXSR FPU environment */
770 abi_ulong _fxsr_env
[6]; /* FXSR FPU env is ignored */
773 struct target_fpxreg _fxsr_st
[8]; /* FXSR FPU reg data is ignored */
774 struct target_xmmreg _xmm
[8];
775 abi_ulong padding
[56];
778 #define X86_FXSR_MAGIC 0x0000
780 struct target_sigcontext
{
798 abi_ulong esp_at_signal
;
800 abi_ulong fpstate
; /* pointer */
805 struct target_ucontext
{
808 target_stack_t tuc_stack
;
809 struct target_sigcontext tuc_mcontext
;
810 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
817 struct target_sigcontext sc
;
818 struct target_fpstate fpstate
;
819 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
829 struct target_siginfo info
;
830 struct target_ucontext uc
;
831 struct target_fpstate fpstate
;
836 * Set up a signal frame.
839 /* XXX: save x87 state */
841 setup_sigcontext(struct target_sigcontext
*sc
, struct target_fpstate
*fpstate
,
842 CPUX86State
*env
, abi_ulong mask
, abi_ulong fpstate_addr
)
844 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
848 /* already locked in setup_frame() */
849 err
|= __put_user(env
->segs
[R_GS
].selector
, (unsigned int *)&sc
->gs
);
850 err
|= __put_user(env
->segs
[R_FS
].selector
, (unsigned int *)&sc
->fs
);
851 err
|= __put_user(env
->segs
[R_ES
].selector
, (unsigned int *)&sc
->es
);
852 err
|= __put_user(env
->segs
[R_DS
].selector
, (unsigned int *)&sc
->ds
);
853 err
|= __put_user(env
->regs
[R_EDI
], &sc
->edi
);
854 err
|= __put_user(env
->regs
[R_ESI
], &sc
->esi
);
855 err
|= __put_user(env
->regs
[R_EBP
], &sc
->ebp
);
856 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp
);
857 err
|= __put_user(env
->regs
[R_EBX
], &sc
->ebx
);
858 err
|= __put_user(env
->regs
[R_EDX
], &sc
->edx
);
859 err
|= __put_user(env
->regs
[R_ECX
], &sc
->ecx
);
860 err
|= __put_user(env
->regs
[R_EAX
], &sc
->eax
);
861 err
|= __put_user(cs
->exception_index
, &sc
->trapno
);
862 err
|= __put_user(env
->error_code
, &sc
->err
);
863 err
|= __put_user(env
->eip
, &sc
->eip
);
864 err
|= __put_user(env
->segs
[R_CS
].selector
, (unsigned int *)&sc
->cs
);
865 err
|= __put_user(env
->eflags
, &sc
->eflags
);
866 err
|= __put_user(env
->regs
[R_ESP
], &sc
->esp_at_signal
);
867 err
|= __put_user(env
->segs
[R_SS
].selector
, (unsigned int *)&sc
->ss
);
869 cpu_x86_fsave(env
, fpstate_addr
, 1);
870 fpstate
->status
= fpstate
->sw
;
872 err
|= __put_user(magic
, &fpstate
->magic
);
873 err
|= __put_user(fpstate_addr
, &sc
->fpstate
);
875 /* non-iBCS2 extensions.. */
876 err
|= __put_user(mask
, &sc
->oldmask
);
877 err
|= __put_user(env
->cr
[2], &sc
->cr2
);
882 * Determine which stack to use..
885 static inline abi_ulong
886 get_sigframe(struct target_sigaction
*ka
, CPUX86State
*env
, size_t frame_size
)
890 /* Default to using normal stack */
891 esp
= env
->regs
[R_ESP
];
892 /* This is the X/Open sanctioned signal stack switching. */
893 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
894 if (sas_ss_flags(esp
) == 0)
895 esp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
898 /* This is the legacy signal stack switching. */
900 if ((env
->segs
[R_SS
].selector
& 0xffff) != __USER_DS
&&
901 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
903 esp
= (unsigned long) ka
->sa_restorer
;
905 return (esp
- frame_size
) & -8ul;
908 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
909 static void setup_frame(int sig
, struct target_sigaction
*ka
,
910 target_sigset_t
*set
, CPUX86State
*env
)
912 abi_ulong frame_addr
;
913 struct sigframe
*frame
;
916 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
918 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
921 err
|= __put_user(current_exec_domain_sig(sig
),
926 setup_sigcontext(&frame
->sc
, &frame
->fpstate
, env
, set
->sig
[0],
927 frame_addr
+ offsetof(struct sigframe
, fpstate
));
931 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
932 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
936 /* Set up to return from userspace. If provided, use a stub
937 already in userspace. */
938 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
939 err
|= __put_user(ka
->sa_restorer
, &frame
->pretcode
);
942 abi_ulong retcode_addr
;
943 retcode_addr
= frame_addr
+ offsetof(struct sigframe
, retcode
);
944 err
|= __put_user(retcode_addr
, &frame
->pretcode
);
945 /* This is popl %eax ; movl $,%eax ; int $0x80 */
947 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+0));
948 err
|= __put_user(TARGET_NR_sigreturn
, (int *)(frame
->retcode
+2));
950 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+6));
956 /* Set up registers for signal handler */
957 env
->regs
[R_ESP
] = frame_addr
;
958 env
->eip
= ka
->_sa_handler
;
960 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
961 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
962 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
963 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
964 env
->eflags
&= ~TF_MASK
;
966 unlock_user_struct(frame
, frame_addr
, 1);
971 unlock_user_struct(frame
, frame_addr
, 1);
972 if (sig
== TARGET_SIGSEGV
)
973 ka
->_sa_handler
= TARGET_SIG_DFL
;
974 force_sig(TARGET_SIGSEGV
/* , current */);
977 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
978 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
979 target_siginfo_t
*info
,
980 target_sigset_t
*set
, CPUX86State
*env
)
982 abi_ulong frame_addr
, addr
;
983 struct rt_sigframe
*frame
;
986 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
988 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
991 err
|= __put_user(current_exec_domain_sig(sig
),
993 addr
= frame_addr
+ offsetof(struct rt_sigframe
, info
);
994 err
|= __put_user(addr
, &frame
->pinfo
);
995 addr
= frame_addr
+ offsetof(struct rt_sigframe
, uc
);
996 err
|= __put_user(addr
, &frame
->puc
);
997 err
|= copy_siginfo_to_user(&frame
->info
, info
);
1001 /* Create the ucontext. */
1002 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
1003 err
|= __put_user(0, &frame
->uc
.tuc_link
);
1004 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
1005 &frame
->uc
.tuc_stack
.ss_sp
);
1006 err
|= __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
1007 &frame
->uc
.tuc_stack
.ss_flags
);
1008 err
|= __put_user(target_sigaltstack_used
.ss_size
,
1009 &frame
->uc
.tuc_stack
.ss_size
);
1010 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, &frame
->fpstate
,
1012 frame_addr
+ offsetof(struct rt_sigframe
, fpstate
));
1013 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1014 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
1018 /* Set up to return from userspace. If provided, use a stub
1019 already in userspace. */
1020 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1021 err
|= __put_user(ka
->sa_restorer
, &frame
->pretcode
);
1024 addr
= frame_addr
+ offsetof(struct rt_sigframe
, retcode
);
1025 err
|= __put_user(addr
, &frame
->pretcode
);
1026 /* This is movl $,%eax ; int $0x80 */
1027 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
1028 err
|= __put_user(TARGET_NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
1030 err
|= __put_user(val16
, (uint16_t *)(frame
->retcode
+5));
1036 /* Set up registers for signal handler */
1037 env
->regs
[R_ESP
] = frame_addr
;
1038 env
->eip
= ka
->_sa_handler
;
1040 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1041 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1042 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1043 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1044 env
->eflags
&= ~TF_MASK
;
1046 unlock_user_struct(frame
, frame_addr
, 1);
1051 unlock_user_struct(frame
, frame_addr
, 1);
1052 if (sig
== TARGET_SIGSEGV
)
1053 ka
->_sa_handler
= TARGET_SIG_DFL
;
1054 force_sig(TARGET_SIGSEGV
/* , current */);
1058 restore_sigcontext(CPUX86State
*env
, struct target_sigcontext
*sc
, int *peax
)
1060 unsigned int err
= 0;
1061 abi_ulong fpstate_addr
;
1062 unsigned int tmpflags
;
1064 cpu_x86_load_seg(env
, R_GS
, tswap16(sc
->gs
));
1065 cpu_x86_load_seg(env
, R_FS
, tswap16(sc
->fs
));
1066 cpu_x86_load_seg(env
, R_ES
, tswap16(sc
->es
));
1067 cpu_x86_load_seg(env
, R_DS
, tswap16(sc
->ds
));
1069 env
->regs
[R_EDI
] = tswapl(sc
->edi
);
1070 env
->regs
[R_ESI
] = tswapl(sc
->esi
);
1071 env
->regs
[R_EBP
] = tswapl(sc
->ebp
);
1072 env
->regs
[R_ESP
] = tswapl(sc
->esp
);
1073 env
->regs
[R_EBX
] = tswapl(sc
->ebx
);
1074 env
->regs
[R_EDX
] = tswapl(sc
->edx
);
1075 env
->regs
[R_ECX
] = tswapl(sc
->ecx
);
1076 env
->eip
= tswapl(sc
->eip
);
1078 cpu_x86_load_seg(env
, R_CS
, lduw_p(&sc
->cs
) | 3);
1079 cpu_x86_load_seg(env
, R_SS
, lduw_p(&sc
->ss
) | 3);
1081 tmpflags
= tswapl(sc
->eflags
);
1082 env
->eflags
= (env
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
1083 // regs->orig_eax = -1; /* disable syscall checks */
1085 fpstate_addr
= tswapl(sc
->fpstate
);
1086 if (fpstate_addr
!= 0) {
1087 if (!access_ok(VERIFY_READ
, fpstate_addr
,
1088 sizeof(struct target_fpstate
)))
1090 cpu_x86_frstor(env
, fpstate_addr
, 1);
1093 *peax
= tswapl(sc
->eax
);
1099 long do_sigreturn(CPUX86State
*env
)
1101 struct sigframe
*frame
;
1102 abi_ulong frame_addr
= env
->regs
[R_ESP
] - 8;
1103 target_sigset_t target_set
;
1107 #if defined(DEBUG_SIGNAL)
1108 fprintf(stderr
, "do_sigreturn\n");
1110 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1112 /* set blocked signals */
1113 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
1115 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1116 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
1120 target_to_host_sigset_internal(&set
, &target_set
);
1121 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
1123 /* restore registers */
1124 if (restore_sigcontext(env
, &frame
->sc
, &eax
))
1126 unlock_user_struct(frame
, frame_addr
, 0);
1130 unlock_user_struct(frame
, frame_addr
, 0);
1131 force_sig(TARGET_SIGSEGV
);
1135 long do_rt_sigreturn(CPUX86State
*env
)
1137 abi_ulong frame_addr
;
1138 struct rt_sigframe
*frame
;
1142 frame_addr
= env
->regs
[R_ESP
] - 4;
1143 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1145 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
1146 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
1148 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
, &eax
))
1151 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe
, uc
.tuc_stack
), 0,
1152 get_sp_from_cpustate(env
)) == -EFAULT
)
1155 unlock_user_struct(frame
, frame_addr
, 0);
1159 unlock_user_struct(frame
, frame_addr
, 0);
1160 force_sig(TARGET_SIGSEGV
);
1164 #elif defined(TARGET_AARCH64)
1166 struct target_sigcontext
{
1167 uint64_t fault_address
;
1168 /* AArch64 registers */
1173 /* 4K reserved for FP/SIMD state and future expansion */
1174 char __reserved
[4096] __attribute__((__aligned__(16)));
1177 struct target_ucontext
{
1178 abi_ulong tuc_flags
;
1180 target_stack_t tuc_stack
;
1181 target_sigset_t tuc_sigmask
;
1182 /* glibc uses a 1024-bit sigset_t */
1183 char __unused
[1024 / 8 - sizeof(target_sigset_t
)];
1184 /* last for future expansion */
1185 struct target_sigcontext tuc_mcontext
;
1189 * Header to be used at the beginning of structures extending the user
1190 * context. Such structures must be placed after the rt_sigframe on the stack
1191 * and be 16-byte aligned. The last structure must be a dummy one with the
1192 * magic and size set to 0.
1194 struct target_aarch64_ctx
{
1199 #define TARGET_FPSIMD_MAGIC 0x46508001
1201 struct target_fpsimd_context
{
1202 struct target_aarch64_ctx head
;
1205 uint64_t vregs
[32 * 2]; /* really uint128_t vregs[32] */
1209 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1210 * user space as it will change with the addition of new context. User space
1211 * should check the magic/size information.
1213 struct target_aux_context
{
1214 struct target_fpsimd_context fpsimd
;
1215 /* additional context to be added before "end" */
1216 struct target_aarch64_ctx end
;
1219 struct target_rt_sigframe
{
1220 struct target_siginfo info
;
1221 struct target_ucontext uc
;
1227 static int target_setup_sigframe(struct target_rt_sigframe
*sf
,
1228 CPUARMState
*env
, target_sigset_t
*set
)
1231 struct target_aux_context
*aux
=
1232 (struct target_aux_context
*)sf
->uc
.tuc_mcontext
.__reserved
;
1234 /* set up the stack frame for unwinding */
1235 __put_user(env
->xregs
[29], &sf
->fp
);
1236 __put_user(env
->xregs
[30], &sf
->lr
);
1238 for (i
= 0; i
< 31; i
++) {
1239 __put_user(env
->xregs
[i
], &sf
->uc
.tuc_mcontext
.regs
[i
]);
1241 __put_user(env
->xregs
[31], &sf
->uc
.tuc_mcontext
.sp
);
1242 __put_user(env
->pc
, &sf
->uc
.tuc_mcontext
.pc
);
1243 __put_user(pstate_read(env
), &sf
->uc
.tuc_mcontext
.pstate
);
1245 __put_user(/*current->thread.fault_address*/ 0,
1246 &sf
->uc
.tuc_mcontext
.fault_address
);
1248 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1249 __put_user(set
->sig
[i
], &sf
->uc
.tuc_sigmask
.sig
[i
]);
1252 for (i
= 0; i
< 32; i
++) {
1253 #ifdef TARGET_WORDS_BIGENDIAN
1254 __put_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1255 __put_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2]);
1257 __put_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2]);
1258 __put_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1261 __put_user(vfp_get_fpsr(env
), &aux
->fpsimd
.fpsr
);
1262 __put_user(vfp_get_fpcr(env
), &aux
->fpsimd
.fpcr
);
1263 __put_user(TARGET_FPSIMD_MAGIC
, &aux
->fpsimd
.head
.magic
);
1264 __put_user(sizeof(struct target_fpsimd_context
),
1265 &aux
->fpsimd
.head
.size
);
1267 /* set the "end" magic */
1268 __put_user(0, &aux
->end
.magic
);
1269 __put_user(0, &aux
->end
.size
);
1274 static int target_restore_sigframe(CPUARMState
*env
,
1275 struct target_rt_sigframe
*sf
)
1279 struct target_aux_context
*aux
=
1280 (struct target_aux_context
*)sf
->uc
.tuc_mcontext
.__reserved
;
1281 uint32_t magic
, size
, fpsr
, fpcr
;
1284 target_to_host_sigset(&set
, &sf
->uc
.tuc_sigmask
);
1285 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
1287 for (i
= 0; i
< 31; i
++) {
1288 __get_user(env
->xregs
[i
], &sf
->uc
.tuc_mcontext
.regs
[i
]);
1291 __get_user(env
->xregs
[31], &sf
->uc
.tuc_mcontext
.sp
);
1292 __get_user(env
->pc
, &sf
->uc
.tuc_mcontext
.pc
);
1293 __get_user(pstate
, &sf
->uc
.tuc_mcontext
.pstate
);
1294 pstate_write(env
, pstate
);
1296 __get_user(magic
, &aux
->fpsimd
.head
.magic
);
1297 __get_user(size
, &aux
->fpsimd
.head
.size
);
1299 if (magic
!= TARGET_FPSIMD_MAGIC
1300 || size
!= sizeof(struct target_fpsimd_context
)) {
1304 for (i
= 0; i
< 32; i
++) {
1305 #ifdef TARGET_WORDS_BIGENDIAN
1306 __get_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1307 __get_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2]);
1309 __get_user(env
->vfp
.regs
[i
* 2], &aux
->fpsimd
.vregs
[i
* 2]);
1310 __get_user(env
->vfp
.regs
[i
* 2 + 1], &aux
->fpsimd
.vregs
[i
* 2 + 1]);
1313 __get_user(fpsr
, &aux
->fpsimd
.fpsr
);
1314 vfp_set_fpsr(env
, fpsr
);
1315 __get_user(fpcr
, &aux
->fpsimd
.fpcr
);
1316 vfp_set_fpcr(env
, fpcr
);
1321 static abi_ulong
get_sigframe(struct target_sigaction
*ka
, CPUARMState
*env
)
1325 sp
= env
->xregs
[31];
1328 * This is the X/Open sanctioned signal stack switching.
1330 if ((ka
->sa_flags
& SA_ONSTACK
) && !sas_ss_flags(sp
)) {
1331 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1334 sp
= (sp
- sizeof(struct target_rt_sigframe
)) & ~15;
1339 static void target_setup_frame(int usig
, struct target_sigaction
*ka
,
1340 target_siginfo_t
*info
, target_sigset_t
*set
,
1343 struct target_rt_sigframe
*frame
;
1344 abi_ulong frame_addr
, return_addr
;
1346 frame_addr
= get_sigframe(ka
, env
);
1347 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
1351 __put_user(0, &frame
->uc
.tuc_flags
);
1352 __put_user(0, &frame
->uc
.tuc_link
);
1354 __put_user(target_sigaltstack_used
.ss_sp
,
1355 &frame
->uc
.tuc_stack
.ss_sp
);
1356 __put_user(sas_ss_flags(env
->xregs
[31]),
1357 &frame
->uc
.tuc_stack
.ss_flags
);
1358 __put_user(target_sigaltstack_used
.ss_size
,
1359 &frame
->uc
.tuc_stack
.ss_size
);
1360 target_setup_sigframe(frame
, env
, set
);
1361 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1362 return_addr
= ka
->sa_restorer
;
1364 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1365 __put_user(0xd2801168, &frame
->tramp
[0]);
1366 __put_user(0xd4000001, &frame
->tramp
[1]);
1367 return_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, tramp
);
1369 env
->xregs
[0] = usig
;
1370 env
->xregs
[31] = frame_addr
;
1371 env
->xregs
[29] = env
->xregs
[31] + offsetof(struct target_rt_sigframe
, fp
);
1372 env
->pc
= ka
->_sa_handler
;
1373 env
->xregs
[30] = return_addr
;
1375 if (copy_siginfo_to_user(&frame
->info
, info
)) {
1378 env
->xregs
[1] = frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
1379 env
->xregs
[2] = frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
1382 unlock_user_struct(frame
, frame_addr
, 1);
1386 unlock_user_struct(frame
, frame_addr
, 1);
1387 force_sig(TARGET_SIGSEGV
);
1390 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
1391 target_siginfo_t
*info
, target_sigset_t
*set
,
1394 target_setup_frame(sig
, ka
, info
, set
, env
);
1397 static void setup_frame(int sig
, struct target_sigaction
*ka
,
1398 target_sigset_t
*set
, CPUARMState
*env
)
1400 target_setup_frame(sig
, ka
, 0, set
, env
);
1403 long do_rt_sigreturn(CPUARMState
*env
)
1405 struct target_rt_sigframe
*frame
= NULL
;
1406 abi_ulong frame_addr
= env
->xregs
[31];
1408 if (frame_addr
& 15) {
1412 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
1416 if (target_restore_sigframe(env
, frame
)) {
1420 if (do_sigaltstack(frame_addr
+
1421 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
1422 0, get_sp_from_cpustate(env
)) == -EFAULT
) {
1426 unlock_user_struct(frame
, frame_addr
, 0);
1427 return env
->xregs
[0];
1430 unlock_user_struct(frame
, frame_addr
, 0);
1431 force_sig(TARGET_SIGSEGV
);
1435 long do_sigreturn(CPUARMState
*env
)
1437 return do_rt_sigreturn(env
);
1440 #elif defined(TARGET_ARM)
1442 struct target_sigcontext
{
1444 abi_ulong error_code
;
1463 abi_ulong fault_address
;
1466 struct target_ucontext_v1
{
1467 abi_ulong tuc_flags
;
1469 target_stack_t tuc_stack
;
1470 struct target_sigcontext tuc_mcontext
;
1471 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1474 struct target_ucontext_v2
{
1475 abi_ulong tuc_flags
;
1477 target_stack_t tuc_stack
;
1478 struct target_sigcontext tuc_mcontext
;
1479 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
1480 char __unused
[128 - sizeof(target_sigset_t
)];
1481 abi_ulong tuc_regspace
[128] __attribute__((__aligned__(8)));
1484 struct target_user_vfp
{
1485 uint64_t fpregs
[32];
1489 struct target_user_vfp_exc
{
1495 struct target_vfp_sigframe
{
1498 struct target_user_vfp ufp
;
1499 struct target_user_vfp_exc ufp_exc
;
1500 } __attribute__((__aligned__(8)));
1502 struct target_iwmmxt_sigframe
{
1506 /* Note that not all the coprocessor control registers are stored here */
1513 } __attribute__((__aligned__(8)));
1515 #define TARGET_VFP_MAGIC 0x56465001
1516 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1520 struct target_sigcontext sc
;
1521 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
1527 struct target_ucontext_v2 uc
;
1531 struct rt_sigframe_v1
1535 struct target_siginfo info
;
1536 struct target_ucontext_v1 uc
;
1540 struct rt_sigframe_v2
1542 struct target_siginfo info
;
1543 struct target_ucontext_v2 uc
;
1547 #define TARGET_CONFIG_CPU_32 1
1550 * For ARM syscalls, we encode the syscall number into the instruction.
1552 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1553 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1556 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1557 * need two 16-bit instructions.
1559 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1560 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1562 static const abi_ulong retcodes
[4] = {
1563 SWI_SYS_SIGRETURN
, SWI_THUMB_SIGRETURN
,
1564 SWI_SYS_RT_SIGRETURN
, SWI_THUMB_RT_SIGRETURN
1568 #define __get_user_error(x,p,e) __get_user(x, p)
1570 static inline int valid_user_regs(CPUARMState
*regs
)
1576 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
1577 CPUARMState
*env
, abi_ulong mask
)
1579 __put_user(env
->regs
[0], &sc
->arm_r0
);
1580 __put_user(env
->regs
[1], &sc
->arm_r1
);
1581 __put_user(env
->regs
[2], &sc
->arm_r2
);
1582 __put_user(env
->regs
[3], &sc
->arm_r3
);
1583 __put_user(env
->regs
[4], &sc
->arm_r4
);
1584 __put_user(env
->regs
[5], &sc
->arm_r5
);
1585 __put_user(env
->regs
[6], &sc
->arm_r6
);
1586 __put_user(env
->regs
[7], &sc
->arm_r7
);
1587 __put_user(env
->regs
[8], &sc
->arm_r8
);
1588 __put_user(env
->regs
[9], &sc
->arm_r9
);
1589 __put_user(env
->regs
[10], &sc
->arm_r10
);
1590 __put_user(env
->regs
[11], &sc
->arm_fp
);
1591 __put_user(env
->regs
[12], &sc
->arm_ip
);
1592 __put_user(env
->regs
[13], &sc
->arm_sp
);
1593 __put_user(env
->regs
[14], &sc
->arm_lr
);
1594 __put_user(env
->regs
[15], &sc
->arm_pc
);
1595 #ifdef TARGET_CONFIG_CPU_32
1596 __put_user(cpsr_read(env
), &sc
->arm_cpsr
);
1599 __put_user(/* current->thread.trap_no */ 0, &sc
->trap_no
);
1600 __put_user(/* current->thread.error_code */ 0, &sc
->error_code
);
1601 __put_user(/* current->thread.address */ 0, &sc
->fault_address
);
1602 __put_user(mask
, &sc
->oldmask
);
1605 static inline abi_ulong
1606 get_sigframe(struct target_sigaction
*ka
, CPUARMState
*regs
, int framesize
)
1608 unsigned long sp
= regs
->regs
[13];
1611 * This is the X/Open sanctioned signal stack switching.
1613 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && !sas_ss_flags(sp
))
1614 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
1616 * ATPCS B01 mandates 8-byte alignment
1618 return (sp
- framesize
) & ~7;
1622 setup_return(CPUARMState
*env
, struct target_sigaction
*ka
,
1623 abi_ulong
*rc
, abi_ulong frame_addr
, int usig
, abi_ulong rc_addr
)
1625 abi_ulong handler
= ka
->_sa_handler
;
1627 int thumb
= handler
& 1;
1628 uint32_t cpsr
= cpsr_read(env
);
1637 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
1638 retcode
= ka
->sa_restorer
;
1640 unsigned int idx
= thumb
;
1642 if (ka
->sa_flags
& TARGET_SA_SIGINFO
)
1645 if (__put_user(retcodes
[idx
], rc
))
1648 retcode
= rc_addr
+ thumb
;
1651 env
->regs
[0] = usig
;
1652 env
->regs
[13] = frame_addr
;
1653 env
->regs
[14] = retcode
;
1654 env
->regs
[15] = handler
& (thumb
? ~1 : ~3);
1655 cpsr_write(env
, cpsr
, 0xffffffff);
1660 static abi_ulong
*setup_sigframe_v2_vfp(abi_ulong
*regspace
, CPUARMState
*env
)
1663 struct target_vfp_sigframe
*vfpframe
;
1664 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
1665 __put_user(TARGET_VFP_MAGIC
, &vfpframe
->magic
);
1666 __put_user(sizeof(*vfpframe
), &vfpframe
->size
);
1667 for (i
= 0; i
< 32; i
++) {
1668 __put_user(float64_val(env
->vfp
.regs
[i
]), &vfpframe
->ufp
.fpregs
[i
]);
1670 __put_user(vfp_get_fpscr(env
), &vfpframe
->ufp
.fpscr
);
1671 __put_user(env
->vfp
.xregs
[ARM_VFP_FPEXC
], &vfpframe
->ufp_exc
.fpexc
);
1672 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
1673 __put_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
1674 return (abi_ulong
*)(vfpframe
+1);
1677 static abi_ulong
*setup_sigframe_v2_iwmmxt(abi_ulong
*regspace
,
1681 struct target_iwmmxt_sigframe
*iwmmxtframe
;
1682 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
1683 __put_user(TARGET_IWMMXT_MAGIC
, &iwmmxtframe
->magic
);
1684 __put_user(sizeof(*iwmmxtframe
), &iwmmxtframe
->size
);
1685 for (i
= 0; i
< 16; i
++) {
1686 __put_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
1688 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
1689 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
1690 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
1691 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
1692 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
1693 __put_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
1694 return (abi_ulong
*)(iwmmxtframe
+1);
1697 static void setup_sigframe_v2(struct target_ucontext_v2
*uc
,
1698 target_sigset_t
*set
, CPUARMState
*env
)
1700 struct target_sigaltstack stack
;
1702 abi_ulong
*regspace
;
1704 /* Clear all the bits of the ucontext we don't use. */
1705 memset(uc
, 0, offsetof(struct target_ucontext_v2
, tuc_mcontext
));
1707 memset(&stack
, 0, sizeof(stack
));
1708 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1709 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1710 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1711 memcpy(&uc
->tuc_stack
, &stack
, sizeof(stack
));
1713 setup_sigcontext(&uc
->tuc_mcontext
, env
, set
->sig
[0]);
1714 /* Save coprocessor signal frame. */
1715 regspace
= uc
->tuc_regspace
;
1716 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
1717 regspace
= setup_sigframe_v2_vfp(regspace
, env
);
1719 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
1720 regspace
= setup_sigframe_v2_iwmmxt(regspace
, env
);
1723 /* Write terminating magic word */
1724 __put_user(0, regspace
);
1726 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1727 __put_user(set
->sig
[i
], &uc
->tuc_sigmask
.sig
[i
]);
1731 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1732 static void setup_frame_v1(int usig
, struct target_sigaction
*ka
,
1733 target_sigset_t
*set
, CPUARMState
*regs
)
1735 struct sigframe_v1
*frame
;
1736 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1739 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1742 setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
1744 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1745 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
1749 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1750 frame_addr
+ offsetof(struct sigframe_v1
, retcode
));
1753 unlock_user_struct(frame
, frame_addr
, 1);
1756 static void setup_frame_v2(int usig
, struct target_sigaction
*ka
,
1757 target_sigset_t
*set
, CPUARMState
*regs
)
1759 struct sigframe_v2
*frame
;
1760 abi_ulong frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
1762 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1765 setup_sigframe_v2(&frame
->uc
, set
, regs
);
1767 setup_return(regs
, ka
, &frame
->retcode
, frame_addr
, usig
,
1768 frame_addr
+ offsetof(struct sigframe_v2
, retcode
));
1770 unlock_user_struct(frame
, frame_addr
, 1);
1773 static void setup_frame(int usig
, struct target_sigaction
*ka
,
1774 target_sigset_t
*set
, CPUARMState
*regs
)
1776 if (get_osversion() >= 0x020612) {
1777 setup_frame_v2(usig
, ka
, set
, regs
);
1779 setup_frame_v1(usig
, ka
, set
, regs
);
1783 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1784 static void setup_rt_frame_v1(int usig
, struct target_sigaction
*ka
,
1785 target_siginfo_t
*info
,
1786 target_sigset_t
*set
, CPUARMState
*env
)
1788 struct rt_sigframe_v1
*frame
;
1789 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1790 struct target_sigaltstack stack
;
1792 abi_ulong info_addr
, uc_addr
;
1794 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1797 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, info
);
1798 __put_user(info_addr
, &frame
->pinfo
);
1799 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
);
1800 __put_user(uc_addr
, &frame
->puc
);
1801 copy_siginfo_to_user(&frame
->info
, info
);
1803 /* Clear all the bits of the ucontext we don't use. */
1804 memset(&frame
->uc
, 0, offsetof(struct target_ucontext_v1
, tuc_mcontext
));
1806 memset(&stack
, 0, sizeof(stack
));
1807 __put_user(target_sigaltstack_used
.ss_sp
, &stack
.ss_sp
);
1808 __put_user(target_sigaltstack_used
.ss_size
, &stack
.ss_size
);
1809 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)), &stack
.ss_flags
);
1810 memcpy(&frame
->uc
.tuc_stack
, &stack
, sizeof(stack
));
1812 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, set
->sig
[0]);
1813 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
1814 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
1818 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1819 frame_addr
+ offsetof(struct rt_sigframe_v1
, retcode
));
1821 env
->regs
[1] = info_addr
;
1822 env
->regs
[2] = uc_addr
;
1825 unlock_user_struct(frame
, frame_addr
, 1);
1828 static void setup_rt_frame_v2(int usig
, struct target_sigaction
*ka
,
1829 target_siginfo_t
*info
,
1830 target_sigset_t
*set
, CPUARMState
*env
)
1832 struct rt_sigframe_v2
*frame
;
1833 abi_ulong frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
1834 abi_ulong info_addr
, uc_addr
;
1836 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
1839 info_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, info
);
1840 uc_addr
= frame_addr
+ offsetof(struct rt_sigframe_v2
, uc
);
1841 copy_siginfo_to_user(&frame
->info
, info
);
1843 setup_sigframe_v2(&frame
->uc
, set
, env
);
1845 setup_return(env
, ka
, &frame
->retcode
, frame_addr
, usig
,
1846 frame_addr
+ offsetof(struct rt_sigframe_v2
, retcode
));
1848 env
->regs
[1] = info_addr
;
1849 env
->regs
[2] = uc_addr
;
1851 unlock_user_struct(frame
, frame_addr
, 1);
1854 static void setup_rt_frame(int usig
, struct target_sigaction
*ka
,
1855 target_siginfo_t
*info
,
1856 target_sigset_t
*set
, CPUARMState
*env
)
1858 if (get_osversion() >= 0x020612) {
1859 setup_rt_frame_v2(usig
, ka
, info
, set
, env
);
1861 setup_rt_frame_v1(usig
, ka
, info
, set
, env
);
1866 restore_sigcontext(CPUARMState
*env
, struct target_sigcontext
*sc
)
1871 __get_user_error(env
->regs
[0], &sc
->arm_r0
, err
);
1872 __get_user_error(env
->regs
[1], &sc
->arm_r1
, err
);
1873 __get_user_error(env
->regs
[2], &sc
->arm_r2
, err
);
1874 __get_user_error(env
->regs
[3], &sc
->arm_r3
, err
);
1875 __get_user_error(env
->regs
[4], &sc
->arm_r4
, err
);
1876 __get_user_error(env
->regs
[5], &sc
->arm_r5
, err
);
1877 __get_user_error(env
->regs
[6], &sc
->arm_r6
, err
);
1878 __get_user_error(env
->regs
[7], &sc
->arm_r7
, err
);
1879 __get_user_error(env
->regs
[8], &sc
->arm_r8
, err
);
1880 __get_user_error(env
->regs
[9], &sc
->arm_r9
, err
);
1881 __get_user_error(env
->regs
[10], &sc
->arm_r10
, err
);
1882 __get_user_error(env
->regs
[11], &sc
->arm_fp
, err
);
1883 __get_user_error(env
->regs
[12], &sc
->arm_ip
, err
);
1884 __get_user_error(env
->regs
[13], &sc
->arm_sp
, err
);
1885 __get_user_error(env
->regs
[14], &sc
->arm_lr
, err
);
1886 __get_user_error(env
->regs
[15], &sc
->arm_pc
, err
);
1887 #ifdef TARGET_CONFIG_CPU_32
1888 __get_user_error(cpsr
, &sc
->arm_cpsr
, err
);
1889 cpsr_write(env
, cpsr
, CPSR_USER
| CPSR_EXEC
);
1892 err
|= !valid_user_regs(env
);
1897 static long do_sigreturn_v1(CPUARMState
*env
)
1899 abi_ulong frame_addr
;
1900 struct sigframe_v1
*frame
= NULL
;
1901 target_sigset_t set
;
1906 * Since we stacked the signal on a 64-bit boundary,
1907 * then 'sp' should be word aligned here. If it's
1908 * not, then the user is trying to mess with us.
1910 frame_addr
= env
->regs
[13];
1911 if (frame_addr
& 7) {
1915 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
1918 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
))
1920 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
1921 if (__get_user(set
.sig
[i
], &frame
->extramask
[i
- 1]))
1925 target_to_host_sigset_internal(&host_set
, &set
);
1926 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
1928 if (restore_sigcontext(env
, &frame
->sc
))
1932 /* Send SIGTRAP if we're single-stepping */
1933 if (ptrace_cancel_bpt(current
))
1934 send_sig(SIGTRAP
, current
, 1);
1936 unlock_user_struct(frame
, frame_addr
, 0);
1937 return env
->regs
[0];
1940 unlock_user_struct(frame
, frame_addr
, 0);
1941 force_sig(TARGET_SIGSEGV
/* , current */);
1945 static abi_ulong
*restore_sigframe_v2_vfp(CPUARMState
*env
, abi_ulong
*regspace
)
1948 abi_ulong magic
, sz
;
1949 uint32_t fpscr
, fpexc
;
1950 struct target_vfp_sigframe
*vfpframe
;
1951 vfpframe
= (struct target_vfp_sigframe
*)regspace
;
1953 __get_user(magic
, &vfpframe
->magic
);
1954 __get_user(sz
, &vfpframe
->size
);
1955 if (magic
!= TARGET_VFP_MAGIC
|| sz
!= sizeof(*vfpframe
)) {
1958 for (i
= 0; i
< 32; i
++) {
1959 __get_user(float64_val(env
->vfp
.regs
[i
]), &vfpframe
->ufp
.fpregs
[i
]);
1961 __get_user(fpscr
, &vfpframe
->ufp
.fpscr
);
1962 vfp_set_fpscr(env
, fpscr
);
1963 __get_user(fpexc
, &vfpframe
->ufp_exc
.fpexc
);
1964 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1965 * and the exception flag is cleared
1968 fpexc
&= ~((1 << 31) | (1 << 28));
1969 env
->vfp
.xregs
[ARM_VFP_FPEXC
] = fpexc
;
1970 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST
], &vfpframe
->ufp_exc
.fpinst
);
1971 __get_user(env
->vfp
.xregs
[ARM_VFP_FPINST2
], &vfpframe
->ufp_exc
.fpinst2
);
1972 return (abi_ulong
*)(vfpframe
+ 1);
1975 static abi_ulong
*restore_sigframe_v2_iwmmxt(CPUARMState
*env
,
1976 abi_ulong
*regspace
)
1979 abi_ulong magic
, sz
;
1980 struct target_iwmmxt_sigframe
*iwmmxtframe
;
1981 iwmmxtframe
= (struct target_iwmmxt_sigframe
*)regspace
;
1983 __get_user(magic
, &iwmmxtframe
->magic
);
1984 __get_user(sz
, &iwmmxtframe
->size
);
1985 if (magic
!= TARGET_IWMMXT_MAGIC
|| sz
!= sizeof(*iwmmxtframe
)) {
1988 for (i
= 0; i
< 16; i
++) {
1989 __get_user(env
->iwmmxt
.regs
[i
], &iwmmxtframe
->regs
[i
]);
1991 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCSSF
], &iwmmxtframe
->wcssf
);
1992 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCASF
], &iwmmxtframe
->wcssf
);
1993 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR0
], &iwmmxtframe
->wcgr0
);
1994 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR1
], &iwmmxtframe
->wcgr1
);
1995 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR2
], &iwmmxtframe
->wcgr2
);
1996 __get_user(env
->vfp
.xregs
[ARM_IWMMXT_wCGR3
], &iwmmxtframe
->wcgr3
);
1997 return (abi_ulong
*)(iwmmxtframe
+ 1);
2000 static int do_sigframe_return_v2(CPUARMState
*env
, target_ulong frame_addr
,
2001 struct target_ucontext_v2
*uc
)
2004 abi_ulong
*regspace
;
2006 target_to_host_sigset(&host_set
, &uc
->tuc_sigmask
);
2007 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
2009 if (restore_sigcontext(env
, &uc
->tuc_mcontext
))
2012 /* Restore coprocessor signal frame */
2013 regspace
= uc
->tuc_regspace
;
2014 if (arm_feature(env
, ARM_FEATURE_VFP
)) {
2015 regspace
= restore_sigframe_v2_vfp(env
, regspace
);
2020 if (arm_feature(env
, ARM_FEATURE_IWMMXT
)) {
2021 regspace
= restore_sigframe_v2_iwmmxt(env
, regspace
);
2027 if (do_sigaltstack(frame_addr
+ offsetof(struct target_ucontext_v2
, tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
2031 /* Send SIGTRAP if we're single-stepping */
2032 if (ptrace_cancel_bpt(current
))
2033 send_sig(SIGTRAP
, current
, 1);
2039 static long do_sigreturn_v2(CPUARMState
*env
)
2041 abi_ulong frame_addr
;
2042 struct sigframe_v2
*frame
= NULL
;
2045 * Since we stacked the signal on a 64-bit boundary,
2046 * then 'sp' should be word aligned here. If it's
2047 * not, then the user is trying to mess with us.
2049 frame_addr
= env
->regs
[13];
2050 if (frame_addr
& 7) {
2054 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2057 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
))
2060 unlock_user_struct(frame
, frame_addr
, 0);
2061 return env
->regs
[0];
2064 unlock_user_struct(frame
, frame_addr
, 0);
2065 force_sig(TARGET_SIGSEGV
/* , current */);
2069 long do_sigreturn(CPUARMState
*env
)
2071 if (get_osversion() >= 0x020612) {
2072 return do_sigreturn_v2(env
);
2074 return do_sigreturn_v1(env
);
2078 static long do_rt_sigreturn_v1(CPUARMState
*env
)
2080 abi_ulong frame_addr
;
2081 struct rt_sigframe_v1
*frame
= NULL
;
2085 * Since we stacked the signal on a 64-bit boundary,
2086 * then 'sp' should be word aligned here. If it's
2087 * not, then the user is trying to mess with us.
2089 frame_addr
= env
->regs
[13];
2090 if (frame_addr
& 7) {
2094 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2097 target_to_host_sigset(&host_set
, &frame
->uc
.tuc_sigmask
);
2098 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
2100 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
))
2103 if (do_sigaltstack(frame_addr
+ offsetof(struct rt_sigframe_v1
, uc
.tuc_stack
), 0, get_sp_from_cpustate(env
)) == -EFAULT
)
2107 /* Send SIGTRAP if we're single-stepping */
2108 if (ptrace_cancel_bpt(current
))
2109 send_sig(SIGTRAP
, current
, 1);
2111 unlock_user_struct(frame
, frame_addr
, 0);
2112 return env
->regs
[0];
2115 unlock_user_struct(frame
, frame_addr
, 0);
2116 force_sig(TARGET_SIGSEGV
/* , current */);
2120 static long do_rt_sigreturn_v2(CPUARMState
*env
)
2122 abi_ulong frame_addr
;
2123 struct rt_sigframe_v2
*frame
= NULL
;
2126 * Since we stacked the signal on a 64-bit boundary,
2127 * then 'sp' should be word aligned here. If it's
2128 * not, then the user is trying to mess with us.
2130 frame_addr
= env
->regs
[13];
2131 if (frame_addr
& 7) {
2135 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
2138 if (do_sigframe_return_v2(env
, frame_addr
, &frame
->uc
))
2141 unlock_user_struct(frame
, frame_addr
, 0);
2142 return env
->regs
[0];
2145 unlock_user_struct(frame
, frame_addr
, 0);
2146 force_sig(TARGET_SIGSEGV
/* , current */);
2150 long do_rt_sigreturn(CPUARMState
*env
)
2152 if (get_osversion() >= 0x020612) {
2153 return do_rt_sigreturn_v2(env
);
2155 return do_rt_sigreturn_v1(env
);
2159 #elif defined(TARGET_SPARC)
2161 #define __SUNOS_MAXWIN 31
2163 /* This is what SunOS does, so shall I. */
2164 struct target_sigcontext
{
2165 abi_ulong sigc_onstack
; /* state to restore */
2167 abi_ulong sigc_mask
; /* sigmask to restore */
2168 abi_ulong sigc_sp
; /* stack pointer */
2169 abi_ulong sigc_pc
; /* program counter */
2170 abi_ulong sigc_npc
; /* next program counter */
2171 abi_ulong sigc_psr
; /* for condition codes etc */
2172 abi_ulong sigc_g1
; /* User uses these two registers */
2173 abi_ulong sigc_o0
; /* within the trampoline code. */
2175 /* Now comes information regarding the users window set
2176 * at the time of the signal.
2178 abi_ulong sigc_oswins
; /* outstanding windows */
2180 /* stack ptrs for each regwin buf */
2181 char *sigc_spbuf
[__SUNOS_MAXWIN
];
2183 /* Windows to restore after signal */
2185 abi_ulong locals
[8];
2187 } sigc_wbuf
[__SUNOS_MAXWIN
];
2189 /* A Sparc stack frame */
2190 struct sparc_stackf
{
2191 abi_ulong locals
[8];
2193 /* It's simpler to treat fp and callers_pc as elements of ins[]
2194 * since we never need to access them ourselves.
2198 abi_ulong xxargs
[1];
2207 abi_ulong u_regs
[16]; /* globals and ins */
2213 abi_ulong si_float_regs
[32];
2214 unsigned long si_fsr
;
2215 unsigned long si_fpqdepth
;
2217 unsigned long *insn_addr
;
2220 } qemu_siginfo_fpu_t
;
2223 struct target_signal_frame
{
2224 struct sparc_stackf ss
;
2227 abi_ulong insns
[2] __attribute__ ((aligned (8)));
2228 abi_ulong extramask
[TARGET_NSIG_WORDS
- 1];
2229 abi_ulong extra_size
; /* Should be 0 */
2230 qemu_siginfo_fpu_t fpu_state
;
2232 struct target_rt_signal_frame
{
2233 struct sparc_stackf ss
;
2238 unsigned int insns
[2];
2240 unsigned int extra_size
; /* Should be 0 */
2241 qemu_siginfo_fpu_t fpu_state
;
2255 #define UREG_FP UREG_I6
2256 #define UREG_SP UREG_O6
2258 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
2260 unsigned long framesize
)
2264 sp
= env
->regwptr
[UREG_FP
];
2266 /* This is the X/Open sanctioned signal stack switching. */
2267 if (sa
->sa_flags
& TARGET_SA_ONSTACK
) {
2268 if (!on_sig_stack(sp
)
2269 && !((target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
) & 7))
2270 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2272 return sp
- framesize
;
2276 setup___siginfo(__siginfo_t
*si
, CPUSPARCState
*env
, abi_ulong mask
)
2280 err
|= __put_user(env
->psr
, &si
->si_regs
.psr
);
2281 err
|= __put_user(env
->pc
, &si
->si_regs
.pc
);
2282 err
|= __put_user(env
->npc
, &si
->si_regs
.npc
);
2283 err
|= __put_user(env
->y
, &si
->si_regs
.y
);
2284 for (i
=0; i
< 8; i
++) {
2285 err
|= __put_user(env
->gregs
[i
], &si
->si_regs
.u_regs
[i
]);
2287 for (i
=0; i
< 8; i
++) {
2288 err
|= __put_user(env
->regwptr
[UREG_I0
+ i
], &si
->si_regs
.u_regs
[i
+8]);
2290 err
|= __put_user(mask
, &si
->si_mask
);
2296 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
2297 CPUSPARCState
*env
, unsigned long mask
)
2301 err
|= __put_user(mask
, &sc
->sigc_mask
);
2302 err
|= __put_user(env
->regwptr
[UREG_SP
], &sc
->sigc_sp
);
2303 err
|= __put_user(env
->pc
, &sc
->sigc_pc
);
2304 err
|= __put_user(env
->npc
, &sc
->sigc_npc
);
2305 err
|= __put_user(env
->psr
, &sc
->sigc_psr
);
2306 err
|= __put_user(env
->gregs
[1], &sc
->sigc_g1
);
2307 err
|= __put_user(env
->regwptr
[UREG_O0
], &sc
->sigc_o0
);
2312 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2314 static void setup_frame(int sig
, struct target_sigaction
*ka
,
2315 target_sigset_t
*set
, CPUSPARCState
*env
)
2318 struct target_signal_frame
*sf
;
2319 int sigframe_size
, err
, i
;
2321 /* 1. Make sure everything is clean */
2322 //synchronize_user_stack();
2324 sigframe_size
= NF_ALIGNEDSZ
;
2325 sf_addr
= get_sigframe(ka
, env
, sigframe_size
);
2327 sf
= lock_user(VERIFY_WRITE
, sf_addr
,
2328 sizeof(struct target_signal_frame
), 0);
2332 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2334 if (invalid_frame_pointer(sf
, sigframe_size
))
2335 goto sigill_and_return
;
2337 /* 2. Save the current process state */
2338 err
= setup___siginfo(&sf
->info
, env
, set
->sig
[0]);
2339 err
|= __put_user(0, &sf
->extra_size
);
2341 //err |= save_fpu_state(regs, &sf->fpu_state);
2342 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
2344 err
|= __put_user(set
->sig
[0], &sf
->info
.si_mask
);
2345 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
2346 err
|= __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
2349 for (i
= 0; i
< 8; i
++) {
2350 err
|= __put_user(env
->regwptr
[i
+ UREG_L0
], &sf
->ss
.locals
[i
]);
2352 for (i
= 0; i
< 8; i
++) {
2353 err
|= __put_user(env
->regwptr
[i
+ UREG_I0
], &sf
->ss
.ins
[i
]);
2358 /* 3. signal handler back-trampoline and parameters */
2359 env
->regwptr
[UREG_FP
] = sf_addr
;
2360 env
->regwptr
[UREG_I0
] = sig
;
2361 env
->regwptr
[UREG_I1
] = sf_addr
+
2362 offsetof(struct target_signal_frame
, info
);
2363 env
->regwptr
[UREG_I2
] = sf_addr
+
2364 offsetof(struct target_signal_frame
, info
);
2366 /* 4. signal handler */
2367 env
->pc
= ka
->_sa_handler
;
2368 env
->npc
= (env
->pc
+ 4);
2369 /* 5. return to kernel instructions */
2370 if (ka
->sa_restorer
)
2371 env
->regwptr
[UREG_I7
] = ka
->sa_restorer
;
2375 env
->regwptr
[UREG_I7
] = sf_addr
+
2376 offsetof(struct target_signal_frame
, insns
) - 2 * 4;
2378 /* mov __NR_sigreturn, %g1 */
2380 err
|= __put_user(val32
, &sf
->insns
[0]);
2384 err
|= __put_user(val32
, &sf
->insns
[1]);
2388 /* Flush instruction space. */
2389 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2392 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
2396 force_sig(TARGET_SIGILL
);
2399 //fprintf(stderr, "force_sig\n");
2400 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
2401 force_sig(TARGET_SIGSEGV
);
2404 restore_fpu_state(CPUSPARCState
*env
, qemu_siginfo_fpu_t
*fpu
)
2409 if (current
->flags
& PF_USEDFPU
)
2410 regs
->psr
&= ~PSR_EF
;
2412 if (current
== last_task_used_math
) {
2413 last_task_used_math
= 0;
2414 regs
->psr
&= ~PSR_EF
;
2417 current
->used_math
= 1;
2418 current
->flags
&= ~PF_USEDFPU
;
2421 if (verify_area (VERIFY_READ
, fpu
, sizeof(*fpu
)))
2425 /* XXX: incorrect */
2426 err
= copy_from_user(&env
->fpr
[0], fpu
->si_float_regs
[0],
2427 (sizeof(abi_ulong
) * 32));
2428 err
|= __get_user(env
->fsr
, &fpu
->si_fsr
);
2430 err
|= __get_user(current
->thread
.fpqdepth
, &fpu
->si_fpqdepth
);
2431 if (current
->thread
.fpqdepth
!= 0)
2432 err
|= __copy_from_user(¤t
->thread
.fpqueue
[0],
2433 &fpu
->si_fpqueue
[0],
2434 ((sizeof(unsigned long) +
2435 (sizeof(unsigned long *)))*16));
2441 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
2442 target_siginfo_t
*info
,
2443 target_sigset_t
*set
, CPUSPARCState
*env
)
2445 fprintf(stderr
, "setup_rt_frame: not implemented\n");
2448 long do_sigreturn(CPUSPARCState
*env
)
2451 struct target_signal_frame
*sf
;
2452 uint32_t up_psr
, pc
, npc
;
2453 target_sigset_t set
;
2457 sf_addr
= env
->regwptr
[UREG_FP
];
2458 if (!lock_user_struct(VERIFY_READ
, sf
, sf_addr
, 1))
2461 fprintf(stderr
, "sigreturn\n");
2462 fprintf(stderr
, "sf: %x pc %x fp %x sp %x\n", sf
, env
->pc
, env
->regwptr
[UREG_FP
], env
->regwptr
[UREG_SP
]);
2464 //cpu_dump_state(env, stderr, fprintf, 0);
2466 /* 1. Make sure we are not getting garbage from the user */
2471 err
= __get_user(pc
, &sf
->info
.si_regs
.pc
);
2472 err
|= __get_user(npc
, &sf
->info
.si_regs
.npc
);
2477 /* 2. Restore the state */
2478 err
|= __get_user(up_psr
, &sf
->info
.si_regs
.psr
);
2480 /* User can only change condition codes and FPU enabling in %psr. */
2481 env
->psr
= (up_psr
& (PSR_ICC
/* | PSR_EF */))
2482 | (env
->psr
& ~(PSR_ICC
/* | PSR_EF */));
2486 err
|= __get_user(env
->y
, &sf
->info
.si_regs
.y
);
2487 for (i
=0; i
< 8; i
++) {
2488 err
|= __get_user(env
->gregs
[i
], &sf
->info
.si_regs
.u_regs
[i
]);
2490 for (i
=0; i
< 8; i
++) {
2491 err
|= __get_user(env
->regwptr
[i
+ UREG_I0
], &sf
->info
.si_regs
.u_regs
[i
+8]);
2494 /* FIXME: implement FPU save/restore:
2495 * __get_user(fpu_save, &sf->fpu_save);
2497 * err |= restore_fpu_state(env, fpu_save);
2500 /* This is pretty much atomic, no amount locking would prevent
2501 * the races which exist anyways.
2503 err
|= __get_user(set
.sig
[0], &sf
->info
.si_mask
);
2504 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
2505 err
|= (__get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]));
2508 target_to_host_sigset_internal(&host_set
, &set
);
2509 do_sigprocmask(SIG_SETMASK
, &host_set
, NULL
);
2513 unlock_user_struct(sf
, sf_addr
, 0);
2514 return env
->regwptr
[0];
2517 unlock_user_struct(sf
, sf_addr
, 0);
2518 force_sig(TARGET_SIGSEGV
);
2521 long do_rt_sigreturn(CPUSPARCState
*env
)
2523 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
2524 return -TARGET_ENOSYS
;
2527 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2549 typedef abi_ulong target_mc_greg_t
;
2550 typedef target_mc_greg_t target_mc_gregset_t
[MC_NGREG
];
2552 struct target_mc_fq
{
2553 abi_ulong
*mcfq_addr
;
2557 struct target_mc_fpu
{
2561 //uint128_t qregs[16];
2563 abi_ulong mcfpu_fsr
;
2564 abi_ulong mcfpu_fprs
;
2565 abi_ulong mcfpu_gsr
;
2566 struct target_mc_fq
*mcfpu_fq
;
2567 unsigned char mcfpu_qcnt
;
2568 unsigned char mcfpu_qentsz
;
2569 unsigned char mcfpu_enab
;
2571 typedef struct target_mc_fpu target_mc_fpu_t
;
2574 target_mc_gregset_t mc_gregs
;
2575 target_mc_greg_t mc_fp
;
2576 target_mc_greg_t mc_i7
;
2577 target_mc_fpu_t mc_fpregs
;
2578 } target_mcontext_t
;
2580 struct target_ucontext
{
2581 struct target_ucontext
*tuc_link
;
2582 abi_ulong tuc_flags
;
2583 target_sigset_t tuc_sigmask
;
2584 target_mcontext_t tuc_mcontext
;
2587 /* A V9 register window */
2588 struct target_reg_window
{
2589 abi_ulong locals
[8];
2593 #define TARGET_STACK_BIAS 2047
2595 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2596 void sparc64_set_context(CPUSPARCState
*env
)
2599 struct target_ucontext
*ucp
;
2600 target_mc_gregset_t
*grp
;
2601 abi_ulong pc
, npc
, tstate
;
2602 abi_ulong fp
, i7
, w_addr
;
2606 ucp_addr
= env
->regwptr
[UREG_I0
];
2607 if (!lock_user_struct(VERIFY_READ
, ucp
, ucp_addr
, 1))
2609 grp
= &ucp
->tuc_mcontext
.mc_gregs
;
2610 err
= __get_user(pc
, &((*grp
)[MC_PC
]));
2611 err
|= __get_user(npc
, &((*grp
)[MC_NPC
]));
2612 if (err
|| ((pc
| npc
) & 3))
2614 if (env
->regwptr
[UREG_I1
]) {
2615 target_sigset_t target_set
;
2618 if (TARGET_NSIG_WORDS
== 1) {
2619 if (__get_user(target_set
.sig
[0], &ucp
->tuc_sigmask
.sig
[0]))
2622 abi_ulong
*src
, *dst
;
2623 src
= ucp
->tuc_sigmask
.sig
;
2624 dst
= target_set
.sig
;
2625 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
2626 err
|= __get_user(*dst
, src
);
2631 target_to_host_sigset_internal(&set
, &target_set
);
2632 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
2636 err
|= __get_user(env
->y
, &((*grp
)[MC_Y
]));
2637 err
|= __get_user(tstate
, &((*grp
)[MC_TSTATE
]));
2638 env
->asi
= (tstate
>> 24) & 0xff;
2639 cpu_put_ccr(env
, tstate
>> 32);
2640 cpu_put_cwp64(env
, tstate
& 0x1f);
2641 err
|= __get_user(env
->gregs
[1], (&(*grp
)[MC_G1
]));
2642 err
|= __get_user(env
->gregs
[2], (&(*grp
)[MC_G2
]));
2643 err
|= __get_user(env
->gregs
[3], (&(*grp
)[MC_G3
]));
2644 err
|= __get_user(env
->gregs
[4], (&(*grp
)[MC_G4
]));
2645 err
|= __get_user(env
->gregs
[5], (&(*grp
)[MC_G5
]));
2646 err
|= __get_user(env
->gregs
[6], (&(*grp
)[MC_G6
]));
2647 err
|= __get_user(env
->gregs
[7], (&(*grp
)[MC_G7
]));
2648 err
|= __get_user(env
->regwptr
[UREG_I0
], (&(*grp
)[MC_O0
]));
2649 err
|= __get_user(env
->regwptr
[UREG_I1
], (&(*grp
)[MC_O1
]));
2650 err
|= __get_user(env
->regwptr
[UREG_I2
], (&(*grp
)[MC_O2
]));
2651 err
|= __get_user(env
->regwptr
[UREG_I3
], (&(*grp
)[MC_O3
]));
2652 err
|= __get_user(env
->regwptr
[UREG_I4
], (&(*grp
)[MC_O4
]));
2653 err
|= __get_user(env
->regwptr
[UREG_I5
], (&(*grp
)[MC_O5
]));
2654 err
|= __get_user(env
->regwptr
[UREG_I6
], (&(*grp
)[MC_O6
]));
2655 err
|= __get_user(env
->regwptr
[UREG_I7
], (&(*grp
)[MC_O7
]));
2657 err
|= __get_user(fp
, &(ucp
->tuc_mcontext
.mc_fp
));
2658 err
|= __get_user(i7
, &(ucp
->tuc_mcontext
.mc_i7
));
2660 w_addr
= TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
];
2661 if (put_user(fp
, w_addr
+ offsetof(struct target_reg_window
, ins
[6]),
2664 if (put_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
2667 /* FIXME this does not match how the kernel handles the FPU in
2668 * its sparc64_set_context implementation. In particular the FPU
2669 * is only restored if fenab is non-zero in:
2670 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2672 err
|= __get_user(env
->fprs
, &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fprs
));
2674 uint32_t *src
= ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2675 for (i
= 0; i
< 64; i
++, src
++) {
2677 err
|= __get_user(env
->fpr
[i
/2].l
.lower
, src
);
2679 err
|= __get_user(env
->fpr
[i
/2].l
.upper
, src
);
2683 err
|= __get_user(env
->fsr
,
2684 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fsr
));
2685 err
|= __get_user(env
->gsr
,
2686 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_gsr
));
2689 unlock_user_struct(ucp
, ucp_addr
, 0);
2692 unlock_user_struct(ucp
, ucp_addr
, 0);
2693 force_sig(TARGET_SIGSEGV
);
2696 void sparc64_get_context(CPUSPARCState
*env
)
2699 struct target_ucontext
*ucp
;
2700 target_mc_gregset_t
*grp
;
2701 target_mcontext_t
*mcp
;
2702 abi_ulong fp
, i7
, w_addr
;
2705 target_sigset_t target_set
;
2708 ucp_addr
= env
->regwptr
[UREG_I0
];
2709 if (!lock_user_struct(VERIFY_WRITE
, ucp
, ucp_addr
, 0))
2712 mcp
= &ucp
->tuc_mcontext
;
2713 grp
= &mcp
->mc_gregs
;
2715 /* Skip over the trap instruction, first. */
2721 do_sigprocmask(0, NULL
, &set
);
2722 host_to_target_sigset_internal(&target_set
, &set
);
2723 if (TARGET_NSIG_WORDS
== 1) {
2724 err
|= __put_user(target_set
.sig
[0],
2725 (abi_ulong
*)&ucp
->tuc_sigmask
);
2727 abi_ulong
*src
, *dst
;
2728 src
= target_set
.sig
;
2729 dst
= ucp
->tuc_sigmask
.sig
;
2730 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
2731 err
|= __put_user(*src
, dst
);
2737 /* XXX: tstate must be saved properly */
2738 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2739 err
|= __put_user(env
->pc
, &((*grp
)[MC_PC
]));
2740 err
|= __put_user(env
->npc
, &((*grp
)[MC_NPC
]));
2741 err
|= __put_user(env
->y
, &((*grp
)[MC_Y
]));
2742 err
|= __put_user(env
->gregs
[1], &((*grp
)[MC_G1
]));
2743 err
|= __put_user(env
->gregs
[2], &((*grp
)[MC_G2
]));
2744 err
|= __put_user(env
->gregs
[3], &((*grp
)[MC_G3
]));
2745 err
|= __put_user(env
->gregs
[4], &((*grp
)[MC_G4
]));
2746 err
|= __put_user(env
->gregs
[5], &((*grp
)[MC_G5
]));
2747 err
|= __put_user(env
->gregs
[6], &((*grp
)[MC_G6
]));
2748 err
|= __put_user(env
->gregs
[7], &((*grp
)[MC_G7
]));
2749 err
|= __put_user(env
->regwptr
[UREG_I0
], &((*grp
)[MC_O0
]));
2750 err
|= __put_user(env
->regwptr
[UREG_I1
], &((*grp
)[MC_O1
]));
2751 err
|= __put_user(env
->regwptr
[UREG_I2
], &((*grp
)[MC_O2
]));
2752 err
|= __put_user(env
->regwptr
[UREG_I3
], &((*grp
)[MC_O3
]));
2753 err
|= __put_user(env
->regwptr
[UREG_I4
], &((*grp
)[MC_O4
]));
2754 err
|= __put_user(env
->regwptr
[UREG_I5
], &((*grp
)[MC_O5
]));
2755 err
|= __put_user(env
->regwptr
[UREG_I6
], &((*grp
)[MC_O6
]));
2756 err
|= __put_user(env
->regwptr
[UREG_I7
], &((*grp
)[MC_O7
]));
2758 w_addr
= TARGET_STACK_BIAS
+env
->regwptr
[UREG_I6
];
2760 if (get_user(fp
, w_addr
+ offsetof(struct target_reg_window
, ins
[6]),
2763 if (get_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
2766 err
|= __put_user(fp
, &(mcp
->mc_fp
));
2767 err
|= __put_user(i7
, &(mcp
->mc_i7
));
2770 uint32_t *dst
= ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fregs
.sregs
;
2771 for (i
= 0; i
< 64; i
++, dst
++) {
2773 err
|= __put_user(env
->fpr
[i
/2].l
.lower
, dst
);
2775 err
|= __put_user(env
->fpr
[i
/2].l
.upper
, dst
);
2779 err
|= __put_user(env
->fsr
, &(mcp
->mc_fpregs
.mcfpu_fsr
));
2780 err
|= __put_user(env
->gsr
, &(mcp
->mc_fpregs
.mcfpu_gsr
));
2781 err
|= __put_user(env
->fprs
, &(mcp
->mc_fpregs
.mcfpu_fprs
));
2785 unlock_user_struct(ucp
, ucp_addr
, 1);
2788 unlock_user_struct(ucp
, ucp_addr
, 1);
2789 force_sig(TARGET_SIGSEGV
);
2792 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2794 # if defined(TARGET_ABI_MIPSO32)
2795 struct target_sigcontext
{
2796 uint32_t sc_regmask
; /* Unused */
2799 uint64_t sc_regs
[32];
2800 uint64_t sc_fpregs
[32];
2801 uint32_t sc_ownedfp
; /* Unused */
2802 uint32_t sc_fpc_csr
;
2803 uint32_t sc_fpc_eir
; /* Unused */
2804 uint32_t sc_used_math
;
2805 uint32_t sc_dsp
; /* dsp status, was sc_ssflags */
2809 target_ulong sc_hi1
; /* Was sc_cause */
2810 target_ulong sc_lo1
; /* Was sc_badvaddr */
2811 target_ulong sc_hi2
; /* Was sc_sigset[4] */
2812 target_ulong sc_lo2
;
2813 target_ulong sc_hi3
;
2814 target_ulong sc_lo3
;
2816 # else /* N32 || N64 */
2817 struct target_sigcontext
{
2818 uint64_t sc_regs
[32];
2819 uint64_t sc_fpregs
[32];
2829 uint32_t sc_fpc_csr
;
2830 uint32_t sc_used_math
;
2832 uint32_t sc_reserved
;
2837 uint32_t sf_ass
[4]; /* argument save space for o32 */
2838 uint32_t sf_code
[2]; /* signal trampoline */
2839 struct target_sigcontext sf_sc
;
2840 target_sigset_t sf_mask
;
2843 struct target_ucontext
{
2844 target_ulong tuc_flags
;
2845 target_ulong tuc_link
;
2846 target_stack_t tuc_stack
;
2848 struct target_sigcontext tuc_mcontext
;
2849 target_sigset_t tuc_sigmask
;
2852 struct target_rt_sigframe
{
2853 uint32_t rs_ass
[4]; /* argument save space for o32 */
2854 uint32_t rs_code
[2]; /* signal trampoline */
2855 struct target_siginfo rs_info
;
2856 struct target_ucontext rs_uc
;
2859 /* Install trampoline to jump back from signal handler */
2860 static inline int install_sigtramp(unsigned int *tramp
, unsigned int syscall
)
2865 * Set up the return code ...
2867 * li v0, __NR__foo_sigreturn
2871 err
|= __put_user(0x24020000 + syscall
, tramp
+ 0);
2872 err
|= __put_user(0x0000000c , tramp
+ 1);
2877 setup_sigcontext(CPUMIPSState
*regs
, struct target_sigcontext
*sc
)
2882 err
|= __put_user(exception_resume_pc(regs
), &sc
->sc_pc
);
2883 regs
->hflags
&= ~MIPS_HFLAG_BMASK
;
2885 __put_user(0, &sc
->sc_regs
[0]);
2886 for (i
= 1; i
< 32; ++i
) {
2887 err
|= __put_user(regs
->active_tc
.gpr
[i
], &sc
->sc_regs
[i
]);
2890 err
|= __put_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2891 err
|= __put_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2893 /* Rather than checking for dsp existence, always copy. The storage
2894 would just be garbage otherwise. */
2895 err
|= __put_user(regs
->active_tc
.HI
[1], &sc
->sc_hi1
);
2896 err
|= __put_user(regs
->active_tc
.HI
[2], &sc
->sc_hi2
);
2897 err
|= __put_user(regs
->active_tc
.HI
[3], &sc
->sc_hi3
);
2898 err
|= __put_user(regs
->active_tc
.LO
[1], &sc
->sc_lo1
);
2899 err
|= __put_user(regs
->active_tc
.LO
[2], &sc
->sc_lo2
);
2900 err
|= __put_user(regs
->active_tc
.LO
[3], &sc
->sc_lo3
);
2902 uint32_t dsp
= cpu_rddsp(0x3ff, regs
);
2903 err
|= __put_user(dsp
, &sc
->sc_dsp
);
2906 err
|= __put_user(1, &sc
->sc_used_math
);
2908 for (i
= 0; i
< 32; ++i
) {
2909 err
|= __put_user(regs
->active_fpu
.fpr
[i
].d
, &sc
->sc_fpregs
[i
]);
2916 restore_sigcontext(CPUMIPSState
*regs
, struct target_sigcontext
*sc
)
2921 err
|= __get_user(regs
->CP0_EPC
, &sc
->sc_pc
);
2923 err
|= __get_user(regs
->active_tc
.HI
[0], &sc
->sc_mdhi
);
2924 err
|= __get_user(regs
->active_tc
.LO
[0], &sc
->sc_mdlo
);
2926 for (i
= 1; i
< 32; ++i
) {
2927 err
|= __get_user(regs
->active_tc
.gpr
[i
], &sc
->sc_regs
[i
]);
2930 err
|= __get_user(regs
->active_tc
.HI
[1], &sc
->sc_hi1
);
2931 err
|= __get_user(regs
->active_tc
.HI
[2], &sc
->sc_hi2
);
2932 err
|= __get_user(regs
->active_tc
.HI
[3], &sc
->sc_hi3
);
2933 err
|= __get_user(regs
->active_tc
.LO
[1], &sc
->sc_lo1
);
2934 err
|= __get_user(regs
->active_tc
.LO
[2], &sc
->sc_lo2
);
2935 err
|= __get_user(regs
->active_tc
.LO
[3], &sc
->sc_lo3
);
2938 err
|= __get_user(dsp
, &sc
->sc_dsp
);
2939 cpu_wrdsp(dsp
, 0x3ff, regs
);
2942 for (i
= 0; i
< 32; ++i
) {
2943 err
|= __get_user(regs
->active_fpu
.fpr
[i
].d
, &sc
->sc_fpregs
[i
]);
2950 * Determine which stack to use..
2952 static inline abi_ulong
2953 get_sigframe(struct target_sigaction
*ka
, CPUMIPSState
*regs
, size_t frame_size
)
2957 /* Default to using normal stack */
2958 sp
= regs
->active_tc
.gpr
[29];
2961 * FPU emulator may have its own trampoline active just
2962 * above the user stack, 16-bytes before the next lowest
2963 * 16 byte boundary. Try to avoid trashing it.
2967 /* This is the X/Open sanctioned signal stack switching. */
2968 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
2969 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
2972 return (sp
- frame_size
) & ~7;
2975 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState
*env
)
2977 if (env
->insn_flags
& (ASE_MIPS16
| ASE_MICROMIPS
)) {
2978 env
->hflags
&= ~MIPS_HFLAG_M16
;
2979 env
->hflags
|= (env
->active_tc
.PC
& 1) << MIPS_HFLAG_M16_SHIFT
;
2980 env
->active_tc
.PC
&= ~(target_ulong
) 1;
2984 # if defined(TARGET_ABI_MIPSO32)
2985 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2986 static void setup_frame(int sig
, struct target_sigaction
* ka
,
2987 target_sigset_t
*set
, CPUMIPSState
*regs
)
2989 struct sigframe
*frame
;
2990 abi_ulong frame_addr
;
2993 frame_addr
= get_sigframe(ka
, regs
, sizeof(*frame
));
2994 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
2997 install_sigtramp(frame
->sf_code
, TARGET_NR_sigreturn
);
2999 if(setup_sigcontext(regs
, &frame
->sf_sc
))
3002 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3003 if(__put_user(set
->sig
[i
], &frame
->sf_mask
.sig
[i
]))
3008 * Arguments to signal handler:
3010 * a0 = signal number
3011 * a1 = 0 (should be cause)
3012 * a2 = pointer to struct sigcontext
3014 * $25 and PC point to the signal handler, $29 points to the
3017 regs
->active_tc
.gpr
[ 4] = sig
;
3018 regs
->active_tc
.gpr
[ 5] = 0;
3019 regs
->active_tc
.gpr
[ 6] = frame_addr
+ offsetof(struct sigframe
, sf_sc
);
3020 regs
->active_tc
.gpr
[29] = frame_addr
;
3021 regs
->active_tc
.gpr
[31] = frame_addr
+ offsetof(struct sigframe
, sf_code
);
3022 /* The original kernel code sets CP0_EPC to the handler
3023 * since it returns to userland using eret
3024 * we cannot do this here, and we must set PC directly */
3025 regs
->active_tc
.PC
= regs
->active_tc
.gpr
[25] = ka
->_sa_handler
;
3026 mips_set_hflags_isa_mode_from_pc(regs
);
3027 unlock_user_struct(frame
, frame_addr
, 1);
3031 unlock_user_struct(frame
, frame_addr
, 1);
3032 force_sig(TARGET_SIGSEGV
/*, current*/);
3035 long do_sigreturn(CPUMIPSState
*regs
)
3037 struct sigframe
*frame
;
3038 abi_ulong frame_addr
;
3040 target_sigset_t target_set
;
3043 #if defined(DEBUG_SIGNAL)
3044 fprintf(stderr
, "do_sigreturn\n");
3046 frame_addr
= regs
->active_tc
.gpr
[29];
3047 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
3050 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3051 if(__get_user(target_set
.sig
[i
], &frame
->sf_mask
.sig
[i
]))
3055 target_to_host_sigset_internal(&blocked
, &target_set
);
3056 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3058 if (restore_sigcontext(regs
, &frame
->sf_sc
))
3063 * Don't let your children do this ...
3065 __asm__
__volatile__(
3073 regs
->active_tc
.PC
= regs
->CP0_EPC
;
3074 mips_set_hflags_isa_mode_from_pc(regs
);
3075 /* I am not sure this is right, but it seems to work
3076 * maybe a problem with nested signals ? */
3078 return -TARGET_QEMU_ESIGRETURN
;
3081 force_sig(TARGET_SIGSEGV
/*, current*/);
3086 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3087 target_siginfo_t
*info
,
3088 target_sigset_t
*set
, CPUMIPSState
*env
)
3090 struct target_rt_sigframe
*frame
;
3091 abi_ulong frame_addr
;
3094 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
3095 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3098 install_sigtramp(frame
->rs_code
, TARGET_NR_rt_sigreturn
);
3100 copy_siginfo_to_user(&frame
->rs_info
, info
);
3102 __put_user(0, &frame
->rs_uc
.tuc_flags
);
3103 __put_user(0, &frame
->rs_uc
.tuc_link
);
3104 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->rs_uc
.tuc_stack
.ss_sp
);
3105 __put_user(target_sigaltstack_used
.ss_size
, &frame
->rs_uc
.tuc_stack
.ss_size
);
3106 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
3107 &frame
->rs_uc
.tuc_stack
.ss_flags
);
3109 setup_sigcontext(env
, &frame
->rs_uc
.tuc_mcontext
);
3111 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3112 __put_user(set
->sig
[i
], &frame
->rs_uc
.tuc_sigmask
.sig
[i
]);
3116 * Arguments to signal handler:
3118 * a0 = signal number
3119 * a1 = pointer to siginfo_t
3120 * a2 = pointer to struct ucontext
3122 * $25 and PC point to the signal handler, $29 points to the
3125 env
->active_tc
.gpr
[ 4] = sig
;
3126 env
->active_tc
.gpr
[ 5] = frame_addr
3127 + offsetof(struct target_rt_sigframe
, rs_info
);
3128 env
->active_tc
.gpr
[ 6] = frame_addr
3129 + offsetof(struct target_rt_sigframe
, rs_uc
);
3130 env
->active_tc
.gpr
[29] = frame_addr
;
3131 env
->active_tc
.gpr
[31] = frame_addr
3132 + offsetof(struct target_rt_sigframe
, rs_code
);
3133 /* The original kernel code sets CP0_EPC to the handler
3134 * since it returns to userland using eret
3135 * we cannot do this here, and we must set PC directly */
3136 env
->active_tc
.PC
= env
->active_tc
.gpr
[25] = ka
->_sa_handler
;
3137 mips_set_hflags_isa_mode_from_pc(env
);
3138 unlock_user_struct(frame
, frame_addr
, 1);
3142 unlock_user_struct(frame
, frame_addr
, 1);
3143 force_sig(TARGET_SIGSEGV
/*, current*/);
3146 long do_rt_sigreturn(CPUMIPSState
*env
)
3148 struct target_rt_sigframe
*frame
;
3149 abi_ulong frame_addr
;
3152 #if defined(DEBUG_SIGNAL)
3153 fprintf(stderr
, "do_rt_sigreturn\n");
3155 frame_addr
= env
->active_tc
.gpr
[29];
3156 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
3159 target_to_host_sigset(&blocked
, &frame
->rs_uc
.tuc_sigmask
);
3160 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3162 if (restore_sigcontext(env
, &frame
->rs_uc
.tuc_mcontext
))
3165 if (do_sigaltstack(frame_addr
+
3166 offsetof(struct target_rt_sigframe
, rs_uc
.tuc_stack
),
3167 0, get_sp_from_cpustate(env
)) == -EFAULT
)
3170 env
->active_tc
.PC
= env
->CP0_EPC
;
3171 mips_set_hflags_isa_mode_from_pc(env
);
3172 /* I am not sure this is right, but it seems to work
3173 * maybe a problem with nested signals ? */
3175 return -TARGET_QEMU_ESIGRETURN
;
3178 force_sig(TARGET_SIGSEGV
/*, current*/);
3182 #elif defined(TARGET_SH4)
3185 * code and data structures from linux kernel:
3186 * include/asm-sh/sigcontext.h
3187 * arch/sh/kernel/signal.c
3190 struct target_sigcontext
{
3191 target_ulong oldmask
;
3194 target_ulong sc_gregs
[16];
3198 target_ulong sc_gbr
;
3199 target_ulong sc_mach
;
3200 target_ulong sc_macl
;
3203 target_ulong sc_fpregs
[16];
3204 target_ulong sc_xfpregs
[16];
3205 unsigned int sc_fpscr
;
3206 unsigned int sc_fpul
;
3207 unsigned int sc_ownedfp
;
3210 struct target_sigframe
3212 struct target_sigcontext sc
;
3213 target_ulong extramask
[TARGET_NSIG_WORDS
-1];
3214 uint16_t retcode
[3];
3218 struct target_ucontext
{
3219 target_ulong tuc_flags
;
3220 struct target_ucontext
*tuc_link
;
3221 target_stack_t tuc_stack
;
3222 struct target_sigcontext tuc_mcontext
;
3223 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
3226 struct target_rt_sigframe
3228 struct target_siginfo info
;
3229 struct target_ucontext uc
;
3230 uint16_t retcode
[3];
3234 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3235 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3237 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3238 unsigned long sp
, size_t frame_size
)
3240 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags(sp
) == 0)) {
3241 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3244 return (sp
- frame_size
) & -8ul;
3247 static int setup_sigcontext(struct target_sigcontext
*sc
,
3248 CPUSH4State
*regs
, unsigned long mask
)
3253 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
3254 COPY(gregs
[0]); COPY(gregs
[1]);
3255 COPY(gregs
[2]); COPY(gregs
[3]);
3256 COPY(gregs
[4]); COPY(gregs
[5]);
3257 COPY(gregs
[6]); COPY(gregs
[7]);
3258 COPY(gregs
[8]); COPY(gregs
[9]);
3259 COPY(gregs
[10]); COPY(gregs
[11]);
3260 COPY(gregs
[12]); COPY(gregs
[13]);
3261 COPY(gregs
[14]); COPY(gregs
[15]);
3262 COPY(gbr
); COPY(mach
);
3263 COPY(macl
); COPY(pr
);
3267 for (i
=0; i
<16; i
++) {
3268 err
|= __put_user(regs
->fregs
[i
], &sc
->sc_fpregs
[i
]);
3270 err
|= __put_user(regs
->fpscr
, &sc
->sc_fpscr
);
3271 err
|= __put_user(regs
->fpul
, &sc
->sc_fpul
);
3273 /* non-iBCS2 extensions.. */
3274 err
|= __put_user(mask
, &sc
->oldmask
);
3279 static int restore_sigcontext(CPUSH4State
*regs
, struct target_sigcontext
*sc
,
3282 unsigned int err
= 0;
3285 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3287 COPY(gregs
[2]); COPY(gregs
[3]);
3288 COPY(gregs
[4]); COPY(gregs
[5]);
3289 COPY(gregs
[6]); COPY(gregs
[7]);
3290 COPY(gregs
[8]); COPY(gregs
[9]);
3291 COPY(gregs
[10]); COPY(gregs
[11]);
3292 COPY(gregs
[12]); COPY(gregs
[13]);
3293 COPY(gregs
[14]); COPY(gregs
[15]);
3294 COPY(gbr
); COPY(mach
);
3295 COPY(macl
); COPY(pr
);
3299 for (i
=0; i
<16; i
++) {
3300 err
|= __get_user(regs
->fregs
[i
], &sc
->sc_fpregs
[i
]);
3302 err
|= __get_user(regs
->fpscr
, &sc
->sc_fpscr
);
3303 err
|= __get_user(regs
->fpul
, &sc
->sc_fpul
);
3305 regs
->tra
= -1; /* disable syscall checks */
3306 err
|= __get_user(*r0_p
, &sc
->sc_gregs
[0]);
3310 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3311 target_sigset_t
*set
, CPUSH4State
*regs
)
3313 struct target_sigframe
*frame
;
3314 abi_ulong frame_addr
;
3319 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
3320 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3323 signal
= current_exec_domain_sig(sig
);
3325 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
3327 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
3328 err
|= __put_user(set
->sig
[i
+ 1], &frame
->extramask
[i
]);
3331 /* Set up to return from userspace. If provided, use a stub
3332 already in userspace. */
3333 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3334 regs
->pr
= (unsigned long) ka
->sa_restorer
;
3336 /* Generate return code (system call to sigreturn) */
3337 err
|= __put_user(MOVW(2), &frame
->retcode
[0]);
3338 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
3339 err
|= __put_user((TARGET_NR_sigreturn
), &frame
->retcode
[2]);
3340 regs
->pr
= (unsigned long) frame
->retcode
;
3346 /* Set up registers for signal handler */
3347 regs
->gregs
[15] = frame_addr
;
3348 regs
->gregs
[4] = signal
; /* Arg for signal handler */
3350 regs
->gregs
[6] = frame_addr
+= offsetof(typeof(*frame
), sc
);
3351 regs
->pc
= (unsigned long) ka
->_sa_handler
;
3353 unlock_user_struct(frame
, frame_addr
, 1);
3357 unlock_user_struct(frame
, frame_addr
, 1);
3358 force_sig(TARGET_SIGSEGV
);
3361 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3362 target_siginfo_t
*info
,
3363 target_sigset_t
*set
, CPUSH4State
*regs
)
3365 struct target_rt_sigframe
*frame
;
3366 abi_ulong frame_addr
;
3371 frame_addr
= get_sigframe(ka
, regs
->gregs
[15], sizeof(*frame
));
3372 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3375 signal
= current_exec_domain_sig(sig
);
3377 err
|= copy_siginfo_to_user(&frame
->info
, info
);
3379 /* Create the ucontext. */
3380 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
3381 err
|= __put_user(0, (unsigned long *)&frame
->uc
.tuc_link
);
3382 err
|= __put_user((unsigned long)target_sigaltstack_used
.ss_sp
,
3383 &frame
->uc
.tuc_stack
.ss_sp
);
3384 err
|= __put_user(sas_ss_flags(regs
->gregs
[15]),
3385 &frame
->uc
.tuc_stack
.ss_flags
);
3386 err
|= __put_user(target_sigaltstack_used
.ss_size
,
3387 &frame
->uc
.tuc_stack
.ss_size
);
3388 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
,
3390 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
3391 err
|= __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
3394 /* Set up to return from userspace. If provided, use a stub
3395 already in userspace. */
3396 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3397 regs
->pr
= (unsigned long) ka
->sa_restorer
;
3399 /* Generate return code (system call to sigreturn) */
3400 err
|= __put_user(MOVW(2), &frame
->retcode
[0]);
3401 err
|= __put_user(TRAP_NOARG
, &frame
->retcode
[1]);
3402 err
|= __put_user((TARGET_NR_rt_sigreturn
), &frame
->retcode
[2]);
3403 regs
->pr
= (unsigned long) frame
->retcode
;
3409 /* Set up registers for signal handler */
3410 regs
->gregs
[15] = frame_addr
;
3411 regs
->gregs
[4] = signal
; /* Arg for signal handler */
3412 regs
->gregs
[5] = frame_addr
+ offsetof(typeof(*frame
), info
);
3413 regs
->gregs
[6] = frame_addr
+ offsetof(typeof(*frame
), uc
);
3414 regs
->pc
= (unsigned long) ka
->_sa_handler
;
3416 unlock_user_struct(frame
, frame_addr
, 1);
3420 unlock_user_struct(frame
, frame_addr
, 1);
3421 force_sig(TARGET_SIGSEGV
);
3424 long do_sigreturn(CPUSH4State
*regs
)
3426 struct target_sigframe
*frame
;
3427 abi_ulong frame_addr
;
3429 target_sigset_t target_set
;
3434 #if defined(DEBUG_SIGNAL)
3435 fprintf(stderr
, "do_sigreturn\n");
3437 frame_addr
= regs
->gregs
[15];
3438 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
3441 err
|= __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
);
3442 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3443 err
|= (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]));
3449 target_to_host_sigset_internal(&blocked
, &target_set
);
3450 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3452 if (restore_sigcontext(regs
, &frame
->sc
, &r0
))
3455 unlock_user_struct(frame
, frame_addr
, 0);
3459 unlock_user_struct(frame
, frame_addr
, 0);
3460 force_sig(TARGET_SIGSEGV
);
3464 long do_rt_sigreturn(CPUSH4State
*regs
)
3466 struct target_rt_sigframe
*frame
;
3467 abi_ulong frame_addr
;
3471 #if defined(DEBUG_SIGNAL)
3472 fprintf(stderr
, "do_rt_sigreturn\n");
3474 frame_addr
= regs
->gregs
[15];
3475 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
3478 target_to_host_sigset(&blocked
, &frame
->uc
.tuc_sigmask
);
3479 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
3481 if (restore_sigcontext(regs
, &frame
->uc
.tuc_mcontext
, &r0
))
3484 if (do_sigaltstack(frame_addr
+
3485 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
3486 0, get_sp_from_cpustate(regs
)) == -EFAULT
)
3489 unlock_user_struct(frame
, frame_addr
, 0);
3493 unlock_user_struct(frame
, frame_addr
, 0);
3494 force_sig(TARGET_SIGSEGV
);
3497 #elif defined(TARGET_MICROBLAZE)
3499 struct target_sigcontext
{
3500 struct target_pt_regs regs
; /* needs to be first */
3504 struct target_stack_t
{
3507 unsigned int ss_size
;
3510 struct target_ucontext
{
3511 abi_ulong tuc_flags
;
3513 struct target_stack_t tuc_stack
;
3514 struct target_sigcontext tuc_mcontext
;
3515 uint32_t tuc_extramask
[TARGET_NSIG_WORDS
- 1];
3518 /* Signal frames. */
3519 struct target_signal_frame
{
3520 struct target_ucontext uc
;
3521 uint32_t extramask
[TARGET_NSIG_WORDS
- 1];
3525 struct rt_signal_frame
{
3531 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUMBState
*env
)
3533 __put_user(env
->regs
[0], &sc
->regs
.r0
);
3534 __put_user(env
->regs
[1], &sc
->regs
.r1
);
3535 __put_user(env
->regs
[2], &sc
->regs
.r2
);
3536 __put_user(env
->regs
[3], &sc
->regs
.r3
);
3537 __put_user(env
->regs
[4], &sc
->regs
.r4
);
3538 __put_user(env
->regs
[5], &sc
->regs
.r5
);
3539 __put_user(env
->regs
[6], &sc
->regs
.r6
);
3540 __put_user(env
->regs
[7], &sc
->regs
.r7
);
3541 __put_user(env
->regs
[8], &sc
->regs
.r8
);
3542 __put_user(env
->regs
[9], &sc
->regs
.r9
);
3543 __put_user(env
->regs
[10], &sc
->regs
.r10
);
3544 __put_user(env
->regs
[11], &sc
->regs
.r11
);
3545 __put_user(env
->regs
[12], &sc
->regs
.r12
);
3546 __put_user(env
->regs
[13], &sc
->regs
.r13
);
3547 __put_user(env
->regs
[14], &sc
->regs
.r14
);
3548 __put_user(env
->regs
[15], &sc
->regs
.r15
);
3549 __put_user(env
->regs
[16], &sc
->regs
.r16
);
3550 __put_user(env
->regs
[17], &sc
->regs
.r17
);
3551 __put_user(env
->regs
[18], &sc
->regs
.r18
);
3552 __put_user(env
->regs
[19], &sc
->regs
.r19
);
3553 __put_user(env
->regs
[20], &sc
->regs
.r20
);
3554 __put_user(env
->regs
[21], &sc
->regs
.r21
);
3555 __put_user(env
->regs
[22], &sc
->regs
.r22
);
3556 __put_user(env
->regs
[23], &sc
->regs
.r23
);
3557 __put_user(env
->regs
[24], &sc
->regs
.r24
);
3558 __put_user(env
->regs
[25], &sc
->regs
.r25
);
3559 __put_user(env
->regs
[26], &sc
->regs
.r26
);
3560 __put_user(env
->regs
[27], &sc
->regs
.r27
);
3561 __put_user(env
->regs
[28], &sc
->regs
.r28
);
3562 __put_user(env
->regs
[29], &sc
->regs
.r29
);
3563 __put_user(env
->regs
[30], &sc
->regs
.r30
);
3564 __put_user(env
->regs
[31], &sc
->regs
.r31
);
3565 __put_user(env
->sregs
[SR_PC
], &sc
->regs
.pc
);
3568 static void restore_sigcontext(struct target_sigcontext
*sc
, CPUMBState
*env
)
3570 __get_user(env
->regs
[0], &sc
->regs
.r0
);
3571 __get_user(env
->regs
[1], &sc
->regs
.r1
);
3572 __get_user(env
->regs
[2], &sc
->regs
.r2
);
3573 __get_user(env
->regs
[3], &sc
->regs
.r3
);
3574 __get_user(env
->regs
[4], &sc
->regs
.r4
);
3575 __get_user(env
->regs
[5], &sc
->regs
.r5
);
3576 __get_user(env
->regs
[6], &sc
->regs
.r6
);
3577 __get_user(env
->regs
[7], &sc
->regs
.r7
);
3578 __get_user(env
->regs
[8], &sc
->regs
.r8
);
3579 __get_user(env
->regs
[9], &sc
->regs
.r9
);
3580 __get_user(env
->regs
[10], &sc
->regs
.r10
);
3581 __get_user(env
->regs
[11], &sc
->regs
.r11
);
3582 __get_user(env
->regs
[12], &sc
->regs
.r12
);
3583 __get_user(env
->regs
[13], &sc
->regs
.r13
);
3584 __get_user(env
->regs
[14], &sc
->regs
.r14
);
3585 __get_user(env
->regs
[15], &sc
->regs
.r15
);
3586 __get_user(env
->regs
[16], &sc
->regs
.r16
);
3587 __get_user(env
->regs
[17], &sc
->regs
.r17
);
3588 __get_user(env
->regs
[18], &sc
->regs
.r18
);
3589 __get_user(env
->regs
[19], &sc
->regs
.r19
);
3590 __get_user(env
->regs
[20], &sc
->regs
.r20
);
3591 __get_user(env
->regs
[21], &sc
->regs
.r21
);
3592 __get_user(env
->regs
[22], &sc
->regs
.r22
);
3593 __get_user(env
->regs
[23], &sc
->regs
.r23
);
3594 __get_user(env
->regs
[24], &sc
->regs
.r24
);
3595 __get_user(env
->regs
[25], &sc
->regs
.r25
);
3596 __get_user(env
->regs
[26], &sc
->regs
.r26
);
3597 __get_user(env
->regs
[27], &sc
->regs
.r27
);
3598 __get_user(env
->regs
[28], &sc
->regs
.r28
);
3599 __get_user(env
->regs
[29], &sc
->regs
.r29
);
3600 __get_user(env
->regs
[30], &sc
->regs
.r30
);
3601 __get_user(env
->regs
[31], &sc
->regs
.r31
);
3602 __get_user(env
->sregs
[SR_PC
], &sc
->regs
.pc
);
3605 static abi_ulong
get_sigframe(struct target_sigaction
*ka
,
3606 CPUMBState
*env
, int frame_size
)
3608 abi_ulong sp
= env
->regs
[1];
3610 if ((ka
->sa_flags
& SA_ONSTACK
) != 0 && !on_sig_stack(sp
))
3611 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
3613 return ((sp
- frame_size
) & -8UL);
3616 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3617 target_sigset_t
*set
, CPUMBState
*env
)
3619 struct target_signal_frame
*frame
;
3620 abi_ulong frame_addr
;
3624 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
3625 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3628 /* Save the mask. */
3629 err
|= __put_user(set
->sig
[0], &frame
->uc
.tuc_mcontext
.oldmask
);
3633 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3634 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
3638 setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
);
3640 /* Set up to return from userspace. If provided, use a stub
3641 already in userspace. */
3642 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3643 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
3644 env
->regs
[15] = ((unsigned long)ka
->sa_restorer
)-8;
3647 /* Note, these encodings are _big endian_! */
3648 /* addi r12, r0, __NR_sigreturn */
3649 t
= 0x31800000UL
| TARGET_NR_sigreturn
;
3650 err
|= __put_user(t
, frame
->tramp
+ 0);
3653 err
|= __put_user(t
, frame
->tramp
+ 1);
3655 /* Return from sighandler will jump to the tramp.
3656 Negative 8 offset because return is rtsd r15, 8 */
3657 env
->regs
[15] = ((unsigned long)frame
->tramp
) - 8;
3663 /* Set up registers for signal handler */
3664 env
->regs
[1] = frame_addr
;
3665 /* Signal handler args: */
3666 env
->regs
[5] = sig
; /* Arg 0: signum */
3668 /* arg 1: sigcontext */
3669 env
->regs
[7] = frame_addr
+= offsetof(typeof(*frame
), uc
);
3671 /* Offset of 4 to handle microblaze rtid r14, 0 */
3672 env
->sregs
[SR_PC
] = (unsigned long)ka
->_sa_handler
;
3674 unlock_user_struct(frame
, frame_addr
, 1);
3677 unlock_user_struct(frame
, frame_addr
, 1);
3678 force_sig(TARGET_SIGSEGV
);
3681 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3682 target_siginfo_t
*info
,
3683 target_sigset_t
*set
, CPUMBState
*env
)
3685 fprintf(stderr
, "Microblaze setup_rt_frame: not implemented\n");
3688 long do_sigreturn(CPUMBState
*env
)
3690 struct target_signal_frame
*frame
;
3691 abi_ulong frame_addr
;
3692 target_sigset_t target_set
;
3696 frame_addr
= env
->regs
[R_SP
];
3697 /* Make sure the guest isn't playing games. */
3698 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
3701 /* Restore blocked signals */
3702 if (__get_user(target_set
.sig
[0], &frame
->uc
.tuc_mcontext
.oldmask
))
3704 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3705 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
3708 target_to_host_sigset_internal(&set
, &target_set
);
3709 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
3711 restore_sigcontext(&frame
->uc
.tuc_mcontext
, env
);
3712 /* We got here through a sigreturn syscall, our path back is via an
3713 rtb insn so setup r14 for that. */
3714 env
->regs
[14] = env
->sregs
[SR_PC
];
3716 unlock_user_struct(frame
, frame_addr
, 0);
3717 return env
->regs
[10];
3719 unlock_user_struct(frame
, frame_addr
, 0);
3720 force_sig(TARGET_SIGSEGV
);
3723 long do_rt_sigreturn(CPUMBState
*env
)
3725 fprintf(stderr
, "Microblaze do_rt_sigreturn: not implemented\n");
3726 return -TARGET_ENOSYS
;
3729 #elif defined(TARGET_CRIS)
3731 struct target_sigcontext
{
3732 struct target_pt_regs regs
; /* needs to be first */
3734 uint32_t usp
; /* usp before stacking this gunk on it */
3737 /* Signal frames. */
3738 struct target_signal_frame
{
3739 struct target_sigcontext sc
;
3740 uint32_t extramask
[TARGET_NSIG_WORDS
- 1];
3741 uint16_t retcode
[4]; /* Trampoline code. */
3744 struct rt_signal_frame
{
3749 uint16_t retcode
[4]; /* Trampoline code. */
3752 static void setup_sigcontext(struct target_sigcontext
*sc
, CPUCRISState
*env
)
3754 __put_user(env
->regs
[0], &sc
->regs
.r0
);
3755 __put_user(env
->regs
[1], &sc
->regs
.r1
);
3756 __put_user(env
->regs
[2], &sc
->regs
.r2
);
3757 __put_user(env
->regs
[3], &sc
->regs
.r3
);
3758 __put_user(env
->regs
[4], &sc
->regs
.r4
);
3759 __put_user(env
->regs
[5], &sc
->regs
.r5
);
3760 __put_user(env
->regs
[6], &sc
->regs
.r6
);
3761 __put_user(env
->regs
[7], &sc
->regs
.r7
);
3762 __put_user(env
->regs
[8], &sc
->regs
.r8
);
3763 __put_user(env
->regs
[9], &sc
->regs
.r9
);
3764 __put_user(env
->regs
[10], &sc
->regs
.r10
);
3765 __put_user(env
->regs
[11], &sc
->regs
.r11
);
3766 __put_user(env
->regs
[12], &sc
->regs
.r12
);
3767 __put_user(env
->regs
[13], &sc
->regs
.r13
);
3768 __put_user(env
->regs
[14], &sc
->usp
);
3769 __put_user(env
->regs
[15], &sc
->regs
.acr
);
3770 __put_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
3771 __put_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
3772 __put_user(env
->pc
, &sc
->regs
.erp
);
3775 static void restore_sigcontext(struct target_sigcontext
*sc
, CPUCRISState
*env
)
3777 __get_user(env
->regs
[0], &sc
->regs
.r0
);
3778 __get_user(env
->regs
[1], &sc
->regs
.r1
);
3779 __get_user(env
->regs
[2], &sc
->regs
.r2
);
3780 __get_user(env
->regs
[3], &sc
->regs
.r3
);
3781 __get_user(env
->regs
[4], &sc
->regs
.r4
);
3782 __get_user(env
->regs
[5], &sc
->regs
.r5
);
3783 __get_user(env
->regs
[6], &sc
->regs
.r6
);
3784 __get_user(env
->regs
[7], &sc
->regs
.r7
);
3785 __get_user(env
->regs
[8], &sc
->regs
.r8
);
3786 __get_user(env
->regs
[9], &sc
->regs
.r9
);
3787 __get_user(env
->regs
[10], &sc
->regs
.r10
);
3788 __get_user(env
->regs
[11], &sc
->regs
.r11
);
3789 __get_user(env
->regs
[12], &sc
->regs
.r12
);
3790 __get_user(env
->regs
[13], &sc
->regs
.r13
);
3791 __get_user(env
->regs
[14], &sc
->usp
);
3792 __get_user(env
->regs
[15], &sc
->regs
.acr
);
3793 __get_user(env
->pregs
[PR_MOF
], &sc
->regs
.mof
);
3794 __get_user(env
->pregs
[PR_SRP
], &sc
->regs
.srp
);
3795 __get_user(env
->pc
, &sc
->regs
.erp
);
3798 static abi_ulong
get_sigframe(CPUCRISState
*env
, int framesize
)
3801 /* Align the stack downwards to 4. */
3802 sp
= (env
->regs
[R_SP
] & ~3);
3803 return sp
- framesize
;
3806 static void setup_frame(int sig
, struct target_sigaction
*ka
,
3807 target_sigset_t
*set
, CPUCRISState
*env
)
3809 struct target_signal_frame
*frame
;
3810 abi_ulong frame_addr
;
3814 frame_addr
= get_sigframe(env
, sizeof *frame
);
3815 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
3819 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3820 * use this trampoline anymore but it sets it up for GDB.
3821 * In QEMU, using the trampoline simplifies things a bit so we use it.
3823 * This is movu.w __NR_sigreturn, r9; break 13;
3825 err
|= __put_user(0x9c5f, frame
->retcode
+0);
3826 err
|= __put_user(TARGET_NR_sigreturn
,
3827 frame
->retcode
+ 1);
3828 err
|= __put_user(0xe93d, frame
->retcode
+ 2);
3830 /* Save the mask. */
3831 err
|= __put_user(set
->sig
[0], &frame
->sc
.oldmask
);
3835 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3836 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
3840 setup_sigcontext(&frame
->sc
, env
);
3842 /* Move the stack and setup the arguments for the handler. */
3843 env
->regs
[R_SP
] = frame_addr
;
3844 env
->regs
[10] = sig
;
3845 env
->pc
= (unsigned long) ka
->_sa_handler
;
3846 /* Link SRP so the guest returns through the trampoline. */
3847 env
->pregs
[PR_SRP
] = frame_addr
+ offsetof(typeof(*frame
), retcode
);
3849 unlock_user_struct(frame
, frame_addr
, 1);
3852 unlock_user_struct(frame
, frame_addr
, 1);
3853 force_sig(TARGET_SIGSEGV
);
3856 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
3857 target_siginfo_t
*info
,
3858 target_sigset_t
*set
, CPUCRISState
*env
)
3860 fprintf(stderr
, "CRIS setup_rt_frame: not implemented\n");
3863 long do_sigreturn(CPUCRISState
*env
)
3865 struct target_signal_frame
*frame
;
3866 abi_ulong frame_addr
;
3867 target_sigset_t target_set
;
3871 frame_addr
= env
->regs
[R_SP
];
3872 /* Make sure the guest isn't playing games. */
3873 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
3876 /* Restore blocked signals */
3877 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
))
3879 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
3880 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
3883 target_to_host_sigset_internal(&set
, &target_set
);
3884 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
3886 restore_sigcontext(&frame
->sc
, env
);
3887 unlock_user_struct(frame
, frame_addr
, 0);
3888 return env
->regs
[10];
3890 unlock_user_struct(frame
, frame_addr
, 0);
3891 force_sig(TARGET_SIGSEGV
);
3894 long do_rt_sigreturn(CPUCRISState
*env
)
3896 fprintf(stderr
, "CRIS do_rt_sigreturn: not implemented\n");
3897 return -TARGET_ENOSYS
;
3900 #elif defined(TARGET_OPENRISC)
3902 struct target_sigcontext
{
3903 struct target_pt_regs regs
;
3908 struct target_ucontext
{
3909 abi_ulong tuc_flags
;
3911 target_stack_t tuc_stack
;
3912 struct target_sigcontext tuc_mcontext
;
3913 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
3916 struct target_rt_sigframe
{
3919 struct target_siginfo info
;
3920 struct target_sigcontext sc
;
3921 struct target_ucontext uc
;
3922 unsigned char retcode
[16]; /* trampoline code */
3925 /* This is the asm-generic/ucontext.h version */
3927 static int restore_sigcontext(CPUOpenRISCState
*regs
,
3928 struct target_sigcontext
*sc
)
3930 unsigned int err
= 0;
3931 unsigned long old_usp
;
3933 /* Alwys make any pending restarted system call return -EINTR */
3934 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
3936 /* restore the regs from &sc->regs (same as sc, since regs is first)
3937 * (sc is already checked for VERIFY_READ since the sigframe was
3938 * checked in sys_sigreturn previously)
3941 if (copy_from_user(regs
, &sc
, sizeof(struct target_pt_regs
))) {
3945 /* make sure the U-flag is set so user-mode cannot fool us */
3949 /* restore the old USP as it was before we stacked the sc etc.
3950 * (we cannot just pop the sigcontext since we aligned the sp and
3951 * stuff after pushing it)
3954 err
|= __get_user(old_usp
, &sc
->usp
);
3955 phx_signal("old_usp 0x%lx", old_usp
);
3957 __PHX__ REALLY
/* ??? */
3959 regs
->gpr
[1] = old_usp
;
3961 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3962 * after this completes, but we don't use that mechanism. maybe we can
3973 /* Set up a signal frame. */
3975 static int setup_sigcontext(struct target_sigcontext
*sc
,
3976 CPUOpenRISCState
*regs
,
3980 unsigned long usp
= regs
->gpr
[1];
3982 /* copy the regs. they are first in sc so we can use sc directly */
3984 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3986 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3987 the signal handler. The frametype will be restored to its previous
3988 value in restore_sigcontext. */
3989 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3991 /* then some other stuff */
3992 err
|= __put_user(mask
, &sc
->oldmask
);
3993 err
|= __put_user(usp
, &sc
->usp
); return err
;
3996 static inline unsigned long align_sigframe(unsigned long sp
)
4003 static inline abi_ulong
get_sigframe(struct target_sigaction
*ka
,
4004 CPUOpenRISCState
*regs
,
4007 unsigned long sp
= regs
->gpr
[1];
4008 int onsigstack
= on_sig_stack(sp
);
4011 /* This is the X/Open sanctioned signal stack switching. */
4012 if ((ka
->sa_flags
& SA_ONSTACK
) != 0 && !onsigstack
) {
4013 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
4016 sp
= align_sigframe(sp
- frame_size
);
4019 * If we are on the alternate signal stack and would overflow it, don't.
4020 * Return an always-bogus address instead so we will die with SIGSEGV.
4023 if (onsigstack
&& !likely(on_sig_stack(sp
))) {
4030 static void setup_frame(int sig
, struct target_sigaction
*ka
,
4031 target_sigset_t
*set
, CPUOpenRISCState
*env
)
4033 qemu_log("Not implement.\n");
4036 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
4037 target_siginfo_t
*info
,
4038 target_sigset_t
*set
, CPUOpenRISCState
*env
)
4041 abi_ulong frame_addr
;
4042 unsigned long return_ip
;
4043 struct target_rt_sigframe
*frame
;
4044 abi_ulong info_addr
, uc_addr
;
4046 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
4047 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
4051 info_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
4052 err
|= __put_user(info_addr
, &frame
->pinfo
);
4053 uc_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
4054 err
|= __put_user(uc_addr
, &frame
->puc
);
4056 if (ka
->sa_flags
& SA_SIGINFO
) {
4057 err
|= copy_siginfo_to_user(&frame
->info
, info
);
4063 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
4064 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
4065 err
|= __put_user(0, &frame
->uc
.tuc_link
);
4066 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
4067 &frame
->uc
.tuc_stack
.ss_sp
);
4068 err
|= __put_user(sas_ss_flags(env
->gpr
[1]), &frame
->uc
.tuc_stack
.ss_flags
);
4069 err
|= __put_user(target_sigaltstack_used
.ss_size
,
4070 &frame
->uc
.tuc_stack
.ss_size
);
4071 err
|= setup_sigcontext(&frame
->sc
, env
, set
->sig
[0]);
4073 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4079 /* trampoline - the desired return ip is the retcode itself */
4080 return_ip
= (unsigned long)&frame
->retcode
;
4081 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
4082 err
|= __put_user(0xa960, (short *)(frame
->retcode
+ 0));
4083 err
|= __put_user(TARGET_NR_rt_sigreturn
, (short *)(frame
->retcode
+ 2));
4084 err
|= __put_user(0x20000001, (unsigned long *)(frame
->retcode
+ 4));
4085 err
|= __put_user(0x15000000, (unsigned long *)(frame
->retcode
+ 8));
4091 /* TODO what is the current->exec_domain stuff and invmap ? */
4093 /* Set up registers for signal handler */
4094 env
->pc
= (unsigned long)ka
->_sa_handler
; /* what we enter NOW */
4095 env
->gpr
[9] = (unsigned long)return_ip
; /* what we enter LATER */
4096 env
->gpr
[3] = (unsigned long)sig
; /* arg 1: signo */
4097 env
->gpr
[4] = (unsigned long)&frame
->info
; /* arg 2: (siginfo_t*) */
4098 env
->gpr
[5] = (unsigned long)&frame
->uc
; /* arg 3: ucontext */
4100 /* actually move the usp to reflect the stacked frame */
4101 env
->gpr
[1] = (unsigned long)frame
;
4106 unlock_user_struct(frame
, frame_addr
, 1);
4107 if (sig
== TARGET_SIGSEGV
) {
4108 ka
->_sa_handler
= TARGET_SIG_DFL
;
4110 force_sig(TARGET_SIGSEGV
);
4113 long do_sigreturn(CPUOpenRISCState
*env
)
4116 qemu_log("do_sigreturn: not implemented\n");
4117 return -TARGET_ENOSYS
;
4120 long do_rt_sigreturn(CPUOpenRISCState
*env
)
4122 qemu_log("do_rt_sigreturn: not implemented\n");
4123 return -TARGET_ENOSYS
;
4125 /* TARGET_OPENRISC */
4127 #elif defined(TARGET_S390X)
4129 #define __NUM_GPRS 16
4130 #define __NUM_FPRS 16
4131 #define __NUM_ACRS 16
4133 #define S390_SYSCALL_SIZE 2
4134 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4136 #define _SIGCONTEXT_NSIG 64
4137 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4138 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4139 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4140 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4141 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4145 target_ulong gprs
[__NUM_GPRS
];
4146 unsigned int acrs
[__NUM_ACRS
];
4147 } target_s390_regs_common
;
4151 double fprs
[__NUM_FPRS
];
4152 } target_s390_fp_regs
;
4155 target_s390_regs_common regs
;
4156 target_s390_fp_regs fpregs
;
4159 struct target_sigcontext
{
4160 target_ulong oldmask
[_SIGCONTEXT_NSIG_WORDS
];
4161 target_sigregs
*sregs
;
4165 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
4166 struct target_sigcontext sc
;
4167 target_sigregs sregs
;
4169 uint8_t retcode
[S390_SYSCALL_SIZE
];
4172 struct target_ucontext
{
4173 target_ulong tuc_flags
;
4174 struct target_ucontext
*tuc_link
;
4175 target_stack_t tuc_stack
;
4176 target_sigregs tuc_mcontext
;
4177 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
4181 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
4182 uint8_t retcode
[S390_SYSCALL_SIZE
];
4183 struct target_siginfo info
;
4184 struct target_ucontext uc
;
4187 static inline abi_ulong
4188 get_sigframe(struct target_sigaction
*ka
, CPUS390XState
*env
, size_t frame_size
)
4192 /* Default to using normal stack */
4195 /* This is the X/Open sanctioned signal stack switching. */
4196 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
4197 if (!sas_ss_flags(sp
)) {
4198 sp
= target_sigaltstack_used
.ss_sp
+
4199 target_sigaltstack_used
.ss_size
;
4203 /* This is the legacy signal stack switching. */
4204 else if (/* FIXME !user_mode(regs) */ 0 &&
4205 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
4207 sp
= (abi_ulong
) ka
->sa_restorer
;
4210 return (sp
- frame_size
) & -8ul;
4213 static void save_sigregs(CPUS390XState
*env
, target_sigregs
*sregs
)
4216 //save_access_regs(current->thread.acrs); FIXME
4218 /* Copy a 'clean' PSW mask to the user to avoid leaking
4219 information about whether PER is currently on. */
4220 __put_user(env
->psw
.mask
, &sregs
->regs
.psw
.mask
);
4221 __put_user(env
->psw
.addr
, &sregs
->regs
.psw
.addr
);
4222 for (i
= 0; i
< 16; i
++) {
4223 __put_user(env
->regs
[i
], &sregs
->regs
.gprs
[i
]);
4225 for (i
= 0; i
< 16; i
++) {
4226 __put_user(env
->aregs
[i
], &sregs
->regs
.acrs
[i
]);
4229 * We have to store the fp registers to current->thread.fp_regs
4230 * to merge them with the emulated registers.
4232 //save_fp_regs(¤t->thread.fp_regs); FIXME
4233 for (i
= 0; i
< 16; i
++) {
4234 __put_user(env
->fregs
[i
].ll
, &sregs
->fpregs
.fprs
[i
]);
4238 static void setup_frame(int sig
, struct target_sigaction
*ka
,
4239 target_sigset_t
*set
, CPUS390XState
*env
)
4242 abi_ulong frame_addr
;
4244 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
4245 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4246 (unsigned long long)frame_addr
);
4247 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
4251 qemu_log("%s: 1\n", __FUNCTION__
);
4252 if (__put_user(set
->sig
[0], &frame
->sc
.oldmask
[0])) {
4256 save_sigregs(env
, &frame
->sregs
);
4258 __put_user((abi_ulong
)(unsigned long)&frame
->sregs
,
4259 (abi_ulong
*)&frame
->sc
.sregs
);
4261 /* Set up to return from userspace. If provided, use a stub
4262 already in userspace. */
4263 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
4264 env
->regs
[14] = (unsigned long)
4265 ka
->sa_restorer
| PSW_ADDR_AMODE
;
4267 env
->regs
[14] = (unsigned long)
4268 frame
->retcode
| PSW_ADDR_AMODE
;
4269 if (__put_user(S390_SYSCALL_OPCODE
| TARGET_NR_sigreturn
,
4270 (uint16_t *)(frame
->retcode
)))
4274 /* Set up backchain. */
4275 if (__put_user(env
->regs
[15], (abi_ulong
*) frame
)) {
4279 /* Set up registers for signal handler */
4280 env
->regs
[15] = frame_addr
;
4281 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
4283 env
->regs
[2] = sig
; //map_signal(sig);
4284 env
->regs
[3] = frame_addr
+= offsetof(typeof(*frame
), sc
);
4286 /* We forgot to include these in the sigcontext.
4287 To avoid breaking binary compatibility, they are passed as args. */
4288 env
->regs
[4] = 0; // FIXME: no clue... current->thread.trap_no;
4289 env
->regs
[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4291 /* Place signal number on stack to allow backtrace from handler. */
4292 if (__put_user(env
->regs
[2], (int *) &frame
->signo
)) {
4295 unlock_user_struct(frame
, frame_addr
, 1);
4299 qemu_log("%s: give_sigsegv\n", __FUNCTION__
);
4300 unlock_user_struct(frame
, frame_addr
, 1);
4301 force_sig(TARGET_SIGSEGV
);
4304 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
4305 target_siginfo_t
*info
,
4306 target_sigset_t
*set
, CPUS390XState
*env
)
4310 abi_ulong frame_addr
;
4312 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
4313 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4314 (unsigned long long)frame_addr
);
4315 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
4319 qemu_log("%s: 1\n", __FUNCTION__
);
4320 if (copy_siginfo_to_user(&frame
->info
, info
)) {
4324 /* Create the ucontext. */
4325 __put_user(0, &frame
->uc
.tuc_flags
);
4326 __put_user((abi_ulong
)0, (abi_ulong
*)&frame
->uc
.tuc_link
);
4327 __put_user(target_sigaltstack_used
.ss_sp
, &frame
->uc
.tuc_stack
.ss_sp
);
4328 __put_user(sas_ss_flags(get_sp_from_cpustate(env
)),
4329 &frame
->uc
.tuc_stack
.ss_flags
);
4330 __put_user(target_sigaltstack_used
.ss_size
, &frame
->uc
.tuc_stack
.ss_size
);
4331 save_sigregs(env
, &frame
->uc
.tuc_mcontext
);
4332 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
4333 __put_user((abi_ulong
)set
->sig
[i
],
4334 (abi_ulong
*)&frame
->uc
.tuc_sigmask
.sig
[i
]);
4337 /* Set up to return from userspace. If provided, use a stub
4338 already in userspace. */
4339 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
4340 env
->regs
[14] = (unsigned long) ka
->sa_restorer
| PSW_ADDR_AMODE
;
4342 env
->regs
[14] = (unsigned long) frame
->retcode
| PSW_ADDR_AMODE
;
4343 if (__put_user(S390_SYSCALL_OPCODE
| TARGET_NR_rt_sigreturn
,
4344 (uint16_t *)(frame
->retcode
))) {
4349 /* Set up backchain. */
4350 if (__put_user(env
->regs
[15], (abi_ulong
*) frame
)) {
4354 /* Set up registers for signal handler */
4355 env
->regs
[15] = frame_addr
;
4356 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
4358 env
->regs
[2] = sig
; //map_signal(sig);
4359 env
->regs
[3] = frame_addr
+ offsetof(typeof(*frame
), info
);
4360 env
->regs
[4] = frame_addr
+ offsetof(typeof(*frame
), uc
);
4364 qemu_log("%s: give_sigsegv\n", __FUNCTION__
);
4365 unlock_user_struct(frame
, frame_addr
, 1);
4366 force_sig(TARGET_SIGSEGV
);
4370 restore_sigregs(CPUS390XState
*env
, target_sigregs
*sc
)
4375 for (i
= 0; i
< 16; i
++) {
4376 err
|= __get_user(env
->regs
[i
], &sc
->regs
.gprs
[i
]);
4379 err
|= __get_user(env
->psw
.mask
, &sc
->regs
.psw
.mask
);
4380 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4381 __FUNCTION__
, (unsigned long long)sc
->regs
.psw
.addr
,
4382 (unsigned long long)env
->psw
.addr
);
4383 err
|= __get_user(env
->psw
.addr
, &sc
->regs
.psw
.addr
);
4384 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4386 for (i
= 0; i
< 16; i
++) {
4387 err
|= __get_user(env
->aregs
[i
], &sc
->regs
.acrs
[i
]);
4389 for (i
= 0; i
< 16; i
++) {
4390 err
|= __get_user(env
->fregs
[i
].ll
, &sc
->fpregs
.fprs
[i
]);
4396 long do_sigreturn(CPUS390XState
*env
)
4399 abi_ulong frame_addr
= env
->regs
[15];
4400 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4401 (unsigned long long)frame_addr
);
4402 target_sigset_t target_set
;
4405 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
4408 if (__get_user(target_set
.sig
[0], &frame
->sc
.oldmask
[0])) {
4412 target_to_host_sigset_internal(&set
, &target_set
);
4413 do_sigprocmask(SIG_SETMASK
, &set
, NULL
); /* ~_BLOCKABLE? */
4415 if (restore_sigregs(env
, &frame
->sregs
)) {
4419 unlock_user_struct(frame
, frame_addr
, 0);
4420 return env
->regs
[2];
4423 unlock_user_struct(frame
, frame_addr
, 0);
4424 force_sig(TARGET_SIGSEGV
);
4428 long do_rt_sigreturn(CPUS390XState
*env
)
4431 abi_ulong frame_addr
= env
->regs
[15];
4432 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__
,
4433 (unsigned long long)frame_addr
);
4436 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
4439 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
4441 do_sigprocmask(SIG_SETMASK
, &set
, NULL
); /* ~_BLOCKABLE? */
4443 if (restore_sigregs(env
, &frame
->uc
.tuc_mcontext
)) {
4447 if (do_sigaltstack(frame_addr
+ offsetof(rt_sigframe
, uc
.tuc_stack
), 0,
4448 get_sp_from_cpustate(env
)) == -EFAULT
) {
4451 unlock_user_struct(frame
, frame_addr
, 0);
4452 return env
->regs
[2];
4455 unlock_user_struct(frame
, frame_addr
, 0);
4456 force_sig(TARGET_SIGSEGV
);
4460 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4462 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4463 the signal handling is different enough that we haven't implemented
4464 support for PPC64 yet. Hence the restriction above.
4466 There are various #if'd blocks for code for TARGET_PPC64. These
4467 blocks should go away so that we can successfully run 32-bit and
4468 64-bit binaries on a QEMU configured for PPC64. */
4470 /* Size of dummy stack frame allocated when calling signal handler.
4471 See arch/powerpc/include/asm/ptrace.h. */
4472 #if defined(TARGET_PPC64)
4473 #define SIGNAL_FRAMESIZE 128
4475 #define SIGNAL_FRAMESIZE 64
4478 /* See arch/powerpc/include/asm/sigcontext.h. */
4479 struct target_sigcontext
{
4480 target_ulong _unused
[4];
4482 #if defined(TARGET_PPC64)
4485 target_ulong handler
;
4486 target_ulong oldmask
;
4487 target_ulong regs
; /* struct pt_regs __user * */
4488 /* TODO: PPC64 includes extra bits here. */
4491 /* Indices for target_mcontext.mc_gregs, below.
4492 See arch/powerpc/include/asm/ptrace.h for details. */
4528 TARGET_PT_ORIG_R3
= 34,
4533 /* Yes, there are two registers with #39. One is 64-bit only. */
4535 TARGET_PT_SOFTE
= 39,
4536 TARGET_PT_TRAP
= 40,
4538 TARGET_PT_DSISR
= 42,
4539 TARGET_PT_RESULT
= 43,
4540 TARGET_PT_REGS_COUNT
= 44
4543 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4544 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4545 struct target_mcontext
{
4546 target_ulong mc_gregs
[48];
4547 /* Includes fpscr. */
4548 uint64_t mc_fregs
[33];
4549 target_ulong mc_pad
[2];
4550 /* We need to handle Altivec and SPE at the same time, which no
4551 kernel needs to do. Fortunately, the kernel defines this bit to
4552 be Altivec-register-large all the time, rather than trying to
4553 twiddle it based on the specific platform. */
4555 /* SPE vector registers. One extra for SPEFSCR. */
4557 /* Altivec vector registers. The packing of VSCR and VRSAVE
4558 varies depending on whether we're PPC64 or not: PPC64 splits
4559 them apart; PPC32 stuffs them together. */
4560 #if defined(TARGET_PPC64)
4561 #define QEMU_NVRREG 34
4563 #define QEMU_NVRREG 33
4565 ppc_avr_t altivec
[QEMU_NVRREG
];
4567 } mc_vregs
__attribute__((__aligned__(16)));
4570 struct target_ucontext
{
4571 target_ulong tuc_flags
;
4572 target_ulong tuc_link
; /* struct ucontext __user * */
4573 struct target_sigaltstack tuc_stack
;
4574 #if !defined(TARGET_PPC64)
4576 target_ulong tuc_regs
; /* struct mcontext __user *
4577 points to uc_mcontext field */
4579 target_sigset_t tuc_sigmask
;
4580 #if defined(TARGET_PPC64)
4581 target_sigset_t unused
[15]; /* Allow for uc_sigmask growth */
4582 struct target_sigcontext tuc_mcontext
;
4584 int32_t tuc_maskext
[30];
4585 int32_t tuc_pad2
[3];
4586 struct target_mcontext tuc_mcontext
;
4590 /* See arch/powerpc/kernel/signal_32.c. */
4591 struct target_sigframe
{
4592 struct target_sigcontext sctx
;
4593 struct target_mcontext mctx
;
4597 struct target_rt_sigframe
{
4598 struct target_siginfo info
;
4599 struct target_ucontext uc
;
4603 /* We use the mc_pad field for the signal return trampoline. */
4604 #define tramp mc_pad
4606 /* See arch/powerpc/kernel/signal.c. */
4607 static target_ulong
get_sigframe(struct target_sigaction
*ka
,
4611 target_ulong oldsp
, newsp
;
4613 oldsp
= env
->gpr
[1];
4615 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) &&
4616 (sas_ss_flags(oldsp
) == 0)) {
4617 oldsp
= (target_sigaltstack_used
.ss_sp
4618 + target_sigaltstack_used
.ss_size
);
4621 newsp
= (oldsp
- frame_size
) & ~0xFUL
;
4626 static int save_user_regs(CPUPPCState
*env
, struct target_mcontext
*frame
,
4629 target_ulong msr
= env
->msr
;
4631 target_ulong ccr
= 0;
4633 /* In general, the kernel attempts to be intelligent about what it
4634 needs to save for Altivec/FP/SPE registers. We don't care that
4635 much, so we just go ahead and save everything. */
4637 /* Save general registers. */
4638 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4639 if (__put_user(env
->gpr
[i
], &frame
->mc_gregs
[i
])) {
4643 if (__put_user(env
->nip
, &frame
->mc_gregs
[TARGET_PT_NIP
])
4644 || __put_user(env
->ctr
, &frame
->mc_gregs
[TARGET_PT_CTR
])
4645 || __put_user(env
->lr
, &frame
->mc_gregs
[TARGET_PT_LNK
])
4646 || __put_user(env
->xer
, &frame
->mc_gregs
[TARGET_PT_XER
]))
4649 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
4650 ccr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
4652 if (__put_user(ccr
, &frame
->mc_gregs
[TARGET_PT_CCR
]))
4655 /* Save Altivec registers if necessary. */
4656 if (env
->insns_flags
& PPC_ALTIVEC
) {
4657 for (i
= 0; i
< ARRAY_SIZE(env
->avr
); i
++) {
4658 ppc_avr_t
*avr
= &env
->avr
[i
];
4659 ppc_avr_t
*vreg
= &frame
->mc_vregs
.altivec
[i
];
4661 if (__put_user(avr
->u64
[0], &vreg
->u64
[0]) ||
4662 __put_user(avr
->u64
[1], &vreg
->u64
[1])) {
4666 /* Set MSR_VR in the saved MSR value to indicate that
4667 frame->mc_vregs contains valid data. */
4669 if (__put_user((uint32_t)env
->spr
[SPR_VRSAVE
],
4670 &frame
->mc_vregs
.altivec
[32].u32
[3]))
4674 /* Save floating point registers. */
4675 if (env
->insns_flags
& PPC_FLOAT
) {
4676 for (i
= 0; i
< ARRAY_SIZE(env
->fpr
); i
++) {
4677 if (__put_user(env
->fpr
[i
], &frame
->mc_fregs
[i
])) {
4681 if (__put_user((uint64_t) env
->fpscr
, &frame
->mc_fregs
[32]))
4685 /* Save SPE registers. The kernel only saves the high half. */
4686 if (env
->insns_flags
& PPC_SPE
) {
4687 #if defined(TARGET_PPC64)
4688 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4689 if (__put_user(env
->gpr
[i
] >> 32, &frame
->mc_vregs
.spe
[i
])) {
4694 for (i
= 0; i
< ARRAY_SIZE(env
->gprh
); i
++) {
4695 if (__put_user(env
->gprh
[i
], &frame
->mc_vregs
.spe
[i
])) {
4700 /* Set MSR_SPE in the saved MSR value to indicate that
4701 frame->mc_vregs contains valid data. */
4703 if (__put_user(env
->spe_fscr
, &frame
->mc_vregs
.spe
[32]))
4708 if (__put_user(msr
, &frame
->mc_gregs
[TARGET_PT_MSR
]))
4711 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4713 if (__put_user(0x38000000UL
| sigret
, &frame
->tramp
[0]) ||
4714 __put_user(0x44000002UL
, &frame
->tramp
[1])) {
4722 static int restore_user_regs(CPUPPCState
*env
,
4723 struct target_mcontext
*frame
, int sig
)
4725 target_ulong save_r2
= 0;
4732 save_r2
= env
->gpr
[2];
4735 /* Restore general registers. */
4736 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4737 if (__get_user(env
->gpr
[i
], &frame
->mc_gregs
[i
])) {
4741 if (__get_user(env
->nip
, &frame
->mc_gregs
[TARGET_PT_NIP
])
4742 || __get_user(env
->ctr
, &frame
->mc_gregs
[TARGET_PT_CTR
])
4743 || __get_user(env
->lr
, &frame
->mc_gregs
[TARGET_PT_LNK
])
4744 || __get_user(env
->xer
, &frame
->mc_gregs
[TARGET_PT_XER
]))
4746 if (__get_user(ccr
, &frame
->mc_gregs
[TARGET_PT_CCR
]))
4749 for (i
= 0; i
< ARRAY_SIZE(env
->crf
); i
++) {
4750 env
->crf
[i
] = (ccr
>> (32 - ((i
+ 1) * 4))) & 0xf;
4754 env
->gpr
[2] = save_r2
;
4757 if (__get_user(msr
, &frame
->mc_gregs
[TARGET_PT_MSR
]))
4760 /* If doing signal return, restore the previous little-endian mode. */
4762 env
->msr
= (env
->msr
& ~MSR_LE
) | (msr
& MSR_LE
);
4764 /* Restore Altivec registers if necessary. */
4765 if (env
->insns_flags
& PPC_ALTIVEC
) {
4766 for (i
= 0; i
< ARRAY_SIZE(env
->avr
); i
++) {
4767 ppc_avr_t
*avr
= &env
->avr
[i
];
4768 ppc_avr_t
*vreg
= &frame
->mc_vregs
.altivec
[i
];
4770 if (__get_user(avr
->u64
[0], &vreg
->u64
[0]) ||
4771 __get_user(avr
->u64
[1], &vreg
->u64
[1])) {
4775 /* Set MSR_VEC in the saved MSR value to indicate that
4776 frame->mc_vregs contains valid data. */
4777 if (__get_user(env
->spr
[SPR_VRSAVE
],
4778 (target_ulong
*)(&frame
->mc_vregs
.altivec
[32].u32
[3])))
4782 /* Restore floating point registers. */
4783 if (env
->insns_flags
& PPC_FLOAT
) {
4785 for (i
= 0; i
< ARRAY_SIZE(env
->fpr
); i
++) {
4786 if (__get_user(env
->fpr
[i
], &frame
->mc_fregs
[i
])) {
4790 if (__get_user(fpscr
, &frame
->mc_fregs
[32]))
4792 env
->fpscr
= (uint32_t) fpscr
;
4795 /* Save SPE registers. The kernel only saves the high half. */
4796 if (env
->insns_flags
& PPC_SPE
) {
4797 #if defined(TARGET_PPC64)
4798 for (i
= 0; i
< ARRAY_SIZE(env
->gpr
); i
++) {
4801 if (__get_user(hi
, &frame
->mc_vregs
.spe
[i
])) {
4804 env
->gpr
[i
] = ((uint64_t)hi
<< 32) | ((uint32_t) env
->gpr
[i
]);
4807 for (i
= 0; i
< ARRAY_SIZE(env
->gprh
); i
++) {
4808 if (__get_user(env
->gprh
[i
], &frame
->mc_vregs
.spe
[i
])) {
4813 if (__get_user(env
->spe_fscr
, &frame
->mc_vregs
.spe
[32]))
4820 static void setup_frame(int sig
, struct target_sigaction
*ka
,
4821 target_sigset_t
*set
, CPUPPCState
*env
)
4823 struct target_sigframe
*frame
;
4824 struct target_sigcontext
*sc
;
4825 target_ulong frame_addr
, newsp
;
4829 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
4830 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 1))
4834 signal
= current_exec_domain_sig(sig
);
4836 err
|= __put_user(ka
->_sa_handler
, &sc
->handler
);
4837 err
|= __put_user(set
->sig
[0], &sc
->oldmask
);
4838 #if defined(TARGET_PPC64)
4839 err
|= __put_user(set
->sig
[0] >> 32, &sc
->_unused
[3]);
4841 err
|= __put_user(set
->sig
[1], &sc
->_unused
[3]);
4843 err
|= __put_user(h2g(&frame
->mctx
), &sc
->regs
);
4844 err
|= __put_user(sig
, &sc
->signal
);
4846 /* Save user regs. */
4847 err
|= save_user_regs(env
, &frame
->mctx
, TARGET_NR_sigreturn
);
4849 /* The kernel checks for the presence of a VDSO here. We don't
4850 emulate a vdso, so use a sigreturn system call. */
4851 env
->lr
= (target_ulong
) h2g(frame
->mctx
.tramp
);
4853 /* Turn off all fp exceptions. */
4856 /* Create a stack frame for the caller of the handler. */
4857 newsp
= frame_addr
- SIGNAL_FRAMESIZE
;
4858 err
|= put_user(env
->gpr
[1], newsp
, target_ulong
);
4863 /* Set up registers for signal handler. */
4864 env
->gpr
[1] = newsp
;
4865 env
->gpr
[3] = signal
;
4866 env
->gpr
[4] = frame_addr
+ offsetof(struct target_sigframe
, sctx
);
4867 env
->nip
= (target_ulong
) ka
->_sa_handler
;
4868 /* Signal handlers are entered in big-endian mode. */
4869 env
->msr
&= ~MSR_LE
;
4871 unlock_user_struct(frame
, frame_addr
, 1);
4875 unlock_user_struct(frame
, frame_addr
, 1);
4876 qemu_log("segfaulting from setup_frame\n");
4877 force_sig(TARGET_SIGSEGV
);
4880 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
4881 target_siginfo_t
*info
,
4882 target_sigset_t
*set
, CPUPPCState
*env
)
4884 struct target_rt_sigframe
*rt_sf
;
4885 struct target_mcontext
*frame
;
4886 target_ulong rt_sf_addr
, newsp
= 0;
4890 rt_sf_addr
= get_sigframe(ka
, env
, sizeof(*rt_sf
));
4891 if (!lock_user_struct(VERIFY_WRITE
, rt_sf
, rt_sf_addr
, 1))
4894 signal
= current_exec_domain_sig(sig
);
4896 err
|= copy_siginfo_to_user(&rt_sf
->info
, info
);
4898 err
|= __put_user(0, &rt_sf
->uc
.tuc_flags
);
4899 err
|= __put_user(0, &rt_sf
->uc
.tuc_link
);
4900 err
|= __put_user((target_ulong
)target_sigaltstack_used
.ss_sp
,
4901 &rt_sf
->uc
.tuc_stack
.ss_sp
);
4902 err
|= __put_user(sas_ss_flags(env
->gpr
[1]),
4903 &rt_sf
->uc
.tuc_stack
.ss_flags
);
4904 err
|= __put_user(target_sigaltstack_used
.ss_size
,
4905 &rt_sf
->uc
.tuc_stack
.ss_size
);
4906 err
|= __put_user(h2g (&rt_sf
->uc
.tuc_mcontext
),
4907 &rt_sf
->uc
.tuc_regs
);
4908 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
4909 err
|= __put_user(set
->sig
[i
], &rt_sf
->uc
.tuc_sigmask
.sig
[i
]);
4912 frame
= &rt_sf
->uc
.tuc_mcontext
;
4913 err
|= save_user_regs(env
, frame
, TARGET_NR_rt_sigreturn
);
4915 /* The kernel checks for the presence of a VDSO here. We don't
4916 emulate a vdso, so use a sigreturn system call. */
4917 env
->lr
= (target_ulong
) h2g(frame
->tramp
);
4919 /* Turn off all fp exceptions. */
4922 /* Create a stack frame for the caller of the handler. */
4923 newsp
= rt_sf_addr
- (SIGNAL_FRAMESIZE
+ 16);
4924 err
|= __put_user(env
->gpr
[1], (target_ulong
*)(uintptr_t) newsp
);
4929 /* Set up registers for signal handler. */
4930 env
->gpr
[1] = newsp
;
4931 env
->gpr
[3] = (target_ulong
) signal
;
4932 env
->gpr
[4] = (target_ulong
) h2g(&rt_sf
->info
);
4933 env
->gpr
[5] = (target_ulong
) h2g(&rt_sf
->uc
);
4934 env
->gpr
[6] = (target_ulong
) h2g(rt_sf
);
4935 env
->nip
= (target_ulong
) ka
->_sa_handler
;
4936 /* Signal handlers are entered in big-endian mode. */
4937 env
->msr
&= ~MSR_LE
;
4939 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4943 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
4944 qemu_log("segfaulting from setup_rt_frame\n");
4945 force_sig(TARGET_SIGSEGV
);
4949 long do_sigreturn(CPUPPCState
*env
)
4951 struct target_sigcontext
*sc
= NULL
;
4952 struct target_mcontext
*sr
= NULL
;
4953 target_ulong sr_addr
= 0, sc_addr
;
4955 target_sigset_t set
;
4957 sc_addr
= env
->gpr
[1] + SIGNAL_FRAMESIZE
;
4958 if (!lock_user_struct(VERIFY_READ
, sc
, sc_addr
, 1))
4961 #if defined(TARGET_PPC64)
4962 set
.sig
[0] = sc
->oldmask
+ ((long)(sc
->_unused
[3]) << 32);
4964 if(__get_user(set
.sig
[0], &sc
->oldmask
) ||
4965 __get_user(set
.sig
[1], &sc
->_unused
[3]))
4968 target_to_host_sigset_internal(&blocked
, &set
);
4969 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
4971 if (__get_user(sr_addr
, &sc
->regs
))
4973 if (!lock_user_struct(VERIFY_READ
, sr
, sr_addr
, 1))
4975 if (restore_user_regs(env
, sr
, 1))
4978 unlock_user_struct(sr
, sr_addr
, 1);
4979 unlock_user_struct(sc
, sc_addr
, 1);
4980 return -TARGET_QEMU_ESIGRETURN
;
4983 unlock_user_struct(sr
, sr_addr
, 1);
4984 unlock_user_struct(sc
, sc_addr
, 1);
4985 qemu_log("segfaulting from do_sigreturn\n");
4986 force_sig(TARGET_SIGSEGV
);
4990 /* See arch/powerpc/kernel/signal_32.c. */
4991 static int do_setcontext(struct target_ucontext
*ucp
, CPUPPCState
*env
, int sig
)
4993 struct target_mcontext
*mcp
;
4994 target_ulong mcp_addr
;
4996 target_sigset_t set
;
4998 if (copy_from_user(&set
, h2g(ucp
) + offsetof(struct target_ucontext
, tuc_sigmask
),
5002 #if defined(TARGET_PPC64)
5003 fprintf (stderr
, "do_setcontext: not implemented\n");
5006 if (__get_user(mcp_addr
, &ucp
->tuc_regs
))
5009 if (!lock_user_struct(VERIFY_READ
, mcp
, mcp_addr
, 1))
5012 target_to_host_sigset_internal(&blocked
, &set
);
5013 do_sigprocmask(SIG_SETMASK
, &blocked
, NULL
);
5014 if (restore_user_regs(env
, mcp
, sig
))
5017 unlock_user_struct(mcp
, mcp_addr
, 1);
5021 unlock_user_struct(mcp
, mcp_addr
, 1);
5026 long do_rt_sigreturn(CPUPPCState
*env
)
5028 struct target_rt_sigframe
*rt_sf
= NULL
;
5029 target_ulong rt_sf_addr
;
5031 rt_sf_addr
= env
->gpr
[1] + SIGNAL_FRAMESIZE
+ 16;
5032 if (!lock_user_struct(VERIFY_READ
, rt_sf
, rt_sf_addr
, 1))
5035 if (do_setcontext(&rt_sf
->uc
, env
, 1))
5038 do_sigaltstack(rt_sf_addr
5039 + offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
5042 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
5043 return -TARGET_QEMU_ESIGRETURN
;
5046 unlock_user_struct(rt_sf
, rt_sf_addr
, 1);
5047 qemu_log("segfaulting from do_rt_sigreturn\n");
5048 force_sig(TARGET_SIGSEGV
);
5052 #elif defined(TARGET_M68K)
5054 struct target_sigcontext
{
5061 unsigned short sc_sr
;
5065 struct target_sigframe
5072 abi_ulong extramask
[TARGET_NSIG_WORDS
-1];
5073 struct target_sigcontext sc
;
5076 typedef int target_greg_t
;
5077 #define TARGET_NGREG 18
5078 typedef target_greg_t target_gregset_t
[TARGET_NGREG
];
5080 typedef struct target_fpregset
{
5083 } target_fpregset_t
;
5085 struct target_mcontext
{
5087 target_gregset_t gregs
;
5088 target_fpregset_t fpregs
;
5091 #define TARGET_MCONTEXT_VERSION 2
5093 struct target_ucontext
{
5094 abi_ulong tuc_flags
;
5096 target_stack_t tuc_stack
;
5097 struct target_mcontext tuc_mcontext
;
5098 abi_long tuc_filler
[80];
5099 target_sigset_t tuc_sigmask
;
5102 struct target_rt_sigframe
5109 struct target_siginfo info
;
5110 struct target_ucontext uc
;
5114 setup_sigcontext(struct target_sigcontext
*sc
, CPUM68KState
*env
,
5119 err
|= __put_user(mask
, &sc
->sc_mask
);
5120 err
|= __put_user(env
->aregs
[7], &sc
->sc_usp
);
5121 err
|= __put_user(env
->dregs
[0], &sc
->sc_d0
);
5122 err
|= __put_user(env
->dregs
[1], &sc
->sc_d1
);
5123 err
|= __put_user(env
->aregs
[0], &sc
->sc_a0
);
5124 err
|= __put_user(env
->aregs
[1], &sc
->sc_a1
);
5125 err
|= __put_user(env
->sr
, &sc
->sc_sr
);
5126 err
|= __put_user(env
->pc
, &sc
->sc_pc
);
5132 restore_sigcontext(CPUM68KState
*env
, struct target_sigcontext
*sc
, int *pd0
)
5137 err
|= __get_user(env
->aregs
[7], &sc
->sc_usp
);
5138 err
|= __get_user(env
->dregs
[1], &sc
->sc_d1
);
5139 err
|= __get_user(env
->aregs
[0], &sc
->sc_a0
);
5140 err
|= __get_user(env
->aregs
[1], &sc
->sc_a1
);
5141 err
|= __get_user(env
->pc
, &sc
->sc_pc
);
5142 err
|= __get_user(temp
, &sc
->sc_sr
);
5143 env
->sr
= (env
->sr
& 0xff00) | (temp
& 0xff);
5145 *pd0
= tswapl(sc
->sc_d0
);
5151 * Determine which stack to use..
5153 static inline abi_ulong
5154 get_sigframe(struct target_sigaction
*ka
, CPUM68KState
*regs
,
5159 sp
= regs
->aregs
[7];
5161 /* This is the X/Open sanctioned signal stack switching. */
5162 if ((ka
->sa_flags
& TARGET_SA_ONSTACK
) && (sas_ss_flags (sp
) == 0)) {
5163 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
5166 return ((sp
- frame_size
) & -8UL);
5169 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5170 target_sigset_t
*set
, CPUM68KState
*env
)
5172 struct target_sigframe
*frame
;
5173 abi_ulong frame_addr
;
5174 abi_ulong retcode_addr
;
5179 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
5180 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
5183 err
|= __put_user(sig
, &frame
->sig
);
5185 sc_addr
= frame_addr
+ offsetof(struct target_sigframe
, sc
);
5186 err
|= __put_user(sc_addr
, &frame
->psc
);
5188 err
|= setup_sigcontext(&frame
->sc
, env
, set
->sig
[0]);
5192 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
5193 if (__put_user(set
->sig
[i
], &frame
->extramask
[i
- 1]))
5197 /* Set up to return from userspace. */
5199 retcode_addr
= frame_addr
+ offsetof(struct target_sigframe
, retcode
);
5200 err
|= __put_user(retcode_addr
, &frame
->pretcode
);
5202 /* moveq #,d0; trap #0 */
5204 err
|= __put_user(0x70004e40 + (TARGET_NR_sigreturn
<< 16),
5205 (long *)(frame
->retcode
));
5210 /* Set up to return from userspace */
5212 env
->aregs
[7] = frame_addr
;
5213 env
->pc
= ka
->_sa_handler
;
5215 unlock_user_struct(frame
, frame_addr
, 1);
5219 unlock_user_struct(frame
, frame_addr
, 1);
5220 force_sig(TARGET_SIGSEGV
);
5223 static inline int target_rt_setup_ucontext(struct target_ucontext
*uc
,
5226 target_greg_t
*gregs
= uc
->tuc_mcontext
.gregs
;
5229 err
= __put_user(TARGET_MCONTEXT_VERSION
, &uc
->tuc_mcontext
.version
);
5230 err
|= __put_user(env
->dregs
[0], &gregs
[0]);
5231 err
|= __put_user(env
->dregs
[1], &gregs
[1]);
5232 err
|= __put_user(env
->dregs
[2], &gregs
[2]);
5233 err
|= __put_user(env
->dregs
[3], &gregs
[3]);
5234 err
|= __put_user(env
->dregs
[4], &gregs
[4]);
5235 err
|= __put_user(env
->dregs
[5], &gregs
[5]);
5236 err
|= __put_user(env
->dregs
[6], &gregs
[6]);
5237 err
|= __put_user(env
->dregs
[7], &gregs
[7]);
5238 err
|= __put_user(env
->aregs
[0], &gregs
[8]);
5239 err
|= __put_user(env
->aregs
[1], &gregs
[9]);
5240 err
|= __put_user(env
->aregs
[2], &gregs
[10]);
5241 err
|= __put_user(env
->aregs
[3], &gregs
[11]);
5242 err
|= __put_user(env
->aregs
[4], &gregs
[12]);
5243 err
|= __put_user(env
->aregs
[5], &gregs
[13]);
5244 err
|= __put_user(env
->aregs
[6], &gregs
[14]);
5245 err
|= __put_user(env
->aregs
[7], &gregs
[15]);
5246 err
|= __put_user(env
->pc
, &gregs
[16]);
5247 err
|= __put_user(env
->sr
, &gregs
[17]);
5252 static inline int target_rt_restore_ucontext(CPUM68KState
*env
,
5253 struct target_ucontext
*uc
,
5258 target_greg_t
*gregs
= uc
->tuc_mcontext
.gregs
;
5260 err
= __get_user(temp
, &uc
->tuc_mcontext
.version
);
5261 if (temp
!= TARGET_MCONTEXT_VERSION
)
5264 /* restore passed registers */
5265 err
|= __get_user(env
->dregs
[0], &gregs
[0]);
5266 err
|= __get_user(env
->dregs
[1], &gregs
[1]);
5267 err
|= __get_user(env
->dregs
[2], &gregs
[2]);
5268 err
|= __get_user(env
->dregs
[3], &gregs
[3]);
5269 err
|= __get_user(env
->dregs
[4], &gregs
[4]);
5270 err
|= __get_user(env
->dregs
[5], &gregs
[5]);
5271 err
|= __get_user(env
->dregs
[6], &gregs
[6]);
5272 err
|= __get_user(env
->dregs
[7], &gregs
[7]);
5273 err
|= __get_user(env
->aregs
[0], &gregs
[8]);
5274 err
|= __get_user(env
->aregs
[1], &gregs
[9]);
5275 err
|= __get_user(env
->aregs
[2], &gregs
[10]);
5276 err
|= __get_user(env
->aregs
[3], &gregs
[11]);
5277 err
|= __get_user(env
->aregs
[4], &gregs
[12]);
5278 err
|= __get_user(env
->aregs
[5], &gregs
[13]);
5279 err
|= __get_user(env
->aregs
[6], &gregs
[14]);
5280 err
|= __get_user(env
->aregs
[7], &gregs
[15]);
5281 err
|= __get_user(env
->pc
, &gregs
[16]);
5282 err
|= __get_user(temp
, &gregs
[17]);
5283 env
->sr
= (env
->sr
& 0xff00) | (temp
& 0xff);
5285 *pd0
= env
->dregs
[0];
5292 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5293 target_siginfo_t
*info
,
5294 target_sigset_t
*set
, CPUM68KState
*env
)
5296 struct target_rt_sigframe
*frame
;
5297 abi_ulong frame_addr
;
5298 abi_ulong retcode_addr
;
5299 abi_ulong info_addr
;
5304 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
5305 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0))
5308 err
|= __put_user(sig
, &frame
->sig
);
5310 info_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
5311 err
|= __put_user(info_addr
, &frame
->pinfo
);
5313 uc_addr
= frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
5314 err
|= __put_user(uc_addr
, &frame
->puc
);
5316 err
|= copy_siginfo_to_user(&frame
->info
, info
);
5318 /* Create the ucontext */
5320 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
5321 err
|= __put_user(0, &frame
->uc
.tuc_link
);
5322 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
5323 &frame
->uc
.tuc_stack
.ss_sp
);
5324 err
|= __put_user(sas_ss_flags(env
->aregs
[7]),
5325 &frame
->uc
.tuc_stack
.ss_flags
);
5326 err
|= __put_user(target_sigaltstack_used
.ss_size
,
5327 &frame
->uc
.tuc_stack
.ss_size
);
5328 err
|= target_rt_setup_ucontext(&frame
->uc
, env
);
5333 for(i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
5334 if (__put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]))
5338 /* Set up to return from userspace. */
5340 retcode_addr
= frame_addr
+ offsetof(struct target_sigframe
, retcode
);
5341 err
|= __put_user(retcode_addr
, &frame
->pretcode
);
5343 /* moveq #,d0; notb d0; trap #0 */
5345 err
|= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn
^ 0xff) << 16),
5346 (long *)(frame
->retcode
+ 0));
5347 err
|= __put_user(0x4e40, (short *)(frame
->retcode
+ 4));
5352 /* Set up to return from userspace */
5354 env
->aregs
[7] = frame_addr
;
5355 env
->pc
= ka
->_sa_handler
;
5357 unlock_user_struct(frame
, frame_addr
, 1);
5361 unlock_user_struct(frame
, frame_addr
, 1);
5362 force_sig(TARGET_SIGSEGV
);
5365 long do_sigreturn(CPUM68KState
*env
)
5367 struct target_sigframe
*frame
;
5368 abi_ulong frame_addr
= env
->aregs
[7] - 4;
5369 target_sigset_t target_set
;
5373 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
5376 /* set blocked signals */
5378 if (__get_user(target_set
.sig
[0], &frame
->sc
.sc_mask
))
5381 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
5382 if (__get_user(target_set
.sig
[i
], &frame
->extramask
[i
- 1]))
5386 target_to_host_sigset_internal(&set
, &target_set
);
5387 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5389 /* restore registers */
5391 if (restore_sigcontext(env
, &frame
->sc
, &d0
))
5394 unlock_user_struct(frame
, frame_addr
, 0);
5398 unlock_user_struct(frame
, frame_addr
, 0);
5399 force_sig(TARGET_SIGSEGV
);
5403 long do_rt_sigreturn(CPUM68KState
*env
)
5405 struct target_rt_sigframe
*frame
;
5406 abi_ulong frame_addr
= env
->aregs
[7] - 4;
5407 target_sigset_t target_set
;
5411 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1))
5414 target_to_host_sigset_internal(&set
, &target_set
);
5415 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5417 /* restore registers */
5419 if (target_rt_restore_ucontext(env
, &frame
->uc
, &d0
))
5422 if (do_sigaltstack(frame_addr
+
5423 offsetof(struct target_rt_sigframe
, uc
.tuc_stack
),
5424 0, get_sp_from_cpustate(env
)) == -EFAULT
)
5427 unlock_user_struct(frame
, frame_addr
, 0);
5431 unlock_user_struct(frame
, frame_addr
, 0);
5432 force_sig(TARGET_SIGSEGV
);
5436 #elif defined(TARGET_ALPHA)
5438 struct target_sigcontext
{
5439 abi_long sc_onstack
;
5443 abi_long sc_regs
[32];
5444 abi_long sc_ownedfp
;
5445 abi_long sc_fpregs
[32];
5447 abi_ulong sc_fp_control
;
5448 abi_ulong sc_reserved1
;
5449 abi_ulong sc_reserved2
;
5452 abi_ulong sc_traparg_a0
;
5453 abi_ulong sc_traparg_a1
;
5454 abi_ulong sc_traparg_a2
;
5455 abi_ulong sc_fp_trap_pc
;
5456 abi_ulong sc_fp_trigger_sum
;
5457 abi_ulong sc_fp_trigger_inst
;
5460 struct target_ucontext
{
5461 abi_ulong tuc_flags
;
5463 abi_ulong tuc_osf_sigmask
;
5464 target_stack_t tuc_stack
;
5465 struct target_sigcontext tuc_mcontext
;
5466 target_sigset_t tuc_sigmask
;
5469 struct target_sigframe
{
5470 struct target_sigcontext sc
;
5471 unsigned int retcode
[3];
5474 struct target_rt_sigframe
{
5475 target_siginfo_t info
;
5476 struct target_ucontext uc
;
5477 unsigned int retcode
[3];
5480 #define INSN_MOV_R30_R16 0x47fe0410
5481 #define INSN_LDI_R0 0x201f0000
5482 #define INSN_CALLSYS 0x00000083
5484 static int setup_sigcontext(struct target_sigcontext
*sc
, CPUAlphaState
*env
,
5485 abi_ulong frame_addr
, target_sigset_t
*set
)
5489 err
|= __put_user(on_sig_stack(frame_addr
), &sc
->sc_onstack
);
5490 err
|= __put_user(set
->sig
[0], &sc
->sc_mask
);
5491 err
|= __put_user(env
->pc
, &sc
->sc_pc
);
5492 err
|= __put_user(8, &sc
->sc_ps
);
5494 for (i
= 0; i
< 31; ++i
) {
5495 err
|= __put_user(env
->ir
[i
], &sc
->sc_regs
[i
]);
5497 err
|= __put_user(0, &sc
->sc_regs
[31]);
5499 for (i
= 0; i
< 31; ++i
) {
5500 err
|= __put_user(env
->fir
[i
], &sc
->sc_fpregs
[i
]);
5502 err
|= __put_user(0, &sc
->sc_fpregs
[31]);
5503 err
|= __put_user(cpu_alpha_load_fpcr(env
), &sc
->sc_fpcr
);
5505 err
|= __put_user(0, &sc
->sc_traparg_a0
); /* FIXME */
5506 err
|= __put_user(0, &sc
->sc_traparg_a1
); /* FIXME */
5507 err
|= __put_user(0, &sc
->sc_traparg_a2
); /* FIXME */
5512 static int restore_sigcontext(CPUAlphaState
*env
,
5513 struct target_sigcontext
*sc
)
5518 err
|= __get_user(env
->pc
, &sc
->sc_pc
);
5520 for (i
= 0; i
< 31; ++i
) {
5521 err
|= __get_user(env
->ir
[i
], &sc
->sc_regs
[i
]);
5523 for (i
= 0; i
< 31; ++i
) {
5524 err
|= __get_user(env
->fir
[i
], &sc
->sc_fpregs
[i
]);
5527 err
|= __get_user(fpcr
, &sc
->sc_fpcr
);
5528 cpu_alpha_store_fpcr(env
, fpcr
);
5533 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
5535 unsigned long framesize
)
5537 abi_ulong sp
= env
->ir
[IR_SP
];
5539 /* This is the X/Open sanctioned signal stack switching. */
5540 if ((sa
->sa_flags
& TARGET_SA_ONSTACK
) != 0 && !sas_ss_flags(sp
)) {
5541 sp
= target_sigaltstack_used
.ss_sp
+ target_sigaltstack_used
.ss_size
;
5543 return (sp
- framesize
) & -32;
5546 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5547 target_sigset_t
*set
, CPUAlphaState
*env
)
5549 abi_ulong frame_addr
, r26
;
5550 struct target_sigframe
*frame
;
5553 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
5554 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5558 err
|= setup_sigcontext(&frame
->sc
, env
, frame_addr
, set
);
5560 if (ka
->sa_restorer
) {
5561 r26
= ka
->sa_restorer
;
5563 err
|= __put_user(INSN_MOV_R30_R16
, &frame
->retcode
[0]);
5564 err
|= __put_user(INSN_LDI_R0
+ TARGET_NR_sigreturn
,
5565 &frame
->retcode
[1]);
5566 err
|= __put_user(INSN_CALLSYS
, &frame
->retcode
[2]);
5571 unlock_user_struct(frame
, frame_addr
, 1);
5575 if (sig
== TARGET_SIGSEGV
) {
5576 ka
->_sa_handler
= TARGET_SIG_DFL
;
5578 force_sig(TARGET_SIGSEGV
);
5581 env
->ir
[IR_RA
] = r26
;
5582 env
->ir
[IR_PV
] = env
->pc
= ka
->_sa_handler
;
5583 env
->ir
[IR_A0
] = sig
;
5585 env
->ir
[IR_A2
] = frame_addr
+ offsetof(struct target_sigframe
, sc
);
5586 env
->ir
[IR_SP
] = frame_addr
;
5589 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5590 target_siginfo_t
*info
,
5591 target_sigset_t
*set
, CPUAlphaState
*env
)
5593 abi_ulong frame_addr
, r26
;
5594 struct target_rt_sigframe
*frame
;
5597 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
5598 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
5602 err
|= copy_siginfo_to_user(&frame
->info
, info
);
5604 err
|= __put_user(0, &frame
->uc
.tuc_flags
);
5605 err
|= __put_user(0, &frame
->uc
.tuc_link
);
5606 err
|= __put_user(set
->sig
[0], &frame
->uc
.tuc_osf_sigmask
);
5607 err
|= __put_user(target_sigaltstack_used
.ss_sp
,
5608 &frame
->uc
.tuc_stack
.ss_sp
);
5609 err
|= __put_user(sas_ss_flags(env
->ir
[IR_SP
]),
5610 &frame
->uc
.tuc_stack
.ss_flags
);
5611 err
|= __put_user(target_sigaltstack_used
.ss_size
,
5612 &frame
->uc
.tuc_stack
.ss_size
);
5613 err
|= setup_sigcontext(&frame
->uc
.tuc_mcontext
, env
, frame_addr
, set
);
5614 for (i
= 0; i
< TARGET_NSIG_WORDS
; ++i
) {
5615 err
|= __put_user(set
->sig
[i
], &frame
->uc
.tuc_sigmask
.sig
[i
]);
5618 if (ka
->sa_restorer
) {
5619 r26
= ka
->sa_restorer
;
5621 err
|= __put_user(INSN_MOV_R30_R16
, &frame
->retcode
[0]);
5622 err
|= __put_user(INSN_LDI_R0
+ TARGET_NR_rt_sigreturn
,
5623 &frame
->retcode
[1]);
5624 err
|= __put_user(INSN_CALLSYS
, &frame
->retcode
[2]);
5631 if (sig
== TARGET_SIGSEGV
) {
5632 ka
->_sa_handler
= TARGET_SIG_DFL
;
5634 force_sig(TARGET_SIGSEGV
);
5637 env
->ir
[IR_RA
] = r26
;
5638 env
->ir
[IR_PV
] = env
->pc
= ka
->_sa_handler
;
5639 env
->ir
[IR_A0
] = sig
;
5640 env
->ir
[IR_A1
] = frame_addr
+ offsetof(struct target_rt_sigframe
, info
);
5641 env
->ir
[IR_A2
] = frame_addr
+ offsetof(struct target_rt_sigframe
, uc
);
5642 env
->ir
[IR_SP
] = frame_addr
;
5645 long do_sigreturn(CPUAlphaState
*env
)
5647 struct target_sigcontext
*sc
;
5648 abi_ulong sc_addr
= env
->ir
[IR_A0
];
5649 target_sigset_t target_set
;
5652 if (!lock_user_struct(VERIFY_READ
, sc
, sc_addr
, 1)) {
5656 target_sigemptyset(&target_set
);
5657 if (__get_user(target_set
.sig
[0], &sc
->sc_mask
)) {
5661 target_to_host_sigset_internal(&set
, &target_set
);
5662 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5664 if (restore_sigcontext(env
, sc
)) {
5667 unlock_user_struct(sc
, sc_addr
, 0);
5668 return env
->ir
[IR_V0
];
5671 unlock_user_struct(sc
, sc_addr
, 0);
5672 force_sig(TARGET_SIGSEGV
);
5675 long do_rt_sigreturn(CPUAlphaState
*env
)
5677 abi_ulong frame_addr
= env
->ir
[IR_A0
];
5678 struct target_rt_sigframe
*frame
;
5681 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
5684 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
5685 do_sigprocmask(SIG_SETMASK
, &set
, NULL
);
5687 if (restore_sigcontext(env
, &frame
->uc
.tuc_mcontext
)) {
5690 if (do_sigaltstack(frame_addr
+ offsetof(struct target_rt_sigframe
,
5692 0, env
->ir
[IR_SP
]) == -EFAULT
) {
5696 unlock_user_struct(frame
, frame_addr
, 0);
5697 return env
->ir
[IR_V0
];
5701 unlock_user_struct(frame
, frame_addr
, 0);
5702 force_sig(TARGET_SIGSEGV
);
5707 static void setup_frame(int sig
, struct target_sigaction
*ka
,
5708 target_sigset_t
*set
, CPUArchState
*env
)
5710 fprintf(stderr
, "setup_frame: not implemented\n");
5713 static void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
5714 target_siginfo_t
*info
,
5715 target_sigset_t
*set
, CPUArchState
*env
)
5717 fprintf(stderr
, "setup_rt_frame: not implemented\n");
5720 long do_sigreturn(CPUArchState
*env
)
5722 fprintf(stderr
, "do_sigreturn: not implemented\n");
5723 return -TARGET_ENOSYS
;
5726 long do_rt_sigreturn(CPUArchState
*env
)
5728 fprintf(stderr
, "do_rt_sigreturn: not implemented\n");
5729 return -TARGET_ENOSYS
;
5734 void process_pending_signals(CPUArchState
*cpu_env
)
5736 CPUState
*cpu
= ENV_GET_CPU(cpu_env
);
5739 sigset_t set
, old_set
;
5740 target_sigset_t target_old_set
;
5741 struct emulated_sigtable
*k
;
5742 struct target_sigaction
*sa
;
5744 TaskState
*ts
= cpu
->opaque
;
5746 if (!ts
->signal_pending
)
5749 /* FIXME: This is not threadsafe. */
5751 for(sig
= 1; sig
<= TARGET_NSIG
; sig
++) {
5756 /* if no signal is pending, just return */
5757 ts
->signal_pending
= 0;
5762 fprintf(stderr
, "qemu: process signal %d\n", sig
);
5764 /* dequeue signal */
5770 sig
= gdb_handlesig(cpu
, sig
);
5773 handler
= TARGET_SIG_IGN
;
5775 sa
= &sigact_table
[sig
- 1];
5776 handler
= sa
->_sa_handler
;
5779 if (ts
->sigsegv_blocked
&& sig
== TARGET_SIGSEGV
) {
5780 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5781 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5782 * because it got a real MMU fault), and treat as if default handler.
5784 handler
= TARGET_SIG_DFL
;
5787 if (handler
== TARGET_SIG_DFL
) {
5788 /* default handler : ignore some signal. The other are job control or fatal */
5789 if (sig
== TARGET_SIGTSTP
|| sig
== TARGET_SIGTTIN
|| sig
== TARGET_SIGTTOU
) {
5790 kill(getpid(),SIGSTOP
);
5791 } else if (sig
!= TARGET_SIGCHLD
&&
5792 sig
!= TARGET_SIGURG
&&
5793 sig
!= TARGET_SIGWINCH
&&
5794 sig
!= TARGET_SIGCONT
) {
5797 } else if (handler
== TARGET_SIG_IGN
) {
5799 } else if (handler
== TARGET_SIG_ERR
) {
5802 /* compute the blocked signals during the handler execution */
5803 target_to_host_sigset(&set
, &sa
->sa_mask
);
5804 /* SA_NODEFER indicates that the current signal should not be
5805 blocked during the handler */
5806 if (!(sa
->sa_flags
& TARGET_SA_NODEFER
))
5807 sigaddset(&set
, target_to_host_signal(sig
));
5809 /* block signals in the handler using Linux */
5810 do_sigprocmask(SIG_BLOCK
, &set
, &old_set
);
5811 /* save the previous blocked signal state to restore it at the
5812 end of the signal execution (see do_sigreturn) */
5813 host_to_target_sigset_internal(&target_old_set
, &old_set
);
5815 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5816 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5818 CPUX86State
*env
= cpu_env
;
5819 if (env
->eflags
& VM_MASK
)
5820 save_v86_state(env
);
5823 /* prepare the stack frame of the virtual CPU */
5824 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5825 /* These targets do not have traditional signals. */
5826 setup_rt_frame(sig
, sa
, &q
->info
, &target_old_set
, cpu_env
);
5828 if (sa
->sa_flags
& TARGET_SA_SIGINFO
)
5829 setup_rt_frame(sig
, sa
, &q
->info
, &target_old_set
, cpu_env
);
5831 setup_frame(sig
, sa
, &target_old_set
, cpu_env
);
5833 if (sa
->sa_flags
& TARGET_SA_RESETHAND
)
5834 sa
->_sa_handler
= TARGET_SIG_DFL
;
5837 free_sigqueue(cpu_env
, q
);