2 * Copyright (C) 1991, 1992 Linus Torvalds
4 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
5 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
7 #include <linux/list.h>
9 #include <linux/personality.h>
10 #include <linux/binfmts.h>
11 #include <linux/suspend.h>
12 #include <linux/kernel.h>
13 #include <linux/ptrace.h>
14 #include <linux/signal.h>
15 #include <linux/stddef.h>
16 #include <linux/unistd.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/tracehook.h>
21 #include <linux/elf.h>
22 #include <linux/smp.h>
25 #include <asm/processor.h>
26 #include <asm/ucontext.h>
27 #include <asm/uaccess.h>
30 #include <asm/syscall.h>
31 #include <asm/syscalls.h>
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
38 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
39 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
43 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
45 # define FIX_EFLAGS __FIX_EFLAGS
49 * Atomically swap in the new signal mask, and wait for a signal.
52 sys_sigsuspend(int history0
, int history1
, old_sigset_t mask
)
55 spin_lock_irq(¤t
->sighand
->siglock
);
56 current
->saved_sigmask
= current
->blocked
;
57 siginitset(¤t
->blocked
, mask
);
59 spin_unlock_irq(¤t
->sighand
->siglock
);
61 current
->state
= TASK_INTERRUPTIBLE
;
63 set_restore_sigmask();
65 return -ERESTARTNOHAND
;
69 sys_sigaction(int sig
, const struct old_sigaction __user
*act
,
70 struct old_sigaction __user
*oact
)
72 struct k_sigaction new_ka
, old_ka
;
78 if (!access_ok(VERIFY_READ
, act
, sizeof(*act
)) ||
79 __get_user(new_ka
.sa
.sa_handler
, &act
->sa_handler
) ||
80 __get_user(new_ka
.sa
.sa_restorer
, &act
->sa_restorer
))
83 __get_user(new_ka
.sa
.sa_flags
, &act
->sa_flags
);
84 __get_user(mask
, &act
->sa_mask
);
85 siginitset(&new_ka
.sa
.sa_mask
, mask
);
88 ret
= do_sigaction(sig
, act
? &new_ka
: NULL
, oact
? &old_ka
: NULL
);
91 if (!access_ok(VERIFY_WRITE
, oact
, sizeof(*oact
)) ||
92 __put_user(old_ka
.sa
.sa_handler
, &oact
->sa_handler
) ||
93 __put_user(old_ka
.sa
.sa_restorer
, &oact
->sa_restorer
))
96 __put_user(old_ka
.sa
.sa_flags
, &oact
->sa_flags
);
97 __put_user(old_ka
.sa
.sa_mask
.sig
[0], &oact
->sa_mask
);
103 asmlinkage
int sys_sigaltstack(unsigned long bx
)
106 * This is needed to make gcc realize it doesn't own the
109 struct pt_regs
*regs
= (struct pt_regs
*)&bx
;
110 const stack_t __user
*uss
= (const stack_t __user
*)bx
;
111 stack_t __user
*uoss
= (stack_t __user
*)regs
->cx
;
113 return do_sigaltstack(uss
, uoss
, regs
->sp
);
117 err |= __get_user(regs->x, &sc->x); \
120 #define COPY_SEG(seg) { \
121 unsigned short tmp; \
122 err |= __get_user(tmp, &sc->seg); \
126 #define COPY_SEG_STRICT(seg) { \
127 unsigned short tmp; \
128 err |= __get_user(tmp, &sc->seg); \
129 regs->seg = tmp | 3; \
132 #define GET_SEG(seg) { \
133 unsigned short tmp; \
134 err |= __get_user(tmp, &sc->seg); \
135 loadsegment(seg, tmp); \
139 * Do a signal return; undo the signal stack.
142 restore_sigcontext(struct pt_regs
*regs
, struct sigcontext __user
*sc
,
146 unsigned int tmpflags
;
147 unsigned int err
= 0;
149 /* Always make any pending restarted system calls return -EINTR */
150 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
157 #endif /* CONFIG_X86_32 */
159 COPY(di
); COPY(si
); COPY(bp
); COPY(sp
); COPY(bx
);
160 COPY(dx
); COPY(cx
); COPY(ip
);
171 #endif /* CONFIG_X86_64 */
176 #else /* !CONFIG_X86_32 */
177 /* Kernel saves and restores only the CS segment register on signals,
178 * which is the bare minimum needed to allow mixed 32/64-bit code.
179 * App's signal handler can save/restore other segments if needed. */
181 #endif /* CONFIG_X86_32 */
183 err
|= __get_user(tmpflags
, &sc
->flags
);
184 regs
->flags
= (regs
->flags
& ~FIX_EFLAGS
) | (tmpflags
& FIX_EFLAGS
);
185 regs
->orig_ax
= -1; /* disable syscall checks */
187 err
|= __get_user(buf
, &sc
->fpstate
);
188 err
|= restore_i387_xstate(buf
);
190 err
|= __get_user(*pax
, &sc
->ax
);
194 asmlinkage
unsigned long sys_sigreturn(unsigned long __unused
)
196 struct sigframe __user
*frame
;
197 struct pt_regs
*regs
;
201 regs
= (struct pt_regs
*) &__unused
;
202 frame
= (struct sigframe __user
*)(regs
->sp
- 8);
204 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
206 if (__get_user(set
.sig
[0], &frame
->sc
.oldmask
) || (_NSIG_WORDS
> 1
207 && __copy_from_user(&set
.sig
[1], &frame
->extramask
,
208 sizeof(frame
->extramask
))))
211 sigdelsetmask(&set
, ~_BLOCKABLE
);
212 spin_lock_irq(¤t
->sighand
->siglock
);
213 current
->blocked
= set
;
215 spin_unlock_irq(¤t
->sighand
->siglock
);
217 if (restore_sigcontext(regs
, &frame
->sc
, &ax
))
222 if (show_unhandled_signals
&& printk_ratelimit()) {
223 printk("%s%s[%d] bad frame in sigreturn frame:"
224 "%p ip:%lx sp:%lx oeax:%lx",
225 task_pid_nr(current
) > 1 ? KERN_INFO
: KERN_EMERG
,
226 current
->comm
, task_pid_nr(current
), frame
, regs
->ip
,
227 regs
->sp
, regs
->orig_ax
);
228 print_vma_addr(" in ", regs
->ip
);
229 printk(KERN_CONT
"\n");
232 force_sig(SIGSEGV
, current
);
237 static long do_rt_sigreturn(struct pt_regs
*regs
)
239 struct rt_sigframe __user
*frame
;
243 frame
= (struct rt_sigframe __user
*)(regs
->sp
- sizeof(long));
244 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
246 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
249 sigdelsetmask(&set
, ~_BLOCKABLE
);
250 spin_lock_irq(¤t
->sighand
->siglock
);
251 current
->blocked
= set
;
253 spin_unlock_irq(¤t
->sighand
->siglock
);
255 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
, &ax
))
258 if (do_sigaltstack(&frame
->uc
.uc_stack
, NULL
, regs
->sp
) == -EFAULT
)
264 signal_fault(regs
, frame
, "rt_sigreturn");
268 asmlinkage
int sys_rt_sigreturn(unsigned long __unused
)
270 struct pt_regs
*regs
= (struct pt_regs
*)&__unused
;
272 return do_rt_sigreturn(regs
);
276 * Set up a signal frame.
279 setup_sigcontext(struct sigcontext __user
*sc
, void __user
*fpstate
,
280 struct pt_regs
*regs
, unsigned long mask
)
284 err
|= __put_user(regs
->fs
, (unsigned int __user
*)&sc
->fs
);
285 savesegment(gs
, tmp
);
286 err
|= __put_user(tmp
, (unsigned int __user
*)&sc
->gs
);
288 err
|= __put_user(regs
->es
, (unsigned int __user
*)&sc
->es
);
289 err
|= __put_user(regs
->ds
, (unsigned int __user
*)&sc
->ds
);
290 err
|= __put_user(regs
->di
, &sc
->di
);
291 err
|= __put_user(regs
->si
, &sc
->si
);
292 err
|= __put_user(regs
->bp
, &sc
->bp
);
293 err
|= __put_user(regs
->sp
, &sc
->sp
);
294 err
|= __put_user(regs
->bx
, &sc
->bx
);
295 err
|= __put_user(regs
->dx
, &sc
->dx
);
296 err
|= __put_user(regs
->cx
, &sc
->cx
);
297 err
|= __put_user(regs
->ax
, &sc
->ax
);
298 err
|= __put_user(current
->thread
.trap_no
, &sc
->trapno
);
299 err
|= __put_user(current
->thread
.error_code
, &sc
->err
);
300 err
|= __put_user(regs
->ip
, &sc
->ip
);
301 err
|= __put_user(regs
->cs
, (unsigned int __user
*)&sc
->cs
);
302 err
|= __put_user(regs
->flags
, &sc
->flags
);
303 err
|= __put_user(regs
->sp
, &sc
->sp_at_signal
);
304 err
|= __put_user(regs
->ss
, (unsigned int __user
*)&sc
->ss
);
306 err
|= __put_user(fpstate
, &sc
->fpstate
);
308 /* non-iBCS2 extensions.. */
309 err
|= __put_user(mask
, &sc
->oldmask
);
310 err
|= __put_user(current
->thread
.cr2
, &sc
->cr2
);
316 * Determine which stack to use..
318 static inline void __user
*
319 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
*regs
, size_t frame_size
,
324 /* Default to using normal stack */
328 * If we are on the alternate signal stack and would overflow it, don't.
329 * Return an always-bogus address instead so we will die with SIGSEGV.
331 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- frame_size
)))
332 return (void __user
*) -1L;
334 /* This is the X/Open sanctioned signal stack switching. */
335 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
336 if (sas_ss_flags(sp
) == 0)
337 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
339 /* This is the legacy signal stack switching. */
340 if ((regs
->ss
& 0xffff) != __USER_DS
&&
341 !(ka
->sa
.sa_flags
& SA_RESTORER
) &&
343 sp
= (unsigned long) ka
->sa
.sa_restorer
;
347 sp
= sp
- sig_xstate_size
;
348 *fpstate
= (struct _fpstate
*) sp
;
349 if (save_i387_xstate(*fpstate
) < 0)
350 return (void __user
*)-1L;
355 * Align the stack pointer according to the i386 ABI,
356 * i.e. so that on function entry ((sp + 4) & 15) == 0.
358 sp
= ((sp
+ 4) & -16ul) - 4;
360 return (void __user
*) sp
;
364 __setup_frame(int sig
, struct k_sigaction
*ka
, sigset_t
*set
,
365 struct pt_regs
*regs
)
367 struct sigframe __user
*frame
;
368 void __user
*restorer
;
370 void __user
*fpstate
= NULL
;
372 frame
= get_sigframe(ka
, regs
, sizeof(*frame
), &fpstate
);
374 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
377 if (__put_user(sig
, &frame
->sig
))
380 if (setup_sigcontext(&frame
->sc
, fpstate
, regs
, set
->sig
[0]))
383 if (_NSIG_WORDS
> 1) {
384 if (__copy_to_user(&frame
->extramask
, &set
->sig
[1],
385 sizeof(frame
->extramask
)))
389 if (current
->mm
->context
.vdso
)
390 restorer
= VDSO32_SYMBOL(current
->mm
->context
.vdso
, sigreturn
);
392 restorer
= &frame
->retcode
;
393 if (ka
->sa
.sa_flags
& SA_RESTORER
)
394 restorer
= ka
->sa
.sa_restorer
;
396 /* Set up to return from userspace. */
397 err
|= __put_user(restorer
, &frame
->pretcode
);
400 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
402 * WE DO NOT USE IT ANY MORE! It's only left here for historical
403 * reasons and because gdb uses it as a signature to notice
404 * signal handler stack frames.
406 err
|= __put_user(0xb858, (short __user
*)(frame
->retcode
+0));
407 err
|= __put_user(__NR_sigreturn
, (int __user
*)(frame
->retcode
+2));
408 err
|= __put_user(0x80cd, (short __user
*)(frame
->retcode
+6));
413 /* Set up registers for signal handler */
414 regs
->sp
= (unsigned long)frame
;
415 regs
->ip
= (unsigned long)ka
->sa
.sa_handler
;
416 regs
->ax
= (unsigned long)sig
;
420 regs
->ds
= __USER_DS
;
421 regs
->es
= __USER_DS
;
422 regs
->ss
= __USER_DS
;
423 regs
->cs
= __USER_CS
;
428 static int __setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
429 sigset_t
*set
, struct pt_regs
*regs
)
431 struct rt_sigframe __user
*frame
;
432 void __user
*restorer
;
434 void __user
*fpstate
= NULL
;
436 frame
= get_sigframe(ka
, regs
, sizeof(*frame
), &fpstate
);
438 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
441 err
|= __put_user(sig
, &frame
->sig
);
442 err
|= __put_user(&frame
->info
, &frame
->pinfo
);
443 err
|= __put_user(&frame
->uc
, &frame
->puc
);
444 err
|= copy_siginfo_to_user(&frame
->info
, info
);
448 /* Create the ucontext. */
450 err
|= __put_user(UC_FP_XSTATE
, &frame
->uc
.uc_flags
);
452 err
|= __put_user(0, &frame
->uc
.uc_flags
);
453 err
|= __put_user(0, &frame
->uc
.uc_link
);
454 err
|= __put_user(current
->sas_ss_sp
, &frame
->uc
.uc_stack
.ss_sp
);
455 err
|= __put_user(sas_ss_flags(regs
->sp
),
456 &frame
->uc
.uc_stack
.ss_flags
);
457 err
|= __put_user(current
->sas_ss_size
, &frame
->uc
.uc_stack
.ss_size
);
458 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, fpstate
,
460 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
464 /* Set up to return from userspace. */
465 restorer
= VDSO32_SYMBOL(current
->mm
->context
.vdso
, rt_sigreturn
);
466 if (ka
->sa
.sa_flags
& SA_RESTORER
)
467 restorer
= ka
->sa
.sa_restorer
;
468 err
|= __put_user(restorer
, &frame
->pretcode
);
471 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
473 * WE DO NOT USE IT ANY MORE! It's only left here for historical
474 * reasons and because gdb uses it as a signature to notice
475 * signal handler stack frames.
477 err
|= __put_user(0xb8, (char __user
*)(frame
->retcode
+0));
478 err
|= __put_user(__NR_rt_sigreturn
, (int __user
*)(frame
->retcode
+1));
479 err
|= __put_user(0x80cd, (short __user
*)(frame
->retcode
+5));
484 /* Set up registers for signal handler */
485 regs
->sp
= (unsigned long)frame
;
486 regs
->ip
= (unsigned long)ka
->sa
.sa_handler
;
487 regs
->ax
= (unsigned long)sig
;
488 regs
->dx
= (unsigned long)&frame
->info
;
489 regs
->cx
= (unsigned long)&frame
->uc
;
491 regs
->ds
= __USER_DS
;
492 regs
->es
= __USER_DS
;
493 regs
->ss
= __USER_DS
;
494 regs
->cs
= __USER_CS
;
500 * OK, we're invoking a handler:
502 static int signr_convert(int sig
)
505 struct thread_info
*info
= current_thread_info();
507 if (info
->exec_domain
&& info
->exec_domain
->signal_invmap
&& sig
< 32)
508 return info
->exec_domain
->signal_invmap
[sig
];
509 #endif /* CONFIG_X86_32 */
516 #define ia32_setup_frame __setup_frame
517 #define ia32_setup_rt_frame __setup_rt_frame
519 #else /* !CONFIG_X86_32 */
521 #ifdef CONFIG_IA32_EMULATION
522 #define is_ia32 test_thread_flag(TIF_IA32)
523 #else /* !CONFIG_IA32_EMULATION */
525 #endif /* CONFIG_IA32_EMULATION */
527 #endif /* CONFIG_X86_32 */
530 setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
531 sigset_t
*set
, struct pt_regs
*regs
)
533 int usig
= signr_convert(sig
);
536 /* Set up the stack frame */
538 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
539 ret
= ia32_setup_rt_frame(usig
, ka
, info
, set
, regs
);
541 ret
= ia32_setup_frame(usig
, ka
, set
, regs
);
543 ret
= __setup_rt_frame(sig
, ka
, info
, set
, regs
);
546 force_sigsegv(sig
, current
);
554 handle_signal(unsigned long sig
, siginfo_t
*info
, struct k_sigaction
*ka
,
555 sigset_t
*oldset
, struct pt_regs
*regs
)
559 /* Are we from a system call? */
560 if (syscall_get_nr(current
, regs
) >= 0) {
561 /* If so, check system call restarting.. */
562 switch (syscall_get_error(current
, regs
)) {
563 case -ERESTART_RESTARTBLOCK
:
564 case -ERESTARTNOHAND
:
569 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
574 case -ERESTARTNOINTR
:
575 regs
->ax
= regs
->orig_ax
;
582 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
583 * flag so that register information in the sigcontext is correct.
585 if (unlikely(regs
->flags
& X86_EFLAGS_TF
) &&
586 likely(test_and_clear_thread_flag(TIF_FORCED_TF
)))
587 regs
->flags
&= ~X86_EFLAGS_TF
;
589 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
596 * This has nothing to do with segment registers,
597 * despite the name. This magic affects uaccess.h
598 * macros' behavior. Reset it to the normal setting.
604 * Clear the direction flag as per the ABI for function entry.
606 regs
->flags
&= ~X86_EFLAGS_DF
;
609 * Clear TF when entering the signal handler, but
610 * notify any tracer that was single-stepping it.
611 * The tracer may want to single-step inside the
614 regs
->flags
&= ~X86_EFLAGS_TF
;
616 spin_lock_irq(¤t
->sighand
->siglock
);
617 sigorsets(¤t
->blocked
, ¤t
->blocked
, &ka
->sa
.sa_mask
);
618 if (!(ka
->sa
.sa_flags
& SA_NODEFER
))
619 sigaddset(¤t
->blocked
, sig
);
621 spin_unlock_irq(¤t
->sighand
->siglock
);
623 tracehook_signal_handler(sig
, info
, ka
, regs
,
624 test_thread_flag(TIF_SINGLESTEP
));
630 #define NR_restart_syscall __NR_restart_syscall
631 #else /* !CONFIG_X86_32 */
632 #define NR_restart_syscall \
633 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
634 #endif /* CONFIG_X86_32 */
637 * Note that 'init' is a special process: it doesn't get signals it doesn't
638 * want to handle. Thus you cannot kill init even with a SIGKILL even by
641 static void do_signal(struct pt_regs
*regs
)
643 struct k_sigaction ka
;
649 * We want the common case to go fast, which is why we may in certain
650 * cases get here from kernel mode. Just return without doing anything
652 * X86_32: vm86 regs switched out by assembly code before reaching
653 * here, so testing against kernel CS suffices.
655 if (!user_mode(regs
))
658 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
)
659 oldset
= ¤t
->saved_sigmask
;
661 oldset
= ¤t
->blocked
;
663 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
666 * Re-enable any watchpoints before delivering the
667 * signal to user space. The processor register will
668 * have been cleared if the watchpoint triggered
671 if (current
->thread
.debugreg7
)
672 set_debugreg(current
->thread
.debugreg7
, 7);
674 /* Whee! Actually deliver the signal. */
675 if (handle_signal(signr
, &info
, &ka
, oldset
, regs
) == 0) {
677 * A signal was successfully delivered; the saved
678 * sigmask will have been stored in the signal frame,
679 * and will be restored by sigreturn, so we can simply
680 * clear the TS_RESTORE_SIGMASK flag.
682 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
687 /* Did we come from a system call? */
688 if (syscall_get_nr(current
, regs
) >= 0) {
689 /* Restart the system call - no handlers present */
690 switch (syscall_get_error(current
, regs
)) {
691 case -ERESTARTNOHAND
:
693 case -ERESTARTNOINTR
:
694 regs
->ax
= regs
->orig_ax
;
698 case -ERESTART_RESTARTBLOCK
:
699 regs
->ax
= NR_restart_syscall
;
706 * If there's no signal to deliver, we just put the saved sigmask
709 if (current_thread_info()->status
& TS_RESTORE_SIGMASK
) {
710 current_thread_info()->status
&= ~TS_RESTORE_SIGMASK
;
711 sigprocmask(SIG_SETMASK
, ¤t
->saved_sigmask
, NULL
);
716 * notification of userspace execution resumption
717 * - triggered by the TIF_WORK_MASK flags
720 do_notify_resume(struct pt_regs
*regs
, void *unused
, __u32 thread_info_flags
)
722 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
723 /* notify userspace of pending MCEs */
724 if (thread_info_flags
& _TIF_MCE_NOTIFY
)
726 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
728 /* deal with pending signal delivery */
729 if (thread_info_flags
& _TIF_SIGPENDING
)
732 if (thread_info_flags
& _TIF_NOTIFY_RESUME
) {
733 clear_thread_flag(TIF_NOTIFY_RESUME
);
734 tracehook_notify_resume(regs
);
738 clear_thread_flag(TIF_IRET
);
739 #endif /* CONFIG_X86_32 */
742 void signal_fault(struct pt_regs
*regs
, void __user
*frame
, char *where
)
744 struct task_struct
*me
= current
;
746 if (show_unhandled_signals
&& printk_ratelimit()) {
748 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
749 me
->comm
, me
->pid
, where
, frame
,
750 regs
->ip
, regs
->sp
, regs
->orig_ax
);
751 print_vma_addr(" in ", regs
->ip
);
752 printk(KERN_CONT
"\n");
755 force_sig(SIGSEGV
, me
);