signal.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "signal.h"
  21. /* Log an error when sending an unhandled signal to a process. Controlled
  22. * through debug.exception-trace sysctl.
  23. */
  24. int show_unhandled_signals = 1;
  25. /*
  26. * Allocate space for the signal frame
  27. */
  28. void __user * get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  29. size_t frame_size, int is_32)
  30. {
  31. unsigned long oldsp, newsp;
  32. /* Default to using normal stack */
  33. oldsp = get_clean_sp(regs, is_32);
  34. /* Check for alt stack */
  35. if ((ka->sa.sa_flags & SA_ONSTACK) &&
  36. current->sas_ss_size && !on_sig_stack(oldsp))
  37. oldsp = (current->sas_ss_sp + current->sas_ss_size);
  38. /* Get aligned frame */
  39. newsp = (oldsp - frame_size) & ~0xFUL;
  40. /* Check access */
  41. if (!access_ok(VERIFY_WRITE, (void __user *)newsp, oldsp - newsp))
  42. return NULL;
  43. return (void __user *)newsp;
  44. }
  45. static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
  46. int has_handler)
  47. {
  48. unsigned long ret = regs->gpr[3];
  49. int restart = 1;
  50. /* syscall ? */
  51. if (TRAP(regs) != 0x0C00)
  52. return;
  53. /* error signalled ? */
  54. if (!(regs->ccr & 0x10000000))
  55. return;
  56. switch (ret) {
  57. case ERESTART_RESTARTBLOCK:
  58. case ERESTARTNOHAND:
  59. /* ERESTARTNOHAND means that the syscall should only be
  60. * restarted if there was no handler for the signal, and since
  61. * we only get here if there is a handler, we dont restart.
  62. */
  63. restart = !has_handler;
  64. break;
  65. case ERESTARTSYS:
  66. /* ERESTARTSYS means to restart the syscall if there is no
  67. * handler or the handler was registered with SA_RESTART
  68. */
  69. restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
  70. break;
  71. case ERESTARTNOINTR:
  72. /* ERESTARTNOINTR means that the syscall should be
  73. * called again after the signal handler returns.
  74. */
  75. break;
  76. default:
  77. return;
  78. }
  79. if (restart) {
  80. if (ret == ERESTART_RESTARTBLOCK)
  81. regs->gpr[0] = __NR_restart_syscall;
  82. else
  83. regs->gpr[3] = regs->orig_gpr3;
  84. regs->nip -= 4;
  85. regs->result = 0;
  86. } else {
  87. regs->result = -EINTR;
  88. regs->gpr[3] = EINTR;
  89. regs->ccr |= 0x10000000;
  90. }
  91. }
  92. static int do_signal(struct pt_regs *regs)
  93. {
  94. sigset_t *oldset = sigmask_to_save();
  95. siginfo_t info;
  96. int signr;
  97. struct k_sigaction ka;
  98. int ret;
  99. int is32 = is_32bit_task();
  100. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  101. /* Is there any syscall restart business here ? */
  102. check_syscall_restart(regs, &ka, signr > 0);
  103. if (signr <= 0) {
  104. /* No signal to deliver -- put the saved sigmask back */
  105. restore_saved_sigmask();
  106. regs->trap = 0;
  107. return 0; /* no signals delivered */
  108. }
  109. #ifndef CONFIG_PPC_ADV_DEBUG_REGS
  110. /*
  111. * Reenable the DABR before delivering the signal to
  112. * user space. The DABR will have been cleared if it
  113. * triggered inside the kernel.
  114. */
  115. if (current->thread.hw_brk.address &&
  116. current->thread.hw_brk.type)
  117. set_breakpoint(&current->thread.hw_brk);
  118. #endif
  119. /* Re-enable the breakpoints for the signal stack */
  120. thread_change_pc(current, regs);
  121. if (is32) {
  122. if (ka.sa.sa_flags & SA_SIGINFO)
  123. ret = handle_rt_signal32(signr, &ka, &info, oldset,
  124. regs);
  125. else
  126. ret = handle_signal32(signr, &ka, &info, oldset,
  127. regs);
  128. } else {
  129. ret = handle_rt_signal64(signr, &ka, &info, oldset, regs);
  130. }
  131. regs->trap = 0;
  132. if (ret) {
  133. signal_delivered(signr, &info, &ka, regs,
  134. test_thread_flag(TIF_SINGLESTEP));
  135. }
  136. return ret;
  137. }
  138. void do_notify_resume(struct pt_regs *regs, unsigned long thread_info_flags)
  139. {
  140. user_exit();
  141. if (thread_info_flags & _TIF_UPROBE)
  142. uprobe_notify_resume(regs);
  143. if (thread_info_flags & _TIF_SIGPENDING)
  144. do_signal(regs);
  145. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  146. clear_thread_flag(TIF_NOTIFY_RESUME);
  147. tracehook_notify_resume(regs);
  148. }
  149. user_enter();
  150. }