blob: b1f5dd63cfebc8d9c3c70552fb49d4f1ec40a2b3 [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
Linus Torvalds1361b832012-02-21 13:19:22 -080013#include <linux/regset.h>
Suresh Siddha050902c2012-07-24 16:05:27 -070014#include <linux/compat.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080015#include <linux/slab.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020016
Linus Torvalds1361b832012-02-21 13:19:22 -080017#include <asm/user.h>
Ingo Molnarf89e32e2015-04-22 10:58:10 +020018#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080019#include <asm/xsave.h>
20
Suresh Siddha72a671c2012-07-24 16:05:29 -070021#ifdef CONFIG_X86_64
22# include <asm/sigcontext32.h>
23# include <asm/user32.h>
Al Viro235b8022012-11-09 23:51:47 -050024struct ksignal;
25int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070026 compat_sigset_t *set, struct pt_regs *regs);
Al Viro235b8022012-11-09 23:51:47 -050027int ia32_setup_frame(int sig, struct ksignal *ksig,
Suresh Siddha72a671c2012-07-24 16:05:29 -070028 compat_sigset_t *set, struct pt_regs *regs);
29#else
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
34#endif
35
36extern unsigned int mxcsr_feature_mask;
Ingo Molnar3a9c4b02015-04-03 13:16:51 +020037extern void fpu__cpu_init(void);
Suresh Siddha5d2bd702012-09-06 14:58:52 -070038extern void eager_fpu_init(void);
Linus Torvalds1361b832012-02-21 13:19:22 -080039
40DECLARE_PER_CPU(struct task_struct *, fpu_owner_task);
41
Suresh Siddha72a671c2012-07-24 16:05:29 -070042extern void convert_from_fxsr(struct user_i387_ia32_struct *env,
43 struct task_struct *tsk);
44extern void convert_to_fxsr(struct task_struct *tsk,
45 const struct user_i387_ia32_struct *env);
46
Linus Torvalds1361b832012-02-21 13:19:22 -080047extern user_regset_active_fn fpregs_active, xfpregs_active;
48extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
49 xstateregs_get;
50extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
51 xstateregs_set;
52
Linus Torvalds1361b832012-02-21 13:19:22 -080053/*
54 * xstateregs_active == fpregs_active. Please refer to the comment
55 * at the definition of fpregs_active.
56 */
57#define xstateregs_active fpregs_active
58
Linus Torvalds1361b832012-02-21 13:19:22 -080059#ifdef CONFIG_MATH_EMULATION
60extern void finit_soft_fpu(struct i387_soft_struct *soft);
61#else
62static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
63#endif
64
Rik van Riel1c927ee2015-02-06 15:02:01 -050065/*
66 * Must be run with preemption disabled: this clears the fpu_owner_task,
67 * on this CPU.
68 *
69 * This will disable any lazy FPU state restore of the current FPU state,
70 * but if the current thread owns the FPU, it will still be saved by.
71 */
72static inline void __cpu_disable_lazy_restore(unsigned int cpu)
73{
74 per_cpu(fpu_owner_task, cpu) = NULL;
75}
76
Rik van Riel33e03de2015-02-06 15:02:02 -050077/*
78 * Used to indicate that the FPU state in memory is newer than the FPU
79 * state in registers, and the FPU state should be reloaded next time the
80 * task is run. Only safe on the current task, or non-running tasks.
81 */
82static inline void task_disable_lazy_fpu_restore(struct task_struct *tsk)
83{
84 tsk->thread.fpu.last_cpu = ~0;
85}
86
Rik van Riel1c927ee2015-02-06 15:02:01 -050087static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu)
88{
89 return new == this_cpu_read_stable(fpu_owner_task) &&
90 cpu == new->thread.fpu.last_cpu;
91}
92
Suresh Siddha050902c2012-07-24 16:05:27 -070093static inline int is_ia32_compat_frame(void)
94{
95 return config_enabled(CONFIG_IA32_EMULATION) &&
96 test_thread_flag(TIF_IA32);
97}
98
99static inline int is_ia32_frame(void)
100{
101 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
102}
103
104static inline int is_x32_frame(void)
105{
106 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
107}
108
Linus Torvalds1361b832012-02-21 13:19:22 -0800109#define X87_FSW_ES (1 << 7) /* Exception Summary */
110
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700111static __always_inline __pure bool use_eager_fpu(void)
112{
Matt Flemingc6b40692014-03-27 15:10:40 -0700113 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700114}
115
Linus Torvalds1361b832012-02-21 13:19:22 -0800116static __always_inline __pure bool use_xsaveopt(void)
117{
Matt Flemingc6b40692014-03-27 15:10:40 -0700118 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800119}
120
121static __always_inline __pure bool use_xsave(void)
122{
Matt Flemingc6b40692014-03-27 15:10:40 -0700123 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800124}
125
126static __always_inline __pure bool use_fxsr(void)
127{
Matt Flemingc6b40692014-03-27 15:10:40 -0700128 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800129}
130
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700131static inline void fx_finit(struct i387_fxsave_struct *fx)
132{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700133 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700134 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700135}
136
Linus Torvalds1361b832012-02-21 13:19:22 -0800137extern void __sanitize_i387_state(struct task_struct *);
138
139static inline void sanitize_i387_state(struct task_struct *tsk)
140{
141 if (!use_xsaveopt())
142 return;
143 __sanitize_i387_state(tsk);
144}
145
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700146#define user_insn(insn, output, input...) \
147({ \
148 int err; \
149 asm volatile(ASM_STAC "\n" \
150 "1:" #insn "\n\t" \
151 "2: " ASM_CLAC "\n" \
152 ".section .fixup,\"ax\"\n" \
153 "3: movl $-1,%[err]\n" \
154 " jmp 2b\n" \
155 ".previous\n" \
156 _ASM_EXTABLE(1b, 3b) \
157 : [err] "=r" (err), output \
158 : "0"(0), input); \
159 err; \
160})
Linus Torvalds1361b832012-02-21 13:19:22 -0800161
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700162#define check_insn(insn, output, input...) \
163({ \
164 int err; \
165 asm volatile("1:" #insn "\n\t" \
166 "2:\n" \
167 ".section .fixup,\"ax\"\n" \
168 "3: movl $-1,%[err]\n" \
169 " jmp 2b\n" \
170 ".previous\n" \
171 _ASM_EXTABLE(1b, 3b) \
172 : [err] "=r" (err), output \
173 : "0"(0), input); \
174 err; \
175})
Linus Torvalds1361b832012-02-21 13:19:22 -0800176
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700177static inline int fsave_user(struct i387_fsave_struct __user *fx)
178{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700179 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800180}
181
182static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
183{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700184 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700185 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700186 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700187 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800188
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700189 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700190 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800191}
192
Linus Torvalds1361b832012-02-21 13:19:22 -0800193static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
194{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700195 if (config_enabled(CONFIG_X86_32))
196 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
197 else if (config_enabled(CONFIG_AS_FXSAVEQ))
198 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800199
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700200 /* See comment in fpu_fxsave() below. */
201 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
202 "m" (*fx));
203}
204
H. Peter Anvine139e952012-09-25 15:42:18 -0700205static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
206{
207 if (config_enabled(CONFIG_X86_32))
208 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
209 else if (config_enabled(CONFIG_AS_FXSAVEQ))
210 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
211
212 /* See comment in fpu_fxsave() below. */
213 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
214 "m" (*fx));
215}
216
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700217static inline int frstor_checking(struct i387_fsave_struct *fx)
218{
219 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800220}
221
H. Peter Anvine139e952012-09-25 15:42:18 -0700222static inline int frstor_user(struct i387_fsave_struct __user *fx)
223{
224 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
225}
226
Linus Torvalds1361b832012-02-21 13:19:22 -0800227static inline void fpu_fxsave(struct fpu *fpu)
228{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700229 if (config_enabled(CONFIG_X86_32))
230 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
231 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100232 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700233 else {
234 /* Using "rex64; fxsave %0" is broken because, if the memory
235 * operand uses any extended registers for addressing, a second
236 * REX prefix will be generated (to the assembler, rex64
237 * followed by semicolon is a separate instruction), and hence
238 * the 64-bitness is lost.
239 *
240 * Using "fxsaveq %0" would be the ideal choice, but is only
241 * supported starting with gas 2.16.
242 *
243 * Using, as a workaround, the properly prefixed form below
244 * isn't accepted by any binutils version so far released,
245 * complaining that the same type of prefix is used twice if
246 * an extended register is needed for addressing (fix submitted
247 * to mainline 2005-11-21).
248 *
249 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
250 *
251 * This, however, we can work around by forcing the compiler to
252 * select an addressing mode that doesn't require extended
253 * registers.
254 */
255 asm volatile( "rex64/fxsave (%[fx])"
256 : "=m" (fpu->state->fxsave)
257 : [fx] "R" (&fpu->state->fxsave));
258 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800259}
260
Linus Torvalds1361b832012-02-21 13:19:22 -0800261/*
262 * These must be called with preempt disabled. Returns
263 * 'true' if the FPU state is still intact.
264 */
265static inline int fpu_save_init(struct fpu *fpu)
266{
267 if (use_xsave()) {
268 fpu_xsave(fpu);
269
270 /*
271 * xsave header may indicate the init state of the FP.
272 */
273 if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
274 return 1;
275 } else if (use_fxsr()) {
276 fpu_fxsave(fpu);
277 } else {
278 asm volatile("fnsave %[fx]; fwait"
279 : [fx] "=m" (fpu->state->fsave));
280 return 0;
281 }
282
283 /*
284 * If exceptions are pending, we need to clear them so
285 * that we don't randomly get exceptions later.
286 *
287 * FIXME! Is this perhaps only true for the old-style
288 * irq13 case? Maybe we could leave the x87 state
289 * intact otherwise?
290 */
291 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
292 asm volatile("fnclex");
293 return 0;
294 }
295 return 1;
296}
297
298static inline int __save_init_fpu(struct task_struct *tsk)
299{
300 return fpu_save_init(&tsk->thread.fpu);
301}
302
Linus Torvalds1361b832012-02-21 13:19:22 -0800303static inline int fpu_restore_checking(struct fpu *fpu)
304{
305 if (use_xsave())
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700306 return fpu_xrstor_checking(&fpu->state->xsave);
307 else if (use_fxsr())
308 return fxrstor_checking(&fpu->state->fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800309 else
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700310 return frstor_checking(&fpu->state->fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800311}
312
313static inline int restore_fpu_checking(struct task_struct *tsk)
314{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100315 /*
316 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
317 * pending. Clear the x87 state here by setting it to fixed values.
318 * "m" is a random variable that should be in L1.
319 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200320 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800321 asm volatile(
322 "fnclex\n\t"
323 "emms\n\t"
324 "fildl %P[addr]" /* set F?P to defined value */
325 : : [addr] "m" (tsk->thread.fpu.has_fpu));
326 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800327
328 return fpu_restore_checking(&tsk->thread.fpu);
329}
330
331/*
332 * Software FPU state helpers. Careful: these need to
333 * be preemption protection *and* they need to be
334 * properly paired with the CR0.TS changes!
335 */
336static inline int __thread_has_fpu(struct task_struct *tsk)
337{
338 return tsk->thread.fpu.has_fpu;
339}
340
341/* Must be paired with an 'stts' after! */
342static inline void __thread_clear_has_fpu(struct task_struct *tsk)
343{
344 tsk->thread.fpu.has_fpu = 0;
Alex Shic6ae41e2012-05-11 15:35:27 +0800345 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800346}
347
348/* Must be paired with a 'clts' before! */
349static inline void __thread_set_has_fpu(struct task_struct *tsk)
350{
351 tsk->thread.fpu.has_fpu = 1;
Alex Shic6ae41e2012-05-11 15:35:27 +0800352 this_cpu_write(fpu_owner_task, tsk);
Linus Torvalds1361b832012-02-21 13:19:22 -0800353}
354
355/*
356 * Encapsulate the CR0.TS handling together with the
357 * software flag.
358 *
359 * These generally need preemption protection to work,
360 * do try to avoid using these on their own.
361 */
362static inline void __thread_fpu_end(struct task_struct *tsk)
363{
364 __thread_clear_has_fpu(tsk);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700365 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700366 stts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800367}
368
369static inline void __thread_fpu_begin(struct task_struct *tsk)
370{
Oleg Nesterov31d96332014-09-02 19:57:20 +0200371 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700372 clts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800373 __thread_set_has_fpu(tsk);
374}
375
Suresh Siddha304bced2012-08-24 14:13:02 -0700376static inline void drop_fpu(struct task_struct *tsk)
377{
378 /*
379 * Forget coprocessor state..
380 */
381 preempt_disable();
Ingo Molnarc0c28032015-04-22 09:52:56 +0200382 tsk->thread.fpu.counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100383
384 if (__thread_has_fpu(tsk)) {
385 /* Ignore delayed exceptions from user space */
386 asm volatile("1: fwait\n"
387 "2:\n"
388 _ASM_EXTABLE(1b, 2b));
389 __thread_fpu_end(tsk);
390 }
391
Oleg Nesterovf4c36862015-03-13 09:53:10 +0100392 clear_stopped_child_used_math(tsk);
Suresh Siddha304bced2012-08-24 14:13:02 -0700393 preempt_enable();
394}
395
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100396static inline void restore_init_xstate(void)
397{
398 if (use_xsave())
399 xrstor_state(init_xstate_buf, -1);
400 else
401 fxrstor_checking(&init_xstate_buf->i387);
402}
403
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100404/*
405 * Reset the FPU state in the eager case and drop it in the lazy case (later use
406 * will reinit it).
407 */
408static inline void fpu_reset_state(struct task_struct *tsk)
Suresh Siddha304bced2012-08-24 14:13:02 -0700409{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700410 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700411 drop_fpu(tsk);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100412 else
413 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700414}
415
Linus Torvalds1361b832012-02-21 13:19:22 -0800416/*
417 * FPU state switching for scheduling.
418 *
419 * This is a two-stage process:
420 *
421 * - switch_fpu_prepare() saves the old state and
422 * sets the new state of the CR0.TS bit. This is
423 * done within the context of the old process.
424 *
425 * - switch_fpu_finish() restores the new state as
426 * necessary.
427 */
428typedef struct { int preload; } fpu_switch_t;
429
Linus Torvalds1361b832012-02-21 13:19:22 -0800430static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu)
431{
432 fpu_switch_t fpu;
433
Suresh Siddha304bced2012-08-24 14:13:02 -0700434 /*
435 * If the task has used the math, pre-load the FPU on xsave processors
436 * or if the past 5 consecutive context-switches used math.
437 */
Rik van Riel1361ef22015-02-06 15:02:03 -0500438 fpu.preload = tsk_used_math(new) &&
Ingo Molnarc0c28032015-04-22 09:52:56 +0200439 (use_eager_fpu() || new->thread.fpu.counter > 5);
Rik van Riel1361ef22015-02-06 15:02:03 -0500440
Linus Torvalds1361b832012-02-21 13:19:22 -0800441 if (__thread_has_fpu(old)) {
442 if (!__save_init_fpu(old))
Rik van Riel6a5fe892015-02-06 15:02:04 -0500443 task_disable_lazy_fpu_restore(old);
Rik van Riel1361ef22015-02-06 15:02:03 -0500444 else
445 old->thread.fpu.last_cpu = cpu;
446
447 /* But leave fpu_owner_task! */
448 old->thread.fpu.has_fpu = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800449
450 /* Don't change CR0.TS if we just switch! */
451 if (fpu.preload) {
Ingo Molnarc0c28032015-04-22 09:52:56 +0200452 new->thread.fpu.counter++;
Linus Torvalds1361b832012-02-21 13:19:22 -0800453 __thread_set_has_fpu(new);
454 prefetch(new->thread.fpu.state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700455 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800456 stts();
457 } else {
Ingo Molnarc0c28032015-04-22 09:52:56 +0200458 old->thread.fpu.counter = 0;
Rik van Riel6a5fe892015-02-06 15:02:04 -0500459 task_disable_lazy_fpu_restore(old);
Linus Torvalds1361b832012-02-21 13:19:22 -0800460 if (fpu.preload) {
Ingo Molnarc0c28032015-04-22 09:52:56 +0200461 new->thread.fpu.counter++;
Rik van Riel728e53f2015-02-06 15:02:05 -0500462 if (fpu_lazy_restore(new, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800463 fpu.preload = 0;
464 else
465 prefetch(new->thread.fpu.state);
466 __thread_fpu_begin(new);
467 }
468 }
469 return fpu;
470}
471
472/*
473 * By the time this gets called, we've already cleared CR0.TS and
474 * given the process the FPU if we are going to preload the FPU
475 * state - all we need to do is to conditionally restore the register
476 * state itself.
477 */
478static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
479{
480 if (fpu.preload) {
481 if (unlikely(restore_fpu_checking(new)))
Borislav Petkovb85e67d2015-03-16 10:21:55 +0100482 fpu_reset_state(new);
Linus Torvalds1361b832012-02-21 13:19:22 -0800483 }
484}
485
486/*
487 * Signal frame handlers...
488 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700489extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
490extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800491
Suresh Siddha72a671c2012-07-24 16:05:29 -0700492static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800493{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700494 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
495}
496
497static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
498{
499 void __user *buf_fx = buf;
500 int size = xstate_sigframe_size();
501
502 if (ia32_frame && use_fxsr()) {
503 buf_fx = buf + sizeof(struct i387_fsave_struct);
504 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800505 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700506
507 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800508}
509
510/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100511 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800512 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700513 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100514 * the save state. It does not do any saving/restoring on its own. In
515 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
516 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800517 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800518static inline void user_fpu_begin(void)
519{
520 preempt_disable();
521 if (!user_has_fpu())
522 __thread_fpu_begin(current);
523 preempt_enable();
524}
525
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700526static inline void __save_fpu(struct task_struct *tsk)
527{
Fenghua Yuf41d8302014-05-29 11:12:41 -0700528 if (use_xsave()) {
529 if (unlikely(system_state == SYSTEM_BOOTING))
Ingo Molnar3e261c12015-04-22 15:08:34 +0200530 xsave_state_booting(&tsk->thread.fpu.state->xsave);
Fenghua Yuf41d8302014-05-29 11:12:41 -0700531 else
Ingo Molnar3e261c12015-04-22 15:08:34 +0200532 xsave_state(&tsk->thread.fpu.state->xsave);
Fenghua Yuf41d8302014-05-29 11:12:41 -0700533 } else
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700534 fpu_fxsave(&tsk->thread.fpu);
535}
536
Linus Torvalds1361b832012-02-21 13:19:22 -0800537/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800538 * 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
Ingo Molnared97b082015-04-03 12:41:14 +0200567extern int fpstate_alloc(struct fpu *fpu);
Linus Torvalds1361b832012-02-21 13:19:22 -0800568
Ingo Molnara7c2a832015-04-03 12:55:22 +0200569static inline void fpstate_free(struct fpu *fpu)
Linus Torvalds1361b832012-02-21 13:19:22 -0800570{
571 if (fpu->state) {
572 kmem_cache_free(task_xstate_cachep, fpu->state);
573 fpu->state = NULL;
574 }
575}
576
Suresh Siddha304bced2012-08-24 14:13:02 -0700577static inline void fpu_copy(struct task_struct *dst, struct task_struct *src)
Linus Torvalds1361b832012-02-21 13:19:22 -0800578{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700579 if (use_eager_fpu()) {
580 memset(&dst->thread.fpu.state->xsave, 0, xstate_size);
581 __save_fpu(dst);
Suresh Siddha304bced2012-08-24 14:13:02 -0700582 } else {
583 struct fpu *dfpu = &dst->thread.fpu;
584 struct fpu *sfpu = &src->thread.fpu;
585
Ingo Molnar0a781552015-04-03 10:58:52 +0200586 fpu__save(src);
Suresh Siddha304bced2012-08-24 14:13:02 -0700587 memcpy(dfpu->state, sfpu->state, xstate_size);
588 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800589}
590
Suresh Siddha72a671c2012-07-24 16:05:29 -0700591static inline unsigned long
592alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
593 unsigned long *size)
594{
595 unsigned long frame_size = xstate_sigframe_size();
596
597 *buf_fx = sp = round_down(sp - frame_size, 64);
598 if (ia32_frame && use_fxsr()) {
599 frame_size += sizeof(struct i387_fsave_struct);
600 sp -= sizeof(struct i387_fsave_struct);
601 }
602
603 *size = frame_size;
604 return sp;
605}
Linus Torvalds1361b832012-02-21 13:19:22 -0800606
607#endif