signal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/stddef.h"
  6. #include "linux/sys.h"
  7. #include "linux/sched.h"
  8. #include "linux/wait.h"
  9. #include "linux/kernel.h"
  10. #include "linux/smp_lock.h"
  11. #include "linux/module.h"
  12. #include "linux/slab.h"
  13. #include "linux/tty.h"
  14. #include "linux/binfmts.h"
  15. #include "linux/ptrace.h"
  16. #include "asm/signal.h"
  17. #include "asm/uaccess.h"
  18. #include "asm/unistd.h"
  19. #include "user_util.h"
  20. #include "asm/ucontext.h"
  21. #include "kern_util.h"
  22. #include "signal_kern.h"
  23. #include "kern.h"
  24. #include "frame_kern.h"
  25. #include "sigcontext.h"
  26. #include "mode.h"
  27. EXPORT_SYMBOL(block_signals);
  28. EXPORT_SYMBOL(unblock_signals);
  29. #define _S(nr) (1<<((nr)-1))
  30. #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
  31. /*
  32. * OK, we're invoking a handler
  33. */
  34. static int handle_signal(struct pt_regs *regs, unsigned long signr,
  35. struct k_sigaction *ka, siginfo_t *info,
  36. sigset_t *oldset)
  37. {
  38. unsigned long sp;
  39. int err;
  40. /* Always make any pending restarted system calls return -EINTR */
  41. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  42. /* Did we come from a system call? */
  43. if(PT_REGS_SYSCALL_NR(regs) >= 0){
  44. /* If so, check system call restarting.. */
  45. switch(PT_REGS_SYSCALL_RET(regs)){
  46. case -ERESTART_RESTARTBLOCK:
  47. case -ERESTARTNOHAND:
  48. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  49. break;
  50. case -ERESTARTSYS:
  51. if (!(ka->sa.sa_flags & SA_RESTART)) {
  52. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  53. break;
  54. }
  55. /* fallthrough */
  56. case -ERESTARTNOINTR:
  57. PT_REGS_RESTART_SYSCALL(regs);
  58. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  59. break;
  60. }
  61. }
  62. sp = PT_REGS_SP(regs);
  63. if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
  64. sp = current->sas_ss_sp + current->sas_ss_size;
  65. #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
  66. if(!(ka->sa.sa_flags & SA_SIGINFO))
  67. err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
  68. else
  69. #endif
  70. err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
  71. if(err){
  72. spin_lock_irq(&current->sighand->siglock);
  73. current->blocked = *oldset;
  74. recalc_sigpending();
  75. spin_unlock_irq(&current->sighand->siglock);
  76. force_sigsegv(signr, current);
  77. } else {
  78. spin_lock_irq(&current->sighand->siglock);
  79. sigorsets(&current->blocked, &current->blocked,
  80. &ka->sa.sa_mask);
  81. if(!(ka->sa.sa_flags & SA_NODEFER))
  82. sigaddset(&current->blocked, signr);
  83. recalc_sigpending();
  84. spin_unlock_irq(&current->sighand->siglock);
  85. }
  86. return err;
  87. }
  88. static int kern_do_signal(struct pt_regs *regs)
  89. {
  90. struct k_sigaction ka_copy;
  91. siginfo_t info;
  92. sigset_t *oldset;
  93. int sig, handled_sig = 0;
  94. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  95. oldset = &current->saved_sigmask;
  96. else
  97. oldset = &current->blocked;
  98. while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){
  99. handled_sig = 1;
  100. /* Whee! Actually deliver the signal. */
  101. if(!handle_signal(regs, sig, &ka_copy, &info, oldset)){
  102. /* a signal was successfully delivered; the saved
  103. * sigmask will have been stored in the signal frame,
  104. * and will be restored by sigreturn, so we can simply
  105. * clear the TIF_RESTORE_SIGMASK flag */
  106. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  107. clear_thread_flag(TIF_RESTORE_SIGMASK);
  108. break;
  109. }
  110. }
  111. /* Did we come from a system call? */
  112. if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){
  113. /* Restart the system call - no handlers present */
  114. switch(PT_REGS_SYSCALL_RET(regs)){
  115. case -ERESTARTNOHAND:
  116. case -ERESTARTSYS:
  117. case -ERESTARTNOINTR:
  118. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  119. PT_REGS_RESTART_SYSCALL(regs);
  120. break;
  121. case -ERESTART_RESTARTBLOCK:
  122. PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
  123. PT_REGS_RESTART_SYSCALL(regs);
  124. break;
  125. }
  126. }
  127. /* This closes a way to execute a system call on the host. If
  128. * you set a breakpoint on a system call instruction and singlestep
  129. * from it, the tracing thread used to PTRACE_SINGLESTEP the process
  130. * rather than PTRACE_SYSCALL it, allowing the system call to execute
  131. * on the host. The tracing thread will check this flag and
  132. * PTRACE_SYSCALL if necessary.
  133. */
  134. if(current->ptrace & PT_DTRACE)
  135. current->thread.singlestep_syscall =
  136. is_syscall(PT_REGS_IP(&current->thread.regs));
  137. /* if there's no signal to deliver, we just put the saved sigmask
  138. * back */
  139. if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) {
  140. clear_thread_flag(TIF_RESTORE_SIGMASK);
  141. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  142. }
  143. return handled_sig;
  144. }
  145. int do_signal(void)
  146. {
  147. return kern_do_signal(&current->thread.regs);
  148. }
  149. /*
  150. * Atomically swap in the new signal mask, and wait for a signal.
  151. */
  152. long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  153. {
  154. mask &= _BLOCKABLE;
  155. spin_lock_irq(&current->sighand->siglock);
  156. current->saved_sigmask = current->blocked;
  157. siginitset(&current->blocked, mask);
  158. recalc_sigpending();
  159. spin_unlock_irq(&current->sighand->siglock);
  160. current->state = TASK_INTERRUPTIBLE;
  161. schedule();
  162. set_thread_flag(TIF_RESTORE_SIGMASK);
  163. return -ERESTARTNOHAND;
  164. }
  165. long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  166. {
  167. return do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs));
  168. }