BootLinuxConsoleTest: Test the SmartFusion2 board
[qemu/kevin.git] / linux-user / sparc / signal.c
blobead169fbaa26e148914c72329a1239b6c7fa0d62
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]);
260 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
261 return;
262 #if 0
263 sigill_and_return:
264 force_sig(TARGET_SIGILL);
265 #endif
266 sigsegv:
267 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
268 force_sigsegv(sig);
271 void setup_rt_frame(int sig, struct target_sigaction *ka,
272 target_siginfo_t *info,
273 target_sigset_t *set, CPUSPARCState *env)
275 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n");
278 long do_sigreturn(CPUSPARCState *env)
280 abi_ulong sf_addr;
281 struct target_signal_frame *sf;
282 uint32_t up_psr, pc, npc;
283 target_sigset_t set;
284 sigset_t host_set;
285 int i;
287 sf_addr = env->regwptr[UREG_FP];
288 trace_user_do_sigreturn(env, sf_addr);
289 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
290 goto segv_and_exit;
293 /* 1. Make sure we are not getting garbage from the user */
295 if (sf_addr & 3)
296 goto segv_and_exit;
298 __get_user(pc, &sf->info.si_regs.pc);
299 __get_user(npc, &sf->info.si_regs.npc);
301 if ((pc | npc) & 3) {
302 goto segv_and_exit;
305 /* 2. Restore the state */
306 __get_user(up_psr, &sf->info.si_regs.psr);
308 /* User can only change condition codes and FPU enabling in %psr. */
309 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
310 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
312 env->pc = pc;
313 env->npc = npc;
314 __get_user(env->y, &sf->info.si_regs.y);
315 for (i=0; i < 8; i++) {
316 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
318 for (i=0; i < 8; i++) {
319 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
322 /* FIXME: implement FPU save/restore:
323 * __get_user(fpu_save, &sf->fpu_save);
324 * if (fpu_save) {
325 * if (restore_fpu_state(env, fpu_save)) {
326 * goto segv_and_exit;
331 /* This is pretty much atomic, no amount locking would prevent
332 * the races which exist anyways.
334 __get_user(set.sig[0], &sf->info.si_mask);
335 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
336 __get_user(set.sig[i], &sf->extramask[i - 1]);
339 target_to_host_sigset_internal(&host_set, &set);
340 set_sigmask(&host_set);
342 unlock_user_struct(sf, sf_addr, 0);
343 return -TARGET_QEMU_ESIGRETURN;
345 segv_and_exit:
346 unlock_user_struct(sf, sf_addr, 0);
347 force_sig(TARGET_SIGSEGV);
348 return -TARGET_QEMU_ESIGRETURN;
351 long do_rt_sigreturn(CPUSPARCState *env)
353 trace_user_do_rt_sigreturn(env, 0);
354 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n");
355 return -TARGET_ENOSYS;
358 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
359 #define SPARC_MC_TSTATE 0
360 #define SPARC_MC_PC 1
361 #define SPARC_MC_NPC 2
362 #define SPARC_MC_Y 3
363 #define SPARC_MC_G1 4
364 #define SPARC_MC_G2 5
365 #define SPARC_MC_G3 6
366 #define SPARC_MC_G4 7
367 #define SPARC_MC_G5 8
368 #define SPARC_MC_G6 9
369 #define SPARC_MC_G7 10
370 #define SPARC_MC_O0 11
371 #define SPARC_MC_O1 12
372 #define SPARC_MC_O2 13
373 #define SPARC_MC_O3 14
374 #define SPARC_MC_O4 15
375 #define SPARC_MC_O5 16
376 #define SPARC_MC_O6 17
377 #define SPARC_MC_O7 18
378 #define SPARC_MC_NGREG 19
380 typedef abi_ulong target_mc_greg_t;
381 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
383 struct target_mc_fq {
384 abi_ulong *mcfq_addr;
385 uint32_t mcfq_insn;
388 struct target_mc_fpu {
389 union {
390 uint32_t sregs[32];
391 uint64_t dregs[32];
392 //uint128_t qregs[16];
393 } mcfpu_fregs;
394 abi_ulong mcfpu_fsr;
395 abi_ulong mcfpu_fprs;
396 abi_ulong mcfpu_gsr;
397 struct target_mc_fq *mcfpu_fq;
398 unsigned char mcfpu_qcnt;
399 unsigned char mcfpu_qentsz;
400 unsigned char mcfpu_enab;
402 typedef struct target_mc_fpu target_mc_fpu_t;
404 typedef struct {
405 target_mc_gregset_t mc_gregs;
406 target_mc_greg_t mc_fp;
407 target_mc_greg_t mc_i7;
408 target_mc_fpu_t mc_fpregs;
409 } target_mcontext_t;
411 struct target_ucontext {
412 struct target_ucontext *tuc_link;
413 abi_ulong tuc_flags;
414 target_sigset_t tuc_sigmask;
415 target_mcontext_t tuc_mcontext;
418 /* A V9 register window */
419 struct target_reg_window {
420 abi_ulong locals[8];
421 abi_ulong ins[8];
424 #define TARGET_STACK_BIAS 2047
426 /* {set, get}context() needed for 64-bit SparcLinux userland. */
427 void sparc64_set_context(CPUSPARCState *env)
429 abi_ulong ucp_addr;
430 struct target_ucontext *ucp;
431 target_mc_gregset_t *grp;
432 abi_ulong pc, npc, tstate;
433 abi_ulong fp, i7, w_addr;
434 unsigned int i;
436 ucp_addr = env->regwptr[UREG_I0];
437 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
438 goto do_sigsegv;
440 grp = &ucp->tuc_mcontext.mc_gregs;
441 __get_user(pc, &((*grp)[SPARC_MC_PC]));
442 __get_user(npc, &((*grp)[SPARC_MC_NPC]));
443 if ((pc | npc) & 3) {
444 goto do_sigsegv;
446 if (env->regwptr[UREG_I1]) {
447 target_sigset_t target_set;
448 sigset_t set;
450 if (TARGET_NSIG_WORDS == 1) {
451 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
452 } else {
453 abi_ulong *src, *dst;
454 src = ucp->tuc_sigmask.sig;
455 dst = target_set.sig;
456 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
457 __get_user(*dst, src);
460 target_to_host_sigset_internal(&set, &target_set);
461 set_sigmask(&set);
463 env->pc = pc;
464 env->npc = npc;
465 __get_user(env->y, &((*grp)[SPARC_MC_Y]));
466 __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
467 env->asi = (tstate >> 24) & 0xff;
468 cpu_put_ccr(env, tstate >> 32);
469 cpu_put_cwp64(env, tstate & 0x1f);
470 __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
471 __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
472 __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
473 __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
474 __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
475 __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
476 __get_user(env->gregs[7], (&(*grp)[SPARC_MC_G7]));
477 __get_user(env->regwptr[UREG_I0], (&(*grp)[SPARC_MC_O0]));
478 __get_user(env->regwptr[UREG_I1], (&(*grp)[SPARC_MC_O1]));
479 __get_user(env->regwptr[UREG_I2], (&(*grp)[SPARC_MC_O2]));
480 __get_user(env->regwptr[UREG_I3], (&(*grp)[SPARC_MC_O3]));
481 __get_user(env->regwptr[UREG_I4], (&(*grp)[SPARC_MC_O4]));
482 __get_user(env->regwptr[UREG_I5], (&(*grp)[SPARC_MC_O5]));
483 __get_user(env->regwptr[UREG_I6], (&(*grp)[SPARC_MC_O6]));
484 __get_user(env->regwptr[UREG_I7], (&(*grp)[SPARC_MC_O7]));
486 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
487 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
489 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
490 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
491 abi_ulong) != 0) {
492 goto do_sigsegv;
494 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
495 abi_ulong) != 0) {
496 goto do_sigsegv;
498 /* FIXME this does not match how the kernel handles the FPU in
499 * its sparc64_set_context implementation. In particular the FPU
500 * is only restored if fenab is non-zero in:
501 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
503 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
505 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
506 for (i = 0; i < 64; i++, src++) {
507 if (i & 1) {
508 __get_user(env->fpr[i/2].l.lower, src);
509 } else {
510 __get_user(env->fpr[i/2].l.upper, src);
514 __get_user(env->fsr,
515 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
516 __get_user(env->gsr,
517 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
518 unlock_user_struct(ucp, ucp_addr, 0);
519 return;
520 do_sigsegv:
521 unlock_user_struct(ucp, ucp_addr, 0);
522 force_sig(TARGET_SIGSEGV);
525 void sparc64_get_context(CPUSPARCState *env)
527 abi_ulong ucp_addr;
528 struct target_ucontext *ucp;
529 target_mc_gregset_t *grp;
530 target_mcontext_t *mcp;
531 abi_ulong fp, i7, w_addr;
532 int err;
533 unsigned int i;
534 target_sigset_t target_set;
535 sigset_t set;
537 ucp_addr = env->regwptr[UREG_I0];
538 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
539 goto do_sigsegv;
542 mcp = &ucp->tuc_mcontext;
543 grp = &mcp->mc_gregs;
545 /* Skip over the trap instruction, first. */
546 env->pc = env->npc;
547 env->npc += 4;
549 /* If we're only reading the signal mask then do_sigprocmask()
550 * is guaranteed not to fail, which is important because we don't
551 * have any way to signal a failure or restart this operation since
552 * this is not a normal syscall.
554 err = do_sigprocmask(0, NULL, &set);
555 assert(err == 0);
556 host_to_target_sigset_internal(&target_set, &set);
557 if (TARGET_NSIG_WORDS == 1) {
558 __put_user(target_set.sig[0],
559 (abi_ulong *)&ucp->tuc_sigmask);
560 } else {
561 abi_ulong *src, *dst;
562 src = target_set.sig;
563 dst = ucp->tuc_sigmask.sig;
564 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
565 __put_user(*src, dst);
567 if (err)
568 goto do_sigsegv;
571 /* XXX: tstate must be saved properly */
572 // __put_user(env->tstate, &((*grp)[SPARC_MC_TSTATE]));
573 __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
574 __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
575 __put_user(env->y, &((*grp)[SPARC_MC_Y]));
576 __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
577 __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
578 __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
579 __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
580 __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
581 __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
582 __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
583 __put_user(env->regwptr[UREG_I0], &((*grp)[SPARC_MC_O0]));
584 __put_user(env->regwptr[UREG_I1], &((*grp)[SPARC_MC_O1]));
585 __put_user(env->regwptr[UREG_I2], &((*grp)[SPARC_MC_O2]));
586 __put_user(env->regwptr[UREG_I3], &((*grp)[SPARC_MC_O3]));
587 __put_user(env->regwptr[UREG_I4], &((*grp)[SPARC_MC_O4]));
588 __put_user(env->regwptr[UREG_I5], &((*grp)[SPARC_MC_O5]));
589 __put_user(env->regwptr[UREG_I6], &((*grp)[SPARC_MC_O6]));
590 __put_user(env->regwptr[UREG_I7], &((*grp)[SPARC_MC_O7]));
592 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
593 fp = i7 = 0;
594 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
595 abi_ulong) != 0) {
596 goto do_sigsegv;
598 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
599 abi_ulong) != 0) {
600 goto do_sigsegv;
602 __put_user(fp, &(mcp->mc_fp));
603 __put_user(i7, &(mcp->mc_i7));
606 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
607 for (i = 0; i < 64; i++, dst++) {
608 if (i & 1) {
609 __put_user(env->fpr[i/2].l.lower, dst);
610 } else {
611 __put_user(env->fpr[i/2].l.upper, dst);
615 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
616 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
617 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
619 if (err)
620 goto do_sigsegv;
621 unlock_user_struct(ucp, ucp_addr, 1);
622 return;
623 do_sigsegv:
624 unlock_user_struct(ucp, ucp_addr, 1);
625 force_sig(TARGET_SIGSEGV);
627 #endif