2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
7 * 2000-2002 x86-64 support by Andi Kleen
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/tracehook.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/uaccess.h>
23 #include <linux/user-return-notifier.h>
24 #include <linux/uprobes.h>
25 #include <linux/context_tracking.h>
27 #include <asm/processor.h>
28 #include <asm/ucontext.h>
30 #include <asm/fpu-internal.h>
33 #include <asm/sighandling.h>
36 #include <asm/proto.h>
37 #include <asm/ia32_unistd.h>
38 #include <asm/sys_ia32.h>
39 #endif /* CONFIG_X86_64 */
41 #include <asm/syscall.h>
42 #include <asm/syscalls.h>
44 #include <asm/sigframe.h>
46 #define COPY(x) do { \
47 get_user_ex(regs->x, &sc->x); \
50 #define GET_SEG(seg) ({ \
52 get_user_ex(tmp, &sc->seg); \
56 #define COPY_SEG(seg) do { \
57 regs->seg = GET_SEG(seg); \
60 #define COPY_SEG_CPL3(seg) do { \
61 regs->seg = GET_SEG(seg) | 3; \
64 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
68 unsigned int tmpflags;
71 /* Always make any pending restarted system calls return -EINTR */
72 current_thread_info()->restart_block.fn = do_no_restart_syscall;
77 set_user_gs(regs, GET_SEG(gs));
81 #endif /* CONFIG_X86_32 */
83 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
84 COPY(dx); COPY(cx); COPY(ip);
95 #endif /* CONFIG_X86_64 */
100 #else /* !CONFIG_X86_32 */
101 /* Kernel saves and restores only the CS segment register on signals,
102 * which is the bare minimum needed to allow mixed 32/64-bit code.
103 * App's signal handler can save/restore other segments if needed. */
105 #endif /* CONFIG_X86_32 */
107 get_user_ex(tmpflags, &sc->flags);
108 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
109 regs->orig_ax = -1; /* disable syscall checks */
111 get_user_ex(buf, &sc->fpstate);
113 get_user_ex(*pax, &sc->ax);
114 } get_user_catch(err);
116 err |= restore_xstate_sig(buf, config_enabled(CONFIG_X86_32));
121 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
122 struct pt_regs *regs, unsigned long mask)
129 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
130 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
131 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
132 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
133 #endif /* CONFIG_X86_32 */
135 put_user_ex(regs->di, &sc->di);
136 put_user_ex(regs->si, &sc->si);
137 put_user_ex(regs->bp, &sc->bp);
138 put_user_ex(regs->sp, &sc->sp);
139 put_user_ex(regs->bx, &sc->bx);
140 put_user_ex(regs->dx, &sc->dx);
141 put_user_ex(regs->cx, &sc->cx);
142 put_user_ex(regs->ax, &sc->ax);
144 put_user_ex(regs->r8, &sc->r8);
145 put_user_ex(regs->r9, &sc->r9);
146 put_user_ex(regs->r10, &sc->r10);
147 put_user_ex(regs->r11, &sc->r11);
148 put_user_ex(regs->r12, &sc->r12);
149 put_user_ex(regs->r13, &sc->r13);
150 put_user_ex(regs->r14, &sc->r14);
151 put_user_ex(regs->r15, &sc->r15);
152 #endif /* CONFIG_X86_64 */
154 put_user_ex(current->thread.trap_nr, &sc->trapno);
155 put_user_ex(current->thread.error_code, &sc->err);
156 put_user_ex(regs->ip, &sc->ip);
158 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
159 put_user_ex(regs->flags, &sc->flags);
160 put_user_ex(regs->sp, &sc->sp_at_signal);
161 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
162 #else /* !CONFIG_X86_32 */
163 put_user_ex(regs->flags, &sc->flags);
164 put_user_ex(regs->cs, &sc->cs);
165 put_user_ex(0, &sc->gs);
166 put_user_ex(0, &sc->fs);
167 #endif /* CONFIG_X86_32 */
169 put_user_ex(fpstate, &sc->fpstate);
171 /* non-iBCS2 extensions.. */
172 put_user_ex(mask, &sc->oldmask);
173 put_user_ex(current->thread.cr2, &sc->cr2);
174 } put_user_catch(err);
180 * Set up a signal frame.
184 * Determine which stack to use..
186 static unsigned long align_sigframe(unsigned long sp)
190 * Align the stack pointer according to the i386 ABI,
191 * i.e. so that on function entry ((sp + 4) & 15) == 0.
193 sp = ((sp + 4) & -16ul) - 4;
194 #else /* !CONFIG_X86_32 */
195 sp = round_down(sp, 16) - 8;
200 static inline void __user *
201 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
202 void __user **fpstate)
204 /* Default to using normal stack */
205 unsigned long math_size = 0;
206 unsigned long sp = regs->sp;
207 unsigned long buf_fx = 0;
208 int onsigstack = on_sig_stack(sp);
211 if (config_enabled(CONFIG_X86_64))
215 /* This is the X/Open sanctioned signal stack switching. */
216 if (ka->sa.sa_flags & SA_ONSTACK) {
217 if (current->sas_ss_size)
218 sp = current->sas_ss_sp + current->sas_ss_size;
219 } else if (config_enabled(CONFIG_X86_32) &&
220 (regs->ss & 0xffff) != __USER_DS &&
221 !(ka->sa.sa_flags & SA_RESTORER) &&
222 ka->sa.sa_restorer) {
223 /* This is the legacy signal stack switching. */
224 sp = (unsigned long) ka->sa.sa_restorer;
229 sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
230 &buf_fx, &math_size);
231 *fpstate = (void __user *)sp;
234 sp = align_sigframe(sp - frame_size);
237 * If we are on the alternate signal stack and would overflow it, don't.
238 * Return an always-bogus address instead so we will die with SIGSEGV.
240 if (onsigstack && !likely(on_sig_stack(sp)))
241 return (void __user *)-1L;
243 /* save i387 and extended state */
245 save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
246 return (void __user *)-1L;
248 return (void __user *)sp;
252 static const struct {
256 } __attribute__((packed)) retcode = {
257 0xb858, /* popl %eax; movl $..., %eax */
259 0x80cd, /* int $0x80 */
262 static const struct {
267 } __attribute__((packed)) rt_retcode = {
268 0xb8, /* movl $..., %eax */
270 0x80cd, /* int $0x80 */
275 __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
276 struct pt_regs *regs)
278 struct sigframe __user *frame;
279 void __user *restorer;
281 void __user *fpstate = NULL;
283 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
285 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288 if (__put_user(sig, &frame->sig))
291 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
294 if (_NSIG_WORDS > 1) {
295 if (__copy_to_user(&frame->extramask, &set->sig[1],
296 sizeof(frame->extramask)))
300 if (current->mm->context.vdso)
301 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
303 restorer = &frame->retcode;
304 if (ksig->ka.sa.sa_flags & SA_RESTORER)
305 restorer = ksig->ka.sa.sa_restorer;
307 /* Set up to return from userspace. */
308 err |= __put_user(restorer, &frame->pretcode);
311 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
313 * WE DO NOT USE IT ANY MORE! It's only left here for historical
314 * reasons and because gdb uses it as a signature to notice
315 * signal handler stack frames.
317 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
322 /* Set up registers for signal handler */
323 regs->sp = (unsigned long)frame;
324 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
325 regs->ax = (unsigned long)sig;
329 regs->ds = __USER_DS;
330 regs->es = __USER_DS;
331 regs->ss = __USER_DS;
332 regs->cs = __USER_CS;
337 static int __setup_rt_frame(int sig, struct ksignal *ksig,
338 sigset_t *set, struct pt_regs *regs)
340 struct rt_sigframe __user *frame;
341 void __user *restorer;
343 void __user *fpstate = NULL;
345 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
347 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
351 put_user_ex(sig, &frame->sig);
352 put_user_ex(&frame->info, &frame->pinfo);
353 put_user_ex(&frame->uc, &frame->puc);
355 /* Create the ucontext. */
357 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
359 put_user_ex(0, &frame->uc.uc_flags);
360 put_user_ex(0, &frame->uc.uc_link);
361 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
363 /* Set up to return from userspace. */
364 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
365 if (ksig->ka.sa.sa_flags & SA_RESTORER)
366 restorer = ksig->ka.sa.sa_restorer;
367 put_user_ex(restorer, &frame->pretcode);
370 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
372 * WE DO NOT USE IT ANY MORE! It's only left here for historical
373 * reasons and because gdb uses it as a signature to notice
374 * signal handler stack frames.
376 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
377 } put_user_catch(err);
379 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
380 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
382 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
387 /* Set up registers for signal handler */
388 regs->sp = (unsigned long)frame;
389 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
390 regs->ax = (unsigned long)sig;
391 regs->dx = (unsigned long)&frame->info;
392 regs->cx = (unsigned long)&frame->uc;
394 regs->ds = __USER_DS;
395 regs->es = __USER_DS;
396 regs->ss = __USER_DS;
397 regs->cs = __USER_CS;
401 #else /* !CONFIG_X86_32 */
402 static int __setup_rt_frame(int sig, struct ksignal *ksig,
403 sigset_t *set, struct pt_regs *regs)
405 struct rt_sigframe __user *frame;
406 void __user *fp = NULL;
409 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
411 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
414 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
415 if (copy_siginfo_to_user(&frame->info, &ksig->info))
420 /* Create the ucontext. */
422 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
424 put_user_ex(0, &frame->uc.uc_flags);
425 put_user_ex(0, &frame->uc.uc_link);
426 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
428 /* Set up to return from userspace. If provided, use a stub
429 already in userspace. */
430 /* x86-64 should always use SA_RESTORER. */
431 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
432 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
434 /* could use a vstub here */
437 } put_user_catch(err);
439 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
440 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
445 /* Set up registers for signal handler */
447 /* In case the signal handler was declared without prototypes */
450 /* This also works for non SA_SIGINFO handlers because they expect the
451 next argument after the signal number on the stack. */
452 regs->si = (unsigned long)&frame->info;
453 regs->dx = (unsigned long)&frame->uc;
454 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
456 regs->sp = (unsigned long)frame;
458 /* Set up the CS register to run signal handlers in 64-bit mode,
459 even if the handler happens to be interrupting 32-bit code. */
460 regs->cs = __USER_CS;
464 #endif /* CONFIG_X86_32 */
466 static int x32_setup_rt_frame(struct ksignal *ksig,
467 compat_sigset_t *set,
468 struct pt_regs *regs)
470 #ifdef CONFIG_X86_X32_ABI
471 struct rt_sigframe_x32 __user *frame;
472 void __user *restorer;
474 void __user *fpstate = NULL;
476 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
478 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
481 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
482 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
487 /* Create the ucontext. */
489 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
491 put_user_ex(0, &frame->uc.uc_flags);
492 put_user_ex(0, &frame->uc.uc_link);
493 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
494 put_user_ex(0, &frame->uc.uc__pad0);
496 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
497 restorer = ksig->ka.sa.sa_restorer;
499 /* could use a vstub here */
503 put_user_ex(restorer, &frame->pretcode);
504 } put_user_catch(err);
506 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
508 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
513 /* Set up registers for signal handler */
514 regs->sp = (unsigned long) frame;
515 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
517 /* We use the x32 calling convention here... */
518 regs->di = ksig->sig;
519 regs->si = (unsigned long) &frame->info;
520 regs->dx = (unsigned long) &frame->uc;
522 loadsegment(ds, __USER_DS);
523 loadsegment(es, __USER_DS);
525 regs->cs = __USER_CS;
526 regs->ss = __USER_DS;
527 #endif /* CONFIG_X86_X32_ABI */
533 * Do a signal return; undo the signal stack.
536 asmlinkage unsigned long sys_sigreturn(void)
538 struct pt_regs *regs = current_pt_regs();
539 struct sigframe __user *frame;
543 frame = (struct sigframe __user *)(regs->sp - 8);
545 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
547 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
548 && __copy_from_user(&set.sig[1], &frame->extramask,
549 sizeof(frame->extramask))))
552 set_current_blocked(&set);
554 if (restore_sigcontext(regs, &frame->sc, &ax))
559 signal_fault(regs, frame, "sigreturn");
563 #endif /* CONFIG_X86_32 */
565 asmlinkage long sys_rt_sigreturn(void)
567 struct pt_regs *regs = current_pt_regs();
568 struct rt_sigframe __user *frame;
572 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
573 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
575 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
578 set_current_blocked(&set);
580 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
583 if (restore_altstack(&frame->uc.uc_stack))
589 signal_fault(regs, frame, "rt_sigreturn");
594 * OK, we're invoking a handler:
596 static int signr_convert(int sig)
599 struct thread_info *info = current_thread_info();
601 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
602 return info->exec_domain->signal_invmap[sig];
603 #endif /* CONFIG_X86_32 */
608 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
610 int usig = signr_convert(ksig->sig);
611 sigset_t *set = sigmask_to_save();
612 compat_sigset_t *cset = (compat_sigset_t *) set;
614 /* Set up the stack frame */
615 if (is_ia32_frame()) {
616 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
617 return ia32_setup_rt_frame(usig, ksig, cset, regs);
619 return ia32_setup_frame(usig, ksig, cset, regs);
620 } else if (is_x32_frame()) {
621 return x32_setup_rt_frame(ksig, cset, regs);
623 return __setup_rt_frame(ksig->sig, ksig, set, regs);
628 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
631 /* Are we from a system call? */
632 if (syscall_get_nr(current, regs) >= 0) {
633 /* If so, check system call restarting.. */
634 switch (syscall_get_error(current, regs)) {
635 case -ERESTART_RESTARTBLOCK:
636 case -ERESTARTNOHAND:
641 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
646 case -ERESTARTNOINTR:
647 regs->ax = regs->orig_ax;
654 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
655 * flag so that register information in the sigcontext is correct.
657 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
658 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
659 regs->flags &= ~X86_EFLAGS_TF;
661 failed = (setup_rt_frame(ksig, regs) < 0);
664 * Clear the direction flag as per the ABI for function entry.
666 * Clear RF when entering the signal handler, because
667 * it might disable possible debug exception from the
670 * Clear TF when entering the signal handler, but
671 * notify any tracer that was single-stepping it.
672 * The tracer may want to single-step inside the
675 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
677 signal_setup_done(failed, ksig, test_thread_flag(TIF_SINGLESTEP));
681 #define NR_restart_syscall __NR_restart_syscall
682 #else /* !CONFIG_X86_32 */
683 #define NR_restart_syscall \
684 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
685 #endif /* CONFIG_X86_32 */
688 * Note that 'init' is a special process: it doesn't get signals it doesn't
689 * want to handle. Thus you cannot kill init even with a SIGKILL even by
692 static void do_signal(struct pt_regs *regs)
696 if (get_signal(&ksig)) {
697 /* Whee! Actually deliver the signal. */
698 handle_signal(&ksig, regs);
702 /* Did we come from a system call? */
703 if (syscall_get_nr(current, regs) >= 0) {
704 /* Restart the system call - no handlers present */
705 switch (syscall_get_error(current, regs)) {
706 case -ERESTARTNOHAND:
708 case -ERESTARTNOINTR:
709 regs->ax = regs->orig_ax;
713 case -ERESTART_RESTARTBLOCK:
714 regs->ax = NR_restart_syscall;
721 * If there's no signal to deliver, we just put the saved sigmask
724 restore_saved_sigmask();
728 * notification of userspace execution resumption
729 * - triggered by the TIF_WORK_MASK flags
732 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
736 #ifdef CONFIG_X86_MCE
737 /* notify userspace of pending MCEs */
738 if (thread_info_flags & _TIF_MCE_NOTIFY)
739 mce_notify_process();
740 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
742 if (thread_info_flags & _TIF_UPROBE)
743 uprobe_notify_resume(regs);
745 /* deal with pending signal delivery */
746 if (thread_info_flags & _TIF_SIGPENDING)
749 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
750 clear_thread_flag(TIF_NOTIFY_RESUME);
751 tracehook_notify_resume(regs);
753 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
754 fire_user_return_notifiers();
759 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
761 struct task_struct *me = current;
763 if (show_unhandled_signals && printk_ratelimit()) {
765 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
766 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
767 me->comm, me->pid, where, frame,
768 regs->ip, regs->sp, regs->orig_ax);
769 print_vma_addr(" in ", regs->ip);
773 force_sig(SIGSEGV, me);
776 #ifdef CONFIG_X86_X32_ABI
777 asmlinkage long sys32_x32_rt_sigreturn(void)
779 struct pt_regs *regs = current_pt_regs();
780 struct rt_sigframe_x32 __user *frame;
784 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
786 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
788 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
791 set_current_blocked(&set);
793 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
796 if (compat_restore_altstack(&frame->uc.uc_stack))
802 signal_fault(regs, frame, "x32 rt_sigreturn");