ARM: mmp: fix potential NULL dereference
[linux/fpc-iii.git] / arch / x86 / kernel / signal.c
blobb280908a376e20efc6773b76c6e80353ef8680f4
1 /*
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
8 */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/sched.h>
13 #include <linux/mm.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>
26 #include <asm/processor.h>
27 #include <asm/ucontext.h>
28 #include <asm/i387.h>
29 #include <asm/fpu-internal.h>
30 #include <asm/vdso.h>
31 #include <asm/mce.h>
32 #include <asm/sighandling.h>
34 #ifdef CONFIG_X86_64
35 #include <asm/proto.h>
36 #include <asm/ia32_unistd.h>
37 #include <asm/sys_ia32.h>
38 #endif /* CONFIG_X86_64 */
40 #include <asm/syscall.h>
41 #include <asm/syscalls.h>
43 #include <asm/sigframe.h>
45 #ifdef CONFIG_X86_32
46 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
47 #else
48 # define FIX_EFLAGS __FIX_EFLAGS
49 #endif
51 #define COPY(x) do { \
52 get_user_ex(regs->x, &sc->x); \
53 } while (0)
55 #define GET_SEG(seg) ({ \
56 unsigned short tmp; \
57 get_user_ex(tmp, &sc->seg); \
58 tmp; \
61 #define COPY_SEG(seg) do { \
62 regs->seg = GET_SEG(seg); \
63 } while (0)
65 #define COPY_SEG_CPL3(seg) do { \
66 regs->seg = GET_SEG(seg) | 3; \
67 } while (0)
69 int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
70 unsigned long *pax)
72 void __user *buf;
73 unsigned int tmpflags;
74 unsigned int err = 0;
76 /* Always make any pending restarted system calls return -EINTR */
77 current_thread_info()->restart_block.fn = do_no_restart_syscall;
79 get_user_try {
81 #ifdef CONFIG_X86_32
82 set_user_gs(regs, GET_SEG(gs));
83 COPY_SEG(fs);
84 COPY_SEG(es);
85 COPY_SEG(ds);
86 #endif /* CONFIG_X86_32 */
88 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
89 COPY(dx); COPY(cx); COPY(ip);
91 #ifdef CONFIG_X86_64
92 COPY(r8);
93 COPY(r9);
94 COPY(r10);
95 COPY(r11);
96 COPY(r12);
97 COPY(r13);
98 COPY(r14);
99 COPY(r15);
100 #endif /* CONFIG_X86_64 */
102 #ifdef CONFIG_X86_32
103 COPY_SEG_CPL3(cs);
104 COPY_SEG_CPL3(ss);
105 #else /* !CONFIG_X86_32 */
106 /* Kernel saves and restores only the CS segment register on signals,
107 * which is the bare minimum needed to allow mixed 32/64-bit code.
108 * App's signal handler can save/restore other segments if needed. */
109 COPY_SEG_CPL3(cs);
110 #endif /* CONFIG_X86_32 */
112 get_user_ex(tmpflags, &sc->flags);
113 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
114 regs->orig_ax = -1; /* disable syscall checks */
116 get_user_ex(buf, &sc->fpstate);
117 err |= restore_i387_xstate(buf);
119 get_user_ex(*pax, &sc->ax);
120 } get_user_catch(err);
122 return err;
125 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
126 struct pt_regs *regs, unsigned long mask)
128 int err = 0;
130 put_user_try {
132 #ifdef CONFIG_X86_32
133 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
134 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
135 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
136 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
137 #endif /* CONFIG_X86_32 */
139 put_user_ex(regs->di, &sc->di);
140 put_user_ex(regs->si, &sc->si);
141 put_user_ex(regs->bp, &sc->bp);
142 put_user_ex(regs->sp, &sc->sp);
143 put_user_ex(regs->bx, &sc->bx);
144 put_user_ex(regs->dx, &sc->dx);
145 put_user_ex(regs->cx, &sc->cx);
146 put_user_ex(regs->ax, &sc->ax);
147 #ifdef CONFIG_X86_64
148 put_user_ex(regs->r8, &sc->r8);
149 put_user_ex(regs->r9, &sc->r9);
150 put_user_ex(regs->r10, &sc->r10);
151 put_user_ex(regs->r11, &sc->r11);
152 put_user_ex(regs->r12, &sc->r12);
153 put_user_ex(regs->r13, &sc->r13);
154 put_user_ex(regs->r14, &sc->r14);
155 put_user_ex(regs->r15, &sc->r15);
156 #endif /* CONFIG_X86_64 */
158 put_user_ex(current->thread.trap_nr, &sc->trapno);
159 put_user_ex(current->thread.error_code, &sc->err);
160 put_user_ex(regs->ip, &sc->ip);
161 #ifdef CONFIG_X86_32
162 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
163 put_user_ex(regs->flags, &sc->flags);
164 put_user_ex(regs->sp, &sc->sp_at_signal);
165 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
166 #else /* !CONFIG_X86_32 */
167 put_user_ex(regs->flags, &sc->flags);
168 put_user_ex(regs->cs, &sc->cs);
169 put_user_ex(0, &sc->gs);
170 put_user_ex(0, &sc->fs);
171 #endif /* CONFIG_X86_32 */
173 put_user_ex(fpstate, &sc->fpstate);
175 /* non-iBCS2 extensions.. */
176 put_user_ex(mask, &sc->oldmask);
177 put_user_ex(current->thread.cr2, &sc->cr2);
178 } put_user_catch(err);
180 return err;
184 * Set up a signal frame.
188 * Determine which stack to use..
190 static unsigned long align_sigframe(unsigned long sp)
192 #ifdef CONFIG_X86_32
194 * Align the stack pointer according to the i386 ABI,
195 * i.e. so that on function entry ((sp + 4) & 15) == 0.
197 sp = ((sp + 4) & -16ul) - 4;
198 #else /* !CONFIG_X86_32 */
199 sp = round_down(sp, 16) - 8;
200 #endif
201 return sp;
204 static inline void __user *
205 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
206 void __user **fpstate)
208 /* Default to using normal stack */
209 unsigned long sp = regs->sp;
210 int onsigstack = on_sig_stack(sp);
212 #ifdef CONFIG_X86_64
213 /* redzone */
214 sp -= 128;
215 #endif /* CONFIG_X86_64 */
217 if (!onsigstack) {
218 /* This is the X/Open sanctioned signal stack switching. */
219 if (ka->sa.sa_flags & SA_ONSTACK) {
220 if (current->sas_ss_size)
221 sp = current->sas_ss_sp + current->sas_ss_size;
222 } else {
223 #ifdef CONFIG_X86_32
224 /* This is the legacy signal stack switching. */
225 if ((regs->ss & 0xffff) != __USER_DS &&
226 !(ka->sa.sa_flags & SA_RESTORER) &&
227 ka->sa.sa_restorer)
228 sp = (unsigned long) ka->sa.sa_restorer;
229 #endif /* CONFIG_X86_32 */
233 if (used_math()) {
234 sp -= sig_xstate_size;
235 #ifdef CONFIG_X86_64
236 sp = round_down(sp, 64);
237 #endif /* CONFIG_X86_64 */
238 *fpstate = (void __user *)sp;
241 sp = align_sigframe(sp - frame_size);
244 * If we are on the alternate signal stack and would overflow it, don't.
245 * Return an always-bogus address instead so we will die with SIGSEGV.
247 if (onsigstack && !likely(on_sig_stack(sp)))
248 return (void __user *)-1L;
250 /* save i387 state */
251 if (used_math() && save_i387_xstate(*fpstate) < 0)
252 return (void __user *)-1L;
254 return (void __user *)sp;
257 #ifdef CONFIG_X86_32
258 static const struct {
259 u16 poplmovl;
260 u32 val;
261 u16 int80;
262 } __attribute__((packed)) retcode = {
263 0xb858, /* popl %eax; movl $..., %eax */
264 __NR_sigreturn,
265 0x80cd, /* int $0x80 */
268 static const struct {
269 u8 movl;
270 u32 val;
271 u16 int80;
272 u8 pad;
273 } __attribute__((packed)) rt_retcode = {
274 0xb8, /* movl $..., %eax */
275 __NR_rt_sigreturn,
276 0x80cd, /* int $0x80 */
280 static int
281 __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
282 struct pt_regs *regs)
284 struct sigframe __user *frame;
285 void __user *restorer;
286 int err = 0;
287 void __user *fpstate = NULL;
289 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
291 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
292 return -EFAULT;
294 if (__put_user(sig, &frame->sig))
295 return -EFAULT;
297 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
298 return -EFAULT;
300 if (_NSIG_WORDS > 1) {
301 if (__copy_to_user(&frame->extramask, &set->sig[1],
302 sizeof(frame->extramask)))
303 return -EFAULT;
306 if (current->mm->context.vdso)
307 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
308 else
309 restorer = &frame->retcode;
310 if (ka->sa.sa_flags & SA_RESTORER)
311 restorer = ka->sa.sa_restorer;
313 /* Set up to return from userspace. */
314 err |= __put_user(restorer, &frame->pretcode);
317 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
319 * WE DO NOT USE IT ANY MORE! It's only left here for historical
320 * reasons and because gdb uses it as a signature to notice
321 * signal handler stack frames.
323 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
325 if (err)
326 return -EFAULT;
328 /* Set up registers for signal handler */
329 regs->sp = (unsigned long)frame;
330 regs->ip = (unsigned long)ka->sa.sa_handler;
331 regs->ax = (unsigned long)sig;
332 regs->dx = 0;
333 regs->cx = 0;
335 regs->ds = __USER_DS;
336 regs->es = __USER_DS;
337 regs->ss = __USER_DS;
338 regs->cs = __USER_CS;
340 return 0;
343 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
344 sigset_t *set, struct pt_regs *regs)
346 struct rt_sigframe __user *frame;
347 void __user *restorer;
348 int err = 0;
349 void __user *fpstate = NULL;
351 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
353 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
354 return -EFAULT;
356 put_user_try {
357 put_user_ex(sig, &frame->sig);
358 put_user_ex(&frame->info, &frame->pinfo);
359 put_user_ex(&frame->uc, &frame->puc);
360 err |= copy_siginfo_to_user(&frame->info, info);
362 /* Create the ucontext. */
363 if (cpu_has_xsave)
364 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
365 else
366 put_user_ex(0, &frame->uc.uc_flags);
367 put_user_ex(0, &frame->uc.uc_link);
368 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
369 put_user_ex(sas_ss_flags(regs->sp),
370 &frame->uc.uc_stack.ss_flags);
371 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
372 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
373 regs, set->sig[0]);
374 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
376 /* Set up to return from userspace. */
377 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
378 if (ka->sa.sa_flags & SA_RESTORER)
379 restorer = ka->sa.sa_restorer;
380 put_user_ex(restorer, &frame->pretcode);
383 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
385 * WE DO NOT USE IT ANY MORE! It's only left here for historical
386 * reasons and because gdb uses it as a signature to notice
387 * signal handler stack frames.
389 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
390 } put_user_catch(err);
392 if (err)
393 return -EFAULT;
395 /* Set up registers for signal handler */
396 regs->sp = (unsigned long)frame;
397 regs->ip = (unsigned long)ka->sa.sa_handler;
398 regs->ax = (unsigned long)sig;
399 regs->dx = (unsigned long)&frame->info;
400 regs->cx = (unsigned long)&frame->uc;
402 regs->ds = __USER_DS;
403 regs->es = __USER_DS;
404 regs->ss = __USER_DS;
405 regs->cs = __USER_CS;
407 return 0;
409 #else /* !CONFIG_X86_32 */
410 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
411 sigset_t *set, struct pt_regs *regs)
413 struct rt_sigframe __user *frame;
414 void __user *fp = NULL;
415 int err = 0;
416 struct task_struct *me = current;
418 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
420 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
421 return -EFAULT;
423 if (ka->sa.sa_flags & SA_SIGINFO) {
424 if (copy_siginfo_to_user(&frame->info, info))
425 return -EFAULT;
428 put_user_try {
429 /* Create the ucontext. */
430 if (cpu_has_xsave)
431 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
432 else
433 put_user_ex(0, &frame->uc.uc_flags);
434 put_user_ex(0, &frame->uc.uc_link);
435 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
436 put_user_ex(sas_ss_flags(regs->sp),
437 &frame->uc.uc_stack.ss_flags);
438 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
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));
442 /* Set up to return from userspace. If provided, use a stub
443 already in userspace. */
444 /* x86-64 should always use SA_RESTORER. */
445 if (ka->sa.sa_flags & SA_RESTORER) {
446 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
447 } else {
448 /* could use a vstub here */
449 err |= -EFAULT;
451 } put_user_catch(err);
453 if (err)
454 return -EFAULT;
456 /* Set up registers for signal handler */
457 regs->di = sig;
458 /* In case the signal handler was declared without prototypes */
459 regs->ax = 0;
461 /* This also works for non SA_SIGINFO handlers because they expect the
462 next argument after the signal number on the stack. */
463 regs->si = (unsigned long)&frame->info;
464 regs->dx = (unsigned long)&frame->uc;
465 regs->ip = (unsigned long) ka->sa.sa_handler;
467 regs->sp = (unsigned long)frame;
469 /* Set up the CS register to run signal handlers in 64-bit mode,
470 even if the handler happens to be interrupting 32-bit code. */
471 regs->cs = __USER_CS;
473 return 0;
475 #endif /* CONFIG_X86_32 */
477 #ifdef CONFIG_X86_32
479 * Atomically swap in the new signal mask, and wait for a signal.
481 asmlinkage int
482 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
484 sigset_t blocked;
485 siginitset(&blocked, mask);
486 return sigsuspend(&blocked);
489 asmlinkage int
490 sys_sigaction(int sig, const struct old_sigaction __user *act,
491 struct old_sigaction __user *oact)
493 struct k_sigaction new_ka, old_ka;
494 int ret = 0;
496 if (act) {
497 old_sigset_t mask;
499 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
500 return -EFAULT;
502 get_user_try {
503 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
504 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
505 get_user_ex(mask, &act->sa_mask);
506 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
507 } get_user_catch(ret);
509 if (ret)
510 return -EFAULT;
511 siginitset(&new_ka.sa.sa_mask, mask);
514 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
516 if (!ret && oact) {
517 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
518 return -EFAULT;
520 put_user_try {
521 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
522 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
523 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
524 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
525 } put_user_catch(ret);
527 if (ret)
528 return -EFAULT;
531 return ret;
533 #endif /* CONFIG_X86_32 */
535 long
536 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
537 struct pt_regs *regs)
539 return do_sigaltstack(uss, uoss, regs->sp);
543 * Do a signal return; undo the signal stack.
545 #ifdef CONFIG_X86_32
546 unsigned long sys_sigreturn(struct pt_regs *regs)
548 struct sigframe __user *frame;
549 unsigned long ax;
550 sigset_t set;
552 frame = (struct sigframe __user *)(regs->sp - 8);
554 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
555 goto badframe;
556 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
557 && __copy_from_user(&set.sig[1], &frame->extramask,
558 sizeof(frame->extramask))))
559 goto badframe;
561 set_current_blocked(&set);
563 if (restore_sigcontext(regs, &frame->sc, &ax))
564 goto badframe;
565 return ax;
567 badframe:
568 signal_fault(regs, frame, "sigreturn");
570 return 0;
572 #endif /* CONFIG_X86_32 */
574 long sys_rt_sigreturn(struct pt_regs *regs)
576 struct rt_sigframe __user *frame;
577 unsigned long ax;
578 sigset_t set;
580 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
581 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
582 goto badframe;
583 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
584 goto badframe;
586 set_current_blocked(&set);
588 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
589 goto badframe;
591 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
592 goto badframe;
594 return ax;
596 badframe:
597 signal_fault(regs, frame, "rt_sigreturn");
598 return 0;
602 * OK, we're invoking a handler:
604 static int signr_convert(int sig)
606 #ifdef CONFIG_X86_32
607 struct thread_info *info = current_thread_info();
609 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
610 return info->exec_domain->signal_invmap[sig];
611 #endif /* CONFIG_X86_32 */
612 return sig;
615 #ifdef CONFIG_X86_32
617 #define is_ia32 1
618 #define ia32_setup_frame __setup_frame
619 #define ia32_setup_rt_frame __setup_rt_frame
621 #else /* !CONFIG_X86_32 */
623 #ifdef CONFIG_IA32_EMULATION
624 #define is_ia32 test_thread_flag(TIF_IA32)
625 #else /* !CONFIG_IA32_EMULATION */
626 #define is_ia32 0
627 #endif /* CONFIG_IA32_EMULATION */
629 #ifdef CONFIG_X86_X32_ABI
630 #define is_x32 test_thread_flag(TIF_X32)
632 static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
633 siginfo_t *info, compat_sigset_t *set,
634 struct pt_regs *regs);
635 #else /* !CONFIG_X86_X32_ABI */
636 #define is_x32 0
637 #endif /* CONFIG_X86_X32_ABI */
639 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
640 sigset_t *set, struct pt_regs *regs);
641 int ia32_setup_frame(int sig, struct k_sigaction *ka,
642 sigset_t *set, struct pt_regs *regs);
644 #endif /* CONFIG_X86_32 */
646 static int
647 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
648 struct pt_regs *regs)
650 int usig = signr_convert(sig);
651 sigset_t *set = sigmask_to_save();
653 /* Set up the stack frame */
654 if (is_ia32) {
655 if (ka->sa.sa_flags & SA_SIGINFO)
656 return ia32_setup_rt_frame(usig, ka, info, set, regs);
657 else
658 return ia32_setup_frame(usig, ka, set, regs);
659 #ifdef CONFIG_X86_X32_ABI
660 } else if (is_x32) {
661 return x32_setup_rt_frame(usig, ka, info,
662 (compat_sigset_t *)set, regs);
663 #endif
664 } else {
665 return __setup_rt_frame(sig, ka, info, set, regs);
669 static void
670 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
671 struct pt_regs *regs)
673 /* Are we from a system call? */
674 if (syscall_get_nr(current, regs) >= 0) {
675 /* If so, check system call restarting.. */
676 switch (syscall_get_error(current, regs)) {
677 case -ERESTART_RESTARTBLOCK:
678 case -ERESTARTNOHAND:
679 regs->ax = -EINTR;
680 break;
682 case -ERESTARTSYS:
683 if (!(ka->sa.sa_flags & SA_RESTART)) {
684 regs->ax = -EINTR;
685 break;
687 /* fallthrough */
688 case -ERESTARTNOINTR:
689 regs->ax = regs->orig_ax;
690 regs->ip -= 2;
691 break;
696 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
697 * flag so that register information in the sigcontext is correct.
699 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
700 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
701 regs->flags &= ~X86_EFLAGS_TF;
703 if (setup_rt_frame(sig, ka, info, regs) < 0) {
704 force_sigsegv(sig, current);
705 return;
709 * Clear the direction flag as per the ABI for function entry.
711 regs->flags &= ~X86_EFLAGS_DF;
714 * Clear TF when entering the signal handler, but
715 * notify any tracer that was single-stepping it.
716 * The tracer may want to single-step inside the
717 * handler too.
719 regs->flags &= ~X86_EFLAGS_TF;
721 signal_delivered(sig, info, ka, regs,
722 test_thread_flag(TIF_SINGLESTEP));
725 #ifdef CONFIG_X86_32
726 #define NR_restart_syscall __NR_restart_syscall
727 #else /* !CONFIG_X86_32 */
728 #define NR_restart_syscall \
729 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
730 #endif /* CONFIG_X86_32 */
733 * Note that 'init' is a special process: it doesn't get signals it doesn't
734 * want to handle. Thus you cannot kill init even with a SIGKILL even by
735 * mistake.
737 static void do_signal(struct pt_regs *regs)
739 struct k_sigaction ka;
740 siginfo_t info;
741 int signr;
743 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
744 if (signr > 0) {
745 /* Whee! Actually deliver the signal. */
746 handle_signal(signr, &info, &ka, regs);
747 return;
750 /* Did we come from a system call? */
751 if (syscall_get_nr(current, regs) >= 0) {
752 /* Restart the system call - no handlers present */
753 switch (syscall_get_error(current, regs)) {
754 case -ERESTARTNOHAND:
755 case -ERESTARTSYS:
756 case -ERESTARTNOINTR:
757 regs->ax = regs->orig_ax;
758 regs->ip -= 2;
759 break;
761 case -ERESTART_RESTARTBLOCK:
762 regs->ax = NR_restart_syscall;
763 regs->ip -= 2;
764 break;
769 * If there's no signal to deliver, we just put the saved sigmask
770 * back.
772 restore_saved_sigmask();
776 * notification of userspace execution resumption
777 * - triggered by the TIF_WORK_MASK flags
779 void
780 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
782 #ifdef CONFIG_X86_MCE
783 /* notify userspace of pending MCEs */
784 if (thread_info_flags & _TIF_MCE_NOTIFY)
785 mce_notify_process();
786 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
788 if (thread_info_flags & _TIF_UPROBE) {
789 clear_thread_flag(TIF_UPROBE);
790 uprobe_notify_resume(regs);
793 /* deal with pending signal delivery */
794 if (thread_info_flags & _TIF_SIGPENDING)
795 do_signal(regs);
797 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
798 clear_thread_flag(TIF_NOTIFY_RESUME);
799 tracehook_notify_resume(regs);
801 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
802 fire_user_return_notifiers();
804 #ifdef CONFIG_X86_32
805 clear_thread_flag(TIF_IRET);
806 #endif /* CONFIG_X86_32 */
809 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
811 struct task_struct *me = current;
813 if (show_unhandled_signals && printk_ratelimit()) {
814 printk("%s"
815 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
816 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
817 me->comm, me->pid, where, frame,
818 regs->ip, regs->sp, regs->orig_ax);
819 print_vma_addr(" in ", regs->ip);
820 pr_cont("\n");
823 force_sig(SIGSEGV, me);
826 #ifdef CONFIG_X86_X32_ABI
827 static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
828 siginfo_t *info, compat_sigset_t *set,
829 struct pt_regs *regs)
831 struct rt_sigframe_x32 __user *frame;
832 void __user *restorer;
833 int err = 0;
834 void __user *fpstate = NULL;
836 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
838 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
839 return -EFAULT;
841 if (ka->sa.sa_flags & SA_SIGINFO) {
842 if (copy_siginfo_to_user32(&frame->info, info))
843 return -EFAULT;
846 put_user_try {
847 /* Create the ucontext. */
848 if (cpu_has_xsave)
849 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
850 else
851 put_user_ex(0, &frame->uc.uc_flags);
852 put_user_ex(0, &frame->uc.uc_link);
853 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
854 put_user_ex(sas_ss_flags(regs->sp),
855 &frame->uc.uc_stack.ss_flags);
856 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
857 put_user_ex(0, &frame->uc.uc__pad0);
858 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
859 regs, set->sig[0]);
860 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
862 if (ka->sa.sa_flags & SA_RESTORER) {
863 restorer = ka->sa.sa_restorer;
864 } else {
865 /* could use a vstub here */
866 restorer = NULL;
867 err |= -EFAULT;
869 put_user_ex(restorer, &frame->pretcode);
870 } put_user_catch(err);
872 if (err)
873 return -EFAULT;
875 /* Set up registers for signal handler */
876 regs->sp = (unsigned long) frame;
877 regs->ip = (unsigned long) ka->sa.sa_handler;
879 /* We use the x32 calling convention here... */
880 regs->di = sig;
881 regs->si = (unsigned long) &frame->info;
882 regs->dx = (unsigned long) &frame->uc;
884 loadsegment(ds, __USER_DS);
885 loadsegment(es, __USER_DS);
887 regs->cs = __USER_CS;
888 regs->ss = __USER_DS;
890 return 0;
893 asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
895 struct rt_sigframe_x32 __user *frame;
896 sigset_t set;
897 unsigned long ax;
898 struct pt_regs tregs;
900 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
902 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
903 goto badframe;
904 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
905 goto badframe;
907 set_current_blocked(&set);
909 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
910 goto badframe;
912 tregs = *regs;
913 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
914 goto badframe;
916 return ax;
918 badframe:
919 signal_fault(regs, frame, "x32 rt_sigreturn");
920 return 0;
922 #endif