1 /* linux/arch/sparc/kernel/signal.c 2 * 3 * Copyright (C) 1991, 1992 Linus Torvalds 4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) 5 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx) 6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be) 7 */ 8 9 #include <linux/sched.h> 10 #include <linux/kernel.h> 11 #include <linux/signal.h> 12 #include <linux/errno.h> 13 #include <linux/wait.h> 14 #include <linux/ptrace.h> 15 #include <linux/unistd.h> 16 #include <linux/mm.h> 17 #include <linux/tty.h> 18 #include <linux/smp.h> 19 #include <linux/binfmts.h> /* do_coredum */ 20 #include <linux/bitops.h> 21 #include <linux/tracehook.h> 22 23 #include <asm/uaccess.h> 24 #include <asm/ptrace.h> 25 #include <asm/pgalloc.h> 26 #include <asm/pgtable.h> 27 #include <asm/cacheflush.h> /* flush_sig_insns */ 28 #include <asm/switch_to.h> 29 30 #include "sigutil.h" 31 32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 33 34 extern void fpsave(unsigned long *fpregs, unsigned long *fsr, 35 void *fpqueue, unsigned long *fpqdepth); 36 extern void fpload(unsigned long *fpregs, unsigned long *fsr); 37 38 struct signal_frame { 39 struct sparc_stackf ss; 40 __siginfo32_t info; 41 __siginfo_fpu_t __user *fpu_save; 42 unsigned long insns[2] __attribute__ ((aligned (8))); 43 unsigned int extramask[_NSIG_WORDS - 1]; 44 unsigned int extra_size; /* Should be 0 */ 45 __siginfo_rwin_t __user *rwin_save; 46 } __attribute__((aligned(8))); 47 48 struct rt_signal_frame { 49 struct sparc_stackf ss; 50 siginfo_t info; 51 struct pt_regs regs; 52 sigset_t mask; 53 __siginfo_fpu_t __user *fpu_save; 54 unsigned int insns[2]; 55 stack_t stack; 56 unsigned int extra_size; /* Should be 0 */ 57 __siginfo_rwin_t __user *rwin_save; 58 } __attribute__((aligned(8))); 59 60 /* Align macros */ 61 #define SF_ALIGNEDSZ (((sizeof(struct signal_frame) + 7) & (~7))) 62 #define RT_ALIGNEDSZ (((sizeof(struct rt_signal_frame) + 7) & (~7))) 63 64 static int _sigpause_common(old_sigset_t set) 65 { 66 sigset_t blocked; 67 siginitset(&blocked, set); 68 return sigsuspend(&blocked); 69 } 70 71 asmlinkage int sys_sigsuspend(old_sigset_t set) 72 { 73 return _sigpause_common(set); 74 } 75 76 asmlinkage void do_sigreturn(struct pt_regs *regs) 77 { 78 struct signal_frame __user *sf; 79 unsigned long up_psr, pc, npc; 80 sigset_t set; 81 __siginfo_fpu_t __user *fpu_save; 82 __siginfo_rwin_t __user *rwin_save; 83 int err; 84 85 /* Always make any pending restarted system calls return -EINTR */ 86 current_thread_info()->restart_block.fn = do_no_restart_syscall; 87 88 synchronize_user_stack(); 89 90 sf = (struct signal_frame __user *) regs->u_regs[UREG_FP]; 91 92 /* 1. Make sure we are not getting garbage from the user */ 93 if (!access_ok(VERIFY_READ, sf, sizeof(*sf))) 94 goto segv_and_exit; 95 96 if (((unsigned long) sf) & 3) 97 goto segv_and_exit; 98 99 err = __get_user(pc, &sf->info.si_regs.pc); 100 err |= __get_user(npc, &sf->info.si_regs.npc); 101 102 if ((pc | npc) & 3) 103 goto segv_and_exit; 104 105 /* 2. Restore the state */ 106 up_psr = regs->psr; 107 err |= __copy_from_user(regs, &sf->info.si_regs, sizeof(struct pt_regs)); 108 109 /* User can only change condition codes and FPU enabling in %psr. */ 110 regs->psr = (up_psr & ~(PSR_ICC | PSR_EF)) 111 | (regs->psr & (PSR_ICC | PSR_EF)); 112 113 /* Prevent syscall restart. */ 114 pt_regs_clear_syscall(regs); 115 116 err |= __get_user(fpu_save, &sf->fpu_save); 117 if (fpu_save) 118 err |= restore_fpu_state(regs, fpu_save); 119 err |= __get_user(rwin_save, &sf->rwin_save); 120 if (rwin_save) 121 err |= restore_rwin_state(rwin_save); 122 123 /* This is pretty much atomic, no amount locking would prevent 124 * the races which exist anyways. 125 */ 126 err |= __get_user(set.sig[0], &sf->info.si_mask); 127 err |= __copy_from_user(&set.sig[1], &sf->extramask, 128 (_NSIG_WORDS-1) * sizeof(unsigned int)); 129 130 if (err) 131 goto segv_and_exit; 132 133 sigdelsetmask(&set, ~_BLOCKABLE); 134 set_current_blocked(&set); 135 return; 136 137 segv_and_exit: 138 force_sig(SIGSEGV, current); 139 } 140 141 asmlinkage void do_rt_sigreturn(struct pt_regs *regs) 142 { 143 struct rt_signal_frame __user *sf; 144 unsigned int psr, pc, npc; 145 __siginfo_fpu_t __user *fpu_save; 146 __siginfo_rwin_t __user *rwin_save; 147 mm_segment_t old_fs; 148 sigset_t set; 149 stack_t st; 150 int err; 151 152 synchronize_user_stack(); 153 sf = (struct rt_signal_frame __user *) regs->u_regs[UREG_FP]; 154 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) || 155 (((unsigned long) sf) & 0x03)) 156 goto segv; 157 158 err = __get_user(pc, &sf->regs.pc); 159 err |= __get_user(npc, &sf->regs.npc); 160 err |= ((pc | npc) & 0x03); 161 162 err |= __get_user(regs->y, &sf->regs.y); 163 err |= __get_user(psr, &sf->regs.psr); 164 165 err |= __copy_from_user(®s->u_regs[UREG_G1], 166 &sf->regs.u_regs[UREG_G1], 15 * sizeof(u32)); 167 168 regs->psr = (regs->psr & ~PSR_ICC) | (psr & PSR_ICC); 169 170 /* Prevent syscall restart. */ 171 pt_regs_clear_syscall(regs); 172 173 err |= __get_user(fpu_save, &sf->fpu_save); 174 if (!err && fpu_save) 175 err |= restore_fpu_state(regs, fpu_save); 176 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t)); 177 178 err |= __copy_from_user(&st, &sf->stack, sizeof(stack_t)); 179 180 if (err) 181 goto segv; 182 183 regs->pc = pc; 184 regs->npc = npc; 185 186 /* It is more difficult to avoid calling this function than to 187 * call it and ignore errors. 188 */ 189 old_fs = get_fs(); 190 set_fs(KERNEL_DS); 191 do_sigaltstack((const stack_t __user *) &st, NULL, (unsigned long)sf); 192 set_fs(old_fs); 193 194 err |= __get_user(rwin_save, &sf->rwin_save); 195 if (!err && rwin_save) { 196 if (restore_rwin_state(rwin_save)) 197 goto segv; 198 } 199 200 sigdelsetmask(&set, ~_BLOCKABLE); 201 set_current_blocked(&set); 202 return; 203 segv: 204 force_sig(SIGSEGV, current); 205 } 206 207 /* Checks if the fp is valid */ 208 static inline int invalid_frame_pointer(void __user *fp, int fplen) 209 { 210 if ((((unsigned long) fp) & 7) || !__access_ok((unsigned long)fp, fplen)) 211 return 1; 212 213 return 0; 214 } 215 216 static inline void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize) 217 { 218 unsigned long sp = regs->u_regs[UREG_FP]; 219 220 /* 221 * If we are on the alternate signal stack and would overflow it, don't. 222 * Return an always-bogus address instead so we will die with SIGSEGV. 223 */ 224 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) 225 return (void __user *) -1L; 226 227 /* This is the X/Open sanctioned signal stack switching. */ 228 if (sa->sa_flags & SA_ONSTACK) { 229 if (sas_ss_flags(sp) == 0) 230 sp = current->sas_ss_sp + current->sas_ss_size; 231 } 232 233 sp -= framesize; 234 235 /* Always align the stack frame. This handles two cases. First, 236 * sigaltstack need not be mindful of platform specific stack 237 * alignment. Second, if we took this signal because the stack 238 * is not aligned properly, we'd like to take the signal cleanly 239 * and report that. 240 */ 241 sp &= ~15UL; 242 243 return (void __user *) sp; 244 } 245 246 static int setup_frame(struct k_sigaction *ka, struct pt_regs *regs, 247 int signo, sigset_t *oldset) 248 { 249 struct signal_frame __user *sf; 250 int sigframe_size, err, wsaved; 251 void __user *tail; 252 253 /* 1. Make sure everything is clean */ 254 synchronize_user_stack(); 255 256 wsaved = current_thread_info()->w_saved; 257 258 sigframe_size = sizeof(*sf); 259 if (used_math()) 260 sigframe_size += sizeof(__siginfo_fpu_t); 261 if (wsaved) 262 sigframe_size += sizeof(__siginfo_rwin_t); 263 264 sf = (struct signal_frame __user *) 265 get_sigframe(&ka->sa, regs, sigframe_size); 266 267 if (invalid_frame_pointer(sf, sigframe_size)) 268 goto sigill_and_return; 269 270 tail = sf + 1; 271 272 /* 2. Save the current process state */ 273 err = __copy_to_user(&sf->info.si_regs, regs, sizeof(struct pt_regs)); 274 275 err |= __put_user(0, &sf->extra_size); 276 277 if (used_math()) { 278 __siginfo_fpu_t __user *fp = tail; 279 tail += sizeof(*fp); 280 err |= save_fpu_state(regs, fp); 281 err |= __put_user(fp, &sf->fpu_save); 282 } else { 283 err |= __put_user(0, &sf->fpu_save); 284 } 285 if (wsaved) { 286 __siginfo_rwin_t __user *rwp = tail; 287 tail += sizeof(*rwp); 288 err |= save_rwin_state(wsaved, rwp); 289 err |= __put_user(rwp, &sf->rwin_save); 290 } else { 291 err |= __put_user(0, &sf->rwin_save); 292 } 293 294 err |= __put_user(oldset->sig[0], &sf->info.si_mask); 295 err |= __copy_to_user(sf->extramask, &oldset->sig[1], 296 (_NSIG_WORDS - 1) * sizeof(unsigned int)); 297 if (!wsaved) { 298 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP], 299 sizeof(struct reg_window32)); 300 } else { 301 struct reg_window32 *rp; 302 303 rp = ¤t_thread_info()->reg_window[wsaved - 1]; 304 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32)); 305 } 306 if (err) 307 goto sigsegv; 308 309 /* 3. signal handler back-trampoline and parameters */ 310 regs->u_regs[UREG_FP] = (unsigned long) sf; 311 regs->u_regs[UREG_I0] = signo; 312 regs->u_regs[UREG_I1] = (unsigned long) &sf->info; 313 regs->u_regs[UREG_I2] = (unsigned long) &sf->info; 314 315 /* 4. signal handler */ 316 regs->pc = (unsigned long) ka->sa.sa_handler; 317 regs->npc = (regs->pc + 4); 318 319 /* 5. return to kernel instructions */ 320 if (ka->ka_restorer) 321 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer; 322 else { 323 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2); 324 325 /* mov __NR_sigreturn, %g1 */ 326 err |= __put_user(0x821020d8, &sf->insns[0]); 327 328 /* t 0x10 */ 329 err |= __put_user(0x91d02010, &sf->insns[1]); 330 if (err) 331 goto sigsegv; 332 333 /* Flush instruction space. */ 334 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0])); 335 } 336 return 0; 337 338 sigill_and_return: 339 do_exit(SIGILL); 340 return -EINVAL; 341 342 sigsegv: 343 force_sigsegv(signo, current); 344 return -EFAULT; 345 } 346 347 static int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs, 348 int signo, sigset_t *oldset, siginfo_t *info) 349 { 350 struct rt_signal_frame __user *sf; 351 int sigframe_size, wsaved; 352 void __user *tail; 353 unsigned int psr; 354 int err; 355 356 synchronize_user_stack(); 357 wsaved = current_thread_info()->w_saved; 358 sigframe_size = sizeof(*sf); 359 if (used_math()) 360 sigframe_size += sizeof(__siginfo_fpu_t); 361 if (wsaved) 362 sigframe_size += sizeof(__siginfo_rwin_t); 363 sf = (struct rt_signal_frame __user *) 364 get_sigframe(&ka->sa, regs, sigframe_size); 365 if (invalid_frame_pointer(sf, sigframe_size)) 366 goto sigill; 367 368 tail = sf + 1; 369 err = __put_user(regs->pc, &sf->regs.pc); 370 err |= __put_user(regs->npc, &sf->regs.npc); 371 err |= __put_user(regs->y, &sf->regs.y); 372 psr = regs->psr; 373 if (used_math()) 374 psr |= PSR_EF; 375 err |= __put_user(psr, &sf->regs.psr); 376 err |= __copy_to_user(&sf->regs.u_regs, regs->u_regs, sizeof(regs->u_regs)); 377 err |= __put_user(0, &sf->extra_size); 378 379 if (psr & PSR_EF) { 380 __siginfo_fpu_t *fp = tail; 381 tail += sizeof(*fp); 382 err |= save_fpu_state(regs, fp); 383 err |= __put_user(fp, &sf->fpu_save); 384 } else { 385 err |= __put_user(0, &sf->fpu_save); 386 } 387 if (wsaved) { 388 __siginfo_rwin_t *rwp = tail; 389 tail += sizeof(*rwp); 390 err |= save_rwin_state(wsaved, rwp); 391 err |= __put_user(rwp, &sf->rwin_save); 392 } else { 393 err |= __put_user(0, &sf->rwin_save); 394 } 395 err |= __copy_to_user(&sf->mask, &oldset->sig[0], sizeof(sigset_t)); 396 397 /* Setup sigaltstack */ 398 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp); 399 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags); 400 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size); 401 402 if (!wsaved) { 403 err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP], 404 sizeof(struct reg_window32)); 405 } else { 406 struct reg_window32 *rp; 407 408 rp = ¤t_thread_info()->reg_window[wsaved - 1]; 409 err |= __copy_to_user(sf, rp, sizeof(struct reg_window32)); 410 } 411 412 err |= copy_siginfo_to_user(&sf->info, info); 413 414 if (err) 415 goto sigsegv; 416 417 regs->u_regs[UREG_FP] = (unsigned long) sf; 418 regs->u_regs[UREG_I0] = signo; 419 regs->u_regs[UREG_I1] = (unsigned long) &sf->info; 420 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs; 421 422 regs->pc = (unsigned long) ka->sa.sa_handler; 423 regs->npc = (regs->pc + 4); 424 425 if (ka->ka_restorer) 426 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer; 427 else { 428 regs->u_regs[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2); 429 430 /* mov __NR_sigreturn, %g1 */ 431 err |= __put_user(0x821020d8, &sf->insns[0]); 432 433 /* t 0x10 */ 434 err |= __put_user(0x91d02010, &sf->insns[1]); 435 if (err) 436 goto sigsegv; 437 438 /* Flush instruction space. */ 439 flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0])); 440 } 441 return 0; 442 443 sigill: 444 do_exit(SIGILL); 445 return -EINVAL; 446 447 sigsegv: 448 force_sigsegv(signo, current); 449 return -EFAULT; 450 } 451 452 static inline int 453 handle_signal(unsigned long signr, struct k_sigaction *ka, 454 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs) 455 { 456 int err; 457 458 if (ka->sa.sa_flags & SA_SIGINFO) 459 err = setup_rt_frame(ka, regs, signr, oldset, info); 460 else 461 err = setup_frame(ka, regs, signr, oldset); 462 463 if (err) 464 return err; 465 466 block_sigmask(ka, signr); 467 tracehook_signal_handler(signr, info, ka, regs, 0); 468 469 return 0; 470 } 471 472 static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs, 473 struct sigaction *sa) 474 { 475 switch(regs->u_regs[UREG_I0]) { 476 case ERESTART_RESTARTBLOCK: 477 case ERESTARTNOHAND: 478 no_system_call_restart: 479 regs->u_regs[UREG_I0] = EINTR; 480 regs->psr |= PSR_C; 481 break; 482 case ERESTARTSYS: 483 if (!(sa->sa_flags & SA_RESTART)) 484 goto no_system_call_restart; 485 /* fallthrough */ 486 case ERESTARTNOINTR: 487 regs->u_regs[UREG_I0] = orig_i0; 488 regs->pc -= 4; 489 regs->npc -= 4; 490 } 491 } 492 493 /* Note that 'init' is a special process: it doesn't get signals it doesn't 494 * want to handle. Thus you cannot kill init even with a SIGKILL even by 495 * mistake. 496 */ 497 static void do_signal(struct pt_regs *regs, unsigned long orig_i0) 498 { 499 struct k_sigaction ka; 500 int restart_syscall; 501 sigset_t *oldset; 502 siginfo_t info; 503 int signr; 504 505 /* It's a lot of work and synchronization to add a new ptrace 506 * register for GDB to save and restore in order to get 507 * orig_i0 correct for syscall restarts when debugging. 508 * 509 * Although it should be the case that most of the global 510 * registers are volatile across a system call, glibc already 511 * depends upon that fact that we preserve them. So we can't 512 * just use any global register to save away the orig_i0 value. 513 * 514 * In particular %g2, %g3, %g4, and %g5 are all assumed to be 515 * preserved across a system call trap by various pieces of 516 * code in glibc. 517 * 518 * %g7 is used as the "thread register". %g6 is not used in 519 * any fixed manner. %g6 is used as a scratch register and 520 * a compiler temporary, but it's value is never used across 521 * a system call. Therefore %g6 is usable for orig_i0 storage. 522 */ 523 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) 524 regs->u_regs[UREG_G6] = orig_i0; 525 526 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 527 oldset = ¤t->saved_sigmask; 528 else 529 oldset = ¤t->blocked; 530 531 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 532 533 /* If the debugger messes with the program counter, it clears 534 * the software "in syscall" bit, directing us to not perform 535 * a syscall restart. 536 */ 537 restart_syscall = 0; 538 if (pt_regs_is_syscall(regs) && (regs->psr & PSR_C)) { 539 restart_syscall = 1; 540 orig_i0 = regs->u_regs[UREG_G6]; 541 } 542 543 544 if (signr > 0) { 545 if (restart_syscall) 546 syscall_restart(orig_i0, regs, &ka.sa); 547 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) { 548 /* a signal was successfully delivered; the saved 549 * sigmask will have been stored in the signal frame, 550 * and will be restored by sigreturn, so we can simply 551 * clear the TIF_RESTORE_SIGMASK flag. 552 */ 553 if (test_thread_flag(TIF_RESTORE_SIGMASK)) 554 clear_thread_flag(TIF_RESTORE_SIGMASK); 555 } 556 return; 557 } 558 if (restart_syscall && 559 (regs->u_regs[UREG_I0] == ERESTARTNOHAND || 560 regs->u_regs[UREG_I0] == ERESTARTSYS || 561 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) { 562 /* replay the system call when we are done */ 563 regs->u_regs[UREG_I0] = orig_i0; 564 regs->pc -= 4; 565 regs->npc -= 4; 566 pt_regs_clear_syscall(regs); 567 } 568 if (restart_syscall && 569 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) { 570 regs->u_regs[UREG_G1] = __NR_restart_syscall; 571 regs->pc -= 4; 572 regs->npc -= 4; 573 pt_regs_clear_syscall(regs); 574 } 575 576 /* if there's no signal to deliver, we just put the saved sigmask 577 * back 578 */ 579 restore_saved_sigmask(); 580 } 581 582 void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, 583 unsigned long thread_info_flags) 584 { 585 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK)) 586 do_signal(regs, orig_i0); 587 if (thread_info_flags & _TIF_NOTIFY_RESUME) { 588 clear_thread_flag(TIF_NOTIFY_RESUME); 589 tracehook_notify_resume(regs); 590 } 591 } 592 593 asmlinkage int 594 do_sys_sigstack(struct sigstack __user *ssptr, struct sigstack __user *ossptr, 595 unsigned long sp) 596 { 597 int ret = -EFAULT; 598 599 /* First see if old state is wanted. */ 600 if (ossptr) { 601 if (put_user(current->sas_ss_sp + current->sas_ss_size, 602 &ossptr->the_stack) || 603 __put_user(on_sig_stack(sp), &ossptr->cur_status)) 604 goto out; 605 } 606 607 /* Now see if we want to update the new state. */ 608 if (ssptr) { 609 char *ss_sp; 610 611 if (get_user(ss_sp, &ssptr->the_stack)) 612 goto out; 613 /* If the current stack was set with sigaltstack, don't 614 swap stacks while we are on it. */ 615 ret = -EPERM; 616 if (current->sas_ss_sp && on_sig_stack(sp)) 617 goto out; 618 619 /* Since we don't know the extent of the stack, and we don't 620 track onstack-ness, but rather calculate it, we must 621 presume a size. Ho hum this interface is lossy. */ 622 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ; 623 current->sas_ss_size = SIGSTKSZ; 624 } 625 ret = 0; 626 out: 627 return ret; 628 } 629