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/elf.h>
21 #include <linux/smp.h>
24 #include <asm/processor.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
35 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
36 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
40 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
42 # define FIX_EFLAGS __FIX_EFLAGS
46 * Atomically swap in the new signal mask, and wait for a signal.
49 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
52 spin_lock_irq(¤t->sighand->siglock);
53 current->saved_sigmask = current->blocked;
54 siginitset(¤t->blocked, mask);
56 spin_unlock_irq(¤t->sighand->siglock);
58 current->state = TASK_INTERRUPTIBLE;
60 set_restore_sigmask();
62 return -ERESTARTNOHAND;
66 sys_sigaction(int sig, const struct old_sigaction __user *act,
67 struct old_sigaction __user *oact)
69 struct k_sigaction new_ka, old_ka;
75 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
76 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
77 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
80 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
81 __get_user(mask, &act->sa_mask);
82 siginitset(&new_ka.sa.sa_mask, mask);
85 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
88 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
89 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
90 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
93 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
94 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
100 asmlinkage int sys_sigaltstack(unsigned long bx)
103 * This is needed to make gcc realize it doesn't own the
106 struct pt_regs *regs = (struct pt_regs *)&bx;
107 const stack_t __user *uss = (const stack_t __user *)bx;
108 stack_t __user *uoss = (stack_t __user *)regs->cx;
110 return do_sigaltstack(uss, uoss, regs->sp);
115 * Do a signal return; undo the signal stack.
118 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
121 unsigned int err = 0;
123 /* Always make any pending restarted system calls return -EINTR */
124 current_thread_info()->restart_block.fn = do_no_restart_syscall;
126 #define COPY(x) err |= __get_user(regs->x, &sc->x)
128 #define COPY_SEG(seg) \
129 { unsigned short tmp; \
130 err |= __get_user(tmp, &sc->seg); \
133 #define COPY_SEG_STRICT(seg) \
134 { unsigned short tmp; \
135 err |= __get_user(tmp, &sc->seg); \
138 #define GET_SEG(seg) \
139 { unsigned short tmp; \
140 err |= __get_user(tmp, &sc->seg); \
141 loadsegment(seg, tmp); }
147 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
148 COPY(dx); COPY(cx); COPY(ip);
153 unsigned int tmpflags;
155 err |= __get_user(tmpflags, &sc->flags);
156 regs->flags = (regs->flags & ~FIX_EFLAGS) |
157 (tmpflags & FIX_EFLAGS);
158 regs->orig_ax = -1; /* disable syscall checks */
162 struct _fpstate __user *buf;
164 err |= __get_user(buf, &sc->fpstate);
166 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
168 err |= restore_i387(buf);
170 struct task_struct *me = current;
179 err |= __get_user(*pax, &sc->ax);
186 asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
188 struct sigframe __user *frame;
189 struct pt_regs *regs;
193 regs = (struct pt_regs *) &__unused;
194 frame = (struct sigframe __user *)(regs->sp - 8);
196 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
198 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
199 && __copy_from_user(&set.sig[1], &frame->extramask,
200 sizeof(frame->extramask))))
203 sigdelsetmask(&set, ~_BLOCKABLE);
204 spin_lock_irq(¤t->sighand->siglock);
205 current->blocked = set;
207 spin_unlock_irq(¤t->sighand->siglock);
209 if (restore_sigcontext(regs, &frame->sc, &ax))
214 if (show_unhandled_signals && printk_ratelimit()) {
215 printk(KERN_INFO "%s%s[%d] bad frame in sigreturn frame:"
216 "%p ip:%lx sp:%lx oeax:%lx",
217 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
218 current->comm, task_pid_nr(current), frame, regs->ip,
219 regs->sp, regs->orig_ax);
220 print_vma_addr(" in ", regs->ip);
221 printk(KERN_CONT "\n");
224 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;
236 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
237 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
239 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
242 sigdelsetmask(&set, ~_BLOCKABLE);
243 spin_lock_irq(¤t->sighand->siglock);
244 current->blocked = set;
246 spin_unlock_irq(¤t->sighand->siglock);
248 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
251 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
257 force_sig(SIGSEGV, current);
262 * Set up a signal frame.
265 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
266 struct pt_regs *regs, unsigned long mask)
270 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
271 savesegment(gs, tmp);
272 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
274 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
275 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
276 err |= __put_user(regs->di, &sc->di);
277 err |= __put_user(regs->si, &sc->si);
278 err |= __put_user(regs->bp, &sc->bp);
279 err |= __put_user(regs->sp, &sc->sp);
280 err |= __put_user(regs->bx, &sc->bx);
281 err |= __put_user(regs->dx, &sc->dx);
282 err |= __put_user(regs->cx, &sc->cx);
283 err |= __put_user(regs->ax, &sc->ax);
284 err |= __put_user(current->thread.trap_no, &sc->trapno);
285 err |= __put_user(current->thread.error_code, &sc->err);
286 err |= __put_user(regs->ip, &sc->ip);
287 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
288 err |= __put_user(regs->flags, &sc->flags);
289 err |= __put_user(regs->sp, &sc->sp_at_signal);
290 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
292 tmp = save_i387(fpstate);
296 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
298 /* non-iBCS2 extensions.. */
299 err |= __put_user(mask, &sc->oldmask);
300 err |= __put_user(current->thread.cr2, &sc->cr2);
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 */
317 * If we are on the alternate signal stack and would overflow it, don't.
318 * Return an always-bogus address instead so we will die with SIGSEGV.
320 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
321 return (void __user *) -1L;
323 /* This is the X/Open sanctioned signal stack switching. */
324 if (ka->sa.sa_flags & SA_ONSTACK) {
325 if (sas_ss_flags(sp) == 0)
326 sp = current->sas_ss_sp + current->sas_ss_size;
328 /* This is the legacy signal stack switching. */
329 if ((regs->ss & 0xffff) != __USER_DS &&
330 !(ka->sa.sa_flags & SA_RESTORER) &&
332 sp = (unsigned long) ka->sa.sa_restorer;
337 * Align the stack pointer according to the i386 ABI,
338 * i.e. so that on function entry ((sp + 4) & 15) == 0.
340 sp = ((sp + 4) & -16ul) - 4;
342 return (void __user *) sp;
346 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
347 struct pt_regs *regs)
349 struct sigframe __user *frame;
350 void __user *restorer;
354 frame = get_sigframe(ka, regs, sizeof(*frame));
356 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
359 usig = current_thread_info()->exec_domain
360 && current_thread_info()->exec_domain->signal_invmap
362 ? current_thread_info()->exec_domain->signal_invmap[sig]
365 err = __put_user(usig, &frame->sig);
369 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
373 if (_NSIG_WORDS > 1) {
374 err = __copy_to_user(&frame->extramask, &set->sig[1],
375 sizeof(frame->extramask));
380 if (current->mm->context.vdso)
381 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
383 restorer = &frame->retcode;
384 if (ka->sa.sa_flags & SA_RESTORER)
385 restorer = ka->sa.sa_restorer;
387 /* Set up to return from userspace. */
388 err |= __put_user(restorer, &frame->pretcode);
391 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
393 * WE DO NOT USE IT ANY MORE! It's only left here for historical
394 * reasons and because gdb uses it as a signature to notice
395 * signal handler stack frames.
397 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
398 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
399 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
404 /* Set up registers for signal handler */
405 regs->sp = (unsigned long)frame;
406 regs->ip = (unsigned long)ka->sa.sa_handler;
407 regs->ax = (unsigned long)sig;
411 regs->ds = __USER_DS;
412 regs->es = __USER_DS;
413 regs->ss = __USER_DS;
414 regs->cs = __USER_CS;
419 force_sigsegv(sig, current);
423 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
424 sigset_t *set, struct pt_regs *regs)
426 struct rt_sigframe __user *frame;
427 void __user *restorer;
431 frame = get_sigframe(ka, regs, sizeof(*frame));
433 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
436 usig = current_thread_info()->exec_domain
437 && current_thread_info()->exec_domain->signal_invmap
439 ? current_thread_info()->exec_domain->signal_invmap[sig]
442 err |= __put_user(usig, &frame->sig);
443 err |= __put_user(&frame->info, &frame->pinfo);
444 err |= __put_user(&frame->uc, &frame->puc);
445 err |= copy_siginfo_to_user(&frame->info, info);
449 /* Create the ucontext. */
450 err |= __put_user(0, &frame->uc.uc_flags);
451 err |= __put_user(0, &frame->uc.uc_link);
452 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
453 err |= __put_user(sas_ss_flags(regs->sp),
454 &frame->uc.uc_stack.ss_flags);
455 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
456 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
458 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
462 /* Set up to return from userspace. */
463 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
464 if (ka->sa.sa_flags & SA_RESTORER)
465 restorer = ka->sa.sa_restorer;
466 err |= __put_user(restorer, &frame->pretcode);
469 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
471 * WE DO NOT USE IT ANY MORE! It's only left here for historical
472 * reasons and because gdb uses it as a signature to notice
473 * signal handler stack frames.
475 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
476 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
477 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
482 /* Set up registers for signal handler */
483 regs->sp = (unsigned long)frame;
484 regs->ip = (unsigned long)ka->sa.sa_handler;
485 regs->ax = (unsigned long)usig;
486 regs->dx = (unsigned long)&frame->info;
487 regs->cx = (unsigned long)&frame->uc;
489 regs->ds = __USER_DS;
490 regs->es = __USER_DS;
491 regs->ss = __USER_DS;
492 regs->cs = __USER_CS;
497 force_sigsegv(sig, current);
502 * OK, we're invoking a handler:
505 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
506 sigset_t *oldset, struct pt_regs *regs)
510 /* Are we from a system call? */
511 if ((long)regs->orig_ax >= 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->ax = regs->orig_ax;
533 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
534 * flag so that register information in the sigcontext is correct.
536 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
537 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
538 regs->flags &= ~X86_EFLAGS_TF;
540 /* Set up the stack frame */
541 if (ka->sa.sa_flags & SA_SIGINFO)
542 ret = setup_rt_frame(sig, ka, info, oldset, regs);
544 ret = setup_frame(sig, ka, oldset, regs);
550 * Clear the direction flag as per the ABI for function entry.
552 regs->flags &= ~X86_EFLAGS_DF;
555 * Clear TF when entering the signal handler, but
556 * notify any tracer that was single-stepping it.
557 * The tracer may want to single-step inside the
560 regs->flags &= ~X86_EFLAGS_TF;
561 if (test_thread_flag(TIF_SINGLESTEP))
562 ptrace_notify(SIGTRAP);
564 spin_lock_irq(¤t->sighand->siglock);
565 sigorsets(¤t->blocked, ¤t->blocked, &ka->sa.sa_mask);
566 if (!(ka->sa.sa_flags & SA_NODEFER))
567 sigaddset(¤t->blocked, sig);
569 spin_unlock_irq(¤t->sighand->siglock);
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 /* Pending single-step? */
661 if (thread_info_flags & _TIF_SINGLESTEP) {
662 regs->flags |= X86_EFLAGS_TF;
663 clear_thread_flag(TIF_SINGLESTEP);
666 /* deal with pending signal delivery */
667 if (thread_info_flags & _TIF_SIGPENDING)
670 if (thread_info_flags & _TIF_HRTICK_RESCHED)
673 clear_thread_flag(TIF_IRET);