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