signal.c 4.8 KB

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