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"
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 */
50 } sigc_wbuf
[__SUNOS_MAXWIN
];
52 /* A Sparc stack frame */
56 /* It's simpler to treat fp and callers_pc as elements of ins[]
57 * since we never need to access them ourselves.
70 abi_ulong u_regs
[16]; /* globals and ins */
76 abi_ulong si_float_regs
[32];
78 unsigned long si_fpqdepth
;
80 unsigned long *insn_addr
;
86 struct target_signal_frame
{
87 struct sparc_stackf ss
;
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
;
101 unsigned int insns
[2];
103 unsigned int extra_size
; /* Should be 0 */
104 qemu_siginfo_fpu_t fpu_state
;
118 #define UREG_FP UREG_I6
119 #define UREG_SP UREG_O6
121 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
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
))) {
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
150 setup___siginfo(__siginfo_t
*si
, CPUSPARCState
*env
, abi_ulong mask
)
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
);
170 setup_sigcontext(struct target_sigcontext
*sc
, /*struct _fpstate *fpstate,*/
171 CPUSPARCState
*env
, unsigned long mask
)
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
);
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
)
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);
208 if (invalid_frame_pointer(sf
, sigframe_size
))
209 goto sigill_and_return
;
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
]);
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
;
249 env
->regwptr
[UREG_I7
] = sf_addr
+
250 offsetof(struct target_signal_frame
, insns
) - 2 * 4;
252 /* mov __NR_sigreturn, %g1 */
254 __put_user(val32
, &sf
->insns
[0]);
258 __put_user(val32
, &sf
->insns
[1]);
262 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
266 force_sig(TARGET_SIGILL
);
269 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
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
)
283 struct target_signal_frame
*sf
;
284 uint32_t up_psr
, pc
, npc
;
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)) {
295 /* 1. Make sure we are not getting garbage from the user */
300 __get_user(pc
, &sf
->info
.si_regs
.pc
);
301 __get_user(npc
, &sf
->info
.si_regs
.npc
);
303 if ((pc
| npc
) & 3) {
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 */));
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);
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
);
344 unlock_user_struct(sf
, sf_addr
, 0);
345 return -TARGET_QEMU_ESIGRETURN
;
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
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
;
390 struct target_mc_fpu
{
394 //uint128_t qregs[16];
397 abi_ulong mcfpu_fprs
;
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
;
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
;
413 struct target_ucontext
{
414 struct target_ucontext
*tuc_link
;
416 target_sigset_t tuc_sigmask
;
417 target_mcontext_t tuc_mcontext
;
420 /* A V9 register window */
421 struct target_reg_window
{
426 #define TARGET_STACK_BIAS 2047
428 /* {set, get}context() needed for 64-bit SparcLinux userland. */
429 void sparc64_set_context(CPUSPARCState
*env
)
432 struct target_ucontext
*ucp
;
433 target_mc_gregset_t
*grp
;
434 abi_ulong pc
, npc
, tstate
;
435 abi_ulong fp
, i7
, w_addr
;
438 ucp_addr
= env
->regwptr
[UREG_I0
];
439 if (!lock_user_struct(VERIFY_READ
, ucp
, ucp_addr
, 1)) {
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) {
448 if (env
->regwptr
[UREG_I1
]) {
449 target_sigset_t target_set
;
452 if (TARGET_NSIG_WORDS
== 1) {
453 __get_user(target_set
.sig
[0], &ucp
->tuc_sigmask
.sig
[0]);
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
);
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]),
496 if (put_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
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
++) {
510 __get_user(env
->fpr
[i
/2].l
.lower
, src
);
512 __get_user(env
->fpr
[i
/2].l
.upper
, src
);
517 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_fsr
));
519 &(ucp
->tuc_mcontext
.mc_fpregs
.mcfpu_gsr
));
520 unlock_user_struct(ucp
, ucp_addr
, 0);
523 unlock_user_struct(ucp
, ucp_addr
, 0);
524 force_sig(TARGET_SIGSEGV
);
527 void sparc64_get_context(CPUSPARCState
*env
)
530 struct target_ucontext
*ucp
;
531 target_mc_gregset_t
*grp
;
532 target_mcontext_t
*mcp
;
533 abi_ulong fp
, i7
, w_addr
;
536 target_sigset_t target_set
;
539 ucp_addr
= env
->regwptr
[UREG_I0
];
540 if (!lock_user_struct(VERIFY_WRITE
, ucp
, ucp_addr
, 0)) {
544 mcp
= &ucp
->tuc_mcontext
;
545 grp
= &mcp
->mc_gregs
;
547 /* Skip over the trap instruction, first. */
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
);
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
);
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
);
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
];
596 if (get_user(fp
, w_addr
+ offsetof(struct target_reg_window
, ins
[6]),
600 if (get_user(i7
, w_addr
+ offsetof(struct target_reg_window
, ins
[7]),
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
++) {
611 __put_user(env
->fpr
[i
/2].l
.lower
, dst
);
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
));
623 unlock_user_struct(ucp
, ucp_addr
, 1);
626 unlock_user_struct(ucp
, ucp_addr
, 1);
627 force_sig(TARGET_SIGSEGV
);