qapi-schema: dump-guest-memory: Improve text
[qemu/ar7.git] / linux-user / signal.c
blobd4d83f247aaba503e03b9c45be34e40b9c41ea13
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/ucontext.h>
26 #include <sys/resource.h>
28 #include "qemu.h"
29 #include "qemu-common.h"
30 #include "target_signal.h"
31 #include "trace.h"
33 static struct target_sigaltstack target_sigaltstack_used = {
34 .ss_sp = 0,
35 .ss_size = 0,
36 .ss_flags = TARGET_SS_DISABLE,
39 static struct target_sigaction sigact_table[TARGET_NSIG];
41 static void host_signal_handler(int host_signum, siginfo_t *info,
42 void *puc);
44 static uint8_t host_to_target_signal_table[_NSIG] = {
45 [SIGHUP] = TARGET_SIGHUP,
46 [SIGINT] = TARGET_SIGINT,
47 [SIGQUIT] = TARGET_SIGQUIT,
48 [SIGILL] = TARGET_SIGILL,
49 [SIGTRAP] = TARGET_SIGTRAP,
50 [SIGABRT] = TARGET_SIGABRT,
51 /* [SIGIOT] = TARGET_SIGIOT,*/
52 [SIGBUS] = TARGET_SIGBUS,
53 [SIGFPE] = TARGET_SIGFPE,
54 [SIGKILL] = TARGET_SIGKILL,
55 [SIGUSR1] = TARGET_SIGUSR1,
56 [SIGSEGV] = TARGET_SIGSEGV,
57 [SIGUSR2] = TARGET_SIGUSR2,
58 [SIGPIPE] = TARGET_SIGPIPE,
59 [SIGALRM] = TARGET_SIGALRM,
60 [SIGTERM] = TARGET_SIGTERM,
61 #ifdef SIGSTKFLT
62 [SIGSTKFLT] = TARGET_SIGSTKFLT,
63 #endif
64 [SIGCHLD] = TARGET_SIGCHLD,
65 [SIGCONT] = TARGET_SIGCONT,
66 [SIGSTOP] = TARGET_SIGSTOP,
67 [SIGTSTP] = TARGET_SIGTSTP,
68 [SIGTTIN] = TARGET_SIGTTIN,
69 [SIGTTOU] = TARGET_SIGTTOU,
70 [SIGURG] = TARGET_SIGURG,
71 [SIGXCPU] = TARGET_SIGXCPU,
72 [SIGXFSZ] = TARGET_SIGXFSZ,
73 [SIGVTALRM] = TARGET_SIGVTALRM,
74 [SIGPROF] = TARGET_SIGPROF,
75 [SIGWINCH] = TARGET_SIGWINCH,
76 [SIGIO] = TARGET_SIGIO,
77 [SIGPWR] = TARGET_SIGPWR,
78 [SIGSYS] = TARGET_SIGSYS,
79 /* next signals stay the same */
80 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
81 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
82 To fix this properly we need to do manual signal delivery multiplexed
83 over a single host signal. */
84 [__SIGRTMIN] = __SIGRTMAX,
85 [__SIGRTMAX] = __SIGRTMIN,
87 static uint8_t target_to_host_signal_table[_NSIG];
89 static inline int on_sig_stack(unsigned long sp)
91 return (sp - target_sigaltstack_used.ss_sp
92 < target_sigaltstack_used.ss_size);
95 static inline int sas_ss_flags(unsigned long sp)
97 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
98 : on_sig_stack(sp) ? SS_ONSTACK : 0);
101 int host_to_target_signal(int sig)
103 if (sig < 0 || sig >= _NSIG)
104 return sig;
105 return host_to_target_signal_table[sig];
108 int target_to_host_signal(int sig)
110 if (sig < 0 || sig >= _NSIG)
111 return sig;
112 return target_to_host_signal_table[sig];
115 static inline void target_sigemptyset(target_sigset_t *set)
117 memset(set, 0, sizeof(*set));
120 static inline void target_sigaddset(target_sigset_t *set, int signum)
122 signum--;
123 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
124 set->sig[signum / TARGET_NSIG_BPW] |= mask;
127 static inline int target_sigismember(const target_sigset_t *set, int signum)
129 signum--;
130 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
131 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
134 static void host_to_target_sigset_internal(target_sigset_t *d,
135 const sigset_t *s)
137 int i;
138 target_sigemptyset(d);
139 for (i = 1; i <= TARGET_NSIG; i++) {
140 if (sigismember(s, i)) {
141 target_sigaddset(d, host_to_target_signal(i));
146 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
148 target_sigset_t d1;
149 int i;
151 host_to_target_sigset_internal(&d1, s);
152 for(i = 0;i < TARGET_NSIG_WORDS; i++)
153 d->sig[i] = tswapal(d1.sig[i]);
156 static void target_to_host_sigset_internal(sigset_t *d,
157 const target_sigset_t *s)
159 int i;
160 sigemptyset(d);
161 for (i = 1; i <= TARGET_NSIG; i++) {
162 if (target_sigismember(s, i)) {
163 sigaddset(d, target_to_host_signal(i));
168 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
170 target_sigset_t s1;
171 int i;
173 for(i = 0;i < TARGET_NSIG_WORDS; i++)
174 s1.sig[i] = tswapal(s->sig[i]);
175 target_to_host_sigset_internal(d, &s1);
178 void host_to_target_old_sigset(abi_ulong *old_sigset,
179 const sigset_t *sigset)
181 target_sigset_t d;
182 host_to_target_sigset(&d, sigset);
183 *old_sigset = d.sig[0];
186 void target_to_host_old_sigset(sigset_t *sigset,
187 const abi_ulong *old_sigset)
189 target_sigset_t d;
190 int i;
192 d.sig[0] = *old_sigset;
193 for(i = 1;i < TARGET_NSIG_WORDS; i++)
194 d.sig[i] = 0;
195 target_to_host_sigset(sigset, &d);
198 /* Wrapper for sigprocmask function
199 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
200 * are host signal set, not guest ones. This wraps the sigprocmask host calls
201 * that should be protected (calls originated from guest)
203 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
205 int ret;
206 sigset_t val;
207 sigset_t *temp = NULL;
208 CPUState *cpu = thread_cpu;
209 TaskState *ts = (TaskState *)cpu->opaque;
210 bool segv_was_blocked = ts->sigsegv_blocked;
212 if (set) {
213 bool has_sigsegv = sigismember(set, SIGSEGV);
214 val = *set;
215 temp = &val;
217 sigdelset(temp, SIGSEGV);
219 switch (how) {
220 case SIG_BLOCK:
221 if (has_sigsegv) {
222 ts->sigsegv_blocked = true;
224 break;
225 case SIG_UNBLOCK:
226 if (has_sigsegv) {
227 ts->sigsegv_blocked = false;
229 break;
230 case SIG_SETMASK:
231 ts->sigsegv_blocked = has_sigsegv;
232 break;
233 default:
234 g_assert_not_reached();
238 ret = sigprocmask(how, temp, oldset);
240 if (oldset && segv_was_blocked) {
241 sigaddset(oldset, SIGSEGV);
244 return ret;
247 /* siginfo conversion */
249 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
250 const siginfo_t *info)
252 int sig = host_to_target_signal(info->si_signo);
253 tinfo->si_signo = sig;
254 tinfo->si_errno = 0;
255 tinfo->si_code = info->si_code;
257 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
258 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
259 /* Should never come here, but who knows. The information for
260 the target is irrelevant. */
261 tinfo->_sifields._sigfault._addr = 0;
262 } else if (sig == TARGET_SIGIO) {
263 tinfo->_sifields._sigpoll._band = info->si_band;
264 tinfo->_sifields._sigpoll._fd = info->si_fd;
265 } else if (sig == TARGET_SIGCHLD) {
266 tinfo->_sifields._sigchld._pid = info->si_pid;
267 tinfo->_sifields._sigchld._uid = info->si_uid;
268 tinfo->_sifields._sigchld._status
269 = host_to_target_waitstatus(info->si_status);
270 tinfo->_sifields._sigchld._utime = info->si_utime;
271 tinfo->_sifields._sigchld._stime = info->si_stime;
272 } else if (sig >= TARGET_SIGRTMIN) {
273 tinfo->_sifields._rt._pid = info->si_pid;
274 tinfo->_sifields._rt._uid = info->si_uid;
275 /* XXX: potential problem if 64 bit */
276 tinfo->_sifields._rt._sigval.sival_ptr
277 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
281 static void tswap_siginfo(target_siginfo_t *tinfo,
282 const target_siginfo_t *info)
284 int sig = info->si_signo;
285 tinfo->si_signo = tswap32(sig);
286 tinfo->si_errno = tswap32(info->si_errno);
287 tinfo->si_code = tswap32(info->si_code);
289 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
290 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
291 tinfo->_sifields._sigfault._addr
292 = tswapal(info->_sifields._sigfault._addr);
293 } else if (sig == TARGET_SIGIO) {
294 tinfo->_sifields._sigpoll._band
295 = tswap32(info->_sifields._sigpoll._band);
296 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
297 } else if (sig == TARGET_SIGCHLD) {
298 tinfo->_sifields._sigchld._pid
299 = tswap32(info->_sifields._sigchld._pid);
300 tinfo->_sifields._sigchld._uid
301 = tswap32(info->_sifields._sigchld._uid);
302 tinfo->_sifields._sigchld._status
303 = tswap32(info->_sifields._sigchld._status);
304 tinfo->_sifields._sigchld._utime
305 = tswapal(info->_sifields._sigchld._utime);
306 tinfo->_sifields._sigchld._stime
307 = tswapal(info->_sifields._sigchld._stime);
308 } else if (sig >= TARGET_SIGRTMIN) {
309 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
310 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
311 tinfo->_sifields._rt._sigval.sival_ptr
312 = tswapal(info->_sifields._rt._sigval.sival_ptr);
317 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
319 host_to_target_siginfo_noswap(tinfo, info);
320 tswap_siginfo(tinfo, tinfo);
323 /* XXX: we support only POSIX RT signals are used. */
324 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
325 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
327 info->si_signo = tswap32(tinfo->si_signo);
328 info->si_errno = tswap32(tinfo->si_errno);
329 info->si_code = tswap32(tinfo->si_code);
330 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
331 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
332 info->si_value.sival_ptr =
333 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
336 static int fatal_signal (int sig)
338 switch (sig) {
339 case TARGET_SIGCHLD:
340 case TARGET_SIGURG:
341 case TARGET_SIGWINCH:
342 /* Ignored by default. */
343 return 0;
344 case TARGET_SIGCONT:
345 case TARGET_SIGSTOP:
346 case TARGET_SIGTSTP:
347 case TARGET_SIGTTIN:
348 case TARGET_SIGTTOU:
349 /* Job control signals. */
350 return 0;
351 default:
352 return 1;
356 /* returns 1 if given signal should dump core if not handled */
357 static int core_dump_signal(int sig)
359 switch (sig) {
360 case TARGET_SIGABRT:
361 case TARGET_SIGFPE:
362 case TARGET_SIGILL:
363 case TARGET_SIGQUIT:
364 case TARGET_SIGSEGV:
365 case TARGET_SIGTRAP:
366 case TARGET_SIGBUS:
367 return (1);
368 default:
369 return (0);
373 void signal_init(void)
375 struct sigaction act;
376 struct sigaction oact;
377 int i, j;
378 int host_sig;
380 /* generate signal conversion tables */
381 for(i = 1; i < _NSIG; i++) {
382 if (host_to_target_signal_table[i] == 0)
383 host_to_target_signal_table[i] = i;
385 for(i = 1; i < _NSIG; i++) {
386 j = host_to_target_signal_table[i];
387 target_to_host_signal_table[j] = i;
390 /* set all host signal handlers. ALL signals are blocked during
391 the handlers to serialize them. */
392 memset(sigact_table, 0, sizeof(sigact_table));
394 sigfillset(&act.sa_mask);
395 act.sa_flags = SA_SIGINFO;
396 act.sa_sigaction = host_signal_handler;
397 for(i = 1; i <= TARGET_NSIG; i++) {
398 host_sig = target_to_host_signal(i);
399 sigaction(host_sig, NULL, &oact);
400 if (oact.sa_sigaction == (void *)SIG_IGN) {
401 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
402 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
403 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
405 /* If there's already a handler installed then something has
406 gone horribly wrong, so don't even try to handle that case. */
407 /* Install some handlers for our own use. We need at least
408 SIGSEGV and SIGBUS, to detect exceptions. We can not just
409 trap all signals because it affects syscall interrupt
410 behavior. But do trap all default-fatal signals. */
411 if (fatal_signal (i))
412 sigaction(host_sig, &act, NULL);
416 /* signal queue handling */
418 static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
420 CPUState *cpu = ENV_GET_CPU(env);
421 TaskState *ts = cpu->opaque;
422 struct sigqueue *q = ts->first_free;
423 if (!q)
424 return NULL;
425 ts->first_free = q->next;
426 return q;
429 static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
431 CPUState *cpu = ENV_GET_CPU(env);
432 TaskState *ts = cpu->opaque;
434 q->next = ts->first_free;
435 ts->first_free = q;
438 /* abort execution with signal */
439 static void QEMU_NORETURN force_sig(int target_sig)
441 CPUState *cpu = thread_cpu;
442 CPUArchState *env = cpu->env_ptr;
443 TaskState *ts = (TaskState *)cpu->opaque;
444 int host_sig, core_dumped = 0;
445 struct sigaction act;
447 host_sig = target_to_host_signal(target_sig);
448 trace_user_force_sig(env, target_sig, host_sig);
449 gdb_signalled(env, target_sig);
451 /* dump core if supported by target binary format */
452 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
453 stop_all_tasks();
454 core_dumped =
455 ((*ts->bprm->core_dump)(target_sig, env) == 0);
457 if (core_dumped) {
458 /* we already dumped the core of target process, we don't want
459 * a coredump of qemu itself */
460 struct rlimit nodump;
461 getrlimit(RLIMIT_CORE, &nodump);
462 nodump.rlim_cur=0;
463 setrlimit(RLIMIT_CORE, &nodump);
464 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
465 target_sig, strsignal(host_sig), "core dumped" );
468 /* The proper exit code for dying from an uncaught signal is
469 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
470 * a negative value. To get the proper exit code we need to
471 * actually die from an uncaught signal. Here the default signal
472 * handler is installed, we send ourself a signal and we wait for
473 * it to arrive. */
474 sigfillset(&act.sa_mask);
475 act.sa_handler = SIG_DFL;
476 act.sa_flags = 0;
477 sigaction(host_sig, &act, NULL);
479 /* For some reason raise(host_sig) doesn't send the signal when
480 * statically linked on x86-64. */
481 kill(getpid(), host_sig);
483 /* Make sure the signal isn't masked (just reuse the mask inside
484 of act) */
485 sigdelset(&act.sa_mask, host_sig);
486 sigsuspend(&act.sa_mask);
488 /* unreachable */
489 abort();
492 /* queue a signal so that it will be send to the virtual CPU as soon
493 as possible */
494 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
496 CPUState *cpu = ENV_GET_CPU(env);
497 TaskState *ts = cpu->opaque;
498 struct emulated_sigtable *k;
499 struct sigqueue *q, **pq;
500 abi_ulong handler;
501 int queue;
503 trace_user_queue_signal(env, sig);
504 k = &ts->sigtab[sig - 1];
505 queue = gdb_queuesig ();
506 handler = sigact_table[sig - 1]._sa_handler;
508 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
509 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
510 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
511 * because it got a real MMU fault). A blocked SIGSEGV in that
512 * situation is treated as if using the default handler. This is
513 * not correct if some other process has randomly sent us a SIGSEGV
514 * via kill(), but that is not easy to distinguish at this point,
515 * so we assume it doesn't happen.
517 handler = TARGET_SIG_DFL;
520 if (!queue && handler == TARGET_SIG_DFL) {
521 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
522 kill(getpid(),SIGSTOP);
523 return 0;
524 } else
525 /* default handler : ignore some signal. The other are fatal */
526 if (sig != TARGET_SIGCHLD &&
527 sig != TARGET_SIGURG &&
528 sig != TARGET_SIGWINCH &&
529 sig != TARGET_SIGCONT) {
530 force_sig(sig);
531 } else {
532 return 0; /* indicate ignored */
534 } else if (!queue && handler == TARGET_SIG_IGN) {
535 /* ignore signal */
536 return 0;
537 } else if (!queue && handler == TARGET_SIG_ERR) {
538 force_sig(sig);
539 } else {
540 pq = &k->first;
541 if (sig < TARGET_SIGRTMIN) {
542 /* if non real time signal, we queue exactly one signal */
543 if (!k->pending)
544 q = &k->info;
545 else
546 return 0;
547 } else {
548 if (!k->pending) {
549 /* first signal */
550 q = &k->info;
551 } else {
552 q = alloc_sigqueue(env);
553 if (!q)
554 return -EAGAIN;
555 while (*pq != NULL)
556 pq = &(*pq)->next;
559 *pq = q;
560 q->info = *info;
561 q->next = NULL;
562 k->pending = 1;
563 /* signal that a new signal is pending */
564 ts->signal_pending = 1;
565 return 1; /* indicates that the signal was queued */
569 static void host_signal_handler(int host_signum, siginfo_t *info,
570 void *puc)
572 CPUArchState *env = thread_cpu->env_ptr;
573 int sig;
574 target_siginfo_t tinfo;
576 /* the CPU emulator uses some host signals to detect exceptions,
577 we forward to it some signals */
578 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
579 && info->si_code > 0) {
580 if (cpu_signal_handler(host_signum, info, puc))
581 return;
584 /* get target signal number */
585 sig = host_to_target_signal(host_signum);
586 if (sig < 1 || sig > TARGET_NSIG)
587 return;
588 trace_user_host_signal(env, host_signum, sig);
589 host_to_target_siginfo_noswap(&tinfo, info);
590 if (queue_signal(env, sig, &tinfo) == 1) {
591 /* interrupt the virtual CPU as soon as possible */
592 cpu_exit(thread_cpu);
596 /* do_sigaltstack() returns target values and errnos. */
597 /* compare linux/kernel/signal.c:do_sigaltstack() */
598 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
600 int ret;
601 struct target_sigaltstack oss;
603 /* XXX: test errors */
604 if(uoss_addr)
606 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
607 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
608 __put_user(sas_ss_flags(sp), &oss.ss_flags);
611 if(uss_addr)
613 struct target_sigaltstack *uss;
614 struct target_sigaltstack ss;
615 size_t minstacksize = TARGET_MINSIGSTKSZ;
617 #if defined(TARGET_PPC64)
618 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
619 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
620 if (get_ppc64_abi(image) > 1) {
621 minstacksize = 4096;
623 #endif
625 ret = -TARGET_EFAULT;
626 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
627 goto out;
629 __get_user(ss.ss_sp, &uss->ss_sp);
630 __get_user(ss.ss_size, &uss->ss_size);
631 __get_user(ss.ss_flags, &uss->ss_flags);
632 unlock_user_struct(uss, uss_addr, 0);
634 ret = -TARGET_EPERM;
635 if (on_sig_stack(sp))
636 goto out;
638 ret = -TARGET_EINVAL;
639 if (ss.ss_flags != TARGET_SS_DISABLE
640 && ss.ss_flags != TARGET_SS_ONSTACK
641 && ss.ss_flags != 0)
642 goto out;
644 if (ss.ss_flags == TARGET_SS_DISABLE) {
645 ss.ss_size = 0;
646 ss.ss_sp = 0;
647 } else {
648 ret = -TARGET_ENOMEM;
649 if (ss.ss_size < minstacksize) {
650 goto out;
654 target_sigaltstack_used.ss_sp = ss.ss_sp;
655 target_sigaltstack_used.ss_size = ss.ss_size;
658 if (uoss_addr) {
659 ret = -TARGET_EFAULT;
660 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
661 goto out;
664 ret = 0;
665 out:
666 return ret;
669 /* do_sigaction() return host values and errnos */
670 int do_sigaction(int sig, const struct target_sigaction *act,
671 struct target_sigaction *oact)
673 struct target_sigaction *k;
674 struct sigaction act1;
675 int host_sig;
676 int ret = 0;
678 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
679 return -EINVAL;
680 k = &sigact_table[sig - 1];
681 if (oact) {
682 __put_user(k->_sa_handler, &oact->_sa_handler);
683 __put_user(k->sa_flags, &oact->sa_flags);
684 #if !defined(TARGET_MIPS)
685 __put_user(k->sa_restorer, &oact->sa_restorer);
686 #endif
687 /* Not swapped. */
688 oact->sa_mask = k->sa_mask;
690 if (act) {
691 /* FIXME: This is not threadsafe. */
692 __get_user(k->_sa_handler, &act->_sa_handler);
693 __get_user(k->sa_flags, &act->sa_flags);
694 #if !defined(TARGET_MIPS)
695 __get_user(k->sa_restorer, &act->sa_restorer);
696 #endif
697 /* To be swapped in target_to_host_sigset. */
698 k->sa_mask = act->sa_mask;
700 /* we update the host linux signal state */
701 host_sig = target_to_host_signal(sig);
702 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
703 sigfillset(&act1.sa_mask);
704 act1.sa_flags = SA_SIGINFO;
705 if (k->sa_flags & TARGET_SA_RESTART)
706 act1.sa_flags |= SA_RESTART;
707 /* NOTE: it is important to update the host kernel signal
708 ignore state to avoid getting unexpected interrupted
709 syscalls */
710 if (k->_sa_handler == TARGET_SIG_IGN) {
711 act1.sa_sigaction = (void *)SIG_IGN;
712 } else if (k->_sa_handler == TARGET_SIG_DFL) {
713 if (fatal_signal (sig))
714 act1.sa_sigaction = host_signal_handler;
715 else
716 act1.sa_sigaction = (void *)SIG_DFL;
717 } else {
718 act1.sa_sigaction = host_signal_handler;
720 ret = sigaction(host_sig, &act1, NULL);
723 return ret;
726 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
728 /* from the Linux kernel */
730 struct target_fpreg {
731 uint16_t significand[4];
732 uint16_t exponent;
735 struct target_fpxreg {
736 uint16_t significand[4];
737 uint16_t exponent;
738 uint16_t padding[3];
741 struct target_xmmreg {
742 abi_ulong element[4];
745 struct target_fpstate {
746 /* Regular FPU environment */
747 abi_ulong cw;
748 abi_ulong sw;
749 abi_ulong tag;
750 abi_ulong ipoff;
751 abi_ulong cssel;
752 abi_ulong dataoff;
753 abi_ulong datasel;
754 struct target_fpreg _st[8];
755 uint16_t status;
756 uint16_t magic; /* 0xffff = regular FPU data only */
758 /* FXSR FPU environment */
759 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
760 abi_ulong mxcsr;
761 abi_ulong reserved;
762 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
763 struct target_xmmreg _xmm[8];
764 abi_ulong padding[56];
767 #define X86_FXSR_MAGIC 0x0000
769 struct target_sigcontext {
770 uint16_t gs, __gsh;
771 uint16_t fs, __fsh;
772 uint16_t es, __esh;
773 uint16_t ds, __dsh;
774 abi_ulong edi;
775 abi_ulong esi;
776 abi_ulong ebp;
777 abi_ulong esp;
778 abi_ulong ebx;
779 abi_ulong edx;
780 abi_ulong ecx;
781 abi_ulong eax;
782 abi_ulong trapno;
783 abi_ulong err;
784 abi_ulong eip;
785 uint16_t cs, __csh;
786 abi_ulong eflags;
787 abi_ulong esp_at_signal;
788 uint16_t ss, __ssh;
789 abi_ulong fpstate; /* pointer */
790 abi_ulong oldmask;
791 abi_ulong cr2;
794 struct target_ucontext {
795 abi_ulong tuc_flags;
796 abi_ulong tuc_link;
797 target_stack_t tuc_stack;
798 struct target_sigcontext tuc_mcontext;
799 target_sigset_t tuc_sigmask; /* mask last for extensibility */
802 struct sigframe
804 abi_ulong pretcode;
805 int sig;
806 struct target_sigcontext sc;
807 struct target_fpstate fpstate;
808 abi_ulong extramask[TARGET_NSIG_WORDS-1];
809 char retcode[8];
812 struct rt_sigframe
814 abi_ulong pretcode;
815 int sig;
816 abi_ulong pinfo;
817 abi_ulong puc;
818 struct target_siginfo info;
819 struct target_ucontext uc;
820 struct target_fpstate fpstate;
821 char retcode[8];
825 * Set up a signal frame.
828 /* XXX: save x87 state */
829 static void setup_sigcontext(struct target_sigcontext *sc,
830 struct target_fpstate *fpstate, CPUX86State *env, abi_ulong mask,
831 abi_ulong fpstate_addr)
833 CPUState *cs = CPU(x86_env_get_cpu(env));
834 uint16_t magic;
836 /* already locked in setup_frame() */
837 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
838 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
839 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
840 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
841 __put_user(env->regs[R_EDI], &sc->edi);
842 __put_user(env->regs[R_ESI], &sc->esi);
843 __put_user(env->regs[R_EBP], &sc->ebp);
844 __put_user(env->regs[R_ESP], &sc->esp);
845 __put_user(env->regs[R_EBX], &sc->ebx);
846 __put_user(env->regs[R_EDX], &sc->edx);
847 __put_user(env->regs[R_ECX], &sc->ecx);
848 __put_user(env->regs[R_EAX], &sc->eax);
849 __put_user(cs->exception_index, &sc->trapno);
850 __put_user(env->error_code, &sc->err);
851 __put_user(env->eip, &sc->eip);
852 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
853 __put_user(env->eflags, &sc->eflags);
854 __put_user(env->regs[R_ESP], &sc->esp_at_signal);
855 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
857 cpu_x86_fsave(env, fpstate_addr, 1);
858 fpstate->status = fpstate->sw;
859 magic = 0xffff;
860 __put_user(magic, &fpstate->magic);
861 __put_user(fpstate_addr, &sc->fpstate);
863 /* non-iBCS2 extensions.. */
864 __put_user(mask, &sc->oldmask);
865 __put_user(env->cr[2], &sc->cr2);
869 * Determine which stack to use..
872 static inline abi_ulong
873 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
875 unsigned long esp;
877 /* Default to using normal stack */
878 esp = env->regs[R_ESP];
879 /* This is the X/Open sanctioned signal stack switching. */
880 if (ka->sa_flags & TARGET_SA_ONSTACK) {
881 if (sas_ss_flags(esp) == 0)
882 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
885 /* This is the legacy signal stack switching. */
886 else
887 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
888 !(ka->sa_flags & TARGET_SA_RESTORER) &&
889 ka->sa_restorer) {
890 esp = (unsigned long) ka->sa_restorer;
892 return (esp - frame_size) & -8ul;
895 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
896 static void setup_frame(int sig, struct target_sigaction *ka,
897 target_sigset_t *set, CPUX86State *env)
899 abi_ulong frame_addr;
900 struct sigframe *frame;
901 int i;
903 frame_addr = get_sigframe(ka, env, sizeof(*frame));
904 trace_user_setup_frame(env, frame_addr);
906 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
907 goto give_sigsegv;
909 __put_user(sig, &frame->sig);
911 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
912 frame_addr + offsetof(struct sigframe, fpstate));
914 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
915 __put_user(set->sig[i], &frame->extramask[i - 1]);
918 /* Set up to return from userspace. If provided, use a stub
919 already in userspace. */
920 if (ka->sa_flags & TARGET_SA_RESTORER) {
921 __put_user(ka->sa_restorer, &frame->pretcode);
922 } else {
923 uint16_t val16;
924 abi_ulong retcode_addr;
925 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
926 __put_user(retcode_addr, &frame->pretcode);
927 /* This is popl %eax ; movl $,%eax ; int $0x80 */
928 val16 = 0xb858;
929 __put_user(val16, (uint16_t *)(frame->retcode+0));
930 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
931 val16 = 0x80cd;
932 __put_user(val16, (uint16_t *)(frame->retcode+6));
936 /* Set up registers for signal handler */
937 env->regs[R_ESP] = frame_addr;
938 env->eip = ka->_sa_handler;
940 cpu_x86_load_seg(env, R_DS, __USER_DS);
941 cpu_x86_load_seg(env, R_ES, __USER_DS);
942 cpu_x86_load_seg(env, R_SS, __USER_DS);
943 cpu_x86_load_seg(env, R_CS, __USER_CS);
944 env->eflags &= ~TF_MASK;
946 unlock_user_struct(frame, frame_addr, 1);
948 return;
950 give_sigsegv:
951 if (sig == TARGET_SIGSEGV)
952 ka->_sa_handler = TARGET_SIG_DFL;
953 force_sig(TARGET_SIGSEGV /* , current */);
956 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
957 static void setup_rt_frame(int sig, struct target_sigaction *ka,
958 target_siginfo_t *info,
959 target_sigset_t *set, CPUX86State *env)
961 abi_ulong frame_addr, addr;
962 struct rt_sigframe *frame;
963 int i;
965 frame_addr = get_sigframe(ka, env, sizeof(*frame));
966 trace_user_setup_rt_frame(env, frame_addr);
968 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
969 goto give_sigsegv;
971 __put_user(sig, &frame->sig);
972 addr = frame_addr + offsetof(struct rt_sigframe, info);
973 __put_user(addr, &frame->pinfo);
974 addr = frame_addr + offsetof(struct rt_sigframe, uc);
975 __put_user(addr, &frame->puc);
976 tswap_siginfo(&frame->info, info);
978 /* Create the ucontext. */
979 __put_user(0, &frame->uc.tuc_flags);
980 __put_user(0, &frame->uc.tuc_link);
981 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
982 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
983 &frame->uc.tuc_stack.ss_flags);
984 __put_user(target_sigaltstack_used.ss_size,
985 &frame->uc.tuc_stack.ss_size);
986 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env,
987 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate));
989 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
990 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
993 /* Set up to return from userspace. If provided, use a stub
994 already in userspace. */
995 if (ka->sa_flags & TARGET_SA_RESTORER) {
996 __put_user(ka->sa_restorer, &frame->pretcode);
997 } else {
998 uint16_t val16;
999 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
1000 __put_user(addr, &frame->pretcode);
1001 /* This is movl $,%eax ; int $0x80 */
1002 __put_user(0xb8, (char *)(frame->retcode+0));
1003 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
1004 val16 = 0x80cd;
1005 __put_user(val16, (uint16_t *)(frame->retcode+5));
1008 /* Set up registers for signal handler */
1009 env->regs[R_ESP] = frame_addr;
1010 env->eip = ka->_sa_handler;
1012 cpu_x86_load_seg(env, R_DS, __USER_DS);
1013 cpu_x86_load_seg(env, R_ES, __USER_DS);
1014 cpu_x86_load_seg(env, R_SS, __USER_DS);
1015 cpu_x86_load_seg(env, R_CS, __USER_CS);
1016 env->eflags &= ~TF_MASK;
1018 unlock_user_struct(frame, frame_addr, 1);
1020 return;
1022 give_sigsegv:
1023 if (sig == TARGET_SIGSEGV)
1024 ka->_sa_handler = TARGET_SIG_DFL;
1025 force_sig(TARGET_SIGSEGV /* , current */);
1028 static int
1029 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
1031 unsigned int err = 0;
1032 abi_ulong fpstate_addr;
1033 unsigned int tmpflags;
1035 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1036 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1037 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1038 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1040 env->regs[R_EDI] = tswapl(sc->edi);
1041 env->regs[R_ESI] = tswapl(sc->esi);
1042 env->regs[R_EBP] = tswapl(sc->ebp);
1043 env->regs[R_ESP] = tswapl(sc->esp);
1044 env->regs[R_EBX] = tswapl(sc->ebx);
1045 env->regs[R_EDX] = tswapl(sc->edx);
1046 env->regs[R_ECX] = tswapl(sc->ecx);
1047 env->eip = tswapl(sc->eip);
1049 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1050 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1052 tmpflags = tswapl(sc->eflags);
1053 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1054 // regs->orig_eax = -1; /* disable syscall checks */
1056 fpstate_addr = tswapl(sc->fpstate);
1057 if (fpstate_addr != 0) {
1058 if (!access_ok(VERIFY_READ, fpstate_addr,
1059 sizeof(struct target_fpstate)))
1060 goto badframe;
1061 cpu_x86_frstor(env, fpstate_addr, 1);
1064 *peax = tswapl(sc->eax);
1065 return err;
1066 badframe:
1067 return 1;
1070 long do_sigreturn(CPUX86State *env)
1072 struct sigframe *frame;
1073 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1074 target_sigset_t target_set;
1075 sigset_t set;
1076 int eax, i;
1078 trace_user_do_sigreturn(env, frame_addr);
1079 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1080 goto badframe;
1081 /* set blocked signals */
1082 __get_user(target_set.sig[0], &frame->sc.oldmask);
1083 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1084 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
1087 target_to_host_sigset_internal(&set, &target_set);
1088 do_sigprocmask(SIG_SETMASK, &set, NULL);
1090 /* restore registers */
1091 if (restore_sigcontext(env, &frame->sc, &eax))
1092 goto badframe;
1093 unlock_user_struct(frame, frame_addr, 0);
1094 return eax;
1096 badframe:
1097 unlock_user_struct(frame, frame_addr, 0);
1098 force_sig(TARGET_SIGSEGV);
1099 return 0;
1102 long do_rt_sigreturn(CPUX86State *env)
1104 abi_ulong frame_addr;
1105 struct rt_sigframe *frame;
1106 sigset_t set;
1107 int eax;
1109 frame_addr = env->regs[R_ESP] - 4;
1110 trace_user_do_rt_sigreturn(env, frame_addr);
1111 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1112 goto badframe;
1113 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1114 do_sigprocmask(SIG_SETMASK, &set, NULL);
1116 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1117 goto badframe;
1119 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1120 get_sp_from_cpustate(env)) == -EFAULT)
1121 goto badframe;
1123 unlock_user_struct(frame, frame_addr, 0);
1124 return eax;
1126 badframe:
1127 unlock_user_struct(frame, frame_addr, 0);
1128 force_sig(TARGET_SIGSEGV);
1129 return 0;
1132 #elif defined(TARGET_AARCH64)
1134 struct target_sigcontext {
1135 uint64_t fault_address;
1136 /* AArch64 registers */
1137 uint64_t regs[31];
1138 uint64_t sp;
1139 uint64_t pc;
1140 uint64_t pstate;
1141 /* 4K reserved for FP/SIMD state and future expansion */
1142 char __reserved[4096] __attribute__((__aligned__(16)));
1145 struct target_ucontext {
1146 abi_ulong tuc_flags;
1147 abi_ulong tuc_link;
1148 target_stack_t tuc_stack;
1149 target_sigset_t tuc_sigmask;
1150 /* glibc uses a 1024-bit sigset_t */
1151 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1152 /* last for future expansion */
1153 struct target_sigcontext tuc_mcontext;
1157 * Header to be used at the beginning of structures extending the user
1158 * context. Such structures must be placed after the rt_sigframe on the stack
1159 * and be 16-byte aligned. The last structure must be a dummy one with the
1160 * magic and size set to 0.
1162 struct target_aarch64_ctx {
1163 uint32_t magic;
1164 uint32_t size;
1167 #define TARGET_FPSIMD_MAGIC 0x46508001
1169 struct target_fpsimd_context {
1170 struct target_aarch64_ctx head;
1171 uint32_t fpsr;
1172 uint32_t fpcr;
1173 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1177 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1178 * user space as it will change with the addition of new context. User space
1179 * should check the magic/size information.
1181 struct target_aux_context {
1182 struct target_fpsimd_context fpsimd;
1183 /* additional context to be added before "end" */
1184 struct target_aarch64_ctx end;
1187 struct target_rt_sigframe {
1188 struct target_siginfo info;
1189 struct target_ucontext uc;
1190 uint64_t fp;
1191 uint64_t lr;
1192 uint32_t tramp[2];
1195 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1196 CPUARMState *env, target_sigset_t *set)
1198 int i;
1199 struct target_aux_context *aux =
1200 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1202 /* set up the stack frame for unwinding */
1203 __put_user(env->xregs[29], &sf->fp);
1204 __put_user(env->xregs[30], &sf->lr);
1206 for (i = 0; i < 31; i++) {
1207 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1209 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1210 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
1211 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
1213 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
1215 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1216 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1219 for (i = 0; i < 32; i++) {
1220 #ifdef TARGET_WORDS_BIGENDIAN
1221 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1222 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1223 #else
1224 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1225 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1226 #endif
1228 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1229 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1230 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1231 __put_user(sizeof(struct target_fpsimd_context),
1232 &aux->fpsimd.head.size);
1234 /* set the "end" magic */
1235 __put_user(0, &aux->end.magic);
1236 __put_user(0, &aux->end.size);
1238 return 0;
1241 static int target_restore_sigframe(CPUARMState *env,
1242 struct target_rt_sigframe *sf)
1244 sigset_t set;
1245 int i;
1246 struct target_aux_context *aux =
1247 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1248 uint32_t magic, size, fpsr, fpcr;
1249 uint64_t pstate;
1251 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1252 do_sigprocmask(SIG_SETMASK, &set, NULL);
1254 for (i = 0; i < 31; i++) {
1255 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1258 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1259 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1260 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1261 pstate_write(env, pstate);
1263 __get_user(magic, &aux->fpsimd.head.magic);
1264 __get_user(size, &aux->fpsimd.head.size);
1266 if (magic != TARGET_FPSIMD_MAGIC
1267 || size != sizeof(struct target_fpsimd_context)) {
1268 return 1;
1271 for (i = 0; i < 32; i++) {
1272 #ifdef TARGET_WORDS_BIGENDIAN
1273 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1274 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1275 #else
1276 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1277 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1278 #endif
1280 __get_user(fpsr, &aux->fpsimd.fpsr);
1281 vfp_set_fpsr(env, fpsr);
1282 __get_user(fpcr, &aux->fpsimd.fpcr);
1283 vfp_set_fpcr(env, fpcr);
1285 return 0;
1288 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1290 abi_ulong sp;
1292 sp = env->xregs[31];
1295 * This is the X/Open sanctioned signal stack switching.
1297 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1298 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1301 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1303 return sp;
1306 static void target_setup_frame(int usig, struct target_sigaction *ka,
1307 target_siginfo_t *info, target_sigset_t *set,
1308 CPUARMState *env)
1310 struct target_rt_sigframe *frame;
1311 abi_ulong frame_addr, return_addr;
1313 frame_addr = get_sigframe(ka, env);
1314 trace_user_setup_frame(env, frame_addr);
1315 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1316 goto give_sigsegv;
1319 __put_user(0, &frame->uc.tuc_flags);
1320 __put_user(0, &frame->uc.tuc_link);
1322 __put_user(target_sigaltstack_used.ss_sp,
1323 &frame->uc.tuc_stack.ss_sp);
1324 __put_user(sas_ss_flags(env->xregs[31]),
1325 &frame->uc.tuc_stack.ss_flags);
1326 __put_user(target_sigaltstack_used.ss_size,
1327 &frame->uc.tuc_stack.ss_size);
1328 target_setup_sigframe(frame, env, set);
1329 if (ka->sa_flags & TARGET_SA_RESTORER) {
1330 return_addr = ka->sa_restorer;
1331 } else {
1332 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1333 __put_user(0xd2801168, &frame->tramp[0]);
1334 __put_user(0xd4000001, &frame->tramp[1]);
1335 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1337 env->xregs[0] = usig;
1338 env->xregs[31] = frame_addr;
1339 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1340 env->pc = ka->_sa_handler;
1341 env->xregs[30] = return_addr;
1342 if (info) {
1343 tswap_siginfo(&frame->info, info);
1344 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1345 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1348 unlock_user_struct(frame, frame_addr, 1);
1349 return;
1351 give_sigsegv:
1352 unlock_user_struct(frame, frame_addr, 1);
1353 force_sig(TARGET_SIGSEGV);
1356 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1357 target_siginfo_t *info, target_sigset_t *set,
1358 CPUARMState *env)
1360 target_setup_frame(sig, ka, info, set, env);
1363 static void setup_frame(int sig, struct target_sigaction *ka,
1364 target_sigset_t *set, CPUARMState *env)
1366 target_setup_frame(sig, ka, 0, set, env);
1369 long do_rt_sigreturn(CPUARMState *env)
1371 struct target_rt_sigframe *frame = NULL;
1372 abi_ulong frame_addr = env->xregs[31];
1374 trace_user_do_rt_sigreturn(env, frame_addr);
1375 if (frame_addr & 15) {
1376 goto badframe;
1379 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1380 goto badframe;
1383 if (target_restore_sigframe(env, frame)) {
1384 goto badframe;
1387 if (do_sigaltstack(frame_addr +
1388 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1389 0, get_sp_from_cpustate(env)) == -EFAULT) {
1390 goto badframe;
1393 unlock_user_struct(frame, frame_addr, 0);
1394 return env->xregs[0];
1396 badframe:
1397 unlock_user_struct(frame, frame_addr, 0);
1398 force_sig(TARGET_SIGSEGV);
1399 return 0;
1402 long do_sigreturn(CPUARMState *env)
1404 return do_rt_sigreturn(env);
1407 #elif defined(TARGET_ARM)
1409 struct target_sigcontext {
1410 abi_ulong trap_no;
1411 abi_ulong error_code;
1412 abi_ulong oldmask;
1413 abi_ulong arm_r0;
1414 abi_ulong arm_r1;
1415 abi_ulong arm_r2;
1416 abi_ulong arm_r3;
1417 abi_ulong arm_r4;
1418 abi_ulong arm_r5;
1419 abi_ulong arm_r6;
1420 abi_ulong arm_r7;
1421 abi_ulong arm_r8;
1422 abi_ulong arm_r9;
1423 abi_ulong arm_r10;
1424 abi_ulong arm_fp;
1425 abi_ulong arm_ip;
1426 abi_ulong arm_sp;
1427 abi_ulong arm_lr;
1428 abi_ulong arm_pc;
1429 abi_ulong arm_cpsr;
1430 abi_ulong fault_address;
1433 struct target_ucontext_v1 {
1434 abi_ulong tuc_flags;
1435 abi_ulong tuc_link;
1436 target_stack_t tuc_stack;
1437 struct target_sigcontext tuc_mcontext;
1438 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1441 struct target_ucontext_v2 {
1442 abi_ulong tuc_flags;
1443 abi_ulong tuc_link;
1444 target_stack_t tuc_stack;
1445 struct target_sigcontext tuc_mcontext;
1446 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1447 char __unused[128 - sizeof(target_sigset_t)];
1448 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1451 struct target_user_vfp {
1452 uint64_t fpregs[32];
1453 abi_ulong fpscr;
1456 struct target_user_vfp_exc {
1457 abi_ulong fpexc;
1458 abi_ulong fpinst;
1459 abi_ulong fpinst2;
1462 struct target_vfp_sigframe {
1463 abi_ulong magic;
1464 abi_ulong size;
1465 struct target_user_vfp ufp;
1466 struct target_user_vfp_exc ufp_exc;
1467 } __attribute__((__aligned__(8)));
1469 struct target_iwmmxt_sigframe {
1470 abi_ulong magic;
1471 abi_ulong size;
1472 uint64_t regs[16];
1473 /* Note that not all the coprocessor control registers are stored here */
1474 uint32_t wcssf;
1475 uint32_t wcasf;
1476 uint32_t wcgr0;
1477 uint32_t wcgr1;
1478 uint32_t wcgr2;
1479 uint32_t wcgr3;
1480 } __attribute__((__aligned__(8)));
1482 #define TARGET_VFP_MAGIC 0x56465001
1483 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1485 struct sigframe_v1
1487 struct target_sigcontext sc;
1488 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1489 abi_ulong retcode;
1492 struct sigframe_v2
1494 struct target_ucontext_v2 uc;
1495 abi_ulong retcode;
1498 struct rt_sigframe_v1
1500 abi_ulong pinfo;
1501 abi_ulong puc;
1502 struct target_siginfo info;
1503 struct target_ucontext_v1 uc;
1504 abi_ulong retcode;
1507 struct rt_sigframe_v2
1509 struct target_siginfo info;
1510 struct target_ucontext_v2 uc;
1511 abi_ulong retcode;
1514 #define TARGET_CONFIG_CPU_32 1
1517 * For ARM syscalls, we encode the syscall number into the instruction.
1519 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1520 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1523 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1524 * need two 16-bit instructions.
1526 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1527 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1529 static const abi_ulong retcodes[4] = {
1530 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1531 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1535 static inline int valid_user_regs(CPUARMState *regs)
1537 return 1;
1540 static void
1541 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1542 CPUARMState *env, abi_ulong mask)
1544 __put_user(env->regs[0], &sc->arm_r0);
1545 __put_user(env->regs[1], &sc->arm_r1);
1546 __put_user(env->regs[2], &sc->arm_r2);
1547 __put_user(env->regs[3], &sc->arm_r3);
1548 __put_user(env->regs[4], &sc->arm_r4);
1549 __put_user(env->regs[5], &sc->arm_r5);
1550 __put_user(env->regs[6], &sc->arm_r6);
1551 __put_user(env->regs[7], &sc->arm_r7);
1552 __put_user(env->regs[8], &sc->arm_r8);
1553 __put_user(env->regs[9], &sc->arm_r9);
1554 __put_user(env->regs[10], &sc->arm_r10);
1555 __put_user(env->regs[11], &sc->arm_fp);
1556 __put_user(env->regs[12], &sc->arm_ip);
1557 __put_user(env->regs[13], &sc->arm_sp);
1558 __put_user(env->regs[14], &sc->arm_lr);
1559 __put_user(env->regs[15], &sc->arm_pc);
1560 #ifdef TARGET_CONFIG_CPU_32
1561 __put_user(cpsr_read(env), &sc->arm_cpsr);
1562 #endif
1564 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1565 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1566 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1567 __put_user(mask, &sc->oldmask);
1570 static inline abi_ulong
1571 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1573 unsigned long sp = regs->regs[13];
1576 * This is the X/Open sanctioned signal stack switching.
1578 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1579 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;
1610 __put_user(retcodes[idx], rc);
1612 retcode = rc_addr + thumb;
1615 env->regs[0] = usig;
1616 env->regs[13] = frame_addr;
1617 env->regs[14] = retcode;
1618 env->regs[15] = handler & (thumb ? ~1 : ~3);
1619 cpsr_write(env, cpsr, 0xffffffff);
1622 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1624 int i;
1625 struct target_vfp_sigframe *vfpframe;
1626 vfpframe = (struct target_vfp_sigframe *)regspace;
1627 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1628 __put_user(sizeof(*vfpframe), &vfpframe->size);
1629 for (i = 0; i < 32; i++) {
1630 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1632 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1633 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1634 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1635 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1636 return (abi_ulong*)(vfpframe+1);
1639 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1640 CPUARMState *env)
1642 int i;
1643 struct target_iwmmxt_sigframe *iwmmxtframe;
1644 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1645 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1646 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1647 for (i = 0; i < 16; i++) {
1648 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1650 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1651 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1652 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1653 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1654 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1655 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1656 return (abi_ulong*)(iwmmxtframe+1);
1659 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1660 target_sigset_t *set, CPUARMState *env)
1662 struct target_sigaltstack stack;
1663 int i;
1664 abi_ulong *regspace;
1666 /* Clear all the bits of the ucontext we don't use. */
1667 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1669 memset(&stack, 0, sizeof(stack));
1670 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1671 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1672 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1673 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1675 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1676 /* Save coprocessor signal frame. */
1677 regspace = uc->tuc_regspace;
1678 if (arm_feature(env, ARM_FEATURE_VFP)) {
1679 regspace = setup_sigframe_v2_vfp(regspace, env);
1681 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1682 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1685 /* Write terminating magic word */
1686 __put_user(0, regspace);
1688 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1689 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1693 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1694 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1695 target_sigset_t *set, CPUARMState *regs)
1697 struct sigframe_v1 *frame;
1698 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1699 int i;
1701 trace_user_setup_frame(regs, frame_addr);
1702 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1703 return;
1705 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1707 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1708 __put_user(set->sig[i], &frame->extramask[i - 1]);
1711 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1712 frame_addr + offsetof(struct sigframe_v1, retcode));
1714 unlock_user_struct(frame, frame_addr, 1);
1717 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1718 target_sigset_t *set, CPUARMState *regs)
1720 struct sigframe_v2 *frame;
1721 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1723 trace_user_setup_frame(regs, frame_addr);
1724 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1725 return;
1727 setup_sigframe_v2(&frame->uc, set, regs);
1729 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1730 frame_addr + offsetof(struct sigframe_v2, retcode));
1732 unlock_user_struct(frame, frame_addr, 1);
1735 static void setup_frame(int usig, struct target_sigaction *ka,
1736 target_sigset_t *set, CPUARMState *regs)
1738 if (get_osversion() >= 0x020612) {
1739 setup_frame_v2(usig, ka, set, regs);
1740 } else {
1741 setup_frame_v1(usig, ka, set, regs);
1745 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1746 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1747 target_siginfo_t *info,
1748 target_sigset_t *set, CPUARMState *env)
1750 struct rt_sigframe_v1 *frame;
1751 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1752 struct target_sigaltstack stack;
1753 int i;
1754 abi_ulong info_addr, uc_addr;
1756 trace_user_setup_rt_frame(env, frame_addr);
1757 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1758 return /* 1 */;
1760 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1761 __put_user(info_addr, &frame->pinfo);
1762 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1763 __put_user(uc_addr, &frame->puc);
1764 tswap_siginfo(&frame->info, info);
1766 /* Clear all the bits of the ucontext we don't use. */
1767 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1769 memset(&stack, 0, sizeof(stack));
1770 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1771 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1772 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1773 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1775 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1776 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1777 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
1780 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1781 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1783 env->regs[1] = info_addr;
1784 env->regs[2] = uc_addr;
1786 unlock_user_struct(frame, frame_addr, 1);
1789 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1790 target_siginfo_t *info,
1791 target_sigset_t *set, CPUARMState *env)
1793 struct rt_sigframe_v2 *frame;
1794 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1795 abi_ulong info_addr, uc_addr;
1797 trace_user_setup_rt_frame(env, frame_addr);
1798 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1799 return /* 1 */;
1801 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1802 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1803 tswap_siginfo(&frame->info, info);
1805 setup_sigframe_v2(&frame->uc, set, env);
1807 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1808 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1810 env->regs[1] = info_addr;
1811 env->regs[2] = uc_addr;
1813 unlock_user_struct(frame, frame_addr, 1);
1816 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1817 target_siginfo_t *info,
1818 target_sigset_t *set, CPUARMState *env)
1820 if (get_osversion() >= 0x020612) {
1821 setup_rt_frame_v2(usig, ka, info, set, env);
1822 } else {
1823 setup_rt_frame_v1(usig, ka, info, set, env);
1827 static int
1828 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1830 int err = 0;
1831 uint32_t cpsr;
1833 __get_user(env->regs[0], &sc->arm_r0);
1834 __get_user(env->regs[1], &sc->arm_r1);
1835 __get_user(env->regs[2], &sc->arm_r2);
1836 __get_user(env->regs[3], &sc->arm_r3);
1837 __get_user(env->regs[4], &sc->arm_r4);
1838 __get_user(env->regs[5], &sc->arm_r5);
1839 __get_user(env->regs[6], &sc->arm_r6);
1840 __get_user(env->regs[7], &sc->arm_r7);
1841 __get_user(env->regs[8], &sc->arm_r8);
1842 __get_user(env->regs[9], &sc->arm_r9);
1843 __get_user(env->regs[10], &sc->arm_r10);
1844 __get_user(env->regs[11], &sc->arm_fp);
1845 __get_user(env->regs[12], &sc->arm_ip);
1846 __get_user(env->regs[13], &sc->arm_sp);
1847 __get_user(env->regs[14], &sc->arm_lr);
1848 __get_user(env->regs[15], &sc->arm_pc);
1849 #ifdef TARGET_CONFIG_CPU_32
1850 __get_user(cpsr, &sc->arm_cpsr);
1851 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1852 #endif
1854 err |= !valid_user_regs(env);
1856 return err;
1859 static long do_sigreturn_v1(CPUARMState *env)
1861 abi_ulong frame_addr;
1862 struct sigframe_v1 *frame = NULL;
1863 target_sigset_t set;
1864 sigset_t host_set;
1865 int i;
1868 * Since we stacked the signal on a 64-bit boundary,
1869 * then 'sp' should be word aligned here. If it's
1870 * not, then the user is trying to mess with us.
1872 frame_addr = env->regs[13];
1873 trace_user_do_sigreturn(env, frame_addr);
1874 if (frame_addr & 7) {
1875 goto badframe;
1878 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1879 goto badframe;
1881 __get_user(set.sig[0], &frame->sc.oldmask);
1882 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1883 __get_user(set.sig[i], &frame->extramask[i - 1]);
1886 target_to_host_sigset_internal(&host_set, &set);
1887 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1889 if (restore_sigcontext(env, &frame->sc))
1890 goto badframe;
1892 #if 0
1893 /* Send SIGTRAP if we're single-stepping */
1894 if (ptrace_cancel_bpt(current))
1895 send_sig(SIGTRAP, current, 1);
1896 #endif
1897 unlock_user_struct(frame, frame_addr, 0);
1898 return env->regs[0];
1900 badframe:
1901 force_sig(TARGET_SIGSEGV /* , current */);
1902 return 0;
1905 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1907 int i;
1908 abi_ulong magic, sz;
1909 uint32_t fpscr, fpexc;
1910 struct target_vfp_sigframe *vfpframe;
1911 vfpframe = (struct target_vfp_sigframe *)regspace;
1913 __get_user(magic, &vfpframe->magic);
1914 __get_user(sz, &vfpframe->size);
1915 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1916 return 0;
1918 for (i = 0; i < 32; i++) {
1919 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1921 __get_user(fpscr, &vfpframe->ufp.fpscr);
1922 vfp_set_fpscr(env, fpscr);
1923 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1924 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1925 * and the exception flag is cleared
1927 fpexc |= (1 << 30);
1928 fpexc &= ~((1 << 31) | (1 << 28));
1929 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1930 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1931 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1932 return (abi_ulong*)(vfpframe + 1);
1935 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1936 abi_ulong *regspace)
1938 int i;
1939 abi_ulong magic, sz;
1940 struct target_iwmmxt_sigframe *iwmmxtframe;
1941 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1943 __get_user(magic, &iwmmxtframe->magic);
1944 __get_user(sz, &iwmmxtframe->size);
1945 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1946 return 0;
1948 for (i = 0; i < 16; i++) {
1949 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1951 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1952 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1953 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1954 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1955 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1956 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1957 return (abi_ulong*)(iwmmxtframe + 1);
1960 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1961 struct target_ucontext_v2 *uc)
1963 sigset_t host_set;
1964 abi_ulong *regspace;
1966 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1967 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
1969 if (restore_sigcontext(env, &uc->tuc_mcontext))
1970 return 1;
1972 /* Restore coprocessor signal frame */
1973 regspace = uc->tuc_regspace;
1974 if (arm_feature(env, ARM_FEATURE_VFP)) {
1975 regspace = restore_sigframe_v2_vfp(env, regspace);
1976 if (!regspace) {
1977 return 1;
1980 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1981 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1982 if (!regspace) {
1983 return 1;
1987 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1988 return 1;
1990 #if 0
1991 /* Send SIGTRAP if we're single-stepping */
1992 if (ptrace_cancel_bpt(current))
1993 send_sig(SIGTRAP, current, 1);
1994 #endif
1996 return 0;
1999 static long do_sigreturn_v2(CPUARMState *env)
2001 abi_ulong frame_addr;
2002 struct sigframe_v2 *frame = NULL;
2005 * Since we stacked the signal on a 64-bit boundary,
2006 * then 'sp' should be word aligned here. If it's
2007 * not, then the user is trying to mess with us.
2009 frame_addr = env->regs[13];
2010 trace_user_do_sigreturn(env, frame_addr);
2011 if (frame_addr & 7) {
2012 goto badframe;
2015 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2016 goto badframe;
2018 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2019 goto badframe;
2021 unlock_user_struct(frame, frame_addr, 0);
2022 return env->regs[0];
2024 badframe:
2025 unlock_user_struct(frame, frame_addr, 0);
2026 force_sig(TARGET_SIGSEGV /* , current */);
2027 return 0;
2030 long do_sigreturn(CPUARMState *env)
2032 if (get_osversion() >= 0x020612) {
2033 return do_sigreturn_v2(env);
2034 } else {
2035 return do_sigreturn_v1(env);
2039 static long do_rt_sigreturn_v1(CPUARMState *env)
2041 abi_ulong frame_addr;
2042 struct rt_sigframe_v1 *frame = NULL;
2043 sigset_t host_set;
2046 * Since we stacked the signal on a 64-bit boundary,
2047 * then 'sp' should be word aligned here. If it's
2048 * not, then the user is trying to mess with us.
2050 frame_addr = env->regs[13];
2051 trace_user_do_rt_sigreturn(env, frame_addr);
2052 if (frame_addr & 7) {
2053 goto badframe;
2056 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2057 goto badframe;
2059 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2060 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2062 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
2063 goto badframe;
2065 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2066 goto badframe;
2068 #if 0
2069 /* Send SIGTRAP if we're single-stepping */
2070 if (ptrace_cancel_bpt(current))
2071 send_sig(SIGTRAP, current, 1);
2072 #endif
2073 unlock_user_struct(frame, frame_addr, 0);
2074 return env->regs[0];
2076 badframe:
2077 unlock_user_struct(frame, frame_addr, 0);
2078 force_sig(TARGET_SIGSEGV /* , current */);
2079 return 0;
2082 static long do_rt_sigreturn_v2(CPUARMState *env)
2084 abi_ulong frame_addr;
2085 struct rt_sigframe_v2 *frame = NULL;
2088 * Since we stacked the signal on a 64-bit boundary,
2089 * then 'sp' should be word aligned here. If it's
2090 * not, then the user is trying to mess with us.
2092 frame_addr = env->regs[13];
2093 trace_user_do_rt_sigreturn(env, frame_addr);
2094 if (frame_addr & 7) {
2095 goto badframe;
2098 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2099 goto badframe;
2101 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2102 goto badframe;
2104 unlock_user_struct(frame, frame_addr, 0);
2105 return env->regs[0];
2107 badframe:
2108 unlock_user_struct(frame, frame_addr, 0);
2109 force_sig(TARGET_SIGSEGV /* , current */);
2110 return 0;
2113 long do_rt_sigreturn(CPUARMState *env)
2115 if (get_osversion() >= 0x020612) {
2116 return do_rt_sigreturn_v2(env);
2117 } else {
2118 return do_rt_sigreturn_v1(env);
2122 #elif defined(TARGET_SPARC)
2124 #define __SUNOS_MAXWIN 31
2126 /* This is what SunOS does, so shall I. */
2127 struct target_sigcontext {
2128 abi_ulong sigc_onstack; /* state to restore */
2130 abi_ulong sigc_mask; /* sigmask to restore */
2131 abi_ulong sigc_sp; /* stack pointer */
2132 abi_ulong sigc_pc; /* program counter */
2133 abi_ulong sigc_npc; /* next program counter */
2134 abi_ulong sigc_psr; /* for condition codes etc */
2135 abi_ulong sigc_g1; /* User uses these two registers */
2136 abi_ulong sigc_o0; /* within the trampoline code. */
2138 /* Now comes information regarding the users window set
2139 * at the time of the signal.
2141 abi_ulong sigc_oswins; /* outstanding windows */
2143 /* stack ptrs for each regwin buf */
2144 char *sigc_spbuf[__SUNOS_MAXWIN];
2146 /* Windows to restore after signal */
2147 struct {
2148 abi_ulong locals[8];
2149 abi_ulong ins[8];
2150 } sigc_wbuf[__SUNOS_MAXWIN];
2152 /* A Sparc stack frame */
2153 struct sparc_stackf {
2154 abi_ulong locals[8];
2155 abi_ulong ins[8];
2156 /* It's simpler to treat fp and callers_pc as elements of ins[]
2157 * since we never need to access them ourselves.
2159 char *structptr;
2160 abi_ulong xargs[6];
2161 abi_ulong xxargs[1];
2164 typedef struct {
2165 struct {
2166 abi_ulong psr;
2167 abi_ulong pc;
2168 abi_ulong npc;
2169 abi_ulong y;
2170 abi_ulong u_regs[16]; /* globals and ins */
2171 } si_regs;
2172 int si_mask;
2173 } __siginfo_t;
2175 typedef struct {
2176 abi_ulong si_float_regs[32];
2177 unsigned long si_fsr;
2178 unsigned long si_fpqdepth;
2179 struct {
2180 unsigned long *insn_addr;
2181 unsigned long insn;
2182 } si_fpqueue [16];
2183 } qemu_siginfo_fpu_t;
2186 struct target_signal_frame {
2187 struct sparc_stackf ss;
2188 __siginfo_t info;
2189 abi_ulong fpu_save;
2190 abi_ulong insns[2] __attribute__ ((aligned (8)));
2191 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2192 abi_ulong extra_size; /* Should be 0 */
2193 qemu_siginfo_fpu_t fpu_state;
2195 struct target_rt_signal_frame {
2196 struct sparc_stackf ss;
2197 siginfo_t info;
2198 abi_ulong regs[20];
2199 sigset_t mask;
2200 abi_ulong fpu_save;
2201 unsigned int insns[2];
2202 stack_t stack;
2203 unsigned int extra_size; /* Should be 0 */
2204 qemu_siginfo_fpu_t fpu_state;
2207 #define UREG_O0 16
2208 #define UREG_O6 22
2209 #define UREG_I0 0
2210 #define UREG_I1 1
2211 #define UREG_I2 2
2212 #define UREG_I3 3
2213 #define UREG_I4 4
2214 #define UREG_I5 5
2215 #define UREG_I6 6
2216 #define UREG_I7 7
2217 #define UREG_L0 8
2218 #define UREG_FP UREG_I6
2219 #define UREG_SP UREG_O6
2221 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2222 CPUSPARCState *env,
2223 unsigned long framesize)
2225 abi_ulong sp;
2227 sp = env->regwptr[UREG_FP];
2229 /* This is the X/Open sanctioned signal stack switching. */
2230 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2231 if (!on_sig_stack(sp)
2232 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2233 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2235 return sp - framesize;
2238 static int
2239 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2241 int err = 0, i;
2243 __put_user(env->psr, &si->si_regs.psr);
2244 __put_user(env->pc, &si->si_regs.pc);
2245 __put_user(env->npc, &si->si_regs.npc);
2246 __put_user(env->y, &si->si_regs.y);
2247 for (i=0; i < 8; i++) {
2248 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2250 for (i=0; i < 8; i++) {
2251 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2253 __put_user(mask, &si->si_mask);
2254 return err;
2257 #if 0
2258 static int
2259 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2260 CPUSPARCState *env, unsigned long mask)
2262 int err = 0;
2264 __put_user(mask, &sc->sigc_mask);
2265 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2266 __put_user(env->pc, &sc->sigc_pc);
2267 __put_user(env->npc, &sc->sigc_npc);
2268 __put_user(env->psr, &sc->sigc_psr);
2269 __put_user(env->gregs[1], &sc->sigc_g1);
2270 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2272 return err;
2274 #endif
2275 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2277 static void setup_frame(int sig, struct target_sigaction *ka,
2278 target_sigset_t *set, CPUSPARCState *env)
2280 abi_ulong sf_addr;
2281 struct target_signal_frame *sf;
2282 int sigframe_size, err, i;
2284 /* 1. Make sure everything is clean */
2285 //synchronize_user_stack();
2287 sigframe_size = NF_ALIGNEDSZ;
2288 sf_addr = get_sigframe(ka, env, sigframe_size);
2289 trace_user_setup_frame(env, sf_addr);
2291 sf = lock_user(VERIFY_WRITE, sf_addr,
2292 sizeof(struct target_signal_frame), 0);
2293 if (!sf)
2294 goto sigsegv;
2296 #if 0
2297 if (invalid_frame_pointer(sf, sigframe_size))
2298 goto sigill_and_return;
2299 #endif
2300 /* 2. Save the current process state */
2301 err = setup___siginfo(&sf->info, env, set->sig[0]);
2302 __put_user(0, &sf->extra_size);
2304 //save_fpu_state(regs, &sf->fpu_state);
2305 //__put_user(&sf->fpu_state, &sf->fpu_save);
2307 __put_user(set->sig[0], &sf->info.si_mask);
2308 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2309 __put_user(set->sig[i + 1], &sf->extramask[i]);
2312 for (i = 0; i < 8; i++) {
2313 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2315 for (i = 0; i < 8; i++) {
2316 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2318 if (err)
2319 goto sigsegv;
2321 /* 3. signal handler back-trampoline and parameters */
2322 env->regwptr[UREG_FP] = sf_addr;
2323 env->regwptr[UREG_I0] = sig;
2324 env->regwptr[UREG_I1] = sf_addr +
2325 offsetof(struct target_signal_frame, info);
2326 env->regwptr[UREG_I2] = sf_addr +
2327 offsetof(struct target_signal_frame, info);
2329 /* 4. signal handler */
2330 env->pc = ka->_sa_handler;
2331 env->npc = (env->pc + 4);
2332 /* 5. return to kernel instructions */
2333 if (ka->sa_restorer)
2334 env->regwptr[UREG_I7] = ka->sa_restorer;
2335 else {
2336 uint32_t val32;
2338 env->regwptr[UREG_I7] = sf_addr +
2339 offsetof(struct target_signal_frame, insns) - 2 * 4;
2341 /* mov __NR_sigreturn, %g1 */
2342 val32 = 0x821020d8;
2343 __put_user(val32, &sf->insns[0]);
2345 /* t 0x10 */
2346 val32 = 0x91d02010;
2347 __put_user(val32, &sf->insns[1]);
2348 if (err)
2349 goto sigsegv;
2351 /* Flush instruction space. */
2352 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2353 // tb_flush(CPU(sparc_env_get_cpu(env)));
2355 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2356 return;
2357 #if 0
2358 sigill_and_return:
2359 force_sig(TARGET_SIGILL);
2360 #endif
2361 sigsegv:
2362 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2363 force_sig(TARGET_SIGSEGV);
2366 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2367 target_siginfo_t *info,
2368 target_sigset_t *set, CPUSPARCState *env)
2370 fprintf(stderr, "setup_rt_frame: not implemented\n");
2373 long do_sigreturn(CPUSPARCState *env)
2375 abi_ulong sf_addr;
2376 struct target_signal_frame *sf;
2377 uint32_t up_psr, pc, npc;
2378 target_sigset_t set;
2379 sigset_t host_set;
2380 int err=0, i;
2382 sf_addr = env->regwptr[UREG_FP];
2383 trace_user_do_sigreturn(env, sf_addr);
2384 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2385 goto segv_and_exit;
2387 /* 1. Make sure we are not getting garbage from the user */
2389 if (sf_addr & 3)
2390 goto segv_and_exit;
2392 __get_user(pc, &sf->info.si_regs.pc);
2393 __get_user(npc, &sf->info.si_regs.npc);
2395 if ((pc | npc) & 3)
2396 goto segv_and_exit;
2398 /* 2. Restore the state */
2399 __get_user(up_psr, &sf->info.si_regs.psr);
2401 /* User can only change condition codes and FPU enabling in %psr. */
2402 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2403 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2405 env->pc = pc;
2406 env->npc = npc;
2407 __get_user(env->y, &sf->info.si_regs.y);
2408 for (i=0; i < 8; i++) {
2409 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2411 for (i=0; i < 8; i++) {
2412 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2415 /* FIXME: implement FPU save/restore:
2416 * __get_user(fpu_save, &sf->fpu_save);
2417 * if (fpu_save)
2418 * err |= restore_fpu_state(env, fpu_save);
2421 /* This is pretty much atomic, no amount locking would prevent
2422 * the races which exist anyways.
2424 __get_user(set.sig[0], &sf->info.si_mask);
2425 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2426 __get_user(set.sig[i], &sf->extramask[i - 1]);
2429 target_to_host_sigset_internal(&host_set, &set);
2430 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
2432 if (err)
2433 goto segv_and_exit;
2434 unlock_user_struct(sf, sf_addr, 0);
2435 return env->regwptr[0];
2437 segv_and_exit:
2438 unlock_user_struct(sf, sf_addr, 0);
2439 force_sig(TARGET_SIGSEGV);
2442 long do_rt_sigreturn(CPUSPARCState *env)
2444 trace_user_do_rt_sigreturn(env, 0);
2445 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2446 return -TARGET_ENOSYS;
2449 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2450 #define MC_TSTATE 0
2451 #define MC_PC 1
2452 #define MC_NPC 2
2453 #define MC_Y 3
2454 #define MC_G1 4
2455 #define MC_G2 5
2456 #define MC_G3 6
2457 #define MC_G4 7
2458 #define MC_G5 8
2459 #define MC_G6 9
2460 #define MC_G7 10
2461 #define MC_O0 11
2462 #define MC_O1 12
2463 #define MC_O2 13
2464 #define MC_O3 14
2465 #define MC_O4 15
2466 #define MC_O5 16
2467 #define MC_O6 17
2468 #define MC_O7 18
2469 #define MC_NGREG 19
2471 typedef abi_ulong target_mc_greg_t;
2472 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2474 struct target_mc_fq {
2475 abi_ulong *mcfq_addr;
2476 uint32_t mcfq_insn;
2479 struct target_mc_fpu {
2480 union {
2481 uint32_t sregs[32];
2482 uint64_t dregs[32];
2483 //uint128_t qregs[16];
2484 } mcfpu_fregs;
2485 abi_ulong mcfpu_fsr;
2486 abi_ulong mcfpu_fprs;
2487 abi_ulong mcfpu_gsr;
2488 struct target_mc_fq *mcfpu_fq;
2489 unsigned char mcfpu_qcnt;
2490 unsigned char mcfpu_qentsz;
2491 unsigned char mcfpu_enab;
2493 typedef struct target_mc_fpu target_mc_fpu_t;
2495 typedef struct {
2496 target_mc_gregset_t mc_gregs;
2497 target_mc_greg_t mc_fp;
2498 target_mc_greg_t mc_i7;
2499 target_mc_fpu_t mc_fpregs;
2500 } target_mcontext_t;
2502 struct target_ucontext {
2503 struct target_ucontext *tuc_link;
2504 abi_ulong tuc_flags;
2505 target_sigset_t tuc_sigmask;
2506 target_mcontext_t tuc_mcontext;
2509 /* A V9 register window */
2510 struct target_reg_window {
2511 abi_ulong locals[8];
2512 abi_ulong ins[8];
2515 #define TARGET_STACK_BIAS 2047
2517 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2518 void sparc64_set_context(CPUSPARCState *env)
2520 abi_ulong ucp_addr;
2521 struct target_ucontext *ucp;
2522 target_mc_gregset_t *grp;
2523 abi_ulong pc, npc, tstate;
2524 abi_ulong fp, i7, w_addr;
2525 unsigned int i;
2527 ucp_addr = env->regwptr[UREG_I0];
2528 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2529 goto do_sigsegv;
2530 grp = &ucp->tuc_mcontext.mc_gregs;
2531 __get_user(pc, &((*grp)[MC_PC]));
2532 __get_user(npc, &((*grp)[MC_NPC]));
2533 if ((pc | npc) & 3)
2534 goto do_sigsegv;
2535 if (env->regwptr[UREG_I1]) {
2536 target_sigset_t target_set;
2537 sigset_t set;
2539 if (TARGET_NSIG_WORDS == 1) {
2540 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
2541 } else {
2542 abi_ulong *src, *dst;
2543 src = ucp->tuc_sigmask.sig;
2544 dst = target_set.sig;
2545 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2546 __get_user(*dst, src);
2549 target_to_host_sigset_internal(&set, &target_set);
2550 do_sigprocmask(SIG_SETMASK, &set, NULL);
2552 env->pc = pc;
2553 env->npc = npc;
2554 __get_user(env->y, &((*grp)[MC_Y]));
2555 __get_user(tstate, &((*grp)[MC_TSTATE]));
2556 env->asi = (tstate >> 24) & 0xff;
2557 cpu_put_ccr(env, tstate >> 32);
2558 cpu_put_cwp64(env, tstate & 0x1f);
2559 __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2560 __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2561 __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2562 __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2563 __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2564 __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2565 __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2566 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2567 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2568 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2569 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2570 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2571 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2572 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2573 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2575 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2576 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2578 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2579 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2580 abi_ulong) != 0)
2581 goto do_sigsegv;
2582 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2583 abi_ulong) != 0)
2584 goto do_sigsegv;
2585 /* FIXME this does not match how the kernel handles the FPU in
2586 * its sparc64_set_context implementation. In particular the FPU
2587 * is only restored if fenab is non-zero in:
2588 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2590 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2592 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2593 for (i = 0; i < 64; i++, src++) {
2594 if (i & 1) {
2595 __get_user(env->fpr[i/2].l.lower, src);
2596 } else {
2597 __get_user(env->fpr[i/2].l.upper, src);
2601 __get_user(env->fsr,
2602 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2603 __get_user(env->gsr,
2604 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2605 unlock_user_struct(ucp, ucp_addr, 0);
2606 return;
2607 do_sigsegv:
2608 unlock_user_struct(ucp, ucp_addr, 0);
2609 force_sig(TARGET_SIGSEGV);
2612 void sparc64_get_context(CPUSPARCState *env)
2614 abi_ulong ucp_addr;
2615 struct target_ucontext *ucp;
2616 target_mc_gregset_t *grp;
2617 target_mcontext_t *mcp;
2618 abi_ulong fp, i7, w_addr;
2619 int err;
2620 unsigned int i;
2621 target_sigset_t target_set;
2622 sigset_t set;
2624 ucp_addr = env->regwptr[UREG_I0];
2625 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2626 goto do_sigsegv;
2628 mcp = &ucp->tuc_mcontext;
2629 grp = &mcp->mc_gregs;
2631 /* Skip over the trap instruction, first. */
2632 env->pc = env->npc;
2633 env->npc += 4;
2635 err = 0;
2637 do_sigprocmask(0, NULL, &set);
2638 host_to_target_sigset_internal(&target_set, &set);
2639 if (TARGET_NSIG_WORDS == 1) {
2640 __put_user(target_set.sig[0],
2641 (abi_ulong *)&ucp->tuc_sigmask);
2642 } else {
2643 abi_ulong *src, *dst;
2644 src = target_set.sig;
2645 dst = ucp->tuc_sigmask.sig;
2646 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2647 __put_user(*src, dst);
2649 if (err)
2650 goto do_sigsegv;
2653 /* XXX: tstate must be saved properly */
2654 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2655 __put_user(env->pc, &((*grp)[MC_PC]));
2656 __put_user(env->npc, &((*grp)[MC_NPC]));
2657 __put_user(env->y, &((*grp)[MC_Y]));
2658 __put_user(env->gregs[1], &((*grp)[MC_G1]));
2659 __put_user(env->gregs[2], &((*grp)[MC_G2]));
2660 __put_user(env->gregs[3], &((*grp)[MC_G3]));
2661 __put_user(env->gregs[4], &((*grp)[MC_G4]));
2662 __put_user(env->gregs[5], &((*grp)[MC_G5]));
2663 __put_user(env->gregs[6], &((*grp)[MC_G6]));
2664 __put_user(env->gregs[7], &((*grp)[MC_G7]));
2665 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2666 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2667 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2668 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2669 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2670 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2671 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2672 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2674 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2675 fp = i7 = 0;
2676 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2677 abi_ulong) != 0)
2678 goto do_sigsegv;
2679 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2680 abi_ulong) != 0)
2681 goto do_sigsegv;
2682 __put_user(fp, &(mcp->mc_fp));
2683 __put_user(i7, &(mcp->mc_i7));
2686 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2687 for (i = 0; i < 64; i++, dst++) {
2688 if (i & 1) {
2689 __put_user(env->fpr[i/2].l.lower, dst);
2690 } else {
2691 __put_user(env->fpr[i/2].l.upper, dst);
2695 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2696 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2697 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2699 if (err)
2700 goto do_sigsegv;
2701 unlock_user_struct(ucp, ucp_addr, 1);
2702 return;
2703 do_sigsegv:
2704 unlock_user_struct(ucp, ucp_addr, 1);
2705 force_sig(TARGET_SIGSEGV);
2707 #endif
2708 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2710 # if defined(TARGET_ABI_MIPSO32)
2711 struct target_sigcontext {
2712 uint32_t sc_regmask; /* Unused */
2713 uint32_t sc_status;
2714 uint64_t sc_pc;
2715 uint64_t sc_regs[32];
2716 uint64_t sc_fpregs[32];
2717 uint32_t sc_ownedfp; /* Unused */
2718 uint32_t sc_fpc_csr;
2719 uint32_t sc_fpc_eir; /* Unused */
2720 uint32_t sc_used_math;
2721 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2722 uint32_t pad0;
2723 uint64_t sc_mdhi;
2724 uint64_t sc_mdlo;
2725 target_ulong sc_hi1; /* Was sc_cause */
2726 target_ulong sc_lo1; /* Was sc_badvaddr */
2727 target_ulong sc_hi2; /* Was sc_sigset[4] */
2728 target_ulong sc_lo2;
2729 target_ulong sc_hi3;
2730 target_ulong sc_lo3;
2732 # else /* N32 || N64 */
2733 struct target_sigcontext {
2734 uint64_t sc_regs[32];
2735 uint64_t sc_fpregs[32];
2736 uint64_t sc_mdhi;
2737 uint64_t sc_hi1;
2738 uint64_t sc_hi2;
2739 uint64_t sc_hi3;
2740 uint64_t sc_mdlo;
2741 uint64_t sc_lo1;
2742 uint64_t sc_lo2;
2743 uint64_t sc_lo3;
2744 uint64_t sc_pc;
2745 uint32_t sc_fpc_csr;
2746 uint32_t sc_used_math;
2747 uint32_t sc_dsp;
2748 uint32_t sc_reserved;
2750 # endif /* O32 */
2752 struct sigframe {
2753 uint32_t sf_ass[4]; /* argument save space for o32 */
2754 uint32_t sf_code[2]; /* signal trampoline */
2755 struct target_sigcontext sf_sc;
2756 target_sigset_t sf_mask;
2759 struct target_ucontext {
2760 target_ulong tuc_flags;
2761 target_ulong tuc_link;
2762 target_stack_t tuc_stack;
2763 target_ulong pad0;
2764 struct target_sigcontext tuc_mcontext;
2765 target_sigset_t tuc_sigmask;
2768 struct target_rt_sigframe {
2769 uint32_t rs_ass[4]; /* argument save space for o32 */
2770 uint32_t rs_code[2]; /* signal trampoline */
2771 struct target_siginfo rs_info;
2772 struct target_ucontext rs_uc;
2775 /* Install trampoline to jump back from signal handler */
2776 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2778 int err = 0;
2781 * Set up the return code ...
2783 * li v0, __NR__foo_sigreturn
2784 * syscall
2787 __put_user(0x24020000 + syscall, tramp + 0);
2788 __put_user(0x0000000c , tramp + 1);
2789 return err;
2792 static inline void setup_sigcontext(CPUMIPSState *regs,
2793 struct target_sigcontext *sc)
2795 int i;
2797 __put_user(exception_resume_pc(regs), &sc->sc_pc);
2798 regs->hflags &= ~MIPS_HFLAG_BMASK;
2800 __put_user(0, &sc->sc_regs[0]);
2801 for (i = 1; i < 32; ++i) {
2802 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2805 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2806 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2808 /* Rather than checking for dsp existence, always copy. The storage
2809 would just be garbage otherwise. */
2810 __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2811 __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2812 __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2813 __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2814 __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2815 __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2817 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2818 __put_user(dsp, &sc->sc_dsp);
2821 __put_user(1, &sc->sc_used_math);
2823 for (i = 0; i < 32; ++i) {
2824 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2828 static inline void
2829 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2831 int i;
2833 __get_user(regs->CP0_EPC, &sc->sc_pc);
2835 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2836 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2838 for (i = 1; i < 32; ++i) {
2839 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2842 __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2843 __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2844 __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2845 __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2846 __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2847 __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2849 uint32_t dsp;
2850 __get_user(dsp, &sc->sc_dsp);
2851 cpu_wrdsp(dsp, 0x3ff, regs);
2854 for (i = 0; i < 32; ++i) {
2855 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2860 * Determine which stack to use..
2862 static inline abi_ulong
2863 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2865 unsigned long sp;
2867 /* Default to using normal stack */
2868 sp = regs->active_tc.gpr[29];
2871 * FPU emulator may have its own trampoline active just
2872 * above the user stack, 16-bytes before the next lowest
2873 * 16 byte boundary. Try to avoid trashing it.
2875 sp -= 32;
2877 /* This is the X/Open sanctioned signal stack switching. */
2878 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2879 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2882 return (sp - frame_size) & ~7;
2885 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2887 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2888 env->hflags &= ~MIPS_HFLAG_M16;
2889 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2890 env->active_tc.PC &= ~(target_ulong) 1;
2894 # if defined(TARGET_ABI_MIPSO32)
2895 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2896 static void setup_frame(int sig, struct target_sigaction * ka,
2897 target_sigset_t *set, CPUMIPSState *regs)
2899 struct sigframe *frame;
2900 abi_ulong frame_addr;
2901 int i;
2903 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2904 trace_user_setup_frame(regs, frame_addr);
2905 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2906 goto give_sigsegv;
2908 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2910 setup_sigcontext(regs, &frame->sf_sc);
2912 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2913 __put_user(set->sig[i], &frame->sf_mask.sig[i]);
2917 * Arguments to signal handler:
2919 * a0 = signal number
2920 * a1 = 0 (should be cause)
2921 * a2 = pointer to struct sigcontext
2923 * $25 and PC point to the signal handler, $29 points to the
2924 * struct sigframe.
2926 regs->active_tc.gpr[ 4] = sig;
2927 regs->active_tc.gpr[ 5] = 0;
2928 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2929 regs->active_tc.gpr[29] = frame_addr;
2930 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2931 /* The original kernel code sets CP0_EPC to the handler
2932 * since it returns to userland using eret
2933 * we cannot do this here, and we must set PC directly */
2934 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2935 mips_set_hflags_isa_mode_from_pc(regs);
2936 unlock_user_struct(frame, frame_addr, 1);
2937 return;
2939 give_sigsegv:
2940 force_sig(TARGET_SIGSEGV/*, current*/);
2943 long do_sigreturn(CPUMIPSState *regs)
2945 struct sigframe *frame;
2946 abi_ulong frame_addr;
2947 sigset_t blocked;
2948 target_sigset_t target_set;
2949 int i;
2951 frame_addr = regs->active_tc.gpr[29];
2952 trace_user_do_sigreturn(regs, frame_addr);
2953 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2954 goto badframe;
2956 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2957 __get_user(target_set.sig[i], &frame->sf_mask.sig[i]);
2960 target_to_host_sigset_internal(&blocked, &target_set);
2961 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
2963 restore_sigcontext(regs, &frame->sf_sc);
2965 #if 0
2967 * Don't let your children do this ...
2969 __asm__ __volatile__(
2970 "move\t$29, %0\n\t"
2971 "j\tsyscall_exit"
2972 :/* no outputs */
2973 :"r" (&regs));
2974 /* Unreached */
2975 #endif
2977 regs->active_tc.PC = regs->CP0_EPC;
2978 mips_set_hflags_isa_mode_from_pc(regs);
2979 /* I am not sure this is right, but it seems to work
2980 * maybe a problem with nested signals ? */
2981 regs->CP0_EPC = 0;
2982 return -TARGET_QEMU_ESIGRETURN;
2984 badframe:
2985 force_sig(TARGET_SIGSEGV/*, current*/);
2986 return 0;
2988 # endif /* O32 */
2990 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2991 target_siginfo_t *info,
2992 target_sigset_t *set, CPUMIPSState *env)
2994 struct target_rt_sigframe *frame;
2995 abi_ulong frame_addr;
2996 int i;
2998 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2999 trace_user_setup_rt_frame(env, frame_addr);
3000 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3001 goto give_sigsegv;
3003 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3005 tswap_siginfo(&frame->rs_info, info);
3007 __put_user(0, &frame->rs_uc.tuc_flags);
3008 __put_user(0, &frame->rs_uc.tuc_link);
3009 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3010 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3011 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3012 &frame->rs_uc.tuc_stack.ss_flags);
3014 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3016 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3017 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3021 * Arguments to signal handler:
3023 * a0 = signal number
3024 * a1 = pointer to siginfo_t
3025 * a2 = pointer to struct ucontext
3027 * $25 and PC point to the signal handler, $29 points to the
3028 * struct sigframe.
3030 env->active_tc.gpr[ 4] = sig;
3031 env->active_tc.gpr[ 5] = frame_addr
3032 + offsetof(struct target_rt_sigframe, rs_info);
3033 env->active_tc.gpr[ 6] = frame_addr
3034 + offsetof(struct target_rt_sigframe, rs_uc);
3035 env->active_tc.gpr[29] = frame_addr;
3036 env->active_tc.gpr[31] = frame_addr
3037 + offsetof(struct target_rt_sigframe, rs_code);
3038 /* The original kernel code sets CP0_EPC to the handler
3039 * since it returns to userland using eret
3040 * we cannot do this here, and we must set PC directly */
3041 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3042 mips_set_hflags_isa_mode_from_pc(env);
3043 unlock_user_struct(frame, frame_addr, 1);
3044 return;
3046 give_sigsegv:
3047 unlock_user_struct(frame, frame_addr, 1);
3048 force_sig(TARGET_SIGSEGV/*, current*/);
3051 long do_rt_sigreturn(CPUMIPSState *env)
3053 struct target_rt_sigframe *frame;
3054 abi_ulong frame_addr;
3055 sigset_t blocked;
3057 frame_addr = env->active_tc.gpr[29];
3058 trace_user_do_rt_sigreturn(env, frame_addr);
3059 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3060 goto badframe;
3062 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3063 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3065 restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3067 if (do_sigaltstack(frame_addr +
3068 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3069 0, get_sp_from_cpustate(env)) == -EFAULT)
3070 goto badframe;
3072 env->active_tc.PC = env->CP0_EPC;
3073 mips_set_hflags_isa_mode_from_pc(env);
3074 /* I am not sure this is right, but it seems to work
3075 * maybe a problem with nested signals ? */
3076 env->CP0_EPC = 0;
3077 return -TARGET_QEMU_ESIGRETURN;
3079 badframe:
3080 force_sig(TARGET_SIGSEGV/*, current*/);
3081 return 0;
3084 #elif defined(TARGET_SH4)
3087 * code and data structures from linux kernel:
3088 * include/asm-sh/sigcontext.h
3089 * arch/sh/kernel/signal.c
3092 struct target_sigcontext {
3093 target_ulong oldmask;
3095 /* CPU registers */
3096 target_ulong sc_gregs[16];
3097 target_ulong sc_pc;
3098 target_ulong sc_pr;
3099 target_ulong sc_sr;
3100 target_ulong sc_gbr;
3101 target_ulong sc_mach;
3102 target_ulong sc_macl;
3104 /* FPU registers */
3105 target_ulong sc_fpregs[16];
3106 target_ulong sc_xfpregs[16];
3107 unsigned int sc_fpscr;
3108 unsigned int sc_fpul;
3109 unsigned int sc_ownedfp;
3112 struct target_sigframe
3114 struct target_sigcontext sc;
3115 target_ulong extramask[TARGET_NSIG_WORDS-1];
3116 uint16_t retcode[3];
3120 struct target_ucontext {
3121 target_ulong tuc_flags;
3122 struct target_ucontext *tuc_link;
3123 target_stack_t tuc_stack;
3124 struct target_sigcontext tuc_mcontext;
3125 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3128 struct target_rt_sigframe
3130 struct target_siginfo info;
3131 struct target_ucontext uc;
3132 uint16_t retcode[3];
3136 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3137 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3139 static abi_ulong get_sigframe(struct target_sigaction *ka,
3140 unsigned long sp, size_t frame_size)
3142 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3143 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3146 return (sp - frame_size) & -8ul;
3149 static void setup_sigcontext(struct target_sigcontext *sc,
3150 CPUSH4State *regs, unsigned long mask)
3152 int i;
3154 #define COPY(x) __put_user(regs->x, &sc->sc_##x)
3155 COPY(gregs[0]); COPY(gregs[1]);
3156 COPY(gregs[2]); COPY(gregs[3]);
3157 COPY(gregs[4]); COPY(gregs[5]);
3158 COPY(gregs[6]); COPY(gregs[7]);
3159 COPY(gregs[8]); COPY(gregs[9]);
3160 COPY(gregs[10]); COPY(gregs[11]);
3161 COPY(gregs[12]); COPY(gregs[13]);
3162 COPY(gregs[14]); COPY(gregs[15]);
3163 COPY(gbr); COPY(mach);
3164 COPY(macl); COPY(pr);
3165 COPY(sr); COPY(pc);
3166 #undef COPY
3168 for (i=0; i<16; i++) {
3169 __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3171 __put_user(regs->fpscr, &sc->sc_fpscr);
3172 __put_user(regs->fpul, &sc->sc_fpul);
3174 /* non-iBCS2 extensions.. */
3175 __put_user(mask, &sc->oldmask);
3178 static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3179 target_ulong *r0_p)
3181 int i;
3183 #define COPY(x) __get_user(regs->x, &sc->sc_##x)
3184 COPY(gregs[1]);
3185 COPY(gregs[2]); COPY(gregs[3]);
3186 COPY(gregs[4]); COPY(gregs[5]);
3187 COPY(gregs[6]); COPY(gregs[7]);
3188 COPY(gregs[8]); COPY(gregs[9]);
3189 COPY(gregs[10]); COPY(gregs[11]);
3190 COPY(gregs[12]); COPY(gregs[13]);
3191 COPY(gregs[14]); COPY(gregs[15]);
3192 COPY(gbr); COPY(mach);
3193 COPY(macl); COPY(pr);
3194 COPY(sr); COPY(pc);
3195 #undef COPY
3197 for (i=0; i<16; i++) {
3198 __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3200 __get_user(regs->fpscr, &sc->sc_fpscr);
3201 __get_user(regs->fpul, &sc->sc_fpul);
3203 regs->tra = -1; /* disable syscall checks */
3204 __get_user(*r0_p, &sc->sc_gregs[0]);
3207 static void setup_frame(int sig, struct target_sigaction *ka,
3208 target_sigset_t *set, CPUSH4State *regs)
3210 struct target_sigframe *frame;
3211 abi_ulong frame_addr;
3212 int i;
3214 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3215 trace_user_setup_frame(regs, frame_addr);
3216 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3217 goto give_sigsegv;
3219 setup_sigcontext(&frame->sc, regs, set->sig[0]);
3221 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3222 __put_user(set->sig[i + 1], &frame->extramask[i]);
3225 /* Set up to return from userspace. If provided, use a stub
3226 already in userspace. */
3227 if (ka->sa_flags & TARGET_SA_RESTORER) {
3228 regs->pr = (unsigned long) ka->sa_restorer;
3229 } else {
3230 /* Generate return code (system call to sigreturn) */
3231 abi_ulong retcode_addr = frame_addr +
3232 offsetof(struct target_sigframe, retcode);
3233 __put_user(MOVW(2), &frame->retcode[0]);
3234 __put_user(TRAP_NOARG, &frame->retcode[1]);
3235 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3236 regs->pr = (unsigned long) retcode_addr;
3239 /* Set up registers for signal handler */
3240 regs->gregs[15] = frame_addr;
3241 regs->gregs[4] = sig; /* Arg for signal handler */
3242 regs->gregs[5] = 0;
3243 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3244 regs->pc = (unsigned long) ka->_sa_handler;
3246 unlock_user_struct(frame, frame_addr, 1);
3247 return;
3249 give_sigsegv:
3250 unlock_user_struct(frame, frame_addr, 1);
3251 force_sig(TARGET_SIGSEGV);
3254 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3255 target_siginfo_t *info,
3256 target_sigset_t *set, CPUSH4State *regs)
3258 struct target_rt_sigframe *frame;
3259 abi_ulong frame_addr;
3260 int i;
3262 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3263 trace_user_setup_rt_frame(regs, frame_addr);
3264 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3265 goto give_sigsegv;
3267 tswap_siginfo(&frame->info, info);
3269 /* Create the ucontext. */
3270 __put_user(0, &frame->uc.tuc_flags);
3271 __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3272 __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3273 &frame->uc.tuc_stack.ss_sp);
3274 __put_user(sas_ss_flags(regs->gregs[15]),
3275 &frame->uc.tuc_stack.ss_flags);
3276 __put_user(target_sigaltstack_used.ss_size,
3277 &frame->uc.tuc_stack.ss_size);
3278 setup_sigcontext(&frame->uc.tuc_mcontext,
3279 regs, set->sig[0]);
3280 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3281 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3284 /* Set up to return from userspace. If provided, use a stub
3285 already in userspace. */
3286 if (ka->sa_flags & TARGET_SA_RESTORER) {
3287 regs->pr = (unsigned long) ka->sa_restorer;
3288 } else {
3289 /* Generate return code (system call to sigreturn) */
3290 abi_ulong retcode_addr = frame_addr +
3291 offsetof(struct target_rt_sigframe, retcode);
3292 __put_user(MOVW(2), &frame->retcode[0]);
3293 __put_user(TRAP_NOARG, &frame->retcode[1]);
3294 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3295 regs->pr = (unsigned long) retcode_addr;
3298 /* Set up registers for signal handler */
3299 regs->gregs[15] = frame_addr;
3300 regs->gregs[4] = sig; /* Arg for signal handler */
3301 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3302 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3303 regs->pc = (unsigned long) ka->_sa_handler;
3305 unlock_user_struct(frame, frame_addr, 1);
3306 return;
3308 give_sigsegv:
3309 unlock_user_struct(frame, frame_addr, 1);
3310 force_sig(TARGET_SIGSEGV);
3313 long do_sigreturn(CPUSH4State *regs)
3315 struct target_sigframe *frame;
3316 abi_ulong frame_addr;
3317 sigset_t blocked;
3318 target_sigset_t target_set;
3319 target_ulong r0;
3320 int i;
3321 int err = 0;
3323 frame_addr = regs->gregs[15];
3324 trace_user_do_sigreturn(regs, frame_addr);
3325 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3326 goto badframe;
3328 __get_user(target_set.sig[0], &frame->sc.oldmask);
3329 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3330 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3333 if (err)
3334 goto badframe;
3336 target_to_host_sigset_internal(&blocked, &target_set);
3337 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3339 restore_sigcontext(regs, &frame->sc, &r0);
3341 unlock_user_struct(frame, frame_addr, 0);
3342 return r0;
3344 badframe:
3345 unlock_user_struct(frame, frame_addr, 0);
3346 force_sig(TARGET_SIGSEGV);
3347 return 0;
3350 long do_rt_sigreturn(CPUSH4State *regs)
3352 struct target_rt_sigframe *frame;
3353 abi_ulong frame_addr;
3354 sigset_t blocked;
3355 target_ulong r0;
3357 frame_addr = regs->gregs[15];
3358 trace_user_do_rt_sigreturn(regs, frame_addr);
3359 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3360 goto badframe;
3362 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3363 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3365 restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0);
3367 if (do_sigaltstack(frame_addr +
3368 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3369 0, get_sp_from_cpustate(regs)) == -EFAULT)
3370 goto badframe;
3372 unlock_user_struct(frame, frame_addr, 0);
3373 return r0;
3375 badframe:
3376 unlock_user_struct(frame, frame_addr, 0);
3377 force_sig(TARGET_SIGSEGV);
3378 return 0;
3380 #elif defined(TARGET_MICROBLAZE)
3382 struct target_sigcontext {
3383 struct target_pt_regs regs; /* needs to be first */
3384 uint32_t oldmask;
3387 struct target_stack_t {
3388 abi_ulong ss_sp;
3389 int ss_flags;
3390 unsigned int ss_size;
3393 struct target_ucontext {
3394 abi_ulong tuc_flags;
3395 abi_ulong tuc_link;
3396 struct target_stack_t tuc_stack;
3397 struct target_sigcontext tuc_mcontext;
3398 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3401 /* Signal frames. */
3402 struct target_signal_frame {
3403 struct target_ucontext uc;
3404 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3405 uint32_t tramp[2];
3408 struct rt_signal_frame {
3409 siginfo_t info;
3410 struct ucontext uc;
3411 uint32_t tramp[2];
3414 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3416 __put_user(env->regs[0], &sc->regs.r0);
3417 __put_user(env->regs[1], &sc->regs.r1);
3418 __put_user(env->regs[2], &sc->regs.r2);
3419 __put_user(env->regs[3], &sc->regs.r3);
3420 __put_user(env->regs[4], &sc->regs.r4);
3421 __put_user(env->regs[5], &sc->regs.r5);
3422 __put_user(env->regs[6], &sc->regs.r6);
3423 __put_user(env->regs[7], &sc->regs.r7);
3424 __put_user(env->regs[8], &sc->regs.r8);
3425 __put_user(env->regs[9], &sc->regs.r9);
3426 __put_user(env->regs[10], &sc->regs.r10);
3427 __put_user(env->regs[11], &sc->regs.r11);
3428 __put_user(env->regs[12], &sc->regs.r12);
3429 __put_user(env->regs[13], &sc->regs.r13);
3430 __put_user(env->regs[14], &sc->regs.r14);
3431 __put_user(env->regs[15], &sc->regs.r15);
3432 __put_user(env->regs[16], &sc->regs.r16);
3433 __put_user(env->regs[17], &sc->regs.r17);
3434 __put_user(env->regs[18], &sc->regs.r18);
3435 __put_user(env->regs[19], &sc->regs.r19);
3436 __put_user(env->regs[20], &sc->regs.r20);
3437 __put_user(env->regs[21], &sc->regs.r21);
3438 __put_user(env->regs[22], &sc->regs.r22);
3439 __put_user(env->regs[23], &sc->regs.r23);
3440 __put_user(env->regs[24], &sc->regs.r24);
3441 __put_user(env->regs[25], &sc->regs.r25);
3442 __put_user(env->regs[26], &sc->regs.r26);
3443 __put_user(env->regs[27], &sc->regs.r27);
3444 __put_user(env->regs[28], &sc->regs.r28);
3445 __put_user(env->regs[29], &sc->regs.r29);
3446 __put_user(env->regs[30], &sc->regs.r30);
3447 __put_user(env->regs[31], &sc->regs.r31);
3448 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3451 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3453 __get_user(env->regs[0], &sc->regs.r0);
3454 __get_user(env->regs[1], &sc->regs.r1);
3455 __get_user(env->regs[2], &sc->regs.r2);
3456 __get_user(env->regs[3], &sc->regs.r3);
3457 __get_user(env->regs[4], &sc->regs.r4);
3458 __get_user(env->regs[5], &sc->regs.r5);
3459 __get_user(env->regs[6], &sc->regs.r6);
3460 __get_user(env->regs[7], &sc->regs.r7);
3461 __get_user(env->regs[8], &sc->regs.r8);
3462 __get_user(env->regs[9], &sc->regs.r9);
3463 __get_user(env->regs[10], &sc->regs.r10);
3464 __get_user(env->regs[11], &sc->regs.r11);
3465 __get_user(env->regs[12], &sc->regs.r12);
3466 __get_user(env->regs[13], &sc->regs.r13);
3467 __get_user(env->regs[14], &sc->regs.r14);
3468 __get_user(env->regs[15], &sc->regs.r15);
3469 __get_user(env->regs[16], &sc->regs.r16);
3470 __get_user(env->regs[17], &sc->regs.r17);
3471 __get_user(env->regs[18], &sc->regs.r18);
3472 __get_user(env->regs[19], &sc->regs.r19);
3473 __get_user(env->regs[20], &sc->regs.r20);
3474 __get_user(env->regs[21], &sc->regs.r21);
3475 __get_user(env->regs[22], &sc->regs.r22);
3476 __get_user(env->regs[23], &sc->regs.r23);
3477 __get_user(env->regs[24], &sc->regs.r24);
3478 __get_user(env->regs[25], &sc->regs.r25);
3479 __get_user(env->regs[26], &sc->regs.r26);
3480 __get_user(env->regs[27], &sc->regs.r27);
3481 __get_user(env->regs[28], &sc->regs.r28);
3482 __get_user(env->regs[29], &sc->regs.r29);
3483 __get_user(env->regs[30], &sc->regs.r30);
3484 __get_user(env->regs[31], &sc->regs.r31);
3485 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3488 static abi_ulong get_sigframe(struct target_sigaction *ka,
3489 CPUMBState *env, int frame_size)
3491 abi_ulong sp = env->regs[1];
3493 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !on_sig_stack(sp)) {
3494 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3497 return ((sp - frame_size) & -8UL);
3500 static void setup_frame(int sig, struct target_sigaction *ka,
3501 target_sigset_t *set, CPUMBState *env)
3503 struct target_signal_frame *frame;
3504 abi_ulong frame_addr;
3505 int i;
3507 frame_addr = get_sigframe(ka, env, sizeof *frame);
3508 trace_user_setup_frame(env, frame_addr);
3509 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3510 goto badframe;
3512 /* Save the mask. */
3513 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3515 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3516 __put_user(set->sig[i], &frame->extramask[i - 1]);
3519 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3521 /* Set up to return from userspace. If provided, use a stub
3522 already in userspace. */
3523 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3524 if (ka->sa_flags & TARGET_SA_RESTORER) {
3525 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3526 } else {
3527 uint32_t t;
3528 /* Note, these encodings are _big endian_! */
3529 /* addi r12, r0, __NR_sigreturn */
3530 t = 0x31800000UL | TARGET_NR_sigreturn;
3531 __put_user(t, frame->tramp + 0);
3532 /* brki r14, 0x8 */
3533 t = 0xb9cc0008UL;
3534 __put_user(t, frame->tramp + 1);
3536 /* Return from sighandler will jump to the tramp.
3537 Negative 8 offset because return is rtsd r15, 8 */
3538 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3541 /* Set up registers for signal handler */
3542 env->regs[1] = frame_addr;
3543 /* Signal handler args: */
3544 env->regs[5] = sig; /* Arg 0: signum */
3545 env->regs[6] = 0;
3546 /* arg 1: sigcontext */
3547 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3549 /* Offset of 4 to handle microblaze rtid r14, 0 */
3550 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3552 unlock_user_struct(frame, frame_addr, 1);
3553 return;
3554 badframe:
3555 force_sig(TARGET_SIGSEGV);
3558 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3559 target_siginfo_t *info,
3560 target_sigset_t *set, CPUMBState *env)
3562 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3565 long do_sigreturn(CPUMBState *env)
3567 struct target_signal_frame *frame;
3568 abi_ulong frame_addr;
3569 target_sigset_t target_set;
3570 sigset_t set;
3571 int i;
3573 frame_addr = env->regs[R_SP];
3574 trace_user_do_sigreturn(env, frame_addr);
3575 /* Make sure the guest isn't playing games. */
3576 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3577 goto badframe;
3579 /* Restore blocked signals */
3580 __get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask);
3581 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3582 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3584 target_to_host_sigset_internal(&set, &target_set);
3585 do_sigprocmask(SIG_SETMASK, &set, NULL);
3587 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3588 /* We got here through a sigreturn syscall, our path back is via an
3589 rtb insn so setup r14 for that. */
3590 env->regs[14] = env->sregs[SR_PC];
3592 unlock_user_struct(frame, frame_addr, 0);
3593 return env->regs[10];
3594 badframe:
3595 force_sig(TARGET_SIGSEGV);
3598 long do_rt_sigreturn(CPUMBState *env)
3600 trace_user_do_rt_sigreturn(env, 0);
3601 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3602 return -TARGET_ENOSYS;
3605 #elif defined(TARGET_CRIS)
3607 struct target_sigcontext {
3608 struct target_pt_regs regs; /* needs to be first */
3609 uint32_t oldmask;
3610 uint32_t usp; /* usp before stacking this gunk on it */
3613 /* Signal frames. */
3614 struct target_signal_frame {
3615 struct target_sigcontext sc;
3616 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3617 uint16_t retcode[4]; /* Trampoline code. */
3620 struct rt_signal_frame {
3621 siginfo_t *pinfo;
3622 void *puc;
3623 siginfo_t info;
3624 struct ucontext uc;
3625 uint16_t retcode[4]; /* Trampoline code. */
3628 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3630 __put_user(env->regs[0], &sc->regs.r0);
3631 __put_user(env->regs[1], &sc->regs.r1);
3632 __put_user(env->regs[2], &sc->regs.r2);
3633 __put_user(env->regs[3], &sc->regs.r3);
3634 __put_user(env->regs[4], &sc->regs.r4);
3635 __put_user(env->regs[5], &sc->regs.r5);
3636 __put_user(env->regs[6], &sc->regs.r6);
3637 __put_user(env->regs[7], &sc->regs.r7);
3638 __put_user(env->regs[8], &sc->regs.r8);
3639 __put_user(env->regs[9], &sc->regs.r9);
3640 __put_user(env->regs[10], &sc->regs.r10);
3641 __put_user(env->regs[11], &sc->regs.r11);
3642 __put_user(env->regs[12], &sc->regs.r12);
3643 __put_user(env->regs[13], &sc->regs.r13);
3644 __put_user(env->regs[14], &sc->usp);
3645 __put_user(env->regs[15], &sc->regs.acr);
3646 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3647 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3648 __put_user(env->pc, &sc->regs.erp);
3651 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3653 __get_user(env->regs[0], &sc->regs.r0);
3654 __get_user(env->regs[1], &sc->regs.r1);
3655 __get_user(env->regs[2], &sc->regs.r2);
3656 __get_user(env->regs[3], &sc->regs.r3);
3657 __get_user(env->regs[4], &sc->regs.r4);
3658 __get_user(env->regs[5], &sc->regs.r5);
3659 __get_user(env->regs[6], &sc->regs.r6);
3660 __get_user(env->regs[7], &sc->regs.r7);
3661 __get_user(env->regs[8], &sc->regs.r8);
3662 __get_user(env->regs[9], &sc->regs.r9);
3663 __get_user(env->regs[10], &sc->regs.r10);
3664 __get_user(env->regs[11], &sc->regs.r11);
3665 __get_user(env->regs[12], &sc->regs.r12);
3666 __get_user(env->regs[13], &sc->regs.r13);
3667 __get_user(env->regs[14], &sc->usp);
3668 __get_user(env->regs[15], &sc->regs.acr);
3669 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3670 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3671 __get_user(env->pc, &sc->regs.erp);
3674 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3676 abi_ulong sp;
3677 /* Align the stack downwards to 4. */
3678 sp = (env->regs[R_SP] & ~3);
3679 return sp - framesize;
3682 static void setup_frame(int sig, struct target_sigaction *ka,
3683 target_sigset_t *set, CPUCRISState *env)
3685 struct target_signal_frame *frame;
3686 abi_ulong frame_addr;
3687 int i;
3689 frame_addr = get_sigframe(env, sizeof *frame);
3690 trace_user_setup_frame(env, frame_addr);
3691 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3692 goto badframe;
3695 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3696 * use this trampoline anymore but it sets it up for GDB.
3697 * In QEMU, using the trampoline simplifies things a bit so we use it.
3699 * This is movu.w __NR_sigreturn, r9; break 13;
3701 __put_user(0x9c5f, frame->retcode+0);
3702 __put_user(TARGET_NR_sigreturn,
3703 frame->retcode + 1);
3704 __put_user(0xe93d, frame->retcode + 2);
3706 /* Save the mask. */
3707 __put_user(set->sig[0], &frame->sc.oldmask);
3709 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3710 __put_user(set->sig[i], &frame->extramask[i - 1]);
3713 setup_sigcontext(&frame->sc, env);
3715 /* Move the stack and setup the arguments for the handler. */
3716 env->regs[R_SP] = frame_addr;
3717 env->regs[10] = sig;
3718 env->pc = (unsigned long) ka->_sa_handler;
3719 /* Link SRP so the guest returns through the trampoline. */
3720 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3722 unlock_user_struct(frame, frame_addr, 1);
3723 return;
3724 badframe:
3725 force_sig(TARGET_SIGSEGV);
3728 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3729 target_siginfo_t *info,
3730 target_sigset_t *set, CPUCRISState *env)
3732 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3735 long do_sigreturn(CPUCRISState *env)
3737 struct target_signal_frame *frame;
3738 abi_ulong frame_addr;
3739 target_sigset_t target_set;
3740 sigset_t set;
3741 int i;
3743 frame_addr = env->regs[R_SP];
3744 trace_user_do_sigreturn(env, frame_addr);
3745 /* Make sure the guest isn't playing games. */
3746 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3747 goto badframe;
3749 /* Restore blocked signals */
3750 __get_user(target_set.sig[0], &frame->sc.oldmask);
3751 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3752 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3754 target_to_host_sigset_internal(&set, &target_set);
3755 do_sigprocmask(SIG_SETMASK, &set, NULL);
3757 restore_sigcontext(&frame->sc, env);
3758 unlock_user_struct(frame, frame_addr, 0);
3759 return env->regs[10];
3760 badframe:
3761 force_sig(TARGET_SIGSEGV);
3764 long do_rt_sigreturn(CPUCRISState *env)
3766 trace_user_do_rt_sigreturn(env, 0);
3767 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3768 return -TARGET_ENOSYS;
3771 #elif defined(TARGET_OPENRISC)
3773 struct target_sigcontext {
3774 struct target_pt_regs regs;
3775 abi_ulong oldmask;
3776 abi_ulong usp;
3779 struct target_ucontext {
3780 abi_ulong tuc_flags;
3781 abi_ulong tuc_link;
3782 target_stack_t tuc_stack;
3783 struct target_sigcontext tuc_mcontext;
3784 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3787 struct target_rt_sigframe {
3788 abi_ulong pinfo;
3789 uint64_t puc;
3790 struct target_siginfo info;
3791 struct target_sigcontext sc;
3792 struct target_ucontext uc;
3793 unsigned char retcode[16]; /* trampoline code */
3796 /* This is the asm-generic/ucontext.h version */
3797 #if 0
3798 static int restore_sigcontext(CPUOpenRISCState *regs,
3799 struct target_sigcontext *sc)
3801 unsigned int err = 0;
3802 unsigned long old_usp;
3804 /* Alwys make any pending restarted system call return -EINTR */
3805 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3807 /* restore the regs from &sc->regs (same as sc, since regs is first)
3808 * (sc is already checked for VERIFY_READ since the sigframe was
3809 * checked in sys_sigreturn previously)
3812 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3813 goto badframe;
3816 /* make sure the U-flag is set so user-mode cannot fool us */
3818 regs->sr &= ~SR_SM;
3820 /* restore the old USP as it was before we stacked the sc etc.
3821 * (we cannot just pop the sigcontext since we aligned the sp and
3822 * stuff after pushing it)
3825 __get_user(old_usp, &sc->usp);
3826 phx_signal("old_usp 0x%lx", old_usp);
3828 __PHX__ REALLY /* ??? */
3829 wrusp(old_usp);
3830 regs->gpr[1] = old_usp;
3832 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3833 * after this completes, but we don't use that mechanism. maybe we can
3834 * use it now ?
3837 return err;
3839 badframe:
3840 return 1;
3842 #endif
3844 /* Set up a signal frame. */
3846 static void setup_sigcontext(struct target_sigcontext *sc,
3847 CPUOpenRISCState *regs,
3848 unsigned long mask)
3850 unsigned long usp = regs->gpr[1];
3852 /* copy the regs. they are first in sc so we can use sc directly */
3854 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3856 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3857 the signal handler. The frametype will be restored to its previous
3858 value in restore_sigcontext. */
3859 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3861 /* then some other stuff */
3862 __put_user(mask, &sc->oldmask);
3863 __put_user(usp, &sc->usp);
3866 static inline unsigned long align_sigframe(unsigned long sp)
3868 unsigned long i;
3869 i = sp & ~3UL;
3870 return i;
3873 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3874 CPUOpenRISCState *regs,
3875 size_t frame_size)
3877 unsigned long sp = regs->gpr[1];
3878 int onsigstack = on_sig_stack(sp);
3880 /* redzone */
3881 /* This is the X/Open sanctioned signal stack switching. */
3882 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !onsigstack) {
3883 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3886 sp = align_sigframe(sp - frame_size);
3889 * If we are on the alternate signal stack and would overflow it, don't.
3890 * Return an always-bogus address instead so we will die with SIGSEGV.
3893 if (onsigstack && !likely(on_sig_stack(sp))) {
3894 return -1L;
3897 return sp;
3900 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3901 target_siginfo_t *info,
3902 target_sigset_t *set, CPUOpenRISCState *env)
3904 int err = 0;
3905 abi_ulong frame_addr;
3906 unsigned long return_ip;
3907 struct target_rt_sigframe *frame;
3908 abi_ulong info_addr, uc_addr;
3910 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3911 trace_user_setup_rt_frame(env, frame_addr);
3912 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3913 goto give_sigsegv;
3916 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3917 __put_user(info_addr, &frame->pinfo);
3918 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3919 __put_user(uc_addr, &frame->puc);
3921 if (ka->sa_flags & SA_SIGINFO) {
3922 tswap_siginfo(&frame->info, info);
3925 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3926 __put_user(0, &frame->uc.tuc_flags);
3927 __put_user(0, &frame->uc.tuc_link);
3928 __put_user(target_sigaltstack_used.ss_sp,
3929 &frame->uc.tuc_stack.ss_sp);
3930 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3931 __put_user(target_sigaltstack_used.ss_size,
3932 &frame->uc.tuc_stack.ss_size);
3933 setup_sigcontext(&frame->sc, env, set->sig[0]);
3935 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3937 /* trampoline - the desired return ip is the retcode itself */
3938 return_ip = (unsigned long)&frame->retcode;
3939 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3940 __put_user(0xa960, (short *)(frame->retcode + 0));
3941 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3942 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3943 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3945 if (err) {
3946 goto give_sigsegv;
3949 /* TODO what is the current->exec_domain stuff and invmap ? */
3951 /* Set up registers for signal handler */
3952 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3953 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3954 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3955 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3956 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3958 /* actually move the usp to reflect the stacked frame */
3959 env->gpr[1] = (unsigned long)frame;
3961 return;
3963 give_sigsegv:
3964 unlock_user_struct(frame, frame_addr, 1);
3965 if (sig == TARGET_SIGSEGV) {
3966 ka->_sa_handler = TARGET_SIG_DFL;
3968 force_sig(TARGET_SIGSEGV);
3971 long do_sigreturn(CPUOpenRISCState *env)
3973 trace_user_do_sigreturn(env, 0);
3974 fprintf(stderr, "do_sigreturn: not implemented\n");
3975 return -TARGET_ENOSYS;
3978 long do_rt_sigreturn(CPUOpenRISCState *env)
3980 trace_user_do_rt_sigreturn(env, 0);
3981 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
3982 return -TARGET_ENOSYS;
3984 /* TARGET_OPENRISC */
3986 #elif defined(TARGET_S390X)
3988 #define __NUM_GPRS 16
3989 #define __NUM_FPRS 16
3990 #define __NUM_ACRS 16
3992 #define S390_SYSCALL_SIZE 2
3993 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3995 #define _SIGCONTEXT_NSIG 64
3996 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3997 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3998 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3999 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4000 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4002 typedef struct {
4003 target_psw_t psw;
4004 target_ulong gprs[__NUM_GPRS];
4005 unsigned int acrs[__NUM_ACRS];
4006 } target_s390_regs_common;
4008 typedef struct {
4009 unsigned int fpc;
4010 double fprs[__NUM_FPRS];
4011 } target_s390_fp_regs;
4013 typedef struct {
4014 target_s390_regs_common regs;
4015 target_s390_fp_regs fpregs;
4016 } target_sigregs;
4018 struct target_sigcontext {
4019 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4020 target_sigregs *sregs;
4023 typedef struct {
4024 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4025 struct target_sigcontext sc;
4026 target_sigregs sregs;
4027 int signo;
4028 uint8_t retcode[S390_SYSCALL_SIZE];
4029 } sigframe;
4031 struct target_ucontext {
4032 target_ulong tuc_flags;
4033 struct target_ucontext *tuc_link;
4034 target_stack_t tuc_stack;
4035 target_sigregs tuc_mcontext;
4036 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4039 typedef struct {
4040 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4041 uint8_t retcode[S390_SYSCALL_SIZE];
4042 struct target_siginfo info;
4043 struct target_ucontext uc;
4044 } rt_sigframe;
4046 static inline abi_ulong
4047 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4049 abi_ulong sp;
4051 /* Default to using normal stack */
4052 sp = env->regs[15];
4054 /* This is the X/Open sanctioned signal stack switching. */
4055 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4056 if (!sas_ss_flags(sp)) {
4057 sp = target_sigaltstack_used.ss_sp +
4058 target_sigaltstack_used.ss_size;
4062 /* This is the legacy signal stack switching. */
4063 else if (/* FIXME !user_mode(regs) */ 0 &&
4064 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4065 ka->sa_restorer) {
4066 sp = (abi_ulong) ka->sa_restorer;
4069 return (sp - frame_size) & -8ul;
4072 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4074 int i;
4075 //save_access_regs(current->thread.acrs); FIXME
4077 /* Copy a 'clean' PSW mask to the user to avoid leaking
4078 information about whether PER is currently on. */
4079 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4080 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4081 for (i = 0; i < 16; i++) {
4082 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4084 for (i = 0; i < 16; i++) {
4085 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4088 * We have to store the fp registers to current->thread.fp_regs
4089 * to merge them with the emulated registers.
4091 //save_fp_regs(&current->thread.fp_regs); FIXME
4092 for (i = 0; i < 16; i++) {
4093 __put_user(get_freg(env, i)->ll, &sregs->fpregs.fprs[i]);
4097 static void setup_frame(int sig, struct target_sigaction *ka,
4098 target_sigset_t *set, CPUS390XState *env)
4100 sigframe *frame;
4101 abi_ulong frame_addr;
4103 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4104 trace_user_setup_frame(env, frame_addr);
4105 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4106 goto give_sigsegv;
4109 __put_user(set->sig[0], &frame->sc.oldmask[0]);
4111 save_sigregs(env, &frame->sregs);
4113 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4114 (abi_ulong *)&frame->sc.sregs);
4116 /* Set up to return from userspace. If provided, use a stub
4117 already in userspace. */
4118 if (ka->sa_flags & TARGET_SA_RESTORER) {
4119 env->regs[14] = (unsigned long)
4120 ka->sa_restorer | PSW_ADDR_AMODE;
4121 } else {
4122 env->regs[14] = (unsigned long)
4123 frame->retcode | PSW_ADDR_AMODE;
4124 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4125 (uint16_t *)(frame->retcode));
4128 /* Set up backchain. */
4129 __put_user(env->regs[15], (abi_ulong *) frame);
4131 /* Set up registers for signal handler */
4132 env->regs[15] = frame_addr;
4133 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4135 env->regs[2] = sig; //map_signal(sig);
4136 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4138 /* We forgot to include these in the sigcontext.
4139 To avoid breaking binary compatibility, they are passed as args. */
4140 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4141 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4143 /* Place signal number on stack to allow backtrace from handler. */
4144 __put_user(env->regs[2], (int *) &frame->signo);
4145 unlock_user_struct(frame, frame_addr, 1);
4146 return;
4148 give_sigsegv:
4149 force_sig(TARGET_SIGSEGV);
4152 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4153 target_siginfo_t *info,
4154 target_sigset_t *set, CPUS390XState *env)
4156 int i;
4157 rt_sigframe *frame;
4158 abi_ulong frame_addr;
4160 frame_addr = get_sigframe(ka, env, sizeof *frame);
4161 trace_user_setup_rt_frame(env, frame_addr);
4162 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4163 goto give_sigsegv;
4166 tswap_siginfo(&frame->info, info);
4168 /* Create the ucontext. */
4169 __put_user(0, &frame->uc.tuc_flags);
4170 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4171 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4172 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4173 &frame->uc.tuc_stack.ss_flags);
4174 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4175 save_sigregs(env, &frame->uc.tuc_mcontext);
4176 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4177 __put_user((abi_ulong)set->sig[i],
4178 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4181 /* Set up to return from userspace. If provided, use a stub
4182 already in userspace. */
4183 if (ka->sa_flags & TARGET_SA_RESTORER) {
4184 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4185 } else {
4186 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4187 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4188 (uint16_t *)(frame->retcode));
4191 /* Set up backchain. */
4192 __put_user(env->regs[15], (abi_ulong *) frame);
4194 /* Set up registers for signal handler */
4195 env->regs[15] = frame_addr;
4196 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4198 env->regs[2] = sig; //map_signal(sig);
4199 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4200 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4201 return;
4203 give_sigsegv:
4204 force_sig(TARGET_SIGSEGV);
4207 static int
4208 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4210 int err = 0;
4211 int i;
4213 for (i = 0; i < 16; i++) {
4214 __get_user(env->regs[i], &sc->regs.gprs[i]);
4217 __get_user(env->psw.mask, &sc->regs.psw.mask);
4218 trace_user_s390x_restore_sigregs(env, (unsigned long long)sc->regs.psw.addr,
4219 (unsigned long long)env->psw.addr);
4220 __get_user(env->psw.addr, &sc->regs.psw.addr);
4221 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4223 for (i = 0; i < 16; i++) {
4224 __get_user(env->aregs[i], &sc->regs.acrs[i]);
4226 for (i = 0; i < 16; i++) {
4227 __get_user(get_freg(env, i)->ll, &sc->fpregs.fprs[i]);
4230 return err;
4233 long do_sigreturn(CPUS390XState *env)
4235 sigframe *frame;
4236 abi_ulong frame_addr = env->regs[15];
4237 target_sigset_t target_set;
4238 sigset_t set;
4240 trace_user_do_sigreturn(env, frame_addr);
4241 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4242 goto badframe;
4244 __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
4246 target_to_host_sigset_internal(&set, &target_set);
4247 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4249 if (restore_sigregs(env, &frame->sregs)) {
4250 goto badframe;
4253 unlock_user_struct(frame, frame_addr, 0);
4254 return env->regs[2];
4256 badframe:
4257 force_sig(TARGET_SIGSEGV);
4258 return 0;
4261 long do_rt_sigreturn(CPUS390XState *env)
4263 rt_sigframe *frame;
4264 abi_ulong frame_addr = env->regs[15];
4265 sigset_t set;
4267 trace_user_do_rt_sigreturn(env, frame_addr);
4268 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4269 goto badframe;
4271 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4273 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4275 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4276 goto badframe;
4279 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4280 get_sp_from_cpustate(env)) == -EFAULT) {
4281 goto badframe;
4283 unlock_user_struct(frame, frame_addr, 0);
4284 return env->regs[2];
4286 badframe:
4287 unlock_user_struct(frame, frame_addr, 0);
4288 force_sig(TARGET_SIGSEGV);
4289 return 0;
4292 #elif defined(TARGET_PPC)
4294 /* Size of dummy stack frame allocated when calling signal handler.
4295 See arch/powerpc/include/asm/ptrace.h. */
4296 #if defined(TARGET_PPC64)
4297 #define SIGNAL_FRAMESIZE 128
4298 #else
4299 #define SIGNAL_FRAMESIZE 64
4300 #endif
4302 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4303 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4304 struct target_mcontext {
4305 target_ulong mc_gregs[48];
4306 /* Includes fpscr. */
4307 uint64_t mc_fregs[33];
4308 target_ulong mc_pad[2];
4309 /* We need to handle Altivec and SPE at the same time, which no
4310 kernel needs to do. Fortunately, the kernel defines this bit to
4311 be Altivec-register-large all the time, rather than trying to
4312 twiddle it based on the specific platform. */
4313 union {
4314 /* SPE vector registers. One extra for SPEFSCR. */
4315 uint32_t spe[33];
4316 /* Altivec vector registers. The packing of VSCR and VRSAVE
4317 varies depending on whether we're PPC64 or not: PPC64 splits
4318 them apart; PPC32 stuffs them together. */
4319 #if defined(TARGET_PPC64)
4320 #define QEMU_NVRREG 34
4321 #else
4322 #define QEMU_NVRREG 33
4323 #endif
4324 ppc_avr_t altivec[QEMU_NVRREG];
4325 #undef QEMU_NVRREG
4326 } mc_vregs __attribute__((__aligned__(16)));
4329 /* See arch/powerpc/include/asm/sigcontext.h. */
4330 struct target_sigcontext {
4331 target_ulong _unused[4];
4332 int32_t signal;
4333 #if defined(TARGET_PPC64)
4334 int32_t pad0;
4335 #endif
4336 target_ulong handler;
4337 target_ulong oldmask;
4338 target_ulong regs; /* struct pt_regs __user * */
4339 #if defined(TARGET_PPC64)
4340 struct target_mcontext mcontext;
4341 #endif
4344 /* Indices for target_mcontext.mc_gregs, below.
4345 See arch/powerpc/include/asm/ptrace.h for details. */
4346 enum {
4347 TARGET_PT_R0 = 0,
4348 TARGET_PT_R1 = 1,
4349 TARGET_PT_R2 = 2,
4350 TARGET_PT_R3 = 3,
4351 TARGET_PT_R4 = 4,
4352 TARGET_PT_R5 = 5,
4353 TARGET_PT_R6 = 6,
4354 TARGET_PT_R7 = 7,
4355 TARGET_PT_R8 = 8,
4356 TARGET_PT_R9 = 9,
4357 TARGET_PT_R10 = 10,
4358 TARGET_PT_R11 = 11,
4359 TARGET_PT_R12 = 12,
4360 TARGET_PT_R13 = 13,
4361 TARGET_PT_R14 = 14,
4362 TARGET_PT_R15 = 15,
4363 TARGET_PT_R16 = 16,
4364 TARGET_PT_R17 = 17,
4365 TARGET_PT_R18 = 18,
4366 TARGET_PT_R19 = 19,
4367 TARGET_PT_R20 = 20,
4368 TARGET_PT_R21 = 21,
4369 TARGET_PT_R22 = 22,
4370 TARGET_PT_R23 = 23,
4371 TARGET_PT_R24 = 24,
4372 TARGET_PT_R25 = 25,
4373 TARGET_PT_R26 = 26,
4374 TARGET_PT_R27 = 27,
4375 TARGET_PT_R28 = 28,
4376 TARGET_PT_R29 = 29,
4377 TARGET_PT_R30 = 30,
4378 TARGET_PT_R31 = 31,
4379 TARGET_PT_NIP = 32,
4380 TARGET_PT_MSR = 33,
4381 TARGET_PT_ORIG_R3 = 34,
4382 TARGET_PT_CTR = 35,
4383 TARGET_PT_LNK = 36,
4384 TARGET_PT_XER = 37,
4385 TARGET_PT_CCR = 38,
4386 /* Yes, there are two registers with #39. One is 64-bit only. */
4387 TARGET_PT_MQ = 39,
4388 TARGET_PT_SOFTE = 39,
4389 TARGET_PT_TRAP = 40,
4390 TARGET_PT_DAR = 41,
4391 TARGET_PT_DSISR = 42,
4392 TARGET_PT_RESULT = 43,
4393 TARGET_PT_REGS_COUNT = 44
4397 struct target_ucontext {
4398 target_ulong tuc_flags;
4399 target_ulong tuc_link; /* struct ucontext __user * */
4400 struct target_sigaltstack tuc_stack;
4401 #if !defined(TARGET_PPC64)
4402 int32_t tuc_pad[7];
4403 target_ulong tuc_regs; /* struct mcontext __user *
4404 points to uc_mcontext field */
4405 #endif
4406 target_sigset_t tuc_sigmask;
4407 #if defined(TARGET_PPC64)
4408 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4409 struct target_sigcontext tuc_sigcontext;
4410 #else
4411 int32_t tuc_maskext[30];
4412 int32_t tuc_pad2[3];
4413 struct target_mcontext tuc_mcontext;
4414 #endif
4417 /* See arch/powerpc/kernel/signal_32.c. */
4418 struct target_sigframe {
4419 struct target_sigcontext sctx;
4420 struct target_mcontext mctx;
4421 int32_t abigap[56];
4424 #if defined(TARGET_PPC64)
4426 #define TARGET_TRAMP_SIZE 6
4428 struct target_rt_sigframe {
4429 /* sys_rt_sigreturn requires the ucontext be the first field */
4430 struct target_ucontext uc;
4431 target_ulong _unused[2];
4432 uint32_t trampoline[TARGET_TRAMP_SIZE];
4433 target_ulong pinfo; /* struct siginfo __user * */
4434 target_ulong puc; /* void __user * */
4435 struct target_siginfo info;
4436 /* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
4437 char abigap[288];
4438 } __attribute__((aligned(16)));
4440 #else
4442 struct target_rt_sigframe {
4443 struct target_siginfo info;
4444 struct target_ucontext uc;
4445 int32_t abigap[56];
4448 #endif
4450 #if defined(TARGET_PPC64)
4452 struct target_func_ptr {
4453 target_ulong entry;
4454 target_ulong toc;
4457 #endif
4459 /* We use the mc_pad field for the signal return trampoline. */
4460 #define tramp mc_pad
4462 /* See arch/powerpc/kernel/signal.c. */
4463 static target_ulong get_sigframe(struct target_sigaction *ka,
4464 CPUPPCState *env,
4465 int frame_size)
4467 target_ulong oldsp, newsp;
4469 oldsp = env->gpr[1];
4471 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4472 (sas_ss_flags(oldsp) == 0)) {
4473 oldsp = (target_sigaltstack_used.ss_sp
4474 + target_sigaltstack_used.ss_size);
4477 newsp = (oldsp - frame_size) & ~0xFUL;
4479 return newsp;
4482 static void save_user_regs(CPUPPCState *env, struct target_mcontext *frame)
4484 target_ulong msr = env->msr;
4485 int i;
4486 target_ulong ccr = 0;
4488 /* In general, the kernel attempts to be intelligent about what it
4489 needs to save for Altivec/FP/SPE registers. We don't care that
4490 much, so we just go ahead and save everything. */
4492 /* Save general registers. */
4493 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4494 __put_user(env->gpr[i], &frame->mc_gregs[i]);
4496 __put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4497 __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4498 __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4499 __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4501 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4502 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4504 __put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4506 /* Save Altivec registers if necessary. */
4507 if (env->insns_flags & PPC_ALTIVEC) {
4508 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4509 ppc_avr_t *avr = &env->avr[i];
4510 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4512 __put_user(avr->u64[0], &vreg->u64[0]);
4513 __put_user(avr->u64[1], &vreg->u64[1]);
4515 /* Set MSR_VR in the saved MSR value to indicate that
4516 frame->mc_vregs contains valid data. */
4517 msr |= MSR_VR;
4518 __put_user((uint32_t)env->spr[SPR_VRSAVE],
4519 &frame->mc_vregs.altivec[32].u32[3]);
4522 /* Save floating point registers. */
4523 if (env->insns_flags & PPC_FLOAT) {
4524 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4525 __put_user(env->fpr[i], &frame->mc_fregs[i]);
4527 __put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]);
4530 /* Save SPE registers. The kernel only saves the high half. */
4531 if (env->insns_flags & PPC_SPE) {
4532 #if defined(TARGET_PPC64)
4533 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4534 __put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i]);
4536 #else
4537 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4538 __put_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4540 #endif
4541 /* Set MSR_SPE in the saved MSR value to indicate that
4542 frame->mc_vregs contains valid data. */
4543 msr |= MSR_SPE;
4544 __put_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4547 /* Store MSR. */
4548 __put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4551 static void encode_trampoline(int sigret, uint32_t *tramp)
4553 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4554 if (sigret) {
4555 __put_user(0x38000000 | sigret, &tramp[0]);
4556 __put_user(0x44000002, &tramp[1]);
4560 static void restore_user_regs(CPUPPCState *env,
4561 struct target_mcontext *frame, int sig)
4563 target_ulong save_r2 = 0;
4564 target_ulong msr;
4565 target_ulong ccr;
4567 int i;
4569 if (!sig) {
4570 save_r2 = env->gpr[2];
4573 /* Restore general registers. */
4574 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4575 __get_user(env->gpr[i], &frame->mc_gregs[i]);
4577 __get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4578 __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4579 __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4580 __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4581 __get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4583 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4584 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4587 if (!sig) {
4588 env->gpr[2] = save_r2;
4590 /* Restore MSR. */
4591 __get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4593 /* If doing signal return, restore the previous little-endian mode. */
4594 if (sig)
4595 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4597 /* Restore Altivec registers if necessary. */
4598 if (env->insns_flags & PPC_ALTIVEC) {
4599 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4600 ppc_avr_t *avr = &env->avr[i];
4601 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4603 __get_user(avr->u64[0], &vreg->u64[0]);
4604 __get_user(avr->u64[1], &vreg->u64[1]);
4606 /* Set MSR_VEC in the saved MSR value to indicate that
4607 frame->mc_vregs contains valid data. */
4608 __get_user(env->spr[SPR_VRSAVE],
4609 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3]));
4612 /* Restore floating point registers. */
4613 if (env->insns_flags & PPC_FLOAT) {
4614 uint64_t fpscr;
4615 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4616 __get_user(env->fpr[i], &frame->mc_fregs[i]);
4618 __get_user(fpscr, &frame->mc_fregs[32]);
4619 env->fpscr = (uint32_t) fpscr;
4622 /* Save SPE registers. The kernel only saves the high half. */
4623 if (env->insns_flags & PPC_SPE) {
4624 #if defined(TARGET_PPC64)
4625 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4626 uint32_t hi;
4628 __get_user(hi, &frame->mc_vregs.spe[i]);
4629 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4631 #else
4632 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4633 __get_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4635 #endif
4636 __get_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4640 static void setup_frame(int sig, struct target_sigaction *ka,
4641 target_sigset_t *set, CPUPPCState *env)
4643 struct target_sigframe *frame;
4644 struct target_sigcontext *sc;
4645 target_ulong frame_addr, newsp;
4646 int err = 0;
4647 #if defined(TARGET_PPC64)
4648 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4649 #endif
4651 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4652 trace_user_setup_frame(env, frame_addr);
4653 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4654 goto sigsegv;
4655 sc = &frame->sctx;
4657 __put_user(ka->_sa_handler, &sc->handler);
4658 __put_user(set->sig[0], &sc->oldmask);
4659 #if TARGET_ABI_BITS == 64
4660 __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4661 #else
4662 __put_user(set->sig[1], &sc->_unused[3]);
4663 #endif
4664 __put_user(h2g(&frame->mctx), &sc->regs);
4665 __put_user(sig, &sc->signal);
4667 /* Save user regs. */
4668 save_user_regs(env, &frame->mctx);
4670 /* Construct the trampoline code on the stack. */
4671 encode_trampoline(TARGET_NR_sigreturn, (uint32_t *)&frame->mctx.tramp);
4673 /* The kernel checks for the presence of a VDSO here. We don't
4674 emulate a vdso, so use a sigreturn system call. */
4675 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4677 /* Turn off all fp exceptions. */
4678 env->fpscr = 0;
4680 /* Create a stack frame for the caller of the handler. */
4681 newsp = frame_addr - SIGNAL_FRAMESIZE;
4682 err |= put_user(env->gpr[1], newsp, target_ulong);
4684 if (err)
4685 goto sigsegv;
4687 /* Set up registers for signal handler. */
4688 env->gpr[1] = newsp;
4689 env->gpr[3] = sig;
4690 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4692 #if defined(TARGET_PPC64)
4693 if (get_ppc64_abi(image) < 2) {
4694 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4695 struct target_func_ptr *handler =
4696 (struct target_func_ptr *)g2h(ka->_sa_handler);
4697 env->nip = tswapl(handler->entry);
4698 env->gpr[2] = tswapl(handler->toc);
4699 } else {
4700 /* ELFv2 PPC64 function pointers are entry points, but R12
4701 * must also be set */
4702 env->nip = tswapl((target_ulong) ka->_sa_handler);
4703 env->gpr[12] = env->nip;
4705 #else
4706 env->nip = (target_ulong) ka->_sa_handler;
4707 #endif
4709 /* Signal handlers are entered in big-endian mode. */
4710 env->msr &= ~MSR_LE;
4712 unlock_user_struct(frame, frame_addr, 1);
4713 return;
4715 sigsegv:
4716 unlock_user_struct(frame, frame_addr, 1);
4717 force_sig(TARGET_SIGSEGV);
4720 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4721 target_siginfo_t *info,
4722 target_sigset_t *set, CPUPPCState *env)
4724 struct target_rt_sigframe *rt_sf;
4725 uint32_t *trampptr = 0;
4726 struct target_mcontext *mctx = 0;
4727 target_ulong rt_sf_addr, newsp = 0;
4728 int i, err = 0;
4729 #if defined(TARGET_PPC64)
4730 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4731 #endif
4733 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4734 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4735 goto sigsegv;
4737 tswap_siginfo(&rt_sf->info, info);
4739 __put_user(0, &rt_sf->uc.tuc_flags);
4740 __put_user(0, &rt_sf->uc.tuc_link);
4741 __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4742 &rt_sf->uc.tuc_stack.ss_sp);
4743 __put_user(sas_ss_flags(env->gpr[1]),
4744 &rt_sf->uc.tuc_stack.ss_flags);
4745 __put_user(target_sigaltstack_used.ss_size,
4746 &rt_sf->uc.tuc_stack.ss_size);
4747 #if !defined(TARGET_PPC64)
4748 __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4749 &rt_sf->uc.tuc_regs);
4750 #endif
4751 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4752 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4755 #if defined(TARGET_PPC64)
4756 mctx = &rt_sf->uc.tuc_sigcontext.mcontext;
4757 trampptr = &rt_sf->trampoline[0];
4758 #else
4759 mctx = &rt_sf->uc.tuc_mcontext;
4760 trampptr = (uint32_t *)&rt_sf->uc.tuc_mcontext.tramp;
4761 #endif
4763 save_user_regs(env, mctx);
4764 encode_trampoline(TARGET_NR_rt_sigreturn, trampptr);
4766 /* The kernel checks for the presence of a VDSO here. We don't
4767 emulate a vdso, so use a sigreturn system call. */
4768 env->lr = (target_ulong) h2g(trampptr);
4770 /* Turn off all fp exceptions. */
4771 env->fpscr = 0;
4773 /* Create a stack frame for the caller of the handler. */
4774 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4775 err |= put_user(env->gpr[1], newsp, target_ulong);
4777 if (err)
4778 goto sigsegv;
4780 /* Set up registers for signal handler. */
4781 env->gpr[1] = newsp;
4782 env->gpr[3] = (target_ulong) sig;
4783 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4784 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4785 env->gpr[6] = (target_ulong) h2g(rt_sf);
4787 #if defined(TARGET_PPC64)
4788 if (get_ppc64_abi(image) < 2) {
4789 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4790 struct target_func_ptr *handler =
4791 (struct target_func_ptr *)g2h(ka->_sa_handler);
4792 env->nip = tswapl(handler->entry);
4793 env->gpr[2] = tswapl(handler->toc);
4794 } else {
4795 /* ELFv2 PPC64 function pointers are entry points, but R12
4796 * must also be set */
4797 env->nip = tswapl((target_ulong) ka->_sa_handler);
4798 env->gpr[12] = env->nip;
4800 #else
4801 env->nip = (target_ulong) ka->_sa_handler;
4802 #endif
4804 /* Signal handlers are entered in big-endian mode. */
4805 env->msr &= ~MSR_LE;
4807 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4808 return;
4810 sigsegv:
4811 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4812 force_sig(TARGET_SIGSEGV);
4816 long do_sigreturn(CPUPPCState *env)
4818 struct target_sigcontext *sc = NULL;
4819 struct target_mcontext *sr = NULL;
4820 target_ulong sr_addr = 0, sc_addr;
4821 sigset_t blocked;
4822 target_sigset_t set;
4824 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4825 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4826 goto sigsegv;
4828 #if defined(TARGET_PPC64)
4829 set.sig[0] = sc->oldmask + ((uint64_t)(sc->_unused[3]) << 32);
4830 #else
4831 __get_user(set.sig[0], &sc->oldmask);
4832 __get_user(set.sig[1], &sc->_unused[3]);
4833 #endif
4834 target_to_host_sigset_internal(&blocked, &set);
4835 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4837 __get_user(sr_addr, &sc->regs);
4838 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4839 goto sigsegv;
4840 restore_user_regs(env, sr, 1);
4842 unlock_user_struct(sr, sr_addr, 1);
4843 unlock_user_struct(sc, sc_addr, 1);
4844 return -TARGET_QEMU_ESIGRETURN;
4846 sigsegv:
4847 unlock_user_struct(sr, sr_addr, 1);
4848 unlock_user_struct(sc, sc_addr, 1);
4849 force_sig(TARGET_SIGSEGV);
4850 return 0;
4853 /* See arch/powerpc/kernel/signal_32.c. */
4854 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4856 struct target_mcontext *mcp;
4857 target_ulong mcp_addr;
4858 sigset_t blocked;
4859 target_sigset_t set;
4861 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4862 sizeof (set)))
4863 return 1;
4865 #if defined(TARGET_PPC64)
4866 mcp_addr = h2g(ucp) +
4867 offsetof(struct target_ucontext, tuc_sigcontext.mcontext);
4868 #else
4869 __get_user(mcp_addr, &ucp->tuc_regs);
4870 #endif
4872 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4873 return 1;
4875 target_to_host_sigset_internal(&blocked, &set);
4876 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4877 restore_user_regs(env, mcp, sig);
4879 unlock_user_struct(mcp, mcp_addr, 1);
4880 return 0;
4883 long do_rt_sigreturn(CPUPPCState *env)
4885 struct target_rt_sigframe *rt_sf = NULL;
4886 target_ulong rt_sf_addr;
4888 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4889 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4890 goto sigsegv;
4892 if (do_setcontext(&rt_sf->uc, env, 1))
4893 goto sigsegv;
4895 do_sigaltstack(rt_sf_addr
4896 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4897 0, env->gpr[1]);
4899 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4900 return -TARGET_QEMU_ESIGRETURN;
4902 sigsegv:
4903 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4904 force_sig(TARGET_SIGSEGV);
4905 return 0;
4908 #elif defined(TARGET_M68K)
4910 struct target_sigcontext {
4911 abi_ulong sc_mask;
4912 abi_ulong sc_usp;
4913 abi_ulong sc_d0;
4914 abi_ulong sc_d1;
4915 abi_ulong sc_a0;
4916 abi_ulong sc_a1;
4917 unsigned short sc_sr;
4918 abi_ulong sc_pc;
4921 struct target_sigframe
4923 abi_ulong pretcode;
4924 int sig;
4925 int code;
4926 abi_ulong psc;
4927 char retcode[8];
4928 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4929 struct target_sigcontext sc;
4932 typedef int target_greg_t;
4933 #define TARGET_NGREG 18
4934 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4936 typedef struct target_fpregset {
4937 int f_fpcntl[3];
4938 int f_fpregs[8*3];
4939 } target_fpregset_t;
4941 struct target_mcontext {
4942 int version;
4943 target_gregset_t gregs;
4944 target_fpregset_t fpregs;
4947 #define TARGET_MCONTEXT_VERSION 2
4949 struct target_ucontext {
4950 abi_ulong tuc_flags;
4951 abi_ulong tuc_link;
4952 target_stack_t tuc_stack;
4953 struct target_mcontext tuc_mcontext;
4954 abi_long tuc_filler[80];
4955 target_sigset_t tuc_sigmask;
4958 struct target_rt_sigframe
4960 abi_ulong pretcode;
4961 int sig;
4962 abi_ulong pinfo;
4963 abi_ulong puc;
4964 char retcode[8];
4965 struct target_siginfo info;
4966 struct target_ucontext uc;
4969 static void setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4970 abi_ulong mask)
4972 __put_user(mask, &sc->sc_mask);
4973 __put_user(env->aregs[7], &sc->sc_usp);
4974 __put_user(env->dregs[0], &sc->sc_d0);
4975 __put_user(env->dregs[1], &sc->sc_d1);
4976 __put_user(env->aregs[0], &sc->sc_a0);
4977 __put_user(env->aregs[1], &sc->sc_a1);
4978 __put_user(env->sr, &sc->sc_sr);
4979 __put_user(env->pc, &sc->sc_pc);
4982 static void
4983 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
4985 int temp;
4987 __get_user(env->aregs[7], &sc->sc_usp);
4988 __get_user(env->dregs[1], &sc->sc_d1);
4989 __get_user(env->aregs[0], &sc->sc_a0);
4990 __get_user(env->aregs[1], &sc->sc_a1);
4991 __get_user(env->pc, &sc->sc_pc);
4992 __get_user(temp, &sc->sc_sr);
4993 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4995 *pd0 = tswapl(sc->sc_d0);
4999 * Determine which stack to use..
5001 static inline abi_ulong
5002 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5003 size_t frame_size)
5005 unsigned long sp;
5007 sp = regs->aregs[7];
5009 /* This is the X/Open sanctioned signal stack switching. */
5010 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5011 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5014 return ((sp - frame_size) & -8UL);
5017 static void setup_frame(int sig, struct target_sigaction *ka,
5018 target_sigset_t *set, CPUM68KState *env)
5020 struct target_sigframe *frame;
5021 abi_ulong frame_addr;
5022 abi_ulong retcode_addr;
5023 abi_ulong sc_addr;
5024 int i;
5026 frame_addr = get_sigframe(ka, env, sizeof *frame);
5027 trace_user_setup_frame(env, frame_addr);
5028 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5029 goto give_sigsegv;
5031 __put_user(sig, &frame->sig);
5033 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5034 __put_user(sc_addr, &frame->psc);
5036 setup_sigcontext(&frame->sc, env, set->sig[0]);
5038 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5039 __put_user(set->sig[i], &frame->extramask[i - 1]);
5042 /* Set up to return from userspace. */
5044 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5045 __put_user(retcode_addr, &frame->pretcode);
5047 /* moveq #,d0; trap #0 */
5049 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5050 (uint32_t *)(frame->retcode));
5052 /* Set up to return from userspace */
5054 env->aregs[7] = frame_addr;
5055 env->pc = ka->_sa_handler;
5057 unlock_user_struct(frame, frame_addr, 1);
5058 return;
5060 give_sigsegv:
5061 force_sig(TARGET_SIGSEGV);
5064 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5065 CPUM68KState *env)
5067 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5069 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5070 __put_user(env->dregs[0], &gregs[0]);
5071 __put_user(env->dregs[1], &gregs[1]);
5072 __put_user(env->dregs[2], &gregs[2]);
5073 __put_user(env->dregs[3], &gregs[3]);
5074 __put_user(env->dregs[4], &gregs[4]);
5075 __put_user(env->dregs[5], &gregs[5]);
5076 __put_user(env->dregs[6], &gregs[6]);
5077 __put_user(env->dregs[7], &gregs[7]);
5078 __put_user(env->aregs[0], &gregs[8]);
5079 __put_user(env->aregs[1], &gregs[9]);
5080 __put_user(env->aregs[2], &gregs[10]);
5081 __put_user(env->aregs[3], &gregs[11]);
5082 __put_user(env->aregs[4], &gregs[12]);
5083 __put_user(env->aregs[5], &gregs[13]);
5084 __put_user(env->aregs[6], &gregs[14]);
5085 __put_user(env->aregs[7], &gregs[15]);
5086 __put_user(env->pc, &gregs[16]);
5087 __put_user(env->sr, &gregs[17]);
5089 return 0;
5092 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5093 struct target_ucontext *uc,
5094 int *pd0)
5096 int temp;
5097 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5099 __get_user(temp, &uc->tuc_mcontext.version);
5100 if (temp != TARGET_MCONTEXT_VERSION)
5101 goto badframe;
5103 /* restore passed registers */
5104 __get_user(env->dregs[0], &gregs[0]);
5105 __get_user(env->dregs[1], &gregs[1]);
5106 __get_user(env->dregs[2], &gregs[2]);
5107 __get_user(env->dregs[3], &gregs[3]);
5108 __get_user(env->dregs[4], &gregs[4]);
5109 __get_user(env->dregs[5], &gregs[5]);
5110 __get_user(env->dregs[6], &gregs[6]);
5111 __get_user(env->dregs[7], &gregs[7]);
5112 __get_user(env->aregs[0], &gregs[8]);
5113 __get_user(env->aregs[1], &gregs[9]);
5114 __get_user(env->aregs[2], &gregs[10]);
5115 __get_user(env->aregs[3], &gregs[11]);
5116 __get_user(env->aregs[4], &gregs[12]);
5117 __get_user(env->aregs[5], &gregs[13]);
5118 __get_user(env->aregs[6], &gregs[14]);
5119 __get_user(env->aregs[7], &gregs[15]);
5120 __get_user(env->pc, &gregs[16]);
5121 __get_user(temp, &gregs[17]);
5122 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5124 *pd0 = env->dregs[0];
5125 return 0;
5127 badframe:
5128 return 1;
5131 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5132 target_siginfo_t *info,
5133 target_sigset_t *set, CPUM68KState *env)
5135 struct target_rt_sigframe *frame;
5136 abi_ulong frame_addr;
5137 abi_ulong retcode_addr;
5138 abi_ulong info_addr;
5139 abi_ulong uc_addr;
5140 int err = 0;
5141 int i;
5143 frame_addr = get_sigframe(ka, env, sizeof *frame);
5144 trace_user_setup_rt_frame(env, frame_addr);
5145 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5146 goto give_sigsegv;
5148 __put_user(sig, &frame->sig);
5150 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5151 __put_user(info_addr, &frame->pinfo);
5153 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5154 __put_user(uc_addr, &frame->puc);
5156 tswap_siginfo(&frame->info, info);
5158 /* Create the ucontext */
5160 __put_user(0, &frame->uc.tuc_flags);
5161 __put_user(0, &frame->uc.tuc_link);
5162 __put_user(target_sigaltstack_used.ss_sp,
5163 &frame->uc.tuc_stack.ss_sp);
5164 __put_user(sas_ss_flags(env->aregs[7]),
5165 &frame->uc.tuc_stack.ss_flags);
5166 __put_user(target_sigaltstack_used.ss_size,
5167 &frame->uc.tuc_stack.ss_size);
5168 err |= target_rt_setup_ucontext(&frame->uc, env);
5170 if (err)
5171 goto give_sigsegv;
5173 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5174 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5177 /* Set up to return from userspace. */
5179 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5180 __put_user(retcode_addr, &frame->pretcode);
5182 /* moveq #,d0; notb d0; trap #0 */
5184 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5185 (uint32_t *)(frame->retcode + 0));
5186 __put_user(0x4e40, (uint16_t *)(frame->retcode + 4));
5188 if (err)
5189 goto give_sigsegv;
5191 /* Set up to return from userspace */
5193 env->aregs[7] = frame_addr;
5194 env->pc = ka->_sa_handler;
5196 unlock_user_struct(frame, frame_addr, 1);
5197 return;
5199 give_sigsegv:
5200 unlock_user_struct(frame, frame_addr, 1);
5201 force_sig(TARGET_SIGSEGV);
5204 long do_sigreturn(CPUM68KState *env)
5206 struct target_sigframe *frame;
5207 abi_ulong frame_addr = env->aregs[7] - 4;
5208 target_sigset_t target_set;
5209 sigset_t set;
5210 int d0, i;
5212 trace_user_do_sigreturn(env, frame_addr);
5213 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5214 goto badframe;
5216 /* set blocked signals */
5218 __get_user(target_set.sig[0], &frame->sc.sc_mask);
5220 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5221 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
5224 target_to_host_sigset_internal(&set, &target_set);
5225 do_sigprocmask(SIG_SETMASK, &set, NULL);
5227 /* restore registers */
5229 restore_sigcontext(env, &frame->sc, &d0);
5231 unlock_user_struct(frame, frame_addr, 0);
5232 return d0;
5234 badframe:
5235 force_sig(TARGET_SIGSEGV);
5236 return 0;
5239 long do_rt_sigreturn(CPUM68KState *env)
5241 struct target_rt_sigframe *frame;
5242 abi_ulong frame_addr = env->aregs[7] - 4;
5243 target_sigset_t target_set;
5244 sigset_t set;
5245 int d0;
5247 trace_user_do_rt_sigreturn(env, frame_addr);
5248 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5249 goto badframe;
5251 target_to_host_sigset_internal(&set, &target_set);
5252 do_sigprocmask(SIG_SETMASK, &set, NULL);
5254 /* restore registers */
5256 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5257 goto badframe;
5259 if (do_sigaltstack(frame_addr +
5260 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5261 0, get_sp_from_cpustate(env)) == -EFAULT)
5262 goto badframe;
5264 unlock_user_struct(frame, frame_addr, 0);
5265 return d0;
5267 badframe:
5268 unlock_user_struct(frame, frame_addr, 0);
5269 force_sig(TARGET_SIGSEGV);
5270 return 0;
5273 #elif defined(TARGET_ALPHA)
5275 struct target_sigcontext {
5276 abi_long sc_onstack;
5277 abi_long sc_mask;
5278 abi_long sc_pc;
5279 abi_long sc_ps;
5280 abi_long sc_regs[32];
5281 abi_long sc_ownedfp;
5282 abi_long sc_fpregs[32];
5283 abi_ulong sc_fpcr;
5284 abi_ulong sc_fp_control;
5285 abi_ulong sc_reserved1;
5286 abi_ulong sc_reserved2;
5287 abi_ulong sc_ssize;
5288 abi_ulong sc_sbase;
5289 abi_ulong sc_traparg_a0;
5290 abi_ulong sc_traparg_a1;
5291 abi_ulong sc_traparg_a2;
5292 abi_ulong sc_fp_trap_pc;
5293 abi_ulong sc_fp_trigger_sum;
5294 abi_ulong sc_fp_trigger_inst;
5297 struct target_ucontext {
5298 abi_ulong tuc_flags;
5299 abi_ulong tuc_link;
5300 abi_ulong tuc_osf_sigmask;
5301 target_stack_t tuc_stack;
5302 struct target_sigcontext tuc_mcontext;
5303 target_sigset_t tuc_sigmask;
5306 struct target_sigframe {
5307 struct target_sigcontext sc;
5308 unsigned int retcode[3];
5311 struct target_rt_sigframe {
5312 target_siginfo_t info;
5313 struct target_ucontext uc;
5314 unsigned int retcode[3];
5317 #define INSN_MOV_R30_R16 0x47fe0410
5318 #define INSN_LDI_R0 0x201f0000
5319 #define INSN_CALLSYS 0x00000083
5321 static void setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5322 abi_ulong frame_addr, target_sigset_t *set)
5324 int i;
5326 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5327 __put_user(set->sig[0], &sc->sc_mask);
5328 __put_user(env->pc, &sc->sc_pc);
5329 __put_user(8, &sc->sc_ps);
5331 for (i = 0; i < 31; ++i) {
5332 __put_user(env->ir[i], &sc->sc_regs[i]);
5334 __put_user(0, &sc->sc_regs[31]);
5336 for (i = 0; i < 31; ++i) {
5337 __put_user(env->fir[i], &sc->sc_fpregs[i]);
5339 __put_user(0, &sc->sc_fpregs[31]);
5340 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5342 __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5343 __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5344 __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5347 static void restore_sigcontext(CPUAlphaState *env,
5348 struct target_sigcontext *sc)
5350 uint64_t fpcr;
5351 int i;
5353 __get_user(env->pc, &sc->sc_pc);
5355 for (i = 0; i < 31; ++i) {
5356 __get_user(env->ir[i], &sc->sc_regs[i]);
5358 for (i = 0; i < 31; ++i) {
5359 __get_user(env->fir[i], &sc->sc_fpregs[i]);
5362 __get_user(fpcr, &sc->sc_fpcr);
5363 cpu_alpha_store_fpcr(env, fpcr);
5366 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5367 CPUAlphaState *env,
5368 unsigned long framesize)
5370 abi_ulong sp = env->ir[IR_SP];
5372 /* This is the X/Open sanctioned signal stack switching. */
5373 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5374 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5376 return (sp - framesize) & -32;
5379 static void setup_frame(int sig, struct target_sigaction *ka,
5380 target_sigset_t *set, CPUAlphaState *env)
5382 abi_ulong frame_addr, r26;
5383 struct target_sigframe *frame;
5384 int err = 0;
5386 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5387 trace_user_setup_frame(env, frame_addr);
5388 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5389 goto give_sigsegv;
5392 setup_sigcontext(&frame->sc, env, frame_addr, set);
5394 if (ka->sa_restorer) {
5395 r26 = ka->sa_restorer;
5396 } else {
5397 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5398 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5399 &frame->retcode[1]);
5400 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5401 /* imb() */
5402 r26 = frame_addr;
5405 unlock_user_struct(frame, frame_addr, 1);
5407 if (err) {
5408 give_sigsegv:
5409 if (sig == TARGET_SIGSEGV) {
5410 ka->_sa_handler = TARGET_SIG_DFL;
5412 force_sig(TARGET_SIGSEGV);
5415 env->ir[IR_RA] = r26;
5416 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5417 env->ir[IR_A0] = sig;
5418 env->ir[IR_A1] = 0;
5419 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5420 env->ir[IR_SP] = frame_addr;
5423 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5424 target_siginfo_t *info,
5425 target_sigset_t *set, CPUAlphaState *env)
5427 abi_ulong frame_addr, r26;
5428 struct target_rt_sigframe *frame;
5429 int i, err = 0;
5431 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5432 trace_user_setup_rt_frame(env, frame_addr);
5433 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5434 goto give_sigsegv;
5437 tswap_siginfo(&frame->info, info);
5439 __put_user(0, &frame->uc.tuc_flags);
5440 __put_user(0, &frame->uc.tuc_link);
5441 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5442 __put_user(target_sigaltstack_used.ss_sp,
5443 &frame->uc.tuc_stack.ss_sp);
5444 __put_user(sas_ss_flags(env->ir[IR_SP]),
5445 &frame->uc.tuc_stack.ss_flags);
5446 __put_user(target_sigaltstack_used.ss_size,
5447 &frame->uc.tuc_stack.ss_size);
5448 setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5449 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5450 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5453 if (ka->sa_restorer) {
5454 r26 = ka->sa_restorer;
5455 } else {
5456 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5457 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5458 &frame->retcode[1]);
5459 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5460 /* imb(); */
5461 r26 = frame_addr;
5464 if (err) {
5465 give_sigsegv:
5466 if (sig == TARGET_SIGSEGV) {
5467 ka->_sa_handler = TARGET_SIG_DFL;
5469 force_sig(TARGET_SIGSEGV);
5472 env->ir[IR_RA] = r26;
5473 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5474 env->ir[IR_A0] = sig;
5475 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5476 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5477 env->ir[IR_SP] = frame_addr;
5480 long do_sigreturn(CPUAlphaState *env)
5482 struct target_sigcontext *sc;
5483 abi_ulong sc_addr = env->ir[IR_A0];
5484 target_sigset_t target_set;
5485 sigset_t set;
5487 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5488 goto badframe;
5491 target_sigemptyset(&target_set);
5492 __get_user(target_set.sig[0], &sc->sc_mask);
5494 target_to_host_sigset_internal(&set, &target_set);
5495 do_sigprocmask(SIG_SETMASK, &set, NULL);
5497 restore_sigcontext(env, sc);
5498 unlock_user_struct(sc, sc_addr, 0);
5499 return env->ir[IR_V0];
5501 badframe:
5502 force_sig(TARGET_SIGSEGV);
5505 long do_rt_sigreturn(CPUAlphaState *env)
5507 abi_ulong frame_addr = env->ir[IR_A0];
5508 struct target_rt_sigframe *frame;
5509 sigset_t set;
5511 trace_user_do_rt_sigreturn(env, frame_addr);
5512 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5513 goto badframe;
5515 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5516 do_sigprocmask(SIG_SETMASK, &set, NULL);
5518 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5519 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5520 uc.tuc_stack),
5521 0, env->ir[IR_SP]) == -EFAULT) {
5522 goto badframe;
5525 unlock_user_struct(frame, frame_addr, 0);
5526 return env->ir[IR_V0];
5529 badframe:
5530 unlock_user_struct(frame, frame_addr, 0);
5531 force_sig(TARGET_SIGSEGV);
5534 #elif defined(TARGET_TILEGX)
5536 struct target_sigcontext {
5537 union {
5538 /* General-purpose registers. */
5539 abi_ulong gregs[56];
5540 struct {
5541 abi_ulong __gregs[53];
5542 abi_ulong tp; /* Aliases gregs[TREG_TP]. */
5543 abi_ulong sp; /* Aliases gregs[TREG_SP]. */
5544 abi_ulong lr; /* Aliases gregs[TREG_LR]. */
5547 abi_ulong pc; /* Program counter. */
5548 abi_ulong ics; /* In Interrupt Critical Section? */
5549 abi_ulong faultnum; /* Fault number. */
5550 abi_ulong pad[5];
5553 struct target_ucontext {
5554 abi_ulong tuc_flags;
5555 abi_ulong tuc_link;
5556 target_stack_t tuc_stack;
5557 struct target_sigcontext tuc_mcontext;
5558 target_sigset_t tuc_sigmask; /* mask last for extensibility */
5561 struct target_rt_sigframe {
5562 unsigned char save_area[16]; /* caller save area */
5563 struct target_siginfo info;
5564 struct target_ucontext uc;
5567 static void setup_sigcontext(struct target_sigcontext *sc,
5568 CPUArchState *env, int signo)
5570 int i;
5572 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5573 __put_user(env->regs[i], &sc->gregs[i]);
5576 __put_user(env->pc, &sc->pc);
5577 __put_user(0, &sc->ics);
5578 __put_user(signo, &sc->faultnum);
5581 static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
5583 int i;
5585 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5586 __get_user(env->regs[i], &sc->gregs[i]);
5589 __get_user(env->pc, &sc->pc);
5592 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
5593 size_t frame_size)
5595 unsigned long sp = env->regs[TILEGX_R_SP];
5597 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
5598 return -1UL;
5601 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
5602 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5605 sp -= frame_size;
5606 sp &= -16UL;
5607 return sp;
5610 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5611 target_siginfo_t *info,
5612 target_sigset_t *set, CPUArchState *env)
5614 abi_ulong frame_addr;
5615 struct target_rt_sigframe *frame;
5616 unsigned long restorer;
5618 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5619 trace_user_setup_rt_frame(env, frame_addr);
5620 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5621 goto give_sigsegv;
5624 /* Always write at least the signal number for the stack backtracer. */
5625 if (ka->sa_flags & TARGET_SA_SIGINFO) {
5626 /* At sigreturn time, restore the callee-save registers too. */
5627 tswap_siginfo(&frame->info, info);
5628 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
5629 } else {
5630 __put_user(info->si_signo, &frame->info.si_signo);
5633 /* Create the ucontext. */
5634 __put_user(0, &frame->uc.tuc_flags);
5635 __put_user(0, &frame->uc.tuc_link);
5636 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
5637 __put_user(sas_ss_flags(env->regs[TILEGX_R_SP]),
5638 &frame->uc.tuc_stack.ss_flags);
5639 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
5640 setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
5642 restorer = (unsigned long) do_rt_sigreturn;
5643 if (ka->sa_flags & TARGET_SA_RESTORER) {
5644 restorer = (unsigned long) ka->sa_restorer;
5646 env->pc = (unsigned long) ka->_sa_handler;
5647 env->regs[TILEGX_R_SP] = (unsigned long) frame;
5648 env->regs[TILEGX_R_LR] = restorer;
5649 env->regs[0] = (unsigned long) sig;
5650 env->regs[1] = (unsigned long) &frame->info;
5651 env->regs[2] = (unsigned long) &frame->uc;
5652 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
5654 unlock_user_struct(frame, frame_addr, 1);
5655 return;
5657 give_sigsegv:
5658 if (sig == TARGET_SIGSEGV) {
5659 ka->_sa_handler = TARGET_SIG_DFL;
5661 force_sig(TARGET_SIGSEGV /* , current */);
5664 long do_rt_sigreturn(CPUTLGState *env)
5666 abi_ulong frame_addr = env->regs[TILEGX_R_SP];
5667 struct target_rt_sigframe *frame;
5668 sigset_t set;
5670 trace_user_do_rt_sigreturn(env, frame_addr);
5671 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5672 goto badframe;
5674 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5675 do_sigprocmask(SIG_SETMASK, &set, NULL);
5677 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5678 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5679 uc.tuc_stack),
5680 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
5681 goto badframe;
5684 unlock_user_struct(frame, frame_addr, 0);
5685 return env->regs[TILEGX_R_RE];
5688 badframe:
5689 unlock_user_struct(frame, frame_addr, 0);
5690 force_sig(TARGET_SIGSEGV);
5693 #else
5695 static void setup_frame(int sig, struct target_sigaction *ka,
5696 target_sigset_t *set, CPUArchState *env)
5698 fprintf(stderr, "setup_frame: not implemented\n");
5701 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5702 target_siginfo_t *info,
5703 target_sigset_t *set, CPUArchState *env)
5705 fprintf(stderr, "setup_rt_frame: not implemented\n");
5708 long do_sigreturn(CPUArchState *env)
5710 fprintf(stderr, "do_sigreturn: not implemented\n");
5711 return -TARGET_ENOSYS;
5714 long do_rt_sigreturn(CPUArchState *env)
5716 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5717 return -TARGET_ENOSYS;
5720 #endif
5722 void process_pending_signals(CPUArchState *cpu_env)
5724 CPUState *cpu = ENV_GET_CPU(cpu_env);
5725 int sig;
5726 abi_ulong handler;
5727 sigset_t set, old_set;
5728 target_sigset_t target_old_set;
5729 struct emulated_sigtable *k;
5730 struct target_sigaction *sa;
5731 struct sigqueue *q;
5732 TaskState *ts = cpu->opaque;
5734 if (!ts->signal_pending)
5735 return;
5737 /* FIXME: This is not threadsafe. */
5738 k = ts->sigtab;
5739 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5740 if (k->pending)
5741 goto handle_signal;
5742 k++;
5744 /* if no signal is pending, just return */
5745 ts->signal_pending = 0;
5746 return;
5748 handle_signal:
5749 trace_user_handle_signal(cpu_env, sig);
5750 /* dequeue signal */
5751 q = k->first;
5752 k->first = q->next;
5753 if (!k->first)
5754 k->pending = 0;
5756 sig = gdb_handlesig(cpu, sig);
5757 if (!sig) {
5758 sa = NULL;
5759 handler = TARGET_SIG_IGN;
5760 } else {
5761 sa = &sigact_table[sig - 1];
5762 handler = sa->_sa_handler;
5765 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5766 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5767 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5768 * because it got a real MMU fault), and treat as if default handler.
5770 handler = TARGET_SIG_DFL;
5773 if (handler == TARGET_SIG_DFL) {
5774 /* default handler : ignore some signal. The other are job control or fatal */
5775 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5776 kill(getpid(),SIGSTOP);
5777 } else if (sig != TARGET_SIGCHLD &&
5778 sig != TARGET_SIGURG &&
5779 sig != TARGET_SIGWINCH &&
5780 sig != TARGET_SIGCONT) {
5781 force_sig(sig);
5783 } else if (handler == TARGET_SIG_IGN) {
5784 /* ignore sig */
5785 } else if (handler == TARGET_SIG_ERR) {
5786 force_sig(sig);
5787 } else {
5788 /* compute the blocked signals during the handler execution */
5789 target_to_host_sigset(&set, &sa->sa_mask);
5790 /* SA_NODEFER indicates that the current signal should not be
5791 blocked during the handler */
5792 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5793 sigaddset(&set, target_to_host_signal(sig));
5795 /* block signals in the handler using Linux */
5796 do_sigprocmask(SIG_BLOCK, &set, &old_set);
5797 /* save the previous blocked signal state to restore it at the
5798 end of the signal execution (see do_sigreturn) */
5799 host_to_target_sigset_internal(&target_old_set, &old_set);
5801 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5802 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5804 CPUX86State *env = cpu_env;
5805 if (env->eflags & VM_MASK)
5806 save_v86_state(env);
5808 #endif
5809 /* prepare the stack frame of the virtual CPU */
5810 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \
5811 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
5812 /* These targets do not have traditional signals. */
5813 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5814 #else
5815 if (sa->sa_flags & TARGET_SA_SIGINFO)
5816 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5817 else
5818 setup_frame(sig, sa, &target_old_set, cpu_env);
5819 #endif
5820 if (sa->sa_flags & TARGET_SA_RESETHAND)
5821 sa->_sa_handler = TARGET_SIG_DFL;
5823 if (q != &k->info)
5824 free_sigqueue(cpu_env, q);