1 /* 2 * Emulation of Linux signals 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * 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. 10 * 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. 15 * 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/>. 18 */ 19 #include "qemu/osdep.h" 20 #include "qemu.h" 21 #include "signal-common.h" 22 #include "linux-user/trace.h" 23 24 /* A Sparc stack frame */ 25 struct sparc_stackf { 26 abi_ulong locals[8]; 27 abi_ulong ins[8]; 28 /* It's simpler to treat fp and callers_pc as elements of ins[] 29 * since we never need to access them ourselves. 30 */ 31 char *structptr; 32 abi_ulong xargs[6]; 33 abi_ulong xxargs[1]; 34 }; 35 36 typedef struct { 37 struct { 38 abi_ulong psr; 39 abi_ulong pc; 40 abi_ulong npc; 41 abi_ulong y; 42 abi_ulong u_regs[16]; /* globals and ins */ 43 } si_regs; 44 int si_mask; 45 } __siginfo_t; 46 47 typedef struct { 48 abi_ulong si_float_regs[32]; 49 unsigned long si_fsr; 50 unsigned long si_fpqdepth; 51 struct { 52 unsigned long *insn_addr; 53 unsigned long insn; 54 } si_fpqueue [16]; 55 } qemu_siginfo_fpu_t; 56 57 58 struct target_signal_frame { 59 struct sparc_stackf ss; 60 __siginfo_t info; 61 abi_ulong fpu_save; 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; 66 }; 67 68 static inline abi_ulong get_sigframe(struct target_sigaction *sa, 69 CPUSPARCState *env, 70 unsigned long framesize) 71 { 72 abi_ulong sp = get_sp_from_cpustate(env); 73 74 /* 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. 77 */ 78 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) { 79 return -1; 80 } 81 82 /* This is the X/Open sanctioned signal stack switching. */ 83 sp = target_sigsp(sp, sa) - framesize; 84 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 89 * and report that. 90 */ 91 sp &= ~15UL; 92 93 return sp; 94 } 95 96 static int 97 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask) 98 { 99 int err = 0, i; 100 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]); 107 } 108 for (i=0; i < 8; i++) { 109 __put_user(env->regwptr[WREG_O0 + i], &si->si_regs.u_regs[i + 8]); 110 } 111 __put_user(mask, &si->si_mask); 112 return err; 113 } 114 115 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7))) 116 117 void setup_frame(int sig, struct target_sigaction *ka, 118 target_sigset_t *set, CPUSPARCState *env) 119 { 120 abi_ulong sf_addr; 121 struct target_signal_frame *sf; 122 int sigframe_size, err, i; 123 124 /* 1. Make sure everything is clean */ 125 //synchronize_user_stack(); 126 127 sigframe_size = NF_ALIGNEDSZ; 128 sf_addr = get_sigframe(ka, env, sigframe_size); 129 trace_user_setup_frame(env, sf_addr); 130 131 sf = lock_user(VERIFY_WRITE, sf_addr, 132 sizeof(struct target_signal_frame), 0); 133 if (!sf) { 134 goto sigsegv; 135 } 136 #if 0 137 if (invalid_frame_pointer(sf, sigframe_size)) 138 goto sigill_and_return; 139 #endif 140 /* 2. Save the current process state */ 141 err = setup___siginfo(&sf->info, env, set->sig[0]); 142 __put_user(0, &sf->extra_size); 143 144 //save_fpu_state(regs, &sf->fpu_state); 145 //__put_user(&sf->fpu_state, &sf->fpu_save); 146 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]); 150 } 151 152 for (i = 0; i < 8; i++) { 153 __put_user(env->regwptr[i + WREG_L0], &sf->ss.locals[i]); 154 } 155 for (i = 0; i < 8; i++) { 156 __put_user(env->regwptr[i + WREG_I0], &sf->ss.ins[i]); 157 } 158 if (err) 159 goto sigsegv; 160 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); 168 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; 175 } else { 176 uint32_t val32; 177 178 env->regwptr[WREG_O7] = sf_addr + 179 offsetof(struct target_signal_frame, insns) - 2 * 4; 180 181 /* mov __NR_sigreturn, %g1 */ 182 val32 = 0x821020d8; 183 __put_user(val32, &sf->insns[0]); 184 185 /* t 0x10 */ 186 val32 = 0x91d02010; 187 __put_user(val32, &sf->insns[1]); 188 } 189 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame)); 190 return; 191 #if 0 192 sigill_and_return: 193 force_sig(TARGET_SIGILL); 194 #endif 195 sigsegv: 196 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame)); 197 force_sigsegv(sig); 198 } 199 200 void setup_rt_frame(int sig, struct target_sigaction *ka, 201 target_siginfo_t *info, 202 target_sigset_t *set, CPUSPARCState *env) 203 { 204 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n"); 205 } 206 207 long do_sigreturn(CPUSPARCState *env) 208 { 209 abi_ulong sf_addr; 210 struct target_signal_frame *sf; 211 abi_ulong up_psr, pc, npc; 212 target_sigset_t set; 213 sigset_t host_set; 214 int i; 215 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)) { 219 goto segv_and_exit; 220 } 221 222 /* 1. Make sure we are not getting garbage from the user */ 223 224 if (sf_addr & 3) 225 goto segv_and_exit; 226 227 __get_user(pc, &sf->info.si_regs.pc); 228 __get_user(npc, &sf->info.si_regs.npc); 229 230 if ((pc | npc) & 3) { 231 goto segv_and_exit; 232 } 233 234 /* 2. Restore the state */ 235 __get_user(up_psr, &sf->info.si_regs.psr); 236 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 */)); 240 241 env->pc = pc; 242 env->npc = npc; 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]); 246 } 247 for (i=0; i < 8; i++) { 248 __get_user(env->regwptr[i + WREG_O0], &sf->info.si_regs.u_regs[i + 8]); 249 } 250 251 /* FIXME: implement FPU save/restore: 252 * __get_user(fpu_save, &sf->fpu_save); 253 * if (fpu_save) { 254 * if (restore_fpu_state(env, fpu_save)) { 255 * goto segv_and_exit; 256 * } 257 * } 258 */ 259 260 /* This is pretty much atomic, no amount locking would prevent 261 * the races which exist anyways. 262 */ 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]); 266 } 267 268 target_to_host_sigset_internal(&host_set, &set); 269 set_sigmask(&host_set); 270 271 unlock_user_struct(sf, sf_addr, 0); 272 return -TARGET_QEMU_ESIGRETURN; 273 274 segv_and_exit: 275 unlock_user_struct(sf, sf_addr, 0); 276 force_sig(TARGET_SIGSEGV); 277 return -TARGET_QEMU_ESIGRETURN; 278 } 279 280 long do_rt_sigreturn(CPUSPARCState *env) 281 { 282 trace_user_do_rt_sigreturn(env, 0); 283 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n"); 284 return -TARGET_ENOSYS; 285 } 286 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 291 #define SPARC_MC_Y 3 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 308 309 typedef abi_ulong target_mc_greg_t; 310 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG]; 311 312 struct target_mc_fq { 313 abi_ulong mcfq_addr; 314 uint32_t mcfq_insn; 315 }; 316 317 /* 318 * Note the manual 16-alignment; the kernel gets this because it 319 * includes a "long double qregs[16]" in the mcpu_fregs union, 320 * which we can't do. 321 */ 322 struct target_mc_fpu { 323 union { 324 uint32_t sregs[32]; 325 uint64_t dregs[32]; 326 //uint128_t qregs[16]; 327 } mcfpu_fregs; 328 abi_ulong mcfpu_fsr; 329 abi_ulong mcfpu_fprs; 330 abi_ulong mcfpu_gsr; 331 abi_ulong mcfpu_fq; 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; 337 338 typedef struct { 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; 343 } target_mcontext_t; 344 345 struct target_ucontext { 346 abi_ulong tuc_link; 347 abi_ulong tuc_flags; 348 target_sigset_t tuc_sigmask; 349 target_mcontext_t tuc_mcontext; 350 }; 351 352 /* A V9 register window */ 353 struct target_reg_window { 354 abi_ulong locals[8]; 355 abi_ulong ins[8]; 356 }; 357 358 /* {set, get}context() needed for 64-bit SparcLinux userland. */ 359 void sparc64_set_context(CPUSPARCState *env) 360 { 361 abi_ulong ucp_addr; 362 struct target_ucontext *ucp; 363 target_mc_gregset_t *grp; 364 target_mc_fpu_t *fpup; 365 abi_ulong pc, npc, tstate; 366 unsigned int i; 367 unsigned char fenab; 368 369 ucp_addr = env->regwptr[WREG_O0]; 370 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) { 371 goto do_sigsegv; 372 } 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) { 377 goto do_sigsegv; 378 } 379 if (env->regwptr[WREG_O1]) { 380 target_sigset_t target_set; 381 sigset_t set; 382 383 if (TARGET_NSIG_WORDS == 1) { 384 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]); 385 } else { 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); 391 } 392 } 393 target_to_host_sigset_internal(&set, &target_set); 394 set_sigmask(&set); 395 } 396 env->pc = pc; 397 env->npc = npc; 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 */ 410 411 /* 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. 418 */ 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])); 427 428 __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp)); 429 __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7)); 430 431 fpup = &ucp->tuc_mcontext.mc_fpregs; 432 433 __get_user(fenab, &(fpup->mcfpu_enab)); 434 if (fenab) { 435 abi_ulong fprs; 436 437 /* 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. 452 */ 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])); 457 } 458 } 459 if (fprs & FPRS_DU) { 460 for (i = 16; i < 32; i++) { 461 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i])); 462 } 463 } 464 __get_user(env->fsr, &(fpup->mcfpu_fsr)); 465 __get_user(env->gsr, &(fpup->mcfpu_gsr)); 466 } 467 unlock_user_struct(ucp, ucp_addr, 0); 468 return; 469 do_sigsegv: 470 unlock_user_struct(ucp, ucp_addr, 0); 471 force_sig(TARGET_SIGSEGV); 472 } 473 474 void sparc64_get_context(CPUSPARCState *env) 475 { 476 abi_ulong ucp_addr; 477 struct target_ucontext *ucp; 478 target_mc_gregset_t *grp; 479 target_mcontext_t *mcp; 480 int err; 481 unsigned int i; 482 target_sigset_t target_set; 483 sigset_t set; 484 485 ucp_addr = env->regwptr[WREG_O0]; 486 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) { 487 goto do_sigsegv; 488 } 489 490 memset(ucp, 0, sizeof(*ucp)); 491 492 mcp = &ucp->tuc_mcontext; 493 grp = &mcp->mc_gregs; 494 495 /* Skip over the trap instruction, first. */ 496 env->pc = env->npc; 497 env->npc += 4; 498 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. 503 */ 504 err = do_sigprocmask(0, NULL, &set); 505 assert(err == 0); 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); 510 } else { 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); 516 } 517 } 518 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])); 530 531 /* 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. 538 */ 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])); 547 548 __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp)); 549 __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7)); 550 551 /* 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). 555 */ 556 557 unlock_user_struct(ucp, ucp_addr, 1); 558 return; 559 do_sigsegv: 560 unlock_user_struct(ucp, ucp_addr, 1); 561 force_sig(TARGET_SIGSEGV); 562 } 563 #endif 564