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 stack frame */
28 /* It's simpler to treat fp and callers_pc as elements of ins[]
29 * since we never need to access them ourselves.
42 abi_ulong u_regs
[16]; /* globals and ins */
48 abi_ulong si_float_regs
[32];
50 unsigned long si_fpqdepth
;
52 unsigned long *insn_addr
;
58 struct target_signal_frame
{
59 struct sparc_stackf ss
;
62 uint32_t insns
[2] QEMU_ALIGNED(8);
63 abi_ulong extramask
[TARGET_NSIG_WORDS
- 1];
64 abi_ulong extra_size
; /* Should be 0 */
65 qemu_siginfo_fpu_t fpu_state
;
68 static inline abi_ulong
get_sigframe(struct target_sigaction
*sa
,
70 unsigned long framesize
)
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
;
85 /* Always align the stack frame. This handles two cases. First,
86 * sigaltstack need not be mindful of platform specific stack
87 * alignment. Second, if we took this signal because the stack
88 * is not aligned properly, we'd like to take the signal cleanly
97 setup___siginfo(__siginfo_t
*si
, CPUSPARCState
*env
, abi_ulong mask
)
101 __put_user(env
->psr
, &si
->si_regs
.psr
);
102 __put_user(env
->pc
, &si
->si_regs
.pc
);
103 __put_user(env
->npc
, &si
->si_regs
.npc
);
104 __put_user(env
->y
, &si
->si_regs
.y
);
105 for (i
=0; i
< 8; i
++) {
106 __put_user(env
->gregs
[i
], &si
->si_regs
.u_regs
[i
]);
108 for (i
=0; i
< 8; i
++) {
109 __put_user(env
->regwptr
[WREG_O0
+ i
], &si
->si_regs
.u_regs
[i
+ 8]);
111 __put_user(mask
, &si
->si_mask
);
115 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
117 void setup_frame(int sig
, struct target_sigaction
*ka
,
118 target_sigset_t
*set
, CPUSPARCState
*env
)
121 struct target_signal_frame
*sf
;
122 int sigframe_size
, err
, i
;
124 /* 1. Make sure everything is clean */
125 //synchronize_user_stack();
127 sigframe_size
= NF_ALIGNEDSZ
;
128 sf_addr
= get_sigframe(ka
, env
, sigframe_size
);
129 trace_user_setup_frame(env
, sf_addr
);
131 sf
= lock_user(VERIFY_WRITE
, sf_addr
,
132 sizeof(struct target_signal_frame
), 0);
137 if (invalid_frame_pointer(sf
, sigframe_size
))
138 goto sigill_and_return
;
140 /* 2. Save the current process state */
141 err
= setup___siginfo(&sf
->info
, env
, set
->sig
[0]);
142 __put_user(0, &sf
->extra_size
);
144 //save_fpu_state(regs, &sf->fpu_state);
145 //__put_user(&sf->fpu_state, &sf->fpu_save);
147 __put_user(set
->sig
[0], &sf
->info
.si_mask
);
148 for (i
= 0; i
< TARGET_NSIG_WORDS
- 1; i
++) {
149 __put_user(set
->sig
[i
+ 1], &sf
->extramask
[i
]);
152 for (i
= 0; i
< 8; i
++) {
153 __put_user(env
->regwptr
[i
+ WREG_L0
], &sf
->ss
.locals
[i
]);
155 for (i
= 0; i
< 8; i
++) {
156 __put_user(env
->regwptr
[i
+ WREG_I0
], &sf
->ss
.ins
[i
]);
161 /* 3. signal handler back-trampoline and parameters */
162 env
->regwptr
[WREG_SP
] = sf_addr
;
163 env
->regwptr
[WREG_O0
] = sig
;
164 env
->regwptr
[WREG_O1
] = sf_addr
+
165 offsetof(struct target_signal_frame
, info
);
166 env
->regwptr
[WREG_O2
] = sf_addr
+
167 offsetof(struct target_signal_frame
, info
);
169 /* 4. signal handler */
170 env
->pc
= ka
->_sa_handler
;
171 env
->npc
= (env
->pc
+ 4);
172 /* 5. return to kernel instructions */
173 if (ka
->ka_restorer
) {
174 env
->regwptr
[WREG_O7
] = ka
->ka_restorer
;
178 env
->regwptr
[WREG_O7
] = sf_addr
+
179 offsetof(struct target_signal_frame
, insns
) - 2 * 4;
181 /* mov __NR_sigreturn, %g1 */
183 __put_user(val32
, &sf
->insns
[0]);
187 __put_user(val32
, &sf
->insns
[1]);
189 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
193 force_sig(TARGET_SIGILL
);
196 unlock_user(sf
, sf_addr
, sizeof(struct target_signal_frame
));
200 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
201 target_siginfo_t
*info
,
202 target_sigset_t
*set
, CPUSPARCState
*env
)
204 qemu_log_mask(LOG_UNIMP
, "setup_rt_frame: not implemented\n");
207 long do_sigreturn(CPUSPARCState
*env
)
210 struct target_signal_frame
*sf
;
211 abi_ulong up_psr
, pc
, npc
;
216 sf_addr
= env
->regwptr
[WREG_SP
];
217 trace_user_do_sigreturn(env
, sf_addr
);
218 if (!lock_user_struct(VERIFY_READ
, sf
, sf_addr
, 1)) {
222 /* 1. Make sure we are not getting garbage from the user */
227 __get_user(pc
, &sf
->info
.si_regs
.pc
);
228 __get_user(npc
, &sf
->info
.si_regs
.npc
);
230 if ((pc
| npc
) & 3) {
234 /* 2. Restore the state */
235 __get_user(up_psr
, &sf
->info
.si_regs
.psr
);
237 /* User can only change condition codes and FPU enabling in %psr. */
238 env
->psr
= (up_psr
& (PSR_ICC
/* | PSR_EF */))
239 | (env
->psr
& ~(PSR_ICC
/* | PSR_EF */));
243 __get_user(env
->y
, &sf
->info
.si_regs
.y
);
244 for (i
=0; i
< 8; i
++) {
245 __get_user(env
->gregs
[i
], &sf
->info
.si_regs
.u_regs
[i
]);
247 for (i
=0; i
< 8; i
++) {
248 __get_user(env
->regwptr
[i
+ WREG_O0
], &sf
->info
.si_regs
.u_regs
[i
+ 8]);
251 /* FIXME: implement FPU save/restore:
252 * __get_user(fpu_save, &sf->fpu_save);
254 * if (restore_fpu_state(env, fpu_save)) {
255 * goto segv_and_exit;
260 /* This is pretty much atomic, no amount locking would prevent
261 * the races which exist anyways.
263 __get_user(set
.sig
[0], &sf
->info
.si_mask
);
264 for(i
= 1; i
< TARGET_NSIG_WORDS
; i
++) {
265 __get_user(set
.sig
[i
], &sf
->extramask
[i
- 1]);
268 target_to_host_sigset_internal(&host_set
, &set
);
269 set_sigmask(&host_set
);
271 unlock_user_struct(sf
, sf_addr
, 0);
272 return -TARGET_QEMU_ESIGRETURN
;
275 unlock_user_struct(sf
, sf_addr
, 0);
276 force_sig(TARGET_SIGSEGV
);
277 return -TARGET_QEMU_ESIGRETURN
;
280 long do_rt_sigreturn(CPUSPARCState
*env
)
282 trace_user_do_rt_sigreturn(env
, 0);
283 qemu_log_mask(LOG_UNIMP
, "do_rt_sigreturn: not implemented\n");
284 return -TARGET_ENOSYS
;
287 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
288 #define SPARC_MC_TSTATE 0
289 #define SPARC_MC_PC 1
290 #define SPARC_MC_NPC 2
292 #define SPARC_MC_G1 4
293 #define SPARC_MC_G2 5
294 #define SPARC_MC_G3 6
295 #define SPARC_MC_G4 7
296 #define SPARC_MC_G5 8
297 #define SPARC_MC_G6 9
298 #define SPARC_MC_G7 10
299 #define SPARC_MC_O0 11
300 #define SPARC_MC_O1 12
301 #define SPARC_MC_O2 13
302 #define SPARC_MC_O3 14
303 #define SPARC_MC_O4 15
304 #define SPARC_MC_O5 16
305 #define SPARC_MC_O6 17
306 #define SPARC_MC_O7 18
307 #define SPARC_MC_NGREG 19
309 typedef abi_ulong target_mc_greg_t
;
310 typedef target_mc_greg_t target_mc_gregset_t
[SPARC_MC_NGREG
];
312 struct target_mc_fq
{
318 * Note the manual 16-alignment; the kernel gets this because it
319 * includes a "long double qregs[16]" in the mcpu_fregs union,
322 struct target_mc_fpu
{
326 //uint128_t qregs[16];
329 abi_ulong mcfpu_fprs
;
332 unsigned char mcfpu_qcnt
;
333 unsigned char mcfpu_qentsz
;
334 unsigned char mcfpu_enab
;
335 } __attribute__((aligned(16)));
336 typedef struct target_mc_fpu target_mc_fpu_t
;
339 target_mc_gregset_t mc_gregs
;
340 target_mc_greg_t mc_fp
;
341 target_mc_greg_t mc_i7
;
342 target_mc_fpu_t mc_fpregs
;
345 struct target_ucontext
{
348 target_sigset_t tuc_sigmask
;
349 target_mcontext_t tuc_mcontext
;
352 /* A V9 register window */
353 struct target_reg_window
{
358 /* {set, get}context() needed for 64-bit SparcLinux userland. */
359 void sparc64_set_context(CPUSPARCState
*env
)
362 struct target_ucontext
*ucp
;
363 target_mc_gregset_t
*grp
;
364 target_mc_fpu_t
*fpup
;
365 abi_ulong pc
, npc
, tstate
;
369 ucp_addr
= env
->regwptr
[WREG_O0
];
370 if (!lock_user_struct(VERIFY_READ
, ucp
, ucp_addr
, 1)) {
373 grp
= &ucp
->tuc_mcontext
.mc_gregs
;
374 __get_user(pc
, &((*grp
)[SPARC_MC_PC
]));
375 __get_user(npc
, &((*grp
)[SPARC_MC_NPC
]));
376 if ((pc
| npc
) & 3) {
379 if (env
->regwptr
[WREG_O1
]) {
380 target_sigset_t target_set
;
383 if (TARGET_NSIG_WORDS
== 1) {
384 __get_user(target_set
.sig
[0], &ucp
->tuc_sigmask
.sig
[0]);
386 abi_ulong
*src
, *dst
;
387 src
= ucp
->tuc_sigmask
.sig
;
388 dst
= target_set
.sig
;
389 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
390 __get_user(*dst
, src
);
393 target_to_host_sigset_internal(&set
, &target_set
);
398 __get_user(env
->y
, &((*grp
)[SPARC_MC_Y
]));
399 __get_user(tstate
, &((*grp
)[SPARC_MC_TSTATE
]));
400 /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
401 env
->asi
= (tstate
>> 24) & 0xff;
402 cpu_put_ccr(env
, (tstate
>> 32) & 0xff);
403 __get_user(env
->gregs
[1], (&(*grp
)[SPARC_MC_G1
]));
404 __get_user(env
->gregs
[2], (&(*grp
)[SPARC_MC_G2
]));
405 __get_user(env
->gregs
[3], (&(*grp
)[SPARC_MC_G3
]));
406 __get_user(env
->gregs
[4], (&(*grp
)[SPARC_MC_G4
]));
407 __get_user(env
->gregs
[5], (&(*grp
)[SPARC_MC_G5
]));
408 __get_user(env
->gregs
[6], (&(*grp
)[SPARC_MC_G6
]));
409 /* Skip g7 as that's the thread register in userspace */
412 * Note that unlike the kernel, we didn't need to mess with the
413 * guest register window state to save it into a pt_regs to run
414 * the kernel. So for us the guest's O regs are still in WREG_O*
415 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
416 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
417 * need to be written back to userspace memory.
419 __get_user(env
->regwptr
[WREG_O0
], (&(*grp
)[SPARC_MC_O0
]));
420 __get_user(env
->regwptr
[WREG_O1
], (&(*grp
)[SPARC_MC_O1
]));
421 __get_user(env
->regwptr
[WREG_O2
], (&(*grp
)[SPARC_MC_O2
]));
422 __get_user(env
->regwptr
[WREG_O3
], (&(*grp
)[SPARC_MC_O3
]));
423 __get_user(env
->regwptr
[WREG_O4
], (&(*grp
)[SPARC_MC_O4
]));
424 __get_user(env
->regwptr
[WREG_O5
], (&(*grp
)[SPARC_MC_O5
]));
425 __get_user(env
->regwptr
[WREG_O6
], (&(*grp
)[SPARC_MC_O6
]));
426 __get_user(env
->regwptr
[WREG_O7
], (&(*grp
)[SPARC_MC_O7
]));
428 __get_user(env
->regwptr
[WREG_FP
], &(ucp
->tuc_mcontext
.mc_fp
));
429 __get_user(env
->regwptr
[WREG_I7
], &(ucp
->tuc_mcontext
.mc_i7
));
431 fpup
= &ucp
->tuc_mcontext
.mc_fpregs
;
433 __get_user(fenab
, &(fpup
->mcfpu_enab
));
438 * We use the FPRS from the guest only in deciding whether
439 * to restore the upper, lower, or both banks of the FPU regs.
440 * The kernel here writes the FPU register data into the
441 * process's current_thread_info state and unconditionally
442 * clears FPRS and TSTATE_PEF: this disables the FPU so that the
443 * next FPU-disabled trap will copy the data out of
444 * current_thread_info and into the real FPU registers.
445 * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
446 * so we always load the data directly into the FPU registers
447 * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
448 * Note that because we (and the kernel) always write zeroes for
449 * the fenab and fprs in sparc64_get_context() none of this code
450 * will execute unless the guest manually constructed or changed
451 * the context structure.
453 __get_user(fprs
, &(fpup
->mcfpu_fprs
));
454 if (fprs
& FPRS_DL
) {
455 for (i
= 0; i
< 16; i
++) {
456 __get_user(env
->fpr
[i
].ll
, &(fpup
->mcfpu_fregs
.dregs
[i
]));
459 if (fprs
& FPRS_DU
) {
460 for (i
= 16; i
< 32; i
++) {
461 __get_user(env
->fpr
[i
].ll
, &(fpup
->mcfpu_fregs
.dregs
[i
]));
464 __get_user(env
->fsr
, &(fpup
->mcfpu_fsr
));
465 __get_user(env
->gsr
, &(fpup
->mcfpu_gsr
));
467 unlock_user_struct(ucp
, ucp_addr
, 0);
470 unlock_user_struct(ucp
, ucp_addr
, 0);
471 force_sig(TARGET_SIGSEGV
);
474 void sparc64_get_context(CPUSPARCState
*env
)
477 struct target_ucontext
*ucp
;
478 target_mc_gregset_t
*grp
;
479 target_mcontext_t
*mcp
;
482 target_sigset_t target_set
;
485 ucp_addr
= env
->regwptr
[WREG_O0
];
486 if (!lock_user_struct(VERIFY_WRITE
, ucp
, ucp_addr
, 0)) {
490 memset(ucp
, 0, sizeof(*ucp
));
492 mcp
= &ucp
->tuc_mcontext
;
493 grp
= &mcp
->mc_gregs
;
495 /* Skip over the trap instruction, first. */
499 /* If we're only reading the signal mask then do_sigprocmask()
500 * is guaranteed not to fail, which is important because we don't
501 * have any way to signal a failure or restart this operation since
502 * this is not a normal syscall.
504 err
= do_sigprocmask(0, NULL
, &set
);
506 host_to_target_sigset_internal(&target_set
, &set
);
507 if (TARGET_NSIG_WORDS
== 1) {
508 __put_user(target_set
.sig
[0],
509 (abi_ulong
*)&ucp
->tuc_sigmask
);
511 abi_ulong
*src
, *dst
;
512 src
= target_set
.sig
;
513 dst
= ucp
->tuc_sigmask
.sig
;
514 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++, dst
++, src
++) {
515 __put_user(*src
, dst
);
519 __put_user(sparc64_tstate(env
), &((*grp
)[SPARC_MC_TSTATE
]));
520 __put_user(env
->pc
, &((*grp
)[SPARC_MC_PC
]));
521 __put_user(env
->npc
, &((*grp
)[SPARC_MC_NPC
]));
522 __put_user(env
->y
, &((*grp
)[SPARC_MC_Y
]));
523 __put_user(env
->gregs
[1], &((*grp
)[SPARC_MC_G1
]));
524 __put_user(env
->gregs
[2], &((*grp
)[SPARC_MC_G2
]));
525 __put_user(env
->gregs
[3], &((*grp
)[SPARC_MC_G3
]));
526 __put_user(env
->gregs
[4], &((*grp
)[SPARC_MC_G4
]));
527 __put_user(env
->gregs
[5], &((*grp
)[SPARC_MC_G5
]));
528 __put_user(env
->gregs
[6], &((*grp
)[SPARC_MC_G6
]));
529 __put_user(env
->gregs
[7], &((*grp
)[SPARC_MC_G7
]));
532 * Note that unlike the kernel, we didn't need to mess with the
533 * guest register window state to save it into a pt_regs to run
534 * the kernel. So for us the guest's O regs are still in WREG_O*
535 * (unlike the kernel which has put them in UREG_I* in a pt_regs)
536 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
537 * need to be fished out of userspace memory.
539 __put_user(env
->regwptr
[WREG_O0
], &((*grp
)[SPARC_MC_O0
]));
540 __put_user(env
->regwptr
[WREG_O1
], &((*grp
)[SPARC_MC_O1
]));
541 __put_user(env
->regwptr
[WREG_O2
], &((*grp
)[SPARC_MC_O2
]));
542 __put_user(env
->regwptr
[WREG_O3
], &((*grp
)[SPARC_MC_O3
]));
543 __put_user(env
->regwptr
[WREG_O4
], &((*grp
)[SPARC_MC_O4
]));
544 __put_user(env
->regwptr
[WREG_O5
], &((*grp
)[SPARC_MC_O5
]));
545 __put_user(env
->regwptr
[WREG_O6
], &((*grp
)[SPARC_MC_O6
]));
546 __put_user(env
->regwptr
[WREG_O7
], &((*grp
)[SPARC_MC_O7
]));
548 __put_user(env
->regwptr
[WREG_FP
], &(mcp
->mc_fp
));
549 __put_user(env
->regwptr
[WREG_I7
], &(mcp
->mc_i7
));
552 * We don't write out the FPU state. This matches the kernel's
553 * implementation (which has the code for doing this but
554 * hidden behind an "if (fenab)" where fenab is always 0).
557 unlock_user_struct(ucp
, ucp_addr
, 1);
560 unlock_user_struct(ucp
, ucp_addr
, 1);
561 force_sig(TARGET_SIGSEGV
);