docs/devel/testing.rst: add missing newlines after code block
[qemu/ar7.git] / linux-user / sparc / signal.c
blobe44e99993c6828169f4cdcd96c9b0a377cf07844
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 #define __SUNOS_MAXWIN 31
26 /* This is what SunOS does, so shall I. */
27 struct target_sigcontext {
28 abi_ulong sigc_onstack; /* state to restore */
30 abi_ulong sigc_mask; /* sigmask to restore */
31 abi_ulong sigc_sp; /* stack pointer */
32 abi_ulong sigc_pc; /* program counter */
33 abi_ulong sigc_npc; /* next program counter */
34 abi_ulong sigc_psr; /* for condition codes etc */
35 abi_ulong sigc_g1; /* User uses these two registers */
36 abi_ulong sigc_o0; /* within the trampoline code. */
38 /* Now comes information regarding the users window set
39 * at the time of the signal.
41 abi_ulong sigc_oswins; /* outstanding windows */
43 /* stack ptrs for each regwin buf */
44 char *sigc_spbuf[__SUNOS_MAXWIN];
46 /* Windows to restore after signal */
47 struct {
48 abi_ulong locals[8];
49 abi_ulong ins[8];
50 } sigc_wbuf[__SUNOS_MAXWIN];
52 /* A Sparc stack frame */
53 struct sparc_stackf {
54 abi_ulong locals[8];
55 abi_ulong ins[8];
56 /* It's simpler to treat fp and callers_pc as elements of ins[]
57 * since we never need to access them ourselves.
59 char *structptr;
60 abi_ulong xargs[6];
61 abi_ulong xxargs[1];
64 typedef struct {
65 struct {
66 abi_ulong psr;
67 abi_ulong pc;
68 abi_ulong npc;
69 abi_ulong y;
70 abi_ulong u_regs[16]; /* globals and ins */
71 } si_regs;
72 int si_mask;
73 } __siginfo_t;
75 typedef struct {
76 abi_ulong si_float_regs[32];
77 unsigned long si_fsr;
78 unsigned long si_fpqdepth;
79 struct {
80 unsigned long *insn_addr;
81 unsigned long insn;
82 } si_fpqueue [16];
83 } qemu_siginfo_fpu_t;
86 struct target_signal_frame {
87 struct sparc_stackf ss;
88 __siginfo_t info;
89 abi_ulong fpu_save;
90 abi_ulong insns[2] __attribute__ ((aligned (8)));
91 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
92 abi_ulong extra_size; /* Should be 0 */
93 qemu_siginfo_fpu_t fpu_state;
95 struct target_rt_signal_frame {
96 struct sparc_stackf ss;
97 siginfo_t info;
98 abi_ulong regs[20];
99 sigset_t mask;
100 abi_ulong fpu_save;
101 unsigned int insns[2];
102 stack_t stack;
103 unsigned int extra_size; /* Should be 0 */
104 qemu_siginfo_fpu_t fpu_state;
107 #define UREG_O0 16
108 #define UREG_O6 22
109 #define UREG_I0 0
110 #define UREG_I1 1
111 #define UREG_I2 2
112 #define UREG_I3 3
113 #define UREG_I4 4
114 #define UREG_I5 5
115 #define UREG_I6 6
116 #define UREG_I7 7
117 #define UREG_L0 8
118 #define UREG_FP UREG_I6
119 #define UREG_SP UREG_O6
121 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
122 CPUSPARCState *env,
123 unsigned long framesize)
125 abi_ulong sp = get_sp_from_cpustate(env);
128 * If we are on the alternate signal stack and would overflow it, don't.
129 * Return an always-bogus address instead so we will die with SIGSEGV.
131 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
132 return -1;
135 /* This is the X/Open sanctioned signal stack switching. */
136 sp = target_sigsp(sp, sa) - framesize;
138 /* Always align the stack frame. This handles two cases. First,
139 * sigaltstack need not be mindful of platform specific stack
140 * alignment. Second, if we took this signal because the stack
141 * is not aligned properly, we'd like to take the signal cleanly
142 * and report that.
144 sp &= ~15UL;
146 return sp;
149 static int
150 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
152 int err = 0, i;
154 __put_user(env->psr, &si->si_regs.psr);
155 __put_user(env->pc, &si->si_regs.pc);
156 __put_user(env->npc, &si->si_regs.npc);
157 __put_user(env->y, &si->si_regs.y);
158 for (i=0; i < 8; i++) {
159 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
161 for (i=0; i < 8; i++) {
162 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
164 __put_user(mask, &si->si_mask);
165 return err;
168 #if 0
169 static int
170 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
171 CPUSPARCState *env, unsigned long mask)
173 int err = 0;
175 __put_user(mask, &sc->sigc_mask);
176 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
177 __put_user(env->pc, &sc->sigc_pc);
178 __put_user(env->npc, &sc->sigc_npc);
179 __put_user(env->psr, &sc->sigc_psr);
180 __put_user(env->gregs[1], &sc->sigc_g1);
181 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
183 return err;
185 #endif
186 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
188 void setup_frame(int sig, struct target_sigaction *ka,
189 target_sigset_t *set, CPUSPARCState *env)
191 abi_ulong sf_addr;
192 struct target_signal_frame *sf;
193 int sigframe_size, err, i;
195 /* 1. Make sure everything is clean */
196 //synchronize_user_stack();
198 sigframe_size = NF_ALIGNEDSZ;
199 sf_addr = get_sigframe(ka, env, sigframe_size);
200 trace_user_setup_frame(env, sf_addr);
202 sf = lock_user(VERIFY_WRITE, sf_addr,
203 sizeof(struct target_signal_frame), 0);
204 if (!sf) {
205 goto sigsegv;
207 #if 0
208 if (invalid_frame_pointer(sf, sigframe_size))
209 goto sigill_and_return;
210 #endif
211 /* 2. Save the current process state */
212 err = setup___siginfo(&sf->info, env, set->sig[0]);
213 __put_user(0, &sf->extra_size);
215 //save_fpu_state(regs, &sf->fpu_state);
216 //__put_user(&sf->fpu_state, &sf->fpu_save);
218 __put_user(set->sig[0], &sf->info.si_mask);
219 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
220 __put_user(set->sig[i + 1], &sf->extramask[i]);
223 for (i = 0; i < 8; i++) {
224 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
226 for (i = 0; i < 8; i++) {
227 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
229 if (err)
230 goto sigsegv;
232 /* 3. signal handler back-trampoline and parameters */
233 env->regwptr[UREG_FP] = sf_addr;
234 env->regwptr[UREG_I0] = sig;
235 env->regwptr[UREG_I1] = sf_addr +
236 offsetof(struct target_signal_frame, info);
237 env->regwptr[UREG_I2] = sf_addr +
238 offsetof(struct target_signal_frame, info);
240 /* 4. signal handler */
241 env->pc = ka->_sa_handler;
242 env->npc = (env->pc + 4);
243 /* 5. return to kernel instructions */
244 if (ka->ka_restorer) {
245 env->regwptr[UREG_I7] = ka->ka_restorer;
246 } else {
247 uint32_t val32;
249 env->regwptr[UREG_I7] = sf_addr +
250 offsetof(struct target_signal_frame, insns) - 2 * 4;
252 /* mov __NR_sigreturn, %g1 */
253 val32 = 0x821020d8;
254 __put_user(val32, &sf->insns[0]);
256 /* t 0x10 */
257 val32 = 0x91d02010;
258 __put_user(val32, &sf->insns[1]);
259 if (err)
260 goto sigsegv;
262 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
263 return;
264 #if 0
265 sigill_and_return:
266 force_sig(TARGET_SIGILL);
267 #endif
268 sigsegv:
269 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
270 force_sigsegv(sig);
273 void setup_rt_frame(int sig, struct target_sigaction *ka,
274 target_siginfo_t *info,
275 target_sigset_t *set, CPUSPARCState *env)
277 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n");
280 long do_sigreturn(CPUSPARCState *env)
282 abi_ulong sf_addr;
283 struct target_signal_frame *sf;
284 uint32_t up_psr, pc, npc;
285 target_sigset_t set;
286 sigset_t host_set;
287 int err=0, i;
289 sf_addr = env->regwptr[UREG_FP];
290 trace_user_do_sigreturn(env, sf_addr);
291 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
292 goto segv_and_exit;
295 /* 1. Make sure we are not getting garbage from the user */
297 if (sf_addr & 3)
298 goto segv_and_exit;
300 __get_user(pc, &sf->info.si_regs.pc);
301 __get_user(npc, &sf->info.si_regs.npc);
303 if ((pc | npc) & 3) {
304 goto segv_and_exit;
307 /* 2. Restore the state */
308 __get_user(up_psr, &sf->info.si_regs.psr);
310 /* User can only change condition codes and FPU enabling in %psr. */
311 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
312 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
314 env->pc = pc;
315 env->npc = npc;
316 __get_user(env->y, &sf->info.si_regs.y);
317 for (i=0; i < 8; i++) {
318 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
320 for (i=0; i < 8; i++) {
321 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
324 /* FIXME: implement FPU save/restore:
325 * __get_user(fpu_save, &sf->fpu_save);
326 * if (fpu_save)
327 * err |= restore_fpu_state(env, fpu_save);
330 /* This is pretty much atomic, no amount locking would prevent
331 * the races which exist anyways.
333 __get_user(set.sig[0], &sf->info.si_mask);
334 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
335 __get_user(set.sig[i], &sf->extramask[i - 1]);
338 target_to_host_sigset_internal(&host_set, &set);
339 set_sigmask(&host_set);
341 if (err) {
342 goto segv_and_exit;
344 unlock_user_struct(sf, sf_addr, 0);
345 return -TARGET_QEMU_ESIGRETURN;
347 segv_and_exit:
348 unlock_user_struct(sf, sf_addr, 0);
349 force_sig(TARGET_SIGSEGV);
350 return -TARGET_QEMU_ESIGRETURN;
353 long do_rt_sigreturn(CPUSPARCState *env)
355 trace_user_do_rt_sigreturn(env, 0);
356 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n");
357 return -TARGET_ENOSYS;
360 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
361 #define SPARC_MC_TSTATE 0
362 #define SPARC_MC_PC 1
363 #define SPARC_MC_NPC 2
364 #define SPARC_MC_Y 3
365 #define SPARC_MC_G1 4
366 #define SPARC_MC_G2 5
367 #define SPARC_MC_G3 6
368 #define SPARC_MC_G4 7
369 #define SPARC_MC_G5 8
370 #define SPARC_MC_G6 9
371 #define SPARC_MC_G7 10
372 #define SPARC_MC_O0 11
373 #define SPARC_MC_O1 12
374 #define SPARC_MC_O2 13
375 #define SPARC_MC_O3 14
376 #define SPARC_MC_O4 15
377 #define SPARC_MC_O5 16
378 #define SPARC_MC_O6 17
379 #define SPARC_MC_O7 18
380 #define SPARC_MC_NGREG 19
382 typedef abi_ulong target_mc_greg_t;
383 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
385 struct target_mc_fq {
386 abi_ulong *mcfq_addr;
387 uint32_t mcfq_insn;
390 struct target_mc_fpu {
391 union {
392 uint32_t sregs[32];
393 uint64_t dregs[32];
394 //uint128_t qregs[16];
395 } mcfpu_fregs;
396 abi_ulong mcfpu_fsr;
397 abi_ulong mcfpu_fprs;
398 abi_ulong mcfpu_gsr;
399 struct target_mc_fq *mcfpu_fq;
400 unsigned char mcfpu_qcnt;
401 unsigned char mcfpu_qentsz;
402 unsigned char mcfpu_enab;
404 typedef struct target_mc_fpu target_mc_fpu_t;
406 typedef struct {
407 target_mc_gregset_t mc_gregs;
408 target_mc_greg_t mc_fp;
409 target_mc_greg_t mc_i7;
410 target_mc_fpu_t mc_fpregs;
411 } target_mcontext_t;
413 struct target_ucontext {
414 struct target_ucontext *tuc_link;
415 abi_ulong tuc_flags;
416 target_sigset_t tuc_sigmask;
417 target_mcontext_t tuc_mcontext;
420 /* A V9 register window */
421 struct target_reg_window {
422 abi_ulong locals[8];
423 abi_ulong ins[8];
426 #define TARGET_STACK_BIAS 2047
428 /* {set, get}context() needed for 64-bit SparcLinux userland. */
429 void sparc64_set_context(CPUSPARCState *env)
431 abi_ulong ucp_addr;
432 struct target_ucontext *ucp;
433 target_mc_gregset_t *grp;
434 abi_ulong pc, npc, tstate;
435 abi_ulong fp, i7, w_addr;
436 unsigned int i;
438 ucp_addr = env->regwptr[UREG_I0];
439 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
440 goto do_sigsegv;
442 grp = &ucp->tuc_mcontext.mc_gregs;
443 __get_user(pc, &((*grp)[SPARC_MC_PC]));
444 __get_user(npc, &((*grp)[SPARC_MC_NPC]));
445 if ((pc | npc) & 3) {
446 goto do_sigsegv;
448 if (env->regwptr[UREG_I1]) {
449 target_sigset_t target_set;
450 sigset_t set;
452 if (TARGET_NSIG_WORDS == 1) {
453 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
454 } else {
455 abi_ulong *src, *dst;
456 src = ucp->tuc_sigmask.sig;
457 dst = target_set.sig;
458 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
459 __get_user(*dst, src);
462 target_to_host_sigset_internal(&set, &target_set);
463 set_sigmask(&set);
465 env->pc = pc;
466 env->npc = npc;
467 __get_user(env->y, &((*grp)[SPARC_MC_Y]));
468 __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
469 env->asi = (tstate >> 24) & 0xff;
470 cpu_put_ccr(env, tstate >> 32);
471 cpu_put_cwp64(env, tstate & 0x1f);
472 __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
473 __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
474 __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
475 __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
476 __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
477 __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
478 __get_user(env->gregs[7], (&(*grp)[SPARC_MC_G7]));
479 __get_user(env->regwptr[UREG_I0], (&(*grp)[SPARC_MC_O0]));
480 __get_user(env->regwptr[UREG_I1], (&(*grp)[SPARC_MC_O1]));
481 __get_user(env->regwptr[UREG_I2], (&(*grp)[SPARC_MC_O2]));
482 __get_user(env->regwptr[UREG_I3], (&(*grp)[SPARC_MC_O3]));
483 __get_user(env->regwptr[UREG_I4], (&(*grp)[SPARC_MC_O4]));
484 __get_user(env->regwptr[UREG_I5], (&(*grp)[SPARC_MC_O5]));
485 __get_user(env->regwptr[UREG_I6], (&(*grp)[SPARC_MC_O6]));
486 __get_user(env->regwptr[UREG_I7], (&(*grp)[SPARC_MC_O7]));
488 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
489 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
491 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
492 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
493 abi_ulong) != 0) {
494 goto do_sigsegv;
496 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
497 abi_ulong) != 0) {
498 goto do_sigsegv;
500 /* FIXME this does not match how the kernel handles the FPU in
501 * its sparc64_set_context implementation. In particular the FPU
502 * is only restored if fenab is non-zero in:
503 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
505 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
507 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
508 for (i = 0; i < 64; i++, src++) {
509 if (i & 1) {
510 __get_user(env->fpr[i/2].l.lower, src);
511 } else {
512 __get_user(env->fpr[i/2].l.upper, src);
516 __get_user(env->fsr,
517 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
518 __get_user(env->gsr,
519 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
520 unlock_user_struct(ucp, ucp_addr, 0);
521 return;
522 do_sigsegv:
523 unlock_user_struct(ucp, ucp_addr, 0);
524 force_sig(TARGET_SIGSEGV);
527 void sparc64_get_context(CPUSPARCState *env)
529 abi_ulong ucp_addr;
530 struct target_ucontext *ucp;
531 target_mc_gregset_t *grp;
532 target_mcontext_t *mcp;
533 abi_ulong fp, i7, w_addr;
534 int err;
535 unsigned int i;
536 target_sigset_t target_set;
537 sigset_t set;
539 ucp_addr = env->regwptr[UREG_I0];
540 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
541 goto do_sigsegv;
544 mcp = &ucp->tuc_mcontext;
545 grp = &mcp->mc_gregs;
547 /* Skip over the trap instruction, first. */
548 env->pc = env->npc;
549 env->npc += 4;
551 /* If we're only reading the signal mask then do_sigprocmask()
552 * is guaranteed not to fail, which is important because we don't
553 * have any way to signal a failure or restart this operation since
554 * this is not a normal syscall.
556 err = do_sigprocmask(0, NULL, &set);
557 assert(err == 0);
558 host_to_target_sigset_internal(&target_set, &set);
559 if (TARGET_NSIG_WORDS == 1) {
560 __put_user(target_set.sig[0],
561 (abi_ulong *)&ucp->tuc_sigmask);
562 } else {
563 abi_ulong *src, *dst;
564 src = target_set.sig;
565 dst = ucp->tuc_sigmask.sig;
566 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
567 __put_user(*src, dst);
569 if (err)
570 goto do_sigsegv;
573 /* XXX: tstate must be saved properly */
574 // __put_user(env->tstate, &((*grp)[SPARC_MC_TSTATE]));
575 __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
576 __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
577 __put_user(env->y, &((*grp)[SPARC_MC_Y]));
578 __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
579 __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
580 __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
581 __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
582 __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
583 __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
584 __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
585 __put_user(env->regwptr[UREG_I0], &((*grp)[SPARC_MC_O0]));
586 __put_user(env->regwptr[UREG_I1], &((*grp)[SPARC_MC_O1]));
587 __put_user(env->regwptr[UREG_I2], &((*grp)[SPARC_MC_O2]));
588 __put_user(env->regwptr[UREG_I3], &((*grp)[SPARC_MC_O3]));
589 __put_user(env->regwptr[UREG_I4], &((*grp)[SPARC_MC_O4]));
590 __put_user(env->regwptr[UREG_I5], &((*grp)[SPARC_MC_O5]));
591 __put_user(env->regwptr[UREG_I6], &((*grp)[SPARC_MC_O6]));
592 __put_user(env->regwptr[UREG_I7], &((*grp)[SPARC_MC_O7]));
594 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
595 fp = i7 = 0;
596 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
597 abi_ulong) != 0) {
598 goto do_sigsegv;
600 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
601 abi_ulong) != 0) {
602 goto do_sigsegv;
604 __put_user(fp, &(mcp->mc_fp));
605 __put_user(i7, &(mcp->mc_i7));
608 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
609 for (i = 0; i < 64; i++, dst++) {
610 if (i & 1) {
611 __put_user(env->fpr[i/2].l.lower, dst);
612 } else {
613 __put_user(env->fpr[i/2].l.upper, dst);
617 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
618 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
619 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
621 if (err)
622 goto do_sigsegv;
623 unlock_user_struct(ucp, ucp_addr, 1);
624 return;
625 do_sigsegv:
626 unlock_user_struct(ucp, ucp_addr, 1);
627 force_sig(TARGET_SIGSEGV);
629 #endif