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