signal.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /* Did we come from a system call? */
  27. if (PT_REGS_SYSCALL_NR(regs) >= 0) {
  28. /* If so, check system call restarting.. */
  29. switch (PT_REGS_SYSCALL_RET(regs)) {
  30. case -ERESTART_RESTARTBLOCK:
  31. case -ERESTARTNOHAND:
  32. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  33. break;
  34. case -ERESTARTSYS:
  35. if (!(ka->sa.sa_flags & SA_RESTART)) {
  36. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  37. break;
  38. }
  39. /* fallthrough */
  40. case -ERESTARTNOINTR:
  41. PT_REGS_RESTART_SYSCALL(regs);
  42. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  43. break;
  44. }
  45. }
  46. sp = PT_REGS_SP(regs);
  47. if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
  48. sp = current->sas_ss_sp + current->sas_ss_size;
  49. #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
  50. if (!(ka->sa.sa_flags & SA_SIGINFO))
  51. err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
  52. else
  53. #endif
  54. err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
  55. if (err)
  56. force_sigsegv(signr, current);
  57. else
  58. block_sigmask(ka, signr);
  59. return err;
  60. }
  61. static int kern_do_signal(struct pt_regs *regs)
  62. {
  63. struct k_sigaction ka_copy;
  64. siginfo_t info;
  65. int sig, handled_sig = 0;
  66. while ((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0) {
  67. sigset_t *oldset;
  68. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  69. oldset = &current->saved_sigmask;
  70. else
  71. oldset = &current->blocked;
  72. handled_sig = 1;
  73. /* Whee! Actually deliver the signal. */
  74. if (!handle_signal(regs, sig, &ka_copy, &info, oldset)) {
  75. /*
  76. * a signal was successfully delivered; the saved
  77. * sigmask will have been stored in the signal frame,
  78. * and will be restored by sigreturn, so we can simply
  79. * clear the TIF_RESTORE_SIGMASK flag
  80. */
  81. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  82. clear_thread_flag(TIF_RESTORE_SIGMASK);
  83. break;
  84. }
  85. }
  86. /* Did we come from a system call? */
  87. if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
  88. /* Restart the system call - no handlers present */
  89. switch (PT_REGS_SYSCALL_RET(regs)) {
  90. case -ERESTARTNOHAND:
  91. case -ERESTARTSYS:
  92. case -ERESTARTNOINTR:
  93. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  94. PT_REGS_RESTART_SYSCALL(regs);
  95. break;
  96. case -ERESTART_RESTARTBLOCK:
  97. PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
  98. PT_REGS_RESTART_SYSCALL(regs);
  99. break;
  100. }
  101. }
  102. /*
  103. * This closes a way to execute a system call on the host. If
  104. * you set a breakpoint on a system call instruction and singlestep
  105. * from it, the tracing thread used to PTRACE_SINGLESTEP the process
  106. * rather than PTRACE_SYSCALL it, allowing the system call to execute
  107. * on the host. The tracing thread will check this flag and
  108. * PTRACE_SYSCALL if necessary.
  109. */
  110. if (current->ptrace & PT_DTRACE)
  111. current->thread.singlestep_syscall =
  112. is_syscall(PT_REGS_IP(&current->thread.regs));
  113. /*
  114. * if there's no signal to deliver, we just put the saved sigmask
  115. * back
  116. */
  117. if (!handled_sig)
  118. restore_saved_sigmask();
  119. return handled_sig;
  120. }
  121. int do_signal(void)
  122. {
  123. return kern_do_signal(&current->thread.regs);
  124. }
  125. /*
  126. * Atomically swap in the new signal mask, and wait for a signal.
  127. */
  128. long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  129. {
  130. sigset_t blocked;
  131. siginitset(&blocked, mask);
  132. return sigsuspend(&blocked);
  133. }
  134. long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  135. {
  136. return do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs));
  137. }