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