linux-user/sparc: Add rwin_save to signal frame
[qemu/ar7.git] / linux-user / sparc / signal.c
blob4a0578ebf37b2ec924e300496b2a914bdf6dd07d
1 /*
2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qemu.h"
21 #include "signal-common.h"
22 #include "linux-user/trace.h"
24 /* A Sparc register window */
25 struct target_reg_window {
26 abi_ulong locals[8];
27 abi_ulong ins[8];
30 /* A Sparc stack frame. */
31 struct target_stackf {
33 * Since qemu does not reference fp or callers_pc directly,
34 * it's simpler to treat fp and callers_pc as elements of ins[],
35 * and then bundle locals[] and ins[] into reg_window.
37 struct target_reg_window win;
39 * Similarly, bundle structptr and xxargs into xargs[].
40 * This portion of the struct is part of the function call abi,
41 * and belongs to the callee for spilling argument registers.
43 abi_ulong xargs[8];
46 struct target_siginfo_fpu {
47 /* It is more convenient for qemu to move doubles, not singles. */
48 uint64_t si_double_regs[16];
49 uint32_t si_fsr;
50 uint32_t si_fpqdepth;
51 struct {
52 uint32_t insn_addr;
53 uint32_t insn;
54 } si_fpqueue [16];
57 struct target_signal_frame {
58 struct target_stackf ss;
59 struct target_pt_regs regs;
60 uint32_t si_mask;
61 abi_ulong fpu_save;
62 uint32_t insns[2] QEMU_ALIGNED(8);
63 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
64 abi_ulong extra_size; /* Should be 0 */
65 abi_ulong rwin_save;
68 static abi_ulong get_sigframe(struct target_sigaction *sa,
69 CPUSPARCState *env,
70 size_t framesize)
72 abi_ulong sp = get_sp_from_cpustate(env);
75 * If we are on the alternate signal stack and would overflow it, don't.
76 * Return an always-bogus address instead so we will die with SIGSEGV.
78 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
79 return -1;
82 /* This is the X/Open sanctioned signal stack switching. */
83 sp = target_sigsp(sp, sa) - framesize;
86 * Always align the stack frame. This handles two cases. First,
87 * sigaltstack need not be mindful of platform specific stack
88 * alignment. Second, if we took this signal because the stack
89 * is not aligned properly, we'd like to take the signal cleanly
90 * and report that.
92 sp &= ~15UL;
94 return sp;
97 static void save_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
99 int i;
101 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
102 __put_user(sparc64_tstate(env), &regs->tstate);
103 /* TODO: magic should contain PT_REG_MAGIC + %tt. */
104 __put_user(0, &regs->magic);
105 #else
106 __put_user(cpu_get_psr(env), &regs->psr);
107 #endif
109 __put_user(env->pc, &regs->pc);
110 __put_user(env->npc, &regs->npc);
111 __put_user(env->y, &regs->y);
113 for (i = 0; i < 8; i++) {
114 __put_user(env->gregs[i], &regs->u_regs[i]);
116 for (i = 0; i < 8; i++) {
117 __put_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
121 static void restore_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
123 int i;
125 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
126 /* User can only change condition codes and %asi in %tstate. */
127 uint64_t tstate;
128 __get_user(tstate, &regs->tstate);
129 cpu_put_ccr(env, tstate >> 32);
130 env->asi = extract64(tstate, 24, 8);
131 #else
133 * User can only change condition codes and FPU enabling in %psr.
134 * But don't bother with FPU enabling, since a real kernel would
135 * just re-enable the FPU upon the next fpu trap.
137 uint32_t psr;
138 __get_user(psr, &regs->psr);
139 env->psr = (psr & PSR_ICC) | (env->psr & ~PSR_ICC);
140 #endif
142 /* Note that pc and npc are handled in the caller. */
144 __get_user(env->y, &regs->y);
146 for (i = 0; i < 8; i++) {
147 __get_user(env->gregs[i], &regs->u_regs[i]);
149 for (i = 0; i < 8; i++) {
150 __get_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
154 static void save_reg_win(struct target_reg_window *win, CPUSPARCState *env)
156 int i;
158 for (i = 0; i < 8; i++) {
159 __put_user(env->regwptr[i + WREG_L0], &win->locals[i]);
161 for (i = 0; i < 8; i++) {
162 __put_user(env->regwptr[i + WREG_I0], &win->ins[i]);
166 static void save_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
168 int i;
170 for (i = 0; i < 16; ++i) {
171 __put_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
173 __put_user(env->fsr, &fpu->si_fsr);
174 __put_user(0, &fpu->si_fpqdepth);
177 static void restore_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
179 int i;
181 for (i = 0; i < 16; ++i) {
182 __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
184 __get_user(env->fsr, &fpu->si_fsr);
187 void setup_frame(int sig, struct target_sigaction *ka,
188 target_sigset_t *set, CPUSPARCState *env)
190 abi_ulong sf_addr;
191 struct target_signal_frame *sf;
192 size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
193 int i;
195 /* 1. Make sure everything is clean */
197 sf_addr = get_sigframe(ka, env, sf_size);
198 trace_user_setup_frame(env, sf_addr);
200 sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
201 if (!sf) {
202 goto sigsegv;
205 /* 2. Save the current process state */
206 save_pt_regs(&sf->regs, env);
207 __put_user(0, &sf->extra_size);
209 save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
210 __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
212 __put_user(0, &sf->rwin_save); /* TODO: save_rwin_state */
214 __put_user(set->sig[0], &sf->si_mask);
215 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
216 __put_user(set->sig[i + 1], &sf->extramask[i]);
219 save_reg_win(&sf->ss.win, env);
221 /* 3. signal handler back-trampoline and parameters */
222 env->regwptr[WREG_SP] = sf_addr;
223 env->regwptr[WREG_O0] = sig;
224 env->regwptr[WREG_O1] = sf_addr +
225 offsetof(struct target_signal_frame, regs);
226 env->regwptr[WREG_O2] = sf_addr +
227 offsetof(struct target_signal_frame, regs);
229 /* 4. signal handler */
230 env->pc = ka->_sa_handler;
231 env->npc = (env->pc + 4);
232 /* 5. return to kernel instructions */
233 if (ka->ka_restorer) {
234 env->regwptr[WREG_O7] = ka->ka_restorer;
235 } else {
236 uint32_t val32;
238 env->regwptr[WREG_O7] = sf_addr +
239 offsetof(struct target_signal_frame, insns) - 2 * 4;
241 /* mov __NR_sigreturn, %g1 */
242 val32 = 0x821020d8;
243 __put_user(val32, &sf->insns[0]);
245 /* t 0x10 */
246 val32 = 0x91d02010;
247 __put_user(val32, &sf->insns[1]);
249 unlock_user(sf, sf_addr, sf_size);
250 return;
251 #if 0
252 sigill_and_return:
253 force_sig(TARGET_SIGILL);
254 #endif
255 sigsegv:
256 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
257 force_sigsegv(sig);
260 void setup_rt_frame(int sig, struct target_sigaction *ka,
261 target_siginfo_t *info,
262 target_sigset_t *set, CPUSPARCState *env)
264 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n");
267 long do_sigreturn(CPUSPARCState *env)
269 abi_ulong sf_addr;
270 struct target_signal_frame *sf;
271 abi_ulong pc, npc, ptr;
272 target_sigset_t set;
273 sigset_t host_set;
274 int i;
276 sf_addr = env->regwptr[WREG_SP];
277 trace_user_do_sigreturn(env, sf_addr);
278 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
279 goto segv_and_exit;
282 /* 1. Make sure we are not getting garbage from the user */
284 if (sf_addr & 3)
285 goto segv_and_exit;
287 __get_user(pc, &sf->regs.pc);
288 __get_user(npc, &sf->regs.npc);
290 if ((pc | npc) & 3) {
291 goto segv_and_exit;
294 /* 2. Restore the state */
295 restore_pt_regs(&sf->regs, env);
296 env->pc = pc;
297 env->npc = npc;
299 __get_user(ptr, &sf->fpu_save);
300 if (ptr) {
301 struct target_siginfo_fpu *fpu;
302 if ((ptr & 3) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
303 goto segv_and_exit;
305 restore_fpu(fpu, env);
306 unlock_user_struct(fpu, ptr, 0);
309 __get_user(ptr, &sf->rwin_save);
310 if (ptr) {
311 goto segv_and_exit; /* TODO: restore_rwin */
314 __get_user(set.sig[0], &sf->si_mask);
315 for (i = 1; i < TARGET_NSIG_WORDS; i++) {
316 __get_user(set.sig[i], &sf->extramask[i - 1]);
319 target_to_host_sigset_internal(&host_set, &set);
320 set_sigmask(&host_set);
322 unlock_user_struct(sf, sf_addr, 0);
323 return -TARGET_QEMU_ESIGRETURN;
325 segv_and_exit:
326 unlock_user_struct(sf, sf_addr, 0);
327 force_sig(TARGET_SIGSEGV);
328 return -TARGET_QEMU_ESIGRETURN;
331 long do_rt_sigreturn(CPUSPARCState *env)
333 trace_user_do_rt_sigreturn(env, 0);
334 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n");
335 return -TARGET_ENOSYS;
338 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
339 #define SPARC_MC_TSTATE 0
340 #define SPARC_MC_PC 1
341 #define SPARC_MC_NPC 2
342 #define SPARC_MC_Y 3
343 #define SPARC_MC_G1 4
344 #define SPARC_MC_G2 5
345 #define SPARC_MC_G3 6
346 #define SPARC_MC_G4 7
347 #define SPARC_MC_G5 8
348 #define SPARC_MC_G6 9
349 #define SPARC_MC_G7 10
350 #define SPARC_MC_O0 11
351 #define SPARC_MC_O1 12
352 #define SPARC_MC_O2 13
353 #define SPARC_MC_O3 14
354 #define SPARC_MC_O4 15
355 #define SPARC_MC_O5 16
356 #define SPARC_MC_O6 17
357 #define SPARC_MC_O7 18
358 #define SPARC_MC_NGREG 19
360 typedef abi_ulong target_mc_greg_t;
361 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
363 struct target_mc_fq {
364 abi_ulong mcfq_addr;
365 uint32_t mcfq_insn;
369 * Note the manual 16-alignment; the kernel gets this because it
370 * includes a "long double qregs[16]" in the mcpu_fregs union,
371 * which we can't do.
373 struct target_mc_fpu {
374 union {
375 uint32_t sregs[32];
376 uint64_t dregs[32];
377 //uint128_t qregs[16];
378 } mcfpu_fregs;
379 abi_ulong mcfpu_fsr;
380 abi_ulong mcfpu_fprs;
381 abi_ulong mcfpu_gsr;
382 abi_ulong mcfpu_fq;
383 unsigned char mcfpu_qcnt;
384 unsigned char mcfpu_qentsz;
385 unsigned char mcfpu_enab;
386 } __attribute__((aligned(16)));
387 typedef struct target_mc_fpu target_mc_fpu_t;
389 typedef struct {
390 target_mc_gregset_t mc_gregs;
391 target_mc_greg_t mc_fp;
392 target_mc_greg_t mc_i7;
393 target_mc_fpu_t mc_fpregs;
394 } target_mcontext_t;
396 struct target_ucontext {
397 abi_ulong tuc_link;
398 abi_ulong tuc_flags;
399 target_sigset_t tuc_sigmask;
400 target_mcontext_t tuc_mcontext;
403 /* {set, get}context() needed for 64-bit SparcLinux userland. */
404 void sparc64_set_context(CPUSPARCState *env)
406 abi_ulong ucp_addr;
407 struct target_ucontext *ucp;
408 target_mc_gregset_t *grp;
409 target_mc_fpu_t *fpup;
410 abi_ulong pc, npc, tstate;
411 unsigned int i;
412 unsigned char fenab;
414 ucp_addr = env->regwptr[WREG_O0];
415 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
416 goto do_sigsegv;
418 grp = &ucp->tuc_mcontext.mc_gregs;
419 __get_user(pc, &((*grp)[SPARC_MC_PC]));
420 __get_user(npc, &((*grp)[SPARC_MC_NPC]));
421 if ((pc | npc) & 3) {
422 goto do_sigsegv;
424 if (env->regwptr[WREG_O1]) {
425 target_sigset_t target_set;
426 sigset_t set;
428 if (TARGET_NSIG_WORDS == 1) {
429 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
430 } else {
431 abi_ulong *src, *dst;
432 src = ucp->tuc_sigmask.sig;
433 dst = target_set.sig;
434 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
435 __get_user(*dst, src);
438 target_to_host_sigset_internal(&set, &target_set);
439 set_sigmask(&set);
441 env->pc = pc;
442 env->npc = npc;
443 __get_user(env->y, &((*grp)[SPARC_MC_Y]));
444 __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
445 /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
446 env->asi = (tstate >> 24) & 0xff;
447 cpu_put_ccr(env, (tstate >> 32) & 0xff);
448 __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
449 __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
450 __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
451 __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
452 __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
453 __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
454 /* Skip g7 as that's the thread register in userspace */
457 * Note that unlike the kernel, we didn't need to mess with the
458 * guest register window state to save it into a pt_regs to run
459 * the kernel. So for us the guest's O regs are still in WREG_O*
460 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
461 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
462 * need to be written back to userspace memory.
464 __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0]));
465 __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1]));
466 __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2]));
467 __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3]));
468 __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4]));
469 __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5]));
470 __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6]));
471 __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7]));
473 __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp));
474 __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7));
476 fpup = &ucp->tuc_mcontext.mc_fpregs;
478 __get_user(fenab, &(fpup->mcfpu_enab));
479 if (fenab) {
480 abi_ulong fprs;
483 * We use the FPRS from the guest only in deciding whether
484 * to restore the upper, lower, or both banks of the FPU regs.
485 * The kernel here writes the FPU register data into the
486 * process's current_thread_info state and unconditionally
487 * clears FPRS and TSTATE_PEF: this disables the FPU so that the
488 * next FPU-disabled trap will copy the data out of
489 * current_thread_info and into the real FPU registers.
490 * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
491 * so we always load the data directly into the FPU registers
492 * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
493 * Note that because we (and the kernel) always write zeroes for
494 * the fenab and fprs in sparc64_get_context() none of this code
495 * will execute unless the guest manually constructed or changed
496 * the context structure.
498 __get_user(fprs, &(fpup->mcfpu_fprs));
499 if (fprs & FPRS_DL) {
500 for (i = 0; i < 16; i++) {
501 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
504 if (fprs & FPRS_DU) {
505 for (i = 16; i < 32; i++) {
506 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
509 __get_user(env->fsr, &(fpup->mcfpu_fsr));
510 __get_user(env->gsr, &(fpup->mcfpu_gsr));
512 unlock_user_struct(ucp, ucp_addr, 0);
513 return;
514 do_sigsegv:
515 unlock_user_struct(ucp, ucp_addr, 0);
516 force_sig(TARGET_SIGSEGV);
519 void sparc64_get_context(CPUSPARCState *env)
521 abi_ulong ucp_addr;
522 struct target_ucontext *ucp;
523 target_mc_gregset_t *grp;
524 target_mcontext_t *mcp;
525 int err;
526 unsigned int i;
527 target_sigset_t target_set;
528 sigset_t set;
530 ucp_addr = env->regwptr[WREG_O0];
531 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
532 goto do_sigsegv;
535 memset(ucp, 0, sizeof(*ucp));
537 mcp = &ucp->tuc_mcontext;
538 grp = &mcp->mc_gregs;
540 /* Skip over the trap instruction, first. */
541 env->pc = env->npc;
542 env->npc += 4;
544 /* If we're only reading the signal mask then do_sigprocmask()
545 * is guaranteed not to fail, which is important because we don't
546 * have any way to signal a failure or restart this operation since
547 * this is not a normal syscall.
549 err = do_sigprocmask(0, NULL, &set);
550 assert(err == 0);
551 host_to_target_sigset_internal(&target_set, &set);
552 if (TARGET_NSIG_WORDS == 1) {
553 __put_user(target_set.sig[0],
554 (abi_ulong *)&ucp->tuc_sigmask);
555 } else {
556 abi_ulong *src, *dst;
557 src = target_set.sig;
558 dst = ucp->tuc_sigmask.sig;
559 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
560 __put_user(*src, dst);
564 __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE]));
565 __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
566 __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
567 __put_user(env->y, &((*grp)[SPARC_MC_Y]));
568 __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
569 __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
570 __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
571 __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
572 __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
573 __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
574 __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
577 * Note that unlike the kernel, we didn't need to mess with the
578 * guest register window state to save it into a pt_regs to run
579 * the kernel. So for us the guest's O regs are still in WREG_O*
580 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
581 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
582 * need to be fished out of userspace memory.
584 __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0]));
585 __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1]));
586 __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2]));
587 __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3]));
588 __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4]));
589 __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5]));
590 __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6]));
591 __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7]));
593 __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp));
594 __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7));
597 * We don't write out the FPU state. This matches the kernel's
598 * implementation (which has the code for doing this but
599 * hidden behind an "if (fenab)" where fenab is always 0).
602 unlock_user_struct(ucp, ucp_addr, 1);
603 return;
604 do_sigsegv:
605 unlock_user_struct(ucp, ucp_addr, 1);
606 force_sig(TARGET_SIGSEGV);
608 #endif