RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / cris / arch-v10 / kernel / signal.c
blob41d4a5f93284abe34e1838c1e5379d1ab120956a
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, 2001 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 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.)
50 int
51 sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
52 long srp, struct pt_regs *regs)
54 sigset_t saveset;
56 mask &= _BLOCKABLE;
57 spin_lock_irq(&current->sighand->siglock);
58 saveset = current->blocked;
59 siginitset(&current->blocked, mask);
60 recalc_sigpending();
61 spin_unlock_irq(&current->sighand->siglock);
63 regs->r10 = -EINTR;
64 while (1) {
65 current->state = TASK_INTERRUPTIBLE;
66 schedule();
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;
74 always -EINTR. */
75 return regs->r10;
79 /* Define dummy arguments to be able to reach the regs argument. (Note that
80 * this arrangement relies on size_t occupying one register.)
82 int
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))
90 return -EINVAL;
92 if (copy_from_user(&newset, unewset, sizeof(newset)))
93 return -EFAULT;
94 sigdelsetmask(&newset, ~_BLOCKABLE);
96 spin_lock_irq(&current->sighand->siglock);
97 saveset = current->blocked;
98 current->blocked = newset;
99 recalc_sigpending();
100 spin_unlock_irq(&current->sighand->siglock);
102 regs->r10 = -EINTR;
103 while (1) {
104 current->state = TASK_INTERRUPTIBLE;
105 schedule();
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;
113 always -EINTR. */
114 return regs->r10;
118 int
119 sys_sigaction(int sig, const struct old_sigaction __user *act,
120 struct old_sigaction *oact)
122 struct k_sigaction new_ka, old_ka;
123 int ret;
125 if (act) {
126 old_sigset_t mask;
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))
130 return -EFAULT;
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);
138 if (!ret && oact) {
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))
142 return -EFAULT;
143 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
144 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
147 return ret;
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.
161 struct sigframe {
162 struct sigcontext sc;
163 unsigned long extramask[_NSIG_WORDS-1];
164 unsigned char retcode[8]; /* trampoline code */
167 struct rt_sigframe {
168 struct siginfo *pinfo;
169 void *puc;
170 struct siginfo info;
171 struct ucontext uc;
172 unsigned char retcode[8]; /* trampoline code */
176 static int
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)))
191 goto badframe;
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);
204 wrusp(old_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
208 * use it now ?
211 return err;
213 badframe:
214 return 1;
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();
223 sigset_t set;
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)
231 goto badframe;
233 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
234 goto badframe;
235 if (__get_user(set.sig[0], &frame->sc.oldmask)
236 || (_NSIG_WORDS > 1
237 && __copy_from_user(&set.sig[1], frame->extramask,
238 sizeof(frame->extramask))))
239 goto badframe;
241 sigdelsetmask(&set, ~_BLOCKABLE);
242 spin_lock_irq(&current->sighand->siglock);
243 current->blocked = set;
244 recalc_sigpending();
245 spin_unlock_irq(&current->sighand->siglock);
247 if (restore_sigcontext(regs, &frame->sc))
248 goto badframe;
250 /* TODO: SIGTRAP when single-stepping as in arm ? */
252 return regs->r10;
254 badframe:
255 force_sig(SIGSEGV, current);
256 return 0;
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();
265 sigset_t set;
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)
273 goto badframe;
275 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
276 goto badframe;
277 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
278 goto badframe;
280 sigdelsetmask(&set, ~_BLOCKABLE);
281 spin_lock_irq(&current->sighand->siglock);
282 current->blocked = set;
283 recalc_sigpending();
284 spin_unlock_irq(&current->sighand->siglock);
286 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
287 goto badframe;
289 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
290 goto badframe;
292 return regs->r10;
294 badframe:
295 force_sig(SIGSEGV, current);
296 return 0;
300 * Set up a signal frame.
303 static int
304 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask)
306 int err = 0;
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);
324 return err;
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 */
342 sp &= ~3;
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;
360 int err = 0;
362 frame = get_sigframe(ka, regs, sizeof(*frame));
364 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
365 goto give_sigsegv;
367 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
368 if (err)
369 goto give_sigsegv;
371 if (_NSIG_WORDS > 1) {
372 err |= __copy_to_user(frame->extramask, &set->sig[1],
373 sizeof(frame->extramask));
375 if (err)
376 goto give_sigsegv;
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;
382 } else {
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));
391 if (err)
392 goto give_sigsegv;
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);
404 return;
406 give_sigsegv:
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;
415 int err = 0;
417 frame = get_sigframe(ka, regs, sizeof(*frame));
419 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
420 goto give_sigsegv;
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);
425 if (err)
426 goto give_sigsegv;
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));
435 if (err)
436 goto give_sigsegv;
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;
442 } else {
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));
451 if (err)
452 goto give_sigsegv;
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);
468 return;
470 give_sigsegv:
471 force_sigsegv(sig, current);
475 * OK, we're invoking a handler
478 static inline void
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? */
484 if (canrestart) {
485 /* If so, check system call restarting.. */
486 switch (regs->r10) {
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 */
492 regs->r10 = -EINTR;
493 break;
495 case -ERESTARTSYS:
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)) {
499 regs->r10 = -EINTR;
500 break;
502 /* fallthrough */
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);
513 else
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(&current->sighand->siglock);
520 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
521 if (!(ka->sa.sa_flags & SA_NODEFER))
522 sigaddset(&current->blocked,sig);
523 recalc_sigpending();
524 spin_unlock_irq(&current->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
530 * mistake.
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
536 * mode below.
539 int do_signal(int canrestart, sigset_t *oldset, struct pt_regs *regs)
541 siginfo_t info;
542 int signr;
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
549 * if so.
551 if (!user_mode(regs))
552 return 1;
554 if (!oldset)
555 oldset = &current->blocked;
557 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
558 if (signr > 0) {
559 /* Whee! Actually deliver the signal. */
560 handle_signal(canrestart, signr, &info, &ka, oldset, regs);
561 return 1;
564 /* Did we come from a system call? */
565 if (canrestart) {
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;
574 regs->irp -= 2;
577 return 0;