signal_kern.c 5.1 KB

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