s390x: Rename 'dprintf' to 'DPRINTF'
[qemu/ar7.git] / linux-user / signal.c
bloba5e8906c4d8a78d856104fb0350442a024ffc673
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 < 0 || sig >= _NSIG)
106 return sig;
107 return host_to_target_signal_table[sig];
110 int target_to_host_signal(int sig)
112 if (sig < 0 || 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 CPUArchState *env = thread_cpu->env_ptr;
392 TaskState *ts = (TaskState *)env->opaque;
393 int host_sig, core_dumped = 0;
394 struct sigaction act;
395 host_sig = target_to_host_signal(target_sig);
396 gdb_signalled(env, target_sig);
398 /* dump core if supported by target binary format */
399 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
400 stop_all_tasks();
401 core_dumped =
402 ((*ts->bprm->core_dump)(target_sig, env) == 0);
404 if (core_dumped) {
405 /* we already dumped the core of target process, we don't want
406 * a coredump of qemu itself */
407 struct rlimit nodump;
408 getrlimit(RLIMIT_CORE, &nodump);
409 nodump.rlim_cur=0;
410 setrlimit(RLIMIT_CORE, &nodump);
411 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
412 target_sig, strsignal(host_sig), "core dumped" );
415 /* The proper exit code for dying from an uncaught signal is
416 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
417 * a negative value. To get the proper exit code we need to
418 * actually die from an uncaught signal. Here the default signal
419 * handler is installed, we send ourself a signal and we wait for
420 * it to arrive. */
421 sigfillset(&act.sa_mask);
422 act.sa_handler = SIG_DFL;
423 sigaction(host_sig, &act, NULL);
425 /* For some reason raise(host_sig) doesn't send the signal when
426 * statically linked on x86-64. */
427 kill(getpid(), host_sig);
429 /* Make sure the signal isn't masked (just reuse the mask inside
430 of act) */
431 sigdelset(&act.sa_mask, host_sig);
432 sigsuspend(&act.sa_mask);
434 /* unreachable */
435 abort();
438 /* queue a signal so that it will be send to the virtual CPU as soon
439 as possible */
440 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
442 TaskState *ts = env->opaque;
443 struct emulated_sigtable *k;
444 struct sigqueue *q, **pq;
445 abi_ulong handler;
446 int queue;
448 #if defined(DEBUG_SIGNAL)
449 fprintf(stderr, "queue_signal: sig=%d\n",
450 sig);
451 #endif
452 k = &ts->sigtab[sig - 1];
453 queue = gdb_queuesig ();
454 handler = sigact_table[sig - 1]._sa_handler;
455 if (!queue && handler == TARGET_SIG_DFL) {
456 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
457 kill(getpid(),SIGSTOP);
458 return 0;
459 } else
460 /* default handler : ignore some signal. The other are fatal */
461 if (sig != TARGET_SIGCHLD &&
462 sig != TARGET_SIGURG &&
463 sig != TARGET_SIGWINCH &&
464 sig != TARGET_SIGCONT) {
465 force_sig(sig);
466 } else {
467 return 0; /* indicate ignored */
469 } else if (!queue && handler == TARGET_SIG_IGN) {
470 /* ignore signal */
471 return 0;
472 } else if (!queue && handler == TARGET_SIG_ERR) {
473 force_sig(sig);
474 } else {
475 pq = &k->first;
476 if (sig < TARGET_SIGRTMIN) {
477 /* if non real time signal, we queue exactly one signal */
478 if (!k->pending)
479 q = &k->info;
480 else
481 return 0;
482 } else {
483 if (!k->pending) {
484 /* first signal */
485 q = &k->info;
486 } else {
487 q = alloc_sigqueue(env);
488 if (!q)
489 return -EAGAIN;
490 while (*pq != NULL)
491 pq = &(*pq)->next;
494 *pq = q;
495 q->info = *info;
496 q->next = NULL;
497 k->pending = 1;
498 /* signal that a new signal is pending */
499 ts->signal_pending = 1;
500 return 1; /* indicates that the signal was queued */
504 static void host_signal_handler(int host_signum, siginfo_t *info,
505 void *puc)
507 CPUArchState *env = thread_cpu->env_ptr;
508 int sig;
509 target_siginfo_t tinfo;
511 /* the CPU emulator uses some host signals to detect exceptions,
512 we forward to it some signals */
513 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
514 && info->si_code > 0) {
515 if (cpu_signal_handler(host_signum, info, puc))
516 return;
519 /* get target signal number */
520 sig = host_to_target_signal(host_signum);
521 if (sig < 1 || sig > TARGET_NSIG)
522 return;
523 #if defined(DEBUG_SIGNAL)
524 fprintf(stderr, "qemu: got signal %d\n", sig);
525 #endif
526 host_to_target_siginfo_noswap(&tinfo, info);
527 if (queue_signal(env, sig, &tinfo) == 1) {
528 /* interrupt the virtual CPU as soon as possible */
529 cpu_exit(thread_cpu);
533 /* do_sigaltstack() returns target values and errnos. */
534 /* compare linux/kernel/signal.c:do_sigaltstack() */
535 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
537 int ret;
538 struct target_sigaltstack oss;
540 /* XXX: test errors */
541 if(uoss_addr)
543 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
544 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
545 __put_user(sas_ss_flags(sp), &oss.ss_flags);
548 if(uss_addr)
550 struct target_sigaltstack *uss;
551 struct target_sigaltstack ss;
553 ret = -TARGET_EFAULT;
554 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
555 || __get_user(ss.ss_sp, &uss->ss_sp)
556 || __get_user(ss.ss_size, &uss->ss_size)
557 || __get_user(ss.ss_flags, &uss->ss_flags))
558 goto out;
559 unlock_user_struct(uss, uss_addr, 0);
561 ret = -TARGET_EPERM;
562 if (on_sig_stack(sp))
563 goto out;
565 ret = -TARGET_EINVAL;
566 if (ss.ss_flags != TARGET_SS_DISABLE
567 && ss.ss_flags != TARGET_SS_ONSTACK
568 && ss.ss_flags != 0)
569 goto out;
571 if (ss.ss_flags == TARGET_SS_DISABLE) {
572 ss.ss_size = 0;
573 ss.ss_sp = 0;
574 } else {
575 ret = -TARGET_ENOMEM;
576 if (ss.ss_size < MINSIGSTKSZ)
577 goto out;
580 target_sigaltstack_used.ss_sp = ss.ss_sp;
581 target_sigaltstack_used.ss_size = ss.ss_size;
584 if (uoss_addr) {
585 ret = -TARGET_EFAULT;
586 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
587 goto out;
590 ret = 0;
591 out:
592 return ret;
595 /* do_sigaction() return host values and errnos */
596 int do_sigaction(int sig, const struct target_sigaction *act,
597 struct target_sigaction *oact)
599 struct target_sigaction *k;
600 struct sigaction act1;
601 int host_sig;
602 int ret = 0;
604 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
605 return -EINVAL;
606 k = &sigact_table[sig - 1];
607 #if defined(DEBUG_SIGNAL)
608 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
609 sig, act, oact);
610 #endif
611 if (oact) {
612 __put_user(k->_sa_handler, &oact->_sa_handler);
613 __put_user(k->sa_flags, &oact->sa_flags);
614 #if !defined(TARGET_MIPS)
615 __put_user(k->sa_restorer, &oact->sa_restorer);
616 #endif
617 /* Not swapped. */
618 oact->sa_mask = k->sa_mask;
620 if (act) {
621 /* FIXME: This is not threadsafe. */
622 __get_user(k->_sa_handler, &act->_sa_handler);
623 __get_user(k->sa_flags, &act->sa_flags);
624 #if !defined(TARGET_MIPS)
625 __get_user(k->sa_restorer, &act->sa_restorer);
626 #endif
627 /* To be swapped in target_to_host_sigset. */
628 k->sa_mask = act->sa_mask;
630 /* we update the host linux signal state */
631 host_sig = target_to_host_signal(sig);
632 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
633 sigfillset(&act1.sa_mask);
634 act1.sa_flags = SA_SIGINFO;
635 if (k->sa_flags & TARGET_SA_RESTART)
636 act1.sa_flags |= SA_RESTART;
637 /* NOTE: it is important to update the host kernel signal
638 ignore state to avoid getting unexpected interrupted
639 syscalls */
640 if (k->_sa_handler == TARGET_SIG_IGN) {
641 act1.sa_sigaction = (void *)SIG_IGN;
642 } else if (k->_sa_handler == TARGET_SIG_DFL) {
643 if (fatal_signal (sig))
644 act1.sa_sigaction = host_signal_handler;
645 else
646 act1.sa_sigaction = (void *)SIG_DFL;
647 } else {
648 act1.sa_sigaction = host_signal_handler;
650 ret = sigaction(host_sig, &act1, NULL);
653 return ret;
656 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
657 const target_siginfo_t *info)
659 tswap_siginfo(tinfo, info);
660 return 0;
663 static inline int current_exec_domain_sig(int sig)
665 return /* current->exec_domain && current->exec_domain->signal_invmap
666 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
669 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
671 /* from the Linux kernel */
673 struct target_fpreg {
674 uint16_t significand[4];
675 uint16_t exponent;
678 struct target_fpxreg {
679 uint16_t significand[4];
680 uint16_t exponent;
681 uint16_t padding[3];
684 struct target_xmmreg {
685 abi_ulong element[4];
688 struct target_fpstate {
689 /* Regular FPU environment */
690 abi_ulong cw;
691 abi_ulong sw;
692 abi_ulong tag;
693 abi_ulong ipoff;
694 abi_ulong cssel;
695 abi_ulong dataoff;
696 abi_ulong datasel;
697 struct target_fpreg _st[8];
698 uint16_t status;
699 uint16_t magic; /* 0xffff = regular FPU data only */
701 /* FXSR FPU environment */
702 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
703 abi_ulong mxcsr;
704 abi_ulong reserved;
705 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
706 struct target_xmmreg _xmm[8];
707 abi_ulong padding[56];
710 #define X86_FXSR_MAGIC 0x0000
712 struct target_sigcontext {
713 uint16_t gs, __gsh;
714 uint16_t fs, __fsh;
715 uint16_t es, __esh;
716 uint16_t ds, __dsh;
717 abi_ulong edi;
718 abi_ulong esi;
719 abi_ulong ebp;
720 abi_ulong esp;
721 abi_ulong ebx;
722 abi_ulong edx;
723 abi_ulong ecx;
724 abi_ulong eax;
725 abi_ulong trapno;
726 abi_ulong err;
727 abi_ulong eip;
728 uint16_t cs, __csh;
729 abi_ulong eflags;
730 abi_ulong esp_at_signal;
731 uint16_t ss, __ssh;
732 abi_ulong fpstate; /* pointer */
733 abi_ulong oldmask;
734 abi_ulong cr2;
737 struct target_ucontext {
738 abi_ulong tuc_flags;
739 abi_ulong tuc_link;
740 target_stack_t tuc_stack;
741 struct target_sigcontext tuc_mcontext;
742 target_sigset_t tuc_sigmask; /* mask last for extensibility */
745 struct sigframe
747 abi_ulong pretcode;
748 int sig;
749 struct target_sigcontext sc;
750 struct target_fpstate fpstate;
751 abi_ulong extramask[TARGET_NSIG_WORDS-1];
752 char retcode[8];
755 struct rt_sigframe
757 abi_ulong pretcode;
758 int sig;
759 abi_ulong pinfo;
760 abi_ulong puc;
761 struct target_siginfo info;
762 struct target_ucontext uc;
763 struct target_fpstate fpstate;
764 char retcode[8];
768 * Set up a signal frame.
771 /* XXX: save x87 state */
772 static int
773 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
774 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
776 int err = 0;
777 uint16_t magic;
779 /* already locked in setup_frame() */
780 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
781 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
782 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
783 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
784 err |= __put_user(env->regs[R_EDI], &sc->edi);
785 err |= __put_user(env->regs[R_ESI], &sc->esi);
786 err |= __put_user(env->regs[R_EBP], &sc->ebp);
787 err |= __put_user(env->regs[R_ESP], &sc->esp);
788 err |= __put_user(env->regs[R_EBX], &sc->ebx);
789 err |= __put_user(env->regs[R_EDX], &sc->edx);
790 err |= __put_user(env->regs[R_ECX], &sc->ecx);
791 err |= __put_user(env->regs[R_EAX], &sc->eax);
792 err |= __put_user(env->exception_index, &sc->trapno);
793 err |= __put_user(env->error_code, &sc->err);
794 err |= __put_user(env->eip, &sc->eip);
795 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
796 err |= __put_user(env->eflags, &sc->eflags);
797 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
798 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
800 cpu_x86_fsave(env, fpstate_addr, 1);
801 fpstate->status = fpstate->sw;
802 magic = 0xffff;
803 err |= __put_user(magic, &fpstate->magic);
804 err |= __put_user(fpstate_addr, &sc->fpstate);
806 /* non-iBCS2 extensions.. */
807 err |= __put_user(mask, &sc->oldmask);
808 err |= __put_user(env->cr[2], &sc->cr2);
809 return err;
813 * Determine which stack to use..
816 static inline abi_ulong
817 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
819 unsigned long esp;
821 /* Default to using normal stack */
822 esp = env->regs[R_ESP];
823 /* This is the X/Open sanctioned signal stack switching. */
824 if (ka->sa_flags & TARGET_SA_ONSTACK) {
825 if (sas_ss_flags(esp) == 0)
826 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
829 /* This is the legacy signal stack switching. */
830 else
831 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
832 !(ka->sa_flags & TARGET_SA_RESTORER) &&
833 ka->sa_restorer) {
834 esp = (unsigned long) ka->sa_restorer;
836 return (esp - frame_size) & -8ul;
839 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
840 static void setup_frame(int sig, struct target_sigaction *ka,
841 target_sigset_t *set, CPUX86State *env)
843 abi_ulong frame_addr;
844 struct sigframe *frame;
845 int i, err = 0;
847 frame_addr = get_sigframe(ka, env, sizeof(*frame));
849 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
850 goto give_sigsegv;
852 err |= __put_user(current_exec_domain_sig(sig),
853 &frame->sig);
854 if (err)
855 goto give_sigsegv;
857 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
858 frame_addr + offsetof(struct sigframe, fpstate));
859 if (err)
860 goto give_sigsegv;
862 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
863 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
864 goto give_sigsegv;
867 /* Set up to return from userspace. If provided, use a stub
868 already in userspace. */
869 if (ka->sa_flags & TARGET_SA_RESTORER) {
870 err |= __put_user(ka->sa_restorer, &frame->pretcode);
871 } else {
872 uint16_t val16;
873 abi_ulong retcode_addr;
874 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
875 err |= __put_user(retcode_addr, &frame->pretcode);
876 /* This is popl %eax ; movl $,%eax ; int $0x80 */
877 val16 = 0xb858;
878 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
879 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
880 val16 = 0x80cd;
881 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
884 if (err)
885 goto give_sigsegv;
887 /* Set up registers for signal handler */
888 env->regs[R_ESP] = frame_addr;
889 env->eip = ka->_sa_handler;
891 cpu_x86_load_seg(env, R_DS, __USER_DS);
892 cpu_x86_load_seg(env, R_ES, __USER_DS);
893 cpu_x86_load_seg(env, R_SS, __USER_DS);
894 cpu_x86_load_seg(env, R_CS, __USER_CS);
895 env->eflags &= ~TF_MASK;
897 unlock_user_struct(frame, frame_addr, 1);
899 return;
901 give_sigsegv:
902 unlock_user_struct(frame, frame_addr, 1);
903 if (sig == TARGET_SIGSEGV)
904 ka->_sa_handler = TARGET_SIG_DFL;
905 force_sig(TARGET_SIGSEGV /* , current */);
908 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
909 static void setup_rt_frame(int sig, struct target_sigaction *ka,
910 target_siginfo_t *info,
911 target_sigset_t *set, CPUX86State *env)
913 abi_ulong frame_addr, addr;
914 struct rt_sigframe *frame;
915 int i, err = 0;
917 frame_addr = get_sigframe(ka, env, sizeof(*frame));
919 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
920 goto give_sigsegv;
922 err |= __put_user(current_exec_domain_sig(sig),
923 &frame->sig);
924 addr = frame_addr + offsetof(struct rt_sigframe, info);
925 err |= __put_user(addr, &frame->pinfo);
926 addr = frame_addr + offsetof(struct rt_sigframe, uc);
927 err |= __put_user(addr, &frame->puc);
928 err |= copy_siginfo_to_user(&frame->info, info);
929 if (err)
930 goto give_sigsegv;
932 /* Create the ucontext. */
933 err |= __put_user(0, &frame->uc.tuc_flags);
934 err |= __put_user(0, &frame->uc.tuc_link);
935 err |= __put_user(target_sigaltstack_used.ss_sp,
936 &frame->uc.tuc_stack.ss_sp);
937 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
938 &frame->uc.tuc_stack.ss_flags);
939 err |= __put_user(target_sigaltstack_used.ss_size,
940 &frame->uc.tuc_stack.ss_size);
941 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
942 env, set->sig[0],
943 frame_addr + offsetof(struct rt_sigframe, fpstate));
944 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
945 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
946 goto give_sigsegv;
949 /* Set up to return from userspace. If provided, use a stub
950 already in userspace. */
951 if (ka->sa_flags & TARGET_SA_RESTORER) {
952 err |= __put_user(ka->sa_restorer, &frame->pretcode);
953 } else {
954 uint16_t val16;
955 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
956 err |= __put_user(addr, &frame->pretcode);
957 /* This is movl $,%eax ; int $0x80 */
958 err |= __put_user(0xb8, (char *)(frame->retcode+0));
959 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
960 val16 = 0x80cd;
961 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
964 if (err)
965 goto give_sigsegv;
967 /* Set up registers for signal handler */
968 env->regs[R_ESP] = frame_addr;
969 env->eip = ka->_sa_handler;
971 cpu_x86_load_seg(env, R_DS, __USER_DS);
972 cpu_x86_load_seg(env, R_ES, __USER_DS);
973 cpu_x86_load_seg(env, R_SS, __USER_DS);
974 cpu_x86_load_seg(env, R_CS, __USER_CS);
975 env->eflags &= ~TF_MASK;
977 unlock_user_struct(frame, frame_addr, 1);
979 return;
981 give_sigsegv:
982 unlock_user_struct(frame, frame_addr, 1);
983 if (sig == TARGET_SIGSEGV)
984 ka->_sa_handler = TARGET_SIG_DFL;
985 force_sig(TARGET_SIGSEGV /* , current */);
988 static int
989 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
991 unsigned int err = 0;
992 abi_ulong fpstate_addr;
993 unsigned int tmpflags;
995 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
996 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
997 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
998 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1000 env->regs[R_EDI] = tswapl(sc->edi);
1001 env->regs[R_ESI] = tswapl(sc->esi);
1002 env->regs[R_EBP] = tswapl(sc->ebp);
1003 env->regs[R_ESP] = tswapl(sc->esp);
1004 env->regs[R_EBX] = tswapl(sc->ebx);
1005 env->regs[R_EDX] = tswapl(sc->edx);
1006 env->regs[R_ECX] = tswapl(sc->ecx);
1007 env->eip = tswapl(sc->eip);
1009 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1010 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1012 tmpflags = tswapl(sc->eflags);
1013 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1014 // regs->orig_eax = -1; /* disable syscall checks */
1016 fpstate_addr = tswapl(sc->fpstate);
1017 if (fpstate_addr != 0) {
1018 if (!access_ok(VERIFY_READ, fpstate_addr,
1019 sizeof(struct target_fpstate)))
1020 goto badframe;
1021 cpu_x86_frstor(env, fpstate_addr, 1);
1024 *peax = tswapl(sc->eax);
1025 return err;
1026 badframe:
1027 return 1;
1030 long do_sigreturn(CPUX86State *env)
1032 struct sigframe *frame;
1033 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1034 target_sigset_t target_set;
1035 sigset_t set;
1036 int eax, i;
1038 #if defined(DEBUG_SIGNAL)
1039 fprintf(stderr, "do_sigreturn\n");
1040 #endif
1041 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1042 goto badframe;
1043 /* set blocked signals */
1044 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1045 goto badframe;
1046 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1047 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1048 goto badframe;
1051 target_to_host_sigset_internal(&set, &target_set);
1052 sigprocmask(SIG_SETMASK, &set, NULL);
1054 /* restore registers */
1055 if (restore_sigcontext(env, &frame->sc, &eax))
1056 goto badframe;
1057 unlock_user_struct(frame, frame_addr, 0);
1058 return eax;
1060 badframe:
1061 unlock_user_struct(frame, frame_addr, 0);
1062 force_sig(TARGET_SIGSEGV);
1063 return 0;
1066 long do_rt_sigreturn(CPUX86State *env)
1068 abi_ulong frame_addr;
1069 struct rt_sigframe *frame;
1070 sigset_t set;
1071 int eax;
1073 frame_addr = env->regs[R_ESP] - 4;
1074 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1075 goto badframe;
1076 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1077 sigprocmask(SIG_SETMASK, &set, NULL);
1079 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1080 goto badframe;
1082 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1083 get_sp_from_cpustate(env)) == -EFAULT)
1084 goto badframe;
1086 unlock_user_struct(frame, frame_addr, 0);
1087 return eax;
1089 badframe:
1090 unlock_user_struct(frame, frame_addr, 0);
1091 force_sig(TARGET_SIGSEGV);
1092 return 0;
1095 #elif defined(TARGET_ARM)
1097 struct target_sigcontext {
1098 abi_ulong trap_no;
1099 abi_ulong error_code;
1100 abi_ulong oldmask;
1101 abi_ulong arm_r0;
1102 abi_ulong arm_r1;
1103 abi_ulong arm_r2;
1104 abi_ulong arm_r3;
1105 abi_ulong arm_r4;
1106 abi_ulong arm_r5;
1107 abi_ulong arm_r6;
1108 abi_ulong arm_r7;
1109 abi_ulong arm_r8;
1110 abi_ulong arm_r9;
1111 abi_ulong arm_r10;
1112 abi_ulong arm_fp;
1113 abi_ulong arm_ip;
1114 abi_ulong arm_sp;
1115 abi_ulong arm_lr;
1116 abi_ulong arm_pc;
1117 abi_ulong arm_cpsr;
1118 abi_ulong fault_address;
1121 struct target_ucontext_v1 {
1122 abi_ulong tuc_flags;
1123 abi_ulong tuc_link;
1124 target_stack_t tuc_stack;
1125 struct target_sigcontext tuc_mcontext;
1126 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1129 struct target_ucontext_v2 {
1130 abi_ulong tuc_flags;
1131 abi_ulong tuc_link;
1132 target_stack_t tuc_stack;
1133 struct target_sigcontext tuc_mcontext;
1134 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1135 char __unused[128 - sizeof(target_sigset_t)];
1136 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1139 struct target_user_vfp {
1140 uint64_t fpregs[32];
1141 abi_ulong fpscr;
1144 struct target_user_vfp_exc {
1145 abi_ulong fpexc;
1146 abi_ulong fpinst;
1147 abi_ulong fpinst2;
1150 struct target_vfp_sigframe {
1151 abi_ulong magic;
1152 abi_ulong size;
1153 struct target_user_vfp ufp;
1154 struct target_user_vfp_exc ufp_exc;
1155 } __attribute__((__aligned__(8)));
1157 struct target_iwmmxt_sigframe {
1158 abi_ulong magic;
1159 abi_ulong size;
1160 uint64_t regs[16];
1161 /* Note that not all the coprocessor control registers are stored here */
1162 uint32_t wcssf;
1163 uint32_t wcasf;
1164 uint32_t wcgr0;
1165 uint32_t wcgr1;
1166 uint32_t wcgr2;
1167 uint32_t wcgr3;
1168 } __attribute__((__aligned__(8)));
1170 #define TARGET_VFP_MAGIC 0x56465001
1171 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1173 struct sigframe_v1
1175 struct target_sigcontext sc;
1176 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1177 abi_ulong retcode;
1180 struct sigframe_v2
1182 struct target_ucontext_v2 uc;
1183 abi_ulong retcode;
1186 struct rt_sigframe_v1
1188 abi_ulong pinfo;
1189 abi_ulong puc;
1190 struct target_siginfo info;
1191 struct target_ucontext_v1 uc;
1192 abi_ulong retcode;
1195 struct rt_sigframe_v2
1197 struct target_siginfo info;
1198 struct target_ucontext_v2 uc;
1199 abi_ulong retcode;
1202 #define TARGET_CONFIG_CPU_32 1
1205 * For ARM syscalls, we encode the syscall number into the instruction.
1207 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1208 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1211 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1212 * need two 16-bit instructions.
1214 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1215 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1217 static const abi_ulong retcodes[4] = {
1218 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1219 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1223 #define __get_user_error(x,p,e) __get_user(x, p)
1225 static inline int valid_user_regs(CPUARMState *regs)
1227 return 1;
1230 static void
1231 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1232 CPUARMState *env, abi_ulong mask)
1234 __put_user(env->regs[0], &sc->arm_r0);
1235 __put_user(env->regs[1], &sc->arm_r1);
1236 __put_user(env->regs[2], &sc->arm_r2);
1237 __put_user(env->regs[3], &sc->arm_r3);
1238 __put_user(env->regs[4], &sc->arm_r4);
1239 __put_user(env->regs[5], &sc->arm_r5);
1240 __put_user(env->regs[6], &sc->arm_r6);
1241 __put_user(env->regs[7], &sc->arm_r7);
1242 __put_user(env->regs[8], &sc->arm_r8);
1243 __put_user(env->regs[9], &sc->arm_r9);
1244 __put_user(env->regs[10], &sc->arm_r10);
1245 __put_user(env->regs[11], &sc->arm_fp);
1246 __put_user(env->regs[12], &sc->arm_ip);
1247 __put_user(env->regs[13], &sc->arm_sp);
1248 __put_user(env->regs[14], &sc->arm_lr);
1249 __put_user(env->regs[15], &sc->arm_pc);
1250 #ifdef TARGET_CONFIG_CPU_32
1251 __put_user(cpsr_read(env), &sc->arm_cpsr);
1252 #endif
1254 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1255 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1256 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1257 __put_user(mask, &sc->oldmask);
1260 static inline abi_ulong
1261 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1263 unsigned long sp = regs->regs[13];
1266 * This is the X/Open sanctioned signal stack switching.
1268 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1269 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1271 * ATPCS B01 mandates 8-byte alignment
1273 return (sp - framesize) & ~7;
1276 static int
1277 setup_return(CPUARMState *env, struct target_sigaction *ka,
1278 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1280 abi_ulong handler = ka->_sa_handler;
1281 abi_ulong retcode;
1282 int thumb = handler & 1;
1283 uint32_t cpsr = cpsr_read(env);
1285 cpsr &= ~CPSR_IT;
1286 if (thumb) {
1287 cpsr |= CPSR_T;
1288 } else {
1289 cpsr &= ~CPSR_T;
1292 if (ka->sa_flags & TARGET_SA_RESTORER) {
1293 retcode = ka->sa_restorer;
1294 } else {
1295 unsigned int idx = thumb;
1297 if (ka->sa_flags & TARGET_SA_SIGINFO)
1298 idx += 2;
1300 if (__put_user(retcodes[idx], rc))
1301 return 1;
1303 retcode = rc_addr + thumb;
1306 env->regs[0] = usig;
1307 env->regs[13] = frame_addr;
1308 env->regs[14] = retcode;
1309 env->regs[15] = handler & (thumb ? ~1 : ~3);
1310 cpsr_write(env, cpsr, 0xffffffff);
1312 return 0;
1315 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1317 int i;
1318 struct target_vfp_sigframe *vfpframe;
1319 vfpframe = (struct target_vfp_sigframe *)regspace;
1320 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1321 __put_user(sizeof(*vfpframe), &vfpframe->size);
1322 for (i = 0; i < 32; i++) {
1323 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1325 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1326 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1327 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1328 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1329 return (abi_ulong*)(vfpframe+1);
1332 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1333 CPUARMState *env)
1335 int i;
1336 struct target_iwmmxt_sigframe *iwmmxtframe;
1337 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1338 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1339 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1340 for (i = 0; i < 16; i++) {
1341 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1343 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1344 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1345 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1346 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1347 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1348 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1349 return (abi_ulong*)(iwmmxtframe+1);
1352 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1353 target_sigset_t *set, CPUARMState *env)
1355 struct target_sigaltstack stack;
1356 int i;
1357 abi_ulong *regspace;
1359 /* Clear all the bits of the ucontext we don't use. */
1360 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1362 memset(&stack, 0, sizeof(stack));
1363 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1364 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1365 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1366 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1368 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1369 /* Save coprocessor signal frame. */
1370 regspace = uc->tuc_regspace;
1371 if (arm_feature(env, ARM_FEATURE_VFP)) {
1372 regspace = setup_sigframe_v2_vfp(regspace, env);
1374 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1375 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1378 /* Write terminating magic word */
1379 __put_user(0, regspace);
1381 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1382 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1386 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1387 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1388 target_sigset_t *set, CPUARMState *regs)
1390 struct sigframe_v1 *frame;
1391 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1392 int i;
1394 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1395 return;
1397 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1399 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1400 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1401 goto end;
1404 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1405 frame_addr + offsetof(struct sigframe_v1, retcode));
1407 end:
1408 unlock_user_struct(frame, frame_addr, 1);
1411 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1412 target_sigset_t *set, CPUARMState *regs)
1414 struct sigframe_v2 *frame;
1415 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1417 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1418 return;
1420 setup_sigframe_v2(&frame->uc, set, regs);
1422 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1423 frame_addr + offsetof(struct sigframe_v2, retcode));
1425 unlock_user_struct(frame, frame_addr, 1);
1428 static void setup_frame(int usig, struct target_sigaction *ka,
1429 target_sigset_t *set, CPUARMState *regs)
1431 if (get_osversion() >= 0x020612) {
1432 setup_frame_v2(usig, ka, set, regs);
1433 } else {
1434 setup_frame_v1(usig, ka, set, regs);
1438 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1439 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1440 target_siginfo_t *info,
1441 target_sigset_t *set, CPUARMState *env)
1443 struct rt_sigframe_v1 *frame;
1444 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1445 struct target_sigaltstack stack;
1446 int i;
1447 abi_ulong info_addr, uc_addr;
1449 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1450 return /* 1 */;
1452 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1453 __put_user(info_addr, &frame->pinfo);
1454 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1455 __put_user(uc_addr, &frame->puc);
1456 copy_siginfo_to_user(&frame->info, info);
1458 /* Clear all the bits of the ucontext we don't use. */
1459 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1461 memset(&stack, 0, sizeof(stack));
1462 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1463 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1464 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1465 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1467 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1468 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1469 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1470 goto end;
1473 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1474 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1476 env->regs[1] = info_addr;
1477 env->regs[2] = uc_addr;
1479 end:
1480 unlock_user_struct(frame, frame_addr, 1);
1483 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1484 target_siginfo_t *info,
1485 target_sigset_t *set, CPUARMState *env)
1487 struct rt_sigframe_v2 *frame;
1488 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1489 abi_ulong info_addr, uc_addr;
1491 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1492 return /* 1 */;
1494 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1495 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1496 copy_siginfo_to_user(&frame->info, info);
1498 setup_sigframe_v2(&frame->uc, set, env);
1500 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1501 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1503 env->regs[1] = info_addr;
1504 env->regs[2] = uc_addr;
1506 unlock_user_struct(frame, frame_addr, 1);
1509 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1510 target_siginfo_t *info,
1511 target_sigset_t *set, CPUARMState *env)
1513 if (get_osversion() >= 0x020612) {
1514 setup_rt_frame_v2(usig, ka, info, set, env);
1515 } else {
1516 setup_rt_frame_v1(usig, ka, info, set, env);
1520 static int
1521 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1523 int err = 0;
1524 uint32_t cpsr;
1526 __get_user_error(env->regs[0], &sc->arm_r0, err);
1527 __get_user_error(env->regs[1], &sc->arm_r1, err);
1528 __get_user_error(env->regs[2], &sc->arm_r2, err);
1529 __get_user_error(env->regs[3], &sc->arm_r3, err);
1530 __get_user_error(env->regs[4], &sc->arm_r4, err);
1531 __get_user_error(env->regs[5], &sc->arm_r5, err);
1532 __get_user_error(env->regs[6], &sc->arm_r6, err);
1533 __get_user_error(env->regs[7], &sc->arm_r7, err);
1534 __get_user_error(env->regs[8], &sc->arm_r8, err);
1535 __get_user_error(env->regs[9], &sc->arm_r9, err);
1536 __get_user_error(env->regs[10], &sc->arm_r10, err);
1537 __get_user_error(env->regs[11], &sc->arm_fp, err);
1538 __get_user_error(env->regs[12], &sc->arm_ip, err);
1539 __get_user_error(env->regs[13], &sc->arm_sp, err);
1540 __get_user_error(env->regs[14], &sc->arm_lr, err);
1541 __get_user_error(env->regs[15], &sc->arm_pc, err);
1542 #ifdef TARGET_CONFIG_CPU_32
1543 __get_user_error(cpsr, &sc->arm_cpsr, err);
1544 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1545 #endif
1547 err |= !valid_user_regs(env);
1549 return err;
1552 static long do_sigreturn_v1(CPUARMState *env)
1554 abi_ulong frame_addr;
1555 struct sigframe_v1 *frame;
1556 target_sigset_t set;
1557 sigset_t host_set;
1558 int i;
1561 * Since we stacked the signal on a 64-bit boundary,
1562 * then 'sp' should be word aligned here. If it's
1563 * not, then the user is trying to mess with us.
1565 if (env->regs[13] & 7)
1566 goto badframe;
1568 frame_addr = env->regs[13];
1569 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1570 goto badframe;
1572 if (__get_user(set.sig[0], &frame->sc.oldmask))
1573 goto badframe;
1574 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1575 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1576 goto badframe;
1579 target_to_host_sigset_internal(&host_set, &set);
1580 sigprocmask(SIG_SETMASK, &host_set, NULL);
1582 if (restore_sigcontext(env, &frame->sc))
1583 goto badframe;
1585 #if 0
1586 /* Send SIGTRAP if we're single-stepping */
1587 if (ptrace_cancel_bpt(current))
1588 send_sig(SIGTRAP, current, 1);
1589 #endif
1590 unlock_user_struct(frame, frame_addr, 0);
1591 return env->regs[0];
1593 badframe:
1594 unlock_user_struct(frame, frame_addr, 0);
1595 force_sig(TARGET_SIGSEGV /* , current */);
1596 return 0;
1599 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
1601 int i;
1602 abi_ulong magic, sz;
1603 uint32_t fpscr, fpexc;
1604 struct target_vfp_sigframe *vfpframe;
1605 vfpframe = (struct target_vfp_sigframe *)regspace;
1607 __get_user(magic, &vfpframe->magic);
1608 __get_user(sz, &vfpframe->size);
1609 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1610 return 0;
1612 for (i = 0; i < 32; i++) {
1613 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1615 __get_user(fpscr, &vfpframe->ufp.fpscr);
1616 vfp_set_fpscr(env, fpscr);
1617 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1618 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1619 * and the exception flag is cleared
1621 fpexc |= (1 << 30);
1622 fpexc &= ~((1 << 31) | (1 << 28));
1623 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1624 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1625 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1626 return (abi_ulong*)(vfpframe + 1);
1629 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1630 abi_ulong *regspace)
1632 int i;
1633 abi_ulong magic, sz;
1634 struct target_iwmmxt_sigframe *iwmmxtframe;
1635 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1637 __get_user(magic, &iwmmxtframe->magic);
1638 __get_user(sz, &iwmmxtframe->size);
1639 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1640 return 0;
1642 for (i = 0; i < 16; i++) {
1643 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1645 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1646 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1647 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1648 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1649 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1650 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1651 return (abi_ulong*)(iwmmxtframe + 1);
1654 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
1655 struct target_ucontext_v2 *uc)
1657 sigset_t host_set;
1658 abi_ulong *regspace;
1660 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1661 sigprocmask(SIG_SETMASK, &host_set, NULL);
1663 if (restore_sigcontext(env, &uc->tuc_mcontext))
1664 return 1;
1666 /* Restore coprocessor signal frame */
1667 regspace = uc->tuc_regspace;
1668 if (arm_feature(env, ARM_FEATURE_VFP)) {
1669 regspace = restore_sigframe_v2_vfp(env, regspace);
1670 if (!regspace) {
1671 return 1;
1674 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1675 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1676 if (!regspace) {
1677 return 1;
1681 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1682 return 1;
1684 #if 0
1685 /* Send SIGTRAP if we're single-stepping */
1686 if (ptrace_cancel_bpt(current))
1687 send_sig(SIGTRAP, current, 1);
1688 #endif
1690 return 0;
1693 static long do_sigreturn_v2(CPUARMState *env)
1695 abi_ulong frame_addr;
1696 struct sigframe_v2 *frame;
1699 * Since we stacked the signal on a 64-bit boundary,
1700 * then 'sp' should be word aligned here. If it's
1701 * not, then the user is trying to mess with us.
1703 if (env->regs[13] & 7)
1704 goto badframe;
1706 frame_addr = env->regs[13];
1707 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1708 goto badframe;
1710 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1711 goto badframe;
1713 unlock_user_struct(frame, frame_addr, 0);
1714 return env->regs[0];
1716 badframe:
1717 unlock_user_struct(frame, frame_addr, 0);
1718 force_sig(TARGET_SIGSEGV /* , current */);
1719 return 0;
1722 long do_sigreturn(CPUARMState *env)
1724 if (get_osversion() >= 0x020612) {
1725 return do_sigreturn_v2(env);
1726 } else {
1727 return do_sigreturn_v1(env);
1731 static long do_rt_sigreturn_v1(CPUARMState *env)
1733 abi_ulong frame_addr;
1734 struct rt_sigframe_v1 *frame;
1735 sigset_t host_set;
1738 * Since we stacked the signal on a 64-bit boundary,
1739 * then 'sp' should be word aligned here. If it's
1740 * not, then the user is trying to mess with us.
1742 if (env->regs[13] & 7)
1743 goto badframe;
1745 frame_addr = env->regs[13];
1746 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1747 goto badframe;
1749 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1750 sigprocmask(SIG_SETMASK, &host_set, NULL);
1752 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1753 goto badframe;
1755 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1756 goto badframe;
1758 #if 0
1759 /* Send SIGTRAP if we're single-stepping */
1760 if (ptrace_cancel_bpt(current))
1761 send_sig(SIGTRAP, current, 1);
1762 #endif
1763 unlock_user_struct(frame, frame_addr, 0);
1764 return env->regs[0];
1766 badframe:
1767 unlock_user_struct(frame, frame_addr, 0);
1768 force_sig(TARGET_SIGSEGV /* , current */);
1769 return 0;
1772 static long do_rt_sigreturn_v2(CPUARMState *env)
1774 abi_ulong frame_addr;
1775 struct rt_sigframe_v2 *frame;
1778 * Since we stacked the signal on a 64-bit boundary,
1779 * then 'sp' should be word aligned here. If it's
1780 * not, then the user is trying to mess with us.
1782 if (env->regs[13] & 7)
1783 goto badframe;
1785 frame_addr = env->regs[13];
1786 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1787 goto badframe;
1789 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1790 goto badframe;
1792 unlock_user_struct(frame, frame_addr, 0);
1793 return env->regs[0];
1795 badframe:
1796 unlock_user_struct(frame, frame_addr, 0);
1797 force_sig(TARGET_SIGSEGV /* , current */);
1798 return 0;
1801 long do_rt_sigreturn(CPUARMState *env)
1803 if (get_osversion() >= 0x020612) {
1804 return do_rt_sigreturn_v2(env);
1805 } else {
1806 return do_rt_sigreturn_v1(env);
1810 #elif defined(TARGET_SPARC)
1812 #define __SUNOS_MAXWIN 31
1814 /* This is what SunOS does, so shall I. */
1815 struct target_sigcontext {
1816 abi_ulong sigc_onstack; /* state to restore */
1818 abi_ulong sigc_mask; /* sigmask to restore */
1819 abi_ulong sigc_sp; /* stack pointer */
1820 abi_ulong sigc_pc; /* program counter */
1821 abi_ulong sigc_npc; /* next program counter */
1822 abi_ulong sigc_psr; /* for condition codes etc */
1823 abi_ulong sigc_g1; /* User uses these two registers */
1824 abi_ulong sigc_o0; /* within the trampoline code. */
1826 /* Now comes information regarding the users window set
1827 * at the time of the signal.
1829 abi_ulong sigc_oswins; /* outstanding windows */
1831 /* stack ptrs for each regwin buf */
1832 char *sigc_spbuf[__SUNOS_MAXWIN];
1834 /* Windows to restore after signal */
1835 struct {
1836 abi_ulong locals[8];
1837 abi_ulong ins[8];
1838 } sigc_wbuf[__SUNOS_MAXWIN];
1840 /* A Sparc stack frame */
1841 struct sparc_stackf {
1842 abi_ulong locals[8];
1843 abi_ulong ins[8];
1844 /* It's simpler to treat fp and callers_pc as elements of ins[]
1845 * since we never need to access them ourselves.
1847 char *structptr;
1848 abi_ulong xargs[6];
1849 abi_ulong xxargs[1];
1852 typedef struct {
1853 struct {
1854 abi_ulong psr;
1855 abi_ulong pc;
1856 abi_ulong npc;
1857 abi_ulong y;
1858 abi_ulong u_regs[16]; /* globals and ins */
1859 } si_regs;
1860 int si_mask;
1861 } __siginfo_t;
1863 typedef struct {
1864 abi_ulong si_float_regs[32];
1865 unsigned long si_fsr;
1866 unsigned long si_fpqdepth;
1867 struct {
1868 unsigned long *insn_addr;
1869 unsigned long insn;
1870 } si_fpqueue [16];
1871 } qemu_siginfo_fpu_t;
1874 struct target_signal_frame {
1875 struct sparc_stackf ss;
1876 __siginfo_t info;
1877 abi_ulong fpu_save;
1878 abi_ulong insns[2] __attribute__ ((aligned (8)));
1879 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1880 abi_ulong extra_size; /* Should be 0 */
1881 qemu_siginfo_fpu_t fpu_state;
1883 struct target_rt_signal_frame {
1884 struct sparc_stackf ss;
1885 siginfo_t info;
1886 abi_ulong regs[20];
1887 sigset_t mask;
1888 abi_ulong fpu_save;
1889 unsigned int insns[2];
1890 stack_t stack;
1891 unsigned int extra_size; /* Should be 0 */
1892 qemu_siginfo_fpu_t fpu_state;
1895 #define UREG_O0 16
1896 #define UREG_O6 22
1897 #define UREG_I0 0
1898 #define UREG_I1 1
1899 #define UREG_I2 2
1900 #define UREG_I3 3
1901 #define UREG_I4 4
1902 #define UREG_I5 5
1903 #define UREG_I6 6
1904 #define UREG_I7 7
1905 #define UREG_L0 8
1906 #define UREG_FP UREG_I6
1907 #define UREG_SP UREG_O6
1909 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1910 CPUSPARCState *env,
1911 unsigned long framesize)
1913 abi_ulong sp;
1915 sp = env->regwptr[UREG_FP];
1917 /* This is the X/Open sanctioned signal stack switching. */
1918 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1919 if (!on_sig_stack(sp)
1920 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1921 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1923 return sp - framesize;
1926 static int
1927 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
1929 int err = 0, i;
1931 err |= __put_user(env->psr, &si->si_regs.psr);
1932 err |= __put_user(env->pc, &si->si_regs.pc);
1933 err |= __put_user(env->npc, &si->si_regs.npc);
1934 err |= __put_user(env->y, &si->si_regs.y);
1935 for (i=0; i < 8; i++) {
1936 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1938 for (i=0; i < 8; i++) {
1939 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1941 err |= __put_user(mask, &si->si_mask);
1942 return err;
1945 #if 0
1946 static int
1947 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1948 CPUSPARCState *env, unsigned long mask)
1950 int err = 0;
1952 err |= __put_user(mask, &sc->sigc_mask);
1953 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1954 err |= __put_user(env->pc, &sc->sigc_pc);
1955 err |= __put_user(env->npc, &sc->sigc_npc);
1956 err |= __put_user(env->psr, &sc->sigc_psr);
1957 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1958 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1960 return err;
1962 #endif
1963 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1965 static void setup_frame(int sig, struct target_sigaction *ka,
1966 target_sigset_t *set, CPUSPARCState *env)
1968 abi_ulong sf_addr;
1969 struct target_signal_frame *sf;
1970 int sigframe_size, err, i;
1972 /* 1. Make sure everything is clean */
1973 //synchronize_user_stack();
1975 sigframe_size = NF_ALIGNEDSZ;
1976 sf_addr = get_sigframe(ka, env, sigframe_size);
1978 sf = lock_user(VERIFY_WRITE, sf_addr,
1979 sizeof(struct target_signal_frame), 0);
1980 if (!sf)
1981 goto sigsegv;
1983 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1984 #if 0
1985 if (invalid_frame_pointer(sf, sigframe_size))
1986 goto sigill_and_return;
1987 #endif
1988 /* 2. Save the current process state */
1989 err = setup___siginfo(&sf->info, env, set->sig[0]);
1990 err |= __put_user(0, &sf->extra_size);
1992 //err |= save_fpu_state(regs, &sf->fpu_state);
1993 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1995 err |= __put_user(set->sig[0], &sf->info.si_mask);
1996 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1997 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
2000 for (i = 0; i < 8; i++) {
2001 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2003 for (i = 0; i < 8; i++) {
2004 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2006 if (err)
2007 goto sigsegv;
2009 /* 3. signal handler back-trampoline and parameters */
2010 env->regwptr[UREG_FP] = sf_addr;
2011 env->regwptr[UREG_I0] = sig;
2012 env->regwptr[UREG_I1] = sf_addr +
2013 offsetof(struct target_signal_frame, info);
2014 env->regwptr[UREG_I2] = sf_addr +
2015 offsetof(struct target_signal_frame, info);
2017 /* 4. signal handler */
2018 env->pc = ka->_sa_handler;
2019 env->npc = (env->pc + 4);
2020 /* 5. return to kernel instructions */
2021 if (ka->sa_restorer)
2022 env->regwptr[UREG_I7] = ka->sa_restorer;
2023 else {
2024 uint32_t val32;
2026 env->regwptr[UREG_I7] = sf_addr +
2027 offsetof(struct target_signal_frame, insns) - 2 * 4;
2029 /* mov __NR_sigreturn, %g1 */
2030 val32 = 0x821020d8;
2031 err |= __put_user(val32, &sf->insns[0]);
2033 /* t 0x10 */
2034 val32 = 0x91d02010;
2035 err |= __put_user(val32, &sf->insns[1]);
2036 if (err)
2037 goto sigsegv;
2039 /* Flush instruction space. */
2040 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2041 // tb_flush(env);
2043 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2044 return;
2045 #if 0
2046 sigill_and_return:
2047 force_sig(TARGET_SIGILL);
2048 #endif
2049 sigsegv:
2050 //fprintf(stderr, "force_sig\n");
2051 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2052 force_sig(TARGET_SIGSEGV);
2054 static inline int
2055 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
2057 int err;
2058 #if 0
2059 #ifdef CONFIG_SMP
2060 if (current->flags & PF_USEDFPU)
2061 regs->psr &= ~PSR_EF;
2062 #else
2063 if (current == last_task_used_math) {
2064 last_task_used_math = 0;
2065 regs->psr &= ~PSR_EF;
2067 #endif
2068 current->used_math = 1;
2069 current->flags &= ~PF_USEDFPU;
2070 #endif
2071 #if 0
2072 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2073 return -EFAULT;
2074 #endif
2076 /* XXX: incorrect */
2077 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2078 (sizeof(abi_ulong) * 32));
2079 err |= __get_user(env->fsr, &fpu->si_fsr);
2080 #if 0
2081 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2082 if (current->thread.fpqdepth != 0)
2083 err |= __copy_from_user(&current->thread.fpqueue[0],
2084 &fpu->si_fpqueue[0],
2085 ((sizeof(unsigned long) +
2086 (sizeof(unsigned long *)))*16));
2087 #endif
2088 return err;
2092 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2093 target_siginfo_t *info,
2094 target_sigset_t *set, CPUSPARCState *env)
2096 fprintf(stderr, "setup_rt_frame: not implemented\n");
2099 long do_sigreturn(CPUSPARCState *env)
2101 abi_ulong sf_addr;
2102 struct target_signal_frame *sf;
2103 uint32_t up_psr, pc, npc;
2104 target_sigset_t set;
2105 sigset_t host_set;
2106 int err, i;
2108 sf_addr = env->regwptr[UREG_FP];
2109 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2110 goto segv_and_exit;
2111 #if 0
2112 fprintf(stderr, "sigreturn\n");
2113 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2114 #endif
2115 //cpu_dump_state(env, stderr, fprintf, 0);
2117 /* 1. Make sure we are not getting garbage from the user */
2119 if (sf_addr & 3)
2120 goto segv_and_exit;
2122 err = __get_user(pc, &sf->info.si_regs.pc);
2123 err |= __get_user(npc, &sf->info.si_regs.npc);
2125 if ((pc | npc) & 3)
2126 goto segv_and_exit;
2128 /* 2. Restore the state */
2129 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2131 /* User can only change condition codes and FPU enabling in %psr. */
2132 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2133 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2135 env->pc = pc;
2136 env->npc = npc;
2137 err |= __get_user(env->y, &sf->info.si_regs.y);
2138 for (i=0; i < 8; i++) {
2139 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2141 for (i=0; i < 8; i++) {
2142 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2145 /* FIXME: implement FPU save/restore:
2146 * __get_user(fpu_save, &sf->fpu_save);
2147 * if (fpu_save)
2148 * err |= restore_fpu_state(env, fpu_save);
2151 /* This is pretty much atomic, no amount locking would prevent
2152 * the races which exist anyways.
2154 err |= __get_user(set.sig[0], &sf->info.si_mask);
2155 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2156 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2159 target_to_host_sigset_internal(&host_set, &set);
2160 sigprocmask(SIG_SETMASK, &host_set, NULL);
2162 if (err)
2163 goto segv_and_exit;
2164 unlock_user_struct(sf, sf_addr, 0);
2165 return env->regwptr[0];
2167 segv_and_exit:
2168 unlock_user_struct(sf, sf_addr, 0);
2169 force_sig(TARGET_SIGSEGV);
2172 long do_rt_sigreturn(CPUSPARCState *env)
2174 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2175 return -TARGET_ENOSYS;
2178 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2179 #define MC_TSTATE 0
2180 #define MC_PC 1
2181 #define MC_NPC 2
2182 #define MC_Y 3
2183 #define MC_G1 4
2184 #define MC_G2 5
2185 #define MC_G3 6
2186 #define MC_G4 7
2187 #define MC_G5 8
2188 #define MC_G6 9
2189 #define MC_G7 10
2190 #define MC_O0 11
2191 #define MC_O1 12
2192 #define MC_O2 13
2193 #define MC_O3 14
2194 #define MC_O4 15
2195 #define MC_O5 16
2196 #define MC_O6 17
2197 #define MC_O7 18
2198 #define MC_NGREG 19
2200 typedef abi_ulong target_mc_greg_t;
2201 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2203 struct target_mc_fq {
2204 abi_ulong *mcfq_addr;
2205 uint32_t mcfq_insn;
2208 struct target_mc_fpu {
2209 union {
2210 uint32_t sregs[32];
2211 uint64_t dregs[32];
2212 //uint128_t qregs[16];
2213 } mcfpu_fregs;
2214 abi_ulong mcfpu_fsr;
2215 abi_ulong mcfpu_fprs;
2216 abi_ulong mcfpu_gsr;
2217 struct target_mc_fq *mcfpu_fq;
2218 unsigned char mcfpu_qcnt;
2219 unsigned char mcfpu_qentsz;
2220 unsigned char mcfpu_enab;
2222 typedef struct target_mc_fpu target_mc_fpu_t;
2224 typedef struct {
2225 target_mc_gregset_t mc_gregs;
2226 target_mc_greg_t mc_fp;
2227 target_mc_greg_t mc_i7;
2228 target_mc_fpu_t mc_fpregs;
2229 } target_mcontext_t;
2231 struct target_ucontext {
2232 struct target_ucontext *tuc_link;
2233 abi_ulong tuc_flags;
2234 target_sigset_t tuc_sigmask;
2235 target_mcontext_t tuc_mcontext;
2238 /* A V9 register window */
2239 struct target_reg_window {
2240 abi_ulong locals[8];
2241 abi_ulong ins[8];
2244 #define TARGET_STACK_BIAS 2047
2246 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2247 void sparc64_set_context(CPUSPARCState *env)
2249 abi_ulong ucp_addr;
2250 struct target_ucontext *ucp;
2251 target_mc_gregset_t *grp;
2252 abi_ulong pc, npc, tstate;
2253 abi_ulong fp, i7, w_addr;
2254 int err;
2255 unsigned int i;
2257 ucp_addr = env->regwptr[UREG_I0];
2258 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2259 goto do_sigsegv;
2260 grp = &ucp->tuc_mcontext.mc_gregs;
2261 err = __get_user(pc, &((*grp)[MC_PC]));
2262 err |= __get_user(npc, &((*grp)[MC_NPC]));
2263 if (err || ((pc | npc) & 3))
2264 goto do_sigsegv;
2265 if (env->regwptr[UREG_I1]) {
2266 target_sigset_t target_set;
2267 sigset_t set;
2269 if (TARGET_NSIG_WORDS == 1) {
2270 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2271 goto do_sigsegv;
2272 } else {
2273 abi_ulong *src, *dst;
2274 src = ucp->tuc_sigmask.sig;
2275 dst = target_set.sig;
2276 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2277 i++, dst++, src++)
2278 err |= __get_user(*dst, src);
2279 if (err)
2280 goto do_sigsegv;
2282 target_to_host_sigset_internal(&set, &target_set);
2283 sigprocmask(SIG_SETMASK, &set, NULL);
2285 env->pc = pc;
2286 env->npc = npc;
2287 err |= __get_user(env->y, &((*grp)[MC_Y]));
2288 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2289 env->asi = (tstate >> 24) & 0xff;
2290 cpu_put_ccr(env, tstate >> 32);
2291 cpu_put_cwp64(env, tstate & 0x1f);
2292 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2293 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2294 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2295 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2296 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2297 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2298 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2299 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2300 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2301 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2302 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2303 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2304 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2305 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2306 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2308 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2309 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2311 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2312 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2313 abi_ulong) != 0)
2314 goto do_sigsegv;
2315 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2316 abi_ulong) != 0)
2317 goto do_sigsegv;
2318 /* FIXME this does not match how the kernel handles the FPU in
2319 * its sparc64_set_context implementation. In particular the FPU
2320 * is only restored if fenab is non-zero in:
2321 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2323 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2325 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2326 for (i = 0; i < 64; i++, src++) {
2327 if (i & 1) {
2328 err |= __get_user(env->fpr[i/2].l.lower, src);
2329 } else {
2330 err |= __get_user(env->fpr[i/2].l.upper, src);
2334 err |= __get_user(env->fsr,
2335 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2336 err |= __get_user(env->gsr,
2337 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2338 if (err)
2339 goto do_sigsegv;
2340 unlock_user_struct(ucp, ucp_addr, 0);
2341 return;
2342 do_sigsegv:
2343 unlock_user_struct(ucp, ucp_addr, 0);
2344 force_sig(TARGET_SIGSEGV);
2347 void sparc64_get_context(CPUSPARCState *env)
2349 abi_ulong ucp_addr;
2350 struct target_ucontext *ucp;
2351 target_mc_gregset_t *grp;
2352 target_mcontext_t *mcp;
2353 abi_ulong fp, i7, w_addr;
2354 int err;
2355 unsigned int i;
2356 target_sigset_t target_set;
2357 sigset_t set;
2359 ucp_addr = env->regwptr[UREG_I0];
2360 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2361 goto do_sigsegv;
2363 mcp = &ucp->tuc_mcontext;
2364 grp = &mcp->mc_gregs;
2366 /* Skip over the trap instruction, first. */
2367 env->pc = env->npc;
2368 env->npc += 4;
2370 err = 0;
2372 sigprocmask(0, NULL, &set);
2373 host_to_target_sigset_internal(&target_set, &set);
2374 if (TARGET_NSIG_WORDS == 1) {
2375 err |= __put_user(target_set.sig[0],
2376 (abi_ulong *)&ucp->tuc_sigmask);
2377 } else {
2378 abi_ulong *src, *dst;
2379 src = target_set.sig;
2380 dst = ucp->tuc_sigmask.sig;
2381 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2382 i++, dst++, src++)
2383 err |= __put_user(*src, dst);
2384 if (err)
2385 goto do_sigsegv;
2388 /* XXX: tstate must be saved properly */
2389 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2390 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2391 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2392 err |= __put_user(env->y, &((*grp)[MC_Y]));
2393 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2394 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2395 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2396 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2397 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2398 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2399 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2400 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2401 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2402 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2403 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2404 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2405 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2406 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2407 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2409 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2410 fp = i7 = 0;
2411 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2412 abi_ulong) != 0)
2413 goto do_sigsegv;
2414 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2415 abi_ulong) != 0)
2416 goto do_sigsegv;
2417 err |= __put_user(fp, &(mcp->mc_fp));
2418 err |= __put_user(i7, &(mcp->mc_i7));
2421 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2422 for (i = 0; i < 64; i++, dst++) {
2423 if (i & 1) {
2424 err |= __put_user(env->fpr[i/2].l.lower, dst);
2425 } else {
2426 err |= __put_user(env->fpr[i/2].l.upper, dst);
2430 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2431 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2432 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2434 if (err)
2435 goto do_sigsegv;
2436 unlock_user_struct(ucp, ucp_addr, 1);
2437 return;
2438 do_sigsegv:
2439 unlock_user_struct(ucp, ucp_addr, 1);
2440 force_sig(TARGET_SIGSEGV);
2442 #endif
2443 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2445 # if defined(TARGET_ABI_MIPSO32)
2446 struct target_sigcontext {
2447 uint32_t sc_regmask; /* Unused */
2448 uint32_t sc_status;
2449 uint64_t sc_pc;
2450 uint64_t sc_regs[32];
2451 uint64_t sc_fpregs[32];
2452 uint32_t sc_ownedfp; /* Unused */
2453 uint32_t sc_fpc_csr;
2454 uint32_t sc_fpc_eir; /* Unused */
2455 uint32_t sc_used_math;
2456 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2457 uint32_t pad0;
2458 uint64_t sc_mdhi;
2459 uint64_t sc_mdlo;
2460 target_ulong sc_hi1; /* Was sc_cause */
2461 target_ulong sc_lo1; /* Was sc_badvaddr */
2462 target_ulong sc_hi2; /* Was sc_sigset[4] */
2463 target_ulong sc_lo2;
2464 target_ulong sc_hi3;
2465 target_ulong sc_lo3;
2467 # else /* N32 || N64 */
2468 struct target_sigcontext {
2469 uint64_t sc_regs[32];
2470 uint64_t sc_fpregs[32];
2471 uint64_t sc_mdhi;
2472 uint64_t sc_hi1;
2473 uint64_t sc_hi2;
2474 uint64_t sc_hi3;
2475 uint64_t sc_mdlo;
2476 uint64_t sc_lo1;
2477 uint64_t sc_lo2;
2478 uint64_t sc_lo3;
2479 uint64_t sc_pc;
2480 uint32_t sc_fpc_csr;
2481 uint32_t sc_used_math;
2482 uint32_t sc_dsp;
2483 uint32_t sc_reserved;
2485 # endif /* O32 */
2487 struct sigframe {
2488 uint32_t sf_ass[4]; /* argument save space for o32 */
2489 uint32_t sf_code[2]; /* signal trampoline */
2490 struct target_sigcontext sf_sc;
2491 target_sigset_t sf_mask;
2494 struct target_ucontext {
2495 target_ulong tuc_flags;
2496 target_ulong tuc_link;
2497 target_stack_t tuc_stack;
2498 target_ulong pad0;
2499 struct target_sigcontext tuc_mcontext;
2500 target_sigset_t tuc_sigmask;
2503 struct target_rt_sigframe {
2504 uint32_t rs_ass[4]; /* argument save space for o32 */
2505 uint32_t rs_code[2]; /* signal trampoline */
2506 struct target_siginfo rs_info;
2507 struct target_ucontext rs_uc;
2510 /* Install trampoline to jump back from signal handler */
2511 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2513 int err = 0;
2516 * Set up the return code ...
2518 * li v0, __NR__foo_sigreturn
2519 * syscall
2522 err |= __put_user(0x24020000 + syscall, tramp + 0);
2523 err |= __put_user(0x0000000c , tramp + 1);
2524 return err;
2527 static inline int
2528 setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2530 int err = 0;
2531 int i;
2533 err |= __put_user(exception_resume_pc(regs), &sc->sc_pc);
2534 regs->hflags &= ~MIPS_HFLAG_BMASK;
2536 __put_user(0, &sc->sc_regs[0]);
2537 for (i = 1; i < 32; ++i) {
2538 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2541 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2542 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2544 /* Rather than checking for dsp existence, always copy. The storage
2545 would just be garbage otherwise. */
2546 err |= __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2547 err |= __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2548 err |= __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2549 err |= __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2550 err |= __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2551 err |= __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2553 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2554 err |= __put_user(dsp, &sc->sc_dsp);
2557 err |= __put_user(1, &sc->sc_used_math);
2559 for (i = 0; i < 32; ++i) {
2560 err |= __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2563 return err;
2566 static inline int
2567 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2569 int err = 0;
2570 int i;
2572 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2574 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2575 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2577 for (i = 1; i < 32; ++i) {
2578 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2581 err |= __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2582 err |= __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2583 err |= __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2584 err |= __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2585 err |= __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2586 err |= __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2588 uint32_t dsp;
2589 err |= __get_user(dsp, &sc->sc_dsp);
2590 cpu_wrdsp(dsp, 0x3ff, regs);
2593 for (i = 0; i < 32; ++i) {
2594 err |= __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2597 return err;
2601 * Determine which stack to use..
2603 static inline abi_ulong
2604 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
2606 unsigned long sp;
2608 /* Default to using normal stack */
2609 sp = regs->active_tc.gpr[29];
2612 * FPU emulator may have its own trampoline active just
2613 * above the user stack, 16-bytes before the next lowest
2614 * 16 byte boundary. Try to avoid trashing it.
2616 sp -= 32;
2618 /* This is the X/Open sanctioned signal stack switching. */
2619 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2620 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2623 return (sp - frame_size) & ~7;
2626 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2628 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2629 env->hflags &= ~MIPS_HFLAG_M16;
2630 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2631 env->active_tc.PC &= ~(target_ulong) 1;
2635 # if defined(TARGET_ABI_MIPSO32)
2636 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2637 static void setup_frame(int sig, struct target_sigaction * ka,
2638 target_sigset_t *set, CPUMIPSState *regs)
2640 struct sigframe *frame;
2641 abi_ulong frame_addr;
2642 int i;
2644 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2645 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2646 goto give_sigsegv;
2648 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2650 if(setup_sigcontext(regs, &frame->sf_sc))
2651 goto give_sigsegv;
2653 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2654 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2655 goto give_sigsegv;
2659 * Arguments to signal handler:
2661 * a0 = signal number
2662 * a1 = 0 (should be cause)
2663 * a2 = pointer to struct sigcontext
2665 * $25 and PC point to the signal handler, $29 points to the
2666 * struct sigframe.
2668 regs->active_tc.gpr[ 4] = sig;
2669 regs->active_tc.gpr[ 5] = 0;
2670 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2671 regs->active_tc.gpr[29] = frame_addr;
2672 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2673 /* The original kernel code sets CP0_EPC to the handler
2674 * since it returns to userland using eret
2675 * we cannot do this here, and we must set PC directly */
2676 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2677 mips_set_hflags_isa_mode_from_pc(regs);
2678 unlock_user_struct(frame, frame_addr, 1);
2679 return;
2681 give_sigsegv:
2682 unlock_user_struct(frame, frame_addr, 1);
2683 force_sig(TARGET_SIGSEGV/*, current*/);
2686 long do_sigreturn(CPUMIPSState *regs)
2688 struct sigframe *frame;
2689 abi_ulong frame_addr;
2690 sigset_t blocked;
2691 target_sigset_t target_set;
2692 int i;
2694 #if defined(DEBUG_SIGNAL)
2695 fprintf(stderr, "do_sigreturn\n");
2696 #endif
2697 frame_addr = regs->active_tc.gpr[29];
2698 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2699 goto badframe;
2701 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2702 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2703 goto badframe;
2706 target_to_host_sigset_internal(&blocked, &target_set);
2707 sigprocmask(SIG_SETMASK, &blocked, NULL);
2709 if (restore_sigcontext(regs, &frame->sf_sc))
2710 goto badframe;
2712 #if 0
2714 * Don't let your children do this ...
2716 __asm__ __volatile__(
2717 "move\t$29, %0\n\t"
2718 "j\tsyscall_exit"
2719 :/* no outputs */
2720 :"r" (&regs));
2721 /* Unreached */
2722 #endif
2724 regs->active_tc.PC = regs->CP0_EPC;
2725 mips_set_hflags_isa_mode_from_pc(regs);
2726 /* I am not sure this is right, but it seems to work
2727 * maybe a problem with nested signals ? */
2728 regs->CP0_EPC = 0;
2729 return -TARGET_QEMU_ESIGRETURN;
2731 badframe:
2732 force_sig(TARGET_SIGSEGV/*, current*/);
2733 return 0;
2735 # endif /* O32 */
2737 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2738 target_siginfo_t *info,
2739 target_sigset_t *set, CPUMIPSState *env)
2741 struct target_rt_sigframe *frame;
2742 abi_ulong frame_addr;
2743 int i;
2745 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2746 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2747 goto give_sigsegv;
2749 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2751 copy_siginfo_to_user(&frame->rs_info, info);
2753 __put_user(0, &frame->rs_uc.tuc_flags);
2754 __put_user(0, &frame->rs_uc.tuc_link);
2755 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2756 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2757 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2758 &frame->rs_uc.tuc_stack.ss_flags);
2760 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2762 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2763 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2767 * Arguments to signal handler:
2769 * a0 = signal number
2770 * a1 = pointer to siginfo_t
2771 * a2 = pointer to struct ucontext
2773 * $25 and PC point to the signal handler, $29 points to the
2774 * struct sigframe.
2776 env->active_tc.gpr[ 4] = sig;
2777 env->active_tc.gpr[ 5] = frame_addr
2778 + offsetof(struct target_rt_sigframe, rs_info);
2779 env->active_tc.gpr[ 6] = frame_addr
2780 + offsetof(struct target_rt_sigframe, rs_uc);
2781 env->active_tc.gpr[29] = frame_addr;
2782 env->active_tc.gpr[31] = frame_addr
2783 + offsetof(struct target_rt_sigframe, rs_code);
2784 /* The original kernel code sets CP0_EPC to the handler
2785 * since it returns to userland using eret
2786 * we cannot do this here, and we must set PC directly */
2787 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2788 mips_set_hflags_isa_mode_from_pc(env);
2789 unlock_user_struct(frame, frame_addr, 1);
2790 return;
2792 give_sigsegv:
2793 unlock_user_struct(frame, frame_addr, 1);
2794 force_sig(TARGET_SIGSEGV/*, current*/);
2797 long do_rt_sigreturn(CPUMIPSState *env)
2799 struct target_rt_sigframe *frame;
2800 abi_ulong frame_addr;
2801 sigset_t blocked;
2803 #if defined(DEBUG_SIGNAL)
2804 fprintf(stderr, "do_rt_sigreturn\n");
2805 #endif
2806 frame_addr = env->active_tc.gpr[29];
2807 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2808 goto badframe;
2810 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2811 sigprocmask(SIG_SETMASK, &blocked, NULL);
2813 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2814 goto badframe;
2816 if (do_sigaltstack(frame_addr +
2817 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2818 0, get_sp_from_cpustate(env)) == -EFAULT)
2819 goto badframe;
2821 env->active_tc.PC = env->CP0_EPC;
2822 mips_set_hflags_isa_mode_from_pc(env);
2823 /* I am not sure this is right, but it seems to work
2824 * maybe a problem with nested signals ? */
2825 env->CP0_EPC = 0;
2826 return -TARGET_QEMU_ESIGRETURN;
2828 badframe:
2829 force_sig(TARGET_SIGSEGV/*, current*/);
2830 return 0;
2833 #elif defined(TARGET_SH4)
2836 * code and data structures from linux kernel:
2837 * include/asm-sh/sigcontext.h
2838 * arch/sh/kernel/signal.c
2841 struct target_sigcontext {
2842 target_ulong oldmask;
2844 /* CPU registers */
2845 target_ulong sc_gregs[16];
2846 target_ulong sc_pc;
2847 target_ulong sc_pr;
2848 target_ulong sc_sr;
2849 target_ulong sc_gbr;
2850 target_ulong sc_mach;
2851 target_ulong sc_macl;
2853 /* FPU registers */
2854 target_ulong sc_fpregs[16];
2855 target_ulong sc_xfpregs[16];
2856 unsigned int sc_fpscr;
2857 unsigned int sc_fpul;
2858 unsigned int sc_ownedfp;
2861 struct target_sigframe
2863 struct target_sigcontext sc;
2864 target_ulong extramask[TARGET_NSIG_WORDS-1];
2865 uint16_t retcode[3];
2869 struct target_ucontext {
2870 target_ulong tuc_flags;
2871 struct target_ucontext *tuc_link;
2872 target_stack_t tuc_stack;
2873 struct target_sigcontext tuc_mcontext;
2874 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2877 struct target_rt_sigframe
2879 struct target_siginfo info;
2880 struct target_ucontext uc;
2881 uint16_t retcode[3];
2885 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2886 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2888 static abi_ulong get_sigframe(struct target_sigaction *ka,
2889 unsigned long sp, size_t frame_size)
2891 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2892 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2895 return (sp - frame_size) & -8ul;
2898 static int setup_sigcontext(struct target_sigcontext *sc,
2899 CPUSH4State *regs, unsigned long mask)
2901 int err = 0;
2902 int i;
2904 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2905 COPY(gregs[0]); COPY(gregs[1]);
2906 COPY(gregs[2]); COPY(gregs[3]);
2907 COPY(gregs[4]); COPY(gregs[5]);
2908 COPY(gregs[6]); COPY(gregs[7]);
2909 COPY(gregs[8]); COPY(gregs[9]);
2910 COPY(gregs[10]); COPY(gregs[11]);
2911 COPY(gregs[12]); COPY(gregs[13]);
2912 COPY(gregs[14]); COPY(gregs[15]);
2913 COPY(gbr); COPY(mach);
2914 COPY(macl); COPY(pr);
2915 COPY(sr); COPY(pc);
2916 #undef COPY
2918 for (i=0; i<16; i++) {
2919 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2921 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2922 err |= __put_user(regs->fpul, &sc->sc_fpul);
2924 /* non-iBCS2 extensions.. */
2925 err |= __put_user(mask, &sc->oldmask);
2927 return err;
2930 static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
2931 target_ulong *r0_p)
2933 unsigned int err = 0;
2934 int i;
2936 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2937 COPY(gregs[1]);
2938 COPY(gregs[2]); COPY(gregs[3]);
2939 COPY(gregs[4]); COPY(gregs[5]);
2940 COPY(gregs[6]); COPY(gregs[7]);
2941 COPY(gregs[8]); COPY(gregs[9]);
2942 COPY(gregs[10]); COPY(gregs[11]);
2943 COPY(gregs[12]); COPY(gregs[13]);
2944 COPY(gregs[14]); COPY(gregs[15]);
2945 COPY(gbr); COPY(mach);
2946 COPY(macl); COPY(pr);
2947 COPY(sr); COPY(pc);
2948 #undef COPY
2950 for (i=0; i<16; i++) {
2951 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
2953 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
2954 err |= __get_user(regs->fpul, &sc->sc_fpul);
2956 regs->tra = -1; /* disable syscall checks */
2957 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
2958 return err;
2961 static void setup_frame(int sig, struct target_sigaction *ka,
2962 target_sigset_t *set, CPUSH4State *regs)
2964 struct target_sigframe *frame;
2965 abi_ulong frame_addr;
2966 int i;
2967 int err = 0;
2968 int signal;
2970 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2971 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2972 goto give_sigsegv;
2974 signal = current_exec_domain_sig(sig);
2976 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2978 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2979 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2982 /* Set up to return from userspace. If provided, use a stub
2983 already in userspace. */
2984 if (ka->sa_flags & TARGET_SA_RESTORER) {
2985 regs->pr = (unsigned long) ka->sa_restorer;
2986 } else {
2987 /* Generate return code (system call to sigreturn) */
2988 err |= __put_user(MOVW(2), &frame->retcode[0]);
2989 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2990 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2991 regs->pr = (unsigned long) frame->retcode;
2994 if (err)
2995 goto give_sigsegv;
2997 /* Set up registers for signal handler */
2998 regs->gregs[15] = frame_addr;
2999 regs->gregs[4] = signal; /* Arg for signal handler */
3000 regs->gregs[5] = 0;
3001 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3002 regs->pc = (unsigned long) ka->_sa_handler;
3004 unlock_user_struct(frame, frame_addr, 1);
3005 return;
3007 give_sigsegv:
3008 unlock_user_struct(frame, frame_addr, 1);
3009 force_sig(TARGET_SIGSEGV);
3012 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3013 target_siginfo_t *info,
3014 target_sigset_t *set, CPUSH4State *regs)
3016 struct target_rt_sigframe *frame;
3017 abi_ulong frame_addr;
3018 int i;
3019 int err = 0;
3020 int signal;
3022 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3023 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3024 goto give_sigsegv;
3026 signal = current_exec_domain_sig(sig);
3028 err |= copy_siginfo_to_user(&frame->info, info);
3030 /* Create the ucontext. */
3031 err |= __put_user(0, &frame->uc.tuc_flags);
3032 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3033 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3034 &frame->uc.tuc_stack.ss_sp);
3035 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3036 &frame->uc.tuc_stack.ss_flags);
3037 err |= __put_user(target_sigaltstack_used.ss_size,
3038 &frame->uc.tuc_stack.ss_size);
3039 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3040 regs, set->sig[0]);
3041 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3042 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3045 /* Set up to return from userspace. If provided, use a stub
3046 already in userspace. */
3047 if (ka->sa_flags & TARGET_SA_RESTORER) {
3048 regs->pr = (unsigned long) ka->sa_restorer;
3049 } else {
3050 /* Generate return code (system call to sigreturn) */
3051 err |= __put_user(MOVW(2), &frame->retcode[0]);
3052 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3053 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3054 regs->pr = (unsigned long) frame->retcode;
3057 if (err)
3058 goto give_sigsegv;
3060 /* Set up registers for signal handler */
3061 regs->gregs[15] = frame_addr;
3062 regs->gregs[4] = signal; /* Arg for signal handler */
3063 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3064 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3065 regs->pc = (unsigned long) ka->_sa_handler;
3067 unlock_user_struct(frame, frame_addr, 1);
3068 return;
3070 give_sigsegv:
3071 unlock_user_struct(frame, frame_addr, 1);
3072 force_sig(TARGET_SIGSEGV);
3075 long do_sigreturn(CPUSH4State *regs)
3077 struct target_sigframe *frame;
3078 abi_ulong frame_addr;
3079 sigset_t blocked;
3080 target_sigset_t target_set;
3081 target_ulong r0;
3082 int i;
3083 int err = 0;
3085 #if defined(DEBUG_SIGNAL)
3086 fprintf(stderr, "do_sigreturn\n");
3087 #endif
3088 frame_addr = regs->gregs[15];
3089 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3090 goto badframe;
3092 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3093 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3094 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3097 if (err)
3098 goto badframe;
3100 target_to_host_sigset_internal(&blocked, &target_set);
3101 sigprocmask(SIG_SETMASK, &blocked, NULL);
3103 if (restore_sigcontext(regs, &frame->sc, &r0))
3104 goto badframe;
3106 unlock_user_struct(frame, frame_addr, 0);
3107 return r0;
3109 badframe:
3110 unlock_user_struct(frame, frame_addr, 0);
3111 force_sig(TARGET_SIGSEGV);
3112 return 0;
3115 long do_rt_sigreturn(CPUSH4State *regs)
3117 struct target_rt_sigframe *frame;
3118 abi_ulong frame_addr;
3119 sigset_t blocked;
3120 target_ulong r0;
3122 #if defined(DEBUG_SIGNAL)
3123 fprintf(stderr, "do_rt_sigreturn\n");
3124 #endif
3125 frame_addr = regs->gregs[15];
3126 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3127 goto badframe;
3129 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3130 sigprocmask(SIG_SETMASK, &blocked, NULL);
3132 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3133 goto badframe;
3135 if (do_sigaltstack(frame_addr +
3136 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3137 0, get_sp_from_cpustate(regs)) == -EFAULT)
3138 goto badframe;
3140 unlock_user_struct(frame, frame_addr, 0);
3141 return r0;
3143 badframe:
3144 unlock_user_struct(frame, frame_addr, 0);
3145 force_sig(TARGET_SIGSEGV);
3146 return 0;
3148 #elif defined(TARGET_MICROBLAZE)
3150 struct target_sigcontext {
3151 struct target_pt_regs regs; /* needs to be first */
3152 uint32_t oldmask;
3155 struct target_stack_t {
3156 abi_ulong ss_sp;
3157 int ss_flags;
3158 unsigned int ss_size;
3161 struct target_ucontext {
3162 abi_ulong tuc_flags;
3163 abi_ulong tuc_link;
3164 struct target_stack_t tuc_stack;
3165 struct target_sigcontext tuc_mcontext;
3166 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3169 /* Signal frames. */
3170 struct target_signal_frame {
3171 struct target_ucontext uc;
3172 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3173 uint32_t tramp[2];
3176 struct rt_signal_frame {
3177 siginfo_t info;
3178 struct ucontext uc;
3179 uint32_t tramp[2];
3182 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3184 __put_user(env->regs[0], &sc->regs.r0);
3185 __put_user(env->regs[1], &sc->regs.r1);
3186 __put_user(env->regs[2], &sc->regs.r2);
3187 __put_user(env->regs[3], &sc->regs.r3);
3188 __put_user(env->regs[4], &sc->regs.r4);
3189 __put_user(env->regs[5], &sc->regs.r5);
3190 __put_user(env->regs[6], &sc->regs.r6);
3191 __put_user(env->regs[7], &sc->regs.r7);
3192 __put_user(env->regs[8], &sc->regs.r8);
3193 __put_user(env->regs[9], &sc->regs.r9);
3194 __put_user(env->regs[10], &sc->regs.r10);
3195 __put_user(env->regs[11], &sc->regs.r11);
3196 __put_user(env->regs[12], &sc->regs.r12);
3197 __put_user(env->regs[13], &sc->regs.r13);
3198 __put_user(env->regs[14], &sc->regs.r14);
3199 __put_user(env->regs[15], &sc->regs.r15);
3200 __put_user(env->regs[16], &sc->regs.r16);
3201 __put_user(env->regs[17], &sc->regs.r17);
3202 __put_user(env->regs[18], &sc->regs.r18);
3203 __put_user(env->regs[19], &sc->regs.r19);
3204 __put_user(env->regs[20], &sc->regs.r20);
3205 __put_user(env->regs[21], &sc->regs.r21);
3206 __put_user(env->regs[22], &sc->regs.r22);
3207 __put_user(env->regs[23], &sc->regs.r23);
3208 __put_user(env->regs[24], &sc->regs.r24);
3209 __put_user(env->regs[25], &sc->regs.r25);
3210 __put_user(env->regs[26], &sc->regs.r26);
3211 __put_user(env->regs[27], &sc->regs.r27);
3212 __put_user(env->regs[28], &sc->regs.r28);
3213 __put_user(env->regs[29], &sc->regs.r29);
3214 __put_user(env->regs[30], &sc->regs.r30);
3215 __put_user(env->regs[31], &sc->regs.r31);
3216 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3219 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3221 __get_user(env->regs[0], &sc->regs.r0);
3222 __get_user(env->regs[1], &sc->regs.r1);
3223 __get_user(env->regs[2], &sc->regs.r2);
3224 __get_user(env->regs[3], &sc->regs.r3);
3225 __get_user(env->regs[4], &sc->regs.r4);
3226 __get_user(env->regs[5], &sc->regs.r5);
3227 __get_user(env->regs[6], &sc->regs.r6);
3228 __get_user(env->regs[7], &sc->regs.r7);
3229 __get_user(env->regs[8], &sc->regs.r8);
3230 __get_user(env->regs[9], &sc->regs.r9);
3231 __get_user(env->regs[10], &sc->regs.r10);
3232 __get_user(env->regs[11], &sc->regs.r11);
3233 __get_user(env->regs[12], &sc->regs.r12);
3234 __get_user(env->regs[13], &sc->regs.r13);
3235 __get_user(env->regs[14], &sc->regs.r14);
3236 __get_user(env->regs[15], &sc->regs.r15);
3237 __get_user(env->regs[16], &sc->regs.r16);
3238 __get_user(env->regs[17], &sc->regs.r17);
3239 __get_user(env->regs[18], &sc->regs.r18);
3240 __get_user(env->regs[19], &sc->regs.r19);
3241 __get_user(env->regs[20], &sc->regs.r20);
3242 __get_user(env->regs[21], &sc->regs.r21);
3243 __get_user(env->regs[22], &sc->regs.r22);
3244 __get_user(env->regs[23], &sc->regs.r23);
3245 __get_user(env->regs[24], &sc->regs.r24);
3246 __get_user(env->regs[25], &sc->regs.r25);
3247 __get_user(env->regs[26], &sc->regs.r26);
3248 __get_user(env->regs[27], &sc->regs.r27);
3249 __get_user(env->regs[28], &sc->regs.r28);
3250 __get_user(env->regs[29], &sc->regs.r29);
3251 __get_user(env->regs[30], &sc->regs.r30);
3252 __get_user(env->regs[31], &sc->regs.r31);
3253 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3256 static abi_ulong get_sigframe(struct target_sigaction *ka,
3257 CPUMBState *env, int frame_size)
3259 abi_ulong sp = env->regs[1];
3261 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3262 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3264 return ((sp - frame_size) & -8UL);
3267 static void setup_frame(int sig, struct target_sigaction *ka,
3268 target_sigset_t *set, CPUMBState *env)
3270 struct target_signal_frame *frame;
3271 abi_ulong frame_addr;
3272 int err = 0;
3273 int i;
3275 frame_addr = get_sigframe(ka, env, sizeof *frame);
3276 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3277 goto badframe;
3279 /* Save the mask. */
3280 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3281 if (err)
3282 goto badframe;
3284 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3285 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3286 goto badframe;
3289 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3291 /* Set up to return from userspace. If provided, use a stub
3292 already in userspace. */
3293 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3294 if (ka->sa_flags & TARGET_SA_RESTORER) {
3295 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3296 } else {
3297 uint32_t t;
3298 /* Note, these encodings are _big endian_! */
3299 /* addi r12, r0, __NR_sigreturn */
3300 t = 0x31800000UL | TARGET_NR_sigreturn;
3301 err |= __put_user(t, frame->tramp + 0);
3302 /* brki r14, 0x8 */
3303 t = 0xb9cc0008UL;
3304 err |= __put_user(t, frame->tramp + 1);
3306 /* Return from sighandler will jump to the tramp.
3307 Negative 8 offset because return is rtsd r15, 8 */
3308 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3311 if (err)
3312 goto badframe;
3314 /* Set up registers for signal handler */
3315 env->regs[1] = frame_addr;
3316 /* Signal handler args: */
3317 env->regs[5] = sig; /* Arg 0: signum */
3318 env->regs[6] = 0;
3319 /* arg 1: sigcontext */
3320 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3322 /* Offset of 4 to handle microblaze rtid r14, 0 */
3323 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3325 unlock_user_struct(frame, frame_addr, 1);
3326 return;
3327 badframe:
3328 unlock_user_struct(frame, frame_addr, 1);
3329 force_sig(TARGET_SIGSEGV);
3332 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3333 target_siginfo_t *info,
3334 target_sigset_t *set, CPUMBState *env)
3336 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3339 long do_sigreturn(CPUMBState *env)
3341 struct target_signal_frame *frame;
3342 abi_ulong frame_addr;
3343 target_sigset_t target_set;
3344 sigset_t set;
3345 int i;
3347 frame_addr = env->regs[R_SP];
3348 /* Make sure the guest isn't playing games. */
3349 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3350 goto badframe;
3352 /* Restore blocked signals */
3353 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3354 goto badframe;
3355 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3356 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3357 goto badframe;
3359 target_to_host_sigset_internal(&set, &target_set);
3360 sigprocmask(SIG_SETMASK, &set, NULL);
3362 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3363 /* We got here through a sigreturn syscall, our path back is via an
3364 rtb insn so setup r14 for that. */
3365 env->regs[14] = env->sregs[SR_PC];
3367 unlock_user_struct(frame, frame_addr, 0);
3368 return env->regs[10];
3369 badframe:
3370 unlock_user_struct(frame, frame_addr, 0);
3371 force_sig(TARGET_SIGSEGV);
3374 long do_rt_sigreturn(CPUMBState *env)
3376 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3377 return -TARGET_ENOSYS;
3380 #elif defined(TARGET_CRIS)
3382 struct target_sigcontext {
3383 struct target_pt_regs regs; /* needs to be first */
3384 uint32_t oldmask;
3385 uint32_t usp; /* usp before stacking this gunk on it */
3388 /* Signal frames. */
3389 struct target_signal_frame {
3390 struct target_sigcontext sc;
3391 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3392 uint8_t retcode[8]; /* Trampoline code. */
3395 struct rt_signal_frame {
3396 siginfo_t *pinfo;
3397 void *puc;
3398 siginfo_t info;
3399 struct ucontext uc;
3400 uint8_t retcode[8]; /* Trampoline code. */
3403 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3405 __put_user(env->regs[0], &sc->regs.r0);
3406 __put_user(env->regs[1], &sc->regs.r1);
3407 __put_user(env->regs[2], &sc->regs.r2);
3408 __put_user(env->regs[3], &sc->regs.r3);
3409 __put_user(env->regs[4], &sc->regs.r4);
3410 __put_user(env->regs[5], &sc->regs.r5);
3411 __put_user(env->regs[6], &sc->regs.r6);
3412 __put_user(env->regs[7], &sc->regs.r7);
3413 __put_user(env->regs[8], &sc->regs.r8);
3414 __put_user(env->regs[9], &sc->regs.r9);
3415 __put_user(env->regs[10], &sc->regs.r10);
3416 __put_user(env->regs[11], &sc->regs.r11);
3417 __put_user(env->regs[12], &sc->regs.r12);
3418 __put_user(env->regs[13], &sc->regs.r13);
3419 __put_user(env->regs[14], &sc->usp);
3420 __put_user(env->regs[15], &sc->regs.acr);
3421 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3422 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3423 __put_user(env->pc, &sc->regs.erp);
3426 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3428 __get_user(env->regs[0], &sc->regs.r0);
3429 __get_user(env->regs[1], &sc->regs.r1);
3430 __get_user(env->regs[2], &sc->regs.r2);
3431 __get_user(env->regs[3], &sc->regs.r3);
3432 __get_user(env->regs[4], &sc->regs.r4);
3433 __get_user(env->regs[5], &sc->regs.r5);
3434 __get_user(env->regs[6], &sc->regs.r6);
3435 __get_user(env->regs[7], &sc->regs.r7);
3436 __get_user(env->regs[8], &sc->regs.r8);
3437 __get_user(env->regs[9], &sc->regs.r9);
3438 __get_user(env->regs[10], &sc->regs.r10);
3439 __get_user(env->regs[11], &sc->regs.r11);
3440 __get_user(env->regs[12], &sc->regs.r12);
3441 __get_user(env->regs[13], &sc->regs.r13);
3442 __get_user(env->regs[14], &sc->usp);
3443 __get_user(env->regs[15], &sc->regs.acr);
3444 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3445 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3446 __get_user(env->pc, &sc->regs.erp);
3449 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3451 abi_ulong sp;
3452 /* Align the stack downwards to 4. */
3453 sp = (env->regs[R_SP] & ~3);
3454 return sp - framesize;
3457 static void setup_frame(int sig, struct target_sigaction *ka,
3458 target_sigset_t *set, CPUCRISState *env)
3460 struct target_signal_frame *frame;
3461 abi_ulong frame_addr;
3462 int err = 0;
3463 int i;
3465 frame_addr = get_sigframe(env, sizeof *frame);
3466 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3467 goto badframe;
3470 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3471 * use this trampoline anymore but it sets it up for GDB.
3472 * In QEMU, using the trampoline simplifies things a bit so we use it.
3474 * This is movu.w __NR_sigreturn, r9; break 13;
3476 err |= __put_user(0x9c5f, frame->retcode+0);
3477 err |= __put_user(TARGET_NR_sigreturn,
3478 frame->retcode+2);
3479 err |= __put_user(0xe93d, frame->retcode+4);
3481 /* Save the mask. */
3482 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3483 if (err)
3484 goto badframe;
3486 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3487 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3488 goto badframe;
3491 setup_sigcontext(&frame->sc, env);
3493 /* Move the stack and setup the arguments for the handler. */
3494 env->regs[R_SP] = frame_addr;
3495 env->regs[10] = sig;
3496 env->pc = (unsigned long) ka->_sa_handler;
3497 /* Link SRP so the guest returns through the trampoline. */
3498 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3500 unlock_user_struct(frame, frame_addr, 1);
3501 return;
3502 badframe:
3503 unlock_user_struct(frame, frame_addr, 1);
3504 force_sig(TARGET_SIGSEGV);
3507 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3508 target_siginfo_t *info,
3509 target_sigset_t *set, CPUCRISState *env)
3511 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3514 long do_sigreturn(CPUCRISState *env)
3516 struct target_signal_frame *frame;
3517 abi_ulong frame_addr;
3518 target_sigset_t target_set;
3519 sigset_t set;
3520 int i;
3522 frame_addr = env->regs[R_SP];
3523 /* Make sure the guest isn't playing games. */
3524 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3525 goto badframe;
3527 /* Restore blocked signals */
3528 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3529 goto badframe;
3530 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3531 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3532 goto badframe;
3534 target_to_host_sigset_internal(&set, &target_set);
3535 sigprocmask(SIG_SETMASK, &set, NULL);
3537 restore_sigcontext(&frame->sc, env);
3538 unlock_user_struct(frame, frame_addr, 0);
3539 return env->regs[10];
3540 badframe:
3541 unlock_user_struct(frame, frame_addr, 0);
3542 force_sig(TARGET_SIGSEGV);
3545 long do_rt_sigreturn(CPUCRISState *env)
3547 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3548 return -TARGET_ENOSYS;
3551 #elif defined(TARGET_OPENRISC)
3553 struct target_sigcontext {
3554 struct target_pt_regs regs;
3555 abi_ulong oldmask;
3556 abi_ulong usp;
3559 struct target_ucontext {
3560 abi_ulong tuc_flags;
3561 abi_ulong tuc_link;
3562 target_stack_t tuc_stack;
3563 struct target_sigcontext tuc_mcontext;
3564 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3567 struct target_rt_sigframe {
3568 abi_ulong pinfo;
3569 uint64_t puc;
3570 struct target_siginfo info;
3571 struct target_sigcontext sc;
3572 struct target_ucontext uc;
3573 unsigned char retcode[16]; /* trampoline code */
3576 /* This is the asm-generic/ucontext.h version */
3577 #if 0
3578 static int restore_sigcontext(CPUOpenRISCState *regs,
3579 struct target_sigcontext *sc)
3581 unsigned int err = 0;
3582 unsigned long old_usp;
3584 /* Alwys make any pending restarted system call return -EINTR */
3585 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3587 /* restore the regs from &sc->regs (same as sc, since regs is first)
3588 * (sc is already checked for VERIFY_READ since the sigframe was
3589 * checked in sys_sigreturn previously)
3592 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3593 goto badframe;
3596 /* make sure the U-flag is set so user-mode cannot fool us */
3598 regs->sr &= ~SR_SM;
3600 /* restore the old USP as it was before we stacked the sc etc.
3601 * (we cannot just pop the sigcontext since we aligned the sp and
3602 * stuff after pushing it)
3605 err |= __get_user(old_usp, &sc->usp);
3606 phx_signal("old_usp 0x%lx", old_usp);
3608 __PHX__ REALLY /* ??? */
3609 wrusp(old_usp);
3610 regs->gpr[1] = old_usp;
3612 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3613 * after this completes, but we don't use that mechanism. maybe we can
3614 * use it now ?
3617 return err;
3619 badframe:
3620 return 1;
3622 #endif
3624 /* Set up a signal frame. */
3626 static int setup_sigcontext(struct target_sigcontext *sc,
3627 CPUOpenRISCState *regs,
3628 unsigned long mask)
3630 int err = 0;
3631 unsigned long usp = regs->gpr[1];
3633 /* copy the regs. they are first in sc so we can use sc directly */
3635 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3637 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3638 the signal handler. The frametype will be restored to its previous
3639 value in restore_sigcontext. */
3640 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3642 /* then some other stuff */
3643 err |= __put_user(mask, &sc->oldmask);
3644 err |= __put_user(usp, &sc->usp); return err;
3647 static inline unsigned long align_sigframe(unsigned long sp)
3649 unsigned long i;
3650 i = sp & ~3UL;
3651 return i;
3654 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3655 CPUOpenRISCState *regs,
3656 size_t frame_size)
3658 unsigned long sp = regs->gpr[1];
3659 int onsigstack = on_sig_stack(sp);
3661 /* redzone */
3662 /* This is the X/Open sanctioned signal stack switching. */
3663 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3664 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3667 sp = align_sigframe(sp - frame_size);
3670 * If we are on the alternate signal stack and would overflow it, don't.
3671 * Return an always-bogus address instead so we will die with SIGSEGV.
3674 if (onsigstack && !likely(on_sig_stack(sp))) {
3675 return -1L;
3678 return sp;
3681 static void setup_frame(int sig, struct target_sigaction *ka,
3682 target_sigset_t *set, CPUOpenRISCState *env)
3684 qemu_log("Not implement.\n");
3687 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3688 target_siginfo_t *info,
3689 target_sigset_t *set, CPUOpenRISCState *env)
3691 int err = 0;
3692 abi_ulong frame_addr;
3693 unsigned long return_ip;
3694 struct target_rt_sigframe *frame;
3695 abi_ulong info_addr, uc_addr;
3697 frame_addr = get_sigframe(ka, env, sizeof *frame);
3699 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3700 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3701 goto give_sigsegv;
3704 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3705 err |= __put_user(info_addr, &frame->pinfo);
3706 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3707 err |= __put_user(uc_addr, &frame->puc);
3709 if (ka->sa_flags & SA_SIGINFO) {
3710 err |= copy_siginfo_to_user(&frame->info, info);
3712 if (err) {
3713 goto give_sigsegv;
3716 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3717 err |= __put_user(0, &frame->uc.tuc_flags);
3718 err |= __put_user(0, &frame->uc.tuc_link);
3719 err |= __put_user(target_sigaltstack_used.ss_sp,
3720 &frame->uc.tuc_stack.ss_sp);
3721 err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3722 err |= __put_user(target_sigaltstack_used.ss_size,
3723 &frame->uc.tuc_stack.ss_size);
3724 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
3726 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3728 if (err) {
3729 goto give_sigsegv;
3732 /* trampoline - the desired return ip is the retcode itself */
3733 return_ip = (unsigned long)&frame->retcode;
3734 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3735 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
3736 err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3737 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3738 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3740 if (err) {
3741 goto give_sigsegv;
3744 /* TODO what is the current->exec_domain stuff and invmap ? */
3746 /* Set up registers for signal handler */
3747 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3748 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3749 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3750 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3751 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3753 /* actually move the usp to reflect the stacked frame */
3754 env->gpr[1] = (unsigned long)frame;
3756 return;
3758 give_sigsegv:
3759 unlock_user_struct(frame, frame_addr, 1);
3760 if (sig == TARGET_SIGSEGV) {
3761 ka->_sa_handler = TARGET_SIG_DFL;
3763 force_sig(TARGET_SIGSEGV);
3766 long do_sigreturn(CPUOpenRISCState *env)
3769 qemu_log("do_sigreturn: not implemented\n");
3770 return -TARGET_ENOSYS;
3773 long do_rt_sigreturn(CPUOpenRISCState *env)
3775 qemu_log("do_rt_sigreturn: not implemented\n");
3776 return -TARGET_ENOSYS;
3778 /* TARGET_OPENRISC */
3780 #elif defined(TARGET_S390X)
3782 #define __NUM_GPRS 16
3783 #define __NUM_FPRS 16
3784 #define __NUM_ACRS 16
3786 #define S390_SYSCALL_SIZE 2
3787 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3789 #define _SIGCONTEXT_NSIG 64
3790 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3791 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3792 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3793 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
3794 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3796 typedef struct {
3797 target_psw_t psw;
3798 target_ulong gprs[__NUM_GPRS];
3799 unsigned int acrs[__NUM_ACRS];
3800 } target_s390_regs_common;
3802 typedef struct {
3803 unsigned int fpc;
3804 double fprs[__NUM_FPRS];
3805 } target_s390_fp_regs;
3807 typedef struct {
3808 target_s390_regs_common regs;
3809 target_s390_fp_regs fpregs;
3810 } target_sigregs;
3812 struct target_sigcontext {
3813 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
3814 target_sigregs *sregs;
3817 typedef struct {
3818 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3819 struct target_sigcontext sc;
3820 target_sigregs sregs;
3821 int signo;
3822 uint8_t retcode[S390_SYSCALL_SIZE];
3823 } sigframe;
3825 struct target_ucontext {
3826 target_ulong tuc_flags;
3827 struct target_ucontext *tuc_link;
3828 target_stack_t tuc_stack;
3829 target_sigregs tuc_mcontext;
3830 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3833 typedef struct {
3834 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3835 uint8_t retcode[S390_SYSCALL_SIZE];
3836 struct target_siginfo info;
3837 struct target_ucontext uc;
3838 } rt_sigframe;
3840 static inline abi_ulong
3841 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
3843 abi_ulong sp;
3845 /* Default to using normal stack */
3846 sp = env->regs[15];
3848 /* This is the X/Open sanctioned signal stack switching. */
3849 if (ka->sa_flags & TARGET_SA_ONSTACK) {
3850 if (!sas_ss_flags(sp)) {
3851 sp = target_sigaltstack_used.ss_sp +
3852 target_sigaltstack_used.ss_size;
3856 /* This is the legacy signal stack switching. */
3857 else if (/* FIXME !user_mode(regs) */ 0 &&
3858 !(ka->sa_flags & TARGET_SA_RESTORER) &&
3859 ka->sa_restorer) {
3860 sp = (abi_ulong) ka->sa_restorer;
3863 return (sp - frame_size) & -8ul;
3866 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
3868 int i;
3869 //save_access_regs(current->thread.acrs); FIXME
3871 /* Copy a 'clean' PSW mask to the user to avoid leaking
3872 information about whether PER is currently on. */
3873 __put_user(env->psw.mask, &sregs->regs.psw.mask);
3874 __put_user(env->psw.addr, &sregs->regs.psw.addr);
3875 for (i = 0; i < 16; i++) {
3876 __put_user(env->regs[i], &sregs->regs.gprs[i]);
3878 for (i = 0; i < 16; i++) {
3879 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3882 * We have to store the fp registers to current->thread.fp_regs
3883 * to merge them with the emulated registers.
3885 //save_fp_regs(&current->thread.fp_regs); FIXME
3886 for (i = 0; i < 16; i++) {
3887 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3891 static void setup_frame(int sig, struct target_sigaction *ka,
3892 target_sigset_t *set, CPUS390XState *env)
3894 sigframe *frame;
3895 abi_ulong frame_addr;
3897 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3898 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3899 (unsigned long long)frame_addr);
3900 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3901 goto give_sigsegv;
3904 qemu_log("%s: 1\n", __FUNCTION__);
3905 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
3906 goto give_sigsegv;
3909 save_sigregs(env, &frame->sregs);
3911 __put_user((abi_ulong)(unsigned long)&frame->sregs,
3912 (abi_ulong *)&frame->sc.sregs);
3914 /* Set up to return from userspace. If provided, use a stub
3915 already in userspace. */
3916 if (ka->sa_flags & TARGET_SA_RESTORER) {
3917 env->regs[14] = (unsigned long)
3918 ka->sa_restorer | PSW_ADDR_AMODE;
3919 } else {
3920 env->regs[14] = (unsigned long)
3921 frame->retcode | PSW_ADDR_AMODE;
3922 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
3923 (uint16_t *)(frame->retcode)))
3924 goto give_sigsegv;
3927 /* Set up backchain. */
3928 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3929 goto give_sigsegv;
3932 /* Set up registers for signal handler */
3933 env->regs[15] = frame_addr;
3934 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3936 env->regs[2] = sig; //map_signal(sig);
3937 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
3939 /* We forgot to include these in the sigcontext.
3940 To avoid breaking binary compatibility, they are passed as args. */
3941 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
3942 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
3944 /* Place signal number on stack to allow backtrace from handler. */
3945 if (__put_user(env->regs[2], (int *) &frame->signo)) {
3946 goto give_sigsegv;
3948 unlock_user_struct(frame, frame_addr, 1);
3949 return;
3951 give_sigsegv:
3952 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3953 unlock_user_struct(frame, frame_addr, 1);
3954 force_sig(TARGET_SIGSEGV);
3957 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3958 target_siginfo_t *info,
3959 target_sigset_t *set, CPUS390XState *env)
3961 int i;
3962 rt_sigframe *frame;
3963 abi_ulong frame_addr;
3965 frame_addr = get_sigframe(ka, env, sizeof *frame);
3966 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3967 (unsigned long long)frame_addr);
3968 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3969 goto give_sigsegv;
3972 qemu_log("%s: 1\n", __FUNCTION__);
3973 if (copy_siginfo_to_user(&frame->info, info)) {
3974 goto give_sigsegv;
3977 /* Create the ucontext. */
3978 __put_user(0, &frame->uc.tuc_flags);
3979 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
3980 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
3981 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3982 &frame->uc.tuc_stack.ss_flags);
3983 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
3984 save_sigregs(env, &frame->uc.tuc_mcontext);
3985 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
3986 __put_user((abi_ulong)set->sig[i],
3987 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
3990 /* Set up to return from userspace. If provided, use a stub
3991 already in userspace. */
3992 if (ka->sa_flags & TARGET_SA_RESTORER) {
3993 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
3994 } else {
3995 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
3996 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
3997 (uint16_t *)(frame->retcode))) {
3998 goto give_sigsegv;
4002 /* Set up backchain. */
4003 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4004 goto give_sigsegv;
4007 /* Set up registers for signal handler */
4008 env->regs[15] = frame_addr;
4009 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4011 env->regs[2] = sig; //map_signal(sig);
4012 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4013 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4014 return;
4016 give_sigsegv:
4017 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4018 unlock_user_struct(frame, frame_addr, 1);
4019 force_sig(TARGET_SIGSEGV);
4022 static int
4023 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4025 int err = 0;
4026 int i;
4028 for (i = 0; i < 16; i++) {
4029 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4032 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4033 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4034 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4035 (unsigned long long)env->psw.addr);
4036 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4037 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4039 for (i = 0; i < 16; i++) {
4040 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4042 for (i = 0; i < 16; i++) {
4043 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4046 return err;
4049 long do_sigreturn(CPUS390XState *env)
4051 sigframe *frame;
4052 abi_ulong frame_addr = env->regs[15];
4053 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4054 (unsigned long long)frame_addr);
4055 target_sigset_t target_set;
4056 sigset_t set;
4058 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4059 goto badframe;
4061 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4062 goto badframe;
4065 target_to_host_sigset_internal(&set, &target_set);
4066 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4068 if (restore_sigregs(env, &frame->sregs)) {
4069 goto badframe;
4072 unlock_user_struct(frame, frame_addr, 0);
4073 return env->regs[2];
4075 badframe:
4076 unlock_user_struct(frame, frame_addr, 0);
4077 force_sig(TARGET_SIGSEGV);
4078 return 0;
4081 long do_rt_sigreturn(CPUS390XState *env)
4083 rt_sigframe *frame;
4084 abi_ulong frame_addr = env->regs[15];
4085 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4086 (unsigned long long)frame_addr);
4087 sigset_t set;
4089 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4090 goto badframe;
4092 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4094 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4096 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4097 goto badframe;
4100 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4101 get_sp_from_cpustate(env)) == -EFAULT) {
4102 goto badframe;
4104 unlock_user_struct(frame, frame_addr, 0);
4105 return env->regs[2];
4107 badframe:
4108 unlock_user_struct(frame, frame_addr, 0);
4109 force_sig(TARGET_SIGSEGV);
4110 return 0;
4113 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4115 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
4116 the signal handling is different enough that we haven't implemented
4117 support for PPC64 yet. Hence the restriction above.
4119 There are various #if'd blocks for code for TARGET_PPC64. These
4120 blocks should go away so that we can successfully run 32-bit and
4121 64-bit binaries on a QEMU configured for PPC64. */
4123 /* Size of dummy stack frame allocated when calling signal handler.
4124 See arch/powerpc/include/asm/ptrace.h. */
4125 #if defined(TARGET_PPC64)
4126 #define SIGNAL_FRAMESIZE 128
4127 #else
4128 #define SIGNAL_FRAMESIZE 64
4129 #endif
4131 /* See arch/powerpc/include/asm/sigcontext.h. */
4132 struct target_sigcontext {
4133 target_ulong _unused[4];
4134 int32_t signal;
4135 #if defined(TARGET_PPC64)
4136 int32_t pad0;
4137 #endif
4138 target_ulong handler;
4139 target_ulong oldmask;
4140 target_ulong regs; /* struct pt_regs __user * */
4141 /* TODO: PPC64 includes extra bits here. */
4144 /* Indices for target_mcontext.mc_gregs, below.
4145 See arch/powerpc/include/asm/ptrace.h for details. */
4146 enum {
4147 TARGET_PT_R0 = 0,
4148 TARGET_PT_R1 = 1,
4149 TARGET_PT_R2 = 2,
4150 TARGET_PT_R3 = 3,
4151 TARGET_PT_R4 = 4,
4152 TARGET_PT_R5 = 5,
4153 TARGET_PT_R6 = 6,
4154 TARGET_PT_R7 = 7,
4155 TARGET_PT_R8 = 8,
4156 TARGET_PT_R9 = 9,
4157 TARGET_PT_R10 = 10,
4158 TARGET_PT_R11 = 11,
4159 TARGET_PT_R12 = 12,
4160 TARGET_PT_R13 = 13,
4161 TARGET_PT_R14 = 14,
4162 TARGET_PT_R15 = 15,
4163 TARGET_PT_R16 = 16,
4164 TARGET_PT_R17 = 17,
4165 TARGET_PT_R18 = 18,
4166 TARGET_PT_R19 = 19,
4167 TARGET_PT_R20 = 20,
4168 TARGET_PT_R21 = 21,
4169 TARGET_PT_R22 = 22,
4170 TARGET_PT_R23 = 23,
4171 TARGET_PT_R24 = 24,
4172 TARGET_PT_R25 = 25,
4173 TARGET_PT_R26 = 26,
4174 TARGET_PT_R27 = 27,
4175 TARGET_PT_R28 = 28,
4176 TARGET_PT_R29 = 29,
4177 TARGET_PT_R30 = 30,
4178 TARGET_PT_R31 = 31,
4179 TARGET_PT_NIP = 32,
4180 TARGET_PT_MSR = 33,
4181 TARGET_PT_ORIG_R3 = 34,
4182 TARGET_PT_CTR = 35,
4183 TARGET_PT_LNK = 36,
4184 TARGET_PT_XER = 37,
4185 TARGET_PT_CCR = 38,
4186 /* Yes, there are two registers with #39. One is 64-bit only. */
4187 TARGET_PT_MQ = 39,
4188 TARGET_PT_SOFTE = 39,
4189 TARGET_PT_TRAP = 40,
4190 TARGET_PT_DAR = 41,
4191 TARGET_PT_DSISR = 42,
4192 TARGET_PT_RESULT = 43,
4193 TARGET_PT_REGS_COUNT = 44
4196 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4197 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4198 struct target_mcontext {
4199 target_ulong mc_gregs[48];
4200 /* Includes fpscr. */
4201 uint64_t mc_fregs[33];
4202 target_ulong mc_pad[2];
4203 /* We need to handle Altivec and SPE at the same time, which no
4204 kernel needs to do. Fortunately, the kernel defines this bit to
4205 be Altivec-register-large all the time, rather than trying to
4206 twiddle it based on the specific platform. */
4207 union {
4208 /* SPE vector registers. One extra for SPEFSCR. */
4209 uint32_t spe[33];
4210 /* Altivec vector registers. The packing of VSCR and VRSAVE
4211 varies depending on whether we're PPC64 or not: PPC64 splits
4212 them apart; PPC32 stuffs them together. */
4213 #if defined(TARGET_PPC64)
4214 #define QEMU_NVRREG 34
4215 #else
4216 #define QEMU_NVRREG 33
4217 #endif
4218 ppc_avr_t altivec[QEMU_NVRREG];
4219 #undef QEMU_NVRREG
4220 } mc_vregs __attribute__((__aligned__(16)));
4223 struct target_ucontext {
4224 target_ulong tuc_flags;
4225 target_ulong tuc_link; /* struct ucontext __user * */
4226 struct target_sigaltstack tuc_stack;
4227 #if !defined(TARGET_PPC64)
4228 int32_t tuc_pad[7];
4229 target_ulong tuc_regs; /* struct mcontext __user *
4230 points to uc_mcontext field */
4231 #endif
4232 target_sigset_t tuc_sigmask;
4233 #if defined(TARGET_PPC64)
4234 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4235 struct target_sigcontext tuc_mcontext;
4236 #else
4237 int32_t tuc_maskext[30];
4238 int32_t tuc_pad2[3];
4239 struct target_mcontext tuc_mcontext;
4240 #endif
4243 /* See arch/powerpc/kernel/signal_32.c. */
4244 struct target_sigframe {
4245 struct target_sigcontext sctx;
4246 struct target_mcontext mctx;
4247 int32_t abigap[56];
4250 struct target_rt_sigframe {
4251 struct target_siginfo info;
4252 struct target_ucontext uc;
4253 int32_t abigap[56];
4256 /* We use the mc_pad field for the signal return trampoline. */
4257 #define tramp mc_pad
4259 /* See arch/powerpc/kernel/signal.c. */
4260 static target_ulong get_sigframe(struct target_sigaction *ka,
4261 CPUPPCState *env,
4262 int frame_size)
4264 target_ulong oldsp, newsp;
4266 oldsp = env->gpr[1];
4268 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4269 (sas_ss_flags(oldsp) == 0)) {
4270 oldsp = (target_sigaltstack_used.ss_sp
4271 + target_sigaltstack_used.ss_size);
4274 newsp = (oldsp - frame_size) & ~0xFUL;
4276 return newsp;
4279 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
4280 int sigret)
4282 target_ulong msr = env->msr;
4283 int i;
4284 target_ulong ccr = 0;
4286 /* In general, the kernel attempts to be intelligent about what it
4287 needs to save for Altivec/FP/SPE registers. We don't care that
4288 much, so we just go ahead and save everything. */
4290 /* Save general registers. */
4291 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4292 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4293 return 1;
4296 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4297 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4298 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4299 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4300 return 1;
4302 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4303 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4305 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4306 return 1;
4308 /* Save Altivec registers if necessary. */
4309 if (env->insns_flags & PPC_ALTIVEC) {
4310 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4311 ppc_avr_t *avr = &env->avr[i];
4312 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4314 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4315 __put_user(avr->u64[1], &vreg->u64[1])) {
4316 return 1;
4319 /* Set MSR_VR in the saved MSR value to indicate that
4320 frame->mc_vregs contains valid data. */
4321 msr |= MSR_VR;
4322 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4323 &frame->mc_vregs.altivec[32].u32[3]))
4324 return 1;
4327 /* Save floating point registers. */
4328 if (env->insns_flags & PPC_FLOAT) {
4329 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4330 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4331 return 1;
4334 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4335 return 1;
4338 /* Save SPE registers. The kernel only saves the high half. */
4339 if (env->insns_flags & PPC_SPE) {
4340 #if defined(TARGET_PPC64)
4341 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4342 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4343 return 1;
4346 #else
4347 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4348 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4349 return 1;
4352 #endif
4353 /* Set MSR_SPE in the saved MSR value to indicate that
4354 frame->mc_vregs contains valid data. */
4355 msr |= MSR_SPE;
4356 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4357 return 1;
4360 /* Store MSR. */
4361 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4362 return 1;
4364 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4365 if (sigret) {
4366 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4367 __put_user(0x44000002UL, &frame->tramp[1])) {
4368 return 1;
4372 return 0;
4375 static int restore_user_regs(CPUPPCState *env,
4376 struct target_mcontext *frame, int sig)
4378 target_ulong save_r2 = 0;
4379 target_ulong msr;
4380 target_ulong ccr;
4382 int i;
4384 if (!sig) {
4385 save_r2 = env->gpr[2];
4388 /* Restore general registers. */
4389 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4390 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4391 return 1;
4394 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4395 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4396 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4397 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4398 return 1;
4399 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4400 return 1;
4402 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4403 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4406 if (!sig) {
4407 env->gpr[2] = save_r2;
4409 /* Restore MSR. */
4410 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4411 return 1;
4413 /* If doing signal return, restore the previous little-endian mode. */
4414 if (sig)
4415 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4417 /* Restore Altivec registers if necessary. */
4418 if (env->insns_flags & PPC_ALTIVEC) {
4419 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4420 ppc_avr_t *avr = &env->avr[i];
4421 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4423 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4424 __get_user(avr->u64[1], &vreg->u64[1])) {
4425 return 1;
4428 /* Set MSR_VEC in the saved MSR value to indicate that
4429 frame->mc_vregs contains valid data. */
4430 if (__get_user(env->spr[SPR_VRSAVE],
4431 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4432 return 1;
4435 /* Restore floating point registers. */
4436 if (env->insns_flags & PPC_FLOAT) {
4437 uint64_t fpscr;
4438 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4439 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4440 return 1;
4443 if (__get_user(fpscr, &frame->mc_fregs[32]))
4444 return 1;
4445 env->fpscr = (uint32_t) fpscr;
4448 /* Save SPE registers. The kernel only saves the high half. */
4449 if (env->insns_flags & PPC_SPE) {
4450 #if defined(TARGET_PPC64)
4451 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4452 uint32_t hi;
4454 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4455 return 1;
4457 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4459 #else
4460 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4461 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4462 return 1;
4465 #endif
4466 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4467 return 1;
4470 return 0;
4473 static void setup_frame(int sig, struct target_sigaction *ka,
4474 target_sigset_t *set, CPUPPCState *env)
4476 struct target_sigframe *frame;
4477 struct target_sigcontext *sc;
4478 target_ulong frame_addr, newsp;
4479 int err = 0;
4480 int signal;
4482 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4483 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4484 goto sigsegv;
4485 sc = &frame->sctx;
4487 signal = current_exec_domain_sig(sig);
4489 err |= __put_user(ka->_sa_handler, &sc->handler);
4490 err |= __put_user(set->sig[0], &sc->oldmask);
4491 #if defined(TARGET_PPC64)
4492 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4493 #else
4494 err |= __put_user(set->sig[1], &sc->_unused[3]);
4495 #endif
4496 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4497 err |= __put_user(sig, &sc->signal);
4499 /* Save user regs. */
4500 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4502 /* The kernel checks for the presence of a VDSO here. We don't
4503 emulate a vdso, so use a sigreturn system call. */
4504 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4506 /* Turn off all fp exceptions. */
4507 env->fpscr = 0;
4509 /* Create a stack frame for the caller of the handler. */
4510 newsp = frame_addr - SIGNAL_FRAMESIZE;
4511 err |= put_user(env->gpr[1], newsp, target_ulong);
4513 if (err)
4514 goto sigsegv;
4516 /* Set up registers for signal handler. */
4517 env->gpr[1] = newsp;
4518 env->gpr[3] = signal;
4519 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4520 env->nip = (target_ulong) ka->_sa_handler;
4521 /* Signal handlers are entered in big-endian mode. */
4522 env->msr &= ~MSR_LE;
4524 unlock_user_struct(frame, frame_addr, 1);
4525 return;
4527 sigsegv:
4528 unlock_user_struct(frame, frame_addr, 1);
4529 qemu_log("segfaulting from setup_frame\n");
4530 force_sig(TARGET_SIGSEGV);
4533 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4534 target_siginfo_t *info,
4535 target_sigset_t *set, CPUPPCState *env)
4537 struct target_rt_sigframe *rt_sf;
4538 struct target_mcontext *frame;
4539 target_ulong rt_sf_addr, newsp = 0;
4540 int i, err = 0;
4541 int signal;
4543 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4544 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4545 goto sigsegv;
4547 signal = current_exec_domain_sig(sig);
4549 err |= copy_siginfo_to_user(&rt_sf->info, info);
4551 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4552 err |= __put_user(0, &rt_sf->uc.tuc_link);
4553 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4554 &rt_sf->uc.tuc_stack.ss_sp);
4555 err |= __put_user(sas_ss_flags(env->gpr[1]),
4556 &rt_sf->uc.tuc_stack.ss_flags);
4557 err |= __put_user(target_sigaltstack_used.ss_size,
4558 &rt_sf->uc.tuc_stack.ss_size);
4559 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4560 &rt_sf->uc.tuc_regs);
4561 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4562 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4565 frame = &rt_sf->uc.tuc_mcontext;
4566 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4568 /* The kernel checks for the presence of a VDSO here. We don't
4569 emulate a vdso, so use a sigreturn system call. */
4570 env->lr = (target_ulong) h2g(frame->tramp);
4572 /* Turn off all fp exceptions. */
4573 env->fpscr = 0;
4575 /* Create a stack frame for the caller of the handler. */
4576 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4577 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4579 if (err)
4580 goto sigsegv;
4582 /* Set up registers for signal handler. */
4583 env->gpr[1] = newsp;
4584 env->gpr[3] = (target_ulong) signal;
4585 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4586 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4587 env->gpr[6] = (target_ulong) h2g(rt_sf);
4588 env->nip = (target_ulong) ka->_sa_handler;
4589 /* Signal handlers are entered in big-endian mode. */
4590 env->msr &= ~MSR_LE;
4592 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4593 return;
4595 sigsegv:
4596 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4597 qemu_log("segfaulting from setup_rt_frame\n");
4598 force_sig(TARGET_SIGSEGV);
4602 long do_sigreturn(CPUPPCState *env)
4604 struct target_sigcontext *sc = NULL;
4605 struct target_mcontext *sr = NULL;
4606 target_ulong sr_addr, sc_addr;
4607 sigset_t blocked;
4608 target_sigset_t set;
4610 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4611 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4612 goto sigsegv;
4614 #if defined(TARGET_PPC64)
4615 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4616 #else
4617 if(__get_user(set.sig[0], &sc->oldmask) ||
4618 __get_user(set.sig[1], &sc->_unused[3]))
4619 goto sigsegv;
4620 #endif
4621 target_to_host_sigset_internal(&blocked, &set);
4622 sigprocmask(SIG_SETMASK, &blocked, NULL);
4624 if (__get_user(sr_addr, &sc->regs))
4625 goto sigsegv;
4626 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4627 goto sigsegv;
4628 if (restore_user_regs(env, sr, 1))
4629 goto sigsegv;
4631 unlock_user_struct(sr, sr_addr, 1);
4632 unlock_user_struct(sc, sc_addr, 1);
4633 return -TARGET_QEMU_ESIGRETURN;
4635 sigsegv:
4636 unlock_user_struct(sr, sr_addr, 1);
4637 unlock_user_struct(sc, sc_addr, 1);
4638 qemu_log("segfaulting from do_sigreturn\n");
4639 force_sig(TARGET_SIGSEGV);
4640 return 0;
4643 /* See arch/powerpc/kernel/signal_32.c. */
4644 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
4646 struct target_mcontext *mcp;
4647 target_ulong mcp_addr;
4648 sigset_t blocked;
4649 target_sigset_t set;
4651 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4652 sizeof (set)))
4653 return 1;
4655 #if defined(TARGET_PPC64)
4656 fprintf (stderr, "do_setcontext: not implemented\n");
4657 return 0;
4658 #else
4659 if (__get_user(mcp_addr, &ucp->tuc_regs))
4660 return 1;
4662 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4663 return 1;
4665 target_to_host_sigset_internal(&blocked, &set);
4666 sigprocmask(SIG_SETMASK, &blocked, NULL);
4667 if (restore_user_regs(env, mcp, sig))
4668 goto sigsegv;
4670 unlock_user_struct(mcp, mcp_addr, 1);
4671 return 0;
4673 sigsegv:
4674 unlock_user_struct(mcp, mcp_addr, 1);
4675 return 1;
4676 #endif
4679 long do_rt_sigreturn(CPUPPCState *env)
4681 struct target_rt_sigframe *rt_sf = NULL;
4682 target_ulong rt_sf_addr;
4684 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4685 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4686 goto sigsegv;
4688 if (do_setcontext(&rt_sf->uc, env, 1))
4689 goto sigsegv;
4691 do_sigaltstack(rt_sf_addr
4692 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4693 0, env->gpr[1]);
4695 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4696 return -TARGET_QEMU_ESIGRETURN;
4698 sigsegv:
4699 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4700 qemu_log("segfaulting from do_rt_sigreturn\n");
4701 force_sig(TARGET_SIGSEGV);
4702 return 0;
4705 #elif defined(TARGET_M68K)
4707 struct target_sigcontext {
4708 abi_ulong sc_mask;
4709 abi_ulong sc_usp;
4710 abi_ulong sc_d0;
4711 abi_ulong sc_d1;
4712 abi_ulong sc_a0;
4713 abi_ulong sc_a1;
4714 unsigned short sc_sr;
4715 abi_ulong sc_pc;
4718 struct target_sigframe
4720 abi_ulong pretcode;
4721 int sig;
4722 int code;
4723 abi_ulong psc;
4724 char retcode[8];
4725 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4726 struct target_sigcontext sc;
4729 typedef int target_greg_t;
4730 #define TARGET_NGREG 18
4731 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4733 typedef struct target_fpregset {
4734 int f_fpcntl[3];
4735 int f_fpregs[8*3];
4736 } target_fpregset_t;
4738 struct target_mcontext {
4739 int version;
4740 target_gregset_t gregs;
4741 target_fpregset_t fpregs;
4744 #define TARGET_MCONTEXT_VERSION 2
4746 struct target_ucontext {
4747 abi_ulong tuc_flags;
4748 abi_ulong tuc_link;
4749 target_stack_t tuc_stack;
4750 struct target_mcontext tuc_mcontext;
4751 abi_long tuc_filler[80];
4752 target_sigset_t tuc_sigmask;
4755 struct target_rt_sigframe
4757 abi_ulong pretcode;
4758 int sig;
4759 abi_ulong pinfo;
4760 abi_ulong puc;
4761 char retcode[8];
4762 struct target_siginfo info;
4763 struct target_ucontext uc;
4766 static int
4767 setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4768 abi_ulong mask)
4770 int err = 0;
4772 err |= __put_user(mask, &sc->sc_mask);
4773 err |= __put_user(env->aregs[7], &sc->sc_usp);
4774 err |= __put_user(env->dregs[0], &sc->sc_d0);
4775 err |= __put_user(env->dregs[1], &sc->sc_d1);
4776 err |= __put_user(env->aregs[0], &sc->sc_a0);
4777 err |= __put_user(env->aregs[1], &sc->sc_a1);
4778 err |= __put_user(env->sr, &sc->sc_sr);
4779 err |= __put_user(env->pc, &sc->sc_pc);
4781 return err;
4784 static int
4785 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
4787 int err = 0;
4788 int temp;
4790 err |= __get_user(env->aregs[7], &sc->sc_usp);
4791 err |= __get_user(env->dregs[1], &sc->sc_d1);
4792 err |= __get_user(env->aregs[0], &sc->sc_a0);
4793 err |= __get_user(env->aregs[1], &sc->sc_a1);
4794 err |= __get_user(env->pc, &sc->sc_pc);
4795 err |= __get_user(temp, &sc->sc_sr);
4796 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4798 *pd0 = tswapl(sc->sc_d0);
4800 return err;
4804 * Determine which stack to use..
4806 static inline abi_ulong
4807 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
4808 size_t frame_size)
4810 unsigned long sp;
4812 sp = regs->aregs[7];
4814 /* This is the X/Open sanctioned signal stack switching. */
4815 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4816 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4819 return ((sp - frame_size) & -8UL);
4822 static void setup_frame(int sig, struct target_sigaction *ka,
4823 target_sigset_t *set, CPUM68KState *env)
4825 struct target_sigframe *frame;
4826 abi_ulong frame_addr;
4827 abi_ulong retcode_addr;
4828 abi_ulong sc_addr;
4829 int err = 0;
4830 int i;
4832 frame_addr = get_sigframe(ka, env, sizeof *frame);
4833 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4834 goto give_sigsegv;
4836 err |= __put_user(sig, &frame->sig);
4838 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4839 err |= __put_user(sc_addr, &frame->psc);
4841 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4842 if (err)
4843 goto give_sigsegv;
4845 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4846 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4847 goto give_sigsegv;
4850 /* Set up to return from userspace. */
4852 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4853 err |= __put_user(retcode_addr, &frame->pretcode);
4855 /* moveq #,d0; trap #0 */
4857 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4858 (long *)(frame->retcode));
4860 if (err)
4861 goto give_sigsegv;
4863 /* Set up to return from userspace */
4865 env->aregs[7] = frame_addr;
4866 env->pc = ka->_sa_handler;
4868 unlock_user_struct(frame, frame_addr, 1);
4869 return;
4871 give_sigsegv:
4872 unlock_user_struct(frame, frame_addr, 1);
4873 force_sig(TARGET_SIGSEGV);
4876 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4877 CPUM68KState *env)
4879 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4880 int err;
4882 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4883 err |= __put_user(env->dregs[0], &gregs[0]);
4884 err |= __put_user(env->dregs[1], &gregs[1]);
4885 err |= __put_user(env->dregs[2], &gregs[2]);
4886 err |= __put_user(env->dregs[3], &gregs[3]);
4887 err |= __put_user(env->dregs[4], &gregs[4]);
4888 err |= __put_user(env->dregs[5], &gregs[5]);
4889 err |= __put_user(env->dregs[6], &gregs[6]);
4890 err |= __put_user(env->dregs[7], &gregs[7]);
4891 err |= __put_user(env->aregs[0], &gregs[8]);
4892 err |= __put_user(env->aregs[1], &gregs[9]);
4893 err |= __put_user(env->aregs[2], &gregs[10]);
4894 err |= __put_user(env->aregs[3], &gregs[11]);
4895 err |= __put_user(env->aregs[4], &gregs[12]);
4896 err |= __put_user(env->aregs[5], &gregs[13]);
4897 err |= __put_user(env->aregs[6], &gregs[14]);
4898 err |= __put_user(env->aregs[7], &gregs[15]);
4899 err |= __put_user(env->pc, &gregs[16]);
4900 err |= __put_user(env->sr, &gregs[17]);
4902 return err;
4905 static inline int target_rt_restore_ucontext(CPUM68KState *env,
4906 struct target_ucontext *uc,
4907 int *pd0)
4909 int temp;
4910 int err;
4911 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4913 err = __get_user(temp, &uc->tuc_mcontext.version);
4914 if (temp != TARGET_MCONTEXT_VERSION)
4915 goto badframe;
4917 /* restore passed registers */
4918 err |= __get_user(env->dregs[0], &gregs[0]);
4919 err |= __get_user(env->dregs[1], &gregs[1]);
4920 err |= __get_user(env->dregs[2], &gregs[2]);
4921 err |= __get_user(env->dregs[3], &gregs[3]);
4922 err |= __get_user(env->dregs[4], &gregs[4]);
4923 err |= __get_user(env->dregs[5], &gregs[5]);
4924 err |= __get_user(env->dregs[6], &gregs[6]);
4925 err |= __get_user(env->dregs[7], &gregs[7]);
4926 err |= __get_user(env->aregs[0], &gregs[8]);
4927 err |= __get_user(env->aregs[1], &gregs[9]);
4928 err |= __get_user(env->aregs[2], &gregs[10]);
4929 err |= __get_user(env->aregs[3], &gregs[11]);
4930 err |= __get_user(env->aregs[4], &gregs[12]);
4931 err |= __get_user(env->aregs[5], &gregs[13]);
4932 err |= __get_user(env->aregs[6], &gregs[14]);
4933 err |= __get_user(env->aregs[7], &gregs[15]);
4934 err |= __get_user(env->pc, &gregs[16]);
4935 err |= __get_user(temp, &gregs[17]);
4936 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4938 *pd0 = env->dregs[0];
4939 return err;
4941 badframe:
4942 return 1;
4945 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4946 target_siginfo_t *info,
4947 target_sigset_t *set, CPUM68KState *env)
4949 struct target_rt_sigframe *frame;
4950 abi_ulong frame_addr;
4951 abi_ulong retcode_addr;
4952 abi_ulong info_addr;
4953 abi_ulong uc_addr;
4954 int err = 0;
4955 int i;
4957 frame_addr = get_sigframe(ka, env, sizeof *frame);
4958 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4959 goto give_sigsegv;
4961 err |= __put_user(sig, &frame->sig);
4963 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4964 err |= __put_user(info_addr, &frame->pinfo);
4966 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4967 err |= __put_user(uc_addr, &frame->puc);
4969 err |= copy_siginfo_to_user(&frame->info, info);
4971 /* Create the ucontext */
4973 err |= __put_user(0, &frame->uc.tuc_flags);
4974 err |= __put_user(0, &frame->uc.tuc_link);
4975 err |= __put_user(target_sigaltstack_used.ss_sp,
4976 &frame->uc.tuc_stack.ss_sp);
4977 err |= __put_user(sas_ss_flags(env->aregs[7]),
4978 &frame->uc.tuc_stack.ss_flags);
4979 err |= __put_user(target_sigaltstack_used.ss_size,
4980 &frame->uc.tuc_stack.ss_size);
4981 err |= target_rt_setup_ucontext(&frame->uc, env);
4983 if (err)
4984 goto give_sigsegv;
4986 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4987 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
4988 goto give_sigsegv;
4991 /* Set up to return from userspace. */
4993 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4994 err |= __put_user(retcode_addr, &frame->pretcode);
4996 /* moveq #,d0; notb d0; trap #0 */
4998 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4999 (long *)(frame->retcode + 0));
5000 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5002 if (err)
5003 goto give_sigsegv;
5005 /* Set up to return from userspace */
5007 env->aregs[7] = frame_addr;
5008 env->pc = ka->_sa_handler;
5010 unlock_user_struct(frame, frame_addr, 1);
5011 return;
5013 give_sigsegv:
5014 unlock_user_struct(frame, frame_addr, 1);
5015 force_sig(TARGET_SIGSEGV);
5018 long do_sigreturn(CPUM68KState *env)
5020 struct target_sigframe *frame;
5021 abi_ulong frame_addr = env->aregs[7] - 4;
5022 target_sigset_t target_set;
5023 sigset_t set;
5024 int d0, i;
5026 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5027 goto badframe;
5029 /* set blocked signals */
5031 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5032 goto badframe;
5034 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5035 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5036 goto badframe;
5039 target_to_host_sigset_internal(&set, &target_set);
5040 sigprocmask(SIG_SETMASK, &set, NULL);
5042 /* restore registers */
5044 if (restore_sigcontext(env, &frame->sc, &d0))
5045 goto badframe;
5047 unlock_user_struct(frame, frame_addr, 0);
5048 return d0;
5050 badframe:
5051 unlock_user_struct(frame, frame_addr, 0);
5052 force_sig(TARGET_SIGSEGV);
5053 return 0;
5056 long do_rt_sigreturn(CPUM68KState *env)
5058 struct target_rt_sigframe *frame;
5059 abi_ulong frame_addr = env->aregs[7] - 4;
5060 target_sigset_t target_set;
5061 sigset_t set;
5062 int d0;
5064 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5065 goto badframe;
5067 target_to_host_sigset_internal(&set, &target_set);
5068 sigprocmask(SIG_SETMASK, &set, NULL);
5070 /* restore registers */
5072 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5073 goto badframe;
5075 if (do_sigaltstack(frame_addr +
5076 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5077 0, get_sp_from_cpustate(env)) == -EFAULT)
5078 goto badframe;
5080 unlock_user_struct(frame, frame_addr, 0);
5081 return d0;
5083 badframe:
5084 unlock_user_struct(frame, frame_addr, 0);
5085 force_sig(TARGET_SIGSEGV);
5086 return 0;
5089 #elif defined(TARGET_ALPHA)
5091 struct target_sigcontext {
5092 abi_long sc_onstack;
5093 abi_long sc_mask;
5094 abi_long sc_pc;
5095 abi_long sc_ps;
5096 abi_long sc_regs[32];
5097 abi_long sc_ownedfp;
5098 abi_long sc_fpregs[32];
5099 abi_ulong sc_fpcr;
5100 abi_ulong sc_fp_control;
5101 abi_ulong sc_reserved1;
5102 abi_ulong sc_reserved2;
5103 abi_ulong sc_ssize;
5104 abi_ulong sc_sbase;
5105 abi_ulong sc_traparg_a0;
5106 abi_ulong sc_traparg_a1;
5107 abi_ulong sc_traparg_a2;
5108 abi_ulong sc_fp_trap_pc;
5109 abi_ulong sc_fp_trigger_sum;
5110 abi_ulong sc_fp_trigger_inst;
5113 struct target_ucontext {
5114 abi_ulong tuc_flags;
5115 abi_ulong tuc_link;
5116 abi_ulong tuc_osf_sigmask;
5117 target_stack_t tuc_stack;
5118 struct target_sigcontext tuc_mcontext;
5119 target_sigset_t tuc_sigmask;
5122 struct target_sigframe {
5123 struct target_sigcontext sc;
5124 unsigned int retcode[3];
5127 struct target_rt_sigframe {
5128 target_siginfo_t info;
5129 struct target_ucontext uc;
5130 unsigned int retcode[3];
5133 #define INSN_MOV_R30_R16 0x47fe0410
5134 #define INSN_LDI_R0 0x201f0000
5135 #define INSN_CALLSYS 0x00000083
5137 static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5138 abi_ulong frame_addr, target_sigset_t *set)
5140 int i, err = 0;
5142 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5143 err |= __put_user(set->sig[0], &sc->sc_mask);
5144 err |= __put_user(env->pc, &sc->sc_pc);
5145 err |= __put_user(8, &sc->sc_ps);
5147 for (i = 0; i < 31; ++i) {
5148 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5150 err |= __put_user(0, &sc->sc_regs[31]);
5152 for (i = 0; i < 31; ++i) {
5153 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5155 err |= __put_user(0, &sc->sc_fpregs[31]);
5156 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5158 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5159 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5160 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5162 return err;
5165 static int restore_sigcontext(CPUAlphaState *env,
5166 struct target_sigcontext *sc)
5168 uint64_t fpcr;
5169 int i, err = 0;
5171 err |= __get_user(env->pc, &sc->sc_pc);
5173 for (i = 0; i < 31; ++i) {
5174 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5176 for (i = 0; i < 31; ++i) {
5177 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5180 err |= __get_user(fpcr, &sc->sc_fpcr);
5181 cpu_alpha_store_fpcr(env, fpcr);
5183 return err;
5186 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5187 CPUAlphaState *env,
5188 unsigned long framesize)
5190 abi_ulong sp = env->ir[IR_SP];
5192 /* This is the X/Open sanctioned signal stack switching. */
5193 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5194 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5196 return (sp - framesize) & -32;
5199 static void setup_frame(int sig, struct target_sigaction *ka,
5200 target_sigset_t *set, CPUAlphaState *env)
5202 abi_ulong frame_addr, r26;
5203 struct target_sigframe *frame;
5204 int err = 0;
5206 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5207 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5208 goto give_sigsegv;
5211 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5213 if (ka->sa_restorer) {
5214 r26 = ka->sa_restorer;
5215 } else {
5216 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5217 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5218 &frame->retcode[1]);
5219 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5220 /* imb() */
5221 r26 = frame_addr;
5224 unlock_user_struct(frame, frame_addr, 1);
5226 if (err) {
5227 give_sigsegv:
5228 if (sig == TARGET_SIGSEGV) {
5229 ka->_sa_handler = TARGET_SIG_DFL;
5231 force_sig(TARGET_SIGSEGV);
5234 env->ir[IR_RA] = r26;
5235 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5236 env->ir[IR_A0] = sig;
5237 env->ir[IR_A1] = 0;
5238 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5239 env->ir[IR_SP] = frame_addr;
5242 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5243 target_siginfo_t *info,
5244 target_sigset_t *set, CPUAlphaState *env)
5246 abi_ulong frame_addr, r26;
5247 struct target_rt_sigframe *frame;
5248 int i, err = 0;
5250 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5251 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5252 goto give_sigsegv;
5255 err |= copy_siginfo_to_user(&frame->info, info);
5257 err |= __put_user(0, &frame->uc.tuc_flags);
5258 err |= __put_user(0, &frame->uc.tuc_link);
5259 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5260 err |= __put_user(target_sigaltstack_used.ss_sp,
5261 &frame->uc.tuc_stack.ss_sp);
5262 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
5263 &frame->uc.tuc_stack.ss_flags);
5264 err |= __put_user(target_sigaltstack_used.ss_size,
5265 &frame->uc.tuc_stack.ss_size);
5266 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5267 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5268 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5271 if (ka->sa_restorer) {
5272 r26 = ka->sa_restorer;
5273 } else {
5274 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5275 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5276 &frame->retcode[1]);
5277 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5278 /* imb(); */
5279 r26 = frame_addr;
5282 if (err) {
5283 give_sigsegv:
5284 if (sig == TARGET_SIGSEGV) {
5285 ka->_sa_handler = TARGET_SIG_DFL;
5287 force_sig(TARGET_SIGSEGV);
5290 env->ir[IR_RA] = r26;
5291 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5292 env->ir[IR_A0] = sig;
5293 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5294 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5295 env->ir[IR_SP] = frame_addr;
5298 long do_sigreturn(CPUAlphaState *env)
5300 struct target_sigcontext *sc;
5301 abi_ulong sc_addr = env->ir[IR_A0];
5302 target_sigset_t target_set;
5303 sigset_t set;
5305 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5306 goto badframe;
5309 target_sigemptyset(&target_set);
5310 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5311 goto badframe;
5314 target_to_host_sigset_internal(&set, &target_set);
5315 sigprocmask(SIG_SETMASK, &set, NULL);
5317 if (restore_sigcontext(env, sc)) {
5318 goto badframe;
5320 unlock_user_struct(sc, sc_addr, 0);
5321 return env->ir[IR_V0];
5323 badframe:
5324 unlock_user_struct(sc, sc_addr, 0);
5325 force_sig(TARGET_SIGSEGV);
5328 long do_rt_sigreturn(CPUAlphaState *env)
5330 abi_ulong frame_addr = env->ir[IR_A0];
5331 struct target_rt_sigframe *frame;
5332 sigset_t set;
5334 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5335 goto badframe;
5337 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5338 sigprocmask(SIG_SETMASK, &set, NULL);
5340 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
5341 goto badframe;
5343 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5344 uc.tuc_stack),
5345 0, env->ir[IR_SP]) == -EFAULT) {
5346 goto badframe;
5349 unlock_user_struct(frame, frame_addr, 0);
5350 return env->ir[IR_V0];
5353 badframe:
5354 unlock_user_struct(frame, frame_addr, 0);
5355 force_sig(TARGET_SIGSEGV);
5358 #else
5360 static void setup_frame(int sig, struct target_sigaction *ka,
5361 target_sigset_t *set, CPUArchState *env)
5363 fprintf(stderr, "setup_frame: not implemented\n");
5366 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5367 target_siginfo_t *info,
5368 target_sigset_t *set, CPUArchState *env)
5370 fprintf(stderr, "setup_rt_frame: not implemented\n");
5373 long do_sigreturn(CPUArchState *env)
5375 fprintf(stderr, "do_sigreturn: not implemented\n");
5376 return -TARGET_ENOSYS;
5379 long do_rt_sigreturn(CPUArchState *env)
5381 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5382 return -TARGET_ENOSYS;
5385 #endif
5387 void process_pending_signals(CPUArchState *cpu_env)
5389 CPUState *cpu = ENV_GET_CPU(cpu_env);
5390 int sig;
5391 abi_ulong handler;
5392 sigset_t set, old_set;
5393 target_sigset_t target_old_set;
5394 struct emulated_sigtable *k;
5395 struct target_sigaction *sa;
5396 struct sigqueue *q;
5397 TaskState *ts = cpu_env->opaque;
5399 if (!ts->signal_pending)
5400 return;
5402 /* FIXME: This is not threadsafe. */
5403 k = ts->sigtab;
5404 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5405 if (k->pending)
5406 goto handle_signal;
5407 k++;
5409 /* if no signal is pending, just return */
5410 ts->signal_pending = 0;
5411 return;
5413 handle_signal:
5414 #ifdef DEBUG_SIGNAL
5415 fprintf(stderr, "qemu: process signal %d\n", sig);
5416 #endif
5417 /* dequeue signal */
5418 q = k->first;
5419 k->first = q->next;
5420 if (!k->first)
5421 k->pending = 0;
5423 sig = gdb_handlesig(cpu, sig);
5424 if (!sig) {
5425 sa = NULL;
5426 handler = TARGET_SIG_IGN;
5427 } else {
5428 sa = &sigact_table[sig - 1];
5429 handler = sa->_sa_handler;
5432 if (handler == TARGET_SIG_DFL) {
5433 /* default handler : ignore some signal. The other are job control or fatal */
5434 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5435 kill(getpid(),SIGSTOP);
5436 } else if (sig != TARGET_SIGCHLD &&
5437 sig != TARGET_SIGURG &&
5438 sig != TARGET_SIGWINCH &&
5439 sig != TARGET_SIGCONT) {
5440 force_sig(sig);
5442 } else if (handler == TARGET_SIG_IGN) {
5443 /* ignore sig */
5444 } else if (handler == TARGET_SIG_ERR) {
5445 force_sig(sig);
5446 } else {
5447 /* compute the blocked signals during the handler execution */
5448 target_to_host_sigset(&set, &sa->sa_mask);
5449 /* SA_NODEFER indicates that the current signal should not be
5450 blocked during the handler */
5451 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5452 sigaddset(&set, target_to_host_signal(sig));
5454 /* block signals in the handler using Linux */
5455 sigprocmask(SIG_BLOCK, &set, &old_set);
5456 /* save the previous blocked signal state to restore it at the
5457 end of the signal execution (see do_sigreturn) */
5458 host_to_target_sigset_internal(&target_old_set, &old_set);
5460 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5461 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5463 CPUX86State *env = cpu_env;
5464 if (env->eflags & VM_MASK)
5465 save_v86_state(env);
5467 #endif
5468 /* prepare the stack frame of the virtual CPU */
5469 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5470 /* These targets do not have traditional signals. */
5471 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5472 #else
5473 if (sa->sa_flags & TARGET_SA_SIGINFO)
5474 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5475 else
5476 setup_frame(sig, sa, &target_old_set, cpu_env);
5477 #endif
5478 if (sa->sa_flags & TARGET_SA_RESETHAND)
5479 sa->_sa_handler = TARGET_SIG_DFL;
5481 if (q != &k->info)
5482 free_sigqueue(cpu_env, q);