Merge branch 'for-upstream' of git://repo.or.cz/qemu/agraf
[qemu.git] / linux-user / signal.c
1 /*
2 * Emulation of Linux signals
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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/>.
18 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <sys/ucontext.h>
27 #include <sys/resource.h>
28
29 #include "qemu.h"
30 #include "qemu-common.h"
31 #include "target_signal.h"
32
33 //#define DEBUG_SIGNAL
34
35 static struct target_sigaltstack target_sigaltstack_used = {
36 .ss_sp = 0,
37 .ss_size = 0,
38 .ss_flags = TARGET_SS_DISABLE,
39 };
40
41 static struct target_sigaction sigact_table[TARGET_NSIG];
42
43 static void host_signal_handler(int host_signum, siginfo_t *info,
44 void *puc);
45
46 static uint8_t host_to_target_signal_table[_NSIG] = {
47 [SIGHUP] = TARGET_SIGHUP,
48 [SIGINT] = TARGET_SIGINT,
49 [SIGQUIT] = TARGET_SIGQUIT,
50 [SIGILL] = TARGET_SIGILL,
51 [SIGTRAP] = TARGET_SIGTRAP,
52 [SIGABRT] = TARGET_SIGABRT,
53 /* [SIGIOT] = TARGET_SIGIOT,*/
54 [SIGBUS] = TARGET_SIGBUS,
55 [SIGFPE] = TARGET_SIGFPE,
56 [SIGKILL] = TARGET_SIGKILL,
57 [SIGUSR1] = TARGET_SIGUSR1,
58 [SIGSEGV] = TARGET_SIGSEGV,
59 [SIGUSR2] = TARGET_SIGUSR2,
60 [SIGPIPE] = TARGET_SIGPIPE,
61 [SIGALRM] = TARGET_SIGALRM,
62 [SIGTERM] = TARGET_SIGTERM,
63 #ifdef SIGSTKFLT
64 [SIGSTKFLT] = TARGET_SIGSTKFLT,
65 #endif
66 [SIGCHLD] = TARGET_SIGCHLD,
67 [SIGCONT] = TARGET_SIGCONT,
68 [SIGSTOP] = TARGET_SIGSTOP,
69 [SIGTSTP] = TARGET_SIGTSTP,
70 [SIGTTIN] = TARGET_SIGTTIN,
71 [SIGTTOU] = TARGET_SIGTTOU,
72 [SIGURG] = TARGET_SIGURG,
73 [SIGXCPU] = TARGET_SIGXCPU,
74 [SIGXFSZ] = TARGET_SIGXFSZ,
75 [SIGVTALRM] = TARGET_SIGVTALRM,
76 [SIGPROF] = TARGET_SIGPROF,
77 [SIGWINCH] = TARGET_SIGWINCH,
78 [SIGIO] = TARGET_SIGIO,
79 [SIGPWR] = TARGET_SIGPWR,
80 [SIGSYS] = TARGET_SIGSYS,
81 /* next signals stay the same */
82 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
83 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
84 To fix this properly we need to do manual signal delivery multiplexed
85 over a single host signal. */
86 [__SIGRTMIN] = __SIGRTMAX,
87 [__SIGRTMAX] = __SIGRTMIN,
88 };
89 static uint8_t target_to_host_signal_table[_NSIG];
90
91 static inline int on_sig_stack(unsigned long sp)
92 {
93 return (sp - target_sigaltstack_used.ss_sp
94 < target_sigaltstack_used.ss_size);
95 }
96
97 static inline int sas_ss_flags(unsigned long sp)
98 {
99 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
100 : on_sig_stack(sp) ? SS_ONSTACK : 0);
101 }
102
103 int host_to_target_signal(int sig)
104 {
105 if (sig >= _NSIG)
106 return sig;
107 return host_to_target_signal_table[sig];
108 }
109
110 int target_to_host_signal(int sig)
111 {
112 if (sig >= _NSIG)
113 return sig;
114 return target_to_host_signal_table[sig];
115 }
116
117 static inline void target_sigemptyset(target_sigset_t *set)
118 {
119 memset(set, 0, sizeof(*set));
120 }
121
122 static inline void target_sigaddset(target_sigset_t *set, int signum)
123 {
124 signum--;
125 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126 set->sig[signum / TARGET_NSIG_BPW] |= mask;
127 }
128
129 static inline int target_sigismember(const target_sigset_t *set, int signum)
130 {
131 signum--;
132 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
133 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
134 }
135
136 static void host_to_target_sigset_internal(target_sigset_t *d,
137 const sigset_t *s)
138 {
139 int i;
140 target_sigemptyset(d);
141 for (i = 1; i <= TARGET_NSIG; i++) {
142 if (sigismember(s, i)) {
143 target_sigaddset(d, host_to_target_signal(i));
144 }
145 }
146 }
147
148 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
149 {
150 target_sigset_t d1;
151 int i;
152
153 host_to_target_sigset_internal(&d1, s);
154 for(i = 0;i < TARGET_NSIG_WORDS; i++)
155 d->sig[i] = tswapal(d1.sig[i]);
156 }
157
158 static void target_to_host_sigset_internal(sigset_t *d,
159 const target_sigset_t *s)
160 {
161 int i;
162 sigemptyset(d);
163 for (i = 1; i <= TARGET_NSIG; i++) {
164 if (target_sigismember(s, i)) {
165 sigaddset(d, target_to_host_signal(i));
166 }
167 }
168 }
169
170 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
171 {
172 target_sigset_t s1;
173 int i;
174
175 for(i = 0;i < TARGET_NSIG_WORDS; i++)
176 s1.sig[i] = tswapal(s->sig[i]);
177 target_to_host_sigset_internal(d, &s1);
178 }
179
180 void host_to_target_old_sigset(abi_ulong *old_sigset,
181 const sigset_t *sigset)
182 {
183 target_sigset_t d;
184 host_to_target_sigset(&d, sigset);
185 *old_sigset = d.sig[0];
186 }
187
188 void target_to_host_old_sigset(sigset_t *sigset,
189 const abi_ulong *old_sigset)
190 {
191 target_sigset_t d;
192 int i;
193
194 d.sig[0] = *old_sigset;
195 for(i = 1;i < TARGET_NSIG_WORDS; i++)
196 d.sig[i] = 0;
197 target_to_host_sigset(sigset, &d);
198 }
199
200 /* siginfo conversion */
201
202 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
203 const siginfo_t *info)
204 {
205 int sig;
206 sig = host_to_target_signal(info->si_signo);
207 tinfo->si_signo = sig;
208 tinfo->si_errno = 0;
209 tinfo->si_code = info->si_code;
210 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
211 sig == SIGBUS || sig == SIGTRAP) {
212 /* should never come here, but who knows. The information for
213 the target is irrelevant */
214 tinfo->_sifields._sigfault._addr = 0;
215 } else if (sig == SIGIO) {
216 tinfo->_sifields._sigpoll._fd = info->si_fd;
217 } else if (sig >= TARGET_SIGRTMIN) {
218 tinfo->_sifields._rt._pid = info->si_pid;
219 tinfo->_sifields._rt._uid = info->si_uid;
220 /* XXX: potential problem if 64 bit */
221 tinfo->_sifields._rt._sigval.sival_ptr =
222 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
223 }
224 }
225
226 static void tswap_siginfo(target_siginfo_t *tinfo,
227 const target_siginfo_t *info)
228 {
229 int sig;
230 sig = info->si_signo;
231 tinfo->si_signo = tswap32(sig);
232 tinfo->si_errno = tswap32(info->si_errno);
233 tinfo->si_code = tswap32(info->si_code);
234 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
235 sig == SIGBUS || sig == SIGTRAP) {
236 tinfo->_sifields._sigfault._addr =
237 tswapal(info->_sifields._sigfault._addr);
238 } else if (sig == SIGIO) {
239 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
240 } else if (sig >= TARGET_SIGRTMIN) {
241 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
242 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
243 tinfo->_sifields._rt._sigval.sival_ptr =
244 tswapal(info->_sifields._rt._sigval.sival_ptr);
245 }
246 }
247
248
249 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
250 {
251 host_to_target_siginfo_noswap(tinfo, info);
252 tswap_siginfo(tinfo, tinfo);
253 }
254
255 /* XXX: we support only POSIX RT signals are used. */
256 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
257 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
258 {
259 info->si_signo = tswap32(tinfo->si_signo);
260 info->si_errno = tswap32(tinfo->si_errno);
261 info->si_code = tswap32(tinfo->si_code);
262 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
263 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
264 info->si_value.sival_ptr =
265 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
266 }
267
268 static int fatal_signal (int sig)
269 {
270 switch (sig) {
271 case TARGET_SIGCHLD:
272 case TARGET_SIGURG:
273 case TARGET_SIGWINCH:
274 /* Ignored by default. */
275 return 0;
276 case TARGET_SIGCONT:
277 case TARGET_SIGSTOP:
278 case TARGET_SIGTSTP:
279 case TARGET_SIGTTIN:
280 case TARGET_SIGTTOU:
281 /* Job control signals. */
282 return 0;
283 default:
284 return 1;
285 }
286 }
287
288 /* returns 1 if given signal should dump core if not handled */
289 static int core_dump_signal(int sig)
290 {
291 switch (sig) {
292 case TARGET_SIGABRT:
293 case TARGET_SIGFPE:
294 case TARGET_SIGILL:
295 case TARGET_SIGQUIT:
296 case TARGET_SIGSEGV:
297 case TARGET_SIGTRAP:
298 case TARGET_SIGBUS:
299 return (1);
300 default:
301 return (0);
302 }
303 }
304
305 void signal_init(void)
306 {
307 struct sigaction act;
308 struct sigaction oact;
309 int i, j;
310 int host_sig;
311
312 /* generate signal conversion tables */
313 for(i = 1; i < _NSIG; i++) {
314 if (host_to_target_signal_table[i] == 0)
315 host_to_target_signal_table[i] = i;
316 }
317 for(i = 1; i < _NSIG; i++) {
318 j = host_to_target_signal_table[i];
319 target_to_host_signal_table[j] = i;
320 }
321
322 /* set all host signal handlers. ALL signals are blocked during
323 the handlers to serialize them. */
324 memset(sigact_table, 0, sizeof(sigact_table));
325
326 sigfillset(&act.sa_mask);
327 act.sa_flags = SA_SIGINFO;
328 act.sa_sigaction = host_signal_handler;
329 for(i = 1; i <= TARGET_NSIG; i++) {
330 host_sig = target_to_host_signal(i);
331 sigaction(host_sig, NULL, &oact);
332 if (oact.sa_sigaction == (void *)SIG_IGN) {
333 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
334 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
335 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
336 }
337 /* If there's already a handler installed then something has
338 gone horribly wrong, so don't even try to handle that case. */
339 /* Install some handlers for our own use. We need at least
340 SIGSEGV and SIGBUS, to detect exceptions. We can not just
341 trap all signals because it affects syscall interrupt
342 behavior. But do trap all default-fatal signals. */
343 if (fatal_signal (i))
344 sigaction(host_sig, &act, NULL);
345 }
346 }
347
348 /* signal queue handling */
349
350 static inline struct sigqueue *alloc_sigqueue(CPUState *env)
351 {
352 TaskState *ts = env->opaque;
353 struct sigqueue *q = ts->first_free;
354 if (!q)
355 return NULL;
356 ts->first_free = q->next;
357 return q;
358 }
359
360 static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
361 {
362 TaskState *ts = env->opaque;
363 q->next = ts->first_free;
364 ts->first_free = q;
365 }
366
367 /* abort execution with signal */
368 static void QEMU_NORETURN force_sig(int target_sig)
369 {
370 TaskState *ts = (TaskState *)thread_env->opaque;
371 int host_sig, core_dumped = 0;
372 struct sigaction act;
373 host_sig = target_to_host_signal(target_sig);
374 gdb_signalled(thread_env, target_sig);
375
376 /* dump core if supported by target binary format */
377 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
378 stop_all_tasks();
379 core_dumped =
380 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
381 }
382 if (core_dumped) {
383 /* we already dumped the core of target process, we don't want
384 * a coredump of qemu itself */
385 struct rlimit nodump;
386 getrlimit(RLIMIT_CORE, &nodump);
387 nodump.rlim_cur=0;
388 setrlimit(RLIMIT_CORE, &nodump);
389 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
390 target_sig, strsignal(host_sig), "core dumped" );
391 }
392
393 /* The proper exit code for dying from an uncaught signal is
394 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
395 * a negative value. To get the proper exit code we need to
396 * actually die from an uncaught signal. Here the default signal
397 * handler is installed, we send ourself a signal and we wait for
398 * it to arrive. */
399 sigfillset(&act.sa_mask);
400 act.sa_handler = SIG_DFL;
401 sigaction(host_sig, &act, NULL);
402
403 /* For some reason raise(host_sig) doesn't send the signal when
404 * statically linked on x86-64. */
405 kill(getpid(), host_sig);
406
407 /* Make sure the signal isn't masked (just reuse the mask inside
408 of act) */
409 sigdelset(&act.sa_mask, host_sig);
410 sigsuspend(&act.sa_mask);
411
412 /* unreachable */
413 abort();
414 }
415
416 /* queue a signal so that it will be send to the virtual CPU as soon
417 as possible */
418 int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
419 {
420 TaskState *ts = env->opaque;
421 struct emulated_sigtable *k;
422 struct sigqueue *q, **pq;
423 abi_ulong handler;
424 int queue;
425
426 #if defined(DEBUG_SIGNAL)
427 fprintf(stderr, "queue_signal: sig=%d\n",
428 sig);
429 #endif
430 k = &ts->sigtab[sig - 1];
431 queue = gdb_queuesig ();
432 handler = sigact_table[sig - 1]._sa_handler;
433 if (!queue && handler == TARGET_SIG_DFL) {
434 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
435 kill(getpid(),SIGSTOP);
436 return 0;
437 } else
438 /* default handler : ignore some signal. The other are fatal */
439 if (sig != TARGET_SIGCHLD &&
440 sig != TARGET_SIGURG &&
441 sig != TARGET_SIGWINCH &&
442 sig != TARGET_SIGCONT) {
443 force_sig(sig);
444 } else {
445 return 0; /* indicate ignored */
446 }
447 } else if (!queue && handler == TARGET_SIG_IGN) {
448 /* ignore signal */
449 return 0;
450 } else if (!queue && handler == TARGET_SIG_ERR) {
451 force_sig(sig);
452 } else {
453 pq = &k->first;
454 if (sig < TARGET_SIGRTMIN) {
455 /* if non real time signal, we queue exactly one signal */
456 if (!k->pending)
457 q = &k->info;
458 else
459 return 0;
460 } else {
461 if (!k->pending) {
462 /* first signal */
463 q = &k->info;
464 } else {
465 q = alloc_sigqueue(env);
466 if (!q)
467 return -EAGAIN;
468 while (*pq != NULL)
469 pq = &(*pq)->next;
470 }
471 }
472 *pq = q;
473 q->info = *info;
474 q->next = NULL;
475 k->pending = 1;
476 /* signal that a new signal is pending */
477 ts->signal_pending = 1;
478 return 1; /* indicates that the signal was queued */
479 }
480 }
481
482 static void host_signal_handler(int host_signum, siginfo_t *info,
483 void *puc)
484 {
485 int sig;
486 target_siginfo_t tinfo;
487
488 /* the CPU emulator uses some host signals to detect exceptions,
489 we forward to it some signals */
490 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
491 && info->si_code > 0) {
492 if (cpu_signal_handler(host_signum, info, puc))
493 return;
494 }
495
496 /* get target signal number */
497 sig = host_to_target_signal(host_signum);
498 if (sig < 1 || sig > TARGET_NSIG)
499 return;
500 #if defined(DEBUG_SIGNAL)
501 fprintf(stderr, "qemu: got signal %d\n", sig);
502 #endif
503 host_to_target_siginfo_noswap(&tinfo, info);
504 if (queue_signal(thread_env, sig, &tinfo) == 1) {
505 /* interrupt the virtual CPU as soon as possible */
506 cpu_exit(thread_env);
507 }
508 }
509
510 /* do_sigaltstack() returns target values and errnos. */
511 /* compare linux/kernel/signal.c:do_sigaltstack() */
512 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
513 {
514 int ret;
515 struct target_sigaltstack oss;
516
517 /* XXX: test errors */
518 if(uoss_addr)
519 {
520 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
521 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
522 __put_user(sas_ss_flags(sp), &oss.ss_flags);
523 }
524
525 if(uss_addr)
526 {
527 struct target_sigaltstack *uss;
528 struct target_sigaltstack ss;
529
530 ret = -TARGET_EFAULT;
531 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
532 || __get_user(ss.ss_sp, &uss->ss_sp)
533 || __get_user(ss.ss_size, &uss->ss_size)
534 || __get_user(ss.ss_flags, &uss->ss_flags))
535 goto out;
536 unlock_user_struct(uss, uss_addr, 0);
537
538 ret = -TARGET_EPERM;
539 if (on_sig_stack(sp))
540 goto out;
541
542 ret = -TARGET_EINVAL;
543 if (ss.ss_flags != TARGET_SS_DISABLE
544 && ss.ss_flags != TARGET_SS_ONSTACK
545 && ss.ss_flags != 0)
546 goto out;
547
548 if (ss.ss_flags == TARGET_SS_DISABLE) {
549 ss.ss_size = 0;
550 ss.ss_sp = 0;
551 } else {
552 ret = -TARGET_ENOMEM;
553 if (ss.ss_size < MINSIGSTKSZ)
554 goto out;
555 }
556
557 target_sigaltstack_used.ss_sp = ss.ss_sp;
558 target_sigaltstack_used.ss_size = ss.ss_size;
559 }
560
561 if (uoss_addr) {
562 ret = -TARGET_EFAULT;
563 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
564 goto out;
565 }
566
567 ret = 0;
568 out:
569 return ret;
570 }
571
572 /* do_sigaction() return host values and errnos */
573 int do_sigaction(int sig, const struct target_sigaction *act,
574 struct target_sigaction *oact)
575 {
576 struct target_sigaction *k;
577 struct sigaction act1;
578 int host_sig;
579 int ret = 0;
580
581 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
582 return -EINVAL;
583 k = &sigact_table[sig - 1];
584 #if defined(DEBUG_SIGNAL)
585 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
586 sig, act, oact);
587 #endif
588 if (oact) {
589 oact->_sa_handler = tswapal(k->_sa_handler);
590 oact->sa_flags = tswapal(k->sa_flags);
591 #if !defined(TARGET_MIPS)
592 oact->sa_restorer = tswapal(k->sa_restorer);
593 #endif
594 oact->sa_mask = k->sa_mask;
595 }
596 if (act) {
597 /* FIXME: This is not threadsafe. */
598 k->_sa_handler = tswapal(act->_sa_handler);
599 k->sa_flags = tswapal(act->sa_flags);
600 #if !defined(TARGET_MIPS)
601 k->sa_restorer = tswapal(act->sa_restorer);
602 #endif
603 k->sa_mask = act->sa_mask;
604
605 /* we update the host linux signal state */
606 host_sig = target_to_host_signal(sig);
607 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
608 sigfillset(&act1.sa_mask);
609 act1.sa_flags = SA_SIGINFO;
610 if (k->sa_flags & TARGET_SA_RESTART)
611 act1.sa_flags |= SA_RESTART;
612 /* NOTE: it is important to update the host kernel signal
613 ignore state to avoid getting unexpected interrupted
614 syscalls */
615 if (k->_sa_handler == TARGET_SIG_IGN) {
616 act1.sa_sigaction = (void *)SIG_IGN;
617 } else if (k->_sa_handler == TARGET_SIG_DFL) {
618 if (fatal_signal (sig))
619 act1.sa_sigaction = host_signal_handler;
620 else
621 act1.sa_sigaction = (void *)SIG_DFL;
622 } else {
623 act1.sa_sigaction = host_signal_handler;
624 }
625 ret = sigaction(host_sig, &act1, NULL);
626 }
627 }
628 return ret;
629 }
630
631 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
632 const target_siginfo_t *info)
633 {
634 tswap_siginfo(tinfo, info);
635 return 0;
636 }
637
638 static inline int current_exec_domain_sig(int sig)
639 {
640 return /* current->exec_domain && current->exec_domain->signal_invmap
641 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
642 }
643
644 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32
645
646 /* from the Linux kernel */
647
648 struct target_fpreg {
649 uint16_t significand[4];
650 uint16_t exponent;
651 };
652
653 struct target_fpxreg {
654 uint16_t significand[4];
655 uint16_t exponent;
656 uint16_t padding[3];
657 };
658
659 struct target_xmmreg {
660 abi_ulong element[4];
661 };
662
663 struct target_fpstate {
664 /* Regular FPU environment */
665 abi_ulong cw;
666 abi_ulong sw;
667 abi_ulong tag;
668 abi_ulong ipoff;
669 abi_ulong cssel;
670 abi_ulong dataoff;
671 abi_ulong datasel;
672 struct target_fpreg _st[8];
673 uint16_t status;
674 uint16_t magic; /* 0xffff = regular FPU data only */
675
676 /* FXSR FPU environment */
677 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
678 abi_ulong mxcsr;
679 abi_ulong reserved;
680 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
681 struct target_xmmreg _xmm[8];
682 abi_ulong padding[56];
683 };
684
685 #define X86_FXSR_MAGIC 0x0000
686
687 struct target_sigcontext {
688 uint16_t gs, __gsh;
689 uint16_t fs, __fsh;
690 uint16_t es, __esh;
691 uint16_t ds, __dsh;
692 abi_ulong edi;
693 abi_ulong esi;
694 abi_ulong ebp;
695 abi_ulong esp;
696 abi_ulong ebx;
697 abi_ulong edx;
698 abi_ulong ecx;
699 abi_ulong eax;
700 abi_ulong trapno;
701 abi_ulong err;
702 abi_ulong eip;
703 uint16_t cs, __csh;
704 abi_ulong eflags;
705 abi_ulong esp_at_signal;
706 uint16_t ss, __ssh;
707 abi_ulong fpstate; /* pointer */
708 abi_ulong oldmask;
709 abi_ulong cr2;
710 };
711
712 struct target_ucontext {
713 abi_ulong tuc_flags;
714 abi_ulong tuc_link;
715 target_stack_t tuc_stack;
716 struct target_sigcontext tuc_mcontext;
717 target_sigset_t tuc_sigmask; /* mask last for extensibility */
718 };
719
720 struct sigframe
721 {
722 abi_ulong pretcode;
723 int sig;
724 struct target_sigcontext sc;
725 struct target_fpstate fpstate;
726 abi_ulong extramask[TARGET_NSIG_WORDS-1];
727 char retcode[8];
728 };
729
730 struct rt_sigframe
731 {
732 abi_ulong pretcode;
733 int sig;
734 abi_ulong pinfo;
735 abi_ulong puc;
736 struct target_siginfo info;
737 struct target_ucontext uc;
738 struct target_fpstate fpstate;
739 char retcode[8];
740 };
741
742 /*
743 * Set up a signal frame.
744 */
745
746 /* XXX: save x87 state */
747 static int
748 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
749 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
750 {
751 int err = 0;
752 uint16_t magic;
753
754 /* already locked in setup_frame() */
755 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
756 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
757 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
758 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
759 err |= __put_user(env->regs[R_EDI], &sc->edi);
760 err |= __put_user(env->regs[R_ESI], &sc->esi);
761 err |= __put_user(env->regs[R_EBP], &sc->ebp);
762 err |= __put_user(env->regs[R_ESP], &sc->esp);
763 err |= __put_user(env->regs[R_EBX], &sc->ebx);
764 err |= __put_user(env->regs[R_EDX], &sc->edx);
765 err |= __put_user(env->regs[R_ECX], &sc->ecx);
766 err |= __put_user(env->regs[R_EAX], &sc->eax);
767 err |= __put_user(env->exception_index, &sc->trapno);
768 err |= __put_user(env->error_code, &sc->err);
769 err |= __put_user(env->eip, &sc->eip);
770 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
771 err |= __put_user(env->eflags, &sc->eflags);
772 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
773 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
774
775 cpu_x86_fsave(env, fpstate_addr, 1);
776 fpstate->status = fpstate->sw;
777 magic = 0xffff;
778 err |= __put_user(magic, &fpstate->magic);
779 err |= __put_user(fpstate_addr, &sc->fpstate);
780
781 /* non-iBCS2 extensions.. */
782 err |= __put_user(mask, &sc->oldmask);
783 err |= __put_user(env->cr[2], &sc->cr2);
784 return err;
785 }
786
787 /*
788 * Determine which stack to use..
789 */
790
791 static inline abi_ulong
792 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
793 {
794 unsigned long esp;
795
796 /* Default to using normal stack */
797 esp = env->regs[R_ESP];
798 /* This is the X/Open sanctioned signal stack switching. */
799 if (ka->sa_flags & TARGET_SA_ONSTACK) {
800 if (sas_ss_flags(esp) == 0)
801 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
802 }
803
804 /* This is the legacy signal stack switching. */
805 else
806 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
807 !(ka->sa_flags & TARGET_SA_RESTORER) &&
808 ka->sa_restorer) {
809 esp = (unsigned long) ka->sa_restorer;
810 }
811 return (esp - frame_size) & -8ul;
812 }
813
814 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
815 static void setup_frame(int sig, struct target_sigaction *ka,
816 target_sigset_t *set, CPUX86State *env)
817 {
818 abi_ulong frame_addr;
819 struct sigframe *frame;
820 int i, err = 0;
821
822 frame_addr = get_sigframe(ka, env, sizeof(*frame));
823
824 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
825 goto give_sigsegv;
826
827 err |= __put_user(current_exec_domain_sig(sig),
828 &frame->sig);
829 if (err)
830 goto give_sigsegv;
831
832 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
833 frame_addr + offsetof(struct sigframe, fpstate));
834 if (err)
835 goto give_sigsegv;
836
837 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
838 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
839 goto give_sigsegv;
840 }
841
842 /* Set up to return from userspace. If provided, use a stub
843 already in userspace. */
844 if (ka->sa_flags & TARGET_SA_RESTORER) {
845 err |= __put_user(ka->sa_restorer, &frame->pretcode);
846 } else {
847 uint16_t val16;
848 abi_ulong retcode_addr;
849 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
850 err |= __put_user(retcode_addr, &frame->pretcode);
851 /* This is popl %eax ; movl $,%eax ; int $0x80 */
852 val16 = 0xb858;
853 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
854 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
855 val16 = 0x80cd;
856 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
857 }
858
859 if (err)
860 goto give_sigsegv;
861
862 /* Set up registers for signal handler */
863 env->regs[R_ESP] = frame_addr;
864 env->eip = ka->_sa_handler;
865
866 cpu_x86_load_seg(env, R_DS, __USER_DS);
867 cpu_x86_load_seg(env, R_ES, __USER_DS);
868 cpu_x86_load_seg(env, R_SS, __USER_DS);
869 cpu_x86_load_seg(env, R_CS, __USER_CS);
870 env->eflags &= ~TF_MASK;
871
872 unlock_user_struct(frame, frame_addr, 1);
873
874 return;
875
876 give_sigsegv:
877 unlock_user_struct(frame, frame_addr, 1);
878 if (sig == TARGET_SIGSEGV)
879 ka->_sa_handler = TARGET_SIG_DFL;
880 force_sig(TARGET_SIGSEGV /* , current */);
881 }
882
883 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
884 static void setup_rt_frame(int sig, struct target_sigaction *ka,
885 target_siginfo_t *info,
886 target_sigset_t *set, CPUX86State *env)
887 {
888 abi_ulong frame_addr, addr;
889 struct rt_sigframe *frame;
890 int i, err = 0;
891
892 frame_addr = get_sigframe(ka, env, sizeof(*frame));
893
894 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
895 goto give_sigsegv;
896
897 err |= __put_user(current_exec_domain_sig(sig),
898 &frame->sig);
899 addr = frame_addr + offsetof(struct rt_sigframe, info);
900 err |= __put_user(addr, &frame->pinfo);
901 addr = frame_addr + offsetof(struct rt_sigframe, uc);
902 err |= __put_user(addr, &frame->puc);
903 err |= copy_siginfo_to_user(&frame->info, info);
904 if (err)
905 goto give_sigsegv;
906
907 /* Create the ucontext. */
908 err |= __put_user(0, &frame->uc.tuc_flags);
909 err |= __put_user(0, &frame->uc.tuc_link);
910 err |= __put_user(target_sigaltstack_used.ss_sp,
911 &frame->uc.tuc_stack.ss_sp);
912 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
913 &frame->uc.tuc_stack.ss_flags);
914 err |= __put_user(target_sigaltstack_used.ss_size,
915 &frame->uc.tuc_stack.ss_size);
916 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
917 env, set->sig[0],
918 frame_addr + offsetof(struct rt_sigframe, fpstate));
919 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
920 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
921 goto give_sigsegv;
922 }
923
924 /* Set up to return from userspace. If provided, use a stub
925 already in userspace. */
926 if (ka->sa_flags & TARGET_SA_RESTORER) {
927 err |= __put_user(ka->sa_restorer, &frame->pretcode);
928 } else {
929 uint16_t val16;
930 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
931 err |= __put_user(addr, &frame->pretcode);
932 /* This is movl $,%eax ; int $0x80 */
933 err |= __put_user(0xb8, (char *)(frame->retcode+0));
934 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
935 val16 = 0x80cd;
936 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
937 }
938
939 if (err)
940 goto give_sigsegv;
941
942 /* Set up registers for signal handler */
943 env->regs[R_ESP] = frame_addr;
944 env->eip = ka->_sa_handler;
945
946 cpu_x86_load_seg(env, R_DS, __USER_DS);
947 cpu_x86_load_seg(env, R_ES, __USER_DS);
948 cpu_x86_load_seg(env, R_SS, __USER_DS);
949 cpu_x86_load_seg(env, R_CS, __USER_CS);
950 env->eflags &= ~TF_MASK;
951
952 unlock_user_struct(frame, frame_addr, 1);
953
954 return;
955
956 give_sigsegv:
957 unlock_user_struct(frame, frame_addr, 1);
958 if (sig == TARGET_SIGSEGV)
959 ka->_sa_handler = TARGET_SIG_DFL;
960 force_sig(TARGET_SIGSEGV /* , current */);
961 }
962
963 static int
964 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
965 {
966 unsigned int err = 0;
967 abi_ulong fpstate_addr;
968 unsigned int tmpflags;
969
970 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
971 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
972 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
973 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
974
975 env->regs[R_EDI] = tswapl(sc->edi);
976 env->regs[R_ESI] = tswapl(sc->esi);
977 env->regs[R_EBP] = tswapl(sc->ebp);
978 env->regs[R_ESP] = tswapl(sc->esp);
979 env->regs[R_EBX] = tswapl(sc->ebx);
980 env->regs[R_EDX] = tswapl(sc->edx);
981 env->regs[R_ECX] = tswapl(sc->ecx);
982 env->eip = tswapl(sc->eip);
983
984 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
985 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
986
987 tmpflags = tswapl(sc->eflags);
988 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
989 // regs->orig_eax = -1; /* disable syscall checks */
990
991 fpstate_addr = tswapl(sc->fpstate);
992 if (fpstate_addr != 0) {
993 if (!access_ok(VERIFY_READ, fpstate_addr,
994 sizeof(struct target_fpstate)))
995 goto badframe;
996 cpu_x86_frstor(env, fpstate_addr, 1);
997 }
998
999 *peax = tswapl(sc->eax);
1000 return err;
1001 badframe:
1002 return 1;
1003 }
1004
1005 long do_sigreturn(CPUX86State *env)
1006 {
1007 struct sigframe *frame;
1008 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1009 target_sigset_t target_set;
1010 sigset_t set;
1011 int eax, i;
1012
1013 #if defined(DEBUG_SIGNAL)
1014 fprintf(stderr, "do_sigreturn\n");
1015 #endif
1016 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1017 goto badframe;
1018 /* set blocked signals */
1019 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1020 goto badframe;
1021 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1022 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1023 goto badframe;
1024 }
1025
1026 target_to_host_sigset_internal(&set, &target_set);
1027 sigprocmask(SIG_SETMASK, &set, NULL);
1028
1029 /* restore registers */
1030 if (restore_sigcontext(env, &frame->sc, &eax))
1031 goto badframe;
1032 unlock_user_struct(frame, frame_addr, 0);
1033 return eax;
1034
1035 badframe:
1036 unlock_user_struct(frame, frame_addr, 0);
1037 force_sig(TARGET_SIGSEGV);
1038 return 0;
1039 }
1040
1041 long do_rt_sigreturn(CPUX86State *env)
1042 {
1043 abi_ulong frame_addr;
1044 struct rt_sigframe *frame;
1045 sigset_t set;
1046 int eax;
1047
1048 frame_addr = env->regs[R_ESP] - 4;
1049 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1050 goto badframe;
1051 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1052 sigprocmask(SIG_SETMASK, &set, NULL);
1053
1054 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1055 goto badframe;
1056
1057 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1058 get_sp_from_cpustate(env)) == -EFAULT)
1059 goto badframe;
1060
1061 unlock_user_struct(frame, frame_addr, 0);
1062 return eax;
1063
1064 badframe:
1065 unlock_user_struct(frame, frame_addr, 0);
1066 force_sig(TARGET_SIGSEGV);
1067 return 0;
1068 }
1069
1070 #elif defined(TARGET_ARM)
1071
1072 struct target_sigcontext {
1073 abi_ulong trap_no;
1074 abi_ulong error_code;
1075 abi_ulong oldmask;
1076 abi_ulong arm_r0;
1077 abi_ulong arm_r1;
1078 abi_ulong arm_r2;
1079 abi_ulong arm_r3;
1080 abi_ulong arm_r4;
1081 abi_ulong arm_r5;
1082 abi_ulong arm_r6;
1083 abi_ulong arm_r7;
1084 abi_ulong arm_r8;
1085 abi_ulong arm_r9;
1086 abi_ulong arm_r10;
1087 abi_ulong arm_fp;
1088 abi_ulong arm_ip;
1089 abi_ulong arm_sp;
1090 abi_ulong arm_lr;
1091 abi_ulong arm_pc;
1092 abi_ulong arm_cpsr;
1093 abi_ulong fault_address;
1094 };
1095
1096 struct target_ucontext_v1 {
1097 abi_ulong tuc_flags;
1098 abi_ulong tuc_link;
1099 target_stack_t tuc_stack;
1100 struct target_sigcontext tuc_mcontext;
1101 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1102 };
1103
1104 struct target_ucontext_v2 {
1105 abi_ulong tuc_flags;
1106 abi_ulong tuc_link;
1107 target_stack_t tuc_stack;
1108 struct target_sigcontext tuc_mcontext;
1109 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1110 char __unused[128 - sizeof(target_sigset_t)];
1111 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1112 };
1113
1114 struct target_user_vfp {
1115 uint64_t fpregs[32];
1116 abi_ulong fpscr;
1117 };
1118
1119 struct target_user_vfp_exc {
1120 abi_ulong fpexc;
1121 abi_ulong fpinst;
1122 abi_ulong fpinst2;
1123 };
1124
1125 struct target_vfp_sigframe {
1126 abi_ulong magic;
1127 abi_ulong size;
1128 struct target_user_vfp ufp;
1129 struct target_user_vfp_exc ufp_exc;
1130 } __attribute__((__aligned__(8)));
1131
1132 struct target_iwmmxt_sigframe {
1133 abi_ulong magic;
1134 abi_ulong size;
1135 uint64_t regs[16];
1136 /* Note that not all the coprocessor control registers are stored here */
1137 uint32_t wcssf;
1138 uint32_t wcasf;
1139 uint32_t wcgr0;
1140 uint32_t wcgr1;
1141 uint32_t wcgr2;
1142 uint32_t wcgr3;
1143 } __attribute__((__aligned__(8)));
1144
1145 #define TARGET_VFP_MAGIC 0x56465001
1146 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1147
1148 struct sigframe_v1
1149 {
1150 struct target_sigcontext sc;
1151 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1152 abi_ulong retcode;
1153 };
1154
1155 struct sigframe_v2
1156 {
1157 struct target_ucontext_v2 uc;
1158 abi_ulong retcode;
1159 };
1160
1161 struct rt_sigframe_v1
1162 {
1163 abi_ulong pinfo;
1164 abi_ulong puc;
1165 struct target_siginfo info;
1166 struct target_ucontext_v1 uc;
1167 abi_ulong retcode;
1168 };
1169
1170 struct rt_sigframe_v2
1171 {
1172 struct target_siginfo info;
1173 struct target_ucontext_v2 uc;
1174 abi_ulong retcode;
1175 };
1176
1177 #define TARGET_CONFIG_CPU_32 1
1178
1179 /*
1180 * For ARM syscalls, we encode the syscall number into the instruction.
1181 */
1182 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1183 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1184
1185 /*
1186 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1187 * need two 16-bit instructions.
1188 */
1189 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1190 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1191
1192 static const abi_ulong retcodes[4] = {
1193 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1194 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1195 };
1196
1197
1198 #define __get_user_error(x,p,e) __get_user(x, p)
1199
1200 static inline int valid_user_regs(CPUState *regs)
1201 {
1202 return 1;
1203 }
1204
1205 static void
1206 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1207 CPUState *env, abi_ulong mask)
1208 {
1209 __put_user(env->regs[0], &sc->arm_r0);
1210 __put_user(env->regs[1], &sc->arm_r1);
1211 __put_user(env->regs[2], &sc->arm_r2);
1212 __put_user(env->regs[3], &sc->arm_r3);
1213 __put_user(env->regs[4], &sc->arm_r4);
1214 __put_user(env->regs[5], &sc->arm_r5);
1215 __put_user(env->regs[6], &sc->arm_r6);
1216 __put_user(env->regs[7], &sc->arm_r7);
1217 __put_user(env->regs[8], &sc->arm_r8);
1218 __put_user(env->regs[9], &sc->arm_r9);
1219 __put_user(env->regs[10], &sc->arm_r10);
1220 __put_user(env->regs[11], &sc->arm_fp);
1221 __put_user(env->regs[12], &sc->arm_ip);
1222 __put_user(env->regs[13], &sc->arm_sp);
1223 __put_user(env->regs[14], &sc->arm_lr);
1224 __put_user(env->regs[15], &sc->arm_pc);
1225 #ifdef TARGET_CONFIG_CPU_32
1226 __put_user(cpsr_read(env), &sc->arm_cpsr);
1227 #endif
1228
1229 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1230 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1231 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1232 __put_user(mask, &sc->oldmask);
1233 }
1234
1235 static inline abi_ulong
1236 get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1237 {
1238 unsigned long sp = regs->regs[13];
1239
1240 /*
1241 * This is the X/Open sanctioned signal stack switching.
1242 */
1243 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1244 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1245 /*
1246 * ATPCS B01 mandates 8-byte alignment
1247 */
1248 return (sp - framesize) & ~7;
1249 }
1250
1251 static int
1252 setup_return(CPUState *env, struct target_sigaction *ka,
1253 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1254 {
1255 abi_ulong handler = ka->_sa_handler;
1256 abi_ulong retcode;
1257 int thumb = handler & 1;
1258 uint32_t cpsr = cpsr_read(env);
1259
1260 cpsr &= ~CPSR_IT;
1261 if (thumb) {
1262 cpsr |= CPSR_T;
1263 } else {
1264 cpsr &= ~CPSR_T;
1265 }
1266
1267 if (ka->sa_flags & TARGET_SA_RESTORER) {
1268 retcode = ka->sa_restorer;
1269 } else {
1270 unsigned int idx = thumb;
1271
1272 if (ka->sa_flags & TARGET_SA_SIGINFO)
1273 idx += 2;
1274
1275 if (__put_user(retcodes[idx], rc))
1276 return 1;
1277
1278 retcode = rc_addr + thumb;
1279 }
1280
1281 env->regs[0] = usig;
1282 env->regs[13] = frame_addr;
1283 env->regs[14] = retcode;
1284 env->regs[15] = handler & (thumb ? ~1 : ~3);
1285 cpsr_write(env, cpsr, 0xffffffff);
1286
1287 return 0;
1288 }
1289
1290 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUState *env)
1291 {
1292 int i;
1293 struct target_vfp_sigframe *vfpframe;
1294 vfpframe = (struct target_vfp_sigframe *)regspace;
1295 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1296 __put_user(sizeof(*vfpframe), &vfpframe->size);
1297 for (i = 0; i < 32; i++) {
1298 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1299 }
1300 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1301 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1302 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1303 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1304 return (abi_ulong*)(vfpframe+1);
1305 }
1306
1307 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace, CPUState *env)
1308 {
1309 int i;
1310 struct target_iwmmxt_sigframe *iwmmxtframe;
1311 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1312 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1313 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1314 for (i = 0; i < 16; i++) {
1315 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1316 }
1317 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1318 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1319 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1320 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1321 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1322 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1323 return (abi_ulong*)(iwmmxtframe+1);
1324 }
1325
1326 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1327 target_sigset_t *set, CPUState *env)
1328 {
1329 struct target_sigaltstack stack;
1330 int i;
1331 abi_ulong *regspace;
1332
1333 /* Clear all the bits of the ucontext we don't use. */
1334 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1335
1336 memset(&stack, 0, sizeof(stack));
1337 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1338 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1339 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1340 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1341
1342 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1343 /* Save coprocessor signal frame. */
1344 regspace = uc->tuc_regspace;
1345 if (arm_feature(env, ARM_FEATURE_VFP)) {
1346 regspace = setup_sigframe_v2_vfp(regspace, env);
1347 }
1348 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1349 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1350 }
1351
1352 /* Write terminating magic word */
1353 __put_user(0, regspace);
1354
1355 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1356 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1357 }
1358 }
1359
1360 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1361 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1362 target_sigset_t *set, CPUState *regs)
1363 {
1364 struct sigframe_v1 *frame;
1365 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1366 int i;
1367
1368 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1369 return;
1370
1371 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1372
1373 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1374 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1375 goto end;
1376 }
1377
1378 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1379 frame_addr + offsetof(struct sigframe_v1, retcode));
1380
1381 end:
1382 unlock_user_struct(frame, frame_addr, 1);
1383 }
1384
1385 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1386 target_sigset_t *set, CPUState *regs)
1387 {
1388 struct sigframe_v2 *frame;
1389 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1390
1391 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1392 return;
1393
1394 setup_sigframe_v2(&frame->uc, set, regs);
1395
1396 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1397 frame_addr + offsetof(struct sigframe_v2, retcode));
1398
1399 unlock_user_struct(frame, frame_addr, 1);
1400 }
1401
1402 static void setup_frame(int usig, struct target_sigaction *ka,
1403 target_sigset_t *set, CPUState *regs)
1404 {
1405 if (get_osversion() >= 0x020612) {
1406 setup_frame_v2(usig, ka, set, regs);
1407 } else {
1408 setup_frame_v1(usig, ka, set, regs);
1409 }
1410 }
1411
1412 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1413 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1414 target_siginfo_t *info,
1415 target_sigset_t *set, CPUState *env)
1416 {
1417 struct rt_sigframe_v1 *frame;
1418 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1419 struct target_sigaltstack stack;
1420 int i;
1421 abi_ulong info_addr, uc_addr;
1422
1423 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1424 return /* 1 */;
1425
1426 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1427 __put_user(info_addr, &frame->pinfo);
1428 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1429 __put_user(uc_addr, &frame->puc);
1430 copy_siginfo_to_user(&frame->info, info);
1431
1432 /* Clear all the bits of the ucontext we don't use. */
1433 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1434
1435 memset(&stack, 0, sizeof(stack));
1436 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1437 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1438 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1439 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1440
1441 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1442 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1443 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1444 goto end;
1445 }
1446
1447 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1448 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1449
1450 env->regs[1] = info_addr;
1451 env->regs[2] = uc_addr;
1452
1453 end:
1454 unlock_user_struct(frame, frame_addr, 1);
1455 }
1456
1457 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1458 target_siginfo_t *info,
1459 target_sigset_t *set, CPUState *env)
1460 {
1461 struct rt_sigframe_v2 *frame;
1462 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1463 abi_ulong info_addr, uc_addr;
1464
1465 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1466 return /* 1 */;
1467
1468 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1469 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1470 copy_siginfo_to_user(&frame->info, info);
1471
1472 setup_sigframe_v2(&frame->uc, set, env);
1473
1474 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1475 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1476
1477 env->regs[1] = info_addr;
1478 env->regs[2] = uc_addr;
1479
1480 unlock_user_struct(frame, frame_addr, 1);
1481 }
1482
1483 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1484 target_siginfo_t *info,
1485 target_sigset_t *set, CPUState *env)
1486 {
1487 if (get_osversion() >= 0x020612) {
1488 setup_rt_frame_v2(usig, ka, info, set, env);
1489 } else {
1490 setup_rt_frame_v1(usig, ka, info, set, env);
1491 }
1492 }
1493
1494 static int
1495 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1496 {
1497 int err = 0;
1498 uint32_t cpsr;
1499
1500 __get_user_error(env->regs[0], &sc->arm_r0, err);
1501 __get_user_error(env->regs[1], &sc->arm_r1, err);
1502 __get_user_error(env->regs[2], &sc->arm_r2, err);
1503 __get_user_error(env->regs[3], &sc->arm_r3, err);
1504 __get_user_error(env->regs[4], &sc->arm_r4, err);
1505 __get_user_error(env->regs[5], &sc->arm_r5, err);
1506 __get_user_error(env->regs[6], &sc->arm_r6, err);
1507 __get_user_error(env->regs[7], &sc->arm_r7, err);
1508 __get_user_error(env->regs[8], &sc->arm_r8, err);
1509 __get_user_error(env->regs[9], &sc->arm_r9, err);
1510 __get_user_error(env->regs[10], &sc->arm_r10, err);
1511 __get_user_error(env->regs[11], &sc->arm_fp, err);
1512 __get_user_error(env->regs[12], &sc->arm_ip, err);
1513 __get_user_error(env->regs[13], &sc->arm_sp, err);
1514 __get_user_error(env->regs[14], &sc->arm_lr, err);
1515 __get_user_error(env->regs[15], &sc->arm_pc, err);
1516 #ifdef TARGET_CONFIG_CPU_32
1517 __get_user_error(cpsr, &sc->arm_cpsr, err);
1518 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1519 #endif
1520
1521 err |= !valid_user_regs(env);
1522
1523 return err;
1524 }
1525
1526 static long do_sigreturn_v1(CPUState *env)
1527 {
1528 abi_ulong frame_addr;
1529 struct sigframe_v1 *frame;
1530 target_sigset_t set;
1531 sigset_t host_set;
1532 int i;
1533
1534 /*
1535 * Since we stacked the signal on a 64-bit boundary,
1536 * then 'sp' should be word aligned here. If it's
1537 * not, then the user is trying to mess with us.
1538 */
1539 if (env->regs[13] & 7)
1540 goto badframe;
1541
1542 frame_addr = env->regs[13];
1543 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1544 goto badframe;
1545
1546 if (__get_user(set.sig[0], &frame->sc.oldmask))
1547 goto badframe;
1548 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1549 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1550 goto badframe;
1551 }
1552
1553 target_to_host_sigset_internal(&host_set, &set);
1554 sigprocmask(SIG_SETMASK, &host_set, NULL);
1555
1556 if (restore_sigcontext(env, &frame->sc))
1557 goto badframe;
1558
1559 #if 0
1560 /* Send SIGTRAP if we're single-stepping */
1561 if (ptrace_cancel_bpt(current))
1562 send_sig(SIGTRAP, current, 1);
1563 #endif
1564 unlock_user_struct(frame, frame_addr, 0);
1565 return env->regs[0];
1566
1567 badframe:
1568 unlock_user_struct(frame, frame_addr, 0);
1569 force_sig(TARGET_SIGSEGV /* , current */);
1570 return 0;
1571 }
1572
1573 static abi_ulong *restore_sigframe_v2_vfp(CPUState *env, abi_ulong *regspace)
1574 {
1575 int i;
1576 abi_ulong magic, sz;
1577 uint32_t fpscr, fpexc;
1578 struct target_vfp_sigframe *vfpframe;
1579 vfpframe = (struct target_vfp_sigframe *)regspace;
1580
1581 __get_user(magic, &vfpframe->magic);
1582 __get_user(sz, &vfpframe->size);
1583 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1584 return 0;
1585 }
1586 for (i = 0; i < 32; i++) {
1587 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1588 }
1589 __get_user(fpscr, &vfpframe->ufp.fpscr);
1590 vfp_set_fpscr(env, fpscr);
1591 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1592 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1593 * and the exception flag is cleared
1594 */
1595 fpexc |= (1 << 30);
1596 fpexc &= ~((1 << 31) | (1 << 28));
1597 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1598 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1599 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1600 return (abi_ulong*)(vfpframe + 1);
1601 }
1602
1603 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUState *env, abi_ulong *regspace)
1604 {
1605 int i;
1606 abi_ulong magic, sz;
1607 struct target_iwmmxt_sigframe *iwmmxtframe;
1608 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1609
1610 __get_user(magic, &iwmmxtframe->magic);
1611 __get_user(sz, &iwmmxtframe->size);
1612 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1613 return 0;
1614 }
1615 for (i = 0; i < 16; i++) {
1616 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1617 }
1618 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1619 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1620 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1621 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1622 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1623 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1624 return (abi_ulong*)(iwmmxtframe + 1);
1625 }
1626
1627 static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1628 struct target_ucontext_v2 *uc)
1629 {
1630 sigset_t host_set;
1631 abi_ulong *regspace;
1632
1633 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1634 sigprocmask(SIG_SETMASK, &host_set, NULL);
1635
1636 if (restore_sigcontext(env, &uc->tuc_mcontext))
1637 return 1;
1638
1639 /* Restore coprocessor signal frame */
1640 regspace = uc->tuc_regspace;
1641 if (arm_feature(env, ARM_FEATURE_VFP)) {
1642 regspace = restore_sigframe_v2_vfp(env, regspace);
1643 if (!regspace) {
1644 return 1;
1645 }
1646 }
1647 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1648 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1649 if (!regspace) {
1650 return 1;
1651 }
1652 }
1653
1654 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1655 return 1;
1656
1657 #if 0
1658 /* Send SIGTRAP if we're single-stepping */
1659 if (ptrace_cancel_bpt(current))
1660 send_sig(SIGTRAP, current, 1);
1661 #endif
1662
1663 return 0;
1664 }
1665
1666 static long do_sigreturn_v2(CPUState *env)
1667 {
1668 abi_ulong frame_addr;
1669 struct sigframe_v2 *frame;
1670
1671 /*
1672 * Since we stacked the signal on a 64-bit boundary,
1673 * then 'sp' should be word aligned here. If it's
1674 * not, then the user is trying to mess with us.
1675 */
1676 if (env->regs[13] & 7)
1677 goto badframe;
1678
1679 frame_addr = env->regs[13];
1680 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1681 goto badframe;
1682
1683 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1684 goto badframe;
1685
1686 unlock_user_struct(frame, frame_addr, 0);
1687 return env->regs[0];
1688
1689 badframe:
1690 unlock_user_struct(frame, frame_addr, 0);
1691 force_sig(TARGET_SIGSEGV /* , current */);
1692 return 0;
1693 }
1694
1695 long do_sigreturn(CPUState *env)
1696 {
1697 if (get_osversion() >= 0x020612) {
1698 return do_sigreturn_v2(env);
1699 } else {
1700 return do_sigreturn_v1(env);
1701 }
1702 }
1703
1704 static long do_rt_sigreturn_v1(CPUState *env)
1705 {
1706 abi_ulong frame_addr;
1707 struct rt_sigframe_v1 *frame;
1708 sigset_t host_set;
1709
1710 /*
1711 * Since we stacked the signal on a 64-bit boundary,
1712 * then 'sp' should be word aligned here. If it's
1713 * not, then the user is trying to mess with us.
1714 */
1715 if (env->regs[13] & 7)
1716 goto badframe;
1717
1718 frame_addr = env->regs[13];
1719 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1720 goto badframe;
1721
1722 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1723 sigprocmask(SIG_SETMASK, &host_set, NULL);
1724
1725 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1726 goto badframe;
1727
1728 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1729 goto badframe;
1730
1731 #if 0
1732 /* Send SIGTRAP if we're single-stepping */
1733 if (ptrace_cancel_bpt(current))
1734 send_sig(SIGTRAP, current, 1);
1735 #endif
1736 unlock_user_struct(frame, frame_addr, 0);
1737 return env->regs[0];
1738
1739 badframe:
1740 unlock_user_struct(frame, frame_addr, 0);
1741 force_sig(TARGET_SIGSEGV /* , current */);
1742 return 0;
1743 }
1744
1745 static long do_rt_sigreturn_v2(CPUState *env)
1746 {
1747 abi_ulong frame_addr;
1748 struct rt_sigframe_v2 *frame;
1749
1750 /*
1751 * Since we stacked the signal on a 64-bit boundary,
1752 * then 'sp' should be word aligned here. If it's
1753 * not, then the user is trying to mess with us.
1754 */
1755 if (env->regs[13] & 7)
1756 goto badframe;
1757
1758 frame_addr = env->regs[13];
1759 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1760 goto badframe;
1761
1762 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1763 goto badframe;
1764
1765 unlock_user_struct(frame, frame_addr, 0);
1766 return env->regs[0];
1767
1768 badframe:
1769 unlock_user_struct(frame, frame_addr, 0);
1770 force_sig(TARGET_SIGSEGV /* , current */);
1771 return 0;
1772 }
1773
1774 long do_rt_sigreturn(CPUState *env)
1775 {
1776 if (get_osversion() >= 0x020612) {
1777 return do_rt_sigreturn_v2(env);
1778 } else {
1779 return do_rt_sigreturn_v1(env);
1780 }
1781 }
1782
1783 #elif defined(TARGET_SPARC)
1784
1785 #define __SUNOS_MAXWIN 31
1786
1787 /* This is what SunOS does, so shall I. */
1788 struct target_sigcontext {
1789 abi_ulong sigc_onstack; /* state to restore */
1790
1791 abi_ulong sigc_mask; /* sigmask to restore */
1792 abi_ulong sigc_sp; /* stack pointer */
1793 abi_ulong sigc_pc; /* program counter */
1794 abi_ulong sigc_npc; /* next program counter */
1795 abi_ulong sigc_psr; /* for condition codes etc */
1796 abi_ulong sigc_g1; /* User uses these two registers */
1797 abi_ulong sigc_o0; /* within the trampoline code. */
1798
1799 /* Now comes information regarding the users window set
1800 * at the time of the signal.
1801 */
1802 abi_ulong sigc_oswins; /* outstanding windows */
1803
1804 /* stack ptrs for each regwin buf */
1805 char *sigc_spbuf[__SUNOS_MAXWIN];
1806
1807 /* Windows to restore after signal */
1808 struct {
1809 abi_ulong locals[8];
1810 abi_ulong ins[8];
1811 } sigc_wbuf[__SUNOS_MAXWIN];
1812 };
1813 /* A Sparc stack frame */
1814 struct sparc_stackf {
1815 abi_ulong locals[8];
1816 abi_ulong ins[8];
1817 /* It's simpler to treat fp and callers_pc as elements of ins[]
1818 * since we never need to access them ourselves.
1819 */
1820 char *structptr;
1821 abi_ulong xargs[6];
1822 abi_ulong xxargs[1];
1823 };
1824
1825 typedef struct {
1826 struct {
1827 abi_ulong psr;
1828 abi_ulong pc;
1829 abi_ulong npc;
1830 abi_ulong y;
1831 abi_ulong u_regs[16]; /* globals and ins */
1832 } si_regs;
1833 int si_mask;
1834 } __siginfo_t;
1835
1836 typedef struct {
1837 unsigned long si_float_regs [32];
1838 unsigned long si_fsr;
1839 unsigned long si_fpqdepth;
1840 struct {
1841 unsigned long *insn_addr;
1842 unsigned long insn;
1843 } si_fpqueue [16];
1844 } qemu_siginfo_fpu_t;
1845
1846
1847 struct target_signal_frame {
1848 struct sparc_stackf ss;
1849 __siginfo_t info;
1850 abi_ulong fpu_save;
1851 abi_ulong insns[2] __attribute__ ((aligned (8)));
1852 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1853 abi_ulong extra_size; /* Should be 0 */
1854 qemu_siginfo_fpu_t fpu_state;
1855 };
1856 struct target_rt_signal_frame {
1857 struct sparc_stackf ss;
1858 siginfo_t info;
1859 abi_ulong regs[20];
1860 sigset_t mask;
1861 abi_ulong fpu_save;
1862 unsigned int insns[2];
1863 stack_t stack;
1864 unsigned int extra_size; /* Should be 0 */
1865 qemu_siginfo_fpu_t fpu_state;
1866 };
1867
1868 #define UREG_O0 16
1869 #define UREG_O6 22
1870 #define UREG_I0 0
1871 #define UREG_I1 1
1872 #define UREG_I2 2
1873 #define UREG_I3 3
1874 #define UREG_I4 4
1875 #define UREG_I5 5
1876 #define UREG_I6 6
1877 #define UREG_I7 7
1878 #define UREG_L0 8
1879 #define UREG_FP UREG_I6
1880 #define UREG_SP UREG_O6
1881
1882 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
1883 CPUState *env, unsigned long framesize)
1884 {
1885 abi_ulong sp;
1886
1887 sp = env->regwptr[UREG_FP];
1888
1889 /* This is the X/Open sanctioned signal stack switching. */
1890 if (sa->sa_flags & TARGET_SA_ONSTACK) {
1891 if (!on_sig_stack(sp)
1892 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1893 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1894 }
1895 return sp - framesize;
1896 }
1897
1898 static int
1899 setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1900 {
1901 int err = 0, i;
1902
1903 err |= __put_user(env->psr, &si->si_regs.psr);
1904 err |= __put_user(env->pc, &si->si_regs.pc);
1905 err |= __put_user(env->npc, &si->si_regs.npc);
1906 err |= __put_user(env->y, &si->si_regs.y);
1907 for (i=0; i < 8; i++) {
1908 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1909 }
1910 for (i=0; i < 8; i++) {
1911 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1912 }
1913 err |= __put_user(mask, &si->si_mask);
1914 return err;
1915 }
1916
1917 #if 0
1918 static int
1919 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1920 CPUState *env, unsigned long mask)
1921 {
1922 int err = 0;
1923
1924 err |= __put_user(mask, &sc->sigc_mask);
1925 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1926 err |= __put_user(env->pc, &sc->sigc_pc);
1927 err |= __put_user(env->npc, &sc->sigc_npc);
1928 err |= __put_user(env->psr, &sc->sigc_psr);
1929 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1930 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1931
1932 return err;
1933 }
1934 #endif
1935 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1936
1937 static void setup_frame(int sig, struct target_sigaction *ka,
1938 target_sigset_t *set, CPUState *env)
1939 {
1940 abi_ulong sf_addr;
1941 struct target_signal_frame *sf;
1942 int sigframe_size, err, i;
1943
1944 /* 1. Make sure everything is clean */
1945 //synchronize_user_stack();
1946
1947 sigframe_size = NF_ALIGNEDSZ;
1948 sf_addr = get_sigframe(ka, env, sigframe_size);
1949
1950 sf = lock_user(VERIFY_WRITE, sf_addr,
1951 sizeof(struct target_signal_frame), 0);
1952 if (!sf)
1953 goto sigsegv;
1954
1955 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1956 #if 0
1957 if (invalid_frame_pointer(sf, sigframe_size))
1958 goto sigill_and_return;
1959 #endif
1960 /* 2. Save the current process state */
1961 err = setup___siginfo(&sf->info, env, set->sig[0]);
1962 err |= __put_user(0, &sf->extra_size);
1963
1964 //err |= save_fpu_state(regs, &sf->fpu_state);
1965 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1966
1967 err |= __put_user(set->sig[0], &sf->info.si_mask);
1968 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1969 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1970 }
1971
1972 for (i = 0; i < 8; i++) {
1973 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1974 }
1975 for (i = 0; i < 8; i++) {
1976 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1977 }
1978 if (err)
1979 goto sigsegv;
1980
1981 /* 3. signal handler back-trampoline and parameters */
1982 env->regwptr[UREG_FP] = sf_addr;
1983 env->regwptr[UREG_I0] = sig;
1984 env->regwptr[UREG_I1] = sf_addr +
1985 offsetof(struct target_signal_frame, info);
1986 env->regwptr[UREG_I2] = sf_addr +
1987 offsetof(struct target_signal_frame, info);
1988
1989 /* 4. signal handler */
1990 env->pc = ka->_sa_handler;
1991 env->npc = (env->pc + 4);
1992 /* 5. return to kernel instructions */
1993 if (ka->sa_restorer)
1994 env->regwptr[UREG_I7] = ka->sa_restorer;
1995 else {
1996 uint32_t val32;
1997
1998 env->regwptr[UREG_I7] = sf_addr +
1999 offsetof(struct target_signal_frame, insns) - 2 * 4;
2000
2001 /* mov __NR_sigreturn, %g1 */
2002 val32 = 0x821020d8;
2003 err |= __put_user(val32, &sf->insns[0]);
2004
2005 /* t 0x10 */
2006 val32 = 0x91d02010;
2007 err |= __put_user(val32, &sf->insns[1]);
2008 if (err)
2009 goto sigsegv;
2010
2011 /* Flush instruction space. */
2012 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2013 // tb_flush(env);
2014 }
2015 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2016 return;
2017 #if 0
2018 sigill_and_return:
2019 force_sig(TARGET_SIGILL);
2020 #endif
2021 sigsegv:
2022 //fprintf(stderr, "force_sig\n");
2023 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2024 force_sig(TARGET_SIGSEGV);
2025 }
2026 static inline int
2027 restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
2028 {
2029 int err;
2030 #if 0
2031 #ifdef CONFIG_SMP
2032 if (current->flags & PF_USEDFPU)
2033 regs->psr &= ~PSR_EF;
2034 #else
2035 if (current == last_task_used_math) {
2036 last_task_used_math = 0;
2037 regs->psr &= ~PSR_EF;
2038 }
2039 #endif
2040 current->used_math = 1;
2041 current->flags &= ~PF_USEDFPU;
2042 #endif
2043 #if 0
2044 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2045 return -EFAULT;
2046 #endif
2047
2048 #if 0
2049 /* XXX: incorrect */
2050 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
2051 (sizeof(unsigned long) * 32));
2052 #endif
2053 err |= __get_user(env->fsr, &fpu->si_fsr);
2054 #if 0
2055 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2056 if (current->thread.fpqdepth != 0)
2057 err |= __copy_from_user(&current->thread.fpqueue[0],
2058 &fpu->si_fpqueue[0],
2059 ((sizeof(unsigned long) +
2060 (sizeof(unsigned long *)))*16));
2061 #endif
2062 return err;
2063 }
2064
2065
2066 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2067 target_siginfo_t *info,
2068 target_sigset_t *set, CPUState *env)
2069 {
2070 fprintf(stderr, "setup_rt_frame: not implemented\n");
2071 }
2072
2073 long do_sigreturn(CPUState *env)
2074 {
2075 abi_ulong sf_addr;
2076 struct target_signal_frame *sf;
2077 uint32_t up_psr, pc, npc;
2078 target_sigset_t set;
2079 sigset_t host_set;
2080 int err, i;
2081
2082 sf_addr = env->regwptr[UREG_FP];
2083 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2084 goto segv_and_exit;
2085 #if 0
2086 fprintf(stderr, "sigreturn\n");
2087 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2088 #endif
2089 //cpu_dump_state(env, stderr, fprintf, 0);
2090
2091 /* 1. Make sure we are not getting garbage from the user */
2092
2093 if (sf_addr & 3)
2094 goto segv_and_exit;
2095
2096 err = __get_user(pc, &sf->info.si_regs.pc);
2097 err |= __get_user(npc, &sf->info.si_regs.npc);
2098
2099 if ((pc | npc) & 3)
2100 goto segv_and_exit;
2101
2102 /* 2. Restore the state */
2103 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2104
2105 /* User can only change condition codes and FPU enabling in %psr. */
2106 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2107 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2108
2109 env->pc = pc;
2110 env->npc = npc;
2111 err |= __get_user(env->y, &sf->info.si_regs.y);
2112 for (i=0; i < 8; i++) {
2113 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2114 }
2115 for (i=0; i < 8; i++) {
2116 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2117 }
2118
2119 /* FIXME: implement FPU save/restore:
2120 * __get_user(fpu_save, &sf->fpu_save);
2121 * if (fpu_save)
2122 * err |= restore_fpu_state(env, fpu_save);
2123 */
2124
2125 /* This is pretty much atomic, no amount locking would prevent
2126 * the races which exist anyways.
2127 */
2128 err |= __get_user(set.sig[0], &sf->info.si_mask);
2129 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2130 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2131 }
2132
2133 target_to_host_sigset_internal(&host_set, &set);
2134 sigprocmask(SIG_SETMASK, &host_set, NULL);
2135
2136 if (err)
2137 goto segv_and_exit;
2138 unlock_user_struct(sf, sf_addr, 0);
2139 return env->regwptr[0];
2140
2141 segv_and_exit:
2142 unlock_user_struct(sf, sf_addr, 0);
2143 force_sig(TARGET_SIGSEGV);
2144 }
2145
2146 long do_rt_sigreturn(CPUState *env)
2147 {
2148 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2149 return -TARGET_ENOSYS;
2150 }
2151
2152 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2153 #define MC_TSTATE 0
2154 #define MC_PC 1
2155 #define MC_NPC 2
2156 #define MC_Y 3
2157 #define MC_G1 4
2158 #define MC_G2 5
2159 #define MC_G3 6
2160 #define MC_G4 7
2161 #define MC_G5 8
2162 #define MC_G6 9
2163 #define MC_G7 10
2164 #define MC_O0 11
2165 #define MC_O1 12
2166 #define MC_O2 13
2167 #define MC_O3 14
2168 #define MC_O4 15
2169 #define MC_O5 16
2170 #define MC_O6 17
2171 #define MC_O7 18
2172 #define MC_NGREG 19
2173
2174 typedef abi_ulong target_mc_greg_t;
2175 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2176
2177 struct target_mc_fq {
2178 abi_ulong *mcfq_addr;
2179 uint32_t mcfq_insn;
2180 };
2181
2182 struct target_mc_fpu {
2183 union {
2184 uint32_t sregs[32];
2185 uint64_t dregs[32];
2186 //uint128_t qregs[16];
2187 } mcfpu_fregs;
2188 abi_ulong mcfpu_fsr;
2189 abi_ulong mcfpu_fprs;
2190 abi_ulong mcfpu_gsr;
2191 struct target_mc_fq *mcfpu_fq;
2192 unsigned char mcfpu_qcnt;
2193 unsigned char mcfpu_qentsz;
2194 unsigned char mcfpu_enab;
2195 };
2196 typedef struct target_mc_fpu target_mc_fpu_t;
2197
2198 typedef struct {
2199 target_mc_gregset_t mc_gregs;
2200 target_mc_greg_t mc_fp;
2201 target_mc_greg_t mc_i7;
2202 target_mc_fpu_t mc_fpregs;
2203 } target_mcontext_t;
2204
2205 struct target_ucontext {
2206 struct target_ucontext *tuc_link;
2207 abi_ulong tuc_flags;
2208 target_sigset_t tuc_sigmask;
2209 target_mcontext_t tuc_mcontext;
2210 };
2211
2212 /* A V9 register window */
2213 struct target_reg_window {
2214 abi_ulong locals[8];
2215 abi_ulong ins[8];
2216 };
2217
2218 #define TARGET_STACK_BIAS 2047
2219
2220 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2221 void sparc64_set_context(CPUSPARCState *env)
2222 {
2223 abi_ulong ucp_addr;
2224 struct target_ucontext *ucp;
2225 target_mc_gregset_t *grp;
2226 abi_ulong pc, npc, tstate;
2227 abi_ulong fp, i7, w_addr;
2228 int err;
2229 unsigned int i;
2230
2231 ucp_addr = env->regwptr[UREG_I0];
2232 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2233 goto do_sigsegv;
2234 grp = &ucp->tuc_mcontext.mc_gregs;
2235 err = __get_user(pc, &((*grp)[MC_PC]));
2236 err |= __get_user(npc, &((*grp)[MC_NPC]));
2237 if (err || ((pc | npc) & 3))
2238 goto do_sigsegv;
2239 if (env->regwptr[UREG_I1]) {
2240 target_sigset_t target_set;
2241 sigset_t set;
2242
2243 if (TARGET_NSIG_WORDS == 1) {
2244 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2245 goto do_sigsegv;
2246 } else {
2247 abi_ulong *src, *dst;
2248 src = ucp->tuc_sigmask.sig;
2249 dst = target_set.sig;
2250 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2251 i++, dst++, src++)
2252 err |= __get_user(*dst, src);
2253 if (err)
2254 goto do_sigsegv;
2255 }
2256 target_to_host_sigset_internal(&set, &target_set);
2257 sigprocmask(SIG_SETMASK, &set, NULL);
2258 }
2259 env->pc = pc;
2260 env->npc = npc;
2261 err |= __get_user(env->y, &((*grp)[MC_Y]));
2262 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2263 env->asi = (tstate >> 24) & 0xff;
2264 cpu_put_ccr(env, tstate >> 32);
2265 cpu_put_cwp64(env, tstate & 0x1f);
2266 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2267 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2268 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2269 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2270 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2271 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2272 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2273 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2274 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2275 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2276 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2277 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2278 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2279 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2280 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2281
2282 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2283 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2284
2285 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2286 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2287 abi_ulong) != 0)
2288 goto do_sigsegv;
2289 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2290 abi_ulong) != 0)
2291 goto do_sigsegv;
2292 /* FIXME this does not match how the kernel handles the FPU in
2293 * its sparc64_set_context implementation. In particular the FPU
2294 * is only restored if fenab is non-zero in:
2295 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2296 */
2297 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2298 {
2299 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2300 for (i = 0; i < 64; i++, src++) {
2301 if (i & 1) {
2302 err |= __get_user(env->fpr[i/2].l.lower, src);
2303 } else {
2304 err |= __get_user(env->fpr[i/2].l.upper, src);
2305 }
2306 }
2307 }
2308 err |= __get_user(env->fsr,
2309 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2310 err |= __get_user(env->gsr,
2311 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2312 if (err)
2313 goto do_sigsegv;
2314 unlock_user_struct(ucp, ucp_addr, 0);
2315 return;
2316 do_sigsegv:
2317 unlock_user_struct(ucp, ucp_addr, 0);
2318 force_sig(TARGET_SIGSEGV);
2319 }
2320
2321 void sparc64_get_context(CPUSPARCState *env)
2322 {
2323 abi_ulong ucp_addr;
2324 struct target_ucontext *ucp;
2325 target_mc_gregset_t *grp;
2326 target_mcontext_t *mcp;
2327 abi_ulong fp, i7, w_addr;
2328 int err;
2329 unsigned int i;
2330 target_sigset_t target_set;
2331 sigset_t set;
2332
2333 ucp_addr = env->regwptr[UREG_I0];
2334 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2335 goto do_sigsegv;
2336
2337 mcp = &ucp->tuc_mcontext;
2338 grp = &mcp->mc_gregs;
2339
2340 /* Skip over the trap instruction, first. */
2341 env->pc = env->npc;
2342 env->npc += 4;
2343
2344 err = 0;
2345
2346 sigprocmask(0, NULL, &set);
2347 host_to_target_sigset_internal(&target_set, &set);
2348 if (TARGET_NSIG_WORDS == 1) {
2349 err |= __put_user(target_set.sig[0],
2350 (abi_ulong *)&ucp->tuc_sigmask);
2351 } else {
2352 abi_ulong *src, *dst;
2353 src = target_set.sig;
2354 dst = ucp->tuc_sigmask.sig;
2355 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2356 i++, dst++, src++)
2357 err |= __put_user(*src, dst);
2358 if (err)
2359 goto do_sigsegv;
2360 }
2361
2362 /* XXX: tstate must be saved properly */
2363 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2364 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2365 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2366 err |= __put_user(env->y, &((*grp)[MC_Y]));
2367 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2368 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2369 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2370 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2371 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2372 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2373 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2374 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2375 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2376 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2377 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2378 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2379 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2380 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2381 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2382
2383 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2384 fp = i7 = 0;
2385 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2386 abi_ulong) != 0)
2387 goto do_sigsegv;
2388 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2389 abi_ulong) != 0)
2390 goto do_sigsegv;
2391 err |= __put_user(fp, &(mcp->mc_fp));
2392 err |= __put_user(i7, &(mcp->mc_i7));
2393
2394 {
2395 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2396 for (i = 0; i < 64; i++, dst++) {
2397 if (i & 1) {
2398 err |= __put_user(env->fpr[i/2].l.lower, dst);
2399 } else {
2400 err |= __put_user(env->fpr[i/2].l.upper, dst);
2401 }
2402 }
2403 }
2404 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2405 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2406 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2407
2408 if (err)
2409 goto do_sigsegv;
2410 unlock_user_struct(ucp, ucp_addr, 1);
2411 return;
2412 do_sigsegv:
2413 unlock_user_struct(ucp, ucp_addr, 1);
2414 force_sig(TARGET_SIGSEGV);
2415 }
2416 #endif
2417 #elif defined(TARGET_ABI_MIPSN64)
2418
2419 # warning signal handling not implemented
2420
2421 static void setup_frame(int sig, struct target_sigaction *ka,
2422 target_sigset_t *set, CPUState *env)
2423 {
2424 fprintf(stderr, "setup_frame: not implemented\n");
2425 }
2426
2427 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2428 target_siginfo_t *info,
2429 target_sigset_t *set, CPUState *env)
2430 {
2431 fprintf(stderr, "setup_rt_frame: not implemented\n");
2432 }
2433
2434 long do_sigreturn(CPUState *env)
2435 {
2436 fprintf(stderr, "do_sigreturn: not implemented\n");
2437 return -TARGET_ENOSYS;
2438 }
2439
2440 long do_rt_sigreturn(CPUState *env)
2441 {
2442 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2443 return -TARGET_ENOSYS;
2444 }
2445
2446 #elif defined(TARGET_ABI_MIPSN32)
2447
2448 # warning signal handling not implemented
2449
2450 static void setup_frame(int sig, struct target_sigaction *ka,
2451 target_sigset_t *set, CPUState *env)
2452 {
2453 fprintf(stderr, "setup_frame: not implemented\n");
2454 }
2455
2456 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2457 target_siginfo_t *info,
2458 target_sigset_t *set, CPUState *env)
2459 {
2460 fprintf(stderr, "setup_rt_frame: not implemented\n");
2461 }
2462
2463 long do_sigreturn(CPUState *env)
2464 {
2465 fprintf(stderr, "do_sigreturn: not implemented\n");
2466 return -TARGET_ENOSYS;
2467 }
2468
2469 long do_rt_sigreturn(CPUState *env)
2470 {
2471 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2472 return -TARGET_ENOSYS;
2473 }
2474
2475 #elif defined(TARGET_ABI_MIPSO32)
2476
2477 struct target_sigcontext {
2478 uint32_t sc_regmask; /* Unused */
2479 uint32_t sc_status;
2480 uint64_t sc_pc;
2481 uint64_t sc_regs[32];
2482 uint64_t sc_fpregs[32];
2483 uint32_t sc_ownedfp; /* Unused */
2484 uint32_t sc_fpc_csr;
2485 uint32_t sc_fpc_eir; /* Unused */
2486 uint32_t sc_used_math;
2487 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2488 uint32_t pad0;
2489 uint64_t sc_mdhi;
2490 uint64_t sc_mdlo;
2491 target_ulong sc_hi1; /* Was sc_cause */
2492 target_ulong sc_lo1; /* Was sc_badvaddr */
2493 target_ulong sc_hi2; /* Was sc_sigset[4] */
2494 target_ulong sc_lo2;
2495 target_ulong sc_hi3;
2496 target_ulong sc_lo3;
2497 };
2498
2499 struct sigframe {
2500 uint32_t sf_ass[4]; /* argument save space for o32 */
2501 uint32_t sf_code[2]; /* signal trampoline */
2502 struct target_sigcontext sf_sc;
2503 target_sigset_t sf_mask;
2504 };
2505
2506 struct target_ucontext {
2507 target_ulong tuc_flags;
2508 target_ulong tuc_link;
2509 target_stack_t tuc_stack;
2510 target_ulong pad0;
2511 struct target_sigcontext tuc_mcontext;
2512 target_sigset_t tuc_sigmask;
2513 };
2514
2515 struct target_rt_sigframe {
2516 uint32_t rs_ass[4]; /* argument save space for o32 */
2517 uint32_t rs_code[2]; /* signal trampoline */
2518 struct target_siginfo rs_info;
2519 struct target_ucontext rs_uc;
2520 };
2521
2522 /* Install trampoline to jump back from signal handler */
2523 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2524 {
2525 int err;
2526
2527 /*
2528 * Set up the return code ...
2529 *
2530 * li v0, __NR__foo_sigreturn
2531 * syscall
2532 */
2533
2534 err = __put_user(0x24020000 + syscall, tramp + 0);
2535 err |= __put_user(0x0000000c , tramp + 1);
2536 /* flush_cache_sigtramp((unsigned long) tramp); */
2537 return err;
2538 }
2539
2540 static inline int
2541 setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2542 {
2543 int err = 0;
2544
2545 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2546
2547 #define save_gp_reg(i) do { \
2548 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2549 } while(0)
2550 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2551 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2552 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2553 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2554 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2555 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2556 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2557 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2558 save_gp_reg(31);
2559 #undef save_gp_reg
2560
2561 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2562 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2563
2564 /* Not used yet, but might be useful if we ever have DSP suppport */
2565 #if 0
2566 if (cpu_has_dsp) {
2567 err |= __put_user(mfhi1(), &sc->sc_hi1);
2568 err |= __put_user(mflo1(), &sc->sc_lo1);
2569 err |= __put_user(mfhi2(), &sc->sc_hi2);
2570 err |= __put_user(mflo2(), &sc->sc_lo2);
2571 err |= __put_user(mfhi3(), &sc->sc_hi3);
2572 err |= __put_user(mflo3(), &sc->sc_lo3);
2573 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2574 }
2575 /* same with 64 bit */
2576 #ifdef CONFIG_64BIT
2577 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2578 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2579 if (cpu_has_dsp) {
2580 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2581 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2582 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2583 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2584 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2585 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2586 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2587 }
2588 #endif
2589 #endif
2590
2591 #if 0
2592 err |= __put_user(!!used_math(), &sc->sc_used_math);
2593
2594 if (!used_math())
2595 goto out;
2596
2597 /*
2598 * Save FPU state to signal context. Signal handler will "inherit"
2599 * current FPU state.
2600 */
2601 preempt_disable();
2602
2603 if (!is_fpu_owner()) {
2604 own_fpu();
2605 restore_fp(current);
2606 }
2607 err |= save_fp_context(sc);
2608
2609 preempt_enable();
2610 out:
2611 #endif
2612 return err;
2613 }
2614
2615 static inline int
2616 restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2617 {
2618 int err = 0;
2619
2620 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2621
2622 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2623 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2624
2625 #define restore_gp_reg(i) do { \
2626 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
2627 } while(0)
2628 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2629 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2630 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2631 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2632 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2633 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2634 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2635 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2636 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2637 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2638 restore_gp_reg(31);
2639 #undef restore_gp_reg
2640
2641 #if 0
2642 if (cpu_has_dsp) {
2643 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2644 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2645 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2646 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2647 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2648 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2649 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2650 }
2651 #ifdef CONFIG_64BIT
2652 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2653 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2654 if (cpu_has_dsp) {
2655 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2656 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2657 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2658 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2659 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2660 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2661 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2662 }
2663 #endif
2664
2665 err |= __get_user(used_math, &sc->sc_used_math);
2666 conditional_used_math(used_math);
2667
2668 preempt_disable();
2669
2670 if (used_math()) {
2671 /* restore fpu context if we have used it before */
2672 own_fpu();
2673 err |= restore_fp_context(sc);
2674 } else {
2675 /* signal handler may have used FPU. Give it up. */
2676 lose_fpu();
2677 }
2678
2679 preempt_enable();
2680 #endif
2681 return err;
2682 }
2683 /*
2684 * Determine which stack to use..
2685 */
2686 static inline abi_ulong
2687 get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2688 {
2689 unsigned long sp;
2690
2691 /* Default to using normal stack */
2692 sp = regs->active_tc.gpr[29];
2693
2694 /*
2695 * FPU emulator may have it's own trampoline active just
2696 * above the user stack, 16-bytes before the next lowest
2697 * 16 byte boundary. Try to avoid trashing it.
2698 */
2699 sp -= 32;
2700
2701 /* This is the X/Open sanctioned signal stack switching. */
2702 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2703 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2704 }
2705
2706 return (sp - frame_size) & ~7;
2707 }
2708
2709 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2710 static void setup_frame(int sig, struct target_sigaction * ka,
2711 target_sigset_t *set, CPUState *regs)
2712 {
2713 struct sigframe *frame;
2714 abi_ulong frame_addr;
2715 int i;
2716
2717 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2718 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2719 goto give_sigsegv;
2720
2721 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2722
2723 if(setup_sigcontext(regs, &frame->sf_sc))
2724 goto give_sigsegv;
2725
2726 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2727 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2728 goto give_sigsegv;
2729 }
2730
2731 /*
2732 * Arguments to signal handler:
2733 *
2734 * a0 = signal number
2735 * a1 = 0 (should be cause)
2736 * a2 = pointer to struct sigcontext
2737 *
2738 * $25 and PC point to the signal handler, $29 points to the
2739 * struct sigframe.
2740 */
2741 regs->active_tc.gpr[ 4] = sig;
2742 regs->active_tc.gpr[ 5] = 0;
2743 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2744 regs->active_tc.gpr[29] = frame_addr;
2745 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2746 /* The original kernel code sets CP0_EPC to the handler
2747 * since it returns to userland using eret
2748 * we cannot do this here, and we must set PC directly */
2749 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2750 unlock_user_struct(frame, frame_addr, 1);
2751 return;
2752
2753 give_sigsegv:
2754 unlock_user_struct(frame, frame_addr, 1);
2755 force_sig(TARGET_SIGSEGV/*, current*/);
2756 return;
2757 }
2758
2759 long do_sigreturn(CPUState *regs)
2760 {
2761 struct sigframe *frame;
2762 abi_ulong frame_addr;
2763 sigset_t blocked;
2764 target_sigset_t target_set;
2765 int i;
2766
2767 #if defined(DEBUG_SIGNAL)
2768 fprintf(stderr, "do_sigreturn\n");
2769 #endif
2770 frame_addr = regs->active_tc.gpr[29];
2771 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2772 goto badframe;
2773
2774 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2775 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2776 goto badframe;
2777 }
2778
2779 target_to_host_sigset_internal(&blocked, &target_set);
2780 sigprocmask(SIG_SETMASK, &blocked, NULL);
2781
2782 if (restore_sigcontext(regs, &frame->sf_sc))
2783 goto badframe;
2784
2785 #if 0
2786 /*
2787 * Don't let your children do this ...
2788 */
2789 __asm__ __volatile__(
2790 "move\t$29, %0\n\t"
2791 "j\tsyscall_exit"
2792 :/* no outputs */
2793 :"r" (&regs));
2794 /* Unreached */
2795 #endif
2796
2797 regs->active_tc.PC = regs->CP0_EPC;
2798 /* I am not sure this is right, but it seems to work
2799 * maybe a problem with nested signals ? */
2800 regs->CP0_EPC = 0;
2801 return -TARGET_QEMU_ESIGRETURN;
2802
2803 badframe:
2804 force_sig(TARGET_SIGSEGV/*, current*/);
2805 return 0;
2806 }
2807
2808 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2809 target_siginfo_t *info,
2810 target_sigset_t *set, CPUState *env)
2811 {
2812 struct target_rt_sigframe *frame;
2813 abi_ulong frame_addr;
2814 int i;
2815
2816 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2817 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2818 goto give_sigsegv;
2819
2820 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2821
2822 copy_siginfo_to_user(&frame->rs_info, info);
2823
2824 __put_user(0, &frame->rs_uc.tuc_flags);
2825 __put_user(0, &frame->rs_uc.tuc_link);
2826 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2827 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2828 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2829 &frame->rs_uc.tuc_stack.ss_flags);
2830
2831 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2832
2833 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2834 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2835 }
2836
2837 /*
2838 * Arguments to signal handler:
2839 *
2840 * a0 = signal number
2841 * a1 = pointer to struct siginfo
2842 * a2 = pointer to struct ucontext
2843 *
2844 * $25 and PC point to the signal handler, $29 points to the
2845 * struct sigframe.
2846 */
2847 env->active_tc.gpr[ 4] = sig;
2848 env->active_tc.gpr[ 5] = frame_addr
2849 + offsetof(struct target_rt_sigframe, rs_info);
2850 env->active_tc.gpr[ 6] = frame_addr
2851 + offsetof(struct target_rt_sigframe, rs_uc);
2852 env->active_tc.gpr[29] = frame_addr;
2853 env->active_tc.gpr[31] = frame_addr
2854 + offsetof(struct target_rt_sigframe, rs_code);
2855 /* The original kernel code sets CP0_EPC to the handler
2856 * since it returns to userland using eret
2857 * we cannot do this here, and we must set PC directly */
2858 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2859 unlock_user_struct(frame, frame_addr, 1);
2860 return;
2861
2862 give_sigsegv:
2863 unlock_user_struct(frame, frame_addr, 1);
2864 force_sig(TARGET_SIGSEGV/*, current*/);
2865 return;
2866 }
2867
2868 long do_rt_sigreturn(CPUState *env)
2869 {
2870 struct target_rt_sigframe *frame;
2871 abi_ulong frame_addr;
2872 sigset_t blocked;
2873
2874 #if defined(DEBUG_SIGNAL)
2875 fprintf(stderr, "do_rt_sigreturn\n");
2876 #endif
2877 frame_addr = env->active_tc.gpr[29];
2878 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2879 goto badframe;
2880
2881 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2882 sigprocmask(SIG_SETMASK, &blocked, NULL);
2883
2884 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2885 goto badframe;
2886
2887 if (do_sigaltstack(frame_addr +
2888 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2889 0, get_sp_from_cpustate(env)) == -EFAULT)
2890 goto badframe;
2891
2892 env->active_tc.PC = env->CP0_EPC;
2893 /* I am not sure this is right, but it seems to work
2894 * maybe a problem with nested signals ? */
2895 env->CP0_EPC = 0;
2896 return -TARGET_QEMU_ESIGRETURN;
2897
2898 badframe:
2899 force_sig(TARGET_SIGSEGV/*, current*/);
2900 return 0;
2901 }
2902
2903 #elif defined(TARGET_SH4)
2904
2905 /*
2906 * code and data structures from linux kernel:
2907 * include/asm-sh/sigcontext.h
2908 * arch/sh/kernel/signal.c
2909 */
2910
2911 struct target_sigcontext {
2912 target_ulong oldmask;
2913
2914 /* CPU registers */
2915 target_ulong sc_gregs[16];
2916 target_ulong sc_pc;
2917 target_ulong sc_pr;
2918 target_ulong sc_sr;
2919 target_ulong sc_gbr;
2920 target_ulong sc_mach;
2921 target_ulong sc_macl;
2922
2923 /* FPU registers */
2924 target_ulong sc_fpregs[16];
2925 target_ulong sc_xfpregs[16];
2926 unsigned int sc_fpscr;
2927 unsigned int sc_fpul;
2928 unsigned int sc_ownedfp;
2929 };
2930
2931 struct target_sigframe
2932 {
2933 struct target_sigcontext sc;
2934 target_ulong extramask[TARGET_NSIG_WORDS-1];
2935 uint16_t retcode[3];
2936 };
2937
2938
2939 struct target_ucontext {
2940 target_ulong tuc_flags;
2941 struct target_ucontext *tuc_link;
2942 target_stack_t tuc_stack;
2943 struct target_sigcontext tuc_mcontext;
2944 target_sigset_t tuc_sigmask; /* mask last for extensibility */
2945 };
2946
2947 struct target_rt_sigframe
2948 {
2949 struct target_siginfo info;
2950 struct target_ucontext uc;
2951 uint16_t retcode[3];
2952 };
2953
2954
2955 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2956 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2957
2958 static abi_ulong get_sigframe(struct target_sigaction *ka,
2959 unsigned long sp, size_t frame_size)
2960 {
2961 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2962 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2963 }
2964
2965 return (sp - frame_size) & -8ul;
2966 }
2967
2968 static int setup_sigcontext(struct target_sigcontext *sc,
2969 CPUState *regs, unsigned long mask)
2970 {
2971 int err = 0;
2972 int i;
2973
2974 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2975 COPY(gregs[0]); COPY(gregs[1]);
2976 COPY(gregs[2]); COPY(gregs[3]);
2977 COPY(gregs[4]); COPY(gregs[5]);
2978 COPY(gregs[6]); COPY(gregs[7]);
2979 COPY(gregs[8]); COPY(gregs[9]);
2980 COPY(gregs[10]); COPY(gregs[11]);
2981 COPY(gregs[12]); COPY(gregs[13]);
2982 COPY(gregs[14]); COPY(gregs[15]);
2983 COPY(gbr); COPY(mach);
2984 COPY(macl); COPY(pr);
2985 COPY(sr); COPY(pc);
2986 #undef COPY
2987
2988 for (i=0; i<16; i++) {
2989 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2990 }
2991 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2992 err |= __put_user(regs->fpul, &sc->sc_fpul);
2993
2994 /* non-iBCS2 extensions.. */
2995 err |= __put_user(mask, &sc->oldmask);
2996
2997 return err;
2998 }
2999
3000 static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
3001 target_ulong *r0_p)
3002 {
3003 unsigned int err = 0;
3004 int i;
3005
3006 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3007 COPY(gregs[1]);
3008 COPY(gregs[2]); COPY(gregs[3]);
3009 COPY(gregs[4]); COPY(gregs[5]);
3010 COPY(gregs[6]); COPY(gregs[7]);
3011 COPY(gregs[8]); COPY(gregs[9]);
3012 COPY(gregs[10]); COPY(gregs[11]);
3013 COPY(gregs[12]); COPY(gregs[13]);
3014 COPY(gregs[14]); COPY(gregs[15]);
3015 COPY(gbr); COPY(mach);
3016 COPY(macl); COPY(pr);
3017 COPY(sr); COPY(pc);
3018 #undef COPY
3019
3020 for (i=0; i<16; i++) {
3021 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3022 }
3023 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3024 err |= __get_user(regs->fpul, &sc->sc_fpul);
3025
3026 regs->tra = -1; /* disable syscall checks */
3027 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3028 return err;
3029 }
3030
3031 static void setup_frame(int sig, struct target_sigaction *ka,
3032 target_sigset_t *set, CPUState *regs)
3033 {
3034 struct target_sigframe *frame;
3035 abi_ulong frame_addr;
3036 int i;
3037 int err = 0;
3038 int signal;
3039
3040 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3041 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3042 goto give_sigsegv;
3043
3044 signal = current_exec_domain_sig(sig);
3045
3046 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3047
3048 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3049 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3050 }
3051
3052 /* Set up to return from userspace. If provided, use a stub
3053 already in userspace. */
3054 if (ka->sa_flags & TARGET_SA_RESTORER) {
3055 regs->pr = (unsigned long) ka->sa_restorer;
3056 } else {
3057 /* Generate return code (system call to sigreturn) */
3058 err |= __put_user(MOVW(2), &frame->retcode[0]);
3059 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3060 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3061 regs->pr = (unsigned long) frame->retcode;
3062 }
3063
3064 if (err)
3065 goto give_sigsegv;
3066
3067 /* Set up registers for signal handler */
3068 regs->gregs[15] = frame_addr;
3069 regs->gregs[4] = signal; /* Arg for signal handler */
3070 regs->gregs[5] = 0;
3071 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3072 regs->pc = (unsigned long) ka->_sa_handler;
3073
3074 unlock_user_struct(frame, frame_addr, 1);
3075 return;
3076
3077 give_sigsegv:
3078 unlock_user_struct(frame, frame_addr, 1);
3079 force_sig(TARGET_SIGSEGV);
3080 }
3081
3082 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3083 target_siginfo_t *info,
3084 target_sigset_t *set, CPUState *regs)
3085 {
3086 struct target_rt_sigframe *frame;
3087 abi_ulong frame_addr;
3088 int i;
3089 int err = 0;
3090 int signal;
3091
3092 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3093 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3094 goto give_sigsegv;
3095
3096 signal = current_exec_domain_sig(sig);
3097
3098 err |= copy_siginfo_to_user(&frame->info, info);
3099
3100 /* Create the ucontext. */
3101 err |= __put_user(0, &frame->uc.tuc_flags);
3102 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3103 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3104 &frame->uc.tuc_stack.ss_sp);
3105 err |= __put_user(sas_ss_flags(regs->gregs[15]),
3106 &frame->uc.tuc_stack.ss_flags);
3107 err |= __put_user(target_sigaltstack_used.ss_size,
3108 &frame->uc.tuc_stack.ss_size);
3109 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3110 regs, set->sig[0]);
3111 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3112 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3113 }
3114
3115 /* Set up to return from userspace. If provided, use a stub
3116 already in userspace. */
3117 if (ka->sa_flags & TARGET_SA_RESTORER) {
3118 regs->pr = (unsigned long) ka->sa_restorer;
3119 } else {
3120 /* Generate return code (system call to sigreturn) */
3121 err |= __put_user(MOVW(2), &frame->retcode[0]);
3122 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3123 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3124 regs->pr = (unsigned long) frame->retcode;
3125 }
3126
3127 if (err)
3128 goto give_sigsegv;
3129
3130 /* Set up registers for signal handler */
3131 regs->gregs[15] = frame_addr;
3132 regs->gregs[4] = signal; /* Arg for signal handler */
3133 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3134 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3135 regs->pc = (unsigned long) ka->_sa_handler;
3136
3137 unlock_user_struct(frame, frame_addr, 1);
3138 return;
3139
3140 give_sigsegv:
3141 unlock_user_struct(frame, frame_addr, 1);
3142 force_sig(TARGET_SIGSEGV);
3143 }
3144
3145 long do_sigreturn(CPUState *regs)
3146 {
3147 struct target_sigframe *frame;
3148 abi_ulong frame_addr;
3149 sigset_t blocked;
3150 target_sigset_t target_set;
3151 target_ulong r0;
3152 int i;
3153 int err = 0;
3154
3155 #if defined(DEBUG_SIGNAL)
3156 fprintf(stderr, "do_sigreturn\n");
3157 #endif
3158 frame_addr = regs->gregs[15];
3159 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3160 goto badframe;
3161
3162 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3163 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3164 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3165 }
3166
3167 if (err)
3168 goto badframe;
3169
3170 target_to_host_sigset_internal(&blocked, &target_set);
3171 sigprocmask(SIG_SETMASK, &blocked, NULL);
3172
3173 if (restore_sigcontext(regs, &frame->sc, &r0))
3174 goto badframe;
3175
3176 unlock_user_struct(frame, frame_addr, 0);
3177 return r0;
3178
3179 badframe:
3180 unlock_user_struct(frame, frame_addr, 0);
3181 force_sig(TARGET_SIGSEGV);
3182 return 0;
3183 }
3184
3185 long do_rt_sigreturn(CPUState *regs)
3186 {
3187 struct target_rt_sigframe *frame;
3188 abi_ulong frame_addr;
3189 sigset_t blocked;
3190 target_ulong r0;
3191
3192 #if defined(DEBUG_SIGNAL)
3193 fprintf(stderr, "do_rt_sigreturn\n");
3194 #endif
3195 frame_addr = regs->gregs[15];
3196 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3197 goto badframe;
3198
3199 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3200 sigprocmask(SIG_SETMASK