signal.c 5.6 KB

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