signal.c 5.3 KB

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