2 * Copyright (C) 2003, Axis Communications AB.
5 #include <linux/sched.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>
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>
25 extern unsigned long cris_signal_return_page;
27 /* Flag to check if a signal is blockable. */
28 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
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.
35 * This relies on that PC points to the instruction after the break call.
37 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
42 unsigned long extramask[_NSIG_WORDS - 1];
43 unsigned char retcode[8]; /* Trampoline code. */
46 struct rt_signal_frame {
47 struct siginfo *pinfo;
51 unsigned char retcode[8]; /* Trampoline code. */
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);
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.
62 sys_sigsuspend(old_sigset_t mask)
65 siginitset(&blocked, mask);
66 return sigsuspend(&blocked);
70 sys_sigaction(int signal, const struct old_sigaction *act,
71 struct old_sigaction *oact)
74 struct k_sigaction newk;
75 struct k_sigaction oldk;
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))
85 __get_user(newk.sa.sa_flags, &act->sa_flags);
86 __get_user(mask, &act->sa_mask);
87 siginitset(&newk.sa.sa_mask, mask);
90 retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
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))
98 __put_user(oldk.sa.sa_flags, &oact->sa_flags);
99 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
106 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
108 return do_sigaltstack(uss, uoss, rdusp());
112 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
114 unsigned int err = 0;
115 unsigned long old_usp;
117 /* Always make any pending restarted system calls return -EINTR */
118 current_thread_info()->restart_block.fn = do_no_restart_syscall;
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().
125 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
128 /* Make that the user-mode flag is set. */
129 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
131 /* Restore the old USP. */
132 err |= __get_user(old_usp, &sc->usp);
141 /* Define some dummy arguments to be able to reach the regs argument. */
143 sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
144 struct pt_regs *regs)
147 struct signal_frame __user *frame;
148 unsigned long oldspc = regs->spc;
149 unsigned long oldccs = regs->ccs;
151 frame = (struct signal_frame *) rdusp();
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.
158 if (((long)frame) & 3)
161 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
164 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
165 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
167 sizeof(frame->extramask))))
170 sigdelsetmask(&set, ~_BLOCKABLE);
171 set_current_blocked(&set);
173 if (restore_sigcontext(regs, &frame->sc))
176 keep_debug_flags(oldccs, oldspc, regs);
181 force_sig(SIGSEGV, current);
185 /* Define some dummy variables to be able to reach the regs argument. */
187 sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
188 struct pt_regs *regs)
191 struct rt_signal_frame __user *frame;
192 unsigned long oldspc = regs->spc;
193 unsigned long oldccs = regs->ccs;
195 frame = (struct rt_signal_frame *) rdusp();
198 * Since the signal is stacked on a dword boundary, the frame
199 * should be dword aligned here as well. It it's not, then the
200 * user is trying some funny business.
202 if (((long)frame) & 3)
205 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
208 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
211 sigdelsetmask(&set, ~_BLOCKABLE);
212 set_current_blocked(&set);
214 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
217 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
220 keep_debug_flags(oldccs, oldspc, regs);
225 force_sig(SIGSEGV, current);
229 /* Setup a signal frame. */
231 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
241 * Copy the registers. They are located first in sc, so it's
242 * possible to use sc directly.
244 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
246 err |= __put_user(mask, &sc->oldmask);
247 err |= __put_user(usp, &sc->usp);
252 /* Figure out where to put the new signal frame - usually on the stack. */
253 static inline void __user *
254 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
260 /* This is the X/Open sanctioned signal stack switching. */
261 if (ka->sa.sa_flags & SA_ONSTACK) {
262 if (!on_sig_stack(sp))
263 sp = current->sas_ss_sp + current->sas_ss_size;
266 /* Make sure the frame is dword-aligned. */
269 return (void __user *)(sp - frame_size);
272 /* Grab and setup a signal frame.
274 * Basically a lot of state-info is stacked, and arranged for the
275 * user-mode program to return to the kernel using either a trampiline
276 * which performs the syscall sigreturn(), or a provided user-mode
280 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
281 struct pt_regs * regs)
284 unsigned long return_ip;
285 struct signal_frame __user *frame;
288 frame = get_sigframe(ka, regs, sizeof(*frame));
290 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
293 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
298 if (_NSIG_WORDS > 1) {
299 err |= __copy_to_user(frame->extramask, &set->sig[1],
300 sizeof(frame->extramask));
307 * Set up to return from user-space. If provided, use a stub
308 * already located in user-space.
310 if (ka->sa.sa_flags & SA_RESTORER) {
311 return_ip = (unsigned long)ka->sa.sa_restorer;
313 /* Trampoline - the desired return ip is in the signal return page. */
314 return_ip = cris_signal_return_page;
317 * This is movu.w __NR_sigreturn, r9; break 13;
319 * WE DO NOT USE IT ANY MORE! It's only left here for historical
320 * reasons and because gdb uses it as a signature to notice
321 * signal handler stack frames.
323 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
324 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
325 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
332 * Set up registers for signal handler.
334 * Where the code enters now.
335 * Where the code enter later.
336 * First argument, signo.
338 regs->erp = (unsigned long) ka->sa.sa_handler;
339 regs->srp = return_ip;
342 /* Actually move the USP to reflect the stacked frame. */
343 wrusp((unsigned long)frame);
349 ka->sa.sa_handler = SIG_DFL;
351 force_sig(SIGSEGV, current);
356 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
357 sigset_t *set, struct pt_regs * regs)
360 unsigned long return_ip;
361 struct rt_signal_frame __user *frame;
364 frame = get_sigframe(ka, regs, sizeof(*frame));
366 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
369 /* TODO: what is the current->exec_domain stuff and invmap ? */
371 err |= __put_user(&frame->info, &frame->pinfo);
372 err |= __put_user(&frame->uc, &frame->puc);
373 err |= copy_siginfo_to_user(&frame->info, info);
378 /* Clear all the bits of the ucontext we don't use. */
379 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
380 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
381 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
387 * Set up to return from user-space. If provided, use a stub
388 * already located in user-space.
390 if (ka->sa.sa_flags & SA_RESTORER) {
391 return_ip = (unsigned long) ka->sa.sa_restorer;
393 /* Trampoline - the desired return ip is in the signal return page. */
394 return_ip = cris_signal_return_page + 6;
397 * This is movu.w __NR_rt_sigreturn, r9; break 13;
399 * WE DO NOT USE IT ANY MORE! It's only left here for historical
400 * reasons and because gdb uses it as a signature to notice
401 * signal handler stack frames.
403 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
405 err |= __put_user(__NR_rt_sigreturn,
406 (short __user*)(frame->retcode+2));
408 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
415 * Set up registers for signal handler.
417 * Where the code enters now.
418 * Where the code enters later.
419 * First argument is signo.
420 * Second argument is (siginfo_t *).
421 * Third argument is unused.
423 regs->erp = (unsigned long) ka->sa.sa_handler;
424 regs->srp = return_ip;
426 regs->r11 = (unsigned long) &frame->info;
429 /* Actually move the usp to reflect the stacked frame. */
430 wrusp((unsigned long)frame);
436 ka->sa.sa_handler = SIG_DFL;
438 force_sig(SIGSEGV, current);
442 /* Invoke a signal handler to, well, handle the signal. */
444 handle_signal(int canrestart, unsigned long sig,
445 siginfo_t *info, struct k_sigaction *ka,
446 sigset_t *oldset, struct pt_regs * regs)
450 /* Check if this got called from a system call. */
452 /* If so, check system call restarting. */
454 case -ERESTART_RESTARTBLOCK:
455 case -ERESTARTNOHAND:
457 * This means that the syscall should
458 * only be restarted if there was no
459 * handler for the signal, and since
460 * this point isn't reached unless
461 * there is a handler, there's no need
469 * This means restart the syscall if
470 * there is no handler, or the handler
471 * was registered with SA_RESTART.
473 if (!(ka->sa.sa_flags & SA_RESTART)) {
480 case -ERESTARTNOINTR:
482 * This means that the syscall should
483 * be called again after the signal
486 RESTART_CRIS_SYS(regs);
491 /* Set up the stack frame. */
492 if (ka->sa.sa_flags & SA_SIGINFO)
493 ret = setup_rt_frame(sig, ka, info, oldset, regs);
495 ret = setup_frame(sig, ka, oldset, regs);
498 block_sigmask(ka, sig);
504 * Note that 'init' is a special process: it doesn't get signals it doesn't
505 * want to handle. Thus you cannot kill init even with a SIGKILL even by
508 * Also note that the regs structure given here as an argument, is the latest
509 * pushed pt_regs. It may or may not be the same as the first pushed registers
510 * when the initial usermode->kernelmode transition took place. Therefore
511 * we can use user_mode(regs) to see if we came directly from kernel or user
515 do_signal(int canrestart, struct pt_regs *regs)
519 struct k_sigaction ka;
523 * The common case should go fast, which is why this point is
524 * reached from kernel-mode. If that's the case, just return
525 * without doing anything.
527 if (!user_mode(regs))
530 if (test_thread_flag(TIF_RESTORE_SIGMASK))
531 oldset = ¤t->saved_sigmask;
533 oldset = ¤t->blocked;
535 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
538 /* Whee! Actually deliver the signal. */
539 if (handle_signal(canrestart, signr, &info, &ka,
541 /* a signal was successfully delivered; the saved
542 * sigmask will have been stored in the signal frame,
543 * and will be restored by sigreturn, so we can simply
544 * clear the TIF_RESTORE_SIGMASK flag */
545 if (test_thread_flag(TIF_RESTORE_SIGMASK))
546 clear_thread_flag(TIF_RESTORE_SIGMASK);
552 /* Got here from a system call? */
554 /* Restart the system call - no handlers present. */
555 if (regs->r10 == -ERESTARTNOHAND ||
556 regs->r10 == -ERESTARTSYS ||
557 regs->r10 == -ERESTARTNOINTR) {
558 RESTART_CRIS_SYS(regs);
561 if (regs->r10 == -ERESTART_RESTARTBLOCK){
562 regs->r9 = __NR_restart_syscall;
567 /* if there's no signal to deliver, we just put the saved sigmask
569 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
570 clear_thread_flag(TIF_RESTORE_SIGMASK);
571 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
576 ugdb_trap_user(struct thread_info *ti, int sig)
578 if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
579 /* Zero single-step PC if the reason we stopped wasn't a single
580 step exception. This is to avoid relying on it when it isn't
582 user_regs(ti)->spc = 0;
584 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
585 not within any configured h/w breakpoint range). Synchronize with
586 what already exists for kernel debugging. */
587 if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
588 /* Break 8: subtract 2 from ERP unless in a delay slot. */
589 if (!(user_regs(ti)->erp & 0x1))
590 user_regs(ti)->erp -= 2;
592 sys_kill(ti->task->pid, sig);
596 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
597 struct pt_regs *regs)
599 if (oldccs & (1 << Q_CCS_BITNR)) {
600 /* Pending single step due to single-stepping the break 13
601 in the signal trampoline: keep the Q flag. */
602 regs->ccs |= (1 << Q_CCS_BITNR);
603 /* S flag should be set - complain if it's not. */
604 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
605 printk("Q flag but no S flag?");
607 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
608 /* Assume the SPC is valid and interesting. */
611 } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
612 /* If a h/w bp was set in the signal handler we need
613 to keep the S flag. */
614 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
615 /* Don't keep the old SPC though; if we got here due to
616 a single-step, the Q flag should have been set. */
617 } else if (regs->spc) {
618 /* If we were single-stepping *before* the signal was taken,
619 we don't want to restore that state now, because GDB will
620 have forgotten all about it. */
622 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
626 /* Set up the trampolines on the signal return page. */
628 cris_init_signal(void)
630 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
632 /* This is movu.w __NR_sigreturn, r9; break 13; */
634 data[1] = __NR_sigreturn;
636 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
638 data[4] = __NR_rt_sigreturn;
641 /* Map to userspace with appropriate permissions (no write access...) */
642 cris_signal_return_page = (unsigned long)
643 __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
648 __initcall(cris_init_signal);