blob: 1b2fc5cf1963362a760eb5f8b4298108915de336 [file] [log] [blame]
Linus Torvalds1361b832012-02-21 13:19:22 -08001/*
2 * Copyright (C) 1994 Linus Torvalds
3 *
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
8 */
9
10#ifndef _FPU_INTERNAL_H
11#define _FPU_INTERNAL_H
12
13#include <linux/kernel_stat.h>
14#include <linux/regset.h>
Suresh Siddha050902c2012-07-24 16:05:27 -070015#include <linux/compat.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080016#include <linux/slab.h>
17#include <asm/asm.h>
18#include <asm/cpufeature.h>
19#include <asm/processor.h>
20#include <asm/sigcontext.h>
21#include <asm/user.h>
22#include <asm/uaccess.h>
23#include <asm/xsave.h>
H. Peter Anvin49b8c692012-09-21 17:18:44 -070024#include <asm/smap.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080025
Suresh Siddha72a671c2012-07-24 16:05:29 -070026#ifdef CONFIG_X86_64
27# include <asm/sigcontext32.h>
28# include <asm/user32.h>
Al Viro235b8022012-11-09 23:51:47 -050029struct ksignal;
30int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070031 compat_sigset_t *set, struct pt_regs *regs);
Al Viro235b8022012-11-09 23:51:47 -050032int ia32_setup_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070033 compat_sigset_t *set, struct pt_regs *regs);
34#else
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
39#endif
40
41extern unsigned int mxcsr_feature_mask;
Linus Torvalds1361b832012-02-21 13:19:22 -080042extern void fpu_init(void);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070043extern void eager_fpu_init(void);
Linus Torvalds1361b832012-02-21 13:19:22 -080044
45DECLARE_PER_CPU(struct task_struct *, fpu_owner_task);
46
Suresh Siddha72a671c2012-07-24 16:05:29 -070047extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
48 struct task_struct *tsk);
49extern void convert_to_fxsr(struct task_struct *tsk,
50 const struct user_i387_ia32_struct *env);
51
Linus Torvalds1361b832012-02-21 13:19:22 -080052extern user_regset_active_fn fpregs_active, xfpregs_active;
53extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
54 xstateregs_get;
55extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
56 xstateregs_set;
57
Linus Torvalds1361b832012-02-21 13:19:22 -080058/*
59 * xstateregs_active == fpregs_active. Please refer to the comment
60 * at the definition of fpregs_active.
61 */
62#define xstateregs_active fpregs_active
63
Linus Torvalds1361b832012-02-21 13:19:22 -080064#ifdef CONFIG_MATH_EMULATION
Suresh Siddha72a671c2012-07-24 16:05:29 -070065# define HAVE_HWFP (boot_cpu_data.hard_math)
Linus Torvalds1361b832012-02-21 13:19:22 -080066extern void finit_soft_fpu(struct i387_soft_struct *soft);
67#else
Suresh Siddha72a671c2012-07-24 16:05:29 -070068# define HAVE_HWFP 1
Linus Torvalds1361b832012-02-21 13:19:22 -080069static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
70#endif
71
Suresh Siddha050902c2012-07-24 16:05:27 -070072static inline int is_ia32_compat_frame(void)
73{
74 return config_enabled(CONFIG_IA32_EMULATION) &&
75 test_thread_flag(TIF_IA32);
76}
77
78static inline int is_ia32_frame(void)
79{
80 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
81}
82
83static inline int is_x32_frame(void)
84{
85 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
86}
87
Linus Torvalds1361b832012-02-21 13:19:22 -080088#define X87_FSW_ES (1 << 7) /* Exception Summary */
89
Suresh Siddha5d2bd702012-09-06 14:58:52 -070090static __always_inline __pure bool use_eager_fpu(void)
91{
92 return static_cpu_has(X86_FEATURE_EAGER_FPU);
93}
94
Linus Torvalds1361b832012-02-21 13:19:22 -080095static __always_inline __pure bool use_xsaveopt(void)
96{
97 return static_cpu_has(X86_FEATURE_XSAVEOPT);
98}
99
100static __always_inline __pure bool use_xsave(void)
101{
102 return static_cpu_has(X86_FEATURE_XSAVE);
103}
104
105static __always_inline __pure bool use_fxsr(void)
106{
107 return static_cpu_has(X86_FEATURE_FXSR);
108}
109
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700110static inline void fx_finit(struct i387_fxsave_struct *fx)
111{
112 memset(fx, 0, xstate_size);
113 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700114 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700115}
116
Linus Torvalds1361b832012-02-21 13:19:22 -0800117extern void __sanitize_i387_state(struct task_struct *);
118
119static inline void sanitize_i387_state(struct task_struct *tsk)
120{
121 if (!use_xsaveopt())
122 return;
123 __sanitize_i387_state(tsk);
124}
125
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700126#define user_insn(insn, output, input...) \
127({ \
128 int err; \
129 asm volatile(ASM_STAC "\n" \
130 "1:" #insn "\n\t" \
131 "2: " ASM_CLAC "\n" \
132 ".section .fixup,\"ax\"\n" \
133 "3: movl $-1,%[err]\n" \
134 " jmp 2b\n" \
135 ".previous\n" \
136 _ASM_EXTABLE(1b, 3b) \
137 : [err] "=r" (err), output \
138 : "0"(0), input); \
139 err; \
140})
Linus Torvalds1361b832012-02-21 13:19:22 -0800141
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700142#define check_insn(insn, output, input...) \
143({ \
144 int err; \
145 asm volatile("1:" #insn "\n\t" \
146 "2:\n" \
147 ".section .fixup,\"ax\"\n" \
148 "3: movl $-1,%[err]\n" \
149 " jmp 2b\n" \
150 ".previous\n" \
151 _ASM_EXTABLE(1b, 3b) \
152 : [err] "=r" (err), output \
153 : "0"(0), input); \
154 err; \
155})
Linus Torvalds1361b832012-02-21 13:19:22 -0800156
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700157static inline int fsave_user(struct i387_fsave_struct __user *fx)
158{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700159 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800160}
161
162static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
163{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700164 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700165 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700166 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700167 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800168
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700169 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700170 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800171}
172
Linus Torvalds1361b832012-02-21 13:19:22 -0800173static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
174{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700175 if (config_enabled(CONFIG_X86_32))
176 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
177 else if (config_enabled(CONFIG_AS_FXSAVEQ))
178 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800179
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700180 /* See comment in fpu_fxsave() below. */
181 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
182 "m" (*fx));
183}
184
H. Peter Anvine139e952012-09-25 15:42:18 -0700185static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
186{
187 if (config_enabled(CONFIG_X86_32))
188 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
189 else if (config_enabled(CONFIG_AS_FXSAVEQ))
190 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
191
192 /* See comment in fpu_fxsave() below. */
193 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
194 "m" (*fx));
195}
196
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700197static inline int frstor_checking(struct i387_fsave_struct *fx)
198{
199 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800200}
201
H. Peter Anvine139e952012-09-25 15:42:18 -0700202static inline int frstor_user(struct i387_fsave_struct __user *fx)
203{
204 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
205}
206
Linus Torvalds1361b832012-02-21 13:19:22 -0800207static inline void fpu_fxsave(struct fpu *fpu)
208{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700209 if (config_enabled(CONFIG_X86_32))
210 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
211 else if (config_enabled(CONFIG_AS_FXSAVEQ))
212 asm volatile("fxsaveq %0" : "=m" (fpu->state->fxsave));
213 else {
214 /* Using "rex64; fxsave %0" is broken because, if the memory
215 * operand uses any extended registers for addressing, a second
216 * REX prefix will be generated (to the assembler, rex64
217 * followed by semicolon is a separate instruction), and hence
218 * the 64-bitness is lost.
219 *
220 * Using "fxsaveq %0" would be the ideal choice, but is only
221 * supported starting with gas 2.16.
222 *
223 * Using, as a workaround, the properly prefixed form below
224 * isn't accepted by any binutils version so far released,
225 * complaining that the same type of prefix is used twice if
226 * an extended register is needed for addressing (fix submitted
227 * to mainline 2005-11-21).
228 *
229 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
230 *
231 * This, however, we can work around by forcing the compiler to
232 * select an addressing mode that doesn't require extended
233 * registers.
234 */
235 asm volatile( "rex64/fxsave (%[fx])"
236 : "=m" (fpu->state->fxsave)
237 : [fx] "R" (&fpu->state->fxsave));
238 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800239}
240
Linus Torvalds1361b832012-02-21 13:19:22 -0800241/*
242 * These must be called with preempt disabled. Returns
243 * 'true' if the FPU state is still intact.
244 */
245static inline int fpu_save_init(struct fpu *fpu)
246{
247 if (use_xsave()) {
248 fpu_xsave(fpu);
249
250 /*
251 * xsave header may indicate the init state of the FP.
252 */
253 if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
254 return 1;
255 } else if (use_fxsr()) {
256 fpu_fxsave(fpu);
257 } else {
258 asm volatile("fnsave %[fx]; fwait"
259 : [fx] "=m" (fpu->state->fsave));
260 return 0;
261 }
262
263 /*
264 * If exceptions are pending, we need to clear them so
265 * that we don't randomly get exceptions later.
266 *
267 * FIXME! Is this perhaps only true for the old-style
268 * irq13 case? Maybe we could leave the x87 state
269 * intact otherwise?
270 */
271 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
272 asm volatile("fnclex");
273 return 0;
274 }
275 return 1;
276}
277
278static inline int __save_init_fpu(struct task_struct *tsk)
279{
280 return fpu_save_init(&tsk->thread.fpu);
281}
282
Linus Torvalds1361b832012-02-21 13:19:22 -0800283static inline int fpu_restore_checking(struct fpu *fpu)
284{
285 if (use_xsave())
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700286 return fpu_xrstor_checking(&fpu->state->xsave);
287 else if (use_fxsr())
288 return fxrstor_checking(&fpu->state->fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800289 else
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700290 return frstor_checking(&fpu->state->fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800291}
292
293static inline int restore_fpu_checking(struct task_struct *tsk)
294{
295 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
296 is pending. Clear the x87 state here by setting it to fixed
297 values. "m" is a random variable that should be in L1 */
Linus Torvaldse23fe362014-01-11 19:15:52 -0800298 if (unlikely(static_cpu_has(X86_FEATURE_FXSAVE_LEAK))) {
299 asm volatile(
300 "fnclex\n\t"
301 "emms\n\t"
302 "fildl %P[addr]" /* set F?P to defined value */
303 : : [addr] "m" (tsk->thread.fpu.has_fpu));
304 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800305
306 return fpu_restore_checking(&tsk->thread.fpu);
307}
308
309/*
310 * Software FPU state helpers. Careful: these need to
311 * be preemption protection *and* they need to be
312 * properly paired with the CR0.TS changes!
313 */
314static inline int __thread_has_fpu(struct task_struct *tsk)
315{
316 return tsk->thread.fpu.has_fpu;
317}
318
319/* Must be paired with an 'stts' after! */
320static inline void __thread_clear_has_fpu(struct task_struct *tsk)
321{
322 tsk->thread.fpu.has_fpu = 0;
Alex Shic6ae41e2012-05-11 15:35:27 +0800323 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800324}
325
326/* Must be paired with a 'clts' before! */
327static inline void __thread_set_has_fpu(struct task_struct *tsk)
328{
329 tsk->thread.fpu.has_fpu = 1;
Alex Shic6ae41e2012-05-11 15:35:27 +0800330 this_cpu_write(fpu_owner_task, tsk);
Linus Torvalds1361b832012-02-21 13:19:22 -0800331}
332
333/*
334 * Encapsulate the CR0.TS handling together with the
335 * software flag.
336 *
337 * These generally need preemption protection to work,
338 * do try to avoid using these on their own.
339 */
340static inline void __thread_fpu_end(struct task_struct *tsk)
341{
342 __thread_clear_has_fpu(tsk);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700343 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700344 stts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800345}
346
347static inline void __thread_fpu_begin(struct task_struct *tsk)
348{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700349 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700350 clts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800351 __thread_set_has_fpu(tsk);
352}
353
Suresh Siddha304bced2012-08-24 14:13:02 -0700354static inline void __drop_fpu(struct task_struct *tsk)
355{
356 if (__thread_has_fpu(tsk)) {
357 /* Ignore delayed exceptions from user space */
358 asm volatile("1: fwait\n"
359 "2:\n"
360 _ASM_EXTABLE(1b, 2b));
361 __thread_fpu_end(tsk);
362 }
363}
364
365static inline void drop_fpu(struct task_struct *tsk)
366{
367 /*
368 * Forget coprocessor state..
369 */
370 preempt_disable();
371 tsk->fpu_counter = 0;
372 __drop_fpu(tsk);
Oleg Nesterov16329d32015-03-13 09:53:10 +0100373 clear_stopped_child_used_math(tsk);
Suresh Siddha304bced2012-08-24 14:13:02 -0700374 preempt_enable();
375}
376
377static inline void drop_init_fpu(struct task_struct *tsk)
378{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700379 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700380 drop_fpu(tsk);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700381 else {
382 if (use_xsave())
383 xrstor_state(init_xstate_buf, -1);
384 else
385 fxrstor_checking(&init_xstate_buf->i387);
386 }
Suresh Siddha304bced2012-08-24 14:13:02 -0700387}
388
Linus Torvalds1361b832012-02-21 13:19:22 -0800389/*
390 * FPU state switching for scheduling.
391 *
392 * This is a two-stage process:
393 *
394 * - switch_fpu_prepare() saves the old state and
395 * sets the new state of the CR0.TS bit. This is
396 * done within the context of the old process.
397 *
398 * - switch_fpu_finish() restores the new state as
399 * necessary.
400 */
401typedef struct { int preload; } fpu_switch_t;
402
403/*
Vincent Palatin644c1542012-11-30 12:15:32 -0800404 * Must be run with preemption disabled: this clears the fpu_owner_task,
405 * on this CPU.
Linus Torvalds1361b832012-02-21 13:19:22 -0800406 *
Vincent Palatin644c1542012-11-30 12:15:32 -0800407 * This will disable any lazy FPU state restore of the current FPU state,
408 * but if the current thread owns the FPU, it will still be saved by.
Linus Torvalds1361b832012-02-21 13:19:22 -0800409 */
Vincent Palatin644c1542012-11-30 12:15:32 -0800410static inline void __cpu_disable_lazy_restore(unsigned int cpu)
411{
412 per_cpu(fpu_owner_task, cpu) = NULL;
413}
414
Linus Torvalds1361b832012-02-21 13:19:22 -0800415static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu)
416{
Alex Shic6ae41e2012-05-11 15:35:27 +0800417 return new == this_cpu_read_stable(fpu_owner_task) &&
Linus Torvalds1361b832012-02-21 13:19:22 -0800418 cpu == new->thread.fpu.last_cpu;
419}
420
421static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu)
422{
423 fpu_switch_t fpu;
424
Suresh Siddha304bced2012-08-24 14:13:02 -0700425 /*
426 * If the task has used the math, pre-load the FPU on xsave processors
427 * or if the past 5 consecutive context-switches used math.
428 */
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700429 fpu.preload = tsk_used_math(new) && (use_eager_fpu() ||
Suresh Siddha304bced2012-08-24 14:13:02 -0700430 new->fpu_counter > 5);
Linus Torvalds1361b832012-02-21 13:19:22 -0800431 if (__thread_has_fpu(old)) {
432 if (!__save_init_fpu(old))
433 cpu = ~0;
434 old->thread.fpu.last_cpu = cpu;
435 old->thread.fpu.has_fpu = 0; /* But leave fpu_owner_task! */
436
437 /* Don't change CR0.TS if we just switch! */
438 if (fpu.preload) {
439 new->fpu_counter++;
440 __thread_set_has_fpu(new);
441 prefetch(new->thread.fpu.state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700442 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800443 stts();
444 } else {
445 old->fpu_counter = 0;
446 old->thread.fpu.last_cpu = ~0;
447 if (fpu.preload) {
448 new->fpu_counter++;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700449 if (!use_eager_fpu() && fpu_lazy_restore(new, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800450 fpu.preload = 0;
451 else
452 prefetch(new->thread.fpu.state);
453 __thread_fpu_begin(new);
454 }
455 }
456 return fpu;
457}
458
459/*
460 * By the time this gets called, we've already cleared CR0.TS and
461 * given the process the FPU if we are going to preload the FPU
462 * state - all we need to do is to conditionally restore the register
463 * state itself.
464 */
465static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
466{
467 if (fpu.preload) {
468 if (unlikely(restore_fpu_checking(new)))
Suresh Siddha304bced2012-08-24 14:13:02 -0700469 drop_init_fpu(new);
Linus Torvalds1361b832012-02-21 13:19:22 -0800470 }
471}
472
473/*
474 * Signal frame handlers...
475 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700476extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
477extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800478
Suresh Siddha72a671c2012-07-24 16:05:29 -0700479static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800480{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700481 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
482}
483
484static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
485{
486 void __user *buf_fx = buf;
487 int size = xstate_sigframe_size();
488
489 if (ia32_frame && use_fxsr()) {
490 buf_fx = buf + sizeof(struct i387_fsave_struct);
491 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800492 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700493
494 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800495}
496
497/*
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700498 * Need to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800499 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700500 * NOTE! user_fpu_begin() must be used only immediately before restoring
501 * it. This function does not do any save/restore on their own.
Linus Torvalds1361b832012-02-21 13:19:22 -0800502 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800503static inline void user_fpu_begin(void)
504{
505 preempt_disable();
506 if (!user_has_fpu())
507 __thread_fpu_begin(current);
508 preempt_enable();
509}
510
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700511static inline void __save_fpu(struct task_struct *tsk)
512{
513 if (use_xsave())
514 xsave_state(&tsk->thread.fpu.state->xsave, -1);
515 else
516 fpu_fxsave(&tsk->thread.fpu);
517}
518
Linus Torvalds1361b832012-02-21 13:19:22 -0800519/*
520 * These disable preemption on their own and are safe
521 */
522static inline void save_init_fpu(struct task_struct *tsk)
523{
524 WARN_ON_ONCE(!__thread_has_fpu(tsk));
Suresh Siddha304bced2012-08-24 14:13:02 -0700525
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700526 if (use_eager_fpu()) {
527 __save_fpu(tsk);
Suresh Siddha304bced2012-08-24 14:13:02 -0700528 return;
529 }
530
Linus Torvalds1361b832012-02-21 13:19:22 -0800531 preempt_disable();
532 __save_init_fpu(tsk);
533 __thread_fpu_end(tsk);
534 preempt_enable();
535}
536
Linus Torvalds1361b832012-02-21 13:19:22 -0800537/*
538 * i387 state interaction
539 */
540static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
541{
542 if (cpu_has_fxsr) {
543 return tsk->thread.fpu.state->fxsave.cwd;
544 } else {
545 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
546 }
547}
548
549static inline unsigned short get_fpu_swd(struct task_struct *tsk)
550{
551 if (cpu_has_fxsr) {
552 return tsk->thread.fpu.state->fxsave.swd;
553 } else {
554 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
555 }
556}
557
558static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
559{
560 if (cpu_has_xmm) {
561 return tsk->thread.fpu.state->fxsave.mxcsr;
562 } else {
563 return MXCSR_DEFAULT;
564 }
565}
566
567static bool fpu_allocated(struct fpu *fpu)
568{
569 return fpu->state != NULL;
570}
571
572static inline int fpu_alloc(struct fpu *fpu)
573{
574 if (fpu_allocated(fpu))
575 return 0;
576 fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
577 if (!fpu->state)
578 return -ENOMEM;
579 WARN_ON((unsigned long)fpu->state & 15);
580 return 0;
581}
582
583static inline void fpu_free(struct fpu *fpu)
584{
585 if (fpu->state) {
586 kmem_cache_free(task_xstate_cachep, fpu->state);
587 fpu->state = NULL;
588 }
589}
590
Suresh Siddha304bced2012-08-24 14:13:02 -0700591static inline void fpu_copy(struct task_struct *dst, struct task_struct *src)
Linus Torvalds1361b832012-02-21 13:19:22 -0800592{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700593 if (use_eager_fpu()) {
594 memset(&dst->thread.fpu.state->xsave, 0, xstate_size);
595 __save_fpu(dst);
Suresh Siddha304bced2012-08-24 14:13:02 -0700596 } else {
597 struct fpu *dfpu = &dst->thread.fpu;
598 struct fpu *sfpu = &src->thread.fpu;
599
600 unlazy_fpu(src);
601 memcpy(dfpu->state, sfpu->state, xstate_size);
602 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800603}
604
Suresh Siddha72a671c2012-07-24 16:05:29 -0700605static inline unsigned long
606alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
607 unsigned long *size)
608{
609 unsigned long frame_size = xstate_sigframe_size();
610
611 *buf_fx = sp = round_down(sp - frame_size, 64);
612 if (ia32_frame && use_fxsr()) {
613 frame_size += sizeof(struct i387_fsave_struct);
614 sp -= sizeof(struct i387_fsave_struct);
615 }
616
617 *size = frame_size;
618 return sp;
619}
Linus Torvalds1361b832012-02-21 13:19:22 -0800620
621#endif