signal_kern.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/config.h"
  6. #include "linux/stddef.h"
  7. #include "linux/sys.h"
  8. #include "linux/sched.h"
  9. #include "linux/wait.h"
  10. #include "linux/kernel.h"
  11. #include "linux/smp_lock.h"
  12. #include "linux/module.h"
  13. #include "linux/slab.h"
  14. #include "linux/tty.h"
  15. #include "linux/binfmts.h"
  16. #include "linux/ptrace.h"
  17. #include "asm/signal.h"
  18. #include "asm/uaccess.h"
  19. #include "asm/unistd.h"
  20. #include "user_util.h"
  21. #include "asm/ucontext.h"
  22. #include "kern_util.h"
  23. #include "signal_kern.h"
  24. #include "signal_user.h"
  25. #include "kern.h"
  26. #include "frame_kern.h"
  27. #include "sigcontext.h"
  28. #include "mode.h"
  29. EXPORT_SYMBOL(block_signals);
  30. EXPORT_SYMBOL(unblock_signals);
  31. #define _S(nr) (1<<((nr)-1))
  32. #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
  33. /*
  34. * OK, we're invoking a handler
  35. */
  36. static int handle_signal(struct pt_regs *regs, unsigned long signr,
  37. struct k_sigaction *ka, siginfo_t *info,
  38. sigset_t *oldset)
  39. {
  40. unsigned long sp;
  41. int err;
  42. /* Always make any pending restarted system calls return -EINTR */
  43. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  44. /* Did we come from a system call? */
  45. if(PT_REGS_SYSCALL_NR(regs) >= 0){
  46. /* If so, check system call restarting.. */
  47. switch(PT_REGS_SYSCALL_RET(regs)){
  48. case -ERESTART_RESTARTBLOCK:
  49. case -ERESTARTNOHAND:
  50. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  51. break;
  52. case -ERESTARTSYS:
  53. if (!(ka->sa.sa_flags & SA_RESTART)) {
  54. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  55. break;
  56. }
  57. /* fallthrough */
  58. case -ERESTARTNOINTR:
  59. PT_REGS_RESTART_SYSCALL(regs);
  60. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  61. break;
  62. }
  63. }
  64. sp = PT_REGS_SP(regs);
  65. if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
  66. sp = current->sas_ss_sp + current->sas_ss_size;
  67. #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
  68. if(!(ka->sa.sa_flags & SA_SIGINFO))
  69. err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
  70. else
  71. #endif
  72. err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
  73. if(err){
  74. spin_lock_irq(&current->sighand->siglock);
  75. current->blocked = *oldset;
  76. recalc_sigpending();
  77. spin_unlock_irq(&current->sighand->siglock);
  78. force_sigsegv(signr, current);
  79. } else {
  80. spin_lock_irq(&current->sighand->siglock);
  81. sigorsets(&current->blocked, &current->blocked,
  82. &ka->sa.sa_mask);
  83. if(!(ka->sa.sa_flags & SA_NODEFER))
  84. sigaddset(&current->blocked, signr);
  85. recalc_sigpending();
  86. spin_unlock_irq(&current->sighand->siglock);
  87. }
  88. return err;
  89. }
  90. static int kern_do_signal(struct pt_regs *regs, sigset_t *oldset)
  91. {
  92. struct k_sigaction ka_copy;
  93. siginfo_t info;
  94. int sig, handled_sig = 0;
  95. while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){
  96. handled_sig = 1;
  97. /* Whee! Actually deliver the signal. */
  98. if(!handle_signal(regs, sig, &ka_copy, &info, oldset))
  99. break;
  100. }
  101. /* Did we come from a system call? */
  102. if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){
  103. /* Restart the system call - no handlers present */
  104. if(PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOHAND ||
  105. PT_REGS_SYSCALL_RET(regs) == -ERESTARTSYS ||
  106. PT_REGS_SYSCALL_RET(regs) == -ERESTARTNOINTR){
  107. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  108. PT_REGS_RESTART_SYSCALL(regs);
  109. }
  110. else if(PT_REGS_SYSCALL_RET(regs) == -ERESTART_RESTARTBLOCK){
  111. PT_REGS_SYSCALL_RET(regs) = __NR_restart_syscall;
  112. PT_REGS_RESTART_SYSCALL(regs);
  113. }
  114. }
  115. /* This closes a way to execute a system call on the host. If
  116. * you set a breakpoint on a system call instruction and singlestep
  117. * from it, the tracing thread used to PTRACE_SINGLESTEP the process
  118. * rather than PTRACE_SYSCALL it, allowing the system call to execute
  119. * on the host. The tracing thread will check this flag and
  120. * PTRACE_SYSCALL if necessary.
  121. */
  122. if(current->ptrace & PT_DTRACE)
  123. current->thread.singlestep_syscall =
  124. is_syscall(PT_REGS_IP(&current->thread.regs));
  125. return(handled_sig);
  126. }
  127. int do_signal(void)
  128. {
  129. return(kern_do_signal(&current->thread.regs, &current->blocked));
  130. }
  131. /*
  132. * Atomically swap in the new signal mask, and wait for a signal.
  133. */
  134. long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  135. {
  136. sigset_t saveset;
  137. mask &= _BLOCKABLE;
  138. spin_lock_irq(&current->sighand->siglock);
  139. saveset = current->blocked;
  140. siginitset(&current->blocked, mask);
  141. recalc_sigpending();
  142. spin_unlock_irq(&current->sighand->siglock);
  143. PT_REGS_SYSCALL_RET(&current->thread.regs) = -EINTR;
  144. while (1) {
  145. current->state = TASK_INTERRUPTIBLE;
  146. schedule();
  147. if(kern_do_signal(&current->thread.regs, &saveset))
  148. return(-EINTR);
  149. }
  150. }
  151. long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
  152. {
  153. sigset_t saveset, newset;
  154. /* XXX: Don't preclude handling different sized sigset_t's. */
  155. if (sigsetsize != sizeof(sigset_t))
  156. return -EINVAL;
  157. if (copy_from_user(&newset, unewset, sizeof(newset)))
  158. return -EFAULT;
  159. sigdelsetmask(&newset, ~_BLOCKABLE);
  160. spin_lock_irq(&current->sighand->siglock);
  161. saveset = current->blocked;
  162. current->blocked = newset;
  163. recalc_sigpending();
  164. spin_unlock_irq(&current->sighand->siglock);
  165. PT_REGS_SYSCALL_RET(&current->thread.regs) = -EINTR;
  166. while (1) {
  167. current->state = TASK_INTERRUPTIBLE;
  168. schedule();
  169. if (kern_do_signal(&current->thread.regs, &saveset))
  170. return(-EINTR);
  171. }
  172. }
  173. long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  174. {
  175. return(do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs)));
  176. }
  177. /*
  178. * Overrides for Emacs so that we follow Linus's tabbing style.
  179. * Emacs will notice this stuff at the end of the file and automatically
  180. * adjust the settings for this buffer only. This must remain at the end
  181. * of the file.
  182. * ---------------------------------------------------------------------------
  183. * Local variables:
  184. * c-file-style: "linux"
  185. * End:
  186. */