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 */
164 struct _fpstate __user *buf;
166 err |= __get_user(buf, &sc->fpstate);
168 if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
170 err |= restore_i387(buf);
172 struct task_struct *me = current;
181 err |= __get_user(*pax, &sc->ax);
188 asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
190 struct sigframe __user *frame;
191 struct pt_regs *regs;
195 regs = (struct pt_regs *) &__unused;
196 frame = (struct sigframe __user *)(regs->sp - 8);
198 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
200 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
201 && __copy_from_user(&set.sig[1], &frame->extramask,
202 sizeof(frame->extramask))))
205 sigdelsetmask(&set, ~_BLOCKABLE);
206 spin_lock_irq(¤t->sighand->siglock);
207 current->blocked = set;
209 spin_unlock_irq(¤t->sighand->siglock);
211 if (restore_sigcontext(regs, &frame->sc, &ax))
216 if (show_unhandled_signals && printk_ratelimit()) {
217 printk("%s%s[%d] bad frame in sigreturn frame:"
218 "%p ip:%lx sp:%lx oeax:%lx",
219 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
220 current->comm, task_pid_nr(current), frame, regs->ip,
221 regs->sp, regs->orig_ax);
222 print_vma_addr(" in ", regs->ip);
223 printk(KERN_CONT "\n");
226 force_sig(SIGSEGV, current);
231 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
233 struct pt_regs *regs = (struct pt_regs *)&__unused;
234 struct rt_sigframe __user *frame;
238 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
239 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
241 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
244 sigdelsetmask(&set, ~_BLOCKABLE);
245 spin_lock_irq(¤t->sighand->siglock);
246 current->blocked = set;
248 spin_unlock_irq(¤t->sighand->siglock);
250 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
253 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
259 force_sig(SIGSEGV, current);
264 * Set up a signal frame.
267 setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
268 struct pt_regs *regs, unsigned long mask)
272 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
273 savesegment(gs, tmp);
274 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
276 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
277 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
278 err |= __put_user(regs->di, &sc->di);
279 err |= __put_user(regs->si, &sc->si);
280 err |= __put_user(regs->bp, &sc->bp);
281 err |= __put_user(regs->sp, &sc->sp);
282 err |= __put_user(regs->bx, &sc->bx);
283 err |= __put_user(regs->dx, &sc->dx);
284 err |= __put_user(regs->cx, &sc->cx);
285 err |= __put_user(regs->ax, &sc->ax);
286 err |= __put_user(current->thread.trap_no, &sc->trapno);
287 err |= __put_user(current->thread.error_code, &sc->err);
288 err |= __put_user(regs->ip, &sc->ip);
289 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
290 err |= __put_user(regs->flags, &sc->flags);
291 err |= __put_user(regs->sp, &sc->sp_at_signal);
292 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
294 tmp = save_i387(fpstate);
298 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
300 /* non-iBCS2 extensions.. */
301 err |= __put_user(mask, &sc->oldmask);
302 err |= __put_user(current->thread.cr2, &sc->cr2);
308 * Determine which stack to use..
310 static inline void __user *
311 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
315 /* Default to using normal stack */
319 * If we are on the alternate signal stack and would overflow it, don't.
320 * Return an always-bogus address instead so we will die with SIGSEGV.
322 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
323 return (void __user *) -1L;
325 /* This is the X/Open sanctioned signal stack switching. */
326 if (ka->sa.sa_flags & SA_ONSTACK) {
327 if (sas_ss_flags(sp) == 0)
328 sp = current->sas_ss_sp + current->sas_ss_size;
330 /* This is the legacy signal stack switching. */
331 if ((regs->ss & 0xffff) != __USER_DS &&
332 !(ka->sa.sa_flags & SA_RESTORER) &&
334 sp = (unsigned long) ka->sa.sa_restorer;
339 * Align the stack pointer according to the i386 ABI,
340 * i.e. so that on function entry ((sp + 4) & 15) == 0.
342 sp = ((sp + 4) & -16ul) - 4;
344 return (void __user *) sp;
348 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
349 struct pt_regs *regs)
351 struct sigframe __user *frame;
352 void __user *restorer;
356 frame = get_sigframe(ka, regs, sizeof(*frame));
358 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
361 usig = current_thread_info()->exec_domain
362 && current_thread_info()->exec_domain->signal_invmap
364 ? current_thread_info()->exec_domain->signal_invmap[sig]
367 err = __put_user(usig, &frame->sig);
371 err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
375 if (_NSIG_WORDS > 1) {
376 err = __copy_to_user(&frame->extramask, &set->sig[1],
377 sizeof(frame->extramask));
382 if (current->mm->context.vdso)
383 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
385 restorer = &frame->retcode;
386 if (ka->sa.sa_flags & SA_RESTORER)
387 restorer = ka->sa.sa_restorer;
389 /* Set up to return from userspace. */
390 err |= __put_user(restorer, &frame->pretcode);
393 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
395 * WE DO NOT USE IT ANY MORE! It's only left here for historical
396 * reasons and because gdb uses it as a signature to notice
397 * signal handler stack frames.
399 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
400 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
401 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
406 /* Set up registers for signal handler */
407 regs->sp = (unsigned long)frame;
408 regs->ip = (unsigned long)ka->sa.sa_handler;
409 regs->ax = (unsigned long)sig;
413 regs->ds = __USER_DS;
414 regs->es = __USER_DS;
415 regs->ss = __USER_DS;
416 regs->cs = __USER_CS;
421 force_sigsegv(sig, current);
425 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
426 sigset_t *set, struct pt_regs *regs)
428 struct rt_sigframe __user *frame;
429 void __user *restorer;
433 frame = get_sigframe(ka, regs, sizeof(*frame));
435 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
438 usig = current_thread_info()->exec_domain
439 && current_thread_info()->exec_domain->signal_invmap
441 ? current_thread_info()->exec_domain->signal_invmap[sig]
444 err |= __put_user(usig, &frame->sig);
445 err |= __put_user(&frame->info, &frame->pinfo);
446 err |= __put_user(&frame->uc, &frame->puc);
447 err |= copy_siginfo_to_user(&frame->info, info);
451 /* Create the ucontext. */
452 err |= __put_user(0, &frame->uc.uc_flags);
453 err |= __put_user(0, &frame->uc.uc_link);
454 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
455 err |= __put_user(sas_ss_flags(regs->sp),
456 &frame->uc.uc_stack.ss_flags);
457 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
458 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
460 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
464 /* Set up to return from userspace. */
465 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
466 if (ka->sa.sa_flags & SA_RESTORER)
467 restorer = ka->sa.sa_restorer;
468 err |= __put_user(restorer, &frame->pretcode);
471 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
473 * WE DO NOT USE IT ANY MORE! It's only left here for historical
474 * reasons and because gdb uses it as a signature to notice
475 * signal handler stack frames.
477 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
478 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
479 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
484 /* Set up registers for signal handler */
485 regs->sp = (unsigned long)frame;
486 regs->ip = (unsigned long)ka->sa.sa_handler;
487 regs->ax = (unsigned long)usig;
488 regs->dx = (unsigned long)&frame->info;
489 regs->cx = (unsigned long)&frame->uc;
491 regs->ds = __USER_DS;
492 regs->es = __USER_DS;
493 regs->ss = __USER_DS;
494 regs->cs = __USER_CS;
499 force_sigsegv(sig, current);
504 * OK, we're invoking a handler:
507 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
508 sigset_t *oldset, struct pt_regs *regs)
512 /* Are we from a system call? */
513 if ((long)regs->orig_ax >= 0) {
514 /* If so, check system call restarting.. */
516 case -ERESTART_RESTARTBLOCK:
517 case -ERESTARTNOHAND:
522 if (!(ka->sa.sa_flags & SA_RESTART)) {
527 case -ERESTARTNOINTR:
528 regs->ax = regs->orig_ax;
535 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
536 * flag so that register information in the sigcontext is correct.
538 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
539 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
540 regs->flags &= ~X86_EFLAGS_TF;
542 /* Set up the stack frame */
543 if (ka->sa.sa_flags & SA_SIGINFO)
544 ret = setup_rt_frame(sig, ka, info, oldset, regs);
546 ret = setup_frame(sig, ka, oldset, regs);
552 * Clear the direction flag as per the ABI for function entry.
554 regs->flags &= ~X86_EFLAGS_DF;
557 * Clear TF when entering the signal handler, but
558 * notify any tracer that was single-stepping it.
559 * The tracer may want to single-step inside the
562 regs->flags &= ~X86_EFLAGS_TF;
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);
571 tracehook_signal_handler(sig, info, ka, regs,
572 test_thread_flag(TIF_SINGLESTEP));
578 * Note that 'init' is a special process: it doesn't get signals it doesn't
579 * want to handle. Thus you cannot kill init even with a SIGKILL even by
582 static void do_signal(struct pt_regs *regs)
584 struct k_sigaction ka;
590 * We want the common case to go fast, which is why we may in certain
591 * cases get here from kernel mode. Just return without doing anything
593 * X86_32: vm86 regs switched out by assembly code before reaching
594 * here, so testing against kernel CS suffices.
596 if (!user_mode(regs))
599 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
600 oldset = ¤t->saved_sigmask;
602 oldset = ¤t->blocked;
604 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) {
618 * A signal was successfully delivered; the saved
619 * sigmask will have been stored in the signal frame,
620 * and will be restored by sigreturn, so we can simply
621 * clear the TS_RESTORE_SIGMASK flag.
623 current_thread_info()->status &= ~TS_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 (current_thread_info()->status & TS_RESTORE_SIGMASK) {
651 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
652 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
657 * notification of userspace execution resumption
658 * - triggered by the TIF_WORK_MASK flags
661 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
663 /* deal with pending signal delivery */
664 if (thread_info_flags & _TIF_SIGPENDING)
667 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
668 clear_thread_flag(TIF_NOTIFY_RESUME);
669 tracehook_notify_resume(regs);
672 clear_thread_flag(TIF_IRET);