quorum: Inline quorum_aio_cb()
[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_X86_64)
258 /* Just set the guest's signal mask to the specified value; the
259 * caller is assumed to have called block_signals() already.
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 #if !(defined(TARGET_X86_64) || defined(TARGET_UNICORE32))
516 /* Force a synchronously taken signal. The kernel force_sig() function
517 * also forces the signal to "not blocked, not ignored", but for QEMU
518 * that work is done in process_pending_signals().
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) && TARGET_ABI_BITS == 32
823
824 /* from the Linux kernel */
825
826 struct target_fpreg {
827 uint16_t significand[4];
828 uint16_t exponent;
829 };
830
831 struct target_fpxreg {
832 uint16_t significand[4];
833 uint16_t exponent;
834 uint16_t padding[3];
835 };
836
837 struct target_xmmreg {
838 abi_ulong element[4];
839 };
840
841 struct target_fpstate {
842 /* Regular FPU environment */
843 abi_ulong cw;
844 abi_ulong sw;
845 abi_ulong tag;
846 abi_ulong ipoff;
847 abi_ulong cssel;
848 abi_ulong dataoff;
849 abi_ulong datasel;
850 struct target_fpreg _st[8];
851 uint16_t status;
852 uint16_t magic; /* 0xffff = regular FPU data only */
853
854 /* FXSR FPU environment */
855 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
856 abi_ulong mxcsr;
857 abi_ulong reserved;
858 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
859 struct target_xmmreg _xmm[8];
860 abi_ulong padding[56];
861 };
862
863 #define X86_FXSR_MAGIC 0x0000
864
865 struct target_sigcontext {
866 uint16_t gs, __gsh;
867 uint16_t fs, __fsh;
868 uint16_t es, __esh;
869 uint16_t ds, __dsh;
870 abi_ulong edi;
871 abi_ulong esi;
872 abi_ulong ebp;
873 abi_ulong esp;
874 abi_ulong ebx;
875 abi_ulong edx;
876 abi_ulong ecx;
877 abi_ulong eax;
878 abi_ulong trapno;
879 abi_ulong err;
880 abi_ulong eip;
881 uint16_t cs, __csh;
882 abi_ulong eflags;
883 abi_ulong esp_at_signal;
884 uint16_t ss, __ssh;
885 abi_ulong fpstate; /* pointer */
886 abi_ulong oldmask;
887 abi_ulong cr2;
888 };
889
890 struct target_ucontext {
891 abi_ulong tuc_flags;
892 abi_ulong tuc_link;
893 target_stack_t tuc_stack;
894 struct target_sigcontext tuc_mcontext;
895 target_sigset_t tuc_sigmask; /* mask last for extensibility */
896 };
897
898 struct sigframe
899 {
900 abi_ulong pretcode;
901 int sig;
902 struct target_sigcontext sc;
903 struct target_fpstate fpstate;
904 abi_ulong extramask[TARGET_NSIG_WORDS-1];
905 char retcode[8];
906 };
907
908 struct rt_sigframe
909 {
910 abi_ulong pretcode;
911 int sig;
912 abi_ulong pinfo;
913 abi_ulong puc;
914 struct target_siginfo info;
915 struct target_ucontext uc;
916 struct target_fpstate fpstate;
917 char retcode[8];
918 };
919
920 /*
921 * Set up a signal frame.
922 */
923
924 /* XXX: save x87 state */
925 static void setup_sigcontext(struct target_sigcontext *sc,
926 struct target_fpstate *fpstate, CPUX86State *env, abi_ulong mask,
927 abi_ulong fpstate_addr)
928 {
929 CPUState *cs = CPU(x86_env_get_cpu(env));
930 uint16_t magic;
931
932 /* already locked in setup_frame() */
933 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
934 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
935 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
936 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
937 __put_user(env->regs[R_EDI], &sc->edi);
938 __put_user(env->regs[R_ESI], &sc->esi);
939 __put_user(env->regs[R_EBP], &sc->ebp);
940 __put_user(env->regs[R_ESP], &sc->esp);
941 __put_user(env->regs[R_EBX], &sc->ebx);
942 __put_user(env->regs[R_EDX], &sc->edx);
943 __put_user(env->regs[R_ECX], &sc->ecx);
944 __put_user(env->regs[R_EAX], &sc->eax);
945 __put_user(cs->exception_index, &sc->trapno);
946 __put_user(env->error_code, &sc->err);
947 __put_user(env->eip, &sc->eip);
948 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
949 __put_user(env->eflags, &sc->eflags);
950 __put_user(env->regs[R_ESP], &sc->esp_at_signal);
951 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
952
953 cpu_x86_fsave(env, fpstate_addr, 1);
954 fpstate->status = fpstate->sw;
955 magic = 0xffff;
956 __put_user(magic, &fpstate->magic);
957 __put_user(fpstate_addr, &sc->fpstate);
958
959 /* non-iBCS2 extensions.. */
960 __put_user(mask, &sc->oldmask);
961 __put_user(env->cr[2], &sc->cr2);
962 }
963
964 /*
965 * Determine which stack to use..
966 */
967
968 static inline abi_ulong
969 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
970 {
971 unsigned long esp;
972
973 /* Default to using normal stack */
974 esp = env->regs[R_ESP];
975 /* This is the X/Open sanctioned signal stack switching. */
976 if (ka->sa_flags & TARGET_SA_ONSTACK) {
977 if (sas_ss_flags(esp) == 0) {
978 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
979 }
980 } else {
981
982 /* This is the legacy signal stack switching. */
983 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
984 !(ka->sa_flags & TARGET_SA_RESTORER) &&
985 ka->sa_restorer) {
986 esp = (unsigned long) ka->sa_restorer;
987 }
988 }
989 return (esp - frame_size) & -8ul;
990 }
991
992 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */
993 static void setup_frame(int sig, struct target_sigaction *ka,
994 target_sigset_t *set, CPUX86State *env)
995 {
996 abi_ulong frame_addr;
997 struct sigframe *frame;
998 int i;
999
1000 frame_addr = get_sigframe(ka, env, sizeof(*frame));
1001 trace_user_setup_frame(env, frame_addr);
1002
1003 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1004 goto give_sigsegv;
1005
1006 __put_user(sig, &frame->sig);
1007
1008 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
1009 frame_addr + offsetof(struct sigframe, fpstate));
1010
1011 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1012 __put_user(set->sig[i], &frame->extramask[i - 1]);
1013 }
1014
1015 /* Set up to return from userspace. If provided, use a stub
1016 already in userspace. */
1017 if (ka->sa_flags & TARGET_SA_RESTORER) {
1018 __put_user(ka->sa_restorer, &frame->pretcode);
1019 } else {
1020 uint16_t val16;
1021 abi_ulong retcode_addr;
1022 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
1023 __put_user(retcode_addr, &frame->pretcode);
1024 /* This is popl %eax ; movl $,%eax ; int $0x80 */
1025 val16 = 0xb858;
1026 __put_user(val16, (uint16_t *)(frame->retcode+0));
1027 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
1028 val16 = 0x80cd;
1029 __put_user(val16, (uint16_t *)(frame->retcode+6));
1030 }
1031
1032
1033 /* Set up registers for signal handler */
1034 env->regs[R_ESP] = frame_addr;
1035 env->eip = ka->_sa_handler;
1036
1037 cpu_x86_load_seg(env, R_DS, __USER_DS);
1038 cpu_x86_load_seg(env, R_ES, __USER_DS);
1039 cpu_x86_load_seg(env, R_SS, __USER_DS);
1040 cpu_x86_load_seg(env, R_CS, __USER_CS);
1041 env->eflags &= ~TF_MASK;
1042
1043 unlock_user_struct(frame, frame_addr, 1);
1044
1045 return;
1046
1047 give_sigsegv:
1048 force_sigsegv(sig);
1049 }
1050
1051 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
1052 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1053 target_siginfo_t *info,
1054 target_sigset_t *set, CPUX86State *env)
1055 {
1056 abi_ulong frame_addr, addr;
1057 struct rt_sigframe *frame;
1058 int i;
1059
1060 frame_addr = get_sigframe(ka, env, sizeof(*frame));
1061 trace_user_setup_rt_frame(env, frame_addr);
1062
1063 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1064 goto give_sigsegv;
1065
1066 __put_user(sig, &frame->sig);
1067 addr = frame_addr + offsetof(struct rt_sigframe, info);
1068 __put_user(addr, &frame->pinfo);
1069 addr = frame_addr + offsetof(struct rt_sigframe, uc);
1070 __put_user(addr, &frame->puc);
1071 tswap_siginfo(&frame->info, info);
1072
1073 /* Create the ucontext. */
1074 __put_user(0, &frame->uc.tuc_flags);
1075 __put_user(0, &frame->uc.tuc_link);
1076 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
1077 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
1078 &frame->uc.tuc_stack.ss_flags);
1079 __put_user(target_sigaltstack_used.ss_size,
1080 &frame->uc.tuc_stack.ss_size);
1081 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env,
1082 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate));
1083
1084 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1085 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
1086 }
1087
1088 /* Set up to return from userspace. If provided, use a stub
1089 already in userspace. */
1090 if (ka->sa_flags & TARGET_SA_RESTORER) {
1091 __put_user(ka->sa_restorer, &frame->pretcode);
1092 } else {
1093 uint16_t val16;
1094 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
1095 __put_user(addr, &frame->pretcode);
1096 /* This is movl $,%eax ; int $0x80 */
1097 __put_user(0xb8, (char *)(frame->retcode+0));
1098 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
1099 val16 = 0x80cd;
1100 __put_user(val16, (uint16_t *)(frame->retcode+5));
1101 }
1102
1103 /* Set up registers for signal handler */
1104 env->regs[R_ESP] = frame_addr;
1105 env->eip = ka->_sa_handler;
1106
1107 cpu_x86_load_seg(env, R_DS, __USER_DS);
1108 cpu_x86_load_seg(env, R_ES, __USER_DS);
1109 cpu_x86_load_seg(env, R_SS, __USER_DS);
1110 cpu_x86_load_seg(env, R_CS, __USER_CS);
1111 env->eflags &= ~TF_MASK;
1112
1113 unlock_user_struct(frame, frame_addr, 1);
1114
1115 return;
1116
1117 give_sigsegv:
1118 force_sigsegv(sig);
1119 }
1120
1121 static int
1122 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc)
1123 {
1124 unsigned int err = 0;
1125 abi_ulong fpstate_addr;
1126 unsigned int tmpflags;
1127
1128 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1129 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1130 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1131 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
1132
1133 env->regs[R_EDI] = tswapl(sc->edi);
1134 env->regs[R_ESI] = tswapl(sc->esi);
1135 env->regs[R_EBP] = tswapl(sc->ebp);
1136 env->regs[R_ESP] = tswapl(sc->esp);
1137 env->regs[R_EBX] = tswapl(sc->ebx);
1138 env->regs[R_EDX] = tswapl(sc->edx);
1139 env->regs[R_ECX] = tswapl(sc->ecx);
1140 env->regs[R_EAX] = tswapl(sc->eax);
1141 env->eip = tswapl(sc->eip);
1142
1143 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1144 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
1145
1146 tmpflags = tswapl(sc->eflags);
1147 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1148 // regs->orig_eax = -1; /* disable syscall checks */
1149
1150 fpstate_addr = tswapl(sc->fpstate);
1151 if (fpstate_addr != 0) {
1152 if (!access_ok(VERIFY_READ, fpstate_addr,
1153 sizeof(struct target_fpstate)))
1154 goto badframe;
1155 cpu_x86_frstor(env, fpstate_addr, 1);
1156 }
1157
1158 return err;
1159 badframe:
1160 return 1;
1161 }
1162
1163 long do_sigreturn(CPUX86State *env)
1164 {
1165 struct sigframe *frame;
1166 abi_ulong frame_addr = env->regs[R_ESP] - 8;
1167 target_sigset_t target_set;
1168 sigset_t set;
1169 int i;
1170
1171 trace_user_do_sigreturn(env, frame_addr);
1172 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1173 goto badframe;
1174 /* set blocked signals */
1175 __get_user(target_set.sig[0], &frame->sc.oldmask);
1176 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1177 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
1178 }
1179
1180 target_to_host_sigset_internal(&set, &target_set);
1181 set_sigmask(&set);
1182
1183 /* restore registers */
1184 if (restore_sigcontext(env, &frame->sc))
1185 goto badframe;
1186 unlock_user_struct(frame, frame_addr, 0);
1187 return -TARGET_QEMU_ESIGRETURN;
1188
1189 badframe:
1190 unlock_user_struct(frame, frame_addr, 0);
1191 force_sig(TARGET_SIGSEGV);
1192 return -TARGET_QEMU_ESIGRETURN;
1193 }
1194
1195 long do_rt_sigreturn(CPUX86State *env)
1196 {
1197 abi_ulong frame_addr;
1198 struct rt_sigframe *frame;
1199 sigset_t set;
1200
1201 frame_addr = env->regs[R_ESP] - 4;
1202 trace_user_do_rt_sigreturn(env, frame_addr);
1203 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1204 goto badframe;
1205 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1206 set_sigmask(&set);
1207
1208 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
1209 goto badframe;
1210 }
1211
1212 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1213 get_sp_from_cpustate(env)) == -EFAULT) {
1214 goto badframe;
1215 }
1216
1217 unlock_user_struct(frame, frame_addr, 0);
1218 return -TARGET_QEMU_ESIGRETURN;
1219
1220 badframe:
1221 unlock_user_struct(frame, frame_addr, 0);
1222 force_sig(TARGET_SIGSEGV);
1223 return -TARGET_QEMU_ESIGRETURN;
1224 }
1225
1226 #elif defined(TARGET_AARCH64)
1227
1228 struct target_sigcontext {
1229 uint64_t fault_address;
1230 /* AArch64 registers */
1231 uint64_t regs[31];
1232 uint64_t sp;
1233 uint64_t pc;
1234 uint64_t pstate;
1235 /* 4K reserved for FP/SIMD state and future expansion */
1236 char __reserved[4096] __attribute__((__aligned__(16)));
1237 };
1238
1239 struct target_ucontext {
1240 abi_ulong tuc_flags;
1241 abi_ulong tuc_link;
1242 target_stack_t tuc_stack;
1243 target_sigset_t tuc_sigmask;
1244 /* glibc uses a 1024-bit sigset_t */
1245 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1246 /* last for future expansion */
1247 struct target_sigcontext tuc_mcontext;
1248 };
1249
1250 /*
1251 * Header to be used at the beginning of structures extending the user
1252 * context. Such structures must be placed after the rt_sigframe on the stack
1253 * and be 16-byte aligned. The last structure must be a dummy one with the
1254 * magic and size set to 0.
1255 */
1256 struct target_aarch64_ctx {
1257 uint32_t magic;
1258 uint32_t size;
1259 };
1260
1261 #define TARGET_FPSIMD_MAGIC 0x46508001
1262
1263 struct target_fpsimd_context {
1264 struct target_aarch64_ctx head;
1265 uint32_t fpsr;
1266 uint32_t fpcr;
1267 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1268 };
1269
1270 /*
1271 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1272 * user space as it will change with the addition of new context. User space
1273 * should check the magic/size information.
1274 */
1275 struct target_aux_context {
1276 struct target_fpsimd_context fpsimd;
1277 /* additional context to be added before "end" */
1278 struct target_aarch64_ctx end;
1279 };
1280
1281 struct target_rt_sigframe {
1282 struct target_siginfo info;
1283 struct target_ucontext uc;
1284 uint64_t fp;
1285 uint64_t lr;
1286 uint32_t tramp[2];
1287 };
1288
1289 static int target_setup_sigframe(struct target_rt_sigframe *sf,
1290 CPUARMState *env, target_sigset_t *set)
1291 {
1292 int i;
1293 struct target_aux_context *aux =
1294 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1295
1296 /* set up the stack frame for unwinding */
1297 __put_user(env->xregs[29], &sf->fp);
1298 __put_user(env->xregs[30], &sf->lr);
1299
1300 for (i = 0; i < 31; i++) {
1301 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1302 }
1303 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1304 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
1305 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
1306
1307 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
1308
1309 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1310 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1311 }
1312
1313 for (i = 0; i < 32; i++) {
1314 #ifdef TARGET_WORDS_BIGENDIAN
1315 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1316 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1317 #else
1318 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1319 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1320 #endif
1321 }
1322 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1323 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
1324 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1325 __put_user(sizeof(struct target_fpsimd_context),
1326 &aux->fpsimd.head.size);
1327
1328 /* set the "end" magic */
1329 __put_user(0, &aux->end.magic);
1330 __put_user(0, &aux->end.size);
1331
1332 return 0;
1333 }
1334
1335 static int target_restore_sigframe(CPUARMState *env,
1336 struct target_rt_sigframe *sf)
1337 {
1338 sigset_t set;
1339 int i;
1340 struct target_aux_context *aux =
1341 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1342 uint32_t magic, size, fpsr, fpcr;
1343 uint64_t pstate;
1344
1345 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
1346 set_sigmask(&set);
1347
1348 for (i = 0; i < 31; i++) {
1349 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1350 }
1351
1352 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1353 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
1354 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1355 pstate_write(env, pstate);
1356
1357 __get_user(magic, &aux->fpsimd.head.magic);
1358 __get_user(size, &aux->fpsimd.head.size);
1359
1360 if (magic != TARGET_FPSIMD_MAGIC
1361 || size != sizeof(struct target_fpsimd_context)) {
1362 return 1;
1363 }
1364
1365 for (i = 0; i < 32; i++) {
1366 #ifdef TARGET_WORDS_BIGENDIAN
1367 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1368 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1369 #else
1370 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1371 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1372 #endif
1373 }
1374 __get_user(fpsr, &aux->fpsimd.fpsr);
1375 vfp_set_fpsr(env, fpsr);
1376 __get_user(fpcr, &aux->fpsimd.fpcr);
1377 vfp_set_fpcr(env, fpcr);
1378
1379 return 0;
1380 }
1381
1382 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1383 {
1384 abi_ulong sp;
1385
1386 sp = env->xregs[31];
1387
1388 /*
1389 * This is the X/Open sanctioned signal stack switching.
1390 */
1391 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1392 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1393 }
1394
1395 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1396
1397 return sp;
1398 }
1399
1400 static void target_setup_frame(int usig, struct target_sigaction *ka,
1401 target_siginfo_t *info, target_sigset_t *set,
1402 CPUARMState *env)
1403 {
1404 struct target_rt_sigframe *frame;
1405 abi_ulong frame_addr, return_addr;
1406
1407 frame_addr = get_sigframe(ka, env);
1408 trace_user_setup_frame(env, frame_addr);
1409 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1410 goto give_sigsegv;
1411 }
1412
1413 __put_user(0, &frame->uc.tuc_flags);
1414 __put_user(0, &frame->uc.tuc_link);
1415
1416 __put_user(target_sigaltstack_used.ss_sp,
1417 &frame->uc.tuc_stack.ss_sp);
1418 __put_user(sas_ss_flags(env->xregs[31]),
1419 &frame->uc.tuc_stack.ss_flags);
1420 __put_user(target_sigaltstack_used.ss_size,
1421 &frame->uc.tuc_stack.ss_size);
1422 target_setup_sigframe(frame, env, set);
1423 if (ka->sa_flags & TARGET_SA_RESTORER) {
1424 return_addr = ka->sa_restorer;
1425 } else {
1426 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1427 __put_user(0xd2801168, &frame->tramp[0]);
1428 __put_user(0xd4000001, &frame->tramp[1]);
1429 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1430 }
1431 env->xregs[0] = usig;
1432 env->xregs[31] = frame_addr;
1433 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1434 env->pc = ka->_sa_handler;
1435 env->xregs[30] = return_addr;
1436 if (info) {
1437 tswap_siginfo(&frame->info, info);
1438 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1439 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1440 }
1441
1442 unlock_user_struct(frame, frame_addr, 1);
1443 return;
1444
1445 give_sigsegv:
1446 unlock_user_struct(frame, frame_addr, 1);
1447 force_sigsegv(usig);
1448 }
1449
1450 static void setup_rt_frame(int sig, struct target_sigaction *ka,
1451 target_siginfo_t *info, target_sigset_t *set,
1452 CPUARMState *env)
1453 {
1454 target_setup_frame(sig, ka, info, set, env);
1455 }
1456
1457 static void setup_frame(int sig, struct target_sigaction *ka,
1458 target_sigset_t *set, CPUARMState *env)
1459 {
1460 target_setup_frame(sig, ka, 0, set, env);
1461 }
1462
1463 long do_rt_sigreturn(CPUARMState *env)
1464 {
1465 struct target_rt_sigframe *frame = NULL;
1466 abi_ulong frame_addr = env->xregs[31];
1467
1468 trace_user_do_rt_sigreturn(env, frame_addr);
1469 if (frame_addr & 15) {
1470 goto badframe;
1471 }
1472
1473 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1474 goto badframe;
1475 }
1476
1477 if (target_restore_sigframe(env, frame)) {
1478 goto badframe;
1479 }
1480
1481 if (do_sigaltstack(frame_addr +
1482 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1483 0, get_sp_from_cpustate(env)) == -EFAULT) {
1484 goto badframe;
1485 }
1486
1487 unlock_user_struct(frame, frame_addr, 0);
1488 return -TARGET_QEMU_ESIGRETURN;
1489
1490 badframe:
1491 unlock_user_struct(frame, frame_addr, 0);
1492 force_sig(TARGET_SIGSEGV);
1493 return -TARGET_QEMU_ESIGRETURN;
1494 }
1495
1496 long do_sigreturn(CPUARMState *env)
1497 {
1498 return do_rt_sigreturn(env);
1499 }
1500
1501 #elif defined(TARGET_ARM)
1502
1503 struct target_sigcontext {
1504 abi_ulong trap_no;
1505 abi_ulong error_code;
1506 abi_ulong oldmask;
1507 abi_ulong arm_r0;
1508 abi_ulong arm_r1;
1509 abi_ulong arm_r2;
1510 abi_ulong arm_r3;
1511 abi_ulong arm_r4;
1512 abi_ulong arm_r5;
1513 abi_ulong arm_r6;
1514 abi_ulong arm_r7;
1515 abi_ulong arm_r8;
1516 abi_ulong arm_r9;
1517 abi_ulong arm_r10;
1518 abi_ulong arm_fp;
1519 abi_ulong arm_ip;
1520 abi_ulong arm_sp;
1521 abi_ulong arm_lr;
1522 abi_ulong arm_pc;
1523 abi_ulong arm_cpsr;
1524 abi_ulong fault_address;
1525 };
1526
1527 struct target_ucontext_v1 {
1528 abi_ulong tuc_flags;
1529 abi_ulong tuc_link;
1530 target_stack_t tuc_stack;
1531 struct target_sigcontext tuc_mcontext;
1532 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1533 };
1534
1535 struct target_ucontext_v2 {
1536 abi_ulong tuc_flags;
1537 abi_ulong tuc_link;
1538 target_stack_t tuc_stack;
1539 struct target_sigcontext tuc_mcontext;
1540 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1541 char __unused[128 - sizeof(target_sigset_t)];
1542 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1543 };
1544
1545 struct target_user_vfp {
1546 uint64_t fpregs[32];
1547 abi_ulong fpscr;
1548 };
1549
1550 struct target_user_vfp_exc {
1551 abi_ulong fpexc;
1552 abi_ulong fpinst;
1553 abi_ulong fpinst2;
1554 };
1555
1556 struct target_vfp_sigframe {
1557 abi_ulong magic;
1558 abi_ulong size;
1559 struct target_user_vfp ufp;
1560 struct target_user_vfp_exc ufp_exc;
1561 } __attribute__((__aligned__(8)));
1562
1563 struct target_iwmmxt_sigframe {
1564 abi_ulong magic;
1565 abi_ulong size;
1566 uint64_t regs[16];
1567 /* Note that not all the coprocessor control registers are stored here */
1568 uint32_t wcssf;
1569 uint32_t wcasf;
1570 uint32_t wcgr0;
1571 uint32_t wcgr1;
1572 uint32_t wcgr2;
1573 uint32_t wcgr3;
1574 } __attribute__((__aligned__(8)));
1575
1576 #define TARGET_VFP_MAGIC 0x56465001
1577 #define TARGET_IWMMXT_MAGIC 0x12ef842a
1578
1579 struct sigframe_v1
1580 {
1581 struct target_sigcontext sc;
1582 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1583 abi_ulong retcode;
1584 };
1585
1586 struct sigframe_v2
1587 {
1588 struct target_ucontext_v2 uc;
1589 abi_ulong retcode;
1590 };
1591
1592 struct rt_sigframe_v1
1593 {
1594 abi_ulong pinfo;
1595 abi_ulong puc;
1596 struct target_siginfo info;
1597 struct target_ucontext_v1 uc;
1598 abi_ulong retcode;
1599 };
1600
1601 struct rt_sigframe_v2
1602 {
1603 struct target_siginfo info;
1604 struct target_ucontext_v2 uc;
1605 abi_ulong retcode;
1606 };
1607
1608 #define TARGET_CONFIG_CPU_32 1
1609
1610 /*
1611 * For ARM syscalls, we encode the syscall number into the instruction.
1612 */
1613 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1614 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1615
1616 /*
1617 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1618 * need two 16-bit instructions.
1619 */
1620 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1621 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1622
1623 static const abi_ulong retcodes[4] = {
1624 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1625 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1626 };
1627
1628
1629 static inline int valid_user_regs(CPUARMState *regs)
1630 {
1631 return 1;
1632 }
1633
1634 static void
1635 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1636 CPUARMState *env, abi_ulong mask)
1637 {
1638 __put_user(env->regs[0], &sc->arm_r0);
1639 __put_user(env->regs[1], &sc->arm_r1);
1640 __put_user(env->regs[2], &sc->arm_r2);
1641 __put_user(env->regs[3], &sc->arm_r3);
1642 __put_user(env->regs[4], &sc->arm_r4);
1643 __put_user(env->regs[5], &sc->arm_r5);
1644 __put_user(env->regs[6], &sc->arm_r6);
1645 __put_user(env->regs[7], &sc->arm_r7);
1646 __put_user(env->regs[8], &sc->arm_r8);
1647 __put_user(env->regs[9], &sc->arm_r9);
1648 __put_user(env->regs[10], &sc->arm_r10);
1649 __put_user(env->regs[11], &sc->arm_fp);
1650 __put_user(env->regs[12], &sc->arm_ip);
1651 __put_user(env->regs[13], &sc->arm_sp);
1652 __put_user(env->regs[14], &sc->arm_lr);
1653 __put_user(env->regs[15], &sc->arm_pc);
1654 #ifdef TARGET_CONFIG_CPU_32
1655 __put_user(cpsr_read(env), &sc->arm_cpsr);
1656 #endif
1657
1658 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1659 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1660 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1661 __put_user(mask, &sc->oldmask);
1662 }
1663
1664 static inline abi_ulong
1665 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
1666 {
1667 unsigned long sp = regs->regs[13];
1668
1669 /*
1670 * This is the X/Open sanctioned signal stack switching.
1671 */
1672 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
1673 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1674 }
1675 /*
1676 * ATPCS B01 mandates 8-byte alignment
1677 */
1678 return (sp - framesize) & ~7;
1679 }
1680
1681 static void
1682 setup_return(CPUARMState *env, struct target_sigaction *ka,
1683 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1684 {
1685 abi_ulong handler = ka->_sa_handler;
1686 abi_ulong retcode;
1687 int thumb = handler & 1;
1688 uint32_t cpsr = cpsr_read(env);
1689
1690 cpsr &= ~CPSR_IT;
1691 if (thumb) {
1692 cpsr |= CPSR_T;
1693 } else {
1694 cpsr &= ~CPSR_T;
1695 }
1696
1697 if (ka->sa_flags & TARGET_SA_RESTORER) {
1698 retcode = ka->sa_restorer;
1699 } else {
1700 unsigned int idx = thumb;
1701
1702 if (ka->sa_flags & TARGET_SA_SIGINFO) {
1703 idx += 2;
1704 }
1705
1706 __put_user(retcodes[idx], rc);
1707
1708 retcode = rc_addr + thumb;
1709 }
1710
1711 env->regs[0] = usig;
1712 env->regs[13] = frame_addr;
1713 env->regs[14] = retcode;
1714 env->regs[15] = handler & (thumb ? ~1 : ~3);
1715 cpsr_write(env, cpsr, CPSR_IT | CPSR_T, CPSRWriteByInstr);
1716 }
1717
1718 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
1719 {
1720 int i;
1721 struct target_vfp_sigframe *vfpframe;
1722 vfpframe = (struct target_vfp_sigframe *)regspace;
1723 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1724 __put_user(sizeof(*vfpframe), &vfpframe->size);
1725 for (i = 0; i < 32; i++) {
1726 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1727 }
1728 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1729 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1730 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1731 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1732 return (abi_ulong*)(vfpframe+1);
1733 }
1734
1735 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1736 CPUARMState *env)
1737 {
1738 int i;
1739 struct target_iwmmxt_sigframe *iwmmxtframe;
1740 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1741 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1742 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1743 for (i = 0; i < 16; i++) {
1744 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1745 }
1746 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1747 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1748 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1749 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1750 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1751 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1752 return (abi_ulong*)(iwmmxtframe+1);
1753 }
1754
1755 static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1756 target_sigset_t *set, CPUARMState *env)
1757 {
1758 struct target_sigaltstack stack;
1759 int i;
1760 abi_ulong *regspace;
1761
1762 /* Clear all the bits of the ucontext we don't use. */
1763 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1764
1765 memset(&stack, 0, sizeof(stack));
1766 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1767 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1768 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1769 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1770
1771 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1772 /* Save coprocessor signal frame. */
1773 regspace = uc->tuc_regspace;
1774 if (arm_feature(env, ARM_FEATURE_VFP)) {
1775 regspace = setup_sigframe_v2_vfp(regspace, env);
1776 }
1777 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1778 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1779 }
1780
1781 /* Write terminating magic word */
1782 __put_user(0, regspace);
1783
1784 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1785 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1786 }
1787 }
1788
1789 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1790 static void setup_frame_v1(int usig, struct target_sigaction *ka,
1791 target_sigset_t *set, CPUARMState *regs)
1792 {
1793 struct sigframe_v1 *frame;
1794 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1795 int i;
1796
1797 trace_user_setup_frame(regs, frame_addr);
1798 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1799 goto sigsegv;
1800 }
1801
1802 setup_sigcontext(&frame->sc, regs, set->sig[0]);
1803
1804 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1805 __put_user(set->sig[i], &frame->extramask[i - 1]);
1806 }
1807
1808 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1809 frame_addr + offsetof(struct sigframe_v1, retcode));
1810
1811 unlock_user_struct(frame, frame_addr, 1);
1812 return;
1813 sigsegv:
1814 force_sigsegv(usig);
1815 }
1816
1817 static void setup_frame_v2(int usig, struct target_sigaction *ka,
1818 target_sigset_t *set, CPUARMState *regs)
1819 {
1820 struct sigframe_v2 *frame;
1821 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1822
1823 trace_user_setup_frame(regs, frame_addr);
1824 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1825 goto sigsegv;
1826 }
1827
1828 setup_sigframe_v2(&frame->uc, set, regs);
1829
1830 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1831 frame_addr + offsetof(struct sigframe_v2, retcode));
1832
1833 unlock_user_struct(frame, frame_addr, 1);
1834 return;
1835 sigsegv:
1836 force_sigsegv(usig);
1837 }
1838
1839 static void setup_frame(int usig, struct target_sigaction *ka,
1840 target_sigset_t *set, CPUARMState *regs)
1841 {
1842 if (get_osversion() >= 0x020612) {
1843 setup_frame_v2(usig, ka, set, regs);
1844 } else {
1845 setup_frame_v1(usig, ka, set, regs);
1846 }
1847 }
1848
1849 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1850 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1851 target_siginfo_t *info,
1852 target_sigset_t *set, CPUARMState *env)
1853 {
1854 struct rt_sigframe_v1 *frame;
1855 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1856 struct target_sigaltstack stack;
1857 int i;
1858 abi_ulong info_addr, uc_addr;
1859
1860 trace_user_setup_rt_frame(env, frame_addr);
1861 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1862 goto sigsegv;
1863 }
1864
1865 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1866 __put_user(info_addr, &frame->pinfo);
1867 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1868 __put_user(uc_addr, &frame->puc);
1869 tswap_siginfo(&frame->info, info);
1870
1871 /* Clear all the bits of the ucontext we don't use. */
1872 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1873
1874 memset(&stack, 0, sizeof(stack));
1875 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1876 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1877 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1878 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1879
1880 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1881 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1882 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
1883 }
1884
1885 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1886 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1887
1888 env->regs[1] = info_addr;
1889 env->regs[2] = uc_addr;
1890
1891 unlock_user_struct(frame, frame_addr, 1);
1892 return;
1893 sigsegv:
1894 force_sigsegv(usig);
1895 }
1896
1897 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1898 target_siginfo_t *info,
1899 target_sigset_t *set, CPUARMState *env)
1900 {
1901 struct rt_sigframe_v2 *frame;
1902 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1903 abi_ulong info_addr, uc_addr;
1904
1905 trace_user_setup_rt_frame(env, frame_addr);
1906 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1907 goto sigsegv;
1908 }
1909
1910 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1911 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1912 tswap_siginfo(&frame->info, info);
1913
1914 setup_sigframe_v2(&frame->uc, set, env);
1915
1916 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1917 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1918
1919 env->regs[1] = info_addr;
1920 env->regs[2] = uc_addr;
1921
1922 unlock_user_struct(frame, frame_addr, 1);
1923 return;
1924 sigsegv:
1925 force_sigsegv(usig);
1926 }
1927
1928 static void setup_rt_frame(int usig, struct target_sigaction *ka,
1929 target_siginfo_t *info,
1930 target_sigset_t *set, CPUARMState *env)
1931 {
1932 if (get_osversion() >= 0x020612) {
1933 setup_rt_frame_v2(usig, ka, info, set, env);
1934 } else {
1935 setup_rt_frame_v1(usig, ka, info, set, env);
1936 }
1937 }
1938
1939 static int
1940 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
1941 {
1942 int err = 0;
1943 uint32_t cpsr;
1944
1945 __get_user(env->regs[0], &sc->arm_r0);
1946 __get_user(env->regs[1], &sc->arm_r1);
1947 __get_user(env->regs[2], &sc->arm_r2);
1948 __get_user(env->regs[3], &sc->arm_r3);
1949 __get_user(env->regs[4], &sc->arm_r4);
1950 __get_user(env->regs[5], &sc->arm_r5);
1951 __get_user(env->regs[6], &sc->arm_r6);
1952 __get_user(env->regs[7], &sc->arm_r7);
1953 __get_user(env->regs[8], &sc->arm_r8);
1954 __get_user(env->regs[9], &sc->arm_r9);
1955 __get_user(env->regs[10], &sc->arm_r10);
1956 __get_user(env->regs[11], &sc->arm_fp);
1957 __get_user(env->regs[12], &sc->arm_ip);
1958 __get_user(env->regs[13], &sc->arm_sp);
1959 __get_user(env->regs[14], &sc->arm_lr);
1960 __get_user(env->regs[15], &sc->arm_pc);
1961 #ifdef TARGET_CONFIG_CPU_32
1962 __get_user(cpsr, &sc->arm_cpsr);
1963 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC, CPSRWriteByInstr);
1964 #endif
1965
1966 err |= !valid_user_regs(env);
1967
1968 return err;
1969 }
1970
1971 static long do_sigreturn_v1(CPUARMState *env)
1972 {
1973 abi_ulong frame_addr;
1974 struct sigframe_v1 *frame = NULL;
1975 target_sigset_t set;
1976 sigset_t host_set;
1977 int i;
1978
1979 /*
1980 * Since we stacked the signal on a 64-bit boundary,
1981 * then 'sp' should be word aligned here. If it's
1982 * not, then the user is trying to mess with us.
1983 */
1984 frame_addr = env->regs[13];
1985 trace_user_do_sigreturn(env, frame_addr);
1986 if (frame_addr & 7) {
1987 goto badframe;
1988 }
1989
1990 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1991 goto badframe;
1992 }
1993
1994 __get_user(set.sig[0], &frame->sc.oldmask);
1995 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1996 __get_user(set.sig[i], &frame->extramask[i - 1]);
1997 }
1998
1999 target_to_host_sigset_internal(&host_set, &set);
2000 set_sigmask(&host_set);
2001
2002 if (restore_sigcontext(env, &frame->sc)) {
2003 goto badframe;
2004 }
2005
2006 #if 0
2007 /* Send SIGTRAP if we're single-stepping */
2008 if (ptrace_cancel_bpt(current))
2009 send_sig(SIGTRAP, current, 1);
2010 #endif
2011 unlock_user_struct(frame, frame_addr, 0);
2012 return -TARGET_QEMU_ESIGRETURN;
2013
2014 badframe:
2015 force_sig(TARGET_SIGSEGV);
2016 return -TARGET_QEMU_ESIGRETURN;
2017 }
2018
2019 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
2020 {
2021 int i;
2022 abi_ulong magic, sz;
2023 uint32_t fpscr, fpexc;
2024 struct target_vfp_sigframe *vfpframe;
2025 vfpframe = (struct target_vfp_sigframe *)regspace;
2026
2027 __get_user(magic, &vfpframe->magic);
2028 __get_user(sz, &vfpframe->size);
2029 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
2030 return 0;
2031 }
2032 for (i = 0; i < 32; i++) {
2033 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
2034 }
2035 __get_user(fpscr, &vfpframe->ufp.fpscr);
2036 vfp_set_fpscr(env, fpscr);
2037 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
2038 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
2039 * and the exception flag is cleared
2040 */
2041 fpexc |= (1 << 30);
2042 fpexc &= ~((1 << 31) | (1 << 28));
2043 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
2044 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
2045 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
2046 return (abi_ulong*)(vfpframe + 1);
2047 }
2048
2049 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
2050 abi_ulong *regspace)
2051 {
2052 int i;
2053 abi_ulong magic, sz;
2054 struct target_iwmmxt_sigframe *iwmmxtframe;
2055 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
2056
2057 __get_user(magic, &iwmmxtframe->magic);
2058 __get_user(sz, &iwmmxtframe->size);
2059 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
2060 return 0;
2061 }
2062 for (i = 0; i < 16; i++) {
2063 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
2064 }
2065 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
2066 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
2067 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
2068 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
2069 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
2070 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
2071 return (abi_ulong*)(iwmmxtframe + 1);
2072 }
2073
2074 static int do_sigframe_return_v2(CPUARMState *env,
2075 target_ulong context_addr,
2076 struct target_ucontext_v2 *uc)
2077 {
2078 sigset_t host_set;
2079 abi_ulong *regspace;
2080
2081 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
2082 set_sigmask(&host_set);
2083
2084 if (restore_sigcontext(env, &uc->tuc_mcontext))
2085 return 1;
2086
2087 /* Restore coprocessor signal frame */
2088 regspace = uc->tuc_regspace;
2089 if (arm_feature(env, ARM_FEATURE_VFP)) {
2090 regspace = restore_sigframe_v2_vfp(env, regspace);
2091 if (!regspace) {
2092 return 1;
2093 }
2094 }
2095 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2096 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
2097 if (!regspace) {
2098 return 1;
2099 }
2100 }
2101
2102 if (do_sigaltstack(context_addr
2103 + offsetof(struct target_ucontext_v2, tuc_stack),
2104 0, get_sp_from_cpustate(env)) == -EFAULT) {
2105 return 1;
2106 }
2107
2108 #if 0
2109 /* Send SIGTRAP if we're single-stepping */
2110 if (ptrace_cancel_bpt(current))
2111 send_sig(SIGTRAP, current, 1);
2112 #endif
2113
2114 return 0;
2115 }
2116
2117 static long do_sigreturn_v2(CPUARMState *env)
2118 {
2119 abi_ulong frame_addr;
2120 struct sigframe_v2 *frame = NULL;
2121
2122 /*
2123 * Since we stacked the signal on a 64-bit boundary,
2124 * then 'sp' should be word aligned here. If it's
2125 * not, then the user is trying to mess with us.
2126 */
2127 frame_addr = env->regs[13];
2128 trace_user_do_sigreturn(env, frame_addr);
2129 if (frame_addr & 7) {
2130 goto badframe;
2131 }
2132
2133 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2134 goto badframe;
2135 }
2136
2137 if (do_sigframe_return_v2(env,
2138 frame_addr
2139 + offsetof(struct sigframe_v2, uc),
2140 &frame->uc)) {
2141 goto badframe;
2142 }
2143
2144 unlock_user_struct(frame, frame_addr, 0);
2145 return -TARGET_QEMU_ESIGRETURN;
2146
2147 badframe:
2148 unlock_user_struct(frame, frame_addr, 0);
2149 force_sig(TARGET_SIGSEGV);
2150 return -TARGET_QEMU_ESIGRETURN;
2151 }
2152
2153 long do_sigreturn(CPUARMState *env)
2154 {
2155 if (get_osversion() >= 0x020612) {
2156 return do_sigreturn_v2(env);
2157 } else {
2158 return do_sigreturn_v1(env);
2159 }
2160 }
2161
2162 static long do_rt_sigreturn_v1(CPUARMState *env)
2163 {
2164 abi_ulong frame_addr;
2165 struct rt_sigframe_v1 *frame = NULL;
2166 sigset_t host_set;
2167
2168 /*
2169 * Since we stacked the signal on a 64-bit boundary,
2170 * then 'sp' should be word aligned here. If it's
2171 * not, then the user is trying to mess with us.
2172 */
2173 frame_addr = env->regs[13];
2174 trace_user_do_rt_sigreturn(env, frame_addr);
2175 if (frame_addr & 7) {
2176 goto badframe;
2177 }
2178
2179 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2180 goto badframe;
2181 }
2182
2183 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
2184 set_sigmask(&host_set);
2185
2186 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
2187 goto badframe;
2188 }
2189
2190 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2191 goto badframe;
2192
2193 #if 0
2194 /* Send SIGTRAP if we're single-stepping */
2195 if (ptrace_cancel_bpt(current))
2196 send_sig(SIGTRAP, current, 1);
2197 #endif
2198 unlock_user_struct(frame, frame_addr, 0);
2199 return -TARGET_QEMU_ESIGRETURN;
2200
2201 badframe:
2202 unlock_user_struct(frame, frame_addr, 0);
2203 force_sig(TARGET_SIGSEGV);
2204 return -TARGET_QEMU_ESIGRETURN;
2205 }
2206
2207 static long do_rt_sigreturn_v2(CPUARMState *env)
2208 {
2209 abi_ulong frame_addr;
2210 struct rt_sigframe_v2 *frame = NULL;
2211
2212 /*
2213 * Since we stacked the signal on a 64-bit boundary,
2214 * then 'sp' should be word aligned here. If it's
2215 * not, then the user is trying to mess with us.
2216 */
2217 frame_addr = env->regs[13];
2218 trace_user_do_rt_sigreturn(env, frame_addr);
2219 if (frame_addr & 7) {
2220 goto badframe;
2221 }
2222
2223 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
2224 goto badframe;
2225 }
2226
2227 if (do_sigframe_return_v2(env,
2228 frame_addr
2229 + offsetof(struct rt_sigframe_v2, uc),
2230 &frame->uc)) {
2231 goto badframe;
2232 }
2233
2234 unlock_user_struct(frame, frame_addr, 0);
2235 return -TARGET_QEMU_ESIGRETURN;
2236
2237 badframe:
2238 unlock_user_struct(frame, frame_addr, 0);
2239 force_sig(TARGET_SIGSEGV);
2240 return -TARGET_QEMU_ESIGRETURN;
2241 }
2242
2243 long do_rt_sigreturn(CPUARMState *env)
2244 {
2245 if (get_osversion() >= 0x020612) {
2246 return do_rt_sigreturn_v2(env);
2247 } else {
2248 return do_rt_sigreturn_v1(env);
2249 }
2250 }
2251
2252 #elif defined(TARGET_SPARC)
2253
2254 #define __SUNOS_MAXWIN 31
2255
2256 /* This is what SunOS does, so shall I. */
2257 struct target_sigcontext {
2258 abi_ulong sigc_onstack; /* state to restore */
2259
2260 abi_ulong sigc_mask; /* sigmask to restore */
2261 abi_ulong sigc_sp; /* stack pointer */
2262 abi_ulong sigc_pc; /* program counter */
2263 abi_ulong sigc_npc; /* next program counter */
2264 abi_ulong sigc_psr; /* for condition codes etc */
2265 abi_ulong sigc_g1; /* User uses these two registers */
2266 abi_ulong sigc_o0; /* within the trampoline code. */
2267
2268 /* Now comes information regarding the users window set
2269 * at the time of the signal.
2270 */
2271 abi_ulong sigc_oswins; /* outstanding windows */
2272
2273 /* stack ptrs for each regwin buf */
2274 char *sigc_spbuf[__SUNOS_MAXWIN];
2275
2276 /* Windows to restore after signal */
2277 struct {
2278 abi_ulong locals[8];
2279 abi_ulong ins[8];
2280 } sigc_wbuf[__SUNOS_MAXWIN];
2281 };
2282 /* A Sparc stack frame */
2283 struct sparc_stackf {
2284 abi_ulong locals[8];
2285 abi_ulong ins[8];
2286 /* It's simpler to treat fp and callers_pc as elements of ins[]
2287 * since we never need to access them ourselves.
2288 */
2289 char *structptr;
2290 abi_ulong xargs[6];
2291 abi_ulong xxargs[1];
2292 };
2293
2294 typedef struct {
2295 struct {
2296 abi_ulong psr;
2297 abi_ulong pc;
2298 abi_ulong npc;
2299 abi_ulong y;
2300 abi_ulong u_regs[16]; /* globals and ins */
2301 } si_regs;
2302 int si_mask;
2303 } __siginfo_t;
2304
2305 typedef struct {
2306 abi_ulong si_float_regs[32];
2307 unsigned long si_fsr;
2308 unsigned long si_fpqdepth;
2309 struct {
2310 unsigned long *insn_addr;
2311 unsigned long insn;
2312 } si_fpqueue [16];
2313 } qemu_siginfo_fpu_t;
2314
2315
2316 struct target_signal_frame {
2317 struct sparc_stackf ss;
2318 __siginfo_t info;
2319 abi_ulong fpu_save;
2320 abi_ulong insns[2] __attribute__ ((aligned (8)));
2321 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2322 abi_ulong extra_size; /* Should be 0 */
2323 qemu_siginfo_fpu_t fpu_state;
2324 };
2325 struct target_rt_signal_frame {
2326 struct sparc_stackf ss;
2327 siginfo_t info;
2328 abi_ulong regs[20];
2329 sigset_t mask;
2330 abi_ulong fpu_save;
2331 unsigned int insns[2];
2332 stack_t stack;
2333 unsigned int extra_size; /* Should be 0 */
2334 qemu_siginfo_fpu_t fpu_state;
2335 };
2336
2337 #define UREG_O0 16
2338 #define UREG_O6 22
2339 #define UREG_I0 0
2340 #define UREG_I1 1
2341 #define UREG_I2 2
2342 #define UREG_I3 3
2343 #define UREG_I4 4
2344 #define UREG_I5 5
2345 #define UREG_I6 6
2346 #define UREG_I7 7
2347 #define UREG_L0 8
2348 #define UREG_FP UREG_I6
2349 #define UREG_SP UREG_O6
2350
2351 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
2352 CPUSPARCState *env,
2353 unsigned long framesize)
2354 {
2355 abi_ulong sp;
2356
2357 sp = env->regwptr[UREG_FP];
2358
2359 /* This is the X/Open sanctioned signal stack switching. */
2360 if (sa->sa_flags & TARGET_SA_ONSTACK) {
2361 if (!on_sig_stack(sp)
2362 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7)) {
2363 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2364 }
2365 }
2366 return sp - framesize;
2367 }
2368
2369 static int
2370 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
2371 {
2372 int err = 0, i;
2373
2374 __put_user(env->psr, &si->si_regs.psr);
2375 __put_user(env->pc, &si->si_regs.pc);
2376 __put_user(env->npc, &si->si_regs.npc);
2377 __put_user(env->y, &si->si_regs.y);
2378 for (i=0; i < 8; i++) {
2379 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
2380 }
2381 for (i=0; i < 8; i++) {
2382 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
2383 }
2384 __put_user(mask, &si->si_mask);
2385 return err;
2386 }
2387
2388 #if 0
2389 static int
2390 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
2391 CPUSPARCState *env, unsigned long mask)
2392 {
2393 int err = 0;
2394
2395 __put_user(mask, &sc->sigc_mask);
2396 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2397 __put_user(env->pc, &sc->sigc_pc);
2398 __put_user(env->npc, &sc->sigc_npc);
2399 __put_user(env->psr, &sc->sigc_psr);
2400 __put_user(env->gregs[1], &sc->sigc_g1);
2401 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
2402
2403 return err;
2404 }
2405 #endif
2406 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2407
2408 static void setup_frame(int sig, struct target_sigaction *ka,
2409 target_sigset_t *set, CPUSPARCState *env)
2410 {
2411 abi_ulong sf_addr;
2412 struct target_signal_frame *sf;
2413 int sigframe_size, err, i;
2414
2415 /* 1. Make sure everything is clean */
2416 //synchronize_user_stack();
2417
2418 sigframe_size = NF_ALIGNEDSZ;
2419 sf_addr = get_sigframe(ka, env, sigframe_size);
2420 trace_user_setup_frame(env, sf_addr);
2421
2422 sf = lock_user(VERIFY_WRITE, sf_addr,
2423 sizeof(struct target_signal_frame), 0);
2424 if (!sf) {
2425 goto sigsegv;
2426 }
2427 #if 0
2428 if (invalid_frame_pointer(sf, sigframe_size))
2429 goto sigill_and_return;
2430 #endif
2431 /* 2. Save the current process state */
2432 err = setup___siginfo(&sf->info, env, set->sig[0]);
2433 __put_user(0, &sf->extra_size);
2434
2435 //save_fpu_state(regs, &sf->fpu_state);
2436 //__put_user(&sf->fpu_state, &sf->fpu_save);
2437
2438 __put_user(set->sig[0], &sf->info.si_mask);
2439 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2440 __put_user(set->sig[i + 1], &sf->extramask[i]);
2441 }
2442
2443 for (i = 0; i < 8; i++) {
2444 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
2445 }
2446 for (i = 0; i < 8; i++) {
2447 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
2448 }
2449 if (err)
2450 goto sigsegv;
2451
2452 /* 3. signal handler back-trampoline and parameters */
2453 env->regwptr[UREG_FP] = sf_addr;
2454 env->regwptr[UREG_I0] = sig;
2455 env->regwptr[UREG_I1] = sf_addr +
2456 offsetof(struct target_signal_frame, info);
2457 env->regwptr[UREG_I2] = sf_addr +
2458 offsetof(struct target_signal_frame, info);
2459
2460 /* 4. signal handler */
2461 env->pc = ka->_sa_handler;
2462 env->npc = (env->pc + 4);
2463 /* 5. return to kernel instructions */
2464 if (ka->sa_restorer) {
2465 env->regwptr[UREG_I7] = ka->sa_restorer;
2466 } else {
2467 uint32_t val32;
2468
2469 env->regwptr[UREG_I7] = sf_addr +
2470 offsetof(struct target_signal_frame, insns) - 2 * 4;
2471
2472 /* mov __NR_sigreturn, %g1 */
2473 val32 = 0x821020d8;
2474 __put_user(val32, &sf->insns[0]);
2475
2476 /* t 0x10 */
2477 val32 = 0x91d02010;
2478 __put_user(val32, &sf->insns[1]);
2479 if (err)
2480 goto sigsegv;
2481
2482 /* Flush instruction space. */
2483 // flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2484 // tb_flush(env);
2485 }
2486 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2487 return;
2488 #if 0
2489 sigill_and_return:
2490 force_sig(TARGET_SIGILL);
2491 #endif
2492 sigsegv:
2493 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2494 force_sigsegv(sig);
2495 }
2496
2497 static void setup_rt_frame(int sig, struct target_sigaction *ka,
2498 target_siginfo_t *info,
2499 target_sigset_t *set, CPUSPARCState *env)
2500 {
2501 fprintf(stderr, "setup_rt_frame: not implemented\n");
2502 }
2503
2504 long do_sigreturn(CPUSPARCState *env)
2505 {
2506 abi_ulong sf_addr;
2507 struct target_signal_frame *sf;
2508 uint32_t up_psr, pc, npc;
2509 target_sigset_t set;
2510 sigset_t host_set;
2511 int err=0, i;
2512
2513 sf_addr = env->regwptr[UREG_FP];
2514 trace_user_do_sigreturn(env, sf_addr);
2515 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
2516 goto segv_and_exit;
2517 }
2518
2519 /* 1. Make sure we are not getting garbage from the user */
2520
2521 if (sf_addr & 3)
2522 goto segv_and_exit;
2523
2524 __get_user(pc, &sf->info.si_regs.pc);
2525 __get_user(npc, &sf->info.si_regs.npc);
2526
2527 if ((pc | npc) & 3) {
2528 goto segv_and_exit;
2529 }
2530
2531 /* 2. Restore the state */
2532 __get_user(up_psr, &sf->info.si_regs.psr);
2533
2534 /* User can only change condition codes and FPU enabling in %psr. */
2535 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2536 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2537
2538 env->pc = pc;
2539 env->npc = npc;
2540 __get_user(env->y, &sf->info.si_regs.y);
2541 for (i=0; i < 8; i++) {
2542 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2543 }
2544 for (i=0; i < 8; i++) {
2545 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2546 }
2547
2548 /* FIXME: implement FPU save/restore:
2549 * __get_user(fpu_save, &sf->fpu_save);
2550 * if (fpu_save)
2551 * err |= restore_fpu_state(env, fpu_save);
2552 */
2553
2554 /* This is pretty much atomic, no amount locking would prevent
2555 * the races which exist anyways.
2556 */
2557 __get_user(set.sig[0], &sf->info.si_mask);
2558 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2559 __get_user(set.sig[i], &sf->extramask[i - 1]);
2560 }
2561
2562 target_to_host_sigset_internal(&host_set, &set);
2563 set_sigmask(&host_set);
2564
2565 if (err) {
2566 goto segv_and_exit;
2567 }
2568 unlock_user_struct(sf, sf_addr, 0);
2569 return -TARGET_QEMU_ESIGRETURN;
2570
2571 segv_and_exit:
2572 unlock_user_struct(sf, sf_addr, 0);
2573 force_sig(TARGET_SIGSEGV);
2574 return -TARGET_QEMU_ESIGRETURN;
2575 }
2576
2577 long do_rt_sigreturn(CPUSPARCState *env)
2578 {
2579 trace_user_do_rt_sigreturn(env, 0);
2580 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2581 return -TARGET_ENOSYS;
2582 }
2583
2584 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2585 #define MC_TSTATE 0
2586 #define MC_PC 1
2587 #define MC_NPC 2
2588 #define MC_Y 3
2589 #define MC_G1 4
2590 #define MC_G2 5
2591 #define MC_G3 6
2592 #define MC_G4 7
2593 #define MC_G5 8
2594 #define MC_G6 9
2595 #define MC_G7 10
2596 #define MC_O0 11
2597 #define MC_O1 12
2598 #define MC_O2 13
2599 #define MC_O3 14
2600 #define MC_O4 15
2601 #define MC_O5 16
2602 #define MC_O6 17
2603 #define MC_O7 18
2604 #define MC_NGREG 19
2605
2606 typedef abi_ulong target_mc_greg_t;
2607 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2608
2609 struct target_mc_fq {
2610 abi_ulong *mcfq_addr;
2611 uint32_t mcfq_insn;
2612 };
2613
2614 struct target_mc_fpu {
2615 union {
2616 uint32_t sregs[32];
2617 uint64_t dregs[32];
2618 //uint128_t qregs[16];
2619 } mcfpu_fregs;
2620 abi_ulong mcfpu_fsr;
2621 abi_ulong mcfpu_fprs;
2622 abi_ulong mcfpu_gsr;
2623 struct target_mc_fq *mcfpu_fq;
2624 unsigned char mcfpu_qcnt;
2625 unsigned char mcfpu_qentsz;
2626 unsigned char mcfpu_enab;
2627 };
2628 typedef struct target_mc_fpu target_mc_fpu_t;
2629
2630 typedef struct {
2631 target_mc_gregset_t mc_gregs;
2632 target_mc_greg_t mc_fp;
2633 target_mc_greg_t mc_i7;
2634 target_mc_fpu_t mc_fpregs;
2635 } target_mcontext_t;
2636
2637 struct target_ucontext {
2638 struct target_ucontext *tuc_link;
2639 abi_ulong tuc_flags;
2640 target_sigset_t tuc_sigmask;
2641 target_mcontext_t tuc_mcontext;
2642 };
2643
2644 /* A V9 register window */
2645 struct target_reg_window {
2646 abi_ulong locals[8];
2647 abi_ulong ins[8];
2648 };
2649
2650 #define TARGET_STACK_BIAS 2047
2651
2652 /* {set, get}context() needed for 64-bit SparcLinux userland. */
2653 void sparc64_set_context(CPUSPARCState *env)
2654 {
2655 abi_ulong ucp_addr;
2656 struct target_ucontext *ucp;
2657 target_mc_gregset_t *grp;
2658 abi_ulong pc, npc, tstate;
2659 abi_ulong fp, i7, w_addr;
2660 unsigned int i;
2661
2662 ucp_addr = env->regwptr[UREG_I0];
2663 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
2664 goto do_sigsegv;
2665 }
2666 grp = &ucp->tuc_mcontext.mc_gregs;
2667 __get_user(pc, &((*grp)[MC_PC]));
2668 __get_user(npc, &((*grp)[MC_NPC]));
2669 if ((pc | npc) & 3) {
2670 goto do_sigsegv;
2671 }
2672 if (env->regwptr[UREG_I1]) {
2673 target_sigset_t target_set;
2674 sigset_t set;
2675
2676 if (TARGET_NSIG_WORDS == 1) {
2677 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
2678 } else {
2679 abi_ulong *src, *dst;
2680 src = ucp->tuc_sigmask.sig;
2681 dst = target_set.sig;
2682 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2683 __get_user(*dst, src);
2684 }
2685 }
2686 target_to_host_sigset_internal(&set, &target_set);
2687 set_sigmask(&set);
2688 }
2689 env->pc = pc;
2690 env->npc = npc;
2691 __get_user(env->y, &((*grp)[MC_Y]));
2692 __get_user(tstate, &((*grp)[MC_TSTATE]));
2693 env->asi = (tstate >> 24) & 0xff;
2694 cpu_put_ccr(env, tstate >> 32);
2695 cpu_put_cwp64(env, tstate & 0x1f);
2696 __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2697 __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2698 __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2699 __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2700 __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2701 __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2702 __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2703 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2704 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2705 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2706 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2707 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2708 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2709 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2710 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2711
2712 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2713 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2714
2715 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2716 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2717 abi_ulong) != 0) {
2718 goto do_sigsegv;
2719 }
2720 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2721 abi_ulong) != 0) {
2722 goto do_sigsegv;
2723 }
2724 /* FIXME this does not match how the kernel handles the FPU in
2725 * its sparc64_set_context implementation. In particular the FPU
2726 * is only restored if fenab is non-zero in:
2727 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2728 */
2729 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2730 {
2731 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2732 for (i = 0; i < 64; i++, src++) {
2733 if (i & 1) {
2734 __get_user(env->fpr[i/2].l.lower, src);
2735 } else {
2736 __get_user(env->fpr[i/2].l.upper, src);
2737 }
2738 }
2739 }
2740 __get_user(env->fsr,
2741 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2742 __get_user(env->gsr,
2743 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2744 unlock_user_struct(ucp, ucp_addr, 0);
2745 return;
2746 do_sigsegv:
2747 unlock_user_struct(ucp, ucp_addr, 0);
2748 force_sig(TARGET_SIGSEGV);
2749 }
2750
2751 void sparc64_get_context(CPUSPARCState *env)
2752 {
2753 abi_ulong ucp_addr;
2754 struct target_ucontext *ucp;
2755 target_mc_gregset_t *grp;
2756 target_mcontext_t *mcp;
2757 abi_ulong fp, i7, w_addr;
2758 int err;
2759 unsigned int i;
2760 target_sigset_t target_set;
2761 sigset_t set;
2762
2763 ucp_addr = env->regwptr[UREG_I0];
2764 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
2765 goto do_sigsegv;
2766 }
2767
2768 mcp = &ucp->tuc_mcontext;
2769 grp = &mcp->mc_gregs;
2770
2771 /* Skip over the trap instruction, first. */
2772 env->pc = env->npc;
2773 env->npc += 4;
2774
2775 /* If we're only reading the signal mask then do_sigprocmask()
2776 * is guaranteed not to fail, which is important because we don't
2777 * have any way to signal a failure or restart this operation since
2778 * this is not a normal syscall.
2779 */
2780 err = do_sigprocmask(0, NULL, &set);
2781 assert(err == 0);
2782 host_to_target_sigset_internal(&target_set, &set);
2783 if (TARGET_NSIG_WORDS == 1) {
2784 __put_user(target_set.sig[0],
2785 (abi_ulong *)&ucp->tuc_sigmask);
2786 } else {
2787 abi_ulong *src, *dst;
2788 src = target_set.sig;
2789 dst = ucp->tuc_sigmask.sig;
2790 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
2791 __put_user(*src, dst);
2792 }
2793 if (err)
2794 goto do_sigsegv;
2795 }
2796
2797 /* XXX: tstate must be saved properly */
2798 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2799 __put_user(env->pc, &((*grp)[MC_PC]));
2800 __put_user(env->npc, &((*grp)[MC_NPC]));
2801 __put_user(env->y, &((*grp)[MC_Y]));
2802 __put_user(env->gregs[1], &((*grp)[MC_G1]));
2803 __put_user(env->gregs[2], &((*grp)[MC_G2]));
2804 __put_user(env->gregs[3], &((*grp)[MC_G3]));
2805 __put_user(env->gregs[4], &((*grp)[MC_G4]));
2806 __put_user(env->gregs[5], &((*grp)[MC_G5]));
2807 __put_user(env->gregs[6], &((*grp)[MC_G6]));
2808 __put_user(env->gregs[7], &((*grp)[MC_G7]));
2809 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2810 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2811 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2812 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2813 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2814 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2815 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2816 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2817
2818 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2819 fp = i7 = 0;
2820 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2821 abi_ulong) != 0) {
2822 goto do_sigsegv;
2823 }
2824 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2825 abi_ulong) != 0) {
2826 goto do_sigsegv;
2827 }
2828 __put_user(fp, &(mcp->mc_fp));
2829 __put_user(i7, &(mcp->mc_i7));
2830
2831 {
2832 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2833 for (i = 0; i < 64; i++, dst++) {
2834 if (i & 1) {
2835 __put_user(env->fpr[i/2].l.lower, dst);
2836 } else {
2837 __put_user(env->fpr[i/2].l.upper, dst);
2838 }
2839 }
2840 }
2841 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2842 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2843 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2844
2845 if (err)
2846 goto do_sigsegv;
2847 unlock_user_struct(ucp, ucp_addr, 1);
2848 return;
2849 do_sigsegv:
2850 unlock_user_struct(ucp, ucp_addr, 1);
2851 force_sig(TARGET_SIGSEGV);
2852 }
2853 #endif
2854 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
2855
2856 # if defined(TARGET_ABI_MIPSO32)
2857 struct target_sigcontext {
2858 uint32_t sc_regmask; /* Unused */
2859 uint32_t sc_status;
2860 uint64_t sc_pc;
2861 uint64_t sc_regs[32];
2862 uint64_t sc_fpregs[32];
2863 uint32_t sc_ownedfp; /* Unused */
2864 uint32_t sc_fpc_csr;
2865 uint32_t sc_fpc_eir; /* Unused */
2866 uint32_t sc_used_math;
2867 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2868 uint32_t pad0;
2869 uint64_t sc_mdhi;
2870 uint64_t sc_mdlo;
2871 target_ulong sc_hi1; /* Was sc_cause */
2872 target_ulong sc_lo1; /* Was sc_badvaddr */
2873 target_ulong sc_hi2; /* Was sc_sigset[4] */
2874 target_ulong sc_lo2;
2875 target_ulong sc_hi3;
2876 target_ulong sc_lo3;
2877 };
2878 # else /* N32 || N64 */
2879 struct target_sigcontext {
2880 uint64_t sc_regs[32];
2881 uint64_t sc_fpregs[32];
2882 uint64_t sc_mdhi;
2883 uint64_t sc_hi1;
2884 uint64_t sc_hi2;
2885 uint64_t sc_hi3;
2886 uint64_t sc_mdlo;
2887 uint64_t sc_lo1;
2888 uint64_t sc_lo2;
2889 uint64_t sc_lo3;
2890 uint64_t sc_pc;
2891 uint32_t sc_fpc_csr;
2892 uint32_t sc_used_math;
2893 uint32_t sc_dsp;
2894 uint32_t sc_reserved;
2895 };
2896 # endif /* O32 */
2897
2898 struct sigframe {
2899 uint32_t sf_ass[4]; /* argument save space for o32 */
2900 uint32_t sf_code[2]; /* signal trampoline */
2901 struct target_sigcontext sf_sc;
2902 target_sigset_t sf_mask;
2903 };
2904
2905 struct target_ucontext {
2906 target_ulong tuc_flags;
2907 target_ulong tuc_link;
2908 target_stack_t tuc_stack;
2909 target_ulong pad0;
2910 struct target_sigcontext tuc_mcontext;
2911 target_sigset_t tuc_sigmask;
2912 };
2913
2914 struct target_rt_sigframe {
2915 uint32_t rs_ass[4]; /* argument save space for o32 */
2916 uint32_t rs_code[2]; /* signal trampoline */
2917 struct target_siginfo rs_info;
2918 struct target_ucontext rs_uc;
2919 };
2920
2921 /* Install trampoline to jump back from signal handler */
2922 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2923 {
2924 int err = 0;
2925
2926 /*
2927 * Set up the return code ...
2928 *
2929 * li v0, __NR__foo_sigreturn
2930 * syscall
2931 */
2932
2933 __put_user(0x24020000 + syscall, tramp + 0);
2934 __put_user(0x0000000c , tramp + 1);
2935 return err;
2936 }
2937
2938 static inline void setup_sigcontext(CPUMIPSState *regs,
2939 struct target_sigcontext *sc)
2940 {
2941 int i;
2942
2943 __put_user(exception_resume_pc(regs), &sc->sc_pc);
2944 regs->hflags &= ~MIPS_HFLAG_BMASK;
2945
2946 __put_user(0, &sc->sc_regs[0]);
2947 for (i = 1; i < 32; ++i) {
2948 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2949 }
2950
2951 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2952 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2953
2954 /* Rather than checking for dsp existence, always copy. The storage
2955 would just be garbage otherwise. */
2956 __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2957 __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2958 __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2959 __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2960 __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2961 __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
2962 {
2963 uint32_t dsp = cpu_rddsp(0x3ff, regs);
2964 __put_user(dsp, &sc->sc_dsp);
2965 }
2966
2967 __put_user(1, &sc->sc_used_math);
2968
2969 for (i = 0; i < 32; ++i) {
2970 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
2971 }
2972 }
2973
2974 static inline void
2975 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
2976 {
2977 int i;
2978
2979 __get_user(regs->CP0_EPC, &sc->sc_pc);
2980
2981 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2982 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2983
2984 for (i = 1; i < 32; ++i) {
2985 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
2986 }
2987
2988 __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2989 __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2990 __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2991 __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2992 __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2993 __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
2994 {
2995 uint32_t dsp;
2996 __get_user(dsp, &sc->sc_dsp);
2997 cpu_wrdsp(dsp, 0x3ff, regs);
2998 }
2999
3000 for (i = 0; i < 32; ++i) {
3001 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
3002 }
3003 }
3004
3005 /*
3006 * Determine which stack to use..
3007 */
3008 static inline abi_ulong
3009 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
3010 {
3011 unsigned long sp;
3012
3013 /* Default to using normal stack */
3014 sp = regs->active_tc.gpr[29];
3015
3016 /*
3017 * FPU emulator may have its own trampoline active just
3018 * above the user stack, 16-bytes before the next lowest
3019 * 16 byte boundary. Try to avoid trashing it.
3020 */
3021 sp -= 32;
3022
3023 /* This is the X/Open sanctioned signal stack switching. */
3024 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
3025 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3026 }
3027
3028 return (sp - frame_size) & ~7;
3029 }
3030
3031 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
3032 {
3033 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
3034 env->hflags &= ~MIPS_HFLAG_M16;
3035 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
3036 env->active_tc.PC &= ~(target_ulong) 1;
3037 }
3038 }
3039
3040 # if defined(TARGET_ABI_MIPSO32)
3041 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
3042 static void setup_frame(int sig, struct target_sigaction * ka,
3043 target_sigset_t *set, CPUMIPSState *regs)
3044 {
3045 struct sigframe *frame;
3046 abi_ulong frame_addr;
3047 int i;
3048
3049 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
3050 trace_user_setup_frame(regs, frame_addr);
3051 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3052 goto give_sigsegv;
3053 }
3054
3055 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
3056
3057 setup_sigcontext(regs, &frame->sf_sc);
3058
3059 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3060 __put_user(set->sig[i], &frame->sf_mask.sig[i]);
3061 }
3062
3063 /*
3064 * Arguments to signal handler:
3065 *
3066 * a0 = signal number
3067 * a1 = 0 (should be cause)
3068 * a2 = pointer to struct sigcontext
3069 *
3070 * $25 and PC point to the signal handler, $29 points to the
3071 * struct sigframe.
3072 */
3073 regs->active_tc.gpr[ 4] = sig;
3074 regs->active_tc.gpr[ 5] = 0;
3075 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
3076 regs->active_tc.gpr[29] = frame_addr;
3077 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
3078 /* The original kernel code sets CP0_EPC to the handler
3079 * since it returns to userland using eret
3080 * we cannot do this here, and we must set PC directly */
3081 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
3082 mips_set_hflags_isa_mode_from_pc(regs);
3083 unlock_user_struct(frame, frame_addr, 1);
3084 return;
3085
3086 give_sigsegv:
3087 force_sigsegv(sig);
3088 }
3089
3090 long do_sigreturn(CPUMIPSState *regs)
3091 {
3092 struct sigframe *frame;
3093 abi_ulong frame_addr;
3094 sigset_t blocked;
3095 target_sigset_t target_set;
3096 int i;
3097
3098 frame_addr = regs->active_tc.gpr[29];
3099 trace_user_do_sigreturn(regs, frame_addr);
3100 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3101 goto badframe;
3102
3103 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3104 __get_user(target_set.sig[i], &frame->sf_mask.sig[i]);
3105 }
3106
3107 target_to_host_sigset_internal(&blocked, &target_set);
3108 set_sigmask(&blocked);
3109
3110 restore_sigcontext(regs, &frame->sf_sc);
3111
3112 #if 0
3113 /*
3114 * Don't let your children do this ...
3115 */
3116 __asm__ __volatile__(
3117 "move\t$29, %0\n\t"
3118 "j\tsyscall_exit"
3119 :/* no outputs */
3120 :"r" (&regs));
3121 /* Unreached */
3122 #endif
3123
3124 regs->active_tc.PC = regs->CP0_EPC;
3125 mips_set_hflags_isa_mode_from_pc(regs);
3126 /* I am not sure this is right, but it seems to work
3127 * maybe a problem with nested signals ? */
3128 regs->CP0_EPC = 0;
3129 return -TARGET_QEMU_ESIGRETURN;
3130
3131 badframe:
3132 force_sig(TARGET_SIGSEGV);
3133 return -TARGET_QEMU_ESIGRETURN;
3134 }
3135 # endif /* O32 */
3136
3137 static void setup_rt_frame(int sig, struct target_sigaction *ka,
3138 target_siginfo_t *info,
3139 target_sigset_t *set, CPUMIPSState *env)
3140 {
3141 struct target_rt_sigframe *frame;
3142 abi_ulong frame_addr;
3143 int i;
3144
3145 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3146 trace_user_setup_rt_frame(env, frame_addr);
3147 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3148 goto give_sigsegv;
3149 }
3150
3151 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3152
3153 tswap_siginfo(&frame->rs_info, info);
3154
3155 __put_user(0, &frame->rs_uc.tuc_flags);
3156 __put_user(0, &frame->rs_uc.tuc_link);
3157 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3158 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
3159 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3160 &frame->rs_uc.tuc_stack.ss_flags);
3161
3162 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3163
3164 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3165 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
3166 }
3167
3168 /*
3169 * Arguments to signal handler:
3170 *
3171 * a0 = signal number
3172 * a1 = pointer to siginfo_t
3173 * a2 = pointer to struct ucontext
3174 *
3175 * $25 and PC point to the signal handler, $29 points to the
3176 * struct sigframe.
3177 */
3178 env->active_tc.gpr[ 4] = sig;
3179 env->active_tc.gpr[ 5] = frame_addr
3180 + offsetof(struct target_rt_sigframe, rs_info);
3181 env->active_tc.gpr[ 6] = frame_addr
3182 + offsetof(struct target_rt_sigframe, rs_uc);
3183 env->active_tc.gpr[29] = frame_addr;
3184 env->active_tc.gpr[31] = frame_addr
3185 + offsetof(struct target_rt_sigframe, rs_code);
3186 /* The original kernel code sets CP0_EPC to the handler
3187 * since it returns to userland using eret
3188 * we cannot do this here, and we must set PC directly */
3189 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
3190 mips_set_hflags_isa_mode_from_pc(env);
3191 unlock_user_struct(frame, frame_addr, 1);
3192 return;
3193
3194 give_sigsegv:
3195 unlock_user_struct(frame, frame_addr, 1);
3196 force_sigsegv(sig);
3197 }
3198
3199 long do_rt_sigreturn(CPUMIPSState *env)
3200 {
3201 struct target_rt_sigframe *frame;
3202 abi_ulong frame_addr;
3203 sigset_t blocked;
3204
3205 frame_addr = env->active_tc.gpr[29];
3206 trace_user_do_rt_sigreturn(env, frame_addr);
3207 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3208 goto badframe;
3209 }
3210
3211 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
3212 set_sigmask(&blocked);
3213
3214 restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
3215
3216 if (do_sigaltstack(frame_addr +
3217 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
3218 0, get_sp_from_cpustate(env)) == -EFAULT)
3219 goto badframe;
3220
3221 env->active_tc.PC = env->CP0_EPC;
3222 mips_set_hflags_isa_mode_from_pc(env);
3223 /* I am not sure this is right, but it seems to work
3224 * maybe a problem with nested signals ? */
3225 env->CP0_EPC = 0;
3226 return -TARGET_QEMU_ESIGRETURN;
3227
3228 badframe:
3229 force_sig(TARGET_SIGSEGV);
3230 return -TARGET_QEMU_ESIGRETURN;
3231 }
3232
3233 #elif defined(TARGET_SH4)
3234
3235 /*
3236 * code and data structures from linux kernel:
3237 * include/asm-sh/sigcontext.h
3238 * arch/sh/kernel/signal.c
3239 */
3240
3241 struct target_sigcontext {
3242 target_ulong oldmask;
3243
3244 /* CPU registers */
3245 target_ulong sc_gregs[16];
3246 target_ulong sc_pc;
3247 target_ulong sc_pr;
3248 target_ulong sc_sr;
3249 target_ulong sc_gbr;
3250 target_ulong sc_mach;
3251 target_ulong sc_macl;
3252
3253 /* FPU registers */
3254 target_ulong sc_fpregs[16];
3255 target_ulong sc_xfpregs[16];
3256 unsigned int sc_fpscr;
3257 unsigned int sc_fpul;
3258 unsigned int sc_ownedfp;
3259 };
3260
3261 struct target_sigframe
3262 {
3263 struct target_sigcontext sc;
3264 target_ulong extramask[TARGET_NSIG_WORDS-1];
3265 uint16_t retcode[3];
3266 };
3267
3268
3269 struct target_ucontext {
3270 target_ulong tuc_flags;
3271 struct target_ucontext *tuc_link;
3272 target_stack_t tuc_stack;
3273 struct target_sigcontext tuc_mcontext;
3274 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3275 };
3276
3277 struct target_rt_sigframe
3278 {
3279 struc