signal.c 3.4 KB

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