x86/fpu: Remove 'struct task_struct' usage from __thread_fpu_end()
[deliverable/linux.git] / arch / x86 / include / asm / fpu-internal.h
CommitLineData
1361b83a
LT
1/*
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
1361b83a 13#include <linux/regset.h>
050902c0 14#include <linux/compat.h>
1361b83a 15#include <linux/slab.h>
f89e32e0 16
1361b83a 17#include <asm/user.h>
f89e32e0 18#include <asm/i387.h>
1361b83a
LT
19#include <asm/xsave.h>
20
72a671ce
SS
21#ifdef CONFIG_X86_64
22# include <asm/sigcontext32.h>
23# include <asm/user32.h>
235b8022
AV
24struct ksignal;
25int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
72a671ce 26 compat_sigset_t *set, struct pt_regs *regs);
235b8022 27int ia32_setup_frame(int sig, struct ksignal *ksig,
72a671ce
SS
28 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;
3a9c4b0d 37extern void fpu__cpu_init(void);
5d2bd700 38extern void eager_fpu_init(void);
1361b83a 39
36b544dc 40DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
1361b83a 41
72a671ce
SS
42extern 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
1361b83a
LT
47extern 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
1361b83a
LT
53/*
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
1361b83a
LT
59#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
1c927eea 65/*
36b544dc 66 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
1c927eea
RR
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{
36b544dc 74 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
1c927eea
RR
75}
76
33e03ded
RR
77/*
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
1c927eea
RR
87static inline int fpu_lazy_restore(struct task_struct *new, unsigned int cpu)
88{
36b544dc 89 return &new->thread.fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) &&
1c927eea
RR
90 cpu == new->thread.fpu.last_cpu;
91}
92
050902c0
SS
93static 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
1361b83a
LT
109#define X87_FSW_ES (1 << 7) /* Exception Summary */
110
5d2bd700
SS
111static __always_inline __pure bool use_eager_fpu(void)
112{
c6b40691 113 return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
5d2bd700
SS
114}
115
1361b83a
LT
116static __always_inline __pure bool use_xsaveopt(void)
117{
c6b40691 118 return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
1361b83a
LT
119}
120
121static __always_inline __pure bool use_xsave(void)
122{
c6b40691 123 return static_cpu_has_safe(X86_FEATURE_XSAVE);
1361b83a
LT
124}
125
126static __always_inline __pure bool use_fxsr(void)
127{
c6b40691 128 return static_cpu_has_safe(X86_FEATURE_FXSR);
1361b83a
LT
129}
130
5d2bd700
SS
131static inline void fx_finit(struct i387_fxsave_struct *fx)
132{
5d2bd700 133 fx->cwd = 0x37f;
a8615af4 134 fx->mxcsr = MXCSR_DEFAULT;
5d2bd700
SS
135}
136
1361b83a
LT
137extern 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
49b8c695
PA
146#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})
161
0ca5bd0d
SS
162#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})
176
177static inline int fsave_user(struct i387_fsave_struct __user *fx)
1361b83a 178{
49b8c695 179 return user_insn(fnsave %[fx]; fwait, [fx] "=m" (*fx), "m" (*fx));
1361b83a
LT
180}
181
182static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
183{
0ca5bd0d 184 if (config_enabled(CONFIG_X86_32))
49b8c695 185 return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
0ca5bd0d 186 else if (config_enabled(CONFIG_AS_FXSAVEQ))
49b8c695 187 return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
1361b83a 188
0ca5bd0d 189 /* See comment in fpu_fxsave() below. */
49b8c695 190 return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
1361b83a
LT
191}
192
0ca5bd0d 193static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
1361b83a 194{
0ca5bd0d
SS
195 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));
1361b83a 199
0ca5bd0d
SS
200 /* See comment in fpu_fxsave() below. */
201 return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
202 "m" (*fx));
1361b83a
LT
203}
204
e139e955
PA
205static 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
0ca5bd0d 217static inline int frstor_checking(struct i387_fsave_struct *fx)
1361b83a 218{
0ca5bd0d 219 return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
e139e955
PA
220}
221
222static inline int frstor_user(struct i387_fsave_struct __user *fx)
223{
224 return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
1361b83a
LT
225}
226
227static inline void fpu_fxsave(struct fpu *fpu)
228{
0ca5bd0d
SS
229 if (config_enabled(CONFIG_X86_32))
230 asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state->fxsave));
231 else if (config_enabled(CONFIG_AS_FXSAVEQ))
6ca7a8a1 232 asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state->fxsave));
0ca5bd0d
SS
233 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 }
1361b83a
LT
259}
260
1361b83a
LT
261/*
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()) {
0afc4a94 268 xsave_state(&fpu->state->xsave);
1361b83a
LT
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
1361b83a
LT
298static inline int fpu_restore_checking(struct fpu *fpu)
299{
300 if (use_xsave())
0ca5bd0d
SS
301 return fpu_xrstor_checking(&fpu->state->xsave);
302 else if (use_fxsr())
303 return fxrstor_checking(&fpu->state->fxsave);
1361b83a 304 else
0ca5bd0d 305 return frstor_checking(&fpu->state->fsave);
1361b83a
LT
306}
307
308static inline int restore_fpu_checking(struct task_struct *tsk)
309{
6ca7a8a1
BP
310 /*
311 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
312 * pending. Clear the x87 state here by setting it to fixed values.
313 * "m" is a random variable that should be in L1.
314 */
9b13a93d 315 if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
26bef131
LT
316 asm volatile(
317 "fnclex\n\t"
318 "emms\n\t"
319 "fildl %P[addr]" /* set F?P to defined value */
320 : : [addr] "m" (tsk->thread.fpu.has_fpu));
321 }
1361b83a
LT
322
323 return fpu_restore_checking(&tsk->thread.fpu);
324}
325
1361b83a 326/* Must be paired with an 'stts' after! */
36fe6175 327static inline void __thread_clear_has_fpu(struct fpu *fpu)
1361b83a 328{
36fe6175 329 fpu->has_fpu = 0;
36b544dc 330 this_cpu_write(fpu_fpregs_owner_ctx, NULL);
1361b83a
LT
331}
332
333/* Must be paired with a 'clts' before! */
c0311f63 334static inline void __thread_set_has_fpu(struct fpu *fpu)
1361b83a 335{
c0311f63
IM
336 fpu->has_fpu = 1;
337 this_cpu_write(fpu_fpregs_owner_ctx, fpu);
1361b83a
LT
338}
339
340/*
341 * Encapsulate the CR0.TS handling together with the
342 * software flag.
343 *
344 * These generally need preemption protection to work,
345 * do try to avoid using these on their own.
346 */
35191e3f 347static inline void __thread_fpu_end(struct fpu *fpu)
1361b83a 348{
35191e3f 349 __thread_clear_has_fpu(fpu);
5d2bd700 350 if (!use_eager_fpu())
304bceda 351 stts();
1361b83a
LT
352}
353
354static inline void __thread_fpu_begin(struct task_struct *tsk)
355{
31d96338 356 if (!use_eager_fpu())
304bceda 357 clts();
c0311f63 358 __thread_set_has_fpu(&tsk->thread.fpu);
1361b83a
LT
359}
360
d2d0ac9a 361static inline void drop_fpu(struct task_struct *tsk)
304bceda 362{
276983f8 363 struct fpu *fpu = &tsk->thread.fpu;
d2d0ac9a
BP
364 /*
365 * Forget coprocessor state..
366 */
367 preempt_disable();
c0c2803d 368 tsk->thread.fpu.counter = 0;
d2d0ac9a 369
276983f8 370 if (fpu->has_fpu) {
304bceda
SS
371 /* Ignore delayed exceptions from user space */
372 asm volatile("1: fwait\n"
373 "2:\n"
374 _ASM_EXTABLE(1b, 2b));
35191e3f 375 __thread_fpu_end(fpu);
304bceda 376 }
304bceda 377
f4c36863 378 clear_stopped_child_used_math(tsk);
304bceda
SS
379 preempt_enable();
380}
381
8f4d8186
ON
382static inline void restore_init_xstate(void)
383{
384 if (use_xsave())
385 xrstor_state(init_xstate_buf, -1);
386 else
387 fxrstor_checking(&init_xstate_buf->i387);
388}
389
b85e67d1
BP
390/*
391 * Reset the FPU state in the eager case and drop it in the lazy case (later use
392 * will reinit it).
393 */
394static inline void fpu_reset_state(struct task_struct *tsk)
304bceda 395{
5d2bd700 396 if (!use_eager_fpu())
304bceda 397 drop_fpu(tsk);
8f4d8186
ON
398 else
399 restore_init_xstate();
304bceda
SS
400}
401
1361b83a
LT
402/*
403 * FPU state switching for scheduling.
404 *
405 * This is a two-stage process:
406 *
407 * - switch_fpu_prepare() saves the old state and
408 * sets the new state of the CR0.TS bit. This is
409 * done within the context of the old process.
410 *
411 * - switch_fpu_finish() restores the new state as
412 * necessary.
413 */
414typedef struct { int preload; } fpu_switch_t;
415
1361b83a
LT
416static inline fpu_switch_t switch_fpu_prepare(struct task_struct *old, struct task_struct *new, int cpu)
417{
276983f8 418 struct fpu *old_fpu = &old->thread.fpu;
c0311f63 419 struct fpu *new_fpu = &new->thread.fpu;
1361b83a
LT
420 fpu_switch_t fpu;
421
304bceda
SS
422 /*
423 * If the task has used the math, pre-load the FPU on xsave processors
424 * or if the past 5 consecutive context-switches used math.
425 */
1361ef29 426 fpu.preload = tsk_used_math(new) &&
c0c2803d 427 (use_eager_fpu() || new->thread.fpu.counter > 5);
1361ef29 428
276983f8 429 if (old_fpu->has_fpu) {
6522d783 430 if (!fpu_save_init(&old->thread.fpu))
6a5fe895 431 task_disable_lazy_fpu_restore(old);
1361ef29
RR
432 else
433 old->thread.fpu.last_cpu = cpu;
434
36b544dc 435 /* But leave fpu_fpregs_owner_ctx! */
1361ef29 436 old->thread.fpu.has_fpu = 0;
1361b83a
LT
437
438 /* Don't change CR0.TS if we just switch! */
439 if (fpu.preload) {
c0c2803d 440 new->thread.fpu.counter++;
c0311f63 441 __thread_set_has_fpu(new_fpu);
1361b83a 442 prefetch(new->thread.fpu.state);
5d2bd700 443 } else if (!use_eager_fpu())
1361b83a
LT
444 stts();
445 } else {
c0c2803d 446 old->thread.fpu.counter = 0;
6a5fe895 447 task_disable_lazy_fpu_restore(old);
1361b83a 448 if (fpu.preload) {
c0c2803d 449 new->thread.fpu.counter++;
728e53fe 450 if (fpu_lazy_restore(new, cpu))
1361b83a
LT
451 fpu.preload = 0;
452 else
453 prefetch(new->thread.fpu.state);
454 __thread_fpu_begin(new);
455 }
456 }
457 return fpu;
458}
459
460/*
461 * By the time this gets called, we've already cleared CR0.TS and
462 * given the process the FPU if we are going to preload the FPU
463 * state - all we need to do is to conditionally restore the register
464 * state itself.
465 */
466static inline void switch_fpu_finish(struct task_struct *new, fpu_switch_t fpu)
467{
468 if (fpu.preload) {
469 if (unlikely(restore_fpu_checking(new)))
b85e67d1 470 fpu_reset_state(new);
1361b83a
LT
471 }
472}
473
474/*
475 * Signal frame handlers...
476 */
72a671ce
SS
477extern int save_xstate_sig(void __user *buf, void __user *fx, int size);
478extern int __restore_xstate_sig(void __user *buf, void __user *fx, int size);
1361b83a 479
72a671ce 480static inline int xstate_sigframe_size(void)
1361b83a 481{
72a671ce
SS
482 return use_xsave() ? xstate_size + FP_XSTATE_MAGIC2_SIZE : xstate_size;
483}
484
485static inline int restore_xstate_sig(void __user *buf, int ia32_frame)
486{
487 void __user *buf_fx = buf;
488 int size = xstate_sigframe_size();
489
490 if (ia32_frame && use_fxsr()) {
491 buf_fx = buf + sizeof(struct i387_fsave_struct);
492 size += sizeof(struct i387_fsave_struct);
1361b83a 493 }
72a671ce
SS
494
495 return __restore_xstate_sig(buf, buf_fx, size);
1361b83a
LT
496}
497
498/*
fb14b4ea 499 * Needs to be preemption-safe.
1361b83a 500 *
377ffbcc 501 * NOTE! user_fpu_begin() must be used only immediately before restoring
fb14b4ea
ON
502 * the save state. It does not do any saving/restoring on its own. In
503 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
504 * the task can lose the FPU right after preempt_enable().
1361b83a 505 */
1361b83a
LT
506static inline void user_fpu_begin(void)
507{
508 preempt_disable();
509 if (!user_has_fpu())
510 __thread_fpu_begin(current);
511 preempt_enable();
512}
513
5d2bd700
SS
514static inline void __save_fpu(struct task_struct *tsk)
515{
f41d830f
FY
516 if (use_xsave()) {
517 if (unlikely(system_state == SYSTEM_BOOTING))
3e261c14 518 xsave_state_booting(&tsk->thread.fpu.state->xsave);
f41d830f 519 else
3e261c14 520 xsave_state(&tsk->thread.fpu.state->xsave);
f41d830f 521 } else
5d2bd700
SS
522 fpu_fxsave(&tsk->thread.fpu);
523}
524
1361b83a
LT
525/*
526 * i387 state interaction
527 */
528static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
529{
530 if (cpu_has_fxsr) {
531 return tsk->thread.fpu.state->fxsave.cwd;
532 } else {
533 return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
534 }
535}
536
537static inline unsigned short get_fpu_swd(struct task_struct *tsk)
538{
539 if (cpu_has_fxsr) {
540 return tsk->thread.fpu.state->fxsave.swd;
541 } else {
542 return (unsigned short)tsk->thread.fpu.state->fsave.swd;
543 }
544}
545
546static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
547{
548 if (cpu_has_xmm) {
549 return tsk->thread.fpu.state->fxsave.mxcsr;
550 } else {
551 return MXCSR_DEFAULT;
552 }
553}
554
8ffb53ab
IM
555extern void fpstate_cache_init(void);
556
ed97b085 557extern int fpstate_alloc(struct fpu *fpu);
5a12bf63 558extern void fpstate_free(struct fpu *fpu);
a752b53d 559extern int fpu__copy(struct task_struct *dst, struct task_struct *src);
1361b83a 560
72a671ce
SS
561static inline unsigned long
562alloc_mathframe(unsigned long sp, int ia32_frame, unsigned long *buf_fx,
563 unsigned long *size)
564{
565 unsigned long frame_size = xstate_sigframe_size();
566
567 *buf_fx = sp = round_down(sp - frame_size, 64);
568 if (ia32_frame && use_fxsr()) {
569 frame_size += sizeof(struct i387_fsave_struct);
570 sp -= sizeof(struct i387_fsave_struct);
571 }
572
573 *size = frame_size;
574 return sp;
575}
1361b83a
LT
576
577#endif
This page took 0.177806 seconds and 5 git commands to generate.