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 /* A Sparc register window */
25 struct target_reg_window
{
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.
46 struct target_siginfo_fpu
{
47 /* It is more convenient for qemu to move doubles, not singles. */
48 uint64_t si_double_regs
[16];
57 struct target_signal_frame
{
58 struct target_stackf ss
;
59 struct target_pt_regs regs
;
62 uint32_t insns
[2] QEMU_ALIGNED(8);
63 abi_ulong extramask
[TARGET_NSIG_WORDS
- 1];
64 abi_ulong extra_size
; /* Should be 0 */
68 static abi_ulong
get_sigframe(struct target_sigaction
*sa
,
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
))) {
82 /* This is the X/Open sanctioned signal stack switching. */
83 sp
= target_sigsp(sp
, sa
) - framesize
;
86 * Always align the stack frame. This handles two cases. First,
87 * sigaltstack need not be mindful of platform specific stack
88 * alignment. Second, if we took this signal because the stack
89 * is not aligned properly, we'd like to take the signal cleanly
97 static void save_pt_regs(struct target_pt_regs
*regs
, CPUSPARCState
*env
)
101 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
102 __put_user(sparc64_tstate(env
), ®s
->tstate
);
103 /* TODO: magic should contain PT_REG_MAGIC + %tt. */
104 __put_user(0, ®s
->magic
);
106 __put_user(cpu_get_psr(env
), ®s
->psr
);
109 __put_user(env
->pc
, ®s
->pc
);
110 __put_user(env
->npc
, ®s
->npc
);
111 __put_user(env
->y
, ®s
->y
);
113 for (i
= 0; i
< 8; i
++) {
114 __put_user(env
->gregs
[i
], ®s
->u_regs
[i
]);
116 for (i
= 0; i
< 8; i
++) {
117 __put_user(env
->regwptr
[WREG_O0
+ i
], ®s
->u_regs
[i
+ 8]);
121 static void restore_pt_regs(struct target_pt_regs
*regs
, CPUSPARCState
*env
)
125 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
126 /* User can only change condition codes and %asi in %tstate. */
128 __get_user(tstate
, ®s
->tstate
);
129 cpu_put_ccr(env
, tstate
>> 32);
130 env
->asi
= extract64(tstate
, 24, 8);
133 * User can only change condition codes and FPU enabling in %psr.
134 * But don't bother with FPU enabling, since a real kernel would
135 * just re-enable the FPU upon the next fpu trap.
138 __get_user(psr
, ®s
->psr
);
139 env
->psr
= (psr
& PSR_ICC
) | (env
->psr
& ~PSR_ICC
);
142 /* Note that pc and npc are handled in the caller. */
144 __get_user(env
->y
, ®s
->y
);
146 for (i
= 0; i
< 8; i
++) {
147 __get_user(env
->gregs
[i
], ®s
->u_regs
[i
]);
149 for (i
= 0; i
< 8; i
++) {
150 __get_user(env
->regwptr
[WREG_O0
+ i
], ®s
->u_regs
[i
+ 8]);
154 static void save_reg_win(struct target_reg_window
*win
, CPUSPARCState
*env
)
158 for (i
= 0; i
< 8; i
++) {
159 __put_user(env
->regwptr
[i
+ WREG_L0
], &win
->locals
[i
]);
161 for (i
= 0; i
< 8; i
++) {
162 __put_user(env
->regwptr
[i
+ WREG_I0
], &win
->ins
[i
]);
166 static void save_fpu(struct target_siginfo_fpu
*fpu
, CPUSPARCState
*env
)
170 for (i
= 0; i
< 16; ++i
) {
171 __put_user(env
->fpr
[i
].ll
, &fpu
->si_double_regs
[i
]);
173 __put_user(env
->fsr
, &fpu
->si_fsr
);
174 __put_user(0, &fpu
->si_fpqdepth
);
177 static void restore_fpu(struct target_siginfo_fpu
*fpu
, CPUSPARCState
*env
)
181 for (i
= 0; i
< 16; ++i
) {
182 __get_user(env
->fpr
[i
].ll
, &fpu
->si_double_regs
[i
]);
184 __get_user(env
->fsr
, &fpu
->si_fsr
);
187 void setup_frame(int sig
, struct target_sigaction
*ka
,
188 target_sigset_t
*set
, CPUSPARCState
*env
)
191 struct target_signal_frame
*sf
;
192 size_t sf_size
= sizeof(*sf
) + sizeof(struct target_siginfo_fpu
);
195 /* 1. Make sure everything is clean */
197 sf_addr
= get_sigframe(ka
, env
, sf_size
);
198 trace_user_setup_frame(env
, sf_addr
);
200 sf
= lock_user(VERIFY_WRITE
, sf_addr
, sf_size
, 0);
205 /* 2. Save the current process state */
206 save_pt_regs(&sf
->regs
, env
);
207 __put_user(0, &sf
->extra_size
);
209 save_fpu((struct target_siginfo_fpu
*)(sf
+ 1), env
);
210 __put_user(sf_addr
+ sizeof(*sf
), &sf
->fpu_save
);
212 __put_user(0, &sf
->rwin_save
); /* TODO: save_rwin_state */
214 __put_user(set
->sig
[0], &sf
->si_mask
);
215 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
216 __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
219 save_reg_win(&sf
->ss
.win
, env
);
221 /* 3. signal handler back-trampoline and parameters */
222 env
->regwptr
[WREG_SP
] = sf_addr
;
223 env
->regwptr
[WREG_O0
] = sig
;
224 env
->regwptr
[WREG_O1
] = sf_addr
+
225 offsetof(struct target_signal_frame
, regs
);
226 env
->regwptr
[WREG_O2
] = sf_addr
+
227 offsetof(struct target_signal_frame
, regs
);
229 /* 4. signal handler */
230 env
->pc
= ka
->_sa_handler
;
231 env
->npc
= (env
->pc
+ 4);
232 /* 5. return to kernel instructions */
233 if (ka
->ka_restorer
) {
234 env
->regwptr
[WREG_O7
] = ka
->ka_restorer
;
238 env
->regwptr
[WREG_O7
] = sf_addr
+
239 offsetof(struct target_signal_frame
, insns
) - 2 * 4;
241 /* mov __NR_sigreturn, %g1 */
243 __put_user(val32
, &sf
->insns
[0]);
247 __put_user(val32
, &sf
->insns
[1]);
249 unlock_user(sf
, sf_addr
, sf_size
);
253 force_sig(TARGET_SIGILL
);
256 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
260 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
261 target_siginfo_t
*info
,
262 target_sigset_t
*set
, CPUSPARCState
*env
)
264 qemu_log_mask(LOG_UNIMP
, "setup_rt_frame: not implemented\n");
267 long do_sigreturn(CPUSPARCState
*env
)
270 struct target_signal_frame
*sf
;
271 abi_ulong pc
, npc
, ptr
;
276 sf_addr
= env
->regwptr
[WREG_SP
];
277 trace_user_do_sigreturn(env
, sf_addr
);
278 if (!lock_user_struct(VERIFY_READ
, sf
, sf_addr
, 1)) {
282 /* 1. Make sure we are not getting garbage from the user */
287 __get_user(pc
, &sf
->regs
.pc
);
288 __get_user(npc
, &sf
->regs
.npc
);
290 if ((pc
| npc
) & 3) {
294 /* 2. Restore the state */
295 restore_pt_regs(&sf
->regs
, env
);
299 __get_user(ptr
, &sf
->fpu_save
);
301 struct target_siginfo_fpu
*fpu
;
302 if ((ptr
& 3) || !lock_user_struct(VERIFY_READ
, fpu
, ptr
, 1)) {
305 restore_fpu(fpu
, env
);
306 unlock_user_struct(fpu
, ptr
, 0);
309 __get_user(ptr
, &sf
->rwin_save
);
311 goto segv_and_exit
; /* TODO: restore_rwin */
314 __get_user(set
.sig
[0], &sf
->si_mask
);
315 for (i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
316 __get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]);
319 target_to_host_sigset_internal(&host_set
, &set
);
320 set_sigmask(&host_set
);
322 unlock_user_struct(sf
, sf_addr
, 0);
323 return -TARGET_QEMU_ESIGRETURN
;
326 unlock_user_struct(sf
, sf_addr
, 0);
327 force_sig(TARGET_SIGSEGV
);
328 return -TARGET_QEMU_ESIGRETURN
;
331 long do_rt_sigreturn(CPUSPARCState
*env
)
333 trace_user_do_rt_sigreturn(env
, 0);
334 qemu_log_mask(LOG_UNIMP
, "do_rt_sigreturn: not implemented\n");
335 return -TARGET_ENOSYS
;
338 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
339 #define SPARC_MC_TSTATE 0
340 #define SPARC_MC_PC 1
341 #define SPARC_MC_NPC 2
343 #define SPARC_MC_G1 4
344 #define SPARC_MC_G2 5
345 #define SPARC_MC_G3 6
346 #define SPARC_MC_G4 7
347 #define SPARC_MC_G5 8
348 #define SPARC_MC_G6 9
349 #define SPARC_MC_G7 10
350 #define SPARC_MC_O0 11
351 #define SPARC_MC_O1 12
352 #define SPARC_MC_O2 13
353 #define SPARC_MC_O3 14
354 #define SPARC_MC_O4 15
355 #define SPARC_MC_O5 16
356 #define SPARC_MC_O6 17
357 #define SPARC_MC_O7 18
358 #define SPARC_MC_NGREG 19
360 typedef abi_ulong target_mc_greg_t
;
361 typedef target_mc_greg_t target_mc_gregset_t
[SPARC_MC_NGREG
];
363 struct target_mc_fq
{
369 * Note the manual 16-alignment; the kernel gets this because it
370 * includes a "long double qregs[16]" in the mcpu_fregs union,
373 struct target_mc_fpu
{
377 //uint128_t qregs[16];
380 abi_ulong mcfpu_fprs
;
383 unsigned char mcfpu_qcnt
;
384 unsigned char mcfpu_qentsz
;
385 unsigned char mcfpu_enab
;
386 } __attribute__((aligned(16)));
387 typedef struct target_mc_fpu target_mc_fpu_t
;
390 target_mc_gregset_t mc_gregs
;
391 target_mc_greg_t mc_fp
;
392 target_mc_greg_t mc_i7
;
393 target_mc_fpu_t mc_fpregs
;
396 struct target_ucontext
{
399 target_sigset_t tuc_sigmask
;
400 target_mcontext_t tuc_mcontext
;
403 /* {set, get}context() needed for 64-bit SparcLinux userland. */
404 void sparc64_set_context(CPUSPARCState
*env
)
407 struct target_ucontext
*ucp
;
408 target_mc_gregset_t
*grp
;
409 target_mc_fpu_t
*fpup
;
410 abi_ulong pc
, npc
, tstate
;
414 ucp_addr
= env
->regwptr
[WREG_O0
];
415 if (!lock_user_struct(VERIFY_READ
, ucp
, ucp_addr
, 1)) {
418 grp
= &ucp
->tuc_mcontext
.mc_gregs
;
419 __get_user(pc
, &((*grp
)[SPARC_MC_PC
]));
420 __get_user(npc
, &((*grp
)[SPARC_MC_NPC
]));
421 if ((pc
| npc
) & 3) {
424 if (env
->regwptr
[WREG_O1
]) {
425 target_sigset_t target_set
;
428 if (TARGET_NSIG_WORDS
== 1) {
429 __get_user(target_set
.sig
[0], &ucp
->tuc_sigmask
.sig
[0]);
431 abi_ulong
*src
, *dst
;
432 src
= ucp
->tuc_sigmask
.sig
;
433 dst
= target_set
.sig
;
434 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
435 __get_user(*dst
, src
);
438 target_to_host_sigset_internal(&set
, &target_set
);
443 __get_user(env
->y
, &((*grp
)[SPARC_MC_Y
]));
444 __get_user(tstate
, &((*grp
)[SPARC_MC_TSTATE
]));
445 /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
446 env
->asi
= (tstate
>> 24) & 0xff;
447 cpu_put_ccr(env
, (tstate
>> 32) & 0xff);
448 __get_user(env
->gregs
[1], (&(*grp
)[SPARC_MC_G1
]));
449 __get_user(env
->gregs
[2], (&(*grp
)[SPARC_MC_G2
]));
450 __get_user(env
->gregs
[3], (&(*grp
)[SPARC_MC_G3
]));
451 __get_user(env
->gregs
[4], (&(*grp
)[SPARC_MC_G4
]));
452 __get_user(env
->gregs
[5], (&(*grp
)[SPARC_MC_G5
]));
453 __get_user(env
->gregs
[6], (&(*grp
)[SPARC_MC_G6
]));
454 /* Skip g7 as that's the thread register in userspace */
457 * Note that unlike the kernel, we didn't need to mess with the
458 * guest register window state to save it into a pt_regs to run
459 * the kernel. So for us the guest's O regs are still in WREG_O*
460 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
461 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
462 * need to be written back to userspace memory.
464 __get_user(env
->regwptr
[WREG_O0
], (&(*grp
)[SPARC_MC_O0
]));
465 __get_user(env
->regwptr
[WREG_O1
], (&(*grp
)[SPARC_MC_O1
]));
466 __get_user(env
->regwptr
[WREG_O2
], (&(*grp
)[SPARC_MC_O2
]));
467 __get_user(env
->regwptr
[WREG_O3
], (&(*grp
)[SPARC_MC_O3
]));
468 __get_user(env
->regwptr
[WREG_O4
], (&(*grp
)[SPARC_MC_O4
]));
469 __get_user(env
->regwptr
[WREG_O5
], (&(*grp
)[SPARC_MC_O5
]));
470 __get_user(env
->regwptr
[WREG_O6
], (&(*grp
)[SPARC_MC_O6
]));
471 __get_user(env
->regwptr
[WREG_O7
], (&(*grp
)[SPARC_MC_O7
]));
473 __get_user(env
->regwptr
[WREG_FP
], &(ucp
->tuc_mcontext
.mc_fp
));
474 __get_user(env
->regwptr
[WREG_I7
], &(ucp
->tuc_mcontext
.mc_i7
));
476 fpup
= &ucp
->tuc_mcontext
.mc_fpregs
;
478 __get_user(fenab
, &(fpup
->mcfpu_enab
));
483 * We use the FPRS from the guest only in deciding whether
484 * to restore the upper, lower, or both banks of the FPU regs.
485 * The kernel here writes the FPU register data into the
486 * process's current_thread_info state and unconditionally
487 * clears FPRS and TSTATE_PEF: this disables the FPU so that the
488 * next FPU-disabled trap will copy the data out of
489 * current_thread_info and into the real FPU registers.
490 * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
491 * so we always load the data directly into the FPU registers
492 * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
493 * Note that because we (and the kernel) always write zeroes for
494 * the fenab and fprs in sparc64_get_context() none of this code
495 * will execute unless the guest manually constructed or changed
496 * the context structure.
498 __get_user(fprs
, &(fpup
->mcfpu_fprs
));
499 if (fprs
& FPRS_DL
) {
500 for (i
= 0; i
< 16; i
++) {
501 __get_user(env
->fpr
[i
].ll
, &(fpup
->mcfpu_fregs
.dregs
[i
]));
504 if (fprs
& FPRS_DU
) {
505 for (i
= 16; i
< 32; i
++) {
506 __get_user(env
->fpr
[i
].ll
, &(fpup
->mcfpu_fregs
.dregs
[i
]));
509 __get_user(env
->fsr
, &(fpup
->mcfpu_fsr
));
510 __get_user(env
->gsr
, &(fpup
->mcfpu_gsr
));
512 unlock_user_struct(ucp
, ucp_addr
, 0);
515 unlock_user_struct(ucp
, ucp_addr
, 0);
516 force_sig(TARGET_SIGSEGV
);
519 void sparc64_get_context(CPUSPARCState
*env
)
522 struct target_ucontext
*ucp
;
523 target_mc_gregset_t
*grp
;
524 target_mcontext_t
*mcp
;
527 target_sigset_t target_set
;
530 ucp_addr
= env
->regwptr
[WREG_O0
];
531 if (!lock_user_struct(VERIFY_WRITE
, ucp
, ucp_addr
, 0)) {
535 memset(ucp
, 0, sizeof(*ucp
));
537 mcp
= &ucp
->tuc_mcontext
;
538 grp
= &mcp
->mc_gregs
;
540 /* Skip over the trap instruction, first. */
544 /* If we're only reading the signal mask then do_sigprocmask()
545 * is guaranteed not to fail, which is important because we don't
546 * have any way to signal a failure or restart this operation since
547 * this is not a normal syscall.
549 err
= do_sigprocmask(0, NULL
, &set
);
551 host_to_target_sigset_internal(&target_set
, &set
);
552 if (TARGET_NSIG_WORDS
== 1) {
553 __put_user(target_set
.sig
[0],
554 (abi_ulong
*)&ucp
->tuc_sigmask
);
556 abi_ulong
*src
, *dst
;
557 src
= target_set
.sig
;
558 dst
= ucp
->tuc_sigmask
.sig
;
559 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
560 __put_user(*src
, dst
);
564 __put_user(sparc64_tstate(env
), &((*grp
)[SPARC_MC_TSTATE
]));
565 __put_user(env
->pc
, &((*grp
)[SPARC_MC_PC
]));
566 __put_user(env
->npc
, &((*grp
)[SPARC_MC_NPC
]));
567 __put_user(env
->y
, &((*grp
)[SPARC_MC_Y
]));
568 __put_user(env
->gregs
[1], &((*grp
)[SPARC_MC_G1
]));
569 __put_user(env
->gregs
[2], &((*grp
)[SPARC_MC_G2
]));
570 __put_user(env
->gregs
[3], &((*grp
)[SPARC_MC_G3
]));
571 __put_user(env
->gregs
[4], &((*grp
)[SPARC_MC_G4
]));
572 __put_user(env
->gregs
[5], &((*grp
)[SPARC_MC_G5
]));
573 __put_user(env
->gregs
[6], &((*grp
)[SPARC_MC_G6
]));
574 __put_user(env
->gregs
[7], &((*grp
)[SPARC_MC_G7
]));
577 * Note that unlike the kernel, we didn't need to mess with the
578 * guest register window state to save it into a pt_regs to run
579 * the kernel. So for us the guest's O regs are still in WREG_O*
580 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
581 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
582 * need to be fished out of userspace memory.
584 __put_user(env
->regwptr
[WREG_O0
], &((*grp
)[SPARC_MC_O0
]));
585 __put_user(env
->regwptr
[WREG_O1
], &((*grp
)[SPARC_MC_O1
]));
586 __put_user(env
->regwptr
[WREG_O2
], &((*grp
)[SPARC_MC_O2
]));
587 __put_user(env
->regwptr
[WREG_O3
], &((*grp
)[SPARC_MC_O3
]));
588 __put_user(env
->regwptr
[WREG_O4
], &((*grp
)[SPARC_MC_O4
]));
589 __put_user(env
->regwptr
[WREG_O5
], &((*grp
)[SPARC_MC_O5
]));
590 __put_user(env
->regwptr
[WREG_O6
], &((*grp
)[SPARC_MC_O6
]));
591 __put_user(env
->regwptr
[WREG_O7
], &((*grp
)[SPARC_MC_O7
]));
593 __put_user(env
->regwptr
[WREG_FP
], &(mcp
->mc_fp
));
594 __put_user(env
->regwptr
[WREG_I7
], &(mcp
->mc_i7
));
597 * We don't write out the FPU state. This matches the kernel's
598 * implementation (which has the code for doing this but
599 * hidden behind an "if (fenab)" where fenab is always 0).
602 unlock_user_struct(ucp
, ucp_addr
, 1);
605 unlock_user_struct(ucp
, ucp_addr
, 1);
606 force_sig(TARGET_SIGSEGV
);