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 <linux/module.h>
9 #include <linux/regset.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
13 #include <asm/sigcontext.h>
14 #include <asm/processor.h>
15 #include <asm/math_emu.h>
16 #include <asm/uaccess.h>
17 #include <asm/ptrace.h>
19 #include <asm/fpu-internal.h>
22 static DEFINE_PER_CPU(bool, in_kernel_fpu);
24 void kernel_fpu_disable(void)
26 WARN_ON(this_cpu_read(in_kernel_fpu));
27 this_cpu_write(in_kernel_fpu, true);
30 void kernel_fpu_enable(void)
32 this_cpu_write(in_kernel_fpu, false);
36 * Were we in an interrupt that interrupted kernel mode?
38 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
39 * pair does nothing at all: the thread must not have fpu (so
40 * that we don't try to save the FPU state), and TS must
41 * be set (so that the clts/stts pair does nothing that is
42 * visible in the interrupted kernel thread).
44 * Except for the eagerfpu case when we return true; in the likely case
45 * the thread has FPU but we are not going to set/clear TS.
47 static inline bool interrupted_kernel_fpu_idle(void)
49 if (this_cpu_read(in_kernel_fpu))
55 return !__thread_has_fpu(current) &&
56 (read_cr0() & X86_CR0_TS);
60 * Were we in user mode (or vm86 mode) when we were
63 * Doing kernel_fpu_begin/end() is ok if we are running
64 * in an interrupt context from user mode - we'll just
65 * save the FPU state as required.
67 static inline bool interrupted_user_mode(void)
69 struct pt_regs *regs = get_irq_regs();
70 return regs && user_mode_vm(regs);
74 * Can we use the FPU in kernel mode with the
75 * whole "kernel_fpu_begin/end()" sequence?
77 * It's always ok in process context (ie "not interrupt")
78 * but it is sometimes ok even from an irq.
80 bool irq_fpu_usable(void)
82 return !in_interrupt() ||
83 interrupted_user_mode() ||
84 interrupted_kernel_fpu_idle();
86 EXPORT_SYMBOL(irq_fpu_usable);
88 void __kernel_fpu_begin(void)
90 struct task_struct *me = current;
92 this_cpu_write(in_kernel_fpu, true);
94 if (__thread_has_fpu(me)) {
97 this_cpu_write(fpu_owner_task, NULL);
102 EXPORT_SYMBOL(__kernel_fpu_begin);
104 void __kernel_fpu_end(void)
106 struct task_struct *me = current;
108 if (__thread_has_fpu(me)) {
109 if (WARN_ON(restore_fpu_checking(me)))
111 } else if (!use_eager_fpu()) {
115 this_cpu_write(in_kernel_fpu, false);
117 EXPORT_SYMBOL(__kernel_fpu_end);
119 void unlazy_fpu(struct task_struct *tsk)
122 if (__thread_has_fpu(tsk)) {
123 if (use_eager_fpu()) {
126 __save_init_fpu(tsk);
127 __thread_fpu_end(tsk);
132 EXPORT_SYMBOL(unlazy_fpu);
134 unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
135 unsigned int xstate_size;
136 EXPORT_SYMBOL_GPL(xstate_size);
137 static struct i387_fxsave_struct fx_scratch;
139 static void mxcsr_feature_mask_init(void)
141 unsigned long mask = 0;
144 memset(&fx_scratch, 0, sizeof(struct i387_fxsave_struct));
145 asm volatile("fxsave %0" : "+m" (fx_scratch));
146 mask = fx_scratch.mxcsr_mask;
150 mxcsr_feature_mask &= mask;
153 static void init_thread_xstate(void)
156 * Note that xstate_size might be overwriten later during
162 * Disable xsave as we do not support it if i387
163 * emulation is enabled.
165 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
166 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
167 xstate_size = sizeof(struct i387_soft_struct);
172 xstate_size = sizeof(struct i387_fxsave_struct);
174 xstate_size = sizeof(struct i387_fsave_struct);
178 * Called at bootup to set up the initial FPU state that is later cloned
179 * into all processes.
185 unsigned long cr4_mask = 0;
187 #ifndef CONFIG_MATH_EMULATION
189 pr_emerg("No FPU found and no math emulation present\n");
190 pr_emerg("Giving up\n");
196 cr4_mask |= X86_CR4_OSFXSR;
198 cr4_mask |= X86_CR4_OSXMMEXCPT;
200 set_in_cr4(cr4_mask);
203 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
209 * init_thread_xstate is only called once to avoid overriding
210 * xstate_size during boot time or during CPU hotplug.
212 if (xstate_size == 0)
213 init_thread_xstate();
215 mxcsr_feature_mask_init();
220 void fpu_finit(struct fpu *fpu)
223 finit_soft_fpu(&fpu->state->soft);
228 fx_finit(&fpu->state->fxsave);
230 struct i387_fsave_struct *fp = &fpu->state->fsave;
231 memset(fp, 0, xstate_size);
232 fp->cwd = 0xffff037fu;
233 fp->swd = 0xffff0000u;
234 fp->twd = 0xffffffffu;
235 fp->fos = 0xffff0000u;
238 EXPORT_SYMBOL_GPL(fpu_finit);
241 * The _current_ task is using the FPU for the first time
242 * so initialize it and set the mxcsr to its default
243 * value at reset if we support XMM instructions and then
244 * remember the current task has used the FPU.
246 int init_fpu(struct task_struct *tsk)
250 if (tsk_used_math(tsk)) {
251 if (cpu_has_fpu && tsk == current)
253 task_disable_lazy_fpu_restore(tsk);
258 * Memory allocation at the first usage of the FPU and other state.
260 ret = fpu_alloc(&tsk->thread.fpu);
264 fpu_finit(&tsk->thread.fpu);
266 set_stopped_child_used_math(tsk);
269 EXPORT_SYMBOL_GPL(init_fpu);
272 * The xstateregs_active() routine is the same as the fpregs_active() routine,
273 * as the "regset->n" for the xstate regset will be updated based on the feature
274 * capabilites supported by the xsave.
276 int fpregs_active(struct task_struct *target, const struct user_regset *regset)
278 return tsk_used_math(target) ? regset->n : 0;
281 int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
283 return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
286 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
287 unsigned int pos, unsigned int count,
288 void *kbuf, void __user *ubuf)
295 ret = init_fpu(target);
299 sanitize_i387_state(target);
301 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
302 &target->thread.fpu.state->fxsave, 0, -1);
305 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
306 unsigned int pos, unsigned int count,
307 const void *kbuf, const void __user *ubuf)
314 ret = init_fpu(target);
318 sanitize_i387_state(target);
320 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
321 &target->thread.fpu.state->fxsave, 0, -1);
324 * mxcsr reserved bits must be masked to zero for security reasons.
326 target->thread.fpu.state->fxsave.mxcsr &= mxcsr_feature_mask;
329 * update the header bits in the xsave header, indicating the
330 * presence of FP and SSE state.
333 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FPSSE;
338 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
339 unsigned int pos, unsigned int count,
340 void *kbuf, void __user *ubuf)
342 struct xsave_struct *xsave = &target->thread.fpu.state->xsave;
348 ret = init_fpu(target);
353 * Copy the 48bytes defined by the software first into the xstate
354 * memory layout in the thread struct, so that we can copy the entire
355 * xstateregs to the user using one user_regset_copyout().
357 memcpy(&xsave->i387.sw_reserved,
358 xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
360 * Copy the xstate memory layout.
362 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
366 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
367 unsigned int pos, unsigned int count,
368 const void *kbuf, const void __user *ubuf)
370 struct xsave_struct *xsave = &target->thread.fpu.state->xsave;
376 ret = init_fpu(target);
380 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
382 * mxcsr reserved bits must be masked to zero for security reasons.
384 xsave->i387.mxcsr &= mxcsr_feature_mask;
385 xsave->xsave_hdr.xstate_bv &= pcntxt_mask;
387 * These bits must be zero.
389 memset(&xsave->xsave_hdr.reserved, 0, 48);
393 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
396 * FPU tag word conversions.
399 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
401 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
403 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
405 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
406 /* and move the valid bits to the lower byte. */
407 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
408 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
409 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
414 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
415 #define FP_EXP_TAG_VALID 0
416 #define FP_EXP_TAG_ZERO 1
417 #define FP_EXP_TAG_SPECIAL 2
418 #define FP_EXP_TAG_EMPTY 3
420 static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
423 u32 tos = (fxsave->swd >> 11) & 7;
424 u32 twd = (unsigned long) fxsave->twd;
426 u32 ret = 0xffff0000u;
429 for (i = 0; i < 8; i++, twd >>= 1) {
431 st = FPREG_ADDR(fxsave, (i - tos) & 7);
433 switch (st->exponent & 0x7fff) {
435 tag = FP_EXP_TAG_SPECIAL;
438 if (!st->significand[0] &&
439 !st->significand[1] &&
440 !st->significand[2] &&
442 tag = FP_EXP_TAG_ZERO;
444 tag = FP_EXP_TAG_SPECIAL;
447 if (st->significand[3] & 0x8000)
448 tag = FP_EXP_TAG_VALID;
450 tag = FP_EXP_TAG_SPECIAL;
454 tag = FP_EXP_TAG_EMPTY;
456 ret |= tag << (2 * i);
462 * FXSR floating point environment conversions.
466 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
468 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
469 struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
470 struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
473 env->cwd = fxsave->cwd | 0xffff0000u;
474 env->swd = fxsave->swd | 0xffff0000u;
475 env->twd = twd_fxsr_to_i387(fxsave);
478 env->fip = fxsave->rip;
479 env->foo = fxsave->rdp;
481 * should be actually ds/cs at fpu exception time, but
482 * that information is not available in 64bit mode.
484 env->fcs = task_pt_regs(tsk)->cs;
485 if (tsk == current) {
486 savesegment(ds, env->fos);
488 env->fos = tsk->thread.ds;
490 env->fos |= 0xffff0000;
492 env->fip = fxsave->fip;
493 env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
494 env->foo = fxsave->foo;
495 env->fos = fxsave->fos;
498 for (i = 0; i < 8; ++i)
499 memcpy(&to[i], &from[i], sizeof(to[0]));
502 void convert_to_fxsr(struct task_struct *tsk,
503 const struct user_i387_ia32_struct *env)
506 struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state->fxsave;
507 struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
508 struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
511 fxsave->cwd = env->cwd;
512 fxsave->swd = env->swd;
513 fxsave->twd = twd_i387_to_fxsr(env->twd);
514 fxsave->fop = (u16) ((u32) env->fcs >> 16);
516 fxsave->rip = env->fip;
517 fxsave->rdp = env->foo;
518 /* cs and ds ignored */
520 fxsave->fip = env->fip;
521 fxsave->fcs = (env->fcs & 0xffff);
522 fxsave->foo = env->foo;
523 fxsave->fos = env->fos;
526 for (i = 0; i < 8; ++i)
527 memcpy(&to[i], &from[i], sizeof(from[0]));
530 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
531 unsigned int pos, unsigned int count,
532 void *kbuf, void __user *ubuf)
534 struct user_i387_ia32_struct env;
537 ret = init_fpu(target);
541 if (!static_cpu_has(X86_FEATURE_FPU))
542 return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
545 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
546 &target->thread.fpu.state->fsave, 0,
549 sanitize_i387_state(target);
551 if (kbuf && pos == 0 && count == sizeof(env)) {
552 convert_from_fxsr(kbuf, target);
556 convert_from_fxsr(&env, target);
558 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
561 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
562 unsigned int pos, unsigned int count,
563 const void *kbuf, const void __user *ubuf)
565 struct user_i387_ia32_struct env;
568 ret = init_fpu(target);
572 sanitize_i387_state(target);
574 if (!static_cpu_has(X86_FEATURE_FPU))
575 return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
578 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
579 &target->thread.fpu.state->fsave, 0,
582 if (pos > 0 || count < sizeof(env))
583 convert_from_fxsr(&env, target);
585 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
587 convert_to_fxsr(target, &env);
590 * update the header bit in the xsave header, indicating the
594 target->thread.fpu.state->xsave.xsave_hdr.xstate_bv |= XSTATE_FP;
599 * FPU state for core dumps.
600 * This is only used for a.out dumps now.
601 * It is declared generically using elf_fpregset_t (which is
602 * struct user_i387_struct) but is in fact only used for 32-bit
603 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
605 int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
607 struct task_struct *tsk = current;
610 fpvalid = !!used_math();
612 fpvalid = !fpregs_get(tsk, NULL,
613 0, sizeof(struct user_i387_ia32_struct),
618 EXPORT_SYMBOL(dump_fpu);
620 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
622 static int __init no_387(char *s)
624 setup_clear_cpu_cap(X86_FEATURE_FPU);
628 __setup("no387", no_387);
630 void fpu_detect(struct cpuinfo_x86 *c)
638 cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
641 asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
642 : "+m" (fsw), "+m" (fcw));
644 if (fsw == 0 && (fcw & 0x103f) == 0x003f)
645 set_cpu_cap(c, X86_FEATURE_FPU);
647 clear_cpu_cap(c, X86_FEATURE_FPU);
649 /* The final cr0 value is set in fpu_init() */