signal.c 4.2 KB

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