exec: Change cpu_watchpoint_{insert,remove{,_by_ref,_all}} argument
[qemu/kevin.git] / linux-user / signal.c
blob24c91f37d9dc8b2bb5b256272c70413b068c320f
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 /* siginfo conversion */
202 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
203 const siginfo_t *info)
205 int sig = host_to_target_signal(info->si_signo);
206 tinfo->si_signo = sig;
207 tinfo->si_errno = 0;
208 tinfo->si_code = info->si_code;
210 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
211 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
212 /* Should never come here, but who knows. The information for
213 the target is irrelevant. */
214 tinfo->_sifields._sigfault._addr = 0;
215 } else if (sig == TARGET_SIGIO) {
216 tinfo->_sifields._sigpoll._band = info->si_band;
217 tinfo->_sifields._sigpoll._fd = info->si_fd;
218 } else if (sig == TARGET_SIGCHLD) {
219 tinfo->_sifields._sigchld._pid = info->si_pid;
220 tinfo->_sifields._sigchld._uid = info->si_uid;
221 tinfo->_sifields._sigchld._status
222 = host_to_target_waitstatus(info->si_status);
223 tinfo->_sifields._sigchld._utime = info->si_utime;
224 tinfo->_sifields._sigchld._stime = info->si_stime;
225 } else if (sig >= TARGET_SIGRTMIN) {
226 tinfo->_sifields._rt._pid = info->si_pid;
227 tinfo->_sifields._rt._uid = info->si_uid;
228 /* XXX: potential problem if 64 bit */
229 tinfo->_sifields._rt._sigval.sival_ptr
230 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
234 static void tswap_siginfo(target_siginfo_t *tinfo,
235 const target_siginfo_t *info)
237 int sig = info->si_signo;
238 tinfo->si_signo = tswap32(sig);
239 tinfo->si_errno = tswap32(info->si_errno);
240 tinfo->si_code = tswap32(info->si_code);
242 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
243 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
244 tinfo->_sifields._sigfault._addr
245 = tswapal(info->_sifields._sigfault._addr);
246 } else if (sig == TARGET_SIGIO) {
247 tinfo->_sifields._sigpoll._band
248 = tswap32(info->_sifields._sigpoll._band);
249 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
250 } else if (sig == TARGET_SIGCHLD) {
251 tinfo->_sifields._sigchld._pid
252 = tswap32(info->_sifields._sigchld._pid);
253 tinfo->_sifields._sigchld._uid
254 = tswap32(info->_sifields._sigchld._uid);
255 tinfo->_sifields._sigchld._status
256 = tswap32(info->_sifields._sigchld._status);
257 tinfo->_sifields._sigchld._utime
258 = tswapal(info->_sifields._sigchld._utime);
259 tinfo->_sifields._sigchld._stime
260 = tswapal(info->_sifields._sigchld._stime);
261 } else if (sig >= TARGET_SIGRTMIN) {
262 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
263 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
264 tinfo->_sifields._rt._sigval.sival_ptr
265 = tswapal(info->_sifields._rt._sigval.sival_ptr);
270 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
272 host_to_target_siginfo_noswap(tinfo, info);
273 tswap_siginfo(tinfo, tinfo);
276 /* XXX: we support only POSIX RT signals are used. */
277 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
278 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
280 info->si_signo = tswap32(tinfo->si_signo);
281 info->si_errno = tswap32(tinfo->si_errno);
282 info->si_code = tswap32(tinfo->si_code);
283 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
284 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
285 info->si_value.sival_ptr =
286 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
289 static int fatal_signal (int sig)
291 switch (sig) {
292 case TARGET_SIGCHLD:
293 case TARGET_SIGURG:
294 case TARGET_SIGWINCH:
295 /* Ignored by default. */
296 return 0;
297 case TARGET_SIGCONT:
298 case TARGET_SIGSTOP:
299 case TARGET_SIGTSTP:
300 case TARGET_SIGTTIN:
301 case TARGET_SIGTTOU:
302 /* Job control signals. */
303 return 0;
304 default:
305 return 1;
309 /* returns 1 if given signal should dump core if not handled */
310 static int core_dump_signal(int sig)
312 switch (sig) {
313 case TARGET_SIGABRT:
314 case TARGET_SIGFPE:
315 case TARGET_SIGILL:
316 case TARGET_SIGQUIT:
317 case TARGET_SIGSEGV:
318 case TARGET_SIGTRAP:
319 case TARGET_SIGBUS:
320 return (1);
321 default:
322 return (0);
326 void signal_init(void)
328 struct sigaction act;
329 struct sigaction oact;
330 int i, j;
331 int host_sig;
333 /* generate signal conversion tables */
334 for(i = 1; i < _NSIG; i++) {
335 if (host_to_target_signal_table[i] == 0)
336 host_to_target_signal_table[i] = i;
338 for(i = 1; i < _NSIG; i++) {
339 j = host_to_target_signal_table[i];
340 target_to_host_signal_table[j] = i;
343 /* set all host signal handlers. ALL signals are blocked during
344 the handlers to serialize them. */
345 memset(sigact_table, 0, sizeof(sigact_table));
347 sigfillset(&act.sa_mask);
348 act.sa_flags = SA_SIGINFO;
349 act.sa_sigaction = host_signal_handler;
350 for(i = 1; i <= TARGET_NSIG; i++) {
351 host_sig = target_to_host_signal(i);
352 sigaction(host_sig, NULL, &oact);
353 if (oact.sa_sigaction == (void *)SIG_IGN) {
354 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
355 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
356 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
358 /* If there's already a handler installed then something has
359 gone horribly wrong, so don't even try to handle that case. */
360 /* Install some handlers for our own use. We need at least
361 SIGSEGV and SIGBUS, to detect exceptions. We can not just
362 trap all signals because it affects syscall interrupt
363 behavior. But do trap all default-fatal signals. */
364 if (fatal_signal (i))
365 sigaction(host_sig, &act, NULL);
369 /* signal queue handling */
371 static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
373 CPUState *cpu = ENV_GET_CPU(env);
374 TaskState *ts = cpu->opaque;
375 struct sigqueue *q = ts->first_free;
376 if (!q)
377 return NULL;
378 ts->first_free = q->next;
379 return q;
382 static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
384 CPUState *cpu = ENV_GET_CPU(env);
385 TaskState *ts = cpu->opaque;
387 q->next = ts->first_free;
388 ts->first_free = q;
391 /* abort execution with signal */
392 static void QEMU_NORETURN force_sig(int target_sig)
394 CPUState *cpu = thread_cpu;
395 CPUArchState *env = cpu->env_ptr;
396 TaskState *ts = (TaskState *)cpu->opaque;
397 int host_sig, core_dumped = 0;
398 struct sigaction act;
399 host_sig = target_to_host_signal(target_sig);
400 gdb_signalled(env, target_sig);
402 /* dump core if supported by target binary format */
403 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
404 stop_all_tasks();
405 core_dumped =
406 ((*ts->bprm->core_dump)(target_sig, env) == 0);
408 if (core_dumped) {
409 /* we already dumped the core of target process, we don't want
410 * a coredump of qemu itself */
411 struct rlimit nodump;
412 getrlimit(RLIMIT_CORE, &nodump);
413 nodump.rlim_cur=0;
414 setrlimit(RLIMIT_CORE, &nodump);
415 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
416 target_sig, strsignal(host_sig), "core dumped" );
419 /* The proper exit code for dying from an uncaught signal is
420 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
421 * a negative value. To get the proper exit code we need to
422 * actually die from an uncaught signal. Here the default signal
423 * handler is installed, we send ourself a signal and we wait for
424 * it to arrive. */
425 sigfillset(&act.sa_mask);
426 act.sa_handler = SIG_DFL;
427 act.sa_flags = 0;
428 sigaction(host_sig, &act, NULL);
430 /* For some reason raise(host_sig) doesn't send the signal when
431 * statically linked on x86-64. */
432 kill(getpid(), host_sig);
434 /* Make sure the signal isn't masked (just reuse the mask inside
435 of act) */
436 sigdelset(&act.sa_mask, host_sig);
437 sigsuspend(&act.sa_mask);
439 /* unreachable */
440 abort();
443 /* queue a signal so that it will be send to the virtual CPU as soon
444 as possible */
445 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
447 CPUState *cpu = ENV_GET_CPU(env);
448 TaskState *ts = cpu->opaque;
449 struct emulated_sigtable *k;
450 struct sigqueue *q, **pq;
451 abi_ulong handler;
452 int queue;
454 #if defined(DEBUG_SIGNAL)
455 fprintf(stderr, "queue_signal: sig=%d\n",
456 sig);
457 #endif
458 k = &ts->sigtab[sig - 1];
459 queue = gdb_queuesig ();
460 handler = sigact_table[sig - 1]._sa_handler;
461 if (!queue && handler == TARGET_SIG_DFL) {
462 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
463 kill(getpid(),SIGSTOP);
464 return 0;
465 } else
466 /* default handler : ignore some signal. The other are fatal */
467 if (sig != TARGET_SIGCHLD &&
468 sig != TARGET_SIGURG &&
469 sig != TARGET_SIGWINCH &&
470 sig != TARGET_SIGCONT) {
471 force_sig(sig);
472 } else {
473 return 0; /* indicate ignored */
475 } else if (!queue && handler == TARGET_SIG_IGN) {
476 /* ignore signal */
477 return 0;
478 } else if (!queue && handler == TARGET_SIG_ERR) {
479 force_sig(sig);
480 } else {
481 pq = &k->first;
482 if (sig < TARGET_SIGRTMIN) {
483 /* if non real time signal, we queue exactly one signal */
484 if (!k->pending)
485 q = &k->info;
486 else
487 return 0;
488 } else {
489 if (!k->pending) {
490 /* first signal */
491 q = &k->info;
492 } else {
493 q = alloc_sigqueue(env);
494 if (!q)
495 return -EAGAIN;
496 while (*pq != NULL)
497 pq = &(*pq)->next;
500 *pq = q;
501 q->info = *info;
502 q->next = NULL;
503 k->pending = 1;
504 /* signal that a new signal is pending */
505 ts->signal_pending = 1;
506 return 1; /* indicates that the signal was queued */
510 static void host_signal_handler(int host_signum, siginfo_t *info,
511 void *puc)
513 CPUArchState *env = thread_cpu->env_ptr;
514 int sig;
515 target_siginfo_t tinfo;
517 /* the CPU emulator uses some host signals to detect exceptions,
518 we forward to it some signals */
519 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
520 && info->si_code > 0) {
521 if (cpu_signal_handler(host_signum, info, puc))
522 return;
525 /* get target signal number */
526 sig = host_to_target_signal(host_signum);
527 if (sig < 1 || sig > TARGET_NSIG)
528 return;
529 #if defined(DEBUG_SIGNAL)
530 fprintf(stderr, "qemu: got signal %d\n", sig);
531 #endif
532 host_to_target_siginfo_noswap(&tinfo, info);
533 if (queue_signal(env, sig, &tinfo) == 1) {
534 /* interrupt the virtual CPU as soon as possible */
535 cpu_exit(thread_cpu);
539 /* do_sigaltstack() returns target values and errnos. */
540 /* compare linux/kernel/signal.c:do_sigaltstack() */
541 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
543 int ret;
544 struct target_sigaltstack oss;
546 /* XXX: test errors */
547 if(uoss_addr)
549 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
550 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
551 __put_user(sas_ss_flags(sp), &oss.ss_flags);
554 if(uss_addr)
556 struct target_sigaltstack *uss;
557 struct target_sigaltstack ss;
559 ret = -TARGET_EFAULT;
560 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
561 || __get_user(ss.ss_sp, &uss->ss_sp)
562 || __get_user(ss.ss_size, &uss->ss_size)
563 || __get_user(ss.ss_flags, &uss->ss_flags))
564 goto out;
565 unlock_user_struct(uss, uss_addr, 0);
567 ret = -TARGET_EPERM;
568 if (on_sig_stack(sp))
569 goto out;
571 ret = -TARGET_EINVAL;
572 if (ss.ss_flags != TARGET_SS_DISABLE
573 && ss.ss_flags != TARGET_SS_ONSTACK
574 && ss.ss_flags != 0)
575 goto out;
577 if (ss.ss_flags == TARGET_SS_DISABLE) {
578 ss.ss_size = 0;
579 ss.ss_sp = 0;
580 } else {
581 ret = -TARGET_ENOMEM;
582 if (ss.ss_size < MINSIGSTKSZ)
583 goto out;
586 target_sigaltstack_used.ss_sp = ss.ss_sp;
587 target_sigaltstack_used.ss_size = ss.ss_size;
590 if (uoss_addr) {
591 ret = -TARGET_EFAULT;
592 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
593 goto out;
596 ret = 0;
597 out:
598 return ret;
601 /* do_sigaction() return host values and errnos */
602 int do_sigaction(int sig, const struct target_sigaction *act,
603 struct target_sigaction *oact)
605 struct target_sigaction *k;
606 struct sigaction act1;
607 int host_sig;
608 int ret = 0;
610 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
611 return -EINVAL;
612 k = &sigact_table[sig - 1];
613 #if defined(DEBUG_SIGNAL)
614 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
615 sig, act, oact);
616 #endif
617 if (oact) {
618 __put_user(k->_sa_handler, &oact->_sa_handler);
619 __put_user(k->sa_flags, &oact->sa_flags);
620 #if !defined(TARGET_MIPS)
621 __put_user(k->sa_restorer, &oact->sa_restorer);
622 #endif
623 /* Not swapped. */
624 oact->sa_mask = k->sa_mask;
626 if (act) {
627 /* FIXME: This is not threadsafe. */
628 __get_user(k->_sa_handler, &act->_sa_handler);
629 __get_user(k->sa_flags, &act->sa_flags);
630 #if !defined(TARGET_MIPS)
631 __get_user(k->sa_restorer, &act->sa_restorer);
632 #endif
633 /* To be swapped in target_to_host_sigset. */
634 k->sa_mask = act->sa_mask;
636 /* we update the host linux signal state */
637 host_sig = target_to_host_signal(sig);
638 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
639 sigfillset(&act1.sa_mask);
640 act1.sa_flags = SA_SIGINFO;
641 if (k->sa_flags & TARGET_SA_RESTART)
642 act1.sa_flags |= SA_RESTART;
643 /* NOTE: it is important to update the host kernel signal
644 ignore state to avoid getting unexpected interrupted
645 syscalls */
646 if (k->_sa_handler == TARGET_SIG_IGN) {
647 act1.sa_sigaction = (void *)SIG_IGN;
648 } else if (k->_sa_handler == TARGET_SIG_DFL) {
649 if (fatal_signal (sig))
650 act1.sa_sigaction = host_signal_handler;
651 else
652 act1.sa_sigaction = (void *)SIG_DFL;
653 } else {
654 act1.sa_sigaction = host_signal_handler;
656 ret = sigaction(host_sig, &act1, NULL);
659 return ret;
662 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
663 const target_siginfo_t *info)
665 tswap_siginfo(tinfo, info);
666 return 0;
669 static inline int current_exec_domain_sig(int sig)
671 return /* current->exec_domain && current->exec_domain->signal_invmap
672 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
675 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
677 /* from the Linux kernel */
679 struct target_fpreg {
680 uint16_t significand[4];
681 uint16_t exponent;
684 struct target_fpxreg {
685 uint16_t significand[4];
686 uint16_t exponent;
687 uint16_t padding[3];
690 struct target_xmmreg {
691 abi_ulong element[4];
694 struct target_fpstate {
695 /* Regular FPU environment */
696 abi_ulong cw;
697 abi_ulong sw;
698 abi_ulong tag;
699 abi_ulong ipoff;
700 abi_ulong cssel;
701 abi_ulong dataoff;
702 abi_ulong datasel;
703 struct target_fpreg _st[8];
704 uint16_t status;
705 uint16_t magic; /* 0xffff = regular FPU data only */
707 /* FXSR FPU environment */
708 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
709 abi_ulong mxcsr;
710 abi_ulong reserved;
711 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
712 struct target_xmmreg _xmm[8];
713 abi_ulong padding[56];
716 #define X86_FXSR_MAGIC 0x0000
718 struct target_sigcontext {
719 uint16_t gs, __gsh;
720 uint16_t fs, __fsh;
721 uint16_t es, __esh;
722 uint16_t ds, __dsh;
723 abi_ulong edi;
724 abi_ulong esi;
725 abi_ulong ebp;
726 abi_ulong esp;
727 abi_ulong ebx;
728 abi_ulong edx;
729 abi_ulong ecx;
730 abi_ulong eax;
731 abi_ulong trapno;
732 abi_ulong err;
733 abi_ulong eip;
734 uint16_t cs, __csh;
735 abi_ulong eflags;
736 abi_ulong esp_at_signal;
737 uint16_t ss, __ssh;
738 abi_ulong fpstate; /* pointer */
739 abi_ulong oldmask;
740 abi_ulong cr2;
743 struct target_ucontext {
744 abi_ulong tuc_flags;
745 abi_ulong tuc_link;
746 target_stack_t tuc_stack;
747 struct target_sigcontext tuc_mcontext;
748 target_sigset_t tuc_sigmask; /* mask last for extensibility */
751 struct sigframe
753 abi_ulong pretcode;
754 int sig;
755 struct target_sigcontext sc;
756 struct target_fpstate fpstate;
757 abi_ulong extramask[TARGET_NSIG_WORDS-1];
758 char retcode[8];
761 struct rt_sigframe
763 abi_ulong pretcode;
764 int sig;
765 abi_ulong pinfo;
766 abi_ulong puc;
767 struct target_siginfo info;
768 struct target_ucontext uc;
769 struct target_fpstate fpstate;
770 char retcode[8];
774 * Set up a signal frame.
777 /* XXX: save x87 state */
778 static int
779 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
780 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
782 CPUState *cs = CPU(x86_env_get_cpu(env));
783 int err = 0;
784 uint16_t magic;
786 /* already locked in setup_frame() */
787 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
788 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
789 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
790 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
791 err |= __put_user(env->regs[R_EDI], &sc->edi);
792 err |= __put_user(env->regs[R_ESI], &sc->esi);
793 err |= __put_user(env->regs[R_EBP], &sc->ebp);
794 err |= __put_user(env->regs[R_ESP], &sc->esp);
795 err |= __put_user(env->regs[R_EBX], &sc->ebx);
796 err |= __put_user(env->regs[R_EDX], &sc->edx);
797 err |= __put_user(env->regs[R_ECX], &sc->ecx);
798 err |= __put_user(env->regs[R_EAX], &sc->eax);
799 err |= __put_user(cs->exception_index, &sc->trapno);
800 err |= __put_user(env->error_code, &sc->err);
801 err |= __put_user(env->eip, &sc->eip);
802 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
803 err |= __put_user(env->eflags, &sc->eflags);
804 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
805 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
807 cpu_x86_fsave(env, fpstate_addr, 1);
808 fpstate->status = fpstate->sw;
809 magic = 0xffff;
810 err |= __put_user(magic, &fpstate->magic);
811 err |= __put_user(fpstate_addr, &sc->fpstate);
813 /* non-iBCS2 extensions.. */
814 err |= __put_user(mask, &sc->oldmask);
815 err |= __put_user(env->cr[2], &sc->cr2);
816 return err;
820 * Determine which stack to use..
823 static inline abi_ulong
824 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
826 unsigned long esp;
828 /* Default to using normal stack */
829 esp = env->regs[R_ESP];
830 /* This is the X/Open sanctioned signal stack switching. */
831 if (ka->sa_flags & TARGET_SA_ONSTACK) {
832 if (sas_ss_flags(esp) == 0)
833 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
836 /* This is the legacy signal stack switching. */
837 else
838 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
839 !(ka->sa_flags & TARGET_SA_RESTORER) &&
840 ka->sa_restorer) {
841 esp = (unsigned long) ka->sa_restorer;
843 return (esp - frame_size) & -8ul;
846 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
847 static void setup_frame(int sig, struct target_sigaction *ka,
848 target_sigset_t *set, CPUX86State *env)
850 abi_ulong frame_addr;
851 struct sigframe *frame;
852 int i, err = 0;
854 frame_addr = get_sigframe(ka, env, sizeof(*frame));
856 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
857 goto give_sigsegv;
859 err |= __put_user(current_exec_domain_sig(sig),
860 &frame->sig);
861 if (err)
862 goto give_sigsegv;
864 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
865 frame_addr + offsetof(struct sigframe, fpstate));
866 if (err)
867 goto give_sigsegv;
869 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
870 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
871 goto give_sigsegv;
874 /* Set up to return from userspace. If provided, use a stub
875 already in userspace. */
876 if (ka->sa_flags & TARGET_SA_RESTORER) {
877 err |= __put_user(ka->sa_restorer, &frame->pretcode);
878 } else {
879 uint16_t val16;
880 abi_ulong retcode_addr;
881 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
882 err |= __put_user(retcode_addr, &frame->pretcode);
883 /* This is popl %eax ; movl $,%eax ; int $0x80 */
884 val16 = 0xb858;
885 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
886 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
887 val16 = 0x80cd;
888 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
891 if (err)
892 goto give_sigsegv;
894 /* Set up registers for signal handler */
895 env->regs[R_ESP] = frame_addr;
896 env->eip = ka->_sa_handler;
898 cpu_x86_load_seg(env, R_DS, __USER_DS);
899 cpu_x86_load_seg(env, R_ES, __USER_DS);
900 cpu_x86_load_seg(env, R_SS, __USER_DS);
901 cpu_x86_load_seg(env, R_CS, __USER_CS);
902 env->eflags &= ~TF_MASK;
904 unlock_user_struct(frame, frame_addr, 1);
906 return;
908 give_sigsegv:
909 unlock_user_struct(frame, frame_addr, 1);
910 if (sig == TARGET_SIGSEGV)
911 ka->_sa_handler = TARGET_SIG_DFL;
912 force_sig(TARGET_SIGSEGV /* , current */);
915 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
916 static void setup_rt_frame(int sig, struct target_sigaction *ka,
917 target_siginfo_t *info,
918 target_sigset_t *set, CPUX86State *env)
920 abi_ulong frame_addr, addr;
921 struct rt_sigframe *frame;
922 int i, err = 0;
924 frame_addr = get_sigframe(ka, env, sizeof(*frame));
926 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
927 goto give_sigsegv;
929 err |= __put_user(current_exec_domain_sig(sig),
930 &frame->sig);
931 addr = frame_addr + offsetof(struct rt_sigframe, info);
932 err |= __put_user(addr, &frame->pinfo);
933 addr = frame_addr + offsetof(struct rt_sigframe, uc);
934 err |= __put_user(addr, &frame->puc);
935 err |= copy_siginfo_to_user(&frame->info, info);
936 if (err)
937 goto give_sigsegv;
939 /* Create the ucontext. */
940 err |= __put_user(0, &frame->uc.tuc_flags);
941 err |= __put_user(0, &frame->uc.tuc_link);
942 err |= __put_user(target_sigaltstack_used.ss_sp,
943 &frame->uc.tuc_stack.ss_sp);
944 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
945 &frame->uc.tuc_stack.ss_flags);
946 err |= __put_user(target_sigaltstack_used.ss_size,
947 &frame->uc.tuc_stack.ss_size);
948 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
949 env, set->sig[0],
950 frame_addr + offsetof(struct rt_sigframe, fpstate));
951 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
952 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
953 goto give_sigsegv;
956 /* Set up to return from userspace. If provided, use a stub
957 already in userspace. */
958 if (ka->sa_flags & TARGET_SA_RESTORER) {
959 err |= __put_user(ka->sa_restorer, &frame->pretcode);
960 } else {
961 uint16_t val16;
962 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
963 err |= __put_user(addr, &frame->pretcode);
964 /* This is movl $,%eax ; int $0x80 */
965 err |= __put_user(0xb8, (char *)(frame->retcode+0));
966 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
967 val16 = 0x80cd;
968 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
971 if (err)
972 goto give_sigsegv;
974 /* Set up registers for signal handler */
975 env->regs[R_ESP] = frame_addr;
976 env->eip = ka->_sa_handler;
978 cpu_x86_load_seg(env, R_DS, __USER_DS);
979 cpu_x86_load_seg(env, R_ES, __USER_DS);
980 cpu_x86_load_seg(env, R_SS, __USER_DS);
981 cpu_x86_load_seg(env, R_CS, __USER_CS);
982 env->eflags &= ~TF_MASK;
984 unlock_user_struct(frame, frame_addr, 1);
986 return;
988 give_sigsegv:
989 unlock_user_struct(frame, frame_addr, 1);
990 if (sig == TARGET_SIGSEGV)
991 ka->_sa_handler = TARGET_SIG_DFL;
992 force_sig(TARGET_SIGSEGV /* , current */);
995 static int
996 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
998 unsigned int err = 0;
999 abi_ulong fpstate_addr;
1000 unsigned int tmpflags;
1002 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1003 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1004 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1005 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1007 env->regs[R_EDI] = tswapl(sc->edi);
1008 env->regs[R_ESI] = tswapl(sc->esi);
1009 env->regs[R_EBP] = tswapl(sc->ebp);
1010 env->regs[R_ESP] = tswapl(sc->esp);
1011 env->regs[R_EBX] = tswapl(sc->ebx);
1012 env->regs[R_EDX] = tswapl(sc->edx);
1013 env->regs[R_ECX] = tswapl(sc->ecx);
1014 env->eip = tswapl(sc->eip);
1016 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1017 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1019 tmpflags = tswapl(sc->eflags);
1020 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1021 // regs->orig_eax = -1; /* disable syscall checks */
1023 fpstate_addr = tswapl(sc->fpstate);
1024 if (fpstate_addr != 0) {
1025 if (!access_ok(VERIFY_READ, fpstate_addr,
1026 sizeof(struct target_fpstate)))
1027 goto badframe;
1028 cpu_x86_frstor(env, fpstate_addr, 1);
1031 *peax = tswapl(sc->eax);
1032 return err;
1033 badframe:
1034 return 1;
1037 long do_sigreturn(CPUX86State *env)
1039 struct sigframe *frame;
1040 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1041 target_sigset_t target_set;
1042 sigset_t set;
1043 int eax, i;
1045 #if defined(DEBUG_SIGNAL)
1046 fprintf(stderr, "do_sigreturn\n");
1047 #endif
1048 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1049 goto badframe;
1050 /* set blocked signals */
1051 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1052 goto badframe;
1053 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1054 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1055 goto badframe;
1058 target_to_host_sigset_internal(&set, &target_set);
1059 sigprocmask(SIG_SETMASK, &set, NULL);
1061 /* restore registers */
1062 if (restore_sigcontext(env, &frame->sc, &eax))
1063 goto badframe;
1064 unlock_user_struct(frame, frame_addr, 0);
1065 return eax;
1067 badframe:
1068 unlock_user_struct(frame, frame_addr, 0);
1069 force_sig(TARGET_SIGSEGV);
1070 return 0;
1073 long do_rt_sigreturn(CPUX86State *env)
1075 abi_ulong frame_addr;
1076 struct rt_sigframe *frame;
1077 sigset_t set;
1078 int eax;
1080 frame_addr = env->regs[R_ESP] - 4;
1081 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1082 goto badframe;
1083 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1084 sigprocmask(SIG_SETMASK, &set, NULL);
1086 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1087 goto badframe;
1089 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1090 get_sp_from_cpustate(env)) == -EFAULT)
1091 goto badframe;
1093 unlock_user_struct(frame, frame_addr, 0);
1094 return eax;
1096 badframe:
1097 unlock_user_struct(frame, frame_addr, 0);
1098 force_sig(TARGET_SIGSEGV);
1099 return 0;
1102 #elif defined(TARGET_AARCH64)
1104 struct target_sigcontext {
1105 uint64_t fault_address;
1106 /* AArch64 registers */
1107 uint64_t regs[31];
1108 uint64_t sp;
1109 uint64_t pc;
1110 uint64_t pstate;
1111 /* 4K reserved for FP/SIMD state and future expansion */
1112 char __reserved[4096] __attribute__((__aligned__(16)));
1115 struct target_ucontext {
1116 abi_ulong tuc_flags;
1117 abi_ulong tuc_link;
1118 target_stack_t tuc_stack;
1119 target_sigset_t tuc_sigmask;
1120 /* glibc uses a 1024-bit sigset_t */
1121 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1122 /* last for future expansion */
1123 struct target_sigcontext tuc_mcontext;
1127 * Header to be used at the beginning of structures extending the user
1128 * context. Such structures must be placed after the rt_sigframe on the stack
1129 * and be 16-byte aligned. The last structure must be a dummy one with the
1130 * magic and size set to 0.
1132 struct target_aarch64_ctx {
1133 uint32_t magic;
1134 uint32_t size;
1137 #define TARGET_FPSIMD_MAGIC 0x46508001
1139 struct target_fpsimd_context {
1140 struct target_aarch64_ctx head;
1141 uint32_t fpsr;
1142 uint32_t fpcr;
1143 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1147 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1148 * user space as it will change with the addition of new context. User space
1149 * should check the magic/size information.
1151 struct target_aux_context {
1152 struct target_fpsimd_context fpsimd;
1153 /* additional context to be added before "end" */
1154 struct target_aarch64_ctx end;
1157 struct target_rt_sigframe {
1158 struct target_siginfo info;
1159 struct target_ucontext uc;
1160 uint64_t fp;
1161 uint64_t lr;
1162 uint32_t tramp[2];
1165 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1166 CPUARMState *env, target_sigset_t *set)
1168 int i;
1169 struct target_aux_context *aux =
1170 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1172 /* set up the stack frame for unwinding */
1173 __put_user(env->xregs[29], &sf->fp);
1174 __put_user(env->xregs[30], &sf->lr);
1176 for (i = 0; i < 31; i++) {
1177 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1179 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1180 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
1181 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
1183 __put_user(/*current->thread.fault_address*/ 0,
1184 &sf->uc.tuc_mcontext.fault_address);
1186 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1187 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1190 for (i = 0; i < 32; i++) {
1191 #ifdef TARGET_WORDS_BIGENDIAN
1192 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1193 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1194 #else
1195 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1196 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1197 #endif
1199 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1200 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1201 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1202 __put_user(sizeof(struct target_fpsimd_context),
1203 &aux->fpsimd.head.size);
1205 /* set the "end" magic */
1206 __put_user(0, &aux->end.magic);
1207 __put_user(0, &aux->end.size);
1209 return 0;
1212 static int target_restore_sigframe(CPUARMState *env,
1213 struct target_rt_sigframe *sf)
1215 sigset_t set;
1216 int i;
1217 struct target_aux_context *aux =
1218 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1219 uint32_t magic, size, fpsr, fpcr;
1220 uint64_t pstate;
1222 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1223 sigprocmask(SIG_SETMASK, &set, NULL);
1225 for (i = 0; i < 31; i++) {
1226 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1229 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1230 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1231 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1232 pstate_write(env, pstate);
1234 __get_user(magic, &aux->fpsimd.head.magic);
1235 __get_user(size, &aux->fpsimd.head.size);
1237 if (magic != TARGET_FPSIMD_MAGIC
1238 || size != sizeof(struct target_fpsimd_context)) {
1239 return 1;
1242 for (i = 0; i < 32; i++) {
1243 #ifdef TARGET_WORDS_BIGENDIAN
1244 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1245 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1246 #else
1247 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1248 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1249 #endif
1251 __get_user(fpsr, &aux->fpsimd.fpsr);
1252 vfp_set_fpsr(env, fpsr);
1253 __get_user(fpcr, &aux->fpsimd.fpcr);
1254 vfp_set_fpcr(env, fpcr);
1256 return 0;
1259 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1261 abi_ulong sp;
1263 sp = env->xregs[31];
1266 * This is the X/Open sanctioned signal stack switching.
1268 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
1269 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1272 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1274 return sp;
1277 static void target_setup_frame(int usig, struct target_sigaction *ka,
1278 target_siginfo_t *info, target_sigset_t *set,
1279 CPUARMState *env)
1281 struct target_rt_sigframe *frame;
1282 abi_ulong frame_addr, return_addr;
1284 frame_addr = get_sigframe(ka, env);
1285 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1286 goto give_sigsegv;
1289 __put_user(0, &frame->uc.tuc_flags);
1290 __put_user(0, &frame->uc.tuc_link);
1292 __put_user(target_sigaltstack_used.ss_sp,
1293 &frame->uc.tuc_stack.ss_sp);
1294 __put_user(sas_ss_flags(env->xregs[31]),
1295 &frame->uc.tuc_stack.ss_flags);
1296 __put_user(target_sigaltstack_used.ss_size,
1297 &frame->uc.tuc_stack.ss_size);
1298 target_setup_sigframe(frame, env, set);
1299 if (ka->sa_flags & TARGET_SA_RESTORER) {
1300 return_addr = ka->sa_restorer;
1301 } else {
1302 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1303 __put_user(0xd2801168, &frame->tramp[0]);
1304 __put_user(0xd4000001, &frame->tramp[1]);
1305 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1307 env->xregs[0] = usig;
1308 env->xregs[31] = frame_addr;
1309 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1310 env->pc = ka->_sa_handler;
1311 env->xregs[30] = return_addr;
1312 if (info) {
1313 if (copy_siginfo_to_user(&frame->info, info)) {
1314 goto give_sigsegv;
1316 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1317 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1320 unlock_user_struct(frame, frame_addr, 1);
1321 return;
1323 give_sigsegv:
1324 unlock_user_struct(frame, frame_addr, 1);
1325 force_sig(TARGET_SIGSEGV);
1328 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1329 target_siginfo_t *info, target_sigset_t *set,
1330 CPUARMState *env)
1332 target_setup_frame(sig, ka, info, set, env);
1335 static void setup_frame(int sig, struct target_sigaction *ka,
1336 target_sigset_t *set, CPUARMState *env)
1338 target_setup_frame(sig, ka, 0, set, env);
1341 long do_rt_sigreturn(CPUARMState *env)
1343 struct target_rt_sigframe *frame;
1344 abi_ulong frame_addr = env->xregs[31];
1346 if (frame_addr & 15) {
1347 goto badframe;
1350 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1351 goto badframe;
1354 if (target_restore_sigframe(env, frame)) {
1355 goto badframe;
1358 if (do_sigaltstack(frame_addr +
1359 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1360 0, get_sp_from_cpustate(env)) == -EFAULT) {
1361 goto badframe;
1364 unlock_user_struct(frame, frame_addr, 0);
1365 return env->xregs[0];
1367 badframe:
1368 unlock_user_struct(frame, frame_addr, 0);
1369 force_sig(TARGET_SIGSEGV);
1370 return 0;
1373 long do_sigreturn(CPUARMState *env)
1375 return do_rt_sigreturn(env);
1378 #elif defined(TARGET_ARM)
1380 struct target_sigcontext {
1381 abi_ulong trap_no;
1382 abi_ulong error_code;
1383 abi_ulong oldmask;
1384 abi_ulong arm_r0;
1385 abi_ulong arm_r1;
1386 abi_ulong arm_r2;
1387 abi_ulong arm_r3;
1388 abi_ulong arm_r4;
1389 abi_ulong arm_r5;
1390 abi_ulong arm_r6;
1391 abi_ulong arm_r7;
1392 abi_ulong arm_r8;
1393 abi_ulong arm_r9;
1394 abi_ulong arm_r10;
1395 abi_ulong arm_fp;
1396 abi_ulong arm_ip;
1397 abi_ulong arm_sp;
1398 abi_ulong arm_lr;
1399 abi_ulong arm_pc;
1400 abi_ulong arm_cpsr;
1401 abi_ulong fault_address;
1404 struct target_ucontext_v1 {
1405 abi_ulong tuc_flags;
1406 abi_ulong tuc_link;
1407 target_stack_t tuc_stack;
1408 struct target_sigcontext tuc_mcontext;
1409 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1412 struct target_ucontext_v2 {
1413 abi_ulong tuc_flags;
1414 abi_ulong tuc_link;
1415 target_stack_t tuc_stack;
1416 struct target_sigcontext tuc_mcontext;
1417 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1418 char __unused[128 - sizeof(target_sigset_t)];
1419 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1422 struct target_user_vfp {
1423 uint64_t fpregs[32];
1424 abi_ulong fpscr;
1427 struct target_user_vfp_exc {
1428 abi_ulong fpexc;
1429 abi_ulong fpinst;
1430 abi_ulong fpinst2;
1433 struct target_vfp_sigframe {
1434 abi_ulong magic;
1435 abi_ulong size;
1436 struct target_user_vfp ufp;
1437 struct target_user_vfp_exc ufp_exc;
1438 } __attribute__((__aligned__(8)));
1440 struct target_iwmmxt_sigframe {
1441 abi_ulong magic;
1442 abi_ulong size;
1443 uint64_t regs[16];
1444 /* Note that not all the coprocessor control registers are stored here */
1445 uint32_t wcssf;
1446 uint32_t wcasf;
1447 uint32_t wcgr0;
1448 uint32_t wcgr1;
1449 uint32_t wcgr2;
1450 uint32_t wcgr3;
1451 } __attribute__((__aligned__(8)));
1453 #define TARGET_VFP_MAGIC 0x56465001
1454 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1456 struct sigframe_v1
1458 struct target_sigcontext sc;
1459 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1460 abi_ulong retcode;
1463 struct sigframe_v2
1465 struct target_ucontext_v2 uc;
1466 abi_ulong retcode;
1469 struct rt_sigframe_v1
1471 abi_ulong pinfo;
1472 abi_ulong puc;
1473 struct target_siginfo info;
1474 struct target_ucontext_v1 uc;
1475 abi_ulong retcode;
1478 struct rt_sigframe_v2
1480 struct target_siginfo info;
1481 struct target_ucontext_v2 uc;
1482 abi_ulong retcode;
1485 #define TARGET_CONFIG_CPU_32 1
1488 * For ARM syscalls, we encode the syscall number into the instruction.
1490 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1491 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1494 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1495 * need two 16-bit instructions.
1497 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1498 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1500 static const abi_ulong retcodes[4] = {
1501 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1502 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1506 #define __get_user_error(x,p,e) __get_user(x, p)
1508 static inline int valid_user_regs(CPUARMState *regs)
1510 return 1;
1513 static void
1514 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1515 CPUARMState *env, abi_ulong mask)
1517 __put_user(env->regs[0], &sc->arm_r0);
1518 __put_user(env->regs[1], &sc->arm_r1);
1519 __put_user(env->regs[2], &sc->arm_r2);
1520 __put_user(env->regs[3], &sc->arm_r3);
1521 __put_user(env->regs[4], &sc->arm_r4);
1522 __put_user(env->regs[5], &sc->arm_r5);
1523 __put_user(env->regs[6], &sc->arm_r6);
1524 __put_user(env->regs[7], &sc->arm_r7);
1525 __put_user(env->regs[8], &sc->arm_r8);
1526 __put_user(env->regs[9], &sc->arm_r9);
1527 __put_user(env->regs[10], &sc->arm_r10);
1528 __put_user(env->regs[11], &sc->arm_fp);
1529 __put_user(env->regs[12], &sc->arm_ip);
1530 __put_user(env->regs[13], &sc->arm_sp);
1531 __put_user(env->regs[14], &sc->arm_lr);
1532 __put_user(env->regs[15], &sc->arm_pc);
1533 #ifdef TARGET_CONFIG_CPU_32
1534 __put_user(cpsr_read(env), &sc->arm_cpsr);
1535 #endif
1537 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1538 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1539 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1540 __put_user(mask, &sc->oldmask);
1543 static inline abi_ulong
1544 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1546 unsigned long sp = regs->regs[13];
1549 * This is the X/Open sanctioned signal stack switching.
1551 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1552 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1554 * ATPCS B01 mandates 8-byte alignment
1556 return (sp - framesize) & ~7;
1559 static int
1560 setup_return(CPUARMState *env, struct target_sigaction *ka,
1561 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1563 abi_ulong handler = ka->_sa_handler;
1564 abi_ulong retcode;
1565 int thumb = handler & 1;
1566 uint32_t cpsr = cpsr_read(env);
1568 cpsr &= ~CPSR_IT;
1569 if (thumb) {
1570 cpsr |= CPSR_T;
1571 } else {
1572 cpsr &= ~CPSR_T;
1575 if (ka->sa_flags & TARGET_SA_RESTORER) {
1576 retcode = ka->sa_restorer;
1577 } else {
1578 unsigned int idx = thumb;
1580 if (ka->sa_flags & TARGET_SA_SIGINFO)
1581 idx += 2;
1583 if (__put_user(retcodes[idx], rc))
1584 return 1;
1586 retcode = rc_addr + thumb;
1589 env->regs[0] = usig;
1590 env->regs[13] = frame_addr;
1591 env->regs[14] = retcode;
1592 env->regs[15] = handler & (thumb ? ~1 : ~3);
1593 cpsr_write(env, cpsr, 0xffffffff);
1595 return 0;
1598 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1600 int i;
1601 struct target_vfp_sigframe *vfpframe;
1602 vfpframe = (struct target_vfp_sigframe *)regspace;
1603 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1604 __put_user(sizeof(*vfpframe), &vfpframe->size);
1605 for (i = 0; i < 32; i++) {
1606 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1608 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1609 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1610 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1611 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1612 return (abi_ulong*)(vfpframe+1);
1615 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1616 CPUARMState *env)
1618 int i;
1619 struct target_iwmmxt_sigframe *iwmmxtframe;
1620 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1621 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1622 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1623 for (i = 0; i < 16; i++) {
1624 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1626 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1627 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1628 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1629 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1630 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1631 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1632 return (abi_ulong*)(iwmmxtframe+1);
1635 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1636 target_sigset_t *set, CPUARMState *env)
1638 struct target_sigaltstack stack;
1639 int i;
1640 abi_ulong *regspace;
1642 /* Clear all the bits of the ucontext we don't use. */
1643 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1645 memset(&stack, 0, sizeof(stack));
1646 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1647 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1648 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1649 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1651 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1652 /* Save coprocessor signal frame. */
1653 regspace = uc->tuc_regspace;
1654 if (arm_feature(env, ARM_FEATURE_VFP)) {
1655 regspace = setup_sigframe_v2_vfp(regspace, env);
1657 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1658 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1661 /* Write terminating magic word */
1662 __put_user(0, regspace);
1664 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1665 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1669 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1670 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1671 target_sigset_t *set, CPUARMState *regs)
1673 struct sigframe_v1 *frame;
1674 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1675 int i;
1677 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1678 return;
1680 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1682 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1683 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1684 goto end;
1687 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1688 frame_addr + offsetof(struct sigframe_v1, retcode));
1690 end:
1691 unlock_user_struct(frame, frame_addr, 1);
1694 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1695 target_sigset_t *set, CPUARMState *regs)
1697 struct sigframe_v2 *frame;
1698 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1700 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1701 return;
1703 setup_sigframe_v2(&frame->uc, set, regs);
1705 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1706 frame_addr + offsetof(struct sigframe_v2, retcode));
1708 unlock_user_struct(frame, frame_addr, 1);
1711 static void setup_frame(int usig, struct target_sigaction *ka,
1712 target_sigset_t *set, CPUARMState *regs)
1714 if (get_osversion() >= 0x020612) {
1715 setup_frame_v2(usig, ka, set, regs);
1716 } else {
1717 setup_frame_v1(usig, ka, set, regs);
1721 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1722 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1723 target_siginfo_t *info,
1724 target_sigset_t *set, CPUARMState *env)
1726 struct rt_sigframe_v1 *frame;
1727 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1728 struct target_sigaltstack stack;
1729 int i;
1730 abi_ulong info_addr, uc_addr;
1732 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1733 return /* 1 */;
1735 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1736 __put_user(info_addr, &frame->pinfo);
1737 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1738 __put_user(uc_addr, &frame->puc);
1739 copy_siginfo_to_user(&frame->info, info);
1741 /* Clear all the bits of the ucontext we don't use. */
1742 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1744 memset(&stack, 0, sizeof(stack));
1745 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1746 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1747 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1748 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1750 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1751 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1752 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1753 goto end;
1756 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1757 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1759 env->regs[1] = info_addr;
1760 env->regs[2] = uc_addr;
1762 end:
1763 unlock_user_struct(frame, frame_addr, 1);
1766 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1767 target_siginfo_t *info,
1768 target_sigset_t *set, CPUARMState *env)
1770 struct rt_sigframe_v2 *frame;
1771 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1772 abi_ulong info_addr, uc_addr;
1774 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1775 return /* 1 */;
1777 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1778 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1779 copy_siginfo_to_user(&frame->info, info);
1781 setup_sigframe_v2(&frame->uc, set, env);
1783 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1784 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1786 env->regs[1] = info_addr;
1787 env->regs[2] = uc_addr;
1789 unlock_user_struct(frame, frame_addr, 1);
1792 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1793 target_siginfo_t *info,
1794 target_sigset_t *set, CPUARMState *env)
1796 if (get_osversion() >= 0x020612) {
1797 setup_rt_frame_v2(usig, ka, info, set, env);
1798 } else {
1799 setup_rt_frame_v1(usig, ka, info, set, env);
1803 static int
1804 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1806 int err = 0;
1807 uint32_t cpsr;
1809 __get_user_error(env->regs[0], &sc->arm_r0, err);
1810 __get_user_error(env->regs[1], &sc->arm_r1, err);
1811 __get_user_error(env->regs[2], &sc->arm_r2, err);
1812 __get_user_error(env->regs[3], &sc->arm_r3, err);
1813 __get_user_error(env->regs[4], &sc->arm_r4, err);
1814 __get_user_error(env->regs[5], &sc->arm_r5, err);
1815 __get_user_error(env->regs[6], &sc->arm_r6, err);
1816 __get_user_error(env->regs[7], &sc->arm_r7, err);
1817 __get_user_error(env->regs[8], &sc->arm_r8, err);
1818 __get_user_error(env->regs[9], &sc->arm_r9, err);
1819 __get_user_error(env->regs[10], &sc->arm_r10, err);
1820 __get_user_error(env->regs[11], &sc->arm_fp, err);
1821 __get_user_error(env->regs[12], &sc->arm_ip, err);
1822 __get_user_error(env->regs[13], &sc->arm_sp, err);
1823 __get_user_error(env->regs[14], &sc->arm_lr, err);
1824 __get_user_error(env->regs[15], &sc->arm_pc, err);
1825 #ifdef TARGET_CONFIG_CPU_32
1826 __get_user_error(cpsr, &sc->arm_cpsr, err);
1827 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1828 #endif
1830 err |= !valid_user_regs(env);
1832 return err;
1835 static long do_sigreturn_v1(CPUARMState *env)
1837 abi_ulong frame_addr;
1838 struct sigframe_v1 *frame = NULL;
1839 target_sigset_t set;
1840 sigset_t host_set;
1841 int i;
1844 * Since we stacked the signal on a 64-bit boundary,
1845 * then 'sp' should be word aligned here. If it's
1846 * not, then the user is trying to mess with us.
1848 frame_addr = env->regs[13];
1849 if (frame_addr & 7) {
1850 goto badframe;
1853 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1854 goto badframe;
1856 if (__get_user(set.sig[0], &frame->sc.oldmask))
1857 goto badframe;
1858 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1859 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1860 goto badframe;
1863 target_to_host_sigset_internal(&host_set, &set);
1864 sigprocmask(SIG_SETMASK, &host_set, NULL);
1866 if (restore_sigcontext(env, &frame->sc))
1867 goto badframe;
1869 #if 0
1870 /* Send SIGTRAP if we're single-stepping */
1871 if (ptrace_cancel_bpt(current))
1872 send_sig(SIGTRAP, current, 1);
1873 #endif
1874 unlock_user_struct(frame, frame_addr, 0);
1875 return env->regs[0];
1877 badframe:
1878 unlock_user_struct(frame, frame_addr, 0);
1879 force_sig(TARGET_SIGSEGV /* , current */);
1880 return 0;
1883 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1885 int i;
1886 abi_ulong magic, sz;
1887 uint32_t fpscr, fpexc;
1888 struct target_vfp_sigframe *vfpframe;
1889 vfpframe = (struct target_vfp_sigframe *)regspace;
1891 __get_user(magic, &vfpframe->magic);
1892 __get_user(sz, &vfpframe->size);
1893 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1894 return 0;
1896 for (i = 0; i < 32; i++) {
1897 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1899 __get_user(fpscr, &vfpframe->ufp.fpscr);
1900 vfp_set_fpscr(env, fpscr);
1901 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1902 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1903 * and the exception flag is cleared
1905 fpexc |= (1 << 30);
1906 fpexc &= ~((1 << 31) | (1 << 28));
1907 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1908 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1909 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1910 return (abi_ulong*)(vfpframe + 1);
1913 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1914 abi_ulong *regspace)
1916 int i;
1917 abi_ulong magic, sz;
1918 struct target_iwmmxt_sigframe *iwmmxtframe;
1919 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1921 __get_user(magic, &iwmmxtframe->magic);
1922 __get_user(sz, &iwmmxtframe->size);
1923 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1924 return 0;
1926 for (i = 0; i < 16; i++) {
1927 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1929 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1930 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1931 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1932 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1933 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1934 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1935 return (abi_ulong*)(iwmmxtframe + 1);
1938 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1939 struct target_ucontext_v2 *uc)
1941 sigset_t host_set;
1942 abi_ulong *regspace;
1944 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1945 sigprocmask(SIG_SETMASK, &host_set, NULL);
1947 if (restore_sigcontext(env, &uc->tuc_mcontext))
1948 return 1;
1950 /* Restore coprocessor signal frame */
1951 regspace = uc->tuc_regspace;
1952 if (arm_feature(env, ARM_FEATURE_VFP)) {
1953 regspace = restore_sigframe_v2_vfp(env, regspace);
1954 if (!regspace) {
1955 return 1;
1958 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1959 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1960 if (!regspace) {
1961 return 1;
1965 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1966 return 1;
1968 #if 0
1969 /* Send SIGTRAP if we're single-stepping */
1970 if (ptrace_cancel_bpt(current))
1971 send_sig(SIGTRAP, current, 1);
1972 #endif
1974 return 0;
1977 static long do_sigreturn_v2(CPUARMState *env)
1979 abi_ulong frame_addr;
1980 struct sigframe_v2 *frame = NULL;
1983 * Since we stacked the signal on a 64-bit boundary,
1984 * then 'sp' should be word aligned here. If it's
1985 * not, then the user is trying to mess with us.
1987 frame_addr = env->regs[13];
1988 if (frame_addr & 7) {
1989 goto badframe;
1992 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1993 goto badframe;
1995 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1996 goto badframe;
1998 unlock_user_struct(frame, frame_addr, 0);
1999 return env->regs[0];
2001 badframe:
2002 unlock_user_struct(frame, frame_addr, 0);
2003 force_sig(TARGET_SIGSEGV /* , current */);
2004 return 0;
2007 long do_sigreturn(CPUARMState *env)
2009 if (get_osversion() >= 0x020612) {
2010 return do_sigreturn_v2(env);
2011 } else {
2012 return do_sigreturn_v1(env);
2016 static long do_rt_sigreturn_v1(CPUARMState *env)
2018 abi_ulong frame_addr;
2019 struct rt_sigframe_v1 *frame = NULL;
2020 sigset_t host_set;
2023 * Since we stacked the signal on a 64-bit boundary,
2024 * then 'sp' should be word aligned here. If it's
2025 * not, then the user is trying to mess with us.
2027 frame_addr = env->regs[13];
2028 if (frame_addr & 7) {
2029 goto badframe;
2032 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2033 goto badframe;
2035 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2036 sigprocmask(SIG_SETMASK, &host_set, NULL);
2038 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
2039 goto badframe;
2041 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2042 goto badframe;
2044 #if 0
2045 /* Send SIGTRAP if we're single-stepping */
2046 if (ptrace_cancel_bpt(current))
2047 send_sig(SIGTRAP, current, 1);
2048 #endif
2049 unlock_user_struct(frame, frame_addr, 0);
2050 return env->regs[0];
2052 badframe:
2053 unlock_user_struct(frame, frame_addr, 0);
2054 force_sig(TARGET_SIGSEGV /* , current */);
2055 return 0;
2058 static long do_rt_sigreturn_v2(CPUARMState *env)
2060 abi_ulong frame_addr;
2061 struct rt_sigframe_v2 *frame = NULL;
2064 * Since we stacked the signal on a 64-bit boundary,
2065 * then 'sp' should be word aligned here. If it's
2066 * not, then the user is trying to mess with us.
2068 frame_addr = env->regs[13];
2069 if (frame_addr & 7) {
2070 goto badframe;
2073 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2074 goto badframe;
2076 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2077 goto badframe;
2079 unlock_user_struct(frame, frame_addr, 0);
2080 return env->regs[0];
2082 badframe:
2083 unlock_user_struct(frame, frame_addr, 0);
2084 force_sig(TARGET_SIGSEGV /* , current */);
2085 return 0;
2088 long do_rt_sigreturn(CPUARMState *env)
2090 if (get_osversion() >= 0x020612) {
2091 return do_rt_sigreturn_v2(env);
2092 } else {
2093 return do_rt_sigreturn_v1(env);
2097 #elif defined(TARGET_SPARC)
2099 #define __SUNOS_MAXWIN 31
2101 /* This is what SunOS does, so shall I. */
2102 struct target_sigcontext {
2103 abi_ulong sigc_onstack; /* state to restore */
2105 abi_ulong sigc_mask; /* sigmask to restore */
2106 abi_ulong sigc_sp; /* stack pointer */
2107 abi_ulong sigc_pc; /* program counter */
2108 abi_ulong sigc_npc; /* next program counter */
2109 abi_ulong sigc_psr; /* for condition codes etc */
2110 abi_ulong sigc_g1; /* User uses these two registers */
2111 abi_ulong sigc_o0; /* within the trampoline code. */
2113 /* Now comes information regarding the users window set
2114 * at the time of the signal.
2116 abi_ulong sigc_oswins; /* outstanding windows */
2118 /* stack ptrs for each regwin buf */
2119 char *sigc_spbuf[__SUNOS_MAXWIN];
2121 /* Windows to restore after signal */
2122 struct {
2123 abi_ulong locals[8];
2124 abi_ulong ins[8];
2125 } sigc_wbuf[__SUNOS_MAXWIN];
2127 /* A Sparc stack frame */
2128 struct sparc_stackf {
2129 abi_ulong locals[8];
2130 abi_ulong ins[8];
2131 /* It's simpler to treat fp and callers_pc as elements of ins[]
2132 * since we never need to access them ourselves.
2134 char *structptr;
2135 abi_ulong xargs[6];
2136 abi_ulong xxargs[1];
2139 typedef struct {
2140 struct {
2141 abi_ulong psr;
2142 abi_ulong pc;
2143 abi_ulong npc;
2144 abi_ulong y;
2145 abi_ulong u_regs[16]; /* globals and ins */
2146 } si_regs;
2147 int si_mask;
2148 } __siginfo_t;
2150 typedef struct {
2151 abi_ulong si_float_regs[32];
2152 unsigned long si_fsr;
2153 unsigned long si_fpqdepth;
2154 struct {
2155 unsigned long *insn_addr;
2156 unsigned long insn;
2157 } si_fpqueue [16];
2158 } qemu_siginfo_fpu_t;
2161 struct target_signal_frame {
2162 struct sparc_stackf ss;
2163 __siginfo_t info;
2164 abi_ulong fpu_save;
2165 abi_ulong insns[2] __attribute__ ((aligned (8)));
2166 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2167 abi_ulong extra_size; /* Should be 0 */
2168 qemu_siginfo_fpu_t fpu_state;
2170 struct target_rt_signal_frame {
2171 struct sparc_stackf ss;
2172 siginfo_t info;
2173 abi_ulong regs[20];
2174 sigset_t mask;
2175 abi_ulong fpu_save;
2176 unsigned int insns[2];
2177 stack_t stack;
2178 unsigned int extra_size; /* Should be 0 */
2179 qemu_siginfo_fpu_t fpu_state;
2182 #define UREG_O0 16
2183 #define UREG_O6 22
2184 #define UREG_I0 0
2185 #define UREG_I1 1
2186 #define UREG_I2 2
2187 #define UREG_I3 3
2188 #define UREG_I4 4
2189 #define UREG_I5 5
2190 #define UREG_I6 6
2191 #define UREG_I7 7
2192 #define UREG_L0 8
2193 #define UREG_FP UREG_I6
2194 #define UREG_SP UREG_O6
2196 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2197 CPUSPARCState *env,
2198 unsigned long framesize)
2200 abi_ulong sp;
2202 sp = env->regwptr[UREG_FP];
2204 /* This is the X/Open sanctioned signal stack switching. */
2205 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2206 if (!on_sig_stack(sp)
2207 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2208 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2210 return sp - framesize;
2213 static int
2214 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2216 int err = 0, i;
2218 err |= __put_user(env->psr, &si->si_regs.psr);
2219 err |= __put_user(env->pc, &si->si_regs.pc);
2220 err |= __put_user(env->npc, &si->si_regs.npc);
2221 err |= __put_user(env->y, &si->si_regs.y);
2222 for (i=0; i < 8; i++) {
2223 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2225 for (i=0; i < 8; i++) {
2226 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2228 err |= __put_user(mask, &si->si_mask);
2229 return err;
2232 #if 0
2233 static int
2234 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2235 CPUSPARCState *env, unsigned long mask)
2237 int err = 0;
2239 err |= __put_user(mask, &sc->sigc_mask);
2240 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2241 err |= __put_user(env->pc, &sc->sigc_pc);
2242 err |= __put_user(env->npc, &sc->sigc_npc);
2243 err |= __put_user(env->psr, &sc->sigc_psr);
2244 err |= __put_user(env->gregs[1], &sc->sigc_g1);
2245 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2247 return err;
2249 #endif
2250 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2252 static void setup_frame(int sig, struct target_sigaction *ka,
2253 target_sigset_t *set, CPUSPARCState *env)
2255 abi_ulong sf_addr;
2256 struct target_signal_frame *sf;
2257 int sigframe_size, err, i;
2259 /* 1. Make sure everything is clean */
2260 //synchronize_user_stack();
2262 sigframe_size = NF_ALIGNEDSZ;
2263 sf_addr = get_sigframe(ka, env, sigframe_size);
2265 sf = lock_user(VERIFY_WRITE, sf_addr,
2266 sizeof(struct target_signal_frame), 0);
2267 if (!sf)
2268 goto sigsegv;
2270 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2271 #if 0
2272 if (invalid_frame_pointer(sf, sigframe_size))
2273 goto sigill_and_return;
2274 #endif
2275 /* 2. Save the current process state */
2276 err = setup___siginfo(&sf->info, env, set->sig[0]);
2277 err |= __put_user(0, &sf->extra_size);
2279 //err |= save_fpu_state(regs, &sf->fpu_state);
2280 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
2282 err |= __put_user(set->sig[0], &sf->info.si_mask);
2283 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2284 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
2287 for (i = 0; i < 8; i++) {
2288 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2290 for (i = 0; i < 8; i++) {
2291 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2293 if (err)
2294 goto sigsegv;
2296 /* 3. signal handler back-trampoline and parameters */
2297 env->regwptr[UREG_FP] = sf_addr;
2298 env->regwptr[UREG_I0] = sig;
2299 env->regwptr[UREG_I1] = sf_addr +
2300 offsetof(struct target_signal_frame, info);
2301 env->regwptr[UREG_I2] = sf_addr +
2302 offsetof(struct target_signal_frame, info);
2304 /* 4. signal handler */
2305 env->pc = ka->_sa_handler;
2306 env->npc = (env->pc + 4);
2307 /* 5. return to kernel instructions */
2308 if (ka->sa_restorer)
2309 env->regwptr[UREG_I7] = ka->sa_restorer;
2310 else {
2311 uint32_t val32;
2313 env->regwptr[UREG_I7] = sf_addr +
2314 offsetof(struct target_signal_frame, insns) - 2 * 4;
2316 /* mov __NR_sigreturn, %g1 */
2317 val32 = 0x821020d8;
2318 err |= __put_user(val32, &sf->insns[0]);
2320 /* t 0x10 */
2321 val32 = 0x91d02010;
2322 err |= __put_user(val32, &sf->insns[1]);
2323 if (err)
2324 goto sigsegv;
2326 /* Flush instruction space. */
2327 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2328 // tb_flush(env);
2330 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2331 return;
2332 #if 0
2333 sigill_and_return:
2334 force_sig(TARGET_SIGILL);
2335 #endif
2336 sigsegv:
2337 //fprintf(stderr, "force_sig\n");
2338 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2339 force_sig(TARGET_SIGSEGV);
2341 static inline int
2342 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
2344 int err;
2345 #if 0
2346 #ifdef CONFIG_SMP
2347 if (current->flags & PF_USEDFPU)
2348 regs->psr &= ~PSR_EF;
2349 #else
2350 if (current == last_task_used_math) {
2351 last_task_used_math = 0;
2352 regs->psr &= ~PSR_EF;
2354 #endif
2355 current->used_math = 1;
2356 current->flags &= ~PF_USEDFPU;
2357 #endif
2358 #if 0
2359 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2360 return -EFAULT;
2361 #endif
2363 /* XXX: incorrect */
2364 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2365 (sizeof(abi_ulong) * 32));
2366 err |= __get_user(env->fsr, &fpu->si_fsr);
2367 #if 0
2368 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2369 if (current->thread.fpqdepth != 0)
2370 err |= __copy_from_user(&current->thread.fpqueue[0],
2371 &fpu->si_fpqueue[0],
2372 ((sizeof(unsigned long) +
2373 (sizeof(unsigned long *)))*16));
2374 #endif
2375 return err;
2379 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2380 target_siginfo_t *info,
2381 target_sigset_t *set, CPUSPARCState *env)
2383 fprintf(stderr, "setup_rt_frame: not implemented\n");
2386 long do_sigreturn(CPUSPARCState *env)
2388 abi_ulong sf_addr;
2389 struct target_signal_frame *sf;
2390 uint32_t up_psr, pc, npc;
2391 target_sigset_t set;
2392 sigset_t host_set;
2393 int err, i;
2395 sf_addr = env->regwptr[UREG_FP];
2396 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2397 goto segv_and_exit;
2398 #if 0
2399 fprintf(stderr, "sigreturn\n");
2400 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2401 #endif
2402 //cpu_dump_state(env, stderr, fprintf, 0);
2404 /* 1. Make sure we are not getting garbage from the user */
2406 if (sf_addr & 3)
2407 goto segv_and_exit;
2409 err = __get_user(pc, &sf->info.si_regs.pc);
2410 err |= __get_user(npc, &sf->info.si_regs.npc);
2412 if ((pc | npc) & 3)
2413 goto segv_and_exit;
2415 /* 2. Restore the state */
2416 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2418 /* User can only change condition codes and FPU enabling in %psr. */
2419 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2420 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2422 env->pc = pc;
2423 env->npc = npc;
2424 err |= __get_user(env->y, &sf->info.si_regs.y);
2425 for (i=0; i < 8; i++) {
2426 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2428 for (i=0; i < 8; i++) {
2429 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2432 /* FIXME: implement FPU save/restore:
2433 * __get_user(fpu_save, &sf->fpu_save);
2434 * if (fpu_save)
2435 * err |= restore_fpu_state(env, fpu_save);
2438 /* This is pretty much atomic, no amount locking would prevent
2439 * the races which exist anyways.
2441 err |= __get_user(set.sig[0], &sf->info.si_mask);
2442 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2443 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2446 target_to_host_sigset_internal(&host_set, &set);
2447 sigprocmask(SIG_SETMASK, &host_set, NULL);
2449 if (err)
2450 goto segv_and_exit;
2451 unlock_user_struct(sf, sf_addr, 0);
2452 return env->regwptr[0];
2454 segv_and_exit:
2455 unlock_user_struct(sf, sf_addr, 0);
2456 force_sig(TARGET_SIGSEGV);
2459 long do_rt_sigreturn(CPUSPARCState *env)
2461 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2462 return -TARGET_ENOSYS;
2465 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2466 #define MC_TSTATE 0
2467 #define MC_PC 1
2468 #define MC_NPC 2
2469 #define MC_Y 3
2470 #define MC_G1 4
2471 #define MC_G2 5
2472 #define MC_G3 6
2473 #define MC_G4 7
2474 #define MC_G5 8
2475 #define MC_G6 9
2476 #define MC_G7 10
2477 #define MC_O0 11
2478 #define MC_O1 12
2479 #define MC_O2 13
2480 #define MC_O3 14
2481 #define MC_O4 15
2482 #define MC_O5 16
2483 #define MC_O6 17
2484 #define MC_O7 18
2485 #define MC_NGREG 19
2487 typedef abi_ulong target_mc_greg_t;
2488 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2490 struct target_mc_fq {
2491 abi_ulong *mcfq_addr;
2492 uint32_t mcfq_insn;
2495 struct target_mc_fpu {
2496 union {
2497 uint32_t sregs[32];
2498 uint64_t dregs[32];
2499 //uint128_t qregs[16];
2500 } mcfpu_fregs;
2501 abi_ulong mcfpu_fsr;
2502 abi_ulong mcfpu_fprs;
2503 abi_ulong mcfpu_gsr;
2504 struct target_mc_fq *mcfpu_fq;
2505 unsigned char mcfpu_qcnt;
2506 unsigned char mcfpu_qentsz;
2507 unsigned char mcfpu_enab;
2509 typedef struct target_mc_fpu target_mc_fpu_t;
2511 typedef struct {
2512 target_mc_gregset_t mc_gregs;
2513 target_mc_greg_t mc_fp;
2514 target_mc_greg_t mc_i7;
2515 target_mc_fpu_t mc_fpregs;
2516 } target_mcontext_t;
2518 struct target_ucontext {
2519 struct target_ucontext *tuc_link;
2520 abi_ulong tuc_flags;
2521 target_sigset_t tuc_sigmask;
2522 target_mcontext_t tuc_mcontext;
2525 /* A V9 register window */
2526 struct target_reg_window {
2527 abi_ulong locals[8];
2528 abi_ulong ins[8];
2531 #define TARGET_STACK_BIAS 2047
2533 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2534 void sparc64_set_context(CPUSPARCState *env)
2536 abi_ulong ucp_addr;
2537 struct target_ucontext *ucp;
2538 target_mc_gregset_t *grp;
2539 abi_ulong pc, npc, tstate;
2540 abi_ulong fp, i7, w_addr;
2541 int err;
2542 unsigned int i;
2544 ucp_addr = env->regwptr[UREG_I0];
2545 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2546 goto do_sigsegv;
2547 grp = &ucp->tuc_mcontext.mc_gregs;
2548 err = __get_user(pc, &((*grp)[MC_PC]));
2549 err |= __get_user(npc, &((*grp)[MC_NPC]));
2550 if (err || ((pc | npc) & 3))
2551 goto do_sigsegv;
2552 if (env->regwptr[UREG_I1]) {
2553 target_sigset_t target_set;
2554 sigset_t set;
2556 if (TARGET_NSIG_WORDS == 1) {
2557 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2558 goto do_sigsegv;
2559 } else {
2560 abi_ulong *src, *dst;
2561 src = ucp->tuc_sigmask.sig;
2562 dst = target_set.sig;
2563 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2564 err |= __get_user(*dst, src);
2566 if (err)
2567 goto do_sigsegv;
2569 target_to_host_sigset_internal(&set, &target_set);
2570 sigprocmask(SIG_SETMASK, &set, NULL);
2572 env->pc = pc;
2573 env->npc = npc;
2574 err |= __get_user(env->y, &((*grp)[MC_Y]));
2575 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2576 env->asi = (tstate >> 24) & 0xff;
2577 cpu_put_ccr(env, tstate >> 32);
2578 cpu_put_cwp64(env, tstate & 0x1f);
2579 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2580 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2581 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2582 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2583 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2584 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2585 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2586 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2587 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2588 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2589 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2590 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2591 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2592 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2593 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2595 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2596 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2598 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2599 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2600 abi_ulong) != 0)
2601 goto do_sigsegv;
2602 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2603 abi_ulong) != 0)
2604 goto do_sigsegv;
2605 /* FIXME this does not match how the kernel handles the FPU in
2606 * its sparc64_set_context implementation. In particular the FPU
2607 * is only restored if fenab is non-zero in:
2608 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2610 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2612 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2613 for (i = 0; i < 64; i++, src++) {
2614 if (i & 1) {
2615 err |= __get_user(env->fpr[i/2].l.lower, src);
2616 } else {
2617 err |= __get_user(env->fpr[i/2].l.upper, src);
2621 err |= __get_user(env->fsr,
2622 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2623 err |= __get_user(env->gsr,
2624 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2625 if (err)
2626 goto do_sigsegv;
2627 unlock_user_struct(ucp, ucp_addr, 0);
2628 return;
2629 do_sigsegv:
2630 unlock_user_struct(ucp, ucp_addr, 0);
2631 force_sig(TARGET_SIGSEGV);
2634 void sparc64_get_context(CPUSPARCState *env)
2636 abi_ulong ucp_addr;
2637 struct target_ucontext *ucp;
2638 target_mc_gregset_t *grp;
2639 target_mcontext_t *mcp;
2640 abi_ulong fp, i7, w_addr;
2641 int err;
2642 unsigned int i;
2643 target_sigset_t target_set;
2644 sigset_t set;
2646 ucp_addr = env->regwptr[UREG_I0];
2647 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2648 goto do_sigsegv;
2650 mcp = &ucp->tuc_mcontext;
2651 grp = &mcp->mc_gregs;
2653 /* Skip over the trap instruction, first. */
2654 env->pc = env->npc;
2655 env->npc += 4;
2657 err = 0;
2659 sigprocmask(0, NULL, &set);
2660 host_to_target_sigset_internal(&target_set, &set);
2661 if (TARGET_NSIG_WORDS == 1) {
2662 err |= __put_user(target_set.sig[0],
2663 (abi_ulong *)&ucp->tuc_sigmask);
2664 } else {
2665 abi_ulong *src, *dst;
2666 src = target_set.sig;
2667 dst = ucp->tuc_sigmask.sig;
2668 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2669 err |= __put_user(*src, dst);
2671 if (err)
2672 goto do_sigsegv;
2675 /* XXX: tstate must be saved properly */
2676 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2677 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2678 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2679 err |= __put_user(env->y, &((*grp)[MC_Y]));
2680 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2681 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2682 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2683 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2684 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2685 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2686 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2687 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2688 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2689 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2690 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2691 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2692 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2693 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2694 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2696 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2697 fp = i7 = 0;
2698 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2699 abi_ulong) != 0)
2700 goto do_sigsegv;
2701 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2702 abi_ulong) != 0)
2703 goto do_sigsegv;
2704 err |= __put_user(fp, &(mcp->mc_fp));
2705 err |= __put_user(i7, &(mcp->mc_i7));
2708 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2709 for (i = 0; i < 64; i++, dst++) {
2710 if (i & 1) {
2711 err |= __put_user(env->fpr[i/2].l.lower, dst);
2712 } else {
2713 err |= __put_user(env->fpr[i/2].l.upper, dst);
2717 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2718 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2719 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2721 if (err)
2722 goto do_sigsegv;
2723 unlock_user_struct(ucp, ucp_addr, 1);
2724 return;
2725 do_sigsegv:
2726 unlock_user_struct(ucp, ucp_addr, 1);
2727 force_sig(TARGET_SIGSEGV);
2729 #endif
2730 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2732 # if defined(TARGET_ABI_MIPSO32)
2733 struct target_sigcontext {
2734 uint32_t sc_regmask; /* Unused */
2735 uint32_t sc_status;
2736 uint64_t sc_pc;
2737 uint64_t sc_regs[32];
2738 uint64_t sc_fpregs[32];
2739 uint32_t sc_ownedfp; /* Unused */
2740 uint32_t sc_fpc_csr;
2741 uint32_t sc_fpc_eir; /* Unused */
2742 uint32_t sc_used_math;
2743 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2744 uint32_t pad0;
2745 uint64_t sc_mdhi;
2746 uint64_t sc_mdlo;
2747 target_ulong sc_hi1; /* Was sc_cause */
2748 target_ulong sc_lo1; /* Was sc_badvaddr */
2749 target_ulong sc_hi2; /* Was sc_sigset[4] */
2750 target_ulong sc_lo2;
2751 target_ulong sc_hi3;
2752 target_ulong sc_lo3;
2754 # else /* N32 || N64 */
2755 struct target_sigcontext {
2756 uint64_t sc_regs[32];
2757 uint64_t sc_fpregs[32];
2758 uint64_t sc_mdhi;
2759 uint64_t sc_hi1;
2760 uint64_t sc_hi2;
2761 uint64_t sc_hi3;
2762 uint64_t sc_mdlo;
2763 uint64_t sc_lo1;
2764 uint64_t sc_lo2;
2765 uint64_t sc_lo3;
2766 uint64_t sc_pc;
2767 uint32_t sc_fpc_csr;
2768 uint32_t sc_used_math;
2769 uint32_t sc_dsp;
2770 uint32_t sc_reserved;
2772 # endif /* O32 */
2774 struct sigframe {
2775 uint32_t sf_ass[4]; /* argument save space for o32 */
2776 uint32_t sf_code[2]; /* signal trampoline */
2777 struct target_sigcontext sf_sc;
2778 target_sigset_t sf_mask;
2781 struct target_ucontext {
2782 target_ulong tuc_flags;
2783 target_ulong tuc_link;
2784 target_stack_t tuc_stack;
2785 target_ulong pad0;
2786 struct target_sigcontext tuc_mcontext;
2787 target_sigset_t tuc_sigmask;
2790 struct target_rt_sigframe {
2791 uint32_t rs_ass[4]; /* argument save space for o32 */
2792 uint32_t rs_code[2]; /* signal trampoline */
2793 struct target_siginfo rs_info;
2794 struct target_ucontext rs_uc;
2797 /* Install trampoline to jump back from signal handler */
2798 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2800 int err = 0;
2803 * Set up the return code ...
2805 * li v0, __NR__foo_sigreturn
2806 * syscall
2809 err |= __put_user(0x24020000 + syscall, tramp + 0);
2810 err |= __put_user(0x0000000c , tramp + 1);
2811 return err;
2814 static inline int
2815 setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2817 int err = 0;
2818 int i;
2820 err |= __put_user(exception_resume_pc(regs), &sc->sc_pc);
2821 regs->hflags &= ~MIPS_HFLAG_BMASK;
2823 __put_user(0, &sc->sc_regs[0]);
2824 for (i = 1; i < 32; ++i) {
2825 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2828 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2829 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2831 /* Rather than checking for dsp existence, always copy. The storage
2832 would just be garbage otherwise. */
2833 err |= __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2834 err |= __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2835 err |= __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2836 err |= __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2837 err |= __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2838 err |= __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2840 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2841 err |= __put_user(dsp, &sc->sc_dsp);
2844 err |= __put_user(1, &sc->sc_used_math);
2846 for (i = 0; i < 32; ++i) {
2847 err |= __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2850 return err;
2853 static inline int
2854 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2856 int err = 0;
2857 int i;
2859 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2861 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2862 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2864 for (i = 1; i < 32; ++i) {
2865 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2868 err |= __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2869 err |= __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2870 err |= __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2871 err |= __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2872 err |= __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2873 err |= __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2875 uint32_t dsp;
2876 err |= __get_user(dsp, &sc->sc_dsp);
2877 cpu_wrdsp(dsp, 0x3ff, regs);
2880 for (i = 0; i < 32; ++i) {
2881 err |= __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2884 return err;
2888 * Determine which stack to use..
2890 static inline abi_ulong
2891 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2893 unsigned long sp;
2895 /* Default to using normal stack */
2896 sp = regs->active_tc.gpr[29];
2899 * FPU emulator may have its own trampoline active just
2900 * above the user stack, 16-bytes before the next lowest
2901 * 16 byte boundary. Try to avoid trashing it.
2903 sp -= 32;
2905 /* This is the X/Open sanctioned signal stack switching. */
2906 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2907 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2910 return (sp - frame_size) & ~7;
2913 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2915 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2916 env->hflags &= ~MIPS_HFLAG_M16;
2917 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2918 env->active_tc.PC &= ~(target_ulong) 1;
2922 # if defined(TARGET_ABI_MIPSO32)
2923 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2924 static void setup_frame(int sig, struct target_sigaction * ka,
2925 target_sigset_t *set, CPUMIPSState *regs)
2927 struct sigframe *frame;
2928 abi_ulong frame_addr;
2929 int i;
2931 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2932 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2933 goto give_sigsegv;
2935 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2937 if(setup_sigcontext(regs, &frame->sf_sc))
2938 goto give_sigsegv;
2940 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2941 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2942 goto give_sigsegv;
2946 * Arguments to signal handler:
2948 * a0 = signal number
2949 * a1 = 0 (should be cause)
2950 * a2 = pointer to struct sigcontext
2952 * $25 and PC point to the signal handler, $29 points to the
2953 * struct sigframe.
2955 regs->active_tc.gpr[ 4] = sig;
2956 regs->active_tc.gpr[ 5] = 0;
2957 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2958 regs->active_tc.gpr[29] = frame_addr;
2959 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2960 /* The original kernel code sets CP0_EPC to the handler
2961 * since it returns to userland using eret
2962 * we cannot do this here, and we must set PC directly */
2963 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2964 mips_set_hflags_isa_mode_from_pc(regs);
2965 unlock_user_struct(frame, frame_addr, 1);
2966 return;
2968 give_sigsegv:
2969 unlock_user_struct(frame, frame_addr, 1);
2970 force_sig(TARGET_SIGSEGV/*, current*/);
2973 long do_sigreturn(CPUMIPSState *regs)
2975 struct sigframe *frame;
2976 abi_ulong frame_addr;
2977 sigset_t blocked;
2978 target_sigset_t target_set;
2979 int i;
2981 #if defined(DEBUG_SIGNAL)
2982 fprintf(stderr, "do_sigreturn\n");
2983 #endif
2984 frame_addr = regs->active_tc.gpr[29];
2985 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2986 goto badframe;
2988 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2989 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2990 goto badframe;
2993 target_to_host_sigset_internal(&blocked, &target_set);
2994 sigprocmask(SIG_SETMASK, &blocked, NULL);
2996 if (restore_sigcontext(regs, &frame->sf_sc))
2997 goto badframe;
2999 #if 0
3001 * Don't let your children do this ...
3003 __asm__ __volatile__(
3004 "move\t$29, %0\n\t"
3005 "j\tsyscall_exit"
3006 :/* no outputs */
3007 :"r" (&regs));
3008 /* Unreached */
3009 #endif
3011 regs->active_tc.PC = regs->CP0_EPC;
3012 mips_set_hflags_isa_mode_from_pc(regs);
3013 /* I am not sure this is right, but it seems to work
3014 * maybe a problem with nested signals ? */
3015 regs->CP0_EPC = 0;
3016 return -TARGET_QEMU_ESIGRETURN;
3018 badframe:
3019 force_sig(TARGET_SIGSEGV/*, current*/);
3020 return 0;
3022 # endif /* O32 */
3024 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3025 target_siginfo_t *info,
3026 target_sigset_t *set, CPUMIPSState *env)
3028 struct target_rt_sigframe *frame;
3029 abi_ulong frame_addr;
3030 int i;
3032 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3033 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3034 goto give_sigsegv;
3036 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3038 copy_siginfo_to_user(&frame->rs_info, info);
3040 __put_user(0, &frame->rs_uc.tuc_flags);
3041 __put_user(0, &frame->rs_uc.tuc_link);
3042 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3043 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3044 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3045 &frame->rs_uc.tuc_stack.ss_flags);
3047 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3049 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3050 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3054 * Arguments to signal handler:
3056 * a0 = signal number
3057 * a1 = pointer to siginfo_t
3058 * a2 = pointer to struct ucontext
3060 * $25 and PC point to the signal handler, $29 points to the
3061 * struct sigframe.
3063 env->active_tc.gpr[ 4] = sig;
3064 env->active_tc.gpr[ 5] = frame_addr
3065 + offsetof(struct target_rt_sigframe, rs_info);
3066 env->active_tc.gpr[ 6] = frame_addr
3067 + offsetof(struct target_rt_sigframe, rs_uc);
3068 env->active_tc.gpr[29] = frame_addr;
3069 env->active_tc.gpr[31] = frame_addr
3070 + offsetof(struct target_rt_sigframe, rs_code);
3071 /* The original kernel code sets CP0_EPC to the handler
3072 * since it returns to userland using eret
3073 * we cannot do this here, and we must set PC directly */
3074 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3075 mips_set_hflags_isa_mode_from_pc(env);
3076 unlock_user_struct(frame, frame_addr, 1);
3077 return;
3079 give_sigsegv:
3080 unlock_user_struct(frame, frame_addr, 1);
3081 force_sig(TARGET_SIGSEGV/*, current*/);
3084 long do_rt_sigreturn(CPUMIPSState *env)
3086 struct target_rt_sigframe *frame;
3087 abi_ulong frame_addr;
3088 sigset_t blocked;
3090 #if defined(DEBUG_SIGNAL)
3091 fprintf(stderr, "do_rt_sigreturn\n");
3092 #endif
3093 frame_addr = env->active_tc.gpr[29];
3094 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3095 goto badframe;
3097 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3098 sigprocmask(SIG_SETMASK, &blocked, NULL);
3100 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
3101 goto badframe;
3103 if (do_sigaltstack(frame_addr +
3104 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3105 0, get_sp_from_cpustate(env)) == -EFAULT)
3106 goto badframe;
3108 env->active_tc.PC = env->CP0_EPC;
3109 mips_set_hflags_isa_mode_from_pc(env);
3110 /* I am not sure this is right, but it seems to work
3111 * maybe a problem with nested signals ? */
3112 env->CP0_EPC = 0;
3113 return -TARGET_QEMU_ESIGRETURN;
3115 badframe:
3116 force_sig(TARGET_SIGSEGV/*, current*/);
3117 return 0;
3120 #elif defined(TARGET_SH4)
3123 * code and data structures from linux kernel:
3124 * include/asm-sh/sigcontext.h
3125 * arch/sh/kernel/signal.c
3128 struct target_sigcontext {
3129 target_ulong oldmask;
3131 /* CPU registers */
3132 target_ulong sc_gregs[16];
3133 target_ulong sc_pc;
3134 target_ulong sc_pr;
3135 target_ulong sc_sr;
3136 target_ulong sc_gbr;
3137 target_ulong sc_mach;
3138 target_ulong sc_macl;
3140 /* FPU registers */
3141 target_ulong sc_fpregs[16];
3142 target_ulong sc_xfpregs[16];
3143 unsigned int sc_fpscr;
3144 unsigned int sc_fpul;
3145 unsigned int sc_ownedfp;
3148 struct target_sigframe
3150 struct target_sigcontext sc;
3151 target_ulong extramask[TARGET_NSIG_WORDS-1];
3152 uint16_t retcode[3];
3156 struct target_ucontext {
3157 target_ulong tuc_flags;
3158 struct target_ucontext *tuc_link;
3159 target_stack_t tuc_stack;
3160 struct target_sigcontext tuc_mcontext;
3161 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3164 struct target_rt_sigframe
3166 struct target_siginfo info;
3167 struct target_ucontext uc;
3168 uint16_t retcode[3];
3172 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3173 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3175 static abi_ulong get_sigframe(struct target_sigaction *ka,
3176 unsigned long sp, size_t frame_size)
3178 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3179 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3182 return (sp - frame_size) & -8ul;
3185 static int setup_sigcontext(struct target_sigcontext *sc,
3186 CPUSH4State *regs, unsigned long mask)
3188 int err = 0;
3189 int i;
3191 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
3192 COPY(gregs[0]); COPY(gregs[1]);
3193 COPY(gregs[2]); COPY(gregs[3]);
3194 COPY(gregs[4]); COPY(gregs[5]);
3195 COPY(gregs[6]); COPY(gregs[7]);
3196 COPY(gregs[8]); COPY(gregs[9]);
3197 COPY(gregs[10]); COPY(gregs[11]);
3198 COPY(gregs[12]); COPY(gregs[13]);
3199 COPY(gregs[14]); COPY(gregs[15]);
3200 COPY(gbr); COPY(mach);
3201 COPY(macl); COPY(pr);
3202 COPY(sr); COPY(pc);
3203 #undef COPY
3205 for (i=0; i<16; i++) {
3206 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3208 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
3209 err |= __put_user(regs->fpul, &sc->sc_fpul);
3211 /* non-iBCS2 extensions.. */
3212 err |= __put_user(mask, &sc->oldmask);
3214 return err;
3217 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3218 target_ulong *r0_p)
3220 unsigned int err = 0;
3221 int i;
3223 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3224 COPY(gregs[1]);
3225 COPY(gregs[2]); COPY(gregs[3]);
3226 COPY(gregs[4]); COPY(gregs[5]);
3227 COPY(gregs[6]); COPY(gregs[7]);
3228 COPY(gregs[8]); COPY(gregs[9]);
3229 COPY(gregs[10]); COPY(gregs[11]);
3230 COPY(gregs[12]); COPY(gregs[13]);
3231 COPY(gregs[14]); COPY(gregs[15]);
3232 COPY(gbr); COPY(mach);
3233 COPY(macl); COPY(pr);
3234 COPY(sr); COPY(pc);
3235 #undef COPY
3237 for (i=0; i<16; i++) {
3238 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3240 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3241 err |= __get_user(regs->fpul, &sc->sc_fpul);
3243 regs->tra = -1; /* disable syscall checks */
3244 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3245 return err;
3248 static void setup_frame(int sig, struct target_sigaction *ka,
3249 target_sigset_t *set, CPUSH4State *regs)
3251 struct target_sigframe *frame;
3252 abi_ulong frame_addr;
3253 int i;
3254 int err = 0;
3255 int signal;
3257 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3258 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3259 goto give_sigsegv;
3261 signal = current_exec_domain_sig(sig);
3263 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3265 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3266 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3269 /* Set up to return from userspace. If provided, use a stub
3270 already in userspace. */
3271 if (ka->sa_flags & TARGET_SA_RESTORER) {
3272 regs->pr = (unsigned long) ka->sa_restorer;
3273 } else {
3274 /* Generate return code (system call to sigreturn) */
3275 err |= __put_user(MOVW(2), &frame->retcode[0]);
3276 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3277 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3278 regs->pr = (unsigned long) frame->retcode;
3281 if (err)
3282 goto give_sigsegv;
3284 /* Set up registers for signal handler */
3285 regs->gregs[15] = frame_addr;
3286 regs->gregs[4] = signal; /* Arg for signal handler */
3287 regs->gregs[5] = 0;
3288 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3289 regs->pc = (unsigned long) ka->_sa_handler;
3291 unlock_user_struct(frame, frame_addr, 1);
3292 return;
3294 give_sigsegv:
3295 unlock_user_struct(frame, frame_addr, 1);
3296 force_sig(TARGET_SIGSEGV);
3299 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3300 target_siginfo_t *info,
3301 target_sigset_t *set, CPUSH4State *regs)
3303 struct target_rt_sigframe *frame;
3304 abi_ulong frame_addr;
3305 int i;
3306 int err = 0;
3307 int signal;
3309 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3310 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3311 goto give_sigsegv;
3313 signal = current_exec_domain_sig(sig);
3315 err |= copy_siginfo_to_user(&frame->info, info);
3317 /* Create the ucontext. */
3318 err |= __put_user(0, &frame->uc.tuc_flags);
3319 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3320 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3321 &frame->uc.tuc_stack.ss_sp);
3322 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3323 &frame->uc.tuc_stack.ss_flags);
3324 err |= __put_user(target_sigaltstack_used.ss_size,
3325 &frame->uc.tuc_stack.ss_size);
3326 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3327 regs, set->sig[0]);
3328 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3329 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3332 /* Set up to return from userspace. If provided, use a stub
3333 already in userspace. */
3334 if (ka->sa_flags & TARGET_SA_RESTORER) {
3335 regs->pr = (unsigned long) ka->sa_restorer;
3336 } else {
3337 /* Generate return code (system call to sigreturn) */
3338 err |= __put_user(MOVW(2), &frame->retcode[0]);
3339 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3340 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3341 regs->pr = (unsigned long) frame->retcode;
3344 if (err)
3345 goto give_sigsegv;
3347 /* Set up registers for signal handler */
3348 regs->gregs[15] = frame_addr;
3349 regs->gregs[4] = signal; /* Arg for signal handler */
3350 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3351 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3352 regs->pc = (unsigned long) ka->_sa_handler;
3354 unlock_user_struct(frame, frame_addr, 1);
3355 return;
3357 give_sigsegv:
3358 unlock_user_struct(frame, frame_addr, 1);
3359 force_sig(TARGET_SIGSEGV);
3362 long do_sigreturn(CPUSH4State *regs)
3364 struct target_sigframe *frame;
3365 abi_ulong frame_addr;
3366 sigset_t blocked;
3367 target_sigset_t target_set;
3368 target_ulong r0;
3369 int i;
3370 int err = 0;
3372 #if defined(DEBUG_SIGNAL)
3373 fprintf(stderr, "do_sigreturn\n");
3374 #endif
3375 frame_addr = regs->gregs[15];
3376 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3377 goto badframe;
3379 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3380 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3381 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3384 if (err)
3385 goto badframe;
3387 target_to_host_sigset_internal(&blocked, &target_set);
3388 sigprocmask(SIG_SETMASK, &blocked, NULL);
3390 if (restore_sigcontext(regs, &frame->sc, &r0))
3391 goto badframe;
3393 unlock_user_struct(frame, frame_addr, 0);
3394 return r0;
3396 badframe:
3397 unlock_user_struct(frame, frame_addr, 0);
3398 force_sig(TARGET_SIGSEGV);
3399 return 0;
3402 long do_rt_sigreturn(CPUSH4State *regs)
3404 struct target_rt_sigframe *frame;
3405 abi_ulong frame_addr;
3406 sigset_t blocked;
3407 target_ulong r0;
3409 #if defined(DEBUG_SIGNAL)
3410 fprintf(stderr, "do_rt_sigreturn\n");
3411 #endif
3412 frame_addr = regs->gregs[15];
3413 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3414 goto badframe;
3416 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3417 sigprocmask(SIG_SETMASK, &blocked, NULL);
3419 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3420 goto badframe;
3422 if (do_sigaltstack(frame_addr +
3423 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3424 0, get_sp_from_cpustate(regs)) == -EFAULT)
3425 goto badframe;
3427 unlock_user_struct(frame, frame_addr, 0);
3428 return r0;
3430 badframe:
3431 unlock_user_struct(frame, frame_addr, 0);
3432 force_sig(TARGET_SIGSEGV);
3433 return 0;
3435 #elif defined(TARGET_MICROBLAZE)
3437 struct target_sigcontext {
3438 struct target_pt_regs regs; /* needs to be first */
3439 uint32_t oldmask;
3442 struct target_stack_t {
3443 abi_ulong ss_sp;
3444 int ss_flags;
3445 unsigned int ss_size;
3448 struct target_ucontext {
3449 abi_ulong tuc_flags;
3450 abi_ulong tuc_link;
3451 struct target_stack_t tuc_stack;
3452 struct target_sigcontext tuc_mcontext;
3453 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3456 /* Signal frames. */
3457 struct target_signal_frame {
3458 struct target_ucontext uc;
3459 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3460 uint32_t tramp[2];
3463 struct rt_signal_frame {
3464 siginfo_t info;
3465 struct ucontext uc;
3466 uint32_t tramp[2];
3469 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3471 __put_user(env->regs[0], &sc->regs.r0);
3472 __put_user(env->regs[1], &sc->regs.r1);
3473 __put_user(env->regs[2], &sc->regs.r2);
3474 __put_user(env->regs[3], &sc->regs.r3);
3475 __put_user(env->regs[4], &sc->regs.r4);
3476 __put_user(env->regs[5], &sc->regs.r5);
3477 __put_user(env->regs[6], &sc->regs.r6);
3478 __put_user(env->regs[7], &sc->regs.r7);
3479 __put_user(env->regs[8], &sc->regs.r8);
3480 __put_user(env->regs[9], &sc->regs.r9);
3481 __put_user(env->regs[10], &sc->regs.r10);
3482 __put_user(env->regs[11], &sc->regs.r11);
3483 __put_user(env->regs[12], &sc->regs.r12);
3484 __put_user(env->regs[13], &sc->regs.r13);
3485 __put_user(env->regs[14], &sc->regs.r14);
3486 __put_user(env->regs[15], &sc->regs.r15);
3487 __put_user(env->regs[16], &sc->regs.r16);
3488 __put_user(env->regs[17], &sc->regs.r17);
3489 __put_user(env->regs[18], &sc->regs.r18);
3490 __put_user(env->regs[19], &sc->regs.r19);
3491 __put_user(env->regs[20], &sc->regs.r20);
3492 __put_user(env->regs[21], &sc->regs.r21);
3493 __put_user(env->regs[22], &sc->regs.r22);
3494 __put_user(env->regs[23], &sc->regs.r23);
3495 __put_user(env->regs[24], &sc->regs.r24);
3496 __put_user(env->regs[25], &sc->regs.r25);
3497 __put_user(env->regs[26], &sc->regs.r26);
3498 __put_user(env->regs[27], &sc->regs.r27);
3499 __put_user(env->regs[28], &sc->regs.r28);
3500 __put_user(env->regs[29], &sc->regs.r29);
3501 __put_user(env->regs[30], &sc->regs.r30);
3502 __put_user(env->regs[31], &sc->regs.r31);
3503 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3506 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3508 __get_user(env->regs[0], &sc->regs.r0);
3509 __get_user(env->regs[1], &sc->regs.r1);
3510 __get_user(env->regs[2], &sc->regs.r2);
3511 __get_user(env->regs[3], &sc->regs.r3);
3512 __get_user(env->regs[4], &sc->regs.r4);
3513 __get_user(env->regs[5], &sc->regs.r5);
3514 __get_user(env->regs[6], &sc->regs.r6);
3515 __get_user(env->regs[7], &sc->regs.r7);
3516 __get_user(env->regs[8], &sc->regs.r8);
3517 __get_user(env->regs[9], &sc->regs.r9);
3518 __get_user(env->regs[10], &sc->regs.r10);
3519 __get_user(env->regs[11], &sc->regs.r11);
3520 __get_user(env->regs[12], &sc->regs.r12);
3521 __get_user(env->regs[13], &sc->regs.r13);
3522 __get_user(env->regs[14], &sc->regs.r14);
3523 __get_user(env->regs[15], &sc->regs.r15);
3524 __get_user(env->regs[16], &sc->regs.r16);
3525 __get_user(env->regs[17], &sc->regs.r17);
3526 __get_user(env->regs[18], &sc->regs.r18);
3527 __get_user(env->regs[19], &sc->regs.r19);
3528 __get_user(env->regs[20], &sc->regs.r20);
3529 __get_user(env->regs[21], &sc->regs.r21);
3530 __get_user(env->regs[22], &sc->regs.r22);
3531 __get_user(env->regs[23], &sc->regs.r23);
3532 __get_user(env->regs[24], &sc->regs.r24);
3533 __get_user(env->regs[25], &sc->regs.r25);
3534 __get_user(env->regs[26], &sc->regs.r26);
3535 __get_user(env->regs[27], &sc->regs.r27);
3536 __get_user(env->regs[28], &sc->regs.r28);
3537 __get_user(env->regs[29], &sc->regs.r29);
3538 __get_user(env->regs[30], &sc->regs.r30);
3539 __get_user(env->regs[31], &sc->regs.r31);
3540 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3543 static abi_ulong get_sigframe(struct target_sigaction *ka,
3544 CPUMBState *env, int frame_size)
3546 abi_ulong sp = env->regs[1];
3548 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3549 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3551 return ((sp - frame_size) & -8UL);
3554 static void setup_frame(int sig, struct target_sigaction *ka,
3555 target_sigset_t *set, CPUMBState *env)
3557 struct target_signal_frame *frame;
3558 abi_ulong frame_addr;
3559 int err = 0;
3560 int i;
3562 frame_addr = get_sigframe(ka, env, sizeof *frame);
3563 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3564 goto badframe;
3566 /* Save the mask. */
3567 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3568 if (err)
3569 goto badframe;
3571 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3572 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3573 goto badframe;
3576 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3578 /* Set up to return from userspace. If provided, use a stub
3579 already in userspace. */
3580 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3581 if (ka->sa_flags & TARGET_SA_RESTORER) {
3582 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3583 } else {
3584 uint32_t t;
3585 /* Note, these encodings are _big endian_! */
3586 /* addi r12, r0, __NR_sigreturn */
3587 t = 0x31800000UL | TARGET_NR_sigreturn;
3588 err |= __put_user(t, frame->tramp + 0);
3589 /* brki r14, 0x8 */
3590 t = 0xb9cc0008UL;
3591 err |= __put_user(t, frame->tramp + 1);
3593 /* Return from sighandler will jump to the tramp.
3594 Negative 8 offset because return is rtsd r15, 8 */
3595 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3598 if (err)
3599 goto badframe;
3601 /* Set up registers for signal handler */
3602 env->regs[1] = frame_addr;
3603 /* Signal handler args: */
3604 env->regs[5] = sig; /* Arg 0: signum */
3605 env->regs[6] = 0;
3606 /* arg 1: sigcontext */
3607 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3609 /* Offset of 4 to handle microblaze rtid r14, 0 */
3610 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3612 unlock_user_struct(frame, frame_addr, 1);
3613 return;
3614 badframe:
3615 unlock_user_struct(frame, frame_addr, 1);
3616 force_sig(TARGET_SIGSEGV);
3619 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3620 target_siginfo_t *info,
3621 target_sigset_t *set, CPUMBState *env)
3623 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3626 long do_sigreturn(CPUMBState *env)
3628 struct target_signal_frame *frame;
3629 abi_ulong frame_addr;
3630 target_sigset_t target_set;
3631 sigset_t set;
3632 int i;
3634 frame_addr = env->regs[R_SP];
3635 /* Make sure the guest isn't playing games. */
3636 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3637 goto badframe;
3639 /* Restore blocked signals */
3640 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3641 goto badframe;
3642 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3643 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3644 goto badframe;
3646 target_to_host_sigset_internal(&set, &target_set);
3647 sigprocmask(SIG_SETMASK, &set, NULL);
3649 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3650 /* We got here through a sigreturn syscall, our path back is via an
3651 rtb insn so setup r14 for that. */
3652 env->regs[14] = env->sregs[SR_PC];
3654 unlock_user_struct(frame, frame_addr, 0);
3655 return env->regs[10];
3656 badframe:
3657 unlock_user_struct(frame, frame_addr, 0);
3658 force_sig(TARGET_SIGSEGV);
3661 long do_rt_sigreturn(CPUMBState *env)
3663 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3664 return -TARGET_ENOSYS;
3667 #elif defined(TARGET_CRIS)
3669 struct target_sigcontext {
3670 struct target_pt_regs regs; /* needs to be first */
3671 uint32_t oldmask;
3672 uint32_t usp; /* usp before stacking this gunk on it */
3675 /* Signal frames. */
3676 struct target_signal_frame {
3677 struct target_sigcontext sc;
3678 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3679 uint16_t retcode[4]; /* Trampoline code. */
3682 struct rt_signal_frame {
3683 siginfo_t *pinfo;
3684 void *puc;
3685 siginfo_t info;
3686 struct ucontext uc;
3687 uint16_t retcode[4]; /* Trampoline code. */
3690 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3692 __put_user(env->regs[0], &sc->regs.r0);
3693 __put_user(env->regs[1], &sc->regs.r1);
3694 __put_user(env->regs[2], &sc->regs.r2);
3695 __put_user(env->regs[3], &sc->regs.r3);
3696 __put_user(env->regs[4], &sc->regs.r4);
3697 __put_user(env->regs[5], &sc->regs.r5);
3698 __put_user(env->regs[6], &sc->regs.r6);
3699 __put_user(env->regs[7], &sc->regs.r7);
3700 __put_user(env->regs[8], &sc->regs.r8);
3701 __put_user(env->regs[9], &sc->regs.r9);
3702 __put_user(env->regs[10], &sc->regs.r10);
3703 __put_user(env->regs[11], &sc->regs.r11);
3704 __put_user(env->regs[12], &sc->regs.r12);
3705 __put_user(env->regs[13], &sc->regs.r13);
3706 __put_user(env->regs[14], &sc->usp);
3707 __put_user(env->regs[15], &sc->regs.acr);
3708 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3709 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3710 __put_user(env->pc, &sc->regs.erp);
3713 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3715 __get_user(env->regs[0], &sc->regs.r0);
3716 __get_user(env->regs[1], &sc->regs.r1);
3717 __get_user(env->regs[2], &sc->regs.r2);
3718 __get_user(env->regs[3], &sc->regs.r3);
3719 __get_user(env->regs[4], &sc->regs.r4);
3720 __get_user(env->regs[5], &sc->regs.r5);
3721 __get_user(env->regs[6], &sc->regs.r6);
3722 __get_user(env->regs[7], &sc->regs.r7);
3723 __get_user(env->regs[8], &sc->regs.r8);
3724 __get_user(env->regs[9], &sc->regs.r9);
3725 __get_user(env->regs[10], &sc->regs.r10);
3726 __get_user(env->regs[11], &sc->regs.r11);
3727 __get_user(env->regs[12], &sc->regs.r12);
3728 __get_user(env->regs[13], &sc->regs.r13);
3729 __get_user(env->regs[14], &sc->usp);
3730 __get_user(env->regs[15], &sc->regs.acr);
3731 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3732 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3733 __get_user(env->pc, &sc->regs.erp);
3736 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3738 abi_ulong sp;
3739 /* Align the stack downwards to 4. */
3740 sp = (env->regs[R_SP] & ~3);
3741 return sp - framesize;
3744 static void setup_frame(int sig, struct target_sigaction *ka,
3745 target_sigset_t *set, CPUCRISState *env)
3747 struct target_signal_frame *frame;
3748 abi_ulong frame_addr;
3749 int err = 0;
3750 int i;
3752 frame_addr = get_sigframe(env, sizeof *frame);
3753 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3754 goto badframe;
3757 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3758 * use this trampoline anymore but it sets it up for GDB.
3759 * In QEMU, using the trampoline simplifies things a bit so we use it.
3761 * This is movu.w __NR_sigreturn, r9; break 13;
3763 err |= __put_user(0x9c5f, frame->retcode+0);
3764 err |= __put_user(TARGET_NR_sigreturn,
3765 frame->retcode + 1);
3766 err |= __put_user(0xe93d, frame->retcode + 2);
3768 /* Save the mask. */
3769 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3770 if (err)
3771 goto badframe;
3773 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3774 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3775 goto badframe;
3778 setup_sigcontext(&frame->sc, env);
3780 /* Move the stack and setup the arguments for the handler. */
3781 env->regs[R_SP] = frame_addr;
3782 env->regs[10] = sig;
3783 env->pc = (unsigned long) ka->_sa_handler;
3784 /* Link SRP so the guest returns through the trampoline. */
3785 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3787 unlock_user_struct(frame, frame_addr, 1);
3788 return;
3789 badframe:
3790 unlock_user_struct(frame, frame_addr, 1);
3791 force_sig(TARGET_SIGSEGV);
3794 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3795 target_siginfo_t *info,
3796 target_sigset_t *set, CPUCRISState *env)
3798 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3801 long do_sigreturn(CPUCRISState *env)
3803 struct target_signal_frame *frame;
3804 abi_ulong frame_addr;
3805 target_sigset_t target_set;
3806 sigset_t set;
3807 int i;
3809 frame_addr = env->regs[R_SP];
3810 /* Make sure the guest isn't playing games. */
3811 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3812 goto badframe;
3814 /* Restore blocked signals */
3815 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3816 goto badframe;
3817 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3818 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3819 goto badframe;
3821 target_to_host_sigset_internal(&set, &target_set);
3822 sigprocmask(SIG_SETMASK, &set, NULL);
3824 restore_sigcontext(&frame->sc, env);
3825 unlock_user_struct(frame, frame_addr, 0);
3826 return env->regs[10];
3827 badframe:
3828 unlock_user_struct(frame, frame_addr, 0);
3829 force_sig(TARGET_SIGSEGV);
3832 long do_rt_sigreturn(CPUCRISState *env)
3834 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3835 return -TARGET_ENOSYS;
3838 #elif defined(TARGET_OPENRISC)
3840 struct target_sigcontext {
3841 struct target_pt_regs regs;
3842 abi_ulong oldmask;
3843 abi_ulong usp;
3846 struct target_ucontext {
3847 abi_ulong tuc_flags;
3848 abi_ulong tuc_link;
3849 target_stack_t tuc_stack;
3850 struct target_sigcontext tuc_mcontext;
3851 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3854 struct target_rt_sigframe {
3855 abi_ulong pinfo;
3856 uint64_t puc;
3857 struct target_siginfo info;
3858 struct target_sigcontext sc;
3859 struct target_ucontext uc;
3860 unsigned char retcode[16]; /* trampoline code */
3863 /* This is the asm-generic/ucontext.h version */
3864 #if 0
3865 static int restore_sigcontext(CPUOpenRISCState *regs,
3866 struct target_sigcontext *sc)
3868 unsigned int err = 0;
3869 unsigned long old_usp;
3871 /* Alwys make any pending restarted system call return -EINTR */
3872 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3874 /* restore the regs from &sc->regs (same as sc, since regs is first)
3875 * (sc is already checked for VERIFY_READ since the sigframe was
3876 * checked in sys_sigreturn previously)
3879 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3880 goto badframe;
3883 /* make sure the U-flag is set so user-mode cannot fool us */
3885 regs->sr &= ~SR_SM;
3887 /* restore the old USP as it was before we stacked the sc etc.
3888 * (we cannot just pop the sigcontext since we aligned the sp and
3889 * stuff after pushing it)
3892 err |= __get_user(old_usp, &sc->usp);
3893 phx_signal("old_usp 0x%lx", old_usp);
3895 __PHX__ REALLY /* ??? */
3896 wrusp(old_usp);
3897 regs->gpr[1] = old_usp;
3899 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3900 * after this completes, but we don't use that mechanism. maybe we can
3901 * use it now ?
3904 return err;
3906 badframe:
3907 return 1;
3909 #endif
3911 /* Set up a signal frame. */
3913 static int setup_sigcontext(struct target_sigcontext *sc,
3914 CPUOpenRISCState *regs,
3915 unsigned long mask)
3917 int err = 0;
3918 unsigned long usp = regs->gpr[1];
3920 /* copy the regs. they are first in sc so we can use sc directly */
3922 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3924 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3925 the signal handler. The frametype will be restored to its previous
3926 value in restore_sigcontext. */
3927 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3929 /* then some other stuff */
3930 err |= __put_user(mask, &sc->oldmask);
3931 err |= __put_user(usp, &sc->usp); return err;
3934 static inline unsigned long align_sigframe(unsigned long sp)
3936 unsigned long i;
3937 i = sp & ~3UL;
3938 return i;
3941 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3942 CPUOpenRISCState *regs,
3943 size_t frame_size)
3945 unsigned long sp = regs->gpr[1];
3946 int onsigstack = on_sig_stack(sp);
3948 /* redzone */
3949 /* This is the X/Open sanctioned signal stack switching. */
3950 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3951 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3954 sp = align_sigframe(sp - frame_size);
3957 * If we are on the alternate signal stack and would overflow it, don't.
3958 * Return an always-bogus address instead so we will die with SIGSEGV.
3961 if (onsigstack && !likely(on_sig_stack(sp))) {
3962 return -1L;
3965 return sp;
3968 static void setup_frame(int sig, struct target_sigaction *ka,
3969 target_sigset_t *set, CPUOpenRISCState *env)
3971 qemu_log("Not implement.\n");
3974 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3975 target_siginfo_t *info,
3976 target_sigset_t *set, CPUOpenRISCState *env)
3978 int err = 0;
3979 abi_ulong frame_addr;
3980 unsigned long return_ip;
3981 struct target_rt_sigframe *frame;
3982 abi_ulong info_addr, uc_addr;
3984 frame_addr = get_sigframe(ka, env, sizeof *frame);
3986 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3987 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3988 goto give_sigsegv;
3991 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3992 err |= __put_user(info_addr, &frame->pinfo);
3993 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3994 err |= __put_user(uc_addr, &frame->puc);
3996 if (ka->sa_flags & SA_SIGINFO) {
3997 err |= copy_siginfo_to_user(&frame->info, info);
3999 if (err) {
4000 goto give_sigsegv;
4003 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
4004 err |= __put_user(0, &frame->uc.tuc_flags);
4005 err |= __put_user(0, &frame->uc.tuc_link);
4006 err |= __put_user(target_sigaltstack_used.ss_sp,
4007 &frame->uc.tuc_stack.ss_sp);
4008 err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
4009 err |= __put_user(target_sigaltstack_used.ss_size,
4010 &frame->uc.tuc_stack.ss_size);
4011 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4013 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4015 if (err) {
4016 goto give_sigsegv;
4019 /* trampoline - the desired return ip is the retcode itself */
4020 return_ip = (unsigned long)&frame->retcode;
4021 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
4022 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
4023 err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
4024 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
4025 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
4027 if (err) {
4028 goto give_sigsegv;
4031 /* TODO what is the current->exec_domain stuff and invmap ? */
4033 /* Set up registers for signal handler */
4034 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
4035 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
4036 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
4037 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
4038 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
4040 /* actually move the usp to reflect the stacked frame */
4041 env->gpr[1] = (unsigned long)frame;
4043 return;
4045 give_sigsegv:
4046 unlock_user_struct(frame, frame_addr, 1);
4047 if (sig == TARGET_SIGSEGV) {
4048 ka->_sa_handler = TARGET_SIG_DFL;
4050 force_sig(TARGET_SIGSEGV);
4053 long do_sigreturn(CPUOpenRISCState *env)
4056 qemu_log("do_sigreturn: not implemented\n");
4057 return -TARGET_ENOSYS;
4060 long do_rt_sigreturn(CPUOpenRISCState *env)
4062 qemu_log("do_rt_sigreturn: not implemented\n");
4063 return -TARGET_ENOSYS;
4065 /* TARGET_OPENRISC */
4067 #elif defined(TARGET_S390X)
4069 #define __NUM_GPRS 16
4070 #define __NUM_FPRS 16
4071 #define __NUM_ACRS 16
4073 #define S390_SYSCALL_SIZE 2
4074 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4076 #define _SIGCONTEXT_NSIG 64
4077 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4078 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4079 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4080 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4081 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4083 typedef struct {
4084 target_psw_t psw;
4085 target_ulong gprs[__NUM_GPRS];
4086 unsigned int acrs[__NUM_ACRS];
4087 } target_s390_regs_common;
4089 typedef struct {
4090 unsigned int fpc;
4091 double fprs[__NUM_FPRS];
4092 } target_s390_fp_regs;
4094 typedef struct {
4095 target_s390_regs_common regs;
4096 target_s390_fp_regs fpregs;
4097 } target_sigregs;
4099 struct target_sigcontext {
4100 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4101 target_sigregs *sregs;
4104 typedef struct {
4105 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4106 struct target_sigcontext sc;
4107 target_sigregs sregs;
4108 int signo;
4109 uint8_t retcode[S390_SYSCALL_SIZE];
4110 } sigframe;
4112 struct target_ucontext {
4113 target_ulong tuc_flags;
4114 struct target_ucontext *tuc_link;
4115 target_stack_t tuc_stack;
4116 target_sigregs tuc_mcontext;
4117 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4120 typedef struct {
4121 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4122 uint8_t retcode[S390_SYSCALL_SIZE];
4123 struct target_siginfo info;
4124 struct target_ucontext uc;
4125 } rt_sigframe;
4127 static inline abi_ulong
4128 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4130 abi_ulong sp;
4132 /* Default to using normal stack */
4133 sp = env->regs[15];
4135 /* This is the X/Open sanctioned signal stack switching. */
4136 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4137 if (!sas_ss_flags(sp)) {
4138 sp = target_sigaltstack_used.ss_sp +
4139 target_sigaltstack_used.ss_size;
4143 /* This is the legacy signal stack switching. */
4144 else if (/* FIXME !user_mode(regs) */ 0 &&
4145 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4146 ka->sa_restorer) {
4147 sp = (abi_ulong) ka->sa_restorer;
4150 return (sp - frame_size) & -8ul;
4153 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4155 int i;
4156 //save_access_regs(current->thread.acrs); FIXME
4158 /* Copy a 'clean' PSW mask to the user to avoid leaking
4159 information about whether PER is currently on. */
4160 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4161 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4162 for (i = 0; i < 16; i++) {
4163 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4165 for (i = 0; i < 16; i++) {
4166 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4169 * We have to store the fp registers to current->thread.fp_regs
4170 * to merge them with the emulated registers.
4172 //save_fp_regs(&current->thread.fp_regs); FIXME
4173 for (i = 0; i < 16; i++) {
4174 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
4178 static void setup_frame(int sig, struct target_sigaction *ka,
4179 target_sigset_t *set, CPUS390XState *env)
4181 sigframe *frame;
4182 abi_ulong frame_addr;
4184 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4185 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4186 (unsigned long long)frame_addr);
4187 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4188 goto give_sigsegv;
4191 qemu_log("%s: 1\n", __FUNCTION__);
4192 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
4193 goto give_sigsegv;
4196 save_sigregs(env, &frame->sregs);
4198 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4199 (abi_ulong *)&frame->sc.sregs);
4201 /* Set up to return from userspace. If provided, use a stub
4202 already in userspace. */
4203 if (ka->sa_flags & TARGET_SA_RESTORER) {
4204 env->regs[14] = (unsigned long)
4205 ka->sa_restorer | PSW_ADDR_AMODE;
4206 } else {
4207 env->regs[14] = (unsigned long)
4208 frame->retcode | PSW_ADDR_AMODE;
4209 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4210 (uint16_t *)(frame->retcode)))
4211 goto give_sigsegv;
4214 /* Set up backchain. */
4215 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4216 goto give_sigsegv;
4219 /* Set up registers for signal handler */
4220 env->regs[15] = frame_addr;
4221 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4223 env->regs[2] = sig; //map_signal(sig);
4224 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4226 /* We forgot to include these in the sigcontext.
4227 To avoid breaking binary compatibility, they are passed as args. */
4228 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4229 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4231 /* Place signal number on stack to allow backtrace from handler. */
4232 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4233 goto give_sigsegv;
4235 unlock_user_struct(frame, frame_addr, 1);
4236 return;
4238 give_sigsegv:
4239 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4240 unlock_user_struct(frame, frame_addr, 1);
4241 force_sig(TARGET_SIGSEGV);
4244 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4245 target_siginfo_t *info,
4246 target_sigset_t *set, CPUS390XState *env)
4248 int i;
4249 rt_sigframe *frame;
4250 abi_ulong frame_addr;
4252 frame_addr = get_sigframe(ka, env, sizeof *frame);
4253 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4254 (unsigned long long)frame_addr);
4255 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4256 goto give_sigsegv;
4259 qemu_log("%s: 1\n", __FUNCTION__);
4260 if (copy_siginfo_to_user(&frame->info, info)) {
4261 goto give_sigsegv;
4264 /* Create the ucontext. */
4265 __put_user(0, &frame->uc.tuc_flags);
4266 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4267 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4268 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4269 &frame->uc.tuc_stack.ss_flags);
4270 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4271 save_sigregs(env, &frame->uc.tuc_mcontext);
4272 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4273 __put_user((abi_ulong)set->sig[i],
4274 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4277 /* Set up to return from userspace. If provided, use a stub
4278 already in userspace. */
4279 if (ka->sa_flags & TARGET_SA_RESTORER) {
4280 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4281 } else {
4282 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4283 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4284 (uint16_t *)(frame->retcode))) {
4285 goto give_sigsegv;
4289 /* Set up backchain. */
4290 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4291 goto give_sigsegv;
4294 /* Set up registers for signal handler */
4295 env->regs[15] = frame_addr;
4296 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4298 env->regs[2] = sig; //map_signal(sig);
4299 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4300 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4301 return;
4303 give_sigsegv:
4304 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4305 unlock_user_struct(frame, frame_addr, 1);
4306 force_sig(TARGET_SIGSEGV);
4309 static int
4310 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4312 int err = 0;
4313 int i;
4315 for (i = 0; i < 16; i++) {
4316 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4319 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4320 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4321 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4322 (unsigned long long)env->psw.addr);
4323 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4324 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4326 for (i = 0; i < 16; i++) {
4327 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4329 for (i = 0; i < 16; i++) {
4330 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4333 return err;
4336 long do_sigreturn(CPUS390XState *env)
4338 sigframe *frame;
4339 abi_ulong frame_addr = env->regs[15];
4340 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4341 (unsigned long long)frame_addr);
4342 target_sigset_t target_set;
4343 sigset_t set;
4345 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4346 goto badframe;
4348 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4349 goto badframe;
4352 target_to_host_sigset_internal(&set, &target_set);
4353 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4355 if (restore_sigregs(env, &frame->sregs)) {
4356 goto badframe;
4359 unlock_user_struct(frame, frame_addr, 0);
4360 return env->regs[2];
4362 badframe:
4363 unlock_user_struct(frame, frame_addr, 0);
4364 force_sig(TARGET_SIGSEGV);
4365 return 0;
4368 long do_rt_sigreturn(CPUS390XState *env)
4370 rt_sigframe *frame;
4371 abi_ulong frame_addr = env->regs[15];
4372 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4373 (unsigned long long)frame_addr);
4374 sigset_t set;
4376 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4377 goto badframe;
4379 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4381 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4383 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4384 goto badframe;
4387 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4388 get_sp_from_cpustate(env)) == -EFAULT) {
4389 goto badframe;
4391 unlock_user_struct(frame, frame_addr, 0);
4392 return env->regs[2];
4394 badframe:
4395 unlock_user_struct(frame, frame_addr, 0);
4396 force_sig(TARGET_SIGSEGV);
4397 return 0;
4400 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4402 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4403 the signal handling is different enough that we haven't implemented
4404 support for PPC64 yet. Hence the restriction above.
4406 There are various #if'd blocks for code for TARGET_PPC64. These
4407 blocks should go away so that we can successfully run 32-bit and
4408 64-bit binaries on a QEMU configured for PPC64. */
4410 /* Size of dummy stack frame allocated when calling signal handler.
4411 See arch/powerpc/include/asm/ptrace.h. */
4412 #if defined(TARGET_PPC64)
4413 #define SIGNAL_FRAMESIZE 128
4414 #else
4415 #define SIGNAL_FRAMESIZE 64
4416 #endif
4418 /* See arch/powerpc/include/asm/sigcontext.h. */
4419 struct target_sigcontext {
4420 target_ulong _unused[4];
4421 int32_t signal;
4422 #if defined(TARGET_PPC64)
4423 int32_t pad0;
4424 #endif
4425 target_ulong handler;
4426 target_ulong oldmask;
4427 target_ulong regs; /* struct pt_regs __user * */
4428 /* TODO: PPC64 includes extra bits here. */
4431 /* Indices for target_mcontext.mc_gregs, below.
4432 See arch/powerpc/include/asm/ptrace.h for details. */
4433 enum {
4434 TARGET_PT_R0 = 0,
4435 TARGET_PT_R1 = 1,
4436 TARGET_PT_R2 = 2,
4437 TARGET_PT_R3 = 3,
4438 TARGET_PT_R4 = 4,
4439 TARGET_PT_R5 = 5,
4440 TARGET_PT_R6 = 6,
4441 TARGET_PT_R7 = 7,
4442 TARGET_PT_R8 = 8,
4443 TARGET_PT_R9 = 9,
4444 TARGET_PT_R10 = 10,
4445 TARGET_PT_R11 = 11,
4446 TARGET_PT_R12 = 12,
4447 TARGET_PT_R13 = 13,
4448 TARGET_PT_R14 = 14,
4449 TARGET_PT_R15 = 15,
4450 TARGET_PT_R16 = 16,
4451 TARGET_PT_R17 = 17,
4452 TARGET_PT_R18 = 18,
4453 TARGET_PT_R19 = 19,
4454 TARGET_PT_R20 = 20,
4455 TARGET_PT_R21 = 21,
4456 TARGET_PT_R22 = 22,
4457 TARGET_PT_R23 = 23,
4458 TARGET_PT_R24 = 24,
4459 TARGET_PT_R25 = 25,
4460 TARGET_PT_R26 = 26,
4461 TARGET_PT_R27 = 27,
4462 TARGET_PT_R28 = 28,
4463 TARGET_PT_R29 = 29,
4464 TARGET_PT_R30 = 30,
4465 TARGET_PT_R31 = 31,
4466 TARGET_PT_NIP = 32,
4467 TARGET_PT_MSR = 33,
4468 TARGET_PT_ORIG_R3 = 34,
4469 TARGET_PT_CTR = 35,
4470 TARGET_PT_LNK = 36,
4471 TARGET_PT_XER = 37,
4472 TARGET_PT_CCR = 38,
4473 /* Yes, there are two registers with #39. One is 64-bit only. */
4474 TARGET_PT_MQ = 39,
4475 TARGET_PT_SOFTE = 39,
4476 TARGET_PT_TRAP = 40,
4477 TARGET_PT_DAR = 41,
4478 TARGET_PT_DSISR = 42,
4479 TARGET_PT_RESULT = 43,
4480 TARGET_PT_REGS_COUNT = 44
4483 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4484 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4485 struct target_mcontext {
4486 target_ulong mc_gregs[48];
4487 /* Includes fpscr. */
4488 uint64_t mc_fregs[33];
4489 target_ulong mc_pad[2];
4490 /* We need to handle Altivec and SPE at the same time, which no
4491 kernel needs to do. Fortunately, the kernel defines this bit to
4492 be Altivec-register-large all the time, rather than trying to
4493 twiddle it based on the specific platform. */
4494 union {
4495 /* SPE vector registers. One extra for SPEFSCR. */
4496 uint32_t spe[33];
4497 /* Altivec vector registers. The packing of VSCR and VRSAVE
4498 varies depending on whether we're PPC64 or not: PPC64 splits
4499 them apart; PPC32 stuffs them together. */
4500 #if defined(TARGET_PPC64)
4501 #define QEMU_NVRREG 34
4502 #else
4503 #define QEMU_NVRREG 33
4504 #endif
4505 ppc_avr_t altivec[QEMU_NVRREG];
4506 #undef QEMU_NVRREG
4507 } mc_vregs __attribute__((__aligned__(16)));
4510 struct target_ucontext {
4511 target_ulong tuc_flags;
4512 target_ulong tuc_link; /* struct ucontext __user * */
4513 struct target_sigaltstack tuc_stack;
4514 #if !defined(TARGET_PPC64)
4515 int32_t tuc_pad[7];
4516 target_ulong tuc_regs; /* struct mcontext __user *
4517 points to uc_mcontext field */
4518 #endif
4519 target_sigset_t tuc_sigmask;
4520 #if defined(TARGET_PPC64)
4521 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4522 struct target_sigcontext tuc_mcontext;
4523 #else
4524 int32_t tuc_maskext[30];
4525 int32_t tuc_pad2[3];
4526 struct target_mcontext tuc_mcontext;
4527 #endif
4530 /* See arch/powerpc/kernel/signal_32.c. */
4531 struct target_sigframe {
4532 struct target_sigcontext sctx;
4533 struct target_mcontext mctx;
4534 int32_t abigap[56];
4537 struct target_rt_sigframe {
4538 struct target_siginfo info;
4539 struct target_ucontext uc;
4540 int32_t abigap[56];
4543 /* We use the mc_pad field for the signal return trampoline. */
4544 #define tramp mc_pad
4546 /* See arch/powerpc/kernel/signal.c. */
4547 static target_ulong get_sigframe(struct target_sigaction *ka,
4548 CPUPPCState *env,
4549 int frame_size)
4551 target_ulong oldsp, newsp;
4553 oldsp = env->gpr[1];
4555 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4556 (sas_ss_flags(oldsp) == 0)) {
4557 oldsp = (target_sigaltstack_used.ss_sp
4558 + target_sigaltstack_used.ss_size);
4561 newsp = (oldsp - frame_size) & ~0xFUL;
4563 return newsp;
4566 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4567 int sigret)
4569 target_ulong msr = env->msr;
4570 int i;
4571 target_ulong ccr = 0;
4573 /* In general, the kernel attempts to be intelligent about what it
4574 needs to save for Altivec/FP/SPE registers. We don't care that
4575 much, so we just go ahead and save everything. */
4577 /* Save general registers. */
4578 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4579 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4580 return 1;
4583 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4584 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4585 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4586 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4587 return 1;
4589 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4590 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4592 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4593 return 1;
4595 /* Save Altivec registers if necessary. */
4596 if (env->insns_flags & PPC_ALTIVEC) {
4597 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4598 ppc_avr_t *avr = &env->avr[i];
4599 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4601 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4602 __put_user(avr->u64[1], &vreg->u64[1])) {
4603 return 1;
4606 /* Set MSR_VR in the saved MSR value to indicate that
4607 frame->mc_vregs contains valid data. */
4608 msr |= MSR_VR;
4609 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4610 &frame->mc_vregs.altivec[32].u32[3]))
4611 return 1;
4614 /* Save floating point registers. */
4615 if (env->insns_flags & PPC_FLOAT) {
4616 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4617 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4618 return 1;
4621 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4622 return 1;
4625 /* Save SPE registers. The kernel only saves the high half. */
4626 if (env->insns_flags & PPC_SPE) {
4627 #if defined(TARGET_PPC64)
4628 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4629 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4630 return 1;
4633 #else
4634 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4635 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4636 return 1;
4639 #endif
4640 /* Set MSR_SPE in the saved MSR value to indicate that
4641 frame->mc_vregs contains valid data. */
4642 msr |= MSR_SPE;
4643 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4644 return 1;
4647 /* Store MSR. */
4648 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4649 return 1;
4651 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4652 if (sigret) {
4653 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4654 __put_user(0x44000002UL, &frame->tramp[1])) {
4655 return 1;
4659 return 0;
4662 static int restore_user_regs(CPUPPCState *env,
4663 struct target_mcontext *frame, int sig)
4665 target_ulong save_r2 = 0;
4666 target_ulong msr;
4667 target_ulong ccr;
4669 int i;
4671 if (!sig) {
4672 save_r2 = env->gpr[2];
4675 /* Restore general registers. */
4676 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4677 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4678 return 1;
4681 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4682 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4683 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4684 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4685 return 1;
4686 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4687 return 1;
4689 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4690 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4693 if (!sig) {
4694 env->gpr[2] = save_r2;
4696 /* Restore MSR. */
4697 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4698 return 1;
4700 /* If doing signal return, restore the previous little-endian mode. */
4701 if (sig)
4702 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4704 /* Restore Altivec registers if necessary. */
4705 if (env->insns_flags & PPC_ALTIVEC) {
4706 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4707 ppc_avr_t *avr = &env->avr[i];
4708 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4710 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4711 __get_user(avr->u64[1], &vreg->u64[1])) {
4712 return 1;
4715 /* Set MSR_VEC in the saved MSR value to indicate that
4716 frame->mc_vregs contains valid data. */
4717 if (__get_user(env->spr[SPR_VRSAVE],
4718 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4719 return 1;
4722 /* Restore floating point registers. */
4723 if (env->insns_flags & PPC_FLOAT) {
4724 uint64_t fpscr;
4725 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4726 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4727 return 1;
4730 if (__get_user(fpscr, &frame->mc_fregs[32]))
4731 return 1;
4732 env->fpscr = (uint32_t) fpscr;
4735 /* Save SPE registers. The kernel only saves the high half. */
4736 if (env->insns_flags & PPC_SPE) {
4737 #if defined(TARGET_PPC64)
4738 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4739 uint32_t hi;
4741 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4742 return 1;
4744 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4746 #else
4747 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4748 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4749 return 1;
4752 #endif
4753 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4754 return 1;
4757 return 0;
4760 static void setup_frame(int sig, struct target_sigaction *ka,
4761 target_sigset_t *set, CPUPPCState *env)
4763 struct target_sigframe *frame;
4764 struct target_sigcontext *sc;
4765 target_ulong frame_addr, newsp;
4766 int err = 0;
4767 int signal;
4769 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4770 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4771 goto sigsegv;
4772 sc = &frame->sctx;
4774 signal = current_exec_domain_sig(sig);
4776 err |= __put_user(ka->_sa_handler, &sc->handler);
4777 err |= __put_user(set->sig[0], &sc->oldmask);
4778 #if defined(TARGET_PPC64)
4779 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4780 #else
4781 err |= __put_user(set->sig[1], &sc->_unused[3]);
4782 #endif
4783 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4784 err |= __put_user(sig, &sc->signal);
4786 /* Save user regs. */
4787 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4789 /* The kernel checks for the presence of a VDSO here. We don't
4790 emulate a vdso, so use a sigreturn system call. */
4791 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4793 /* Turn off all fp exceptions. */
4794 env->fpscr = 0;
4796 /* Create a stack frame for the caller of the handler. */
4797 newsp = frame_addr - SIGNAL_FRAMESIZE;
4798 err |= put_user(env->gpr[1], newsp, target_ulong);
4800 if (err)
4801 goto sigsegv;
4803 /* Set up registers for signal handler. */
4804 env->gpr[1] = newsp;
4805 env->gpr[3] = signal;
4806 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4807 env->nip = (target_ulong) ka->_sa_handler;
4808 /* Signal handlers are entered in big-endian mode. */
4809 env->msr &= ~MSR_LE;
4811 unlock_user_struct(frame, frame_addr, 1);
4812 return;
4814 sigsegv:
4815 unlock_user_struct(frame, frame_addr, 1);
4816 qemu_log("segfaulting from setup_frame\n");
4817 force_sig(TARGET_SIGSEGV);
4820 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4821 target_siginfo_t *info,
4822 target_sigset_t *set, CPUPPCState *env)
4824 struct target_rt_sigframe *rt_sf;
4825 struct target_mcontext *frame;
4826 target_ulong rt_sf_addr, newsp = 0;
4827 int i, err = 0;
4828 int signal;
4830 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4831 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4832 goto sigsegv;
4834 signal = current_exec_domain_sig(sig);
4836 err |= copy_siginfo_to_user(&rt_sf->info, info);
4838 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4839 err |= __put_user(0, &rt_sf->uc.tuc_link);
4840 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4841 &rt_sf->uc.tuc_stack.ss_sp);
4842 err |= __put_user(sas_ss_flags(env->gpr[1]),
4843 &rt_sf->uc.tuc_stack.ss_flags);
4844 err |= __put_user(target_sigaltstack_used.ss_size,
4845 &rt_sf->uc.tuc_stack.ss_size);
4846 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4847 &rt_sf->uc.tuc_regs);
4848 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4849 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4852 frame = &rt_sf->uc.tuc_mcontext;
4853 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4855 /* The kernel checks for the presence of a VDSO here. We don't
4856 emulate a vdso, so use a sigreturn system call. */
4857 env->lr = (target_ulong) h2g(frame->tramp);
4859 /* Turn off all fp exceptions. */
4860 env->fpscr = 0;
4862 /* Create a stack frame for the caller of the handler. */
4863 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4864 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4866 if (err)
4867 goto sigsegv;
4869 /* Set up registers for signal handler. */
4870 env->gpr[1] = newsp;
4871 env->gpr[3] = (target_ulong) signal;
4872 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4873 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4874 env->gpr[6] = (target_ulong) h2g(rt_sf);
4875 env->nip = (target_ulong) ka->_sa_handler;
4876 /* Signal handlers are entered in big-endian mode. */
4877 env->msr &= ~MSR_LE;
4879 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4880 return;
4882 sigsegv:
4883 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4884 qemu_log("segfaulting from setup_rt_frame\n");
4885 force_sig(TARGET_SIGSEGV);
4889 long do_sigreturn(CPUPPCState *env)
4891 struct target_sigcontext *sc = NULL;
4892 struct target_mcontext *sr = NULL;
4893 target_ulong sr_addr = 0, sc_addr;
4894 sigset_t blocked;
4895 target_sigset_t set;
4897 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4898 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4899 goto sigsegv;
4901 #if defined(TARGET_PPC64)
4902 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4903 #else
4904 if(__get_user(set.sig[0], &sc->oldmask) ||
4905 __get_user(set.sig[1], &sc->_unused[3]))
4906 goto sigsegv;
4907 #endif
4908 target_to_host_sigset_internal(&blocked, &set);
4909 sigprocmask(SIG_SETMASK, &blocked, NULL);
4911 if (__get_user(sr_addr, &sc->regs))
4912 goto sigsegv;
4913 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4914 goto sigsegv;
4915 if (restore_user_regs(env, sr, 1))
4916 goto sigsegv;
4918 unlock_user_struct(sr, sr_addr, 1);
4919 unlock_user_struct(sc, sc_addr, 1);
4920 return -TARGET_QEMU_ESIGRETURN;
4922 sigsegv:
4923 unlock_user_struct(sr, sr_addr, 1);
4924 unlock_user_struct(sc, sc_addr, 1);
4925 qemu_log("segfaulting from do_sigreturn\n");
4926 force_sig(TARGET_SIGSEGV);
4927 return 0;
4930 /* See arch/powerpc/kernel/signal_32.c. */
4931 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4933 struct target_mcontext *mcp;
4934 target_ulong mcp_addr;
4935 sigset_t blocked;
4936 target_sigset_t set;
4938 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4939 sizeof (set)))
4940 return 1;
4942 #if defined(TARGET_PPC64)
4943 fprintf (stderr, "do_setcontext: not implemented\n");
4944 return 0;
4945 #else
4946 if (__get_user(mcp_addr, &ucp->tuc_regs))
4947 return 1;
4949 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4950 return 1;
4952 target_to_host_sigset_internal(&blocked, &set);
4953 sigprocmask(SIG_SETMASK, &blocked, NULL);
4954 if (restore_user_regs(env, mcp, sig))
4955 goto sigsegv;
4957 unlock_user_struct(mcp, mcp_addr, 1);
4958 return 0;
4960 sigsegv:
4961 unlock_user_struct(mcp, mcp_addr, 1);
4962 return 1;
4963 #endif
4966 long do_rt_sigreturn(CPUPPCState *env)
4968 struct target_rt_sigframe *rt_sf = NULL;
4969 target_ulong rt_sf_addr;
4971 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4972 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4973 goto sigsegv;
4975 if (do_setcontext(&rt_sf->uc, env, 1))
4976 goto sigsegv;
4978 do_sigaltstack(rt_sf_addr
4979 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4980 0, env->gpr[1]);
4982 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4983 return -TARGET_QEMU_ESIGRETURN;
4985 sigsegv:
4986 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4987 qemu_log("segfaulting from do_rt_sigreturn\n");
4988 force_sig(TARGET_SIGSEGV);
4989 return 0;
4992 #elif defined(TARGET_M68K)
4994 struct target_sigcontext {
4995 abi_ulong sc_mask;
4996 abi_ulong sc_usp;
4997 abi_ulong sc_d0;
4998 abi_ulong sc_d1;
4999 abi_ulong sc_a0;
5000 abi_ulong sc_a1;
5001 unsigned short sc_sr;
5002 abi_ulong sc_pc;
5005 struct target_sigframe
5007 abi_ulong pretcode;
5008 int sig;
5009 int code;
5010 abi_ulong psc;
5011 char retcode[8];
5012 abi_ulong extramask[TARGET_NSIG_WORDS-1];
5013 struct target_sigcontext sc;
5016 typedef int target_greg_t;
5017 #define TARGET_NGREG 18
5018 typedef target_greg_t target_gregset_t[TARGET_NGREG];
5020 typedef struct target_fpregset {
5021 int f_fpcntl[3];
5022 int f_fpregs[8*3];
5023 } target_fpregset_t;
5025 struct target_mcontext {
5026 int version;
5027 target_gregset_t gregs;
5028 target_fpregset_t fpregs;
5031 #define TARGET_MCONTEXT_VERSION 2
5033 struct target_ucontext {
5034 abi_ulong tuc_flags;
5035 abi_ulong tuc_link;
5036 target_stack_t tuc_stack;
5037 struct target_mcontext tuc_mcontext;
5038 abi_long tuc_filler[80];
5039 target_sigset_t tuc_sigmask;
5042 struct target_rt_sigframe
5044 abi_ulong pretcode;
5045 int sig;
5046 abi_ulong pinfo;
5047 abi_ulong puc;
5048 char retcode[8];
5049 struct target_siginfo info;
5050 struct target_ucontext uc;
5053 static int
5054 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
5055 abi_ulong mask)
5057 int err = 0;
5059 err |= __put_user(mask, &sc->sc_mask);
5060 err |= __put_user(env->aregs[7], &sc->sc_usp);
5061 err |= __put_user(env->dregs[0], &sc->sc_d0);
5062 err |= __put_user(env->dregs[1], &sc->sc_d1);
5063 err |= __put_user(env->aregs[0], &sc->sc_a0);
5064 err |= __put_user(env->aregs[1], &sc->sc_a1);
5065 err |= __put_user(env->sr, &sc->sc_sr);
5066 err |= __put_user(env->pc, &sc->sc_pc);
5068 return err;
5071 static int
5072 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
5074 int err = 0;
5075 int temp;
5077 err |= __get_user(env->aregs[7], &sc->sc_usp);
5078 err |= __get_user(env->dregs[1], &sc->sc_d1);
5079 err |= __get_user(env->aregs[0], &sc->sc_a0);
5080 err |= __get_user(env->aregs[1], &sc->sc_a1);
5081 err |= __get_user(env->pc, &sc->sc_pc);
5082 err |= __get_user(temp, &sc->sc_sr);
5083 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5085 *pd0 = tswapl(sc->sc_d0);
5087 return err;
5091 * Determine which stack to use..
5093 static inline abi_ulong
5094 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5095 size_t frame_size)
5097 unsigned long sp;
5099 sp = regs->aregs[7];
5101 /* This is the X/Open sanctioned signal stack switching. */
5102 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5103 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5106 return ((sp - frame_size) & -8UL);
5109 static void setup_frame(int sig, struct target_sigaction *ka,
5110 target_sigset_t *set, CPUM68KState *env)
5112 struct target_sigframe *frame;
5113 abi_ulong frame_addr;
5114 abi_ulong retcode_addr;
5115 abi_ulong sc_addr;
5116 int err = 0;
5117 int i;
5119 frame_addr = get_sigframe(ka, env, sizeof *frame);
5120 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5121 goto give_sigsegv;
5123 err |= __put_user(sig, &frame->sig);
5125 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5126 err |= __put_user(sc_addr, &frame->psc);
5128 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
5129 if (err)
5130 goto give_sigsegv;
5132 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5133 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
5134 goto give_sigsegv;
5137 /* Set up to return from userspace. */
5139 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5140 err |= __put_user(retcode_addr, &frame->pretcode);
5142 /* moveq #,d0; trap #0 */
5144 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5145 (long *)(frame->retcode));
5147 if (err)
5148 goto give_sigsegv;
5150 /* Set up to return from userspace */
5152 env->aregs[7] = frame_addr;
5153 env->pc = ka->_sa_handler;
5155 unlock_user_struct(frame, frame_addr, 1);
5156 return;
5158 give_sigsegv:
5159 unlock_user_struct(frame, frame_addr, 1);
5160 force_sig(TARGET_SIGSEGV);
5163 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5164 CPUM68KState *env)
5166 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5167 int err;
5169 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5170 err |= __put_user(env->dregs[0], &gregs[0]);
5171 err |= __put_user(env->dregs[1], &gregs[1]);
5172 err |= __put_user(env->dregs[2], &gregs[2]);
5173 err |= __put_user(env->dregs[3], &gregs[3]);
5174 err |= __put_user(env->dregs[4], &gregs[4]);
5175 err |= __put_user(env->dregs[5], &gregs[5]);
5176 err |= __put_user(env->dregs[6], &gregs[6]);
5177 err |= __put_user(env->dregs[7], &gregs[7]);
5178 err |= __put_user(env->aregs[0], &gregs[8]);
5179 err |= __put_user(env->aregs[1], &gregs[9]);
5180 err |= __put_user(env->aregs[2], &gregs[10]);
5181 err |= __put_user(env->aregs[3], &gregs[11]);
5182 err |= __put_user(env->aregs[4], &gregs[12]);
5183 err |= __put_user(env->aregs[5], &gregs[13]);
5184 err |= __put_user(env->aregs[6], &gregs[14]);
5185 err |= __put_user(env->aregs[7], &gregs[15]);
5186 err |= __put_user(env->pc, &gregs[16]);
5187 err |= __put_user(env->sr, &gregs[17]);
5189 return err;
5192 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5193 struct target_ucontext *uc,
5194 int *pd0)
5196 int temp;
5197 int err;
5198 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5200 err = __get_user(temp, &uc->tuc_mcontext.version);
5201 if (temp != TARGET_MCONTEXT_VERSION)
5202 goto badframe;
5204 /* restore passed registers */
5205 err |= __get_user(env->dregs[0], &gregs[0]);
5206 err |= __get_user(env->dregs[1], &gregs[1]);
5207 err |= __get_user(env->dregs[2], &gregs[2]);
5208 err |= __get_user(env->dregs[3], &gregs[3]);
5209 err |= __get_user(env->dregs[4], &gregs[4]);
5210 err |= __get_user(env->dregs[5], &gregs[5]);
5211 err |= __get_user(env->dregs[6], &gregs[6]);
5212 err |= __get_user(env->dregs[7], &gregs[7]);
5213 err |= __get_user(env->aregs[0], &gregs[8]);
5214 err |= __get_user(env->aregs[1], &gregs[9]);
5215 err |= __get_user(env->aregs[2], &gregs[10]);
5216 err |= __get_user(env->aregs[3], &gregs[11]);
5217 err |= __get_user(env->aregs[4], &gregs[12]);
5218 err |= __get_user(env->aregs[5], &gregs[13]);
5219 err |= __get_user(env->aregs[6], &gregs[14]);
5220 err |= __get_user(env->aregs[7], &gregs[15]);
5221 err |= __get_user(env->pc, &gregs[16]);
5222 err |= __get_user(temp, &gregs[17]);
5223 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5225 *pd0 = env->dregs[0];
5226 return err;
5228 badframe:
5229 return 1;
5232 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5233 target_siginfo_t *info,
5234 target_sigset_t *set, CPUM68KState *env)
5236 struct target_rt_sigframe *frame;
5237 abi_ulong frame_addr;
5238 abi_ulong retcode_addr;
5239 abi_ulong info_addr;
5240 abi_ulong uc_addr;
5241 int err = 0;
5242 int i;
5244 frame_addr = get_sigframe(ka, env, sizeof *frame);
5245 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5246 goto give_sigsegv;
5248 err |= __put_user(sig, &frame->sig);
5250 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5251 err |= __put_user(info_addr, &frame->pinfo);
5253 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5254 err |= __put_user(uc_addr, &frame->puc);
5256 err |= copy_siginfo_to_user(&frame->info, info);
5258 /* Create the ucontext */
5260 err |= __put_user(0, &frame->uc.tuc_flags);
5261 err |= __put_user(0, &frame->uc.tuc_link);
5262 err |= __put_user(target_sigaltstack_used.ss_sp,
5263 &frame->uc.tuc_stack.ss_sp);
5264 err |= __put_user(sas_ss_flags(env->aregs[7]),
5265 &frame->uc.tuc_stack.ss_flags);
5266 err |= __put_user(target_sigaltstack_used.ss_size,
5267 &frame->uc.tuc_stack.ss_size);
5268 err |= target_rt_setup_ucontext(&frame->uc, env);
5270 if (err)
5271 goto give_sigsegv;
5273 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5274 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
5275 goto give_sigsegv;
5278 /* Set up to return from userspace. */
5280 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5281 err |= __put_user(retcode_addr, &frame->pretcode);
5283 /* moveq #,d0; notb d0; trap #0 */
5285 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5286 (long *)(frame->retcode + 0));
5287 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5289 if (err)
5290 goto give_sigsegv;
5292 /* Set up to return from userspace */
5294 env->aregs[7] = frame_addr;
5295 env->pc = ka->_sa_handler;
5297 unlock_user_struct(frame, frame_addr, 1);
5298 return;
5300 give_sigsegv:
5301 unlock_user_struct(frame, frame_addr, 1);
5302 force_sig(TARGET_SIGSEGV);
5305 long do_sigreturn(CPUM68KState *env)
5307 struct target_sigframe *frame;
5308 abi_ulong frame_addr = env->aregs[7] - 4;
5309 target_sigset_t target_set;
5310 sigset_t set;
5311 int d0, i;
5313 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5314 goto badframe;
5316 /* set blocked signals */
5318 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5319 goto badframe;
5321 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5322 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5323 goto badframe;
5326 target_to_host_sigset_internal(&set, &target_set);
5327 sigprocmask(SIG_SETMASK, &set, NULL);
5329 /* restore registers */
5331 if (restore_sigcontext(env, &frame->sc, &d0))
5332 goto badframe;
5334 unlock_user_struct(frame, frame_addr, 0);
5335 return d0;
5337 badframe:
5338 unlock_user_struct(frame, frame_addr, 0);
5339 force_sig(TARGET_SIGSEGV);
5340 return 0;
5343 long do_rt_sigreturn(CPUM68KState *env)
5345 struct target_rt_sigframe *frame;
5346 abi_ulong frame_addr = env->aregs[7] - 4;
5347 target_sigset_t target_set;
5348 sigset_t set;
5349 int d0;
5351 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5352 goto badframe;
5354 target_to_host_sigset_internal(&set, &target_set);
5355 sigprocmask(SIG_SETMASK, &set, NULL);
5357 /* restore registers */
5359 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5360 goto badframe;
5362 if (do_sigaltstack(frame_addr +
5363 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5364 0, get_sp_from_cpustate(env)) == -EFAULT)
5365 goto badframe;
5367 unlock_user_struct(frame, frame_addr, 0);
5368 return d0;
5370 badframe:
5371 unlock_user_struct(frame, frame_addr, 0);
5372 force_sig(TARGET_SIGSEGV);
5373 return 0;
5376 #elif defined(TARGET_ALPHA)
5378 struct target_sigcontext {
5379 abi_long sc_onstack;
5380 abi_long sc_mask;
5381 abi_long sc_pc;
5382 abi_long sc_ps;
5383 abi_long sc_regs[32];
5384 abi_long sc_ownedfp;
5385 abi_long sc_fpregs[32];
5386 abi_ulong sc_fpcr;
5387 abi_ulong sc_fp_control;
5388 abi_ulong sc_reserved1;
5389 abi_ulong sc_reserved2;
5390 abi_ulong sc_ssize;
5391 abi_ulong sc_sbase;
5392 abi_ulong sc_traparg_a0;
5393 abi_ulong sc_traparg_a1;
5394 abi_ulong sc_traparg_a2;
5395 abi_ulong sc_fp_trap_pc;
5396 abi_ulong sc_fp_trigger_sum;
5397 abi_ulong sc_fp_trigger_inst;
5400 struct target_ucontext {
5401 abi_ulong tuc_flags;
5402 abi_ulong tuc_link;
5403 abi_ulong tuc_osf_sigmask;
5404 target_stack_t tuc_stack;
5405 struct target_sigcontext tuc_mcontext;
5406 target_sigset_t tuc_sigmask;
5409 struct target_sigframe {
5410 struct target_sigcontext sc;
5411 unsigned int retcode[3];
5414 struct target_rt_sigframe {
5415 target_siginfo_t info;
5416 struct target_ucontext uc;
5417 unsigned int retcode[3];
5420 #define INSN_MOV_R30_R16 0x47fe0410
5421 #define INSN_LDI_R0 0x201f0000
5422 #define INSN_CALLSYS 0x00000083
5424 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5425 abi_ulong frame_addr, target_sigset_t *set)
5427 int i, err = 0;
5429 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5430 err |= __put_user(set->sig[0], &sc->sc_mask);
5431 err |= __put_user(env->pc, &sc->sc_pc);
5432 err |= __put_user(8, &sc->sc_ps);
5434 for (i = 0; i < 31; ++i) {
5435 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5437 err |= __put_user(0, &sc->sc_regs[31]);
5439 for (i = 0; i < 31; ++i) {
5440 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5442 err |= __put_user(0, &sc->sc_fpregs[31]);
5443 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5445 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5446 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5447 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5449 return err;
5452 static int restore_sigcontext(CPUAlphaState *env,
5453 struct target_sigcontext *sc)
5455 uint64_t fpcr;
5456 int i, err = 0;
5458 err |= __get_user(env->pc, &sc->sc_pc);
5460 for (i = 0; i < 31; ++i) {
5461 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5463 for (i = 0; i < 31; ++i) {
5464 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5467 err |= __get_user(fpcr, &sc->sc_fpcr);
5468 cpu_alpha_store_fpcr(env, fpcr);
5470 return err;
5473 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5474 CPUAlphaState *env,
5475 unsigned long framesize)
5477 abi_ulong sp = env->ir[IR_SP];
5479 /* This is the X/Open sanctioned signal stack switching. */
5480 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5481 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5483 return (sp - framesize) & -32;
5486 static void setup_frame(int sig, struct target_sigaction *ka,
5487 target_sigset_t *set, CPUAlphaState *env)
5489 abi_ulong frame_addr, r26;
5490 struct target_sigframe *frame;
5491 int err = 0;
5493 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5494 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5495 goto give_sigsegv;
5498 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5500 if (ka->sa_restorer) {
5501 r26 = ka->sa_restorer;
5502 } else {
5503 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5504 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5505 &frame->retcode[1]);
5506 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5507 /* imb() */
5508 r26 = frame_addr;
5511 unlock_user_struct(frame, frame_addr, 1);
5513 if (err) {
5514 give_sigsegv:
5515 if (sig == TARGET_SIGSEGV) {
5516 ka->_sa_handler = TARGET_SIG_DFL;
5518 force_sig(TARGET_SIGSEGV);
5521 env->ir[IR_RA] = r26;
5522 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5523 env->ir[IR_A0] = sig;
5524 env->ir[IR_A1] = 0;
5525 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5526 env->ir[IR_SP] = frame_addr;
5529 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5530 target_siginfo_t *info,
5531 target_sigset_t *set, CPUAlphaState *env)
5533 abi_ulong frame_addr, r26;
5534 struct target_rt_sigframe *frame;
5535 int i, err = 0;
5537 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5538 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5539 goto give_sigsegv;
5542 err |= copy_siginfo_to_user(&frame->info, info);
5544 err |= __put_user(0, &frame->uc.tuc_flags);
5545 err |= __put_user(0, &frame->uc.tuc_link);
5546 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5547 err |= __put_user(target_sigaltstack_used.ss_sp,
5548 &frame->uc.tuc_stack.ss_sp);
5549 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5550 &frame->uc.tuc_stack.ss_flags);
5551 err |= __put_user(target_sigaltstack_used.ss_size,
5552 &frame->uc.tuc_stack.ss_size);
5553 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5554 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5555 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5558 if (ka->sa_restorer) {
5559 r26 = ka->sa_restorer;
5560 } else {
5561 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5562 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5563 &frame->retcode[1]);
5564 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5565 /* imb(); */
5566 r26 = frame_addr;
5569 if (err) {
5570 give_sigsegv:
5571 if (sig == TARGET_SIGSEGV) {
5572 ka->_sa_handler = TARGET_SIG_DFL;
5574 force_sig(TARGET_SIGSEGV);
5577 env->ir[IR_RA] = r26;
5578 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5579 env->ir[IR_A0] = sig;
5580 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5581 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5582 env->ir[IR_SP] = frame_addr;
5585 long do_sigreturn(CPUAlphaState *env)
5587 struct target_sigcontext *sc;
5588 abi_ulong sc_addr = env->ir[IR_A0];
5589 target_sigset_t target_set;
5590 sigset_t set;
5592 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5593 goto badframe;
5596 target_sigemptyset(&target_set);
5597 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5598 goto badframe;
5601 target_to_host_sigset_internal(&set, &target_set);
5602 sigprocmask(SIG_SETMASK, &set, NULL);
5604 if (restore_sigcontext(env, sc)) {
5605 goto badframe;
5607 unlock_user_struct(sc, sc_addr, 0);
5608 return env->ir[IR_V0];
5610 badframe:
5611 unlock_user_struct(sc, sc_addr, 0);
5612 force_sig(TARGET_SIGSEGV);
5615 long do_rt_sigreturn(CPUAlphaState *env)
5617 abi_ulong frame_addr = env->ir[IR_A0];
5618 struct target_rt_sigframe *frame;
5619 sigset_t set;
5621 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5622 goto badframe;
5624 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5625 sigprocmask(SIG_SETMASK, &set, NULL);
5627 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5628 goto badframe;
5630 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5631 uc.tuc_stack),
5632 0, env->ir[IR_SP]) == -EFAULT) {
5633 goto badframe;
5636 unlock_user_struct(frame, frame_addr, 0);
5637 return env->ir[IR_V0];
5640 badframe:
5641 unlock_user_struct(frame, frame_addr, 0);
5642 force_sig(TARGET_SIGSEGV);
5645 #else
5647 static void setup_frame(int sig, struct target_sigaction *ka,
5648 target_sigset_t *set, CPUArchState *env)
5650 fprintf(stderr, "setup_frame: not implemented\n");
5653 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5654 target_siginfo_t *info,
5655 target_sigset_t *set, CPUArchState *env)
5657 fprintf(stderr, "setup_rt_frame: not implemented\n");
5660 long do_sigreturn(CPUArchState *env)
5662 fprintf(stderr, "do_sigreturn: not implemented\n");
5663 return -TARGET_ENOSYS;
5666 long do_rt_sigreturn(CPUArchState *env)
5668 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5669 return -TARGET_ENOSYS;
5672 #endif
5674 void process_pending_signals(CPUArchState *cpu_env)
5676 CPUState *cpu = ENV_GET_CPU(cpu_env);
5677 int sig;
5678 abi_ulong handler;
5679 sigset_t set, old_set;
5680 target_sigset_t target_old_set;
5681 struct emulated_sigtable *k;
5682 struct target_sigaction *sa;
5683 struct sigqueue *q;
5684 TaskState *ts = cpu->opaque;
5686 if (!ts->signal_pending)
5687 return;
5689 /* FIXME: This is not threadsafe. */
5690 k = ts->sigtab;
5691 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5692 if (k->pending)
5693 goto handle_signal;
5694 k++;
5696 /* if no signal is pending, just return */
5697 ts->signal_pending = 0;
5698 return;
5700 handle_signal:
5701 #ifdef DEBUG_SIGNAL
5702 fprintf(stderr, "qemu: process signal %d\n", sig);
5703 #endif
5704 /* dequeue signal */
5705 q = k->first;
5706 k->first = q->next;
5707 if (!k->first)
5708 k->pending = 0;
5710 sig = gdb_handlesig(cpu, sig);
5711 if (!sig) {
5712 sa = NULL;
5713 handler = TARGET_SIG_IGN;
5714 } else {
5715 sa = &sigact_table[sig - 1];
5716 handler = sa->_sa_handler;
5719 if (handler == TARGET_SIG_DFL) {
5720 /* default handler : ignore some signal. The other are job control or fatal */
5721 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5722 kill(getpid(),SIGSTOP);
5723 } else if (sig != TARGET_SIGCHLD &&
5724 sig != TARGET_SIGURG &&
5725 sig != TARGET_SIGWINCH &&
5726 sig != TARGET_SIGCONT) {
5727 force_sig(sig);
5729 } else if (handler == TARGET_SIG_IGN) {
5730 /* ignore sig */
5731 } else if (handler == TARGET_SIG_ERR) {
5732 force_sig(sig);
5733 } else {
5734 /* compute the blocked signals during the handler execution */
5735 target_to_host_sigset(&set, &sa->sa_mask);
5736 /* SA_NODEFER indicates that the current signal should not be
5737 blocked during the handler */
5738 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5739 sigaddset(&set, target_to_host_signal(sig));
5741 /* block signals in the handler using Linux */
5742 sigprocmask(SIG_BLOCK, &set, &old_set);
5743 /* save the previous blocked signal state to restore it at the
5744 end of the signal execution (see do_sigreturn) */
5745 host_to_target_sigset_internal(&target_old_set, &old_set);
5747 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5748 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5750 CPUX86State *env = cpu_env;
5751 if (env->eflags & VM_MASK)
5752 save_v86_state(env);
5754 #endif
5755 /* prepare the stack frame of the virtual CPU */
5756 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5757 /* These targets do not have traditional signals. */
5758 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5759 #else
5760 if (sa->sa_flags & TARGET_SA_SIGINFO)
5761 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5762 else
5763 setup_frame(sig, sa, &target_old_set, cpu_env);
5764 #endif
5765 if (sa->sa_flags & TARGET_SA_RESETHAND)
5766 sa->_sa_handler = TARGET_SIG_DFL;
5768 if (q != &k->info)
5769 free_sigqueue(cpu_env, q);