x86: unify appropriate bits from dumpstack_32 and dumpstack_64
[linux/fpc-iii.git] / arch / x86 / kernel / signal_32.c
blobd6dd057d0f2210784023ee1c591ed4f660e837e2
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
4 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
5 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
6 */
7 #include <linux/list.h>
9 #include <linux/personality.h>
10 #include <linux/binfmts.h>
11 #include <linux/suspend.h>
12 #include <linux/kernel.h>
13 #include <linux/ptrace.h>
14 #include <linux/signal.h>
15 #include <linux/stddef.h>
16 #include <linux/unistd.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/tracehook.h>
21 #include <linux/elf.h>
22 #include <linux/smp.h>
23 #include <linux/mm.h>
25 #include <asm/processor.h>
26 #include <asm/ucontext.h>
27 #include <asm/uaccess.h>
28 #include <asm/i387.h>
29 #include <asm/vdso.h>
30 #include <asm/syscall.h>
31 #include <asm/syscalls.h>
33 #include "sigframe.h"
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
38 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
39 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
40 X86_EFLAGS_CF)
42 #ifdef CONFIG_X86_32
43 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
44 #else
45 # define FIX_EFLAGS __FIX_EFLAGS
46 #endif
49 * Atomically swap in the new signal mask, and wait for a signal.
51 asmlinkage int
52 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
54 mask &= _BLOCKABLE;
55 spin_lock_irq(&current->sighand->siglock);
56 current->saved_sigmask = current->blocked;
57 siginitset(&current->blocked, mask);
58 recalc_sigpending();
59 spin_unlock_irq(&current->sighand->siglock);
61 current->state = TASK_INTERRUPTIBLE;
62 schedule();
63 set_restore_sigmask();
65 return -ERESTARTNOHAND;
68 asmlinkage int
69 sys_sigaction(int sig, const struct old_sigaction __user *act,
70 struct old_sigaction __user *oact)
72 struct k_sigaction new_ka, old_ka;
73 int ret;
75 if (act) {
76 old_sigset_t mask;
78 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
79 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
80 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
81 return -EFAULT;
83 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
84 __get_user(mask, &act->sa_mask);
85 siginitset(&new_ka.sa.sa_mask, mask);
88 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
90 if (!ret && oact) {
91 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
92 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
93 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
94 return -EFAULT;
96 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
97 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
100 return ret;
103 asmlinkage int sys_sigaltstack(unsigned long bx)
106 * This is needed to make gcc realize it doesn't own the
107 * "struct pt_regs"
109 struct pt_regs *regs = (struct pt_regs *)&bx;
110 const stack_t __user *uss = (const stack_t __user *)bx;
111 stack_t __user *uoss = (stack_t __user *)regs->cx;
113 return do_sigaltstack(uss, uoss, regs->sp);
116 #define COPY(x) { \
117 err |= __get_user(regs->x, &sc->x); \
120 #define COPY_SEG(seg) { \
121 unsigned short tmp; \
122 err |= __get_user(tmp, &sc->seg); \
123 regs->seg = tmp; \
126 #define COPY_SEG_STRICT(seg) { \
127 unsigned short tmp; \
128 err |= __get_user(tmp, &sc->seg); \
129 regs->seg = tmp | 3; \
132 #define GET_SEG(seg) { \
133 unsigned short tmp; \
134 err |= __get_user(tmp, &sc->seg); \
135 loadsegment(seg, tmp); \
139 * Do a signal return; undo the signal stack.
141 static int
142 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
143 unsigned long *pax)
145 void __user *buf;
146 unsigned int tmpflags;
147 unsigned int err = 0;
149 /* Always make any pending restarted system calls return -EINTR */
150 current_thread_info()->restart_block.fn = do_no_restart_syscall;
152 GET_SEG(gs);
153 COPY_SEG(fs);
154 COPY_SEG(es);
155 COPY_SEG(ds);
156 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
157 COPY(dx); COPY(cx); COPY(ip);
158 COPY_SEG_STRICT(cs);
159 COPY_SEG_STRICT(ss);
161 err |= __get_user(tmpflags, &sc->flags);
162 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
163 regs->orig_ax = -1; /* disable syscall checks */
165 err |= __get_user(buf, &sc->fpstate);
166 err |= restore_i387_xstate(buf);
168 err |= __get_user(*pax, &sc->ax);
169 return err;
172 asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
174 struct sigframe __user *frame;
175 struct pt_regs *regs;
176 unsigned long ax;
177 sigset_t set;
179 regs = (struct pt_regs *) &__unused;
180 frame = (struct sigframe __user *)(regs->sp - 8);
182 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
183 goto badframe;
184 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
185 && __copy_from_user(&set.sig[1], &frame->extramask,
186 sizeof(frame->extramask))))
187 goto badframe;
189 sigdelsetmask(&set, ~_BLOCKABLE);
190 spin_lock_irq(&current->sighand->siglock);
191 current->blocked = set;
192 recalc_sigpending();
193 spin_unlock_irq(&current->sighand->siglock);
195 if (restore_sigcontext(regs, &frame->sc, &ax))
196 goto badframe;
197 return ax;
199 badframe:
200 if (show_unhandled_signals && printk_ratelimit()) {
201 printk("%s%s[%d] bad frame in sigreturn frame:"
202 "%p ip:%lx sp:%lx oeax:%lx",
203 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
204 current->comm, task_pid_nr(current), frame, regs->ip,
205 regs->sp, regs->orig_ax);
206 print_vma_addr(" in ", regs->ip);
207 printk(KERN_CONT "\n");
210 force_sig(SIGSEGV, current);
212 return 0;
215 static long do_rt_sigreturn(struct pt_regs *regs)
217 struct rt_sigframe __user *frame;
218 unsigned long ax;
219 sigset_t set;
221 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
222 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
223 goto badframe;
224 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
225 goto badframe;
227 sigdelsetmask(&set, ~_BLOCKABLE);
228 spin_lock_irq(&current->sighand->siglock);
229 current->blocked = set;
230 recalc_sigpending();
231 spin_unlock_irq(&current->sighand->siglock);
233 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
234 goto badframe;
236 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
237 goto badframe;
239 return ax;
241 badframe:
242 signal_fault(regs, frame, "rt_sigreturn");
243 return 0;
246 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
248 struct pt_regs *regs = (struct pt_regs *)&__unused;
250 return do_rt_sigreturn(regs);
254 * Set up a signal frame.
256 static int
257 setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
258 struct pt_regs *regs, unsigned long mask)
260 int tmp, err = 0;
262 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
263 savesegment(gs, tmp);
264 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
266 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
267 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
268 err |= __put_user(regs->di, &sc->di);
269 err |= __put_user(regs->si, &sc->si);
270 err |= __put_user(regs->bp, &sc->bp);
271 err |= __put_user(regs->sp, &sc->sp);
272 err |= __put_user(regs->bx, &sc->bx);
273 err |= __put_user(regs->dx, &sc->dx);
274 err |= __put_user(regs->cx, &sc->cx);
275 err |= __put_user(regs->ax, &sc->ax);
276 err |= __put_user(current->thread.trap_no, &sc->trapno);
277 err |= __put_user(current->thread.error_code, &sc->err);
278 err |= __put_user(regs->ip, &sc->ip);
279 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
280 err |= __put_user(regs->flags, &sc->flags);
281 err |= __put_user(regs->sp, &sc->sp_at_signal);
282 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
284 tmp = save_i387_xstate(fpstate);
285 if (tmp < 0)
286 err = 1;
287 else
288 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
290 /* non-iBCS2 extensions.. */
291 err |= __put_user(mask, &sc->oldmask);
292 err |= __put_user(current->thread.cr2, &sc->cr2);
294 return err;
298 * Determine which stack to use..
300 static inline void __user *
301 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
302 void **fpstate)
304 unsigned long sp;
306 /* Default to using normal stack */
307 sp = regs->sp;
310 * If we are on the alternate signal stack and would overflow it, don't.
311 * Return an always-bogus address instead so we will die with SIGSEGV.
313 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
314 return (void __user *) -1L;
316 /* This is the X/Open sanctioned signal stack switching. */
317 if (ka->sa.sa_flags & SA_ONSTACK) {
318 if (sas_ss_flags(sp) == 0)
319 sp = current->sas_ss_sp + current->sas_ss_size;
320 } else {
321 /* This is the legacy signal stack switching. */
322 if ((regs->ss & 0xffff) != __USER_DS &&
323 !(ka->sa.sa_flags & SA_RESTORER) &&
324 ka->sa.sa_restorer)
325 sp = (unsigned long) ka->sa.sa_restorer;
328 if (used_math()) {
329 sp = sp - sig_xstate_size;
330 *fpstate = (struct _fpstate *) sp;
333 sp -= frame_size;
335 * Align the stack pointer according to the i386 ABI,
336 * i.e. so that on function entry ((sp + 4) & 15) == 0.
338 sp = ((sp + 4) & -16ul) - 4;
340 return (void __user *) sp;
343 static int
344 __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
345 struct pt_regs *regs)
347 struct sigframe __user *frame;
348 void __user *restorer;
349 int err = 0;
350 void __user *fpstate = NULL;
352 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
354 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
355 return -EFAULT;
357 if (__put_user(sig, &frame->sig))
358 return -EFAULT;
360 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
361 return -EFAULT;
363 if (_NSIG_WORDS > 1) {
364 if (__copy_to_user(&frame->extramask, &set->sig[1],
365 sizeof(frame->extramask)))
366 return -EFAULT;
369 if (current->mm->context.vdso)
370 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
371 else
372 restorer = &frame->retcode;
373 if (ka->sa.sa_flags & SA_RESTORER)
374 restorer = ka->sa.sa_restorer;
376 /* Set up to return from userspace. */
377 err |= __put_user(restorer, &frame->pretcode);
380 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
382 * WE DO NOT USE IT ANY MORE! It's only left here for historical
383 * reasons and because gdb uses it as a signature to notice
384 * signal handler stack frames.
386 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
387 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
388 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
390 if (err)
391 return -EFAULT;
393 /* Set up registers for signal handler */
394 regs->sp = (unsigned long)frame;
395 regs->ip = (unsigned long)ka->sa.sa_handler;
396 regs->ax = (unsigned long)sig;
397 regs->dx = 0;
398 regs->cx = 0;
400 regs->ds = __USER_DS;
401 regs->es = __USER_DS;
402 regs->ss = __USER_DS;
403 regs->cs = __USER_CS;
405 return 0;
408 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
409 sigset_t *set, struct pt_regs *regs)
411 struct rt_sigframe __user *frame;
412 void __user *restorer;
413 int err = 0;
414 void __user *fpstate = NULL;
416 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
418 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
419 return -EFAULT;
421 err |= __put_user(sig, &frame->sig);
422 err |= __put_user(&frame->info, &frame->pinfo);
423 err |= __put_user(&frame->uc, &frame->puc);
424 err |= copy_siginfo_to_user(&frame->info, info);
425 if (err)
426 return -EFAULT;
428 /* Create the ucontext. */
429 if (cpu_has_xsave)
430 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
431 else
432 err |= __put_user(0, &frame->uc.uc_flags);
433 err |= __put_user(0, &frame->uc.uc_link);
434 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
435 err |= __put_user(sas_ss_flags(regs->sp),
436 &frame->uc.uc_stack.ss_flags);
437 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
438 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
439 regs, set->sig[0]);
440 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
441 if (err)
442 return -EFAULT;
444 /* Set up to return from userspace. */
445 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
446 if (ka->sa.sa_flags & SA_RESTORER)
447 restorer = ka->sa.sa_restorer;
448 err |= __put_user(restorer, &frame->pretcode);
451 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
453 * WE DO NOT USE IT ANY MORE! It's only left here for historical
454 * reasons and because gdb uses it as a signature to notice
455 * signal handler stack frames.
457 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
458 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
459 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
461 if (err)
462 return -EFAULT;
464 /* Set up registers for signal handler */
465 regs->sp = (unsigned long)frame;
466 regs->ip = (unsigned long)ka->sa.sa_handler;
467 regs->ax = (unsigned long)sig;
468 regs->dx = (unsigned long)&frame->info;
469 regs->cx = (unsigned long)&frame->uc;
471 regs->ds = __USER_DS;
472 regs->es = __USER_DS;
473 regs->ss = __USER_DS;
474 regs->cs = __USER_CS;
476 return 0;
480 * OK, we're invoking a handler:
482 static int signr_convert(int sig)
484 struct thread_info *info = current_thread_info();
486 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
487 return info->exec_domain->signal_invmap[sig];
488 return sig;
491 #define is_ia32 1
492 #define ia32_setup_frame __setup_frame
493 #define ia32_setup_rt_frame __setup_rt_frame
495 static int
496 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
497 sigset_t *set, struct pt_regs *regs)
499 int usig = signr_convert(sig);
500 int ret;
502 /* Set up the stack frame */
503 if (is_ia32) {
504 if (ka->sa.sa_flags & SA_SIGINFO)
505 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
506 else
507 ret = ia32_setup_frame(usig, ka, set, regs);
508 } else
509 ret = __setup_rt_frame(sig, ka, info, set, regs);
511 if (ret) {
512 force_sigsegv(sig, current);
513 return -EFAULT;
516 return ret;
519 static int
520 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
521 sigset_t *oldset, struct pt_regs *regs)
523 int ret;
525 /* Are we from a system call? */
526 if (syscall_get_nr(current, regs) >= 0) {
527 /* If so, check system call restarting.. */
528 switch (syscall_get_error(current, regs)) {
529 case -ERESTART_RESTARTBLOCK:
530 case -ERESTARTNOHAND:
531 regs->ax = -EINTR;
532 break;
534 case -ERESTARTSYS:
535 if (!(ka->sa.sa_flags & SA_RESTART)) {
536 regs->ax = -EINTR;
537 break;
539 /* fallthrough */
540 case -ERESTARTNOINTR:
541 regs->ax = regs->orig_ax;
542 regs->ip -= 2;
543 break;
548 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
549 * flag so that register information in the sigcontext is correct.
551 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
552 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
553 regs->flags &= ~X86_EFLAGS_TF;
555 ret = setup_rt_frame(sig, ka, info, oldset, regs);
557 if (ret)
558 return ret;
560 #ifdef CONFIG_X86_64
562 * This has nothing to do with segment registers,
563 * despite the name. This magic affects uaccess.h
564 * macros' behavior. Reset it to the normal setting.
566 set_fs(USER_DS);
567 #endif
570 * Clear the direction flag as per the ABI for function entry.
572 regs->flags &= ~X86_EFLAGS_DF;
575 * Clear TF when entering the signal handler, but
576 * notify any tracer that was single-stepping it.
577 * The tracer may want to single-step inside the
578 * handler too.
580 regs->flags &= ~X86_EFLAGS_TF;
582 spin_lock_irq(&current->sighand->siglock);
583 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
584 if (!(ka->sa.sa_flags & SA_NODEFER))
585 sigaddset(&current->blocked, sig);
586 recalc_sigpending();
587 spin_unlock_irq(&current->sighand->siglock);
589 tracehook_signal_handler(sig, info, ka, regs,
590 test_thread_flag(TIF_SINGLESTEP));
592 return 0;
595 #define NR_restart_syscall __NR_restart_syscall
597 * Note that 'init' is a special process: it doesn't get signals it doesn't
598 * want to handle. Thus you cannot kill init even with a SIGKILL even by
599 * mistake.
601 static void do_signal(struct pt_regs *regs)
603 struct k_sigaction ka;
604 siginfo_t info;
605 int signr;
606 sigset_t *oldset;
609 * We want the common case to go fast, which is why we may in certain
610 * cases get here from kernel mode. Just return without doing anything
611 * if so.
612 * X86_32: vm86 regs switched out by assembly code before reaching
613 * here, so testing against kernel CS suffices.
615 if (!user_mode(regs))
616 return;
618 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
619 oldset = &current->saved_sigmask;
620 else
621 oldset = &current->blocked;
623 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
624 if (signr > 0) {
626 * Re-enable any watchpoints before delivering the
627 * signal to user space. The processor register will
628 * have been cleared if the watchpoint triggered
629 * inside the kernel.
631 if (current->thread.debugreg7)
632 set_debugreg(current->thread.debugreg7, 7);
634 /* Whee! Actually deliver the signal. */
635 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
637 * A signal was successfully delivered; the saved
638 * sigmask will have been stored in the signal frame,
639 * and will be restored by sigreturn, so we can simply
640 * clear the TS_RESTORE_SIGMASK flag.
642 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
644 return;
647 /* Did we come from a system call? */
648 if (syscall_get_nr(current, regs) >= 0) {
649 /* Restart the system call - no handlers present */
650 switch (syscall_get_error(current, regs)) {
651 case -ERESTARTNOHAND:
652 case -ERESTARTSYS:
653 case -ERESTARTNOINTR:
654 regs->ax = regs->orig_ax;
655 regs->ip -= 2;
656 break;
658 case -ERESTART_RESTARTBLOCK:
659 regs->ax = NR_restart_syscall;
660 regs->ip -= 2;
661 break;
666 * If there's no signal to deliver, we just put the saved sigmask
667 * back.
669 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
670 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
671 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
676 * notification of userspace execution resumption
677 * - triggered by the TIF_WORK_MASK flags
679 void
680 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
682 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
683 /* notify userspace of pending MCEs */
684 if (thread_info_flags & _TIF_MCE_NOTIFY)
685 mce_notify_user();
686 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
688 /* deal with pending signal delivery */
689 if (thread_info_flags & _TIF_SIGPENDING)
690 do_signal(regs);
692 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
693 clear_thread_flag(TIF_NOTIFY_RESUME);
694 tracehook_notify_resume(regs);
697 #ifdef CONFIG_X86_32
698 clear_thread_flag(TIF_IRET);
699 #endif /* CONFIG_X86_32 */
702 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
704 struct task_struct *me = current;
706 if (show_unhandled_signals && printk_ratelimit()) {
707 printk(KERN_INFO
708 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
709 me->comm, me->pid, where, frame,
710 regs->ip, regs->sp, regs->orig_ax);
711 print_vma_addr(" in ", regs->ip);
712 printk(KERN_CONT "\n");
715 force_sig(SIGSEGV, me);