I2C: TWL4030: Kconfig and Makefile changes
[linux-ginger.git] / arch / cris / arch-v32 / kernel / signal.c
blob58c1866804e31974fdef993c44d7a34cb2f37aae
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 void do_signal(int restart, 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 mask &= _BLOCKABLE;
65 spin_lock_irq(&current->sighand->siglock);
66 current->saved_sigmask = current->blocked;
67 siginitset(&current->blocked, mask);
68 recalc_sigpending();
69 spin_unlock_irq(&current->sighand->siglock);
70 current->state = TASK_INTERRUPTIBLE;
71 schedule();
72 set_thread_flag(TIF_RESTORE_SIGMASK);
73 return -ERESTARTNOHAND;
76 int
77 sys_sigaction(int signal, const struct old_sigaction *act,
78 struct old_sigaction *oact)
80 int retval;
81 struct k_sigaction newk;
82 struct k_sigaction oldk;
84 if (act) {
85 old_sigset_t mask;
87 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
88 __get_user(newk.sa.sa_handler, &act->sa_handler) ||
89 __get_user(newk.sa.sa_restorer, &act->sa_restorer))
90 return -EFAULT;
92 __get_user(newk.sa.sa_flags, &act->sa_flags);
93 __get_user(mask, &act->sa_mask);
94 siginitset(&newk.sa.sa_mask, mask);
97 retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
99 if (!retval && oact) {
100 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
101 __put_user(oldk.sa.sa_handler, &oact->sa_handler) ||
102 __put_user(oldk.sa.sa_restorer, &oact->sa_restorer))
103 return -EFAULT;
105 __put_user(oldk.sa.sa_flags, &oact->sa_flags);
106 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
109 return retval;
113 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
115 return do_sigaltstack(uss, uoss, rdusp());
118 static int
119 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
121 unsigned int err = 0;
122 unsigned long old_usp;
124 /* Always make any pending restarted system calls return -EINTR */
125 current_thread_info()->restart_block.fn = do_no_restart_syscall;
128 * Restore the registers from &sc->regs. sc is already checked
129 * for VERIFY_READ since the signal_frame was previously
130 * checked in sys_sigreturn().
132 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
133 goto badframe;
135 /* Make that the user-mode flag is set. */
136 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
138 /* Restore the old USP. */
139 err |= __get_user(old_usp, &sc->usp);
140 wrusp(old_usp);
142 return err;
144 badframe:
145 return 1;
148 /* Define some dummy arguments to be able to reach the regs argument. */
149 asmlinkage int
150 sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
151 struct pt_regs *regs)
153 sigset_t set;
154 struct signal_frame __user *frame;
155 unsigned long oldspc = regs->spc;
156 unsigned long oldccs = regs->ccs;
158 frame = (struct signal_frame *) rdusp();
161 * Since the signal is stacked on a dword boundary, the frame
162 * should be dword aligned here as well. It it's not, then the
163 * user is trying some funny business.
165 if (((long)frame) & 3)
166 goto badframe;
168 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
169 goto badframe;
171 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
172 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
173 frame->extramask,
174 sizeof(frame->extramask))))
175 goto badframe;
177 sigdelsetmask(&set, ~_BLOCKABLE);
178 spin_lock_irq(&current->sighand->siglock);
180 current->blocked = set;
182 recalc_sigpending();
183 spin_unlock_irq(&current->sighand->siglock);
185 if (restore_sigcontext(regs, &frame->sc))
186 goto badframe;
188 keep_debug_flags(oldccs, oldspc, regs);
190 return regs->r10;
192 badframe:
193 force_sig(SIGSEGV, current);
194 return 0;
197 /* Define some dummy variables to be able to reach the regs argument. */
198 asmlinkage int
199 sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
200 struct pt_regs *regs)
202 sigset_t set;
203 struct rt_signal_frame __user *frame;
204 unsigned long oldspc = regs->spc;
205 unsigned long oldccs = regs->ccs;
207 frame = (struct rt_signal_frame *) rdusp();
210 * Since the signal is stacked on a dword boundary, the frame
211 * should be dword aligned here as well. It it's not, then the
212 * user is trying some funny business.
214 if (((long)frame) & 3)
215 goto badframe;
217 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
218 goto badframe;
220 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
221 goto badframe;
223 sigdelsetmask(&set, ~_BLOCKABLE);
224 spin_lock_irq(&current->sighand->siglock);
226 current->blocked = set;
228 recalc_sigpending();
229 spin_unlock_irq(&current->sighand->siglock);
231 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
232 goto badframe;
234 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
235 goto badframe;
237 keep_debug_flags(oldccs, oldspc, regs);
239 return regs->r10;
241 badframe:
242 force_sig(SIGSEGV, current);
243 return 0;
246 /* Setup a signal frame. */
247 static int
248 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
249 unsigned long mask)
251 int err;
252 unsigned long usp;
254 err = 0;
255 usp = rdusp();
258 * Copy the registers. They are located first in sc, so it's
259 * possible to use sc directly.
261 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
263 err |= __put_user(mask, &sc->oldmask);
264 err |= __put_user(usp, &sc->usp);
266 return err;
269 /* Figure out where to put the new signal frame - usually on the stack. */
270 static inline void __user *
271 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
273 unsigned long sp;
275 sp = rdusp();
277 /* This is the X/Open sanctioned signal stack switching. */
278 if (ka->sa.sa_flags & SA_ONSTACK) {
279 if (!on_sig_stack(sp))
280 sp = current->sas_ss_sp + current->sas_ss_size;
283 /* Make sure the frame is dword-aligned. */
284 sp &= ~3;
286 return (void __user *)(sp - frame_size);
289 /* Grab and setup a signal frame.
291 * Basically a lot of state-info is stacked, and arranged for the
292 * user-mode program to return to the kernel using either a trampiline
293 * which performs the syscall sigreturn(), or a provided user-mode
294 * trampoline.
296 static int
297 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
298 struct pt_regs * regs)
300 int err;
301 unsigned long return_ip;
302 struct signal_frame __user *frame;
304 err = 0;
305 frame = get_sigframe(ka, regs, sizeof(*frame));
307 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
308 goto give_sigsegv;
310 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
312 if (err)
313 goto give_sigsegv;
315 if (_NSIG_WORDS > 1) {
316 err |= __copy_to_user(frame->extramask, &set->sig[1],
317 sizeof(frame->extramask));
320 if (err)
321 goto give_sigsegv;
324 * Set up to return from user-space. If provided, use a stub
325 * already located in user-space.
327 if (ka->sa.sa_flags & SA_RESTORER) {
328 return_ip = (unsigned long)ka->sa.sa_restorer;
329 } else {
330 /* Trampoline - the desired return ip is in the signal return page. */
331 return_ip = cris_signal_return_page;
334 * This is movu.w __NR_sigreturn, r9; break 13;
336 * WE DO NOT USE IT ANY MORE! It's only left here for historical
337 * reasons and because gdb uses it as a signature to notice
338 * signal handler stack frames.
340 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
341 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
342 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
345 if (err)
346 goto give_sigsegv;
349 * Set up registers for signal handler.
351 * Where the code enters now.
352 * Where the code enter later.
353 * First argument, signo.
355 regs->erp = (unsigned long) ka->sa.sa_handler;
356 regs->srp = return_ip;
357 regs->r10 = sig;
359 /* Actually move the USP to reflect the stacked frame. */
360 wrusp((unsigned long)frame);
362 return 0;
364 give_sigsegv:
365 if (sig == SIGSEGV)
366 ka->sa.sa_handler = SIG_DFL;
368 force_sig(SIGSEGV, current);
369 return -EFAULT;
372 static int
373 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
374 sigset_t *set, struct pt_regs * regs)
376 int err;
377 unsigned long return_ip;
378 struct rt_signal_frame __user *frame;
380 err = 0;
381 frame = get_sigframe(ka, regs, sizeof(*frame));
383 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
384 goto give_sigsegv;
386 /* TODO: what is the current->exec_domain stuff and invmap ? */
388 err |= __put_user(&frame->info, &frame->pinfo);
389 err |= __put_user(&frame->uc, &frame->puc);
390 err |= copy_siginfo_to_user(&frame->info, info);
392 if (err)
393 goto give_sigsegv;
395 /* Clear all the bits of the ucontext we don't use. */
396 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
397 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
398 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
400 if (err)
401 goto give_sigsegv;
404 * Set up to return from user-space. If provided, use a stub
405 * already located in user-space.
407 if (ka->sa.sa_flags & SA_RESTORER) {
408 return_ip = (unsigned long) ka->sa.sa_restorer;
409 } else {
410 /* Trampoline - the desired return ip is in the signal return page. */
411 return_ip = cris_signal_return_page + 6;
414 * This is movu.w __NR_rt_sigreturn, r9; break 13;
416 * WE DO NOT USE IT ANY MORE! It's only left here for historical
417 * reasons and because gdb uses it as a signature to notice
418 * signal handler stack frames.
420 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
422 err |= __put_user(__NR_rt_sigreturn,
423 (short __user*)(frame->retcode+2));
425 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
428 if (err)
429 goto give_sigsegv;
432 * Set up registers for signal handler.
434 * Where the code enters now.
435 * Where the code enters later.
436 * First argument is signo.
437 * Second argument is (siginfo_t *).
438 * Third argument is unused.
440 regs->erp = (unsigned long) ka->sa.sa_handler;
441 regs->srp = return_ip;
442 regs->r10 = sig;
443 regs->r11 = (unsigned long) &frame->info;
444 regs->r12 = 0;
446 /* Actually move the usp to reflect the stacked frame. */
447 wrusp((unsigned long)frame);
449 return 0;
451 give_sigsegv:
452 if (sig == SIGSEGV)
453 ka->sa.sa_handler = SIG_DFL;
455 force_sig(SIGSEGV, current);
456 return -EFAULT;
459 /* Invoke a singal handler to, well, handle the signal. */
460 static inline int
461 handle_signal(int canrestart, unsigned long sig,
462 siginfo_t *info, struct k_sigaction *ka,
463 sigset_t *oldset, struct pt_regs * regs)
465 int ret;
467 /* Check if this got called from a system call. */
468 if (canrestart) {
469 /* If so, check system call restarting. */
470 switch (regs->r10) {
471 case -ERESTART_RESTARTBLOCK:
472 case -ERESTARTNOHAND:
474 * This means that the syscall should
475 * only be restarted if there was no
476 * handler for the signal, and since
477 * this point isn't reached unless
478 * there is a handler, there's no need
479 * to restart.
481 regs->r10 = -EINTR;
482 break;
484 case -ERESTARTSYS:
486 * This means restart the syscall if
487 * there is no handler, or the handler
488 * was registered with SA_RESTART.
490 if (!(ka->sa.sa_flags & SA_RESTART)) {
491 regs->r10 = -EINTR;
492 break;
495 /* Fall through. */
497 case -ERESTARTNOINTR:
499 * This means that the syscall should
500 * be called again after the signal
501 * handler returns.
503 RESTART_CRIS_SYS(regs);
504 break;
508 /* Set up the stack frame. */
509 if (ka->sa.sa_flags & SA_SIGINFO)
510 ret = setup_rt_frame(sig, ka, info, oldset, regs);
511 else
512 ret = setup_frame(sig, ka, oldset, regs);
514 if (ka->sa.sa_flags & SA_ONESHOT)
515 ka->sa.sa_handler = SIG_DFL;
517 if (ret == 0) {
518 spin_lock_irq(&current->sighand->siglock);
519 sigorsets(&current->blocked, &current->blocked,
520 &ka->sa.sa_mask);
521 if (!(ka->sa.sa_flags & SA_NODEFER))
522 sigaddset(&current->blocked, sig);
523 recalc_sigpending();
524 spin_unlock_irq(&current->sighand->siglock);
527 return ret;
531 * Note that 'init' is a special process: it doesn't get signals it doesn't
532 * want to handle. Thus you cannot kill init even with a SIGKILL even by
533 * mistake.
535 * Also note that the regs structure given here as an argument, is the latest
536 * pushed pt_regs. It may or may not be the same as the first pushed registers
537 * when the initial usermode->kernelmode transition took place. Therefore
538 * we can use user_mode(regs) to see if we came directly from kernel or user
539 * mode below.
541 void
542 do_signal(int canrestart, struct pt_regs *regs)
544 int signr;
545 siginfo_t info;
546 struct k_sigaction ka;
547 sigset_t *oldset;
550 * The common case should go fast, which is why this point is
551 * reached from kernel-mode. If that's the case, just return
552 * without doing anything.
554 if (!user_mode(regs))
555 return;
557 if (test_thread_flag(TIF_RESTORE_SIGMASK))
558 oldset = &current->saved_sigmask;
559 else
560 oldset = &current->blocked;
562 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
564 if (signr > 0) {
565 /* Whee! Actually deliver the signal. */
566 if (handle_signal(canrestart, signr, &info, &ka,
567 oldset, regs)) {
568 /* a signal was successfully delivered; the saved
569 * sigmask will have been stored in the signal frame,
570 * and will be restored by sigreturn, so we can simply
571 * clear the TIF_RESTORE_SIGMASK flag */
572 if (test_thread_flag(TIF_RESTORE_SIGMASK))
573 clear_thread_flag(TIF_RESTORE_SIGMASK);
576 return;
579 /* Got here from a system call? */
580 if (canrestart) {
581 /* Restart the system call - no handlers present. */
582 if (regs->r10 == -ERESTARTNOHAND ||
583 regs->r10 == -ERESTARTSYS ||
584 regs->r10 == -ERESTARTNOINTR) {
585 RESTART_CRIS_SYS(regs);
588 if (regs->r10 == -ERESTART_RESTARTBLOCK){
589 regs->r10 = __NR_restart_syscall;
590 regs->erp -= 2;
594 /* if there's no signal to deliver, we just put the saved sigmask
595 * back */
596 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
597 clear_thread_flag(TIF_RESTORE_SIGMASK);
598 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
602 asmlinkage void
603 ugdb_trap_user(struct thread_info *ti, int sig)
605 if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
606 /* Zero single-step PC if the reason we stopped wasn't a single
607 step exception. This is to avoid relying on it when it isn't
608 reliable. */
609 user_regs(ti)->spc = 0;
611 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
612 not withing any configured h/w breakpoint range). Synchronize with
613 what already exists for kernel debugging. */
614 if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
615 /* Break 8: subtract 2 from ERP unless in a delay slot. */
616 if (!(user_regs(ti)->erp & 0x1))
617 user_regs(ti)->erp -= 2;
619 sys_kill(ti->task->pid, sig);
622 void
623 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
624 struct pt_regs *regs)
626 if (oldccs & (1 << Q_CCS_BITNR)) {
627 /* Pending single step due to single-stepping the break 13
628 in the signal trampoline: keep the Q flag. */
629 regs->ccs |= (1 << Q_CCS_BITNR);
630 /* S flag should be set - complain if it's not. */
631 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
632 printk("Q flag but no S flag?");
634 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
635 /* Assume the SPC is valid and interesting. */
636 regs->spc = oldspc;
638 } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
639 /* If a h/w bp was set in the signal handler we need
640 to keep the S flag. */
641 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
642 /* Don't keep the old SPC though; if we got here due to
643 a single-step, the Q flag should have been set. */
644 } else if (regs->spc) {
645 /* If we were single-stepping *before* the signal was taken,
646 we don't want to restore that state now, because GDB will
647 have forgotten all about it. */
648 regs->spc = 0;
649 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
653 /* Set up the trampolines on the signal return page. */
654 int __init
655 cris_init_signal(void)
657 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
659 /* This is movu.w __NR_sigreturn, r9; break 13; */
660 data[0] = 0x9c5f;
661 data[1] = __NR_sigreturn;
662 data[2] = 0xe93d;
663 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
664 data[3] = 0x9c5f;
665 data[4] = __NR_rt_sigreturn;
666 data[5] = 0xe93d;
668 /* Map to userspace with appropriate permissions (no write access...) */
669 cris_signal_return_page = (unsigned long)
670 __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
672 return 0;
675 __initcall(cris_init_signal);