blob: 2d4adff428ac717c3dc1e9ad55c0c032ea19ae84 [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
65extern void finit_soft_fpu(struct i387_soft_struct *soft);
66#else
67static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
68#endif
69
Rik van Riel1c927ee2015-02-06 15:02:01 -050070/*
71 * Must be run with preemption disabled: this clears the fpu_owner_task,
72 * on this CPU.
73 *
74 * This will disable any lazy FPU state restore of the current FPU state,
75 * but if the current thread owns the FPU, it will still be saved by.
76 */
77static inline void __cpu_disable_lazy_restore(unsigned int cpu)
78{
79 per_cpu(fpu_owner_task, cpu) = NULL;
80}
81
Rik van Riel33e03de2015-02-06 15:02:02 -050082/*
83 * Used to indicate that the FPU state in memory is newer than the FPU
84 * state in registers, and the FPU state should be reloaded next time the
85 * task is run. Only safe on the current task, or non-running tasks.
86 */
87static inline void task_disable_lazy_fpu_restore(struct task_struct *tsk)
88{
89 tsk->thread.fpu.last_cpu = ~0;
90}
91
Rik van Riel1c927ee2015-02-06 15:02:01 -050092static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu)
93{
94 return new == this_cpu_read_stable(fpu_owner_task) &&
95 cpu == new->thread.fpu.last_cpu;
96}
97
Suresh Siddha050902c2012-07-24 16:05:27 -070098static inline int is_ia32_compat_frame(void)
99{
100 return config_enabled(CONFIG_IA32_EMULATION) &&
101 test_thread_flag(TIF_IA32);
102}
103
104static inline int is_ia32_frame(void)
105{
106 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
107}
108
109static inline int is_x32_frame(void)
110{
111 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
112}
113
Linus Torvalds1361b832012-02-21 13:19:22 -0800114#define X87_FSW_ES (1 << 7) /* Exception Summary */
115
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700116static __always_inline __pure bool use_eager_fpu(void)
117{
Matt Flemingc6b40692014-03-27 15:10:40 -0700118 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700119}
120
Linus Torvalds1361b832012-02-21 13:19:22 -0800121static __always_inline __pure bool use_xsaveopt(void)
122{
Matt Flemingc6b40692014-03-27 15:10:40 -0700123 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
Linus Torvalds1361b832012-02-21 13:19:22 -0800124}
125
126static __always_inline __pure bool use_xsave(void)
127{
Matt Flemingc6b40692014-03-27 15:10:40 -0700128 return static_cpu_has_safe(X86_FEATURE_XSAVE);
Linus Torvalds1361b832012-02-21 13:19:22 -0800129}
130
131static __always_inline __pure bool use_fxsr(void)
132{
Matt Flemingc6b40692014-03-27 15:10:40 -0700133 return static_cpu_has_safe(X86_FEATURE_FXSR);
Linus Torvalds1361b832012-02-21 13:19:22 -0800134}
135
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700136static inline void fx_finit(struct i387_fxsave_struct *fx)
137{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700138 fx->cwd = 0x37f;
Suresh Siddhaa8615af2012-09-10 10:40:08 -0700139 fx->mxcsr = MXCSR_DEFAULT;
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700140}
141
Linus Torvalds1361b832012-02-21 13:19:22 -0800142extern void __sanitize_i387_state(struct task_struct *);
143
144static inline void sanitize_i387_state(struct task_struct *tsk)
145{
146 if (!use_xsaveopt())
147 return;
148 __sanitize_i387_state(tsk);
149}
150
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700151#define user_insn(insn, output, input...) \
152({ \
153 int err; \
154 asm volatile(ASM_STAC "\n" \
155 "1:" #insn "\n\t" \
156 "2: " ASM_CLAC "\n" \
157 ".section .fixup,\"ax\"\n" \
158 "3: movl $-1,%[err]\n" \
159 " jmp 2b\n" \
160 ".previous\n" \
161 _ASM_EXTABLE(1b, 3b) \
162 : [err] "=r" (err), output \
163 : "0"(0), input); \
164 err; \
165})
Linus Torvalds1361b832012-02-21 13:19:22 -0800166
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700167#define check_insn(insn, output, input...) \
168({ \
169 int err; \
170 asm volatile("1:" #insn "\n\t" \
171 "2:\n" \
172 ".section .fixup,\"ax\"\n" \
173 "3: movl $-1,%[err]\n" \
174 " jmp 2b\n" \
175 ".previous\n" \
176 _ASM_EXTABLE(1b, 3b) \
177 : [err] "=r" (err), output \
178 : "0"(0), input); \
179 err; \
180})
Linus Torvalds1361b832012-02-21 13:19:22 -0800181
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700182static inline int fsave_user(struct i387_fsave_struct __user *fx)
183{
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700184 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800185}
186
187static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
188{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700189 if (config_enabled(CONFIG_X86_32))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700190 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700191 else if (config_enabled(CONFIG_AS_FXSAVEQ))
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700192 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800193
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700194 /* See comment in fpu_fxsave() below. */
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700195 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800196}
197
Linus Torvalds1361b832012-02-21 13:19:22 -0800198static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
199{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700200 if (config_enabled(CONFIG_X86_32))
201 return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
202 else if (config_enabled(CONFIG_AS_FXSAVEQ))
203 return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800204
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700205 /* See comment in fpu_fxsave() below. */
206 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
207 "m" (*fx));
208}
209
H. Peter Anvine139e952012-09-25 15:42:18 -0700210static inline int fxrstor_user(struct i387_fxsave_struct __user *fx)
211{
212 if (config_enabled(CONFIG_X86_32))
213 return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
214 else if (config_enabled(CONFIG_AS_FXSAVEQ))
215 return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
216
217 /* See comment in fpu_fxsave() below. */
218 return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
219 "m" (*fx));
220}
221
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700222static inline int frstor_checking(struct i387_fsave_struct *fx)
223{
224 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
Linus Torvalds1361b832012-02-21 13:19:22 -0800225}
226
H. Peter Anvine139e952012-09-25 15:42:18 -0700227static inline int frstor_user(struct i387_fsave_struct __user *fx)
228{
229 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
230}
231
Linus Torvalds1361b832012-02-21 13:19:22 -0800232static inline void fpu_fxsave(struct fpu *fpu)
233{
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700234 if (config_enabled(CONFIG_X86_32))
235 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
236 else if (config_enabled(CONFIG_AS_FXSAVEQ))
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100237 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700238 else {
239 /* Using "rex64; fxsave %0" is broken because, if the memory
240 * operand uses any extended registers for addressing, a second
241 * REX prefix will be generated (to the assembler, rex64
242 * followed by semicolon is a separate instruction), and hence
243 * the 64-bitness is lost.
244 *
245 * Using "fxsaveq %0" would be the ideal choice, but is only
246 * supported starting with gas 2.16.
247 *
248 * Using, as a workaround, the properly prefixed form below
249 * isn't accepted by any binutils version so far released,
250 * complaining that the same type of prefix is used twice if
251 * an extended register is needed for addressing (fix submitted
252 * to mainline 2005-11-21).
253 *
254 * asm volatile("rex64/fxsave %0" : "=m" (fpu->state->fxsave));
255 *
256 * This, however, we can work around by forcing the compiler to
257 * select an addressing mode that doesn't require extended
258 * registers.
259 */
260 asm volatile( "rex64/fxsave (%[fx])"
261 : "=m" (fpu->state->fxsave)
262 : [fx] "R" (&fpu->state->fxsave));
263 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800264}
265
Linus Torvalds1361b832012-02-21 13:19:22 -0800266/*
267 * These must be called with preempt disabled. Returns
268 * 'true' if the FPU state is still intact.
269 */
270static inline int fpu_save_init(struct fpu *fpu)
271{
272 if (use_xsave()) {
273 fpu_xsave(fpu);
274
275 /*
276 * xsave header may indicate the init state of the FP.
277 */
278 if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
279 return 1;
280 } else if (use_fxsr()) {
281 fpu_fxsave(fpu);
282 } else {
283 asm volatile("fnsave %[fx]; fwait"
284 : [fx] "=m" (fpu->state->fsave));
285 return 0;
286 }
287
288 /*
289 * If exceptions are pending, we need to clear them so
290 * that we don't randomly get exceptions later.
291 *
292 * FIXME! Is this perhaps only true for the old-style
293 * irq13 case? Maybe we could leave the x87 state
294 * intact otherwise?
295 */
296 if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES)) {
297 asm volatile("fnclex");
298 return 0;
299 }
300 return 1;
301}
302
303static inline int __save_init_fpu(struct task_struct *tsk)
304{
305 return fpu_save_init(&tsk->thread.fpu);
306}
307
Linus Torvalds1361b832012-02-21 13:19:22 -0800308static inline int fpu_restore_checking(struct fpu *fpu)
309{
310 if (use_xsave())
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700311 return fpu_xrstor_checking(&fpu->state->xsave);
312 else if (use_fxsr())
313 return fxrstor_checking(&fpu->state->fxsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800314 else
Suresh Siddha0ca5bd02012-07-24 16:05:28 -0700315 return frstor_checking(&fpu->state->fsave);
Linus Torvalds1361b832012-02-21 13:19:22 -0800316}
317
318static inline int restore_fpu_checking(struct task_struct *tsk)
319{
Borislav Petkov6ca7a8a2014-12-21 15:02:23 +0100320 /*
321 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
322 * pending. Clear the x87 state here by setting it to fixed values.
323 * "m" is a random variable that should be in L1.
324 */
Borislav Petkov9b13a932014-06-18 00:06:23 +0200325 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
Linus Torvalds26bef132014-01-11 19:15:52 -0800326 asm volatile(
327 "fnclex\n\t"
328 "emms\n\t"
329 "fildl %P[addr]" /* set F?P to defined value */
330 : : [addr] "m" (tsk->thread.fpu.has_fpu));
331 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800332
333 return fpu_restore_checking(&tsk->thread.fpu);
334}
335
336/*
337 * Software FPU state helpers. Careful: these need to
338 * be preemption protection *and* they need to be
339 * properly paired with the CR0.TS changes!
340 */
341static inline int __thread_has_fpu(struct task_struct *tsk)
342{
343 return tsk->thread.fpu.has_fpu;
344}
345
346/* Must be paired with an 'stts' after! */
347static inline void __thread_clear_has_fpu(struct task_struct *tsk)
348{
349 tsk->thread.fpu.has_fpu = 0;
Alex Shic6ae41e2012-05-11 15:35:27 +0800350 this_cpu_write(fpu_owner_task, NULL);
Linus Torvalds1361b832012-02-21 13:19:22 -0800351}
352
353/* Must be paired with a 'clts' before! */
354static inline void __thread_set_has_fpu(struct task_struct *tsk)
355{
356 tsk->thread.fpu.has_fpu = 1;
Alex Shic6ae41e2012-05-11 15:35:27 +0800357 this_cpu_write(fpu_owner_task, tsk);
Linus Torvalds1361b832012-02-21 13:19:22 -0800358}
359
360/*
361 * Encapsulate the CR0.TS handling together with the
362 * software flag.
363 *
364 * These generally need preemption protection to work,
365 * do try to avoid using these on their own.
366 */
367static inline void __thread_fpu_end(struct task_struct *tsk)
368{
369 __thread_clear_has_fpu(tsk);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700370 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700371 stts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800372}
373
374static inline void __thread_fpu_begin(struct task_struct *tsk)
375{
Oleg Nesterov31d96332014-09-02 19:57:20 +0200376 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700377 clts();
Linus Torvalds1361b832012-02-21 13:19:22 -0800378 __thread_set_has_fpu(tsk);
379}
380
Suresh Siddha304bced2012-08-24 14:13:02 -0700381static inline void drop_fpu(struct task_struct *tsk)
382{
383 /*
384 * Forget coprocessor state..
385 */
386 preempt_disable();
Vineet Guptac375f152013-11-12 15:08:46 -0800387 tsk->thread.fpu_counter = 0;
Borislav Petkovd2d0ac92015-03-14 11:52:12 +0100388
389 if (__thread_has_fpu(tsk)) {
390 /* Ignore delayed exceptions from user space */
391 asm volatile("1: fwait\n"
392 "2:\n"
393 _ASM_EXTABLE(1b, 2b));
394 __thread_fpu_end(tsk);
395 }
396
Oleg Nesterovf4c36862015-03-13 09:53:10 +0100397 clear_stopped_child_used_math(tsk);
Suresh Siddha304bced2012-08-24 14:13:02 -0700398 preempt_enable();
399}
400
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100401static inline void restore_init_xstate(void)
402{
403 if (use_xsave())
404 xrstor_state(init_xstate_buf, -1);
405 else
406 fxrstor_checking(&init_xstate_buf->i387);
407}
408
Suresh Siddha304bced2012-08-24 14:13:02 -0700409static inline void drop_init_fpu(struct task_struct *tsk)
410{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700411 if (!use_eager_fpu())
Suresh Siddha304bced2012-08-24 14:13:02 -0700412 drop_fpu(tsk);
Oleg Nesterov8f4d8182015-03-11 18:34:29 +0100413 else
414 restore_init_xstate();
Suresh Siddha304bced2012-08-24 14:13:02 -0700415}
416
Linus Torvalds1361b832012-02-21 13:19:22 -0800417/*
418 * FPU state switching for scheduling.
419 *
420 * This is a two-stage process:
421 *
422 * - switch_fpu_prepare() saves the old state and
423 * sets the new state of the CR0.TS bit. This is
424 * done within the context of the old process.
425 *
426 * - switch_fpu_finish() restores the new state as
427 * necessary.
428 */
429typedef struct { int preload; } fpu_switch_t;
430
Linus Torvalds1361b832012-02-21 13:19:22 -0800431static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu)
432{
433 fpu_switch_t fpu;
434
Suresh Siddha304bced2012-08-24 14:13:02 -0700435 /*
436 * If the task has used the math, pre-load the FPU on xsave processors
437 * or if the past 5 consecutive context-switches used math.
438 */
Rik van Riel1361ef22015-02-06 15:02:03 -0500439 fpu.preload = tsk_used_math(new) &&
440 (use_eager_fpu() || new->thread.fpu_counter > 5);
441
Linus Torvalds1361b832012-02-21 13:19:22 -0800442 if (__thread_has_fpu(old)) {
443 if (!__save_init_fpu(old))
Rik van Riel6a5fe892015-02-06 15:02:04 -0500444 task_disable_lazy_fpu_restore(old);
Rik van Riel1361ef22015-02-06 15:02:03 -0500445 else
446 old->thread.fpu.last_cpu = cpu;
447
448 /* But leave fpu_owner_task! */
449 old->thread.fpu.has_fpu = 0;
Linus Torvalds1361b832012-02-21 13:19:22 -0800450
451 /* Don't change CR0.TS if we just switch! */
452 if (fpu.preload) {
Vineet Guptac375f152013-11-12 15:08:46 -0800453 new->thread.fpu_counter++;
Linus Torvalds1361b832012-02-21 13:19:22 -0800454 __thread_set_has_fpu(new);
455 prefetch(new->thread.fpu.state);
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700456 } else if (!use_eager_fpu())
Linus Torvalds1361b832012-02-21 13:19:22 -0800457 stts();
458 } else {
Vineet Guptac375f152013-11-12 15:08:46 -0800459 old->thread.fpu_counter = 0;
Rik van Riel6a5fe892015-02-06 15:02:04 -0500460 task_disable_lazy_fpu_restore(old);
Linus Torvalds1361b832012-02-21 13:19:22 -0800461 if (fpu.preload) {
Vineet Guptac375f152013-11-12 15:08:46 -0800462 new->thread.fpu_counter++;
Rik van Riel728e53f2015-02-06 15:02:05 -0500463 if (fpu_lazy_restore(new, cpu))
Linus Torvalds1361b832012-02-21 13:19:22 -0800464 fpu.preload = 0;
465 else
466 prefetch(new->thread.fpu.state);
467 __thread_fpu_begin(new);
468 }
469 }
470 return fpu;
471}
472
473/*
474 * By the time this gets called, we've already cleared CR0.TS and
475 * given the process the FPU if we are going to preload the FPU
476 * state - all we need to do is to conditionally restore the register
477 * state itself.
478 */
479static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
480{
481 if (fpu.preload) {
482 if (unlikely(restore_fpu_checking(new)))
Suresh Siddha304bced2012-08-24 14:13:02 -0700483 drop_init_fpu(new);
Linus Torvalds1361b832012-02-21 13:19:22 -0800484 }
485}
486
487/*
488 * Signal frame handlers...
489 */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700490extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
491extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800492
Suresh Siddha72a671c2012-07-24 16:05:29 -0700493static inline int xstate_sigframe_size(void)
Linus Torvalds1361b832012-02-21 13:19:22 -0800494{
Suresh Siddha72a671c2012-07-24 16:05:29 -0700495 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
496}
497
498static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
499{
500 void __user *buf_fx = buf;
501 int size = xstate_sigframe_size();
502
503 if (ia32_frame && use_fxsr()) {
504 buf_fx = buf + sizeof(struct i387_fsave_struct);
505 size += sizeof(struct i387_fsave_struct);
Linus Torvalds1361b832012-02-21 13:19:22 -0800506 }
Suresh Siddha72a671c2012-07-24 16:05:29 -0700507
508 return __restore_xstate_sig(buf, buf_fx, size);
Linus Torvalds1361b832012-02-21 13:19:22 -0800509}
510
511/*
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100512 * Needs to be preemption-safe.
Linus Torvalds1361b832012-02-21 13:19:22 -0800513 *
Suresh Siddha377ffbc2012-08-24 14:12:58 -0700514 * NOTE! user_fpu_begin() must be used only immediately before restoring
Oleg Nesterovfb14b4e2015-03-11 18:34:09 +0100515 * the save state. It does not do any saving/restoring on its own. In
516 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
517 * the task can lose the FPU right after preempt_enable().
Linus Torvalds1361b832012-02-21 13:19:22 -0800518 */
Linus Torvalds1361b832012-02-21 13:19:22 -0800519static inline void user_fpu_begin(void)
520{
521 preempt_disable();
522 if (!user_has_fpu())
523 __thread_fpu_begin(current);
524 preempt_enable();
525}
526
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700527static inline void __save_fpu(struct task_struct *tsk)
528{
Fenghua Yuf41d8302014-05-29 11:12:41 -0700529 if (use_xsave()) {
530 if (unlikely(system_state == SYSTEM_BOOTING))
531 xsave_state_booting(&tsk->thread.fpu.state->xsave, -1);
532 else
533 xsave_state(&tsk->thread.fpu.state->xsave, -1);
534 } else
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700535 fpu_fxsave(&tsk->thread.fpu);
536}
537
Linus Torvalds1361b832012-02-21 13:19:22 -0800538/*
Linus Torvalds1361b832012-02-21 13:19:22 -0800539 * i387 state interaction
540 */
541static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
542{
543 if (cpu_has_fxsr) {
544 return tsk->thread.fpu.state->fxsave.cwd;
545 } else {
546 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
547 }
548}
549
550static inline unsigned short get_fpu_swd(struct task_struct *tsk)
551{
552 if (cpu_has_fxsr) {
553 return tsk->thread.fpu.state->fxsave.swd;
554 } else {
555 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
556 }
557}
558
559static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
560{
561 if (cpu_has_xmm) {
562 return tsk->thread.fpu.state->fxsave.mxcsr;
563 } else {
564 return MXCSR_DEFAULT;
565 }
566}
567
568static bool fpu_allocated(struct fpu *fpu)
569{
570 return fpu->state != NULL;
571}
572
573static inline int fpu_alloc(struct fpu *fpu)
574{
575 if (fpu_allocated(fpu))
576 return 0;
577 fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
578 if (!fpu->state)
579 return -ENOMEM;
580 WARN_ON((unsigned long)fpu->state & 15);
581 return 0;
582}
583
584static inline void fpu_free(struct fpu *fpu)
585{
586 if (fpu->state) {
587 kmem_cache_free(task_xstate_cachep, fpu->state);
588 fpu->state = NULL;
589 }
590}
591
Suresh Siddha304bced2012-08-24 14:13:02 -0700592static inline void fpu_copy(struct task_struct *dst, struct task_struct *src)
Linus Torvalds1361b832012-02-21 13:19:22 -0800593{
Suresh Siddha5d2bd702012-09-06 14:58:52 -0700594 if (use_eager_fpu()) {
595 memset(&dst->thread.fpu.state->xsave, 0, xstate_size);
596 __save_fpu(dst);
Suresh Siddha304bced2012-08-24 14:13:02 -0700597 } else {
598 struct fpu *dfpu = &dst->thread.fpu;
599 struct fpu *sfpu = &src->thread.fpu;
600
601 unlazy_fpu(src);
602 memcpy(dfpu->state, sfpu->state, xstate_size);
603 }
Linus Torvalds1361b832012-02-21 13:19:22 -0800604}
605
Suresh Siddha72a671c2012-07-24 16:05:29 -0700606static inline unsigned long
607alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
608 unsigned long *size)
609{
610 unsigned long frame_size = xstate_sigframe_size();
611
612 *buf_fx = sp = round_down(sp - frame_size, 64);
613 if (ia32_frame && use_fxsr()) {
614 frame_size += sizeof(struct i387_fsave_struct);
615 sp -= sizeof(struct i387_fsave_struct);
616 }
617
618 *size = frame_size;
619 return sp;
620}
Linus Torvalds1361b832012-02-21 13:19:22 -0800621
622#endif