Blackfin: fix single stepping over system calls
[safe/jmp/linux-2.6] / 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
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         return 0;
210
211  give_sigsegv:
212         if (sig == SIGSEGV)
213                 ka->sa.sa_handler = SIG_DFL;
214         force_sig(SIGSEGV, current);
215         return -EFAULT;
216 }
217
218 static inline void
219 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
220 {
221         switch (regs->r0) {
222         case -ERESTARTNOHAND:
223                 if (!has_handler)
224                         goto do_restart;
225                 regs->r0 = -EINTR;
226                 break;
227
228         case -ERESTARTSYS:
229                 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
230                         regs->r0 = -EINTR;
231                         break;
232                 }
233                 /* fallthrough */
234         case -ERESTARTNOINTR:
235  do_restart:
236                 regs->p0 = regs->orig_p0;
237                 regs->r0 = regs->orig_r0;
238                 regs->pc -= 2;
239                 break;
240         }
241 }
242
243 /*
244  * OK, we're invoking a handler
245  */
246 static int
247 handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
248               sigset_t *oldset, struct pt_regs *regs)
249 {
250         int ret;
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         ret = setup_rt_frame(sig, ka, info, oldset, regs);
259
260         if (ret == 0) {
261                 spin_lock_irq(&current->sighand->siglock);
262                 sigorsets(&current->blocked, &current->blocked,
263                           &ka->sa.sa_mask);
264                 if (!(ka->sa.sa_flags & SA_NODEFER))
265                         sigaddset(&current->blocked, sig);
266                 recalc_sigpending();
267                 spin_unlock_irq(&current->sighand->siglock);
268         }
269         return ret;
270 }
271
272 /*
273  * Note that 'init' is a special process: it doesn't get signals it doesn't
274  * want to handle. Thus you cannot kill init even with a SIGKILL even by
275  * mistake.
276  *
277  * Note that we go through the signals twice: once to check the signals
278  * that the kernel can handle, and then we build all the user-level signal
279  * handling stack-frames in one go after that.
280  */
281 asmlinkage void do_signal(struct pt_regs *regs)
282 {
283         siginfo_t info;
284         int signr;
285         struct k_sigaction ka;
286         sigset_t *oldset;
287
288         current->thread.esp0 = (unsigned long)regs;
289
290         if (try_to_freeze())
291                 goto no_signal;
292
293         if (test_thread_flag(TIF_RESTORE_SIGMASK))
294                 oldset = &current->saved_sigmask;
295         else
296                 oldset = &current->blocked;
297
298         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
299         if (signr > 0) {
300                 /* Whee!  Actually deliver the signal.  */
301                 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
302                         /* a signal was successfully delivered; the saved
303                          * sigmask will have been stored in the signal frame,
304                          * and will be restored by sigreturn, so we can simply
305                          * clear the TIF_RESTORE_SIGMASK flag */
306                         if (test_thread_flag(TIF_RESTORE_SIGMASK))
307                                 clear_thread_flag(TIF_RESTORE_SIGMASK);
308
309                         tracehook_signal_handler(signr, &info, &ka, regs,
310                                 test_thread_flag(TIF_SINGLESTEP));
311                 }
312
313                 return;
314         }
315
316  no_signal:
317         /* Did we come from a system call? */
318         if (regs->orig_p0 >= 0)
319                 /* Restart the system call - no handlers present */
320                 handle_restart(regs, NULL, 0);
321
322         /* if there's no signal to deliver, we just put the saved sigmask
323          * back */
324         if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
325                 clear_thread_flag(TIF_RESTORE_SIGMASK);
326                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
327         }
328 }
329
330 /*
331  * notification of userspace execution resumption
332  */
333 asmlinkage void do_notify_resume(struct pt_regs *regs)
334 {
335         if (test_thread_flag(TIF_SIGPENDING) || test_thread_flag(TIF_RESTORE_SIGMASK))
336                 do_signal(regs);
337
338         if (test_thread_flag(TIF_NOTIFY_RESUME)) {
339                 clear_thread_flag(TIF_NOTIFY_RESUME);
340                 tracehook_notify_resume(regs);
341                 if (current->replacement_session_keyring)
342                         key_replace_session_keyring();
343         }
344 }
345