vmxnet3: Introduce 'x-old-msi-offsets' back-compat property
[qemu.git] / linux-user / signal.c
blob919aa836fafe6840585dae8ea4161949e9950aa8
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;
3213 int err = 0;
3215 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3216 trace_user_setup_frame(regs, frame_addr);
3217 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3218 goto give_sigsegv;
3220 setup_sigcontext(&frame->sc, regs, set->sig[0]);
3222 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3223 __put_user(set->sig[i + 1], &frame->extramask[i]);
3226 /* Set up to return from userspace. If provided, use a stub
3227 already in userspace. */
3228 if (ka->sa_flags & TARGET_SA_RESTORER) {
3229 regs->pr = (unsigned long) ka->sa_restorer;
3230 } else {
3231 /* Generate return code (system call to sigreturn) */
3232 __put_user(MOVW(2), &frame->retcode[0]);
3233 __put_user(TRAP_NOARG, &frame->retcode[1]);
3234 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3235 regs->pr = (unsigned long) frame->retcode;
3238 if (err)
3239 goto give_sigsegv;
3241 /* Set up registers for signal handler */
3242 regs->gregs[15] = frame_addr;
3243 regs->gregs[4] = sig; /* Arg for signal handler */
3244 regs->gregs[5] = 0;
3245 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3246 regs->pc = (unsigned long) ka->_sa_handler;
3248 unlock_user_struct(frame, frame_addr, 1);
3249 return;
3251 give_sigsegv:
3252 unlock_user_struct(frame, frame_addr, 1);
3253 force_sig(TARGET_SIGSEGV);
3256 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3257 target_siginfo_t *info,
3258 target_sigset_t *set, CPUSH4State *regs)
3260 struct target_rt_sigframe *frame;
3261 abi_ulong frame_addr;
3262 int i;
3263 int err = 0;
3265 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3266 trace_user_setup_rt_frame(regs, frame_addr);
3267 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3268 goto give_sigsegv;
3270 tswap_siginfo(&frame->info, info);
3272 /* Create the ucontext. */
3273 __put_user(0, &frame->uc.tuc_flags);
3274 __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3275 __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3276 &frame->uc.tuc_stack.ss_sp);
3277 __put_user(sas_ss_flags(regs->gregs[15]),
3278 &frame->uc.tuc_stack.ss_flags);
3279 __put_user(target_sigaltstack_used.ss_size,
3280 &frame->uc.tuc_stack.ss_size);
3281 setup_sigcontext(&frame->uc.tuc_mcontext,
3282 regs, set->sig[0]);
3283 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3284 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3287 /* Set up to return from userspace. If provided, use a stub
3288 already in userspace. */
3289 if (ka->sa_flags & TARGET_SA_RESTORER) {
3290 regs->pr = (unsigned long) ka->sa_restorer;
3291 } else {
3292 /* Generate return code (system call to sigreturn) */
3293 __put_user(MOVW(2), &frame->retcode[0]);
3294 __put_user(TRAP_NOARG, &frame->retcode[1]);
3295 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3296 regs->pr = (unsigned long) frame->retcode;
3299 if (err)
3300 goto give_sigsegv;
3302 /* Set up registers for signal handler */
3303 regs->gregs[15] = frame_addr;
3304 regs->gregs[4] = sig; /* Arg for signal handler */
3305 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3306 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3307 regs->pc = (unsigned long) ka->_sa_handler;
3309 unlock_user_struct(frame, frame_addr, 1);
3310 return;
3312 give_sigsegv:
3313 unlock_user_struct(frame, frame_addr, 1);
3314 force_sig(TARGET_SIGSEGV);
3317 long do_sigreturn(CPUSH4State *regs)
3319 struct target_sigframe *frame;
3320 abi_ulong frame_addr;
3321 sigset_t blocked;
3322 target_sigset_t target_set;
3323 target_ulong r0;
3324 int i;
3325 int err = 0;
3327 frame_addr = regs->gregs[15];
3328 trace_user_do_sigreturn(regs, frame_addr);
3329 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3330 goto badframe;
3332 __get_user(target_set.sig[0], &frame->sc.oldmask);
3333 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3334 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3337 if (err)
3338 goto badframe;
3340 target_to_host_sigset_internal(&blocked, &target_set);
3341 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3343 restore_sigcontext(regs, &frame->sc, &r0);
3345 unlock_user_struct(frame, frame_addr, 0);
3346 return r0;
3348 badframe:
3349 unlock_user_struct(frame, frame_addr, 0);
3350 force_sig(TARGET_SIGSEGV);
3351 return 0;
3354 long do_rt_sigreturn(CPUSH4State *regs)
3356 struct target_rt_sigframe *frame;
3357 abi_ulong frame_addr;
3358 sigset_t blocked;
3359 target_ulong r0;
3361 frame_addr = regs->gregs[15];
3362 trace_user_do_rt_sigreturn(regs, frame_addr);
3363 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3364 goto badframe;
3366 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3367 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
3369 restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0);
3371 if (do_sigaltstack(frame_addr +
3372 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3373 0, get_sp_from_cpustate(regs)) == -EFAULT)
3374 goto badframe;
3376 unlock_user_struct(frame, frame_addr, 0);
3377 return r0;
3379 badframe:
3380 unlock_user_struct(frame, frame_addr, 0);
3381 force_sig(TARGET_SIGSEGV);
3382 return 0;
3384 #elif defined(TARGET_MICROBLAZE)
3386 struct target_sigcontext {
3387 struct target_pt_regs regs; /* needs to be first */
3388 uint32_t oldmask;
3391 struct target_stack_t {
3392 abi_ulong ss_sp;
3393 int ss_flags;
3394 unsigned int ss_size;
3397 struct target_ucontext {
3398 abi_ulong tuc_flags;
3399 abi_ulong tuc_link;
3400 struct target_stack_t tuc_stack;
3401 struct target_sigcontext tuc_mcontext;
3402 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3405 /* Signal frames. */
3406 struct target_signal_frame {
3407 struct target_ucontext uc;
3408 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3409 uint32_t tramp[2];
3412 struct rt_signal_frame {
3413 siginfo_t info;
3414 struct ucontext uc;
3415 uint32_t tramp[2];
3418 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3420 __put_user(env->regs[0], &sc->regs.r0);
3421 __put_user(env->regs[1], &sc->regs.r1);
3422 __put_user(env->regs[2], &sc->regs.r2);
3423 __put_user(env->regs[3], &sc->regs.r3);
3424 __put_user(env->regs[4], &sc->regs.r4);
3425 __put_user(env->regs[5], &sc->regs.r5);
3426 __put_user(env->regs[6], &sc->regs.r6);
3427 __put_user(env->regs[7], &sc->regs.r7);
3428 __put_user(env->regs[8], &sc->regs.r8);
3429 __put_user(env->regs[9], &sc->regs.r9);
3430 __put_user(env->regs[10], &sc->regs.r10);
3431 __put_user(env->regs[11], &sc->regs.r11);
3432 __put_user(env->regs[12], &sc->regs.r12);
3433 __put_user(env->regs[13], &sc->regs.r13);
3434 __put_user(env->regs[14], &sc->regs.r14);
3435 __put_user(env->regs[15], &sc->regs.r15);
3436 __put_user(env->regs[16], &sc->regs.r16);
3437 __put_user(env->regs[17], &sc->regs.r17);
3438 __put_user(env->regs[18], &sc->regs.r18);
3439 __put_user(env->regs[19], &sc->regs.r19);
3440 __put_user(env->regs[20], &sc->regs.r20);
3441 __put_user(env->regs[21], &sc->regs.r21);
3442 __put_user(env->regs[22], &sc->regs.r22);
3443 __put_user(env->regs[23], &sc->regs.r23);
3444 __put_user(env->regs[24], &sc->regs.r24);
3445 __put_user(env->regs[25], &sc->regs.r25);
3446 __put_user(env->regs[26], &sc->regs.r26);
3447 __put_user(env->regs[27], &sc->regs.r27);
3448 __put_user(env->regs[28], &sc->regs.r28);
3449 __put_user(env->regs[29], &sc->regs.r29);
3450 __put_user(env->regs[30], &sc->regs.r30);
3451 __put_user(env->regs[31], &sc->regs.r31);
3452 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3455 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3457 __get_user(env->regs[0], &sc->regs.r0);
3458 __get_user(env->regs[1], &sc->regs.r1);
3459 __get_user(env->regs[2], &sc->regs.r2);
3460 __get_user(env->regs[3], &sc->regs.r3);
3461 __get_user(env->regs[4], &sc->regs.r4);
3462 __get_user(env->regs[5], &sc->regs.r5);
3463 __get_user(env->regs[6], &sc->regs.r6);
3464 __get_user(env->regs[7], &sc->regs.r7);
3465 __get_user(env->regs[8], &sc->regs.r8);
3466 __get_user(env->regs[9], &sc->regs.r9);
3467 __get_user(env->regs[10], &sc->regs.r10);
3468 __get_user(env->regs[11], &sc->regs.r11);
3469 __get_user(env->regs[12], &sc->regs.r12);
3470 __get_user(env->regs[13], &sc->regs.r13);
3471 __get_user(env->regs[14], &sc->regs.r14);
3472 __get_user(env->regs[15], &sc->regs.r15);
3473 __get_user(env->regs[16], &sc->regs.r16);
3474 __get_user(env->regs[17], &sc->regs.r17);
3475 __get_user(env->regs[18], &sc->regs.r18);
3476 __get_user(env->regs[19], &sc->regs.r19);
3477 __get_user(env->regs[20], &sc->regs.r20);
3478 __get_user(env->regs[21], &sc->regs.r21);
3479 __get_user(env->regs[22], &sc->regs.r22);
3480 __get_user(env->regs[23], &sc->regs.r23);
3481 __get_user(env->regs[24], &sc->regs.r24);
3482 __get_user(env->regs[25], &sc->regs.r25);
3483 __get_user(env->regs[26], &sc->regs.r26);
3484 __get_user(env->regs[27], &sc->regs.r27);
3485 __get_user(env->regs[28], &sc->regs.r28);
3486 __get_user(env->regs[29], &sc->regs.r29);
3487 __get_user(env->regs[30], &sc->regs.r30);
3488 __get_user(env->regs[31], &sc->regs.r31);
3489 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3492 static abi_ulong get_sigframe(struct target_sigaction *ka,
3493 CPUMBState *env, int frame_size)
3495 abi_ulong sp = env->regs[1];
3497 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !on_sig_stack(sp)) {
3498 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3501 return ((sp - frame_size) & -8UL);
3504 static void setup_frame(int sig, struct target_sigaction *ka,
3505 target_sigset_t *set, CPUMBState *env)
3507 struct target_signal_frame *frame;
3508 abi_ulong frame_addr;
3509 int i;
3511 frame_addr = get_sigframe(ka, env, sizeof *frame);
3512 trace_user_setup_frame(env, frame_addr);
3513 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3514 goto badframe;
3516 /* Save the mask. */
3517 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3519 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3520 __put_user(set->sig[i], &frame->extramask[i - 1]);
3523 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3525 /* Set up to return from userspace. If provided, use a stub
3526 already in userspace. */
3527 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3528 if (ka->sa_flags & TARGET_SA_RESTORER) {
3529 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3530 } else {
3531 uint32_t t;
3532 /* Note, these encodings are _big endian_! */
3533 /* addi r12, r0, __NR_sigreturn */
3534 t = 0x31800000UL | TARGET_NR_sigreturn;
3535 __put_user(t, frame->tramp + 0);
3536 /* brki r14, 0x8 */
3537 t = 0xb9cc0008UL;
3538 __put_user(t, frame->tramp + 1);
3540 /* Return from sighandler will jump to the tramp.
3541 Negative 8 offset because return is rtsd r15, 8 */
3542 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3545 /* Set up registers for signal handler */
3546 env->regs[1] = frame_addr;
3547 /* Signal handler args: */
3548 env->regs[5] = sig; /* Arg 0: signum */
3549 env->regs[6] = 0;
3550 /* arg 1: sigcontext */
3551 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3553 /* Offset of 4 to handle microblaze rtid r14, 0 */
3554 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3556 unlock_user_struct(frame, frame_addr, 1);
3557 return;
3558 badframe:
3559 force_sig(TARGET_SIGSEGV);
3562 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3563 target_siginfo_t *info,
3564 target_sigset_t *set, CPUMBState *env)
3566 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3569 long do_sigreturn(CPUMBState *env)
3571 struct target_signal_frame *frame;
3572 abi_ulong frame_addr;
3573 target_sigset_t target_set;
3574 sigset_t set;
3575 int i;
3577 frame_addr = env->regs[R_SP];
3578 trace_user_do_sigreturn(env, frame_addr);
3579 /* Make sure the guest isn't playing games. */
3580 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3581 goto badframe;
3583 /* Restore blocked signals */
3584 __get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask);
3585 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3586 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3588 target_to_host_sigset_internal(&set, &target_set);
3589 do_sigprocmask(SIG_SETMASK, &set, NULL);
3591 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3592 /* We got here through a sigreturn syscall, our path back is via an
3593 rtb insn so setup r14 for that. */
3594 env->regs[14] = env->sregs[SR_PC];
3596 unlock_user_struct(frame, frame_addr, 0);
3597 return env->regs[10];
3598 badframe:
3599 force_sig(TARGET_SIGSEGV);
3602 long do_rt_sigreturn(CPUMBState *env)
3604 trace_user_do_rt_sigreturn(env, 0);
3605 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3606 return -TARGET_ENOSYS;
3609 #elif defined(TARGET_CRIS)
3611 struct target_sigcontext {
3612 struct target_pt_regs regs; /* needs to be first */
3613 uint32_t oldmask;
3614 uint32_t usp; /* usp before stacking this gunk on it */
3617 /* Signal frames. */
3618 struct target_signal_frame {
3619 struct target_sigcontext sc;
3620 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3621 uint16_t retcode[4]; /* Trampoline code. */
3624 struct rt_signal_frame {
3625 siginfo_t *pinfo;
3626 void *puc;
3627 siginfo_t info;
3628 struct ucontext uc;
3629 uint16_t retcode[4]; /* Trampoline code. */
3632 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3634 __put_user(env->regs[0], &sc->regs.r0);
3635 __put_user(env->regs[1], &sc->regs.r1);
3636 __put_user(env->regs[2], &sc->regs.r2);
3637 __put_user(env->regs[3], &sc->regs.r3);
3638 __put_user(env->regs[4], &sc->regs.r4);
3639 __put_user(env->regs[5], &sc->regs.r5);
3640 __put_user(env->regs[6], &sc->regs.r6);
3641 __put_user(env->regs[7], &sc->regs.r7);
3642 __put_user(env->regs[8], &sc->regs.r8);
3643 __put_user(env->regs[9], &sc->regs.r9);
3644 __put_user(env->regs[10], &sc->regs.r10);
3645 __put_user(env->regs[11], &sc->regs.r11);
3646 __put_user(env->regs[12], &sc->regs.r12);
3647 __put_user(env->regs[13], &sc->regs.r13);
3648 __put_user(env->regs[14], &sc->usp);
3649 __put_user(env->regs[15], &sc->regs.acr);
3650 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3651 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3652 __put_user(env->pc, &sc->regs.erp);
3655 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3657 __get_user(env->regs[0], &sc->regs.r0);
3658 __get_user(env->regs[1], &sc->regs.r1);
3659 __get_user(env->regs[2], &sc->regs.r2);
3660 __get_user(env->regs[3], &sc->regs.r3);
3661 __get_user(env->regs[4], &sc->regs.r4);
3662 __get_user(env->regs[5], &sc->regs.r5);
3663 __get_user(env->regs[6], &sc->regs.r6);
3664 __get_user(env->regs[7], &sc->regs.r7);
3665 __get_user(env->regs[8], &sc->regs.r8);
3666 __get_user(env->regs[9], &sc->regs.r9);
3667 __get_user(env->regs[10], &sc->regs.r10);
3668 __get_user(env->regs[11], &sc->regs.r11);
3669 __get_user(env->regs[12], &sc->regs.r12);
3670 __get_user(env->regs[13], &sc->regs.r13);
3671 __get_user(env->regs[14], &sc->usp);
3672 __get_user(env->regs[15], &sc->regs.acr);
3673 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3674 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3675 __get_user(env->pc, &sc->regs.erp);
3678 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3680 abi_ulong sp;
3681 /* Align the stack downwards to 4. */
3682 sp = (env->regs[R_SP] & ~3);
3683 return sp - framesize;
3686 static void setup_frame(int sig, struct target_sigaction *ka,
3687 target_sigset_t *set, CPUCRISState *env)
3689 struct target_signal_frame *frame;
3690 abi_ulong frame_addr;
3691 int i;
3693 frame_addr = get_sigframe(env, sizeof *frame);
3694 trace_user_setup_frame(env, frame_addr);
3695 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3696 goto badframe;
3699 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3700 * use this trampoline anymore but it sets it up for GDB.
3701 * In QEMU, using the trampoline simplifies things a bit so we use it.
3703 * This is movu.w __NR_sigreturn, r9; break 13;
3705 __put_user(0x9c5f, frame->retcode+0);
3706 __put_user(TARGET_NR_sigreturn,
3707 frame->retcode + 1);
3708 __put_user(0xe93d, frame->retcode + 2);
3710 /* Save the mask. */
3711 __put_user(set->sig[0], &frame->sc.oldmask);
3713 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3714 __put_user(set->sig[i], &frame->extramask[i - 1]);
3717 setup_sigcontext(&frame->sc, env);
3719 /* Move the stack and setup the arguments for the handler. */
3720 env->regs[R_SP] = frame_addr;
3721 env->regs[10] = sig;
3722 env->pc = (unsigned long) ka->_sa_handler;
3723 /* Link SRP so the guest returns through the trampoline. */
3724 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3726 unlock_user_struct(frame, frame_addr, 1);
3727 return;
3728 badframe:
3729 force_sig(TARGET_SIGSEGV);
3732 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3733 target_siginfo_t *info,
3734 target_sigset_t *set, CPUCRISState *env)
3736 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3739 long do_sigreturn(CPUCRISState *env)
3741 struct target_signal_frame *frame;
3742 abi_ulong frame_addr;
3743 target_sigset_t target_set;
3744 sigset_t set;
3745 int i;
3747 frame_addr = env->regs[R_SP];
3748 trace_user_do_sigreturn(env, frame_addr);
3749 /* Make sure the guest isn't playing games. */
3750 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3751 goto badframe;
3753 /* Restore blocked signals */
3754 __get_user(target_set.sig[0], &frame->sc.oldmask);
3755 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3756 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3758 target_to_host_sigset_internal(&set, &target_set);
3759 do_sigprocmask(SIG_SETMASK, &set, NULL);
3761 restore_sigcontext(&frame->sc, env);
3762 unlock_user_struct(frame, frame_addr, 0);
3763 return env->regs[10];
3764 badframe:
3765 force_sig(TARGET_SIGSEGV);
3768 long do_rt_sigreturn(CPUCRISState *env)
3770 trace_user_do_rt_sigreturn(env, 0);
3771 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3772 return -TARGET_ENOSYS;
3775 #elif defined(TARGET_OPENRISC)
3777 struct target_sigcontext {
3778 struct target_pt_regs regs;
3779 abi_ulong oldmask;
3780 abi_ulong usp;
3783 struct target_ucontext {
3784 abi_ulong tuc_flags;
3785 abi_ulong tuc_link;
3786 target_stack_t tuc_stack;
3787 struct target_sigcontext tuc_mcontext;
3788 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3791 struct target_rt_sigframe {
3792 abi_ulong pinfo;
3793 uint64_t puc;
3794 struct target_siginfo info;
3795 struct target_sigcontext sc;
3796 struct target_ucontext uc;
3797 unsigned char retcode[16]; /* trampoline code */
3800 /* This is the asm-generic/ucontext.h version */
3801 #if 0
3802 static int restore_sigcontext(CPUOpenRISCState *regs,
3803 struct target_sigcontext *sc)
3805 unsigned int err = 0;
3806 unsigned long old_usp;
3808 /* Alwys make any pending restarted system call return -EINTR */
3809 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3811 /* restore the regs from &sc->regs (same as sc, since regs is first)
3812 * (sc is already checked for VERIFY_READ since the sigframe was
3813 * checked in sys_sigreturn previously)
3816 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3817 goto badframe;
3820 /* make sure the U-flag is set so user-mode cannot fool us */
3822 regs->sr &= ~SR_SM;
3824 /* restore the old USP as it was before we stacked the sc etc.
3825 * (we cannot just pop the sigcontext since we aligned the sp and
3826 * stuff after pushing it)
3829 __get_user(old_usp, &sc->usp);
3830 phx_signal("old_usp 0x%lx", old_usp);
3832 __PHX__ REALLY /* ??? */
3833 wrusp(old_usp);
3834 regs->gpr[1] = old_usp;
3836 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3837 * after this completes, but we don't use that mechanism. maybe we can
3838 * use it now ?
3841 return err;
3843 badframe:
3844 return 1;
3846 #endif
3848 /* Set up a signal frame. */
3850 static void setup_sigcontext(struct target_sigcontext *sc,
3851 CPUOpenRISCState *regs,
3852 unsigned long mask)
3854 unsigned long usp = regs->gpr[1];
3856 /* copy the regs. they are first in sc so we can use sc directly */
3858 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3860 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3861 the signal handler. The frametype will be restored to its previous
3862 value in restore_sigcontext. */
3863 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3865 /* then some other stuff */
3866 __put_user(mask, &sc->oldmask);
3867 __put_user(usp, &sc->usp);
3870 static inline unsigned long align_sigframe(unsigned long sp)
3872 unsigned long i;
3873 i = sp & ~3UL;
3874 return i;
3877 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3878 CPUOpenRISCState *regs,
3879 size_t frame_size)
3881 unsigned long sp = regs->gpr[1];
3882 int onsigstack = on_sig_stack(sp);
3884 /* redzone */
3885 /* This is the X/Open sanctioned signal stack switching. */
3886 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !onsigstack) {
3887 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3890 sp = align_sigframe(sp - frame_size);
3893 * If we are on the alternate signal stack and would overflow it, don't.
3894 * Return an always-bogus address instead so we will die with SIGSEGV.
3897 if (onsigstack && !likely(on_sig_stack(sp))) {
3898 return -1L;
3901 return sp;
3904 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3905 target_siginfo_t *info,
3906 target_sigset_t *set, CPUOpenRISCState *env)
3908 int err = 0;
3909 abi_ulong frame_addr;
3910 unsigned long return_ip;
3911 struct target_rt_sigframe *frame;
3912 abi_ulong info_addr, uc_addr;
3914 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3915 trace_user_setup_rt_frame(env, frame_addr);
3916 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3917 goto give_sigsegv;
3920 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3921 __put_user(info_addr, &frame->pinfo);
3922 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3923 __put_user(uc_addr, &frame->puc);
3925 if (ka->sa_flags & SA_SIGINFO) {
3926 tswap_siginfo(&frame->info, info);
3929 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3930 __put_user(0, &frame->uc.tuc_flags);
3931 __put_user(0, &frame->uc.tuc_link);
3932 __put_user(target_sigaltstack_used.ss_sp,
3933 &frame->uc.tuc_stack.ss_sp);
3934 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3935 __put_user(target_sigaltstack_used.ss_size,
3936 &frame->uc.tuc_stack.ss_size);
3937 setup_sigcontext(&frame->sc, env, set->sig[0]);
3939 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3941 /* trampoline - the desired return ip is the retcode itself */
3942 return_ip = (unsigned long)&frame->retcode;
3943 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3944 __put_user(0xa960, (short *)(frame->retcode + 0));
3945 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3946 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3947 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3949 if (err) {
3950 goto give_sigsegv;
3953 /* TODO what is the current->exec_domain stuff and invmap ? */
3955 /* Set up registers for signal handler */
3956 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3957 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3958 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3959 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3960 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3962 /* actually move the usp to reflect the stacked frame */
3963 env->gpr[1] = (unsigned long)frame;
3965 return;
3967 give_sigsegv:
3968 unlock_user_struct(frame, frame_addr, 1);
3969 if (sig == TARGET_SIGSEGV) {
3970 ka->_sa_handler = TARGET_SIG_DFL;
3972 force_sig(TARGET_SIGSEGV);
3975 long do_sigreturn(CPUOpenRISCState *env)
3977 trace_user_do_sigreturn(env, 0);
3978 fprintf(stderr, "do_sigreturn: not implemented\n");
3979 return -TARGET_ENOSYS;
3982 long do_rt_sigreturn(CPUOpenRISCState *env)
3984 trace_user_do_rt_sigreturn(env, 0);
3985 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
3986 return -TARGET_ENOSYS;
3988 /* TARGET_OPENRISC */
3990 #elif defined(TARGET_S390X)
3992 #define __NUM_GPRS 16
3993 #define __NUM_FPRS 16
3994 #define __NUM_ACRS 16
3996 #define S390_SYSCALL_SIZE 2
3997 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3999 #define _SIGCONTEXT_NSIG 64
4000 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4001 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4002 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4003 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4004 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4006 typedef struct {
4007 target_psw_t psw;
4008 target_ulong gprs[__NUM_GPRS];
4009 unsigned int acrs[__NUM_ACRS];
4010 } target_s390_regs_common;
4012 typedef struct {
4013 unsigned int fpc;
4014 double fprs[__NUM_FPRS];
4015 } target_s390_fp_regs;
4017 typedef struct {
4018 target_s390_regs_common regs;
4019 target_s390_fp_regs fpregs;
4020 } target_sigregs;
4022 struct target_sigcontext {
4023 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4024 target_sigregs *sregs;
4027 typedef struct {
4028 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4029 struct target_sigcontext sc;
4030 target_sigregs sregs;
4031 int signo;
4032 uint8_t retcode[S390_SYSCALL_SIZE];
4033 } sigframe;
4035 struct target_ucontext {
4036 target_ulong tuc_flags;
4037 struct target_ucontext *tuc_link;
4038 target_stack_t tuc_stack;
4039 target_sigregs tuc_mcontext;
4040 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4043 typedef struct {
4044 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4045 uint8_t retcode[S390_SYSCALL_SIZE];
4046 struct target_siginfo info;
4047 struct target_ucontext uc;
4048 } rt_sigframe;
4050 static inline abi_ulong
4051 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4053 abi_ulong sp;
4055 /* Default to using normal stack */
4056 sp = env->regs[15];
4058 /* This is the X/Open sanctioned signal stack switching. */
4059 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4060 if (!sas_ss_flags(sp)) {
4061 sp = target_sigaltstack_used.ss_sp +
4062 target_sigaltstack_used.ss_size;
4066 /* This is the legacy signal stack switching. */
4067 else if (/* FIXME !user_mode(regs) */ 0 &&
4068 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4069 ka->sa_restorer) {
4070 sp = (abi_ulong) ka->sa_restorer;
4073 return (sp - frame_size) & -8ul;
4076 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4078 int i;
4079 //save_access_regs(current->thread.acrs); FIXME
4081 /* Copy a 'clean' PSW mask to the user to avoid leaking
4082 information about whether PER is currently on. */
4083 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4084 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4085 for (i = 0; i < 16; i++) {
4086 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4088 for (i = 0; i < 16; i++) {
4089 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4092 * We have to store the fp registers to current->thread.fp_regs
4093 * to merge them with the emulated registers.
4095 //save_fp_regs(&current->thread.fp_regs); FIXME
4096 for (i = 0; i < 16; i++) {
4097 __put_user(get_freg(env, i)->ll, &sregs->fpregs.fprs[i]);
4101 static void setup_frame(int sig, struct target_sigaction *ka,
4102 target_sigset_t *set, CPUS390XState *env)
4104 sigframe *frame;
4105 abi_ulong frame_addr;
4107 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4108 trace_user_setup_frame(env, frame_addr);
4109 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4110 goto give_sigsegv;
4113 __put_user(set->sig[0], &frame->sc.oldmask[0]);
4115 save_sigregs(env, &frame->sregs);
4117 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4118 (abi_ulong *)&frame->sc.sregs);
4120 /* Set up to return from userspace. If provided, use a stub
4121 already in userspace. */
4122 if (ka->sa_flags & TARGET_SA_RESTORER) {
4123 env->regs[14] = (unsigned long)
4124 ka->sa_restorer | PSW_ADDR_AMODE;
4125 } else {
4126 env->regs[14] = (unsigned long)
4127 frame->retcode | PSW_ADDR_AMODE;
4128 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4129 (uint16_t *)(frame->retcode));
4132 /* Set up backchain. */
4133 __put_user(env->regs[15], (abi_ulong *) frame);
4135 /* Set up registers for signal handler */
4136 env->regs[15] = frame_addr;
4137 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4139 env->regs[2] = sig; //map_signal(sig);
4140 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4142 /* We forgot to include these in the sigcontext.
4143 To avoid breaking binary compatibility, they are passed as args. */
4144 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4145 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4147 /* Place signal number on stack to allow backtrace from handler. */
4148 __put_user(env->regs[2], (int *) &frame->signo);
4149 unlock_user_struct(frame, frame_addr, 1);
4150 return;
4152 give_sigsegv:
4153 force_sig(TARGET_SIGSEGV);
4156 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4157 target_siginfo_t *info,
4158 target_sigset_t *set, CPUS390XState *env)
4160 int i;
4161 rt_sigframe *frame;
4162 abi_ulong frame_addr;
4164 frame_addr = get_sigframe(ka, env, sizeof *frame);
4165 trace_user_setup_rt_frame(env, frame_addr);
4166 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4167 goto give_sigsegv;
4170 tswap_siginfo(&frame->info, info);
4172 /* Create the ucontext. */
4173 __put_user(0, &frame->uc.tuc_flags);
4174 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4175 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4176 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4177 &frame->uc.tuc_stack.ss_flags);
4178 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4179 save_sigregs(env, &frame->uc.tuc_mcontext);
4180 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4181 __put_user((abi_ulong)set->sig[i],
4182 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4185 /* Set up to return from userspace. If provided, use a stub
4186 already in userspace. */
4187 if (ka->sa_flags & TARGET_SA_RESTORER) {
4188 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4189 } else {
4190 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4191 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4192 (uint16_t *)(frame->retcode));
4195 /* Set up backchain. */
4196 __put_user(env->regs[15], (abi_ulong *) frame);
4198 /* Set up registers for signal handler */
4199 env->regs[15] = frame_addr;
4200 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4202 env->regs[2] = sig; //map_signal(sig);
4203 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4204 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4205 return;
4207 give_sigsegv:
4208 force_sig(TARGET_SIGSEGV);
4211 static int
4212 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4214 int err = 0;
4215 int i;
4217 for (i = 0; i < 16; i++) {
4218 __get_user(env->regs[i], &sc->regs.gprs[i]);
4221 __get_user(env->psw.mask, &sc->regs.psw.mask);
4222 trace_user_s390x_restore_sigregs(env, (unsigned long long)sc->regs.psw.addr,
4223 (unsigned long long)env->psw.addr);
4224 __get_user(env->psw.addr, &sc->regs.psw.addr);
4225 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4227 for (i = 0; i < 16; i++) {
4228 __get_user(env->aregs[i], &sc->regs.acrs[i]);
4230 for (i = 0; i < 16; i++) {
4231 __get_user(get_freg(env, i)->ll, &sc->fpregs.fprs[i]);
4234 return err;
4237 long do_sigreturn(CPUS390XState *env)
4239 sigframe *frame;
4240 abi_ulong frame_addr = env->regs[15];
4241 target_sigset_t target_set;
4242 sigset_t set;
4244 trace_user_do_sigreturn(env, frame_addr);
4245 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4246 goto badframe;
4248 __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
4250 target_to_host_sigset_internal(&set, &target_set);
4251 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4253 if (restore_sigregs(env, &frame->sregs)) {
4254 goto badframe;
4257 unlock_user_struct(frame, frame_addr, 0);
4258 return env->regs[2];
4260 badframe:
4261 force_sig(TARGET_SIGSEGV);
4262 return 0;
4265 long do_rt_sigreturn(CPUS390XState *env)
4267 rt_sigframe *frame;
4268 abi_ulong frame_addr = env->regs[15];
4269 sigset_t set;
4271 trace_user_do_rt_sigreturn(env, frame_addr);
4272 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4273 goto badframe;
4275 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4277 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4279 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4280 goto badframe;
4283 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4284 get_sp_from_cpustate(env)) == -EFAULT) {
4285 goto badframe;
4287 unlock_user_struct(frame, frame_addr, 0);
4288 return env->regs[2];
4290 badframe:
4291 unlock_user_struct(frame, frame_addr, 0);
4292 force_sig(TARGET_SIGSEGV);
4293 return 0;
4296 #elif defined(TARGET_PPC)
4298 /* Size of dummy stack frame allocated when calling signal handler.
4299 See arch/powerpc/include/asm/ptrace.h. */
4300 #if defined(TARGET_PPC64)
4301 #define SIGNAL_FRAMESIZE 128
4302 #else
4303 #define SIGNAL_FRAMESIZE 64
4304 #endif
4306 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4307 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4308 struct target_mcontext {
4309 target_ulong mc_gregs[48];
4310 /* Includes fpscr. */
4311 uint64_t mc_fregs[33];
4312 target_ulong mc_pad[2];
4313 /* We need to handle Altivec and SPE at the same time, which no
4314 kernel needs to do. Fortunately, the kernel defines this bit to
4315 be Altivec-register-large all the time, rather than trying to
4316 twiddle it based on the specific platform. */
4317 union {
4318 /* SPE vector registers. One extra for SPEFSCR. */
4319 uint32_t spe[33];
4320 /* Altivec vector registers. The packing of VSCR and VRSAVE
4321 varies depending on whether we're PPC64 or not: PPC64 splits
4322 them apart; PPC32 stuffs them together. */
4323 #if defined(TARGET_PPC64)
4324 #define QEMU_NVRREG 34
4325 #else
4326 #define QEMU_NVRREG 33
4327 #endif
4328 ppc_avr_t altivec[QEMU_NVRREG];
4329 #undef QEMU_NVRREG
4330 } mc_vregs __attribute__((__aligned__(16)));
4333 /* See arch/powerpc/include/asm/sigcontext.h. */
4334 struct target_sigcontext {
4335 target_ulong _unused[4];
4336 int32_t signal;
4337 #if defined(TARGET_PPC64)
4338 int32_t pad0;
4339 #endif
4340 target_ulong handler;
4341 target_ulong oldmask;
4342 target_ulong regs; /* struct pt_regs __user * */
4343 #if defined(TARGET_PPC64)
4344 struct target_mcontext mcontext;
4345 #endif
4348 /* Indices for target_mcontext.mc_gregs, below.
4349 See arch/powerpc/include/asm/ptrace.h for details. */
4350 enum {
4351 TARGET_PT_R0 = 0,
4352 TARGET_PT_R1 = 1,
4353 TARGET_PT_R2 = 2,
4354 TARGET_PT_R3 = 3,
4355 TARGET_PT_R4 = 4,
4356 TARGET_PT_R5 = 5,
4357 TARGET_PT_R6 = 6,
4358 TARGET_PT_R7 = 7,
4359 TARGET_PT_R8 = 8,
4360 TARGET_PT_R9 = 9,
4361 TARGET_PT_R10 = 10,
4362 TARGET_PT_R11 = 11,
4363 TARGET_PT_R12 = 12,
4364 TARGET_PT_R13 = 13,
4365 TARGET_PT_R14 = 14,
4366 TARGET_PT_R15 = 15,
4367 TARGET_PT_R16 = 16,
4368 TARGET_PT_R17 = 17,
4369 TARGET_PT_R18 = 18,
4370 TARGET_PT_R19 = 19,
4371 TARGET_PT_R20 = 20,
4372 TARGET_PT_R21 = 21,
4373 TARGET_PT_R22 = 22,
4374 TARGET_PT_R23 = 23,
4375 TARGET_PT_R24 = 24,
4376 TARGET_PT_R25 = 25,
4377 TARGET_PT_R26 = 26,
4378 TARGET_PT_R27 = 27,
4379 TARGET_PT_R28 = 28,
4380 TARGET_PT_R29 = 29,
4381 TARGET_PT_R30 = 30,
4382 TARGET_PT_R31 = 31,
4383 TARGET_PT_NIP = 32,
4384 TARGET_PT_MSR = 33,
4385 TARGET_PT_ORIG_R3 = 34,
4386 TARGET_PT_CTR = 35,
4387 TARGET_PT_LNK = 36,
4388 TARGET_PT_XER = 37,
4389 TARGET_PT_CCR = 38,
4390 /* Yes, there are two registers with #39. One is 64-bit only. */
4391 TARGET_PT_MQ = 39,
4392 TARGET_PT_SOFTE = 39,
4393 TARGET_PT_TRAP = 40,
4394 TARGET_PT_DAR = 41,
4395 TARGET_PT_DSISR = 42,
4396 TARGET_PT_RESULT = 43,
4397 TARGET_PT_REGS_COUNT = 44
4401 struct target_ucontext {
4402 target_ulong tuc_flags;
4403 target_ulong tuc_link; /* struct ucontext __user * */
4404 struct target_sigaltstack tuc_stack;
4405 #if !defined(TARGET_PPC64)
4406 int32_t tuc_pad[7];
4407 target_ulong tuc_regs; /* struct mcontext __user *
4408 points to uc_mcontext field */
4409 #endif
4410 target_sigset_t tuc_sigmask;
4411 #if defined(TARGET_PPC64)
4412 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4413 struct target_sigcontext tuc_sigcontext;
4414 #else
4415 int32_t tuc_maskext[30];
4416 int32_t tuc_pad2[3];
4417 struct target_mcontext tuc_mcontext;
4418 #endif
4421 /* See arch/powerpc/kernel/signal_32.c. */
4422 struct target_sigframe {
4423 struct target_sigcontext sctx;
4424 struct target_mcontext mctx;
4425 int32_t abigap[56];
4428 #if defined(TARGET_PPC64)
4430 #define TARGET_TRAMP_SIZE 6
4432 struct target_rt_sigframe {
4433 /* sys_rt_sigreturn requires the ucontext be the first field */
4434 struct target_ucontext uc;
4435 target_ulong _unused[2];
4436 uint32_t trampoline[TARGET_TRAMP_SIZE];
4437 target_ulong pinfo; /* struct siginfo __user * */
4438 target_ulong puc; /* void __user * */
4439 struct target_siginfo info;
4440 /* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
4441 char abigap[288];
4442 } __attribute__((aligned(16)));
4444 #else
4446 struct target_rt_sigframe {
4447 struct target_siginfo info;
4448 struct target_ucontext uc;
4449 int32_t abigap[56];
4452 #endif
4454 #if defined(TARGET_PPC64)
4456 struct target_func_ptr {
4457 target_ulong entry;
4458 target_ulong toc;
4461 #endif
4463 /* We use the mc_pad field for the signal return trampoline. */
4464 #define tramp mc_pad
4466 /* See arch/powerpc/kernel/signal.c. */
4467 static target_ulong get_sigframe(struct target_sigaction *ka,
4468 CPUPPCState *env,
4469 int frame_size)
4471 target_ulong oldsp, newsp;
4473 oldsp = env->gpr[1];
4475 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4476 (sas_ss_flags(oldsp) == 0)) {
4477 oldsp = (target_sigaltstack_used.ss_sp
4478 + target_sigaltstack_used.ss_size);
4481 newsp = (oldsp - frame_size) & ~0xFUL;
4483 return newsp;
4486 static void save_user_regs(CPUPPCState *env, struct target_mcontext *frame)
4488 target_ulong msr = env->msr;
4489 int i;
4490 target_ulong ccr = 0;
4492 /* In general, the kernel attempts to be intelligent about what it
4493 needs to save for Altivec/FP/SPE registers. We don't care that
4494 much, so we just go ahead and save everything. */
4496 /* Save general registers. */
4497 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4498 __put_user(env->gpr[i], &frame->mc_gregs[i]);
4500 __put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4501 __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4502 __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4503 __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4505 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4506 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4508 __put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4510 /* Save Altivec registers if necessary. */
4511 if (env->insns_flags & PPC_ALTIVEC) {
4512 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4513 ppc_avr_t *avr = &env->avr[i];
4514 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4516 __put_user(avr->u64[0], &vreg->u64[0]);
4517 __put_user(avr->u64[1], &vreg->u64[1]);
4519 /* Set MSR_VR in the saved MSR value to indicate that
4520 frame->mc_vregs contains valid data. */
4521 msr |= MSR_VR;
4522 __put_user((uint32_t)env->spr[SPR_VRSAVE],
4523 &frame->mc_vregs.altivec[32].u32[3]);
4526 /* Save floating point registers. */
4527 if (env->insns_flags & PPC_FLOAT) {
4528 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4529 __put_user(env->fpr[i], &frame->mc_fregs[i]);
4531 __put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]);
4534 /* Save SPE registers. The kernel only saves the high half. */
4535 if (env->insns_flags & PPC_SPE) {
4536 #if defined(TARGET_PPC64)
4537 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4538 __put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i]);
4540 #else
4541 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4542 __put_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4544 #endif
4545 /* Set MSR_SPE in the saved MSR value to indicate that
4546 frame->mc_vregs contains valid data. */
4547 msr |= MSR_SPE;
4548 __put_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4551 /* Store MSR. */
4552 __put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4555 static void encode_trampoline(int sigret, uint32_t *tramp)
4557 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4558 if (sigret) {
4559 __put_user(0x38000000 | sigret, &tramp[0]);
4560 __put_user(0x44000002, &tramp[1]);
4564 static void restore_user_regs(CPUPPCState *env,
4565 struct target_mcontext *frame, int sig)
4567 target_ulong save_r2 = 0;
4568 target_ulong msr;
4569 target_ulong ccr;
4571 int i;
4573 if (!sig) {
4574 save_r2 = env->gpr[2];
4577 /* Restore general registers. */
4578 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4579 __get_user(env->gpr[i], &frame->mc_gregs[i]);
4581 __get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4582 __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4583 __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4584 __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4585 __get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4587 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4588 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4591 if (!sig) {
4592 env->gpr[2] = save_r2;
4594 /* Restore MSR. */
4595 __get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4597 /* If doing signal return, restore the previous little-endian mode. */
4598 if (sig)
4599 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4601 /* Restore Altivec registers if necessary. */
4602 if (env->insns_flags & PPC_ALTIVEC) {
4603 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4604 ppc_avr_t *avr = &env->avr[i];
4605 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4607 __get_user(avr->u64[0], &vreg->u64[0]);
4608 __get_user(avr->u64[1], &vreg->u64[1]);
4610 /* Set MSR_VEC in the saved MSR value to indicate that
4611 frame->mc_vregs contains valid data. */
4612 __get_user(env->spr[SPR_VRSAVE],
4613 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3]));
4616 /* Restore floating point registers. */
4617 if (env->insns_flags & PPC_FLOAT) {
4618 uint64_t fpscr;
4619 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4620 __get_user(env->fpr[i], &frame->mc_fregs[i]);
4622 __get_user(fpscr, &frame->mc_fregs[32]);
4623 env->fpscr = (uint32_t) fpscr;
4626 /* Save SPE registers. The kernel only saves the high half. */
4627 if (env->insns_flags & PPC_SPE) {
4628 #if defined(TARGET_PPC64)
4629 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4630 uint32_t hi;
4632 __get_user(hi, &frame->mc_vregs.spe[i]);
4633 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4635 #else
4636 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4637 __get_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4639 #endif
4640 __get_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4644 static void setup_frame(int sig, struct target_sigaction *ka,
4645 target_sigset_t *set, CPUPPCState *env)
4647 struct target_sigframe *frame;
4648 struct target_sigcontext *sc;
4649 target_ulong frame_addr, newsp;
4650 int err = 0;
4651 #if defined(TARGET_PPC64)
4652 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4653 #endif
4655 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4656 trace_user_setup_frame(env, frame_addr);
4657 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4658 goto sigsegv;
4659 sc = &frame->sctx;
4661 __put_user(ka->_sa_handler, &sc->handler);
4662 __put_user(set->sig[0], &sc->oldmask);
4663 #if TARGET_ABI_BITS == 64
4664 __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4665 #else
4666 __put_user(set->sig[1], &sc->_unused[3]);
4667 #endif
4668 __put_user(h2g(&frame->mctx), &sc->regs);
4669 __put_user(sig, &sc->signal);
4671 /* Save user regs. */
4672 save_user_regs(env, &frame->mctx);
4674 /* Construct the trampoline code on the stack. */
4675 encode_trampoline(TARGET_NR_sigreturn, (uint32_t *)&frame->mctx.tramp);
4677 /* The kernel checks for the presence of a VDSO here. We don't
4678 emulate a vdso, so use a sigreturn system call. */
4679 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4681 /* Turn off all fp exceptions. */
4682 env->fpscr = 0;
4684 /* Create a stack frame for the caller of the handler. */
4685 newsp = frame_addr - SIGNAL_FRAMESIZE;
4686 err |= put_user(env->gpr[1], newsp, target_ulong);
4688 if (err)
4689 goto sigsegv;
4691 /* Set up registers for signal handler. */
4692 env->gpr[1] = newsp;
4693 env->gpr[3] = sig;
4694 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4696 #if defined(TARGET_PPC64)
4697 if (get_ppc64_abi(image) < 2) {
4698 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4699 struct target_func_ptr *handler =
4700 (struct target_func_ptr *)g2h(ka->_sa_handler);
4701 env->nip = tswapl(handler->entry);
4702 env->gpr[2] = tswapl(handler->toc);
4703 } else {
4704 /* ELFv2 PPC64 function pointers are entry points, but R12
4705 * must also be set */
4706 env->nip = tswapl((target_ulong) ka->_sa_handler);
4707 env->gpr[12] = env->nip;
4709 #else
4710 env->nip = (target_ulong) ka->_sa_handler;
4711 #endif
4713 /* Signal handlers are entered in big-endian mode. */
4714 env->msr &= ~MSR_LE;
4716 unlock_user_struct(frame, frame_addr, 1);
4717 return;
4719 sigsegv:
4720 unlock_user_struct(frame, frame_addr, 1);
4721 force_sig(TARGET_SIGSEGV);
4724 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4725 target_siginfo_t *info,
4726 target_sigset_t *set, CPUPPCState *env)
4728 struct target_rt_sigframe *rt_sf;
4729 uint32_t *trampptr = 0;
4730 struct target_mcontext *mctx = 0;
4731 target_ulong rt_sf_addr, newsp = 0;
4732 int i, err = 0;
4733 #if defined(TARGET_PPC64)
4734 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4735 #endif
4737 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4738 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4739 goto sigsegv;
4741 tswap_siginfo(&rt_sf->info, info);
4743 __put_user(0, &rt_sf->uc.tuc_flags);
4744 __put_user(0, &rt_sf->uc.tuc_link);
4745 __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4746 &rt_sf->uc.tuc_stack.ss_sp);
4747 __put_user(sas_ss_flags(env->gpr[1]),
4748 &rt_sf->uc.tuc_stack.ss_flags);
4749 __put_user(target_sigaltstack_used.ss_size,
4750 &rt_sf->uc.tuc_stack.ss_size);
4751 #if !defined(TARGET_PPC64)
4752 __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4753 &rt_sf->uc.tuc_regs);
4754 #endif
4755 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4756 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4759 #if defined(TARGET_PPC64)
4760 mctx = &rt_sf->uc.tuc_sigcontext.mcontext;
4761 trampptr = &rt_sf->trampoline[0];
4762 #else
4763 mctx = &rt_sf->uc.tuc_mcontext;
4764 trampptr = (uint32_t *)&rt_sf->uc.tuc_mcontext.tramp;
4765 #endif
4767 save_user_regs(env, mctx);
4768 encode_trampoline(TARGET_NR_rt_sigreturn, trampptr);
4770 /* The kernel checks for the presence of a VDSO here. We don't
4771 emulate a vdso, so use a sigreturn system call. */
4772 env->lr = (target_ulong) h2g(trampptr);
4774 /* Turn off all fp exceptions. */
4775 env->fpscr = 0;
4777 /* Create a stack frame for the caller of the handler. */
4778 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4779 err |= put_user(env->gpr[1], newsp, target_ulong);
4781 if (err)
4782 goto sigsegv;
4784 /* Set up registers for signal handler. */
4785 env->gpr[1] = newsp;
4786 env->gpr[3] = (target_ulong) sig;
4787 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4788 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4789 env->gpr[6] = (target_ulong) h2g(rt_sf);
4791 #if defined(TARGET_PPC64)
4792 if (get_ppc64_abi(image) < 2) {
4793 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4794 struct target_func_ptr *handler =
4795 (struct target_func_ptr *)g2h(ka->_sa_handler);
4796 env->nip = tswapl(handler->entry);
4797 env->gpr[2] = tswapl(handler->toc);
4798 } else {
4799 /* ELFv2 PPC64 function pointers are entry points, but R12
4800 * must also be set */
4801 env->nip = tswapl((target_ulong) ka->_sa_handler);
4802 env->gpr[12] = env->nip;
4804 #else
4805 env->nip = (target_ulong) ka->_sa_handler;
4806 #endif
4808 /* Signal handlers are entered in big-endian mode. */
4809 env->msr &= ~MSR_LE;
4811 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4812 return;
4814 sigsegv:
4815 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4816 force_sig(TARGET_SIGSEGV);
4820 long do_sigreturn(CPUPPCState *env)
4822 struct target_sigcontext *sc = NULL;
4823 struct target_mcontext *sr = NULL;
4824 target_ulong sr_addr = 0, sc_addr;
4825 sigset_t blocked;
4826 target_sigset_t set;
4828 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4829 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4830 goto sigsegv;
4832 #if defined(TARGET_PPC64)
4833 set.sig[0] = sc->oldmask + ((uint64_t)(sc->_unused[3]) << 32);
4834 #else
4835 __get_user(set.sig[0], &sc->oldmask);
4836 __get_user(set.sig[1], &sc->_unused[3]);
4837 #endif
4838 target_to_host_sigset_internal(&blocked, &set);
4839 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4841 __get_user(sr_addr, &sc->regs);
4842 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4843 goto sigsegv;
4844 restore_user_regs(env, sr, 1);
4846 unlock_user_struct(sr, sr_addr, 1);
4847 unlock_user_struct(sc, sc_addr, 1);
4848 return -TARGET_QEMU_ESIGRETURN;
4850 sigsegv:
4851 unlock_user_struct(sr, sr_addr, 1);
4852 unlock_user_struct(sc, sc_addr, 1);
4853 force_sig(TARGET_SIGSEGV);
4854 return 0;
4857 /* See arch/powerpc/kernel/signal_32.c. */
4858 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4860 struct target_mcontext *mcp;
4861 target_ulong mcp_addr;
4862 sigset_t blocked;
4863 target_sigset_t set;
4865 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4866 sizeof (set)))
4867 return 1;
4869 #if defined(TARGET_PPC64)
4870 mcp_addr = h2g(ucp) +
4871 offsetof(struct target_ucontext, tuc_sigcontext.mcontext);
4872 #else
4873 __get_user(mcp_addr, &ucp->tuc_regs);
4874 #endif
4876 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4877 return 1;
4879 target_to_host_sigset_internal(&blocked, &set);
4880 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
4881 restore_user_regs(env, mcp, sig);
4883 unlock_user_struct(mcp, mcp_addr, 1);
4884 return 0;
4887 long do_rt_sigreturn(CPUPPCState *env)
4889 struct target_rt_sigframe *rt_sf = NULL;
4890 target_ulong rt_sf_addr;
4892 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4893 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4894 goto sigsegv;
4896 if (do_setcontext(&rt_sf->uc, env, 1))
4897 goto sigsegv;
4899 do_sigaltstack(rt_sf_addr
4900 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4901 0, env->gpr[1]);
4903 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4904 return -TARGET_QEMU_ESIGRETURN;
4906 sigsegv:
4907 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4908 force_sig(TARGET_SIGSEGV);
4909 return 0;
4912 #elif defined(TARGET_M68K)
4914 struct target_sigcontext {
4915 abi_ulong sc_mask;
4916 abi_ulong sc_usp;
4917 abi_ulong sc_d0;
4918 abi_ulong sc_d1;
4919 abi_ulong sc_a0;
4920 abi_ulong sc_a1;
4921 unsigned short sc_sr;
4922 abi_ulong sc_pc;
4925 struct target_sigframe
4927 abi_ulong pretcode;
4928 int sig;
4929 int code;
4930 abi_ulong psc;
4931 char retcode[8];
4932 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4933 struct target_sigcontext sc;
4936 typedef int target_greg_t;
4937 #define TARGET_NGREG 18
4938 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4940 typedef struct target_fpregset {
4941 int f_fpcntl[3];
4942 int f_fpregs[8*3];
4943 } target_fpregset_t;
4945 struct target_mcontext {
4946 int version;
4947 target_gregset_t gregs;
4948 target_fpregset_t fpregs;
4951 #define TARGET_MCONTEXT_VERSION 2
4953 struct target_ucontext {
4954 abi_ulong tuc_flags;
4955 abi_ulong tuc_link;
4956 target_stack_t tuc_stack;
4957 struct target_mcontext tuc_mcontext;
4958 abi_long tuc_filler[80];
4959 target_sigset_t tuc_sigmask;
4962 struct target_rt_sigframe
4964 abi_ulong pretcode;
4965 int sig;
4966 abi_ulong pinfo;
4967 abi_ulong puc;
4968 char retcode[8];
4969 struct target_siginfo info;
4970 struct target_ucontext uc;
4973 static void setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4974 abi_ulong mask)
4976 __put_user(mask, &sc->sc_mask);
4977 __put_user(env->aregs[7], &sc->sc_usp);
4978 __put_user(env->dregs[0], &sc->sc_d0);
4979 __put_user(env->dregs[1], &sc->sc_d1);
4980 __put_user(env->aregs[0], &sc->sc_a0);
4981 __put_user(env->aregs[1], &sc->sc_a1);
4982 __put_user(env->sr, &sc->sc_sr);
4983 __put_user(env->pc, &sc->sc_pc);
4986 static void
4987 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
4989 int temp;
4991 __get_user(env->aregs[7], &sc->sc_usp);
4992 __get_user(env->dregs[1], &sc->sc_d1);
4993 __get_user(env->aregs[0], &sc->sc_a0);
4994 __get_user(env->aregs[1], &sc->sc_a1);
4995 __get_user(env->pc, &sc->sc_pc);
4996 __get_user(temp, &sc->sc_sr);
4997 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4999 *pd0 = tswapl(sc->sc_d0);
5003 * Determine which stack to use..
5005 static inline abi_ulong
5006 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5007 size_t frame_size)
5009 unsigned long sp;
5011 sp = regs->aregs[7];
5013 /* This is the X/Open sanctioned signal stack switching. */
5014 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5015 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5018 return ((sp - frame_size) & -8UL);
5021 static void setup_frame(int sig, struct target_sigaction *ka,
5022 target_sigset_t *set, CPUM68KState *env)
5024 struct target_sigframe *frame;
5025 abi_ulong frame_addr;
5026 abi_ulong retcode_addr;
5027 abi_ulong sc_addr;
5028 int i;
5030 frame_addr = get_sigframe(ka, env, sizeof *frame);
5031 trace_user_setup_frame(env, frame_addr);
5032 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5033 goto give_sigsegv;
5035 __put_user(sig, &frame->sig);
5037 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5038 __put_user(sc_addr, &frame->psc);
5040 setup_sigcontext(&frame->sc, env, set->sig[0]);
5042 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5043 __put_user(set->sig[i], &frame->extramask[i - 1]);
5046 /* Set up to return from userspace. */
5048 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5049 __put_user(retcode_addr, &frame->pretcode);
5051 /* moveq #,d0; trap #0 */
5053 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5054 (uint32_t *)(frame->retcode));
5056 /* Set up to return from userspace */
5058 env->aregs[7] = frame_addr;
5059 env->pc = ka->_sa_handler;
5061 unlock_user_struct(frame, frame_addr, 1);
5062 return;
5064 give_sigsegv:
5065 force_sig(TARGET_SIGSEGV);
5068 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5069 CPUM68KState *env)
5071 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5073 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5074 __put_user(env->dregs[0], &gregs[0]);
5075 __put_user(env->dregs[1], &gregs[1]);
5076 __put_user(env->dregs[2], &gregs[2]);
5077 __put_user(env->dregs[3], &gregs[3]);
5078 __put_user(env->dregs[4], &gregs[4]);
5079 __put_user(env->dregs[5], &gregs[5]);
5080 __put_user(env->dregs[6], &gregs[6]);
5081 __put_user(env->dregs[7], &gregs[7]);
5082 __put_user(env->aregs[0], &gregs[8]);
5083 __put_user(env->aregs[1], &gregs[9]);
5084 __put_user(env->aregs[2], &gregs[10]);
5085 __put_user(env->aregs[3], &gregs[11]);
5086 __put_user(env->aregs[4], &gregs[12]);
5087 __put_user(env->aregs[5], &gregs[13]);
5088 __put_user(env->aregs[6], &gregs[14]);
5089 __put_user(env->aregs[7], &gregs[15]);
5090 __put_user(env->pc, &gregs[16]);
5091 __put_user(env->sr, &gregs[17]);
5093 return 0;
5096 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5097 struct target_ucontext *uc,
5098 int *pd0)
5100 int temp;
5101 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5103 __get_user(temp, &uc->tuc_mcontext.version);
5104 if (temp != TARGET_MCONTEXT_VERSION)
5105 goto badframe;
5107 /* restore passed registers */
5108 __get_user(env->dregs[0], &gregs[0]);
5109 __get_user(env->dregs[1], &gregs[1]);
5110 __get_user(env->dregs[2], &gregs[2]);
5111 __get_user(env->dregs[3], &gregs[3]);
5112 __get_user(env->dregs[4], &gregs[4]);
5113 __get_user(env->dregs[5], &gregs[5]);
5114 __get_user(env->dregs[6], &gregs[6]);
5115 __get_user(env->dregs[7], &gregs[7]);
5116 __get_user(env->aregs[0], &gregs[8]);
5117 __get_user(env->aregs[1], &gregs[9]);
5118 __get_user(env->aregs[2], &gregs[10]);
5119 __get_user(env->aregs[3], &gregs[11]);
5120 __get_user(env->aregs[4], &gregs[12]);
5121 __get_user(env->aregs[5], &gregs[13]);
5122 __get_user(env->aregs[6], &gregs[14]);
5123 __get_user(env->aregs[7], &gregs[15]);
5124 __get_user(env->pc, &gregs[16]);
5125 __get_user(temp, &gregs[17]);
5126 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5128 *pd0 = env->dregs[0];
5129 return 0;
5131 badframe:
5132 return 1;
5135 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5136 target_siginfo_t *info,
5137 target_sigset_t *set, CPUM68KState *env)
5139 struct target_rt_sigframe *frame;
5140 abi_ulong frame_addr;
5141 abi_ulong retcode_addr;
5142 abi_ulong info_addr;
5143 abi_ulong uc_addr;
5144 int err = 0;
5145 int i;
5147 frame_addr = get_sigframe(ka, env, sizeof *frame);
5148 trace_user_setup_rt_frame(env, frame_addr);
5149 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5150 goto give_sigsegv;
5152 __put_user(sig, &frame->sig);
5154 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5155 __put_user(info_addr, &frame->pinfo);
5157 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5158 __put_user(uc_addr, &frame->puc);
5160 tswap_siginfo(&frame->info, info);
5162 /* Create the ucontext */
5164 __put_user(0, &frame->uc.tuc_flags);
5165 __put_user(0, &frame->uc.tuc_link);
5166 __put_user(target_sigaltstack_used.ss_sp,
5167 &frame->uc.tuc_stack.ss_sp);
5168 __put_user(sas_ss_flags(env->aregs[7]),
5169 &frame->uc.tuc_stack.ss_flags);
5170 __put_user(target_sigaltstack_used.ss_size,
5171 &frame->uc.tuc_stack.ss_size);
5172 err |= target_rt_setup_ucontext(&frame->uc, env);
5174 if (err)
5175 goto give_sigsegv;
5177 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5178 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5181 /* Set up to return from userspace. */
5183 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5184 __put_user(retcode_addr, &frame->pretcode);
5186 /* moveq #,d0; notb d0; trap #0 */
5188 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5189 (uint32_t *)(frame->retcode + 0));
5190 __put_user(0x4e40, (uint16_t *)(frame->retcode + 4));
5192 if (err)
5193 goto give_sigsegv;
5195 /* Set up to return from userspace */
5197 env->aregs[7] = frame_addr;
5198 env->pc = ka->_sa_handler;
5200 unlock_user_struct(frame, frame_addr, 1);
5201 return;
5203 give_sigsegv:
5204 unlock_user_struct(frame, frame_addr, 1);
5205 force_sig(TARGET_SIGSEGV);
5208 long do_sigreturn(CPUM68KState *env)
5210 struct target_sigframe *frame;
5211 abi_ulong frame_addr = env->aregs[7] - 4;
5212 target_sigset_t target_set;
5213 sigset_t set;
5214 int d0, i;
5216 trace_user_do_sigreturn(env, frame_addr);
5217 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5218 goto badframe;
5220 /* set blocked signals */
5222 __get_user(target_set.sig[0], &frame->sc.sc_mask);
5224 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5225 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
5228 target_to_host_sigset_internal(&set, &target_set);
5229 do_sigprocmask(SIG_SETMASK, &set, NULL);
5231 /* restore registers */
5233 restore_sigcontext(env, &frame->sc, &d0);
5235 unlock_user_struct(frame, frame_addr, 0);
5236 return d0;
5238 badframe:
5239 force_sig(TARGET_SIGSEGV);
5240 return 0;
5243 long do_rt_sigreturn(CPUM68KState *env)
5245 struct target_rt_sigframe *frame;
5246 abi_ulong frame_addr = env->aregs[7] - 4;
5247 target_sigset_t target_set;
5248 sigset_t set;
5249 int d0;
5251 trace_user_do_rt_sigreturn(env, frame_addr);
5252 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5253 goto badframe;
5255 target_to_host_sigset_internal(&set, &target_set);
5256 do_sigprocmask(SIG_SETMASK, &set, NULL);
5258 /* restore registers */
5260 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5261 goto badframe;
5263 if (do_sigaltstack(frame_addr +
5264 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5265 0, get_sp_from_cpustate(env)) == -EFAULT)
5266 goto badframe;
5268 unlock_user_struct(frame, frame_addr, 0);
5269 return d0;
5271 badframe:
5272 unlock_user_struct(frame, frame_addr, 0);
5273 force_sig(TARGET_SIGSEGV);
5274 return 0;
5277 #elif defined(TARGET_ALPHA)
5279 struct target_sigcontext {
5280 abi_long sc_onstack;
5281 abi_long sc_mask;
5282 abi_long sc_pc;
5283 abi_long sc_ps;
5284 abi_long sc_regs[32];
5285 abi_long sc_ownedfp;
5286 abi_long sc_fpregs[32];
5287 abi_ulong sc_fpcr;
5288 abi_ulong sc_fp_control;
5289 abi_ulong sc_reserved1;
5290 abi_ulong sc_reserved2;
5291 abi_ulong sc_ssize;
5292 abi_ulong sc_sbase;
5293 abi_ulong sc_traparg_a0;
5294 abi_ulong sc_traparg_a1;
5295 abi_ulong sc_traparg_a2;
5296 abi_ulong sc_fp_trap_pc;
5297 abi_ulong sc_fp_trigger_sum;
5298 abi_ulong sc_fp_trigger_inst;
5301 struct target_ucontext {
5302 abi_ulong tuc_flags;
5303 abi_ulong tuc_link;
5304 abi_ulong tuc_osf_sigmask;
5305 target_stack_t tuc_stack;
5306 struct target_sigcontext tuc_mcontext;
5307 target_sigset_t tuc_sigmask;
5310 struct target_sigframe {
5311 struct target_sigcontext sc;
5312 unsigned int retcode[3];
5315 struct target_rt_sigframe {
5316 target_siginfo_t info;
5317 struct target_ucontext uc;
5318 unsigned int retcode[3];
5321 #define INSN_MOV_R30_R16 0x47fe0410
5322 #define INSN_LDI_R0 0x201f0000
5323 #define INSN_CALLSYS 0x00000083
5325 static void setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5326 abi_ulong frame_addr, target_sigset_t *set)
5328 int i;
5330 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5331 __put_user(set->sig[0], &sc->sc_mask);
5332 __put_user(env->pc, &sc->sc_pc);
5333 __put_user(8, &sc->sc_ps);
5335 for (i = 0; i < 31; ++i) {
5336 __put_user(env->ir[i], &sc->sc_regs[i]);
5338 __put_user(0, &sc->sc_regs[31]);
5340 for (i = 0; i < 31; ++i) {
5341 __put_user(env->fir[i], &sc->sc_fpregs[i]);
5343 __put_user(0, &sc->sc_fpregs[31]);
5344 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5346 __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5347 __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5348 __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5351 static void restore_sigcontext(CPUAlphaState *env,
5352 struct target_sigcontext *sc)
5354 uint64_t fpcr;
5355 int i;
5357 __get_user(env->pc, &sc->sc_pc);
5359 for (i = 0; i < 31; ++i) {
5360 __get_user(env->ir[i], &sc->sc_regs[i]);
5362 for (i = 0; i < 31; ++i) {
5363 __get_user(env->fir[i], &sc->sc_fpregs[i]);
5366 __get_user(fpcr, &sc->sc_fpcr);
5367 cpu_alpha_store_fpcr(env, fpcr);
5370 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5371 CPUAlphaState *env,
5372 unsigned long framesize)
5374 abi_ulong sp = env->ir[IR_SP];
5376 /* This is the X/Open sanctioned signal stack switching. */
5377 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5378 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5380 return (sp - framesize) & -32;
5383 static void setup_frame(int sig, struct target_sigaction *ka,
5384 target_sigset_t *set, CPUAlphaState *env)
5386 abi_ulong frame_addr, r26;
5387 struct target_sigframe *frame;
5388 int err = 0;
5390 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5391 trace_user_setup_frame(env, frame_addr);
5392 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5393 goto give_sigsegv;
5396 setup_sigcontext(&frame->sc, env, frame_addr, set);
5398 if (ka->sa_restorer) {
5399 r26 = ka->sa_restorer;
5400 } else {
5401 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5402 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5403 &frame->retcode[1]);
5404 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5405 /* imb() */
5406 r26 = frame_addr;
5409 unlock_user_struct(frame, frame_addr, 1);
5411 if (err) {
5412 give_sigsegv:
5413 if (sig == TARGET_SIGSEGV) {
5414 ka->_sa_handler = TARGET_SIG_DFL;
5416 force_sig(TARGET_SIGSEGV);
5419 env->ir[IR_RA] = r26;
5420 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5421 env->ir[IR_A0] = sig;
5422 env->ir[IR_A1] = 0;
5423 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5424 env->ir[IR_SP] = frame_addr;
5427 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5428 target_siginfo_t *info,
5429 target_sigset_t *set, CPUAlphaState *env)
5431 abi_ulong frame_addr, r26;
5432 struct target_rt_sigframe *frame;
5433 int i, err = 0;
5435 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5436 trace_user_setup_rt_frame(env, frame_addr);
5437 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5438 goto give_sigsegv;
5441 tswap_siginfo(&frame->info, info);
5443 __put_user(0, &frame->uc.tuc_flags);
5444 __put_user(0, &frame->uc.tuc_link);
5445 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5446 __put_user(target_sigaltstack_used.ss_sp,
5447 &frame->uc.tuc_stack.ss_sp);
5448 __put_user(sas_ss_flags(env->ir[IR_SP]),
5449 &frame->uc.tuc_stack.ss_flags);
5450 __put_user(target_sigaltstack_used.ss_size,
5451 &frame->uc.tuc_stack.ss_size);
5452 setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5453 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5454 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5457 if (ka->sa_restorer) {
5458 r26 = ka->sa_restorer;
5459 } else {
5460 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5461 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5462 &frame->retcode[1]);
5463 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5464 /* imb(); */
5465 r26 = frame_addr;
5468 if (err) {
5469 give_sigsegv:
5470 if (sig == TARGET_SIGSEGV) {
5471 ka->_sa_handler = TARGET_SIG_DFL;
5473 force_sig(TARGET_SIGSEGV);
5476 env->ir[IR_RA] = r26;
5477 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5478 env->ir[IR_A0] = sig;
5479 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5480 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5481 env->ir[IR_SP] = frame_addr;
5484 long do_sigreturn(CPUAlphaState *env)
5486 struct target_sigcontext *sc;
5487 abi_ulong sc_addr = env->ir[IR_A0];
5488 target_sigset_t target_set;
5489 sigset_t set;
5491 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5492 goto badframe;
5495 target_sigemptyset(&target_set);
5496 __get_user(target_set.sig[0], &sc->sc_mask);
5498 target_to_host_sigset_internal(&set, &target_set);
5499 do_sigprocmask(SIG_SETMASK, &set, NULL);
5501 restore_sigcontext(env, sc);
5502 unlock_user_struct(sc, sc_addr, 0);
5503 return env->ir[IR_V0];
5505 badframe:
5506 force_sig(TARGET_SIGSEGV);
5509 long do_rt_sigreturn(CPUAlphaState *env)
5511 abi_ulong frame_addr = env->ir[IR_A0];
5512 struct target_rt_sigframe *frame;
5513 sigset_t set;
5515 trace_user_do_rt_sigreturn(env, frame_addr);
5516 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5517 goto badframe;
5519 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5520 do_sigprocmask(SIG_SETMASK, &set, NULL);
5522 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5523 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5524 uc.tuc_stack),
5525 0, env->ir[IR_SP]) == -EFAULT) {
5526 goto badframe;
5529 unlock_user_struct(frame, frame_addr, 0);
5530 return env->ir[IR_V0];
5533 badframe:
5534 unlock_user_struct(frame, frame_addr, 0);
5535 force_sig(TARGET_SIGSEGV);
5538 #elif defined(TARGET_TILEGX)
5540 struct target_sigcontext {
5541 union {
5542 /* General-purpose registers. */
5543 abi_ulong gregs[56];
5544 struct {
5545 abi_ulong __gregs[53];
5546 abi_ulong tp; /* Aliases gregs[TREG_TP]. */
5547 abi_ulong sp; /* Aliases gregs[TREG_SP]. */
5548 abi_ulong lr; /* Aliases gregs[TREG_LR]. */
5551 abi_ulong pc; /* Program counter. */
5552 abi_ulong ics; /* In Interrupt Critical Section? */
5553 abi_ulong faultnum; /* Fault number. */
5554 abi_ulong pad[5];
5557 struct target_ucontext {
5558 abi_ulong tuc_flags;
5559 abi_ulong tuc_link;
5560 target_stack_t tuc_stack;
5561 struct target_sigcontext tuc_mcontext;
5562 target_sigset_t tuc_sigmask; /* mask last for extensibility */
5565 struct target_rt_sigframe {
5566 unsigned char save_area[16]; /* caller save area */
5567 struct target_siginfo info;
5568 struct target_ucontext uc;
5571 static void setup_sigcontext(struct target_sigcontext *sc,
5572 CPUArchState *env, int signo)
5574 int i;
5576 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5577 __put_user(env->regs[i], &sc->gregs[i]);
5580 __put_user(env->pc, &sc->pc);
5581 __put_user(0, &sc->ics);
5582 __put_user(signo, &sc->faultnum);
5585 static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
5587 int i;
5589 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5590 __get_user(env->regs[i], &sc->gregs[i]);
5593 __get_user(env->pc, &sc->pc);
5596 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
5597 size_t frame_size)
5599 unsigned long sp = env->regs[TILEGX_R_SP];
5601 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
5602 return -1UL;
5605 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
5606 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5609 sp -= frame_size;
5610 sp &= -16UL;
5611 return sp;
5614 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5615 target_siginfo_t *info,
5616 target_sigset_t *set, CPUArchState *env)
5618 abi_ulong frame_addr;
5619 struct target_rt_sigframe *frame;
5620 unsigned long restorer;
5622 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5623 trace_user_setup_rt_frame(env, frame_addr);
5624 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5625 goto give_sigsegv;
5628 /* Always write at least the signal number for the stack backtracer. */
5629 if (ka->sa_flags & TARGET_SA_SIGINFO) {
5630 /* At sigreturn time, restore the callee-save registers too. */
5631 tswap_siginfo(&frame->info, info);
5632 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
5633 } else {
5634 __put_user(info->si_signo, &frame->info.si_signo);
5637 /* Create the ucontext. */
5638 __put_user(0, &frame->uc.tuc_flags);
5639 __put_user(0, &frame->uc.tuc_link);
5640 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
5641 __put_user(sas_ss_flags(env->regs[TILEGX_R_SP]),
5642 &frame->uc.tuc_stack.ss_flags);
5643 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
5644 setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
5646 restorer = (unsigned long) do_rt_sigreturn;
5647 if (ka->sa_flags & TARGET_SA_RESTORER) {
5648 restorer = (unsigned long) ka->sa_restorer;
5650 env->pc = (unsigned long) ka->_sa_handler;
5651 env->regs[TILEGX_R_SP] = (unsigned long) frame;
5652 env->regs[TILEGX_R_LR] = restorer;
5653 env->regs[0] = (unsigned long) sig;
5654 env->regs[1] = (unsigned long) &frame->info;
5655 env->regs[2] = (unsigned long) &frame->uc;
5656 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
5658 unlock_user_struct(frame, frame_addr, 1);
5659 return;
5661 give_sigsegv:
5662 if (sig == TARGET_SIGSEGV) {
5663 ka->_sa_handler = TARGET_SIG_DFL;
5665 force_sig(TARGET_SIGSEGV /* , current */);
5668 long do_rt_sigreturn(CPUTLGState *env)
5670 abi_ulong frame_addr = env->regs[TILEGX_R_SP];
5671 struct target_rt_sigframe *frame;
5672 sigset_t set;
5674 trace_user_do_rt_sigreturn(env, frame_addr);
5675 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5676 goto badframe;
5678 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5679 do_sigprocmask(SIG_SETMASK, &set, NULL);
5681 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5682 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5683 uc.tuc_stack),
5684 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
5685 goto badframe;
5688 unlock_user_struct(frame, frame_addr, 0);
5689 return env->regs[TILEGX_R_RE];
5692 badframe:
5693 unlock_user_struct(frame, frame_addr, 0);
5694 force_sig(TARGET_SIGSEGV);
5697 #else
5699 static void setup_frame(int sig, struct target_sigaction *ka,
5700 target_sigset_t *set, CPUArchState *env)
5702 fprintf(stderr, "setup_frame: not implemented\n");
5705 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5706 target_siginfo_t *info,
5707 target_sigset_t *set, CPUArchState *env)
5709 fprintf(stderr, "setup_rt_frame: not implemented\n");
5712 long do_sigreturn(CPUArchState *env)
5714 fprintf(stderr, "do_sigreturn: not implemented\n");
5715 return -TARGET_ENOSYS;
5718 long do_rt_sigreturn(CPUArchState *env)
5720 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5721 return -TARGET_ENOSYS;
5724 #endif
5726 void process_pending_signals(CPUArchState *cpu_env)
5728 CPUState *cpu = ENV_GET_CPU(cpu_env);
5729 int sig;
5730 abi_ulong handler;
5731 sigset_t set, old_set;
5732 target_sigset_t target_old_set;
5733 struct emulated_sigtable *k;
5734 struct target_sigaction *sa;
5735 struct sigqueue *q;
5736 TaskState *ts = cpu->opaque;
5738 if (!ts->signal_pending)
5739 return;
5741 /* FIXME: This is not threadsafe. */
5742 k = ts->sigtab;
5743 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5744 if (k->pending)
5745 goto handle_signal;
5746 k++;
5748 /* if no signal is pending, just return */
5749 ts->signal_pending = 0;
5750 return;
5752 handle_signal:
5753 trace_user_handle_signal(cpu_env, sig);
5754 /* dequeue signal */
5755 q = k->first;
5756 k->first = q->next;
5757 if (!k->first)
5758 k->pending = 0;
5760 sig = gdb_handlesig(cpu, sig);
5761 if (!sig) {
5762 sa = NULL;
5763 handler = TARGET_SIG_IGN;
5764 } else {
5765 sa = &sigact_table[sig - 1];
5766 handler = sa->_sa_handler;
5769 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5770 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5771 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5772 * because it got a real MMU fault), and treat as if default handler.
5774 handler = TARGET_SIG_DFL;
5777 if (handler == TARGET_SIG_DFL) {
5778 /* default handler : ignore some signal. The other are job control or fatal */
5779 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5780 kill(getpid(),SIGSTOP);
5781 } else if (sig != TARGET_SIGCHLD &&
5782 sig != TARGET_SIGURG &&
5783 sig != TARGET_SIGWINCH &&
5784 sig != TARGET_SIGCONT) {
5785 force_sig(sig);
5787 } else if (handler == TARGET_SIG_IGN) {
5788 /* ignore sig */
5789 } else if (handler == TARGET_SIG_ERR) {
5790 force_sig(sig);
5791 } else {
5792 /* compute the blocked signals during the handler execution */
5793 target_to_host_sigset(&set, &sa->sa_mask);
5794 /* SA_NODEFER indicates that the current signal should not be
5795 blocked during the handler */
5796 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5797 sigaddset(&set, target_to_host_signal(sig));
5799 /* block signals in the handler using Linux */
5800 do_sigprocmask(SIG_BLOCK, &set, &old_set);
5801 /* save the previous blocked signal state to restore it at the
5802 end of the signal execution (see do_sigreturn) */
5803 host_to_target_sigset_internal(&target_old_set, &old_set);
5805 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5806 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5808 CPUX86State *env = cpu_env;
5809 if (env->eflags & VM_MASK)
5810 save_v86_state(env);
5812 #endif
5813 /* prepare the stack frame of the virtual CPU */
5814 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \
5815 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
5816 /* These targets do not have traditional signals. */
5817 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5818 #else
5819 if (sa->sa_flags & TARGET_SA_SIGINFO)
5820 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5821 else
5822 setup_frame(sig, sa, &target_old_set, cpu_env);
5823 #endif
5824 if (sa->sa_flags & TARGET_SA_RESETHAND)
5825 sa->_sa_handler = TARGET_SIG_DFL;
5827 if (q != &k->info)
5828 free_sigqueue(cpu_env, q);