target-ppc: Initialize OpenPIC properly
[qemu.git] / linux-user / signal.c
blobdf189c3cbe3e967af6e91bbea604ac0031467485
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 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(sig);
375 gdb_signalled(thread_env, sig);
377 /* dump core if supported by target binary format */
378 if (core_dump_signal(sig) && (ts->bprm->core_dump != NULL)) {
379 stop_all_tasks();
380 core_dumped =
381 ((*ts->bprm->core_dump)(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 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 assert(0);
418 /* queue a signal so that it will be send to the virtual CPU as soon
419 as possible */
420 int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
422 TaskState *ts = env->opaque;
423 struct emulated_sigtable *k;
424 struct sigqueue *q, **pq;
425 abi_ulong handler;
426 int queue;
428 #if defined(DEBUG_SIGNAL)
429 fprintf(stderr, "queue_signal: sig=%d\n",
430 sig);
431 #endif
432 k = &ts->sigtab[sig - 1];
433 queue = gdb_queuesig ();
434 handler = sigact_table[sig - 1]._sa_handler;
435 if (!queue && handler == TARGET_SIG_DFL) {
436 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
437 kill(getpid(),SIGSTOP);
438 return 0;
439 } else
440 /* default handler : ignore some signal. The other are fatal */
441 if (sig != TARGET_SIGCHLD &&
442 sig != TARGET_SIGURG &&
443 sig != TARGET_SIGWINCH &&
444 sig != TARGET_SIGCONT) {
445 force_sig(sig);
446 } else {
447 return 0; /* indicate ignored */
449 } else if (!queue && handler == TARGET_SIG_IGN) {
450 /* ignore signal */
451 return 0;
452 } else if (!queue && handler == TARGET_SIG_ERR) {
453 force_sig(sig);
454 } else {
455 pq = &k->first;
456 if (sig < TARGET_SIGRTMIN) {
457 /* if non real time signal, we queue exactly one signal */
458 if (!k->pending)
459 q = &k->info;
460 else
461 return 0;
462 } else {
463 if (!k->pending) {
464 /* first signal */
465 q = &k->info;
466 } else {
467 q = alloc_sigqueue(env);
468 if (!q)
469 return -EAGAIN;
470 while (*pq != NULL)
471 pq = &(*pq)->next;
474 *pq = q;
475 q->info = *info;
476 q->next = NULL;
477 k->pending = 1;
478 /* signal that a new signal is pending */
479 ts->signal_pending = 1;
480 return 1; /* indicates that the signal was queued */
484 static void host_signal_handler(int host_signum, siginfo_t *info,
485 void *puc)
487 int sig;
488 target_siginfo_t tinfo;
490 /* the CPU emulator uses some host signals to detect exceptions,
491 we forward to it some signals */
492 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
493 && info->si_code > 0) {
494 if (cpu_signal_handler(host_signum, info, puc))
495 return;
498 /* get target signal number */
499 sig = host_to_target_signal(host_signum);
500 if (sig < 1 || sig > TARGET_NSIG)
501 return;
502 #if defined(DEBUG_SIGNAL)
503 fprintf(stderr, "qemu: got signal %d\n", sig);
504 #endif
505 host_to_target_siginfo_noswap(&tinfo, info);
506 if (queue_signal(thread_env, sig, &tinfo) == 1) {
507 /* interrupt the virtual CPU as soon as possible */
508 cpu_exit(thread_env);
512 /* do_sigaltstack() returns target values and errnos. */
513 /* compare linux/kernel/signal.c:do_sigaltstack() */
514 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
516 int ret;
517 struct target_sigaltstack oss;
519 /* XXX: test errors */
520 if(uoss_addr)
522 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
523 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
524 __put_user(sas_ss_flags(sp), &oss.ss_flags);
527 if(uss_addr)
529 struct target_sigaltstack *uss;
530 struct target_sigaltstack ss;
532 ret = -TARGET_EFAULT;
533 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
534 || __get_user(ss.ss_sp, &uss->ss_sp)
535 || __get_user(ss.ss_size, &uss->ss_size)
536 || __get_user(ss.ss_flags, &uss->ss_flags))
537 goto out;
538 unlock_user_struct(uss, uss_addr, 0);
540 ret = -TARGET_EPERM;
541 if (on_sig_stack(sp))
542 goto out;
544 ret = -TARGET_EINVAL;
545 if (ss.ss_flags != TARGET_SS_DISABLE
546 && ss.ss_flags != TARGET_SS_ONSTACK
547 && ss.ss_flags != 0)
548 goto out;
550 if (ss.ss_flags == TARGET_SS_DISABLE) {
551 ss.ss_size = 0;
552 ss.ss_sp = 0;
553 } else {
554 ret = -TARGET_ENOMEM;
555 if (ss.ss_size < MINSIGSTKSZ)
556 goto out;
559 target_sigaltstack_used.ss_sp = ss.ss_sp;
560 target_sigaltstack_used.ss_size = ss.ss_size;
563 if (uoss_addr) {
564 ret = -TARGET_EFAULT;
565 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
566 goto out;
569 ret = 0;
570 out:
571 return ret;
574 /* do_sigaction() return host values and errnos */
575 int do_sigaction(int sig, const struct target_sigaction *act,
576 struct target_sigaction *oact)
578 struct target_sigaction *k;
579 struct sigaction act1;
580 int host_sig;
581 int ret = 0;
583 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
584 return -EINVAL;
585 k = &sigact_table[sig - 1];
586 #if defined(DEBUG_SIGNAL)
587 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
588 sig, act, oact);
589 #endif
590 if (oact) {
591 oact->_sa_handler = tswapl(k->_sa_handler);
592 oact->sa_flags = tswapl(k->sa_flags);
593 #if !defined(TARGET_MIPS)
594 oact->sa_restorer = tswapl(k->sa_restorer);
595 #endif
596 oact->sa_mask = k->sa_mask;
598 if (act) {
599 /* FIXME: This is not threadsafe. */
600 k->_sa_handler = tswapl(act->_sa_handler);
601 k->sa_flags = tswapl(act->sa_flags);
602 #if !defined(TARGET_MIPS)
603 k->sa_restorer = tswapl(act->sa_restorer);
604 #endif
605 k->sa_mask = act->sa_mask;
607 /* we update the host linux signal state */
608 host_sig = target_to_host_signal(sig);
609 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
610 sigfillset(&act1.sa_mask);
611 act1.sa_flags = SA_SIGINFO;
612 if (k->sa_flags & TARGET_SA_RESTART)
613 act1.sa_flags |= SA_RESTART;
614 /* NOTE: it is important to update the host kernel signal
615 ignore state to avoid getting unexpected interrupted
616 syscalls */
617 if (k->_sa_handler == TARGET_SIG_IGN) {
618 act1.sa_sigaction = (void *)SIG_IGN;
619 } else if (k->_sa_handler == TARGET_SIG_DFL) {
620 if (fatal_signal (sig))
621 act1.sa_sigaction = host_signal_handler;
622 else
623 act1.sa_sigaction = (void *)SIG_DFL;
624 } else {
625 act1.sa_sigaction = host_signal_handler;
627 ret = sigaction(host_sig, &act1, NULL);
630 return ret;
633 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
634 const target_siginfo_t *info)
636 tswap_siginfo(tinfo, info);
637 return 0;
640 static inline int current_exec_domain_sig(int sig)
642 return /* current->exec_domain && current->exec_domain->signal_invmap
643 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
646 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
648 /* from the Linux kernel */
650 struct target_fpreg {
651 uint16_t significand[4];
652 uint16_t exponent;
655 struct target_fpxreg {
656 uint16_t significand[4];
657 uint16_t exponent;
658 uint16_t padding[3];
661 struct target_xmmreg {
662 abi_ulong element[4];
665 struct target_fpstate {
666 /* Regular FPU environment */
667 abi_ulong cw;
668 abi_ulong sw;
669 abi_ulong tag;
670 abi_ulong ipoff;
671 abi_ulong cssel;
672 abi_ulong dataoff;
673 abi_ulong datasel;
674 struct target_fpreg _st[8];
675 uint16_t status;
676 uint16_t magic; /* 0xffff = regular FPU data only */
678 /* FXSR FPU environment */
679 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
680 abi_ulong mxcsr;
681 abi_ulong reserved;
682 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
683 struct target_xmmreg _xmm[8];
684 abi_ulong padding[56];
687 #define X86_FXSR_MAGIC 0x0000
689 struct target_sigcontext {
690 uint16_t gs, __gsh;
691 uint16_t fs, __fsh;
692 uint16_t es, __esh;
693 uint16_t ds, __dsh;
694 abi_ulong edi;
695 abi_ulong esi;
696 abi_ulong ebp;
697 abi_ulong esp;
698 abi_ulong ebx;
699 abi_ulong edx;
700 abi_ulong ecx;
701 abi_ulong eax;
702 abi_ulong trapno;
703 abi_ulong err;
704 abi_ulong eip;
705 uint16_t cs, __csh;
706 abi_ulong eflags;
707 abi_ulong esp_at_signal;
708 uint16_t ss, __ssh;
709 abi_ulong fpstate; /* pointer */
710 abi_ulong oldmask;
711 abi_ulong cr2;
714 struct target_ucontext {
715 abi_ulong tuc_flags;
716 abi_ulong tuc_link;
717 target_stack_t tuc_stack;
718 struct target_sigcontext tuc_mcontext;
719 target_sigset_t tuc_sigmask; /* mask last for extensibility */
722 struct sigframe
724 abi_ulong pretcode;
725 int sig;
726 struct target_sigcontext sc;
727 struct target_fpstate fpstate;
728 abi_ulong extramask[TARGET_NSIG_WORDS-1];
729 char retcode[8];
732 struct rt_sigframe
734 abi_ulong pretcode;
735 int sig;
736 abi_ulong pinfo;
737 abi_ulong puc;
738 struct target_siginfo info;
739 struct target_ucontext uc;
740 struct target_fpstate fpstate;
741 char retcode[8];
745 * Set up a signal frame.
748 /* XXX: save x87 state */
749 static int
750 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
751 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
753 int err = 0;
754 uint16_t magic;
756 /* already locked in setup_frame() */
757 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
758 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
759 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
760 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
761 err |= __put_user(env->regs[R_EDI], &sc->edi);
762 err |= __put_user(env->regs[R_ESI], &sc->esi);
763 err |= __put_user(env->regs[R_EBP], &sc->ebp);
764 err |= __put_user(env->regs[R_ESP], &sc->esp);
765 err |= __put_user(env->regs[R_EBX], &sc->ebx);
766 err |= __put_user(env->regs[R_EDX], &sc->edx);
767 err |= __put_user(env->regs[R_ECX], &sc->ecx);
768 err |= __put_user(env->regs[R_EAX], &sc->eax);
769 err |= __put_user(env->exception_index, &sc->trapno);
770 err |= __put_user(env->error_code, &sc->err);
771 err |= __put_user(env->eip, &sc->eip);
772 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
773 err |= __put_user(env->eflags, &sc->eflags);
774 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
775 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
777 cpu_x86_fsave(env, fpstate_addr, 1);
778 fpstate->status = fpstate->sw;
779 magic = 0xffff;
780 err |= __put_user(magic, &fpstate->magic);
781 err |= __put_user(fpstate_addr, &sc->fpstate);
783 /* non-iBCS2 extensions.. */
784 err |= __put_user(mask, &sc->oldmask);
785 err |= __put_user(env->cr[2], &sc->cr2);
786 return err;
790 * Determine which stack to use..
793 static inline abi_ulong
794 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
796 unsigned long esp;
798 /* Default to using normal stack */
799 esp = env->regs[R_ESP];
800 /* This is the X/Open sanctioned signal stack switching. */
801 if (ka->sa_flags & TARGET_SA_ONSTACK) {
802 if (sas_ss_flags(esp) == 0)
803 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
806 /* This is the legacy signal stack switching. */
807 else
808 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
809 !(ka->sa_flags & TARGET_SA_RESTORER) &&
810 ka->sa_restorer) {
811 esp = (unsigned long) ka->sa_restorer;
813 return (esp - frame_size) & -8ul;
816 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
817 static void setup_frame(int sig, struct target_sigaction *ka,
818 target_sigset_t *set, CPUX86State *env)
820 abi_ulong frame_addr;
821 struct sigframe *frame;
822 int i, err = 0;
824 frame_addr = get_sigframe(ka, env, sizeof(*frame));
826 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
827 goto give_sigsegv;
829 err |= __put_user(current_exec_domain_sig(sig),
830 &frame->sig);
831 if (err)
832 goto give_sigsegv;
834 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
835 frame_addr + offsetof(struct sigframe, fpstate));
836 if (err)
837 goto give_sigsegv;
839 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
840 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
841 goto give_sigsegv;
844 /* Set up to return from userspace. If provided, use a stub
845 already in userspace. */
846 if (ka->sa_flags & TARGET_SA_RESTORER) {
847 err |= __put_user(ka->sa_restorer, &frame->pretcode);
848 } else {
849 uint16_t val16;
850 abi_ulong retcode_addr;
851 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
852 err |= __put_user(retcode_addr, &frame->pretcode);
853 /* This is popl %eax ; movl $,%eax ; int $0x80 */
854 val16 = 0xb858;
855 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
856 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
857 val16 = 0x80cd;
858 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
861 if (err)
862 goto give_sigsegv;
864 /* Set up registers for signal handler */
865 env->regs[R_ESP] = frame_addr;
866 env->eip = ka->_sa_handler;
868 cpu_x86_load_seg(env, R_DS, __USER_DS);
869 cpu_x86_load_seg(env, R_ES, __USER_DS);
870 cpu_x86_load_seg(env, R_SS, __USER_DS);
871 cpu_x86_load_seg(env, R_CS, __USER_CS);
872 env->eflags &= ~TF_MASK;
874 unlock_user_struct(frame, frame_addr, 1);
876 return;
878 give_sigsegv:
879 unlock_user_struct(frame, frame_addr, 1);
880 if (sig == TARGET_SIGSEGV)
881 ka->_sa_handler = TARGET_SIG_DFL;
882 force_sig(TARGET_SIGSEGV /* , current */);
885 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
886 static void setup_rt_frame(int sig, struct target_sigaction *ka,
887 target_siginfo_t *info,
888 target_sigset_t *set, CPUX86State *env)
890 abi_ulong frame_addr, addr;
891 struct rt_sigframe *frame;
892 int i, err = 0;
894 frame_addr = get_sigframe(ka, env, sizeof(*frame));
896 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
897 goto give_sigsegv;
899 err |= __put_user(current_exec_domain_sig(sig),
900 &frame->sig);
901 addr = frame_addr + offsetof(struct rt_sigframe, info);
902 err |= __put_user(addr, &frame->pinfo);
903 addr = frame_addr + offsetof(struct rt_sigframe, uc);
904 err |= __put_user(addr, &frame->puc);
905 err |= copy_siginfo_to_user(&frame->info, info);
906 if (err)
907 goto give_sigsegv;
909 /* Create the ucontext. */
910 err |= __put_user(0, &frame->uc.tuc_flags);
911 err |= __put_user(0, &frame->uc.tuc_link);
912 err |= __put_user(target_sigaltstack_used.ss_sp,
913 &frame->uc.tuc_stack.ss_sp);
914 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
915 &frame->uc.tuc_stack.ss_flags);
916 err |= __put_user(target_sigaltstack_used.ss_size,
917 &frame->uc.tuc_stack.ss_size);
918 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
919 env, set->sig[0],
920 frame_addr + offsetof(struct rt_sigframe, fpstate));
921 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
922 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
923 goto give_sigsegv;
926 /* Set up to return from userspace. If provided, use a stub
927 already in userspace. */
928 if (ka->sa_flags & TARGET_SA_RESTORER) {
929 err |= __put_user(ka->sa_restorer, &frame->pretcode);
930 } else {
931 uint16_t val16;
932 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
933 err |= __put_user(addr, &frame->pretcode);
934 /* This is movl $,%eax ; int $0x80 */
935 err |= __put_user(0xb8, (char *)(frame->retcode+0));
936 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
937 val16 = 0x80cd;
938 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
941 if (err)
942 goto give_sigsegv;
944 /* Set up registers for signal handler */
945 env->regs[R_ESP] = frame_addr;
946 env->eip = ka->_sa_handler;
948 cpu_x86_load_seg(env, R_DS, __USER_DS);
949 cpu_x86_load_seg(env, R_ES, __USER_DS);
950 cpu_x86_load_seg(env, R_SS, __USER_DS);
951 cpu_x86_load_seg(env, R_CS, __USER_CS);
952 env->eflags &= ~TF_MASK;
954 unlock_user_struct(frame, frame_addr, 1);
956 return;
958 give_sigsegv:
959 unlock_user_struct(frame, frame_addr, 1);
960 if (sig == TARGET_SIGSEGV)
961 ka->_sa_handler = TARGET_SIG_DFL;
962 force_sig(TARGET_SIGSEGV /* , current */);
965 static int
966 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
968 unsigned int err = 0;
969 abi_ulong fpstate_addr;
970 unsigned int tmpflags;
972 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
973 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
974 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
975 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
977 env->regs[R_EDI] = tswapl(sc->edi);
978 env->regs[R_ESI] = tswapl(sc->esi);
979 env->regs[R_EBP] = tswapl(sc->ebp);
980 env->regs[R_ESP] = tswapl(sc->esp);
981 env->regs[R_EBX] = tswapl(sc->ebx);
982 env->regs[R_EDX] = tswapl(sc->edx);
983 env->regs[R_ECX] = tswapl(sc->ecx);
984 env->eip = tswapl(sc->eip);
986 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
987 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
989 tmpflags = tswapl(sc->eflags);
990 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
991 // regs->orig_eax = -1; /* disable syscall checks */
993 fpstate_addr = tswapl(sc->fpstate);
994 if (fpstate_addr != 0) {
995 if (!access_ok(VERIFY_READ, fpstate_addr,
996 sizeof(struct target_fpstate)))
997 goto badframe;
998 cpu_x86_frstor(env, fpstate_addr, 1);
1001 *peax = tswapl(sc->eax);
1002 return err;
1003 badframe:
1004 return 1;
1007 long do_sigreturn(CPUX86State *env)
1009 struct sigframe *frame;
1010 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1011 target_sigset_t target_set;
1012 sigset_t set;
1013 int eax, i;
1015 #if defined(DEBUG_SIGNAL)
1016 fprintf(stderr, "do_sigreturn\n");
1017 #endif
1018 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1019 goto badframe;
1020 /* set blocked signals */
1021 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1022 goto badframe;
1023 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1024 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1025 goto badframe;
1028 target_to_host_sigset_internal(&set, &target_set);
1029 sigprocmask(SIG_SETMASK, &set, NULL);
1031 /* restore registers */
1032 if (restore_sigcontext(env, &frame->sc, &eax))
1033 goto badframe;
1034 unlock_user_struct(frame, frame_addr, 0);
1035 return eax;
1037 badframe:
1038 unlock_user_struct(frame, frame_addr, 0);
1039 force_sig(TARGET_SIGSEGV);
1040 return 0;
1043 long do_rt_sigreturn(CPUX86State *env)
1045 abi_ulong frame_addr;
1046 struct rt_sigframe *frame;
1047 sigset_t set;
1048 int eax;
1050 frame_addr = env->regs[R_ESP] - 4;
1051 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1052 goto badframe;
1053 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1054 sigprocmask(SIG_SETMASK, &set, NULL);
1056 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1057 goto badframe;
1059 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1060 get_sp_from_cpustate(env)) == -EFAULT)
1061 goto badframe;
1063 unlock_user_struct(frame, frame_addr, 0);
1064 return eax;
1066 badframe:
1067 unlock_user_struct(frame, frame_addr, 0);
1068 force_sig(TARGET_SIGSEGV);
1069 return 0;
1072 #elif defined(TARGET_ARM)
1074 struct target_sigcontext {
1075 abi_ulong trap_no;
1076 abi_ulong error_code;
1077 abi_ulong oldmask;
1078 abi_ulong arm_r0;
1079 abi_ulong arm_r1;
1080 abi_ulong arm_r2;
1081 abi_ulong arm_r3;
1082 abi_ulong arm_r4;
1083 abi_ulong arm_r5;
1084 abi_ulong arm_r6;
1085 abi_ulong arm_r7;
1086 abi_ulong arm_r8;
1087 abi_ulong arm_r9;
1088 abi_ulong arm_r10;
1089 abi_ulong arm_fp;
1090 abi_ulong arm_ip;
1091 abi_ulong arm_sp;
1092 abi_ulong arm_lr;
1093 abi_ulong arm_pc;
1094 abi_ulong arm_cpsr;
1095 abi_ulong fault_address;
1098 struct target_ucontext_v1 {
1099 abi_ulong tuc_flags;
1100 abi_ulong tuc_link;
1101 target_stack_t tuc_stack;
1102 struct target_sigcontext tuc_mcontext;
1103 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1106 struct target_ucontext_v2 {
1107 abi_ulong tuc_flags;
1108 abi_ulong tuc_link;
1109 target_stack_t tuc_stack;
1110 struct target_sigcontext tuc_mcontext;
1111 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1112 char __unused[128 - sizeof(sigset_t)];
1113 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1116 struct sigframe_v1
1118 struct target_sigcontext sc;
1119 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1120 abi_ulong retcode;
1123 struct sigframe_v2
1125 struct target_ucontext_v2 uc;
1126 abi_ulong retcode;
1129 struct rt_sigframe_v1
1131 abi_ulong pinfo;
1132 abi_ulong puc;
1133 struct target_siginfo info;
1134 struct target_ucontext_v1 uc;
1135 abi_ulong retcode;
1138 struct rt_sigframe_v2
1140 struct target_siginfo info;
1141 struct target_ucontext_v2 uc;
1142 abi_ulong retcode;
1145 #define TARGET_CONFIG_CPU_32 1
1148 * For ARM syscalls, we encode the syscall number into the instruction.
1150 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1151 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1154 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1155 * need two 16-bit instructions.
1157 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1158 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1160 static const abi_ulong retcodes[4] = {
1161 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1162 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1166 #define __get_user_error(x,p,e) __get_user(x, p)
1168 static inline int valid_user_regs(CPUState *regs)
1170 return 1;
1173 static void
1174 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1175 CPUState *env, abi_ulong mask)
1177 __put_user(env->regs[0], &sc->arm_r0);
1178 __put_user(env->regs[1], &sc->arm_r1);
1179 __put_user(env->regs[2], &sc->arm_r2);
1180 __put_user(env->regs[3], &sc->arm_r3);
1181 __put_user(env->regs[4], &sc->arm_r4);
1182 __put_user(env->regs[5], &sc->arm_r5);
1183 __put_user(env->regs[6], &sc->arm_r6);
1184 __put_user(env->regs[7], &sc->arm_r7);
1185 __put_user(env->regs[8], &sc->arm_r8);
1186 __put_user(env->regs[9], &sc->arm_r9);
1187 __put_user(env->regs[10], &sc->arm_r10);
1188 __put_user(env->regs[11], &sc->arm_fp);
1189 __put_user(env->regs[12], &sc->arm_ip);
1190 __put_user(env->regs[13], &sc->arm_sp);
1191 __put_user(env->regs[14], &sc->arm_lr);
1192 __put_user(env->regs[15], &sc->arm_pc);
1193 #ifdef TARGET_CONFIG_CPU_32
1194 __put_user(cpsr_read(env), &sc->arm_cpsr);
1195 #endif
1197 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1198 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1199 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1200 __put_user(mask, &sc->oldmask);
1203 static inline abi_ulong
1204 get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1206 unsigned long sp = regs->regs[13];
1209 * This is the X/Open sanctioned signal stack switching.
1211 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1212 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1214 * ATPCS B01 mandates 8-byte alignment
1216 return (sp - framesize) & ~7;
1219 static int
1220 setup_return(CPUState *env, struct target_sigaction *ka,
1221 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1223 abi_ulong handler = ka->_sa_handler;
1224 abi_ulong retcode;
1225 int thumb = handler & 1;
1227 if (ka->sa_flags & TARGET_SA_RESTORER) {
1228 retcode = ka->sa_restorer;
1229 } else {
1230 unsigned int idx = thumb;
1232 if (ka->sa_flags & TARGET_SA_SIGINFO)
1233 idx += 2;
1235 if (__put_user(retcodes[idx], rc))
1236 return 1;
1237 #if 0
1238 flush_icache_range((abi_ulong)rc,
1239 (abi_ulong)(rc + 1));
1240 #endif
1241 retcode = rc_addr + thumb;
1244 env->regs[0] = usig;
1245 env->regs[13] = frame_addr;
1246 env->regs[14] = retcode;
1247 env->regs[15] = handler & (thumb ? ~1 : ~3);
1248 env->thumb = thumb;
1250 #if 0
1251 #ifdef TARGET_CONFIG_CPU_32
1252 env->cpsr = cpsr;
1253 #endif
1254 #endif
1256 return 0;
1259 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1260 target_sigset_t *set, CPUState *env)
1262 struct target_sigaltstack stack;
1263 int i;
1265 /* Clear all the bits of the ucontext we don't use. */
1266 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1268 memset(&stack, 0, sizeof(stack));
1269 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1270 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1271 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1272 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1274 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1275 /* FIXME: Save coprocessor signal frame. */
1276 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1277 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1281 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1282 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1283 target_sigset_t *set, CPUState *regs)
1285 struct sigframe_v1 *frame;
1286 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1287 int i;
1289 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1290 return;
1292 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1294 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1295 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1296 goto end;
1299 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1300 frame_addr + offsetof(struct sigframe_v1, retcode));
1302 end:
1303 unlock_user_struct(frame, frame_addr, 1);
1306 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1307 target_sigset_t *set, CPUState *regs)
1309 struct sigframe_v2 *frame;
1310 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1312 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1313 return;
1315 setup_sigframe_v2(&frame->uc, set, regs);
1317 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1318 frame_addr + offsetof(struct sigframe_v2, retcode));
1320 unlock_user_struct(frame, frame_addr, 1);
1323 static void setup_frame(int usig, struct target_sigaction *ka,
1324 target_sigset_t *set, CPUState *regs)
1326 if (get_osversion() >= 0x020612) {
1327 setup_frame_v2(usig, ka, set, regs);
1328 } else {
1329 setup_frame_v1(usig, ka, set, regs);
1333 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1334 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1335 target_siginfo_t *info,
1336 target_sigset_t *set, CPUState *env)
1338 struct rt_sigframe_v1 *frame;
1339 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1340 struct target_sigaltstack stack;
1341 int i;
1342 abi_ulong info_addr, uc_addr;
1344 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1345 return /* 1 */;
1347 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1348 __put_user(info_addr, &frame->pinfo);
1349 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1350 __put_user(uc_addr, &frame->puc);
1351 copy_siginfo_to_user(&frame->info, info);
1353 /* Clear all the bits of the ucontext we don't use. */
1354 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1356 memset(&stack, 0, sizeof(stack));
1357 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1358 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1359 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1360 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1362 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1363 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1364 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1365 goto end;
1368 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1369 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1371 env->regs[1] = info_addr;
1372 env->regs[2] = uc_addr;
1374 end:
1375 unlock_user_struct(frame, frame_addr, 1);
1378 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1379 target_siginfo_t *info,
1380 target_sigset_t *set, CPUState *env)
1382 struct rt_sigframe_v2 *frame;
1383 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1384 abi_ulong info_addr, uc_addr;
1386 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1387 return /* 1 */;
1389 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1390 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1391 copy_siginfo_to_user(&frame->info, info);
1393 setup_sigframe_v2(&frame->uc, set, env);
1395 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1396 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1398 env->regs[1] = info_addr;
1399 env->regs[2] = uc_addr;
1401 unlock_user_struct(frame, frame_addr, 1);
1404 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1405 target_siginfo_t *info,
1406 target_sigset_t *set, CPUState *env)
1408 if (get_osversion() >= 0x020612) {
1409 setup_rt_frame_v2(usig, ka, info, set, env);
1410 } else {
1411 setup_rt_frame_v1(usig, ka, info, set, env);
1415 static int
1416 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1418 int err = 0;
1419 uint32_t cpsr;
1421 __get_user_error(env->regs[0], &sc->arm_r0, err);
1422 __get_user_error(env->regs[1], &sc->arm_r1, err);
1423 __get_user_error(env->regs[2], &sc->arm_r2, err);
1424 __get_user_error(env->regs[3], &sc->arm_r3, err);
1425 __get_user_error(env->regs[4], &sc->arm_r4, err);
1426 __get_user_error(env->regs[5], &sc->arm_r5, err);
1427 __get_user_error(env->regs[6], &sc->arm_r6, err);
1428 __get_user_error(env->regs[7], &sc->arm_r7, err);
1429 __get_user_error(env->regs[8], &sc->arm_r8, err);
1430 __get_user_error(env->regs[9], &sc->arm_r9, err);
1431 __get_user_error(env->regs[10], &sc->arm_r10, err);
1432 __get_user_error(env->regs[11], &sc->arm_fp, err);
1433 __get_user_error(env->regs[12], &sc->arm_ip, err);
1434 __get_user_error(env->regs[13], &sc->arm_sp, err);
1435 __get_user_error(env->regs[14], &sc->arm_lr, err);
1436 __get_user_error(env->regs[15], &sc->arm_pc, err);
1437 #ifdef TARGET_CONFIG_CPU_32
1438 __get_user_error(cpsr, &sc->arm_cpsr, err);
1439 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1440 #endif
1442 err |= !valid_user_regs(env);
1444 return err;
1447 static long do_sigreturn_v1(CPUState *env)
1449 abi_ulong frame_addr;
1450 struct sigframe_v1 *frame;
1451 target_sigset_t set;
1452 sigset_t host_set;
1453 int i;
1456 * Since we stacked the signal on a 64-bit boundary,
1457 * then 'sp' should be word aligned here. If it's
1458 * not, then the user is trying to mess with us.
1460 if (env->regs[13] & 7)
1461 goto badframe;
1463 frame_addr = env->regs[13];
1464 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1465 goto badframe;
1467 if (__get_user(set.sig[0], &frame->sc.oldmask))
1468 goto badframe;
1469 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1470 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1471 goto badframe;
1474 target_to_host_sigset_internal(&host_set, &set);
1475 sigprocmask(SIG_SETMASK, &host_set, NULL);
1477 if (restore_sigcontext(env, &frame->sc))
1478 goto badframe;
1480 #if 0
1481 /* Send SIGTRAP if we're single-stepping */
1482 if (ptrace_cancel_bpt(current))
1483 send_sig(SIGTRAP, current, 1);
1484 #endif
1485 unlock_user_struct(frame, frame_addr, 0);
1486 return env->regs[0];
1488 badframe:
1489 unlock_user_struct(frame, frame_addr, 0);
1490 force_sig(SIGSEGV /* , current */);
1491 return 0;
1494 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1495 struct target_ucontext_v2 *uc)
1497 sigset_t host_set;
1499 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1500 sigprocmask(SIG_SETMASK, &host_set, NULL);
1502 if (restore_sigcontext(env, &uc->tuc_mcontext))
1503 return 1;
1505 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1506 return 1;
1508 #if 0
1509 /* Send SIGTRAP if we're single-stepping */
1510 if (ptrace_cancel_bpt(current))
1511 send_sig(SIGTRAP, current, 1);
1512 #endif
1514 return 0;
1517 static long do_sigreturn_v2(CPUState *env)
1519 abi_ulong frame_addr;
1520 struct sigframe_v2 *frame;
1523 * Since we stacked the signal on a 64-bit boundary,
1524 * then 'sp' should be word aligned here. If it's
1525 * not, then the user is trying to mess with us.
1527 if (env->regs[13] & 7)
1528 goto badframe;
1530 frame_addr = env->regs[13];
1531 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1532 goto badframe;
1534 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1535 goto badframe;
1537 unlock_user_struct(frame, frame_addr, 0);
1538 return env->regs[0];
1540 badframe:
1541 unlock_user_struct(frame, frame_addr, 0);
1542 force_sig(SIGSEGV /* , current */);
1543 return 0;
1546 long do_sigreturn(CPUState *env)
1548 if (get_osversion() >= 0x020612) {
1549 return do_sigreturn_v2(env);
1550 } else {
1551 return do_sigreturn_v1(env);
1555 static long do_rt_sigreturn_v1(CPUState *env)
1557 abi_ulong frame_addr;
1558 struct rt_sigframe_v1 *frame;
1559 sigset_t host_set;
1562 * Since we stacked the signal on a 64-bit boundary,
1563 * then 'sp' should be word aligned here. If it's
1564 * not, then the user is trying to mess with us.
1566 if (env->regs[13] & 7)
1567 goto badframe;
1569 frame_addr = env->regs[13];
1570 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1571 goto badframe;
1573 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1574 sigprocmask(SIG_SETMASK, &host_set, NULL);
1576 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1577 goto badframe;
1579 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1580 goto badframe;
1582 #if 0
1583 /* Send SIGTRAP if we're single-stepping */
1584 if (ptrace_cancel_bpt(current))
1585 send_sig(SIGTRAP, current, 1);
1586 #endif
1587 unlock_user_struct(frame, frame_addr, 0);
1588 return env->regs[0];
1590 badframe:
1591 unlock_user_struct(frame, frame_addr, 0);
1592 force_sig(SIGSEGV /* , current */);
1593 return 0;
1596 static long do_rt_sigreturn_v2(CPUState *env)
1598 abi_ulong frame_addr;
1599 struct rt_sigframe_v2 *frame;
1602 * Since we stacked the signal on a 64-bit boundary,
1603 * then 'sp' should be word aligned here. If it's
1604 * not, then the user is trying to mess with us.
1606 if (env->regs[13] & 7)
1607 goto badframe;
1609 frame_addr = env->regs[13];
1610 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1611 goto badframe;
1613 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1614 goto badframe;
1616 unlock_user_struct(frame, frame_addr, 0);
1617 return env->regs[0];
1619 badframe:
1620 unlock_user_struct(frame, frame_addr, 0);
1621 force_sig(SIGSEGV /* , current */);
1622 return 0;
1625 long do_rt_sigreturn(CPUState *env)
1627 if (get_osversion() >= 0x020612) {
1628 return do_rt_sigreturn_v2(env);
1629 } else {
1630 return do_rt_sigreturn_v1(env);
1634 #elif defined(TARGET_SPARC)
1636 #define __SUNOS_MAXWIN 31
1638 /* This is what SunOS does, so shall I. */
1639 struct target_sigcontext {
1640 abi_ulong sigc_onstack; /* state to restore */
1642 abi_ulong sigc_mask; /* sigmask to restore */
1643 abi_ulong sigc_sp; /* stack pointer */
1644 abi_ulong sigc_pc; /* program counter */
1645 abi_ulong sigc_npc; /* next program counter */
1646 abi_ulong sigc_psr; /* for condition codes etc */
1647 abi_ulong sigc_g1; /* User uses these two registers */
1648 abi_ulong sigc_o0; /* within the trampoline code. */
1650 /* Now comes information regarding the users window set
1651 * at the time of the signal.
1653 abi_ulong sigc_oswins; /* outstanding windows */
1655 /* stack ptrs for each regwin buf */
1656 char *sigc_spbuf[__SUNOS_MAXWIN];
1658 /* Windows to restore after signal */
1659 struct {
1660 abi_ulong locals[8];
1661 abi_ulong ins[8];
1662 } sigc_wbuf[__SUNOS_MAXWIN];
1664 /* A Sparc stack frame */
1665 struct sparc_stackf {
1666 abi_ulong locals[8];
1667 abi_ulong ins[6];
1668 struct sparc_stackf *fp;
1669 abi_ulong callers_pc;
1670 char *structptr;
1671 abi_ulong xargs[6];
1672 abi_ulong xxargs[1];
1675 typedef struct {
1676 struct {
1677 abi_ulong psr;
1678 abi_ulong pc;
1679 abi_ulong npc;
1680 abi_ulong y;
1681 abi_ulong u_regs[16]; /* globals and ins */
1682 } si_regs;
1683 int si_mask;
1684 } __siginfo_t;
1686 typedef struct {
1687 unsigned long si_float_regs [32];
1688 unsigned long si_fsr;
1689 unsigned long si_fpqdepth;
1690 struct {
1691 unsigned long *insn_addr;
1692 unsigned long insn;
1693 } si_fpqueue [16];
1694 } qemu_siginfo_fpu_t;
1697 struct target_signal_frame {
1698 struct sparc_stackf ss;
1699 __siginfo_t info;
1700 abi_ulong fpu_save;
1701 abi_ulong insns[2] __attribute__ ((aligned (8)));
1702 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1703 abi_ulong extra_size; /* Should be 0 */
1704 qemu_siginfo_fpu_t fpu_state;
1706 struct target_rt_signal_frame {
1707 struct sparc_stackf ss;
1708 siginfo_t info;
1709 abi_ulong regs[20];
1710 sigset_t mask;
1711 abi_ulong fpu_save;
1712 unsigned int insns[2];
1713 stack_t stack;
1714 unsigned int extra_size; /* Should be 0 */
1715 qemu_siginfo_fpu_t fpu_state;
1718 #define UREG_O0 16
1719 #define UREG_O6 22
1720 #define UREG_I0 0
1721 #define UREG_I1 1
1722 #define UREG_I2 2
1723 #define UREG_I3 3
1724 #define UREG_I4 4
1725 #define UREG_I5 5
1726 #define UREG_I6 6
1727 #define UREG_I7 7
1728 #define UREG_L0 8
1729 #define UREG_FP UREG_I6
1730 #define UREG_SP UREG_O6
1732 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1733 CPUState *env, unsigned long framesize)
1735 abi_ulong sp;
1737 sp = env->regwptr[UREG_FP];
1739 /* This is the X/Open sanctioned signal stack switching. */
1740 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1741 if (!on_sig_stack(sp)
1742 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1743 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1745 return sp - framesize;
1748 static int
1749 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1751 int err = 0, i;
1753 err |= __put_user(env->psr, &si->si_regs.psr);
1754 err |= __put_user(env->pc, &si->si_regs.pc);
1755 err |= __put_user(env->npc, &si->si_regs.npc);
1756 err |= __put_user(env->y, &si->si_regs.y);
1757 for (i=0; i < 8; i++) {
1758 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1760 for (i=0; i < 8; i++) {
1761 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1763 err |= __put_user(mask, &si->si_mask);
1764 return err;
1767 #if 0
1768 static int
1769 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1770 CPUState *env, unsigned long mask)
1772 int err = 0;
1774 err |= __put_user(mask, &sc->sigc_mask);
1775 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1776 err |= __put_user(env->pc, &sc->sigc_pc);
1777 err |= __put_user(env->npc, &sc->sigc_npc);
1778 err |= __put_user(env->psr, &sc->sigc_psr);
1779 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1780 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1782 return err;
1784 #endif
1785 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1787 static void setup_frame(int sig, struct target_sigaction *ka,
1788 target_sigset_t *set, CPUState *env)
1790 abi_ulong sf_addr;
1791 struct target_signal_frame *sf;
1792 int sigframe_size, err, i;
1794 /* 1. Make sure everything is clean */
1795 //synchronize_user_stack();
1797 sigframe_size = NF_ALIGNEDSZ;
1798 sf_addr = get_sigframe(ka, env, sigframe_size);
1800 sf = lock_user(VERIFY_WRITE, sf_addr,
1801 sizeof(struct target_signal_frame), 0);
1802 if (!sf)
1803 goto sigsegv;
1805 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1806 #if 0
1807 if (invalid_frame_pointer(sf, sigframe_size))
1808 goto sigill_and_return;
1809 #endif
1810 /* 2. Save the current process state */
1811 err = setup___siginfo(&sf->info, env, set->sig[0]);
1812 err |= __put_user(0, &sf->extra_size);
1814 //err |= save_fpu_state(regs, &sf->fpu_state);
1815 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1817 err |= __put_user(set->sig[0], &sf->info.si_mask);
1818 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1819 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1822 for (i = 0; i < 8; i++) {
1823 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1825 for (i = 0; i < 8; i++) {
1826 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1828 if (err)
1829 goto sigsegv;
1831 /* 3. signal handler back-trampoline and parameters */
1832 env->regwptr[UREG_FP] = sf_addr;
1833 env->regwptr[UREG_I0] = sig;
1834 env->regwptr[UREG_I1] = sf_addr +
1835 offsetof(struct target_signal_frame, info);
1836 env->regwptr[UREG_I2] = sf_addr +
1837 offsetof(struct target_signal_frame, info);
1839 /* 4. signal handler */
1840 env->pc = ka->_sa_handler;
1841 env->npc = (env->pc + 4);
1842 /* 5. return to kernel instructions */
1843 if (ka->sa_restorer)
1844 env->regwptr[UREG_I7] = ka->sa_restorer;
1845 else {
1846 uint32_t val32;
1848 env->regwptr[UREG_I7] = sf_addr +
1849 offsetof(struct target_signal_frame, insns) - 2 * 4;
1851 /* mov __NR_sigreturn, %g1 */
1852 val32 = 0x821020d8;
1853 err |= __put_user(val32, &sf->insns[0]);
1855 /* t 0x10 */
1856 val32 = 0x91d02010;
1857 err |= __put_user(val32, &sf->insns[1]);
1858 if (err)
1859 goto sigsegv;
1861 /* Flush instruction space. */
1862 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1863 // tb_flush(env);
1865 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1866 return;
1867 #if 0
1868 sigill_and_return:
1869 force_sig(TARGET_SIGILL);
1870 #endif
1871 sigsegv:
1872 //fprintf(stderr, "force_sig\n");
1873 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1874 force_sig(TARGET_SIGSEGV);
1876 static inline int
1877 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
1879 int err;
1880 #if 0
1881 #ifdef CONFIG_SMP
1882 if (current->flags & PF_USEDFPU)
1883 regs->psr &= ~PSR_EF;
1884 #else
1885 if (current == last_task_used_math) {
1886 last_task_used_math = 0;
1887 regs->psr &= ~PSR_EF;
1889 #endif
1890 current->used_math = 1;
1891 current->flags &= ~PF_USEDFPU;
1892 #endif
1893 #if 0
1894 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1895 return -EFAULT;
1896 #endif
1898 #if 0
1899 /* XXX: incorrect */
1900 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1901 (sizeof(unsigned long) * 32));
1902 #endif
1903 err |= __get_user(env->fsr, &fpu->si_fsr);
1904 #if 0
1905 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1906 if (current->thread.fpqdepth != 0)
1907 err |= __copy_from_user(&current->thread.fpqueue[0],
1908 &fpu->si_fpqueue[0],
1909 ((sizeof(unsigned long) +
1910 (sizeof(unsigned long *)))*16));
1911 #endif
1912 return err;
1916 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1917 target_siginfo_t *info,
1918 target_sigset_t *set, CPUState *env)
1920 fprintf(stderr, "setup_rt_frame: not implemented\n");
1923 long do_sigreturn(CPUState *env)
1925 abi_ulong sf_addr;
1926 struct target_signal_frame *sf;
1927 uint32_t up_psr, pc, npc;
1928 target_sigset_t set;
1929 sigset_t host_set;
1930 abi_ulong fpu_save_addr;
1931 int err, i;
1933 sf_addr = env->regwptr[UREG_FP];
1934 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1935 goto segv_and_exit;
1936 #if 0
1937 fprintf(stderr, "sigreturn\n");
1938 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1939 #endif
1940 //cpu_dump_state(env, stderr, fprintf, 0);
1942 /* 1. Make sure we are not getting garbage from the user */
1944 if (sf_addr & 3)
1945 goto segv_and_exit;
1947 err = __get_user(pc, &sf->info.si_regs.pc);
1948 err |= __get_user(npc, &sf->info.si_regs.npc);
1950 if ((pc | npc) & 3)
1951 goto segv_and_exit;
1953 /* 2. Restore the state */
1954 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1956 /* User can only change condition codes and FPU enabling in %psr. */
1957 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1958 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1960 env->pc = pc;
1961 env->npc = npc;
1962 err |= __get_user(env->y, &sf->info.si_regs.y);
1963 for (i=0; i < 8; i++) {
1964 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1966 for (i=0; i < 8; i++) {
1967 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1970 err |= __get_user(fpu_save_addr, &sf->fpu_save);
1972 //if (fpu_save)
1973 // err |= restore_fpu_state(env, fpu_save);
1975 /* This is pretty much atomic, no amount locking would prevent
1976 * the races which exist anyways.
1978 err |= __get_user(set.sig[0], &sf->info.si_mask);
1979 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1980 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1983 target_to_host_sigset_internal(&host_set, &set);
1984 sigprocmask(SIG_SETMASK, &host_set, NULL);
1986 if (err)
1987 goto segv_and_exit;
1988 unlock_user_struct(sf, sf_addr, 0);
1989 return env->regwptr[0];
1991 segv_and_exit:
1992 unlock_user_struct(sf, sf_addr, 0);
1993 force_sig(TARGET_SIGSEGV);
1996 long do_rt_sigreturn(CPUState *env)
1998 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1999 return -TARGET_ENOSYS;
2002 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2003 #define MC_TSTATE 0
2004 #define MC_PC 1
2005 #define MC_NPC 2
2006 #define MC_Y 3
2007 #define MC_G1 4
2008 #define MC_G2 5
2009 #define MC_G3 6
2010 #define MC_G4 7
2011 #define MC_G5 8
2012 #define MC_G6 9
2013 #define MC_G7 10
2014 #define MC_O0 11
2015 #define MC_O1 12
2016 #define MC_O2 13
2017 #define MC_O3 14
2018 #define MC_O4 15
2019 #define MC_O5 16
2020 #define MC_O6 17
2021 #define MC_O7 18
2022 #define MC_NGREG 19
2024 typedef abi_ulong target_mc_greg_t;
2025 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2027 struct target_mc_fq {
2028 abi_ulong *mcfq_addr;
2029 uint32_t mcfq_insn;
2032 struct target_mc_fpu {
2033 union {
2034 uint32_t sregs[32];
2035 uint64_t dregs[32];
2036 //uint128_t qregs[16];
2037 } mcfpu_fregs;
2038 abi_ulong mcfpu_fsr;
2039 abi_ulong mcfpu_fprs;
2040 abi_ulong mcfpu_gsr;
2041 struct target_mc_fq *mcfpu_fq;
2042 unsigned char mcfpu_qcnt;
2043 unsigned char mcfpu_qentsz;
2044 unsigned char mcfpu_enab;
2046 typedef struct target_mc_fpu target_mc_fpu_t;
2048 typedef struct {
2049 target_mc_gregset_t mc_gregs;
2050 target_mc_greg_t mc_fp;
2051 target_mc_greg_t mc_i7;
2052 target_mc_fpu_t mc_fpregs;
2053 } target_mcontext_t;
2055 struct target_ucontext {
2056 struct target_ucontext *uc_link;
2057 abi_ulong uc_flags;
2058 target_sigset_t uc_sigmask;
2059 target_mcontext_t uc_mcontext;
2062 /* A V9 register window */
2063 struct target_reg_window {
2064 abi_ulong locals[8];
2065 abi_ulong ins[8];
2068 #define TARGET_STACK_BIAS 2047
2070 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2071 void sparc64_set_context(CPUSPARCState *env)
2073 abi_ulong ucp_addr;
2074 struct target_ucontext *ucp;
2075 target_mc_gregset_t *grp;
2076 abi_ulong pc, npc, tstate;
2077 abi_ulong fp, i7, w_addr;
2078 unsigned char fenab;
2079 int err;
2080 unsigned int i;
2082 ucp_addr = env->regwptr[UREG_I0];
2083 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2084 goto do_sigsegv;
2085 grp = &ucp->uc_mcontext.mc_gregs;
2086 err = __get_user(pc, &((*grp)[MC_PC]));
2087 err |= __get_user(npc, &((*grp)[MC_NPC]));
2088 if (err || ((pc | npc) & 3))
2089 goto do_sigsegv;
2090 if (env->regwptr[UREG_I1]) {
2091 target_sigset_t target_set;
2092 sigset_t set;
2094 if (TARGET_NSIG_WORDS == 1) {
2095 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
2096 goto do_sigsegv;
2097 } else {
2098 abi_ulong *src, *dst;
2099 src = ucp->uc_sigmask.sig;
2100 dst = target_set.sig;
2101 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2102 i++, dst++, src++)
2103 err |= __get_user(*dst, src);
2104 if (err)
2105 goto do_sigsegv;
2107 target_to_host_sigset_internal(&set, &target_set);
2108 sigprocmask(SIG_SETMASK, &set, NULL);
2110 env->pc = pc;
2111 env->npc = npc;
2112 err |= __get_user(env->y, &((*grp)[MC_Y]));
2113 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2114 env->asi = (tstate >> 24) & 0xff;
2115 PUT_CCR(env, tstate >> 32);
2116 PUT_CWP64(env, tstate & 0x1f);
2117 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2118 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2119 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2120 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2121 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2122 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2123 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2124 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2125 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2126 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2127 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2128 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2129 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2130 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2131 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2133 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2134 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
2136 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2137 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2138 abi_ulong) != 0)
2139 goto do_sigsegv;
2140 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2141 abi_ulong) != 0)
2142 goto do_sigsegv;
2143 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2144 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
2146 uint32_t *src, *dst;
2147 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2148 dst = env->fpr;
2149 /* XXX: check that the CPU storage is the same as user context */
2150 for (i = 0; i < 64; i++, dst++, src++)
2151 err |= __get_user(*dst, src);
2153 err |= __get_user(env->fsr,
2154 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2155 err |= __get_user(env->gsr,
2156 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
2157 if (err)
2158 goto do_sigsegv;
2159 unlock_user_struct(ucp, ucp_addr, 0);
2160 return;
2161 do_sigsegv:
2162 unlock_user_struct(ucp, ucp_addr, 0);
2163 force_sig(SIGSEGV);
2166 void sparc64_get_context(CPUSPARCState *env)
2168 abi_ulong ucp_addr;
2169 struct target_ucontext *ucp;
2170 target_mc_gregset_t *grp;
2171 target_mcontext_t *mcp;
2172 abi_ulong fp, i7, w_addr;
2173 int err;
2174 unsigned int i;
2175 target_sigset_t target_set;
2176 sigset_t set;
2178 ucp_addr = env->regwptr[UREG_I0];
2179 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2180 goto do_sigsegv;
2182 mcp = &ucp->uc_mcontext;
2183 grp = &mcp->mc_gregs;
2185 /* Skip over the trap instruction, first. */
2186 env->pc = env->npc;
2187 env->npc += 4;
2189 err = 0;
2191 sigprocmask(0, NULL, &set);
2192 host_to_target_sigset_internal(&target_set, &set);
2193 if (TARGET_NSIG_WORDS == 1) {
2194 err |= __put_user(target_set.sig[0],
2195 (abi_ulong *)&ucp->uc_sigmask);
2196 } else {
2197 abi_ulong *src, *dst;
2198 src = target_set.sig;
2199 dst = ucp->uc_sigmask.sig;
2200 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2201 i++, dst++, src++)
2202 err |= __put_user(*src, dst);
2203 if (err)
2204 goto do_sigsegv;
2207 /* XXX: tstate must be saved properly */
2208 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2209 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2210 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2211 err |= __put_user(env->y, &((*grp)[MC_Y]));
2212 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2213 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2214 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2215 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2216 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2217 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2218 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2219 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2220 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2221 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2222 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2223 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2224 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2225 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2226 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2228 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2229 fp = i7 = 0;
2230 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2231 abi_ulong) != 0)
2232 goto do_sigsegv;
2233 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2234 abi_ulong) != 0)
2235 goto do_sigsegv;
2236 err |= __put_user(fp, &(mcp->mc_fp));
2237 err |= __put_user(i7, &(mcp->mc_i7));
2240 uint32_t *src, *dst;
2241 src = env->fpr;
2242 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2243 /* XXX: check that the CPU storage is the same as user context */
2244 for (i = 0; i < 64; i++, dst++, src++)
2245 err |= __put_user(*src, dst);
2247 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2248 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2249 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2251 if (err)
2252 goto do_sigsegv;
2253 unlock_user_struct(ucp, ucp_addr, 1);
2254 return;
2255 do_sigsegv:
2256 unlock_user_struct(ucp, ucp_addr, 1);
2257 force_sig(SIGSEGV);
2259 #endif
2260 #elif defined(TARGET_ABI_MIPSN64)
2262 # warning signal handling not implemented
2264 static void setup_frame(int sig, struct target_sigaction *ka,
2265 target_sigset_t *set, CPUState *env)
2267 fprintf(stderr, "setup_frame: not implemented\n");
2270 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2271 target_siginfo_t *info,
2272 target_sigset_t *set, CPUState *env)
2274 fprintf(stderr, "setup_rt_frame: not implemented\n");
2277 long do_sigreturn(CPUState *env)
2279 fprintf(stderr, "do_sigreturn: not implemented\n");
2280 return -TARGET_ENOSYS;
2283 long do_rt_sigreturn(CPUState *env)
2285 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2286 return -TARGET_ENOSYS;
2289 #elif defined(TARGET_ABI_MIPSN32)
2291 # warning signal handling not implemented
2293 static void setup_frame(int sig, struct target_sigaction *ka,
2294 target_sigset_t *set, CPUState *env)
2296 fprintf(stderr, "setup_frame: not implemented\n");
2299 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2300 target_siginfo_t *info,
2301 target_sigset_t *set, CPUState *env)
2303 fprintf(stderr, "setup_rt_frame: not implemented\n");
2306 long do_sigreturn(CPUState *env)
2308 fprintf(stderr, "do_sigreturn: not implemented\n");
2309 return -TARGET_ENOSYS;
2312 long do_rt_sigreturn(CPUState *env)
2314 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2315 return -TARGET_ENOSYS;
2318 #elif defined(TARGET_ABI_MIPSO32)
2320 struct target_sigcontext {
2321 uint32_t sc_regmask; /* Unused */
2322 uint32_t sc_status;
2323 uint64_t sc_pc;
2324 uint64_t sc_regs[32];
2325 uint64_t sc_fpregs[32];
2326 uint32_t sc_ownedfp; /* Unused */
2327 uint32_t sc_fpc_csr;
2328 uint32_t sc_fpc_eir; /* Unused */
2329 uint32_t sc_used_math;
2330 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2331 uint32_t pad0;
2332 uint64_t sc_mdhi;
2333 uint64_t sc_mdlo;
2334 target_ulong sc_hi1; /* Was sc_cause */
2335 target_ulong sc_lo1; /* Was sc_badvaddr */
2336 target_ulong sc_hi2; /* Was sc_sigset[4] */
2337 target_ulong sc_lo2;
2338 target_ulong sc_hi3;
2339 target_ulong sc_lo3;
2342 struct sigframe {
2343 uint32_t sf_ass[4]; /* argument save space for o32 */
2344 uint32_t sf_code[2]; /* signal trampoline */
2345 struct target_sigcontext sf_sc;
2346 target_sigset_t sf_mask;
2349 struct target_ucontext {
2350 target_ulong uc_flags;
2351 target_ulong uc_link;
2352 target_stack_t uc_stack;
2353 target_ulong pad0;
2354 struct target_sigcontext uc_mcontext;
2355 target_sigset_t uc_sigmask;
2358 struct target_rt_sigframe {
2359 uint32_t rs_ass[4]; /* argument save space for o32 */
2360 uint32_t rs_code[2]; /* signal trampoline */
2361 struct target_siginfo rs_info;
2362 struct target_ucontext rs_uc;
2365 /* Install trampoline to jump back from signal handler */
2366 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2368 int err;
2371 * Set up the return code ...
2373 * li v0, __NR__foo_sigreturn
2374 * syscall
2377 err = __put_user(0x24020000 + syscall, tramp + 0);
2378 err |= __put_user(0x0000000c , tramp + 1);
2379 /* flush_cache_sigtramp((unsigned long) tramp); */
2380 return err;
2383 static inline int
2384 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2386 int err = 0;
2388 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2390 #define save_gp_reg(i) do { \
2391 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2392 } while(0)
2393 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2394 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2395 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2396 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2397 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2398 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2399 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2400 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2401 save_gp_reg(31);
2402 #undef save_gp_reg
2404 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2405 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2407 /* Not used yet, but might be useful if we ever have DSP suppport */
2408 #if 0
2409 if (cpu_has_dsp) {
2410 err |= __put_user(mfhi1(), &sc->sc_hi1);
2411 err |= __put_user(mflo1(), &sc->sc_lo1);
2412 err |= __put_user(mfhi2(), &sc->sc_hi2);
2413 err |= __put_user(mflo2(), &sc->sc_lo2);
2414 err |= __put_user(mfhi3(), &sc->sc_hi3);
2415 err |= __put_user(mflo3(), &sc->sc_lo3);
2416 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2418 /* same with 64 bit */
2419 #ifdef CONFIG_64BIT
2420 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2421 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2422 if (cpu_has_dsp) {
2423 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2424 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2425 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2426 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2427 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2428 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2429 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2431 #endif
2432 #endif
2434 #if 0
2435 err |= __put_user(!!used_math(), &sc->sc_used_math);
2437 if (!used_math())
2438 goto out;
2441 * Save FPU state to signal context. Signal handler will "inherit"
2442 * current FPU state.
2444 preempt_disable();
2446 if (!is_fpu_owner()) {
2447 own_fpu();
2448 restore_fp(current);
2450 err |= save_fp_context(sc);
2452 preempt_enable();
2453 out:
2454 #endif
2455 return err;
2458 static inline int
2459 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2461 int err = 0;
2463 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2465 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2466 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2468 #define restore_gp_reg(i) do { \
2469 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2470 } while(0)
2471 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2472 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2473 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2474 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2475 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2476 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2477 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2478 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2479 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2480 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2481 restore_gp_reg(31);
2482 #undef restore_gp_reg
2484 #if 0
2485 if (cpu_has_dsp) {
2486 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2487 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2488 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2489 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2490 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2491 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2492 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2494 #ifdef CONFIG_64BIT
2495 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2496 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2497 if (cpu_has_dsp) {
2498 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2499 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2500 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2501 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2502 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2503 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2504 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2506 #endif
2508 err |= __get_user(used_math, &sc->sc_used_math);
2509 conditional_used_math(used_math);
2511 preempt_disable();
2513 if (used_math()) {
2514 /* restore fpu context if we have used it before */
2515 own_fpu();
2516 err |= restore_fp_context(sc);
2517 } else {
2518 /* signal handler may have used FPU. Give it up. */
2519 lose_fpu();
2522 preempt_enable();
2523 #endif
2524 return err;
2527 * Determine which stack to use..
2529 static inline abi_ulong
2530 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2532 unsigned long sp;
2534 /* Default to using normal stack */
2535 sp = regs->active_tc.gpr[29];
2538 * FPU emulator may have it's own trampoline active just
2539 * above the user stack, 16-bytes before the next lowest
2540 * 16 byte boundary. Try to avoid trashing it.
2542 sp -= 32;
2544 /* This is the X/Open sanctioned signal stack switching. */
2545 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2546 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2549 return (sp - frame_size) & ~7;
2552 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2553 static void setup_frame(int sig, struct target_sigaction * ka,
2554 target_sigset_t *set, CPUState *regs)
2556 struct sigframe *frame;
2557 abi_ulong frame_addr;
2558 int i;
2560 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2561 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2562 goto give_sigsegv;
2564 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2566 if(setup_sigcontext(regs, &frame->sf_sc))
2567 goto give_sigsegv;
2569 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2570 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2571 goto give_sigsegv;
2575 * Arguments to signal handler:
2577 * a0 = signal number
2578 * a1 = 0 (should be cause)
2579 * a2 = pointer to struct sigcontext
2581 * $25 and PC point to the signal handler, $29 points to the
2582 * struct sigframe.
2584 regs->active_tc.gpr[ 4] = sig;
2585 regs->active_tc.gpr[ 5] = 0;
2586 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2587 regs->active_tc.gpr[29] = frame_addr;
2588 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2589 /* The original kernel code sets CP0_EPC to the handler
2590 * since it returns to userland using eret
2591 * we cannot do this here, and we must set PC directly */
2592 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2593 unlock_user_struct(frame, frame_addr, 1);
2594 return;
2596 give_sigsegv:
2597 unlock_user_struct(frame, frame_addr, 1);
2598 force_sig(TARGET_SIGSEGV/*, current*/);
2599 return;
2602 long do_sigreturn(CPUState *regs)
2604 struct sigframe *frame;
2605 abi_ulong frame_addr;
2606 sigset_t blocked;
2607 target_sigset_t target_set;
2608 int i;
2610 #if defined(DEBUG_SIGNAL)
2611 fprintf(stderr, "do_sigreturn\n");
2612 #endif
2613 frame_addr = regs->active_tc.gpr[29];
2614 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2615 goto badframe;
2617 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2618 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2619 goto badframe;
2622 target_to_host_sigset_internal(&blocked, &target_set);
2623 sigprocmask(SIG_SETMASK, &blocked, NULL);
2625 if (restore_sigcontext(regs, &frame->sf_sc))
2626 goto badframe;
2628 #if 0
2630 * Don't let your children do this ...
2632 __asm__ __volatile__(
2633 "move\t$29, %0\n\t"
2634 "j\tsyscall_exit"
2635 :/* no outputs */
2636 :"r" (&regs));
2637 /* Unreached */
2638 #endif
2640 regs->active_tc.PC = regs->CP0_EPC;
2641 /* I am not sure this is right, but it seems to work
2642 * maybe a problem with nested signals ? */
2643 regs->CP0_EPC = 0;
2644 return -TARGET_QEMU_ESIGRETURN;
2646 badframe:
2647 force_sig(TARGET_SIGSEGV/*, current*/);
2648 return 0;
2651 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2652 target_siginfo_t *info,
2653 target_sigset_t *set, CPUState *env)
2655 struct target_rt_sigframe *frame;
2656 abi_ulong frame_addr;
2657 int i;
2659 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2660 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2661 goto give_sigsegv;
2663 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2665 copy_siginfo_to_user(&frame->rs_info, info);
2667 __put_user(0, &frame->rs_uc.uc_flags);
2668 __put_user(0, &frame->rs_uc.uc_link);
2669 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.uc_stack.ss_sp);
2670 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.uc_stack.ss_size);
2671 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2672 &frame->rs_uc.uc_stack.ss_flags);
2674 setup_sigcontext(env, &frame->rs_uc.uc_mcontext);
2676 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2677 __put_user(set->sig[i], &frame->rs_uc.uc_sigmask.sig[i]);
2681 * Arguments to signal handler:
2683 * a0 = signal number
2684 * a1 = pointer to struct siginfo
2685 * a2 = pointer to struct ucontext
2687 * $25 and PC point to the signal handler, $29 points to the
2688 * struct sigframe.
2690 env->active_tc.gpr[ 4] = sig;
2691 env->active_tc.gpr[ 5] = frame_addr
2692 + offsetof(struct target_rt_sigframe, rs_info);
2693 env->active_tc.gpr[ 6] = frame_addr
2694 + offsetof(struct target_rt_sigframe, rs_uc);
2695 env->active_tc.gpr[29] = frame_addr;
2696 env->active_tc.gpr[31] = frame_addr
2697 + offsetof(struct target_rt_sigframe, rs_code);
2698 /* The original kernel code sets CP0_EPC to the handler
2699 * since it returns to userland using eret
2700 * we cannot do this here, and we must set PC directly */
2701 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2702 unlock_user_struct(frame, frame_addr, 1);
2703 return;
2705 give_sigsegv:
2706 unlock_user_struct(frame, frame_addr, 1);
2707 force_sig(TARGET_SIGSEGV/*, current*/);
2708 return;
2711 long do_rt_sigreturn(CPUState *env)
2713 struct target_rt_sigframe *frame;
2714 abi_ulong frame_addr;
2715 sigset_t blocked;
2717 #if defined(DEBUG_SIGNAL)
2718 fprintf(stderr, "do_rt_sigreturn\n");
2719 #endif
2720 frame_addr = env->active_tc.gpr[29];
2721 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2722 goto badframe;
2724 target_to_host_sigset(&blocked, &frame->rs_uc.uc_sigmask);
2725 sigprocmask(SIG_SETMASK, &blocked, NULL);
2727 if (restore_sigcontext(env, &frame->rs_uc.uc_mcontext))
2728 goto badframe;
2730 if (do_sigaltstack(frame_addr +
2731 offsetof(struct target_rt_sigframe, rs_uc.uc_stack),
2732 0, get_sp_from_cpustate(env)) == -EFAULT)
2733 goto badframe;
2735 env->active_tc.PC = env->CP0_EPC;
2736 /* I am not sure this is right, but it seems to work
2737 * maybe a problem with nested signals ? */
2738 env->CP0_EPC = 0;
2739 return -TARGET_QEMU_ESIGRETURN;
2741 badframe:
2742 force_sig(TARGET_SIGSEGV/*, current*/);
2743 return 0;
2746 #elif defined(TARGET_SH4)
2749 * code and data structures from linux kernel:
2750 * include/asm-sh/sigcontext.h
2751 * arch/sh/kernel/signal.c
2754 struct target_sigcontext {
2755 target_ulong oldmask;
2757 /* CPU registers */
2758 target_ulong sc_gregs[16];
2759 target_ulong sc_pc;
2760 target_ulong sc_pr;
2761 target_ulong sc_sr;
2762 target_ulong sc_gbr;
2763 target_ulong sc_mach;
2764 target_ulong sc_macl;
2766 /* FPU registers */
2767 target_ulong sc_fpregs[16];
2768 target_ulong sc_xfpregs[16];
2769 unsigned int sc_fpscr;
2770 unsigned int sc_fpul;
2771 unsigned int sc_ownedfp;
2774 struct target_sigframe
2776 struct target_sigcontext sc;
2777 target_ulong extramask[TARGET_NSIG_WORDS-1];
2778 uint16_t retcode[3];
2782 struct target_ucontext {
2783 target_ulong uc_flags;
2784 struct target_ucontext *uc_link;
2785 target_stack_t uc_stack;
2786 struct target_sigcontext uc_mcontext;
2787 target_sigset_t uc_sigmask; /* mask last for extensibility */
2790 struct target_rt_sigframe
2792 struct target_siginfo info;
2793 struct target_ucontext uc;
2794 uint16_t retcode[3];
2798 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2799 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2801 static abi_ulong get_sigframe(struct target_sigaction *ka,
2802 unsigned long sp, size_t frame_size)
2804 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2805 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2808 return (sp - frame_size) & -8ul;
2811 static int setup_sigcontext(struct target_sigcontext *sc,
2812 CPUState *regs, unsigned long mask)
2814 int err = 0;
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 /* todo: save FPU registers here */
2832 /* non-iBCS2 extensions.. */
2833 err |= __put_user(mask, &sc->oldmask);
2835 return err;
2838 static int restore_sigcontext(CPUState *regs,
2839 struct target_sigcontext *sc)
2841 unsigned int err = 0;
2843 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2844 COPY(gregs[1]);
2845 COPY(gregs[2]); COPY(gregs[3]);
2846 COPY(gregs[4]); COPY(gregs[5]);
2847 COPY(gregs[6]); COPY(gregs[7]);
2848 COPY(gregs[8]); COPY(gregs[9]);
2849 COPY(gregs[10]); COPY(gregs[11]);
2850 COPY(gregs[12]); COPY(gregs[13]);
2851 COPY(gregs[14]); COPY(gregs[15]);
2852 COPY(gbr); COPY(mach);
2853 COPY(macl); COPY(pr);
2854 COPY(sr); COPY(pc);
2855 #undef COPY
2857 /* todo: restore FPU registers here */
2859 regs->tra = -1; /* disable syscall checks */
2860 return err;
2863 static void setup_frame(int sig, struct target_sigaction *ka,
2864 target_sigset_t *set, CPUState *regs)
2866 struct target_sigframe *frame;
2867 abi_ulong frame_addr;
2868 int i;
2869 int err = 0;
2870 int signal;
2872 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2873 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2874 goto give_sigsegv;
2876 signal = current_exec_domain_sig(sig);
2878 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2880 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2881 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2884 /* Set up to return from userspace. If provided, use a stub
2885 already in userspace. */
2886 if (ka->sa_flags & TARGET_SA_RESTORER) {
2887 regs->pr = (unsigned long) ka->sa_restorer;
2888 } else {
2889 /* Generate return code (system call to sigreturn) */
2890 err |= __put_user(MOVW(2), &frame->retcode[0]);
2891 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2892 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2893 regs->pr = (unsigned long) frame->retcode;
2896 if (err)
2897 goto give_sigsegv;
2899 /* Set up registers for signal handler */
2900 regs->gregs[15] = (unsigned long) frame;
2901 regs->gregs[4] = signal; /* Arg for signal handler */
2902 regs->gregs[5] = 0;
2903 regs->gregs[6] = (unsigned long) &frame->sc;
2904 regs->pc = (unsigned long) ka->_sa_handler;
2906 unlock_user_struct(frame, frame_addr, 1);
2907 return;
2909 give_sigsegv:
2910 unlock_user_struct(frame, frame_addr, 1);
2911 force_sig(SIGSEGV);
2914 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2915 target_siginfo_t *info,
2916 target_sigset_t *set, CPUState *regs)
2918 struct target_rt_sigframe *frame;
2919 abi_ulong frame_addr;
2920 int i;
2921 int err = 0;
2922 int signal;
2924 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2925 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2926 goto give_sigsegv;
2928 signal = current_exec_domain_sig(sig);
2930 err |= copy_siginfo_to_user(&frame->info, info);
2932 /* Create the ucontext. */
2933 err |= __put_user(0, &frame->uc.uc_flags);
2934 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2935 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
2936 &frame->uc.uc_stack.ss_sp);
2937 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2938 &frame->uc.uc_stack.ss_flags);
2939 err |= __put_user(target_sigaltstack_used.ss_size,
2940 &frame->uc.uc_stack.ss_size);
2941 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2942 regs, set->sig[0]);
2943 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2944 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2947 /* Set up to return from userspace. If provided, use a stub
2948 already in userspace. */
2949 if (ka->sa_flags & TARGET_SA_RESTORER) {
2950 regs->pr = (unsigned long) ka->sa_restorer;
2951 } else {
2952 /* Generate return code (system call to sigreturn) */
2953 err |= __put_user(MOVW(2), &frame->retcode[0]);
2954 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2955 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2956 regs->pr = (unsigned long) frame->retcode;
2959 if (err)
2960 goto give_sigsegv;
2962 /* Set up registers for signal handler */
2963 regs->gregs[15] = (unsigned long) frame;
2964 regs->gregs[4] = signal; /* Arg for signal handler */
2965 regs->gregs[5] = (unsigned long) &frame->info;
2966 regs->gregs[6] = (unsigned long) &frame->uc;
2967 regs->pc = (unsigned long) ka->_sa_handler;
2969 unlock_user_struct(frame, frame_addr, 1);
2970 return;
2972 give_sigsegv:
2973 unlock_user_struct(frame, frame_addr, 1);
2974 force_sig(SIGSEGV);
2977 long do_sigreturn(CPUState *regs)
2979 struct target_sigframe *frame;
2980 abi_ulong frame_addr;
2981 sigset_t blocked;
2982 target_sigset_t target_set;
2983 int i;
2984 int err = 0;
2986 #if defined(DEBUG_SIGNAL)
2987 fprintf(stderr, "do_sigreturn\n");
2988 #endif
2989 frame_addr = regs->gregs[15];
2990 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2991 goto badframe;
2993 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2994 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2995 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2998 if (err)
2999 goto badframe;
3001 target_to_host_sigset_internal(&blocked, &target_set);
3002 sigprocmask(SIG_SETMASK, &blocked, NULL);
3004 if (restore_sigcontext(regs, &frame->sc))
3005 goto badframe;
3007 unlock_user_struct(frame, frame_addr, 0);
3008 return regs->gregs[0];
3010 badframe:
3011 unlock_user_struct(frame, frame_addr, 0);
3012 force_sig(TARGET_SIGSEGV);
3013 return 0;
3016 long do_rt_sigreturn(CPUState *regs)
3018 struct target_rt_sigframe *frame;
3019 abi_ulong frame_addr;
3020 sigset_t blocked;
3022 #if defined(DEBUG_SIGNAL)
3023 fprintf(stderr, "do_rt_sigreturn\n");
3024 #endif
3025 frame_addr = regs->gregs[15];
3026 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3027 goto badframe;
3029 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
3030 sigprocmask(SIG_SETMASK, &blocked, NULL);
3032 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
3033 goto badframe;
3035 if (do_sigaltstack(frame_addr +
3036 offsetof(struct target_rt_sigframe, uc.uc_stack),
3037 0, get_sp_from_cpustate(regs)) == -EFAULT)
3038 goto badframe;
3040 unlock_user_struct(frame, frame_addr, 0);
3041 return regs->gregs[0];
3043 badframe:
3044 unlock_user_struct(frame, frame_addr, 0);
3045 force_sig(TARGET_SIGSEGV);
3046 return 0;
3048 #elif defined(TARGET_MICROBLAZE)
3050 struct target_sigcontext {
3051 struct target_pt_regs regs; /* needs to be first */
3052 uint32_t oldmask;
3055 /* Signal frames. */
3056 struct target_signal_frame {
3057 struct target_sigcontext sc;
3058 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3059 uint32_t tramp[2];
3062 struct rt_signal_frame {
3063 struct siginfo info;
3064 struct ucontext uc;
3065 uint32_t tramp[2];
3068 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3070 __put_user(env->regs[0], &sc->regs.r0);
3071 __put_user(env->regs[1], &sc->regs.r1);
3072 __put_user(env->regs[2], &sc->regs.r2);
3073 __put_user(env->regs[3], &sc->regs.r3);
3074 __put_user(env->regs[4], &sc->regs.r4);
3075 __put_user(env->regs[5], &sc->regs.r5);
3076 __put_user(env->regs[6], &sc->regs.r6);
3077 __put_user(env->regs[7], &sc->regs.r7);
3078 __put_user(env->regs[8], &sc->regs.r8);
3079 __put_user(env->regs[9], &sc->regs.r9);
3080 __put_user(env->regs[10], &sc->regs.r10);
3081 __put_user(env->regs[11], &sc->regs.r11);
3082 __put_user(env->regs[12], &sc->regs.r12);
3083 __put_user(env->regs[13], &sc->regs.r13);
3084 __put_user(env->regs[14], &sc->regs.r14);
3085 __put_user(env->regs[15], &sc->regs.r15);
3086 __put_user(env->regs[16], &sc->regs.r16);
3087 __put_user(env->regs[17], &sc->regs.r17);
3088 __put_user(env->regs[18], &sc->regs.r18);
3089 __put_user(env->regs[19], &sc->regs.r19);
3090 __put_user(env->regs[20], &sc->regs.r20);
3091 __put_user(env->regs[21], &sc->regs.r21);
3092 __put_user(env->regs[22], &sc->regs.r22);
3093 __put_user(env->regs[23], &sc->regs.r23);
3094 __put_user(env->regs[24], &sc->regs.r24);
3095 __put_user(env->regs[25], &sc->regs.r25);
3096 __put_user(env->regs[26], &sc->regs.r26);
3097 __put_user(env->regs[27], &sc->regs.r27);
3098 __put_user(env->regs[28], &sc->regs.r28);
3099 __put_user(env->regs[29], &sc->regs.r29);
3100 __put_user(env->regs[30], &sc->regs.r30);
3101 __put_user(env->regs[31], &sc->regs.r31);
3102 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3105 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3107 __get_user(env->regs[0], &sc->regs.r0);
3108 __get_user(env->regs[1], &sc->regs.r1);
3109 __get_user(env->regs[2], &sc->regs.r2);
3110 __get_user(env->regs[3], &sc->regs.r3);
3111 __get_user(env->regs[4], &sc->regs.r4);
3112 __get_user(env->regs[5], &sc->regs.r5);
3113 __get_user(env->regs[6], &sc->regs.r6);
3114 __get_user(env->regs[7], &sc->regs.r7);
3115 __get_user(env->regs[8], &sc->regs.r8);
3116 __get_user(env->regs[9], &sc->regs.r9);
3117 __get_user(env->regs[10], &sc->regs.r10);
3118 __get_user(env->regs[11], &sc->regs.r11);
3119 __get_user(env->regs[12], &sc->regs.r12);
3120 __get_user(env->regs[13], &sc->regs.r13);
3121 __get_user(env->regs[14], &sc->regs.r14);
3122 __get_user(env->regs[15], &sc->regs.r15);
3123 __get_user(env->regs[16], &sc->regs.r16);
3124 __get_user(env->regs[17], &sc->regs.r17);
3125 __get_user(env->regs[18], &sc->regs.r18);
3126 __get_user(env->regs[19], &sc->regs.r19);
3127 __get_user(env->regs[20], &sc->regs.r20);
3128 __get_user(env->regs[21], &sc->regs.r21);
3129 __get_user(env->regs[22], &sc->regs.r22);
3130 __get_user(env->regs[23], &sc->regs.r23);
3131 __get_user(env->regs[24], &sc->regs.r24);
3132 __get_user(env->regs[25], &sc->regs.r25);
3133 __get_user(env->regs[26], &sc->regs.r26);
3134 __get_user(env->regs[27], &sc->regs.r27);
3135 __get_user(env->regs[28], &sc->regs.r28);
3136 __get_user(env->regs[29], &sc->regs.r29);
3137 __get_user(env->regs[30], &sc->regs.r30);
3138 __get_user(env->regs[31], &sc->regs.r31);
3139 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3142 static abi_ulong get_sigframe(struct target_sigaction *ka,
3143 CPUState *env, int frame_size)
3145 abi_ulong sp = env->regs[1];
3147 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3148 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3150 return ((sp - frame_size) & -8UL);
3153 static void setup_frame(int sig, struct target_sigaction *ka,
3154 target_sigset_t *set, CPUState *env)
3156 struct target_signal_frame *frame;
3157 abi_ulong frame_addr;
3158 int err = 0;
3159 int i;
3161 frame_addr = get_sigframe(ka, env, sizeof *frame);
3162 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3163 goto badframe;
3165 /* Save the mask. */
3166 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3167 if (err)
3168 goto badframe;
3170 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3171 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3172 goto badframe;
3175 setup_sigcontext(&frame->sc, env);
3177 /* Set up to return from userspace. If provided, use a stub
3178 already in userspace. */
3179 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3180 if (ka->sa_flags & TARGET_SA_RESTORER) {
3181 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3182 } else {
3183 uint32_t t;
3184 /* Note, these encodings are _big endian_! */
3185 /* addi r12, r0, __NR_sigreturn */
3186 t = 0x31800000UL | TARGET_NR_sigreturn;
3187 err |= __put_user(t, frame->tramp + 0);
3188 /* brki r14, 0x8 */
3189 t = 0xb9cc0008UL;
3190 err |= __put_user(t, frame->tramp + 1);
3192 /* Return from sighandler will jump to the tramp.
3193 Negative 8 offset because return is rtsd r15, 8 */
3194 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3197 if (err)
3198 goto badframe;
3200 /* Set up registers for signal handler */
3201 env->regs[1] = (unsigned long) frame;
3202 /* Signal handler args: */
3203 env->regs[5] = sig; /* Arg 0: signum */
3204 env->regs[6] = (unsigned long) &frame->sc; /* arg 1: sigcontext */
3206 /* Offset of 4 to handle microblaze rtid r14, 0 */
3207 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3209 unlock_user_struct(frame, frame_addr, 1);
3210 return;
3211 badframe:
3212 unlock_user_struct(frame, frame_addr, 1);
3213 force_sig(TARGET_SIGSEGV);
3216 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3217 target_siginfo_t *info,
3218 target_sigset_t *set, CPUState *env)
3220 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3223 long do_sigreturn(CPUState *env)
3225 struct target_signal_frame *frame;
3226 abi_ulong frame_addr;
3227 target_sigset_t target_set;
3228 sigset_t set;
3229 int i;
3231 frame_addr = env->regs[R_SP];
3232 /* Make sure the guest isn't playing games. */
3233 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3234 goto badframe;
3236 /* Restore blocked signals */
3237 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3238 goto badframe;
3239 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3240 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3241 goto badframe;
3243 target_to_host_sigset_internal(&set, &target_set);
3244 sigprocmask(SIG_SETMASK, &set, NULL);
3246 restore_sigcontext(&frame->sc, env);
3247 /* We got here through a sigreturn syscall, our path back is via an
3248 rtb insn so setup r14 for that. */
3249 env->regs[14] = env->sregs[SR_PC];
3251 unlock_user_struct(frame, frame_addr, 0);
3252 return env->regs[10];
3253 badframe:
3254 unlock_user_struct(frame, frame_addr, 0);
3255 force_sig(TARGET_SIGSEGV);
3258 long do_rt_sigreturn(CPUState *env)
3260 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3261 return -TARGET_ENOSYS;
3264 #elif defined(TARGET_CRIS)
3266 struct target_sigcontext {
3267 struct target_pt_regs regs; /* needs to be first */
3268 uint32_t oldmask;
3269 uint32_t usp; /* usp before stacking this gunk on it */
3272 /* Signal frames. */
3273 struct target_signal_frame {
3274 struct target_sigcontext sc;
3275 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3276 uint8_t retcode[8]; /* Trampoline code. */
3279 struct rt_signal_frame {
3280 struct siginfo *pinfo;
3281 void *puc;
3282 struct siginfo info;
3283 struct ucontext uc;
3284 uint8_t retcode[8]; /* Trampoline code. */
3287 static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3289 __put_user(env->regs[0], &sc->regs.r0);
3290 __put_user(env->regs[1], &sc->regs.r1);
3291 __put_user(env->regs[2], &sc->regs.r2);
3292 __put_user(env->regs[3], &sc->regs.r3);
3293 __put_user(env->regs[4], &sc->regs.r4);
3294 __put_user(env->regs[5], &sc->regs.r5);
3295 __put_user(env->regs[6], &sc->regs.r6);
3296 __put_user(env->regs[7], &sc->regs.r7);
3297 __put_user(env->regs[8], &sc->regs.r8);
3298 __put_user(env->regs[9], &sc->regs.r9);
3299 __put_user(env->regs[10], &sc->regs.r10);
3300 __put_user(env->regs[11], &sc->regs.r11);
3301 __put_user(env->regs[12], &sc->regs.r12);
3302 __put_user(env->regs[13], &sc->regs.r13);
3303 __put_user(env->regs[14], &sc->usp);
3304 __put_user(env->regs[15], &sc->regs.acr);
3305 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3306 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3307 __put_user(env->pc, &sc->regs.erp);
3310 static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3312 __get_user(env->regs[0], &sc->regs.r0);
3313 __get_user(env->regs[1], &sc->regs.r1);
3314 __get_user(env->regs[2], &sc->regs.r2);
3315 __get_user(env->regs[3], &sc->regs.r3);
3316 __get_user(env->regs[4], &sc->regs.r4);
3317 __get_user(env->regs[5], &sc->regs.r5);
3318 __get_user(env->regs[6], &sc->regs.r6);
3319 __get_user(env->regs[7], &sc->regs.r7);
3320 __get_user(env->regs[8], &sc->regs.r8);
3321 __get_user(env->regs[9], &sc->regs.r9);
3322 __get_user(env->regs[10], &sc->regs.r10);
3323 __get_user(env->regs[11], &sc->regs.r11);
3324 __get_user(env->regs[12], &sc->regs.r12);
3325 __get_user(env->regs[13], &sc->regs.r13);
3326 __get_user(env->regs[14], &sc->usp);
3327 __get_user(env->regs[15], &sc->regs.acr);
3328 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3329 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3330 __get_user(env->pc, &sc->regs.erp);
3333 static abi_ulong get_sigframe(CPUState *env, int framesize)
3335 abi_ulong sp;
3336 /* Align the stack downwards to 4. */
3337 sp = (env->regs[R_SP] & ~3);
3338 return sp - framesize;
3341 static void setup_frame(int sig, struct target_sigaction *ka,
3342 target_sigset_t *set, CPUState *env)
3344 struct target_signal_frame *frame;
3345 abi_ulong frame_addr;
3346 int err = 0;
3347 int i;
3349 frame_addr = get_sigframe(env, sizeof *frame);
3350 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3351 goto badframe;
3354 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3355 * use this trampoline anymore but it sets it up for GDB.
3356 * In QEMU, using the trampoline simplifies things a bit so we use it.
3358 * This is movu.w __NR_sigreturn, r9; break 13;
3360 err |= __put_user(0x9c5f, frame->retcode+0);
3361 err |= __put_user(TARGET_NR_sigreturn,
3362 frame->retcode+2);
3363 err |= __put_user(0xe93d, frame->retcode+4);
3365 /* Save the mask. */
3366 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3367 if (err)
3368 goto badframe;
3370 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3371 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3372 goto badframe;
3375 setup_sigcontext(&frame->sc, env);
3377 /* Move the stack and setup the arguments for the handler. */
3378 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
3379 env->regs[10] = sig;
3380 env->pc = (unsigned long) ka->_sa_handler;
3381 /* Link SRP so the guest returns through the trampoline. */
3382 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
3384 unlock_user_struct(frame, frame_addr, 1);
3385 return;
3386 badframe:
3387 unlock_user_struct(frame, frame_addr, 1);
3388 force_sig(TARGET_SIGSEGV);
3391 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3392 target_siginfo_t *info,
3393 target_sigset_t *set, CPUState *env)
3395 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3398 long do_sigreturn(CPUState *env)
3400 struct target_signal_frame *frame;
3401 abi_ulong frame_addr;
3402 target_sigset_t target_set;
3403 sigset_t set;
3404 int i;
3406 frame_addr = env->regs[R_SP];
3407 /* Make sure the guest isn't playing games. */
3408 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3409 goto badframe;
3411 /* Restore blocked signals */
3412 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3413 goto badframe;
3414 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3415 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3416 goto badframe;
3418 target_to_host_sigset_internal(&set, &target_set);
3419 sigprocmask(SIG_SETMASK, &set, NULL);
3421 restore_sigcontext(&frame->sc, env);
3422 unlock_user_struct(frame, frame_addr, 0);
3423 return env->regs[10];
3424 badframe:
3425 unlock_user_struct(frame, frame_addr, 0);
3426 force_sig(TARGET_SIGSEGV);
3429 long do_rt_sigreturn(CPUState *env)
3431 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3432 return -TARGET_ENOSYS;
3435 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3437 /* FIXME: Many of the structures are defined for both PPC and PPC64, but
3438 the signal handling is different enough that we haven't implemented
3439 support for PPC64 yet. Hence the restriction above.
3441 There are various #if'd blocks for code for TARGET_PPC64. These
3442 blocks should go away so that we can successfully run 32-bit and
3443 64-bit binaries on a QEMU configured for PPC64. */
3445 /* Size of dummy stack frame allocated when calling signal handler.
3446 See arch/powerpc/include/asm/ptrace.h. */
3447 #if defined(TARGET_PPC64)
3448 #define SIGNAL_FRAMESIZE 128
3449 #else
3450 #define SIGNAL_FRAMESIZE 64
3451 #endif
3453 /* See arch/powerpc/include/asm/sigcontext.h. */
3454 struct target_sigcontext {
3455 target_ulong _unused[4];
3456 int32_t signal;
3457 #if defined(TARGET_PPC64)
3458 int32_t pad0;
3459 #endif
3460 target_ulong handler;
3461 target_ulong oldmask;
3462 target_ulong regs; /* struct pt_regs __user * */
3463 /* TODO: PPC64 includes extra bits here. */
3466 /* Indices for target_mcontext.mc_gregs, below.
3467 See arch/powerpc/include/asm/ptrace.h for details. */
3468 enum {
3469 TARGET_PT_R0 = 0,
3470 TARGET_PT_R1 = 1,
3471 TARGET_PT_R2 = 2,
3472 TARGET_PT_R3 = 3,
3473 TARGET_PT_R4 = 4,
3474 TARGET_PT_R5 = 5,
3475 TARGET_PT_R6 = 6,
3476 TARGET_PT_R7 = 7,
3477 TARGET_PT_R8 = 8,
3478 TARGET_PT_R9 = 9,
3479 TARGET_PT_R10 = 10,
3480 TARGET_PT_R11 = 11,
3481 TARGET_PT_R12 = 12,
3482 TARGET_PT_R13 = 13,
3483 TARGET_PT_R14 = 14,
3484 TARGET_PT_R15 = 15,
3485 TARGET_PT_R16 = 16,
3486 TARGET_PT_R17 = 17,
3487 TARGET_PT_R18 = 18,
3488 TARGET_PT_R19 = 19,
3489 TARGET_PT_R20 = 20,
3490 TARGET_PT_R21 = 21,
3491 TARGET_PT_R22 = 22,
3492 TARGET_PT_R23 = 23,
3493 TARGET_PT_R24 = 24,
3494 TARGET_PT_R25 = 25,
3495 TARGET_PT_R26 = 26,
3496 TARGET_PT_R27 = 27,
3497 TARGET_PT_R28 = 28,
3498 TARGET_PT_R29 = 29,
3499 TARGET_PT_R30 = 30,
3500 TARGET_PT_R31 = 31,
3501 TARGET_PT_NIP = 32,
3502 TARGET_PT_MSR = 33,
3503 TARGET_PT_ORIG_R3 = 34,
3504 TARGET_PT_CTR = 35,
3505 TARGET_PT_LNK = 36,
3506 TARGET_PT_XER = 37,
3507 TARGET_PT_CCR = 38,
3508 /* Yes, there are two registers with #39. One is 64-bit only. */
3509 TARGET_PT_MQ = 39,
3510 TARGET_PT_SOFTE = 39,
3511 TARGET_PT_TRAP = 40,
3512 TARGET_PT_DAR = 41,
3513 TARGET_PT_DSISR = 42,
3514 TARGET_PT_RESULT = 43,
3515 TARGET_PT_REGS_COUNT = 44
3518 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
3519 on 64-bit PPC, sigcontext and mcontext are one and the same. */
3520 struct target_mcontext {
3521 target_ulong mc_gregs[48];
3522 /* Includes fpscr. */
3523 uint64_t mc_fregs[33];
3524 target_ulong mc_pad[2];
3525 /* We need to handle Altivec and SPE at the same time, which no
3526 kernel needs to do. Fortunately, the kernel defines this bit to
3527 be Altivec-register-large all the time, rather than trying to
3528 twiddle it based on the specific platform. */
3529 union {
3530 /* SPE vector registers. One extra for SPEFSCR. */
3531 uint32_t spe[33];
3532 /* Altivec vector registers. The packing of VSCR and VRSAVE
3533 varies depending on whether we're PPC64 or not: PPC64 splits
3534 them apart; PPC32 stuffs them together. */
3535 #if defined(TARGET_PPC64)
3536 #define QEMU_NVRREG 34
3537 #else
3538 #define QEMU_NVRREG 33
3539 #endif
3540 ppc_avr_t altivec[QEMU_NVRREG];
3541 #undef QEMU_NVRREG
3542 } mc_vregs __attribute__((__aligned__(16)));
3545 struct target_ucontext {
3546 target_ulong uc_flags;
3547 target_ulong uc_link; /* struct ucontext __user * */
3548 struct target_sigaltstack uc_stack;
3549 #if !defined(TARGET_PPC64)
3550 int32_t uc_pad[7];
3551 target_ulong uc_regs; /* struct mcontext __user *
3552 points to uc_mcontext field */
3553 #endif
3554 target_sigset_t uc_sigmask;
3555 #if defined(TARGET_PPC64)
3556 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
3557 struct target_sigcontext uc_mcontext;
3558 #else
3559 int32_t uc_maskext[30];
3560 int32_t uc_pad2[3];
3561 struct target_mcontext uc_mcontext;
3562 #endif
3565 /* See arch/powerpc/kernel/signal_32.c. */
3566 struct target_sigframe {
3567 struct target_sigcontext sctx;
3568 struct target_mcontext mctx;
3569 int32_t abigap[56];
3572 struct target_rt_sigframe {
3573 struct target_siginfo info;
3574 struct target_ucontext uc;
3575 int32_t abigap[56];
3578 /* We use the mc_pad field for the signal return trampoline. */
3579 #define tramp mc_pad
3581 /* See arch/powerpc/kernel/signal.c. */
3582 static target_ulong get_sigframe(struct target_sigaction *ka,
3583 CPUState *env,
3584 int frame_size)
3586 target_ulong oldsp, newsp;
3588 oldsp = env->gpr[1];
3590 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
3591 (sas_ss_flags(oldsp))) {
3592 oldsp = (target_sigaltstack_used.ss_sp
3593 + target_sigaltstack_used.ss_size);
3596 newsp = (oldsp - frame_size) & ~0xFUL;
3598 return newsp;
3601 static int save_user_regs(CPUState *env, struct target_mcontext *frame,
3602 int sigret)
3604 target_ulong msr = env->msr;
3605 int i;
3606 target_ulong ccr = 0;
3608 /* In general, the kernel attempts to be intelligent about what it
3609 needs to save for Altivec/FP/SPE registers. We don't care that
3610 much, so we just go ahead and save everything. */
3612 /* Save general registers. */
3613 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3614 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
3615 return 1;
3618 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3619 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3620 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3621 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3622 return 1;
3624 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3625 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
3627 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3628 return 1;
3630 /* Save Altivec registers if necessary. */
3631 if (env->insns_flags & PPC_ALTIVEC) {
3632 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3633 ppc_avr_t *avr = &env->avr[i];
3634 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3636 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
3637 __put_user(avr->u64[1], &vreg->u64[1])) {
3638 return 1;
3641 /* Set MSR_VR in the saved MSR value to indicate that
3642 frame->mc_vregs contains valid data. */
3643 msr |= MSR_VR;
3644 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
3645 &frame->mc_vregs.altivec[32].u32[3]))
3646 return 1;
3649 /* Save floating point registers. */
3650 if (env->insns_flags & PPC_FLOAT) {
3651 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3652 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
3653 return 1;
3656 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
3657 return 1;
3660 /* Save SPE registers. The kernel only saves the high half. */
3661 if (env->insns_flags & PPC_SPE) {
3662 #if defined(TARGET_PPC64)
3663 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3664 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
3665 return 1;
3668 #else
3669 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3670 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3671 return 1;
3674 #endif
3675 /* Set MSR_SPE in the saved MSR value to indicate that
3676 frame->mc_vregs contains valid data. */
3677 msr |= MSR_SPE;
3678 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3679 return 1;
3682 /* Store MSR. */
3683 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3684 return 1;
3686 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
3687 if (sigret) {
3688 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
3689 __put_user(0x44000002UL, &frame->tramp[1])) {
3690 return 1;
3694 return 0;
3697 static int restore_user_regs(CPUState *env,
3698 struct target_mcontext *frame, int sig)
3700 target_ulong save_r2 = 0;
3701 target_ulong msr;
3702 target_ulong ccr;
3704 int i;
3706 if (!sig) {
3707 save_r2 = env->gpr[2];
3710 /* Restore general registers. */
3711 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3712 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
3713 return 1;
3716 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3717 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3718 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3719 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3720 return 1;
3721 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3722 return 1;
3724 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3725 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
3728 if (!sig) {
3729 env->gpr[2] = save_r2;
3731 /* Restore MSR. */
3732 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3733 return 1;
3735 /* If doing signal return, restore the previous little-endian mode. */
3736 if (sig)
3737 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
3739 /* Restore Altivec registers if necessary. */
3740 if (env->insns_flags & PPC_ALTIVEC) {
3741 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3742 ppc_avr_t *avr = &env->avr[i];
3743 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3745 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
3746 __get_user(avr->u64[1], &vreg->u64[1])) {
3747 return 1;
3750 /* Set MSR_VEC in the saved MSR value to indicate that
3751 frame->mc_vregs contains valid data. */
3752 if (__get_user(env->spr[SPR_VRSAVE],
3753 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
3754 return 1;
3757 /* Restore floating point registers. */
3758 if (env->insns_flags & PPC_FLOAT) {
3759 uint64_t fpscr;
3760 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3761 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
3762 return 1;
3765 if (__get_user(fpscr, &frame->mc_fregs[32]))
3766 return 1;
3767 env->fpscr = (uint32_t) fpscr;
3770 /* Save SPE registers. The kernel only saves the high half. */
3771 if (env->insns_flags & PPC_SPE) {
3772 #if defined(TARGET_PPC64)
3773 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3774 uint32_t hi;
3776 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
3777 return 1;
3779 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
3781 #else
3782 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3783 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3784 return 1;
3787 #endif
3788 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3789 return 1;
3792 return 0;
3795 static void setup_frame(int sig, struct target_sigaction *ka,
3796 target_sigset_t *set, CPUState *env)
3798 struct target_sigframe *frame;
3799 struct target_sigcontext *sc;
3800 target_ulong frame_addr, newsp;
3801 int err = 0;
3802 int signal;
3804 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3805 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3806 goto sigsegv;
3807 sc = &frame->sctx;
3809 signal = current_exec_domain_sig(sig);
3811 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
3812 err |= __put_user(set->sig[0], &sc->oldmask);
3813 #if defined(TARGET_PPC64)
3814 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
3815 #else
3816 err |= __put_user(set->sig[1], &sc->_unused[3]);
3817 #endif
3818 err |= __put_user(h2g(&frame->mctx), &sc->regs);
3819 err |= __put_user(sig, &sc->signal);
3821 /* Save user regs. */
3822 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
3824 /* The kernel checks for the presence of a VDSO here. We don't
3825 emulate a vdso, so use a sigreturn system call. */
3826 env->lr = (target_ulong) h2g(frame->mctx.tramp);
3828 /* Turn off all fp exceptions. */
3829 env->fpscr = 0;
3831 /* Create a stack frame for the caller of the handler. */
3832 newsp = frame_addr - SIGNAL_FRAMESIZE;
3833 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3835 if (err)
3836 goto sigsegv;
3838 /* Set up registers for signal handler. */
3839 env->gpr[1] = newsp;
3840 env->gpr[3] = signal;
3841 env->gpr[4] = (target_ulong) h2g(sc);
3842 env->nip = (target_ulong) ka->_sa_handler;
3843 /* Signal handlers are entered in big-endian mode. */
3844 env->msr &= ~MSR_LE;
3846 unlock_user_struct(frame, frame_addr, 1);
3847 return;
3849 sigsegv:
3850 unlock_user_struct(frame, frame_addr, 1);
3851 if (logfile)
3852 fprintf (logfile, "segfaulting from setup_frame\n");
3853 force_sig(SIGSEGV);
3856 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3857 target_siginfo_t *info,
3858 target_sigset_t *set, CPUState *env)
3860 struct target_rt_sigframe *rt_sf;
3861 struct target_mcontext *frame;
3862 target_ulong rt_sf_addr, newsp = 0;
3863 int i, err = 0;
3864 int signal;
3866 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
3867 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
3868 goto sigsegv;
3870 signal = current_exec_domain_sig(sig);
3872 err |= copy_siginfo_to_user(&rt_sf->info, info);
3874 err |= __put_user(0, &rt_sf->uc.uc_flags);
3875 err |= __put_user(0, &rt_sf->uc.uc_link);
3876 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
3877 &rt_sf->uc.uc_stack.ss_sp);
3878 err |= __put_user(sas_ss_flags(env->gpr[1]),
3879 &rt_sf->uc.uc_stack.ss_flags);
3880 err |= __put_user(target_sigaltstack_used.ss_size,
3881 &rt_sf->uc.uc_stack.ss_size);
3882 err |= __put_user(h2g (&rt_sf->uc.uc_mcontext),
3883 &rt_sf->uc.uc_regs);
3884 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3885 err |= __put_user(set->sig[i], &rt_sf->uc.uc_sigmask.sig[i]);
3888 frame = &rt_sf->uc.uc_mcontext;
3889 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
3891 /* The kernel checks for the presence of a VDSO here. We don't
3892 emulate a vdso, so use a sigreturn system call. */
3893 env->lr = (target_ulong) h2g(frame->tramp);
3895 /* Turn off all fp exceptions. */
3896 env->fpscr = 0;
3898 /* Create a stack frame for the caller of the handler. */
3899 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
3900 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3902 if (err)
3903 goto sigsegv;
3905 /* Set up registers for signal handler. */
3906 env->gpr[1] = newsp;
3907 env->gpr[3] = (target_ulong) signal;
3908 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
3909 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
3910 env->gpr[6] = (target_ulong) h2g(rt_sf);
3911 env->nip = (target_ulong) ka->_sa_handler;
3912 /* Signal handlers are entered in big-endian mode. */
3913 env->msr &= ~MSR_LE;
3915 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3916 return;
3918 sigsegv:
3919 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3920 if (logfile)
3921 fprintf (logfile, "segfaulting from setup_rt_frame\n");
3922 force_sig(SIGSEGV);
3926 long do_sigreturn(CPUState *env)
3928 struct target_sigcontext *sc = NULL;
3929 struct target_mcontext *sr = NULL;
3930 target_ulong sr_addr, sc_addr;
3931 sigset_t blocked;
3932 target_sigset_t set;
3934 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
3935 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
3936 goto sigsegv;
3938 #if defined(TARGET_PPC64)
3939 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
3940 #else
3941 if(__get_user(set.sig[0], &sc->oldmask) ||
3942 __get_user(set.sig[1], &sc->_unused[3]))
3943 goto sigsegv;
3944 #endif
3945 target_to_host_sigset_internal(&blocked, &set);
3946 sigprocmask(SIG_SETMASK, &blocked, NULL);
3948 if (__get_user(sr_addr, &sc->regs))
3949 goto sigsegv;
3950 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
3951 goto sigsegv;
3952 if (restore_user_regs(env, sr, 1))
3953 goto sigsegv;
3955 unlock_user_struct(sr, sr_addr, 1);
3956 unlock_user_struct(sc, sc_addr, 1);
3957 return -TARGET_QEMU_ESIGRETURN;
3959 sigsegv:
3960 unlock_user_struct(sr, sr_addr, 1);
3961 unlock_user_struct(sc, sc_addr, 1);
3962 if (logfile)
3963 fprintf (logfile, "segfaulting from do_sigreturn\n");
3964 force_sig(SIGSEGV);
3965 return 0;
3968 /* See arch/powerpc/kernel/signal_32.c. */
3969 static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
3971 struct target_mcontext *mcp;
3972 target_ulong mcp_addr;
3973 sigset_t blocked;
3974 target_sigset_t set;
3976 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, uc_sigmask),
3977 sizeof (set)))
3978 return 1;
3980 #if defined(TARGET_PPC64)
3981 fprintf (stderr, "do_setcontext: not implemented\n");
3982 return 0;
3983 #else
3984 if (__get_user(mcp_addr, &ucp->uc_regs))
3985 return 1;
3987 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
3988 return 1;
3990 target_to_host_sigset_internal(&blocked, &set);
3991 sigprocmask(SIG_SETMASK, &blocked, NULL);
3992 if (restore_user_regs(env, mcp, sig))
3993 goto sigsegv;
3995 unlock_user_struct(mcp, mcp_addr, 1);
3996 return 0;
3998 sigsegv:
3999 unlock_user_struct(mcp, mcp_addr, 1);
4000 return 1;
4001 #endif
4004 long do_rt_sigreturn(CPUState *env)
4006 struct target_rt_sigframe *rt_sf = NULL;
4007 target_ulong rt_sf_addr;
4009 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4010 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4011 goto sigsegv;
4013 if (do_setcontext(&rt_sf->uc, env, 1))
4014 goto sigsegv;
4016 do_sigaltstack(rt_sf_addr
4017 + offsetof(struct target_rt_sigframe, uc.uc_stack),
4018 0, env->gpr[1]);
4020 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4021 return -TARGET_QEMU_ESIGRETURN;
4023 sigsegv:
4024 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4025 if (logfile)
4026 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
4027 force_sig(SIGSEGV);
4028 return 0;
4031 #elif defined(TARGET_M68K)
4033 struct target_sigcontext {
4034 abi_ulong sc_mask;
4035 abi_ulong sc_usp;
4036 abi_ulong sc_d0;
4037 abi_ulong sc_d1;
4038 abi_ulong sc_a0;
4039 abi_ulong sc_a1;
4040 unsigned short sc_sr;
4041 abi_ulong sc_pc;
4044 struct target_sigframe
4046 abi_ulong pretcode;
4047 int sig;
4048 int code;
4049 abi_ulong psc;
4050 char retcode[8];
4051 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4052 struct target_sigcontext sc;
4055 typedef int target_greg_t;
4056 #define TARGET_NGREG 18
4057 typedef target_greg_t target_gregset_t[TARGET_NGREG];
4059 typedef struct target_fpregset {
4060 int f_fpcntl[3];
4061 int f_fpregs[8*3];
4062 } target_fpregset_t;
4064 struct target_mcontext {
4065 int version;
4066 target_gregset_t gregs;
4067 target_fpregset_t fpregs;
4070 #define TARGET_MCONTEXT_VERSION 2
4072 struct target_ucontext {
4073 abi_ulong uc_flags;
4074 abi_ulong uc_link;
4075 target_stack_t uc_stack;
4076 struct target_mcontext uc_mcontext;
4077 abi_long uc_filler[80];
4078 target_sigset_t uc_sigmask;
4081 struct target_rt_sigframe
4083 abi_ulong pretcode;
4084 int sig;
4085 abi_ulong pinfo;
4086 abi_ulong puc;
4087 char retcode[8];
4088 struct target_siginfo info;
4089 struct target_ucontext uc;
4092 static int
4093 setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4095 int err = 0;
4097 err |= __put_user(mask, &sc->sc_mask);
4098 err |= __put_user(env->aregs[7], &sc->sc_usp);
4099 err |= __put_user(env->dregs[0], &sc->sc_d0);
4100 err |= __put_user(env->dregs[1], &sc->sc_d1);
4101 err |= __put_user(env->aregs[0], &sc->sc_a0);
4102 err |= __put_user(env->aregs[1], &sc->sc_a1);
4103 err |= __put_user(env->sr, &sc->sc_sr);
4104 err |= __put_user(env->pc, &sc->sc_pc);
4106 return err;
4109 static int
4110 restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4112 int err = 0;
4113 int temp;
4115 err |= __get_user(env->aregs[7], &sc->sc_usp);
4116 err |= __get_user(env->dregs[1], &sc->sc_d1);
4117 err |= __get_user(env->aregs[0], &sc->sc_a0);
4118 err |= __get_user(env->aregs[1], &sc->sc_a1);
4119 err |= __get_user(env->pc, &sc->sc_pc);
4120 err |= __get_user(temp, &sc->sc_sr);
4121 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4123 *pd0 = tswapl(sc->sc_d0);
4125 return err;
4129 * Determine which stack to use..
4131 static inline abi_ulong
4132 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4134 unsigned long sp;
4136 sp = regs->aregs[7];
4138 /* This is the X/Open sanctioned signal stack switching. */
4139 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4140 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4143 return ((sp - frame_size) & -8UL);
4146 static void setup_frame(int sig, struct target_sigaction *ka,
4147 target_sigset_t *set, CPUState *env)
4149 struct target_sigframe *frame;
4150 abi_ulong frame_addr;
4151 abi_ulong retcode_addr;
4152 abi_ulong sc_addr;
4153 int err = 0;
4154 int i;
4156 frame_addr = get_sigframe(ka, env, sizeof *frame);
4157 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4158 goto give_sigsegv;
4160 err |= __put_user(sig, &frame->sig);
4162 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4163 err |= __put_user(sc_addr, &frame->psc);
4165 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4166 if (err)
4167 goto give_sigsegv;
4169 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4170 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4171 goto give_sigsegv;
4174 /* Set up to return from userspace. */
4176 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4177 err |= __put_user(retcode_addr, &frame->pretcode);
4179 /* moveq #,d0; trap #0 */
4181 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4182 (long *)(frame->retcode));
4184 if (err)
4185 goto give_sigsegv;
4187 /* Set up to return from userspace */
4189 env->aregs[7] = frame_addr;
4190 env->pc = ka->_sa_handler;
4192 unlock_user_struct(frame, frame_addr, 1);
4193 return;
4195 give_sigsegv:
4196 unlock_user_struct(frame, frame_addr, 1);
4197 force_sig(SIGSEGV);
4200 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4201 CPUState *env)
4203 target_greg_t *gregs = uc->uc_mcontext.gregs;
4204 int err;
4206 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->uc_mcontext.version);
4207 err |= __put_user(env->dregs[0], &gregs[0]);
4208 err |= __put_user(env->dregs[1], &gregs[1]);
4209 err |= __put_user(env->dregs[2], &gregs[2]);
4210 err |= __put_user(env->dregs[3], &gregs[3]);
4211 err |= __put_user(env->dregs[4], &gregs[4]);
4212 err |= __put_user(env->dregs[5], &gregs[5]);
4213 err |= __put_user(env->dregs[6], &gregs[6]);
4214 err |= __put_user(env->dregs[7], &gregs[7]);
4215 err |= __put_user(env->aregs[0], &gregs[8]);
4216 err |= __put_user(env->aregs[1], &gregs[9]);
4217 err |= __put_user(env->aregs[2], &gregs[10]);
4218 err |= __put_user(env->aregs[3], &gregs[11]);
4219 err |= __put_user(env->aregs[4], &gregs[12]);
4220 err |= __put_user(env->aregs[5], &gregs[13]);
4221 err |= __put_user(env->aregs[6], &gregs[14]);
4222 err |= __put_user(env->aregs[7], &gregs[15]);
4223 err |= __put_user(env->pc, &gregs[16]);
4224 err |= __put_user(env->sr, &gregs[17]);
4226 return err;
4229 static inline int target_rt_restore_ucontext(CPUState *env,
4230 struct target_ucontext *uc,
4231 int *pd0)
4233 int temp;
4234 int err;
4235 target_greg_t *gregs = uc->uc_mcontext.gregs;
4237 err = __get_user(temp, &uc->uc_mcontext.version);
4238 if (temp != TARGET_MCONTEXT_VERSION)
4239 goto badframe;
4241 /* restore passed registers */
4242 err |= __get_user(env->dregs[0], &gregs[0]);
4243 err |= __get_user(env->dregs[1], &gregs[1]);
4244 err |= __get_user(env->dregs[2], &gregs[2]);
4245 err |= __get_user(env->dregs[3], &gregs[3]);
4246 err |= __get_user(env->dregs[4], &gregs[4]);
4247 err |= __get_user(env->dregs[5], &gregs[5]);
4248 err |= __get_user(env->dregs[6], &gregs[6]);
4249 err |= __get_user(env->dregs[7], &gregs[7]);
4250 err |= __get_user(env->aregs[0], &gregs[8]);
4251 err |= __get_user(env->aregs[1], &gregs[9]);
4252 err |= __get_user(env->aregs[2], &gregs[10]);
4253 err |= __get_user(env->aregs[3], &gregs[11]);
4254 err |= __get_user(env->aregs[4], &gregs[12]);
4255 err |= __get_user(env->aregs[5], &gregs[13]);
4256 err |= __get_user(env->aregs[6], &gregs[14]);
4257 err |= __get_user(env->aregs[7], &gregs[15]);
4258 err |= __get_user(env->pc, &gregs[16]);
4259 err |= __get_user(temp, &gregs[17]);
4260 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4262 *pd0 = env->dregs[0];
4263 return err;
4265 badframe:
4266 return 1;
4269 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4270 target_siginfo_t *info,
4271 target_sigset_t *set, CPUState *env)
4273 struct target_rt_sigframe *frame;
4274 abi_ulong frame_addr;
4275 abi_ulong retcode_addr;
4276 abi_ulong info_addr;
4277 abi_ulong uc_addr;
4278 int err = 0;
4279 int i;
4281 frame_addr = get_sigframe(ka, env, sizeof *frame);
4282 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4283 goto give_sigsegv;
4285 err |= __put_user(sig, &frame->sig);
4287 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4288 err |= __put_user(info_addr, &frame->pinfo);
4290 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4291 err |= __put_user(uc_addr, &frame->puc);
4293 err |= copy_siginfo_to_user(&frame->info, info);
4295 /* Create the ucontext */
4297 err |= __put_user(0, &frame->uc.uc_flags);
4298 err |= __put_user(0, &frame->uc.uc_link);
4299 err |= __put_user(target_sigaltstack_used.ss_sp,
4300 &frame->uc.uc_stack.ss_sp);
4301 err |= __put_user(sas_ss_flags(env->aregs[7]),
4302 &frame->uc.uc_stack.ss_flags);
4303 err |= __put_user(target_sigaltstack_used.ss_size,
4304 &frame->uc.uc_stack.ss_size);
4305 err |= target_rt_setup_ucontext(&frame->uc, env);
4307 if (err)
4308 goto give_sigsegv;
4310 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4311 if (__put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]))
4312 goto give_sigsegv;
4315 /* Set up to return from userspace. */
4317 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4318 err |= __put_user(retcode_addr, &frame->pretcode);
4320 /* moveq #,d0; notb d0; trap #0 */
4322 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4323 (long *)(frame->retcode + 0));
4324 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4326 if (err)
4327 goto give_sigsegv;
4329 /* Set up to return from userspace */
4331 env->aregs[7] = frame_addr;
4332 env->pc = ka->_sa_handler;
4334 unlock_user_struct(frame, frame_addr, 1);
4335 return;
4337 give_sigsegv:
4338 unlock_user_struct(frame, frame_addr, 1);
4339 force_sig(SIGSEGV);
4342 long do_sigreturn(CPUState *env)
4344 struct target_sigframe *frame;
4345 abi_ulong frame_addr = env->aregs[7] - 4;
4346 target_sigset_t target_set;
4347 sigset_t set;
4348 int d0, i;
4350 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4351 goto badframe;
4353 /* set blocked signals */
4355 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4356 goto badframe;
4358 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4359 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4360 goto badframe;
4363 target_to_host_sigset_internal(&set, &target_set);
4364 sigprocmask(SIG_SETMASK, &set, NULL);
4366 /* restore registers */
4368 if (restore_sigcontext(env, &frame->sc, &d0))
4369 goto badframe;
4371 unlock_user_struct(frame, frame_addr, 0);
4372 return d0;
4374 badframe:
4375 unlock_user_struct(frame, frame_addr, 0);
4376 force_sig(TARGET_SIGSEGV);
4377 return 0;
4380 long do_rt_sigreturn(CPUState *env)
4382 struct target_rt_sigframe *frame;
4383 abi_ulong frame_addr = env->aregs[7] - 4;
4384 target_sigset_t target_set;
4385 sigset_t set;
4386 int d0;
4388 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4389 goto badframe;
4391 target_to_host_sigset_internal(&set, &target_set);
4392 sigprocmask(SIG_SETMASK, &set, NULL);
4394 /* restore registers */
4396 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4397 goto badframe;
4399 if (do_sigaltstack(frame_addr +
4400 offsetof(struct target_rt_sigframe, uc.uc_stack),
4401 0, get_sp_from_cpustate(env)) == -EFAULT)
4402 goto badframe;
4404 unlock_user_struct(frame, frame_addr, 0);
4405 return d0;
4407 badframe:
4408 unlock_user_struct(frame, frame_addr, 0);
4409 force_sig(TARGET_SIGSEGV);
4410 return 0;
4413 #else
4415 static void setup_frame(int sig, struct target_sigaction *ka,
4416 target_sigset_t *set, CPUState *env)
4418 fprintf(stderr, "setup_frame: not implemented\n");
4421 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4422 target_siginfo_t *info,
4423 target_sigset_t *set, CPUState *env)
4425 fprintf(stderr, "setup_rt_frame: not implemented\n");
4428 long do_sigreturn(CPUState *env)
4430 fprintf(stderr, "do_sigreturn: not implemented\n");
4431 return -TARGET_ENOSYS;
4434 long do_rt_sigreturn(CPUState *env)
4436 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
4437 return -TARGET_ENOSYS;
4440 #endif
4442 void process_pending_signals(CPUState *cpu_env)
4444 int sig;
4445 abi_ulong handler;
4446 sigset_t set, old_set;
4447 target_sigset_t target_old_set;
4448 struct emulated_sigtable *k;
4449 struct target_sigaction *sa;
4450 struct sigqueue *q;
4451 TaskState *ts = cpu_env->opaque;
4453 if (!ts->signal_pending)
4454 return;
4456 /* FIXME: This is not threadsafe. */
4457 k = ts->sigtab;
4458 for(sig = 1; sig <= TARGET_NSIG; sig++) {
4459 if (k->pending)
4460 goto handle_signal;
4461 k++;
4463 /* if no signal is pending, just return */
4464 ts->signal_pending = 0;
4465 return;
4467 handle_signal:
4468 #ifdef DEBUG_SIGNAL
4469 fprintf(stderr, "qemu: process signal %d\n", sig);
4470 #endif
4471 /* dequeue signal */
4472 q = k->first;
4473 k->first = q->next;
4474 if (!k->first)
4475 k->pending = 0;
4477 sig = gdb_handlesig (cpu_env, sig);
4478 if (!sig) {
4479 sa = NULL;
4480 handler = TARGET_SIG_IGN;
4481 } else {
4482 sa = &sigact_table[sig - 1];
4483 handler = sa->_sa_handler;
4486 if (handler == TARGET_SIG_DFL) {
4487 /* default handler : ignore some signal. The other are job control or fatal */
4488 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
4489 kill(getpid(),SIGSTOP);
4490 } else if (sig != TARGET_SIGCHLD &&
4491 sig != TARGET_SIGURG &&
4492 sig != TARGET_SIGWINCH &&
4493 sig != TARGET_SIGCONT) {
4494 force_sig(sig);
4496 } else if (handler == TARGET_SIG_IGN) {
4497 /* ignore sig */
4498 } else if (handler == TARGET_SIG_ERR) {
4499 force_sig(sig);
4500 } else {
4501 /* compute the blocked signals during the handler execution */
4502 target_to_host_sigset(&set, &sa->sa_mask);
4503 /* SA_NODEFER indicates that the current signal should not be
4504 blocked during the handler */
4505 if (!(sa->sa_flags & TARGET_SA_NODEFER))
4506 sigaddset(&set, target_to_host_signal(sig));
4508 /* block signals in the handler using Linux */
4509 sigprocmask(SIG_BLOCK, &set, &old_set);
4510 /* save the previous blocked signal state to restore it at the
4511 end of the signal execution (see do_sigreturn) */
4512 host_to_target_sigset_internal(&target_old_set, &old_set);
4514 /* if the CPU is in VM86 mode, we restore the 32 bit values */
4515 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
4517 CPUX86State *env = cpu_env;
4518 if (env->eflags & VM_MASK)
4519 save_v86_state(env);
4521 #endif
4522 /* prepare the stack frame of the virtual CPU */
4523 if (sa->sa_flags & TARGET_SA_SIGINFO)
4524 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
4525 else
4526 setup_frame(sig, sa, &target_old_set, cpu_env);
4527 if (sa->sa_flags & TARGET_SA_RESETHAND)
4528 sa->_sa_handler = TARGET_SIG_DFL;
4530 if (q != &k->info)
4531 free_sigqueue(cpu_env, q);