cg3: move initialisation from realizefn to initfn
[qemu/ar7.git] / linux-user / signal.c
blob5b8a01f9989c5e3397285b7bb8b9c68a38170e90
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <sys/ucontext.h>
27 #include <sys/resource.h>
29 #include "qemu.h"
30 #include "qemu-common.h"
31 #include "target_signal.h"
33 //#define DEBUG_SIGNAL
35 static struct target_sigaltstack target_sigaltstack_used = {
36 .ss_sp = 0,
37 .ss_size = 0,
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,
44 void *puc);
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,
63 #ifdef SIGSTKFLT
64 [SIGSTKFLT] = TARGET_SIGSTKFLT,
65 #endif
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)
106 return sig;
107 return host_to_target_signal_table[sig];
110 int target_to_host_signal(int sig)
112 if (sig < 0 || sig >= _NSIG)
113 return sig;
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)
124 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)
131 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,
137 const sigset_t *s)
139 int i;
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)
150 target_sigset_t d1;
151 int i;
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)
161 int i;
162 sigemptyset(d);
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)
172 target_sigset_t s1;
173 int i;
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)
183 target_sigset_t d;
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)
191 target_sigset_t d;
192 int i;
194 d.sig[0] = *old_sigset;
195 for(i = 1;i < TARGET_NSIG_WORDS; i++)
196 d.sig[i] = 0;
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)
207 int ret;
208 sigset_t val;
209 sigset_t *temp = NULL;
210 CPUState *cpu = thread_cpu;
211 TaskState *ts = (TaskState *)cpu->opaque;
212 bool segv_was_blocked = ts->sigsegv_blocked;
214 if (set) {
215 bool has_sigsegv = sigismember(set, SIGSEGV);
216 val = *set;
217 temp = &val;
219 sigdelset(temp, SIGSEGV);
221 switch (how) {
222 case SIG_BLOCK:
223 if (has_sigsegv) {
224 ts->sigsegv_blocked = true;
226 break;
227 case SIG_UNBLOCK:
228 if (has_sigsegv) {
229 ts->sigsegv_blocked = false;
231 break;
232 case SIG_SETMASK:
233 ts->sigsegv_blocked = has_sigsegv;
234 break;
235 default:
236 g_assert_not_reached();
240 ret = sigprocmask(how, temp, oldset);
242 if (oldset && segv_was_blocked) {
243 sigaddset(oldset, SIGSEGV);
246 return ret;
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;
256 tinfo->si_errno = 0;
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)
340 switch (sig) {
341 case TARGET_SIGCHLD:
342 case TARGET_SIGURG:
343 case TARGET_SIGWINCH:
344 /* Ignored by default. */
345 return 0;
346 case TARGET_SIGCONT:
347 case TARGET_SIGSTOP:
348 case TARGET_SIGTSTP:
349 case TARGET_SIGTTIN:
350 case TARGET_SIGTTOU:
351 /* Job control signals. */
352 return 0;
353 default:
354 return 1;
358 /* returns 1 if given signal should dump core if not handled */
359 static int core_dump_signal(int sig)
361 switch (sig) {
362 case TARGET_SIGABRT:
363 case TARGET_SIGFPE:
364 case TARGET_SIGILL:
365 case TARGET_SIGQUIT:
366 case TARGET_SIGSEGV:
367 case TARGET_SIGTRAP:
368 case TARGET_SIGBUS:
369 return (1);
370 default:
371 return (0);
375 void signal_init(void)
377 struct sigaction act;
378 struct sigaction oact;
379 int i, j;
380 int host_sig;
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;
425 if (!q)
426 return NULL;
427 ts->first_free = q->next;
428 return q;
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;
437 ts->first_free = q;
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)) {
453 stop_all_tasks();
454 core_dumped =
455 ((*ts->bprm->core_dump)(target_sig, env) == 0);
457 if (core_dumped) {
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);
462 nodump.rlim_cur=0;
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
473 * it to arrive. */
474 sigfillset(&act.sa_mask);
475 act.sa_handler = SIG_DFL;
476 act.sa_flags = 0;
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
484 of act) */
485 sigdelset(&act.sa_mask, host_sig);
486 sigsuspend(&act.sa_mask);
488 /* unreachable */
489 abort();
492 /* queue a signal so that it will be send to the virtual CPU as soon
493 as possible */
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;
500 abi_ulong handler;
501 int queue;
503 #if defined(DEBUG_SIGNAL)
504 fprintf(stderr, "queue_signal: sig=%d\n",
505 sig);
506 #endif
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);
526 return 0;
527 } else
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) {
533 force_sig(sig);
534 } else {
535 return 0; /* indicate ignored */
537 } else if (!queue && handler == TARGET_SIG_IGN) {
538 /* ignore signal */
539 return 0;
540 } else if (!queue && handler == TARGET_SIG_ERR) {
541 force_sig(sig);
542 } else {
543 pq = &k->first;
544 if (sig < TARGET_SIGRTMIN) {
545 /* if non real time signal, we queue exactly one signal */
546 if (!k->pending)
547 q = &k->info;
548 else
549 return 0;
550 } else {
551 if (!k->pending) {
552 /* first signal */
553 q = &k->info;
554 } else {
555 q = alloc_sigqueue(env);
556 if (!q)
557 return -EAGAIN;
558 while (*pq != NULL)
559 pq = &(*pq)->next;
562 *pq = q;
563 q->info = *info;
564 q->next = NULL;
565 k->pending = 1;
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,
573 void *puc)
575 CPUArchState *env = thread_cpu->env_ptr;
576 int sig;
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))
584 return;
587 /* get target signal number */
588 sig = host_to_target_signal(host_signum);
589 if (sig < 1 || sig > TARGET_NSIG)
590 return;
591 #if defined(DEBUG_SIGNAL)
592 fprintf(stderr, "qemu: got signal %d\n", sig);
593 #endif
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)
605 int ret;
606 struct target_sigaltstack oss;
608 /* XXX: test errors */
609 if(uoss_addr)
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);
616 if(uss_addr)
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))
626 goto out;
627 unlock_user_struct(uss, uss_addr, 0);
629 ret = -TARGET_EPERM;
630 if (on_sig_stack(sp))
631 goto out;
633 ret = -TARGET_EINVAL;
634 if (ss.ss_flags != TARGET_SS_DISABLE
635 && ss.ss_flags != TARGET_SS_ONSTACK
636 && ss.ss_flags != 0)
637 goto out;
639 if (ss.ss_flags == TARGET_SS_DISABLE) {
640 ss.ss_size = 0;
641 ss.ss_sp = 0;
642 } else {
643 ret = -TARGET_ENOMEM;
644 if (ss.ss_size < MINSIGSTKSZ)
645 goto out;
648 target_sigaltstack_used.ss_sp = ss.ss_sp;
649 target_sigaltstack_used.ss_size = ss.ss_size;
652 if (uoss_addr) {
653 ret = -TARGET_EFAULT;
654 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
655 goto out;
658 ret = 0;
659 out:
660 return ret;
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;
669 int host_sig;
670 int ret = 0;
672 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
673 return -EINVAL;
674 k = &sigact_table[sig - 1];
675 #if defined(DEBUG_SIGNAL)
676 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
677 sig, act, oact);
678 #endif
679 if (oact) {
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);
684 #endif
685 /* Not swapped. */
686 oact->sa_mask = k->sa_mask;
688 if (act) {
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);
694 #endif
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
707 syscalls */
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;
713 else
714 act1.sa_sigaction = (void *)SIG_DFL;
715 } else {
716 act1.sa_sigaction = host_signal_handler;
718 ret = sigaction(host_sig, &act1, NULL);
721 return ret;
724 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
725 const target_siginfo_t *info)
727 tswap_siginfo(tinfo, info);
728 return 0;
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];
743 uint16_t exponent;
746 struct target_fpxreg {
747 uint16_t significand[4];
748 uint16_t exponent;
749 uint16_t padding[3];
752 struct target_xmmreg {
753 abi_ulong element[4];
756 struct target_fpstate {
757 /* Regular FPU environment */
758 abi_ulong cw;
759 abi_ulong sw;
760 abi_ulong tag;
761 abi_ulong ipoff;
762 abi_ulong cssel;
763 abi_ulong dataoff;
764 abi_ulong datasel;
765 struct target_fpreg _st[8];
766 uint16_t status;
767 uint16_t magic; /* 0xffff = regular FPU data only */
769 /* FXSR FPU environment */
770 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
771 abi_ulong mxcsr;
772 abi_ulong reserved;
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 {
781 uint16_t gs, __gsh;
782 uint16_t fs, __fsh;
783 uint16_t es, __esh;
784 uint16_t ds, __dsh;
785 abi_ulong edi;
786 abi_ulong esi;
787 abi_ulong ebp;
788 abi_ulong esp;
789 abi_ulong ebx;
790 abi_ulong edx;
791 abi_ulong ecx;
792 abi_ulong eax;
793 abi_ulong trapno;
794 abi_ulong err;
795 abi_ulong eip;
796 uint16_t cs, __csh;
797 abi_ulong eflags;
798 abi_ulong esp_at_signal;
799 uint16_t ss, __ssh;
800 abi_ulong fpstate; /* pointer */
801 abi_ulong oldmask;
802 abi_ulong cr2;
805 struct target_ucontext {
806 abi_ulong tuc_flags;
807 abi_ulong tuc_link;
808 target_stack_t tuc_stack;
809 struct target_sigcontext tuc_mcontext;
810 target_sigset_t tuc_sigmask; /* mask last for extensibility */
813 struct sigframe
815 abi_ulong pretcode;
816 int sig;
817 struct target_sigcontext sc;
818 struct target_fpstate fpstate;
819 abi_ulong extramask[TARGET_NSIG_WORDS-1];
820 char retcode[8];
823 struct rt_sigframe
825 abi_ulong pretcode;
826 int sig;
827 abi_ulong pinfo;
828 abi_ulong puc;
829 struct target_siginfo info;
830 struct target_ucontext uc;
831 struct target_fpstate fpstate;
832 char retcode[8];
836 * Set up a signal frame.
839 /* XXX: save x87 state */
840 static int
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));
845 int err = 0;
846 uint16_t magic;
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;
871 magic = 0xffff;
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);
878 return err;
882 * Determine which stack to use..
885 static inline abi_ulong
886 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
888 unsigned long esp;
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. */
899 else
900 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
901 !(ka->sa_flags & TARGET_SA_RESTORER) &&
902 ka->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;
914 int i, err = 0;
916 frame_addr = get_sigframe(ka, env, sizeof(*frame));
918 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
919 goto give_sigsegv;
921 err |= __put_user(current_exec_domain_sig(sig),
922 &frame->sig);
923 if (err)
924 goto give_sigsegv;
926 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
927 frame_addr + offsetof(struct sigframe, fpstate));
928 if (err)
929 goto give_sigsegv;
931 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
932 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
933 goto give_sigsegv;
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);
940 } else {
941 uint16_t val16;
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 */
946 val16 = 0xb858;
947 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
948 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
949 val16 = 0x80cd;
950 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
953 if (err)
954 goto give_sigsegv;
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);
968 return;
970 give_sigsegv:
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;
984 int i, err = 0;
986 frame_addr = get_sigframe(ka, env, sizeof(*frame));
988 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
989 goto give_sigsegv;
991 err |= __put_user(current_exec_domain_sig(sig),
992 &frame->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);
998 if (err)
999 goto give_sigsegv;
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,
1011 env, set->sig[0],
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]))
1015 goto give_sigsegv;
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);
1022 } else {
1023 uint16_t val16;
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));
1029 val16 = 0x80cd;
1030 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
1033 if (err)
1034 goto give_sigsegv;
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);
1048 return;
1050 give_sigsegv:
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 */);
1057 static int
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)))
1089 goto badframe;
1090 cpu_x86_frstor(env, fpstate_addr, 1);
1093 *peax = tswapl(sc->eax);
1094 return err;
1095 badframe:
1096 return 1;
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;
1104 sigset_t set;
1105 int eax, i;
1107 #if defined(DEBUG_SIGNAL)
1108 fprintf(stderr, "do_sigreturn\n");
1109 #endif
1110 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1111 goto badframe;
1112 /* set blocked signals */
1113 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1114 goto badframe;
1115 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1116 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1117 goto badframe;
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))
1125 goto badframe;
1126 unlock_user_struct(frame, frame_addr, 0);
1127 return eax;
1129 badframe:
1130 unlock_user_struct(frame, frame_addr, 0);
1131 force_sig(TARGET_SIGSEGV);
1132 return 0;
1135 long do_rt_sigreturn(CPUX86State *env)
1137 abi_ulong frame_addr;
1138 struct rt_sigframe *frame;
1139 sigset_t set;
1140 int eax;
1142 frame_addr = env->regs[R_ESP] - 4;
1143 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1144 goto badframe;
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))
1149 goto badframe;
1151 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1152 get_sp_from_cpustate(env)) == -EFAULT)
1153 goto badframe;
1155 unlock_user_struct(frame, frame_addr, 0);
1156 return eax;
1158 badframe:
1159 unlock_user_struct(frame, frame_addr, 0);
1160 force_sig(TARGET_SIGSEGV);
1161 return 0;
1164 #elif defined(TARGET_AARCH64)
1166 struct target_sigcontext {
1167 uint64_t fault_address;
1168 /* AArch64 registers */
1169 uint64_t regs[31];
1170 uint64_t sp;
1171 uint64_t pc;
1172 uint64_t pstate;
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;
1179 abi_ulong tuc_link;
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 {
1195 uint32_t magic;
1196 uint32_t size;
1199 #define TARGET_FPSIMD_MAGIC 0x46508001
1201 struct target_fpsimd_context {
1202 struct target_aarch64_ctx head;
1203 uint32_t fpsr;
1204 uint32_t fpcr;
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;
1222 uint64_t fp;
1223 uint64_t lr;
1224 uint32_t tramp[2];
1227 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1228 CPUARMState *env, target_sigset_t *set)
1230 int i;
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(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
1247 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1248 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1251 for (i = 0; i < 32; i++) {
1252 #ifdef TARGET_WORDS_BIGENDIAN
1253 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1254 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1255 #else
1256 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1257 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1258 #endif
1260 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1261 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1262 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1263 __put_user(sizeof(struct target_fpsimd_context),
1264 &aux->fpsimd.head.size);
1266 /* set the "end" magic */
1267 __put_user(0, &aux->end.magic);
1268 __put_user(0, &aux->end.size);
1270 return 0;
1273 static int target_restore_sigframe(CPUARMState *env,
1274 struct target_rt_sigframe *sf)
1276 sigset_t set;
1277 int i;
1278 struct target_aux_context *aux =
1279 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1280 uint32_t magic, size, fpsr, fpcr;
1281 uint64_t pstate;
1283 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1284 do_sigprocmask(SIG_SETMASK, &set, NULL);
1286 for (i = 0; i < 31; i++) {
1287 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1290 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1291 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1292 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1293 pstate_write(env, pstate);
1295 __get_user(magic, &aux->fpsimd.head.magic);
1296 __get_user(size, &aux->fpsimd.head.size);
1298 if (magic != TARGET_FPSIMD_MAGIC
1299 || size != sizeof(struct target_fpsimd_context)) {
1300 return 1;
1303 for (i = 0; i < 32; i++) {
1304 #ifdef TARGET_WORDS_BIGENDIAN
1305 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1306 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1307 #else
1308 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1309 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1310 #endif
1312 __get_user(fpsr, &aux->fpsimd.fpsr);
1313 vfp_set_fpsr(env, fpsr);
1314 __get_user(fpcr, &aux->fpsimd.fpcr);
1315 vfp_set_fpcr(env, fpcr);
1317 return 0;
1320 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1322 abi_ulong sp;
1324 sp = env->xregs[31];
1327 * This is the X/Open sanctioned signal stack switching.
1329 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
1330 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1333 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1335 return sp;
1338 static void target_setup_frame(int usig, struct target_sigaction *ka,
1339 target_siginfo_t *info, target_sigset_t *set,
1340 CPUARMState *env)
1342 struct target_rt_sigframe *frame;
1343 abi_ulong frame_addr, return_addr;
1345 frame_addr = get_sigframe(ka, env);
1346 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1347 goto give_sigsegv;
1350 __put_user(0, &frame->uc.tuc_flags);
1351 __put_user(0, &frame->uc.tuc_link);
1353 __put_user(target_sigaltstack_used.ss_sp,
1354 &frame->uc.tuc_stack.ss_sp);
1355 __put_user(sas_ss_flags(env->xregs[31]),
1356 &frame->uc.tuc_stack.ss_flags);
1357 __put_user(target_sigaltstack_used.ss_size,
1358 &frame->uc.tuc_stack.ss_size);
1359 target_setup_sigframe(frame, env, set);
1360 if (ka->sa_flags & TARGET_SA_RESTORER) {
1361 return_addr = ka->sa_restorer;
1362 } else {
1363 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1364 __put_user(0xd2801168, &frame->tramp[0]);
1365 __put_user(0xd4000001, &frame->tramp[1]);
1366 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1368 env->xregs[0] = usig;
1369 env->xregs[31] = frame_addr;
1370 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1371 env->pc = ka->_sa_handler;
1372 env->xregs[30] = return_addr;
1373 if (info) {
1374 if (copy_siginfo_to_user(&frame->info, info)) {
1375 goto give_sigsegv;
1377 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1378 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1381 unlock_user_struct(frame, frame_addr, 1);
1382 return;
1384 give_sigsegv:
1385 unlock_user_struct(frame, frame_addr, 1);
1386 force_sig(TARGET_SIGSEGV);
1389 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1390 target_siginfo_t *info, target_sigset_t *set,
1391 CPUARMState *env)
1393 target_setup_frame(sig, ka, info, set, env);
1396 static void setup_frame(int sig, struct target_sigaction *ka,
1397 target_sigset_t *set, CPUARMState *env)
1399 target_setup_frame(sig, ka, 0, set, env);
1402 long do_rt_sigreturn(CPUARMState *env)
1404 struct target_rt_sigframe *frame = NULL;
1405 abi_ulong frame_addr = env->xregs[31];
1407 if (frame_addr & 15) {
1408 goto badframe;
1411 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1412 goto badframe;
1415 if (target_restore_sigframe(env, frame)) {
1416 goto badframe;
1419 if (do_sigaltstack(frame_addr +
1420 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1421 0, get_sp_from_cpustate(env)) == -EFAULT) {
1422 goto badframe;
1425 unlock_user_struct(frame, frame_addr, 0);
1426 return env->xregs[0];
1428 badframe:
1429 unlock_user_struct(frame, frame_addr, 0);
1430 force_sig(TARGET_SIGSEGV);
1431 return 0;
1434 long do_sigreturn(CPUARMState *env)
1436 return do_rt_sigreturn(env);
1439 #elif defined(TARGET_ARM)
1441 struct target_sigcontext {
1442 abi_ulong trap_no;
1443 abi_ulong error_code;
1444 abi_ulong oldmask;
1445 abi_ulong arm_r0;
1446 abi_ulong arm_r1;
1447 abi_ulong arm_r2;
1448 abi_ulong arm_r3;
1449 abi_ulong arm_r4;
1450 abi_ulong arm_r5;
1451 abi_ulong arm_r6;
1452 abi_ulong arm_r7;
1453 abi_ulong arm_r8;
1454 abi_ulong arm_r9;
1455 abi_ulong arm_r10;
1456 abi_ulong arm_fp;
1457 abi_ulong arm_ip;
1458 abi_ulong arm_sp;
1459 abi_ulong arm_lr;
1460 abi_ulong arm_pc;
1461 abi_ulong arm_cpsr;
1462 abi_ulong fault_address;
1465 struct target_ucontext_v1 {
1466 abi_ulong tuc_flags;
1467 abi_ulong tuc_link;
1468 target_stack_t tuc_stack;
1469 struct target_sigcontext tuc_mcontext;
1470 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1473 struct target_ucontext_v2 {
1474 abi_ulong tuc_flags;
1475 abi_ulong tuc_link;
1476 target_stack_t tuc_stack;
1477 struct target_sigcontext tuc_mcontext;
1478 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1479 char __unused[128 - sizeof(target_sigset_t)];
1480 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1483 struct target_user_vfp {
1484 uint64_t fpregs[32];
1485 abi_ulong fpscr;
1488 struct target_user_vfp_exc {
1489 abi_ulong fpexc;
1490 abi_ulong fpinst;
1491 abi_ulong fpinst2;
1494 struct target_vfp_sigframe {
1495 abi_ulong magic;
1496 abi_ulong size;
1497 struct target_user_vfp ufp;
1498 struct target_user_vfp_exc ufp_exc;
1499 } __attribute__((__aligned__(8)));
1501 struct target_iwmmxt_sigframe {
1502 abi_ulong magic;
1503 abi_ulong size;
1504 uint64_t regs[16];
1505 /* Note that not all the coprocessor control registers are stored here */
1506 uint32_t wcssf;
1507 uint32_t wcasf;
1508 uint32_t wcgr0;
1509 uint32_t wcgr1;
1510 uint32_t wcgr2;
1511 uint32_t wcgr3;
1512 } __attribute__((__aligned__(8)));
1514 #define TARGET_VFP_MAGIC 0x56465001
1515 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1517 struct sigframe_v1
1519 struct target_sigcontext sc;
1520 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1521 abi_ulong retcode;
1524 struct sigframe_v2
1526 struct target_ucontext_v2 uc;
1527 abi_ulong retcode;
1530 struct rt_sigframe_v1
1532 abi_ulong pinfo;
1533 abi_ulong puc;
1534 struct target_siginfo info;
1535 struct target_ucontext_v1 uc;
1536 abi_ulong retcode;
1539 struct rt_sigframe_v2
1541 struct target_siginfo info;
1542 struct target_ucontext_v2 uc;
1543 abi_ulong retcode;
1546 #define TARGET_CONFIG_CPU_32 1
1549 * For ARM syscalls, we encode the syscall number into the instruction.
1551 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1552 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1555 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1556 * need two 16-bit instructions.
1558 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1559 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1561 static const abi_ulong retcodes[4] = {
1562 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1563 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1567 #define __get_user_error(x,p,e) __get_user(x, p)
1569 static inline int valid_user_regs(CPUARMState *regs)
1571 return 1;
1574 static void
1575 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1576 CPUARMState *env, abi_ulong mask)
1578 __put_user(env->regs[0], &sc->arm_r0);
1579 __put_user(env->regs[1], &sc->arm_r1);
1580 __put_user(env->regs[2], &sc->arm_r2);
1581 __put_user(env->regs[3], &sc->arm_r3);
1582 __put_user(env->regs[4], &sc->arm_r4);
1583 __put_user(env->regs[5], &sc->arm_r5);
1584 __put_user(env->regs[6], &sc->arm_r6);
1585 __put_user(env->regs[7], &sc->arm_r7);
1586 __put_user(env->regs[8], &sc->arm_r8);
1587 __put_user(env->regs[9], &sc->arm_r9);
1588 __put_user(env->regs[10], &sc->arm_r10);
1589 __put_user(env->regs[11], &sc->arm_fp);
1590 __put_user(env->regs[12], &sc->arm_ip);
1591 __put_user(env->regs[13], &sc->arm_sp);
1592 __put_user(env->regs[14], &sc->arm_lr);
1593 __put_user(env->regs[15], &sc->arm_pc);
1594 #ifdef TARGET_CONFIG_CPU_32
1595 __put_user(cpsr_read(env), &sc->arm_cpsr);
1596 #endif
1598 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1599 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1600 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1601 __put_user(mask, &sc->oldmask);
1604 static inline abi_ulong
1605 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1607 unsigned long sp = regs->regs[13];
1610 * This is the X/Open sanctioned signal stack switching.
1612 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1613 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1615 * ATPCS B01 mandates 8-byte alignment
1617 return (sp - framesize) & ~7;
1620 static int
1621 setup_return(CPUARMState *env, struct target_sigaction *ka,
1622 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1624 abi_ulong handler = ka->_sa_handler;
1625 abi_ulong retcode;
1626 int thumb = handler & 1;
1627 uint32_t cpsr = cpsr_read(env);
1629 cpsr &= ~CPSR_IT;
1630 if (thumb) {
1631 cpsr |= CPSR_T;
1632 } else {
1633 cpsr &= ~CPSR_T;
1636 if (ka->sa_flags & TARGET_SA_RESTORER) {
1637 retcode = ka->sa_restorer;
1638 } else {
1639 unsigned int idx = thumb;
1641 if (ka->sa_flags & TARGET_SA_SIGINFO)
1642 idx += 2;
1644 if (__put_user(retcodes[idx], rc))
1645 return 1;
1647 retcode = rc_addr + thumb;
1650 env->regs[0] = usig;
1651 env->regs[13] = frame_addr;
1652 env->regs[14] = retcode;
1653 env->regs[15] = handler & (thumb ? ~1 : ~3);
1654 cpsr_write(env, cpsr, 0xffffffff);
1656 return 0;
1659 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1661 int i;
1662 struct target_vfp_sigframe *vfpframe;
1663 vfpframe = (struct target_vfp_sigframe *)regspace;
1664 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1665 __put_user(sizeof(*vfpframe), &vfpframe->size);
1666 for (i = 0; i < 32; i++) {
1667 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1669 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1670 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1671 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1672 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1673 return (abi_ulong*)(vfpframe+1);
1676 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1677 CPUARMState *env)
1679 int i;
1680 struct target_iwmmxt_sigframe *iwmmxtframe;
1681 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1682 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1683 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1684 for (i = 0; i < 16; i++) {
1685 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1687 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1688 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1689 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1690 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1691 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1692 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1693 return (abi_ulong*)(iwmmxtframe+1);
1696 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1697 target_sigset_t *set, CPUARMState *env)
1699 struct target_sigaltstack stack;
1700 int i;
1701 abi_ulong *regspace;
1703 /* Clear all the bits of the ucontext we don't use. */
1704 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1706 memset(&stack, 0, sizeof(stack));
1707 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1708 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1709 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1710 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1712 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1713 /* Save coprocessor signal frame. */
1714 regspace = uc->tuc_regspace;
1715 if (arm_feature(env, ARM_FEATURE_VFP)) {
1716 regspace = setup_sigframe_v2_vfp(regspace, env);
1718 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1719 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1722 /* Write terminating magic word */
1723 __put_user(0, regspace);
1725 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1726 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1730 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1731 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1732 target_sigset_t *set, CPUARMState *regs)
1734 struct sigframe_v1 *frame;
1735 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1736 int i;
1738 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1739 return;
1741 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1743 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1744 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1745 goto end;
1748 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1749 frame_addr + offsetof(struct sigframe_v1, retcode));
1751 end:
1752 unlock_user_struct(frame, frame_addr, 1);
1755 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1756 target_sigset_t *set, CPUARMState *regs)
1758 struct sigframe_v2 *frame;
1759 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1761 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1762 return;
1764 setup_sigframe_v2(&frame->uc, set, regs);
1766 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1767 frame_addr + offsetof(struct sigframe_v2, retcode));
1769 unlock_user_struct(frame, frame_addr, 1);
1772 static void setup_frame(int usig, struct target_sigaction *ka,
1773 target_sigset_t *set, CPUARMState *regs)
1775 if (get_osversion() >= 0x020612) {
1776 setup_frame_v2(usig, ka, set, regs);
1777 } else {
1778 setup_frame_v1(usig, ka, set, regs);
1782 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1783 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1784 target_siginfo_t *info,
1785 target_sigset_t *set, CPUARMState *env)
1787 struct rt_sigframe_v1 *frame;
1788 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1789 struct target_sigaltstack stack;
1790 int i;
1791 abi_ulong info_addr, uc_addr;
1793 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1794 return /* 1 */;
1796 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1797 __put_user(info_addr, &frame->pinfo);
1798 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1799 __put_user(uc_addr, &frame->puc);
1800 copy_siginfo_to_user(&frame->info, info);
1802 /* Clear all the bits of the ucontext we don't use. */
1803 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1805 memset(&stack, 0, sizeof(stack));
1806 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1807 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1808 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1809 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1811 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1812 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1813 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1814 goto end;
1817 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1818 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1820 env->regs[1] = info_addr;
1821 env->regs[2] = uc_addr;
1823 end:
1824 unlock_user_struct(frame, frame_addr, 1);
1827 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1828 target_siginfo_t *info,
1829 target_sigset_t *set, CPUARMState *env)
1831 struct rt_sigframe_v2 *frame;
1832 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1833 abi_ulong info_addr, uc_addr;
1835 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1836 return /* 1 */;
1838 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1839 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1840 copy_siginfo_to_user(&frame->info, info);
1842 setup_sigframe_v2(&frame->uc, set, env);
1844 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1845 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1847 env->regs[1] = info_addr;
1848 env->regs[2] = uc_addr;
1850 unlock_user_struct(frame, frame_addr, 1);
1853 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1854 target_siginfo_t *info,
1855 target_sigset_t *set, CPUARMState *env)
1857 if (get_osversion() >= 0x020612) {
1858 setup_rt_frame_v2(usig, ka, info, set, env);
1859 } else {
1860 setup_rt_frame_v1(usig, ka, info, set, env);
1864 static int
1865 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1867 int err = 0;
1868 uint32_t cpsr;
1870 __get_user_error(env->regs[0], &sc->arm_r0, err);
1871 __get_user_error(env->regs[1], &sc->arm_r1, err);
1872 __get_user_error(env->regs[2], &sc->arm_r2, err);
1873 __get_user_error(env->regs[3], &sc->arm_r3, err);
1874 __get_user_error(env->regs[4], &sc->arm_r4, err);
1875 __get_user_error(env->regs[5], &sc->arm_r5, err);
1876 __get_user_error(env->regs[6], &sc->arm_r6, err);
1877 __get_user_error(env->regs[7], &sc->arm_r7, err);
1878 __get_user_error(env->regs[8], &sc->arm_r8, err);
1879 __get_user_error(env->regs[9], &sc->arm_r9, err);
1880 __get_user_error(env->regs[10], &sc->arm_r10, err);
1881 __get_user_error(env->regs[11], &sc->arm_fp, err);
1882 __get_user_error(env->regs[12], &sc->arm_ip, err);
1883 __get_user_error(env->regs[13], &sc->arm_sp, err);
1884 __get_user_error(env->regs[14], &sc->arm_lr, err);
1885 __get_user_error(env->regs[15], &sc->arm_pc, err);
1886 #ifdef TARGET_CONFIG_CPU_32
1887 __get_user_error(cpsr, &sc->arm_cpsr, err);
1888 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1889 #endif
1891 err |= !valid_user_regs(env);
1893 return err;
1896 static long do_sigreturn_v1(CPUARMState *env)
1898 abi_ulong frame_addr;
1899 struct sigframe_v1 *frame = NULL;
1900 target_sigset_t set;
1901 sigset_t host_set;
1902 int i;
1905 * Since we stacked the signal on a 64-bit boundary,
1906 * then 'sp' should be word aligned here. If it's
1907 * not, then the user is trying to mess with us.
1909 frame_addr = env->regs[13];
1910 if (frame_addr & 7) {
1911 goto badframe;
1914 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1915 goto badframe;
1917 if (__get_user(set.sig[0], &frame->sc.oldmask))
1918 goto badframe;
1919 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1920 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1921 goto badframe;
1924 target_to_host_sigset_internal(&host_set, &set);
1925 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1927 if (restore_sigcontext(env, &frame->sc))
1928 goto badframe;
1930 #if 0
1931 /* Send SIGTRAP if we're single-stepping */
1932 if (ptrace_cancel_bpt(current))
1933 send_sig(SIGTRAP, current, 1);
1934 #endif
1935 unlock_user_struct(frame, frame_addr, 0);
1936 return env->regs[0];
1938 badframe:
1939 unlock_user_struct(frame, frame_addr, 0);
1940 force_sig(TARGET_SIGSEGV /* , current */);
1941 return 0;
1944 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1946 int i;
1947 abi_ulong magic, sz;
1948 uint32_t fpscr, fpexc;
1949 struct target_vfp_sigframe *vfpframe;
1950 vfpframe = (struct target_vfp_sigframe *)regspace;
1952 __get_user(magic, &vfpframe->magic);
1953 __get_user(sz, &vfpframe->size);
1954 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1955 return 0;
1957 for (i = 0; i < 32; i++) {
1958 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1960 __get_user(fpscr, &vfpframe->ufp.fpscr);
1961 vfp_set_fpscr(env, fpscr);
1962 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1963 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1964 * and the exception flag is cleared
1966 fpexc |= (1 << 30);
1967 fpexc &= ~((1 << 31) | (1 << 28));
1968 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1969 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1970 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1971 return (abi_ulong*)(vfpframe + 1);
1974 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1975 abi_ulong *regspace)
1977 int i;
1978 abi_ulong magic, sz;
1979 struct target_iwmmxt_sigframe *iwmmxtframe;
1980 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1982 __get_user(magic, &iwmmxtframe->magic);
1983 __get_user(sz, &iwmmxtframe->size);
1984 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1985 return 0;
1987 for (i = 0; i < 16; i++) {
1988 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1990 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1991 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1992 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1993 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1994 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1995 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1996 return (abi_ulong*)(iwmmxtframe + 1);
1999 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
2000 struct target_ucontext_v2 *uc)
2002 sigset_t host_set;
2003 abi_ulong *regspace;
2005 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
2006 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2008 if (restore_sigcontext(env, &uc->tuc_mcontext))
2009 return 1;
2011 /* Restore coprocessor signal frame */
2012 regspace = uc->tuc_regspace;
2013 if (arm_feature(env, ARM_FEATURE_VFP)) {
2014 regspace = restore_sigframe_v2_vfp(env, regspace);
2015 if (!regspace) {
2016 return 1;
2019 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2020 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
2021 if (!regspace) {
2022 return 1;
2026 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2027 return 1;
2029 #if 0
2030 /* Send SIGTRAP if we're single-stepping */
2031 if (ptrace_cancel_bpt(current))
2032 send_sig(SIGTRAP, current, 1);
2033 #endif
2035 return 0;
2038 static long do_sigreturn_v2(CPUARMState *env)
2040 abi_ulong frame_addr;
2041 struct sigframe_v2 *frame = NULL;
2044 * Since we stacked the signal on a 64-bit boundary,
2045 * then 'sp' should be word aligned here. If it's
2046 * not, then the user is trying to mess with us.
2048 frame_addr = env->regs[13];
2049 if (frame_addr & 7) {
2050 goto badframe;
2053 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2054 goto badframe;
2056 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2057 goto badframe;
2059 unlock_user_struct(frame, frame_addr, 0);
2060 return env->regs[0];
2062 badframe:
2063 unlock_user_struct(frame, frame_addr, 0);
2064 force_sig(TARGET_SIGSEGV /* , current */);
2065 return 0;
2068 long do_sigreturn(CPUARMState *env)
2070 if (get_osversion() >= 0x020612) {
2071 return do_sigreturn_v2(env);
2072 } else {
2073 return do_sigreturn_v1(env);
2077 static long do_rt_sigreturn_v1(CPUARMState *env)
2079 abi_ulong frame_addr;
2080 struct rt_sigframe_v1 *frame = NULL;
2081 sigset_t host_set;
2084 * Since we stacked the signal on a 64-bit boundary,
2085 * then 'sp' should be word aligned here. If it's
2086 * not, then the user is trying to mess with us.
2088 frame_addr = env->regs[13];
2089 if (frame_addr & 7) {
2090 goto badframe;
2093 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2094 goto badframe;
2096 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2097 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2099 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
2100 goto badframe;
2102 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2103 goto badframe;
2105 #if 0
2106 /* Send SIGTRAP if we're single-stepping */
2107 if (ptrace_cancel_bpt(current))
2108 send_sig(SIGTRAP, current, 1);
2109 #endif
2110 unlock_user_struct(frame, frame_addr, 0);
2111 return env->regs[0];
2113 badframe:
2114 unlock_user_struct(frame, frame_addr, 0);
2115 force_sig(TARGET_SIGSEGV /* , current */);
2116 return 0;
2119 static long do_rt_sigreturn_v2(CPUARMState *env)
2121 abi_ulong frame_addr;
2122 struct rt_sigframe_v2 *frame = NULL;
2125 * Since we stacked the signal on a 64-bit boundary,
2126 * then 'sp' should be word aligned here. If it's
2127 * not, then the user is trying to mess with us.
2129 frame_addr = env->regs[13];
2130 if (frame_addr & 7) {
2131 goto badframe;
2134 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2135 goto badframe;
2137 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2138 goto badframe;
2140 unlock_user_struct(frame, frame_addr, 0);
2141 return env->regs[0];
2143 badframe:
2144 unlock_user_struct(frame, frame_addr, 0);
2145 force_sig(TARGET_SIGSEGV /* , current */);
2146 return 0;
2149 long do_rt_sigreturn(CPUARMState *env)
2151 if (get_osversion() >= 0x020612) {
2152 return do_rt_sigreturn_v2(env);
2153 } else {
2154 return do_rt_sigreturn_v1(env);
2158 #elif defined(TARGET_SPARC)
2160 #define __SUNOS_MAXWIN 31
2162 /* This is what SunOS does, so shall I. */
2163 struct target_sigcontext {
2164 abi_ulong sigc_onstack; /* state to restore */
2166 abi_ulong sigc_mask; /* sigmask to restore */
2167 abi_ulong sigc_sp; /* stack pointer */
2168 abi_ulong sigc_pc; /* program counter */
2169 abi_ulong sigc_npc; /* next program counter */
2170 abi_ulong sigc_psr; /* for condition codes etc */
2171 abi_ulong sigc_g1; /* User uses these two registers */
2172 abi_ulong sigc_o0; /* within the trampoline code. */
2174 /* Now comes information regarding the users window set
2175 * at the time of the signal.
2177 abi_ulong sigc_oswins; /* outstanding windows */
2179 /* stack ptrs for each regwin buf */
2180 char *sigc_spbuf[__SUNOS_MAXWIN];
2182 /* Windows to restore after signal */
2183 struct {
2184 abi_ulong locals[8];
2185 abi_ulong ins[8];
2186 } sigc_wbuf[__SUNOS_MAXWIN];
2188 /* A Sparc stack frame */
2189 struct sparc_stackf {
2190 abi_ulong locals[8];
2191 abi_ulong ins[8];
2192 /* It's simpler to treat fp and callers_pc as elements of ins[]
2193 * since we never need to access them ourselves.
2195 char *structptr;
2196 abi_ulong xargs[6];
2197 abi_ulong xxargs[1];
2200 typedef struct {
2201 struct {
2202 abi_ulong psr;
2203 abi_ulong pc;
2204 abi_ulong npc;
2205 abi_ulong y;
2206 abi_ulong u_regs[16]; /* globals and ins */
2207 } si_regs;
2208 int si_mask;
2209 } __siginfo_t;
2211 typedef struct {
2212 abi_ulong si_float_regs[32];
2213 unsigned long si_fsr;
2214 unsigned long si_fpqdepth;
2215 struct {
2216 unsigned long *insn_addr;
2217 unsigned long insn;
2218 } si_fpqueue [16];
2219 } qemu_siginfo_fpu_t;
2222 struct target_signal_frame {
2223 struct sparc_stackf ss;
2224 __siginfo_t info;
2225 abi_ulong fpu_save;
2226 abi_ulong insns[2] __attribute__ ((aligned (8)));
2227 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2228 abi_ulong extra_size; /* Should be 0 */
2229 qemu_siginfo_fpu_t fpu_state;
2231 struct target_rt_signal_frame {
2232 struct sparc_stackf ss;
2233 siginfo_t info;
2234 abi_ulong regs[20];
2235 sigset_t mask;
2236 abi_ulong fpu_save;
2237 unsigned int insns[2];
2238 stack_t stack;
2239 unsigned int extra_size; /* Should be 0 */
2240 qemu_siginfo_fpu_t fpu_state;
2243 #define UREG_O0 16
2244 #define UREG_O6 22
2245 #define UREG_I0 0
2246 #define UREG_I1 1
2247 #define UREG_I2 2
2248 #define UREG_I3 3
2249 #define UREG_I4 4
2250 #define UREG_I5 5
2251 #define UREG_I6 6
2252 #define UREG_I7 7
2253 #define UREG_L0 8
2254 #define UREG_FP UREG_I6
2255 #define UREG_SP UREG_O6
2257 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2258 CPUSPARCState *env,
2259 unsigned long framesize)
2261 abi_ulong sp;
2263 sp = env->regwptr[UREG_FP];
2265 /* This is the X/Open sanctioned signal stack switching. */
2266 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2267 if (!on_sig_stack(sp)
2268 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2269 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2271 return sp - framesize;
2274 static int
2275 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2277 int err = 0, i;
2279 err |= __put_user(env->psr, &si->si_regs.psr);
2280 err |= __put_user(env->pc, &si->si_regs.pc);
2281 err |= __put_user(env->npc, &si->si_regs.npc);
2282 err |= __put_user(env->y, &si->si_regs.y);
2283 for (i=0; i < 8; i++) {
2284 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2286 for (i=0; i < 8; i++) {
2287 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2289 err |= __put_user(mask, &si->si_mask);
2290 return err;
2293 #if 0
2294 static int
2295 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2296 CPUSPARCState *env, unsigned long mask)
2298 int err = 0;
2300 err |= __put_user(mask, &sc->sigc_mask);
2301 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2302 err |= __put_user(env->pc, &sc->sigc_pc);
2303 err |= __put_user(env->npc, &sc->sigc_npc);
2304 err |= __put_user(env->psr, &sc->sigc_psr);
2305 err |= __put_user(env->gregs[1], &sc->sigc_g1);
2306 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2308 return err;
2310 #endif
2311 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2313 static void setup_frame(int sig, struct target_sigaction *ka,
2314 target_sigset_t *set, CPUSPARCState *env)
2316 abi_ulong sf_addr;
2317 struct target_signal_frame *sf;
2318 int sigframe_size, err, i;
2320 /* 1. Make sure everything is clean */
2321 //synchronize_user_stack();
2323 sigframe_size = NF_ALIGNEDSZ;
2324 sf_addr = get_sigframe(ka, env, sigframe_size);
2326 sf = lock_user(VERIFY_WRITE, sf_addr,
2327 sizeof(struct target_signal_frame), 0);
2328 if (!sf)
2329 goto sigsegv;
2331 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2332 #if 0
2333 if (invalid_frame_pointer(sf, sigframe_size))
2334 goto sigill_and_return;
2335 #endif
2336 /* 2. Save the current process state */
2337 err = setup___siginfo(&sf->info, env, set->sig[0]);
2338 err |= __put_user(0, &sf->extra_size);
2340 //err |= save_fpu_state(regs, &sf->fpu_state);
2341 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
2343 err |= __put_user(set->sig[0], &sf->info.si_mask);
2344 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2345 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
2348 for (i = 0; i < 8; i++) {
2349 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2351 for (i = 0; i < 8; i++) {
2352 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2354 if (err)
2355 goto sigsegv;
2357 /* 3. signal handler back-trampoline and parameters */
2358 env->regwptr[UREG_FP] = sf_addr;
2359 env->regwptr[UREG_I0] = sig;
2360 env->regwptr[UREG_I1] = sf_addr +
2361 offsetof(struct target_signal_frame, info);
2362 env->regwptr[UREG_I2] = sf_addr +
2363 offsetof(struct target_signal_frame, info);
2365 /* 4. signal handler */
2366 env->pc = ka->_sa_handler;
2367 env->npc = (env->pc + 4);
2368 /* 5. return to kernel instructions */
2369 if (ka->sa_restorer)
2370 env->regwptr[UREG_I7] = ka->sa_restorer;
2371 else {
2372 uint32_t val32;
2374 env->regwptr[UREG_I7] = sf_addr +
2375 offsetof(struct target_signal_frame, insns) - 2 * 4;
2377 /* mov __NR_sigreturn, %g1 */
2378 val32 = 0x821020d8;
2379 err |= __put_user(val32, &sf->insns[0]);
2381 /* t 0x10 */
2382 val32 = 0x91d02010;
2383 err |= __put_user(val32, &sf->insns[1]);
2384 if (err)
2385 goto sigsegv;
2387 /* Flush instruction space. */
2388 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2389 // tb_flush(env);
2391 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2392 return;
2393 #if 0
2394 sigill_and_return:
2395 force_sig(TARGET_SIGILL);
2396 #endif
2397 sigsegv:
2398 //fprintf(stderr, "force_sig\n");
2399 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2400 force_sig(TARGET_SIGSEGV);
2402 static inline int
2403 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
2405 int err;
2406 #if 0
2407 #ifdef CONFIG_SMP
2408 if (current->flags & PF_USEDFPU)
2409 regs->psr &= ~PSR_EF;
2410 #else
2411 if (current == last_task_used_math) {
2412 last_task_used_math = 0;
2413 regs->psr &= ~PSR_EF;
2415 #endif
2416 current->used_math = 1;
2417 current->flags &= ~PF_USEDFPU;
2418 #endif
2419 #if 0
2420 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2421 return -EFAULT;
2422 #endif
2424 /* XXX: incorrect */
2425 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2426 (sizeof(abi_ulong) * 32));
2427 err |= __get_user(env->fsr, &fpu->si_fsr);
2428 #if 0
2429 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2430 if (current->thread.fpqdepth != 0)
2431 err |= __copy_from_user(&current->thread.fpqueue[0],
2432 &fpu->si_fpqueue[0],
2433 ((sizeof(unsigned long) +
2434 (sizeof(unsigned long *)))*16));
2435 #endif
2436 return err;
2440 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2441 target_siginfo_t *info,
2442 target_sigset_t *set, CPUSPARCState *env)
2444 fprintf(stderr, "setup_rt_frame: not implemented\n");
2447 long do_sigreturn(CPUSPARCState *env)
2449 abi_ulong sf_addr;
2450 struct target_signal_frame *sf;
2451 uint32_t up_psr, pc, npc;
2452 target_sigset_t set;
2453 sigset_t host_set;
2454 int err, i;
2456 sf_addr = env->regwptr[UREG_FP];
2457 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2458 goto segv_and_exit;
2459 #if 0
2460 fprintf(stderr, "sigreturn\n");
2461 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2462 #endif
2463 //cpu_dump_state(env, stderr, fprintf, 0);
2465 /* 1. Make sure we are not getting garbage from the user */
2467 if (sf_addr & 3)
2468 goto segv_and_exit;
2470 err = __get_user(pc, &sf->info.si_regs.pc);
2471 err |= __get_user(npc, &sf->info.si_regs.npc);
2473 if ((pc | npc) & 3)
2474 goto segv_and_exit;
2476 /* 2. Restore the state */
2477 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2479 /* User can only change condition codes and FPU enabling in %psr. */
2480 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2481 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2483 env->pc = pc;
2484 env->npc = npc;
2485 err |= __get_user(env->y, &sf->info.si_regs.y);
2486 for (i=0; i < 8; i++) {
2487 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2489 for (i=0; i < 8; i++) {
2490 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2493 /* FIXME: implement FPU save/restore:
2494 * __get_user(fpu_save, &sf->fpu_save);
2495 * if (fpu_save)
2496 * err |= restore_fpu_state(env, fpu_save);
2499 /* This is pretty much atomic, no amount locking would prevent
2500 * the races which exist anyways.
2502 err |= __get_user(set.sig[0], &sf->info.si_mask);
2503 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2504 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2507 target_to_host_sigset_internal(&host_set, &set);
2508 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2510 if (err)
2511 goto segv_and_exit;
2512 unlock_user_struct(sf, sf_addr, 0);
2513 return env->regwptr[0];
2515 segv_and_exit:
2516 unlock_user_struct(sf, sf_addr, 0);
2517 force_sig(TARGET_SIGSEGV);
2520 long do_rt_sigreturn(CPUSPARCState *env)
2522 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2523 return -TARGET_ENOSYS;
2526 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2527 #define MC_TSTATE 0
2528 #define MC_PC 1
2529 #define MC_NPC 2
2530 #define MC_Y 3
2531 #define MC_G1 4
2532 #define MC_G2 5
2533 #define MC_G3 6
2534 #define MC_G4 7
2535 #define MC_G5 8
2536 #define MC_G6 9
2537 #define MC_G7 10
2538 #define MC_O0 11
2539 #define MC_O1 12
2540 #define MC_O2 13
2541 #define MC_O3 14
2542 #define MC_O4 15
2543 #define MC_O5 16
2544 #define MC_O6 17
2545 #define MC_O7 18
2546 #define MC_NGREG 19
2548 typedef abi_ulong target_mc_greg_t;
2549 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2551 struct target_mc_fq {
2552 abi_ulong *mcfq_addr;
2553 uint32_t mcfq_insn;
2556 struct target_mc_fpu {
2557 union {
2558 uint32_t sregs[32];
2559 uint64_t dregs[32];
2560 //uint128_t qregs[16];
2561 } mcfpu_fregs;
2562 abi_ulong mcfpu_fsr;
2563 abi_ulong mcfpu_fprs;
2564 abi_ulong mcfpu_gsr;
2565 struct target_mc_fq *mcfpu_fq;
2566 unsigned char mcfpu_qcnt;
2567 unsigned char mcfpu_qentsz;
2568 unsigned char mcfpu_enab;
2570 typedef struct target_mc_fpu target_mc_fpu_t;
2572 typedef struct {
2573 target_mc_gregset_t mc_gregs;
2574 target_mc_greg_t mc_fp;
2575 target_mc_greg_t mc_i7;
2576 target_mc_fpu_t mc_fpregs;
2577 } target_mcontext_t;
2579 struct target_ucontext {
2580 struct target_ucontext *tuc_link;
2581 abi_ulong tuc_flags;
2582 target_sigset_t tuc_sigmask;
2583 target_mcontext_t tuc_mcontext;
2586 /* A V9 register window */
2587 struct target_reg_window {
2588 abi_ulong locals[8];
2589 abi_ulong ins[8];
2592 #define TARGET_STACK_BIAS 2047
2594 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2595 void sparc64_set_context(CPUSPARCState *env)
2597 abi_ulong ucp_addr;
2598 struct target_ucontext *ucp;
2599 target_mc_gregset_t *grp;
2600 abi_ulong pc, npc, tstate;
2601 abi_ulong fp, i7, w_addr;
2602 int err;
2603 unsigned int i;
2605 ucp_addr = env->regwptr[UREG_I0];
2606 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2607 goto do_sigsegv;
2608 grp = &ucp->tuc_mcontext.mc_gregs;
2609 err = __get_user(pc, &((*grp)[MC_PC]));
2610 err |= __get_user(npc, &((*grp)[MC_NPC]));
2611 if (err || ((pc | npc) & 3))
2612 goto do_sigsegv;
2613 if (env->regwptr[UREG_I1]) {
2614 target_sigset_t target_set;
2615 sigset_t set;
2617 if (TARGET_NSIG_WORDS == 1) {
2618 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2619 goto do_sigsegv;
2620 } else {
2621 abi_ulong *src, *dst;
2622 src = ucp->tuc_sigmask.sig;
2623 dst = target_set.sig;
2624 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2625 err |= __get_user(*dst, src);
2627 if (err)
2628 goto do_sigsegv;
2630 target_to_host_sigset_internal(&set, &target_set);
2631 do_sigprocmask(SIG_SETMASK, &set, NULL);
2633 env->pc = pc;
2634 env->npc = npc;
2635 err |= __get_user(env->y, &((*grp)[MC_Y]));
2636 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2637 env->asi = (tstate >> 24) & 0xff;
2638 cpu_put_ccr(env, tstate >> 32);
2639 cpu_put_cwp64(env, tstate & 0x1f);
2640 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2641 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2642 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2643 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2644 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2645 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2646 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2647 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2648 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2649 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2650 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2651 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2652 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2653 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2654 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2656 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2657 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2659 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2660 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2661 abi_ulong) != 0)
2662 goto do_sigsegv;
2663 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2664 abi_ulong) != 0)
2665 goto do_sigsegv;
2666 /* FIXME this does not match how the kernel handles the FPU in
2667 * its sparc64_set_context implementation. In particular the FPU
2668 * is only restored if fenab is non-zero in:
2669 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2671 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2673 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2674 for (i = 0; i < 64; i++, src++) {
2675 if (i & 1) {
2676 err |= __get_user(env->fpr[i/2].l.lower, src);
2677 } else {
2678 err |= __get_user(env->fpr[i/2].l.upper, src);
2682 err |= __get_user(env->fsr,
2683 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2684 err |= __get_user(env->gsr,
2685 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2686 if (err)
2687 goto do_sigsegv;
2688 unlock_user_struct(ucp, ucp_addr, 0);
2689 return;
2690 do_sigsegv:
2691 unlock_user_struct(ucp, ucp_addr, 0);
2692 force_sig(TARGET_SIGSEGV);
2695 void sparc64_get_context(CPUSPARCState *env)
2697 abi_ulong ucp_addr;
2698 struct target_ucontext *ucp;
2699 target_mc_gregset_t *grp;
2700 target_mcontext_t *mcp;
2701 abi_ulong fp, i7, w_addr;
2702 int err;
2703 unsigned int i;
2704 target_sigset_t target_set;
2705 sigset_t set;
2707 ucp_addr = env->regwptr[UREG_I0];
2708 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2709 goto do_sigsegv;
2711 mcp = &ucp->tuc_mcontext;
2712 grp = &mcp->mc_gregs;
2714 /* Skip over the trap instruction, first. */
2715 env->pc = env->npc;
2716 env->npc += 4;
2718 err = 0;
2720 do_sigprocmask(0, NULL, &set);
2721 host_to_target_sigset_internal(&target_set, &set);
2722 if (TARGET_NSIG_WORDS == 1) {
2723 err |= __put_user(target_set.sig[0],
2724 (abi_ulong *)&ucp->tuc_sigmask);
2725 } else {
2726 abi_ulong *src, *dst;
2727 src = target_set.sig;
2728 dst = ucp->tuc_sigmask.sig;
2729 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2730 err |= __put_user(*src, dst);
2732 if (err)
2733 goto do_sigsegv;
2736 /* XXX: tstate must be saved properly */
2737 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2738 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2739 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2740 err |= __put_user(env->y, &((*grp)[MC_Y]));
2741 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2742 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2743 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2744 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2745 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2746 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2747 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2748 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2749 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2750 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2751 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2752 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2753 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2754 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2755 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2757 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2758 fp = i7 = 0;
2759 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2760 abi_ulong) != 0)
2761 goto do_sigsegv;
2762 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2763 abi_ulong) != 0)
2764 goto do_sigsegv;
2765 err |= __put_user(fp, &(mcp->mc_fp));
2766 err |= __put_user(i7, &(mcp->mc_i7));
2769 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2770 for (i = 0; i < 64; i++, dst++) {
2771 if (i & 1) {
2772 err |= __put_user(env->fpr[i/2].l.lower, dst);
2773 } else {
2774 err |= __put_user(env->fpr[i/2].l.upper, dst);
2778 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2779 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2780 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2782 if (err)
2783 goto do_sigsegv;
2784 unlock_user_struct(ucp, ucp_addr, 1);
2785 return;
2786 do_sigsegv:
2787 unlock_user_struct(ucp, ucp_addr, 1);
2788 force_sig(TARGET_SIGSEGV);
2790 #endif
2791 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2793 # if defined(TARGET_ABI_MIPSO32)
2794 struct target_sigcontext {
2795 uint32_t sc_regmask; /* Unused */
2796 uint32_t sc_status;
2797 uint64_t sc_pc;
2798 uint64_t sc_regs[32];
2799 uint64_t sc_fpregs[32];
2800 uint32_t sc_ownedfp; /* Unused */
2801 uint32_t sc_fpc_csr;
2802 uint32_t sc_fpc_eir; /* Unused */
2803 uint32_t sc_used_math;
2804 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2805 uint32_t pad0;
2806 uint64_t sc_mdhi;
2807 uint64_t sc_mdlo;
2808 target_ulong sc_hi1; /* Was sc_cause */
2809 target_ulong sc_lo1; /* Was sc_badvaddr */
2810 target_ulong sc_hi2; /* Was sc_sigset[4] */
2811 target_ulong sc_lo2;
2812 target_ulong sc_hi3;
2813 target_ulong sc_lo3;
2815 # else /* N32 || N64 */
2816 struct target_sigcontext {
2817 uint64_t sc_regs[32];
2818 uint64_t sc_fpregs[32];
2819 uint64_t sc_mdhi;
2820 uint64_t sc_hi1;
2821 uint64_t sc_hi2;
2822 uint64_t sc_hi3;
2823 uint64_t sc_mdlo;
2824 uint64_t sc_lo1;
2825 uint64_t sc_lo2;
2826 uint64_t sc_lo3;
2827 uint64_t sc_pc;
2828 uint32_t sc_fpc_csr;
2829 uint32_t sc_used_math;
2830 uint32_t sc_dsp;
2831 uint32_t sc_reserved;
2833 # endif /* O32 */
2835 struct sigframe {
2836 uint32_t sf_ass[4]; /* argument save space for o32 */
2837 uint32_t sf_code[2]; /* signal trampoline */
2838 struct target_sigcontext sf_sc;
2839 target_sigset_t sf_mask;
2842 struct target_ucontext {
2843 target_ulong tuc_flags;
2844 target_ulong tuc_link;
2845 target_stack_t tuc_stack;
2846 target_ulong pad0;
2847 struct target_sigcontext tuc_mcontext;
2848 target_sigset_t tuc_sigmask;
2851 struct target_rt_sigframe {
2852 uint32_t rs_ass[4]; /* argument save space for o32 */
2853 uint32_t rs_code[2]; /* signal trampoline */
2854 struct target_siginfo rs_info;
2855 struct target_ucontext rs_uc;
2858 /* Install trampoline to jump back from signal handler */
2859 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2861 int err = 0;
2864 * Set up the return code ...
2866 * li v0, __NR__foo_sigreturn
2867 * syscall
2870 err |= __put_user(0x24020000 + syscall, tramp + 0);
2871 err |= __put_user(0x0000000c , tramp + 1);
2872 return err;
2875 static inline int
2876 setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2878 int err = 0;
2879 int i;
2881 err |= __put_user(exception_resume_pc(regs), &sc->sc_pc);
2882 regs->hflags &= ~MIPS_HFLAG_BMASK;
2884 __put_user(0, &sc->sc_regs[0]);
2885 for (i = 1; i < 32; ++i) {
2886 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2889 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2890 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2892 /* Rather than checking for dsp existence, always copy. The storage
2893 would just be garbage otherwise. */
2894 err |= __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2895 err |= __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2896 err |= __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2897 err |= __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2898 err |= __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2899 err |= __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2901 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2902 err |= __put_user(dsp, &sc->sc_dsp);
2905 err |= __put_user(1, &sc->sc_used_math);
2907 for (i = 0; i < 32; ++i) {
2908 err |= __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2911 return err;
2914 static inline int
2915 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2917 int err = 0;
2918 int i;
2920 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2922 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2923 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2925 for (i = 1; i < 32; ++i) {
2926 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2929 err |= __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2930 err |= __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2931 err |= __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2932 err |= __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2933 err |= __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2934 err |= __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2936 uint32_t dsp;
2937 err |= __get_user(dsp, &sc->sc_dsp);
2938 cpu_wrdsp(dsp, 0x3ff, regs);
2941 for (i = 0; i < 32; ++i) {
2942 err |= __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2945 return err;
2949 * Determine which stack to use..
2951 static inline abi_ulong
2952 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2954 unsigned long sp;
2956 /* Default to using normal stack */
2957 sp = regs->active_tc.gpr[29];
2960 * FPU emulator may have its own trampoline active just
2961 * above the user stack, 16-bytes before the next lowest
2962 * 16 byte boundary. Try to avoid trashing it.
2964 sp -= 32;
2966 /* This is the X/Open sanctioned signal stack switching. */
2967 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2968 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2971 return (sp - frame_size) & ~7;
2974 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2976 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2977 env->hflags &= ~MIPS_HFLAG_M16;
2978 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2979 env->active_tc.PC &= ~(target_ulong) 1;
2983 # if defined(TARGET_ABI_MIPSO32)
2984 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2985 static void setup_frame(int sig, struct target_sigaction * ka,
2986 target_sigset_t *set, CPUMIPSState *regs)
2988 struct sigframe *frame;
2989 abi_ulong frame_addr;
2990 int i;
2992 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2993 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2994 goto give_sigsegv;
2996 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2998 if(setup_sigcontext(regs, &frame->sf_sc))
2999 goto give_sigsegv;
3001 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3002 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
3003 goto give_sigsegv;
3007 * Arguments to signal handler:
3009 * a0 = signal number
3010 * a1 = 0 (should be cause)
3011 * a2 = pointer to struct sigcontext
3013 * $25 and PC point to the signal handler, $29 points to the
3014 * struct sigframe.
3016 regs->active_tc.gpr[ 4] = sig;
3017 regs->active_tc.gpr[ 5] = 0;
3018 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
3019 regs->active_tc.gpr[29] = frame_addr;
3020 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
3021 /* The original kernel code sets CP0_EPC to the handler
3022 * since it returns to userland using eret
3023 * we cannot do this here, and we must set PC directly */
3024 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
3025 mips_set_hflags_isa_mode_from_pc(regs);
3026 unlock_user_struct(frame, frame_addr, 1);
3027 return;
3029 give_sigsegv:
3030 unlock_user_struct(frame, frame_addr, 1);
3031 force_sig(TARGET_SIGSEGV/*, current*/);
3034 long do_sigreturn(CPUMIPSState *regs)
3036 struct sigframe *frame;
3037 abi_ulong frame_addr;
3038 sigset_t blocked;
3039 target_sigset_t target_set;
3040 int i;
3042 #if defined(DEBUG_SIGNAL)
3043 fprintf(stderr, "do_sigreturn\n");
3044 #endif
3045 frame_addr = regs->active_tc.gpr[29];
3046 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3047 goto badframe;
3049 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3050 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
3051 goto badframe;
3054 target_to_host_sigset_internal(&blocked, &target_set);
3055 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3057 if (restore_sigcontext(regs, &frame->sf_sc))
3058 goto badframe;
3060 #if 0
3062 * Don't let your children do this ...
3064 __asm__ __volatile__(
3065 "move\t$29, %0\n\t"
3066 "j\tsyscall_exit"
3067 :/* no outputs */
3068 :"r" (&regs));
3069 /* Unreached */
3070 #endif
3072 regs->active_tc.PC = regs->CP0_EPC;
3073 mips_set_hflags_isa_mode_from_pc(regs);
3074 /* I am not sure this is right, but it seems to work
3075 * maybe a problem with nested signals ? */
3076 regs->CP0_EPC = 0;
3077 return -TARGET_QEMU_ESIGRETURN;
3079 badframe:
3080 force_sig(TARGET_SIGSEGV/*, current*/);
3081 return 0;
3083 # endif /* O32 */
3085 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3086 target_siginfo_t *info,
3087 target_sigset_t *set, CPUMIPSState *env)
3089 struct target_rt_sigframe *frame;
3090 abi_ulong frame_addr;
3091 int i;
3093 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3094 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3095 goto give_sigsegv;
3097 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3099 copy_siginfo_to_user(&frame->rs_info, info);
3101 __put_user(0, &frame->rs_uc.tuc_flags);
3102 __put_user(0, &frame->rs_uc.tuc_link);
3103 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3104 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3105 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3106 &frame->rs_uc.tuc_stack.ss_flags);
3108 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3110 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3111 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3115 * Arguments to signal handler:
3117 * a0 = signal number
3118 * a1 = pointer to siginfo_t
3119 * a2 = pointer to struct ucontext
3121 * $25 and PC point to the signal handler, $29 points to the
3122 * struct sigframe.
3124 env->active_tc.gpr[ 4] = sig;
3125 env->active_tc.gpr[ 5] = frame_addr
3126 + offsetof(struct target_rt_sigframe, rs_info);
3127 env->active_tc.gpr[ 6] = frame_addr
3128 + offsetof(struct target_rt_sigframe, rs_uc);
3129 env->active_tc.gpr[29] = frame_addr;
3130 env->active_tc.gpr[31] = frame_addr
3131 + offsetof(struct target_rt_sigframe, rs_code);
3132 /* The original kernel code sets CP0_EPC to the handler
3133 * since it returns to userland using eret
3134 * we cannot do this here, and we must set PC directly */
3135 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3136 mips_set_hflags_isa_mode_from_pc(env);
3137 unlock_user_struct(frame, frame_addr, 1);
3138 return;
3140 give_sigsegv:
3141 unlock_user_struct(frame, frame_addr, 1);
3142 force_sig(TARGET_SIGSEGV/*, current*/);
3145 long do_rt_sigreturn(CPUMIPSState *env)
3147 struct target_rt_sigframe *frame;
3148 abi_ulong frame_addr;
3149 sigset_t blocked;
3151 #if defined(DEBUG_SIGNAL)
3152 fprintf(stderr, "do_rt_sigreturn\n");
3153 #endif
3154 frame_addr = env->active_tc.gpr[29];
3155 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3156 goto badframe;
3158 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3159 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3161 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
3162 goto badframe;
3164 if (do_sigaltstack(frame_addr +
3165 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3166 0, get_sp_from_cpustate(env)) == -EFAULT)
3167 goto badframe;
3169 env->active_tc.PC = env->CP0_EPC;
3170 mips_set_hflags_isa_mode_from_pc(env);
3171 /* I am not sure this is right, but it seems to work
3172 * maybe a problem with nested signals ? */
3173 env->CP0_EPC = 0;
3174 return -TARGET_QEMU_ESIGRETURN;
3176 badframe:
3177 force_sig(TARGET_SIGSEGV/*, current*/);
3178 return 0;
3181 #elif defined(TARGET_SH4)
3184 * code and data structures from linux kernel:
3185 * include/asm-sh/sigcontext.h
3186 * arch/sh/kernel/signal.c
3189 struct target_sigcontext {
3190 target_ulong oldmask;
3192 /* CPU registers */
3193 target_ulong sc_gregs[16];
3194 target_ulong sc_pc;
3195 target_ulong sc_pr;
3196 target_ulong sc_sr;
3197 target_ulong sc_gbr;
3198 target_ulong sc_mach;
3199 target_ulong sc_macl;
3201 /* FPU registers */
3202 target_ulong sc_fpregs[16];
3203 target_ulong sc_xfpregs[16];
3204 unsigned int sc_fpscr;
3205 unsigned int sc_fpul;
3206 unsigned int sc_ownedfp;
3209 struct target_sigframe
3211 struct target_sigcontext sc;
3212 target_ulong extramask[TARGET_NSIG_WORDS-1];
3213 uint16_t retcode[3];
3217 struct target_ucontext {
3218 target_ulong tuc_flags;
3219 struct target_ucontext *tuc_link;
3220 target_stack_t tuc_stack;
3221 struct target_sigcontext tuc_mcontext;
3222 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3225 struct target_rt_sigframe
3227 struct target_siginfo info;
3228 struct target_ucontext uc;
3229 uint16_t retcode[3];
3233 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3234 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3236 static abi_ulong get_sigframe(struct target_sigaction *ka,
3237 unsigned long sp, size_t frame_size)
3239 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3240 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3243 return (sp - frame_size) & -8ul;
3246 static int setup_sigcontext(struct target_sigcontext *sc,
3247 CPUSH4State *regs, unsigned long mask)
3249 int err = 0;
3250 int i;
3252 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
3253 COPY(gregs[0]); COPY(gregs[1]);
3254 COPY(gregs[2]); COPY(gregs[3]);
3255 COPY(gregs[4]); COPY(gregs[5]);
3256 COPY(gregs[6]); COPY(gregs[7]);
3257 COPY(gregs[8]); COPY(gregs[9]);
3258 COPY(gregs[10]); COPY(gregs[11]);
3259 COPY(gregs[12]); COPY(gregs[13]);
3260 COPY(gregs[14]); COPY(gregs[15]);
3261 COPY(gbr); COPY(mach);
3262 COPY(macl); COPY(pr);
3263 COPY(sr); COPY(pc);
3264 #undef COPY
3266 for (i=0; i<16; i++) {
3267 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3269 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
3270 err |= __put_user(regs->fpul, &sc->sc_fpul);
3272 /* non-iBCS2 extensions.. */
3273 err |= __put_user(mask, &sc->oldmask);
3275 return err;
3278 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3279 target_ulong *r0_p)
3281 unsigned int err = 0;
3282 int i;
3284 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3285 COPY(gregs[1]);
3286 COPY(gregs[2]); COPY(gregs[3]);
3287 COPY(gregs[4]); COPY(gregs[5]);
3288 COPY(gregs[6]); COPY(gregs[7]);
3289 COPY(gregs[8]); COPY(gregs[9]);
3290 COPY(gregs[10]); COPY(gregs[11]);
3291 COPY(gregs[12]); COPY(gregs[13]);
3292 COPY(gregs[14]); COPY(gregs[15]);
3293 COPY(gbr); COPY(mach);
3294 COPY(macl); COPY(pr);
3295 COPY(sr); COPY(pc);
3296 #undef COPY
3298 for (i=0; i<16; i++) {
3299 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3301 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3302 err |= __get_user(regs->fpul, &sc->sc_fpul);
3304 regs->tra = -1; /* disable syscall checks */
3305 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3306 return err;
3309 static void setup_frame(int sig, struct target_sigaction *ka,
3310 target_sigset_t *set, CPUSH4State *regs)
3312 struct target_sigframe *frame;
3313 abi_ulong frame_addr;
3314 int i;
3315 int err = 0;
3316 int signal;
3318 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3319 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3320 goto give_sigsegv;
3322 signal = current_exec_domain_sig(sig);
3324 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3326 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3327 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3330 /* Set up to return from userspace. If provided, use a stub
3331 already in userspace. */
3332 if (ka->sa_flags & TARGET_SA_RESTORER) {
3333 regs->pr = (unsigned long) ka->sa_restorer;
3334 } else {
3335 /* Generate return code (system call to sigreturn) */
3336 err |= __put_user(MOVW(2), &frame->retcode[0]);
3337 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3338 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3339 regs->pr = (unsigned long) frame->retcode;
3342 if (err)
3343 goto give_sigsegv;
3345 /* Set up registers for signal handler */
3346 regs->gregs[15] = frame_addr;
3347 regs->gregs[4] = signal; /* Arg for signal handler */
3348 regs->gregs[5] = 0;
3349 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3350 regs->pc = (unsigned long) ka->_sa_handler;
3352 unlock_user_struct(frame, frame_addr, 1);
3353 return;
3355 give_sigsegv:
3356 unlock_user_struct(frame, frame_addr, 1);
3357 force_sig(TARGET_SIGSEGV);
3360 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3361 target_siginfo_t *info,
3362 target_sigset_t *set, CPUSH4State *regs)
3364 struct target_rt_sigframe *frame;
3365 abi_ulong frame_addr;
3366 int i;
3367 int err = 0;
3368 int signal;
3370 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3371 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3372 goto give_sigsegv;
3374 signal = current_exec_domain_sig(sig);
3376 err |= copy_siginfo_to_user(&frame->info, info);
3378 /* Create the ucontext. */
3379 err |= __put_user(0, &frame->uc.tuc_flags);
3380 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3381 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3382 &frame->uc.tuc_stack.ss_sp);
3383 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3384 &frame->uc.tuc_stack.ss_flags);
3385 err |= __put_user(target_sigaltstack_used.ss_size,
3386 &frame->uc.tuc_stack.ss_size);
3387 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3388 regs, set->sig[0]);
3389 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3390 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3393 /* Set up to return from userspace. If provided, use a stub
3394 already in userspace. */
3395 if (ka->sa_flags & TARGET_SA_RESTORER) {
3396 regs->pr = (unsigned long) ka->sa_restorer;
3397 } else {
3398 /* Generate return code (system call to sigreturn) */
3399 err |= __put_user(MOVW(2), &frame->retcode[0]);
3400 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3401 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3402 regs->pr = (unsigned long) frame->retcode;
3405 if (err)
3406 goto give_sigsegv;
3408 /* Set up registers for signal handler */
3409 regs->gregs[15] = frame_addr;
3410 regs->gregs[4] = signal; /* Arg for signal handler */
3411 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3412 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3413 regs->pc = (unsigned long) ka->_sa_handler;
3415 unlock_user_struct(frame, frame_addr, 1);
3416 return;
3418 give_sigsegv:
3419 unlock_user_struct(frame, frame_addr, 1);
3420 force_sig(TARGET_SIGSEGV);
3423 long do_sigreturn(CPUSH4State *regs)
3425 struct target_sigframe *frame;
3426 abi_ulong frame_addr;
3427 sigset_t blocked;
3428 target_sigset_t target_set;
3429 target_ulong r0;
3430 int i;
3431 int err = 0;
3433 #if defined(DEBUG_SIGNAL)
3434 fprintf(stderr, "do_sigreturn\n");
3435 #endif
3436 frame_addr = regs->gregs[15];
3437 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3438 goto badframe;
3440 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3441 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3442 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3445 if (err)
3446 goto badframe;
3448 target_to_host_sigset_internal(&blocked, &target_set);
3449 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3451 if (restore_sigcontext(regs, &frame->sc, &r0))
3452 goto badframe;
3454 unlock_user_struct(frame, frame_addr, 0);
3455 return r0;
3457 badframe:
3458 unlock_user_struct(frame, frame_addr, 0);
3459 force_sig(TARGET_SIGSEGV);
3460 return 0;
3463 long do_rt_sigreturn(CPUSH4State *regs)
3465 struct target_rt_sigframe *frame;
3466 abi_ulong frame_addr;
3467 sigset_t blocked;
3468 target_ulong r0;
3470 #if defined(DEBUG_SIGNAL)
3471 fprintf(stderr, "do_rt_sigreturn\n");
3472 #endif
3473 frame_addr = regs->gregs[15];
3474 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3475 goto badframe;
3477 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3478 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3480 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3481 goto badframe;
3483 if (do_sigaltstack(frame_addr +
3484 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3485 0, get_sp_from_cpustate(regs)) == -EFAULT)
3486 goto badframe;
3488 unlock_user_struct(frame, frame_addr, 0);
3489 return r0;
3491 badframe:
3492 unlock_user_struct(frame, frame_addr, 0);
3493 force_sig(TARGET_SIGSEGV);
3494 return 0;
3496 #elif defined(TARGET_MICROBLAZE)
3498 struct target_sigcontext {
3499 struct target_pt_regs regs; /* needs to be first */
3500 uint32_t oldmask;
3503 struct target_stack_t {
3504 abi_ulong ss_sp;
3505 int ss_flags;
3506 unsigned int ss_size;
3509 struct target_ucontext {
3510 abi_ulong tuc_flags;
3511 abi_ulong tuc_link;
3512 struct target_stack_t tuc_stack;
3513 struct target_sigcontext tuc_mcontext;
3514 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3517 /* Signal frames. */
3518 struct target_signal_frame {
3519 struct target_ucontext uc;
3520 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3521 uint32_t tramp[2];
3524 struct rt_signal_frame {
3525 siginfo_t info;
3526 struct ucontext uc;
3527 uint32_t tramp[2];
3530 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3532 __put_user(env->regs[0], &sc->regs.r0);
3533 __put_user(env->regs[1], &sc->regs.r1);
3534 __put_user(env->regs[2], &sc->regs.r2);
3535 __put_user(env->regs[3], &sc->regs.r3);
3536 __put_user(env->regs[4], &sc->regs.r4);
3537 __put_user(env->regs[5], &sc->regs.r5);
3538 __put_user(env->regs[6], &sc->regs.r6);
3539 __put_user(env->regs[7], &sc->regs.r7);
3540 __put_user(env->regs[8], &sc->regs.r8);
3541 __put_user(env->regs[9], &sc->regs.r9);
3542 __put_user(env->regs[10], &sc->regs.r10);
3543 __put_user(env->regs[11], &sc->regs.r11);
3544 __put_user(env->regs[12], &sc->regs.r12);
3545 __put_user(env->regs[13], &sc->regs.r13);
3546 __put_user(env->regs[14], &sc->regs.r14);
3547 __put_user(env->regs[15], &sc->regs.r15);
3548 __put_user(env->regs[16], &sc->regs.r16);
3549 __put_user(env->regs[17], &sc->regs.r17);
3550 __put_user(env->regs[18], &sc->regs.r18);
3551 __put_user(env->regs[19], &sc->regs.r19);
3552 __put_user(env->regs[20], &sc->regs.r20);
3553 __put_user(env->regs[21], &sc->regs.r21);
3554 __put_user(env->regs[22], &sc->regs.r22);
3555 __put_user(env->regs[23], &sc->regs.r23);
3556 __put_user(env->regs[24], &sc->regs.r24);
3557 __put_user(env->regs[25], &sc->regs.r25);
3558 __put_user(env->regs[26], &sc->regs.r26);
3559 __put_user(env->regs[27], &sc->regs.r27);
3560 __put_user(env->regs[28], &sc->regs.r28);
3561 __put_user(env->regs[29], &sc->regs.r29);
3562 __put_user(env->regs[30], &sc->regs.r30);
3563 __put_user(env->regs[31], &sc->regs.r31);
3564 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3567 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3569 __get_user(env->regs[0], &sc->regs.r0);
3570 __get_user(env->regs[1], &sc->regs.r1);
3571 __get_user(env->regs[2], &sc->regs.r2);
3572 __get_user(env->regs[3], &sc->regs.r3);
3573 __get_user(env->regs[4], &sc->regs.r4);
3574 __get_user(env->regs[5], &sc->regs.r5);
3575 __get_user(env->regs[6], &sc->regs.r6);
3576 __get_user(env->regs[7], &sc->regs.r7);
3577 __get_user(env->regs[8], &sc->regs.r8);
3578 __get_user(env->regs[9], &sc->regs.r9);
3579 __get_user(env->regs[10], &sc->regs.r10);
3580 __get_user(env->regs[11], &sc->regs.r11);
3581 __get_user(env->regs[12], &sc->regs.r12);
3582 __get_user(env->regs[13], &sc->regs.r13);
3583 __get_user(env->regs[14], &sc->regs.r14);
3584 __get_user(env->regs[15], &sc->regs.r15);
3585 __get_user(env->regs[16], &sc->regs.r16);
3586 __get_user(env->regs[17], &sc->regs.r17);
3587 __get_user(env->regs[18], &sc->regs.r18);
3588 __get_user(env->regs[19], &sc->regs.r19);
3589 __get_user(env->regs[20], &sc->regs.r20);
3590 __get_user(env->regs[21], &sc->regs.r21);
3591 __get_user(env->regs[22], &sc->regs.r22);
3592 __get_user(env->regs[23], &sc->regs.r23);
3593 __get_user(env->regs[24], &sc->regs.r24);
3594 __get_user(env->regs[25], &sc->regs.r25);
3595 __get_user(env->regs[26], &sc->regs.r26);
3596 __get_user(env->regs[27], &sc->regs.r27);
3597 __get_user(env->regs[28], &sc->regs.r28);
3598 __get_user(env->regs[29], &sc->regs.r29);
3599 __get_user(env->regs[30], &sc->regs.r30);
3600 __get_user(env->regs[31], &sc->regs.r31);
3601 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3604 static abi_ulong get_sigframe(struct target_sigaction *ka,
3605 CPUMBState *env, int frame_size)
3607 abi_ulong sp = env->regs[1];
3609 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3610 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3612 return ((sp - frame_size) & -8UL);
3615 static void setup_frame(int sig, struct target_sigaction *ka,
3616 target_sigset_t *set, CPUMBState *env)
3618 struct target_signal_frame *frame;
3619 abi_ulong frame_addr;
3620 int err = 0;
3621 int i;
3623 frame_addr = get_sigframe(ka, env, sizeof *frame);
3624 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3625 goto badframe;
3627 /* Save the mask. */
3628 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3629 if (err)
3630 goto badframe;
3632 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3633 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3634 goto badframe;
3637 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3639 /* Set up to return from userspace. If provided, use a stub
3640 already in userspace. */
3641 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3642 if (ka->sa_flags & TARGET_SA_RESTORER) {
3643 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3644 } else {
3645 uint32_t t;
3646 /* Note, these encodings are _big endian_! */
3647 /* addi r12, r0, __NR_sigreturn */
3648 t = 0x31800000UL | TARGET_NR_sigreturn;
3649 err |= __put_user(t, frame->tramp + 0);
3650 /* brki r14, 0x8 */
3651 t = 0xb9cc0008UL;
3652 err |= __put_user(t, frame->tramp + 1);
3654 /* Return from sighandler will jump to the tramp.
3655 Negative 8 offset because return is rtsd r15, 8 */
3656 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3659 if (err)
3660 goto badframe;
3662 /* Set up registers for signal handler */
3663 env->regs[1] = frame_addr;
3664 /* Signal handler args: */
3665 env->regs[5] = sig; /* Arg 0: signum */
3666 env->regs[6] = 0;
3667 /* arg 1: sigcontext */
3668 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3670 /* Offset of 4 to handle microblaze rtid r14, 0 */
3671 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3673 unlock_user_struct(frame, frame_addr, 1);
3674 return;
3675 badframe:
3676 unlock_user_struct(frame, frame_addr, 1);
3677 force_sig(TARGET_SIGSEGV);
3680 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3681 target_siginfo_t *info,
3682 target_sigset_t *set, CPUMBState *env)
3684 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3687 long do_sigreturn(CPUMBState *env)
3689 struct target_signal_frame *frame;
3690 abi_ulong frame_addr;
3691 target_sigset_t target_set;
3692 sigset_t set;
3693 int i;
3695 frame_addr = env->regs[R_SP];
3696 /* Make sure the guest isn't playing games. */
3697 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3698 goto badframe;
3700 /* Restore blocked signals */
3701 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3702 goto badframe;
3703 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3704 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3705 goto badframe;
3707 target_to_host_sigset_internal(&set, &target_set);
3708 do_sigprocmask(SIG_SETMASK, &set, NULL);
3710 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3711 /* We got here through a sigreturn syscall, our path back is via an
3712 rtb insn so setup r14 for that. */
3713 env->regs[14] = env->sregs[SR_PC];
3715 unlock_user_struct(frame, frame_addr, 0);
3716 return env->regs[10];
3717 badframe:
3718 unlock_user_struct(frame, frame_addr, 0);
3719 force_sig(TARGET_SIGSEGV);
3722 long do_rt_sigreturn(CPUMBState *env)
3724 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3725 return -TARGET_ENOSYS;
3728 #elif defined(TARGET_CRIS)
3730 struct target_sigcontext {
3731 struct target_pt_regs regs; /* needs to be first */
3732 uint32_t oldmask;
3733 uint32_t usp; /* usp before stacking this gunk on it */
3736 /* Signal frames. */
3737 struct target_signal_frame {
3738 struct target_sigcontext sc;
3739 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3740 uint16_t retcode[4]; /* Trampoline code. */
3743 struct rt_signal_frame {
3744 siginfo_t *pinfo;
3745 void *puc;
3746 siginfo_t info;
3747 struct ucontext uc;
3748 uint16_t retcode[4]; /* Trampoline code. */
3751 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3753 __put_user(env->regs[0], &sc->regs.r0);
3754 __put_user(env->regs[1], &sc->regs.r1);
3755 __put_user(env->regs[2], &sc->regs.r2);
3756 __put_user(env->regs[3], &sc->regs.r3);
3757 __put_user(env->regs[4], &sc->regs.r4);
3758 __put_user(env->regs[5], &sc->regs.r5);
3759 __put_user(env->regs[6], &sc->regs.r6);
3760 __put_user(env->regs[7], &sc->regs.r7);
3761 __put_user(env->regs[8], &sc->regs.r8);
3762 __put_user(env->regs[9], &sc->regs.r9);
3763 __put_user(env->regs[10], &sc->regs.r10);
3764 __put_user(env->regs[11], &sc->regs.r11);
3765 __put_user(env->regs[12], &sc->regs.r12);
3766 __put_user(env->regs[13], &sc->regs.r13);
3767 __put_user(env->regs[14], &sc->usp);
3768 __put_user(env->regs[15], &sc->regs.acr);
3769 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3770 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3771 __put_user(env->pc, &sc->regs.erp);
3774 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3776 __get_user(env->regs[0], &sc->regs.r0);
3777 __get_user(env->regs[1], &sc->regs.r1);
3778 __get_user(env->regs[2], &sc->regs.r2);
3779 __get_user(env->regs[3], &sc->regs.r3);
3780 __get_user(env->regs[4], &sc->regs.r4);
3781 __get_user(env->regs[5], &sc->regs.r5);
3782 __get_user(env->regs[6], &sc->regs.r6);
3783 __get_user(env->regs[7], &sc->regs.r7);
3784 __get_user(env->regs[8], &sc->regs.r8);
3785 __get_user(env->regs[9], &sc->regs.r9);
3786 __get_user(env->regs[10], &sc->regs.r10);
3787 __get_user(env->regs[11], &sc->regs.r11);
3788 __get_user(env->regs[12], &sc->regs.r12);
3789 __get_user(env->regs[13], &sc->regs.r13);
3790 __get_user(env->regs[14], &sc->usp);
3791 __get_user(env->regs[15], &sc->regs.acr);
3792 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3793 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3794 __get_user(env->pc, &sc->regs.erp);
3797 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3799 abi_ulong sp;
3800 /* Align the stack downwards to 4. */
3801 sp = (env->regs[R_SP] & ~3);
3802 return sp - framesize;
3805 static void setup_frame(int sig, struct target_sigaction *ka,
3806 target_sigset_t *set, CPUCRISState *env)
3808 struct target_signal_frame *frame;
3809 abi_ulong frame_addr;
3810 int err = 0;
3811 int i;
3813 frame_addr = get_sigframe(env, sizeof *frame);
3814 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3815 goto badframe;
3818 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3819 * use this trampoline anymore but it sets it up for GDB.
3820 * In QEMU, using the trampoline simplifies things a bit so we use it.
3822 * This is movu.w __NR_sigreturn, r9; break 13;
3824 err |= __put_user(0x9c5f, frame->retcode+0);
3825 err |= __put_user(TARGET_NR_sigreturn,
3826 frame->retcode + 1);
3827 err |= __put_user(0xe93d, frame->retcode + 2);
3829 /* Save the mask. */
3830 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3831 if (err)
3832 goto badframe;
3834 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3835 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3836 goto badframe;
3839 setup_sigcontext(&frame->sc, env);
3841 /* Move the stack and setup the arguments for the handler. */
3842 env->regs[R_SP] = frame_addr;
3843 env->regs[10] = sig;
3844 env->pc = (unsigned long) ka->_sa_handler;
3845 /* Link SRP so the guest returns through the trampoline. */
3846 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3848 unlock_user_struct(frame, frame_addr, 1);
3849 return;
3850 badframe:
3851 unlock_user_struct(frame, frame_addr, 1);
3852 force_sig(TARGET_SIGSEGV);
3855 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3856 target_siginfo_t *info,
3857 target_sigset_t *set, CPUCRISState *env)
3859 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3862 long do_sigreturn(CPUCRISState *env)
3864 struct target_signal_frame *frame;
3865 abi_ulong frame_addr;
3866 target_sigset_t target_set;
3867 sigset_t set;
3868 int i;
3870 frame_addr = env->regs[R_SP];
3871 /* Make sure the guest isn't playing games. */
3872 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3873 goto badframe;
3875 /* Restore blocked signals */
3876 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3877 goto badframe;
3878 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3879 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3880 goto badframe;
3882 target_to_host_sigset_internal(&set, &target_set);
3883 do_sigprocmask(SIG_SETMASK, &set, NULL);
3885 restore_sigcontext(&frame->sc, env);
3886 unlock_user_struct(frame, frame_addr, 0);
3887 return env->regs[10];
3888 badframe:
3889 unlock_user_struct(frame, frame_addr, 0);
3890 force_sig(TARGET_SIGSEGV);
3893 long do_rt_sigreturn(CPUCRISState *env)
3895 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3896 return -TARGET_ENOSYS;
3899 #elif defined(TARGET_OPENRISC)
3901 struct target_sigcontext {
3902 struct target_pt_regs regs;
3903 abi_ulong oldmask;
3904 abi_ulong usp;
3907 struct target_ucontext {
3908 abi_ulong tuc_flags;
3909 abi_ulong tuc_link;
3910 target_stack_t tuc_stack;
3911 struct target_sigcontext tuc_mcontext;
3912 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3915 struct target_rt_sigframe {
3916 abi_ulong pinfo;
3917 uint64_t puc;
3918 struct target_siginfo info;
3919 struct target_sigcontext sc;
3920 struct target_ucontext uc;
3921 unsigned char retcode[16]; /* trampoline code */
3924 /* This is the asm-generic/ucontext.h version */
3925 #if 0
3926 static int restore_sigcontext(CPUOpenRISCState *regs,
3927 struct target_sigcontext *sc)
3929 unsigned int err = 0;
3930 unsigned long old_usp;
3932 /* Alwys make any pending restarted system call return -EINTR */
3933 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3935 /* restore the regs from &sc->regs (same as sc, since regs is first)
3936 * (sc is already checked for VERIFY_READ since the sigframe was
3937 * checked in sys_sigreturn previously)
3940 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3941 goto badframe;
3944 /* make sure the U-flag is set so user-mode cannot fool us */
3946 regs->sr &= ~SR_SM;
3948 /* restore the old USP as it was before we stacked the sc etc.
3949 * (we cannot just pop the sigcontext since we aligned the sp and
3950 * stuff after pushing it)
3953 err |= __get_user(old_usp, &sc->usp);
3954 phx_signal("old_usp 0x%lx", old_usp);
3956 __PHX__ REALLY /* ??? */
3957 wrusp(old_usp);
3958 regs->gpr[1] = old_usp;
3960 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3961 * after this completes, but we don't use that mechanism. maybe we can
3962 * use it now ?
3965 return err;
3967 badframe:
3968 return 1;
3970 #endif
3972 /* Set up a signal frame. */
3974 static int setup_sigcontext(struct target_sigcontext *sc,
3975 CPUOpenRISCState *regs,
3976 unsigned long mask)
3978 int err = 0;
3979 unsigned long usp = regs->gpr[1];
3981 /* copy the regs. they are first in sc so we can use sc directly */
3983 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3985 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3986 the signal handler. The frametype will be restored to its previous
3987 value in restore_sigcontext. */
3988 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3990 /* then some other stuff */
3991 err |= __put_user(mask, &sc->oldmask);
3992 err |= __put_user(usp, &sc->usp); return err;
3995 static inline unsigned long align_sigframe(unsigned long sp)
3997 unsigned long i;
3998 i = sp & ~3UL;
3999 return i;
4002 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
4003 CPUOpenRISCState *regs,
4004 size_t frame_size)
4006 unsigned long sp = regs->gpr[1];
4007 int onsigstack = on_sig_stack(sp);
4009 /* redzone */
4010 /* This is the X/Open sanctioned signal stack switching. */
4011 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
4012 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4015 sp = align_sigframe(sp - frame_size);
4018 * If we are on the alternate signal stack and would overflow it, don't.
4019 * Return an always-bogus address instead so we will die with SIGSEGV.
4022 if (onsigstack && !likely(on_sig_stack(sp))) {
4023 return -1L;
4026 return sp;
4029 static void setup_frame(int sig, struct target_sigaction *ka,
4030 target_sigset_t *set, CPUOpenRISCState *env)
4032 qemu_log("Not implement.\n");
4035 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4036 target_siginfo_t *info,
4037 target_sigset_t *set, CPUOpenRISCState *env)
4039 int err = 0;
4040 abi_ulong frame_addr;
4041 unsigned long return_ip;
4042 struct target_rt_sigframe *frame;
4043 abi_ulong info_addr, uc_addr;
4045 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4046 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4047 goto give_sigsegv;
4050 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4051 err |= __put_user(info_addr, &frame->pinfo);
4052 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4053 err |= __put_user(uc_addr, &frame->puc);
4055 if (ka->sa_flags & SA_SIGINFO) {
4056 err |= copy_siginfo_to_user(&frame->info, info);
4058 if (err) {
4059 goto give_sigsegv;
4062 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
4063 err |= __put_user(0, &frame->uc.tuc_flags);
4064 err |= __put_user(0, &frame->uc.tuc_link);
4065 err |= __put_user(target_sigaltstack_used.ss_sp,
4066 &frame->uc.tuc_stack.ss_sp);
4067 err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
4068 err |= __put_user(target_sigaltstack_used.ss_size,
4069 &frame->uc.tuc_stack.ss_size);
4070 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4072 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4074 if (err) {
4075 goto give_sigsegv;
4078 /* trampoline - the desired return ip is the retcode itself */
4079 return_ip = (unsigned long)&frame->retcode;
4080 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
4081 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
4082 err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
4083 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
4084 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
4086 if (err) {
4087 goto give_sigsegv;
4090 /* TODO what is the current->exec_domain stuff and invmap ? */
4092 /* Set up registers for signal handler */
4093 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
4094 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
4095 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
4096 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
4097 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
4099 /* actually move the usp to reflect the stacked frame */
4100 env->gpr[1] = (unsigned long)frame;
4102 return;
4104 give_sigsegv:
4105 unlock_user_struct(frame, frame_addr, 1);
4106 if (sig == TARGET_SIGSEGV) {
4107 ka->_sa_handler = TARGET_SIG_DFL;
4109 force_sig(TARGET_SIGSEGV);
4112 long do_sigreturn(CPUOpenRISCState *env)
4115 qemu_log("do_sigreturn: not implemented\n");
4116 return -TARGET_ENOSYS;
4119 long do_rt_sigreturn(CPUOpenRISCState *env)
4121 qemu_log("do_rt_sigreturn: not implemented\n");
4122 return -TARGET_ENOSYS;
4124 /* TARGET_OPENRISC */
4126 #elif defined(TARGET_S390X)
4128 #define __NUM_GPRS 16
4129 #define __NUM_FPRS 16
4130 #define __NUM_ACRS 16
4132 #define S390_SYSCALL_SIZE 2
4133 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4135 #define _SIGCONTEXT_NSIG 64
4136 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4137 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4138 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4139 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4140 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4142 typedef struct {
4143 target_psw_t psw;
4144 target_ulong gprs[__NUM_GPRS];
4145 unsigned int acrs[__NUM_ACRS];
4146 } target_s390_regs_common;
4148 typedef struct {
4149 unsigned int fpc;
4150 double fprs[__NUM_FPRS];
4151 } target_s390_fp_regs;
4153 typedef struct {
4154 target_s390_regs_common regs;
4155 target_s390_fp_regs fpregs;
4156 } target_sigregs;
4158 struct target_sigcontext {
4159 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4160 target_sigregs *sregs;
4163 typedef struct {
4164 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4165 struct target_sigcontext sc;
4166 target_sigregs sregs;
4167 int signo;
4168 uint8_t retcode[S390_SYSCALL_SIZE];
4169 } sigframe;
4171 struct target_ucontext {
4172 target_ulong tuc_flags;
4173 struct target_ucontext *tuc_link;
4174 target_stack_t tuc_stack;
4175 target_sigregs tuc_mcontext;
4176 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4179 typedef struct {
4180 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4181 uint8_t retcode[S390_SYSCALL_SIZE];
4182 struct target_siginfo info;
4183 struct target_ucontext uc;
4184 } rt_sigframe;
4186 static inline abi_ulong
4187 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4189 abi_ulong sp;
4191 /* Default to using normal stack */
4192 sp = env->regs[15];
4194 /* This is the X/Open sanctioned signal stack switching. */
4195 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4196 if (!sas_ss_flags(sp)) {
4197 sp = target_sigaltstack_used.ss_sp +
4198 target_sigaltstack_used.ss_size;
4202 /* This is the legacy signal stack switching. */
4203 else if (/* FIXME !user_mode(regs) */ 0 &&
4204 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4205 ka->sa_restorer) {
4206 sp = (abi_ulong) ka->sa_restorer;
4209 return (sp - frame_size) & -8ul;
4212 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4214 int i;
4215 //save_access_regs(current->thread.acrs); FIXME
4217 /* Copy a 'clean' PSW mask to the user to avoid leaking
4218 information about whether PER is currently on. */
4219 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4220 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4221 for (i = 0; i < 16; i++) {
4222 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4224 for (i = 0; i < 16; i++) {
4225 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4228 * We have to store the fp registers to current->thread.fp_regs
4229 * to merge them with the emulated registers.
4231 //save_fp_regs(&current->thread.fp_regs); FIXME
4232 for (i = 0; i < 16; i++) {
4233 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
4237 static void setup_frame(int sig, struct target_sigaction *ka,
4238 target_sigset_t *set, CPUS390XState *env)
4240 sigframe *frame;
4241 abi_ulong frame_addr;
4243 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4244 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4245 (unsigned long long)frame_addr);
4246 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4247 goto give_sigsegv;
4250 qemu_log("%s: 1\n", __FUNCTION__);
4251 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
4252 goto give_sigsegv;
4255 save_sigregs(env, &frame->sregs);
4257 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4258 (abi_ulong *)&frame->sc.sregs);
4260 /* Set up to return from userspace. If provided, use a stub
4261 already in userspace. */
4262 if (ka->sa_flags & TARGET_SA_RESTORER) {
4263 env->regs[14] = (unsigned long)
4264 ka->sa_restorer | PSW_ADDR_AMODE;
4265 } else {
4266 env->regs[14] = (unsigned long)
4267 frame->retcode | PSW_ADDR_AMODE;
4268 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4269 (uint16_t *)(frame->retcode)))
4270 goto give_sigsegv;
4273 /* Set up backchain. */
4274 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4275 goto give_sigsegv;
4278 /* Set up registers for signal handler */
4279 env->regs[15] = frame_addr;
4280 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4282 env->regs[2] = sig; //map_signal(sig);
4283 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4285 /* We forgot to include these in the sigcontext.
4286 To avoid breaking binary compatibility, they are passed as args. */
4287 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4288 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4290 /* Place signal number on stack to allow backtrace from handler. */
4291 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4292 goto give_sigsegv;
4294 unlock_user_struct(frame, frame_addr, 1);
4295 return;
4297 give_sigsegv:
4298 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4299 unlock_user_struct(frame, frame_addr, 1);
4300 force_sig(TARGET_SIGSEGV);
4303 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4304 target_siginfo_t *info,
4305 target_sigset_t *set, CPUS390XState *env)
4307 int i;
4308 rt_sigframe *frame;
4309 abi_ulong frame_addr;
4311 frame_addr = get_sigframe(ka, env, sizeof *frame);
4312 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4313 (unsigned long long)frame_addr);
4314 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4315 goto give_sigsegv;
4318 qemu_log("%s: 1\n", __FUNCTION__);
4319 if (copy_siginfo_to_user(&frame->info, info)) {
4320 goto give_sigsegv;
4323 /* Create the ucontext. */
4324 __put_user(0, &frame->uc.tuc_flags);
4325 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4326 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4327 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4328 &frame->uc.tuc_stack.ss_flags);
4329 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4330 save_sigregs(env, &frame->uc.tuc_mcontext);
4331 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4332 __put_user((abi_ulong)set->sig[i],
4333 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4336 /* Set up to return from userspace. If provided, use a stub
4337 already in userspace. */
4338 if (ka->sa_flags & TARGET_SA_RESTORER) {
4339 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4340 } else {
4341 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4342 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4343 (uint16_t *)(frame->retcode))) {
4344 goto give_sigsegv;
4348 /* Set up backchain. */
4349 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4350 goto give_sigsegv;
4353 /* Set up registers for signal handler */
4354 env->regs[15] = frame_addr;
4355 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4357 env->regs[2] = sig; //map_signal(sig);
4358 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4359 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4360 return;
4362 give_sigsegv:
4363 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4364 unlock_user_struct(frame, frame_addr, 1);
4365 force_sig(TARGET_SIGSEGV);
4368 static int
4369 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4371 int err = 0;
4372 int i;
4374 for (i = 0; i < 16; i++) {
4375 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4378 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4379 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4380 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4381 (unsigned long long)env->psw.addr);
4382 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4383 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4385 for (i = 0; i < 16; i++) {
4386 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4388 for (i = 0; i < 16; i++) {
4389 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4392 return err;
4395 long do_sigreturn(CPUS390XState *env)
4397 sigframe *frame;
4398 abi_ulong frame_addr = env->regs[15];
4399 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4400 (unsigned long long)frame_addr);
4401 target_sigset_t target_set;
4402 sigset_t set;
4404 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4405 goto badframe;
4407 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4408 goto badframe;
4411 target_to_host_sigset_internal(&set, &target_set);
4412 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4414 if (restore_sigregs(env, &frame->sregs)) {
4415 goto badframe;
4418 unlock_user_struct(frame, frame_addr, 0);
4419 return env->regs[2];
4421 badframe:
4422 unlock_user_struct(frame, frame_addr, 0);
4423 force_sig(TARGET_SIGSEGV);
4424 return 0;
4427 long do_rt_sigreturn(CPUS390XState *env)
4429 rt_sigframe *frame;
4430 abi_ulong frame_addr = env->regs[15];
4431 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4432 (unsigned long long)frame_addr);
4433 sigset_t set;
4435 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4436 goto badframe;
4438 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4440 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4442 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4443 goto badframe;
4446 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4447 get_sp_from_cpustate(env)) == -EFAULT) {
4448 goto badframe;
4450 unlock_user_struct(frame, frame_addr, 0);
4451 return env->regs[2];
4453 badframe:
4454 unlock_user_struct(frame, frame_addr, 0);
4455 force_sig(TARGET_SIGSEGV);
4456 return 0;
4459 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4461 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4462 the signal handling is different enough that we haven't implemented
4463 support for PPC64 yet. Hence the restriction above.
4465 There are various #if'd blocks for code for TARGET_PPC64. These
4466 blocks should go away so that we can successfully run 32-bit and
4467 64-bit binaries on a QEMU configured for PPC64. */
4469 /* Size of dummy stack frame allocated when calling signal handler.
4470 See arch/powerpc/include/asm/ptrace.h. */
4471 #if defined(TARGET_PPC64)
4472 #define SIGNAL_FRAMESIZE 128
4473 #else
4474 #define SIGNAL_FRAMESIZE 64
4475 #endif
4477 /* See arch/powerpc/include/asm/sigcontext.h. */
4478 struct target_sigcontext {
4479 target_ulong _unused[4];
4480 int32_t signal;
4481 #if defined(TARGET_PPC64)
4482 int32_t pad0;
4483 #endif
4484 target_ulong handler;
4485 target_ulong oldmask;
4486 target_ulong regs; /* struct pt_regs __user * */
4487 /* TODO: PPC64 includes extra bits here. */
4490 /* Indices for target_mcontext.mc_gregs, below.
4491 See arch/powerpc/include/asm/ptrace.h for details. */
4492 enum {
4493 TARGET_PT_R0 = 0,
4494 TARGET_PT_R1 = 1,
4495 TARGET_PT_R2 = 2,
4496 TARGET_PT_R3 = 3,
4497 TARGET_PT_R4 = 4,
4498 TARGET_PT_R5 = 5,
4499 TARGET_PT_R6 = 6,
4500 TARGET_PT_R7 = 7,
4501 TARGET_PT_R8 = 8,
4502 TARGET_PT_R9 = 9,
4503 TARGET_PT_R10 = 10,
4504 TARGET_PT_R11 = 11,
4505 TARGET_PT_R12 = 12,
4506 TARGET_PT_R13 = 13,
4507 TARGET_PT_R14 = 14,
4508 TARGET_PT_R15 = 15,
4509 TARGET_PT_R16 = 16,
4510 TARGET_PT_R17 = 17,
4511 TARGET_PT_R18 = 18,
4512 TARGET_PT_R19 = 19,
4513 TARGET_PT_R20 = 20,
4514 TARGET_PT_R21 = 21,
4515 TARGET_PT_R22 = 22,
4516 TARGET_PT_R23 = 23,
4517 TARGET_PT_R24 = 24,
4518 TARGET_PT_R25 = 25,
4519 TARGET_PT_R26 = 26,
4520 TARGET_PT_R27 = 27,
4521 TARGET_PT_R28 = 28,
4522 TARGET_PT_R29 = 29,
4523 TARGET_PT_R30 = 30,
4524 TARGET_PT_R31 = 31,
4525 TARGET_PT_NIP = 32,
4526 TARGET_PT_MSR = 33,
4527 TARGET_PT_ORIG_R3 = 34,
4528 TARGET_PT_CTR = 35,
4529 TARGET_PT_LNK = 36,
4530 TARGET_PT_XER = 37,
4531 TARGET_PT_CCR = 38,
4532 /* Yes, there are two registers with #39. One is 64-bit only. */
4533 TARGET_PT_MQ = 39,
4534 TARGET_PT_SOFTE = 39,
4535 TARGET_PT_TRAP = 40,
4536 TARGET_PT_DAR = 41,
4537 TARGET_PT_DSISR = 42,
4538 TARGET_PT_RESULT = 43,
4539 TARGET_PT_REGS_COUNT = 44
4542 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4543 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4544 struct target_mcontext {
4545 target_ulong mc_gregs[48];
4546 /* Includes fpscr. */
4547 uint64_t mc_fregs[33];
4548 target_ulong mc_pad[2];
4549 /* We need to handle Altivec and SPE at the same time, which no
4550 kernel needs to do. Fortunately, the kernel defines this bit to
4551 be Altivec-register-large all the time, rather than trying to
4552 twiddle it based on the specific platform. */
4553 union {
4554 /* SPE vector registers. One extra for SPEFSCR. */
4555 uint32_t spe[33];
4556 /* Altivec vector registers. The packing of VSCR and VRSAVE
4557 varies depending on whether we're PPC64 or not: PPC64 splits
4558 them apart; PPC32 stuffs them together. */
4559 #if defined(TARGET_PPC64)
4560 #define QEMU_NVRREG 34
4561 #else
4562 #define QEMU_NVRREG 33
4563 #endif
4564 ppc_avr_t altivec[QEMU_NVRREG];
4565 #undef QEMU_NVRREG
4566 } mc_vregs __attribute__((__aligned__(16)));
4569 struct target_ucontext {
4570 target_ulong tuc_flags;
4571 target_ulong tuc_link; /* struct ucontext __user * */
4572 struct target_sigaltstack tuc_stack;
4573 #if !defined(TARGET_PPC64)
4574 int32_t tuc_pad[7];
4575 target_ulong tuc_regs; /* struct mcontext __user *
4576 points to uc_mcontext field */
4577 #endif
4578 target_sigset_t tuc_sigmask;
4579 #if defined(TARGET_PPC64)
4580 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4581 struct target_sigcontext tuc_mcontext;
4582 #else
4583 int32_t tuc_maskext[30];
4584 int32_t tuc_pad2[3];
4585 struct target_mcontext tuc_mcontext;
4586 #endif
4589 /* See arch/powerpc/kernel/signal_32.c. */
4590 struct target_sigframe {
4591 struct target_sigcontext sctx;
4592 struct target_mcontext mctx;
4593 int32_t abigap[56];
4596 struct target_rt_sigframe {
4597 struct target_siginfo info;
4598 struct target_ucontext uc;
4599 int32_t abigap[56];
4602 /* We use the mc_pad field for the signal return trampoline. */
4603 #define tramp mc_pad
4605 /* See arch/powerpc/kernel/signal.c. */
4606 static target_ulong get_sigframe(struct target_sigaction *ka,
4607 CPUPPCState *env,
4608 int frame_size)
4610 target_ulong oldsp, newsp;
4612 oldsp = env->gpr[1];
4614 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4615 (sas_ss_flags(oldsp) == 0)) {
4616 oldsp = (target_sigaltstack_used.ss_sp
4617 + target_sigaltstack_used.ss_size);
4620 newsp = (oldsp - frame_size) & ~0xFUL;
4622 return newsp;
4625 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4626 int sigret)
4628 target_ulong msr = env->msr;
4629 int i;
4630 target_ulong ccr = 0;
4632 /* In general, the kernel attempts to be intelligent about what it
4633 needs to save for Altivec/FP/SPE registers. We don't care that
4634 much, so we just go ahead and save everything. */
4636 /* Save general registers. */
4637 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4638 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4639 return 1;
4642 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4643 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4644 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4645 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4646 return 1;
4648 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4649 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4651 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4652 return 1;
4654 /* Save Altivec registers if necessary. */
4655 if (env->insns_flags & PPC_ALTIVEC) {
4656 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4657 ppc_avr_t *avr = &env->avr[i];
4658 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4660 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4661 __put_user(avr->u64[1], &vreg->u64[1])) {
4662 return 1;
4665 /* Set MSR_VR in the saved MSR value to indicate that
4666 frame->mc_vregs contains valid data. */
4667 msr |= MSR_VR;
4668 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4669 &frame->mc_vregs.altivec[32].u32[3]))
4670 return 1;
4673 /* Save floating point registers. */
4674 if (env->insns_flags & PPC_FLOAT) {
4675 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4676 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4677 return 1;
4680 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4681 return 1;
4684 /* Save SPE registers. The kernel only saves the high half. */
4685 if (env->insns_flags & PPC_SPE) {
4686 #if defined(TARGET_PPC64)
4687 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4688 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4689 return 1;
4692 #else
4693 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4694 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4695 return 1;
4698 #endif
4699 /* Set MSR_SPE in the saved MSR value to indicate that
4700 frame->mc_vregs contains valid data. */
4701 msr |= MSR_SPE;
4702 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4703 return 1;
4706 /* Store MSR. */
4707 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4708 return 1;
4710 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4711 if (sigret) {
4712 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4713 __put_user(0x44000002UL, &frame->tramp[1])) {
4714 return 1;
4718 return 0;
4721 static int restore_user_regs(CPUPPCState *env,
4722 struct target_mcontext *frame, int sig)
4724 target_ulong save_r2 = 0;
4725 target_ulong msr;
4726 target_ulong ccr;
4728 int i;
4730 if (!sig) {
4731 save_r2 = env->gpr[2];
4734 /* Restore general registers. */
4735 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4736 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4737 return 1;
4740 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4741 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4742 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4743 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4744 return 1;
4745 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4746 return 1;
4748 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4749 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4752 if (!sig) {
4753 env->gpr[2] = save_r2;
4755 /* Restore MSR. */
4756 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4757 return 1;
4759 /* If doing signal return, restore the previous little-endian mode. */
4760 if (sig)
4761 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4763 /* Restore Altivec registers if necessary. */
4764 if (env->insns_flags & PPC_ALTIVEC) {
4765 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4766 ppc_avr_t *avr = &env->avr[i];
4767 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4769 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4770 __get_user(avr->u64[1], &vreg->u64[1])) {
4771 return 1;
4774 /* Set MSR_VEC in the saved MSR value to indicate that
4775 frame->mc_vregs contains valid data. */
4776 if (__get_user(env->spr[SPR_VRSAVE],
4777 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4778 return 1;
4781 /* Restore floating point registers. */
4782 if (env->insns_flags & PPC_FLOAT) {
4783 uint64_t fpscr;
4784 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4785 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4786 return 1;
4789 if (__get_user(fpscr, &frame->mc_fregs[32]))
4790 return 1;
4791 env->fpscr = (uint32_t) fpscr;
4794 /* Save SPE registers. The kernel only saves the high half. */
4795 if (env->insns_flags & PPC_SPE) {
4796 #if defined(TARGET_PPC64)
4797 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4798 uint32_t hi;
4800 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4801 return 1;
4803 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4805 #else
4806 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4807 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4808 return 1;
4811 #endif
4812 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4813 return 1;
4816 return 0;
4819 static void setup_frame(int sig, struct target_sigaction *ka,
4820 target_sigset_t *set, CPUPPCState *env)
4822 struct target_sigframe *frame;
4823 struct target_sigcontext *sc;
4824 target_ulong frame_addr, newsp;
4825 int err = 0;
4826 int signal;
4828 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4829 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4830 goto sigsegv;
4831 sc = &frame->sctx;
4833 signal = current_exec_domain_sig(sig);
4835 err |= __put_user(ka->_sa_handler, &sc->handler);
4836 err |= __put_user(set->sig[0], &sc->oldmask);
4837 #if defined(TARGET_PPC64)
4838 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4839 #else
4840 err |= __put_user(set->sig[1], &sc->_unused[3]);
4841 #endif
4842 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4843 err |= __put_user(sig, &sc->signal);
4845 /* Save user regs. */
4846 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4848 /* The kernel checks for the presence of a VDSO here. We don't
4849 emulate a vdso, so use a sigreturn system call. */
4850 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4852 /* Turn off all fp exceptions. */
4853 env->fpscr = 0;
4855 /* Create a stack frame for the caller of the handler. */
4856 newsp = frame_addr - SIGNAL_FRAMESIZE;
4857 err |= put_user(env->gpr[1], newsp, target_ulong);
4859 if (err)
4860 goto sigsegv;
4862 /* Set up registers for signal handler. */
4863 env->gpr[1] = newsp;
4864 env->gpr[3] = signal;
4865 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4866 env->nip = (target_ulong) ka->_sa_handler;
4867 /* Signal handlers are entered in big-endian mode. */
4868 env->msr &= ~MSR_LE;
4870 unlock_user_struct(frame, frame_addr, 1);
4871 return;
4873 sigsegv:
4874 unlock_user_struct(frame, frame_addr, 1);
4875 qemu_log("segfaulting from setup_frame\n");
4876 force_sig(TARGET_SIGSEGV);
4879 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4880 target_siginfo_t *info,
4881 target_sigset_t *set, CPUPPCState *env)
4883 struct target_rt_sigframe *rt_sf;
4884 struct target_mcontext *frame;
4885 target_ulong rt_sf_addr, newsp = 0;
4886 int i, err = 0;
4887 int signal;
4889 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4890 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4891 goto sigsegv;
4893 signal = current_exec_domain_sig(sig);
4895 err |= copy_siginfo_to_user(&rt_sf->info, info);
4897 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4898 err |= __put_user(0, &rt_sf->uc.tuc_link);
4899 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4900 &rt_sf->uc.tuc_stack.ss_sp);
4901 err |= __put_user(sas_ss_flags(env->gpr[1]),
4902 &rt_sf->uc.tuc_stack.ss_flags);
4903 err |= __put_user(target_sigaltstack_used.ss_size,
4904 &rt_sf->uc.tuc_stack.ss_size);
4905 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4906 &rt_sf->uc.tuc_regs);
4907 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4908 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4911 frame = &rt_sf->uc.tuc_mcontext;
4912 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4914 /* The kernel checks for the presence of a VDSO here. We don't
4915 emulate a vdso, so use a sigreturn system call. */
4916 env->lr = (target_ulong) h2g(frame->tramp);
4918 /* Turn off all fp exceptions. */
4919 env->fpscr = 0;
4921 /* Create a stack frame for the caller of the handler. */
4922 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4923 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4925 if (err)
4926 goto sigsegv;
4928 /* Set up registers for signal handler. */
4929 env->gpr[1] = newsp;
4930 env->gpr[3] = (target_ulong) signal;
4931 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4932 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4933 env->gpr[6] = (target_ulong) h2g(rt_sf);
4934 env->nip = (target_ulong) ka->_sa_handler;
4935 /* Signal handlers are entered in big-endian mode. */
4936 env->msr &= ~MSR_LE;
4938 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4939 return;
4941 sigsegv:
4942 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4943 qemu_log("segfaulting from setup_rt_frame\n");
4944 force_sig(TARGET_SIGSEGV);
4948 long do_sigreturn(CPUPPCState *env)
4950 struct target_sigcontext *sc = NULL;
4951 struct target_mcontext *sr = NULL;
4952 target_ulong sr_addr = 0, sc_addr;
4953 sigset_t blocked;
4954 target_sigset_t set;
4956 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4957 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4958 goto sigsegv;
4960 #if defined(TARGET_PPC64)
4961 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4962 #else
4963 if(__get_user(set.sig[0], &sc->oldmask) ||
4964 __get_user(set.sig[1], &sc->_unused[3]))
4965 goto sigsegv;
4966 #endif
4967 target_to_host_sigset_internal(&blocked, &set);
4968 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4970 if (__get_user(sr_addr, &sc->regs))
4971 goto sigsegv;
4972 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4973 goto sigsegv;
4974 if (restore_user_regs(env, sr, 1))
4975 goto sigsegv;
4977 unlock_user_struct(sr, sr_addr, 1);
4978 unlock_user_struct(sc, sc_addr, 1);
4979 return -TARGET_QEMU_ESIGRETURN;
4981 sigsegv:
4982 unlock_user_struct(sr, sr_addr, 1);
4983 unlock_user_struct(sc, sc_addr, 1);
4984 qemu_log("segfaulting from do_sigreturn\n");
4985 force_sig(TARGET_SIGSEGV);
4986 return 0;
4989 /* See arch/powerpc/kernel/signal_32.c. */
4990 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4992 struct target_mcontext *mcp;
4993 target_ulong mcp_addr;
4994 sigset_t blocked;
4995 target_sigset_t set;
4997 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4998 sizeof (set)))
4999 return 1;
5001 #if defined(TARGET_PPC64)
5002 fprintf (stderr, "do_setcontext: not implemented\n");
5003 return 0;
5004 #else
5005 if (__get_user(mcp_addr, &ucp->tuc_regs))
5006 return 1;
5008 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
5009 return 1;
5011 target_to_host_sigset_internal(&blocked, &set);
5012 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
5013 if (restore_user_regs(env, mcp, sig))
5014 goto sigsegv;
5016 unlock_user_struct(mcp, mcp_addr, 1);
5017 return 0;
5019 sigsegv:
5020 unlock_user_struct(mcp, mcp_addr, 1);
5021 return 1;
5022 #endif
5025 long do_rt_sigreturn(CPUPPCState *env)
5027 struct target_rt_sigframe *rt_sf = NULL;
5028 target_ulong rt_sf_addr;
5030 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
5031 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
5032 goto sigsegv;
5034 if (do_setcontext(&rt_sf->uc, env, 1))
5035 goto sigsegv;
5037 do_sigaltstack(rt_sf_addr
5038 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
5039 0, env->gpr[1]);
5041 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5042 return -TARGET_QEMU_ESIGRETURN;
5044 sigsegv:
5045 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5046 qemu_log("segfaulting from do_rt_sigreturn\n");
5047 force_sig(TARGET_SIGSEGV);
5048 return 0;
5051 #elif defined(TARGET_M68K)
5053 struct target_sigcontext {
5054 abi_ulong sc_mask;
5055 abi_ulong sc_usp;
5056 abi_ulong sc_d0;
5057 abi_ulong sc_d1;
5058 abi_ulong sc_a0;
5059 abi_ulong sc_a1;
5060 unsigned short sc_sr;
5061 abi_ulong sc_pc;
5064 struct target_sigframe
5066 abi_ulong pretcode;
5067 int sig;
5068 int code;
5069 abi_ulong psc;
5070 char retcode[8];
5071 abi_ulong extramask[TARGET_NSIG_WORDS-1];
5072 struct target_sigcontext sc;
5075 typedef int target_greg_t;
5076 #define TARGET_NGREG 18
5077 typedef target_greg_t target_gregset_t[TARGET_NGREG];
5079 typedef struct target_fpregset {
5080 int f_fpcntl[3];
5081 int f_fpregs[8*3];
5082 } target_fpregset_t;
5084 struct target_mcontext {
5085 int version;
5086 target_gregset_t gregs;
5087 target_fpregset_t fpregs;
5090 #define TARGET_MCONTEXT_VERSION 2
5092 struct target_ucontext {
5093 abi_ulong tuc_flags;
5094 abi_ulong tuc_link;
5095 target_stack_t tuc_stack;
5096 struct target_mcontext tuc_mcontext;
5097 abi_long tuc_filler[80];
5098 target_sigset_t tuc_sigmask;
5101 struct target_rt_sigframe
5103 abi_ulong pretcode;
5104 int sig;
5105 abi_ulong pinfo;
5106 abi_ulong puc;
5107 char retcode[8];
5108 struct target_siginfo info;
5109 struct target_ucontext uc;
5112 static int
5113 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
5114 abi_ulong mask)
5116 int err = 0;
5118 err |= __put_user(mask, &sc->sc_mask);
5119 err |= __put_user(env->aregs[7], &sc->sc_usp);
5120 err |= __put_user(env->dregs[0], &sc->sc_d0);
5121 err |= __put_user(env->dregs[1], &sc->sc_d1);
5122 err |= __put_user(env->aregs[0], &sc->sc_a0);
5123 err |= __put_user(env->aregs[1], &sc->sc_a1);
5124 err |= __put_user(env->sr, &sc->sc_sr);
5125 err |= __put_user(env->pc, &sc->sc_pc);
5127 return err;
5130 static int
5131 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
5133 int err = 0;
5134 int temp;
5136 err |= __get_user(env->aregs[7], &sc->sc_usp);
5137 err |= __get_user(env->dregs[1], &sc->sc_d1);
5138 err |= __get_user(env->aregs[0], &sc->sc_a0);
5139 err |= __get_user(env->aregs[1], &sc->sc_a1);
5140 err |= __get_user(env->pc, &sc->sc_pc);
5141 err |= __get_user(temp, &sc->sc_sr);
5142 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5144 *pd0 = tswapl(sc->sc_d0);
5146 return err;
5150 * Determine which stack to use..
5152 static inline abi_ulong
5153 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5154 size_t frame_size)
5156 unsigned long sp;
5158 sp = regs->aregs[7];
5160 /* This is the X/Open sanctioned signal stack switching. */
5161 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5162 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5165 return ((sp - frame_size) & -8UL);
5168 static void setup_frame(int sig, struct target_sigaction *ka,
5169 target_sigset_t *set, CPUM68KState *env)
5171 struct target_sigframe *frame;
5172 abi_ulong frame_addr;
5173 abi_ulong retcode_addr;
5174 abi_ulong sc_addr;
5175 int err = 0;
5176 int i;
5178 frame_addr = get_sigframe(ka, env, sizeof *frame);
5179 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5180 goto give_sigsegv;
5182 err |= __put_user(sig, &frame->sig);
5184 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5185 err |= __put_user(sc_addr, &frame->psc);
5187 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
5188 if (err)
5189 goto give_sigsegv;
5191 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5192 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
5193 goto give_sigsegv;
5196 /* Set up to return from userspace. */
5198 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5199 err |= __put_user(retcode_addr, &frame->pretcode);
5201 /* moveq #,d0; trap #0 */
5203 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5204 (long *)(frame->retcode));
5206 if (err)
5207 goto give_sigsegv;
5209 /* Set up to return from userspace */
5211 env->aregs[7] = frame_addr;
5212 env->pc = ka->_sa_handler;
5214 unlock_user_struct(frame, frame_addr, 1);
5215 return;
5217 give_sigsegv:
5218 unlock_user_struct(frame, frame_addr, 1);
5219 force_sig(TARGET_SIGSEGV);
5222 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5223 CPUM68KState *env)
5225 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5226 int err;
5228 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5229 err |= __put_user(env->dregs[0], &gregs[0]);
5230 err |= __put_user(env->dregs[1], &gregs[1]);
5231 err |= __put_user(env->dregs[2], &gregs[2]);
5232 err |= __put_user(env->dregs[3], &gregs[3]);
5233 err |= __put_user(env->dregs[4], &gregs[4]);
5234 err |= __put_user(env->dregs[5], &gregs[5]);
5235 err |= __put_user(env->dregs[6], &gregs[6]);
5236 err |= __put_user(env->dregs[7], &gregs[7]);
5237 err |= __put_user(env->aregs[0], &gregs[8]);
5238 err |= __put_user(env->aregs[1], &gregs[9]);
5239 err |= __put_user(env->aregs[2], &gregs[10]);
5240 err |= __put_user(env->aregs[3], &gregs[11]);
5241 err |= __put_user(env->aregs[4], &gregs[12]);
5242 err |= __put_user(env->aregs[5], &gregs[13]);
5243 err |= __put_user(env->aregs[6], &gregs[14]);
5244 err |= __put_user(env->aregs[7], &gregs[15]);
5245 err |= __put_user(env->pc, &gregs[16]);
5246 err |= __put_user(env->sr, &gregs[17]);
5248 return err;
5251 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5252 struct target_ucontext *uc,
5253 int *pd0)
5255 int temp;
5256 int err;
5257 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5259 err = __get_user(temp, &uc->tuc_mcontext.version);
5260 if (temp != TARGET_MCONTEXT_VERSION)
5261 goto badframe;
5263 /* restore passed registers */
5264 err |= __get_user(env->dregs[0], &gregs[0]);
5265 err |= __get_user(env->dregs[1], &gregs[1]);
5266 err |= __get_user(env->dregs[2], &gregs[2]);
5267 err |= __get_user(env->dregs[3], &gregs[3]);
5268 err |= __get_user(env->dregs[4], &gregs[4]);
5269 err |= __get_user(env->dregs[5], &gregs[5]);
5270 err |= __get_user(env->dregs[6], &gregs[6]);
5271 err |= __get_user(env->dregs[7], &gregs[7]);
5272 err |= __get_user(env->aregs[0], &gregs[8]);
5273 err |= __get_user(env->aregs[1], &gregs[9]);
5274 err |= __get_user(env->aregs[2], &gregs[10]);
5275 err |= __get_user(env->aregs[3], &gregs[11]);
5276 err |= __get_user(env->aregs[4], &gregs[12]);
5277 err |= __get_user(env->aregs[5], &gregs[13]);
5278 err |= __get_user(env->aregs[6], &gregs[14]);
5279 err |= __get_user(env->aregs[7], &gregs[15]);
5280 err |= __get_user(env->pc, &gregs[16]);
5281 err |= __get_user(temp, &gregs[17]);
5282 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5284 *pd0 = env->dregs[0];
5285 return err;
5287 badframe:
5288 return 1;
5291 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5292 target_siginfo_t *info,
5293 target_sigset_t *set, CPUM68KState *env)
5295 struct target_rt_sigframe *frame;
5296 abi_ulong frame_addr;
5297 abi_ulong retcode_addr;
5298 abi_ulong info_addr;
5299 abi_ulong uc_addr;
5300 int err = 0;
5301 int i;
5303 frame_addr = get_sigframe(ka, env, sizeof *frame);
5304 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5305 goto give_sigsegv;
5307 err |= __put_user(sig, &frame->sig);
5309 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5310 err |= __put_user(info_addr, &frame->pinfo);
5312 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5313 err |= __put_user(uc_addr, &frame->puc);
5315 err |= copy_siginfo_to_user(&frame->info, info);
5317 /* Create the ucontext */
5319 err |= __put_user(0, &frame->uc.tuc_flags);
5320 err |= __put_user(0, &frame->uc.tuc_link);
5321 err |= __put_user(target_sigaltstack_used.ss_sp,
5322 &frame->uc.tuc_stack.ss_sp);
5323 err |= __put_user(sas_ss_flags(env->aregs[7]),
5324 &frame->uc.tuc_stack.ss_flags);
5325 err |= __put_user(target_sigaltstack_used.ss_size,
5326 &frame->uc.tuc_stack.ss_size);
5327 err |= target_rt_setup_ucontext(&frame->uc, env);
5329 if (err)
5330 goto give_sigsegv;
5332 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5333 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
5334 goto give_sigsegv;
5337 /* Set up to return from userspace. */
5339 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5340 err |= __put_user(retcode_addr, &frame->pretcode);
5342 /* moveq #,d0; notb d0; trap #0 */
5344 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5345 (long *)(frame->retcode + 0));
5346 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5348 if (err)
5349 goto give_sigsegv;
5351 /* Set up to return from userspace */
5353 env->aregs[7] = frame_addr;
5354 env->pc = ka->_sa_handler;
5356 unlock_user_struct(frame, frame_addr, 1);
5357 return;
5359 give_sigsegv:
5360 unlock_user_struct(frame, frame_addr, 1);
5361 force_sig(TARGET_SIGSEGV);
5364 long do_sigreturn(CPUM68KState *env)
5366 struct target_sigframe *frame;
5367 abi_ulong frame_addr = env->aregs[7] - 4;
5368 target_sigset_t target_set;
5369 sigset_t set;
5370 int d0, i;
5372 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5373 goto badframe;
5375 /* set blocked signals */
5377 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5378 goto badframe;
5380 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5381 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5382 goto badframe;
5385 target_to_host_sigset_internal(&set, &target_set);
5386 do_sigprocmask(SIG_SETMASK, &set, NULL);
5388 /* restore registers */
5390 if (restore_sigcontext(env, &frame->sc, &d0))
5391 goto badframe;
5393 unlock_user_struct(frame, frame_addr, 0);
5394 return d0;
5396 badframe:
5397 unlock_user_struct(frame, frame_addr, 0);
5398 force_sig(TARGET_SIGSEGV);
5399 return 0;
5402 long do_rt_sigreturn(CPUM68KState *env)
5404 struct target_rt_sigframe *frame;
5405 abi_ulong frame_addr = env->aregs[7] - 4;
5406 target_sigset_t target_set;
5407 sigset_t set;
5408 int d0;
5410 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5411 goto badframe;
5413 target_to_host_sigset_internal(&set, &target_set);
5414 do_sigprocmask(SIG_SETMASK, &set, NULL);
5416 /* restore registers */
5418 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5419 goto badframe;
5421 if (do_sigaltstack(frame_addr +
5422 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5423 0, get_sp_from_cpustate(env)) == -EFAULT)
5424 goto badframe;
5426 unlock_user_struct(frame, frame_addr, 0);
5427 return d0;
5429 badframe:
5430 unlock_user_struct(frame, frame_addr, 0);
5431 force_sig(TARGET_SIGSEGV);
5432 return 0;
5435 #elif defined(TARGET_ALPHA)
5437 struct target_sigcontext {
5438 abi_long sc_onstack;
5439 abi_long sc_mask;
5440 abi_long sc_pc;
5441 abi_long sc_ps;
5442 abi_long sc_regs[32];
5443 abi_long sc_ownedfp;
5444 abi_long sc_fpregs[32];
5445 abi_ulong sc_fpcr;
5446 abi_ulong sc_fp_control;
5447 abi_ulong sc_reserved1;
5448 abi_ulong sc_reserved2;
5449 abi_ulong sc_ssize;
5450 abi_ulong sc_sbase;
5451 abi_ulong sc_traparg_a0;
5452 abi_ulong sc_traparg_a1;
5453 abi_ulong sc_traparg_a2;
5454 abi_ulong sc_fp_trap_pc;
5455 abi_ulong sc_fp_trigger_sum;
5456 abi_ulong sc_fp_trigger_inst;
5459 struct target_ucontext {
5460 abi_ulong tuc_flags;
5461 abi_ulong tuc_link;
5462 abi_ulong tuc_osf_sigmask;
5463 target_stack_t tuc_stack;
5464 struct target_sigcontext tuc_mcontext;
5465 target_sigset_t tuc_sigmask;
5468 struct target_sigframe {
5469 struct target_sigcontext sc;
5470 unsigned int retcode[3];
5473 struct target_rt_sigframe {
5474 target_siginfo_t info;
5475 struct target_ucontext uc;
5476 unsigned int retcode[3];
5479 #define INSN_MOV_R30_R16 0x47fe0410
5480 #define INSN_LDI_R0 0x201f0000
5481 #define INSN_CALLSYS 0x00000083
5483 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5484 abi_ulong frame_addr, target_sigset_t *set)
5486 int i, err = 0;
5488 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5489 err |= __put_user(set->sig[0], &sc->sc_mask);
5490 err |= __put_user(env->pc, &sc->sc_pc);
5491 err |= __put_user(8, &sc->sc_ps);
5493 for (i = 0; i < 31; ++i) {
5494 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5496 err |= __put_user(0, &sc->sc_regs[31]);
5498 for (i = 0; i < 31; ++i) {
5499 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5501 err |= __put_user(0, &sc->sc_fpregs[31]);
5502 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5504 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5505 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5506 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5508 return err;
5511 static int restore_sigcontext(CPUAlphaState *env,
5512 struct target_sigcontext *sc)
5514 uint64_t fpcr;
5515 int i, err = 0;
5517 err |= __get_user(env->pc, &sc->sc_pc);
5519 for (i = 0; i < 31; ++i) {
5520 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5522 for (i = 0; i < 31; ++i) {
5523 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5526 err |= __get_user(fpcr, &sc->sc_fpcr);
5527 cpu_alpha_store_fpcr(env, fpcr);
5529 return err;
5532 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5533 CPUAlphaState *env,
5534 unsigned long framesize)
5536 abi_ulong sp = env->ir[IR_SP];
5538 /* This is the X/Open sanctioned signal stack switching. */
5539 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5540 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5542 return (sp - framesize) & -32;
5545 static void setup_frame(int sig, struct target_sigaction *ka,
5546 target_sigset_t *set, CPUAlphaState *env)
5548 abi_ulong frame_addr, r26;
5549 struct target_sigframe *frame;
5550 int err = 0;
5552 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5553 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5554 goto give_sigsegv;
5557 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5559 if (ka->sa_restorer) {
5560 r26 = ka->sa_restorer;
5561 } else {
5562 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5563 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5564 &frame->retcode[1]);
5565 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5566 /* imb() */
5567 r26 = frame_addr;
5570 unlock_user_struct(frame, frame_addr, 1);
5572 if (err) {
5573 give_sigsegv:
5574 if (sig == TARGET_SIGSEGV) {
5575 ka->_sa_handler = TARGET_SIG_DFL;
5577 force_sig(TARGET_SIGSEGV);
5580 env->ir[IR_RA] = r26;
5581 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5582 env->ir[IR_A0] = sig;
5583 env->ir[IR_A1] = 0;
5584 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5585 env->ir[IR_SP] = frame_addr;
5588 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5589 target_siginfo_t *info,
5590 target_sigset_t *set, CPUAlphaState *env)
5592 abi_ulong frame_addr, r26;
5593 struct target_rt_sigframe *frame;
5594 int i, err = 0;
5596 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5597 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5598 goto give_sigsegv;
5601 err |= copy_siginfo_to_user(&frame->info, info);
5603 err |= __put_user(0, &frame->uc.tuc_flags);
5604 err |= __put_user(0, &frame->uc.tuc_link);
5605 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5606 err |= __put_user(target_sigaltstack_used.ss_sp,
5607 &frame->uc.tuc_stack.ss_sp);
5608 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5609 &frame->uc.tuc_stack.ss_flags);
5610 err |= __put_user(target_sigaltstack_used.ss_size,
5611 &frame->uc.tuc_stack.ss_size);
5612 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5613 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5614 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5617 if (ka->sa_restorer) {
5618 r26 = ka->sa_restorer;
5619 } else {
5620 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5621 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5622 &frame->retcode[1]);
5623 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5624 /* imb(); */
5625 r26 = frame_addr;
5628 if (err) {
5629 give_sigsegv:
5630 if (sig == TARGET_SIGSEGV) {
5631 ka->_sa_handler = TARGET_SIG_DFL;
5633 force_sig(TARGET_SIGSEGV);
5636 env->ir[IR_RA] = r26;
5637 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5638 env->ir[IR_A0] = sig;
5639 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5640 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5641 env->ir[IR_SP] = frame_addr;
5644 long do_sigreturn(CPUAlphaState *env)
5646 struct target_sigcontext *sc;
5647 abi_ulong sc_addr = env->ir[IR_A0];
5648 target_sigset_t target_set;
5649 sigset_t set;
5651 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5652 goto badframe;
5655 target_sigemptyset(&target_set);
5656 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5657 goto badframe;
5660 target_to_host_sigset_internal(&set, &target_set);
5661 do_sigprocmask(SIG_SETMASK, &set, NULL);
5663 if (restore_sigcontext(env, sc)) {
5664 goto badframe;
5666 unlock_user_struct(sc, sc_addr, 0);
5667 return env->ir[IR_V0];
5669 badframe:
5670 unlock_user_struct(sc, sc_addr, 0);
5671 force_sig(TARGET_SIGSEGV);
5674 long do_rt_sigreturn(CPUAlphaState *env)
5676 abi_ulong frame_addr = env->ir[IR_A0];
5677 struct target_rt_sigframe *frame;
5678 sigset_t set;
5680 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5681 goto badframe;
5683 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5684 do_sigprocmask(SIG_SETMASK, &set, NULL);
5686 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5687 goto badframe;
5689 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5690 uc.tuc_stack),
5691 0, env->ir[IR_SP]) == -EFAULT) {
5692 goto badframe;
5695 unlock_user_struct(frame, frame_addr, 0);
5696 return env->ir[IR_V0];
5699 badframe:
5700 unlock_user_struct(frame, frame_addr, 0);
5701 force_sig(TARGET_SIGSEGV);
5704 #else
5706 static void setup_frame(int sig, struct target_sigaction *ka,
5707 target_sigset_t *set, CPUArchState *env)
5709 fprintf(stderr, "setup_frame: not implemented\n");
5712 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5713 target_siginfo_t *info,
5714 target_sigset_t *set, CPUArchState *env)
5716 fprintf(stderr, "setup_rt_frame: not implemented\n");
5719 long do_sigreturn(CPUArchState *env)
5721 fprintf(stderr, "do_sigreturn: not implemented\n");
5722 return -TARGET_ENOSYS;
5725 long do_rt_sigreturn(CPUArchState *env)
5727 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5728 return -TARGET_ENOSYS;
5731 #endif
5733 void process_pending_signals(CPUArchState *cpu_env)
5735 CPUState *cpu = ENV_GET_CPU(cpu_env);
5736 int sig;
5737 abi_ulong handler;
5738 sigset_t set, old_set;
5739 target_sigset_t target_old_set;
5740 struct emulated_sigtable *k;
5741 struct target_sigaction *sa;
5742 struct sigqueue *q;
5743 TaskState *ts = cpu->opaque;
5745 if (!ts->signal_pending)
5746 return;
5748 /* FIXME: This is not threadsafe. */
5749 k = ts->sigtab;
5750 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5751 if (k->pending)
5752 goto handle_signal;
5753 k++;
5755 /* if no signal is pending, just return */
5756 ts->signal_pending = 0;
5757 return;
5759 handle_signal:
5760 #ifdef DEBUG_SIGNAL
5761 fprintf(stderr, "qemu: process signal %d\n", sig);
5762 #endif
5763 /* dequeue signal */
5764 q = k->first;
5765 k->first = q->next;
5766 if (!k->first)
5767 k->pending = 0;
5769 sig = gdb_handlesig(cpu, sig);
5770 if (!sig) {
5771 sa = NULL;
5772 handler = TARGET_SIG_IGN;
5773 } else {
5774 sa = &sigact_table[sig - 1];
5775 handler = sa->_sa_handler;
5778 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5779 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5780 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5781 * because it got a real MMU fault), and treat as if default handler.
5783 handler = TARGET_SIG_DFL;
5786 if (handler == TARGET_SIG_DFL) {
5787 /* default handler : ignore some signal. The other are job control or fatal */
5788 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5789 kill(getpid(),SIGSTOP);
5790 } else if (sig != TARGET_SIGCHLD &&
5791 sig != TARGET_SIGURG &&
5792 sig != TARGET_SIGWINCH &&
5793 sig != TARGET_SIGCONT) {
5794 force_sig(sig);
5796 } else if (handler == TARGET_SIG_IGN) {
5797 /* ignore sig */
5798 } else if (handler == TARGET_SIG_ERR) {
5799 force_sig(sig);
5800 } else {
5801 /* compute the blocked signals during the handler execution */
5802 target_to_host_sigset(&set, &sa->sa_mask);
5803 /* SA_NODEFER indicates that the current signal should not be
5804 blocked during the handler */
5805 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5806 sigaddset(&set, target_to_host_signal(sig));
5808 /* block signals in the handler using Linux */
5809 do_sigprocmask(SIG_BLOCK, &set, &old_set);
5810 /* save the previous blocked signal state to restore it at the
5811 end of the signal execution (see do_sigreturn) */
5812 host_to_target_sigset_internal(&target_old_set, &old_set);
5814 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5815 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5817 CPUX86State *env = cpu_env;
5818 if (env->eflags & VM_MASK)
5819 save_v86_state(env);
5821 #endif
5822 /* prepare the stack frame of the virtual CPU */
5823 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5824 /* These targets do not have traditional signals. */
5825 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5826 #else
5827 if (sa->sa_flags & TARGET_SA_SIGINFO)
5828 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5829 else
5830 setup_frame(sig, sa, &target_old_set, cpu_env);
5831 #endif
5832 if (sa->sa_flags & TARGET_SA_RESETHAND)
5833 sa->_sa_handler = TARGET_SIG_DFL;
5835 if (q != &k->info)
5836 free_sigqueue(cpu_env, q);