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 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
37 /* manipulate regs so that upon return, it will be re-executed */
39 /* We rely on that pc points to the instruction after "break 13", so the
40 * library must never do strange things like putting it in a delay slot.
42 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
44 void do_signal(int canrestart, struct pt_regs *regs);
47 * Atomically swap in the new signal mask, and wait for a signal. Define
48 * dummy arguments to be able to reach the regs argument. (Note that this
49 * arrangement relies on old_sigset_t occupying one register.)
51 int sys_sigsuspend(old_sigset_t mask)
54 siginitset(&blocked, mask);
55 return sigsuspend(&blocked);
58 int sys_sigaction(int sig, const struct old_sigaction __user *act,
59 struct old_sigaction *oact)
61 struct k_sigaction new_ka, old_ka;
66 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
67 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
68 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
69 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
70 __get_user(mask, &act->sa_mask))
72 siginitset(&new_ka.sa.sa_mask, mask);
75 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
78 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
79 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
80 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
81 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
82 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
89 int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
91 return do_sigaltstack(uss, uoss, rdusp());
96 * Do a signal return; undo the signal stack.
100 struct sigcontext sc;
101 unsigned long extramask[_NSIG_WORDS-1];
102 unsigned char retcode[8]; /* trampoline code */
106 struct siginfo *pinfo;
110 unsigned char retcode[8]; /* trampoline code */
115 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
117 unsigned int err = 0;
118 unsigned long old_usp;
120 /* Always make any pending restarted system calls return -EINTR */
121 current_thread_info()->restart_block.fn = do_no_restart_syscall;
123 /* restore the regs from &sc->regs (same as sc, since regs is first)
124 * (sc is already checked for VERIFY_READ since the sigframe was
125 * checked in sys_sigreturn previously)
128 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
131 /* make sure the U-flag is set so user-mode cannot fool us */
133 regs->dccr |= 1 << 8;
135 /* restore the old USP as it was before we stacked the sc etc.
136 * (we cannot just pop the sigcontext since we aligned the sp and
137 * stuff after pushing it)
140 err |= __get_user(old_usp, &sc->usp);
144 /* TODO: the other ports use regs->orig_XX to disable syscall checks
145 * after this completes, but we don't use that mechanism. maybe we can
155 /* Define dummy arguments to be able to reach the regs argument. */
157 asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
158 long srp, struct pt_regs *regs)
160 struct sigframe __user *frame = (struct sigframe *)rdusp();
164 * Since we stacked the signal on a dword boundary,
165 * then frame should be dword aligned here. If it's
166 * not, then the user is trying to mess with us.
168 if (((long)frame) & 3)
171 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
173 if (__get_user(set.sig[0], &frame->sc.oldmask)
175 && __copy_from_user(&set.sig[1], frame->extramask,
176 sizeof(frame->extramask))))
179 sigdelsetmask(&set, ~_BLOCKABLE);
180 set_current_blocked(&set);
182 if (restore_sigcontext(regs, &frame->sc))
185 /* TODO: SIGTRAP when single-stepping as in arm ? */
190 force_sig(SIGSEGV, current);
194 /* Define dummy arguments to be able to reach the regs argument. */
196 asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
197 long mof, long srp, struct pt_regs *regs)
199 struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
203 * Since we stacked the signal on a dword boundary,
204 * then frame should be dword aligned here. If it's
205 * not, then the user is trying to mess with us.
207 if (((long)frame) & 3)
210 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
212 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
215 sigdelsetmask(&set, ~_BLOCKABLE);
216 set_current_blocked(&set);
218 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
221 if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
227 force_sig(SIGSEGV, current);
232 * Set up a signal frame.
235 static int setup_sigcontext(struct sigcontext __user *sc,
236 struct pt_regs *regs, unsigned long mask)
239 unsigned long usp = rdusp();
241 /* copy the regs. they are first in sc so we can use sc directly */
243 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
245 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
246 the signal handler. The frametype will be restored to its previous
247 value in restore_sigcontext. */
248 regs->frametype = CRIS_FRAME_NORMAL;
250 /* then some other stuff */
252 err |= __put_user(mask, &sc->oldmask);
254 err |= __put_user(usp, &sc->usp);
259 /* Figure out where we want to put the new signal frame
260 * - usually on the stack. */
262 static inline void __user *
263 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
265 unsigned long sp = rdusp();
267 /* This is the X/Open sanctioned signal stack switching. */
268 if (ka->sa.sa_flags & SA_ONSTACK) {
269 if (! on_sig_stack(sp))
270 sp = current->sas_ss_sp + current->sas_ss_size;
273 /* make sure the frame is dword-aligned */
277 return (void __user*)(sp - frame_size);
280 /* grab and setup a signal frame.
282 * basically we stack a lot of state info, and arrange for the
283 * user-mode program to return to the kernel using either a
284 * trampoline which performs the syscall sigreturn, or a provided
285 * user-mode trampoline.
288 static int setup_frame(int sig, struct k_sigaction *ka,
289 sigset_t *set, struct pt_regs *regs)
291 struct sigframe __user *frame;
292 unsigned long return_ip;
295 frame = get_sigframe(ka, regs, sizeof(*frame));
297 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
300 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
304 if (_NSIG_WORDS > 1) {
305 err |= __copy_to_user(frame->extramask, &set->sig[1],
306 sizeof(frame->extramask));
311 /* Set up to return from userspace. If provided, use a stub
312 already in userspace. */
313 if (ka->sa.sa_flags & SA_RESTORER) {
314 return_ip = (unsigned long)ka->sa.sa_restorer;
316 /* trampoline - the desired return ip is the retcode itself */
317 return_ip = (unsigned long)&frame->retcode;
318 /* This is movu.w __NR_sigreturn, r9; break 13; */
319 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
320 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
321 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
327 /* Set up registers for signal handler */
329 regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
330 regs->srp = return_ip; /* what we enter LATER */
331 regs->r10 = sig; /* first argument is signo */
333 /* actually move the usp to reflect the stacked frame */
335 wrusp((unsigned long)frame);
340 force_sigsegv(sig, current);
344 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
345 sigset_t *set, struct pt_regs *regs)
347 struct rt_sigframe __user *frame;
348 unsigned long return_ip;
351 frame = get_sigframe(ka, regs, sizeof(*frame));
353 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
356 err |= __put_user(&frame->info, &frame->pinfo);
357 err |= __put_user(&frame->uc, &frame->puc);
358 err |= copy_siginfo_to_user(&frame->info, info);
362 /* Clear all the bits of the ucontext we don't use. */
363 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
365 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
367 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
372 /* Set up to return from userspace. If provided, use a stub
373 already in userspace. */
374 if (ka->sa.sa_flags & SA_RESTORER) {
375 return_ip = (unsigned long)ka->sa.sa_restorer;
377 /* trampoline - the desired return ip is the retcode itself */
378 return_ip = (unsigned long)&frame->retcode;
379 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
380 err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
381 err |= __put_user(__NR_rt_sigreturn,
382 (short __user *)(frame->retcode+2));
383 err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
389 /* TODO what is the current->exec_domain stuff and invmap ? */
391 /* Set up registers for signal handler */
393 /* What we enter NOW */
394 regs->irp = (unsigned long) ka->sa.sa_handler;
395 /* What we enter LATER */
396 regs->srp = return_ip;
397 /* First argument is signo */
399 /* Second argument is (siginfo_t *) */
400 regs->r11 = (unsigned long)&frame->info;
401 /* Third argument is unused */
404 /* Actually move the usp to reflect the stacked frame */
405 wrusp((unsigned long)frame);
410 force_sigsegv(sig, current);
415 * OK, we're invoking a handler
418 static inline int handle_signal(int canrestart, unsigned long sig,
419 siginfo_t *info, struct k_sigaction *ka,
420 sigset_t *oldset, struct pt_regs *regs)
424 /* Are we from a system call? */
426 /* If so, check system call restarting.. */
428 case -ERESTART_RESTARTBLOCK:
429 case -ERESTARTNOHAND:
430 /* ERESTARTNOHAND means that the syscall should
431 * only be restarted if there was no handler for
432 * the signal, and since we only get here if there
433 * is a handler, we don't restart */
437 /* ERESTARTSYS means to restart the syscall if
438 * there is no handler or the handler was
439 * registered with SA_RESTART */
440 if (!(ka->sa.sa_flags & SA_RESTART)) {
445 case -ERESTARTNOINTR:
446 /* ERESTARTNOINTR means that the syscall should
447 * be called again after the signal handler returns. */
448 RESTART_CRIS_SYS(regs);
452 /* Set up the stack frame */
453 if (ka->sa.sa_flags & SA_SIGINFO)
454 ret = setup_rt_frame(sig, ka, info, oldset, regs);
456 ret = setup_frame(sig, ka, oldset, regs);
459 block_sigmask(ka, sig);
465 * Note that 'init' is a special process: it doesn't get signals it doesn't
466 * want to handle. Thus you cannot kill init even with a SIGKILL even by
469 * Also note that the regs structure given here as an argument, is the latest
470 * pushed pt_regs. It may or may not be the same as the first pushed registers
471 * when the initial usermode->kernelmode transition took place. Therefore
472 * we can use user_mode(regs) to see if we came directly from kernel or user
476 void do_signal(int canrestart, struct pt_regs *regs)
480 struct k_sigaction ka;
484 * We want the common case to go fast, which
485 * is why we may in certain cases get here from
486 * kernel mode. Just return without doing anything
489 if (!user_mode(regs))
492 if (test_thread_flag(TIF_RESTORE_SIGMASK))
493 oldset = ¤t->saved_sigmask;
495 oldset = ¤t->blocked;
497 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
499 /* Whee! Actually deliver the signal. */
500 if (handle_signal(canrestart, signr, &info, &ka,
502 /* a signal was successfully delivered; the saved
503 * sigmask will have been stored in the signal frame,
504 * and will be restored by sigreturn, so we can simply
505 * clear the TIF_RESTORE_SIGMASK flag */
506 if (test_thread_flag(TIF_RESTORE_SIGMASK))
507 clear_thread_flag(TIF_RESTORE_SIGMASK);
512 /* Did we come from a system call? */
514 /* Restart the system call - no handlers present */
515 if (regs->r10 == -ERESTARTNOHAND ||
516 regs->r10 == -ERESTARTSYS ||
517 regs->r10 == -ERESTARTNOINTR) {
518 RESTART_CRIS_SYS(regs);
520 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
521 regs->r9 = __NR_restart_syscall;
526 /* if there's no signal to deliver, we just put the saved sigmask
528 restore_saved_sigmask();