2 * Copyright (C) 1994 Linus Torvalds
4 * Pentium III FXSR, SSE support
5 * General FPU state handling cleanups
6 * Gareth Hughes <gareth@valinux.com>, May 2000
8 #include <asm/fpu/internal.h>
11 * Track whether the kernel is using the FPU state
16 * - by IRQ context code to potentially use the FPU
19 * - to debug kernel_fpu_begin()/end() correctness
21 static DEFINE_PER_CPU(bool, in_kernel_fpu);
24 * Track which context is using the FPU on the CPU:
26 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
28 static void kernel_fpu_disable(void)
30 WARN_ON(this_cpu_read(in_kernel_fpu));
31 this_cpu_write(in_kernel_fpu, true);
34 static void kernel_fpu_enable(void)
36 WARN_ON_ONCE(!this_cpu_read(in_kernel_fpu));
37 this_cpu_write(in_kernel_fpu, false);
40 static bool kernel_fpu_disabled(void)
42 return this_cpu_read(in_kernel_fpu);
46 * Were we in an interrupt that interrupted kernel mode?
48 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
49 * pair does nothing at all: the thread must not have fpu (so
50 * that we don't try to save the FPU state), and TS must
51 * be set (so that the clts/stts pair does nothing that is
52 * visible in the interrupted kernel thread).
54 * Except for the eagerfpu case when we return true; in the likely case
55 * the thread has FPU but we are not going to set/clear TS.
57 static bool interrupted_kernel_fpu_idle(void)
59 if (kernel_fpu_disabled())
65 return !current->thread.fpu.fpregs_active && (read_cr0() & X86_CR0_TS);
69 * Were we in user mode (or vm86 mode) when we were
72 * Doing kernel_fpu_begin/end() is ok if we are running
73 * in an interrupt context from user mode - we'll just
74 * save the FPU state as required.
76 static bool interrupted_user_mode(void)
78 struct pt_regs *regs = get_irq_regs();
79 return regs && user_mode(regs);
83 * Can we use the FPU in kernel mode with the
84 * whole "kernel_fpu_begin/end()" sequence?
86 * It's always ok in process context (ie "not interrupt")
87 * but it is sometimes ok even from an irq.
89 bool irq_fpu_usable(void)
91 return !in_interrupt() ||
92 interrupted_user_mode() ||
93 interrupted_kernel_fpu_idle();
95 EXPORT_SYMBOL(irq_fpu_usable);
97 void __kernel_fpu_begin(void)
99 struct fpu *fpu = ¤t->thread.fpu;
101 kernel_fpu_disable();
103 if (fpu->fpregs_active) {
106 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
107 if (!use_eager_fpu())
111 EXPORT_SYMBOL(__kernel_fpu_begin);
113 void __kernel_fpu_end(void)
115 struct fpu *fpu = ¤t->thread.fpu;
117 if (fpu->fpregs_active) {
118 if (WARN_ON(restore_fpu_checking(fpu)))
119 fpu_reset_state(fpu);
120 } else if (!use_eager_fpu()) {
126 EXPORT_SYMBOL(__kernel_fpu_end);
128 void kernel_fpu_begin(void)
131 WARN_ON_ONCE(!irq_fpu_usable());
132 __kernel_fpu_begin();
134 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
136 void kernel_fpu_end(void)
141 EXPORT_SYMBOL_GPL(kernel_fpu_end);
143 static void __save_fpu(struct fpu *fpu)
146 if (unlikely(system_state == SYSTEM_BOOTING))
147 xsave_state_booting(&fpu->state->xsave);
149 xsave_state(&fpu->state->xsave);
156 * Save the FPU state (initialize it if necessary):
158 * This only ever gets called for the current task.
160 void fpu__save(struct fpu *fpu)
162 WARN_ON(fpu != ¤t->thread.fpu);
165 if (fpu->fpregs_active) {
166 if (use_eager_fpu()) {
170 fpregs_deactivate(fpu);
175 EXPORT_SYMBOL_GPL(fpu__save);
177 void fpstate_init(struct fpu *fpu)
180 finit_soft_fpu(&fpu->state->soft);
184 memset(fpu->state, 0, xstate_size);
187 fx_finit(&fpu->state->fxsave);
189 struct i387_fsave_struct *fp = &fpu->state->fsave;
190 fp->cwd = 0xffff037fu;
191 fp->swd = 0xffff0000u;
192 fp->twd = 0xffffffffu;
193 fp->fos = 0xffff0000u;
196 EXPORT_SYMBOL_GPL(fpstate_init);
199 * FPU state allocation:
201 static struct kmem_cache *task_xstate_cachep;
203 void fpstate_cache_init(void)
206 kmem_cache_create("task_xstate", xstate_size,
207 __alignof__(union thread_xstate),
208 SLAB_PANIC | SLAB_NOTRACK, NULL);
212 int fpstate_alloc(struct fpu *fpu)
217 fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
221 /* The CPU requires the FPU state to be aligned to 16 byte boundaries: */
222 WARN_ON((unsigned long)fpu->state & 15);
226 EXPORT_SYMBOL_GPL(fpstate_alloc);
228 void fpstate_free(struct fpu *fpu)
231 kmem_cache_free(task_xstate_cachep, fpu->state);
235 EXPORT_SYMBOL_GPL(fpstate_free);
238 * Copy the current task's FPU state to a new task's FPU context.
240 * In the 'eager' case we just save to the destination context.
242 * In the 'lazy' case we save to the source context, mark the FPU lazy
243 * via stts() and copy the source context into the destination context.
245 static void fpu_copy(struct fpu *dst_fpu, struct fpu *src_fpu)
247 WARN_ON(src_fpu != ¤t->thread.fpu);
249 if (use_eager_fpu()) {
250 memset(&dst_fpu->state->xsave, 0, xstate_size);
254 memcpy(dst_fpu->state, src_fpu->state, xstate_size);
258 int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
260 dst_fpu->counter = 0;
261 dst_fpu->fpregs_active = 0;
262 dst_fpu->state = NULL;
263 dst_fpu->last_cpu = -1;
265 if (src_fpu->fpstate_active) {
266 int err = fpstate_alloc(dst_fpu);
270 fpu_copy(dst_fpu, src_fpu);
276 * Allocate the backing store for the current task's FPU registers
277 * and initialize the registers themselves as well.
281 int fpstate_alloc_init(struct fpu *fpu)
285 if (WARN_ON_ONCE(fpu != ¤t->thread.fpu))
287 if (WARN_ON_ONCE(fpu->fpstate_active))
291 * Memory allocation at the first usage of the FPU and other state.
293 ret = fpstate_alloc(fpu);
299 /* Safe to do for the current task: */
300 fpu->fpstate_active = 1;
304 EXPORT_SYMBOL_GPL(fpstate_alloc_init);
307 * This function is called before we modify a stopped child's
310 * If the child has not used the FPU before then initialize its
313 * If the child has used the FPU before then unlazy it.
315 * [ After this function call, after the context is modified and
316 * the child task is woken up, the child task will restore
317 * the modified FPU state from the modified context. If we
318 * didn't clear its lazy status here then the lazy in-registers
319 * state pending on its former CPU could be restored, losing
320 * the modifications. ]
322 * This function is also called before we read a stopped child's
323 * FPU state - to make sure it's modified.
325 * TODO: A future optimization would be to skip the unlazying in
326 * the read-only case, it's not strictly necessary for
327 * read-only access to the context.
329 static int fpu__unlazy_stopped(struct fpu *child_fpu)
333 if (WARN_ON_ONCE(child_fpu == ¤t->thread.fpu))
336 if (child_fpu->fpstate_active) {
337 child_fpu->last_cpu = -1;
342 * Memory allocation at the first usage of the FPU and other state.
344 ret = fpstate_alloc(child_fpu);
348 fpstate_init(child_fpu);
350 /* Safe to do for stopped child tasks: */
351 child_fpu->fpstate_active = 1;
357 * 'fpu__restore()' saves the current math information in the
358 * old math state array, and gets the new ones from the current task
360 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
361 * Don't touch unless you *really* know how it works.
363 * Must be called with kernel preemption disabled (eg with local
364 * local interrupts as in the case of do_device_not_available).
366 void fpu__restore(void)
368 struct task_struct *tsk = current;
369 struct fpu *fpu = &tsk->thread.fpu;
371 if (!fpu->fpstate_active) {
374 * does a slab alloc which can sleep
376 if (fpstate_alloc_init(fpu)) {
380 do_group_exit(SIGKILL);
386 /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
387 kernel_fpu_disable();
388 fpregs_activate(fpu);
389 if (unlikely(restore_fpu_checking(fpu))) {
390 fpu_reset_state(fpu);
391 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
393 tsk->thread.fpu.counter++;
397 EXPORT_SYMBOL_GPL(fpu__restore);
399 void fpu__clear(struct task_struct *tsk)
401 struct fpu *fpu = &tsk->thread.fpu;
403 WARN_ON_ONCE(tsk != current); /* Almost certainly an anomaly */
405 if (!use_eager_fpu()) {
406 /* FPU state will be reallocated lazily at the first use. */
410 if (!fpu->fpstate_active) {
411 /* kthread execs. TODO: cleanup this horror. */
412 if (WARN_ON(fpstate_alloc_init(fpu)))
413 force_sig(SIGKILL, tsk);
416 restore_init_xstate();
421 * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
422 * as the "regset->n" for the xstate regset will be updated based on the feature
423 * capabilites supported by the xsave.
425 int regset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
427 struct fpu *target_fpu = &target->thread.fpu;
429 return target_fpu->fpstate_active ? regset->n : 0;
432 int regset_xregset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
434 struct fpu *target_fpu = &target->thread.fpu;
436 return (cpu_has_fxsr && target_fpu->fpstate_active) ? regset->n : 0;
439 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
440 unsigned int pos, unsigned int count,
441 void *kbuf, void __user *ubuf)
443 struct fpu *fpu = &target->thread.fpu;
449 ret = fpu__unlazy_stopped(fpu);
453 sanitize_i387_state(target);
455 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
456 &fpu->state->fxsave, 0, -1);
459 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
460 unsigned int pos, unsigned int count,
461 const void *kbuf, const void __user *ubuf)
463 struct fpu *fpu = &target->thread.fpu;
469 ret = fpu__unlazy_stopped(fpu);
473 sanitize_i387_state(target);
475 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
476 &fpu->state->fxsave, 0, -1);
479 * mxcsr reserved bits must be masked to zero for security reasons.
481 fpu->state->fxsave.mxcsr &= mxcsr_feature_mask;
484 * update the header bits in the xsave header, indicating the
485 * presence of FP and SSE state.
488 fpu->state->xsave.header.xfeatures |= XSTATE_FPSSE;
493 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
494 unsigned int pos, unsigned int count,
495 void *kbuf, void __user *ubuf)
497 struct fpu *fpu = &target->thread.fpu;
498 struct xsave_struct *xsave;
504 ret = fpu__unlazy_stopped(fpu);
508 xsave = &fpu->state->xsave;
511 * Copy the 48bytes defined by the software first into the xstate
512 * memory layout in the thread struct, so that we can copy the entire
513 * xstateregs to the user using one user_regset_copyout().
515 memcpy(&xsave->i387.sw_reserved,
516 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
518 * Copy the xstate memory layout.
520 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
524 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
525 unsigned int pos, unsigned int count,
526 const void *kbuf, const void __user *ubuf)
528 struct fpu *fpu = &target->thread.fpu;
529 struct xsave_struct *xsave;
535 ret = fpu__unlazy_stopped(fpu);
539 xsave = &fpu->state->xsave;
541 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
543 * mxcsr reserved bits must be masked to zero for security reasons.
545 xsave->i387.mxcsr &= mxcsr_feature_mask;
546 xsave->header.xfeatures &= xfeatures_mask;
548 * These bits must be zero.
550 memset(&xsave->header.reserved, 0, 48);
555 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
558 * FPU tag word conversions.
561 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
563 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
565 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
567 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
568 /* and move the valid bits to the lower byte. */
569 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
570 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
571 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
576 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
577 #define FP_EXP_TAG_VALID 0
578 #define FP_EXP_TAG_ZERO 1
579 #define FP_EXP_TAG_SPECIAL 2
580 #define FP_EXP_TAG_EMPTY 3
582 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
585 u32 tos = (fxsave->swd >> 11) & 7;
586 u32 twd = (unsigned long) fxsave->twd;
588 u32 ret = 0xffff0000u;
591 for (i = 0; i < 8; i++, twd >>= 1) {
593 st = FPREG_ADDR(fxsave, (i - tos) & 7);
595 switch (st->exponent & 0x7fff) {
597 tag = FP_EXP_TAG_SPECIAL;
600 if (!st->significand[0] &&
601 !st->significand[1] &&
602 !st->significand[2] &&
604 tag = FP_EXP_TAG_ZERO;
606 tag = FP_EXP_TAG_SPECIAL;
609 if (st->significand[3] & 0x8000)
610 tag = FP_EXP_TAG_VALID;
612 tag = FP_EXP_TAG_SPECIAL;
616 tag = FP_EXP_TAG_EMPTY;
618 ret |= tag << (2 * i);
624 * FXSR floating point environment conversions.
628 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
630 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
631 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
632 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
635 env->cwd = fxsave->cwd | 0xffff0000u;
636 env->swd = fxsave->swd | 0xffff0000u;
637 env->twd = twd_fxsr_to_i387(fxsave);
640 env->fip = fxsave->rip;
641 env->foo = fxsave->rdp;
643 * should be actually ds/cs at fpu exception time, but
644 * that information is not available in 64bit mode.
646 env->fcs = task_pt_regs(tsk)->cs;
647 if (tsk == current) {
648 savesegment(ds, env->fos);
650 env->fos = tsk->thread.ds;
652 env->fos |= 0xffff0000;
654 env->fip = fxsave->fip;
655 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
656 env->foo = fxsave->foo;
657 env->fos = fxsave->fos;
660 for (i = 0; i < 8; ++i)
661 memcpy(&to[i], &from[i], sizeof(to[0]));
664 void convert_to_fxsr(struct task_struct *tsk,
665 const struct user_i387_ia32_struct *env)
668 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
669 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
670 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
673 fxsave->cwd = env->cwd;
674 fxsave->swd = env->swd;
675 fxsave->twd = twd_i387_to_fxsr(env->twd);
676 fxsave->fop = (u16) ((u32) env->fcs >> 16);
678 fxsave->rip = env->fip;
679 fxsave->rdp = env->foo;
680 /* cs and ds ignored */
682 fxsave->fip = env->fip;
683 fxsave->fcs = (env->fcs & 0xffff);
684 fxsave->foo = env->foo;
685 fxsave->fos = env->fos;
688 for (i = 0; i < 8; ++i)
689 memcpy(&to[i], &from[i], sizeof(from[0]));
692 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
693 unsigned int pos, unsigned int count,
694 void *kbuf, void __user *ubuf)
696 struct fpu *fpu = &target->thread.fpu;
697 struct user_i387_ia32_struct env;
700 ret = fpu__unlazy_stopped(fpu);
704 if (!static_cpu_has(X86_FEATURE_FPU))
705 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
708 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
709 &fpu->state->fsave, 0,
712 sanitize_i387_state(target);
714 if (kbuf && pos == 0 && count == sizeof(env)) {
715 convert_from_fxsr(kbuf, target);
719 convert_from_fxsr(&env, target);
721 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
724 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
725 unsigned int pos, unsigned int count,
726 const void *kbuf, const void __user *ubuf)
728 struct fpu *fpu = &target->thread.fpu;
729 struct user_i387_ia32_struct env;
732 ret = fpu__unlazy_stopped(fpu);
736 sanitize_i387_state(target);
738 if (!static_cpu_has(X86_FEATURE_FPU))
739 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
742 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
743 &fpu->state->fsave, 0,
746 if (pos > 0 || count < sizeof(env))
747 convert_from_fxsr(&env, target);
749 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
751 convert_to_fxsr(target, &env);
754 * update the header bit in the xsave header, indicating the
758 fpu->state->xsave.header.xfeatures |= XSTATE_FP;
763 * FPU state for core dumps.
764 * This is only used for a.out dumps now.
765 * It is declared generically using elf_fpregset_t (which is
766 * struct user_i387_struct) but is in fact only used for 32-bit
767 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
769 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *ufpu)
771 struct task_struct *tsk = current;
772 struct fpu *fpu = &tsk->thread.fpu;
775 fpvalid = fpu->fpstate_active;
777 fpvalid = !fpregs_get(tsk, NULL,
778 0, sizeof(struct user_i387_ia32_struct),
783 EXPORT_SYMBOL(dump_fpu);
785 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */