1 /* 2 * linux/arch/sh/kernel/signal.c 3 * 4 * Copyright (C) 1991, 1992 Linus Torvalds 5 * 6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson 7 * 8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima 9 * 10 */ 11 #include <linux/sched.h> 12 #include <linux/mm.h> 13 #include <linux/smp.h> 14 #include <linux/kernel.h> 15 #include <linux/signal.h> 16 #include <linux/errno.h> 17 #include <linux/wait.h> 18 #include <linux/ptrace.h> 19 #include <linux/unistd.h> 20 #include <linux/stddef.h> 21 #include <linux/tty.h> 22 #include <linux/elf.h> 23 #include <linux/personality.h> 24 #include <linux/binfmts.h> 25 #include <linux/freezer.h> 26 #include <linux/io.h> 27 #include <linux/tracehook.h> 28 #include <asm/ucontext.h> 29 #include <asm/uaccess.h> 30 #include <asm/pgtable.h> 31 #include <asm/cacheflush.h> 32 #include <asm/syscalls.h> 33 #include <asm/fpu.h> 34 35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 36 37 struct fdpic_func_descriptor { 38 unsigned long text; 39 unsigned long GOT; 40 }; 41 42 /* 43 * The following define adds a 64 byte gap between the signal 44 * stack frame and previous contents of the stack. This allows 45 * frame unwinding in a function epilogue but only if a frame 46 * pointer is used in the function. This is necessary because 47 * current gcc compilers (<4.3) do not generate unwind info on 48 * SH for function epilogues. 49 */ 50 #define UNWINDGUARD 64 51 52 /* 53 * Atomically swap in the new signal mask, and wait for a signal. 54 */ 55 asmlinkage int 56 sys_sigsuspend(old_sigset_t mask, 57 unsigned long r5, unsigned long r6, unsigned long r7, 58 struct pt_regs __regs) 59 { 60 sigset_t blocked; 61 62 current->saved_sigmask = current->blocked; 63 64 mask &= _BLOCKABLE; 65 siginitset(&blocked, mask); 66 set_current_blocked(&blocked); 67 68 current->state = TASK_INTERRUPTIBLE; 69 schedule(); 70 set_restore_sigmask(); 71 72 return -ERESTARTNOHAND; 73 } 74 75 asmlinkage int 76 sys_sigaction(int sig, const struct old_sigaction __user *act, 77 struct old_sigaction __user *oact) 78 { 79 struct k_sigaction new_ka, old_ka; 80 int ret; 81 82 if (act) { 83 old_sigset_t mask; 84 if (!access_ok(VERIFY_READ, act, sizeof(*act)) || 85 __get_user(new_ka.sa.sa_handler, &act->sa_handler) || 86 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer)) 87 return -EFAULT; 88 __get_user(new_ka.sa.sa_flags, &act->sa_flags); 89 __get_user(mask, &act->sa_mask); 90 siginitset(&new_ka.sa.sa_mask, mask); 91 } 92 93 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 94 95 if (!ret && oact) { 96 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || 97 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || 98 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer)) 99 return -EFAULT; 100 __put_user(old_ka.sa.sa_flags, &oact->sa_flags); 101 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask); 102 } 103 104 return ret; 105 } 106 107 asmlinkage int 108 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, 109 unsigned long r6, unsigned long r7, 110 struct pt_regs __regs) 111 { 112 struct pt_regs *regs = RELOC_HIDE(&__regs, 0); 113 114 return do_sigaltstack(uss, uoss, regs->regs[15]); 115 } 116 117 118 /* 119 * Do a signal return; undo the signal stack. 120 */ 121 122 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */ 123 #if defined(CONFIG_CPU_SH2) 124 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */ 125 #else 126 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */ 127 #endif 128 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */ 129 130 struct sigframe 131 { 132 struct sigcontext sc; 133 unsigned long extramask[_NSIG_WORDS-1]; 134 u16 retcode[8]; 135 }; 136 137 struct rt_sigframe 138 { 139 struct siginfo info; 140 struct ucontext uc; 141 u16 retcode[8]; 142 }; 143 144 #ifdef CONFIG_SH_FPU 145 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc) 146 { 147 struct task_struct *tsk = current; 148 149 if (!(boot_cpu_data.flags & CPU_HAS_FPU)) 150 return 0; 151 152 set_used_math(); 153 return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0], 154 sizeof(long)*(16*2+2)); 155 } 156 157 static inline int save_sigcontext_fpu(struct sigcontext __user *sc, 158 struct pt_regs *regs) 159 { 160 struct task_struct *tsk = current; 161 162 if (!(boot_cpu_data.flags & CPU_HAS_FPU)) 163 return 0; 164 165 if (!used_math()) { 166 __put_user(0, &sc->sc_ownedfp); 167 return 0; 168 } 169 170 __put_user(1, &sc->sc_ownedfp); 171 172 /* This will cause a "finit" to be triggered by the next 173 attempted FPU operation by the 'current' process. 174 */ 175 clear_used_math(); 176 177 unlazy_fpu(tsk, regs); 178 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu, 179 sizeof(long)*(16*2+2)); 180 } 181 #endif /* CONFIG_SH_FPU */ 182 183 static int 184 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p) 185 { 186 unsigned int err = 0; 187 188 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x) 189 COPY(regs[1]); 190 COPY(regs[2]); COPY(regs[3]); 191 COPY(regs[4]); COPY(regs[5]); 192 COPY(regs[6]); COPY(regs[7]); 193 COPY(regs[8]); COPY(regs[9]); 194 COPY(regs[10]); COPY(regs[11]); 195 COPY(regs[12]); COPY(regs[13]); 196 COPY(regs[14]); COPY(regs[15]); 197 COPY(gbr); COPY(mach); 198 COPY(macl); COPY(pr); 199 COPY(sr); COPY(pc); 200 #undef COPY 201 202 #ifdef CONFIG_SH_FPU 203 if (boot_cpu_data.flags & CPU_HAS_FPU) { 204 int owned_fp; 205 struct task_struct *tsk = current; 206 207 regs->sr |= SR_FD; /* Release FPU */ 208 clear_fpu(tsk, regs); 209 clear_used_math(); 210 __get_user (owned_fp, &sc->sc_ownedfp); 211 if (owned_fp) 212 err |= restore_sigcontext_fpu(sc); 213 } 214 #endif 215 216 regs->tra = -1; /* disable syscall checks */ 217 err |= __get_user(*r0_p, &sc->sc_regs[0]); 218 return err; 219 } 220 221 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5, 222 unsigned long r6, unsigned long r7, 223 struct pt_regs __regs) 224 { 225 struct pt_regs *regs = RELOC_HIDE(&__regs, 0); 226 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15]; 227 sigset_t set; 228 int r0; 229 230 /* Always make any pending restarted system calls return -EINTR */ 231 current_thread_info()->restart_block.fn = do_no_restart_syscall; 232 233 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 234 goto badframe; 235 236 if (__get_user(set.sig[0], &frame->sc.oldmask) 237 || (_NSIG_WORDS > 1 238 && __copy_from_user(&set.sig[1], &frame->extramask, 239 sizeof(frame->extramask)))) 240 goto badframe; 241 242 sigdelsetmask(&set, ~_BLOCKABLE); 243 set_current_blocked(&set); 244 245 if (restore_sigcontext(regs, &frame->sc, &r0)) 246 goto badframe; 247 return r0; 248 249 badframe: 250 force_sig(SIGSEGV, current); 251 return 0; 252 } 253 254 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5, 255 unsigned long r6, unsigned long r7, 256 struct pt_regs __regs) 257 { 258 struct pt_regs *regs = RELOC_HIDE(&__regs, 0); 259 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15]; 260 sigset_t set; 261 int r0; 262 263 /* Always make any pending restarted system calls return -EINTR */ 264 current_thread_info()->restart_block.fn = do_no_restart_syscall; 265 266 if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) 267 goto badframe; 268 269 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 270 goto badframe; 271 272 sigdelsetmask(&set, ~_BLOCKABLE); 273 set_current_blocked(&set); 274 275 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0)) 276 goto badframe; 277 278 if (do_sigaltstack(&frame->uc.uc_stack, NULL, 279 regs->regs[15]) == -EFAULT) 280 goto badframe; 281 282 return r0; 283 284 badframe: 285 force_sig(SIGSEGV, current); 286 return 0; 287 } 288 289 /* 290 * Set up a signal frame. 291 */ 292 293 static int 294 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, 295 unsigned long mask) 296 { 297 int err = 0; 298 299 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x) 300 COPY(regs[0]); COPY(regs[1]); 301 COPY(regs[2]); COPY(regs[3]); 302 COPY(regs[4]); COPY(regs[5]); 303 COPY(regs[6]); COPY(regs[7]); 304 COPY(regs[8]); COPY(regs[9]); 305 COPY(regs[10]); COPY(regs[11]); 306 COPY(regs[12]); COPY(regs[13]); 307 COPY(regs[14]); COPY(regs[15]); 308 COPY(gbr); COPY(mach); 309 COPY(macl); COPY(pr); 310 COPY(sr); COPY(pc); 311 #undef COPY 312 313 #ifdef CONFIG_SH_FPU 314 err |= save_sigcontext_fpu(sc, regs); 315 #endif 316 317 /* non-iBCS2 extensions.. */ 318 err |= __put_user(mask, &sc->oldmask); 319 320 return err; 321 } 322 323 /* 324 * Determine which stack to use.. 325 */ 326 static inline void __user * 327 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size) 328 { 329 if (ka->sa.sa_flags & SA_ONSTACK) { 330 if (sas_ss_flags(sp) == 0) 331 sp = current->sas_ss_sp + current->sas_ss_size; 332 } 333 334 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul); 335 } 336 337 /* These symbols are defined with the addresses in the vsyscall page. 338 See vsyscall-trapa.S. */ 339 extern void __kernel_sigreturn(void); 340 extern void __kernel_rt_sigreturn(void); 341 342 static int setup_frame(int sig, struct k_sigaction *ka, 343 sigset_t *set, struct pt_regs *regs) 344 { 345 struct sigframe __user *frame; 346 int err = 0; 347 int signal; 348 349 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame)); 350 351 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 352 goto give_sigsegv; 353 354 signal = current_thread_info()->exec_domain 355 && current_thread_info()->exec_domain->signal_invmap 356 && sig < 32 357 ? current_thread_info()->exec_domain->signal_invmap[sig] 358 : sig; 359 360 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]); 361 362 if (_NSIG_WORDS > 1) 363 err |= __copy_to_user(frame->extramask, &set->sig[1], 364 sizeof(frame->extramask)); 365 366 /* Set up to return from userspace. If provided, use a stub 367 already in userspace. */ 368 if (ka->sa.sa_flags & SA_RESTORER) { 369 regs->pr = (unsigned long) ka->sa.sa_restorer; 370 #ifdef CONFIG_VSYSCALL 371 } else if (likely(current->mm->context.vdso)) { 372 regs->pr = VDSO_SYM(&__kernel_sigreturn); 373 #endif 374 } else { 375 /* Generate return code (system call to sigreturn) */ 376 err |= __put_user(MOVW(7), &frame->retcode[0]); 377 err |= __put_user(TRAP_NOARG, &frame->retcode[1]); 378 err |= __put_user(OR_R0_R0, &frame->retcode[2]); 379 err |= __put_user(OR_R0_R0, &frame->retcode[3]); 380 err |= __put_user(OR_R0_R0, &frame->retcode[4]); 381 err |= __put_user(OR_R0_R0, &frame->retcode[5]); 382 err |= __put_user(OR_R0_R0, &frame->retcode[6]); 383 err |= __put_user((__NR_sigreturn), &frame->retcode[7]); 384 regs->pr = (unsigned long) frame->retcode; 385 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode)); 386 } 387 388 if (err) 389 goto give_sigsegv; 390 391 /* Set up registers for signal handler */ 392 regs->regs[15] = (unsigned long) frame; 393 regs->regs[4] = signal; /* Arg for signal handler */ 394 regs->regs[5] = 0; 395 regs->regs[6] = (unsigned long) &frame->sc; 396 397 if (current->personality & FDPIC_FUNCPTRS) { 398 struct fdpic_func_descriptor __user *funcptr = 399 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler; 400 401 __get_user(regs->pc, &funcptr->text); 402 __get_user(regs->regs[12], &funcptr->GOT); 403 } else 404 regs->pc = (unsigned long)ka->sa.sa_handler; 405 406 set_fs(USER_DS); 407 408 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n", 409 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr); 410 411 return 0; 412 413 give_sigsegv: 414 force_sigsegv(sig, current); 415 return -EFAULT; 416 } 417 418 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 419 sigset_t *set, struct pt_regs *regs) 420 { 421 struct rt_sigframe __user *frame; 422 int err = 0; 423 int signal; 424 425 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame)); 426 427 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 428 goto give_sigsegv; 429 430 signal = current_thread_info()->exec_domain 431 && current_thread_info()->exec_domain->signal_invmap 432 && sig < 32 433 ? current_thread_info()->exec_domain->signal_invmap[sig] 434 : sig; 435 436 err |= copy_siginfo_to_user(&frame->info, info); 437 438 /* Create the ucontext. */ 439 err |= __put_user(0, &frame->uc.uc_flags); 440 err |= __put_user(NULL, &frame->uc.uc_link); 441 err |= __put_user((void *)current->sas_ss_sp, 442 &frame->uc.uc_stack.ss_sp); 443 err |= __put_user(sas_ss_flags(regs->regs[15]), 444 &frame->uc.uc_stack.ss_flags); 445 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); 446 err |= setup_sigcontext(&frame->uc.uc_mcontext, 447 regs, set->sig[0]); 448 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 449 450 /* Set up to return from userspace. If provided, use a stub 451 already in userspace. */ 452 if (ka->sa.sa_flags & SA_RESTORER) { 453 regs->pr = (unsigned long) ka->sa.sa_restorer; 454 #ifdef CONFIG_VSYSCALL 455 } else if (likely(current->mm->context.vdso)) { 456 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn); 457 #endif 458 } else { 459 /* Generate return code (system call to rt_sigreturn) */ 460 err |= __put_user(MOVW(7), &frame->retcode[0]); 461 err |= __put_user(TRAP_NOARG, &frame->retcode[1]); 462 err |= __put_user(OR_R0_R0, &frame->retcode[2]); 463 err |= __put_user(OR_R0_R0, &frame->retcode[3]); 464 err |= __put_user(OR_R0_R0, &frame->retcode[4]); 465 err |= __put_user(OR_R0_R0, &frame->retcode[5]); 466 err |= __put_user(OR_R0_R0, &frame->retcode[6]); 467 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]); 468 regs->pr = (unsigned long) frame->retcode; 469 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode)); 470 } 471 472 if (err) 473 goto give_sigsegv; 474 475 /* Set up registers for signal handler */ 476 regs->regs[15] = (unsigned long) frame; 477 regs->regs[4] = signal; /* Arg for signal handler */ 478 regs->regs[5] = (unsigned long) &frame->info; 479 regs->regs[6] = (unsigned long) &frame->uc; 480 481 if (current->personality & FDPIC_FUNCPTRS) { 482 struct fdpic_func_descriptor __user *funcptr = 483 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler; 484 485 __get_user(regs->pc, &funcptr->text); 486 __get_user(regs->regs[12], &funcptr->GOT); 487 } else 488 regs->pc = (unsigned long)ka->sa.sa_handler; 489 490 set_fs(USER_DS); 491 492 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n", 493 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr); 494 495 return 0; 496 497 give_sigsegv: 498 force_sigsegv(sig, current); 499 return -EFAULT; 500 } 501 502 static inline void 503 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs, 504 struct sigaction *sa) 505 { 506 /* If we're not from a syscall, bail out */ 507 if (regs->tra < 0) 508 return; 509 510 /* check for system call restart.. */ 511 switch (regs->regs[0]) { 512 case -ERESTART_RESTARTBLOCK: 513 case -ERESTARTNOHAND: 514 no_system_call_restart: 515 regs->regs[0] = -EINTR; 516 break; 517 518 case -ERESTARTSYS: 519 if (!(sa->sa_flags & SA_RESTART)) 520 goto no_system_call_restart; 521 /* fallthrough */ 522 case -ERESTARTNOINTR: 523 regs->regs[0] = save_r0; 524 regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); 525 break; 526 } 527 } 528 529 /* 530 * OK, we're invoking a handler 531 */ 532 static int 533 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info, 534 sigset_t *oldset, struct pt_regs *regs, unsigned int save_r0) 535 { 536 int ret; 537 538 /* Set up the stack frame */ 539 if (ka->sa.sa_flags & SA_SIGINFO) 540 ret = setup_rt_frame(sig, ka, info, oldset, regs); 541 else 542 ret = setup_frame(sig, ka, oldset, regs); 543 544 if (ret == 0) 545 block_sigmask(ka, sig); 546 547 return ret; 548 } 549 550 /* 551 * Note that 'init' is a special process: it doesn't get signals it doesn't 552 * want to handle. Thus you cannot kill init even with a SIGKILL even by 553 * mistake. 554 * 555 * Note that we go through the signals twice: once to check the signals that 556 * the kernel can handle, and then we build all the user-level signal handling 557 * stack-frames in one go after that. 558 */ 559 static void do_signal(struct pt_regs *regs, unsigned int save_r0) 560 { 561 siginfo_t info; 562 int signr; 563 struct k_sigaction ka; 564 sigset_t *oldset; 565 566 /* 567 * We want the common case to go fast, which 568 * is why we may in certain cases get here from 569 * kernel mode. Just return without doing anything 570 * if so. 571 */ 572 if (!user_mode(regs)) 573 return; 574 575 if (current_thread_info()->status & TS_RESTORE_SIGMASK) 576 oldset = ¤t->saved_sigmask; 577 else 578 oldset = ¤t->blocked; 579 580 signr = get_signal_to_deliver(&info, &ka, regs, NULL); 581 if (signr > 0) { 582 handle_syscall_restart(save_r0, regs, &ka.sa); 583 584 /* Whee! Actually deliver the signal. */ 585 if (handle_signal(signr, &ka, &info, oldset, 586 regs, save_r0) == 0) { 587 /* 588 * A signal was successfully delivered; the saved 589 * sigmask will have been stored in the signal frame, 590 * and will be restored by sigreturn, so we can simply 591 * clear the TS_RESTORE_SIGMASK flag 592 */ 593 current_thread_info()->status &= ~TS_RESTORE_SIGMASK; 594 595 tracehook_signal_handler(signr, &info, &ka, regs, 596 test_thread_flag(TIF_SINGLESTEP)); 597 } 598 599 return; 600 } 601 602 /* Did we come from a system call? */ 603 if (regs->tra >= 0) { 604 /* Restart the system call - no handlers present */ 605 if (regs->regs[0] == -ERESTARTNOHAND || 606 regs->regs[0] == -ERESTARTSYS || 607 regs->regs[0] == -ERESTARTNOINTR) { 608 regs->regs[0] = save_r0; 609 regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); 610 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) { 611 regs->pc -= instruction_size(__raw_readw(regs->pc - 4)); 612 regs->regs[3] = __NR_restart_syscall; 613 } 614 } 615 616 /* 617 * If there's no signal to deliver, we just put the saved sigmask 618 * back. 619 */ 620 if (current_thread_info()->status & TS_RESTORE_SIGMASK) { 621 current_thread_info()->status &= ~TS_RESTORE_SIGMASK; 622 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); 623 } 624 } 625 626 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0, 627 unsigned long thread_info_flags) 628 { 629 /* deal with pending signal delivery */ 630 if (thread_info_flags & _TIF_SIGPENDING) 631 do_signal(regs, save_r0); 632 633 if (thread_info_flags & _TIF_NOTIFY_RESUME) { 634 clear_thread_flag(TIF_NOTIFY_RESUME); 635 tracehook_notify_resume(regs); 636 if (current->replacement_session_keyring) 637 key_replace_session_keyring(); 638 } 639 } 640