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