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