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/slab.h>
18 #include <asm/fpu/api.h>
19 #include <asm/fpu/xsave.h>
22 # include <asm/sigcontext32.h>
23 # include <asm/user32.h>
25 int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
26 compat_sigset_t *set, struct pt_regs *regs);
27 int ia32_setup_frame(int sig, struct ksignal *ksig,
28 compat_sigset_t *set, struct pt_regs *regs);
30 # define user_i387_ia32_struct user_i387_struct
31 # define user32_fxsr_struct user_fxsr_struct
32 # define ia32_setup_frame __setup_frame
33 # define ia32_setup_rt_frame __setup_rt_frame
36 #define MXCSR_DEFAULT 0x1f80
38 extern unsigned int mxcsr_feature_mask;
39 extern void fpu__cpu_init(void);
40 extern void eager_fpu_init(void);
42 extern void fpu__init_system_xstate(void);
43 extern void fpu__init_cpu_xstate(void);
45 DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
47 extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
48 struct task_struct *tsk);
49 extern void convert_to_fxsr(struct task_struct *tsk,
50 const struct user_i387_ia32_struct *env);
52 extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active;
53 extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
55 extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
59 * xstateregs_active == regset_fpregs_active. Please refer to the comment
60 * at the definition of regset_fpregs_active.
62 #define xstateregs_active regset_fpregs_active
64 #ifdef CONFIG_MATH_EMULATION
65 extern void finit_soft_fpu(struct i387_soft_struct *soft);
67 static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
71 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
74 * This will disable any lazy FPU state restore of the current FPU state,
75 * but if the current thread owns the FPU, it will still be saved by.
77 static inline void __cpu_disable_lazy_restore(unsigned int cpu)
79 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
82 static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
84 return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
87 static inline int is_ia32_compat_frame(void)
89 return config_enabled(CONFIG_IA32_EMULATION) &&
90 test_thread_flag(TIF_IA32);
93 static inline int is_ia32_frame(void)
95 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
98 static inline int is_x32_frame(void)
100 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
103 #define X87_FSW_ES (1 << 7) /* Exception Summary */
105 static __always_inline __pure bool use_eager_fpu(void)
107 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
110 static __always_inline __pure bool use_xsaveopt(void)
112 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
115 static __always_inline __pure bool use_xsave(void)
117 return static_cpu_has_safe(X86_FEATURE_XSAVE);
120 static __always_inline __pure bool use_fxsr(void)
122 return static_cpu_has_safe(X86_FEATURE_FXSR);
125 static inline void fx_finit(struct i387_fxsave_struct *fx)
128 fx->mxcsr = MXCSR_DEFAULT;
131 extern void __sanitize_i387_state(struct task_struct *);
133 static inline void sanitize_i387_state(struct task_struct *tsk)
137 __sanitize_i387_state(tsk);
140 #define user_insn(insn, output, input...) \
143 asm volatile(ASM_STAC "\n" \
145 "2: " ASM_CLAC "\n" \
146 ".section .fixup,\"ax\"\n" \
147 "3: movl $-1,%[err]\n" \
150 _ASM_EXTABLE(1b, 3b) \
151 : [err] "=r" (err), output \
156 #define check_insn(insn, output, input...) \
159 asm volatile("1:" #insn "\n\t" \
161 ".section .fixup,\"ax\"\n" \
162 "3: movl $-1,%[err]\n" \
165 _ASM_EXTABLE(1b, 3b) \
166 : [err] "=r" (err), output \
171 static inline int fsave_user(struct i387_fsave_struct __user *fx)
173 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
176 static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
178 if (config_enabled(CONFIG_X86_32))
179 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
180 else if (config_enabled(CONFIG_AS_FXSAVEQ))
181 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
183 /* See comment in fpu_fxsave() below. */
184 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
187 static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
189 if (config_enabled(CONFIG_X86_32))
190 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
191 else if (config_enabled(CONFIG_AS_FXSAVEQ))
192 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
194 /* See comment in fpu_fxsave() below. */
195 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
199 static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
201 if (config_enabled(CONFIG_X86_32))
202 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
203 else if (config_enabled(CONFIG_AS_FXSAVEQ))
204 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
206 /* See comment in fpu_fxsave() below. */
207 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
211 static inline int frstor_checking(struct i387_fsave_struct *fx)
213 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
216 static inline int frstor_user(struct i387_fsave_struct __user *fx)
218 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
221 static inline void fpu_fxsave(struct fpu *fpu)
223 if (config_enabled(CONFIG_X86_32))
224 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
225 else if (config_enabled(CONFIG_AS_FXSAVEQ))
226 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
228 /* Using "rex64; fxsave %0" is broken because, if the memory
229 * operand uses any extended registers for addressing, a second
230 * REX prefix will be generated (to the assembler, rex64
231 * followed by semicolon is a separate instruction), and hence
232 * the 64-bitness is lost.
234 * Using "fxsaveq %0" would be the ideal choice, but is only
235 * supported starting with gas 2.16.
237 * Using, as a workaround, the properly prefixed form below
238 * isn't accepted by any binutils version so far released,
239 * complaining that the same type of prefix is used twice if
240 * an extended register is needed for addressing (fix submitted
241 * to mainline 2005-11-21).
243 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
245 * This, however, we can work around by forcing the compiler to
246 * select an addressing mode that doesn't require extended
249 asm volatile( "rex64/fxsave (%[fx])"
250 : "=m" (fpu->state->fxsave)
251 : [fx] "R" (&fpu->state->fxsave));
256 * These must be called with preempt disabled. Returns
257 * 'true' if the FPU state is still intact.
259 static inline int fpu_save_init(struct fpu *fpu)
262 xsave_state(&fpu->state->xsave);
265 * xsave header may indicate the init state of the FP.
267 if (!(fpu->state->xsave.header.xfeatures & XSTATE_FP))
269 } else if (use_fxsr()) {
272 asm volatile("fnsave %[fx]; fwait"
273 : [fx] "=m" (fpu->state->fsave));
278 * If exceptions are pending, we need to clear them so
279 * that we don't randomly get exceptions later.
281 * FIXME! Is this perhaps only true for the old-style
282 * irq13 case? Maybe we could leave the x87 state
285 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
286 asm volatile("fnclex");
292 static inline int fpu_restore_checking(struct fpu *fpu)
295 return fpu_xrstor_checking(&fpu->state->xsave);
297 return fxrstor_checking(&fpu->state->fxsave);
299 return frstor_checking(&fpu->state->fsave);
302 static inline int restore_fpu_checking(struct fpu *fpu)
305 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
306 * pending. Clear the x87 state here by setting it to fixed values.
307 * "m" is a random variable that should be in L1.
309 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
313 "fildl %P[addr]" /* set F?P to defined value */
314 : : [addr] "m" (fpu->fpregs_active));
317 return fpu_restore_checking(fpu);
320 /* Must be paired with an 'stts' after! */
321 static inline void __fpregs_deactivate(struct fpu *fpu)
323 fpu->fpregs_active = 0;
324 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
327 /* Must be paired with a 'clts' before! */
328 static inline void __fpregs_activate(struct fpu *fpu)
330 fpu->fpregs_active = 1;
331 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
335 * Encapsulate the CR0.TS handling together with the
338 * These generally need preemption protection to work,
339 * do try to avoid using these on their own.
341 static inline void fpregs_activate(struct fpu *fpu)
343 if (!use_eager_fpu())
345 __fpregs_activate(fpu);
348 static inline void fpregs_deactivate(struct fpu *fpu)
350 __fpregs_deactivate(fpu);
351 if (!use_eager_fpu())
355 static inline void drop_fpu(struct fpu *fpu)
358 * Forget coprocessor state..
363 if (fpu->fpregs_active) {
364 /* Ignore delayed exceptions from user space */
365 asm volatile("1: fwait\n"
367 _ASM_EXTABLE(1b, 2b));
368 fpregs_deactivate(fpu);
371 fpu->fpstate_active = 0;
376 static inline void restore_init_xstate(void)
379 xrstor_state(&init_xstate_ctx, -1);
381 fxrstor_checking(&init_xstate_ctx.i387);
385 * Reset the FPU state in the eager case and drop it in the lazy case (later use
388 static inline void fpu_reset_state(struct fpu *fpu)
390 if (!use_eager_fpu())
393 restore_init_xstate();
397 * FPU state switching for scheduling.
399 * This is a two-stage process:
401 * - switch_fpu_prepare() saves the old state and
402 * sets the new state of the CR0.TS bit. This is
403 * done within the context of the old process.
405 * - switch_fpu_finish() restores the new state as
408 typedef struct { int preload; } fpu_switch_t;
410 static inline fpu_switch_t
411 switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
416 * If the task has used the math, pre-load the FPU on xsave processors
417 * or if the past 5 consecutive context-switches used math.
419 fpu.preload = new_fpu->fpstate_active &&
420 (use_eager_fpu() || new_fpu->counter > 5);
422 if (old_fpu->fpregs_active) {
423 if (!fpu_save_init(old_fpu))
424 old_fpu->last_cpu = -1;
426 old_fpu->last_cpu = cpu;
428 /* But leave fpu_fpregs_owner_ctx! */
429 old_fpu->fpregs_active = 0;
431 /* Don't change CR0.TS if we just switch! */
434 __fpregs_activate(new_fpu);
435 prefetch(new_fpu->state);
436 } else if (!use_eager_fpu())
439 old_fpu->counter = 0;
440 old_fpu->last_cpu = -1;
443 if (fpu_want_lazy_restore(new_fpu, cpu))
446 prefetch(new_fpu->state);
447 fpregs_activate(new_fpu);
454 * By the time this gets called, we've already cleared CR0.TS and
455 * given the process the FPU if we are going to preload the FPU
456 * state - all we need to do is to conditionally restore the register
459 static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
461 if (fpu_switch.preload) {
462 if (unlikely(restore_fpu_checking(new_fpu)))
463 fpu_reset_state(new_fpu);
468 * Signal frame handlers...
470 extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
471 extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
473 static inline int xstate_sigframe_size(void)
475 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
478 static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
480 void __user *buf_fx = buf;
481 int size = xstate_sigframe_size();
483 if (ia32_frame && use_fxsr()) {
484 buf_fx = buf + sizeof(struct i387_fsave_struct);
485 size += sizeof(struct i387_fsave_struct);
488 return __restore_xstate_sig(buf, buf_fx, size);
492 * Needs to be preemption-safe.
494 * NOTE! user_fpu_begin() must be used only immediately before restoring
495 * the save state. It does not do any saving/restoring on its own. In
496 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
497 * the task can lose the FPU right after preempt_enable().
499 static inline void user_fpu_begin(void)
501 struct fpu *fpu = ¤t->thread.fpu;
505 fpregs_activate(fpu);
510 * i387 state interaction
512 static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
515 return tsk->thread.fpu.state->fxsave.cwd;
517 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
521 static inline unsigned short get_fpu_swd(struct task_struct *tsk)
524 return tsk->thread.fpu.state->fxsave.swd;
526 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
530 static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
533 return tsk->thread.fpu.state->fxsave.mxcsr;
535 return MXCSR_DEFAULT;
539 extern void fpstate_cache_init(void);
541 extern int fpstate_alloc(struct fpu *fpu);
542 extern void fpstate_free(struct fpu *fpu);
543 extern int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
545 static inline unsigned long
546 alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
549 unsigned long frame_size = xstate_sigframe_size();
551 *buf_fx = sp = round_down(sp - frame_size, 64);
552 if (ia32_frame && use_fxsr()) {
553 frame_size += sizeof(struct i387_fsave_struct);
554 sp -= sizeof(struct i387_fsave_struct);
561 #endif /* _ASM_X86_FPU_INTERNAL_H */