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 if ((tmp & 0xfffc) /* not a NULL selectors */ \
189 && (tmp & 0x4) != 0x4 /* not a LDT selector */ \
190 && (tmp & 3) != 3) /* not a RPL3 GDT selector */ \
192 regs->x##seg = tmp; }
194 #define COPY_SEG_STRICT(seg) \
195 { unsigned short tmp; \
196 err |= __get_user(tmp, &sc->seg); \
197 if ((tmp & 0xfffc) && (tmp & 3) != 3) goto badframe; \
198 regs->x##seg = tmp; }
200 #define GET_SEG(seg) \
201 { unsigned short tmp; \
202 err |= __get_user(tmp, &sc->seg); \
203 if ((tmp & 0xfffc) /* not a NULL selectors */ \
204 && (tmp & 0x4) != 0x4 /* not a LDT selector */ \
205 && (tmp & 3) != 3) /* not a RPL3 GDT selector */ \
207 loadsegment(seg,tmp); }
225 unsigned int tmpflags
;
226 err
|= __get_user(tmpflags
, &sc
->eflags
);
227 regs
->eflags
= (regs
->eflags
& ~0x40DD5) | (tmpflags
& 0x40DD5);
228 regs
->orig_eax
= -1; /* disable syscall checks */
232 struct _fpstate
* buf
;
233 err
|= __get_user(buf
, &sc
->fpstate
);
235 if (verify_area(VERIFY_READ
, buf
, sizeof(*buf
)))
237 err
|= restore_i387(buf
);
241 err
|= __get_user(*peax
, &sc
->eax
);
248 asmlinkage
int sys_sigreturn(unsigned long __unused
)
250 struct pt_regs
*regs
= (struct pt_regs
*) &__unused
;
251 struct sigframe
*frame
= (struct sigframe
*)(regs
->esp
- 8);
255 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
257 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
)
259 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
260 sizeof(frame
->extramask
))))
263 sigdelsetmask(&set
, ~_BLOCKABLE
);
264 spin_lock_irq(¤t
->sigmask_lock
);
265 current
->blocked
= set
;
266 recalc_sigpending(current
);
267 spin_unlock_irq(¤t
->sigmask_lock
);
269 if (restore_sigcontext(regs
, &frame
->sc
, &eax
))
274 force_sig(SIGSEGV
, current
);
278 asmlinkage
int sys_rt_sigreturn(unsigned long __unused
)
280 struct pt_regs
*regs
= (struct pt_regs
*) &__unused
;
281 struct rt_sigframe
*frame
= (struct rt_sigframe
*)(regs
->esp
- 4);
286 if (verify_area(VERIFY_READ
, frame
, sizeof(*frame
)))
288 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
291 sigdelsetmask(&set
, ~_BLOCKABLE
);
292 spin_lock_irq(¤t
->sigmask_lock
);
293 current
->blocked
= set
;
294 recalc_sigpending(current
);
295 spin_unlock_irq(¤t
->sigmask_lock
);
297 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &eax
))
300 if (__copy_from_user(&st
, &frame
->uc
.uc_stack
, sizeof(st
)))
302 /* It is more difficult to avoid calling this function than to
303 call it and ignore errors. */
304 do_sigaltstack(&st
, NULL
, regs
->esp
);
309 force_sig(SIGSEGV
, current
);
314 * Set up a signal frame.
317 static inline int save_i387_hard(struct _fpstate
* buf
)
319 if (current
->flags
& PF_USEDFPU
) {
320 current
->flags
&= ~PF_USEDFPU
;
321 __asm__
__volatile__("fnsave %0":"=m"(current
->tss
.i387
.hard
));
324 asm volatile("fwait");
325 current
->tss
.i387
.hard
.status
= current
->tss
.i387
.hard
.swd
;
326 if (__copy_to_user(buf
, ¤t
->tss
.i387
.hard
, sizeof(*buf
)))
331 static int save_i387(struct _fpstate
*buf
)
333 if (!current
->used_math
)
336 /* This will cause a "finit" to be triggered by the next
337 attempted FPU operation by the 'current' process.
339 current
->used_math
= 0;
341 #ifndef CONFIG_MATH_EMULATION
342 return save_i387_hard(buf
);
344 return boot_cpu_data
.hard_math
? save_i387_hard(buf
)
345 : save_i387_soft(¤t
->tss
.i387
.soft
, buf
);
350 setup_sigcontext(struct sigcontext
*sc
, struct _fpstate
*fpstate
,
351 struct pt_regs
*regs
, unsigned long mask
)
356 __asm__("movl %%gs,%w0" : "=r"(tmp
): "0"(tmp
));
357 err
|= __put_user(tmp
, (unsigned int *)&sc
->gs
);
358 __asm__("movl %%fs,%w0" : "=r"(tmp
): "0"(tmp
));
359 err
|= __put_user(tmp
, (unsigned int *)&sc
->fs
);
361 err
|= __put_user(regs
->xes
, (unsigned int *)&sc
->es
);
362 err
|= __put_user(regs
->xds
, (unsigned int *)&sc
->ds
);
363 err
|= __put_user(regs
->edi
, &sc
->edi
);
364 err
|= __put_user(regs
->esi
, &sc
->esi
);
365 err
|= __put_user(regs
->ebp
, &sc
->ebp
);
366 err
|= __put_user(regs
->esp
, &sc
->esp
);
367 err
|= __put_user(regs
->ebx
, &sc
->ebx
);
368 err
|= __put_user(regs
->edx
, &sc
->edx
);
369 err
|= __put_user(regs
->ecx
, &sc
->ecx
);
370 err
|= __put_user(regs
->eax
, &sc
->eax
);
371 err
|= __put_user(current
->tss
.trap_no
, &sc
->trapno
);
372 err
|= __put_user(current
->tss
.error_code
, &sc
->err
);
373 err
|= __put_user(regs
->eip
, &sc
->eip
);
374 err
|= __put_user(regs
->xcs
, (unsigned int *)&sc
->cs
);
375 err
|= __put_user(regs
->eflags
, &sc
->eflags
);
376 err
|= __put_user(regs
->esp
, &sc
->esp_at_signal
);
377 err
|= __put_user(regs
->xss
, (unsigned int *)&sc
->ss
);
379 tmp
= save_i387(fpstate
);
383 err
|= __put_user(tmp
? fpstate
: NULL
, &sc
->fpstate
);
385 /* non-iBCS2 extensions.. */
386 err
|= __put_user(mask
, &sc
->oldmask
);
387 err
|= __put_user(current
->tss
.cr2
, &sc
->cr2
);
393 * Determine which stack to use..
396 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
400 /* Default to using normal stack */
403 /* This is the X/Open sanctioned signal stack switching. */
404 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
405 if (! on_sig_stack(esp
))
406 esp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
409 /* This is the legacy signal stack switching. */
410 else if ((regs
->xss
& 0xffff) != __USER_DS
&&
411 !(ka
->sa
.sa_flags
& SA_RESTORER
) &&
412 ka
->sa
.sa_restorer
) {
413 esp
= (unsigned long) ka
->sa
.sa_restorer
;
416 return (void *)((esp
- frame_size
) & -8ul);
419 static void setup_frame(int sig
, struct k_sigaction
*ka
,
420 sigset_t
*set
, struct pt_regs
* regs
)
422 struct sigframe
*frame
;
425 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
427 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
430 err
|= __put_user((current
->exec_domain
431 && current
->exec_domain
->signal_invmap
433 ? current
->exec_domain
->signal_invmap
[sig
]
437 err
|= setup_sigcontext(&frame
->sc
, &frame
->fpstate
, regs
, set
->sig
[0]);
439 if (_NSIG_WORDS
> 1) {
440 err
|= __copy_to_user(frame
->extramask
, &set
->sig
[1],
441 sizeof(frame
->extramask
));
444 /* Set up to return from userspace. If provided, use a stub
445 already in userspace. */
446 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
447 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
449 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
450 /* This is popl %eax ; movl $,%eax ; int $0x80 */
451 err
|= __put_user(0xb858, (short *)(frame
->retcode
+0));
452 err
|= __put_user(__NR_sigreturn
, (int *)(frame
->retcode
+2));
453 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+6));
459 /* Set up registers for signal handler */
460 regs
->esp
= (unsigned long) frame
;
461 regs
->eip
= (unsigned long) ka
->sa
.sa_handler
;
463 unsigned long seg
= __USER_DS
;
464 __asm__("movl %w0,%%fs ; movl %w0,%%gs": "=r"(seg
) : "0"(seg
));
469 regs
->xcs
= __USER_CS
;
471 regs
->eflags
&= ~TF_MASK
;
474 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
475 current
->comm
, current
->pid
, frame
, regs
->eip
, frame
->pretcode
);
482 ka
->sa
.sa_handler
= SIG_DFL
;
483 force_sig(SIGSEGV
, current
);
486 static void setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
487 sigset_t
*set
, struct pt_regs
* regs
)
489 struct rt_sigframe
*frame
;
492 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
494 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
497 err
|= __put_user((current
->exec_domain
498 && current
->exec_domain
->signal_invmap
500 ? current
->exec_domain
->signal_invmap
[sig
]
503 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
504 err
|= __put_user(&frame
->uc
, &frame
->puc
);
505 err
|= __copy_to_user(&frame
->info
, info
, sizeof(*info
));
507 /* Create the ucontext. */
508 err
|= __put_user(0, &frame
->uc
.uc_flags
);
509 err
|= __put_user(0, &frame
->uc
.uc_link
);
510 err
|= __put_user(current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
511 err
|= __put_user(sas_ss_flags(regs
->esp
),
512 &frame
->uc
.uc_stack
.ss_flags
);
513 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
514 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, &frame
->fpstate
,
516 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
518 /* Set up to return from userspace. If provided, use a stub
519 already in userspace. */
520 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
521 err
|= __put_user(ka
->sa
.sa_restorer
, &frame
->pretcode
);
523 err
|= __put_user(frame
->retcode
, &frame
->pretcode
);
524 /* This is movl $,%eax ; int $0x80 */
525 err
|= __put_user(0xb8, (char *)(frame
->retcode
+0));
526 err
|= __put_user(__NR_rt_sigreturn
, (int *)(frame
->retcode
+1));
527 err
|= __put_user(0x80cd, (short *)(frame
->retcode
+5));
533 /* Set up registers for signal handler */
534 regs
->esp
= (unsigned long) frame
;
535 regs
->eip
= (unsigned long) ka
->sa
.sa_handler
;
537 unsigned long seg
= __USER_DS
;
538 __asm__("movl %w0,%%fs ; movl %w0,%%gs": "=r"(seg
) : "0"(seg
));
543 regs
->xcs
= __USER_CS
;
545 regs
->eflags
&= ~TF_MASK
;
548 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
549 current
->comm
, current
->pid
, frame
, regs
->eip
, frame
->pretcode
);
556 ka
->sa
.sa_handler
= SIG_DFL
;
557 force_sig(SIGSEGV
, current
);
561 * OK, we're invoking a handler
565 handle_signal(unsigned long sig
, struct k_sigaction
*ka
,
566 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
* regs
)
568 /* Are we from a system call? */
569 if (regs
->orig_eax
>= 0) {
570 /* If so, check system call restarting.. */
572 case -ERESTARTNOHAND
:
577 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
582 case -ERESTARTNOINTR
:
583 regs
->eax
= regs
->orig_eax
;
588 /* Set up the stack frame */
589 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
590 setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
592 setup_frame(sig
, ka
, oldset
, regs
);
594 if (ka
->sa
.sa_flags
& SA_ONESHOT
)
595 ka
->sa
.sa_handler
= SIG_DFL
;
597 if (!(ka
->sa
.sa_flags
& SA_NODEFER
)) {
598 spin_lock_irq(¤t
->sigmask_lock
);
599 sigorsets(¤t
->blocked
,¤t
->blocked
,&ka
->sa
.sa_mask
);
600 sigaddset(¤t
->blocked
,sig
);
601 recalc_sigpending(current
);
602 spin_unlock_irq(¤t
->sigmask_lock
);
607 * Note that 'init' is a special process: it doesn't get signals it doesn't
608 * want to handle. Thus you cannot kill init even with a SIGKILL even by
611 * Note that we go through the signals twice: once to check the signals that
612 * the kernel can handle, and then we build all the user-level signal handling
613 * stack-frames in one go after that.
615 asmlinkage
int do_signal(sigset_t
*oldset
, struct pt_regs
*regs
)
618 struct k_sigaction
*ka
;
621 * We want the common case to go fast, which
622 * is why we may in certain cases get here from
623 * kernel mode. Just return without doing anything
626 if ((regs
->xcs
& 3) != 3)
630 oldset
= ¤t
->blocked
;
635 spin_lock_irq(¤t
->sigmask_lock
);
636 signr
= dequeue_signal(¤t
->blocked
, &info
);
637 spin_unlock_irq(¤t
->sigmask_lock
);
642 if ((current
->flags
& PF_PTRACED
) && signr
!= SIGKILL
) {
643 /* Let the debugger run. */
644 current
->exit_code
= signr
;
645 current
->state
= TASK_STOPPED
;
646 notify_parent(current
, SIGCHLD
);
649 /* We're back. Did the debugger cancel the sig? */
650 if (!(signr
= current
->exit_code
))
652 current
->exit_code
= 0;
654 /* The debugger continued. Ignore SIGSTOP. */
655 if (signr
== SIGSTOP
)
658 /* Update the siginfo structure. Is this good? */
659 if (signr
!= info
.si_signo
) {
660 info
.si_signo
= signr
;
662 info
.si_code
= SI_USER
;
663 info
.si_pid
= current
->p_pptr
->pid
;
664 info
.si_uid
= current
->p_pptr
->uid
;
667 /* If the (new) signal is now blocked, requeue it. */
668 if (sigismember(¤t
->blocked
, signr
)) {
669 send_sig_info(signr
, &info
, current
);
674 ka
= ¤t
->sig
->action
[signr
-1];
675 if (ka
->sa
.sa_handler
== SIG_IGN
) {
676 if (signr
!= SIGCHLD
)
678 /* Check for SIGCHLD: it's special. */
679 while (sys_wait4(-1, NULL
, WNOHANG
, NULL
) > 0)
684 if (ka
->sa
.sa_handler
== SIG_DFL
) {
685 int exit_code
= signr
;
687 /* Init gets no signals it doesn't want. */
688 if (current
->pid
== 1)
692 case SIGCONT
: case SIGCHLD
: case SIGWINCH
:
695 case SIGTSTP
: case SIGTTIN
: case SIGTTOU
:
696 if (is_orphaned_pgrp(current
->pgrp
))
701 current
->state
= TASK_STOPPED
;
702 current
->exit_code
= signr
;
703 if (!(current
->p_pptr
->sig
->action
[SIGCHLD
-1].sa
.sa_flags
& SA_NOCLDSTOP
))
704 notify_parent(current
, SIGCHLD
);
708 case SIGQUIT
: case SIGILL
: case SIGTRAP
:
709 case SIGABRT
: case SIGFPE
: case SIGSEGV
:
712 && current
->binfmt
->core_dump
713 && current
->binfmt
->core_dump(signr
, regs
))
720 sigaddset(¤t
->signal
, signr
);
721 current
->flags
|= PF_SIGNALED
;
727 /* Whee! Actually deliver the signal. */
728 handle_signal(signr
, ka
, &info
, oldset
, regs
);
732 /* Did we come from a system call? */
733 if (regs
->orig_eax
>= 0) {
734 /* Restart the system call - no handlers present */
735 if (regs
->eax
== -ERESTARTNOHAND
||
736 regs
->eax
== -ERESTARTSYS
||
737 regs
->eax
== -ERESTARTNOINTR
) {
738 regs
->eax
= regs
->orig_eax
;