signal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Common signal handling code for both 32 and 64 bits
  3. *
  4. * Copyright (c) 2007 Benjamin Herrenschmidt, IBM Corporation
  5. * Extracted from signal_32.c and signal_64.c
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file README.legal in the main directory of
  9. * this archive for more details.
  10. */
  11. #include <linux/tracehook.h>
  12. #include <linux/signal.h>
  13. #include <linux/uprobes.h>
  14. #include <linux/key.h>
  15. #include <linux/context_tracking.h>
  16. #include <linux/livepatch.h>
  17. #include <linux/syscalls.h>
  18. #include <asm/hw_breakpoint.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/unistd.h>
  21. #include <asm/debug.h>
  22. #include <asm/tm.h>
  23. #include "signal.h"
  24. /* Log an error when sending an unhandled signal to a process. Controlled
  25. * through debug.exception-trace sysctl.
  26. */
  27. int show_unhandled_signals = 1;
  28. /*
  29. * Allocate space for the signal frame
  30. */
  31. void __user *get_sigframe(struct ksignal *ksig, unsigned long sp,
  32. size_t frame_size, int is_32)
  33. {
  34. unsigned long oldsp, newsp;
  35. /* Default to using normal stack */
  36. oldsp = get_clean_sp(sp, is_32);
  37. oldsp = sigsp(oldsp, ksig);
  38. newsp = (oldsp - frame_size) & ~0xFUL;
  39. /* Check access */
  40. if (!access_ok(VERIFY_WRITE, (void __user *)newsp, oldsp - newsp))
  41. return NULL;
  42. return (void __user *)newsp;
  43. }
  44. static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
  45. int has_handler)
  46. {
  47. unsigned long ret = regs->gpr[3];
  48. int restart = 1;
  49. /* syscall ? */
  50. if (TRAP(regs) != 0x0C00)
  51. return;
  52. /* error signalled ? */
  53. if (!(regs->ccr & 0x10000000))
  54. return;
  55. switch (ret) {
  56. case ERESTART_RESTARTBLOCK:
  57. case ERESTARTNOHAND:
  58. /* ERESTARTNOHAND means that the syscall should only be
  59. * restarted if there was no handler for the signal, and since
  60. * we only get here if there is a handler, we dont restart.
  61. */
  62. restart = !has_handler;
  63. break;
  64. case ERESTARTSYS:
  65. /* ERESTARTSYS means to restart the syscall if there is no
  66. * handler or the handler was registered with SA_RESTART
  67. */
  68. restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
  69. break;
  70. case ERESTARTNOINTR:
  71. /* ERESTARTNOINTR means that the syscall should be
  72. * called again after the signal handler returns.
  73. */
  74. break;
  75. default:
  76. return;
  77. }
  78. if (restart) {
  79. if (ret == ERESTART_RESTARTBLOCK)
  80. regs->gpr[0] = __NR_restart_syscall;
  81. else
  82. regs->gpr[3] = regs->orig_gpr3;
  83. regs->nip -= 4;
  84. regs->result = 0;
  85. } else {
  86. regs->result = -EINTR;
  87. regs->gpr[3] = EINTR;
  88. regs->ccr |= 0x10000000;
  89. }
  90. }
  91. static void do_signal(struct task_struct *tsk)
  92. {
  93. sigset_t *oldset = sigmask_to_save();
  94. struct ksignal ksig = { .sig = 0 };
  95. int ret;
  96. int is32 = is_32bit_task();
  97. BUG_ON(tsk != current);
  98. get_signal(&ksig);
  99. /* Is there any syscall restart business here ? */
  100. check_syscall_restart(tsk->thread.regs, &ksig.ka, ksig.sig > 0);
  101. if (ksig.sig <= 0) {
  102. /* No signal to deliver -- put the saved sigmask back */
  103. restore_saved_sigmask();
  104. tsk->thread.regs->trap = 0;
  105. return; /* no signals delivered */
  106. }
  107. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  108. /*
  109. * Reenable the DABR before delivering the signal to
  110. * user space. The DABR will have been cleared if it
  111. * triggered inside the kernel.
  112. */
  113. if (tsk->thread.hw_brk.address && tsk->thread.hw_brk.type)
  114. __set_breakpoint(&tsk->thread.hw_brk);
  115. #endif
  116. /* Re-enable the breakpoints for the signal stack */
  117. thread_change_pc(tsk, tsk->thread.regs);
  118. rseq_signal_deliver(&ksig, tsk->thread.regs);
  119. if (is32) {
  120. if (ksig.ka.sa.sa_flags & SA_SIGINFO)
  121. ret = handle_rt_signal32(&ksig, oldset, tsk);
  122. else
  123. ret = handle_signal32(&ksig, oldset, tsk);
  124. } else {
  125. ret = handle_rt_signal64(&ksig, oldset, tsk);
  126. }
  127. tsk->thread.regs->trap = 0;
  128. signal_setup_done(ret, &ksig, test_thread_flag(TIF_SINGLESTEP));
  129. }
  130. void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  131. {
  132. user_exit();
  133. /* Check valid addr_limit, TIF check is done there */
  134. addr_limit_user_check();
  135. if (thread_info_flags & _TIF_UPROBE)
  136. uprobe_notify_resume(regs);
  137. if (thread_info_flags & _TIF_PATCH_PENDING)
  138. klp_update_patch_state(current);
  139. if (thread_info_flags & _TIF_SIGPENDING) {
  140. BUG_ON(regs != current->thread.regs);
  141. do_signal(current);
  142. }
  143. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  144. clear_thread_flag(TIF_NOTIFY_RESUME);
  145. tracehook_notify_resume(regs);
  146. rseq_handle_notify_resume(NULL, regs);
  147. }
  148. user_enter();
  149. }
  150. unsigned long get_tm_stackpointer(struct task_struct *tsk)
  151. {
  152. /* When in an active transaction that takes a signal, we need to be
  153. * careful with the stack. It's possible that the stack has moved back
  154. * up after the tbegin. The obvious case here is when the tbegin is
  155. * called inside a function that returns before a tend. In this case,
  156. * the stack is part of the checkpointed transactional memory state.
  157. * If we write over this non transactionally or in suspend, we are in
  158. * trouble because if we get a tm abort, the program counter and stack
  159. * pointer will be back at the tbegin but our in memory stack won't be
  160. * valid anymore.
  161. *
  162. * To avoid this, when taking a signal in an active transaction, we
  163. * need to use the stack pointer from the checkpointed state, rather
  164. * than the speculated state. This ensures that the signal context
  165. * (written tm suspended) will be written below the stack required for
  166. * the rollback. The transaction is aborted because of the treclaim,
  167. * so any memory written between the tbegin and the signal will be
  168. * rolled back anyway.
  169. *
  170. * For signals taken in non-TM or suspended mode, we use the
  171. * normal/non-checkpointed stack pointer.
  172. */
  173. unsigned long ret = tsk->thread.regs->gpr[1];
  174. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  175. BUG_ON(tsk != current);
  176. if (MSR_TM_ACTIVE(tsk->thread.regs->msr)) {
  177. preempt_disable();
  178. tm_reclaim_current(TM_CAUSE_SIGNAL);
  179. if (MSR_TM_TRANSACTIONAL(tsk->thread.regs->msr))
  180. ret = tsk->thread.ckpt_regs.gpr[1];
  181. /*
  182. * If we treclaim, we must clear the current thread's TM bits
  183. * before re-enabling preemption. Otherwise we might be
  184. * preempted and have the live MSR[TS] changed behind our back
  185. * (tm_recheckpoint_new_task() would recheckpoint). Besides, we
  186. * enter the signal handler in non-transactional state.
  187. */
  188. tsk->thread.regs->msr &= ~MSR_TS_MASK;
  189. preempt_enable();
  190. }
  191. #endif
  192. return ret;
  193. }