gtk: Fix -serial vc
[qemu/rayw.git] / linux-user / signal.c
blob7d6246f4480a1f173bc30e73834b82b8fd4a1637
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(/*current->thread.fault_address*/ 0,
1246 &sf->uc.tuc_mcontext.fault_address);
1248 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1249 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1252 for (i = 0; i < 32; i++) {
1253 #ifdef TARGET_WORDS_BIGENDIAN
1254 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1255 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1256 #else
1257 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1258 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1259 #endif
1261 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1262 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1263 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1264 __put_user(sizeof(struct target_fpsimd_context),
1265 &aux->fpsimd.head.size);
1267 /* set the "end" magic */
1268 __put_user(0, &aux->end.magic);
1269 __put_user(0, &aux->end.size);
1271 return 0;
1274 static int target_restore_sigframe(CPUARMState *env,
1275 struct target_rt_sigframe *sf)
1277 sigset_t set;
1278 int i;
1279 struct target_aux_context *aux =
1280 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1281 uint32_t magic, size, fpsr, fpcr;
1282 uint64_t pstate;
1284 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1285 do_sigprocmask(SIG_SETMASK, &set, NULL);
1287 for (i = 0; i < 31; i++) {
1288 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1291 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1292 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1293 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1294 pstate_write(env, pstate);
1296 __get_user(magic, &aux->fpsimd.head.magic);
1297 __get_user(size, &aux->fpsimd.head.size);
1299 if (magic != TARGET_FPSIMD_MAGIC
1300 || size != sizeof(struct target_fpsimd_context)) {
1301 return 1;
1304 for (i = 0; i < 32; i++) {
1305 #ifdef TARGET_WORDS_BIGENDIAN
1306 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1307 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1308 #else
1309 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1310 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1311 #endif
1313 __get_user(fpsr, &aux->fpsimd.fpsr);
1314 vfp_set_fpsr(env, fpsr);
1315 __get_user(fpcr, &aux->fpsimd.fpcr);
1316 vfp_set_fpcr(env, fpcr);
1318 return 0;
1321 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1323 abi_ulong sp;
1325 sp = env->xregs[31];
1328 * This is the X/Open sanctioned signal stack switching.
1330 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
1331 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1334 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1336 return sp;
1339 static void target_setup_frame(int usig, struct target_sigaction *ka,
1340 target_siginfo_t *info, target_sigset_t *set,
1341 CPUARMState *env)
1343 struct target_rt_sigframe *frame;
1344 abi_ulong frame_addr, return_addr;
1346 frame_addr = get_sigframe(ka, env);
1347 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1348 goto give_sigsegv;
1351 __put_user(0, &frame->uc.tuc_flags);
1352 __put_user(0, &frame->uc.tuc_link);
1354 __put_user(target_sigaltstack_used.ss_sp,
1355 &frame->uc.tuc_stack.ss_sp);
1356 __put_user(sas_ss_flags(env->xregs[31]),
1357 &frame->uc.tuc_stack.ss_flags);
1358 __put_user(target_sigaltstack_used.ss_size,
1359 &frame->uc.tuc_stack.ss_size);
1360 target_setup_sigframe(frame, env, set);
1361 if (ka->sa_flags & TARGET_SA_RESTORER) {
1362 return_addr = ka->sa_restorer;
1363 } else {
1364 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1365 __put_user(0xd2801168, &frame->tramp[0]);
1366 __put_user(0xd4000001, &frame->tramp[1]);
1367 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1369 env->xregs[0] = usig;
1370 env->xregs[31] = frame_addr;
1371 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1372 env->pc = ka->_sa_handler;
1373 env->xregs[30] = return_addr;
1374 if (info) {
1375 if (copy_siginfo_to_user(&frame->info, info)) {
1376 goto give_sigsegv;
1378 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1379 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1382 unlock_user_struct(frame, frame_addr, 1);
1383 return;
1385 give_sigsegv:
1386 unlock_user_struct(frame, frame_addr, 1);
1387 force_sig(TARGET_SIGSEGV);
1390 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1391 target_siginfo_t *info, target_sigset_t *set,
1392 CPUARMState *env)
1394 target_setup_frame(sig, ka, info, set, env);
1397 static void setup_frame(int sig, struct target_sigaction *ka,
1398 target_sigset_t *set, CPUARMState *env)
1400 target_setup_frame(sig, ka, 0, set, env);
1403 long do_rt_sigreturn(CPUARMState *env)
1405 struct target_rt_sigframe *frame = NULL;
1406 abi_ulong frame_addr = env->xregs[31];
1408 if (frame_addr & 15) {
1409 goto badframe;
1412 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1413 goto badframe;
1416 if (target_restore_sigframe(env, frame)) {
1417 goto badframe;
1420 if (do_sigaltstack(frame_addr +
1421 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1422 0, get_sp_from_cpustate(env)) == -EFAULT) {
1423 goto badframe;
1426 unlock_user_struct(frame, frame_addr, 0);
1427 return env->xregs[0];
1429 badframe:
1430 unlock_user_struct(frame, frame_addr, 0);
1431 force_sig(TARGET_SIGSEGV);
1432 return 0;
1435 long do_sigreturn(CPUARMState *env)
1437 return do_rt_sigreturn(env);
1440 #elif defined(TARGET_ARM)
1442 struct target_sigcontext {
1443 abi_ulong trap_no;
1444 abi_ulong error_code;
1445 abi_ulong oldmask;
1446 abi_ulong arm_r0;
1447 abi_ulong arm_r1;
1448 abi_ulong arm_r2;
1449 abi_ulong arm_r3;
1450 abi_ulong arm_r4;
1451 abi_ulong arm_r5;
1452 abi_ulong arm_r6;
1453 abi_ulong arm_r7;
1454 abi_ulong arm_r8;
1455 abi_ulong arm_r9;
1456 abi_ulong arm_r10;
1457 abi_ulong arm_fp;
1458 abi_ulong arm_ip;
1459 abi_ulong arm_sp;
1460 abi_ulong arm_lr;
1461 abi_ulong arm_pc;
1462 abi_ulong arm_cpsr;
1463 abi_ulong fault_address;
1466 struct target_ucontext_v1 {
1467 abi_ulong tuc_flags;
1468 abi_ulong tuc_link;
1469 target_stack_t tuc_stack;
1470 struct target_sigcontext tuc_mcontext;
1471 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1474 struct target_ucontext_v2 {
1475 abi_ulong tuc_flags;
1476 abi_ulong tuc_link;
1477 target_stack_t tuc_stack;
1478 struct target_sigcontext tuc_mcontext;
1479 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1480 char __unused[128 - sizeof(target_sigset_t)];
1481 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1484 struct target_user_vfp {
1485 uint64_t fpregs[32];
1486 abi_ulong fpscr;
1489 struct target_user_vfp_exc {
1490 abi_ulong fpexc;
1491 abi_ulong fpinst;
1492 abi_ulong fpinst2;
1495 struct target_vfp_sigframe {
1496 abi_ulong magic;
1497 abi_ulong size;
1498 struct target_user_vfp ufp;
1499 struct target_user_vfp_exc ufp_exc;
1500 } __attribute__((__aligned__(8)));
1502 struct target_iwmmxt_sigframe {
1503 abi_ulong magic;
1504 abi_ulong size;
1505 uint64_t regs[16];
1506 /* Note that not all the coprocessor control registers are stored here */
1507 uint32_t wcssf;
1508 uint32_t wcasf;
1509 uint32_t wcgr0;
1510 uint32_t wcgr1;
1511 uint32_t wcgr2;
1512 uint32_t wcgr3;
1513 } __attribute__((__aligned__(8)));
1515 #define TARGET_VFP_MAGIC 0x56465001
1516 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1518 struct sigframe_v1
1520 struct target_sigcontext sc;
1521 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1522 abi_ulong retcode;
1525 struct sigframe_v2
1527 struct target_ucontext_v2 uc;
1528 abi_ulong retcode;
1531 struct rt_sigframe_v1
1533 abi_ulong pinfo;
1534 abi_ulong puc;
1535 struct target_siginfo info;
1536 struct target_ucontext_v1 uc;
1537 abi_ulong retcode;
1540 struct rt_sigframe_v2
1542 struct target_siginfo info;
1543 struct target_ucontext_v2 uc;
1544 abi_ulong retcode;
1547 #define TARGET_CONFIG_CPU_32 1
1550 * For ARM syscalls, we encode the syscall number into the instruction.
1552 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1553 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1556 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1557 * need two 16-bit instructions.
1559 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1560 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1562 static const abi_ulong retcodes[4] = {
1563 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1564 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1568 #define __get_user_error(x,p,e) __get_user(x, p)
1570 static inline int valid_user_regs(CPUARMState *regs)
1572 return 1;
1575 static void
1576 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1577 CPUARMState *env, abi_ulong mask)
1579 __put_user(env->regs[0], &sc->arm_r0);
1580 __put_user(env->regs[1], &sc->arm_r1);
1581 __put_user(env->regs[2], &sc->arm_r2);
1582 __put_user(env->regs[3], &sc->arm_r3);
1583 __put_user(env->regs[4], &sc->arm_r4);
1584 __put_user(env->regs[5], &sc->arm_r5);
1585 __put_user(env->regs[6], &sc->arm_r6);
1586 __put_user(env->regs[7], &sc->arm_r7);
1587 __put_user(env->regs[8], &sc->arm_r8);
1588 __put_user(env->regs[9], &sc->arm_r9);
1589 __put_user(env->regs[10], &sc->arm_r10);
1590 __put_user(env->regs[11], &sc->arm_fp);
1591 __put_user(env->regs[12], &sc->arm_ip);
1592 __put_user(env->regs[13], &sc->arm_sp);
1593 __put_user(env->regs[14], &sc->arm_lr);
1594 __put_user(env->regs[15], &sc->arm_pc);
1595 #ifdef TARGET_CONFIG_CPU_32
1596 __put_user(cpsr_read(env), &sc->arm_cpsr);
1597 #endif
1599 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1600 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1601 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1602 __put_user(mask, &sc->oldmask);
1605 static inline abi_ulong
1606 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1608 unsigned long sp = regs->regs[13];
1611 * This is the X/Open sanctioned signal stack switching.
1613 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1614 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1616 * ATPCS B01 mandates 8-byte alignment
1618 return (sp - framesize) & ~7;
1621 static int
1622 setup_return(CPUARMState *env, struct target_sigaction *ka,
1623 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1625 abi_ulong handler = ka->_sa_handler;
1626 abi_ulong retcode;
1627 int thumb = handler & 1;
1628 uint32_t cpsr = cpsr_read(env);
1630 cpsr &= ~CPSR_IT;
1631 if (thumb) {
1632 cpsr |= CPSR_T;
1633 } else {
1634 cpsr &= ~CPSR_T;
1637 if (ka->sa_flags & TARGET_SA_RESTORER) {
1638 retcode = ka->sa_restorer;
1639 } else {
1640 unsigned int idx = thumb;
1642 if (ka->sa_flags & TARGET_SA_SIGINFO)
1643 idx += 2;
1645 if (__put_user(retcodes[idx], rc))
1646 return 1;
1648 retcode = rc_addr + thumb;
1651 env->regs[0] = usig;
1652 env->regs[13] = frame_addr;
1653 env->regs[14] = retcode;
1654 env->regs[15] = handler & (thumb ? ~1 : ~3);
1655 cpsr_write(env, cpsr, 0xffffffff);
1657 return 0;
1660 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1662 int i;
1663 struct target_vfp_sigframe *vfpframe;
1664 vfpframe = (struct target_vfp_sigframe *)regspace;
1665 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1666 __put_user(sizeof(*vfpframe), &vfpframe->size);
1667 for (i = 0; i < 32; i++) {
1668 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1670 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1671 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1672 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1673 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1674 return (abi_ulong*)(vfpframe+1);
1677 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1678 CPUARMState *env)
1680 int i;
1681 struct target_iwmmxt_sigframe *iwmmxtframe;
1682 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1683 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1684 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1685 for (i = 0; i < 16; i++) {
1686 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1688 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1689 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1690 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1691 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1692 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1693 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1694 return (abi_ulong*)(iwmmxtframe+1);
1697 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1698 target_sigset_t *set, CPUARMState *env)
1700 struct target_sigaltstack stack;
1701 int i;
1702 abi_ulong *regspace;
1704 /* Clear all the bits of the ucontext we don't use. */
1705 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1707 memset(&stack, 0, sizeof(stack));
1708 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1709 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1710 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1711 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1713 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1714 /* Save coprocessor signal frame. */
1715 regspace = uc->tuc_regspace;
1716 if (arm_feature(env, ARM_FEATURE_VFP)) {
1717 regspace = setup_sigframe_v2_vfp(regspace, env);
1719 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1720 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1723 /* Write terminating magic word */
1724 __put_user(0, regspace);
1726 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1727 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1731 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1732 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1733 target_sigset_t *set, CPUARMState *regs)
1735 struct sigframe_v1 *frame;
1736 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1737 int i;
1739 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1740 return;
1742 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1744 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1745 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1746 goto end;
1749 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1750 frame_addr + offsetof(struct sigframe_v1, retcode));
1752 end:
1753 unlock_user_struct(frame, frame_addr, 1);
1756 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1757 target_sigset_t *set, CPUARMState *regs)
1759 struct sigframe_v2 *frame;
1760 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1762 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1763 return;
1765 setup_sigframe_v2(&frame->uc, set, regs);
1767 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1768 frame_addr + offsetof(struct sigframe_v2, retcode));
1770 unlock_user_struct(frame, frame_addr, 1);
1773 static void setup_frame(int usig, struct target_sigaction *ka,
1774 target_sigset_t *set, CPUARMState *regs)
1776 if (get_osversion() >= 0x020612) {
1777 setup_frame_v2(usig, ka, set, regs);
1778 } else {
1779 setup_frame_v1(usig, ka, set, regs);
1783 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1784 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1785 target_siginfo_t *info,
1786 target_sigset_t *set, CPUARMState *env)
1788 struct rt_sigframe_v1 *frame;
1789 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1790 struct target_sigaltstack stack;
1791 int i;
1792 abi_ulong info_addr, uc_addr;
1794 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1795 return /* 1 */;
1797 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1798 __put_user(info_addr, &frame->pinfo);
1799 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1800 __put_user(uc_addr, &frame->puc);
1801 copy_siginfo_to_user(&frame->info, info);
1803 /* Clear all the bits of the ucontext we don't use. */
1804 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1806 memset(&stack, 0, sizeof(stack));
1807 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1808 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1809 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1810 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1812 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1813 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1814 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1815 goto end;
1818 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1819 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1821 env->regs[1] = info_addr;
1822 env->regs[2] = uc_addr;
1824 end:
1825 unlock_user_struct(frame, frame_addr, 1);
1828 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1829 target_siginfo_t *info,
1830 target_sigset_t *set, CPUARMState *env)
1832 struct rt_sigframe_v2 *frame;
1833 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1834 abi_ulong info_addr, uc_addr;
1836 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1837 return /* 1 */;
1839 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1840 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1841 copy_siginfo_to_user(&frame->info, info);
1843 setup_sigframe_v2(&frame->uc, set, env);
1845 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1846 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1848 env->regs[1] = info_addr;
1849 env->regs[2] = uc_addr;
1851 unlock_user_struct(frame, frame_addr, 1);
1854 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1855 target_siginfo_t *info,
1856 target_sigset_t *set, CPUARMState *env)
1858 if (get_osversion() >= 0x020612) {
1859 setup_rt_frame_v2(usig, ka, info, set, env);
1860 } else {
1861 setup_rt_frame_v1(usig, ka, info, set, env);
1865 static int
1866 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1868 int err = 0;
1869 uint32_t cpsr;
1871 __get_user_error(env->regs[0], &sc->arm_r0, err);
1872 __get_user_error(env->regs[1], &sc->arm_r1, err);
1873 __get_user_error(env->regs[2], &sc->arm_r2, err);
1874 __get_user_error(env->regs[3], &sc->arm_r3, err);
1875 __get_user_error(env->regs[4], &sc->arm_r4, err);
1876 __get_user_error(env->regs[5], &sc->arm_r5, err);
1877 __get_user_error(env->regs[6], &sc->arm_r6, err);
1878 __get_user_error(env->regs[7], &sc->arm_r7, err);
1879 __get_user_error(env->regs[8], &sc->arm_r8, err);
1880 __get_user_error(env->regs[9], &sc->arm_r9, err);
1881 __get_user_error(env->regs[10], &sc->arm_r10, err);
1882 __get_user_error(env->regs[11], &sc->arm_fp, err);
1883 __get_user_error(env->regs[12], &sc->arm_ip, err);
1884 __get_user_error(env->regs[13], &sc->arm_sp, err);
1885 __get_user_error(env->regs[14], &sc->arm_lr, err);
1886 __get_user_error(env->regs[15], &sc->arm_pc, err);
1887 #ifdef TARGET_CONFIG_CPU_32
1888 __get_user_error(cpsr, &sc->arm_cpsr, err);
1889 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1890 #endif
1892 err |= !valid_user_regs(env);
1894 return err;
1897 static long do_sigreturn_v1(CPUARMState *env)
1899 abi_ulong frame_addr;
1900 struct sigframe_v1 *frame = NULL;
1901 target_sigset_t set;
1902 sigset_t host_set;
1903 int i;
1906 * Since we stacked the signal on a 64-bit boundary,
1907 * then 'sp' should be word aligned here. If it's
1908 * not, then the user is trying to mess with us.
1910 frame_addr = env->regs[13];
1911 if (frame_addr & 7) {
1912 goto badframe;
1915 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1916 goto badframe;
1918 if (__get_user(set.sig[0], &frame->sc.oldmask))
1919 goto badframe;
1920 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1921 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1922 goto badframe;
1925 target_to_host_sigset_internal(&host_set, &set);
1926 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1928 if (restore_sigcontext(env, &frame->sc))
1929 goto badframe;
1931 #if 0
1932 /* Send SIGTRAP if we're single-stepping */
1933 if (ptrace_cancel_bpt(current))
1934 send_sig(SIGTRAP, current, 1);
1935 #endif
1936 unlock_user_struct(frame, frame_addr, 0);
1937 return env->regs[0];
1939 badframe:
1940 unlock_user_struct(frame, frame_addr, 0);
1941 force_sig(TARGET_SIGSEGV /* , current */);
1942 return 0;
1945 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1947 int i;
1948 abi_ulong magic, sz;
1949 uint32_t fpscr, fpexc;
1950 struct target_vfp_sigframe *vfpframe;
1951 vfpframe = (struct target_vfp_sigframe *)regspace;
1953 __get_user(magic, &vfpframe->magic);
1954 __get_user(sz, &vfpframe->size);
1955 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1956 return 0;
1958 for (i = 0; i < 32; i++) {
1959 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1961 __get_user(fpscr, &vfpframe->ufp.fpscr);
1962 vfp_set_fpscr(env, fpscr);
1963 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1964 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1965 * and the exception flag is cleared
1967 fpexc |= (1 << 30);
1968 fpexc &= ~((1 << 31) | (1 << 28));
1969 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1970 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1971 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1972 return (abi_ulong*)(vfpframe + 1);
1975 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1976 abi_ulong *regspace)
1978 int i;
1979 abi_ulong magic, sz;
1980 struct target_iwmmxt_sigframe *iwmmxtframe;
1981 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1983 __get_user(magic, &iwmmxtframe->magic);
1984 __get_user(sz, &iwmmxtframe->size);
1985 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1986 return 0;
1988 for (i = 0; i < 16; i++) {
1989 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1991 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1992 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1993 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1994 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1995 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1996 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1997 return (abi_ulong*)(iwmmxtframe + 1);
2000 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
2001 struct target_ucontext_v2 *uc)
2003 sigset_t host_set;
2004 abi_ulong *regspace;
2006 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
2007 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2009 if (restore_sigcontext(env, &uc->tuc_mcontext))
2010 return 1;
2012 /* Restore coprocessor signal frame */
2013 regspace = uc->tuc_regspace;
2014 if (arm_feature(env, ARM_FEATURE_VFP)) {
2015 regspace = restore_sigframe_v2_vfp(env, regspace);
2016 if (!regspace) {
2017 return 1;
2020 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2021 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
2022 if (!regspace) {
2023 return 1;
2027 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2028 return 1;
2030 #if 0
2031 /* Send SIGTRAP if we're single-stepping */
2032 if (ptrace_cancel_bpt(current))
2033 send_sig(SIGTRAP, current, 1);
2034 #endif
2036 return 0;
2039 static long do_sigreturn_v2(CPUARMState *env)
2041 abi_ulong frame_addr;
2042 struct sigframe_v2 *frame = NULL;
2045 * Since we stacked the signal on a 64-bit boundary,
2046 * then 'sp' should be word aligned here. If it's
2047 * not, then the user is trying to mess with us.
2049 frame_addr = env->regs[13];
2050 if (frame_addr & 7) {
2051 goto badframe;
2054 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2055 goto badframe;
2057 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2058 goto badframe;
2060 unlock_user_struct(frame, frame_addr, 0);
2061 return env->regs[0];
2063 badframe:
2064 unlock_user_struct(frame, frame_addr, 0);
2065 force_sig(TARGET_SIGSEGV /* , current */);
2066 return 0;
2069 long do_sigreturn(CPUARMState *env)
2071 if (get_osversion() >= 0x020612) {
2072 return do_sigreturn_v2(env);
2073 } else {
2074 return do_sigreturn_v1(env);
2078 static long do_rt_sigreturn_v1(CPUARMState *env)
2080 abi_ulong frame_addr;
2081 struct rt_sigframe_v1 *frame = NULL;
2082 sigset_t host_set;
2085 * Since we stacked the signal on a 64-bit boundary,
2086 * then 'sp' should be word aligned here. If it's
2087 * not, then the user is trying to mess with us.
2089 frame_addr = env->regs[13];
2090 if (frame_addr & 7) {
2091 goto badframe;
2094 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2095 goto badframe;
2097 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2098 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2100 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
2101 goto badframe;
2103 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2104 goto badframe;
2106 #if 0
2107 /* Send SIGTRAP if we're single-stepping */
2108 if (ptrace_cancel_bpt(current))
2109 send_sig(SIGTRAP, current, 1);
2110 #endif
2111 unlock_user_struct(frame, frame_addr, 0);
2112 return env->regs[0];
2114 badframe:
2115 unlock_user_struct(frame, frame_addr, 0);
2116 force_sig(TARGET_SIGSEGV /* , current */);
2117 return 0;
2120 static long do_rt_sigreturn_v2(CPUARMState *env)
2122 abi_ulong frame_addr;
2123 struct rt_sigframe_v2 *frame = NULL;
2126 * Since we stacked the signal on a 64-bit boundary,
2127 * then 'sp' should be word aligned here. If it's
2128 * not, then the user is trying to mess with us.
2130 frame_addr = env->regs[13];
2131 if (frame_addr & 7) {
2132 goto badframe;
2135 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2136 goto badframe;
2138 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2139 goto badframe;
2141 unlock_user_struct(frame, frame_addr, 0);
2142 return env->regs[0];
2144 badframe:
2145 unlock_user_struct(frame, frame_addr, 0);
2146 force_sig(TARGET_SIGSEGV /* , current */);
2147 return 0;
2150 long do_rt_sigreturn(CPUARMState *env)
2152 if (get_osversion() >= 0x020612) {
2153 return do_rt_sigreturn_v2(env);
2154 } else {
2155 return do_rt_sigreturn_v1(env);
2159 #elif defined(TARGET_SPARC)
2161 #define __SUNOS_MAXWIN 31
2163 /* This is what SunOS does, so shall I. */
2164 struct target_sigcontext {
2165 abi_ulong sigc_onstack; /* state to restore */
2167 abi_ulong sigc_mask; /* sigmask to restore */
2168 abi_ulong sigc_sp; /* stack pointer */
2169 abi_ulong sigc_pc; /* program counter */
2170 abi_ulong sigc_npc; /* next program counter */
2171 abi_ulong sigc_psr; /* for condition codes etc */
2172 abi_ulong sigc_g1; /* User uses these two registers */
2173 abi_ulong sigc_o0; /* within the trampoline code. */
2175 /* Now comes information regarding the users window set
2176 * at the time of the signal.
2178 abi_ulong sigc_oswins; /* outstanding windows */
2180 /* stack ptrs for each regwin buf */
2181 char *sigc_spbuf[__SUNOS_MAXWIN];
2183 /* Windows to restore after signal */
2184 struct {
2185 abi_ulong locals[8];
2186 abi_ulong ins[8];
2187 } sigc_wbuf[__SUNOS_MAXWIN];
2189 /* A Sparc stack frame */
2190 struct sparc_stackf {
2191 abi_ulong locals[8];
2192 abi_ulong ins[8];
2193 /* It's simpler to treat fp and callers_pc as elements of ins[]
2194 * since we never need to access them ourselves.
2196 char *structptr;
2197 abi_ulong xargs[6];
2198 abi_ulong xxargs[1];
2201 typedef struct {
2202 struct {
2203 abi_ulong psr;
2204 abi_ulong pc;
2205 abi_ulong npc;
2206 abi_ulong y;
2207 abi_ulong u_regs[16]; /* globals and ins */
2208 } si_regs;
2209 int si_mask;
2210 } __siginfo_t;
2212 typedef struct {
2213 abi_ulong si_float_regs[32];
2214 unsigned long si_fsr;
2215 unsigned long si_fpqdepth;
2216 struct {
2217 unsigned long *insn_addr;
2218 unsigned long insn;
2219 } si_fpqueue [16];
2220 } qemu_siginfo_fpu_t;
2223 struct target_signal_frame {
2224 struct sparc_stackf ss;
2225 __siginfo_t info;
2226 abi_ulong fpu_save;
2227 abi_ulong insns[2] __attribute__ ((aligned (8)));
2228 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2229 abi_ulong extra_size; /* Should be 0 */
2230 qemu_siginfo_fpu_t fpu_state;
2232 struct target_rt_signal_frame {
2233 struct sparc_stackf ss;
2234 siginfo_t info;
2235 abi_ulong regs[20];
2236 sigset_t mask;
2237 abi_ulong fpu_save;
2238 unsigned int insns[2];
2239 stack_t stack;
2240 unsigned int extra_size; /* Should be 0 */
2241 qemu_siginfo_fpu_t fpu_state;
2244 #define UREG_O0 16
2245 #define UREG_O6 22
2246 #define UREG_I0 0
2247 #define UREG_I1 1
2248 #define UREG_I2 2
2249 #define UREG_I3 3
2250 #define UREG_I4 4
2251 #define UREG_I5 5
2252 #define UREG_I6 6
2253 #define UREG_I7 7
2254 #define UREG_L0 8
2255 #define UREG_FP UREG_I6
2256 #define UREG_SP UREG_O6
2258 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2259 CPUSPARCState *env,
2260 unsigned long framesize)
2262 abi_ulong sp;
2264 sp = env->regwptr[UREG_FP];
2266 /* This is the X/Open sanctioned signal stack switching. */
2267 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2268 if (!on_sig_stack(sp)
2269 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2270 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2272 return sp - framesize;
2275 static int
2276 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2278 int err = 0, i;
2280 err |= __put_user(env->psr, &si->si_regs.psr);
2281 err |= __put_user(env->pc, &si->si_regs.pc);
2282 err |= __put_user(env->npc, &si->si_regs.npc);
2283 err |= __put_user(env->y, &si->si_regs.y);
2284 for (i=0; i < 8; i++) {
2285 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2287 for (i=0; i < 8; i++) {
2288 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2290 err |= __put_user(mask, &si->si_mask);
2291 return err;
2294 #if 0
2295 static int
2296 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2297 CPUSPARCState *env, unsigned long mask)
2299 int err = 0;
2301 err |= __put_user(mask, &sc->sigc_mask);
2302 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2303 err |= __put_user(env->pc, &sc->sigc_pc);
2304 err |= __put_user(env->npc, &sc->sigc_npc);
2305 err |= __put_user(env->psr, &sc->sigc_psr);
2306 err |= __put_user(env->gregs[1], &sc->sigc_g1);
2307 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2309 return err;
2311 #endif
2312 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2314 static void setup_frame(int sig, struct target_sigaction *ka,
2315 target_sigset_t *set, CPUSPARCState *env)
2317 abi_ulong sf_addr;
2318 struct target_signal_frame *sf;
2319 int sigframe_size, err, i;
2321 /* 1. Make sure everything is clean */
2322 //synchronize_user_stack();
2324 sigframe_size = NF_ALIGNEDSZ;
2325 sf_addr = get_sigframe(ka, env, sigframe_size);
2327 sf = lock_user(VERIFY_WRITE, sf_addr,
2328 sizeof(struct target_signal_frame), 0);
2329 if (!sf)
2330 goto sigsegv;
2332 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2333 #if 0
2334 if (invalid_frame_pointer(sf, sigframe_size))
2335 goto sigill_and_return;
2336 #endif
2337 /* 2. Save the current process state */
2338 err = setup___siginfo(&sf->info, env, set->sig[0]);
2339 err |= __put_user(0, &sf->extra_size);
2341 //err |= save_fpu_state(regs, &sf->fpu_state);
2342 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
2344 err |= __put_user(set->sig[0], &sf->info.si_mask);
2345 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2346 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
2349 for (i = 0; i < 8; i++) {
2350 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2352 for (i = 0; i < 8; i++) {
2353 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2355 if (err)
2356 goto sigsegv;
2358 /* 3. signal handler back-trampoline and parameters */
2359 env->regwptr[UREG_FP] = sf_addr;
2360 env->regwptr[UREG_I0] = sig;
2361 env->regwptr[UREG_I1] = sf_addr +
2362 offsetof(struct target_signal_frame, info);
2363 env->regwptr[UREG_I2] = sf_addr +
2364 offsetof(struct target_signal_frame, info);
2366 /* 4. signal handler */
2367 env->pc = ka->_sa_handler;
2368 env->npc = (env->pc + 4);
2369 /* 5. return to kernel instructions */
2370 if (ka->sa_restorer)
2371 env->regwptr[UREG_I7] = ka->sa_restorer;
2372 else {
2373 uint32_t val32;
2375 env->regwptr[UREG_I7] = sf_addr +
2376 offsetof(struct target_signal_frame, insns) - 2 * 4;
2378 /* mov __NR_sigreturn, %g1 */
2379 val32 = 0x821020d8;
2380 err |= __put_user(val32, &sf->insns[0]);
2382 /* t 0x10 */
2383 val32 = 0x91d02010;
2384 err |= __put_user(val32, &sf->insns[1]);
2385 if (err)
2386 goto sigsegv;
2388 /* Flush instruction space. */
2389 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2390 // tb_flush(env);
2392 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2393 return;
2394 #if 0
2395 sigill_and_return:
2396 force_sig(TARGET_SIGILL);
2397 #endif
2398 sigsegv:
2399 //fprintf(stderr, "force_sig\n");
2400 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2401 force_sig(TARGET_SIGSEGV);
2403 static inline int
2404 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
2406 int err;
2407 #if 0
2408 #ifdef CONFIG_SMP
2409 if (current->flags & PF_USEDFPU)
2410 regs->psr &= ~PSR_EF;
2411 #else
2412 if (current == last_task_used_math) {
2413 last_task_used_math = 0;
2414 regs->psr &= ~PSR_EF;
2416 #endif
2417 current->used_math = 1;
2418 current->flags &= ~PF_USEDFPU;
2419 #endif
2420 #if 0
2421 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2422 return -EFAULT;
2423 #endif
2425 /* XXX: incorrect */
2426 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2427 (sizeof(abi_ulong) * 32));
2428 err |= __get_user(env->fsr, &fpu->si_fsr);
2429 #if 0
2430 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2431 if (current->thread.fpqdepth != 0)
2432 err |= __copy_from_user(&current->thread.fpqueue[0],
2433 &fpu->si_fpqueue[0],
2434 ((sizeof(unsigned long) +
2435 (sizeof(unsigned long *)))*16));
2436 #endif
2437 return err;
2441 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2442 target_siginfo_t *info,
2443 target_sigset_t *set, CPUSPARCState *env)
2445 fprintf(stderr, "setup_rt_frame: not implemented\n");
2448 long do_sigreturn(CPUSPARCState *env)
2450 abi_ulong sf_addr;
2451 struct target_signal_frame *sf;
2452 uint32_t up_psr, pc, npc;
2453 target_sigset_t set;
2454 sigset_t host_set;
2455 int err, i;
2457 sf_addr = env->regwptr[UREG_FP];
2458 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2459 goto segv_and_exit;
2460 #if 0
2461 fprintf(stderr, "sigreturn\n");
2462 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2463 #endif
2464 //cpu_dump_state(env, stderr, fprintf, 0);
2466 /* 1. Make sure we are not getting garbage from the user */
2468 if (sf_addr & 3)
2469 goto segv_and_exit;
2471 err = __get_user(pc, &sf->info.si_regs.pc);
2472 err |= __get_user(npc, &sf->info.si_regs.npc);
2474 if ((pc | npc) & 3)
2475 goto segv_and_exit;
2477 /* 2. Restore the state */
2478 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2480 /* User can only change condition codes and FPU enabling in %psr. */
2481 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2482 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2484 env->pc = pc;
2485 env->npc = npc;
2486 err |= __get_user(env->y, &sf->info.si_regs.y);
2487 for (i=0; i < 8; i++) {
2488 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2490 for (i=0; i < 8; i++) {
2491 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2494 /* FIXME: implement FPU save/restore:
2495 * __get_user(fpu_save, &sf->fpu_save);
2496 * if (fpu_save)
2497 * err |= restore_fpu_state(env, fpu_save);
2500 /* This is pretty much atomic, no amount locking would prevent
2501 * the races which exist anyways.
2503 err |= __get_user(set.sig[0], &sf->info.si_mask);
2504 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2505 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2508 target_to_host_sigset_internal(&host_set, &set);
2509 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2511 if (err)
2512 goto segv_and_exit;
2513 unlock_user_struct(sf, sf_addr, 0);
2514 return env->regwptr[0];
2516 segv_and_exit:
2517 unlock_user_struct(sf, sf_addr, 0);
2518 force_sig(TARGET_SIGSEGV);
2521 long do_rt_sigreturn(CPUSPARCState *env)
2523 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2524 return -TARGET_ENOSYS;
2527 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2528 #define MC_TSTATE 0
2529 #define MC_PC 1
2530 #define MC_NPC 2
2531 #define MC_Y 3
2532 #define MC_G1 4
2533 #define MC_G2 5
2534 #define MC_G3 6
2535 #define MC_G4 7
2536 #define MC_G5 8
2537 #define MC_G6 9
2538 #define MC_G7 10
2539 #define MC_O0 11
2540 #define MC_O1 12
2541 #define MC_O2 13
2542 #define MC_O3 14
2543 #define MC_O4 15
2544 #define MC_O5 16
2545 #define MC_O6 17
2546 #define MC_O7 18
2547 #define MC_NGREG 19
2549 typedef abi_ulong target_mc_greg_t;
2550 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2552 struct target_mc_fq {
2553 abi_ulong *mcfq_addr;
2554 uint32_t mcfq_insn;
2557 struct target_mc_fpu {
2558 union {
2559 uint32_t sregs[32];
2560 uint64_t dregs[32];
2561 //uint128_t qregs[16];
2562 } mcfpu_fregs;
2563 abi_ulong mcfpu_fsr;
2564 abi_ulong mcfpu_fprs;
2565 abi_ulong mcfpu_gsr;
2566 struct target_mc_fq *mcfpu_fq;
2567 unsigned char mcfpu_qcnt;
2568 unsigned char mcfpu_qentsz;
2569 unsigned char mcfpu_enab;
2571 typedef struct target_mc_fpu target_mc_fpu_t;
2573 typedef struct {
2574 target_mc_gregset_t mc_gregs;
2575 target_mc_greg_t mc_fp;
2576 target_mc_greg_t mc_i7;
2577 target_mc_fpu_t mc_fpregs;
2578 } target_mcontext_t;
2580 struct target_ucontext {
2581 struct target_ucontext *tuc_link;
2582 abi_ulong tuc_flags;
2583 target_sigset_t tuc_sigmask;
2584 target_mcontext_t tuc_mcontext;
2587 /* A V9 register window */
2588 struct target_reg_window {
2589 abi_ulong locals[8];
2590 abi_ulong ins[8];
2593 #define TARGET_STACK_BIAS 2047
2595 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2596 void sparc64_set_context(CPUSPARCState *env)
2598 abi_ulong ucp_addr;
2599 struct target_ucontext *ucp;
2600 target_mc_gregset_t *grp;
2601 abi_ulong pc, npc, tstate;
2602 abi_ulong fp, i7, w_addr;
2603 int err;
2604 unsigned int i;
2606 ucp_addr = env->regwptr[UREG_I0];
2607 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2608 goto do_sigsegv;
2609 grp = &ucp->tuc_mcontext.mc_gregs;
2610 err = __get_user(pc, &((*grp)[MC_PC]));
2611 err |= __get_user(npc, &((*grp)[MC_NPC]));
2612 if (err || ((pc | npc) & 3))
2613 goto do_sigsegv;
2614 if (env->regwptr[UREG_I1]) {
2615 target_sigset_t target_set;
2616 sigset_t set;
2618 if (TARGET_NSIG_WORDS == 1) {
2619 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2620 goto do_sigsegv;
2621 } else {
2622 abi_ulong *src, *dst;
2623 src = ucp->tuc_sigmask.sig;
2624 dst = target_set.sig;
2625 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2626 err |= __get_user(*dst, src);
2628 if (err)
2629 goto do_sigsegv;
2631 target_to_host_sigset_internal(&set, &target_set);
2632 do_sigprocmask(SIG_SETMASK, &set, NULL);
2634 env->pc = pc;
2635 env->npc = npc;
2636 err |= __get_user(env->y, &((*grp)[MC_Y]));
2637 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2638 env->asi = (tstate >> 24) & 0xff;
2639 cpu_put_ccr(env, tstate >> 32);
2640 cpu_put_cwp64(env, tstate & 0x1f);
2641 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2642 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2643 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2644 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2645 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2646 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2647 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2648 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2649 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2650 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2651 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2652 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2653 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2654 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2655 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2657 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2658 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2660 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2661 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2662 abi_ulong) != 0)
2663 goto do_sigsegv;
2664 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2665 abi_ulong) != 0)
2666 goto do_sigsegv;
2667 /* FIXME this does not match how the kernel handles the FPU in
2668 * its sparc64_set_context implementation. In particular the FPU
2669 * is only restored if fenab is non-zero in:
2670 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2672 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2674 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2675 for (i = 0; i < 64; i++, src++) {
2676 if (i & 1) {
2677 err |= __get_user(env->fpr[i/2].l.lower, src);
2678 } else {
2679 err |= __get_user(env->fpr[i/2].l.upper, src);
2683 err |= __get_user(env->fsr,
2684 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2685 err |= __get_user(env->gsr,
2686 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2687 if (err)
2688 goto do_sigsegv;
2689 unlock_user_struct(ucp, ucp_addr, 0);
2690 return;
2691 do_sigsegv:
2692 unlock_user_struct(ucp, ucp_addr, 0);
2693 force_sig(TARGET_SIGSEGV);
2696 void sparc64_get_context(CPUSPARCState *env)
2698 abi_ulong ucp_addr;
2699 struct target_ucontext *ucp;
2700 target_mc_gregset_t *grp;
2701 target_mcontext_t *mcp;
2702 abi_ulong fp, i7, w_addr;
2703 int err;
2704 unsigned int i;
2705 target_sigset_t target_set;
2706 sigset_t set;
2708 ucp_addr = env->regwptr[UREG_I0];
2709 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2710 goto do_sigsegv;
2712 mcp = &ucp->tuc_mcontext;
2713 grp = &mcp->mc_gregs;
2715 /* Skip over the trap instruction, first. */
2716 env->pc = env->npc;
2717 env->npc += 4;
2719 err = 0;
2721 do_sigprocmask(0, NULL, &set);
2722 host_to_target_sigset_internal(&target_set, &set);
2723 if (TARGET_NSIG_WORDS == 1) {
2724 err |= __put_user(target_set.sig[0],
2725 (abi_ulong *)&ucp->tuc_sigmask);
2726 } else {
2727 abi_ulong *src, *dst;
2728 src = target_set.sig;
2729 dst = ucp->tuc_sigmask.sig;
2730 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2731 err |= __put_user(*src, dst);
2733 if (err)
2734 goto do_sigsegv;
2737 /* XXX: tstate must be saved properly */
2738 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2739 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2740 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2741 err |= __put_user(env->y, &((*grp)[MC_Y]));
2742 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2743 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2744 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2745 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2746 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2747 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2748 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2749 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2750 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2751 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2752 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2753 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2754 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2755 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2756 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2758 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2759 fp = i7 = 0;
2760 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2761 abi_ulong) != 0)
2762 goto do_sigsegv;
2763 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2764 abi_ulong) != 0)
2765 goto do_sigsegv;
2766 err |= __put_user(fp, &(mcp->mc_fp));
2767 err |= __put_user(i7, &(mcp->mc_i7));
2770 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2771 for (i = 0; i < 64; i++, dst++) {
2772 if (i & 1) {
2773 err |= __put_user(env->fpr[i/2].l.lower, dst);
2774 } else {
2775 err |= __put_user(env->fpr[i/2].l.upper, dst);
2779 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2780 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2781 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2783 if (err)
2784 goto do_sigsegv;
2785 unlock_user_struct(ucp, ucp_addr, 1);
2786 return;
2787 do_sigsegv:
2788 unlock_user_struct(ucp, ucp_addr, 1);
2789 force_sig(TARGET_SIGSEGV);
2791 #endif
2792 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2794 # if defined(TARGET_ABI_MIPSO32)
2795 struct target_sigcontext {
2796 uint32_t sc_regmask; /* Unused */
2797 uint32_t sc_status;
2798 uint64_t sc_pc;
2799 uint64_t sc_regs[32];
2800 uint64_t sc_fpregs[32];
2801 uint32_t sc_ownedfp; /* Unused */
2802 uint32_t sc_fpc_csr;
2803 uint32_t sc_fpc_eir; /* Unused */
2804 uint32_t sc_used_math;
2805 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2806 uint32_t pad0;
2807 uint64_t sc_mdhi;
2808 uint64_t sc_mdlo;
2809 target_ulong sc_hi1; /* Was sc_cause */
2810 target_ulong sc_lo1; /* Was sc_badvaddr */
2811 target_ulong sc_hi2; /* Was sc_sigset[4] */
2812 target_ulong sc_lo2;
2813 target_ulong sc_hi3;
2814 target_ulong sc_lo3;
2816 # else /* N32 || N64 */
2817 struct target_sigcontext {
2818 uint64_t sc_regs[32];
2819 uint64_t sc_fpregs[32];
2820 uint64_t sc_mdhi;
2821 uint64_t sc_hi1;
2822 uint64_t sc_hi2;
2823 uint64_t sc_hi3;
2824 uint64_t sc_mdlo;
2825 uint64_t sc_lo1;
2826 uint64_t sc_lo2;
2827 uint64_t sc_lo3;
2828 uint64_t sc_pc;
2829 uint32_t sc_fpc_csr;
2830 uint32_t sc_used_math;
2831 uint32_t sc_dsp;
2832 uint32_t sc_reserved;
2834 # endif /* O32 */
2836 struct sigframe {
2837 uint32_t sf_ass[4]; /* argument save space for o32 */
2838 uint32_t sf_code[2]; /* signal trampoline */
2839 struct target_sigcontext sf_sc;
2840 target_sigset_t sf_mask;
2843 struct target_ucontext {
2844 target_ulong tuc_flags;
2845 target_ulong tuc_link;
2846 target_stack_t tuc_stack;
2847 target_ulong pad0;
2848 struct target_sigcontext tuc_mcontext;
2849 target_sigset_t tuc_sigmask;
2852 struct target_rt_sigframe {
2853 uint32_t rs_ass[4]; /* argument save space for o32 */
2854 uint32_t rs_code[2]; /* signal trampoline */
2855 struct target_siginfo rs_info;
2856 struct target_ucontext rs_uc;
2859 /* Install trampoline to jump back from signal handler */
2860 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2862 int err = 0;
2865 * Set up the return code ...
2867 * li v0, __NR__foo_sigreturn
2868 * syscall
2871 err |= __put_user(0x24020000 + syscall, tramp + 0);
2872 err |= __put_user(0x0000000c , tramp + 1);
2873 return err;
2876 static inline int
2877 setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2879 int err = 0;
2880 int i;
2882 err |= __put_user(exception_resume_pc(regs), &sc->sc_pc);
2883 regs->hflags &= ~MIPS_HFLAG_BMASK;
2885 __put_user(0, &sc->sc_regs[0]);
2886 for (i = 1; i < 32; ++i) {
2887 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2890 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2891 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2893 /* Rather than checking for dsp existence, always copy. The storage
2894 would just be garbage otherwise. */
2895 err |= __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2896 err |= __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2897 err |= __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2898 err |= __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2899 err |= __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2900 err |= __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2902 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2903 err |= __put_user(dsp, &sc->sc_dsp);
2906 err |= __put_user(1, &sc->sc_used_math);
2908 for (i = 0; i < 32; ++i) {
2909 err |= __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2912 return err;
2915 static inline int
2916 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2918 int err = 0;
2919 int i;
2921 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2923 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2924 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2926 for (i = 1; i < 32; ++i) {
2927 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2930 err |= __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2931 err |= __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2932 err |= __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2933 err |= __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2934 err |= __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2935 err |= __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2937 uint32_t dsp;
2938 err |= __get_user(dsp, &sc->sc_dsp);
2939 cpu_wrdsp(dsp, 0x3ff, regs);
2942 for (i = 0; i < 32; ++i) {
2943 err |= __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2946 return err;
2950 * Determine which stack to use..
2952 static inline abi_ulong
2953 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2955 unsigned long sp;
2957 /* Default to using normal stack */
2958 sp = regs->active_tc.gpr[29];
2961 * FPU emulator may have its own trampoline active just
2962 * above the user stack, 16-bytes before the next lowest
2963 * 16 byte boundary. Try to avoid trashing it.
2965 sp -= 32;
2967 /* This is the X/Open sanctioned signal stack switching. */
2968 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2969 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2972 return (sp - frame_size) & ~7;
2975 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2977 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2978 env->hflags &= ~MIPS_HFLAG_M16;
2979 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2980 env->active_tc.PC &= ~(target_ulong) 1;
2984 # if defined(TARGET_ABI_MIPSO32)
2985 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2986 static void setup_frame(int sig, struct target_sigaction * ka,
2987 target_sigset_t *set, CPUMIPSState *regs)
2989 struct sigframe *frame;
2990 abi_ulong frame_addr;
2991 int i;
2993 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2994 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2995 goto give_sigsegv;
2997 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2999 if(setup_sigcontext(regs, &frame->sf_sc))
3000 goto give_sigsegv;
3002 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3003 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
3004 goto give_sigsegv;
3008 * Arguments to signal handler:
3010 * a0 = signal number
3011 * a1 = 0 (should be cause)
3012 * a2 = pointer to struct sigcontext
3014 * $25 and PC point to the signal handler, $29 points to the
3015 * struct sigframe.
3017 regs->active_tc.gpr[ 4] = sig;
3018 regs->active_tc.gpr[ 5] = 0;
3019 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
3020 regs->active_tc.gpr[29] = frame_addr;
3021 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
3022 /* The original kernel code sets CP0_EPC to the handler
3023 * since it returns to userland using eret
3024 * we cannot do this here, and we must set PC directly */
3025 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
3026 mips_set_hflags_isa_mode_from_pc(regs);
3027 unlock_user_struct(frame, frame_addr, 1);
3028 return;
3030 give_sigsegv:
3031 unlock_user_struct(frame, frame_addr, 1);
3032 force_sig(TARGET_SIGSEGV/*, current*/);
3035 long do_sigreturn(CPUMIPSState *regs)
3037 struct sigframe *frame;
3038 abi_ulong frame_addr;
3039 sigset_t blocked;
3040 target_sigset_t target_set;
3041 int i;
3043 #if defined(DEBUG_SIGNAL)
3044 fprintf(stderr, "do_sigreturn\n");
3045 #endif
3046 frame_addr = regs->active_tc.gpr[29];
3047 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3048 goto badframe;
3050 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3051 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
3052 goto badframe;
3055 target_to_host_sigset_internal(&blocked, &target_set);
3056 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3058 if (restore_sigcontext(regs, &frame->sf_sc))
3059 goto badframe;
3061 #if 0
3063 * Don't let your children do this ...
3065 __asm__ __volatile__(
3066 "move\t$29, %0\n\t"
3067 "j\tsyscall_exit"
3068 :/* no outputs */
3069 :"r" (&regs));
3070 /* Unreached */
3071 #endif
3073 regs->active_tc.PC = regs->CP0_EPC;
3074 mips_set_hflags_isa_mode_from_pc(regs);
3075 /* I am not sure this is right, but it seems to work
3076 * maybe a problem with nested signals ? */
3077 regs->CP0_EPC = 0;
3078 return -TARGET_QEMU_ESIGRETURN;
3080 badframe:
3081 force_sig(TARGET_SIGSEGV/*, current*/);
3082 return 0;
3084 # endif /* O32 */
3086 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3087 target_siginfo_t *info,
3088 target_sigset_t *set, CPUMIPSState *env)
3090 struct target_rt_sigframe *frame;
3091 abi_ulong frame_addr;
3092 int i;
3094 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3095 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3096 goto give_sigsegv;
3098 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3100 copy_siginfo_to_user(&frame->rs_info, info);
3102 __put_user(0, &frame->rs_uc.tuc_flags);
3103 __put_user(0, &frame->rs_uc.tuc_link);
3104 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3105 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3106 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3107 &frame->rs_uc.tuc_stack.ss_flags);
3109 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3111 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3112 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3116 * Arguments to signal handler:
3118 * a0 = signal number
3119 * a1 = pointer to siginfo_t
3120 * a2 = pointer to struct ucontext
3122 * $25 and PC point to the signal handler, $29 points to the
3123 * struct sigframe.
3125 env->active_tc.gpr[ 4] = sig;
3126 env->active_tc.gpr[ 5] = frame_addr
3127 + offsetof(struct target_rt_sigframe, rs_info);
3128 env->active_tc.gpr[ 6] = frame_addr
3129 + offsetof(struct target_rt_sigframe, rs_uc);
3130 env->active_tc.gpr[29] = frame_addr;
3131 env->active_tc.gpr[31] = frame_addr
3132 + offsetof(struct target_rt_sigframe, rs_code);
3133 /* The original kernel code sets CP0_EPC to the handler
3134 * since it returns to userland using eret
3135 * we cannot do this here, and we must set PC directly */
3136 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3137 mips_set_hflags_isa_mode_from_pc(env);
3138 unlock_user_struct(frame, frame_addr, 1);
3139 return;
3141 give_sigsegv:
3142 unlock_user_struct(frame, frame_addr, 1);
3143 force_sig(TARGET_SIGSEGV/*, current*/);
3146 long do_rt_sigreturn(CPUMIPSState *env)
3148 struct target_rt_sigframe *frame;
3149 abi_ulong frame_addr;
3150 sigset_t blocked;
3152 #if defined(DEBUG_SIGNAL)
3153 fprintf(stderr, "do_rt_sigreturn\n");
3154 #endif
3155 frame_addr = env->active_tc.gpr[29];
3156 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3157 goto badframe;
3159 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3160 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3162 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
3163 goto badframe;
3165 if (do_sigaltstack(frame_addr +
3166 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3167 0, get_sp_from_cpustate(env)) == -EFAULT)
3168 goto badframe;
3170 env->active_tc.PC = env->CP0_EPC;
3171 mips_set_hflags_isa_mode_from_pc(env);
3172 /* I am not sure this is right, but it seems to work
3173 * maybe a problem with nested signals ? */
3174 env->CP0_EPC = 0;
3175 return -TARGET_QEMU_ESIGRETURN;
3177 badframe:
3178 force_sig(TARGET_SIGSEGV/*, current*/);
3179 return 0;
3182 #elif defined(TARGET_SH4)
3185 * code and data structures from linux kernel:
3186 * include/asm-sh/sigcontext.h
3187 * arch/sh/kernel/signal.c
3190 struct target_sigcontext {
3191 target_ulong oldmask;
3193 /* CPU registers */
3194 target_ulong sc_gregs[16];
3195 target_ulong sc_pc;
3196 target_ulong sc_pr;
3197 target_ulong sc_sr;
3198 target_ulong sc_gbr;
3199 target_ulong sc_mach;
3200 target_ulong sc_macl;
3202 /* FPU registers */
3203 target_ulong sc_fpregs[16];
3204 target_ulong sc_xfpregs[16];
3205 unsigned int sc_fpscr;
3206 unsigned int sc_fpul;
3207 unsigned int sc_ownedfp;
3210 struct target_sigframe
3212 struct target_sigcontext sc;
3213 target_ulong extramask[TARGET_NSIG_WORDS-1];
3214 uint16_t retcode[3];
3218 struct target_ucontext {
3219 target_ulong tuc_flags;
3220 struct target_ucontext *tuc_link;
3221 target_stack_t tuc_stack;
3222 struct target_sigcontext tuc_mcontext;
3223 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3226 struct target_rt_sigframe
3228 struct target_siginfo info;
3229 struct target_ucontext uc;
3230 uint16_t retcode[3];
3234 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3235 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3237 static abi_ulong get_sigframe(struct target_sigaction *ka,
3238 unsigned long sp, size_t frame_size)
3240 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3241 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3244 return (sp - frame_size) & -8ul;
3247 static int setup_sigcontext(struct target_sigcontext *sc,
3248 CPUSH4State *regs, unsigned long mask)
3250 int err = 0;
3251 int i;
3253 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
3254 COPY(gregs[0]); COPY(gregs[1]);
3255 COPY(gregs[2]); COPY(gregs[3]);
3256 COPY(gregs[4]); COPY(gregs[5]);
3257 COPY(gregs[6]); COPY(gregs[7]);
3258 COPY(gregs[8]); COPY(gregs[9]);
3259 COPY(gregs[10]); COPY(gregs[11]);
3260 COPY(gregs[12]); COPY(gregs[13]);
3261 COPY(gregs[14]); COPY(gregs[15]);
3262 COPY(gbr); COPY(mach);
3263 COPY(macl); COPY(pr);
3264 COPY(sr); COPY(pc);
3265 #undef COPY
3267 for (i=0; i<16; i++) {
3268 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3270 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
3271 err |= __put_user(regs->fpul, &sc->sc_fpul);
3273 /* non-iBCS2 extensions.. */
3274 err |= __put_user(mask, &sc->oldmask);
3276 return err;
3279 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3280 target_ulong *r0_p)
3282 unsigned int err = 0;
3283 int i;
3285 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3286 COPY(gregs[1]);
3287 COPY(gregs[2]); COPY(gregs[3]);
3288 COPY(gregs[4]); COPY(gregs[5]);
3289 COPY(gregs[6]); COPY(gregs[7]);
3290 COPY(gregs[8]); COPY(gregs[9]);
3291 COPY(gregs[10]); COPY(gregs[11]);
3292 COPY(gregs[12]); COPY(gregs[13]);
3293 COPY(gregs[14]); COPY(gregs[15]);
3294 COPY(gbr); COPY(mach);
3295 COPY(macl); COPY(pr);
3296 COPY(sr); COPY(pc);
3297 #undef COPY
3299 for (i=0; i<16; i++) {
3300 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3302 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3303 err |= __get_user(regs->fpul, &sc->sc_fpul);
3305 regs->tra = -1; /* disable syscall checks */
3306 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3307 return err;
3310 static void setup_frame(int sig, struct target_sigaction *ka,
3311 target_sigset_t *set, CPUSH4State *regs)
3313 struct target_sigframe *frame;
3314 abi_ulong frame_addr;
3315 int i;
3316 int err = 0;
3317 int signal;
3319 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3320 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3321 goto give_sigsegv;
3323 signal = current_exec_domain_sig(sig);
3325 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3327 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3328 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3331 /* Set up to return from userspace. If provided, use a stub
3332 already in userspace. */
3333 if (ka->sa_flags & TARGET_SA_RESTORER) {
3334 regs->pr = (unsigned long) ka->sa_restorer;
3335 } else {
3336 /* Generate return code (system call to sigreturn) */
3337 err |= __put_user(MOVW(2), &frame->retcode[0]);
3338 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3339 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3340 regs->pr = (unsigned long) frame->retcode;
3343 if (err)
3344 goto give_sigsegv;
3346 /* Set up registers for signal handler */
3347 regs->gregs[15] = frame_addr;
3348 regs->gregs[4] = signal; /* Arg for signal handler */
3349 regs->gregs[5] = 0;
3350 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3351 regs->pc = (unsigned long) ka->_sa_handler;
3353 unlock_user_struct(frame, frame_addr, 1);
3354 return;
3356 give_sigsegv:
3357 unlock_user_struct(frame, frame_addr, 1);
3358 force_sig(TARGET_SIGSEGV);
3361 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3362 target_siginfo_t *info,
3363 target_sigset_t *set, CPUSH4State *regs)
3365 struct target_rt_sigframe *frame;
3366 abi_ulong frame_addr;
3367 int i;
3368 int err = 0;
3369 int signal;
3371 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3372 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3373 goto give_sigsegv;
3375 signal = current_exec_domain_sig(sig);
3377 err |= copy_siginfo_to_user(&frame->info, info);
3379 /* Create the ucontext. */
3380 err |= __put_user(0, &frame->uc.tuc_flags);
3381 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3382 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3383 &frame->uc.tuc_stack.ss_sp);
3384 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3385 &frame->uc.tuc_stack.ss_flags);
3386 err |= __put_user(target_sigaltstack_used.ss_size,
3387 &frame->uc.tuc_stack.ss_size);
3388 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3389 regs, set->sig[0]);
3390 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3391 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3394 /* Set up to return from userspace. If provided, use a stub
3395 already in userspace. */
3396 if (ka->sa_flags & TARGET_SA_RESTORER) {
3397 regs->pr = (unsigned long) ka->sa_restorer;
3398 } else {
3399 /* Generate return code (system call to sigreturn) */
3400 err |= __put_user(MOVW(2), &frame->retcode[0]);
3401 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3402 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3403 regs->pr = (unsigned long) frame->retcode;
3406 if (err)
3407 goto give_sigsegv;
3409 /* Set up registers for signal handler */
3410 regs->gregs[15] = frame_addr;
3411 regs->gregs[4] = signal; /* Arg for signal handler */
3412 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3413 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3414 regs->pc = (unsigned long) ka->_sa_handler;
3416 unlock_user_struct(frame, frame_addr, 1);
3417 return;
3419 give_sigsegv:
3420 unlock_user_struct(frame, frame_addr, 1);
3421 force_sig(TARGET_SIGSEGV);
3424 long do_sigreturn(CPUSH4State *regs)
3426 struct target_sigframe *frame;
3427 abi_ulong frame_addr;
3428 sigset_t blocked;
3429 target_sigset_t target_set;
3430 target_ulong r0;
3431 int i;
3432 int err = 0;
3434 #if defined(DEBUG_SIGNAL)
3435 fprintf(stderr, "do_sigreturn\n");
3436 #endif
3437 frame_addr = regs->gregs[15];
3438 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3439 goto badframe;
3441 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3442 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3443 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3446 if (err)
3447 goto badframe;
3449 target_to_host_sigset_internal(&blocked, &target_set);
3450 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3452 if (restore_sigcontext(regs, &frame->sc, &r0))
3453 goto badframe;
3455 unlock_user_struct(frame, frame_addr, 0);
3456 return r0;
3458 badframe:
3459 unlock_user_struct(frame, frame_addr, 0);
3460 force_sig(TARGET_SIGSEGV);
3461 return 0;
3464 long do_rt_sigreturn(CPUSH4State *regs)
3466 struct target_rt_sigframe *frame;
3467 abi_ulong frame_addr;
3468 sigset_t blocked;
3469 target_ulong r0;
3471 #if defined(DEBUG_SIGNAL)
3472 fprintf(stderr, "do_rt_sigreturn\n");
3473 #endif
3474 frame_addr = regs->gregs[15];
3475 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3476 goto badframe;
3478 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3479 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3481 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3482 goto badframe;
3484 if (do_sigaltstack(frame_addr +
3485 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3486 0, get_sp_from_cpustate(regs)) == -EFAULT)
3487 goto badframe;
3489 unlock_user_struct(frame, frame_addr, 0);
3490 return r0;
3492 badframe:
3493 unlock_user_struct(frame, frame_addr, 0);
3494 force_sig(TARGET_SIGSEGV);
3495 return 0;
3497 #elif defined(TARGET_MICROBLAZE)
3499 struct target_sigcontext {
3500 struct target_pt_regs regs; /* needs to be first */
3501 uint32_t oldmask;
3504 struct target_stack_t {
3505 abi_ulong ss_sp;
3506 int ss_flags;
3507 unsigned int ss_size;
3510 struct target_ucontext {
3511 abi_ulong tuc_flags;
3512 abi_ulong tuc_link;
3513 struct target_stack_t tuc_stack;
3514 struct target_sigcontext tuc_mcontext;
3515 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3518 /* Signal frames. */
3519 struct target_signal_frame {
3520 struct target_ucontext uc;
3521 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3522 uint32_t tramp[2];
3525 struct rt_signal_frame {
3526 siginfo_t info;
3527 struct ucontext uc;
3528 uint32_t tramp[2];
3531 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3533 __put_user(env->regs[0], &sc->regs.r0);
3534 __put_user(env->regs[1], &sc->regs.r1);
3535 __put_user(env->regs[2], &sc->regs.r2);
3536 __put_user(env->regs[3], &sc->regs.r3);
3537 __put_user(env->regs[4], &sc->regs.r4);
3538 __put_user(env->regs[5], &sc->regs.r5);
3539 __put_user(env->regs[6], &sc->regs.r6);
3540 __put_user(env->regs[7], &sc->regs.r7);
3541 __put_user(env->regs[8], &sc->regs.r8);
3542 __put_user(env->regs[9], &sc->regs.r9);
3543 __put_user(env->regs[10], &sc->regs.r10);
3544 __put_user(env->regs[11], &sc->regs.r11);
3545 __put_user(env->regs[12], &sc->regs.r12);
3546 __put_user(env->regs[13], &sc->regs.r13);
3547 __put_user(env->regs[14], &sc->regs.r14);
3548 __put_user(env->regs[15], &sc->regs.r15);
3549 __put_user(env->regs[16], &sc->regs.r16);
3550 __put_user(env->regs[17], &sc->regs.r17);
3551 __put_user(env->regs[18], &sc->regs.r18);
3552 __put_user(env->regs[19], &sc->regs.r19);
3553 __put_user(env->regs[20], &sc->regs.r20);
3554 __put_user(env->regs[21], &sc->regs.r21);
3555 __put_user(env->regs[22], &sc->regs.r22);
3556 __put_user(env->regs[23], &sc->regs.r23);
3557 __put_user(env->regs[24], &sc->regs.r24);
3558 __put_user(env->regs[25], &sc->regs.r25);
3559 __put_user(env->regs[26], &sc->regs.r26);
3560 __put_user(env->regs[27], &sc->regs.r27);
3561 __put_user(env->regs[28], &sc->regs.r28);
3562 __put_user(env->regs[29], &sc->regs.r29);
3563 __put_user(env->regs[30], &sc->regs.r30);
3564 __put_user(env->regs[31], &sc->regs.r31);
3565 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3568 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3570 __get_user(env->regs[0], &sc->regs.r0);
3571 __get_user(env->regs[1], &sc->regs.r1);
3572 __get_user(env->regs[2], &sc->regs.r2);
3573 __get_user(env->regs[3], &sc->regs.r3);
3574 __get_user(env->regs[4], &sc->regs.r4);
3575 __get_user(env->regs[5], &sc->regs.r5);
3576 __get_user(env->regs[6], &sc->regs.r6);
3577 __get_user(env->regs[7], &sc->regs.r7);
3578 __get_user(env->regs[8], &sc->regs.r8);
3579 __get_user(env->regs[9], &sc->regs.r9);
3580 __get_user(env->regs[10], &sc->regs.r10);
3581 __get_user(env->regs[11], &sc->regs.r11);
3582 __get_user(env->regs[12], &sc->regs.r12);
3583 __get_user(env->regs[13], &sc->regs.r13);
3584 __get_user(env->regs[14], &sc->regs.r14);
3585 __get_user(env->regs[15], &sc->regs.r15);
3586 __get_user(env->regs[16], &sc->regs.r16);
3587 __get_user(env->regs[17], &sc->regs.r17);
3588 __get_user(env->regs[18], &sc->regs.r18);
3589 __get_user(env->regs[19], &sc->regs.r19);
3590 __get_user(env->regs[20], &sc->regs.r20);
3591 __get_user(env->regs[21], &sc->regs.r21);
3592 __get_user(env->regs[22], &sc->regs.r22);
3593 __get_user(env->regs[23], &sc->regs.r23);
3594 __get_user(env->regs[24], &sc->regs.r24);
3595 __get_user(env->regs[25], &sc->regs.r25);
3596 __get_user(env->regs[26], &sc->regs.r26);
3597 __get_user(env->regs[27], &sc->regs.r27);
3598 __get_user(env->regs[28], &sc->regs.r28);
3599 __get_user(env->regs[29], &sc->regs.r29);
3600 __get_user(env->regs[30], &sc->regs.r30);
3601 __get_user(env->regs[31], &sc->regs.r31);
3602 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3605 static abi_ulong get_sigframe(struct target_sigaction *ka,
3606 CPUMBState *env, int frame_size)
3608 abi_ulong sp = env->regs[1];
3610 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3611 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3613 return ((sp - frame_size) & -8UL);
3616 static void setup_frame(int sig, struct target_sigaction *ka,
3617 target_sigset_t *set, CPUMBState *env)
3619 struct target_signal_frame *frame;
3620 abi_ulong frame_addr;
3621 int err = 0;
3622 int i;
3624 frame_addr = get_sigframe(ka, env, sizeof *frame);
3625 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3626 goto badframe;
3628 /* Save the mask. */
3629 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3630 if (err)
3631 goto badframe;
3633 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3634 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3635 goto badframe;
3638 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3640 /* Set up to return from userspace. If provided, use a stub
3641 already in userspace. */
3642 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3643 if (ka->sa_flags & TARGET_SA_RESTORER) {
3644 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3645 } else {
3646 uint32_t t;
3647 /* Note, these encodings are _big endian_! */
3648 /* addi r12, r0, __NR_sigreturn */
3649 t = 0x31800000UL | TARGET_NR_sigreturn;
3650 err |= __put_user(t, frame->tramp + 0);
3651 /* brki r14, 0x8 */
3652 t = 0xb9cc0008UL;
3653 err |= __put_user(t, frame->tramp + 1);
3655 /* Return from sighandler will jump to the tramp.
3656 Negative 8 offset because return is rtsd r15, 8 */
3657 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3660 if (err)
3661 goto badframe;
3663 /* Set up registers for signal handler */
3664 env->regs[1] = frame_addr;
3665 /* Signal handler args: */
3666 env->regs[5] = sig; /* Arg 0: signum */
3667 env->regs[6] = 0;
3668 /* arg 1: sigcontext */
3669 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3671 /* Offset of 4 to handle microblaze rtid r14, 0 */
3672 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3674 unlock_user_struct(frame, frame_addr, 1);
3675 return;
3676 badframe:
3677 unlock_user_struct(frame, frame_addr, 1);
3678 force_sig(TARGET_SIGSEGV);
3681 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3682 target_siginfo_t *info,
3683 target_sigset_t *set, CPUMBState *env)
3685 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3688 long do_sigreturn(CPUMBState *env)
3690 struct target_signal_frame *frame;
3691 abi_ulong frame_addr;
3692 target_sigset_t target_set;
3693 sigset_t set;
3694 int i;
3696 frame_addr = env->regs[R_SP];
3697 /* Make sure the guest isn't playing games. */
3698 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3699 goto badframe;
3701 /* Restore blocked signals */
3702 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3703 goto badframe;
3704 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3705 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3706 goto badframe;
3708 target_to_host_sigset_internal(&set, &target_set);
3709 do_sigprocmask(SIG_SETMASK, &set, NULL);
3711 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3712 /* We got here through a sigreturn syscall, our path back is via an
3713 rtb insn so setup r14 for that. */
3714 env->regs[14] = env->sregs[SR_PC];
3716 unlock_user_struct(frame, frame_addr, 0);
3717 return env->regs[10];
3718 badframe:
3719 unlock_user_struct(frame, frame_addr, 0);
3720 force_sig(TARGET_SIGSEGV);
3723 long do_rt_sigreturn(CPUMBState *env)
3725 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3726 return -TARGET_ENOSYS;
3729 #elif defined(TARGET_CRIS)
3731 struct target_sigcontext {
3732 struct target_pt_regs regs; /* needs to be first */
3733 uint32_t oldmask;
3734 uint32_t usp; /* usp before stacking this gunk on it */
3737 /* Signal frames. */
3738 struct target_signal_frame {
3739 struct target_sigcontext sc;
3740 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3741 uint16_t retcode[4]; /* Trampoline code. */
3744 struct rt_signal_frame {
3745 siginfo_t *pinfo;
3746 void *puc;
3747 siginfo_t info;
3748 struct ucontext uc;
3749 uint16_t retcode[4]; /* Trampoline code. */
3752 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3754 __put_user(env->regs[0], &sc->regs.r0);
3755 __put_user(env->regs[1], &sc->regs.r1);
3756 __put_user(env->regs[2], &sc->regs.r2);
3757 __put_user(env->regs[3], &sc->regs.r3);
3758 __put_user(env->regs[4], &sc->regs.r4);
3759 __put_user(env->regs[5], &sc->regs.r5);
3760 __put_user(env->regs[6], &sc->regs.r6);
3761 __put_user(env->regs[7], &sc->regs.r7);
3762 __put_user(env->regs[8], &sc->regs.r8);
3763 __put_user(env->regs[9], &sc->regs.r9);
3764 __put_user(env->regs[10], &sc->regs.r10);
3765 __put_user(env->regs[11], &sc->regs.r11);
3766 __put_user(env->regs[12], &sc->regs.r12);
3767 __put_user(env->regs[13], &sc->regs.r13);
3768 __put_user(env->regs[14], &sc->usp);
3769 __put_user(env->regs[15], &sc->regs.acr);
3770 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3771 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3772 __put_user(env->pc, &sc->regs.erp);
3775 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3777 __get_user(env->regs[0], &sc->regs.r0);
3778 __get_user(env->regs[1], &sc->regs.r1);
3779 __get_user(env->regs[2], &sc->regs.r2);
3780 __get_user(env->regs[3], &sc->regs.r3);
3781 __get_user(env->regs[4], &sc->regs.r4);
3782 __get_user(env->regs[5], &sc->regs.r5);
3783 __get_user(env->regs[6], &sc->regs.r6);
3784 __get_user(env->regs[7], &sc->regs.r7);
3785 __get_user(env->regs[8], &sc->regs.r8);
3786 __get_user(env->regs[9], &sc->regs.r9);
3787 __get_user(env->regs[10], &sc->regs.r10);
3788 __get_user(env->regs[11], &sc->regs.r11);
3789 __get_user(env->regs[12], &sc->regs.r12);
3790 __get_user(env->regs[13], &sc->regs.r13);
3791 __get_user(env->regs[14], &sc->usp);
3792 __get_user(env->regs[15], &sc->regs.acr);
3793 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3794 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3795 __get_user(env->pc, &sc->regs.erp);
3798 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3800 abi_ulong sp;
3801 /* Align the stack downwards to 4. */
3802 sp = (env->regs[R_SP] & ~3);
3803 return sp - framesize;
3806 static void setup_frame(int sig, struct target_sigaction *ka,
3807 target_sigset_t *set, CPUCRISState *env)
3809 struct target_signal_frame *frame;
3810 abi_ulong frame_addr;
3811 int err = 0;
3812 int i;
3814 frame_addr = get_sigframe(env, sizeof *frame);
3815 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3816 goto badframe;
3819 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3820 * use this trampoline anymore but it sets it up for GDB.
3821 * In QEMU, using the trampoline simplifies things a bit so we use it.
3823 * This is movu.w __NR_sigreturn, r9; break 13;
3825 err |= __put_user(0x9c5f, frame->retcode+0);
3826 err |= __put_user(TARGET_NR_sigreturn,
3827 frame->retcode + 1);
3828 err |= __put_user(0xe93d, frame->retcode + 2);
3830 /* Save the mask. */
3831 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3832 if (err)
3833 goto badframe;
3835 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3836 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3837 goto badframe;
3840 setup_sigcontext(&frame->sc, env);
3842 /* Move the stack and setup the arguments for the handler. */
3843 env->regs[R_SP] = frame_addr;
3844 env->regs[10] = sig;
3845 env->pc = (unsigned long) ka->_sa_handler;
3846 /* Link SRP so the guest returns through the trampoline. */
3847 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3849 unlock_user_struct(frame, frame_addr, 1);
3850 return;
3851 badframe:
3852 unlock_user_struct(frame, frame_addr, 1);
3853 force_sig(TARGET_SIGSEGV);
3856 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3857 target_siginfo_t *info,
3858 target_sigset_t *set, CPUCRISState *env)
3860 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3863 long do_sigreturn(CPUCRISState *env)
3865 struct target_signal_frame *frame;
3866 abi_ulong frame_addr;
3867 target_sigset_t target_set;
3868 sigset_t set;
3869 int i;
3871 frame_addr = env->regs[R_SP];
3872 /* Make sure the guest isn't playing games. */
3873 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3874 goto badframe;
3876 /* Restore blocked signals */
3877 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3878 goto badframe;
3879 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3880 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3881 goto badframe;
3883 target_to_host_sigset_internal(&set, &target_set);
3884 do_sigprocmask(SIG_SETMASK, &set, NULL);
3886 restore_sigcontext(&frame->sc, env);
3887 unlock_user_struct(frame, frame_addr, 0);
3888 return env->regs[10];
3889 badframe:
3890 unlock_user_struct(frame, frame_addr, 0);
3891 force_sig(TARGET_SIGSEGV);
3894 long do_rt_sigreturn(CPUCRISState *env)
3896 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3897 return -TARGET_ENOSYS;
3900 #elif defined(TARGET_OPENRISC)
3902 struct target_sigcontext {
3903 struct target_pt_regs regs;
3904 abi_ulong oldmask;
3905 abi_ulong usp;
3908 struct target_ucontext {
3909 abi_ulong tuc_flags;
3910 abi_ulong tuc_link;
3911 target_stack_t tuc_stack;
3912 struct target_sigcontext tuc_mcontext;
3913 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3916 struct target_rt_sigframe {
3917 abi_ulong pinfo;
3918 uint64_t puc;
3919 struct target_siginfo info;
3920 struct target_sigcontext sc;
3921 struct target_ucontext uc;
3922 unsigned char retcode[16]; /* trampoline code */
3925 /* This is the asm-generic/ucontext.h version */
3926 #if 0
3927 static int restore_sigcontext(CPUOpenRISCState *regs,
3928 struct target_sigcontext *sc)
3930 unsigned int err = 0;
3931 unsigned long old_usp;
3933 /* Alwys make any pending restarted system call return -EINTR */
3934 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3936 /* restore the regs from &sc->regs (same as sc, since regs is first)
3937 * (sc is already checked for VERIFY_READ since the sigframe was
3938 * checked in sys_sigreturn previously)
3941 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3942 goto badframe;
3945 /* make sure the U-flag is set so user-mode cannot fool us */
3947 regs->sr &= ~SR_SM;
3949 /* restore the old USP as it was before we stacked the sc etc.
3950 * (we cannot just pop the sigcontext since we aligned the sp and
3951 * stuff after pushing it)
3954 err |= __get_user(old_usp, &sc->usp);
3955 phx_signal("old_usp 0x%lx", old_usp);
3957 __PHX__ REALLY /* ??? */
3958 wrusp(old_usp);
3959 regs->gpr[1] = old_usp;
3961 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3962 * after this completes, but we don't use that mechanism. maybe we can
3963 * use it now ?
3966 return err;
3968 badframe:
3969 return 1;
3971 #endif
3973 /* Set up a signal frame. */
3975 static int setup_sigcontext(struct target_sigcontext *sc,
3976 CPUOpenRISCState *regs,
3977 unsigned long mask)
3979 int err = 0;
3980 unsigned long usp = regs->gpr[1];
3982 /* copy the regs. they are first in sc so we can use sc directly */
3984 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3986 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3987 the signal handler. The frametype will be restored to its previous
3988 value in restore_sigcontext. */
3989 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3991 /* then some other stuff */
3992 err |= __put_user(mask, &sc->oldmask);
3993 err |= __put_user(usp, &sc->usp); return err;
3996 static inline unsigned long align_sigframe(unsigned long sp)
3998 unsigned long i;
3999 i = sp & ~3UL;
4000 return i;
4003 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
4004 CPUOpenRISCState *regs,
4005 size_t frame_size)
4007 unsigned long sp = regs->gpr[1];
4008 int onsigstack = on_sig_stack(sp);
4010 /* redzone */
4011 /* This is the X/Open sanctioned signal stack switching. */
4012 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
4013 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4016 sp = align_sigframe(sp - frame_size);
4019 * If we are on the alternate signal stack and would overflow it, don't.
4020 * Return an always-bogus address instead so we will die with SIGSEGV.
4023 if (onsigstack && !likely(on_sig_stack(sp))) {
4024 return -1L;
4027 return sp;
4030 static void setup_frame(int sig, struct target_sigaction *ka,
4031 target_sigset_t *set, CPUOpenRISCState *env)
4033 qemu_log("Not implement.\n");
4036 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4037 target_siginfo_t *info,
4038 target_sigset_t *set, CPUOpenRISCState *env)
4040 int err = 0;
4041 abi_ulong frame_addr;
4042 unsigned long return_ip;
4043 struct target_rt_sigframe *frame;
4044 abi_ulong info_addr, uc_addr;
4046 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4047 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4048 goto give_sigsegv;
4051 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4052 err |= __put_user(info_addr, &frame->pinfo);
4053 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4054 err |= __put_user(uc_addr, &frame->puc);
4056 if (ka->sa_flags & SA_SIGINFO) {
4057 err |= copy_siginfo_to_user(&frame->info, info);
4059 if (err) {
4060 goto give_sigsegv;
4063 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
4064 err |= __put_user(0, &frame->uc.tuc_flags);
4065 err |= __put_user(0, &frame->uc.tuc_link);
4066 err |= __put_user(target_sigaltstack_used.ss_sp,
4067 &frame->uc.tuc_stack.ss_sp);
4068 err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
4069 err |= __put_user(target_sigaltstack_used.ss_size,
4070 &frame->uc.tuc_stack.ss_size);
4071 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4073 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4075 if (err) {
4076 goto give_sigsegv;
4079 /* trampoline - the desired return ip is the retcode itself */
4080 return_ip = (unsigned long)&frame->retcode;
4081 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
4082 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
4083 err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
4084 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
4085 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
4087 if (err) {
4088 goto give_sigsegv;
4091 /* TODO what is the current->exec_domain stuff and invmap ? */
4093 /* Set up registers for signal handler */
4094 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
4095 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
4096 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
4097 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
4098 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
4100 /* actually move the usp to reflect the stacked frame */
4101 env->gpr[1] = (unsigned long)frame;
4103 return;
4105 give_sigsegv:
4106 unlock_user_struct(frame, frame_addr, 1);
4107 if (sig == TARGET_SIGSEGV) {
4108 ka->_sa_handler = TARGET_SIG_DFL;
4110 force_sig(TARGET_SIGSEGV);
4113 long do_sigreturn(CPUOpenRISCState *env)
4116 qemu_log("do_sigreturn: not implemented\n");
4117 return -TARGET_ENOSYS;
4120 long do_rt_sigreturn(CPUOpenRISCState *env)
4122 qemu_log("do_rt_sigreturn: not implemented\n");
4123 return -TARGET_ENOSYS;
4125 /* TARGET_OPENRISC */
4127 #elif defined(TARGET_S390X)
4129 #define __NUM_GPRS 16
4130 #define __NUM_FPRS 16
4131 #define __NUM_ACRS 16
4133 #define S390_SYSCALL_SIZE 2
4134 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4136 #define _SIGCONTEXT_NSIG 64
4137 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4138 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4139 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4140 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4141 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4143 typedef struct {
4144 target_psw_t psw;
4145 target_ulong gprs[__NUM_GPRS];
4146 unsigned int acrs[__NUM_ACRS];
4147 } target_s390_regs_common;
4149 typedef struct {
4150 unsigned int fpc;
4151 double fprs[__NUM_FPRS];
4152 } target_s390_fp_regs;
4154 typedef struct {
4155 target_s390_regs_common regs;
4156 target_s390_fp_regs fpregs;
4157 } target_sigregs;
4159 struct target_sigcontext {
4160 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4161 target_sigregs *sregs;
4164 typedef struct {
4165 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4166 struct target_sigcontext sc;
4167 target_sigregs sregs;
4168 int signo;
4169 uint8_t retcode[S390_SYSCALL_SIZE];
4170 } sigframe;
4172 struct target_ucontext {
4173 target_ulong tuc_flags;
4174 struct target_ucontext *tuc_link;
4175 target_stack_t tuc_stack;
4176 target_sigregs tuc_mcontext;
4177 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4180 typedef struct {
4181 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4182 uint8_t retcode[S390_SYSCALL_SIZE];
4183 struct target_siginfo info;
4184 struct target_ucontext uc;
4185 } rt_sigframe;
4187 static inline abi_ulong
4188 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4190 abi_ulong sp;
4192 /* Default to using normal stack */
4193 sp = env->regs[15];
4195 /* This is the X/Open sanctioned signal stack switching. */
4196 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4197 if (!sas_ss_flags(sp)) {
4198 sp = target_sigaltstack_used.ss_sp +
4199 target_sigaltstack_used.ss_size;
4203 /* This is the legacy signal stack switching. */
4204 else if (/* FIXME !user_mode(regs) */ 0 &&
4205 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4206 ka->sa_restorer) {
4207 sp = (abi_ulong) ka->sa_restorer;
4210 return (sp - frame_size) & -8ul;
4213 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4215 int i;
4216 //save_access_regs(current->thread.acrs); FIXME
4218 /* Copy a 'clean' PSW mask to the user to avoid leaking
4219 information about whether PER is currently on. */
4220 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4221 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4222 for (i = 0; i < 16; i++) {
4223 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4225 for (i = 0; i < 16; i++) {
4226 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4229 * We have to store the fp registers to current->thread.fp_regs
4230 * to merge them with the emulated registers.
4232 //save_fp_regs(&current->thread.fp_regs); FIXME
4233 for (i = 0; i < 16; i++) {
4234 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
4238 static void setup_frame(int sig, struct target_sigaction *ka,
4239 target_sigset_t *set, CPUS390XState *env)
4241 sigframe *frame;
4242 abi_ulong frame_addr;
4244 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4245 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4246 (unsigned long long)frame_addr);
4247 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4248 goto give_sigsegv;
4251 qemu_log("%s: 1\n", __FUNCTION__);
4252 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
4253 goto give_sigsegv;
4256 save_sigregs(env, &frame->sregs);
4258 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4259 (abi_ulong *)&frame->sc.sregs);
4261 /* Set up to return from userspace. If provided, use a stub
4262 already in userspace. */
4263 if (ka->sa_flags & TARGET_SA_RESTORER) {
4264 env->regs[14] = (unsigned long)
4265 ka->sa_restorer | PSW_ADDR_AMODE;
4266 } else {
4267 env->regs[14] = (unsigned long)
4268 frame->retcode | PSW_ADDR_AMODE;
4269 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4270 (uint16_t *)(frame->retcode)))
4271 goto give_sigsegv;
4274 /* Set up backchain. */
4275 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4276 goto give_sigsegv;
4279 /* Set up registers for signal handler */
4280 env->regs[15] = frame_addr;
4281 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4283 env->regs[2] = sig; //map_signal(sig);
4284 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4286 /* We forgot to include these in the sigcontext.
4287 To avoid breaking binary compatibility, they are passed as args. */
4288 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4289 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4291 /* Place signal number on stack to allow backtrace from handler. */
4292 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4293 goto give_sigsegv;
4295 unlock_user_struct(frame, frame_addr, 1);
4296 return;
4298 give_sigsegv:
4299 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4300 unlock_user_struct(frame, frame_addr, 1);
4301 force_sig(TARGET_SIGSEGV);
4304 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4305 target_siginfo_t *info,
4306 target_sigset_t *set, CPUS390XState *env)
4308 int i;
4309 rt_sigframe *frame;
4310 abi_ulong frame_addr;
4312 frame_addr = get_sigframe(ka, env, sizeof *frame);
4313 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4314 (unsigned long long)frame_addr);
4315 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4316 goto give_sigsegv;
4319 qemu_log("%s: 1\n", __FUNCTION__);
4320 if (copy_siginfo_to_user(&frame->info, info)) {
4321 goto give_sigsegv;
4324 /* Create the ucontext. */
4325 __put_user(0, &frame->uc.tuc_flags);
4326 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4327 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4328 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4329 &frame->uc.tuc_stack.ss_flags);
4330 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4331 save_sigregs(env, &frame->uc.tuc_mcontext);
4332 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4333 __put_user((abi_ulong)set->sig[i],
4334 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4337 /* Set up to return from userspace. If provided, use a stub
4338 already in userspace. */
4339 if (ka->sa_flags & TARGET_SA_RESTORER) {
4340 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4341 } else {
4342 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4343 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4344 (uint16_t *)(frame->retcode))) {
4345 goto give_sigsegv;
4349 /* Set up backchain. */
4350 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4351 goto give_sigsegv;
4354 /* Set up registers for signal handler */
4355 env->regs[15] = frame_addr;
4356 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4358 env->regs[2] = sig; //map_signal(sig);
4359 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4360 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4361 return;
4363 give_sigsegv:
4364 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4365 unlock_user_struct(frame, frame_addr, 1);
4366 force_sig(TARGET_SIGSEGV);
4369 static int
4370 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4372 int err = 0;
4373 int i;
4375 for (i = 0; i < 16; i++) {
4376 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4379 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4380 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4381 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4382 (unsigned long long)env->psw.addr);
4383 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4384 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4386 for (i = 0; i < 16; i++) {
4387 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4389 for (i = 0; i < 16; i++) {
4390 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4393 return err;
4396 long do_sigreturn(CPUS390XState *env)
4398 sigframe *frame;
4399 abi_ulong frame_addr = env->regs[15];
4400 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4401 (unsigned long long)frame_addr);
4402 target_sigset_t target_set;
4403 sigset_t set;
4405 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4406 goto badframe;
4408 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4409 goto badframe;
4412 target_to_host_sigset_internal(&set, &target_set);
4413 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4415 if (restore_sigregs(env, &frame->sregs)) {
4416 goto badframe;
4419 unlock_user_struct(frame, frame_addr, 0);
4420 return env->regs[2];
4422 badframe:
4423 unlock_user_struct(frame, frame_addr, 0);
4424 force_sig(TARGET_SIGSEGV);
4425 return 0;
4428 long do_rt_sigreturn(CPUS390XState *env)
4430 rt_sigframe *frame;
4431 abi_ulong frame_addr = env->regs[15];
4432 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4433 (unsigned long long)frame_addr);
4434 sigset_t set;
4436 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4437 goto badframe;
4439 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4441 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4443 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4444 goto badframe;
4447 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4448 get_sp_from_cpustate(env)) == -EFAULT) {
4449 goto badframe;
4451 unlock_user_struct(frame, frame_addr, 0);
4452 return env->regs[2];
4454 badframe:
4455 unlock_user_struct(frame, frame_addr, 0);
4456 force_sig(TARGET_SIGSEGV);
4457 return 0;
4460 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4462 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4463 the signal handling is different enough that we haven't implemented
4464 support for PPC64 yet. Hence the restriction above.
4466 There are various #if'd blocks for code for TARGET_PPC64. These
4467 blocks should go away so that we can successfully run 32-bit and
4468 64-bit binaries on a QEMU configured for PPC64. */
4470 /* Size of dummy stack frame allocated when calling signal handler.
4471 See arch/powerpc/include/asm/ptrace.h. */
4472 #if defined(TARGET_PPC64)
4473 #define SIGNAL_FRAMESIZE 128
4474 #else
4475 #define SIGNAL_FRAMESIZE 64
4476 #endif
4478 /* See arch/powerpc/include/asm/sigcontext.h. */
4479 struct target_sigcontext {
4480 target_ulong _unused[4];
4481 int32_t signal;
4482 #if defined(TARGET_PPC64)
4483 int32_t pad0;
4484 #endif
4485 target_ulong handler;
4486 target_ulong oldmask;
4487 target_ulong regs; /* struct pt_regs __user * */
4488 /* TODO: PPC64 includes extra bits here. */
4491 /* Indices for target_mcontext.mc_gregs, below.
4492 See arch/powerpc/include/asm/ptrace.h for details. */
4493 enum {
4494 TARGET_PT_R0 = 0,
4495 TARGET_PT_R1 = 1,
4496 TARGET_PT_R2 = 2,
4497 TARGET_PT_R3 = 3,
4498 TARGET_PT_R4 = 4,
4499 TARGET_PT_R5 = 5,
4500 TARGET_PT_R6 = 6,
4501 TARGET_PT_R7 = 7,
4502 TARGET_PT_R8 = 8,
4503 TARGET_PT_R9 = 9,
4504 TARGET_PT_R10 = 10,
4505 TARGET_PT_R11 = 11,
4506 TARGET_PT_R12 = 12,
4507 TARGET_PT_R13 = 13,
4508 TARGET_PT_R14 = 14,
4509 TARGET_PT_R15 = 15,
4510 TARGET_PT_R16 = 16,
4511 TARGET_PT_R17 = 17,
4512 TARGET_PT_R18 = 18,
4513 TARGET_PT_R19 = 19,
4514 TARGET_PT_R20 = 20,
4515 TARGET_PT_R21 = 21,
4516 TARGET_PT_R22 = 22,
4517 TARGET_PT_R23 = 23,
4518 TARGET_PT_R24 = 24,
4519 TARGET_PT_R25 = 25,
4520 TARGET_PT_R26 = 26,
4521 TARGET_PT_R27 = 27,
4522 TARGET_PT_R28 = 28,
4523 TARGET_PT_R29 = 29,
4524 TARGET_PT_R30 = 30,
4525 TARGET_PT_R31 = 31,
4526 TARGET_PT_NIP = 32,
4527 TARGET_PT_MSR = 33,
4528 TARGET_PT_ORIG_R3 = 34,
4529 TARGET_PT_CTR = 35,
4530 TARGET_PT_LNK = 36,
4531 TARGET_PT_XER = 37,
4532 TARGET_PT_CCR = 38,
4533 /* Yes, there are two registers with #39. One is 64-bit only. */
4534 TARGET_PT_MQ = 39,
4535 TARGET_PT_SOFTE = 39,
4536 TARGET_PT_TRAP = 40,
4537 TARGET_PT_DAR = 41,
4538 TARGET_PT_DSISR = 42,
4539 TARGET_PT_RESULT = 43,
4540 TARGET_PT_REGS_COUNT = 44
4543 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4544 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4545 struct target_mcontext {
4546 target_ulong mc_gregs[48];
4547 /* Includes fpscr. */
4548 uint64_t mc_fregs[33];
4549 target_ulong mc_pad[2];
4550 /* We need to handle Altivec and SPE at the same time, which no
4551 kernel needs to do. Fortunately, the kernel defines this bit to
4552 be Altivec-register-large all the time, rather than trying to
4553 twiddle it based on the specific platform. */
4554 union {
4555 /* SPE vector registers. One extra for SPEFSCR. */
4556 uint32_t spe[33];
4557 /* Altivec vector registers. The packing of VSCR and VRSAVE
4558 varies depending on whether we're PPC64 or not: PPC64 splits
4559 them apart; PPC32 stuffs them together. */
4560 #if defined(TARGET_PPC64)
4561 #define QEMU_NVRREG 34
4562 #else
4563 #define QEMU_NVRREG 33
4564 #endif
4565 ppc_avr_t altivec[QEMU_NVRREG];
4566 #undef QEMU_NVRREG
4567 } mc_vregs __attribute__((__aligned__(16)));
4570 struct target_ucontext {
4571 target_ulong tuc_flags;
4572 target_ulong tuc_link; /* struct ucontext __user * */
4573 struct target_sigaltstack tuc_stack;
4574 #if !defined(TARGET_PPC64)
4575 int32_t tuc_pad[7];
4576 target_ulong tuc_regs; /* struct mcontext __user *
4577 points to uc_mcontext field */
4578 #endif
4579 target_sigset_t tuc_sigmask;
4580 #if defined(TARGET_PPC64)
4581 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4582 struct target_sigcontext tuc_mcontext;
4583 #else
4584 int32_t tuc_maskext[30];
4585 int32_t tuc_pad2[3];
4586 struct target_mcontext tuc_mcontext;
4587 #endif
4590 /* See arch/powerpc/kernel/signal_32.c. */
4591 struct target_sigframe {
4592 struct target_sigcontext sctx;
4593 struct target_mcontext mctx;
4594 int32_t abigap[56];
4597 struct target_rt_sigframe {
4598 struct target_siginfo info;
4599 struct target_ucontext uc;
4600 int32_t abigap[56];
4603 /* We use the mc_pad field for the signal return trampoline. */
4604 #define tramp mc_pad
4606 /* See arch/powerpc/kernel/signal.c. */
4607 static target_ulong get_sigframe(struct target_sigaction *ka,
4608 CPUPPCState *env,
4609 int frame_size)
4611 target_ulong oldsp, newsp;
4613 oldsp = env->gpr[1];
4615 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4616 (sas_ss_flags(oldsp) == 0)) {
4617 oldsp = (target_sigaltstack_used.ss_sp
4618 + target_sigaltstack_used.ss_size);
4621 newsp = (oldsp - frame_size) & ~0xFUL;
4623 return newsp;
4626 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4627 int sigret)
4629 target_ulong msr = env->msr;
4630 int i;
4631 target_ulong ccr = 0;
4633 /* In general, the kernel attempts to be intelligent about what it
4634 needs to save for Altivec/FP/SPE registers. We don't care that
4635 much, so we just go ahead and save everything. */
4637 /* Save general registers. */
4638 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4639 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4640 return 1;
4643 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4644 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4645 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4646 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4647 return 1;
4649 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4650 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4652 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4653 return 1;
4655 /* Save Altivec registers if necessary. */
4656 if (env->insns_flags & PPC_ALTIVEC) {
4657 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4658 ppc_avr_t *avr = &env->avr[i];
4659 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4661 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4662 __put_user(avr->u64[1], &vreg->u64[1])) {
4663 return 1;
4666 /* Set MSR_VR in the saved MSR value to indicate that
4667 frame->mc_vregs contains valid data. */
4668 msr |= MSR_VR;
4669 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4670 &frame->mc_vregs.altivec[32].u32[3]))
4671 return 1;
4674 /* Save floating point registers. */
4675 if (env->insns_flags & PPC_FLOAT) {
4676 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4677 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4678 return 1;
4681 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4682 return 1;
4685 /* Save SPE registers. The kernel only saves the high half. */
4686 if (env->insns_flags & PPC_SPE) {
4687 #if defined(TARGET_PPC64)
4688 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4689 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4690 return 1;
4693 #else
4694 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4695 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4696 return 1;
4699 #endif
4700 /* Set MSR_SPE in the saved MSR value to indicate that
4701 frame->mc_vregs contains valid data. */
4702 msr |= MSR_SPE;
4703 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4704 return 1;
4707 /* Store MSR. */
4708 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4709 return 1;
4711 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4712 if (sigret) {
4713 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4714 __put_user(0x44000002UL, &frame->tramp[1])) {
4715 return 1;
4719 return 0;
4722 static int restore_user_regs(CPUPPCState *env,
4723 struct target_mcontext *frame, int sig)
4725 target_ulong save_r2 = 0;
4726 target_ulong msr;
4727 target_ulong ccr;
4729 int i;
4731 if (!sig) {
4732 save_r2 = env->gpr[2];
4735 /* Restore general registers. */
4736 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4737 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4738 return 1;
4741 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4742 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4743 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4744 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4745 return 1;
4746 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4747 return 1;
4749 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4750 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4753 if (!sig) {
4754 env->gpr[2] = save_r2;
4756 /* Restore MSR. */
4757 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4758 return 1;
4760 /* If doing signal return, restore the previous little-endian mode. */
4761 if (sig)
4762 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4764 /* Restore Altivec registers if necessary. */
4765 if (env->insns_flags & PPC_ALTIVEC) {
4766 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4767 ppc_avr_t *avr = &env->avr[i];
4768 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4770 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4771 __get_user(avr->u64[1], &vreg->u64[1])) {
4772 return 1;
4775 /* Set MSR_VEC in the saved MSR value to indicate that
4776 frame->mc_vregs contains valid data. */
4777 if (__get_user(env->spr[SPR_VRSAVE],
4778 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4779 return 1;
4782 /* Restore floating point registers. */
4783 if (env->insns_flags & PPC_FLOAT) {
4784 uint64_t fpscr;
4785 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4786 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4787 return 1;
4790 if (__get_user(fpscr, &frame->mc_fregs[32]))
4791 return 1;
4792 env->fpscr = (uint32_t) fpscr;
4795 /* Save SPE registers. The kernel only saves the high half. */
4796 if (env->insns_flags & PPC_SPE) {
4797 #if defined(TARGET_PPC64)
4798 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4799 uint32_t hi;
4801 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4802 return 1;
4804 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4806 #else
4807 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4808 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4809 return 1;
4812 #endif
4813 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4814 return 1;
4817 return 0;
4820 static void setup_frame(int sig, struct target_sigaction *ka,
4821 target_sigset_t *set, CPUPPCState *env)
4823 struct target_sigframe *frame;
4824 struct target_sigcontext *sc;
4825 target_ulong frame_addr, newsp;
4826 int err = 0;
4827 int signal;
4829 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4830 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4831 goto sigsegv;
4832 sc = &frame->sctx;
4834 signal = current_exec_domain_sig(sig);
4836 err |= __put_user(ka->_sa_handler, &sc->handler);
4837 err |= __put_user(set->sig[0], &sc->oldmask);
4838 #if defined(TARGET_PPC64)
4839 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4840 #else
4841 err |= __put_user(set->sig[1], &sc->_unused[3]);
4842 #endif
4843 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4844 err |= __put_user(sig, &sc->signal);
4846 /* Save user regs. */
4847 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4849 /* The kernel checks for the presence of a VDSO here. We don't
4850 emulate a vdso, so use a sigreturn system call. */
4851 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4853 /* Turn off all fp exceptions. */
4854 env->fpscr = 0;
4856 /* Create a stack frame for the caller of the handler. */
4857 newsp = frame_addr - SIGNAL_FRAMESIZE;
4858 err |= put_user(env->gpr[1], newsp, target_ulong);
4860 if (err)
4861 goto sigsegv;
4863 /* Set up registers for signal handler. */
4864 env->gpr[1] = newsp;
4865 env->gpr[3] = signal;
4866 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4867 env->nip = (target_ulong) ka->_sa_handler;
4868 /* Signal handlers are entered in big-endian mode. */
4869 env->msr &= ~MSR_LE;
4871 unlock_user_struct(frame, frame_addr, 1);
4872 return;
4874 sigsegv:
4875 unlock_user_struct(frame, frame_addr, 1);
4876 qemu_log("segfaulting from setup_frame\n");
4877 force_sig(TARGET_SIGSEGV);
4880 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4881 target_siginfo_t *info,
4882 target_sigset_t *set, CPUPPCState *env)
4884 struct target_rt_sigframe *rt_sf;
4885 struct target_mcontext *frame;
4886 target_ulong rt_sf_addr, newsp = 0;
4887 int i, err = 0;
4888 int signal;
4890 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4891 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4892 goto sigsegv;
4894 signal = current_exec_domain_sig(sig);
4896 err |= copy_siginfo_to_user(&rt_sf->info, info);
4898 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4899 err |= __put_user(0, &rt_sf->uc.tuc_link);
4900 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4901 &rt_sf->uc.tuc_stack.ss_sp);
4902 err |= __put_user(sas_ss_flags(env->gpr[1]),
4903 &rt_sf->uc.tuc_stack.ss_flags);
4904 err |= __put_user(target_sigaltstack_used.ss_size,
4905 &rt_sf->uc.tuc_stack.ss_size);
4906 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4907 &rt_sf->uc.tuc_regs);
4908 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4909 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4912 frame = &rt_sf->uc.tuc_mcontext;
4913 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4915 /* The kernel checks for the presence of a VDSO here. We don't
4916 emulate a vdso, so use a sigreturn system call. */
4917 env->lr = (target_ulong) h2g(frame->tramp);
4919 /* Turn off all fp exceptions. */
4920 env->fpscr = 0;
4922 /* Create a stack frame for the caller of the handler. */
4923 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4924 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4926 if (err)
4927 goto sigsegv;
4929 /* Set up registers for signal handler. */
4930 env->gpr[1] = newsp;
4931 env->gpr[3] = (target_ulong) signal;
4932 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4933 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4934 env->gpr[6] = (target_ulong) h2g(rt_sf);
4935 env->nip = (target_ulong) ka->_sa_handler;
4936 /* Signal handlers are entered in big-endian mode. */
4937 env->msr &= ~MSR_LE;
4939 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4940 return;
4942 sigsegv:
4943 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4944 qemu_log("segfaulting from setup_rt_frame\n");
4945 force_sig(TARGET_SIGSEGV);
4949 long do_sigreturn(CPUPPCState *env)
4951 struct target_sigcontext *sc = NULL;
4952 struct target_mcontext *sr = NULL;
4953 target_ulong sr_addr = 0, sc_addr;
4954 sigset_t blocked;
4955 target_sigset_t set;
4957 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4958 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4959 goto sigsegv;
4961 #if defined(TARGET_PPC64)
4962 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4963 #else
4964 if(__get_user(set.sig[0], &sc->oldmask) ||
4965 __get_user(set.sig[1], &sc->_unused[3]))
4966 goto sigsegv;
4967 #endif
4968 target_to_host_sigset_internal(&blocked, &set);
4969 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4971 if (__get_user(sr_addr, &sc->regs))
4972 goto sigsegv;
4973 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4974 goto sigsegv;
4975 if (restore_user_regs(env, sr, 1))
4976 goto sigsegv;
4978 unlock_user_struct(sr, sr_addr, 1);
4979 unlock_user_struct(sc, sc_addr, 1);
4980 return -TARGET_QEMU_ESIGRETURN;
4982 sigsegv:
4983 unlock_user_struct(sr, sr_addr, 1);
4984 unlock_user_struct(sc, sc_addr, 1);
4985 qemu_log("segfaulting from do_sigreturn\n");
4986 force_sig(TARGET_SIGSEGV);
4987 return 0;
4990 /* See arch/powerpc/kernel/signal_32.c. */
4991 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4993 struct target_mcontext *mcp;
4994 target_ulong mcp_addr;
4995 sigset_t blocked;
4996 target_sigset_t set;
4998 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4999 sizeof (set)))
5000 return 1;
5002 #if defined(TARGET_PPC64)
5003 fprintf (stderr, "do_setcontext: not implemented\n");
5004 return 0;
5005 #else
5006 if (__get_user(mcp_addr, &ucp->tuc_regs))
5007 return 1;
5009 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
5010 return 1;
5012 target_to_host_sigset_internal(&blocked, &set);
5013 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
5014 if (restore_user_regs(env, mcp, sig))
5015 goto sigsegv;
5017 unlock_user_struct(mcp, mcp_addr, 1);
5018 return 0;
5020 sigsegv:
5021 unlock_user_struct(mcp, mcp_addr, 1);
5022 return 1;
5023 #endif
5026 long do_rt_sigreturn(CPUPPCState *env)
5028 struct target_rt_sigframe *rt_sf = NULL;
5029 target_ulong rt_sf_addr;
5031 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
5032 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
5033 goto sigsegv;
5035 if (do_setcontext(&rt_sf->uc, env, 1))
5036 goto sigsegv;
5038 do_sigaltstack(rt_sf_addr
5039 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
5040 0, env->gpr[1]);
5042 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5043 return -TARGET_QEMU_ESIGRETURN;
5045 sigsegv:
5046 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5047 qemu_log("segfaulting from do_rt_sigreturn\n");
5048 force_sig(TARGET_SIGSEGV);
5049 return 0;
5052 #elif defined(TARGET_M68K)
5054 struct target_sigcontext {
5055 abi_ulong sc_mask;
5056 abi_ulong sc_usp;
5057 abi_ulong sc_d0;
5058 abi_ulong sc_d1;
5059 abi_ulong sc_a0;
5060 abi_ulong sc_a1;
5061 unsigned short sc_sr;
5062 abi_ulong sc_pc;
5065 struct target_sigframe
5067 abi_ulong pretcode;
5068 int sig;
5069 int code;
5070 abi_ulong psc;
5071 char retcode[8];
5072 abi_ulong extramask[TARGET_NSIG_WORDS-1];
5073 struct target_sigcontext sc;
5076 typedef int target_greg_t;
5077 #define TARGET_NGREG 18
5078 typedef target_greg_t target_gregset_t[TARGET_NGREG];
5080 typedef struct target_fpregset {
5081 int f_fpcntl[3];
5082 int f_fpregs[8*3];
5083 } target_fpregset_t;
5085 struct target_mcontext {
5086 int version;
5087 target_gregset_t gregs;
5088 target_fpregset_t fpregs;
5091 #define TARGET_MCONTEXT_VERSION 2
5093 struct target_ucontext {
5094 abi_ulong tuc_flags;
5095 abi_ulong tuc_link;
5096 target_stack_t tuc_stack;
5097 struct target_mcontext tuc_mcontext;
5098 abi_long tuc_filler[80];
5099 target_sigset_t tuc_sigmask;
5102 struct target_rt_sigframe
5104 abi_ulong pretcode;
5105 int sig;
5106 abi_ulong pinfo;
5107 abi_ulong puc;
5108 char retcode[8];
5109 struct target_siginfo info;
5110 struct target_ucontext uc;
5113 static int
5114 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
5115 abi_ulong mask)
5117 int err = 0;
5119 err |= __put_user(mask, &sc->sc_mask);
5120 err |= __put_user(env->aregs[7], &sc->sc_usp);
5121 err |= __put_user(env->dregs[0], &sc->sc_d0);
5122 err |= __put_user(env->dregs[1], &sc->sc_d1);
5123 err |= __put_user(env->aregs[0], &sc->sc_a0);
5124 err |= __put_user(env->aregs[1], &sc->sc_a1);
5125 err |= __put_user(env->sr, &sc->sc_sr);
5126 err |= __put_user(env->pc, &sc->sc_pc);
5128 return err;
5131 static int
5132 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
5134 int err = 0;
5135 int temp;
5137 err |= __get_user(env->aregs[7], &sc->sc_usp);
5138 err |= __get_user(env->dregs[1], &sc->sc_d1);
5139 err |= __get_user(env->aregs[0], &sc->sc_a0);
5140 err |= __get_user(env->aregs[1], &sc->sc_a1);
5141 err |= __get_user(env->pc, &sc->sc_pc);
5142 err |= __get_user(temp, &sc->sc_sr);
5143 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5145 *pd0 = tswapl(sc->sc_d0);
5147 return err;
5151 * Determine which stack to use..
5153 static inline abi_ulong
5154 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5155 size_t frame_size)
5157 unsigned long sp;
5159 sp = regs->aregs[7];
5161 /* This is the X/Open sanctioned signal stack switching. */
5162 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5163 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5166 return ((sp - frame_size) & -8UL);
5169 static void setup_frame(int sig, struct target_sigaction *ka,
5170 target_sigset_t *set, CPUM68KState *env)
5172 struct target_sigframe *frame;
5173 abi_ulong frame_addr;
5174 abi_ulong retcode_addr;
5175 abi_ulong sc_addr;
5176 int err = 0;
5177 int i;
5179 frame_addr = get_sigframe(ka, env, sizeof *frame);
5180 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5181 goto give_sigsegv;
5183 err |= __put_user(sig, &frame->sig);
5185 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5186 err |= __put_user(sc_addr, &frame->psc);
5188 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
5189 if (err)
5190 goto give_sigsegv;
5192 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5193 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
5194 goto give_sigsegv;
5197 /* Set up to return from userspace. */
5199 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5200 err |= __put_user(retcode_addr, &frame->pretcode);
5202 /* moveq #,d0; trap #0 */
5204 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5205 (long *)(frame->retcode));
5207 if (err)
5208 goto give_sigsegv;
5210 /* Set up to return from userspace */
5212 env->aregs[7] = frame_addr;
5213 env->pc = ka->_sa_handler;
5215 unlock_user_struct(frame, frame_addr, 1);
5216 return;
5218 give_sigsegv:
5219 unlock_user_struct(frame, frame_addr, 1);
5220 force_sig(TARGET_SIGSEGV);
5223 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5224 CPUM68KState *env)
5226 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5227 int err;
5229 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5230 err |= __put_user(env->dregs[0], &gregs[0]);
5231 err |= __put_user(env->dregs[1], &gregs[1]);
5232 err |= __put_user(env->dregs[2], &gregs[2]);
5233 err |= __put_user(env->dregs[3], &gregs[3]);
5234 err |= __put_user(env->dregs[4], &gregs[4]);
5235 err |= __put_user(env->dregs[5], &gregs[5]);
5236 err |= __put_user(env->dregs[6], &gregs[6]);
5237 err |= __put_user(env->dregs[7], &gregs[7]);
5238 err |= __put_user(env->aregs[0], &gregs[8]);
5239 err |= __put_user(env->aregs[1], &gregs[9]);
5240 err |= __put_user(env->aregs[2], &gregs[10]);
5241 err |= __put_user(env->aregs[3], &gregs[11]);
5242 err |= __put_user(env->aregs[4], &gregs[12]);
5243 err |= __put_user(env->aregs[5], &gregs[13]);
5244 err |= __put_user(env->aregs[6], &gregs[14]);
5245 err |= __put_user(env->aregs[7], &gregs[15]);
5246 err |= __put_user(env->pc, &gregs[16]);
5247 err |= __put_user(env->sr, &gregs[17]);
5249 return err;
5252 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5253 struct target_ucontext *uc,
5254 int *pd0)
5256 int temp;
5257 int err;
5258 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5260 err = __get_user(temp, &uc->tuc_mcontext.version);
5261 if (temp != TARGET_MCONTEXT_VERSION)
5262 goto badframe;
5264 /* restore passed registers */
5265 err |= __get_user(env->dregs[0], &gregs[0]);
5266 err |= __get_user(env->dregs[1], &gregs[1]);
5267 err |= __get_user(env->dregs[2], &gregs[2]);
5268 err |= __get_user(env->dregs[3], &gregs[3]);
5269 err |= __get_user(env->dregs[4], &gregs[4]);
5270 err |= __get_user(env->dregs[5], &gregs[5]);
5271 err |= __get_user(env->dregs[6], &gregs[6]);
5272 err |= __get_user(env->dregs[7], &gregs[7]);
5273 err |= __get_user(env->aregs[0], &gregs[8]);
5274 err |= __get_user(env->aregs[1], &gregs[9]);
5275 err |= __get_user(env->aregs[2], &gregs[10]);
5276 err |= __get_user(env->aregs[3], &gregs[11]);
5277 err |= __get_user(env->aregs[4], &gregs[12]);
5278 err |= __get_user(env->aregs[5], &gregs[13]);
5279 err |= __get_user(env->aregs[6], &gregs[14]);
5280 err |= __get_user(env->aregs[7], &gregs[15]);
5281 err |= __get_user(env->pc, &gregs[16]);
5282 err |= __get_user(temp, &gregs[17]);
5283 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5285 *pd0 = env->dregs[0];
5286 return err;
5288 badframe:
5289 return 1;
5292 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5293 target_siginfo_t *info,
5294 target_sigset_t *set, CPUM68KState *env)
5296 struct target_rt_sigframe *frame;
5297 abi_ulong frame_addr;
5298 abi_ulong retcode_addr;
5299 abi_ulong info_addr;
5300 abi_ulong uc_addr;
5301 int err = 0;
5302 int i;
5304 frame_addr = get_sigframe(ka, env, sizeof *frame);
5305 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5306 goto give_sigsegv;
5308 err |= __put_user(sig, &frame->sig);
5310 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5311 err |= __put_user(info_addr, &frame->pinfo);
5313 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5314 err |= __put_user(uc_addr, &frame->puc);
5316 err |= copy_siginfo_to_user(&frame->info, info);
5318 /* Create the ucontext */
5320 err |= __put_user(0, &frame->uc.tuc_flags);
5321 err |= __put_user(0, &frame->uc.tuc_link);
5322 err |= __put_user(target_sigaltstack_used.ss_sp,
5323 &frame->uc.tuc_stack.ss_sp);
5324 err |= __put_user(sas_ss_flags(env->aregs[7]),
5325 &frame->uc.tuc_stack.ss_flags);
5326 err |= __put_user(target_sigaltstack_used.ss_size,
5327 &frame->uc.tuc_stack.ss_size);
5328 err |= target_rt_setup_ucontext(&frame->uc, env);
5330 if (err)
5331 goto give_sigsegv;
5333 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5334 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
5335 goto give_sigsegv;
5338 /* Set up to return from userspace. */
5340 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5341 err |= __put_user(retcode_addr, &frame->pretcode);
5343 /* moveq #,d0; notb d0; trap #0 */
5345 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5346 (long *)(frame->retcode + 0));
5347 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5349 if (err)
5350 goto give_sigsegv;
5352 /* Set up to return from userspace */
5354 env->aregs[7] = frame_addr;
5355 env->pc = ka->_sa_handler;
5357 unlock_user_struct(frame, frame_addr, 1);
5358 return;
5360 give_sigsegv:
5361 unlock_user_struct(frame, frame_addr, 1);
5362 force_sig(TARGET_SIGSEGV);
5365 long do_sigreturn(CPUM68KState *env)
5367 struct target_sigframe *frame;
5368 abi_ulong frame_addr = env->aregs[7] - 4;
5369 target_sigset_t target_set;
5370 sigset_t set;
5371 int d0, i;
5373 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5374 goto badframe;
5376 /* set blocked signals */
5378 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5379 goto badframe;
5381 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5382 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5383 goto badframe;
5386 target_to_host_sigset_internal(&set, &target_set);
5387 do_sigprocmask(SIG_SETMASK, &set, NULL);
5389 /* restore registers */
5391 if (restore_sigcontext(env, &frame->sc, &d0))
5392 goto badframe;
5394 unlock_user_struct(frame, frame_addr, 0);
5395 return d0;
5397 badframe:
5398 unlock_user_struct(frame, frame_addr, 0);
5399 force_sig(TARGET_SIGSEGV);
5400 return 0;
5403 long do_rt_sigreturn(CPUM68KState *env)
5405 struct target_rt_sigframe *frame;
5406 abi_ulong frame_addr = env->aregs[7] - 4;
5407 target_sigset_t target_set;
5408 sigset_t set;
5409 int d0;
5411 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5412 goto badframe;
5414 target_to_host_sigset_internal(&set, &target_set);
5415 do_sigprocmask(SIG_SETMASK, &set, NULL);
5417 /* restore registers */
5419 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5420 goto badframe;
5422 if (do_sigaltstack(frame_addr +
5423 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5424 0, get_sp_from_cpustate(env)) == -EFAULT)
5425 goto badframe;
5427 unlock_user_struct(frame, frame_addr, 0);
5428 return d0;
5430 badframe:
5431 unlock_user_struct(frame, frame_addr, 0);
5432 force_sig(TARGET_SIGSEGV);
5433 return 0;
5436 #elif defined(TARGET_ALPHA)
5438 struct target_sigcontext {
5439 abi_long sc_onstack;
5440 abi_long sc_mask;
5441 abi_long sc_pc;
5442 abi_long sc_ps;
5443 abi_long sc_regs[32];
5444 abi_long sc_ownedfp;
5445 abi_long sc_fpregs[32];
5446 abi_ulong sc_fpcr;
5447 abi_ulong sc_fp_control;
5448 abi_ulong sc_reserved1;
5449 abi_ulong sc_reserved2;
5450 abi_ulong sc_ssize;
5451 abi_ulong sc_sbase;
5452 abi_ulong sc_traparg_a0;
5453 abi_ulong sc_traparg_a1;
5454 abi_ulong sc_traparg_a2;
5455 abi_ulong sc_fp_trap_pc;
5456 abi_ulong sc_fp_trigger_sum;
5457 abi_ulong sc_fp_trigger_inst;
5460 struct target_ucontext {
5461 abi_ulong tuc_flags;
5462 abi_ulong tuc_link;
5463 abi_ulong tuc_osf_sigmask;
5464 target_stack_t tuc_stack;
5465 struct target_sigcontext tuc_mcontext;
5466 target_sigset_t tuc_sigmask;
5469 struct target_sigframe {
5470 struct target_sigcontext sc;
5471 unsigned int retcode[3];
5474 struct target_rt_sigframe {
5475 target_siginfo_t info;
5476 struct target_ucontext uc;
5477 unsigned int retcode[3];
5480 #define INSN_MOV_R30_R16 0x47fe0410
5481 #define INSN_LDI_R0 0x201f0000
5482 #define INSN_CALLSYS 0x00000083
5484 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5485 abi_ulong frame_addr, target_sigset_t *set)
5487 int i, err = 0;
5489 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5490 err |= __put_user(set->sig[0], &sc->sc_mask);
5491 err |= __put_user(env->pc, &sc->sc_pc);
5492 err |= __put_user(8, &sc->sc_ps);
5494 for (i = 0; i < 31; ++i) {
5495 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5497 err |= __put_user(0, &sc->sc_regs[31]);
5499 for (i = 0; i < 31; ++i) {
5500 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5502 err |= __put_user(0, &sc->sc_fpregs[31]);
5503 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5505 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5506 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5507 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5509 return err;
5512 static int restore_sigcontext(CPUAlphaState *env,
5513 struct target_sigcontext *sc)
5515 uint64_t fpcr;
5516 int i, err = 0;
5518 err |= __get_user(env->pc, &sc->sc_pc);
5520 for (i = 0; i < 31; ++i) {
5521 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5523 for (i = 0; i < 31; ++i) {
5524 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5527 err |= __get_user(fpcr, &sc->sc_fpcr);
5528 cpu_alpha_store_fpcr(env, fpcr);
5530 return err;
5533 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5534 CPUAlphaState *env,
5535 unsigned long framesize)
5537 abi_ulong sp = env->ir[IR_SP];
5539 /* This is the X/Open sanctioned signal stack switching. */
5540 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5541 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5543 return (sp - framesize) & -32;
5546 static void setup_frame(int sig, struct target_sigaction *ka,
5547 target_sigset_t *set, CPUAlphaState *env)
5549 abi_ulong frame_addr, r26;
5550 struct target_sigframe *frame;
5551 int err = 0;
5553 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5554 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5555 goto give_sigsegv;
5558 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5560 if (ka->sa_restorer) {
5561 r26 = ka->sa_restorer;
5562 } else {
5563 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5564 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5565 &frame->retcode[1]);
5566 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5567 /* imb() */
5568 r26 = frame_addr;
5571 unlock_user_struct(frame, frame_addr, 1);
5573 if (err) {
5574 give_sigsegv:
5575 if (sig == TARGET_SIGSEGV) {
5576 ka->_sa_handler = TARGET_SIG_DFL;
5578 force_sig(TARGET_SIGSEGV);
5581 env->ir[IR_RA] = r26;
5582 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5583 env->ir[IR_A0] = sig;
5584 env->ir[IR_A1] = 0;
5585 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5586 env->ir[IR_SP] = frame_addr;
5589 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5590 target_siginfo_t *info,
5591 target_sigset_t *set, CPUAlphaState *env)
5593 abi_ulong frame_addr, r26;
5594 struct target_rt_sigframe *frame;
5595 int i, err = 0;
5597 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5598 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5599 goto give_sigsegv;
5602 err |= copy_siginfo_to_user(&frame->info, info);
5604 err |= __put_user(0, &frame->uc.tuc_flags);
5605 err |= __put_user(0, &frame->uc.tuc_link);
5606 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5607 err |= __put_user(target_sigaltstack_used.ss_sp,
5608 &frame->uc.tuc_stack.ss_sp);
5609 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5610 &frame->uc.tuc_stack.ss_flags);
5611 err |= __put_user(target_sigaltstack_used.ss_size,
5612 &frame->uc.tuc_stack.ss_size);
5613 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5614 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5615 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5618 if (ka->sa_restorer) {
5619 r26 = ka->sa_restorer;
5620 } else {
5621 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5622 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5623 &frame->retcode[1]);
5624 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5625 /* imb(); */
5626 r26 = frame_addr;
5629 if (err) {
5630 give_sigsegv:
5631 if (sig == TARGET_SIGSEGV) {
5632 ka->_sa_handler = TARGET_SIG_DFL;
5634 force_sig(TARGET_SIGSEGV);
5637 env->ir[IR_RA] = r26;
5638 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5639 env->ir[IR_A0] = sig;
5640 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5641 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5642 env->ir[IR_SP] = frame_addr;
5645 long do_sigreturn(CPUAlphaState *env)
5647 struct target_sigcontext *sc;
5648 abi_ulong sc_addr = env->ir[IR_A0];
5649 target_sigset_t target_set;
5650 sigset_t set;
5652 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5653 goto badframe;
5656 target_sigemptyset(&target_set);
5657 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5658 goto badframe;
5661 target_to_host_sigset_internal(&set, &target_set);
5662 do_sigprocmask(SIG_SETMASK, &set, NULL);
5664 if (restore_sigcontext(env, sc)) {
5665 goto badframe;
5667 unlock_user_struct(sc, sc_addr, 0);
5668 return env->ir[IR_V0];
5670 badframe:
5671 unlock_user_struct(sc, sc_addr, 0);
5672 force_sig(TARGET_SIGSEGV);
5675 long do_rt_sigreturn(CPUAlphaState *env)
5677 abi_ulong frame_addr = env->ir[IR_A0];
5678 struct target_rt_sigframe *frame;
5679 sigset_t set;
5681 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5682 goto badframe;
5684 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5685 do_sigprocmask(SIG_SETMASK, &set, NULL);
5687 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5688 goto badframe;
5690 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5691 uc.tuc_stack),
5692 0, env->ir[IR_SP]) == -EFAULT) {
5693 goto badframe;
5696 unlock_user_struct(frame, frame_addr, 0);
5697 return env->ir[IR_V0];
5700 badframe:
5701 unlock_user_struct(frame, frame_addr, 0);
5702 force_sig(TARGET_SIGSEGV);
5705 #else
5707 static void setup_frame(int sig, struct target_sigaction *ka,
5708 target_sigset_t *set, CPUArchState *env)
5710 fprintf(stderr, "setup_frame: not implemented\n");
5713 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5714 target_siginfo_t *info,
5715 target_sigset_t *set, CPUArchState *env)
5717 fprintf(stderr, "setup_rt_frame: not implemented\n");
5720 long do_sigreturn(CPUArchState *env)
5722 fprintf(stderr, "do_sigreturn: not implemented\n");
5723 return -TARGET_ENOSYS;
5726 long do_rt_sigreturn(CPUArchState *env)
5728 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5729 return -TARGET_ENOSYS;
5732 #endif
5734 void process_pending_signals(CPUArchState *cpu_env)
5736 CPUState *cpu = ENV_GET_CPU(cpu_env);
5737 int sig;
5738 abi_ulong handler;
5739 sigset_t set, old_set;
5740 target_sigset_t target_old_set;
5741 struct emulated_sigtable *k;
5742 struct target_sigaction *sa;
5743 struct sigqueue *q;
5744 TaskState *ts = cpu->opaque;
5746 if (!ts->signal_pending)
5747 return;
5749 /* FIXME: This is not threadsafe. */
5750 k = ts->sigtab;
5751 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5752 if (k->pending)
5753 goto handle_signal;
5754 k++;
5756 /* if no signal is pending, just return */
5757 ts->signal_pending = 0;
5758 return;
5760 handle_signal:
5761 #ifdef DEBUG_SIGNAL
5762 fprintf(stderr, "qemu: process signal %d\n", sig);
5763 #endif
5764 /* dequeue signal */
5765 q = k->first;
5766 k->first = q->next;
5767 if (!k->first)
5768 k->pending = 0;
5770 sig = gdb_handlesig(cpu, sig);
5771 if (!sig) {
5772 sa = NULL;
5773 handler = TARGET_SIG_IGN;
5774 } else {
5775 sa = &sigact_table[sig - 1];
5776 handler = sa->_sa_handler;
5779 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5780 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5781 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5782 * because it got a real MMU fault), and treat as if default handler.
5784 handler = TARGET_SIG_DFL;
5787 if (handler == TARGET_SIG_DFL) {
5788 /* default handler : ignore some signal. The other are job control or fatal */
5789 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5790 kill(getpid(),SIGSTOP);
5791 } else if (sig != TARGET_SIGCHLD &&
5792 sig != TARGET_SIGURG &&
5793 sig != TARGET_SIGWINCH &&
5794 sig != TARGET_SIGCONT) {
5795 force_sig(sig);
5797 } else if (handler == TARGET_SIG_IGN) {
5798 /* ignore sig */
5799 } else if (handler == TARGET_SIG_ERR) {
5800 force_sig(sig);
5801 } else {
5802 /* compute the blocked signals during the handler execution */
5803 target_to_host_sigset(&set, &sa->sa_mask);
5804 /* SA_NODEFER indicates that the current signal should not be
5805 blocked during the handler */
5806 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5807 sigaddset(&set, target_to_host_signal(sig));
5809 /* block signals in the handler using Linux */
5810 do_sigprocmask(SIG_BLOCK, &set, &old_set);
5811 /* save the previous blocked signal state to restore it at the
5812 end of the signal execution (see do_sigreturn) */
5813 host_to_target_sigset_internal(&target_old_set, &old_set);
5815 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5816 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5818 CPUX86State *env = cpu_env;
5819 if (env->eflags & VM_MASK)
5820 save_v86_state(env);
5822 #endif
5823 /* prepare the stack frame of the virtual CPU */
5824 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5825 /* These targets do not have traditional signals. */
5826 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5827 #else
5828 if (sa->sa_flags & TARGET_SA_SIGINFO)
5829 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5830 else
5831 setup_frame(sig, sa, &target_old_set, cpu_env);
5832 #endif
5833 if (sa->sa_flags & TARGET_SA_RESETHAND)
5834 sa->_sa_handler = TARGET_SIG_DFL;
5836 if (q != &k->info)
5837 free_sigqueue(cpu_env, q);