2 * linux/arch/i386/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
9 #include <linux/config.h>
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <asm/ucontext.h>
23 #include <asm/uaccess.h>
27 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
29 asmlinkage
int sys_wait4(pid_t pid
, unsigned long *stat_addr
,
30 int options
, unsigned long *ru
);
31 asmlinkage
int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
);
34 * Atomically swap in the new signal mask, and wait for a signal.
37 sys_sigsuspend(int history0
, int history1
, old_sigset_t mask
)
39 struct pt_regs
* regs
= (struct pt_regs
*) &history0
;
43 spin_lock_irq(¤t
->sigmask_lock
);
44 saveset
= current
->blocked
;
45 siginitset(¤t
->blocked
, mask
);
46 recalc_sigpending(current
);
47 spin_unlock_irq(¤t
->sigmask_lock
);
51 current
->state
= TASK_INTERRUPTIBLE
;
53 if (do_signal(&saveset
, regs
))
59 sys_rt_sigsuspend(sigset_t
*unewset
, size_t sigsetsize
)
61 struct pt_regs
* regs
= (struct pt_regs
*) &unewset
;
62 sigset_t saveset
, newset
;
64 /* XXX: Don't preclude handling different sized sigset_t's. */
65 if (sigsetsize
!= sizeof(sigset_t
))
68 if (copy_from_user(&newset
, unewset
, sizeof(newset
)))
70 sigdelsetmask(&newset
, ~_BLOCKABLE
);
72 spin_lock_irq(¤t
->sigmask_lock
);
73 saveset
= current
->blocked
;
74 current
->blocked
= newset
;
75 recalc_sigpending(current
);
76 spin_unlock_irq(¤t
->sigmask_lock
);
80 current
->state
= TASK_INTERRUPTIBLE
;
82 if (do_signal(&saveset
, regs
))
88 sys_sigaction(int sig
, const struct old_sigaction
*act
,
89 struct old_sigaction
*oact
)
91 struct k_sigaction new_ka
, old_ka
;
96 if (verify_area(VERIFY_READ
, act
, sizeof(*act
)) ||
97 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
98 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
100 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
101 __get_user(mask
, &act
->sa_mask
);
102 siginitset(&new_ka
.sa
.sa_mask
, mask
);
105 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
108 if (verify_area(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
109 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
110 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
112 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
113 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
120 sys_sigaltstack(const stack_t
*uss
, stack_t
*uoss
)
122 struct pt_regs
*regs
= (struct pt_regs
*) &uss
;
123 return do_sigaltstack(uss
, uoss
, regs
->esp
);
128 * Do a signal return; undo the signal stack.
135 struct sigcontext sc
;
136 struct _fpstate fpstate
;
137 unsigned long extramask
[_NSIG_WORDS
-1];
145 struct siginfo
*pinfo
;
149 struct _fpstate fpstate
;
154 static inline int restore_i387_hard(struct _fpstate
*buf
)
156 if (current
->flags
& PF_USEDFPU
) {
157 current
->flags
&= ~PF_USEDFPU
;
160 return __copy_from_user(¤t
->tss
.i387
.hard
, buf
, sizeof(*buf
));
163 static inline int restore_i387(struct _fpstate
*buf
)
166 #ifndef CONFIG_MATH_EMULATION
167 err
= restore_i387_hard(buf
);
169 if (boot_cpu_data
.hard_math
)
170 err
= restore_i387_hard(buf
);
172 err
= restore_i387_soft(¤t
->tss
.i387
.soft
, buf
);
174 current
->used_math
= 1;
179 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext
*sc
, int *peax
)
181 unsigned int err
= 0;
183 #define COPY(x) err |= __get_user(regs->x, &sc->x)
185 #define COPY_SEG(seg) \
186 { unsigned short tmp; \
187 err |= __get_user(tmp, &sc->seg); \
188 regs->x##seg = tmp; }
190 #define COPY_SEG_STRICT(seg) \
191 { unsigned short tmp; \
192 err |= __get_user(tmp, &sc->seg); \
193 regs->x##seg = tmp|3; }
195 #define GET_SEG(seg) \
196 { unsigned short tmp; \
197 err |= __get_user(tmp, &sc->seg); \
198 loadsegment(seg,tmp); }
216 unsigned int tmpflags
;
217 err
|= __get_user(tmpflags
, &sc
->eflags
);
218 regs
->eflags
= (regs
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
219 regs
->orig_eax
= -1; /* disable syscall checks */
223 struct _fpstate
* buf
;
224 err
|= __get_user(buf
, &sc
->fpstate
);
226 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
228 err
|= restore_i387(buf
);
232 err
|= __get_user(*peax
, &sc
->eax
);
239 asmlinkage
int sys_sigreturn(unsigned long __unused
)
241 struct pt_regs
*regs
= (struct pt_regs
*) &__unused
;
242 struct sigframe
*frame
= (struct sigframe
*)(regs
->esp
- 8);
246 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
248 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
250 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
251 sizeof(frame
->extramask
))))
254 sigdelsetmask(&set
, ~_BLOCKABLE
);
255 spin_lock_irq(¤t
->sigmask_lock
);
256 current
->blocked
= set
;
257 recalc_sigpending(current
);
258 spin_unlock_irq(¤t
->sigmask_lock
);
260 if (restore_sigcontext(regs
, &frame
->sc
, &eax
))
265 force_sig(SIGSEGV
, current
);
269 asmlinkage
int sys_rt_sigreturn(unsigned long __unused
)
271 struct pt_regs
*regs
= (struct pt_regs
*) &__unused
;
272 struct rt_sigframe
*frame
= (struct rt_sigframe
*)(regs
->esp
- 4);
277 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
279 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
282 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(regs
, &frame
->uc
.uc_mcontext
, &eax
))
291 if (__copy_from_user(&st
, &frame
->uc
.uc_stack
, sizeof(st
)))
293 /* It is more difficult to avoid calling this function than to
294 call it and ignore errors. */
295 do_sigaltstack(&st
, NULL
, regs
->esp
);
300 force_sig(SIGSEGV
, current
);
305 * Set up a signal frame.
308 static inline int save_i387_hard(struct _fpstate
* buf
)
310 if (current
->flags
& PF_USEDFPU
) {
311 current
->flags
&= ~PF_USEDFPU
;
312 __asm__
__volatile__("fnsave %0":"=m"(current
->tss
.i387
.hard
));
315 asm volatile("fwait");
316 current
->tss
.i387
.hard
.status
= current
->tss
.i387
.hard
.swd
;
317 if (__copy_to_user(buf
, ¤t
->tss
.i387
.hard
, sizeof(*buf
)))
322 static int save_i387(struct _fpstate
*buf
)
324 if (!current
->used_math
)
327 /* This will cause a "finit" to be triggered by the next
328 attempted FPU operation by the 'current' process.
330 current
->used_math
= 0;
332 #ifndef CONFIG_MATH_EMULATION
333 return save_i387_hard(buf
);
335 return boot_cpu_data
.hard_math
? save_i387_hard(buf
)
336 : save_i387_soft(¤t
->tss
.i387
.soft
, buf
);
341 setup_sigcontext(struct sigcontext
*sc
, struct _fpstate
*fpstate
,
342 struct pt_regs
*regs
, unsigned long mask
)
347 __asm__("movl %%gs,%w0" : "=r"(tmp
): "0"(tmp
));
348 err
|= __put_user(tmp
, (unsigned int *)&sc
->gs
);
349 __asm__("movl %%fs,%w0" : "=r"(tmp
): "0"(tmp
));
350 err
|= __put_user(tmp
, (unsigned int *)&sc
->fs
);
352 err
|= __put_user(regs
->xes
, (unsigned int *)&sc
->es
);
353 err
|= __put_user(regs
->xds
, (unsigned int *)&sc
->ds
);
354 err
|= __put_user(regs
->edi
, &sc
->edi
);
355 err
|= __put_user(regs
->esi
, &sc
->esi
);
356 err
|= __put_user(regs
->ebp
, &sc
->ebp
);
357 err
|= __put_user(regs
->esp
, &sc
->esp
);
358 err
|= __put_user(regs
->ebx
, &sc
->ebx
);
359 err
|= __put_user(regs
->edx
, &sc
->edx
);
360 err
|= __put_user(regs
->ecx
, &sc
->ecx
);
361 err
|= __put_user(regs
->eax
, &sc
->eax
);
362 err
|= __put_user(current
->tss
.trap_no
, &sc
->trapno
);
363 err
|= __put_user(current
->tss
.error_code
, &sc
->err
);
364 err
|= __put_user(regs
->eip
, &sc
->eip
);
365 err
|= __put_user(regs
->xcs
, (unsigned int *)&sc
->cs
);
366 err
|= __put_user(regs
->eflags
, &sc
->eflags
);
367 err
|= __put_user(regs
->esp
, &sc
->esp_at_signal
);
368 err
|= __put_user(regs
->xss
, (unsigned int *)&sc
->ss
);
370 tmp
= save_i387(fpstate
);
374 err
|= __put_user(tmp
? fpstate
: NULL
, &sc
->fpstate
);
376 /* non-iBCS2 extensions.. */
377 err
|= __put_user(mask
, &sc
->oldmask
);
378 err
|= __put_user(current
->tss
.cr2
, &sc
->cr2
);
384 * Determine which stack to use..
387 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
391 /* Default to using normal stack */
394 /* This is the X/Open sanctioned signal stack switching. */
395 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
396 if (! on_sig_stack(esp
))
397 esp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
400 /* This is the legacy signal stack switching. */
401 else if ((regs
->xss
& 0xffff) != __USER_DS
&&
402 !(ka
->sa
.sa_flags
& SA_RESTORER
) &&
403 ka
->sa
.sa_restorer
) {
404 esp
= (unsigned long) ka
->sa
.sa_restorer
;
407 return (void *)((esp
- frame_size
) & -8ul);
410 static void setup_frame(int sig
, struct k_sigaction
*ka
,
411 sigset_t
*set
, struct pt_regs
* regs
)
413 struct sigframe
*frame
;
416 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
418 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
421 err
|= __put_user((current
->exec_domain
422 && current
->exec_domain
->signal_invmap
424 ? current
->exec_domain
->signal_invmap
[sig
]
428 err
|= setup_sigcontext(&frame
->sc
, &frame
->fpstate
, regs
, set
->sig
[0]);
430 if (_NSIG_WORDS
> 1) {
431 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
432 sizeof(frame
->extramask
));
435 /* Set up to return from userspace. If provided, use a stub
436 already in userspace. */
437 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
438 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
440 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
441 /* This is popl %eax ; movl $,%eax ; int $0x80 */
442 err
|= __put_user(0xb858, (short *)(frame
->retcode
+0));
443 err
|= __put_user(__NR_sigreturn
, (int *)(frame
->retcode
+2));
444 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+6));
450 /* Set up registers for signal handler */
451 regs
->esp
= (unsigned long) frame
;
452 regs
->eip
= (unsigned long) ka
->sa
.sa_handler
;
455 regs
->xds
= __USER_DS
;
456 regs
->xes
= __USER_DS
;
457 regs
->xss
= __USER_DS
;
458 regs
->xcs
= __USER_CS
;
459 regs
->eflags
&= ~TF_MASK
;
462 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
463 current
->comm
, current
->pid
, frame
, regs
->eip
, frame
->pretcode
);
470 ka
->sa
.sa_handler
= SIG_DFL
;
471 force_sig(SIGSEGV
, current
);
474 static void setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
475 sigset_t
*set
, struct pt_regs
* regs
)
477 struct rt_sigframe
*frame
;
480 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
482 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
485 err
|= __put_user((current
->exec_domain
486 && current
->exec_domain
->signal_invmap
488 ? current
->exec_domain
->signal_invmap
[sig
]
491 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
492 err
|= __put_user(&frame
->uc
, &frame
->puc
);
493 err
|= __copy_to_user(&frame
->info
, info
, sizeof(*info
));
495 /* Create the ucontext. */
496 err
|= __put_user(0, &frame
->uc
.uc_flags
);
497 err
|= __put_user(0, &frame
->uc
.uc_link
);
498 err
|= __put_user(current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
499 err
|= __put_user(sas_ss_flags(regs
->esp
),
500 &frame
->uc
.uc_stack
.ss_flags
);
501 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
502 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, &frame
->fpstate
,
504 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
506 /* Set up to return from userspace. If provided, use a stub
507 already in userspace. */
508 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
509 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
511 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
512 /* This is movl $,%eax ; int $0x80 */
513 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
514 err
|= __put_user(__NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
515 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+5));
521 /* Set up registers for signal handler */
522 regs
->esp
= (unsigned long) frame
;
523 regs
->eip
= (unsigned long) ka
->sa
.sa_handler
;
526 regs
->xds
= __USER_DS
;
527 regs
->xes
= __USER_DS
;
528 regs
->xss
= __USER_DS
;
529 regs
->xcs
= __USER_CS
;
530 regs
->eflags
&= ~TF_MASK
;
533 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
534 current
->comm
, current
->pid
, frame
, regs
->eip
, frame
->pretcode
);
541 ka
->sa
.sa_handler
= SIG_DFL
;
542 force_sig(SIGSEGV
, current
);
546 * OK, we're invoking a handler
550 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
551 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
* regs
)
553 /* Are we from a system call? */
554 if (regs
->orig_eax
>= 0) {
555 /* If so, check system call restarting.. */
557 case -ERESTARTNOHAND
:
562 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
567 case -ERESTARTNOINTR
:
568 regs
->eax
= regs
->orig_eax
;
573 /* Set up the stack frame */
574 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
575 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
577 setup_frame(sig
, ka
, oldset
, regs
);
579 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
580 ka
->sa
.sa_handler
= SIG_DFL
;
582 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
583 spin_lock_irq(¤t
->sigmask_lock
);
584 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
585 sigaddset(¤t
->blocked
,sig
);
586 recalc_sigpending(current
);
587 spin_unlock_irq(¤t
->sigmask_lock
);
592 * Note that 'init' is a special process: it doesn't get signals it doesn't
593 * want to handle. Thus you cannot kill init even with a SIGKILL even by
596 * Note that we go through the signals twice: once to check the signals that
597 * the kernel can handle, and then we build all the user-level signal handling
598 * stack-frames in one go after that.
600 asmlinkage
int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
)
603 struct k_sigaction
*ka
;
606 * We want the common case to go fast, which
607 * is why we may in certain cases get here from
608 * kernel mode. Just return without doing anything
611 if ((regs
->xcs
& 3) != 3)
615 oldset
= ¤t
->blocked
;
620 spin_lock_irq(¤t
->sigmask_lock
);
621 signr
= dequeue_signal(¤t
->blocked
, &info
);
622 spin_unlock_irq(¤t
->sigmask_lock
);
627 if ((current
->flags
& PF_PTRACED
) && signr
!= SIGKILL
) {
628 /* Let the debugger run. */
629 current
->exit_code
= signr
;
630 current
->state
= TASK_STOPPED
;
631 notify_parent(current
, SIGCHLD
);
634 /* We're back. Did the debugger cancel the sig? */
635 if (!(signr
= current
->exit_code
))
637 current
->exit_code
= 0;
639 /* The debugger continued. Ignore SIGSTOP. */
640 if (signr
== SIGSTOP
)
643 /* Update the siginfo structure. Is this good? */
644 if (signr
!= info
.si_signo
) {
645 info
.si_signo
= signr
;
647 info
.si_code
= SI_USER
;
648 info
.si_pid
= current
->p_pptr
->pid
;
649 info
.si_uid
= current
->p_pptr
->uid
;
652 /* If the (new) signal is now blocked, requeue it. */
653 if (sigismember(¤t
->blocked
, signr
)) {
654 send_sig_info(signr
, &info
, current
);
659 ka
= ¤t
->sig
->action
[signr
-1];
660 if (ka
->sa
.sa_handler
== SIG_IGN
) {
661 if (signr
!= SIGCHLD
)
663 /* Check for SIGCHLD: it's special. */
664 while (sys_wait4(-1, NULL
, WNOHANG
, NULL
) > 0)
669 if (ka
->sa
.sa_handler
== SIG_DFL
) {
670 int exit_code
= signr
;
672 /* Init gets no signals it doesn't want. */
673 if (current
->pid
== 1)
677 case SIGCONT
: case SIGCHLD
: case SIGWINCH
:
680 case SIGTSTP
: case SIGTTIN
: case SIGTTOU
:
681 if (is_orphaned_pgrp(current
->pgrp
))
686 current
->state
= TASK_STOPPED
;
687 current
->exit_code
= signr
;
688 if (!(current
->p_pptr
->sig
->action
[SIGCHLD
-1].sa
.sa_flags
& SA_NOCLDSTOP
))
689 notify_parent(current
, SIGCHLD
);
693 case SIGQUIT
: case SIGILL
: case SIGTRAP
:
694 case SIGABRT
: case SIGFPE
: case SIGSEGV
:
697 && current
->binfmt
->core_dump
698 && current
->binfmt
->core_dump(signr
, regs
))
705 sigaddset(¤t
->signal
, signr
);
706 current
->flags
|= PF_SIGNALED
;
712 /* Whee! Actually deliver the signal. */
713 handle_signal(signr
, ka
, &info
, oldset
, regs
);
717 /* Did we come from a system call? */
718 if (regs
->orig_eax
>= 0) {
719 /* Restart the system call - no handlers present */
720 if (regs
->eax
== -ERESTARTNOHAND
||
721 regs
->eax
== -ERESTARTSYS
||
722 regs
->eax
== -ERESTARTNOINTR
) {
723 regs
->eax
= regs
->orig_eax
;