linux-user: Support for restarting system calls for tilegx targets
[qemu.git] / linux-user / signal.c
blobb4641dffb8d5f592a88c431ebfbbe1b3954ee8ed
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;
879 } else {
881 /* This is the legacy signal stack switching. */
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;
888 return (esp - frame_size) & -8ul;
891 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
892 static void setup_frame(int sig, struct target_sigaction *ka,
893 target_sigset_t *set, CPUX86State *env)
895 abi_ulong frame_addr;
896 struct sigframe *frame;
897 int i;
899 frame_addr = get_sigframe(ka, env, sizeof(*frame));
900 trace_user_setup_frame(env, frame_addr);
902 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
903 goto give_sigsegv;
905 __put_user(sig, &frame->sig);
907 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
908 frame_addr + offsetof(struct sigframe, fpstate));
910 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
911 __put_user(set->sig[i], &frame->extramask[i - 1]);
914 /* Set up to return from userspace. If provided, use a stub
915 already in userspace. */
916 if (ka->sa_flags & TARGET_SA_RESTORER) {
917 __put_user(ka->sa_restorer, &frame->pretcode);
918 } else {
919 uint16_t val16;
920 abi_ulong retcode_addr;
921 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
922 __put_user(retcode_addr, &frame->pretcode);
923 /* This is popl %eax ; movl $,%eax ; int $0x80 */
924 val16 = 0xb858;
925 __put_user(val16, (uint16_t *)(frame->retcode+0));
926 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
927 val16 = 0x80cd;
928 __put_user(val16, (uint16_t *)(frame->retcode+6));
932 /* Set up registers for signal handler */
933 env->regs[R_ESP] = frame_addr;
934 env->eip = ka->_sa_handler;
936 cpu_x86_load_seg(env, R_DS, __USER_DS);
937 cpu_x86_load_seg(env, R_ES, __USER_DS);
938 cpu_x86_load_seg(env, R_SS, __USER_DS);
939 cpu_x86_load_seg(env, R_CS, __USER_CS);
940 env->eflags &= ~TF_MASK;
942 unlock_user_struct(frame, frame_addr, 1);
944 return;
946 give_sigsegv:
947 if (sig == TARGET_SIGSEGV) {
948 ka->_sa_handler = TARGET_SIG_DFL;
950 force_sig(TARGET_SIGSEGV /* , current */);
953 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
954 static void setup_rt_frame(int sig, struct target_sigaction *ka,
955 target_siginfo_t *info,
956 target_sigset_t *set, CPUX86State *env)
958 abi_ulong frame_addr, addr;
959 struct rt_sigframe *frame;
960 int i;
962 frame_addr = get_sigframe(ka, env, sizeof(*frame));
963 trace_user_setup_rt_frame(env, frame_addr);
965 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
966 goto give_sigsegv;
968 __put_user(sig, &frame->sig);
969 addr = frame_addr + offsetof(struct rt_sigframe, info);
970 __put_user(addr, &frame->pinfo);
971 addr = frame_addr + offsetof(struct rt_sigframe, uc);
972 __put_user(addr, &frame->puc);
973 tswap_siginfo(&frame->info, info);
975 /* Create the ucontext. */
976 __put_user(0, &frame->uc.tuc_flags);
977 __put_user(0, &frame->uc.tuc_link);
978 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
979 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
980 &frame->uc.tuc_stack.ss_flags);
981 __put_user(target_sigaltstack_used.ss_size,
982 &frame->uc.tuc_stack.ss_size);
983 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env,
984 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate));
986 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
987 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
990 /* Set up to return from userspace. If provided, use a stub
991 already in userspace. */
992 if (ka->sa_flags & TARGET_SA_RESTORER) {
993 __put_user(ka->sa_restorer, &frame->pretcode);
994 } else {
995 uint16_t val16;
996 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
997 __put_user(addr, &frame->pretcode);
998 /* This is movl $,%eax ; int $0x80 */
999 __put_user(0xb8, (char *)(frame->retcode+0));
1000 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
1001 val16 = 0x80cd;
1002 __put_user(val16, (uint16_t *)(frame->retcode+5));
1005 /* Set up registers for signal handler */
1006 env->regs[R_ESP] = frame_addr;
1007 env->eip = ka->_sa_handler;
1009 cpu_x86_load_seg(env, R_DS, __USER_DS);
1010 cpu_x86_load_seg(env, R_ES, __USER_DS);
1011 cpu_x86_load_seg(env, R_SS, __USER_DS);
1012 cpu_x86_load_seg(env, R_CS, __USER_CS);
1013 env->eflags &= ~TF_MASK;
1015 unlock_user_struct(frame, frame_addr, 1);
1017 return;
1019 give_sigsegv:
1020 if (sig == TARGET_SIGSEGV) {
1021 ka->_sa_handler = TARGET_SIG_DFL;
1023 force_sig(TARGET_SIGSEGV /* , current */);
1026 static int
1027 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc)
1029 unsigned int err = 0;
1030 abi_ulong fpstate_addr;
1031 unsigned int tmpflags;
1033 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1034 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1035 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1036 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1038 env->regs[R_EDI] = tswapl(sc->edi);
1039 env->regs[R_ESI] = tswapl(sc->esi);
1040 env->regs[R_EBP] = tswapl(sc->ebp);
1041 env->regs[R_ESP] = tswapl(sc->esp);
1042 env->regs[R_EBX] = tswapl(sc->ebx);
1043 env->regs[R_EDX] = tswapl(sc->edx);
1044 env->regs[R_ECX] = tswapl(sc->ecx);
1045 env->regs[R_EAX] = tswapl(sc->eax);
1046 env->eip = tswapl(sc->eip);
1048 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1049 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1051 tmpflags = tswapl(sc->eflags);
1052 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1053 // regs->orig_eax = -1; /* disable syscall checks */
1055 fpstate_addr = tswapl(sc->fpstate);
1056 if (fpstate_addr != 0) {
1057 if (!access_ok(VERIFY_READ, fpstate_addr,
1058 sizeof(struct target_fpstate)))
1059 goto badframe;
1060 cpu_x86_frstor(env, fpstate_addr, 1);
1063 return err;
1064 badframe:
1065 return 1;
1068 long do_sigreturn(CPUX86State *env)
1070 struct sigframe *frame;
1071 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1072 target_sigset_t target_set;
1073 sigset_t set;
1074 int i;
1076 trace_user_do_sigreturn(env, frame_addr);
1077 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1078 goto badframe;
1079 /* set blocked signals */
1080 __get_user(target_set.sig[0], &frame->sc.oldmask);
1081 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1082 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
1085 target_to_host_sigset_internal(&set, &target_set);
1086 do_sigprocmask(SIG_SETMASK, &set, NULL);
1088 /* restore registers */
1089 if (restore_sigcontext(env, &frame->sc))
1090 goto badframe;
1091 unlock_user_struct(frame, frame_addr, 0);
1092 return -TARGET_QEMU_ESIGRETURN;
1094 badframe:
1095 unlock_user_struct(frame, frame_addr, 0);
1096 force_sig(TARGET_SIGSEGV);
1097 return 0;
1100 long do_rt_sigreturn(CPUX86State *env)
1102 abi_ulong frame_addr;
1103 struct rt_sigframe *frame;
1104 sigset_t set;
1106 frame_addr = env->regs[R_ESP] - 4;
1107 trace_user_do_rt_sigreturn(env, frame_addr);
1108 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1109 goto badframe;
1110 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1111 do_sigprocmask(SIG_SETMASK, &set, NULL);
1113 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
1114 goto badframe;
1117 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1118 get_sp_from_cpustate(env)) == -EFAULT) {
1119 goto badframe;
1122 unlock_user_struct(frame, frame_addr, 0);
1123 return -TARGET_QEMU_ESIGRETURN;
1125 badframe:
1126 unlock_user_struct(frame, frame_addr, 0);
1127 force_sig(TARGET_SIGSEGV);
1128 return 0;
1131 #elif defined(TARGET_AARCH64)
1133 struct target_sigcontext {
1134 uint64_t fault_address;
1135 /* AArch64 registers */
1136 uint64_t regs[31];
1137 uint64_t sp;
1138 uint64_t pc;
1139 uint64_t pstate;
1140 /* 4K reserved for FP/SIMD state and future expansion */
1141 char __reserved[4096] __attribute__((__aligned__(16)));
1144 struct target_ucontext {
1145 abi_ulong tuc_flags;
1146 abi_ulong tuc_link;
1147 target_stack_t tuc_stack;
1148 target_sigset_t tuc_sigmask;
1149 /* glibc uses a 1024-bit sigset_t */
1150 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1151 /* last for future expansion */
1152 struct target_sigcontext tuc_mcontext;
1156 * Header to be used at the beginning of structures extending the user
1157 * context. Such structures must be placed after the rt_sigframe on the stack
1158 * and be 16-byte aligned. The last structure must be a dummy one with the
1159 * magic and size set to 0.
1161 struct target_aarch64_ctx {
1162 uint32_t magic;
1163 uint32_t size;
1166 #define TARGET_FPSIMD_MAGIC 0x46508001
1168 struct target_fpsimd_context {
1169 struct target_aarch64_ctx head;
1170 uint32_t fpsr;
1171 uint32_t fpcr;
1172 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1176 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1177 * user space as it will change with the addition of new context. User space
1178 * should check the magic/size information.
1180 struct target_aux_context {
1181 struct target_fpsimd_context fpsimd;
1182 /* additional context to be added before "end" */
1183 struct target_aarch64_ctx end;
1186 struct target_rt_sigframe {
1187 struct target_siginfo info;
1188 struct target_ucontext uc;
1189 uint64_t fp;
1190 uint64_t lr;
1191 uint32_t tramp[2];
1194 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1195 CPUARMState *env, target_sigset_t *set)
1197 int i;
1198 struct target_aux_context *aux =
1199 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1201 /* set up the stack frame for unwinding */
1202 __put_user(env->xregs[29], &sf->fp);
1203 __put_user(env->xregs[30], &sf->lr);
1205 for (i = 0; i < 31; i++) {
1206 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1208 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1209 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
1210 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
1212 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
1214 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1215 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1218 for (i = 0; i < 32; i++) {
1219 #ifdef TARGET_WORDS_BIGENDIAN
1220 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1221 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1222 #else
1223 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1224 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1225 #endif
1227 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1228 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1229 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1230 __put_user(sizeof(struct target_fpsimd_context),
1231 &aux->fpsimd.head.size);
1233 /* set the "end" magic */
1234 __put_user(0, &aux->end.magic);
1235 __put_user(0, &aux->end.size);
1237 return 0;
1240 static int target_restore_sigframe(CPUARMState *env,
1241 struct target_rt_sigframe *sf)
1243 sigset_t set;
1244 int i;
1245 struct target_aux_context *aux =
1246 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1247 uint32_t magic, size, fpsr, fpcr;
1248 uint64_t pstate;
1250 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1251 do_sigprocmask(SIG_SETMASK, &set, NULL);
1253 for (i = 0; i < 31; i++) {
1254 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1257 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1258 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1259 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1260 pstate_write(env, pstate);
1262 __get_user(magic, &aux->fpsimd.head.magic);
1263 __get_user(size, &aux->fpsimd.head.size);
1265 if (magic != TARGET_FPSIMD_MAGIC
1266 || size != sizeof(struct target_fpsimd_context)) {
1267 return 1;
1270 for (i = 0; i < 32; i++) {
1271 #ifdef TARGET_WORDS_BIGENDIAN
1272 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1273 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1274 #else
1275 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1276 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1277 #endif
1279 __get_user(fpsr, &aux->fpsimd.fpsr);
1280 vfp_set_fpsr(env, fpsr);
1281 __get_user(fpcr, &aux->fpsimd.fpcr);
1282 vfp_set_fpcr(env, fpcr);
1284 return 0;
1287 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1289 abi_ulong sp;
1291 sp = env->xregs[31];
1294 * This is the X/Open sanctioned signal stack switching.
1296 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1297 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1300 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1302 return sp;
1305 static void target_setup_frame(int usig, struct target_sigaction *ka,
1306 target_siginfo_t *info, target_sigset_t *set,
1307 CPUARMState *env)
1309 struct target_rt_sigframe *frame;
1310 abi_ulong frame_addr, return_addr;
1312 frame_addr = get_sigframe(ka, env);
1313 trace_user_setup_frame(env, frame_addr);
1314 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1315 goto give_sigsegv;
1318 __put_user(0, &frame->uc.tuc_flags);
1319 __put_user(0, &frame->uc.tuc_link);
1321 __put_user(target_sigaltstack_used.ss_sp,
1322 &frame->uc.tuc_stack.ss_sp);
1323 __put_user(sas_ss_flags(env->xregs[31]),
1324 &frame->uc.tuc_stack.ss_flags);
1325 __put_user(target_sigaltstack_used.ss_size,
1326 &frame->uc.tuc_stack.ss_size);
1327 target_setup_sigframe(frame, env, set);
1328 if (ka->sa_flags & TARGET_SA_RESTORER) {
1329 return_addr = ka->sa_restorer;
1330 } else {
1331 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1332 __put_user(0xd2801168, &frame->tramp[0]);
1333 __put_user(0xd4000001, &frame->tramp[1]);
1334 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1336 env->xregs[0] = usig;
1337 env->xregs[31] = frame_addr;
1338 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1339 env->pc = ka->_sa_handler;
1340 env->xregs[30] = return_addr;
1341 if (info) {
1342 tswap_siginfo(&frame->info, info);
1343 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1344 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1347 unlock_user_struct(frame, frame_addr, 1);
1348 return;
1350 give_sigsegv:
1351 unlock_user_struct(frame, frame_addr, 1);
1352 force_sig(TARGET_SIGSEGV);
1355 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1356 target_siginfo_t *info, target_sigset_t *set,
1357 CPUARMState *env)
1359 target_setup_frame(sig, ka, info, set, env);
1362 static void setup_frame(int sig, struct target_sigaction *ka,
1363 target_sigset_t *set, CPUARMState *env)
1365 target_setup_frame(sig, ka, 0, set, env);
1368 long do_rt_sigreturn(CPUARMState *env)
1370 struct target_rt_sigframe *frame = NULL;
1371 abi_ulong frame_addr = env->xregs[31];
1373 trace_user_do_rt_sigreturn(env, frame_addr);
1374 if (frame_addr & 15) {
1375 goto badframe;
1378 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1379 goto badframe;
1382 if (target_restore_sigframe(env, frame)) {
1383 goto badframe;
1386 if (do_sigaltstack(frame_addr +
1387 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1388 0, get_sp_from_cpustate(env)) == -EFAULT) {
1389 goto badframe;
1392 unlock_user_struct(frame, frame_addr, 0);
1393 return -TARGET_QEMU_ESIGRETURN;
1395 badframe:
1396 unlock_user_struct(frame, frame_addr, 0);
1397 force_sig(TARGET_SIGSEGV);
1398 return 0;
1401 long do_sigreturn(CPUARMState *env)
1403 return do_rt_sigreturn(env);
1406 #elif defined(TARGET_ARM)
1408 struct target_sigcontext {
1409 abi_ulong trap_no;
1410 abi_ulong error_code;
1411 abi_ulong oldmask;
1412 abi_ulong arm_r0;
1413 abi_ulong arm_r1;
1414 abi_ulong arm_r2;
1415 abi_ulong arm_r3;
1416 abi_ulong arm_r4;
1417 abi_ulong arm_r5;
1418 abi_ulong arm_r6;
1419 abi_ulong arm_r7;
1420 abi_ulong arm_r8;
1421 abi_ulong arm_r9;
1422 abi_ulong arm_r10;
1423 abi_ulong arm_fp;
1424 abi_ulong arm_ip;
1425 abi_ulong arm_sp;
1426 abi_ulong arm_lr;
1427 abi_ulong arm_pc;
1428 abi_ulong arm_cpsr;
1429 abi_ulong fault_address;
1432 struct target_ucontext_v1 {
1433 abi_ulong tuc_flags;
1434 abi_ulong tuc_link;
1435 target_stack_t tuc_stack;
1436 struct target_sigcontext tuc_mcontext;
1437 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1440 struct target_ucontext_v2 {
1441 abi_ulong tuc_flags;
1442 abi_ulong tuc_link;
1443 target_stack_t tuc_stack;
1444 struct target_sigcontext tuc_mcontext;
1445 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1446 char __unused[128 - sizeof(target_sigset_t)];
1447 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1450 struct target_user_vfp {
1451 uint64_t fpregs[32];
1452 abi_ulong fpscr;
1455 struct target_user_vfp_exc {
1456 abi_ulong fpexc;
1457 abi_ulong fpinst;
1458 abi_ulong fpinst2;
1461 struct target_vfp_sigframe {
1462 abi_ulong magic;
1463 abi_ulong size;
1464 struct target_user_vfp ufp;
1465 struct target_user_vfp_exc ufp_exc;
1466 } __attribute__((__aligned__(8)));
1468 struct target_iwmmxt_sigframe {
1469 abi_ulong magic;
1470 abi_ulong size;
1471 uint64_t regs[16];
1472 /* Note that not all the coprocessor control registers are stored here */
1473 uint32_t wcssf;
1474 uint32_t wcasf;
1475 uint32_t wcgr0;
1476 uint32_t wcgr1;
1477 uint32_t wcgr2;
1478 uint32_t wcgr3;
1479 } __attribute__((__aligned__(8)));
1481 #define TARGET_VFP_MAGIC 0x56465001
1482 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1484 struct sigframe_v1
1486 struct target_sigcontext sc;
1487 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1488 abi_ulong retcode;
1491 struct sigframe_v2
1493 struct target_ucontext_v2 uc;
1494 abi_ulong retcode;
1497 struct rt_sigframe_v1
1499 abi_ulong pinfo;
1500 abi_ulong puc;
1501 struct target_siginfo info;
1502 struct target_ucontext_v1 uc;
1503 abi_ulong retcode;
1506 struct rt_sigframe_v2
1508 struct target_siginfo info;
1509 struct target_ucontext_v2 uc;
1510 abi_ulong retcode;
1513 #define TARGET_CONFIG_CPU_32 1
1516 * For ARM syscalls, we encode the syscall number into the instruction.
1518 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1519 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1522 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1523 * need two 16-bit instructions.
1525 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1526 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1528 static const abi_ulong retcodes[4] = {
1529 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1530 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1534 static inline int valid_user_regs(CPUARMState *regs)
1536 return 1;
1539 static void
1540 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1541 CPUARMState *env, abi_ulong mask)
1543 __put_user(env->regs[0], &sc->arm_r0);
1544 __put_user(env->regs[1], &sc->arm_r1);
1545 __put_user(env->regs[2], &sc->arm_r2);
1546 __put_user(env->regs[3], &sc->arm_r3);
1547 __put_user(env->regs[4], &sc->arm_r4);
1548 __put_user(env->regs[5], &sc->arm_r5);
1549 __put_user(env->regs[6], &sc->arm_r6);
1550 __put_user(env->regs[7], &sc->arm_r7);
1551 __put_user(env->regs[8], &sc->arm_r8);
1552 __put_user(env->regs[9], &sc->arm_r9);
1553 __put_user(env->regs[10], &sc->arm_r10);
1554 __put_user(env->regs[11], &sc->arm_fp);
1555 __put_user(env->regs[12], &sc->arm_ip);
1556 __put_user(env->regs[13], &sc->arm_sp);
1557 __put_user(env->regs[14], &sc->arm_lr);
1558 __put_user(env->regs[15], &sc->arm_pc);
1559 #ifdef TARGET_CONFIG_CPU_32
1560 __put_user(cpsr_read(env), &sc->arm_cpsr);
1561 #endif
1563 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1564 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1565 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1566 __put_user(mask, &sc->oldmask);
1569 static inline abi_ulong
1570 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1572 unsigned long sp = regs->regs[13];
1575 * This is the X/Open sanctioned signal stack switching.
1577 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1578 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1581 * ATPCS B01 mandates 8-byte alignment
1583 return (sp - framesize) & ~7;
1586 static void
1587 setup_return(CPUARMState *env, struct target_sigaction *ka,
1588 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1590 abi_ulong handler = ka->_sa_handler;
1591 abi_ulong retcode;
1592 int thumb = handler & 1;
1593 uint32_t cpsr = cpsr_read(env);
1595 cpsr &= ~CPSR_IT;
1596 if (thumb) {
1597 cpsr |= CPSR_T;
1598 } else {
1599 cpsr &= ~CPSR_T;
1602 if (ka->sa_flags & TARGET_SA_RESTORER) {
1603 retcode = ka->sa_restorer;
1604 } else {
1605 unsigned int idx = thumb;
1607 if (ka->sa_flags & TARGET_SA_SIGINFO) {
1608 idx += 2;
1611 __put_user(retcodes[idx], rc);
1613 retcode = rc_addr + thumb;
1616 env->regs[0] = usig;
1617 env->regs[13] = frame_addr;
1618 env->regs[14] = retcode;
1619 env->regs[15] = handler & (thumb ? ~1 : ~3);
1620 cpsr_write(env, cpsr, CPSR_IT | CPSR_T, CPSRWriteByInstr);
1623 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1625 int i;
1626 struct target_vfp_sigframe *vfpframe;
1627 vfpframe = (struct target_vfp_sigframe *)regspace;
1628 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1629 __put_user(sizeof(*vfpframe), &vfpframe->size);
1630 for (i = 0; i < 32; i++) {
1631 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1633 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1634 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1635 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1636 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1637 return (abi_ulong*)(vfpframe+1);
1640 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1641 CPUARMState *env)
1643 int i;
1644 struct target_iwmmxt_sigframe *iwmmxtframe;
1645 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1646 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1647 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1648 for (i = 0; i < 16; i++) {
1649 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1651 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1652 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1653 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1654 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1655 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1656 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1657 return (abi_ulong*)(iwmmxtframe+1);
1660 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1661 target_sigset_t *set, CPUARMState *env)
1663 struct target_sigaltstack stack;
1664 int i;
1665 abi_ulong *regspace;
1667 /* Clear all the bits of the ucontext we don't use. */
1668 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1670 memset(&stack, 0, sizeof(stack));
1671 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1672 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1673 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1674 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1676 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1677 /* Save coprocessor signal frame. */
1678 regspace = uc->tuc_regspace;
1679 if (arm_feature(env, ARM_FEATURE_VFP)) {
1680 regspace = setup_sigframe_v2_vfp(regspace, env);
1682 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1683 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1686 /* Write terminating magic word */
1687 __put_user(0, regspace);
1689 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1690 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1694 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1695 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1696 target_sigset_t *set, CPUARMState *regs)
1698 struct sigframe_v1 *frame;
1699 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1700 int i;
1702 trace_user_setup_frame(regs, frame_addr);
1703 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1704 return;
1707 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1709 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1710 __put_user(set->sig[i], &frame->extramask[i - 1]);
1713 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1714 frame_addr + offsetof(struct sigframe_v1, retcode));
1716 unlock_user_struct(frame, frame_addr, 1);
1719 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1720 target_sigset_t *set, CPUARMState *regs)
1722 struct sigframe_v2 *frame;
1723 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1725 trace_user_setup_frame(regs, frame_addr);
1726 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1727 return;
1730 setup_sigframe_v2(&frame->uc, set, regs);
1732 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1733 frame_addr + offsetof(struct sigframe_v2, retcode));
1735 unlock_user_struct(frame, frame_addr, 1);
1738 static void setup_frame(int usig, struct target_sigaction *ka,
1739 target_sigset_t *set, CPUARMState *regs)
1741 if (get_osversion() >= 0x020612) {
1742 setup_frame_v2(usig, ka, set, regs);
1743 } else {
1744 setup_frame_v1(usig, ka, set, regs);
1748 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1749 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1750 target_siginfo_t *info,
1751 target_sigset_t *set, CPUARMState *env)
1753 struct rt_sigframe_v1 *frame;
1754 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1755 struct target_sigaltstack stack;
1756 int i;
1757 abi_ulong info_addr, uc_addr;
1759 trace_user_setup_rt_frame(env, frame_addr);
1760 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1761 return /* 1 */;
1764 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1765 __put_user(info_addr, &frame->pinfo);
1766 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1767 __put_user(uc_addr, &frame->puc);
1768 tswap_siginfo(&frame->info, info);
1770 /* Clear all the bits of the ucontext we don't use. */
1771 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1773 memset(&stack, 0, sizeof(stack));
1774 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1775 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1776 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1777 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1779 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1780 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1781 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
1784 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1785 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1787 env->regs[1] = info_addr;
1788 env->regs[2] = uc_addr;
1790 unlock_user_struct(frame, frame_addr, 1);
1793 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1794 target_siginfo_t *info,
1795 target_sigset_t *set, CPUARMState *env)
1797 struct rt_sigframe_v2 *frame;
1798 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1799 abi_ulong info_addr, uc_addr;
1801 trace_user_setup_rt_frame(env, frame_addr);
1802 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1803 return /* 1 */;
1806 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1807 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1808 tswap_siginfo(&frame->info, info);
1810 setup_sigframe_v2(&frame->uc, set, env);
1812 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1813 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1815 env->regs[1] = info_addr;
1816 env->regs[2] = uc_addr;
1818 unlock_user_struct(frame, frame_addr, 1);
1821 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1822 target_siginfo_t *info,
1823 target_sigset_t *set, CPUARMState *env)
1825 if (get_osversion() >= 0x020612) {
1826 setup_rt_frame_v2(usig, ka, info, set, env);
1827 } else {
1828 setup_rt_frame_v1(usig, ka, info, set, env);
1832 static int
1833 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1835 int err = 0;
1836 uint32_t cpsr;
1838 __get_user(env->regs[0], &sc->arm_r0);
1839 __get_user(env->regs[1], &sc->arm_r1);
1840 __get_user(env->regs[2], &sc->arm_r2);
1841 __get_user(env->regs[3], &sc->arm_r3);
1842 __get_user(env->regs[4], &sc->arm_r4);
1843 __get_user(env->regs[5], &sc->arm_r5);
1844 __get_user(env->regs[6], &sc->arm_r6);
1845 __get_user(env->regs[7], &sc->arm_r7);
1846 __get_user(env->regs[8], &sc->arm_r8);
1847 __get_user(env->regs[9], &sc->arm_r9);
1848 __get_user(env->regs[10], &sc->arm_r10);
1849 __get_user(env->regs[11], &sc->arm_fp);
1850 __get_user(env->regs[12], &sc->arm_ip);
1851 __get_user(env->regs[13], &sc->arm_sp);
1852 __get_user(env->regs[14], &sc->arm_lr);
1853 __get_user(env->regs[15], &sc->arm_pc);
1854 #ifdef TARGET_CONFIG_CPU_32
1855 __get_user(cpsr, &sc->arm_cpsr);
1856 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC, CPSRWriteByInstr);
1857 #endif
1859 err |= !valid_user_regs(env);
1861 return err;
1864 static long do_sigreturn_v1(CPUARMState *env)
1866 abi_ulong frame_addr;
1867 struct sigframe_v1 *frame = NULL;
1868 target_sigset_t set;
1869 sigset_t host_set;
1870 int i;
1873 * Since we stacked the signal on a 64-bit boundary,
1874 * then 'sp' should be word aligned here. If it's
1875 * not, then the user is trying to mess with us.
1877 frame_addr = env->regs[13];
1878 trace_user_do_sigreturn(env, frame_addr);
1879 if (frame_addr & 7) {
1880 goto badframe;
1883 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1884 goto badframe;
1887 __get_user(set.sig[0], &frame->sc.oldmask);
1888 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1889 __get_user(set.sig[i], &frame->extramask[i - 1]);
1892 target_to_host_sigset_internal(&host_set, &set);
1893 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1895 if (restore_sigcontext(env, &frame->sc)) {
1896 goto badframe;
1899 #if 0
1900 /* Send SIGTRAP if we're single-stepping */
1901 if (ptrace_cancel_bpt(current))
1902 send_sig(SIGTRAP, current, 1);
1903 #endif
1904 unlock_user_struct(frame, frame_addr, 0);
1905 return -TARGET_QEMU_ESIGRETURN;
1907 badframe:
1908 force_sig(TARGET_SIGSEGV /* , current */);
1909 return 0;
1912 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1914 int i;
1915 abi_ulong magic, sz;
1916 uint32_t fpscr, fpexc;
1917 struct target_vfp_sigframe *vfpframe;
1918 vfpframe = (struct target_vfp_sigframe *)regspace;
1920 __get_user(magic, &vfpframe->magic);
1921 __get_user(sz, &vfpframe->size);
1922 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1923 return 0;
1925 for (i = 0; i < 32; i++) {
1926 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1928 __get_user(fpscr, &vfpframe->ufp.fpscr);
1929 vfp_set_fpscr(env, fpscr);
1930 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1931 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1932 * and the exception flag is cleared
1934 fpexc |= (1 << 30);
1935 fpexc &= ~((1 << 31) | (1 << 28));
1936 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1937 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1938 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1939 return (abi_ulong*)(vfpframe + 1);
1942 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1943 abi_ulong *regspace)
1945 int i;
1946 abi_ulong magic, sz;
1947 struct target_iwmmxt_sigframe *iwmmxtframe;
1948 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1950 __get_user(magic, &iwmmxtframe->magic);
1951 __get_user(sz, &iwmmxtframe->size);
1952 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1953 return 0;
1955 for (i = 0; i < 16; i++) {
1956 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1958 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1959 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1960 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1961 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1962 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1963 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1964 return (abi_ulong*)(iwmmxtframe + 1);
1967 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1968 struct target_ucontext_v2 *uc)
1970 sigset_t host_set;
1971 abi_ulong *regspace;
1973 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1974 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1976 if (restore_sigcontext(env, &uc->tuc_mcontext))
1977 return 1;
1979 /* Restore coprocessor signal frame */
1980 regspace = uc->tuc_regspace;
1981 if (arm_feature(env, ARM_FEATURE_VFP)) {
1982 regspace = restore_sigframe_v2_vfp(env, regspace);
1983 if (!regspace) {
1984 return 1;
1987 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1988 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1989 if (!regspace) {
1990 return 1;
1994 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1995 return 1;
1997 #if 0
1998 /* Send SIGTRAP if we're single-stepping */
1999 if (ptrace_cancel_bpt(current))
2000 send_sig(SIGTRAP, current, 1);
2001 #endif
2003 return 0;
2006 static long do_sigreturn_v2(CPUARMState *env)
2008 abi_ulong frame_addr;
2009 struct sigframe_v2 *frame = NULL;
2012 * Since we stacked the signal on a 64-bit boundary,
2013 * then 'sp' should be word aligned here. If it's
2014 * not, then the user is trying to mess with us.
2016 frame_addr = env->regs[13];
2017 trace_user_do_sigreturn(env, frame_addr);
2018 if (frame_addr & 7) {
2019 goto badframe;
2022 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2023 goto badframe;
2026 if (do_sigframe_return_v2(env, frame_addr, &frame->uc)) {
2027 goto badframe;
2030 unlock_user_struct(frame, frame_addr, 0);
2031 return -TARGET_QEMU_ESIGRETURN;
2033 badframe:
2034 unlock_user_struct(frame, frame_addr, 0);
2035 force_sig(TARGET_SIGSEGV /* , current */);
2036 return 0;
2039 long do_sigreturn(CPUARMState *env)
2041 if (get_osversion() >= 0x020612) {
2042 return do_sigreturn_v2(env);
2043 } else {
2044 return do_sigreturn_v1(env);
2048 static long do_rt_sigreturn_v1(CPUARMState *env)
2050 abi_ulong frame_addr;
2051 struct rt_sigframe_v1 *frame = NULL;
2052 sigset_t host_set;
2055 * Since we stacked the signal on a 64-bit boundary,
2056 * then 'sp' should be word aligned here. If it's
2057 * not, then the user is trying to mess with us.
2059 frame_addr = env->regs[13];
2060 trace_user_do_rt_sigreturn(env, frame_addr);
2061 if (frame_addr & 7) {
2062 goto badframe;
2065 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2066 goto badframe;
2069 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2070 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2072 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
2073 goto badframe;
2076 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2077 goto badframe;
2079 #if 0
2080 /* Send SIGTRAP if we're single-stepping */
2081 if (ptrace_cancel_bpt(current))
2082 send_sig(SIGTRAP, current, 1);
2083 #endif
2084 unlock_user_struct(frame, frame_addr, 0);
2085 return -TARGET_QEMU_ESIGRETURN;
2087 badframe:
2088 unlock_user_struct(frame, frame_addr, 0);
2089 force_sig(TARGET_SIGSEGV /* , current */);
2090 return 0;
2093 static long do_rt_sigreturn_v2(CPUARMState *env)
2095 abi_ulong frame_addr;
2096 struct rt_sigframe_v2 *frame = NULL;
2099 * Since we stacked the signal on a 64-bit boundary,
2100 * then 'sp' should be word aligned here. If it's
2101 * not, then the user is trying to mess with us.
2103 frame_addr = env->regs[13];
2104 trace_user_do_rt_sigreturn(env, frame_addr);
2105 if (frame_addr & 7) {
2106 goto badframe;
2109 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2110 goto badframe;
2113 if (do_sigframe_return_v2(env, frame_addr, &frame->uc)) {
2114 goto badframe;
2117 unlock_user_struct(frame, frame_addr, 0);
2118 return -TARGET_QEMU_ESIGRETURN;
2120 badframe:
2121 unlock_user_struct(frame, frame_addr, 0);
2122 force_sig(TARGET_SIGSEGV /* , current */);
2123 return 0;
2126 long do_rt_sigreturn(CPUARMState *env)
2128 if (get_osversion() >= 0x020612) {
2129 return do_rt_sigreturn_v2(env);
2130 } else {
2131 return do_rt_sigreturn_v1(env);
2135 #elif defined(TARGET_SPARC)
2137 #define __SUNOS_MAXWIN 31
2139 /* This is what SunOS does, so shall I. */
2140 struct target_sigcontext {
2141 abi_ulong sigc_onstack; /* state to restore */
2143 abi_ulong sigc_mask; /* sigmask to restore */
2144 abi_ulong sigc_sp; /* stack pointer */
2145 abi_ulong sigc_pc; /* program counter */
2146 abi_ulong sigc_npc; /* next program counter */
2147 abi_ulong sigc_psr; /* for condition codes etc */
2148 abi_ulong sigc_g1; /* User uses these two registers */
2149 abi_ulong sigc_o0; /* within the trampoline code. */
2151 /* Now comes information regarding the users window set
2152 * at the time of the signal.
2154 abi_ulong sigc_oswins; /* outstanding windows */
2156 /* stack ptrs for each regwin buf */
2157 char *sigc_spbuf[__SUNOS_MAXWIN];
2159 /* Windows to restore after signal */
2160 struct {
2161 abi_ulong locals[8];
2162 abi_ulong ins[8];
2163 } sigc_wbuf[__SUNOS_MAXWIN];
2165 /* A Sparc stack frame */
2166 struct sparc_stackf {
2167 abi_ulong locals[8];
2168 abi_ulong ins[8];
2169 /* It's simpler to treat fp and callers_pc as elements of ins[]
2170 * since we never need to access them ourselves.
2172 char *structptr;
2173 abi_ulong xargs[6];
2174 abi_ulong xxargs[1];
2177 typedef struct {
2178 struct {
2179 abi_ulong psr;
2180 abi_ulong pc;
2181 abi_ulong npc;
2182 abi_ulong y;
2183 abi_ulong u_regs[16]; /* globals and ins */
2184 } si_regs;
2185 int si_mask;
2186 } __siginfo_t;
2188 typedef struct {
2189 abi_ulong si_float_regs[32];
2190 unsigned long si_fsr;
2191 unsigned long si_fpqdepth;
2192 struct {
2193 unsigned long *insn_addr;
2194 unsigned long insn;
2195 } si_fpqueue [16];
2196 } qemu_siginfo_fpu_t;
2199 struct target_signal_frame {
2200 struct sparc_stackf ss;
2201 __siginfo_t info;
2202 abi_ulong fpu_save;
2203 abi_ulong insns[2] __attribute__ ((aligned (8)));
2204 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2205 abi_ulong extra_size; /* Should be 0 */
2206 qemu_siginfo_fpu_t fpu_state;
2208 struct target_rt_signal_frame {
2209 struct sparc_stackf ss;
2210 siginfo_t info;
2211 abi_ulong regs[20];
2212 sigset_t mask;
2213 abi_ulong fpu_save;
2214 unsigned int insns[2];
2215 stack_t stack;
2216 unsigned int extra_size; /* Should be 0 */
2217 qemu_siginfo_fpu_t fpu_state;
2220 #define UREG_O0 16
2221 #define UREG_O6 22
2222 #define UREG_I0 0
2223 #define UREG_I1 1
2224 #define UREG_I2 2
2225 #define UREG_I3 3
2226 #define UREG_I4 4
2227 #define UREG_I5 5
2228 #define UREG_I6 6
2229 #define UREG_I7 7
2230 #define UREG_L0 8
2231 #define UREG_FP UREG_I6
2232 #define UREG_SP UREG_O6
2234 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2235 CPUSPARCState *env,
2236 unsigned long framesize)
2238 abi_ulong sp;
2240 sp = env->regwptr[UREG_FP];
2242 /* This is the X/Open sanctioned signal stack switching. */
2243 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2244 if (!on_sig_stack(sp)
2245 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7)) {
2246 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2249 return sp - framesize;
2252 static int
2253 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2255 int err = 0, i;
2257 __put_user(env->psr, &si->si_regs.psr);
2258 __put_user(env->pc, &si->si_regs.pc);
2259 __put_user(env->npc, &si->si_regs.npc);
2260 __put_user(env->y, &si->si_regs.y);
2261 for (i=0; i < 8; i++) {
2262 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2264 for (i=0; i < 8; i++) {
2265 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2267 __put_user(mask, &si->si_mask);
2268 return err;
2271 #if 0
2272 static int
2273 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2274 CPUSPARCState *env, unsigned long mask)
2276 int err = 0;
2278 __put_user(mask, &sc->sigc_mask);
2279 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2280 __put_user(env->pc, &sc->sigc_pc);
2281 __put_user(env->npc, &sc->sigc_npc);
2282 __put_user(env->psr, &sc->sigc_psr);
2283 __put_user(env->gregs[1], &sc->sigc_g1);
2284 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2286 return err;
2288 #endif
2289 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2291 static void setup_frame(int sig, struct target_sigaction *ka,
2292 target_sigset_t *set, CPUSPARCState *env)
2294 abi_ulong sf_addr;
2295 struct target_signal_frame *sf;
2296 int sigframe_size, err, i;
2298 /* 1. Make sure everything is clean */
2299 //synchronize_user_stack();
2301 sigframe_size = NF_ALIGNEDSZ;
2302 sf_addr = get_sigframe(ka, env, sigframe_size);
2303 trace_user_setup_frame(env, sf_addr);
2305 sf = lock_user(VERIFY_WRITE, sf_addr,
2306 sizeof(struct target_signal_frame), 0);
2307 if (!sf) {
2308 goto sigsegv;
2310 #if 0
2311 if (invalid_frame_pointer(sf, sigframe_size))
2312 goto sigill_and_return;
2313 #endif
2314 /* 2. Save the current process state */
2315 err = setup___siginfo(&sf->info, env, set->sig[0]);
2316 __put_user(0, &sf->extra_size);
2318 //save_fpu_state(regs, &sf->fpu_state);
2319 //__put_user(&sf->fpu_state, &sf->fpu_save);
2321 __put_user(set->sig[0], &sf->info.si_mask);
2322 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2323 __put_user(set->sig[i + 1], &sf->extramask[i]);
2326 for (i = 0; i < 8; i++) {
2327 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2329 for (i = 0; i < 8; i++) {
2330 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2332 if (err)
2333 goto sigsegv;
2335 /* 3. signal handler back-trampoline and parameters */
2336 env->regwptr[UREG_FP] = sf_addr;
2337 env->regwptr[UREG_I0] = sig;
2338 env->regwptr[UREG_I1] = sf_addr +
2339 offsetof(struct target_signal_frame, info);
2340 env->regwptr[UREG_I2] = sf_addr +
2341 offsetof(struct target_signal_frame, info);
2343 /* 4. signal handler */
2344 env->pc = ka->_sa_handler;
2345 env->npc = (env->pc + 4);
2346 /* 5. return to kernel instructions */
2347 if (ka->sa_restorer) {
2348 env->regwptr[UREG_I7] = ka->sa_restorer;
2349 } else {
2350 uint32_t val32;
2352 env->regwptr[UREG_I7] = sf_addr +
2353 offsetof(struct target_signal_frame, insns) - 2 * 4;
2355 /* mov __NR_sigreturn, %g1 */
2356 val32 = 0x821020d8;
2357 __put_user(val32, &sf->insns[0]);
2359 /* t 0x10 */
2360 val32 = 0x91d02010;
2361 __put_user(val32, &sf->insns[1]);
2362 if (err)
2363 goto sigsegv;
2365 /* Flush instruction space. */
2366 // flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2367 // tb_flush(env);
2369 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2370 return;
2371 #if 0
2372 sigill_and_return:
2373 force_sig(TARGET_SIGILL);
2374 #endif
2375 sigsegv:
2376 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2377 force_sig(TARGET_SIGSEGV);
2380 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2381 target_siginfo_t *info,
2382 target_sigset_t *set, CPUSPARCState *env)
2384 fprintf(stderr, "setup_rt_frame: not implemented\n");
2387 long do_sigreturn(CPUSPARCState *env)
2389 abi_ulong sf_addr;
2390 struct target_signal_frame *sf;
2391 uint32_t up_psr, pc, npc;
2392 target_sigset_t set;
2393 sigset_t host_set;
2394 int err=0, i;
2396 sf_addr = env->regwptr[UREG_FP];
2397 trace_user_do_sigreturn(env, sf_addr);
2398 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
2399 goto segv_and_exit;
2402 /* 1. Make sure we are not getting garbage from the user */
2404 if (sf_addr & 3)
2405 goto segv_and_exit;
2407 __get_user(pc, &sf->info.si_regs.pc);
2408 __get_user(npc, &sf->info.si_regs.npc);
2410 if ((pc | npc) & 3) {
2411 goto segv_and_exit;
2414 /* 2. Restore the state */
2415 __get_user(up_psr, &sf->info.si_regs.psr);
2417 /* User can only change condition codes and FPU enabling in %psr. */
2418 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2419 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2421 env->pc = pc;
2422 env->npc = npc;
2423 __get_user(env->y, &sf->info.si_regs.y);
2424 for (i=0; i < 8; i++) {
2425 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2427 for (i=0; i < 8; i++) {
2428 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2431 /* FIXME: implement FPU save/restore:
2432 * __get_user(fpu_save, &sf->fpu_save);
2433 * if (fpu_save)
2434 * err |= restore_fpu_state(env, fpu_save);
2437 /* This is pretty much atomic, no amount locking would prevent
2438 * the races which exist anyways.
2440 __get_user(set.sig[0], &sf->info.si_mask);
2441 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2442 __get_user(set.sig[i], &sf->extramask[i - 1]);
2445 target_to_host_sigset_internal(&host_set, &set);
2446 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2448 if (err) {
2449 goto segv_and_exit;
2451 unlock_user_struct(sf, sf_addr, 0);
2452 return -TARGET_QEMU_ESIGRETURN;
2454 segv_and_exit:
2455 unlock_user_struct(sf, sf_addr, 0);
2456 force_sig(TARGET_SIGSEGV);
2459 long do_rt_sigreturn(CPUSPARCState *env)
2461 trace_user_do_rt_sigreturn(env, 0);
2462 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2463 return -TARGET_ENOSYS;
2466 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2467 #define MC_TSTATE 0
2468 #define MC_PC 1
2469 #define MC_NPC 2
2470 #define MC_Y 3
2471 #define MC_G1 4
2472 #define MC_G2 5
2473 #define MC_G3 6
2474 #define MC_G4 7
2475 #define MC_G5 8
2476 #define MC_G6 9
2477 #define MC_G7 10
2478 #define MC_O0 11
2479 #define MC_O1 12
2480 #define MC_O2 13
2481 #define MC_O3 14
2482 #define MC_O4 15
2483 #define MC_O5 16
2484 #define MC_O6 17
2485 #define MC_O7 18
2486 #define MC_NGREG 19
2488 typedef abi_ulong target_mc_greg_t;
2489 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2491 struct target_mc_fq {
2492 abi_ulong *mcfq_addr;
2493 uint32_t mcfq_insn;
2496 struct target_mc_fpu {
2497 union {
2498 uint32_t sregs[32];
2499 uint64_t dregs[32];
2500 //uint128_t qregs[16];
2501 } mcfpu_fregs;
2502 abi_ulong mcfpu_fsr;
2503 abi_ulong mcfpu_fprs;
2504 abi_ulong mcfpu_gsr;
2505 struct target_mc_fq *mcfpu_fq;
2506 unsigned char mcfpu_qcnt;
2507 unsigned char mcfpu_qentsz;
2508 unsigned char mcfpu_enab;
2510 typedef struct target_mc_fpu target_mc_fpu_t;
2512 typedef struct {
2513 target_mc_gregset_t mc_gregs;
2514 target_mc_greg_t mc_fp;
2515 target_mc_greg_t mc_i7;
2516 target_mc_fpu_t mc_fpregs;
2517 } target_mcontext_t;
2519 struct target_ucontext {
2520 struct target_ucontext *tuc_link;
2521 abi_ulong tuc_flags;
2522 target_sigset_t tuc_sigmask;
2523 target_mcontext_t tuc_mcontext;
2526 /* A V9 register window */
2527 struct target_reg_window {
2528 abi_ulong locals[8];
2529 abi_ulong ins[8];
2532 #define TARGET_STACK_BIAS 2047
2534 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2535 void sparc64_set_context(CPUSPARCState *env)
2537 abi_ulong ucp_addr;
2538 struct target_ucontext *ucp;
2539 target_mc_gregset_t *grp;
2540 abi_ulong pc, npc, tstate;
2541 abi_ulong fp, i7, w_addr;
2542 unsigned int i;
2544 ucp_addr = env->regwptr[UREG_I0];
2545 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
2546 goto do_sigsegv;
2548 grp = &ucp->tuc_mcontext.mc_gregs;
2549 __get_user(pc, &((*grp)[MC_PC]));
2550 __get_user(npc, &((*grp)[MC_NPC]));
2551 if ((pc | npc) & 3) {
2552 goto do_sigsegv;
2554 if (env->regwptr[UREG_I1]) {
2555 target_sigset_t target_set;
2556 sigset_t set;
2558 if (TARGET_NSIG_WORDS == 1) {
2559 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
2560 } else {
2561 abi_ulong *src, *dst;
2562 src = ucp->tuc_sigmask.sig;
2563 dst = target_set.sig;
2564 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2565 __get_user(*dst, src);
2568 target_to_host_sigset_internal(&set, &target_set);
2569 do_sigprocmask(SIG_SETMASK, &set, NULL);
2571 env->pc = pc;
2572 env->npc = npc;
2573 __get_user(env->y, &((*grp)[MC_Y]));
2574 __get_user(tstate, &((*grp)[MC_TSTATE]));
2575 env->asi = (tstate >> 24) & 0xff;
2576 cpu_put_ccr(env, tstate >> 32);
2577 cpu_put_cwp64(env, tstate & 0x1f);
2578 __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2579 __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2580 __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2581 __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2582 __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2583 __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2584 __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2585 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2586 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2587 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2588 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2589 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2590 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2591 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2592 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2594 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2595 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2597 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2598 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2599 abi_ulong) != 0) {
2600 goto do_sigsegv;
2602 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2603 abi_ulong) != 0) {
2604 goto do_sigsegv;
2606 /* FIXME this does not match how the kernel handles the FPU in
2607 * its sparc64_set_context implementation. In particular the FPU
2608 * is only restored if fenab is non-zero in:
2609 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2611 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2613 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2614 for (i = 0; i < 64; i++, src++) {
2615 if (i & 1) {
2616 __get_user(env->fpr[i/2].l.lower, src);
2617 } else {
2618 __get_user(env->fpr[i/2].l.upper, src);
2622 __get_user(env->fsr,
2623 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2624 __get_user(env->gsr,
2625 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2626 unlock_user_struct(ucp, ucp_addr, 0);
2627 return;
2628 do_sigsegv:
2629 unlock_user_struct(ucp, ucp_addr, 0);
2630 force_sig(TARGET_SIGSEGV);
2633 void sparc64_get_context(CPUSPARCState *env)
2635 abi_ulong ucp_addr;
2636 struct target_ucontext *ucp;
2637 target_mc_gregset_t *grp;
2638 target_mcontext_t *mcp;
2639 abi_ulong fp, i7, w_addr;
2640 int err;
2641 unsigned int i;
2642 target_sigset_t target_set;
2643 sigset_t set;
2645 ucp_addr = env->regwptr[UREG_I0];
2646 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
2647 goto do_sigsegv;
2650 mcp = &ucp->tuc_mcontext;
2651 grp = &mcp->mc_gregs;
2653 /* Skip over the trap instruction, first. */
2654 env->pc = env->npc;
2655 env->npc += 4;
2657 err = 0;
2659 do_sigprocmask(0, NULL, &set);
2660 host_to_target_sigset_internal(&target_set, &set);
2661 if (TARGET_NSIG_WORDS == 1) {
2662 __put_user(target_set.sig[0],
2663 (abi_ulong *)&ucp->tuc_sigmask);
2664 } else {
2665 abi_ulong *src, *dst;
2666 src = target_set.sig;
2667 dst = ucp->tuc_sigmask.sig;
2668 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2669 __put_user(*src, dst);
2671 if (err)
2672 goto do_sigsegv;
2675 /* XXX: tstate must be saved properly */
2676 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2677 __put_user(env->pc, &((*grp)[MC_PC]));
2678 __put_user(env->npc, &((*grp)[MC_NPC]));
2679 __put_user(env->y, &((*grp)[MC_Y]));
2680 __put_user(env->gregs[1], &((*grp)[MC_G1]));
2681 __put_user(env->gregs[2], &((*grp)[MC_G2]));
2682 __put_user(env->gregs[3], &((*grp)[MC_G3]));
2683 __put_user(env->gregs[4], &((*grp)[MC_G4]));
2684 __put_user(env->gregs[5], &((*grp)[MC_G5]));
2685 __put_user(env->gregs[6], &((*grp)[MC_G6]));
2686 __put_user(env->gregs[7], &((*grp)[MC_G7]));
2687 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2688 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2689 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2690 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2691 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2692 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2693 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2694 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2696 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2697 fp = i7 = 0;
2698 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2699 abi_ulong) != 0) {
2700 goto do_sigsegv;
2702 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2703 abi_ulong) != 0) {
2704 goto do_sigsegv;
2706 __put_user(fp, &(mcp->mc_fp));
2707 __put_user(i7, &(mcp->mc_i7));
2710 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2711 for (i = 0; i < 64; i++, dst++) {
2712 if (i & 1) {
2713 __put_user(env->fpr[i/2].l.lower, dst);
2714 } else {
2715 __put_user(env->fpr[i/2].l.upper, dst);
2719 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2720 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2721 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2723 if (err)
2724 goto do_sigsegv;
2725 unlock_user_struct(ucp, ucp_addr, 1);
2726 return;
2727 do_sigsegv:
2728 unlock_user_struct(ucp, ucp_addr, 1);
2729 force_sig(TARGET_SIGSEGV);
2731 #endif
2732 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2734 # if defined(TARGET_ABI_MIPSO32)
2735 struct target_sigcontext {
2736 uint32_t sc_regmask; /* Unused */
2737 uint32_t sc_status;
2738 uint64_t sc_pc;
2739 uint64_t sc_regs[32];
2740 uint64_t sc_fpregs[32];
2741 uint32_t sc_ownedfp; /* Unused */
2742 uint32_t sc_fpc_csr;
2743 uint32_t sc_fpc_eir; /* Unused */
2744 uint32_t sc_used_math;
2745 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2746 uint32_t pad0;
2747 uint64_t sc_mdhi;
2748 uint64_t sc_mdlo;
2749 target_ulong sc_hi1; /* Was sc_cause */
2750 target_ulong sc_lo1; /* Was sc_badvaddr */
2751 target_ulong sc_hi2; /* Was sc_sigset[4] */
2752 target_ulong sc_lo2;
2753 target_ulong sc_hi3;
2754 target_ulong sc_lo3;
2756 # else /* N32 || N64 */
2757 struct target_sigcontext {
2758 uint64_t sc_regs[32];
2759 uint64_t sc_fpregs[32];
2760 uint64_t sc_mdhi;
2761 uint64_t sc_hi1;
2762 uint64_t sc_hi2;
2763 uint64_t sc_hi3;
2764 uint64_t sc_mdlo;
2765 uint64_t sc_lo1;
2766 uint64_t sc_lo2;
2767 uint64_t sc_lo3;
2768 uint64_t sc_pc;
2769 uint32_t sc_fpc_csr;
2770 uint32_t sc_used_math;
2771 uint32_t sc_dsp;
2772 uint32_t sc_reserved;
2774 # endif /* O32 */
2776 struct sigframe {
2777 uint32_t sf_ass[4]; /* argument save space for o32 */
2778 uint32_t sf_code[2]; /* signal trampoline */
2779 struct target_sigcontext sf_sc;
2780 target_sigset_t sf_mask;
2783 struct target_ucontext {
2784 target_ulong tuc_flags;
2785 target_ulong tuc_link;
2786 target_stack_t tuc_stack;
2787 target_ulong pad0;
2788 struct target_sigcontext tuc_mcontext;
2789 target_sigset_t tuc_sigmask;
2792 struct target_rt_sigframe {
2793 uint32_t rs_ass[4]; /* argument save space for o32 */
2794 uint32_t rs_code[2]; /* signal trampoline */
2795 struct target_siginfo rs_info;
2796 struct target_ucontext rs_uc;
2799 /* Install trampoline to jump back from signal handler */
2800 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2802 int err = 0;
2805 * Set up the return code ...
2807 * li v0, __NR__foo_sigreturn
2808 * syscall
2811 __put_user(0x24020000 + syscall, tramp + 0);
2812 __put_user(0x0000000c , tramp + 1);
2813 return err;
2816 static inline void setup_sigcontext(CPUMIPSState *regs,
2817 struct target_sigcontext *sc)
2819 int i;
2821 __put_user(exception_resume_pc(regs), &sc->sc_pc);
2822 regs->hflags &= ~MIPS_HFLAG_BMASK;
2824 __put_user(0, &sc->sc_regs[0]);
2825 for (i = 1; i < 32; ++i) {
2826 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2829 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2830 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2832 /* Rather than checking for dsp existence, always copy. The storage
2833 would just be garbage otherwise. */
2834 __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2835 __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2836 __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2837 __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2838 __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2839 __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2841 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2842 __put_user(dsp, &sc->sc_dsp);
2845 __put_user(1, &sc->sc_used_math);
2847 for (i = 0; i < 32; ++i) {
2848 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2852 static inline void
2853 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2855 int i;
2857 __get_user(regs->CP0_EPC, &sc->sc_pc);
2859 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2860 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2862 for (i = 1; i < 32; ++i) {
2863 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2866 __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2867 __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2868 __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2869 __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2870 __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2871 __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2873 uint32_t dsp;
2874 __get_user(dsp, &sc->sc_dsp);
2875 cpu_wrdsp(dsp, 0x3ff, regs);
2878 for (i = 0; i < 32; ++i) {
2879 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2884 * Determine which stack to use..
2886 static inline abi_ulong
2887 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2889 unsigned long sp;
2891 /* Default to using normal stack */
2892 sp = regs->active_tc.gpr[29];
2895 * FPU emulator may have its own trampoline active just
2896 * above the user stack, 16-bytes before the next lowest
2897 * 16 byte boundary. Try to avoid trashing it.
2899 sp -= 32;
2901 /* This is the X/Open sanctioned signal stack switching. */
2902 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2903 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2906 return (sp - frame_size) & ~7;
2909 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2911 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2912 env->hflags &= ~MIPS_HFLAG_M16;
2913 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2914 env->active_tc.PC &= ~(target_ulong) 1;
2918 # if defined(TARGET_ABI_MIPSO32)
2919 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2920 static void setup_frame(int sig, struct target_sigaction * ka,
2921 target_sigset_t *set, CPUMIPSState *regs)
2923 struct sigframe *frame;
2924 abi_ulong frame_addr;
2925 int i;
2927 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2928 trace_user_setup_frame(regs, frame_addr);
2929 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
2930 goto give_sigsegv;
2933 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2935 setup_sigcontext(regs, &frame->sf_sc);
2937 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2938 __put_user(set->sig[i], &frame->sf_mask.sig[i]);
2942 * Arguments to signal handler:
2944 * a0 = signal number
2945 * a1 = 0 (should be cause)
2946 * a2 = pointer to struct sigcontext
2948 * $25 and PC point to the signal handler, $29 points to the
2949 * struct sigframe.
2951 regs->active_tc.gpr[ 4] = sig;
2952 regs->active_tc.gpr[ 5] = 0;
2953 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2954 regs->active_tc.gpr[29] = frame_addr;
2955 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2956 /* The original kernel code sets CP0_EPC to the handler
2957 * since it returns to userland using eret
2958 * we cannot do this here, and we must set PC directly */
2959 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2960 mips_set_hflags_isa_mode_from_pc(regs);
2961 unlock_user_struct(frame, frame_addr, 1);
2962 return;
2964 give_sigsegv:
2965 force_sig(TARGET_SIGSEGV/*, current*/);
2968 long do_sigreturn(CPUMIPSState *regs)
2970 struct sigframe *frame;
2971 abi_ulong frame_addr;
2972 sigset_t blocked;
2973 target_sigset_t target_set;
2974 int i;
2976 frame_addr = regs->active_tc.gpr[29];
2977 trace_user_do_sigreturn(regs, frame_addr);
2978 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2979 goto badframe;
2981 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2982 __get_user(target_set.sig[i], &frame->sf_mask.sig[i]);
2985 target_to_host_sigset_internal(&blocked, &target_set);
2986 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
2988 restore_sigcontext(regs, &frame->sf_sc);
2990 #if 0
2992 * Don't let your children do this ...
2994 __asm__ __volatile__(
2995 "move\t$29, %0\n\t"
2996 "j\tsyscall_exit"
2997 :/* no outputs */
2998 :"r" (&regs));
2999 /* Unreached */
3000 #endif
3002 regs->active_tc.PC = regs->CP0_EPC;
3003 mips_set_hflags_isa_mode_from_pc(regs);
3004 /* I am not sure this is right, but it seems to work
3005 * maybe a problem with nested signals ? */
3006 regs->CP0_EPC = 0;
3007 return -TARGET_QEMU_ESIGRETURN;
3009 badframe:
3010 force_sig(TARGET_SIGSEGV/*, current*/);
3011 return 0;
3013 # endif /* O32 */
3015 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3016 target_siginfo_t *info,
3017 target_sigset_t *set, CPUMIPSState *env)
3019 struct target_rt_sigframe *frame;
3020 abi_ulong frame_addr;
3021 int i;
3023 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3024 trace_user_setup_rt_frame(env, frame_addr);
3025 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3026 goto give_sigsegv;
3029 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3031 tswap_siginfo(&frame->rs_info, info);
3033 __put_user(0, &frame->rs_uc.tuc_flags);
3034 __put_user(0, &frame->rs_uc.tuc_link);
3035 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3036 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3037 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3038 &frame->rs_uc.tuc_stack.ss_flags);
3040 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3042 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3043 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3047 * Arguments to signal handler:
3049 * a0 = signal number
3050 * a1 = pointer to siginfo_t
3051 * a2 = pointer to struct ucontext
3053 * $25 and PC point to the signal handler, $29 points to the
3054 * struct sigframe.
3056 env->active_tc.gpr[ 4] = sig;
3057 env->active_tc.gpr[ 5] = frame_addr
3058 + offsetof(struct target_rt_sigframe, rs_info);
3059 env->active_tc.gpr[ 6] = frame_addr
3060 + offsetof(struct target_rt_sigframe, rs_uc);
3061 env->active_tc.gpr[29] = frame_addr;
3062 env->active_tc.gpr[31] = frame_addr
3063 + offsetof(struct target_rt_sigframe, rs_code);
3064 /* The original kernel code sets CP0_EPC to the handler
3065 * since it returns to userland using eret
3066 * we cannot do this here, and we must set PC directly */
3067 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3068 mips_set_hflags_isa_mode_from_pc(env);
3069 unlock_user_struct(frame, frame_addr, 1);
3070 return;
3072 give_sigsegv:
3073 unlock_user_struct(frame, frame_addr, 1);
3074 force_sig(TARGET_SIGSEGV/*, current*/);
3077 long do_rt_sigreturn(CPUMIPSState *env)
3079 struct target_rt_sigframe *frame;
3080 abi_ulong frame_addr;
3081 sigset_t blocked;
3083 frame_addr = env->active_tc.gpr[29];
3084 trace_user_do_rt_sigreturn(env, frame_addr);
3085 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3086 goto badframe;
3089 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3090 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3092 restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3094 if (do_sigaltstack(frame_addr +
3095 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3096 0, get_sp_from_cpustate(env)) == -EFAULT)
3097 goto badframe;
3099 env->active_tc.PC = env->CP0_EPC;
3100 mips_set_hflags_isa_mode_from_pc(env);
3101 /* I am not sure this is right, but it seems to work
3102 * maybe a problem with nested signals ? */
3103 env->CP0_EPC = 0;
3104 return -TARGET_QEMU_ESIGRETURN;
3106 badframe:
3107 force_sig(TARGET_SIGSEGV/*, current*/);
3108 return 0;
3111 #elif defined(TARGET_SH4)
3114 * code and data structures from linux kernel:
3115 * include/asm-sh/sigcontext.h
3116 * arch/sh/kernel/signal.c
3119 struct target_sigcontext {
3120 target_ulong oldmask;
3122 /* CPU registers */
3123 target_ulong sc_gregs[16];
3124 target_ulong sc_pc;
3125 target_ulong sc_pr;
3126 target_ulong sc_sr;
3127 target_ulong sc_gbr;
3128 target_ulong sc_mach;
3129 target_ulong sc_macl;
3131 /* FPU registers */
3132 target_ulong sc_fpregs[16];
3133 target_ulong sc_xfpregs[16];
3134 unsigned int sc_fpscr;
3135 unsigned int sc_fpul;
3136 unsigned int sc_ownedfp;
3139 struct target_sigframe
3141 struct target_sigcontext sc;
3142 target_ulong extramask[TARGET_NSIG_WORDS-1];
3143 uint16_t retcode[3];
3147 struct target_ucontext {
3148 target_ulong tuc_flags;
3149 struct target_ucontext *tuc_link;
3150 target_stack_t tuc_stack;
3151 struct target_sigcontext tuc_mcontext;
3152 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3155 struct target_rt_sigframe
3157 struct target_siginfo info;
3158 struct target_ucontext uc;
3159 uint16_t retcode[3];
3163 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3164 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3166 static abi_ulong get_sigframe(struct target_sigaction *ka,
3167 unsigned long sp, size_t frame_size)
3169 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3170 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3173 return (sp - frame_size) & -8ul;
3176 static void setup_sigcontext(struct target_sigcontext *sc,
3177 CPUSH4State *regs, unsigned long mask)
3179 int i;
3181 #define COPY(x) __put_user(regs->x, &sc->sc_##x)
3182 COPY(gregs[0]); COPY(gregs[1]);
3183 COPY(gregs[2]); COPY(gregs[3]);
3184 COPY(gregs[4]); COPY(gregs[5]);
3185 COPY(gregs[6]); COPY(gregs[7]);
3186 COPY(gregs[8]); COPY(gregs[9]);
3187 COPY(gregs[10]); COPY(gregs[11]);
3188 COPY(gregs[12]); COPY(gregs[13]);
3189 COPY(gregs[14]); COPY(gregs[15]);
3190 COPY(gbr); COPY(mach);
3191 COPY(macl); COPY(pr);
3192 COPY(sr); COPY(pc);
3193 #undef COPY
3195 for (i=0; i<16; i++) {
3196 __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3198 __put_user(regs->fpscr, &sc->sc_fpscr);
3199 __put_user(regs->fpul, &sc->sc_fpul);
3201 /* non-iBCS2 extensions.. */
3202 __put_user(mask, &sc->oldmask);
3205 static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc)
3207 int i;
3209 #define COPY(x) __get_user(regs->x, &sc->sc_##x)
3210 COPY(gregs[0]); COPY(gregs[1]);
3211 COPY(gregs[2]); COPY(gregs[3]);
3212 COPY(gregs[4]); COPY(gregs[5]);
3213 COPY(gregs[6]); COPY(gregs[7]);
3214 COPY(gregs[8]); COPY(gregs[9]);
3215 COPY(gregs[10]); COPY(gregs[11]);
3216 COPY(gregs[12]); COPY(gregs[13]);
3217 COPY(gregs[14]); COPY(gregs[15]);
3218 COPY(gbr); COPY(mach);
3219 COPY(macl); COPY(pr);
3220 COPY(sr); COPY(pc);
3221 #undef COPY
3223 for (i=0; i<16; i++) {
3224 __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3226 __get_user(regs->fpscr, &sc->sc_fpscr);
3227 __get_user(regs->fpul, &sc->sc_fpul);
3229 regs->tra = -1; /* disable syscall checks */
3232 static void setup_frame(int sig, struct target_sigaction *ka,
3233 target_sigset_t *set, CPUSH4State *regs)
3235 struct target_sigframe *frame;
3236 abi_ulong frame_addr;
3237 int i;
3239 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3240 trace_user_setup_frame(regs, frame_addr);
3241 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3242 goto give_sigsegv;
3245 setup_sigcontext(&frame->sc, regs, set->sig[0]);
3247 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3248 __put_user(set->sig[i + 1], &frame->extramask[i]);
3251 /* Set up to return from userspace. If provided, use a stub
3252 already in userspace. */
3253 if (ka->sa_flags & TARGET_SA_RESTORER) {
3254 regs->pr = (unsigned long) ka->sa_restorer;
3255 } else {
3256 /* Generate return code (system call to sigreturn) */
3257 abi_ulong retcode_addr = frame_addr +
3258 offsetof(struct target_sigframe, retcode);
3259 __put_user(MOVW(2), &frame->retcode[0]);
3260 __put_user(TRAP_NOARG, &frame->retcode[1]);
3261 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3262 regs->pr = (unsigned long) retcode_addr;
3265 /* Set up registers for signal handler */
3266 regs->gregs[15] = frame_addr;
3267 regs->gregs[4] = sig; /* Arg for signal handler */
3268 regs->gregs[5] = 0;
3269 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3270 regs->pc = (unsigned long) ka->_sa_handler;
3272 unlock_user_struct(frame, frame_addr, 1);
3273 return;
3275 give_sigsegv:
3276 unlock_user_struct(frame, frame_addr, 1);
3277 force_sig(TARGET_SIGSEGV);
3280 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3281 target_siginfo_t *info,
3282 target_sigset_t *set, CPUSH4State *regs)
3284 struct target_rt_sigframe *frame;
3285 abi_ulong frame_addr;
3286 int i;
3288 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3289 trace_user_setup_rt_frame(regs, frame_addr);
3290 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3291 goto give_sigsegv;
3294 tswap_siginfo(&frame->info, info);
3296 /* Create the ucontext. */
3297 __put_user(0, &frame->uc.tuc_flags);
3298 __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3299 __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3300 &frame->uc.tuc_stack.ss_sp);
3301 __put_user(sas_ss_flags(regs->gregs[15]),
3302 &frame->uc.tuc_stack.ss_flags);
3303 __put_user(target_sigaltstack_used.ss_size,
3304 &frame->uc.tuc_stack.ss_size);
3305 setup_sigcontext(&frame->uc.tuc_mcontext,
3306 regs, set->sig[0]);
3307 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3308 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3311 /* Set up to return from userspace. If provided, use a stub
3312 already in userspace. */
3313 if (ka->sa_flags & TARGET_SA_RESTORER) {
3314 regs->pr = (unsigned long) ka->sa_restorer;
3315 } else {
3316 /* Generate return code (system call to sigreturn) */
3317 abi_ulong retcode_addr = frame_addr +
3318 offsetof(struct target_rt_sigframe, retcode);
3319 __put_user(MOVW(2), &frame->retcode[0]);
3320 __put_user(TRAP_NOARG, &frame->retcode[1]);
3321 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3322 regs->pr = (unsigned long) retcode_addr;
3325 /* Set up registers for signal handler */
3326 regs->gregs[15] = frame_addr;
3327 regs->gregs[4] = sig; /* Arg for signal handler */
3328 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3329 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3330 regs->pc = (unsigned long) ka->_sa_handler;
3332 unlock_user_struct(frame, frame_addr, 1);
3333 return;
3335 give_sigsegv:
3336 unlock_user_struct(frame, frame_addr, 1);
3337 force_sig(TARGET_SIGSEGV);
3340 long do_sigreturn(CPUSH4State *regs)
3342 struct target_sigframe *frame;
3343 abi_ulong frame_addr;
3344 sigset_t blocked;
3345 target_sigset_t target_set;
3346 int i;
3347 int err = 0;
3349 frame_addr = regs->gregs[15];
3350 trace_user_do_sigreturn(regs, frame_addr);
3351 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3352 goto badframe;
3355 __get_user(target_set.sig[0], &frame->sc.oldmask);
3356 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3357 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3360 if (err)
3361 goto badframe;
3363 target_to_host_sigset_internal(&blocked, &target_set);
3364 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3366 restore_sigcontext(regs, &frame->sc);
3368 unlock_user_struct(frame, frame_addr, 0);
3369 return -TARGET_QEMU_ESIGRETURN;
3371 badframe:
3372 unlock_user_struct(frame, frame_addr, 0);
3373 force_sig(TARGET_SIGSEGV);
3374 return 0;
3377 long do_rt_sigreturn(CPUSH4State *regs)
3379 struct target_rt_sigframe *frame;
3380 abi_ulong frame_addr;
3381 sigset_t blocked;
3383 frame_addr = regs->gregs[15];
3384 trace_user_do_rt_sigreturn(regs, frame_addr);
3385 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3386 goto badframe;
3389 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3390 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3392 restore_sigcontext(regs, &frame->uc.tuc_mcontext);
3394 if (do_sigaltstack(frame_addr +
3395 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3396 0, get_sp_from_cpustate(regs)) == -EFAULT) {
3397 goto badframe;
3400 unlock_user_struct(frame, frame_addr, 0);
3401 return -TARGET_QEMU_ESIGRETURN;
3403 badframe:
3404 unlock_user_struct(frame, frame_addr, 0);
3405 force_sig(TARGET_SIGSEGV);
3406 return 0;
3408 #elif defined(TARGET_MICROBLAZE)
3410 struct target_sigcontext {
3411 struct target_pt_regs regs; /* needs to be first */
3412 uint32_t oldmask;
3415 struct target_stack_t {
3416 abi_ulong ss_sp;
3417 int ss_flags;
3418 unsigned int ss_size;
3421 struct target_ucontext {
3422 abi_ulong tuc_flags;
3423 abi_ulong tuc_link;
3424 struct target_stack_t tuc_stack;
3425 struct target_sigcontext tuc_mcontext;
3426 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3429 /* Signal frames. */
3430 struct target_signal_frame {
3431 struct target_ucontext uc;
3432 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3433 uint32_t tramp[2];
3436 struct rt_signal_frame {
3437 siginfo_t info;
3438 struct ucontext uc;
3439 uint32_t tramp[2];
3442 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3444 __put_user(env->regs[0], &sc->regs.r0);
3445 __put_user(env->regs[1], &sc->regs.r1);
3446 __put_user(env->regs[2], &sc->regs.r2);
3447 __put_user(env->regs[3], &sc->regs.r3);
3448 __put_user(env->regs[4], &sc->regs.r4);
3449 __put_user(env->regs[5], &sc->regs.r5);
3450 __put_user(env->regs[6], &sc->regs.r6);
3451 __put_user(env->regs[7], &sc->regs.r7);
3452 __put_user(env->regs[8], &sc->regs.r8);
3453 __put_user(env->regs[9], &sc->regs.r9);
3454 __put_user(env->regs[10], &sc->regs.r10);
3455 __put_user(env->regs[11], &sc->regs.r11);
3456 __put_user(env->regs[12], &sc->regs.r12);
3457 __put_user(env->regs[13], &sc->regs.r13);
3458 __put_user(env->regs[14], &sc->regs.r14);
3459 __put_user(env->regs[15], &sc->regs.r15);
3460 __put_user(env->regs[16], &sc->regs.r16);
3461 __put_user(env->regs[17], &sc->regs.r17);
3462 __put_user(env->regs[18], &sc->regs.r18);
3463 __put_user(env->regs[19], &sc->regs.r19);
3464 __put_user(env->regs[20], &sc->regs.r20);
3465 __put_user(env->regs[21], &sc->regs.r21);
3466 __put_user(env->regs[22], &sc->regs.r22);
3467 __put_user(env->regs[23], &sc->regs.r23);
3468 __put_user(env->regs[24], &sc->regs.r24);
3469 __put_user(env->regs[25], &sc->regs.r25);
3470 __put_user(env->regs[26], &sc->regs.r26);
3471 __put_user(env->regs[27], &sc->regs.r27);
3472 __put_user(env->regs[28], &sc->regs.r28);
3473 __put_user(env->regs[29], &sc->regs.r29);
3474 __put_user(env->regs[30], &sc->regs.r30);
3475 __put_user(env->regs[31], &sc->regs.r31);
3476 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3479 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3481 __get_user(env->regs[0], &sc->regs.r0);
3482 __get_user(env->regs[1], &sc->regs.r1);
3483 __get_user(env->regs[2], &sc->regs.r2);
3484 __get_user(env->regs[3], &sc->regs.r3);
3485 __get_user(env->regs[4], &sc->regs.r4);
3486 __get_user(env->regs[5], &sc->regs.r5);
3487 __get_user(env->regs[6], &sc->regs.r6);
3488 __get_user(env->regs[7], &sc->regs.r7);
3489 __get_user(env->regs[8], &sc->regs.r8);
3490 __get_user(env->regs[9], &sc->regs.r9);
3491 __get_user(env->regs[10], &sc->regs.r10);
3492 __get_user(env->regs[11], &sc->regs.r11);
3493 __get_user(env->regs[12], &sc->regs.r12);
3494 __get_user(env->regs[13], &sc->regs.r13);
3495 __get_user(env->regs[14], &sc->regs.r14);
3496 __get_user(env->regs[15], &sc->regs.r15);
3497 __get_user(env->regs[16], &sc->regs.r16);
3498 __get_user(env->regs[17], &sc->regs.r17);
3499 __get_user(env->regs[18], &sc->regs.r18);
3500 __get_user(env->regs[19], &sc->regs.r19);
3501 __get_user(env->regs[20], &sc->regs.r20);
3502 __get_user(env->regs[21], &sc->regs.r21);
3503 __get_user(env->regs[22], &sc->regs.r22);
3504 __get_user(env->regs[23], &sc->regs.r23);
3505 __get_user(env->regs[24], &sc->regs.r24);
3506 __get_user(env->regs[25], &sc->regs.r25);
3507 __get_user(env->regs[26], &sc->regs.r26);
3508 __get_user(env->regs[27], &sc->regs.r27);
3509 __get_user(env->regs[28], &sc->regs.r28);
3510 __get_user(env->regs[29], &sc->regs.r29);
3511 __get_user(env->regs[30], &sc->regs.r30);
3512 __get_user(env->regs[31], &sc->regs.r31);
3513 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3516 static abi_ulong get_sigframe(struct target_sigaction *ka,
3517 CPUMBState *env, int frame_size)
3519 abi_ulong sp = env->regs[1];
3521 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !on_sig_stack(sp)) {
3522 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3525 return ((sp - frame_size) & -8UL);
3528 static void setup_frame(int sig, struct target_sigaction *ka,
3529 target_sigset_t *set, CPUMBState *env)
3531 struct target_signal_frame *frame;
3532 abi_ulong frame_addr;
3533 int i;
3535 frame_addr = get_sigframe(ka, env, sizeof *frame);
3536 trace_user_setup_frame(env, frame_addr);
3537 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3538 goto badframe;
3540 /* Save the mask. */
3541 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3543 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3544 __put_user(set->sig[i], &frame->extramask[i - 1]);
3547 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3549 /* Set up to return from userspace. If provided, use a stub
3550 already in userspace. */
3551 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3552 if (ka->sa_flags & TARGET_SA_RESTORER) {
3553 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3554 } else {
3555 uint32_t t;
3556 /* Note, these encodings are _big endian_! */
3557 /* addi r12, r0, __NR_sigreturn */
3558 t = 0x31800000UL | TARGET_NR_sigreturn;
3559 __put_user(t, frame->tramp + 0);
3560 /* brki r14, 0x8 */
3561 t = 0xb9cc0008UL;
3562 __put_user(t, frame->tramp + 1);
3564 /* Return from sighandler will jump to the tramp.
3565 Negative 8 offset because return is rtsd r15, 8 */
3566 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3569 /* Set up registers for signal handler */
3570 env->regs[1] = frame_addr;
3571 /* Signal handler args: */
3572 env->regs[5] = sig; /* Arg 0: signum */
3573 env->regs[6] = 0;
3574 /* arg 1: sigcontext */
3575 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3577 /* Offset of 4 to handle microblaze rtid r14, 0 */
3578 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3580 unlock_user_struct(frame, frame_addr, 1);
3581 return;
3582 badframe:
3583 force_sig(TARGET_SIGSEGV);
3586 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3587 target_siginfo_t *info,
3588 target_sigset_t *set, CPUMBState *env)
3590 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3593 long do_sigreturn(CPUMBState *env)
3595 struct target_signal_frame *frame;
3596 abi_ulong frame_addr;
3597 target_sigset_t target_set;
3598 sigset_t set;
3599 int i;
3601 frame_addr = env->regs[R_SP];
3602 trace_user_do_sigreturn(env, frame_addr);
3603 /* Make sure the guest isn't playing games. */
3604 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3605 goto badframe;
3607 /* Restore blocked signals */
3608 __get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask);
3609 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3610 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3612 target_to_host_sigset_internal(&set, &target_set);
3613 do_sigprocmask(SIG_SETMASK, &set, NULL);
3615 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3616 /* We got here through a sigreturn syscall, our path back is via an
3617 rtb insn so setup r14 for that. */
3618 env->regs[14] = env->sregs[SR_PC];
3620 unlock_user_struct(frame, frame_addr, 0);
3621 return env->regs[10];
3622 badframe:
3623 force_sig(TARGET_SIGSEGV);
3626 long do_rt_sigreturn(CPUMBState *env)
3628 trace_user_do_rt_sigreturn(env, 0);
3629 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3630 return -TARGET_ENOSYS;
3633 #elif defined(TARGET_CRIS)
3635 struct target_sigcontext {
3636 struct target_pt_regs regs; /* needs to be first */
3637 uint32_t oldmask;
3638 uint32_t usp; /* usp before stacking this gunk on it */
3641 /* Signal frames. */
3642 struct target_signal_frame {
3643 struct target_sigcontext sc;
3644 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3645 uint16_t retcode[4]; /* Trampoline code. */
3648 struct rt_signal_frame {
3649 siginfo_t *pinfo;
3650 void *puc;
3651 siginfo_t info;
3652 struct ucontext uc;
3653 uint16_t retcode[4]; /* Trampoline code. */
3656 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3658 __put_user(env->regs[0], &sc->regs.r0);
3659 __put_user(env->regs[1], &sc->regs.r1);
3660 __put_user(env->regs[2], &sc->regs.r2);
3661 __put_user(env->regs[3], &sc->regs.r3);
3662 __put_user(env->regs[4], &sc->regs.r4);
3663 __put_user(env->regs[5], &sc->regs.r5);
3664 __put_user(env->regs[6], &sc->regs.r6);
3665 __put_user(env->regs[7], &sc->regs.r7);
3666 __put_user(env->regs[8], &sc->regs.r8);
3667 __put_user(env->regs[9], &sc->regs.r9);
3668 __put_user(env->regs[10], &sc->regs.r10);
3669 __put_user(env->regs[11], &sc->regs.r11);
3670 __put_user(env->regs[12], &sc->regs.r12);
3671 __put_user(env->regs[13], &sc->regs.r13);
3672 __put_user(env->regs[14], &sc->usp);
3673 __put_user(env->regs[15], &sc->regs.acr);
3674 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3675 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3676 __put_user(env->pc, &sc->regs.erp);
3679 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3681 __get_user(env->regs[0], &sc->regs.r0);
3682 __get_user(env->regs[1], &sc->regs.r1);
3683 __get_user(env->regs[2], &sc->regs.r2);
3684 __get_user(env->regs[3], &sc->regs.r3);
3685 __get_user(env->regs[4], &sc->regs.r4);
3686 __get_user(env->regs[5], &sc->regs.r5);
3687 __get_user(env->regs[6], &sc->regs.r6);
3688 __get_user(env->regs[7], &sc->regs.r7);
3689 __get_user(env->regs[8], &sc->regs.r8);
3690 __get_user(env->regs[9], &sc->regs.r9);
3691 __get_user(env->regs[10], &sc->regs.r10);
3692 __get_user(env->regs[11], &sc->regs.r11);
3693 __get_user(env->regs[12], &sc->regs.r12);
3694 __get_user(env->regs[13], &sc->regs.r13);
3695 __get_user(env->regs[14], &sc->usp);
3696 __get_user(env->regs[15], &sc->regs.acr);
3697 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3698 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3699 __get_user(env->pc, &sc->regs.erp);
3702 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3704 abi_ulong sp;
3705 /* Align the stack downwards to 4. */
3706 sp = (env->regs[R_SP] & ~3);
3707 return sp - framesize;
3710 static void setup_frame(int sig, struct target_sigaction *ka,
3711 target_sigset_t *set, CPUCRISState *env)
3713 struct target_signal_frame *frame;
3714 abi_ulong frame_addr;
3715 int i;
3717 frame_addr = get_sigframe(env, sizeof *frame);
3718 trace_user_setup_frame(env, frame_addr);
3719 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3720 goto badframe;
3723 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3724 * use this trampoline anymore but it sets it up for GDB.
3725 * In QEMU, using the trampoline simplifies things a bit so we use it.
3727 * This is movu.w __NR_sigreturn, r9; break 13;
3729 __put_user(0x9c5f, frame->retcode+0);
3730 __put_user(TARGET_NR_sigreturn,
3731 frame->retcode + 1);
3732 __put_user(0xe93d, frame->retcode + 2);
3734 /* Save the mask. */
3735 __put_user(set->sig[0], &frame->sc.oldmask);
3737 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3738 __put_user(set->sig[i], &frame->extramask[i - 1]);
3741 setup_sigcontext(&frame->sc, env);
3743 /* Move the stack and setup the arguments for the handler. */
3744 env->regs[R_SP] = frame_addr;
3745 env->regs[10] = sig;
3746 env->pc = (unsigned long) ka->_sa_handler;
3747 /* Link SRP so the guest returns through the trampoline. */
3748 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3750 unlock_user_struct(frame, frame_addr, 1);
3751 return;
3752 badframe:
3753 force_sig(TARGET_SIGSEGV);
3756 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3757 target_siginfo_t *info,
3758 target_sigset_t *set, CPUCRISState *env)
3760 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3763 long do_sigreturn(CPUCRISState *env)
3765 struct target_signal_frame *frame;
3766 abi_ulong frame_addr;
3767 target_sigset_t target_set;
3768 sigset_t set;
3769 int i;
3771 frame_addr = env->regs[R_SP];
3772 trace_user_do_sigreturn(env, frame_addr);
3773 /* Make sure the guest isn't playing games. */
3774 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1)) {
3775 goto badframe;
3778 /* Restore blocked signals */
3779 __get_user(target_set.sig[0], &frame->sc.oldmask);
3780 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3781 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3783 target_to_host_sigset_internal(&set, &target_set);
3784 do_sigprocmask(SIG_SETMASK, &set, NULL);
3786 restore_sigcontext(&frame->sc, env);
3787 unlock_user_struct(frame, frame_addr, 0);
3788 return -TARGET_QEMU_ESIGRETURN;
3789 badframe:
3790 force_sig(TARGET_SIGSEGV);
3793 long do_rt_sigreturn(CPUCRISState *env)
3795 trace_user_do_rt_sigreturn(env, 0);
3796 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3797 return -TARGET_ENOSYS;
3800 #elif defined(TARGET_OPENRISC)
3802 struct target_sigcontext {
3803 struct target_pt_regs regs;
3804 abi_ulong oldmask;
3805 abi_ulong usp;
3808 struct target_ucontext {
3809 abi_ulong tuc_flags;
3810 abi_ulong tuc_link;
3811 target_stack_t tuc_stack;
3812 struct target_sigcontext tuc_mcontext;
3813 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3816 struct target_rt_sigframe {
3817 abi_ulong pinfo;
3818 uint64_t puc;
3819 struct target_siginfo info;
3820 struct target_sigcontext sc;
3821 struct target_ucontext uc;
3822 unsigned char retcode[16]; /* trampoline code */
3825 /* This is the asm-generic/ucontext.h version */
3826 #if 0
3827 static int restore_sigcontext(CPUOpenRISCState *regs,
3828 struct target_sigcontext *sc)
3830 unsigned int err = 0;
3831 unsigned long old_usp;
3833 /* Alwys make any pending restarted system call return -EINTR */
3834 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3836 /* restore the regs from &sc->regs (same as sc, since regs is first)
3837 * (sc is already checked for VERIFY_READ since the sigframe was
3838 * checked in sys_sigreturn previously)
3841 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3842 goto badframe;
3845 /* make sure the U-flag is set so user-mode cannot fool us */
3847 regs->sr &= ~SR_SM;
3849 /* restore the old USP as it was before we stacked the sc etc.
3850 * (we cannot just pop the sigcontext since we aligned the sp and
3851 * stuff after pushing it)
3854 __get_user(old_usp, &sc->usp);
3855 phx_signal("old_usp 0x%lx", old_usp);
3857 __PHX__ REALLY /* ??? */
3858 wrusp(old_usp);
3859 regs->gpr[1] = old_usp;
3861 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3862 * after this completes, but we don't use that mechanism. maybe we can
3863 * use it now ?
3866 return err;
3868 badframe:
3869 return 1;
3871 #endif
3873 /* Set up a signal frame. */
3875 static void setup_sigcontext(struct target_sigcontext *sc,
3876 CPUOpenRISCState *regs,
3877 unsigned long mask)
3879 unsigned long usp = regs->gpr[1];
3881 /* copy the regs. they are first in sc so we can use sc directly */
3883 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3885 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3886 the signal handler. The frametype will be restored to its previous
3887 value in restore_sigcontext. */
3888 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3890 /* then some other stuff */
3891 __put_user(mask, &sc->oldmask);
3892 __put_user(usp, &sc->usp);
3895 static inline unsigned long align_sigframe(unsigned long sp)
3897 unsigned long i;
3898 i = sp & ~3UL;
3899 return i;
3902 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3903 CPUOpenRISCState *regs,
3904 size_t frame_size)
3906 unsigned long sp = regs->gpr[1];
3907 int onsigstack = on_sig_stack(sp);
3909 /* redzone */
3910 /* This is the X/Open sanctioned signal stack switching. */
3911 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !onsigstack) {
3912 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3915 sp = align_sigframe(sp - frame_size);
3918 * If we are on the alternate signal stack and would overflow it, don't.
3919 * Return an always-bogus address instead so we will die with SIGSEGV.
3922 if (onsigstack && !likely(on_sig_stack(sp))) {
3923 return -1L;
3926 return sp;
3929 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3930 target_siginfo_t *info,
3931 target_sigset_t *set, CPUOpenRISCState *env)
3933 int err = 0;
3934 abi_ulong frame_addr;
3935 unsigned long return_ip;
3936 struct target_rt_sigframe *frame;
3937 abi_ulong info_addr, uc_addr;
3939 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3940 trace_user_setup_rt_frame(env, frame_addr);
3941 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3942 goto give_sigsegv;
3945 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3946 __put_user(info_addr, &frame->pinfo);
3947 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3948 __put_user(uc_addr, &frame->puc);
3950 if (ka->sa_flags & SA_SIGINFO) {
3951 tswap_siginfo(&frame->info, info);
3954 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3955 __put_user(0, &frame->uc.tuc_flags);
3956 __put_user(0, &frame->uc.tuc_link);
3957 __put_user(target_sigaltstack_used.ss_sp,
3958 &frame->uc.tuc_stack.ss_sp);
3959 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3960 __put_user(target_sigaltstack_used.ss_size,
3961 &frame->uc.tuc_stack.ss_size);
3962 setup_sigcontext(&frame->sc, env, set->sig[0]);
3964 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3966 /* trampoline - the desired return ip is the retcode itself */
3967 return_ip = (unsigned long)&frame->retcode;
3968 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3969 __put_user(0xa960, (short *)(frame->retcode + 0));
3970 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3971 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3972 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3974 if (err) {
3975 goto give_sigsegv;
3978 /* TODO what is the current->exec_domain stuff and invmap ? */
3980 /* Set up registers for signal handler */
3981 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3982 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3983 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3984 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3985 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3987 /* actually move the usp to reflect the stacked frame */
3988 env->gpr[1] = (unsigned long)frame;
3990 return;
3992 give_sigsegv:
3993 unlock_user_struct(frame, frame_addr, 1);
3994 if (sig == TARGET_SIGSEGV) {
3995 ka->_sa_handler = TARGET_SIG_DFL;
3997 force_sig(TARGET_SIGSEGV);
4000 long do_sigreturn(CPUOpenRISCState *env)
4002 trace_user_do_sigreturn(env, 0);
4003 fprintf(stderr, "do_sigreturn: not implemented\n");
4004 return -TARGET_ENOSYS;
4007 long do_rt_sigreturn(CPUOpenRISCState *env)
4009 trace_user_do_rt_sigreturn(env, 0);
4010 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
4011 return -TARGET_ENOSYS;
4013 /* TARGET_OPENRISC */
4015 #elif defined(TARGET_S390X)
4017 #define __NUM_GPRS 16
4018 #define __NUM_FPRS 16
4019 #define __NUM_ACRS 16
4021 #define S390_SYSCALL_SIZE 2
4022 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4024 #define _SIGCONTEXT_NSIG 64
4025 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4026 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4027 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4028 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4029 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4031 typedef struct {
4032 target_psw_t psw;
4033 target_ulong gprs[__NUM_GPRS];
4034 unsigned int acrs[__NUM_ACRS];
4035 } target_s390_regs_common;
4037 typedef struct {
4038 unsigned int fpc;
4039 double fprs[__NUM_FPRS];
4040 } target_s390_fp_regs;
4042 typedef struct {
4043 target_s390_regs_common regs;
4044 target_s390_fp_regs fpregs;
4045 } target_sigregs;
4047 struct target_sigcontext {
4048 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4049 target_sigregs *sregs;
4052 typedef struct {
4053 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4054 struct target_sigcontext sc;
4055 target_sigregs sregs;
4056 int signo;
4057 uint8_t retcode[S390_SYSCALL_SIZE];
4058 } sigframe;
4060 struct target_ucontext {
4061 target_ulong tuc_flags;
4062 struct target_ucontext *tuc_link;
4063 target_stack_t tuc_stack;
4064 target_sigregs tuc_mcontext;
4065 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4068 typedef struct {
4069 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4070 uint8_t retcode[S390_SYSCALL_SIZE];
4071 struct target_siginfo info;
4072 struct target_ucontext uc;
4073 } rt_sigframe;
4075 static inline abi_ulong
4076 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4078 abi_ulong sp;
4080 /* Default to using normal stack */
4081 sp = env->regs[15];
4083 /* This is the X/Open sanctioned signal stack switching. */
4084 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4085 if (!sas_ss_flags(sp)) {
4086 sp = target_sigaltstack_used.ss_sp +
4087 target_sigaltstack_used.ss_size;
4091 /* This is the legacy signal stack switching. */
4092 else if (/* FIXME !user_mode(regs) */ 0 &&
4093 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4094 ka->sa_restorer) {
4095 sp = (abi_ulong) ka->sa_restorer;
4098 return (sp - frame_size) & -8ul;
4101 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4103 int i;
4104 //save_access_regs(current->thread.acrs); FIXME
4106 /* Copy a 'clean' PSW mask to the user to avoid leaking
4107 information about whether PER is currently on. */
4108 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4109 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4110 for (i = 0; i < 16; i++) {
4111 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4113 for (i = 0; i < 16; i++) {
4114 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4117 * We have to store the fp registers to current->thread.fp_regs
4118 * to merge them with the emulated registers.
4120 //save_fp_regs(&current->thread.fp_regs); FIXME
4121 for (i = 0; i < 16; i++) {
4122 __put_user(get_freg(env, i)->ll, &sregs->fpregs.fprs[i]);
4126 static void setup_frame(int sig, struct target_sigaction *ka,
4127 target_sigset_t *set, CPUS390XState *env)
4129 sigframe *frame;
4130 abi_ulong frame_addr;
4132 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4133 trace_user_setup_frame(env, frame_addr);
4134 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4135 goto give_sigsegv;
4138 __put_user(set->sig[0], &frame->sc.oldmask[0]);
4140 save_sigregs(env, &frame->sregs);
4142 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4143 (abi_ulong *)&frame->sc.sregs);
4145 /* Set up to return from userspace. If provided, use a stub
4146 already in userspace. */
4147 if (ka->sa_flags & TARGET_SA_RESTORER) {
4148 env->regs[14] = (unsigned long)
4149 ka->sa_restorer | PSW_ADDR_AMODE;
4150 } else {
4151 env->regs[14] = (unsigned long)
4152 frame->retcode | PSW_ADDR_AMODE;
4153 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4154 (uint16_t *)(frame->retcode));
4157 /* Set up backchain. */
4158 __put_user(env->regs[15], (abi_ulong *) frame);
4160 /* Set up registers for signal handler */
4161 env->regs[15] = frame_addr;
4162 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4164 env->regs[2] = sig; //map_signal(sig);
4165 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4167 /* We forgot to include these in the sigcontext.
4168 To avoid breaking binary compatibility, they are passed as args. */
4169 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4170 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4172 /* Place signal number on stack to allow backtrace from handler. */
4173 __put_user(env->regs[2], (int *) &frame->signo);
4174 unlock_user_struct(frame, frame_addr, 1);
4175 return;
4177 give_sigsegv:
4178 force_sig(TARGET_SIGSEGV);
4181 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4182 target_siginfo_t *info,
4183 target_sigset_t *set, CPUS390XState *env)
4185 int i;
4186 rt_sigframe *frame;
4187 abi_ulong frame_addr;
4189 frame_addr = get_sigframe(ka, env, sizeof *frame);
4190 trace_user_setup_rt_frame(env, frame_addr);
4191 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4192 goto give_sigsegv;
4195 tswap_siginfo(&frame->info, info);
4197 /* Create the ucontext. */
4198 __put_user(0, &frame->uc.tuc_flags);
4199 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4200 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4201 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4202 &frame->uc.tuc_stack.ss_flags);
4203 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4204 save_sigregs(env, &frame->uc.tuc_mcontext);
4205 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4206 __put_user((abi_ulong)set->sig[i],
4207 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4210 /* Set up to return from userspace. If provided, use a stub
4211 already in userspace. */
4212 if (ka->sa_flags & TARGET_SA_RESTORER) {
4213 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4214 } else {
4215 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4216 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4217 (uint16_t *)(frame->retcode));
4220 /* Set up backchain. */
4221 __put_user(env->regs[15], (abi_ulong *) frame);
4223 /* Set up registers for signal handler */
4224 env->regs[15] = frame_addr;
4225 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4227 env->regs[2] = sig; //map_signal(sig);
4228 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4229 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4230 return;
4232 give_sigsegv:
4233 force_sig(TARGET_SIGSEGV);
4236 static int
4237 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4239 int err = 0;
4240 int i;
4242 for (i = 0; i < 16; i++) {
4243 __get_user(env->regs[i], &sc->regs.gprs[i]);
4246 __get_user(env->psw.mask, &sc->regs.psw.mask);
4247 trace_user_s390x_restore_sigregs(env, (unsigned long long)sc->regs.psw.addr,
4248 (unsigned long long)env->psw.addr);
4249 __get_user(env->psw.addr, &sc->regs.psw.addr);
4250 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4252 for (i = 0; i < 16; i++) {
4253 __get_user(env->aregs[i], &sc->regs.acrs[i]);
4255 for (i = 0; i < 16; i++) {
4256 __get_user(get_freg(env, i)->ll, &sc->fpregs.fprs[i]);
4259 return err;
4262 long do_sigreturn(CPUS390XState *env)
4264 sigframe *frame;
4265 abi_ulong frame_addr = env->regs[15];
4266 target_sigset_t target_set;
4267 sigset_t set;
4269 trace_user_do_sigreturn(env, frame_addr);
4270 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4271 goto badframe;
4273 __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
4275 target_to_host_sigset_internal(&set, &target_set);
4276 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4278 if (restore_sigregs(env, &frame->sregs)) {
4279 goto badframe;
4282 unlock_user_struct(frame, frame_addr, 0);
4283 return -TARGET_QEMU_ESIGRETURN;
4285 badframe:
4286 force_sig(TARGET_SIGSEGV);
4287 return 0;
4290 long do_rt_sigreturn(CPUS390XState *env)
4292 rt_sigframe *frame;
4293 abi_ulong frame_addr = env->regs[15];
4294 sigset_t set;
4296 trace_user_do_rt_sigreturn(env, frame_addr);
4297 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4298 goto badframe;
4300 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4302 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4304 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4305 goto badframe;
4308 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4309 get_sp_from_cpustate(env)) == -EFAULT) {
4310 goto badframe;
4312 unlock_user_struct(frame, frame_addr, 0);
4313 return -TARGET_QEMU_ESIGRETURN;
4315 badframe:
4316 unlock_user_struct(frame, frame_addr, 0);
4317 force_sig(TARGET_SIGSEGV);
4318 return 0;
4321 #elif defined(TARGET_PPC)
4323 /* Size of dummy stack frame allocated when calling signal handler.
4324 See arch/powerpc/include/asm/ptrace.h. */
4325 #if defined(TARGET_PPC64)
4326 #define SIGNAL_FRAMESIZE 128
4327 #else
4328 #define SIGNAL_FRAMESIZE 64
4329 #endif
4331 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4332 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4333 struct target_mcontext {
4334 target_ulong mc_gregs[48];
4335 /* Includes fpscr. */
4336 uint64_t mc_fregs[33];
4337 target_ulong mc_pad[2];
4338 /* We need to handle Altivec and SPE at the same time, which no
4339 kernel needs to do. Fortunately, the kernel defines this bit to
4340 be Altivec-register-large all the time, rather than trying to
4341 twiddle it based on the specific platform. */
4342 union {
4343 /* SPE vector registers. One extra for SPEFSCR. */
4344 uint32_t spe[33];
4345 /* Altivec vector registers. The packing of VSCR and VRSAVE
4346 varies depending on whether we're PPC64 or not: PPC64 splits
4347 them apart; PPC32 stuffs them together. */
4348 #if defined(TARGET_PPC64)
4349 #define QEMU_NVRREG 34
4350 #else
4351 #define QEMU_NVRREG 33
4352 #endif
4353 ppc_avr_t altivec[QEMU_NVRREG];
4354 #undef QEMU_NVRREG
4355 } mc_vregs __attribute__((__aligned__(16)));
4358 /* See arch/powerpc/include/asm/sigcontext.h. */
4359 struct target_sigcontext {
4360 target_ulong _unused[4];
4361 int32_t signal;
4362 #if defined(TARGET_PPC64)
4363 int32_t pad0;
4364 #endif
4365 target_ulong handler;
4366 target_ulong oldmask;
4367 target_ulong regs; /* struct pt_regs __user * */
4368 #if defined(TARGET_PPC64)
4369 struct target_mcontext mcontext;
4370 #endif
4373 /* Indices for target_mcontext.mc_gregs, below.
4374 See arch/powerpc/include/asm/ptrace.h for details. */
4375 enum {
4376 TARGET_PT_R0 = 0,
4377 TARGET_PT_R1 = 1,
4378 TARGET_PT_R2 = 2,
4379 TARGET_PT_R3 = 3,
4380 TARGET_PT_R4 = 4,
4381 TARGET_PT_R5 = 5,
4382 TARGET_PT_R6 = 6,
4383 TARGET_PT_R7 = 7,
4384 TARGET_PT_R8 = 8,
4385 TARGET_PT_R9 = 9,
4386 TARGET_PT_R10 = 10,
4387 TARGET_PT_R11 = 11,
4388 TARGET_PT_R12 = 12,
4389 TARGET_PT_R13 = 13,
4390 TARGET_PT_R14 = 14,
4391 TARGET_PT_R15 = 15,
4392 TARGET_PT_R16 = 16,
4393 TARGET_PT_R17 = 17,
4394 TARGET_PT_R18 = 18,
4395 TARGET_PT_R19 = 19,
4396 TARGET_PT_R20 = 20,
4397 TARGET_PT_R21 = 21,
4398 TARGET_PT_R22 = 22,
4399 TARGET_PT_R23 = 23,
4400 TARGET_PT_R24 = 24,
4401 TARGET_PT_R25 = 25,
4402 TARGET_PT_R26 = 26,
4403 TARGET_PT_R27 = 27,
4404 TARGET_PT_R28 = 28,
4405 TARGET_PT_R29 = 29,
4406 TARGET_PT_R30 = 30,
4407 TARGET_PT_R31 = 31,
4408 TARGET_PT_NIP = 32,
4409 TARGET_PT_MSR = 33,
4410 TARGET_PT_ORIG_R3 = 34,
4411 TARGET_PT_CTR = 35,
4412 TARGET_PT_LNK = 36,
4413 TARGET_PT_XER = 37,
4414 TARGET_PT_CCR = 38,
4415 /* Yes, there are two registers with #39. One is 64-bit only. */
4416 TARGET_PT_MQ = 39,
4417 TARGET_PT_SOFTE = 39,
4418 TARGET_PT_TRAP = 40,
4419 TARGET_PT_DAR = 41,
4420 TARGET_PT_DSISR = 42,
4421 TARGET_PT_RESULT = 43,
4422 TARGET_PT_REGS_COUNT = 44
4426 struct target_ucontext {
4427 target_ulong tuc_flags;
4428 target_ulong tuc_link; /* struct ucontext __user * */
4429 struct target_sigaltstack tuc_stack;
4430 #if !defined(TARGET_PPC64)
4431 int32_t tuc_pad[7];
4432 target_ulong tuc_regs; /* struct mcontext __user *
4433 points to uc_mcontext field */
4434 #endif
4435 target_sigset_t tuc_sigmask;
4436 #if defined(TARGET_PPC64)
4437 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4438 struct target_sigcontext tuc_sigcontext;
4439 #else
4440 int32_t tuc_maskext[30];
4441 int32_t tuc_pad2[3];
4442 struct target_mcontext tuc_mcontext;
4443 #endif
4446 /* See arch/powerpc/kernel/signal_32.c. */
4447 struct target_sigframe {
4448 struct target_sigcontext sctx;
4449 struct target_mcontext mctx;
4450 int32_t abigap[56];
4453 #if defined(TARGET_PPC64)
4455 #define TARGET_TRAMP_SIZE 6
4457 struct target_rt_sigframe {
4458 /* sys_rt_sigreturn requires the ucontext be the first field */
4459 struct target_ucontext uc;
4460 target_ulong _unused[2];
4461 uint32_t trampoline[TARGET_TRAMP_SIZE];
4462 target_ulong pinfo; /* struct siginfo __user * */
4463 target_ulong puc; /* void __user * */
4464 struct target_siginfo info;
4465 /* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
4466 char abigap[288];
4467 } __attribute__((aligned(16)));
4469 #else
4471 struct target_rt_sigframe {
4472 struct target_siginfo info;
4473 struct target_ucontext uc;
4474 int32_t abigap[56];
4477 #endif
4479 #if defined(TARGET_PPC64)
4481 struct target_func_ptr {
4482 target_ulong entry;
4483 target_ulong toc;
4486 #endif
4488 /* We use the mc_pad field for the signal return trampoline. */
4489 #define tramp mc_pad
4491 /* See arch/powerpc/kernel/signal.c. */
4492 static target_ulong get_sigframe(struct target_sigaction *ka,
4493 CPUPPCState *env,
4494 int frame_size)
4496 target_ulong oldsp, newsp;
4498 oldsp = env->gpr[1];
4500 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4501 (sas_ss_flags(oldsp) == 0)) {
4502 oldsp = (target_sigaltstack_used.ss_sp
4503 + target_sigaltstack_used.ss_size);
4506 newsp = (oldsp - frame_size) & ~0xFUL;
4508 return newsp;
4511 static void save_user_regs(CPUPPCState *env, struct target_mcontext *frame)
4513 target_ulong msr = env->msr;
4514 int i;
4515 target_ulong ccr = 0;
4517 /* In general, the kernel attempts to be intelligent about what it
4518 needs to save for Altivec/FP/SPE registers. We don't care that
4519 much, so we just go ahead and save everything. */
4521 /* Save general registers. */
4522 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4523 __put_user(env->gpr[i], &frame->mc_gregs[i]);
4525 __put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4526 __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4527 __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4528 __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4530 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4531 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4533 __put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4535 /* Save Altivec registers if necessary. */
4536 if (env->insns_flags & PPC_ALTIVEC) {
4537 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4538 ppc_avr_t *avr = &env->avr[i];
4539 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4541 __put_user(avr->u64[0], &vreg->u64[0]);
4542 __put_user(avr->u64[1], &vreg->u64[1]);
4544 /* Set MSR_VR in the saved MSR value to indicate that
4545 frame->mc_vregs contains valid data. */
4546 msr |= MSR_VR;
4547 __put_user((uint32_t)env->spr[SPR_VRSAVE],
4548 &frame->mc_vregs.altivec[32].u32[3]);
4551 /* Save floating point registers. */
4552 if (env->insns_flags & PPC_FLOAT) {
4553 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4554 __put_user(env->fpr[i], &frame->mc_fregs[i]);
4556 __put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]);
4559 /* Save SPE registers. The kernel only saves the high half. */
4560 if (env->insns_flags & PPC_SPE) {
4561 #if defined(TARGET_PPC64)
4562 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4563 __put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i]);
4565 #else
4566 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4567 __put_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4569 #endif
4570 /* Set MSR_SPE in the saved MSR value to indicate that
4571 frame->mc_vregs contains valid data. */
4572 msr |= MSR_SPE;
4573 __put_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4576 /* Store MSR. */
4577 __put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4580 static void encode_trampoline(int sigret, uint32_t *tramp)
4582 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4583 if (sigret) {
4584 __put_user(0x38000000 | sigret, &tramp[0]);
4585 __put_user(0x44000002, &tramp[1]);
4589 static void restore_user_regs(CPUPPCState *env,
4590 struct target_mcontext *frame, int sig)
4592 target_ulong save_r2 = 0;
4593 target_ulong msr;
4594 target_ulong ccr;
4596 int i;
4598 if (!sig) {
4599 save_r2 = env->gpr[2];
4602 /* Restore general registers. */
4603 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4604 __get_user(env->gpr[i], &frame->mc_gregs[i]);
4606 __get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4607 __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4608 __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4609 __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4610 __get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4612 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4613 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4616 if (!sig) {
4617 env->gpr[2] = save_r2;
4619 /* Restore MSR. */
4620 __get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4622 /* If doing signal return, restore the previous little-endian mode. */
4623 if (sig)
4624 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4626 /* Restore Altivec registers if necessary. */
4627 if (env->insns_flags & PPC_ALTIVEC) {
4628 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4629 ppc_avr_t *avr = &env->avr[i];
4630 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4632 __get_user(avr->u64[0], &vreg->u64[0]);
4633 __get_user(avr->u64[1], &vreg->u64[1]);
4635 /* Set MSR_VEC in the saved MSR value to indicate that
4636 frame->mc_vregs contains valid data. */
4637 __get_user(env->spr[SPR_VRSAVE],
4638 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3]));
4641 /* Restore floating point registers. */
4642 if (env->insns_flags & PPC_FLOAT) {
4643 uint64_t fpscr;
4644 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4645 __get_user(env->fpr[i], &frame->mc_fregs[i]);
4647 __get_user(fpscr, &frame->mc_fregs[32]);
4648 env->fpscr = (uint32_t) fpscr;
4651 /* Save SPE registers. The kernel only saves the high half. */
4652 if (env->insns_flags & PPC_SPE) {
4653 #if defined(TARGET_PPC64)
4654 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4655 uint32_t hi;
4657 __get_user(hi, &frame->mc_vregs.spe[i]);
4658 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4660 #else
4661 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4662 __get_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4664 #endif
4665 __get_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4669 static void setup_frame(int sig, struct target_sigaction *ka,
4670 target_sigset_t *set, CPUPPCState *env)
4672 struct target_sigframe *frame;
4673 struct target_sigcontext *sc;
4674 target_ulong frame_addr, newsp;
4675 int err = 0;
4676 #if defined(TARGET_PPC64)
4677 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4678 #endif
4680 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4681 trace_user_setup_frame(env, frame_addr);
4682 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4683 goto sigsegv;
4684 sc = &frame->sctx;
4686 __put_user(ka->_sa_handler, &sc->handler);
4687 __put_user(set->sig[0], &sc->oldmask);
4688 #if TARGET_ABI_BITS == 64
4689 __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4690 #else
4691 __put_user(set->sig[1], &sc->_unused[3]);
4692 #endif
4693 __put_user(h2g(&frame->mctx), &sc->regs);
4694 __put_user(sig, &sc->signal);
4696 /* Save user regs. */
4697 save_user_regs(env, &frame->mctx);
4699 /* Construct the trampoline code on the stack. */
4700 encode_trampoline(TARGET_NR_sigreturn, (uint32_t *)&frame->mctx.tramp);
4702 /* The kernel checks for the presence of a VDSO here. We don't
4703 emulate a vdso, so use a sigreturn system call. */
4704 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4706 /* Turn off all fp exceptions. */
4707 env->fpscr = 0;
4709 /* Create a stack frame for the caller of the handler. */
4710 newsp = frame_addr - SIGNAL_FRAMESIZE;
4711 err |= put_user(env->gpr[1], newsp, target_ulong);
4713 if (err)
4714 goto sigsegv;
4716 /* Set up registers for signal handler. */
4717 env->gpr[1] = newsp;
4718 env->gpr[3] = sig;
4719 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4721 #if defined(TARGET_PPC64)
4722 if (get_ppc64_abi(image) < 2) {
4723 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4724 struct target_func_ptr *handler =
4725 (struct target_func_ptr *)g2h(ka->_sa_handler);
4726 env->nip = tswapl(handler->entry);
4727 env->gpr[2] = tswapl(handler->toc);
4728 } else {
4729 /* ELFv2 PPC64 function pointers are entry points, but R12
4730 * must also be set */
4731 env->nip = tswapl((target_ulong) ka->_sa_handler);
4732 env->gpr[12] = env->nip;
4734 #else
4735 env->nip = (target_ulong) ka->_sa_handler;
4736 #endif
4738 /* Signal handlers are entered in big-endian mode. */
4739 env->msr &= ~MSR_LE;
4741 unlock_user_struct(frame, frame_addr, 1);
4742 return;
4744 sigsegv:
4745 unlock_user_struct(frame, frame_addr, 1);
4746 force_sig(TARGET_SIGSEGV);
4749 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4750 target_siginfo_t *info,
4751 target_sigset_t *set, CPUPPCState *env)
4753 struct target_rt_sigframe *rt_sf;
4754 uint32_t *trampptr = 0;
4755 struct target_mcontext *mctx = 0;
4756 target_ulong rt_sf_addr, newsp = 0;
4757 int i, err = 0;
4758 #if defined(TARGET_PPC64)
4759 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4760 #endif
4762 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4763 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4764 goto sigsegv;
4766 tswap_siginfo(&rt_sf->info, info);
4768 __put_user(0, &rt_sf->uc.tuc_flags);
4769 __put_user(0, &rt_sf->uc.tuc_link);
4770 __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4771 &rt_sf->uc.tuc_stack.ss_sp);
4772 __put_user(sas_ss_flags(env->gpr[1]),
4773 &rt_sf->uc.tuc_stack.ss_flags);
4774 __put_user(target_sigaltstack_used.ss_size,
4775 &rt_sf->uc.tuc_stack.ss_size);
4776 #if !defined(TARGET_PPC64)
4777 __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4778 &rt_sf->uc.tuc_regs);
4779 #endif
4780 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4781 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4784 #if defined(TARGET_PPC64)
4785 mctx = &rt_sf->uc.tuc_sigcontext.mcontext;
4786 trampptr = &rt_sf->trampoline[0];
4787 #else
4788 mctx = &rt_sf->uc.tuc_mcontext;
4789 trampptr = (uint32_t *)&rt_sf->uc.tuc_mcontext.tramp;
4790 #endif
4792 save_user_regs(env, mctx);
4793 encode_trampoline(TARGET_NR_rt_sigreturn, trampptr);
4795 /* The kernel checks for the presence of a VDSO here. We don't
4796 emulate a vdso, so use a sigreturn system call. */
4797 env->lr = (target_ulong) h2g(trampptr);
4799 /* Turn off all fp exceptions. */
4800 env->fpscr = 0;
4802 /* Create a stack frame for the caller of the handler. */
4803 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4804 err |= put_user(env->gpr[1], newsp, target_ulong);
4806 if (err)
4807 goto sigsegv;
4809 /* Set up registers for signal handler. */
4810 env->gpr[1] = newsp;
4811 env->gpr[3] = (target_ulong) sig;
4812 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4813 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4814 env->gpr[6] = (target_ulong) h2g(rt_sf);
4816 #if defined(TARGET_PPC64)
4817 if (get_ppc64_abi(image) < 2) {
4818 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4819 struct target_func_ptr *handler =
4820 (struct target_func_ptr *)g2h(ka->_sa_handler);
4821 env->nip = tswapl(handler->entry);
4822 env->gpr[2] = tswapl(handler->toc);
4823 } else {
4824 /* ELFv2 PPC64 function pointers are entry points, but R12
4825 * must also be set */
4826 env->nip = tswapl((target_ulong) ka->_sa_handler);
4827 env->gpr[12] = env->nip;
4829 #else
4830 env->nip = (target_ulong) ka->_sa_handler;
4831 #endif
4833 /* Signal handlers are entered in big-endian mode. */
4834 env->msr &= ~MSR_LE;
4836 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4837 return;
4839 sigsegv:
4840 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4841 force_sig(TARGET_SIGSEGV);
4845 long do_sigreturn(CPUPPCState *env)
4847 struct target_sigcontext *sc = NULL;
4848 struct target_mcontext *sr = NULL;
4849 target_ulong sr_addr = 0, sc_addr;
4850 sigset_t blocked;
4851 target_sigset_t set;
4853 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4854 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4855 goto sigsegv;
4857 #if defined(TARGET_PPC64)
4858 set.sig[0] = sc->oldmask + ((uint64_t)(sc->_unused[3]) << 32);
4859 #else
4860 __get_user(set.sig[0], &sc->oldmask);
4861 __get_user(set.sig[1], &sc->_unused[3]);
4862 #endif
4863 target_to_host_sigset_internal(&blocked, &set);
4864 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4866 __get_user(sr_addr, &sc->regs);
4867 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4868 goto sigsegv;
4869 restore_user_regs(env, sr, 1);
4871 unlock_user_struct(sr, sr_addr, 1);
4872 unlock_user_struct(sc, sc_addr, 1);
4873 return -TARGET_QEMU_ESIGRETURN;
4875 sigsegv:
4876 unlock_user_struct(sr, sr_addr, 1);
4877 unlock_user_struct(sc, sc_addr, 1);
4878 force_sig(TARGET_SIGSEGV);
4879 return 0;
4882 /* See arch/powerpc/kernel/signal_32.c. */
4883 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4885 struct target_mcontext *mcp;
4886 target_ulong mcp_addr;
4887 sigset_t blocked;
4888 target_sigset_t set;
4890 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4891 sizeof (set)))
4892 return 1;
4894 #if defined(TARGET_PPC64)
4895 mcp_addr = h2g(ucp) +
4896 offsetof(struct target_ucontext, tuc_sigcontext.mcontext);
4897 #else
4898 __get_user(mcp_addr, &ucp->tuc_regs);
4899 #endif
4901 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4902 return 1;
4904 target_to_host_sigset_internal(&blocked, &set);
4905 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4906 restore_user_regs(env, mcp, sig);
4908 unlock_user_struct(mcp, mcp_addr, 1);
4909 return 0;
4912 long do_rt_sigreturn(CPUPPCState *env)
4914 struct target_rt_sigframe *rt_sf = NULL;
4915 target_ulong rt_sf_addr;
4917 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4918 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4919 goto sigsegv;
4921 if (do_setcontext(&rt_sf->uc, env, 1))
4922 goto sigsegv;
4924 do_sigaltstack(rt_sf_addr
4925 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4926 0, env->gpr[1]);
4928 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4929 return -TARGET_QEMU_ESIGRETURN;
4931 sigsegv:
4932 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4933 force_sig(TARGET_SIGSEGV);
4934 return 0;
4937 #elif defined(TARGET_M68K)
4939 struct target_sigcontext {
4940 abi_ulong sc_mask;
4941 abi_ulong sc_usp;
4942 abi_ulong sc_d0;
4943 abi_ulong sc_d1;
4944 abi_ulong sc_a0;
4945 abi_ulong sc_a1;
4946 unsigned short sc_sr;
4947 abi_ulong sc_pc;
4950 struct target_sigframe
4952 abi_ulong pretcode;
4953 int sig;
4954 int code;
4955 abi_ulong psc;
4956 char retcode[8];
4957 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4958 struct target_sigcontext sc;
4961 typedef int target_greg_t;
4962 #define TARGET_NGREG 18
4963 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4965 typedef struct target_fpregset {
4966 int f_fpcntl[3];
4967 int f_fpregs[8*3];
4968 } target_fpregset_t;
4970 struct target_mcontext {
4971 int version;
4972 target_gregset_t gregs;
4973 target_fpregset_t fpregs;
4976 #define TARGET_MCONTEXT_VERSION 2
4978 struct target_ucontext {
4979 abi_ulong tuc_flags;
4980 abi_ulong tuc_link;
4981 target_stack_t tuc_stack;
4982 struct target_mcontext tuc_mcontext;
4983 abi_long tuc_filler[80];
4984 target_sigset_t tuc_sigmask;
4987 struct target_rt_sigframe
4989 abi_ulong pretcode;
4990 int sig;
4991 abi_ulong pinfo;
4992 abi_ulong puc;
4993 char retcode[8];
4994 struct target_siginfo info;
4995 struct target_ucontext uc;
4998 static void setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4999 abi_ulong mask)
5001 __put_user(mask, &sc->sc_mask);
5002 __put_user(env->aregs[7], &sc->sc_usp);
5003 __put_user(env->dregs[0], &sc->sc_d0);
5004 __put_user(env->dregs[1], &sc->sc_d1);
5005 __put_user(env->aregs[0], &sc->sc_a0);
5006 __put_user(env->aregs[1], &sc->sc_a1);
5007 __put_user(env->sr, &sc->sc_sr);
5008 __put_user(env->pc, &sc->sc_pc);
5011 static void
5012 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc)
5014 int temp;
5016 __get_user(env->aregs[7], &sc->sc_usp);
5017 __get_user(env->dregs[0], &sc->sc_d0);
5018 __get_user(env->dregs[1], &sc->sc_d1);
5019 __get_user(env->aregs[0], &sc->sc_a0);
5020 __get_user(env->aregs[1], &sc->sc_a1);
5021 __get_user(env->pc, &sc->sc_pc);
5022 __get_user(temp, &sc->sc_sr);
5023 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5027 * Determine which stack to use..
5029 static inline abi_ulong
5030 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5031 size_t frame_size)
5033 unsigned long sp;
5035 sp = regs->aregs[7];
5037 /* This is the X/Open sanctioned signal stack switching. */
5038 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5039 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5042 return ((sp - frame_size) & -8UL);
5045 static void setup_frame(int sig, struct target_sigaction *ka,
5046 target_sigset_t *set, CPUM68KState *env)
5048 struct target_sigframe *frame;
5049 abi_ulong frame_addr;
5050 abi_ulong retcode_addr;
5051 abi_ulong sc_addr;
5052 int i;
5054 frame_addr = get_sigframe(ka, env, sizeof *frame);
5055 trace_user_setup_frame(env, frame_addr);
5056 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5057 goto give_sigsegv;
5060 __put_user(sig, &frame->sig);
5062 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5063 __put_user(sc_addr, &frame->psc);
5065 setup_sigcontext(&frame->sc, env, set->sig[0]);
5067 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5068 __put_user(set->sig[i], &frame->extramask[i - 1]);
5071 /* Set up to return from userspace. */
5073 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5074 __put_user(retcode_addr, &frame->pretcode);
5076 /* moveq #,d0; trap #0 */
5078 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5079 (uint32_t *)(frame->retcode));
5081 /* Set up to return from userspace */
5083 env->aregs[7] = frame_addr;
5084 env->pc = ka->_sa_handler;
5086 unlock_user_struct(frame, frame_addr, 1);
5087 return;
5089 give_sigsegv:
5090 force_sig(TARGET_SIGSEGV);
5093 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5094 CPUM68KState *env)
5096 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5098 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5099 __put_user(env->dregs[0], &gregs[0]);
5100 __put_user(env->dregs[1], &gregs[1]);
5101 __put_user(env->dregs[2], &gregs[2]);
5102 __put_user(env->dregs[3], &gregs[3]);
5103 __put_user(env->dregs[4], &gregs[4]);
5104 __put_user(env->dregs[5], &gregs[5]);
5105 __put_user(env->dregs[6], &gregs[6]);
5106 __put_user(env->dregs[7], &gregs[7]);
5107 __put_user(env->aregs[0], &gregs[8]);
5108 __put_user(env->aregs[1], &gregs[9]);
5109 __put_user(env->aregs[2], &gregs[10]);
5110 __put_user(env->aregs[3], &gregs[11]);
5111 __put_user(env->aregs[4], &gregs[12]);
5112 __put_user(env->aregs[5], &gregs[13]);
5113 __put_user(env->aregs[6], &gregs[14]);
5114 __put_user(env->aregs[7], &gregs[15]);
5115 __put_user(env->pc, &gregs[16]);
5116 __put_user(env->sr, &gregs[17]);
5118 return 0;
5121 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5122 struct target_ucontext *uc)
5124 int temp;
5125 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5127 __get_user(temp, &uc->tuc_mcontext.version);
5128 if (temp != TARGET_MCONTEXT_VERSION)
5129 goto badframe;
5131 /* restore passed registers */
5132 __get_user(env->dregs[0], &gregs[0]);
5133 __get_user(env->dregs[1], &gregs[1]);
5134 __get_user(env->dregs[2], &gregs[2]);
5135 __get_user(env->dregs[3], &gregs[3]);
5136 __get_user(env->dregs[4], &gregs[4]);
5137 __get_user(env->dregs[5], &gregs[5]);
5138 __get_user(env->dregs[6], &gregs[6]);
5139 __get_user(env->dregs[7], &gregs[7]);
5140 __get_user(env->aregs[0], &gregs[8]);
5141 __get_user(env->aregs[1], &gregs[9]);
5142 __get_user(env->aregs[2], &gregs[10]);
5143 __get_user(env->aregs[3], &gregs[11]);
5144 __get_user(env->aregs[4], &gregs[12]);
5145 __get_user(env->aregs[5], &gregs[13]);
5146 __get_user(env->aregs[6], &gregs[14]);
5147 __get_user(env->aregs[7], &gregs[15]);
5148 __get_user(env->pc, &gregs[16]);
5149 __get_user(temp, &gregs[17]);
5150 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5152 return 0;
5154 badframe:
5155 return 1;
5158 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5159 target_siginfo_t *info,
5160 target_sigset_t *set, CPUM68KState *env)
5162 struct target_rt_sigframe *frame;
5163 abi_ulong frame_addr;
5164 abi_ulong retcode_addr;
5165 abi_ulong info_addr;
5166 abi_ulong uc_addr;
5167 int err = 0;
5168 int i;
5170 frame_addr = get_sigframe(ka, env, sizeof *frame);
5171 trace_user_setup_rt_frame(env, frame_addr);
5172 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5173 goto give_sigsegv;
5176 __put_user(sig, &frame->sig);
5178 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5179 __put_user(info_addr, &frame->pinfo);
5181 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5182 __put_user(uc_addr, &frame->puc);
5184 tswap_siginfo(&frame->info, info);
5186 /* Create the ucontext */
5188 __put_user(0, &frame->uc.tuc_flags);
5189 __put_user(0, &frame->uc.tuc_link);
5190 __put_user(target_sigaltstack_used.ss_sp,
5191 &frame->uc.tuc_stack.ss_sp);
5192 __put_user(sas_ss_flags(env->aregs[7]),
5193 &frame->uc.tuc_stack.ss_flags);
5194 __put_user(target_sigaltstack_used.ss_size,
5195 &frame->uc.tuc_stack.ss_size);
5196 err |= target_rt_setup_ucontext(&frame->uc, env);
5198 if (err)
5199 goto give_sigsegv;
5201 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5202 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5205 /* Set up to return from userspace. */
5207 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5208 __put_user(retcode_addr, &frame->pretcode);
5210 /* moveq #,d0; notb d0; trap #0 */
5212 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5213 (uint32_t *)(frame->retcode + 0));
5214 __put_user(0x4e40, (uint16_t *)(frame->retcode + 4));
5216 if (err)
5217 goto give_sigsegv;
5219 /* Set up to return from userspace */
5221 env->aregs[7] = frame_addr;
5222 env->pc = ka->_sa_handler;
5224 unlock_user_struct(frame, frame_addr, 1);
5225 return;
5227 give_sigsegv:
5228 unlock_user_struct(frame, frame_addr, 1);
5229 force_sig(TARGET_SIGSEGV);
5232 long do_sigreturn(CPUM68KState *env)
5234 struct target_sigframe *frame;
5235 abi_ulong frame_addr = env->aregs[7] - 4;
5236 target_sigset_t target_set;
5237 sigset_t set;
5238 int i;
5240 trace_user_do_sigreturn(env, frame_addr);
5241 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5242 goto badframe;
5244 /* set blocked signals */
5246 __get_user(target_set.sig[0], &frame->sc.sc_mask);
5248 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5249 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
5252 target_to_host_sigset_internal(&set, &target_set);
5253 do_sigprocmask(SIG_SETMASK, &set, NULL);
5255 /* restore registers */
5257 restore_sigcontext(env, &frame->sc);
5259 unlock_user_struct(frame, frame_addr, 0);
5260 return -TARGET_QEMU_ESIGRETURN;
5262 badframe:
5263 force_sig(TARGET_SIGSEGV);
5264 return 0;
5267 long do_rt_sigreturn(CPUM68KState *env)
5269 struct target_rt_sigframe *frame;
5270 abi_ulong frame_addr = env->aregs[7] - 4;
5271 target_sigset_t target_set;
5272 sigset_t set;
5274 trace_user_do_rt_sigreturn(env, frame_addr);
5275 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5276 goto badframe;
5278 target_to_host_sigset_internal(&set, &target_set);
5279 do_sigprocmask(SIG_SETMASK, &set, NULL);
5281 /* restore registers */
5283 if (target_rt_restore_ucontext(env, &frame->uc))
5284 goto badframe;
5286 if (do_sigaltstack(frame_addr +
5287 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5288 0, get_sp_from_cpustate(env)) == -EFAULT)
5289 goto badframe;
5291 unlock_user_struct(frame, frame_addr, 0);
5292 return -TARGET_QEMU_ESIGRETURN;
5294 badframe:
5295 unlock_user_struct(frame, frame_addr, 0);
5296 force_sig(TARGET_SIGSEGV);
5297 return 0;
5300 #elif defined(TARGET_ALPHA)
5302 struct target_sigcontext {
5303 abi_long sc_onstack;
5304 abi_long sc_mask;
5305 abi_long sc_pc;
5306 abi_long sc_ps;
5307 abi_long sc_regs[32];
5308 abi_long sc_ownedfp;
5309 abi_long sc_fpregs[32];
5310 abi_ulong sc_fpcr;
5311 abi_ulong sc_fp_control;
5312 abi_ulong sc_reserved1;
5313 abi_ulong sc_reserved2;
5314 abi_ulong sc_ssize;
5315 abi_ulong sc_sbase;
5316 abi_ulong sc_traparg_a0;
5317 abi_ulong sc_traparg_a1;
5318 abi_ulong sc_traparg_a2;
5319 abi_ulong sc_fp_trap_pc;
5320 abi_ulong sc_fp_trigger_sum;
5321 abi_ulong sc_fp_trigger_inst;
5324 struct target_ucontext {
5325 abi_ulong tuc_flags;
5326 abi_ulong tuc_link;
5327 abi_ulong tuc_osf_sigmask;
5328 target_stack_t tuc_stack;
5329 struct target_sigcontext tuc_mcontext;
5330 target_sigset_t tuc_sigmask;
5333 struct target_sigframe {
5334 struct target_sigcontext sc;
5335 unsigned int retcode[3];
5338 struct target_rt_sigframe {
5339 target_siginfo_t info;
5340 struct target_ucontext uc;
5341 unsigned int retcode[3];
5344 #define INSN_MOV_R30_R16 0x47fe0410
5345 #define INSN_LDI_R0 0x201f0000
5346 #define INSN_CALLSYS 0x00000083
5348 static void setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5349 abi_ulong frame_addr, target_sigset_t *set)
5351 int i;
5353 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5354 __put_user(set->sig[0], &sc->sc_mask);
5355 __put_user(env->pc, &sc->sc_pc);
5356 __put_user(8, &sc->sc_ps);
5358 for (i = 0; i < 31; ++i) {
5359 __put_user(env->ir[i], &sc->sc_regs[i]);
5361 __put_user(0, &sc->sc_regs[31]);
5363 for (i = 0; i < 31; ++i) {
5364 __put_user(env->fir[i], &sc->sc_fpregs[i]);
5366 __put_user(0, &sc->sc_fpregs[31]);
5367 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5369 __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5370 __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5371 __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5374 static void restore_sigcontext(CPUAlphaState *env,
5375 struct target_sigcontext *sc)
5377 uint64_t fpcr;
5378 int i;
5380 __get_user(env->pc, &sc->sc_pc);
5382 for (i = 0; i < 31; ++i) {
5383 __get_user(env->ir[i], &sc->sc_regs[i]);
5385 for (i = 0; i < 31; ++i) {
5386 __get_user(env->fir[i], &sc->sc_fpregs[i]);
5389 __get_user(fpcr, &sc->sc_fpcr);
5390 cpu_alpha_store_fpcr(env, fpcr);
5393 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5394 CPUAlphaState *env,
5395 unsigned long framesize)
5397 abi_ulong sp = env->ir[IR_SP];
5399 /* This is the X/Open sanctioned signal stack switching. */
5400 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5401 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5403 return (sp - framesize) & -32;
5406 static void setup_frame(int sig, struct target_sigaction *ka,
5407 target_sigset_t *set, CPUAlphaState *env)
5409 abi_ulong frame_addr, r26;
5410 struct target_sigframe *frame;
5411 int err = 0;
5413 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5414 trace_user_setup_frame(env, frame_addr);
5415 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5416 goto give_sigsegv;
5419 setup_sigcontext(&frame->sc, env, frame_addr, set);
5421 if (ka->sa_restorer) {
5422 r26 = ka->sa_restorer;
5423 } else {
5424 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5425 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5426 &frame->retcode[1]);
5427 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5428 /* imb() */
5429 r26 = frame_addr;
5432 unlock_user_struct(frame, frame_addr, 1);
5434 if (err) {
5435 give_sigsegv:
5436 if (sig == TARGET_SIGSEGV) {
5437 ka->_sa_handler = TARGET_SIG_DFL;
5439 force_sig(TARGET_SIGSEGV);
5442 env->ir[IR_RA] = r26;
5443 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5444 env->ir[IR_A0] = sig;
5445 env->ir[IR_A1] = 0;
5446 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5447 env->ir[IR_SP] = frame_addr;
5450 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5451 target_siginfo_t *info,
5452 target_sigset_t *set, CPUAlphaState *env)
5454 abi_ulong frame_addr, r26;
5455 struct target_rt_sigframe *frame;
5456 int i, err = 0;
5458 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5459 trace_user_setup_rt_frame(env, frame_addr);
5460 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5461 goto give_sigsegv;
5464 tswap_siginfo(&frame->info, info);
5466 __put_user(0, &frame->uc.tuc_flags);
5467 __put_user(0, &frame->uc.tuc_link);
5468 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5469 __put_user(target_sigaltstack_used.ss_sp,
5470 &frame->uc.tuc_stack.ss_sp);
5471 __put_user(sas_ss_flags(env->ir[IR_SP]),
5472 &frame->uc.tuc_stack.ss_flags);
5473 __put_user(target_sigaltstack_used.ss_size,
5474 &frame->uc.tuc_stack.ss_size);
5475 setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5476 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5477 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5480 if (ka->sa_restorer) {
5481 r26 = ka->sa_restorer;
5482 } else {
5483 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5484 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5485 &frame->retcode[1]);
5486 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5487 /* imb(); */
5488 r26 = frame_addr;
5491 if (err) {
5492 give_sigsegv:
5493 if (sig == TARGET_SIGSEGV) {
5494 ka->_sa_handler = TARGET_SIG_DFL;
5496 force_sig(TARGET_SIGSEGV);
5499 env->ir[IR_RA] = r26;
5500 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5501 env->ir[IR_A0] = sig;
5502 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5503 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5504 env->ir[IR_SP] = frame_addr;
5507 long do_sigreturn(CPUAlphaState *env)
5509 struct target_sigcontext *sc;
5510 abi_ulong sc_addr = env->ir[IR_A0];
5511 target_sigset_t target_set;
5512 sigset_t set;
5514 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5515 goto badframe;
5518 target_sigemptyset(&target_set);
5519 __get_user(target_set.sig[0], &sc->sc_mask);
5521 target_to_host_sigset_internal(&set, &target_set);
5522 do_sigprocmask(SIG_SETMASK, &set, NULL);
5524 restore_sigcontext(env, sc);
5525 unlock_user_struct(sc, sc_addr, 0);
5526 return -TARGET_QEMU_ESIGRETURN;
5528 badframe:
5529 force_sig(TARGET_SIGSEGV);
5532 long do_rt_sigreturn(CPUAlphaState *env)
5534 abi_ulong frame_addr = env->ir[IR_A0];
5535 struct target_rt_sigframe *frame;
5536 sigset_t set;
5538 trace_user_do_rt_sigreturn(env, frame_addr);
5539 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5540 goto badframe;
5542 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5543 do_sigprocmask(SIG_SETMASK, &set, NULL);
5545 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5546 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5547 uc.tuc_stack),
5548 0, env->ir[IR_SP]) == -EFAULT) {
5549 goto badframe;
5552 unlock_user_struct(frame, frame_addr, 0);
5553 return -TARGET_QEMU_ESIGRETURN;
5556 badframe:
5557 unlock_user_struct(frame, frame_addr, 0);
5558 force_sig(TARGET_SIGSEGV);
5561 #elif defined(TARGET_TILEGX)
5563 struct target_sigcontext {
5564 union {
5565 /* General-purpose registers. */
5566 abi_ulong gregs[56];
5567 struct {
5568 abi_ulong __gregs[53];
5569 abi_ulong tp; /* Aliases gregs[TREG_TP]. */
5570 abi_ulong sp; /* Aliases gregs[TREG_SP]. */
5571 abi_ulong lr; /* Aliases gregs[TREG_LR]. */
5574 abi_ulong pc; /* Program counter. */
5575 abi_ulong ics; /* In Interrupt Critical Section? */
5576 abi_ulong faultnum; /* Fault number. */
5577 abi_ulong pad[5];
5580 struct target_ucontext {
5581 abi_ulong tuc_flags;
5582 abi_ulong tuc_link;
5583 target_stack_t tuc_stack;
5584 struct target_sigcontext tuc_mcontext;
5585 target_sigset_t tuc_sigmask; /* mask last for extensibility */
5588 struct target_rt_sigframe {
5589 unsigned char save_area[16]; /* caller save area */
5590 struct target_siginfo info;
5591 struct target_ucontext uc;
5594 static void setup_sigcontext(struct target_sigcontext *sc,
5595 CPUArchState *env, int signo)
5597 int i;
5599 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5600 __put_user(env->regs[i], &sc->gregs[i]);
5603 __put_user(env->pc, &sc->pc);
5604 __put_user(0, &sc->ics);
5605 __put_user(signo, &sc->faultnum);
5608 static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
5610 int i;
5612 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5613 __get_user(env->regs[i], &sc->gregs[i]);
5616 __get_user(env->pc, &sc->pc);
5619 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
5620 size_t frame_size)
5622 unsigned long sp = env->regs[TILEGX_R_SP];
5624 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
5625 return -1UL;
5628 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
5629 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5632 sp -= frame_size;
5633 sp &= -16UL;
5634 return sp;
5637 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5638 target_siginfo_t *info,
5639 target_sigset_t *set, CPUArchState *env)
5641 abi_ulong frame_addr;
5642 struct target_rt_sigframe *frame;
5643 unsigned long restorer;
5645 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5646 trace_user_setup_rt_frame(env, frame_addr);
5647 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5648 goto give_sigsegv;
5651 /* Always write at least the signal number for the stack backtracer. */
5652 if (ka->sa_flags & TARGET_SA_SIGINFO) {
5653 /* At sigreturn time, restore the callee-save registers too. */
5654 tswap_siginfo(&frame->info, info);
5655 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
5656 } else {
5657 __put_user(info->si_signo, &frame->info.si_signo);
5660 /* Create the ucontext. */
5661 __put_user(0, &frame->uc.tuc_flags);
5662 __put_user(0, &frame->uc.tuc_link);
5663 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
5664 __put_user(sas_ss_flags(env->regs[TILEGX_R_SP]),
5665 &frame->uc.tuc_stack.ss_flags);
5666 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
5667 setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
5669 restorer = (unsigned long) do_rt_sigreturn;
5670 if (ka->sa_flags & TARGET_SA_RESTORER) {
5671 restorer = (unsigned long) ka->sa_restorer;
5673 env->pc = (unsigned long) ka->_sa_handler;
5674 env->regs[TILEGX_R_SP] = (unsigned long) frame;
5675 env->regs[TILEGX_R_LR] = restorer;
5676 env->regs[0] = (unsigned long) sig;
5677 env->regs[1] = (unsigned long) &frame->info;
5678 env->regs[2] = (unsigned long) &frame->uc;
5679 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
5681 unlock_user_struct(frame, frame_addr, 1);
5682 return;
5684 give_sigsegv:
5685 if (sig == TARGET_SIGSEGV) {
5686 ka->_sa_handler = TARGET_SIG_DFL;
5688 force_sig(TARGET_SIGSEGV /* , current */);
5691 long do_rt_sigreturn(CPUTLGState *env)
5693 abi_ulong frame_addr = env->regs[TILEGX_R_SP];
5694 struct target_rt_sigframe *frame;
5695 sigset_t set;
5697 trace_user_do_rt_sigreturn(env, frame_addr);
5698 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5699 goto badframe;
5701 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5702 do_sigprocmask(SIG_SETMASK, &set, NULL);
5704 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5705 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5706 uc.tuc_stack),
5707 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
5708 goto badframe;
5711 unlock_user_struct(frame, frame_addr, 0);
5712 return -TARGET_QEMU_ESIGRETURN;
5715 badframe:
5716 unlock_user_struct(frame, frame_addr, 0);
5717 force_sig(TARGET_SIGSEGV);
5720 #else
5722 static void setup_frame(int sig, struct target_sigaction *ka,
5723 target_sigset_t *set, CPUArchState *env)
5725 fprintf(stderr, "setup_frame: not implemented\n");
5728 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5729 target_siginfo_t *info,
5730 target_sigset_t *set, CPUArchState *env)
5732 fprintf(stderr, "setup_rt_frame: not implemented\n");
5735 long do_sigreturn(CPUArchState *env)
5737 fprintf(stderr, "do_sigreturn: not implemented\n");
5738 return -TARGET_ENOSYS;
5741 long do_rt_sigreturn(CPUArchState *env)
5743 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5744 return -TARGET_ENOSYS;
5747 #endif
5749 void process_pending_signals(CPUArchState *cpu_env)
5751 CPUState *cpu = ENV_GET_CPU(cpu_env);
5752 int sig;
5753 abi_ulong handler;
5754 sigset_t set, old_set;
5755 target_sigset_t target_old_set;
5756 struct emulated_sigtable *k;
5757 struct target_sigaction *sa;
5758 struct sigqueue *q;
5759 TaskState *ts = cpu->opaque;
5761 if (!ts->signal_pending)
5762 return;
5764 /* FIXME: This is not threadsafe. */
5765 k = ts->sigtab;
5766 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5767 if (k->pending)
5768 goto handle_signal;
5769 k++;
5771 /* if no signal is pending, just return */
5772 ts->signal_pending = 0;
5773 return;
5775 handle_signal:
5776 trace_user_handle_signal(cpu_env, sig);
5777 /* dequeue signal */
5778 q = k->first;
5779 k->first = q->next;
5780 if (!k->first)
5781 k->pending = 0;
5783 sig = gdb_handlesig(cpu, sig);
5784 if (!sig) {
5785 sa = NULL;
5786 handler = TARGET_SIG_IGN;
5787 } else {
5788 sa = &sigact_table[sig - 1];
5789 handler = sa->_sa_handler;
5792 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5793 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5794 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5795 * because it got a real MMU fault), and treat as if default handler.
5797 handler = TARGET_SIG_DFL;
5800 if (handler == TARGET_SIG_DFL) {
5801 /* default handler : ignore some signal. The other are job control or fatal */
5802 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5803 kill(getpid(),SIGSTOP);
5804 } else if (sig != TARGET_SIGCHLD &&
5805 sig != TARGET_SIGURG &&
5806 sig != TARGET_SIGWINCH &&
5807 sig != TARGET_SIGCONT) {
5808 force_sig(sig);
5810 } else if (handler == TARGET_SIG_IGN) {
5811 /* ignore sig */
5812 } else if (handler == TARGET_SIG_ERR) {
5813 force_sig(sig);
5814 } else {
5815 /* compute the blocked signals during the handler execution */
5816 target_to_host_sigset(&set, &sa->sa_mask);
5817 /* SA_NODEFER indicates that the current signal should not be
5818 blocked during the handler */
5819 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5820 sigaddset(&set, target_to_host_signal(sig));
5822 /* block signals in the handler using Linux */
5823 do_sigprocmask(SIG_BLOCK, &set, &old_set);
5824 /* save the previous blocked signal state to restore it at the
5825 end of the signal execution (see do_sigreturn) */
5826 host_to_target_sigset_internal(&target_old_set, &old_set);
5828 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5829 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5831 CPUX86State *env = cpu_env;
5832 if (env->eflags & VM_MASK)
5833 save_v86_state(env);
5835 #endif
5836 /* prepare the stack frame of the virtual CPU */
5837 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \
5838 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
5839 /* These targets do not have traditional signals. */
5840 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5841 #else
5842 if (sa->sa_flags & TARGET_SA_SIGINFO)
5843 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5844 else
5845 setup_frame(sig, sa, &target_old_set, cpu_env);
5846 #endif
5847 if (sa->sa_flags & TARGET_SA_RESETHAND)
5848 sa->_sa_handler = TARGET_SIG_DFL;
5850 if (q != &k->info)
5851 free_sigqueue(cpu_env, q);