1/* 2 * linux/arch/cris/kernel/signal.c 3 * 4 * Based on arch/i386/kernel/signal.c by 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson * 7 * 8 * Ideas also taken from arch/arm. 9 * 10 * Copyright (C) 2000, 2001, 2002 Axis Communications AB 11 * 12 * Authors: Bjorn Wesen (bjornw@axis.com) 13 * 14 */ 15 16#include <linux/sched.h> 17#include <linux/mm.h> 18#include <linux/smp.h> 19#include <linux/smp_lock.h> 20#include <linux/kernel.h> 21#include <linux/signal.h> 22#include <linux/errno.h> 23#include <linux/wait.h> 24#include <linux/ptrace.h> 25#include <linux/unistd.h> 26#include <linux/stddef.h> 27 28#include <asm/processor.h> 29#include <asm/ucontext.h> 30#include <asm/uaccess.h> 31 32#define DEBUG_SIG 0 33 34#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) 35 36/* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */ 37/* manipulate regs so that upon return, it will be re-executed */ 38 39/* We rely on that pc points to the instruction after "break 13", so the 40 * library must never do strange things like putting it in a delay slot. 41 */ 42#define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2; 43 44int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs); 45 46int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from) 47{ 48 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t))) 49 return -EFAULT; 50 if (from->si_code < 0) 51 return __copy_to_user(to, from, sizeof(siginfo_t)); 52 else { 53 int err; 54 55 /* If you change siginfo_t structure, please be sure 56 this code is fixed accordingly. 57 It should never copy any pad contained in the structure 58 to avoid security leaks, but must copy the generic 59 3 ints plus the relevant union member. */ 60 err = __put_user(from->si_signo, &to->si_signo); 61 err |= __put_user(from->si_errno, &to->si_errno); 62 err |= __put_user((short)from->si_code, &to->si_code); 63 /* First 32bits of unions are always present. */ 64 err |= __put_user(from->si_pid, &to->si_pid); 65 switch (from->si_code >> 16) { 66 case __SI_FAULT >> 16: 67 err |= __put_user(from->si_addr, &to->si_addr); 68 break; 69 case __SI_CHLD >> 16: 70 err |= __put_user(from->si_utime, &to->si_utime); 71 err |= __put_user(from->si_stime, &to->si_stime); 72 err |= __put_user(from->si_status, &to->si_status); 73 default: 74 err |= __put_user(from->si_uid, &to->si_uid); 75 break; 76 /* case __SI_RT: This is not generated by the kernel as of now. */ 77 } 78 return err; 79 } 80} 81 82/* 83 * Atomically swap in the new signal mask, and wait for a signal. Define 84 * dummy arguments to be able to reach the regs argument. (Note that this 85 * arrangement relies on old_sigset_t occupying one register.) 86 */ 87int 88sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof, 89 long srp, struct pt_regs *regs) 90{ 91 sigset_t saveset; 92 93 mask &= _BLOCKABLE; 94 spin_lock_irq(¤t->sigmask_lock); 95 saveset = current->blocked; 96 siginitset(¤t->blocked, mask); 97 recalc_sigpending(current); 98 spin_unlock_irq(¤t->sigmask_lock); 99 100 regs->r10 = -EINTR; 101 while (1) { 102 current->state = TASK_INTERRUPTIBLE; 103 schedule(); 104 if (do_signal(0, &saveset, regs)) 105 /* We will get here twice: once to call the signal 106 handler, then again to return from the 107 sigsuspend system call. When calling the 108 signal handler, R10 holds the signal number as 109 set through do_signal. The sigsuspend call 110 will return with the restored value set above; 111 always -EINTR. */ 112 return regs->r10; 113 } 114} 115 116/* Define dummy arguments to be able to reach the regs argument. (Note that 117 * this arrangement relies on size_t occupying one register.) 118 */ 119int 120sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, long r12, long r13, 121 long mof, long srp, struct pt_regs *regs) 122{ 123 sigset_t saveset, newset; 124 125 if (sigsetsize != sizeof(sigset_t)) 126 return -EINVAL; 127 128 if (copy_from_user(&newset, unewset, sizeof(newset))) 129 return -EFAULT; 130 sigdelsetmask(&newset, ~_BLOCKABLE); 131 132 spin_lock_irq(¤t->sigmask_lock); 133 saveset = current->blocked; 134 current->blocked = newset; 135 recalc_sigpending(current); 136 spin_unlock_irq(¤t->sigmask_lock); 137 138 regs->r10 = -EINTR; 139 while (1) { 140 current->state = TASK_INTERRUPTIBLE; 141 schedule(); 142 if (do_signal(0, &saveset, regs)) 143 /* We will get here twice: once to call the signal 144 handler, then again to return from the 145 sigsuspend system call. When calling the 146 signal handler, R10 holds the signal number as 147 set through do_signal. The sigsuspend call 148 will return with the restored value set above; 149 always -EINTR. */ 150 return regs->r10; 151 } 152} 153 154int 155sys_sigaction(int sig, const struct old_sigaction *act, 156 struct old_sigaction *oact) 157{ 158 struct k_sigaction new_ka, old_ka; 159 int ret; 160 161 if (act) { 162 old_sigset_t mask; 163 if (verify_area(VERIFY_READ, act, sizeof(*act)) || 164 __get_user(new_ka.sa.sa_handler, &act->sa_handler) || 165 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer)) 166 return -EFAULT; 167 __get_user(new_ka.sa.sa_flags, &act->sa_flags); 168 __get_user(mask, &act->sa_mask); 169 siginitset(&new_ka.sa.sa_mask, mask); 170 } 171 172 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); 173 174 if (!ret && oact) { 175 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) || 176 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || 177 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer)) 178 return -EFAULT; 179 __put_user(old_ka.sa.sa_flags, &oact->sa_flags); 180 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask); 181 } 182 183 return ret; 184} 185 186int 187sys_sigaltstack(const stack_t *uss, stack_t *uoss) 188{ 189 return do_sigaltstack(uss, uoss, rdusp()); 190} 191 192 193/* 194 * Do a signal return; undo the signal stack. 195 */ 196 197struct sigframe { 198 struct sigcontext sc; 199 unsigned long extramask[_NSIG_WORDS-1]; 200 unsigned char retcode[8]; /* trampoline code */ 201}; 202 203struct rt_sigframe { 204 struct siginfo *pinfo; 205 void *puc; 206 struct siginfo info; 207 struct ucontext uc; 208 unsigned char retcode[8]; /* trampoline code */ 209}; 210 211 212static int 213restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc) 214{ 215 unsigned int err = 0; 216 unsigned long old_usp; 217 218 /* restore the regs from &sc->regs (same as sc, since regs is first) 219 * (sc is already checked for VERIFY_READ since the sigframe was 220 * checked in sys_sigreturn previously) 221 */ 222 223 if (__copy_from_user(regs, sc, sizeof(struct pt_regs))) 224 goto badframe; 225 226 /* make sure the U-flag is set so user-mode cannot fool us */ 227 228 regs->dccr |= 1 << 8; 229 230 /* restore the old USP as it was before we stacked the sc etc. 231 * (we cannot just pop the sigcontext since we aligned the sp and 232 * stuff after pushing it) 233 */ 234 235 err |= __get_user(old_usp, &sc->usp); 236 237 wrusp(old_usp); 238 239 /* TODO: the other ports use regs->orig_XX to disable syscall checks 240 * after this completes, but we don't use that mechanism. maybe we can 241 * use it now ? 242 */ 243 244 return err; 245 246badframe: 247 return 1; 248} 249 250/* Define dummy arguments to be able to reach the regs argument. */ 251 252asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof, 253 long srp, struct pt_regs *regs) 254{ 255 struct sigframe *frame = (struct sigframe *)rdusp(); 256 sigset_t set; 257 258 /* 259 * Since we stacked the signal on a dword boundary, 260 * then frame should be dword aligned here. If it's 261 * not, then the user is trying to mess with us. 262 */ 263 if (((long)frame) & 3) 264 goto badframe; 265 266 if (verify_area(VERIFY_READ, frame, sizeof(*frame))) 267 goto badframe; 268 if (__get_user(set.sig[0], &frame->sc.oldmask) 269 || (_NSIG_WORDS > 1 270 && __copy_from_user(&set.sig[1], &frame->extramask, 271 sizeof(frame->extramask)))) 272 goto badframe; 273 274 sigdelsetmask(&set, ~_BLOCKABLE); 275 spin_lock_irq(¤t->sigmask_lock); 276 current->blocked = set; 277 recalc_sigpending(current); 278 spin_unlock_irq(¤t->sigmask_lock); 279 280 if (restore_sigcontext(regs, &frame->sc)) 281 goto badframe; 282 283 /* TODO: SIGTRAP when single-stepping as in arm ? */ 284 285 return regs->r10; 286 287badframe: 288 force_sig(SIGSEGV, current); 289 return 0; 290} 291 292/* Define dummy arguments to be able to reach the regs argument. */ 293 294asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13, 295 long mof, long srp, struct pt_regs *regs) 296{ 297 struct rt_sigframe *frame = (struct rt_sigframe *)rdusp(); 298 sigset_t set; 299 stack_t st; 300 301 /* 302 * Since we stacked the signal on a dword boundary, 303 * then frame should be dword aligned here. If it's 304 * not, then the user is trying to mess with us. 305 */ 306 if (((long)frame) & 3) 307 goto badframe; 308 309 if (verify_area(VERIFY_READ, frame, sizeof(*frame))) 310 goto badframe; 311 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) 312 goto badframe; 313 314 sigdelsetmask(&set, ~_BLOCKABLE); 315 spin_lock_irq(¤t->sigmask_lock); 316 current->blocked = set; 317 recalc_sigpending(current); 318 spin_unlock_irq(¤t->sigmask_lock); 319 320 if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) 321 goto badframe; 322 323 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st))) 324 goto badframe; 325 /* It is more difficult to avoid calling this function than to 326 call it and ignore errors. */ 327 do_sigaltstack(&st, NULL, rdusp()); 328 329 return regs->r10; 330 331badframe: 332 force_sig(SIGSEGV, current); 333 return 0; 334} 335 336/* 337 * Set up a signal frame. 338 */ 339 340static int 341setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, unsigned long mask) 342{ 343 int err = 0; 344 unsigned long usp = rdusp(); 345 346 /* copy the regs. they are first in sc so we can use sc directly */ 347 348 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs)); 349 350 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of 351 the signal handler. The frametype will be restored to its previous 352 value in restore_sigcontext. */ 353 regs->frametype = CRIS_FRAME_NORMAL; 354 355 /* then some other stuff */ 356 357 err |= __put_user(mask, &sc->oldmask); 358 359 err |= __put_user(usp, &sc->usp); 360 361 return err; 362} 363 364/* figure out where we want to put the new signal frame - usually on the stack */ 365 366static inline void * 367get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) 368{ 369 unsigned long sp = rdusp(); 370 371 /* This is the X/Open sanctioned signal stack switching. */ 372 if (ka->sa.sa_flags & SA_ONSTACK) { 373 if (! on_sig_stack(sp)) 374 sp = current->sas_ss_sp + current->sas_ss_size; 375 } 376 377 /* make sure the frame is dword-aligned */ 378 379 sp &= ~3; 380 381 return (void *)(sp - frame_size); 382} 383 384/* grab and setup a signal frame. 385 * 386 * basically we stack a lot of state info, and arrange for the 387 * user-mode program to return to the kernel using either a 388 * trampoline which performs the syscall sigreturn, or a provided 389 * user-mode trampoline. 390 */ 391 392static void setup_frame(int sig, struct k_sigaction *ka, 393 sigset_t *set, struct pt_regs * regs) 394{ 395 struct sigframe *frame; 396 unsigned long return_ip; 397 int err = 0; 398 399 frame = get_sigframe(ka, regs, sizeof(*frame)); 400 401 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 402 goto give_sigsegv; 403 404 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]); 405 if (err) 406 goto give_sigsegv; 407 408 if (_NSIG_WORDS > 1) { 409 err |= __copy_to_user(frame->extramask, &set->sig[1], 410 sizeof(frame->extramask)); 411 } 412 if (err) 413 goto give_sigsegv; 414 415 /* Set up to return from userspace. If provided, use a stub 416 already in userspace. */ 417 if (ka->sa.sa_flags & SA_RESTORER) { 418 return_ip = (unsigned long)ka->sa.sa_restorer; 419 } else { 420 /* trampoline - the desired return ip is the retcode itself */ 421 return_ip = (unsigned long)&frame->retcode; 422 /* This is movu.w __NR_sigreturn, r9; break 13; */ 423 err |= __put_user(0x9c5f, (short *)(frame->retcode+0)); 424 err |= __put_user(__NR_sigreturn, (short *)(frame->retcode+2)); 425 err |= __put_user(0xe93d, (short *)(frame->retcode+4)); 426 } 427 428 if (err) 429 goto give_sigsegv; 430 431 /* Set up registers for signal handler */ 432 433 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */ 434 regs->srp = return_ip; /* what we enter LATER */ 435 regs->r10 = sig; /* first argument is signo */ 436 437 /* actually move the usp to reflect the stacked frame */ 438 439 wrusp((unsigned long)frame); 440 441 return; 442 443give_sigsegv: 444 if (sig == SIGSEGV) 445 ka->sa.sa_handler = SIG_DFL; 446 force_sig(SIGSEGV, current); 447} 448 449static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, 450 sigset_t *set, struct pt_regs * regs) 451{ 452 struct rt_sigframe *frame; 453 unsigned long return_ip; 454 int err = 0; 455 456 frame = get_sigframe(ka, regs, sizeof(*frame)); 457 458 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 459 goto give_sigsegv; 460 461 err |= __put_user(&frame->info, &frame->pinfo); 462 err |= __put_user(&frame->uc, &frame->puc); 463 err |= copy_siginfo_to_user(&frame->info, info); 464 if (err) 465 goto give_sigsegv; 466 467 /* Clear all the bits of the ucontext we don't use. */ 468 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext)); 469 470 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]); 471 472 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); 473 474 if (err) 475 goto give_sigsegv; 476 477 /* Set up to return from userspace. If provided, use a stub 478 already in userspace. */ 479 if (ka->sa.sa_flags & SA_RESTORER) { 480 return_ip = (unsigned long)ka->sa.sa_restorer; 481 } else { 482 /* trampoline - the desired return ip is the retcode itself */ 483 return_ip = (unsigned long)&frame->retcode; 484 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */ 485 err |= __put_user(0x9c5f, (short *)(frame->retcode+0)); 486 err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode+2)); 487 err |= __put_user(0xe93d, (short *)(frame->retcode+4)); 488 } 489 490 if (err) 491 goto give_sigsegv; 492 493 /* TODO what is the current->exec_domain stuff and invmap ? */ 494 495 /* Set up registers for signal handler */ 496 497 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */ 498 regs->srp = return_ip; /* what we enter LATER */ 499 regs->r10 = sig; /* first argument is signo */ 500 regs->r11 = (unsigned long) &frame->info; /* second argument is (siginfo_t *) */ 501 regs->r12 = 0; /* third argument is unused */ 502 503 /* actually move the usp to reflect the stacked frame */ 504 505 wrusp((unsigned long)frame); 506 507 return; 508 509give_sigsegv: 510 if (sig == SIGSEGV) 511 ka->sa.sa_handler = SIG_DFL; 512 force_sig(SIGSEGV, current); 513} 514 515/* 516 * OK, we're invoking a handler 517 */ 518 519static inline void 520handle_signal(int canrestart, unsigned long sig, struct k_sigaction *ka, 521 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs) 522{ 523 /* Are we from a system call? */ 524 if (canrestart) { 525 /* If so, check system call restarting.. */ 526 switch (regs->r10) { 527 case -ERESTARTNOHAND: 528 /* ERESTARTNOHAND means that the syscall should only be 529 restarted if there was no handler for the signal, and since 530 we only get here if there is a handler, we dont restart */ 531 regs->r10 = -EINTR; 532 break; 533 534 case -ERESTARTSYS: 535 /* ERESTARTSYS means to restart the syscall if there is no 536 handler or the handler was registered with SA_RESTART */ 537 if (!(ka->sa.sa_flags & SA_RESTART)) { 538 regs->r10 = -EINTR; 539 break; 540 } 541 /* fallthrough */ 542 case -ERESTARTNOINTR: 543 /* ERESTARTNOINTR means that the syscall should be called again 544 after the signal handler returns. */ 545 RESTART_CRIS_SYS(regs); 546 } 547 } 548 549 /* Set up the stack frame */ 550 if (ka->sa.sa_flags & SA_SIGINFO) 551 setup_rt_frame(sig, ka, info, oldset, regs); 552 else 553 setup_frame(sig, ka, oldset, regs); 554 555 if (ka->sa.sa_flags & SA_ONESHOT) 556 ka->sa.sa_handler = SIG_DFL; 557 558 if (!(ka->sa.sa_flags & SA_NODEFER)) { 559 spin_lock_irq(¤t->sigmask_lock); 560 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask); 561 sigaddset(¤t->blocked,sig); 562 recalc_sigpending(current); 563 spin_unlock_irq(¤t->sigmask_lock); 564 } 565} 566 567/* 568 * Note that 'init' is a special process: it doesn't get signals it doesn't 569 * want to handle. Thus you cannot kill init even with a SIGKILL even by 570 * mistake. 571 * 572 * Also note that the regs structure given here as an argument, is the latest 573 * pushed pt_regs. It may or may not be the same as the first pushed registers 574 * when the initial usermode->kernelmode transition took place. Therefore 575 * we can use user_mode(regs) to see if we came directly from kernel or user 576 * mode below. 577 */ 578 579int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs) 580{ 581 siginfo_t info; 582 struct k_sigaction *ka; 583 584 /* 585 * We want the common case to go fast, which 586 * is why we may in certain cases get here from 587 * kernel mode. Just return without doing anything 588 * if so. 589 */ 590 if (!user_mode(regs)) 591 return 1; 592 593 if (!oldset) 594 oldset = ¤t->blocked; 595 596 for (;;) { 597 unsigned long signr; 598 599 spin_lock_irq(¤t->sigmask_lock); 600 signr = dequeue_signal(¤t->blocked, &info); 601 spin_unlock_irq(¤t->sigmask_lock); 602 603 if (!signr) 604 break; 605 606 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) { 607 /* Let the debugger run. */ 608 current->exit_code = signr; 609 current->state = TASK_STOPPED; 610 notify_parent(current, SIGCHLD); 611 schedule(); 612 613 /* We're back. Did the debugger cancel the sig? */ 614 if (!(signr = current->exit_code)) 615 continue; 616 current->exit_code = 0; 617 618 /* The debugger continued. Ignore SIGSTOP. */ 619 if (signr == SIGSTOP) 620 continue; 621 622 /* Update the siginfo structure. Is this good? */ 623 if (signr != info.si_signo) { 624 info.si_signo = signr; 625 info.si_errno = 0; 626 info.si_code = SI_USER; 627 info.si_pid = current->p_pptr->pid; 628 info.si_uid = current->p_pptr->uid; 629 } 630 631 /* If the (new) signal is now blocked, requeue it. */ 632 if (sigismember(¤t->blocked, signr)) { 633 send_sig_info(signr, &info, current); 634 continue; 635 } 636 } 637 638 ka = ¤t->sig->action[signr-1]; 639 if (ka->sa.sa_handler == SIG_IGN) { 640 if (signr != SIGCHLD) 641 continue; 642 /* Check for SIGCHLD: it's special. */ 643 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0) 644 /* nothing */; 645 continue; 646 } 647 648 if (ka->sa.sa_handler == SIG_DFL) { 649 int exit_code = signr; 650 651 /* Init gets no signals it doesn't want. */ 652 if (current->pid == 1) 653 continue; 654 655 switch (signr) { 656 case SIGCONT: case SIGCHLD: case SIGWINCH: 657 continue; 658 659 case SIGTSTP: case SIGTTIN: case SIGTTOU: 660 if (is_orphaned_pgrp(current->pgrp)) 661 continue; 662 /* FALLTHRU */ 663 664 case SIGSTOP: 665 current->state = TASK_STOPPED; 666 current->exit_code = signr; 667 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP)) 668 notify_parent(current, SIGCHLD); 669 schedule(); 670 continue; 671 672 case SIGQUIT: case SIGILL: case SIGTRAP: 673 case SIGABRT: case SIGFPE: case SIGSEGV: 674 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ: 675 if (do_coredump(signr, regs)) 676 exit_code |= 0x80; 677 /* FALLTHRU */ 678 679 default: 680 lock_kernel(); 681 sig_exit(signr, exit_code, &info); 682 /* NOTREACHED */ 683 } 684 } 685 686 /* Whee! Actually deliver the signal. */ 687 handle_signal(canrestart, signr, ka, &info, oldset, regs); 688 return 1; 689 } 690 691 /* Did we come from a system call? */ 692 if (canrestart) { 693 /* Restart the system call - no handlers present */ 694 if (regs->r10 == -ERESTARTNOHAND || 695 regs->r10 == -ERESTARTSYS || 696 regs->r10 == -ERESTARTNOINTR) { 697 RESTART_CRIS_SYS(regs); 698 } 699 } 700 return 0; 701} 702