2 * linux/arch/cris/kernel/signal.c
4 * Based on arch/i386/kernel/signal.c by
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *
8 * Ideas also taken from arch/arm.
10 * Copyright (C) 2000, 2001, 2002 Axis Communications AB
12 * Authors: Bjorn Wesen (bjornw@axis.com)
16 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
28 #include <asm/processor.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
37 /* manipulate regs so that upon return, it will be re-executed */
39 /* We rely on that pc points to the instruction after "break 13", so the
40 * library must never do strange things like putting it in a delay slot.
42 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
44 int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs);
46 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
48 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
50 if (from->si_code < 0)
51 return __copy_to_user(to, from, sizeof(siginfo_t));
55 /* If you change siginfo_t structure, please be sure
56 this code is fixed accordingly.
57 It should never copy any pad contained in the structure
58 to avoid security leaks, but must copy the generic
59 3 ints plus the relevant union member. */
60 err = __put_user(from->si_signo, &to->si_signo);
61 err |= __put_user(from->si_errno, &to->si_errno);
62 err |= __put_user((short)from->si_code, &to->si_code);
63 /* First 32bits of unions are always present. */
64 err |= __put_user(from->si_pid, &to->si_pid);
65 switch (from->si_code >> 16) {
66 case __SI_FAULT >> 16:
67 err |= __put_user(from->si_addr, &to->si_addr);
70 err |= __put_user(from->si_utime, &to->si_utime);
71 err |= __put_user(from->si_stime, &to->si_stime);
72 err |= __put_user(from->si_status, &to->si_status);
74 err |= __put_user(from->si_uid, &to->si_uid);
76 /* case __SI_RT: This is not generated by the kernel as of now. */
83 * Atomically swap in the new signal mask, and wait for a signal. Define
84 * dummy arguments to be able to reach the regs argument. (Note that this
85 * arrangement relies on old_sigset_t occupying one register.)
88 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
89 long srp, struct pt_regs *regs)
94 spin_lock_irq(¤t->sigmask_lock);
95 saveset = current->blocked;
96 siginitset(¤t->blocked, mask);
97 recalc_sigpending(current);
98 spin_unlock_irq(¤t->sigmask_lock);
102 current->state = TASK_INTERRUPTIBLE;
104 if (do_signal(0, &saveset, regs))
105 /* We will get here twice: once to call the signal
106 handler, then again to return from the
107 sigsuspend system call. When calling the
108 signal handler, R10 holds the signal number as
109 set through do_signal. The sigsuspend call
110 will return with the restored value set above;
116 /* Define dummy arguments to be able to reach the regs argument. (Note that
117 * this arrangement relies on size_t occupying one register.)
120 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, long r12, long r13,
121 long mof, long srp, struct pt_regs *regs)
123 sigset_t saveset, newset;
125 /* XXX: Don't preclude handling different sized sigset_t's. */
126 if (sigsetsize != sizeof(sigset_t))
129 if (copy_from_user(&newset, unewset, sizeof(newset)))
131 sigdelsetmask(&newset, ~_BLOCKABLE);
133 spin_lock_irq(¤t->sigmask_lock);
134 saveset = current->blocked;
135 current->blocked = newset;
136 recalc_sigpending(current);
137 spin_unlock_irq(¤t->sigmask_lock);
141 current->state = TASK_INTERRUPTIBLE;
143 if (do_signal(0, &saveset, regs))
144 /* We will get here twice: once to call the signal
145 handler, then again to return from the
146 sigsuspend system call. When calling the
147 signal handler, R10 holds the signal number as
148 set through do_signal. The sigsuspend call
149 will return with the restored value set above;
156 sys_sigaction(int sig, const struct old_sigaction *act,
157 struct old_sigaction *oact)
159 struct k_sigaction new_ka, old_ka;
164 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
165 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
166 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
168 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
169 __get_user(mask, &act->sa_mask);
170 siginitset(&new_ka.sa.sa_mask, mask);
173 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
176 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
177 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
178 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
180 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
181 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
188 sys_sigaltstack(const stack_t *uss, stack_t *uoss)
190 return do_sigaltstack(uss, uoss, rdusp());
195 * Do a signal return; undo the signal stack.
199 struct sigcontext sc;
200 unsigned long extramask[_NSIG_WORDS-1];
201 unsigned char retcode[8]; /* trampoline code */
205 struct siginfo *pinfo;
209 unsigned char retcode[8]; /* trampoline code */
214 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
216 unsigned int err = 0;
217 unsigned long old_usp;
219 /* restore the regs from &sc->regs (same as sc, since regs is first)
220 * (sc is already checked for VERIFY_READ since the sigframe was
221 * checked in sys_sigreturn previously)
224 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
227 /* make sure the U-flag is set so user-mode cannot fool us */
229 regs->dccr |= 1 << 8;
231 /* restore the old USP as it was before we stacked the sc etc.
232 * (we cannot just pop the sigcontext since we aligned the sp and
233 * stuff after pushing it)
236 err |= __get_user(old_usp, &sc->usp);
240 /* TODO: the other ports use regs->orig_XX to disable syscall checks
241 * after this completes, but we don't use that mechanism. maybe we can
251 /* Define dummy arguments to be able to reach the regs argument. */
253 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
254 long srp, struct pt_regs *regs)
256 struct sigframe *frame = (struct sigframe *)rdusp();
260 * Since we stacked the signal on a dword boundary,
261 * then frame should be dword aligned here. If it's
262 * not, then the user is trying to mess with us.
264 if (((long)frame) & 3)
267 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
269 if (__get_user(set.sig[0], &frame->sc.oldmask)
271 && __copy_from_user(&set.sig[1], &frame->extramask,
272 sizeof(frame->extramask))))
275 sigdelsetmask(&set, ~_BLOCKABLE);
276 spin_lock_irq(¤t->sigmask_lock);
277 current->blocked = set;
278 recalc_sigpending(current);
279 spin_unlock_irq(¤t->sigmask_lock);
281 if (restore_sigcontext(regs, &frame->sc))
284 /* TODO: SIGTRAP when single-stepping as in arm ? */
289 force_sig(SIGSEGV, current);
293 /* Define dummy arguments to be able to reach the regs argument. */
295 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
296 long mof, long srp, struct pt_regs *regs)
298 struct rt_sigframe *frame = (struct rt_sigframe *)rdusp();
303 * Since we stacked the signal on a dword boundary,
304 * then frame should be dword aligned here. If it's
305 * not, then the user is trying to mess with us.
307 if (((long)frame) & 3)
310 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
312 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
315 sigdelsetmask(&set, ~_BLOCKABLE);
316 spin_lock_irq(¤t->sigmask_lock);
317 current->blocked = set;
318 recalc_sigpending(current);
319 spin_unlock_irq(¤t->sigmask_lock);
321 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
324 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
326 /* It is more difficult to avoid calling this function than to
327 call it and ignore errors. */
328 do_sigaltstack(&st, NULL, rdusp());
333 force_sig(SIGSEGV, current);
338 * Set up a signal frame.
342 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, unsigned long mask)
345 unsigned long usp = rdusp();
347 /* copy the regs. they are first in sc so we can use sc directly */
349 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
351 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
352 the signal handler. The frametype will be restored to its previous
353 value in restore_sigcontext. */
354 regs->frametype = CRIS_FRAME_NORMAL;
356 /* then some other stuff */
358 err |= __put_user(mask, &sc->oldmask);
360 err |= __put_user(usp, &sc->usp);
365 /* figure out where we want to put the new signal frame - usually on the stack */
368 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
370 unsigned long sp = rdusp();
372 /* This is the X/Open sanctioned signal stack switching. */
373 if (ka->sa.sa_flags & SA_ONSTACK) {
374 if (! on_sig_stack(sp))
375 sp = current->sas_ss_sp + current->sas_ss_size;
378 /* make sure the frame is dword-aligned */
382 return (void *)(sp - frame_size);
385 /* grab and setup a signal frame.
387 * basically we stack a lot of state info, and arrange for the
388 * user-mode program to return to the kernel using either a
389 * trampoline which performs the syscall sigreturn, or a provided
390 * user-mode trampoline.
393 static void setup_frame(int sig, struct k_sigaction *ka,
394 sigset_t *set, struct pt_regs * regs)
396 struct sigframe *frame;
397 unsigned long return_ip;
400 frame = get_sigframe(ka, regs, sizeof(*frame));
402 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
405 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
409 if (_NSIG_WORDS > 1) {
410 err |= __copy_to_user(frame->extramask, &set->sig[1],
411 sizeof(frame->extramask));
416 /* Set up to return from userspace. If provided, use a stub
417 already in userspace. */
418 if (ka->sa.sa_flags & SA_RESTORER) {
419 return_ip = (unsigned long)ka->sa.sa_restorer;
421 /* trampoline - the desired return ip is the retcode itself */
422 return_ip = (unsigned long)&frame->retcode;
423 /* This is movu.w __NR_sigreturn, r9; break 13; */
424 err |= __put_user(0x9c5f, (short *)(frame->retcode+0));
425 err |= __put_user(__NR_sigreturn, (short *)(frame->retcode+2));
426 err |= __put_user(0xe93d, (short *)(frame->retcode+4));
432 /* Set up registers for signal handler */
434 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
435 regs->srp = return_ip; /* what we enter LATER */
436 regs->r10 = sig; /* first argument is signo */
438 /* actually move the usp to reflect the stacked frame */
440 wrusp((unsigned long)frame);
446 ka->sa.sa_handler = SIG_DFL;
447 force_sig(SIGSEGV, current);
450 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
451 sigset_t *set, struct pt_regs * regs)
453 struct rt_sigframe *frame;
454 unsigned long return_ip;
457 frame = get_sigframe(ka, regs, sizeof(*frame));
459 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
462 err |= __put_user(&frame->info, &frame->pinfo);
463 err |= __put_user(&frame->uc, &frame->puc);
464 err |= copy_siginfo_to_user(&frame->info, info);
468 /* Clear all the bits of the ucontext we don't use. */
469 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
471 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
473 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
478 /* Set up to return from userspace. If provided, use a stub
479 already in userspace. */
480 if (ka->sa.sa_flags & SA_RESTORER) {
481 return_ip = (unsigned long)ka->sa.sa_restorer;
483 /* trampoline - the desired return ip is the retcode itself */
484 return_ip = (unsigned long)&frame->retcode;
485 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
486 err |= __put_user(0x9c5f, (short *)(frame->retcode+0));
487 err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode+2));
488 err |= __put_user(0xe93d, (short *)(frame->retcode+4));
494 /* TODO what is the current->exec_domain stuff and invmap ? */
496 /* Set up registers for signal handler */
498 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
499 regs->srp = return_ip; /* what we enter LATER */
500 regs->r10 = sig; /* first argument is signo */
501 regs->r11 = (unsigned long) &frame->info; /* second argument is (siginfo_t *) */
502 regs->r12 = 0; /* third argument is unused */
504 /* actually move the usp to reflect the stacked frame */
506 wrusp((unsigned long)frame);
512 ka->sa.sa_handler = SIG_DFL;
513 force_sig(SIGSEGV, current);
517 * OK, we're invoking a handler
521 handle_signal(int canrestart, unsigned long sig, struct k_sigaction *ka,
522 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
524 /* Are we from a system call? */
526 /* If so, check system call restarting.. */
528 case -ERESTARTNOHAND:
529 /* ERESTARTNOHAND means that the syscall should only be
530 restarted if there was no handler for the signal, and since
531 we only get here if there is a handler, we don't restart */
536 /* ERESTARTSYS means to restart the syscall if there is no
537 handler or the handler was registered with SA_RESTART */
538 if (!(ka->sa.sa_flags & SA_RESTART)) {
543 case -ERESTARTNOINTR:
544 /* ERESTARTNOINTR means that the syscall should be called again
545 after the signal handler returns. */
546 RESTART_CRIS_SYS(regs);
550 /* Set up the stack frame */
551 if (ka->sa.sa_flags & SA_SIGINFO)
552 setup_rt_frame(sig, ka, info, oldset, regs);
554 setup_frame(sig, ka, oldset, regs);
556 if (ka->sa.sa_flags & SA_ONESHOT)
557 ka->sa.sa_handler = SIG_DFL;
559 if (!(ka->sa.sa_flags & SA_NODEFER)) {
560 spin_lock_irq(¤t->sigmask_lock);
561 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
562 sigaddset(¤t->blocked,sig);
563 recalc_sigpending(current);
564 spin_unlock_irq(¤t->sigmask_lock);
569 * Note that 'init' is a special process: it doesn't get signals it doesn't
570 * want to handle. Thus you cannot kill init even with a SIGKILL even by
573 * Also note that the regs structure given here as an argument, is the latest
574 * pushed pt_regs. It may or may not be the same as the first pushed registers
575 * when the initial usermode->kernelmode transition took place. Therefore
576 * we can use user_mode(regs) to see if we came directly from kernel or user
580 int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
583 struct k_sigaction *ka;
586 * We want the common case to go fast, which
587 * is why we may in certain cases get here from
588 * kernel mode. Just return without doing anything
591 if (!user_mode(regs))
595 oldset = ¤t->blocked;
600 spin_lock_irq(¤t->sigmask_lock);
601 signr = dequeue_signal(¤t->blocked, &info);
602 spin_unlock_irq(¤t->sigmask_lock);
607 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
608 /* Let the debugger run. */
609 current->exit_code = signr;
610 current->state = TASK_STOPPED;
611 notify_parent(current, SIGCHLD);
614 /* We're back. Did the debugger cancel the sig? */
615 if (!(signr = current->exit_code))
617 current->exit_code = 0;
619 /* The debugger continued. Ignore SIGSTOP. */
620 if (signr == SIGSTOP)
623 /* Update the siginfo structure. Is this good? */
624 if (signr != info.si_signo) {
625 info.si_signo = signr;
627 info.si_code = SI_USER;
628 info.si_pid = current->p_pptr->pid;
629 info.si_uid = current->p_pptr->uid;
632 /* If the (new) signal is now blocked, requeue it. */
633 if (sigismember(¤t->blocked, signr)) {
634 send_sig_info(signr, &info, current);
639 ka = ¤t->sig->action[signr-1];
640 if (ka->sa.sa_handler == SIG_IGN) {
641 if (signr != SIGCHLD)
643 /* Check for SIGCHLD: it's special. */
644 while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
649 if (ka->sa.sa_handler == SIG_DFL) {
650 int exit_code = signr;
652 /* Init gets no signals it doesn't want. */
653 if (current->pid == 1)
657 case SIGCONT: case SIGCHLD: case SIGWINCH:
660 case SIGTSTP: case SIGTTIN: case SIGTTOU:
661 if (is_orphaned_pgrp(current->pgrp))
666 current->state = TASK_STOPPED;
667 current->exit_code = signr;
668 if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
669 notify_parent(current, SIGCHLD);
673 case SIGQUIT: case SIGILL: case SIGTRAP:
674 case SIGABRT: case SIGFPE: case SIGSEGV:
675 case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
676 if (do_coredump(signr, regs))
682 sig_exit(signr, exit_code, &info);
687 /* Whee! Actually deliver the signal. */
688 handle_signal(canrestart, signr, ka, &info, oldset, regs);
692 /* Did we come from a system call? */
694 /* Restart the system call - no handlers present */
695 if (regs->r10 == -ERESTARTNOHAND ||
696 regs->r10 == -ERESTARTSYS ||
697 regs->r10 == -ERESTARTNOINTR) {
698 RESTART_CRIS_SYS(regs);