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-2007 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>
30 #include <arch/system.h>
34 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
35 /* manipulate regs so that upon return, it will be re-executed */
37 /* We rely on that pc points to the instruction after "break 13", so the
38 * library must never do strange things like putting it in a delay slot.
40 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
42 void do_signal(int canrestart, struct pt_regs *regs);
45 * Atomically swap in the new signal mask, and wait for a signal. Define
46 * dummy arguments to be able to reach the regs argument. (Note that this
47 * arrangement relies on old_sigset_t occupying one register.)
49 int sys_sigsuspend(old_sigset_t mask)
52 siginitset(&blocked, mask);
53 return sigsuspend(&blocked);
56 int sys_sigaction(int sig, const struct old_sigaction __user *act,
57 struct old_sigaction *oact)
59 struct k_sigaction new_ka, old_ka;
64 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
65 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
66 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
67 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
68 __get_user(mask, &act->sa_mask))
70 siginitset(&new_ka.sa.sa_mask, mask);
73 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
76 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
77 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
78 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
79 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
80 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
87 int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
89 return do_sigaltstack(uss, uoss, rdusp());
94 * Do a signal return; undo the signal stack.
99 unsigned long extramask[_NSIG_WORDS-1];
100 unsigned char retcode[8]; /* trampoline code */
104 struct siginfo *pinfo;
108 unsigned char retcode[8]; /* trampoline code */
113 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
115 unsigned int err = 0;
116 unsigned long old_usp;
118 /* Always make any pending restarted system calls return -EINTR */
119 current_thread_info()->restart_block.fn = do_no_restart_syscall;
121 /* restore the regs from &sc->regs (same as sc, since regs is first)
122 * (sc is already checked for VERIFY_READ since the sigframe was
123 * checked in sys_sigreturn previously)
126 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
129 /* make sure the U-flag is set so user-mode cannot fool us */
131 regs->dccr |= 1 << 8;
133 /* restore the old USP as it was before we stacked the sc etc.
134 * (we cannot just pop the sigcontext since we aligned the sp and
135 * stuff after pushing it)
138 err |= __get_user(old_usp, &sc->usp);
142 /* TODO: the other ports use regs->orig_XX to disable syscall checks
143 * after this completes, but we don't use that mechanism. maybe we can
153 /* Define dummy arguments to be able to reach the regs argument. */
155 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
156 long srp, struct pt_regs *regs)
158 struct sigframe __user *frame = (struct sigframe *)rdusp();
162 * Since we stacked the signal on a dword boundary,
163 * then frame should be dword aligned here. If it's
164 * not, then the user is trying to mess with us.
166 if (((long)frame) & 3)
169 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
171 if (__get_user(set.sig[0], &frame->sc.oldmask)
173 && __copy_from_user(&set.sig[1], frame->extramask,
174 sizeof(frame->extramask))))
177 set_current_blocked(&set);
179 if (restore_sigcontext(regs, &frame->sc))
182 /* TODO: SIGTRAP when single-stepping as in arm ? */
187 force_sig(SIGSEGV, current);
191 /* Define dummy arguments to be able to reach the regs argument. */
193 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
194 long mof, long srp, struct pt_regs *regs)
196 struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
200 * Since we stacked the signal on a dword boundary,
201 * then frame should be dword aligned here. If it's
202 * not, then the user is trying to mess with us.
204 if (((long)frame) & 3)
207 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
209 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
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)
223 force_sig(SIGSEGV, current);
228 * Set up a signal frame.
231 static int setup_sigcontext(struct sigcontext __user *sc,
232 struct pt_regs *regs, unsigned long mask)
235 unsigned long usp = rdusp();
237 /* copy the regs. they are first in sc so we can use sc directly */
239 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
241 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
242 the signal handler. The frametype will be restored to its previous
243 value in restore_sigcontext. */
244 regs->frametype = CRIS_FRAME_NORMAL;
246 /* then some other stuff */
248 err |= __put_user(mask, &sc->oldmask);
250 err |= __put_user(usp, &sc->usp);
255 /* Figure out where we want to put the new signal frame
256 * - usually on the stack. */
258 static inline void __user *
259 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
261 unsigned long sp = rdusp();
263 /* This is the X/Open sanctioned signal stack switching. */
264 if (ka->sa.sa_flags & SA_ONSTACK) {
265 if (! on_sig_stack(sp))
266 sp = current->sas_ss_sp + current->sas_ss_size;
269 /* make sure the frame is dword-aligned */
273 return (void __user*)(sp - frame_size);
276 /* grab and setup a signal frame.
278 * basically we stack a lot of state info, and arrange for the
279 * user-mode program to return to the kernel using either a
280 * trampoline which performs the syscall sigreturn, or a provided
281 * user-mode trampoline.
284 static int setup_frame(int sig, struct k_sigaction *ka,
285 sigset_t *set, struct pt_regs *regs)
287 struct sigframe __user *frame;
288 unsigned long return_ip;
291 frame = get_sigframe(ka, regs, sizeof(*frame));
293 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
296 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
300 if (_NSIG_WORDS > 1) {
301 err |= __copy_to_user(frame->extramask, &set->sig[1],
302 sizeof(frame->extramask));
307 /* Set up to return from userspace. If provided, use a stub
308 already in userspace. */
309 if (ka->sa.sa_flags & SA_RESTORER) {
310 return_ip = (unsigned long)ka->sa.sa_restorer;
312 /* trampoline - the desired return ip is the retcode itself */
313 return_ip = (unsigned long)&frame->retcode;
314 /* This is movu.w __NR_sigreturn, r9; break 13; */
315 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
316 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
317 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
323 /* Set up registers for signal handler */
325 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
326 regs->srp = return_ip; /* what we enter LATER */
327 regs->r10 = sig; /* first argument is signo */
329 /* actually move the usp to reflect the stacked frame */
331 wrusp((unsigned long)frame);
336 force_sigsegv(sig, current);
340 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
341 sigset_t *set, struct pt_regs *regs)
343 struct rt_sigframe __user *frame;
344 unsigned long return_ip;
347 frame = get_sigframe(ka, regs, sizeof(*frame));
349 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
352 err |= __put_user(&frame->info, &frame->pinfo);
353 err |= __put_user(&frame->uc, &frame->puc);
354 err |= copy_siginfo_to_user(&frame->info, info);
358 /* Clear all the bits of the ucontext we don't use. */
359 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
361 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
363 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
368 /* Set up to return from userspace. If provided, use a stub
369 already in userspace. */
370 if (ka->sa.sa_flags & SA_RESTORER) {
371 return_ip = (unsigned long)ka->sa.sa_restorer;
373 /* trampoline - the desired return ip is the retcode itself */
374 return_ip = (unsigned long)&frame->retcode;
375 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
376 err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
377 err |= __put_user(__NR_rt_sigreturn,
378 (short __user *)(frame->retcode+2));
379 err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
385 /* TODO what is the current->exec_domain stuff and invmap ? */
387 /* Set up registers for signal handler */
389 /* What we enter NOW */
390 regs->irp = (unsigned long) ka->sa.sa_handler;
391 /* What we enter LATER */
392 regs->srp = return_ip;
393 /* First argument is signo */
395 /* Second argument is (siginfo_t *) */
396 regs->r11 = (unsigned long)&frame->info;
397 /* Third argument is unused */
400 /* Actually move the usp to reflect the stacked frame */
401 wrusp((unsigned long)frame);
406 force_sigsegv(sig, current);
411 * OK, we're invoking a handler
414 static inline void handle_signal(int canrestart, unsigned long sig,
415 siginfo_t *info, struct k_sigaction *ka,
416 struct pt_regs *regs)
418 sigset_t *oldset = sigmask_to_save();
421 /* Are we from a system call? */
423 /* If so, check system call restarting.. */
425 case -ERESTART_RESTARTBLOCK:
426 case -ERESTARTNOHAND:
427 /* ERESTARTNOHAND means that the syscall should
428 * only be restarted if there was no handler for
429 * the signal, and since we only get here if there
430 * is a handler, we don't restart */
434 /* ERESTARTSYS means to restart the syscall if
435 * there is no handler or the handler was
436 * registered with SA_RESTART */
437 if (!(ka->sa.sa_flags & SA_RESTART)) {
442 case -ERESTARTNOINTR:
443 /* ERESTARTNOINTR means that the syscall should
444 * be called again after the signal handler returns. */
445 RESTART_CRIS_SYS(regs);
449 /* Set up the stack frame */
450 if (ka->sa.sa_flags & SA_SIGINFO)
451 ret = setup_rt_frame(sig, ka, info, oldset, regs);
453 ret = setup_frame(sig, ka, oldset, regs);
456 signal_delivered(sig, info, ka, regs, 0);
460 * Note that 'init' is a special process: it doesn't get signals it doesn't
461 * want to handle. Thus you cannot kill init even with a SIGKILL even by
464 * Also note that the regs structure given here as an argument, is the latest
465 * pushed pt_regs. It may or may not be the same as the first pushed registers
466 * when the initial usermode->kernelmode transition took place. Therefore
467 * we can use user_mode(regs) to see if we came directly from kernel or user
471 void do_signal(int canrestart, struct pt_regs *regs)
475 struct k_sigaction ka;
478 * We want the common case to go fast, which
479 * is why we may in certain cases get here from
480 * kernel mode. Just return without doing anything
483 if (!user_mode(regs))
486 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
488 /* Whee! Actually deliver the signal. */
489 handle_signal(canrestart, signr, &info, &ka, regs);
493 /* Did we come from a system call? */
495 /* Restart the system call - no handlers present */
496 if (regs->r10 == -ERESTARTNOHAND ||
497 regs->r10 == -ERESTARTSYS ||
498 regs->r10 == -ERESTARTNOINTR) {
499 RESTART_CRIS_SYS(regs);
501 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
502 regs->r9 = __NR_restart_syscall;
507 /* if there's no signal to deliver, we just put the saved sigmask
509 restore_saved_sigmask();