blob: 409b9ccf551837f2df045a411d584a134bf0e478 [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>
29int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
30 compat_sigset_t *set, struct pt_regs *regs);
31int ia32_setup_frame(int sig, struct k_sigaction *ka,
32 compat_sigset_t *set, struct pt_regs *regs);
33#else
34# define user_i387_ia32_struct user_i387_struct
35# define user32_fxsr_struct user_fxsr_struct
36# define ia32_setup_frame __setup_frame
37# define ia32_setup_rt_frame __setup_rt_frame
38#endif
39
40extern unsigned int mxcsr_feature_mask;
Linus Torvalds1361b832012-02-21 13:19:22 -080041extern void fpu_init(void);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070042extern void eager_fpu_init(void);
Linus Torvalds1361b832012-02-21 13:19:22 -080043
44DECLARE_PER_CPU(struct task_struct *, fpu_owner_task);
45
Suresh Siddha72a671c2012-07-24 16:05:29 -070046extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
47 struct task_struct *tsk);
48extern void convert_to_fxsr(struct task_struct *tsk,
49 const struct user_i387_ia32_struct *env);
50
Linus Torvalds1361b832012-02-21 13:19:22 -080051extern user_regset_active_fn fpregs_active, xfpregs_active;
52extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
53 xstateregs_get;
54extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
55 xstateregs_set;
56
Linus Torvalds1361b832012-02-21 13:19:22 -080057/*
58 * xstateregs_active == fpregs_active. Please refer to the comment
59 * at the definition of fpregs_active.
60 */
61#define xstateregs_active fpregs_active
62
Linus Torvalds1361b832012-02-21 13:19:22 -080063#ifdef CONFIG_MATH_EMULATION
Suresh Siddha72a671c2012-07-24 16:05:29 -070064# define HAVE_HWFP (boot_cpu_data.hard_math)
Linus Torvalds1361b832012-02-21 13:19:22 -080065extern void finit_soft_fpu(struct i387_soft_struct *soft);
66#else
Suresh Siddha72a671c2012-07-24 16:05:29 -070067# define HAVE_HWFP 1
Linus Torvalds1361b832012-02-21 13:19:22 -080068static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
69#endif
70
Suresh Siddha050902c2012-07-24 16:05:27 -070071static inline int is_ia32_compat_frame(void)
72{
73 return config_enabled(CONFIG_IA32_EMULATION) &&
74 test_thread_flag(TIF_IA32);
75}
76
77static inline int is_ia32_frame(void)
78{
79 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
80}
81
82static inline int is_x32_frame(void)
83{
84 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
85}
86
Linus Torvalds1361b832012-02-21 13:19:22 -080087#define X87_FSW_ES (1 << 7) /* Exception Summary */
88
Suresh Siddha5d2bd702012-09-06 14:58:52 -070089static __always_inline __pure bool use_eager_fpu(void)
90{
91 return static_cpu_has(X86_FEATURE_EAGER_FPU);
92}
93
Linus Torvalds1361b832012-02-21 13:19:22 -080094static __always_inline __pure bool use_xsaveopt(void)
95{
96 return static_cpu_has(X86_FEATURE_XSAVEOPT);
97}
98
99static __always_inline __pure bool use_xsave(void)
100{
101 return static_cpu_has(X86_FEATURE_XSAVE);
102}
103
104static __always_inline __pure bool use_fxsr(void)
105{
106 return static_cpu_has(X86_FEATURE_FXSR);
107}
108
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700109static inline void fx_finit(struct i387_fxsave_struct *fx)
110{
111 memset(fx, 0, xstate_size);
112 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700113 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700114}
115
Linus Torvalds1361b832012-02-21 13:19:22 -0800116extern void __sanitize_i387_state(struct task_struct *);
117
118static inline void sanitize_i387_state(struct task_struct *tsk)
119{
120 if (!use_xsaveopt())
121 return;
122 __sanitize_i387_state(tsk);
123}
124
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700125#define user_insn(insn, output, input...) \
126({ \
127 int err; \
128 asm volatile(ASM_STAC "\n" \
129 "1:" #insn "\n\t" \
130 "2: " ASM_CLAC "\n" \
131 ".section .fixup,\"ax\"\n" \
132 "3: movl $-1,%[err]\n" \
133 " jmp 2b\n" \
134 ".previous\n" \
135 _ASM_EXTABLE(1b, 3b) \
136 : [err] "=r" (err), output \
137 : "0"(0), input); \
138 err; \
139})
Linus Torvalds1361b832012-02-21 13:19:22 -0800140
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700141#define check_insn(insn, output, input...) \
142({ \
143 int err; \
144 asm volatile("1:" #insn "\n\t" \
145 "2:\n" \
146 ".section .fixup,\"ax\"\n" \
147 "3: movl $-1,%[err]\n" \
148 " jmp 2b\n" \
149 ".previous\n" \
150 _ASM_EXTABLE(1b, 3b) \
151 : [err] "=r" (err), output \
152 : "0"(0), input); \
153 err; \
154})
Linus Torvalds1361b832012-02-21 13:19:22 -0800155
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700156static inline int fsave_user(struct i387_fsave_struct __user *fx)
157{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700158 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800159}
160
161static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
162{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700163 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700164 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700165 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700166 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800167
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700168 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700169 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800170}
171
Linus Torvalds1361b832012-02-21 13:19:22 -0800172static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
173{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700174 if (config_enabled(CONFIG_X86_32))
175 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
176 else if (config_enabled(CONFIG_AS_FXSAVEQ))
177 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800178
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700179 /* See comment in fpu_fxsave() below. */
180 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
181 "m" (*fx));
182}
183
184static inline int frstor_checking(struct i387_fsave_struct *fx)
185{
186 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800187}
188
189static inline void fpu_fxsave(struct fpu *fpu)
190{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700191 if (config_enabled(CONFIG_X86_32))
192 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
193 else if (config_enabled(CONFIG_AS_FXSAVEQ))
194 asm volatile("fxsaveq %0" : "=m" (fpu->state->fxsave));
195 else {
196 /* Using "rex64; fxsave %0" is broken because, if the memory
197 * operand uses any extended registers for addressing, a second
198 * REX prefix will be generated (to the assembler, rex64
199 * followed by semicolon is a separate instruction), and hence
200 * the 64-bitness is lost.
201 *
202 * Using "fxsaveq %0" would be the ideal choice, but is only
203 * supported starting with gas 2.16.
204 *
205 * Using, as a workaround, the properly prefixed form below
206 * isn't accepted by any binutils version so far released,
207 * complaining that the same type of prefix is used twice if
208 * an extended register is needed for addressing (fix submitted
209 * to mainline 2005-11-21).
210 *
211 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
212 *
213 * This, however, we can work around by forcing the compiler to
214 * select an addressing mode that doesn't require extended
215 * registers.
216 */
217 asm volatile( "rex64/fxsave (%[fx])"
218 : "=m" (fpu->state->fxsave)
219 : [fx] "R" (&fpu->state->fxsave));
220 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800221}
222
Linus Torvalds1361b832012-02-21 13:19:22 -0800223/*
224 * These must be called with preempt disabled. Returns
225 * 'true' if the FPU state is still intact.
226 */
227static inline int fpu_save_init(struct fpu *fpu)
228{
229 if (use_xsave()) {
230 fpu_xsave(fpu);
231
232 /*
233 * xsave header may indicate the init state of the FP.
234 */
235 if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
236 return 1;
237 } else if (use_fxsr()) {
238 fpu_fxsave(fpu);
239 } else {
240 asm volatile("fnsave %[fx]; fwait"
241 : [fx] "=m" (fpu->state->fsave));
242 return 0;
243 }
244
245 /*
246 * If exceptions are pending, we need to clear them so
247 * that we don't randomly get exceptions later.
248 *
249 * FIXME! Is this perhaps only true for the old-style
250 * irq13 case? Maybe we could leave the x87 state
251 * intact otherwise?
252 */
253 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
254 asm volatile("fnclex");
255 return 0;
256 }
257 return 1;
258}
259
260static inline int __save_init_fpu(struct task_struct *tsk)
261{
262 return fpu_save_init(&tsk->thread.fpu);
263}
264
Linus Torvalds1361b832012-02-21 13:19:22 -0800265static inline int fpu_restore_checking(struct fpu *fpu)
266{
267 if (use_xsave())
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700268 return fpu_xrstor_checking(&fpu->state->xsave);
269 else if (use_fxsr())
270 return fxrstor_checking(&fpu->state->fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800271 else
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700272 return frstor_checking(&fpu->state->fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800273}
274
275static inline int restore_fpu_checking(struct task_struct *tsk)
276{
277 /* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
278 is pending. Clear the x87 state here by setting it to fixed
279 values. "m" is a random variable that should be in L1 */
280 alternative_input(
281 ASM_NOP8 ASM_NOP2,
282 "emms\n\t" /* clear stack tags */
283 "fildl %P[addr]", /* set F?P to defined value */
284 X86_FEATURE_FXSAVE_LEAK,
285 [addr] "m" (tsk->thread.fpu.has_fpu));
286
287 return fpu_restore_checking(&tsk->thread.fpu);
288}
289
290/*
291 * Software FPU state helpers. Careful: these need to
292 * be preemption protection *and* they need to be
293 * properly paired with the CR0.TS changes!
294 */
295static inline int __thread_has_fpu(struct task_struct *tsk)
296{
297 return tsk->thread.fpu.has_fpu;
298}
299
300/* Must be paired with an 'stts' after! */
301static inline void __thread_clear_has_fpu(struct task_struct *tsk)
302{
303 tsk->thread.fpu.has_fpu = 0;
Alex Shic6ae41e2012-05-11 15:35:27 +0800304 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800305}
306
307/* Must be paired with a 'clts' before! */
308static inline void __thread_set_has_fpu(struct task_struct *tsk)
309{
310 tsk->thread.fpu.has_fpu = 1;
Alex Shic6ae41e2012-05-11 15:35:27 +0800311 this_cpu_write(fpu_owner_task, tsk);
Linus Torvalds1361b832012-02-21 13:19:22 -0800312}
313
314/*
315 * Encapsulate the CR0.TS handling together with the
316 * software flag.
317 *
318 * These generally need preemption protection to work,
319 * do try to avoid using these on their own.
320 */
321static inline void __thread_fpu_end(struct task_struct *tsk)
322{
323 __thread_clear_has_fpu(tsk);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700324 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700325 stts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800326}
327
328static inline void __thread_fpu_begin(struct task_struct *tsk)
329{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700330 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700331 clts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800332 __thread_set_has_fpu(tsk);
333}
334
Suresh Siddha304bced2012-08-24 14:13:02 -0700335static inline void __drop_fpu(struct task_struct *tsk)
336{
337 if (__thread_has_fpu(tsk)) {
338 /* Ignore delayed exceptions from user space */
339 asm volatile("1: fwait\n"
340 "2:\n"
341 _ASM_EXTABLE(1b, 2b));
342 __thread_fpu_end(tsk);
343 }
344}
345
346static inline void drop_fpu(struct task_struct *tsk)
347{
348 /*
349 * Forget coprocessor state..
350 */
351 preempt_disable();
352 tsk->fpu_counter = 0;
353 __drop_fpu(tsk);
354 clear_used_math();
355 preempt_enable();
356}
357
358static inline void drop_init_fpu(struct task_struct *tsk)
359{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700360 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700361 drop_fpu(tsk);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700362 else {
363 if (use_xsave())
364 xrstor_state(init_xstate_buf, -1);
365 else
366 fxrstor_checking(&init_xstate_buf->i387);
367 }
Suresh Siddha304bced2012-08-24 14:13:02 -0700368}
369
Linus Torvalds1361b832012-02-21 13:19:22 -0800370/*
371 * FPU state switching for scheduling.
372 *
373 * This is a two-stage process:
374 *
375 * - switch_fpu_prepare() saves the old state and
376 * sets the new state of the CR0.TS bit. This is
377 * done within the context of the old process.
378 *
379 * - switch_fpu_finish() restores the new state as
380 * necessary.
381 */
382typedef struct { int preload; } fpu_switch_t;
383
384/*
385 * FIXME! We could do a totally lazy restore, but we need to
386 * add a per-cpu "this was the task that last touched the FPU
387 * on this CPU" variable, and the task needs to have a "I last
388 * touched the FPU on this CPU" and check them.
389 *
390 * We don't do that yet, so "fpu_lazy_restore()" always returns
391 * false, but some day..
392 */
393static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu)
394{
Alex Shic6ae41e2012-05-11 15:35:27 +0800395 return new == this_cpu_read_stable(fpu_owner_task) &&
Linus Torvalds1361b832012-02-21 13:19:22 -0800396 cpu == new->thread.fpu.last_cpu;
397}
398
399static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu)
400{
401 fpu_switch_t fpu;
402
Suresh Siddha304bced2012-08-24 14:13:02 -0700403 /*
404 * If the task has used the math, pre-load the FPU on xsave processors
405 * or if the past 5 consecutive context-switches used math.
406 */
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700407 fpu.preload = tsk_used_math(new) && (use_eager_fpu() ||
Suresh Siddha304bced2012-08-24 14:13:02 -0700408 new->fpu_counter > 5);
Linus Torvalds1361b832012-02-21 13:19:22 -0800409 if (__thread_has_fpu(old)) {
410 if (!__save_init_fpu(old))
411 cpu = ~0;
412 old->thread.fpu.last_cpu = cpu;
413 old->thread.fpu.has_fpu = 0; /* But leave fpu_owner_task! */
414
415 /* Don't change CR0.TS if we just switch! */
416 if (fpu.preload) {
417 new->fpu_counter++;
418 __thread_set_has_fpu(new);
419 prefetch(new->thread.fpu.state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700420 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800421 stts();
422 } else {
423 old->fpu_counter = 0;
424 old->thread.fpu.last_cpu = ~0;
425 if (fpu.preload) {
426 new->fpu_counter++;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700427 if (!use_eager_fpu() && fpu_lazy_restore(new, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800428 fpu.preload = 0;
429 else
430 prefetch(new->thread.fpu.state);
431 __thread_fpu_begin(new);
432 }
433 }
434 return fpu;
435}
436
437/*
438 * By the time this gets called, we've already cleared CR0.TS and
439 * given the process the FPU if we are going to preload the FPU
440 * state - all we need to do is to conditionally restore the register
441 * state itself.
442 */
443static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
444{
445 if (fpu.preload) {
446 if (unlikely(restore_fpu_checking(new)))
Suresh Siddha304bced2012-08-24 14:13:02 -0700447 drop_init_fpu(new);
Linus Torvalds1361b832012-02-21 13:19:22 -0800448 }
449}
450
451/*
452 * Signal frame handlers...
453 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700454extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
455extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800456
Suresh Siddha72a671c2012-07-24 16:05:29 -0700457static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800458{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700459 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
460}
461
462static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
463{
464 void __user *buf_fx = buf;
465 int size = xstate_sigframe_size();
466
467 if (ia32_frame && use_fxsr()) {
468 buf_fx = buf + sizeof(struct i387_fsave_struct);
469 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800470 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700471
472 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800473}
474
475/*
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700476 * Need to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800477 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700478 * NOTE! user_fpu_begin() must be used only immediately before restoring
479 * it. This function does not do any save/restore on their own.
Linus Torvalds1361b832012-02-21 13:19:22 -0800480 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800481static inline void user_fpu_begin(void)
482{
483 preempt_disable();
484 if (!user_has_fpu())
485 __thread_fpu_begin(current);
486 preempt_enable();
487}
488
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700489static inline void __save_fpu(struct task_struct *tsk)
490{
491 if (use_xsave())
492 xsave_state(&tsk->thread.fpu.state->xsave, -1);
493 else
494 fpu_fxsave(&tsk->thread.fpu);
495}
496
Linus Torvalds1361b832012-02-21 13:19:22 -0800497/*
498 * These disable preemption on their own and are safe
499 */
500static inline void save_init_fpu(struct task_struct *tsk)
501{
502 WARN_ON_ONCE(!__thread_has_fpu(tsk));
Suresh Siddha304bced2012-08-24 14:13:02 -0700503
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700504 if (use_eager_fpu()) {
505 __save_fpu(tsk);
Suresh Siddha304bced2012-08-24 14:13:02 -0700506 return;
507 }
508
Linus Torvalds1361b832012-02-21 13:19:22 -0800509 preempt_disable();
510 __save_init_fpu(tsk);
511 __thread_fpu_end(tsk);
512 preempt_enable();
513}
514
Linus Torvalds1361b832012-02-21 13:19:22 -0800515/*
516 * i387 state interaction
517 */
518static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
519{
520 if (cpu_has_fxsr) {
521 return tsk->thread.fpu.state->fxsave.cwd;
522 } else {
523 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
524 }
525}
526
527static inline unsigned short get_fpu_swd(struct task_struct *tsk)
528{
529 if (cpu_has_fxsr) {
530 return tsk->thread.fpu.state->fxsave.swd;
531 } else {
532 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
533 }
534}
535
536static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
537{
538 if (cpu_has_xmm) {
539 return tsk->thread.fpu.state->fxsave.mxcsr;
540 } else {
541 return MXCSR_DEFAULT;
542 }
543}
544
545static bool fpu_allocated(struct fpu *fpu)
546{
547 return fpu->state != NULL;
548}
549
550static inline int fpu_alloc(struct fpu *fpu)
551{
552 if (fpu_allocated(fpu))
553 return 0;
554 fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
555 if (!fpu->state)
556 return -ENOMEM;
557 WARN_ON((unsigned long)fpu->state & 15);
558 return 0;
559}
560
561static inline void fpu_free(struct fpu *fpu)
562{
563 if (fpu->state) {
564 kmem_cache_free(task_xstate_cachep, fpu->state);
565 fpu->state = NULL;
566 }
567}
568
Suresh Siddha304bced2012-08-24 14:13:02 -0700569static inline void fpu_copy(struct task_struct *dst, struct task_struct *src)
Linus Torvalds1361b832012-02-21 13:19:22 -0800570{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700571 if (use_eager_fpu()) {
572 memset(&dst->thread.fpu.state->xsave, 0, xstate_size);
573 __save_fpu(dst);
Suresh Siddha304bced2012-08-24 14:13:02 -0700574 } else {
575 struct fpu *dfpu = &dst->thread.fpu;
576 struct fpu *sfpu = &src->thread.fpu;
577
578 unlazy_fpu(src);
579 memcpy(dfpu->state, sfpu->state, xstate_size);
580 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800581}
582
Suresh Siddha72a671c2012-07-24 16:05:29 -0700583static inline unsigned long
584alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
585 unsigned long *size)
586{
587 unsigned long frame_size = xstate_sigframe_size();
588
589 *buf_fx = sp = round_down(sp - frame_size, 64);
590 if (ia32_frame && use_fxsr()) {
591 frame_size += sizeof(struct i387_fsave_struct);
592 sp -= sizeof(struct i387_fsave_struct);
593 }
594
595 *size = frame_size;
596 return sp;
597}
Linus Torvalds1361b832012-02-21 13:19:22 -0800598
599#endif