signal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ksignal *ksig, struct pt_regs *regs)
  19. {
  20. sigset_t *oldset = sigmask_to_save();
  21. int singlestep = 0;
  22. unsigned long sp;
  23. int err;
  24. if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
  25. singlestep = 1;
  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 (!(ksig->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 ((ksig->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 (!(ksig->ka.sa.sa_flags & SA_SIGINFO))
  51. err = setup_signal_stack_sc(sp, ksig, regs, oldset);
  52. else
  53. #endif
  54. err = setup_signal_stack_si(sp, ksig, regs, oldset);
  55. signal_setup_done(err, ksig, singlestep);
  56. }
  57. void do_signal(struct pt_regs *regs)
  58. {
  59. struct ksignal ksig;
  60. int handled_sig = 0;
  61. while (get_signal(&ksig)) {
  62. handled_sig = 1;
  63. /* Whee! Actually deliver the signal. */
  64. handle_signal(&ksig, regs);
  65. }
  66. /* Did we come from a system call? */
  67. if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
  68. /* Restart the system call - no handlers present */
  69. switch (PT_REGS_SYSCALL_RET(regs)) {
  70. case -ERESTARTNOHAND:
  71. case -ERESTARTSYS:
  72. case -ERESTARTNOINTR:
  73. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  74. PT_REGS_RESTART_SYSCALL(regs);
  75. break;
  76. case -ERESTART_RESTARTBLOCK:
  77. PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
  78. PT_REGS_RESTART_SYSCALL(regs);
  79. break;
  80. }
  81. }
  82. /*
  83. * This closes a way to execute a system call on the host. If
  84. * you set a breakpoint on a system call instruction and singlestep
  85. * from it, the tracing thread used to PTRACE_SINGLESTEP the process
  86. * rather than PTRACE_SYSCALL it, allowing the system call to execute
  87. * on the host. The tracing thread will check this flag and
  88. * PTRACE_SYSCALL if necessary.
  89. */
  90. if (current->ptrace & PT_DTRACE)
  91. current->thread.singlestep_syscall =
  92. is_syscall(PT_REGS_IP(&current->thread.regs));
  93. /*
  94. * if there's no signal to deliver, we just put the saved sigmask
  95. * back
  96. */
  97. if (!handled_sig)
  98. restore_saved_sigmask();
  99. }