Input: wm97xx: add new AC97 bus support
[sfrench/cifs-2.6.git] / arch / cris / arch-v10 / kernel / signal.c
1 /*
2  *  linux/arch/cris/kernel/signal.c
3  *
4  *  Based on arch/i386/kernel/signal.c by
5  *     Copyright (C) 1991, 1992  Linus Torvalds
6  *     1997-11-28  Modified for POSIX.1b signals by Richard Henderson *
7  *
8  *  Ideas also taken from arch/arm.
9  *
10  *  Copyright (C) 2000-2007 Axis Communications AB
11  *
12  *  Authors:  Bjorn Wesen (bjornw@axis.com)
13  *
14  */
15
16 #include <linux/sched.h>
17 #include <linux/sched/task_stack.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27
28 #include <asm/processor.h>
29 #include <asm/ucontext.h>
30 #include <linux/uaccess.h>
31 #include <arch/system.h>
32
33 #define DEBUG_SIG 0
34
35 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
36 /* manipulate regs so that upon return, it will be re-executed */
37
38 /* We rely on that pc points to the instruction after "break 13", so the
39  * library must never do strange things like putting it in a delay slot.
40  */
41 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
42
43 void do_signal(int canrestart, struct pt_regs *regs);
44
45 /*
46  * Do a signal return; undo the signal stack.
47  */
48
49 struct sigframe {
50         struct sigcontext sc;
51         unsigned long extramask[_NSIG_WORDS-1];
52         unsigned char retcode[8];  /* trampoline code */
53 };
54
55 struct rt_sigframe {
56         struct siginfo *pinfo;
57         void *puc;
58         struct siginfo info;
59         struct ucontext uc;
60         unsigned char retcode[8];  /* trampoline code */
61 };
62
63
64 static int
65 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
66 {
67         unsigned int err = 0;
68         unsigned long old_usp;
69
70         /* Always make any pending restarted system calls return -EINTR */
71         current->restart_block.fn = do_no_restart_syscall;
72
73         /* restore the regs from &sc->regs (same as sc, since regs is first)
74          * (sc is already checked for VERIFY_READ since the sigframe was
75          *  checked in sys_sigreturn previously)
76          */
77
78         if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
79                 goto badframe;
80
81         /* make sure the U-flag is set so user-mode cannot fool us */
82
83         regs->dccr |= 1 << 8;
84
85         /* restore the old USP as it was before we stacked the sc etc.
86          * (we cannot just pop the sigcontext since we aligned the sp and
87          *  stuff after pushing it)
88          */
89
90         err |= __get_user(old_usp, &sc->usp);
91
92         wrusp(old_usp);
93
94         /* TODO: the other ports use regs->orig_XX to disable syscall checks
95          * after this completes, but we don't use that mechanism. maybe we can
96          * use it now ?
97          */
98
99         return err;
100
101 badframe:
102         return 1;
103 }
104
105 asmlinkage int sys_sigreturn(void)
106 {
107         struct pt_regs *regs = current_pt_regs();
108         struct sigframe __user *frame = (struct sigframe *)rdusp();
109         sigset_t set;
110
111         /*
112          * Since we stacked the signal on a dword boundary,
113          * then frame should be dword aligned here.  If it's
114          * not, then the user is trying to mess with us.
115          */
116         if (((long)frame) & 3)
117                 goto badframe;
118
119         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
120                 goto badframe;
121         if (__get_user(set.sig[0], &frame->sc.oldmask)
122             || (_NSIG_WORDS > 1
123                 && __copy_from_user(&set.sig[1], frame->extramask,
124                                     sizeof(frame->extramask))))
125                 goto badframe;
126
127         set_current_blocked(&set);
128
129         if (restore_sigcontext(regs, &frame->sc))
130                 goto badframe;
131
132         /* TODO: SIGTRAP when single-stepping as in arm ? */
133
134         return regs->r10;
135
136 badframe:
137         force_sig(SIGSEGV, current);
138         return 0;
139 }
140
141 asmlinkage int sys_rt_sigreturn(void)
142 {
143         struct pt_regs *regs = current_pt_regs();
144         struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
145         sigset_t set;
146
147         /*
148          * Since we stacked the signal on a dword boundary,
149          * then frame should be dword aligned here.  If it's
150          * not, then the user is trying to mess with us.
151          */
152         if (((long)frame) & 3)
153                 goto badframe;
154
155         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
156                 goto badframe;
157         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
158                 goto badframe;
159
160         set_current_blocked(&set);
161
162         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
163                 goto badframe;
164
165         if (restore_altstack(&frame->uc.uc_stack))
166                 goto badframe;
167
168         return regs->r10;
169
170 badframe:
171         force_sig(SIGSEGV, current);
172         return 0;
173 }
174
175 /*
176  * Set up a signal frame.
177  */
178
179 static int setup_sigcontext(struct sigcontext __user *sc,
180         struct pt_regs *regs, unsigned long mask)
181 {
182         int err = 0;
183         unsigned long usp = rdusp();
184
185         /* copy the regs. they are first in sc so we can use sc directly */
186
187         err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
188
189         /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
190            the signal handler. The frametype will be restored to its previous
191            value in restore_sigcontext. */
192         regs->frametype = CRIS_FRAME_NORMAL;
193
194         /* then some other stuff */
195
196         err |= __put_user(mask, &sc->oldmask);
197
198         err |= __put_user(usp, &sc->usp);
199
200         return err;
201 }
202
203 /* Figure out where we want to put the new signal frame
204  * - usually on the stack. */
205
206 static inline void __user *
207 get_sigframe(struct ksignal *ksig, size_t frame_size)
208 {
209         unsigned long sp = sigsp(rdusp(), ksig);
210
211         /* make sure the frame is dword-aligned */
212
213         sp &= ~3;
214
215         return (void __user*)(sp - frame_size);
216 }
217
218 /* grab and setup a signal frame.
219  *
220  * basically we stack a lot of state info, and arrange for the
221  * user-mode program to return to the kernel using either a
222  * trampoline which performs the syscall sigreturn, or a provided
223  * user-mode trampoline.
224  */
225
226 static int setup_frame(struct ksignal *ksig, sigset_t *set,
227                        struct pt_regs *regs)
228 {
229         struct sigframe __user *frame;
230         unsigned long return_ip;
231         int err = 0;
232
233         frame = get_sigframe(ksig, sizeof(*frame));
234
235         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
236                 return -EFAULT;
237
238         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
239         if (err)
240                 return -EFAULT;
241
242         if (_NSIG_WORDS > 1) {
243                 err |= __copy_to_user(frame->extramask, &set->sig[1],
244                                       sizeof(frame->extramask));
245         }
246         if (err)
247                 return -EFAULT;
248
249         /* Set up to return from userspace.  If provided, use a stub
250            already in userspace.  */
251         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
252                 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
253         } else {
254                 /* trampoline - the desired return ip is the retcode itself */
255                 return_ip = (unsigned long)&frame->retcode;
256                 /* This is movu.w __NR_sigreturn, r9; break 13; */
257                 err |= __put_user(0x9c5f,         (short __user*)(frame->retcode+0));
258                 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
259                 err |= __put_user(0xe93d,         (short __user*)(frame->retcode+4));
260         }
261
262         if (err)
263                 return -EFAULT;
264
265         /* Set up registers for signal handler */
266
267         regs->irp = (unsigned long) ksig->ka.sa.sa_handler;  /* what we enter NOW   */
268         regs->srp = return_ip;                          /* what we enter LATER */
269         regs->r10 = ksig->sig;                                /* first argument is signo */
270
271         /* actually move the usp to reflect the stacked frame */
272
273         wrusp((unsigned long)frame);
274
275         return 0;
276 }
277
278 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
279                           struct pt_regs *regs)
280 {
281         struct rt_sigframe __user *frame;
282         unsigned long return_ip;
283         int err = 0;
284
285         frame = get_sigframe(ksig, sizeof(*frame));
286
287         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288                 return -EFAULT;
289
290         err |= __put_user(&frame->info, &frame->pinfo);
291         err |= __put_user(&frame->uc, &frame->puc);
292         err |= copy_siginfo_to_user(&frame->info, &ksig->info);
293         if (err)
294                 return -EFAULT;
295
296         /* Clear all the bits of the ucontext we don't use.  */
297         err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
298
299         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
300
301         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
302
303         err |= __save_altstack(&frame->uc.uc_stack, rdusp());
304
305         if (err)
306                 return -EFAULT;
307
308         /* Set up to return from userspace.  If provided, use a stub
309            already in userspace.  */
310         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
311                 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
312         } else {
313                 /* trampoline - the desired return ip is the retcode itself */
314                 return_ip = (unsigned long)&frame->retcode;
315                 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
316                 err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
317                 err |= __put_user(__NR_rt_sigreturn,
318                         (short __user *)(frame->retcode+2));
319                 err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
320         }
321
322         if (err)
323                 return -EFAULT;
324
325         /* Set up registers for signal handler */
326
327         /* What we enter NOW   */
328         regs->irp = (unsigned long) ksig->ka.sa.sa_handler;
329         /* What we enter LATER */
330         regs->srp = return_ip;
331         /* First argument is signo */
332         regs->r10 = ksig->sig;
333         /* Second argument is (siginfo_t *) */
334         regs->r11 = (unsigned long)&frame->info;
335         /* Third argument is unused */
336         regs->r12 = 0;
337
338         /* Actually move the usp to reflect the stacked frame */
339         wrusp((unsigned long)frame);
340
341         return 0;
342 }
343
344 /*
345  * OK, we're invoking a handler
346  */
347
348 static inline void handle_signal(int canrestart, struct ksignal *ksig,
349                                  struct pt_regs *regs)
350 {
351         sigset_t *oldset = sigmask_to_save();
352         int ret;
353
354         /* Are we from a system call? */
355         if (canrestart) {
356                 /* If so, check system call restarting.. */
357                 switch (regs->r10) {
358                 case -ERESTART_RESTARTBLOCK:
359                 case -ERESTARTNOHAND:
360                         /* ERESTARTNOHAND means that the syscall should
361                          * only be restarted if there was no handler for
362                          * the signal, and since we only get here if there
363                          * is a handler, we don't restart */
364                         regs->r10 = -EINTR;
365                         break;
366                 case -ERESTARTSYS:
367                         /* ERESTARTSYS means to restart the syscall if
368                          * there is no handler or the handler was
369                          * registered with SA_RESTART */
370                         if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
371                                 regs->r10 = -EINTR;
372                                 break;
373                         }
374                 /* fallthrough */
375                 case -ERESTARTNOINTR:
376                         /* ERESTARTNOINTR means that the syscall should
377                          * be called again after the signal handler returns. */
378                         RESTART_CRIS_SYS(regs);
379                 }
380         }
381
382         /* Set up the stack frame */
383         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
384                 ret = setup_rt_frame(ksig, oldset, regs);
385         else
386                 ret = setup_frame(ksig, oldset, regs);
387
388         signal_setup_done(ret, ksig, 0);
389 }
390
391 /*
392  * Note that 'init' is a special process: it doesn't get signals it doesn't
393  * want to handle. Thus you cannot kill init even with a SIGKILL even by
394  * mistake.
395  *
396  * Also note that the regs structure given here as an argument, is the latest
397  * pushed pt_regs. It may or may not be the same as the first pushed registers
398  * when the initial usermode->kernelmode transition took place. Therefore
399  * we can use user_mode(regs) to see if we came directly from kernel or user
400  * mode below.
401  */
402
403 void do_signal(int canrestart, struct pt_regs *regs)
404 {
405         struct ksignal ksig;
406
407         /*
408          * We want the common case to go fast, which
409          * is why we may in certain cases get here from
410          * kernel mode. Just return without doing anything
411          * if so.
412          */
413         if (!user_mode(regs))
414                 return;
415
416         if (get_signal(&ksig)) {
417                 /* Whee!  Actually deliver the signal.  */
418                 handle_signal(canrestart, &ksig, regs);
419                 return;
420         }
421
422         /* Did we come from a system call? */
423         if (canrestart) {
424                 /* Restart the system call - no handlers present */
425                 if (regs->r10 == -ERESTARTNOHAND ||
426                     regs->r10 == -ERESTARTSYS ||
427                     regs->r10 == -ERESTARTNOINTR) {
428                         RESTART_CRIS_SYS(regs);
429                 }
430                 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
431                         regs->r9 = __NR_restart_syscall;
432                         regs->irp -= 2;
433                 }
434         }
435
436         /* if there's no signal to deliver, we just put the saved sigmask
437          * back */
438         restore_saved_sigmask();
439 }