signal.c 5.7 KB

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