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
8 #include <linux/sched.h>
10 #include <linux/smp.h>
11 #include <linux/kernel.h>
12 #include <linux/signal.h>
13 #include <linux/errno.h>
14 #include <linux/wait.h>
15 #include <linux/ptrace.h>
16 #include <linux/tracehook.h>
17 #include <linux/unistd.h>
18 #include <linux/stddef.h>
19 #include <linux/personality.h>
20 #include <linux/uaccess.h>
22 #include <asm/processor.h>
23 #include <asm/ucontext.h>
28 #include <asm/proto.h>
29 #include <asm/ia32_unistd.h>
31 #endif /* CONFIG_X86_64 */
33 #include <asm/syscall.h>
34 #include <asm/syscalls.h>
38 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
41 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
42 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
46 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
48 # define FIX_EFLAGS __FIX_EFLAGS
55 } __attribute__((packed)) retcode = {
56 0xb858, /* popl %eax; movl $..., %eax */
58 0x80cd, /* int $0x80 */
66 } __attribute__((packed)) rt_retcode = {
67 0xb8, /* movl $..., %eax */
69 0x80cd, /* int $0x80 */
74 * Atomically swap in the new signal mask, and wait for a signal.
77 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
80 spin_lock_irq(¤t->sighand->siglock);
81 current->saved_sigmask = current->blocked;
82 siginitset(¤t->blocked, mask);
84 spin_unlock_irq(¤t->sighand->siglock);
86 current->state = TASK_INTERRUPTIBLE;
88 set_restore_sigmask();
90 return -ERESTARTNOHAND;
94 sys_sigaction(int sig, const struct old_sigaction __user *act,
95 struct old_sigaction __user *oact)
97 struct k_sigaction new_ka, old_ka;
103 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
104 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
105 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
108 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
109 __get_user(mask, &act->sa_mask);
110 siginitset(&new_ka.sa.sa_mask, mask);
113 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
116 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
117 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
118 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
121 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
122 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
129 asmlinkage int sys_sigaltstack(unsigned long bx)
132 * This is needed to make gcc realize it doesn't own the
135 struct pt_regs *regs = (struct pt_regs *)&bx;
136 const stack_t __user *uss = (const stack_t __user *)bx;
137 stack_t __user *uoss = (stack_t __user *)regs->cx;
139 return do_sigaltstack(uss, uoss, regs->sp);
141 #else /* !CONFIG_X86_32 */
143 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
144 struct pt_regs *regs)
146 return do_sigaltstack(uss, uoss, regs->sp);
148 #endif /* CONFIG_X86_32 */
151 err |= __get_user(regs->x, &sc->x); \
154 #define COPY_SEG(seg) { \
155 unsigned short tmp; \
156 err |= __get_user(tmp, &sc->seg); \
160 #define COPY_SEG_CPL3(seg) { \
161 unsigned short tmp; \
162 err |= __get_user(tmp, &sc->seg); \
163 regs->seg = tmp | 3; \
166 #define GET_SEG(seg) { \
167 unsigned short tmp; \
168 err |= __get_user(tmp, &sc->seg); \
169 loadsegment(seg, tmp); \
173 * Do a signal return; undo the signal stack.
176 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
180 unsigned int tmpflags;
181 unsigned int err = 0;
183 /* Always make any pending restarted system calls return -EINTR */
184 current_thread_info()->restart_block.fn = do_no_restart_syscall;
191 #endif /* CONFIG_X86_32 */
193 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
194 COPY(dx); COPY(cx); COPY(ip);
205 #endif /* CONFIG_X86_64 */
210 #else /* !CONFIG_X86_32 */
211 /* Kernel saves and restores only the CS segment register on signals,
212 * which is the bare minimum needed to allow mixed 32/64-bit code.
213 * App's signal handler can save/restore other segments if needed. */
215 #endif /* CONFIG_X86_32 */
217 err |= __get_user(tmpflags, &sc->flags);
218 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
219 regs->orig_ax = -1; /* disable syscall checks */
221 err |= __get_user(buf, &sc->fpstate);
222 err |= restore_i387_xstate(buf);
224 err |= __get_user(*pax, &sc->ax);
228 asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
230 struct sigframe __user *frame;
231 struct pt_regs *regs;
235 regs = (struct pt_regs *) &__unused;
236 frame = (struct sigframe __user *)(regs->sp - 8);
238 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
240 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
241 && __copy_from_user(&set.sig[1], &frame->extramask,
242 sizeof(frame->extramask))))
245 sigdelsetmask(&set, ~_BLOCKABLE);
246 spin_lock_irq(¤t->sighand->siglock);
247 current->blocked = set;
249 spin_unlock_irq(¤t->sighand->siglock);
251 if (restore_sigcontext(regs, &frame->sc, &ax))
256 if (show_unhandled_signals && printk_ratelimit()) {
257 printk("%s%s[%d] bad frame in sigreturn frame:"
258 "%p ip:%lx sp:%lx oeax:%lx",
259 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
260 current->comm, task_pid_nr(current), frame, regs->ip,
261 regs->sp, regs->orig_ax);
262 print_vma_addr(" in ", regs->ip);
263 printk(KERN_CONT "\n");
266 force_sig(SIGSEGV, current);
271 static long do_rt_sigreturn(struct pt_regs *regs)
273 struct rt_sigframe __user *frame;
277 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
278 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
280 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
283 sigdelsetmask(&set, ~_BLOCKABLE);
284 spin_lock_irq(¤t->sighand->siglock);
285 current->blocked = set;
287 spin_unlock_irq(¤t->sighand->siglock);
289 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
292 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
298 signal_fault(regs, frame, "rt_sigreturn");
302 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
304 struct pt_regs *regs = (struct pt_regs *)&__unused;
306 return do_rt_sigreturn(regs);
310 * Set up a signal frame.
313 setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
314 struct pt_regs *regs, unsigned long mask)
322 savesegment(gs, tmp);
323 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
325 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
326 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
327 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
328 #endif /* CONFIG_X86_32 */
330 err |= __put_user(regs->di, &sc->di);
331 err |= __put_user(regs->si, &sc->si);
332 err |= __put_user(regs->bp, &sc->bp);
333 err |= __put_user(regs->sp, &sc->sp);
334 err |= __put_user(regs->bx, &sc->bx);
335 err |= __put_user(regs->dx, &sc->dx);
336 err |= __put_user(regs->cx, &sc->cx);
337 err |= __put_user(regs->ax, &sc->ax);
339 err |= __put_user(regs->r8, &sc->r8);
340 err |= __put_user(regs->r9, &sc->r9);
341 err |= __put_user(regs->r10, &sc->r10);
342 err |= __put_user(regs->r11, &sc->r11);
343 err |= __put_user(regs->r12, &sc->r12);
344 err |= __put_user(regs->r13, &sc->r13);
345 err |= __put_user(regs->r14, &sc->r14);
346 err |= __put_user(regs->r15, &sc->r15);
347 #endif /* CONFIG_X86_64 */
349 err |= __put_user(current->thread.trap_no, &sc->trapno);
350 err |= __put_user(current->thread.error_code, &sc->err);
351 err |= __put_user(regs->ip, &sc->ip);
353 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
354 err |= __put_user(regs->flags, &sc->flags);
355 err |= __put_user(regs->sp, &sc->sp_at_signal);
356 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
357 #else /* !CONFIG_X86_32 */
358 err |= __put_user(regs->flags, &sc->flags);
359 err |= __put_user(regs->cs, &sc->cs);
360 err |= __put_user(0, &sc->gs);
361 err |= __put_user(0, &sc->fs);
362 #endif /* CONFIG_X86_32 */
364 err |= __put_user(fpstate, &sc->fpstate);
366 /* non-iBCS2 extensions.. */
367 err |= __put_user(mask, &sc->oldmask);
368 err |= __put_user(current->thread.cr2, &sc->cr2);
374 * Determine which stack to use..
376 static inline void __user *
377 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
382 /* Default to using normal stack */
386 * If we are on the alternate signal stack and would overflow it, don't.
387 * Return an always-bogus address instead so we will die with SIGSEGV.
389 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
390 return (void __user *) -1L;
392 /* This is the X/Open sanctioned signal stack switching. */
393 if (ka->sa.sa_flags & SA_ONSTACK) {
394 if (sas_ss_flags(sp) == 0)
395 sp = current->sas_ss_sp + current->sas_ss_size;
397 /* This is the legacy signal stack switching. */
398 if ((regs->ss & 0xffff) != __USER_DS &&
399 !(ka->sa.sa_flags & SA_RESTORER) &&
401 sp = (unsigned long) ka->sa.sa_restorer;
405 sp = sp - sig_xstate_size;
406 *fpstate = (struct _fpstate *) sp;
407 if (save_i387_xstate(*fpstate) < 0)
408 return (void __user *)-1L;
413 * Align the stack pointer according to the i386 ABI,
414 * i.e. so that on function entry ((sp + 4) & 15) == 0.
416 sp = ((sp + 4) & -16ul) - 4;
418 return (void __user *) sp;
422 __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
423 struct pt_regs *regs)
425 struct sigframe __user *frame;
426 void __user *restorer;
428 void __user *fpstate = NULL;
430 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
432 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
435 if (__put_user(sig, &frame->sig))
438 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
441 if (_NSIG_WORDS > 1) {
442 if (__copy_to_user(&frame->extramask, &set->sig[1],
443 sizeof(frame->extramask)))
447 if (current->mm->context.vdso)
448 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
450 restorer = &frame->retcode;
451 if (ka->sa.sa_flags & SA_RESTORER)
452 restorer = ka->sa.sa_restorer;
454 /* Set up to return from userspace. */
455 err |= __put_user(restorer, &frame->pretcode);
458 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
460 * WE DO NOT USE IT ANY MORE! It's only left here for historical
461 * reasons and because gdb uses it as a signature to notice
462 * signal handler stack frames.
464 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
469 /* Set up registers for signal handler */
470 regs->sp = (unsigned long)frame;
471 regs->ip = (unsigned long)ka->sa.sa_handler;
472 regs->ax = (unsigned long)sig;
476 regs->ds = __USER_DS;
477 regs->es = __USER_DS;
478 regs->ss = __USER_DS;
479 regs->cs = __USER_CS;
484 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
485 sigset_t *set, struct pt_regs *regs)
487 struct rt_sigframe __user *frame;
488 void __user *restorer;
490 void __user *fpstate = NULL;
492 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
494 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
497 err |= __put_user(sig, &frame->sig);
498 err |= __put_user(&frame->info, &frame->pinfo);
499 err |= __put_user(&frame->uc, &frame->puc);
500 err |= copy_siginfo_to_user(&frame->info, info);
504 /* Create the ucontext. */
506 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
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->sp),
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, fpstate,
516 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
520 /* Set up to return from userspace. */
521 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
522 if (ka->sa.sa_flags & SA_RESTORER)
523 restorer = ka->sa.sa_restorer;
524 err |= __put_user(restorer, &frame->pretcode);
527 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
529 * WE DO NOT USE IT ANY MORE! It's only left here for historical
530 * reasons and because gdb uses it as a signature to notice
531 * signal handler stack frames.
533 err |= __put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
538 /* Set up registers for signal handler */
539 regs->sp = (unsigned long)frame;
540 regs->ip = (unsigned long)ka->sa.sa_handler;
541 regs->ax = (unsigned long)sig;
542 regs->dx = (unsigned long)&frame->info;
543 regs->cx = (unsigned long)&frame->uc;
545 regs->ds = __USER_DS;
546 regs->es = __USER_DS;
547 regs->ss = __USER_DS;
548 regs->cs = __USER_CS;
554 * OK, we're invoking a handler:
556 static int signr_convert(int sig)
559 struct thread_info *info = current_thread_info();
561 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
562 return info->exec_domain->signal_invmap[sig];
563 #endif /* CONFIG_X86_32 */
570 #define ia32_setup_frame __setup_frame
571 #define ia32_setup_rt_frame __setup_rt_frame
573 #else /* !CONFIG_X86_32 */
575 #ifdef CONFIG_IA32_EMULATION
576 #define is_ia32 test_thread_flag(TIF_IA32)
577 #else /* !CONFIG_IA32_EMULATION */
579 #endif /* CONFIG_IA32_EMULATION */
581 #endif /* CONFIG_X86_32 */
584 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
585 sigset_t *set, struct pt_regs *regs)
587 int usig = signr_convert(sig);
590 /* Set up the stack frame */
592 if (ka->sa.sa_flags & SA_SIGINFO)
593 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
595 ret = ia32_setup_frame(usig, ka, set, regs);
597 ret = __setup_rt_frame(sig, ka, info, set, regs);
600 force_sigsegv(sig, current);
608 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
609 sigset_t *oldset, struct pt_regs *regs)
613 /* Are we from a system call? */
614 if (syscall_get_nr(current, regs) >= 0) {
615 /* If so, check system call restarting.. */
616 switch (syscall_get_error(current, regs)) {
617 case -ERESTART_RESTARTBLOCK:
618 case -ERESTARTNOHAND:
623 if (!(ka->sa.sa_flags & SA_RESTART)) {
628 case -ERESTARTNOINTR:
629 regs->ax = regs->orig_ax;
636 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
637 * flag so that register information in the sigcontext is correct.
639 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
640 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
641 regs->flags &= ~X86_EFLAGS_TF;
643 ret = setup_rt_frame(sig, ka, info, oldset, regs);
650 * This has nothing to do with segment registers,
651 * despite the name. This magic affects uaccess.h
652 * macros' behavior. Reset it to the normal setting.
658 * Clear the direction flag as per the ABI for function entry.
660 regs->flags &= ~X86_EFLAGS_DF;
663 * Clear TF when entering the signal handler, but
664 * notify any tracer that was single-stepping it.
665 * The tracer may want to single-step inside the
668 regs->flags &= ~X86_EFLAGS_TF;
670 spin_lock_irq(¤t->sighand->siglock);
671 sigorsets(¤t->blocked, ¤t->blocked, &ka->sa.sa_mask);
672 if (!(ka->sa.sa_flags & SA_NODEFER))
673 sigaddset(¤t->blocked, sig);
675 spin_unlock_irq(¤t->sighand->siglock);
677 tracehook_signal_handler(sig, info, ka, regs,
678 test_thread_flag(TIF_SINGLESTEP));
684 #define NR_restart_syscall __NR_restart_syscall
685 #else /* !CONFIG_X86_32 */
686 #define NR_restart_syscall \
687 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
688 #endif /* CONFIG_X86_32 */
691 * Note that 'init' is a special process: it doesn't get signals it doesn't
692 * want to handle. Thus you cannot kill init even with a SIGKILL even by
695 static void do_signal(struct pt_regs *regs)
697 struct k_sigaction ka;
703 * We want the common case to go fast, which is why we may in certain
704 * cases get here from kernel mode. Just return without doing anything
706 * X86_32: vm86 regs switched out by assembly code before reaching
707 * here, so testing against kernel CS suffices.
709 if (!user_mode(regs))
712 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
713 oldset = ¤t->saved_sigmask;
715 oldset = ¤t->blocked;
717 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
720 * Re-enable any watchpoints before delivering the
721 * signal to user space. The processor register will
722 * have been cleared if the watchpoint triggered
725 if (current->thread.debugreg7)
726 set_debugreg(current->thread.debugreg7, 7);
728 /* Whee! Actually deliver the signal. */
729 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
731 * A signal was successfully delivered; the saved
732 * sigmask will have been stored in the signal frame,
733 * and will be restored by sigreturn, so we can simply
734 * clear the TS_RESTORE_SIGMASK flag.
736 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
741 /* Did we come from a system call? */
742 if (syscall_get_nr(current, regs) >= 0) {
743 /* Restart the system call - no handlers present */
744 switch (syscall_get_error(current, regs)) {
745 case -ERESTARTNOHAND:
747 case -ERESTARTNOINTR:
748 regs->ax = regs->orig_ax;
752 case -ERESTART_RESTARTBLOCK:
753 regs->ax = NR_restart_syscall;
760 * If there's no signal to deliver, we just put the saved sigmask
763 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
764 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
765 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
770 * notification of userspace execution resumption
771 * - triggered by the TIF_WORK_MASK flags
774 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
776 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
777 /* notify userspace of pending MCEs */
778 if (thread_info_flags & _TIF_MCE_NOTIFY)
780 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
782 /* deal with pending signal delivery */
783 if (thread_info_flags & _TIF_SIGPENDING)
786 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
787 clear_thread_flag(TIF_NOTIFY_RESUME);
788 tracehook_notify_resume(regs);
792 clear_thread_flag(TIF_IRET);
793 #endif /* CONFIG_X86_32 */
796 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
798 struct task_struct *me = current;
800 if (show_unhandled_signals && printk_ratelimit()) {
802 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
803 me->comm, me->pid, where, frame,
804 regs->ip, regs->sp, regs->orig_ax);
805 print_vma_addr(" in ", regs->ip);
806 printk(KERN_CONT "\n");
809 force_sig(SIGSEGV, me);