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>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
28 #include <asm/processor.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.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 int do_signal(int canrestart
, sigset_t
*oldset
, 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.)
52 sys_sigsuspend(old_sigset_t mask
, long r11
, long r12
, long r13
, long mof
,
53 long srp
, struct pt_regs
*regs
)
58 spin_lock_irq(¤t
->sighand
->siglock
);
59 saveset
= current
->blocked
;
60 siginitset(¤t
->blocked
, mask
);
62 spin_unlock_irq(¤t
->sighand
->siglock
);
66 current
->state
= TASK_INTERRUPTIBLE
;
68 if (do_signal(0, &saveset
, regs
))
69 /* We will get here twice: once to call the signal
70 handler, then again to return from the
71 sigsuspend system call. When calling the
72 signal handler, R10 holds the signal number as
73 set through do_signal. The sigsuspend call
74 will return with the restored value set above;
80 /* Define dummy arguments to be able to reach the regs argument. (Note that
81 * this arrangement relies on size_t occupying one register.)
84 sys_rt_sigsuspend(sigset_t
*unewset
, size_t sigsetsize
, long r12
, long r13
,
85 long mof
, long srp
, struct pt_regs
*regs
)
87 sigset_t saveset
, newset
;
89 /* XXX: Don't preclude handling different sized sigset_t's. */
90 if (sigsetsize
!= sizeof(sigset_t
))
93 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
95 sigdelsetmask(&newset
, ~_BLOCKABLE
);
97 spin_lock_irq(¤t
->sighand
->siglock
);
98 saveset
= current
->blocked
;
99 current
->blocked
= newset
;
101 spin_unlock_irq(¤t
->sighand
->siglock
);
105 current
->state
= TASK_INTERRUPTIBLE
;
107 if (do_signal(0, &saveset
, regs
))
108 /* We will get here twice: once to call the signal
109 handler, then again to return from the
110 sigsuspend system call. When calling the
111 signal handler, R10 holds the signal number as
112 set through do_signal. The sigsuspend call
113 will return with the restored value set above;
120 sys_sigaction(int sig
, const struct old_sigaction __user
*act
,
121 struct old_sigaction
*oact
)
123 struct k_sigaction new_ka
, old_ka
;
128 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
129 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
130 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
132 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
133 __get_user(mask
, &act
->sa_mask
);
134 siginitset(&new_ka
.sa
.sa_mask
, mask
);
137 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
140 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
141 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
142 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
144 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
145 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
152 sys_sigaltstack(const stack_t
*uss
, stack_t __user
*uoss
)
154 return do_sigaltstack(uss
, uoss
, rdusp());
159 * Do a signal return; undo the signal stack.
163 struct sigcontext sc
;
164 unsigned long extramask
[_NSIG_WORDS
-1];
165 unsigned char retcode
[8]; /* trampoline code */
169 struct siginfo
*pinfo
;
173 unsigned char retcode
[8]; /* trampoline code */
178 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
)
180 unsigned int err
= 0;
181 unsigned long old_usp
;
183 /* Always make any pending restarted system calls return -EINTR */
184 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
186 /* restore the regs from &sc->regs (same as sc, since regs is first)
187 * (sc is already checked for VERIFY_READ since the sigframe was
188 * checked in sys_sigreturn previously)
191 if (__copy_from_user(regs
, sc
, sizeof(struct pt_regs
)))
194 /* make sure the U-flag is set so user-mode cannot fool us */
196 regs
->dccr
|= 1 << 8;
198 /* restore the old USP as it was before we stacked the sc etc.
199 * (we cannot just pop the sigcontext since we aligned the sp and
200 * stuff after pushing it)
203 err
|= __get_user(old_usp
, &sc
->usp
);
207 /* TODO: the other ports use regs->orig_XX to disable syscall checks
208 * after this completes, but we don't use that mechanism. maybe we can
218 /* Define dummy arguments to be able to reach the regs argument. */
220 asmlinkage
int sys_sigreturn(long r10
, long r11
, long r12
, long r13
, long mof
,
221 long srp
, struct pt_regs
*regs
)
223 struct sigframe __user
*frame
= (struct sigframe
*)rdusp();
227 * Since we stacked the signal on a dword boundary,
228 * then frame should be dword aligned here. If it's
229 * not, then the user is trying to mess with us.
231 if (((long)frame
) & 3)
234 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
236 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
238 && __copy_from_user(&set
.sig
[1], frame
->extramask
,
239 sizeof(frame
->extramask
))))
242 sigdelsetmask(&set
, ~_BLOCKABLE
);
243 spin_lock_irq(¤t
->sighand
->siglock
);
244 current
->blocked
= set
;
246 spin_unlock_irq(¤t
->sighand
->siglock
);
248 if (restore_sigcontext(regs
, &frame
->sc
))
251 /* TODO: SIGTRAP when single-stepping as in arm ? */
256 force_sig(SIGSEGV
, current
);
260 /* Define dummy arguments to be able to reach the regs argument. */
262 asmlinkage
int sys_rt_sigreturn(long r10
, long r11
, long r12
, long r13
,
263 long mof
, long srp
, struct pt_regs
*regs
)
265 struct rt_sigframe __user
*frame
= (struct rt_sigframe
*)rdusp();
269 * Since we stacked the signal on a dword boundary,
270 * then frame should be dword aligned here. If it's
271 * not, then the user is trying to mess with us.
273 if (((long)frame
) & 3)
276 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
278 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
281 sigdelsetmask(&set
, ~_BLOCKABLE
);
282 spin_lock_irq(¤t
->sighand
->siglock
);
283 current
->blocked
= set
;
285 spin_unlock_irq(¤t
->sighand
->siglock
);
287 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
290 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, rdusp()) == -EFAULT
)
296 force_sig(SIGSEGV
, current
);
301 * Set up a signal frame.
305 setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
, unsigned long mask
)
308 unsigned long usp
= rdusp();
310 /* copy the regs. they are first in sc so we can use sc directly */
312 err
|= __copy_to_user(sc
, regs
, sizeof(struct pt_regs
));
314 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
315 the signal handler. The frametype will be restored to its previous
316 value in restore_sigcontext. */
317 regs
->frametype
= CRIS_FRAME_NORMAL
;
319 /* then some other stuff */
321 err
|= __put_user(mask
, &sc
->oldmask
);
323 err
|= __put_user(usp
, &sc
->usp
);
328 /* figure out where we want to put the new signal frame - usually on the stack */
330 static inline void __user
*
331 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
333 unsigned long sp
= rdusp();
335 /* This is the X/Open sanctioned signal stack switching. */
336 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
337 if (! on_sig_stack(sp
))
338 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
341 /* make sure the frame is dword-aligned */
345 return (void __user
*)(sp
- frame_size
);
348 /* grab and setup a signal frame.
350 * basically we stack a lot of state info, and arrange for the
351 * user-mode program to return to the kernel using either a
352 * trampoline which performs the syscall sigreturn, or a provided
353 * user-mode trampoline.
356 static void setup_frame(int sig
, struct k_sigaction
*ka
,
357 sigset_t
*set
, struct pt_regs
* regs
)
359 struct sigframe __user
*frame
;
360 unsigned long return_ip
;
363 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
365 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
368 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
372 if (_NSIG_WORDS
> 1) {
373 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
374 sizeof(frame
->extramask
));
379 /* Set up to return from userspace. If provided, use a stub
380 already in userspace. */
381 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
382 return_ip
= (unsigned long)ka
->sa
.sa_restorer
;
384 /* trampoline - the desired return ip is the retcode itself */
385 return_ip
= (unsigned long)&frame
->retcode
;
386 /* This is movu.w __NR_sigreturn, r9; break 13; */
387 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
388 err
|= __put_user(__NR_sigreturn
, (short __user
*)(frame
->retcode
+2));
389 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
395 /* Set up registers for signal handler */
397 regs
->irp
= (unsigned long) ka
->sa
.sa_handler
; /* what we enter NOW */
398 regs
->srp
= return_ip
; /* what we enter LATER */
399 regs
->r10
= sig
; /* first argument is signo */
401 /* actually move the usp to reflect the stacked frame */
403 wrusp((unsigned long)frame
);
408 force_sigsegv(sig
, current
);
411 static void setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
412 sigset_t
*set
, struct pt_regs
* regs
)
414 struct rt_sigframe __user
*frame
;
415 unsigned long return_ip
;
418 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
420 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
423 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
424 err
|= __put_user(&frame
->uc
, &frame
->puc
);
425 err
|= copy_siginfo_to_user(&frame
->info
, info
);
429 /* Clear all the bits of the ucontext we don't use. */
430 err
|= __clear_user(&frame
->uc
, offsetof(struct ucontext
, uc_mcontext
));
432 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
, set
->sig
[0]);
434 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
439 /* Set up to return from userspace. If provided, use a stub
440 already in userspace. */
441 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
442 return_ip
= (unsigned long)ka
->sa
.sa_restorer
;
444 /* trampoline - the desired return ip is the retcode itself */
445 return_ip
= (unsigned long)&frame
->retcode
;
446 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
447 err
|= __put_user(0x9c5f, (short __user
*)(frame
->retcode
+0));
448 err
|= __put_user(__NR_rt_sigreturn
, (short __user
*)(frame
->retcode
+2));
449 err
|= __put_user(0xe93d, (short __user
*)(frame
->retcode
+4));
455 /* TODO what is the current->exec_domain stuff and invmap ? */
457 /* Set up registers for signal handler */
459 regs
->irp
= (unsigned long) ka
->sa
.sa_handler
; /* what we enter NOW */
460 regs
->srp
= return_ip
; /* what we enter LATER */
461 regs
->r10
= sig
; /* first argument is signo */
462 regs
->r11
= (unsigned long) &frame
->info
; /* second argument is (siginfo_t *) */
463 regs
->r12
= 0; /* third argument is unused */
465 /* actually move the usp to reflect the stacked frame */
467 wrusp((unsigned long)frame
);
472 force_sigsegv(sig
, current
);
476 * OK, we're invoking a handler
480 handle_signal(int canrestart
, unsigned long sig
,
481 siginfo_t
*info
, struct k_sigaction
*ka
,
482 sigset_t
*oldset
, struct pt_regs
* regs
)
484 /* Are we from a system call? */
486 /* If so, check system call restarting.. */
488 case -ERESTART_RESTARTBLOCK
:
489 case -ERESTARTNOHAND
:
490 /* ERESTARTNOHAND means that the syscall should only be
491 restarted if there was no handler for the signal, and since
492 we only get here if there is a handler, we don't restart */
497 /* ERESTARTSYS means to restart the syscall if there is no
498 handler or the handler was registered with SA_RESTART */
499 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
504 case -ERESTARTNOINTR
:
505 /* ERESTARTNOINTR means that the syscall should be called again
506 after the signal handler returns. */
507 RESTART_CRIS_SYS(regs
);
511 /* Set up the stack frame */
512 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
513 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
515 setup_frame(sig
, ka
, oldset
, regs
);
517 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
518 ka
->sa
.sa_handler
= SIG_DFL
;
520 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
521 spin_lock_irq(¤t
->sighand
->siglock
);
522 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
523 sigaddset(¤t
->blocked
,sig
);
525 spin_unlock_irq(¤t
->sighand
->siglock
);
530 * Note that 'init' is a special process: it doesn't get signals it doesn't
531 * want to handle. Thus you cannot kill init even with a SIGKILL even by
534 * Also note that the regs structure given here as an argument, is the latest
535 * pushed pt_regs. It may or may not be the same as the first pushed registers
536 * when the initial usermode->kernelmode transition took place. Therefore
537 * we can use user_mode(regs) to see if we came directly from kernel or user
541 int do_signal(int canrestart
, sigset_t
*oldset
, struct pt_regs
*regs
)
545 struct k_sigaction ka
;
548 * We want the common case to go fast, which
549 * is why we may in certain cases get here from
550 * kernel mode. Just return without doing anything
553 if (!user_mode(regs
))
557 oldset
= ¤t
->blocked
;
559 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
561 /* Whee! Actually deliver the signal. */
562 handle_signal(canrestart
, signr
, &info
, &ka
, oldset
, regs
);
566 /* Did we come from a system call? */
568 /* Restart the system call - no handlers present */
569 if (regs
->r10
== -ERESTARTNOHAND
||
570 regs
->r10
== -ERESTARTSYS
||
571 regs
->r10
== -ERESTARTNOINTR
) {
572 RESTART_CRIS_SYS(regs
);
574 if (regs
->r10
== -ERESTART_RESTARTBLOCK
){
575 regs
->r10
= __NR_restart_syscall
;