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);
23 static void kernel_fpu_disable(void)
25 WARN_ON(this_cpu_read(in_kernel_fpu));
26 this_cpu_write(in_kernel_fpu, true);
29 static void kernel_fpu_enable(void)
31 WARN_ON_ONCE(!this_cpu_read(in_kernel_fpu));
32 this_cpu_write(in_kernel_fpu, false);
35 static bool kernel_fpu_disabled(void)
37 return this_cpu_read(in_kernel_fpu);
41 * Were we in an interrupt that interrupted kernel mode?
43 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
44 * pair does nothing at all: the thread must not have fpu (so
45 * that we don't try to save the FPU state), and TS must
46 * be set (so that the clts/stts pair does nothing that is
47 * visible in the interrupted kernel thread).
49 * Except for the eagerfpu case when we return true; in the likely case
50 * the thread has FPU but we are not going to set/clear TS.
52 static bool interrupted_kernel_fpu_idle(void)
54 if (kernel_fpu_disabled())
60 return !__thread_has_fpu(current) &&
61 (read_cr0() & X86_CR0_TS);
65 * Were we in user mode (or vm86 mode) when we were
68 * Doing kernel_fpu_begin/end() is ok if we are running
69 * in an interrupt context from user mode - we'll just
70 * save the FPU state as required.
72 static bool interrupted_user_mode(void)
74 struct pt_regs *regs = get_irq_regs();
75 return regs && user_mode(regs);
79 * Can we use the FPU in kernel mode with the
80 * whole "kernel_fpu_begin/end()" sequence?
82 * It's always ok in process context (ie "not interrupt")
83 * but it is sometimes ok even from an irq.
85 bool irq_fpu_usable(void)
87 return !in_interrupt() ||
88 interrupted_user_mode() ||
89 interrupted_kernel_fpu_idle();
91 EXPORT_SYMBOL(irq_fpu_usable);
93 void __kernel_fpu_begin(void)
95 struct task_struct *me = current;
99 if (__thread_has_fpu(me)) {
102 this_cpu_write(fpu_owner_task, NULL);
103 if (!use_eager_fpu())
107 EXPORT_SYMBOL(__kernel_fpu_begin);
109 void __kernel_fpu_end(void)
111 struct task_struct *me = current;
113 if (__thread_has_fpu(me)) {
114 if (WARN_ON(restore_fpu_checking(me)))
116 } else if (!use_eager_fpu()) {
122 EXPORT_SYMBOL(__kernel_fpu_end);
125 * Save the FPU state (initialize it if necessary):
127 * This only ever gets called for the current task.
129 void fpu__save(struct task_struct *tsk)
131 WARN_ON(tsk != current);
134 if (__thread_has_fpu(tsk)) {
135 if (use_eager_fpu()) {
138 __save_init_fpu(tsk);
139 __thread_fpu_end(tsk);
144 EXPORT_SYMBOL_GPL(fpu__save);
146 void fpstate_init(struct fpu *fpu)
149 finit_soft_fpu(&fpu->state->soft);
153 memset(fpu->state, 0, xstate_size);
156 fx_finit(&fpu->state->fxsave);
158 struct i387_fsave_struct *fp = &fpu->state->fsave;
159 fp->cwd = 0xffff037fu;
160 fp->swd = 0xffff0000u;
161 fp->twd = 0xffffffffu;
162 fp->fos = 0xffff0000u;
165 EXPORT_SYMBOL_GPL(fpstate_init);
168 * FPU state allocation:
170 static struct kmem_cache *task_xstate_cachep;
172 void fpstate_cache_init(void)
175 kmem_cache_create("task_xstate", xstate_size,
176 __alignof__(union thread_xstate),
177 SLAB_PANIC | SLAB_NOTRACK, NULL);
181 int fpstate_alloc(struct fpu *fpu)
186 fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
190 /* The CPU requires the FPU state to be aligned to 16 byte boundaries: */
191 WARN_ON((unsigned long)fpu->state & 15);
195 EXPORT_SYMBOL_GPL(fpstate_alloc);
197 void fpstate_free(struct fpu *fpu)
200 kmem_cache_free(task_xstate_cachep, fpu->state);
204 EXPORT_SYMBOL_GPL(fpstate_free);
206 int fpu__copy(struct task_struct *dst, struct task_struct *src)
208 dst->thread.fpu.counter = 0;
209 dst->thread.fpu.has_fpu = 0;
210 dst->thread.fpu.state = NULL;
212 task_disable_lazy_fpu_restore(dst);
214 if (tsk_used_math(src)) {
215 int err = fpstate_alloc(&dst->thread.fpu);
225 * Allocate the backing store for the current task's FPU registers
226 * and initialize the registers themselves as well.
230 int fpstate_alloc_init(struct task_struct *curr)
234 if (WARN_ON_ONCE(curr != current))
236 if (WARN_ON_ONCE(curr->flags & PF_USED_MATH))
240 * Memory allocation at the first usage of the FPU and other state.
242 ret = fpstate_alloc(&curr->thread.fpu);
246 fpstate_init(&curr->thread.fpu);
248 /* Safe to do for the current task: */
249 curr->flags |= PF_USED_MATH;
253 EXPORT_SYMBOL_GPL(fpstate_alloc_init);
256 * The _current_ task is using the FPU for the first time
257 * so initialize it and set the mxcsr to its default
258 * value at reset if we support XMM instructions and then
259 * remember the current task has used the FPU.
261 static int fpu__unlazy_stopped(struct task_struct *child)
265 if (WARN_ON_ONCE(child == current))
268 if (child->flags & PF_USED_MATH) {
269 task_disable_lazy_fpu_restore(child);
274 * Memory allocation at the first usage of the FPU and other state.
276 ret = fpstate_alloc(&child->thread.fpu);
280 fpstate_init(&child->thread.fpu);
282 /* Safe to do for stopped child tasks: */
283 child->flags |= PF_USED_MATH;
289 * 'fpu__restore()' saves the current math information in the
290 * old math state array, and gets the new ones from the current task
292 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
293 * Don't touch unless you *really* know how it works.
295 * Must be called with kernel preemption disabled (eg with local
296 * local interrupts as in the case of do_device_not_available).
298 void fpu__restore(void)
300 struct task_struct *tsk = current;
302 if (!tsk_used_math(tsk)) {
305 * does a slab alloc which can sleep
307 if (fpstate_alloc_init(tsk)) {
311 do_group_exit(SIGKILL);
317 /* Avoid __kernel_fpu_begin() right after __thread_fpu_begin() */
318 kernel_fpu_disable();
319 __thread_fpu_begin(tsk);
320 if (unlikely(restore_fpu_checking(tsk))) {
321 fpu_reset_state(tsk);
322 force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
324 tsk->thread.fpu.counter++;
328 EXPORT_SYMBOL_GPL(fpu__restore);
330 void fpu__flush_thread(struct task_struct *tsk)
332 if (!use_eager_fpu()) {
333 /* FPU state will be reallocated lazily at the first use. */
335 fpstate_free(&tsk->thread.fpu);
337 if (!tsk_used_math(tsk)) {
338 /* kthread execs. TODO: cleanup this horror. */
339 if (WARN_ON(fpstate_alloc_init(tsk)))
340 force_sig(SIGKILL, tsk);
343 restore_init_xstate();
348 * The xstateregs_active() routine is the same as the fpregs_active() routine,
349 * as the "regset->n" for the xstate regset will be updated based on the feature
350 * capabilites supported by the xsave.
352 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
354 return tsk_used_math(target) ? regset->n : 0;
357 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
359 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
362 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
363 unsigned int pos, unsigned int count,
364 void *kbuf, void __user *ubuf)
371 ret = fpu__unlazy_stopped(target);
375 sanitize_i387_state(target);
377 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
378 &target->thread.fpu.state->fxsave, 0, -1);
381 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
382 unsigned int pos, unsigned int count,
383 const void *kbuf, const void __user *ubuf)
390 ret = fpu__unlazy_stopped(target);
394 sanitize_i387_state(target);
396 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
397 &target->thread.fpu.state->fxsave, 0, -1);
400 * mxcsr reserved bits must be masked to zero for security reasons.
402 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
405 * update the header bits in the xsave header, indicating the
406 * presence of FP and SSE state.
409 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
414 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
415 unsigned int pos, unsigned int count,
416 void *kbuf, void __user *ubuf)
418 struct xsave_struct *xsave;
424 ret = fpu__unlazy_stopped(target);
428 xsave = &target->thread.fpu.state->xsave;
431 * Copy the 48bytes defined by the software first into the xstate
432 * memory layout in the thread struct, so that we can copy the entire
433 * xstateregs to the user using one user_regset_copyout().
435 memcpy(&xsave->i387.sw_reserved,
436 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
438 * Copy the xstate memory layout.
440 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
444 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
445 unsigned int pos, unsigned int count,
446 const void *kbuf, const void __user *ubuf)
448 struct xsave_struct *xsave;
454 ret = fpu__unlazy_stopped(target);
458 xsave = &target->thread.fpu.state->xsave;
460 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
462 * mxcsr reserved bits must be masked to zero for security reasons.
464 xsave->i387.mxcsr &= mxcsr_feature_mask;
465 xsave->xsave_hdr.xstate_bv &= pcntxt_mask;
467 * These bits must be zero.
469 memset(&xsave->xsave_hdr.reserved, 0, 48);
473 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
476 * FPU tag word conversions.
479 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
481 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
483 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
485 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
486 /* and move the valid bits to the lower byte. */
487 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
488 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
489 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
494 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
495 #define FP_EXP_TAG_VALID 0
496 #define FP_EXP_TAG_ZERO 1
497 #define FP_EXP_TAG_SPECIAL 2
498 #define FP_EXP_TAG_EMPTY 3
500 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
503 u32 tos = (fxsave->swd >> 11) & 7;
504 u32 twd = (unsigned long) fxsave->twd;
506 u32 ret = 0xffff0000u;
509 for (i = 0; i < 8; i++, twd >>= 1) {
511 st = FPREG_ADDR(fxsave, (i - tos) & 7);
513 switch (st->exponent & 0x7fff) {
515 tag = FP_EXP_TAG_SPECIAL;
518 if (!st->significand[0] &&
519 !st->significand[1] &&
520 !st->significand[2] &&
522 tag = FP_EXP_TAG_ZERO;
524 tag = FP_EXP_TAG_SPECIAL;
527 if (st->significand[3] & 0x8000)
528 tag = FP_EXP_TAG_VALID;
530 tag = FP_EXP_TAG_SPECIAL;
534 tag = FP_EXP_TAG_EMPTY;
536 ret |= tag << (2 * i);
542 * FXSR floating point environment conversions.
546 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
548 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
549 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
550 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
553 env->cwd = fxsave->cwd | 0xffff0000u;
554 env->swd = fxsave->swd | 0xffff0000u;
555 env->twd = twd_fxsr_to_i387(fxsave);
558 env->fip = fxsave->rip;
559 env->foo = fxsave->rdp;
561 * should be actually ds/cs at fpu exception time, but
562 * that information is not available in 64bit mode.
564 env->fcs = task_pt_regs(tsk)->cs;
565 if (tsk == current) {
566 savesegment(ds, env->fos);
568 env->fos = tsk->thread.ds;
570 env->fos |= 0xffff0000;
572 env->fip = fxsave->fip;
573 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
574 env->foo = fxsave->foo;
575 env->fos = fxsave->fos;
578 for (i = 0; i < 8; ++i)
579 memcpy(&to[i], &from[i], sizeof(to[0]));
582 void convert_to_fxsr(struct task_struct *tsk,
583 const struct user_i387_ia32_struct *env)
586 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
587 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
588 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
591 fxsave->cwd = env->cwd;
592 fxsave->swd = env->swd;
593 fxsave->twd = twd_i387_to_fxsr(env->twd);
594 fxsave->fop = (u16) ((u32) env->fcs >> 16);
596 fxsave->rip = env->fip;
597 fxsave->rdp = env->foo;
598 /* cs and ds ignored */
600 fxsave->fip = env->fip;
601 fxsave->fcs = (env->fcs & 0xffff);
602 fxsave->foo = env->foo;
603 fxsave->fos = env->fos;
606 for (i = 0; i < 8; ++i)
607 memcpy(&to[i], &from[i], sizeof(from[0]));
610 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
611 unsigned int pos, unsigned int count,
612 void *kbuf, void __user *ubuf)
614 struct user_i387_ia32_struct env;
617 ret = fpu__unlazy_stopped(target);
621 if (!static_cpu_has(X86_FEATURE_FPU))
622 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
625 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
626 &target->thread.fpu.state->fsave, 0,
629 sanitize_i387_state(target);
631 if (kbuf && pos == 0 && count == sizeof(env)) {
632 convert_from_fxsr(kbuf, target);
636 convert_from_fxsr(&env, target);
638 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
641 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
642 unsigned int pos, unsigned int count,
643 const void *kbuf, const void __user *ubuf)
645 struct user_i387_ia32_struct env;
648 ret = fpu__unlazy_stopped(target);
652 sanitize_i387_state(target);
654 if (!static_cpu_has(X86_FEATURE_FPU))
655 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
658 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
659 &target->thread.fpu.state->fsave, 0,
662 if (pos > 0 || count < sizeof(env))
663 convert_from_fxsr(&env, target);
665 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
667 convert_to_fxsr(target, &env);
670 * update the header bit in the xsave header, indicating the
674 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
679 * FPU state for core dumps.
680 * This is only used for a.out dumps now.
681 * It is declared generically using elf_fpregset_t (which is
682 * struct user_i387_struct) but is in fact only used for 32-bit
683 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
685 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
687 struct task_struct *tsk = current;
690 fpvalid = !!used_math();
692 fpvalid = !fpregs_get(tsk, NULL,
693 0, sizeof(struct user_i387_ia32_struct),
698 EXPORT_SYMBOL(dump_fpu);
700 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */