signal.c 5.5 KB

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