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/smp_lock.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/unistd.h>
19 #include <linux/stddef.h>
20 #include <linux/personality.h>
21 #include <linux/suspend.h>
22 #include <linux/elf.h>
23 #include <asm/processor.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
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->sighand->siglock);
44 saveset = current->blocked;
45 siginitset(¤t->blocked, mask);
47 spin_unlock_irq(¤t->sighand->siglock);
51 current->state = TASK_INTERRUPTIBLE;
53 if (do_signal(regs, &saveset))
59 sys_rt_sigsuspend(struct pt_regs regs)
61 sigset_t saveset, newset;
63 /* XXX: Don't preclude handling different sized sigset_t's. */
64 if (regs.ecx != sizeof(sigset_t))
67 if (copy_from_user(&newset, (sigset_t __user *)regs.ebx, sizeof(newset)))
69 sigdelsetmask(&newset, ~_BLOCKABLE);
71 spin_lock_irq(¤t->sighand->siglock);
72 saveset = current->blocked;
73 current->blocked = newset;
75 spin_unlock_irq(¤t->sighand->siglock);
79 current->state = TASK_INTERRUPTIBLE;
81 if (do_signal(®s, &saveset))
87 sys_sigaction(int sig, const struct old_sigaction __user *act,
88 struct old_sigaction __user *oact)
90 struct k_sigaction new_ka, old_ka;
95 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
96 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
97 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
99 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
100 __get_user(mask, &act->sa_mask);
101 siginitset(&new_ka.sa.sa_mask, mask);
104 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
107 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
108 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
109 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
111 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
112 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
119 sys_sigaltstack(unsigned long ebx)
121 /* This is needed to make gcc realize it doesn't own the "struct pt_regs" */
122 struct pt_regs *regs = (struct pt_regs *)&ebx;
123 const stack_t __user *uss = (const stack_t __user *)ebx;
124 stack_t __user *uoss = (stack_t __user *)regs->ecx;
126 return do_sigaltstack(uss, uoss, regs->esp);
131 * Do a signal return; undo the signal stack.
135 restore_sigcontext(struct pt_regs *regs,
136 struct sigcontext __user *__sc, int *peax)
138 struct sigcontext scratch; /* 88 bytes of scratch area */
140 /* Always make any pending restarted system calls return -EINTR */
141 current_thread_info()->restart_block.fn = do_no_restart_syscall;
143 if (copy_from_user(&scratch, __sc, sizeof(scratch)))
146 #define COPY(x) regs->x = scratch.x
148 #define COPY_SEG(seg) \
149 { unsigned short tmp = scratch.seg; \
150 regs->x##seg = tmp; }
152 #define COPY_SEG_STRICT(seg) \
153 { unsigned short tmp = scratch.seg; \
154 regs->x##seg = tmp|3; }
156 #define GET_SEG(seg) \
157 { unsigned short tmp = scratch.seg; \
158 loadsegment(seg,tmp); }
160 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | X86_EFLAGS_DF | \
161 X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \
162 X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF)
180 unsigned int tmpflags = scratch.eflags;
181 regs->eflags = (regs->eflags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
182 regs->orig_eax = -1; /* disable syscall checks */
186 struct _fpstate * buf = scratch.fpstate;
188 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
190 if (restore_i387(buf))
199 asmlinkage int sys_sigreturn(unsigned long __unused)
201 struct pt_regs *regs = (struct pt_regs *) &__unused;
202 struct sigframe __user *frame = (struct sigframe __user *)(regs->esp - 8);
206 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
208 if (__get_user(set.sig[0], &frame->sc.oldmask)
210 && __copy_from_user(&set.sig[1], &frame->extramask,
211 sizeof(frame->extramask))))
214 sigdelsetmask(&set, ~_BLOCKABLE);
215 spin_lock_irq(¤t->sighand->siglock);
216 current->blocked = set;
218 spin_unlock_irq(¤t->sighand->siglock);
220 if (restore_sigcontext(regs, &frame->sc, &eax))
225 force_sig(SIGSEGV, current);
229 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
231 struct pt_regs *regs = (struct pt_regs *) &__unused;
232 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4);
236 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
238 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
241 sigdelsetmask(&set, ~_BLOCKABLE);
242 spin_lock_irq(¤t->sighand->siglock);
243 current->blocked = set;
245 spin_unlock_irq(¤t->sighand->siglock);
247 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
250 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT)
256 force_sig(SIGSEGV, current);
261 * Set up a signal frame.
265 setup_sigcontext(struct sigcontext __user *__sc, struct _fpstate __user *fpstate,
266 struct pt_regs *regs, unsigned long mask)
268 struct sigcontext sc; /* 88 bytes of scratch area */
272 __asm__("movl %%gs,%0" : "=r"(tmp): "0"(tmp));
273 *(unsigned int *)&sc.gs = tmp;
274 __asm__("movl %%fs,%0" : "=r"(tmp): "0"(tmp));
275 *(unsigned int *)&sc.fs = tmp;
276 *(unsigned int *)&sc.es = regs->xes;
277 *(unsigned int *)&sc.ds = regs->xds;
286 sc.trapno = current->thread.trap_no;
287 sc.err = current->thread.error_code;
289 *(unsigned int *)&sc.cs = regs->xcs;
290 sc.eflags = regs->eflags;
291 sc.esp_at_signal = regs->esp;
292 *(unsigned int *)&sc.ss = regs->xss;
294 tmp = save_i387(fpstate);
297 sc.fpstate = tmp ? fpstate : NULL;
299 /* non-iBCS2 extensions.. */
301 sc.cr2 = current->thread.cr2;
303 if (copy_to_user(__sc, &sc, sizeof(sc)))
309 * Determine which stack to use..
311 static inline void __user *
312 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
316 /* Default to using normal stack */
319 /* This is the X/Open sanctioned signal stack switching. */
320 if (ka->sa.sa_flags & SA_ONSTACK) {
321 if (sas_ss_flags(esp) == 0)
322 esp = current->sas_ss_sp + current->sas_ss_size;
325 /* This is the legacy signal stack switching. */
326 else if ((regs->xss & 0xffff) != __USER_DS &&
327 !(ka->sa.sa_flags & SA_RESTORER) &&
328 ka->sa.sa_restorer) {
329 esp = (unsigned long) ka->sa.sa_restorer;
332 return (void __user *)((esp - frame_size) & -8ul);
335 /* These symbols are defined with the addresses in the vsyscall page.
336 See vsyscall-sigreturn.S. */
337 extern void __user __kernel_sigreturn;
338 extern void __user __kernel_rt_sigreturn;
339 extern SYSENTER_RETURN;
341 static void setup_frame(int sig, struct k_sigaction *ka,
342 sigset_t *set, struct pt_regs * regs)
344 void __user *restorer;
345 struct sigframe __user *frame;
348 frame = get_sigframe(ka, regs, sizeof(*frame));
350 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
353 err |= __put_user((current_thread_info()->exec_domain
354 && current_thread_info()->exec_domain->signal_invmap
356 ? current_thread_info()->exec_domain->signal_invmap[sig]
362 err |= setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
366 if (_NSIG_WORDS > 1) {
367 err |= __copy_to_user(&frame->extramask, &set->sig[1],
368 sizeof(frame->extramask));
373 restorer = current->mm->context.vdso + (long)&__kernel_sigreturn;
374 if (ka->sa.sa_flags & SA_RESTORER)
375 restorer = ka->sa.sa_restorer;
377 /* Set up to return from userspace. */
378 err |= __put_user(restorer, &frame->pretcode);
381 * This is popl %eax ; movl $,%eax ; int $0x80
383 * WE DO NOT USE IT ANY MORE! It's only left here for historical
384 * reasons and because gdb uses it as a signature to notice
385 * signal handler stack frames.
387 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
388 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
389 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
394 /* Set up registers for signal handler */
395 regs->esp = (unsigned long) frame;
396 regs->eip = (unsigned long) ka->sa.sa_handler;
399 regs->xds = __USER_DS;
400 regs->xes = __USER_DS;
401 regs->xss = __USER_DS;
402 regs->xcs = __USER_CS;
403 regs->eflags &= ~TF_MASK;
406 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
407 current->comm, current->pid, frame, regs->eip, frame->pretcode);
414 ka->sa.sa_handler = SIG_DFL;
415 force_sig(SIGSEGV, current);
418 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
419 sigset_t *set, struct pt_regs * regs)
421 void __user *restorer;
422 struct rt_sigframe __user *frame;
425 frame = get_sigframe(ka, regs, sizeof(*frame));
427 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
430 err |= __put_user((current_thread_info()->exec_domain
431 && current_thread_info()->exec_domain->signal_invmap
433 ? current_thread_info()->exec_domain->signal_invmap[sig]
436 err |= __put_user(&frame->info, &frame->pinfo);
437 err |= __put_user(&frame->uc, &frame->puc);
438 err |= copy_siginfo_to_user(&frame->info, info);
442 /* Create the ucontext. */
443 err |= __put_user(0, &frame->uc.uc_flags);
444 err |= __put_user(0, &frame->uc.uc_link);
445 err |= __put_user(current->sas_ss_sp, (unsigned long *)&frame->uc.uc_stack.ss_sp);
446 err |= __put_user(sas_ss_flags(regs->esp),
447 &frame->uc.uc_stack.ss_flags);
448 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
449 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
451 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
455 /* Set up to return from userspace. */
456 restorer = current->mm->context.vdso + (long)&__kernel_rt_sigreturn;
457 if (ka->sa.sa_flags & SA_RESTORER)
458 restorer = ka->sa.sa_restorer;
460 err |= __put_user(restorer, &frame->pretcode);
463 * This is movl $,%eax ; int $0x80
465 * WE DO NOT USE IT ANY MORE! It's only left here for historical
466 * reasons and because gdb uses it as a signature to notice
467 * signal handler stack frames.
469 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
470 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
471 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
476 /* Set up registers for signal handler */
477 regs->esp = (unsigned long) frame;
478 regs->eip = (unsigned long) ka->sa.sa_handler;
481 regs->xds = __USER_DS;
482 regs->xes = __USER_DS;
483 regs->xss = __USER_DS;
484 regs->xcs = __USER_CS;
485 regs->eflags &= ~TF_MASK;
488 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
489 current->comm, current->pid, frame, regs->eip, frame->pretcode);
496 ka->sa.sa_handler = SIG_DFL;
497 force_sig(SIGSEGV, current);
501 * OK, we're invoking a handler
505 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
506 struct pt_regs * regs)
508 struct k_sigaction *ka = ¤t->sighand->action[sig-1];
510 /* Are we from a system call? */
511 if (regs->orig_eax >= 0) {
512 /* If so, check system call restarting.. */
514 case -ERESTART_RESTARTBLOCK:
515 case -ERESTARTNOHAND:
520 if (!(ka->sa.sa_flags & SA_RESTART)) {
525 case -ERESTARTNOINTR:
526 regs->eax = regs->orig_eax;
531 /* Set up the stack frame */
532 if (ka->sa.sa_flags & SA_SIGINFO)
533 setup_rt_frame(sig, ka, info, oldset, regs);
535 setup_frame(sig, ka, oldset, regs);
537 if (ka->sa.sa_flags & SA_ONESHOT)
538 ka->sa.sa_handler = SIG_DFL;
540 if (!(ka->sa.sa_flags & SA_NODEFER)) {
541 spin_lock_irq(¤t->sighand->siglock);
542 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
543 sigaddset(¤t->blocked,sig);
545 spin_unlock_irq(¤t->sighand->siglock);
550 * Note that 'init' is a special process: it doesn't get signals it doesn't
551 * want to handle. Thus you cannot kill init even with a SIGKILL even by
554 int fastcall do_signal(struct pt_regs *regs, sigset_t *oldset)
560 * We want the common case to go fast, which
561 * is why we may in certain cases get here from
562 * kernel mode. Just return without doing anything
565 if ((regs->xcs & 3) != 3)
568 if (current->flags & PF_FREEZE) {
574 oldset = ¤t->blocked;
576 signr = get_signal_to_deliver(&info, regs, NULL);
578 /* Reenable any watchpoints before delivering the
579 * signal to user space. The processor register will
580 * have been cleared if the watchpoint triggered
583 __asm__("movl %0,%%db7" : : "r" (current->thread.debugreg[7]));
585 /* Whee! Actually deliver the signal. */
586 handle_signal(signr, &info, oldset, regs);
591 /* Did we come from a system call? */
592 if (regs->orig_eax >= 0) {
593 /* Restart the system call - no handlers present */
594 if (regs->eax == -ERESTARTNOHAND ||
595 regs->eax == -ERESTARTSYS ||
596 regs->eax == -ERESTARTNOINTR) {
597 regs->eax = regs->orig_eax;
600 if (regs->eax == -ERESTART_RESTARTBLOCK){
601 regs->eax = __NR_restart_syscall;
609 * notification of userspace execution resumption
610 * - triggered by current->work.notify_resume
612 __attribute__((regparm(3)))
613 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
614 __u32 thread_info_flags)
616 /* Pending single-step? */
617 if (thread_info_flags & _TIF_SINGLESTEP) {
618 regs->eflags |= TF_MASK;
619 clear_thread_flag(TIF_SINGLESTEP);
621 /* deal with pending signal delivery */
622 if (thread_info_flags & _TIF_SIGPENDING)
623 do_signal(regs,oldset);
625 clear_thread_flag(TIF_IRET);