linux-user/sparc: Use target_pt_regs
[qemu/ar7.git] / linux-user / sparc / signal.c
blob0d9305818f8c0b25919cc07cf0d44489604e0683
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 typedef struct {
47 abi_ulong si_float_regs[32];
48 unsigned long si_fsr;
49 unsigned long si_fpqdepth;
50 struct {
51 unsigned long *insn_addr;
52 unsigned long insn;
53 } si_fpqueue [16];
54 } qemu_siginfo_fpu_t;
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 qemu_siginfo_fpu_t fpu_state;
68 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
69 CPUSPARCState *env,
70 unsigned long 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;
85 /* Always align the stack frame. This handles two cases. First,
86 * sigaltstack need not be mindful of platform specific stack
87 * alignment. Second, if we took this signal because the stack
88 * is not aligned properly, we'd like to take the signal cleanly
89 * and report that.
91 sp &= ~15UL;
93 return sp;
96 static void save_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
98 int i;
100 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
101 __put_user(sparc64_tstate(env), &regs->tstate);
102 /* TODO: magic should contain PT_REG_MAGIC + %tt. */
103 __put_user(0, &regs->magic);
104 #else
105 __put_user(cpu_get_psr(env), &regs->psr);
106 #endif
108 __put_user(env->pc, &regs->pc);
109 __put_user(env->npc, &regs->npc);
110 __put_user(env->y, &regs->y);
112 for (i = 0; i < 8; i++) {
113 __put_user(env->gregs[i], &regs->u_regs[i]);
115 for (i = 0; i < 8; i++) {
116 __put_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
120 static void restore_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
122 int i;
124 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
125 /* User can only change condition codes and %asi in %tstate. */
126 uint64_t tstate;
127 __get_user(tstate, &regs->tstate);
128 cpu_put_ccr(env, tstate >> 32);
129 env->asi = extract64(tstate, 24, 8);
130 #else
132 * User can only change condition codes and FPU enabling in %psr.
133 * But don't bother with FPU enabling, since a real kernel would
134 * just re-enable the FPU upon the next fpu trap.
136 uint32_t psr;
137 __get_user(psr, &regs->psr);
138 env->psr = (psr & PSR_ICC) | (env->psr & ~PSR_ICC);
139 #endif
141 /* Note that pc and npc are handled in the caller. */
143 __get_user(env->y, &regs->y);
145 for (i = 0; i < 8; i++) {
146 __get_user(env->gregs[i], &regs->u_regs[i]);
148 for (i = 0; i < 8; i++) {
149 __get_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
153 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
155 void setup_frame(int sig, struct target_sigaction *ka,
156 target_sigset_t *set, CPUSPARCState *env)
158 abi_ulong sf_addr;
159 struct target_signal_frame *sf;
160 int sigframe_size, i;
162 /* 1. Make sure everything is clean */
163 //synchronize_user_stack();
165 sigframe_size = NF_ALIGNEDSZ;
166 sf_addr = get_sigframe(ka, env, sigframe_size);
167 trace_user_setup_frame(env, sf_addr);
169 sf = lock_user(VERIFY_WRITE, sf_addr,
170 sizeof(struct target_signal_frame), 0);
171 if (!sf) {
172 goto sigsegv;
174 /* 2. Save the current process state */
175 save_pt_regs(&sf->regs, env);
176 __put_user(0, &sf->extra_size);
178 //save_fpu_state(regs, &sf->fpu_state);
179 //__put_user(&sf->fpu_state, &sf->fpu_save);
181 __put_user(set->sig[0], &sf->si_mask);
182 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
183 __put_user(set->sig[i + 1], &sf->extramask[i]);
186 for (i = 0; i < 8; i++) {
187 __put_user(env->regwptr[i + WREG_L0], &sf->ss.win.locals[i]);
189 for (i = 0; i < 8; i++) {
190 __put_user(env->regwptr[i + WREG_I0], &sf->ss.win.ins[i]);
193 /* 3. signal handler back-trampoline and parameters */
194 env->regwptr[WREG_SP] = sf_addr;
195 env->regwptr[WREG_O0] = sig;
196 env->regwptr[WREG_O1] = sf_addr +
197 offsetof(struct target_signal_frame, regs);
198 env->regwptr[WREG_O2] = sf_addr +
199 offsetof(struct target_signal_frame, regs);
201 /* 4. signal handler */
202 env->pc = ka->_sa_handler;
203 env->npc = (env->pc + 4);
204 /* 5. return to kernel instructions */
205 if (ka->ka_restorer) {
206 env->regwptr[WREG_O7] = ka->ka_restorer;
207 } else {
208 uint32_t val32;
210 env->regwptr[WREG_O7] = sf_addr +
211 offsetof(struct target_signal_frame, insns) - 2 * 4;
213 /* mov __NR_sigreturn, %g1 */
214 val32 = 0x821020d8;
215 __put_user(val32, &sf->insns[0]);
217 /* t 0x10 */
218 val32 = 0x91d02010;
219 __put_user(val32, &sf->insns[1]);
221 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
222 return;
223 #if 0
224 sigill_and_return:
225 force_sig(TARGET_SIGILL);
226 #endif
227 sigsegv:
228 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
229 force_sigsegv(sig);
232 void setup_rt_frame(int sig, struct target_sigaction *ka,
233 target_siginfo_t *info,
234 target_sigset_t *set, CPUSPARCState *env)
236 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n");
239 long do_sigreturn(CPUSPARCState *env)
241 abi_ulong sf_addr;
242 struct target_signal_frame *sf;
243 abi_ulong pc, npc;
244 target_sigset_t set;
245 sigset_t host_set;
246 int i;
248 sf_addr = env->regwptr[WREG_SP];
249 trace_user_do_sigreturn(env, sf_addr);
250 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
251 goto segv_and_exit;
254 /* 1. Make sure we are not getting garbage from the user */
256 if (sf_addr & 3)
257 goto segv_and_exit;
259 __get_user(pc, &sf->regs.pc);
260 __get_user(npc, &sf->regs.npc);
262 if ((pc | npc) & 3) {
263 goto segv_and_exit;
266 /* 2. Restore the state */
267 restore_pt_regs(&sf->regs, env);
268 env->pc = pc;
269 env->npc = npc;
271 /* FIXME: implement FPU save/restore:
272 * __get_user(fpu_save, &sf->fpu_save);
273 * if (fpu_save) {
274 * if (restore_fpu_state(env, fpu_save)) {
275 * goto segv_and_exit;
280 __get_user(set.sig[0], &sf->si_mask);
281 for (i = 1; i < TARGET_NSIG_WORDS; i++) {
282 __get_user(set.sig[i], &sf->extramask[i - 1]);
285 target_to_host_sigset_internal(&host_set, &set);
286 set_sigmask(&host_set);
288 unlock_user_struct(sf, sf_addr, 0);
289 return -TARGET_QEMU_ESIGRETURN;
291 segv_and_exit:
292 unlock_user_struct(sf, sf_addr, 0);
293 force_sig(TARGET_SIGSEGV);
294 return -TARGET_QEMU_ESIGRETURN;
297 long do_rt_sigreturn(CPUSPARCState *env)
299 trace_user_do_rt_sigreturn(env, 0);
300 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n");
301 return -TARGET_ENOSYS;
304 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
305 #define SPARC_MC_TSTATE 0
306 #define SPARC_MC_PC 1
307 #define SPARC_MC_NPC 2
308 #define SPARC_MC_Y 3
309 #define SPARC_MC_G1 4
310 #define SPARC_MC_G2 5
311 #define SPARC_MC_G3 6
312 #define SPARC_MC_G4 7
313 #define SPARC_MC_G5 8
314 #define SPARC_MC_G6 9
315 #define SPARC_MC_G7 10
316 #define SPARC_MC_O0 11
317 #define SPARC_MC_O1 12
318 #define SPARC_MC_O2 13
319 #define SPARC_MC_O3 14
320 #define SPARC_MC_O4 15
321 #define SPARC_MC_O5 16
322 #define SPARC_MC_O6 17
323 #define SPARC_MC_O7 18
324 #define SPARC_MC_NGREG 19
326 typedef abi_ulong target_mc_greg_t;
327 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
329 struct target_mc_fq {
330 abi_ulong mcfq_addr;
331 uint32_t mcfq_insn;
335 * Note the manual 16-alignment; the kernel gets this because it
336 * includes a "long double qregs[16]" in the mcpu_fregs union,
337 * which we can't do.
339 struct target_mc_fpu {
340 union {
341 uint32_t sregs[32];
342 uint64_t dregs[32];
343 //uint128_t qregs[16];
344 } mcfpu_fregs;
345 abi_ulong mcfpu_fsr;
346 abi_ulong mcfpu_fprs;
347 abi_ulong mcfpu_gsr;
348 abi_ulong mcfpu_fq;
349 unsigned char mcfpu_qcnt;
350 unsigned char mcfpu_qentsz;
351 unsigned char mcfpu_enab;
352 } __attribute__((aligned(16)));
353 typedef struct target_mc_fpu target_mc_fpu_t;
355 typedef struct {
356 target_mc_gregset_t mc_gregs;
357 target_mc_greg_t mc_fp;
358 target_mc_greg_t mc_i7;
359 target_mc_fpu_t mc_fpregs;
360 } target_mcontext_t;
362 struct target_ucontext {
363 abi_ulong tuc_link;
364 abi_ulong tuc_flags;
365 target_sigset_t tuc_sigmask;
366 target_mcontext_t tuc_mcontext;
369 /* {set, get}context() needed for 64-bit SparcLinux userland. */
370 void sparc64_set_context(CPUSPARCState *env)
372 abi_ulong ucp_addr;
373 struct target_ucontext *ucp;
374 target_mc_gregset_t *grp;
375 target_mc_fpu_t *fpup;
376 abi_ulong pc, npc, tstate;
377 unsigned int i;
378 unsigned char fenab;
380 ucp_addr = env->regwptr[WREG_O0];
381 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
382 goto do_sigsegv;
384 grp = &ucp->tuc_mcontext.mc_gregs;
385 __get_user(pc, &((*grp)[SPARC_MC_PC]));
386 __get_user(npc, &((*grp)[SPARC_MC_NPC]));
387 if ((pc | npc) & 3) {
388 goto do_sigsegv;
390 if (env->regwptr[WREG_O1]) {
391 target_sigset_t target_set;
392 sigset_t set;
394 if (TARGET_NSIG_WORDS == 1) {
395 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
396 } else {
397 abi_ulong *src, *dst;
398 src = ucp->tuc_sigmask.sig;
399 dst = target_set.sig;
400 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
401 __get_user(*dst, src);
404 target_to_host_sigset_internal(&set, &target_set);
405 set_sigmask(&set);
407 env->pc = pc;
408 env->npc = npc;
409 __get_user(env->y, &((*grp)[SPARC_MC_Y]));
410 __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
411 /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
412 env->asi = (tstate >> 24) & 0xff;
413 cpu_put_ccr(env, (tstate >> 32) & 0xff);
414 __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
415 __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
416 __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
417 __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
418 __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
419 __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
420 /* Skip g7 as that's the thread register in userspace */
423 * Note that unlike the kernel, we didn't need to mess with the
424 * guest register window state to save it into a pt_regs to run
425 * the kernel. So for us the guest's O regs are still in WREG_O*
426 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
427 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
428 * need to be written back to userspace memory.
430 __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0]));
431 __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1]));
432 __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2]));
433 __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3]));
434 __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4]));
435 __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5]));
436 __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6]));
437 __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7]));
439 __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp));
440 __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7));
442 fpup = &ucp->tuc_mcontext.mc_fpregs;
444 __get_user(fenab, &(fpup->mcfpu_enab));
445 if (fenab) {
446 abi_ulong fprs;
449 * We use the FPRS from the guest only in deciding whether
450 * to restore the upper, lower, or both banks of the FPU regs.
451 * The kernel here writes the FPU register data into the
452 * process's current_thread_info state and unconditionally
453 * clears FPRS and TSTATE_PEF: this disables the FPU so that the
454 * next FPU-disabled trap will copy the data out of
455 * current_thread_info and into the real FPU registers.
456 * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
457 * so we always load the data directly into the FPU registers
458 * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
459 * Note that because we (and the kernel) always write zeroes for
460 * the fenab and fprs in sparc64_get_context() none of this code
461 * will execute unless the guest manually constructed or changed
462 * the context structure.
464 __get_user(fprs, &(fpup->mcfpu_fprs));
465 if (fprs & FPRS_DL) {
466 for (i = 0; i < 16; i++) {
467 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
470 if (fprs & FPRS_DU) {
471 for (i = 16; i < 32; i++) {
472 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
475 __get_user(env->fsr, &(fpup->mcfpu_fsr));
476 __get_user(env->gsr, &(fpup->mcfpu_gsr));
478 unlock_user_struct(ucp, ucp_addr, 0);
479 return;
480 do_sigsegv:
481 unlock_user_struct(ucp, ucp_addr, 0);
482 force_sig(TARGET_SIGSEGV);
485 void sparc64_get_context(CPUSPARCState *env)
487 abi_ulong ucp_addr;
488 struct target_ucontext *ucp;
489 target_mc_gregset_t *grp;
490 target_mcontext_t *mcp;
491 int err;
492 unsigned int i;
493 target_sigset_t target_set;
494 sigset_t set;
496 ucp_addr = env->regwptr[WREG_O0];
497 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
498 goto do_sigsegv;
501 memset(ucp, 0, sizeof(*ucp));
503 mcp = &ucp->tuc_mcontext;
504 grp = &mcp->mc_gregs;
506 /* Skip over the trap instruction, first. */
507 env->pc = env->npc;
508 env->npc += 4;
510 /* If we're only reading the signal mask then do_sigprocmask()
511 * is guaranteed not to fail, which is important because we don't
512 * have any way to signal a failure or restart this operation since
513 * this is not a normal syscall.
515 err = do_sigprocmask(0, NULL, &set);
516 assert(err == 0);
517 host_to_target_sigset_internal(&target_set, &set);
518 if (TARGET_NSIG_WORDS == 1) {
519 __put_user(target_set.sig[0],
520 (abi_ulong *)&ucp->tuc_sigmask);
521 } else {
522 abi_ulong *src, *dst;
523 src = target_set.sig;
524 dst = ucp->tuc_sigmask.sig;
525 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
526 __put_user(*src, dst);
530 __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE]));
531 __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
532 __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
533 __put_user(env->y, &((*grp)[SPARC_MC_Y]));
534 __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
535 __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
536 __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
537 __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
538 __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
539 __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
540 __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
543 * Note that unlike the kernel, we didn't need to mess with the
544 * guest register window state to save it into a pt_regs to run
545 * the kernel. So for us the guest's O regs are still in WREG_O*
546 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
547 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
548 * need to be fished out of userspace memory.
550 __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0]));
551 __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1]));
552 __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2]));
553 __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3]));
554 __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4]));
555 __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5]));
556 __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6]));
557 __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7]));
559 __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp));
560 __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7));
563 * We don't write out the FPU state. This matches the kernel's
564 * implementation (which has the code for doing this but
565 * hidden behind an "if (fenab)" where fenab is always 0).
568 unlock_user_struct(ucp, ucp_addr, 1);
569 return;
570 do_sigsegv:
571 unlock_user_struct(ucp, ucp_addr, 1);
572 force_sig(TARGET_SIGSEGV);
574 #endif