target-alpha: Enable NPTL.
[qemu/aliguori-queue.git] / linux-user / signal.c
bloba72c15ca08526be9287ddc9cf02b38077a2e1917
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 <signal.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <sys/ucontext.h>
28 #include <sys/resource.h>
30 #include "qemu.h"
31 #include "qemu-common.h"
32 #include "target_signal.h"
34 //#define DEBUG_SIGNAL
36 static struct target_sigaltstack target_sigaltstack_used = {
37 .ss_sp = 0,
38 .ss_size = 0,
39 .ss_flags = TARGET_SS_DISABLE,
42 static struct target_sigaction sigact_table[TARGET_NSIG];
44 static void host_signal_handler(int host_signum, siginfo_t *info,
45 void *puc);
47 static uint8_t host_to_target_signal_table[_NSIG] = {
48 [SIGHUP] = TARGET_SIGHUP,
49 [SIGINT] = TARGET_SIGINT,
50 [SIGQUIT] = TARGET_SIGQUIT,
51 [SIGILL] = TARGET_SIGILL,
52 [SIGTRAP] = TARGET_SIGTRAP,
53 [SIGABRT] = TARGET_SIGABRT,
54 /* [SIGIOT] = TARGET_SIGIOT,*/
55 [SIGBUS] = TARGET_SIGBUS,
56 [SIGFPE] = TARGET_SIGFPE,
57 [SIGKILL] = TARGET_SIGKILL,
58 [SIGUSR1] = TARGET_SIGUSR1,
59 [SIGSEGV] = TARGET_SIGSEGV,
60 [SIGUSR2] = TARGET_SIGUSR2,
61 [SIGPIPE] = TARGET_SIGPIPE,
62 [SIGALRM] = TARGET_SIGALRM,
63 [SIGTERM] = TARGET_SIGTERM,
64 #ifdef SIGSTKFLT
65 [SIGSTKFLT] = TARGET_SIGSTKFLT,
66 #endif
67 [SIGCHLD] = TARGET_SIGCHLD,
68 [SIGCONT] = TARGET_SIGCONT,
69 [SIGSTOP] = TARGET_SIGSTOP,
70 [SIGTSTP] = TARGET_SIGTSTP,
71 [SIGTTIN] = TARGET_SIGTTIN,
72 [SIGTTOU] = TARGET_SIGTTOU,
73 [SIGURG] = TARGET_SIGURG,
74 [SIGXCPU] = TARGET_SIGXCPU,
75 [SIGXFSZ] = TARGET_SIGXFSZ,
76 [SIGVTALRM] = TARGET_SIGVTALRM,
77 [SIGPROF] = TARGET_SIGPROF,
78 [SIGWINCH] = TARGET_SIGWINCH,
79 [SIGIO] = TARGET_SIGIO,
80 [SIGPWR] = TARGET_SIGPWR,
81 [SIGSYS] = TARGET_SIGSYS,
82 /* next signals stay the same */
83 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
84 host libpthread signals. This assumes noone actually uses SIGRTMAX :-/
85 To fix this properly we need to do manual signal delivery multiplexed
86 over a single host signal. */
87 [__SIGRTMIN] = __SIGRTMAX,
88 [__SIGRTMAX] = __SIGRTMIN,
90 static uint8_t target_to_host_signal_table[_NSIG];
92 static inline int on_sig_stack(unsigned long sp)
94 return (sp - target_sigaltstack_used.ss_sp
95 < target_sigaltstack_used.ss_size);
98 static inline int sas_ss_flags(unsigned long sp)
100 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
101 : on_sig_stack(sp) ? SS_ONSTACK : 0);
104 int host_to_target_signal(int sig)
106 if (sig >= _NSIG)
107 return sig;
108 return host_to_target_signal_table[sig];
111 int target_to_host_signal(int sig)
113 if (sig >= _NSIG)
114 return sig;
115 return target_to_host_signal_table[sig];
118 static inline void target_sigemptyset(target_sigset_t *set)
120 memset(set, 0, sizeof(*set));
123 static inline void target_sigaddset(target_sigset_t *set, int signum)
125 signum--;
126 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
127 set->sig[signum / TARGET_NSIG_BPW] |= mask;
130 static inline int target_sigismember(const target_sigset_t *set, int signum)
132 signum--;
133 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
134 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
137 static void host_to_target_sigset_internal(target_sigset_t *d,
138 const sigset_t *s)
140 int i;
141 target_sigemptyset(d);
142 for (i = 1; i <= TARGET_NSIG; i++) {
143 if (sigismember(s, i)) {
144 target_sigaddset(d, host_to_target_signal(i));
149 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
151 target_sigset_t d1;
152 int i;
154 host_to_target_sigset_internal(&d1, s);
155 for(i = 0;i < TARGET_NSIG_WORDS; i++)
156 d->sig[i] = tswapl(d1.sig[i]);
159 static void target_to_host_sigset_internal(sigset_t *d,
160 const target_sigset_t *s)
162 int i;
163 sigemptyset(d);
164 for (i = 1; i <= TARGET_NSIG; i++) {
165 if (target_sigismember(s, i)) {
166 sigaddset(d, target_to_host_signal(i));
171 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
173 target_sigset_t s1;
174 int i;
176 for(i = 0;i < TARGET_NSIG_WORDS; i++)
177 s1.sig[i] = tswapl(s->sig[i]);
178 target_to_host_sigset_internal(d, &s1);
181 void host_to_target_old_sigset(abi_ulong *old_sigset,
182 const sigset_t *sigset)
184 target_sigset_t d;
185 host_to_target_sigset(&d, sigset);
186 *old_sigset = d.sig[0];
189 void target_to_host_old_sigset(sigset_t *sigset,
190 const abi_ulong *old_sigset)
192 target_sigset_t d;
193 int i;
195 d.sig[0] = *old_sigset;
196 for(i = 1;i < TARGET_NSIG_WORDS; i++)
197 d.sig[i] = 0;
198 target_to_host_sigset(sigset, &d);
201 /* siginfo conversion */
203 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
204 const siginfo_t *info)
206 int sig;
207 sig = host_to_target_signal(info->si_signo);
208 tinfo->si_signo = sig;
209 tinfo->si_errno = 0;
210 tinfo->si_code = info->si_code;
211 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
212 sig == SIGBUS || sig == SIGTRAP) {
213 /* should never come here, but who knows. The information for
214 the target is irrelevant */
215 tinfo->_sifields._sigfault._addr = 0;
216 } else if (sig == SIGIO) {
217 tinfo->_sifields._sigpoll._fd = info->si_fd;
218 } else if (sig >= TARGET_SIGRTMIN) {
219 tinfo->_sifields._rt._pid = info->si_pid;
220 tinfo->_sifields._rt._uid = info->si_uid;
221 /* XXX: potential problem if 64 bit */
222 tinfo->_sifields._rt._sigval.sival_ptr =
223 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
227 static void tswap_siginfo(target_siginfo_t *tinfo,
228 const target_siginfo_t *info)
230 int sig;
231 sig = info->si_signo;
232 tinfo->si_signo = tswap32(sig);
233 tinfo->si_errno = tswap32(info->si_errno);
234 tinfo->si_code = tswap32(info->si_code);
235 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
236 sig == SIGBUS || sig == SIGTRAP) {
237 tinfo->_sifields._sigfault._addr =
238 tswapl(info->_sifields._sigfault._addr);
239 } else if (sig == SIGIO) {
240 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
241 } else if (sig >= TARGET_SIGRTMIN) {
242 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
243 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
244 tinfo->_sifields._rt._sigval.sival_ptr =
245 tswapl(info->_sifields._rt._sigval.sival_ptr);
250 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
252 host_to_target_siginfo_noswap(tinfo, info);
253 tswap_siginfo(tinfo, tinfo);
256 /* XXX: we support only POSIX RT signals are used. */
257 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
258 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
260 info->si_signo = tswap32(tinfo->si_signo);
261 info->si_errno = tswap32(tinfo->si_errno);
262 info->si_code = tswap32(tinfo->si_code);
263 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
264 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
265 info->si_value.sival_ptr =
266 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
269 static int fatal_signal (int sig)
271 switch (sig) {
272 case TARGET_SIGCHLD:
273 case TARGET_SIGURG:
274 case TARGET_SIGWINCH:
275 /* Ignored by default. */
276 return 0;
277 case TARGET_SIGCONT:
278 case TARGET_SIGSTOP:
279 case TARGET_SIGTSTP:
280 case TARGET_SIGTTIN:
281 case TARGET_SIGTTOU:
282 /* Job control signals. */
283 return 0;
284 default:
285 return 1;
289 /* returns 1 if given signal should dump core if not handled */
290 static int core_dump_signal(int sig)
292 switch (sig) {
293 case TARGET_SIGABRT:
294 case TARGET_SIGFPE:
295 case TARGET_SIGILL:
296 case TARGET_SIGQUIT:
297 case TARGET_SIGSEGV:
298 case TARGET_SIGTRAP:
299 case TARGET_SIGBUS:
300 return (1);
301 default:
302 return (0);
306 void signal_init(void)
308 struct sigaction act;
309 struct sigaction oact;
310 int i, j;
311 int host_sig;
313 /* generate signal conversion tables */
314 for(i = 1; i < _NSIG; i++) {
315 if (host_to_target_signal_table[i] == 0)
316 host_to_target_signal_table[i] = i;
318 for(i = 1; i < _NSIG; i++) {
319 j = host_to_target_signal_table[i];
320 target_to_host_signal_table[j] = i;
323 /* set all host signal handlers. ALL signals are blocked during
324 the handlers to serialize them. */
325 memset(sigact_table, 0, sizeof(sigact_table));
327 sigfillset(&act.sa_mask);
328 act.sa_flags = SA_SIGINFO;
329 act.sa_sigaction = host_signal_handler;
330 for(i = 1; i <= TARGET_NSIG; i++) {
331 host_sig = target_to_host_signal(i);
332 sigaction(host_sig, NULL, &oact);
333 if (oact.sa_sigaction == (void *)SIG_IGN) {
334 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
335 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
336 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
338 /* If there's already a handler installed then something has
339 gone horribly wrong, so don't even try to handle that case. */
340 /* Install some handlers for our own use. We need at least
341 SIGSEGV and SIGBUS, to detect exceptions. We can not just
342 trap all signals because it affects syscall interrupt
343 behavior. But do trap all default-fatal signals. */
344 if (fatal_signal (i))
345 sigaction(host_sig, &act, NULL);
349 /* signal queue handling */
351 static inline struct sigqueue *alloc_sigqueue(CPUState *env)
353 TaskState *ts = env->opaque;
354 struct sigqueue *q = ts->first_free;
355 if (!q)
356 return NULL;
357 ts->first_free = q->next;
358 return q;
361 static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
363 TaskState *ts = env->opaque;
364 q->next = ts->first_free;
365 ts->first_free = q;
368 /* abort execution with signal */
369 static void QEMU_NORETURN force_sig(int target_sig)
371 TaskState *ts = (TaskState *)thread_env->opaque;
372 int host_sig, core_dumped = 0;
373 struct sigaction act;
374 host_sig = target_to_host_signal(target_sig);
375 gdb_signalled(thread_env, target_sig);
377 /* dump core if supported by target binary format */
378 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
379 stop_all_tasks();
380 core_dumped =
381 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
383 if (core_dumped) {
384 /* we already dumped the core of target process, we don't want
385 * a coredump of qemu itself */
386 struct rlimit nodump;
387 getrlimit(RLIMIT_CORE, &nodump);
388 nodump.rlim_cur=0;
389 setrlimit(RLIMIT_CORE, &nodump);
390 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
391 target_sig, strsignal(host_sig), "core dumped" );
394 /* The proper exit code for dieing from an uncaught signal is
395 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
396 * a negative value. To get the proper exit code we need to
397 * actually die from an uncaught signal. Here the default signal
398 * handler is installed, we send ourself a signal and we wait for
399 * it to arrive. */
400 sigfillset(&act.sa_mask);
401 act.sa_handler = SIG_DFL;
402 sigaction(host_sig, &act, NULL);
404 /* For some reason raise(host_sig) doesn't send the signal when
405 * statically linked on x86-64. */
406 kill(getpid(), host_sig);
408 /* Make sure the signal isn't masked (just reuse the mask inside
409 of act) */
410 sigdelset(&act.sa_mask, host_sig);
411 sigsuspend(&act.sa_mask);
413 /* unreachable */
414 abort();
417 /* queue a signal so that it will be send to the virtual CPU as soon
418 as possible */
419 int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
421 TaskState *ts = env->opaque;
422 struct emulated_sigtable *k;
423 struct sigqueue *q, **pq;
424 abi_ulong handler;
425 int queue;
427 #if defined(DEBUG_SIGNAL)
428 fprintf(stderr, "queue_signal: sig=%d\n",
429 sig);
430 #endif
431 k = &ts->sigtab[sig - 1];
432 queue = gdb_queuesig ();
433 handler = sigact_table[sig - 1]._sa_handler;
434 if (!queue && handler == TARGET_SIG_DFL) {
435 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
436 kill(getpid(),SIGSTOP);
437 return 0;
438 } else
439 /* default handler : ignore some signal. The other are fatal */
440 if (sig != TARGET_SIGCHLD &&
441 sig != TARGET_SIGURG &&
442 sig != TARGET_SIGWINCH &&
443 sig != TARGET_SIGCONT) {
444 force_sig(sig);
445 } else {
446 return 0; /* indicate ignored */
448 } else if (!queue && handler == TARGET_SIG_IGN) {
449 /* ignore signal */
450 return 0;
451 } else if (!queue && handler == TARGET_SIG_ERR) {
452 force_sig(sig);
453 } else {
454 pq = &k->first;
455 if (sig < TARGET_SIGRTMIN) {
456 /* if non real time signal, we queue exactly one signal */
457 if (!k->pending)
458 q = &k->info;
459 else
460 return 0;
461 } else {
462 if (!k->pending) {
463 /* first signal */
464 q = &k->info;
465 } else {
466 q = alloc_sigqueue(env);
467 if (!q)
468 return -EAGAIN;
469 while (*pq != NULL)
470 pq = &(*pq)->next;
473 *pq = q;
474 q->info = *info;
475 q->next = NULL;
476 k->pending = 1;
477 /* signal that a new signal is pending */
478 ts->signal_pending = 1;
479 return 1; /* indicates that the signal was queued */
483 static void host_signal_handler(int host_signum, siginfo_t *info,
484 void *puc)
486 int sig;
487 target_siginfo_t tinfo;
489 /* the CPU emulator uses some host signals to detect exceptions,
490 we forward to it some signals */
491 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
492 && info->si_code > 0) {
493 if (cpu_signal_handler(host_signum, info, puc))
494 return;
497 /* get target signal number */
498 sig = host_to_target_signal(host_signum);
499 if (sig < 1 || sig > TARGET_NSIG)
500 return;
501 #if defined(DEBUG_SIGNAL)
502 fprintf(stderr, "qemu: got signal %d\n", sig);
503 #endif
504 host_to_target_siginfo_noswap(&tinfo, info);
505 if (queue_signal(thread_env, sig, &tinfo) == 1) {
506 /* interrupt the virtual CPU as soon as possible */
507 cpu_exit(thread_env);
511 /* do_sigaltstack() returns target values and errnos. */
512 /* compare linux/kernel/signal.c:do_sigaltstack() */
513 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
515 int ret;
516 struct target_sigaltstack oss;
518 /* XXX: test errors */
519 if(uoss_addr)
521 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
522 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
523 __put_user(sas_ss_flags(sp), &oss.ss_flags);
526 if(uss_addr)
528 struct target_sigaltstack *uss;
529 struct target_sigaltstack ss;
531 ret = -TARGET_EFAULT;
532 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
533 || __get_user(ss.ss_sp, &uss->ss_sp)
534 || __get_user(ss.ss_size, &uss->ss_size)
535 || __get_user(ss.ss_flags, &uss->ss_flags))
536 goto out;
537 unlock_user_struct(uss, uss_addr, 0);
539 ret = -TARGET_EPERM;
540 if (on_sig_stack(sp))
541 goto out;
543 ret = -TARGET_EINVAL;
544 if (ss.ss_flags != TARGET_SS_DISABLE
545 && ss.ss_flags != TARGET_SS_ONSTACK
546 && ss.ss_flags != 0)
547 goto out;
549 if (ss.ss_flags == TARGET_SS_DISABLE) {
550 ss.ss_size = 0;
551 ss.ss_sp = 0;
552 } else {
553 ret = -TARGET_ENOMEM;
554 if (ss.ss_size < MINSIGSTKSZ)
555 goto out;
558 target_sigaltstack_used.ss_sp = ss.ss_sp;
559 target_sigaltstack_used.ss_size = ss.ss_size;
562 if (uoss_addr) {
563 ret = -TARGET_EFAULT;
564 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
565 goto out;
568 ret = 0;
569 out:
570 return ret;
573 /* do_sigaction() return host values and errnos */
574 int do_sigaction(int sig, const struct target_sigaction *act,
575 struct target_sigaction *oact)
577 struct target_sigaction *k;
578 struct sigaction act1;
579 int host_sig;
580 int ret = 0;
582 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
583 return -EINVAL;
584 k = &sigact_table[sig - 1];
585 #if defined(DEBUG_SIGNAL)
586 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
587 sig, act, oact);
588 #endif
589 if (oact) {
590 oact->_sa_handler = tswapl(k->_sa_handler);
591 oact->sa_flags = tswapl(k->sa_flags);
592 #if !defined(TARGET_MIPS)
593 oact->sa_restorer = tswapl(k->sa_restorer);
594 #endif
595 oact->sa_mask = k->sa_mask;
597 if (act) {
598 /* FIXME: This is not threadsafe. */
599 k->_sa_handler = tswapl(act->_sa_handler);
600 k->sa_flags = tswapl(act->sa_flags);
601 #if !defined(TARGET_MIPS)
602 k->sa_restorer = tswapl(act->sa_restorer);
603 #endif
604 k->sa_mask = act->sa_mask;
606 /* we update the host linux signal state */
607 host_sig = target_to_host_signal(sig);
608 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
609 sigfillset(&act1.sa_mask);
610 act1.sa_flags = SA_SIGINFO;
611 if (k->sa_flags & TARGET_SA_RESTART)
612 act1.sa_flags |= SA_RESTART;
613 /* NOTE: it is important to update the host kernel signal
614 ignore state to avoid getting unexpected interrupted
615 syscalls */
616 if (k->_sa_handler == TARGET_SIG_IGN) {
617 act1.sa_sigaction = (void *)SIG_IGN;
618 } else if (k->_sa_handler == TARGET_SIG_DFL) {
619 if (fatal_signal (sig))
620 act1.sa_sigaction = host_signal_handler;
621 else
622 act1.sa_sigaction = (void *)SIG_DFL;
623 } else {
624 act1.sa_sigaction = host_signal_handler;
626 ret = sigaction(host_sig, &act1, NULL);
629 return ret;
632 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
633 const target_siginfo_t *info)
635 tswap_siginfo(tinfo, info);
636 return 0;
639 static inline int current_exec_domain_sig(int sig)
641 return /* current->exec_domain && current->exec_domain->signal_invmap
642 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
645 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
647 /* from the Linux kernel */
649 struct target_fpreg {
650 uint16_t significand[4];
651 uint16_t exponent;
654 struct target_fpxreg {
655 uint16_t significand[4];
656 uint16_t exponent;
657 uint16_t padding[3];
660 struct target_xmmreg {
661 abi_ulong element[4];
664 struct target_fpstate {
665 /* Regular FPU environment */
666 abi_ulong cw;
667 abi_ulong sw;
668 abi_ulong tag;
669 abi_ulong ipoff;
670 abi_ulong cssel;
671 abi_ulong dataoff;
672 abi_ulong datasel;
673 struct target_fpreg _st[8];
674 uint16_t status;
675 uint16_t magic; /* 0xffff = regular FPU data only */
677 /* FXSR FPU environment */
678 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
679 abi_ulong mxcsr;
680 abi_ulong reserved;
681 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
682 struct target_xmmreg _xmm[8];
683 abi_ulong padding[56];
686 #define X86_FXSR_MAGIC 0x0000
688 struct target_sigcontext {
689 uint16_t gs, __gsh;
690 uint16_t fs, __fsh;
691 uint16_t es, __esh;
692 uint16_t ds, __dsh;
693 abi_ulong edi;
694 abi_ulong esi;
695 abi_ulong ebp;
696 abi_ulong esp;
697 abi_ulong ebx;
698 abi_ulong edx;
699 abi_ulong ecx;
700 abi_ulong eax;
701 abi_ulong trapno;
702 abi_ulong err;
703 abi_ulong eip;
704 uint16_t cs, __csh;
705 abi_ulong eflags;
706 abi_ulong esp_at_signal;
707 uint16_t ss, __ssh;
708 abi_ulong fpstate; /* pointer */
709 abi_ulong oldmask;
710 abi_ulong cr2;
713 struct target_ucontext {
714 abi_ulong tuc_flags;
715 abi_ulong tuc_link;
716 target_stack_t tuc_stack;
717 struct target_sigcontext tuc_mcontext;
718 target_sigset_t tuc_sigmask; /* mask last for extensibility */
721 struct sigframe
723 abi_ulong pretcode;
724 int sig;
725 struct target_sigcontext sc;
726 struct target_fpstate fpstate;
727 abi_ulong extramask[TARGET_NSIG_WORDS-1];
728 char retcode[8];
731 struct rt_sigframe
733 abi_ulong pretcode;
734 int sig;
735 abi_ulong pinfo;
736 abi_ulong puc;
737 struct target_siginfo info;
738 struct target_ucontext uc;
739 struct target_fpstate fpstate;
740 char retcode[8];
744 * Set up a signal frame.
747 /* XXX: save x87 state */
748 static int
749 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
750 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
752 int err = 0;
753 uint16_t magic;
755 /* already locked in setup_frame() */
756 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
757 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
758 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
759 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
760 err |= __put_user(env->regs[R_EDI], &sc->edi);
761 err |= __put_user(env->regs[R_ESI], &sc->esi);
762 err |= __put_user(env->regs[R_EBP], &sc->ebp);
763 err |= __put_user(env->regs[R_ESP], &sc->esp);
764 err |= __put_user(env->regs[R_EBX], &sc->ebx);
765 err |= __put_user(env->regs[R_EDX], &sc->edx);
766 err |= __put_user(env->regs[R_ECX], &sc->ecx);
767 err |= __put_user(env->regs[R_EAX], &sc->eax);
768 err |= __put_user(env->exception_index, &sc->trapno);
769 err |= __put_user(env->error_code, &sc->err);
770 err |= __put_user(env->eip, &sc->eip);
771 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
772 err |= __put_user(env->eflags, &sc->eflags);
773 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
774 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
776 cpu_x86_fsave(env, fpstate_addr, 1);
777 fpstate->status = fpstate->sw;
778 magic = 0xffff;
779 err |= __put_user(magic, &fpstate->magic);
780 err |= __put_user(fpstate_addr, &sc->fpstate);
782 /* non-iBCS2 extensions.. */
783 err |= __put_user(mask, &sc->oldmask);
784 err |= __put_user(env->cr[2], &sc->cr2);
785 return err;
789 * Determine which stack to use..
792 static inline abi_ulong
793 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
795 unsigned long esp;
797 /* Default to using normal stack */
798 esp = env->regs[R_ESP];
799 /* This is the X/Open sanctioned signal stack switching. */
800 if (ka->sa_flags & TARGET_SA_ONSTACK) {
801 if (sas_ss_flags(esp) == 0)
802 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
805 /* This is the legacy signal stack switching. */
806 else
807 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
808 !(ka->sa_flags & TARGET_SA_RESTORER) &&
809 ka->sa_restorer) {
810 esp = (unsigned long) ka->sa_restorer;
812 return (esp - frame_size) & -8ul;
815 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
816 static void setup_frame(int sig, struct target_sigaction *ka,
817 target_sigset_t *set, CPUX86State *env)
819 abi_ulong frame_addr;
820 struct sigframe *frame;
821 int i, err = 0;
823 frame_addr = get_sigframe(ka, env, sizeof(*frame));
825 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
826 goto give_sigsegv;
828 err |= __put_user(current_exec_domain_sig(sig),
829 &frame->sig);
830 if (err)
831 goto give_sigsegv;
833 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
834 frame_addr + offsetof(struct sigframe, fpstate));
835 if (err)
836 goto give_sigsegv;
838 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
839 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
840 goto give_sigsegv;
843 /* Set up to return from userspace. If provided, use a stub
844 already in userspace. */
845 if (ka->sa_flags & TARGET_SA_RESTORER) {
846 err |= __put_user(ka->sa_restorer, &frame->pretcode);
847 } else {
848 uint16_t val16;
849 abi_ulong retcode_addr;
850 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
851 err |= __put_user(retcode_addr, &frame->pretcode);
852 /* This is popl %eax ; movl $,%eax ; int $0x80 */
853 val16 = 0xb858;
854 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
855 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
856 val16 = 0x80cd;
857 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
860 if (err)
861 goto give_sigsegv;
863 /* Set up registers for signal handler */
864 env->regs[R_ESP] = frame_addr;
865 env->eip = ka->_sa_handler;
867 cpu_x86_load_seg(env, R_DS, __USER_DS);
868 cpu_x86_load_seg(env, R_ES, __USER_DS);
869 cpu_x86_load_seg(env, R_SS, __USER_DS);
870 cpu_x86_load_seg(env, R_CS, __USER_CS);
871 env->eflags &= ~TF_MASK;
873 unlock_user_struct(frame, frame_addr, 1);
875 return;
877 give_sigsegv:
878 unlock_user_struct(frame, frame_addr, 1);
879 if (sig == TARGET_SIGSEGV)
880 ka->_sa_handler = TARGET_SIG_DFL;
881 force_sig(TARGET_SIGSEGV /* , current */);
884 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
885 static void setup_rt_frame(int sig, struct target_sigaction *ka,
886 target_siginfo_t *info,
887 target_sigset_t *set, CPUX86State *env)
889 abi_ulong frame_addr, addr;
890 struct rt_sigframe *frame;
891 int i, err = 0;
893 frame_addr = get_sigframe(ka, env, sizeof(*frame));
895 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
896 goto give_sigsegv;
898 err |= __put_user(current_exec_domain_sig(sig),
899 &frame->sig);
900 addr = frame_addr + offsetof(struct rt_sigframe, info);
901 err |= __put_user(addr, &frame->pinfo);
902 addr = frame_addr + offsetof(struct rt_sigframe, uc);
903 err |= __put_user(addr, &frame->puc);
904 err |= copy_siginfo_to_user(&frame->info, info);
905 if (err)
906 goto give_sigsegv;
908 /* Create the ucontext. */
909 err |= __put_user(0, &frame->uc.tuc_flags);
910 err |= __put_user(0, &frame->uc.tuc_link);
911 err |= __put_user(target_sigaltstack_used.ss_sp,
912 &frame->uc.tuc_stack.ss_sp);
913 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
914 &frame->uc.tuc_stack.ss_flags);
915 err |= __put_user(target_sigaltstack_used.ss_size,
916 &frame->uc.tuc_stack.ss_size);
917 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
918 env, set->sig[0],
919 frame_addr + offsetof(struct rt_sigframe, fpstate));
920 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
921 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
922 goto give_sigsegv;
925 /* Set up to return from userspace. If provided, use a stub
926 already in userspace. */
927 if (ka->sa_flags & TARGET_SA_RESTORER) {
928 err |= __put_user(ka->sa_restorer, &frame->pretcode);
929 } else {
930 uint16_t val16;
931 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
932 err |= __put_user(addr, &frame->pretcode);
933 /* This is movl $,%eax ; int $0x80 */
934 err |= __put_user(0xb8, (char *)(frame->retcode+0));
935 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
936 val16 = 0x80cd;
937 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
940 if (err)
941 goto give_sigsegv;
943 /* Set up registers for signal handler */
944 env->regs[R_ESP] = frame_addr;
945 env->eip = ka->_sa_handler;
947 cpu_x86_load_seg(env, R_DS, __USER_DS);
948 cpu_x86_load_seg(env, R_ES, __USER_DS);
949 cpu_x86_load_seg(env, R_SS, __USER_DS);
950 cpu_x86_load_seg(env, R_CS, __USER_CS);
951 env->eflags &= ~TF_MASK;
953 unlock_user_struct(frame, frame_addr, 1);
955 return;
957 give_sigsegv:
958 unlock_user_struct(frame, frame_addr, 1);
959 if (sig == TARGET_SIGSEGV)
960 ka->_sa_handler = TARGET_SIG_DFL;
961 force_sig(TARGET_SIGSEGV /* , current */);
964 static int
965 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
967 unsigned int err = 0;
968 abi_ulong fpstate_addr;
969 unsigned int tmpflags;
971 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
972 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
973 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
974 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
976 env->regs[R_EDI] = tswapl(sc->edi);
977 env->regs[R_ESI] = tswapl(sc->esi);
978 env->regs[R_EBP] = tswapl(sc->ebp);
979 env->regs[R_ESP] = tswapl(sc->esp);
980 env->regs[R_EBX] = tswapl(sc->ebx);
981 env->regs[R_EDX] = tswapl(sc->edx);
982 env->regs[R_ECX] = tswapl(sc->ecx);
983 env->eip = tswapl(sc->eip);
985 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
986 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
988 tmpflags = tswapl(sc->eflags);
989 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
990 // regs->orig_eax = -1; /* disable syscall checks */
992 fpstate_addr = tswapl(sc->fpstate);
993 if (fpstate_addr != 0) {
994 if (!access_ok(VERIFY_READ, fpstate_addr,
995 sizeof(struct target_fpstate)))
996 goto badframe;
997 cpu_x86_frstor(env, fpstate_addr, 1);
1000 *peax = tswapl(sc->eax);
1001 return err;
1002 badframe:
1003 return 1;
1006 long do_sigreturn(CPUX86State *env)
1008 struct sigframe *frame;
1009 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1010 target_sigset_t target_set;
1011 sigset_t set;
1012 int eax, i;
1014 #if defined(DEBUG_SIGNAL)
1015 fprintf(stderr, "do_sigreturn\n");
1016 #endif
1017 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1018 goto badframe;
1019 /* set blocked signals */
1020 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1021 goto badframe;
1022 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1023 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1024 goto badframe;
1027 target_to_host_sigset_internal(&set, &target_set);
1028 sigprocmask(SIG_SETMASK, &set, NULL);
1030 /* restore registers */
1031 if (restore_sigcontext(env, &frame->sc, &eax))
1032 goto badframe;
1033 unlock_user_struct(frame, frame_addr, 0);
1034 return eax;
1036 badframe:
1037 unlock_user_struct(frame, frame_addr, 0);
1038 force_sig(TARGET_SIGSEGV);
1039 return 0;
1042 long do_rt_sigreturn(CPUX86State *env)
1044 abi_ulong frame_addr;
1045 struct rt_sigframe *frame;
1046 sigset_t set;
1047 int eax;
1049 frame_addr = env->regs[R_ESP] - 4;
1050 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1051 goto badframe;
1052 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1053 sigprocmask(SIG_SETMASK, &set, NULL);
1055 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1056 goto badframe;
1058 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1059 get_sp_from_cpustate(env)) == -EFAULT)
1060 goto badframe;
1062 unlock_user_struct(frame, frame_addr, 0);
1063 return eax;
1065 badframe:
1066 unlock_user_struct(frame, frame_addr, 0);
1067 force_sig(TARGET_SIGSEGV);
1068 return 0;
1071 #elif defined(TARGET_ARM)
1073 struct target_sigcontext {
1074 abi_ulong trap_no;
1075 abi_ulong error_code;
1076 abi_ulong oldmask;
1077 abi_ulong arm_r0;
1078 abi_ulong arm_r1;
1079 abi_ulong arm_r2;
1080 abi_ulong arm_r3;
1081 abi_ulong arm_r4;
1082 abi_ulong arm_r5;
1083 abi_ulong arm_r6;
1084 abi_ulong arm_r7;
1085 abi_ulong arm_r8;
1086 abi_ulong arm_r9;
1087 abi_ulong arm_r10;
1088 abi_ulong arm_fp;
1089 abi_ulong arm_ip;
1090 abi_ulong arm_sp;
1091 abi_ulong arm_lr;
1092 abi_ulong arm_pc;
1093 abi_ulong arm_cpsr;
1094 abi_ulong fault_address;
1097 struct target_ucontext_v1 {
1098 abi_ulong tuc_flags;
1099 abi_ulong tuc_link;
1100 target_stack_t tuc_stack;
1101 struct target_sigcontext tuc_mcontext;
1102 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1105 struct target_ucontext_v2 {
1106 abi_ulong tuc_flags;
1107 abi_ulong tuc_link;
1108 target_stack_t tuc_stack;
1109 struct target_sigcontext tuc_mcontext;
1110 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1111 char __unused[128 - sizeof(sigset_t)];
1112 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1115 struct sigframe_v1
1117 struct target_sigcontext sc;
1118 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1119 abi_ulong retcode;
1122 struct sigframe_v2
1124 struct target_ucontext_v2 uc;
1125 abi_ulong retcode;
1128 struct rt_sigframe_v1
1130 abi_ulong pinfo;
1131 abi_ulong puc;
1132 struct target_siginfo info;
1133 struct target_ucontext_v1 uc;
1134 abi_ulong retcode;
1137 struct rt_sigframe_v2
1139 struct target_siginfo info;
1140 struct target_ucontext_v2 uc;
1141 abi_ulong retcode;
1144 #define TARGET_CONFIG_CPU_32 1
1147 * For ARM syscalls, we encode the syscall number into the instruction.
1149 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1150 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1153 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1154 * need two 16-bit instructions.
1156 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1157 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1159 static const abi_ulong retcodes[4] = {
1160 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1161 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1165 #define __get_user_error(x,p,e) __get_user(x, p)
1167 static inline int valid_user_regs(CPUState *regs)
1169 return 1;
1172 static void
1173 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1174 CPUState *env, abi_ulong mask)
1176 __put_user(env->regs[0], &sc->arm_r0);
1177 __put_user(env->regs[1], &sc->arm_r1);
1178 __put_user(env->regs[2], &sc->arm_r2);
1179 __put_user(env->regs[3], &sc->arm_r3);
1180 __put_user(env->regs[4], &sc->arm_r4);
1181 __put_user(env->regs[5], &sc->arm_r5);
1182 __put_user(env->regs[6], &sc->arm_r6);
1183 __put_user(env->regs[7], &sc->arm_r7);
1184 __put_user(env->regs[8], &sc->arm_r8);
1185 __put_user(env->regs[9], &sc->arm_r9);
1186 __put_user(env->regs[10], &sc->arm_r10);
1187 __put_user(env->regs[11], &sc->arm_fp);
1188 __put_user(env->regs[12], &sc->arm_ip);
1189 __put_user(env->regs[13], &sc->arm_sp);
1190 __put_user(env->regs[14], &sc->arm_lr);
1191 __put_user(env->regs[15], &sc->arm_pc);
1192 #ifdef TARGET_CONFIG_CPU_32
1193 __put_user(cpsr_read(env), &sc->arm_cpsr);
1194 #endif
1196 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1197 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1198 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1199 __put_user(mask, &sc->oldmask);
1202 static inline abi_ulong
1203 get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1205 unsigned long sp = regs->regs[13];
1208 * This is the X/Open sanctioned signal stack switching.
1210 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1211 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1213 * ATPCS B01 mandates 8-byte alignment
1215 return (sp - framesize) & ~7;
1218 static int
1219 setup_return(CPUState *env, struct target_sigaction *ka,
1220 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1222 abi_ulong handler = ka->_sa_handler;
1223 abi_ulong retcode;
1224 int thumb = handler & 1;
1226 if (ka->sa_flags & TARGET_SA_RESTORER) {
1227 retcode = ka->sa_restorer;
1228 } else {
1229 unsigned int idx = thumb;
1231 if (ka->sa_flags & TARGET_SA_SIGINFO)
1232 idx += 2;
1234 if (__put_user(retcodes[idx], rc))
1235 return 1;
1236 #if 0
1237 flush_icache_range((abi_ulong)rc,
1238 (abi_ulong)(rc + 1));
1239 #endif
1240 retcode = rc_addr + thumb;
1243 env->regs[0] = usig;
1244 env->regs[13] = frame_addr;
1245 env->regs[14] = retcode;
1246 env->regs[15] = handler & (thumb ? ~1 : ~3);
1247 env->thumb = thumb;
1249 #if 0
1250 #ifdef TARGET_CONFIG_CPU_32
1251 env->cpsr = cpsr;
1252 #endif
1253 #endif
1255 return 0;
1258 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1259 target_sigset_t *set, CPUState *env)
1261 struct target_sigaltstack stack;
1262 int i;
1264 /* Clear all the bits of the ucontext we don't use. */
1265 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1267 memset(&stack, 0, sizeof(stack));
1268 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1269 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1270 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1271 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1273 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1274 /* FIXME: Save coprocessor signal frame. */
1275 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1276 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1280 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1281 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1282 target_sigset_t *set, CPUState *regs)
1284 struct sigframe_v1 *frame;
1285 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1286 int i;
1288 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1289 return;
1291 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1293 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1294 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1295 goto end;
1298 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1299 frame_addr + offsetof(struct sigframe_v1, retcode));
1301 end:
1302 unlock_user_struct(frame, frame_addr, 1);
1305 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1306 target_sigset_t *set, CPUState *regs)
1308 struct sigframe_v2 *frame;
1309 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1311 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1312 return;
1314 setup_sigframe_v2(&frame->uc, set, regs);
1316 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1317 frame_addr + offsetof(struct sigframe_v2, retcode));
1319 unlock_user_struct(frame, frame_addr, 1);
1322 static void setup_frame(int usig, struct target_sigaction *ka,
1323 target_sigset_t *set, CPUState *regs)
1325 if (get_osversion() >= 0x020612) {
1326 setup_frame_v2(usig, ka, set, regs);
1327 } else {
1328 setup_frame_v1(usig, ka, set, regs);
1332 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1333 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1334 target_siginfo_t *info,
1335 target_sigset_t *set, CPUState *env)
1337 struct rt_sigframe_v1 *frame;
1338 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1339 struct target_sigaltstack stack;
1340 int i;
1341 abi_ulong info_addr, uc_addr;
1343 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1344 return /* 1 */;
1346 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1347 __put_user(info_addr, &frame->pinfo);
1348 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1349 __put_user(uc_addr, &frame->puc);
1350 copy_siginfo_to_user(&frame->info, info);
1352 /* Clear all the bits of the ucontext we don't use. */
1353 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1355 memset(&stack, 0, sizeof(stack));
1356 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1357 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1358 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1359 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1361 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1362 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1363 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1364 goto end;
1367 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1368 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1370 env->regs[1] = info_addr;
1371 env->regs[2] = uc_addr;
1373 end:
1374 unlock_user_struct(frame, frame_addr, 1);
1377 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1378 target_siginfo_t *info,
1379 target_sigset_t *set, CPUState *env)
1381 struct rt_sigframe_v2 *frame;
1382 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1383 abi_ulong info_addr, uc_addr;
1385 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1386 return /* 1 */;
1388 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1389 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1390 copy_siginfo_to_user(&frame->info, info);
1392 setup_sigframe_v2(&frame->uc, set, env);
1394 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1395 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1397 env->regs[1] = info_addr;
1398 env->regs[2] = uc_addr;
1400 unlock_user_struct(frame, frame_addr, 1);
1403 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1404 target_siginfo_t *info,
1405 target_sigset_t *set, CPUState *env)
1407 if (get_osversion() >= 0x020612) {
1408 setup_rt_frame_v2(usig, ka, info, set, env);
1409 } else {
1410 setup_rt_frame_v1(usig, ka, info, set, env);
1414 static int
1415 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1417 int err = 0;
1418 uint32_t cpsr;
1420 __get_user_error(env->regs[0], &sc->arm_r0, err);
1421 __get_user_error(env->regs[1], &sc->arm_r1, err);
1422 __get_user_error(env->regs[2], &sc->arm_r2, err);
1423 __get_user_error(env->regs[3], &sc->arm_r3, err);
1424 __get_user_error(env->regs[4], &sc->arm_r4, err);
1425 __get_user_error(env->regs[5], &sc->arm_r5, err);
1426 __get_user_error(env->regs[6], &sc->arm_r6, err);
1427 __get_user_error(env->regs[7], &sc->arm_r7, err);
1428 __get_user_error(env->regs[8], &sc->arm_r8, err);
1429 __get_user_error(env->regs[9], &sc->arm_r9, err);
1430 __get_user_error(env->regs[10], &sc->arm_r10, err);
1431 __get_user_error(env->regs[11], &sc->arm_fp, err);
1432 __get_user_error(env->regs[12], &sc->arm_ip, err);
1433 __get_user_error(env->regs[13], &sc->arm_sp, err);
1434 __get_user_error(env->regs[14], &sc->arm_lr, err);
1435 __get_user_error(env->regs[15], &sc->arm_pc, err);
1436 #ifdef TARGET_CONFIG_CPU_32
1437 __get_user_error(cpsr, &sc->arm_cpsr, err);
1438 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1439 #endif
1441 err |= !valid_user_regs(env);
1443 return err;
1446 static long do_sigreturn_v1(CPUState *env)
1448 abi_ulong frame_addr;
1449 struct sigframe_v1 *frame;
1450 target_sigset_t set;
1451 sigset_t host_set;
1452 int i;
1455 * Since we stacked the signal on a 64-bit boundary,
1456 * then 'sp' should be word aligned here. If it's
1457 * not, then the user is trying to mess with us.
1459 if (env->regs[13] & 7)
1460 goto badframe;
1462 frame_addr = env->regs[13];
1463 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1464 goto badframe;
1466 if (__get_user(set.sig[0], &frame->sc.oldmask))
1467 goto badframe;
1468 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1469 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1470 goto badframe;
1473 target_to_host_sigset_internal(&host_set, &set);
1474 sigprocmask(SIG_SETMASK, &host_set, NULL);
1476 if (restore_sigcontext(env, &frame->sc))
1477 goto badframe;
1479 #if 0
1480 /* Send SIGTRAP if we're single-stepping */
1481 if (ptrace_cancel_bpt(current))
1482 send_sig(SIGTRAP, current, 1);
1483 #endif
1484 unlock_user_struct(frame, frame_addr, 0);
1485 return env->regs[0];
1487 badframe:
1488 unlock_user_struct(frame, frame_addr, 0);
1489 force_sig(TARGET_SIGSEGV /* , current */);
1490 return 0;
1493 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1494 struct target_ucontext_v2 *uc)
1496 sigset_t host_set;
1498 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1499 sigprocmask(SIG_SETMASK, &host_set, NULL);
1501 if (restore_sigcontext(env, &uc->tuc_mcontext))
1502 return 1;
1504 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1505 return 1;
1507 #if 0
1508 /* Send SIGTRAP if we're single-stepping */
1509 if (ptrace_cancel_bpt(current))
1510 send_sig(SIGTRAP, current, 1);
1511 #endif
1513 return 0;
1516 static long do_sigreturn_v2(CPUState *env)
1518 abi_ulong frame_addr;
1519 struct sigframe_v2 *frame;
1522 * Since we stacked the signal on a 64-bit boundary,
1523 * then 'sp' should be word aligned here. If it's
1524 * not, then the user is trying to mess with us.
1526 if (env->regs[13] & 7)
1527 goto badframe;
1529 frame_addr = env->regs[13];
1530 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1531 goto badframe;
1533 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1534 goto badframe;
1536 unlock_user_struct(frame, frame_addr, 0);
1537 return env->regs[0];
1539 badframe:
1540 unlock_user_struct(frame, frame_addr, 0);
1541 force_sig(TARGET_SIGSEGV /* , current */);
1542 return 0;
1545 long do_sigreturn(CPUState *env)
1547 if (get_osversion() >= 0x020612) {
1548 return do_sigreturn_v2(env);
1549 } else {
1550 return do_sigreturn_v1(env);
1554 static long do_rt_sigreturn_v1(CPUState *env)
1556 abi_ulong frame_addr;
1557 struct rt_sigframe_v1 *frame;
1558 sigset_t host_set;
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 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1573 sigprocmask(SIG_SETMASK, &host_set, NULL);
1575 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1576 goto badframe;
1578 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1579 goto badframe;
1581 #if 0
1582 /* Send SIGTRAP if we're single-stepping */
1583 if (ptrace_cancel_bpt(current))
1584 send_sig(SIGTRAP, current, 1);
1585 #endif
1586 unlock_user_struct(frame, frame_addr, 0);
1587 return env->regs[0];
1589 badframe:
1590 unlock_user_struct(frame, frame_addr, 0);
1591 force_sig(TARGET_SIGSEGV /* , current */);
1592 return 0;
1595 static long do_rt_sigreturn_v2(CPUState *env)
1597 abi_ulong frame_addr;
1598 struct rt_sigframe_v2 *frame;
1601 * Since we stacked the signal on a 64-bit boundary,
1602 * then 'sp' should be word aligned here. If it's
1603 * not, then the user is trying to mess with us.
1605 if (env->regs[13] & 7)
1606 goto badframe;
1608 frame_addr = env->regs[13];
1609 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1610 goto badframe;
1612 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1613 goto badframe;
1615 unlock_user_struct(frame, frame_addr, 0);
1616 return env->regs[0];
1618 badframe:
1619 unlock_user_struct(frame, frame_addr, 0);
1620 force_sig(TARGET_SIGSEGV /* , current */);
1621 return 0;
1624 long do_rt_sigreturn(CPUState *env)
1626 if (get_osversion() >= 0x020612) {
1627 return do_rt_sigreturn_v2(env);
1628 } else {
1629 return do_rt_sigreturn_v1(env);
1633 #elif defined(TARGET_SPARC)
1635 #define __SUNOS_MAXWIN 31
1637 /* This is what SunOS does, so shall I. */
1638 struct target_sigcontext {
1639 abi_ulong sigc_onstack; /* state to restore */
1641 abi_ulong sigc_mask; /* sigmask to restore */
1642 abi_ulong sigc_sp; /* stack pointer */
1643 abi_ulong sigc_pc; /* program counter */
1644 abi_ulong sigc_npc; /* next program counter */
1645 abi_ulong sigc_psr; /* for condition codes etc */
1646 abi_ulong sigc_g1; /* User uses these two registers */
1647 abi_ulong sigc_o0; /* within the trampoline code. */
1649 /* Now comes information regarding the users window set
1650 * at the time of the signal.
1652 abi_ulong sigc_oswins; /* outstanding windows */
1654 /* stack ptrs for each regwin buf */
1655 char *sigc_spbuf[__SUNOS_MAXWIN];
1657 /* Windows to restore after signal */
1658 struct {
1659 abi_ulong locals[8];
1660 abi_ulong ins[8];
1661 } sigc_wbuf[__SUNOS_MAXWIN];
1663 /* A Sparc stack frame */
1664 struct sparc_stackf {
1665 abi_ulong locals[8];
1666 abi_ulong ins[6];
1667 struct sparc_stackf *fp;
1668 abi_ulong callers_pc;
1669 char *structptr;
1670 abi_ulong xargs[6];
1671 abi_ulong xxargs[1];
1674 typedef struct {
1675 struct {
1676 abi_ulong psr;
1677 abi_ulong pc;
1678 abi_ulong npc;
1679 abi_ulong y;
1680 abi_ulong u_regs[16]; /* globals and ins */
1681 } si_regs;
1682 int si_mask;
1683 } __siginfo_t;
1685 typedef struct {
1686 unsigned long si_float_regs [32];
1687 unsigned long si_fsr;
1688 unsigned long si_fpqdepth;
1689 struct {
1690 unsigned long *insn_addr;
1691 unsigned long insn;
1692 } si_fpqueue [16];
1693 } qemu_siginfo_fpu_t;
1696 struct target_signal_frame {
1697 struct sparc_stackf ss;
1698 __siginfo_t info;
1699 abi_ulong fpu_save;
1700 abi_ulong insns[2] __attribute__ ((aligned (8)));
1701 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1702 abi_ulong extra_size; /* Should be 0 */
1703 qemu_siginfo_fpu_t fpu_state;
1705 struct target_rt_signal_frame {
1706 struct sparc_stackf ss;
1707 siginfo_t info;
1708 abi_ulong regs[20];
1709 sigset_t mask;
1710 abi_ulong fpu_save;
1711 unsigned int insns[2];
1712 stack_t stack;
1713 unsigned int extra_size; /* Should be 0 */
1714 qemu_siginfo_fpu_t fpu_state;
1717 #define UREG_O0 16
1718 #define UREG_O6 22
1719 #define UREG_I0 0
1720 #define UREG_I1 1
1721 #define UREG_I2 2
1722 #define UREG_I3 3
1723 #define UREG_I4 4
1724 #define UREG_I5 5
1725 #define UREG_I6 6
1726 #define UREG_I7 7
1727 #define UREG_L0 8
1728 #define UREG_FP UREG_I6
1729 #define UREG_SP UREG_O6
1731 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1732 CPUState *env, unsigned long framesize)
1734 abi_ulong sp;
1736 sp = env->regwptr[UREG_FP];
1738 /* This is the X/Open sanctioned signal stack switching. */
1739 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1740 if (!on_sig_stack(sp)
1741 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1742 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1744 return sp - framesize;
1747 static int
1748 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1750 int err = 0, i;
1752 err |= __put_user(env->psr, &si->si_regs.psr);
1753 err |= __put_user(env->pc, &si->si_regs.pc);
1754 err |= __put_user(env->npc, &si->si_regs.npc);
1755 err |= __put_user(env->y, &si->si_regs.y);
1756 for (i=0; i < 8; i++) {
1757 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1759 for (i=0; i < 8; i++) {
1760 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1762 err |= __put_user(mask, &si->si_mask);
1763 return err;
1766 #if 0
1767 static int
1768 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1769 CPUState *env, unsigned long mask)
1771 int err = 0;
1773 err |= __put_user(mask, &sc->sigc_mask);
1774 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1775 err |= __put_user(env->pc, &sc->sigc_pc);
1776 err |= __put_user(env->npc, &sc->sigc_npc);
1777 err |= __put_user(env->psr, &sc->sigc_psr);
1778 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1779 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1781 return err;
1783 #endif
1784 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1786 static void setup_frame(int sig, struct target_sigaction *ka,
1787 target_sigset_t *set, CPUState *env)
1789 abi_ulong sf_addr;
1790 struct target_signal_frame *sf;
1791 int sigframe_size, err, i;
1793 /* 1. Make sure everything is clean */
1794 //synchronize_user_stack();
1796 sigframe_size = NF_ALIGNEDSZ;
1797 sf_addr = get_sigframe(ka, env, sigframe_size);
1799 sf = lock_user(VERIFY_WRITE, sf_addr,
1800 sizeof(struct target_signal_frame), 0);
1801 if (!sf)
1802 goto sigsegv;
1804 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1805 #if 0
1806 if (invalid_frame_pointer(sf, sigframe_size))
1807 goto sigill_and_return;
1808 #endif
1809 /* 2. Save the current process state */
1810 err = setup___siginfo(&sf->info, env, set->sig[0]);
1811 err |= __put_user(0, &sf->extra_size);
1813 //err |= save_fpu_state(regs, &sf->fpu_state);
1814 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1816 err |= __put_user(set->sig[0], &sf->info.si_mask);
1817 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1818 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1821 for (i = 0; i < 8; i++) {
1822 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1824 for (i = 0; i < 8; i++) {
1825 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1827 if (err)
1828 goto sigsegv;
1830 /* 3. signal handler back-trampoline and parameters */
1831 env->regwptr[UREG_FP] = sf_addr;
1832 env->regwptr[UREG_I0] = sig;
1833 env->regwptr[UREG_I1] = sf_addr +
1834 offsetof(struct target_signal_frame, info);
1835 env->regwptr[UREG_I2] = sf_addr +
1836 offsetof(struct target_signal_frame, info);
1838 /* 4. signal handler */
1839 env->pc = ka->_sa_handler;
1840 env->npc = (env->pc + 4);
1841 /* 5. return to kernel instructions */
1842 if (ka->sa_restorer)
1843 env->regwptr[UREG_I7] = ka->sa_restorer;
1844 else {
1845 uint32_t val32;
1847 env->regwptr[UREG_I7] = sf_addr +
1848 offsetof(struct target_signal_frame, insns) - 2 * 4;
1850 /* mov __NR_sigreturn, %g1 */
1851 val32 = 0x821020d8;
1852 err |= __put_user(val32, &sf->insns[0]);
1854 /* t 0x10 */
1855 val32 = 0x91d02010;
1856 err |= __put_user(val32, &sf->insns[1]);
1857 if (err)
1858 goto sigsegv;
1860 /* Flush instruction space. */
1861 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1862 // tb_flush(env);
1864 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1865 return;
1866 #if 0
1867 sigill_and_return:
1868 force_sig(TARGET_SIGILL);
1869 #endif
1870 sigsegv:
1871 //fprintf(stderr, "force_sig\n");
1872 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1873 force_sig(TARGET_SIGSEGV);
1875 static inline int
1876 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
1878 int err;
1879 #if 0
1880 #ifdef CONFIG_SMP
1881 if (current->flags & PF_USEDFPU)
1882 regs->psr &= ~PSR_EF;
1883 #else
1884 if (current == last_task_used_math) {
1885 last_task_used_math = 0;
1886 regs->psr &= ~PSR_EF;
1888 #endif
1889 current->used_math = 1;
1890 current->flags &= ~PF_USEDFPU;
1891 #endif
1892 #if 0
1893 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1894 return -EFAULT;
1895 #endif
1897 #if 0
1898 /* XXX: incorrect */
1899 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1900 (sizeof(unsigned long) * 32));
1901 #endif
1902 err |= __get_user(env->fsr, &fpu->si_fsr);
1903 #if 0
1904 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1905 if (current->thread.fpqdepth != 0)
1906 err |= __copy_from_user(&current->thread.fpqueue[0],
1907 &fpu->si_fpqueue[0],
1908 ((sizeof(unsigned long) +
1909 (sizeof(unsigned long *)))*16));
1910 #endif
1911 return err;
1915 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1916 target_siginfo_t *info,
1917 target_sigset_t *set, CPUState *env)
1919 fprintf(stderr, "setup_rt_frame: not implemented\n");
1922 long do_sigreturn(CPUState *env)
1924 abi_ulong sf_addr;
1925 struct target_signal_frame *sf;
1926 uint32_t up_psr, pc, npc;
1927 target_sigset_t set;
1928 sigset_t host_set;
1929 abi_ulong fpu_save_addr;
1930 int err, i;
1932 sf_addr = env->regwptr[UREG_FP];
1933 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1934 goto segv_and_exit;
1935 #if 0
1936 fprintf(stderr, "sigreturn\n");
1937 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1938 #endif
1939 //cpu_dump_state(env, stderr, fprintf, 0);
1941 /* 1. Make sure we are not getting garbage from the user */
1943 if (sf_addr & 3)
1944 goto segv_and_exit;
1946 err = __get_user(pc, &sf->info.si_regs.pc);
1947 err |= __get_user(npc, &sf->info.si_regs.npc);
1949 if ((pc | npc) & 3)
1950 goto segv_and_exit;
1952 /* 2. Restore the state */
1953 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1955 /* User can only change condition codes and FPU enabling in %psr. */
1956 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1957 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1959 env->pc = pc;
1960 env->npc = npc;
1961 err |= __get_user(env->y, &sf->info.si_regs.y);
1962 for (i=0; i < 8; i++) {
1963 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1965 for (i=0; i < 8; i++) {
1966 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1969 err |= __get_user(fpu_save_addr, &sf->fpu_save);
1971 //if (fpu_save)
1972 // err |= restore_fpu_state(env, fpu_save);
1974 /* This is pretty much atomic, no amount locking would prevent
1975 * the races which exist anyways.
1977 err |= __get_user(set.sig[0], &sf->info.si_mask);
1978 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1979 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1982 target_to_host_sigset_internal(&host_set, &set);
1983 sigprocmask(SIG_SETMASK, &host_set, NULL);
1985 if (err)
1986 goto segv_and_exit;
1987 unlock_user_struct(sf, sf_addr, 0);
1988 return env->regwptr[0];
1990 segv_and_exit:
1991 unlock_user_struct(sf, sf_addr, 0);
1992 force_sig(TARGET_SIGSEGV);
1995 long do_rt_sigreturn(CPUState *env)
1997 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1998 return -TARGET_ENOSYS;
2001 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2002 #define MC_TSTATE 0
2003 #define MC_PC 1
2004 #define MC_NPC 2
2005 #define MC_Y 3
2006 #define MC_G1 4
2007 #define MC_G2 5
2008 #define MC_G3 6
2009 #define MC_G4 7
2010 #define MC_G5 8
2011 #define MC_G6 9
2012 #define MC_G7 10
2013 #define MC_O0 11
2014 #define MC_O1 12
2015 #define MC_O2 13
2016 #define MC_O3 14
2017 #define MC_O4 15
2018 #define MC_O5 16
2019 #define MC_O6 17
2020 #define MC_O7 18
2021 #define MC_NGREG 19
2023 typedef abi_ulong target_mc_greg_t;
2024 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2026 struct target_mc_fq {
2027 abi_ulong *mcfq_addr;
2028 uint32_t mcfq_insn;
2031 struct target_mc_fpu {
2032 union {
2033 uint32_t sregs[32];
2034 uint64_t dregs[32];
2035 //uint128_t qregs[16];
2036 } mcfpu_fregs;
2037 abi_ulong mcfpu_fsr;
2038 abi_ulong mcfpu_fprs;
2039 abi_ulong mcfpu_gsr;
2040 struct target_mc_fq *mcfpu_fq;
2041 unsigned char mcfpu_qcnt;
2042 unsigned char mcfpu_qentsz;
2043 unsigned char mcfpu_enab;
2045 typedef struct target_mc_fpu target_mc_fpu_t;
2047 typedef struct {
2048 target_mc_gregset_t mc_gregs;
2049 target_mc_greg_t mc_fp;
2050 target_mc_greg_t mc_i7;
2051 target_mc_fpu_t mc_fpregs;
2052 } target_mcontext_t;
2054 struct target_ucontext {
2055 struct target_ucontext *tuc_link;
2056 abi_ulong tuc_flags;
2057 target_sigset_t tuc_sigmask;
2058 target_mcontext_t tuc_mcontext;
2061 /* A V9 register window */
2062 struct target_reg_window {
2063 abi_ulong locals[8];
2064 abi_ulong ins[8];
2067 #define TARGET_STACK_BIAS 2047
2069 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2070 void sparc64_set_context(CPUSPARCState *env)
2072 abi_ulong ucp_addr;
2073 struct target_ucontext *ucp;
2074 target_mc_gregset_t *grp;
2075 abi_ulong pc, npc, tstate;
2076 abi_ulong fp, i7, w_addr;
2077 unsigned char fenab;
2078 int err;
2079 unsigned int i;
2081 ucp_addr = env->regwptr[UREG_I0];
2082 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2083 goto do_sigsegv;
2084 grp = &ucp->tuc_mcontext.mc_gregs;
2085 err = __get_user(pc, &((*grp)[MC_PC]));
2086 err |= __get_user(npc, &((*grp)[MC_NPC]));
2087 if (err || ((pc | npc) & 3))
2088 goto do_sigsegv;
2089 if (env->regwptr[UREG_I1]) {
2090 target_sigset_t target_set;
2091 sigset_t set;
2093 if (TARGET_NSIG_WORDS == 1) {
2094 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2095 goto do_sigsegv;
2096 } else {
2097 abi_ulong *src, *dst;
2098 src = ucp->tuc_sigmask.sig;
2099 dst = target_set.sig;
2100 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2101 i++, dst++, src++)
2102 err |= __get_user(*dst, src);
2103 if (err)
2104 goto do_sigsegv;
2106 target_to_host_sigset_internal(&set, &target_set);
2107 sigprocmask(SIG_SETMASK, &set, NULL);
2109 env->pc = pc;
2110 env->npc = npc;
2111 err |= __get_user(env->y, &((*grp)[MC_Y]));
2112 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2113 env->asi = (tstate >> 24) & 0xff;
2114 PUT_CCR(env, tstate >> 32);
2115 PUT_CWP64(env, tstate & 0x1f);
2116 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2117 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2118 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2119 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2120 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2121 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2122 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2123 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2124 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2125 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2126 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2127 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2128 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2129 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2130 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2132 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2133 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2135 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2136 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2137 abi_ulong) != 0)
2138 goto do_sigsegv;
2139 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2140 abi_ulong) != 0)
2141 goto do_sigsegv;
2142 err |= __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2143 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2145 uint32_t *src, *dst;
2146 src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2147 dst = env->fpr;
2148 /* XXX: check that the CPU storage is the same as user context */
2149 for (i = 0; i < 64; i++, dst++, src++)
2150 err |= __get_user(*dst, src);
2152 err |= __get_user(env->fsr,
2153 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2154 err |= __get_user(env->gsr,
2155 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2156 if (err)
2157 goto do_sigsegv;
2158 unlock_user_struct(ucp, ucp_addr, 0);
2159 return;
2160 do_sigsegv:
2161 unlock_user_struct(ucp, ucp_addr, 0);
2162 force_sig(TARGET_SIGSEGV);
2165 void sparc64_get_context(CPUSPARCState *env)
2167 abi_ulong ucp_addr;
2168 struct target_ucontext *ucp;
2169 target_mc_gregset_t *grp;
2170 target_mcontext_t *mcp;
2171 abi_ulong fp, i7, w_addr;
2172 int err;
2173 unsigned int i;
2174 target_sigset_t target_set;
2175 sigset_t set;
2177 ucp_addr = env->regwptr[UREG_I0];
2178 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2179 goto do_sigsegv;
2181 mcp = &ucp->tuc_mcontext;
2182 grp = &mcp->mc_gregs;
2184 /* Skip over the trap instruction, first. */
2185 env->pc = env->npc;
2186 env->npc += 4;
2188 err = 0;
2190 sigprocmask(0, NULL, &set);
2191 host_to_target_sigset_internal(&target_set, &set);
2192 if (TARGET_NSIG_WORDS == 1) {
2193 err |= __put_user(target_set.sig[0],
2194 (abi_ulong *)&ucp->tuc_sigmask);
2195 } else {
2196 abi_ulong *src, *dst;
2197 src = target_set.sig;
2198 dst = ucp->tuc_sigmask.sig;
2199 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2200 i++, dst++, src++)
2201 err |= __put_user(*src, dst);
2202 if (err)
2203 goto do_sigsegv;
2206 /* XXX: tstate must be saved properly */
2207 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2208 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2209 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2210 err |= __put_user(env->y, &((*grp)[MC_Y]));
2211 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2212 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2213 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2214 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2215 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2216 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2217 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2218 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2219 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2220 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2221 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2222 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2223 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2224 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2225 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2227 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2228 fp = i7 = 0;
2229 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2230 abi_ulong) != 0)
2231 goto do_sigsegv;
2232 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2233 abi_ulong) != 0)
2234 goto do_sigsegv;
2235 err |= __put_user(fp, &(mcp->mc_fp));
2236 err |= __put_user(i7, &(mcp->mc_i7));
2239 uint32_t *src, *dst;
2240 src = env->fpr;
2241 dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2242 /* XXX: check that the CPU storage is the same as user context */
2243 for (i = 0; i < 64; i++, dst++, src++)
2244 err |= __put_user(*src, dst);
2246 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2247 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2248 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2250 if (err)
2251 goto do_sigsegv;
2252 unlock_user_struct(ucp, ucp_addr, 1);
2253 return;
2254 do_sigsegv:
2255 unlock_user_struct(ucp, ucp_addr, 1);
2256 force_sig(TARGET_SIGSEGV);
2258 #endif
2259 #elif defined(TARGET_ABI_MIPSN64)
2261 # warning signal handling not implemented
2263 static void setup_frame(int sig, struct target_sigaction *ka,
2264 target_sigset_t *set, CPUState *env)
2266 fprintf(stderr, "setup_frame: not implemented\n");
2269 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2270 target_siginfo_t *info,
2271 target_sigset_t *set, CPUState *env)
2273 fprintf(stderr, "setup_rt_frame: not implemented\n");
2276 long do_sigreturn(CPUState *env)
2278 fprintf(stderr, "do_sigreturn: not implemented\n");
2279 return -TARGET_ENOSYS;
2282 long do_rt_sigreturn(CPUState *env)
2284 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2285 return -TARGET_ENOSYS;
2288 #elif defined(TARGET_ABI_MIPSN32)
2290 # warning signal handling not implemented
2292 static void setup_frame(int sig, struct target_sigaction *ka,
2293 target_sigset_t *set, CPUState *env)
2295 fprintf(stderr, "setup_frame: not implemented\n");
2298 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2299 target_siginfo_t *info,
2300 target_sigset_t *set, CPUState *env)
2302 fprintf(stderr, "setup_rt_frame: not implemented\n");
2305 long do_sigreturn(CPUState *env)
2307 fprintf(stderr, "do_sigreturn: not implemented\n");
2308 return -TARGET_ENOSYS;
2311 long do_rt_sigreturn(CPUState *env)
2313 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2314 return -TARGET_ENOSYS;
2317 #elif defined(TARGET_ABI_MIPSO32)
2319 struct target_sigcontext {
2320 uint32_t sc_regmask; /* Unused */
2321 uint32_t sc_status;
2322 uint64_t sc_pc;
2323 uint64_t sc_regs[32];
2324 uint64_t sc_fpregs[32];
2325 uint32_t sc_ownedfp; /* Unused */
2326 uint32_t sc_fpc_csr;
2327 uint32_t sc_fpc_eir; /* Unused */
2328 uint32_t sc_used_math;
2329 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2330 uint32_t pad0;
2331 uint64_t sc_mdhi;
2332 uint64_t sc_mdlo;
2333 target_ulong sc_hi1; /* Was sc_cause */
2334 target_ulong sc_lo1; /* Was sc_badvaddr */
2335 target_ulong sc_hi2; /* Was sc_sigset[4] */
2336 target_ulong sc_lo2;
2337 target_ulong sc_hi3;
2338 target_ulong sc_lo3;
2341 struct sigframe {
2342 uint32_t sf_ass[4]; /* argument save space for o32 */
2343 uint32_t sf_code[2]; /* signal trampoline */
2344 struct target_sigcontext sf_sc;
2345 target_sigset_t sf_mask;
2348 struct target_ucontext {
2349 target_ulong tuc_flags;
2350 target_ulong tuc_link;
2351 target_stack_t tuc_stack;
2352 target_ulong pad0;
2353 struct target_sigcontext tuc_mcontext;
2354 target_sigset_t tuc_sigmask;
2357 struct target_rt_sigframe {
2358 uint32_t rs_ass[4]; /* argument save space for o32 */
2359 uint32_t rs_code[2]; /* signal trampoline */
2360 struct target_siginfo rs_info;
2361 struct target_ucontext rs_uc;
2364 /* Install trampoline to jump back from signal handler */
2365 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2367 int err;
2370 * Set up the return code ...
2372 * li v0, __NR__foo_sigreturn
2373 * syscall
2376 err = __put_user(0x24020000 + syscall, tramp + 0);
2377 err |= __put_user(0x0000000c , tramp + 1);
2378 /* flush_cache_sigtramp((unsigned long) tramp); */
2379 return err;
2382 static inline int
2383 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2385 int err = 0;
2387 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2389 #define save_gp_reg(i) do { \
2390 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2391 } while(0)
2392 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2393 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2394 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2395 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2396 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2397 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2398 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2399 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2400 save_gp_reg(31);
2401 #undef save_gp_reg
2403 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2404 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2406 /* Not used yet, but might be useful if we ever have DSP suppport */
2407 #if 0
2408 if (cpu_has_dsp) {
2409 err |= __put_user(mfhi1(), &sc->sc_hi1);
2410 err |= __put_user(mflo1(), &sc->sc_lo1);
2411 err |= __put_user(mfhi2(), &sc->sc_hi2);
2412 err |= __put_user(mflo2(), &sc->sc_lo2);
2413 err |= __put_user(mfhi3(), &sc->sc_hi3);
2414 err |= __put_user(mflo3(), &sc->sc_lo3);
2415 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2417 /* same with 64 bit */
2418 #ifdef CONFIG_64BIT
2419 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2420 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2421 if (cpu_has_dsp) {
2422 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2423 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2424 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2425 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2426 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2427 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2428 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2430 #endif
2431 #endif
2433 #if 0
2434 err |= __put_user(!!used_math(), &sc->sc_used_math);
2436 if (!used_math())
2437 goto out;
2440 * Save FPU state to signal context. Signal handler will "inherit"
2441 * current FPU state.
2443 preempt_disable();
2445 if (!is_fpu_owner()) {
2446 own_fpu();
2447 restore_fp(current);
2449 err |= save_fp_context(sc);
2451 preempt_enable();
2452 out:
2453 #endif
2454 return err;
2457 static inline int
2458 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2460 int err = 0;
2462 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2464 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2465 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2467 #define restore_gp_reg(i) do { \
2468 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2469 } while(0)
2470 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2471 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2472 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2473 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2474 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2475 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2476 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2477 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2478 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2479 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2480 restore_gp_reg(31);
2481 #undef restore_gp_reg
2483 #if 0
2484 if (cpu_has_dsp) {
2485 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2486 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2487 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2488 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2489 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2490 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2491 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2493 #ifdef CONFIG_64BIT
2494 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2495 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2496 if (cpu_has_dsp) {
2497 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2498 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2499 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2500 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2501 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2502 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2503 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2505 #endif
2507 err |= __get_user(used_math, &sc->sc_used_math);
2508 conditional_used_math(used_math);
2510 preempt_disable();
2512 if (used_math()) {
2513 /* restore fpu context if we have used it before */
2514 own_fpu();
2515 err |= restore_fp_context(sc);
2516 } else {
2517 /* signal handler may have used FPU. Give it up. */
2518 lose_fpu();
2521 preempt_enable();
2522 #endif
2523 return err;
2526 * Determine which stack to use..
2528 static inline abi_ulong
2529 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2531 unsigned long sp;
2533 /* Default to using normal stack */
2534 sp = regs->active_tc.gpr[29];
2537 * FPU emulator may have it's own trampoline active just
2538 * above the user stack, 16-bytes before the next lowest
2539 * 16 byte boundary. Try to avoid trashing it.
2541 sp -= 32;
2543 /* This is the X/Open sanctioned signal stack switching. */
2544 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2545 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2548 return (sp - frame_size) & ~7;
2551 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2552 static void setup_frame(int sig, struct target_sigaction * ka,
2553 target_sigset_t *set, CPUState *regs)
2555 struct sigframe *frame;
2556 abi_ulong frame_addr;
2557 int i;
2559 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2560 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2561 goto give_sigsegv;
2563 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2565 if(setup_sigcontext(regs, &frame->sf_sc))
2566 goto give_sigsegv;
2568 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2569 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2570 goto give_sigsegv;
2574 * Arguments to signal handler:
2576 * a0 = signal number
2577 * a1 = 0 (should be cause)
2578 * a2 = pointer to struct sigcontext
2580 * $25 and PC point to the signal handler, $29 points to the
2581 * struct sigframe.
2583 regs->active_tc.gpr[ 4] = sig;
2584 regs->active_tc.gpr[ 5] = 0;
2585 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2586 regs->active_tc.gpr[29] = frame_addr;
2587 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2588 /* The original kernel code sets CP0_EPC to the handler
2589 * since it returns to userland using eret
2590 * we cannot do this here, and we must set PC directly */
2591 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2592 unlock_user_struct(frame, frame_addr, 1);
2593 return;
2595 give_sigsegv:
2596 unlock_user_struct(frame, frame_addr, 1);
2597 force_sig(TARGET_SIGSEGV/*, current*/);
2598 return;
2601 long do_sigreturn(CPUState *regs)
2603 struct sigframe *frame;
2604 abi_ulong frame_addr;
2605 sigset_t blocked;
2606 target_sigset_t target_set;
2607 int i;
2609 #if defined(DEBUG_SIGNAL)
2610 fprintf(stderr, "do_sigreturn\n");
2611 #endif
2612 frame_addr = regs->active_tc.gpr[29];
2613 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2614 goto badframe;
2616 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2617 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2618 goto badframe;
2621 target_to_host_sigset_internal(&blocked, &target_set);
2622 sigprocmask(SIG_SETMASK, &blocked, NULL);
2624 if (restore_sigcontext(regs, &frame->sf_sc))
2625 goto badframe;
2627 #if 0
2629 * Don't let your children do this ...
2631 __asm__ __volatile__(
2632 "move\t$29, %0\n\t"
2633 "j\tsyscall_exit"
2634 :/* no outputs */
2635 :"r" (&regs));
2636 /* Unreached */
2637 #endif
2639 regs->active_tc.PC = regs->CP0_EPC;
2640 /* I am not sure this is right, but it seems to work
2641 * maybe a problem with nested signals ? */
2642 regs->CP0_EPC = 0;
2643 return -TARGET_QEMU_ESIGRETURN;
2645 badframe:
2646 force_sig(TARGET_SIGSEGV/*, current*/);
2647 return 0;
2650 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2651 target_siginfo_t *info,
2652 target_sigset_t *set, CPUState *env)
2654 struct target_rt_sigframe *frame;
2655 abi_ulong frame_addr;
2656 int i;
2658 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2659 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2660 goto give_sigsegv;
2662 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2664 copy_siginfo_to_user(&frame->rs_info, info);
2666 __put_user(0, &frame->rs_uc.tuc_flags);
2667 __put_user(0, &frame->rs_uc.tuc_link);
2668 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2669 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2670 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2671 &frame->rs_uc.tuc_stack.ss_flags);
2673 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2675 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2676 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2680 * Arguments to signal handler:
2682 * a0 = signal number
2683 * a1 = pointer to struct siginfo
2684 * a2 = pointer to struct ucontext
2686 * $25 and PC point to the signal handler, $29 points to the
2687 * struct sigframe.
2689 env->active_tc.gpr[ 4] = sig;
2690 env->active_tc.gpr[ 5] = frame_addr
2691 + offsetof(struct target_rt_sigframe, rs_info);
2692 env->active_tc.gpr[ 6] = frame_addr
2693 + offsetof(struct target_rt_sigframe, rs_uc);
2694 env->active_tc.gpr[29] = frame_addr;
2695 env->active_tc.gpr[31] = frame_addr
2696 + offsetof(struct target_rt_sigframe, rs_code);
2697 /* The original kernel code sets CP0_EPC to the handler
2698 * since it returns to userland using eret
2699 * we cannot do this here, and we must set PC directly */
2700 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2701 unlock_user_struct(frame, frame_addr, 1);
2702 return;
2704 give_sigsegv:
2705 unlock_user_struct(frame, frame_addr, 1);
2706 force_sig(TARGET_SIGSEGV/*, current*/);
2707 return;
2710 long do_rt_sigreturn(CPUState *env)
2712 struct target_rt_sigframe *frame;
2713 abi_ulong frame_addr;
2714 sigset_t blocked;
2716 #if defined(DEBUG_SIGNAL)
2717 fprintf(stderr, "do_rt_sigreturn\n");
2718 #endif
2719 frame_addr = env->active_tc.gpr[29];
2720 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2721 goto badframe;
2723 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2724 sigprocmask(SIG_SETMASK, &blocked, NULL);
2726 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2727 goto badframe;
2729 if (do_sigaltstack(frame_addr +
2730 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2731 0, get_sp_from_cpustate(env)) == -EFAULT)
2732 goto badframe;
2734 env->active_tc.PC = env->CP0_EPC;
2735 /* I am not sure this is right, but it seems to work
2736 * maybe a problem with nested signals ? */
2737 env->CP0_EPC = 0;
2738 return -TARGET_QEMU_ESIGRETURN;
2740 badframe:
2741 force_sig(TARGET_SIGSEGV/*, current*/);
2742 return 0;
2745 #elif defined(TARGET_SH4)
2748 * code and data structures from linux kernel:
2749 * include/asm-sh/sigcontext.h
2750 * arch/sh/kernel/signal.c
2753 struct target_sigcontext {
2754 target_ulong oldmask;
2756 /* CPU registers */
2757 target_ulong sc_gregs[16];
2758 target_ulong sc_pc;
2759 target_ulong sc_pr;
2760 target_ulong sc_sr;
2761 target_ulong sc_gbr;
2762 target_ulong sc_mach;
2763 target_ulong sc_macl;
2765 /* FPU registers */
2766 target_ulong sc_fpregs[16];
2767 target_ulong sc_xfpregs[16];
2768 unsigned int sc_fpscr;
2769 unsigned int sc_fpul;
2770 unsigned int sc_ownedfp;
2773 struct target_sigframe
2775 struct target_sigcontext sc;
2776 target_ulong extramask[TARGET_NSIG_WORDS-1];
2777 uint16_t retcode[3];
2781 struct target_ucontext {
2782 target_ulong tuc_flags;
2783 struct target_ucontext *tuc_link;
2784 target_stack_t tuc_stack;
2785 struct target_sigcontext tuc_mcontext;
2786 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2789 struct target_rt_sigframe
2791 struct target_siginfo info;
2792 struct target_ucontext uc;
2793 uint16_t retcode[3];
2797 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2798 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2800 static abi_ulong get_sigframe(struct target_sigaction *ka,
2801 unsigned long sp, size_t frame_size)
2803 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2804 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2807 return (sp - frame_size) & -8ul;
2810 static int setup_sigcontext(struct target_sigcontext *sc,
2811 CPUState *regs, unsigned long mask)
2813 int err = 0;
2814 int i;
2816 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2817 COPY(gregs[0]); COPY(gregs[1]);
2818 COPY(gregs[2]); COPY(gregs[3]);
2819 COPY(gregs[4]); COPY(gregs[5]);
2820 COPY(gregs[6]); COPY(gregs[7]);
2821 COPY(gregs[8]); COPY(gregs[9]);
2822 COPY(gregs[10]); COPY(gregs[11]);
2823 COPY(gregs[12]); COPY(gregs[13]);
2824 COPY(gregs[14]); COPY(gregs[15]);
2825 COPY(gbr); COPY(mach);
2826 COPY(macl); COPY(pr);
2827 COPY(sr); COPY(pc);
2828 #undef COPY
2830 for (i=0; i<16; i++) {
2831 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2833 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2834 err |= __put_user(regs->fpul, &sc->sc_fpul);
2836 /* non-iBCS2 extensions.. */
2837 err |= __put_user(mask, &sc->oldmask);
2839 return err;
2842 static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
2843 target_ulong *r0_p)
2845 unsigned int err = 0;
2846 int i;
2848 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2849 COPY(gregs[1]);
2850 COPY(gregs[2]); COPY(gregs[3]);
2851 COPY(gregs[4]); COPY(gregs[5]);
2852 COPY(gregs[6]); COPY(gregs[7]);
2853 COPY(gregs[8]); COPY(gregs[9]);
2854 COPY(gregs[10]); COPY(gregs[11]);
2855 COPY(gregs[12]); COPY(gregs[13]);
2856 COPY(gregs[14]); COPY(gregs[15]);
2857 COPY(gbr); COPY(mach);
2858 COPY(macl); COPY(pr);
2859 COPY(sr); COPY(pc);
2860 #undef COPY
2862 for (i=0; i<16; i++) {
2863 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
2865 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
2866 err |= __get_user(regs->fpul, &sc->sc_fpul);
2868 regs->tra = -1; /* disable syscall checks */
2869 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
2870 return err;
2873 static void setup_frame(int sig, struct target_sigaction *ka,
2874 target_sigset_t *set, CPUState *regs)
2876 struct target_sigframe *frame;
2877 abi_ulong frame_addr;
2878 int i;
2879 int err = 0;
2880 int signal;
2882 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2883 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2884 goto give_sigsegv;
2886 signal = current_exec_domain_sig(sig);
2888 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2890 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2891 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2894 /* Set up to return from userspace. If provided, use a stub
2895 already in userspace. */
2896 if (ka->sa_flags & TARGET_SA_RESTORER) {
2897 regs->pr = (unsigned long) ka->sa_restorer;
2898 } else {
2899 /* Generate return code (system call to sigreturn) */
2900 err |= __put_user(MOVW(2), &frame->retcode[0]);
2901 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2902 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2903 regs->pr = (unsigned long) frame->retcode;
2906 if (err)
2907 goto give_sigsegv;
2909 /* Set up registers for signal handler */
2910 regs->gregs[15] = (unsigned long) frame;
2911 regs->gregs[4] = signal; /* Arg for signal handler */
2912 regs->gregs[5] = 0;
2913 regs->gregs[6] = (unsigned long) &frame->sc;
2914 regs->pc = (unsigned long) ka->_sa_handler;
2916 unlock_user_struct(frame, frame_addr, 1);
2917 return;
2919 give_sigsegv:
2920 unlock_user_struct(frame, frame_addr, 1);
2921 force_sig(TARGET_SIGSEGV);
2924 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2925 target_siginfo_t *info,
2926 target_sigset_t *set, CPUState *regs)
2928 struct target_rt_sigframe *frame;
2929 abi_ulong frame_addr;
2930 int i;
2931 int err = 0;
2932 int signal;
2934 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2935 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2936 goto give_sigsegv;
2938 signal = current_exec_domain_sig(sig);
2940 err |= copy_siginfo_to_user(&frame->info, info);
2942 /* Create the ucontext. */
2943 err |= __put_user(0, &frame->uc.tuc_flags);
2944 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
2945 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
2946 &frame->uc.tuc_stack.ss_sp);
2947 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2948 &frame->uc.tuc_stack.ss_flags);
2949 err |= __put_user(target_sigaltstack_used.ss_size,
2950 &frame->uc.tuc_stack.ss_size);
2951 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
2952 regs, set->sig[0]);
2953 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2954 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
2957 /* Set up to return from userspace. If provided, use a stub
2958 already in userspace. */
2959 if (ka->sa_flags & TARGET_SA_RESTORER) {
2960 regs->pr = (unsigned long) ka->sa_restorer;
2961 } else {
2962 /* Generate return code (system call to sigreturn) */
2963 err |= __put_user(MOVW(2), &frame->retcode[0]);
2964 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2965 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2966 regs->pr = (unsigned long) frame->retcode;
2969 if (err)
2970 goto give_sigsegv;
2972 /* Set up registers for signal handler */
2973 regs->gregs[15] = (unsigned long) frame;
2974 regs->gregs[4] = signal; /* Arg for signal handler */
2975 regs->gregs[5] = (unsigned long) &frame->info;
2976 regs->gregs[6] = (unsigned long) &frame->uc;
2977 regs->pc = (unsigned long) ka->_sa_handler;
2979 unlock_user_struct(frame, frame_addr, 1);
2980 return;
2982 give_sigsegv:
2983 unlock_user_struct(frame, frame_addr, 1);
2984 force_sig(TARGET_SIGSEGV);
2987 long do_sigreturn(CPUState *regs)
2989 struct target_sigframe *frame;
2990 abi_ulong frame_addr;
2991 sigset_t blocked;
2992 target_sigset_t target_set;
2993 target_ulong r0;
2994 int i;
2995 int err = 0;
2997 #if defined(DEBUG_SIGNAL)
2998 fprintf(stderr, "do_sigreturn\n");
2999 #endif
3000 frame_addr = regs->gregs[15];
3001 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3002 goto badframe;
3004 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3005 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3006 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3009 if (err)
3010 goto badframe;
3012 target_to_host_sigset_internal(&blocked, &target_set);
3013 sigprocmask(SIG_SETMASK, &blocked, NULL);
3015 if (restore_sigcontext(regs, &frame->sc, &r0))
3016 goto badframe;
3018 unlock_user_struct(frame, frame_addr, 0);
3019 return r0;
3021 badframe:
3022 unlock_user_struct(frame, frame_addr, 0);
3023 force_sig(TARGET_SIGSEGV);
3024 return 0;
3027 long do_rt_sigreturn(CPUState *regs)
3029 struct target_rt_sigframe *frame;
3030 abi_ulong frame_addr;
3031 sigset_t blocked;
3032 target_ulong r0;
3034 #if defined(DEBUG_SIGNAL)
3035 fprintf(stderr, "do_rt_sigreturn\n");
3036 #endif
3037 frame_addr = regs->gregs[15];
3038 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3039 goto badframe;
3041 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3042 sigprocmask(SIG_SETMASK, &blocked, NULL);
3044 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3045 goto badframe;
3047 if (do_sigaltstack(frame_addr +
3048 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3049 0, get_sp_from_cpustate(regs)) == -EFAULT)
3050 goto badframe;
3052 unlock_user_struct(frame, frame_addr, 0);
3053 return r0;
3055 badframe:
3056 unlock_user_struct(frame, frame_addr, 0);
3057 force_sig(TARGET_SIGSEGV);
3058 return 0;
3060 #elif defined(TARGET_MICROBLAZE)
3062 struct target_sigcontext {
3063 struct target_pt_regs regs; /* needs to be first */
3064 uint32_t oldmask;
3067 /* Signal frames. */
3068 struct target_signal_frame {
3069 struct target_sigcontext sc;
3070 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3071 uint32_t tramp[2];
3074 struct rt_signal_frame {
3075 struct siginfo info;
3076 struct ucontext uc;
3077 uint32_t tramp[2];
3080 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3082 __put_user(env->regs[0], &sc->regs.r0);
3083 __put_user(env->regs[1], &sc->regs.r1);
3084 __put_user(env->regs[2], &sc->regs.r2);
3085 __put_user(env->regs[3], &sc->regs.r3);
3086 __put_user(env->regs[4], &sc->regs.r4);
3087 __put_user(env->regs[5], &sc->regs.r5);
3088 __put_user(env->regs[6], &sc->regs.r6);
3089 __put_user(env->regs[7], &sc->regs.r7);
3090 __put_user(env->regs[8], &sc->regs.r8);
3091 __put_user(env->regs[9], &sc->regs.r9);
3092 __put_user(env->regs[10], &sc->regs.r10);
3093 __put_user(env->regs[11], &sc->regs.r11);
3094 __put_user(env->regs[12], &sc->regs.r12);
3095 __put_user(env->regs[13], &sc->regs.r13);
3096 __put_user(env->regs[14], &sc->regs.r14);
3097 __put_user(env->regs[15], &sc->regs.r15);
3098 __put_user(env->regs[16], &sc->regs.r16);
3099 __put_user(env->regs[17], &sc->regs.r17);
3100 __put_user(env->regs[18], &sc->regs.r18);
3101 __put_user(env->regs[19], &sc->regs.r19);
3102 __put_user(env->regs[20], &sc->regs.r20);
3103 __put_user(env->regs[21], &sc->regs.r21);
3104 __put_user(env->regs[22], &sc->regs.r22);
3105 __put_user(env->regs[23], &sc->regs.r23);
3106 __put_user(env->regs[24], &sc->regs.r24);
3107 __put_user(env->regs[25], &sc->regs.r25);
3108 __put_user(env->regs[26], &sc->regs.r26);
3109 __put_user(env->regs[27], &sc->regs.r27);
3110 __put_user(env->regs[28], &sc->regs.r28);
3111 __put_user(env->regs[29], &sc->regs.r29);
3112 __put_user(env->regs[30], &sc->regs.r30);
3113 __put_user(env->regs[31], &sc->regs.r31);
3114 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3117 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3119 __get_user(env->regs[0], &sc->regs.r0);
3120 __get_user(env->regs[1], &sc->regs.r1);
3121 __get_user(env->regs[2], &sc->regs.r2);
3122 __get_user(env->regs[3], &sc->regs.r3);
3123 __get_user(env->regs[4], &sc->regs.r4);
3124 __get_user(env->regs[5], &sc->regs.r5);
3125 __get_user(env->regs[6], &sc->regs.r6);
3126 __get_user(env->regs[7], &sc->regs.r7);
3127 __get_user(env->regs[8], &sc->regs.r8);
3128 __get_user(env->regs[9], &sc->regs.r9);
3129 __get_user(env->regs[10], &sc->regs.r10);
3130 __get_user(env->regs[11], &sc->regs.r11);
3131 __get_user(env->regs[12], &sc->regs.r12);
3132 __get_user(env->regs[13], &sc->regs.r13);
3133 __get_user(env->regs[14], &sc->regs.r14);
3134 __get_user(env->regs[15], &sc->regs.r15);
3135 __get_user(env->regs[16], &sc->regs.r16);
3136 __get_user(env->regs[17], &sc->regs.r17);
3137 __get_user(env->regs[18], &sc->regs.r18);
3138 __get_user(env->regs[19], &sc->regs.r19);
3139 __get_user(env->regs[20], &sc->regs.r20);
3140 __get_user(env->regs[21], &sc->regs.r21);
3141 __get_user(env->regs[22], &sc->regs.r22);
3142 __get_user(env->regs[23], &sc->regs.r23);
3143 __get_user(env->regs[24], &sc->regs.r24);
3144 __get_user(env->regs[25], &sc->regs.r25);
3145 __get_user(env->regs[26], &sc->regs.r26);
3146 __get_user(env->regs[27], &sc->regs.r27);
3147 __get_user(env->regs[28], &sc->regs.r28);
3148 __get_user(env->regs[29], &sc->regs.r29);
3149 __get_user(env->regs[30], &sc->regs.r30);
3150 __get_user(env->regs[31], &sc->regs.r31);
3151 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3154 static abi_ulong get_sigframe(struct target_sigaction *ka,
3155 CPUState *env, int frame_size)
3157 abi_ulong sp = env->regs[1];
3159 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3160 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3162 return ((sp - frame_size) & -8UL);
3165 static void setup_frame(int sig, struct target_sigaction *ka,
3166 target_sigset_t *set, CPUState *env)
3168 struct target_signal_frame *frame;
3169 abi_ulong frame_addr;
3170 int err = 0;
3171 int i;
3173 frame_addr = get_sigframe(ka, env, sizeof *frame);
3174 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3175 goto badframe;
3177 /* Save the mask. */
3178 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3179 if (err)
3180 goto badframe;
3182 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3183 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3184 goto badframe;
3187 setup_sigcontext(&frame->sc, env);
3189 /* Set up to return from userspace. If provided, use a stub
3190 already in userspace. */
3191 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3192 if (ka->sa_flags & TARGET_SA_RESTORER) {
3193 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3194 } else {
3195 uint32_t t;
3196 /* Note, these encodings are _big endian_! */
3197 /* addi r12, r0, __NR_sigreturn */
3198 t = 0x31800000UL | TARGET_NR_sigreturn;
3199 err |= __put_user(t, frame->tramp + 0);
3200 /* brki r14, 0x8 */
3201 t = 0xb9cc0008UL;
3202 err |= __put_user(t, frame->tramp + 1);
3204 /* Return from sighandler will jump to the tramp.
3205 Negative 8 offset because return is rtsd r15, 8 */
3206 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3209 if (err)
3210 goto badframe;
3212 /* Set up registers for signal handler */
3213 env->regs[1] = (unsigned long) frame;
3214 /* Signal handler args: */
3215 env->regs[5] = sig; /* Arg 0: signum */
3216 env->regs[6] = (unsigned long) &frame->sc; /* arg 1: sigcontext */
3218 /* Offset of 4 to handle microblaze rtid r14, 0 */
3219 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3221 unlock_user_struct(frame, frame_addr, 1);
3222 return;
3223 badframe:
3224 unlock_user_struct(frame, frame_addr, 1);
3225 force_sig(TARGET_SIGSEGV);
3228 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3229 target_siginfo_t *info,
3230 target_sigset_t *set, CPUState *env)
3232 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3235 long do_sigreturn(CPUState *env)
3237 struct target_signal_frame *frame;
3238 abi_ulong frame_addr;
3239 target_sigset_t target_set;
3240 sigset_t set;
3241 int i;
3243 frame_addr = env->regs[R_SP];
3244 /* Make sure the guest isn't playing games. */
3245 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3246 goto badframe;
3248 /* Restore blocked signals */
3249 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3250 goto badframe;
3251 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3252 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3253 goto badframe;
3255 target_to_host_sigset_internal(&set, &target_set);
3256 sigprocmask(SIG_SETMASK, &set, NULL);
3258 restore_sigcontext(&frame->sc, env);
3259 /* We got here through a sigreturn syscall, our path back is via an
3260 rtb insn so setup r14 for that. */
3261 env->regs[14] = env->sregs[SR_PC];
3263 unlock_user_struct(frame, frame_addr, 0);
3264 return env->regs[10];
3265 badframe:
3266 unlock_user_struct(frame, frame_addr, 0);
3267 force_sig(TARGET_SIGSEGV);
3270 long do_rt_sigreturn(CPUState *env)
3272 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3273 return -TARGET_ENOSYS;
3276 #elif defined(TARGET_CRIS)
3278 struct target_sigcontext {
3279 struct target_pt_regs regs; /* needs to be first */
3280 uint32_t oldmask;
3281 uint32_t usp; /* usp before stacking this gunk on it */
3284 /* Signal frames. */
3285 struct target_signal_frame {
3286 struct target_sigcontext sc;
3287 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3288 uint8_t retcode[8]; /* Trampoline code. */
3291 struct rt_signal_frame {
3292 struct siginfo *pinfo;
3293 void *puc;
3294 struct siginfo info;
3295 struct ucontext uc;
3296 uint8_t retcode[8]; /* Trampoline code. */
3299 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3301 __put_user(env->regs[0], &sc->regs.r0);
3302 __put_user(env->regs[1], &sc->regs.r1);
3303 __put_user(env->regs[2], &sc->regs.r2);
3304 __put_user(env->regs[3], &sc->regs.r3);
3305 __put_user(env->regs[4], &sc->regs.r4);
3306 __put_user(env->regs[5], &sc->regs.r5);
3307 __put_user(env->regs[6], &sc->regs.r6);
3308 __put_user(env->regs[7], &sc->regs.r7);
3309 __put_user(env->regs[8], &sc->regs.r8);
3310 __put_user(env->regs[9], &sc->regs.r9);
3311 __put_user(env->regs[10], &sc->regs.r10);
3312 __put_user(env->regs[11], &sc->regs.r11);
3313 __put_user(env->regs[12], &sc->regs.r12);
3314 __put_user(env->regs[13], &sc->regs.r13);
3315 __put_user(env->regs[14], &sc->usp);
3316 __put_user(env->regs[15], &sc->regs.acr);
3317 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3318 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3319 __put_user(env->pc, &sc->regs.erp);
3322 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3324 __get_user(env->regs[0], &sc->regs.r0);
3325 __get_user(env->regs[1], &sc->regs.r1);
3326 __get_user(env->regs[2], &sc->regs.r2);
3327 __get_user(env->regs[3], &sc->regs.r3);
3328 __get_user(env->regs[4], &sc->regs.r4);
3329 __get_user(env->regs[5], &sc->regs.r5);
3330 __get_user(env->regs[6], &sc->regs.r6);
3331 __get_user(env->regs[7], &sc->regs.r7);
3332 __get_user(env->regs[8], &sc->regs.r8);
3333 __get_user(env->regs[9], &sc->regs.r9);
3334 __get_user(env->regs[10], &sc->regs.r10);
3335 __get_user(env->regs[11], &sc->regs.r11);
3336 __get_user(env->regs[12], &sc->regs.r12);
3337 __get_user(env->regs[13], &sc->regs.r13);
3338 __get_user(env->regs[14], &sc->usp);
3339 __get_user(env->regs[15], &sc->regs.acr);
3340 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3341 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3342 __get_user(env->pc, &sc->regs.erp);
3345 static abi_ulong get_sigframe(CPUState *env, int framesize)
3347 abi_ulong sp;
3348 /* Align the stack downwards to 4. */
3349 sp = (env->regs[R_SP] & ~3);
3350 return sp - framesize;
3353 static void setup_frame(int sig, struct target_sigaction *ka,
3354 target_sigset_t *set, CPUState *env)
3356 struct target_signal_frame *frame;
3357 abi_ulong frame_addr;
3358 int err = 0;
3359 int i;
3361 frame_addr = get_sigframe(env, sizeof *frame);
3362 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3363 goto badframe;
3366 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3367 * use this trampoline anymore but it sets it up for GDB.
3368 * In QEMU, using the trampoline simplifies things a bit so we use it.
3370 * This is movu.w __NR_sigreturn, r9; break 13;
3372 err |= __put_user(0x9c5f, frame->retcode+0);
3373 err |= __put_user(TARGET_NR_sigreturn,
3374 frame->retcode+2);
3375 err |= __put_user(0xe93d, frame->retcode+4);
3377 /* Save the mask. */
3378 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3379 if (err)
3380 goto badframe;
3382 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3383 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3384 goto badframe;
3387 setup_sigcontext(&frame->sc, env);
3389 /* Move the stack and setup the arguments for the handler. */
3390 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
3391 env->regs[10] = sig;
3392 env->pc = (unsigned long) ka->_sa_handler;
3393 /* Link SRP so the guest returns through the trampoline. */
3394 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
3396 unlock_user_struct(frame, frame_addr, 1);
3397 return;
3398 badframe:
3399 unlock_user_struct(frame, frame_addr, 1);
3400 force_sig(TARGET_SIGSEGV);
3403 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3404 target_siginfo_t *info,
3405 target_sigset_t *set, CPUState *env)
3407 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3410 long do_sigreturn(CPUState *env)
3412 struct target_signal_frame *frame;
3413 abi_ulong frame_addr;
3414 target_sigset_t target_set;
3415 sigset_t set;
3416 int i;
3418 frame_addr = env->regs[R_SP];
3419 /* Make sure the guest isn't playing games. */
3420 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3421 goto badframe;
3423 /* Restore blocked signals */
3424 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3425 goto badframe;
3426 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3427 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3428 goto badframe;
3430 target_to_host_sigset_internal(&set, &target_set);
3431 sigprocmask(SIG_SETMASK, &set, NULL);
3433 restore_sigcontext(&frame->sc, env);
3434 unlock_user_struct(frame, frame_addr, 0);
3435 return env->regs[10];
3436 badframe:
3437 unlock_user_struct(frame, frame_addr, 0);
3438 force_sig(TARGET_SIGSEGV);
3441 long do_rt_sigreturn(CPUState *env)
3443 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3444 return -TARGET_ENOSYS;
3447 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3449 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
3450 the signal handling is different enough that we haven't implemented
3451 support for PPC64 yet. Hence the restriction above.
3453 There are various #if'd blocks for code for TARGET_PPC64. These
3454 blocks should go away so that we can successfully run 32-bit and
3455 64-bit binaries on a QEMU configured for PPC64. */
3457 /* Size of dummy stack frame allocated when calling signal handler.
3458 See arch/powerpc/include/asm/ptrace.h. */
3459 #if defined(TARGET_PPC64)
3460 #define SIGNAL_FRAMESIZE 128
3461 #else
3462 #define SIGNAL_FRAMESIZE 64
3463 #endif
3465 /* See arch/powerpc/include/asm/sigcontext.h. */
3466 struct target_sigcontext {
3467 target_ulong _unused[4];
3468 int32_t signal;
3469 #if defined(TARGET_PPC64)
3470 int32_t pad0;
3471 #endif
3472 target_ulong handler;
3473 target_ulong oldmask;
3474 target_ulong regs; /* struct pt_regs __user * */
3475 /* TODO: PPC64 includes extra bits here. */
3478 /* Indices for target_mcontext.mc_gregs, below.
3479 See arch/powerpc/include/asm/ptrace.h for details. */
3480 enum {
3481 TARGET_PT_R0 = 0,
3482 TARGET_PT_R1 = 1,
3483 TARGET_PT_R2 = 2,
3484 TARGET_PT_R3 = 3,
3485 TARGET_PT_R4 = 4,
3486 TARGET_PT_R5 = 5,
3487 TARGET_PT_R6 = 6,
3488 TARGET_PT_R7 = 7,
3489 TARGET_PT_R8 = 8,
3490 TARGET_PT_R9 = 9,
3491 TARGET_PT_R10 = 10,
3492 TARGET_PT_R11 = 11,
3493 TARGET_PT_R12 = 12,
3494 TARGET_PT_R13 = 13,
3495 TARGET_PT_R14 = 14,
3496 TARGET_PT_R15 = 15,
3497 TARGET_PT_R16 = 16,
3498 TARGET_PT_R17 = 17,
3499 TARGET_PT_R18 = 18,
3500 TARGET_PT_R19 = 19,
3501 TARGET_PT_R20 = 20,
3502 TARGET_PT_R21 = 21,
3503 TARGET_PT_R22 = 22,
3504 TARGET_PT_R23 = 23,
3505 TARGET_PT_R24 = 24,
3506 TARGET_PT_R25 = 25,
3507 TARGET_PT_R26 = 26,
3508 TARGET_PT_R27 = 27,
3509 TARGET_PT_R28 = 28,
3510 TARGET_PT_R29 = 29,
3511 TARGET_PT_R30 = 30,
3512 TARGET_PT_R31 = 31,
3513 TARGET_PT_NIP = 32,
3514 TARGET_PT_MSR = 33,
3515 TARGET_PT_ORIG_R3 = 34,
3516 TARGET_PT_CTR = 35,
3517 TARGET_PT_LNK = 36,
3518 TARGET_PT_XER = 37,
3519 TARGET_PT_CCR = 38,
3520 /* Yes, there are two registers with #39. One is 64-bit only. */
3521 TARGET_PT_MQ = 39,
3522 TARGET_PT_SOFTE = 39,
3523 TARGET_PT_TRAP = 40,
3524 TARGET_PT_DAR = 41,
3525 TARGET_PT_DSISR = 42,
3526 TARGET_PT_RESULT = 43,
3527 TARGET_PT_REGS_COUNT = 44
3530 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
3531 on 64-bit PPC, sigcontext and mcontext are one and the same. */
3532 struct target_mcontext {
3533 target_ulong mc_gregs[48];
3534 /* Includes fpscr. */
3535 uint64_t mc_fregs[33];
3536 target_ulong mc_pad[2];
3537 /* We need to handle Altivec and SPE at the same time, which no
3538 kernel needs to do. Fortunately, the kernel defines this bit to
3539 be Altivec-register-large all the time, rather than trying to
3540 twiddle it based on the specific platform. */
3541 union {
3542 /* SPE vector registers. One extra for SPEFSCR. */
3543 uint32_t spe[33];
3544 /* Altivec vector registers. The packing of VSCR and VRSAVE
3545 varies depending on whether we're PPC64 or not: PPC64 splits
3546 them apart; PPC32 stuffs them together. */
3547 #if defined(TARGET_PPC64)
3548 #define QEMU_NVRREG 34
3549 #else
3550 #define QEMU_NVRREG 33
3551 #endif
3552 ppc_avr_t altivec[QEMU_NVRREG];
3553 #undef QEMU_NVRREG
3554 } mc_vregs __attribute__((__aligned__(16)));
3557 struct target_ucontext {
3558 target_ulong tuc_flags;
3559 target_ulong tuc_link; /* struct ucontext __user * */
3560 struct target_sigaltstack tuc_stack;
3561 #if !defined(TARGET_PPC64)
3562 int32_t tuc_pad[7];
3563 target_ulong tuc_regs; /* struct mcontext __user *
3564 points to uc_mcontext field */
3565 #endif
3566 target_sigset_t tuc_sigmask;
3567 #if defined(TARGET_PPC64)
3568 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
3569 struct target_sigcontext tuc_mcontext;
3570 #else
3571 int32_t tuc_maskext[30];
3572 int32_t tuc_pad2[3];
3573 struct target_mcontext tuc_mcontext;
3574 #endif
3577 /* See arch/powerpc/kernel/signal_32.c. */
3578 struct target_sigframe {
3579 struct target_sigcontext sctx;
3580 struct target_mcontext mctx;
3581 int32_t abigap[56];
3584 struct target_rt_sigframe {
3585 struct target_siginfo info;
3586 struct target_ucontext uc;
3587 int32_t abigap[56];
3590 /* We use the mc_pad field for the signal return trampoline. */
3591 #define tramp mc_pad
3593 /* See arch/powerpc/kernel/signal.c. */
3594 static target_ulong get_sigframe(struct target_sigaction *ka,
3595 CPUState *env,
3596 int frame_size)
3598 target_ulong oldsp, newsp;
3600 oldsp = env->gpr[1];
3602 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
3603 (sas_ss_flags(oldsp))) {
3604 oldsp = (target_sigaltstack_used.ss_sp
3605 + target_sigaltstack_used.ss_size);
3608 newsp = (oldsp - frame_size) & ~0xFUL;
3610 return newsp;
3613 static int save_user_regs(CPUState *env, struct target_mcontext *frame,
3614 int sigret)
3616 target_ulong msr = env->msr;
3617 int i;
3618 target_ulong ccr = 0;
3620 /* In general, the kernel attempts to be intelligent about what it
3621 needs to save for Altivec/FP/SPE registers. We don't care that
3622 much, so we just go ahead and save everything. */
3624 /* Save general registers. */
3625 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3626 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
3627 return 1;
3630 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3631 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3632 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3633 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3634 return 1;
3636 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3637 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
3639 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3640 return 1;
3642 /* Save Altivec registers if necessary. */
3643 if (env->insns_flags & PPC_ALTIVEC) {
3644 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3645 ppc_avr_t *avr = &env->avr[i];
3646 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3648 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
3649 __put_user(avr->u64[1], &vreg->u64[1])) {
3650 return 1;
3653 /* Set MSR_VR in the saved MSR value to indicate that
3654 frame->mc_vregs contains valid data. */
3655 msr |= MSR_VR;
3656 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
3657 &frame->mc_vregs.altivec[32].u32[3]))
3658 return 1;
3661 /* Save floating point registers. */
3662 if (env->insns_flags & PPC_FLOAT) {
3663 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3664 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
3665 return 1;
3668 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
3669 return 1;
3672 /* Save SPE registers. The kernel only saves the high half. */
3673 if (env->insns_flags & PPC_SPE) {
3674 #if defined(TARGET_PPC64)
3675 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3676 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
3677 return 1;
3680 #else
3681 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3682 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3683 return 1;
3686 #endif
3687 /* Set MSR_SPE in the saved MSR value to indicate that
3688 frame->mc_vregs contains valid data. */
3689 msr |= MSR_SPE;
3690 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3691 return 1;
3694 /* Store MSR. */
3695 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3696 return 1;
3698 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
3699 if (sigret) {
3700 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
3701 __put_user(0x44000002UL, &frame->tramp[1])) {
3702 return 1;
3706 return 0;
3709 static int restore_user_regs(CPUState *env,
3710 struct target_mcontext *frame, int sig)
3712 target_ulong save_r2 = 0;
3713 target_ulong msr;
3714 target_ulong ccr;
3716 int i;
3718 if (!sig) {
3719 save_r2 = env->gpr[2];
3722 /* Restore general registers. */
3723 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3724 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
3725 return 1;
3728 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3729 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3730 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3731 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3732 return 1;
3733 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3734 return 1;
3736 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3737 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
3740 if (!sig) {
3741 env->gpr[2] = save_r2;
3743 /* Restore MSR. */
3744 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3745 return 1;
3747 /* If doing signal return, restore the previous little-endian mode. */
3748 if (sig)
3749 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
3751 /* Restore Altivec registers if necessary. */
3752 if (env->insns_flags & PPC_ALTIVEC) {
3753 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3754 ppc_avr_t *avr = &env->avr[i];
3755 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3757 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
3758 __get_user(avr->u64[1], &vreg->u64[1])) {
3759 return 1;
3762 /* Set MSR_VEC in the saved MSR value to indicate that
3763 frame->mc_vregs contains valid data. */
3764 if (__get_user(env->spr[SPR_VRSAVE],
3765 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
3766 return 1;
3769 /* Restore floating point registers. */
3770 if (env->insns_flags & PPC_FLOAT) {
3771 uint64_t fpscr;
3772 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3773 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
3774 return 1;
3777 if (__get_user(fpscr, &frame->mc_fregs[32]))
3778 return 1;
3779 env->fpscr = (uint32_t) fpscr;
3782 /* Save SPE registers. The kernel only saves the high half. */
3783 if (env->insns_flags & PPC_SPE) {
3784 #if defined(TARGET_PPC64)
3785 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3786 uint32_t hi;
3788 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
3789 return 1;
3791 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
3793 #else
3794 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3795 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3796 return 1;
3799 #endif
3800 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3801 return 1;
3804 return 0;
3807 static void setup_frame(int sig, struct target_sigaction *ka,
3808 target_sigset_t *set, CPUState *env)
3810 struct target_sigframe *frame;
3811 struct target_sigcontext *sc;
3812 target_ulong frame_addr, newsp;
3813 int err = 0;
3814 int signal;
3816 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3817 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3818 goto sigsegv;
3819 sc = &frame->sctx;
3821 signal = current_exec_domain_sig(sig);
3823 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
3824 err |= __put_user(set->sig[0], &sc->oldmask);
3825 #if defined(TARGET_PPC64)
3826 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
3827 #else
3828 err |= __put_user(set->sig[1], &sc->_unused[3]);
3829 #endif
3830 err |= __put_user(h2g(&frame->mctx), &sc->regs);
3831 err |= __put_user(sig, &sc->signal);
3833 /* Save user regs. */
3834 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
3836 /* The kernel checks for the presence of a VDSO here. We don't
3837 emulate a vdso, so use a sigreturn system call. */
3838 env->lr = (target_ulong) h2g(frame->mctx.tramp);
3840 /* Turn off all fp exceptions. */
3841 env->fpscr = 0;
3843 /* Create a stack frame for the caller of the handler. */
3844 newsp = frame_addr - SIGNAL_FRAMESIZE;
3845 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3847 if (err)
3848 goto sigsegv;
3850 /* Set up registers for signal handler. */
3851 env->gpr[1] = newsp;
3852 env->gpr[3] = signal;
3853 env->gpr[4] = (target_ulong) h2g(sc);
3854 env->nip = (target_ulong) ka->_sa_handler;
3855 /* Signal handlers are entered in big-endian mode. */
3856 env->msr &= ~MSR_LE;
3858 unlock_user_struct(frame, frame_addr, 1);
3859 return;
3861 sigsegv:
3862 unlock_user_struct(frame, frame_addr, 1);
3863 if (logfile)
3864 fprintf (logfile, "segfaulting from setup_frame\n");
3865 force_sig(TARGET_SIGSEGV);
3868 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3869 target_siginfo_t *info,
3870 target_sigset_t *set, CPUState *env)
3872 struct target_rt_sigframe *rt_sf;
3873 struct target_mcontext *frame;
3874 target_ulong rt_sf_addr, newsp = 0;
3875 int i, err = 0;
3876 int signal;
3878 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
3879 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
3880 goto sigsegv;
3882 signal = current_exec_domain_sig(sig);
3884 err |= copy_siginfo_to_user(&rt_sf->info, info);
3886 err |= __put_user(0, &rt_sf->uc.tuc_flags);
3887 err |= __put_user(0, &rt_sf->uc.tuc_link);
3888 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
3889 &rt_sf->uc.tuc_stack.ss_sp);
3890 err |= __put_user(sas_ss_flags(env->gpr[1]),
3891 &rt_sf->uc.tuc_stack.ss_flags);
3892 err |= __put_user(target_sigaltstack_used.ss_size,
3893 &rt_sf->uc.tuc_stack.ss_size);
3894 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
3895 &rt_sf->uc.tuc_regs);
3896 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3897 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
3900 frame = &rt_sf->uc.tuc_mcontext;
3901 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
3903 /* The kernel checks for the presence of a VDSO here. We don't
3904 emulate a vdso, so use a sigreturn system call. */
3905 env->lr = (target_ulong) h2g(frame->tramp);
3907 /* Turn off all fp exceptions. */
3908 env->fpscr = 0;
3910 /* Create a stack frame for the caller of the handler. */
3911 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
3912 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3914 if (err)
3915 goto sigsegv;
3917 /* Set up registers for signal handler. */
3918 env->gpr[1] = newsp;
3919 env->gpr[3] = (target_ulong) signal;
3920 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
3921 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
3922 env->gpr[6] = (target_ulong) h2g(rt_sf);
3923 env->nip = (target_ulong) ka->_sa_handler;
3924 /* Signal handlers are entered in big-endian mode. */
3925 env->msr &= ~MSR_LE;
3927 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3928 return;
3930 sigsegv:
3931 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3932 if (logfile)
3933 fprintf (logfile, "segfaulting from setup_rt_frame\n");
3934 force_sig(TARGET_SIGSEGV);
3938 long do_sigreturn(CPUState *env)
3940 struct target_sigcontext *sc = NULL;
3941 struct target_mcontext *sr = NULL;
3942 target_ulong sr_addr, sc_addr;
3943 sigset_t blocked;
3944 target_sigset_t set;
3946 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
3947 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
3948 goto sigsegv;
3950 #if defined(TARGET_PPC64)
3951 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
3952 #else
3953 if(__get_user(set.sig[0], &sc->oldmask) ||
3954 __get_user(set.sig[1], &sc->_unused[3]))
3955 goto sigsegv;
3956 #endif
3957 target_to_host_sigset_internal(&blocked, &set);
3958 sigprocmask(SIG_SETMASK, &blocked, NULL);
3960 if (__get_user(sr_addr, &sc->regs))
3961 goto sigsegv;
3962 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
3963 goto sigsegv;
3964 if (restore_user_regs(env, sr, 1))
3965 goto sigsegv;
3967 unlock_user_struct(sr, sr_addr, 1);
3968 unlock_user_struct(sc, sc_addr, 1);
3969 return -TARGET_QEMU_ESIGRETURN;
3971 sigsegv:
3972 unlock_user_struct(sr, sr_addr, 1);
3973 unlock_user_struct(sc, sc_addr, 1);
3974 if (logfile)
3975 fprintf (logfile, "segfaulting from do_sigreturn\n");
3976 force_sig(TARGET_SIGSEGV);
3977 return 0;
3980 /* See arch/powerpc/kernel/signal_32.c. */
3981 static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
3983 struct target_mcontext *mcp;
3984 target_ulong mcp_addr;
3985 sigset_t blocked;
3986 target_sigset_t set;
3988 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
3989 sizeof (set)))
3990 return 1;
3992 #if defined(TARGET_PPC64)
3993 fprintf (stderr, "do_setcontext: not implemented\n");
3994 return 0;
3995 #else
3996 if (__get_user(mcp_addr, &ucp->tuc_regs))
3997 return 1;
3999 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4000 return 1;
4002 target_to_host_sigset_internal(&blocked, &set);
4003 sigprocmask(SIG_SETMASK, &blocked, NULL);
4004 if (restore_user_regs(env, mcp, sig))
4005 goto sigsegv;
4007 unlock_user_struct(mcp, mcp_addr, 1);
4008 return 0;
4010 sigsegv:
4011 unlock_user_struct(mcp, mcp_addr, 1);
4012 return 1;
4013 #endif
4016 long do_rt_sigreturn(CPUState *env)
4018 struct target_rt_sigframe *rt_sf = NULL;
4019 target_ulong rt_sf_addr;
4021 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4022 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4023 goto sigsegv;
4025 if (do_setcontext(&rt_sf->uc, env, 1))
4026 goto sigsegv;
4028 do_sigaltstack(rt_sf_addr
4029 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4030 0, env->gpr[1]);
4032 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4033 return -TARGET_QEMU_ESIGRETURN;
4035 sigsegv:
4036 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4037 if (logfile)
4038 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
4039 force_sig(TARGET_SIGSEGV);
4040 return 0;
4043 #elif defined(TARGET_M68K)
4045 struct target_sigcontext {
4046 abi_ulong sc_mask;
4047 abi_ulong sc_usp;
4048 abi_ulong sc_d0;
4049 abi_ulong sc_d1;
4050 abi_ulong sc_a0;
4051 abi_ulong sc_a1;
4052 unsigned short sc_sr;
4053 abi_ulong sc_pc;
4056 struct target_sigframe
4058 abi_ulong pretcode;
4059 int sig;
4060 int code;
4061 abi_ulong psc;
4062 char retcode[8];
4063 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4064 struct target_sigcontext sc;
4067 typedef int target_greg_t;
4068 #define TARGET_NGREG 18
4069 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4071 typedef struct target_fpregset {
4072 int f_fpcntl[3];
4073 int f_fpregs[8*3];
4074 } target_fpregset_t;
4076 struct target_mcontext {
4077 int version;
4078 target_gregset_t gregs;
4079 target_fpregset_t fpregs;
4082 #define TARGET_MCONTEXT_VERSION 2
4084 struct target_ucontext {
4085 abi_ulong tuc_flags;
4086 abi_ulong tuc_link;
4087 target_stack_t tuc_stack;
4088 struct target_mcontext tuc_mcontext;
4089 abi_long tuc_filler[80];
4090 target_sigset_t tuc_sigmask;
4093 struct target_rt_sigframe
4095 abi_ulong pretcode;
4096 int sig;
4097 abi_ulong pinfo;
4098 abi_ulong puc;
4099 char retcode[8];
4100 struct target_siginfo info;
4101 struct target_ucontext uc;
4104 static int
4105 setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4107 int err = 0;
4109 err |= __put_user(mask, &sc->sc_mask);
4110 err |= __put_user(env->aregs[7], &sc->sc_usp);
4111 err |= __put_user(env->dregs[0], &sc->sc_d0);
4112 err |= __put_user(env->dregs[1], &sc->sc_d1);
4113 err |= __put_user(env->aregs[0], &sc->sc_a0);
4114 err |= __put_user(env->aregs[1], &sc->sc_a1);
4115 err |= __put_user(env->sr, &sc->sc_sr);
4116 err |= __put_user(env->pc, &sc->sc_pc);
4118 return err;
4121 static int
4122 restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4124 int err = 0;
4125 int temp;
4127 err |= __get_user(env->aregs[7], &sc->sc_usp);
4128 err |= __get_user(env->dregs[1], &sc->sc_d1);
4129 err |= __get_user(env->aregs[0], &sc->sc_a0);
4130 err |= __get_user(env->aregs[1], &sc->sc_a1);
4131 err |= __get_user(env->pc, &sc->sc_pc);
4132 err |= __get_user(temp, &sc->sc_sr);
4133 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4135 *pd0 = tswapl(sc->sc_d0);
4137 return err;
4141 * Determine which stack to use..
4143 static inline abi_ulong
4144 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4146 unsigned long sp;
4148 sp = regs->aregs[7];
4150 /* This is the X/Open sanctioned signal stack switching. */
4151 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4152 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4155 return ((sp - frame_size) & -8UL);
4158 static void setup_frame(int sig, struct target_sigaction *ka,
4159 target_sigset_t *set, CPUState *env)
4161 struct target_sigframe *frame;
4162 abi_ulong frame_addr;
4163 abi_ulong retcode_addr;
4164 abi_ulong sc_addr;
4165 int err = 0;
4166 int i;
4168 frame_addr = get_sigframe(ka, env, sizeof *frame);
4169 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4170 goto give_sigsegv;
4172 err |= __put_user(sig, &frame->sig);
4174 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4175 err |= __put_user(sc_addr, &frame->psc);
4177 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4178 if (err)
4179 goto give_sigsegv;
4181 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4182 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4183 goto give_sigsegv;
4186 /* Set up to return from userspace. */
4188 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4189 err |= __put_user(retcode_addr, &frame->pretcode);
4191 /* moveq #,d0; trap #0 */
4193 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4194 (long *)(frame->retcode));
4196 if (err)
4197 goto give_sigsegv;
4199 /* Set up to return from userspace */
4201 env->aregs[7] = frame_addr;
4202 env->pc = ka->_sa_handler;
4204 unlock_user_struct(frame, frame_addr, 1);
4205 return;
4207 give_sigsegv:
4208 unlock_user_struct(frame, frame_addr, 1);
4209 force_sig(TARGET_SIGSEGV);
4212 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4213 CPUState *env)
4215 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4216 int err;
4218 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4219 err |= __put_user(env->dregs[0], &gregs[0]);
4220 err |= __put_user(env->dregs[1], &gregs[1]);
4221 err |= __put_user(env->dregs[2], &gregs[2]);
4222 err |= __put_user(env->dregs[3], &gregs[3]);
4223 err |= __put_user(env->dregs[4], &gregs[4]);
4224 err |= __put_user(env->dregs[5], &gregs[5]);
4225 err |= __put_user(env->dregs[6], &gregs[6]);
4226 err |= __put_user(env->dregs[7], &gregs[7]);
4227 err |= __put_user(env->aregs[0], &gregs[8]);
4228 err |= __put_user(env->aregs[1], &gregs[9]);
4229 err |= __put_user(env->aregs[2], &gregs[10]);
4230 err |= __put_user(env->aregs[3], &gregs[11]);
4231 err |= __put_user(env->aregs[4], &gregs[12]);
4232 err |= __put_user(env->aregs[5], &gregs[13]);
4233 err |= __put_user(env->aregs[6], &gregs[14]);
4234 err |= __put_user(env->aregs[7], &gregs[15]);
4235 err |= __put_user(env->pc, &gregs[16]);
4236 err |= __put_user(env->sr, &gregs[17]);
4238 return err;
4241 static inline int target_rt_restore_ucontext(CPUState *env,
4242 struct target_ucontext *uc,
4243 int *pd0)
4245 int temp;
4246 int err;
4247 target_greg_t *gregs = uc->tuc_mcontext.gregs;
4249 err = __get_user(temp, &uc->tuc_mcontext.version);
4250 if (temp != TARGET_MCONTEXT_VERSION)
4251 goto badframe;
4253 /* restore passed registers */
4254 err |= __get_user(env->dregs[0], &gregs[0]);
4255 err |= __get_user(env->dregs[1], &gregs[1]);
4256 err |= __get_user(env->dregs[2], &gregs[2]);
4257 err |= __get_user(env->dregs[3], &gregs[3]);
4258 err |= __get_user(env->dregs[4], &gregs[4]);
4259 err |= __get_user(env->dregs[5], &gregs[5]);
4260 err |= __get_user(env->dregs[6], &gregs[6]);
4261 err |= __get_user(env->dregs[7], &gregs[7]);
4262 err |= __get_user(env->aregs[0], &gregs[8]);
4263 err |= __get_user(env->aregs[1], &gregs[9]);
4264 err |= __get_user(env->aregs[2], &gregs[10]);
4265 err |= __get_user(env->aregs[3], &gregs[11]);
4266 err |= __get_user(env->aregs[4], &gregs[12]);
4267 err |= __get_user(env->aregs[5], &gregs[13]);
4268 err |= __get_user(env->aregs[6], &gregs[14]);
4269 err |= __get_user(env->aregs[7], &gregs[15]);
4270 err |= __get_user(env->pc, &gregs[16]);
4271 err |= __get_user(temp, &gregs[17]);
4272 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4274 *pd0 = env->dregs[0];
4275 return err;
4277 badframe:
4278 return 1;
4281 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4282 target_siginfo_t *info,
4283 target_sigset_t *set, CPUState *env)
4285 struct target_rt_sigframe *frame;
4286 abi_ulong frame_addr;
4287 abi_ulong retcode_addr;
4288 abi_ulong info_addr;
4289 abi_ulong uc_addr;
4290 int err = 0;
4291 int i;
4293 frame_addr = get_sigframe(ka, env, sizeof *frame);
4294 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4295 goto give_sigsegv;
4297 err |= __put_user(sig, &frame->sig);
4299 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4300 err |= __put_user(info_addr, &frame->pinfo);
4302 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4303 err |= __put_user(uc_addr, &frame->puc);
4305 err |= copy_siginfo_to_user(&frame->info, info);
4307 /* Create the ucontext */
4309 err |= __put_user(0, &frame->uc.tuc_flags);
4310 err |= __put_user(0, &frame->uc.tuc_link);
4311 err |= __put_user(target_sigaltstack_used.ss_sp,
4312 &frame->uc.tuc_stack.ss_sp);
4313 err |= __put_user(sas_ss_flags(env->aregs[7]),
4314 &frame->uc.tuc_stack.ss_flags);
4315 err |= __put_user(target_sigaltstack_used.ss_size,
4316 &frame->uc.tuc_stack.ss_size);
4317 err |= target_rt_setup_ucontext(&frame->uc, env);
4319 if (err)
4320 goto give_sigsegv;
4322 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4323 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
4324 goto give_sigsegv;
4327 /* Set up to return from userspace. */
4329 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4330 err |= __put_user(retcode_addr, &frame->pretcode);
4332 /* moveq #,d0; notb d0; trap #0 */
4334 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4335 (long *)(frame->retcode + 0));
4336 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4338 if (err)
4339 goto give_sigsegv;
4341 /* Set up to return from userspace */
4343 env->aregs[7] = frame_addr;
4344 env->pc = ka->_sa_handler;
4346 unlock_user_struct(frame, frame_addr, 1);
4347 return;
4349 give_sigsegv:
4350 unlock_user_struct(frame, frame_addr, 1);
4351 force_sig(TARGET_SIGSEGV);
4354 long do_sigreturn(CPUState *env)
4356 struct target_sigframe *frame;
4357 abi_ulong frame_addr = env->aregs[7] - 4;
4358 target_sigset_t target_set;
4359 sigset_t set;
4360 int d0, i;
4362 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4363 goto badframe;
4365 /* set blocked signals */
4367 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4368 goto badframe;
4370 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4371 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4372 goto badframe;
4375 target_to_host_sigset_internal(&set, &target_set);
4376 sigprocmask(SIG_SETMASK, &set, NULL);
4378 /* restore registers */
4380 if (restore_sigcontext(env, &frame->sc, &d0))
4381 goto badframe;
4383 unlock_user_struct(frame, frame_addr, 0);
4384 return d0;
4386 badframe:
4387 unlock_user_struct(frame, frame_addr, 0);
4388 force_sig(TARGET_SIGSEGV);
4389 return 0;
4392 long do_rt_sigreturn(CPUState *env)
4394 struct target_rt_sigframe *frame;
4395 abi_ulong frame_addr = env->aregs[7] - 4;
4396 target_sigset_t target_set;
4397 sigset_t set;
4398 int d0;
4400 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4401 goto badframe;
4403 target_to_host_sigset_internal(&set, &target_set);
4404 sigprocmask(SIG_SETMASK, &set, NULL);
4406 /* restore registers */
4408 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4409 goto badframe;
4411 if (do_sigaltstack(frame_addr +
4412 offsetof(struct target_rt_sigframe, uc.tuc_stack),
4413 0, get_sp_from_cpustate(env)) == -EFAULT)
4414 goto badframe;
4416 unlock_user_struct(frame, frame_addr, 0);
4417 return d0;
4419 badframe:
4420 unlock_user_struct(frame, frame_addr, 0);
4421 force_sig(TARGET_SIGSEGV);
4422 return 0;
4425 #elif defined(TARGET_ALPHA)
4427 struct target_sigcontext {
4428 abi_long sc_onstack;
4429 abi_long sc_mask;
4430 abi_long sc_pc;
4431 abi_long sc_ps;
4432 abi_long sc_regs[32];
4433 abi_long sc_ownedfp;
4434 abi_long sc_fpregs[32];
4435 abi_ulong sc_fpcr;
4436 abi_ulong sc_fp_control;
4437 abi_ulong sc_reserved1;
4438 abi_ulong sc_reserved2;
4439 abi_ulong sc_ssize;
4440 abi_ulong sc_sbase;
4441 abi_ulong sc_traparg_a0;
4442 abi_ulong sc_traparg_a1;
4443 abi_ulong sc_traparg_a2;
4444 abi_ulong sc_fp_trap_pc;
4445 abi_ulong sc_fp_trigger_sum;
4446 abi_ulong sc_fp_trigger_inst;
4449 struct target_ucontext {
4450 abi_ulong tuc_flags;
4451 abi_ulong tuc_link;
4452 abi_ulong tuc_osf_sigmask;
4453 target_stack_t tuc_stack;
4454 struct target_sigcontext tuc_mcontext;
4455 target_sigset_t tuc_sigmask;
4458 struct target_sigframe {
4459 struct target_sigcontext sc;
4460 unsigned int retcode[3];
4463 struct target_rt_sigframe {
4464 target_siginfo_t info;
4465 struct target_ucontext uc;
4466 unsigned int retcode[3];
4469 #define INSN_MOV_R30_R16 0x47fe0410
4470 #define INSN_LDI_R0 0x201f0000
4471 #define INSN_CALLSYS 0x00000083
4473 static int setup_sigcontext(struct target_sigcontext *sc, CPUState *env,
4474 abi_ulong frame_addr, target_sigset_t *set)
4476 int i, err = 0;
4478 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
4479 err |= __put_user(set->sig[0], &sc->sc_mask);
4480 err |= __put_user(env->pc, &sc->sc_pc);
4481 err |= __put_user(8, &sc->sc_ps);
4483 for (i = 0; i < 31; ++i) {
4484 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
4486 err |= __put_user(0, &sc->sc_regs[31]);
4488 for (i = 0; i < 31; ++i) {
4489 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
4491 err |= __put_user(0, &sc->sc_fpregs[31]);
4492 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
4494 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
4495 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
4496 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
4498 return err;
4501 static int restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
4503 uint64_t fpcr;
4504 int i, err = 0;
4506 err |= __get_user(env->pc, &sc->sc_pc);
4508 for (i = 0; i < 31; ++i) {
4509 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
4511 for (i = 0; i < 31; ++i) {
4512 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
4515 err |= __get_user(fpcr, &sc->sc_fpcr);
4516 cpu_alpha_store_fpcr(env, fpcr);
4518 return err;
4521 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
4522 CPUState *env, unsigned long framesize)
4524 abi_ulong sp = env->ir[IR_SP];
4526 /* This is the X/Open sanctioned signal stack switching. */
4527 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
4528 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4530 return (sp - framesize) & -32;
4533 static void setup_frame(int sig, struct target_sigaction *ka,
4534 target_sigset_t *set, CPUState *env)
4536 abi_ulong frame_addr, r26;
4537 struct target_sigframe *frame;
4538 int err = 0;
4540 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4541 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4542 goto give_sigsegv;
4545 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
4547 if (ka->sa_restorer) {
4548 r26 = ka->sa_restorer;
4549 } else {
4550 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4551 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
4552 &frame->retcode[1]);
4553 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4554 /* imb() */
4555 r26 = frame_addr;
4558 unlock_user_struct(frame, frame_addr, 1);
4560 if (err) {
4561 give_sigsegv:
4562 if (sig == TARGET_SIGSEGV) {
4563 ka->_sa_handler = TARGET_SIG_DFL;
4565 force_sig(TARGET_SIGSEGV);
4568 env->ir[IR_RA] = r26;
4569 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4570 env->ir[IR_A0] = sig;
4571 env->ir[IR_A1] = 0;
4572 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
4573 env->ir[IR_SP] = frame_addr;
4576 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4577 target_siginfo_t *info,
4578 target_sigset_t *set, CPUState *env)
4580 abi_ulong frame_addr, r26;
4581 struct target_rt_sigframe *frame;
4582 int i, err = 0;
4584 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4585 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4586 goto give_sigsegv;
4589 err |= copy_siginfo_to_user(&frame->info, info);
4591 err |= __put_user(0, &frame->uc.tuc_flags);
4592 err |= __put_user(0, &frame->uc.tuc_link);
4593 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
4594 err |= __put_user(target_sigaltstack_used.ss_sp,
4595 &frame->uc.tuc_stack.ss_sp);
4596 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
4597 &frame->uc.tuc_stack.ss_flags);
4598 err |= __put_user(target_sigaltstack_used.ss_size,
4599 &frame->uc.tuc_stack.ss_size);
4600 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
4601 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
4602 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
4605 if (ka->sa_restorer) {
4606 r26 = ka->sa_restorer;
4607 } else {
4608 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4609 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
4610 &frame->retcode[1]);
4611 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4612 /* imb(); */
4613 r26 = frame_addr;
4616 if (err) {
4617 give_sigsegv:
4618 if (sig == TARGET_SIGSEGV) {
4619 ka->_sa_handler = TARGET_SIG_DFL;
4621 force_sig(TARGET_SIGSEGV);
4624 env->ir[IR_RA] = r26;
4625 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4626 env->ir[IR_A0] = sig;
4627 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
4628 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
4629 env->ir[IR_SP] = frame_addr;
4632 long do_sigreturn(CPUState *env)
4634 struct target_sigcontext *sc;
4635 abi_ulong sc_addr = env->ir[IR_A0];
4636 target_sigset_t target_set;
4637 sigset_t set;
4639 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
4640 goto badframe;
4643 target_sigemptyset(&target_set);
4644 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
4645 goto badframe;
4648 target_to_host_sigset_internal(&set, &target_set);
4649 sigprocmask(SIG_SETMASK, &set, NULL);
4651 if (restore_sigcontext(env, sc)) {
4652 goto badframe;
4654 unlock_user_struct(sc, sc_addr, 0);
4655 return env->ir[IR_V0];
4657 badframe:
4658 unlock_user_struct(sc, sc_addr, 0);
4659 force_sig(TARGET_SIGSEGV);
4662 long do_rt_sigreturn(CPUState *env)
4664 abi_ulong frame_addr = env->ir[IR_A0];
4665 struct target_rt_sigframe *frame;
4666 sigset_t set;
4668 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4669 goto badframe;
4671 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4672 sigprocmask(SIG_SETMASK, &set, NULL);
4674 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
4675 goto badframe;
4677 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
4678 uc.tuc_stack),
4679 0, env->ir[IR_SP]) == -EFAULT) {
4680 goto badframe;
4683 unlock_user_struct(frame, frame_addr, 0);
4684 return env->ir[IR_V0];
4687 badframe:
4688 unlock_user_struct(frame, frame_addr, 0);
4689 force_sig(TARGET_SIGSEGV);
4692 #else
4694 static void setup_frame(int sig, struct target_sigaction *ka,
4695 target_sigset_t *set, CPUState *env)
4697 fprintf(stderr, "setup_frame: not implemented\n");
4700 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4701 target_siginfo_t *info,
4702 target_sigset_t *set, CPUState *env)
4704 fprintf(stderr, "setup_rt_frame: not implemented\n");
4707 long do_sigreturn(CPUState *env)
4709 fprintf(stderr, "do_sigreturn: not implemented\n");
4710 return -TARGET_ENOSYS;
4713 long do_rt_sigreturn(CPUState *env)
4715 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
4716 return -TARGET_ENOSYS;
4719 #endif
4721 void process_pending_signals(CPUState *cpu_env)
4723 int sig;
4724 abi_ulong handler;
4725 sigset_t set, old_set;
4726 target_sigset_t target_old_set;
4727 struct emulated_sigtable *k;
4728 struct target_sigaction *sa;
4729 struct sigqueue *q;
4730 TaskState *ts = cpu_env->opaque;
4732 if (!ts->signal_pending)
4733 return;
4735 /* FIXME: This is not threadsafe. */
4736 k = ts->sigtab;
4737 for(sig = 1; sig <= TARGET_NSIG; sig++) {
4738 if (k->pending)
4739 goto handle_signal;
4740 k++;
4742 /* if no signal is pending, just return */
4743 ts->signal_pending = 0;
4744 return;
4746 handle_signal:
4747 #ifdef DEBUG_SIGNAL
4748 fprintf(stderr, "qemu: process signal %d\n", sig);
4749 #endif
4750 /* dequeue signal */
4751 q = k->first;
4752 k->first = q->next;
4753 if (!k->first)
4754 k->pending = 0;
4756 sig = gdb_handlesig (cpu_env, sig);
4757 if (!sig) {
4758 sa = NULL;
4759 handler = TARGET_SIG_IGN;
4760 } else {
4761 sa = &sigact_table[sig - 1];
4762 handler = sa->_sa_handler;
4765 if (handler == TARGET_SIG_DFL) {
4766 /* default handler : ignore some signal. The other are job control or fatal */
4767 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
4768 kill(getpid(),SIGSTOP);
4769 } else if (sig != TARGET_SIGCHLD &&
4770 sig != TARGET_SIGURG &&
4771 sig != TARGET_SIGWINCH &&
4772 sig != TARGET_SIGCONT) {
4773 force_sig(sig);
4775 } else if (handler == TARGET_SIG_IGN) {
4776 /* ignore sig */
4777 } else if (handler == TARGET_SIG_ERR) {
4778 force_sig(sig);
4779 } else {
4780 /* compute the blocked signals during the handler execution */
4781 target_to_host_sigset(&set, &sa->sa_mask);
4782 /* SA_NODEFER indicates that the current signal should not be
4783 blocked during the handler */
4784 if (!(sa->sa_flags & TARGET_SA_NODEFER))
4785 sigaddset(&set, target_to_host_signal(sig));
4787 /* block signals in the handler using Linux */
4788 sigprocmask(SIG_BLOCK, &set, &old_set);
4789 /* save the previous blocked signal state to restore it at the
4790 end of the signal execution (see do_sigreturn) */
4791 host_to_target_sigset_internal(&target_old_set, &old_set);
4793 /* if the CPU is in VM86 mode, we restore the 32 bit values */
4794 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
4796 CPUX86State *env = cpu_env;
4797 if (env->eflags & VM_MASK)
4798 save_v86_state(env);
4800 #endif
4801 /* prepare the stack frame of the virtual CPU */
4802 if (sa->sa_flags & TARGET_SA_SIGINFO)
4803 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
4804 else
4805 setup_frame(sig, sa, &target_old_set, cpu_env);
4806 if (sa->sa_flags & TARGET_SA_RESETHAND)
4807 sa->_sa_handler = TARGET_SIG_DFL;
4809 if (q != &k->info)
4810 free_sigqueue(cpu_env, q);