Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / cris / arch-v10 / kernel / signal.c
bloba6e1ff0c48ff441932861f1159e38e02107163d6
1 /*
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>
17 #include <linux/mm.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>
31 #define DEBUG_SIG 0
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 void do_signal(int canrestart, 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.)
50 int sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
51 long srp, struct pt_regs *regs)
53 sigset_t blocked;
55 current->saved_sigmask = current->blocked;
57 mask &= _BLOCKABLE;
58 siginitset(&blocked, mask);
59 set_current_blocked(&blocked);
61 current->state = TASK_INTERRUPTIBLE;
62 schedule();
63 set_thread_flag(TIF_RESTORE_SIGMASK);
64 return -ERESTARTNOHAND;
67 int sys_sigaction(int sig, const struct old_sigaction __user *act,
68 struct old_sigaction *oact)
70 struct k_sigaction new_ka, old_ka;
71 int ret;
73 if (act) {
74 old_sigset_t mask;
75 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
76 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
77 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
78 return -EFAULT;
79 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
80 __get_user(mask, &act->sa_mask);
81 siginitset(&new_ka.sa.sa_mask, mask);
84 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
86 if (!ret && oact) {
87 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
88 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
89 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
90 return -EFAULT;
91 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
92 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
95 return ret;
98 int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
100 return do_sigaltstack(uss, uoss, rdusp());
105 * Do a signal return; undo the signal stack.
108 struct sigframe {
109 struct sigcontext sc;
110 unsigned long extramask[_NSIG_WORDS-1];
111 unsigned char retcode[8]; /* trampoline code */
114 struct rt_sigframe {
115 struct siginfo *pinfo;
116 void *puc;
117 struct siginfo info;
118 struct ucontext uc;
119 unsigned char retcode[8]; /* trampoline code */
123 static int
124 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
126 unsigned int err = 0;
127 unsigned long old_usp;
129 /* Always make any pending restarted system calls return -EINTR */
130 current_thread_info()->restart_block.fn = do_no_restart_syscall;
132 /* restore the regs from &sc->regs (same as sc, since regs is first)
133 * (sc is already checked for VERIFY_READ since the sigframe was
134 * checked in sys_sigreturn previously)
137 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
138 goto badframe;
140 /* make sure the U-flag is set so user-mode cannot fool us */
142 regs->dccr |= 1 << 8;
144 /* restore the old USP as it was before we stacked the sc etc.
145 * (we cannot just pop the sigcontext since we aligned the sp and
146 * stuff after pushing it)
149 err |= __get_user(old_usp, &sc->usp);
151 wrusp(old_usp);
153 /* TODO: the other ports use regs->orig_XX to disable syscall checks
154 * after this completes, but we don't use that mechanism. maybe we can
155 * use it now ?
158 return err;
160 badframe:
161 return 1;
164 /* Define dummy arguments to be able to reach the regs argument. */
166 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
167 long srp, struct pt_regs *regs)
169 struct sigframe __user *frame = (struct sigframe *)rdusp();
170 sigset_t set;
173 * Since we stacked the signal on a dword boundary,
174 * then frame should be dword aligned here. If it's
175 * not, then the user is trying to mess with us.
177 if (((long)frame) & 3)
178 goto badframe;
180 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
181 goto badframe;
182 if (__get_user(set.sig[0], &frame->sc.oldmask)
183 || (_NSIG_WORDS > 1
184 && __copy_from_user(&set.sig[1], frame->extramask,
185 sizeof(frame->extramask))))
186 goto badframe;
188 sigdelsetmask(&set, ~_BLOCKABLE);
189 set_current_blocked(&set);
191 if (restore_sigcontext(regs, &frame->sc))
192 goto badframe;
194 /* TODO: SIGTRAP when single-stepping as in arm ? */
196 return regs->r10;
198 badframe:
199 force_sig(SIGSEGV, current);
200 return 0;
203 /* Define dummy arguments to be able to reach the regs argument. */
205 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
206 long mof, long srp, struct pt_regs *regs)
208 struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
209 sigset_t set;
212 * Since we stacked the signal on a dword boundary,
213 * then frame should be dword aligned here. If it's
214 * not, then the user is trying to mess with us.
216 if (((long)frame) & 3)
217 goto badframe;
219 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
220 goto badframe;
221 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
222 goto badframe;
224 sigdelsetmask(&set, ~_BLOCKABLE);
225 set_current_blocked(&set);
227 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
228 goto badframe;
230 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
231 goto badframe;
233 return regs->r10;
235 badframe:
236 force_sig(SIGSEGV, current);
237 return 0;
241 * Set up a signal frame.
244 static int setup_sigcontext(struct sigcontext __user *sc,
245 struct pt_regs *regs, unsigned long mask)
247 int err = 0;
248 unsigned long usp = rdusp();
250 /* copy the regs. they are first in sc so we can use sc directly */
252 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
254 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
255 the signal handler. The frametype will be restored to its previous
256 value in restore_sigcontext. */
257 regs->frametype = CRIS_FRAME_NORMAL;
259 /* then some other stuff */
261 err |= __put_user(mask, &sc->oldmask);
263 err |= __put_user(usp, &sc->usp);
265 return err;
268 /* Figure out where we want to put the new signal frame
269 * - usually on the stack. */
271 static inline void __user *
272 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
274 unsigned long sp = rdusp();
276 /* This is the X/Open sanctioned signal stack switching. */
277 if (ka->sa.sa_flags & SA_ONSTACK) {
278 if (! on_sig_stack(sp))
279 sp = current->sas_ss_sp + current->sas_ss_size;
282 /* make sure the frame is dword-aligned */
284 sp &= ~3;
286 return (void __user*)(sp - frame_size);
289 /* grab and setup a signal frame.
291 * basically we stack a lot of state info, and arrange for the
292 * user-mode program to return to the kernel using either a
293 * trampoline which performs the syscall sigreturn, or a provided
294 * user-mode trampoline.
297 static int setup_frame(int sig, struct k_sigaction *ka,
298 sigset_t *set, struct pt_regs *regs)
300 struct sigframe __user *frame;
301 unsigned long return_ip;
302 int err = 0;
304 frame = get_sigframe(ka, regs, sizeof(*frame));
306 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
307 goto give_sigsegv;
309 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
310 if (err)
311 goto give_sigsegv;
313 if (_NSIG_WORDS > 1) {
314 err |= __copy_to_user(frame->extramask, &set->sig[1],
315 sizeof(frame->extramask));
317 if (err)
318 goto give_sigsegv;
320 /* Set up to return from userspace. If provided, use a stub
321 already in userspace. */
322 if (ka->sa.sa_flags & SA_RESTORER) {
323 return_ip = (unsigned long)ka->sa.sa_restorer;
324 } else {
325 /* trampoline - the desired return ip is the retcode itself */
326 return_ip = (unsigned long)&frame->retcode;
327 /* This is movu.w __NR_sigreturn, r9; break 13; */
328 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
329 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
330 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
333 if (err)
334 goto give_sigsegv;
336 /* Set up registers for signal handler */
338 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
339 regs->srp = return_ip; /* what we enter LATER */
340 regs->r10 = sig; /* first argument is signo */
342 /* actually move the usp to reflect the stacked frame */
344 wrusp((unsigned long)frame);
346 return 0;
348 give_sigsegv:
349 force_sigsegv(sig, current);
350 return -EFAULT;
353 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
354 sigset_t *set, struct pt_regs *regs)
356 struct rt_sigframe __user *frame;
357 unsigned long return_ip;
358 int err = 0;
360 frame = get_sigframe(ka, regs, sizeof(*frame));
362 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
363 goto give_sigsegv;
365 err |= __put_user(&frame->info, &frame->pinfo);
366 err |= __put_user(&frame->uc, &frame->puc);
367 err |= copy_siginfo_to_user(&frame->info, info);
368 if (err)
369 goto give_sigsegv;
371 /* Clear all the bits of the ucontext we don't use. */
372 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
374 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
376 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
378 if (err)
379 goto give_sigsegv;
381 /* Set up to return from userspace. If provided, use a stub
382 already in userspace. */
383 if (ka->sa.sa_flags & SA_RESTORER) {
384 return_ip = (unsigned long)ka->sa.sa_restorer;
385 } else {
386 /* trampoline - the desired return ip is the retcode itself */
387 return_ip = (unsigned long)&frame->retcode;
388 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
389 err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
390 err |= __put_user(__NR_rt_sigreturn,
391 (short __user *)(frame->retcode+2));
392 err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
395 if (err)
396 goto give_sigsegv;
398 /* TODO what is the current->exec_domain stuff and invmap ? */
400 /* Set up registers for signal handler */
402 /* What we enter NOW */
403 regs->irp = (unsigned long) ka->sa.sa_handler;
404 /* What we enter LATER */
405 regs->srp = return_ip;
406 /* First argument is signo */
407 regs->r10 = sig;
408 /* Second argument is (siginfo_t *) */
409 regs->r11 = (unsigned long)&frame->info;
410 /* Third argument is unused */
411 regs->r12 = 0;
413 /* Actually move the usp to reflect the stacked frame */
414 wrusp((unsigned long)frame);
416 return 0;
418 give_sigsegv:
419 force_sigsegv(sig, current);
420 return -EFAULT;
424 * OK, we're invoking a handler
427 static inline int handle_signal(int canrestart, unsigned long sig,
428 siginfo_t *info, struct k_sigaction *ka,
429 sigset_t *oldset, struct pt_regs *regs)
431 int ret;
433 /* Are we from a system call? */
434 if (canrestart) {
435 /* If so, check system call restarting.. */
436 switch (regs->r10) {
437 case -ERESTART_RESTARTBLOCK:
438 case -ERESTARTNOHAND:
439 /* ERESTARTNOHAND means that the syscall should
440 * only be restarted if there was no handler for
441 * the signal, and since we only get here if there
442 * is a handler, we don't restart */
443 regs->r10 = -EINTR;
444 break;
445 case -ERESTARTSYS:
446 /* ERESTARTSYS means to restart the syscall if
447 * there is no handler or the handler was
448 * registered with SA_RESTART */
449 if (!(ka->sa.sa_flags & SA_RESTART)) {
450 regs->r10 = -EINTR;
451 break;
453 /* fallthrough */
454 case -ERESTARTNOINTR:
455 /* ERESTARTNOINTR means that the syscall should
456 * be called again after the signal handler returns. */
457 RESTART_CRIS_SYS(regs);
461 /* Set up the stack frame */
462 if (ka->sa.sa_flags & SA_SIGINFO)
463 ret = setup_rt_frame(sig, ka, info, oldset, regs);
464 else
465 ret = setup_frame(sig, ka, oldset, regs);
467 if (ret == 0)
468 block_sigmask(ka, sig);
470 return ret;
474 * Note that 'init' is a special process: it doesn't get signals it doesn't
475 * want to handle. Thus you cannot kill init even with a SIGKILL even by
476 * mistake.
478 * Also note that the regs structure given here as an argument, is the latest
479 * pushed pt_regs. It may or may not be the same as the first pushed registers
480 * when the initial usermode->kernelmode transition took place. Therefore
481 * we can use user_mode(regs) to see if we came directly from kernel or user
482 * mode below.
485 void do_signal(int canrestart, struct pt_regs *regs)
487 siginfo_t info;
488 int signr;
489 struct k_sigaction ka;
490 sigset_t *oldset;
493 * We want the common case to go fast, which
494 * is why we may in certain cases get here from
495 * kernel mode. Just return without doing anything
496 * if so.
498 if (!user_mode(regs))
499 return;
501 if (test_thread_flag(TIF_RESTORE_SIGMASK))
502 oldset = &current->saved_sigmask;
503 else
504 oldset = &current->blocked;
506 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
507 if (signr > 0) {
508 /* Whee! Actually deliver the signal. */
509 if (handle_signal(canrestart, signr, &info, &ka,
510 oldset, regs)) {
511 /* a signal was successfully delivered; the saved
512 * sigmask will have been stored in the signal frame,
513 * and will be restored by sigreturn, so we can simply
514 * clear the TIF_RESTORE_SIGMASK flag */
515 if (test_thread_flag(TIF_RESTORE_SIGMASK))
516 clear_thread_flag(TIF_RESTORE_SIGMASK);
518 return;
521 /* Did we come from a system call? */
522 if (canrestart) {
523 /* Restart the system call - no handlers present */
524 if (regs->r10 == -ERESTARTNOHAND ||
525 regs->r10 == -ERESTARTSYS ||
526 regs->r10 == -ERESTARTNOINTR) {
527 RESTART_CRIS_SYS(regs);
529 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
530 regs->r9 = __NR_restart_syscall;
531 regs->irp -= 2;
535 /* if there's no signal to deliver, we just put the saved sigmask
536 * back */
537 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
538 clear_thread_flag(TIF_RESTORE_SIGMASK);
539 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);