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 register window */ 25 struct target_reg_window { 26 abi_ulong locals[8]; 27 abi_ulong ins[8]; 28 }; 29 30 /* A Sparc stack frame. */ 31 struct target_stackf { 32 /* 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. 36 */ 37 struct target_reg_window win; 38 /* 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. 42 */ 43 abi_ulong xargs[8]; 44 }; 45 46 typedef struct { 47 abi_ulong si_float_regs[32]; 48 unsigned long si_fsr; 49 unsigned long si_fpqdepth; 50 struct { 51 unsigned long *insn_addr; 52 unsigned long insn; 53 } si_fpqueue [16]; 54 } qemu_siginfo_fpu_t; 55 56 57 struct target_signal_frame { 58 struct target_stackf ss; 59 struct target_pt_regs regs; 60 uint32_t si_mask; 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 abi_ulong get_sigframe(struct target_sigaction *sa, 69 CPUSPARCState *env, 70 size_t 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 /* 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 90 * and report that. 91 */ 92 sp &= ~15UL; 93 94 return sp; 95 } 96 97 static void save_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env) 98 { 99 int i; 100 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); 105 #else 106 __put_user(cpu_get_psr(env), ®s->psr); 107 #endif 108 109 __put_user(env->pc, ®s->pc); 110 __put_user(env->npc, ®s->npc); 111 __put_user(env->y, ®s->y); 112 113 for (i = 0; i < 8; i++) { 114 __put_user(env->gregs[i], ®s->u_regs[i]); 115 } 116 for (i = 0; i < 8; i++) { 117 __put_user(env->regwptr[WREG_O0 + i], ®s->u_regs[i + 8]); 118 } 119 } 120 121 static void restore_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env) 122 { 123 int i; 124 125 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 126 /* User can only change condition codes and %asi in %tstate. */ 127 uint64_t tstate; 128 __get_user(tstate, ®s->tstate); 129 cpu_put_ccr(env, tstate >> 32); 130 env->asi = extract64(tstate, 24, 8); 131 #else 132 /* 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. 136 */ 137 uint32_t psr; 138 __get_user(psr, ®s->psr); 139 env->psr = (psr & PSR_ICC) | (env->psr & ~PSR_ICC); 140 #endif 141 142 /* Note that pc and npc are handled in the caller. */ 143 144 __get_user(env->y, ®s->y); 145 146 for (i = 0; i < 8; i++) { 147 __get_user(env->gregs[i], ®s->u_regs[i]); 148 } 149 for (i = 0; i < 8; i++) { 150 __get_user(env->regwptr[WREG_O0 + i], ®s->u_regs[i + 8]); 151 } 152 } 153 154 static void save_reg_win(struct target_reg_window *win, CPUSPARCState *env) 155 { 156 int i; 157 158 for (i = 0; i < 8; i++) { 159 __put_user(env->regwptr[i + WREG_L0], &win->locals[i]); 160 } 161 for (i = 0; i < 8; i++) { 162 __put_user(env->regwptr[i + WREG_I0], &win->ins[i]); 163 } 164 } 165 166 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7))) 167 168 void setup_frame(int sig, struct target_sigaction *ka, 169 target_sigset_t *set, CPUSPARCState *env) 170 { 171 abi_ulong sf_addr; 172 struct target_signal_frame *sf; 173 int sigframe_size, i; 174 175 /* 1. Make sure everything is clean */ 176 //synchronize_user_stack(); 177 178 sigframe_size = NF_ALIGNEDSZ; 179 sf_addr = get_sigframe(ka, env, sigframe_size); 180 trace_user_setup_frame(env, sf_addr); 181 182 sf = lock_user(VERIFY_WRITE, sf_addr, 183 sizeof(struct target_signal_frame), 0); 184 if (!sf) { 185 goto sigsegv; 186 } 187 /* 2. Save the current process state */ 188 save_pt_regs(&sf->regs, env); 189 __put_user(0, &sf->extra_size); 190 191 //save_fpu_state(regs, &sf->fpu_state); 192 //__put_user(&sf->fpu_state, &sf->fpu_save); 193 194 __put_user(set->sig[0], &sf->si_mask); 195 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) { 196 __put_user(set->sig[i + 1], &sf->extramask[i]); 197 } 198 199 save_reg_win(&sf->ss.win, env); 200 201 /* 3. signal handler back-trampoline and parameters */ 202 env->regwptr[WREG_SP] = sf_addr; 203 env->regwptr[WREG_O0] = sig; 204 env->regwptr[WREG_O1] = sf_addr + 205 offsetof(struct target_signal_frame, regs); 206 env->regwptr[WREG_O2] = sf_addr + 207 offsetof(struct target_signal_frame, regs); 208 209 /* 4. signal handler */ 210 env->pc = ka->_sa_handler; 211 env->npc = (env->pc + 4); 212 /* 5. return to kernel instructions */ 213 if (ka->ka_restorer) { 214 env->regwptr[WREG_O7] = ka->ka_restorer; 215 } else { 216 uint32_t val32; 217 218 env->regwptr[WREG_O7] = sf_addr + 219 offsetof(struct target_signal_frame, insns) - 2 * 4; 220 221 /* mov __NR_sigreturn, %g1 */ 222 val32 = 0x821020d8; 223 __put_user(val32, &sf->insns[0]); 224 225 /* t 0x10 */ 226 val32 = 0x91d02010; 227 __put_user(val32, &sf->insns[1]); 228 } 229 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame)); 230 return; 231 #if 0 232 sigill_and_return: 233 force_sig(TARGET_SIGILL); 234 #endif 235 sigsegv: 236 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame)); 237 force_sigsegv(sig); 238 } 239 240 void setup_rt_frame(int sig, struct target_sigaction *ka, 241 target_siginfo_t *info, 242 target_sigset_t *set, CPUSPARCState *env) 243 { 244 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n"); 245 } 246 247 long do_sigreturn(CPUSPARCState *env) 248 { 249 abi_ulong sf_addr; 250 struct target_signal_frame *sf; 251 abi_ulong pc, npc; 252 target_sigset_t set; 253 sigset_t host_set; 254 int i; 255 256 sf_addr = env->regwptr[WREG_SP]; 257 trace_user_do_sigreturn(env, sf_addr); 258 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) { 259 goto segv_and_exit; 260 } 261 262 /* 1. Make sure we are not getting garbage from the user */ 263 264 if (sf_addr & 3) 265 goto segv_and_exit; 266 267 __get_user(pc, &sf->regs.pc); 268 __get_user(npc, &sf->regs.npc); 269 270 if ((pc | npc) & 3) { 271 goto segv_and_exit; 272 } 273 274 /* 2. Restore the state */ 275 restore_pt_regs(&sf->regs, env); 276 env->pc = pc; 277 env->npc = npc; 278 279 /* FIXME: implement FPU save/restore: 280 * __get_user(fpu_save, &sf->fpu_save); 281 * if (fpu_save) { 282 * if (restore_fpu_state(env, fpu_save)) { 283 * goto segv_and_exit; 284 * } 285 * } 286 */ 287 288 __get_user(set.sig[0], &sf->si_mask); 289 for (i = 1; i < TARGET_NSIG_WORDS; i++) { 290 __get_user(set.sig[i], &sf->extramask[i - 1]); 291 } 292 293 target_to_host_sigset_internal(&host_set, &set); 294 set_sigmask(&host_set); 295 296 unlock_user_struct(sf, sf_addr, 0); 297 return -TARGET_QEMU_ESIGRETURN; 298 299 segv_and_exit: 300 unlock_user_struct(sf, sf_addr, 0); 301 force_sig(TARGET_SIGSEGV); 302 return -TARGET_QEMU_ESIGRETURN; 303 } 304 305 long do_rt_sigreturn(CPUSPARCState *env) 306 { 307 trace_user_do_rt_sigreturn(env, 0); 308 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n"); 309 return -TARGET_ENOSYS; 310 } 311 312 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 313 #define SPARC_MC_TSTATE 0 314 #define SPARC_MC_PC 1 315 #define SPARC_MC_NPC 2 316 #define SPARC_MC_Y 3 317 #define SPARC_MC_G1 4 318 #define SPARC_MC_G2 5 319 #define SPARC_MC_G3 6 320 #define SPARC_MC_G4 7 321 #define SPARC_MC_G5 8 322 #define SPARC_MC_G6 9 323 #define SPARC_MC_G7 10 324 #define SPARC_MC_O0 11 325 #define SPARC_MC_O1 12 326 #define SPARC_MC_O2 13 327 #define SPARC_MC_O3 14 328 #define SPARC_MC_O4 15 329 #define SPARC_MC_O5 16 330 #define SPARC_MC_O6 17 331 #define SPARC_MC_O7 18 332 #define SPARC_MC_NGREG 19 333 334 typedef abi_ulong target_mc_greg_t; 335 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG]; 336 337 struct target_mc_fq { 338 abi_ulong mcfq_addr; 339 uint32_t mcfq_insn; 340 }; 341 342 /* 343 * Note the manual 16-alignment; the kernel gets this because it 344 * includes a "long double qregs[16]" in the mcpu_fregs union, 345 * which we can't do. 346 */ 347 struct target_mc_fpu { 348 union { 349 uint32_t sregs[32]; 350 uint64_t dregs[32]; 351 //uint128_t qregs[16]; 352 } mcfpu_fregs; 353 abi_ulong mcfpu_fsr; 354 abi_ulong mcfpu_fprs; 355 abi_ulong mcfpu_gsr; 356 abi_ulong mcfpu_fq; 357 unsigned char mcfpu_qcnt; 358 unsigned char mcfpu_qentsz; 359 unsigned char mcfpu_enab; 360 } __attribute__((aligned(16))); 361 typedef struct target_mc_fpu target_mc_fpu_t; 362 363 typedef struct { 364 target_mc_gregset_t mc_gregs; 365 target_mc_greg_t mc_fp; 366 target_mc_greg_t mc_i7; 367 target_mc_fpu_t mc_fpregs; 368 } target_mcontext_t; 369 370 struct target_ucontext { 371 abi_ulong tuc_link; 372 abi_ulong tuc_flags; 373 target_sigset_t tuc_sigmask; 374 target_mcontext_t tuc_mcontext; 375 }; 376 377 /* {set, get}context() needed for 64-bit SparcLinux userland. */ 378 void sparc64_set_context(CPUSPARCState *env) 379 { 380 abi_ulong ucp_addr; 381 struct target_ucontext *ucp; 382 target_mc_gregset_t *grp; 383 target_mc_fpu_t *fpup; 384 abi_ulong pc, npc, tstate; 385 unsigned int i; 386 unsigned char fenab; 387 388 ucp_addr = env->regwptr[WREG_O0]; 389 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) { 390 goto do_sigsegv; 391 } 392 grp = &ucp->tuc_mcontext.mc_gregs; 393 __get_user(pc, &((*grp)[SPARC_MC_PC])); 394 __get_user(npc, &((*grp)[SPARC_MC_NPC])); 395 if ((pc | npc) & 3) { 396 goto do_sigsegv; 397 } 398 if (env->regwptr[WREG_O1]) { 399 target_sigset_t target_set; 400 sigset_t set; 401 402 if (TARGET_NSIG_WORDS == 1) { 403 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]); 404 } else { 405 abi_ulong *src, *dst; 406 src = ucp->tuc_sigmask.sig; 407 dst = target_set.sig; 408 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) { 409 __get_user(*dst, src); 410 } 411 } 412 target_to_host_sigset_internal(&set, &target_set); 413 set_sigmask(&set); 414 } 415 env->pc = pc; 416 env->npc = npc; 417 __get_user(env->y, &((*grp)[SPARC_MC_Y])); 418 __get_user(tstate, &((*grp)[SPARC_MC_TSTATE])); 419 /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */ 420 env->asi = (tstate >> 24) & 0xff; 421 cpu_put_ccr(env, (tstate >> 32) & 0xff); 422 __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1])); 423 __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2])); 424 __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3])); 425 __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4])); 426 __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5])); 427 __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6])); 428 /* Skip g7 as that's the thread register in userspace */ 429 430 /* 431 * Note that unlike the kernel, we didn't need to mess with the 432 * guest register window state to save it into a pt_regs to run 433 * the kernel. So for us the guest's O regs are still in WREG_O* 434 * (unlike the kernel which has put them in UREG_I* in a pt_regs) 435 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't 436 * need to be written back to userspace memory. 437 */ 438 __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0])); 439 __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1])); 440 __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2])); 441 __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3])); 442 __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4])); 443 __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5])); 444 __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6])); 445 __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7])); 446 447 __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp)); 448 __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7)); 449 450 fpup = &ucp->tuc_mcontext.mc_fpregs; 451 452 __get_user(fenab, &(fpup->mcfpu_enab)); 453 if (fenab) { 454 abi_ulong fprs; 455 456 /* 457 * We use the FPRS from the guest only in deciding whether 458 * to restore the upper, lower, or both banks of the FPU regs. 459 * The kernel here writes the FPU register data into the 460 * process's current_thread_info state and unconditionally 461 * clears FPRS and TSTATE_PEF: this disables the FPU so that the 462 * next FPU-disabled trap will copy the data out of 463 * current_thread_info and into the real FPU registers. 464 * QEMU doesn't need to handle lazy-FPU-state-restoring like that, 465 * so we always load the data directly into the FPU registers 466 * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled). 467 * Note that because we (and the kernel) always write zeroes for 468 * the fenab and fprs in sparc64_get_context() none of this code 469 * will execute unless the guest manually constructed or changed 470 * the context structure. 471 */ 472 __get_user(fprs, &(fpup->mcfpu_fprs)); 473 if (fprs & FPRS_DL) { 474 for (i = 0; i < 16; i++) { 475 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i])); 476 } 477 } 478 if (fprs & FPRS_DU) { 479 for (i = 16; i < 32; i++) { 480 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i])); 481 } 482 } 483 __get_user(env->fsr, &(fpup->mcfpu_fsr)); 484 __get_user(env->gsr, &(fpup->mcfpu_gsr)); 485 } 486 unlock_user_struct(ucp, ucp_addr, 0); 487 return; 488 do_sigsegv: 489 unlock_user_struct(ucp, ucp_addr, 0); 490 force_sig(TARGET_SIGSEGV); 491 } 492 493 void sparc64_get_context(CPUSPARCState *env) 494 { 495 abi_ulong ucp_addr; 496 struct target_ucontext *ucp; 497 target_mc_gregset_t *grp; 498 target_mcontext_t *mcp; 499 int err; 500 unsigned int i; 501 target_sigset_t target_set; 502 sigset_t set; 503 504 ucp_addr = env->regwptr[WREG_O0]; 505 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) { 506 goto do_sigsegv; 507 } 508 509 memset(ucp, 0, sizeof(*ucp)); 510 511 mcp = &ucp->tuc_mcontext; 512 grp = &mcp->mc_gregs; 513 514 /* Skip over the trap instruction, first. */ 515 env->pc = env->npc; 516 env->npc += 4; 517 518 /* If we're only reading the signal mask then do_sigprocmask() 519 * is guaranteed not to fail, which is important because we don't 520 * have any way to signal a failure or restart this operation since 521 * this is not a normal syscall. 522 */ 523 err = do_sigprocmask(0, NULL, &set); 524 assert(err == 0); 525 host_to_target_sigset_internal(&target_set, &set); 526 if (TARGET_NSIG_WORDS == 1) { 527 __put_user(target_set.sig[0], 528 (abi_ulong *)&ucp->tuc_sigmask); 529 } else { 530 abi_ulong *src, *dst; 531 src = target_set.sig; 532 dst = ucp->tuc_sigmask.sig; 533 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) { 534 __put_user(*src, dst); 535 } 536 } 537 538 __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE])); 539 __put_user(env->pc, &((*grp)[SPARC_MC_PC])); 540 __put_user(env->npc, &((*grp)[SPARC_MC_NPC])); 541 __put_user(env->y, &((*grp)[SPARC_MC_Y])); 542 __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1])); 543 __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2])); 544 __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3])); 545 __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4])); 546 __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5])); 547 __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6])); 548 __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7])); 549 550 /* 551 * Note that unlike the kernel, we didn't need to mess with the 552 * guest register window state to save it into a pt_regs to run 553 * the kernel. So for us the guest's O regs are still in WREG_O* 554 * (unlike the kernel which has put them in UREG_I* in a pt_regs) 555 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't 556 * need to be fished out of userspace memory. 557 */ 558 __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0])); 559 __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1])); 560 __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2])); 561 __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3])); 562 __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4])); 563 __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5])); 564 __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6])); 565 __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7])); 566 567 __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp)); 568 __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7)); 569 570 /* 571 * We don't write out the FPU state. This matches the kernel's 572 * implementation (which has the code for doing this but 573 * hidden behind an "if (fenab)" where fenab is always 0). 574 */ 575 576 unlock_user_struct(ucp, ucp_addr, 1); 577 return; 578 do_sigsegv: 579 unlock_user_struct(ucp, ucp_addr, 1); 580 force_sig(TARGET_SIGSEGV); 581 } 582 #endif 583