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>
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
)
54 spin_lock_irq(¤t
->sighand
->siglock
);
55 current
->saved_sigmask
= current
->blocked
;
56 siginitset(¤t
->blocked
, mask
);
58 spin_unlock_irq(¤t
->sighand
->siglock
);
59 current
->state
= TASK_INTERRUPTIBLE
;
61 set_thread_flag(TIF_RESTORE_SIGMASK
);
62 return -ERESTARTNOHAND
;
65 int sys_sigaction(int sig
, const struct old_sigaction __user
*act
,
66 struct old_sigaction
*oact
)
68 struct k_sigaction new_ka
, old_ka
;
73 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
74 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
75 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
77 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
78 __get_user(mask
, &act
->sa_mask
);
79 siginitset(&new_ka
.sa
.sa_mask
, mask
);
82 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
85 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
86 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
87 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
89 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
90 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
96 int sys_sigaltstack(const stack_t
*uss
, stack_t __user
*uoss
)
98 return do_sigaltstack(uss
, uoss
, rdusp());
103 * Do a signal return; undo the signal stack.
107 struct sigcontext sc
;
108 unsigned long extramask
[_NSIG_WORDS
-1];
109 unsigned char retcode
[8]; /* trampoline code */
113 struct siginfo
*pinfo
;
117 unsigned char retcode
[8]; /* trampoline code */
122 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
124 unsigned int err
= 0;
125 unsigned long old_usp
;
127 /* Always make any pending restarted system calls return -EINTR */
128 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
130 /* restore the regs from &sc->regs (same as sc, since regs is first)
131 * (sc is already checked for VERIFY_READ since the sigframe was
132 * checked in sys_sigreturn previously)
135 if (__copy_from_user(regs
, sc
, sizeof(struct pt_regs
)))
138 /* make sure the U-flag is set so user-mode cannot fool us */
140 regs
->dccr
|= 1 << 8;
142 /* restore the old USP as it was before we stacked the sc etc.
143 * (we cannot just pop the sigcontext since we aligned the sp and
144 * stuff after pushing it)
147 err
|= __get_user(old_usp
, &sc
->usp
);
151 /* TODO: the other ports use regs->orig_XX to disable syscall checks
152 * after this completes, but we don't use that mechanism. maybe we can
162 /* Define dummy arguments to be able to reach the regs argument. */
164 asmlinkage
int sys_sigreturn(long r10
, long r11
, long r12
, long r13
, long mof
,
165 long srp
, struct pt_regs
*regs
)
167 struct sigframe __user
*frame
= (struct sigframe
*)rdusp();
171 * Since we stacked the signal on a dword boundary,
172 * then frame should be dword aligned here. If it's
173 * not, then the user is trying to mess with us.
175 if (((long)frame
) & 3)
178 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
180 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
182 && __copy_from_user(&set
.sig
[1], frame
->extramask
,
183 sizeof(frame
->extramask
))))
186 sigdelsetmask(&set
, ~_BLOCKABLE
);
187 spin_lock_irq(¤t
->sighand
->siglock
);
188 current
->blocked
= set
;
190 spin_unlock_irq(¤t
->sighand
->siglock
);
192 if (restore_sigcontext(regs
, &frame
->sc
))
195 /* TODO: SIGTRAP when single-stepping as in arm ? */
200 force_sig(SIGSEGV
, current
);
204 /* Define dummy arguments to be able to reach the regs argument. */
206 asmlinkage
int sys_rt_sigreturn(long r10
, long r11
, long r12
, long r13
,
207 long mof
, long srp
, struct pt_regs
*regs
)
209 struct rt_sigframe __user
*frame
= (struct rt_sigframe
*)rdusp();
213 * Since we stacked the signal on a dword boundary,
214 * then frame should be dword aligned here. If it's
215 * not, then the user is trying to mess with us.
217 if (((long)frame
) & 3)
220 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
222 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
225 sigdelsetmask(&set
, ~_BLOCKABLE
);
226 spin_lock_irq(¤t
->sighand
->siglock
);
227 current
->blocked
= set
;
229 spin_unlock_irq(¤t
->sighand
->siglock
);
231 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
234 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, rdusp()) == -EFAULT
)
240 force_sig(SIGSEGV
, current
);
245 * Set up a signal frame.
248 static int setup_sigcontext(struct sigcontext __user
*sc
,
249 struct pt_regs
*regs
, unsigned long mask
)
252 unsigned long usp
= rdusp();
254 /* copy the regs. they are first in sc so we can use sc directly */
256 err
|= __copy_to_user(sc
, regs
, sizeof(struct pt_regs
));
258 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
259 the signal handler. The frametype will be restored to its previous
260 value in restore_sigcontext. */
261 regs
->frametype
= CRIS_FRAME_NORMAL
;
263 /* then some other stuff */
265 err
|= __put_user(mask
, &sc
->oldmask
);
267 err
|= __put_user(usp
, &sc
->usp
);
272 /* Figure out where we want to put the new signal frame
273 * - usually on the stack. */
275 static inline void __user
*
276 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, size_t frame_size
)
278 unsigned long sp
= rdusp();
280 /* This is the X/Open sanctioned signal stack switching. */
281 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
282 if (! on_sig_stack(sp
))
283 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
286 /* make sure the frame is dword-aligned */
290 return (void __user
*)(sp
- frame_size
);
293 /* grab and setup a signal frame.
295 * basically we stack a lot of state info, and arrange for the
296 * user-mode program to return to the kernel using either a
297 * trampoline which performs the syscall sigreturn, or a provided
298 * user-mode trampoline.
301 static int setup_frame(int sig
, struct k_sigaction
*ka
,
302 sigset_t
*set
, struct pt_regs
*regs
)
304 struct sigframe __user
*frame
;
305 unsigned long return_ip
;
308 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
310 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
313 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
317 if (_NSIG_WORDS
> 1) {
318 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
319 sizeof(frame
->extramask
));
324 /* Set up to return from userspace. If provided, use a stub
325 already in userspace. */
326 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
327 return_ip
= (unsigned long)ka
->sa
.sa_restorer
;
329 /* trampoline - the desired return ip is the retcode itself */
330 return_ip
= (unsigned long)&frame
->retcode
;
331 /* This is movu.w __NR_sigreturn, r9; break 13; */
332 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
333 err
|= __put_user(__NR_sigreturn
, (short __user
*)(frame
->retcode
+2));
334 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
340 /* Set up registers for signal handler */
342 regs
->irp
= (unsigned long) ka
->sa
.sa_handler
; /* what we enter NOW */
343 regs
->srp
= return_ip
; /* what we enter LATER */
344 regs
->r10
= sig
; /* first argument is signo */
346 /* actually move the usp to reflect the stacked frame */
348 wrusp((unsigned long)frame
);
353 force_sigsegv(sig
, current
);
357 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
358 sigset_t
*set
, struct pt_regs
*regs
)
360 struct rt_sigframe __user
*frame
;
361 unsigned long return_ip
;
364 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
366 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
369 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
370 err
|= __put_user(&frame
->uc
, &frame
->puc
);
371 err
|= copy_siginfo_to_user(&frame
->info
, info
);
375 /* Clear all the bits of the ucontext we don't use. */
376 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
378 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, set
->sig
[0]);
380 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
385 /* Set up to return from userspace. If provided, use a stub
386 already in userspace. */
387 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
388 return_ip
= (unsigned long)ka
->sa
.sa_restorer
;
390 /* trampoline - the desired return ip is the retcode itself */
391 return_ip
= (unsigned long)&frame
->retcode
;
392 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
393 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
394 err
|= __put_user(__NR_rt_sigreturn
,
395 (short __user
*)(frame
->retcode
+2));
396 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
402 /* TODO what is the current->exec_domain stuff and invmap ? */
404 /* Set up registers for signal handler */
406 /* What we enter NOW */
407 regs
->irp
= (unsigned long) ka
->sa
.sa_handler
;
408 /* What we enter LATER */
409 regs
->srp
= return_ip
;
410 /* First argument is signo */
412 /* Second argument is (siginfo_t *) */
413 regs
->r11
= (unsigned long)&frame
->info
;
414 /* Third argument is unused */
417 /* Actually move the usp to reflect the stacked frame */
418 wrusp((unsigned long)frame
);
423 force_sigsegv(sig
, current
);
428 * OK, we're invoking a handler
431 static inline int handle_signal(int canrestart
, unsigned long sig
,
432 siginfo_t
*info
, struct k_sigaction
*ka
,
433 sigset_t
*oldset
, struct pt_regs
*regs
)
437 /* Are we from a system call? */
439 /* If so, check system call restarting.. */
441 case -ERESTART_RESTARTBLOCK
:
442 case -ERESTARTNOHAND
:
443 /* ERESTARTNOHAND means that the syscall should
444 * only be restarted if there was no handler for
445 * the signal, and since we only get here if there
446 * is a handler, we don't restart */
450 /* ERESTARTSYS means to restart the syscall if
451 * there is no handler or the handler was
452 * registered with SA_RESTART */
453 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
458 case -ERESTARTNOINTR
:
459 /* ERESTARTNOINTR means that the syscall should
460 * be called again after the signal handler returns. */
461 RESTART_CRIS_SYS(regs
);
465 /* Set up the stack frame */
466 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
467 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
469 ret
= setup_frame(sig
, ka
, oldset
, regs
);
472 spin_lock_irq(¤t
->sighand
->siglock
);
473 sigorsets(¤t
->blocked
, ¤t
->blocked
,
475 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
476 sigaddset(¤t
->blocked
, sig
);
478 spin_unlock_irq(¤t
->sighand
->siglock
);
484 * Note that 'init' is a special process: it doesn't get signals it doesn't
485 * want to handle. Thus you cannot kill init even with a SIGKILL even by
488 * Also note that the regs structure given here as an argument, is the latest
489 * pushed pt_regs. It may or may not be the same as the first pushed registers
490 * when the initial usermode->kernelmode transition took place. Therefore
491 * we can use user_mode(regs) to see if we came directly from kernel or user
495 void do_signal(int canrestart
, struct pt_regs
*regs
)
499 struct k_sigaction ka
;
503 * We want the common case to go fast, which
504 * is why we may in certain cases get here from
505 * kernel mode. Just return without doing anything
508 if (!user_mode(regs
))
511 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
512 oldset
= ¤t
->saved_sigmask
;
514 oldset
= ¤t
->blocked
;
516 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
518 /* Whee! Actually deliver the signal. */
519 if (handle_signal(canrestart
, signr
, &info
, &ka
,
521 /* a signal was successfully delivered; the saved
522 * sigmask will have been stored in the signal frame,
523 * and will be restored by sigreturn, so we can simply
524 * clear the TIF_RESTORE_SIGMASK flag */
525 if (test_thread_flag(TIF_RESTORE_SIGMASK
))
526 clear_thread_flag(TIF_RESTORE_SIGMASK
);
531 /* Did we come from a system call? */
533 /* Restart the system call - no handlers present */
534 if (regs
->r10
== -ERESTARTNOHAND
||
535 regs
->r10
== -ERESTARTSYS
||
536 regs
->r10
== -ERESTARTNOINTR
) {
537 RESTART_CRIS_SYS(regs
);
539 if (regs
->r10
== -ERESTART_RESTARTBLOCK
) {
540 regs
->r10
= __NR_restart_syscall
;
545 /* if there's no signal to deliver, we just put the saved sigmask
547 if (test_thread_flag(TIF_RESTORE_SIGMASK
)) {
548 clear_thread_flag(TIF_RESTORE_SIGMASK
);
549 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);