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 struct target_siginfo_fpu { 47 /* It is more convenient for qemu to move doubles, not singles. */ 48 uint64_t si_double_regs[16]; 49 uint32_t si_fsr; 50 uint32_t si_fpqdepth; 51 struct { 52 uint32_t insn_addr; 53 uint32_t insn; 54 } si_fpqueue [16]; 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 abi_ulong rwin_save; 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 static void save_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env) 167 { 168 int i; 169 170 for (i = 0; i < 16; ++i) { 171 __put_user(env->fpr[i].ll, &fpu->si_double_regs[i]); 172 } 173 __put_user(env->fsr, &fpu->si_fsr); 174 __put_user(0, &fpu->si_fpqdepth); 175 } 176 177 static void restore_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env) 178 { 179 int i; 180 181 for (i = 0; i < 16; ++i) { 182 __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]); 183 } 184 __get_user(env->fsr, &fpu->si_fsr); 185 } 186 187 void setup_frame(int sig, struct target_sigaction *ka, 188 target_sigset_t *set, CPUSPARCState *env) 189 { 190 abi_ulong sf_addr; 191 struct target_signal_frame *sf; 192 size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu); 193 int i; 194 195 /* 1. Make sure everything is clean */ 196 197 sf_addr = get_sigframe(ka, env, sf_size); 198 trace_user_setup_frame(env, sf_addr); 199 200 sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0); 201 if (!sf) { 202 goto sigsegv; 203 } 204 205 /* 2. Save the current process state */ 206 save_pt_regs(&sf->regs, env); 207 __put_user(0, &sf->extra_size); 208 209 save_fpu((struct target_siginfo_fpu *)(sf + 1), env); 210 __put_user(sf_addr + sizeof(*sf), &sf->fpu_save); 211 212 __put_user(0, &sf->rwin_save); /* TODO: save_rwin_state */ 213 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]); 217 } 218 219 save_reg_win(&sf->ss.win, env); 220 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); 228 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; 235 } else { 236 uint32_t val32; 237 238 env->regwptr[WREG_O7] = sf_addr + 239 offsetof(struct target_signal_frame, insns) - 2 * 4; 240 241 /* mov __NR_sigreturn, %g1 */ 242 val32 = 0x821020d8; 243 __put_user(val32, &sf->insns[0]); 244 245 /* t 0x10 */ 246 val32 = 0x91d02010; 247 __put_user(val32, &sf->insns[1]); 248 } 249 unlock_user(sf, sf_addr, sf_size); 250 return; 251 #if 0 252 sigill_and_return: 253 force_sig(TARGET_SIGILL); 254 #endif 255 sigsegv: 256 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame)); 257 force_sigsegv(sig); 258 } 259 260 void setup_rt_frame(int sig, struct target_sigaction *ka, 261 target_siginfo_t *info, 262 target_sigset_t *set, CPUSPARCState *env) 263 { 264 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n"); 265 } 266 267 long do_sigreturn(CPUSPARCState *env) 268 { 269 abi_ulong sf_addr; 270 struct target_signal_frame *sf; 271 abi_ulong pc, npc, ptr; 272 target_sigset_t set; 273 sigset_t host_set; 274 int i; 275 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)) { 279 goto segv_and_exit; 280 } 281 282 /* 1. Make sure we are not getting garbage from the user */ 283 284 if (sf_addr & 3) 285 goto segv_and_exit; 286 287 __get_user(pc, &sf->regs.pc); 288 __get_user(npc, &sf->regs.npc); 289 290 if ((pc | npc) & 3) { 291 goto segv_and_exit; 292 } 293 294 /* 2. Restore the state */ 295 restore_pt_regs(&sf->regs, env); 296 env->pc = pc; 297 env->npc = npc; 298 299 __get_user(ptr, &sf->fpu_save); 300 if (ptr) { 301 struct target_siginfo_fpu *fpu; 302 if ((ptr & 3) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) { 303 goto segv_and_exit; 304 } 305 restore_fpu(fpu, env); 306 unlock_user_struct(fpu, ptr, 0); 307 } 308 309 __get_user(ptr, &sf->rwin_save); 310 if (ptr) { 311 goto segv_and_exit; /* TODO: restore_rwin */ 312 } 313 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]); 317 } 318 319 target_to_host_sigset_internal(&host_set, &set); 320 set_sigmask(&host_set); 321 322 unlock_user_struct(sf, sf_addr, 0); 323 return -TARGET_QEMU_ESIGRETURN; 324 325 segv_and_exit: 326 unlock_user_struct(sf, sf_addr, 0); 327 force_sig(TARGET_SIGSEGV); 328 return -TARGET_QEMU_ESIGRETURN; 329 } 330 331 long do_rt_sigreturn(CPUSPARCState *env) 332 { 333 trace_user_do_rt_sigreturn(env, 0); 334 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n"); 335 return -TARGET_ENOSYS; 336 } 337 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 342 #define SPARC_MC_Y 3 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 359 360 typedef abi_ulong target_mc_greg_t; 361 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG]; 362 363 struct target_mc_fq { 364 abi_ulong mcfq_addr; 365 uint32_t mcfq_insn; 366 }; 367 368 /* 369 * Note the manual 16-alignment; the kernel gets this because it 370 * includes a "long double qregs[16]" in the mcpu_fregs union, 371 * which we can't do. 372 */ 373 struct target_mc_fpu { 374 union { 375 uint32_t sregs[32]; 376 uint64_t dregs[32]; 377 //uint128_t qregs[16]; 378 } mcfpu_fregs; 379 abi_ulong mcfpu_fsr; 380 abi_ulong mcfpu_fprs; 381 abi_ulong mcfpu_gsr; 382 abi_ulong mcfpu_fq; 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; 388 389 typedef struct { 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; 394 } target_mcontext_t; 395 396 struct target_ucontext { 397 abi_ulong tuc_link; 398 abi_ulong tuc_flags; 399 target_sigset_t tuc_sigmask; 400 target_mcontext_t tuc_mcontext; 401 }; 402 403 /* {set, get}context() needed for 64-bit SparcLinux userland. */ 404 void sparc64_set_context(CPUSPARCState *env) 405 { 406 abi_ulong ucp_addr; 407 struct target_ucontext *ucp; 408 target_mc_gregset_t *grp; 409 target_mc_fpu_t *fpup; 410 abi_ulong pc, npc, tstate; 411 unsigned int i; 412 unsigned char fenab; 413 414 ucp_addr = env->regwptr[WREG_O0]; 415 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) { 416 goto do_sigsegv; 417 } 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) { 422 goto do_sigsegv; 423 } 424 if (env->regwptr[WREG_O1]) { 425 target_sigset_t target_set; 426 sigset_t set; 427 428 if (TARGET_NSIG_WORDS == 1) { 429 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]); 430 } else { 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); 436 } 437 } 438 target_to_host_sigset_internal(&set, &target_set); 439 set_sigmask(&set); 440 } 441 env->pc = pc; 442 env->npc = npc; 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 */ 455 456 /* 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. 463 */ 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])); 472 473 __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp)); 474 __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7)); 475 476 fpup = &ucp->tuc_mcontext.mc_fpregs; 477 478 __get_user(fenab, &(fpup->mcfpu_enab)); 479 if (fenab) { 480 abi_ulong fprs; 481 482 /* 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. 497 */ 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])); 502 } 503 } 504 if (fprs & FPRS_DU) { 505 for (i = 16; i < 32; i++) { 506 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i])); 507 } 508 } 509 __get_user(env->fsr, &(fpup->mcfpu_fsr)); 510 __get_user(env->gsr, &(fpup->mcfpu_gsr)); 511 } 512 unlock_user_struct(ucp, ucp_addr, 0); 513 return; 514 do_sigsegv: 515 unlock_user_struct(ucp, ucp_addr, 0); 516 force_sig(TARGET_SIGSEGV); 517 } 518 519 void sparc64_get_context(CPUSPARCState *env) 520 { 521 abi_ulong ucp_addr; 522 struct target_ucontext *ucp; 523 target_mc_gregset_t *grp; 524 target_mcontext_t *mcp; 525 int err; 526 unsigned int i; 527 target_sigset_t target_set; 528 sigset_t set; 529 530 ucp_addr = env->regwptr[WREG_O0]; 531 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) { 532 goto do_sigsegv; 533 } 534 535 memset(ucp, 0, sizeof(*ucp)); 536 537 mcp = &ucp->tuc_mcontext; 538 grp = &mcp->mc_gregs; 539 540 /* Skip over the trap instruction, first. */ 541 env->pc = env->npc; 542 env->npc += 4; 543 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. 548 */ 549 err = do_sigprocmask(0, NULL, &set); 550 assert(err == 0); 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); 555 } else { 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); 561 } 562 } 563 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])); 575 576 /* 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. 583 */ 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])); 592 593 __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp)); 594 __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7)); 595 596 /* 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). 600 */ 601 602 unlock_user_struct(ucp, ucp_addr, 1); 603 return; 604 do_sigsegv: 605 unlock_user_struct(ucp, ucp_addr, 1); 606 force_sig(TARGET_SIGSEGV); 607 } 608 #endif 609