Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/linville/wireles...
[pandora-kernel.git] / arch / blackfin / kernel / signal.c
1 /*
2  * File:         arch/blackfin/kernel/signal.c
3  * Based on:
4  * Author:
5  *
6  * Created:
7  * Description:
8  *
9  * Modified:
10  *               Copyright 2004-2006 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see the file COPYING, or write
26  * to the Free Software Foundation, Inc.,
27  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29
30 #include <linux/signal.h>
31 #include <linux/syscalls.h>
32 #include <linux/ptrace.h>
33 #include <linux/tty.h>
34 #include <linux/personality.h>
35 #include <linux/binfmts.h>
36 #include <linux/freezer.h>
37 #include <linux/uaccess.h>
38
39 #include <asm/cacheflush.h>
40 #include <asm/ucontext.h>
41 #include <asm/fixed_code.h>
42
43 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
44
45 struct fdpic_func_descriptor {
46         unsigned long   text;
47         unsigned long   GOT;
48 };
49
50 struct rt_sigframe {
51         int sig;
52         struct siginfo *pinfo;
53         void *puc;
54         /* This is no longer needed by the kernel, but unfortunately userspace
55          * code expects it to be there.  */
56         char retcode[8];
57         struct siginfo info;
58         struct ucontext uc;
59 };
60
61 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
62 {
63         return do_sigaltstack(uss, uoss, rdusp());
64 }
65
66 static inline int
67 rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
68 {
69         unsigned long usp = 0;
70         int err = 0;
71
72 #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
73
74         /* restore passed registers */
75         RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
76         RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
77         RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
78         RESTORE(p4); RESTORE(p5);
79         err |= __get_user(usp, &sc->sc_usp);
80         wrusp(usp);
81         RESTORE(a0w); RESTORE(a1w);
82         RESTORE(a0x); RESTORE(a1x);
83         RESTORE(astat);
84         RESTORE(rets);
85         RESTORE(pc);
86         RESTORE(retx);
87         RESTORE(fp);
88         RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
89         RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
90         RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
91         RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
92         RESTORE(lc0); RESTORE(lc1);
93         RESTORE(lt0); RESTORE(lt1);
94         RESTORE(lb0); RESTORE(lb1);
95         RESTORE(seqstat);
96
97         regs->orig_p0 = -1;     /* disable syscall checks */
98
99         *pr0 = regs->r0;
100         return err;
101 }
102
103 asmlinkage int do_rt_sigreturn(unsigned long __unused)
104 {
105         struct pt_regs *regs = (struct pt_regs *)__unused;
106         unsigned long usp = rdusp();
107         struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
108         sigset_t set;
109         int r0;
110
111         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
112                 goto badframe;
113         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
114                 goto badframe;
115
116         sigdelsetmask(&set, ~_BLOCKABLE);
117         spin_lock_irq(&current->sighand->siglock);
118         current->blocked = set;
119         recalc_sigpending();
120         spin_unlock_irq(&current->sighand->siglock);
121
122         if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
123                 goto badframe;
124
125         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
126                 goto badframe;
127
128         return r0;
129
130  badframe:
131         force_sig(SIGSEGV, current);
132         return 0;
133 }
134
135 static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
136 {
137         int err = 0;
138
139 #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
140
141         SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
142         SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
143         SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
144         SETUP(p4); SETUP(p5);
145         err |= __put_user(rdusp(), &sc->sc_usp);
146         SETUP(a0w); SETUP(a1w);
147         SETUP(a0x); SETUP(a1x);
148         SETUP(astat);
149         SETUP(rets);
150         SETUP(pc);
151         SETUP(retx);
152         SETUP(fp);
153         SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
154         SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
155         SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
156         SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
157         SETUP(lc0); SETUP(lc1);
158         SETUP(lt0); SETUP(lt1);
159         SETUP(lb0); SETUP(lb1);
160         SETUP(seqstat);
161
162         return err;
163 }
164
165 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
166                                  size_t frame_size)
167 {
168         unsigned long usp;
169
170         /* Default to using normal stack.  */
171         usp = rdusp();
172
173         /* This is the X/Open sanctioned signal stack switching.  */
174         if (ka->sa.sa_flags & SA_ONSTACK) {
175                 if (!on_sig_stack(usp))
176                         usp = current->sas_ss_sp + current->sas_ss_size;
177         }
178         return (void *)((usp - frame_size) & -8UL);
179 }
180
181 static int
182 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
183                sigset_t * set, struct pt_regs *regs)
184 {
185         struct rt_sigframe *frame;
186         int err = 0;
187
188         frame = get_sigframe(ka, regs, sizeof(*frame));
189
190         err |= __put_user((current_thread_info()->exec_domain
191                            && current_thread_info()->exec_domain->signal_invmap
192                            && sig < 32
193                            ? current_thread_info()->exec_domain->
194                            signal_invmap[sig] : sig), &frame->sig);
195
196         err |= __put_user(&frame->info, &frame->pinfo);
197         err |= __put_user(&frame->uc, &frame->puc);
198         err |= copy_siginfo_to_user(&frame->info, info);
199
200         /* Create the ucontext.  */
201         err |= __put_user(0, &frame->uc.uc_flags);
202         err |= __put_user(0, &frame->uc.uc_link);
203         err |=
204             __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
205         err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
206         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
207         err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
208         err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
209
210         if (err)
211                 goto give_sigsegv;
212
213         /* Set up registers for signal handler */
214         wrusp((unsigned long)frame);
215         if (current->personality & FDPIC_FUNCPTRS) {
216                 struct fdpic_func_descriptor __user *funcptr =
217                         (struct fdpic_func_descriptor *) ka->sa.sa_handler;
218                 __get_user(regs->pc, &funcptr->text);
219                 __get_user(regs->p3, &funcptr->GOT);
220         } else
221                 regs->pc = (unsigned long)ka->sa.sa_handler;
222         regs->rets = SIGRETURN_STUB;
223
224         regs->r0 = frame->sig;
225         regs->r1 = (unsigned long)(&frame->info);
226         regs->r2 = (unsigned long)(&frame->uc);
227
228         return 0;
229
230  give_sigsegv:
231         if (sig == SIGSEGV)
232                 ka->sa.sa_handler = SIG_DFL;
233         force_sig(SIGSEGV, current);
234         return -EFAULT;
235 }
236
237 static inline void
238 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
239 {
240         switch (regs->r0) {
241         case -ERESTARTNOHAND:
242                 if (!has_handler)
243                         goto do_restart;
244                 regs->r0 = -EINTR;
245                 break;
246
247         case -ERESTARTSYS:
248                 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
249                         regs->r0 = -EINTR;
250                         break;
251                 }
252                 /* fallthrough */
253         case -ERESTARTNOINTR:
254  do_restart:
255                 regs->p0 = regs->orig_p0;
256                 regs->r0 = regs->orig_r0;
257                 regs->pc -= 2;
258                 break;
259         }
260 }
261
262 /*
263  * OK, we're invoking a handler
264  */
265 static int
266 handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
267               sigset_t *oldset, struct pt_regs *regs)
268 {
269         int ret;
270
271         /* are we from a system call? to see pt_regs->orig_p0 */
272         if (regs->orig_p0 >= 0)
273                 /* If so, check system call restarting.. */
274                 handle_restart(regs, ka, 1);
275
276         /* set up the stack frame */
277         ret = setup_rt_frame(sig, ka, info, oldset, regs);
278
279         if (ret == 0) {
280                 spin_lock_irq(&current->sighand->siglock);
281                 sigorsets(&current->blocked, &current->blocked,
282                           &ka->sa.sa_mask);
283                 if (!(ka->sa.sa_flags & SA_NODEFER))
284                         sigaddset(&current->blocked, sig);
285                 recalc_sigpending();
286                 spin_unlock_irq(&current->sighand->siglock);
287         }
288         return ret;
289 }
290
291 /*
292  * Note that 'init' is a special process: it doesn't get signals it doesn't
293  * want to handle. Thus you cannot kill init even with a SIGKILL even by
294  * mistake.
295  *
296  * Note that we go through the signals twice: once to check the signals
297  * that the kernel can handle, and then we build all the user-level signal
298  * handling stack-frames in one go after that.
299  */
300 asmlinkage void do_signal(struct pt_regs *regs)
301 {
302         siginfo_t info;
303         int signr;
304         struct k_sigaction ka;
305         sigset_t *oldset;
306
307         current->thread.esp0 = (unsigned long)regs;
308
309         if (try_to_freeze())
310                 goto no_signal;
311
312         if (test_thread_flag(TIF_RESTORE_SIGMASK))
313                 oldset = &current->saved_sigmask;
314         else
315                 oldset = &current->blocked;
316
317         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
318         if (signr > 0) {
319                 /* Whee!  Actually deliver the signal.  */
320                 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
321                         /* a signal was successfully delivered; the saved
322                          * sigmask will have been stored in the signal frame,
323                          * and will be restored by sigreturn, so we can simply
324                          * clear the TIF_RESTORE_SIGMASK flag */
325                         if (test_thread_flag(TIF_RESTORE_SIGMASK))
326                                 clear_thread_flag(TIF_RESTORE_SIGMASK);
327                 }
328
329                 return;
330         }
331
332  no_signal:
333         /* Did we come from a system call? */
334         if (regs->orig_p0 >= 0)
335                 /* Restart the system call - no handlers present */
336                 handle_restart(regs, NULL, 0);
337
338         /* if there's no signal to deliver, we just put the saved sigmask
339          * back */
340         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
341                 clear_thread_flag(TIF_RESTORE_SIGMASK);
342                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
343         }
344 }