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
7 * x86-64 work by Andi Kleen 2002
10 #ifndef _ASM_X86_FPU_INTERNAL_H
11 #define _ASM_X86_FPU_INTERNAL_H
13 #include <linux/regset.h>
14 #include <linux/compat.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
19 #include <asm/fpu/api.h>
20 #include <asm/fpu/xstate.h>
22 #define MXCSR_DEFAULT 0x1f80
24 extern unsigned int mxcsr_feature_mask
;
26 extern void fpu__init_cpu(void);
27 extern void fpu__init_system_xstate(void);
28 extern void fpu__init_cpu_xstate(void);
29 extern void fpu__init_system(struct cpuinfo_x86
*c
);
31 extern void fpu__activate_curr(struct fpu
*fpu
);
32 extern void fpstate_init(struct fpu
*fpu
);
34 extern int dump_fpu(struct pt_regs
*, struct user_i387_struct
*);
37 * High level FPU state handling functions:
39 extern void fpu__save(struct fpu
*fpu
);
40 extern void fpu__restore(void);
41 extern int fpu__restore_sig(void __user
*buf
, int ia32_frame
);
42 extern void fpu__drop(struct fpu
*fpu
);
43 extern int fpu__copy(struct fpu
*dst_fpu
, struct fpu
*src_fpu
);
44 extern void fpu__clear(struct fpu
*fpu
);
46 extern void fpu__init_check_bugs(void);
47 extern void fpu__resume_cpu(void);
49 DECLARE_PER_CPU(struct fpu
*, fpu_fpregs_owner_ctx
);
51 extern user_regset_active_fn regset_fpregs_active
, regset_xregset_fpregs_active
;
52 extern user_regset_get_fn fpregs_get
, xfpregs_get
, fpregs_soft_get
,
54 extern user_regset_set_fn fpregs_set
, xfpregs_set
, fpregs_soft_set
,
58 * xstateregs_active == regset_fpregs_active. Please refer to the comment
59 * at the definition of regset_fpregs_active.
61 #define xstateregs_active regset_fpregs_active
63 #ifdef CONFIG_MATH_EMULATION
64 extern void finit_soft_fpu(struct i387_soft_struct
*soft
);
66 static inline void finit_soft_fpu(struct i387_soft_struct
*soft
) {}
70 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
73 * This will disable any lazy FPU state restore of the current FPU state,
74 * but if the current thread owns the FPU, it will still be saved by.
76 static inline void __cpu_disable_lazy_restore(unsigned int cpu
)
78 per_cpu(fpu_fpregs_owner_ctx
, cpu
) = NULL
;
81 static inline int fpu_want_lazy_restore(struct fpu
*fpu
, unsigned int cpu
)
83 return fpu
== this_cpu_read_stable(fpu_fpregs_owner_ctx
) && cpu
== fpu
->last_cpu
;
86 #define X87_FSW_ES (1 << 7) /* Exception Summary */
88 static __always_inline __pure
bool use_eager_fpu(void)
90 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU
);
93 static __always_inline __pure
bool use_xsaveopt(void)
95 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT
);
98 static __always_inline __pure
bool use_xsave(void)
100 return static_cpu_has_safe(X86_FEATURE_XSAVE
);
103 static __always_inline __pure
bool use_fxsr(void)
105 return static_cpu_has_safe(X86_FEATURE_FXSR
);
108 static inline void fx_finit(struct i387_fxsave_struct
*fx
)
111 fx
->mxcsr
= MXCSR_DEFAULT
;
114 extern void fpstate_sanitize_xstate(struct fpu
*fpu
);
116 #define user_insn(insn, output, input...) \
119 asm volatile(ASM_STAC "\n" \
121 "2: " ASM_CLAC "\n" \
122 ".section .fixup,\"ax\"\n" \
123 "3: movl $-1,%[err]\n" \
126 _ASM_EXTABLE(1b, 3b) \
127 : [err] "=r" (err), output \
132 #define check_insn(insn, output, input...) \
135 asm volatile("1:" #insn "\n\t" \
137 ".section .fixup,\"ax\"\n" \
138 "3: movl $-1,%[err]\n" \
141 _ASM_EXTABLE(1b, 3b) \
142 : [err] "=r" (err), output \
147 static inline int fsave_user(struct i387_fsave_struct __user
*fx
)
149 return user_insn(fnsave
%[fx
]; fwait
, [fx
] "=m" (*fx
), "m" (*fx
));
152 static inline int fxsave_user(struct i387_fxsave_struct __user
*fx
)
154 if (config_enabled(CONFIG_X86_32
))
155 return user_insn(fxsave
%[fx
], [fx
] "=m" (*fx
), "m" (*fx
));
156 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
157 return user_insn(fxsaveq
%[fx
], [fx
] "=m" (*fx
), "m" (*fx
));
159 /* See comment in fpu_fxsave() below. */
160 return user_insn(rex64
/fxsave (%[fx
]), "=m" (*fx
), [fx
] "R" (fx
));
163 static inline int fxrstor_checking(struct i387_fxsave_struct
*fx
)
165 if (config_enabled(CONFIG_X86_32
))
166 return check_insn(fxrstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
167 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
168 return check_insn(fxrstorq
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
170 /* See comment in fpu_fxsave() below. */
171 return check_insn(rex64
/fxrstor (%[fx
]), "=m" (*fx
), [fx
] "R" (fx
),
175 static inline int fxrstor_user(struct i387_fxsave_struct __user
*fx
)
177 if (config_enabled(CONFIG_X86_32
))
178 return user_insn(fxrstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
179 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
180 return user_insn(fxrstorq
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
182 /* See comment in fpu_fxsave() below. */
183 return user_insn(rex64
/fxrstor (%[fx
]), "=m" (*fx
), [fx
] "R" (fx
),
187 static inline int frstor_checking(struct i387_fsave_struct
*fx
)
189 return check_insn(frstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
192 static inline int frstor_user(struct i387_fsave_struct __user
*fx
)
194 return user_insn(frstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
197 static inline void fpu_fxsave(struct fpu
*fpu
)
199 if (config_enabled(CONFIG_X86_32
))
200 asm volatile( "fxsave %[fx]" : [fx
] "=m" (fpu
->state
.fxsave
));
201 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
202 asm volatile("fxsaveq %[fx]" : [fx
] "=m" (fpu
->state
.fxsave
));
204 /* Using "rex64; fxsave %0" is broken because, if the memory
205 * operand uses any extended registers for addressing, a second
206 * REX prefix will be generated (to the assembler, rex64
207 * followed by semicolon is a separate instruction), and hence
208 * the 64-bitness is lost.
210 * Using "fxsaveq %0" would be the ideal choice, but is only
211 * supported starting with gas 2.16.
213 * Using, as a workaround, the properly prefixed form below
214 * isn't accepted by any binutils version so far released,
215 * complaining that the same type of prefix is used twice if
216 * an extended register is needed for addressing (fix submitted
217 * to mainline 2005-11-21).
219 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
221 * This, however, we can work around by forcing the compiler to
222 * select an addressing mode that doesn't require extended
225 asm volatile( "rex64/fxsave (%[fx])"
226 : "=m" (fpu
->state
.fxsave
)
227 : [fx
] "R" (&fpu
->state
.fxsave
));
232 * These must be called with preempt disabled. Returns
233 * 'true' if the FPU state is still intact and we can
234 * keep registers active.
236 * The legacy FNSAVE instruction cleared all FPU state
237 * unconditionally, so registers are essentially destroyed.
238 * Modern FPU state can be kept in registers, if there are
239 * no pending FP exceptions.
241 static inline int copy_fpregs_to_fpstate(struct fpu
*fpu
)
243 if (likely(use_xsave())) {
244 xsave_state(&fpu
->state
.xsave
);
248 if (likely(use_fxsr())) {
254 * Legacy FPU register saving, FNSAVE always clears FPU registers,
255 * so we have to mark them inactive:
257 asm volatile("fnsave %[fx]; fwait" : [fx
] "=m" (fpu
->state
.fsave
));
262 static inline int __copy_fpstate_to_fpregs(struct fpu
*fpu
)
265 return fpu_xrstor_checking(&fpu
->state
.xsave
);
267 return fxrstor_checking(&fpu
->state
.fxsave
);
269 return frstor_checking(&fpu
->state
.fsave
);
272 static inline int copy_fpstate_to_fpregs(struct fpu
*fpu
)
275 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
276 * pending. Clear the x87 state here by setting it to fixed values.
277 * "m" is a random variable that should be in L1.
279 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK
))) {
283 "fildl %P[addr]" /* set F?P to defined value */
284 : : [addr
] "m" (fpu
->fpregs_active
));
287 return __copy_fpstate_to_fpregs(fpu
);
291 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
292 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
295 static inline void __fpregs_activate_hw(void)
297 if (!use_eager_fpu())
301 static inline void __fpregs_deactivate_hw(void)
303 if (!use_eager_fpu())
307 /* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
308 static inline void __fpregs_deactivate(struct fpu
*fpu
)
310 fpu
->fpregs_active
= 0;
311 this_cpu_write(fpu_fpregs_owner_ctx
, NULL
);
314 /* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
315 static inline void __fpregs_activate(struct fpu
*fpu
)
317 fpu
->fpregs_active
= 1;
318 this_cpu_write(fpu_fpregs_owner_ctx
, fpu
);
322 * The question "does this thread have fpu access?"
323 * is slightly racy, since preemption could come in
324 * and revoke it immediately after the test.
326 * However, even in that very unlikely scenario,
327 * we can just assume we have FPU access - typically
328 * to save the FP state - we'll just take a #NM
329 * fault and get the FPU access back.
331 static inline int fpregs_active(void)
333 return current
->thread
.fpu
.fpregs_active
;
337 * Encapsulate the CR0.TS handling together with the
340 * These generally need preemption protection to work,
341 * do try to avoid using these on their own.
343 static inline void fpregs_activate(struct fpu
*fpu
)
345 __fpregs_activate_hw();
346 __fpregs_activate(fpu
);
349 static inline void fpregs_deactivate(struct fpu
*fpu
)
351 __fpregs_deactivate(fpu
);
352 __fpregs_deactivate_hw();
355 static inline void restore_init_xstate(void)
358 xrstor_state(&init_xstate_ctx
, -1);
360 fxrstor_checking(&init_xstate_ctx
.i387
);
364 * Definitions for the eXtended Control Register instructions
367 #define XCR_XFEATURE_ENABLED_MASK 0x00000000
369 static inline u64
xgetbv(u32 index
)
373 asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
374 : "=a" (eax
), "=d" (edx
)
376 return eax
+ ((u64
)edx
<< 32);
379 static inline void xsetbv(u32 index
, u64 value
)
382 u32 edx
= value
>> 32;
384 asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
385 : : "a" (eax
), "d" (edx
), "c" (index
));
389 * FPU state switching for scheduling.
391 * This is a two-stage process:
393 * - switch_fpu_prepare() saves the old state and
394 * sets the new state of the CR0.TS bit. This is
395 * done within the context of the old process.
397 * - switch_fpu_finish() restores the new state as
400 typedef struct { int preload
; } fpu_switch_t
;
402 static inline fpu_switch_t
403 switch_fpu_prepare(struct fpu
*old_fpu
, struct fpu
*new_fpu
, int cpu
)
408 * If the task has used the math, pre-load the FPU on xsave processors
409 * or if the past 5 consecutive context-switches used math.
411 fpu
.preload
= new_fpu
->fpstate_active
&&
412 (use_eager_fpu() || new_fpu
->counter
> 5);
414 if (old_fpu
->fpregs_active
) {
415 if (!copy_fpregs_to_fpstate(old_fpu
))
416 old_fpu
->last_cpu
= -1;
418 old_fpu
->last_cpu
= cpu
;
420 /* But leave fpu_fpregs_owner_ctx! */
421 old_fpu
->fpregs_active
= 0;
423 /* Don't change CR0.TS if we just switch! */
426 __fpregs_activate(new_fpu
);
427 prefetch(&new_fpu
->state
);
429 __fpregs_deactivate_hw();
432 old_fpu
->counter
= 0;
433 old_fpu
->last_cpu
= -1;
436 if (fpu_want_lazy_restore(new_fpu
, cpu
))
439 prefetch(&new_fpu
->state
);
440 fpregs_activate(new_fpu
);
447 * By the time this gets called, we've already cleared CR0.TS and
448 * given the process the FPU if we are going to preload the FPU
449 * state - all we need to do is to conditionally restore the register
452 static inline void switch_fpu_finish(struct fpu
*new_fpu
, fpu_switch_t fpu_switch
)
454 if (fpu_switch
.preload
) {
455 if (unlikely(copy_fpstate_to_fpregs(new_fpu
)))
461 * Signal frame handlers...
463 extern int copy_fpstate_to_sigframe(void __user
*buf
, void __user
*fx
, int size
);
466 * Needs to be preemption-safe.
468 * NOTE! user_fpu_begin() must be used only immediately before restoring
469 * the save state. It does not do any saving/restoring on its own. In
470 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
471 * the task can lose the FPU right after preempt_enable().
473 static inline void user_fpu_begin(void)
475 struct fpu
*fpu
= ¤t
->thread
.fpu
;
478 if (!fpregs_active())
479 fpregs_activate(fpu
);
484 * i387 state interaction
486 static inline unsigned short get_fpu_cwd(struct task_struct
*tsk
)
489 return tsk
->thread
.fpu
.state
.fxsave
.cwd
;
491 return (unsigned short)tsk
->thread
.fpu
.state
.fsave
.cwd
;
495 static inline unsigned short get_fpu_swd(struct task_struct
*tsk
)
498 return tsk
->thread
.fpu
.state
.fxsave
.swd
;
500 return (unsigned short)tsk
->thread
.fpu
.state
.fsave
.swd
;
504 static inline unsigned short get_fpu_mxcsr(struct task_struct
*tsk
)
507 return tsk
->thread
.fpu
.state
.fxsave
.mxcsr
;
509 return MXCSR_DEFAULT
;
513 #endif /* _ASM_X86_FPU_INTERNAL_H */