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 sf_addr = get_sigframe(ka, env, sf_size); 196 trace_user_setup_frame(env, sf_addr); 197 198 sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0); 199 if (!sf) { 200 force_sigsegv(sig); 201 return; 202 } 203 204 /* 2. Save the current process state */ 205 save_pt_regs(&sf->regs, env); 206 __put_user(0, &sf->extra_size); 207 208 save_fpu((struct target_siginfo_fpu *)(sf + 1), env); 209 __put_user(sf_addr + sizeof(*sf), &sf->fpu_save); 210 211 __put_user(0, &sf->rwin_save); /* TODO: save_rwin_state */ 212 213 __put_user(set->sig[0], &sf->si_mask); 214 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) { 215 __put_user(set->sig[i + 1], &sf->extramask[i]); 216 } 217 218 save_reg_win(&sf->ss.win, env); 219 220 /* 3. signal handler back-trampoline and parameters */ 221 env->regwptr[WREG_SP] = sf_addr; 222 env->regwptr[WREG_O0] = sig; 223 env->regwptr[WREG_O1] = sf_addr + 224 offsetof(struct target_signal_frame, regs); 225 env->regwptr[WREG_O2] = sf_addr + 226 offsetof(struct target_signal_frame, regs); 227 228 /* 4. signal handler */ 229 env->pc = ka->_sa_handler; 230 env->npc = env->pc + 4; 231 232 /* 5. return to kernel instructions */ 233 if (ka->ka_restorer) { 234 env->regwptr[WREG_O7] = ka->ka_restorer; 235 } else { 236 env->regwptr[WREG_O7] = sf_addr + 237 offsetof(struct target_signal_frame, insns) - 2 * 4; 238 239 /* mov __NR_sigreturn, %g1 */ 240 __put_user(0x821020d8u, &sf->insns[0]); 241 /* t 0x10 */ 242 __put_user(0x91d02010u, &sf->insns[1]); 243 } 244 unlock_user(sf, sf_addr, sf_size); 245 } 246 247 void setup_rt_frame(int sig, struct target_sigaction *ka, 248 target_siginfo_t *info, 249 target_sigset_t *set, CPUSPARCState *env) 250 { 251 qemu_log_mask(LOG_UNIMP, "setup_rt_frame: not implemented\n"); 252 } 253 254 long do_sigreturn(CPUSPARCState *env) 255 { 256 abi_ulong sf_addr; 257 struct target_signal_frame *sf; 258 abi_ulong pc, npc, ptr; 259 target_sigset_t set; 260 sigset_t host_set; 261 int i; 262 263 sf_addr = env->regwptr[WREG_SP]; 264 trace_user_do_sigreturn(env, sf_addr); 265 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) { 266 goto segv_and_exit; 267 } 268 269 /* 1. Make sure we are not getting garbage from the user */ 270 271 if (sf_addr & 3) 272 goto segv_and_exit; 273 274 __get_user(pc, &sf->regs.pc); 275 __get_user(npc, &sf->regs.npc); 276 277 if ((pc | npc) & 3) { 278 goto segv_and_exit; 279 } 280 281 /* 2. Restore the state */ 282 restore_pt_regs(&sf->regs, env); 283 env->pc = pc; 284 env->npc = npc; 285 286 __get_user(ptr, &sf->fpu_save); 287 if (ptr) { 288 struct target_siginfo_fpu *fpu; 289 if ((ptr & 3) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) { 290 goto segv_and_exit; 291 } 292 restore_fpu(fpu, env); 293 unlock_user_struct(fpu, ptr, 0); 294 } 295 296 __get_user(ptr, &sf->rwin_save); 297 if (ptr) { 298 goto segv_and_exit; /* TODO: restore_rwin */ 299 } 300 301 __get_user(set.sig[0], &sf->si_mask); 302 for (i = 1; i < TARGET_NSIG_WORDS; i++) { 303 __get_user(set.sig[i], &sf->extramask[i - 1]); 304 } 305 306 target_to_host_sigset_internal(&host_set, &set); 307 set_sigmask(&host_set); 308 309 unlock_user_struct(sf, sf_addr, 0); 310 return -TARGET_QEMU_ESIGRETURN; 311 312 segv_and_exit: 313 unlock_user_struct(sf, sf_addr, 0); 314 force_sig(TARGET_SIGSEGV); 315 return -TARGET_QEMU_ESIGRETURN; 316 } 317 318 long do_rt_sigreturn(CPUSPARCState *env) 319 { 320 trace_user_do_rt_sigreturn(env, 0); 321 qemu_log_mask(LOG_UNIMP, "do_rt_sigreturn: not implemented\n"); 322 return -TARGET_ENOSYS; 323 } 324 325 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 326 #define SPARC_MC_TSTATE 0 327 #define SPARC_MC_PC 1 328 #define SPARC_MC_NPC 2 329 #define SPARC_MC_Y 3 330 #define SPARC_MC_G1 4 331 #define SPARC_MC_G2 5 332 #define SPARC_MC_G3 6 333 #define SPARC_MC_G4 7 334 #define SPARC_MC_G5 8 335 #define SPARC_MC_G6 9 336 #define SPARC_MC_G7 10 337 #define SPARC_MC_O0 11 338 #define SPARC_MC_O1 12 339 #define SPARC_MC_O2 13 340 #define SPARC_MC_O3 14 341 #define SPARC_MC_O4 15 342 #define SPARC_MC_O5 16 343 #define SPARC_MC_O6 17 344 #define SPARC_MC_O7 18 345 #define SPARC_MC_NGREG 19 346 347 typedef abi_ulong target_mc_greg_t; 348 typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG]; 349 350 struct target_mc_fq { 351 abi_ulong mcfq_addr; 352 uint32_t mcfq_insn; 353 }; 354 355 /* 356 * Note the manual 16-alignment; the kernel gets this because it 357 * includes a "long double qregs[16]" in the mcpu_fregs union, 358 * which we can't do. 359 */ 360 struct target_mc_fpu { 361 union { 362 uint32_t sregs[32]; 363 uint64_t dregs[32]; 364 //uint128_t qregs[16]; 365 } mcfpu_fregs; 366 abi_ulong mcfpu_fsr; 367 abi_ulong mcfpu_fprs; 368 abi_ulong mcfpu_gsr; 369 abi_ulong mcfpu_fq; 370 unsigned char mcfpu_qcnt; 371 unsigned char mcfpu_qentsz; 372 unsigned char mcfpu_enab; 373 } __attribute__((aligned(16))); 374 typedef struct target_mc_fpu target_mc_fpu_t; 375 376 typedef struct { 377 target_mc_gregset_t mc_gregs; 378 target_mc_greg_t mc_fp; 379 target_mc_greg_t mc_i7; 380 target_mc_fpu_t mc_fpregs; 381 } target_mcontext_t; 382 383 struct target_ucontext { 384 abi_ulong tuc_link; 385 abi_ulong tuc_flags; 386 target_sigset_t tuc_sigmask; 387 target_mcontext_t tuc_mcontext; 388 }; 389 390 /* {set, get}context() needed for 64-bit SparcLinux userland. */ 391 void sparc64_set_context(CPUSPARCState *env) 392 { 393 abi_ulong ucp_addr; 394 struct target_ucontext *ucp; 395 target_mc_gregset_t *grp; 396 target_mc_fpu_t *fpup; 397 abi_ulong pc, npc, tstate; 398 unsigned int i; 399 unsigned char fenab; 400 401 ucp_addr = env->regwptr[WREG_O0]; 402 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) { 403 goto do_sigsegv; 404 } 405 grp = &ucp->tuc_mcontext.mc_gregs; 406 __get_user(pc, &((*grp)[SPARC_MC_PC])); 407 __get_user(npc, &((*grp)[SPARC_MC_NPC])); 408 if ((pc | npc) & 3) { 409 goto do_sigsegv; 410 } 411 if (env->regwptr[WREG_O1]) { 412 target_sigset_t target_set; 413 sigset_t set; 414 415 if (TARGET_NSIG_WORDS == 1) { 416 __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]); 417 } else { 418 abi_ulong *src, *dst; 419 src = ucp->tuc_sigmask.sig; 420 dst = target_set.sig; 421 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) { 422 __get_user(*dst, src); 423 } 424 } 425 target_to_host_sigset_internal(&set, &target_set); 426 set_sigmask(&set); 427 } 428 env->pc = pc; 429 env->npc = npc; 430 __get_user(env->y, &((*grp)[SPARC_MC_Y])); 431 __get_user(tstate, &((*grp)[SPARC_MC_TSTATE])); 432 /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */ 433 env->asi = (tstate >> 24) & 0xff; 434 cpu_put_ccr(env, (tstate >> 32) & 0xff); 435 __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1])); 436 __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2])); 437 __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3])); 438 __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4])); 439 __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5])); 440 __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6])); 441 /* Skip g7 as that's the thread register in userspace */ 442 443 /* 444 * Note that unlike the kernel, we didn't need to mess with the 445 * guest register window state to save it into a pt_regs to run 446 * the kernel. So for us the guest's O regs are still in WREG_O* 447 * (unlike the kernel which has put them in UREG_I* in a pt_regs) 448 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't 449 * need to be written back to userspace memory. 450 */ 451 __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0])); 452 __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1])); 453 __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2])); 454 __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3])); 455 __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4])); 456 __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5])); 457 __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6])); 458 __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7])); 459 460 __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp)); 461 __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7)); 462 463 fpup = &ucp->tuc_mcontext.mc_fpregs; 464 465 __get_user(fenab, &(fpup->mcfpu_enab)); 466 if (fenab) { 467 abi_ulong fprs; 468 469 /* 470 * We use the FPRS from the guest only in deciding whether 471 * to restore the upper, lower, or both banks of the FPU regs. 472 * The kernel here writes the FPU register data into the 473 * process's current_thread_info state and unconditionally 474 * clears FPRS and TSTATE_PEF: this disables the FPU so that the 475 * next FPU-disabled trap will copy the data out of 476 * current_thread_info and into the real FPU registers. 477 * QEMU doesn't need to handle lazy-FPU-state-restoring like that, 478 * so we always load the data directly into the FPU registers 479 * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled). 480 * Note that because we (and the kernel) always write zeroes for 481 * the fenab and fprs in sparc64_get_context() none of this code 482 * will execute unless the guest manually constructed or changed 483 * the context structure. 484 */ 485 __get_user(fprs, &(fpup->mcfpu_fprs)); 486 if (fprs & FPRS_DL) { 487 for (i = 0; i < 16; i++) { 488 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i])); 489 } 490 } 491 if (fprs & FPRS_DU) { 492 for (i = 16; i < 32; i++) { 493 __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i])); 494 } 495 } 496 __get_user(env->fsr, &(fpup->mcfpu_fsr)); 497 __get_user(env->gsr, &(fpup->mcfpu_gsr)); 498 } 499 unlock_user_struct(ucp, ucp_addr, 0); 500 return; 501 do_sigsegv: 502 unlock_user_struct(ucp, ucp_addr, 0); 503 force_sig(TARGET_SIGSEGV); 504 } 505 506 void sparc64_get_context(CPUSPARCState *env) 507 { 508 abi_ulong ucp_addr; 509 struct target_ucontext *ucp; 510 target_mc_gregset_t *grp; 511 target_mcontext_t *mcp; 512 int err; 513 unsigned int i; 514 target_sigset_t target_set; 515 sigset_t set; 516 517 ucp_addr = env->regwptr[WREG_O0]; 518 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) { 519 goto do_sigsegv; 520 } 521 522 memset(ucp, 0, sizeof(*ucp)); 523 524 mcp = &ucp->tuc_mcontext; 525 grp = &mcp->mc_gregs; 526 527 /* Skip over the trap instruction, first. */ 528 env->pc = env->npc; 529 env->npc += 4; 530 531 /* If we're only reading the signal mask then do_sigprocmask() 532 * is guaranteed not to fail, which is important because we don't 533 * have any way to signal a failure or restart this operation since 534 * this is not a normal syscall. 535 */ 536 err = do_sigprocmask(0, NULL, &set); 537 assert(err == 0); 538 host_to_target_sigset_internal(&target_set, &set); 539 if (TARGET_NSIG_WORDS == 1) { 540 __put_user(target_set.sig[0], 541 (abi_ulong *)&ucp->tuc_sigmask); 542 } else { 543 abi_ulong *src, *dst; 544 src = target_set.sig; 545 dst = ucp->tuc_sigmask.sig; 546 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) { 547 __put_user(*src, dst); 548 } 549 } 550 551 __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE])); 552 __put_user(env->pc, &((*grp)[SPARC_MC_PC])); 553 __put_user(env->npc, &((*grp)[SPARC_MC_NPC])); 554 __put_user(env->y, &((*grp)[SPARC_MC_Y])); 555 __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1])); 556 __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2])); 557 __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3])); 558 __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4])); 559 __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5])); 560 __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6])); 561 __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7])); 562 563 /* 564 * Note that unlike the kernel, we didn't need to mess with the 565 * guest register window state to save it into a pt_regs to run 566 * the kernel. So for us the guest's O regs are still in WREG_O* 567 * (unlike the kernel which has put them in UREG_I* in a pt_regs) 568 * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't 569 * need to be fished out of userspace memory. 570 */ 571 __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0])); 572 __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1])); 573 __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2])); 574 __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3])); 575 __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4])); 576 __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5])); 577 __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6])); 578 __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7])); 579 580 __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp)); 581 __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7)); 582 583 /* 584 * We don't write out the FPU state. This matches the kernel's 585 * implementation (which has the code for doing this but 586 * hidden behind an "if (fenab)" where fenab is always 0). 587 */ 588 589 unlock_user_struct(ucp, ucp_addr, 1); 590 return; 591 do_sigsegv: 592 unlock_user_struct(ucp, ucp_addr, 1); 593 force_sig(TARGET_SIGSEGV); 594 } 595 #endif 596