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 <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;
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 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.
61 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
62 long srp, struct pt_regs *regs)
68 spin_lock_irq(¤t->sighand->siglock);
70 saveset = current->blocked;
72 siginitset(¤t->blocked, mask);
75 spin_unlock_irq(¤t->sighand->siglock);
80 current->state = TASK_INTERRUPTIBLE;
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.
98 /* Define some dummy arguments to be able to reach the regs argument. */
100 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, long r12, long r13,
101 long mof, long srp, struct pt_regs *regs)
106 if (sigsetsize != sizeof(sigset_t))
109 if (copy_from_user(&newset, unewset, sizeof(newset)))
112 sigdelsetmask(&newset, ~_BLOCKABLE);
113 spin_lock_irq(¤t->sighand->siglock);
115 saveset = current->blocked;
116 current->blocked = newset;
119 spin_unlock_irq(¤t->sighand->siglock);
124 current->state = TASK_INTERRUPTIBLE;
127 if (do_signal(0, &saveset, regs)) {
128 /* See comment in function above. */
135 sys_sigaction(int signal, const struct old_sigaction *act,
136 struct old_sigaction *oact)
139 struct k_sigaction newk;
140 struct k_sigaction oldk;
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))
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))
163 __put_user(oldk.sa.sa_flags, &oact->sa_flags);
164 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
171 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
173 return do_sigaltstack(uss, uoss, rdusp());
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)))
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);
206 /* Define some dummy arguments to be able to reach the regs argument. */
208 sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
209 struct pt_regs *regs)
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)
226 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
229 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
230 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
232 sizeof(frame->extramask))))
235 sigdelsetmask(&set, ~_BLOCKABLE);
236 spin_lock_irq(¤t->sighand->siglock);
238 current->blocked = set;
241 spin_unlock_irq(¤t->sighand->siglock);
243 if (restore_sigcontext(regs, &frame->sc))
246 keep_debug_flags(oldccs, oldspc, regs);
251 force_sig(SIGSEGV, current);
255 /* Define some dummy variables to be able to reach the regs argument. */
257 sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
258 struct pt_regs *regs)
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)
275 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
278 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
281 sigdelsetmask(&set, ~_BLOCKABLE);
282 spin_lock_irq(¤t->sighand->siglock);
284 current->blocked = set;
287 spin_unlock_irq(¤t->sighand->siglock);
289 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
292 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
295 keep_debug_flags(oldccs, oldspc, regs);
300 force_sig(SIGSEGV, current);
304 /* Setup a signal frame. */
306 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
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);
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)
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. */
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
355 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
356 struct pt_regs * regs)
359 unsigned long return_ip;
360 struct signal_frame __user *frame;
363 frame = get_sigframe(ka, regs, sizeof(*frame));
365 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
368 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
373 if (_NSIG_WORDS > 1) {
374 err |= __copy_to_user(frame->extramask, &set->sig[1],
375 sizeof(frame->extramask));
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;
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));
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;
417 /* Actually move the USP to reflect the stacked frame. */
418 wrusp((unsigned long)frame);
424 ka->sa.sa_handler = SIG_DFL;
426 force_sig(SIGSEGV, current);
430 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
431 sigset_t *set, struct pt_regs * regs)
434 unsigned long return_ip;
435 struct rt_signal_frame __user *frame;
438 frame = get_sigframe(ka, regs, sizeof(*frame));
440 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
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);
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));
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;
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));
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;
500 regs->r11 = (unsigned long) &frame->info;
503 /* Actually move the usp to reflect the stacked frame. */
504 wrusp((unsigned long)frame);
510 ka->sa.sa_handler = SIG_DFL;
512 force_sig(SIGSEGV, current);
515 /* Invoke a singal handler to, well, handle the signal. */
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. */
523 /* If so, check system call restarting. */
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
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)) {
551 case -ERESTARTNOINTR:
553 * This means that the syscall should
554 * be called again after the signal
557 RESTART_CRIS_SYS(regs);
562 /* Set up the stack frame. */
563 if (ka->sa.sa_flags & SA_SIGINFO)
564 setup_rt_frame(sig, ka, info, oldset, regs);
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(¤t->sighand->siglock);
572 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
573 if (!(ka->sa.sa_flags & SA_NODEFER))
574 sigaddset(¤t->blocked,sig);
576 spin_unlock_irq(¤t->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
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
591 do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
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))
606 oldset = ¤t->blocked;
608 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
611 /* Deliver the signal. */
612 handle_signal(canrestart, signr, &info, &ka, oldset, regs);
616 /* Got here from a system call? */
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;
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
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);
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. */
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. */
681 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
685 /* Set up the trampolines on the signal return page. */
687 cris_init_signal(void)
689 u16* data = (u16*)kmalloc(PAGE_SIZE, GFP_KERNEL);
691 /* This is movu.w __NR_sigreturn, r9; break 13; */
693 data[1] = __NR_sigreturn;
695 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
697 data[4] = __NR_rt_sigreturn;
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);
707 __initcall(cris_init_signal);