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 Axis Communications AB
12 * Authors: Bjorn Wesen (bjornw@axis.com)
16 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/kernel.h>
20 #include <linux/signal.h>
21 #include <linux/errno.h>
22 #include <linux/wait.h>
23 #include <linux/ptrace.h>
24 #include <linux/unistd.h>
25 #include <linux/stddef.h>
27 #include <asm/processor.h>
28 #include <asm/ucontext.h>
29 #include <asm/uaccess.h>
33 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
36 /* manipulate regs so that upon return, it will be re-executed */
38 /* We rely on that pc points to the instruction after "break 13", so the
39 * library must never do strange things like putting it in a delay slot.
41 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
43 int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs);
46 * Atomically swap in the new signal mask, and wait for a signal. Define
47 * dummy arguments to be able to reach the regs argument. (Note that this
48 * arrangement relies on old_sigset_t occupying one register.)
51 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
52 long srp, struct pt_regs *regs)
57 spin_lock_irq(¤t->sighand->siglock);
58 saveset = current->blocked;
59 siginitset(¤t->blocked, mask);
61 spin_unlock_irq(¤t->sighand->siglock);
65 current->state = TASK_INTERRUPTIBLE;
67 if (do_signal(0, &saveset, regs))
68 /* We will get here twice: once to call the signal
69 handler, then again to return from the
70 sigsuspend system call. When calling the
71 signal handler, R10 holds the signal number as
72 set through do_signal. The sigsuspend call
73 will return with the restored value set above;
79 /* Define dummy arguments to be able to reach the regs argument. (Note that
80 * this arrangement relies on size_t occupying one register.)
83 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, long r12, long r13,
84 long mof, long srp, struct pt_regs *regs)
86 sigset_t saveset, newset;
88 /* XXX: Don't preclude handling different sized sigset_t's. */
89 if (sigsetsize != sizeof(sigset_t))
92 if (copy_from_user(&newset, unewset, sizeof(newset)))
94 sigdelsetmask(&newset, ~_BLOCKABLE);
96 spin_lock_irq(¤t->sighand->siglock);
97 saveset = current->blocked;
98 current->blocked = newset;
100 spin_unlock_irq(¤t->sighand->siglock);
104 current->state = TASK_INTERRUPTIBLE;
106 if (do_signal(0, &saveset, regs))
107 /* We will get here twice: once to call the signal
108 handler, then again to return from the
109 sigsuspend system call. When calling the
110 signal handler, R10 holds the signal number as
111 set through do_signal. The sigsuspend call
112 will return with the restored value set above;
119 sys_sigaction(int sig, const struct old_sigaction __user *act,
120 struct old_sigaction *oact)
122 struct k_sigaction new_ka, old_ka;
127 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
128 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
129 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
131 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
132 __get_user(mask, &act->sa_mask);
133 siginitset(&new_ka.sa.sa_mask, mask);
136 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
139 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
140 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
141 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
143 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
144 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
151 sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
153 return do_sigaltstack(uss, uoss, rdusp());
158 * Do a signal return; undo the signal stack.
162 struct sigcontext sc;
163 unsigned long extramask[_NSIG_WORDS-1];
164 unsigned char retcode[8]; /* trampoline code */
168 struct siginfo *pinfo;
172 unsigned char retcode[8]; /* trampoline code */
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;
185 /* restore the regs from &sc->regs (same as sc, since regs is first)
186 * (sc is already checked for VERIFY_READ since the sigframe was
187 * checked in sys_sigreturn previously)
190 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
193 /* make sure the U-flag is set so user-mode cannot fool us */
195 regs->dccr |= 1 << 8;
197 /* restore the old USP as it was before we stacked the sc etc.
198 * (we cannot just pop the sigcontext since we aligned the sp and
199 * stuff after pushing it)
202 err |= __get_user(old_usp, &sc->usp);
206 /* TODO: the other ports use regs->orig_XX to disable syscall checks
207 * after this completes, but we don't use that mechanism. maybe we can
217 /* Define dummy arguments to be able to reach the regs argument. */
219 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
220 long srp, struct pt_regs *regs)
222 struct sigframe __user *frame = (struct sigframe *)rdusp();
226 * Since we stacked the signal on a dword boundary,
227 * then frame should be dword aligned here. If it's
228 * not, then the user is trying to mess with us.
230 if (((long)frame) & 3)
233 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
235 if (__get_user(set.sig[0], &frame->sc.oldmask)
237 && __copy_from_user(&set.sig[1], frame->extramask,
238 sizeof(frame->extramask))))
241 sigdelsetmask(&set, ~_BLOCKABLE);
242 spin_lock_irq(¤t->sighand->siglock);
243 current->blocked = set;
245 spin_unlock_irq(¤t->sighand->siglock);
247 if (restore_sigcontext(regs, &frame->sc))
250 /* TODO: SIGTRAP when single-stepping as in arm ? */
255 force_sig(SIGSEGV, current);
259 /* Define dummy arguments to be able to reach the regs argument. */
261 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
262 long mof, long srp, struct pt_regs *regs)
264 struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
268 * Since we stacked the signal on a dword boundary,
269 * then frame should be dword aligned here. If it's
270 * not, then the user is trying to mess with us.
272 if (((long)frame) & 3)
275 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
277 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
280 sigdelsetmask(&set, ~_BLOCKABLE);
281 spin_lock_irq(¤t->sighand->siglock);
282 current->blocked = set;
284 spin_unlock_irq(¤t->sighand->siglock);
286 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
289 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
295 force_sig(SIGSEGV, current);
300 * Set up a signal frame.
304 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask)
307 unsigned long usp = rdusp();
309 /* copy the regs. they are first in sc so we can use sc directly */
311 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
313 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
314 the signal handler. The frametype will be restored to its previous
315 value in restore_sigcontext. */
316 regs->frametype = CRIS_FRAME_NORMAL;
318 /* then some other stuff */
320 err |= __put_user(mask, &sc->oldmask);
322 err |= __put_user(usp, &sc->usp);
327 /* figure out where we want to put the new signal frame - usually on the stack */
329 static inline void __user *
330 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
332 unsigned long sp = rdusp();
334 /* This is the X/Open sanctioned signal stack switching. */
335 if (ka->sa.sa_flags & SA_ONSTACK) {
336 if (! on_sig_stack(sp))
337 sp = current->sas_ss_sp + current->sas_ss_size;
340 /* make sure the frame is dword-aligned */
344 return (void __user*)(sp - frame_size);
347 /* grab and setup a signal frame.
349 * basically we stack a lot of state info, and arrange for the
350 * user-mode program to return to the kernel using either a
351 * trampoline which performs the syscall sigreturn, or a provided
352 * user-mode trampoline.
355 static void setup_frame(int sig, struct k_sigaction *ka,
356 sigset_t *set, struct pt_regs * regs)
358 struct sigframe __user *frame;
359 unsigned long return_ip;
362 frame = get_sigframe(ka, regs, sizeof(*frame));
364 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
367 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
371 if (_NSIG_WORDS > 1) {
372 err |= __copy_to_user(frame->extramask, &set->sig[1],
373 sizeof(frame->extramask));
378 /* Set up to return from userspace. If provided, use a stub
379 already in userspace. */
380 if (ka->sa.sa_flags & SA_RESTORER) {
381 return_ip = (unsigned long)ka->sa.sa_restorer;
383 /* trampoline - the desired return ip is the retcode itself */
384 return_ip = (unsigned long)&frame->retcode;
385 /* This is movu.w __NR_sigreturn, r9; break 13; */
386 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
387 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
388 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
394 /* Set up registers for signal handler */
396 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
397 regs->srp = return_ip; /* what we enter LATER */
398 regs->r10 = sig; /* first argument is signo */
400 /* actually move the usp to reflect the stacked frame */
402 wrusp((unsigned long)frame);
407 force_sigsegv(sig, current);
410 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
411 sigset_t *set, struct pt_regs * regs)
413 struct rt_sigframe __user *frame;
414 unsigned long return_ip;
417 frame = get_sigframe(ka, regs, sizeof(*frame));
419 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
422 err |= __put_user(&frame->info, &frame->pinfo);
423 err |= __put_user(&frame->uc, &frame->puc);
424 err |= copy_siginfo_to_user(&frame->info, info);
428 /* Clear all the bits of the ucontext we don't use. */
429 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
431 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
433 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
438 /* Set up to return from userspace. If provided, use a stub
439 already in userspace. */
440 if (ka->sa.sa_flags & SA_RESTORER) {
441 return_ip = (unsigned long)ka->sa.sa_restorer;
443 /* trampoline - the desired return ip is the retcode itself */
444 return_ip = (unsigned long)&frame->retcode;
445 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
446 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
447 err |= __put_user(__NR_rt_sigreturn, (short __user*)(frame->retcode+2));
448 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
454 /* TODO what is the current->exec_domain stuff and invmap ? */
456 /* Set up registers for signal handler */
458 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
459 regs->srp = return_ip; /* what we enter LATER */
460 regs->r10 = sig; /* first argument is signo */
461 regs->r11 = (unsigned long) &frame->info; /* second argument is (siginfo_t *) */
462 regs->r12 = 0; /* third argument is unused */
464 /* actually move the usp to reflect the stacked frame */
466 wrusp((unsigned long)frame);
471 force_sigsegv(sig, current);
475 * OK, we're invoking a handler
479 handle_signal(int canrestart, unsigned long sig,
480 siginfo_t *info, struct k_sigaction *ka,
481 sigset_t *oldset, struct pt_regs * regs)
483 /* Are we from a system call? */
485 /* If so, check system call restarting.. */
487 case -ERESTART_RESTARTBLOCK:
488 case -ERESTARTNOHAND:
489 /* ERESTARTNOHAND means that the syscall should only be
490 restarted if there was no handler for the signal, and since
491 we only get here if there is a handler, we don't restart */
496 /* ERESTARTSYS means to restart the syscall if there is no
497 handler or the handler was registered with SA_RESTART */
498 if (!(ka->sa.sa_flags & SA_RESTART)) {
503 case -ERESTARTNOINTR:
504 /* ERESTARTNOINTR means that the syscall should be called again
505 after the signal handler returns. */
506 RESTART_CRIS_SYS(regs);
510 /* Set up the stack frame */
511 if (ka->sa.sa_flags & SA_SIGINFO)
512 setup_rt_frame(sig, ka, info, oldset, regs);
514 setup_frame(sig, ka, oldset, regs);
516 if (ka->sa.sa_flags & SA_ONESHOT)
517 ka->sa.sa_handler = SIG_DFL;
519 spin_lock_irq(¤t->sighand->siglock);
520 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
521 if (!(ka->sa.sa_flags & SA_NODEFER))
522 sigaddset(¤t->blocked,sig);
524 spin_unlock_irq(¤t->sighand->siglock);
528 * Note that 'init' is a special process: it doesn't get signals it doesn't
529 * want to handle. Thus you cannot kill init even with a SIGKILL even by
532 * Also note that the regs structure given here as an argument, is the latest
533 * pushed pt_regs. It may or may not be the same as the first pushed registers
534 * when the initial usermode->kernelmode transition took place. Therefore
535 * we can use user_mode(regs) to see if we came directly from kernel or user
539 int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
543 struct k_sigaction ka;
546 * We want the common case to go fast, which
547 * is why we may in certain cases get here from
548 * kernel mode. Just return without doing anything
551 if (!user_mode(regs))
555 oldset = ¤t->blocked;
557 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
559 /* Whee! Actually deliver the signal. */
560 handle_signal(canrestart, signr, &info, &ka, oldset, regs);
564 /* Did we come from a system call? */
566 /* Restart the system call - no handlers present */
567 if (regs->r10 == -ERESTARTNOHAND ||
568 regs->r10 == -ERESTARTSYS ||
569 regs->r10 == -ERESTARTNOINTR) {
570 RESTART_CRIS_SYS(regs);
572 if (regs->r10 == -ERESTART_RESTARTBLOCK){
573 regs->r10 = __NR_restart_syscall;