1 /* 2 * linux/arch/alpha/kernel/signal.c 3 * 4 * Copyright (C) 1995 Linus Torvalds 5 * 6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson 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/smp.h> 18 #include <linux/stddef.h> 19 #include <linux/tty.h> 20 #include <linux/binfmts.h> 21 #include <linux/bitops.h> 22 #include <linux/syscalls.h> 23 #include <linux/tracehook.h> 24 25 #include <asm/uaccess.h> 26 #include <asm/sigcontext.h> 27 #include <asm/ucontext.h> 28 29 #include "proto.h" 30 31 32 #define DEBUG_SIG 0 33 34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 35 36 asmlinkage void ret_from_sys_call(void); 37 38 /* 39 * The OSF/1 sigprocmask calling sequence is different from the 40 * C sigprocmask() sequence.. 41 */ 42 SYSCALL_DEFINE2(osf_sigprocmask, int, how, unsigned long, newmask) 43 { 44 sigset_t oldmask; 45 sigset_t mask; 46 unsigned long res; 47 48 siginitset(&mask, newmask & _BLOCKABLE); 49 res = sigprocmask(how, &mask, &oldmask); 50 if (!res) { 51 force_successful_syscall_return(); 52 res = oldmask.sig[0]; 53 } 54 return res; 55 } 56 57 SYSCALL_DEFINE3(osf_sigaction, int, sig, 58 const struct osf_sigaction __user *, act, 59 struct osf_sigaction __user *, oact) 60 { 61 struct k_sigaction new_ka, old_ka; 62 int ret; 63 64 if (act) { 65 old_sigset_t mask; 66 if (!access_ok(VERIFY_READ, act, sizeof(*act)) || 67 __get_user(new_ka.sa.sa_handler, &act->sa_handler) || 68 __get_user(new_ka.sa.sa_flags, &act->sa_flags) || 69 __get_user(mask, &act->sa_mask)) 70 return -EFAULT; 71 siginitset(&new_ka.sa.sa_mask, mask); 72 new_ka.ka_restorer = NULL; 73 } 74 75 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 76 77 if (!ret && oact) { 78 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || 79 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || 80 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) || 81 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask)) 82 return -EFAULT; 83 } 84 85 return ret; 86 } 87 88 SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act, 89 struct sigaction __user *, oact, 90 size_t, sigsetsize, void __user *, restorer) 91 { 92 struct k_sigaction new_ka, old_ka; 93 int ret; 94 95 /* XXX: Don't preclude handling different sized sigset_t's. */ 96 if (sigsetsize != sizeof(sigset_t)) 97 return -EINVAL; 98 99 if (act) { 100 new_ka.ka_restorer = restorer; 101 if (copy_from_user(&new_ka.sa, act, sizeof(*act))) 102 return -EFAULT; 103 } 104 105 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 106 107 if (!ret && oact) { 108 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact))) 109 return -EFAULT; 110 } 111 112 return ret; 113 } 114 115 /* 116 * Atomically swap in the new signal mask, and wait for a signal. 117 */ 118 SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask) 119 { 120 sigset_t blocked; 121 siginitset(&blocked, mask); 122 return sigsuspend(&blocked); 123 } 124 125 /* 126 * Do a signal return; undo the signal stack. 127 */ 128 129 #if _NSIG_WORDS > 1 130 # error "Non SA_SIGINFO frame needs rearranging" 131 #endif 132 133 struct sigframe 134 { 135 struct sigcontext sc; 136 unsigned int retcode[3]; 137 }; 138 139 struct rt_sigframe 140 { 141 struct siginfo info; 142 struct ucontext uc; 143 unsigned int retcode[3]; 144 }; 145 146 /* If this changes, userland unwinders that Know Things about our signal 147 frame will break. Do not undertake lightly. It also implies an ABI 148 change wrt the size of siginfo_t, which may cause some pain. */ 149 extern char compile_time_assert 150 [offsetof(struct rt_sigframe, uc.uc_mcontext) == 176 ? 1 : -1]; 151 152 #define INSN_MOV_R30_R16 0x47fe0410 153 #define INSN_LDI_R0 0x201f0000 154 #define INSN_CALLSYS 0x00000083 155 156 static long 157 restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs) 158 { 159 unsigned long usp; 160 struct switch_stack *sw = (struct switch_stack *)regs - 1; 161 long i, err = __get_user(regs->pc, &sc->sc_pc); 162 163 current_thread_info()->restart_block.fn = do_no_restart_syscall; 164 165 sw->r26 = (unsigned long) ret_from_sys_call; 166 167 err |= __get_user(regs->r0, sc->sc_regs+0); 168 err |= __get_user(regs->r1, sc->sc_regs+1); 169 err |= __get_user(regs->r2, sc->sc_regs+2); 170 err |= __get_user(regs->r3, sc->sc_regs+3); 171 err |= __get_user(regs->r4, sc->sc_regs+4); 172 err |= __get_user(regs->r5, sc->sc_regs+5); 173 err |= __get_user(regs->r6, sc->sc_regs+6); 174 err |= __get_user(regs->r7, sc->sc_regs+7); 175 err |= __get_user(regs->r8, sc->sc_regs+8); 176 err |= __get_user(sw->r9, sc->sc_regs+9); 177 err |= __get_user(sw->r10, sc->sc_regs+10); 178 err |= __get_user(sw->r11, sc->sc_regs+11); 179 err |= __get_user(sw->r12, sc->sc_regs+12); 180 err |= __get_user(sw->r13, sc->sc_regs+13); 181 err |= __get_user(sw->r14, sc->sc_regs+14); 182 err |= __get_user(sw->r15, sc->sc_regs+15); 183 err |= __get_user(regs->r16, sc->sc_regs+16); 184 err |= __get_user(regs->r17, sc->sc_regs+17); 185 err |= __get_user(regs->r18, sc->sc_regs+18); 186 err |= __get_user(regs->r19, sc->sc_regs+19); 187 err |= __get_user(regs->r20, sc->sc_regs+20); 188 err |= __get_user(regs->r21, sc->sc_regs+21); 189 err |= __get_user(regs->r22, sc->sc_regs+22); 190 err |= __get_user(regs->r23, sc->sc_regs+23); 191 err |= __get_user(regs->r24, sc->sc_regs+24); 192 err |= __get_user(regs->r25, sc->sc_regs+25); 193 err |= __get_user(regs->r26, sc->sc_regs+26); 194 err |= __get_user(regs->r27, sc->sc_regs+27); 195 err |= __get_user(regs->r28, sc->sc_regs+28); 196 err |= __get_user(regs->gp, sc->sc_regs+29); 197 err |= __get_user(usp, sc->sc_regs+30); 198 wrusp(usp); 199 200 for (i = 0; i < 31; i++) 201 err |= __get_user(sw->fp[i], sc->sc_fpregs+i); 202 err |= __get_user(sw->fp[31], &sc->sc_fpcr); 203 204 return err; 205 } 206 207 /* Note that this syscall is also used by setcontext(3) to install 208 a given sigcontext. This because it's impossible to set *all* 209 registers and transfer control from userland. */ 210 211 asmlinkage void 212 do_sigreturn(struct sigcontext __user *sc) 213 { 214 struct pt_regs *regs = current_pt_regs(); 215 sigset_t set; 216 217 /* Verify that it's a good sigcontext before using it */ 218 if (!access_ok(VERIFY_READ, sc, sizeof(*sc))) 219 goto give_sigsegv; 220 if (__get_user(set.sig[0], &sc->sc_mask)) 221 goto give_sigsegv; 222 223 set_current_blocked(&set); 224 225 if (restore_sigcontext(sc, regs)) 226 goto give_sigsegv; 227 228 /* Send SIGTRAP if we're single-stepping: */ 229 if (ptrace_cancel_bpt (current)) { 230 siginfo_t info; 231 232 info.si_signo = SIGTRAP; 233 info.si_errno = 0; 234 info.si_code = TRAP_BRKPT; 235 info.si_addr = (void __user *) regs->pc; 236 info.si_trapno = 0; 237 send_sig_info(SIGTRAP, &info, current); 238 } 239 return; 240 241 give_sigsegv: 242 force_sig(SIGSEGV, current); 243 } 244 245 asmlinkage void 246 do_rt_sigreturn(struct rt_sigframe __user *frame) 247 { 248 struct pt_regs *regs = current_pt_regs(); 249 sigset_t set; 250 251 /* Verify that it's a good ucontext_t before using it */ 252 if (!access_ok(VERIFY_READ, &frame->uc, sizeof(frame->uc))) 253 goto give_sigsegv; 254 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 255 goto give_sigsegv; 256 257 set_current_blocked(&set); 258 259 if (restore_sigcontext(&frame->uc.uc_mcontext, regs)) 260 goto give_sigsegv; 261 262 /* Send SIGTRAP if we're single-stepping: */ 263 if (ptrace_cancel_bpt (current)) { 264 siginfo_t info; 265 266 info.si_signo = SIGTRAP; 267 info.si_errno = 0; 268 info.si_code = TRAP_BRKPT; 269 info.si_addr = (void __user *) regs->pc; 270 info.si_trapno = 0; 271 send_sig_info(SIGTRAP, &info, current); 272 } 273 return; 274 275 give_sigsegv: 276 force_sig(SIGSEGV, current); 277 } 278 279 280 /* 281 * Set up a signal frame. 282 */ 283 284 static inline void __user * 285 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size) 286 { 287 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! sas_ss_flags(sp)) 288 sp = current->sas_ss_sp + current->sas_ss_size; 289 290 return (void __user *)((sp - frame_size) & -32ul); 291 } 292 293 static long 294 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, 295 unsigned long mask, unsigned long sp) 296 { 297 struct switch_stack *sw = (struct switch_stack *)regs - 1; 298 long i, err = 0; 299 300 err |= __put_user(on_sig_stack((unsigned long)sc), &sc->sc_onstack); 301 err |= __put_user(mask, &sc->sc_mask); 302 err |= __put_user(regs->pc, &sc->sc_pc); 303 err |= __put_user(8, &sc->sc_ps); 304 305 err |= __put_user(regs->r0 , sc->sc_regs+0); 306 err |= __put_user(regs->r1 , sc->sc_regs+1); 307 err |= __put_user(regs->r2 , sc->sc_regs+2); 308 err |= __put_user(regs->r3 , sc->sc_regs+3); 309 err |= __put_user(regs->r4 , sc->sc_regs+4); 310 err |= __put_user(regs->r5 , sc->sc_regs+5); 311 err |= __put_user(regs->r6 , sc->sc_regs+6); 312 err |= __put_user(regs->r7 , sc->sc_regs+7); 313 err |= __put_user(regs->r8 , sc->sc_regs+8); 314 err |= __put_user(sw->r9 , sc->sc_regs+9); 315 err |= __put_user(sw->r10 , sc->sc_regs+10); 316 err |= __put_user(sw->r11 , sc->sc_regs+11); 317 err |= __put_user(sw->r12 , sc->sc_regs+12); 318 err |= __put_user(sw->r13 , sc->sc_regs+13); 319 err |= __put_user(sw->r14 , sc->sc_regs+14); 320 err |= __put_user(sw->r15 , sc->sc_regs+15); 321 err |= __put_user(regs->r16, sc->sc_regs+16); 322 err |= __put_user(regs->r17, sc->sc_regs+17); 323 err |= __put_user(regs->r18, sc->sc_regs+18); 324 err |= __put_user(regs->r19, sc->sc_regs+19); 325 err |= __put_user(regs->r20, sc->sc_regs+20); 326 err |= __put_user(regs->r21, sc->sc_regs+21); 327 err |= __put_user(regs->r22, sc->sc_regs+22); 328 err |= __put_user(regs->r23, sc->sc_regs+23); 329 err |= __put_user(regs->r24, sc->sc_regs+24); 330 err |= __put_user(regs->r25, sc->sc_regs+25); 331 err |= __put_user(regs->r26, sc->sc_regs+26); 332 err |= __put_user(regs->r27, sc->sc_regs+27); 333 err |= __put_user(regs->r28, sc->sc_regs+28); 334 err |= __put_user(regs->gp , sc->sc_regs+29); 335 err |= __put_user(sp, sc->sc_regs+30); 336 err |= __put_user(0, sc->sc_regs+31); 337 338 for (i = 0; i < 31; i++) 339 err |= __put_user(sw->fp[i], sc->sc_fpregs+i); 340 err |= __put_user(0, sc->sc_fpregs+31); 341 err |= __put_user(sw->fp[31], &sc->sc_fpcr); 342 343 err |= __put_user(regs->trap_a0, &sc->sc_traparg_a0); 344 err |= __put_user(regs->trap_a1, &sc->sc_traparg_a1); 345 err |= __put_user(regs->trap_a2, &sc->sc_traparg_a2); 346 347 return err; 348 } 349 350 static int 351 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set, 352 struct pt_regs *regs) 353 { 354 unsigned long oldsp, r26, err = 0; 355 struct sigframe __user *frame; 356 357 oldsp = rdusp(); 358 frame = get_sigframe(ka, oldsp, sizeof(*frame)); 359 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 360 return -EFAULT; 361 362 err |= setup_sigcontext(&frame->sc, regs, set->sig[0], oldsp); 363 if (err) 364 return -EFAULT; 365 366 /* Set up to return from userspace. If provided, use a stub 367 already in userspace. */ 368 if (ka->ka_restorer) { 369 r26 = (unsigned long) ka->ka_restorer; 370 } else { 371 err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0); 372 err |= __put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1); 373 err |= __put_user(INSN_CALLSYS, frame->retcode+2); 374 imb(); 375 r26 = (unsigned long) frame->retcode; 376 } 377 378 /* Check that everything was written properly. */ 379 if (err) 380 return err; 381 382 /* "Return" to the handler */ 383 regs->r26 = r26; 384 regs->r27 = regs->pc = (unsigned long) ka->sa.sa_handler; 385 regs->r16 = sig; /* a0: signal number */ 386 regs->r17 = 0; /* a1: exception code */ 387 regs->r18 = (unsigned long) &frame->sc; /* a2: sigcontext pointer */ 388 wrusp((unsigned long) frame); 389 390 #if DEBUG_SIG 391 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", 392 current->comm, current->pid, frame, regs->pc, regs->r26); 393 #endif 394 return 0; 395 } 396 397 static int 398 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 399 sigset_t *set, struct pt_regs *regs) 400 { 401 unsigned long oldsp, r26, err = 0; 402 struct rt_sigframe __user *frame; 403 404 oldsp = rdusp(); 405 frame = get_sigframe(ka, oldsp, sizeof(*frame)); 406 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 407 return -EFAULT; 408 409 err |= copy_siginfo_to_user(&frame->info, info); 410 411 /* Create the ucontext. */ 412 err |= __put_user(0, &frame->uc.uc_flags); 413 err |= __put_user(0, &frame->uc.uc_link); 414 err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask); 415 err |= __save_altstack(&frame->uc.uc_stack, oldsp); 416 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, 417 set->sig[0], oldsp); 418 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 419 if (err) 420 return -EFAULT; 421 422 /* Set up to return from userspace. If provided, use a stub 423 already in userspace. */ 424 if (ka->ka_restorer) { 425 r26 = (unsigned long) ka->ka_restorer; 426 } else { 427 err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0); 428 err |= __put_user(INSN_LDI_R0+__NR_rt_sigreturn, 429 frame->retcode+1); 430 err |= __put_user(INSN_CALLSYS, frame->retcode+2); 431 imb(); 432 r26 = (unsigned long) frame->retcode; 433 } 434 435 if (err) 436 return -EFAULT; 437 438 /* "Return" to the handler */ 439 regs->r26 = r26; 440 regs->r27 = regs->pc = (unsigned long) ka->sa.sa_handler; 441 regs->r16 = sig; /* a0: signal number */ 442 regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */ 443 regs->r18 = (unsigned long) &frame->uc; /* a2: ucontext pointer */ 444 wrusp((unsigned long) frame); 445 446 #if DEBUG_SIG 447 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", 448 current->comm, current->pid, frame, regs->pc, regs->r26); 449 #endif 450 451 return 0; 452 } 453 454 455 /* 456 * OK, we're invoking a handler. 457 */ 458 static inline void 459 handle_signal(int sig, struct k_sigaction *ka, siginfo_t *info, 460 struct pt_regs * regs) 461 { 462 sigset_t *oldset = sigmask_to_save(); 463 int ret; 464 465 if (ka->sa.sa_flags & SA_SIGINFO) 466 ret = setup_rt_frame(sig, ka, info, oldset, regs); 467 else 468 ret = setup_frame(sig, ka, oldset, regs); 469 470 if (ret) { 471 force_sigsegv(sig, current); 472 return; 473 } 474 signal_delivered(sig, info, ka, regs, 0); 475 } 476 477 static inline void 478 syscall_restart(unsigned long r0, unsigned long r19, 479 struct pt_regs *regs, struct k_sigaction *ka) 480 { 481 switch (regs->r0) { 482 case ERESTARTSYS: 483 if (!(ka->sa.sa_flags & SA_RESTART)) { 484 case ERESTARTNOHAND: 485 regs->r0 = EINTR; 486 break; 487 } 488 /* fallthrough */ 489 case ERESTARTNOINTR: 490 regs->r0 = r0; /* reset v0 and a3 and replay syscall */ 491 regs->r19 = r19; 492 regs->pc -= 4; 493 break; 494 case ERESTART_RESTARTBLOCK: 495 regs->r0 = EINTR; 496 break; 497 } 498 } 499 500 501 /* 502 * Note that 'init' is a special process: it doesn't get signals it doesn't 503 * want to handle. Thus you cannot kill init even with a SIGKILL even by 504 * mistake. 505 * 506 * Note that we go through the signals twice: once to check the signals that 507 * the kernel can handle, and then we build all the user-level signal handling 508 * stack-frames in one go after that. 509 * 510 * "r0" and "r19" are the registers we need to restore for system call 511 * restart. "r0" is also used as an indicator whether we can restart at 512 * all (if we get here from anything but a syscall return, it will be 0) 513 */ 514 static void 515 do_signal(struct pt_regs *regs, unsigned long r0, unsigned long r19) 516 { 517 siginfo_t info; 518 int signr; 519 unsigned long single_stepping = ptrace_cancel_bpt(current); 520 struct k_sigaction ka; 521 522 /* This lets the debugger run, ... */ 523 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 524 525 /* ... so re-check the single stepping. */ 526 single_stepping |= ptrace_cancel_bpt(current); 527 528 if (signr > 0) { 529 /* Whee! Actually deliver the signal. */ 530 if (r0) 531 syscall_restart(r0, r19, regs, &ka); 532 handle_signal(signr, &ka, &info, regs); 533 if (single_stepping) 534 ptrace_set_bpt(current); /* re-set bpt */ 535 return; 536 } 537 538 if (r0) { 539 switch (regs->r0) { 540 case ERESTARTNOHAND: 541 case ERESTARTSYS: 542 case ERESTARTNOINTR: 543 /* Reset v0 and a3 and replay syscall. */ 544 regs->r0 = r0; 545 regs->r19 = r19; 546 regs->pc -= 4; 547 break; 548 case ERESTART_RESTARTBLOCK: 549 /* Force v0 to the restart syscall and reply. */ 550 regs->r0 = __NR_restart_syscall; 551 regs->pc -= 4; 552 break; 553 } 554 } 555 556 /* If there's no signal to deliver, we just restore the saved mask. */ 557 restore_saved_sigmask(); 558 if (single_stepping) 559 ptrace_set_bpt(current); /* re-set breakpoint */ 560 } 561 562 void 563 do_work_pending(struct pt_regs *regs, unsigned long thread_flags, 564 unsigned long r0, unsigned long r19) 565 { 566 do { 567 if (thread_flags & _TIF_NEED_RESCHED) { 568 schedule(); 569 } else { 570 local_irq_enable(); 571 if (thread_flags & _TIF_SIGPENDING) { 572 do_signal(regs, r0, r19); 573 r0 = 0; 574 } else { 575 clear_thread_flag(TIF_NOTIFY_RESUME); 576 tracehook_notify_resume(regs); 577 } 578 } 579 local_irq_disable(); 580 thread_flags = current_thread_info()->flags; 581 } while (thread_flags & _TIF_WORK_MASK); 582 } 583