Merge branch 'master' of /home/cbou/linux-2.6
[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 /* Location of the trace bit in SYSCFG. */
46 #define TRACE_BITS 0x0001
47
48 struct fdpic_func_descriptor {
49         unsigned long   text;
50         unsigned long   GOT;
51 };
52
53 struct rt_sigframe {
54         int sig;
55         struct siginfo *pinfo;
56         void *puc;
57         /* This is no longer needed by the kernel, but unfortunately userspace
58          * code expects it to be there.  */
59         char retcode[8];
60         struct siginfo info;
61         struct ucontext uc;
62 };
63
64 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
65 {
66         return do_sigaltstack(uss, uoss, rdusp());
67 }
68
69 static inline int
70 rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
71 {
72         unsigned long usp = 0;
73         int err = 0;
74
75 #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
76
77         /* restore passed registers */
78         RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
79         RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
80         RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
81         RESTORE(p4); RESTORE(p5);
82         err |= __get_user(usp, &sc->sc_usp);
83         wrusp(usp);
84         RESTORE(a0w); RESTORE(a1w);
85         RESTORE(a0x); RESTORE(a1x);
86         RESTORE(astat);
87         RESTORE(rets);
88         RESTORE(pc);
89         RESTORE(retx);
90         RESTORE(fp);
91         RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
92         RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
93         RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
94         RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
95         RESTORE(lc0); RESTORE(lc1);
96         RESTORE(lt0); RESTORE(lt1);
97         RESTORE(lb0); RESTORE(lb1);
98         RESTORE(seqstat);
99
100         regs->orig_p0 = -1;     /* disable syscall checks */
101
102         *pr0 = regs->r0;
103         return err;
104 }
105
106 asmlinkage int do_rt_sigreturn(unsigned long __unused)
107 {
108         struct pt_regs *regs = (struct pt_regs *)__unused;
109         unsigned long usp = rdusp();
110         struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
111         sigset_t set;
112         int r0;
113
114         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
115                 goto badframe;
116         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
117                 goto badframe;
118
119         sigdelsetmask(&set, ~_BLOCKABLE);
120         spin_lock_irq(&current->sighand->siglock);
121         current->blocked = set;
122         recalc_sigpending();
123         spin_unlock_irq(&current->sighand->siglock);
124
125         if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
126                 goto badframe;
127
128         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
129                 goto badframe;
130
131         return r0;
132
133  badframe:
134         force_sig(SIGSEGV, current);
135         return 0;
136 }
137
138 static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
139 {
140         int err = 0;
141
142 #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
143
144         SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
145         SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
146         SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
147         SETUP(p4); SETUP(p5);
148         err |= __put_user(rdusp(), &sc->sc_usp);
149         SETUP(a0w); SETUP(a1w);
150         SETUP(a0x); SETUP(a1x);
151         SETUP(astat);
152         SETUP(rets);
153         SETUP(pc);
154         SETUP(retx);
155         SETUP(fp);
156         SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
157         SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
158         SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
159         SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
160         SETUP(lc0); SETUP(lc1);
161         SETUP(lt0); SETUP(lt1);
162         SETUP(lb0); SETUP(lb1);
163         SETUP(seqstat);
164
165         return err;
166 }
167
168 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
169                                  size_t frame_size)
170 {
171         unsigned long usp;
172
173         /* Default to using normal stack.  */
174         usp = rdusp();
175
176         /* This is the X/Open sanctioned signal stack switching.  */
177         if (ka->sa.sa_flags & SA_ONSTACK) {
178                 if (!on_sig_stack(usp))
179                         usp = current->sas_ss_sp + current->sas_ss_size;
180         }
181         return (void *)((usp - frame_size) & -8UL);
182 }
183
184 static int
185 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
186                sigset_t * set, struct pt_regs *regs)
187 {
188         struct rt_sigframe *frame;
189         int err = 0;
190
191         frame = get_sigframe(ka, regs, sizeof(*frame));
192
193         err |= __put_user((current_thread_info()->exec_domain
194                            && current_thread_info()->exec_domain->signal_invmap
195                            && sig < 32
196                            ? current_thread_info()->exec_domain->
197                            signal_invmap[sig] : sig), &frame->sig);
198
199         err |= __put_user(&frame->info, &frame->pinfo);
200         err |= __put_user(&frame->uc, &frame->puc);
201         err |= copy_siginfo_to_user(&frame->info, info);
202
203         /* Create the ucontext.  */
204         err |= __put_user(0, &frame->uc.uc_flags);
205         err |= __put_user(0, &frame->uc.uc_link);
206         err |=
207             __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
208         err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
209         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
210         err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
211         err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
212
213         if (err)
214                 goto give_sigsegv;
215
216         /* Set up registers for signal handler */
217         wrusp((unsigned long)frame);
218         if (current->personality & FDPIC_FUNCPTRS) {
219                 struct fdpic_func_descriptor __user *funcptr =
220                         (struct fdpic_func_descriptor *) ka->sa.sa_handler;
221                 __get_user(regs->pc, &funcptr->text);
222                 __get_user(regs->p3, &funcptr->GOT);
223         } else
224                 regs->pc = (unsigned long)ka->sa.sa_handler;
225         regs->rets = SIGRETURN_STUB;
226
227         regs->r0 = frame->sig;
228         regs->r1 = (unsigned long)(&frame->info);
229         regs->r2 = (unsigned long)(&frame->uc);
230
231         /*
232          * Clear the trace flag when entering the signal handler, but
233          * notify any tracer that was single-stepping it. The tracer
234          * may want to single-step inside the handler too.
235          */
236         if (regs->syscfg & TRACE_BITS) {
237                 regs->syscfg &= ~TRACE_BITS;
238                 ptrace_notify(SIGTRAP);
239         }
240
241         return 0;
242
243  give_sigsegv:
244         if (sig == SIGSEGV)
245                 ka->sa.sa_handler = SIG_DFL;
246         force_sig(SIGSEGV, current);
247         return -EFAULT;
248 }
249
250 static inline void
251 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
252 {
253         switch (regs->r0) {
254         case -ERESTARTNOHAND:
255                 if (!has_handler)
256                         goto do_restart;
257                 regs->r0 = -EINTR;
258                 break;
259
260         case -ERESTARTSYS:
261                 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
262                         regs->r0 = -EINTR;
263                         break;
264                 }
265                 /* fallthrough */
266         case -ERESTARTNOINTR:
267  do_restart:
268                 regs->p0 = regs->orig_p0;
269                 regs->r0 = regs->orig_r0;
270                 regs->pc -= 2;
271                 break;
272         }
273 }
274
275 /*
276  * OK, we're invoking a handler
277  */
278 static int
279 handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
280               sigset_t *oldset, struct pt_regs *regs)
281 {
282         int ret;
283
284         /* are we from a system call? to see pt_regs->orig_p0 */
285         if (regs->orig_p0 >= 0)
286                 /* If so, check system call restarting.. */
287                 handle_restart(regs, ka, 1);
288
289         /* set up the stack frame */
290         ret = setup_rt_frame(sig, ka, info, oldset, regs);
291
292         if (ret == 0) {
293                 spin_lock_irq(&current->sighand->siglock);
294                 sigorsets(&current->blocked, &current->blocked,
295                           &ka->sa.sa_mask);
296                 if (!(ka->sa.sa_flags & SA_NODEFER))
297                         sigaddset(&current->blocked, sig);
298                 recalc_sigpending();
299                 spin_unlock_irq(&current->sighand->siglock);
300         }
301         return ret;
302 }
303
304 /*
305  * Note that 'init' is a special process: it doesn't get signals it doesn't
306  * want to handle. Thus you cannot kill init even with a SIGKILL even by
307  * mistake.
308  *
309  * Note that we go through the signals twice: once to check the signals
310  * that the kernel can handle, and then we build all the user-level signal
311  * handling stack-frames in one go after that.
312  */
313 asmlinkage void do_signal(struct pt_regs *regs)
314 {
315         siginfo_t info;
316         int signr;
317         struct k_sigaction ka;
318         sigset_t *oldset;
319
320         current->thread.esp0 = (unsigned long)regs;
321
322         if (try_to_freeze())
323                 goto no_signal;
324
325         if (test_thread_flag(TIF_RESTORE_SIGMASK))
326                 oldset = &current->saved_sigmask;
327         else
328                 oldset = &current->blocked;
329
330         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
331         if (signr > 0) {
332                 /* Whee!  Actually deliver the signal.  */
333                 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
334                         /* a signal was successfully delivered; the saved
335                          * sigmask will have been stored in the signal frame,
336                          * and will be restored by sigreturn, so we can simply
337                          * clear the TIF_RESTORE_SIGMASK flag */
338                         if (test_thread_flag(TIF_RESTORE_SIGMASK))
339                                 clear_thread_flag(TIF_RESTORE_SIGMASK);
340                 }
341
342                 return;
343         }
344
345  no_signal:
346         /* Did we come from a system call? */
347         if (regs->orig_p0 >= 0)
348                 /* Restart the system call - no handlers present */
349                 handle_restart(regs, NULL, 0);
350
351         /* if there's no signal to deliver, we just put the saved sigmask
352          * back */
353         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
354                 clear_thread_flag(TIF_RESTORE_SIGMASK);
355                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
356         }
357 }