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/unistd.h>
16 #include <linux/stddef.h>
17 #include <linux/personality.h>
18 #include <linux/suspend.h>
19 #include <linux/ptrace.h>
20 #include <linux/elf.h>
21 #include <linux/binfmts.h>
22 #include <asm/processor.h>
23 #include <asm/ucontext.h>
24 #include <asm/uaccess.h>
27 #include "sigframe_32.h"
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
34 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
35 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
39 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
41 # define FIX_EFLAGS __FIX_EFLAGS
45 * Atomically swap in the new signal mask, and wait for a signal.
48 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
51 spin_lock_irq(¤t->sighand->siglock);
52 current->saved_sigmask = current->blocked;
53 siginitset(¤t->blocked, mask);
55 spin_unlock_irq(¤t->sighand->siglock);
57 current->state = TASK_INTERRUPTIBLE;
59 set_thread_flag(TIF_RESTORE_SIGMASK);
60 return -ERESTARTNOHAND;
64 sys_sigaction(int sig, const struct old_sigaction __user *act,
65 struct old_sigaction __user *oact)
67 struct k_sigaction new_ka, old_ka;
72 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
73 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
74 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
76 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
77 __get_user(mask, &act->sa_mask);
78 siginitset(&new_ka.sa.sa_mask, mask);
81 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
84 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
85 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
86 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
88 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
89 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
96 sys_sigaltstack(unsigned long bx)
98 /* This is needed to make gcc realize it doesn't own the "struct pt_regs" */
99 struct pt_regs *regs = (struct pt_regs *)&bx;
100 const stack_t __user *uss = (const stack_t __user *)bx;
101 stack_t __user *uoss = (stack_t __user *)regs->cx;
103 return do_sigaltstack(uss, uoss, regs->sp);
108 * Do a signal return; undo the signal stack.
112 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *peax)
114 unsigned int err = 0;
116 /* Always make any pending restarted system calls return -EINTR */
117 current_thread_info()->restart_block.fn = do_no_restart_syscall;
119 #define COPY(x) err |= __get_user(regs->x, &sc->x)
121 #define COPY_SEG(seg) \
122 { unsigned short tmp; \
123 err |= __get_user(tmp, &sc->seg); \
126 #define COPY_SEG_STRICT(seg) \
127 { unsigned short tmp; \
128 err |= __get_user(tmp, &sc->seg); \
131 #define GET_SEG(seg) \
132 { unsigned short tmp; \
133 err |= __get_user(tmp, &sc->seg); \
134 loadsegment(seg,tmp); }
140 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
141 COPY(dx); COPY(cx); COPY(ip);
146 unsigned int tmpflags;
147 err |= __get_user(tmpflags, &sc->flags);
148 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
149 regs->orig_ax = -1; /* disable syscall checks */
153 struct _fpstate __user * buf;
154 err |= __get_user(buf, &sc->fpstate);
156 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
158 err |= restore_i387(buf);
160 struct task_struct *me = current;
168 err |= __get_user(*peax, &sc->ax);
175 asmlinkage int sys_sigreturn(unsigned long __unused)
177 struct pt_regs *regs = (struct pt_regs *) &__unused;
178 struct sigframe __user *frame = (struct sigframe __user *)(regs->sp - 8);
182 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
184 if (__get_user(set.sig[0], &frame->sc.oldmask)
186 && __copy_from_user(&set.sig[1], &frame->extramask,
187 sizeof(frame->extramask))))
190 sigdelsetmask(&set, ~_BLOCKABLE);
191 spin_lock_irq(¤t->sighand->siglock);
192 current->blocked = set;
194 spin_unlock_irq(¤t->sighand->siglock);
196 if (restore_sigcontext(regs, &frame->sc, &ax))
201 if (show_unhandled_signals && printk_ratelimit()) {
202 printk("%s%s[%d] bad frame in sigreturn frame:%p ip:%lx"
204 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
205 current->comm, task_pid_nr(current), frame, regs->ip,
206 regs->sp, regs->orig_ax);
207 print_vma_addr(" in ", regs->ip);
211 force_sig(SIGSEGV, current);
215 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
217 struct pt_regs *regs = (struct pt_regs *)&__unused;
218 struct rt_sigframe __user *frame;
222 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
223 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
225 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
228 sigdelsetmask(&set, ~_BLOCKABLE);
229 spin_lock_irq(¤t->sighand->siglock);
230 current->blocked = set;
232 spin_unlock_irq(¤t->sighand->siglock);
234 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
237 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
243 force_sig(SIGSEGV, current);
248 * Set up a signal frame.
252 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
253 struct pt_regs *regs, unsigned long mask)
257 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
258 savesegment(gs, tmp);
259 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
261 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
262 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
263 err |= __put_user(regs->di, &sc->di);
264 err |= __put_user(regs->si, &sc->si);
265 err |= __put_user(regs->bp, &sc->bp);
266 err |= __put_user(regs->sp, &sc->sp);
267 err |= __put_user(regs->bx, &sc->bx);
268 err |= __put_user(regs->dx, &sc->dx);
269 err |= __put_user(regs->cx, &sc->cx);
270 err |= __put_user(regs->ax, &sc->ax);
271 err |= __put_user(current->thread.trap_no, &sc->trapno);
272 err |= __put_user(current->thread.error_code, &sc->err);
273 err |= __put_user(regs->ip, &sc->ip);
274 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
275 err |= __put_user(regs->flags, &sc->flags);
276 err |= __put_user(regs->sp, &sc->sp_at_signal);
277 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
279 tmp = save_i387(fpstate);
283 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
285 /* non-iBCS2 extensions.. */
286 err |= __put_user(mask, &sc->oldmask);
287 err |= __put_user(current->thread.cr2, &sc->cr2);
293 * Determine which stack to use..
295 static inline void __user *
296 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
300 /* Default to using normal stack */
304 * If we are on the alternate signal stack and would overflow it, don't.
305 * Return an always-bogus address instead so we will die with SIGSEGV.
307 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
308 return (void __user *) -1L;
310 /* This is the X/Open sanctioned signal stack switching. */
311 if (ka->sa.sa_flags & SA_ONSTACK) {
312 if (sas_ss_flags(sp) == 0)
313 sp = current->sas_ss_sp + current->sas_ss_size;
316 /* This is the legacy signal stack switching. */
317 else if ((regs->ss & 0xffff) != __USER_DS &&
318 !(ka->sa.sa_flags & SA_RESTORER) &&
319 ka->sa.sa_restorer) {
320 sp = (unsigned long) ka->sa.sa_restorer;
324 /* Align the stack pointer according to the i386 ABI,
325 * i.e. so that on function entry ((sp + 4) & 15) == 0. */
326 sp = ((sp + 4) & -16ul) - 4;
327 return (void __user *) sp;
330 /* These symbols are defined with the addresses in the vsyscall page.
331 See vsyscall-sigreturn.S. */
332 extern void __user __kernel_sigreturn;
333 extern void __user __kernel_rt_sigreturn;
335 static int setup_frame(int sig, struct k_sigaction *ka,
336 sigset_t *set, struct pt_regs * regs)
338 void __user *restorer;
339 struct sigframe __user *frame;
343 frame = get_sigframe(ka, regs, sizeof(*frame));
345 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
348 usig = current_thread_info()->exec_domain
349 && current_thread_info()->exec_domain->signal_invmap
351 ? current_thread_info()->exec_domain->signal_invmap[sig]
354 err = __put_user(usig, &frame->sig);
358 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
362 if (_NSIG_WORDS > 1) {
363 err = __copy_to_user(&frame->extramask, &set->sig[1],
364 sizeof(frame->extramask));
369 if (current->mm->context.vdso)
370 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
372 restorer = &frame->retcode;
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->sp = (unsigned long) frame;
395 regs->ip = (unsigned long) ka->sa.sa_handler;
396 regs->ax = (unsigned long) sig;
400 regs->ds = __USER_DS;
401 regs->es = __USER_DS;
402 regs->ss = __USER_DS;
403 regs->cs = __USER_CS;
406 * Clear TF when entering the signal handler, but
407 * notify any tracer that was single-stepping it.
408 * The tracer may want to single-step inside the
411 regs->flags &= ~(TF_MASK | X86_EFLAGS_DF);
412 if (test_thread_flag(TIF_SINGLESTEP))
413 ptrace_notify(SIGTRAP);
416 printk("SIG deliver (%s:%d): sp=%p pc=%lx ra=%p\n",
417 current->comm, current->pid, frame, regs->ip, frame->pretcode);
423 force_sigsegv(sig, current);
427 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
428 sigset_t *set, struct pt_regs * regs)
430 void __user *restorer;
431 struct rt_sigframe __user *frame;
435 frame = get_sigframe(ka, regs, sizeof(*frame));
437 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
440 usig = current_thread_info()->exec_domain
441 && current_thread_info()->exec_domain->signal_invmap
443 ? current_thread_info()->exec_domain->signal_invmap[sig]
446 err |= __put_user(usig, &frame->sig);
447 err |= __put_user(&frame->info, &frame->pinfo);
448 err |= __put_user(&frame->uc, &frame->puc);
449 err |= copy_siginfo_to_user(&frame->info, info);
453 /* Create the ucontext. */
454 err |= __put_user(0, &frame->uc.uc_flags);
455 err |= __put_user(0, &frame->uc.uc_link);
456 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
457 err |= __put_user(sas_ss_flags(regs->sp),
458 &frame->uc.uc_stack.ss_flags);
459 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
460 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
462 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
466 /* Set up to return from userspace. */
467 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
468 if (ka->sa.sa_flags & SA_RESTORER)
469 restorer = ka->sa.sa_restorer;
470 err |= __put_user(restorer, &frame->pretcode);
473 * This is movl $,%ax ; int $0x80
475 * WE DO NOT USE IT ANY MORE! It's only left here for historical
476 * reasons and because gdb uses it as a signature to notice
477 * signal handler stack frames.
479 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
480 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
481 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
486 /* Set up registers for signal handler */
487 regs->sp = (unsigned long) frame;
488 regs->ip = (unsigned long) ka->sa.sa_handler;
489 regs->ax = (unsigned long) usig;
490 regs->dx = (unsigned long) &frame->info;
491 regs->cx = (unsigned long) &frame->uc;
493 regs->ds = __USER_DS;
494 regs->es = __USER_DS;
495 regs->ss = __USER_DS;
496 regs->cs = __USER_CS;
499 * Clear TF when entering the signal handler, but
500 * notify any tracer that was single-stepping it.
501 * The tracer may want to single-step inside the
504 regs->flags &= ~(TF_MASK | X86_EFLAGS_DF);
505 if (test_thread_flag(TIF_SINGLESTEP))
506 ptrace_notify(SIGTRAP);
509 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
510 current->comm, current->pid, frame, regs->ip, frame->pretcode);
516 force_sigsegv(sig, current);
521 * OK, we're invoking a handler
525 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
526 sigset_t *oldset, struct pt_regs *regs)
530 /* Are we from a system call? */
531 if ((long)regs->orig_ax >= 0) {
532 /* If so, check system call restarting.. */
534 case -ERESTART_RESTARTBLOCK:
535 case -ERESTARTNOHAND:
540 if (!(ka->sa.sa_flags & SA_RESTART)) {
545 case -ERESTARTNOINTR:
546 regs->ax = regs->orig_ax;
553 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
554 * flag so that register information in the sigcontext is correct.
556 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
557 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
558 regs->flags &= ~X86_EFLAGS_TF;
560 /* Set up the stack frame */
561 if (ka->sa.sa_flags & SA_SIGINFO)
562 ret = setup_rt_frame(sig, ka, info, oldset, regs);
564 ret = setup_frame(sig, ka, oldset, regs);
567 spin_lock_irq(¤t->sighand->siglock);
568 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
569 if (!(ka->sa.sa_flags & SA_NODEFER))
570 sigaddset(¤t->blocked,sig);
572 spin_unlock_irq(¤t->sighand->siglock);
579 * Note that 'init' is a special process: it doesn't get signals it doesn't
580 * want to handle. Thus you cannot kill init even with a SIGKILL even by
583 static void do_signal(struct pt_regs *regs)
585 struct k_sigaction ka;
591 * We want the common case to go fast, which is why we may in certain
592 * cases get here from kernel mode. Just return without doing anything
594 * X86_32: vm86 regs switched out by assembly code before reaching
595 * here, so testing against kernel CS suffices.
597 if (!user_mode(regs))
600 if (test_thread_flag(TIF_RESTORE_SIGMASK))
601 oldset = ¤t->saved_sigmask;
603 oldset = ¤t->blocked;
605 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
607 /* Re-enable any watchpoints before delivering the
608 * signal to user space. The processor register will
609 * have been cleared if the watchpoint triggered
612 if (current->thread.debugreg7)
613 set_debugreg(current->thread.debugreg7, 7);
615 /* Whee! Actually deliver the signal. */
616 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
617 /* a signal was successfully delivered; the saved
618 * sigmask will have been stored in the signal frame,
619 * and will be restored by sigreturn, so we can simply
620 * clear the TIF_RESTORE_SIGMASK flag */
621 if (test_thread_flag(TIF_RESTORE_SIGMASK))
622 clear_thread_flag(TIF_RESTORE_SIGMASK);
628 /* Did we come from a system call? */
629 if ((long)regs->orig_ax >= 0) {
630 /* Restart the system call - no handlers present */
632 case -ERESTARTNOHAND:
634 case -ERESTARTNOINTR:
635 regs->ax = regs->orig_ax;
639 case -ERESTART_RESTARTBLOCK:
640 regs->ax = __NR_restart_syscall;
647 * If there's no signal to deliver, we just put the saved sigmask
650 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
651 clear_thread_flag(TIF_RESTORE_SIGMASK);
652 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
657 * notification of userspace execution resumption
658 * - triggered by the TIF_WORK_MASK flags
660 void do_notify_resume(struct pt_regs *regs, void *unused,
661 __u32 thread_info_flags)
663 /* Pending single-step? */
664 if (thread_info_flags & _TIF_SINGLESTEP) {
665 regs->flags |= X86_EFLAGS_TF;
666 clear_thread_flag(TIF_SINGLESTEP);
669 /* deal with pending signal delivery */
670 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
673 if (thread_info_flags & _TIF_HRTICK_RESCHED)
676 clear_thread_flag(TIF_IRET);