most of set_current_blocked() callers want SIGKILL/SIGSTOP removed from set
[linux-2.6.git] / arch / cris / arch-v10 / kernel / signal.c
blobcf6380cb9a57a011355be4484ae6b8e1b800ed68
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>
30 #include <arch/system.h>
32 #define DEBUG_SIG 0
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)
51 sigset_t blocked;
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;
60 int ret;
62 if (act) {
63 old_sigset_t mask;
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))
69 return -EFAULT;
70 siginitset(&new_ka.sa.sa_mask, mask);
73 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
75 if (!ret && oact) {
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))
81 return -EFAULT;
84 return ret;
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.
97 struct sigframe {
98 struct sigcontext sc;
99 unsigned long extramask[_NSIG_WORDS-1];
100 unsigned char retcode[8]; /* trampoline code */
103 struct rt_sigframe {
104 struct siginfo *pinfo;
105 void *puc;
106 struct siginfo info;
107 struct ucontext uc;
108 unsigned char retcode[8]; /* trampoline code */
112 static int
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)))
127 goto badframe;
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);
140 wrusp(old_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
144 * use it now ?
147 return err;
149 badframe:
150 return 1;
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();
159 sigset_t set;
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)
167 goto badframe;
169 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
170 goto badframe;
171 if (__get_user(set.sig[0], &frame->sc.oldmask)
172 || (_NSIG_WORDS > 1
173 && __copy_from_user(&set.sig[1], frame->extramask,
174 sizeof(frame->extramask))))
175 goto badframe;
177 set_current_blocked(&set);
179 if (restore_sigcontext(regs, &frame->sc))
180 goto badframe;
182 /* TODO: SIGTRAP when single-stepping as in arm ? */
184 return regs->r10;
186 badframe:
187 force_sig(SIGSEGV, current);
188 return 0;
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();
197 sigset_t set;
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)
205 goto badframe;
207 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
208 goto badframe;
209 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
210 goto badframe;
212 set_current_blocked(&set);
214 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
215 goto badframe;
217 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
218 goto badframe;
220 return regs->r10;
222 badframe:
223 force_sig(SIGSEGV, current);
224 return 0;
228 * Set up a signal frame.
231 static int setup_sigcontext(struct sigcontext __user *sc,
232 struct pt_regs *regs, unsigned long mask)
234 int err = 0;
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);
252 return err;
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 */
271 sp &= ~3;
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;
289 int err = 0;
291 frame = get_sigframe(ka, regs, sizeof(*frame));
293 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
294 goto give_sigsegv;
296 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
297 if (err)
298 goto give_sigsegv;
300 if (_NSIG_WORDS > 1) {
301 err |= __copy_to_user(frame->extramask, &set->sig[1],
302 sizeof(frame->extramask));
304 if (err)
305 goto give_sigsegv;
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;
311 } else {
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));
320 if (err)
321 goto give_sigsegv;
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);
333 return 0;
335 give_sigsegv:
336 force_sigsegv(sig, current);
337 return -EFAULT;
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;
345 int err = 0;
347 frame = get_sigframe(ka, regs, sizeof(*frame));
349 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
350 goto give_sigsegv;
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);
355 if (err)
356 goto give_sigsegv;
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));
365 if (err)
366 goto give_sigsegv;
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;
372 } else {
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));
382 if (err)
383 goto give_sigsegv;
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 */
394 regs->r10 = sig;
395 /* Second argument is (siginfo_t *) */
396 regs->r11 = (unsigned long)&frame->info;
397 /* Third argument is unused */
398 regs->r12 = 0;
400 /* Actually move the usp to reflect the stacked frame */
401 wrusp((unsigned long)frame);
403 return 0;
405 give_sigsegv:
406 force_sigsegv(sig, current);
407 return -EFAULT;
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();
419 int ret;
421 /* Are we from a system call? */
422 if (canrestart) {
423 /* If so, check system call restarting.. */
424 switch (regs->r10) {
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 */
431 regs->r10 = -EINTR;
432 break;
433 case -ERESTARTSYS:
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)) {
438 regs->r10 = -EINTR;
439 break;
441 /* fallthrough */
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);
452 else
453 ret = setup_frame(sig, ka, oldset, regs);
455 if (ret == 0)
456 block_sigmask(ka, sig);
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
462 * mistake.
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
468 * mode below.
471 void do_signal(int canrestart, struct pt_regs *regs)
473 siginfo_t info;
474 int signr;
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
481 * if so.
483 if (!user_mode(regs))
484 return;
486 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
487 if (signr > 0) {
488 /* Whee! Actually deliver the signal. */
489 handle_signal(canrestart, signr, &info, &ka, regs);
490 return;
493 /* Did we come from a system call? */
494 if (canrestart) {
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;
503 regs->irp -= 2;
507 /* if there's no signal to deliver, we just put the saved sigmask
508 * back */
509 restore_saved_sigmask();