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;
340 static void setup_frame(int sig, struct k_sigaction *ka,
341 sigset_t *set, struct pt_regs * regs)
343 void __user *restorer;
344 struct sigframe __user *frame;
347 frame = get_sigframe(ka, regs, sizeof(*frame));
349 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
352 err |= __put_user((current_thread_info()->exec_domain
353 && current_thread_info()->exec_domain->signal_invmap
355 ? current_thread_info()->exec_domain->signal_invmap[sig]
361 err |= setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
365 if (_NSIG_WORDS > 1) {
366 err |= __copy_to_user(&frame->extramask, &set->sig[1],
367 sizeof(frame->extramask));
372 restorer = current->mm->context.vdso + (long)&__kernel_sigreturn;
373 if (ka->sa.sa_flags & SA_RESTORER)
374 restorer = ka->sa.sa_restorer;
376 /* Set up to return from userspace. */
377 err |= __put_user(restorer, &frame->pretcode);
380 * This is popl %eax ; movl $,%eax ; int $0x80
382 * WE DO NOT USE IT ANY MORE! It's only left here for historical
383 * reasons and because gdb uses it as a signature to notice
384 * signal handler stack frames.
386 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
387 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
388 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
393 /* Set up registers for signal handler */
394 regs->esp = (unsigned long) frame;
395 regs->eip = (unsigned long) ka->sa.sa_handler;
398 regs->xds = __USER_DS;
399 regs->xes = __USER_DS;
400 regs->xss = __USER_DS;
401 regs->xcs = __USER_CS;
402 regs->eflags &= ~TF_MASK;
405 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
406 current->comm, current->pid, frame, regs->eip, frame->pretcode);
413 ka->sa.sa_handler = SIG_DFL;
414 force_sig(SIGSEGV, current);
417 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
418 sigset_t *set, struct pt_regs * regs)
420 void __user *restorer;
421 struct rt_sigframe __user *frame;
424 frame = get_sigframe(ka, regs, sizeof(*frame));
426 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
429 err |= __put_user((current_thread_info()->exec_domain
430 && current_thread_info()->exec_domain->signal_invmap
432 ? current_thread_info()->exec_domain->signal_invmap[sig]
435 err |= __put_user(&frame->info, &frame->pinfo);
436 err |= __put_user(&frame->uc, &frame->puc);
437 err |= copy_siginfo_to_user(&frame->info, info);
441 /* Create the ucontext. */
442 err |= __put_user(0, &frame->uc.uc_flags);
443 err |= __put_user(0, &frame->uc.uc_link);
444 err |= __put_user(current->sas_ss_sp, (unsigned long *)&frame->uc.uc_stack.ss_sp);
445 err |= __put_user(sas_ss_flags(regs->esp),
446 &frame->uc.uc_stack.ss_flags);
447 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
448 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
450 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
454 /* Set up to return from userspace. */
455 restorer = current->mm->context.vdso + (long)&__kernel_rt_sigreturn;
456 if (ka->sa.sa_flags & SA_RESTORER)
457 restorer = ka->sa.sa_restorer;
459 err |= __put_user(restorer, &frame->pretcode);
462 * This is movl $,%eax ; int $0x80
464 * WE DO NOT USE IT ANY MORE! It's only left here for historical
465 * reasons and because gdb uses it as a signature to notice
466 * signal handler stack frames.
468 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
469 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
470 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
475 /* Set up registers for signal handler */
476 regs->esp = (unsigned long) frame;
477 regs->eip = (unsigned long) ka->sa.sa_handler;
480 regs->xds = __USER_DS;
481 regs->xes = __USER_DS;
482 regs->xss = __USER_DS;
483 regs->xcs = __USER_CS;
484 regs->eflags &= ~TF_MASK;
487 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
488 current->comm, current->pid, frame, regs->eip, frame->pretcode);
495 ka->sa.sa_handler = SIG_DFL;
496 force_sig(SIGSEGV, current);
500 * OK, we're invoking a handler
504 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
505 struct pt_regs * regs)
507 struct k_sigaction *ka = ¤t->sighand->action[sig-1];
509 /* Are we from a system call? */
510 if (regs->orig_eax >= 0) {
511 /* If so, check system call restarting.. */
513 case -ERESTART_RESTARTBLOCK:
514 case -ERESTARTNOHAND:
519 if (!(ka->sa.sa_flags & SA_RESTART)) {
524 case -ERESTARTNOINTR:
525 regs->eax = regs->orig_eax;
530 /* Set up the stack frame */
531 if (ka->sa.sa_flags & SA_SIGINFO)
532 setup_rt_frame(sig, ka, info, oldset, regs);
534 setup_frame(sig, ka, oldset, regs);
536 if (ka->sa.sa_flags & SA_ONESHOT)
537 ka->sa.sa_handler = SIG_DFL;
539 if (!(ka->sa.sa_flags & SA_NODEFER)) {
540 spin_lock_irq(¤t->sighand->siglock);
541 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
542 sigaddset(¤t->blocked,sig);
544 spin_unlock_irq(¤t->sighand->siglock);
549 * Note that 'init' is a special process: it doesn't get signals it doesn't
550 * want to handle. Thus you cannot kill init even with a SIGKILL even by
553 int fastcall do_signal(struct pt_regs *regs, sigset_t *oldset)
559 * We want the common case to go fast, which
560 * is why we may in certain cases get here from
561 * kernel mode. Just return without doing anything
564 if ((regs->xcs & 3) != 3)
567 if (current->flags & PF_FREEZE) {
573 oldset = ¤t->blocked;
575 signr = get_signal_to_deliver(&info, regs, NULL);
577 /* Reenable any watchpoints before delivering the
578 * signal to user space. The processor register will
579 * have been cleared if the watchpoint triggered
582 __asm__("movl %0,%%db7" : : "r" (current->thread.debugreg[7]));
584 /* Whee! Actually deliver the signal. */
585 handle_signal(signr, &info, oldset, regs);
590 /* Did we come from a system call? */
591 if (regs->orig_eax >= 0) {
592 /* Restart the system call - no handlers present */
593 if (regs->eax == -ERESTARTNOHAND ||
594 regs->eax == -ERESTARTSYS ||
595 regs->eax == -ERESTARTNOINTR) {
596 regs->eax = regs->orig_eax;
599 if (regs->eax == -ERESTART_RESTARTBLOCK){
600 regs->eax = __NR_restart_syscall;
608 * notification of userspace execution resumption
609 * - triggered by current->work.notify_resume
611 __attribute__((regparm(3)))
612 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
613 __u32 thread_info_flags)
615 /* Pending single-step? */
616 if (thread_info_flags & _TIF_SINGLESTEP) {
617 regs->eflags |= TF_MASK;
618 clear_thread_flag(TIF_SINGLESTEP);
620 /* deal with pending signal delivery */
621 if (thread_info_flags & _TIF_SIGPENDING)
622 do_signal(regs,oldset);
624 clear_thread_flag(TIF_IRET);