1 /* $Id: signal.c,v 1.21 2000/03/11 14:06:21 gniibe Exp $
3 * linux/arch/sh/kernel/signal.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
9 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
13 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/stddef.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32 asmlinkage
int sys_wait4(pid_t pid
, unsigned long *stat_addr
,
33 int options
, unsigned long *ru
);
34 asmlinkage
int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
);
36 int copy_siginfo_to_user(siginfo_t
*to
, siginfo_t
*from
)
38 if (!access_ok (VERIFY_WRITE
, to
, sizeof(siginfo_t
)))
40 if (from
->si_code
< 0)
41 return __copy_to_user(to
, from
, sizeof(siginfo_t
));
45 /* If you change siginfo_t structure, please be sure
46 this code is fixed accordingly.
47 It should never copy any pad contained in the structure
48 to avoid security leaks, but must copy the generic
49 3 ints plus the relevant union member. */
50 err
= __put_user(from
->si_signo
, &to
->si_signo
);
51 err
|= __put_user(from
->si_errno
, &to
->si_errno
);
52 err
|= __put_user((short)from
->si_code
, &to
->si_code
);
53 /* First 32bits of unions are always present. */
54 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
55 switch (from
->si_code
>> 16) {
56 case __SI_FAULT
>> 16:
59 err
|= __put_user(from
->si_utime
, &to
->si_utime
);
60 err
|= __put_user(from
->si_stime
, &to
->si_stime
);
61 err
|= __put_user(from
->si_status
, &to
->si_status
);
63 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
65 /* case __SI_RT: This is not generated by the kernel as of now. */
72 * Atomically swap in the new signal mask, and wait for a signal.
75 sys_sigsuspend(old_sigset_t mask
,
76 unsigned long r5
, unsigned long r6
, unsigned long r7
,
82 spin_lock_irq(¤t
->sigmask_lock
);
83 saveset
= current
->blocked
;
84 siginitset(¤t
->blocked
, mask
);
85 recalc_sigpending(current
);
86 spin_unlock_irq(¤t
->sigmask_lock
);
88 regs
.regs
[0] = -EINTR
;
90 current
->state
= TASK_INTERRUPTIBLE
;
92 if (do_signal(®s
, &saveset
))
98 sys_rt_sigsuspend(sigset_t
*unewset
, size_t sigsetsize
,
99 unsigned long r6
, unsigned long r7
,
102 sigset_t saveset
, newset
;
104 /* XXX: Don't preclude handling different sized sigset_t's. */
105 if (sigsetsize
!= sizeof(sigset_t
))
108 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
110 sigdelsetmask(&newset
, ~_BLOCKABLE
);
111 spin_lock_irq(¤t
->sigmask_lock
);
112 saveset
= current
->blocked
;
113 current
->blocked
= newset
;
114 recalc_sigpending(current
);
115 spin_unlock_irq(¤t
->sigmask_lock
);
117 regs
.regs
[0] = -EINTR
;
119 current
->state
= TASK_INTERRUPTIBLE
;
121 if (do_signal(®s
, &saveset
))
127 sys_sigaction(int sig
, const struct old_sigaction
*act
,
128 struct old_sigaction
*oact
)
130 struct k_sigaction new_ka
, old_ka
;
135 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
136 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
137 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
139 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
140 __get_user(mask
, &act
->sa_mask
);
141 siginitset(&new_ka
.sa
.sa_mask
, mask
);
144 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
147 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
148 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
149 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
151 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
152 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
159 sys_sigaltstack(const stack_t
*uss
, stack_t
*uoss
,
160 unsigned long r6
, unsigned long r7
,
163 return do_sigaltstack(uss
, uoss
, regs
.regs
[15]);
168 * Do a signal return; undo the signal stack.
173 struct sigcontext sc
;
174 unsigned long extramask
[_NSIG_WORDS
-1];
180 struct siginfo
*pinfo
;
188 static inline int restore_sigcontext_fpu(struct sigcontext
*sc
)
190 struct task_struct
*tsk
= current
;
193 return __copy_from_user(&tsk
->thread
.fpu
.hard
, &sc
->sc_fpregs
[0],
194 sizeof(long)*(16*2+2));
197 static inline int save_sigcontext_fpu(struct sigcontext
*sc
)
199 struct task_struct
*tsk
= current
;
203 if (!tsk
->used_math
) {
205 __copy_to_user(&sc
->sc_ownedfp
, &val
, sizeof(int));
210 __copy_to_user(&sc
->sc_ownedfp
, &val
, sizeof(int));
212 /* This will cause a "finit" to be triggered by the next
213 attempted FPU operation by the 'current' process.
219 restore_flags(flags
);
221 return __copy_to_user(&sc
->sc_fpregs
[0], &tsk
->thread
.fpu
.hard
,
222 sizeof(long)*(16*2+2));
227 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*sc
, int *r0_p
)
229 unsigned int err
= 0;
231 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
233 COPY(regs
[2]); COPY(regs
[3]);
234 COPY(regs
[4]); COPY(regs
[5]);
235 COPY(regs
[6]); COPY(regs
[7]);
236 COPY(regs
[8]); COPY(regs
[9]);
237 COPY(regs
[10]); COPY(regs
[11]);
238 COPY(regs
[12]); COPY(regs
[13]);
239 COPY(regs
[14]); COPY(regs
[15]);
240 COPY(gbr
); COPY(mach
);
241 COPY(macl
); COPY(pr
);
248 struct task_struct
*tsk
= current
;
250 regs
->sr
|= SR_FD
; /* Release FPU */
252 current
->used_math
= 0;
253 __get_user (owned_fp
, &sc
->sc_ownedfp
);
255 err
|= restore_sigcontext_fpu(sc
);
259 regs
->syscall_nr
= -1; /* disable syscall checks */
260 err
|= __get_user(*r0_p
, &sc
->sc_regs
[0]);
264 asmlinkage
int sys_sigreturn(unsigned long r4
, unsigned long r5
,
265 unsigned long r6
, unsigned long r7
,
268 struct sigframe
*frame
= (struct sigframe
*)regs
.regs
[15];
272 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
275 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
277 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
278 sizeof(frame
->extramask
))))
281 sigdelsetmask(&set
, ~_BLOCKABLE
);
283 spin_lock_irq(¤t
->sigmask_lock
);
284 current
->blocked
= set
;
285 recalc_sigpending(current
);
286 spin_unlock_irq(¤t
->sigmask_lock
);
288 if (restore_sigcontext(®s
, &frame
->sc
, &r0
))
293 force_sig(SIGSEGV
, current
);
297 asmlinkage
int sys_rt_sigreturn(unsigned long r4
, unsigned long r5
,
298 unsigned long r6
, unsigned long r7
,
301 struct rt_sigframe
*frame
= (struct rt_sigframe
*)regs
.regs
[15];
306 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
309 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
312 sigdelsetmask(&set
, ~_BLOCKABLE
);
313 spin_lock_irq(¤t
->sigmask_lock
);
314 current
->blocked
= set
;
315 recalc_sigpending(current
);
316 spin_unlock_irq(¤t
->sigmask_lock
);
318 if (restore_sigcontext(®s
, &frame
->uc
.uc_mcontext
, &r0
))
321 if (__copy_from_user(&st
, &frame
->uc
.uc_stack
, sizeof(st
)))
323 /* It is more difficult to avoid calling this function than to
324 call it and ignore errors. */
325 do_sigaltstack(&st
, NULL
, regs
.regs
[15]);
330 force_sig(SIGSEGV
, current
);
335 * Set up a signal frame.
339 setup_sigcontext(struct sigcontext
*sc
, struct pt_regs
*regs
,
344 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
345 COPY(regs
[0]); COPY(regs
[1]);
346 COPY(regs
[2]); COPY(regs
[3]);
347 COPY(regs
[4]); COPY(regs
[5]);
348 COPY(regs
[6]); COPY(regs
[7]);
349 COPY(regs
[8]); COPY(regs
[9]);
350 COPY(regs
[10]); COPY(regs
[11]);
351 COPY(regs
[12]); COPY(regs
[13]);
352 COPY(regs
[14]); COPY(regs
[15]);
353 COPY(gbr
); COPY(mach
);
354 COPY(macl
); COPY(pr
);
359 err
|= save_sigcontext_fpu(sc
);
362 /* non-iBCS2 extensions.. */
363 err
|= __put_user(mask
, &sc
->oldmask
);
369 * Determine which stack to use..
372 get_sigframe(struct k_sigaction
*ka
, unsigned long sp
, size_t frame_size
)
374 if ((ka
->sa
.sa_flags
& SA_ONSTACK
) != 0 && ! on_sig_stack(sp
))
375 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
377 return (void *)((sp
- frame_size
) & -8ul);
380 static void setup_frame(int sig
, struct k_sigaction
*ka
,
381 sigset_t
*set
, struct pt_regs
*regs
)
383 struct sigframe
*frame
;
387 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
389 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
392 signal
= current
->exec_domain
393 && current
->exec_domain
->signal_invmap
395 ? current
->exec_domain
->signal_invmap
[sig
]
398 err
|= setup_sigcontext(&frame
->sc
, regs
, set
->sig
[0]);
400 if (_NSIG_WORDS
> 1) {
401 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
402 sizeof(frame
->extramask
));
405 /* Set up to return from userspace. If provided, use a stub
406 already in userspace. */
407 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
408 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
410 /* This is : mov #__NR_sigreturn,r3 ; trapa #0x10 */
411 #ifdef __LITTLE_ENDIAN__
412 unsigned long code
= 0xc310e300 | (__NR_sigreturn
);
414 unsigned long code
= 0xe300c310 | (__NR_sigreturn
<< 16);
417 regs
->pr
= (unsigned long) frame
->retcode
;
418 err
|= __put_user(code
, (long *)(frame
->retcode
+0));
424 /* Set up registers for signal handler */
425 regs
->regs
[15] = (unsigned long) frame
;
426 regs
->regs
[4] = signal
; /* Arg for signal handler */
427 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
432 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
433 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->pr
);
436 flush_icache_range(regs
->pr
, regs
->pr
+4);
441 ka
->sa
.sa_handler
= SIG_DFL
;
442 force_sig(SIGSEGV
, current
);
445 static void setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
446 sigset_t
*set
, struct pt_regs
*regs
)
448 struct rt_sigframe
*frame
;
452 frame
= get_sigframe(ka
, regs
->regs
[15], sizeof(*frame
));
454 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
457 signal
= current
->exec_domain
458 && current
->exec_domain
->signal_invmap
460 ? current
->exec_domain
->signal_invmap
[sig
]
463 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
464 err
|= __put_user(&frame
->uc
, &frame
->puc
);
465 err
|= copy_siginfo_to_user(&frame
->info
, info
);
467 /* Create the ucontext. */
468 err
|= __put_user(0, &frame
->uc
.uc_flags
);
469 err
|= __put_user(0, &frame
->uc
.uc_link
);
470 err
|= __put_user((void *)current
->sas_ss_sp
,
471 &frame
->uc
.uc_stack
.ss_sp
);
472 err
|= __put_user(sas_ss_flags(regs
->regs
[15]),
473 &frame
->uc
.uc_stack
.ss_flags
);
474 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
475 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
,
477 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
479 /* Set up to return from userspace. If provided, use a stub
480 already in userspace. */
481 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
482 regs
->pr
= (unsigned long) ka
->sa
.sa_restorer
;
484 /* This is : mov #__NR_rt_sigreturn,r3 ; trapa #0x10 */
485 #ifdef __LITTLE_ENDIAN__
486 unsigned long code
= 0xc310e300 | (__NR_rt_sigreturn
);
488 unsigned long code
= 0xe300c310 | (__NR_rt_sigreturn
<< 16);
491 regs
->pr
= (unsigned long) frame
->retcode
;
492 err
|= __put_user(code
, (long *)(frame
->retcode
+0));
498 /* Set up registers for signal handler */
499 regs
->regs
[15] = (unsigned long) frame
;
500 regs
->regs
[4] = signal
; /* Arg for signal handler */
501 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
506 printk("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
507 current
->comm
, current
->pid
, frame
, regs
->pc
, regs
->pr
);
510 flush_icache_range(regs
->pr
, regs
->pr
+4);
515 ka
->sa
.sa_handler
= SIG_DFL
;
516 force_sig(SIGSEGV
, current
);
520 * OK, we're invoking a handler
524 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
525 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
* regs
)
527 /* Are we from a system call? */
528 if (regs
->syscall_nr
>= 0) {
529 /* If so, check system call restarting.. */
530 switch (regs
->regs
[0]) {
531 case -ERESTARTNOHAND
:
532 regs
->regs
[0] = -EINTR
;
536 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
537 regs
->regs
[0] = -EINTR
;
541 case -ERESTARTNOINTR
:
542 regs
->regs
[0] = regs
->syscall_nr
;
547 /* Set up the stack frame */
548 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
549 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
551 setup_frame(sig
, ka
, oldset
, regs
);
553 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
554 ka
->sa
.sa_handler
= SIG_DFL
;
556 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
557 spin_lock_irq(¤t
->sigmask_lock
);
558 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
559 sigaddset(¤t
->blocked
,sig
);
560 recalc_sigpending(current
);
561 spin_unlock_irq(¤t
->sigmask_lock
);
566 * Note that 'init' is a special process: it doesn't get signals it doesn't
567 * want to handle. Thus you cannot kill init even with a SIGKILL even by
570 * Note that we go through the signals twice: once to check the signals that
571 * the kernel can handle, and then we build all the user-level signal handling
572 * stack-frames in one go after that.
574 int do_signal(struct pt_regs
*regs
, sigset_t
*oldset
)
577 struct k_sigaction
*ka
;
580 * We want the common case to go fast, which
581 * is why we may in certain cases get here from
582 * kernel mode. Just return without doing anything
585 if (!user_mode(regs
))
589 oldset
= ¤t
->blocked
;
594 spin_lock_irq(¤t
->sigmask_lock
);
595 signr
= dequeue_signal(¤t
->blocked
, &info
);
596 spin_unlock_irq(¤t
->sigmask_lock
);
601 if ((current
->ptrace
& PT_PTRACED
) && signr
!= SIGKILL
) {
602 /* Let the debugger run. */
603 current
->exit_code
= signr
;
604 current
->state
= TASK_STOPPED
;
605 notify_parent(current
, SIGCHLD
);
608 /* We're back. Did the debugger cancel the sig? */
609 if (!(signr
= current
->exit_code
))
611 current
->exit_code
= 0;
613 /* The debugger continued. Ignore SIGSTOP. */
614 if (signr
== SIGSTOP
)
617 /* Update the siginfo structure. Is this good? */
618 if (signr
!= info
.si_signo
) {
619 info
.si_signo
= signr
;
621 info
.si_code
= SI_USER
;
622 info
.si_pid
= current
->p_pptr
->pid
;
623 info
.si_uid
= current
->p_pptr
->uid
;
626 /* If the (new) signal is now blocked, requeue it. */
627 if (sigismember(¤t
->blocked
, signr
)) {
628 send_sig_info(signr
, &info
, current
);
633 ka
= ¤t
->sig
->action
[signr
-1];
634 if (ka
->sa
.sa_handler
== SIG_IGN
) {
635 if (signr
!= SIGCHLD
)
637 /* Check for SIGCHLD: it's special. */
638 while (sys_wait4(-1, NULL
, WNOHANG
, NULL
) > 0)
643 if (ka
->sa
.sa_handler
== SIG_DFL
) {
644 int exit_code
= signr
;
646 /* Init gets no signals it doesn't want. */
647 if (current
->pid
== 1)
651 case SIGCONT
: case SIGCHLD
: case SIGWINCH
:
654 case SIGTSTP
: case SIGTTIN
: case SIGTTOU
:
655 if (is_orphaned_pgrp(current
->pgrp
))
660 current
->state
= TASK_STOPPED
;
661 current
->exit_code
= signr
;
662 if (!(current
->p_pptr
->sig
->action
[SIGCHLD
-1].sa
.sa_flags
& SA_NOCLDSTOP
))
663 notify_parent(current
, SIGCHLD
);
667 case SIGQUIT
: case SIGILL
: case SIGTRAP
:
668 case SIGABRT
: case SIGFPE
: case SIGSEGV
:
669 case SIGBUS
: case SIGSYS
: case SIGXCPU
: case SIGXFSZ
:
670 if (do_coredump(signr
, regs
))
675 sigaddset(¤t
->pending
.signal
, signr
);
676 recalc_sigpending(current
);
677 current
->flags
|= PF_SIGNALED
;
683 /* Whee! Actually deliver the signal. */
684 handle_signal(signr
, ka
, &info
, oldset
, regs
);
688 /* Did we come from a system call? */
689 if (regs
->syscall_nr
>= 0) {
690 /* Restart the system call - no handlers present */
691 if (regs
->regs
[0] == -ERESTARTNOHAND
||
692 regs
->regs
[0] == -ERESTARTSYS
||
693 regs
->regs
[0] == -ERESTARTNOINTR
) {
694 regs
->regs
[0] = regs
->syscall_nr
;