signal.c 5.2 KB

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