Merge tag 'parisc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/parisc-2.6
[cascardo/linux.git] / arch / blackfin / kernel / signal.c
1 /*
2  * Copyright 2004-2010 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 #include <asm/syscall.h>
21
22 /* Location of the trace bit in SYSCFG. */
23 #define TRACE_BITS 0x0001
24
25 struct fdpic_func_descriptor {
26         unsigned long   text;
27         unsigned long   GOT;
28 };
29
30 struct rt_sigframe {
31         int sig;
32         struct siginfo *pinfo;
33         void *puc;
34         /* This is no longer needed by the kernel, but unfortunately userspace
35          * code expects it to be there.  */
36         char retcode[8];
37         struct siginfo info;
38         struct ucontext uc;
39 };
40
41 asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
42 {
43         return do_sigaltstack(uss, uoss, rdusp());
44 }
45
46 static inline int
47 rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
48 {
49         unsigned long usp = 0;
50         int err = 0;
51
52         /* Always make any pending restarted system calls return -EINTR */
53         current_thread_info()->restart_block.fn = do_no_restart_syscall;
54
55 #define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
56
57         /* restore passed registers */
58         RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
59         RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
60         RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
61         RESTORE(p4); RESTORE(p5);
62         err |= __get_user(usp, &sc->sc_usp);
63         wrusp(usp);
64         RESTORE(a0w); RESTORE(a1w);
65         RESTORE(a0x); RESTORE(a1x);
66         RESTORE(astat);
67         RESTORE(rets);
68         RESTORE(pc);
69         RESTORE(retx);
70         RESTORE(fp);
71         RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
72         RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
73         RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
74         RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
75         RESTORE(lc0); RESTORE(lc1);
76         RESTORE(lt0); RESTORE(lt1);
77         RESTORE(lb0); RESTORE(lb1);
78         RESTORE(seqstat);
79
80         regs->orig_p0 = -1;     /* disable syscall checks */
81
82         *pr0 = regs->r0;
83         return err;
84 }
85
86 asmlinkage int do_rt_sigreturn(unsigned long __unused)
87 {
88         struct pt_regs *regs = (struct pt_regs *)__unused;
89         unsigned long usp = rdusp();
90         struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
91         sigset_t set;
92         int r0;
93
94         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
95                 goto badframe;
96         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
97                 goto badframe;
98
99         set_current_blocked(&set);
100
101         if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
102                 goto badframe;
103
104         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
105                 goto badframe;
106
107         return r0;
108
109  badframe:
110         force_sig(SIGSEGV, current);
111         return 0;
112 }
113
114 static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
115 {
116         int err = 0;
117
118 #define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
119
120         SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
121         SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
122         SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
123         SETUP(p4); SETUP(p5);
124         err |= __put_user(rdusp(), &sc->sc_usp);
125         SETUP(a0w); SETUP(a1w);
126         SETUP(a0x); SETUP(a1x);
127         SETUP(astat);
128         SETUP(rets);
129         SETUP(pc);
130         SETUP(retx);
131         SETUP(fp);
132         SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
133         SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
134         SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
135         SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
136         SETUP(lc0); SETUP(lc1);
137         SETUP(lt0); SETUP(lt1);
138         SETUP(lb0); SETUP(lb1);
139         SETUP(seqstat);
140
141         return err;
142 }
143
144 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
145                                  size_t frame_size)
146 {
147         unsigned long usp;
148
149         /* Default to using normal stack.  */
150         usp = rdusp();
151
152         /* This is the X/Open sanctioned signal stack switching.  */
153         if (ka->sa.sa_flags & SA_ONSTACK) {
154                 if (!on_sig_stack(usp))
155                         usp = current->sas_ss_sp + current->sas_ss_size;
156         }
157         return (void *)((usp - frame_size) & -8UL);
158 }
159
160 static int
161 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
162                sigset_t * set, struct pt_regs *regs)
163 {
164         struct rt_sigframe *frame;
165         int err = 0;
166
167         frame = get_sigframe(ka, regs, sizeof(*frame));
168
169         err |= __put_user((current_thread_info()->exec_domain
170                            && current_thread_info()->exec_domain->signal_invmap
171                            && sig < 32
172                            ? current_thread_info()->exec_domain->
173                            signal_invmap[sig] : sig), &frame->sig);
174
175         err |= __put_user(&frame->info, &frame->pinfo);
176         err |= __put_user(&frame->uc, &frame->puc);
177         err |= copy_siginfo_to_user(&frame->info, info);
178
179         /* Create the ucontext.  */
180         err |= __put_user(0, &frame->uc.uc_flags);
181         err |= __put_user(0, &frame->uc.uc_link);
182         err |=
183             __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
184         err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
185         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
186         err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
187         err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
188
189         if (err)
190                 return -EFAULT;
191
192         /* Set up registers for signal handler */
193         if (current->personality & FDPIC_FUNCPTRS) {
194                 struct fdpic_func_descriptor __user *funcptr =
195                         (struct fdpic_func_descriptor *) ka->sa.sa_handler;
196                 u32 pc, p3;
197                 err |= __get_user(pc, &funcptr->text);
198                 err |= __get_user(p3, &funcptr->GOT);
199                 if (err)
200                         return -EFAULT;
201                 regs->pc = pc;
202                 regs->p3 = p3;
203         } else
204                 regs->pc = (unsigned long)ka->sa.sa_handler;
205         wrusp((unsigned long)frame);
206         regs->rets = SIGRETURN_STUB;
207
208         regs->r0 = frame->sig;
209         regs->r1 = (unsigned long)(&frame->info);
210         regs->r2 = (unsigned long)(&frame->uc);
211
212         return 0;
213 }
214
215 static inline void
216 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
217 {
218         switch (regs->r0) {
219         case -ERESTARTNOHAND:
220                 if (!has_handler)
221                         goto do_restart;
222                 regs->r0 = -EINTR;
223                 break;
224
225         case -ERESTARTSYS:
226                 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
227                         regs->r0 = -EINTR;
228                         break;
229                 }
230                 /* fallthrough */
231         case -ERESTARTNOINTR:
232  do_restart:
233                 regs->p0 = regs->orig_p0;
234                 regs->r0 = regs->orig_r0;
235                 regs->pc -= 2;
236                 break;
237
238         case -ERESTART_RESTARTBLOCK:
239                 regs->p0 = __NR_restart_syscall;
240                 regs->pc -= 2;
241                 break;
242         }
243 }
244
245 /*
246  * OK, we're invoking a handler
247  */
248 static void
249 handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
250               struct pt_regs *regs)
251 {
252         /* are we from a system call? to see pt_regs->orig_p0 */
253         if (regs->orig_p0 >= 0)
254                 /* If so, check system call restarting.. */
255                 handle_restart(regs, ka, 1);
256
257         /* set up the stack frame */
258         if (setup_rt_frame(sig, ka, info, sigmask_to_save(), regs) < 0)
259                 force_sigsegv(sig, current);
260         else 
261                 signal_delivered(sig, info, ka, regs,
262                                 test_thread_flag(TIF_SINGLESTEP));
263 }
264
265 /*
266  * Note that 'init' is a special process: it doesn't get signals it doesn't
267  * want to handle. Thus you cannot kill init even with a SIGKILL even by
268  * mistake.
269  *
270  * Note that we go through the signals twice: once to check the signals
271  * that the kernel can handle, and then we build all the user-level signal
272  * handling stack-frames in one go after that.
273  */
274 asmlinkage void do_signal(struct pt_regs *regs)
275 {
276         siginfo_t info;
277         int signr;
278         struct k_sigaction ka;
279
280         current->thread.esp0 = (unsigned long)regs;
281
282         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
283         if (signr > 0) {
284                 /* Whee!  Actually deliver the signal.  */
285                 handle_signal(signr, &info, &ka, regs);
286                 return;
287         }
288
289         /* Did we come from a system call? */
290         if (regs->orig_p0 >= 0)
291                 /* Restart the system call - no handlers present */
292                 handle_restart(regs, NULL, 0);
293
294         /* if there's no signal to deliver, we just put the saved sigmask
295          * back */
296         restore_saved_sigmask();
297 }
298
299 /*
300  * notification of userspace execution resumption
301  */
302 asmlinkage void do_notify_resume(struct pt_regs *regs)
303 {
304         if (test_thread_flag(TIF_SIGPENDING))
305                 do_signal(regs);
306
307         if (test_thread_flag(TIF_NOTIFY_RESUME)) {
308                 clear_thread_flag(TIF_NOTIFY_RESUME);
309                 tracehook_notify_resume(regs);
310         }
311 }
312