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>
25 extern unsigned int sig_xstate_size;
26 extern void fpu_init(void);
28 DECLARE_PER_CPU(struct task_struct *, fpu_owner_task);
30 extern user_regset_active_fn fpregs_active, xfpregs_active;
31 extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
33 extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
37 * xstateregs_active == fpregs_active. Please refer to the comment
38 * at the definition of fpregs_active.
40 #define xstateregs_active fpregs_active
42 extern struct _fpx_sw_bytes fx_sw_reserved;
43 #ifdef CONFIG_IA32_EMULATION
44 extern unsigned int sig_xstate_ia32_size;
45 extern struct _fpx_sw_bytes fx_sw_reserved_ia32;
48 extern int save_i387_xstate_ia32(void __user *buf);
49 extern int restore_i387_xstate_ia32(void __user *buf);
52 #ifdef CONFIG_MATH_EMULATION
53 extern void finit_soft_fpu(struct i387_soft_struct *soft);
55 static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
58 static inline int is_ia32_compat_frame(void)
60 return config_enabled(CONFIG_IA32_EMULATION) &&
61 test_thread_flag(TIF_IA32);
64 static inline int is_ia32_frame(void)
66 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
69 static inline int is_x32_frame(void)
71 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
74 #define X87_FSW_ES (1 << 7) /* Exception Summary */
76 static __always_inline __pure bool use_xsaveopt(void)
78 return static_cpu_has(X86_FEATURE_XSAVEOPT);
81 static __always_inline __pure bool use_xsave(void)
83 return static_cpu_has(X86_FEATURE_XSAVE);
86 static __always_inline __pure bool use_fxsr(void)
88 return static_cpu_has(X86_FEATURE_FXSR);
91 extern void __sanitize_i387_state(struct task_struct *);
93 static inline void sanitize_i387_state(struct task_struct *tsk)
97 __sanitize_i387_state(tsk);
100 #define check_insn(insn, output, input...) \
103 asm volatile("1:" #insn "\n\t" \
105 ".section .fixup,\"ax\"\n" \
106 "3: movl $-1,%[err]\n" \
109 _ASM_EXTABLE(1b, 3b) \
110 : [err] "=r" (err), output \
115 static inline int fsave_user(struct i387_fsave_struct __user *fx)
117 return check_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
120 static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
125 * Clear the bytes not touched by the fxsave and reserved
128 err = __clear_user(&fx->sw_reserved,
129 sizeof(struct _fpx_sw_bytes));
133 if (config_enabled(CONFIG_X86_32))
134 return check_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
135 else if (config_enabled(CONFIG_AS_FXSAVEQ))
136 return check_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
138 /* See comment in fpu_fxsave() below. */
139 return check_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
142 static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
144 if (config_enabled(CONFIG_X86_32))
145 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
146 else if (config_enabled(CONFIG_AS_FXSAVEQ))
147 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
149 /* See comment in fpu_fxsave() below. */
150 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
154 static inline int frstor_checking(struct i387_fsave_struct *fx)
156 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
159 static inline void fpu_fxsave(struct fpu *fpu)
161 if (config_enabled(CONFIG_X86_32))
162 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
163 else if (config_enabled(CONFIG_AS_FXSAVEQ))
164 asm volatile("fxsaveq %0" : "=m" (fpu->state->fxsave));
166 /* Using "rex64; fxsave %0" is broken because, if the memory
167 * operand uses any extended registers for addressing, a second
168 * REX prefix will be generated (to the assembler, rex64
169 * followed by semicolon is a separate instruction), and hence
170 * the 64-bitness is lost.
172 * Using "fxsaveq %0" would be the ideal choice, but is only
173 * supported starting with gas 2.16.
175 * Using, as a workaround, the properly prefixed form below
176 * isn't accepted by any binutils version so far released,
177 * complaining that the same type of prefix is used twice if
178 * an extended register is needed for addressing (fix submitted
179 * to mainline 2005-11-21).
181 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
183 * This, however, we can work around by forcing the compiler to
184 * select an addressing mode that doesn't require extended
187 asm volatile( "rex64/fxsave (%[fx])"
188 : "=m" (fpu->state->fxsave)
189 : [fx] "R" (&fpu->state->fxsave));
194 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
195 compat_sigset_t *set, struct pt_regs *regs);
196 int ia32_setup_frame(int sig, struct k_sigaction *ka,
197 compat_sigset_t *set, struct pt_regs *regs);
199 #else /* CONFIG_X86_32 */
201 #define ia32_setup_frame __setup_frame
202 #define ia32_setup_rt_frame __setup_rt_frame
204 #endif /* CONFIG_X86_64 */
207 * These must be called with preempt disabled. Returns
208 * 'true' if the FPU state is still intact.
210 static inline int fpu_save_init(struct fpu *fpu)
216 * xsave header may indicate the init state of the FP.
218 if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
220 } else if (use_fxsr()) {
223 asm volatile("fnsave %[fx]; fwait"
224 : [fx] "=m" (fpu->state->fsave));
229 * If exceptions are pending, we need to clear them so
230 * that we don't randomly get exceptions later.
232 * FIXME! Is this perhaps only true for the old-style
233 * irq13 case? Maybe we could leave the x87 state
236 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
237 asm volatile("fnclex");
243 static inline int __save_init_fpu(struct task_struct *tsk)
245 return fpu_save_init(&tsk->thread.fpu);
248 static inline int fpu_restore_checking(struct fpu *fpu)
251 return fpu_xrstor_checking(&fpu->state->xsave);
253 return fxrstor_checking(&fpu->state->fxsave);
255 return frstor_checking(&fpu->state->fsave);
258 static inline int restore_fpu_checking(struct task_struct *tsk)
260 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
261 is pending. Clear the x87 state here by setting it to fixed
262 values. "m" is a random variable that should be in L1 */
265 "emms\n\t" /* clear stack tags */
266 "fildl %P[addr]", /* set F?P to defined value */
267 X86_FEATURE_FXSAVE_LEAK,
268 [addr] "m" (tsk->thread.fpu.has_fpu));
270 return fpu_restore_checking(&tsk->thread.fpu);
274 * Software FPU state helpers. Careful: these need to
275 * be preemption protection *and* they need to be
276 * properly paired with the CR0.TS changes!
278 static inline int __thread_has_fpu(struct task_struct *tsk)
280 return tsk->thread.fpu.has_fpu;
283 /* Must be paired with an 'stts' after! */
284 static inline void __thread_clear_has_fpu(struct task_struct *tsk)
286 tsk->thread.fpu.has_fpu = 0;
287 this_cpu_write(fpu_owner_task, NULL);
290 /* Must be paired with a 'clts' before! */
291 static inline void __thread_set_has_fpu(struct task_struct *tsk)
293 tsk->thread.fpu.has_fpu = 1;
294 this_cpu_write(fpu_owner_task, tsk);
298 * Encapsulate the CR0.TS handling together with the
301 * These generally need preemption protection to work,
302 * do try to avoid using these on their own.
304 static inline void __thread_fpu_end(struct task_struct *tsk)
306 __thread_clear_has_fpu(tsk);
310 static inline void __thread_fpu_begin(struct task_struct *tsk)
313 __thread_set_has_fpu(tsk);
317 * FPU state switching for scheduling.
319 * This is a two-stage process:
321 * - switch_fpu_prepare() saves the old state and
322 * sets the new state of the CR0.TS bit. This is
323 * done within the context of the old process.
325 * - switch_fpu_finish() restores the new state as
328 typedef struct { int preload; } fpu_switch_t;
331 * FIXME! We could do a totally lazy restore, but we need to
332 * add a per-cpu "this was the task that last touched the FPU
333 * on this CPU" variable, and the task needs to have a "I last
334 * touched the FPU on this CPU" and check them.
336 * We don't do that yet, so "fpu_lazy_restore()" always returns
337 * false, but some day..
339 static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu)
341 return new == this_cpu_read_stable(fpu_owner_task) &&
342 cpu == new->thread.fpu.last_cpu;
345 static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu)
349 fpu.preload = tsk_used_math(new) && new->fpu_counter > 5;
350 if (__thread_has_fpu(old)) {
351 if (!__save_init_fpu(old))
353 old->thread.fpu.last_cpu = cpu;
354 old->thread.fpu.has_fpu = 0; /* But leave fpu_owner_task! */
356 /* Don't change CR0.TS if we just switch! */
359 __thread_set_has_fpu(new);
360 prefetch(new->thread.fpu.state);
364 old->fpu_counter = 0;
365 old->thread.fpu.last_cpu = ~0;
368 if (fpu_lazy_restore(new, cpu))
371 prefetch(new->thread.fpu.state);
372 __thread_fpu_begin(new);
379 * By the time this gets called, we've already cleared CR0.TS and
380 * given the process the FPU if we are going to preload the FPU
381 * state - all we need to do is to conditionally restore the register
384 static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
387 if (unlikely(restore_fpu_checking(new)))
388 __thread_fpu_end(new);
393 * Signal frame handlers...
395 extern int save_i387_xstate(void __user *buf);
396 extern int restore_i387_xstate(void __user *buf);
398 static inline void __clear_fpu(struct task_struct *tsk)
400 if (__thread_has_fpu(tsk)) {
401 /* Ignore delayed exceptions from user space */
402 asm volatile("1: fwait\n"
404 _ASM_EXTABLE(1b, 2b));
405 __thread_fpu_end(tsk);
410 * The actual user_fpu_begin/end() functions
411 * need to be preemption-safe.
413 * NOTE! user_fpu_end() must be used only after you
414 * have saved the FP state, and user_fpu_begin() must
415 * be used only immediately before restoring it.
416 * These functions do not do any save/restore on
419 static inline void user_fpu_end(void)
422 __thread_fpu_end(current);
426 static inline void user_fpu_begin(void)
430 __thread_fpu_begin(current);
435 * These disable preemption on their own and are safe
437 static inline void save_init_fpu(struct task_struct *tsk)
439 WARN_ON_ONCE(!__thread_has_fpu(tsk));
441 __save_init_fpu(tsk);
442 __thread_fpu_end(tsk);
446 static inline void clear_fpu(struct task_struct *tsk)
454 * i387 state interaction
456 static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
459 return tsk->thread.fpu.state->fxsave.cwd;
461 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
465 static inline unsigned short get_fpu_swd(struct task_struct *tsk)
468 return tsk->thread.fpu.state->fxsave.swd;
470 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
474 static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
477 return tsk->thread.fpu.state->fxsave.mxcsr;
479 return MXCSR_DEFAULT;
483 static bool fpu_allocated(struct fpu *fpu)
485 return fpu->state != NULL;
488 static inline int fpu_alloc(struct fpu *fpu)
490 if (fpu_allocated(fpu))
492 fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
495 WARN_ON((unsigned long)fpu->state & 15);
499 static inline void fpu_free(struct fpu *fpu)
502 kmem_cache_free(task_xstate_cachep, fpu->state);
507 static inline void fpu_copy(struct fpu *dst, struct fpu *src)
509 memcpy(dst->state, src->state, xstate_size);
512 extern void fpu_finit(struct fpu *fpu);