signal.c 4.5 KB

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