dec: convert to realize()
[qemu/ar7.git] / linux-user / signal.c
blob327c03254cb597a5a07afe3ed8a8a8a4f1ac9e92
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 "qemu/osdep.h"
20 #include <sys/ucontext.h>
21 #include <sys/resource.h>
23 #include "qemu.h"
24 #include "qemu-common.h"
25 #include "target_signal.h"
26 #include "trace.h"
28 static struct target_sigaltstack target_sigaltstack_used = {
29 .ss_sp = 0,
30 .ss_size = 0,
31 .ss_flags = TARGET_SS_DISABLE,
34 static struct target_sigaction sigact_table[TARGET_NSIG];
36 static void host_signal_handler(int host_signum, siginfo_t *info,
37 void *puc);
39 static uint8_t host_to_target_signal_table[_NSIG] = {
40 [SIGHUP] = TARGET_SIGHUP,
41 [SIGINT] = TARGET_SIGINT,
42 [SIGQUIT] = TARGET_SIGQUIT,
43 [SIGILL] = TARGET_SIGILL,
44 [SIGTRAP] = TARGET_SIGTRAP,
45 [SIGABRT] = TARGET_SIGABRT,
46 /* [SIGIOT] = TARGET_SIGIOT,*/
47 [SIGBUS] = TARGET_SIGBUS,
48 [SIGFPE] = TARGET_SIGFPE,
49 [SIGKILL] = TARGET_SIGKILL,
50 [SIGUSR1] = TARGET_SIGUSR1,
51 [SIGSEGV] = TARGET_SIGSEGV,
52 [SIGUSR2] = TARGET_SIGUSR2,
53 [SIGPIPE] = TARGET_SIGPIPE,
54 [SIGALRM] = TARGET_SIGALRM,
55 [SIGTERM] = TARGET_SIGTERM,
56 #ifdef SIGSTKFLT
57 [SIGSTKFLT] = TARGET_SIGSTKFLT,
58 #endif
59 [SIGCHLD] = TARGET_SIGCHLD,
60 [SIGCONT] = TARGET_SIGCONT,
61 [SIGSTOP] = TARGET_SIGSTOP,
62 [SIGTSTP] = TARGET_SIGTSTP,
63 [SIGTTIN] = TARGET_SIGTTIN,
64 [SIGTTOU] = TARGET_SIGTTOU,
65 [SIGURG] = TARGET_SIGURG,
66 [SIGXCPU] = TARGET_SIGXCPU,
67 [SIGXFSZ] = TARGET_SIGXFSZ,
68 [SIGVTALRM] = TARGET_SIGVTALRM,
69 [SIGPROF] = TARGET_SIGPROF,
70 [SIGWINCH] = TARGET_SIGWINCH,
71 [SIGIO] = TARGET_SIGIO,
72 [SIGPWR] = TARGET_SIGPWR,
73 [SIGSYS] = TARGET_SIGSYS,
74 /* next signals stay the same */
75 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
76 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
77 To fix this properly we need to do manual signal delivery multiplexed
78 over a single host signal. */
79 [__SIGRTMIN] = __SIGRTMAX,
80 [__SIGRTMAX] = __SIGRTMIN,
82 static uint8_t target_to_host_signal_table[_NSIG];
84 static inline int on_sig_stack(unsigned long sp)
86 return (sp - target_sigaltstack_used.ss_sp
87 < target_sigaltstack_used.ss_size);
90 static inline int sas_ss_flags(unsigned long sp)
92 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
93 : on_sig_stack(sp) ? SS_ONSTACK : 0);
96 int host_to_target_signal(int sig)
98 if (sig < 0 || sig >= _NSIG)
99 return sig;
100 return host_to_target_signal_table[sig];
103 int target_to_host_signal(int sig)
105 if (sig < 0 || sig >= _NSIG)
106 return sig;
107 return target_to_host_signal_table[sig];
110 static inline void target_sigemptyset(target_sigset_t *set)
112 memset(set, 0, sizeof(*set));
115 static inline void target_sigaddset(target_sigset_t *set, int signum)
117 signum--;
118 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
119 set->sig[signum / TARGET_NSIG_BPW] |= mask;
122 static inline int target_sigismember(const target_sigset_t *set, int signum)
124 signum--;
125 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
129 static void host_to_target_sigset_internal(target_sigset_t *d,
130 const sigset_t *s)
132 int i;
133 target_sigemptyset(d);
134 for (i = 1; i <= TARGET_NSIG; i++) {
135 if (sigismember(s, i)) {
136 target_sigaddset(d, host_to_target_signal(i));
141 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
143 target_sigset_t d1;
144 int i;
146 host_to_target_sigset_internal(&d1, s);
147 for(i = 0;i < TARGET_NSIG_WORDS; i++)
148 d->sig[i] = tswapal(d1.sig[i]);
151 static void target_to_host_sigset_internal(sigset_t *d,
152 const target_sigset_t *s)
154 int i;
155 sigemptyset(d);
156 for (i = 1; i <= TARGET_NSIG; i++) {
157 if (target_sigismember(s, i)) {
158 sigaddset(d, target_to_host_signal(i));
163 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
165 target_sigset_t s1;
166 int i;
168 for(i = 0;i < TARGET_NSIG_WORDS; i++)
169 s1.sig[i] = tswapal(s->sig[i]);
170 target_to_host_sigset_internal(d, &s1);
173 void host_to_target_old_sigset(abi_ulong *old_sigset,
174 const sigset_t *sigset)
176 target_sigset_t d;
177 host_to_target_sigset(&d, sigset);
178 *old_sigset = d.sig[0];
181 void target_to_host_old_sigset(sigset_t *sigset,
182 const abi_ulong *old_sigset)
184 target_sigset_t d;
185 int i;
187 d.sig[0] = *old_sigset;
188 for(i = 1;i < TARGET_NSIG_WORDS; i++)
189 d.sig[i] = 0;
190 target_to_host_sigset(sigset, &d);
193 /* Wrapper for sigprocmask function
194 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
195 * are host signal set, not guest ones. This wraps the sigprocmask host calls
196 * that should be protected (calls originated from guest)
198 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
200 int ret;
201 sigset_t val;
202 sigset_t *temp = NULL;
203 CPUState *cpu = thread_cpu;
204 TaskState *ts = (TaskState *)cpu->opaque;
205 bool segv_was_blocked = ts->sigsegv_blocked;
207 if (set) {
208 bool has_sigsegv = sigismember(set, SIGSEGV);
209 val = *set;
210 temp = &val;
212 sigdelset(temp, SIGSEGV);
214 switch (how) {
215 case SIG_BLOCK:
216 if (has_sigsegv) {
217 ts->sigsegv_blocked = true;
219 break;
220 case SIG_UNBLOCK:
221 if (has_sigsegv) {
222 ts->sigsegv_blocked = false;
224 break;
225 case SIG_SETMASK:
226 ts->sigsegv_blocked = has_sigsegv;
227 break;
228 default:
229 g_assert_not_reached();
233 ret = sigprocmask(how, temp, oldset);
235 if (oldset && segv_was_blocked) {
236 sigaddset(oldset, SIGSEGV);
239 return ret;
242 /* siginfo conversion */
244 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
245 const siginfo_t *info)
247 int sig = host_to_target_signal(info->si_signo);
248 tinfo->si_signo = sig;
249 tinfo->si_errno = 0;
250 tinfo->si_code = info->si_code;
252 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
253 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
254 /* Should never come here, but who knows. The information for
255 the target is irrelevant. */
256 tinfo->_sifields._sigfault._addr = 0;
257 } else if (sig == TARGET_SIGIO) {
258 tinfo->_sifields._sigpoll._band = info->si_band;
259 tinfo->_sifields._sigpoll._fd = info->si_fd;
260 } else if (sig == TARGET_SIGCHLD) {
261 tinfo->_sifields._sigchld._pid = info->si_pid;
262 tinfo->_sifields._sigchld._uid = info->si_uid;
263 tinfo->_sifields._sigchld._status
264 = host_to_target_waitstatus(info->si_status);
265 tinfo->_sifields._sigchld._utime = info->si_utime;
266 tinfo->_sifields._sigchld._stime = info->si_stime;
267 } else if (sig >= TARGET_SIGRTMIN) {
268 tinfo->_sifields._rt._pid = info->si_pid;
269 tinfo->_sifields._rt._uid = info->si_uid;
270 /* XXX: potential problem if 64 bit */
271 tinfo->_sifields._rt._sigval.sival_ptr
272 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
276 static void tswap_siginfo(target_siginfo_t *tinfo,
277 const target_siginfo_t *info)
279 int sig = info->si_signo;
280 tinfo->si_signo = tswap32(sig);
281 tinfo->si_errno = tswap32(info->si_errno);
282 tinfo->si_code = tswap32(info->si_code);
284 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
285 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
286 tinfo->_sifields._sigfault._addr
287 = tswapal(info->_sifields._sigfault._addr);
288 } else if (sig == TARGET_SIGIO) {
289 tinfo->_sifields._sigpoll._band
290 = tswap32(info->_sifields._sigpoll._band);
291 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
292 } else if (sig == TARGET_SIGCHLD) {
293 tinfo->_sifields._sigchld._pid
294 = tswap32(info->_sifields._sigchld._pid);
295 tinfo->_sifields._sigchld._uid
296 = tswap32(info->_sifields._sigchld._uid);
297 tinfo->_sifields._sigchld._status
298 = tswap32(info->_sifields._sigchld._status);
299 tinfo->_sifields._sigchld._utime
300 = tswapal(info->_sifields._sigchld._utime);
301 tinfo->_sifields._sigchld._stime
302 = tswapal(info->_sifields._sigchld._stime);
303 } else if (sig >= TARGET_SIGRTMIN) {
304 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
305 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
306 tinfo->_sifields._rt._sigval.sival_ptr
307 = tswapal(info->_sifields._rt._sigval.sival_ptr);
312 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
314 host_to_target_siginfo_noswap(tinfo, info);
315 tswap_siginfo(tinfo, tinfo);
318 /* XXX: we support only POSIX RT signals are used. */
319 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
320 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
322 info->si_signo = tswap32(tinfo->si_signo);
323 info->si_errno = tswap32(tinfo->si_errno);
324 info->si_code = tswap32(tinfo->si_code);
325 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
326 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
327 info->si_value.sival_ptr =
328 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
331 static int fatal_signal (int sig)
333 switch (sig) {
334 case TARGET_SIGCHLD:
335 case TARGET_SIGURG:
336 case TARGET_SIGWINCH:
337 /* Ignored by default. */
338 return 0;
339 case TARGET_SIGCONT:
340 case TARGET_SIGSTOP:
341 case TARGET_SIGTSTP:
342 case TARGET_SIGTTIN:
343 case TARGET_SIGTTOU:
344 /* Job control signals. */
345 return 0;
346 default:
347 return 1;
351 /* returns 1 if given signal should dump core if not handled */
352 static int core_dump_signal(int sig)
354 switch (sig) {
355 case TARGET_SIGABRT:
356 case TARGET_SIGFPE:
357 case TARGET_SIGILL:
358 case TARGET_SIGQUIT:
359 case TARGET_SIGSEGV:
360 case TARGET_SIGTRAP:
361 case TARGET_SIGBUS:
362 return (1);
363 default:
364 return (0);
368 void signal_init(void)
370 struct sigaction act;
371 struct sigaction oact;
372 int i, j;
373 int host_sig;
375 /* generate signal conversion tables */
376 for(i = 1; i < _NSIG; i++) {
377 if (host_to_target_signal_table[i] == 0)
378 host_to_target_signal_table[i] = i;
380 for(i = 1; i < _NSIG; i++) {
381 j = host_to_target_signal_table[i];
382 target_to_host_signal_table[j] = i;
385 /* set all host signal handlers. ALL signals are blocked during
386 the handlers to serialize them. */
387 memset(sigact_table, 0, sizeof(sigact_table));
389 sigfillset(&act.sa_mask);
390 act.sa_flags = SA_SIGINFO;
391 act.sa_sigaction = host_signal_handler;
392 for(i = 1; i <= TARGET_NSIG; i++) {
393 host_sig = target_to_host_signal(i);
394 sigaction(host_sig, NULL, &oact);
395 if (oact.sa_sigaction == (void *)SIG_IGN) {
396 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
397 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
398 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
400 /* If there's already a handler installed then something has
401 gone horribly wrong, so don't even try to handle that case. */
402 /* Install some handlers for our own use. We need at least
403 SIGSEGV and SIGBUS, to detect exceptions. We can not just
404 trap all signals because it affects syscall interrupt
405 behavior. But do trap all default-fatal signals. */
406 if (fatal_signal (i))
407 sigaction(host_sig, &act, NULL);
411 /* signal queue handling */
413 static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
415 CPUState *cpu = ENV_GET_CPU(env);
416 TaskState *ts = cpu->opaque;
417 struct sigqueue *q = ts->first_free;
418 if (!q)
419 return NULL;
420 ts->first_free = q->next;
421 return q;
424 static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
426 CPUState *cpu = ENV_GET_CPU(env);
427 TaskState *ts = cpu->opaque;
429 q->next = ts->first_free;
430 ts->first_free = q;
433 /* abort execution with signal */
434 static void QEMU_NORETURN force_sig(int target_sig)
436 CPUState *cpu = thread_cpu;
437 CPUArchState *env = cpu->env_ptr;
438 TaskState *ts = (TaskState *)cpu->opaque;
439 int host_sig, core_dumped = 0;
440 struct sigaction act;
442 host_sig = target_to_host_signal(target_sig);
443 trace_user_force_sig(env, target_sig, host_sig);
444 gdb_signalled(env, target_sig);
446 /* dump core if supported by target binary format */
447 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
448 stop_all_tasks();
449 core_dumped =
450 ((*ts->bprm->core_dump)(target_sig, env) == 0);
452 if (core_dumped) {
453 /* we already dumped the core of target process, we don't want
454 * a coredump of qemu itself */
455 struct rlimit nodump;
456 getrlimit(RLIMIT_CORE, &nodump);
457 nodump.rlim_cur=0;
458 setrlimit(RLIMIT_CORE, &nodump);
459 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
460 target_sig, strsignal(host_sig), "core dumped" );
463 /* The proper exit code for dying from an uncaught signal is
464 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
465 * a negative value. To get the proper exit code we need to
466 * actually die from an uncaught signal. Here the default signal
467 * handler is installed, we send ourself a signal and we wait for
468 * it to arrive. */
469 sigfillset(&act.sa_mask);
470 act.sa_handler = SIG_DFL;
471 act.sa_flags = 0;
472 sigaction(host_sig, &act, NULL);
474 /* For some reason raise(host_sig) doesn't send the signal when
475 * statically linked on x86-64. */
476 kill(getpid(), host_sig);
478 /* Make sure the signal isn't masked (just reuse the mask inside
479 of act) */
480 sigdelset(&act.sa_mask, host_sig);
481 sigsuspend(&act.sa_mask);
483 /* unreachable */
484 abort();
487 /* queue a signal so that it will be send to the virtual CPU as soon
488 as possible */
489 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
491 CPUState *cpu = ENV_GET_CPU(env);
492 TaskState *ts = cpu->opaque;
493 struct emulated_sigtable *k;
494 struct sigqueue *q, **pq;
495 abi_ulong handler;
496 int queue;
498 trace_user_queue_signal(env, sig);
499 k = &ts->sigtab[sig - 1];
500 queue = gdb_queuesig ();
501 handler = sigact_table[sig - 1]._sa_handler;
503 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
504 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
505 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
506 * because it got a real MMU fault). A blocked SIGSEGV in that
507 * situation is treated as if using the default handler. This is
508 * not correct if some other process has randomly sent us a SIGSEGV
509 * via kill(), but that is not easy to distinguish at this point,
510 * so we assume it doesn't happen.
512 handler = TARGET_SIG_DFL;
515 if (!queue && handler == TARGET_SIG_DFL) {
516 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
517 kill(getpid(),SIGSTOP);
518 return 0;
519 } else
520 /* default handler : ignore some signal. The other are fatal */
521 if (sig != TARGET_SIGCHLD &&
522 sig != TARGET_SIGURG &&
523 sig != TARGET_SIGWINCH &&
524 sig != TARGET_SIGCONT) {
525 force_sig(sig);
526 } else {
527 return 0; /* indicate ignored */
529 } else if (!queue && handler == TARGET_SIG_IGN) {
530 /* ignore signal */
531 return 0;
532 } else if (!queue && handler == TARGET_SIG_ERR) {
533 force_sig(sig);
534 } else {
535 pq = &k->first;
536 if (sig < TARGET_SIGRTMIN) {
537 /* if non real time signal, we queue exactly one signal */
538 if (!k->pending)
539 q = &k->info;
540 else
541 return 0;
542 } else {
543 if (!k->pending) {
544 /* first signal */
545 q = &k->info;
546 } else {
547 q = alloc_sigqueue(env);
548 if (!q)
549 return -EAGAIN;
550 while (*pq != NULL)
551 pq = &(*pq)->next;
554 *pq = q;
555 q->info = *info;
556 q->next = NULL;
557 k->pending = 1;
558 /* signal that a new signal is pending */
559 ts->signal_pending = 1;
560 return 1; /* indicates that the signal was queued */
564 static void host_signal_handler(int host_signum, siginfo_t *info,
565 void *puc)
567 CPUArchState *env = thread_cpu->env_ptr;
568 int sig;
569 target_siginfo_t tinfo;
571 /* the CPU emulator uses some host signals to detect exceptions,
572 we forward to it some signals */
573 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
574 && info->si_code > 0) {
575 if (cpu_signal_handler(host_signum, info, puc))
576 return;
579 /* get target signal number */
580 sig = host_to_target_signal(host_signum);
581 if (sig < 1 || sig > TARGET_NSIG)
582 return;
583 trace_user_host_signal(env, host_signum, sig);
584 host_to_target_siginfo_noswap(&tinfo, info);
585 if (queue_signal(env, sig, &tinfo) == 1) {
586 /* interrupt the virtual CPU as soon as possible */
587 cpu_exit(thread_cpu);
591 /* do_sigaltstack() returns target values and errnos. */
592 /* compare linux/kernel/signal.c:do_sigaltstack() */
593 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
595 int ret;
596 struct target_sigaltstack oss;
598 /* XXX: test errors */
599 if(uoss_addr)
601 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
602 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
603 __put_user(sas_ss_flags(sp), &oss.ss_flags);
606 if(uss_addr)
608 struct target_sigaltstack *uss;
609 struct target_sigaltstack ss;
610 size_t minstacksize = TARGET_MINSIGSTKSZ;
612 #if defined(TARGET_PPC64)
613 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
614 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
615 if (get_ppc64_abi(image) > 1) {
616 minstacksize = 4096;
618 #endif
620 ret = -TARGET_EFAULT;
621 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
622 goto out;
624 __get_user(ss.ss_sp, &uss->ss_sp);
625 __get_user(ss.ss_size, &uss->ss_size);
626 __get_user(ss.ss_flags, &uss->ss_flags);
627 unlock_user_struct(uss, uss_addr, 0);
629 ret = -TARGET_EPERM;
630 if (on_sig_stack(sp))
631 goto out;
633 ret = -TARGET_EINVAL;
634 if (ss.ss_flags != TARGET_SS_DISABLE
635 && ss.ss_flags != TARGET_SS_ONSTACK
636 && ss.ss_flags != 0)
637 goto out;
639 if (ss.ss_flags == TARGET_SS_DISABLE) {
640 ss.ss_size = 0;
641 ss.ss_sp = 0;
642 } else {
643 ret = -TARGET_ENOMEM;
644 if (ss.ss_size < minstacksize) {
645 goto out;
649 target_sigaltstack_used.ss_sp = ss.ss_sp;
650 target_sigaltstack_used.ss_size = ss.ss_size;
653 if (uoss_addr) {
654 ret = -TARGET_EFAULT;
655 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
656 goto out;
659 ret = 0;
660 out:
661 return ret;
664 /* do_sigaction() return host values and errnos */
665 int do_sigaction(int sig, const struct target_sigaction *act,
666 struct target_sigaction *oact)
668 struct target_sigaction *k;
669 struct sigaction act1;
670 int host_sig;
671 int ret = 0;
673 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
674 return -EINVAL;
675 k = &sigact_table[sig - 1];
676 if (oact) {
677 __put_user(k->_sa_handler, &oact->_sa_handler);
678 __put_user(k->sa_flags, &oact->sa_flags);
679 #if !defined(TARGET_MIPS)
680 __put_user(k->sa_restorer, &oact->sa_restorer);
681 #endif
682 /* Not swapped. */
683 oact->sa_mask = k->sa_mask;
685 if (act) {
686 /* FIXME: This is not threadsafe. */
687 __get_user(k->_sa_handler, &act->_sa_handler);
688 __get_user(k->sa_flags, &act->sa_flags);
689 #if !defined(TARGET_MIPS)
690 __get_user(k->sa_restorer, &act->sa_restorer);
691 #endif
692 /* To be swapped in target_to_host_sigset. */
693 k->sa_mask = act->sa_mask;
695 /* we update the host linux signal state */
696 host_sig = target_to_host_signal(sig);
697 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
698 sigfillset(&act1.sa_mask);
699 act1.sa_flags = SA_SIGINFO;
700 if (k->sa_flags & TARGET_SA_RESTART)
701 act1.sa_flags |= SA_RESTART;
702 /* NOTE: it is important to update the host kernel signal
703 ignore state to avoid getting unexpected interrupted
704 syscalls */
705 if (k->_sa_handler == TARGET_SIG_IGN) {
706 act1.sa_sigaction = (void *)SIG_IGN;
707 } else if (k->_sa_handler == TARGET_SIG_DFL) {
708 if (fatal_signal (sig))
709 act1.sa_sigaction = host_signal_handler;
710 else
711 act1.sa_sigaction = (void *)SIG_DFL;
712 } else {
713 act1.sa_sigaction = host_signal_handler;
715 ret = sigaction(host_sig, &act1, NULL);
718 return ret;
721 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
723 /* from the Linux kernel */
725 struct target_fpreg {
726 uint16_t significand[4];
727 uint16_t exponent;
730 struct target_fpxreg {
731 uint16_t significand[4];
732 uint16_t exponent;
733 uint16_t padding[3];
736 struct target_xmmreg {
737 abi_ulong element[4];
740 struct target_fpstate {
741 /* Regular FPU environment */
742 abi_ulong cw;
743 abi_ulong sw;
744 abi_ulong tag;
745 abi_ulong ipoff;
746 abi_ulong cssel;
747 abi_ulong dataoff;
748 abi_ulong datasel;
749 struct target_fpreg _st[8];
750 uint16_t status;
751 uint16_t magic; /* 0xffff = regular FPU data only */
753 /* FXSR FPU environment */
754 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
755 abi_ulong mxcsr;
756 abi_ulong reserved;
757 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
758 struct target_xmmreg _xmm[8];
759 abi_ulong padding[56];
762 #define X86_FXSR_MAGIC 0x0000
764 struct target_sigcontext {
765 uint16_t gs, __gsh;
766 uint16_t fs, __fsh;
767 uint16_t es, __esh;
768 uint16_t ds, __dsh;
769 abi_ulong edi;
770 abi_ulong esi;
771 abi_ulong ebp;
772 abi_ulong esp;
773 abi_ulong ebx;
774 abi_ulong edx;
775 abi_ulong ecx;
776 abi_ulong eax;
777 abi_ulong trapno;
778 abi_ulong err;
779 abi_ulong eip;
780 uint16_t cs, __csh;
781 abi_ulong eflags;
782 abi_ulong esp_at_signal;
783 uint16_t ss, __ssh;
784 abi_ulong fpstate; /* pointer */
785 abi_ulong oldmask;
786 abi_ulong cr2;
789 struct target_ucontext {
790 abi_ulong tuc_flags;
791 abi_ulong tuc_link;
792 target_stack_t tuc_stack;
793 struct target_sigcontext tuc_mcontext;
794 target_sigset_t tuc_sigmask; /* mask last for extensibility */
797 struct sigframe
799 abi_ulong pretcode;
800 int sig;
801 struct target_sigcontext sc;
802 struct target_fpstate fpstate;
803 abi_ulong extramask[TARGET_NSIG_WORDS-1];
804 char retcode[8];
807 struct rt_sigframe
809 abi_ulong pretcode;
810 int sig;
811 abi_ulong pinfo;
812 abi_ulong puc;
813 struct target_siginfo info;
814 struct target_ucontext uc;
815 struct target_fpstate fpstate;
816 char retcode[8];
820 * Set up a signal frame.
823 /* XXX: save x87 state */
824 static void setup_sigcontext(struct target_sigcontext *sc,
825 struct target_fpstate *fpstate, CPUX86State *env, abi_ulong mask,
826 abi_ulong fpstate_addr)
828 CPUState *cs = CPU(x86_env_get_cpu(env));
829 uint16_t magic;
831 /* already locked in setup_frame() */
832 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
833 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
834 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
835 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
836 __put_user(env->regs[R_EDI], &sc->edi);
837 __put_user(env->regs[R_ESI], &sc->esi);
838 __put_user(env->regs[R_EBP], &sc->ebp);
839 __put_user(env->regs[R_ESP], &sc->esp);
840 __put_user(env->regs[R_EBX], &sc->ebx);
841 __put_user(env->regs[R_EDX], &sc->edx);
842 __put_user(env->regs[R_ECX], &sc->ecx);
843 __put_user(env->regs[R_EAX], &sc->eax);
844 __put_user(cs->exception_index, &sc->trapno);
845 __put_user(env->error_code, &sc->err);
846 __put_user(env->eip, &sc->eip);
847 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
848 __put_user(env->eflags, &sc->eflags);
849 __put_user(env->regs[R_ESP], &sc->esp_at_signal);
850 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
852 cpu_x86_fsave(env, fpstate_addr, 1);
853 fpstate->status = fpstate->sw;
854 magic = 0xffff;
855 __put_user(magic, &fpstate->magic);
856 __put_user(fpstate_addr, &sc->fpstate);
858 /* non-iBCS2 extensions.. */
859 __put_user(mask, &sc->oldmask);
860 __put_user(env->cr[2], &sc->cr2);
864 * Determine which stack to use..
867 static inline abi_ulong
868 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
870 unsigned long esp;
872 /* Default to using normal stack */
873 esp = env->regs[R_ESP];
874 /* This is the X/Open sanctioned signal stack switching. */
875 if (ka->sa_flags & TARGET_SA_ONSTACK) {
876 if (sas_ss_flags(esp) == 0)
877 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
880 /* This is the legacy signal stack switching. */
881 else
882 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
883 !(ka->sa_flags & TARGET_SA_RESTORER) &&
884 ka->sa_restorer) {
885 esp = (unsigned long) ka->sa_restorer;
887 return (esp - frame_size) & -8ul;
890 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
891 static void setup_frame(int sig, struct target_sigaction *ka,
892 target_sigset_t *set, CPUX86State *env)
894 abi_ulong frame_addr;
895 struct sigframe *frame;
896 int i;
898 frame_addr = get_sigframe(ka, env, sizeof(*frame));
899 trace_user_setup_frame(env, frame_addr);
901 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
902 goto give_sigsegv;
904 __put_user(sig, &frame->sig);
906 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
907 frame_addr + offsetof(struct sigframe, fpstate));
909 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
910 __put_user(set->sig[i], &frame->extramask[i - 1]);
913 /* Set up to return from userspace. If provided, use a stub
914 already in userspace. */
915 if (ka->sa_flags & TARGET_SA_RESTORER) {
916 __put_user(ka->sa_restorer, &frame->pretcode);
917 } else {
918 uint16_t val16;
919 abi_ulong retcode_addr;
920 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
921 __put_user(retcode_addr, &frame->pretcode);
922 /* This is popl %eax ; movl $,%eax ; int $0x80 */
923 val16 = 0xb858;
924 __put_user(val16, (uint16_t *)(frame->retcode+0));
925 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
926 val16 = 0x80cd;
927 __put_user(val16, (uint16_t *)(frame->retcode+6));
931 /* Set up registers for signal handler */
932 env->regs[R_ESP] = frame_addr;
933 env->eip = ka->_sa_handler;
935 cpu_x86_load_seg(env, R_DS, __USER_DS);
936 cpu_x86_load_seg(env, R_ES, __USER_DS);
937 cpu_x86_load_seg(env, R_SS, __USER_DS);
938 cpu_x86_load_seg(env, R_CS, __USER_CS);
939 env->eflags &= ~TF_MASK;
941 unlock_user_struct(frame, frame_addr, 1);
943 return;
945 give_sigsegv:
946 if (sig == TARGET_SIGSEGV)
947 ka->_sa_handler = TARGET_SIG_DFL;
948 force_sig(TARGET_SIGSEGV /* , current */);
951 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
952 static void setup_rt_frame(int sig, struct target_sigaction *ka,
953 target_siginfo_t *info,
954 target_sigset_t *set, CPUX86State *env)
956 abi_ulong frame_addr, addr;
957 struct rt_sigframe *frame;
958 int i;
960 frame_addr = get_sigframe(ka, env, sizeof(*frame));
961 trace_user_setup_rt_frame(env, frame_addr);
963 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
964 goto give_sigsegv;
966 __put_user(sig, &frame->sig);
967 addr = frame_addr + offsetof(struct rt_sigframe, info);
968 __put_user(addr, &frame->pinfo);
969 addr = frame_addr + offsetof(struct rt_sigframe, uc);
970 __put_user(addr, &frame->puc);
971 tswap_siginfo(&frame->info, info);
973 /* Create the ucontext. */
974 __put_user(0, &frame->uc.tuc_flags);
975 __put_user(0, &frame->uc.tuc_link);
976 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
977 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
978 &frame->uc.tuc_stack.ss_flags);
979 __put_user(target_sigaltstack_used.ss_size,
980 &frame->uc.tuc_stack.ss_size);
981 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env,
982 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate));
984 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
985 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
988 /* Set up to return from userspace. If provided, use a stub
989 already in userspace. */
990 if (ka->sa_flags & TARGET_SA_RESTORER) {
991 __put_user(ka->sa_restorer, &frame->pretcode);
992 } else {
993 uint16_t val16;
994 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
995 __put_user(addr, &frame->pretcode);
996 /* This is movl $,%eax ; int $0x80 */
997 __put_user(0xb8, (char *)(frame->retcode+0));
998 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
999 val16 = 0x80cd;
1000 __put_user(val16, (uint16_t *)(frame->retcode+5));
1003 /* Set up registers for signal handler */
1004 env->regs[R_ESP] = frame_addr;
1005 env->eip = ka->_sa_handler;
1007 cpu_x86_load_seg(env, R_DS, __USER_DS);
1008 cpu_x86_load_seg(env, R_ES, __USER_DS);
1009 cpu_x86_load_seg(env, R_SS, __USER_DS);
1010 cpu_x86_load_seg(env, R_CS, __USER_CS);
1011 env->eflags &= ~TF_MASK;
1013 unlock_user_struct(frame, frame_addr, 1);
1015 return;
1017 give_sigsegv:
1018 if (sig == TARGET_SIGSEGV)
1019 ka->_sa_handler = TARGET_SIG_DFL;
1020 force_sig(TARGET_SIGSEGV /* , current */);
1023 static int
1024 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
1026 unsigned int err = 0;
1027 abi_ulong fpstate_addr;
1028 unsigned int tmpflags;
1030 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1031 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1032 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1033 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1035 env->regs[R_EDI] = tswapl(sc->edi);
1036 env->regs[R_ESI] = tswapl(sc->esi);
1037 env->regs[R_EBP] = tswapl(sc->ebp);
1038 env->regs[R_ESP] = tswapl(sc->esp);
1039 env->regs[R_EBX] = tswapl(sc->ebx);
1040 env->regs[R_EDX] = tswapl(sc->edx);
1041 env->regs[R_ECX] = tswapl(sc->ecx);
1042 env->eip = tswapl(sc->eip);
1044 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1045 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1047 tmpflags = tswapl(sc->eflags);
1048 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1049 // regs->orig_eax = -1; /* disable syscall checks */
1051 fpstate_addr = tswapl(sc->fpstate);
1052 if (fpstate_addr != 0) {
1053 if (!access_ok(VERIFY_READ, fpstate_addr,
1054 sizeof(struct target_fpstate)))
1055 goto badframe;
1056 cpu_x86_frstor(env, fpstate_addr, 1);
1059 *peax = tswapl(sc->eax);
1060 return err;
1061 badframe:
1062 return 1;
1065 long do_sigreturn(CPUX86State *env)
1067 struct sigframe *frame;
1068 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1069 target_sigset_t target_set;
1070 sigset_t set;
1071 int eax, i;
1073 trace_user_do_sigreturn(env, frame_addr);
1074 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1075 goto badframe;
1076 /* set blocked signals */
1077 __get_user(target_set.sig[0], &frame->sc.oldmask);
1078 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1079 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
1082 target_to_host_sigset_internal(&set, &target_set);
1083 do_sigprocmask(SIG_SETMASK, &set, NULL);
1085 /* restore registers */
1086 if (restore_sigcontext(env, &frame->sc, &eax))
1087 goto badframe;
1088 unlock_user_struct(frame, frame_addr, 0);
1089 return eax;
1091 badframe:
1092 unlock_user_struct(frame, frame_addr, 0);
1093 force_sig(TARGET_SIGSEGV);
1094 return 0;
1097 long do_rt_sigreturn(CPUX86State *env)
1099 abi_ulong frame_addr;
1100 struct rt_sigframe *frame;
1101 sigset_t set;
1102 int eax;
1104 frame_addr = env->regs[R_ESP] - 4;
1105 trace_user_do_rt_sigreturn(env, frame_addr);
1106 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1107 goto badframe;
1108 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1109 do_sigprocmask(SIG_SETMASK, &set, NULL);
1111 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1112 goto badframe;
1114 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1115 get_sp_from_cpustate(env)) == -EFAULT)
1116 goto badframe;
1118 unlock_user_struct(frame, frame_addr, 0);
1119 return eax;
1121 badframe:
1122 unlock_user_struct(frame, frame_addr, 0);
1123 force_sig(TARGET_SIGSEGV);
1124 return 0;
1127 #elif defined(TARGET_AARCH64)
1129 struct target_sigcontext {
1130 uint64_t fault_address;
1131 /* AArch64 registers */
1132 uint64_t regs[31];
1133 uint64_t sp;
1134 uint64_t pc;
1135 uint64_t pstate;
1136 /* 4K reserved for FP/SIMD state and future expansion */
1137 char __reserved[4096] __attribute__((__aligned__(16)));
1140 struct target_ucontext {
1141 abi_ulong tuc_flags;
1142 abi_ulong tuc_link;
1143 target_stack_t tuc_stack;
1144 target_sigset_t tuc_sigmask;
1145 /* glibc uses a 1024-bit sigset_t */
1146 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1147 /* last for future expansion */
1148 struct target_sigcontext tuc_mcontext;
1152 * Header to be used at the beginning of structures extending the user
1153 * context. Such structures must be placed after the rt_sigframe on the stack
1154 * and be 16-byte aligned. The last structure must be a dummy one with the
1155 * magic and size set to 0.
1157 struct target_aarch64_ctx {
1158 uint32_t magic;
1159 uint32_t size;
1162 #define TARGET_FPSIMD_MAGIC 0x46508001
1164 struct target_fpsimd_context {
1165 struct target_aarch64_ctx head;
1166 uint32_t fpsr;
1167 uint32_t fpcr;
1168 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1172 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1173 * user space as it will change with the addition of new context. User space
1174 * should check the magic/size information.
1176 struct target_aux_context {
1177 struct target_fpsimd_context fpsimd;
1178 /* additional context to be added before "end" */
1179 struct target_aarch64_ctx end;
1182 struct target_rt_sigframe {
1183 struct target_siginfo info;
1184 struct target_ucontext uc;
1185 uint64_t fp;
1186 uint64_t lr;
1187 uint32_t tramp[2];
1190 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1191 CPUARMState *env, target_sigset_t *set)
1193 int i;
1194 struct target_aux_context *aux =
1195 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1197 /* set up the stack frame for unwinding */
1198 __put_user(env->xregs[29], &sf->fp);
1199 __put_user(env->xregs[30], &sf->lr);
1201 for (i = 0; i < 31; i++) {
1202 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1204 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1205 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
1206 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
1208 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
1210 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1211 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1214 for (i = 0; i < 32; i++) {
1215 #ifdef TARGET_WORDS_BIGENDIAN
1216 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1217 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1218 #else
1219 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1220 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1221 #endif
1223 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1224 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1225 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1226 __put_user(sizeof(struct target_fpsimd_context),
1227 &aux->fpsimd.head.size);
1229 /* set the "end" magic */
1230 __put_user(0, &aux->end.magic);
1231 __put_user(0, &aux->end.size);
1233 return 0;
1236 static int target_restore_sigframe(CPUARMState *env,
1237 struct target_rt_sigframe *sf)
1239 sigset_t set;
1240 int i;
1241 struct target_aux_context *aux =
1242 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1243 uint32_t magic, size, fpsr, fpcr;
1244 uint64_t pstate;
1246 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1247 do_sigprocmask(SIG_SETMASK, &set, NULL);
1249 for (i = 0; i < 31; i++) {
1250 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1253 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1254 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1255 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1256 pstate_write(env, pstate);
1258 __get_user(magic, &aux->fpsimd.head.magic);
1259 __get_user(size, &aux->fpsimd.head.size);
1261 if (magic != TARGET_FPSIMD_MAGIC
1262 || size != sizeof(struct target_fpsimd_context)) {
1263 return 1;
1266 for (i = 0; i < 32; i++) {
1267 #ifdef TARGET_WORDS_BIGENDIAN
1268 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1269 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1270 #else
1271 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1272 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1273 #endif
1275 __get_user(fpsr, &aux->fpsimd.fpsr);
1276 vfp_set_fpsr(env, fpsr);
1277 __get_user(fpcr, &aux->fpsimd.fpcr);
1278 vfp_set_fpcr(env, fpcr);
1280 return 0;
1283 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1285 abi_ulong sp;
1287 sp = env->xregs[31];
1290 * This is the X/Open sanctioned signal stack switching.
1292 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1293 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1296 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1298 return sp;
1301 static void target_setup_frame(int usig, struct target_sigaction *ka,
1302 target_siginfo_t *info, target_sigset_t *set,
1303 CPUARMState *env)
1305 struct target_rt_sigframe *frame;
1306 abi_ulong frame_addr, return_addr;
1308 frame_addr = get_sigframe(ka, env);
1309 trace_user_setup_frame(env, frame_addr);
1310 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1311 goto give_sigsegv;
1314 __put_user(0, &frame->uc.tuc_flags);
1315 __put_user(0, &frame->uc.tuc_link);
1317 __put_user(target_sigaltstack_used.ss_sp,
1318 &frame->uc.tuc_stack.ss_sp);
1319 __put_user(sas_ss_flags(env->xregs[31]),
1320 &frame->uc.tuc_stack.ss_flags);
1321 __put_user(target_sigaltstack_used.ss_size,
1322 &frame->uc.tuc_stack.ss_size);
1323 target_setup_sigframe(frame, env, set);
1324 if (ka->sa_flags & TARGET_SA_RESTORER) {
1325 return_addr = ka->sa_restorer;
1326 } else {
1327 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1328 __put_user(0xd2801168, &frame->tramp[0]);
1329 __put_user(0xd4000001, &frame->tramp[1]);
1330 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1332 env->xregs[0] = usig;
1333 env->xregs[31] = frame_addr;
1334 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1335 env->pc = ka->_sa_handler;
1336 env->xregs[30] = return_addr;
1337 if (info) {
1338 tswap_siginfo(&frame->info, info);
1339 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1340 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1343 unlock_user_struct(frame, frame_addr, 1);
1344 return;
1346 give_sigsegv:
1347 unlock_user_struct(frame, frame_addr, 1);
1348 force_sig(TARGET_SIGSEGV);
1351 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1352 target_siginfo_t *info, target_sigset_t *set,
1353 CPUARMState *env)
1355 target_setup_frame(sig, ka, info, set, env);
1358 static void setup_frame(int sig, struct target_sigaction *ka,
1359 target_sigset_t *set, CPUARMState *env)
1361 target_setup_frame(sig, ka, 0, set, env);
1364 long do_rt_sigreturn(CPUARMState *env)
1366 struct target_rt_sigframe *frame = NULL;
1367 abi_ulong frame_addr = env->xregs[31];
1369 trace_user_do_rt_sigreturn(env, frame_addr);
1370 if (frame_addr & 15) {
1371 goto badframe;
1374 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1375 goto badframe;
1378 if (target_restore_sigframe(env, frame)) {
1379 goto badframe;
1382 if (do_sigaltstack(frame_addr +
1383 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1384 0, get_sp_from_cpustate(env)) == -EFAULT) {
1385 goto badframe;
1388 unlock_user_struct(frame, frame_addr, 0);
1389 return env->xregs[0];
1391 badframe:
1392 unlock_user_struct(frame, frame_addr, 0);
1393 force_sig(TARGET_SIGSEGV);
1394 return 0;
1397 long do_sigreturn(CPUARMState *env)
1399 return do_rt_sigreturn(env);
1402 #elif defined(TARGET_ARM)
1404 struct target_sigcontext {
1405 abi_ulong trap_no;
1406 abi_ulong error_code;
1407 abi_ulong oldmask;
1408 abi_ulong arm_r0;
1409 abi_ulong arm_r1;
1410 abi_ulong arm_r2;
1411 abi_ulong arm_r3;
1412 abi_ulong arm_r4;
1413 abi_ulong arm_r5;
1414 abi_ulong arm_r6;
1415 abi_ulong arm_r7;
1416 abi_ulong arm_r8;
1417 abi_ulong arm_r9;
1418 abi_ulong arm_r10;
1419 abi_ulong arm_fp;
1420 abi_ulong arm_ip;
1421 abi_ulong arm_sp;
1422 abi_ulong arm_lr;
1423 abi_ulong arm_pc;
1424 abi_ulong arm_cpsr;
1425 abi_ulong fault_address;
1428 struct target_ucontext_v1 {
1429 abi_ulong tuc_flags;
1430 abi_ulong tuc_link;
1431 target_stack_t tuc_stack;
1432 struct target_sigcontext tuc_mcontext;
1433 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1436 struct target_ucontext_v2 {
1437 abi_ulong tuc_flags;
1438 abi_ulong tuc_link;
1439 target_stack_t tuc_stack;
1440 struct target_sigcontext tuc_mcontext;
1441 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1442 char __unused[128 - sizeof(target_sigset_t)];
1443 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1446 struct target_user_vfp {
1447 uint64_t fpregs[32];
1448 abi_ulong fpscr;
1451 struct target_user_vfp_exc {
1452 abi_ulong fpexc;
1453 abi_ulong fpinst;
1454 abi_ulong fpinst2;
1457 struct target_vfp_sigframe {
1458 abi_ulong magic;
1459 abi_ulong size;
1460 struct target_user_vfp ufp;
1461 struct target_user_vfp_exc ufp_exc;
1462 } __attribute__((__aligned__(8)));
1464 struct target_iwmmxt_sigframe {
1465 abi_ulong magic;
1466 abi_ulong size;
1467 uint64_t regs[16];
1468 /* Note that not all the coprocessor control registers are stored here */
1469 uint32_t wcssf;
1470 uint32_t wcasf;
1471 uint32_t wcgr0;
1472 uint32_t wcgr1;
1473 uint32_t wcgr2;
1474 uint32_t wcgr3;
1475 } __attribute__((__aligned__(8)));
1477 #define TARGET_VFP_MAGIC 0x56465001
1478 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1480 struct sigframe_v1
1482 struct target_sigcontext sc;
1483 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1484 abi_ulong retcode;
1487 struct sigframe_v2
1489 struct target_ucontext_v2 uc;
1490 abi_ulong retcode;
1493 struct rt_sigframe_v1
1495 abi_ulong pinfo;
1496 abi_ulong puc;
1497 struct target_siginfo info;
1498 struct target_ucontext_v1 uc;
1499 abi_ulong retcode;
1502 struct rt_sigframe_v2
1504 struct target_siginfo info;
1505 struct target_ucontext_v2 uc;
1506 abi_ulong retcode;
1509 #define TARGET_CONFIG_CPU_32 1
1512 * For ARM syscalls, we encode the syscall number into the instruction.
1514 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1515 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1518 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1519 * need two 16-bit instructions.
1521 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1522 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1524 static const abi_ulong retcodes[4] = {
1525 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1526 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1530 static inline int valid_user_regs(CPUARMState *regs)
1532 return 1;
1535 static void
1536 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1537 CPUARMState *env, abi_ulong mask)
1539 __put_user(env->regs[0], &sc->arm_r0);
1540 __put_user(env->regs[1], &sc->arm_r1);
1541 __put_user(env->regs[2], &sc->arm_r2);
1542 __put_user(env->regs[3], &sc->arm_r3);
1543 __put_user(env->regs[4], &sc->arm_r4);
1544 __put_user(env->regs[5], &sc->arm_r5);
1545 __put_user(env->regs[6], &sc->arm_r6);
1546 __put_user(env->regs[7], &sc->arm_r7);
1547 __put_user(env->regs[8], &sc->arm_r8);
1548 __put_user(env->regs[9], &sc->arm_r9);
1549 __put_user(env->regs[10], &sc->arm_r10);
1550 __put_user(env->regs[11], &sc->arm_fp);
1551 __put_user(env->regs[12], &sc->arm_ip);
1552 __put_user(env->regs[13], &sc->arm_sp);
1553 __put_user(env->regs[14], &sc->arm_lr);
1554 __put_user(env->regs[15], &sc->arm_pc);
1555 #ifdef TARGET_CONFIG_CPU_32
1556 __put_user(cpsr_read(env), &sc->arm_cpsr);
1557 #endif
1559 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1560 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1561 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1562 __put_user(mask, &sc->oldmask);
1565 static inline abi_ulong
1566 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1568 unsigned long sp = regs->regs[13];
1571 * This is the X/Open sanctioned signal stack switching.
1573 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1574 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1576 * ATPCS B01 mandates 8-byte alignment
1578 return (sp - framesize) & ~7;
1581 static void
1582 setup_return(CPUARMState *env, struct target_sigaction *ka,
1583 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1585 abi_ulong handler = ka->_sa_handler;
1586 abi_ulong retcode;
1587 int thumb = handler & 1;
1588 uint32_t cpsr = cpsr_read(env);
1590 cpsr &= ~CPSR_IT;
1591 if (thumb) {
1592 cpsr |= CPSR_T;
1593 } else {
1594 cpsr &= ~CPSR_T;
1597 if (ka->sa_flags & TARGET_SA_RESTORER) {
1598 retcode = ka->sa_restorer;
1599 } else {
1600 unsigned int idx = thumb;
1602 if (ka->sa_flags & TARGET_SA_SIGINFO)
1603 idx += 2;
1605 __put_user(retcodes[idx], rc);
1607 retcode = rc_addr + thumb;
1610 env->regs[0] = usig;
1611 env->regs[13] = frame_addr;
1612 env->regs[14] = retcode;
1613 env->regs[15] = handler & (thumb ? ~1 : ~3);
1614 cpsr_write(env, cpsr, 0xffffffff);
1617 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1619 int i;
1620 struct target_vfp_sigframe *vfpframe;
1621 vfpframe = (struct target_vfp_sigframe *)regspace;
1622 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1623 __put_user(sizeof(*vfpframe), &vfpframe->size);
1624 for (i = 0; i < 32; i++) {
1625 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1627 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1628 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1629 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1630 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1631 return (abi_ulong*)(vfpframe+1);
1634 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1635 CPUARMState *env)
1637 int i;
1638 struct target_iwmmxt_sigframe *iwmmxtframe;
1639 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1640 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1641 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1642 for (i = 0; i < 16; i++) {
1643 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1645 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1646 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1647 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1648 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1649 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1650 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1651 return (abi_ulong*)(iwmmxtframe+1);
1654 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1655 target_sigset_t *set, CPUARMState *env)
1657 struct target_sigaltstack stack;
1658 int i;
1659 abi_ulong *regspace;
1661 /* Clear all the bits of the ucontext we don't use. */
1662 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1664 memset(&stack, 0, sizeof(stack));
1665 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1666 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1667 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1668 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1670 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1671 /* Save coprocessor signal frame. */
1672 regspace = uc->tuc_regspace;
1673 if (arm_feature(env, ARM_FEATURE_VFP)) {
1674 regspace = setup_sigframe_v2_vfp(regspace, env);
1676 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1677 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1680 /* Write terminating magic word */
1681 __put_user(0, regspace);
1683 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1684 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1688 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1689 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1690 target_sigset_t *set, CPUARMState *regs)
1692 struct sigframe_v1 *frame;
1693 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1694 int i;
1696 trace_user_setup_frame(regs, frame_addr);
1697 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1698 return;
1700 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1702 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1703 __put_user(set->sig[i], &frame->extramask[i - 1]);
1706 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1707 frame_addr + offsetof(struct sigframe_v1, retcode));
1709 unlock_user_struct(frame, frame_addr, 1);
1712 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1713 target_sigset_t *set, CPUARMState *regs)
1715 struct sigframe_v2 *frame;
1716 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1718 trace_user_setup_frame(regs, frame_addr);
1719 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1720 return;
1722 setup_sigframe_v2(&frame->uc, set, regs);
1724 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1725 frame_addr + offsetof(struct sigframe_v2, retcode));
1727 unlock_user_struct(frame, frame_addr, 1);
1730 static void setup_frame(int usig, struct target_sigaction *ka,
1731 target_sigset_t *set, CPUARMState *regs)
1733 if (get_osversion() >= 0x020612) {
1734 setup_frame_v2(usig, ka, set, regs);
1735 } else {
1736 setup_frame_v1(usig, ka, set, regs);
1740 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1741 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1742 target_siginfo_t *info,
1743 target_sigset_t *set, CPUARMState *env)
1745 struct rt_sigframe_v1 *frame;
1746 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1747 struct target_sigaltstack stack;
1748 int i;
1749 abi_ulong info_addr, uc_addr;
1751 trace_user_setup_rt_frame(env, frame_addr);
1752 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1753 return /* 1 */;
1755 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1756 __put_user(info_addr, &frame->pinfo);
1757 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1758 __put_user(uc_addr, &frame->puc);
1759 tswap_siginfo(&frame->info, info);
1761 /* Clear all the bits of the ucontext we don't use. */
1762 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1764 memset(&stack, 0, sizeof(stack));
1765 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1766 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1767 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1768 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1770 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1771 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1772 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
1775 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1776 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1778 env->regs[1] = info_addr;
1779 env->regs[2] = uc_addr;
1781 unlock_user_struct(frame, frame_addr, 1);
1784 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1785 target_siginfo_t *info,
1786 target_sigset_t *set, CPUARMState *env)
1788 struct rt_sigframe_v2 *frame;
1789 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1790 abi_ulong info_addr, uc_addr;
1792 trace_user_setup_rt_frame(env, frame_addr);
1793 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1794 return /* 1 */;
1796 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1797 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1798 tswap_siginfo(&frame->info, info);
1800 setup_sigframe_v2(&frame->uc, set, env);
1802 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1803 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1805 env->regs[1] = info_addr;
1806 env->regs[2] = uc_addr;
1808 unlock_user_struct(frame, frame_addr, 1);
1811 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1812 target_siginfo_t *info,
1813 target_sigset_t *set, CPUARMState *env)
1815 if (get_osversion() >= 0x020612) {
1816 setup_rt_frame_v2(usig, ka, info, set, env);
1817 } else {
1818 setup_rt_frame_v1(usig, ka, info, set, env);
1822 static int
1823 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1825 int err = 0;
1826 uint32_t cpsr;
1828 __get_user(env->regs[0], &sc->arm_r0);
1829 __get_user(env->regs[1], &sc->arm_r1);
1830 __get_user(env->regs[2], &sc->arm_r2);
1831 __get_user(env->regs[3], &sc->arm_r3);
1832 __get_user(env->regs[4], &sc->arm_r4);
1833 __get_user(env->regs[5], &sc->arm_r5);
1834 __get_user(env->regs[6], &sc->arm_r6);
1835 __get_user(env->regs[7], &sc->arm_r7);
1836 __get_user(env->regs[8], &sc->arm_r8);
1837 __get_user(env->regs[9], &sc->arm_r9);
1838 __get_user(env->regs[10], &sc->arm_r10);
1839 __get_user(env->regs[11], &sc->arm_fp);
1840 __get_user(env->regs[12], &sc->arm_ip);
1841 __get_user(env->regs[13], &sc->arm_sp);
1842 __get_user(env->regs[14], &sc->arm_lr);
1843 __get_user(env->regs[15], &sc->arm_pc);
1844 #ifdef TARGET_CONFIG_CPU_32
1845 __get_user(cpsr, &sc->arm_cpsr);
1846 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1847 #endif
1849 err |= !valid_user_regs(env);
1851 return err;
1854 static long do_sigreturn_v1(CPUARMState *env)
1856 abi_ulong frame_addr;
1857 struct sigframe_v1 *frame = NULL;
1858 target_sigset_t set;
1859 sigset_t host_set;
1860 int i;
1863 * Since we stacked the signal on a 64-bit boundary,
1864 * then 'sp' should be word aligned here. If it's
1865 * not, then the user is trying to mess with us.
1867 frame_addr = env->regs[13];
1868 trace_user_do_sigreturn(env, frame_addr);
1869 if (frame_addr & 7) {
1870 goto badframe;
1873 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1874 goto badframe;
1876 __get_user(set.sig[0], &frame->sc.oldmask);
1877 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1878 __get_user(set.sig[i], &frame->extramask[i - 1]);
1881 target_to_host_sigset_internal(&host_set, &set);
1882 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1884 if (restore_sigcontext(env, &frame->sc))
1885 goto badframe;
1887 #if 0
1888 /* Send SIGTRAP if we're single-stepping */
1889 if (ptrace_cancel_bpt(current))
1890 send_sig(SIGTRAP, current, 1);
1891 #endif
1892 unlock_user_struct(frame, frame_addr, 0);
1893 return env->regs[0];
1895 badframe:
1896 force_sig(TARGET_SIGSEGV /* , current */);
1897 return 0;
1900 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1902 int i;
1903 abi_ulong magic, sz;
1904 uint32_t fpscr, fpexc;
1905 struct target_vfp_sigframe *vfpframe;
1906 vfpframe = (struct target_vfp_sigframe *)regspace;
1908 __get_user(magic, &vfpframe->magic);
1909 __get_user(sz, &vfpframe->size);
1910 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1911 return 0;
1913 for (i = 0; i < 32; i++) {
1914 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1916 __get_user(fpscr, &vfpframe->ufp.fpscr);
1917 vfp_set_fpscr(env, fpscr);
1918 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1919 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1920 * and the exception flag is cleared
1922 fpexc |= (1 << 30);
1923 fpexc &= ~((1 << 31) | (1 << 28));
1924 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1925 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1926 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1927 return (abi_ulong*)(vfpframe + 1);
1930 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1931 abi_ulong *regspace)
1933 int i;
1934 abi_ulong magic, sz;
1935 struct target_iwmmxt_sigframe *iwmmxtframe;
1936 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1938 __get_user(magic, &iwmmxtframe->magic);
1939 __get_user(sz, &iwmmxtframe->size);
1940 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1941 return 0;
1943 for (i = 0; i < 16; i++) {
1944 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1946 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1947 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1948 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1949 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1950 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1951 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1952 return (abi_ulong*)(iwmmxtframe + 1);
1955 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1956 struct target_ucontext_v2 *uc)
1958 sigset_t host_set;
1959 abi_ulong *regspace;
1961 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1962 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1964 if (restore_sigcontext(env, &uc->tuc_mcontext))
1965 return 1;
1967 /* Restore coprocessor signal frame */
1968 regspace = uc->tuc_regspace;
1969 if (arm_feature(env, ARM_FEATURE_VFP)) {
1970 regspace = restore_sigframe_v2_vfp(env, regspace);
1971 if (!regspace) {
1972 return 1;
1975 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1976 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1977 if (!regspace) {
1978 return 1;
1982 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1983 return 1;
1985 #if 0
1986 /* Send SIGTRAP if we're single-stepping */
1987 if (ptrace_cancel_bpt(current))
1988 send_sig(SIGTRAP, current, 1);
1989 #endif
1991 return 0;
1994 static long do_sigreturn_v2(CPUARMState *env)
1996 abi_ulong frame_addr;
1997 struct sigframe_v2 *frame = NULL;
2000 * Since we stacked the signal on a 64-bit boundary,
2001 * then 'sp' should be word aligned here. If it's
2002 * not, then the user is trying to mess with us.
2004 frame_addr = env->regs[13];
2005 trace_user_do_sigreturn(env, frame_addr);
2006 if (frame_addr & 7) {
2007 goto badframe;
2010 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2011 goto badframe;
2013 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2014 goto badframe;
2016 unlock_user_struct(frame, frame_addr, 0);
2017 return env->regs[0];
2019 badframe:
2020 unlock_user_struct(frame, frame_addr, 0);
2021 force_sig(TARGET_SIGSEGV /* , current */);
2022 return 0;
2025 long do_sigreturn(CPUARMState *env)
2027 if (get_osversion() >= 0x020612) {
2028 return do_sigreturn_v2(env);
2029 } else {
2030 return do_sigreturn_v1(env);
2034 static long do_rt_sigreturn_v1(CPUARMState *env)
2036 abi_ulong frame_addr;
2037 struct rt_sigframe_v1 *frame = NULL;
2038 sigset_t host_set;
2041 * Since we stacked the signal on a 64-bit boundary,
2042 * then 'sp' should be word aligned here. If it's
2043 * not, then the user is trying to mess with us.
2045 frame_addr = env->regs[13];
2046 trace_user_do_rt_sigreturn(env, frame_addr);
2047 if (frame_addr & 7) {
2048 goto badframe;
2051 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2052 goto badframe;
2054 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2055 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2057 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
2058 goto badframe;
2060 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2061 goto badframe;
2063 #if 0
2064 /* Send SIGTRAP if we're single-stepping */
2065 if (ptrace_cancel_bpt(current))
2066 send_sig(SIGTRAP, current, 1);
2067 #endif
2068 unlock_user_struct(frame, frame_addr, 0);
2069 return env->regs[0];
2071 badframe:
2072 unlock_user_struct(frame, frame_addr, 0);
2073 force_sig(TARGET_SIGSEGV /* , current */);
2074 return 0;
2077 static long do_rt_sigreturn_v2(CPUARMState *env)
2079 abi_ulong frame_addr;
2080 struct rt_sigframe_v2 *frame = NULL;
2083 * Since we stacked the signal on a 64-bit boundary,
2084 * then 'sp' should be word aligned here. If it's
2085 * not, then the user is trying to mess with us.
2087 frame_addr = env->regs[13];
2088 trace_user_do_rt_sigreturn(env, frame_addr);
2089 if (frame_addr & 7) {
2090 goto badframe;
2093 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2094 goto badframe;
2096 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2097 goto badframe;
2099 unlock_user_struct(frame, frame_addr, 0);
2100 return env->regs[0];
2102 badframe:
2103 unlock_user_struct(frame, frame_addr, 0);
2104 force_sig(TARGET_SIGSEGV /* , current */);
2105 return 0;
2108 long do_rt_sigreturn(CPUARMState *env)
2110 if (get_osversion() >= 0x020612) {
2111 return do_rt_sigreturn_v2(env);
2112 } else {
2113 return do_rt_sigreturn_v1(env);
2117 #elif defined(TARGET_SPARC)
2119 #define __SUNOS_MAXWIN 31
2121 /* This is what SunOS does, so shall I. */
2122 struct target_sigcontext {
2123 abi_ulong sigc_onstack; /* state to restore */
2125 abi_ulong sigc_mask; /* sigmask to restore */
2126 abi_ulong sigc_sp; /* stack pointer */
2127 abi_ulong sigc_pc; /* program counter */
2128 abi_ulong sigc_npc; /* next program counter */
2129 abi_ulong sigc_psr; /* for condition codes etc */
2130 abi_ulong sigc_g1; /* User uses these two registers */
2131 abi_ulong sigc_o0; /* within the trampoline code. */
2133 /* Now comes information regarding the users window set
2134 * at the time of the signal.
2136 abi_ulong sigc_oswins; /* outstanding windows */
2138 /* stack ptrs for each regwin buf */
2139 char *sigc_spbuf[__SUNOS_MAXWIN];
2141 /* Windows to restore after signal */
2142 struct {
2143 abi_ulong locals[8];
2144 abi_ulong ins[8];
2145 } sigc_wbuf[__SUNOS_MAXWIN];
2147 /* A Sparc stack frame */
2148 struct sparc_stackf {
2149 abi_ulong locals[8];
2150 abi_ulong ins[8];
2151 /* It's simpler to treat fp and callers_pc as elements of ins[]
2152 * since we never need to access them ourselves.
2154 char *structptr;
2155 abi_ulong xargs[6];
2156 abi_ulong xxargs[1];
2159 typedef struct {
2160 struct {
2161 abi_ulong psr;
2162 abi_ulong pc;
2163 abi_ulong npc;
2164 abi_ulong y;
2165 abi_ulong u_regs[16]; /* globals and ins */
2166 } si_regs;
2167 int si_mask;
2168 } __siginfo_t;
2170 typedef struct {
2171 abi_ulong si_float_regs[32];
2172 unsigned long si_fsr;
2173 unsigned long si_fpqdepth;
2174 struct {
2175 unsigned long *insn_addr;
2176 unsigned long insn;
2177 } si_fpqueue [16];
2178 } qemu_siginfo_fpu_t;
2181 struct target_signal_frame {
2182 struct sparc_stackf ss;
2183 __siginfo_t info;
2184 abi_ulong fpu_save;
2185 abi_ulong insns[2] __attribute__ ((aligned (8)));
2186 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2187 abi_ulong extra_size; /* Should be 0 */
2188 qemu_siginfo_fpu_t fpu_state;
2190 struct target_rt_signal_frame {
2191 struct sparc_stackf ss;
2192 siginfo_t info;
2193 abi_ulong regs[20];
2194 sigset_t mask;
2195 abi_ulong fpu_save;
2196 unsigned int insns[2];
2197 stack_t stack;
2198 unsigned int extra_size; /* Should be 0 */
2199 qemu_siginfo_fpu_t fpu_state;
2202 #define UREG_O0 16
2203 #define UREG_O6 22
2204 #define UREG_I0 0
2205 #define UREG_I1 1
2206 #define UREG_I2 2
2207 #define UREG_I3 3
2208 #define UREG_I4 4
2209 #define UREG_I5 5
2210 #define UREG_I6 6
2211 #define UREG_I7 7
2212 #define UREG_L0 8
2213 #define UREG_FP UREG_I6
2214 #define UREG_SP UREG_O6
2216 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2217 CPUSPARCState *env,
2218 unsigned long framesize)
2220 abi_ulong sp;
2222 sp = env->regwptr[UREG_FP];
2224 /* This is the X/Open sanctioned signal stack switching. */
2225 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2226 if (!on_sig_stack(sp)
2227 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2228 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2230 return sp - framesize;
2233 static int
2234 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2236 int err = 0, i;
2238 __put_user(env->psr, &si->si_regs.psr);
2239 __put_user(env->pc, &si->si_regs.pc);
2240 __put_user(env->npc, &si->si_regs.npc);
2241 __put_user(env->y, &si->si_regs.y);
2242 for (i=0; i < 8; i++) {
2243 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2245 for (i=0; i < 8; i++) {
2246 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2248 __put_user(mask, &si->si_mask);
2249 return err;
2252 #if 0
2253 static int
2254 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2255 CPUSPARCState *env, unsigned long mask)
2257 int err = 0;
2259 __put_user(mask, &sc->sigc_mask);
2260 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2261 __put_user(env->pc, &sc->sigc_pc);
2262 __put_user(env->npc, &sc->sigc_npc);
2263 __put_user(env->psr, &sc->sigc_psr);
2264 __put_user(env->gregs[1], &sc->sigc_g1);
2265 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2267 return err;
2269 #endif
2270 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2272 static void setup_frame(int sig, struct target_sigaction *ka,
2273 target_sigset_t *set, CPUSPARCState *env)
2275 abi_ulong sf_addr;
2276 struct target_signal_frame *sf;
2277 int sigframe_size, err, i;
2279 /* 1. Make sure everything is clean */
2280 //synchronize_user_stack();
2282 sigframe_size = NF_ALIGNEDSZ;
2283 sf_addr = get_sigframe(ka, env, sigframe_size);
2284 trace_user_setup_frame(env, sf_addr);
2286 sf = lock_user(VERIFY_WRITE, sf_addr,
2287 sizeof(struct target_signal_frame), 0);
2288 if (!sf)
2289 goto sigsegv;
2291 #if 0
2292 if (invalid_frame_pointer(sf, sigframe_size))
2293 goto sigill_and_return;
2294 #endif
2295 /* 2. Save the current process state */
2296 err = setup___siginfo(&sf->info, env, set->sig[0]);
2297 __put_user(0, &sf->extra_size);
2299 //save_fpu_state(regs, &sf->fpu_state);
2300 //__put_user(&sf->fpu_state, &sf->fpu_save);
2302 __put_user(set->sig[0], &sf->info.si_mask);
2303 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2304 __put_user(set->sig[i + 1], &sf->extramask[i]);
2307 for (i = 0; i < 8; i++) {
2308 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2310 for (i = 0; i < 8; i++) {
2311 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2313 if (err)
2314 goto sigsegv;
2316 /* 3. signal handler back-trampoline and parameters */
2317 env->regwptr[UREG_FP] = sf_addr;
2318 env->regwptr[UREG_I0] = sig;
2319 env->regwptr[UREG_I1] = sf_addr +
2320 offsetof(struct target_signal_frame, info);
2321 env->regwptr[UREG_I2] = sf_addr +
2322 offsetof(struct target_signal_frame, info);
2324 /* 4. signal handler */
2325 env->pc = ka->_sa_handler;
2326 env->npc = (env->pc + 4);
2327 /* 5. return to kernel instructions */
2328 if (ka->sa_restorer)
2329 env->regwptr[UREG_I7] = ka->sa_restorer;
2330 else {
2331 uint32_t val32;
2333 env->regwptr[UREG_I7] = sf_addr +
2334 offsetof(struct target_signal_frame, insns) - 2 * 4;
2336 /* mov __NR_sigreturn, %g1 */
2337 val32 = 0x821020d8;
2338 __put_user(val32, &sf->insns[0]);
2340 /* t 0x10 */
2341 val32 = 0x91d02010;
2342 __put_user(val32, &sf->insns[1]);
2343 if (err)
2344 goto sigsegv;
2346 /* Flush instruction space. */
2347 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2348 // tb_flush(CPU(sparc_env_get_cpu(env)));
2350 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2351 return;
2352 #if 0
2353 sigill_and_return:
2354 force_sig(TARGET_SIGILL);
2355 #endif
2356 sigsegv:
2357 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2358 force_sig(TARGET_SIGSEGV);
2361 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2362 target_siginfo_t *info,
2363 target_sigset_t *set, CPUSPARCState *env)
2365 fprintf(stderr, "setup_rt_frame: not implemented\n");
2368 long do_sigreturn(CPUSPARCState *env)
2370 abi_ulong sf_addr;
2371 struct target_signal_frame *sf;
2372 uint32_t up_psr, pc, npc;
2373 target_sigset_t set;
2374 sigset_t host_set;
2375 int err=0, i;
2377 sf_addr = env->regwptr[UREG_FP];
2378 trace_user_do_sigreturn(env, sf_addr);
2379 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2380 goto segv_and_exit;
2382 /* 1. Make sure we are not getting garbage from the user */
2384 if (sf_addr & 3)
2385 goto segv_and_exit;
2387 __get_user(pc, &sf->info.si_regs.pc);
2388 __get_user(npc, &sf->info.si_regs.npc);
2390 if ((pc | npc) & 3)
2391 goto segv_and_exit;
2393 /* 2. Restore the state */
2394 __get_user(up_psr, &sf->info.si_regs.psr);
2396 /* User can only change condition codes and FPU enabling in %psr. */
2397 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2398 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2400 env->pc = pc;
2401 env->npc = npc;
2402 __get_user(env->y, &sf->info.si_regs.y);
2403 for (i=0; i < 8; i++) {
2404 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2406 for (i=0; i < 8; i++) {
2407 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2410 /* FIXME: implement FPU save/restore:
2411 * __get_user(fpu_save, &sf->fpu_save);
2412 * if (fpu_save)
2413 * err |= restore_fpu_state(env, fpu_save);
2416 /* This is pretty much atomic, no amount locking would prevent
2417 * the races which exist anyways.
2419 __get_user(set.sig[0], &sf->info.si_mask);
2420 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2421 __get_user(set.sig[i], &sf->extramask[i - 1]);
2424 target_to_host_sigset_internal(&host_set, &set);
2425 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2427 if (err)
2428 goto segv_and_exit;
2429 unlock_user_struct(sf, sf_addr, 0);
2430 return env->regwptr[0];
2432 segv_and_exit:
2433 unlock_user_struct(sf, sf_addr, 0);
2434 force_sig(TARGET_SIGSEGV);
2437 long do_rt_sigreturn(CPUSPARCState *env)
2439 trace_user_do_rt_sigreturn(env, 0);
2440 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2441 return -TARGET_ENOSYS;
2444 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2445 #define MC_TSTATE 0
2446 #define MC_PC 1
2447 #define MC_NPC 2
2448 #define MC_Y 3
2449 #define MC_G1 4
2450 #define MC_G2 5
2451 #define MC_G3 6
2452 #define MC_G4 7
2453 #define MC_G5 8
2454 #define MC_G6 9
2455 #define MC_G7 10
2456 #define MC_O0 11
2457 #define MC_O1 12
2458 #define MC_O2 13
2459 #define MC_O3 14
2460 #define MC_O4 15
2461 #define MC_O5 16
2462 #define MC_O6 17
2463 #define MC_O7 18
2464 #define MC_NGREG 19
2466 typedef abi_ulong target_mc_greg_t;
2467 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2469 struct target_mc_fq {
2470 abi_ulong *mcfq_addr;
2471 uint32_t mcfq_insn;
2474 struct target_mc_fpu {
2475 union {
2476 uint32_t sregs[32];
2477 uint64_t dregs[32];
2478 //uint128_t qregs[16];
2479 } mcfpu_fregs;
2480 abi_ulong mcfpu_fsr;
2481 abi_ulong mcfpu_fprs;
2482 abi_ulong mcfpu_gsr;
2483 struct target_mc_fq *mcfpu_fq;
2484 unsigned char mcfpu_qcnt;
2485 unsigned char mcfpu_qentsz;
2486 unsigned char mcfpu_enab;
2488 typedef struct target_mc_fpu target_mc_fpu_t;
2490 typedef struct {
2491 target_mc_gregset_t mc_gregs;
2492 target_mc_greg_t mc_fp;
2493 target_mc_greg_t mc_i7;
2494 target_mc_fpu_t mc_fpregs;
2495 } target_mcontext_t;
2497 struct target_ucontext {
2498 struct target_ucontext *tuc_link;
2499 abi_ulong tuc_flags;
2500 target_sigset_t tuc_sigmask;
2501 target_mcontext_t tuc_mcontext;
2504 /* A V9 register window */
2505 struct target_reg_window {
2506 abi_ulong locals[8];
2507 abi_ulong ins[8];
2510 #define TARGET_STACK_BIAS 2047
2512 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2513 void sparc64_set_context(CPUSPARCState *env)
2515 abi_ulong ucp_addr;
2516 struct target_ucontext *ucp;
2517 target_mc_gregset_t *grp;
2518 abi_ulong pc, npc, tstate;
2519 abi_ulong fp, i7, w_addr;
2520 unsigned int i;
2522 ucp_addr = env->regwptr[UREG_I0];
2523 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2524 goto do_sigsegv;
2525 grp = &ucp->tuc_mcontext.mc_gregs;
2526 __get_user(pc, &((*grp)[MC_PC]));
2527 __get_user(npc, &((*grp)[MC_NPC]));
2528 if ((pc | npc) & 3)
2529 goto do_sigsegv;
2530 if (env->regwptr[UREG_I1]) {
2531 target_sigset_t target_set;
2532 sigset_t set;
2534 if (TARGET_NSIG_WORDS == 1) {
2535 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
2536 } else {
2537 abi_ulong *src, *dst;
2538 src = ucp->tuc_sigmask.sig;
2539 dst = target_set.sig;
2540 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2541 __get_user(*dst, src);
2544 target_to_host_sigset_internal(&set, &target_set);
2545 do_sigprocmask(SIG_SETMASK, &set, NULL);
2547 env->pc = pc;
2548 env->npc = npc;
2549 __get_user(env->y, &((*grp)[MC_Y]));
2550 __get_user(tstate, &((*grp)[MC_TSTATE]));
2551 env->asi = (tstate >> 24) & 0xff;
2552 cpu_put_ccr(env, tstate >> 32);
2553 cpu_put_cwp64(env, tstate & 0x1f);
2554 __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2555 __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2556 __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2557 __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2558 __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2559 __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2560 __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2561 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2562 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2563 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2564 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2565 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2566 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2567 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2568 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2570 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2571 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2573 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2574 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2575 abi_ulong) != 0)
2576 goto do_sigsegv;
2577 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2578 abi_ulong) != 0)
2579 goto do_sigsegv;
2580 /* FIXME this does not match how the kernel handles the FPU in
2581 * its sparc64_set_context implementation. In particular the FPU
2582 * is only restored if fenab is non-zero in:
2583 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2585 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2587 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2588 for (i = 0; i < 64; i++, src++) {
2589 if (i & 1) {
2590 __get_user(env->fpr[i/2].l.lower, src);
2591 } else {
2592 __get_user(env->fpr[i/2].l.upper, src);
2596 __get_user(env->fsr,
2597 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2598 __get_user(env->gsr,
2599 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2600 unlock_user_struct(ucp, ucp_addr, 0);
2601 return;
2602 do_sigsegv:
2603 unlock_user_struct(ucp, ucp_addr, 0);
2604 force_sig(TARGET_SIGSEGV);
2607 void sparc64_get_context(CPUSPARCState *env)
2609 abi_ulong ucp_addr;
2610 struct target_ucontext *ucp;
2611 target_mc_gregset_t *grp;
2612 target_mcontext_t *mcp;
2613 abi_ulong fp, i7, w_addr;
2614 int err;
2615 unsigned int i;
2616 target_sigset_t target_set;
2617 sigset_t set;
2619 ucp_addr = env->regwptr[UREG_I0];
2620 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2621 goto do_sigsegv;
2623 mcp = &ucp->tuc_mcontext;
2624 grp = &mcp->mc_gregs;
2626 /* Skip over the trap instruction, first. */
2627 env->pc = env->npc;
2628 env->npc += 4;
2630 err = 0;
2632 do_sigprocmask(0, NULL, &set);
2633 host_to_target_sigset_internal(&target_set, &set);
2634 if (TARGET_NSIG_WORDS == 1) {
2635 __put_user(target_set.sig[0],
2636 (abi_ulong *)&ucp->tuc_sigmask);
2637 } else {
2638 abi_ulong *src, *dst;
2639 src = target_set.sig;
2640 dst = ucp->tuc_sigmask.sig;
2641 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2642 __put_user(*src, dst);
2644 if (err)
2645 goto do_sigsegv;
2648 /* XXX: tstate must be saved properly */
2649 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2650 __put_user(env->pc, &((*grp)[MC_PC]));
2651 __put_user(env->npc, &((*grp)[MC_NPC]));
2652 __put_user(env->y, &((*grp)[MC_Y]));
2653 __put_user(env->gregs[1], &((*grp)[MC_G1]));
2654 __put_user(env->gregs[2], &((*grp)[MC_G2]));
2655 __put_user(env->gregs[3], &((*grp)[MC_G3]));
2656 __put_user(env->gregs[4], &((*grp)[MC_G4]));
2657 __put_user(env->gregs[5], &((*grp)[MC_G5]));
2658 __put_user(env->gregs[6], &((*grp)[MC_G6]));
2659 __put_user(env->gregs[7], &((*grp)[MC_G7]));
2660 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2661 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2662 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2663 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2664 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2665 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2666 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2667 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2669 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2670 fp = i7 = 0;
2671 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2672 abi_ulong) != 0)
2673 goto do_sigsegv;
2674 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2675 abi_ulong) != 0)
2676 goto do_sigsegv;
2677 __put_user(fp, &(mcp->mc_fp));
2678 __put_user(i7, &(mcp->mc_i7));
2681 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2682 for (i = 0; i < 64; i++, dst++) {
2683 if (i & 1) {
2684 __put_user(env->fpr[i/2].l.lower, dst);
2685 } else {
2686 __put_user(env->fpr[i/2].l.upper, dst);
2690 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2691 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2692 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2694 if (err)
2695 goto do_sigsegv;
2696 unlock_user_struct(ucp, ucp_addr, 1);
2697 return;
2698 do_sigsegv:
2699 unlock_user_struct(ucp, ucp_addr, 1);
2700 force_sig(TARGET_SIGSEGV);
2702 #endif
2703 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2705 # if defined(TARGET_ABI_MIPSO32)
2706 struct target_sigcontext {
2707 uint32_t sc_regmask; /* Unused */
2708 uint32_t sc_status;
2709 uint64_t sc_pc;
2710 uint64_t sc_regs[32];
2711 uint64_t sc_fpregs[32];
2712 uint32_t sc_ownedfp; /* Unused */
2713 uint32_t sc_fpc_csr;
2714 uint32_t sc_fpc_eir; /* Unused */
2715 uint32_t sc_used_math;
2716 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2717 uint32_t pad0;
2718 uint64_t sc_mdhi;
2719 uint64_t sc_mdlo;
2720 target_ulong sc_hi1; /* Was sc_cause */
2721 target_ulong sc_lo1; /* Was sc_badvaddr */
2722 target_ulong sc_hi2; /* Was sc_sigset[4] */
2723 target_ulong sc_lo2;
2724 target_ulong sc_hi3;
2725 target_ulong sc_lo3;
2727 # else /* N32 || N64 */
2728 struct target_sigcontext {
2729 uint64_t sc_regs[32];
2730 uint64_t sc_fpregs[32];
2731 uint64_t sc_mdhi;
2732 uint64_t sc_hi1;
2733 uint64_t sc_hi2;
2734 uint64_t sc_hi3;
2735 uint64_t sc_mdlo;
2736 uint64_t sc_lo1;
2737 uint64_t sc_lo2;
2738 uint64_t sc_lo3;
2739 uint64_t sc_pc;
2740 uint32_t sc_fpc_csr;
2741 uint32_t sc_used_math;
2742 uint32_t sc_dsp;
2743 uint32_t sc_reserved;
2745 # endif /* O32 */
2747 struct sigframe {
2748 uint32_t sf_ass[4]; /* argument save space for o32 */
2749 uint32_t sf_code[2]; /* signal trampoline */
2750 struct target_sigcontext sf_sc;
2751 target_sigset_t sf_mask;
2754 struct target_ucontext {
2755 target_ulong tuc_flags;
2756 target_ulong tuc_link;
2757 target_stack_t tuc_stack;
2758 target_ulong pad0;
2759 struct target_sigcontext tuc_mcontext;
2760 target_sigset_t tuc_sigmask;
2763 struct target_rt_sigframe {
2764 uint32_t rs_ass[4]; /* argument save space for o32 */
2765 uint32_t rs_code[2]; /* signal trampoline */
2766 struct target_siginfo rs_info;
2767 struct target_ucontext rs_uc;
2770 /* Install trampoline to jump back from signal handler */
2771 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2773 int err = 0;
2776 * Set up the return code ...
2778 * li v0, __NR__foo_sigreturn
2779 * syscall
2782 __put_user(0x24020000 + syscall, tramp + 0);
2783 __put_user(0x0000000c , tramp + 1);
2784 return err;
2787 static inline void setup_sigcontext(CPUMIPSState *regs,
2788 struct target_sigcontext *sc)
2790 int i;
2792 __put_user(exception_resume_pc(regs), &sc->sc_pc);
2793 regs->hflags &= ~MIPS_HFLAG_BMASK;
2795 __put_user(0, &sc->sc_regs[0]);
2796 for (i = 1; i < 32; ++i) {
2797 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2800 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2801 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2803 /* Rather than checking for dsp existence, always copy. The storage
2804 would just be garbage otherwise. */
2805 __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2806 __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2807 __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2808 __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2809 __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2810 __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2812 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2813 __put_user(dsp, &sc->sc_dsp);
2816 __put_user(1, &sc->sc_used_math);
2818 for (i = 0; i < 32; ++i) {
2819 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2823 static inline void
2824 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2826 int i;
2828 __get_user(regs->CP0_EPC, &sc->sc_pc);
2830 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2831 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2833 for (i = 1; i < 32; ++i) {
2834 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2837 __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2838 __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2839 __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2840 __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2841 __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2842 __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2844 uint32_t dsp;
2845 __get_user(dsp, &sc->sc_dsp);
2846 cpu_wrdsp(dsp, 0x3ff, regs);
2849 for (i = 0; i < 32; ++i) {
2850 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2855 * Determine which stack to use..
2857 static inline abi_ulong
2858 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2860 unsigned long sp;
2862 /* Default to using normal stack */
2863 sp = regs->active_tc.gpr[29];
2866 * FPU emulator may have its own trampoline active just
2867 * above the user stack, 16-bytes before the next lowest
2868 * 16 byte boundary. Try to avoid trashing it.
2870 sp -= 32;
2872 /* This is the X/Open sanctioned signal stack switching. */
2873 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2874 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2877 return (sp - frame_size) & ~7;
2880 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2882 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2883 env->hflags &= ~MIPS_HFLAG_M16;
2884 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2885 env->active_tc.PC &= ~(target_ulong) 1;
2889 # if defined(TARGET_ABI_MIPSO32)
2890 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2891 static void setup_frame(int sig, struct target_sigaction * ka,
2892 target_sigset_t *set, CPUMIPSState *regs)
2894 struct sigframe *frame;
2895 abi_ulong frame_addr;
2896 int i;
2898 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2899 trace_user_setup_frame(regs, frame_addr);
2900 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2901 goto give_sigsegv;
2903 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2905 setup_sigcontext(regs, &frame->sf_sc);
2907 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2908 __put_user(set->sig[i], &frame->sf_mask.sig[i]);
2912 * Arguments to signal handler:
2914 * a0 = signal number
2915 * a1 = 0 (should be cause)
2916 * a2 = pointer to struct sigcontext
2918 * $25 and PC point to the signal handler, $29 points to the
2919 * struct sigframe.
2921 regs->active_tc.gpr[ 4] = sig;
2922 regs->active_tc.gpr[ 5] = 0;
2923 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2924 regs->active_tc.gpr[29] = frame_addr;
2925 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2926 /* The original kernel code sets CP0_EPC to the handler
2927 * since it returns to userland using eret
2928 * we cannot do this here, and we must set PC directly */
2929 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2930 mips_set_hflags_isa_mode_from_pc(regs);
2931 unlock_user_struct(frame, frame_addr, 1);
2932 return;
2934 give_sigsegv:
2935 force_sig(TARGET_SIGSEGV/*, current*/);
2938 long do_sigreturn(CPUMIPSState *regs)
2940 struct sigframe *frame;
2941 abi_ulong frame_addr;
2942 sigset_t blocked;
2943 target_sigset_t target_set;
2944 int i;
2946 frame_addr = regs->active_tc.gpr[29];
2947 trace_user_do_sigreturn(regs, frame_addr);
2948 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2949 goto badframe;
2951 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2952 __get_user(target_set.sig[i], &frame->sf_mask.sig[i]);
2955 target_to_host_sigset_internal(&blocked, &target_set);
2956 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
2958 restore_sigcontext(regs, &frame->sf_sc);
2960 #if 0
2962 * Don't let your children do this ...
2964 __asm__ __volatile__(
2965 "move\t$29, %0\n\t"
2966 "j\tsyscall_exit"
2967 :/* no outputs */
2968 :"r" (&regs));
2969 /* Unreached */
2970 #endif
2972 regs->active_tc.PC = regs->CP0_EPC;
2973 mips_set_hflags_isa_mode_from_pc(regs);
2974 /* I am not sure this is right, but it seems to work
2975 * maybe a problem with nested signals ? */
2976 regs->CP0_EPC = 0;
2977 return -TARGET_QEMU_ESIGRETURN;
2979 badframe:
2980 force_sig(TARGET_SIGSEGV/*, current*/);
2981 return 0;
2983 # endif /* O32 */
2985 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2986 target_siginfo_t *info,
2987 target_sigset_t *set, CPUMIPSState *env)
2989 struct target_rt_sigframe *frame;
2990 abi_ulong frame_addr;
2991 int i;
2993 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2994 trace_user_setup_rt_frame(env, frame_addr);
2995 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2996 goto give_sigsegv;
2998 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3000 tswap_siginfo(&frame->rs_info, info);
3002 __put_user(0, &frame->rs_uc.tuc_flags);
3003 __put_user(0, &frame->rs_uc.tuc_link);
3004 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3005 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3006 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3007 &frame->rs_uc.tuc_stack.ss_flags);
3009 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3011 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3012 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3016 * Arguments to signal handler:
3018 * a0 = signal number
3019 * a1 = pointer to siginfo_t
3020 * a2 = pointer to struct ucontext
3022 * $25 and PC point to the signal handler, $29 points to the
3023 * struct sigframe.
3025 env->active_tc.gpr[ 4] = sig;
3026 env->active_tc.gpr[ 5] = frame_addr
3027 + offsetof(struct target_rt_sigframe, rs_info);
3028 env->active_tc.gpr[ 6] = frame_addr
3029 + offsetof(struct target_rt_sigframe, rs_uc);
3030 env->active_tc.gpr[29] = frame_addr;
3031 env->active_tc.gpr[31] = frame_addr
3032 + offsetof(struct target_rt_sigframe, rs_code);
3033 /* The original kernel code sets CP0_EPC to the handler
3034 * since it returns to userland using eret
3035 * we cannot do this here, and we must set PC directly */
3036 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3037 mips_set_hflags_isa_mode_from_pc(env);
3038 unlock_user_struct(frame, frame_addr, 1);
3039 return;
3041 give_sigsegv:
3042 unlock_user_struct(frame, frame_addr, 1);
3043 force_sig(TARGET_SIGSEGV/*, current*/);
3046 long do_rt_sigreturn(CPUMIPSState *env)
3048 struct target_rt_sigframe *frame;
3049 abi_ulong frame_addr;
3050 sigset_t blocked;
3052 frame_addr = env->active_tc.gpr[29];
3053 trace_user_do_rt_sigreturn(env, frame_addr);
3054 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3055 goto badframe;
3057 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3058 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3060 restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3062 if (do_sigaltstack(frame_addr +
3063 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3064 0, get_sp_from_cpustate(env)) == -EFAULT)
3065 goto badframe;
3067 env->active_tc.PC = env->CP0_EPC;
3068 mips_set_hflags_isa_mode_from_pc(env);
3069 /* I am not sure this is right, but it seems to work
3070 * maybe a problem with nested signals ? */
3071 env->CP0_EPC = 0;
3072 return -TARGET_QEMU_ESIGRETURN;
3074 badframe:
3075 force_sig(TARGET_SIGSEGV/*, current*/);
3076 return 0;
3079 #elif defined(TARGET_SH4)
3082 * code and data structures from linux kernel:
3083 * include/asm-sh/sigcontext.h
3084 * arch/sh/kernel/signal.c
3087 struct target_sigcontext {
3088 target_ulong oldmask;
3090 /* CPU registers */
3091 target_ulong sc_gregs[16];
3092 target_ulong sc_pc;
3093 target_ulong sc_pr;
3094 target_ulong sc_sr;
3095 target_ulong sc_gbr;
3096 target_ulong sc_mach;
3097 target_ulong sc_macl;
3099 /* FPU registers */
3100 target_ulong sc_fpregs[16];
3101 target_ulong sc_xfpregs[16];
3102 unsigned int sc_fpscr;
3103 unsigned int sc_fpul;
3104 unsigned int sc_ownedfp;
3107 struct target_sigframe
3109 struct target_sigcontext sc;
3110 target_ulong extramask[TARGET_NSIG_WORDS-1];
3111 uint16_t retcode[3];
3115 struct target_ucontext {
3116 target_ulong tuc_flags;
3117 struct target_ucontext *tuc_link;
3118 target_stack_t tuc_stack;
3119 struct target_sigcontext tuc_mcontext;
3120 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3123 struct target_rt_sigframe
3125 struct target_siginfo info;
3126 struct target_ucontext uc;
3127 uint16_t retcode[3];
3131 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3132 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3134 static abi_ulong get_sigframe(struct target_sigaction *ka,
3135 unsigned long sp, size_t frame_size)
3137 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3138 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3141 return (sp - frame_size) & -8ul;
3144 static void setup_sigcontext(struct target_sigcontext *sc,
3145 CPUSH4State *regs, unsigned long mask)
3147 int i;
3149 #define COPY(x) __put_user(regs->x, &sc->sc_##x)
3150 COPY(gregs[0]); COPY(gregs[1]);
3151 COPY(gregs[2]); COPY(gregs[3]);
3152 COPY(gregs[4]); COPY(gregs[5]);
3153 COPY(gregs[6]); COPY(gregs[7]);
3154 COPY(gregs[8]); COPY(gregs[9]);
3155 COPY(gregs[10]); COPY(gregs[11]);
3156 COPY(gregs[12]); COPY(gregs[13]);
3157 COPY(gregs[14]); COPY(gregs[15]);
3158 COPY(gbr); COPY(mach);
3159 COPY(macl); COPY(pr);
3160 COPY(sr); COPY(pc);
3161 #undef COPY
3163 for (i=0; i<16; i++) {
3164 __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3166 __put_user(regs->fpscr, &sc->sc_fpscr);
3167 __put_user(regs->fpul, &sc->sc_fpul);
3169 /* non-iBCS2 extensions.. */
3170 __put_user(mask, &sc->oldmask);
3173 static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3174 target_ulong *r0_p)
3176 int i;
3178 #define COPY(x) __get_user(regs->x, &sc->sc_##x)
3179 COPY(gregs[1]);
3180 COPY(gregs[2]); COPY(gregs[3]);
3181 COPY(gregs[4]); COPY(gregs[5]);
3182 COPY(gregs[6]); COPY(gregs[7]);
3183 COPY(gregs[8]); COPY(gregs[9]);
3184 COPY(gregs[10]); COPY(gregs[11]);
3185 COPY(gregs[12]); COPY(gregs[13]);
3186 COPY(gregs[14]); COPY(gregs[15]);
3187 COPY(gbr); COPY(mach);
3188 COPY(macl); COPY(pr);
3189 COPY(sr); COPY(pc);
3190 #undef COPY
3192 for (i=0; i<16; i++) {
3193 __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3195 __get_user(regs->fpscr, &sc->sc_fpscr);
3196 __get_user(regs->fpul, &sc->sc_fpul);
3198 regs->tra = -1; /* disable syscall checks */
3199 __get_user(*r0_p, &sc->sc_gregs[0]);
3202 static void setup_frame(int sig, struct target_sigaction *ka,
3203 target_sigset_t *set, CPUSH4State *regs)
3205 struct target_sigframe *frame;
3206 abi_ulong frame_addr;
3207 int i;
3209 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3210 trace_user_setup_frame(regs, frame_addr);
3211 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3212 goto give_sigsegv;
3214 setup_sigcontext(&frame->sc, regs, set->sig[0]);
3216 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3217 __put_user(set->sig[i + 1], &frame->extramask[i]);
3220 /* Set up to return from userspace. If provided, use a stub
3221 already in userspace. */
3222 if (ka->sa_flags & TARGET_SA_RESTORER) {
3223 regs->pr = (unsigned long) ka->sa_restorer;
3224 } else {
3225 /* Generate return code (system call to sigreturn) */
3226 abi_ulong retcode_addr = frame_addr +
3227 offsetof(struct target_sigframe, retcode);
3228 __put_user(MOVW(2), &frame->retcode[0]);
3229 __put_user(TRAP_NOARG, &frame->retcode[1]);
3230 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3231 regs->pr = (unsigned long) retcode_addr;
3234 /* Set up registers for signal handler */
3235 regs->gregs[15] = frame_addr;
3236 regs->gregs[4] = sig; /* Arg for signal handler */
3237 regs->gregs[5] = 0;
3238 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3239 regs->pc = (unsigned long) ka->_sa_handler;
3241 unlock_user_struct(frame, frame_addr, 1);
3242 return;
3244 give_sigsegv:
3245 unlock_user_struct(frame, frame_addr, 1);
3246 force_sig(TARGET_SIGSEGV);
3249 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3250 target_siginfo_t *info,
3251 target_sigset_t *set, CPUSH4State *regs)
3253 struct target_rt_sigframe *frame;
3254 abi_ulong frame_addr;
3255 int i;
3257 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3258 trace_user_setup_rt_frame(regs, frame_addr);
3259 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3260 goto give_sigsegv;
3262 tswap_siginfo(&frame->info, info);
3264 /* Create the ucontext. */
3265 __put_user(0, &frame->uc.tuc_flags);
3266 __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3267 __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3268 &frame->uc.tuc_stack.ss_sp);
3269 __put_user(sas_ss_flags(regs->gregs[15]),
3270 &frame->uc.tuc_stack.ss_flags);
3271 __put_user(target_sigaltstack_used.ss_size,
3272 &frame->uc.tuc_stack.ss_size);
3273 setup_sigcontext(&frame->uc.tuc_mcontext,
3274 regs, set->sig[0]);
3275 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3276 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3279 /* Set up to return from userspace. If provided, use a stub
3280 already in userspace. */
3281 if (ka->sa_flags & TARGET_SA_RESTORER) {
3282 regs->pr = (unsigned long) ka->sa_restorer;
3283 } else {
3284 /* Generate return code (system call to sigreturn) */
3285 abi_ulong retcode_addr = frame_addr +
3286 offsetof(struct target_rt_sigframe, retcode);
3287 __put_user(MOVW(2), &frame->retcode[0]);
3288 __put_user(TRAP_NOARG, &frame->retcode[1]);
3289 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3290 regs->pr = (unsigned long) retcode_addr;
3293 /* Set up registers for signal handler */
3294 regs->gregs[15] = frame_addr;
3295 regs->gregs[4] = sig; /* Arg for signal handler */
3296 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3297 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3298 regs->pc = (unsigned long) ka->_sa_handler;
3300 unlock_user_struct(frame, frame_addr, 1);
3301 return;
3303 give_sigsegv:
3304 unlock_user_struct(frame, frame_addr, 1);
3305 force_sig(TARGET_SIGSEGV);
3308 long do_sigreturn(CPUSH4State *regs)
3310 struct target_sigframe *frame;
3311 abi_ulong frame_addr;
3312 sigset_t blocked;
3313 target_sigset_t target_set;
3314 target_ulong r0;
3315 int i;
3316 int err = 0;
3318 frame_addr = regs->gregs[15];
3319 trace_user_do_sigreturn(regs, frame_addr);
3320 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3321 goto badframe;
3323 __get_user(target_set.sig[0], &frame->sc.oldmask);
3324 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3325 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3328 if (err)
3329 goto badframe;
3331 target_to_host_sigset_internal(&blocked, &target_set);
3332 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3334 restore_sigcontext(regs, &frame->sc, &r0);
3336 unlock_user_struct(frame, frame_addr, 0);
3337 return r0;
3339 badframe:
3340 unlock_user_struct(frame, frame_addr, 0);
3341 force_sig(TARGET_SIGSEGV);
3342 return 0;
3345 long do_rt_sigreturn(CPUSH4State *regs)
3347 struct target_rt_sigframe *frame;
3348 abi_ulong frame_addr;
3349 sigset_t blocked;
3350 target_ulong r0;
3352 frame_addr = regs->gregs[15];
3353 trace_user_do_rt_sigreturn(regs, frame_addr);
3354 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3355 goto badframe;
3357 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3358 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3360 restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0);
3362 if (do_sigaltstack(frame_addr +
3363 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3364 0, get_sp_from_cpustate(regs)) == -EFAULT)
3365 goto badframe;
3367 unlock_user_struct(frame, frame_addr, 0);
3368 return r0;
3370 badframe:
3371 unlock_user_struct(frame, frame_addr, 0);
3372 force_sig(TARGET_SIGSEGV);
3373 return 0;
3375 #elif defined(TARGET_MICROBLAZE)
3377 struct target_sigcontext {
3378 struct target_pt_regs regs; /* needs to be first */
3379 uint32_t oldmask;
3382 struct target_stack_t {
3383 abi_ulong ss_sp;
3384 int ss_flags;
3385 unsigned int ss_size;
3388 struct target_ucontext {
3389 abi_ulong tuc_flags;
3390 abi_ulong tuc_link;
3391 struct target_stack_t tuc_stack;
3392 struct target_sigcontext tuc_mcontext;
3393 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3396 /* Signal frames. */
3397 struct target_signal_frame {
3398 struct target_ucontext uc;
3399 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3400 uint32_t tramp[2];
3403 struct rt_signal_frame {
3404 siginfo_t info;
3405 struct ucontext uc;
3406 uint32_t tramp[2];
3409 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3411 __put_user(env->regs[0], &sc->regs.r0);
3412 __put_user(env->regs[1], &sc->regs.r1);
3413 __put_user(env->regs[2], &sc->regs.r2);
3414 __put_user(env->regs[3], &sc->regs.r3);
3415 __put_user(env->regs[4], &sc->regs.r4);
3416 __put_user(env->regs[5], &sc->regs.r5);
3417 __put_user(env->regs[6], &sc->regs.r6);
3418 __put_user(env->regs[7], &sc->regs.r7);
3419 __put_user(env->regs[8], &sc->regs.r8);
3420 __put_user(env->regs[9], &sc->regs.r9);
3421 __put_user(env->regs[10], &sc->regs.r10);
3422 __put_user(env->regs[11], &sc->regs.r11);
3423 __put_user(env->regs[12], &sc->regs.r12);
3424 __put_user(env->regs[13], &sc->regs.r13);
3425 __put_user(env->regs[14], &sc->regs.r14);
3426 __put_user(env->regs[15], &sc->regs.r15);
3427 __put_user(env->regs[16], &sc->regs.r16);
3428 __put_user(env->regs[17], &sc->regs.r17);
3429 __put_user(env->regs[18], &sc->regs.r18);
3430 __put_user(env->regs[19], &sc->regs.r19);
3431 __put_user(env->regs[20], &sc->regs.r20);
3432 __put_user(env->regs[21], &sc->regs.r21);
3433 __put_user(env->regs[22], &sc->regs.r22);
3434 __put_user(env->regs[23], &sc->regs.r23);
3435 __put_user(env->regs[24], &sc->regs.r24);
3436 __put_user(env->regs[25], &sc->regs.r25);
3437 __put_user(env->regs[26], &sc->regs.r26);
3438 __put_user(env->regs[27], &sc->regs.r27);
3439 __put_user(env->regs[28], &sc->regs.r28);
3440 __put_user(env->regs[29], &sc->regs.r29);
3441 __put_user(env->regs[30], &sc->regs.r30);
3442 __put_user(env->regs[31], &sc->regs.r31);
3443 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3446 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3448 __get_user(env->regs[0], &sc->regs.r0);
3449 __get_user(env->regs[1], &sc->regs.r1);
3450 __get_user(env->regs[2], &sc->regs.r2);
3451 __get_user(env->regs[3], &sc->regs.r3);
3452 __get_user(env->regs[4], &sc->regs.r4);
3453 __get_user(env->regs[5], &sc->regs.r5);
3454 __get_user(env->regs[6], &sc->regs.r6);
3455 __get_user(env->regs[7], &sc->regs.r7);
3456 __get_user(env->regs[8], &sc->regs.r8);
3457 __get_user(env->regs[9], &sc->regs.r9);
3458 __get_user(env->regs[10], &sc->regs.r10);
3459 __get_user(env->regs[11], &sc->regs.r11);
3460 __get_user(env->regs[12], &sc->regs.r12);
3461 __get_user(env->regs[13], &sc->regs.r13);
3462 __get_user(env->regs[14], &sc->regs.r14);
3463 __get_user(env->regs[15], &sc->regs.r15);
3464 __get_user(env->regs[16], &sc->regs.r16);
3465 __get_user(env->regs[17], &sc->regs.r17);
3466 __get_user(env->regs[18], &sc->regs.r18);
3467 __get_user(env->regs[19], &sc->regs.r19);
3468 __get_user(env->regs[20], &sc->regs.r20);
3469 __get_user(env->regs[21], &sc->regs.r21);
3470 __get_user(env->regs[22], &sc->regs.r22);
3471 __get_user(env->regs[23], &sc->regs.r23);
3472 __get_user(env->regs[24], &sc->regs.r24);
3473 __get_user(env->regs[25], &sc->regs.r25);
3474 __get_user(env->regs[26], &sc->regs.r26);
3475 __get_user(env->regs[27], &sc->regs.r27);
3476 __get_user(env->regs[28], &sc->regs.r28);
3477 __get_user(env->regs[29], &sc->regs.r29);
3478 __get_user(env->regs[30], &sc->regs.r30);
3479 __get_user(env->regs[31], &sc->regs.r31);
3480 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3483 static abi_ulong get_sigframe(struct target_sigaction *ka,
3484 CPUMBState *env, int frame_size)
3486 abi_ulong sp = env->regs[1];
3488 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !on_sig_stack(sp)) {
3489 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3492 return ((sp - frame_size) & -8UL);
3495 static void setup_frame(int sig, struct target_sigaction *ka,
3496 target_sigset_t *set, CPUMBState *env)
3498 struct target_signal_frame *frame;
3499 abi_ulong frame_addr;
3500 int i;
3502 frame_addr = get_sigframe(ka, env, sizeof *frame);
3503 trace_user_setup_frame(env, frame_addr);
3504 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3505 goto badframe;
3507 /* Save the mask. */
3508 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3510 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3511 __put_user(set->sig[i], &frame->extramask[i - 1]);
3514 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3516 /* Set up to return from userspace. If provided, use a stub
3517 already in userspace. */
3518 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3519 if (ka->sa_flags & TARGET_SA_RESTORER) {
3520 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3521 } else {
3522 uint32_t t;
3523 /* Note, these encodings are _big endian_! */
3524 /* addi r12, r0, __NR_sigreturn */
3525 t = 0x31800000UL | TARGET_NR_sigreturn;
3526 __put_user(t, frame->tramp + 0);
3527 /* brki r14, 0x8 */
3528 t = 0xb9cc0008UL;
3529 __put_user(t, frame->tramp + 1);
3531 /* Return from sighandler will jump to the tramp.
3532 Negative 8 offset because return is rtsd r15, 8 */
3533 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3536 /* Set up registers for signal handler */
3537 env->regs[1] = frame_addr;
3538 /* Signal handler args: */
3539 env->regs[5] = sig; /* Arg 0: signum */
3540 env->regs[6] = 0;
3541 /* arg 1: sigcontext */
3542 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3544 /* Offset of 4 to handle microblaze rtid r14, 0 */
3545 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3547 unlock_user_struct(frame, frame_addr, 1);
3548 return;
3549 badframe:
3550 force_sig(TARGET_SIGSEGV);
3553 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3554 target_siginfo_t *info,
3555 target_sigset_t *set, CPUMBState *env)
3557 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3560 long do_sigreturn(CPUMBState *env)
3562 struct target_signal_frame *frame;
3563 abi_ulong frame_addr;
3564 target_sigset_t target_set;
3565 sigset_t set;
3566 int i;
3568 frame_addr = env->regs[R_SP];
3569 trace_user_do_sigreturn(env, frame_addr);
3570 /* Make sure the guest isn't playing games. */
3571 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3572 goto badframe;
3574 /* Restore blocked signals */
3575 __get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask);
3576 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3577 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3579 target_to_host_sigset_internal(&set, &target_set);
3580 do_sigprocmask(SIG_SETMASK, &set, NULL);
3582 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3583 /* We got here through a sigreturn syscall, our path back is via an
3584 rtb insn so setup r14 for that. */
3585 env->regs[14] = env->sregs[SR_PC];
3587 unlock_user_struct(frame, frame_addr, 0);
3588 return env->regs[10];
3589 badframe:
3590 force_sig(TARGET_SIGSEGV);
3593 long do_rt_sigreturn(CPUMBState *env)
3595 trace_user_do_rt_sigreturn(env, 0);
3596 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3597 return -TARGET_ENOSYS;
3600 #elif defined(TARGET_CRIS)
3602 struct target_sigcontext {
3603 struct target_pt_regs regs; /* needs to be first */
3604 uint32_t oldmask;
3605 uint32_t usp; /* usp before stacking this gunk on it */
3608 /* Signal frames. */
3609 struct target_signal_frame {
3610 struct target_sigcontext sc;
3611 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3612 uint16_t retcode[4]; /* Trampoline code. */
3615 struct rt_signal_frame {
3616 siginfo_t *pinfo;
3617 void *puc;
3618 siginfo_t info;
3619 struct ucontext uc;
3620 uint16_t retcode[4]; /* Trampoline code. */
3623 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3625 __put_user(env->regs[0], &sc->regs.r0);
3626 __put_user(env->regs[1], &sc->regs.r1);
3627 __put_user(env->regs[2], &sc->regs.r2);
3628 __put_user(env->regs[3], &sc->regs.r3);
3629 __put_user(env->regs[4], &sc->regs.r4);
3630 __put_user(env->regs[5], &sc->regs.r5);
3631 __put_user(env->regs[6], &sc->regs.r6);
3632 __put_user(env->regs[7], &sc->regs.r7);
3633 __put_user(env->regs[8], &sc->regs.r8);
3634 __put_user(env->regs[9], &sc->regs.r9);
3635 __put_user(env->regs[10], &sc->regs.r10);
3636 __put_user(env->regs[11], &sc->regs.r11);
3637 __put_user(env->regs[12], &sc->regs.r12);
3638 __put_user(env->regs[13], &sc->regs.r13);
3639 __put_user(env->regs[14], &sc->usp);
3640 __put_user(env->regs[15], &sc->regs.acr);
3641 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3642 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3643 __put_user(env->pc, &sc->regs.erp);
3646 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3648 __get_user(env->regs[0], &sc->regs.r0);
3649 __get_user(env->regs[1], &sc->regs.r1);
3650 __get_user(env->regs[2], &sc->regs.r2);
3651 __get_user(env->regs[3], &sc->regs.r3);
3652 __get_user(env->regs[4], &sc->regs.r4);
3653 __get_user(env->regs[5], &sc->regs.r5);
3654 __get_user(env->regs[6], &sc->regs.r6);
3655 __get_user(env->regs[7], &sc->regs.r7);
3656 __get_user(env->regs[8], &sc->regs.r8);
3657 __get_user(env->regs[9], &sc->regs.r9);
3658 __get_user(env->regs[10], &sc->regs.r10);
3659 __get_user(env->regs[11], &sc->regs.r11);
3660 __get_user(env->regs[12], &sc->regs.r12);
3661 __get_user(env->regs[13], &sc->regs.r13);
3662 __get_user(env->regs[14], &sc->usp);
3663 __get_user(env->regs[15], &sc->regs.acr);
3664 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3665 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3666 __get_user(env->pc, &sc->regs.erp);
3669 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3671 abi_ulong sp;
3672 /* Align the stack downwards to 4. */
3673 sp = (env->regs[R_SP] & ~3);
3674 return sp - framesize;
3677 static void setup_frame(int sig, struct target_sigaction *ka,
3678 target_sigset_t *set, CPUCRISState *env)
3680 struct target_signal_frame *frame;
3681 abi_ulong frame_addr;
3682 int i;
3684 frame_addr = get_sigframe(env, sizeof *frame);
3685 trace_user_setup_frame(env, frame_addr);
3686 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3687 goto badframe;
3690 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3691 * use this trampoline anymore but it sets it up for GDB.
3692 * In QEMU, using the trampoline simplifies things a bit so we use it.
3694 * This is movu.w __NR_sigreturn, r9; break 13;
3696 __put_user(0x9c5f, frame->retcode+0);
3697 __put_user(TARGET_NR_sigreturn,
3698 frame->retcode + 1);
3699 __put_user(0xe93d, frame->retcode + 2);
3701 /* Save the mask. */
3702 __put_user(set->sig[0], &frame->sc.oldmask);
3704 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3705 __put_user(set->sig[i], &frame->extramask[i - 1]);
3708 setup_sigcontext(&frame->sc, env);
3710 /* Move the stack and setup the arguments for the handler. */
3711 env->regs[R_SP] = frame_addr;
3712 env->regs[10] = sig;
3713 env->pc = (unsigned long) ka->_sa_handler;
3714 /* Link SRP so the guest returns through the trampoline. */
3715 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3717 unlock_user_struct(frame, frame_addr, 1);
3718 return;
3719 badframe:
3720 force_sig(TARGET_SIGSEGV);
3723 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3724 target_siginfo_t *info,
3725 target_sigset_t *set, CPUCRISState *env)
3727 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3730 long do_sigreturn(CPUCRISState *env)
3732 struct target_signal_frame *frame;
3733 abi_ulong frame_addr;
3734 target_sigset_t target_set;
3735 sigset_t set;
3736 int i;
3738 frame_addr = env->regs[R_SP];
3739 trace_user_do_sigreturn(env, frame_addr);
3740 /* Make sure the guest isn't playing games. */
3741 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3742 goto badframe;
3744 /* Restore blocked signals */
3745 __get_user(target_set.sig[0], &frame->sc.oldmask);
3746 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3747 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3749 target_to_host_sigset_internal(&set, &target_set);
3750 do_sigprocmask(SIG_SETMASK, &set, NULL);
3752 restore_sigcontext(&frame->sc, env);
3753 unlock_user_struct(frame, frame_addr, 0);
3754 return env->regs[10];
3755 badframe:
3756 force_sig(TARGET_SIGSEGV);
3759 long do_rt_sigreturn(CPUCRISState *env)
3761 trace_user_do_rt_sigreturn(env, 0);
3762 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3763 return -TARGET_ENOSYS;
3766 #elif defined(TARGET_OPENRISC)
3768 struct target_sigcontext {
3769 struct target_pt_regs regs;
3770 abi_ulong oldmask;
3771 abi_ulong usp;
3774 struct target_ucontext {
3775 abi_ulong tuc_flags;
3776 abi_ulong tuc_link;
3777 target_stack_t tuc_stack;
3778 struct target_sigcontext tuc_mcontext;
3779 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3782 struct target_rt_sigframe {
3783 abi_ulong pinfo;
3784 uint64_t puc;
3785 struct target_siginfo info;
3786 struct target_sigcontext sc;
3787 struct target_ucontext uc;
3788 unsigned char retcode[16]; /* trampoline code */
3791 /* This is the asm-generic/ucontext.h version */
3792 #if 0
3793 static int restore_sigcontext(CPUOpenRISCState *regs,
3794 struct target_sigcontext *sc)
3796 unsigned int err = 0;
3797 unsigned long old_usp;
3799 /* Alwys make any pending restarted system call return -EINTR */
3800 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3802 /* restore the regs from &sc->regs (same as sc, since regs is first)
3803 * (sc is already checked for VERIFY_READ since the sigframe was
3804 * checked in sys_sigreturn previously)
3807 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3808 goto badframe;
3811 /* make sure the U-flag is set so user-mode cannot fool us */
3813 regs->sr &= ~SR_SM;
3815 /* restore the old USP as it was before we stacked the sc etc.
3816 * (we cannot just pop the sigcontext since we aligned the sp and
3817 * stuff after pushing it)
3820 __get_user(old_usp, &sc->usp);
3821 phx_signal("old_usp 0x%lx", old_usp);
3823 __PHX__ REALLY /* ??? */
3824 wrusp(old_usp);
3825 regs->gpr[1] = old_usp;
3827 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3828 * after this completes, but we don't use that mechanism. maybe we can
3829 * use it now ?
3832 return err;
3834 badframe:
3835 return 1;
3837 #endif
3839 /* Set up a signal frame. */
3841 static void setup_sigcontext(struct target_sigcontext *sc,
3842 CPUOpenRISCState *regs,
3843 unsigned long mask)
3845 unsigned long usp = regs->gpr[1];
3847 /* copy the regs. they are first in sc so we can use sc directly */
3849 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3851 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3852 the signal handler. The frametype will be restored to its previous
3853 value in restore_sigcontext. */
3854 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3856 /* then some other stuff */
3857 __put_user(mask, &sc->oldmask);
3858 __put_user(usp, &sc->usp);
3861 static inline unsigned long align_sigframe(unsigned long sp)
3863 unsigned long i;
3864 i = sp & ~3UL;
3865 return i;
3868 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3869 CPUOpenRISCState *regs,
3870 size_t frame_size)
3872 unsigned long sp = regs->gpr[1];
3873 int onsigstack = on_sig_stack(sp);
3875 /* redzone */
3876 /* This is the X/Open sanctioned signal stack switching. */
3877 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !onsigstack) {
3878 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3881 sp = align_sigframe(sp - frame_size);
3884 * If we are on the alternate signal stack and would overflow it, don't.
3885 * Return an always-bogus address instead so we will die with SIGSEGV.
3888 if (onsigstack && !likely(on_sig_stack(sp))) {
3889 return -1L;
3892 return sp;
3895 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3896 target_siginfo_t *info,
3897 target_sigset_t *set, CPUOpenRISCState *env)
3899 int err = 0;
3900 abi_ulong frame_addr;
3901 unsigned long return_ip;
3902 struct target_rt_sigframe *frame;
3903 abi_ulong info_addr, uc_addr;
3905 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3906 trace_user_setup_rt_frame(env, frame_addr);
3907 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3908 goto give_sigsegv;
3911 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3912 __put_user(info_addr, &frame->pinfo);
3913 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3914 __put_user(uc_addr, &frame->puc);
3916 if (ka->sa_flags & SA_SIGINFO) {
3917 tswap_siginfo(&frame->info, info);
3920 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3921 __put_user(0, &frame->uc.tuc_flags);
3922 __put_user(0, &frame->uc.tuc_link);
3923 __put_user(target_sigaltstack_used.ss_sp,
3924 &frame->uc.tuc_stack.ss_sp);
3925 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3926 __put_user(target_sigaltstack_used.ss_size,
3927 &frame->uc.tuc_stack.ss_size);
3928 setup_sigcontext(&frame->sc, env, set->sig[0]);
3930 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3932 /* trampoline - the desired return ip is the retcode itself */
3933 return_ip = (unsigned long)&frame->retcode;
3934 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3935 __put_user(0xa960, (short *)(frame->retcode + 0));
3936 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3937 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3938 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3940 if (err) {
3941 goto give_sigsegv;
3944 /* TODO what is the current->exec_domain stuff and invmap ? */
3946 /* Set up registers for signal handler */
3947 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3948 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3949 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3950 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3951 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3953 /* actually move the usp to reflect the stacked frame */
3954 env->gpr[1] = (unsigned long)frame;
3956 return;
3958 give_sigsegv:
3959 unlock_user_struct(frame, frame_addr, 1);
3960 if (sig == TARGET_SIGSEGV) {
3961 ka->_sa_handler = TARGET_SIG_DFL;
3963 force_sig(TARGET_SIGSEGV);
3966 long do_sigreturn(CPUOpenRISCState *env)
3968 trace_user_do_sigreturn(env, 0);
3969 fprintf(stderr, "do_sigreturn: not implemented\n");
3970 return -TARGET_ENOSYS;
3973 long do_rt_sigreturn(CPUOpenRISCState *env)
3975 trace_user_do_rt_sigreturn(env, 0);
3976 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
3977 return -TARGET_ENOSYS;
3979 /* TARGET_OPENRISC */
3981 #elif defined(TARGET_S390X)
3983 #define __NUM_GPRS 16
3984 #define __NUM_FPRS 16
3985 #define __NUM_ACRS 16
3987 #define S390_SYSCALL_SIZE 2
3988 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3990 #define _SIGCONTEXT_NSIG 64
3991 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3992 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3993 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3994 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
3995 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3997 typedef struct {
3998 target_psw_t psw;
3999 target_ulong gprs[__NUM_GPRS];
4000 unsigned int acrs[__NUM_ACRS];
4001 } target_s390_regs_common;
4003 typedef struct {
4004 unsigned int fpc;
4005 double fprs[__NUM_FPRS];
4006 } target_s390_fp_regs;
4008 typedef struct {
4009 target_s390_regs_common regs;
4010 target_s390_fp_regs fpregs;
4011 } target_sigregs;
4013 struct target_sigcontext {
4014 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4015 target_sigregs *sregs;
4018 typedef struct {
4019 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4020 struct target_sigcontext sc;
4021 target_sigregs sregs;
4022 int signo;
4023 uint8_t retcode[S390_SYSCALL_SIZE];
4024 } sigframe;
4026 struct target_ucontext {
4027 target_ulong tuc_flags;
4028 struct target_ucontext *tuc_link;
4029 target_stack_t tuc_stack;
4030 target_sigregs tuc_mcontext;
4031 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4034 typedef struct {
4035 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4036 uint8_t retcode[S390_SYSCALL_SIZE];
4037 struct target_siginfo info;
4038 struct target_ucontext uc;
4039 } rt_sigframe;
4041 static inline abi_ulong
4042 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4044 abi_ulong sp;
4046 /* Default to using normal stack */
4047 sp = env->regs[15];
4049 /* This is the X/Open sanctioned signal stack switching. */
4050 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4051 if (!sas_ss_flags(sp)) {
4052 sp = target_sigaltstack_used.ss_sp +
4053 target_sigaltstack_used.ss_size;
4057 /* This is the legacy signal stack switching. */
4058 else if (/* FIXME !user_mode(regs) */ 0 &&
4059 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4060 ka->sa_restorer) {
4061 sp = (abi_ulong) ka->sa_restorer;
4064 return (sp - frame_size) & -8ul;
4067 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4069 int i;
4070 //save_access_regs(current->thread.acrs); FIXME
4072 /* Copy a 'clean' PSW mask to the user to avoid leaking
4073 information about whether PER is currently on. */
4074 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4075 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4076 for (i = 0; i < 16; i++) {
4077 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4079 for (i = 0; i < 16; i++) {
4080 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4083 * We have to store the fp registers to current->thread.fp_regs
4084 * to merge them with the emulated registers.
4086 //save_fp_regs(&current->thread.fp_regs); FIXME
4087 for (i = 0; i < 16; i++) {
4088 __put_user(get_freg(env, i)->ll, &sregs->fpregs.fprs[i]);
4092 static void setup_frame(int sig, struct target_sigaction *ka,
4093 target_sigset_t *set, CPUS390XState *env)
4095 sigframe *frame;
4096 abi_ulong frame_addr;
4098 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4099 trace_user_setup_frame(env, frame_addr);
4100 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4101 goto give_sigsegv;
4104 __put_user(set->sig[0], &frame->sc.oldmask[0]);
4106 save_sigregs(env, &frame->sregs);
4108 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4109 (abi_ulong *)&frame->sc.sregs);
4111 /* Set up to return from userspace. If provided, use a stub
4112 already in userspace. */
4113 if (ka->sa_flags & TARGET_SA_RESTORER) {
4114 env->regs[14] = (unsigned long)
4115 ka->sa_restorer | PSW_ADDR_AMODE;
4116 } else {
4117 env->regs[14] = (unsigned long)
4118 frame->retcode | PSW_ADDR_AMODE;
4119 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4120 (uint16_t *)(frame->retcode));
4123 /* Set up backchain. */
4124 __put_user(env->regs[15], (abi_ulong *) frame);
4126 /* Set up registers for signal handler */
4127 env->regs[15] = frame_addr;
4128 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4130 env->regs[2] = sig; //map_signal(sig);
4131 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4133 /* We forgot to include these in the sigcontext.
4134 To avoid breaking binary compatibility, they are passed as args. */
4135 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4136 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4138 /* Place signal number on stack to allow backtrace from handler. */
4139 __put_user(env->regs[2], (int *) &frame->signo);
4140 unlock_user_struct(frame, frame_addr, 1);
4141 return;
4143 give_sigsegv:
4144 force_sig(TARGET_SIGSEGV);
4147 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4148 target_siginfo_t *info,
4149 target_sigset_t *set, CPUS390XState *env)
4151 int i;
4152 rt_sigframe *frame;
4153 abi_ulong frame_addr;
4155 frame_addr = get_sigframe(ka, env, sizeof *frame);
4156 trace_user_setup_rt_frame(env, frame_addr);
4157 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4158 goto give_sigsegv;
4161 tswap_siginfo(&frame->info, info);
4163 /* Create the ucontext. */
4164 __put_user(0, &frame->uc.tuc_flags);
4165 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4166 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4167 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4168 &frame->uc.tuc_stack.ss_flags);
4169 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4170 save_sigregs(env, &frame->uc.tuc_mcontext);
4171 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4172 __put_user((abi_ulong)set->sig[i],
4173 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4176 /* Set up to return from userspace. If provided, use a stub
4177 already in userspace. */
4178 if (ka->sa_flags & TARGET_SA_RESTORER) {
4179 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4180 } else {
4181 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4182 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4183 (uint16_t *)(frame->retcode));
4186 /* Set up backchain. */
4187 __put_user(env->regs[15], (abi_ulong *) frame);
4189 /* Set up registers for signal handler */
4190 env->regs[15] = frame_addr;
4191 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4193 env->regs[2] = sig; //map_signal(sig);
4194 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4195 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4196 return;
4198 give_sigsegv:
4199 force_sig(TARGET_SIGSEGV);
4202 static int
4203 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4205 int err = 0;
4206 int i;
4208 for (i = 0; i < 16; i++) {
4209 __get_user(env->regs[i], &sc->regs.gprs[i]);
4212 __get_user(env->psw.mask, &sc->regs.psw.mask);
4213 trace_user_s390x_restore_sigregs(env, (unsigned long long)sc->regs.psw.addr,
4214 (unsigned long long)env->psw.addr);
4215 __get_user(env->psw.addr, &sc->regs.psw.addr);
4216 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4218 for (i = 0; i < 16; i++) {
4219 __get_user(env->aregs[i], &sc->regs.acrs[i]);
4221 for (i = 0; i < 16; i++) {
4222 __get_user(get_freg(env, i)->ll, &sc->fpregs.fprs[i]);
4225 return err;
4228 long do_sigreturn(CPUS390XState *env)
4230 sigframe *frame;
4231 abi_ulong frame_addr = env->regs[15];
4232 target_sigset_t target_set;
4233 sigset_t set;
4235 trace_user_do_sigreturn(env, frame_addr);
4236 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4237 goto badframe;
4239 __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
4241 target_to_host_sigset_internal(&set, &target_set);
4242 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4244 if (restore_sigregs(env, &frame->sregs)) {
4245 goto badframe;
4248 unlock_user_struct(frame, frame_addr, 0);
4249 return env->regs[2];
4251 badframe:
4252 force_sig(TARGET_SIGSEGV);
4253 return 0;
4256 long do_rt_sigreturn(CPUS390XState *env)
4258 rt_sigframe *frame;
4259 abi_ulong frame_addr = env->regs[15];
4260 sigset_t set;
4262 trace_user_do_rt_sigreturn(env, frame_addr);
4263 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4264 goto badframe;
4266 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4268 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4270 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4271 goto badframe;
4274 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4275 get_sp_from_cpustate(env)) == -EFAULT) {
4276 goto badframe;
4278 unlock_user_struct(frame, frame_addr, 0);
4279 return env->regs[2];
4281 badframe:
4282 unlock_user_struct(frame, frame_addr, 0);
4283 force_sig(TARGET_SIGSEGV);
4284 return 0;
4287 #elif defined(TARGET_PPC)
4289 /* Size of dummy stack frame allocated when calling signal handler.
4290 See arch/powerpc/include/asm/ptrace.h. */
4291 #if defined(TARGET_PPC64)
4292 #define SIGNAL_FRAMESIZE 128
4293 #else
4294 #define SIGNAL_FRAMESIZE 64
4295 #endif
4297 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4298 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4299 struct target_mcontext {
4300 target_ulong mc_gregs[48];
4301 /* Includes fpscr. */
4302 uint64_t mc_fregs[33];
4303 target_ulong mc_pad[2];
4304 /* We need to handle Altivec and SPE at the same time, which no
4305 kernel needs to do. Fortunately, the kernel defines this bit to
4306 be Altivec-register-large all the time, rather than trying to
4307 twiddle it based on the specific platform. */
4308 union {
4309 /* SPE vector registers. One extra for SPEFSCR. */
4310 uint32_t spe[33];
4311 /* Altivec vector registers. The packing of VSCR and VRSAVE
4312 varies depending on whether we're PPC64 or not: PPC64 splits
4313 them apart; PPC32 stuffs them together. */
4314 #if defined(TARGET_PPC64)
4315 #define QEMU_NVRREG 34
4316 #else
4317 #define QEMU_NVRREG 33
4318 #endif
4319 ppc_avr_t altivec[QEMU_NVRREG];
4320 #undef QEMU_NVRREG
4321 } mc_vregs __attribute__((__aligned__(16)));
4324 /* See arch/powerpc/include/asm/sigcontext.h. */
4325 struct target_sigcontext {
4326 target_ulong _unused[4];
4327 int32_t signal;
4328 #if defined(TARGET_PPC64)
4329 int32_t pad0;
4330 #endif
4331 target_ulong handler;
4332 target_ulong oldmask;
4333 target_ulong regs; /* struct pt_regs __user * */
4334 #if defined(TARGET_PPC64)
4335 struct target_mcontext mcontext;
4336 #endif
4339 /* Indices for target_mcontext.mc_gregs, below.
4340 See arch/powerpc/include/asm/ptrace.h for details. */
4341 enum {
4342 TARGET_PT_R0 = 0,
4343 TARGET_PT_R1 = 1,
4344 TARGET_PT_R2 = 2,
4345 TARGET_PT_R3 = 3,
4346 TARGET_PT_R4 = 4,
4347 TARGET_PT_R5 = 5,
4348 TARGET_PT_R6 = 6,
4349 TARGET_PT_R7 = 7,
4350 TARGET_PT_R8 = 8,
4351 TARGET_PT_R9 = 9,
4352 TARGET_PT_R10 = 10,
4353 TARGET_PT_R11 = 11,
4354 TARGET_PT_R12 = 12,
4355 TARGET_PT_R13 = 13,
4356 TARGET_PT_R14 = 14,
4357 TARGET_PT_R15 = 15,
4358 TARGET_PT_R16 = 16,
4359 TARGET_PT_R17 = 17,
4360 TARGET_PT_R18 = 18,
4361 TARGET_PT_R19 = 19,
4362 TARGET_PT_R20 = 20,
4363 TARGET_PT_R21 = 21,
4364 TARGET_PT_R22 = 22,
4365 TARGET_PT_R23 = 23,
4366 TARGET_PT_R24 = 24,
4367 TARGET_PT_R25 = 25,
4368 TARGET_PT_R26 = 26,
4369 TARGET_PT_R27 = 27,
4370 TARGET_PT_R28 = 28,
4371 TARGET_PT_R29 = 29,
4372 TARGET_PT_R30 = 30,
4373 TARGET_PT_R31 = 31,
4374 TARGET_PT_NIP = 32,
4375 TARGET_PT_MSR = 33,
4376 TARGET_PT_ORIG_R3 = 34,
4377 TARGET_PT_CTR = 35,
4378 TARGET_PT_LNK = 36,
4379 TARGET_PT_XER = 37,
4380 TARGET_PT_CCR = 38,
4381 /* Yes, there are two registers with #39. One is 64-bit only. */
4382 TARGET_PT_MQ = 39,
4383 TARGET_PT_SOFTE = 39,
4384 TARGET_PT_TRAP = 40,
4385 TARGET_PT_DAR = 41,
4386 TARGET_PT_DSISR = 42,
4387 TARGET_PT_RESULT = 43,
4388 TARGET_PT_REGS_COUNT = 44
4392 struct target_ucontext {
4393 target_ulong tuc_flags;
4394 target_ulong tuc_link; /* struct ucontext __user * */
4395 struct target_sigaltstack tuc_stack;
4396 #if !defined(TARGET_PPC64)
4397 int32_t tuc_pad[7];
4398 target_ulong tuc_regs; /* struct mcontext __user *
4399 points to uc_mcontext field */
4400 #endif
4401 target_sigset_t tuc_sigmask;
4402 #if defined(TARGET_PPC64)
4403 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4404 struct target_sigcontext tuc_sigcontext;
4405 #else
4406 int32_t tuc_maskext[30];
4407 int32_t tuc_pad2[3];
4408 struct target_mcontext tuc_mcontext;
4409 #endif
4412 /* See arch/powerpc/kernel/signal_32.c. */
4413 struct target_sigframe {
4414 struct target_sigcontext sctx;
4415 struct target_mcontext mctx;
4416 int32_t abigap[56];
4419 #if defined(TARGET_PPC64)
4421 #define TARGET_TRAMP_SIZE 6
4423 struct target_rt_sigframe {
4424 /* sys_rt_sigreturn requires the ucontext be the first field */
4425 struct target_ucontext uc;
4426 target_ulong _unused[2];
4427 uint32_t trampoline[TARGET_TRAMP_SIZE];
4428 target_ulong pinfo; /* struct siginfo __user * */
4429 target_ulong puc; /* void __user * */
4430 struct target_siginfo info;
4431 /* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
4432 char abigap[288];
4433 } __attribute__((aligned(16)));
4435 #else
4437 struct target_rt_sigframe {
4438 struct target_siginfo info;
4439 struct target_ucontext uc;
4440 int32_t abigap[56];
4443 #endif
4445 #if defined(TARGET_PPC64)
4447 struct target_func_ptr {
4448 target_ulong entry;
4449 target_ulong toc;
4452 #endif
4454 /* We use the mc_pad field for the signal return trampoline. */
4455 #define tramp mc_pad
4457 /* See arch/powerpc/kernel/signal.c. */
4458 static target_ulong get_sigframe(struct target_sigaction *ka,
4459 CPUPPCState *env,
4460 int frame_size)
4462 target_ulong oldsp, newsp;
4464 oldsp = env->gpr[1];
4466 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4467 (sas_ss_flags(oldsp) == 0)) {
4468 oldsp = (target_sigaltstack_used.ss_sp
4469 + target_sigaltstack_used.ss_size);
4472 newsp = (oldsp - frame_size) & ~0xFUL;
4474 return newsp;
4477 static void save_user_regs(CPUPPCState *env, struct target_mcontext *frame)
4479 target_ulong msr = env->msr;
4480 int i;
4481 target_ulong ccr = 0;
4483 /* In general, the kernel attempts to be intelligent about what it
4484 needs to save for Altivec/FP/SPE registers. We don't care that
4485 much, so we just go ahead and save everything. */
4487 /* Save general registers. */
4488 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4489 __put_user(env->gpr[i], &frame->mc_gregs[i]);
4491 __put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4492 __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4493 __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4494 __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4496 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4497 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4499 __put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4501 /* Save Altivec registers if necessary. */
4502 if (env->insns_flags & PPC_ALTIVEC) {
4503 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4504 ppc_avr_t *avr = &env->avr[i];
4505 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4507 __put_user(avr->u64[0], &vreg->u64[0]);
4508 __put_user(avr->u64[1], &vreg->u64[1]);
4510 /* Set MSR_VR in the saved MSR value to indicate that
4511 frame->mc_vregs contains valid data. */
4512 msr |= MSR_VR;
4513 __put_user((uint32_t)env->spr[SPR_VRSAVE],
4514 &frame->mc_vregs.altivec[32].u32[3]);
4517 /* Save floating point registers. */
4518 if (env->insns_flags & PPC_FLOAT) {
4519 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4520 __put_user(env->fpr[i], &frame->mc_fregs[i]);
4522 __put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]);
4525 /* Save SPE registers. The kernel only saves the high half. */
4526 if (env->insns_flags & PPC_SPE) {
4527 #if defined(TARGET_PPC64)
4528 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4529 __put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i]);
4531 #else
4532 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4533 __put_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4535 #endif
4536 /* Set MSR_SPE in the saved MSR value to indicate that
4537 frame->mc_vregs contains valid data. */
4538 msr |= MSR_SPE;
4539 __put_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4542 /* Store MSR. */
4543 __put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4546 static void encode_trampoline(int sigret, uint32_t *tramp)
4548 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4549 if (sigret) {
4550 __put_user(0x38000000 | sigret, &tramp[0]);
4551 __put_user(0x44000002, &tramp[1]);
4555 static void restore_user_regs(CPUPPCState *env,
4556 struct target_mcontext *frame, int sig)
4558 target_ulong save_r2 = 0;
4559 target_ulong msr;
4560 target_ulong ccr;
4562 int i;
4564 if (!sig) {
4565 save_r2 = env->gpr[2];
4568 /* Restore general registers. */
4569 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4570 __get_user(env->gpr[i], &frame->mc_gregs[i]);
4572 __get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4573 __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4574 __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4575 __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4576 __get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4578 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4579 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4582 if (!sig) {
4583 env->gpr[2] = save_r2;
4585 /* Restore MSR. */
4586 __get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4588 /* If doing signal return, restore the previous little-endian mode. */
4589 if (sig)
4590 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4592 /* Restore Altivec registers if necessary. */
4593 if (env->insns_flags & PPC_ALTIVEC) {
4594 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4595 ppc_avr_t *avr = &env->avr[i];
4596 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4598 __get_user(avr->u64[0], &vreg->u64[0]);
4599 __get_user(avr->u64[1], &vreg->u64[1]);
4601 /* Set MSR_VEC in the saved MSR value to indicate that
4602 frame->mc_vregs contains valid data. */
4603 __get_user(env->spr[SPR_VRSAVE],
4604 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3]));
4607 /* Restore floating point registers. */
4608 if (env->insns_flags & PPC_FLOAT) {
4609 uint64_t fpscr;
4610 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4611 __get_user(env->fpr[i], &frame->mc_fregs[i]);
4613 __get_user(fpscr, &frame->mc_fregs[32]);
4614 env->fpscr = (uint32_t) fpscr;
4617 /* Save SPE registers. The kernel only saves the high half. */
4618 if (env->insns_flags & PPC_SPE) {
4619 #if defined(TARGET_PPC64)
4620 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4621 uint32_t hi;
4623 __get_user(hi, &frame->mc_vregs.spe[i]);
4624 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4626 #else
4627 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4628 __get_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4630 #endif
4631 __get_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4635 static void setup_frame(int sig, struct target_sigaction *ka,
4636 target_sigset_t *set, CPUPPCState *env)
4638 struct target_sigframe *frame;
4639 struct target_sigcontext *sc;
4640 target_ulong frame_addr, newsp;
4641 int err = 0;
4642 #if defined(TARGET_PPC64)
4643 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4644 #endif
4646 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4647 trace_user_setup_frame(env, frame_addr);
4648 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4649 goto sigsegv;
4650 sc = &frame->sctx;
4652 __put_user(ka->_sa_handler, &sc->handler);
4653 __put_user(set->sig[0], &sc->oldmask);
4654 #if TARGET_ABI_BITS == 64
4655 __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4656 #else
4657 __put_user(set->sig[1], &sc->_unused[3]);
4658 #endif
4659 __put_user(h2g(&frame->mctx), &sc->regs);
4660 __put_user(sig, &sc->signal);
4662 /* Save user regs. */
4663 save_user_regs(env, &frame->mctx);
4665 /* Construct the trampoline code on the stack. */
4666 encode_trampoline(TARGET_NR_sigreturn, (uint32_t *)&frame->mctx.tramp);
4668 /* The kernel checks for the presence of a VDSO here. We don't
4669 emulate a vdso, so use a sigreturn system call. */
4670 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4672 /* Turn off all fp exceptions. */
4673 env->fpscr = 0;
4675 /* Create a stack frame for the caller of the handler. */
4676 newsp = frame_addr - SIGNAL_FRAMESIZE;
4677 err |= put_user(env->gpr[1], newsp, target_ulong);
4679 if (err)
4680 goto sigsegv;
4682 /* Set up registers for signal handler. */
4683 env->gpr[1] = newsp;
4684 env->gpr[3] = sig;
4685 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4687 #if defined(TARGET_PPC64)
4688 if (get_ppc64_abi(image) < 2) {
4689 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4690 struct target_func_ptr *handler =
4691 (struct target_func_ptr *)g2h(ka->_sa_handler);
4692 env->nip = tswapl(handler->entry);
4693 env->gpr[2] = tswapl(handler->toc);
4694 } else {
4695 /* ELFv2 PPC64 function pointers are entry points, but R12
4696 * must also be set */
4697 env->nip = tswapl((target_ulong) ka->_sa_handler);
4698 env->gpr[12] = env->nip;
4700 #else
4701 env->nip = (target_ulong) ka->_sa_handler;
4702 #endif
4704 /* Signal handlers are entered in big-endian mode. */
4705 env->msr &= ~MSR_LE;
4707 unlock_user_struct(frame, frame_addr, 1);
4708 return;
4710 sigsegv:
4711 unlock_user_struct(frame, frame_addr, 1);
4712 force_sig(TARGET_SIGSEGV);
4715 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4716 target_siginfo_t *info,
4717 target_sigset_t *set, CPUPPCState *env)
4719 struct target_rt_sigframe *rt_sf;
4720 uint32_t *trampptr = 0;
4721 struct target_mcontext *mctx = 0;
4722 target_ulong rt_sf_addr, newsp = 0;
4723 int i, err = 0;
4724 #if defined(TARGET_PPC64)
4725 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4726 #endif
4728 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4729 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4730 goto sigsegv;
4732 tswap_siginfo(&rt_sf->info, info);
4734 __put_user(0, &rt_sf->uc.tuc_flags);
4735 __put_user(0, &rt_sf->uc.tuc_link);
4736 __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4737 &rt_sf->uc.tuc_stack.ss_sp);
4738 __put_user(sas_ss_flags(env->gpr[1]),
4739 &rt_sf->uc.tuc_stack.ss_flags);
4740 __put_user(target_sigaltstack_used.ss_size,
4741 &rt_sf->uc.tuc_stack.ss_size);
4742 #if !defined(TARGET_PPC64)
4743 __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4744 &rt_sf->uc.tuc_regs);
4745 #endif
4746 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4747 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4750 #if defined(TARGET_PPC64)
4751 mctx = &rt_sf->uc.tuc_sigcontext.mcontext;
4752 trampptr = &rt_sf->trampoline[0];
4753 #else
4754 mctx = &rt_sf->uc.tuc_mcontext;
4755 trampptr = (uint32_t *)&rt_sf->uc.tuc_mcontext.tramp;
4756 #endif
4758 save_user_regs(env, mctx);
4759 encode_trampoline(TARGET_NR_rt_sigreturn, trampptr);
4761 /* The kernel checks for the presence of a VDSO here. We don't
4762 emulate a vdso, so use a sigreturn system call. */
4763 env->lr = (target_ulong) h2g(trampptr);
4765 /* Turn off all fp exceptions. */
4766 env->fpscr = 0;
4768 /* Create a stack frame for the caller of the handler. */
4769 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4770 err |= put_user(env->gpr[1], newsp, target_ulong);
4772 if (err)
4773 goto sigsegv;
4775 /* Set up registers for signal handler. */
4776 env->gpr[1] = newsp;
4777 env->gpr[3] = (target_ulong) sig;
4778 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4779 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4780 env->gpr[6] = (target_ulong) h2g(rt_sf);
4782 #if defined(TARGET_PPC64)
4783 if (get_ppc64_abi(image) < 2) {
4784 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4785 struct target_func_ptr *handler =
4786 (struct target_func_ptr *)g2h(ka->_sa_handler);
4787 env->nip = tswapl(handler->entry);
4788 env->gpr[2] = tswapl(handler->toc);
4789 } else {
4790 /* ELFv2 PPC64 function pointers are entry points, but R12
4791 * must also be set */
4792 env->nip = tswapl((target_ulong) ka->_sa_handler);
4793 env->gpr[12] = env->nip;
4795 #else
4796 env->nip = (target_ulong) ka->_sa_handler;
4797 #endif
4799 /* Signal handlers are entered in big-endian mode. */
4800 env->msr &= ~MSR_LE;
4802 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4803 return;
4805 sigsegv:
4806 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4807 force_sig(TARGET_SIGSEGV);
4811 long do_sigreturn(CPUPPCState *env)
4813 struct target_sigcontext *sc = NULL;
4814 struct target_mcontext *sr = NULL;
4815 target_ulong sr_addr = 0, sc_addr;
4816 sigset_t blocked;
4817 target_sigset_t set;
4819 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4820 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4821 goto sigsegv;
4823 #if defined(TARGET_PPC64)
4824 set.sig[0] = sc->oldmask + ((uint64_t)(sc->_unused[3]) << 32);
4825 #else
4826 __get_user(set.sig[0], &sc->oldmask);
4827 __get_user(set.sig[1], &sc->_unused[3]);
4828 #endif
4829 target_to_host_sigset_internal(&blocked, &set);
4830 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4832 __get_user(sr_addr, &sc->regs);
4833 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4834 goto sigsegv;
4835 restore_user_regs(env, sr, 1);
4837 unlock_user_struct(sr, sr_addr, 1);
4838 unlock_user_struct(sc, sc_addr, 1);
4839 return -TARGET_QEMU_ESIGRETURN;
4841 sigsegv:
4842 unlock_user_struct(sr, sr_addr, 1);
4843 unlock_user_struct(sc, sc_addr, 1);
4844 force_sig(TARGET_SIGSEGV);
4845 return 0;
4848 /* See arch/powerpc/kernel/signal_32.c. */
4849 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4851 struct target_mcontext *mcp;
4852 target_ulong mcp_addr;
4853 sigset_t blocked;
4854 target_sigset_t set;
4856 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4857 sizeof (set)))
4858 return 1;
4860 #if defined(TARGET_PPC64)
4861 mcp_addr = h2g(ucp) +
4862 offsetof(struct target_ucontext, tuc_sigcontext.mcontext);
4863 #else
4864 __get_user(mcp_addr, &ucp->tuc_regs);
4865 #endif
4867 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4868 return 1;
4870 target_to_host_sigset_internal(&blocked, &set);
4871 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4872 restore_user_regs(env, mcp, sig);
4874 unlock_user_struct(mcp, mcp_addr, 1);
4875 return 0;
4878 long do_rt_sigreturn(CPUPPCState *env)
4880 struct target_rt_sigframe *rt_sf = NULL;
4881 target_ulong rt_sf_addr;
4883 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4884 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4885 goto sigsegv;
4887 if (do_setcontext(&rt_sf->uc, env, 1))
4888 goto sigsegv;
4890 do_sigaltstack(rt_sf_addr
4891 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4892 0, env->gpr[1]);
4894 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4895 return -TARGET_QEMU_ESIGRETURN;
4897 sigsegv:
4898 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4899 force_sig(TARGET_SIGSEGV);
4900 return 0;
4903 #elif defined(TARGET_M68K)
4905 struct target_sigcontext {
4906 abi_ulong sc_mask;
4907 abi_ulong sc_usp;
4908 abi_ulong sc_d0;
4909 abi_ulong sc_d1;
4910 abi_ulong sc_a0;
4911 abi_ulong sc_a1;
4912 unsigned short sc_sr;
4913 abi_ulong sc_pc;
4916 struct target_sigframe
4918 abi_ulong pretcode;
4919 int sig;
4920 int code;
4921 abi_ulong psc;
4922 char retcode[8];
4923 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4924 struct target_sigcontext sc;
4927 typedef int target_greg_t;
4928 #define TARGET_NGREG 18
4929 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4931 typedef struct target_fpregset {
4932 int f_fpcntl[3];
4933 int f_fpregs[8*3];
4934 } target_fpregset_t;
4936 struct target_mcontext {
4937 int version;
4938 target_gregset_t gregs;
4939 target_fpregset_t fpregs;
4942 #define TARGET_MCONTEXT_VERSION 2
4944 struct target_ucontext {
4945 abi_ulong tuc_flags;
4946 abi_ulong tuc_link;
4947 target_stack_t tuc_stack;
4948 struct target_mcontext tuc_mcontext;
4949 abi_long tuc_filler[80];
4950 target_sigset_t tuc_sigmask;
4953 struct target_rt_sigframe
4955 abi_ulong pretcode;
4956 int sig;
4957 abi_ulong pinfo;
4958 abi_ulong puc;
4959 char retcode[8];
4960 struct target_siginfo info;
4961 struct target_ucontext uc;
4964 static void setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4965 abi_ulong mask)
4967 __put_user(mask, &sc->sc_mask);
4968 __put_user(env->aregs[7], &sc->sc_usp);
4969 __put_user(env->dregs[0], &sc->sc_d0);
4970 __put_user(env->dregs[1], &sc->sc_d1);
4971 __put_user(env->aregs[0], &sc->sc_a0);
4972 __put_user(env->aregs[1], &sc->sc_a1);
4973 __put_user(env->sr, &sc->sc_sr);
4974 __put_user(env->pc, &sc->sc_pc);
4977 static void
4978 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
4980 int temp;
4982 __get_user(env->aregs[7], &sc->sc_usp);
4983 __get_user(env->dregs[1], &sc->sc_d1);
4984 __get_user(env->aregs[0], &sc->sc_a0);
4985 __get_user(env->aregs[1], &sc->sc_a1);
4986 __get_user(env->pc, &sc->sc_pc);
4987 __get_user(temp, &sc->sc_sr);
4988 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4990 *pd0 = tswapl(sc->sc_d0);
4994 * Determine which stack to use..
4996 static inline abi_ulong
4997 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
4998 size_t frame_size)
5000 unsigned long sp;
5002 sp = regs->aregs[7];
5004 /* This is the X/Open sanctioned signal stack switching. */
5005 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5006 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5009 return ((sp - frame_size) & -8UL);
5012 static void setup_frame(int sig, struct target_sigaction *ka,
5013 target_sigset_t *set, CPUM68KState *env)
5015 struct target_sigframe *frame;
5016 abi_ulong frame_addr;
5017 abi_ulong retcode_addr;
5018 abi_ulong sc_addr;
5019 int i;
5021 frame_addr = get_sigframe(ka, env, sizeof *frame);
5022 trace_user_setup_frame(env, frame_addr);
5023 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5024 goto give_sigsegv;
5026 __put_user(sig, &frame->sig);
5028 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5029 __put_user(sc_addr, &frame->psc);
5031 setup_sigcontext(&frame->sc, env, set->sig[0]);
5033 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5034 __put_user(set->sig[i], &frame->extramask[i - 1]);
5037 /* Set up to return from userspace. */
5039 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5040 __put_user(retcode_addr, &frame->pretcode);
5042 /* moveq #,d0; trap #0 */
5044 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5045 (uint32_t *)(frame->retcode));
5047 /* Set up to return from userspace */
5049 env->aregs[7] = frame_addr;
5050 env->pc = ka->_sa_handler;
5052 unlock_user_struct(frame, frame_addr, 1);
5053 return;
5055 give_sigsegv:
5056 force_sig(TARGET_SIGSEGV);
5059 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5060 CPUM68KState *env)
5062 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5064 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5065 __put_user(env->dregs[0], &gregs[0]);
5066 __put_user(env->dregs[1], &gregs[1]);
5067 __put_user(env->dregs[2], &gregs[2]);
5068 __put_user(env->dregs[3], &gregs[3]);
5069 __put_user(env->dregs[4], &gregs[4]);
5070 __put_user(env->dregs[5], &gregs[5]);
5071 __put_user(env->dregs[6], &gregs[6]);
5072 __put_user(env->dregs[7], &gregs[7]);
5073 __put_user(env->aregs[0], &gregs[8]);
5074 __put_user(env->aregs[1], &gregs[9]);
5075 __put_user(env->aregs[2], &gregs[10]);
5076 __put_user(env->aregs[3], &gregs[11]);
5077 __put_user(env->aregs[4], &gregs[12]);
5078 __put_user(env->aregs[5], &gregs[13]);
5079 __put_user(env->aregs[6], &gregs[14]);
5080 __put_user(env->aregs[7], &gregs[15]);
5081 __put_user(env->pc, &gregs[16]);
5082 __put_user(env->sr, &gregs[17]);
5084 return 0;
5087 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5088 struct target_ucontext *uc,
5089 int *pd0)
5091 int temp;
5092 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5094 __get_user(temp, &uc->tuc_mcontext.version);
5095 if (temp != TARGET_MCONTEXT_VERSION)
5096 goto badframe;
5098 /* restore passed registers */
5099 __get_user(env->dregs[0], &gregs[0]);
5100 __get_user(env->dregs[1], &gregs[1]);
5101 __get_user(env->dregs[2], &gregs[2]);
5102 __get_user(env->dregs[3], &gregs[3]);
5103 __get_user(env->dregs[4], &gregs[4]);
5104 __get_user(env->dregs[5], &gregs[5]);
5105 __get_user(env->dregs[6], &gregs[6]);
5106 __get_user(env->dregs[7], &gregs[7]);
5107 __get_user(env->aregs[0], &gregs[8]);
5108 __get_user(env->aregs[1], &gregs[9]);
5109 __get_user(env->aregs[2], &gregs[10]);
5110 __get_user(env->aregs[3], &gregs[11]);
5111 __get_user(env->aregs[4], &gregs[12]);
5112 __get_user(env->aregs[5], &gregs[13]);
5113 __get_user(env->aregs[6], &gregs[14]);
5114 __get_user(env->aregs[7], &gregs[15]);
5115 __get_user(env->pc, &gregs[16]);
5116 __get_user(temp, &gregs[17]);
5117 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5119 *pd0 = env->dregs[0];
5120 return 0;
5122 badframe:
5123 return 1;
5126 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5127 target_siginfo_t *info,
5128 target_sigset_t *set, CPUM68KState *env)
5130 struct target_rt_sigframe *frame;
5131 abi_ulong frame_addr;
5132 abi_ulong retcode_addr;
5133 abi_ulong info_addr;
5134 abi_ulong uc_addr;
5135 int err = 0;
5136 int i;
5138 frame_addr = get_sigframe(ka, env, sizeof *frame);
5139 trace_user_setup_rt_frame(env, frame_addr);
5140 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5141 goto give_sigsegv;
5143 __put_user(sig, &frame->sig);
5145 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5146 __put_user(info_addr, &frame->pinfo);
5148 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5149 __put_user(uc_addr, &frame->puc);
5151 tswap_siginfo(&frame->info, info);
5153 /* Create the ucontext */
5155 __put_user(0, &frame->uc.tuc_flags);
5156 __put_user(0, &frame->uc.tuc_link);
5157 __put_user(target_sigaltstack_used.ss_sp,
5158 &frame->uc.tuc_stack.ss_sp);
5159 __put_user(sas_ss_flags(env->aregs[7]),
5160 &frame->uc.tuc_stack.ss_flags);
5161 __put_user(target_sigaltstack_used.ss_size,
5162 &frame->uc.tuc_stack.ss_size);
5163 err |= target_rt_setup_ucontext(&frame->uc, env);
5165 if (err)
5166 goto give_sigsegv;
5168 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5169 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5172 /* Set up to return from userspace. */
5174 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5175 __put_user(retcode_addr, &frame->pretcode);
5177 /* moveq #,d0; notb d0; trap #0 */
5179 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5180 (uint32_t *)(frame->retcode + 0));
5181 __put_user(0x4e40, (uint16_t *)(frame->retcode + 4));
5183 if (err)
5184 goto give_sigsegv;
5186 /* Set up to return from userspace */
5188 env->aregs[7] = frame_addr;
5189 env->pc = ka->_sa_handler;
5191 unlock_user_struct(frame, frame_addr, 1);
5192 return;
5194 give_sigsegv:
5195 unlock_user_struct(frame, frame_addr, 1);
5196 force_sig(TARGET_SIGSEGV);
5199 long do_sigreturn(CPUM68KState *env)
5201 struct target_sigframe *frame;
5202 abi_ulong frame_addr = env->aregs[7] - 4;
5203 target_sigset_t target_set;
5204 sigset_t set;
5205 int d0, i;
5207 trace_user_do_sigreturn(env, frame_addr);
5208 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5209 goto badframe;
5211 /* set blocked signals */
5213 __get_user(target_set.sig[0], &frame->sc.sc_mask);
5215 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5216 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
5219 target_to_host_sigset_internal(&set, &target_set);
5220 do_sigprocmask(SIG_SETMASK, &set, NULL);
5222 /* restore registers */
5224 restore_sigcontext(env, &frame->sc, &d0);
5226 unlock_user_struct(frame, frame_addr, 0);
5227 return d0;
5229 badframe:
5230 force_sig(TARGET_SIGSEGV);
5231 return 0;
5234 long do_rt_sigreturn(CPUM68KState *env)
5236 struct target_rt_sigframe *frame;
5237 abi_ulong frame_addr = env->aregs[7] - 4;
5238 target_sigset_t target_set;
5239 sigset_t set;
5240 int d0;
5242 trace_user_do_rt_sigreturn(env, frame_addr);
5243 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5244 goto badframe;
5246 target_to_host_sigset_internal(&set, &target_set);
5247 do_sigprocmask(SIG_SETMASK, &set, NULL);
5249 /* restore registers */
5251 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5252 goto badframe;
5254 if (do_sigaltstack(frame_addr +
5255 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5256 0, get_sp_from_cpustate(env)) == -EFAULT)
5257 goto badframe;
5259 unlock_user_struct(frame, frame_addr, 0);
5260 return d0;
5262 badframe:
5263 unlock_user_struct(frame, frame_addr, 0);
5264 force_sig(TARGET_SIGSEGV);
5265 return 0;
5268 #elif defined(TARGET_ALPHA)
5270 struct target_sigcontext {
5271 abi_long sc_onstack;
5272 abi_long sc_mask;
5273 abi_long sc_pc;
5274 abi_long sc_ps;
5275 abi_long sc_regs[32];
5276 abi_long sc_ownedfp;
5277 abi_long sc_fpregs[32];
5278 abi_ulong sc_fpcr;
5279 abi_ulong sc_fp_control;
5280 abi_ulong sc_reserved1;
5281 abi_ulong sc_reserved2;
5282 abi_ulong sc_ssize;
5283 abi_ulong sc_sbase;
5284 abi_ulong sc_traparg_a0;
5285 abi_ulong sc_traparg_a1;
5286 abi_ulong sc_traparg_a2;
5287 abi_ulong sc_fp_trap_pc;
5288 abi_ulong sc_fp_trigger_sum;
5289 abi_ulong sc_fp_trigger_inst;
5292 struct target_ucontext {
5293 abi_ulong tuc_flags;
5294 abi_ulong tuc_link;
5295 abi_ulong tuc_osf_sigmask;
5296 target_stack_t tuc_stack;
5297 struct target_sigcontext tuc_mcontext;
5298 target_sigset_t tuc_sigmask;
5301 struct target_sigframe {
5302 struct target_sigcontext sc;
5303 unsigned int retcode[3];
5306 struct target_rt_sigframe {
5307 target_siginfo_t info;
5308 struct target_ucontext uc;
5309 unsigned int retcode[3];
5312 #define INSN_MOV_R30_R16 0x47fe0410
5313 #define INSN_LDI_R0 0x201f0000
5314 #define INSN_CALLSYS 0x00000083
5316 static void setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5317 abi_ulong frame_addr, target_sigset_t *set)
5319 int i;
5321 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5322 __put_user(set->sig[0], &sc->sc_mask);
5323 __put_user(env->pc, &sc->sc_pc);
5324 __put_user(8, &sc->sc_ps);
5326 for (i = 0; i < 31; ++i) {
5327 __put_user(env->ir[i], &sc->sc_regs[i]);
5329 __put_user(0, &sc->sc_regs[31]);
5331 for (i = 0; i < 31; ++i) {
5332 __put_user(env->fir[i], &sc->sc_fpregs[i]);
5334 __put_user(0, &sc->sc_fpregs[31]);
5335 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5337 __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5338 __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5339 __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5342 static void restore_sigcontext(CPUAlphaState *env,
5343 struct target_sigcontext *sc)
5345 uint64_t fpcr;
5346 int i;
5348 __get_user(env->pc, &sc->sc_pc);
5350 for (i = 0; i < 31; ++i) {
5351 __get_user(env->ir[i], &sc->sc_regs[i]);
5353 for (i = 0; i < 31; ++i) {
5354 __get_user(env->fir[i], &sc->sc_fpregs[i]);
5357 __get_user(fpcr, &sc->sc_fpcr);
5358 cpu_alpha_store_fpcr(env, fpcr);
5361 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5362 CPUAlphaState *env,
5363 unsigned long framesize)
5365 abi_ulong sp = env->ir[IR_SP];
5367 /* This is the X/Open sanctioned signal stack switching. */
5368 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5369 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5371 return (sp - framesize) & -32;
5374 static void setup_frame(int sig, struct target_sigaction *ka,
5375 target_sigset_t *set, CPUAlphaState *env)
5377 abi_ulong frame_addr, r26;
5378 struct target_sigframe *frame;
5379 int err = 0;
5381 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5382 trace_user_setup_frame(env, frame_addr);
5383 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5384 goto give_sigsegv;
5387 setup_sigcontext(&frame->sc, env, frame_addr, set);
5389 if (ka->sa_restorer) {
5390 r26 = ka->sa_restorer;
5391 } else {
5392 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5393 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5394 &frame->retcode[1]);
5395 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5396 /* imb() */
5397 r26 = frame_addr;
5400 unlock_user_struct(frame, frame_addr, 1);
5402 if (err) {
5403 give_sigsegv:
5404 if (sig == TARGET_SIGSEGV) {
5405 ka->_sa_handler = TARGET_SIG_DFL;
5407 force_sig(TARGET_SIGSEGV);
5410 env->ir[IR_RA] = r26;
5411 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5412 env->ir[IR_A0] = sig;
5413 env->ir[IR_A1] = 0;
5414 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5415 env->ir[IR_SP] = frame_addr;
5418 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5419 target_siginfo_t *info,
5420 target_sigset_t *set, CPUAlphaState *env)
5422 abi_ulong frame_addr, r26;
5423 struct target_rt_sigframe *frame;
5424 int i, err = 0;
5426 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5427 trace_user_setup_rt_frame(env, frame_addr);
5428 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5429 goto give_sigsegv;
5432 tswap_siginfo(&frame->info, info);
5434 __put_user(0, &frame->uc.tuc_flags);
5435 __put_user(0, &frame->uc.tuc_link);
5436 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5437 __put_user(target_sigaltstack_used.ss_sp,
5438 &frame->uc.tuc_stack.ss_sp);
5439 __put_user(sas_ss_flags(env->ir[IR_SP]),
5440 &frame->uc.tuc_stack.ss_flags);
5441 __put_user(target_sigaltstack_used.ss_size,
5442 &frame->uc.tuc_stack.ss_size);
5443 setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5444 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5445 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5448 if (ka->sa_restorer) {
5449 r26 = ka->sa_restorer;
5450 } else {
5451 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5452 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5453 &frame->retcode[1]);
5454 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5455 /* imb(); */
5456 r26 = frame_addr;
5459 if (err) {
5460 give_sigsegv:
5461 if (sig == TARGET_SIGSEGV) {
5462 ka->_sa_handler = TARGET_SIG_DFL;
5464 force_sig(TARGET_SIGSEGV);
5467 env->ir[IR_RA] = r26;
5468 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5469 env->ir[IR_A0] = sig;
5470 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5471 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5472 env->ir[IR_SP] = frame_addr;
5475 long do_sigreturn(CPUAlphaState *env)
5477 struct target_sigcontext *sc;
5478 abi_ulong sc_addr = env->ir[IR_A0];
5479 target_sigset_t target_set;
5480 sigset_t set;
5482 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5483 goto badframe;
5486 target_sigemptyset(&target_set);
5487 __get_user(target_set.sig[0], &sc->sc_mask);
5489 target_to_host_sigset_internal(&set, &target_set);
5490 do_sigprocmask(SIG_SETMASK, &set, NULL);
5492 restore_sigcontext(env, sc);
5493 unlock_user_struct(sc, sc_addr, 0);
5494 return env->ir[IR_V0];
5496 badframe:
5497 force_sig(TARGET_SIGSEGV);
5500 long do_rt_sigreturn(CPUAlphaState *env)
5502 abi_ulong frame_addr = env->ir[IR_A0];
5503 struct target_rt_sigframe *frame;
5504 sigset_t set;
5506 trace_user_do_rt_sigreturn(env, frame_addr);
5507 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5508 goto badframe;
5510 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5511 do_sigprocmask(SIG_SETMASK, &set, NULL);
5513 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5514 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5515 uc.tuc_stack),
5516 0, env->ir[IR_SP]) == -EFAULT) {
5517 goto badframe;
5520 unlock_user_struct(frame, frame_addr, 0);
5521 return env->ir[IR_V0];
5524 badframe:
5525 unlock_user_struct(frame, frame_addr, 0);
5526 force_sig(TARGET_SIGSEGV);
5529 #elif defined(TARGET_TILEGX)
5531 struct target_sigcontext {
5532 union {
5533 /* General-purpose registers. */
5534 abi_ulong gregs[56];
5535 struct {
5536 abi_ulong __gregs[53];
5537 abi_ulong tp; /* Aliases gregs[TREG_TP]. */
5538 abi_ulong sp; /* Aliases gregs[TREG_SP]. */
5539 abi_ulong lr; /* Aliases gregs[TREG_LR]. */
5542 abi_ulong pc; /* Program counter. */
5543 abi_ulong ics; /* In Interrupt Critical Section? */
5544 abi_ulong faultnum; /* Fault number. */
5545 abi_ulong pad[5];
5548 struct target_ucontext {
5549 abi_ulong tuc_flags;
5550 abi_ulong tuc_link;
5551 target_stack_t tuc_stack;
5552 struct target_sigcontext tuc_mcontext;
5553 target_sigset_t tuc_sigmask; /* mask last for extensibility */
5556 struct target_rt_sigframe {
5557 unsigned char save_area[16]; /* caller save area */
5558 struct target_siginfo info;
5559 struct target_ucontext uc;
5562 static void setup_sigcontext(struct target_sigcontext *sc,
5563 CPUArchState *env, int signo)
5565 int i;
5567 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5568 __put_user(env->regs[i], &sc->gregs[i]);
5571 __put_user(env->pc, &sc->pc);
5572 __put_user(0, &sc->ics);
5573 __put_user(signo, &sc->faultnum);
5576 static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
5578 int i;
5580 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5581 __get_user(env->regs[i], &sc->gregs[i]);
5584 __get_user(env->pc, &sc->pc);
5587 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
5588 size_t frame_size)
5590 unsigned long sp = env->regs[TILEGX_R_SP];
5592 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
5593 return -1UL;
5596 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
5597 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5600 sp -= frame_size;
5601 sp &= -16UL;
5602 return sp;
5605 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5606 target_siginfo_t *info,
5607 target_sigset_t *set, CPUArchState *env)
5609 abi_ulong frame_addr;
5610 struct target_rt_sigframe *frame;
5611 unsigned long restorer;
5613 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5614 trace_user_setup_rt_frame(env, frame_addr);
5615 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5616 goto give_sigsegv;
5619 /* Always write at least the signal number for the stack backtracer. */
5620 if (ka->sa_flags & TARGET_SA_SIGINFO) {
5621 /* At sigreturn time, restore the callee-save registers too. */
5622 tswap_siginfo(&frame->info, info);
5623 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
5624 } else {
5625 __put_user(info->si_signo, &frame->info.si_signo);
5628 /* Create the ucontext. */
5629 __put_user(0, &frame->uc.tuc_flags);
5630 __put_user(0, &frame->uc.tuc_link);
5631 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
5632 __put_user(sas_ss_flags(env->regs[TILEGX_R_SP]),
5633 &frame->uc.tuc_stack.ss_flags);
5634 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
5635 setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
5637 restorer = (unsigned long) do_rt_sigreturn;
5638 if (ka->sa_flags & TARGET_SA_RESTORER) {
5639 restorer = (unsigned long) ka->sa_restorer;
5641 env->pc = (unsigned long) ka->_sa_handler;
5642 env->regs[TILEGX_R_SP] = (unsigned long) frame;
5643 env->regs[TILEGX_R_LR] = restorer;
5644 env->regs[0] = (unsigned long) sig;
5645 env->regs[1] = (unsigned long) &frame->info;
5646 env->regs[2] = (unsigned long) &frame->uc;
5647 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
5649 unlock_user_struct(frame, frame_addr, 1);
5650 return;
5652 give_sigsegv:
5653 if (sig == TARGET_SIGSEGV) {
5654 ka->_sa_handler = TARGET_SIG_DFL;
5656 force_sig(TARGET_SIGSEGV /* , current */);
5659 long do_rt_sigreturn(CPUTLGState *env)
5661 abi_ulong frame_addr = env->regs[TILEGX_R_SP];
5662 struct target_rt_sigframe *frame;
5663 sigset_t set;
5665 trace_user_do_rt_sigreturn(env, frame_addr);
5666 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5667 goto badframe;
5669 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5670 do_sigprocmask(SIG_SETMASK, &set, NULL);
5672 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5673 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5674 uc.tuc_stack),
5675 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
5676 goto badframe;
5679 unlock_user_struct(frame, frame_addr, 0);
5680 return env->regs[TILEGX_R_RE];
5683 badframe:
5684 unlock_user_struct(frame, frame_addr, 0);
5685 force_sig(TARGET_SIGSEGV);
5688 #else
5690 static void setup_frame(int sig, struct target_sigaction *ka,
5691 target_sigset_t *set, CPUArchState *env)
5693 fprintf(stderr, "setup_frame: not implemented\n");
5696 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5697 target_siginfo_t *info,
5698 target_sigset_t *set, CPUArchState *env)
5700 fprintf(stderr, "setup_rt_frame: not implemented\n");
5703 long do_sigreturn(CPUArchState *env)
5705 fprintf(stderr, "do_sigreturn: not implemented\n");
5706 return -TARGET_ENOSYS;
5709 long do_rt_sigreturn(CPUArchState *env)
5711 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5712 return -TARGET_ENOSYS;
5715 #endif
5717 void process_pending_signals(CPUArchState *cpu_env)
5719 CPUState *cpu = ENV_GET_CPU(cpu_env);
5720 int sig;
5721 abi_ulong handler;
5722 sigset_t set, old_set;
5723 target_sigset_t target_old_set;
5724 struct emulated_sigtable *k;
5725 struct target_sigaction *sa;
5726 struct sigqueue *q;
5727 TaskState *ts = cpu->opaque;
5729 if (!ts->signal_pending)
5730 return;
5732 /* FIXME: This is not threadsafe. */
5733 k = ts->sigtab;
5734 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5735 if (k->pending)
5736 goto handle_signal;
5737 k++;
5739 /* if no signal is pending, just return */
5740 ts->signal_pending = 0;
5741 return;
5743 handle_signal:
5744 trace_user_handle_signal(cpu_env, sig);
5745 /* dequeue signal */
5746 q = k->first;
5747 k->first = q->next;
5748 if (!k->first)
5749 k->pending = 0;
5751 sig = gdb_handlesig(cpu, sig);
5752 if (!sig) {
5753 sa = NULL;
5754 handler = TARGET_SIG_IGN;
5755 } else {
5756 sa = &sigact_table[sig - 1];
5757 handler = sa->_sa_handler;
5760 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5761 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5762 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5763 * because it got a real MMU fault), and treat as if default handler.
5765 handler = TARGET_SIG_DFL;
5768 if (handler == TARGET_SIG_DFL) {
5769 /* default handler : ignore some signal. The other are job control or fatal */
5770 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5771 kill(getpid(),SIGSTOP);
5772 } else if (sig != TARGET_SIGCHLD &&
5773 sig != TARGET_SIGURG &&
5774 sig != TARGET_SIGWINCH &&
5775 sig != TARGET_SIGCONT) {
5776 force_sig(sig);
5778 } else if (handler == TARGET_SIG_IGN) {
5779 /* ignore sig */
5780 } else if (handler == TARGET_SIG_ERR) {
5781 force_sig(sig);
5782 } else {
5783 /* compute the blocked signals during the handler execution */
5784 target_to_host_sigset(&set, &sa->sa_mask);
5785 /* SA_NODEFER indicates that the current signal should not be
5786 blocked during the handler */
5787 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5788 sigaddset(&set, target_to_host_signal(sig));
5790 /* block signals in the handler using Linux */
5791 do_sigprocmask(SIG_BLOCK, &set, &old_set);
5792 /* save the previous blocked signal state to restore it at the
5793 end of the signal execution (see do_sigreturn) */
5794 host_to_target_sigset_internal(&target_old_set, &old_set);
5796 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5797 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5799 CPUX86State *env = cpu_env;
5800 if (env->eflags & VM_MASK)
5801 save_v86_state(env);
5803 #endif
5804 /* prepare the stack frame of the virtual CPU */
5805 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \
5806 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
5807 /* These targets do not have traditional signals. */
5808 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5809 #else
5810 if (sa->sa_flags & TARGET_SA_SIGINFO)
5811 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5812 else
5813 setup_frame(sig, sa, &target_old_set, cpu_env);
5814 #endif
5815 if (sa->sa_flags & TARGET_SA_RESETHAND)
5816 sa->_sa_handler = TARGET_SIG_DFL;
5818 if (q != &k->info)
5819 free_sigqueue(cpu_env, q);