[CRYPTO] api: Remove deprecated interface
[pv_ops_mirror.git] / arch / cris / arch-v32 / kernel / signal.c
blob7cd6ac80340940300defd4f75ce48be1629461e9
1 /*
2 * Copyright (C) 2003, Axis Communications AB.
3 */
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>
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>
24 extern unsigned long cris_signal_return_page;
26 /* Flag to check if a signal is blockable. */
27 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
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.
34 * This relies on that PC points to the instruction after the break call.
36 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
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. */
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. */
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);
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.
60 int
61 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
62 long srp, struct pt_regs *regs)
64 sigset_t saveset;
66 mask &= _BLOCKABLE;
68 spin_lock_irq(&current->sighand->siglock);
70 saveset = current->blocked;
72 siginitset(&current->blocked, mask);
74 recalc_sigpending();
75 spin_unlock_irq(&current->sighand->siglock);
77 regs->r10 = -EINTR;
79 while (1) {
80 current->state = TASK_INTERRUPTIBLE;
81 schedule();
83 if (do_signal(0, &saveset, regs)) {
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.
93 return regs->r10;
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)
103 sigset_t saveset;
104 sigset_t newset;
106 if (sigsetsize != sizeof(sigset_t))
107 return -EINVAL;
109 if (copy_from_user(&newset, unewset, sizeof(newset)))
110 return -EFAULT;
112 sigdelsetmask(&newset, ~_BLOCKABLE);
113 spin_lock_irq(&current->sighand->siglock);
115 saveset = current->blocked;
116 current->blocked = newset;
118 recalc_sigpending();
119 spin_unlock_irq(&current->sighand->siglock);
121 regs->r10 = -EINTR;
123 while (1) {
124 current->state = TASK_INTERRUPTIBLE;
125 schedule();
127 if (do_signal(0, &saveset, regs)) {
128 /* See comment in function above. */
129 return regs->r10;
135 sys_sigaction(int signal, const struct old_sigaction *act,
136 struct old_sigaction *oact)
138 int retval;
139 struct k_sigaction newk;
140 struct k_sigaction oldk;
142 if (act) {
143 old_sigset_t mask;
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;
150 __get_user(newk.sa.sa_flags, &act->sa_flags);
151 __get_user(mask, &act->sa_mask);
152 siginitset(&newk.sa.sa_mask, mask);
155 retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
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;
163 __put_user(oldk.sa.sa_flags, &oact->sa_flags);
164 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
167 return retval;
171 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
173 return do_sigaltstack(uss, uoss, rdusp());
176 static int
177 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
179 unsigned int err = 0;
180 unsigned long old_usp;
182 /* Always make any pending restarted system calls return -EINTR */
183 current_thread_info()->restart_block.fn = do_no_restart_syscall;
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().
190 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
191 goto badframe;
193 /* Make that the user-mode flag is set. */
194 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
196 /* Restore the old USP. */
197 err |= __get_user(old_usp, &sc->usp);
198 wrusp(old_usp);
200 return err;
202 badframe:
203 return 1;
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)
211 sigset_t set;
212 struct signal_frame __user *frame;
213 unsigned long oldspc = regs->spc;
214 unsigned long oldccs = regs->ccs;
216 frame = (struct signal_frame *) rdusp();
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.
223 if (((long)frame) & 3)
224 goto badframe;
226 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
227 goto badframe;
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;
235 sigdelsetmask(&set, ~_BLOCKABLE);
236 spin_lock_irq(&current->sighand->siglock);
238 current->blocked = set;
240 recalc_sigpending();
241 spin_unlock_irq(&current->sighand->siglock);
243 if (restore_sigcontext(regs, &frame->sc))
244 goto badframe;
246 keep_debug_flags(oldccs, oldspc, regs);
248 return regs->r10;
250 badframe:
251 force_sig(SIGSEGV, current);
252 return 0;
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)
260 sigset_t set;
261 struct rt_signal_frame __user *frame;
262 unsigned long oldspc = regs->spc;
263 unsigned long oldccs = regs->ccs;
265 frame = (struct rt_signal_frame *) rdusp();
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.
272 if (((long)frame) & 3)
273 goto badframe;
275 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
276 goto badframe;
278 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
279 goto badframe;
281 sigdelsetmask(&set, ~_BLOCKABLE);
282 spin_lock_irq(&current->sighand->siglock);
284 current->blocked = set;
286 recalc_sigpending();
287 spin_unlock_irq(&current->sighand->siglock);
289 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
290 goto badframe;
292 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
293 goto badframe;
295 keep_debug_flags(oldccs, oldspc, regs);
297 return regs->r10;
299 badframe:
300 force_sig(SIGSEGV, current);
301 return 0;
304 /* Setup a signal frame. */
305 static int
306 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
307 unsigned long mask)
309 int err;
310 unsigned long usp;
312 err = 0;
313 usp = rdusp();
316 * Copy the registers. They are located first in sc, so it's
317 * possible to use sc directly.
319 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
321 err |= __put_user(mask, &sc->oldmask);
322 err |= __put_user(usp, &sc->usp);
324 return err;
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)
331 unsigned long sp;
333 sp = rdusp();
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;
341 /* Make sure the frame is dword-aligned. */
342 sp &= ~3;
344 return (void __user *)(sp - frame_size);
347 /* Grab and setup a signal frame.
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.
354 static void
355 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
356 struct pt_regs * regs)
358 int err;
359 unsigned long return_ip;
360 struct signal_frame __user *frame;
362 err = 0;
363 frame = get_sigframe(ka, regs, sizeof(*frame));
365 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
366 goto give_sigsegv;
368 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
370 if (err)
371 goto give_sigsegv;
373 if (_NSIG_WORDS > 1) {
374 err |= __copy_to_user(frame->extramask, &set->sig[1],
375 sizeof(frame->extramask));
378 if (err)
379 goto give_sigsegv;
382 * Set up to return from user-space. If provided, use a stub
383 * already located in user-space.
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;
392 * This is movu.w __NR_sigreturn, r9; break 13;
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.
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));
403 if (err)
404 goto give_sigsegv;
407 * Set up registers for signal handler.
409 * Where the code enters now.
410 * Where the code enter later.
411 * First argument, signo.
413 regs->erp = (unsigned long) ka->sa.sa_handler;
414 regs->srp = return_ip;
415 regs->r10 = sig;
417 /* Actually move the USP to reflect the stacked frame. */
418 wrusp((unsigned long)frame);
420 return;
422 give_sigsegv:
423 if (sig == SIGSEGV)
424 ka->sa.sa_handler = SIG_DFL;
426 force_sig(SIGSEGV, current);
429 static void
430 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
431 sigset_t *set, struct pt_regs * regs)
433 int err;
434 unsigned long return_ip;
435 struct rt_signal_frame __user *frame;
437 err = 0;
438 frame = get_sigframe(ka, regs, sizeof(*frame));
440 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
441 goto give_sigsegv;
443 /* TODO: what is the current->exec_domain stuff and invmap ? */
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);
449 if (err)
450 goto give_sigsegv;
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));
457 if (err)
458 goto give_sigsegv;
461 * Set up to return from user-space. If provided, use a stub
462 * already located in user-space.
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;
471 * This is movu.w __NR_rt_sigreturn, r9; break 13;
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.
477 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
479 err |= __put_user(__NR_rt_sigreturn,
480 (short __user*)(frame->retcode+2));
482 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
485 if (err)
486 goto give_sigsegv;
489 * Set up registers for signal handler.
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.
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;
503 /* Actually move the usp to reflect the stacked frame. */
504 wrusp((unsigned long)frame);
506 return;
508 give_sigsegv:
509 if (sig == SIGSEGV)
510 ka->sa.sa_handler = SIG_DFL;
512 force_sig(SIGSEGV, current);
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)
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:
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.
535 regs->r10 = -EINTR;
536 break;
538 case -ERESTARTSYS:
540 * This means restart the syscall if
541 * there is no handler, or the handler
542 * was registered with SA_RESTART.
544 if (!(ka->sa.sa_flags & SA_RESTART)) {
545 regs->r10 = -EINTR;
546 break;
549 /* Fall through. */
551 case -ERESTARTNOINTR:
553 * This means that the syscall should
554 * be called again after the signal
555 * handler returns.
557 RESTART_CRIS_SYS(regs);
558 break;
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);
568 if (ka->sa.sa_flags & SA_ONESHOT)
569 ka->sa.sa_handler = SIG_DFL;
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);
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.
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.
591 do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
593 int signr;
594 siginfo_t info;
595 struct k_sigaction ka;
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.
602 if (!user_mode(regs))
603 return 1;
605 if (!oldset)
606 oldset = &current->blocked;
608 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
610 if (signr > 0) {
611 /* Deliver the signal. */
612 handle_signal(canrestart, signr, &info, &ka, oldset, regs);
613 return 1;
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);
625 if (regs->r10 == -ERESTART_RESTARTBLOCK){
626 regs->r10 = __NR_restart_syscall;
627 regs->erp -= 2;
631 return 0;
634 asmlinkage void
635 ugdb_trap_user(struct thread_info *ti, int sig)
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;
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;
651 sys_kill(ti->task->pid, sig);
654 void
655 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
656 struct pt_regs *regs)
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?");
666 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
667 /* Assume the SPC is valid and interesting. */
668 regs->spc = oldspc;
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));
685 /* Set up the trampolines on the signal return page. */
686 int __init
687 cris_init_signal(void)
689 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
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;
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);
704 return 0;
707 __initcall(cris_init_signal);