x86, xsave: reorganization of signal save/restore fpstate code layout
[linux-2.6/mini2440.git] / arch / x86 / kernel / signal_32.c
blob690cc616ac075abe7abffa12188c4e55e18aba31
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/elf.h>
21 #include <linux/smp.h>
22 #include <linux/mm.h>
24 #include <asm/processor.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/i387.h>
28 #include <asm/vdso.h>
30 #include "sigframe.h"
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
34 #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
35 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
36 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
37 X86_EFLAGS_CF)
39 #ifdef CONFIG_X86_32
40 # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
41 #else
42 # define FIX_EFLAGS __FIX_EFLAGS
43 #endif
46 * Atomically swap in the new signal mask, and wait for a signal.
48 asmlinkage int
49 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
51 mask &= _BLOCKABLE;
52 spin_lock_irq(&current->sighand->siglock);
53 current->saved_sigmask = current->blocked;
54 siginitset(&current->blocked, mask);
55 recalc_sigpending();
56 spin_unlock_irq(&current->sighand->siglock);
58 current->state = TASK_INTERRUPTIBLE;
59 schedule();
60 set_restore_sigmask();
62 return -ERESTARTNOHAND;
65 asmlinkage int
66 sys_sigaction(int sig, const struct old_sigaction __user *act,
67 struct old_sigaction __user *oact)
69 struct k_sigaction new_ka, old_ka;
70 int ret;
72 if (act) {
73 old_sigset_t mask;
75 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
76 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
77 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
78 return -EFAULT;
80 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
81 __get_user(mask, &act->sa_mask);
82 siginitset(&new_ka.sa.sa_mask, mask);
85 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
87 if (!ret && oact) {
88 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
89 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
90 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
91 return -EFAULT;
93 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
94 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
97 return ret;
100 asmlinkage int sys_sigaltstack(unsigned long bx)
103 * This is needed to make gcc realize it doesn't own the
104 * "struct pt_regs"
106 struct pt_regs *regs = (struct pt_regs *)&bx;
107 const stack_t __user *uss = (const stack_t __user *)bx;
108 stack_t __user *uoss = (stack_t __user *)regs->cx;
110 return do_sigaltstack(uss, uoss, regs->sp);
115 * Do a signal return; undo the signal stack.
117 static int
118 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
119 unsigned long *pax)
121 unsigned int err = 0;
123 /* Always make any pending restarted system calls return -EINTR */
124 current_thread_info()->restart_block.fn = do_no_restart_syscall;
126 #define COPY(x) err |= __get_user(regs->x, &sc->x)
128 #define COPY_SEG(seg) \
129 { unsigned short tmp; \
130 err |= __get_user(tmp, &sc->seg); \
131 regs->seg = tmp; }
133 #define COPY_SEG_STRICT(seg) \
134 { unsigned short tmp; \
135 err |= __get_user(tmp, &sc->seg); \
136 regs->seg = tmp|3; }
138 #define GET_SEG(seg) \
139 { unsigned short tmp; \
140 err |= __get_user(tmp, &sc->seg); \
141 loadsegment(seg, tmp); }
143 GET_SEG(gs);
144 COPY_SEG(fs);
145 COPY_SEG(es);
146 COPY_SEG(ds);
147 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
148 COPY(dx); COPY(cx); COPY(ip);
149 COPY_SEG_STRICT(cs);
150 COPY_SEG_STRICT(ss);
153 unsigned int tmpflags;
155 err |= __get_user(tmpflags, &sc->flags);
156 regs->flags = (regs->flags & ~FIX_EFLAGS) |
157 (tmpflags & FIX_EFLAGS);
158 regs->orig_ax = -1; /* disable syscall checks */
162 void __user *buf;
164 err |= __get_user(buf, &sc->fpstate);
165 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 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
217 struct pt_regs *regs = (struct pt_regs *)&__unused;
218 struct rt_sigframe __user *frame;
219 unsigned long ax;
220 sigset_t set;
222 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
223 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
224 goto badframe;
225 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
226 goto badframe;
228 sigdelsetmask(&set, ~_BLOCKABLE);
229 spin_lock_irq(&current->sighand->siglock);
230 current->blocked = set;
231 recalc_sigpending();
232 spin_unlock_irq(&current->sighand->siglock);
234 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
235 goto badframe;
237 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
238 goto badframe;
240 return ax;
242 badframe:
243 force_sig(SIGSEGV, current);
244 return 0;
248 * Set up a signal frame.
250 static int
251 setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
252 struct pt_regs *regs, unsigned long mask)
254 int tmp, err = 0;
256 err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
257 savesegment(gs, tmp);
258 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
260 err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
261 err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
262 err |= __put_user(regs->di, &sc->di);
263 err |= __put_user(regs->si, &sc->si);
264 err |= __put_user(regs->bp, &sc->bp);
265 err |= __put_user(regs->sp, &sc->sp);
266 err |= __put_user(regs->bx, &sc->bx);
267 err |= __put_user(regs->dx, &sc->dx);
268 err |= __put_user(regs->cx, &sc->cx);
269 err |= __put_user(regs->ax, &sc->ax);
270 err |= __put_user(current->thread.trap_no, &sc->trapno);
271 err |= __put_user(current->thread.error_code, &sc->err);
272 err |= __put_user(regs->ip, &sc->ip);
273 err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
274 err |= __put_user(regs->flags, &sc->flags);
275 err |= __put_user(regs->sp, &sc->sp_at_signal);
276 err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
278 tmp = save_i387_xstate(fpstate);
279 if (tmp < 0)
280 err = 1;
281 else
282 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
284 /* non-iBCS2 extensions.. */
285 err |= __put_user(mask, &sc->oldmask);
286 err |= __put_user(current->thread.cr2, &sc->cr2);
288 return err;
292 * Determine which stack to use..
294 static inline void __user *
295 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
296 void **fpstate)
298 unsigned long sp;
300 /* Default to using normal stack */
301 sp = regs->sp;
304 * If we are on the alternate signal stack and would overflow it, don't.
305 * Return an always-bogus address instead so we will die with SIGSEGV.
307 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
308 return (void __user *) -1L;
310 /* This is the X/Open sanctioned signal stack switching. */
311 if (ka->sa.sa_flags & SA_ONSTACK) {
312 if (sas_ss_flags(sp) == 0)
313 sp = current->sas_ss_sp + current->sas_ss_size;
314 } else {
315 /* This is the legacy signal stack switching. */
316 if ((regs->ss & 0xffff) != __USER_DS &&
317 !(ka->sa.sa_flags & SA_RESTORER) &&
318 ka->sa.sa_restorer)
319 sp = (unsigned long) ka->sa.sa_restorer;
322 if (used_math()) {
323 sp = sp - sig_xstate_size;
324 *fpstate = (struct _fpstate *) sp;
327 sp -= frame_size;
329 * Align the stack pointer according to the i386 ABI,
330 * i.e. so that on function entry ((sp + 4) & 15) == 0.
332 sp = ((sp + 4) & -16ul) - 4;
334 return (void __user *) sp;
337 static int
338 setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
339 struct pt_regs *regs)
341 struct sigframe __user *frame;
342 void __user *restorer;
343 int err = 0;
344 int usig;
345 void __user *fpstate = NULL;
347 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
349 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
350 goto give_sigsegv;
352 usig = current_thread_info()->exec_domain
353 && current_thread_info()->exec_domain->signal_invmap
354 && sig < 32
355 ? current_thread_info()->exec_domain->signal_invmap[sig]
356 : sig;
358 err = __put_user(usig, &frame->sig);
359 if (err)
360 goto give_sigsegv;
362 err = setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]);
363 if (err)
364 goto give_sigsegv;
366 if (_NSIG_WORDS > 1) {
367 err = __copy_to_user(&frame->extramask, &set->sig[1],
368 sizeof(frame->extramask));
369 if (err)
370 goto give_sigsegv;
373 if (current->mm->context.vdso)
374 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
375 else
376 restorer = &frame->retcode;
377 if (ka->sa.sa_flags & SA_RESTORER)
378 restorer = ka->sa.sa_restorer;
380 /* Set up to return from userspace. */
381 err |= __put_user(restorer, &frame->pretcode);
384 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
386 * WE DO NOT USE IT ANY MORE! It's only left here for historical
387 * reasons and because gdb uses it as a signature to notice
388 * signal handler stack frames.
390 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
391 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
392 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
394 if (err)
395 goto give_sigsegv;
397 /* Set up registers for signal handler */
398 regs->sp = (unsigned long)frame;
399 regs->ip = (unsigned long)ka->sa.sa_handler;
400 regs->ax = (unsigned long)sig;
401 regs->dx = 0;
402 regs->cx = 0;
404 regs->ds = __USER_DS;
405 regs->es = __USER_DS;
406 regs->ss = __USER_DS;
407 regs->cs = __USER_CS;
409 return 0;
411 give_sigsegv:
412 force_sigsegv(sig, current);
413 return -EFAULT;
416 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
417 sigset_t *set, struct pt_regs *regs)
419 struct rt_sigframe __user *frame;
420 void __user *restorer;
421 int err = 0;
422 int usig;
423 void __user *fpstate = NULL;
425 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
427 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
428 goto give_sigsegv;
430 usig = current_thread_info()->exec_domain
431 && current_thread_info()->exec_domain->signal_invmap
432 && sig < 32
433 ? current_thread_info()->exec_domain->signal_invmap[sig]
434 : sig;
436 err |= __put_user(usig, &frame->sig);
437 err |= __put_user(&frame->info, &frame->pinfo);
438 err |= __put_user(&frame->uc, &frame->puc);
439 err |= copy_siginfo_to_user(&frame->info, info);
440 if (err)
441 goto give_sigsegv;
443 /* Create the ucontext. */
444 err |= __put_user(0, &frame->uc.uc_flags);
445 err |= __put_user(0, &frame->uc.uc_link);
446 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
447 err |= __put_user(sas_ss_flags(regs->sp),
448 &frame->uc.uc_stack.ss_flags);
449 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
450 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
451 regs, set->sig[0]);
452 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
453 if (err)
454 goto give_sigsegv;
456 /* Set up to return from userspace. */
457 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
458 if (ka->sa.sa_flags & SA_RESTORER)
459 restorer = ka->sa.sa_restorer;
460 err |= __put_user(restorer, &frame->pretcode);
463 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
465 * WE DO NOT USE IT ANY MORE! It's only left here for historical
466 * reasons and because gdb uses it as a signature to notice
467 * signal handler stack frames.
469 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
470 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
471 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
473 if (err)
474 goto give_sigsegv;
476 /* Set up registers for signal handler */
477 regs->sp = (unsigned long)frame;
478 regs->ip = (unsigned long)ka->sa.sa_handler;
479 regs->ax = (unsigned long)usig;
480 regs->dx = (unsigned long)&frame->info;
481 regs->cx = (unsigned long)&frame->uc;
483 regs->ds = __USER_DS;
484 regs->es = __USER_DS;
485 regs->ss = __USER_DS;
486 regs->cs = __USER_CS;
488 return 0;
490 give_sigsegv:
491 force_sigsegv(sig, current);
492 return -EFAULT;
496 * OK, we're invoking a handler:
498 static int
499 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
500 sigset_t *oldset, struct pt_regs *regs)
502 int ret;
504 /* Are we from a system call? */
505 if ((long)regs->orig_ax >= 0) {
506 /* If so, check system call restarting.. */
507 switch (regs->ax) {
508 case -ERESTART_RESTARTBLOCK:
509 case -ERESTARTNOHAND:
510 regs->ax = -EINTR;
511 break;
513 case -ERESTARTSYS:
514 if (!(ka->sa.sa_flags & SA_RESTART)) {
515 regs->ax = -EINTR;
516 break;
518 /* fallthrough */
519 case -ERESTARTNOINTR:
520 regs->ax = regs->orig_ax;
521 regs->ip -= 2;
522 break;
527 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
528 * flag so that register information in the sigcontext is correct.
530 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
531 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
532 regs->flags &= ~X86_EFLAGS_TF;
534 /* Set up the stack frame */
535 if (ka->sa.sa_flags & SA_SIGINFO)
536 ret = setup_rt_frame(sig, ka, info, oldset, regs);
537 else
538 ret = setup_frame(sig, ka, oldset, regs);
540 if (ret)
541 return ret;
544 * Clear the direction flag as per the ABI for function entry.
546 regs->flags &= ~X86_EFLAGS_DF;
549 * Clear TF when entering the signal handler, but
550 * notify any tracer that was single-stepping it.
551 * The tracer may want to single-step inside the
552 * handler too.
554 regs->flags &= ~X86_EFLAGS_TF;
555 if (test_thread_flag(TIF_SINGLESTEP))
556 ptrace_notify(SIGTRAP);
558 spin_lock_irq(&current->sighand->siglock);
559 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
560 if (!(ka->sa.sa_flags & SA_NODEFER))
561 sigaddset(&current->blocked, sig);
562 recalc_sigpending();
563 spin_unlock_irq(&current->sighand->siglock);
565 return 0;
569 * Note that 'init' is a special process: it doesn't get signals it doesn't
570 * want to handle. Thus you cannot kill init even with a SIGKILL even by
571 * mistake.
573 static void do_signal(struct pt_regs *regs)
575 struct k_sigaction ka;
576 siginfo_t info;
577 int signr;
578 sigset_t *oldset;
581 * We want the common case to go fast, which is why we may in certain
582 * cases get here from kernel mode. Just return without doing anything
583 * if so.
584 * X86_32: vm86 regs switched out by assembly code before reaching
585 * here, so testing against kernel CS suffices.
587 if (!user_mode(regs))
588 return;
590 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
591 oldset = &current->saved_sigmask;
592 else
593 oldset = &current->blocked;
595 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
596 if (signr > 0) {
598 * Re-enable any watchpoints before delivering the
599 * signal to user space. The processor register will
600 * have been cleared if the watchpoint triggered
601 * inside the kernel.
603 if (current->thread.debugreg7)
604 set_debugreg(current->thread.debugreg7, 7);
606 /* Whee! Actually deliver the signal. */
607 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
609 * A signal was successfully delivered; the saved
610 * sigmask will have been stored in the signal frame,
611 * and will be restored by sigreturn, so we can simply
612 * clear the TS_RESTORE_SIGMASK flag.
614 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
616 return;
619 /* Did we come from a system call? */
620 if ((long)regs->orig_ax >= 0) {
621 /* Restart the system call - no handlers present */
622 switch (regs->ax) {
623 case -ERESTARTNOHAND:
624 case -ERESTARTSYS:
625 case -ERESTARTNOINTR:
626 regs->ax = regs->orig_ax;
627 regs->ip -= 2;
628 break;
630 case -ERESTART_RESTARTBLOCK:
631 regs->ax = __NR_restart_syscall;
632 regs->ip -= 2;
633 break;
638 * If there's no signal to deliver, we just put the saved sigmask
639 * back.
641 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
642 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
643 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
648 * notification of userspace execution resumption
649 * - triggered by the TIF_WORK_MASK flags
651 void
652 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
654 /* deal with pending signal delivery */
655 if (thread_info_flags & _TIF_SIGPENDING)
656 do_signal(regs);
658 clear_thread_flag(TIF_IRET);