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