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