signal.c 4.8 KB

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