Pull video into test branch
[linux-drm-fsl-dcu.git] / arch / cris / arch-v32 / kernel / signal.c
1 /*
2  * Copyright (C) 2003, Axis Communications AB.
3  */
4
5 #include <linux/sched.h>
6 #include <linux/mm.h>
7 #include <linux/kernel.h>
8 #include <linux/signal.h>
9 #include <linux/errno.h>
10 #include <linux/wait.h>
11 #include <linux/ptrace.h>
12 #include <linux/unistd.h>
13 #include <linux/stddef.h>
14 #include <linux/syscalls.h>
15 #include <linux/vmalloc.h>
16
17 #include <asm/io.h>
18 #include <asm/processor.h>
19 #include <asm/ucontext.h>
20 #include <asm/uaccess.h>
21 #include <asm/arch/ptrace.h>
22 #include <asm/arch/hwregs/cpu_vect.h>
23
24 extern unsigned long cris_signal_return_page;
25
26 /* Flag to check if a signal is blockable. */
27 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
28
29 /*
30  * A syscall in CRIS is really a "break 13" instruction, which is 2
31  * bytes. The registers is manipulated so upon return the instruction
32  * will be executed again.
33  *
34  * This relies on that PC points to the instruction after the break call.
35  */
36 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
37
38 /* Signal frames. */
39 struct signal_frame {
40         struct sigcontext sc;
41         unsigned long extramask[_NSIG_WORDS - 1];
42         unsigned char retcode[8];       /* Trampoline code. */
43 };
44
45 struct rt_signal_frame {
46         struct siginfo *pinfo;
47         void *puc;
48         struct siginfo info;
49         struct ucontext uc;
50         unsigned char retcode[8];       /* Trampoline code. */
51 };
52
53 int do_signal(int restart, sigset_t *oldset, struct pt_regs *regs);
54 void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
55                       struct pt_regs *regs);
56 /*
57  * Swap in the new signal mask, and wait for a signal. Define some
58  * dummy arguments to be able to reach the regs argument.
59  */
60 int
61 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
62                long srp, struct pt_regs *regs)
63 {
64         sigset_t saveset;
65
66         mask &= _BLOCKABLE;
67
68         spin_lock_irq(&current->sighand->siglock);
69
70         saveset = current->blocked;
71
72         siginitset(&current->blocked, mask);
73
74         recalc_sigpending();
75         spin_unlock_irq(&current->sighand->siglock);
76
77         regs->r10 = -EINTR;
78
79         while (1) {
80                 current->state = TASK_INTERRUPTIBLE;
81                 schedule();
82
83                 if (do_signal(0, &saveset, regs)) {
84                         /*
85                          * This point is reached twice: once to call
86                          * the signal handler, then again to return
87                          * from the sigsuspend system call. When
88                          * calling the signal handler, R10 hold the
89                          * signal number as set by do_signal(). The
90                          * sigsuspend  call will always return with
91                          * the restored value above; -EINTR.
92                          */
93                         return regs->r10;
94                 }
95         }
96 }
97
98 /* Define some dummy arguments to be able to reach the regs argument. */
99 int
100 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, long r12, long r13,
101                   long mof, long srp, struct pt_regs *regs)
102 {
103         sigset_t saveset;
104         sigset_t newset;
105
106         if (sigsetsize != sizeof(sigset_t))
107                 return -EINVAL;
108
109         if (copy_from_user(&newset, unewset, sizeof(newset)))
110                 return -EFAULT;
111
112         sigdelsetmask(&newset, ~_BLOCKABLE);
113         spin_lock_irq(&current->sighand->siglock);
114
115         saveset = current->blocked;
116         current->blocked = newset;
117
118         recalc_sigpending();
119         spin_unlock_irq(&current->sighand->siglock);
120
121         regs->r10 = -EINTR;
122
123         while (1) {
124                 current->state = TASK_INTERRUPTIBLE;
125                 schedule();
126
127                 if (do_signal(0, &saveset, regs)) {
128                         /* See comment in function above. */
129                         return regs->r10;
130                 }
131         }
132 }
133
134 int
135 sys_sigaction(int signal, const struct old_sigaction *act,
136               struct old_sigaction *oact)
137 {
138         int retval;
139         struct k_sigaction newk;
140         struct k_sigaction oldk;
141
142         if (act) {
143                 old_sigset_t mask;
144
145                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
146                     __get_user(newk.sa.sa_handler, &act->sa_handler) ||
147                     __get_user(newk.sa.sa_restorer, &act->sa_restorer))
148                         return -EFAULT;
149
150                 __get_user(newk.sa.sa_flags, &act->sa_flags);
151                 __get_user(mask, &act->sa_mask);
152                 siginitset(&newk.sa.sa_mask, mask);
153         }
154
155         retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
156
157         if (!retval && oact) {
158                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
159                     __put_user(oldk.sa.sa_handler, &oact->sa_handler) ||
160                     __put_user(oldk.sa.sa_restorer, &oact->sa_restorer))
161                         return -EFAULT;
162
163                 __put_user(oldk.sa.sa_flags, &oact->sa_flags);
164                 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
165         }
166
167         return retval;
168 }
169
170 int
171 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
172 {
173         return do_sigaltstack(uss, uoss, rdusp());
174 }
175
176 static int
177 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
178 {
179         unsigned int err = 0;
180         unsigned long old_usp;
181
182         /* Always make any pending restarted system calls return -EINTR */
183         current_thread_info()->restart_block.fn = do_no_restart_syscall;
184
185         /*
186          * Restore the registers from &sc->regs. sc is already checked
187          * for VERIFY_READ since the signal_frame was previously
188          * checked in sys_sigreturn().
189          */
190         if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
191                 goto badframe;
192
193         /* Make that the user-mode flag is set. */
194         regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
195
196         /* Restore the old USP. */
197         err |= __get_user(old_usp, &sc->usp);
198         wrusp(old_usp);
199
200         return err;
201
202 badframe:
203         return 1;
204 }
205
206 /* Define some dummy arguments to be able to reach the regs argument. */
207 asmlinkage int
208 sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
209               struct pt_regs *regs)
210 {
211         sigset_t set;
212         struct signal_frame __user *frame;
213         unsigned long oldspc = regs->spc;
214         unsigned long oldccs = regs->ccs;
215
216         frame = (struct signal_frame *) rdusp();
217
218         /*
219          * Since the signal is stacked on a dword boundary, the frame
220          * should be dword aligned here as well. It it's not, then the
221          * user is trying some funny business.
222          */
223         if (((long)frame) & 3)
224                 goto badframe;
225
226         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
227                 goto badframe;
228
229         if (__get_user(set.sig[0], &frame->sc.oldmask) ||
230             (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
231                                                  frame->extramask,
232                                                  sizeof(frame->extramask))))
233                 goto badframe;
234
235         sigdelsetmask(&set, ~_BLOCKABLE);
236         spin_lock_irq(&current->sighand->siglock);
237
238         current->blocked = set;
239
240         recalc_sigpending();
241         spin_unlock_irq(&current->sighand->siglock);
242
243         if (restore_sigcontext(regs, &frame->sc))
244                 goto badframe;
245
246         keep_debug_flags(oldccs, oldspc, regs);
247
248         return regs->r10;
249
250 badframe:
251         force_sig(SIGSEGV, current);
252         return 0;
253 }
254
255 /* Define some dummy variables to be able to reach the regs argument. */
256 asmlinkage int
257 sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
258                  struct pt_regs *regs)
259 {
260         sigset_t set;
261         struct rt_signal_frame __user *frame;
262         unsigned long oldspc = regs->spc;
263         unsigned long oldccs = regs->ccs;
264
265         frame = (struct rt_signal_frame *) rdusp();
266
267         /*
268          * Since the signal is stacked on a dword boundary, the frame
269          * should be dword aligned here as well. It it's not, then the
270          * user is trying some funny business.
271          */
272         if (((long)frame) & 3)
273                 goto badframe;
274
275         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
276                 goto badframe;
277
278         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
279                 goto badframe;
280
281         sigdelsetmask(&set, ~_BLOCKABLE);
282         spin_lock_irq(&current->sighand->siglock);
283
284         current->blocked = set;
285
286         recalc_sigpending();
287         spin_unlock_irq(&current->sighand->siglock);
288
289         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
290                 goto badframe;
291
292         if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
293                 goto badframe;
294
295         keep_debug_flags(oldccs, oldspc, regs);
296
297         return regs->r10;
298
299 badframe:
300         force_sig(SIGSEGV, current);
301         return 0;
302 }
303
304 /* Setup a signal frame. */
305 static int
306 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
307                  unsigned long mask)
308 {
309         int err;
310         unsigned long usp;
311
312         err = 0;
313         usp = rdusp();
314
315         /*
316          * Copy the registers. They are located first in sc, so it's
317          * possible to use sc directly.
318          */
319         err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
320
321         err |= __put_user(mask, &sc->oldmask);
322         err |= __put_user(usp, &sc->usp);
323
324         return err;
325 }
326
327 /* Figure out where to put the new signal frame - usually on the stack. */
328 static inline void __user *
329 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
330 {
331         unsigned long sp;
332
333         sp = rdusp();
334
335         /* This is the X/Open sanctioned signal stack switching. */
336         if (ka->sa.sa_flags & SA_ONSTACK) {
337                 if (!on_sig_stack(sp))
338                         sp = current->sas_ss_sp + current->sas_ss_size;
339         }
340
341         /* Make sure the frame is dword-aligned. */
342         sp &= ~3;
343
344         return (void __user *)(sp - frame_size);
345 }
346
347 /* Grab and setup a signal frame.
348  *
349  * Basically a lot of state-info is stacked, and arranged for the
350  * user-mode program to return to the kernel using either a trampiline
351  * which performs the syscall sigreturn(), or a provided user-mode
352  * trampoline.
353   */
354 static void
355 setup_frame(int sig, struct k_sigaction *ka,  sigset_t *set,
356             struct pt_regs * regs)
357 {
358         int err;
359         unsigned long return_ip;
360         struct signal_frame __user *frame;
361
362         err = 0;
363         frame = get_sigframe(ka, regs, sizeof(*frame));
364
365         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
366                 goto give_sigsegv;
367
368         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
369
370         if (err)
371                 goto give_sigsegv;
372
373         if (_NSIG_WORDS > 1) {
374                 err |= __copy_to_user(frame->extramask, &set->sig[1],
375                                       sizeof(frame->extramask));
376         }
377
378         if (err)
379                 goto give_sigsegv;
380
381         /*
382          * Set up to return from user-space. If provided, use a stub
383          * already located in user-space.
384          */
385         if (ka->sa.sa_flags & SA_RESTORER) {
386                 return_ip = (unsigned long)ka->sa.sa_restorer;
387         } else {
388                 /* Trampoline - the desired return ip is in the signal return page. */
389                 return_ip = cris_signal_return_page;
390
391                 /*
392                  * This is movu.w __NR_sigreturn, r9; break 13;
393                  *
394                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
395                  * reasons and because gdb uses it as a signature to notice
396                  * signal handler stack frames.
397                  */
398                 err |= __put_user(0x9c5f,         (short __user*)(frame->retcode+0));
399                 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
400                 err |= __put_user(0xe93d,         (short __user*)(frame->retcode+4));
401         }
402
403         if (err)
404                 goto give_sigsegv;
405
406         /*
407          * Set up registers for signal handler.
408          *
409          * Where the code enters now.
410          * Where the code enter later.
411          * First argument, signo.
412          */
413         regs->erp = (unsigned long) ka->sa.sa_handler;
414         regs->srp = return_ip;
415         regs->r10 = sig;
416
417         /* Actually move the USP to reflect the stacked frame. */
418         wrusp((unsigned long)frame);
419
420         return;
421
422 give_sigsegv:
423         if (sig == SIGSEGV)
424                 ka->sa.sa_handler = SIG_DFL;
425
426         force_sig(SIGSEGV, current);
427 }
428
429 static void
430 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
431                sigset_t *set, struct pt_regs * regs)
432 {
433         int err;
434         unsigned long return_ip;
435         struct rt_signal_frame __user *frame;
436
437         err = 0;
438         frame = get_sigframe(ka, regs, sizeof(*frame));
439
440         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
441                 goto give_sigsegv;
442
443         /* TODO: what is the current->exec_domain stuff and invmap ? */
444
445         err |= __put_user(&frame->info, &frame->pinfo);
446         err |= __put_user(&frame->uc, &frame->puc);
447         err |= copy_siginfo_to_user(&frame->info, info);
448
449         if (err)
450                 goto give_sigsegv;
451
452         /* Clear all the bits of the ucontext we don't use.  */
453         err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
454         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
455         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
456
457         if (err)
458                 goto give_sigsegv;
459
460         /*
461          * Set up to return from user-space. If provided, use a stub
462          * already located in user-space.
463          */
464         if (ka->sa.sa_flags & SA_RESTORER) {
465                 return_ip = (unsigned long) ka->sa.sa_restorer;
466         } else {
467                 /* Trampoline - the desired return ip is in the signal return page. */
468                 return_ip = cris_signal_return_page + 6;
469
470                 /*
471                  * This is movu.w __NR_rt_sigreturn, r9; break 13;
472                  *
473                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
474                  * reasons and because gdb uses it as a signature to notice
475                  * signal handler stack frames.
476                  */
477                 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
478
479                 err |= __put_user(__NR_rt_sigreturn,
480                                   (short __user*)(frame->retcode+2));
481
482                 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
483         }
484
485         if (err)
486                 goto give_sigsegv;
487
488         /*
489          * Set up registers for signal handler.
490          *
491          * Where the code enters now.
492          * Where the code enters later.
493          * First argument is signo.
494          * Second argument is (siginfo_t *).
495          * Third argument is unused.
496          */
497         regs->erp = (unsigned long) ka->sa.sa_handler;
498         regs->srp = return_ip;
499         regs->r10 = sig;
500         regs->r11 = (unsigned long) &frame->info;
501         regs->r12 = 0;
502
503         /* Actually move the usp to reflect the stacked frame. */
504         wrusp((unsigned long)frame);
505
506         return;
507
508 give_sigsegv:
509         if (sig == SIGSEGV)
510                 ka->sa.sa_handler = SIG_DFL;
511
512         force_sig(SIGSEGV, current);
513 }
514
515 /* Invoke a singal handler to, well, handle the signal. */
516 static inline void
517 handle_signal(int canrestart, unsigned long sig,
518               siginfo_t *info, struct k_sigaction *ka,
519               sigset_t *oldset, struct pt_regs * regs)
520 {
521         /* Check if this got called from a system call. */
522         if (canrestart) {
523                 /* If so, check system call restarting. */
524                 switch (regs->r10) {
525                         case -ERESTART_RESTARTBLOCK:
526                         case -ERESTARTNOHAND:
527                                 /*
528                                  * This means that the syscall should
529                                  * only be restarted if there was no
530                                  * handler for the signal, and since
531                                  * this point isn't reached unless
532                                  * there is a handler, there's no need
533                                  * to restart.
534                                  */
535                                 regs->r10 = -EINTR;
536                                 break;
537
538                         case -ERESTARTSYS:
539                                 /*
540                                  * This means restart the syscall if
541                                  * there is no handler, or the handler
542                                  * was registered with SA_RESTART.
543                                  */
544                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
545                                         regs->r10 = -EINTR;
546                                         break;
547                                 }
548
549                                 /* Fall through. */
550
551                         case -ERESTARTNOINTR:
552                                 /*
553                                  * This means that the syscall should
554                                  * be called again after the signal
555                                  * handler returns.
556                                  */
557                                 RESTART_CRIS_SYS(regs);
558                                 break;
559                 }
560         }
561
562         /* Set up the stack frame. */
563         if (ka->sa.sa_flags & SA_SIGINFO)
564                 setup_rt_frame(sig, ka, info, oldset, regs);
565         else
566                 setup_frame(sig, ka, oldset, regs);
567
568         if (ka->sa.sa_flags & SA_ONESHOT)
569                 ka->sa.sa_handler = SIG_DFL;
570
571         spin_lock_irq(&current->sighand->siglock);
572         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
573         if (!(ka->sa.sa_flags & SA_NODEFER))
574                 sigaddset(&current->blocked,sig);
575         recalc_sigpending();
576         spin_unlock_irq(&current->sighand->siglock);
577 }
578
579 /*
580  * Note that 'init' is a special process: it doesn't get signals it doesn't
581  * want to handle. Thus you cannot kill init even with a SIGKILL even by
582  * mistake.
583  *
584  * Also note that the regs structure given here as an argument, is the latest
585  * pushed pt_regs. It may or may not be the same as the first pushed registers
586  * when the initial usermode->kernelmode transition took place. Therefore
587  * we can use user_mode(regs) to see if we came directly from kernel or user
588  * mode below.
589  */
590 int
591 do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
592 {
593         int signr;
594         siginfo_t info;
595         struct k_sigaction ka;
596
597         /*
598          * The common case should go fast, which is why this point is
599          * reached from kernel-mode. If that's the case, just return
600          * without doing anything.
601          */
602         if (!user_mode(regs))
603                 return 1;
604
605         if (!oldset)
606                 oldset = &current->blocked;
607
608         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
609
610         if (signr > 0) {
611                 /* Deliver the signal. */
612                 handle_signal(canrestart, signr, &info, &ka, oldset, regs);
613                 return 1;
614         }
615
616         /* Got here from a system call? */
617         if (canrestart) {
618                 /* Restart the system call - no handlers present. */
619                 if (regs->r10 == -ERESTARTNOHAND ||
620                     regs->r10 == -ERESTARTSYS ||
621                     regs->r10 == -ERESTARTNOINTR) {
622                         RESTART_CRIS_SYS(regs);
623                 }
624
625                 if (regs->r10 == -ERESTART_RESTARTBLOCK){
626                         regs->r10 = __NR_restart_syscall;
627                         regs->erp -= 2;
628                 }
629         }
630
631         return 0;
632 }
633
634 asmlinkage void
635 ugdb_trap_user(struct thread_info *ti, int sig)
636 {
637         if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
638                 /* Zero single-step PC if the reason we stopped wasn't a single
639                    step exception. This is to avoid relying on it when it isn't
640                    reliable. */
641                 user_regs(ti)->spc = 0;
642         }
643         /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
644            not withing any configured h/w breakpoint range). Synchronize with
645            what already exists for kernel debugging.  */
646         if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
647                 /* Break 8: subtract 2 from ERP unless in a delay slot. */
648                 if (!(user_regs(ti)->erp & 0x1))
649                         user_regs(ti)->erp -= 2;
650         }
651         sys_kill(ti->task->pid, sig);
652 }
653
654 void
655 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
656                  struct pt_regs *regs)
657 {
658         if (oldccs & (1 << Q_CCS_BITNR)) {
659                 /* Pending single step due to single-stepping the break 13
660                    in the signal trampoline: keep the Q flag. */
661                 regs->ccs |= (1 << Q_CCS_BITNR);
662                 /* S flag should be set - complain if it's not. */
663                 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
664                         printk("Q flag but no S flag?");
665                 }
666                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
667                 /* Assume the SPC is valid and interesting. */
668                 regs->spc = oldspc;
669
670         } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
671                 /* If a h/w bp was set in the signal handler we need
672                    to keep the S flag. */
673                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
674                 /* Don't keep the old SPC though; if we got here due to
675                    a single-step, the Q flag should have been set. */
676         } else if (regs->spc) {
677                 /* If we were single-stepping *before* the signal was taken,
678                    we don't want to restore that state now, because GDB will
679                    have forgotten all about it. */
680                 regs->spc = 0;
681                 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
682         }
683 }
684
685 /* Set up the trampolines on the signal return page. */
686 int __init
687 cris_init_signal(void)
688 {
689         u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
690
691         /* This is movu.w __NR_sigreturn, r9; break 13; */
692         data[0] = 0x9c5f;
693         data[1] = __NR_sigreturn;
694         data[2] = 0xe93d;
695         /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
696         data[3] = 0x9c5f;
697         data[4] = __NR_rt_sigreturn;
698         data[5] = 0xe93d;
699
700         /* Map to userspace with appropriate permissions (no write access...) */
701         cris_signal_return_page = (unsigned long)
702           __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
703
704         return 0;
705 }
706
707 __initcall(cris_init_signal);