use qemu_opts_create_nofail
[qemu.git] / linux-user / signal.c
blob95e2ffa007adfc0c0fb814c295b3f254bd0cda2c
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <sys/ucontext.h>
27 #include <sys/resource.h>
29 #include "qemu.h"
30 #include "qemu-common.h"
31 #include "target_signal.h"
33 //#define DEBUG_SIGNAL
35 static struct target_sigaltstack target_sigaltstack_used = {
36 .ss_sp = 0,
37 .ss_size = 0,
38 .ss_flags = TARGET_SS_DISABLE,
41 static struct target_sigaction sigact_table[TARGET_NSIG];
43 static void host_signal_handler(int host_signum, siginfo_t *info,
44 void *puc);
46 static uint8_t host_to_target_signal_table[_NSIG] = {
47 [SIGHUP] = TARGET_SIGHUP,
48 [SIGINT] = TARGET_SIGINT,
49 [SIGQUIT] = TARGET_SIGQUIT,
50 [SIGILL] = TARGET_SIGILL,
51 [SIGTRAP] = TARGET_SIGTRAP,
52 [SIGABRT] = TARGET_SIGABRT,
53 /* [SIGIOT] = TARGET_SIGIOT,*/
54 [SIGBUS] = TARGET_SIGBUS,
55 [SIGFPE] = TARGET_SIGFPE,
56 [SIGKILL] = TARGET_SIGKILL,
57 [SIGUSR1] = TARGET_SIGUSR1,
58 [SIGSEGV] = TARGET_SIGSEGV,
59 [SIGUSR2] = TARGET_SIGUSR2,
60 [SIGPIPE] = TARGET_SIGPIPE,
61 [SIGALRM] = TARGET_SIGALRM,
62 [SIGTERM] = TARGET_SIGTERM,
63 #ifdef SIGSTKFLT
64 [SIGSTKFLT] = TARGET_SIGSTKFLT,
65 #endif
66 [SIGCHLD] = TARGET_SIGCHLD,
67 [SIGCONT] = TARGET_SIGCONT,
68 [SIGSTOP] = TARGET_SIGSTOP,
69 [SIGTSTP] = TARGET_SIGTSTP,
70 [SIGTTIN] = TARGET_SIGTTIN,
71 [SIGTTOU] = TARGET_SIGTTOU,
72 [SIGURG] = TARGET_SIGURG,
73 [SIGXCPU] = TARGET_SIGXCPU,
74 [SIGXFSZ] = TARGET_SIGXFSZ,
75 [SIGVTALRM] = TARGET_SIGVTALRM,
76 [SIGPROF] = TARGET_SIGPROF,
77 [SIGWINCH] = TARGET_SIGWINCH,
78 [SIGIO] = TARGET_SIGIO,
79 [SIGPWR] = TARGET_SIGPWR,
80 [SIGSYS] = TARGET_SIGSYS,
81 /* next signals stay the same */
82 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
83 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
84 To fix this properly we need to do manual signal delivery multiplexed
85 over a single host signal. */
86 [__SIGRTMIN] = __SIGRTMAX,
87 [__SIGRTMAX] = __SIGRTMIN,
89 static uint8_t target_to_host_signal_table[_NSIG];
91 static inline int on_sig_stack(unsigned long sp)
93 return (sp - target_sigaltstack_used.ss_sp
94 < target_sigaltstack_used.ss_size);
97 static inline int sas_ss_flags(unsigned long sp)
99 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
100 : on_sig_stack(sp) ? SS_ONSTACK : 0);
103 int host_to_target_signal(int sig)
105 if (sig >= _NSIG)
106 return sig;
107 return host_to_target_signal_table[sig];
110 int target_to_host_signal(int sig)
112 if (sig >= _NSIG)
113 return sig;
114 return target_to_host_signal_table[sig];
117 static inline void target_sigemptyset(target_sigset_t *set)
119 memset(set, 0, sizeof(*set));
122 static inline void target_sigaddset(target_sigset_t *set, int signum)
124 signum--;
125 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126 set->sig[signum / TARGET_NSIG_BPW] |= mask;
129 static inline int target_sigismember(const target_sigset_t *set, int signum)
131 signum--;
132 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
133 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
136 static void host_to_target_sigset_internal(target_sigset_t *d,
137 const sigset_t *s)
139 int i;
140 target_sigemptyset(d);
141 for (i = 1; i <= TARGET_NSIG; i++) {
142 if (sigismember(s, i)) {
143 target_sigaddset(d, host_to_target_signal(i));
148 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
150 target_sigset_t d1;
151 int i;
153 host_to_target_sigset_internal(&d1, s);
154 for(i = 0;i < TARGET_NSIG_WORDS; i++)
155 d->sig[i] = tswapal(d1.sig[i]);
158 static void target_to_host_sigset_internal(sigset_t *d,
159 const target_sigset_t *s)
161 int i;
162 sigemptyset(d);
163 for (i = 1; i <= TARGET_NSIG; i++) {
164 if (target_sigismember(s, i)) {
165 sigaddset(d, target_to_host_signal(i));
170 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
172 target_sigset_t s1;
173 int i;
175 for(i = 0;i < TARGET_NSIG_WORDS; i++)
176 s1.sig[i] = tswapal(s->sig[i]);
177 target_to_host_sigset_internal(d, &s1);
180 void host_to_target_old_sigset(abi_ulong *old_sigset,
181 const sigset_t *sigset)
183 target_sigset_t d;
184 host_to_target_sigset(&d, sigset);
185 *old_sigset = d.sig[0];
188 void target_to_host_old_sigset(sigset_t *sigset,
189 const abi_ulong *old_sigset)
191 target_sigset_t d;
192 int i;
194 d.sig[0] = *old_sigset;
195 for(i = 1;i < TARGET_NSIG_WORDS; i++)
196 d.sig[i] = 0;
197 target_to_host_sigset(sigset, &d);
200 /* siginfo conversion */
202 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
203 const siginfo_t *info)
205 int sig = host_to_target_signal(info->si_signo);
206 tinfo->si_signo = sig;
207 tinfo->si_errno = 0;
208 tinfo->si_code = info->si_code;
210 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
211 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
212 /* Should never come here, but who knows. The information for
213 the target is irrelevant. */
214 tinfo->_sifields._sigfault._addr = 0;
215 } else if (sig == TARGET_SIGIO) {
216 tinfo->_sifields._sigpoll._band = info->si_band;
217 tinfo->_sifields._sigpoll._fd = info->si_fd;
218 } else if (sig == TARGET_SIGCHLD) {
219 tinfo->_sifields._sigchld._pid = info->si_pid;
220 tinfo->_sifields._sigchld._uid = info->si_uid;
221 tinfo->_sifields._sigchld._status
222 = host_to_target_waitstatus(info->si_status);
223 tinfo->_sifields._sigchld._utime = info->si_utime;
224 tinfo->_sifields._sigchld._stime = info->si_stime;
225 } else if (sig >= TARGET_SIGRTMIN) {
226 tinfo->_sifields._rt._pid = info->si_pid;
227 tinfo->_sifields._rt._uid = info->si_uid;
228 /* XXX: potential problem if 64 bit */
229 tinfo->_sifields._rt._sigval.sival_ptr
230 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
234 static void tswap_siginfo(target_siginfo_t *tinfo,
235 const target_siginfo_t *info)
237 int sig = info->si_signo;
238 tinfo->si_signo = tswap32(sig);
239 tinfo->si_errno = tswap32(info->si_errno);
240 tinfo->si_code = tswap32(info->si_code);
242 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
243 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
244 tinfo->_sifields._sigfault._addr
245 = tswapal(info->_sifields._sigfault._addr);
246 } else if (sig == TARGET_SIGIO) {
247 tinfo->_sifields._sigpoll._band
248 = tswap32(info->_sifields._sigpoll._band);
249 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
250 } else if (sig == TARGET_SIGCHLD) {
251 tinfo->_sifields._sigchld._pid
252 = tswap32(info->_sifields._sigchld._pid);
253 tinfo->_sifields._sigchld._uid
254 = tswap32(info->_sifields._sigchld._uid);
255 tinfo->_sifields._sigchld._status
256 = tswap32(info->_sifields._sigchld._status);
257 tinfo->_sifields._sigchld._utime
258 = tswapal(info->_sifields._sigchld._utime);
259 tinfo->_sifields._sigchld._stime
260 = tswapal(info->_sifields._sigchld._stime);
261 } else if (sig >= TARGET_SIGRTMIN) {
262 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
263 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
264 tinfo->_sifields._rt._sigval.sival_ptr
265 = tswapal(info->_sifields._rt._sigval.sival_ptr);
270 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
272 host_to_target_siginfo_noswap(tinfo, info);
273 tswap_siginfo(tinfo, tinfo);
276 /* XXX: we support only POSIX RT signals are used. */
277 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
278 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
280 info->si_signo = tswap32(tinfo->si_signo);
281 info->si_errno = tswap32(tinfo->si_errno);
282 info->si_code = tswap32(tinfo->si_code);
283 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
284 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
285 info->si_value.sival_ptr =
286 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
289 static int fatal_signal (int sig)
291 switch (sig) {
292 case TARGET_SIGCHLD:
293 case TARGET_SIGURG:
294 case TARGET_SIGWINCH:
295 /* Ignored by default. */
296 return 0;
297 case TARGET_SIGCONT:
298 case TARGET_SIGSTOP:
299 case TARGET_SIGTSTP:
300 case TARGET_SIGTTIN:
301 case TARGET_SIGTTOU:
302 /* Job control signals. */
303 return 0;
304 default:
305 return 1;
309 /* returns 1 if given signal should dump core if not handled */
310 static int core_dump_signal(int sig)
312 switch (sig) {
313 case TARGET_SIGABRT:
314 case TARGET_SIGFPE:
315 case TARGET_SIGILL:
316 case TARGET_SIGQUIT:
317 case TARGET_SIGSEGV:
318 case TARGET_SIGTRAP:
319 case TARGET_SIGBUS:
320 return (1);
321 default:
322 return (0);
326 void signal_init(void)
328 struct sigaction act;
329 struct sigaction oact;
330 int i, j;
331 int host_sig;
333 /* generate signal conversion tables */
334 for(i = 1; i < _NSIG; i++) {
335 if (host_to_target_signal_table[i] == 0)
336 host_to_target_signal_table[i] = i;
338 for(i = 1; i < _NSIG; i++) {
339 j = host_to_target_signal_table[i];
340 target_to_host_signal_table[j] = i;
343 /* set all host signal handlers. ALL signals are blocked during
344 the handlers to serialize them. */
345 memset(sigact_table, 0, sizeof(sigact_table));
347 sigfillset(&act.sa_mask);
348 act.sa_flags = SA_SIGINFO;
349 act.sa_sigaction = host_signal_handler;
350 for(i = 1; i <= TARGET_NSIG; i++) {
351 host_sig = target_to_host_signal(i);
352 sigaction(host_sig, NULL, &oact);
353 if (oact.sa_sigaction == (void *)SIG_IGN) {
354 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
355 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
356 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
358 /* If there's already a handler installed then something has
359 gone horribly wrong, so don't even try to handle that case. */
360 /* Install some handlers for our own use. We need at least
361 SIGSEGV and SIGBUS, to detect exceptions. We can not just
362 trap all signals because it affects syscall interrupt
363 behavior. But do trap all default-fatal signals. */
364 if (fatal_signal (i))
365 sigaction(host_sig, &act, NULL);
369 /* signal queue handling */
371 static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
373 TaskState *ts = env->opaque;
374 struct sigqueue *q = ts->first_free;
375 if (!q)
376 return NULL;
377 ts->first_free = q->next;
378 return q;
381 static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
383 TaskState *ts = env->opaque;
384 q->next = ts->first_free;
385 ts->first_free = q;
388 /* abort execution with signal */
389 static void QEMU_NORETURN force_sig(int target_sig)
391 TaskState *ts = (TaskState *)thread_env->opaque;
392 int host_sig, core_dumped = 0;
393 struct sigaction act;
394 host_sig = target_to_host_signal(target_sig);
395 gdb_signalled(thread_env, target_sig);
397 /* dump core if supported by target binary format */
398 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
399 stop_all_tasks();
400 core_dumped =
401 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
403 if (core_dumped) {
404 /* we already dumped the core of target process, we don't want
405 * a coredump of qemu itself */
406 struct rlimit nodump;
407 getrlimit(RLIMIT_CORE, &nodump);
408 nodump.rlim_cur=0;
409 setrlimit(RLIMIT_CORE, &nodump);
410 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
411 target_sig, strsignal(host_sig), "core dumped" );
414 /* The proper exit code for dying from an uncaught signal is
415 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
416 * a negative value. To get the proper exit code we need to
417 * actually die from an uncaught signal. Here the default signal
418 * handler is installed, we send ourself a signal and we wait for
419 * it to arrive. */
420 sigfillset(&act.sa_mask);
421 act.sa_handler = SIG_DFL;
422 sigaction(host_sig, &act, NULL);
424 /* For some reason raise(host_sig) doesn't send the signal when
425 * statically linked on x86-64. */
426 kill(getpid(), host_sig);
428 /* Make sure the signal isn't masked (just reuse the mask inside
429 of act) */
430 sigdelset(&act.sa_mask, host_sig);
431 sigsuspend(&act.sa_mask);
433 /* unreachable */
434 abort();
437 /* queue a signal so that it will be send to the virtual CPU as soon
438 as possible */
439 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
441 TaskState *ts = env->opaque;
442 struct emulated_sigtable *k;
443 struct sigqueue *q, **pq;
444 abi_ulong handler;
445 int queue;
447 #if defined(DEBUG_SIGNAL)
448 fprintf(stderr, "queue_signal: sig=%d\n",
449 sig);
450 #endif
451 k = &ts->sigtab[sig - 1];
452 queue = gdb_queuesig ();
453 handler = sigact_table[sig - 1]._sa_handler;
454 if (!queue && handler == TARGET_SIG_DFL) {
455 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
456 kill(getpid(),SIGSTOP);
457 return 0;
458 } else
459 /* default handler : ignore some signal. The other are fatal */
460 if (sig != TARGET_SIGCHLD &&
461 sig != TARGET_SIGURG &&
462 sig != TARGET_SIGWINCH &&
463 sig != TARGET_SIGCONT) {
464 force_sig(sig);
465 } else {
466 return 0; /* indicate ignored */
468 } else if (!queue && handler == TARGET_SIG_IGN) {
469 /* ignore signal */
470 return 0;
471 } else if (!queue && handler == TARGET_SIG_ERR) {
472 force_sig(sig);
473 } else {
474 pq = &k->first;
475 if (sig < TARGET_SIGRTMIN) {
476 /* if non real time signal, we queue exactly one signal */
477 if (!k->pending)
478 q = &k->info;
479 else
480 return 0;
481 } else {
482 if (!k->pending) {
483 /* first signal */
484 q = &k->info;
485 } else {
486 q = alloc_sigqueue(env);
487 if (!q)
488 return -EAGAIN;
489 while (*pq != NULL)
490 pq = &(*pq)->next;
493 *pq = q;
494 q->info = *info;
495 q->next = NULL;
496 k->pending = 1;
497 /* signal that a new signal is pending */
498 ts->signal_pending = 1;
499 return 1; /* indicates that the signal was queued */
503 static void host_signal_handler(int host_signum, siginfo_t *info,
504 void *puc)
506 int sig;
507 target_siginfo_t tinfo;
509 /* the CPU emulator uses some host signals to detect exceptions,
510 we forward to it some signals */
511 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
512 && info->si_code > 0) {
513 if (cpu_signal_handler(host_signum, info, puc))
514 return;
517 /* get target signal number */
518 sig = host_to_target_signal(host_signum);
519 if (sig < 1 || sig > TARGET_NSIG)
520 return;
521 #if defined(DEBUG_SIGNAL)
522 fprintf(stderr, "qemu: got signal %d\n", sig);
523 #endif
524 host_to_target_siginfo_noswap(&tinfo, info);
525 if (queue_signal(thread_env, sig, &tinfo) == 1) {
526 /* interrupt the virtual CPU as soon as possible */
527 cpu_exit(thread_env);
531 /* do_sigaltstack() returns target values and errnos. */
532 /* compare linux/kernel/signal.c:do_sigaltstack() */
533 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
535 int ret;
536 struct target_sigaltstack oss;
538 /* XXX: test errors */
539 if(uoss_addr)
541 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
542 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
543 __put_user(sas_ss_flags(sp), &oss.ss_flags);
546 if(uss_addr)
548 struct target_sigaltstack *uss;
549 struct target_sigaltstack ss;
551 ret = -TARGET_EFAULT;
552 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
553 || __get_user(ss.ss_sp, &uss->ss_sp)
554 || __get_user(ss.ss_size, &uss->ss_size)
555 || __get_user(ss.ss_flags, &uss->ss_flags))
556 goto out;
557 unlock_user_struct(uss, uss_addr, 0);
559 ret = -TARGET_EPERM;
560 if (on_sig_stack(sp))
561 goto out;
563 ret = -TARGET_EINVAL;
564 if (ss.ss_flags != TARGET_SS_DISABLE
565 && ss.ss_flags != TARGET_SS_ONSTACK
566 && ss.ss_flags != 0)
567 goto out;
569 if (ss.ss_flags == TARGET_SS_DISABLE) {
570 ss.ss_size = 0;
571 ss.ss_sp = 0;
572 } else {
573 ret = -TARGET_ENOMEM;
574 if (ss.ss_size < MINSIGSTKSZ)
575 goto out;
578 target_sigaltstack_used.ss_sp = ss.ss_sp;
579 target_sigaltstack_used.ss_size = ss.ss_size;
582 if (uoss_addr) {
583 ret = -TARGET_EFAULT;
584 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
585 goto out;
588 ret = 0;
589 out:
590 return ret;
593 /* do_sigaction() return host values and errnos */
594 int do_sigaction(int sig, const struct target_sigaction *act,
595 struct target_sigaction *oact)
597 struct target_sigaction *k;
598 struct sigaction act1;
599 int host_sig;
600 int ret = 0;
602 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
603 return -EINVAL;
604 k = &sigact_table[sig - 1];
605 #if defined(DEBUG_SIGNAL)
606 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
607 sig, act, oact);
608 #endif
609 if (oact) {
610 oact->_sa_handler = tswapal(k->_sa_handler);
611 #if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
612 oact->sa_flags = bswap32(k->sa_flags);
613 #else
614 oact->sa_flags = tswapal(k->sa_flags);
615 #endif
616 #if !defined(TARGET_MIPS)
617 oact->sa_restorer = tswapal(k->sa_restorer);
618 #endif
619 oact->sa_mask = k->sa_mask;
621 if (act) {
622 /* FIXME: This is not threadsafe. */
623 k->_sa_handler = tswapal(act->_sa_handler);
624 #if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
625 k->sa_flags = bswap32(act->sa_flags);
626 #else
627 k->sa_flags = tswapal(act->sa_flags);
628 #endif
629 #if !defined(TARGET_MIPS)
630 k->sa_restorer = tswapal(act->sa_restorer);
631 #endif
632 k->sa_mask = act->sa_mask;
634 /* we update the host linux signal state */
635 host_sig = target_to_host_signal(sig);
636 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
637 sigfillset(&act1.sa_mask);
638 act1.sa_flags = SA_SIGINFO;
639 if (k->sa_flags & TARGET_SA_RESTART)
640 act1.sa_flags |= SA_RESTART;
641 /* NOTE: it is important to update the host kernel signal
642 ignore state to avoid getting unexpected interrupted
643 syscalls */
644 if (k->_sa_handler == TARGET_SIG_IGN) {
645 act1.sa_sigaction = (void *)SIG_IGN;
646 } else if (k->_sa_handler == TARGET_SIG_DFL) {
647 if (fatal_signal (sig))
648 act1.sa_sigaction = host_signal_handler;
649 else
650 act1.sa_sigaction = (void *)SIG_DFL;
651 } else {
652 act1.sa_sigaction = host_signal_handler;
654 ret = sigaction(host_sig, &act1, NULL);
657 return ret;
660 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
661 const target_siginfo_t *info)
663 tswap_siginfo(tinfo, info);
664 return 0;
667 static inline int current_exec_domain_sig(int sig)
669 return /* current->exec_domain && current->exec_domain->signal_invmap
670 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
673 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
675 /* from the Linux kernel */
677 struct target_fpreg {
678 uint16_t significand[4];
679 uint16_t exponent;
682 struct target_fpxreg {
683 uint16_t significand[4];
684 uint16_t exponent;
685 uint16_t padding[3];
688 struct target_xmmreg {
689 abi_ulong element[4];
692 struct target_fpstate {
693 /* Regular FPU environment */
694 abi_ulong cw;
695 abi_ulong sw;
696 abi_ulong tag;
697 abi_ulong ipoff;
698 abi_ulong cssel;
699 abi_ulong dataoff;
700 abi_ulong datasel;
701 struct target_fpreg _st[8];
702 uint16_t status;
703 uint16_t magic; /* 0xffff = regular FPU data only */
705 /* FXSR FPU environment */
706 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
707 abi_ulong mxcsr;
708 abi_ulong reserved;
709 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
710 struct target_xmmreg _xmm[8];
711 abi_ulong padding[56];
714 #define X86_FXSR_MAGIC 0x0000
716 struct target_sigcontext {
717 uint16_t gs, __gsh;
718 uint16_t fs, __fsh;
719 uint16_t es, __esh;
720 uint16_t ds, __dsh;
721 abi_ulong edi;
722 abi_ulong esi;
723 abi_ulong ebp;
724 abi_ulong esp;
725 abi_ulong ebx;
726 abi_ulong edx;
727 abi_ulong ecx;
728 abi_ulong eax;
729 abi_ulong trapno;
730 abi_ulong err;
731 abi_ulong eip;
732 uint16_t cs, __csh;
733 abi_ulong eflags;
734 abi_ulong esp_at_signal;
735 uint16_t ss, __ssh;
736 abi_ulong fpstate; /* pointer */
737 abi_ulong oldmask;
738 abi_ulong cr2;
741 struct target_ucontext {
742 abi_ulong tuc_flags;
743 abi_ulong tuc_link;
744 target_stack_t tuc_stack;
745 struct target_sigcontext tuc_mcontext;
746 target_sigset_t tuc_sigmask; /* mask last for extensibility */
749 struct sigframe
751 abi_ulong pretcode;
752 int sig;
753 struct target_sigcontext sc;
754 struct target_fpstate fpstate;
755 abi_ulong extramask[TARGET_NSIG_WORDS-1];
756 char retcode[8];
759 struct rt_sigframe
761 abi_ulong pretcode;
762 int sig;
763 abi_ulong pinfo;
764 abi_ulong puc;
765 struct target_siginfo info;
766 struct target_ucontext uc;
767 struct target_fpstate fpstate;
768 char retcode[8];
772 * Set up a signal frame.
775 /* XXX: save x87 state */
776 static int
777 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
778 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
780 int err = 0;
781 uint16_t magic;
783 /* already locked in setup_frame() */
784 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
785 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
786 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
787 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
788 err |= __put_user(env->regs[R_EDI], &sc->edi);
789 err |= __put_user(env->regs[R_ESI], &sc->esi);
790 err |= __put_user(env->regs[R_EBP], &sc->ebp);
791 err |= __put_user(env->regs[R_ESP], &sc->esp);
792 err |= __put_user(env->regs[R_EBX], &sc->ebx);
793 err |= __put_user(env->regs[R_EDX], &sc->edx);
794 err |= __put_user(env->regs[R_ECX], &sc->ecx);
795 err |= __put_user(env->regs[R_EAX], &sc->eax);
796 err |= __put_user(env->exception_index, &sc->trapno);
797 err |= __put_user(env->error_code, &sc->err);
798 err |= __put_user(env->eip, &sc->eip);
799 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
800 err |= __put_user(env->eflags, &sc->eflags);
801 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
802 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
804 cpu_x86_fsave(env, fpstate_addr, 1);
805 fpstate->status = fpstate->sw;
806 magic = 0xffff;
807 err |= __put_user(magic, &fpstate->magic);
808 err |= __put_user(fpstate_addr, &sc->fpstate);
810 /* non-iBCS2 extensions.. */
811 err |= __put_user(mask, &sc->oldmask);
812 err |= __put_user(env->cr[2], &sc->cr2);
813 return err;
817 * Determine which stack to use..
820 static inline abi_ulong
821 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
823 unsigned long esp;
825 /* Default to using normal stack */
826 esp = env->regs[R_ESP];
827 /* This is the X/Open sanctioned signal stack switching. */
828 if (ka->sa_flags & TARGET_SA_ONSTACK) {
829 if (sas_ss_flags(esp) == 0)
830 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
833 /* This is the legacy signal stack switching. */
834 else
835 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
836 !(ka->sa_flags & TARGET_SA_RESTORER) &&
837 ka->sa_restorer) {
838 esp = (unsigned long) ka->sa_restorer;
840 return (esp - frame_size) & -8ul;
843 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
844 static void setup_frame(int sig, struct target_sigaction *ka,
845 target_sigset_t *set, CPUX86State *env)
847 abi_ulong frame_addr;
848 struct sigframe *frame;
849 int i, err = 0;
851 frame_addr = get_sigframe(ka, env, sizeof(*frame));
853 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
854 goto give_sigsegv;
856 err |= __put_user(current_exec_domain_sig(sig),
857 &frame->sig);
858 if (err)
859 goto give_sigsegv;
861 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
862 frame_addr + offsetof(struct sigframe, fpstate));
863 if (err)
864 goto give_sigsegv;
866 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
867 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
868 goto give_sigsegv;
871 /* Set up to return from userspace. If provided, use a stub
872 already in userspace. */
873 if (ka->sa_flags & TARGET_SA_RESTORER) {
874 err |= __put_user(ka->sa_restorer, &frame->pretcode);
875 } else {
876 uint16_t val16;
877 abi_ulong retcode_addr;
878 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
879 err |= __put_user(retcode_addr, &frame->pretcode);
880 /* This is popl %eax ; movl $,%eax ; int $0x80 */
881 val16 = 0xb858;
882 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
883 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
884 val16 = 0x80cd;
885 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
888 if (err)
889 goto give_sigsegv;
891 /* Set up registers for signal handler */
892 env->regs[R_ESP] = frame_addr;
893 env->eip = ka->_sa_handler;
895 cpu_x86_load_seg(env, R_DS, __USER_DS);
896 cpu_x86_load_seg(env, R_ES, __USER_DS);
897 cpu_x86_load_seg(env, R_SS, __USER_DS);
898 cpu_x86_load_seg(env, R_CS, __USER_CS);
899 env->eflags &= ~TF_MASK;
901 unlock_user_struct(frame, frame_addr, 1);
903 return;
905 give_sigsegv:
906 unlock_user_struct(frame, frame_addr, 1);
907 if (sig == TARGET_SIGSEGV)
908 ka->_sa_handler = TARGET_SIG_DFL;
909 force_sig(TARGET_SIGSEGV /* , current */);
912 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
913 static void setup_rt_frame(int sig, struct target_sigaction *ka,
914 target_siginfo_t *info,
915 target_sigset_t *set, CPUX86State *env)
917 abi_ulong frame_addr, addr;
918 struct rt_sigframe *frame;
919 int i, err = 0;
921 frame_addr = get_sigframe(ka, env, sizeof(*frame));
923 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
924 goto give_sigsegv;
926 err |= __put_user(current_exec_domain_sig(sig),
927 &frame->sig);
928 addr = frame_addr + offsetof(struct rt_sigframe, info);
929 err |= __put_user(addr, &frame->pinfo);
930 addr = frame_addr + offsetof(struct rt_sigframe, uc);
931 err |= __put_user(addr, &frame->puc);
932 err |= copy_siginfo_to_user(&frame->info, info);
933 if (err)
934 goto give_sigsegv;
936 /* Create the ucontext. */
937 err |= __put_user(0, &frame->uc.tuc_flags);
938 err |= __put_user(0, &frame->uc.tuc_link);
939 err |= __put_user(target_sigaltstack_used.ss_sp,
940 &frame->uc.tuc_stack.ss_sp);
941 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
942 &frame->uc.tuc_stack.ss_flags);
943 err |= __put_user(target_sigaltstack_used.ss_size,
944 &frame->uc.tuc_stack.ss_size);
945 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
946 env, set->sig[0],
947 frame_addr + offsetof(struct rt_sigframe, fpstate));
948 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
949 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
950 goto give_sigsegv;
953 /* Set up to return from userspace. If provided, use a stub
954 already in userspace. */
955 if (ka->sa_flags & TARGET_SA_RESTORER) {
956 err |= __put_user(ka->sa_restorer, &frame->pretcode);
957 } else {
958 uint16_t val16;
959 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
960 err |= __put_user(addr, &frame->pretcode);
961 /* This is movl $,%eax ; int $0x80 */
962 err |= __put_user(0xb8, (char *)(frame->retcode+0));
963 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
964 val16 = 0x80cd;
965 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
968 if (err)
969 goto give_sigsegv;
971 /* Set up registers for signal handler */
972 env->regs[R_ESP] = frame_addr;
973 env->eip = ka->_sa_handler;
975 cpu_x86_load_seg(env, R_DS, __USER_DS);
976 cpu_x86_load_seg(env, R_ES, __USER_DS);
977 cpu_x86_load_seg(env, R_SS, __USER_DS);
978 cpu_x86_load_seg(env, R_CS, __USER_CS);
979 env->eflags &= ~TF_MASK;
981 unlock_user_struct(frame, frame_addr, 1);
983 return;
985 give_sigsegv:
986 unlock_user_struct(frame, frame_addr, 1);
987 if (sig == TARGET_SIGSEGV)
988 ka->_sa_handler = TARGET_SIG_DFL;
989 force_sig(TARGET_SIGSEGV /* , current */);
992 static int
993 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
995 unsigned int err = 0;
996 abi_ulong fpstate_addr;
997 unsigned int tmpflags;
999 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1000 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1001 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1002 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1004 env->regs[R_EDI] = tswapl(sc->edi);
1005 env->regs[R_ESI] = tswapl(sc->esi);
1006 env->regs[R_EBP] = tswapl(sc->ebp);
1007 env->regs[R_ESP] = tswapl(sc->esp);
1008 env->regs[R_EBX] = tswapl(sc->ebx);
1009 env->regs[R_EDX] = tswapl(sc->edx);
1010 env->regs[R_ECX] = tswapl(sc->ecx);
1011 env->eip = tswapl(sc->eip);
1013 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1014 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1016 tmpflags = tswapl(sc->eflags);
1017 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1018 // regs->orig_eax = -1; /* disable syscall checks */
1020 fpstate_addr = tswapl(sc->fpstate);
1021 if (fpstate_addr != 0) {
1022 if (!access_ok(VERIFY_READ, fpstate_addr,
1023 sizeof(struct target_fpstate)))
1024 goto badframe;
1025 cpu_x86_frstor(env, fpstate_addr, 1);
1028 *peax = tswapl(sc->eax);
1029 return err;
1030 badframe:
1031 return 1;
1034 long do_sigreturn(CPUX86State *env)
1036 struct sigframe *frame;
1037 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1038 target_sigset_t target_set;
1039 sigset_t set;
1040 int eax, i;
1042 #if defined(DEBUG_SIGNAL)
1043 fprintf(stderr, "do_sigreturn\n");
1044 #endif
1045 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1046 goto badframe;
1047 /* set blocked signals */
1048 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1049 goto badframe;
1050 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1051 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1052 goto badframe;
1055 target_to_host_sigset_internal(&set, &target_set);
1056 sigprocmask(SIG_SETMASK, &set, NULL);
1058 /* restore registers */
1059 if (restore_sigcontext(env, &frame->sc, &eax))
1060 goto badframe;
1061 unlock_user_struct(frame, frame_addr, 0);
1062 return eax;
1064 badframe:
1065 unlock_user_struct(frame, frame_addr, 0);
1066 force_sig(TARGET_SIGSEGV);
1067 return 0;
1070 long do_rt_sigreturn(CPUX86State *env)
1072 abi_ulong frame_addr;
1073 struct rt_sigframe *frame;
1074 sigset_t set;
1075 int eax;
1077 frame_addr = env->regs[R_ESP] - 4;
1078 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1079 goto badframe;
1080 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1081 sigprocmask(SIG_SETMASK, &set, NULL);
1083 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1084 goto badframe;
1086 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1087 get_sp_from_cpustate(env)) == -EFAULT)
1088 goto badframe;
1090 unlock_user_struct(frame, frame_addr, 0);
1091 return eax;
1093 badframe:
1094 unlock_user_struct(frame, frame_addr, 0);
1095 force_sig(TARGET_SIGSEGV);
1096 return 0;
1099 #elif defined(TARGET_ARM)
1101 struct target_sigcontext {
1102 abi_ulong trap_no;
1103 abi_ulong error_code;
1104 abi_ulong oldmask;
1105 abi_ulong arm_r0;
1106 abi_ulong arm_r1;
1107 abi_ulong arm_r2;
1108 abi_ulong arm_r3;
1109 abi_ulong arm_r4;
1110 abi_ulong arm_r5;
1111 abi_ulong arm_r6;
1112 abi_ulong arm_r7;
1113 abi_ulong arm_r8;
1114 abi_ulong arm_r9;
1115 abi_ulong arm_r10;
1116 abi_ulong arm_fp;
1117 abi_ulong arm_ip;
1118 abi_ulong arm_sp;
1119 abi_ulong arm_lr;
1120 abi_ulong arm_pc;
1121 abi_ulong arm_cpsr;
1122 abi_ulong fault_address;
1125 struct target_ucontext_v1 {
1126 abi_ulong tuc_flags;
1127 abi_ulong tuc_link;
1128 target_stack_t tuc_stack;
1129 struct target_sigcontext tuc_mcontext;
1130 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1133 struct target_ucontext_v2 {
1134 abi_ulong tuc_flags;
1135 abi_ulong tuc_link;
1136 target_stack_t tuc_stack;
1137 struct target_sigcontext tuc_mcontext;
1138 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1139 char __unused[128 - sizeof(target_sigset_t)];
1140 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1143 struct target_user_vfp {
1144 uint64_t fpregs[32];
1145 abi_ulong fpscr;
1148 struct target_user_vfp_exc {
1149 abi_ulong fpexc;
1150 abi_ulong fpinst;
1151 abi_ulong fpinst2;
1154 struct target_vfp_sigframe {
1155 abi_ulong magic;
1156 abi_ulong size;
1157 struct target_user_vfp ufp;
1158 struct target_user_vfp_exc ufp_exc;
1159 } __attribute__((__aligned__(8)));
1161 struct target_iwmmxt_sigframe {
1162 abi_ulong magic;
1163 abi_ulong size;
1164 uint64_t regs[16];
1165 /* Note that not all the coprocessor control registers are stored here */
1166 uint32_t wcssf;
1167 uint32_t wcasf;
1168 uint32_t wcgr0;
1169 uint32_t wcgr1;
1170 uint32_t wcgr2;
1171 uint32_t wcgr3;
1172 } __attribute__((__aligned__(8)));
1174 #define TARGET_VFP_MAGIC 0x56465001
1175 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1177 struct sigframe_v1
1179 struct target_sigcontext sc;
1180 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1181 abi_ulong retcode;
1184 struct sigframe_v2
1186 struct target_ucontext_v2 uc;
1187 abi_ulong retcode;
1190 struct rt_sigframe_v1
1192 abi_ulong pinfo;
1193 abi_ulong puc;
1194 struct target_siginfo info;
1195 struct target_ucontext_v1 uc;
1196 abi_ulong retcode;
1199 struct rt_sigframe_v2
1201 struct target_siginfo info;
1202 struct target_ucontext_v2 uc;
1203 abi_ulong retcode;
1206 #define TARGET_CONFIG_CPU_32 1
1209 * For ARM syscalls, we encode the syscall number into the instruction.
1211 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1212 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1215 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1216 * need two 16-bit instructions.
1218 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1219 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1221 static const abi_ulong retcodes[4] = {
1222 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1223 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1227 #define __get_user_error(x,p,e) __get_user(x, p)
1229 static inline int valid_user_regs(CPUARMState *regs)
1231 return 1;
1234 static void
1235 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1236 CPUARMState *env, abi_ulong mask)
1238 __put_user(env->regs[0], &sc->arm_r0);
1239 __put_user(env->regs[1], &sc->arm_r1);
1240 __put_user(env->regs[2], &sc->arm_r2);
1241 __put_user(env->regs[3], &sc->arm_r3);
1242 __put_user(env->regs[4], &sc->arm_r4);
1243 __put_user(env->regs[5], &sc->arm_r5);
1244 __put_user(env->regs[6], &sc->arm_r6);
1245 __put_user(env->regs[7], &sc->arm_r7);
1246 __put_user(env->regs[8], &sc->arm_r8);
1247 __put_user(env->regs[9], &sc->arm_r9);
1248 __put_user(env->regs[10], &sc->arm_r10);
1249 __put_user(env->regs[11], &sc->arm_fp);
1250 __put_user(env->regs[12], &sc->arm_ip);
1251 __put_user(env->regs[13], &sc->arm_sp);
1252 __put_user(env->regs[14], &sc->arm_lr);
1253 __put_user(env->regs[15], &sc->arm_pc);
1254 #ifdef TARGET_CONFIG_CPU_32
1255 __put_user(cpsr_read(env), &sc->arm_cpsr);
1256 #endif
1258 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1259 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1260 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1261 __put_user(mask, &sc->oldmask);
1264 static inline abi_ulong
1265 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1267 unsigned long sp = regs->regs[13];
1270 * This is the X/Open sanctioned signal stack switching.
1272 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1273 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1275 * ATPCS B01 mandates 8-byte alignment
1277 return (sp - framesize) & ~7;
1280 static int
1281 setup_return(CPUARMState *env, struct target_sigaction *ka,
1282 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1284 abi_ulong handler = ka->_sa_handler;
1285 abi_ulong retcode;
1286 int thumb = handler & 1;
1287 uint32_t cpsr = cpsr_read(env);
1289 cpsr &= ~CPSR_IT;
1290 if (thumb) {
1291 cpsr |= CPSR_T;
1292 } else {
1293 cpsr &= ~CPSR_T;
1296 if (ka->sa_flags & TARGET_SA_RESTORER) {
1297 retcode = ka->sa_restorer;
1298 } else {
1299 unsigned int idx = thumb;
1301 if (ka->sa_flags & TARGET_SA_SIGINFO)
1302 idx += 2;
1304 if (__put_user(retcodes[idx], rc))
1305 return 1;
1307 retcode = rc_addr + thumb;
1310 env->regs[0] = usig;
1311 env->regs[13] = frame_addr;
1312 env->regs[14] = retcode;
1313 env->regs[15] = handler & (thumb ? ~1 : ~3);
1314 cpsr_write(env, cpsr, 0xffffffff);
1316 return 0;
1319 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1321 int i;
1322 struct target_vfp_sigframe *vfpframe;
1323 vfpframe = (struct target_vfp_sigframe *)regspace;
1324 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1325 __put_user(sizeof(*vfpframe), &vfpframe->size);
1326 for (i = 0; i < 32; i++) {
1327 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1329 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1330 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1331 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1332 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1333 return (abi_ulong*)(vfpframe+1);
1336 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1337 CPUARMState *env)
1339 int i;
1340 struct target_iwmmxt_sigframe *iwmmxtframe;
1341 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1342 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1343 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1344 for (i = 0; i < 16; i++) {
1345 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1347 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1348 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1349 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1350 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1351 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1352 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1353 return (abi_ulong*)(iwmmxtframe+1);
1356 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1357 target_sigset_t *set, CPUARMState *env)
1359 struct target_sigaltstack stack;
1360 int i;
1361 abi_ulong *regspace;
1363 /* Clear all the bits of the ucontext we don't use. */
1364 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1366 memset(&stack, 0, sizeof(stack));
1367 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1368 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1369 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1370 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1372 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1373 /* Save coprocessor signal frame. */
1374 regspace = uc->tuc_regspace;
1375 if (arm_feature(env, ARM_FEATURE_VFP)) {
1376 regspace = setup_sigframe_v2_vfp(regspace, env);
1378 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1379 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1382 /* Write terminating magic word */
1383 __put_user(0, regspace);
1385 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1386 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1390 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1391 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1392 target_sigset_t *set, CPUARMState *regs)
1394 struct sigframe_v1 *frame;
1395 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1396 int i;
1398 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1399 return;
1401 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1403 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1404 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1405 goto end;
1408 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1409 frame_addr + offsetof(struct sigframe_v1, retcode));
1411 end:
1412 unlock_user_struct(frame, frame_addr, 1);
1415 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1416 target_sigset_t *set, CPUARMState *regs)
1418 struct sigframe_v2 *frame;
1419 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1421 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1422 return;
1424 setup_sigframe_v2(&frame->uc, set, regs);
1426 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1427 frame_addr + offsetof(struct sigframe_v2, retcode));
1429 unlock_user_struct(frame, frame_addr, 1);
1432 static void setup_frame(int usig, struct target_sigaction *ka,
1433 target_sigset_t *set, CPUARMState *regs)
1435 if (get_osversion() >= 0x020612) {
1436 setup_frame_v2(usig, ka, set, regs);
1437 } else {
1438 setup_frame_v1(usig, ka, set, regs);
1442 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1443 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1444 target_siginfo_t *info,
1445 target_sigset_t *set, CPUARMState *env)
1447 struct rt_sigframe_v1 *frame;
1448 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1449 struct target_sigaltstack stack;
1450 int i;
1451 abi_ulong info_addr, uc_addr;
1453 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1454 return /* 1 */;
1456 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1457 __put_user(info_addr, &frame->pinfo);
1458 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1459 __put_user(uc_addr, &frame->puc);
1460 copy_siginfo_to_user(&frame->info, info);
1462 /* Clear all the bits of the ucontext we don't use. */
1463 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1465 memset(&stack, 0, sizeof(stack));
1466 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1467 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1468 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1469 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1471 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1472 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1473 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1474 goto end;
1477 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1478 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1480 env->regs[1] = info_addr;
1481 env->regs[2] = uc_addr;
1483 end:
1484 unlock_user_struct(frame, frame_addr, 1);
1487 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1488 target_siginfo_t *info,
1489 target_sigset_t *set, CPUARMState *env)
1491 struct rt_sigframe_v2 *frame;
1492 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1493 abi_ulong info_addr, uc_addr;
1495 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1496 return /* 1 */;
1498 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1499 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1500 copy_siginfo_to_user(&frame->info, info);
1502 setup_sigframe_v2(&frame->uc, set, env);
1504 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1505 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1507 env->regs[1] = info_addr;
1508 env->regs[2] = uc_addr;
1510 unlock_user_struct(frame, frame_addr, 1);
1513 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1514 target_siginfo_t *info,
1515 target_sigset_t *set, CPUARMState *env)
1517 if (get_osversion() >= 0x020612) {
1518 setup_rt_frame_v2(usig, ka, info, set, env);
1519 } else {
1520 setup_rt_frame_v1(usig, ka, info, set, env);
1524 static int
1525 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1527 int err = 0;
1528 uint32_t cpsr;
1530 __get_user_error(env->regs[0], &sc->arm_r0, err);
1531 __get_user_error(env->regs[1], &sc->arm_r1, err);
1532 __get_user_error(env->regs[2], &sc->arm_r2, err);
1533 __get_user_error(env->regs[3], &sc->arm_r3, err);
1534 __get_user_error(env->regs[4], &sc->arm_r4, err);
1535 __get_user_error(env->regs[5], &sc->arm_r5, err);
1536 __get_user_error(env->regs[6], &sc->arm_r6, err);
1537 __get_user_error(env->regs[7], &sc->arm_r7, err);
1538 __get_user_error(env->regs[8], &sc->arm_r8, err);
1539 __get_user_error(env->regs[9], &sc->arm_r9, err);
1540 __get_user_error(env->regs[10], &sc->arm_r10, err);
1541 __get_user_error(env->regs[11], &sc->arm_fp, err);
1542 __get_user_error(env->regs[12], &sc->arm_ip, err);
1543 __get_user_error(env->regs[13], &sc->arm_sp, err);
1544 __get_user_error(env->regs[14], &sc->arm_lr, err);
1545 __get_user_error(env->regs[15], &sc->arm_pc, err);
1546 #ifdef TARGET_CONFIG_CPU_32
1547 __get_user_error(cpsr, &sc->arm_cpsr, err);
1548 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1549 #endif
1551 err |= !valid_user_regs(env);
1553 return err;
1556 static long do_sigreturn_v1(CPUARMState *env)
1558 abi_ulong frame_addr;
1559 struct sigframe_v1 *frame;
1560 target_sigset_t set;
1561 sigset_t host_set;
1562 int i;
1565 * Since we stacked the signal on a 64-bit boundary,
1566 * then 'sp' should be word aligned here. If it's
1567 * not, then the user is trying to mess with us.
1569 if (env->regs[13] & 7)
1570 goto badframe;
1572 frame_addr = env->regs[13];
1573 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1574 goto badframe;
1576 if (__get_user(set.sig[0], &frame->sc.oldmask))
1577 goto badframe;
1578 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1579 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1580 goto badframe;
1583 target_to_host_sigset_internal(&host_set, &set);
1584 sigprocmask(SIG_SETMASK, &host_set, NULL);
1586 if (restore_sigcontext(env, &frame->sc))
1587 goto badframe;
1589 #if 0
1590 /* Send SIGTRAP if we're single-stepping */
1591 if (ptrace_cancel_bpt(current))
1592 send_sig(SIGTRAP, current, 1);
1593 #endif
1594 unlock_user_struct(frame, frame_addr, 0);
1595 return env->regs[0];
1597 badframe:
1598 unlock_user_struct(frame, frame_addr, 0);
1599 force_sig(TARGET_SIGSEGV /* , current */);
1600 return 0;
1603 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1605 int i;
1606 abi_ulong magic, sz;
1607 uint32_t fpscr, fpexc;
1608 struct target_vfp_sigframe *vfpframe;
1609 vfpframe = (struct target_vfp_sigframe *)regspace;
1611 __get_user(magic, &vfpframe->magic);
1612 __get_user(sz, &vfpframe->size);
1613 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1614 return 0;
1616 for (i = 0; i < 32; i++) {
1617 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1619 __get_user(fpscr, &vfpframe->ufp.fpscr);
1620 vfp_set_fpscr(env, fpscr);
1621 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1622 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1623 * and the exception flag is cleared
1625 fpexc |= (1 << 30);
1626 fpexc &= ~((1 << 31) | (1 << 28));
1627 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1628 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1629 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1630 return (abi_ulong*)(vfpframe + 1);
1633 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1634 abi_ulong *regspace)
1636 int i;
1637 abi_ulong magic, sz;
1638 struct target_iwmmxt_sigframe *iwmmxtframe;
1639 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1641 __get_user(magic, &iwmmxtframe->magic);
1642 __get_user(sz, &iwmmxtframe->size);
1643 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1644 return 0;
1646 for (i = 0; i < 16; i++) {
1647 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1649 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1650 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1651 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1652 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1653 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1654 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1655 return (abi_ulong*)(iwmmxtframe + 1);
1658 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1659 struct target_ucontext_v2 *uc)
1661 sigset_t host_set;
1662 abi_ulong *regspace;
1664 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1665 sigprocmask(SIG_SETMASK, &host_set, NULL);
1667 if (restore_sigcontext(env, &uc->tuc_mcontext))
1668 return 1;
1670 /* Restore coprocessor signal frame */
1671 regspace = uc->tuc_regspace;
1672 if (arm_feature(env, ARM_FEATURE_VFP)) {
1673 regspace = restore_sigframe_v2_vfp(env, regspace);
1674 if (!regspace) {
1675 return 1;
1678 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1679 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1680 if (!regspace) {
1681 return 1;
1685 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1686 return 1;
1688 #if 0
1689 /* Send SIGTRAP if we're single-stepping */
1690 if (ptrace_cancel_bpt(current))
1691 send_sig(SIGTRAP, current, 1);
1692 #endif
1694 return 0;
1697 static long do_sigreturn_v2(CPUARMState *env)
1699 abi_ulong frame_addr;
1700 struct sigframe_v2 *frame;
1703 * Since we stacked the signal on a 64-bit boundary,
1704 * then 'sp' should be word aligned here. If it's
1705 * not, then the user is trying to mess with us.
1707 if (env->regs[13] & 7)
1708 goto badframe;
1710 frame_addr = env->regs[13];
1711 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1712 goto badframe;
1714 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1715 goto badframe;
1717 unlock_user_struct(frame, frame_addr, 0);
1718 return env->regs[0];
1720 badframe:
1721 unlock_user_struct(frame, frame_addr, 0);
1722 force_sig(TARGET_SIGSEGV /* , current */);
1723 return 0;
1726 long do_sigreturn(CPUARMState *env)
1728 if (get_osversion() >= 0x020612) {
1729 return do_sigreturn_v2(env);
1730 } else {
1731 return do_sigreturn_v1(env);
1735 static long do_rt_sigreturn_v1(CPUARMState *env)
1737 abi_ulong frame_addr;
1738 struct rt_sigframe_v1 *frame;
1739 sigset_t host_set;
1742 * Since we stacked the signal on a 64-bit boundary,
1743 * then 'sp' should be word aligned here. If it's
1744 * not, then the user is trying to mess with us.
1746 if (env->regs[13] & 7)
1747 goto badframe;
1749 frame_addr = env->regs[13];
1750 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1751 goto badframe;
1753 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1754 sigprocmask(SIG_SETMASK, &host_set, NULL);
1756 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1757 goto badframe;
1759 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1760 goto badframe;
1762 #if 0
1763 /* Send SIGTRAP if we're single-stepping */
1764 if (ptrace_cancel_bpt(current))
1765 send_sig(SIGTRAP, current, 1);
1766 #endif
1767 unlock_user_struct(frame, frame_addr, 0);
1768 return env->regs[0];
1770 badframe:
1771 unlock_user_struct(frame, frame_addr, 0);
1772 force_sig(TARGET_SIGSEGV /* , current */);
1773 return 0;
1776 static long do_rt_sigreturn_v2(CPUARMState *env)
1778 abi_ulong frame_addr;
1779 struct rt_sigframe_v2 *frame;
1782 * Since we stacked the signal on a 64-bit boundary,
1783 * then 'sp' should be word aligned here. If it's
1784 * not, then the user is trying to mess with us.
1786 if (env->regs[13] & 7)
1787 goto badframe;
1789 frame_addr = env->regs[13];
1790 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1791 goto badframe;
1793 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1794 goto badframe;
1796 unlock_user_struct(frame, frame_addr, 0);
1797 return env->regs[0];
1799 badframe:
1800 unlock_user_struct(frame, frame_addr, 0);
1801 force_sig(TARGET_SIGSEGV /* , current */);
1802 return 0;
1805 long do_rt_sigreturn(CPUARMState *env)
1807 if (get_osversion() >= 0x020612) {
1808 return do_rt_sigreturn_v2(env);
1809 } else {
1810 return do_rt_sigreturn_v1(env);
1814 #elif defined(TARGET_SPARC)
1816 #define __SUNOS_MAXWIN 31
1818 /* This is what SunOS does, so shall I. */
1819 struct target_sigcontext {
1820 abi_ulong sigc_onstack; /* state to restore */
1822 abi_ulong sigc_mask; /* sigmask to restore */
1823 abi_ulong sigc_sp; /* stack pointer */
1824 abi_ulong sigc_pc; /* program counter */
1825 abi_ulong sigc_npc; /* next program counter */
1826 abi_ulong sigc_psr; /* for condition codes etc */
1827 abi_ulong sigc_g1; /* User uses these two registers */
1828 abi_ulong sigc_o0; /* within the trampoline code. */
1830 /* Now comes information regarding the users window set
1831 * at the time of the signal.
1833 abi_ulong sigc_oswins; /* outstanding windows */
1835 /* stack ptrs for each regwin buf */
1836 char *sigc_spbuf[__SUNOS_MAXWIN];
1838 /* Windows to restore after signal */
1839 struct {
1840 abi_ulong locals[8];
1841 abi_ulong ins[8];
1842 } sigc_wbuf[__SUNOS_MAXWIN];
1844 /* A Sparc stack frame */
1845 struct sparc_stackf {
1846 abi_ulong locals[8];
1847 abi_ulong ins[8];
1848 /* It's simpler to treat fp and callers_pc as elements of ins[]
1849 * since we never need to access them ourselves.
1851 char *structptr;
1852 abi_ulong xargs[6];
1853 abi_ulong xxargs[1];
1856 typedef struct {
1857 struct {
1858 abi_ulong psr;
1859 abi_ulong pc;
1860 abi_ulong npc;
1861 abi_ulong y;
1862 abi_ulong u_regs[16]; /* globals and ins */
1863 } si_regs;
1864 int si_mask;
1865 } __siginfo_t;
1867 typedef struct {
1868 abi_ulong si_float_regs[32];
1869 unsigned long si_fsr;
1870 unsigned long si_fpqdepth;
1871 struct {
1872 unsigned long *insn_addr;
1873 unsigned long insn;
1874 } si_fpqueue [16];
1875 } qemu_siginfo_fpu_t;
1878 struct target_signal_frame {
1879 struct sparc_stackf ss;
1880 __siginfo_t info;
1881 abi_ulong fpu_save;
1882 abi_ulong insns[2] __attribute__ ((aligned (8)));
1883 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1884 abi_ulong extra_size; /* Should be 0 */
1885 qemu_siginfo_fpu_t fpu_state;
1887 struct target_rt_signal_frame {
1888 struct sparc_stackf ss;
1889 siginfo_t info;
1890 abi_ulong regs[20];
1891 sigset_t mask;
1892 abi_ulong fpu_save;
1893 unsigned int insns[2];
1894 stack_t stack;
1895 unsigned int extra_size; /* Should be 0 */
1896 qemu_siginfo_fpu_t fpu_state;
1899 #define UREG_O0 16
1900 #define UREG_O6 22
1901 #define UREG_I0 0
1902 #define UREG_I1 1
1903 #define UREG_I2 2
1904 #define UREG_I3 3
1905 #define UREG_I4 4
1906 #define UREG_I5 5
1907 #define UREG_I6 6
1908 #define UREG_I7 7
1909 #define UREG_L0 8
1910 #define UREG_FP UREG_I6
1911 #define UREG_SP UREG_O6
1913 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1914 CPUSPARCState *env,
1915 unsigned long framesize)
1917 abi_ulong sp;
1919 sp = env->regwptr[UREG_FP];
1921 /* This is the X/Open sanctioned signal stack switching. */
1922 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1923 if (!on_sig_stack(sp)
1924 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1925 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1927 return sp - framesize;
1930 static int
1931 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
1933 int err = 0, i;
1935 err |= __put_user(env->psr, &si->si_regs.psr);
1936 err |= __put_user(env->pc, &si->si_regs.pc);
1937 err |= __put_user(env->npc, &si->si_regs.npc);
1938 err |= __put_user(env->y, &si->si_regs.y);
1939 for (i=0; i < 8; i++) {
1940 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1942 for (i=0; i < 8; i++) {
1943 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1945 err |= __put_user(mask, &si->si_mask);
1946 return err;
1949 #if 0
1950 static int
1951 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1952 CPUSPARCState *env, unsigned long mask)
1954 int err = 0;
1956 err |= __put_user(mask, &sc->sigc_mask);
1957 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1958 err |= __put_user(env->pc, &sc->sigc_pc);
1959 err |= __put_user(env->npc, &sc->sigc_npc);
1960 err |= __put_user(env->psr, &sc->sigc_psr);
1961 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1962 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1964 return err;
1966 #endif
1967 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1969 static void setup_frame(int sig, struct target_sigaction *ka,
1970 target_sigset_t *set, CPUSPARCState *env)
1972 abi_ulong sf_addr;
1973 struct target_signal_frame *sf;
1974 int sigframe_size, err, i;
1976 /* 1. Make sure everything is clean */
1977 //synchronize_user_stack();
1979 sigframe_size = NF_ALIGNEDSZ;
1980 sf_addr = get_sigframe(ka, env, sigframe_size);
1982 sf = lock_user(VERIFY_WRITE, sf_addr,
1983 sizeof(struct target_signal_frame), 0);
1984 if (!sf)
1985 goto sigsegv;
1987 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1988 #if 0
1989 if (invalid_frame_pointer(sf, sigframe_size))
1990 goto sigill_and_return;
1991 #endif
1992 /* 2. Save the current process state */
1993 err = setup___siginfo(&sf->info, env, set->sig[0]);
1994 err |= __put_user(0, &sf->extra_size);
1996 //err |= save_fpu_state(regs, &sf->fpu_state);
1997 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1999 err |= __put_user(set->sig[0], &sf->info.si_mask);
2000 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2001 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
2004 for (i = 0; i < 8; i++) {
2005 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2007 for (i = 0; i < 8; i++) {
2008 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2010 if (err)
2011 goto sigsegv;
2013 /* 3. signal handler back-trampoline and parameters */
2014 env->regwptr[UREG_FP] = sf_addr;
2015 env->regwptr[UREG_I0] = sig;
2016 env->regwptr[UREG_I1] = sf_addr +
2017 offsetof(struct target_signal_frame, info);
2018 env->regwptr[UREG_I2] = sf_addr +
2019 offsetof(struct target_signal_frame, info);
2021 /* 4. signal handler */
2022 env->pc = ka->_sa_handler;
2023 env->npc = (env->pc + 4);
2024 /* 5. return to kernel instructions */
2025 if (ka->sa_restorer)
2026 env->regwptr[UREG_I7] = ka->sa_restorer;
2027 else {
2028 uint32_t val32;
2030 env->regwptr[UREG_I7] = sf_addr +
2031 offsetof(struct target_signal_frame, insns) - 2 * 4;
2033 /* mov __NR_sigreturn, %g1 */
2034 val32 = 0x821020d8;
2035 err |= __put_user(val32, &sf->insns[0]);
2037 /* t 0x10 */
2038 val32 = 0x91d02010;
2039 err |= __put_user(val32, &sf->insns[1]);
2040 if (err)
2041 goto sigsegv;
2043 /* Flush instruction space. */
2044 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2045 // tb_flush(env);
2047 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2048 return;
2049 #if 0
2050 sigill_and_return:
2051 force_sig(TARGET_SIGILL);
2052 #endif
2053 sigsegv:
2054 //fprintf(stderr, "force_sig\n");
2055 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2056 force_sig(TARGET_SIGSEGV);
2058 static inline int
2059 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
2061 int err;
2062 #if 0
2063 #ifdef CONFIG_SMP
2064 if (current->flags & PF_USEDFPU)
2065 regs->psr &= ~PSR_EF;
2066 #else
2067 if (current == last_task_used_math) {
2068 last_task_used_math = 0;
2069 regs->psr &= ~PSR_EF;
2071 #endif
2072 current->used_math = 1;
2073 current->flags &= ~PF_USEDFPU;
2074 #endif
2075 #if 0
2076 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2077 return -EFAULT;
2078 #endif
2080 /* XXX: incorrect */
2081 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2082 (sizeof(abi_ulong) * 32));
2083 err |= __get_user(env->fsr, &fpu->si_fsr);
2084 #if 0
2085 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2086 if (current->thread.fpqdepth != 0)
2087 err |= __copy_from_user(&current->thread.fpqueue[0],
2088 &fpu->si_fpqueue[0],
2089 ((sizeof(unsigned long) +
2090 (sizeof(unsigned long *)))*16));
2091 #endif
2092 return err;
2096 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2097 target_siginfo_t *info,
2098 target_sigset_t *set, CPUSPARCState *env)
2100 fprintf(stderr, "setup_rt_frame: not implemented\n");
2103 long do_sigreturn(CPUSPARCState *env)
2105 abi_ulong sf_addr;
2106 struct target_signal_frame *sf;
2107 uint32_t up_psr, pc, npc;
2108 target_sigset_t set;
2109 sigset_t host_set;
2110 int err, i;
2112 sf_addr = env->regwptr[UREG_FP];
2113 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2114 goto segv_and_exit;
2115 #if 0
2116 fprintf(stderr, "sigreturn\n");
2117 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2118 #endif
2119 //cpu_dump_state(env, stderr, fprintf, 0);
2121 /* 1. Make sure we are not getting garbage from the user */
2123 if (sf_addr & 3)
2124 goto segv_and_exit;
2126 err = __get_user(pc, &sf->info.si_regs.pc);
2127 err |= __get_user(npc, &sf->info.si_regs.npc);
2129 if ((pc | npc) & 3)
2130 goto segv_and_exit;
2132 /* 2. Restore the state */
2133 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2135 /* User can only change condition codes and FPU enabling in %psr. */
2136 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2137 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2139 env->pc = pc;
2140 env->npc = npc;
2141 err |= __get_user(env->y, &sf->info.si_regs.y);
2142 for (i=0; i < 8; i++) {
2143 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2145 for (i=0; i < 8; i++) {
2146 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2149 /* FIXME: implement FPU save/restore:
2150 * __get_user(fpu_save, &sf->fpu_save);
2151 * if (fpu_save)
2152 * err |= restore_fpu_state(env, fpu_save);
2155 /* This is pretty much atomic, no amount locking would prevent
2156 * the races which exist anyways.
2158 err |= __get_user(set.sig[0], &sf->info.si_mask);
2159 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2160 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2163 target_to_host_sigset_internal(&host_set, &set);
2164 sigprocmask(SIG_SETMASK, &host_set, NULL);
2166 if (err)
2167 goto segv_and_exit;
2168 unlock_user_struct(sf, sf_addr, 0);
2169 return env->regwptr[0];
2171 segv_and_exit:
2172 unlock_user_struct(sf, sf_addr, 0);
2173 force_sig(TARGET_SIGSEGV);
2176 long do_rt_sigreturn(CPUSPARCState *env)
2178 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2179 return -TARGET_ENOSYS;
2182 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2183 #define MC_TSTATE 0
2184 #define MC_PC 1
2185 #define MC_NPC 2
2186 #define MC_Y 3
2187 #define MC_G1 4
2188 #define MC_G2 5
2189 #define MC_G3 6
2190 #define MC_G4 7
2191 #define MC_G5 8
2192 #define MC_G6 9
2193 #define MC_G7 10
2194 #define MC_O0 11
2195 #define MC_O1 12
2196 #define MC_O2 13
2197 #define MC_O3 14
2198 #define MC_O4 15
2199 #define MC_O5 16
2200 #define MC_O6 17
2201 #define MC_O7 18
2202 #define MC_NGREG 19
2204 typedef abi_ulong target_mc_greg_t;
2205 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2207 struct target_mc_fq {
2208 abi_ulong *mcfq_addr;
2209 uint32_t mcfq_insn;
2212 struct target_mc_fpu {
2213 union {
2214 uint32_t sregs[32];
2215 uint64_t dregs[32];
2216 //uint128_t qregs[16];
2217 } mcfpu_fregs;
2218 abi_ulong mcfpu_fsr;
2219 abi_ulong mcfpu_fprs;
2220 abi_ulong mcfpu_gsr;
2221 struct target_mc_fq *mcfpu_fq;
2222 unsigned char mcfpu_qcnt;
2223 unsigned char mcfpu_qentsz;
2224 unsigned char mcfpu_enab;
2226 typedef struct target_mc_fpu target_mc_fpu_t;
2228 typedef struct {
2229 target_mc_gregset_t mc_gregs;
2230 target_mc_greg_t mc_fp;
2231 target_mc_greg_t mc_i7;
2232 target_mc_fpu_t mc_fpregs;
2233 } target_mcontext_t;
2235 struct target_ucontext {
2236 struct target_ucontext *tuc_link;
2237 abi_ulong tuc_flags;
2238 target_sigset_t tuc_sigmask;
2239 target_mcontext_t tuc_mcontext;
2242 /* A V9 register window */
2243 struct target_reg_window {
2244 abi_ulong locals[8];
2245 abi_ulong ins[8];
2248 #define TARGET_STACK_BIAS 2047
2250 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2251 void sparc64_set_context(CPUSPARCState *env)
2253 abi_ulong ucp_addr;
2254 struct target_ucontext *ucp;
2255 target_mc_gregset_t *grp;
2256 abi_ulong pc, npc, tstate;
2257 abi_ulong fp, i7, w_addr;
2258 int err;
2259 unsigned int i;
2261 ucp_addr = env->regwptr[UREG_I0];
2262 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2263 goto do_sigsegv;
2264 grp = &ucp->tuc_mcontext.mc_gregs;
2265 err = __get_user(pc, &((*grp)[MC_PC]));
2266 err |= __get_user(npc, &((*grp)[MC_NPC]));
2267 if (err || ((pc | npc) & 3))
2268 goto do_sigsegv;
2269 if (env->regwptr[UREG_I1]) {
2270 target_sigset_t target_set;
2271 sigset_t set;
2273 if (TARGET_NSIG_WORDS == 1) {
2274 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2275 goto do_sigsegv;
2276 } else {
2277 abi_ulong *src, *dst;
2278 src = ucp->tuc_sigmask.sig;
2279 dst = target_set.sig;
2280 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2281 i++, dst++, src++)
2282 err |= __get_user(*dst, src);
2283 if (err)
2284 goto do_sigsegv;
2286 target_to_host_sigset_internal(&set, &target_set);
2287 sigprocmask(SIG_SETMASK, &set, NULL);
2289 env->pc = pc;
2290 env->npc = npc;
2291 err |= __get_user(env->y, &((*grp)[MC_Y]));
2292 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2293 env->asi = (tstate >> 24) & 0xff;
2294 cpu_put_ccr(env, tstate >> 32);
2295 cpu_put_cwp64(env, tstate & 0x1f);
2296 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2297 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2298 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2299 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2300 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2301 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2302 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2303 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2304 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2305 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2306 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2307 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2308 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2309 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2310 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2312 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2313 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2315 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2316 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2317 abi_ulong) != 0)
2318 goto do_sigsegv;
2319 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2320 abi_ulong) != 0)
2321 goto do_sigsegv;
2322 /* FIXME this does not match how the kernel handles the FPU in
2323 * its sparc64_set_context implementation. In particular the FPU
2324 * is only restored if fenab is non-zero in:
2325 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2327 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2329 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2330 for (i = 0; i < 64; i++, src++) {
2331 if (i & 1) {
2332 err |= __get_user(env->fpr[i/2].l.lower, src);
2333 } else {
2334 err |= __get_user(env->fpr[i/2].l.upper, src);
2338 err |= __get_user(env->fsr,
2339 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2340 err |= __get_user(env->gsr,
2341 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2342 if (err)
2343 goto do_sigsegv;
2344 unlock_user_struct(ucp, ucp_addr, 0);
2345 return;
2346 do_sigsegv:
2347 unlock_user_struct(ucp, ucp_addr, 0);
2348 force_sig(TARGET_SIGSEGV);
2351 void sparc64_get_context(CPUSPARCState *env)
2353 abi_ulong ucp_addr;
2354 struct target_ucontext *ucp;
2355 target_mc_gregset_t *grp;
2356 target_mcontext_t *mcp;
2357 abi_ulong fp, i7, w_addr;
2358 int err;
2359 unsigned int i;
2360 target_sigset_t target_set;
2361 sigset_t set;
2363 ucp_addr = env->regwptr[UREG_I0];
2364 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2365 goto do_sigsegv;
2367 mcp = &ucp->tuc_mcontext;
2368 grp = &mcp->mc_gregs;
2370 /* Skip over the trap instruction, first. */
2371 env->pc = env->npc;
2372 env->npc += 4;
2374 err = 0;
2376 sigprocmask(0, NULL, &set);
2377 host_to_target_sigset_internal(&target_set, &set);
2378 if (TARGET_NSIG_WORDS == 1) {
2379 err |= __put_user(target_set.sig[0],
2380 (abi_ulong *)&ucp->tuc_sigmask);
2381 } else {
2382 abi_ulong *src, *dst;
2383 src = target_set.sig;
2384 dst = ucp->tuc_sigmask.sig;
2385 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2386 i++, dst++, src++)
2387 err |= __put_user(*src, dst);
2388 if (err)
2389 goto do_sigsegv;
2392 /* XXX: tstate must be saved properly */
2393 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2394 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2395 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2396 err |= __put_user(env->y, &((*grp)[MC_Y]));
2397 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2398 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2399 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2400 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2401 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2402 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2403 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2404 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2405 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2406 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2407 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2408 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2409 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2410 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2411 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2413 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2414 fp = i7 = 0;
2415 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2416 abi_ulong) != 0)
2417 goto do_sigsegv;
2418 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2419 abi_ulong) != 0)
2420 goto do_sigsegv;
2421 err |= __put_user(fp, &(mcp->mc_fp));
2422 err |= __put_user(i7, &(mcp->mc_i7));
2425 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2426 for (i = 0; i < 64; i++, dst++) {
2427 if (i & 1) {
2428 err |= __put_user(env->fpr[i/2].l.lower, dst);
2429 } else {
2430 err |= __put_user(env->fpr[i/2].l.upper, dst);
2434 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2435 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2436 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2438 if (err)
2439 goto do_sigsegv;
2440 unlock_user_struct(ucp, ucp_addr, 1);
2441 return;
2442 do_sigsegv:
2443 unlock_user_struct(ucp, ucp_addr, 1);
2444 force_sig(TARGET_SIGSEGV);
2446 #endif
2447 #elif defined(TARGET_ABI_MIPSN64)
2449 # warning signal handling not implemented
2451 static void setup_frame(int sig, struct target_sigaction *ka,
2452 target_sigset_t *set, CPUMIPSState *env)
2454 fprintf(stderr, "setup_frame: not implemented\n");
2457 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2458 target_siginfo_t *info,
2459 target_sigset_t *set, CPUMIPSState *env)
2461 fprintf(stderr, "setup_rt_frame: not implemented\n");
2464 long do_sigreturn(CPUMIPSState *env)
2466 fprintf(stderr, "do_sigreturn: not implemented\n");
2467 return -TARGET_ENOSYS;
2470 long do_rt_sigreturn(CPUMIPSState *env)
2472 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2473 return -TARGET_ENOSYS;
2476 #elif defined(TARGET_ABI_MIPSN32)
2478 # warning signal handling not implemented
2480 static void setup_frame(int sig, struct target_sigaction *ka,
2481 target_sigset_t *set, CPUMIPSState *env)
2483 fprintf(stderr, "setup_frame: not implemented\n");
2486 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2487 target_siginfo_t *info,
2488 target_sigset_t *set, CPUMIPSState *env)
2490 fprintf(stderr, "setup_rt_frame: not implemented\n");
2493 long do_sigreturn(CPUMIPSState *env)
2495 fprintf(stderr, "do_sigreturn: not implemented\n");
2496 return -TARGET_ENOSYS;
2499 long do_rt_sigreturn(CPUMIPSState *env)
2501 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2502 return -TARGET_ENOSYS;
2505 #elif defined(TARGET_ABI_MIPSO32)
2507 struct target_sigcontext {
2508 uint32_t sc_regmask; /* Unused */
2509 uint32_t sc_status;
2510 uint64_t sc_pc;
2511 uint64_t sc_regs[32];
2512 uint64_t sc_fpregs[32];
2513 uint32_t sc_ownedfp; /* Unused */
2514 uint32_t sc_fpc_csr;
2515 uint32_t sc_fpc_eir; /* Unused */
2516 uint32_t sc_used_math;
2517 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2518 uint32_t pad0;
2519 uint64_t sc_mdhi;
2520 uint64_t sc_mdlo;
2521 target_ulong sc_hi1; /* Was sc_cause */
2522 target_ulong sc_lo1; /* Was sc_badvaddr */
2523 target_ulong sc_hi2; /* Was sc_sigset[4] */
2524 target_ulong sc_lo2;
2525 target_ulong sc_hi3;
2526 target_ulong sc_lo3;
2529 struct sigframe {
2530 uint32_t sf_ass[4]; /* argument save space for o32 */
2531 uint32_t sf_code[2]; /* signal trampoline */
2532 struct target_sigcontext sf_sc;
2533 target_sigset_t sf_mask;
2536 struct target_ucontext {
2537 target_ulong tuc_flags;
2538 target_ulong tuc_link;
2539 target_stack_t tuc_stack;
2540 target_ulong pad0;
2541 struct target_sigcontext tuc_mcontext;
2542 target_sigset_t tuc_sigmask;
2545 struct target_rt_sigframe {
2546 uint32_t rs_ass[4]; /* argument save space for o32 */
2547 uint32_t rs_code[2]; /* signal trampoline */
2548 struct target_siginfo rs_info;
2549 struct target_ucontext rs_uc;
2552 /* Install trampoline to jump back from signal handler */
2553 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2555 int err;
2558 * Set up the return code ...
2560 * li v0, __NR__foo_sigreturn
2561 * syscall
2564 err = __put_user(0x24020000 + syscall, tramp + 0);
2565 err |= __put_user(0x0000000c , tramp + 1);
2566 /* flush_cache_sigtramp((unsigned long) tramp); */
2567 return err;
2570 static inline int
2571 setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2573 int err = 0;
2575 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2577 #define save_gp_reg(i) do { \
2578 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2579 } while(0)
2580 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2581 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2582 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2583 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2584 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2585 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2586 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2587 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2588 save_gp_reg(31);
2589 #undef save_gp_reg
2591 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2592 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2594 /* Not used yet, but might be useful if we ever have DSP suppport */
2595 #if 0
2596 if (cpu_has_dsp) {
2597 err |= __put_user(mfhi1(), &sc->sc_hi1);
2598 err |= __put_user(mflo1(), &sc->sc_lo1);
2599 err |= __put_user(mfhi2(), &sc->sc_hi2);
2600 err |= __put_user(mflo2(), &sc->sc_lo2);
2601 err |= __put_user(mfhi3(), &sc->sc_hi3);
2602 err |= __put_user(mflo3(), &sc->sc_lo3);
2603 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2605 /* same with 64 bit */
2606 #ifdef CONFIG_64BIT
2607 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2608 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2609 if (cpu_has_dsp) {
2610 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2611 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2612 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2613 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2614 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2615 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2616 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2618 #endif
2619 #endif
2621 #if 0
2622 err |= __put_user(!!used_math(), &sc->sc_used_math);
2624 if (!used_math())
2625 goto out;
2628 * Save FPU state to signal context. Signal handler will "inherit"
2629 * current FPU state.
2631 preempt_disable();
2633 if (!is_fpu_owner()) {
2634 own_fpu();
2635 restore_fp(current);
2637 err |= save_fp_context(sc);
2639 preempt_enable();
2640 out:
2641 #endif
2642 return err;
2645 static inline int
2646 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2648 int err = 0;
2650 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2652 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2653 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2655 #define restore_gp_reg(i) do { \
2656 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2657 } while(0)
2658 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2659 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2660 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2661 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2662 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2663 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2664 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2665 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2666 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2667 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2668 restore_gp_reg(31);
2669 #undef restore_gp_reg
2671 #if 0
2672 if (cpu_has_dsp) {
2673 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2674 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2675 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2676 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2677 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2678 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2679 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2681 #ifdef CONFIG_64BIT
2682 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2683 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2684 if (cpu_has_dsp) {
2685 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2686 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2687 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2688 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2689 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2690 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2691 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2693 #endif
2695 err |= __get_user(used_math, &sc->sc_used_math);
2696 conditional_used_math(used_math);
2698 preempt_disable();
2700 if (used_math()) {
2701 /* restore fpu context if we have used it before */
2702 own_fpu();
2703 err |= restore_fp_context(sc);
2704 } else {
2705 /* signal handler may have used FPU. Give it up. */
2706 lose_fpu();
2709 preempt_enable();
2710 #endif
2711 return err;
2714 * Determine which stack to use..
2716 static inline abi_ulong
2717 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2719 unsigned long sp;
2721 /* Default to using normal stack */
2722 sp = regs->active_tc.gpr[29];
2725 * FPU emulator may have its own trampoline active just
2726 * above the user stack, 16-bytes before the next lowest
2727 * 16 byte boundary. Try to avoid trashing it.
2729 sp -= 32;
2731 /* This is the X/Open sanctioned signal stack switching. */
2732 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2733 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2736 return (sp - frame_size) & ~7;
2739 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2740 static void setup_frame(int sig, struct target_sigaction * ka,
2741 target_sigset_t *set, CPUMIPSState *regs)
2743 struct sigframe *frame;
2744 abi_ulong frame_addr;
2745 int i;
2747 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2748 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2749 goto give_sigsegv;
2751 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2753 if(setup_sigcontext(regs, &frame->sf_sc))
2754 goto give_sigsegv;
2756 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2757 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2758 goto give_sigsegv;
2762 * Arguments to signal handler:
2764 * a0 = signal number
2765 * a1 = 0 (should be cause)
2766 * a2 = pointer to struct sigcontext
2768 * $25 and PC point to the signal handler, $29 points to the
2769 * struct sigframe.
2771 regs->active_tc.gpr[ 4] = sig;
2772 regs->active_tc.gpr[ 5] = 0;
2773 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2774 regs->active_tc.gpr[29] = frame_addr;
2775 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2776 /* The original kernel code sets CP0_EPC to the handler
2777 * since it returns to userland using eret
2778 * we cannot do this here, and we must set PC directly */
2779 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2780 unlock_user_struct(frame, frame_addr, 1);
2781 return;
2783 give_sigsegv:
2784 unlock_user_struct(frame, frame_addr, 1);
2785 force_sig(TARGET_SIGSEGV/*, current*/);
2788 long do_sigreturn(CPUMIPSState *regs)
2790 struct sigframe *frame;
2791 abi_ulong frame_addr;
2792 sigset_t blocked;
2793 target_sigset_t target_set;
2794 int i;
2796 #if defined(DEBUG_SIGNAL)
2797 fprintf(stderr, "do_sigreturn\n");
2798 #endif
2799 frame_addr = regs->active_tc.gpr[29];
2800 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2801 goto badframe;
2803 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2804 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2805 goto badframe;
2808 target_to_host_sigset_internal(&blocked, &target_set);
2809 sigprocmask(SIG_SETMASK, &blocked, NULL);
2811 if (restore_sigcontext(regs, &frame->sf_sc))
2812 goto badframe;
2814 #if 0
2816 * Don't let your children do this ...
2818 __asm__ __volatile__(
2819 "move\t$29, %0\n\t"
2820 "j\tsyscall_exit"
2821 :/* no outputs */
2822 :"r" (&regs));
2823 /* Unreached */
2824 #endif
2826 regs->active_tc.PC = regs->CP0_EPC;
2827 /* I am not sure this is right, but it seems to work
2828 * maybe a problem with nested signals ? */
2829 regs->CP0_EPC = 0;
2830 return -TARGET_QEMU_ESIGRETURN;
2832 badframe:
2833 force_sig(TARGET_SIGSEGV/*, current*/);
2834 return 0;
2837 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2838 target_siginfo_t *info,
2839 target_sigset_t *set, CPUMIPSState *env)
2841 struct target_rt_sigframe *frame;
2842 abi_ulong frame_addr;
2843 int i;
2845 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2846 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2847 goto give_sigsegv;
2849 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2851 copy_siginfo_to_user(&frame->rs_info, info);
2853 __put_user(0, &frame->rs_uc.tuc_flags);
2854 __put_user(0, &frame->rs_uc.tuc_link);
2855 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2856 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2857 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2858 &frame->rs_uc.tuc_stack.ss_flags);
2860 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2862 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2863 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2867 * Arguments to signal handler:
2869 * a0 = signal number
2870 * a1 = pointer to siginfo_t
2871 * a2 = pointer to struct ucontext
2873 * $25 and PC point to the signal handler, $29 points to the
2874 * struct sigframe.
2876 env->active_tc.gpr[ 4] = sig;
2877 env->active_tc.gpr[ 5] = frame_addr
2878 + offsetof(struct target_rt_sigframe, rs_info);
2879 env->active_tc.gpr[ 6] = frame_addr
2880 + offsetof(struct target_rt_sigframe, rs_uc);
2881 env->active_tc.gpr[29] = frame_addr;
2882 env->active_tc.gpr[31] = frame_addr
2883 + offsetof(struct target_rt_sigframe, rs_code);
2884 /* The original kernel code sets CP0_EPC to the handler
2885 * since it returns to userland using eret
2886 * we cannot do this here, and we must set PC directly */
2887 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2888 unlock_user_struct(frame, frame_addr, 1);
2889 return;
2891 give_sigsegv:
2892 unlock_user_struct(frame, frame_addr, 1);
2893 force_sig(TARGET_SIGSEGV/*, current*/);
2896 long do_rt_sigreturn(CPUMIPSState *env)
2898 struct target_rt_sigframe *frame;
2899 abi_ulong frame_addr;
2900 sigset_t blocked;
2902 #if defined(DEBUG_SIGNAL)
2903 fprintf(stderr, "do_rt_sigreturn\n");
2904 #endif
2905 frame_addr = env->active_tc.gpr[29];
2906 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2907 goto badframe;
2909 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2910 sigprocmask(SIG_SETMASK, &blocked, NULL);
2912 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2913 goto badframe;
2915 if (do_sigaltstack(frame_addr +
2916 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2917 0, get_sp_from_cpustate(env)) == -EFAULT)
2918 goto badframe;
2920 env->active_tc.PC = env->CP0_EPC;
2921 /* I am not sure this is right, but it seems to work
2922 * maybe a problem with nested signals ? */
2923 env->CP0_EPC = 0;
2924 return -TARGET_QEMU_ESIGRETURN;
2926 badframe:
2927 force_sig(TARGET_SIGSEGV/*, current*/);
2928 return 0;
2931 #elif defined(TARGET_SH4)
2934 * code and data structures from linux kernel:
2935 * include/asm-sh/sigcontext.h
2936 * arch/sh/kernel/signal.c
2939 struct target_sigcontext {
2940 target_ulong oldmask;
2942 /* CPU registers */
2943 target_ulong sc_gregs[16];
2944 target_ulong sc_pc;
2945 target_ulong sc_pr;
2946 target_ulong sc_sr;
2947 target_ulong sc_gbr;
2948 target_ulong sc_mach;
2949 target_ulong sc_macl;
2951 /* FPU registers */
2952 target_ulong sc_fpregs[16];
2953 target_ulong sc_xfpregs[16];
2954 unsigned int sc_fpscr;
2955 unsigned int sc_fpul;
2956 unsigned int sc_ownedfp;
2959 struct target_sigframe
2961 struct target_sigcontext sc;
2962 target_ulong extramask[TARGET_NSIG_WORDS-1];
2963 uint16_t retcode[3];
2967 struct target_ucontext {
2968 target_ulong tuc_flags;
2969 struct target_ucontext *tuc_link;
2970 target_stack_t tuc_stack;
2971 struct target_sigcontext tuc_mcontext;
2972 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2975 struct target_rt_sigframe
2977 struct target_siginfo info;
2978 struct target_ucontext uc;
2979 uint16_t retcode[3];
2983 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2984 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2986 static abi_ulong get_sigframe(struct target_sigaction *ka,
2987 unsigned long sp, size_t frame_size)
2989 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2990 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2993 return (sp - frame_size) & -8ul;
2996 static int setup_sigcontext(struct target_sigcontext *sc,
2997 CPUSH4State *regs, unsigned long mask)
2999 int err = 0;
3000 int i;
3002 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
3003 COPY(gregs[0]); COPY(gregs[1]);
3004 COPY(gregs[2]); COPY(gregs[3]);
3005 COPY(gregs[4]); COPY(gregs[5]);
3006 COPY(gregs[6]); COPY(gregs[7]);
3007 COPY(gregs[8]); COPY(gregs[9]);
3008 COPY(gregs[10]); COPY(gregs[11]);
3009 COPY(gregs[12]); COPY(gregs[13]);
3010 COPY(gregs[14]); COPY(gregs[15]);
3011 COPY(gbr); COPY(mach);
3012 COPY(macl); COPY(pr);
3013 COPY(sr); COPY(pc);
3014 #undef COPY
3016 for (i=0; i<16; i++) {
3017 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3019 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
3020 err |= __put_user(regs->fpul, &sc->sc_fpul);
3022 /* non-iBCS2 extensions.. */
3023 err |= __put_user(mask, &sc->oldmask);
3025 return err;
3028 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
3029 target_ulong *r0_p)
3031 unsigned int err = 0;
3032 int i;
3034 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3035 COPY(gregs[1]);
3036 COPY(gregs[2]); COPY(gregs[3]);
3037 COPY(gregs[4]); COPY(gregs[5]);
3038 COPY(gregs[6]); COPY(gregs[7]);
3039 COPY(gregs[8]); COPY(gregs[9]);
3040 COPY(gregs[10]); COPY(gregs[11]);
3041 COPY(gregs[12]); COPY(gregs[13]);
3042 COPY(gregs[14]); COPY(gregs[15]);
3043 COPY(gbr); COPY(mach);
3044 COPY(macl); COPY(pr);
3045 COPY(sr); COPY(pc);
3046 #undef COPY
3048 for (i=0; i<16; i++) {
3049 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3051 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3052 err |= __get_user(regs->fpul, &sc->sc_fpul);
3054 regs->tra = -1; /* disable syscall checks */
3055 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3056 return err;
3059 static void setup_frame(int sig, struct target_sigaction *ka,
3060 target_sigset_t *set, CPUSH4State *regs)
3062 struct target_sigframe *frame;
3063 abi_ulong frame_addr;
3064 int i;
3065 int err = 0;
3066 int signal;
3068 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3069 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3070 goto give_sigsegv;
3072 signal = current_exec_domain_sig(sig);
3074 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3076 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3077 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3080 /* Set up to return from userspace. If provided, use a stub
3081 already in userspace. */
3082 if (ka->sa_flags & TARGET_SA_RESTORER) {
3083 regs->pr = (unsigned long) ka->sa_restorer;
3084 } else {
3085 /* Generate return code (system call to sigreturn) */
3086 err |= __put_user(MOVW(2), &frame->retcode[0]);
3087 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3088 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3089 regs->pr = (unsigned long) frame->retcode;
3092 if (err)
3093 goto give_sigsegv;
3095 /* Set up registers for signal handler */
3096 regs->gregs[15] = frame_addr;
3097 regs->gregs[4] = signal; /* Arg for signal handler */
3098 regs->gregs[5] = 0;
3099 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3100 regs->pc = (unsigned long) ka->_sa_handler;
3102 unlock_user_struct(frame, frame_addr, 1);
3103 return;
3105 give_sigsegv:
3106 unlock_user_struct(frame, frame_addr, 1);
3107 force_sig(TARGET_SIGSEGV);
3110 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3111 target_siginfo_t *info,
3112 target_sigset_t *set, CPUSH4State *regs)
3114 struct target_rt_sigframe *frame;
3115 abi_ulong frame_addr;
3116 int i;
3117 int err = 0;
3118 int signal;
3120 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3121 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3122 goto give_sigsegv;
3124 signal = current_exec_domain_sig(sig);
3126 err |= copy_siginfo_to_user(&frame->info, info);
3128 /* Create the ucontext. */
3129 err |= __put_user(0, &frame->uc.tuc_flags);
3130 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3131 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3132 &frame->uc.tuc_stack.ss_sp);
3133 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3134 &frame->uc.tuc_stack.ss_flags);
3135 err |= __put_user(target_sigaltstack_used.ss_size,
3136 &frame->uc.tuc_stack.ss_size);
3137 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3138 regs, set->sig[0]);
3139 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3140 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3143 /* Set up to return from userspace. If provided, use a stub
3144 already in userspace. */
3145 if (ka->sa_flags & TARGET_SA_RESTORER) {
3146 regs->pr = (unsigned long) ka->sa_restorer;
3147 } else {
3148 /* Generate return code (system call to sigreturn) */
3149 err |= __put_user(MOVW(2), &frame->retcode[0]);
3150 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3151 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3152 regs->pr = (unsigned long) frame->retcode;
3155 if (err)
3156 goto give_sigsegv;
3158 /* Set up registers for signal handler */
3159 regs->gregs[15] = frame_addr;
3160 regs->gregs[4] = signal; /* Arg for signal handler */
3161 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3162 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3163 regs->pc = (unsigned long) ka->_sa_handler;
3165 unlock_user_struct(frame, frame_addr, 1);
3166 return;
3168 give_sigsegv:
3169 unlock_user_struct(frame, frame_addr, 1);
3170 force_sig(TARGET_SIGSEGV);
3173 long do_sigreturn(CPUSH4State *regs)
3175 struct target_sigframe *frame;
3176 abi_ulong frame_addr;
3177 sigset_t blocked;
3178 target_sigset_t target_set;
3179 target_ulong r0;
3180 int i;
3181 int err = 0;
3183 #if defined(DEBUG_SIGNAL)
3184 fprintf(stderr, "do_sigreturn\n");
3185 #endif
3186 frame_addr = regs->gregs[15];
3187 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3188 goto badframe;
3190 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3191 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3192 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3195 if (err)
3196 goto badframe;
3198 target_to_host_sigset_internal(&blocked, &target_set);
3199 sigprocmask(SIG_SETMASK, &blocked, NULL);
3201 if (restore_sigcontext(regs, &frame->sc, &r0))
3202 goto badframe;
3204 unlock_user_struct(frame, frame_addr, 0);
3205 return r0;
3207 badframe:
3208 unlock_user_struct(frame, frame_addr, 0);
3209 force_sig(TARGET_SIGSEGV);
3210 return 0;
3213 long do_rt_sigreturn(CPUSH4State *regs)
3215 struct target_rt_sigframe *frame;
3216 abi_ulong frame_addr;
3217 sigset_t blocked;
3218 target_ulong r0;
3220 #if defined(DEBUG_SIGNAL)
3221 fprintf(stderr, "do_rt_sigreturn\n");
3222 #endif
3223 frame_addr = regs->gregs[15];
3224 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3225 goto badframe;
3227 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3228 sigprocmask(SIG_SETMASK, &blocked, NULL);
3230 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3231 goto badframe;
3233 if (do_sigaltstack(frame_addr +
3234 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3235 0, get_sp_from_cpustate(regs)) == -EFAULT)
3236 goto badframe;
3238 unlock_user_struct(frame, frame_addr, 0);
3239 return r0;
3241 badframe:
3242 unlock_user_struct(frame, frame_addr, 0);
3243 force_sig(TARGET_SIGSEGV);
3244 return 0;
3246 #elif defined(TARGET_MICROBLAZE)
3248 struct target_sigcontext {
3249 struct target_pt_regs regs; /* needs to be first */
3250 uint32_t oldmask;
3253 struct target_stack_t {
3254 abi_ulong ss_sp;
3255 int ss_flags;
3256 unsigned int ss_size;
3259 struct target_ucontext {
3260 abi_ulong tuc_flags;
3261 abi_ulong tuc_link;
3262 struct target_stack_t tuc_stack;
3263 struct target_sigcontext tuc_mcontext;
3264 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3267 /* Signal frames. */
3268 struct target_signal_frame {
3269 struct target_ucontext uc;
3270 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3271 uint32_t tramp[2];
3274 struct rt_signal_frame {
3275 siginfo_t info;
3276 struct ucontext uc;
3277 uint32_t tramp[2];
3280 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3282 __put_user(env->regs[0], &sc->regs.r0);
3283 __put_user(env->regs[1], &sc->regs.r1);
3284 __put_user(env->regs[2], &sc->regs.r2);
3285 __put_user(env->regs[3], &sc->regs.r3);
3286 __put_user(env->regs[4], &sc->regs.r4);
3287 __put_user(env->regs[5], &sc->regs.r5);
3288 __put_user(env->regs[6], &sc->regs.r6);
3289 __put_user(env->regs[7], &sc->regs.r7);
3290 __put_user(env->regs[8], &sc->regs.r8);
3291 __put_user(env->regs[9], &sc->regs.r9);
3292 __put_user(env->regs[10], &sc->regs.r10);
3293 __put_user(env->regs[11], &sc->regs.r11);
3294 __put_user(env->regs[12], &sc->regs.r12);
3295 __put_user(env->regs[13], &sc->regs.r13);
3296 __put_user(env->regs[14], &sc->regs.r14);
3297 __put_user(env->regs[15], &sc->regs.r15);
3298 __put_user(env->regs[16], &sc->regs.r16);
3299 __put_user(env->regs[17], &sc->regs.r17);
3300 __put_user(env->regs[18], &sc->regs.r18);
3301 __put_user(env->regs[19], &sc->regs.r19);
3302 __put_user(env->regs[20], &sc->regs.r20);
3303 __put_user(env->regs[21], &sc->regs.r21);
3304 __put_user(env->regs[22], &sc->regs.r22);
3305 __put_user(env->regs[23], &sc->regs.r23);
3306 __put_user(env->regs[24], &sc->regs.r24);
3307 __put_user(env->regs[25], &sc->regs.r25);
3308 __put_user(env->regs[26], &sc->regs.r26);
3309 __put_user(env->regs[27], &sc->regs.r27);
3310 __put_user(env->regs[28], &sc->regs.r28);
3311 __put_user(env->regs[29], &sc->regs.r29);
3312 __put_user(env->regs[30], &sc->regs.r30);
3313 __put_user(env->regs[31], &sc->regs.r31);
3314 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3317 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3319 __get_user(env->regs[0], &sc->regs.r0);
3320 __get_user(env->regs[1], &sc->regs.r1);
3321 __get_user(env->regs[2], &sc->regs.r2);
3322 __get_user(env->regs[3], &sc->regs.r3);
3323 __get_user(env->regs[4], &sc->regs.r4);
3324 __get_user(env->regs[5], &sc->regs.r5);
3325 __get_user(env->regs[6], &sc->regs.r6);
3326 __get_user(env->regs[7], &sc->regs.r7);
3327 __get_user(env->regs[8], &sc->regs.r8);
3328 __get_user(env->regs[9], &sc->regs.r9);
3329 __get_user(env->regs[10], &sc->regs.r10);
3330 __get_user(env->regs[11], &sc->regs.r11);
3331 __get_user(env->regs[12], &sc->regs.r12);
3332 __get_user(env->regs[13], &sc->regs.r13);
3333 __get_user(env->regs[14], &sc->regs.r14);
3334 __get_user(env->regs[15], &sc->regs.r15);
3335 __get_user(env->regs[16], &sc->regs.r16);
3336 __get_user(env->regs[17], &sc->regs.r17);
3337 __get_user(env->regs[18], &sc->regs.r18);
3338 __get_user(env->regs[19], &sc->regs.r19);
3339 __get_user(env->regs[20], &sc->regs.r20);
3340 __get_user(env->regs[21], &sc->regs.r21);
3341 __get_user(env->regs[22], &sc->regs.r22);
3342 __get_user(env->regs[23], &sc->regs.r23);
3343 __get_user(env->regs[24], &sc->regs.r24);
3344 __get_user(env->regs[25], &sc->regs.r25);
3345 __get_user(env->regs[26], &sc->regs.r26);
3346 __get_user(env->regs[27], &sc->regs.r27);
3347 __get_user(env->regs[28], &sc->regs.r28);
3348 __get_user(env->regs[29], &sc->regs.r29);
3349 __get_user(env->regs[30], &sc->regs.r30);
3350 __get_user(env->regs[31], &sc->regs.r31);
3351 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3354 static abi_ulong get_sigframe(struct target_sigaction *ka,
3355 CPUMBState *env, int frame_size)
3357 abi_ulong sp = env->regs[1];
3359 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3360 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3362 return ((sp - frame_size) & -8UL);
3365 static void setup_frame(int sig, struct target_sigaction *ka,
3366 target_sigset_t *set, CPUMBState *env)
3368 struct target_signal_frame *frame;
3369 abi_ulong frame_addr;
3370 int err = 0;
3371 int i;
3373 frame_addr = get_sigframe(ka, env, sizeof *frame);
3374 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3375 goto badframe;
3377 /* Save the mask. */
3378 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3379 if (err)
3380 goto badframe;
3382 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3383 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3384 goto badframe;
3387 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3389 /* Set up to return from userspace. If provided, use a stub
3390 already in userspace. */
3391 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3392 if (ka->sa_flags & TARGET_SA_RESTORER) {
3393 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3394 } else {
3395 uint32_t t;
3396 /* Note, these encodings are _big endian_! */
3397 /* addi r12, r0, __NR_sigreturn */
3398 t = 0x31800000UL | TARGET_NR_sigreturn;
3399 err |= __put_user(t, frame->tramp + 0);
3400 /* brki r14, 0x8 */
3401 t = 0xb9cc0008UL;
3402 err |= __put_user(t, frame->tramp + 1);
3404 /* Return from sighandler will jump to the tramp.
3405 Negative 8 offset because return is rtsd r15, 8 */
3406 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3409 if (err)
3410 goto badframe;
3412 /* Set up registers for signal handler */
3413 env->regs[1] = frame_addr;
3414 /* Signal handler args: */
3415 env->regs[5] = sig; /* Arg 0: signum */
3416 env->regs[6] = 0;
3417 /* arg 1: sigcontext */
3418 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3420 /* Offset of 4 to handle microblaze rtid r14, 0 */
3421 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3423 unlock_user_struct(frame, frame_addr, 1);
3424 return;
3425 badframe:
3426 unlock_user_struct(frame, frame_addr, 1);
3427 force_sig(TARGET_SIGSEGV);
3430 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3431 target_siginfo_t *info,
3432 target_sigset_t *set, CPUMBState *env)
3434 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3437 long do_sigreturn(CPUMBState *env)
3439 struct target_signal_frame *frame;
3440 abi_ulong frame_addr;
3441 target_sigset_t target_set;
3442 sigset_t set;
3443 int i;
3445 frame_addr = env->regs[R_SP];
3446 /* Make sure the guest isn't playing games. */
3447 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3448 goto badframe;
3450 /* Restore blocked signals */
3451 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3452 goto badframe;
3453 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3454 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3455 goto badframe;
3457 target_to_host_sigset_internal(&set, &target_set);
3458 sigprocmask(SIG_SETMASK, &set, NULL);
3460 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3461 /* We got here through a sigreturn syscall, our path back is via an
3462 rtb insn so setup r14 for that. */
3463 env->regs[14] = env->sregs[SR_PC];
3465 unlock_user_struct(frame, frame_addr, 0);
3466 return env->regs[10];
3467 badframe:
3468 unlock_user_struct(frame, frame_addr, 0);
3469 force_sig(TARGET_SIGSEGV);
3472 long do_rt_sigreturn(CPUMBState *env)
3474 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3475 return -TARGET_ENOSYS;
3478 #elif defined(TARGET_CRIS)
3480 struct target_sigcontext {
3481 struct target_pt_regs regs; /* needs to be first */
3482 uint32_t oldmask;
3483 uint32_t usp; /* usp before stacking this gunk on it */
3486 /* Signal frames. */
3487 struct target_signal_frame {
3488 struct target_sigcontext sc;
3489 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3490 uint8_t retcode[8]; /* Trampoline code. */
3493 struct rt_signal_frame {
3494 siginfo_t *pinfo;
3495 void *puc;
3496 siginfo_t info;
3497 struct ucontext uc;
3498 uint8_t retcode[8]; /* Trampoline code. */
3501 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3503 __put_user(env->regs[0], &sc->regs.r0);
3504 __put_user(env->regs[1], &sc->regs.r1);
3505 __put_user(env->regs[2], &sc->regs.r2);
3506 __put_user(env->regs[3], &sc->regs.r3);
3507 __put_user(env->regs[4], &sc->regs.r4);
3508 __put_user(env->regs[5], &sc->regs.r5);
3509 __put_user(env->regs[6], &sc->regs.r6);
3510 __put_user(env->regs[7], &sc->regs.r7);
3511 __put_user(env->regs[8], &sc->regs.r8);
3512 __put_user(env->regs[9], &sc->regs.r9);
3513 __put_user(env->regs[10], &sc->regs.r10);
3514 __put_user(env->regs[11], &sc->regs.r11);
3515 __put_user(env->regs[12], &sc->regs.r12);
3516 __put_user(env->regs[13], &sc->regs.r13);
3517 __put_user(env->regs[14], &sc->usp);
3518 __put_user(env->regs[15], &sc->regs.acr);
3519 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3520 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3521 __put_user(env->pc, &sc->regs.erp);
3524 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3526 __get_user(env->regs[0], &sc->regs.r0);
3527 __get_user(env->regs[1], &sc->regs.r1);
3528 __get_user(env->regs[2], &sc->regs.r2);
3529 __get_user(env->regs[3], &sc->regs.r3);
3530 __get_user(env->regs[4], &sc->regs.r4);
3531 __get_user(env->regs[5], &sc->regs.r5);
3532 __get_user(env->regs[6], &sc->regs.r6);
3533 __get_user(env->regs[7], &sc->regs.r7);
3534 __get_user(env->regs[8], &sc->regs.r8);
3535 __get_user(env->regs[9], &sc->regs.r9);
3536 __get_user(env->regs[10], &sc->regs.r10);
3537 __get_user(env->regs[11], &sc->regs.r11);
3538 __get_user(env->regs[12], &sc->regs.r12);
3539 __get_user(env->regs[13], &sc->regs.r13);
3540 __get_user(env->regs[14], &sc->usp);
3541 __get_user(env->regs[15], &sc->regs.acr);
3542 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3543 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3544 __get_user(env->pc, &sc->regs.erp);
3547 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3549 abi_ulong sp;
3550 /* Align the stack downwards to 4. */
3551 sp = (env->regs[R_SP] & ~3);
3552 return sp - framesize;
3555 static void setup_frame(int sig, struct target_sigaction *ka,
3556 target_sigset_t *set, CPUCRISState *env)
3558 struct target_signal_frame *frame;
3559 abi_ulong frame_addr;
3560 int err = 0;
3561 int i;
3563 frame_addr = get_sigframe(env, sizeof *frame);
3564 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3565 goto badframe;
3568 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3569 * use this trampoline anymore but it sets it up for GDB.
3570 * In QEMU, using the trampoline simplifies things a bit so we use it.
3572 * This is movu.w __NR_sigreturn, r9; break 13;
3574 err |= __put_user(0x9c5f, frame->retcode+0);
3575 err |= __put_user(TARGET_NR_sigreturn,
3576 frame->retcode+2);
3577 err |= __put_user(0xe93d, frame->retcode+4);
3579 /* Save the mask. */
3580 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3581 if (err)
3582 goto badframe;
3584 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3585 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3586 goto badframe;
3589 setup_sigcontext(&frame->sc, env);
3591 /* Move the stack and setup the arguments for the handler. */
3592 env->regs[R_SP] = frame_addr;
3593 env->regs[10] = sig;
3594 env->pc = (unsigned long) ka->_sa_handler;
3595 /* Link SRP so the guest returns through the trampoline. */
3596 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3598 unlock_user_struct(frame, frame_addr, 1);
3599 return;
3600 badframe:
3601 unlock_user_struct(frame, frame_addr, 1);
3602 force_sig(TARGET_SIGSEGV);
3605 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3606 target_siginfo_t *info,
3607 target_sigset_t *set, CPUCRISState *env)
3609 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3612 long do_sigreturn(CPUCRISState *env)
3614 struct target_signal_frame *frame;
3615 abi_ulong frame_addr;
3616 target_sigset_t target_set;
3617 sigset_t set;
3618 int i;
3620 frame_addr = env->regs[R_SP];
3621 /* Make sure the guest isn't playing games. */
3622 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3623 goto badframe;
3625 /* Restore blocked signals */
3626 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3627 goto badframe;
3628 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3629 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3630 goto badframe;
3632 target_to_host_sigset_internal(&set, &target_set);
3633 sigprocmask(SIG_SETMASK, &set, NULL);
3635 restore_sigcontext(&frame->sc, env);
3636 unlock_user_struct(frame, frame_addr, 0);
3637 return env->regs[10];
3638 badframe:
3639 unlock_user_struct(frame, frame_addr, 0);
3640 force_sig(TARGET_SIGSEGV);
3643 long do_rt_sigreturn(CPUCRISState *env)
3645 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3646 return -TARGET_ENOSYS;
3649 #elif defined(TARGET_OPENRISC)
3651 struct target_sigcontext {
3652 struct target_pt_regs regs;
3653 abi_ulong oldmask;
3654 abi_ulong usp;
3657 struct target_ucontext {
3658 abi_ulong tuc_flags;
3659 abi_ulong tuc_link;
3660 target_stack_t tuc_stack;
3661 struct target_sigcontext tuc_mcontext;
3662 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3665 struct target_rt_sigframe {
3666 abi_ulong pinfo;
3667 uint64_t puc;
3668 struct target_siginfo info;
3669 struct target_sigcontext sc;
3670 struct target_ucontext uc;
3671 unsigned char retcode[16]; /* trampoline code */
3674 /* This is the asm-generic/ucontext.h version */
3675 #if 0
3676 static int restore_sigcontext(CPUOpenRISCState *regs,
3677 struct target_sigcontext *sc)
3679 unsigned int err = 0;
3680 unsigned long old_usp;
3682 /* Alwys make any pending restarted system call return -EINTR */
3683 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3685 /* restore the regs from &sc->regs (same as sc, since regs is first)
3686 * (sc is already checked for VERIFY_READ since the sigframe was
3687 * checked in sys_sigreturn previously)
3690 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3691 goto badframe;
3694 /* make sure the U-flag is set so user-mode cannot fool us */
3696 regs->sr &= ~SR_SM;
3698 /* restore the old USP as it was before we stacked the sc etc.
3699 * (we cannot just pop the sigcontext since we aligned the sp and
3700 * stuff after pushing it)
3703 err |= __get_user(old_usp, &sc->usp);
3704 phx_signal("old_usp 0x%lx", old_usp);
3706 __PHX__ REALLY /* ??? */
3707 wrusp(old_usp);
3708 regs->gpr[1] = old_usp;
3710 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3711 * after this completes, but we don't use that mechanism. maybe we can
3712 * use it now ?
3715 return err;
3717 badframe:
3718 return 1;
3720 #endif
3722 /* Set up a signal frame. */
3724 static int setup_sigcontext(struct target_sigcontext *sc,
3725 CPUOpenRISCState *regs,
3726 unsigned long mask)
3728 int err = 0;
3729 unsigned long usp = regs->gpr[1];
3731 /* copy the regs. they are first in sc so we can use sc directly */
3733 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3735 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3736 the signal handler. The frametype will be restored to its previous
3737 value in restore_sigcontext. */
3738 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3740 /* then some other stuff */
3741 err |= __put_user(mask, &sc->oldmask);
3742 err |= __put_user(usp, &sc->usp); return err;
3745 static inline unsigned long align_sigframe(unsigned long sp)
3747 unsigned long i;
3748 i = sp & ~3UL;
3749 return i;
3752 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3753 CPUOpenRISCState *regs,
3754 size_t frame_size)
3756 unsigned long sp = regs->gpr[1];
3757 int onsigstack = on_sig_stack(sp);
3759 /* redzone */
3760 /* This is the X/Open sanctioned signal stack switching. */
3761 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3762 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3765 sp = align_sigframe(sp - frame_size);
3768 * If we are on the alternate signal stack and would overflow it, don't.
3769 * Return an always-bogus address instead so we will die with SIGSEGV.
3772 if (onsigstack && !likely(on_sig_stack(sp))) {
3773 return -1L;
3776 return sp;
3779 static void setup_frame(int sig, struct target_sigaction *ka,
3780 target_sigset_t *set, CPUOpenRISCState *env)
3782 qemu_log("Not implement.\n");
3785 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3786 target_siginfo_t *info,
3787 target_sigset_t *set, CPUOpenRISCState *env)
3789 int err = 0;
3790 abi_ulong frame_addr;
3791 unsigned long return_ip;
3792 struct target_rt_sigframe *frame;
3793 abi_ulong info_addr, uc_addr;
3795 frame_addr = get_sigframe(ka, env, sizeof *frame);
3797 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3798 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3799 goto give_sigsegv;
3802 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3803 err |= __put_user(info_addr, &frame->pinfo);
3804 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3805 err |= __put_user(uc_addr, &frame->puc);
3807 if (ka->sa_flags & SA_SIGINFO) {
3808 err |= copy_siginfo_to_user(&frame->info, info);
3810 if (err) {
3811 goto give_sigsegv;
3814 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3815 err |= __put_user(0, &frame->uc.tuc_flags);
3816 err |= __put_user(0, &frame->uc.tuc_link);
3817 err |= __put_user(target_sigaltstack_used.ss_sp,
3818 &frame->uc.tuc_stack.ss_sp);
3819 err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3820 err |= __put_user(target_sigaltstack_used.ss_size,
3821 &frame->uc.tuc_stack.ss_size);
3822 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
3824 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3826 if (err) {
3827 goto give_sigsegv;
3830 /* trampoline - the desired return ip is the retcode itself */
3831 return_ip = (unsigned long)&frame->retcode;
3832 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3833 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
3834 err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3835 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3836 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3838 if (err) {
3839 goto give_sigsegv;
3842 /* TODO what is the current->exec_domain stuff and invmap ? */
3844 /* Set up registers for signal handler */
3845 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3846 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3847 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3848 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3849 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3851 /* actually move the usp to reflect the stacked frame */
3852 env->gpr[1] = (unsigned long)frame;
3854 return;
3856 give_sigsegv:
3857 unlock_user_struct(frame, frame_addr, 1);
3858 if (sig == TARGET_SIGSEGV) {
3859 ka->_sa_handler = TARGET_SIG_DFL;
3861 force_sig(TARGET_SIGSEGV);
3864 long do_sigreturn(CPUOpenRISCState *env)
3867 qemu_log("do_sigreturn: not implemented\n");
3868 return -TARGET_ENOSYS;
3871 long do_rt_sigreturn(CPUOpenRISCState *env)
3873 qemu_log("do_rt_sigreturn: not implemented\n");
3874 return -TARGET_ENOSYS;
3876 /* TARGET_OPENRISC */
3878 #elif defined(TARGET_S390X)
3880 #define __NUM_GPRS 16
3881 #define __NUM_FPRS 16
3882 #define __NUM_ACRS 16
3884 #define S390_SYSCALL_SIZE 2
3885 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3887 #define _SIGCONTEXT_NSIG 64
3888 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3889 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3890 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3891 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
3892 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3894 typedef struct {
3895 target_psw_t psw;
3896 target_ulong gprs[__NUM_GPRS];
3897 unsigned int acrs[__NUM_ACRS];
3898 } target_s390_regs_common;
3900 typedef struct {
3901 unsigned int fpc;
3902 double fprs[__NUM_FPRS];
3903 } target_s390_fp_regs;
3905 typedef struct {
3906 target_s390_regs_common regs;
3907 target_s390_fp_regs fpregs;
3908 } target_sigregs;
3910 struct target_sigcontext {
3911 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
3912 target_sigregs *sregs;
3915 typedef struct {
3916 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3917 struct target_sigcontext sc;
3918 target_sigregs sregs;
3919 int signo;
3920 uint8_t retcode[S390_SYSCALL_SIZE];
3921 } sigframe;
3923 struct target_ucontext {
3924 target_ulong tuc_flags;
3925 struct target_ucontext *tuc_link;
3926 target_stack_t tuc_stack;
3927 target_sigregs tuc_mcontext;
3928 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3931 typedef struct {
3932 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3933 uint8_t retcode[S390_SYSCALL_SIZE];
3934 struct target_siginfo info;
3935 struct target_ucontext uc;
3936 } rt_sigframe;
3938 static inline abi_ulong
3939 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
3941 abi_ulong sp;
3943 /* Default to using normal stack */
3944 sp = env->regs[15];
3946 /* This is the X/Open sanctioned signal stack switching. */
3947 if (ka->sa_flags & TARGET_SA_ONSTACK) {
3948 if (!sas_ss_flags(sp)) {
3949 sp = target_sigaltstack_used.ss_sp +
3950 target_sigaltstack_used.ss_size;
3954 /* This is the legacy signal stack switching. */
3955 else if (/* FIXME !user_mode(regs) */ 0 &&
3956 !(ka->sa_flags & TARGET_SA_RESTORER) &&
3957 ka->sa_restorer) {
3958 sp = (abi_ulong) ka->sa_restorer;
3961 return (sp - frame_size) & -8ul;
3964 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
3966 int i;
3967 //save_access_regs(current->thread.acrs); FIXME
3969 /* Copy a 'clean' PSW mask to the user to avoid leaking
3970 information about whether PER is currently on. */
3971 __put_user(env->psw.mask, &sregs->regs.psw.mask);
3972 __put_user(env->psw.addr, &sregs->regs.psw.addr);
3973 for (i = 0; i < 16; i++) {
3974 __put_user(env->regs[i], &sregs->regs.gprs[i]);
3976 for (i = 0; i < 16; i++) {
3977 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3980 * We have to store the fp registers to current->thread.fp_regs
3981 * to merge them with the emulated registers.
3983 //save_fp_regs(&current->thread.fp_regs); FIXME
3984 for (i = 0; i < 16; i++) {
3985 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3989 static void setup_frame(int sig, struct target_sigaction *ka,
3990 target_sigset_t *set, CPUS390XState *env)
3992 sigframe *frame;
3993 abi_ulong frame_addr;
3995 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3996 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3997 (unsigned long long)frame_addr);
3998 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3999 goto give_sigsegv;
4002 qemu_log("%s: 1\n", __FUNCTION__);
4003 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
4004 goto give_sigsegv;
4007 save_sigregs(env, &frame->sregs);
4009 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4010 (abi_ulong *)&frame->sc.sregs);
4012 /* Set up to return from userspace. If provided, use a stub
4013 already in userspace. */
4014 if (ka->sa_flags & TARGET_SA_RESTORER) {
4015 env->regs[14] = (unsigned long)
4016 ka->sa_restorer | PSW_ADDR_AMODE;
4017 } else {
4018 env->regs[14] = (unsigned long)
4019 frame->retcode | PSW_ADDR_AMODE;
4020 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4021 (uint16_t *)(frame->retcode)))
4022 goto give_sigsegv;
4025 /* Set up backchain. */
4026 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4027 goto give_sigsegv;
4030 /* Set up registers for signal handler */
4031 env->regs[15] = frame_addr;
4032 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4034 env->regs[2] = sig; //map_signal(sig);
4035 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4037 /* We forgot to include these in the sigcontext.
4038 To avoid breaking binary compatibility, they are passed as args. */
4039 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4040 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4042 /* Place signal number on stack to allow backtrace from handler. */
4043 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4044 goto give_sigsegv;
4046 unlock_user_struct(frame, frame_addr, 1);
4047 return;
4049 give_sigsegv:
4050 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4051 unlock_user_struct(frame, frame_addr, 1);
4052 force_sig(TARGET_SIGSEGV);
4055 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4056 target_siginfo_t *info,
4057 target_sigset_t *set, CPUS390XState *env)
4059 int i;
4060 rt_sigframe *frame;
4061 abi_ulong frame_addr;
4063 frame_addr = get_sigframe(ka, env, sizeof *frame);
4064 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4065 (unsigned long long)frame_addr);
4066 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4067 goto give_sigsegv;
4070 qemu_log("%s: 1\n", __FUNCTION__);
4071 if (copy_siginfo_to_user(&frame->info, info)) {
4072 goto give_sigsegv;
4075 /* Create the ucontext. */
4076 __put_user(0, &frame->uc.tuc_flags);
4077 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4078 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4079 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4080 &frame->uc.tuc_stack.ss_flags);
4081 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4082 save_sigregs(env, &frame->uc.tuc_mcontext);
4083 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4084 __put_user((abi_ulong)set->sig[i],
4085 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4088 /* Set up to return from userspace. If provided, use a stub
4089 already in userspace. */
4090 if (ka->sa_flags & TARGET_SA_RESTORER) {
4091 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4092 } else {
4093 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4094 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4095 (uint16_t *)(frame->retcode))) {
4096 goto give_sigsegv;
4100 /* Set up backchain. */
4101 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4102 goto give_sigsegv;
4105 /* Set up registers for signal handler */
4106 env->regs[15] = frame_addr;
4107 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4109 env->regs[2] = sig; //map_signal(sig);
4110 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4111 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4112 return;
4114 give_sigsegv:
4115 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4116 unlock_user_struct(frame, frame_addr, 1);
4117 force_sig(TARGET_SIGSEGV);
4120 static int
4121 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4123 int err = 0;
4124 int i;
4126 for (i = 0; i < 16; i++) {
4127 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4130 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4131 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4132 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4133 (unsigned long long)env->psw.addr);
4134 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4135 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4137 for (i = 0; i < 16; i++) {
4138 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4140 for (i = 0; i < 16; i++) {
4141 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4144 return err;
4147 long do_sigreturn(CPUS390XState *env)
4149 sigframe *frame;
4150 abi_ulong frame_addr = env->regs[15];
4151 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4152 (unsigned long long)frame_addr);
4153 target_sigset_t target_set;
4154 sigset_t set;
4156 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4157 goto badframe;
4159 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4160 goto badframe;
4163 target_to_host_sigset_internal(&set, &target_set);
4164 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4166 if (restore_sigregs(env, &frame->sregs)) {
4167 goto badframe;
4170 unlock_user_struct(frame, frame_addr, 0);
4171 return env->regs[2];
4173 badframe:
4174 unlock_user_struct(frame, frame_addr, 0);
4175 force_sig(TARGET_SIGSEGV);
4176 return 0;
4179 long do_rt_sigreturn(CPUS390XState *env)
4181 rt_sigframe *frame;
4182 abi_ulong frame_addr = env->regs[15];
4183 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4184 (unsigned long long)frame_addr);
4185 sigset_t set;
4187 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4188 goto badframe;
4190 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4192 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4194 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4195 goto badframe;
4198 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4199 get_sp_from_cpustate(env)) == -EFAULT) {
4200 goto badframe;
4202 unlock_user_struct(frame, frame_addr, 0);
4203 return env->regs[2];
4205 badframe:
4206 unlock_user_struct(frame, frame_addr, 0);
4207 force_sig(TARGET_SIGSEGV);
4208 return 0;
4211 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4213 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4214 the signal handling is different enough that we haven't implemented
4215 support for PPC64 yet. Hence the restriction above.
4217 There are various #if'd blocks for code for TARGET_PPC64. These
4218 blocks should go away so that we can successfully run 32-bit and
4219 64-bit binaries on a QEMU configured for PPC64. */
4221 /* Size of dummy stack frame allocated when calling signal handler.
4222 See arch/powerpc/include/asm/ptrace.h. */
4223 #if defined(TARGET_PPC64)
4224 #define SIGNAL_FRAMESIZE 128
4225 #else
4226 #define SIGNAL_FRAMESIZE 64
4227 #endif
4229 /* See arch/powerpc/include/asm/sigcontext.h. */
4230 struct target_sigcontext {
4231 target_ulong _unused[4];
4232 int32_t signal;
4233 #if defined(TARGET_PPC64)
4234 int32_t pad0;
4235 #endif
4236 target_ulong handler;
4237 target_ulong oldmask;
4238 target_ulong regs; /* struct pt_regs __user * */
4239 /* TODO: PPC64 includes extra bits here. */
4242 /* Indices for target_mcontext.mc_gregs, below.
4243 See arch/powerpc/include/asm/ptrace.h for details. */
4244 enum {
4245 TARGET_PT_R0 = 0,
4246 TARGET_PT_R1 = 1,
4247 TARGET_PT_R2 = 2,
4248 TARGET_PT_R3 = 3,
4249 TARGET_PT_R4 = 4,
4250 TARGET_PT_R5 = 5,
4251 TARGET_PT_R6 = 6,
4252 TARGET_PT_R7 = 7,
4253 TARGET_PT_R8 = 8,
4254 TARGET_PT_R9 = 9,
4255 TARGET_PT_R10 = 10,
4256 TARGET_PT_R11 = 11,
4257 TARGET_PT_R12 = 12,
4258 TARGET_PT_R13 = 13,
4259 TARGET_PT_R14 = 14,
4260 TARGET_PT_R15 = 15,
4261 TARGET_PT_R16 = 16,
4262 TARGET_PT_R17 = 17,
4263 TARGET_PT_R18 = 18,
4264 TARGET_PT_R19 = 19,
4265 TARGET_PT_R20 = 20,
4266 TARGET_PT_R21 = 21,
4267 TARGET_PT_R22 = 22,
4268 TARGET_PT_R23 = 23,
4269 TARGET_PT_R24 = 24,
4270 TARGET_PT_R25 = 25,
4271 TARGET_PT_R26 = 26,
4272 TARGET_PT_R27 = 27,
4273 TARGET_PT_R28 = 28,
4274 TARGET_PT_R29 = 29,
4275 TARGET_PT_R30 = 30,
4276 TARGET_PT_R31 = 31,
4277 TARGET_PT_NIP = 32,
4278 TARGET_PT_MSR = 33,
4279 TARGET_PT_ORIG_R3 = 34,
4280 TARGET_PT_CTR = 35,
4281 TARGET_PT_LNK = 36,
4282 TARGET_PT_XER = 37,
4283 TARGET_PT_CCR = 38,
4284 /* Yes, there are two registers with #39. One is 64-bit only. */
4285 TARGET_PT_MQ = 39,
4286 TARGET_PT_SOFTE = 39,
4287 TARGET_PT_TRAP = 40,
4288 TARGET_PT_DAR = 41,
4289 TARGET_PT_DSISR = 42,
4290 TARGET_PT_RESULT = 43,
4291 TARGET_PT_REGS_COUNT = 44
4294 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4295 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4296 struct target_mcontext {
4297 target_ulong mc_gregs[48];
4298 /* Includes fpscr. */
4299 uint64_t mc_fregs[33];
4300 target_ulong mc_pad[2];
4301 /* We need to handle Altivec and SPE at the same time, which no
4302 kernel needs to do. Fortunately, the kernel defines this bit to
4303 be Altivec-register-large all the time, rather than trying to
4304 twiddle it based on the specific platform. */
4305 union {
4306 /* SPE vector registers. One extra for SPEFSCR. */
4307 uint32_t spe[33];
4308 /* Altivec vector registers. The packing of VSCR and VRSAVE
4309 varies depending on whether we're PPC64 or not: PPC64 splits
4310 them apart; PPC32 stuffs them together. */
4311 #if defined(TARGET_PPC64)
4312 #define QEMU_NVRREG 34
4313 #else
4314 #define QEMU_NVRREG 33
4315 #endif
4316 ppc_avr_t altivec[QEMU_NVRREG];
4317 #undef QEMU_NVRREG
4318 } mc_vregs __attribute__((__aligned__(16)));
4321 struct target_ucontext {
4322 target_ulong tuc_flags;
4323 target_ulong tuc_link; /* struct ucontext __user * */
4324 struct target_sigaltstack tuc_stack;
4325 #if !defined(TARGET_PPC64)
4326 int32_t tuc_pad[7];
4327 target_ulong tuc_regs; /* struct mcontext __user *
4328 points to uc_mcontext field */
4329 #endif
4330 target_sigset_t tuc_sigmask;
4331 #if defined(TARGET_PPC64)
4332 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4333 struct target_sigcontext tuc_mcontext;
4334 #else
4335 int32_t tuc_maskext[30];
4336 int32_t tuc_pad2[3];
4337 struct target_mcontext tuc_mcontext;
4338 #endif
4341 /* See arch/powerpc/kernel/signal_32.c. */
4342 struct target_sigframe {
4343 struct target_sigcontext sctx;
4344 struct target_mcontext mctx;
4345 int32_t abigap[56];
4348 struct target_rt_sigframe {
4349 struct target_siginfo info;
4350 struct target_ucontext uc;
4351 int32_t abigap[56];
4354 /* We use the mc_pad field for the signal return trampoline. */
4355 #define tramp mc_pad
4357 /* See arch/powerpc/kernel/signal.c. */
4358 static target_ulong get_sigframe(struct target_sigaction *ka,
4359 CPUPPCState *env,
4360 int frame_size)
4362 target_ulong oldsp, newsp;
4364 oldsp = env->gpr[1];
4366 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4367 (sas_ss_flags(oldsp) == 0)) {
4368 oldsp = (target_sigaltstack_used.ss_sp
4369 + target_sigaltstack_used.ss_size);
4372 newsp = (oldsp - frame_size) & ~0xFUL;
4374 return newsp;
4377 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4378 int sigret)
4380 target_ulong msr = env->msr;
4381 int i;
4382 target_ulong ccr = 0;
4384 /* In general, the kernel attempts to be intelligent about what it
4385 needs to save for Altivec/FP/SPE registers. We don't care that
4386 much, so we just go ahead and save everything. */
4388 /* Save general registers. */
4389 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4390 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4391 return 1;
4394 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4395 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4396 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4397 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4398 return 1;
4400 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4401 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4403 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4404 return 1;
4406 /* Save Altivec registers if necessary. */
4407 if (env->insns_flags & PPC_ALTIVEC) {
4408 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4409 ppc_avr_t *avr = &env->avr[i];
4410 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4412 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4413 __put_user(avr->u64[1], &vreg->u64[1])) {
4414 return 1;
4417 /* Set MSR_VR in the saved MSR value to indicate that
4418 frame->mc_vregs contains valid data. */
4419 msr |= MSR_VR;
4420 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4421 &frame->mc_vregs.altivec[32].u32[3]))
4422 return 1;
4425 /* Save floating point registers. */
4426 if (env->insns_flags & PPC_FLOAT) {
4427 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4428 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4429 return 1;
4432 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4433 return 1;
4436 /* Save SPE registers. The kernel only saves the high half. */
4437 if (env->insns_flags & PPC_SPE) {
4438 #if defined(TARGET_PPC64)
4439 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4440 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4441 return 1;
4444 #else
4445 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4446 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4447 return 1;
4450 #endif
4451 /* Set MSR_SPE in the saved MSR value to indicate that
4452 frame->mc_vregs contains valid data. */
4453 msr |= MSR_SPE;
4454 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4455 return 1;
4458 /* Store MSR. */
4459 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4460 return 1;
4462 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4463 if (sigret) {
4464 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4465 __put_user(0x44000002UL, &frame->tramp[1])) {
4466 return 1;
4470 return 0;
4473 static int restore_user_regs(CPUPPCState *env,
4474 struct target_mcontext *frame, int sig)
4476 target_ulong save_r2 = 0;
4477 target_ulong msr;
4478 target_ulong ccr;
4480 int i;
4482 if (!sig) {
4483 save_r2 = env->gpr[2];
4486 /* Restore general registers. */
4487 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4488 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4489 return 1;
4492 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4493 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4494 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4495 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4496 return 1;
4497 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4498 return 1;
4500 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4501 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4504 if (!sig) {
4505 env->gpr[2] = save_r2;
4507 /* Restore MSR. */
4508 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4509 return 1;
4511 /* If doing signal return, restore the previous little-endian mode. */
4512 if (sig)
4513 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4515 /* Restore Altivec registers if necessary. */
4516 if (env->insns_flags & PPC_ALTIVEC) {
4517 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4518 ppc_avr_t *avr = &env->avr[i];
4519 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4521 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4522 __get_user(avr->u64[1], &vreg->u64[1])) {
4523 return 1;
4526 /* Set MSR_VEC in the saved MSR value to indicate that
4527 frame->mc_vregs contains valid data. */
4528 if (__get_user(env->spr[SPR_VRSAVE],
4529 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4530 return 1;
4533 /* Restore floating point registers. */
4534 if (env->insns_flags & PPC_FLOAT) {
4535 uint64_t fpscr;
4536 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4537 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4538 return 1;
4541 if (__get_user(fpscr, &frame->mc_fregs[32]))
4542 return 1;
4543 env->fpscr = (uint32_t) fpscr;
4546 /* Save SPE registers. The kernel only saves the high half. */
4547 if (env->insns_flags & PPC_SPE) {
4548 #if defined(TARGET_PPC64)
4549 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4550 uint32_t hi;
4552 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4553 return 1;
4555 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4557 #else
4558 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4559 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4560 return 1;
4563 #endif
4564 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4565 return 1;
4568 return 0;
4571 static void setup_frame(int sig, struct target_sigaction *ka,
4572 target_sigset_t *set, CPUPPCState *env)
4574 struct target_sigframe *frame;
4575 struct target_sigcontext *sc;
4576 target_ulong frame_addr, newsp;
4577 int err = 0;
4578 int signal;
4580 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4581 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4582 goto sigsegv;
4583 sc = &frame->sctx;
4585 signal = current_exec_domain_sig(sig);
4587 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
4588 err |= __put_user(set->sig[0], &sc->oldmask);
4589 #if defined(TARGET_PPC64)
4590 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4591 #else
4592 err |= __put_user(set->sig[1], &sc->_unused[3]);
4593 #endif
4594 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4595 err |= __put_user(sig, &sc->signal);
4597 /* Save user regs. */
4598 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4600 /* The kernel checks for the presence of a VDSO here. We don't
4601 emulate a vdso, so use a sigreturn system call. */
4602 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4604 /* Turn off all fp exceptions. */
4605 env->fpscr = 0;
4607 /* Create a stack frame for the caller of the handler. */
4608 newsp = frame_addr - SIGNAL_FRAMESIZE;
4609 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4611 if (err)
4612 goto sigsegv;
4614 /* Set up registers for signal handler. */
4615 env->gpr[1] = newsp;
4616 env->gpr[3] = signal;
4617 env->gpr[4] = (target_ulong) h2g(sc);
4618 env->nip = (target_ulong) ka->_sa_handler;
4619 /* Signal handlers are entered in big-endian mode. */
4620 env->msr &= ~MSR_LE;
4622 unlock_user_struct(frame, frame_addr, 1);
4623 return;
4625 sigsegv:
4626 unlock_user_struct(frame, frame_addr, 1);
4627 qemu_log("segfaulting from setup_frame\n");
4628 force_sig(TARGET_SIGSEGV);
4631 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4632 target_siginfo_t *info,
4633 target_sigset_t *set, CPUPPCState *env)
4635 struct target_rt_sigframe *rt_sf;
4636 struct target_mcontext *frame;
4637 target_ulong rt_sf_addr, newsp = 0;
4638 int i, err = 0;
4639 int signal;
4641 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4642 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4643 goto sigsegv;
4645 signal = current_exec_domain_sig(sig);
4647 err |= copy_siginfo_to_user(&rt_sf->info, info);
4649 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4650 err |= __put_user(0, &rt_sf->uc.tuc_link);
4651 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4652 &rt_sf->uc.tuc_stack.ss_sp);
4653 err |= __put_user(sas_ss_flags(env->gpr[1]),
4654 &rt_sf->uc.tuc_stack.ss_flags);
4655 err |= __put_user(target_sigaltstack_used.ss_size,
4656 &rt_sf->uc.tuc_stack.ss_size);
4657 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4658 &rt_sf->uc.tuc_regs);
4659 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4660 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4663 frame = &rt_sf->uc.tuc_mcontext;
4664 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4666 /* The kernel checks for the presence of a VDSO here. We don't
4667 emulate a vdso, so use a sigreturn system call. */
4668 env->lr = (target_ulong) h2g(frame->tramp);
4670 /* Turn off all fp exceptions. */
4671 env->fpscr = 0;
4673 /* Create a stack frame for the caller of the handler. */
4674 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4675 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4677 if (err)
4678 goto sigsegv;
4680 /* Set up registers for signal handler. */
4681 env->gpr[1] = newsp;
4682 env->gpr[3] = (target_ulong) signal;
4683 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4684 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4685 env->gpr[6] = (target_ulong) h2g(rt_sf);
4686 env->nip = (target_ulong) ka->_sa_handler;
4687 /* Signal handlers are entered in big-endian mode. */
4688 env->msr &= ~MSR_LE;
4690 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4691 return;
4693 sigsegv:
4694 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4695 qemu_log("segfaulting from setup_rt_frame\n");
4696 force_sig(TARGET_SIGSEGV);
4700 long do_sigreturn(CPUPPCState *env)
4702 struct target_sigcontext *sc = NULL;
4703 struct target_mcontext *sr = NULL;
4704 target_ulong sr_addr, sc_addr;
4705 sigset_t blocked;
4706 target_sigset_t set;
4708 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4709 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4710 goto sigsegv;
4712 #if defined(TARGET_PPC64)
4713 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4714 #else
4715 if(__get_user(set.sig[0], &sc->oldmask) ||
4716 __get_user(set.sig[1], &sc->_unused[3]))
4717 goto sigsegv;
4718 #endif
4719 target_to_host_sigset_internal(&blocked, &set);
4720 sigprocmask(SIG_SETMASK, &blocked, NULL);
4722 if (__get_user(sr_addr, &sc->regs))
4723 goto sigsegv;
4724 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4725 goto sigsegv;
4726 if (restore_user_regs(env, sr, 1))
4727 goto sigsegv;
4729 unlock_user_struct(sr, sr_addr, 1);
4730 unlock_user_struct(sc, sc_addr, 1);
4731 return -TARGET_QEMU_ESIGRETURN;
4733 sigsegv:
4734 unlock_user_struct(sr, sr_addr, 1);
4735 unlock_user_struct(sc, sc_addr, 1);
4736 qemu_log("segfaulting from do_sigreturn\n");
4737 force_sig(TARGET_SIGSEGV);
4738 return 0;
4741 /* See arch/powerpc/kernel/signal_32.c. */
4742 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4744 struct target_mcontext *mcp;
4745 target_ulong mcp_addr;
4746 sigset_t blocked;
4747 target_sigset_t set;
4749 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4750 sizeof (set)))
4751 return 1;
4753 #if defined(TARGET_PPC64)
4754 fprintf (stderr, "do_setcontext: not implemented\n");
4755 return 0;
4756 #else
4757 if (__get_user(mcp_addr, &ucp->tuc_regs))
4758 return 1;
4760 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4761 return 1;
4763 target_to_host_sigset_internal(&blocked, &set);
4764 sigprocmask(SIG_SETMASK, &blocked, NULL);
4765 if (restore_user_regs(env, mcp, sig))
4766 goto sigsegv;
4768 unlock_user_struct(mcp, mcp_addr, 1);
4769 return 0;
4771 sigsegv:
4772 unlock_user_struct(mcp, mcp_addr, 1);
4773 return 1;
4774 #endif
4777 long do_rt_sigreturn(CPUPPCState *env)
4779 struct target_rt_sigframe *rt_sf = NULL;
4780 target_ulong rt_sf_addr;
4782 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4783 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4784 goto sigsegv;
4786 if (do_setcontext(&rt_sf->uc, env, 1))
4787 goto sigsegv;
4789 do_sigaltstack(rt_sf_addr
4790 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4791 0, env->gpr[1]);
4793 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4794 return -TARGET_QEMU_ESIGRETURN;
4796 sigsegv:
4797 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4798 qemu_log("segfaulting from do_rt_sigreturn\n");
4799 force_sig(TARGET_SIGSEGV);
4800 return 0;
4803 #elif defined(TARGET_M68K)
4805 struct target_sigcontext {
4806 abi_ulong sc_mask;
4807 abi_ulong sc_usp;
4808 abi_ulong sc_d0;
4809 abi_ulong sc_d1;
4810 abi_ulong sc_a0;
4811 abi_ulong sc_a1;
4812 unsigned short sc_sr;
4813 abi_ulong sc_pc;
4816 struct target_sigframe
4818 abi_ulong pretcode;
4819 int sig;
4820 int code;
4821 abi_ulong psc;
4822 char retcode[8];
4823 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4824 struct target_sigcontext sc;
4827 typedef int target_greg_t;
4828 #define TARGET_NGREG 18
4829 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4831 typedef struct target_fpregset {
4832 int f_fpcntl[3];
4833 int f_fpregs[8*3];
4834 } target_fpregset_t;
4836 struct target_mcontext {
4837 int version;
4838 target_gregset_t gregs;
4839 target_fpregset_t fpregs;
4842 #define TARGET_MCONTEXT_VERSION 2
4844 struct target_ucontext {
4845 abi_ulong tuc_flags;
4846 abi_ulong tuc_link;
4847 target_stack_t tuc_stack;
4848 struct target_mcontext tuc_mcontext;
4849 abi_long tuc_filler[80];
4850 target_sigset_t tuc_sigmask;
4853 struct target_rt_sigframe
4855 abi_ulong pretcode;
4856 int sig;
4857 abi_ulong pinfo;
4858 abi_ulong puc;
4859 char retcode[8];
4860 struct target_siginfo info;
4861 struct target_ucontext uc;
4864 static int
4865 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4866 abi_ulong mask)
4868 int err = 0;
4870 err |= __put_user(mask, &sc->sc_mask);
4871 err |= __put_user(env->aregs[7], &sc->sc_usp);
4872 err |= __put_user(env->dregs[0], &sc->sc_d0);
4873 err |= __put_user(env->dregs[1], &sc->sc_d1);
4874 err |= __put_user(env->aregs[0], &sc->sc_a0);
4875 err |= __put_user(env->aregs[1], &sc->sc_a1);
4876 err |= __put_user(env->sr, &sc->sc_sr);
4877 err |= __put_user(env->pc, &sc->sc_pc);
4879 return err;
4882 static int
4883 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
4885 int err = 0;
4886 int temp;
4888 err |= __get_user(env->aregs[7], &sc->sc_usp);
4889 err |= __get_user(env->dregs[1], &sc->sc_d1);
4890 err |= __get_user(env->aregs[0], &sc->sc_a0);
4891 err |= __get_user(env->aregs[1], &sc->sc_a1);
4892 err |= __get_user(env->pc, &sc->sc_pc);
4893 err |= __get_user(temp, &sc->sc_sr);
4894 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4896 *pd0 = tswapl(sc->sc_d0);
4898 return err;
4902 * Determine which stack to use..
4904 static inline abi_ulong
4905 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
4906 size_t frame_size)
4908 unsigned long sp;
4910 sp = regs->aregs[7];
4912 /* This is the X/Open sanctioned signal stack switching. */
4913 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4914 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4917 return ((sp - frame_size) & -8UL);
4920 static void setup_frame(int sig, struct target_sigaction *ka,
4921 target_sigset_t *set, CPUM68KState *env)
4923 struct target_sigframe *frame;
4924 abi_ulong frame_addr;
4925 abi_ulong retcode_addr;
4926 abi_ulong sc_addr;
4927 int err = 0;
4928 int i;
4930 frame_addr = get_sigframe(ka, env, sizeof *frame);
4931 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4932 goto give_sigsegv;
4934 err |= __put_user(sig, &frame->sig);
4936 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4937 err |= __put_user(sc_addr, &frame->psc);
4939 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4940 if (err)
4941 goto give_sigsegv;
4943 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4944 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4945 goto give_sigsegv;
4948 /* Set up to return from userspace. */
4950 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4951 err |= __put_user(retcode_addr, &frame->pretcode);
4953 /* moveq #,d0; trap #0 */
4955 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4956 (long *)(frame->retcode));
4958 if (err)
4959 goto give_sigsegv;
4961 /* Set up to return from userspace */
4963 env->aregs[7] = frame_addr;
4964 env->pc = ka->_sa_handler;
4966 unlock_user_struct(frame, frame_addr, 1);
4967 return;
4969 give_sigsegv:
4970 unlock_user_struct(frame, frame_addr, 1);
4971 force_sig(TARGET_SIGSEGV);
4974 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4975 CPUM68KState *env)
4977 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4978 int err;
4980 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4981 err |= __put_user(env->dregs[0], &gregs[0]);
4982 err |= __put_user(env->dregs[1], &gregs[1]);
4983 err |= __put_user(env->dregs[2], &gregs[2]);
4984 err |= __put_user(env->dregs[3], &gregs[3]);
4985 err |= __put_user(env->dregs[4], &gregs[4]);
4986 err |= __put_user(env->dregs[5], &gregs[5]);
4987 err |= __put_user(env->dregs[6], &gregs[6]);
4988 err |= __put_user(env->dregs[7], &gregs[7]);
4989 err |= __put_user(env->aregs[0], &gregs[8]);
4990 err |= __put_user(env->aregs[1], &gregs[9]);
4991 err |= __put_user(env->aregs[2], &gregs[10]);
4992 err |= __put_user(env->aregs[3], &gregs[11]);
4993 err |= __put_user(env->aregs[4], &gregs[12]);
4994 err |= __put_user(env->aregs[5], &gregs[13]);
4995 err |= __put_user(env->aregs[6], &gregs[14]);
4996 err |= __put_user(env->aregs[7], &gregs[15]);
4997 err |= __put_user(env->pc, &gregs[16]);
4998 err |= __put_user(env->sr, &gregs[17]);
5000 return err;
5003 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5004 struct target_ucontext *uc,
5005 int *pd0)
5007 int temp;
5008 int err;
5009 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5011 err = __get_user(temp, &uc->tuc_mcontext.version);
5012 if (temp != TARGET_MCONTEXT_VERSION)
5013 goto badframe;
5015 /* restore passed registers */
5016 err |= __get_user(env->dregs[0], &gregs[0]);
5017 err |= __get_user(env->dregs[1], &gregs[1]);
5018 err |= __get_user(env->dregs[2], &gregs[2]);
5019 err |= __get_user(env->dregs[3], &gregs[3]);
5020 err |= __get_user(env->dregs[4], &gregs[4]);
5021 err |= __get_user(env->dregs[5], &gregs[5]);
5022 err |= __get_user(env->dregs[6], &gregs[6]);
5023 err |= __get_user(env->dregs[7], &gregs[7]);
5024 err |= __get_user(env->aregs[0], &gregs[8]);
5025 err |= __get_user(env->aregs[1], &gregs[9]);
5026 err |= __get_user(env->aregs[2], &gregs[10]);
5027 err |= __get_user(env->aregs[3], &gregs[11]);
5028 err |= __get_user(env->aregs[4], &gregs[12]);
5029 err |= __get_user(env->aregs[5], &gregs[13]);
5030 err |= __get_user(env->aregs[6], &gregs[14]);
5031 err |= __get_user(env->aregs[7], &gregs[15]);
5032 err |= __get_user(env->pc, &gregs[16]);
5033 err |= __get_user(temp, &gregs[17]);
5034 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5036 *pd0 = env->dregs[0];
5037 return err;
5039 badframe:
5040 return 1;
5043 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5044 target_siginfo_t *info,
5045 target_sigset_t *set, CPUM68KState *env)
5047 struct target_rt_sigframe *frame;
5048 abi_ulong frame_addr;
5049 abi_ulong retcode_addr;
5050 abi_ulong info_addr;
5051 abi_ulong uc_addr;
5052 int err = 0;
5053 int i;
5055 frame_addr = get_sigframe(ka, env, sizeof *frame);
5056 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5057 goto give_sigsegv;
5059 err |= __put_user(sig, &frame->sig);
5061 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5062 err |= __put_user(info_addr, &frame->pinfo);
5064 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5065 err |= __put_user(uc_addr, &frame->puc);
5067 err |= copy_siginfo_to_user(&frame->info, info);
5069 /* Create the ucontext */
5071 err |= __put_user(0, &frame->uc.tuc_flags);
5072 err |= __put_user(0, &frame->uc.tuc_link);
5073 err |= __put_user(target_sigaltstack_used.ss_sp,
5074 &frame->uc.tuc_stack.ss_sp);
5075 err |= __put_user(sas_ss_flags(env->aregs[7]),
5076 &frame->uc.tuc_stack.ss_flags);
5077 err |= __put_user(target_sigaltstack_used.ss_size,
5078 &frame->uc.tuc_stack.ss_size);
5079 err |= target_rt_setup_ucontext(&frame->uc, env);
5081 if (err)
5082 goto give_sigsegv;
5084 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5085 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
5086 goto give_sigsegv;
5089 /* Set up to return from userspace. */
5091 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5092 err |= __put_user(retcode_addr, &frame->pretcode);
5094 /* moveq #,d0; notb d0; trap #0 */
5096 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5097 (long *)(frame->retcode + 0));
5098 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5100 if (err)
5101 goto give_sigsegv;
5103 /* Set up to return from userspace */
5105 env->aregs[7] = frame_addr;
5106 env->pc = ka->_sa_handler;
5108 unlock_user_struct(frame, frame_addr, 1);
5109 return;
5111 give_sigsegv:
5112 unlock_user_struct(frame, frame_addr, 1);
5113 force_sig(TARGET_SIGSEGV);
5116 long do_sigreturn(CPUM68KState *env)
5118 struct target_sigframe *frame;
5119 abi_ulong frame_addr = env->aregs[7] - 4;
5120 target_sigset_t target_set;
5121 sigset_t set;
5122 int d0, i;
5124 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5125 goto badframe;
5127 /* set blocked signals */
5129 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5130 goto badframe;
5132 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5133 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5134 goto badframe;
5137 target_to_host_sigset_internal(&set, &target_set);
5138 sigprocmask(SIG_SETMASK, &set, NULL);
5140 /* restore registers */
5142 if (restore_sigcontext(env, &frame->sc, &d0))
5143 goto badframe;
5145 unlock_user_struct(frame, frame_addr, 0);
5146 return d0;
5148 badframe:
5149 unlock_user_struct(frame, frame_addr, 0);
5150 force_sig(TARGET_SIGSEGV);
5151 return 0;
5154 long do_rt_sigreturn(CPUM68KState *env)
5156 struct target_rt_sigframe *frame;
5157 abi_ulong frame_addr = env->aregs[7] - 4;
5158 target_sigset_t target_set;
5159 sigset_t set;
5160 int d0;
5162 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5163 goto badframe;
5165 target_to_host_sigset_internal(&set, &target_set);
5166 sigprocmask(SIG_SETMASK, &set, NULL);
5168 /* restore registers */
5170 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5171 goto badframe;
5173 if (do_sigaltstack(frame_addr +
5174 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5175 0, get_sp_from_cpustate(env)) == -EFAULT)
5176 goto badframe;
5178 unlock_user_struct(frame, frame_addr, 0);
5179 return d0;
5181 badframe:
5182 unlock_user_struct(frame, frame_addr, 0);
5183 force_sig(TARGET_SIGSEGV);
5184 return 0;
5187 #elif defined(TARGET_ALPHA)
5189 struct target_sigcontext {
5190 abi_long sc_onstack;
5191 abi_long sc_mask;
5192 abi_long sc_pc;
5193 abi_long sc_ps;
5194 abi_long sc_regs[32];
5195 abi_long sc_ownedfp;
5196 abi_long sc_fpregs[32];
5197 abi_ulong sc_fpcr;
5198 abi_ulong sc_fp_control;
5199 abi_ulong sc_reserved1;
5200 abi_ulong sc_reserved2;
5201 abi_ulong sc_ssize;
5202 abi_ulong sc_sbase;
5203 abi_ulong sc_traparg_a0;
5204 abi_ulong sc_traparg_a1;
5205 abi_ulong sc_traparg_a2;
5206 abi_ulong sc_fp_trap_pc;
5207 abi_ulong sc_fp_trigger_sum;
5208 abi_ulong sc_fp_trigger_inst;
5211 struct target_ucontext {
5212 abi_ulong tuc_flags;
5213 abi_ulong tuc_link;
5214 abi_ulong tuc_osf_sigmask;
5215 target_stack_t tuc_stack;
5216 struct target_sigcontext tuc_mcontext;
5217 target_sigset_t tuc_sigmask;
5220 struct target_sigframe {
5221 struct target_sigcontext sc;
5222 unsigned int retcode[3];
5225 struct target_rt_sigframe {
5226 target_siginfo_t info;
5227 struct target_ucontext uc;
5228 unsigned int retcode[3];
5231 #define INSN_MOV_R30_R16 0x47fe0410
5232 #define INSN_LDI_R0 0x201f0000
5233 #define INSN_CALLSYS 0x00000083
5235 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5236 abi_ulong frame_addr, target_sigset_t *set)
5238 int i, err = 0;
5240 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5241 err |= __put_user(set->sig[0], &sc->sc_mask);
5242 err |= __put_user(env->pc, &sc->sc_pc);
5243 err |= __put_user(8, &sc->sc_ps);
5245 for (i = 0; i < 31; ++i) {
5246 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5248 err |= __put_user(0, &sc->sc_regs[31]);
5250 for (i = 0; i < 31; ++i) {
5251 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5253 err |= __put_user(0, &sc->sc_fpregs[31]);
5254 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5256 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5257 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5258 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5260 return err;
5263 static int restore_sigcontext(CPUAlphaState *env,
5264 struct target_sigcontext *sc)
5266 uint64_t fpcr;
5267 int i, err = 0;
5269 err |= __get_user(env->pc, &sc->sc_pc);
5271 for (i = 0; i < 31; ++i) {
5272 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5274 for (i = 0; i < 31; ++i) {
5275 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5278 err |= __get_user(fpcr, &sc->sc_fpcr);
5279 cpu_alpha_store_fpcr(env, fpcr);
5281 return err;
5284 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5285 CPUAlphaState *env,
5286 unsigned long framesize)
5288 abi_ulong sp = env->ir[IR_SP];
5290 /* This is the X/Open sanctioned signal stack switching. */
5291 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5292 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5294 return (sp - framesize) & -32;
5297 static void setup_frame(int sig, struct target_sigaction *ka,
5298 target_sigset_t *set, CPUAlphaState *env)
5300 abi_ulong frame_addr, r26;
5301 struct target_sigframe *frame;
5302 int err = 0;
5304 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5305 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5306 goto give_sigsegv;
5309 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5311 if (ka->sa_restorer) {
5312 r26 = ka->sa_restorer;
5313 } else {
5314 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5315 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5316 &frame->retcode[1]);
5317 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5318 /* imb() */
5319 r26 = frame_addr;
5322 unlock_user_struct(frame, frame_addr, 1);
5324 if (err) {
5325 give_sigsegv:
5326 if (sig == TARGET_SIGSEGV) {
5327 ka->_sa_handler = TARGET_SIG_DFL;
5329 force_sig(TARGET_SIGSEGV);
5332 env->ir[IR_RA] = r26;
5333 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5334 env->ir[IR_A0] = sig;
5335 env->ir[IR_A1] = 0;
5336 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5337 env->ir[IR_SP] = frame_addr;
5340 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5341 target_siginfo_t *info,
5342 target_sigset_t *set, CPUAlphaState *env)
5344 abi_ulong frame_addr, r26;
5345 struct target_rt_sigframe *frame;
5346 int i, err = 0;
5348 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5349 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5350 goto give_sigsegv;
5353 err |= copy_siginfo_to_user(&frame->info, info);
5355 err |= __put_user(0, &frame->uc.tuc_flags);
5356 err |= __put_user(0, &frame->uc.tuc_link);
5357 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5358 err |= __put_user(target_sigaltstack_used.ss_sp,
5359 &frame->uc.tuc_stack.ss_sp);
5360 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5361 &frame->uc.tuc_stack.ss_flags);
5362 err |= __put_user(target_sigaltstack_used.ss_size,
5363 &frame->uc.tuc_stack.ss_size);
5364 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5365 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5366 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5369 if (ka->sa_restorer) {
5370 r26 = ka->sa_restorer;
5371 } else {
5372 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5373 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5374 &frame->retcode[1]);
5375 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5376 /* imb(); */
5377 r26 = frame_addr;
5380 if (err) {
5381 give_sigsegv:
5382 if (sig == TARGET_SIGSEGV) {
5383 ka->_sa_handler = TARGET_SIG_DFL;
5385 force_sig(TARGET_SIGSEGV);
5388 env->ir[IR_RA] = r26;
5389 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5390 env->ir[IR_A0] = sig;
5391 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5392 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5393 env->ir[IR_SP] = frame_addr;
5396 long do_sigreturn(CPUAlphaState *env)
5398 struct target_sigcontext *sc;
5399 abi_ulong sc_addr = env->ir[IR_A0];
5400 target_sigset_t target_set;
5401 sigset_t set;
5403 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5404 goto badframe;
5407 target_sigemptyset(&target_set);
5408 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5409 goto badframe;
5412 target_to_host_sigset_internal(&set, &target_set);
5413 sigprocmask(SIG_SETMASK, &set, NULL);
5415 if (restore_sigcontext(env, sc)) {
5416 goto badframe;
5418 unlock_user_struct(sc, sc_addr, 0);
5419 return env->ir[IR_V0];
5421 badframe:
5422 unlock_user_struct(sc, sc_addr, 0);
5423 force_sig(TARGET_SIGSEGV);
5426 long do_rt_sigreturn(CPUAlphaState *env)
5428 abi_ulong frame_addr = env->ir[IR_A0];
5429 struct target_rt_sigframe *frame;
5430 sigset_t set;
5432 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5433 goto badframe;
5435 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5436 sigprocmask(SIG_SETMASK, &set, NULL);
5438 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5439 goto badframe;
5441 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5442 uc.tuc_stack),
5443 0, env->ir[IR_SP]) == -EFAULT) {
5444 goto badframe;
5447 unlock_user_struct(frame, frame_addr, 0);
5448 return env->ir[IR_V0];
5451 badframe:
5452 unlock_user_struct(frame, frame_addr, 0);
5453 force_sig(TARGET_SIGSEGV);
5456 #else
5458 static void setup_frame(int sig, struct target_sigaction *ka,
5459 target_sigset_t *set, CPUArchState *env)
5461 fprintf(stderr, "setup_frame: not implemented\n");
5464 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5465 target_siginfo_t *info,
5466 target_sigset_t *set, CPUArchState *env)
5468 fprintf(stderr, "setup_rt_frame: not implemented\n");
5471 long do_sigreturn(CPUArchState *env)
5473 fprintf(stderr, "do_sigreturn: not implemented\n");
5474 return -TARGET_ENOSYS;
5477 long do_rt_sigreturn(CPUArchState *env)
5479 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5480 return -TARGET_ENOSYS;
5483 #endif
5485 void process_pending_signals(CPUArchState *cpu_env)
5487 int sig;
5488 abi_ulong handler;
5489 sigset_t set, old_set;
5490 target_sigset_t target_old_set;
5491 struct emulated_sigtable *k;
5492 struct target_sigaction *sa;
5493 struct sigqueue *q;
5494 TaskState *ts = cpu_env->opaque;
5496 if (!ts->signal_pending)
5497 return;
5499 /* FIXME: This is not threadsafe. */
5500 k = ts->sigtab;
5501 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5502 if (k->pending)
5503 goto handle_signal;
5504 k++;
5506 /* if no signal is pending, just return */
5507 ts->signal_pending = 0;
5508 return;
5510 handle_signal:
5511 #ifdef DEBUG_SIGNAL
5512 fprintf(stderr, "qemu: process signal %d\n", sig);
5513 #endif
5514 /* dequeue signal */
5515 q = k->first;
5516 k->first = q->next;
5517 if (!k->first)
5518 k->pending = 0;
5520 sig = gdb_handlesig (cpu_env, sig);
5521 if (!sig) {
5522 sa = NULL;
5523 handler = TARGET_SIG_IGN;
5524 } else {
5525 sa = &sigact_table[sig - 1];
5526 handler = sa->_sa_handler;
5529 if (handler == TARGET_SIG_DFL) {
5530 /* default handler : ignore some signal. The other are job control or fatal */
5531 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5532 kill(getpid(),SIGSTOP);
5533 } else if (sig != TARGET_SIGCHLD &&
5534 sig != TARGET_SIGURG &&
5535 sig != TARGET_SIGWINCH &&
5536 sig != TARGET_SIGCONT) {
5537 force_sig(sig);
5539 } else if (handler == TARGET_SIG_IGN) {
5540 /* ignore sig */
5541 } else if (handler == TARGET_SIG_ERR) {
5542 force_sig(sig);
5543 } else {
5544 /* compute the blocked signals during the handler execution */
5545 target_to_host_sigset(&set, &sa->sa_mask);
5546 /* SA_NODEFER indicates that the current signal should not be
5547 blocked during the handler */
5548 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5549 sigaddset(&set, target_to_host_signal(sig));
5551 /* block signals in the handler using Linux */
5552 sigprocmask(SIG_BLOCK, &set, &old_set);
5553 /* save the previous blocked signal state to restore it at the
5554 end of the signal execution (see do_sigreturn) */
5555 host_to_target_sigset_internal(&target_old_set, &old_set);
5557 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5558 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5560 CPUX86State *env = cpu_env;
5561 if (env->eflags & VM_MASK)
5562 save_v86_state(env);
5564 #endif
5565 /* prepare the stack frame of the virtual CPU */
5566 if (sa->sa_flags & TARGET_SA_SIGINFO)
5567 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5568 else
5569 setup_frame(sig, sa, &target_old_set, cpu_env);
5570 if (sa->sa_flags & TARGET_SA_RESETHAND)
5571 sa->_sa_handler = TARGET_SIG_DFL;
5573 if (q != &k->info)
5574 free_sigqueue(cpu_env, q);