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>
9 #include <asm/fpu/regset.h>
10 #include <asm/fpu/signal.h>
12 #include <linux/hardirq.h>
15 * Track whether the kernel is using the FPU state
20 * - by IRQ context code to potentially use the FPU
23 * - to debug kernel_fpu_begin()/end() correctness
25 static DEFINE_PER_CPU(bool, in_kernel_fpu);
28 * Track which context is using the FPU on the CPU:
30 DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
32 static void kernel_fpu_disable(void)
34 WARN_ON(this_cpu_read(in_kernel_fpu));
35 this_cpu_write(in_kernel_fpu, true);
38 static void kernel_fpu_enable(void)
40 WARN_ON_ONCE(!this_cpu_read(in_kernel_fpu));
41 this_cpu_write(in_kernel_fpu, false);
44 static bool kernel_fpu_disabled(void)
46 return this_cpu_read(in_kernel_fpu);
50 * Were we in an interrupt that interrupted kernel mode?
52 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
53 * pair does nothing at all: the thread must not have fpu (so
54 * that we don't try to save the FPU state), and TS must
55 * be set (so that the clts/stts pair does nothing that is
56 * visible in the interrupted kernel thread).
58 * Except for the eagerfpu case when we return true; in the likely case
59 * the thread has FPU but we are not going to set/clear TS.
61 static bool interrupted_kernel_fpu_idle(void)
63 if (kernel_fpu_disabled())
69 return !current->thread.fpu.fpregs_active && (read_cr0() & X86_CR0_TS);
73 * Were we in user mode (or vm86 mode) when we were
76 * Doing kernel_fpu_begin/end() is ok if we are running
77 * in an interrupt context from user mode - we'll just
78 * save the FPU state as required.
80 static bool interrupted_user_mode(void)
82 struct pt_regs *regs = get_irq_regs();
83 return regs && user_mode(regs);
87 * Can we use the FPU in kernel mode with the
88 * whole "kernel_fpu_begin/end()" sequence?
90 * It's always ok in process context (ie "not interrupt")
91 * but it is sometimes ok even from an irq.
93 bool irq_fpu_usable(void)
95 return !in_interrupt() ||
96 interrupted_user_mode() ||
97 interrupted_kernel_fpu_idle();
99 EXPORT_SYMBOL(irq_fpu_usable);
101 void __kernel_fpu_begin(void)
103 struct fpu *fpu = ¤t->thread.fpu;
105 kernel_fpu_disable();
107 if (fpu->fpregs_active) {
108 copy_fpregs_to_fpstate(fpu);
110 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
111 __fpregs_activate_hw();
114 EXPORT_SYMBOL(__kernel_fpu_begin);
116 void __kernel_fpu_end(void)
118 struct fpu *fpu = ¤t->thread.fpu;
120 if (fpu->fpregs_active) {
121 if (WARN_ON(copy_fpstate_to_fpregs(fpu)))
124 __fpregs_deactivate_hw();
129 EXPORT_SYMBOL(__kernel_fpu_end);
131 void kernel_fpu_begin(void)
134 WARN_ON_ONCE(!irq_fpu_usable());
135 __kernel_fpu_begin();
137 EXPORT_SYMBOL_GPL(kernel_fpu_begin);
139 void kernel_fpu_end(void)
144 EXPORT_SYMBOL_GPL(kernel_fpu_end);
147 * CR0::TS save/restore functions:
149 int irq_ts_save(void)
152 * If in process context and not atomic, we can take a spurious DNA fault.
153 * Otherwise, doing clts() in process context requires disabling preemption
154 * or some heavy lifting like kernel_fpu_begin()
159 if (read_cr0() & X86_CR0_TS) {
166 EXPORT_SYMBOL_GPL(irq_ts_save);
168 void irq_ts_restore(int TS_state)
173 EXPORT_SYMBOL_GPL(irq_ts_restore);
176 * Save the FPU state (mark it for reload if necessary):
178 * This only ever gets called for the current task.
180 void fpu__save(struct fpu *fpu)
182 WARN_ON(fpu != ¤t->thread.fpu);
185 if (fpu->fpregs_active) {
186 if (!copy_fpregs_to_fpstate(fpu))
187 fpregs_deactivate(fpu);
191 EXPORT_SYMBOL_GPL(fpu__save);
193 void fpstate_init(struct fpu *fpu)
196 finit_soft_fpu(&fpu->state.soft);
200 memset(&fpu->state, 0, xstate_size);
203 fx_finit(&fpu->state.fxsave);
205 struct i387_fsave_struct *fp = &fpu->state.fsave;
206 fp->cwd = 0xffff037fu;
207 fp->swd = 0xffff0000u;
208 fp->twd = 0xffffffffu;
209 fp->fos = 0xffff0000u;
212 EXPORT_SYMBOL_GPL(fpstate_init);
215 * Copy the current task's FPU state to a new task's FPU context.
217 * In the 'eager' case we just save to the destination context.
219 * In the 'lazy' case we save to the source context, mark the FPU lazy
220 * via stts() and copy the source context into the destination context.
222 static void fpu_copy(struct fpu *dst_fpu, struct fpu *src_fpu)
224 WARN_ON(src_fpu != ¤t->thread.fpu);
227 * Don't let 'init optimized' areas of the XSAVE area
228 * leak into the child task:
231 memset(&dst_fpu->state.xsave, 0, xstate_size);
234 * Save current FPU registers directly into the child
235 * FPU context, without any memory-to-memory copying.
237 * If the FPU context got destroyed in the process (FNSAVE
238 * done on old CPUs) then copy it back into the source
239 * context and mark the current task for lazy restore.
241 * We have to do all this with preemption disabled,
242 * mostly because of the FNSAVE case, because in that
243 * case we must not allow preemption in the window
244 * between the FNSAVE and us marking the context lazy.
246 * It shouldn't be an issue as even FNSAVE is plenty
247 * fast in terms of critical section length.
250 if (!copy_fpregs_to_fpstate(dst_fpu)) {
251 memcpy(&src_fpu->state, &dst_fpu->state, xstate_size);
252 fpregs_deactivate(src_fpu);
257 int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
259 dst_fpu->counter = 0;
260 dst_fpu->fpregs_active = 0;
261 dst_fpu->last_cpu = -1;
263 if (src_fpu->fpstate_active)
264 fpu_copy(dst_fpu, src_fpu);
270 * Activate the current task's in-memory FPU context,
271 * if it has not been used before:
273 void fpu__activate_curr(struct fpu *fpu)
275 WARN_ON_ONCE(fpu != ¤t->thread.fpu);
277 if (!fpu->fpstate_active) {
280 /* Safe to do for the current task: */
281 fpu->fpstate_active = 1;
284 EXPORT_SYMBOL_GPL(fpu__activate_curr);
287 * This function must be called before we modify a stopped child's
290 * If the child has not used the FPU before then initialize its
293 * If the child has used the FPU before then unlazy it.
295 * [ After this function call, after registers in the fpstate are
296 * modified and the child task has woken up, the child task will
297 * restore the modified FPU state from the modified context. If we
298 * didn't clear its lazy status here then the lazy in-registers
299 * state pending on its former CPU could be restored, corrupting
300 * the modifications. ]
302 * This function is also called before we read a stopped child's
303 * FPU state - to make sure it's initialized if the child has
304 * no active FPU state.
306 * TODO: A future optimization would be to skip the unlazying in
307 * the read-only case, it's not strictly necessary for
308 * read-only access to the context.
310 static void fpu__activate_stopped(struct fpu *child_fpu)
312 WARN_ON_ONCE(child_fpu == ¤t->thread.fpu);
314 if (child_fpu->fpstate_active) {
315 child_fpu->last_cpu = -1;
317 fpstate_init(child_fpu);
319 /* Safe to do for stopped child tasks: */
320 child_fpu->fpstate_active = 1;
325 * 'fpu__restore()' is called to copy FPU registers from
326 * the FPU fpstate to the live hw registers and to activate
327 * access to the hardware registers, so that FPU instructions
328 * can be used afterwards.
330 * Must be called with kernel preemption disabled (for example
331 * with local interrupts disabled, as it is in the case of
332 * do_device_not_available()).
334 void fpu__restore(void)
336 struct task_struct *tsk = current;
337 struct fpu *fpu = &tsk->thread.fpu;
339 fpu__activate_curr(fpu);
341 /* Avoid __kernel_fpu_begin() right after fpregs_activate() */
342 kernel_fpu_disable();
343 fpregs_activate(fpu);
344 if (unlikely(copy_fpstate_to_fpregs(fpu))) {
346 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
348 tsk->thread.fpu.counter++;
352 EXPORT_SYMBOL_GPL(fpu__restore);
355 * Drops current FPU state: deactivates the fpregs and
356 * the fpstate. NOTE: it still leaves previous contents
357 * in the fpregs in the eager-FPU case.
359 * This function can be used in cases where we know that
360 * a state-restore is coming: either an explicit one,
363 void fpu__drop(struct fpu *fpu)
368 if (fpu->fpregs_active) {
369 /* Ignore delayed exceptions from user space */
370 asm volatile("1: fwait\n"
372 _ASM_EXTABLE(1b, 2b));
373 fpregs_deactivate(fpu);
376 fpu->fpstate_active = 0;
382 * Clear the FPU state back to init state.
384 * Called by sys_execve(), by the signal handler code and by various
387 void fpu__clear(struct fpu *fpu)
389 WARN_ON_ONCE(fpu != ¤t->thread.fpu); /* Almost certainly an anomaly */
391 if (!use_eager_fpu()) {
392 /* FPU state will be reallocated lazily at the first use. */
395 if (!fpu->fpstate_active) {
396 fpu__activate_curr(fpu);
399 restore_init_xstate();
404 * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
405 * as the "regset->n" for the xstate regset will be updated based on the feature
406 * capabilites supported by the xsave.
408 int regset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
410 struct fpu *target_fpu = &target->thread.fpu;
412 return target_fpu->fpstate_active ? regset->n : 0;
415 int regset_xregset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
417 struct fpu *target_fpu = &target->thread.fpu;
419 return (cpu_has_fxsr && target_fpu->fpstate_active) ? regset->n : 0;
422 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
423 unsigned int pos, unsigned int count,
424 void *kbuf, void __user *ubuf)
426 struct fpu *fpu = &target->thread.fpu;
431 fpu__activate_stopped(fpu);
432 fpstate_sanitize_xstate(fpu);
434 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
435 &fpu->state.fxsave, 0, -1);
438 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
439 unsigned int pos, unsigned int count,
440 const void *kbuf, const void __user *ubuf)
442 struct fpu *fpu = &target->thread.fpu;
448 fpu__activate_stopped(fpu);
449 fpstate_sanitize_xstate(fpu);
451 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
452 &fpu->state.fxsave, 0, -1);
455 * mxcsr reserved bits must be masked to zero for security reasons.
457 fpu->state.fxsave.mxcsr &= mxcsr_feature_mask;
460 * update the header bits in the xsave header, indicating the
461 * presence of FP and SSE state.
464 fpu->state.xsave.header.xfeatures |= XSTATE_FPSSE;
469 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
470 unsigned int pos, unsigned int count,
471 void *kbuf, void __user *ubuf)
473 struct fpu *fpu = &target->thread.fpu;
474 struct xsave_struct *xsave;
480 fpu__activate_stopped(fpu);
482 xsave = &fpu->state.xsave;
485 * Copy the 48bytes defined by the software first into the xstate
486 * memory layout in the thread struct, so that we can copy the entire
487 * xstateregs to the user using one user_regset_copyout().
489 memcpy(&xsave->i387.sw_reserved,
490 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
492 * Copy the xstate memory layout.
494 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
498 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
499 unsigned int pos, unsigned int count,
500 const void *kbuf, const void __user *ubuf)
502 struct fpu *fpu = &target->thread.fpu;
503 struct xsave_struct *xsave;
509 fpu__activate_stopped(fpu);
511 xsave = &fpu->state.xsave;
513 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
515 * mxcsr reserved bits must be masked to zero for security reasons.
517 xsave->i387.mxcsr &= mxcsr_feature_mask;
518 xsave->header.xfeatures &= xfeatures_mask;
520 * These bits must be zero.
522 memset(&xsave->header.reserved, 0, 48);
527 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
530 * FPU tag word conversions.
533 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
535 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
537 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
539 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
540 /* and move the valid bits to the lower byte. */
541 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
542 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
543 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
548 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
549 #define FP_EXP_TAG_VALID 0
550 #define FP_EXP_TAG_ZERO 1
551 #define FP_EXP_TAG_SPECIAL 2
552 #define FP_EXP_TAG_EMPTY 3
554 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
557 u32 tos = (fxsave->swd >> 11) & 7;
558 u32 twd = (unsigned long) fxsave->twd;
560 u32 ret = 0xffff0000u;
563 for (i = 0; i < 8; i++, twd >>= 1) {
565 st = FPREG_ADDR(fxsave, (i - tos) & 7);
567 switch (st->exponent & 0x7fff) {
569 tag = FP_EXP_TAG_SPECIAL;
572 if (!st->significand[0] &&
573 !st->significand[1] &&
574 !st->significand[2] &&
576 tag = FP_EXP_TAG_ZERO;
578 tag = FP_EXP_TAG_SPECIAL;
581 if (st->significand[3] & 0x8000)
582 tag = FP_EXP_TAG_VALID;
584 tag = FP_EXP_TAG_SPECIAL;
588 tag = FP_EXP_TAG_EMPTY;
590 ret |= tag << (2 * i);
596 * FXSR floating point environment conversions.
600 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
602 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state.fxsave;
603 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
604 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
607 env->cwd = fxsave->cwd | 0xffff0000u;
608 env->swd = fxsave->swd | 0xffff0000u;
609 env->twd = twd_fxsr_to_i387(fxsave);
612 env->fip = fxsave->rip;
613 env->foo = fxsave->rdp;
615 * should be actually ds/cs at fpu exception time, but
616 * that information is not available in 64bit mode.
618 env->fcs = task_pt_regs(tsk)->cs;
619 if (tsk == current) {
620 savesegment(ds, env->fos);
622 env->fos = tsk->thread.ds;
624 env->fos |= 0xffff0000;
626 env->fip = fxsave->fip;
627 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
628 env->foo = fxsave->foo;
629 env->fos = fxsave->fos;
632 for (i = 0; i < 8; ++i)
633 memcpy(&to[i], &from[i], sizeof(to[0]));
636 void convert_to_fxsr(struct task_struct *tsk,
637 const struct user_i387_ia32_struct *env)
640 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state.fxsave;
641 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
642 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
645 fxsave->cwd = env->cwd;
646 fxsave->swd = env->swd;
647 fxsave->twd = twd_i387_to_fxsr(env->twd);
648 fxsave->fop = (u16) ((u32) env->fcs >> 16);
650 fxsave->rip = env->fip;
651 fxsave->rdp = env->foo;
652 /* cs and ds ignored */
654 fxsave->fip = env->fip;
655 fxsave->fcs = (env->fcs & 0xffff);
656 fxsave->foo = env->foo;
657 fxsave->fos = env->fos;
660 for (i = 0; i < 8; ++i)
661 memcpy(&to[i], &from[i], sizeof(from[0]));
664 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
665 unsigned int pos, unsigned int count,
666 void *kbuf, void __user *ubuf)
668 struct fpu *fpu = &target->thread.fpu;
669 struct user_i387_ia32_struct env;
671 fpu__activate_stopped(fpu);
673 if (!static_cpu_has(X86_FEATURE_FPU))
674 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
677 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
678 &fpu->state.fsave, 0,
681 fpstate_sanitize_xstate(fpu);
683 if (kbuf && pos == 0 && count == sizeof(env)) {
684 convert_from_fxsr(kbuf, target);
688 convert_from_fxsr(&env, target);
690 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
693 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
694 unsigned int pos, unsigned int count,
695 const void *kbuf, const void __user *ubuf)
697 struct fpu *fpu = &target->thread.fpu;
698 struct user_i387_ia32_struct env;
701 fpu__activate_stopped(fpu);
702 fpstate_sanitize_xstate(fpu);
704 if (!static_cpu_has(X86_FEATURE_FPU))
705 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
708 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
709 &fpu->state.fsave, 0,
712 if (pos > 0 || count < sizeof(env))
713 convert_from_fxsr(&env, target);
715 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
717 convert_to_fxsr(target, &env);
720 * update the header bit in the xsave header, indicating the
724 fpu->state.xsave.header.xfeatures |= XSTATE_FP;
729 * FPU state for core dumps.
730 * This is only used for a.out dumps now.
731 * It is declared generically using elf_fpregset_t (which is
732 * struct user_i387_struct) but is in fact only used for 32-bit
733 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
735 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *ufpu)
737 struct task_struct *tsk = current;
738 struct fpu *fpu = &tsk->thread.fpu;
741 fpvalid = fpu->fpstate_active;
743 fpvalid = !fpregs_get(tsk, NULL,
744 0, sizeof(struct user_i387_ia32_struct),
749 EXPORT_SYMBOL(dump_fpu);
751 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */