signal_kern.c 5.7 KB

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