thermal: fix Mediatek thermal controller build
[linux/fpc-iii.git] / arch / x86 / kernel / signal.c
blob548ddf7d6fd2085676328ffcde6375ce3e88b477
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>
25 #include <linux/context_tracking.h>
27 #include <asm/processor.h>
28 #include <asm/ucontext.h>
29 #include <asm/fpu/internal.h>
30 #include <asm/fpu/signal.h>
31 #include <asm/vdso.h>
32 #include <asm/mce.h>
33 #include <asm/sighandling.h>
34 #include <asm/vm86.h>
36 #ifdef CONFIG_X86_64
37 #include <asm/proto.h>
38 #include <asm/ia32_unistd.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); \
48 } while (0)
50 #define GET_SEG(seg) ({ \
51 unsigned short tmp; \
52 get_user_ex(tmp, &sc->seg); \
53 tmp; \
56 #define COPY_SEG(seg) do { \
57 regs->seg = GET_SEG(seg); \
58 } while (0)
60 #define COPY_SEG_CPL3(seg) do { \
61 regs->seg = GET_SEG(seg) | 3; \
62 } while (0)
64 #ifdef CONFIG_X86_64
66 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
67 * alone. Using this generally makes no sense unless
68 * user_64bit_mode(regs) would return true.
70 static void force_valid_ss(struct pt_regs *regs)
72 u32 ar;
73 asm volatile ("lar %[old_ss], %[ar]\n\t"
74 "jz 1f\n\t" /* If invalid: */
75 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
76 "1:"
77 : [ar] "=r" (ar)
78 : [old_ss] "rm" ((u16)regs->ss));
81 * For a valid 64-bit user context, we need DPL 3, type
82 * read-write data or read-write exp-down data, and S and P
83 * set. We can't use VERW because VERW doesn't check the
84 * P bit.
86 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
87 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
88 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
89 regs->ss = __USER_DS;
91 #endif
93 static int restore_sigcontext(struct pt_regs *regs,
94 struct sigcontext __user *sc,
95 unsigned long uc_flags)
97 unsigned long buf_val;
98 void __user *buf;
99 unsigned int tmpflags;
100 unsigned int err = 0;
102 /* Always make any pending restarted system calls return -EINTR */
103 current->restart_block.fn = do_no_restart_syscall;
105 get_user_try {
107 #ifdef CONFIG_X86_32
108 set_user_gs(regs, GET_SEG(gs));
109 COPY_SEG(fs);
110 COPY_SEG(es);
111 COPY_SEG(ds);
112 #endif /* CONFIG_X86_32 */
114 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
115 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
117 #ifdef CONFIG_X86_64
118 COPY(r8);
119 COPY(r9);
120 COPY(r10);
121 COPY(r11);
122 COPY(r12);
123 COPY(r13);
124 COPY(r14);
125 COPY(r15);
126 #endif /* CONFIG_X86_64 */
128 COPY_SEG_CPL3(cs);
129 COPY_SEG_CPL3(ss);
131 #ifdef CONFIG_X86_64
133 * Fix up SS if needed for the benefit of old DOSEMU and
134 * CRIU.
136 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
137 user_64bit_mode(regs)))
138 force_valid_ss(regs);
139 #endif
141 get_user_ex(tmpflags, &sc->flags);
142 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
143 regs->orig_ax = -1; /* disable syscall checks */
145 get_user_ex(buf_val, &sc->fpstate);
146 buf = (void __user *)buf_val;
147 } get_user_catch(err);
149 err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
151 force_iret();
153 return err;
156 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
157 struct pt_regs *regs, unsigned long mask)
159 int err = 0;
161 put_user_try {
163 #ifdef CONFIG_X86_32
164 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
165 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
166 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
167 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
168 #endif /* CONFIG_X86_32 */
170 put_user_ex(regs->di, &sc->di);
171 put_user_ex(regs->si, &sc->si);
172 put_user_ex(regs->bp, &sc->bp);
173 put_user_ex(regs->sp, &sc->sp);
174 put_user_ex(regs->bx, &sc->bx);
175 put_user_ex(regs->dx, &sc->dx);
176 put_user_ex(regs->cx, &sc->cx);
177 put_user_ex(regs->ax, &sc->ax);
178 #ifdef CONFIG_X86_64
179 put_user_ex(regs->r8, &sc->r8);
180 put_user_ex(regs->r9, &sc->r9);
181 put_user_ex(regs->r10, &sc->r10);
182 put_user_ex(regs->r11, &sc->r11);
183 put_user_ex(regs->r12, &sc->r12);
184 put_user_ex(regs->r13, &sc->r13);
185 put_user_ex(regs->r14, &sc->r14);
186 put_user_ex(regs->r15, &sc->r15);
187 #endif /* CONFIG_X86_64 */
189 put_user_ex(current->thread.trap_nr, &sc->trapno);
190 put_user_ex(current->thread.error_code, &sc->err);
191 put_user_ex(regs->ip, &sc->ip);
192 #ifdef CONFIG_X86_32
193 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
194 put_user_ex(regs->flags, &sc->flags);
195 put_user_ex(regs->sp, &sc->sp_at_signal);
196 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
197 #else /* !CONFIG_X86_32 */
198 put_user_ex(regs->flags, &sc->flags);
199 put_user_ex(regs->cs, &sc->cs);
200 put_user_ex(0, &sc->gs);
201 put_user_ex(0, &sc->fs);
202 put_user_ex(regs->ss, &sc->ss);
203 #endif /* CONFIG_X86_32 */
205 put_user_ex(fpstate, &sc->fpstate);
207 /* non-iBCS2 extensions.. */
208 put_user_ex(mask, &sc->oldmask);
209 put_user_ex(current->thread.cr2, &sc->cr2);
210 } put_user_catch(err);
212 return err;
216 * Set up a signal frame.
220 * Determine which stack to use..
222 static unsigned long align_sigframe(unsigned long sp)
224 #ifdef CONFIG_X86_32
226 * Align the stack pointer according to the i386 ABI,
227 * i.e. so that on function entry ((sp + 4) & 15) == 0.
229 sp = ((sp + 4) & -16ul) - 4;
230 #else /* !CONFIG_X86_32 */
231 sp = round_down(sp, 16) - 8;
232 #endif
233 return sp;
236 static void __user *
237 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
238 void __user **fpstate)
240 /* Default to using normal stack */
241 unsigned long math_size = 0;
242 unsigned long sp = regs->sp;
243 unsigned long buf_fx = 0;
244 int onsigstack = on_sig_stack(sp);
245 struct fpu *fpu = &current->thread.fpu;
247 /* redzone */
248 if (config_enabled(CONFIG_X86_64))
249 sp -= 128;
251 if (!onsigstack) {
252 /* This is the X/Open sanctioned signal stack switching. */
253 if (ka->sa.sa_flags & SA_ONSTACK) {
254 if (current->sas_ss_size)
255 sp = current->sas_ss_sp + current->sas_ss_size;
256 } else if (config_enabled(CONFIG_X86_32) &&
257 (regs->ss & 0xffff) != __USER_DS &&
258 !(ka->sa.sa_flags & SA_RESTORER) &&
259 ka->sa.sa_restorer) {
260 /* This is the legacy signal stack switching. */
261 sp = (unsigned long) ka->sa.sa_restorer;
265 if (fpu->fpstate_active) {
266 sp = fpu__alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
267 &buf_fx, &math_size);
268 *fpstate = (void __user *)sp;
271 sp = align_sigframe(sp - frame_size);
274 * If we are on the alternate signal stack and would overflow it, don't.
275 * Return an always-bogus address instead so we will die with SIGSEGV.
277 if (onsigstack && !likely(on_sig_stack(sp)))
278 return (void __user *)-1L;
280 /* save i387 and extended state */
281 if (fpu->fpstate_active &&
282 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
283 return (void __user *)-1L;
285 return (void __user *)sp;
288 #ifdef CONFIG_X86_32
289 static const struct {
290 u16 poplmovl;
291 u32 val;
292 u16 int80;
293 } __attribute__((packed)) retcode = {
294 0xb858, /* popl %eax; movl $..., %eax */
295 __NR_sigreturn,
296 0x80cd, /* int $0x80 */
299 static const struct {
300 u8 movl;
301 u32 val;
302 u16 int80;
303 u8 pad;
304 } __attribute__((packed)) rt_retcode = {
305 0xb8, /* movl $..., %eax */
306 __NR_rt_sigreturn,
307 0x80cd, /* int $0x80 */
311 static int
312 __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
313 struct pt_regs *regs)
315 struct sigframe __user *frame;
316 void __user *restorer;
317 int err = 0;
318 void __user *fpstate = NULL;
320 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
322 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
323 return -EFAULT;
325 if (__put_user(sig, &frame->sig))
326 return -EFAULT;
328 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
329 return -EFAULT;
331 if (_NSIG_WORDS > 1) {
332 if (__copy_to_user(&frame->extramask, &set->sig[1],
333 sizeof(frame->extramask)))
334 return -EFAULT;
337 if (current->mm->context.vdso)
338 restorer = current->mm->context.vdso +
339 vdso_image_32.sym___kernel_sigreturn;
340 else
341 restorer = &frame->retcode;
342 if (ksig->ka.sa.sa_flags & SA_RESTORER)
343 restorer = ksig->ka.sa.sa_restorer;
345 /* Set up to return from userspace. */
346 err |= __put_user(restorer, &frame->pretcode);
349 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
351 * WE DO NOT USE IT ANY MORE! It's only left here for historical
352 * reasons and because gdb uses it as a signature to notice
353 * signal handler stack frames.
355 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
357 if (err)
358 return -EFAULT;
360 /* Set up registers for signal handler */
361 regs->sp = (unsigned long)frame;
362 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
363 regs->ax = (unsigned long)sig;
364 regs->dx = 0;
365 regs->cx = 0;
367 regs->ds = __USER_DS;
368 regs->es = __USER_DS;
369 regs->ss = __USER_DS;
370 regs->cs = __USER_CS;
372 return 0;
375 static int __setup_rt_frame(int sig, struct ksignal *ksig,
376 sigset_t *set, struct pt_regs *regs)
378 struct rt_sigframe __user *frame;
379 void __user *restorer;
380 int err = 0;
381 void __user *fpstate = NULL;
383 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
385 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
386 return -EFAULT;
388 put_user_try {
389 put_user_ex(sig, &frame->sig);
390 put_user_ex(&frame->info, &frame->pinfo);
391 put_user_ex(&frame->uc, &frame->puc);
393 /* Create the ucontext. */
394 if (cpu_has_xsave)
395 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
396 else
397 put_user_ex(0, &frame->uc.uc_flags);
398 put_user_ex(0, &frame->uc.uc_link);
399 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
401 /* Set up to return from userspace. */
402 restorer = current->mm->context.vdso +
403 vdso_image_32.sym___kernel_rt_sigreturn;
404 if (ksig->ka.sa.sa_flags & SA_RESTORER)
405 restorer = ksig->ka.sa.sa_restorer;
406 put_user_ex(restorer, &frame->pretcode);
409 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
411 * WE DO NOT USE IT ANY MORE! It's only left here for historical
412 * reasons and because gdb uses it as a signature to notice
413 * signal handler stack frames.
415 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
416 } put_user_catch(err);
418 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
419 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
420 regs, set->sig[0]);
421 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
423 if (err)
424 return -EFAULT;
426 /* Set up registers for signal handler */
427 regs->sp = (unsigned long)frame;
428 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
429 regs->ax = (unsigned long)sig;
430 regs->dx = (unsigned long)&frame->info;
431 regs->cx = (unsigned long)&frame->uc;
433 regs->ds = __USER_DS;
434 regs->es = __USER_DS;
435 regs->ss = __USER_DS;
436 regs->cs = __USER_CS;
438 return 0;
440 #else /* !CONFIG_X86_32 */
441 static unsigned long frame_uc_flags(struct pt_regs *regs)
443 unsigned long flags;
445 if (cpu_has_xsave)
446 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
447 else
448 flags = UC_SIGCONTEXT_SS;
450 if (likely(user_64bit_mode(regs)))
451 flags |= UC_STRICT_RESTORE_SS;
453 return flags;
456 static int __setup_rt_frame(int sig, struct ksignal *ksig,
457 sigset_t *set, struct pt_regs *regs)
459 struct rt_sigframe __user *frame;
460 void __user *fp = NULL;
461 int err = 0;
463 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
465 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
466 return -EFAULT;
468 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
469 if (copy_siginfo_to_user(&frame->info, &ksig->info))
470 return -EFAULT;
473 put_user_try {
474 /* Create the ucontext. */
475 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
476 put_user_ex(0, &frame->uc.uc_link);
477 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
479 /* Set up to return from userspace. If provided, use a stub
480 already in userspace. */
481 /* x86-64 should always use SA_RESTORER. */
482 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
483 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
484 } else {
485 /* could use a vstub here */
486 err |= -EFAULT;
488 } put_user_catch(err);
490 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
491 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
493 if (err)
494 return -EFAULT;
496 /* Set up registers for signal handler */
497 regs->di = sig;
498 /* In case the signal handler was declared without prototypes */
499 regs->ax = 0;
501 /* This also works for non SA_SIGINFO handlers because they expect the
502 next argument after the signal number on the stack. */
503 regs->si = (unsigned long)&frame->info;
504 regs->dx = (unsigned long)&frame->uc;
505 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
507 regs->sp = (unsigned long)frame;
510 * Set up the CS and SS registers to run signal handlers in
511 * 64-bit mode, even if the handler happens to be interrupting
512 * 32-bit or 16-bit code.
514 * SS is subtle. In 64-bit mode, we don't need any particular
515 * SS descriptor, but we do need SS to be valid. It's possible
516 * that the old SS is entirely bogus -- this can happen if the
517 * signal we're trying to deliver is #GP or #SS caused by a bad
518 * SS value. We also have a compatbility issue here: DOSEMU
519 * relies on the contents of the SS register indicating the
520 * SS value at the time of the signal, even though that code in
521 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
522 * avoids relying on sigreturn to restore SS; instead it uses
523 * a trampoline.) So we do our best: if the old SS was valid,
524 * we keep it. Otherwise we replace it.
526 regs->cs = __USER_CS;
528 if (unlikely(regs->ss != __USER_DS))
529 force_valid_ss(regs);
531 return 0;
533 #endif /* CONFIG_X86_32 */
535 static int x32_setup_rt_frame(struct ksignal *ksig,
536 compat_sigset_t *set,
537 struct pt_regs *regs)
539 #ifdef CONFIG_X86_X32_ABI
540 struct rt_sigframe_x32 __user *frame;
541 void __user *restorer;
542 int err = 0;
543 void __user *fpstate = NULL;
545 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
547 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
548 return -EFAULT;
550 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
551 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
552 return -EFAULT;
555 put_user_try {
556 /* Create the ucontext. */
557 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
558 put_user_ex(0, &frame->uc.uc_link);
559 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
560 put_user_ex(0, &frame->uc.uc__pad0);
562 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
563 restorer = ksig->ka.sa.sa_restorer;
564 } else {
565 /* could use a vstub here */
566 restorer = NULL;
567 err |= -EFAULT;
569 put_user_ex(restorer, &frame->pretcode);
570 } put_user_catch(err);
572 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
573 regs, set->sig[0]);
574 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
576 if (err)
577 return -EFAULT;
579 /* Set up registers for signal handler */
580 regs->sp = (unsigned long) frame;
581 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
583 /* We use the x32 calling convention here... */
584 regs->di = ksig->sig;
585 regs->si = (unsigned long) &frame->info;
586 regs->dx = (unsigned long) &frame->uc;
588 loadsegment(ds, __USER_DS);
589 loadsegment(es, __USER_DS);
591 regs->cs = __USER_CS;
592 regs->ss = __USER_DS;
593 #endif /* CONFIG_X86_X32_ABI */
595 return 0;
599 * Do a signal return; undo the signal stack.
601 #ifdef CONFIG_X86_32
602 asmlinkage unsigned long sys_sigreturn(void)
604 struct pt_regs *regs = current_pt_regs();
605 struct sigframe __user *frame;
606 sigset_t set;
608 frame = (struct sigframe __user *)(regs->sp - 8);
610 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
611 goto badframe;
612 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
613 && __copy_from_user(&set.sig[1], &frame->extramask,
614 sizeof(frame->extramask))))
615 goto badframe;
617 set_current_blocked(&set);
620 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
621 * Save a few cycles by skipping the __get_user.
623 if (restore_sigcontext(regs, &frame->sc, 0))
624 goto badframe;
625 return regs->ax;
627 badframe:
628 signal_fault(regs, frame, "sigreturn");
630 return 0;
632 #endif /* CONFIG_X86_32 */
634 asmlinkage long sys_rt_sigreturn(void)
636 struct pt_regs *regs = current_pt_regs();
637 struct rt_sigframe __user *frame;
638 sigset_t set;
639 unsigned long uc_flags;
641 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
642 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
643 goto badframe;
644 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
645 goto badframe;
646 if (__get_user(uc_flags, &frame->uc.uc_flags))
647 goto badframe;
649 set_current_blocked(&set);
651 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
652 goto badframe;
654 if (restore_altstack(&frame->uc.uc_stack))
655 goto badframe;
657 return regs->ax;
659 badframe:
660 signal_fault(regs, frame, "rt_sigreturn");
661 return 0;
664 static inline int is_ia32_compat_frame(void)
666 return config_enabled(CONFIG_IA32_EMULATION) &&
667 test_thread_flag(TIF_IA32);
670 static inline int is_ia32_frame(void)
672 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
675 static inline int is_x32_frame(void)
677 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
680 static int
681 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
683 int usig = ksig->sig;
684 sigset_t *set = sigmask_to_save();
685 compat_sigset_t *cset = (compat_sigset_t *) set;
687 /* Set up the stack frame */
688 if (is_ia32_frame()) {
689 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
690 return ia32_setup_rt_frame(usig, ksig, cset, regs);
691 else
692 return ia32_setup_frame(usig, ksig, cset, regs);
693 } else if (is_x32_frame()) {
694 return x32_setup_rt_frame(ksig, cset, regs);
695 } else {
696 return __setup_rt_frame(ksig->sig, ksig, set, regs);
700 static void
701 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
703 bool stepping, failed;
704 struct fpu *fpu = &current->thread.fpu;
706 if (v8086_mode(regs))
707 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
709 /* Are we from a system call? */
710 if (syscall_get_nr(current, regs) >= 0) {
711 /* If so, check system call restarting.. */
712 switch (syscall_get_error(current, regs)) {
713 case -ERESTART_RESTARTBLOCK:
714 case -ERESTARTNOHAND:
715 regs->ax = -EINTR;
716 break;
718 case -ERESTARTSYS:
719 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
720 regs->ax = -EINTR;
721 break;
723 /* fallthrough */
724 case -ERESTARTNOINTR:
725 regs->ax = regs->orig_ax;
726 regs->ip -= 2;
727 break;
732 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
733 * so that register information in the sigcontext is correct and
734 * then notify the tracer before entering the signal handler.
736 stepping = test_thread_flag(TIF_SINGLESTEP);
737 if (stepping)
738 user_disable_single_step(current);
740 failed = (setup_rt_frame(ksig, regs) < 0);
741 if (!failed) {
743 * Clear the direction flag as per the ABI for function entry.
745 * Clear RF when entering the signal handler, because
746 * it might disable possible debug exception from the
747 * signal handler.
749 * Clear TF for the case when it wasn't set by debugger to
750 * avoid the recursive send_sigtrap() in SIGTRAP handler.
752 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
754 * Ensure the signal handler starts with the new fpu state.
756 if (fpu->fpstate_active)
757 fpu__clear(fpu);
759 signal_setup_done(failed, ksig, stepping);
762 static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
764 #ifdef CONFIG_X86_64
765 if (is_ia32_task())
766 return __NR_ia32_restart_syscall;
767 #endif
768 #ifdef CONFIG_X86_X32_ABI
769 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
770 #else
771 return __NR_restart_syscall;
772 #endif
776 * Note that 'init' is a special process: it doesn't get signals it doesn't
777 * want to handle. Thus you cannot kill init even with a SIGKILL even by
778 * mistake.
780 void do_signal(struct pt_regs *regs)
782 struct ksignal ksig;
784 if (get_signal(&ksig)) {
785 /* Whee! Actually deliver the signal. */
786 handle_signal(&ksig, regs);
787 return;
790 /* Did we come from a system call? */
791 if (syscall_get_nr(current, regs) >= 0) {
792 /* Restart the system call - no handlers present */
793 switch (syscall_get_error(current, regs)) {
794 case -ERESTARTNOHAND:
795 case -ERESTARTSYS:
796 case -ERESTARTNOINTR:
797 regs->ax = regs->orig_ax;
798 regs->ip -= 2;
799 break;
801 case -ERESTART_RESTARTBLOCK:
802 regs->ax = get_nr_restart_syscall(regs);
803 regs->ip -= 2;
804 break;
809 * If there's no signal to deliver, we just put the saved sigmask
810 * back.
812 restore_saved_sigmask();
815 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
817 struct task_struct *me = current;
819 if (show_unhandled_signals && printk_ratelimit()) {
820 printk("%s"
821 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
822 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
823 me->comm, me->pid, where, frame,
824 regs->ip, regs->sp, regs->orig_ax);
825 print_vma_addr(" in ", regs->ip);
826 pr_cont("\n");
829 force_sig(SIGSEGV, me);
832 #ifdef CONFIG_X86_X32_ABI
833 asmlinkage long sys32_x32_rt_sigreturn(void)
835 struct pt_regs *regs = current_pt_regs();
836 struct rt_sigframe_x32 __user *frame;
837 sigset_t set;
838 unsigned long uc_flags;
840 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
842 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
843 goto badframe;
844 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
845 goto badframe;
846 if (__get_user(uc_flags, &frame->uc.uc_flags))
847 goto badframe;
849 set_current_blocked(&set);
851 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
852 goto badframe;
854 if (compat_restore_altstack(&frame->uc.uc_stack))
855 goto badframe;
857 return regs->ax;
859 badframe:
860 signal_fault(regs, frame, "x32 rt_sigreturn");
861 return 0;
863 #endif