signal.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. set_current_blocked(set);
  49. }
  50. static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
  51. int has_handler)
  52. {
  53. unsigned long ret = regs->gpr[3];
  54. int restart = 1;
  55. /* syscall ? */
  56. if (TRAP(regs) != 0x0C00)
  57. return;
  58. /* error signalled ? */
  59. if (!(regs->ccr & 0x10000000))
  60. return;
  61. switch (ret) {
  62. case ERESTART_RESTARTBLOCK:
  63. case ERESTARTNOHAND:
  64. /* ERESTARTNOHAND means that the syscall should only be
  65. * restarted if there was no handler for the signal, and since
  66. * we only get here if there is a handler, we dont restart.
  67. */
  68. restart = !has_handler;
  69. break;
  70. case ERESTARTSYS:
  71. /* ERESTARTSYS means to restart the syscall if there is no
  72. * handler or the handler was registered with SA_RESTART
  73. */
  74. restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
  75. break;
  76. case ERESTARTNOINTR:
  77. /* ERESTARTNOINTR means that the syscall should be
  78. * called again after the signal handler returns.
  79. */
  80. break;
  81. default:
  82. return;
  83. }
  84. if (restart) {
  85. if (ret == ERESTART_RESTARTBLOCK)
  86. regs->gpr[0] = __NR_restart_syscall;
  87. else
  88. regs->gpr[3] = regs->orig_gpr3;
  89. regs->nip -= 4;
  90. regs->result = 0;
  91. } else {
  92. regs->result = -EINTR;
  93. regs->gpr[3] = EINTR;
  94. regs->ccr |= 0x10000000;
  95. }
  96. }
  97. static int do_signal(struct pt_regs *regs)
  98. {
  99. sigset_t *oldset;
  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
  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. regs->trap = 0;
  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. regs->trap = 0;
  144. if (ret) {
  145. block_sigmask(&ka, signr);
  146. /*
  147. * A signal was successfully delivered; the saved sigmask is in
  148. * its frame, and we can clear the TLF_RESTORE_SIGMASK flag.
  149. */
  150. current_thread_info()->local_flags &= ~_TLF_RESTORE_SIGMASK;
  151. /*
  152. * Let tracing know that we've done the handler setup.
  153. */
  154. tracehook_signal_handler(signr, &info, &ka, regs,
  155. test_thread_flag(TIF_SINGLESTEP));
  156. }
  157. return ret;
  158. }
  159. void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  160. {
  161. if (thread_info_flags & _TIF_SIGPENDING)
  162. do_signal(regs);
  163. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  164. clear_thread_flag(TIF_NOTIFY_RESUME);
  165. tracehook_notify_resume(regs);
  166. if (current->replacement_session_keyring)
  167. key_replace_session_keyring();
  168. }
  169. }
  170. long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  171. unsigned long r5, unsigned long r6, unsigned long r7,
  172. unsigned long r8, struct pt_regs *regs)
  173. {
  174. return do_sigaltstack(uss, uoss, regs->gpr[1]);
  175. }