mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / x86 / kernel / signal.c
blob01741834fd6a09881dc3081a1702f292214c4bed
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
8 * 2000-2002 x86-64 support by Andi Kleen
9 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/sched.h>
14 #include <linux/sched/task_stack.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/tracehook.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/personality.h>
24 #include <linux/uaccess.h>
25 #include <linux/user-return-notifier.h>
26 #include <linux/uprobes.h>
27 #include <linux/context_tracking.h>
29 #include <asm/processor.h>
30 #include <asm/ucontext.h>
31 #include <asm/fpu/internal.h>
32 #include <asm/fpu/signal.h>
33 #include <asm/vdso.h>
34 #include <asm/mce.h>
35 #include <asm/sighandling.h>
36 #include <asm/vm86.h>
38 #ifdef CONFIG_X86_64
39 #include <asm/proto.h>
40 #include <asm/ia32_unistd.h>
41 #endif /* CONFIG_X86_64 */
43 #include <asm/syscall.h>
44 #include <asm/syscalls.h>
46 #include <asm/sigframe.h>
47 #include <asm/signal.h>
49 #define COPY(x) do { \
50 get_user_ex(regs->x, &sc->x); \
51 } while (0)
53 #define GET_SEG(seg) ({ \
54 unsigned short tmp; \
55 get_user_ex(tmp, &sc->seg); \
56 tmp; \
59 #define COPY_SEG(seg) do { \
60 regs->seg = GET_SEG(seg); \
61 } while (0)
63 #define COPY_SEG_CPL3(seg) do { \
64 regs->seg = GET_SEG(seg) | 3; \
65 } while (0)
67 #ifdef CONFIG_X86_64
69 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
70 * alone. Using this generally makes no sense unless
71 * user_64bit_mode(regs) would return true.
73 static void force_valid_ss(struct pt_regs *regs)
75 u32 ar;
76 asm volatile ("lar %[old_ss], %[ar]\n\t"
77 "jz 1f\n\t" /* If invalid: */
78 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
79 "1:"
80 : [ar] "=r" (ar)
81 : [old_ss] "rm" ((u16)regs->ss));
84 * For a valid 64-bit user context, we need DPL 3, type
85 * read-write data or read-write exp-down data, and S and P
86 * set. We can't use VERW because VERW doesn't check the
87 * P bit.
89 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
90 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
91 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
92 regs->ss = __USER_DS;
94 #endif
96 static int restore_sigcontext(struct pt_regs *regs,
97 struct sigcontext __user *sc,
98 unsigned long uc_flags)
100 unsigned long buf_val;
101 void __user *buf;
102 unsigned int tmpflags;
103 unsigned int err = 0;
105 /* Always make any pending restarted system calls return -EINTR */
106 current->restart_block.fn = do_no_restart_syscall;
108 get_user_try {
110 #ifdef CONFIG_X86_32
111 set_user_gs(regs, GET_SEG(gs));
112 COPY_SEG(fs);
113 COPY_SEG(es);
114 COPY_SEG(ds);
115 #endif /* CONFIG_X86_32 */
117 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
118 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
120 #ifdef CONFIG_X86_64
121 COPY(r8);
122 COPY(r9);
123 COPY(r10);
124 COPY(r11);
125 COPY(r12);
126 COPY(r13);
127 COPY(r14);
128 COPY(r15);
129 #endif /* CONFIG_X86_64 */
131 COPY_SEG_CPL3(cs);
132 COPY_SEG_CPL3(ss);
134 get_user_ex(tmpflags, &sc->flags);
135 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
136 regs->orig_ax = -1; /* disable syscall checks */
138 get_user_ex(buf_val, &sc->fpstate);
139 buf = (void __user *)buf_val;
140 } get_user_catch(err);
142 #ifdef CONFIG_X86_64
144 * Fix up SS if needed for the benefit of old DOSEMU and
145 * CRIU.
147 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) && user_64bit_mode(regs)))
148 force_valid_ss(regs);
149 #endif
151 err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
153 force_iret();
155 return err;
158 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
159 struct pt_regs *regs, unsigned long mask)
161 int err = 0;
163 put_user_try {
165 #ifdef CONFIG_X86_32
166 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
167 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
168 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
169 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
170 #endif /* CONFIG_X86_32 */
172 put_user_ex(regs->di, &sc->di);
173 put_user_ex(regs->si, &sc->si);
174 put_user_ex(regs->bp, &sc->bp);
175 put_user_ex(regs->sp, &sc->sp);
176 put_user_ex(regs->bx, &sc->bx);
177 put_user_ex(regs->dx, &sc->dx);
178 put_user_ex(regs->cx, &sc->cx);
179 put_user_ex(regs->ax, &sc->ax);
180 #ifdef CONFIG_X86_64
181 put_user_ex(regs->r8, &sc->r8);
182 put_user_ex(regs->r9, &sc->r9);
183 put_user_ex(regs->r10, &sc->r10);
184 put_user_ex(regs->r11, &sc->r11);
185 put_user_ex(regs->r12, &sc->r12);
186 put_user_ex(regs->r13, &sc->r13);
187 put_user_ex(regs->r14, &sc->r14);
188 put_user_ex(regs->r15, &sc->r15);
189 #endif /* CONFIG_X86_64 */
191 put_user_ex(current->thread.trap_nr, &sc->trapno);
192 put_user_ex(current->thread.error_code, &sc->err);
193 put_user_ex(regs->ip, &sc->ip);
194 #ifdef CONFIG_X86_32
195 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
196 put_user_ex(regs->flags, &sc->flags);
197 put_user_ex(regs->sp, &sc->sp_at_signal);
198 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
199 #else /* !CONFIG_X86_32 */
200 put_user_ex(regs->flags, &sc->flags);
201 put_user_ex(regs->cs, &sc->cs);
202 put_user_ex(0, &sc->gs);
203 put_user_ex(0, &sc->fs);
204 put_user_ex(regs->ss, &sc->ss);
205 #endif /* CONFIG_X86_32 */
207 put_user_ex(fpstate, &sc->fpstate);
209 /* non-iBCS2 extensions.. */
210 put_user_ex(mask, &sc->oldmask);
211 put_user_ex(current->thread.cr2, &sc->cr2);
212 } put_user_catch(err);
214 return err;
218 * Set up a signal frame.
222 * Determine which stack to use..
224 static unsigned long align_sigframe(unsigned long sp)
226 #ifdef CONFIG_X86_32
228 * Align the stack pointer according to the i386 ABI,
229 * i.e. so that on function entry ((sp + 4) & 15) == 0.
231 sp = ((sp + 4) & -16ul) - 4;
232 #else /* !CONFIG_X86_32 */
233 sp = round_down(sp, 16) - 8;
234 #endif
235 return sp;
238 static void __user *
239 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
240 void __user **fpstate)
242 /* Default to using normal stack */
243 unsigned long math_size = 0;
244 unsigned long sp = regs->sp;
245 unsigned long buf_fx = 0;
246 int onsigstack = on_sig_stack(sp);
247 struct fpu *fpu = &current->thread.fpu;
249 /* redzone */
250 if (IS_ENABLED(CONFIG_X86_64))
251 sp -= 128;
253 /* This is the X/Open sanctioned signal stack switching. */
254 if (ka->sa.sa_flags & SA_ONSTACK) {
255 if (sas_ss_flags(sp) == 0)
256 sp = current->sas_ss_sp + current->sas_ss_size;
257 } else if (IS_ENABLED(CONFIG_X86_32) &&
258 !onsigstack &&
259 regs->ss != __USER_DS &&
260 !(ka->sa.sa_flags & SA_RESTORER) &&
261 ka->sa.sa_restorer) {
262 /* This is the legacy signal stack switching. */
263 sp = (unsigned long) ka->sa.sa_restorer;
266 if (fpu->initialized) {
267 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
268 &buf_fx, &math_size);
269 *fpstate = (void __user *)sp;
272 sp = align_sigframe(sp - frame_size);
275 * If we are on the alternate signal stack and would overflow it, don't.
276 * Return an always-bogus address instead so we will die with SIGSEGV.
278 if (onsigstack && !likely(on_sig_stack(sp)))
279 return (void __user *)-1L;
281 /* save i387 and extended state */
282 if (fpu->initialized &&
283 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
284 return (void __user *)-1L;
286 return (void __user *)sp;
289 #ifdef CONFIG_X86_32
290 static const struct {
291 u16 poplmovl;
292 u32 val;
293 u16 int80;
294 } __attribute__((packed)) retcode = {
295 0xb858, /* popl %eax; movl $..., %eax */
296 __NR_sigreturn,
297 0x80cd, /* int $0x80 */
300 static const struct {
301 u8 movl;
302 u32 val;
303 u16 int80;
304 u8 pad;
305 } __attribute__((packed)) rt_retcode = {
306 0xb8, /* movl $..., %eax */
307 __NR_rt_sigreturn,
308 0x80cd, /* int $0x80 */
312 static int
313 __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
314 struct pt_regs *regs)
316 struct sigframe __user *frame;
317 void __user *restorer;
318 int err = 0;
319 void __user *fpstate = NULL;
321 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
323 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
324 return -EFAULT;
326 if (__put_user(sig, &frame->sig))
327 return -EFAULT;
329 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
330 return -EFAULT;
332 if (_NSIG_WORDS > 1) {
333 if (__copy_to_user(&frame->extramask, &set->sig[1],
334 sizeof(frame->extramask)))
335 return -EFAULT;
338 if (current->mm->context.vdso)
339 restorer = current->mm->context.vdso +
340 vdso_image_32.sym___kernel_sigreturn;
341 else
342 restorer = &frame->retcode;
343 if (ksig->ka.sa.sa_flags & SA_RESTORER)
344 restorer = ksig->ka.sa.sa_restorer;
346 /* Set up to return from userspace. */
347 err |= __put_user(restorer, &frame->pretcode);
350 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
352 * WE DO NOT USE IT ANY MORE! It's only left here for historical
353 * reasons and because gdb uses it as a signature to notice
354 * signal handler stack frames.
356 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
358 if (err)
359 return -EFAULT;
361 /* Set up registers for signal handler */
362 regs->sp = (unsigned long)frame;
363 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
364 regs->ax = (unsigned long)sig;
365 regs->dx = 0;
366 regs->cx = 0;
368 regs->ds = __USER_DS;
369 regs->es = __USER_DS;
370 regs->ss = __USER_DS;
371 regs->cs = __USER_CS;
373 return 0;
376 static int __setup_rt_frame(int sig, struct ksignal *ksig,
377 sigset_t *set, struct pt_regs *regs)
379 struct rt_sigframe __user *frame;
380 void __user *restorer;
381 int err = 0;
382 void __user *fpstate = NULL;
384 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
386 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
387 return -EFAULT;
389 put_user_try {
390 put_user_ex(sig, &frame->sig);
391 put_user_ex(&frame->info, &frame->pinfo);
392 put_user_ex(&frame->uc, &frame->puc);
394 /* Create the ucontext. */
395 if (boot_cpu_has(X86_FEATURE_XSAVE))
396 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
397 else
398 put_user_ex(0, &frame->uc.uc_flags);
399 put_user_ex(0, &frame->uc.uc_link);
400 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
402 /* Set up to return from userspace. */
403 restorer = current->mm->context.vdso +
404 vdso_image_32.sym___kernel_rt_sigreturn;
405 if (ksig->ka.sa.sa_flags & SA_RESTORER)
406 restorer = ksig->ka.sa.sa_restorer;
407 put_user_ex(restorer, &frame->pretcode);
410 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
412 * WE DO NOT USE IT ANY MORE! It's only left here for historical
413 * reasons and because gdb uses it as a signature to notice
414 * signal handler stack frames.
416 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
417 } put_user_catch(err);
419 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
420 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
421 regs, set->sig[0]);
422 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
424 if (err)
425 return -EFAULT;
427 /* Set up registers for signal handler */
428 regs->sp = (unsigned long)frame;
429 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
430 regs->ax = (unsigned long)sig;
431 regs->dx = (unsigned long)&frame->info;
432 regs->cx = (unsigned long)&frame->uc;
434 regs->ds = __USER_DS;
435 regs->es = __USER_DS;
436 regs->ss = __USER_DS;
437 regs->cs = __USER_CS;
439 return 0;
441 #else /* !CONFIG_X86_32 */
442 static unsigned long frame_uc_flags(struct pt_regs *regs)
444 unsigned long flags;
446 if (boot_cpu_has(X86_FEATURE_XSAVE))
447 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
448 else
449 flags = UC_SIGCONTEXT_SS;
451 if (likely(user_64bit_mode(regs)))
452 flags |= UC_STRICT_RESTORE_SS;
454 return flags;
457 static int __setup_rt_frame(int sig, struct ksignal *ksig,
458 sigset_t *set, struct pt_regs *regs)
460 struct rt_sigframe __user *frame;
461 void __user *fp = NULL;
462 unsigned long uc_flags;
463 int err = 0;
465 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
467 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
468 return -EFAULT;
470 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
471 if (copy_siginfo_to_user(&frame->info, &ksig->info))
472 return -EFAULT;
475 uc_flags = frame_uc_flags(regs);
477 put_user_try {
478 /* Create the ucontext. */
479 put_user_ex(uc_flags, &frame->uc.uc_flags);
480 put_user_ex(0, &frame->uc.uc_link);
481 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
483 /* Set up to return from userspace. If provided, use a stub
484 already in userspace. */
485 /* x86-64 should always use SA_RESTORER. */
486 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
487 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
488 } else {
489 /* could use a vstub here */
490 err |= -EFAULT;
492 } put_user_catch(err);
494 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
495 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
497 if (err)
498 return -EFAULT;
500 /* Set up registers for signal handler */
501 regs->di = sig;
502 /* In case the signal handler was declared without prototypes */
503 regs->ax = 0;
505 /* This also works for non SA_SIGINFO handlers because they expect the
506 next argument after the signal number on the stack. */
507 regs->si = (unsigned long)&frame->info;
508 regs->dx = (unsigned long)&frame->uc;
509 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
511 regs->sp = (unsigned long)frame;
514 * Set up the CS and SS registers to run signal handlers in
515 * 64-bit mode, even if the handler happens to be interrupting
516 * 32-bit or 16-bit code.
518 * SS is subtle. In 64-bit mode, we don't need any particular
519 * SS descriptor, but we do need SS to be valid. It's possible
520 * that the old SS is entirely bogus -- this can happen if the
521 * signal we're trying to deliver is #GP or #SS caused by a bad
522 * SS value. We also have a compatbility issue here: DOSEMU
523 * relies on the contents of the SS register indicating the
524 * SS value at the time of the signal, even though that code in
525 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
526 * avoids relying on sigreturn to restore SS; instead it uses
527 * a trampoline.) So we do our best: if the old SS was valid,
528 * we keep it. Otherwise we replace it.
530 regs->cs = __USER_CS;
532 if (unlikely(regs->ss != __USER_DS))
533 force_valid_ss(regs);
535 return 0;
537 #endif /* CONFIG_X86_32 */
539 static int x32_setup_rt_frame(struct ksignal *ksig,
540 compat_sigset_t *set,
541 struct pt_regs *regs)
543 #ifdef CONFIG_X86_X32_ABI
544 struct rt_sigframe_x32 __user *frame;
545 unsigned long uc_flags;
546 void __user *restorer;
547 int err = 0;
548 void __user *fpstate = NULL;
550 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
552 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
553 return -EFAULT;
555 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
556 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
557 return -EFAULT;
560 uc_flags = frame_uc_flags(regs);
562 put_user_try {
563 /* Create the ucontext. */
564 put_user_ex(uc_flags, &frame->uc.uc_flags);
565 put_user_ex(0, &frame->uc.uc_link);
566 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
567 put_user_ex(0, &frame->uc.uc__pad0);
569 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
570 restorer = ksig->ka.sa.sa_restorer;
571 } else {
572 /* could use a vstub here */
573 restorer = NULL;
574 err |= -EFAULT;
576 put_user_ex(restorer, &frame->pretcode);
577 } put_user_catch(err);
579 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
580 regs, set->sig[0]);
581 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
583 if (err)
584 return -EFAULT;
586 /* Set up registers for signal handler */
587 regs->sp = (unsigned long) frame;
588 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
590 /* We use the x32 calling convention here... */
591 regs->di = ksig->sig;
592 regs->si = (unsigned long) &frame->info;
593 regs->dx = (unsigned long) &frame->uc;
595 loadsegment(ds, __USER_DS);
596 loadsegment(es, __USER_DS);
598 regs->cs = __USER_CS;
599 regs->ss = __USER_DS;
600 #endif /* CONFIG_X86_X32_ABI */
602 return 0;
606 * Do a signal return; undo the signal stack.
608 #ifdef CONFIG_X86_32
609 asmlinkage unsigned long sys_sigreturn(void)
611 struct pt_regs *regs = current_pt_regs();
612 struct sigframe __user *frame;
613 sigset_t set;
615 frame = (struct sigframe __user *)(regs->sp - 8);
617 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
618 goto badframe;
619 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
620 && __copy_from_user(&set.sig[1], &frame->extramask,
621 sizeof(frame->extramask))))
622 goto badframe;
624 set_current_blocked(&set);
627 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
628 * Save a few cycles by skipping the __get_user.
630 if (restore_sigcontext(regs, &frame->sc, 0))
631 goto badframe;
632 return regs->ax;
634 badframe:
635 signal_fault(regs, frame, "sigreturn");
637 return 0;
639 #endif /* CONFIG_X86_32 */
641 asmlinkage long sys_rt_sigreturn(void)
643 struct pt_regs *regs = current_pt_regs();
644 struct rt_sigframe __user *frame;
645 sigset_t set;
646 unsigned long uc_flags;
648 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
649 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
650 goto badframe;
651 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
652 goto badframe;
653 if (__get_user(uc_flags, &frame->uc.uc_flags))
654 goto badframe;
656 set_current_blocked(&set);
658 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
659 goto badframe;
661 if (restore_altstack(&frame->uc.uc_stack))
662 goto badframe;
664 return regs->ax;
666 badframe:
667 signal_fault(regs, frame, "rt_sigreturn");
668 return 0;
671 static inline int is_ia32_compat_frame(struct ksignal *ksig)
673 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
674 ksig->ka.sa.sa_flags & SA_IA32_ABI;
677 static inline int is_ia32_frame(struct ksignal *ksig)
679 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
682 static inline int is_x32_frame(struct ksignal *ksig)
684 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
685 ksig->ka.sa.sa_flags & SA_X32_ABI;
688 static int
689 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
691 int usig = ksig->sig;
692 sigset_t *set = sigmask_to_save();
693 compat_sigset_t *cset = (compat_sigset_t *) set;
695 /* Set up the stack frame */
696 if (is_ia32_frame(ksig)) {
697 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
698 return ia32_setup_rt_frame(usig, ksig, cset, regs);
699 else
700 return ia32_setup_frame(usig, ksig, cset, regs);
701 } else if (is_x32_frame(ksig)) {
702 return x32_setup_rt_frame(ksig, cset, regs);
703 } else {
704 return __setup_rt_frame(ksig->sig, ksig, set, regs);
708 static void
709 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
711 bool stepping, failed;
712 struct fpu *fpu = &current->thread.fpu;
714 if (v8086_mode(regs))
715 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
717 /* Are we from a system call? */
718 if (syscall_get_nr(current, regs) >= 0) {
719 /* If so, check system call restarting.. */
720 switch (syscall_get_error(current, regs)) {
721 case -ERESTART_RESTARTBLOCK:
722 case -ERESTARTNOHAND:
723 regs->ax = -EINTR;
724 break;
726 case -ERESTARTSYS:
727 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
728 regs->ax = -EINTR;
729 break;
731 /* fallthrough */
732 case -ERESTARTNOINTR:
733 regs->ax = regs->orig_ax;
734 regs->ip -= 2;
735 break;
740 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
741 * so that register information in the sigcontext is correct and
742 * then notify the tracer before entering the signal handler.
744 stepping = test_thread_flag(TIF_SINGLESTEP);
745 if (stepping)
746 user_disable_single_step(current);
748 failed = (setup_rt_frame(ksig, regs) < 0);
749 if (!failed) {
751 * Clear the direction flag as per the ABI for function entry.
753 * Clear RF when entering the signal handler, because
754 * it might disable possible debug exception from the
755 * signal handler.
757 * Clear TF for the case when it wasn't set by debugger to
758 * avoid the recursive send_sigtrap() in SIGTRAP handler.
760 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
762 * Ensure the signal handler starts with the new fpu state.
764 if (fpu->initialized)
765 fpu__clear(fpu);
767 signal_setup_done(failed, ksig, stepping);
770 static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
773 * This function is fundamentally broken as currently
774 * implemented.
776 * The idea is that we want to trigger a call to the
777 * restart_block() syscall and that we want in_ia32_syscall(),
778 * in_x32_syscall(), etc. to match whatever they were in the
779 * syscall being restarted. We assume that the syscall
780 * instruction at (regs->ip - 2) matches whatever syscall
781 * instruction we used to enter in the first place.
783 * The problem is that we can get here when ptrace pokes
784 * syscall-like values into regs even if we're not in a syscall
785 * at all.
787 * For now, we maintain historical behavior and guess based on
788 * stored state. We could do better by saving the actual
789 * syscall arch in restart_block or (with caveats on x32) by
790 * checking if regs->ip points to 'int $0x80'. The current
791 * behavior is incorrect if a tracer has a different bitness
792 * than the tracee.
794 #ifdef CONFIG_IA32_EMULATION
795 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
796 return __NR_ia32_restart_syscall;
797 #endif
798 #ifdef CONFIG_X86_X32_ABI
799 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
800 #else
801 return __NR_restart_syscall;
802 #endif
806 * Note that 'init' is a special process: it doesn't get signals it doesn't
807 * want to handle. Thus you cannot kill init even with a SIGKILL even by
808 * mistake.
810 void do_signal(struct pt_regs *regs)
812 struct ksignal ksig;
814 if (get_signal(&ksig)) {
815 /* Whee! Actually deliver the signal. */
816 handle_signal(&ksig, regs);
817 return;
820 /* Did we come from a system call? */
821 if (syscall_get_nr(current, regs) >= 0) {
822 /* Restart the system call - no handlers present */
823 switch (syscall_get_error(current, regs)) {
824 case -ERESTARTNOHAND:
825 case -ERESTARTSYS:
826 case -ERESTARTNOINTR:
827 regs->ax = regs->orig_ax;
828 regs->ip -= 2;
829 break;
831 case -ERESTART_RESTARTBLOCK:
832 regs->ax = get_nr_restart_syscall(regs);
833 regs->ip -= 2;
834 break;
839 * If there's no signal to deliver, we just put the saved sigmask
840 * back.
842 restore_saved_sigmask();
845 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
847 struct task_struct *me = current;
849 if (show_unhandled_signals && printk_ratelimit()) {
850 printk("%s"
851 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
852 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
853 me->comm, me->pid, where, frame,
854 regs->ip, regs->sp, regs->orig_ax);
855 print_vma_addr(KERN_CONT " in ", regs->ip);
856 pr_cont("\n");
859 force_sig(SIGSEGV, me);
862 #ifdef CONFIG_X86_X32_ABI
863 asmlinkage long sys32_x32_rt_sigreturn(void)
865 struct pt_regs *regs = current_pt_regs();
866 struct rt_sigframe_x32 __user *frame;
867 sigset_t set;
868 unsigned long uc_flags;
870 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
872 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
873 goto badframe;
874 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
875 goto badframe;
876 if (__get_user(uc_flags, &frame->uc.uc_flags))
877 goto badframe;
879 set_current_blocked(&set);
881 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
882 goto badframe;
884 if (compat_restore_altstack(&frame->uc.uc_stack))
885 goto badframe;
887 return regs->ax;
889 badframe:
890 signal_fault(regs, frame, "x32 rt_sigreturn");
891 return 0;
893 #endif