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(struct pt_regs regs)
121 const stack_t __user *uss = (const stack_t __user *)regs.ebx;
122 stack_t __user *uoss = (stack_t __user *)regs.ecx;
123 return do_sigaltstack(uss, uoss, regs.esp);
128 * Do a signal return; undo the signal stack.
132 restore_sigcontext(struct pt_regs *regs,
133 struct sigcontext __user *__sc, int *peax)
135 struct sigcontext scratch; /* 88 bytes of scratch area */
137 /* Always make any pending restarted system calls return -EINTR */
138 current_thread_info()->restart_block.fn = do_no_restart_syscall;
140 if (copy_from_user(&scratch, __sc, sizeof(scratch)))
143 #define COPY(x) regs->x = scratch.x
145 #define COPY_SEG(seg) \
146 { unsigned short tmp = scratch.seg; \
147 regs->x##seg = tmp; }
149 #define COPY_SEG_STRICT(seg) \
150 { unsigned short tmp = scratch.seg; \
151 regs->x##seg = tmp|3; }
153 #define GET_SEG(seg) \
154 { unsigned short tmp = scratch.seg; \
155 loadsegment(seg,tmp); }
157 #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | X86_EFLAGS_DF | \
158 X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \
159 X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF)
177 unsigned int tmpflags = scratch.eflags;
178 regs->eflags = (regs->eflags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
179 regs->orig_eax = -1; /* disable syscall checks */
183 struct _fpstate * buf = scratch.fpstate;
185 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
187 if (restore_i387(buf))
196 asmlinkage int sys_sigreturn(unsigned long __unused)
198 struct pt_regs *regs = (struct pt_regs *) &__unused;
199 struct sigframe __user *frame = (struct sigframe __user *)(regs->esp - 8);
203 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
205 if (__get_user(set.sig[0], &frame->sc.oldmask)
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, &eax))
222 force_sig(SIGSEGV, current);
226 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
228 struct pt_regs *regs = (struct pt_regs *) &__unused;
229 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4);
233 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
235 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
238 sigdelsetmask(&set, ~_BLOCKABLE);
239 spin_lock_irq(¤t->sighand->siglock);
240 current->blocked = set;
242 spin_unlock_irq(¤t->sighand->siglock);
244 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
247 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT)
253 force_sig(SIGSEGV, current);
258 * Set up a signal frame.
262 setup_sigcontext(struct sigcontext __user *__sc, struct _fpstate __user *fpstate,
263 struct pt_regs *regs, unsigned long mask)
265 struct sigcontext sc; /* 88 bytes of scratch area */
269 __asm__("movl %%gs,%0" : "=r"(tmp): "0"(tmp));
270 *(unsigned int *)&sc.gs = tmp;
271 __asm__("movl %%fs,%0" : "=r"(tmp): "0"(tmp));
272 *(unsigned int *)&sc.fs = tmp;
273 *(unsigned int *)&sc.es = regs->xes;
274 *(unsigned int *)&sc.ds = regs->xds;
283 sc.trapno = current->thread.trap_no;
284 sc.err = current->thread.error_code;
286 *(unsigned int *)&sc.cs = regs->xcs;
287 sc.eflags = regs->eflags;
288 sc.esp_at_signal = regs->esp;
289 *(unsigned int *)&sc.ss = regs->xss;
291 tmp = save_i387(fpstate);
294 sc.fpstate = tmp ? fpstate : NULL;
296 /* non-iBCS2 extensions.. */
298 sc.cr2 = current->thread.cr2;
300 if (copy_to_user(__sc, &sc, sizeof(sc)))
306 * Determine which stack to use..
308 static inline void __user *
309 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
313 /* Default to using normal stack */
316 /* This is the X/Open sanctioned signal stack switching. */
317 if (ka->sa.sa_flags & SA_ONSTACK) {
318 if (sas_ss_flags(esp) == 0)
319 esp = current->sas_ss_sp + current->sas_ss_size;
322 /* This is the legacy signal stack switching. */
323 else if ((regs->xss & 0xffff) != __USER_DS &&
324 !(ka->sa.sa_flags & SA_RESTORER) &&
325 ka->sa.sa_restorer) {
326 esp = (unsigned long) ka->sa.sa_restorer;
329 return (void __user *)((esp - frame_size) & -8ul);
332 /* These symbols are defined with the addresses in the vsyscall page.
333 See vsyscall-sigreturn.S. */
334 extern char __kernel_sigreturn, __kernel_rt_sigreturn, SYSENTER_RETURN;
336 static void setup_frame(int sig, struct k_sigaction *ka,
337 sigset_t *set, struct pt_regs * regs)
340 struct sigframe __user *frame;
343 frame = get_sigframe(ka, regs, sizeof(*frame));
345 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
348 err |= __put_user((current_thread_info()->exec_domain
349 && current_thread_info()->exec_domain->signal_invmap
351 ? current_thread_info()->exec_domain->signal_invmap[sig]
357 err |= setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
361 if (_NSIG_WORDS > 1) {
362 err |= __copy_to_user(&frame->extramask, &set->sig[1],
363 sizeof(frame->extramask));
368 restorer = current->mm->context.vdso + (long)&__kernel_sigreturn;
369 if (ka->sa.sa_flags & SA_RESTORER)
370 restorer = ka->sa.sa_restorer;
372 /* Set up to return from userspace. */
373 err |= __put_user(restorer, &frame->pretcode);
376 * This is popl %eax ; movl $,%eax ; int $0x80
378 * WE DO NOT USE IT ANY MORE! It's only left here for historical
379 * reasons and because gdb uses it as a signature to notice
380 * signal handler stack frames.
382 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
383 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
384 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
389 /* Set up registers for signal handler */
390 regs->esp = (unsigned long) frame;
391 regs->eip = (unsigned long) ka->sa.sa_handler;
394 regs->xds = __USER_DS;
395 regs->xes = __USER_DS;
396 regs->xss = __USER_DS;
397 regs->xcs = __USER_CS;
398 regs->eflags &= ~TF_MASK;
401 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
402 current->comm, current->pid, frame, regs->eip, frame->pretcode);
409 ka->sa.sa_handler = SIG_DFL;
410 force_sig(SIGSEGV, current);
413 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
414 sigset_t *set, struct pt_regs * regs)
417 struct rt_sigframe __user *frame;
420 frame = get_sigframe(ka, regs, sizeof(*frame));
422 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
425 err |= __put_user((current_thread_info()->exec_domain
426 && current_thread_info()->exec_domain->signal_invmap
428 ? current_thread_info()->exec_domain->signal_invmap[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, (unsigned long *)&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 = current->mm->context.vdso + (long)&__kernel_rt_sigreturn;
452 if (ka->sa.sa_flags & SA_RESTORER)
453 restorer = ka->sa.sa_restorer;
455 err |= __put_user(restorer, &frame->pretcode);
458 * This is movl $,%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(0xb8, (char __user *)(frame->retcode+0));
465 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
466 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
471 /* Set up registers for signal handler */
472 regs->esp = (unsigned long) frame;
473 regs->eip = (unsigned long) ka->sa.sa_handler;
476 regs->xds = __USER_DS;
477 regs->xes = __USER_DS;
478 regs->xss = __USER_DS;
479 regs->xcs = __USER_CS;
480 regs->eflags &= ~TF_MASK;
483 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
484 current->comm, current->pid, frame, regs->eip, frame->pretcode);
491 ka->sa.sa_handler = SIG_DFL;
492 force_sig(SIGSEGV, current);
496 * OK, we're invoking a handler
500 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
501 struct pt_regs * regs)
503 struct k_sigaction *ka = ¤t->sighand->action[sig-1];
505 /* Are we from a system call? */
506 if (regs->orig_eax >= 0) {
507 /* If so, check system call restarting.. */
509 case -ERESTART_RESTARTBLOCK:
510 case -ERESTARTNOHAND:
515 if (!(ka->sa.sa_flags & SA_RESTART)) {
520 case -ERESTARTNOINTR:
521 regs->eax = regs->orig_eax;
526 /* Set up the stack frame */
527 if (ka->sa.sa_flags & SA_SIGINFO)
528 setup_rt_frame(sig, ka, info, oldset, regs);
530 setup_frame(sig, ka, oldset, regs);
532 if (ka->sa.sa_flags & SA_ONESHOT)
533 ka->sa.sa_handler = SIG_DFL;
535 if (!(ka->sa.sa_flags & SA_NODEFER)) {
536 spin_lock_irq(¤t->sighand->siglock);
537 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
538 sigaddset(¤t->blocked,sig);
540 spin_unlock_irq(¤t->sighand->siglock);
545 * Note that 'init' is a special process: it doesn't get signals it doesn't
546 * want to handle. Thus you cannot kill init even with a SIGKILL even by
549 int fastcall do_signal(struct pt_regs *regs, sigset_t *oldset)
555 * We want the common case to go fast, which
556 * is why we may in certain cases get here from
557 * kernel mode. Just return without doing anything
560 if ((regs->xcs & 3) != 3)
563 if (current->flags & PF_FREEZE) {
569 oldset = ¤t->blocked;
571 signr = get_signal_to_deliver(&info, regs, NULL);
573 /* Reenable any watchpoints before delivering the
574 * signal to user space. The processor register will
575 * have been cleared if the watchpoint triggered
578 __asm__("movl %0,%%db7" : : "r" (current->thread.debugreg[7]));
580 /* Whee! Actually deliver the signal. */
581 handle_signal(signr, &info, oldset, regs);
586 /* Did we come from a system call? */
587 if (regs->orig_eax >= 0) {
588 /* Restart the system call - no handlers present */
589 if (regs->eax == -ERESTARTNOHAND ||
590 regs->eax == -ERESTARTSYS ||
591 regs->eax == -ERESTARTNOINTR) {
592 regs->eax = regs->orig_eax;
595 if (regs->eax == -ERESTART_RESTARTBLOCK){
596 regs->eax = __NR_restart_syscall;
604 * notification of userspace execution resumption
605 * - triggered by current->work.notify_resume
607 __attribute__((regparm(3)))
608 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
609 __u32 thread_info_flags)
611 /* Pending single-step? */
612 if (thread_info_flags & _TIF_SINGLESTEP) {
613 regs->eflags |= TF_MASK;
614 clear_thread_flag(TIF_SINGLESTEP);
616 /* deal with pending signal delivery */
617 if (thread_info_flags & _TIF_SIGPENDING)
618 do_signal(regs,oldset);
620 clear_thread_flag(TIF_IRET);