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
7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
10 #include <linux/sched.h>
12 #include <linux/smp.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/errno.h>
16 #include <linux/wait.h>
17 #include <linux/unistd.h>
18 #include <linux/stddef.h>
19 #include <linux/personality.h>
20 #include <linux/suspend.h>
21 #include <linux/ptrace.h>
22 #include <linux/elf.h>
23 #include <linux/binfmts.h>
24 #include <asm/processor.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35 * Atomically swap in the new signal mask, and wait for a signal.
38 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
41 spin_lock_irq(¤t->sighand->siglock);
42 current->saved_sigmask = current->blocked;
43 siginitset(¤t->blocked, mask);
45 spin_unlock_irq(¤t->sighand->siglock);
47 current->state = TASK_INTERRUPTIBLE;
49 set_thread_flag(TIF_RESTORE_SIGMASK);
50 return -ERESTARTNOHAND;
54 sys_sigaction(int sig, const struct old_sigaction __user *act,
55 struct old_sigaction __user *oact)
57 struct k_sigaction new_ka, old_ka;
62 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
63 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
64 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
66 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
67 __get_user(mask, &act->sa_mask);
68 siginitset(&new_ka.sa.sa_mask, mask);
71 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
74 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
75 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
76 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
78 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
79 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
86 sys_sigaltstack(unsigned long ebx)
88 /* This is needed to make gcc realize it doesn't own the "struct pt_regs" */
89 struct pt_regs *regs = (struct pt_regs *)&ebx;
90 const stack_t __user *uss = (const stack_t __user *)ebx;
91 stack_t __user *uoss = (stack_t __user *)regs->ecx;
93 return do_sigaltstack(uss, uoss, regs->esp);
98 * Do a signal return; undo the signal stack.
102 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *peax)
104 unsigned int err = 0;
106 /* Always make any pending restarted system calls return -EINTR */
107 current_thread_info()->restart_block.fn = do_no_restart_syscall;
109 #define COPY(x) err |= __get_user(regs->x, &sc->x)
111 #define COPY_SEG(seg) \
112 { unsigned short tmp; \
113 err |= __get_user(tmp, &sc->seg); \
114 regs->x##seg = tmp; }
116 #define COPY_SEG_STRICT(seg) \
117 { unsigned short tmp; \
118 err |= __get_user(tmp, &sc->seg); \
119 regs->x##seg = tmp|3; }
121 #define GET_SEG(seg) \
122 { unsigned short tmp; \
123 err |= __get_user(tmp, &sc->seg); \
124 loadsegment(seg,tmp); }
126 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_RF | \
127 X86_EFLAGS_OF | X86_EFLAGS_DF | \
128 X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \
129 X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF)
147 unsigned int tmpflags;
148 err |= __get_user(tmpflags, &sc->eflags);
149 regs->eflags = (regs->eflags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
150 regs->orig_eax = -1; /* disable syscall checks */
154 struct _fpstate __user * buf;
155 err |= __get_user(buf, &sc->fpstate);
157 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
159 err |= restore_i387(buf);
161 struct task_struct *me = current;
169 err |= __get_user(*peax, &sc->eax);
176 asmlinkage int sys_sigreturn(unsigned long __unused)
178 struct pt_regs *regs = (struct pt_regs *) &__unused;
179 struct sigframe __user *frame = (struct sigframe __user *)(regs->esp - 8);
183 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
185 if (__get_user(set.sig[0], &frame->sc.oldmask)
187 && __copy_from_user(&set.sig[1], &frame->extramask,
188 sizeof(frame->extramask))))
191 sigdelsetmask(&set, ~_BLOCKABLE);
192 spin_lock_irq(¤t->sighand->siglock);
193 current->blocked = set;
195 spin_unlock_irq(¤t->sighand->siglock);
197 if (restore_sigcontext(regs, &frame->sc, &eax))
202 force_sig(SIGSEGV, current);
206 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
208 struct pt_regs *regs = (struct pt_regs *) &__unused;
209 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4);
213 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
215 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
218 sigdelsetmask(&set, ~_BLOCKABLE);
219 spin_lock_irq(¤t->sighand->siglock);
220 current->blocked = set;
222 spin_unlock_irq(¤t->sighand->siglock);
224 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
227 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT)
233 force_sig(SIGSEGV, current);
238 * Set up a signal frame.
242 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
243 struct pt_regs *regs, unsigned long mask)
247 err |= __put_user(regs->xfs, (unsigned int __user *)&sc->fs);
248 savesegment(gs, tmp);
249 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
251 err |= __put_user(regs->xes, (unsigned int __user *)&sc->es);
252 err |= __put_user(regs->xds, (unsigned int __user *)&sc->ds);
253 err |= __put_user(regs->edi, &sc->edi);
254 err |= __put_user(regs->esi, &sc->esi);
255 err |= __put_user(regs->ebp, &sc->ebp);
256 err |= __put_user(regs->esp, &sc->esp);
257 err |= __put_user(regs->ebx, &sc->ebx);
258 err |= __put_user(regs->edx, &sc->edx);
259 err |= __put_user(regs->ecx, &sc->ecx);
260 err |= __put_user(regs->eax, &sc->eax);
261 err |= __put_user(current->thread.trap_no, &sc->trapno);
262 err |= __put_user(current->thread.error_code, &sc->err);
263 err |= __put_user(regs->eip, &sc->eip);
264 err |= __put_user(regs->xcs, (unsigned int __user *)&sc->cs);
265 err |= __put_user(regs->eflags, &sc->eflags);
266 err |= __put_user(regs->esp, &sc->esp_at_signal);
267 err |= __put_user(regs->xss, (unsigned int __user *)&sc->ss);
269 tmp = save_i387(fpstate);
273 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
275 /* non-iBCS2 extensions.. */
276 err |= __put_user(mask, &sc->oldmask);
277 err |= __put_user(current->thread.cr2, &sc->cr2);
283 * Determine which stack to use..
285 static inline void __user *
286 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
290 /* Default to using normal stack */
293 /* This is the X/Open sanctioned signal stack switching. */
294 if (ka->sa.sa_flags & SA_ONSTACK) {
295 if (sas_ss_flags(esp) == 0)
296 esp = current->sas_ss_sp + current->sas_ss_size;
299 /* This is the legacy signal stack switching. */
300 else if ((regs->xss & 0xffff) != __USER_DS &&
301 !(ka->sa.sa_flags & SA_RESTORER) &&
302 ka->sa.sa_restorer) {
303 esp = (unsigned long) ka->sa.sa_restorer;
307 /* Align the stack pointer according to the i386 ABI,
308 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
309 esp = ((esp + 4) & -16ul) - 4;
310 return (void __user *) esp;
313 /* These symbols are defined with the addresses in the vsyscall page.
314 See vsyscall-sigreturn.S. */
315 extern void __user __kernel_sigreturn;
316 extern void __user __kernel_rt_sigreturn;
318 static int setup_frame(int sig, struct k_sigaction *ka,
319 sigset_t *set, struct pt_regs * regs)
321 void __user *restorer;
322 struct sigframe __user *frame;
326 frame = get_sigframe(ka, regs, sizeof(*frame));
328 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
331 usig = current_thread_info()->exec_domain
332 && current_thread_info()->exec_domain->signal_invmap
334 ? current_thread_info()->exec_domain->signal_invmap[sig]
337 err = __put_user(usig, &frame->sig);
341 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
345 if (_NSIG_WORDS > 1) {
346 err = __copy_to_user(&frame->extramask, &set->sig[1],
347 sizeof(frame->extramask));
352 if (current->binfmt->hasvdso)
353 restorer = (void *)VDSO_SYM(&__kernel_sigreturn);
355 restorer = (void *)&frame->retcode;
356 if (ka->sa.sa_flags & SA_RESTORER)
357 restorer = ka->sa.sa_restorer;
359 /* Set up to return from userspace. */
360 err |= __put_user(restorer, &frame->pretcode);
363 * This is popl %eax ; movl $,%eax ; int $0x80
365 * WE DO NOT USE IT ANY MORE! It's only left here for historical
366 * reasons and because gdb uses it as a signature to notice
367 * signal handler stack frames.
369 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
370 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
371 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
376 /* Set up registers for signal handler */
377 regs->esp = (unsigned long) frame;
378 regs->eip = (unsigned long) ka->sa.sa_handler;
379 regs->eax = (unsigned long) sig;
380 regs->edx = (unsigned long) 0;
381 regs->ecx = (unsigned long) 0;
384 regs->xds = __USER_DS;
385 regs->xes = __USER_DS;
386 regs->xss = __USER_DS;
387 regs->xcs = __USER_CS;
390 * Clear TF when entering the signal handler, but
391 * notify any tracer that was single-stepping it.
392 * The tracer may want to single-step inside the
395 regs->eflags &= ~TF_MASK;
396 if (test_thread_flag(TIF_SINGLESTEP))
397 ptrace_notify(SIGTRAP);
400 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
401 current->comm, current->pid, frame, regs->eip, frame->pretcode);
407 force_sigsegv(sig, current);
411 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
412 sigset_t *set, struct pt_regs * regs)
414 void __user *restorer;
415 struct rt_sigframe __user *frame;
419 frame = get_sigframe(ka, regs, sizeof(*frame));
421 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
424 usig = current_thread_info()->exec_domain
425 && current_thread_info()->exec_domain->signal_invmap
427 ? current_thread_info()->exec_domain->signal_invmap[sig]
430 err |= __put_user(usig, &frame->sig);
431 err |= __put_user(&frame->info, &frame->pinfo);
432 err |= __put_user(&frame->uc, &frame->puc);
433 err |= copy_siginfo_to_user(&frame->info, info);
437 /* Create the ucontext. */
438 err |= __put_user(0, &frame->uc.uc_flags);
439 err |= __put_user(0, &frame->uc.uc_link);
440 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
441 err |= __put_user(sas_ss_flags(regs->esp),
442 &frame->uc.uc_stack.ss_flags);
443 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
444 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
446 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
450 /* Set up to return from userspace. */
451 restorer = (void *)VDSO_SYM(&__kernel_rt_sigreturn);
452 if (ka->sa.sa_flags & SA_RESTORER)
453 restorer = ka->sa.sa_restorer;
454 err |= __put_user(restorer, &frame->pretcode);
457 * This is movl $,%eax ; int $0x80
459 * WE DO NOT USE IT ANY MORE! It's only left here for historical
460 * reasons and because gdb uses it as a signature to notice
461 * signal handler stack frames.
463 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
464 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
465 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
470 /* Set up registers for signal handler */
471 regs->esp = (unsigned long) frame;
472 regs->eip = (unsigned long) ka->sa.sa_handler;
473 regs->eax = (unsigned long) usig;
474 regs->edx = (unsigned long) &frame->info;
475 regs->ecx = (unsigned long) &frame->uc;
478 regs->xds = __USER_DS;
479 regs->xes = __USER_DS;
480 regs->xss = __USER_DS;
481 regs->xcs = __USER_CS;
484 * Clear TF when entering the signal handler, but
485 * notify any tracer that was single-stepping it.
486 * The tracer may want to single-step inside the
489 regs->eflags &= ~TF_MASK;
490 if (test_thread_flag(TIF_SINGLESTEP))
491 ptrace_notify(SIGTRAP);
494 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
495 current->comm, current->pid, frame, regs->eip, frame->pretcode);
501 force_sigsegv(sig, current);
506 * OK, we're invoking a handler
510 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
511 sigset_t *oldset, struct pt_regs * regs)
515 /* Are we from a system call? */
516 if (regs->orig_eax >= 0) {
517 /* If so, check system call restarting.. */
519 case -ERESTART_RESTARTBLOCK:
520 case -ERESTARTNOHAND:
525 if (!(ka->sa.sa_flags & SA_RESTART)) {
530 case -ERESTARTNOINTR:
531 regs->eax = regs->orig_eax;
537 * If TF is set due to a debugger (PT_DTRACE), clear the TF flag so
538 * that register information in the sigcontext is correct.
540 if (unlikely(regs->eflags & TF_MASK)
541 && likely(current->ptrace & PT_DTRACE)) {
542 current->ptrace &= ~PT_DTRACE;
543 regs->eflags &= ~TF_MASK;
546 /* Set up the stack frame */
547 if (ka->sa.sa_flags & SA_SIGINFO)
548 ret = setup_rt_frame(sig, ka, info, oldset, regs);
550 ret = setup_frame(sig, ka, oldset, regs);
553 spin_lock_irq(¤t->sighand->siglock);
554 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
555 if (!(ka->sa.sa_flags & SA_NODEFER))
556 sigaddset(¤t->blocked,sig);
558 spin_unlock_irq(¤t->sighand->siglock);
565 * Note that 'init' is a special process: it doesn't get signals it doesn't
566 * want to handle. Thus you cannot kill init even with a SIGKILL even by
569 static void fastcall do_signal(struct pt_regs *regs)
573 struct k_sigaction ka;
577 * We want the common case to go fast, which
578 * is why we may in certain cases get here from
579 * kernel mode. Just return without doing anything
580 * if so. vm86 regs switched out by assembly code
581 * before reaching here, so testing against kernel
584 if (!user_mode(regs))
587 if (test_thread_flag(TIF_RESTORE_SIGMASK))
588 oldset = ¤t->saved_sigmask;
590 oldset = ¤t->blocked;
592 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
594 /* Reenable any watchpoints before delivering the
595 * signal to user space. The processor register will
596 * have been cleared if the watchpoint triggered
599 if (unlikely(current->thread.debugreg[7]))
600 set_debugreg(current->thread.debugreg[7], 7);
602 /* Whee! Actually deliver the signal. */
603 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
604 /* a signal was successfully delivered; the saved
605 * sigmask will have been stored in the signal frame,
606 * and will be restored by sigreturn, so we can simply
607 * clear the TIF_RESTORE_SIGMASK flag */
608 if (test_thread_flag(TIF_RESTORE_SIGMASK))
609 clear_thread_flag(TIF_RESTORE_SIGMASK);
615 /* Did we come from a system call? */
616 if (regs->orig_eax >= 0) {
617 /* Restart the system call - no handlers present */
619 case -ERESTARTNOHAND:
621 case -ERESTARTNOINTR:
622 regs->eax = regs->orig_eax;
626 case -ERESTART_RESTARTBLOCK:
627 regs->eax = __NR_restart_syscall;
633 /* if there's no signal to deliver, we just put the saved sigmask
635 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
636 clear_thread_flag(TIF_RESTORE_SIGMASK);
637 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
642 * notification of userspace execution resumption
643 * - triggered by the TIF_WORK_MASK flags
645 __attribute__((regparm(3)))
646 void do_notify_resume(struct pt_regs *regs, void *_unused,
647 __u32 thread_info_flags)
649 /* Pending single-step? */
650 if (thread_info_flags & _TIF_SINGLESTEP) {
651 regs->eflags |= TF_MASK;
652 clear_thread_flag(TIF_SINGLESTEP);
655 /* deal with pending signal delivery */
656 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
659 clear_thread_flag(TIF_IRET);