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 _FPU_INTERNAL_H
11 #define _FPU_INTERNAL_H
13 #include <linux/kernel_stat.h>
14 #include <linux/regset.h>
15 #include <linux/compat.h>
16 #include <linux/slab.h>
18 #include <asm/cpufeature.h>
19 #include <asm/processor.h>
20 #include <asm/sigcontext.h>
22 #include <asm/uaccess.h>
23 #include <asm/xsave.h>
27 # include <asm/sigcontext32.h>
28 # include <asm/user32.h>
30 int ia32_setup_rt_frame(int sig
, struct ksignal
*ksig
,
31 compat_sigset_t
*set
, struct pt_regs
*regs
);
32 int ia32_setup_frame(int sig
, struct ksignal
*ksig
,
33 compat_sigset_t
*set
, struct pt_regs
*regs
);
35 # define user_i387_ia32_struct user_i387_struct
36 # define user32_fxsr_struct user_fxsr_struct
37 # define ia32_setup_frame __setup_frame
38 # define ia32_setup_rt_frame __setup_rt_frame
41 extern unsigned int mxcsr_feature_mask
;
42 extern void fpu_init(void);
43 extern void eager_fpu_init(void);
45 DECLARE_PER_CPU(struct task_struct
*, fpu_owner_task
);
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 fpregs_active
, xfpregs_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 == fpregs_active. Please refer to the comment
60 * at the definition of fpregs_active.
62 #define xstateregs_active 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
) {}
70 static inline int is_ia32_compat_frame(void)
72 return config_enabled(CONFIG_IA32_EMULATION
) &&
73 test_thread_flag(TIF_IA32
);
76 static inline int is_ia32_frame(void)
78 return config_enabled(CONFIG_X86_32
) || is_ia32_compat_frame();
81 static inline int is_x32_frame(void)
83 return config_enabled(CONFIG_X86_X32_ABI
) && test_thread_flag(TIF_X32
);
86 #define X87_FSW_ES (1 << 7) /* Exception Summary */
88 static __always_inline __pure
bool use_eager_fpu(void)
90 return static_cpu_has(X86_FEATURE_EAGER_FPU
);
93 static __always_inline __pure
bool use_xsaveopt(void)
95 return static_cpu_has(X86_FEATURE_XSAVEOPT
);
98 static __always_inline __pure
bool use_xsave(void)
100 return static_cpu_has(X86_FEATURE_XSAVE
);
103 static __always_inline __pure
bool use_fxsr(void)
105 return static_cpu_has(X86_FEATURE_FXSR
);
108 static inline void fx_finit(struct i387_fxsave_struct
*fx
)
110 memset(fx
, 0, xstate_size
);
112 fx
->mxcsr
= MXCSR_DEFAULT
;
115 extern void __sanitize_i387_state(struct task_struct
*);
117 static inline void sanitize_i387_state(struct task_struct
*tsk
)
121 __sanitize_i387_state(tsk
);
124 #define user_insn(insn, output, input...) \
127 asm volatile(ASM_STAC "\n" \
129 "2: " ASM_CLAC "\n" \
130 ".section .fixup,\"ax\"\n" \
131 "3: movl $-1,%[err]\n" \
134 _ASM_EXTABLE(1b, 3b) \
135 : [err] "=r" (err), output \
140 #define check_insn(insn, output, input...) \
143 asm volatile("1:" #insn "\n\t" \
145 ".section .fixup,\"ax\"\n" \
146 "3: movl $-1,%[err]\n" \
149 _ASM_EXTABLE(1b, 3b) \
150 : [err] "=r" (err), output \
155 static inline int fsave_user(struct i387_fsave_struct __user
*fx
)
157 return user_insn(fnsave
%[fx
]; fwait
, [fx
] "=m" (*fx
), "m" (*fx
));
160 static inline int fxsave_user(struct i387_fxsave_struct __user
*fx
)
162 if (config_enabled(CONFIG_X86_32
))
163 return user_insn(fxsave
%[fx
], [fx
] "=m" (*fx
), "m" (*fx
));
164 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
165 return user_insn(fxsaveq
%[fx
], [fx
] "=m" (*fx
), "m" (*fx
));
167 /* See comment in fpu_fxsave() below. */
168 return user_insn(rex64
/fxsave (%[fx
]), "=m" (*fx
), [fx
] "R" (fx
));
171 static inline int fxrstor_checking(struct i387_fxsave_struct
*fx
)
173 if (config_enabled(CONFIG_X86_32
))
174 return check_insn(fxrstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
175 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
176 return check_insn(fxrstorq
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
178 /* See comment in fpu_fxsave() below. */
179 return check_insn(rex64
/fxrstor (%[fx
]), "=m" (*fx
), [fx
] "R" (fx
),
183 static inline int fxrstor_user(struct i387_fxsave_struct __user
*fx
)
185 if (config_enabled(CONFIG_X86_32
))
186 return user_insn(fxrstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
187 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
188 return user_insn(fxrstorq
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
190 /* See comment in fpu_fxsave() below. */
191 return user_insn(rex64
/fxrstor (%[fx
]), "=m" (*fx
), [fx
] "R" (fx
),
195 static inline int frstor_checking(struct i387_fsave_struct
*fx
)
197 return check_insn(frstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
200 static inline int frstor_user(struct i387_fsave_struct __user
*fx
)
202 return user_insn(frstor
%[fx
], "=m" (*fx
), [fx
] "m" (*fx
));
205 static inline void fpu_fxsave(struct fpu
*fpu
)
207 if (config_enabled(CONFIG_X86_32
))
208 asm volatile( "fxsave %[fx]" : [fx
] "=m" (fpu
->state
->fxsave
));
209 else if (config_enabled(CONFIG_AS_FXSAVEQ
))
210 asm volatile("fxsaveq %0" : "=m" (fpu
->state
->fxsave
));
212 /* Using "rex64; fxsave %0" is broken because, if the memory
213 * operand uses any extended registers for addressing, a second
214 * REX prefix will be generated (to the assembler, rex64
215 * followed by semicolon is a separate instruction), and hence
216 * the 64-bitness is lost.
218 * Using "fxsaveq %0" would be the ideal choice, but is only
219 * supported starting with gas 2.16.
221 * Using, as a workaround, the properly prefixed form below
222 * isn't accepted by any binutils version so far released,
223 * complaining that the same type of prefix is used twice if
224 * an extended register is needed for addressing (fix submitted
225 * to mainline 2005-11-21).
227 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
229 * This, however, we can work around by forcing the compiler to
230 * select an addressing mode that doesn't require extended
233 asm volatile( "rex64/fxsave (%[fx])"
234 : "=m" (fpu
->state
->fxsave
)
235 : [fx
] "R" (&fpu
->state
->fxsave
));
240 * These must be called with preempt disabled. Returns
241 * 'true' if the FPU state is still intact.
243 static inline int fpu_save_init(struct fpu
*fpu
)
249 * xsave header may indicate the init state of the FP.
251 if (!(fpu
->state
->xsave
.xsave_hdr
.xstate_bv
& XSTATE_FP
))
253 } else if (use_fxsr()) {
256 asm volatile("fnsave %[fx]; fwait"
257 : [fx
] "=m" (fpu
->state
->fsave
));
262 * If exceptions are pending, we need to clear them so
263 * that we don't randomly get exceptions later.
265 * FIXME! Is this perhaps only true for the old-style
266 * irq13 case? Maybe we could leave the x87 state
269 if (unlikely(fpu
->state
->fxsave
.swd
& X87_FSW_ES
)) {
270 asm volatile("fnclex");
276 static inline int __save_init_fpu(struct task_struct
*tsk
)
278 return fpu_save_init(&tsk
->thread
.fpu
);
281 static inline int fpu_restore_checking(struct fpu
*fpu
)
284 return fpu_xrstor_checking(&fpu
->state
->xsave
);
286 return fxrstor_checking(&fpu
->state
->fxsave
);
288 return frstor_checking(&fpu
->state
->fsave
);
291 static inline int restore_fpu_checking(struct task_struct
*tsk
)
293 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
294 is pending. Clear the x87 state here by setting it to fixed
295 values. "m" is a random variable that should be in L1 */
298 "emms\n\t" /* clear stack tags */
299 "fildl %P[addr]", /* set F?P to defined value */
300 X86_FEATURE_FXSAVE_LEAK
,
301 [addr
] "m" (tsk
->thread
.fpu
.has_fpu
));
303 return fpu_restore_checking(&tsk
->thread
.fpu
);
307 * Software FPU state helpers. Careful: these need to
308 * be preemption protection *and* they need to be
309 * properly paired with the CR0.TS changes!
311 static inline int __thread_has_fpu(struct task_struct
*tsk
)
313 return tsk
->thread
.fpu
.has_fpu
;
316 /* Must be paired with an 'stts' after! */
317 static inline void __thread_clear_has_fpu(struct task_struct
*tsk
)
319 tsk
->thread
.fpu
.has_fpu
= 0;
320 this_cpu_write(fpu_owner_task
, NULL
);
323 /* Must be paired with a 'clts' before! */
324 static inline void __thread_set_has_fpu(struct task_struct
*tsk
)
326 tsk
->thread
.fpu
.has_fpu
= 1;
327 this_cpu_write(fpu_owner_task
, tsk
);
331 * Encapsulate the CR0.TS handling together with the
334 * These generally need preemption protection to work,
335 * do try to avoid using these on their own.
337 static inline void __thread_fpu_end(struct task_struct
*tsk
)
339 __thread_clear_has_fpu(tsk
);
340 if (!use_eager_fpu())
344 static inline void __thread_fpu_begin(struct task_struct
*tsk
)
346 if (!static_cpu_has_safe(X86_FEATURE_EAGER_FPU
))
348 __thread_set_has_fpu(tsk
);
351 static inline void __drop_fpu(struct task_struct
*tsk
)
353 if (__thread_has_fpu(tsk
)) {
354 /* Ignore delayed exceptions from user space */
355 asm volatile("1: fwait\n"
357 _ASM_EXTABLE(1b
, 2b
));
358 __thread_fpu_end(tsk
);
362 static inline void drop_fpu(struct task_struct
*tsk
)
365 * Forget coprocessor state..
368 tsk
->fpu_counter
= 0;
374 static inline void drop_init_fpu(struct task_struct
*tsk
)
376 if (!use_eager_fpu())
380 xrstor_state(init_xstate_buf
, -1);
382 fxrstor_checking(&init_xstate_buf
->i387
);
387 * FPU state switching for scheduling.
389 * This is a two-stage process:
391 * - switch_fpu_prepare() saves the old state and
392 * sets the new state of the CR0.TS bit. This is
393 * done within the context of the old process.
395 * - switch_fpu_finish() restores the new state as
398 typedef struct { int preload
; } fpu_switch_t
;
401 * Must be run with preemption disabled: this clears the fpu_owner_task,
404 * This will disable any lazy FPU state restore of the current FPU state,
405 * but if the current thread owns the FPU, it will still be saved by.
407 static inline void __cpu_disable_lazy_restore(unsigned int cpu
)
409 per_cpu(fpu_owner_task
, cpu
) = NULL
;
412 static inline int fpu_lazy_restore(struct task_struct
*new, unsigned int cpu
)
414 return new == this_cpu_read_stable(fpu_owner_task
) &&
415 cpu
== new->thread
.fpu
.last_cpu
;
418 static inline fpu_switch_t
switch_fpu_prepare(struct task_struct
*old
, struct task_struct
*new, int cpu
)
423 * If the task has used the math, pre-load the FPU on xsave processors
424 * or if the past 5 consecutive context-switches used math.
426 fpu
.preload
= tsk_used_math(new) && (use_eager_fpu() ||
427 new->fpu_counter
> 5);
428 if (__thread_has_fpu(old
)) {
429 if (!__save_init_fpu(old
))
431 old
->thread
.fpu
.last_cpu
= cpu
;
432 old
->thread
.fpu
.has_fpu
= 0; /* But leave fpu_owner_task! */
434 /* Don't change CR0.TS if we just switch! */
437 __thread_set_has_fpu(new);
438 prefetch(new->thread
.fpu
.state
);
439 } else if (!use_eager_fpu())
442 old
->fpu_counter
= 0;
443 old
->thread
.fpu
.last_cpu
= ~0;
446 if (!use_eager_fpu() && fpu_lazy_restore(new, cpu
))
449 prefetch(new->thread
.fpu
.state
);
450 __thread_fpu_begin(new);
457 * By the time this gets called, we've already cleared CR0.TS and
458 * given the process the FPU if we are going to preload the FPU
459 * state - all we need to do is to conditionally restore the register
462 static inline void switch_fpu_finish(struct task_struct
*new, fpu_switch_t fpu
)
465 if (unlikely(restore_fpu_checking(new)))
471 * Signal frame handlers...
473 extern int save_xstate_sig(void __user
*buf
, void __user
*fx
, int size
);
474 extern int __restore_xstate_sig(void __user
*buf
, void __user
*fx
, int size
);
476 static inline int xstate_sigframe_size(void)
478 return use_xsave() ? xstate_size
+ FP_XSTATE_MAGIC2_SIZE
: xstate_size
;
481 static inline int restore_xstate_sig(void __user
*buf
, int ia32_frame
)
483 void __user
*buf_fx
= buf
;
484 int size
= xstate_sigframe_size();
486 if (ia32_frame
&& use_fxsr()) {
487 buf_fx
= buf
+ sizeof(struct i387_fsave_struct
);
488 size
+= sizeof(struct i387_fsave_struct
);
491 return __restore_xstate_sig(buf
, buf_fx
, size
);
495 * Need to be preemption-safe.
497 * NOTE! user_fpu_begin() must be used only immediately before restoring
498 * it. This function does not do any save/restore on their own.
500 static inline void user_fpu_begin(void)
504 __thread_fpu_begin(current
);
508 static inline void __save_fpu(struct task_struct
*tsk
)
511 xsave_state(&tsk
->thread
.fpu
.state
->xsave
, -1);
513 fpu_fxsave(&tsk
->thread
.fpu
);
517 * These disable preemption on their own and are safe
519 static inline void save_init_fpu(struct task_struct
*tsk
)
521 WARN_ON_ONCE(!__thread_has_fpu(tsk
));
523 if (use_eager_fpu()) {
529 __save_init_fpu(tsk
);
530 __thread_fpu_end(tsk
);
535 * i387 state interaction
537 static inline unsigned short get_fpu_cwd(struct task_struct
*tsk
)
540 return tsk
->thread
.fpu
.state
->fxsave
.cwd
;
542 return (unsigned short)tsk
->thread
.fpu
.state
->fsave
.cwd
;
546 static inline unsigned short get_fpu_swd(struct task_struct
*tsk
)
549 return tsk
->thread
.fpu
.state
->fxsave
.swd
;
551 return (unsigned short)tsk
->thread
.fpu
.state
->fsave
.swd
;
555 static inline unsigned short get_fpu_mxcsr(struct task_struct
*tsk
)
558 return tsk
->thread
.fpu
.state
->fxsave
.mxcsr
;
560 return MXCSR_DEFAULT
;
564 static bool fpu_allocated(struct fpu
*fpu
)
566 return fpu
->state
!= NULL
;
569 static inline int fpu_alloc(struct fpu
*fpu
)
571 if (fpu_allocated(fpu
))
573 fpu
->state
= kmem_cache_alloc(task_xstate_cachep
, GFP_KERNEL
);
576 WARN_ON((unsigned long)fpu
->state
& 15);
580 static inline void fpu_free(struct fpu
*fpu
)
583 kmem_cache_free(task_xstate_cachep
, fpu
->state
);
588 static inline void fpu_copy(struct task_struct
*dst
, struct task_struct
*src
)
590 if (use_eager_fpu()) {
591 memset(&dst
->thread
.fpu
.state
->xsave
, 0, xstate_size
);
594 struct fpu
*dfpu
= &dst
->thread
.fpu
;
595 struct fpu
*sfpu
= &src
->thread
.fpu
;
598 memcpy(dfpu
->state
, sfpu
->state
, xstate_size
);
602 static inline unsigned long
603 alloc_mathframe(unsigned long sp
, int ia32_frame
, unsigned long *buf_fx
,
606 unsigned long frame_size
= xstate_sigframe_size();
608 *buf_fx
= sp
= round_down(sp
- frame_size
, 64);
609 if (ia32_frame
&& use_fxsr()) {
610 frame_size
+= sizeof(struct i387_fsave_struct
);
611 sp
-= sizeof(struct i387_fsave_struct
);