2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/stddef.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"
25 #include "frame_kern.h"
26 #include "sigcontext.h"
29 EXPORT_SYMBOL(block_signals);
30 EXPORT_SYMBOL(unblock_signals);
32 #define _S(nr) (1<<((nr)-1))
34 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
37 * OK, we're invoking a handler
39 static int handle_signal(struct pt_regs *regs, unsigned long signr,
40 struct k_sigaction *ka, siginfo_t *info,
46 /* Always make any pending restarted system calls return -EINTR */
47 current_thread_info()->restart_block.fn = do_no_restart_syscall;
49 /* Did we come from a system call? */
50 if(PT_REGS_SYSCALL_NR(regs) >= 0){
51 /* If so, check system call restarting.. */
52 switch(PT_REGS_SYSCALL_RET(regs)){
53 case -ERESTART_RESTARTBLOCK:
55 PT_REGS_SYSCALL_RET(regs) = -EINTR;
59 if (!(ka->sa.sa_flags & SA_RESTART)) {
60 PT_REGS_SYSCALL_RET(regs) = -EINTR;
65 PT_REGS_RESTART_SYSCALL(regs);
66 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
71 sp = PT_REGS_SP(regs);
72 if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
73 sp = current->sas_ss_sp + current->sas_ss_size;
75 #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
76 if(!(ka->sa.sa_flags & SA_SIGINFO))
77 err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
80 err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
83 spin_lock_irq(¤t->sighand->siglock);
84 current->blocked = *oldset;
86 spin_unlock_irq(¤t->sighand->siglock);
87 force_sigsegv(signr, current);
89 spin_lock_irq(¤t->sighand->siglock);
90 sigorsets(¤t->blocked, ¤t->blocked,
92 if(!(ka->sa.sa_flags & SA_NODEFER))
93 sigaddset(¤t->blocked, signr);
95 spin_unlock_irq(¤t->sighand->siglock);
101 static int kern_do_signal(struct pt_regs *regs)
103 struct k_sigaction ka_copy;
106 int sig, handled_sig = 0;
108 if (test_thread_flag(TIF_RESTORE_SIGMASK))
109 oldset = ¤t->saved_sigmask;
111 oldset = ¤t->blocked;
113 while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){
115 /* Whee! Actually deliver the signal. */
116 if(!handle_signal(regs, sig, &ka_copy, &info, oldset)){
117 /* a signal was successfully delivered; the saved
118 * sigmask will have been stored in the signal frame,
119 * and will be restored by sigreturn, so we can simply
120 * clear the TIF_RESTORE_SIGMASK flag */
121 if (test_thread_flag(TIF_RESTORE_SIGMASK))
122 clear_thread_flag(TIF_RESTORE_SIGMASK);
127 /* Did we come from a system call? */
128 if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){
129 /* Restart the system call - no handlers present */
130 switch(PT_REGS_SYSCALL_RET(regs)){
131 case -ERESTARTNOHAND:
133 case -ERESTARTNOINTR:
134 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
135 PT_REGS_RESTART_SYSCALL(regs);
137 case -ERESTART_RESTARTBLOCK:
138 PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
139 PT_REGS_RESTART_SYSCALL(regs);
144 /* This closes a way to execute a system call on the host. If
145 * you set a breakpoint on a system call instruction and singlestep
146 * from it, the tracing thread used to PTRACE_SINGLESTEP the process
147 * rather than PTRACE_SYSCALL it, allowing the system call to execute
148 * on the host. The tracing thread will check this flag and
149 * PTRACE_SYSCALL if necessary.
151 if(current->ptrace & PT_DTRACE)
152 current->thread.singlestep_syscall =
153 is_syscall(PT_REGS_IP(¤t->thread.regs));
155 /* if there's no signal to deliver, we just put the saved sigmask
157 if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) {
158 clear_thread_flag(TIF_RESTORE_SIGMASK);
159 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
166 return(kern_do_signal(¤t->thread.regs));
170 * Atomically swap in the new signal mask, and wait for a signal.
172 long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
175 spin_lock_irq(¤t->sighand->siglock);
176 current->saved_sigmask = current->blocked;
177 siginitset(¤t->blocked, mask);
179 spin_unlock_irq(¤t->sighand->siglock);
181 current->state = TASK_INTERRUPTIBLE;
183 set_thread_flag(TIF_RESTORE_SIGMASK);
184 return -ERESTARTNOHAND;
187 long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
189 return(do_sigaltstack(uss, uoss, PT_REGS_SP(¤t->thread.regs)));