qemu-iotests/141: Avoid blockdev-add with id
[qemu.git] / linux-user / signal.c
blobe4eea697b44097fc0f721bbd6df413adb4225ca1
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 "qemu/osdep.h"
20 #include "qemu/bitops.h"
21 #include <sys/ucontext.h>
22 #include <sys/resource.h>
24 #include "qemu.h"
25 #include "qemu-common.h"
26 #include "target_signal.h"
27 #include "trace.h"
29 static struct target_sigaltstack target_sigaltstack_used = {
30 .ss_sp = 0,
31 .ss_size = 0,
32 .ss_flags = TARGET_SS_DISABLE,
35 static struct target_sigaction sigact_table[TARGET_NSIG];
37 static void host_signal_handler(int host_signum, siginfo_t *info,
38 void *puc);
40 static uint8_t host_to_target_signal_table[_NSIG] = {
41 [SIGHUP] = TARGET_SIGHUP,
42 [SIGINT] = TARGET_SIGINT,
43 [SIGQUIT] = TARGET_SIGQUIT,
44 [SIGILL] = TARGET_SIGILL,
45 [SIGTRAP] = TARGET_SIGTRAP,
46 [SIGABRT] = TARGET_SIGABRT,
47 /* [SIGIOT] = TARGET_SIGIOT,*/
48 [SIGBUS] = TARGET_SIGBUS,
49 [SIGFPE] = TARGET_SIGFPE,
50 [SIGKILL] = TARGET_SIGKILL,
51 [SIGUSR1] = TARGET_SIGUSR1,
52 [SIGSEGV] = TARGET_SIGSEGV,
53 [SIGUSR2] = TARGET_SIGUSR2,
54 [SIGPIPE] = TARGET_SIGPIPE,
55 [SIGALRM] = TARGET_SIGALRM,
56 [SIGTERM] = TARGET_SIGTERM,
57 #ifdef SIGSTKFLT
58 [SIGSTKFLT] = TARGET_SIGSTKFLT,
59 #endif
60 [SIGCHLD] = TARGET_SIGCHLD,
61 [SIGCONT] = TARGET_SIGCONT,
62 [SIGSTOP] = TARGET_SIGSTOP,
63 [SIGTSTP] = TARGET_SIGTSTP,
64 [SIGTTIN] = TARGET_SIGTTIN,
65 [SIGTTOU] = TARGET_SIGTTOU,
66 [SIGURG] = TARGET_SIGURG,
67 [SIGXCPU] = TARGET_SIGXCPU,
68 [SIGXFSZ] = TARGET_SIGXFSZ,
69 [SIGVTALRM] = TARGET_SIGVTALRM,
70 [SIGPROF] = TARGET_SIGPROF,
71 [SIGWINCH] = TARGET_SIGWINCH,
72 [SIGIO] = TARGET_SIGIO,
73 [SIGPWR] = TARGET_SIGPWR,
74 [SIGSYS] = TARGET_SIGSYS,
75 /* next signals stay the same */
76 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
77 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
78 To fix this properly we need to do manual signal delivery multiplexed
79 over a single host signal. */
80 [__SIGRTMIN] = __SIGRTMAX,
81 [__SIGRTMAX] = __SIGRTMIN,
83 static uint8_t target_to_host_signal_table[_NSIG];
85 static inline int on_sig_stack(unsigned long sp)
87 return (sp - target_sigaltstack_used.ss_sp
88 < target_sigaltstack_used.ss_size);
91 static inline int sas_ss_flags(unsigned long sp)
93 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
94 : on_sig_stack(sp) ? SS_ONSTACK : 0);
97 int host_to_target_signal(int sig)
99 if (sig < 0 || sig >= _NSIG)
100 return sig;
101 return host_to_target_signal_table[sig];
104 int target_to_host_signal(int sig)
106 if (sig < 0 || sig >= _NSIG)
107 return sig;
108 return target_to_host_signal_table[sig];
111 static inline void target_sigemptyset(target_sigset_t *set)
113 memset(set, 0, sizeof(*set));
116 static inline void target_sigaddset(target_sigset_t *set, int signum)
118 signum--;
119 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
120 set->sig[signum / TARGET_NSIG_BPW] |= mask;
123 static inline int target_sigismember(const target_sigset_t *set, int signum)
125 signum--;
126 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
127 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
130 static void host_to_target_sigset_internal(target_sigset_t *d,
131 const sigset_t *s)
133 int i;
134 target_sigemptyset(d);
135 for (i = 1; i <= TARGET_NSIG; i++) {
136 if (sigismember(s, i)) {
137 target_sigaddset(d, host_to_target_signal(i));
142 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
144 target_sigset_t d1;
145 int i;
147 host_to_target_sigset_internal(&d1, s);
148 for(i = 0;i < TARGET_NSIG_WORDS; i++)
149 d->sig[i] = tswapal(d1.sig[i]);
152 static void target_to_host_sigset_internal(sigset_t *d,
153 const target_sigset_t *s)
155 int i;
156 sigemptyset(d);
157 for (i = 1; i <= TARGET_NSIG; i++) {
158 if (target_sigismember(s, i)) {
159 sigaddset(d, target_to_host_signal(i));
164 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
166 target_sigset_t s1;
167 int i;
169 for(i = 0;i < TARGET_NSIG_WORDS; i++)
170 s1.sig[i] = tswapal(s->sig[i]);
171 target_to_host_sigset_internal(d, &s1);
174 void host_to_target_old_sigset(abi_ulong *old_sigset,
175 const sigset_t *sigset)
177 target_sigset_t d;
178 host_to_target_sigset(&d, sigset);
179 *old_sigset = d.sig[0];
182 void target_to_host_old_sigset(sigset_t *sigset,
183 const abi_ulong *old_sigset)
185 target_sigset_t d;
186 int i;
188 d.sig[0] = *old_sigset;
189 for(i = 1;i < TARGET_NSIG_WORDS; i++)
190 d.sig[i] = 0;
191 target_to_host_sigset(sigset, &d);
194 int block_signals(void)
196 TaskState *ts = (TaskState *)thread_cpu->opaque;
197 sigset_t set;
199 /* It's OK to block everything including SIGSEGV, because we won't
200 * run any further guest code before unblocking signals in
201 * process_pending_signals().
203 sigfillset(&set);
204 sigprocmask(SIG_SETMASK, &set, 0);
206 return atomic_xchg(&ts->signal_pending, 1);
209 /* Wrapper for sigprocmask function
210 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
211 * are host signal set, not guest ones. Returns -TARGET_ERESTARTSYS if
212 * a signal was already pending and the syscall must be restarted, or
213 * 0 on success.
214 * If set is NULL, this is guaranteed not to fail.
216 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
218 TaskState *ts = (TaskState *)thread_cpu->opaque;
220 if (oldset) {
221 *oldset = ts->signal_mask;
224 if (set) {
225 int i;
227 if (block_signals()) {
228 return -TARGET_ERESTARTSYS;
231 switch (how) {
232 case SIG_BLOCK:
233 sigorset(&ts->signal_mask, &ts->signal_mask, set);
234 break;
235 case SIG_UNBLOCK:
236 for (i = 1; i <= NSIG; ++i) {
237 if (sigismember(set, i)) {
238 sigdelset(&ts->signal_mask, i);
241 break;
242 case SIG_SETMASK:
243 ts->signal_mask = *set;
244 break;
245 default:
246 g_assert_not_reached();
249 /* Silently ignore attempts to change blocking status of KILL or STOP */
250 sigdelset(&ts->signal_mask, SIGKILL);
251 sigdelset(&ts->signal_mask, SIGSTOP);
253 return 0;
256 #if !defined(TARGET_OPENRISC) && !defined(TARGET_UNICORE32) && \
257 !defined(TARGET_X86_64)
258 /* Just set the guest's signal mask to the specified value; the
259 * caller is assumed to have called block_signals() already.
261 static void set_sigmask(const sigset_t *set)
263 TaskState *ts = (TaskState *)thread_cpu->opaque;
265 ts->signal_mask = *set;
267 #endif
269 /* siginfo conversion */
271 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
272 const siginfo_t *info)
274 int sig = host_to_target_signal(info->si_signo);
275 int si_code = info->si_code;
276 int si_type;
277 tinfo->si_signo = sig;
278 tinfo->si_errno = 0;
279 tinfo->si_code = info->si_code;
281 /* This memset serves two purposes:
282 * (1) ensure we don't leak random junk to the guest later
283 * (2) placate false positives from gcc about fields
284 * being used uninitialized if it chooses to inline both this
285 * function and tswap_siginfo() into host_to_target_siginfo().
287 memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
289 /* This is awkward, because we have to use a combination of
290 * the si_code and si_signo to figure out which of the union's
291 * members are valid. (Within the host kernel it is always possible
292 * to tell, but the kernel carefully avoids giving userspace the
293 * high 16 bits of si_code, so we don't have the information to
294 * do this the easy way...) We therefore make our best guess,
295 * bearing in mind that a guest can spoof most of the si_codes
296 * via rt_sigqueueinfo() if it likes.
298 * Once we have made our guess, we record it in the top 16 bits of
299 * the si_code, so that tswap_siginfo() later can use it.
300 * tswap_siginfo() will strip these top bits out before writing
301 * si_code to the guest (sign-extending the lower bits).
304 switch (si_code) {
305 case SI_USER:
306 case SI_TKILL:
307 case SI_KERNEL:
308 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
309 * These are the only unspoofable si_code values.
311 tinfo->_sifields._kill._pid = info->si_pid;
312 tinfo->_sifields._kill._uid = info->si_uid;
313 si_type = QEMU_SI_KILL;
314 break;
315 default:
316 /* Everything else is spoofable. Make best guess based on signal */
317 switch (sig) {
318 case TARGET_SIGCHLD:
319 tinfo->_sifields._sigchld._pid = info->si_pid;
320 tinfo->_sifields._sigchld._uid = info->si_uid;
321 tinfo->_sifields._sigchld._status
322 = host_to_target_waitstatus(info->si_status);
323 tinfo->_sifields._sigchld._utime = info->si_utime;
324 tinfo->_sifields._sigchld._stime = info->si_stime;
325 si_type = QEMU_SI_CHLD;
326 break;
327 case TARGET_SIGIO:
328 tinfo->_sifields._sigpoll._band = info->si_band;
329 tinfo->_sifields._sigpoll._fd = info->si_fd;
330 si_type = QEMU_SI_POLL;
331 break;
332 default:
333 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
334 tinfo->_sifields._rt._pid = info->si_pid;
335 tinfo->_sifields._rt._uid = info->si_uid;
336 /* XXX: potential problem if 64 bit */
337 tinfo->_sifields._rt._sigval.sival_ptr
338 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
339 si_type = QEMU_SI_RT;
340 break;
342 break;
345 tinfo->si_code = deposit32(si_code, 16, 16, si_type);
348 static void tswap_siginfo(target_siginfo_t *tinfo,
349 const target_siginfo_t *info)
351 int si_type = extract32(info->si_code, 16, 16);
352 int si_code = sextract32(info->si_code, 0, 16);
354 __put_user(info->si_signo, &tinfo->si_signo);
355 __put_user(info->si_errno, &tinfo->si_errno);
356 __put_user(si_code, &tinfo->si_code);
358 /* We can use our internal marker of which fields in the structure
359 * are valid, rather than duplicating the guesswork of
360 * host_to_target_siginfo_noswap() here.
362 switch (si_type) {
363 case QEMU_SI_KILL:
364 __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
365 __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
366 break;
367 case QEMU_SI_TIMER:
368 __put_user(info->_sifields._timer._timer1,
369 &tinfo->_sifields._timer._timer1);
370 __put_user(info->_sifields._timer._timer2,
371 &tinfo->_sifields._timer._timer2);
372 break;
373 case QEMU_SI_POLL:
374 __put_user(info->_sifields._sigpoll._band,
375 &tinfo->_sifields._sigpoll._band);
376 __put_user(info->_sifields._sigpoll._fd,
377 &tinfo->_sifields._sigpoll._fd);
378 break;
379 case QEMU_SI_FAULT:
380 __put_user(info->_sifields._sigfault._addr,
381 &tinfo->_sifields._sigfault._addr);
382 break;
383 case QEMU_SI_CHLD:
384 __put_user(info->_sifields._sigchld._pid,
385 &tinfo->_sifields._sigchld._pid);
386 __put_user(info->_sifields._sigchld._uid,
387 &tinfo->_sifields._sigchld._uid);
388 __put_user(info->_sifields._sigchld._status,
389 &tinfo->_sifields._sigchld._status);
390 __put_user(info->_sifields._sigchld._utime,
391 &tinfo->_sifields._sigchld._utime);
392 __put_user(info->_sifields._sigchld._stime,
393 &tinfo->_sifields._sigchld._stime);
394 break;
395 case QEMU_SI_RT:
396 __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
397 __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
398 __put_user(info->_sifields._rt._sigval.sival_ptr,
399 &tinfo->_sifields._rt._sigval.sival_ptr);
400 break;
401 default:
402 g_assert_not_reached();
406 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
408 target_siginfo_t tgt_tmp;
409 host_to_target_siginfo_noswap(&tgt_tmp, info);
410 tswap_siginfo(tinfo, &tgt_tmp);
413 /* XXX: we support only POSIX RT signals are used. */
414 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
415 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
417 /* This conversion is used only for the rt_sigqueueinfo syscall,
418 * and so we know that the _rt fields are the valid ones.
420 abi_ulong sival_ptr;
422 __get_user(info->si_signo, &tinfo->si_signo);
423 __get_user(info->si_errno, &tinfo->si_errno);
424 __get_user(info->si_code, &tinfo->si_code);
425 __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
426 __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
427 __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
428 info->si_value.sival_ptr = (void *)(long)sival_ptr;
431 static int fatal_signal (int sig)
433 switch (sig) {
434 case TARGET_SIGCHLD:
435 case TARGET_SIGURG:
436 case TARGET_SIGWINCH:
437 /* Ignored by default. */
438 return 0;
439 case TARGET_SIGCONT:
440 case TARGET_SIGSTOP:
441 case TARGET_SIGTSTP:
442 case TARGET_SIGTTIN:
443 case TARGET_SIGTTOU:
444 /* Job control signals. */
445 return 0;
446 default:
447 return 1;
451 /* returns 1 if given signal should dump core if not handled */
452 static int core_dump_signal(int sig)
454 switch (sig) {
455 case TARGET_SIGABRT:
456 case TARGET_SIGFPE:
457 case TARGET_SIGILL:
458 case TARGET_SIGQUIT:
459 case TARGET_SIGSEGV:
460 case TARGET_SIGTRAP:
461 case TARGET_SIGBUS:
462 return (1);
463 default:
464 return (0);
468 void signal_init(void)
470 TaskState *ts = (TaskState *)thread_cpu->opaque;
471 struct sigaction act;
472 struct sigaction oact;
473 int i, j;
474 int host_sig;
476 /* generate signal conversion tables */
477 for(i = 1; i < _NSIG; i++) {
478 if (host_to_target_signal_table[i] == 0)
479 host_to_target_signal_table[i] = i;
481 for(i = 1; i < _NSIG; i++) {
482 j = host_to_target_signal_table[i];
483 target_to_host_signal_table[j] = i;
486 /* Set the signal mask from the host mask. */
487 sigprocmask(0, 0, &ts->signal_mask);
489 /* set all host signal handlers. ALL signals are blocked during
490 the handlers to serialize them. */
491 memset(sigact_table, 0, sizeof(sigact_table));
493 sigfillset(&act.sa_mask);
494 act.sa_flags = SA_SIGINFO;
495 act.sa_sigaction = host_signal_handler;
496 for(i = 1; i <= TARGET_NSIG; i++) {
497 host_sig = target_to_host_signal(i);
498 sigaction(host_sig, NULL, &oact);
499 if (oact.sa_sigaction == (void *)SIG_IGN) {
500 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
501 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
502 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
504 /* If there's already a handler installed then something has
505 gone horribly wrong, so don't even try to handle that case. */
506 /* Install some handlers for our own use. We need at least
507 SIGSEGV and SIGBUS, to detect exceptions. We can not just
508 trap all signals because it affects syscall interrupt
509 behavior. But do trap all default-fatal signals. */
510 if (fatal_signal (i))
511 sigaction(host_sig, &act, NULL);
515 #if !(defined(TARGET_X86_64) || defined(TARGET_UNICORE32))
516 /* Force a synchronously taken signal. The kernel force_sig() function
517 * also forces the signal to "not blocked, not ignored", but for QEMU
518 * that work is done in process_pending_signals().
520 static void force_sig(int sig)
522 CPUState *cpu = thread_cpu;
523 CPUArchState *env = cpu->env_ptr;
524 target_siginfo_t info;
526 info.si_signo = sig;
527 info.si_errno = 0;
528 info.si_code = TARGET_SI_KERNEL;
529 info._sifields._kill._pid = 0;
530 info._sifields._kill._uid = 0;
531 queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
534 /* Force a SIGSEGV if we couldn't write to memory trying to set
535 * up the signal frame. oldsig is the signal we were trying to handle
536 * at the point of failure.
538 static void force_sigsegv(int oldsig)
540 if (oldsig == SIGSEGV) {
541 /* Make sure we don't try to deliver the signal again; this will
542 * end up with handle_pending_signal() calling dump_core_and_abort().
544 sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
546 force_sig(TARGET_SIGSEGV);
548 #endif
550 /* abort execution with signal */
551 static void QEMU_NORETURN dump_core_and_abort(int target_sig)
553 CPUState *cpu = thread_cpu;
554 CPUArchState *env = cpu->env_ptr;
555 TaskState *ts = (TaskState *)cpu->opaque;
556 int host_sig, core_dumped = 0;
557 struct sigaction act;
559 host_sig = target_to_host_signal(target_sig);
560 trace_user_force_sig(env, target_sig, host_sig);
561 gdb_signalled(env, target_sig);
563 /* dump core if supported by target binary format */
564 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
565 stop_all_tasks();
566 core_dumped =
567 ((*ts->bprm->core_dump)(target_sig, env) == 0);
569 if (core_dumped) {
570 /* we already dumped the core of target process, we don't want
571 * a coredump of qemu itself */
572 struct rlimit nodump;
573 getrlimit(RLIMIT_CORE, &nodump);
574 nodump.rlim_cur=0;
575 setrlimit(RLIMIT_CORE, &nodump);
576 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
577 target_sig, strsignal(host_sig), "core dumped" );
580 /* The proper exit code for dying from an uncaught signal is
581 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
582 * a negative value. To get the proper exit code we need to
583 * actually die from an uncaught signal. Here the default signal
584 * handler is installed, we send ourself a signal and we wait for
585 * it to arrive. */
586 sigfillset(&act.sa_mask);
587 act.sa_handler = SIG_DFL;
588 act.sa_flags = 0;
589 sigaction(host_sig, &act, NULL);
591 /* For some reason raise(host_sig) doesn't send the signal when
592 * statically linked on x86-64. */
593 kill(getpid(), host_sig);
595 /* Make sure the signal isn't masked (just reuse the mask inside
596 of act) */
597 sigdelset(&act.sa_mask, host_sig);
598 sigsuspend(&act.sa_mask);
600 /* unreachable */
601 abort();
604 /* queue a signal so that it will be send to the virtual CPU as soon
605 as possible */
606 int queue_signal(CPUArchState *env, int sig, int si_type,
607 target_siginfo_t *info)
609 CPUState *cpu = ENV_GET_CPU(env);
610 TaskState *ts = cpu->opaque;
612 trace_user_queue_signal(env, sig);
614 info->si_code = deposit32(info->si_code, 16, 16, si_type);
616 ts->sync_signal.info = *info;
617 ts->sync_signal.pending = sig;
618 /* signal that a new signal is pending */
619 atomic_set(&ts->signal_pending, 1);
620 return 1; /* indicates that the signal was queued */
623 #ifndef HAVE_SAFE_SYSCALL
624 static inline void rewind_if_in_safe_syscall(void *puc)
626 /* Default version: never rewind */
628 #endif
630 static void host_signal_handler(int host_signum, siginfo_t *info,
631 void *puc)
633 CPUArchState *env = thread_cpu->env_ptr;
634 CPUState *cpu = ENV_GET_CPU(env);
635 TaskState *ts = cpu->opaque;
637 int sig;
638 target_siginfo_t tinfo;
639 ucontext_t *uc = puc;
640 struct emulated_sigtable *k;
642 /* the CPU emulator uses some host signals to detect exceptions,
643 we forward to it some signals */
644 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
645 && info->si_code > 0) {
646 if (cpu_signal_handler(host_signum, info, puc))
647 return;
650 /* get target signal number */
651 sig = host_to_target_signal(host_signum);
652 if (sig < 1 || sig > TARGET_NSIG)
653 return;
654 trace_user_host_signal(env, host_signum, sig);
656 rewind_if_in_safe_syscall(puc);
658 host_to_target_siginfo_noswap(&tinfo, info);
659 k = &ts->sigtab[sig - 1];
660 k->info = tinfo;
661 k->pending = sig;
662 ts->signal_pending = 1;
664 /* Block host signals until target signal handler entered. We
665 * can't block SIGSEGV or SIGBUS while we're executing guest
666 * code in case the guest code provokes one in the window between
667 * now and it getting out to the main loop. Signals will be
668 * unblocked again in process_pending_signals().
670 * WARNING: we cannot use sigfillset() here because the uc_sigmask
671 * field is a kernel sigset_t, which is much smaller than the
672 * libc sigset_t which sigfillset() operates on. Using sigfillset()
673 * would write 0xff bytes off the end of the structure and trash
674 * data on the struct.
675 * We can't use sizeof(uc->uc_sigmask) either, because the libc
676 * headers define the struct field with the wrong (too large) type.
678 memset(&uc->uc_sigmask, 0xff, SIGSET_T_SIZE);
679 sigdelset(&uc->uc_sigmask, SIGSEGV);
680 sigdelset(&uc->uc_sigmask, SIGBUS);
682 /* interrupt the virtual CPU as soon as possible */
683 cpu_exit(thread_cpu);
686 /* do_sigaltstack() returns target values and errnos. */
687 /* compare linux/kernel/signal.c:do_sigaltstack() */
688 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
690 int ret;
691 struct target_sigaltstack oss;
693 /* XXX: test errors */
694 if(uoss_addr)
696 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
697 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
698 __put_user(sas_ss_flags(sp), &oss.ss_flags);
701 if(uss_addr)
703 struct target_sigaltstack *uss;
704 struct target_sigaltstack ss;
705 size_t minstacksize = TARGET_MINSIGSTKSZ;
707 #if defined(TARGET_PPC64)
708 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
709 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
710 if (get_ppc64_abi(image) > 1) {
711 minstacksize = 4096;
713 #endif
715 ret = -TARGET_EFAULT;
716 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
717 goto out;
719 __get_user(ss.ss_sp, &uss->ss_sp);
720 __get_user(ss.ss_size, &uss->ss_size);
721 __get_user(ss.ss_flags, &uss->ss_flags);
722 unlock_user_struct(uss, uss_addr, 0);
724 ret = -TARGET_EPERM;
725 if (on_sig_stack(sp))
726 goto out;
728 ret = -TARGET_EINVAL;
729 if (ss.ss_flags != TARGET_SS_DISABLE
730 && ss.ss_flags != TARGET_SS_ONSTACK
731 && ss.ss_flags != 0)
732 goto out;
734 if (ss.ss_flags == TARGET_SS_DISABLE) {
735 ss.ss_size = 0;
736 ss.ss_sp = 0;
737 } else {
738 ret = -TARGET_ENOMEM;
739 if (ss.ss_size < minstacksize) {
740 goto out;
744 target_sigaltstack_used.ss_sp = ss.ss_sp;
745 target_sigaltstack_used.ss_size = ss.ss_size;
748 if (uoss_addr) {
749 ret = -TARGET_EFAULT;
750 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
751 goto out;
754 ret = 0;
755 out:
756 return ret;
759 /* do_sigaction() return target values and host errnos */
760 int do_sigaction(int sig, const struct target_sigaction *act,
761 struct target_sigaction *oact)
763 struct target_sigaction *k;
764 struct sigaction act1;
765 int host_sig;
766 int ret = 0;
768 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) {
769 return -TARGET_EINVAL;
772 if (block_signals()) {
773 return -TARGET_ERESTARTSYS;
776 k = &sigact_table[sig - 1];
777 if (oact) {
778 __put_user(k->_sa_handler, &oact->_sa_handler);
779 __put_user(k->sa_flags, &oact->sa_flags);
780 #if !defined(TARGET_MIPS)
781 __put_user(k->sa_restorer, &oact->sa_restorer);
782 #endif
783 /* Not swapped. */
784 oact->sa_mask = k->sa_mask;
786 if (act) {
787 /* FIXME: This is not threadsafe. */
788 __get_user(k->_sa_handler, &act->_sa_handler);
789 __get_user(k->sa_flags, &act->sa_flags);
790 #if !defined(TARGET_MIPS)
791 __get_user(k->sa_restorer, &act->sa_restorer);
792 #endif
793 /* To be swapped in target_to_host_sigset. */
794 k->sa_mask = act->sa_mask;
796 /* we update the host linux signal state */
797 host_sig = target_to_host_signal(sig);
798 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
799 sigfillset(&act1.sa_mask);
800 act1.sa_flags = SA_SIGINFO;
801 if (k->sa_flags & TARGET_SA_RESTART)
802 act1.sa_flags |= SA_RESTART;
803 /* NOTE: it is important to update the host kernel signal
804 ignore state to avoid getting unexpected interrupted
805 syscalls */
806 if (k->_sa_handler == TARGET_SIG_IGN) {
807 act1.sa_sigaction = (void *)SIG_IGN;
808 } else if (k->_sa_handler == TARGET_SIG_DFL) {
809 if (fatal_signal (sig))
810 act1.sa_sigaction = host_signal_handler;
811 else
812 act1.sa_sigaction = (void *)SIG_DFL;
813 } else {
814 act1.sa_sigaction = host_signal_handler;
816 ret = sigaction(host_sig, &act1, NULL);
819 return ret;
822 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
824 /* from the Linux kernel */
826 struct target_fpreg {
827 uint16_t significand[4];
828 uint16_t exponent;
831 struct target_fpxreg {
832 uint16_t significand[4];
833 uint16_t exponent;
834 uint16_t padding[3];
837 struct target_xmmreg {
838 abi_ulong element[4];
841 struct target_fpstate {
842 /* Regular FPU environment */
843 abi_ulong cw;
844 abi_ulong sw;
845 abi_ulong tag;
846 abi_ulong ipoff;
847 abi_ulong cssel;
848 abi_ulong dataoff;
849 abi_ulong datasel;
850 struct target_fpreg _st[8];
851 uint16_t status;
852 uint16_t magic; /* 0xffff = regular FPU data only */
854 /* FXSR FPU environment */
855 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
856 abi_ulong mxcsr;
857 abi_ulong reserved;
858 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
859 struct target_xmmreg _xmm[8];
860 abi_ulong padding[56];
863 #define X86_FXSR_MAGIC 0x0000
865 struct target_sigcontext {
866 uint16_t gs, __gsh;
867 uint16_t fs, __fsh;
868 uint16_t es, __esh;
869 uint16_t ds, __dsh;
870 abi_ulong edi;
871 abi_ulong esi;
872 abi_ulong ebp;
873 abi_ulong esp;
874 abi_ulong ebx;
875 abi_ulong edx;
876 abi_ulong ecx;
877 abi_ulong eax;
878 abi_ulong trapno;
879 abi_ulong err;
880 abi_ulong eip;
881 uint16_t cs, __csh;
882 abi_ulong eflags;
883 abi_ulong esp_at_signal;
884 uint16_t ss, __ssh;
885 abi_ulong fpstate; /* pointer */
886 abi_ulong oldmask;
887 abi_ulong cr2;
890 struct target_ucontext {
891 abi_ulong tuc_flags;
892 abi_ulong tuc_link;
893 target_stack_t tuc_stack;
894 struct target_sigcontext tuc_mcontext;
895 target_sigset_t tuc_sigmask; /* mask last for extensibility */
898 struct sigframe
900 abi_ulong pretcode;
901 int sig;
902 struct target_sigcontext sc;
903 struct target_fpstate fpstate;
904 abi_ulong extramask[TARGET_NSIG_WORDS-1];
905 char retcode[8];
908 struct rt_sigframe
910 abi_ulong pretcode;
911 int sig;
912 abi_ulong pinfo;
913 abi_ulong puc;
914 struct target_siginfo info;
915 struct target_ucontext uc;
916 struct target_fpstate fpstate;
917 char retcode[8];
921 * Set up a signal frame.
924 /* XXX: save x87 state */
925 static void setup_sigcontext(struct target_sigcontext *sc,
926 struct target_fpstate *fpstate, CPUX86State *env, abi_ulong mask,
927 abi_ulong fpstate_addr)
929 CPUState *cs = CPU(x86_env_get_cpu(env));
930 uint16_t magic;
932 /* already locked in setup_frame() */
933 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
934 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
935 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
936 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
937 __put_user(env->regs[R_EDI], &sc->edi);
938 __put_user(env->regs[R_ESI], &sc->esi);
939 __put_user(env->regs[R_EBP], &sc->ebp);
940 __put_user(env->regs[R_ESP], &sc->esp);
941 __put_user(env->regs[R_EBX], &sc->ebx);
942 __put_user(env->regs[R_EDX], &sc->edx);
943 __put_user(env->regs[R_ECX], &sc->ecx);
944 __put_user(env->regs[R_EAX], &sc->eax);
945 __put_user(cs->exception_index, &sc->trapno);
946 __put_user(env->error_code, &sc->err);
947 __put_user(env->eip, &sc->eip);
948 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
949 __put_user(env->eflags, &sc->eflags);
950 __put_user(env->regs[R_ESP], &sc->esp_at_signal);
951 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
953 cpu_x86_fsave(env, fpstate_addr, 1);
954 fpstate->status = fpstate->sw;
955 magic = 0xffff;
956 __put_user(magic, &fpstate->magic);
957 __put_user(fpstate_addr, &sc->fpstate);
959 /* non-iBCS2 extensions.. */
960 __put_user(mask, &sc->oldmask);
961 __put_user(env->cr[2], &sc->cr2);
965 * Determine which stack to use..
968 static inline abi_ulong
969 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
971 unsigned long esp;
973 /* Default to using normal stack */
974 esp = env->regs[R_ESP];
975 /* This is the X/Open sanctioned signal stack switching. */
976 if (ka->sa_flags & TARGET_SA_ONSTACK) {
977 if (sas_ss_flags(esp) == 0) {
978 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
980 } else {
982 /* This is the legacy signal stack switching. */
983 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
984 !(ka->sa_flags & TARGET_SA_RESTORER) &&
985 ka->sa_restorer) {
986 esp = (unsigned long) ka->sa_restorer;
989 return (esp - frame_size) & -8ul;
992 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
993 static void setup_frame(int sig, struct target_sigaction *ka,
994 target_sigset_t *set, CPUX86State *env)
996 abi_ulong frame_addr;
997 struct sigframe *frame;
998 int i;
1000 frame_addr = get_sigframe(ka, env, sizeof(*frame));
1001 trace_user_setup_frame(env, frame_addr);
1003 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1004 goto give_sigsegv;
1006 __put_user(sig, &frame->sig);
1008 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
1009 frame_addr + offsetof(struct sigframe, fpstate));
1011 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1012 __put_user(set->sig[i], &frame->extramask[i - 1]);
1015 /* Set up to return from userspace. If provided, use a stub
1016 already in userspace. */
1017 if (ka->sa_flags & TARGET_SA_RESTORER) {
1018 __put_user(ka->sa_restorer, &frame->pretcode);
1019 } else {
1020 uint16_t val16;
1021 abi_ulong retcode_addr;
1022 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
1023 __put_user(retcode_addr, &frame->pretcode);
1024 /* This is popl %eax ; movl $,%eax ; int $0x80 */
1025 val16 = 0xb858;
1026 __put_user(val16, (uint16_t *)(frame->retcode+0));
1027 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
1028 val16 = 0x80cd;
1029 __put_user(val16, (uint16_t *)(frame->retcode+6));
1033 /* Set up registers for signal handler */
1034 env->regs[R_ESP] = frame_addr;
1035 env->eip = ka->_sa_handler;
1037 cpu_x86_load_seg(env, R_DS, __USER_DS);
1038 cpu_x86_load_seg(env, R_ES, __USER_DS);
1039 cpu_x86_load_seg(env, R_SS, __USER_DS);
1040 cpu_x86_load_seg(env, R_CS, __USER_CS);
1041 env->eflags &= ~TF_MASK;
1043 unlock_user_struct(frame, frame_addr, 1);
1045 return;
1047 give_sigsegv:
1048 force_sigsegv(sig);
1051 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
1052 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1053 target_siginfo_t *info,
1054 target_sigset_t *set, CPUX86State *env)
1056 abi_ulong frame_addr, addr;
1057 struct rt_sigframe *frame;
1058 int i;
1060 frame_addr = get_sigframe(ka, env, sizeof(*frame));
1061 trace_user_setup_rt_frame(env, frame_addr);
1063 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1064 goto give_sigsegv;
1066 __put_user(sig, &frame->sig);
1067 addr = frame_addr + offsetof(struct rt_sigframe, info);
1068 __put_user(addr, &frame->pinfo);
1069 addr = frame_addr + offsetof(struct rt_sigframe, uc);
1070 __put_user(addr, &frame->puc);
1071 tswap_siginfo(&frame->info, info);
1073 /* Create the ucontext. */
1074 __put_user(0, &frame->uc.tuc_flags);
1075 __put_user(0, &frame->uc.tuc_link);
1076 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
1077 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
1078 &frame->uc.tuc_stack.ss_flags);
1079 __put_user(target_sigaltstack_used.ss_size,
1080 &frame->uc.tuc_stack.ss_size);
1081 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env,
1082 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate));
1084 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1085 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
1088 /* Set up to return from userspace. If provided, use a stub
1089 already in userspace. */
1090 if (ka->sa_flags & TARGET_SA_RESTORER) {
1091 __put_user(ka->sa_restorer, &frame->pretcode);
1092 } else {
1093 uint16_t val16;
1094 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
1095 __put_user(addr, &frame->pretcode);
1096 /* This is movl $,%eax ; int $0x80 */
1097 __put_user(0xb8, (char *)(frame->retcode+0));
1098 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
1099 val16 = 0x80cd;
1100 __put_user(val16, (uint16_t *)(frame->retcode+5));
1103 /* Set up registers for signal handler */
1104 env->regs[R_ESP] = frame_addr;
1105 env->eip = ka->_sa_handler;
1107 cpu_x86_load_seg(env, R_DS, __USER_DS);
1108 cpu_x86_load_seg(env, R_ES, __USER_DS);
1109 cpu_x86_load_seg(env, R_SS, __USER_DS);
1110 cpu_x86_load_seg(env, R_CS, __USER_CS);
1111 env->eflags &= ~TF_MASK;
1113 unlock_user_struct(frame, frame_addr, 1);
1115 return;
1117 give_sigsegv:
1118 force_sigsegv(sig);
1121 static int
1122 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc)
1124 unsigned int err = 0;
1125 abi_ulong fpstate_addr;
1126 unsigned int tmpflags;
1128 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1129 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1130 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1131 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1133 env->regs[R_EDI] = tswapl(sc->edi);
1134 env->regs[R_ESI] = tswapl(sc->esi);
1135 env->regs[R_EBP] = tswapl(sc->ebp);
1136 env->regs[R_ESP] = tswapl(sc->esp);
1137 env->regs[R_EBX] = tswapl(sc->ebx);
1138 env->regs[R_EDX] = tswapl(sc->edx);
1139 env->regs[R_ECX] = tswapl(sc->ecx);
1140 env->regs[R_EAX] = tswapl(sc->eax);
1141 env->eip = tswapl(sc->eip);
1143 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1144 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1146 tmpflags = tswapl(sc->eflags);
1147 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1148 // regs->orig_eax = -1; /* disable syscall checks */
1150 fpstate_addr = tswapl(sc->fpstate);
1151 if (fpstate_addr != 0) {
1152 if (!access_ok(VERIFY_READ, fpstate_addr,
1153 sizeof(struct target_fpstate)))
1154 goto badframe;
1155 cpu_x86_frstor(env, fpstate_addr, 1);
1158 return err;
1159 badframe:
1160 return 1;
1163 long do_sigreturn(CPUX86State *env)
1165 struct sigframe *frame;
1166 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1167 target_sigset_t target_set;
1168 sigset_t set;
1169 int i;
1171 trace_user_do_sigreturn(env, frame_addr);
1172 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1173 goto badframe;
1174 /* set blocked signals */
1175 __get_user(target_set.sig[0], &frame->sc.oldmask);
1176 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1177 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
1180 target_to_host_sigset_internal(&set, &target_set);
1181 set_sigmask(&set);
1183 /* restore registers */
1184 if (restore_sigcontext(env, &frame->sc))
1185 goto badframe;
1186 unlock_user_struct(frame, frame_addr, 0);
1187 return -TARGET_QEMU_ESIGRETURN;
1189 badframe:
1190 unlock_user_struct(frame, frame_addr, 0);
1191 force_sig(TARGET_SIGSEGV);
1192 return -TARGET_QEMU_ESIGRETURN;
1195 long do_rt_sigreturn(CPUX86State *env)
1197 abi_ulong frame_addr;
1198 struct rt_sigframe *frame;
1199 sigset_t set;
1201 frame_addr = env->regs[R_ESP] - 4;
1202 trace_user_do_rt_sigreturn(env, frame_addr);
1203 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1204 goto badframe;
1205 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1206 set_sigmask(&set);
1208 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
1209 goto badframe;
1212 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1213 get_sp_from_cpustate(env)) == -EFAULT) {
1214 goto badframe;
1217 unlock_user_struct(frame, frame_addr, 0);
1218 return -TARGET_QEMU_ESIGRETURN;
1220 badframe:
1221 unlock_user_struct(frame, frame_addr, 0);
1222 force_sig(TARGET_SIGSEGV);
1223 return -TARGET_QEMU_ESIGRETURN;
1226 #elif defined(TARGET_AARCH64)
1228 struct target_sigcontext {
1229 uint64_t fault_address;
1230 /* AArch64 registers */
1231 uint64_t regs[31];
1232 uint64_t sp;
1233 uint64_t pc;
1234 uint64_t pstate;
1235 /* 4K reserved for FP/SIMD state and future expansion */
1236 char __reserved[4096] __attribute__((__aligned__(16)));
1239 struct target_ucontext {
1240 abi_ulong tuc_flags;
1241 abi_ulong tuc_link;
1242 target_stack_t tuc_stack;
1243 target_sigset_t tuc_sigmask;
1244 /* glibc uses a 1024-bit sigset_t */
1245 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1246 /* last for future expansion */
1247 struct target_sigcontext tuc_mcontext;
1251 * Header to be used at the beginning of structures extending the user
1252 * context. Such structures must be placed after the rt_sigframe on the stack
1253 * and be 16-byte aligned. The last structure must be a dummy one with the
1254 * magic and size set to 0.
1256 struct target_aarch64_ctx {
1257 uint32_t magic;
1258 uint32_t size;
1261 #define TARGET_FPSIMD_MAGIC 0x46508001
1263 struct target_fpsimd_context {
1264 struct target_aarch64_ctx head;
1265 uint32_t fpsr;
1266 uint32_t fpcr;
1267 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1271 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1272 * user space as it will change with the addition of new context. User space
1273 * should check the magic/size information.
1275 struct target_aux_context {
1276 struct target_fpsimd_context fpsimd;
1277 /* additional context to be added before "end" */
1278 struct target_aarch64_ctx end;
1281 struct target_rt_sigframe {
1282 struct target_siginfo info;
1283 struct target_ucontext uc;
1284 uint64_t fp;
1285 uint64_t lr;
1286 uint32_t tramp[2];
1289 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1290 CPUARMState *env, target_sigset_t *set)
1292 int i;
1293 struct target_aux_context *aux =
1294 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1296 /* set up the stack frame for unwinding */
1297 __put_user(env->xregs[29], &sf->fp);
1298 __put_user(env->xregs[30], &sf->lr);
1300 for (i = 0; i < 31; i++) {
1301 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1303 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1304 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
1305 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
1307 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
1309 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1310 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1313 for (i = 0; i < 32; i++) {
1314 #ifdef TARGET_WORDS_BIGENDIAN
1315 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1316 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1317 #else
1318 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1319 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1320 #endif
1322 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1323 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1324 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1325 __put_user(sizeof(struct target_fpsimd_context),
1326 &aux->fpsimd.head.size);
1328 /* set the "end" magic */
1329 __put_user(0, &aux->end.magic);
1330 __put_user(0, &aux->end.size);
1332 return 0;
1335 static int target_restore_sigframe(CPUARMState *env,
1336 struct target_rt_sigframe *sf)
1338 sigset_t set;
1339 int i;
1340 struct target_aux_context *aux =
1341 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1342 uint32_t magic, size, fpsr, fpcr;
1343 uint64_t pstate;
1345 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1346 set_sigmask(&set);
1348 for (i = 0; i < 31; i++) {
1349 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1352 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1353 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1354 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1355 pstate_write(env, pstate);
1357 __get_user(magic, &aux->fpsimd.head.magic);
1358 __get_user(size, &aux->fpsimd.head.size);
1360 if (magic != TARGET_FPSIMD_MAGIC
1361 || size != sizeof(struct target_fpsimd_context)) {
1362 return 1;
1365 for (i = 0; i < 32; i++) {
1366 #ifdef TARGET_WORDS_BIGENDIAN
1367 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1368 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1369 #else
1370 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1371 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1372 #endif
1374 __get_user(fpsr, &aux->fpsimd.fpsr);
1375 vfp_set_fpsr(env, fpsr);
1376 __get_user(fpcr, &aux->fpsimd.fpcr);
1377 vfp_set_fpcr(env, fpcr);
1379 return 0;
1382 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1384 abi_ulong sp;
1386 sp = env->xregs[31];
1389 * This is the X/Open sanctioned signal stack switching.
1391 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1392 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1395 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1397 return sp;
1400 static void target_setup_frame(int usig, struct target_sigaction *ka,
1401 target_siginfo_t *info, target_sigset_t *set,
1402 CPUARMState *env)
1404 struct target_rt_sigframe *frame;
1405 abi_ulong frame_addr, return_addr;
1407 frame_addr = get_sigframe(ka, env);
1408 trace_user_setup_frame(env, frame_addr);
1409 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1410 goto give_sigsegv;
1413 __put_user(0, &frame->uc.tuc_flags);
1414 __put_user(0, &frame->uc.tuc_link);
1416 __put_user(target_sigaltstack_used.ss_sp,
1417 &frame->uc.tuc_stack.ss_sp);
1418 __put_user(sas_ss_flags(env->xregs[31]),
1419 &frame->uc.tuc_stack.ss_flags);
1420 __put_user(target_sigaltstack_used.ss_size,
1421 &frame->uc.tuc_stack.ss_size);
1422 target_setup_sigframe(frame, env, set);
1423 if (ka->sa_flags & TARGET_SA_RESTORER) {
1424 return_addr = ka->sa_restorer;
1425 } else {
1426 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1427 __put_user(0xd2801168, &frame->tramp[0]);
1428 __put_user(0xd4000001, &frame->tramp[1]);
1429 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1431 env->xregs[0] = usig;
1432 env->xregs[31] = frame_addr;
1433 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1434 env->pc = ka->_sa_handler;
1435 env->xregs[30] = return_addr;
1436 if (info) {
1437 tswap_siginfo(&frame->info, info);
1438 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1439 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1442 unlock_user_struct(frame, frame_addr, 1);
1443 return;
1445 give_sigsegv:
1446 unlock_user_struct(frame, frame_addr, 1);
1447 force_sigsegv(usig);
1450 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1451 target_siginfo_t *info, target_sigset_t *set,
1452 CPUARMState *env)
1454 target_setup_frame(sig, ka, info, set, env);
1457 static void setup_frame(int sig, struct target_sigaction *ka,
1458 target_sigset_t *set, CPUARMState *env)
1460 target_setup_frame(sig, ka, 0, set, env);
1463 long do_rt_sigreturn(CPUARMState *env)
1465 struct target_rt_sigframe *frame = NULL;
1466 abi_ulong frame_addr = env->xregs[31];
1468 trace_user_do_rt_sigreturn(env, frame_addr);
1469 if (frame_addr & 15) {
1470 goto badframe;
1473 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1474 goto badframe;
1477 if (target_restore_sigframe(env, frame)) {
1478 goto badframe;
1481 if (do_sigaltstack(frame_addr +
1482 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1483 0, get_sp_from_cpustate(env)) == -EFAULT) {
1484 goto badframe;
1487 unlock_user_struct(frame, frame_addr, 0);
1488 return -TARGET_QEMU_ESIGRETURN;
1490 badframe:
1491 unlock_user_struct(frame, frame_addr, 0);
1492 force_sig(TARGET_SIGSEGV);
1493 return -TARGET_QEMU_ESIGRETURN;
1496 long do_sigreturn(CPUARMState *env)
1498 return do_rt_sigreturn(env);
1501 #elif defined(TARGET_ARM)
1503 struct target_sigcontext {
1504 abi_ulong trap_no;
1505 abi_ulong error_code;
1506 abi_ulong oldmask;
1507 abi_ulong arm_r0;
1508 abi_ulong arm_r1;
1509 abi_ulong arm_r2;
1510 abi_ulong arm_r3;
1511 abi_ulong arm_r4;
1512 abi_ulong arm_r5;
1513 abi_ulong arm_r6;
1514 abi_ulong arm_r7;
1515 abi_ulong arm_r8;
1516 abi_ulong arm_r9;
1517 abi_ulong arm_r10;
1518 abi_ulong arm_fp;
1519 abi_ulong arm_ip;
1520 abi_ulong arm_sp;
1521 abi_ulong arm_lr;
1522 abi_ulong arm_pc;
1523 abi_ulong arm_cpsr;
1524 abi_ulong fault_address;
1527 struct target_ucontext_v1 {
1528 abi_ulong tuc_flags;
1529 abi_ulong tuc_link;
1530 target_stack_t tuc_stack;
1531 struct target_sigcontext tuc_mcontext;
1532 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1535 struct target_ucontext_v2 {
1536 abi_ulong tuc_flags;
1537 abi_ulong tuc_link;
1538 target_stack_t tuc_stack;
1539 struct target_sigcontext tuc_mcontext;
1540 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1541 char __unused[128 - sizeof(target_sigset_t)];
1542 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1545 struct target_user_vfp {
1546 uint64_t fpregs[32];
1547 abi_ulong fpscr;
1550 struct target_user_vfp_exc {
1551 abi_ulong fpexc;
1552 abi_ulong fpinst;
1553 abi_ulong fpinst2;
1556 struct target_vfp_sigframe {
1557 abi_ulong magic;
1558 abi_ulong size;
1559 struct target_user_vfp ufp;
1560 struct target_user_vfp_exc ufp_exc;
1561 } __attribute__((__aligned__(8)));
1563 struct target_iwmmxt_sigframe {
1564 abi_ulong magic;
1565 abi_ulong size;
1566 uint64_t regs[16];
1567 /* Note that not all the coprocessor control registers are stored here */
1568 uint32_t wcssf;
1569 uint32_t wcasf;
1570 uint32_t wcgr0;
1571 uint32_t wcgr1;
1572 uint32_t wcgr2;
1573 uint32_t wcgr3;
1574 } __attribute__((__aligned__(8)));
1576 #define TARGET_VFP_MAGIC 0x56465001
1577 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1579 struct sigframe_v1
1581 struct target_sigcontext sc;
1582 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1583 abi_ulong retcode;
1586 struct sigframe_v2
1588 struct target_ucontext_v2 uc;
1589 abi_ulong retcode;
1592 struct rt_sigframe_v1
1594 abi_ulong pinfo;
1595 abi_ulong puc;
1596 struct target_siginfo info;
1597 struct target_ucontext_v1 uc;
1598 abi_ulong retcode;
1601 struct rt_sigframe_v2
1603 struct target_siginfo info;
1604 struct target_ucontext_v2 uc;
1605 abi_ulong retcode;
1608 #define TARGET_CONFIG_CPU_32 1
1611 * For ARM syscalls, we encode the syscall number into the instruction.
1613 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1614 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1617 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1618 * need two 16-bit instructions.
1620 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1621 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1623 static const abi_ulong retcodes[4] = {
1624 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1625 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1629 static inline int valid_user_regs(CPUARMState *regs)
1631 return 1;
1634 static void
1635 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1636 CPUARMState *env, abi_ulong mask)
1638 __put_user(env->regs[0], &sc->arm_r0);
1639 __put_user(env->regs[1], &sc->arm_r1);
1640 __put_user(env->regs[2], &sc->arm_r2);
1641 __put_user(env->regs[3], &sc->arm_r3);
1642 __put_user(env->regs[4], &sc->arm_r4);
1643 __put_user(env->regs[5], &sc->arm_r5);
1644 __put_user(env->regs[6], &sc->arm_r6);
1645 __put_user(env->regs[7], &sc->arm_r7);
1646 __put_user(env->regs[8], &sc->arm_r8);
1647 __put_user(env->regs[9], &sc->arm_r9);
1648 __put_user(env->regs[10], &sc->arm_r10);
1649 __put_user(env->regs[11], &sc->arm_fp);
1650 __put_user(env->regs[12], &sc->arm_ip);
1651 __put_user(env->regs[13], &sc->arm_sp);
1652 __put_user(env->regs[14], &sc->arm_lr);
1653 __put_user(env->regs[15], &sc->arm_pc);
1654 #ifdef TARGET_CONFIG_CPU_32
1655 __put_user(cpsr_read(env), &sc->arm_cpsr);
1656 #endif
1658 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1659 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1660 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1661 __put_user(mask, &sc->oldmask);
1664 static inline abi_ulong
1665 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1667 unsigned long sp = regs->regs[13];
1670 * This is the X/Open sanctioned signal stack switching.
1672 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1673 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1676 * ATPCS B01 mandates 8-byte alignment
1678 return (sp - framesize) & ~7;
1681 static void
1682 setup_return(CPUARMState *env, struct target_sigaction *ka,
1683 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1685 abi_ulong handler = ka->_sa_handler;
1686 abi_ulong retcode;
1687 int thumb = handler & 1;
1688 uint32_t cpsr = cpsr_read(env);
1690 cpsr &= ~CPSR_IT;
1691 if (thumb) {
1692 cpsr |= CPSR_T;
1693 } else {
1694 cpsr &= ~CPSR_T;
1697 if (ka->sa_flags & TARGET_SA_RESTORER) {
1698 retcode = ka->sa_restorer;
1699 } else {
1700 unsigned int idx = thumb;
1702 if (ka->sa_flags & TARGET_SA_SIGINFO) {
1703 idx += 2;
1706 __put_user(retcodes[idx], rc);
1708 retcode = rc_addr + thumb;
1711 env->regs[0] = usig;
1712 env->regs[13] = frame_addr;
1713 env->regs[14] = retcode;
1714 env->regs[15] = handler & (thumb ? ~1 : ~3);
1715 cpsr_write(env, cpsr, CPSR_IT | CPSR_T, CPSRWriteByInstr);
1718 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1720 int i;
1721 struct target_vfp_sigframe *vfpframe;
1722 vfpframe = (struct target_vfp_sigframe *)regspace;
1723 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1724 __put_user(sizeof(*vfpframe), &vfpframe->size);
1725 for (i = 0; i < 32; i++) {
1726 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1728 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1729 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1730 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1731 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1732 return (abi_ulong*)(vfpframe+1);
1735 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1736 CPUARMState *env)
1738 int i;
1739 struct target_iwmmxt_sigframe *iwmmxtframe;
1740 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1741 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1742 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1743 for (i = 0; i < 16; i++) {
1744 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1746 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1747 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1748 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1749 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1750 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1751 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1752 return (abi_ulong*)(iwmmxtframe+1);
1755 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1756 target_sigset_t *set, CPUARMState *env)
1758 struct target_sigaltstack stack;
1759 int i;
1760 abi_ulong *regspace;
1762 /* Clear all the bits of the ucontext we don't use. */
1763 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1765 memset(&stack, 0, sizeof(stack));
1766 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1767 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1768 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1769 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1771 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1772 /* Save coprocessor signal frame. */
1773 regspace = uc->tuc_regspace;
1774 if (arm_feature(env, ARM_FEATURE_VFP)) {
1775 regspace = setup_sigframe_v2_vfp(regspace, env);
1777 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1778 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1781 /* Write terminating magic word */
1782 __put_user(0, regspace);
1784 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1785 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1789 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1790 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1791 target_sigset_t *set, CPUARMState *regs)
1793 struct sigframe_v1 *frame;
1794 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1795 int i;
1797 trace_user_setup_frame(regs, frame_addr);
1798 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1799 goto sigsegv;
1802 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1804 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1805 __put_user(set->sig[i], &frame->extramask[i - 1]);
1808 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1809 frame_addr + offsetof(struct sigframe_v1, retcode));
1811 unlock_user_struct(frame, frame_addr, 1);
1812 return;
1813 sigsegv:
1814 force_sigsegv(usig);
1817 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1818 target_sigset_t *set, CPUARMState *regs)
1820 struct sigframe_v2 *frame;
1821 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1823 trace_user_setup_frame(regs, frame_addr);
1824 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1825 goto sigsegv;
1828 setup_sigframe_v2(&frame->uc, set, regs);
1830 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1831 frame_addr + offsetof(struct sigframe_v2, retcode));
1833 unlock_user_struct(frame, frame_addr, 1);
1834 return;
1835 sigsegv:
1836 force_sigsegv(usig);
1839 static void setup_frame(int usig, struct target_sigaction *ka,
1840 target_sigset_t *set, CPUARMState *regs)
1842 if (get_osversion() >= 0x020612) {
1843 setup_frame_v2(usig, ka, set, regs);
1844 } else {
1845 setup_frame_v1(usig, ka, set, regs);
1849 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1850 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1851 target_siginfo_t *info,
1852 target_sigset_t *set, CPUARMState *env)
1854 struct rt_sigframe_v1 *frame;
1855 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1856 struct target_sigaltstack stack;
1857 int i;
1858 abi_ulong info_addr, uc_addr;
1860 trace_user_setup_rt_frame(env, frame_addr);
1861 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1862 goto sigsegv;
1865 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1866 __put_user(info_addr, &frame->pinfo);
1867 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1868 __put_user(uc_addr, &frame->puc);
1869 tswap_siginfo(&frame->info, info);
1871 /* Clear all the bits of the ucontext we don't use. */
1872 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1874 memset(&stack, 0, sizeof(stack));
1875 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1876 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1877 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1878 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1880 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1881 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1882 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
1885 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1886 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1888 env->regs[1] = info_addr;
1889 env->regs[2] = uc_addr;
1891 unlock_user_struct(frame, frame_addr, 1);
1892 return;
1893 sigsegv:
1894 force_sigsegv(usig);
1897 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1898 target_siginfo_t *info,
1899 target_sigset_t *set, CPUARMState *env)
1901 struct rt_sigframe_v2 *frame;
1902 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1903 abi_ulong info_addr, uc_addr;
1905 trace_user_setup_rt_frame(env, frame_addr);
1906 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1907 goto sigsegv;
1910 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1911 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1912 tswap_siginfo(&frame->info, info);
1914 setup_sigframe_v2(&frame->uc, set, env);
1916 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1917 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1919 env->regs[1] = info_addr;
1920 env->regs[2] = uc_addr;
1922 unlock_user_struct(frame, frame_addr, 1);
1923 return;
1924 sigsegv:
1925 force_sigsegv(usig);
1928 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1929 target_siginfo_t *info,
1930 target_sigset_t *set, CPUARMState *env)
1932 if (get_osversion() >= 0x020612) {
1933 setup_rt_frame_v2(usig, ka, info, set, env);
1934 } else {
1935 setup_rt_frame_v1(usig, ka, info, set, env);
1939 static int
1940 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1942 int err = 0;
1943 uint32_t cpsr;
1945 __get_user(env->regs[0], &sc->arm_r0);
1946 __get_user(env->regs[1], &sc->arm_r1);
1947 __get_user(env->regs[2], &sc->arm_r2);
1948 __get_user(env->regs[3], &sc->arm_r3);
1949 __get_user(env->regs[4], &sc->arm_r4);
1950 __get_user(env->regs[5], &sc->arm_r5);
1951 __get_user(env->regs[6], &sc->arm_r6);
1952 __get_user(env->regs[7], &sc->arm_r7);
1953 __get_user(env->regs[8], &sc->arm_r8);
1954 __get_user(env->regs[9], &sc->arm_r9);
1955 __get_user(env->regs[10], &sc->arm_r10);
1956 __get_user(env->regs[11], &sc->arm_fp);
1957 __get_user(env->regs[12], &sc->arm_ip);
1958 __get_user(env->regs[13], &sc->arm_sp);
1959 __get_user(env->regs[14], &sc->arm_lr);
1960 __get_user(env->regs[15], &sc->arm_pc);
1961 #ifdef TARGET_CONFIG_CPU_32
1962 __get_user(cpsr, &sc->arm_cpsr);
1963 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC, CPSRWriteByInstr);
1964 #endif
1966 err |= !valid_user_regs(env);
1968 return err;
1971 static long do_sigreturn_v1(CPUARMState *env)
1973 abi_ulong frame_addr;
1974 struct sigframe_v1 *frame = NULL;
1975 target_sigset_t set;
1976 sigset_t host_set;
1977 int i;
1980 * Since we stacked the signal on a 64-bit boundary,
1981 * then 'sp' should be word aligned here. If it's
1982 * not, then the user is trying to mess with us.
1984 frame_addr = env->regs[13];
1985 trace_user_do_sigreturn(env, frame_addr);
1986 if (frame_addr & 7) {
1987 goto badframe;
1990 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1991 goto badframe;
1994 __get_user(set.sig[0], &frame->sc.oldmask);
1995 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1996 __get_user(set.sig[i], &frame->extramask[i - 1]);
1999 target_to_host_sigset_internal(&host_set, &set);
2000 set_sigmask(&host_set);
2002 if (restore_sigcontext(env, &frame->sc)) {
2003 goto badframe;
2006 #if 0
2007 /* Send SIGTRAP if we're single-stepping */
2008 if (ptrace_cancel_bpt(current))
2009 send_sig(SIGTRAP, current, 1);
2010 #endif
2011 unlock_user_struct(frame, frame_addr, 0);
2012 return -TARGET_QEMU_ESIGRETURN;
2014 badframe:
2015 force_sig(TARGET_SIGSEGV);
2016 return -TARGET_QEMU_ESIGRETURN;
2019 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
2021 int i;
2022 abi_ulong magic, sz;
2023 uint32_t fpscr, fpexc;
2024 struct target_vfp_sigframe *vfpframe;
2025 vfpframe = (struct target_vfp_sigframe *)regspace;
2027 __get_user(magic, &vfpframe->magic);
2028 __get_user(sz, &vfpframe->size);
2029 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
2030 return 0;
2032 for (i = 0; i < 32; i++) {
2033 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
2035 __get_user(fpscr, &vfpframe->ufp.fpscr);
2036 vfp_set_fpscr(env, fpscr);
2037 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
2038 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
2039 * and the exception flag is cleared
2041 fpexc |= (1 << 30);
2042 fpexc &= ~((1 << 31) | (1 << 28));
2043 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
2044 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
2045 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
2046 return (abi_ulong*)(vfpframe + 1);
2049 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
2050 abi_ulong *regspace)
2052 int i;
2053 abi_ulong magic, sz;
2054 struct target_iwmmxt_sigframe *iwmmxtframe;
2055 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
2057 __get_user(magic, &iwmmxtframe->magic);
2058 __get_user(sz, &iwmmxtframe->size);
2059 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
2060 return 0;
2062 for (i = 0; i < 16; i++) {
2063 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
2065 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
2066 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
2067 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
2068 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
2069 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
2070 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
2071 return (abi_ulong*)(iwmmxtframe + 1);
2074 static int do_sigframe_return_v2(CPUARMState *env,
2075 target_ulong context_addr,
2076 struct target_ucontext_v2 *uc)
2078 sigset_t host_set;
2079 abi_ulong *regspace;
2081 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
2082 set_sigmask(&host_set);
2084 if (restore_sigcontext(env, &uc->tuc_mcontext))
2085 return 1;
2087 /* Restore coprocessor signal frame */
2088 regspace = uc->tuc_regspace;
2089 if (arm_feature(env, ARM_FEATURE_VFP)) {
2090 regspace = restore_sigframe_v2_vfp(env, regspace);
2091 if (!regspace) {
2092 return 1;
2095 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2096 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
2097 if (!regspace) {
2098 return 1;
2102 if (do_sigaltstack(context_addr
2103 + offsetof(struct target_ucontext_v2, tuc_stack),
2104 0, get_sp_from_cpustate(env)) == -EFAULT) {
2105 return 1;
2108 #if 0
2109 /* Send SIGTRAP if we're single-stepping */
2110 if (ptrace_cancel_bpt(current))
2111 send_sig(SIGTRAP, current, 1);
2112 #endif
2114 return 0;
2117 static long do_sigreturn_v2(CPUARMState *env)
2119 abi_ulong frame_addr;
2120 struct sigframe_v2 *frame = NULL;
2123 * Since we stacked the signal on a 64-bit boundary,
2124 * then 'sp' should be word aligned here. If it's
2125 * not, then the user is trying to mess with us.
2127 frame_addr = env->regs[13];
2128 trace_user_do_sigreturn(env, frame_addr);
2129 if (frame_addr & 7) {
2130 goto badframe;
2133 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2134 goto badframe;
2137 if (do_sigframe_return_v2(env,
2138 frame_addr
2139 + offsetof(struct sigframe_v2, uc),
2140 &frame->uc)) {
2141 goto badframe;
2144 unlock_user_struct(frame, frame_addr, 0);
2145 return -TARGET_QEMU_ESIGRETURN;
2147 badframe:
2148 unlock_user_struct(frame, frame_addr, 0);
2149 force_sig(TARGET_SIGSEGV);
2150 return -TARGET_QEMU_ESIGRETURN;
2153 long do_sigreturn(CPUARMState *env)
2155 if (get_osversion() >= 0x020612) {
2156 return do_sigreturn_v2(env);
2157 } else {
2158 return do_sigreturn_v1(env);
2162 static long do_rt_sigreturn_v1(CPUARMState *env)
2164 abi_ulong frame_addr;
2165 struct rt_sigframe_v1 *frame = NULL;
2166 sigset_t host_set;
2169 * Since we stacked the signal on a 64-bit boundary,
2170 * then 'sp' should be word aligned here. If it's
2171 * not, then the user is trying to mess with us.
2173 frame_addr = env->regs[13];
2174 trace_user_do_rt_sigreturn(env, frame_addr);
2175 if (frame_addr & 7) {
2176 goto badframe;
2179 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2180 goto badframe;
2183 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2184 set_sigmask(&host_set);
2186 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
2187 goto badframe;
2190 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2191 goto badframe;
2193 #if 0
2194 /* Send SIGTRAP if we're single-stepping */
2195 if (ptrace_cancel_bpt(current))
2196 send_sig(SIGTRAP, current, 1);
2197 #endif
2198 unlock_user_struct(frame, frame_addr, 0);
2199 return -TARGET_QEMU_ESIGRETURN;
2201 badframe:
2202 unlock_user_struct(frame, frame_addr, 0);
2203 force_sig(TARGET_SIGSEGV);
2204 return -TARGET_QEMU_ESIGRETURN;
2207 static long do_rt_sigreturn_v2(CPUARMState *env)
2209 abi_ulong frame_addr;
2210 struct rt_sigframe_v2 *frame = NULL;
2213 * Since we stacked the signal on a 64-bit boundary,
2214 * then 'sp' should be word aligned here. If it's
2215 * not, then the user is trying to mess with us.
2217 frame_addr = env->regs[13];
2218 trace_user_do_rt_sigreturn(env, frame_addr);
2219 if (frame_addr & 7) {
2220 goto badframe;
2223 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2224 goto badframe;
2227 if (do_sigframe_return_v2(env,
2228 frame_addr
2229 + offsetof(struct rt_sigframe_v2, uc),
2230 &frame->uc)) {
2231 goto badframe;
2234 unlock_user_struct(frame, frame_addr, 0);
2235 return -TARGET_QEMU_ESIGRETURN;
2237 badframe:
2238 unlock_user_struct(frame, frame_addr, 0);
2239 force_sig(TARGET_SIGSEGV);
2240 return -TARGET_QEMU_ESIGRETURN;
2243 long do_rt_sigreturn(CPUARMState *env)
2245 if (get_osversion() >= 0x020612) {
2246 return do_rt_sigreturn_v2(env);
2247 } else {
2248 return do_rt_sigreturn_v1(env);
2252 #elif defined(TARGET_SPARC)
2254 #define __SUNOS_MAXWIN 31
2256 /* This is what SunOS does, so shall I. */
2257 struct target_sigcontext {
2258 abi_ulong sigc_onstack; /* state to restore */
2260 abi_ulong sigc_mask; /* sigmask to restore */
2261 abi_ulong sigc_sp; /* stack pointer */
2262 abi_ulong sigc_pc; /* program counter */
2263 abi_ulong sigc_npc; /* next program counter */
2264 abi_ulong sigc_psr; /* for condition codes etc */
2265 abi_ulong sigc_g1; /* User uses these two registers */
2266 abi_ulong sigc_o0; /* within the trampoline code. */
2268 /* Now comes information regarding the users window set
2269 * at the time of the signal.
2271 abi_ulong sigc_oswins; /* outstanding windows */
2273 /* stack ptrs for each regwin buf */
2274 char *sigc_spbuf[__SUNOS_MAXWIN];
2276 /* Windows to restore after signal */
2277 struct {
2278 abi_ulong locals[8];
2279 abi_ulong ins[8];
2280 } sigc_wbuf[__SUNOS_MAXWIN];
2282 /* A Sparc stack frame */
2283 struct sparc_stackf {
2284 abi_ulong locals[8];
2285 abi_ulong ins[8];
2286 /* It's simpler to treat fp and callers_pc as elements of ins[]
2287 * since we never need to access them ourselves.
2289 char *structptr;
2290 abi_ulong xargs[6];
2291 abi_ulong xxargs[1];
2294 typedef struct {
2295 struct {
2296 abi_ulong psr;
2297 abi_ulong pc;
2298 abi_ulong npc;
2299 abi_ulong y;
2300 abi_ulong u_regs[16]; /* globals and ins */
2301 } si_regs;
2302 int si_mask;
2303 } __siginfo_t;
2305 typedef struct {
2306 abi_ulong si_float_regs[32];
2307 unsigned long si_fsr;
2308 unsigned long si_fpqdepth;
2309 struct {
2310 unsigned long *insn_addr;
2311 unsigned long insn;
2312 } si_fpqueue [16];
2313 } qemu_siginfo_fpu_t;
2316 struct target_signal_frame {
2317 struct sparc_stackf ss;
2318 __siginfo_t info;
2319 abi_ulong fpu_save;
2320 abi_ulong insns[2] __attribute__ ((aligned (8)));
2321 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2322 abi_ulong extra_size; /* Should be 0 */
2323 qemu_siginfo_fpu_t fpu_state;
2325 struct target_rt_signal_frame {
2326 struct sparc_stackf ss;
2327 siginfo_t info;
2328 abi_ulong regs[20];
2329 sigset_t mask;
2330 abi_ulong fpu_save;
2331 unsigned int insns[2];
2332 stack_t stack;
2333 unsigned int extra_size; /* Should be 0 */
2334 qemu_siginfo_fpu_t fpu_state;
2337 #define UREG_O0 16
2338 #define UREG_O6 22
2339 #define UREG_I0 0
2340 #define UREG_I1 1
2341 #define UREG_I2 2
2342 #define UREG_I3 3
2343 #define UREG_I4 4
2344 #define UREG_I5 5
2345 #define UREG_I6 6
2346 #define UREG_I7 7
2347 #define UREG_L0 8
2348 #define UREG_FP UREG_I6
2349 #define UREG_SP UREG_O6
2351 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2352 CPUSPARCState *env,
2353 unsigned long framesize)
2355 abi_ulong sp;
2357 sp = env->regwptr[UREG_FP];
2359 /* This is the X/Open sanctioned signal stack switching. */
2360 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2361 if (!on_sig_stack(sp)
2362 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7)) {
2363 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2366 return sp - framesize;
2369 static int
2370 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2372 int err = 0, i;
2374 __put_user(env->psr, &si->si_regs.psr);
2375 __put_user(env->pc, &si->si_regs.pc);
2376 __put_user(env->npc, &si->si_regs.npc);
2377 __put_user(env->y, &si->si_regs.y);
2378 for (i=0; i < 8; i++) {
2379 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2381 for (i=0; i < 8; i++) {
2382 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2384 __put_user(mask, &si->si_mask);
2385 return err;
2388 #if 0
2389 static int
2390 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2391 CPUSPARCState *env, unsigned long mask)
2393 int err = 0;
2395 __put_user(mask, &sc->sigc_mask);
2396 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2397 __put_user(env->pc, &sc->sigc_pc);
2398 __put_user(env->npc, &sc->sigc_npc);
2399 __put_user(env->psr, &sc->sigc_psr);
2400 __put_user(env->gregs[1], &sc->sigc_g1);
2401 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2403 return err;
2405 #endif
2406 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2408 static void setup_frame(int sig, struct target_sigaction *ka,
2409 target_sigset_t *set, CPUSPARCState *env)
2411 abi_ulong sf_addr;
2412 struct target_signal_frame *sf;
2413 int sigframe_size, err, i;
2415 /* 1. Make sure everything is clean */
2416 //synchronize_user_stack();
2418 sigframe_size = NF_ALIGNEDSZ;
2419 sf_addr = get_sigframe(ka, env, sigframe_size);
2420 trace_user_setup_frame(env, sf_addr);
2422 sf = lock_user(VERIFY_WRITE, sf_addr,
2423 sizeof(struct target_signal_frame), 0);
2424 if (!sf) {
2425 goto sigsegv;
2427 #if 0
2428 if (invalid_frame_pointer(sf, sigframe_size))
2429 goto sigill_and_return;
2430 #endif
2431 /* 2. Save the current process state */
2432 err = setup___siginfo(&sf->info, env, set->sig[0]);
2433 __put_user(0, &sf->extra_size);
2435 //save_fpu_state(regs, &sf->fpu_state);
2436 //__put_user(&sf->fpu_state, &sf->fpu_save);
2438 __put_user(set->sig[0], &sf->info.si_mask);
2439 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2440 __put_user(set->sig[i + 1], &sf->extramask[i]);
2443 for (i = 0; i < 8; i++) {
2444 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2446 for (i = 0; i < 8; i++) {
2447 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2449 if (err)
2450 goto sigsegv;
2452 /* 3. signal handler back-trampoline and parameters */
2453 env->regwptr[UREG_FP] = sf_addr;
2454 env->regwptr[UREG_I0] = sig;
2455 env->regwptr[UREG_I1] = sf_addr +
2456 offsetof(struct target_signal_frame, info);
2457 env->regwptr[UREG_I2] = sf_addr +
2458 offsetof(struct target_signal_frame, info);
2460 /* 4. signal handler */
2461 env->pc = ka->_sa_handler;
2462 env->npc = (env->pc + 4);
2463 /* 5. return to kernel instructions */
2464 if (ka->sa_restorer) {
2465 env->regwptr[UREG_I7] = ka->sa_restorer;
2466 } else {
2467 uint32_t val32;
2469 env->regwptr[UREG_I7] = sf_addr +
2470 offsetof(struct target_signal_frame, insns) - 2 * 4;
2472 /* mov __NR_sigreturn, %g1 */
2473 val32 = 0x821020d8;
2474 __put_user(val32, &sf->insns[0]);
2476 /* t 0x10 */
2477 val32 = 0x91d02010;
2478 __put_user(val32, &sf->insns[1]);
2479 if (err)
2480 goto sigsegv;
2482 /* Flush instruction space. */
2483 // flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2484 // tb_flush(env);
2486 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2487 return;
2488 #if 0
2489 sigill_and_return:
2490 force_sig(TARGET_SIGILL);
2491 #endif
2492 sigsegv:
2493 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2494 force_sigsegv(sig);
2497 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2498 target_siginfo_t *info,
2499 target_sigset_t *set, CPUSPARCState *env)
2501 fprintf(stderr, "setup_rt_frame: not implemented\n");
2504 long do_sigreturn(CPUSPARCState *env)
2506 abi_ulong sf_addr;
2507 struct target_signal_frame *sf;
2508 uint32_t up_psr, pc, npc;
2509 target_sigset_t set;
2510 sigset_t host_set;
2511 int err=0, i;
2513 sf_addr = env->regwptr[UREG_FP];
2514 trace_user_do_sigreturn(env, sf_addr);
2515 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
2516 goto segv_and_exit;
2519 /* 1. Make sure we are not getting garbage from the user */
2521 if (sf_addr & 3)
2522 goto segv_and_exit;
2524 __get_user(pc, &sf->info.si_regs.pc);
2525 __get_user(npc, &sf->info.si_regs.npc);
2527 if ((pc | npc) & 3) {
2528 goto segv_and_exit;
2531 /* 2. Restore the state */
2532 __get_user(up_psr, &sf->info.si_regs.psr);
2534 /* User can only change condition codes and FPU enabling in %psr. */
2535 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2536 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2538 env->pc = pc;
2539 env->npc = npc;
2540 __get_user(env->y, &sf->info.si_regs.y);
2541 for (i=0; i < 8; i++) {
2542 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2544 for (i=0; i < 8; i++) {
2545 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2548 /* FIXME: implement FPU save/restore:
2549 * __get_user(fpu_save, &sf->fpu_save);
2550 * if (fpu_save)
2551 * err |= restore_fpu_state(env, fpu_save);
2554 /* This is pretty much atomic, no amount locking would prevent
2555 * the races which exist anyways.
2557 __get_user(set.sig[0], &sf->info.si_mask);
2558 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2559 __get_user(set.sig[i], &sf->extramask[i - 1]);
2562 target_to_host_sigset_internal(&host_set, &set);
2563 set_sigmask(&host_set);
2565 if (err) {
2566 goto segv_and_exit;
2568 unlock_user_struct(sf, sf_addr, 0);
2569 return -TARGET_QEMU_ESIGRETURN;
2571 segv_and_exit:
2572 unlock_user_struct(sf, sf_addr, 0);
2573 force_sig(TARGET_SIGSEGV);
2574 return -TARGET_QEMU_ESIGRETURN;
2577 long do_rt_sigreturn(CPUSPARCState *env)
2579 trace_user_do_rt_sigreturn(env, 0);
2580 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2581 return -TARGET_ENOSYS;
2584 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2585 #define MC_TSTATE 0
2586 #define MC_PC 1
2587 #define MC_NPC 2
2588 #define MC_Y 3
2589 #define MC_G1 4
2590 #define MC_G2 5
2591 #define MC_G3 6
2592 #define MC_G4 7
2593 #define MC_G5 8
2594 #define MC_G6 9
2595 #define MC_G7 10
2596 #define MC_O0 11
2597 #define MC_O1 12
2598 #define MC_O2 13
2599 #define MC_O3 14
2600 #define MC_O4 15
2601 #define MC_O5 16
2602 #define MC_O6 17
2603 #define MC_O7 18
2604 #define MC_NGREG 19
2606 typedef abi_ulong target_mc_greg_t;
2607 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2609 struct target_mc_fq {
2610 abi_ulong *mcfq_addr;
2611 uint32_t mcfq_insn;
2614 struct target_mc_fpu {
2615 union {
2616 uint32_t sregs[32];
2617 uint64_t dregs[32];
2618 //uint128_t qregs[16];
2619 } mcfpu_fregs;
2620 abi_ulong mcfpu_fsr;
2621 abi_ulong mcfpu_fprs;
2622 abi_ulong mcfpu_gsr;
2623 struct target_mc_fq *mcfpu_fq;
2624 unsigned char mcfpu_qcnt;
2625 unsigned char mcfpu_qentsz;
2626 unsigned char mcfpu_enab;
2628 typedef struct target_mc_fpu target_mc_fpu_t;
2630 typedef struct {
2631 target_mc_gregset_t mc_gregs;
2632 target_mc_greg_t mc_fp;
2633 target_mc_greg_t mc_i7;
2634 target_mc_fpu_t mc_fpregs;
2635 } target_mcontext_t;
2637 struct target_ucontext {
2638 struct target_ucontext *tuc_link;
2639 abi_ulong tuc_flags;
2640 target_sigset_t tuc_sigmask;
2641 target_mcontext_t tuc_mcontext;
2644 /* A V9 register window */
2645 struct target_reg_window {
2646 abi_ulong locals[8];
2647 abi_ulong ins[8];
2650 #define TARGET_STACK_BIAS 2047
2652 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2653 void sparc64_set_context(CPUSPARCState *env)
2655 abi_ulong ucp_addr;
2656 struct target_ucontext *ucp;
2657 target_mc_gregset_t *grp;
2658 abi_ulong pc, npc, tstate;
2659 abi_ulong fp, i7, w_addr;
2660 unsigned int i;
2662 ucp_addr = env->regwptr[UREG_I0];
2663 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
2664 goto do_sigsegv;
2666 grp = &ucp->tuc_mcontext.mc_gregs;
2667 __get_user(pc, &((*grp)[MC_PC]));
2668 __get_user(npc, &((*grp)[MC_NPC]));
2669 if ((pc | npc) & 3) {
2670 goto do_sigsegv;
2672 if (env->regwptr[UREG_I1]) {
2673 target_sigset_t target_set;
2674 sigset_t set;
2676 if (TARGET_NSIG_WORDS == 1) {
2677 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
2678 } else {
2679 abi_ulong *src, *dst;
2680 src = ucp->tuc_sigmask.sig;
2681 dst = target_set.sig;
2682 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2683 __get_user(*dst, src);
2686 target_to_host_sigset_internal(&set, &target_set);
2687 set_sigmask(&set);
2689 env->pc = pc;
2690 env->npc = npc;
2691 __get_user(env->y, &((*grp)[MC_Y]));
2692 __get_user(tstate, &((*grp)[MC_TSTATE]));
2693 env->asi = (tstate >> 24) & 0xff;
2694 cpu_put_ccr(env, tstate >> 32);
2695 cpu_put_cwp64(env, tstate & 0x1f);
2696 __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2697 __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2698 __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2699 __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2700 __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2701 __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2702 __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2703 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2704 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2705 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2706 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2707 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2708 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2709 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2710 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2712 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2713 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2715 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2716 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2717 abi_ulong) != 0) {
2718 goto do_sigsegv;
2720 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2721 abi_ulong) != 0) {
2722 goto do_sigsegv;
2724 /* FIXME this does not match how the kernel handles the FPU in
2725 * its sparc64_set_context implementation. In particular the FPU
2726 * is only restored if fenab is non-zero in:
2727 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2729 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2731 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2732 for (i = 0; i < 64; i++, src++) {
2733 if (i & 1) {
2734 __get_user(env->fpr[i/2].l.lower, src);
2735 } else {
2736 __get_user(env->fpr[i/2].l.upper, src);
2740 __get_user(env->fsr,
2741 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2742 __get_user(env->gsr,
2743 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2744 unlock_user_struct(ucp, ucp_addr, 0);
2745 return;
2746 do_sigsegv:
2747 unlock_user_struct(ucp, ucp_addr, 0);
2748 force_sig(TARGET_SIGSEGV);
2751 void sparc64_get_context(CPUSPARCState *env)
2753 abi_ulong ucp_addr;
2754 struct target_ucontext *ucp;
2755 target_mc_gregset_t *grp;
2756 target_mcontext_t *mcp;
2757 abi_ulong fp, i7, w_addr;
2758 int err;
2759 unsigned int i;
2760 target_sigset_t target_set;
2761 sigset_t set;
2763 ucp_addr = env->regwptr[UREG_I0];
2764 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
2765 goto do_sigsegv;
2768 mcp = &ucp->tuc_mcontext;
2769 grp = &mcp->mc_gregs;
2771 /* Skip over the trap instruction, first. */
2772 env->pc = env->npc;
2773 env->npc += 4;
2775 /* If we're only reading the signal mask then do_sigprocmask()
2776 * is guaranteed not to fail, which is important because we don't
2777 * have any way to signal a failure or restart this operation since
2778 * this is not a normal syscall.
2780 err = do_sigprocmask(0, NULL, &set);
2781 assert(err == 0);
2782 host_to_target_sigset_internal(&target_set, &set);
2783 if (TARGET_NSIG_WORDS == 1) {
2784 __put_user(target_set.sig[0],
2785 (abi_ulong *)&ucp->tuc_sigmask);
2786 } else {
2787 abi_ulong *src, *dst;
2788 src = target_set.sig;
2789 dst = ucp->tuc_sigmask.sig;
2790 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2791 __put_user(*src, dst);
2793 if (err)
2794 goto do_sigsegv;
2797 /* XXX: tstate must be saved properly */
2798 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2799 __put_user(env->pc, &((*grp)[MC_PC]));
2800 __put_user(env->npc, &((*grp)[MC_NPC]));
2801 __put_user(env->y, &((*grp)[MC_Y]));
2802 __put_user(env->gregs[1], &((*grp)[MC_G1]));
2803 __put_user(env->gregs[2], &((*grp)[MC_G2]));
2804 __put_user(env->gregs[3], &((*grp)[MC_G3]));
2805 __put_user(env->gregs[4], &((*grp)[MC_G4]));
2806 __put_user(env->gregs[5], &((*grp)[MC_G5]));
2807 __put_user(env->gregs[6], &((*grp)[MC_G6]));
2808 __put_user(env->gregs[7], &((*grp)[MC_G7]));
2809 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2810 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2811 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2812 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2813 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2814 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2815 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2816 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2818 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2819 fp = i7 = 0;
2820 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2821 abi_ulong) != 0) {
2822 goto do_sigsegv;
2824 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2825 abi_ulong) != 0) {
2826 goto do_sigsegv;
2828 __put_user(fp, &(mcp->mc_fp));
2829 __put_user(i7, &(mcp->mc_i7));
2832 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2833 for (i = 0; i < 64; i++, dst++) {
2834 if (i & 1) {
2835 __put_user(env->fpr[i/2].l.lower, dst);
2836 } else {
2837 __put_user(env->fpr[i/2].l.upper, dst);
2841 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2842 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2843 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2845 if (err)
2846 goto do_sigsegv;
2847 unlock_user_struct(ucp, ucp_addr, 1);
2848 return;
2849 do_sigsegv:
2850 unlock_user_struct(ucp, ucp_addr, 1);
2851 force_sig(TARGET_SIGSEGV);
2853 #endif
2854 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2856 # if defined(TARGET_ABI_MIPSO32)
2857 struct target_sigcontext {
2858 uint32_t sc_regmask; /* Unused */
2859 uint32_t sc_status;
2860 uint64_t sc_pc;
2861 uint64_t sc_regs[32];
2862 uint64_t sc_fpregs[32];
2863 uint32_t sc_ownedfp; /* Unused */
2864 uint32_t sc_fpc_csr;
2865 uint32_t sc_fpc_eir; /* Unused */
2866 uint32_t sc_used_math;
2867 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2868 uint32_t pad0;
2869 uint64_t sc_mdhi;
2870 uint64_t sc_mdlo;
2871 target_ulong sc_hi1; /* Was sc_cause */
2872 target_ulong sc_lo1; /* Was sc_badvaddr */
2873 target_ulong sc_hi2; /* Was sc_sigset[4] */
2874 target_ulong sc_lo2;
2875 target_ulong sc_hi3;
2876 target_ulong sc_lo3;
2878 # else /* N32 || N64 */
2879 struct target_sigcontext {
2880 uint64_t sc_regs[32];
2881 uint64_t sc_fpregs[32];
2882 uint64_t sc_mdhi;
2883 uint64_t sc_hi1;
2884 uint64_t sc_hi2;
2885 uint64_t sc_hi3;
2886 uint64_t sc_mdlo;
2887 uint64_t sc_lo1;
2888 uint64_t sc_lo2;
2889 uint64_t sc_lo3;
2890 uint64_t sc_pc;
2891 uint32_t sc_fpc_csr;
2892 uint32_t sc_used_math;
2893 uint32_t sc_dsp;
2894 uint32_t sc_reserved;
2896 # endif /* O32 */
2898 struct sigframe {
2899 uint32_t sf_ass[4]; /* argument save space for o32 */
2900 uint32_t sf_code[2]; /* signal trampoline */
2901 struct target_sigcontext sf_sc;
2902 target_sigset_t sf_mask;
2905 struct target_ucontext {
2906 target_ulong tuc_flags;
2907 target_ulong tuc_link;
2908 target_stack_t tuc_stack;
2909 target_ulong pad0;
2910 struct target_sigcontext tuc_mcontext;
2911 target_sigset_t tuc_sigmask;
2914 struct target_rt_sigframe {
2915 uint32_t rs_ass[4]; /* argument save space for o32 */
2916 uint32_t rs_code[2]; /* signal trampoline */
2917 struct target_siginfo rs_info;
2918 struct target_ucontext rs_uc;
2921 /* Install trampoline to jump back from signal handler */
2922 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2924 int err = 0;
2927 * Set up the return code ...
2929 * li v0, __NR__foo_sigreturn
2930 * syscall
2933 __put_user(0x24020000 + syscall, tramp + 0);
2934 __put_user(0x0000000c , tramp + 1);
2935 return err;
2938 static inline void setup_sigcontext(CPUMIPSState *regs,
2939 struct target_sigcontext *sc)
2941 int i;
2943 __put_user(exception_resume_pc(regs), &sc->sc_pc);
2944 regs->hflags &= ~MIPS_HFLAG_BMASK;
2946 __put_user(0, &sc->sc_regs[0]);
2947 for (i = 1; i < 32; ++i) {
2948 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2951 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2952 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2954 /* Rather than checking for dsp existence, always copy. The storage
2955 would just be garbage otherwise. */
2956 __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2957 __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2958 __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2959 __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2960 __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2961 __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2963 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2964 __put_user(dsp, &sc->sc_dsp);
2967 __put_user(1, &sc->sc_used_math);
2969 for (i = 0; i < 32; ++i) {
2970 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2974 static inline void
2975 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2977 int i;
2979 __get_user(regs->CP0_EPC, &sc->sc_pc);
2981 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2982 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2984 for (i = 1; i < 32; ++i) {
2985 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2988 __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2989 __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2990 __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2991 __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2992 __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2993 __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2995 uint32_t dsp;
2996 __get_user(dsp, &sc->sc_dsp);
2997 cpu_wrdsp(dsp, 0x3ff, regs);
3000 for (i = 0; i < 32; ++i) {
3001 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
3006 * Determine which stack to use..
3008 static inline abi_ulong
3009 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
3011 unsigned long sp;
3013 /* Default to using normal stack */
3014 sp = regs->active_tc.gpr[29];
3017 * FPU emulator may have its own trampoline active just
3018 * above the user stack, 16-bytes before the next lowest
3019 * 16 byte boundary. Try to avoid trashing it.
3021 sp -= 32;
3023 /* This is the X/Open sanctioned signal stack switching. */
3024 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
3025 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3028 return (sp - frame_size) & ~7;
3031 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
3033 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
3034 env->hflags &= ~MIPS_HFLAG_M16;
3035 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
3036 env->active_tc.PC &= ~(target_ulong) 1;
3040 # if defined(TARGET_ABI_MIPSO32)
3041 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
3042 static void setup_frame(int sig, struct target_sigaction * ka,
3043 target_sigset_t *set, CPUMIPSState *regs)
3045 struct sigframe *frame;
3046 abi_ulong frame_addr;
3047 int i;
3049 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
3050 trace_user_setup_frame(regs, frame_addr);
3051 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3052 goto give_sigsegv;
3055 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
3057 setup_sigcontext(regs, &frame->sf_sc);
3059 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3060 __put_user(set->sig[i], &frame->sf_mask.sig[i]);
3064 * Arguments to signal handler:
3066 * a0 = signal number
3067 * a1 = 0 (should be cause)
3068 * a2 = pointer to struct sigcontext
3070 * $25 and PC point to the signal handler, $29 points to the
3071 * struct sigframe.
3073 regs->active_tc.gpr[ 4] = sig;
3074 regs->active_tc.gpr[ 5] = 0;
3075 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
3076 regs->active_tc.gpr[29] = frame_addr;
3077 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
3078 /* The original kernel code sets CP0_EPC to the handler
3079 * since it returns to userland using eret
3080 * we cannot do this here, and we must set PC directly */
3081 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
3082 mips_set_hflags_isa_mode_from_pc(regs);
3083 unlock_user_struct(frame, frame_addr, 1);
3084 return;
3086 give_sigsegv:
3087 force_sigsegv(sig);
3090 long do_sigreturn(CPUMIPSState *regs)
3092 struct sigframe *frame;
3093 abi_ulong frame_addr;
3094 sigset_t blocked;
3095 target_sigset_t target_set;
3096 int i;
3098 frame_addr = regs->active_tc.gpr[29];
3099 trace_user_do_sigreturn(regs, frame_addr);
3100 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3101 goto badframe;
3103 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3104 __get_user(target_set.sig[i], &frame->sf_mask.sig[i]);
3107 target_to_host_sigset_internal(&blocked, &target_set);
3108 set_sigmask(&blocked);
3110 restore_sigcontext(regs, &frame->sf_sc);
3112 #if 0
3114 * Don't let your children do this ...
3116 __asm__ __volatile__(
3117 "move\t$29, %0\n\t"
3118 "j\tsyscall_exit"
3119 :/* no outputs */
3120 :"r" (&regs));
3121 /* Unreached */
3122 #endif
3124 regs->active_tc.PC = regs->CP0_EPC;
3125 mips_set_hflags_isa_mode_from_pc(regs);
3126 /* I am not sure this is right, but it seems to work
3127 * maybe a problem with nested signals ? */
3128 regs->CP0_EPC = 0;
3129 return -TARGET_QEMU_ESIGRETURN;
3131 badframe:
3132 force_sig(TARGET_SIGSEGV);
3133 return -TARGET_QEMU_ESIGRETURN;
3135 # endif /* O32 */
3137 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3138 target_siginfo_t *info,
3139 target_sigset_t *set, CPUMIPSState *env)
3141 struct target_rt_sigframe *frame;
3142 abi_ulong frame_addr;
3143 int i;
3145 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3146 trace_user_setup_rt_frame(env, frame_addr);
3147 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3148 goto give_sigsegv;
3151 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3153 tswap_siginfo(&frame->rs_info, info);
3155 __put_user(0, &frame->rs_uc.tuc_flags);
3156 __put_user(0, &frame->rs_uc.tuc_link);
3157 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3158 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3159 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3160 &frame->rs_uc.tuc_stack.ss_flags);
3162 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3164 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3165 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3169 * Arguments to signal handler:
3171 * a0 = signal number
3172 * a1 = pointer to siginfo_t
3173 * a2 = pointer to struct ucontext
3175 * $25 and PC point to the signal handler, $29 points to the
3176 * struct sigframe.
3178 env->active_tc.gpr[ 4] = sig;
3179 env->active_tc.gpr[ 5] = frame_addr
3180 + offsetof(struct target_rt_sigframe, rs_info);
3181 env->active_tc.gpr[ 6] = frame_addr
3182 + offsetof(struct target_rt_sigframe, rs_uc);
3183 env->active_tc.gpr[29] = frame_addr;
3184 env->active_tc.gpr[31] = frame_addr
3185 + offsetof(struct target_rt_sigframe, rs_code);
3186 /* The original kernel code sets CP0_EPC to the handler
3187 * since it returns to userland using eret
3188 * we cannot do this here, and we must set PC directly */
3189 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3190 mips_set_hflags_isa_mode_from_pc(env);
3191 unlock_user_struct(frame, frame_addr, 1);
3192 return;
3194 give_sigsegv:
3195 unlock_user_struct(frame, frame_addr, 1);
3196 force_sigsegv(sig);
3199 long do_rt_sigreturn(CPUMIPSState *env)
3201 struct target_rt_sigframe *frame;
3202 abi_ulong frame_addr;
3203 sigset_t blocked;
3205 frame_addr = env->active_tc.gpr[29];
3206 trace_user_do_rt_sigreturn(env, frame_addr);
3207 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3208 goto badframe;
3211 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3212 set_sigmask(&blocked);
3214 restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3216 if (do_sigaltstack(frame_addr +
3217 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3218 0, get_sp_from_cpustate(env)) == -EFAULT)
3219 goto badframe;
3221 env->active_tc.PC = env->CP0_EPC;
3222 mips_set_hflags_isa_mode_from_pc(env);
3223 /* I am not sure this is right, but it seems to work
3224 * maybe a problem with nested signals ? */
3225 env->CP0_EPC = 0;
3226 return -TARGET_QEMU_ESIGRETURN;
3228 badframe:
3229 force_sig(TARGET_SIGSEGV);
3230 return -TARGET_QEMU_ESIGRETURN;
3233 #elif defined(TARGET_SH4)
3236 * code and data structures from linux kernel:
3237 * include/asm-sh/sigcontext.h
3238 * arch/sh/kernel/signal.c
3241 struct target_sigcontext {
3242 target_ulong oldmask;
3244 /* CPU registers */
3245 target_ulong sc_gregs[16];
3246 target_ulong sc_pc;
3247 target_ulong sc_pr;
3248 target_ulong sc_sr;
3249 target_ulong sc_gbr;
3250 target_ulong sc_mach;
3251 target_ulong sc_macl;
3253 /* FPU registers */
3254 target_ulong sc_fpregs[16];
3255 target_ulong sc_xfpregs[16];
3256 unsigned int sc_fpscr;
3257 unsigned int sc_fpul;
3258 unsigned int sc_ownedfp;
3261 struct target_sigframe
3263 struct target_sigcontext sc;
3264 target_ulong extramask[TARGET_NSIG_WORDS-1];
3265 uint16_t retcode[3];
3269 struct target_ucontext {
3270 target_ulong tuc_flags;
3271 struct target_ucontext *tuc_link;
3272 target_stack_t tuc_stack;
3273 struct target_sigcontext tuc_mcontext;
3274 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3277 struct target_rt_sigframe
3279 struct target_siginfo info;
3280 struct target_ucontext uc;
3281 uint16_t retcode[3];
3285 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3286 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3288 static abi_ulong get_sigframe(struct target_sigaction *ka,
3289 unsigned long sp, size_t frame_size)
3291 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
3292 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3295 return (sp - frame_size) & -8ul;
3298 static void setup_sigcontext(struct target_sigcontext *sc,
3299 CPUSH4State *regs, unsigned long mask)
3301 int i;
3303 #define COPY(x) __put_user(regs->x, &sc->sc_##x)
3304 COPY(gregs[0]); COPY(gregs[1]);
3305 COPY(gregs[2]); COPY(gregs[3]);
3306 COPY(gregs[4]); COPY(gregs[5]);
3307 COPY(gregs[6]); COPY(gregs[7]);
3308 COPY(gregs[8]); COPY(gregs[9]);
3309 COPY(gregs[10]); COPY(gregs[11]);
3310 COPY(gregs[12]); COPY(gregs[13]);
3311 COPY(gregs[14]); COPY(gregs[15]);
3312 COPY(gbr); COPY(mach);
3313 COPY(macl); COPY(pr);
3314 COPY(sr); COPY(pc);
3315 #undef COPY
3317 for (i=0; i<16; i++) {
3318 __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3320 __put_user(regs->fpscr, &sc->sc_fpscr);
3321 __put_user(regs->fpul, &sc->sc_fpul);
3323 /* non-iBCS2 extensions.. */
3324 __put_user(mask, &sc->oldmask);
3327 static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc)
3329 int i;
3331 #define COPY(x) __get_user(regs->x, &sc->sc_##x)
3332 COPY(gregs[0]); COPY(gregs[1]);
3333 COPY(gregs[2]); COPY(gregs[3]);
3334 COPY(gregs[4]); COPY(gregs[5]);
3335 COPY(gregs[6]); COPY(gregs[7]);
3336 COPY(gregs[8]); COPY(gregs[9]);
3337 COPY(gregs[10]); COPY(gregs[11]);
3338 COPY(gregs[12]); COPY(gregs[13]);
3339 COPY(gregs[14]); COPY(gregs[15]);
3340 COPY(gbr); COPY(mach);
3341 COPY(macl); COPY(pr);
3342 COPY(sr); COPY(pc);
3343 #undef COPY
3345 for (i=0; i<16; i++) {
3346 __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3348 __get_user(regs->fpscr, &sc->sc_fpscr);
3349 __get_user(regs->fpul, &sc->sc_fpul);
3351 regs->tra = -1; /* disable syscall checks */
3354 static void setup_frame(int sig, struct target_sigaction *ka,
3355 target_sigset_t *set, CPUSH4State *regs)
3357 struct target_sigframe *frame;
3358 abi_ulong frame_addr;
3359 int i;
3361 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3362 trace_user_setup_frame(regs, frame_addr);
3363 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3364 goto give_sigsegv;
3367 setup_sigcontext(&frame->sc, regs, set->sig[0]);
3369 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3370 __put_user(set->sig[i + 1], &frame->extramask[i]);
3373 /* Set up to return from userspace. If provided, use a stub
3374 already in userspace. */
3375 if (ka->sa_flags & TARGET_SA_RESTORER) {
3376 regs->pr = (unsigned long) ka->sa_restorer;
3377 } else {
3378 /* Generate return code (system call to sigreturn) */
3379 abi_ulong retcode_addr = frame_addr +
3380 offsetof(struct target_sigframe, retcode);
3381 __put_user(MOVW(2), &frame->retcode[0]);
3382 __put_user(TRAP_NOARG, &frame->retcode[1]);
3383 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3384 regs->pr = (unsigned long) retcode_addr;
3387 /* Set up registers for signal handler */
3388 regs->gregs[15] = frame_addr;
3389 regs->gregs[4] = sig; /* Arg for signal handler */
3390 regs->gregs[5] = 0;
3391 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3392 regs->pc = (unsigned long) ka->_sa_handler;
3394 unlock_user_struct(frame, frame_addr, 1);
3395 return;
3397 give_sigsegv:
3398 unlock_user_struct(frame, frame_addr, 1);
3399 force_sigsegv(sig);
3402 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3403 target_siginfo_t *info,
3404 target_sigset_t *set, CPUSH4State *regs)
3406 struct target_rt_sigframe *frame;
3407 abi_ulong frame_addr;
3408 int i;
3410 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3411 trace_user_setup_rt_frame(regs, frame_addr);
3412 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3413 goto give_sigsegv;
3416 tswap_siginfo(&frame->info, info);
3418 /* Create the ucontext. */
3419 __put_user(0, &frame->uc.tuc_flags);
3420 __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3421 __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3422 &frame->uc.tuc_stack.ss_sp);
3423 __put_user(sas_ss_flags(regs->gregs[15]),
3424 &frame->uc.tuc_stack.ss_flags);
3425 __put_user(target_sigaltstack_used.ss_size,
3426 &frame->uc.tuc_stack.ss_size);
3427 setup_sigcontext(&frame->uc.tuc_mcontext,
3428 regs, set->sig[0]);
3429 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3430 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3433 /* Set up to return from userspace. If provided, use a stub
3434 already in userspace. */
3435 if (ka->sa_flags & TARGET_SA_RESTORER) {
3436 regs->pr = (unsigned long) ka->sa_restorer;
3437 } else {
3438 /* Generate return code (system call to sigreturn) */
3439 abi_ulong retcode_addr = frame_addr +
3440 offsetof(struct target_rt_sigframe, retcode);
3441 __put_user(MOVW(2), &frame->retcode[0]);
3442 __put_user(TRAP_NOARG, &frame->retcode[1]);
3443 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3444 regs->pr = (unsigned long) retcode_addr;
3447 /* Set up registers for signal handler */
3448 regs->gregs[15] = frame_addr;
3449 regs->gregs[4] = sig; /* Arg for signal handler */
3450 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3451 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3452 regs->pc = (unsigned long) ka->_sa_handler;
3454 unlock_user_struct(frame, frame_addr, 1);
3455 return;
3457 give_sigsegv:
3458 unlock_user_struct(frame, frame_addr, 1);
3459 force_sigsegv(sig);
3462 long do_sigreturn(CPUSH4State *regs)
3464 struct target_sigframe *frame;
3465 abi_ulong frame_addr;
3466 sigset_t blocked;
3467 target_sigset_t target_set;
3468 int i;
3469 int err = 0;
3471 frame_addr = regs->gregs[15];
3472 trace_user_do_sigreturn(regs, frame_addr);
3473 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3474 goto badframe;
3477 __get_user(target_set.sig[0], &frame->sc.oldmask);
3478 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3479 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3482 if (err)
3483 goto badframe;
3485 target_to_host_sigset_internal(&blocked, &target_set);
3486 set_sigmask(&blocked);
3488 restore_sigcontext(regs, &frame->sc);
3490 unlock_user_struct(frame, frame_addr, 0);
3491 return -TARGET_QEMU_ESIGRETURN;
3493 badframe:
3494 unlock_user_struct(frame, frame_addr, 0);
3495 force_sig(TARGET_SIGSEGV);
3496 return -TARGET_QEMU_ESIGRETURN;
3499 long do_rt_sigreturn(CPUSH4State *regs)
3501 struct target_rt_sigframe *frame;
3502 abi_ulong frame_addr;
3503 sigset_t blocked;
3505 frame_addr = regs->gregs[15];
3506 trace_user_do_rt_sigreturn(regs, frame_addr);
3507 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3508 goto badframe;
3511 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3512 set_sigmask(&blocked);
3514 restore_sigcontext(regs, &frame->uc.tuc_mcontext);
3516 if (do_sigaltstack(frame_addr +
3517 offsetof(struct target_rt_sigframe, uc.tuc_stack),
3518 0, get_sp_from_cpustate(regs)) == -EFAULT) {
3519 goto badframe;
3522 unlock_user_struct(frame, frame_addr, 0);
3523 return -TARGET_QEMU_ESIGRETURN;
3525 badframe:
3526 unlock_user_struct(frame, frame_addr, 0);
3527 force_sig(TARGET_SIGSEGV);
3528 return -TARGET_QEMU_ESIGRETURN;
3530 #elif defined(TARGET_MICROBLAZE)
3532 struct target_sigcontext {
3533 struct target_pt_regs regs; /* needs to be first */
3534 uint32_t oldmask;
3537 struct target_stack_t {
3538 abi_ulong ss_sp;
3539 int ss_flags;
3540 unsigned int ss_size;
3543 struct target_ucontext {
3544 abi_ulong tuc_flags;
3545 abi_ulong tuc_link;
3546 struct target_stack_t tuc_stack;
3547 struct target_sigcontext tuc_mcontext;
3548 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3551 /* Signal frames. */
3552 struct target_signal_frame {
3553 struct target_ucontext uc;
3554 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3555 uint32_t tramp[2];
3558 struct rt_signal_frame {
3559 siginfo_t info;
3560 struct ucontext uc;
3561 uint32_t tramp[2];
3564 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3566 __put_user(env->regs[0], &sc->regs.r0);
3567 __put_user(env->regs[1], &sc->regs.r1);
3568 __put_user(env->regs[2], &sc->regs.r2);
3569 __put_user(env->regs[3], &sc->regs.r3);
3570 __put_user(env->regs[4], &sc->regs.r4);
3571 __put_user(env->regs[5], &sc->regs.r5);
3572 __put_user(env->regs[6], &sc->regs.r6);
3573 __put_user(env->regs[7], &sc->regs.r7);
3574 __put_user(env->regs[8], &sc->regs.r8);
3575 __put_user(env->regs[9], &sc->regs.r9);
3576 __put_user(env->regs[10], &sc->regs.r10);
3577 __put_user(env->regs[11], &sc->regs.r11);
3578 __put_user(env->regs[12], &sc->regs.r12);
3579 __put_user(env->regs[13], &sc->regs.r13);
3580 __put_user(env->regs[14], &sc->regs.r14);
3581 __put_user(env->regs[15], &sc->regs.r15);
3582 __put_user(env->regs[16], &sc->regs.r16);
3583 __put_user(env->regs[17], &sc->regs.r17);
3584 __put_user(env->regs[18], &sc->regs.r18);
3585 __put_user(env->regs[19], &sc->regs.r19);
3586 __put_user(env->regs[20], &sc->regs.r20);
3587 __put_user(env->regs[21], &sc->regs.r21);
3588 __put_user(env->regs[22], &sc->regs.r22);
3589 __put_user(env->regs[23], &sc->regs.r23);
3590 __put_user(env->regs[24], &sc->regs.r24);
3591 __put_user(env->regs[25], &sc->regs.r25);
3592 __put_user(env->regs[26], &sc->regs.r26);
3593 __put_user(env->regs[27], &sc->regs.r27);
3594 __put_user(env->regs[28], &sc->regs.r28);
3595 __put_user(env->regs[29], &sc->regs.r29);
3596 __put_user(env->regs[30], &sc->regs.r30);
3597 __put_user(env->regs[31], &sc->regs.r31);
3598 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3601 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
3603 __get_user(env->regs[0], &sc->regs.r0);
3604 __get_user(env->regs[1], &sc->regs.r1);
3605 __get_user(env->regs[2], &sc->regs.r2);
3606 __get_user(env->regs[3], &sc->regs.r3);
3607 __get_user(env->regs[4], &sc->regs.r4);
3608 __get_user(env->regs[5], &sc->regs.r5);
3609 __get_user(env->regs[6], &sc->regs.r6);
3610 __get_user(env->regs[7], &sc->regs.r7);
3611 __get_user(env->regs[8], &sc->regs.r8);
3612 __get_user(env->regs[9], &sc->regs.r9);
3613 __get_user(env->regs[10], &sc->regs.r10);
3614 __get_user(env->regs[11], &sc->regs.r11);
3615 __get_user(env->regs[12], &sc->regs.r12);
3616 __get_user(env->regs[13], &sc->regs.r13);
3617 __get_user(env->regs[14], &sc->regs.r14);
3618 __get_user(env->regs[15], &sc->regs.r15);
3619 __get_user(env->regs[16], &sc->regs.r16);
3620 __get_user(env->regs[17], &sc->regs.r17);
3621 __get_user(env->regs[18], &sc->regs.r18);
3622 __get_user(env->regs[19], &sc->regs.r19);
3623 __get_user(env->regs[20], &sc->regs.r20);
3624 __get_user(env->regs[21], &sc->regs.r21);
3625 __get_user(env->regs[22], &sc->regs.r22);
3626 __get_user(env->regs[23], &sc->regs.r23);
3627 __get_user(env->regs[24], &sc->regs.r24);
3628 __get_user(env->regs[25], &sc->regs.r25);
3629 __get_user(env->regs[26], &sc->regs.r26);
3630 __get_user(env->regs[27], &sc->regs.r27);
3631 __get_user(env->regs[28], &sc->regs.r28);
3632 __get_user(env->regs[29], &sc->regs.r29);
3633 __get_user(env->regs[30], &sc->regs.r30);
3634 __get_user(env->regs[31], &sc->regs.r31);
3635 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3638 static abi_ulong get_sigframe(struct target_sigaction *ka,
3639 CPUMBState *env, int frame_size)
3641 abi_ulong sp = env->regs[1];
3643 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !on_sig_stack(sp)) {
3644 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3647 return ((sp - frame_size) & -8UL);
3650 static void setup_frame(int sig, struct target_sigaction *ka,
3651 target_sigset_t *set, CPUMBState *env)
3653 struct target_signal_frame *frame;
3654 abi_ulong frame_addr;
3655 int i;
3657 frame_addr = get_sigframe(ka, env, sizeof *frame);
3658 trace_user_setup_frame(env, frame_addr);
3659 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3660 goto badframe;
3662 /* Save the mask. */
3663 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3665 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3666 __put_user(set->sig[i], &frame->extramask[i - 1]);
3669 setup_sigcontext(&frame->uc.tuc_mcontext, env);
3671 /* Set up to return from userspace. If provided, use a stub
3672 already in userspace. */
3673 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3674 if (ka->sa_flags & TARGET_SA_RESTORER) {
3675 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3676 } else {
3677 uint32_t t;
3678 /* Note, these encodings are _big endian_! */
3679 /* addi r12, r0, __NR_sigreturn */
3680 t = 0x31800000UL | TARGET_NR_sigreturn;
3681 __put_user(t, frame->tramp + 0);
3682 /* brki r14, 0x8 */
3683 t = 0xb9cc0008UL;
3684 __put_user(t, frame->tramp + 1);
3686 /* Return from sighandler will jump to the tramp.
3687 Negative 8 offset because return is rtsd r15, 8 */
3688 env->regs[15] = frame_addr + offsetof(struct target_signal_frame, tramp)
3689 - 8;
3692 /* Set up registers for signal handler */
3693 env->regs[1] = frame_addr;
3694 /* Signal handler args: */
3695 env->regs[5] = sig; /* Arg 0: signum */
3696 env->regs[6] = 0;
3697 /* arg 1: sigcontext */
3698 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3700 /* Offset of 4 to handle microblaze rtid r14, 0 */
3701 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3703 unlock_user_struct(frame, frame_addr, 1);
3704 return;
3705 badframe:
3706 force_sigsegv(sig);
3709 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3710 target_siginfo_t *info,
3711 target_sigset_t *set, CPUMBState *env)
3713 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3716 long do_sigreturn(CPUMBState *env)
3718 struct target_signal_frame *frame;
3719 abi_ulong frame_addr;
3720 target_sigset_t target_set;
3721 sigset_t set;
3722 int i;
3724 frame_addr = env->regs[R_SP];
3725 trace_user_do_sigreturn(env, frame_addr);
3726 /* Make sure the guest isn't playing games. */
3727 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3728 goto badframe;
3730 /* Restore blocked signals */
3731 __get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask);
3732 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3733 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3735 target_to_host_sigset_internal(&set, &target_set);
3736 set_sigmask(&set);
3738 restore_sigcontext(&frame->uc.tuc_mcontext, env);
3739 /* We got here through a sigreturn syscall, our path back is via an
3740 rtb insn so setup r14 for that. */
3741 env->regs[14] = env->sregs[SR_PC];
3743 unlock_user_struct(frame, frame_addr, 0);
3744 return -TARGET_QEMU_ESIGRETURN;
3745 badframe:
3746 force_sig(TARGET_SIGSEGV);
3747 return -TARGET_QEMU_ESIGRETURN;
3750 long do_rt_sigreturn(CPUMBState *env)
3752 trace_user_do_rt_sigreturn(env, 0);
3753 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3754 return -TARGET_ENOSYS;
3757 #elif defined(TARGET_CRIS)
3759 struct target_sigcontext {
3760 struct target_pt_regs regs; /* needs to be first */
3761 uint32_t oldmask;
3762 uint32_t usp; /* usp before stacking this gunk on it */
3765 /* Signal frames. */
3766 struct target_signal_frame {
3767 struct target_sigcontext sc;
3768 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3769 uint16_t retcode[4]; /* Trampoline code. */
3772 struct rt_signal_frame {
3773 siginfo_t *pinfo;
3774 void *puc;
3775 siginfo_t info;
3776 struct ucontext uc;
3777 uint16_t retcode[4]; /* Trampoline code. */
3780 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3782 __put_user(env->regs[0], &sc->regs.r0);
3783 __put_user(env->regs[1], &sc->regs.r1);
3784 __put_user(env->regs[2], &sc->regs.r2);
3785 __put_user(env->regs[3], &sc->regs.r3);
3786 __put_user(env->regs[4], &sc->regs.r4);
3787 __put_user(env->regs[5], &sc->regs.r5);
3788 __put_user(env->regs[6], &sc->regs.r6);
3789 __put_user(env->regs[7], &sc->regs.r7);
3790 __put_user(env->regs[8], &sc->regs.r8);
3791 __put_user(env->regs[9], &sc->regs.r9);
3792 __put_user(env->regs[10], &sc->regs.r10);
3793 __put_user(env->regs[11], &sc->regs.r11);
3794 __put_user(env->regs[12], &sc->regs.r12);
3795 __put_user(env->regs[13], &sc->regs.r13);
3796 __put_user(env->regs[14], &sc->usp);
3797 __put_user(env->regs[15], &sc->regs.acr);
3798 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3799 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3800 __put_user(env->pc, &sc->regs.erp);
3803 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
3805 __get_user(env->regs[0], &sc->regs.r0);
3806 __get_user(env->regs[1], &sc->regs.r1);
3807 __get_user(env->regs[2], &sc->regs.r2);
3808 __get_user(env->regs[3], &sc->regs.r3);
3809 __get_user(env->regs[4], &sc->regs.r4);
3810 __get_user(env->regs[5], &sc->regs.r5);
3811 __get_user(env->regs[6], &sc->regs.r6);
3812 __get_user(env->regs[7], &sc->regs.r7);
3813 __get_user(env->regs[8], &sc->regs.r8);
3814 __get_user(env->regs[9], &sc->regs.r9);
3815 __get_user(env->regs[10], &sc->regs.r10);
3816 __get_user(env->regs[11], &sc->regs.r11);
3817 __get_user(env->regs[12], &sc->regs.r12);
3818 __get_user(env->regs[13], &sc->regs.r13);
3819 __get_user(env->regs[14], &sc->usp);
3820 __get_user(env->regs[15], &sc->regs.acr);
3821 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3822 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3823 __get_user(env->pc, &sc->regs.erp);
3826 static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
3828 abi_ulong sp;
3829 /* Align the stack downwards to 4. */
3830 sp = (env->regs[R_SP] & ~3);
3831 return sp - framesize;
3834 static void setup_frame(int sig, struct target_sigaction *ka,
3835 target_sigset_t *set, CPUCRISState *env)
3837 struct target_signal_frame *frame;
3838 abi_ulong frame_addr;
3839 int i;
3841 frame_addr = get_sigframe(env, sizeof *frame);
3842 trace_user_setup_frame(env, frame_addr);
3843 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3844 goto badframe;
3847 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3848 * use this trampoline anymore but it sets it up for GDB.
3849 * In QEMU, using the trampoline simplifies things a bit so we use it.
3851 * This is movu.w __NR_sigreturn, r9; break 13;
3853 __put_user(0x9c5f, frame->retcode+0);
3854 __put_user(TARGET_NR_sigreturn,
3855 frame->retcode + 1);
3856 __put_user(0xe93d, frame->retcode + 2);
3858 /* Save the mask. */
3859 __put_user(set->sig[0], &frame->sc.oldmask);
3861 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3862 __put_user(set->sig[i], &frame->extramask[i - 1]);
3865 setup_sigcontext(&frame->sc, env);
3867 /* Move the stack and setup the arguments for the handler. */
3868 env->regs[R_SP] = frame_addr;
3869 env->regs[10] = sig;
3870 env->pc = (unsigned long) ka->_sa_handler;
3871 /* Link SRP so the guest returns through the trampoline. */
3872 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3874 unlock_user_struct(frame, frame_addr, 1);
3875 return;
3876 badframe:
3877 force_sigsegv(sig);
3880 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3881 target_siginfo_t *info,
3882 target_sigset_t *set, CPUCRISState *env)
3884 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3887 long do_sigreturn(CPUCRISState *env)
3889 struct target_signal_frame *frame;
3890 abi_ulong frame_addr;
3891 target_sigset_t target_set;
3892 sigset_t set;
3893 int i;
3895 frame_addr = env->regs[R_SP];
3896 trace_user_do_sigreturn(env, frame_addr);
3897 /* Make sure the guest isn't playing games. */
3898 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1)) {
3899 goto badframe;
3902 /* Restore blocked signals */
3903 __get_user(target_set.sig[0], &frame->sc.oldmask);
3904 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3905 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
3907 target_to_host_sigset_internal(&set, &target_set);
3908 set_sigmask(&set);
3910 restore_sigcontext(&frame->sc, env);
3911 unlock_user_struct(frame, frame_addr, 0);
3912 return -TARGET_QEMU_ESIGRETURN;
3913 badframe:
3914 force_sig(TARGET_SIGSEGV);
3915 return -TARGET_QEMU_ESIGRETURN;
3918 long do_rt_sigreturn(CPUCRISState *env)
3920 trace_user_do_rt_sigreturn(env, 0);
3921 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3922 return -TARGET_ENOSYS;
3925 #elif defined(TARGET_OPENRISC)
3927 struct target_sigcontext {
3928 struct target_pt_regs regs;
3929 abi_ulong oldmask;
3930 abi_ulong usp;
3933 struct target_ucontext {
3934 abi_ulong tuc_flags;
3935 abi_ulong tuc_link;
3936 target_stack_t tuc_stack;
3937 struct target_sigcontext tuc_mcontext;
3938 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3941 struct target_rt_sigframe {
3942 abi_ulong pinfo;
3943 uint64_t puc;
3944 struct target_siginfo info;
3945 struct target_sigcontext sc;
3946 struct target_ucontext uc;
3947 unsigned char retcode[16]; /* trampoline code */
3950 /* This is the asm-generic/ucontext.h version */
3951 #if 0
3952 static int restore_sigcontext(CPUOpenRISCState *regs,
3953 struct target_sigcontext *sc)
3955 unsigned int err = 0;
3956 unsigned long old_usp;
3958 /* Alwys make any pending restarted system call return -EINTR */
3959 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3961 /* restore the regs from &sc->regs (same as sc, since regs is first)
3962 * (sc is already checked for VERIFY_READ since the sigframe was
3963 * checked in sys_sigreturn previously)
3966 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3967 goto badframe;
3970 /* make sure the U-flag is set so user-mode cannot fool us */
3972 regs->sr &= ~SR_SM;
3974 /* restore the old USP as it was before we stacked the sc etc.
3975 * (we cannot just pop the sigcontext since we aligned the sp and
3976 * stuff after pushing it)
3979 __get_user(old_usp, &sc->usp);
3980 phx_signal("old_usp 0x%lx", old_usp);
3982 __PHX__ REALLY /* ??? */
3983 wrusp(old_usp);
3984 regs->gpr[1] = old_usp;
3986 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3987 * after this completes, but we don't use that mechanism. maybe we can
3988 * use it now ?
3991 return err;
3993 badframe:
3994 return 1;
3996 #endif
3998 /* Set up a signal frame. */
4000 static void setup_sigcontext(struct target_sigcontext *sc,
4001 CPUOpenRISCState *regs,
4002 unsigned long mask)
4004 unsigned long usp = regs->gpr[1];
4006 /* copy the regs. they are first in sc so we can use sc directly */
4008 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
4010 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
4011 the signal handler. The frametype will be restored to its previous
4012 value in restore_sigcontext. */
4013 /*regs->frametype = CRIS_FRAME_NORMAL;*/
4015 /* then some other stuff */
4016 __put_user(mask, &sc->oldmask);
4017 __put_user(usp, &sc->usp);
4020 static inline unsigned long align_sigframe(unsigned long sp)
4022 return sp & ~3UL;
4025 static inline abi_ulong get_sigframe(struct target_sigaction *ka,
4026 CPUOpenRISCState *regs,
4027 size_t frame_size)
4029 unsigned long sp = regs->gpr[1];
4030 int onsigstack = on_sig_stack(sp);
4032 /* redzone */
4033 /* This is the X/Open sanctioned signal stack switching. */
4034 if ((ka->sa_flags & TARGET_SA_ONSTACK) != 0 && !onsigstack) {
4035 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4038 sp = align_sigframe(sp - frame_size);
4041 * If we are on the alternate signal stack and would overflow it, don't.
4042 * Return an always-bogus address instead so we will die with SIGSEGV.
4045 if (onsigstack && !likely(on_sig_stack(sp))) {
4046 return -1L;
4049 return sp;
4052 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4053 target_siginfo_t *info,
4054 target_sigset_t *set, CPUOpenRISCState *env)
4056 int err = 0;
4057 abi_ulong frame_addr;
4058 unsigned long return_ip;
4059 struct target_rt_sigframe *frame;
4060 abi_ulong info_addr, uc_addr;
4062 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4063 trace_user_setup_rt_frame(env, frame_addr);
4064 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4065 goto give_sigsegv;
4068 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4069 __put_user(info_addr, &frame->pinfo);
4070 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4071 __put_user(uc_addr, &frame->puc);
4073 if (ka->sa_flags & SA_SIGINFO) {
4074 tswap_siginfo(&frame->info, info);
4077 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
4078 __put_user(0, &frame->uc.tuc_flags);
4079 __put_user(0, &frame->uc.tuc_link);
4080 __put_user(target_sigaltstack_used.ss_sp,
4081 &frame->uc.tuc_stack.ss_sp);
4082 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
4083 __put_user(target_sigaltstack_used.ss_size,
4084 &frame->uc.tuc_stack.ss_size);
4085 setup_sigcontext(&frame->sc, env, set->sig[0]);
4087 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4089 /* trampoline - the desired return ip is the retcode itself */
4090 return_ip = (unsigned long)&frame->retcode;
4091 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
4092 __put_user(0xa960, (short *)(frame->retcode + 0));
4093 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
4094 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
4095 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
4097 if (err) {
4098 goto give_sigsegv;
4101 /* TODO what is the current->exec_domain stuff and invmap ? */
4103 /* Set up registers for signal handler */
4104 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
4105 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
4106 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
4107 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
4108 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
4110 /* actually move the usp to reflect the stacked frame */
4111 env->gpr[1] = (unsigned long)frame;
4113 return;
4115 give_sigsegv:
4116 unlock_user_struct(frame, frame_addr, 1);
4117 force_sigsegv(sig);
4120 long do_sigreturn(CPUOpenRISCState *env)
4122 trace_user_do_sigreturn(env, 0);
4123 fprintf(stderr, "do_sigreturn: not implemented\n");
4124 return -TARGET_ENOSYS;
4127 long do_rt_sigreturn(CPUOpenRISCState *env)
4129 trace_user_do_rt_sigreturn(env, 0);
4130 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
4131 return -TARGET_ENOSYS;
4133 /* TARGET_OPENRISC */
4135 #elif defined(TARGET_S390X)
4137 #define __NUM_GPRS 16
4138 #define __NUM_FPRS 16
4139 #define __NUM_ACRS 16
4141 #define S390_SYSCALL_SIZE 2
4142 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4144 #define _SIGCONTEXT_NSIG 64
4145 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4146 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4147 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4148 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4149 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4151 typedef struct {
4152 target_psw_t psw;
4153 target_ulong gprs[__NUM_GPRS];
4154 unsigned int acrs[__NUM_ACRS];
4155 } target_s390_regs_common;
4157 typedef struct {
4158 unsigned int fpc;
4159 double fprs[__NUM_FPRS];
4160 } target_s390_fp_regs;
4162 typedef struct {
4163 target_s390_regs_common regs;
4164 target_s390_fp_regs fpregs;
4165 } target_sigregs;
4167 struct target_sigcontext {
4168 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4169 target_sigregs *sregs;
4172 typedef struct {
4173 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4174 struct target_sigcontext sc;
4175 target_sigregs sregs;
4176 int signo;
4177 uint8_t retcode[S390_SYSCALL_SIZE];
4178 } sigframe;
4180 struct target_ucontext {
4181 target_ulong tuc_flags;
4182 struct target_ucontext *tuc_link;
4183 target_stack_t tuc_stack;
4184 target_sigregs tuc_mcontext;
4185 target_sigset_t tuc_sigmask; /* mask last for extensibility */
4188 typedef struct {
4189 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4190 uint8_t retcode[S390_SYSCALL_SIZE];
4191 struct target_siginfo info;
4192 struct target_ucontext uc;
4193 } rt_sigframe;
4195 static inline abi_ulong
4196 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
4198 abi_ulong sp;
4200 /* Default to using normal stack */
4201 sp = env->regs[15];
4203 /* This is the X/Open sanctioned signal stack switching. */
4204 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4205 if (!sas_ss_flags(sp)) {
4206 sp = target_sigaltstack_used.ss_sp +
4207 target_sigaltstack_used.ss_size;
4211 /* This is the legacy signal stack switching. */
4212 else if (/* FIXME !user_mode(regs) */ 0 &&
4213 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4214 ka->sa_restorer) {
4215 sp = (abi_ulong) ka->sa_restorer;
4218 return (sp - frame_size) & -8ul;
4221 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
4223 int i;
4224 //save_access_regs(current->thread.acrs); FIXME
4226 /* Copy a 'clean' PSW mask to the user to avoid leaking
4227 information about whether PER is currently on. */
4228 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4229 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4230 for (i = 0; i < 16; i++) {
4231 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4233 for (i = 0; i < 16; i++) {
4234 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4237 * We have to store the fp registers to current->thread.fp_regs
4238 * to merge them with the emulated registers.
4240 //save_fp_regs(&current->thread.fp_regs); FIXME
4241 for (i = 0; i < 16; i++) {
4242 __put_user(get_freg(env, i)->ll, &sregs->fpregs.fprs[i]);
4246 static void setup_frame(int sig, struct target_sigaction *ka,
4247 target_sigset_t *set, CPUS390XState *env)
4249 sigframe *frame;
4250 abi_ulong frame_addr;
4252 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4253 trace_user_setup_frame(env, frame_addr);
4254 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4255 goto give_sigsegv;
4258 __put_user(set->sig[0], &frame->sc.oldmask[0]);
4260 save_sigregs(env, &frame->sregs);
4262 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4263 (abi_ulong *)&frame->sc.sregs);
4265 /* Set up to return from userspace. If provided, use a stub
4266 already in userspace. */
4267 if (ka->sa_flags & TARGET_SA_RESTORER) {
4268 env->regs[14] = (unsigned long)
4269 ka->sa_restorer | PSW_ADDR_AMODE;
4270 } else {
4271 env->regs[14] = (frame_addr + offsetof(sigframe, retcode))
4272 | PSW_ADDR_AMODE;
4273 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4274 (uint16_t *)(frame->retcode));
4277 /* Set up backchain. */
4278 __put_user(env->regs[15], (abi_ulong *) frame);
4280 /* Set up registers for signal handler */
4281 env->regs[15] = frame_addr;
4282 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4284 env->regs[2] = sig; //map_signal(sig);
4285 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
4287 /* We forgot to include these in the sigcontext.
4288 To avoid breaking binary compatibility, they are passed as args. */
4289 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4290 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4292 /* Place signal number on stack to allow backtrace from handler. */
4293 __put_user(env->regs[2], &frame->signo);
4294 unlock_user_struct(frame, frame_addr, 1);
4295 return;
4297 give_sigsegv:
4298 force_sigsegv(sig);
4301 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4302 target_siginfo_t *info,
4303 target_sigset_t *set, CPUS390XState *env)
4305 int i;
4306 rt_sigframe *frame;
4307 abi_ulong frame_addr;
4309 frame_addr = get_sigframe(ka, env, sizeof *frame);
4310 trace_user_setup_rt_frame(env, frame_addr);
4311 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4312 goto give_sigsegv;
4315 tswap_siginfo(&frame->info, info);
4317 /* Create the ucontext. */
4318 __put_user(0, &frame->uc.tuc_flags);
4319 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4320 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
4321 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
4322 &frame->uc.tuc_stack.ss_flags);
4323 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4324 save_sigregs(env, &frame->uc.tuc_mcontext);
4325 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4326 __put_user((abi_ulong)set->sig[i],
4327 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
4330 /* Set up to return from userspace. If provided, use a stub
4331 already in userspace. */
4332 if (ka->sa_flags & TARGET_SA_RESTORER) {
4333 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4334 } else {
4335 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4336 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4337 (uint16_t *)(frame->retcode));
4340 /* Set up backchain. */
4341 __put_user(env->regs[15], (abi_ulong *) frame);
4343 /* Set up registers for signal handler */
4344 env->regs[15] = frame_addr;
4345 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4347 env->regs[2] = sig; //map_signal(sig);
4348 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4349 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
4350 return;
4352 give_sigsegv:
4353 force_sigsegv(sig);
4356 static int
4357 restore_sigregs(CPUS390XState *env, target_sigregs *sc)
4359 int err = 0;
4360 int i;
4362 for (i = 0; i < 16; i++) {
4363 __get_user(env->regs[i], &sc->regs.gprs[i]);
4366 __get_user(env->psw.mask, &sc->regs.psw.mask);
4367 trace_user_s390x_restore_sigregs(env, (unsigned long long)sc->regs.psw.addr,
4368 (unsigned long long)env->psw.addr);
4369 __get_user(env->psw.addr, &sc->regs.psw.addr);
4370 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4372 for (i = 0; i < 16; i++) {
4373 __get_user(env->aregs[i], &sc->regs.acrs[i]);
4375 for (i = 0; i < 16; i++) {
4376 __get_user(get_freg(env, i)->ll, &sc->fpregs.fprs[i]);
4379 return err;
4382 long do_sigreturn(CPUS390XState *env)
4384 sigframe *frame;
4385 abi_ulong frame_addr = env->regs[15];
4386 target_sigset_t target_set;
4387 sigset_t set;
4389 trace_user_do_sigreturn(env, frame_addr);
4390 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4391 goto badframe;
4393 __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
4395 target_to_host_sigset_internal(&set, &target_set);
4396 set_sigmask(&set); /* ~_BLOCKABLE? */
4398 if (restore_sigregs(env, &frame->sregs)) {
4399 goto badframe;
4402 unlock_user_struct(frame, frame_addr, 0);
4403 return -TARGET_QEMU_ESIGRETURN;
4405 badframe:
4406 force_sig(TARGET_SIGSEGV);
4407 return -TARGET_QEMU_ESIGRETURN;
4410 long do_rt_sigreturn(CPUS390XState *env)
4412 rt_sigframe *frame;
4413 abi_ulong frame_addr = env->regs[15];
4414 sigset_t set;
4416 trace_user_do_rt_sigreturn(env, frame_addr);
4417 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4418 goto badframe;
4420 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
4422 set_sigmask(&set); /* ~_BLOCKABLE? */
4424 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
4425 goto badframe;
4428 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
4429 get_sp_from_cpustate(env)) == -EFAULT) {
4430 goto badframe;
4432 unlock_user_struct(frame, frame_addr, 0);
4433 return -TARGET_QEMU_ESIGRETURN;
4435 badframe:
4436 unlock_user_struct(frame, frame_addr, 0);
4437 force_sig(TARGET_SIGSEGV);
4438 return -TARGET_QEMU_ESIGRETURN;
4441 #elif defined(TARGET_PPC)
4443 /* Size of dummy stack frame allocated when calling signal handler.
4444 See arch/powerpc/include/asm/ptrace.h. */
4445 #if defined(TARGET_PPC64)
4446 #define SIGNAL_FRAMESIZE 128
4447 #else
4448 #define SIGNAL_FRAMESIZE 64
4449 #endif
4451 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4452 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4453 struct target_mcontext {
4454 target_ulong mc_gregs[48];
4455 /* Includes fpscr. */
4456 uint64_t mc_fregs[33];
4457 target_ulong mc_pad[2];
4458 /* We need to handle Altivec and SPE at the same time, which no
4459 kernel needs to do. Fortunately, the kernel defines this bit to
4460 be Altivec-register-large all the time, rather than trying to
4461 twiddle it based on the specific platform. */
4462 union {
4463 /* SPE vector registers. One extra for SPEFSCR. */
4464 uint32_t spe[33];
4465 /* Altivec vector registers. The packing of VSCR and VRSAVE
4466 varies depending on whether we're PPC64 or not: PPC64 splits
4467 them apart; PPC32 stuffs them together. */
4468 #if defined(TARGET_PPC64)
4469 #define QEMU_NVRREG 34
4470 #else
4471 #define QEMU_NVRREG 33
4472 #endif
4473 ppc_avr_t altivec[QEMU_NVRREG];
4474 #undef QEMU_NVRREG
4475 } mc_vregs __attribute__((__aligned__(16)));
4478 /* See arch/powerpc/include/asm/sigcontext.h. */
4479 struct target_sigcontext {
4480 target_ulong _unused[4];
4481 int32_t signal;
4482 #if defined(TARGET_PPC64)
4483 int32_t pad0;
4484 #endif
4485 target_ulong handler;
4486 target_ulong oldmask;
4487 target_ulong regs; /* struct pt_regs __user * */
4488 #if defined(TARGET_PPC64)
4489 struct target_mcontext mcontext;
4490 #endif
4493 /* Indices for target_mcontext.mc_gregs, below.
4494 See arch/powerpc/include/asm/ptrace.h for details. */
4495 enum {
4496 TARGET_PT_R0 = 0,
4497 TARGET_PT_R1 = 1,
4498 TARGET_PT_R2 = 2,
4499 TARGET_PT_R3 = 3,
4500 TARGET_PT_R4 = 4,
4501 TARGET_PT_R5 = 5,
4502 TARGET_PT_R6 = 6,
4503 TARGET_PT_R7 = 7,
4504 TARGET_PT_R8 = 8,
4505 TARGET_PT_R9 = 9,
4506 TARGET_PT_R10 = 10,
4507 TARGET_PT_R11 = 11,
4508 TARGET_PT_R12 = 12,
4509 TARGET_PT_R13 = 13,
4510 TARGET_PT_R14 = 14,
4511 TARGET_PT_R15 = 15,
4512 TARGET_PT_R16 = 16,
4513 TARGET_PT_R17 = 17,
4514 TARGET_PT_R18 = 18,
4515 TARGET_PT_R19 = 19,
4516 TARGET_PT_R20 = 20,
4517 TARGET_PT_R21 = 21,
4518 TARGET_PT_R22 = 22,
4519 TARGET_PT_R23 = 23,
4520 TARGET_PT_R24 = 24,
4521 TARGET_PT_R25 = 25,
4522 TARGET_PT_R26 = 26,
4523 TARGET_PT_R27 = 27,
4524 TARGET_PT_R28 = 28,
4525 TARGET_PT_R29 = 29,
4526 TARGET_PT_R30 = 30,
4527 TARGET_PT_R31 = 31,
4528 TARGET_PT_NIP = 32,
4529 TARGET_PT_MSR = 33,
4530 TARGET_PT_ORIG_R3 = 34,
4531 TARGET_PT_CTR = 35,
4532 TARGET_PT_LNK = 36,
4533 TARGET_PT_XER = 37,
4534 TARGET_PT_CCR = 38,
4535 /* Yes, there are two registers with #39. One is 64-bit only. */
4536 TARGET_PT_MQ = 39,
4537 TARGET_PT_SOFTE = 39,
4538 TARGET_PT_TRAP = 40,
4539 TARGET_PT_DAR = 41,
4540 TARGET_PT_DSISR = 42,
4541 TARGET_PT_RESULT = 43,
4542 TARGET_PT_REGS_COUNT = 44
4546 struct target_ucontext {
4547 target_ulong tuc_flags;
4548 target_ulong tuc_link; /* struct ucontext __user * */
4549 struct target_sigaltstack tuc_stack;
4550 #if !defined(TARGET_PPC64)
4551 int32_t tuc_pad[7];
4552 target_ulong tuc_regs; /* struct mcontext __user *
4553 points to uc_mcontext field */
4554 #endif
4555 target_sigset_t tuc_sigmask;
4556 #if defined(TARGET_PPC64)
4557 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4558 struct target_sigcontext tuc_sigcontext;
4559 #else
4560 int32_t tuc_maskext[30];
4561 int32_t tuc_pad2[3];
4562 struct target_mcontext tuc_mcontext;
4563 #endif
4566 /* See arch/powerpc/kernel/signal_32.c. */
4567 struct target_sigframe {
4568 struct target_sigcontext sctx;
4569 struct target_mcontext mctx;
4570 int32_t abigap[56];
4573 #if defined(TARGET_PPC64)
4575 #define TARGET_TRAMP_SIZE 6
4577 struct target_rt_sigframe {
4578 /* sys_rt_sigreturn requires the ucontext be the first field */
4579 struct target_ucontext uc;
4580 target_ulong _unused[2];
4581 uint32_t trampoline[TARGET_TRAMP_SIZE];
4582 target_ulong pinfo; /* struct siginfo __user * */
4583 target_ulong puc; /* void __user * */
4584 struct target_siginfo info;
4585 /* 64 bit ABI allows for 288 bytes below sp before decrementing it. */
4586 char abigap[288];
4587 } __attribute__((aligned(16)));
4589 #else
4591 struct target_rt_sigframe {
4592 struct target_siginfo info;
4593 struct target_ucontext uc;
4594 int32_t abigap[56];
4597 #endif
4599 #if defined(TARGET_PPC64)
4601 struct target_func_ptr {
4602 target_ulong entry;
4603 target_ulong toc;
4606 #endif
4608 /* We use the mc_pad field for the signal return trampoline. */
4609 #define tramp mc_pad
4611 /* See arch/powerpc/kernel/signal.c. */
4612 static target_ulong get_sigframe(struct target_sigaction *ka,
4613 CPUPPCState *env,
4614 int frame_size)
4616 target_ulong oldsp;
4618 oldsp = env->gpr[1];
4620 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4621 (sas_ss_flags(oldsp) == 0)) {
4622 oldsp = (target_sigaltstack_used.ss_sp
4623 + target_sigaltstack_used.ss_size);
4626 return (oldsp - frame_size) & ~0xFUL;
4629 static void save_user_regs(CPUPPCState *env, struct target_mcontext *frame)
4631 target_ulong msr = env->msr;
4632 int i;
4633 target_ulong ccr = 0;
4635 /* In general, the kernel attempts to be intelligent about what it
4636 needs to save for Altivec/FP/SPE registers. We don't care that
4637 much, so we just go ahead and save everything. */
4639 /* Save general registers. */
4640 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4641 __put_user(env->gpr[i], &frame->mc_gregs[i]);
4643 __put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4644 __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4645 __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4646 __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4648 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4649 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4651 __put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4653 /* Save Altivec registers if necessary. */
4654 if (env->insns_flags & PPC_ALTIVEC) {
4655 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4656 ppc_avr_t *avr = &env->avr[i];
4657 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4659 __put_user(avr->u64[0], &vreg->u64[0]);
4660 __put_user(avr->u64[1], &vreg->u64[1]);
4662 /* Set MSR_VR in the saved MSR value to indicate that
4663 frame->mc_vregs contains valid data. */
4664 msr |= MSR_VR;
4665 __put_user((uint32_t)env->spr[SPR_VRSAVE],
4666 &frame->mc_vregs.altivec[32].u32[3]);
4669 /* Save floating point registers. */
4670 if (env->insns_flags & PPC_FLOAT) {
4671 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4672 __put_user(env->fpr[i], &frame->mc_fregs[i]);
4674 __put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]);
4677 /* Save SPE registers. The kernel only saves the high half. */
4678 if (env->insns_flags & PPC_SPE) {
4679 #if defined(TARGET_PPC64)
4680 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4681 __put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i]);
4683 #else
4684 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4685 __put_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4687 #endif
4688 /* Set MSR_SPE in the saved MSR value to indicate that
4689 frame->mc_vregs contains valid data. */
4690 msr |= MSR_SPE;
4691 __put_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4694 /* Store MSR. */
4695 __put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4698 static void encode_trampoline(int sigret, uint32_t *tramp)
4700 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4701 if (sigret) {
4702 __put_user(0x38000000 | sigret, &tramp[0]);
4703 __put_user(0x44000002, &tramp[1]);
4707 static void restore_user_regs(CPUPPCState *env,
4708 struct target_mcontext *frame, int sig)
4710 target_ulong save_r2 = 0;
4711 target_ulong msr;
4712 target_ulong ccr;
4714 int i;
4716 if (!sig) {
4717 save_r2 = env->gpr[2];
4720 /* Restore general registers. */
4721 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4722 __get_user(env->gpr[i], &frame->mc_gregs[i]);
4724 __get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]);
4725 __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]);
4726 __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]);
4727 __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]);
4728 __get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]);
4730 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4731 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4734 if (!sig) {
4735 env->gpr[2] = save_r2;
4737 /* Restore MSR. */
4738 __get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]);
4740 /* If doing signal return, restore the previous little-endian mode. */
4741 if (sig)
4742 env->msr = (env->msr & ~(1ull << MSR_LE)) | (msr & (1ull << MSR_LE));
4744 /* Restore Altivec registers if necessary. */
4745 if (env->insns_flags & PPC_ALTIVEC) {
4746 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4747 ppc_avr_t *avr = &env->avr[i];
4748 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4750 __get_user(avr->u64[0], &vreg->u64[0]);
4751 __get_user(avr->u64[1], &vreg->u64[1]);
4753 /* Set MSR_VEC in the saved MSR value to indicate that
4754 frame->mc_vregs contains valid data. */
4755 __get_user(env->spr[SPR_VRSAVE],
4756 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3]));
4759 /* Restore floating point registers. */
4760 if (env->insns_flags & PPC_FLOAT) {
4761 uint64_t fpscr;
4762 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4763 __get_user(env->fpr[i], &frame->mc_fregs[i]);
4765 __get_user(fpscr, &frame->mc_fregs[32]);
4766 env->fpscr = (uint32_t) fpscr;
4769 /* Save SPE registers. The kernel only saves the high half. */
4770 if (env->insns_flags & PPC_SPE) {
4771 #if defined(TARGET_PPC64)
4772 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4773 uint32_t hi;
4775 __get_user(hi, &frame->mc_vregs.spe[i]);
4776 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4778 #else
4779 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4780 __get_user(env->gprh[i], &frame->mc_vregs.spe[i]);
4782 #endif
4783 __get_user(env->spe_fscr, &frame->mc_vregs.spe[32]);
4787 static void setup_frame(int sig, struct target_sigaction *ka,
4788 target_sigset_t *set, CPUPPCState *env)
4790 struct target_sigframe *frame;
4791 struct target_sigcontext *sc;
4792 target_ulong frame_addr, newsp;
4793 int err = 0;
4794 #if defined(TARGET_PPC64)
4795 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4796 #endif
4798 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4799 trace_user_setup_frame(env, frame_addr);
4800 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4801 goto sigsegv;
4802 sc = &frame->sctx;
4804 __put_user(ka->_sa_handler, &sc->handler);
4805 __put_user(set->sig[0], &sc->oldmask);
4806 #if TARGET_ABI_BITS == 64
4807 __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4808 #else
4809 __put_user(set->sig[1], &sc->_unused[3]);
4810 #endif
4811 __put_user(h2g(&frame->mctx), &sc->regs);
4812 __put_user(sig, &sc->signal);
4814 /* Save user regs. */
4815 save_user_regs(env, &frame->mctx);
4817 /* Construct the trampoline code on the stack. */
4818 encode_trampoline(TARGET_NR_sigreturn, (uint32_t *)&frame->mctx.tramp);
4820 /* The kernel checks for the presence of a VDSO here. We don't
4821 emulate a vdso, so use a sigreturn system call. */
4822 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4824 /* Turn off all fp exceptions. */
4825 env->fpscr = 0;
4827 /* Create a stack frame for the caller of the handler. */
4828 newsp = frame_addr - SIGNAL_FRAMESIZE;
4829 err |= put_user(env->gpr[1], newsp, target_ulong);
4831 if (err)
4832 goto sigsegv;
4834 /* Set up registers for signal handler. */
4835 env->gpr[1] = newsp;
4836 env->gpr[3] = sig;
4837 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
4839 #if defined(TARGET_PPC64)
4840 if (get_ppc64_abi(image) < 2) {
4841 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4842 struct target_func_ptr *handler =
4843 (struct target_func_ptr *)g2h(ka->_sa_handler);
4844 env->nip = tswapl(handler->entry);
4845 env->gpr[2] = tswapl(handler->toc);
4846 } else {
4847 /* ELFv2 PPC64 function pointers are entry points, but R12
4848 * must also be set */
4849 env->nip = tswapl((target_ulong) ka->_sa_handler);
4850 env->gpr[12] = env->nip;
4852 #else
4853 env->nip = (target_ulong) ka->_sa_handler;
4854 #endif
4856 /* Signal handlers are entered in big-endian mode. */
4857 env->msr &= ~(1ull << MSR_LE);
4859 unlock_user_struct(frame, frame_addr, 1);
4860 return;
4862 sigsegv:
4863 unlock_user_struct(frame, frame_addr, 1);
4864 force_sigsegv(sig);
4867 static void setup_rt_frame(int sig, struct target_sigaction *ka,
4868 target_siginfo_t *info,
4869 target_sigset_t *set, CPUPPCState *env)
4871 struct target_rt_sigframe *rt_sf;
4872 uint32_t *trampptr = 0;
4873 struct target_mcontext *mctx = 0;
4874 target_ulong rt_sf_addr, newsp = 0;
4875 int i, err = 0;
4876 #if defined(TARGET_PPC64)
4877 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
4878 #endif
4880 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4881 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4882 goto sigsegv;
4884 tswap_siginfo(&rt_sf->info, info);
4886 __put_user(0, &rt_sf->uc.tuc_flags);
4887 __put_user(0, &rt_sf->uc.tuc_link);
4888 __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4889 &rt_sf->uc.tuc_stack.ss_sp);
4890 __put_user(sas_ss_flags(env->gpr[1]),
4891 &rt_sf->uc.tuc_stack.ss_flags);
4892 __put_user(target_sigaltstack_used.ss_size,
4893 &rt_sf->uc.tuc_stack.ss_size);
4894 #if !defined(TARGET_PPC64)
4895 __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4896 &rt_sf->uc.tuc_regs);
4897 #endif
4898 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4899 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4902 #if defined(TARGET_PPC64)
4903 mctx = &rt_sf->uc.tuc_sigcontext.mcontext;
4904 trampptr = &rt_sf->trampoline[0];
4905 #else
4906 mctx = &rt_sf->uc.tuc_mcontext;
4907 trampptr = (uint32_t *)&rt_sf->uc.tuc_mcontext.tramp;
4908 #endif
4910 save_user_regs(env, mctx);
4911 encode_trampoline(TARGET_NR_rt_sigreturn, trampptr);
4913 /* The kernel checks for the presence of a VDSO here. We don't
4914 emulate a vdso, so use a sigreturn system call. */
4915 env->lr = (target_ulong) h2g(trampptr);
4917 /* Turn off all fp exceptions. */
4918 env->fpscr = 0;
4920 /* Create a stack frame for the caller of the handler. */
4921 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4922 err |= put_user(env->gpr[1], newsp, target_ulong);
4924 if (err)
4925 goto sigsegv;
4927 /* Set up registers for signal handler. */
4928 env->gpr[1] = newsp;
4929 env->gpr[3] = (target_ulong) sig;
4930 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4931 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4932 env->gpr[6] = (target_ulong) h2g(rt_sf);
4934 #if defined(TARGET_PPC64)
4935 if (get_ppc64_abi(image) < 2) {
4936 /* ELFv1 PPC64 function pointers are pointers to OPD entries. */
4937 struct target_func_ptr *handler =
4938 (struct target_func_ptr *)g2h(ka->_sa_handler);
4939 env->nip = tswapl(handler->entry);
4940 env->gpr[2] = tswapl(handler->toc);
4941 } else {
4942 /* ELFv2 PPC64 function pointers are entry points, but R12
4943 * must also be set */
4944 env->nip = tswapl((target_ulong) ka->_sa_handler);
4945 env->gpr[12] = env->nip;
4947 #else
4948 env->nip = (target_ulong) ka->_sa_handler;
4949 #endif
4951 /* Signal handlers are entered in big-endian mode. */
4952 env->msr &= ~(1ull << MSR_LE);
4954 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4955 return;
4957 sigsegv:
4958 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4959 force_sigsegv(sig);
4963 long do_sigreturn(CPUPPCState *env)
4965 struct target_sigcontext *sc = NULL;
4966 struct target_mcontext *sr = NULL;
4967 target_ulong sr_addr = 0, sc_addr;
4968 sigset_t blocked;
4969 target_sigset_t set;
4971 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4972 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4973 goto sigsegv;
4975 #if defined(TARGET_PPC64)
4976 set.sig[0] = sc->oldmask + ((uint64_t)(sc->_unused[3]) << 32);
4977 #else
4978 __get_user(set.sig[0], &sc->oldmask);
4979 __get_user(set.sig[1], &sc->_unused[3]);
4980 #endif
4981 target_to_host_sigset_internal(&blocked, &set);
4982 set_sigmask(&blocked);
4984 __get_user(sr_addr, &sc->regs);
4985 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4986 goto sigsegv;
4987 restore_user_regs(env, sr, 1);
4989 unlock_user_struct(sr, sr_addr, 1);
4990 unlock_user_struct(sc, sc_addr, 1);
4991 return -TARGET_QEMU_ESIGRETURN;
4993 sigsegv:
4994 unlock_user_struct(sr, sr_addr, 1);
4995 unlock_user_struct(sc, sc_addr, 1);
4996 force_sig(TARGET_SIGSEGV);
4997 return -TARGET_QEMU_ESIGRETURN;
5000 /* See arch/powerpc/kernel/signal_32.c. */
5001 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
5003 struct target_mcontext *mcp;
5004 target_ulong mcp_addr;
5005 sigset_t blocked;
5006 target_sigset_t set;
5008 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
5009 sizeof (set)))
5010 return 1;
5012 #if defined(TARGET_PPC64)
5013 mcp_addr = h2g(ucp) +
5014 offsetof(struct target_ucontext, tuc_sigcontext.mcontext);
5015 #else
5016 __get_user(mcp_addr, &ucp->tuc_regs);
5017 #endif
5019 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
5020 return 1;
5022 target_to_host_sigset_internal(&blocked, &set);
5023 set_sigmask(&blocked);
5024 restore_user_regs(env, mcp, sig);
5026 unlock_user_struct(mcp, mcp_addr, 1);
5027 return 0;
5030 long do_rt_sigreturn(CPUPPCState *env)
5032 struct target_rt_sigframe *rt_sf = NULL;
5033 target_ulong rt_sf_addr;
5035 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
5036 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
5037 goto sigsegv;
5039 if (do_setcontext(&rt_sf->uc, env, 1))
5040 goto sigsegv;
5042 do_sigaltstack(rt_sf_addr
5043 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
5044 0, env->gpr[1]);
5046 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5047 return -TARGET_QEMU_ESIGRETURN;
5049 sigsegv:
5050 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5051 force_sig(TARGET_SIGSEGV);
5052 return -TARGET_QEMU_ESIGRETURN;
5055 #elif defined(TARGET_M68K)
5057 struct target_sigcontext {
5058 abi_ulong sc_mask;
5059 abi_ulong sc_usp;
5060 abi_ulong sc_d0;
5061 abi_ulong sc_d1;
5062 abi_ulong sc_a0;
5063 abi_ulong sc_a1;
5064 unsigned short sc_sr;
5065 abi_ulong sc_pc;
5068 struct target_sigframe
5070 abi_ulong pretcode;
5071 int sig;
5072 int code;
5073 abi_ulong psc;
5074 char retcode[8];
5075 abi_ulong extramask[TARGET_NSIG_WORDS-1];
5076 struct target_sigcontext sc;
5079 typedef int target_greg_t;
5080 #define TARGET_NGREG 18
5081 typedef target_greg_t target_gregset_t[TARGET_NGREG];
5083 typedef struct target_fpregset {
5084 int f_fpcntl[3];
5085 int f_fpregs[8*3];
5086 } target_fpregset_t;
5088 struct target_mcontext {
5089 int version;
5090 target_gregset_t gregs;
5091 target_fpregset_t fpregs;
5094 #define TARGET_MCONTEXT_VERSION 2
5096 struct target_ucontext {
5097 abi_ulong tuc_flags;
5098 abi_ulong tuc_link;
5099 target_stack_t tuc_stack;
5100 struct target_mcontext tuc_mcontext;
5101 abi_long tuc_filler[80];
5102 target_sigset_t tuc_sigmask;
5105 struct target_rt_sigframe
5107 abi_ulong pretcode;
5108 int sig;
5109 abi_ulong pinfo;
5110 abi_ulong puc;
5111 char retcode[8];
5112 struct target_siginfo info;
5113 struct target_ucontext uc;
5116 static void setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
5117 abi_ulong mask)
5119 __put_user(mask, &sc->sc_mask);
5120 __put_user(env->aregs[7], &sc->sc_usp);
5121 __put_user(env->dregs[0], &sc->sc_d0);
5122 __put_user(env->dregs[1], &sc->sc_d1);
5123 __put_user(env->aregs[0], &sc->sc_a0);
5124 __put_user(env->aregs[1], &sc->sc_a1);
5125 __put_user(env->sr, &sc->sc_sr);
5126 __put_user(env->pc, &sc->sc_pc);
5129 static void
5130 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc)
5132 int temp;
5134 __get_user(env->aregs[7], &sc->sc_usp);
5135 __get_user(env->dregs[0], &sc->sc_d0);
5136 __get_user(env->dregs[1], &sc->sc_d1);
5137 __get_user(env->aregs[0], &sc->sc_a0);
5138 __get_user(env->aregs[1], &sc->sc_a1);
5139 __get_user(env->pc, &sc->sc_pc);
5140 __get_user(temp, &sc->sc_sr);
5141 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5145 * Determine which stack to use..
5147 static inline abi_ulong
5148 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5149 size_t frame_size)
5151 unsigned long sp;
5153 sp = regs->aregs[7];
5155 /* This is the X/Open sanctioned signal stack switching. */
5156 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5157 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5160 return ((sp - frame_size) & -8UL);
5163 static void setup_frame(int sig, struct target_sigaction *ka,
5164 target_sigset_t *set, CPUM68KState *env)
5166 struct target_sigframe *frame;
5167 abi_ulong frame_addr;
5168 abi_ulong retcode_addr;
5169 abi_ulong sc_addr;
5170 int i;
5172 frame_addr = get_sigframe(ka, env, sizeof *frame);
5173 trace_user_setup_frame(env, frame_addr);
5174 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5175 goto give_sigsegv;
5178 __put_user(sig, &frame->sig);
5180 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
5181 __put_user(sc_addr, &frame->psc);
5183 setup_sigcontext(&frame->sc, env, set->sig[0]);
5185 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5186 __put_user(set->sig[i], &frame->extramask[i - 1]);
5189 /* Set up to return from userspace. */
5191 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5192 __put_user(retcode_addr, &frame->pretcode);
5194 /* moveq #,d0; trap #0 */
5196 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
5197 (uint32_t *)(frame->retcode));
5199 /* Set up to return from userspace */
5201 env->aregs[7] = frame_addr;
5202 env->pc = ka->_sa_handler;
5204 unlock_user_struct(frame, frame_addr, 1);
5205 return;
5207 give_sigsegv:
5208 force_sigsegv(sig);
5211 static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
5212 CPUM68KState *env)
5214 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5216 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5217 __put_user(env->dregs[0], &gregs[0]);
5218 __put_user(env->dregs[1], &gregs[1]);
5219 __put_user(env->dregs[2], &gregs[2]);
5220 __put_user(env->dregs[3], &gregs[3]);
5221 __put_user(env->dregs[4], &gregs[4]);
5222 __put_user(env->dregs[5], &gregs[5]);
5223 __put_user(env->dregs[6], &gregs[6]);
5224 __put_user(env->dregs[7], &gregs[7]);
5225 __put_user(env->aregs[0], &gregs[8]);
5226 __put_user(env->aregs[1], &gregs[9]);
5227 __put_user(env->aregs[2], &gregs[10]);
5228 __put_user(env->aregs[3], &gregs[11]);
5229 __put_user(env->aregs[4], &gregs[12]);
5230 __put_user(env->aregs[5], &gregs[13]);
5231 __put_user(env->aregs[6], &gregs[14]);
5232 __put_user(env->aregs[7], &gregs[15]);
5233 __put_user(env->pc, &gregs[16]);
5234 __put_user(env->sr, &gregs[17]);
5236 return 0;
5239 static inline int target_rt_restore_ucontext(CPUM68KState *env,
5240 struct target_ucontext *uc)
5242 int temp;
5243 target_greg_t *gregs = uc->tuc_mcontext.gregs;
5245 __get_user(temp, &uc->tuc_mcontext.version);
5246 if (temp != TARGET_MCONTEXT_VERSION)
5247 goto badframe;
5249 /* restore passed registers */
5250 __get_user(env->dregs[0], &gregs[0]);
5251 __get_user(env->dregs[1], &gregs[1]);
5252 __get_user(env->dregs[2], &gregs[2]);
5253 __get_user(env->dregs[3], &gregs[3]);
5254 __get_user(env->dregs[4], &gregs[4]);
5255 __get_user(env->dregs[5], &gregs[5]);
5256 __get_user(env->dregs[6], &gregs[6]);
5257 __get_user(env->dregs[7], &gregs[7]);
5258 __get_user(env->aregs[0], &gregs[8]);
5259 __get_user(env->aregs[1], &gregs[9]);
5260 __get_user(env->aregs[2], &gregs[10]);
5261 __get_user(env->aregs[3], &gregs[11]);
5262 __get_user(env->aregs[4], &gregs[12]);
5263 __get_user(env->aregs[5], &gregs[13]);
5264 __get_user(env->aregs[6], &gregs[14]);
5265 __get_user(env->aregs[7], &gregs[15]);
5266 __get_user(env->pc, &gregs[16]);
5267 __get_user(temp, &gregs[17]);
5268 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5270 return 0;
5272 badframe:
5273 return 1;
5276 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5277 target_siginfo_t *info,
5278 target_sigset_t *set, CPUM68KState *env)
5280 struct target_rt_sigframe *frame;
5281 abi_ulong frame_addr;
5282 abi_ulong retcode_addr;
5283 abi_ulong info_addr;
5284 abi_ulong uc_addr;
5285 int err = 0;
5286 int i;
5288 frame_addr = get_sigframe(ka, env, sizeof *frame);
5289 trace_user_setup_rt_frame(env, frame_addr);
5290 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5291 goto give_sigsegv;
5294 __put_user(sig, &frame->sig);
5296 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5297 __put_user(info_addr, &frame->pinfo);
5299 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5300 __put_user(uc_addr, &frame->puc);
5302 tswap_siginfo(&frame->info, info);
5304 /* Create the ucontext */
5306 __put_user(0, &frame->uc.tuc_flags);
5307 __put_user(0, &frame->uc.tuc_link);
5308 __put_user(target_sigaltstack_used.ss_sp,
5309 &frame->uc.tuc_stack.ss_sp);
5310 __put_user(sas_ss_flags(env->aregs[7]),
5311 &frame->uc.tuc_stack.ss_flags);
5312 __put_user(target_sigaltstack_used.ss_size,
5313 &frame->uc.tuc_stack.ss_size);
5314 err |= target_rt_setup_ucontext(&frame->uc, env);
5316 if (err)
5317 goto give_sigsegv;
5319 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
5320 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5323 /* Set up to return from userspace. */
5325 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5326 __put_user(retcode_addr, &frame->pretcode);
5328 /* moveq #,d0; notb d0; trap #0 */
5330 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5331 (uint32_t *)(frame->retcode + 0));
5332 __put_user(0x4e40, (uint16_t *)(frame->retcode + 4));
5334 if (err)
5335 goto give_sigsegv;
5337 /* Set up to return from userspace */
5339 env->aregs[7] = frame_addr;
5340 env->pc = ka->_sa_handler;
5342 unlock_user_struct(frame, frame_addr, 1);
5343 return;
5345 give_sigsegv:
5346 unlock_user_struct(frame, frame_addr, 1);
5347 force_sigsegv(sig);
5350 long do_sigreturn(CPUM68KState *env)
5352 struct target_sigframe *frame;
5353 abi_ulong frame_addr = env->aregs[7] - 4;
5354 target_sigset_t target_set;
5355 sigset_t set;
5356 int i;
5358 trace_user_do_sigreturn(env, frame_addr);
5359 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5360 goto badframe;
5362 /* set blocked signals */
5364 __get_user(target_set.sig[0], &frame->sc.sc_mask);
5366 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5367 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
5370 target_to_host_sigset_internal(&set, &target_set);
5371 set_sigmask(&set);
5373 /* restore registers */
5375 restore_sigcontext(env, &frame->sc);
5377 unlock_user_struct(frame, frame_addr, 0);
5378 return -TARGET_QEMU_ESIGRETURN;
5380 badframe:
5381 force_sig(TARGET_SIGSEGV);
5382 return -TARGET_QEMU_ESIGRETURN;
5385 long do_rt_sigreturn(CPUM68KState *env)
5387 struct target_rt_sigframe *frame;
5388 abi_ulong frame_addr = env->aregs[7] - 4;
5389 target_sigset_t target_set;
5390 sigset_t set;
5392 trace_user_do_rt_sigreturn(env, frame_addr);
5393 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5394 goto badframe;
5396 target_to_host_sigset_internal(&set, &target_set);
5397 set_sigmask(&set);
5399 /* restore registers */
5401 if (target_rt_restore_ucontext(env, &frame->uc))
5402 goto badframe;
5404 if (do_sigaltstack(frame_addr +
5405 offsetof(struct target_rt_sigframe, uc.tuc_stack),
5406 0, get_sp_from_cpustate(env)) == -EFAULT)
5407 goto badframe;
5409 unlock_user_struct(frame, frame_addr, 0);
5410 return -TARGET_QEMU_ESIGRETURN;
5412 badframe:
5413 unlock_user_struct(frame, frame_addr, 0);
5414 force_sig(TARGET_SIGSEGV);
5415 return -TARGET_QEMU_ESIGRETURN;
5418 #elif defined(TARGET_ALPHA)
5420 struct target_sigcontext {
5421 abi_long sc_onstack;
5422 abi_long sc_mask;
5423 abi_long sc_pc;
5424 abi_long sc_ps;
5425 abi_long sc_regs[32];
5426 abi_long sc_ownedfp;
5427 abi_long sc_fpregs[32];
5428 abi_ulong sc_fpcr;
5429 abi_ulong sc_fp_control;
5430 abi_ulong sc_reserved1;
5431 abi_ulong sc_reserved2;
5432 abi_ulong sc_ssize;
5433 abi_ulong sc_sbase;
5434 abi_ulong sc_traparg_a0;
5435 abi_ulong sc_traparg_a1;
5436 abi_ulong sc_traparg_a2;
5437 abi_ulong sc_fp_trap_pc;
5438 abi_ulong sc_fp_trigger_sum;
5439 abi_ulong sc_fp_trigger_inst;
5442 struct target_ucontext {
5443 abi_ulong tuc_flags;
5444 abi_ulong tuc_link;
5445 abi_ulong tuc_osf_sigmask;
5446 target_stack_t tuc_stack;
5447 struct target_sigcontext tuc_mcontext;
5448 target_sigset_t tuc_sigmask;
5451 struct target_sigframe {
5452 struct target_sigcontext sc;
5453 unsigned int retcode[3];
5456 struct target_rt_sigframe {
5457 target_siginfo_t info;
5458 struct target_ucontext uc;
5459 unsigned int retcode[3];
5462 #define INSN_MOV_R30_R16 0x47fe0410
5463 #define INSN_LDI_R0 0x201f0000
5464 #define INSN_CALLSYS 0x00000083
5466 static void setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
5467 abi_ulong frame_addr, target_sigset_t *set)
5469 int i;
5471 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5472 __put_user(set->sig[0], &sc->sc_mask);
5473 __put_user(env->pc, &sc->sc_pc);
5474 __put_user(8, &sc->sc_ps);
5476 for (i = 0; i < 31; ++i) {
5477 __put_user(env->ir[i], &sc->sc_regs[i]);
5479 __put_user(0, &sc->sc_regs[31]);
5481 for (i = 0; i < 31; ++i) {
5482 __put_user(env->fir[i], &sc->sc_fpregs[i]);
5484 __put_user(0, &sc->sc_fpregs[31]);
5485 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5487 __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5488 __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5489 __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5492 static void restore_sigcontext(CPUAlphaState *env,
5493 struct target_sigcontext *sc)
5495 uint64_t fpcr;
5496 int i;
5498 __get_user(env->pc, &sc->sc_pc);
5500 for (i = 0; i < 31; ++i) {
5501 __get_user(env->ir[i], &sc->sc_regs[i]);
5503 for (i = 0; i < 31; ++i) {
5504 __get_user(env->fir[i], &sc->sc_fpregs[i]);
5507 __get_user(fpcr, &sc->sc_fpcr);
5508 cpu_alpha_store_fpcr(env, fpcr);
5511 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
5512 CPUAlphaState *env,
5513 unsigned long framesize)
5515 abi_ulong sp = env->ir[IR_SP];
5517 /* This is the X/Open sanctioned signal stack switching. */
5518 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5519 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5521 return (sp - framesize) & -32;
5524 static void setup_frame(int sig, struct target_sigaction *ka,
5525 target_sigset_t *set, CPUAlphaState *env)
5527 abi_ulong frame_addr, r26;
5528 struct target_sigframe *frame;
5529 int err = 0;
5531 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5532 trace_user_setup_frame(env, frame_addr);
5533 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5534 goto give_sigsegv;
5537 setup_sigcontext(&frame->sc, env, frame_addr, set);
5539 if (ka->sa_restorer) {
5540 r26 = ka->sa_restorer;
5541 } else {
5542 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5543 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5544 &frame->retcode[1]);
5545 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5546 /* imb() */
5547 r26 = frame_addr;
5550 unlock_user_struct(frame, frame_addr, 1);
5552 if (err) {
5553 give_sigsegv:
5554 force_sigsegv(sig);
5555 return;
5558 env->ir[IR_RA] = r26;
5559 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5560 env->ir[IR_A0] = sig;
5561 env->ir[IR_A1] = 0;
5562 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5563 env->ir[IR_SP] = frame_addr;
5566 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5567 target_siginfo_t *info,
5568 target_sigset_t *set, CPUAlphaState *env)
5570 abi_ulong frame_addr, r26;
5571 struct target_rt_sigframe *frame;
5572 int i, err = 0;
5574 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5575 trace_user_setup_rt_frame(env, frame_addr);
5576 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5577 goto give_sigsegv;
5580 tswap_siginfo(&frame->info, info);
5582 __put_user(0, &frame->uc.tuc_flags);
5583 __put_user(0, &frame->uc.tuc_link);
5584 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5585 __put_user(target_sigaltstack_used.ss_sp,
5586 &frame->uc.tuc_stack.ss_sp);
5587 __put_user(sas_ss_flags(env->ir[IR_SP]),
5588 &frame->uc.tuc_stack.ss_flags);
5589 __put_user(target_sigaltstack_used.ss_size,
5590 &frame->uc.tuc_stack.ss_size);
5591 setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
5592 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
5593 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
5596 if (ka->sa_restorer) {
5597 r26 = ka->sa_restorer;
5598 } else {
5599 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5600 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5601 &frame->retcode[1]);
5602 __put_user(INSN_CALLSYS, &frame->retcode[2]);
5603 /* imb(); */
5604 r26 = frame_addr;
5607 if (err) {
5608 give_sigsegv:
5609 force_sigsegv(sig);
5610 return;
5613 env->ir[IR_RA] = r26;
5614 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5615 env->ir[IR_A0] = sig;
5616 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5617 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5618 env->ir[IR_SP] = frame_addr;
5621 long do_sigreturn(CPUAlphaState *env)
5623 struct target_sigcontext *sc;
5624 abi_ulong sc_addr = env->ir[IR_A0];
5625 target_sigset_t target_set;
5626 sigset_t set;
5628 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5629 goto badframe;
5632 target_sigemptyset(&target_set);
5633 __get_user(target_set.sig[0], &sc->sc_mask);
5635 target_to_host_sigset_internal(&set, &target_set);
5636 set_sigmask(&set);
5638 restore_sigcontext(env, sc);
5639 unlock_user_struct(sc, sc_addr, 0);
5640 return -TARGET_QEMU_ESIGRETURN;
5642 badframe:
5643 force_sig(TARGET_SIGSEGV);
5644 return -TARGET_QEMU_ESIGRETURN;
5647 long do_rt_sigreturn(CPUAlphaState *env)
5649 abi_ulong frame_addr = env->ir[IR_A0];
5650 struct target_rt_sigframe *frame;
5651 sigset_t set;
5653 trace_user_do_rt_sigreturn(env, frame_addr);
5654 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5655 goto badframe;
5657 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5658 set_sigmask(&set);
5660 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5661 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5662 uc.tuc_stack),
5663 0, env->ir[IR_SP]) == -EFAULT) {
5664 goto badframe;
5667 unlock_user_struct(frame, frame_addr, 0);
5668 return -TARGET_QEMU_ESIGRETURN;
5671 badframe:
5672 unlock_user_struct(frame, frame_addr, 0);
5673 force_sig(TARGET_SIGSEGV);
5674 return -TARGET_QEMU_ESIGRETURN;
5677 #elif defined(TARGET_TILEGX)
5679 struct target_sigcontext {
5680 union {
5681 /* General-purpose registers. */
5682 abi_ulong gregs[56];
5683 struct {
5684 abi_ulong __gregs[53];
5685 abi_ulong tp; /* Aliases gregs[TREG_TP]. */
5686 abi_ulong sp; /* Aliases gregs[TREG_SP]. */
5687 abi_ulong lr; /* Aliases gregs[TREG_LR]. */
5690 abi_ulong pc; /* Program counter. */
5691 abi_ulong ics; /* In Interrupt Critical Section? */
5692 abi_ulong faultnum; /* Fault number. */
5693 abi_ulong pad[5];
5696 struct target_ucontext {
5697 abi_ulong tuc_flags;
5698 abi_ulong tuc_link;
5699 target_stack_t tuc_stack;
5700 struct target_sigcontext tuc_mcontext;
5701 target_sigset_t tuc_sigmask; /* mask last for extensibility */
5704 struct target_rt_sigframe {
5705 unsigned char save_area[16]; /* caller save area */
5706 struct target_siginfo info;
5707 struct target_ucontext uc;
5708 abi_ulong retcode[2];
5711 #define INSN_MOVELI_R10_139 0x00045fe551483000ULL /* { moveli r10, 139 } */
5712 #define INSN_SWINT1 0x286b180051485000ULL /* { swint1 } */
5715 static void setup_sigcontext(struct target_sigcontext *sc,
5716 CPUArchState *env, int signo)
5718 int i;
5720 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5721 __put_user(env->regs[i], &sc->gregs[i]);
5724 __put_user(env->pc, &sc->pc);
5725 __put_user(0, &sc->ics);
5726 __put_user(signo, &sc->faultnum);
5729 static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
5731 int i;
5733 for (i = 0; i < TILEGX_R_COUNT; ++i) {
5734 __get_user(env->regs[i], &sc->gregs[i]);
5737 __get_user(env->pc, &sc->pc);
5740 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
5741 size_t frame_size)
5743 unsigned long sp = env->regs[TILEGX_R_SP];
5745 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
5746 return -1UL;
5749 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
5750 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5753 sp -= frame_size;
5754 sp &= -16UL;
5755 return sp;
5758 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5759 target_siginfo_t *info,
5760 target_sigset_t *set, CPUArchState *env)
5762 abi_ulong frame_addr;
5763 struct target_rt_sigframe *frame;
5764 unsigned long restorer;
5766 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5767 trace_user_setup_rt_frame(env, frame_addr);
5768 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5769 goto give_sigsegv;
5772 /* Always write at least the signal number for the stack backtracer. */
5773 if (ka->sa_flags & TARGET_SA_SIGINFO) {
5774 /* At sigreturn time, restore the callee-save registers too. */
5775 tswap_siginfo(&frame->info, info);
5776 /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
5777 } else {
5778 __put_user(info->si_signo, &frame->info.si_signo);
5781 /* Create the ucontext. */
5782 __put_user(0, &frame->uc.tuc_flags);
5783 __put_user(0, &frame->uc.tuc_link);
5784 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
5785 __put_user(sas_ss_flags(env->regs[TILEGX_R_SP]),
5786 &frame->uc.tuc_stack.ss_flags);
5787 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
5788 setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
5790 if (ka->sa_flags & TARGET_SA_RESTORER) {
5791 restorer = (unsigned long) ka->sa_restorer;
5792 } else {
5793 __put_user(INSN_MOVELI_R10_139, &frame->retcode[0]);
5794 __put_user(INSN_SWINT1, &frame->retcode[1]);
5795 restorer = frame_addr + offsetof(struct target_rt_sigframe, retcode);
5797 env->pc = (unsigned long) ka->_sa_handler;
5798 env->regs[TILEGX_R_SP] = (unsigned long) frame;
5799 env->regs[TILEGX_R_LR] = restorer;
5800 env->regs[0] = (unsigned long) sig;
5801 env->regs[1] = (unsigned long) &frame->info;
5802 env->regs[2] = (unsigned long) &frame->uc;
5803 /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
5805 unlock_user_struct(frame, frame_addr, 1);
5806 return;
5808 give_sigsegv:
5809 force_sigsegv(sig);
5812 long do_rt_sigreturn(CPUTLGState *env)
5814 abi_ulong frame_addr = env->regs[TILEGX_R_SP];
5815 struct target_rt_sigframe *frame;
5816 sigset_t set;
5818 trace_user_do_rt_sigreturn(env, frame_addr);
5819 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5820 goto badframe;
5822 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
5823 set_sigmask(&set);
5825 restore_sigcontext(env, &frame->uc.tuc_mcontext);
5826 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
5827 uc.tuc_stack),
5828 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
5829 goto badframe;
5832 unlock_user_struct(frame, frame_addr, 0);
5833 return -TARGET_QEMU_ESIGRETURN;
5836 badframe:
5837 unlock_user_struct(frame, frame_addr, 0);
5838 force_sig(TARGET_SIGSEGV);
5839 return -TARGET_QEMU_ESIGRETURN;
5842 #else
5844 static void setup_frame(int sig, struct target_sigaction *ka,
5845 target_sigset_t *set, CPUArchState *env)
5847 fprintf(stderr, "setup_frame: not implemented\n");
5850 static void setup_rt_frame(int sig, struct target_sigaction *ka,
5851 target_siginfo_t *info,
5852 target_sigset_t *set, CPUArchState *env)
5854 fprintf(stderr, "setup_rt_frame: not implemented\n");
5857 long do_sigreturn(CPUArchState *env)
5859 fprintf(stderr, "do_sigreturn: not implemented\n");
5860 return -TARGET_ENOSYS;
5863 long do_rt_sigreturn(CPUArchState *env)
5865 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
5866 return -TARGET_ENOSYS;
5869 #endif
5871 static void handle_pending_signal(CPUArchState *cpu_env, int sig,
5872 struct emulated_sigtable *k)
5874 CPUState *cpu = ENV_GET_CPU(cpu_env);
5875 abi_ulong handler;
5876 sigset_t set;
5877 target_sigset_t target_old_set;
5878 struct target_sigaction *sa;
5879 TaskState *ts = cpu->opaque;
5881 trace_user_handle_signal(cpu_env, sig);
5882 /* dequeue signal */
5883 k->pending = 0;
5885 sig = gdb_handlesig(cpu, sig);
5886 if (!sig) {
5887 sa = NULL;
5888 handler = TARGET_SIG_IGN;
5889 } else {
5890 sa = &sigact_table[sig - 1];
5891 handler = sa->_sa_handler;
5894 if (do_strace) {
5895 print_taken_signal(sig, &k->info);
5898 if (handler == TARGET_SIG_DFL) {
5899 /* default handler : ignore some signal. The other are job control or fatal */
5900 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5901 kill(getpid(),SIGSTOP);
5902 } else if (sig != TARGET_SIGCHLD &&
5903 sig != TARGET_SIGURG &&
5904 sig != TARGET_SIGWINCH &&
5905 sig != TARGET_SIGCONT) {
5906 dump_core_and_abort(sig);
5908 } else if (handler == TARGET_SIG_IGN) {
5909 /* ignore sig */
5910 } else if (handler == TARGET_SIG_ERR) {
5911 dump_core_and_abort(sig);
5912 } else {
5913 /* compute the blocked signals during the handler execution */
5914 sigset_t *blocked_set;
5916 target_to_host_sigset(&set, &sa->sa_mask);
5917 /* SA_NODEFER indicates that the current signal should not be
5918 blocked during the handler */
5919 if (!(sa->sa_flags & TARGET_SA_NODEFER))
5920 sigaddset(&set, target_to_host_signal(sig));
5922 /* save the previous blocked signal state to restore it at the
5923 end of the signal execution (see do_sigreturn) */
5924 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
5926 /* block signals in the handler */
5927 blocked_set = ts->in_sigsuspend ?
5928 &ts->sigsuspend_mask : &ts->signal_mask;
5929 sigorset(&ts->signal_mask, blocked_set, &set);
5930 ts->in_sigsuspend = 0;
5932 /* if the CPU is in VM86 mode, we restore the 32 bit values */
5933 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
5935 CPUX86State *env = cpu_env;
5936 if (env->eflags & VM_MASK)
5937 save_v86_state(env);
5939 #endif
5940 /* prepare the stack frame of the virtual CPU */
5941 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \
5942 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX)
5943 /* These targets do not have traditional signals. */
5944 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
5945 #else
5946 if (sa->sa_flags & TARGET_SA_SIGINFO)
5947 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
5948 else
5949 setup_frame(sig, sa, &target_old_set, cpu_env);
5950 #endif
5951 if (sa->sa_flags & TARGET_SA_RESETHAND) {
5952 sa->_sa_handler = TARGET_SIG_DFL;
5957 void process_pending_signals(CPUArchState *cpu_env)
5959 CPUState *cpu = ENV_GET_CPU(cpu_env);
5960 int sig;
5961 TaskState *ts = cpu->opaque;
5962 sigset_t set;
5963 sigset_t *blocked_set;
5965 while (atomic_read(&ts->signal_pending)) {
5966 /* FIXME: This is not threadsafe. */
5967 sigfillset(&set);
5968 sigprocmask(SIG_SETMASK, &set, 0);
5970 restart_scan:
5971 sig = ts->sync_signal.pending;
5972 if (sig) {
5973 /* Synchronous signals are forced,
5974 * see force_sig_info() and callers in Linux
5975 * Note that not all of our queue_signal() calls in QEMU correspond
5976 * to force_sig_info() calls in Linux (some are send_sig_info()).
5977 * However it seems like a kernel bug to me to allow the process
5978 * to block a synchronous signal since it could then just end up
5979 * looping round and round indefinitely.
5981 if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
5982 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
5983 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
5984 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
5987 handle_pending_signal(cpu_env, sig, &ts->sync_signal);
5990 for (sig = 1; sig <= TARGET_NSIG; sig++) {
5991 blocked_set = ts->in_sigsuspend ?
5992 &ts->sigsuspend_mask : &ts->signal_mask;
5994 if (ts->sigtab[sig - 1].pending &&
5995 (!sigismember(blocked_set,
5996 target_to_host_signal_table[sig]))) {
5997 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
5998 /* Restart scan from the beginning, as handle_pending_signal
5999 * might have resulted in a new synchronous signal (eg SIGSEGV).
6001 goto restart_scan;
6005 /* if no signal is pending, unblock signals and recheck (the act
6006 * of unblocking might cause us to take another host signal which
6007 * will set signal_pending again).
6009 atomic_set(&ts->signal_pending, 0);
6010 ts->in_sigsuspend = 0;
6011 set = ts->signal_mask;
6012 sigdelset(&set, SIGSEGV);
6013 sigdelset(&set, SIGBUS);
6014 sigprocmask(SIG_SETMASK, &set, 0);
6016 ts->in_sigsuspend = 0;