First Support on Ginger and OMAP TI
[linux-ginger.git] / arch / x86 / kernel / signal.c
blob6a44a76055adcb781572978d1e332a9923f7e2d6
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 */
9 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/tracehook.h>
18 #include <linux/unistd.h>
19 #include <linux/stddef.h>
20 #include <linux/personality.h>
21 #include <linux/uaccess.h>
23 #include <asm/processor.h>
24 #include <asm/ucontext.h>
25 #include <asm/i387.h>
26 #include <asm/vdso.h>
27 #include <asm/mce.h>
29 #ifdef CONFIG_X86_64
30 #include <asm/proto.h>
31 #include <asm/ia32_unistd.h>
32 #endif /* CONFIG_X86_64 */
34 #include <asm/syscall.h>
35 #include <asm/syscalls.h>
37 #include <asm/sigframe.h>
39 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
42 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
43 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
44 X86_EFLAGS_CF)
46 #ifdef CONFIG_X86_32
47 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
48 #else
49 # define FIX_EFLAGS __FIX_EFLAGS
50 #endif
52 #define COPY(x) do { \
53 get_user_ex(regs->x, &sc->x); \
54 } while (0)
56 #define GET_SEG(seg) ({ \
57 unsigned short tmp; \
58 get_user_ex(tmp, &sc->seg); \
59 tmp; \
62 #define COPY_SEG(seg) do { \
63 regs->seg = GET_SEG(seg); \
64 } while (0)
66 #define COPY_SEG_CPL3(seg) do { \
67 regs->seg = GET_SEG(seg) | 3; \
68 } while (0)
70 static int
71 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
72 unsigned long *pax)
74 void __user *buf;
75 unsigned int tmpflags;
76 unsigned int err = 0;
78 /* Always make any pending restarted system calls return -EINTR */
79 current_thread_info()->restart_block.fn = do_no_restart_syscall;
81 get_user_try {
83 #ifdef CONFIG_X86_32
84 set_user_gs(regs, GET_SEG(gs));
85 COPY_SEG(fs);
86 COPY_SEG(es);
87 COPY_SEG(ds);
88 #endif /* CONFIG_X86_32 */
90 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
91 COPY(dx); COPY(cx); COPY(ip);
93 #ifdef CONFIG_X86_64
94 COPY(r8);
95 COPY(r9);
96 COPY(r10);
97 COPY(r11);
98 COPY(r12);
99 COPY(r13);
100 COPY(r14);
101 COPY(r15);
102 #endif /* CONFIG_X86_64 */
104 #ifdef CONFIG_X86_32
105 COPY_SEG_CPL3(cs);
106 COPY_SEG_CPL3(ss);
107 #else /* !CONFIG_X86_32 */
108 /* Kernel saves and restores only the CS segment register on signals,
109 * which is the bare minimum needed to allow mixed 32/64-bit code.
110 * App's signal handler can save/restore other segments if needed. */
111 COPY_SEG_CPL3(cs);
112 #endif /* CONFIG_X86_32 */
114 get_user_ex(tmpflags, &sc->flags);
115 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
116 regs->orig_ax = -1; /* disable syscall checks */
118 get_user_ex(buf, &sc->fpstate);
119 err |= restore_i387_xstate(buf);
121 get_user_ex(*pax, &sc->ax);
122 } get_user_catch(err);
124 return err;
127 static int
128 setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
129 struct pt_regs *regs, unsigned long mask)
131 int err = 0;
133 put_user_try {
135 #ifdef CONFIG_X86_32
136 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
137 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
138 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
139 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
140 #endif /* CONFIG_X86_32 */
142 put_user_ex(regs->di, &sc->di);
143 put_user_ex(regs->si, &sc->si);
144 put_user_ex(regs->bp, &sc->bp);
145 put_user_ex(regs->sp, &sc->sp);
146 put_user_ex(regs->bx, &sc->bx);
147 put_user_ex(regs->dx, &sc->dx);
148 put_user_ex(regs->cx, &sc->cx);
149 put_user_ex(regs->ax, &sc->ax);
150 #ifdef CONFIG_X86_64
151 put_user_ex(regs->r8, &sc->r8);
152 put_user_ex(regs->r9, &sc->r9);
153 put_user_ex(regs->r10, &sc->r10);
154 put_user_ex(regs->r11, &sc->r11);
155 put_user_ex(regs->r12, &sc->r12);
156 put_user_ex(regs->r13, &sc->r13);
157 put_user_ex(regs->r14, &sc->r14);
158 put_user_ex(regs->r15, &sc->r15);
159 #endif /* CONFIG_X86_64 */
161 put_user_ex(current->thread.trap_no, &sc->trapno);
162 put_user_ex(current->thread.error_code, &sc->err);
163 put_user_ex(regs->ip, &sc->ip);
164 #ifdef CONFIG_X86_32
165 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
166 put_user_ex(regs->flags, &sc->flags);
167 put_user_ex(regs->sp, &sc->sp_at_signal);
168 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
169 #else /* !CONFIG_X86_32 */
170 put_user_ex(regs->flags, &sc->flags);
171 put_user_ex(regs->cs, &sc->cs);
172 put_user_ex(0, &sc->gs);
173 put_user_ex(0, &sc->fs);
174 #endif /* CONFIG_X86_32 */
176 put_user_ex(fpstate, &sc->fpstate);
178 /* non-iBCS2 extensions.. */
179 put_user_ex(mask, &sc->oldmask);
180 put_user_ex(current->thread.cr2, &sc->cr2);
181 } put_user_catch(err);
183 return err;
187 * Set up a signal frame.
191 * Determine which stack to use..
193 static unsigned long align_sigframe(unsigned long sp)
195 #ifdef CONFIG_X86_32
197 * Align the stack pointer according to the i386 ABI,
198 * i.e. so that on function entry ((sp + 4) & 15) == 0.
200 sp = ((sp + 4) & -16ul) - 4;
201 #else /* !CONFIG_X86_32 */
202 sp = round_down(sp, 16) - 8;
203 #endif
204 return sp;
207 static inline void __user *
208 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
209 void __user **fpstate)
211 /* Default to using normal stack */
212 unsigned long sp = regs->sp;
213 int onsigstack = on_sig_stack(sp);
215 #ifdef CONFIG_X86_64
216 /* redzone */
217 sp -= 128;
218 #endif /* CONFIG_X86_64 */
220 if (!onsigstack) {
221 /* This is the X/Open sanctioned signal stack switching. */
222 if (ka->sa.sa_flags & SA_ONSTACK) {
223 if (current->sas_ss_size)
224 sp = current->sas_ss_sp + current->sas_ss_size;
225 } else {
226 #ifdef CONFIG_X86_32
227 /* This is the legacy signal stack switching. */
228 if ((regs->ss & 0xffff) != __USER_DS &&
229 !(ka->sa.sa_flags & SA_RESTORER) &&
230 ka->sa.sa_restorer)
231 sp = (unsigned long) ka->sa.sa_restorer;
232 #endif /* CONFIG_X86_32 */
236 if (used_math()) {
237 sp -= sig_xstate_size;
238 #ifdef CONFIG_X86_64
239 sp = round_down(sp, 64);
240 #endif /* CONFIG_X86_64 */
241 *fpstate = (void __user *)sp;
244 sp = align_sigframe(sp - frame_size);
247 * If we are on the alternate signal stack and would overflow it, don't.
248 * Return an always-bogus address instead so we will die with SIGSEGV.
250 if (onsigstack && !likely(on_sig_stack(sp)))
251 return (void __user *)-1L;
253 /* save i387 state */
254 if (used_math() && save_i387_xstate(*fpstate) < 0)
255 return (void __user *)-1L;
257 return (void __user *)sp;
260 #ifdef CONFIG_X86_32
261 static const struct {
262 u16 poplmovl;
263 u32 val;
264 u16 int80;
265 } __attribute__((packed)) retcode = {
266 0xb858, /* popl %eax; movl $..., %eax */
267 __NR_sigreturn,
268 0x80cd, /* int $0x80 */
271 static const struct {
272 u8 movl;
273 u32 val;
274 u16 int80;
275 u8 pad;
276 } __attribute__((packed)) rt_retcode = {
277 0xb8, /* movl $..., %eax */
278 __NR_rt_sigreturn,
279 0x80cd, /* int $0x80 */
283 static int
284 __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
285 struct pt_regs *regs)
287 struct sigframe __user *frame;
288 void __user *restorer;
289 int err = 0;
290 void __user *fpstate = NULL;
292 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
294 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
295 return -EFAULT;
297 if (__put_user(sig, &frame->sig))
298 return -EFAULT;
300 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
301 return -EFAULT;
303 if (_NSIG_WORDS > 1) {
304 if (__copy_to_user(&frame->extramask, &set->sig[1],
305 sizeof(frame->extramask)))
306 return -EFAULT;
309 if (current->mm->context.vdso)
310 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
311 else
312 restorer = &frame->retcode;
313 if (ka->sa.sa_flags & SA_RESTORER)
314 restorer = ka->sa.sa_restorer;
316 /* Set up to return from userspace. */
317 err |= __put_user(restorer, &frame->pretcode);
320 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
322 * WE DO NOT USE IT ANY MORE! It's only left here for historical
323 * reasons and because gdb uses it as a signature to notice
324 * signal handler stack frames.
326 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
328 if (err)
329 return -EFAULT;
331 /* Set up registers for signal handler */
332 regs->sp = (unsigned long)frame;
333 regs->ip = (unsigned long)ka->sa.sa_handler;
334 regs->ax = (unsigned long)sig;
335 regs->dx = 0;
336 regs->cx = 0;
338 regs->ds = __USER_DS;
339 regs->es = __USER_DS;
340 regs->ss = __USER_DS;
341 regs->cs = __USER_CS;
343 return 0;
346 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
347 sigset_t *set, struct pt_regs *regs)
349 struct rt_sigframe __user *frame;
350 void __user *restorer;
351 int err = 0;
352 void __user *fpstate = NULL;
354 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
356 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
357 return -EFAULT;
359 put_user_try {
360 put_user_ex(sig, &frame->sig);
361 put_user_ex(&frame->info, &frame->pinfo);
362 put_user_ex(&frame->uc, &frame->puc);
363 err |= copy_siginfo_to_user(&frame->info, info);
365 /* Create the ucontext. */
366 if (cpu_has_xsave)
367 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
368 else
369 put_user_ex(0, &frame->uc.uc_flags);
370 put_user_ex(0, &frame->uc.uc_link);
371 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
372 put_user_ex(sas_ss_flags(regs->sp),
373 &frame->uc.uc_stack.ss_flags);
374 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
375 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
376 regs, set->sig[0]);
377 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
379 /* Set up to return from userspace. */
380 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
381 if (ka->sa.sa_flags & SA_RESTORER)
382 restorer = ka->sa.sa_restorer;
383 put_user_ex(restorer, &frame->pretcode);
386 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
388 * WE DO NOT USE IT ANY MORE! It's only left here for historical
389 * reasons and because gdb uses it as a signature to notice
390 * signal handler stack frames.
392 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
393 } put_user_catch(err);
395 if (err)
396 return -EFAULT;
398 /* Set up registers for signal handler */
399 regs->sp = (unsigned long)frame;
400 regs->ip = (unsigned long)ka->sa.sa_handler;
401 regs->ax = (unsigned long)sig;
402 regs->dx = (unsigned long)&frame->info;
403 regs->cx = (unsigned long)&frame->uc;
405 regs->ds = __USER_DS;
406 regs->es = __USER_DS;
407 regs->ss = __USER_DS;
408 regs->cs = __USER_CS;
410 return 0;
412 #else /* !CONFIG_X86_32 */
413 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
414 sigset_t *set, struct pt_regs *regs)
416 struct rt_sigframe __user *frame;
417 void __user *fp = NULL;
418 int err = 0;
419 struct task_struct *me = current;
421 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
423 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
424 return -EFAULT;
426 if (ka->sa.sa_flags & SA_SIGINFO) {
427 if (copy_siginfo_to_user(&frame->info, info))
428 return -EFAULT;
431 put_user_try {
432 /* Create the ucontext. */
433 if (cpu_has_xsave)
434 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
435 else
436 put_user_ex(0, &frame->uc.uc_flags);
437 put_user_ex(0, &frame->uc.uc_link);
438 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
439 put_user_ex(sas_ss_flags(regs->sp),
440 &frame->uc.uc_stack.ss_flags);
441 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
442 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
443 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
445 /* Set up to return from userspace. If provided, use a stub
446 already in userspace. */
447 /* x86-64 should always use SA_RESTORER. */
448 if (ka->sa.sa_flags & SA_RESTORER) {
449 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
450 } else {
451 /* could use a vstub here */
452 err |= -EFAULT;
454 } put_user_catch(err);
456 if (err)
457 return -EFAULT;
459 /* Set up registers for signal handler */
460 regs->di = sig;
461 /* In case the signal handler was declared without prototypes */
462 regs->ax = 0;
464 /* This also works for non SA_SIGINFO handlers because they expect the
465 next argument after the signal number on the stack. */
466 regs->si = (unsigned long)&frame->info;
467 regs->dx = (unsigned long)&frame->uc;
468 regs->ip = (unsigned long) ka->sa.sa_handler;
470 regs->sp = (unsigned long)frame;
472 /* Set up the CS register to run signal handlers in 64-bit mode,
473 even if the handler happens to be interrupting 32-bit code. */
474 regs->cs = __USER_CS;
476 return 0;
478 #endif /* CONFIG_X86_32 */
480 #ifdef CONFIG_X86_32
482 * Atomically swap in the new signal mask, and wait for a signal.
484 asmlinkage int
485 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
487 mask &= _BLOCKABLE;
488 spin_lock_irq(&current->sighand->siglock);
489 current->saved_sigmask = current->blocked;
490 siginitset(&current->blocked, mask);
491 recalc_sigpending();
492 spin_unlock_irq(&current->sighand->siglock);
494 current->state = TASK_INTERRUPTIBLE;
495 schedule();
496 set_restore_sigmask();
498 return -ERESTARTNOHAND;
501 asmlinkage int
502 sys_sigaction(int sig, const struct old_sigaction __user *act,
503 struct old_sigaction __user *oact)
505 struct k_sigaction new_ka, old_ka;
506 int ret = 0;
508 if (act) {
509 old_sigset_t mask;
511 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
512 return -EFAULT;
514 get_user_try {
515 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
516 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
517 get_user_ex(mask, &act->sa_mask);
518 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
519 } get_user_catch(ret);
521 if (ret)
522 return -EFAULT;
523 siginitset(&new_ka.sa.sa_mask, mask);
526 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
528 if (!ret && oact) {
529 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
530 return -EFAULT;
532 put_user_try {
533 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
534 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
535 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
536 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
537 } put_user_catch(ret);
539 if (ret)
540 return -EFAULT;
543 return ret;
545 #endif /* CONFIG_X86_32 */
547 #ifdef CONFIG_X86_32
548 int sys_sigaltstack(struct pt_regs *regs)
550 const stack_t __user *uss = (const stack_t __user *)regs->bx;
551 stack_t __user *uoss = (stack_t __user *)regs->cx;
553 return do_sigaltstack(uss, uoss, regs->sp);
555 #else /* !CONFIG_X86_32 */
556 asmlinkage long
557 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
558 struct pt_regs *regs)
560 return do_sigaltstack(uss, uoss, regs->sp);
562 #endif /* CONFIG_X86_32 */
565 * Do a signal return; undo the signal stack.
567 #ifdef CONFIG_X86_32
568 unsigned long sys_sigreturn(struct pt_regs *regs)
570 struct sigframe __user *frame;
571 unsigned long ax;
572 sigset_t set;
574 frame = (struct sigframe __user *)(regs->sp - 8);
576 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
577 goto badframe;
578 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
579 && __copy_from_user(&set.sig[1], &frame->extramask,
580 sizeof(frame->extramask))))
581 goto badframe;
583 sigdelsetmask(&set, ~_BLOCKABLE);
584 spin_lock_irq(&current->sighand->siglock);
585 current->blocked = set;
586 recalc_sigpending();
587 spin_unlock_irq(&current->sighand->siglock);
589 if (restore_sigcontext(regs, &frame->sc, &ax))
590 goto badframe;
591 return ax;
593 badframe:
594 signal_fault(regs, frame, "sigreturn");
596 return 0;
598 #endif /* CONFIG_X86_32 */
600 long sys_rt_sigreturn(struct pt_regs *regs)
602 struct rt_sigframe __user *frame;
603 unsigned long ax;
604 sigset_t set;
606 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
607 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
608 goto badframe;
609 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
610 goto badframe;
612 sigdelsetmask(&set, ~_BLOCKABLE);
613 spin_lock_irq(&current->sighand->siglock);
614 current->blocked = set;
615 recalc_sigpending();
616 spin_unlock_irq(&current->sighand->siglock);
618 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
619 goto badframe;
621 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
622 goto badframe;
624 return ax;
626 badframe:
627 signal_fault(regs, frame, "rt_sigreturn");
628 return 0;
632 * OK, we're invoking a handler:
634 static int signr_convert(int sig)
636 #ifdef CONFIG_X86_32
637 struct thread_info *info = current_thread_info();
639 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
640 return info->exec_domain->signal_invmap[sig];
641 #endif /* CONFIG_X86_32 */
642 return sig;
645 #ifdef CONFIG_X86_32
647 #define is_ia32 1
648 #define ia32_setup_frame __setup_frame
649 #define ia32_setup_rt_frame __setup_rt_frame
651 #else /* !CONFIG_X86_32 */
653 #ifdef CONFIG_IA32_EMULATION
654 #define is_ia32 test_thread_flag(TIF_IA32)
655 #else /* !CONFIG_IA32_EMULATION */
656 #define is_ia32 0
657 #endif /* CONFIG_IA32_EMULATION */
659 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
660 sigset_t *set, struct pt_regs *regs);
661 int ia32_setup_frame(int sig, struct k_sigaction *ka,
662 sigset_t *set, struct pt_regs *regs);
664 #endif /* CONFIG_X86_32 */
666 static int
667 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
668 sigset_t *set, struct pt_regs *regs)
670 int usig = signr_convert(sig);
671 int ret;
673 /* Set up the stack frame */
674 if (is_ia32) {
675 if (ka->sa.sa_flags & SA_SIGINFO)
676 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
677 else
678 ret = ia32_setup_frame(usig, ka, set, regs);
679 } else
680 ret = __setup_rt_frame(sig, ka, info, set, regs);
682 if (ret) {
683 force_sigsegv(sig, current);
684 return -EFAULT;
687 return ret;
690 static int
691 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
692 sigset_t *oldset, struct pt_regs *regs)
694 int ret;
696 /* Are we from a system call? */
697 if (syscall_get_nr(current, regs) >= 0) {
698 /* If so, check system call restarting.. */
699 switch (syscall_get_error(current, regs)) {
700 case -ERESTART_RESTARTBLOCK:
701 case -ERESTARTNOHAND:
702 regs->ax = -EINTR;
703 break;
705 case -ERESTARTSYS:
706 if (!(ka->sa.sa_flags & SA_RESTART)) {
707 regs->ax = -EINTR;
708 break;
710 /* fallthrough */
711 case -ERESTARTNOINTR:
712 regs->ax = regs->orig_ax;
713 regs->ip -= 2;
714 break;
719 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
720 * flag so that register information in the sigcontext is correct.
722 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
723 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
724 regs->flags &= ~X86_EFLAGS_TF;
726 ret = setup_rt_frame(sig, ka, info, oldset, regs);
728 if (ret)
729 return ret;
731 #ifdef CONFIG_X86_64
733 * This has nothing to do with segment registers,
734 * despite the name. This magic affects uaccess.h
735 * macros' behavior. Reset it to the normal setting.
737 set_fs(USER_DS);
738 #endif
741 * Clear the direction flag as per the ABI for function entry.
743 regs->flags &= ~X86_EFLAGS_DF;
746 * Clear TF when entering the signal handler, but
747 * notify any tracer that was single-stepping it.
748 * The tracer may want to single-step inside the
749 * handler too.
751 regs->flags &= ~X86_EFLAGS_TF;
753 spin_lock_irq(&current->sighand->siglock);
754 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
755 if (!(ka->sa.sa_flags & SA_NODEFER))
756 sigaddset(&current->blocked, sig);
757 recalc_sigpending();
758 spin_unlock_irq(&current->sighand->siglock);
760 tracehook_signal_handler(sig, info, ka, regs,
761 test_thread_flag(TIF_SINGLESTEP));
763 return 0;
766 #ifdef CONFIG_X86_32
767 #define NR_restart_syscall __NR_restart_syscall
768 #else /* !CONFIG_X86_32 */
769 #define NR_restart_syscall \
770 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
771 #endif /* CONFIG_X86_32 */
774 * Note that 'init' is a special process: it doesn't get signals it doesn't
775 * want to handle. Thus you cannot kill init even with a SIGKILL even by
776 * mistake.
778 static void do_signal(struct pt_regs *regs)
780 struct k_sigaction ka;
781 siginfo_t info;
782 int signr;
783 sigset_t *oldset;
786 * We want the common case to go fast, which is why we may in certain
787 * cases get here from kernel mode. Just return without doing anything
788 * if so.
789 * X86_32: vm86 regs switched out by assembly code before reaching
790 * here, so testing against kernel CS suffices.
792 if (!user_mode(regs))
793 return;
795 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
796 oldset = &current->saved_sigmask;
797 else
798 oldset = &current->blocked;
800 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
801 if (signr > 0) {
803 * Re-enable any watchpoints before delivering the
804 * signal to user space. The processor register will
805 * have been cleared if the watchpoint triggered
806 * inside the kernel.
808 if (current->thread.debugreg7)
809 set_debugreg(current->thread.debugreg7, 7);
811 /* Whee! Actually deliver the signal. */
812 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
814 * A signal was successfully delivered; the saved
815 * sigmask will have been stored in the signal frame,
816 * and will be restored by sigreturn, so we can simply
817 * clear the TS_RESTORE_SIGMASK flag.
819 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
821 return;
824 /* Did we come from a system call? */
825 if (syscall_get_nr(current, regs) >= 0) {
826 /* Restart the system call - no handlers present */
827 switch (syscall_get_error(current, regs)) {
828 case -ERESTARTNOHAND:
829 case -ERESTARTSYS:
830 case -ERESTARTNOINTR:
831 regs->ax = regs->orig_ax;
832 regs->ip -= 2;
833 break;
835 case -ERESTART_RESTARTBLOCK:
836 regs->ax = NR_restart_syscall;
837 regs->ip -= 2;
838 break;
843 * If there's no signal to deliver, we just put the saved sigmask
844 * back.
846 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
847 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
848 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
853 * notification of userspace execution resumption
854 * - triggered by the TIF_WORK_MASK flags
856 void
857 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
859 #ifdef CONFIG_X86_MCE
860 /* notify userspace of pending MCEs */
861 if (thread_info_flags & _TIF_MCE_NOTIFY)
862 mce_notify_process();
863 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
865 /* deal with pending signal delivery */
866 if (thread_info_flags & _TIF_SIGPENDING)
867 do_signal(regs);
869 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
870 clear_thread_flag(TIF_NOTIFY_RESUME);
871 tracehook_notify_resume(regs);
872 if (current->replacement_session_keyring)
873 key_replace_session_keyring();
876 #ifdef CONFIG_X86_32
877 clear_thread_flag(TIF_IRET);
878 #endif /* CONFIG_X86_32 */
881 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
883 struct task_struct *me = current;
885 if (show_unhandled_signals && printk_ratelimit()) {
886 printk("%s"
887 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
888 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
889 me->comm, me->pid, where, frame,
890 regs->ip, regs->sp, regs->orig_ax);
891 print_vma_addr(" in ", regs->ip);
892 printk(KERN_CONT "\n");
895 force_sig(SIGSEGV, me);