2 * Copyright (C) 2003, Axis Communications AB.
5 #include <linux/sched.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>
18 #include <asm/processor.h>
19 #include <asm/ucontext.h>
20 #include <asm/uaccess.h>
21 #include <arch/ptrace.h>
22 #include <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;
41 unsigned long extramask[_NSIG_WORDS - 1];
42 unsigned char retcode[8]; /* Trampoline code. */
45 struct rt_signal_frame {
46 struct siginfo *pinfo;
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.
61 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
62 long srp, struct pt_regs *regs)
65 spin_lock_irq(¤t->sighand->siglock);
66 current->saved_sigmask = current->blocked;
67 siginitset(¤t->blocked, mask);
69 spin_unlock_irq(¤t->sighand->siglock);
70 current->state = TASK_INTERRUPTIBLE;
72 set_thread_flag(TIF_RESTORE_SIGMASK);
73 return -ERESTARTNOHAND;
77 sys_sigaction(int signal, const struct old_sigaction *act,
78 struct old_sigaction *oact)
81 struct k_sigaction newk;
82 struct k_sigaction oldk;
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))
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))
105 __put_user(oldk.sa.sa_flags, &oact->sa_flags);
106 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
113 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
115 return do_sigaltstack(uss, uoss, rdusp());
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)))
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);
148 /* Define some dummy arguments to be able to reach the regs argument. */
150 sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
151 struct pt_regs *regs)
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)
168 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
171 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
172 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
174 sizeof(frame->extramask))))
177 sigdelsetmask(&set, ~_BLOCKABLE);
178 spin_lock_irq(¤t->sighand->siglock);
180 current->blocked = set;
183 spin_unlock_irq(¤t->sighand->siglock);
185 if (restore_sigcontext(regs, &frame->sc))
188 keep_debug_flags(oldccs, oldspc, regs);
193 force_sig(SIGSEGV, current);
197 /* Define some dummy variables to be able to reach the regs argument. */
199 sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
200 struct pt_regs *regs)
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)
217 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
220 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
223 sigdelsetmask(&set, ~_BLOCKABLE);
224 spin_lock_irq(¤t->sighand->siglock);
226 current->blocked = set;
229 spin_unlock_irq(¤t->sighand->siglock);
231 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
234 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
237 keep_debug_flags(oldccs, oldspc, regs);
242 force_sig(SIGSEGV, current);
246 /* Setup a signal frame. */
248 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
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);
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)
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. */
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
297 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
298 struct pt_regs * regs)
301 unsigned long return_ip;
302 struct signal_frame __user *frame;
305 frame = get_sigframe(ka, regs, sizeof(*frame));
307 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
310 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
315 if (_NSIG_WORDS > 1) {
316 err |= __copy_to_user(frame->extramask, &set->sig[1],
317 sizeof(frame->extramask));
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;
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));
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;
359 /* Actually move the USP to reflect the stacked frame. */
360 wrusp((unsigned long)frame);
366 ka->sa.sa_handler = SIG_DFL;
368 force_sig(SIGSEGV, current);
373 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
374 sigset_t *set, struct pt_regs * regs)
377 unsigned long return_ip;
378 struct rt_signal_frame __user *frame;
381 frame = get_sigframe(ka, regs, sizeof(*frame));
383 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
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);
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));
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;
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));
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;
443 regs->r11 = (unsigned long) &frame->info;
446 /* Actually move the usp to reflect the stacked frame. */
447 wrusp((unsigned long)frame);
453 ka->sa.sa_handler = SIG_DFL;
455 force_sig(SIGSEGV, current);
459 /* Invoke a singal handler to, well, handle the signal. */
461 handle_signal(int canrestart, unsigned long sig,
462 siginfo_t *info, struct k_sigaction *ka,
463 sigset_t *oldset, struct pt_regs * regs)
467 /* Check if this got called from a system call. */
469 /* If so, check system call restarting. */
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
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)) {
497 case -ERESTARTNOINTR:
499 * This means that the syscall should
500 * be called again after the signal
503 RESTART_CRIS_SYS(regs);
508 /* Set up the stack frame. */
509 if (ka->sa.sa_flags & SA_SIGINFO)
510 ret = setup_rt_frame(sig, ka, info, oldset, regs);
512 ret = setup_frame(sig, ka, oldset, regs);
514 if (ka->sa.sa_flags & SA_ONESHOT)
515 ka->sa.sa_handler = SIG_DFL;
518 spin_lock_irq(¤t->sighand->siglock);
519 sigorsets(¤t->blocked, ¤t->blocked,
521 if (!(ka->sa.sa_flags & SA_NODEFER))
522 sigaddset(¤t->blocked, sig);
524 spin_unlock_irq(¤t->sighand->siglock);
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
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
542 do_signal(int canrestart, struct pt_regs *regs)
546 struct k_sigaction ka;
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))
557 if (test_thread_flag(TIF_RESTORE_SIGMASK))
558 oldset = ¤t->saved_sigmask;
560 oldset = ¤t->blocked;
562 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
565 /* Whee! Actually deliver the signal. */
566 if (handle_signal(canrestart, signr, &info, &ka,
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);
579 /* Got here from a system call? */
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;
594 /* if there's no signal to deliver, we just put the saved sigmask
596 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
597 clear_thread_flag(TIF_RESTORE_SIGMASK);
598 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
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
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);
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. */
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. */
649 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
653 /* Set up the trampolines on the signal return page. */
655 cris_init_signal(void)
657 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
659 /* This is movu.w __NR_sigreturn, r9; break 13; */
661 data[1] = __NR_sigreturn;
663 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
665 data[4] = __NR_rt_sigreturn;
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);
675 __initcall(cris_init_signal);