signal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/ptrace.h>
  7. #include <linux/sched.h>
  8. #include <linux/ftrace.h>
  9. #include <asm/siginfo.h>
  10. #include <asm/signal.h>
  11. #include <asm/unistd.h>
  12. #include <frame_kern.h>
  13. #include <kern_util.h>
  14. #include <os.h>
  15. EXPORT_SYMBOL(block_signals);
  16. EXPORT_SYMBOL(unblock_signals);
  17. void block_signals_trace(void)
  18. {
  19. block_signals();
  20. if (current_thread_info())
  21. trace_hardirqs_off();
  22. }
  23. void unblock_signals_trace(void)
  24. {
  25. if (current_thread_info())
  26. trace_hardirqs_on();
  27. unblock_signals();
  28. }
  29. void um_trace_signals_on(void)
  30. {
  31. if (current_thread_info())
  32. trace_hardirqs_on();
  33. }
  34. void um_trace_signals_off(void)
  35. {
  36. if (current_thread_info())
  37. trace_hardirqs_off();
  38. }
  39. /*
  40. * OK, we're invoking a handler
  41. */
  42. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  43. {
  44. sigset_t *oldset = sigmask_to_save();
  45. int singlestep = 0;
  46. unsigned long sp;
  47. int err;
  48. if (test_thread_flag(TIF_SINGLESTEP) && (current->ptrace & PT_PTRACED))
  49. singlestep = 1;
  50. /* Did we come from a system call? */
  51. if (PT_REGS_SYSCALL_NR(regs) >= 0) {
  52. /* If so, check system call restarting.. */
  53. switch (PT_REGS_SYSCALL_RET(regs)) {
  54. case -ERESTART_RESTARTBLOCK:
  55. case -ERESTARTNOHAND:
  56. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  57. break;
  58. case -ERESTARTSYS:
  59. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  60. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  61. break;
  62. }
  63. fallthrough;
  64. case -ERESTARTNOINTR:
  65. PT_REGS_RESTART_SYSCALL(regs);
  66. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  67. break;
  68. }
  69. }
  70. sp = PT_REGS_SP(regs);
  71. if ((ksig->ka.sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
  72. sp = current->sas_ss_sp + current->sas_ss_size;
  73. #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
  74. if (!(ksig->ka.sa.sa_flags & SA_SIGINFO))
  75. err = setup_signal_stack_sc(sp, ksig, regs, oldset);
  76. else
  77. #endif
  78. err = setup_signal_stack_si(sp, ksig, regs, oldset);
  79. signal_setup_done(err, ksig, singlestep);
  80. }
  81. void do_signal(struct pt_regs *regs)
  82. {
  83. struct ksignal ksig;
  84. int handled_sig = 0;
  85. while (get_signal(&ksig)) {
  86. handled_sig = 1;
  87. /* Whee! Actually deliver the signal. */
  88. handle_signal(&ksig, regs);
  89. }
  90. /* Did we come from a system call? */
  91. if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
  92. /* Restart the system call - no handlers present */
  93. switch (PT_REGS_SYSCALL_RET(regs)) {
  94. case -ERESTARTNOHAND:
  95. case -ERESTARTSYS:
  96. case -ERESTARTNOINTR:
  97. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  98. PT_REGS_RESTART_SYSCALL(regs);
  99. break;
  100. case -ERESTART_RESTARTBLOCK:
  101. PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
  102. PT_REGS_RESTART_SYSCALL(regs);
  103. break;
  104. }
  105. }
  106. /*
  107. * This closes a way to execute a system call on the host. If
  108. * you set a breakpoint on a system call instruction and singlestep
  109. * from it, the tracing thread used to PTRACE_SINGLESTEP the process
  110. * rather than PTRACE_SYSCALL it, allowing the system call to execute
  111. * on the host. The tracing thread will check this flag and
  112. * PTRACE_SYSCALL if necessary.
  113. */
  114. if (test_thread_flag(TIF_SINGLESTEP))
  115. current->thread.singlestep_syscall =
  116. is_syscall(PT_REGS_IP(&current->thread.regs));
  117. /*
  118. * if there's no signal to deliver, we just put the saved sigmask
  119. * back
  120. */
  121. if (!handled_sig)
  122. restore_saved_sigmask();
  123. }