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
7 #include <linux/list.h>
9 #include <linux/personality.h>
10 #include <linux/binfmts.h>
11 #include <linux/suspend.h>
12 #include <linux/kernel.h>
13 #include <linux/ptrace.h>
14 #include <linux/signal.h>
15 #include <linux/stddef.h>
16 #include <linux/unistd.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/tracehook.h>
21 #include <linux/elf.h>
22 #include <linux/smp.h>
25 #include <asm/processor.h>
26 #include <asm/ucontext.h>
27 #include <asm/uaccess.h>
30 #include <asm/syscalls.h>
34 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
37 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
38 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
42 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
44 # define FIX_EFLAGS __FIX_EFLAGS
48 * Atomically swap in the new signal mask, and wait for a signal.
51 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
54 spin_lock_irq(¤t->sighand->siglock);
55 current->saved_sigmask = current->blocked;
56 siginitset(¤t->blocked, mask);
58 spin_unlock_irq(¤t->sighand->siglock);
60 current->state = TASK_INTERRUPTIBLE;
62 set_restore_sigmask();
64 return -ERESTARTNOHAND;
68 sys_sigaction(int sig, const struct old_sigaction __user *act,
69 struct old_sigaction __user *oact)
71 struct k_sigaction new_ka, old_ka;
77 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
78 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
79 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
82 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
83 __get_user(mask, &act->sa_mask);
84 siginitset(&new_ka.sa.sa_mask, mask);
87 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
90 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
91 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
92 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
95 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
96 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
102 asmlinkage int sys_sigaltstack(unsigned long bx)
105 * This is needed to make gcc realize it doesn't own the
108 struct pt_regs *regs = (struct pt_regs *)&bx;
109 const stack_t __user *uss = (const stack_t __user *)bx;
110 stack_t __user *uoss = (stack_t __user *)regs->cx;
112 return do_sigaltstack(uss, uoss, regs->sp);
117 * Do a signal return; undo the signal stack.
120 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
123 unsigned int err = 0;
125 /* Always make any pending restarted system calls return -EINTR */
126 current_thread_info()->restart_block.fn = do_no_restart_syscall;
128 #define COPY(x) err |= __get_user(regs->x, &sc->x)
130 #define COPY_SEG(seg) \
131 { unsigned short tmp; \
132 err |= __get_user(tmp, &sc->seg); \
135 #define COPY_SEG_STRICT(seg) \
136 { unsigned short tmp; \
137 err |= __get_user(tmp, &sc->seg); \
140 #define GET_SEG(seg) \
141 { unsigned short tmp; \
142 err |= __get_user(tmp, &sc->seg); \
143 loadsegment(seg, tmp); }
149 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
150 COPY(dx); COPY(cx); COPY(ip);
155 unsigned int tmpflags;
157 err |= __get_user(tmpflags, &sc->flags);
158 regs->flags = (regs->flags & ~FIX_EFLAGS) |
159 (tmpflags & FIX_EFLAGS);
160 regs->orig_ax = -1; /* disable syscall checks */
166 err |= __get_user(buf, &sc->fpstate);
167 err |= restore_i387_xstate(buf);
170 err |= __get_user(*pax, &sc->ax);
174 asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
176 struct sigframe __user *frame;
177 struct pt_regs *regs;
181 regs = (struct pt_regs *) &__unused;
182 frame = (struct sigframe __user *)(regs->sp - 8);
184 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
186 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
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, &ax))
202 if (show_unhandled_signals && printk_ratelimit()) {
203 printk("%s%s[%d] bad frame in sigreturn frame:"
204 "%p ip:%lx sp:%lx oeax:%lx",
205 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
206 current->comm, task_pid_nr(current), frame, regs->ip,
207 regs->sp, regs->orig_ax);
208 print_vma_addr(" in ", regs->ip);
209 printk(KERN_CONT "\n");
212 force_sig(SIGSEGV, current);
217 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
219 struct pt_regs *regs = (struct pt_regs *)&__unused;
220 struct rt_sigframe __user *frame;
224 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
225 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
227 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
230 sigdelsetmask(&set, ~_BLOCKABLE);
231 spin_lock_irq(¤t->sighand->siglock);
232 current->blocked = set;
234 spin_unlock_irq(¤t->sighand->siglock);
236 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
239 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
245 force_sig(SIGSEGV, current);
250 * Set up a signal frame.
253 setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
254 struct pt_regs *regs, unsigned long mask)
258 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
259 savesegment(gs, tmp);
260 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
262 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
263 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
264 err |= __put_user(regs->di, &sc->di);
265 err |= __put_user(regs->si, &sc->si);
266 err |= __put_user(regs->bp, &sc->bp);
267 err |= __put_user(regs->sp, &sc->sp);
268 err |= __put_user(regs->bx, &sc->bx);
269 err |= __put_user(regs->dx, &sc->dx);
270 err |= __put_user(regs->cx, &sc->cx);
271 err |= __put_user(regs->ax, &sc->ax);
272 err |= __put_user(current->thread.trap_no, &sc->trapno);
273 err |= __put_user(current->thread.error_code, &sc->err);
274 err |= __put_user(regs->ip, &sc->ip);
275 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
276 err |= __put_user(regs->flags, &sc->flags);
277 err |= __put_user(regs->sp, &sc->sp_at_signal);
278 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
280 tmp = save_i387_xstate(fpstate);
284 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
286 /* non-iBCS2 extensions.. */
287 err |= __put_user(mask, &sc->oldmask);
288 err |= __put_user(current->thread.cr2, &sc->cr2);
294 * Determine which stack to use..
296 static inline void __user *
297 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
302 /* Default to using normal stack */
306 * If we are on the alternate signal stack and would overflow it, don't.
307 * Return an always-bogus address instead so we will die with SIGSEGV.
309 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
310 return (void __user *) -1L;
312 /* This is the X/Open sanctioned signal stack switching. */
313 if (ka->sa.sa_flags & SA_ONSTACK) {
314 if (sas_ss_flags(sp) == 0)
315 sp = current->sas_ss_sp + current->sas_ss_size;
317 /* This is the legacy signal stack switching. */
318 if ((regs->ss & 0xffff) != __USER_DS &&
319 !(ka->sa.sa_flags & SA_RESTORER) &&
321 sp = (unsigned long) ka->sa.sa_restorer;
325 sp = sp - sig_xstate_size;
326 *fpstate = (struct _fpstate *) sp;
331 * Align the stack pointer according to the i386 ABI,
332 * i.e. so that on function entry ((sp + 4) & 15) == 0.
334 sp = ((sp + 4) & -16ul) - 4;
336 return (void __user *) sp;
340 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
341 struct pt_regs *regs)
343 struct sigframe __user *frame;
344 void __user *restorer;
347 void __user *fpstate = NULL;
349 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
351 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
354 usig = current_thread_info()->exec_domain
355 && current_thread_info()->exec_domain->signal_invmap
357 ? current_thread_info()->exec_domain->signal_invmap[sig]
360 err = __put_user(usig, &frame->sig);
364 err = setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]);
368 if (_NSIG_WORDS > 1) {
369 err = __copy_to_user(&frame->extramask, &set->sig[1],
370 sizeof(frame->extramask));
375 if (current->mm->context.vdso)
376 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
378 restorer = &frame->retcode;
379 if (ka->sa.sa_flags & SA_RESTORER)
380 restorer = ka->sa.sa_restorer;
382 /* Set up to return from userspace. */
383 err |= __put_user(restorer, &frame->pretcode);
386 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
388 * WE DO NOT USE IT ANY MORE! It's only left here for historical
389 * reasons and because gdb uses it as a signature to notice
390 * signal handler stack frames.
392 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
393 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
394 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
399 /* Set up registers for signal handler */
400 regs->sp = (unsigned long)frame;
401 regs->ip = (unsigned long)ka->sa.sa_handler;
402 regs->ax = (unsigned long)sig;
406 regs->ds = __USER_DS;
407 regs->es = __USER_DS;
408 regs->ss = __USER_DS;
409 regs->cs = __USER_CS;
414 force_sigsegv(sig, current);
418 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
419 sigset_t *set, struct pt_regs *regs)
421 struct rt_sigframe __user *frame;
422 void __user *restorer;
425 void __user *fpstate = NULL;
427 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
429 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
432 usig = current_thread_info()->exec_domain
433 && current_thread_info()->exec_domain->signal_invmap
435 ? current_thread_info()->exec_domain->signal_invmap[sig]
438 err |= __put_user(usig, &frame->sig);
439 err |= __put_user(&frame->info, &frame->pinfo);
440 err |= __put_user(&frame->uc, &frame->puc);
441 err |= copy_siginfo_to_user(&frame->info, info);
445 /* Create the ucontext. */
447 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
449 err |= __put_user(0, &frame->uc.uc_flags);
450 err |= __put_user(0, &frame->uc.uc_link);
451 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
452 err |= __put_user(sas_ss_flags(regs->sp),
453 &frame->uc.uc_stack.ss_flags);
454 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
455 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
457 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
461 /* Set up to return from userspace. */
462 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
463 if (ka->sa.sa_flags & SA_RESTORER)
464 restorer = ka->sa.sa_restorer;
465 err |= __put_user(restorer, &frame->pretcode);
468 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
470 * WE DO NOT USE IT ANY MORE! It's only left here for historical
471 * reasons and because gdb uses it as a signature to notice
472 * signal handler stack frames.
474 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
475 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
476 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
481 /* Set up registers for signal handler */
482 regs->sp = (unsigned long)frame;
483 regs->ip = (unsigned long)ka->sa.sa_handler;
484 regs->ax = (unsigned long)usig;
485 regs->dx = (unsigned long)&frame->info;
486 regs->cx = (unsigned long)&frame->uc;
488 regs->ds = __USER_DS;
489 regs->es = __USER_DS;
490 regs->ss = __USER_DS;
491 regs->cs = __USER_CS;
496 force_sigsegv(sig, current);
501 * OK, we're invoking a handler:
504 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
505 sigset_t *oldset, struct pt_regs *regs)
509 /* Are we from a system call? */
510 if ((long)regs->orig_ax >= 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->ax = regs->orig_ax;
532 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
533 * flag so that register information in the sigcontext is correct.
535 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
536 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
537 regs->flags &= ~X86_EFLAGS_TF;
539 /* Set up the stack frame */
540 if (ka->sa.sa_flags & SA_SIGINFO)
541 ret = setup_rt_frame(sig, ka, info, oldset, regs);
543 ret = setup_frame(sig, ka, oldset, regs);
549 * Clear the direction flag as per the ABI for function entry.
551 regs->flags &= ~X86_EFLAGS_DF;
554 * Clear TF when entering the signal handler, but
555 * notify any tracer that was single-stepping it.
556 * The tracer may want to single-step inside the
559 regs->flags &= ~X86_EFLAGS_TF;
561 spin_lock_irq(¤t->sighand->siglock);
562 sigorsets(¤t->blocked, ¤t->blocked, &ka->sa.sa_mask);
563 if (!(ka->sa.sa_flags & SA_NODEFER))
564 sigaddset(¤t->blocked, sig);
566 spin_unlock_irq(¤t->sighand->siglock);
568 tracehook_signal_handler(sig, info, ka, regs,
569 test_thread_flag(TIF_SINGLESTEP));
575 * Note that 'init' is a special process: it doesn't get signals it doesn't
576 * want to handle. Thus you cannot kill init even with a SIGKILL even by
579 static void do_signal(struct pt_regs *regs)
581 struct k_sigaction ka;
587 * We want the common case to go fast, which is why we may in certain
588 * cases get here from kernel mode. Just return without doing anything
590 * X86_32: vm86 regs switched out by assembly code before reaching
591 * here, so testing against kernel CS suffices.
593 if (!user_mode(regs))
596 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
597 oldset = ¤t->saved_sigmask;
599 oldset = ¤t->blocked;
601 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
604 * Re-enable any watchpoints before delivering the
605 * signal to user space. The processor register will
606 * have been cleared if the watchpoint triggered
609 if (current->thread.debugreg7)
610 set_debugreg(current->thread.debugreg7, 7);
612 /* Whee! Actually deliver the signal. */
613 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
615 * A signal was successfully delivered; the saved
616 * sigmask will have been stored in the signal frame,
617 * and will be restored by sigreturn, so we can simply
618 * clear the TS_RESTORE_SIGMASK flag.
620 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
625 /* Did we come from a system call? */
626 if ((long)regs->orig_ax >= 0) {
627 /* Restart the system call - no handlers present */
629 case -ERESTARTNOHAND:
631 case -ERESTARTNOINTR:
632 regs->ax = regs->orig_ax;
636 case -ERESTART_RESTARTBLOCK:
637 regs->ax = __NR_restart_syscall;
644 * If there's no signal to deliver, we just put the saved sigmask
647 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
648 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
649 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
654 * notification of userspace execution resumption
655 * - triggered by the TIF_WORK_MASK flags
658 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
660 /* deal with pending signal delivery */
661 if (thread_info_flags & _TIF_SIGPENDING)
664 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
665 clear_thread_flag(TIF_NOTIFY_RESUME);
666 tracehook_notify_resume(regs);
669 clear_thread_flag(TIF_IRET);