signal.c 3.5 KB

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