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;
28 * A syscall in CRIS is really a "break 13" instruction, which is 2
29 * bytes. The registers is manipulated so upon return the instruction
30 * will be executed again.
32 * This relies on that PC points to the instruction after the break call.
34 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
39 unsigned long extramask[_NSIG_WORDS - 1];
40 unsigned char retcode[8]; /* Trampoline code. */
43 struct rt_signal_frame {
44 struct siginfo *pinfo;
48 unsigned char retcode[8]; /* Trampoline code. */
51 void do_signal(int restart, struct pt_regs *regs);
52 void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
53 struct pt_regs *regs);
55 * Swap in the new signal mask, and wait for a signal. Define some
56 * dummy arguments to be able to reach the regs argument.
59 sys_sigsuspend(old_sigset_t mask)
62 siginitset(&blocked, mask);
63 return sigsuspend(&blocked);
67 sys_sigaction(int signal, const struct old_sigaction *act,
68 struct old_sigaction *oact)
71 struct k_sigaction newk;
72 struct k_sigaction oldk;
77 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
78 __get_user(newk.sa.sa_handler, &act->sa_handler) ||
79 __get_user(newk.sa.sa_restorer, &act->sa_restorer) ||
80 __get_user(newk.sa.sa_flags, &act->sa_flags) ||
81 __get_user(mask, &act->sa_mask))
84 siginitset(&newk.sa.sa_mask, mask);
87 retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
89 if (!retval && oact) {
90 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
91 __put_user(oldk.sa.sa_handler, &oact->sa_handler) ||
92 __put_user(oldk.sa.sa_restorer, &oact->sa_restorer) ||
93 __put_user(oldk.sa.sa_flags, &oact->sa_flags) ||
94 __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask))
103 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
105 return do_sigaltstack(uss, uoss, rdusp());
109 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
111 unsigned int err = 0;
112 unsigned long old_usp;
114 /* Always make any pending restarted system calls return -EINTR */
115 current_thread_info()->restart_block.fn = do_no_restart_syscall;
118 * Restore the registers from &sc->regs. sc is already checked
119 * for VERIFY_READ since the signal_frame was previously
120 * checked in sys_sigreturn().
122 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
125 /* Make that the user-mode flag is set. */
126 regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
128 /* Restore the old USP. */
129 err |= __get_user(old_usp, &sc->usp);
138 /* Define some dummy arguments to be able to reach the regs argument. */
140 sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
141 struct pt_regs *regs)
144 struct signal_frame __user *frame;
145 unsigned long oldspc = regs->spc;
146 unsigned long oldccs = regs->ccs;
148 frame = (struct signal_frame *) rdusp();
151 * Since the signal is stacked on a dword boundary, the frame
152 * should be dword aligned here as well. It it's not, then the
153 * user is trying some funny business.
155 if (((long)frame) & 3)
158 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
161 if (__get_user(set.sig[0], &frame->sc.oldmask) ||
162 (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
164 sizeof(frame->extramask))))
167 set_current_blocked(&set);
169 if (restore_sigcontext(regs, &frame->sc))
172 keep_debug_flags(oldccs, oldspc, regs);
177 force_sig(SIGSEGV, current);
181 /* Define some dummy variables to be able to reach the regs argument. */
183 sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
184 struct pt_regs *regs)
187 struct rt_signal_frame __user *frame;
188 unsigned long oldspc = regs->spc;
189 unsigned long oldccs = regs->ccs;
191 frame = (struct rt_signal_frame *) rdusp();
194 * Since the signal is stacked on a dword boundary, the frame
195 * should be dword aligned here as well. It it's not, then the
196 * user is trying some funny business.
198 if (((long)frame) & 3)
201 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
204 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
207 set_current_blocked(&set);
209 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
212 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
215 keep_debug_flags(oldccs, oldspc, regs);
220 force_sig(SIGSEGV, current);
224 /* Setup a signal frame. */
226 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
236 * Copy the registers. They are located first in sc, so it's
237 * possible to use sc directly.
239 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
241 err |= __put_user(mask, &sc->oldmask);
242 err |= __put_user(usp, &sc->usp);
247 /* Figure out where to put the new signal frame - usually on the stack. */
248 static inline void __user *
249 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
255 /* This is the X/Open sanctioned signal stack switching. */
256 if (ka->sa.sa_flags & SA_ONSTACK) {
257 if (!on_sig_stack(sp))
258 sp = current->sas_ss_sp + current->sas_ss_size;
261 /* Make sure the frame is dword-aligned. */
264 return (void __user *)(sp - frame_size);
267 /* Grab and setup a signal frame.
269 * Basically a lot of state-info is stacked, and arranged for the
270 * user-mode program to return to the kernel using either a trampiline
271 * which performs the syscall sigreturn(), or a provided user-mode
275 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
276 struct pt_regs * regs)
279 unsigned long return_ip;
280 struct signal_frame __user *frame;
283 frame = get_sigframe(ka, regs, sizeof(*frame));
285 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
293 if (_NSIG_WORDS > 1) {
294 err |= __copy_to_user(frame->extramask, &set->sig[1],
295 sizeof(frame->extramask));
302 * Set up to return from user-space. If provided, use a stub
303 * already located in user-space.
305 if (ka->sa.sa_flags & SA_RESTORER) {
306 return_ip = (unsigned long)ka->sa.sa_restorer;
308 /* Trampoline - the desired return ip is in the signal return page. */
309 return_ip = cris_signal_return_page;
312 * This is movu.w __NR_sigreturn, r9; break 13;
314 * WE DO NOT USE IT ANY MORE! It's only left here for historical
315 * reasons and because gdb uses it as a signature to notice
316 * signal handler stack frames.
318 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
319 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
320 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
327 * Set up registers for signal handler.
329 * Where the code enters now.
330 * Where the code enter later.
331 * First argument, signo.
333 regs->erp = (unsigned long) ka->sa.sa_handler;
334 regs->srp = return_ip;
337 /* Actually move the USP to reflect the stacked frame. */
338 wrusp((unsigned long)frame);
343 force_sigsegv(sig, current);
348 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
349 sigset_t *set, struct pt_regs * regs)
352 unsigned long return_ip;
353 struct rt_signal_frame __user *frame;
356 frame = get_sigframe(ka, regs, sizeof(*frame));
358 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
361 /* TODO: what is the current->exec_domain stuff and invmap ? */
363 err |= __put_user(&frame->info, &frame->pinfo);
364 err |= __put_user(&frame->uc, &frame->puc);
365 err |= copy_siginfo_to_user(&frame->info, info);
370 /* Clear all the bits of the ucontext we don't use. */
371 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
372 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
373 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
379 * Set up to return from user-space. If provided, use a stub
380 * already located in user-space.
382 if (ka->sa.sa_flags & SA_RESTORER) {
383 return_ip = (unsigned long) ka->sa.sa_restorer;
385 /* Trampoline - the desired return ip is in the signal return page. */
386 return_ip = cris_signal_return_page + 6;
389 * This is movu.w __NR_rt_sigreturn, r9; break 13;
391 * WE DO NOT USE IT ANY MORE! It's only left here for historical
392 * reasons and because gdb uses it as a signature to notice
393 * signal handler stack frames.
395 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
397 err |= __put_user(__NR_rt_sigreturn,
398 (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 enters later.
411 * First argument is signo.
412 * Second argument is (siginfo_t *).
413 * Third argument is unused.
415 regs->erp = (unsigned long) ka->sa.sa_handler;
416 regs->srp = return_ip;
418 regs->r11 = (unsigned long) &frame->info;
421 /* Actually move the usp to reflect the stacked frame. */
422 wrusp((unsigned long)frame);
427 force_sigsegv(sig, current);
431 /* Invoke a signal handler to, well, handle the signal. */
433 handle_signal(int canrestart, unsigned long sig,
434 siginfo_t *info, struct k_sigaction *ka,
435 struct pt_regs * regs)
437 sigset_t *oldset = sigmask_to_save();
440 /* Check if this got called from a system call. */
442 /* If so, check system call restarting. */
444 case -ERESTART_RESTARTBLOCK:
445 case -ERESTARTNOHAND:
447 * This means that the syscall should
448 * only be restarted if there was no
449 * handler for the signal, and since
450 * this point isn't reached unless
451 * there is a handler, there's no need
459 * This means restart the syscall if
460 * there is no handler, or the handler
461 * was registered with SA_RESTART.
463 if (!(ka->sa.sa_flags & SA_RESTART)) {
470 case -ERESTARTNOINTR:
472 * This means that the syscall should
473 * be called again after the signal
476 RESTART_CRIS_SYS(regs);
481 /* Set up the stack frame. */
482 if (ka->sa.sa_flags & SA_SIGINFO)
483 ret = setup_rt_frame(sig, ka, info, oldset, regs);
485 ret = setup_frame(sig, ka, oldset, regs);
488 signal_delivered(sig, info, ka, regs, 0);
492 * Note that 'init' is a special process: it doesn't get signals it doesn't
493 * want to handle. Thus you cannot kill init even with a SIGKILL even by
496 * Also note that the regs structure given here as an argument, is the latest
497 * pushed pt_regs. It may or may not be the same as the first pushed registers
498 * when the initial usermode->kernelmode transition took place. Therefore
499 * we can use user_mode(regs) to see if we came directly from kernel or user
503 do_signal(int canrestart, struct pt_regs *regs)
507 struct k_sigaction ka;
510 * The common case should go fast, which is why this point is
511 * reached from kernel-mode. If that's the case, just return
512 * without doing anything.
514 if (!user_mode(regs))
517 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
520 /* Whee! Actually deliver the signal. */
521 handle_signal(canrestart, signr, &info, &ka, regs);
525 /* Got here from a system call? */
527 /* Restart the system call - no handlers present. */
528 if (regs->r10 == -ERESTARTNOHAND ||
529 regs->r10 == -ERESTARTSYS ||
530 regs->r10 == -ERESTARTNOINTR) {
531 RESTART_CRIS_SYS(regs);
534 if (regs->r10 == -ERESTART_RESTARTBLOCK){
535 regs->r9 = __NR_restart_syscall;
540 /* if there's no signal to deliver, we just put the saved sigmask
542 restore_saved_sigmask();
546 ugdb_trap_user(struct thread_info *ti, int sig)
548 if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
549 /* Zero single-step PC if the reason we stopped wasn't a single
550 step exception. This is to avoid relying on it when it isn't
552 user_regs(ti)->spc = 0;
554 /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
555 not within any configured h/w breakpoint range). Synchronize with
556 what already exists for kernel debugging. */
557 if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
558 /* Break 8: subtract 2 from ERP unless in a delay slot. */
559 if (!(user_regs(ti)->erp & 0x1))
560 user_regs(ti)->erp -= 2;
562 sys_kill(ti->task->pid, sig);
566 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
567 struct pt_regs *regs)
569 if (oldccs & (1 << Q_CCS_BITNR)) {
570 /* Pending single step due to single-stepping the break 13
571 in the signal trampoline: keep the Q flag. */
572 regs->ccs |= (1 << Q_CCS_BITNR);
573 /* S flag should be set - complain if it's not. */
574 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
575 printk("Q flag but no S flag?");
577 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
578 /* Assume the SPC is valid and interesting. */
581 } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
582 /* If a h/w bp was set in the signal handler we need
583 to keep the S flag. */
584 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
585 /* Don't keep the old SPC though; if we got here due to
586 a single-step, the Q flag should have been set. */
587 } else if (regs->spc) {
588 /* If we were single-stepping *before* the signal was taken,
589 we don't want to restore that state now, because GDB will
590 have forgotten all about it. */
592 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
596 /* Set up the trampolines on the signal return page. */
598 cris_init_signal(void)
600 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
602 /* This is movu.w __NR_sigreturn, r9; break 13; */
604 data[1] = __NR_sigreturn;
606 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
608 data[4] = __NR_rt_sigreturn;
611 /* Map to userspace with appropriate permissions (no write access...) */
612 cris_signal_return_page = (unsigned long)
613 __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
618 __initcall(cris_init_signal);