signal.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/key.h>
  14. #include <asm/hw_breakpoint.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/unistd.h>
  17. #include "signal.h"
  18. /* Log an error when sending an unhandled signal to a process. Controlled
  19. * through debug.exception-trace sysctl.
  20. */
  21. int show_unhandled_signals = 0;
  22. /*
  23. * Allocate space for the signal frame
  24. */
  25. void __user * get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  26. size_t frame_size, int is_32)
  27. {
  28. unsigned long oldsp, newsp;
  29. /* Default to using normal stack */
  30. oldsp = get_clean_sp(regs, is_32);
  31. /* Check for alt stack */
  32. if ((ka->sa.sa_flags & SA_ONSTACK) &&
  33. current->sas_ss_size && !on_sig_stack(oldsp))
  34. oldsp = (current->sas_ss_sp + current->sas_ss_size);
  35. /* Get aligned frame */
  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. /*
  43. * Restore the user process's signal mask
  44. */
  45. void restore_sigmask(sigset_t *set)
  46. {
  47. sigdelsetmask(set, ~_BLOCKABLE);
  48. spin_lock_irq(&current->sighand->siglock);
  49. current->blocked = *set;
  50. recalc_sigpending();
  51. spin_unlock_irq(&current->sighand->siglock);
  52. }
  53. static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
  54. int has_handler)
  55. {
  56. unsigned long ret = regs->gpr[3];
  57. int restart = 1;
  58. /* syscall ? */
  59. if (TRAP(regs) != 0x0C00)
  60. return;
  61. /* error signalled ? */
  62. if (!(regs->ccr & 0x10000000))
  63. return;
  64. switch (ret) {
  65. case ERESTART_RESTARTBLOCK:
  66. case ERESTARTNOHAND:
  67. /* ERESTARTNOHAND means that the syscall should only be
  68. * restarted if there was no handler for the signal, and since
  69. * we only get here if there is a handler, we dont restart.
  70. */
  71. restart = !has_handler;
  72. break;
  73. case ERESTARTSYS:
  74. /* ERESTARTSYS means to restart the syscall if there is no
  75. * handler or the handler was registered with SA_RESTART
  76. */
  77. restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
  78. break;
  79. case ERESTARTNOINTR:
  80. /* ERESTARTNOINTR means that the syscall should be
  81. * called again after the signal handler returns.
  82. */
  83. break;
  84. default:
  85. return;
  86. }
  87. if (restart) {
  88. if (ret == ERESTART_RESTARTBLOCK)
  89. regs->gpr[0] = __NR_restart_syscall;
  90. else
  91. regs->gpr[3] = regs->orig_gpr3;
  92. regs->nip -= 4;
  93. regs->result = 0;
  94. } else {
  95. regs->result = -EINTR;
  96. regs->gpr[3] = EINTR;
  97. regs->ccr |= 0x10000000;
  98. }
  99. }
  100. static int do_signal(struct pt_regs *regs)
  101. {
  102. sigset_t *oldset;
  103. siginfo_t info;
  104. int signr;
  105. struct k_sigaction ka;
  106. int ret;
  107. int is32 = is_32bit_task();
  108. if (current_thread_info()->local_flags & _TLF_RESTORE_SIGMASK)
  109. oldset = &current->saved_sigmask;
  110. else
  111. oldset = &current->blocked;
  112. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  113. /* Is there any syscall restart business here ? */
  114. check_syscall_restart(regs, &ka, signr > 0);
  115. if (signr <= 0) {
  116. struct thread_info *ti = current_thread_info();
  117. /* No signal to deliver -- put the saved sigmask back */
  118. if (ti->local_flags & _TLF_RESTORE_SIGMASK) {
  119. ti->local_flags &= ~_TLF_RESTORE_SIGMASK;
  120. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  121. }
  122. regs->trap = 0;
  123. return 0; /* no signals delivered */
  124. }
  125. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  126. /*
  127. * Reenable the DABR before delivering the signal to
  128. * user space. The DABR will have been cleared if it
  129. * triggered inside the kernel.
  130. */
  131. if (current->thread.dabr)
  132. set_dabr(current->thread.dabr);
  133. #endif
  134. /* Re-enable the breakpoints for the signal stack */
  135. thread_change_pc(current, regs);
  136. if (is32) {
  137. if (ka.sa.sa_flags & SA_SIGINFO)
  138. ret = handle_rt_signal32(signr, &ka, &info, oldset,
  139. regs);
  140. else
  141. ret = handle_signal32(signr, &ka, &info, oldset,
  142. regs);
  143. } else {
  144. ret = handle_rt_signal64(signr, &ka, &info, oldset, regs);
  145. }
  146. regs->trap = 0;
  147. if (ret) {
  148. spin_lock_irq(&current->sighand->siglock);
  149. sigorsets(&current->blocked, &current->blocked,
  150. &ka.sa.sa_mask);
  151. if (!(ka.sa.sa_flags & SA_NODEFER))
  152. sigaddset(&current->blocked, signr);
  153. recalc_sigpending();
  154. spin_unlock_irq(&current->sighand->siglock);
  155. /*
  156. * A signal was successfully delivered; the saved sigmask is in
  157. * its frame, and we can clear the TLF_RESTORE_SIGMASK flag.
  158. */
  159. current_thread_info()->local_flags &= ~_TLF_RESTORE_SIGMASK;
  160. /*
  161. * Let tracing know that we've done the handler setup.
  162. */
  163. tracehook_signal_handler(signr, &info, &ka, regs,
  164. test_thread_flag(TIF_SINGLESTEP));
  165. }
  166. return ret;
  167. }
  168. void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  169. {
  170. if (thread_info_flags & _TIF_SIGPENDING)
  171. do_signal(regs);
  172. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  173. clear_thread_flag(TIF_NOTIFY_RESUME);
  174. tracehook_notify_resume(regs);
  175. if (current->replacement_session_keyring)
  176. key_replace_session_keyring();
  177. }
  178. }
  179. long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  180. unsigned long r5, unsigned long r6, unsigned long r7,
  181. unsigned long r8, struct pt_regs *regs)
  182. {
  183. return do_sigaltstack(uss, uoss, regs->gpr[1]);
  184. }