linux-user: move ppc/ppc64 signal.c parts to ppc directory
[qemu/kevin.git] / linux-user / sparc / signal.c
blobc823e61cee7320aefcab3e243fef89561dd6cc99
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 "target_signal.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
25 #define __SUNOS_MAXWIN 31
27 /* This is what SunOS does, so shall I. */
28 struct target_sigcontext {
29 abi_ulong sigc_onstack; /* state to restore */
31 abi_ulong sigc_mask; /* sigmask to restore */
32 abi_ulong sigc_sp; /* stack pointer */
33 abi_ulong sigc_pc; /* program counter */
34 abi_ulong sigc_npc; /* next program counter */
35 abi_ulong sigc_psr; /* for condition codes etc */
36 abi_ulong sigc_g1; /* User uses these two registers */
37 abi_ulong sigc_o0; /* within the trampoline code. */
39 /* Now comes information regarding the users window set
40 * at the time of the signal.
42 abi_ulong sigc_oswins; /* outstanding windows */
44 /* stack ptrs for each regwin buf */
45 char *sigc_spbuf[__SUNOS_MAXWIN];
47 /* Windows to restore after signal */
48 struct {
49 abi_ulong locals[8];
50 abi_ulong ins[8];
51 } sigc_wbuf[__SUNOS_MAXWIN];
53 /* A Sparc stack frame */
54 struct sparc_stackf {
55 abi_ulong locals[8];
56 abi_ulong ins[8];
57 /* It's simpler to treat fp and callers_pc as elements of ins[]
58 * since we never need to access them ourselves.
60 char *structptr;
61 abi_ulong xargs[6];
62 abi_ulong xxargs[1];
65 typedef struct {
66 struct {
67 abi_ulong psr;
68 abi_ulong pc;
69 abi_ulong npc;
70 abi_ulong y;
71 abi_ulong u_regs[16]; /* globals and ins */
72 } si_regs;
73 int si_mask;
74 } __siginfo_t;
76 typedef struct {
77 abi_ulong si_float_regs[32];
78 unsigned long si_fsr;
79 unsigned long si_fpqdepth;
80 struct {
81 unsigned long *insn_addr;
82 unsigned long insn;
83 } si_fpqueue [16];
84 } qemu_siginfo_fpu_t;
87 struct target_signal_frame {
88 struct sparc_stackf ss;
89 __siginfo_t info;
90 abi_ulong fpu_save;
91 abi_ulong insns[2] __attribute__ ((aligned (8)));
92 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
93 abi_ulong extra_size; /* Should be 0 */
94 qemu_siginfo_fpu_t fpu_state;
96 struct target_rt_signal_frame {
97 struct sparc_stackf ss;
98 siginfo_t info;
99 abi_ulong regs[20];
100 sigset_t mask;
101 abi_ulong fpu_save;
102 unsigned int insns[2];
103 stack_t stack;
104 unsigned int extra_size; /* Should be 0 */
105 qemu_siginfo_fpu_t fpu_state;
108 #define UREG_O0 16
109 #define UREG_O6 22
110 #define UREG_I0 0
111 #define UREG_I1 1
112 #define UREG_I2 2
113 #define UREG_I3 3
114 #define UREG_I4 4
115 #define UREG_I5 5
116 #define UREG_I6 6
117 #define UREG_I7 7
118 #define UREG_L0 8
119 #define UREG_FP UREG_I6
120 #define UREG_SP UREG_O6
122 static inline abi_ulong get_sigframe(struct target_sigaction *sa,
123 CPUSPARCState *env,
124 unsigned long framesize)
126 abi_ulong sp;
128 sp = env->regwptr[UREG_FP];
130 /* This is the X/Open sanctioned signal stack switching. */
131 if (sa->sa_flags & TARGET_SA_ONSTACK) {
132 if (!on_sig_stack(sp)
133 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7)) {
134 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
137 return sp - framesize;
140 static int
141 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
143 int err = 0, i;
145 __put_user(env->psr, &si->si_regs.psr);
146 __put_user(env->pc, &si->si_regs.pc);
147 __put_user(env->npc, &si->si_regs.npc);
148 __put_user(env->y, &si->si_regs.y);
149 for (i=0; i < 8; i++) {
150 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
152 for (i=0; i < 8; i++) {
153 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
155 __put_user(mask, &si->si_mask);
156 return err;
159 #if 0
160 static int
161 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
162 CPUSPARCState *env, unsigned long mask)
164 int err = 0;
166 __put_user(mask, &sc->sigc_mask);
167 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
168 __put_user(env->pc, &sc->sigc_pc);
169 __put_user(env->npc, &sc->sigc_npc);
170 __put_user(env->psr, &sc->sigc_psr);
171 __put_user(env->gregs[1], &sc->sigc_g1);
172 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
174 return err;
176 #endif
177 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
179 void setup_frame(int sig, struct target_sigaction *ka,
180 target_sigset_t *set, CPUSPARCState *env)
182 abi_ulong sf_addr;
183 struct target_signal_frame *sf;
184 int sigframe_size, err, i;
186 /* 1. Make sure everything is clean */
187 //synchronize_user_stack();
189 sigframe_size = NF_ALIGNEDSZ;
190 sf_addr = get_sigframe(ka, env, sigframe_size);
191 trace_user_setup_frame(env, sf_addr);
193 sf = lock_user(VERIFY_WRITE, sf_addr,
194 sizeof(struct target_signal_frame), 0);
195 if (!sf) {
196 goto sigsegv;
198 #if 0
199 if (invalid_frame_pointer(sf, sigframe_size))
200 goto sigill_and_return;
201 #endif
202 /* 2. Save the current process state */
203 err = setup___siginfo(&sf->info, env, set->sig[0]);
204 __put_user(0, &sf->extra_size);
206 //save_fpu_state(regs, &sf->fpu_state);
207 //__put_user(&sf->fpu_state, &sf->fpu_save);
209 __put_user(set->sig[0], &sf->info.si_mask);
210 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
211 __put_user(set->sig[i + 1], &sf->extramask[i]);
214 for (i = 0; i < 8; i++) {
215 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
217 for (i = 0; i < 8; i++) {
218 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
220 if (err)
221 goto sigsegv;
223 /* 3. signal handler back-trampoline and parameters */
224 env->regwptr[UREG_FP] = sf_addr;
225 env->regwptr[UREG_I0] = sig;
226 env->regwptr[UREG_I1] = sf_addr +
227 offsetof(struct target_signal_frame, info);
228 env->regwptr[UREG_I2] = sf_addr +
229 offsetof(struct target_signal_frame, info);
231 /* 4. signal handler */
232 env->pc = ka->_sa_handler;
233 env->npc = (env->pc + 4);
234 /* 5. return to kernel instructions */
235 if (ka->ka_restorer) {
236 env->regwptr[UREG_I7] = ka->ka_restorer;
237 } else {
238 uint32_t val32;
240 env->regwptr[UREG_I7] = sf_addr +
241 offsetof(struct target_signal_frame, insns) - 2 * 4;
243 /* mov __NR_sigreturn, %g1 */
244 val32 = 0x821020d8;
245 __put_user(val32, &sf->insns[0]);
247 /* t 0x10 */
248 val32 = 0x91d02010;
249 __put_user(val32, &sf->insns[1]);
250 if (err)
251 goto sigsegv;
253 /* Flush instruction space. */
254 // flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
255 // tb_flush(env);
257 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
258 return;
259 #if 0
260 sigill_and_return:
261 force_sig(TARGET_SIGILL);
262 #endif
263 sigsegv:
264 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
265 force_sigsegv(sig);
268 void setup_rt_frame(int sig, struct target_sigaction *ka,
269 target_siginfo_t *info,
270 target_sigset_t *set, CPUSPARCState *env)
272 fprintf(stderr, "setup_rt_frame: not implemented\n");
275 long do_sigreturn(CPUSPARCState *env)
277 abi_ulong sf_addr;
278 struct target_signal_frame *sf;
279 uint32_t up_psr, pc, npc;
280 target_sigset_t set;
281 sigset_t host_set;
282 int err=0, i;
284 sf_addr = env->regwptr[UREG_FP];
285 trace_user_do_sigreturn(env, sf_addr);
286 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
287 goto segv_and_exit;
290 /* 1. Make sure we are not getting garbage from the user */
292 if (sf_addr & 3)
293 goto segv_and_exit;
295 __get_user(pc, &sf->info.si_regs.pc);
296 __get_user(npc, &sf->info.si_regs.npc);
298 if ((pc | npc) & 3) {
299 goto segv_and_exit;
302 /* 2. Restore the state */
303 __get_user(up_psr, &sf->info.si_regs.psr);
305 /* User can only change condition codes and FPU enabling in %psr. */
306 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
307 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
309 env->pc = pc;
310 env->npc = npc;
311 __get_user(env->y, &sf->info.si_regs.y);
312 for (i=0; i < 8; i++) {
313 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
315 for (i=0; i < 8; i++) {
316 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
319 /* FIXME: implement FPU save/restore:
320 * __get_user(fpu_save, &sf->fpu_save);
321 * if (fpu_save)
322 * err |= restore_fpu_state(env, fpu_save);
325 /* This is pretty much atomic, no amount locking would prevent
326 * the races which exist anyways.
328 __get_user(set.sig[0], &sf->info.si_mask);
329 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
330 __get_user(set.sig[i], &sf->extramask[i - 1]);
333 target_to_host_sigset_internal(&host_set, &set);
334 set_sigmask(&host_set);
336 if (err) {
337 goto segv_and_exit;
339 unlock_user_struct(sf, sf_addr, 0);
340 return -TARGET_QEMU_ESIGRETURN;
342 segv_and_exit:
343 unlock_user_struct(sf, sf_addr, 0);
344 force_sig(TARGET_SIGSEGV);
345 return -TARGET_QEMU_ESIGRETURN;
348 long do_rt_sigreturn(CPUSPARCState *env)
350 trace_user_do_rt_sigreturn(env, 0);
351 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
352 return -TARGET_ENOSYS;
355 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
356 #define SPARC_MC_TSTATE 0
357 #define SPARC_MC_PC 1
358 #define SPARC_MC_NPC 2
359 #define SPARC_MC_Y 3
360 #define SPARC_MC_G1 4
361 #define SPARC_MC_G2 5
362 #define SPARC_MC_G3 6
363 #define SPARC_MC_G4 7
364 #define SPARC_MC_G5 8
365 #define SPARC_MC_G6 9
366 #define SPARC_MC_G7 10
367 #define SPARC_MC_O0 11
368 #define SPARC_MC_O1 12
369 #define SPARC_MC_O2 13
370 #define SPARC_MC_O3 14
371 #define SPARC_MC_O4 15
372 #define SPARC_MC_O5 16
373 #define SPARC_MC_O6 17
374 #define SPARC_MC_O7 18
375 #define SPARC_MC_NGREG 19
377 typedef abi_ulong target_mc_greg_t;
378 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
380 struct target_mc_fq {
381 abi_ulong *mcfq_addr;
382 uint32_t mcfq_insn;
385 struct target_mc_fpu {
386 union {
387 uint32_t sregs[32];
388 uint64_t dregs[32];
389 //uint128_t qregs[16];
390 } mcfpu_fregs;
391 abi_ulong mcfpu_fsr;
392 abi_ulong mcfpu_fprs;
393 abi_ulong mcfpu_gsr;
394 struct target_mc_fq *mcfpu_fq;
395 unsigned char mcfpu_qcnt;
396 unsigned char mcfpu_qentsz;
397 unsigned char mcfpu_enab;
399 typedef struct target_mc_fpu target_mc_fpu_t;
401 typedef struct {
402 target_mc_gregset_t mc_gregs;
403 target_mc_greg_t mc_fp;
404 target_mc_greg_t mc_i7;
405 target_mc_fpu_t mc_fpregs;
406 } target_mcontext_t;
408 struct target_ucontext {
409 struct target_ucontext *tuc_link;
410 abi_ulong tuc_flags;
411 target_sigset_t tuc_sigmask;
412 target_mcontext_t tuc_mcontext;
415 /* A V9 register window */
416 struct target_reg_window {
417 abi_ulong locals[8];
418 abi_ulong ins[8];
421 #define TARGET_STACK_BIAS 2047
423 /* {set, get}context() needed for 64-bit SparcLinux userland. */
424 void sparc64_set_context(CPUSPARCState *env)
426 abi_ulong ucp_addr;
427 struct target_ucontext *ucp;
428 target_mc_gregset_t *grp;
429 abi_ulong pc, npc, tstate;
430 abi_ulong fp, i7, w_addr;
431 unsigned int i;
433 ucp_addr = env->regwptr[UREG_I0];
434 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
435 goto do_sigsegv;
437 grp = &ucp->tuc_mcontext.mc_gregs;
438 __get_user(pc, &((*grp)[SPARC_MC_PC]));
439 __get_user(npc, &((*grp)[SPARC_MC_NPC]));
440 if ((pc | npc) & 3) {
441 goto do_sigsegv;
443 if (env->regwptr[UREG_I1]) {
444 target_sigset_t target_set;
445 sigset_t set;
447 if (TARGET_NSIG_WORDS == 1) {
448 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
449 } else {
450 abi_ulong *src, *dst;
451 src = ucp->tuc_sigmask.sig;
452 dst = target_set.sig;
453 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
454 __get_user(*dst, src);
457 target_to_host_sigset_internal(&set, &target_set);
458 set_sigmask(&set);
460 env->pc = pc;
461 env->npc = npc;
462 __get_user(env->y, &((*grp)[SPARC_MC_Y]));
463 __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
464 env->asi = (tstate >> 24) & 0xff;
465 cpu_put_ccr(env, tstate >> 32);
466 cpu_put_cwp64(env, tstate & 0x1f);
467 __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
468 __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
469 __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
470 __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
471 __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
472 __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
473 __get_user(env->gregs[7], (&(*grp)[SPARC_MC_G7]));
474 __get_user(env->regwptr[UREG_I0], (&(*grp)[SPARC_MC_O0]));
475 __get_user(env->regwptr[UREG_I1], (&(*grp)[SPARC_MC_O1]));
476 __get_user(env->regwptr[UREG_I2], (&(*grp)[SPARC_MC_O2]));
477 __get_user(env->regwptr[UREG_I3], (&(*grp)[SPARC_MC_O3]));
478 __get_user(env->regwptr[UREG_I4], (&(*grp)[SPARC_MC_O4]));
479 __get_user(env->regwptr[UREG_I5], (&(*grp)[SPARC_MC_O5]));
480 __get_user(env->regwptr[UREG_I6], (&(*grp)[SPARC_MC_O6]));
481 __get_user(env->regwptr[UREG_I7], (&(*grp)[SPARC_MC_O7]));
483 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
484 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
486 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
487 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
488 abi_ulong) != 0) {
489 goto do_sigsegv;
491 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
492 abi_ulong) != 0) {
493 goto do_sigsegv;
495 /* FIXME this does not match how the kernel handles the FPU in
496 * its sparc64_set_context implementation. In particular the FPU
497 * is only restored if fenab is non-zero in:
498 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
500 __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
502 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
503 for (i = 0; i < 64; i++, src++) {
504 if (i & 1) {
505 __get_user(env->fpr[i/2].l.lower, src);
506 } else {
507 __get_user(env->fpr[i/2].l.upper, src);
511 __get_user(env->fsr,
512 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
513 __get_user(env->gsr,
514 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
515 unlock_user_struct(ucp, ucp_addr, 0);
516 return;
517 do_sigsegv:
518 unlock_user_struct(ucp, ucp_addr, 0);
519 force_sig(TARGET_SIGSEGV);
522 void sparc64_get_context(CPUSPARCState *env)
524 abi_ulong ucp_addr;
525 struct target_ucontext *ucp;
526 target_mc_gregset_t *grp;
527 target_mcontext_t *mcp;
528 abi_ulong fp, i7, w_addr;
529 int err;
530 unsigned int i;
531 target_sigset_t target_set;
532 sigset_t set;
534 ucp_addr = env->regwptr[UREG_I0];
535 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
536 goto do_sigsegv;
539 mcp = &ucp->tuc_mcontext;
540 grp = &mcp->mc_gregs;
542 /* Skip over the trap instruction, first. */
543 env->pc = env->npc;
544 env->npc += 4;
546 /* If we're only reading the signal mask then do_sigprocmask()
547 * is guaranteed not to fail, which is important because we don't
548 * have any way to signal a failure or restart this operation since
549 * this is not a normal syscall.
551 err = do_sigprocmask(0, NULL, &set);
552 assert(err == 0);
553 host_to_target_sigset_internal(&target_set, &set);
554 if (TARGET_NSIG_WORDS == 1) {
555 __put_user(target_set.sig[0],
556 (abi_ulong *)&ucp->tuc_sigmask);
557 } else {
558 abi_ulong *src, *dst;
559 src = target_set.sig;
560 dst = ucp->tuc_sigmask.sig;
561 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
562 __put_user(*src, dst);
564 if (err)
565 goto do_sigsegv;
568 /* XXX: tstate must be saved properly */
569 // __put_user(env->tstate, &((*grp)[SPARC_MC_TSTATE]));
570 __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
571 __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
572 __put_user(env->y, &((*grp)[SPARC_MC_Y]));
573 __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
574 __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
575 __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
576 __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
577 __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
578 __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
579 __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
580 __put_user(env->regwptr[UREG_I0], &((*grp)[SPARC_MC_O0]));
581 __put_user(env->regwptr[UREG_I1], &((*grp)[SPARC_MC_O1]));
582 __put_user(env->regwptr[UREG_I2], &((*grp)[SPARC_MC_O2]));
583 __put_user(env->regwptr[UREG_I3], &((*grp)[SPARC_MC_O3]));
584 __put_user(env->regwptr[UREG_I4], &((*grp)[SPARC_MC_O4]));
585 __put_user(env->regwptr[UREG_I5], &((*grp)[SPARC_MC_O5]));
586 __put_user(env->regwptr[UREG_I6], &((*grp)[SPARC_MC_O6]));
587 __put_user(env->regwptr[UREG_I7], &((*grp)[SPARC_MC_O7]));
589 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
590 fp = i7 = 0;
591 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
592 abi_ulong) != 0) {
593 goto do_sigsegv;
595 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
596 abi_ulong) != 0) {
597 goto do_sigsegv;
599 __put_user(fp, &(mcp->mc_fp));
600 __put_user(i7, &(mcp->mc_i7));
603 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
604 for (i = 0; i < 64; i++, dst++) {
605 if (i & 1) {
606 __put_user(env->fpr[i/2].l.lower, dst);
607 } else {
608 __put_user(env->fpr[i/2].l.upper, dst);
612 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
613 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
614 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
616 if (err)
617 goto do_sigsegv;
618 unlock_user_struct(ucp, ucp_addr, 1);
619 return;
620 do_sigsegv:
621 unlock_user_struct(ucp, ucp_addr, 1);
622 force_sig(TARGET_SIGSEGV);
624 #endif